Added HMAC to STM32 hash test.

This commit is contained in:
Caleb Garrett 2024-02-13 10:17:19 -05:00
parent 14a678fe45
commit f0045b9217
2 changed files with 23 additions and 0 deletions

View File

@ -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.

View File

@ -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<Sha256>;
#[cfg(any(feature = "stm32l4a6zg", feature = "stm32h755zi", feature = "stm32h753zi"))]
bind_interrupts!(struct Irqs {
HASH_RNG => hash::InterruptHandler<peripherals::HASH>;
@ -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();
}