Skip to content

Commit 923ba97

Browse files
committed
Improve the hash wait logic by separating the data input ready from the digest calculation complete.
1 parent c12c3e0 commit 923ba97

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

wolfcrypt/src/port/st/stm32.c

+28-7
Original file line numberDiff line numberDiff line change
@@ -252,24 +252,45 @@ static void wc_Stm32_Hash_GetDigest(byte* hash, int digestSize)
252252
#endif
253253
}
254254

255-
static int wc_Stm32_Hash_WaitDone(STM32_HASH_Context* stmCtx)
255+
static int wc_Stm32_Hash_WaitDataReady(STM32_HASH_Context* stmCtx)
256256
{
257257
int timeout = 0;
258258
(void)stmCtx;
259259

260-
/* wait until not busy and hash digest / input block are complete */
261-
while (((HASH->SR & HASH_SR_BUSY)
260+
/* wait until not busy and data input buffer ready */
261+
while ((HASH->SR & HASH_SR_BUSY)
262262
#ifdef HASH_IMR_DCIE
263-
|| (HASH->SR & HASH_SR_DCIS) == 0
263+
&& (HASH->SR & HASH_SR_DCIS) == 0
264264
#endif
265+
&& ++timeout < STM32_HASH_TIMEOUT) {
266+
};
267+
268+
#ifdef DEBUG_STM32_HASH
269+
printf("STM Wait Data %d, HASH->SR %lx\n", timeout, HASH->SR);
270+
#endif
271+
272+
/* verify timeout did not occur */
273+
if (timeout >= STM32_HASH_TIMEOUT) {
274+
return WC_TIMEOUT_E;
275+
}
276+
return 0;
277+
}
278+
279+
static int wc_Stm32_Hash_WaitCalcComp(STM32_HASH_Context* stmCtx)
280+
{
281+
int timeout = 0;
282+
(void)stmCtx;
283+
284+
/* wait until not busy and hash digest calculation complete */
285+
while (((HASH->SR & HASH_SR_BUSY)
265286
#ifdef HASH_IMR_DINIE
266287
|| (HASH->SR & HASH_SR_DINIS) == 0
267288
#endif
268289
) && ++timeout < STM32_HASH_TIMEOUT) {
269290
};
270291

271292
#ifdef DEBUG_STM32_HASH
272-
printf("STM Wait done %d, HASH->SR %lx\n", timeout, HASH->SR);
293+
printf("STM Wait Calc %d, HASH->SR %lx\n", timeout, HASH->SR);
273294
#endif
274295

275296
/* verify timeout did not occur */
@@ -364,7 +385,7 @@ int wc_Stm32_Hash_Update(STM32_HASH_Context* stmCtx, word32 algo,
364385

365386
if (wroteToFifo) {
366387
/* make sure hash operation is done */
367-
ret = wc_Stm32_Hash_WaitDone(stmCtx);
388+
ret = wc_Stm32_Hash_WaitDataReady(stmCtx);
368389

369390
/* save hash state for next operation */
370391
wc_Stm32_Hash_SaveContext(stmCtx);
@@ -405,7 +426,7 @@ int wc_Stm32_Hash_Final(STM32_HASH_Context* stmCtx, word32 algo,
405426
HASH->STR |= HASH_STR_DCAL;
406427

407428
/* wait for hash done */
408-
ret = wc_Stm32_Hash_WaitDone(stmCtx);
429+
ret = wc_Stm32_Hash_WaitCalcComp(stmCtx);
409430
if (ret == 0) {
410431
/* read message digest */
411432
wc_Stm32_Hash_GetDigest(hash, digestSize);

0 commit comments

Comments
 (0)