Added HMAC to STM32 hash test.
This commit is contained in:
parent
14a678fe45
commit
f0045b9217
2 changed files with 23 additions and 0 deletions
|
@ -76,6 +76,7 @@ portable-atomic = { version = "1.5", features = [] }
|
||||||
|
|
||||||
chrono = { version = "^0.4", default-features = false, optional = true}
|
chrono = { version = "^0.4", default-features = false, optional = true}
|
||||||
sha2 = { version = "0.10.8", default-features = false }
|
sha2 = { version = "0.10.8", default-features = false }
|
||||||
|
hmac = "0.12.1"
|
||||||
|
|
||||||
# BEGIN TESTS
|
# BEGIN TESTS
|
||||||
# Generated by gen_test.py. DO NOT EDIT.
|
# Generated by gen_test.py. DO NOT EDIT.
|
||||||
|
|
|
@ -9,9 +9,12 @@ use embassy_executor::Spawner;
|
||||||
use embassy_stm32::dma::NoDma;
|
use embassy_stm32::dma::NoDma;
|
||||||
use embassy_stm32::hash::*;
|
use embassy_stm32::hash::*;
|
||||||
use embassy_stm32::{bind_interrupts, hash, peripherals};
|
use embassy_stm32::{bind_interrupts, hash, peripherals};
|
||||||
|
use hmac::{Hmac, Mac};
|
||||||
use sha2::{Digest, Sha224, Sha256};
|
use sha2::{Digest, Sha224, Sha256};
|
||||||
use {defmt_rtt as _, panic_probe as _};
|
use {defmt_rtt as _, panic_probe as _};
|
||||||
|
|
||||||
|
type HmacSha256 = Hmac<Sha256>;
|
||||||
|
|
||||||
#[cfg(any(feature = "stm32l4a6zg", feature = "stm32h755zi", feature = "stm32h753zi"))]
|
#[cfg(any(feature = "stm32l4a6zg", feature = "stm32h755zi", feature = "stm32h753zi"))]
|
||||||
bind_interrupts!(struct Irqs {
|
bind_interrupts!(struct Irqs {
|
||||||
HASH_RNG => hash::InterruptHandler<peripherals::HASH>;
|
HASH_RNG => hash::InterruptHandler<peripherals::HASH>;
|
||||||
|
@ -73,6 +76,25 @@ async fn main(_spawner: Spawner) {
|
||||||
info!("Software SHA-256 Digest: {:?}", sw_sha224_digest[..]);
|
info!("Software SHA-256 Digest: {:?}", sw_sha224_digest[..]);
|
||||||
defmt::assert!(sha224_digest_buffer == 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");
|
info!("Test OK");
|
||||||
cortex_m::asm::bkpt();
|
cortex_m::asm::bkpt();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue