diff options
| author | Caleb Garrett <[email protected]> | 2024-03-12 14:52:34 -0400 |
|---|---|---|
| committer | Caleb Garrett <[email protected]> | 2024-03-12 14:52:34 -0400 |
| commit | 1ec9fc58f44987c11ac1e093f117679c56dbe2ed (patch) | |
| tree | aba18c0ff6f23c65c305c92505b1b2fca14b08f2 /tests/stm32/src/bin/cryp.rs | |
| parent | 61050a16d5f02a7db718c6e39c811e6e434b032b (diff) | |
Add async CRYP to test.
Diffstat (limited to 'tests/stm32/src/bin/cryp.rs')
| -rw-r--r-- | tests/stm32/src/bin/cryp.rs | 31 |
1 files changed, 21 insertions, 10 deletions
diff --git a/tests/stm32/src/bin/cryp.rs b/tests/stm32/src/bin/cryp.rs index f105abf26..6bca55f55 100644 --- a/tests/stm32/src/bin/cryp.rs +++ b/tests/stm32/src/bin/cryp.rs | |||
| @@ -10,9 +10,17 @@ use aes_gcm::aead::{AeadInPlace, KeyInit}; | |||
| 10 | use aes_gcm::Aes128Gcm; | 10 | use aes_gcm::Aes128Gcm; |
| 11 | use common::*; | 11 | use common::*; |
| 12 | use embassy_executor::Spawner; | 12 | use embassy_executor::Spawner; |
| 13 | use embassy_stm32::cryp::*; | 13 | use embassy_stm32::{ |
| 14 | bind_interrupts, | ||
| 15 | cryp::{self, *}, | ||
| 16 | peripherals | ||
| 17 | }; | ||
| 14 | use {defmt_rtt as _, panic_probe as _}; | 18 | use {defmt_rtt as _, panic_probe as _}; |
| 15 | 19 | ||
| 20 | bind_interrupts!(struct Irqs { | ||
| 21 | CRYP => cryp::InterruptHandler<peripherals::CRYP>; | ||
| 22 | }); | ||
| 23 | |||
| 16 | #[embassy_executor::main] | 24 | #[embassy_executor::main] |
| 17 | async fn main(_spawner: Spawner) { | 25 | async fn main(_spawner: Spawner) { |
| 18 | let p: embassy_stm32::Peripherals = embassy_stm32::init(config()); | 26 | let p: embassy_stm32::Peripherals = embassy_stm32::init(config()); |
| @@ -22,27 +30,30 @@ async fn main(_spawner: Spawner) { | |||
| 22 | const AAD1: &[u8] = b"additional data 1 stdargadrhaethaethjatjatjaetjartjstrjsfkk;'jopofyuisrteytweTASTUIKFUKIXTRDTEREharhaeryhaterjartjarthaethjrtjarthaetrhartjatejatrjsrtjartjyt1"; | 30 | const AAD1: &[u8] = b"additional data 1 stdargadrhaethaethjatjatjaetjartjstrjsfkk;'jopofyuisrteytweTASTUIKFUKIXTRDTEREharhaeryhaterjartjarthaethjrtjarthaetrhartjatejatrjsrtjartjyt1"; |
| 23 | const AAD2: &[u8] = b"additional data 2 stdhthsthsthsrthsrthsrtjdykjdukdyuldadfhsdghsdghsdghsadghjk'hioethjrtjarthaetrhartjatecfgjhzdfhgzdfhzdfghzdfhzdfhzfhjatrjsrtjartjytjfytjfyg"; | 31 | const AAD2: &[u8] = b"additional data 2 stdhthsthsthsrthsrthsrtjdykjdukdyuldadfhsdghsdghsdghsadghjk'hioethjrtjarthaetrhartjatecfgjhzdfhgzdfhzdfghzdfhzdfhzfhjatrjsrtjartjytjfytjfyg"; |
| 24 | 32 | ||
| 25 | let hw_cryp = Cryp::new(p.CRYP); | 33 | let in_dma = peri!(p, CRYP_IN_DMA); |
| 34 | let out_dma = peri!(p, CRYP_OUT_DMA); | ||
| 35 | |||
| 36 | let mut hw_cryp = Cryp::new(p.CRYP, in_dma, out_dma, Irqs); | ||
| 26 | let key: [u8; 16] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; | 37 | 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()]; | 38 | 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()]; | 39 | 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]; | 40 | let iv: [u8; 12] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; |
| 30 | 41 | ||
| 31 | // Encrypt in hardware using AES-GCM 128-bit | 42 | // Encrypt in hardware using AES-GCM 128-bit in blocking mode. |
| 32 | let aes_gcm = AesGcm::new(&key, &iv); | 43 | let aes_gcm = AesGcm::new(&key, &iv); |
| 33 | let mut gcm_encrypt = hw_cryp.start(&aes_gcm, Direction::Encrypt); | 44 | let mut gcm_encrypt = hw_cryp.start_blocking(&aes_gcm, Direction::Encrypt); |
| 34 | hw_cryp.aad_blocking(&mut gcm_encrypt, AAD1, false); | 45 | hw_cryp.aad_blocking(&mut gcm_encrypt, AAD1, false); |
| 35 | hw_cryp.aad_blocking(&mut gcm_encrypt, AAD2, true); | 46 | hw_cryp.aad_blocking(&mut gcm_encrypt, AAD2, true); |
| 36 | hw_cryp.payload_blocking(&mut gcm_encrypt, PAYLOAD1, &mut ciphertext[..PAYLOAD1.len()], false); | 47 | 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); | 48 | hw_cryp.payload_blocking(&mut gcm_encrypt, PAYLOAD2, &mut ciphertext[PAYLOAD1.len()..], true); |
| 38 | let encrypt_tag = hw_cryp.finish_blocking(gcm_encrypt); | 49 | let encrypt_tag = hw_cryp.finish_blocking(gcm_encrypt); |
| 39 | 50 | ||
| 40 | // Decrypt in hardware using AES-GCM 128-bit | 51 | // Decrypt in hardware using AES-GCM 128-bit in async (DMA) mode. |
| 41 | let mut gcm_decrypt = hw_cryp.start(&aes_gcm, Direction::Decrypt); | 52 | let mut gcm_decrypt = hw_cryp.start(&aes_gcm, Direction::Decrypt).await; |
| 42 | hw_cryp.aad_blocking(&mut gcm_decrypt, AAD1, false); | 53 | hw_cryp.aad(&mut gcm_decrypt, AAD1, false).await; |
| 43 | hw_cryp.aad_blocking(&mut gcm_decrypt, AAD2, true); | 54 | hw_cryp.aad(&mut gcm_decrypt, AAD2, true).await; |
| 44 | hw_cryp.payload_blocking(&mut gcm_decrypt, &ciphertext, &mut plaintext, true); | 55 | hw_cryp.payload(&mut gcm_decrypt, &ciphertext, &mut plaintext, true).await; |
| 45 | let decrypt_tag = hw_cryp.finish_blocking(gcm_decrypt); | 56 | let decrypt_tag = hw_cryp.finish(gcm_decrypt).await; |
| 46 | 57 | ||
| 47 | info!("AES-GCM Ciphertext: {:?}", ciphertext); | 58 | info!("AES-GCM Ciphertext: {:?}", ciphertext); |
| 48 | info!("AES-GCM Plaintext: {:?}", plaintext); | 59 | info!("AES-GCM Plaintext: {:?}", plaintext); |
