aboutsummaryrefslogtreecommitdiff
path: root/examples/stm32f7/src/bin/cryp.rs
diff options
context:
space:
mode:
authorCaleb Garrett <[email protected]>2024-02-22 15:47:36 -0500
committerCaleb Garrett <[email protected]>2024-02-25 20:59:07 -0500
commitbf4cbd75779b230e9e33a9d2a849f67335a68cf9 (patch)
tree4f42da64f4cead8be34c9b4f2a79c2a9c191092b /examples/stm32f7/src/bin/cryp.rs
parentcbca3a5c9f8f46582287b88db173ad6876686141 (diff)
Add CRYP example.
Diffstat (limited to 'examples/stm32f7/src/bin/cryp.rs')
-rw-r--r--examples/stm32f7/src/bin/cryp.rs69
1 files changed, 69 insertions, 0 deletions
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
4use aes_gcm::{
5 aead::{heapless::Vec, AeadInPlace, KeyInit},
6 Aes128Gcm,
7};
8use defmt::info;
9use embassy_executor::Spawner;
10use embassy_stm32::cryp::*;
11use embassy_stm32::Config;
12use embassy_time::Instant;
13use {defmt_rtt as _, panic_probe as _};
14
15#[embassy_executor::main]
16async 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}