@@ -533,6 +533,54 @@ int wc_HmacSetKey_ex(Hmac* hmac, int type, const byte* key, word32 length,
533533 return 0 ;
534534#else
535535
536+ #if defined(STM32_HASH ) && defined(STM32_HMAC )
537+ {
538+ word32 stmAlgo , stmBlockSize , stmDigestSize ;
539+ /* Check if this hash type is supported by STM32 HMAC hardware */
540+ if (wc_Stm32_Hmac_GetAlgoInfo (type , & stmAlgo , & stmBlockSize ,
541+ & stmDigestSize ) == 0 ) {
542+ /* Store raw key - pre-hash if longer than hash block size */
543+ if (length <= stmBlockSize ) {
544+ if (key != NULL ) {
545+ XMEMCPY (hmac -> stmKey , key , length );
546+ }
547+ hmac -> stmKeyLen = length ;
548+ }
549+ else {
550+ /* Pre-hash long key using STM32 HASH hardware */
551+ STM32_HASH_Context tmpCtx ;
552+ wc_Stm32_Hash_Init (& tmpCtx );
553+ ret = wolfSSL_CryptHwMutexLock ();
554+ if (ret == 0 ) {
555+ ret = wc_Stm32_Hash_Update (& tmpCtx , stmAlgo ,
556+ key , length , stmBlockSize );
557+ if (ret == 0 ) {
558+ ret = wc_Stm32_Hash_Final (& tmpCtx , stmAlgo ,
559+ hmac -> stmKey , stmDigestSize );
560+ }
561+ wolfSSL_CryptHwMutexUnLock ();
562+ }
563+ if (ret != 0 )
564+ return ret ;
565+ hmac -> stmKeyLen = stmDigestSize ;
566+ }
567+
568+ /* HW HMAC Phase 1: feed key */
569+ ret = wolfSSL_CryptHwMutexLock ();
570+ if (ret == 0 ) {
571+ ret = wc_Stm32_Hmac_SetKey (& hmac -> stmCtx , type ,
572+ hmac -> stmKey , hmac -> stmKeyLen );
573+ wolfSSL_CryptHwMutexUnLock ();
574+ }
575+ if (ret == 0 ) {
576+ hmac -> innerHashKeyed = WC_HMAC_INNER_HASH_KEYED_DEV ;
577+ }
578+ return ret ;
579+ }
580+ /* Unsupported algo falls through to software */
581+ }
582+ #endif /* STM32_HASH && STM32_HMAC */
583+
536584 ip = (byte * )hmac -> ipad ;
537585 op = (byte * )hmac -> opad ;
538586
@@ -853,6 +901,18 @@ int wc_HmacUpdate(Hmac* hmac, const byte* msg, word32 length)
853901 }
854902#endif /* WOLFSSL_ASYNC_CRYPT */
855903
904+ #if defined(STM32_HASH ) && defined(STM32_HMAC )
905+ if (hmac -> innerHashKeyed == WC_HMAC_INNER_HASH_KEYED_DEV ) {
906+ ret = wolfSSL_CryptHwMutexLock ();
907+ if (ret == 0 ) {
908+ ret = wc_Stm32_Hmac_Update (& hmac -> stmCtx , hmac -> macType ,
909+ msg , length );
910+ wolfSSL_CryptHwMutexUnLock ();
911+ }
912+ return ret ;
913+ }
914+ #endif /* STM32_HASH && STM32_HMAC */
915+
856916 if (!hmac -> innerHashKeyed ) {
857917#ifndef WOLFSSL_HMAC_COPY_HASH
858918 ret = HmacKeyHashUpdate (hmac -> macType , & hmac -> hash , (byte * )hmac -> ipad );
@@ -970,6 +1030,21 @@ int wc_HmacFinal(Hmac* hmac, byte* hash)
9701030 }
9711031#endif /* WOLFSSL_ASYNC_CRYPT */
9721032
1033+ #if defined(STM32_HASH ) && defined(STM32_HMAC )
1034+ if (hmac -> innerHashKeyed == WC_HMAC_INNER_HASH_KEYED_DEV ) {
1035+ ret = wolfSSL_CryptHwMutexLock ();
1036+ if (ret == 0 ) {
1037+ ret = wc_Stm32_Hmac_Final (& hmac -> stmCtx , hmac -> macType ,
1038+ hmac -> stmKey , hmac -> stmKeyLen , hash );
1039+ wolfSSL_CryptHwMutexUnLock ();
1040+ }
1041+ if (ret == 0 ) {
1042+ hmac -> innerHashKeyed = 0 ;
1043+ }
1044+ return ret ;
1045+ }
1046+ #endif /* STM32_HASH && STM32_HMAC */
1047+
9731048 if (!hmac -> innerHashKeyed ) {
9741049#ifndef WOLFSSL_HMAC_COPY_HASH
9751050 ret = HmacKeyHashUpdate (hmac -> macType , & hmac -> hash , (byte * )hmac -> ipad );
0 commit comments