mirror of
https://github.com/embassy-rs/embassy.git
synced 2024-11-22 06:42:32 +00:00
Added HMAC example.
This commit is contained in:
parent
37c869827e
commit
f0f1f2d14c
@ -29,6 +29,7 @@ critical-section = "1.1"
|
||||
embedded-storage = "0.3.1"
|
||||
static_cell = "2"
|
||||
sha2 = { version = "0.10.8", default-features = false }
|
||||
hmac = "0.12.1"
|
||||
|
||||
[profile.release]
|
||||
debug = 2
|
||||
|
@ -6,9 +6,12 @@ use embassy_executor::Spawner;
|
||||
use embassy_stm32::hash::*;
|
||||
use embassy_stm32::{bind_interrupts, hash, peripherals, Config};
|
||||
use embassy_time::Instant;
|
||||
use hmac::{Hmac, Mac};
|
||||
use sha2::{Digest, Sha256};
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
type HmacSha256 = Hmac<Sha256>;
|
||||
|
||||
bind_interrupts!(struct Irqs {
|
||||
HASH_RNG => hash::InterruptHandler<peripherals::HASH>;
|
||||
});
|
||||
@ -52,5 +55,24 @@ async fn main(_spawner: Spawner) -> ! {
|
||||
info!("Software Execution Time: {:?}", sw_execution_time);
|
||||
assert_eq!(hw_digest, sw_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(&mut sha256hmac_context, test_1).await;
|
||||
hw_hasher.update(&mut sha256hmac_context, test_2).await;
|
||||
let mut hw_hmac: [u8; 32] = [0; 32];
|
||||
hw_hasher.finish(sha256hmac_context, &mut hw_hmac).await;
|
||||
|
||||
// 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[..]);
|
||||
assert_eq!(hw_hmac, sw_hmac[..]);
|
||||
|
||||
loop {}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user