Skip to content

Commit 457281c

Browse files
committed
cms: ecc-kari support - add From<ContentEncryptionAlgorithm> for KeyWrapAlgorithm
1 parent a042e2c commit 457281c

File tree

1 file changed

+29
-2
lines changed
  • cms/src/builder/utils

1 file changed

+29
-2
lines changed

cms/src/builder/utils/kw.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
use alloc::{string::String, vec::Vec};
77

8-
use crate::builder::{Error, Result};
8+
use crate::builder::{ContentEncryptionAlgorithm, Error, Result};
99
use aes_kw::Kek;
1010
use const_oid::ObjectIdentifier;
1111
use der::Any;
@@ -37,7 +37,7 @@ use spki::AlgorithmIdentifierOwned;
3737
///
3838
/// [RFC 5753 Section 8]: https://datatracker.ietf.org/doc/html/rfc5753#section-8
3939
/// [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)]
4141
pub enum KeyWrapAlgorithm {
4242
/// id-aes128-wrap
4343
Aes128,
@@ -129,6 +129,18 @@ impl From<KeyWrapAlgorithm> for AlgorithmIdentifierOwned {
129129
}
130130
}
131131
}
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+
}
132144

133145
/// This struct can be used to perform key wrapping operation.
134146
///
@@ -345,6 +357,21 @@ mod tests {
345357
}
346358
)
347359
}
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+
}
348375

349376
#[test]
350377
fn test_keywrapper_try_new() {

0 commit comments

Comments
 (0)