|
5 | 5 |
|
6 | 6 | use alloc::{string::String, vec::Vec}; |
7 | 7 |
|
8 | | -use crate::builder::{Error, Result}; |
| 8 | +use crate::builder::{ContentEncryptionAlgorithm, Error, Result}; |
9 | 9 | use aes_kw::Kek; |
10 | 10 | use const_oid::ObjectIdentifier; |
11 | 11 | use der::Any; |
@@ -37,7 +37,7 @@ use spki::AlgorithmIdentifierOwned; |
37 | 37 | /// |
38 | 38 | /// [RFC 5753 Section 8]: https://datatracker.ietf.org/doc/html/rfc5753#section-8 |
39 | 39 | /// [RFC 5753 Section 7.1.5]: https://datatracker.ietf.org/doc/html/rfc5753#section-7.1.5 |
40 | | -#[derive(Copy, Clone)] |
| 40 | +#[derive(Copy, Clone, Debug, PartialEq, Eq)] |
41 | 41 | pub enum KeyWrapAlgorithm { |
42 | 42 | /// id-aes128-wrap |
43 | 43 | Aes128, |
@@ -129,6 +129,18 @@ impl From<KeyWrapAlgorithm> for AlgorithmIdentifierOwned { |
129 | 129 | } |
130 | 130 | } |
131 | 131 | } |
| 132 | +impl From<ContentEncryptionAlgorithm> for KeyWrapAlgorithm { |
| 133 | + /// Convert a `ContentEncryptionAlgorithm` to a `KeyWrapAlgorithm`. |
| 134 | + /// |
| 135 | + /// Conversion is done matching encryption strength. |
| 136 | + fn from(ce_algo: ContentEncryptionAlgorithm) -> Self { |
| 137 | + match ce_algo { |
| 138 | + ContentEncryptionAlgorithm::Aes128Cbc => Self::Aes128, |
| 139 | + ContentEncryptionAlgorithm::Aes192Cbc => Self::Aes192, |
| 140 | + ContentEncryptionAlgorithm::Aes256Cbc => Self::Aes256, |
| 141 | + } |
| 142 | + } |
| 143 | +} |
132 | 144 |
|
133 | 145 | /// This struct can be used to perform key wrapping operation. |
134 | 146 | /// |
@@ -345,6 +357,21 @@ mod tests { |
345 | 357 | } |
346 | 358 | ) |
347 | 359 | } |
| 360 | + #[test] |
| 361 | + fn test_keywrapalgorithm_from_contentencryptionalgorithm() { |
| 362 | + assert_eq!( |
| 363 | + KeyWrapAlgorithm::from(ContentEncryptionAlgorithm::Aes128Cbc), |
| 364 | + KeyWrapAlgorithm::Aes128 |
| 365 | + ); |
| 366 | + assert_eq!( |
| 367 | + KeyWrapAlgorithm::from(ContentEncryptionAlgorithm::Aes192Cbc), |
| 368 | + KeyWrapAlgorithm::Aes192 |
| 369 | + ); |
| 370 | + assert_eq!( |
| 371 | + KeyWrapAlgorithm::from(ContentEncryptionAlgorithm::Aes256Cbc), |
| 372 | + KeyWrapAlgorithm::Aes256 |
| 373 | + ); |
| 374 | + } |
348 | 375 |
|
349 | 376 | #[test] |
350 | 377 | fn test_keywrapper_try_new() { |
|
0 commit comments