@@ -282,27 +282,62 @@ static void ot_hmac_report_error(OtHMACState *s, uint32_t error)
282
282
ot_hmac_update_irqs (s );
283
283
}
284
284
285
+ static void ot_hmac_writeback_digest_state (OtHMACState * s )
286
+ {
287
+ /* copy intermediary digest to mock HMAC operation for stop/continue
288
+ behaviour. */
289
+ /* TODO: add support for SHA2-384 and SHA2-512 */
290
+ unsigned digest_length = OT_HMAC_DIGEST_LENGTH / sizeof (uint32_t );
291
+ for (unsigned i = 0 ; i < digest_length ; i ++ ) {
292
+ STORE32H (s -> ctx -> state .sha256 .state [i ], s -> regs -> digest + i );
293
+ }
294
+ }
295
+
296
+ static void ot_hmac_sha_init (OtHMACState * s , bool write_back )
297
+ {
298
+ /* TODO: add support for SHA2-384 and SHA2-512 */
299
+ sha256_init (& s -> ctx -> state );
300
+ if (write_back ) {
301
+ ot_hmac_writeback_digest_state (s );
302
+ }
303
+ }
304
+
305
+ static void ot_hmac_sha_process (OtHMACState * s , const uint8_t * in , size_t inlen ,
306
+ bool write_back )
307
+ {
308
+ /* TODO: add support for SHA2-384 and SHA2-512 */
309
+ sha256_process (& s -> ctx -> state , in , inlen );
310
+ if (write_back ) {
311
+ ot_hmac_writeback_digest_state (s );
312
+ }
313
+ }
314
+
315
+ static void ot_hmac_sha_done (OtHMACState * s )
316
+ {
317
+ /* TODO: add support for SHA2-384 and SHA2-512 */
318
+ sha256_done (& s -> ctx -> state , (uint8_t * )s -> regs -> digest );
319
+ }
320
+
285
321
static void ot_hmac_compute_digest (OtHMACState * s )
286
322
{
287
323
trace_ot_hmac_debug (s -> ot_id , __func__ );
288
324
289
325
/* HMAC mode, perform outer hash */
290
326
if (s -> regs -> cfg & R_CFG_HMAC_EN_MASK ) {
291
- sha256_done ( & s -> ctx -> state , ( uint8_t * ) s -> regs -> digest );
327
+ ot_hmac_sha_done ( s );
292
328
293
329
uint64_t opad [8u ];
294
330
memset (opad , 0 , sizeof (opad ));
295
331
memcpy (opad , s -> regs -> key , sizeof (s -> regs -> key ));
296
332
for (unsigned i = 0 ; i < ARRAY_SIZE (opad ); i ++ ) {
297
333
opad [i ] ^= 0x5c5c5c5c5c5c5c5cull ;
298
334
}
299
- sha256_init ( & s -> ctx -> state );
300
- sha256_process ( & s -> ctx -> state , (const uint8_t * )opad , sizeof (opad ));
301
- sha256_process ( & s -> ctx -> state , (const uint8_t * )s -> regs -> digest ,
302
- sizeof (s -> regs -> digest ));
335
+ ot_hmac_sha_init ( s , false );
336
+ ot_hmac_sha_process ( s , (const uint8_t * )opad , sizeof (opad ), false );
337
+ ot_hmac_sha_process ( s , (const uint8_t * )s -> regs -> digest ,
338
+ sizeof (s -> regs -> digest ), true );
303
339
}
304
-
305
- sha256_done (& s -> ctx -> state , (uint8_t * )s -> regs -> digest );
340
+ ot_hmac_sha_done (s );
306
341
}
307
342
308
343
static void ot_hmac_process_fifo (OtHMACState * s )
@@ -312,7 +347,7 @@ static void ot_hmac_process_fifo(OtHMACState *s)
312
347
if (!fifo8_is_empty (& s -> input_fifo )) {
313
348
while (!fifo8_is_empty (& s -> input_fifo )) {
314
349
uint8_t value = fifo8_pop (& s -> input_fifo );
315
- sha256_process ( & s -> ctx -> state , & value , 1 );
350
+ ot_hmac_sha_process ( s , & value , 1u , false );
316
351
}
317
352
318
353
/* assert FIFO Empty IRQ */
@@ -582,7 +617,7 @@ static void ot_hmac_regs_write(void *opaque, hwaddr addr, uint64_t value,
582
617
583
618
ibex_irq_set (& s -> clkmgr , true);
584
619
585
- sha256_init ( & s -> ctx -> state );
620
+ ot_hmac_sha_init ( s , true );
586
621
587
622
/* HMAC mode, process input padding */
588
623
if (s -> regs -> cfg & R_CFG_HMAC_EN_MASK ) {
@@ -592,8 +627,8 @@ static void ot_hmac_regs_write(void *opaque, hwaddr addr, uint64_t value,
592
627
for (unsigned i = 0 ; i < ARRAY_SIZE (ipad ); i ++ ) {
593
628
ipad [i ] ^= 0x3636363636363636u ;
594
629
}
595
- sha256_process ( & s -> ctx -> state , (const uint8_t * )ipad ,
596
- sizeof ( ipad ) );
630
+ ot_hmac_sha_process ( s , (const uint8_t * )ipad , sizeof ( ipad ) ,
631
+ true );
597
632
}
598
633
}
599
634
0 commit comments