aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/stm32f7/Cargo.toml1
-rw-r--r--examples/stm32f7/src/bin/hash.rs22
2 files changed, 23 insertions, 0 deletions
diff --git a/examples/stm32f7/Cargo.toml b/examples/stm32f7/Cargo.toml
index a612c2554..736e81723 100644
--- a/examples/stm32f7/Cargo.toml
+++ b/examples/stm32f7/Cargo.toml
@@ -29,6 +29,7 @@ critical-section = "1.1"
29embedded-storage = "0.3.1" 29embedded-storage = "0.3.1"
30static_cell = "2" 30static_cell = "2"
31sha2 = { version = "0.10.8", default-features = false } 31sha2 = { version = "0.10.8", default-features = false }
32hmac = "0.12.1"
32 33
33[profile.release] 34[profile.release]
34debug = 2 35debug = 2
diff --git a/examples/stm32f7/src/bin/hash.rs b/examples/stm32f7/src/bin/hash.rs
index cbb880353..c2d1a7158 100644
--- a/examples/stm32f7/src/bin/hash.rs
+++ b/examples/stm32f7/src/bin/hash.rs
@@ -6,9 +6,12 @@ use embassy_executor::Spawner;
6use embassy_stm32::hash::*; 6use embassy_stm32::hash::*;
7use embassy_stm32::{bind_interrupts, hash, peripherals, Config}; 7use embassy_stm32::{bind_interrupts, hash, peripherals, Config};
8use embassy_time::Instant; 8use embassy_time::Instant;
9use hmac::{Hmac, Mac};
9use sha2::{Digest, Sha256}; 10use sha2::{Digest, Sha256};
10use {defmt_rtt as _, panic_probe as _}; 11use {defmt_rtt as _, panic_probe as _};
11 12
13type HmacSha256 = Hmac<Sha256>;
14
12bind_interrupts!(struct Irqs { 15bind_interrupts!(struct Irqs {
13 HASH_RNG => hash::InterruptHandler<peripherals::HASH>; 16 HASH_RNG => hash::InterruptHandler<peripherals::HASH>;
14}); 17});
@@ -52,5 +55,24 @@ async fn main(_spawner: Spawner) -> ! {
52 info!("Software Execution Time: {:?}", sw_execution_time); 55 info!("Software Execution Time: {:?}", sw_execution_time);
53 assert_eq!(hw_digest, sw_digest[..]); 56 assert_eq!(hw_digest, sw_digest[..]);
54 57
58 let hmac_key: [u8; 64] = [0x55; 64];
59
60 // Compute HMAC in hardware.
61 let mut sha256hmac_context = hw_hasher.start(Algorithm::SHA256, DataType::Width8, Some(&hmac_key));
62 hw_hasher.update(&mut sha256hmac_context, test_1).await;
63 hw_hasher.update(&mut sha256hmac_context, test_2).await;
64 let mut hw_hmac: [u8; 32] = [0; 32];
65 hw_hasher.finish(sha256hmac_context, &mut hw_hmac).await;
66
67 // Compute HMAC in software.
68 let mut sw_mac = HmacSha256::new_from_slice(&hmac_key).unwrap();
69 sw_mac.update(test_1);
70 sw_mac.update(test_2);
71 let sw_hmac = sw_mac.finalize().into_bytes();
72
73 info!("Hardware HMAC: {:?}", hw_hmac);
74 info!("Software HMAC: {:?}", sw_hmac[..]);
75 assert_eq!(hw_hmac, sw_hmac[..]);
76
55 loop {} 77 loop {}
56} 78}