From f0045b92173a46e45be1724d28e1a59045c9c3f6 Mon Sep 17 00:00:00 2001 From: Caleb Garrett <47389035+caleb-garrett@users.noreply.github.com> Date: Tue, 13 Feb 2024 10:17:19 -0500 Subject: [PATCH] Added HMAC to STM32 hash test. --- tests/stm32/Cargo.toml | 1 + tests/stm32/src/bin/hash.rs | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/tests/stm32/Cargo.toml b/tests/stm32/Cargo.toml index fc4420687..d94045737 100644 --- a/tests/stm32/Cargo.toml +++ b/tests/stm32/Cargo.toml @@ -76,6 +76,7 @@ portable-atomic = { version = "1.5", features = [] } chrono = { version = "^0.4", default-features = false, optional = true} sha2 = { version = "0.10.8", default-features = false } +hmac = "0.12.1" # BEGIN TESTS # Generated by gen_test.py. DO NOT EDIT. diff --git a/tests/stm32/src/bin/hash.rs b/tests/stm32/src/bin/hash.rs index 1b89f46e8..d1cfac5ce 100644 --- a/tests/stm32/src/bin/hash.rs +++ b/tests/stm32/src/bin/hash.rs @@ -9,9 +9,12 @@ use embassy_executor::Spawner; use embassy_stm32::dma::NoDma; use embassy_stm32::hash::*; use embassy_stm32::{bind_interrupts, hash, peripherals}; +use hmac::{Hmac, Mac}; use sha2::{Digest, Sha224, Sha256}; use {defmt_rtt as _, panic_probe as _}; +type HmacSha256 = Hmac; + #[cfg(any(feature = "stm32l4a6zg", feature = "stm32h755zi", feature = "stm32h753zi"))] bind_interrupts!(struct Irqs { HASH_RNG => hash::InterruptHandler; @@ -73,6 +76,25 @@ async fn main(_spawner: Spawner) { info!("Software SHA-256 Digest: {:?}", sw_sha224_digest[..]); defmt::assert!(sha224_digest_buffer == sw_sha224_digest[..]); + let hmac_key: [u8; 64] = [0x55; 64]; + + // Compute HMAC in hardware. + let mut sha256hmac_context = hw_hasher.start(Algorithm::SHA256, DataType::Width8, Some(&hmac_key)); + hw_hasher.update_blocking(&mut sha256hmac_context, test_1); + hw_hasher.update_blocking(&mut sha256hmac_context, test_2); + let mut hw_hmac: [u8; 32] = [0; 32]; + hw_hasher.finish_blocking(sha256hmac_context, &mut hw_hmac); + + // Compute HMAC in software. + let mut sw_mac = HmacSha256::new_from_slice(&hmac_key).unwrap(); + sw_mac.update(test_1); + sw_mac.update(test_2); + let sw_hmac = sw_mac.finalize().into_bytes(); + + info!("Hardware HMAC: {:?}", hw_hmac); + info!("Software HMAC: {:?}", sw_hmac[..]); + defmt::assert!(hw_hmac == sw_hmac[..]); + info!("Test OK"); cortex_m::asm::bkpt(); }