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