@@ -162,7 +162,8 @@ static void wc_Stm32_Hash_SaveContext(STM32_HASH_Context* ctx)
162162#endif
163163}
164164
165- static void wc_Stm32_Hash_RestoreContext (STM32_HASH_Context * ctx , int algo )
165+ static void wc_Stm32_Hash_RestoreContext (STM32_HASH_Context * ctx , word32 algo ,
166+ word32 mode )
166167{
167168 int i ;
168169
@@ -182,8 +183,10 @@ static void wc_Stm32_Hash_RestoreContext(STM32_HASH_Context* ctx, int algo)
182183 #endif
183184 );
184185
185- /* configure algorithm, mode and data type */
186- HASH -> CR |= (algo | HASH_ALGOMODE_HASH | HASH_DATATYPE_8B );
186+ /* configure algorithm, mode and data type
187+ * mode is HASH_ALGOMODE_HASH or HASH_ALGOMODE_HMAC
188+ * (may include HASH_CR_LKEY for HMAC with long key) */
189+ HASH -> CR |= (algo | mode | HASH_DATATYPE_8B );
187190
188191 /* reset HASH processor */
189192 HASH -> CR |= HASH_CR_INIT ;
@@ -192,7 +195,8 @@ static void wc_Stm32_Hash_RestoreContext(STM32_HASH_Context* ctx, int algo)
192195 wc_Stm32_Hash_NumValidBits (0 );
193196
194197#ifdef DEBUG_STM32_HASH
195- printf ("STM Init algo %x\n" , algo );
198+ printf ("STM Init algo %x, mode %x\n" , (unsigned int )algo ,
199+ (unsigned int )mode );
196200#endif
197201 }
198202 else {
@@ -363,7 +367,7 @@ int wc_Stm32_Hash_Update(STM32_HASH_Context* stmCtx, word32 algo,
363367 STM32_HASH_CLOCK_ENABLE (stmCtx );
364368
365369 /* restore hash context or init as new hash */
366- wc_Stm32_Hash_RestoreContext (stmCtx , algo );
370+ wc_Stm32_Hash_RestoreContext (stmCtx , algo , HASH_ALGOMODE_HASH );
367371
368372 /* write blocks to FIFO */
369373 while (len ) {
@@ -417,7 +421,7 @@ int wc_Stm32_Hash_Final(STM32_HASH_Context* stmCtx, word32 algo,
417421 STM32_HASH_CLOCK_ENABLE (stmCtx );
418422
419423 /* restore hash context or init as new hash */
420- wc_Stm32_Hash_RestoreContext (stmCtx , algo );
424+ wc_Stm32_Hash_RestoreContext (stmCtx , algo , HASH_ALGOMODE_HASH );
421425
422426 /* finish reading any trailing bytes into FIFO */
423427 if (stmCtx -> buffLen > 0 ) {
@@ -505,74 +509,6 @@ int wc_Stm32_Hmac_GetAlgoInfo(int macType, word32* algo, word32* blockSize,
505509 return ret ;
506510}
507511
508- static void wc_Stm32_Hmac_RestoreContext (STM32_HASH_Context * ctx ,
509- word32 algo , word32 keyLen , word32 blockSize )
510- {
511- int i ;
512-
513- if (ctx -> HASH_CR == 0 ) {
514- /* first-time init */
515-
516- #if defined(HASH_IMR_DINIE ) && defined(HASH_IMR_DCIE )
517- /* Disable IRQ's */
518- HASH -> IMR &= ~(HASH_IMR_DINIE | HASH_IMR_DCIE );
519- #endif
520-
521- /* reset the control register */
522- HASH -> CR &= ~(HASH_CR_ALGO | HASH_CR_MODE | HASH_CR_DATATYPE
523- #ifdef HASH_CR_LKEY
524- | HASH_CR_LKEY
525- #endif
526- );
527-
528- /* configure algorithm, HMAC mode and data type */
529- HASH -> CR |= (algo | HASH_ALGOMODE_HMAC | HASH_DATATYPE_8B );
530-
531- #ifdef HASH_CR_LKEY
532- /* set LKEY bit if key is longer than block size */
533- if (keyLen > blockSize ) {
534- HASH -> CR |= HASH_CR_LKEY ;
535- }
536- #endif
537-
538- /* reset HASH processor */
539- HASH -> CR |= HASH_CR_INIT ;
540-
541- /* by default mark all bits valid */
542- wc_Stm32_Hash_NumValidBits (0 );
543-
544- #ifdef DEBUG_STM32_HASH
545- printf ("STM HMAC Init algo %x, keyLen %d\n" , (unsigned int )algo ,
546- (int )keyLen );
547- #endif
548- }
549- else {
550- /* restore context registers */
551- HASH -> IMR = ctx -> HASH_IMR ;
552- HASH -> STR = ctx -> HASH_STR ;
553- HASH -> CR = ctx -> HASH_CR ;
554- #ifdef STM32_HASH_SHA3
555- HASH -> SHA3CFGR = ctx -> SHA3CFGR ;
556- #endif
557-
558- /* Initialize the hash processor */
559- HASH -> CR |= HASH_CR_INIT ;
560-
561- /* continue restoring context registers */
562- for (i = 0 ; i < HASH_CR_SIZE ; i ++ ) {
563- HASH -> CSR [i ] = ctx -> HASH_CSR [i ];
564- }
565-
566- #ifdef DEBUG_STM32_HASH
567- printf ("STM HMAC Restore CR %lx, IMR %lx, STR %lx\n" ,
568- HASH -> CR , HASH -> IMR , HASH -> STR );
569- #endif
570- }
571-
572- (void )keyLen ;
573- (void )blockSize ;
574- }
575-
576512static void wc_Stm32_Hmac_FeedKey (const byte * key , word32 keySz )
577513{
578514 word32 i , blocks ;
@@ -605,6 +541,7 @@ int wc_Stm32_Hmac_SetKey(STM32_HASH_Context* stmCtx, int macType,
605541{
606542 int ret ;
607543 word32 algo , blockSize , digestSize ;
544+ word32 mode ;
608545
609546 if (stmCtx == NULL || key == NULL )
610547 return BAD_FUNC_ARG ;
@@ -624,7 +561,13 @@ int wc_Stm32_Hmac_SetKey(STM32_HASH_Context* stmCtx, int macType,
624561 STM32_HASH_CLOCK_ENABLE (stmCtx );
625562
626563 /* initialize hardware for HMAC mode */
627- wc_Stm32_Hmac_RestoreContext (stmCtx , algo , keySz , blockSize );
564+ mode = HASH_ALGOMODE_HMAC ;
565+ #ifdef HASH_CR_LKEY
566+ if (keySz > blockSize ) {
567+ mode |= HASH_CR_LKEY ;
568+ }
569+ #endif
570+ wc_Stm32_Hash_RestoreContext (stmCtx , algo , mode );
628571
629572 /* Phase 1: Feed key into HASH->DIN */
630573 wc_Stm32_Hmac_FeedKey (key , keySz );
@@ -647,55 +590,25 @@ int wc_Stm32_Hmac_SetKey(STM32_HASH_Context* stmCtx, int macType,
647590 return ret ;
648591}
649592
650- int wc_Stm32_Hmac_Update (STM32_HASH_Context * stmCtx , int macType ,
651- const byte * data , word32 len )
593+ int wc_Stm32_Hmac_Final (STM32_HASH_Context * stmCtx , word32 algo ,
594+ const byte * key , word32 keySz , byte * hash , word32 digestSize )
652595{
653596 int ret ;
654- word32 algo , blockSize , digestSize ;
655-
656- if (stmCtx == NULL || (data == NULL && len > 0 ))
657- return BAD_FUNC_ARG ;
658- if (len == 0 )
659- return 0 ;
660-
661- ret = wc_Stm32_Hmac_GetAlgoInfo (macType , & algo , & blockSize , & digestSize );
662- if (ret != 0 )
663- return ret ;
664-
665- #ifdef DEBUG_STM32_HASH
666- printf ("STM HMAC Update: macType %d, len %d\n" , macType , (int )len );
667- #endif
668-
669- /* Reuse Hash_Update - the saved context CR already contains
670- * HASH_ALGOMODE_HMAC, so RestoreContext will preserve HMAC mode */
671- ret = wc_Stm32_Hash_Update (stmCtx , algo , data , len , blockSize );
672-
673- return ret ;
674- }
675-
676- int wc_Stm32_Hmac_Final (STM32_HASH_Context * stmCtx , int macType ,
677- const byte * key , word32 keySz , byte * hash )
678- {
679- int ret ;
680- word32 algo , blockSize , digestSize ;
681597
682598 if (stmCtx == NULL || key == NULL || hash == NULL )
683599 return BAD_FUNC_ARG ;
684600
685- ret = wc_Stm32_Hmac_GetAlgoInfo (macType , & algo , & blockSize , & digestSize );
686- if (ret != 0 )
687- return ret ;
688-
689601#ifdef DEBUG_STM32_HASH
690- printf ("STM HMAC Final: macType %d, keySz %d, buffLen %d, fifoBytes %d\n" ,
691- macType , (int )keySz , (int )stmCtx -> buffLen , (int )stmCtx -> fifoBytes );
602+ printf ("STM HMAC Final: algo %x, keySz %d, buffLen %d, fifoBytes %d\n" ,
603+ (unsigned int )algo , (int )keySz , (int )stmCtx -> buffLen ,
604+ (int )stmCtx -> fifoBytes );
692605#endif
693606
694607 /* turn on hash clock */
695608 STM32_HASH_CLOCK_ENABLE (stmCtx );
696609
697610 /* restore HMAC context */
698- wc_Stm32_Hash_RestoreContext (stmCtx , algo );
611+ wc_Stm32_Hash_RestoreContext (stmCtx , algo , HASH_ALGOMODE_HMAC );
699612
700613 /* finish reading any trailing bytes into FIFO */
701614 if (stmCtx -> buffLen > 0 ) {
0 commit comments