diff options
| author | Caleb Garrett <[email protected]> | 2024-02-22 15:47:36 -0500 |
|---|---|---|
| committer | Caleb Garrett <[email protected]> | 2024-02-25 20:59:07 -0500 |
| commit | bf4cbd75779b230e9e33a9d2a849f67335a68cf9 (patch) | |
| tree | 4f42da64f4cead8be34c9b4f2a79c2a9c191092b /examples/stm32f7 | |
| parent | cbca3a5c9f8f46582287b88db173ad6876686141 (diff) | |
Add CRYP example.
Diffstat (limited to 'examples/stm32f7')
| -rw-r--r-- | examples/stm32f7/Cargo.toml | 1 | ||||
| -rw-r--r-- | examples/stm32f7/src/bin/cryp.rs | 69 |
2 files changed, 70 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..c1b80ddc3 --- /dev/null +++ b/examples/stm32f7/src/bin/cryp.rs | |||
| @@ -0,0 +1,69 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use aes_gcm::{ | ||
| 5 | aead::{heapless::Vec, AeadInPlace, KeyInit}, | ||
| 6 | Aes128Gcm, | ||
| 7 | }; | ||
| 8 | use defmt::info; | ||
| 9 | use embassy_executor::Spawner; | ||
| 10 | use embassy_stm32::cryp::*; | ||
| 11 | use embassy_stm32::Config; | ||
| 12 | use embassy_time::Instant; | ||
| 13 | use {defmt_rtt as _, panic_probe as _}; | ||
| 14 | |||
| 15 | #[embassy_executor::main] | ||
| 16 | async fn main(_spawner: Spawner) -> ! { | ||
| 17 | let config = Config::default(); | ||
| 18 | let p = embassy_stm32::init(config); | ||
| 19 | |||
| 20 | let payload: &[u8] = b"hello world"; | ||
| 21 | let aad: &[u8] = b"additional data"; | ||
| 22 | |||
| 23 | let hw_cryp = Cryp::new(p.CRYP); | ||
| 24 | let key: [u8; 16] = [0; 16]; | ||
| 25 | let mut ciphertext: [u8; 11] = [0; 11]; | ||
| 26 | let mut plaintext: [u8; 11] = [0; 11]; | ||
| 27 | let iv: [u8; 12] = [0; 12]; | ||
| 28 | |||
| 29 | let hw_start_time = Instant::now(); | ||
| 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, aad, true); | ||
| 35 | hw_cryp.payload_blocking(&mut gcm_encrypt, payload, &mut ciphertext, true); | ||
| 36 | let encrypt_tag = hw_cryp.finish_blocking(gcm_encrypt); | ||
| 37 | |||
| 38 | // Decrypt in hardware using AES-GCM 128-bit | ||
| 39 | let mut gcm_decrypt = hw_cryp.start(&aes_gcm, Direction::Decrypt); | ||
| 40 | hw_cryp.aad_blocking(&mut gcm_decrypt, aad, true); | ||
| 41 | hw_cryp.payload_blocking(&mut gcm_decrypt, &ciphertext, &mut plaintext, true); | ||
| 42 | let decrypt_tag = hw_cryp.finish_blocking(gcm_decrypt); | ||
| 43 | |||
| 44 | let hw_end_time = Instant::now(); | ||
| 45 | let hw_execution_time = hw_end_time - hw_start_time; | ||
| 46 | |||
| 47 | info!("AES-GCM Ciphertext: {:?}", ciphertext); | ||
| 48 | info!("AES-GCM Plaintext: {:?}", plaintext); | ||
| 49 | assert_eq!(payload, plaintext); | ||
| 50 | assert_eq!(encrypt_tag, decrypt_tag); | ||
| 51 | |||
| 52 | let sw_start_time = Instant::now(); | ||
| 53 | |||
| 54 | //Encrypt in software using AES-GCM 128-bit | ||
| 55 | let mut payload_vec: Vec<u8, 32> = Vec::from_slice(&payload).unwrap(); | ||
| 56 | let cipher = Aes128Gcm::new(&key.into()); | ||
| 57 | let _ = cipher.encrypt_in_place(&iv.into(), aad.into(), &mut payload_vec); | ||
| 58 | |||
| 59 | //Decrypt in software using AES-GCM 128-bit | ||
| 60 | let _ = cipher.encrypt_in_place(&iv.into(), aad.into(), &mut payload_vec); | ||
| 61 | |||
| 62 | let sw_end_time = Instant::now(); | ||
| 63 | let sw_execution_time = sw_end_time - sw_start_time; | ||
| 64 | |||
| 65 | info!("Hardware Execution Time: {:?}", hw_execution_time); | ||
| 66 | info!("Software Execution Time: {:?}", sw_execution_time); | ||
| 67 | |||
| 68 | loop {} | ||
| 69 | } | ||
