@@ -9,9 +9,12 @@ use embassy_executor::Spawner;
9
9
use embassy_stm32:: dma:: NoDma ;
10
10
use embassy_stm32:: hash:: * ;
11
11
use embassy_stm32:: { bind_interrupts, hash, peripherals} ;
12
+ use hmac:: { Hmac , Mac } ;
12
13
use sha2:: { Digest , Sha224 , Sha256 } ;
13
14
use { defmt_rtt as _, panic_probe as _} ;
14
15
16
+ type HmacSha256 = Hmac < Sha256 > ;
17
+
15
18
#[ cfg( any( feature = "stm32l4a6zg" , feature = "stm32h755zi" , feature = "stm32h753zi" ) ) ]
16
19
bind_interrupts ! ( struct Irqs {
17
20
HASH_RNG => hash:: InterruptHandler <peripherals:: HASH >;
@@ -73,6 +76,25 @@ async fn main(_spawner: Spawner) {
73
76
info ! ( "Software SHA-256 Digest: {:?}" , sw_sha224_digest[ ..] ) ;
74
77
defmt:: assert!( sha224_digest_buffer == sw_sha224_digest[ ..] ) ;
75
78
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
+
76
98
info ! ( "Test OK" ) ;
77
99
cortex_m:: asm:: bkpt ( ) ;
78
100
}
0 commit comments