diff options
| author | Caleb Garrett <[email protected]> | 2024-02-12 20:33:04 -0500 |
|---|---|---|
| committer | Caleb Garrett <[email protected]> | 2024-02-12 20:33:04 -0500 |
| commit | f0f1f2d14c6cb82ee1a4f452bdece2f98479c39f (patch) | |
| tree | add4622df0b14af14b7c5741e3212e16ca34b5d0 | |
| parent | 37c869827e96fb17838f89a082f12c08f3177fff (diff) | |
Added HMAC example.
| -rw-r--r-- | examples/stm32f7/Cargo.toml | 1 | ||||
| -rw-r--r-- | examples/stm32f7/src/bin/hash.rs | 22 |
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" | |||
| 29 | embedded-storage = "0.3.1" | 29 | 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 | 33 | ||
| 33 | [profile.release] | 34 | [profile.release] |
| 34 | debug = 2 | 35 | debug = 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; | |||
| 6 | use embassy_stm32::hash::*; | 6 | use embassy_stm32::hash::*; |
| 7 | use embassy_stm32::{bind_interrupts, hash, peripherals, Config}; | 7 | use embassy_stm32::{bind_interrupts, hash, peripherals, Config}; |
| 8 | use embassy_time::Instant; | 8 | use embassy_time::Instant; |
| 9 | use hmac::{Hmac, Mac}; | ||
| 9 | use sha2::{Digest, Sha256}; | 10 | use sha2::{Digest, Sha256}; |
| 10 | use {defmt_rtt as _, panic_probe as _}; | 11 | use {defmt_rtt as _, panic_probe as _}; |
| 11 | 12 | ||
| 13 | type HmacSha256 = Hmac<Sha256>; | ||
| 14 | |||
| 12 | bind_interrupts!(struct Irqs { | 15 | bind_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 | } |
