diff options
| author | Caleb Garrett <[email protected]> | 2024-02-24 16:31:43 -0500 |
|---|---|---|
| committer | Caleb Garrett <[email protected]> | 2024-02-25 20:59:07 -0500 |
| commit | 236fc6f650af41980af05ef03a3901b2dfcfc381 (patch) | |
| tree | 844bd267f8858a5241c9c50358880d355c35a76c /tests/stm32/src/bin/cryp.rs | |
| parent | f352b6d68b17fee886af58494b7e793cea3ea383 (diff) | |
Add CRYP test.
Diffstat (limited to 'tests/stm32/src/bin/cryp.rs')
| -rw-r--r-- | tests/stm32/src/bin/cryp.rs | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/tests/stm32/src/bin/cryp.rs b/tests/stm32/src/bin/cryp.rs new file mode 100644 index 000000000..59c85f258 --- /dev/null +++ b/tests/stm32/src/bin/cryp.rs | |||
| @@ -0,0 +1,71 @@ | |||
| 1 | // required-features: cryp | ||
| 2 | #![no_std] | ||
| 3 | #![no_main] | ||
| 4 | |||
| 5 | #[path = "../common.rs"] | ||
| 6 | mod common; | ||
| 7 | |||
| 8 | use aes_gcm::aead::heapless::Vec; | ||
| 9 | use aes_gcm::aead::{AeadInPlace, KeyInit}; | ||
| 10 | use aes_gcm::Aes128Gcm; | ||
| 11 | use common::*; | ||
| 12 | use embassy_executor::Spawner; | ||
| 13 | use embassy_stm32::cryp::*; | ||
| 14 | use {defmt_rtt as _, panic_probe as _}; | ||
| 15 | |||
| 16 | #[embassy_executor::main] | ||
| 17 | async fn main(_spawner: Spawner) { | ||
| 18 | let p: embassy_stm32::Peripherals = embassy_stm32::init(config()); | ||
| 19 | |||
| 20 | const PAYLOAD1: &[u8] = b"payload data 1 ;zdfhzdfhS;GKJASBDG;ASKDJBAL,zdfhzdfhzdfhzdfhvljhb,jhbjhb,sdhsdghsdhsfhsghzdfhzdfhzdfhzdfdhsdthsthsdhsgaadfhhgkdgfuoyguoft6783567"; | ||
| 21 | const PAYLOAD2: &[u8] = b"payload data 2 ;SKEzdfhzdfhzbhgvljhb,jhbjhb,sdhsdghsdhsfhsghshsfhshstsdthadfhsdfjhsfgjsfgjxfgjzdhgDFghSDGHjtfjtjszftjzsdtjhstdsdhsdhsdhsdhsdthsthsdhsgfh"; | ||
| 22 | const AAD1: &[u8] = b"additional data 1 stdargadrhaethaethjatjatjaetjartjstrjsfkk;'jopofyuisrteytweTASTUIKFUKIXTRDTEREharhaeryhaterjartjarthaethjrtjarthaetrhartjatejatrjsrtjartjyt1"; | ||
| 23 | const AAD2: &[u8] = b"additional data 2 stdhthsthsthsrthsrthsrtjdykjdukdyuldadfhsdghsdghsdghsadghjk'hioethjrtjarthaetrhartjatecfgjhzdfhgzdfhzdfghzdfhzdfhzfhjatrjsrtjartjytjfytjfyg"; | ||
| 24 | |||
| 25 | let hw_cryp = Cryp::new(p.CRYP); | ||
| 26 | let key: [u8; 16] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; | ||
| 27 | let mut ciphertext: [u8; PAYLOAD1.len() + PAYLOAD2.len()] = [0; PAYLOAD1.len() + PAYLOAD2.len()]; | ||
| 28 | let mut plaintext: [u8; PAYLOAD1.len() + PAYLOAD2.len()] = [0; PAYLOAD1.len() + PAYLOAD2.len()]; | ||
| 29 | let iv: [u8; 12] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; | ||
| 30 | |||
| 31 | // Encrypt in hardware using AES-GCM 128-bit | ||
| 32 | let aes_gcm = AesGcm::new(&key, &iv); | ||
| 33 | let mut gcm_encrypt = hw_cryp.start(&aes_gcm, Direction::Encrypt); | ||
| 34 | hw_cryp.aad_blocking(&mut gcm_encrypt, AAD1, false); | ||
| 35 | hw_cryp.aad_blocking(&mut gcm_encrypt, AAD2, true); | ||
| 36 | hw_cryp.payload_blocking(&mut gcm_encrypt, PAYLOAD1, &mut ciphertext[..PAYLOAD1.len()], false); | ||
| 37 | hw_cryp.payload_blocking(&mut gcm_encrypt, PAYLOAD2, &mut ciphertext[PAYLOAD1.len()..], true); | ||
| 38 | let encrypt_tag = hw_cryp.finish_blocking(gcm_encrypt); | ||
| 39 | |||
| 40 | // Decrypt in hardware using AES-GCM 128-bit | ||
| 41 | let mut gcm_decrypt = hw_cryp.start(&aes_gcm, Direction::Decrypt); | ||
| 42 | hw_cryp.aad_blocking(&mut gcm_decrypt, AAD1, false); | ||
| 43 | hw_cryp.aad_blocking(&mut gcm_decrypt, AAD2, true); | ||
| 44 | hw_cryp.payload_blocking(&mut gcm_decrypt, &ciphertext, &mut plaintext, true); | ||
| 45 | let decrypt_tag = hw_cryp.finish_blocking(gcm_decrypt); | ||
| 46 | |||
| 47 | info!("AES-GCM Ciphertext: {:?}", ciphertext); | ||
| 48 | info!("AES-GCM Plaintext: {:?}", plaintext); | ||
| 49 | defmt::assert!(PAYLOAD1 == &plaintext[..PAYLOAD1.len()]); | ||
| 50 | defmt::assert!(PAYLOAD2 == &plaintext[PAYLOAD1.len()..]); | ||
| 51 | defmt::assert!(encrypt_tag == decrypt_tag); | ||
| 52 | |||
| 53 | // Encrypt in software using AES-GCM 128-bit | ||
| 54 | let mut payload_vec: Vec<u8, { PAYLOAD1.len() + PAYLOAD2.len() + 16 }> = Vec::from_slice(&PAYLOAD1).unwrap(); | ||
| 55 | payload_vec.extend_from_slice(&PAYLOAD2).unwrap(); | ||
| 56 | let cipher = Aes128Gcm::new(&key.into()); | ||
| 57 | let mut aad: Vec<u8, { AAD1.len() + AAD2.len() }> = Vec::from_slice(&AAD1).unwrap(); | ||
| 58 | aad.extend_from_slice(&AAD2).unwrap(); | ||
| 59 | let _ = cipher.encrypt_in_place(&iv.into(), &aad, &mut payload_vec); | ||
| 60 | |||
| 61 | defmt::assert!(ciphertext == payload_vec[0..ciphertext.len()]); | ||
| 62 | defmt::assert!( | ||
| 63 | encrypt_tag == payload_vec[ciphertext.len()..ciphertext.len() + encrypt_tag.len()] | ||
| 64 | ); | ||
| 65 | |||
| 66 | // Decrypt in software using AES-GCM 128-bit | ||
| 67 | let _ = cipher.decrypt_in_place(&iv.into(), &aad, &mut payload_vec); | ||
| 68 | |||
| 69 | info!("Test OK"); | ||
| 70 | cortex_m::asm::bkpt(); | ||
| 71 | } | ||
