Skip to content

Commit 6cd569d

Browse files
committed
Add a check 'non busy' status of the HW before save restore procedures
1 parent 9906e6a commit 6cd569d

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

features/mbedtls/targets/TARGET_STM/md5_alt.c

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

31-
static void st_md5_restore_hw_context(mbedtls_md5_context *ctx)
31+
static int st_md5_restore_hw_context(mbedtls_md5_context *ctx)
3232
{
3333
uint32_t i;
34+
uint32_t tickstart;
3435
/* allow multi-instance of HASH use: save context for HASH HW module CR */
36+
/* Check that there is no HASH activity on going */
37+
tickstart = HAL_GetTick();
38+
while ((HASH->SR & (HASH_FLAG_BUSY | HASH_FLAG_DMAS)) != 0) {
39+
if ((HAL_GetTick() - tickstart) > ST_MD5_TIMEOUT) {
40+
return 0; // timeout: HASH processor is busy
41+
}
42+
}
3543
HASH->STR = ctx->ctx_save_str;
3644
HASH->CR = (ctx->ctx_save_cr | HASH_CR_INIT);
3745
for (i=0;i<38;i++) {
3846
HASH->CSR[i] = ctx->ctx_save_csr[i];
3947
}
48+
return 1;
4049
}
4150

42-
static void st_md5_save_hw_context(mbedtls_md5_context *ctx)
51+
static int st_md5_save_hw_context(mbedtls_md5_context *ctx)
4352
{
4453
uint32_t i;
54+
uint32_t tickstart;
55+
/* Check that there is no HASH activity on going */
56+
tickstart = HAL_GetTick();
57+
while ((HASH->SR & (HASH_FLAG_BUSY | HASH_FLAG_DMAS)) != 0) {
58+
if ((HAL_GetTick() - tickstart) > ST_MD5_TIMEOUT) {
59+
return 0; // timeout: HASH processor is busy
60+
}
61+
}
4562
/* allow multi-instance of HASH use: restore context for HASH HW module CR */
4663
ctx->ctx_save_cr = HASH->CR;
4764
ctx->ctx_save_str = HASH->STR;
4865
for (i=0;i<38;i++) {
4966
ctx->ctx_save_csr[i] = HASH->CSR[i];
5067
}
68+
return 1;
5169
}
5270

5371
void mbedtls_md5_init( mbedtls_md5_context *ctx )
@@ -86,23 +104,30 @@ void mbedtls_md5_starts( mbedtls_md5_context *ctx )
86104
// return error code
87105
return;
88106
}
89-
st_md5_save_hw_context(ctx);
107+
if (st_md5_save_hw_context(ctx) != 1) {
108+
return; // return HASH_BUSY timeout Error here
109+
}
90110
}
91111

92112
void mbedtls_md5_process( mbedtls_md5_context *ctx, const unsigned char data[ST_MD5_BLOCK_SIZE] )
93113
{
94-
st_md5_restore_hw_context(ctx);
114+
if (st_md5_restore_hw_context(ctx) != 1) {
115+
return; // Return HASH_BUSY timout error here
116+
}
95117
if (HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, (uint8_t *)data, ST_MD5_BLOCK_SIZE) != 0) {
96118
return; // Return error code here
97119
}
98-
st_md5_save_hw_context(ctx);
120+
if (st_md5_save_hw_context(ctx) != 1) {
121+
return; // return HASH_BUSY timeout Error here
122+
}
99123
}
100124

101125
void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, size_t ilen )
102126
{
103127
size_t currentlen = ilen;
104-
st_md5_restore_hw_context(ctx);
105-
128+
if (st_md5_restore_hw_context(ctx) != 1) {
129+
return; // Return HASH_BUSY timout error here
130+
}
106131
// store mechanism to accumulate ST_MD5_BLOCK_SIZE bytes (512 bits) in the HW
107132
if (currentlen == 0){ // only change HW status is size if 0
108133
if(ctx->hhash_md5.Phase == HAL_HASH_PHASE_READY) {
@@ -133,12 +158,16 @@ void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, s
133158
memcpy(ctx->sbuf, input + ilen - ctx->sbuf_len, ctx->sbuf_len);
134159
}
135160
}
136-
st_md5_save_hw_context(ctx);
161+
if (st_md5_save_hw_context(ctx) != 1) {
162+
return; // return HASH_BUSY timeout Error here
163+
}
137164
}
138165

139166
void mbedtls_md5_finish( mbedtls_md5_context *ctx, unsigned char output[16] )
140167
{
141-
st_md5_restore_hw_context(ctx);
168+
if (st_md5_restore_hw_context(ctx) != 1) {
169+
return; // Return HASH_BUSY timout error here
170+
}
142171
if (ctx->sbuf_len > 0) {
143172
if (HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, ctx->sbuf, ctx->sbuf_len) != 0) {
144173
return; // Return error code here
@@ -151,7 +180,9 @@ void mbedtls_md5_finish( mbedtls_md5_context *ctx, unsigned char output[16] )
151180
if (HAL_HASH_MD5_Finish(&ctx->hhash_md5, output, 10)) {
152181
// error code to be returned
153182
}
154-
st_md5_save_hw_context(ctx);
183+
if (st_md5_save_hw_context(ctx) != 1) {
184+
return; // return HASH_BUSY timeout Error here
185+
}
155186
}
156187

157188
#endif /* MBEDTLS_MD5_ALT */

features/mbedtls/targets/TARGET_STM/md5_alt.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ extern "C" {
3232
#endif
3333

3434
#define ST_MD5_BLOCK_SIZE ((size_t)(64)) // HW handles 512 bits, ie 64 bytes
35+
#define ST_MD5_TIMEOUT ((uint32_t) 3)
36+
3537
/**
3638
* \brief MD5 context structure
3739
* \note HAL_HASH_MD5_Accumulate will accumulate 512 bits packets, unless it is the last call to the function

0 commit comments

Comments
 (0)