@@ -223,6 +223,22 @@ static const char *REG_NAMES[REGS_COUNT] = {
223
223
};
224
224
#undef REG_NAME_ENTRY
225
225
226
+ enum OtHMACDigestSize {
227
+ HMAC_SHA2_256 ,
228
+ HMAC_SHA2_384 ,
229
+ HMAC_SHA2_512 ,
230
+ HMAC_SHA2_NONE ,
231
+ };
232
+
233
+ enum OtHMACKeyLength {
234
+ HMAC_KEY_128 ,
235
+ HMAC_KEY_256 ,
236
+ HMAC_KEY_384 ,
237
+ HMAC_KEY_512 ,
238
+ HMAC_KEY_1024 ,
239
+ HMAC_KEY_NONE ,
240
+ };
241
+
226
242
struct OtHMACRegisters {
227
243
uint32_t intr_state ;
228
244
uint32_t intr_enable ;
@@ -239,6 +255,7 @@ typedef struct OtHMACRegisters OtHMACRegisters;
239
255
240
256
struct OtHMACContext {
241
257
hash_state state ;
258
+ enum OtHMACDigestSize digest_size_started ;
242
259
};
243
260
typedef struct OtHMACContext OtHMACContext ;
244
261
@@ -260,22 +277,6 @@ struct OtHMACState {
260
277
char * ot_id ;
261
278
};
262
279
263
- enum OtHMACDigestSize {
264
- HMAC_SHA2_256 ,
265
- HMAC_SHA2_384 ,
266
- HMAC_SHA2_512 ,
267
- HMAC_SHA2_NONE ,
268
- };
269
-
270
- enum OtHMACKeyLength {
271
- HMAC_KEY_128 ,
272
- HMAC_KEY_256 ,
273
- HMAC_KEY_384 ,
274
- HMAC_KEY_512 ,
275
- HMAC_KEY_1024 ,
276
- HMAC_KEY_NONE ,
277
- };
278
-
279
280
static inline enum OtHMACDigestSize ot_hmac_get_digest_size (uint32_t cfg_reg )
280
281
{
281
282
switch ((cfg_reg & R_CFG_DIGEST_SIZE_MASK ) >> R_CFG_DIGEST_SIZE_SHIFT ) {
@@ -291,9 +292,9 @@ static inline enum OtHMACDigestSize ot_hmac_get_digest_size(uint32_t cfg_reg)
291
292
}
292
293
}
293
294
294
- static inline size_t ot_hmac_get_digest_bytes (OtHMACState * s )
295
+ static inline size_t ot_hmac_get_digest_bytes (enum OtHMACDigestSize digest_size )
295
296
{
296
- switch (ot_hmac_get_digest_size ( s -> regs -> cfg ) ) {
297
+ switch (digest_size ) {
297
298
case HMAC_SHA2_256 :
298
299
return 32u ;
299
300
case HMAC_SHA2_384 :
@@ -399,7 +400,7 @@ static void ot_hmac_writeback_digest_state(OtHMACState *s)
399
400
{
400
401
/* copy intermediary digest to mock HMAC operation for stop/continue
401
402
behaviour. */
402
- switch (ot_hmac_get_digest_size ( s -> regs -> cfg ) ) {
403
+ switch (s -> ctx -> digest_size_started ) {
403
404
case HMAC_SHA2_256 :
404
405
for (unsigned idx = 0 ; idx < 8u ; idx ++ ) {
405
406
STORE32H (s -> ctx -> state .sha256 .state [idx ], s -> regs -> digest + idx );
@@ -426,7 +427,7 @@ static void ot_hmac_writeback_digest_state(OtHMACState *s)
426
427
427
428
static void ot_hmac_restore_context (OtHMACState * s )
428
429
{
429
- switch (ot_hmac_get_digest_size ( s -> regs -> cfg ) ) {
430
+ switch (s -> ctx -> digest_size_started ) {
430
431
case HMAC_SHA2_256 :
431
432
s -> ctx -> state .sha256 .curlen = 0 ;
432
433
s -> ctx -> state .sha256 .length = s -> regs -> msg_length ;
@@ -458,7 +459,7 @@ static void ot_hmac_restore_context(OtHMACState *s)
458
459
459
460
static size_t ot_hmac_get_curlen (OtHMACState * s )
460
461
{
461
- switch (ot_hmac_get_digest_size ( s -> regs -> cfg ) ) {
462
+ switch (s -> ctx -> digest_size_started ) {
462
463
case HMAC_SHA2_256 :
463
464
return s -> ctx -> state .sha256 .curlen ;
464
465
case HMAC_SHA2_384 :
@@ -476,7 +477,7 @@ static size_t ot_hmac_get_curlen(OtHMACState *s)
476
477
477
478
static void ot_hmac_sha_init (OtHMACState * s , bool write_back )
478
479
{
479
- switch (ot_hmac_get_digest_size ( s -> regs -> cfg ) ) {
480
+ switch (s -> ctx -> digest_size_started ) {
480
481
case HMAC_SHA2_256 :
481
482
sha256_init (& s -> ctx -> state );
482
483
break ;
@@ -502,7 +503,7 @@ static void ot_hmac_sha_init(OtHMACState *s, bool write_back)
502
503
static void ot_hmac_sha_process (OtHMACState * s , const uint8_t * in , size_t inlen ,
503
504
bool write_back )
504
505
{
505
- switch (ot_hmac_get_digest_size ( s -> regs -> cfg ) ) {
506
+ switch (s -> ctx -> digest_size_started ) {
506
507
case HMAC_SHA2_256 :
507
508
sha256_process (& s -> ctx -> state , in , inlen );
508
509
break ;
@@ -528,7 +529,7 @@ static void ot_hmac_sha_process(OtHMACState *s, const uint8_t *in, size_t inlen,
528
529
529
530
static void ot_hmac_sha_done (OtHMACState * s )
530
531
{
531
- switch (ot_hmac_get_digest_size ( s -> regs -> cfg ) ) {
532
+ switch (s -> ctx -> digest_size_started ) {
532
533
case HMAC_SHA2_256 :
533
534
sha256_done (& s -> ctx -> state , (uint8_t * )s -> regs -> digest );
534
535
return ;
@@ -571,7 +572,9 @@ static void ot_hmac_compute_digest(OtHMACState *s)
571
572
ot_hmac_sha_init (s , false);
572
573
ot_hmac_sha_process (s , (const uint8_t * )opad , pad_length_b , false);
573
574
ot_hmac_sha_process (s , (const uint8_t * )s -> regs -> digest ,
574
- ot_hmac_get_digest_bytes (s ), true);
575
+ ot_hmac_get_digest_bytes (
576
+ s -> ctx -> digest_size_started ),
577
+ true);
575
578
}
576
579
ot_hmac_sha_done (s );
577
580
}
@@ -911,6 +914,10 @@ static void ot_hmac_regs_write(void *opaque, hwaddr addr, uint64_t value,
911
914
912
915
ibex_irq_set (& s -> clkmgr , true);
913
916
917
+ /* Hold the previous digest size until the HMAC is started with the
918
+ new digest size configured */
919
+ s -> ctx -> digest_size_started = ot_hmac_get_digest_size (s -> regs -> cfg );
920
+
914
921
ot_hmac_sha_init (s , true);
915
922
916
923
/* HMAC mode, process input padding */
@@ -976,6 +983,10 @@ static void ot_hmac_regs_write(void *opaque, hwaddr addr, uint64_t value,
976
983
977
984
s -> regs -> cmd = R_CMD_HASH_CONTINUE_MASK ;
978
985
986
+ /* Hold the previous digest size until the HMAC is started with the
987
+ new digest size configured */
988
+ s -> ctx -> digest_size_started = ot_hmac_get_digest_size (s -> regs -> cfg );
989
+
979
990
ot_hmac_restore_context (s );
980
991
981
992
/* trigger delayed processing of FIFO */
0 commit comments