Skip to content

Commit a27498c

Browse files
committed
Check HASH is not busy before save/resteore context
1 parent 8ea9ca0 commit a27498c

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

features/mbedtls/targets/TARGET_STM/sha256_alt.c

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,44 @@ static void mbedtls_zeroize( void *v, size_t n ) {
2727
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
2828
}
2929

30-
static void st_sha256_restore_hw_context(mbedtls_sha256_context *ctx)
30+
static int st_sha256_restore_hw_context(mbedtls_sha256_context *ctx)
3131
{
3232
uint32_t i;
33+
uint32_t tickstart;
3334
/* allow multi-instance of HASH use: save context for HASH HW module CR */
35+
/* Check that there is no HASH activity on going */
36+
tickstart = HAL_GetTick();
37+
while ((HASH->SR & (HASH_FLAG_BUSY | HASH_FLAG_DMAS)) != 0) {
38+
if ((HAL_GetTick() - tickstart) > ST_SHA256_TIMEOUT) {
39+
return 0; // timeout: HASH processor is busy
40+
}
41+
}
3442
HASH->STR = ctx->ctx_save_str;
35-
HASH->CR = (ctx->ctx_save_cr|HASH_CR_INIT);
43+
HASH->CR = (ctx->ctx_save_cr | HASH_CR_INIT);
3644
for (i=0;i<38;i++) {
3745
HASH->CSR[i] = ctx->ctx_save_csr[i];
3846
}
47+
return 1;
3948
}
4049

41-
static void st_sha256_save_hw_context(mbedtls_sha256_context *ctx)
50+
static int st_sha256_save_hw_context(mbedtls_sha256_context *ctx)
4251
{
4352
uint32_t i;
53+
uint32_t tickstart;
54+
/* Check that there is no HASH activity on going */
55+
tickstart = HAL_GetTick();
56+
while ((HASH->SR & (HASH_FLAG_BUSY | HASH_FLAG_DMAS)) != 0) {
57+
if ((HAL_GetTick() - tickstart) > ST_SHA256_TIMEOUT) {
58+
return 0; // timeout: HASH processor is busy
59+
}
60+
}
4461
/* allow multi-instance of HASH use: restore context for HASH HW module CR */
4562
ctx->ctx_save_cr = HASH->CR;
4663
ctx->ctx_save_str = HASH->STR;
4764
for (i=0;i<38;i++) {
4865
ctx->ctx_save_csr[i] = HASH->CSR[i];
4966
}
67+
return 1;
5068
}
5169

5270
void mbedtls_sha256_init( mbedtls_sha256_context *ctx )
@@ -85,12 +103,16 @@ void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 )
85103
// error found to be returned
86104
return;
87105
}
88-
st_sha256_save_hw_context(ctx);
106+
if (st_sha256_save_hw_context(ctx) != 1) {
107+
return; // return HASH_BUSY timeout Error here
108+
}
89109
}
90110

91111
void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[ST_SHA256_BLOCK_SIZE] )
92112
{
93-
st_sha256_restore_hw_context(ctx);
113+
if (st_sha256_restore_hw_context(ctx) != 1) {
114+
return; // Return HASH_BUSY timout error here
115+
}
94116
if (ctx->is224 == 0) {
95117
if (HAL_HASHEx_SHA256_Accumulate(&ctx->hhash_sha256, (uint8_t *) data, ST_SHA256_BLOCK_SIZE) != 0) {
96118
return; // Return error code
@@ -101,16 +123,20 @@ void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char da
101123
}
102124
}
103125

104-
st_sha256_save_hw_context(ctx);
126+
if (st_sha256_save_hw_context(ctx) != 1) {
127+
return; // return HASH_BUSY timeout Error here
128+
}
105129
}
106130

107131
void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *input, size_t ilen )
108132
{
109133
size_t currentlen = ilen;
110-
st_sha256_restore_hw_context(ctx);
134+
if (st_sha256_restore_hw_context(ctx) != 1) {
135+
return; // Return HASH_BUSY timout error here
136+
}
111137

112138
// store mechanism to accumulate ST_SHA256_BLOCK_SIZE bytes (512 bits) in the HW
113-
if (currentlen == 0){ // only change HW status is size if 0
139+
if (currentlen == 0) { // only change HW status is size if 0
114140
if(ctx->hhash_sha256.Phase == HAL_HASH_PHASE_READY) {
115141
/* Select the SHA256 or SHA224 mode and reset the HASH processor core, so that the HASH will be ready to compute
116142
the message digest of a new message */
@@ -149,12 +175,16 @@ void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *in
149175
memcpy(ctx->sbuf, input + ilen - ctx->sbuf_len, ctx->sbuf_len);
150176
}
151177
}
152-
st_sha256_save_hw_context(ctx);
178+
if (st_sha256_save_hw_context(ctx) != 1) {
179+
return; // return HASH_BUSY timeout Error here
180+
}
153181
}
154182

155183
void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char output[32] )
156184
{
157-
st_sha256_restore_hw_context(ctx);
185+
if (st_sha256_restore_hw_context(ctx) != 1) {
186+
return; // Return HASH_BUSY timout error here
187+
}
158188
if (ctx->sbuf_len > 0) {
159189
if (ctx->is224 == 0) {
160190
if (HAL_HASHEx_SHA256_Accumulate(&ctx->hhash_sha256, ctx->sbuf, ctx->sbuf_len) != 0) {
@@ -179,7 +209,9 @@ void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char output[32
179209
return; // Return error code here
180210
}
181211
}
182-
st_sha256_save_hw_context(ctx);
212+
if (st_sha256_save_hw_context(ctx) != 1) {
213+
return; // return HASH_BUSY timeout Error here
214+
}
183215
}
184216

185217
#endif /*MBEDTLS_SHA256_ALT*/

features/mbedtls/targets/TARGET_STM/sha256_alt.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
extern "C" {
3232
#endif
3333

34-
#define ST_SHA256_BLOCK_SIZE ((size_t)(64)) // HW handles 512 bits, ie 64 bytes
34+
#define ST_SHA256_BLOCK_SIZE ((size_t) 64) // HW handles 512 bits, ie 64 bytes
35+
#define ST_SHA256_TIMEOUT ((uint32_t) 3)
3536
/**
3637
* \brief SHA-256 context structure
3738
* \note HAL_HASH_SHA256_Accumulate will accumulate 512 bits packets, unless it is the last call to the function

0 commit comments

Comments
 (0)