diff options
| author | Dario Nieuwenhuis <[email protected]> | 2024-03-01 19:35:57 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-03-01 19:35:57 +0000 |
| commit | d5c9c611fa317e066d6cf7c5af0513b40bd69d8c (patch) | |
| tree | e7e11fc7a420e53d1703a724c052a47c0f95a36f /examples | |
| parent | defc1845c91a6edb215ee766c9e9b5e6f09109c3 (diff) | |
| parent | 97e125872e707e96bf81cd8e601f92f0f9f688a1 (diff) | |
Merge pull request #2619 from caleb-garrett/cryp
STM32 Crypto Accelerator
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32f7/Cargo.toml | 1 | ||||
| -rw-r--r-- | examples/stm32f7/src/bin/cryp.rs | 74 |
2 files changed, 75 insertions, 0 deletions
diff --git a/examples/stm32f7/Cargo.toml b/examples/stm32f7/Cargo.toml index 736e81723..305816a2b 100644 --- a/examples/stm32f7/Cargo.toml +++ b/examples/stm32f7/Cargo.toml | |||
| @@ -30,6 +30,7 @@ embedded-storage = "0.3.1" | |||
| 30 | static_cell = "2" | 30 | static_cell = "2" |
| 31 | sha2 = { version = "0.10.8", default-features = false } | 31 | sha2 = { version = "0.10.8", default-features = false } |
| 32 | hmac = "0.12.1" | 32 | hmac = "0.12.1" |
| 33 | aes-gcm = {version = "0.10.3", default-features = false, features = ["aes", "heapless"] } | ||
| 33 | 34 | ||
| 34 | [profile.release] | 35 | [profile.release] |
| 35 | debug = 2 | 36 | debug = 2 |
diff --git a/examples/stm32f7/src/bin/cryp.rs b/examples/stm32f7/src/bin/cryp.rs new file mode 100644 index 000000000..04927841a --- /dev/null +++ b/examples/stm32f7/src/bin/cryp.rs | |||
| @@ -0,0 +1,74 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use aes_gcm::aead::heapless::Vec; | ||
| 5 | use aes_gcm::aead::{AeadInPlace, KeyInit}; | ||
| 6 | use aes_gcm::Aes128Gcm; | ||
| 7 | use defmt::info; | ||
| 8 | use embassy_executor::Spawner; | ||
| 9 | use embassy_stm32::cryp::*; | ||
| 10 | use embassy_stm32::Config; | ||
| 11 | use embassy_time::Instant; | ||
| 12 | use {defmt_rtt as _, panic_probe as _}; | ||
| 13 | |||
| 14 | #[embassy_executor::main] | ||
| 15 | async fn main(_spawner: Spawner) -> ! { | ||
| 16 | let config = Config::default(); | ||
| 17 | let p = embassy_stm32::init(config); | ||
| 18 | |||
| 19 | let payload: &[u8] = b"hello world"; | ||
| 20 | let aad: &[u8] = b"additional data"; | ||
| 21 | |||
| 22 | let hw_cryp = Cryp::new(p.CRYP); | ||
| 23 | let key: [u8; 16] = [0; 16]; | ||
| 24 | let mut ciphertext: [u8; 11] = [0; 11]; | ||
| 25 | let mut plaintext: [u8; 11] = [0; 11]; | ||
| 26 | let iv: [u8; 12] = [0; 12]; | ||
| 27 | |||
| 28 | let hw_start_time = Instant::now(); | ||
| 29 | |||
| 30 | // Encrypt in hardware using AES-GCM 128-bit | ||
| 31 | let aes_gcm = AesGcm::new(&key, &iv); | ||
| 32 | let mut gcm_encrypt = hw_cryp.start(&aes_gcm, Direction::Encrypt); | ||
| 33 | hw_cryp.aad_blocking(&mut gcm_encrypt, aad, true); | ||
| 34 | hw_cryp.payload_blocking(&mut gcm_encrypt, payload, &mut ciphertext, true); | ||
| 35 | let encrypt_tag = hw_cryp.finish_blocking(gcm_encrypt); | ||
| 36 | |||
| 37 | // Decrypt in hardware using AES-GCM 128-bit | ||
| 38 | let mut gcm_decrypt = hw_cryp.start(&aes_gcm, Direction::Decrypt); | ||
| 39 | hw_cryp.aad_blocking(&mut gcm_decrypt, aad, true); | ||
| 40 | hw_cryp.payload_blocking(&mut gcm_decrypt, &ciphertext, &mut plaintext, true); | ||
| 41 | let decrypt_tag = hw_cryp.finish_blocking(gcm_decrypt); | ||
| 42 | |||
| 43 | let hw_end_time = Instant::now(); | ||
| 44 | let hw_execution_time = hw_end_time - hw_start_time; | ||
| 45 | |||
| 46 | info!("AES-GCM Ciphertext: {:?}", ciphertext); | ||
| 47 | info!("AES-GCM Plaintext: {:?}", plaintext); | ||
| 48 | assert_eq!(payload, plaintext); | ||
| 49 | assert_eq!(encrypt_tag, decrypt_tag); | ||
| 50 | |||
| 51 | let sw_start_time = Instant::now(); | ||
| 52 | |||
| 53 | // Encrypt in software using AES-GCM 128-bit | ||
| 54 | let mut payload_vec: Vec<u8, 32> = Vec::from_slice(&payload).unwrap(); | ||
| 55 | let cipher = Aes128Gcm::new(&key.into()); | ||
| 56 | let _ = cipher.encrypt_in_place(&iv.into(), aad.into(), &mut payload_vec); | ||
| 57 | |||
| 58 | assert_eq!(ciphertext, payload_vec[0..ciphertext.len()]); | ||
| 59 | assert_eq!( | ||
| 60 | encrypt_tag, | ||
| 61 | payload_vec[ciphertext.len()..ciphertext.len() + encrypt_tag.len()] | ||
| 62 | ); | ||
| 63 | |||
| 64 | // Decrypt in software using AES-GCM 128-bit | ||
| 65 | let _ = cipher.decrypt_in_place(&iv.into(), aad.into(), &mut payload_vec); | ||
| 66 | |||
| 67 | let sw_end_time = Instant::now(); | ||
| 68 | let sw_execution_time = sw_end_time - sw_start_time; | ||
| 69 | |||
| 70 | info!("Hardware Execution Time: {:?}", hw_execution_time); | ||
| 71 | info!("Software Execution Time: {:?}", sw_execution_time); | ||
| 72 | |||
| 73 | loop {} | ||
| 74 | } | ||
