aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2024-02-13 20:47:42 +0100
committerGitHub <[email protected]>2024-02-13 20:47:42 +0100
commit4c7ed5e0556e7edbf7020b4cac9f301ad6152905 (patch)
tree0137498a23b9ae116460c0e9c4b467e8d0e82ed3 /examples
parentee4afa42808109efa45a472fd29e4c772550a0fd (diff)
parentf0045b92173a46e45be1724d28e1a59045c9c3f6 (diff)
Merge pull request #2565 from caleb-garrett/hmac
STM32 HMAC
Diffstat (limited to 'examples')
-rw-r--r--examples/stm32f7/Cargo.toml1
-rw-r--r--examples/stm32f7/src/bin/hash.rs24
2 files changed, 24 insertions, 1 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 96e50f84b..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});
@@ -26,7 +29,7 @@ async fn main(_spawner: Spawner) -> ! {
26 let hw_start_time = Instant::now(); 29 let hw_start_time = Instant::now();
27 30
28 // Compute a digest in hardware. 31 // Compute a digest in hardware.
29 let mut context = hw_hasher.start(Algorithm::SHA256, DataType::Width8); 32 let mut context = hw_hasher.start(Algorithm::SHA256, DataType::Width8, None);
30 hw_hasher.update(&mut context, test_1).await; 33 hw_hasher.update(&mut context, test_1).await;
31 hw_hasher.update(&mut context, test_2).await; 34 hw_hasher.update(&mut context, test_2).await;
32 let mut hw_digest: [u8; 32] = [0; 32]; 35 let mut hw_digest: [u8; 32] = [0; 32];
@@ -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}