Skip to content

Commit 034af8d

Browse files
authored
Merge pull request #7787 from dgarske/stm32u5a
Fix STM32 Hash FIFO and add support for STM32U5A9xx
2 parents 3b74a64 + 42403a5 commit 034af8d

6 files changed

Lines changed: 21 additions & 55 deletions

File tree

IDE/STM32Cube/default_conf.ftl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,11 @@ extern ${variable.value} ${variable.name};
148148
#define HAL_CONSOLE_UART huart2
149149
#define NO_STM32_RNG
150150
#define WOLFSSL_GENSEED_FORTEST /* no HW RNG is available use test seed */
151-
#elif defined(STM32U575xx) || defined(STM32U585xx)
151+
#elif defined(STM32U575xx) || defined(STM32U585xx) || defined(STM32U5A9xx)
152152
#define HAL_CONSOLE_UART huart1
153153
#define WOLFSSL_STM32U5
154154
#define STM32_HAL_V2
155-
#ifdef STM32U585xx
155+
#if defined(STM32U585xx) || defined(STM32U5A9xx)
156156
#undef NO_STM32_HASH
157157
#undef NO_STM32_CRYPTO
158158
#define WOLFSSL_STM32_PKA

IDE/STM32Cube/wolfssl_example.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ typedef struct {
284284
typedef struct {
285285
int ret;
286286

287-
osThreadId threadId;
287+
osThreadId_t threadId;
288288
#ifdef CMSIS_OS2_H_
289289
osSemaphoreId_t mutex;
290290
#else

wolfcrypt/src/port/st/stm32.c

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,11 @@ int wc_Stm32_Hash_Update(STM32_HASH_Context* stmCtx, word32 algo,
303303
int ret = 0;
304304
byte* local = (byte*)stmCtx->buffer;
305305
int wroteToFifo = 0;
306-
const word32 fifoSz = (STM32_HASH_FIFO_SIZE * STM32_HASH_REG_SIZE);
307306
word32 chunkSz;
308307

309308
#ifdef DEBUG_STM32_HASH
310-
printf("STM Hash Update: algo %x, len %d, blockSz %d\n",
311-
algo, len, blockSize);
309+
printf("STM Hash Update: algo %x, len %d, buffLen %d, fifoBytes %d\n",
310+
algo, len, stmCtx->buffLen, stmCtx->fifoBytes);
312311
#endif
313312
(void)blockSize;
314313

@@ -323,40 +322,27 @@ int wc_Stm32_Hash_Update(STM32_HASH_Context* stmCtx, word32 algo,
323322
/* restore hash context or init as new hash */
324323
wc_Stm32_Hash_RestoreContext(stmCtx, algo);
325324

326-
chunkSz = fifoSz;
327-
#ifdef STM32_HASH_FIFO_WORKAROUND
328-
/* if FIFO already has bytes written then fill remainder first */
329-
if (stmCtx->fifoBytes > 0) {
330-
chunkSz -= stmCtx->fifoBytes;
331-
stmCtx->fifoBytes = 0;
332-
}
333-
#endif
334-
335325
/* write blocks to FIFO */
336326
while (len) {
337-
word32 add = min(len, chunkSz - stmCtx->buffLen);
327+
word32 add;
328+
329+
/* fill the FIFO plus one additional to flush the block */
330+
chunkSz = ((STM32_HASH_FIFO_SIZE + 1) * STM32_HASH_REG_SIZE);
331+
/* account for extra bytes in the FIFO (use mask 0x3F to get remain) */
332+
chunkSz -= (stmCtx->fifoBytes &
333+
((STM32_HASH_FIFO_SIZE * STM32_HASH_REG_SIZE)-1));
334+
335+
add = min(len, chunkSz - stmCtx->buffLen);
338336
XMEMCPY(&local[stmCtx->buffLen], data, add);
339337

340338
stmCtx->buffLen += add;
341339
data += add;
342340
len -= add;
343341

344-
#ifdef STM32_HASH_FIFO_WORKAROUND
345-
/* We cannot leave the FIFO full and do save/restore
346-
* the last must be large enough to flush block from FIFO */
347-
if (stmCtx->buffLen + len <= fifoSz * 2) {
348-
chunkSz = fifoSz + STM32_HASH_REG_SIZE;
349-
}
350-
#endif
351-
352342
if (stmCtx->buffLen == chunkSz) {
353343
wc_Stm32_Hash_Data(stmCtx, stmCtx->buffLen);
354344
wroteToFifo = 1;
355-
#ifdef STM32_HASH_FIFO_WORKAROUND
356-
if (chunkSz > fifoSz)
357-
stmCtx->fifoBytes = chunkSz - fifoSz;
358-
chunkSz = fifoSz;
359-
#endif
345+
stmCtx->fifoBytes += chunkSz;
360346
}
361347
}
362348

@@ -380,7 +366,8 @@ int wc_Stm32_Hash_Final(STM32_HASH_Context* stmCtx, word32 algo,
380366
int ret = 0;
381367

382368
#ifdef DEBUG_STM32_HASH
383-
printf("STM Hash Final: algo %x, digestSz %d\n", algo, digestSize);
369+
printf("STM Hash Final: algo %x, digestSz %d, buffLen %d, fifoBytes %d\n",
370+
algo, digestSize, stmCtx->buffLen, stmCtx->fifoBytes);
384371
#endif
385372

386373
/* turn on hash clock */

wolfcrypt/src/sha256.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2496,7 +2496,7 @@ int wc_Sha256GetHash(wc_Sha256* sha256, byte* hash)
24962496
ret = wc_Sha256Copy(sha256, tmpSha256);
24972497
if (ret == 0) {
24982498
ret = wc_Sha256Final(tmpSha256, hash);
2499-
wc_Sha256Free(tmpSha256); /* TODO move outside brackets? */
2499+
wc_Sha256Free(tmpSha256);
25002500
}
25012501

25022502

wolfssl/wolfcrypt/port/st/stm32.h

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -71,26 +71,6 @@
7171
#define STM32_HASH_REG_SIZE 4
7272
#define STM32_HASH_FIFO_SIZE 16 /* FIFO is 16 deep 32-bits wide */
7373

74-
#if (defined(WOLFSSL_STM32U5) || defined(WOLFSSL_STM32H5) || \
75-
defined(WOLFSSL_STM32H7)) && !defined(NO_STM32_HASH_FIFO_WORKAROUND)
76-
/* workaround for hash FIFO to write one extra to finalize */
77-
/* RM: Message Data Feeding: Data are entered into the HASH
78-
* one 32-bit word at a time, by writing them into the HASH_DIN register.
79-
* The current contents of the HASH_DIN register are transferred to the
80-
* 16 words input FIFO each time the register is written with new data.
81-
* Hence HASH_DIN and the FIFO form a seventeen 32-bit words length FIFO. */
82-
#undef STM32_HASH_BUFFER_SIZE
83-
#define STM32_HASH_BUFFER_SIZE 17
84-
85-
#undef STM32_HASH_FIFO_WORKAROUND
86-
#define STM32_HASH_FIFO_WORKAROUND
87-
#endif
88-
89-
#ifndef STM32_HASH_BUFFER_SIZE
90-
#define STM32_HASH_BUFFER_SIZE STM32_HASH_FIFO_SIZE
91-
#endif
92-
93-
9474
/* STM32 Hash Context */
9575
typedef struct {
9676
/* Context switching registers */
@@ -100,13 +80,11 @@ typedef struct {
10080
uint32_t HASH_CSR[HASH_CR_SIZE];
10181

10282
/* Hash state / buffers */
103-
word32 buffer[STM32_HASH_BUFFER_SIZE]; /* partial word buffer */
83+
word32 buffer[STM32_HASH_FIFO_SIZE+1]; /* partial word buffer */
10484
word32 buffLen; /* partial word remain */
10585
word32 loLen; /* total update bytes
10686
(only lsb 6-bits is used for nbr valid bytes in last word) */
107-
#ifdef STM32_HASH_FIFO_WORKAROUND
108-
int fifoBytes; /* number of currently filled FIFO bytes */
109-
#endif
87+
word32 fifoBytes; /* number of currently filled FIFO bytes */
11088
} STM32_HASH_Context;
11189

11290

wolfssl/wolfcrypt/tfm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,7 @@ int fp_sqr_comba64(fp_int *a, fp_int *b);
779779
#define MP_VAL FP_VAL /* invalid */
780780
#define MP_MEM FP_MEM /* memory error */
781781
#define MP_NOT_INF FP_NOT_INF /* point not at infinity */
782+
#define MP_RANGE FP_NOT_INF
782783
#define MP_OKAY FP_OKAY /* ok result */
783784
#define MP_NO FP_NO /* yes/no result */
784785
#define MP_YES FP_YES /* yes/no result */

0 commit comments

Comments
 (0)