Skip to content

Commit 0919f82

Browse files
committed
Support for STM32 HMAC hardware
1 parent 6a44159 commit 0919f82

6 files changed

Lines changed: 392 additions & 0 deletions

File tree

.wolfssl_known_macro_extras

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ GOAHEAD_WS
251251
HAL_RTC_MODULE_ENABLED
252252
HARDWARE_CACHE_COHERENCY
253253
HASH_AlgoMode_HASH
254+
HASH_AlgoMode_HMAC
254255
HASH_BYTE_SWAP
255256
HASH_CR_LKEY
256257
HASH_DIGEST
@@ -426,6 +427,7 @@ NO_SESSION_CACHE_ROW_LOCK
426427
NO_SKID
427428
NO_SKIP_PREVIEW
428429
NO_STDIO_FGETS_REMAP
430+
NO_STM32_HMAC
429431
NO_TKERNEL_MEM_POOL
430432
NO_TLSX_PSKKEM_PLAIN_ANNOUNCE
431433
NO_VERIFY_OID

wolfcrypt/src/hmac.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)