forked from wolfSSL/wolfssl
Fix to resolve STM32 hash FIFO. Simplify logic for ensuring FIFO gets filled before doing a save/restore. ZD 18294
This commit is contained in:
@ -303,12 +303,11 @@ int wc_Stm32_Hash_Update(STM32_HASH_Context* stmCtx, word32 algo,
|
|||||||
int ret = 0;
|
int ret = 0;
|
||||||
byte* local = (byte*)stmCtx->buffer;
|
byte* local = (byte*)stmCtx->buffer;
|
||||||
int wroteToFifo = 0;
|
int wroteToFifo = 0;
|
||||||
const word32 fifoSz = (STM32_HASH_FIFO_SIZE * STM32_HASH_REG_SIZE);
|
|
||||||
word32 chunkSz;
|
word32 chunkSz;
|
||||||
|
|
||||||
#ifdef DEBUG_STM32_HASH
|
#ifdef DEBUG_STM32_HASH
|
||||||
printf("STM Hash Update: algo %x, len %d, blockSz %d\n",
|
printf("STM Hash Update: algo %x, len %d, buffLen %d, fifoBytes %d\n",
|
||||||
algo, len, blockSize);
|
algo, len, stmCtx->buffLen, stmCtx->fifoBytes);
|
||||||
#endif
|
#endif
|
||||||
(void)blockSize;
|
(void)blockSize;
|
||||||
|
|
||||||
@ -323,40 +322,27 @@ int wc_Stm32_Hash_Update(STM32_HASH_Context* stmCtx, word32 algo,
|
|||||||
/* restore hash context or init as new hash */
|
/* restore hash context or init as new hash */
|
||||||
wc_Stm32_Hash_RestoreContext(stmCtx, algo);
|
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 */
|
/* write blocks to FIFO */
|
||||||
while (len) {
|
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);
|
XMEMCPY(&local[stmCtx->buffLen], data, add);
|
||||||
|
|
||||||
stmCtx->buffLen += add;
|
stmCtx->buffLen += add;
|
||||||
data += add;
|
data += add;
|
||||||
len -= 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) {
|
if (stmCtx->buffLen == chunkSz) {
|
||||||
wc_Stm32_Hash_Data(stmCtx, stmCtx->buffLen);
|
wc_Stm32_Hash_Data(stmCtx, stmCtx->buffLen);
|
||||||
wroteToFifo = 1;
|
wroteToFifo = 1;
|
||||||
#ifdef STM32_HASH_FIFO_WORKAROUND
|
stmCtx->fifoBytes += chunkSz;
|
||||||
if (chunkSz > fifoSz)
|
|
||||||
stmCtx->fifoBytes = chunkSz - fifoSz;
|
|
||||||
chunkSz = fifoSz;
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -380,7 +366,8 @@ int wc_Stm32_Hash_Final(STM32_HASH_Context* stmCtx, word32 algo,
|
|||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
#ifdef DEBUG_STM32_HASH
|
#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
|
#endif
|
||||||
|
|
||||||
/* turn on hash clock */
|
/* turn on hash clock */
|
||||||
|
@ -2496,7 +2496,7 @@ int wc_Sha256GetHash(wc_Sha256* sha256, byte* hash)
|
|||||||
ret = wc_Sha256Copy(sha256, tmpSha256);
|
ret = wc_Sha256Copy(sha256, tmpSha256);
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
ret = wc_Sha256Final(tmpSha256, hash);
|
ret = wc_Sha256Final(tmpSha256, hash);
|
||||||
wc_Sha256Free(tmpSha256); /* TODO move outside brackets? */
|
wc_Sha256Free(tmpSha256);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -71,26 +71,6 @@
|
|||||||
#define STM32_HASH_REG_SIZE 4
|
#define STM32_HASH_REG_SIZE 4
|
||||||
#define STM32_HASH_FIFO_SIZE 16 /* FIFO is 16 deep 32-bits wide */
|
#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 */
|
/* STM32 Hash Context */
|
||||||
typedef struct {
|
typedef struct {
|
||||||
/* Context switching registers */
|
/* Context switching registers */
|
||||||
@ -100,13 +80,11 @@ typedef struct {
|
|||||||
uint32_t HASH_CSR[HASH_CR_SIZE];
|
uint32_t HASH_CSR[HASH_CR_SIZE];
|
||||||
|
|
||||||
/* Hash state / buffers */
|
/* 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 buffLen; /* partial word remain */
|
||||||
word32 loLen; /* total update bytes
|
word32 loLen; /* total update bytes
|
||||||
(only lsb 6-bits is used for nbr valid bytes in last word) */
|
(only lsb 6-bits is used for nbr valid bytes in last word) */
|
||||||
#ifdef STM32_HASH_FIFO_WORKAROUND
|
word32 fifoBytes; /* number of currently filled FIFO bytes */
|
||||||
int fifoBytes; /* number of currently filled FIFO bytes */
|
|
||||||
#endif
|
|
||||||
} STM32_HASH_Context;
|
} STM32_HASH_Context;
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user