-
Notifications
You must be signed in to change notification settings - Fork 925
Support for STM32 HMAC hardware #9745
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -162,7 +162,8 @@ static void wc_Stm32_Hash_SaveContext(STM32_HASH_Context* ctx) | |||||||||||
| #endif | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| static void wc_Stm32_Hash_RestoreContext(STM32_HASH_Context* ctx, int algo) | ||||||||||||
| static void wc_Stm32_Hash_RestoreContext(STM32_HASH_Context* ctx, word32 algo, | ||||||||||||
| word32 mode) | ||||||||||||
| { | ||||||||||||
| int i; | ||||||||||||
|
|
||||||||||||
|
|
@@ -182,8 +183,10 @@ static void wc_Stm32_Hash_RestoreContext(STM32_HASH_Context* ctx, int algo) | |||||||||||
| #endif | ||||||||||||
| ); | ||||||||||||
|
|
||||||||||||
| /* configure algorithm, mode and data type */ | ||||||||||||
| HASH->CR |= (algo | HASH_ALGOMODE_HASH | HASH_DATATYPE_8B); | ||||||||||||
| /* configure algorithm, mode and data type | ||||||||||||
| * mode is HASH_ALGOMODE_HASH or HASH_ALGOMODE_HMAC | ||||||||||||
| * (may include HASH_CR_LKEY for HMAC with long key) */ | ||||||||||||
| HASH->CR |= (algo | mode | HASH_DATATYPE_8B); | ||||||||||||
|
|
||||||||||||
| /* reset HASH processor */ | ||||||||||||
| HASH->CR |= HASH_CR_INIT; | ||||||||||||
|
|
@@ -192,7 +195,8 @@ static void wc_Stm32_Hash_RestoreContext(STM32_HASH_Context* ctx, int algo) | |||||||||||
| wc_Stm32_Hash_NumValidBits(0); | ||||||||||||
|
|
||||||||||||
| #ifdef DEBUG_STM32_HASH | ||||||||||||
| printf("STM Init algo %x\n", algo); | ||||||||||||
| printf("STM Init algo %x, mode %x\n", (unsigned int)algo, | ||||||||||||
| (unsigned int)mode); | ||||||||||||
| #endif | ||||||||||||
| } | ||||||||||||
| else { | ||||||||||||
|
|
@@ -363,7 +367,7 @@ int wc_Stm32_Hash_Update(STM32_HASH_Context* stmCtx, word32 algo, | |||||||||||
| STM32_HASH_CLOCK_ENABLE(stmCtx); | ||||||||||||
|
|
||||||||||||
| /* restore hash context or init as new hash */ | ||||||||||||
| wc_Stm32_Hash_RestoreContext(stmCtx, algo); | ||||||||||||
| wc_Stm32_Hash_RestoreContext(stmCtx, algo, HASH_ALGOMODE_HASH); | ||||||||||||
|
|
||||||||||||
| /* write blocks to FIFO */ | ||||||||||||
| while (len) { | ||||||||||||
|
|
@@ -417,7 +421,7 @@ int wc_Stm32_Hash_Final(STM32_HASH_Context* stmCtx, word32 algo, | |||||||||||
| STM32_HASH_CLOCK_ENABLE(stmCtx); | ||||||||||||
|
|
||||||||||||
| /* restore hash context or init as new hash */ | ||||||||||||
| wc_Stm32_Hash_RestoreContext(stmCtx, algo); | ||||||||||||
| wc_Stm32_Hash_RestoreContext(stmCtx, algo, HASH_ALGOMODE_HASH); | ||||||||||||
|
|
||||||||||||
| /* finish reading any trailing bytes into FIFO */ | ||||||||||||
| if (stmCtx->buffLen > 0) { | ||||||||||||
|
|
@@ -444,6 +448,206 @@ int wc_Stm32_Hash_Final(STM32_HASH_Context* stmCtx, word32 algo, | |||||||||||
| return ret; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| #if defined(STM32_HMAC) && !defined(NO_HMAC) | ||||||||||||
|
|
||||||||||||
| /* STM32 Port HMAC Functions */ | ||||||||||||
| #include <wolfssl/wolfcrypt/hmac.h> | ||||||||||||
|
|
||||||||||||
| int wc_Stm32_Hmac_GetAlgoInfo(int macType, word32* algo, word32* blockSize, | ||||||||||||
| word32* digestSize) | ||||||||||||
| { | ||||||||||||
| int ret = 0; | ||||||||||||
|
|
||||||||||||
| switch (macType) { | ||||||||||||
| #if !defined(NO_MD5) && !defined(STM32_NOMD5) | ||||||||||||
| case WC_MD5: | ||||||||||||
| if (algo) *algo = HASH_AlgoSelection_MD5; | ||||||||||||
| if (blockSize) *blockSize = WC_MD5_BLOCK_SIZE; | ||||||||||||
| if (digestSize) *digestSize = WC_MD5_DIGEST_SIZE; | ||||||||||||
| break; | ||||||||||||
| #endif | ||||||||||||
| #ifndef NO_SHA | ||||||||||||
| case WC_SHA: | ||||||||||||
| if (algo) *algo = HASH_AlgoSelection_SHA1; | ||||||||||||
| if (blockSize) *blockSize = WC_SHA_BLOCK_SIZE; | ||||||||||||
| if (digestSize) *digestSize = WC_SHA_DIGEST_SIZE; | ||||||||||||
| break; | ||||||||||||
| #endif | ||||||||||||
| #ifdef WOLFSSL_SHA224 | ||||||||||||
| case WC_SHA224: | ||||||||||||
| if (algo) *algo = HASH_AlgoSelection_SHA224; | ||||||||||||
| if (blockSize) *blockSize = WC_SHA224_BLOCK_SIZE; | ||||||||||||
| if (digestSize) *digestSize = WC_SHA224_DIGEST_SIZE; | ||||||||||||
| break; | ||||||||||||
| #endif | ||||||||||||
| #ifndef NO_SHA256 | ||||||||||||
| case WC_SHA256: | ||||||||||||
| if (algo) *algo = HASH_AlgoSelection_SHA256; | ||||||||||||
| if (blockSize) *blockSize = WC_SHA256_BLOCK_SIZE; | ||||||||||||
| if (digestSize) *digestSize = WC_SHA256_DIGEST_SIZE; | ||||||||||||
| break; | ||||||||||||
| #endif | ||||||||||||
| #if defined(STM32_HASH_SHA384) && defined(WOLFSSL_SHA384) | ||||||||||||
| case WC_SHA384: | ||||||||||||
| if (algo) *algo = HASH_ALGOSELECTION_SHA384; | ||||||||||||
| if (blockSize) *blockSize = WC_SHA384_BLOCK_SIZE; | ||||||||||||
| if (digestSize) *digestSize = WC_SHA384_DIGEST_SIZE; | ||||||||||||
| break; | ||||||||||||
| #endif | ||||||||||||
| #if defined(STM32_HASH_SHA512) && defined(WOLFSSL_SHA512) | ||||||||||||
| case WC_SHA512: | ||||||||||||
| if (algo) *algo = HASH_ALGOSELECTION_SHA512; | ||||||||||||
|
||||||||||||
| if (algo) *algo = HASH_ALGOSELECTION_SHA512; | |
| if (algo) *algo = HASH_AlgoSelection_SHA512; |
Copilot
AI
Feb 13, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The HASH_CR_LKEY check at line 566 is unreachable dead code. When the original key is longer than blockSize, it is always pre-hashed in hmac.c (lines 555-570) before being passed to wc_Stm32_Hmac_SetKey. By the time this function checks keySz, it will be the digestSize (not the original key length), which is always less than blockSize. This means the condition "keySz > blockSize" will never be true. Either remove this dead code block (lines 565-569), or restructure the code to let the STM32 hardware handle key hashing by passing the original long key and setting HASH_CR_LKEY appropriately.
| #ifdef HASH_CR_LKEY | |
| if (keySz > blockSize) { | |
| mode |= HASH_CR_LKEY; | |
| } | |
| #endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent naming convention used for SHA384 algorithm constant. The code uses
HASH_ALGOSELECTION_SHA384(all caps) while earlier hash algorithms useHASH_AlgoSelection_SHA1,HASH_AlgoSelection_SHA224, andHASH_AlgoSelection_SHA256(mixed case). This should be changed toHASH_AlgoSelection_SHA384for consistency with the naming pattern used by other hash algorithms.