Skip to content

Commit 299f39c

Browse files
committed
Improve the restore context function to avoid duplicate code. Reduce stack use.
1 parent d3d4f8e commit 299f39c

4 files changed

Lines changed: 52 additions & 130 deletions

File tree

wolfcrypt/src/hmac.c

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -539,24 +539,29 @@ int wc_HmacSetKey_ex(Hmac* hmac, int type, const byte* key, word32 length,
539539
/* Check if this hash type is supported by STM32 HMAC hardware */
540540
if (wc_Stm32_Hmac_GetAlgoInfo(type, &stmAlgo, &stmBlockSize,
541541
&stmDigestSize) == 0) {
542-
/* Store raw key - pre-hash if longer than hash block size */
542+
/* Cache algo info for Update/Final */
543+
hmac->stmAlgo = stmAlgo;
544+
hmac->stmBlockSize = stmBlockSize;
545+
hmac->stmDigestSize = stmDigestSize;
546+
547+
/* Store raw key in ipad (unused in HW HMAC mode).
548+
* Pre-hash if longer than hash block size. */
543549
if (length <= stmBlockSize) {
544550
if (key != NULL) {
545-
XMEMCPY(hmac->stmKey, key, length);
551+
XMEMCPY(hmac->ipad, key, length);
546552
}
547553
hmac->stmKeyLen = length;
548554
}
549555
else {
550-
/* Pre-hash long key using STM32 HASH hardware */
551-
STM32_HASH_Context tmpCtx;
552-
wc_Stm32_Hash_Init(&tmpCtx);
556+
/* Pre-hash long key using stmCtx (re-initialized below) */
557+
wc_Stm32_Hash_Init(&hmac->stmCtx);
553558
ret = wolfSSL_CryptHwMutexLock();
554559
if (ret == 0) {
555-
ret = wc_Stm32_Hash_Update(&tmpCtx, stmAlgo,
560+
ret = wc_Stm32_Hash_Update(&hmac->stmCtx, stmAlgo,
556561
key, length, stmBlockSize);
557562
if (ret == 0) {
558-
ret = wc_Stm32_Hash_Final(&tmpCtx, stmAlgo,
559-
hmac->stmKey, stmDigestSize);
563+
ret = wc_Stm32_Hash_Final(&hmac->stmCtx, stmAlgo,
564+
(byte*)hmac->ipad, stmDigestSize);
560565
}
561566
wolfSSL_CryptHwMutexUnLock();
562567
}
@@ -569,7 +574,7 @@ int wc_HmacSetKey_ex(Hmac* hmac, int type, const byte* key, word32 length,
569574
ret = wolfSSL_CryptHwMutexLock();
570575
if (ret == 0) {
571576
ret = wc_Stm32_Hmac_SetKey(&hmac->stmCtx, type,
572-
hmac->stmKey, hmac->stmKeyLen);
577+
(const byte*)hmac->ipad, hmac->stmKeyLen);
573578
wolfSSL_CryptHwMutexUnLock();
574579
}
575580
if (ret == 0) {
@@ -905,8 +910,8 @@ int wc_HmacUpdate(Hmac* hmac, const byte* msg, word32 length)
905910
if (hmac->innerHashKeyed == WC_HMAC_INNER_HASH_KEYED_DEV) {
906911
ret = wolfSSL_CryptHwMutexLock();
907912
if (ret == 0) {
908-
ret = wc_Stm32_Hmac_Update(&hmac->stmCtx, hmac->macType,
909-
msg, length);
913+
ret = wc_Stm32_Hmac_Update(&hmac->stmCtx, hmac->stmAlgo,
914+
msg, length, hmac->stmBlockSize);
910915
wolfSSL_CryptHwMutexUnLock();
911916
}
912917
return ret;
@@ -1034,8 +1039,9 @@ int wc_HmacFinal(Hmac* hmac, byte* hash)
10341039
if (hmac->innerHashKeyed == WC_HMAC_INNER_HASH_KEYED_DEV) {
10351040
ret = wolfSSL_CryptHwMutexLock();
10361041
if (ret == 0) {
1037-
ret = wc_Stm32_Hmac_Final(&hmac->stmCtx, hmac->macType,
1038-
hmac->stmKey, hmac->stmKeyLen, hash);
1042+
ret = wc_Stm32_Hmac_Final(&hmac->stmCtx, hmac->stmAlgo,
1043+
(const byte*)hmac->ipad, hmac->stmKeyLen, hash,
1044+
hmac->stmDigestSize);
10391045
wolfSSL_CryptHwMutexUnLock();
10401046
}
10411047
if (ret == 0) {

wolfcrypt/src/port/st/stm32.c

Lines changed: 24 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
576512
static 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) {

wolfssl/wolfcrypt/hmac.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,10 @@ struct Hmac {
160160
#endif
161161
#if defined(STM32_HASH) && defined(STM32_HMAC)
162162
STM32_HASH_Context stmCtx;
163-
byte stmKey[WC_HMAC_BLOCK_SIZE]; /* raw key for HW HMAC phases 1 & 3 */
164-
word32 stmKeyLen;
163+
word32 stmAlgo; /* cached STM32 HASH algo selection */
164+
word32 stmBlockSize; /* cached hash block size */
165+
word32 stmDigestSize; /* cached digest size */
166+
word32 stmKeyLen; /* key length (raw key stored in ipad) */
165167
#endif
166168
};
167169

wolfssl/wolfcrypt/port/st/stm32.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,11 @@ int wc_Stm32_Hmac_GetAlgoInfo(int macType, word32* algo, word32* blockSize,
140140
word32* digestSize);
141141
int wc_Stm32_Hmac_SetKey(STM32_HASH_Context* stmCtx, int macType,
142142
const byte* key, word32 keySz);
143-
int wc_Stm32_Hmac_Update(STM32_HASH_Context* stmCtx, int macType,
144-
const byte* data, word32 len);
145-
int wc_Stm32_Hmac_Final(STM32_HASH_Context* stmCtx, int macType,
146-
const byte* key, word32 keySz, byte* hash);
143+
/* HMAC Update uses the same data feeding as Hash Update */
144+
#define wc_Stm32_Hmac_Update(stmCtx, algo, data, len, blockSize) \
145+
wc_Stm32_Hash_Update((stmCtx), (algo), (data), (len), (blockSize))
146+
int wc_Stm32_Hmac_Final(STM32_HASH_Context* stmCtx, word32 algo,
147+
const byte* key, word32 keySz, byte* hash, word32 digestSize);
147148
#endif /* STM32_HMAC */
148149

149150
#endif /* STM32_HASH */

0 commit comments

Comments
 (0)