Skip to content

Commit dcb7d1f

Browse files
test: aes ccm roundtrip
1 parent 39d96a9 commit dcb7d1f

File tree

4 files changed

+171
-0
lines changed

4 files changed

+171
-0
lines changed

crypto/lakers-crypto-cryptocell310-sys/src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,3 +366,20 @@ impl digest::OutputSizeUser for HashInProcessSha256 {
366366
type OutputSize = digest::typenum::U32;
367367
}
368368
impl digest::HashMarker for HashInProcessSha256 {}
369+
370+
#[cfg(test)]
371+
mod tests {
372+
use super::*;
373+
use lakers_shared::test_helper::{
374+
test_aes_ccm_roundtrip, test_aes_ccm_tag_16, test_aes_ccm_tag_8,
375+
};
376+
377+
#[test]
378+
fn test_cryptocell_aes_ccm() {
379+
test_aes_ccm_roundtrip::<Crypto, CcmTagLen8>(&mut Crypto);
380+
test_aes_ccm_roundtrip::<Crypto, CcmTagLen16>(&mut Crypto);
381+
382+
test_aes_ccm_tag_8::<Crypto>(&mut Crypto);
383+
test_aes_ccm_tag_16::<Crypto>(&mut Crypto);
384+
}
385+
}

crypto/lakers-crypto-psa/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,9 @@ impl digest::HashMarker for BufferedHasherSha256 {}
324324
#[cfg(test)]
325325
mod tests {
326326
use super::*;
327+
use lakers_shared::test_helper::{
328+
test_aes_ccm_roundtrip, test_aes_ccm_tag_16, test_aes_ccm_tag_8,
329+
};
327330

328331
#[test]
329332
fn test_hmac_sha256() {
@@ -347,4 +350,13 @@ mod tests {
347350
let result_2 = Crypto.hmac_sha256(&MESSAGE_2, &KEY);
348351
assert_eq!(result_2, RESULT_2_TV);
349352
}
353+
354+
#[test]
355+
fn test_psa_aes_ccm() {
356+
test_aes_ccm_roundtrip::<Crypto, CcmTagLen8>(&mut Crypto);
357+
test_aes_ccm_roundtrip::<Crypto, CcmTagLen16>(&mut Crypto);
358+
359+
test_aes_ccm_tag_8::<Crypto>(&mut Crypto);
360+
test_aes_ccm_tag_16::<Crypto>(&mut Crypto);
361+
}
350362
}

crypto/lakers-crypto-rustcrypto/src/lib.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,23 @@ impl<Rng: rand_core::RngCore + rand_core::CryptoRng> CryptoTrait for Crypto<Rng>
185185
(private_key.into(), public_key.into())
186186
}
187187
}
188+
189+
#[cfg(test)]
190+
mod tests {
191+
use lakers_shared::test_helper::{
192+
test_aes_ccm_roundtrip, test_aes_ccm_tag_16, test_aes_ccm_tag_8,
193+
};
194+
use lakers_shared::{CcmTagLen16, CcmTagLen8};
195+
196+
use super::*;
197+
198+
#[test]
199+
fn test_rustcrypto_aes_ccm() {
200+
let mut crypto = Crypto::new(rand_core::OsRng);
201+
test_aes_ccm_roundtrip::<Crypto<rand_core::OsRng>, CcmTagLen8>(&mut crypto);
202+
test_aes_ccm_roundtrip::<Crypto<rand_core::OsRng>, CcmTagLen16>(&mut crypto);
203+
204+
test_aes_ccm_tag_8::<Crypto<rand_core::OsRng>>(&mut crypto);
205+
test_aes_ccm_tag_16::<Crypto<rand_core::OsRng>>(&mut crypto);
206+
}
207+
}

shared/src/crypto.rs

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,125 @@ pub struct CcmTagLen16;
9898
impl CcmTagLen for CcmTagLen16 {
9999
const LEN: usize = 16;
100100
}
101+
102+
pub mod test_helper {
103+
use super::*;
104+
105+
pub fn test_aes_ccm_roundtrip<C: Crypto, Tag: CcmTagLen>(crypto: &mut C) {
106+
let key: BytesCcmKeyLen = [
107+
0x26, 0x51, 0x1f, 0xb5, 0x1f, 0xcf, 0xa7, 0x5c, 0xb4, 0xb4, 0x4d, 0xa7, 0x5a, 0x6e,
108+
0x5a, 0x0e,
109+
]
110+
.into();
111+
112+
let iv: BytesCcmIvLen = [
113+
0x5a, 0x8a, 0xa4, 0x85, 0xc3, 0x16, 0xe9, 0x40, 0x3a, 0xff, 0x85, 0x9f, 0xbb,
114+
]
115+
.into();
116+
117+
let ad = [
118+
0xa1, 0x6a, 0x2e, 0x74, 0x1f, 0x1c, 0xd9, 0x71, 0x72, 0x85, 0xb6, 0xd8, 0x82, 0xc1,
119+
0xfc, 0x53, 0x65, 0x5e, 0x97, 0x73, 0x76, 0x1a, 0xd6, 0x97, 0xa7, 0xee, 0x64, 0x10,
120+
0x18, 0x4c, 0x79, 0x82,
121+
];
122+
let plaintext = [
123+
0x87, 0x39, 0xb4, 0xbe, 0xa1, 0xa0, 0x99, 0xfe, 0x54, 0x74, 0x99, 0xcb, 0xc6, 0xd1,
124+
0xb1, 0x3d, 0x84, 0x9b, 0x80, 0x84, 0xc9, 0xb6, 0xac, 0xc5,
125+
];
126+
127+
let ciphertext = crypto.aes_ccm_encrypt::<64, Tag>(&key, &iv, &ad, &plaintext);
128+
assert_eq!(ciphertext.len(), plaintext.len() + Tag::LEN);
129+
130+
let decrypted = crypto
131+
.aes_ccm_decrypt::<64, Tag>(&key, &iv, &ad, ciphertext.as_slice())
132+
.expect("decryption should succeed");
133+
134+
assert_eq!(decrypted.as_slice(), &plaintext);
135+
}
136+
137+
pub fn test_aes_ccm_tag_8<C: Crypto>(crypto: &mut C) {
138+
type Tag = CcmTagLen8;
139+
let key: BytesCcmKeyLen = [
140+
0x36, 0x8f, 0x35, 0xa1, 0xf8, 0x0e, 0xaa, 0xac, 0xd6, 0xbb, 0x13, 0x66, 0x09, 0x38,
141+
0x97, 0x27,
142+
]
143+
.into();
144+
145+
let iv: BytesCcmIvLen = [
146+
0x84, 0x2a, 0x84, 0x45, 0x84, 0x75, 0x02, 0xea, 0x77, 0x36, 0x3a, 0x16, 0xb6,
147+
]
148+
.into();
149+
150+
let ad = [
151+
0x34, 0x39, 0x6d, 0xfc, 0xfa, 0x6f, 0x74, 0x2a, 0xea, 0x70, 0x40, 0x97, 0x6b, 0xd5,
152+
0x96, 0x49, 0x7a, 0x7a, 0x6f, 0xa4, 0xfb, 0x85, 0xee, 0x8e, 0x4c, 0xa3, 0x94, 0xd0,
153+
0x20, 0x95, 0xb7, 0xbf,
154+
];
155+
let plaintext = [
156+
0x1c, 0xcc, 0xd5, 0x58, 0x25, 0x31, 0x6a, 0x94, 0xc5, 0x97, 0x9e, 0x04, 0x93, 0x10,
157+
0xd1, 0xd7, 0x17, 0xcd, 0xfb, 0x76, 0x24, 0x28, 0x9d, 0xac,
158+
];
159+
160+
let expected_ct: [u8; 32] = [
161+
0x1a, 0x58, 0x09, 0x4f, 0x0e, 0x8c, 0x60, 0x35, 0xa5, 0x58, 0x4b, 0xfa, 0x8d, 0x10,
162+
0x09, 0xc5, 0xf7, 0x8f, 0xd2, 0xca, 0x48, 0x7f, 0xf2, 0x22, 0xf6, 0xd1, 0xd8, 0x97,
163+
0xd6, 0x05, 0x16, 0x18,
164+
];
165+
166+
let ciphertext = crypto.aes_ccm_encrypt::<64, Tag>(&key, &iv, &ad, &plaintext);
167+
assert_eq!(ciphertext.len(), plaintext.len() + Tag::LEN);
168+
assert_eq!(
169+
ciphertext,
170+
EdhocBuffer::new_from_slice(&expected_ct).expect("expected_ct.length() <= 64")
171+
);
172+
173+
let decrypted = crypto
174+
.aes_ccm_decrypt::<64, Tag>(&key, &iv, &ad, ciphertext.as_slice())
175+
.expect("decryption should succeed");
176+
177+
assert_eq!(decrypted.as_slice(), &plaintext);
178+
}
179+
180+
pub fn test_aes_ccm_tag_16<C: Crypto>(crypto: &mut C) {
181+
type Tag = CcmTagLen16;
182+
let key: BytesCcmKeyLen = [
183+
0x41, 0x89, 0x35, 0x1b, 0x5c, 0xae, 0xa3, 0x75, 0xa0, 0x29, 0x9e, 0x81, 0xc6, 0x21,
184+
0xbf, 0x43,
185+
]
186+
.into();
187+
188+
let iv: BytesCcmIvLen = [
189+
0x48, 0xc0, 0x90, 0x69, 0x30, 0x56, 0x1e, 0x0a, 0xb0, 0xef, 0x4c, 0xd9, 0x72,
190+
]
191+
.into();
192+
193+
let ad = [
194+
0x40, 0xa2, 0x7c, 0x1d, 0x1e, 0x23, 0xea, 0x3d, 0xbe, 0x80, 0x56, 0xb2, 0x77, 0x48,
195+
0x61, 0xa4, 0xa2, 0x01, 0xcc, 0xe4, 0x9f, 0x19, 0x99, 0x7d, 0x19, 0x20, 0x6d, 0x8c,
196+
0x8a, 0x34, 0x39, 0x51,
197+
];
198+
let plaintext = [
199+
0x45, 0x35, 0xd1, 0x2b, 0x43, 0x77, 0x92, 0x8a, 0x7c, 0x0a, 0x61, 0xc9, 0xf8, 0x25,
200+
0xa4, 0x86, 0x71, 0xea, 0x05, 0x91, 0x07, 0x48, 0xc8, 0xef,
201+
];
202+
203+
let expected_ct: [u8; 40] = [
204+
0x26, 0xc5, 0x69, 0x61, 0xc0, 0x35, 0xa7, 0xe4, 0x52, 0xcc, 0xe6, 0x1b, 0xc6, 0xee,
205+
0x22, 0x0d, 0x77, 0xb3, 0xf9, 0x4d, 0x18, 0xfd, 0x10, 0xb6, 0xd8, 0x0e, 0x8b, 0xf8,
206+
0x0f, 0x4a, 0x46, 0xca, 0xb0, 0x6d, 0x43, 0x13, 0xf0, 0xdb, 0x9b, 0xe9,
207+
];
208+
209+
let ciphertext = crypto.aes_ccm_encrypt::<64, Tag>(&key, &iv, &ad, &plaintext);
210+
assert_eq!(ciphertext.len(), plaintext.len() + Tag::LEN);
211+
assert_eq!(
212+
ciphertext,
213+
EdhocBuffer::new_from_slice(&expected_ct).expect("expected_ct.length() < 64")
214+
);
215+
216+
let decrypted = crypto
217+
.aes_ccm_decrypt::<64, Tag>(&key, &iv, &ad, ciphertext.as_slice())
218+
.expect("decryption should succeed");
219+
220+
assert_eq!(decrypted.as_slice(), &plaintext);
221+
}
222+
}

0 commit comments

Comments
 (0)