Skip to content

Commit f0045b9

Browse files
committed
Added HMAC to STM32 hash test.
1 parent 14a678f commit f0045b9

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

tests/stm32/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ portable-atomic = { version = "1.5", features = [] }
7676

7777
chrono = { version = "^0.4", default-features = false, optional = true}
7878
sha2 = { version = "0.10.8", default-features = false }
79+
hmac = "0.12.1"
7980

8081
# BEGIN TESTS
8182
# Generated by gen_test.py. DO NOT EDIT.

tests/stm32/src/bin/hash.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ use embassy_executor::Spawner;
99
use embassy_stm32::dma::NoDma;
1010
use embassy_stm32::hash::*;
1111
use embassy_stm32::{bind_interrupts, hash, peripherals};
12+
use hmac::{Hmac, Mac};
1213
use sha2::{Digest, Sha224, Sha256};
1314
use {defmt_rtt as _, panic_probe as _};
1415

16+
type HmacSha256 = Hmac<Sha256>;
17+
1518
#[cfg(any(feature = "stm32l4a6zg", feature = "stm32h755zi", feature = "stm32h753zi"))]
1619
bind_interrupts!(struct Irqs {
1720
HASH_RNG => hash::InterruptHandler<peripherals::HASH>;
@@ -73,6 +76,25 @@ async fn main(_spawner: Spawner) {
7376
info!("Software SHA-256 Digest: {:?}", sw_sha224_digest[..]);
7477
defmt::assert!(sha224_digest_buffer == sw_sha224_digest[..]);
7578

79+
let hmac_key: [u8; 64] = [0x55; 64];
80+
81+
// Compute HMAC in hardware.
82+
let mut sha256hmac_context = hw_hasher.start(Algorithm::SHA256, DataType::Width8, Some(&hmac_key));
83+
hw_hasher.update_blocking(&mut sha256hmac_context, test_1);
84+
hw_hasher.update_blocking(&mut sha256hmac_context, test_2);
85+
let mut hw_hmac: [u8; 32] = [0; 32];
86+
hw_hasher.finish_blocking(sha256hmac_context, &mut hw_hmac);
87+
88+
// Compute HMAC in software.
89+
let mut sw_mac = HmacSha256::new_from_slice(&hmac_key).unwrap();
90+
sw_mac.update(test_1);
91+
sw_mac.update(test_2);
92+
let sw_hmac = sw_mac.finalize().into_bytes();
93+
94+
info!("Hardware HMAC: {:?}", hw_hmac);
95+
info!("Software HMAC: {:?}", sw_hmac[..]);
96+
defmt::assert!(hw_hmac == sw_hmac[..]);
97+
7698
info!("Test OK");
7799
cortex_m::asm::bkpt();
78100
}

0 commit comments

Comments
 (0)