66#![ no_std]
77#![ no_main]
88
9+ use capsules_extra:: test:: hmac_sha256:: TestHmacSha256 ;
910use kernel:: capabilities;
1011use kernel:: component:: Component ;
1112use kernel:: debug:: PanicResources ;
@@ -51,6 +52,10 @@ struct NucleoU545RE {
5152 stm32u545:: tim:: Tim2 < ' static > ,
5253 > ,
5354 > ,
55+ test_hmac_sha256 : & ' static capsules_extra:: test:: hmac_sha256:: TestHmacSha256 <
56+ ' static ,
57+ stm32u545:: hash:: sha256:: Sha256Adapter < ' static > ,
58+ > ,
5459}
5560
5661impl SyscallDriverLookup for NucleoU545RE {
@@ -151,10 +156,17 @@ unsafe fn start() -> (
151156 ) ;
152157 usart1. register ( ) ;
153158
159+ let hash = static_init ! (
160+ stm32u545:: hash:: core_unit:: Hash <' static >,
161+ stm32u545:: hash:: core_unit:: Hash :: new( stm32u545:: hash:: regs:: HASH_BASE )
162+ ) ;
163+
164+ hash. register ( ) ;
165+
154166 // Load Peripherals Bundle
155167 let periphs = static_init ! (
156168 stm32u545:: chip:: Stm32u5xxDefaultPeripherals <' static >,
157- stm32u545:: chip:: Stm32u5xxDefaultPeripherals :: new( usart1, exti, dma1)
169+ stm32u545:: chip:: Stm32u5xxDefaultPeripherals :: new( usart1, exti, dma1, hash )
158170 ) ;
159171
160172 // Initialize wiring (DMA, clocks)
@@ -164,6 +176,16 @@ unsafe fn start() -> (
164176 periphs. tim2 . start ( ) ;
165177 set_pin_primary_functions ( periphs) ;
166178
179+ // Create an adapter for the HASH peripheral.
180+ // In this way it is ensured that only one mode is used by the peripheral.
181+ let sha256 = static_init ! (
182+ stm32u545:: hash:: sha256:: Sha256Adapter <' static >,
183+ stm32u545:: hash:: sha256:: Sha256Adapter :: new( hash)
184+ ) ;
185+
186+ // Adapter receives callbacks from the peripheral
187+ let _ = hash. set_sha256_adapter ( sha256) ;
188+
167189 // Kernel and Muxes
168190 let processes = components:: process_array:: ProcessArrayComponent :: new ( )
169191 . finalize ( components:: process_array_component_static!( NUM_PROCS ) ) ;
@@ -232,6 +254,50 @@ unsafe fn start() -> (
232254 )
233255 . finalize ( components:: button_component_static!( stm32u545:: gpio:: Pin ) ) ;
234256
257+ let hmac_key = static_init ! (
258+ [ u8 ; 64 ] ,
259+ [
260+ 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 0x0A , 0x0B , 0x0C , 0x0D ,
261+ 0x0E , 0x0F , 0x10 , 0x11 , 0x12 , 0x13 , 0x14 , 0x15 , 0x16 , 0x17 , 0x18 , 0x19 , 0x1A , 0x1B ,
262+ 0x1C , 0x1D , 0x1E , 0x1F , 0x20 , 0x21 , 0x22 , 0x23 , 0x24 , 0x25 , 0x26 , 0x27 , 0x28 , 0x29 ,
263+ 0x2A , 0x2B , 0x2C , 0x2D , 0x2E , 0x2F , 0x30 , 0x31 , 0x32 , 0x33 , 0x34 , 0x35 , 0x36 , 0x37 ,
264+ 0x38 , 0x39 , 0x3A , 0x3B , 0x3C , 0x3D , 0x3E , 0x3F
265+ ]
266+ ) ;
267+
268+ // An example from STM32 RM0456 Reference manual
269+ //
270+ // “Sample message for keylen = blocklen”
271+ let hash_data_buffer = static_init ! (
272+ [ u8 ; 34 ] ,
273+ [
274+ 0x53 , 0x61 , 0x6d , 0x70 , 0x6C , 0x65 , 0x20 , 0x6d , 0x65 , 0x73 , 0x73 , 0x61 , 0x67 , 0x65 ,
275+ 0x20 , 0x66 , 0x6f , 0x72 , 0x20 , 0x6b , 0x65 , 0x79 , 0x6C , 0x65 , 0x6e , 0x3d , 0x62 , 0x6c ,
276+ 0x6f , 0x63 , 0x6b , 0x6c , 0x65 , 0x6e
277+ ]
278+ ) ;
279+
280+ let hash_digest_buffer = static_init ! ( [ u8 ; 32 ] , [ 0u8 ; 32 ] ) ;
281+ let correct = static_init ! (
282+ [ u8 ; 32 ] ,
283+ [
284+ 0x8b , 0xb9 , 0xa1 , 0xdb , 0x98 , 0x06 , 0xf2 , 0x0d , 0xf7 , 0xf7 , 0x7b , 0x82 , 0x13 , 0x8c ,
285+ 0x79 , 0x14 , 0xd1 , 0x74 , 0xd5 , 0x9e , 0x13 , 0xdc , 0x4d , 0x01 , 0x69 , 0xc9 , 0x05 , 0x7b ,
286+ 0x13 , 0x3e , 0x1d , 0x62
287+ ]
288+ ) ;
289+
290+ let test_hmac_sha256 = static_init ! (
291+ TestHmacSha256 <' static , stm32u545:: hash:: sha256:: Sha256Adapter <' static >>,
292+ TestHmacSha256 :: new(
293+ sha256,
294+ hmac_key,
295+ hash_data_buffer,
296+ hash_digest_buffer,
297+ correct
298+ )
299+ ) ;
300+
235301 // Platform and Interrupts
236302 let platform = static_init ! (
237303 NucleoU545RE ,
@@ -243,6 +309,7 @@ unsafe fn start() -> (
243309 led,
244310 button,
245311 alarm,
312+ test_hmac_sha256
246313 }
247314 ) ;
248315
@@ -291,6 +358,7 @@ pub unsafe fn main() {
291358 let main_loop_capability = create_capability ! ( capabilities:: MainLoopCapability ) ;
292359
293360 let ( board_kernel, platform, chip) = start ( ) ;
361+ platform. test_hmac_sha256 . run ( ) ;
294362 // Hand over control to the Tock Kernel Loop
295363 board_kernel. kernel_loop :: < NucleoU545RE , ChipHw , { NUM_PROCS as u8 } > (
296364 platform,
0 commit comments