Skip to content

Commit

Permalink
Merge pull request #7787 from dgarske/stm32u5a
Browse files Browse the repository at this point in the history
Fix STM32 Hash FIFO and add support for STM32U5A9xx
  • Loading branch information
SparkiDev authored Jul 29, 2024
2 parents 3b74a64 + 42403a5 commit 034af8d
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 55 deletions.
4 changes: 2 additions & 2 deletions IDE/STM32Cube/default_conf.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ extern ${variable.value} ${variable.name};
#define HAL_CONSOLE_UART huart2
#define NO_STM32_RNG
#define WOLFSSL_GENSEED_FORTEST /* no HW RNG is available use test seed */
#elif defined(STM32U575xx) || defined(STM32U585xx)
#elif defined(STM32U575xx) || defined(STM32U585xx) || defined(STM32U5A9xx)
#define HAL_CONSOLE_UART huart1
#define WOLFSSL_STM32U5
#define STM32_HAL_V2
#ifdef STM32U585xx
#if defined(STM32U585xx) || defined(STM32U5A9xx)
#undef NO_STM32_HASH
#undef NO_STM32_CRYPTO
#define WOLFSSL_STM32_PKA
Expand Down
2 changes: 1 addition & 1 deletion IDE/STM32Cube/wolfssl_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ typedef struct {
typedef struct {
int ret;

osThreadId threadId;
osThreadId_t threadId;
#ifdef CMSIS_OS2_H_
osSemaphoreId_t mutex;
#else
Expand Down
41 changes: 14 additions & 27 deletions wolfcrypt/src/port/st/stm32.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,11 @@ int wc_Stm32_Hash_Update(STM32_HASH_Context* stmCtx, word32 algo,
int ret = 0;
byte* local = (byte*)stmCtx->buffer;
int wroteToFifo = 0;
const word32 fifoSz = (STM32_HASH_FIFO_SIZE * STM32_HASH_REG_SIZE);
word32 chunkSz;

#ifdef DEBUG_STM32_HASH
printf("STM Hash Update: algo %x, len %d, blockSz %d\n",
algo, len, blockSize);
printf("STM Hash Update: algo %x, len %d, buffLen %d, fifoBytes %d\n",
algo, len, stmCtx->buffLen, stmCtx->fifoBytes);
#endif
(void)blockSize;

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

chunkSz = fifoSz;
#ifdef STM32_HASH_FIFO_WORKAROUND
/* if FIFO already has bytes written then fill remainder first */
if (stmCtx->fifoBytes > 0) {
chunkSz -= stmCtx->fifoBytes;
stmCtx->fifoBytes = 0;
}
#endif

/* write blocks to FIFO */
while (len) {
word32 add = min(len, chunkSz - stmCtx->buffLen);
word32 add;

/* fill the FIFO plus one additional to flush the block */
chunkSz = ((STM32_HASH_FIFO_SIZE + 1) * STM32_HASH_REG_SIZE);
/* account for extra bytes in the FIFO (use mask 0x3F to get remain) */
chunkSz -= (stmCtx->fifoBytes &
((STM32_HASH_FIFO_SIZE * STM32_HASH_REG_SIZE)-1));

add = min(len, chunkSz - stmCtx->buffLen);
XMEMCPY(&local[stmCtx->buffLen], data, add);

stmCtx->buffLen += add;
data += add;
len -= add;

#ifdef STM32_HASH_FIFO_WORKAROUND
/* We cannot leave the FIFO full and do save/restore
* the last must be large enough to flush block from FIFO */
if (stmCtx->buffLen + len <= fifoSz * 2) {
chunkSz = fifoSz + STM32_HASH_REG_SIZE;
}
#endif

if (stmCtx->buffLen == chunkSz) {
wc_Stm32_Hash_Data(stmCtx, stmCtx->buffLen);
wroteToFifo = 1;
#ifdef STM32_HASH_FIFO_WORKAROUND
if (chunkSz > fifoSz)
stmCtx->fifoBytes = chunkSz - fifoSz;
chunkSz = fifoSz;
#endif
stmCtx->fifoBytes += chunkSz;
}
}

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

#ifdef DEBUG_STM32_HASH
printf("STM Hash Final: algo %x, digestSz %d\n", algo, digestSize);
printf("STM Hash Final: algo %x, digestSz %d, buffLen %d, fifoBytes %d\n",
algo, digestSize, stmCtx->buffLen, stmCtx->fifoBytes);
#endif

/* turn on hash clock */
Expand Down
2 changes: 1 addition & 1 deletion wolfcrypt/src/sha256.c
Original file line number Diff line number Diff line change
Expand Up @@ -2496,7 +2496,7 @@ int wc_Sha256GetHash(wc_Sha256* sha256, byte* hash)
ret = wc_Sha256Copy(sha256, tmpSha256);
if (ret == 0) {
ret = wc_Sha256Final(tmpSha256, hash);
wc_Sha256Free(tmpSha256); /* TODO move outside brackets? */
wc_Sha256Free(tmpSha256);
}


Expand Down
26 changes: 2 additions & 24 deletions wolfssl/wolfcrypt/port/st/stm32.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,6 @@
#define STM32_HASH_REG_SIZE 4
#define STM32_HASH_FIFO_SIZE 16 /* FIFO is 16 deep 32-bits wide */

#if (defined(WOLFSSL_STM32U5) || defined(WOLFSSL_STM32H5) || \
defined(WOLFSSL_STM32H7)) && !defined(NO_STM32_HASH_FIFO_WORKAROUND)
/* workaround for hash FIFO to write one extra to finalize */
/* RM: Message Data Feeding: Data are entered into the HASH
* one 32-bit word at a time, by writing them into the HASH_DIN register.
* The current contents of the HASH_DIN register are transferred to the
* 16 words input FIFO each time the register is written with new data.
* Hence HASH_DIN and the FIFO form a seventeen 32-bit words length FIFO. */
#undef STM32_HASH_BUFFER_SIZE
#define STM32_HASH_BUFFER_SIZE 17

#undef STM32_HASH_FIFO_WORKAROUND
#define STM32_HASH_FIFO_WORKAROUND
#endif

#ifndef STM32_HASH_BUFFER_SIZE
#define STM32_HASH_BUFFER_SIZE STM32_HASH_FIFO_SIZE
#endif


/* STM32 Hash Context */
typedef struct {
/* Context switching registers */
Expand All @@ -100,13 +80,11 @@ typedef struct {
uint32_t HASH_CSR[HASH_CR_SIZE];

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


Expand Down
1 change: 1 addition & 0 deletions wolfssl/wolfcrypt/tfm.h
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,7 @@ int fp_sqr_comba64(fp_int *a, fp_int *b);
#define MP_VAL FP_VAL /* invalid */
#define MP_MEM FP_MEM /* memory error */
#define MP_NOT_INF FP_NOT_INF /* point not at infinity */
#define MP_RANGE FP_NOT_INF
#define MP_OKAY FP_OKAY /* ok result */
#define MP_NO FP_NO /* yes/no result */
#define MP_YES FP_YES /* yes/no result */
Expand Down

0 comments on commit 034af8d

Please sign in to comment.