Skip to content

Commit e63912f

Browse files
committed
Check that the HASH is not busy before save and restore iHW registers
1 parent 842791b commit e63912f

File tree

2 files changed

+44
-11
lines changed

2 files changed

+44
-11
lines changed

features/mbedtls/targets/TARGET_STM/sha1_alt.c

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

29-
static void st_sha1_restore_hw_context(mbedtls_sha1_context *ctx)
29+
static int st_sha1_restore_hw_context(mbedtls_sha1_context *ctx)
3030
{
3131
uint32_t i;
32+
uint32_t tickstart;
3233
/* allow multi-instance of HASH use: save context for HASH HW module CR */
34+
/* Check that there is no HASH activity on going */
35+
tickstart = HAL_GetTick();
36+
while ((HASH->SR & (HASH_FLAG_BUSY | HASH_FLAG_DMAS)) != 0) {
37+
if ((HAL_GetTick() - tickstart) > ST_SHA1_TIMEOUT) {
38+
return 0; // timeout: HASH processor is busy
39+
}
40+
}
3341
HASH->STR = ctx->ctx_save_str;
34-
HASH->CR = (ctx->ctx_save_cr|HASH_CR_INIT);
42+
HASH->CR = (ctx->ctx_save_cr | HASH_CR_INIT);
3543
for (i=0;i<38;i++) {
3644
HASH->CSR[i] = ctx->ctx_save_csr[i];
3745
}
46+
return 1;
3847
}
3948

40-
static void st_sha1_save_hw_context(mbedtls_sha1_context *ctx)
49+
static int st_sha1_save_hw_context(mbedtls_sha1_context *ctx)
4150
{
4251
uint32_t i;
52+
uint32_t tickstart;
53+
/* Check that there is no HASH activity on going */
54+
tickstart = HAL_GetTick();
55+
while ((HASH->SR & (HASH_FLAG_BUSY | HASH_FLAG_DMAS)) != 0) {
56+
if ((HAL_GetTick() - tickstart) > ST_SHA1_TIMEOUT) {
57+
return 0; // timeout: HASH processor is busy
58+
}
59+
}
4360
/* allow multi-instance of HASH use: restore context for HASH HW module CR */
4461
ctx->ctx_save_cr = HASH->CR;
4562
ctx->ctx_save_str = HASH->STR;
4663
for (i=0;i<38;i++) {
4764
ctx->ctx_save_csr[i] = HASH->CSR[i];
4865
}
66+
return 1;
4967
}
5068

5169
void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
@@ -84,23 +102,31 @@ void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
84102
// error found to be returned
85103
return;
86104
}
87-
st_sha1_save_hw_context(ctx);
105+
if (st_sha1_save_hw_context(ctx) != 1) {
106+
return; // return HASH_BUSY timeout Error here
107+
}
88108
}
89109

90110
void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[ST_SHA1_BLOCK_SIZE] )
91111
{
92-
st_sha1_restore_hw_context(ctx);
112+
if (st_sha1_restore_hw_context(ctx) != 1) {
113+
return; // Return HASH_BUSY timout error here
114+
}
93115
if (HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, (uint8_t *) data, ST_SHA1_BLOCK_SIZE) != 0) {
94116
return; // Return error code
95117
}
96118

97-
st_sha1_save_hw_context(ctx);
119+
if (st_sha1_save_hw_context(ctx) != 1) {
120+
return; // return HASH_BUSY timeout Error here
121+
}
98122
}
99123

100124
void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen )
101125
{
102126
size_t currentlen = ilen;
103-
st_sha1_restore_hw_context(ctx);
127+
if (st_sha1_restore_hw_context(ctx) != 1) {
128+
return; // Return HASH_BUSY timout error here
129+
}
104130

105131
// store mechanism to accumulate ST_SHA1_BLOCK_SIZE bytes (512 bits) in the HW
106132
if (currentlen == 0){ // only change HW status is size if 0
@@ -130,12 +156,16 @@ void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input,
130156
memcpy(ctx->sbuf, input + ilen - ctx->sbuf_len, ctx->sbuf_len);
131157
}
132158
}
133-
st_sha1_save_hw_context(ctx);
159+
if (st_sha1_save_hw_context(ctx) != 1) {
160+
return; // return HASH_BUSY timeout Error here
161+
}
134162
}
135163

136164
void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] )
137165
{
138-
st_sha1_restore_hw_context(ctx);
166+
if (st_sha1_restore_hw_context(ctx) != 1) {
167+
return; // Return HASH_BUSY timout error here
168+
}
139169

140170
if (ctx->sbuf_len > 0) {
141171
if (HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, ctx->sbuf, ctx->sbuf_len) != 0) {
@@ -149,7 +179,9 @@ void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] )
149179
if (HAL_HASH_SHA1_Finish(&ctx->hhash_sha1, output, 10) != 0){
150180
return; // error code to be returned
151181
}
152-
st_sha1_save_hw_context(ctx);
182+
if (st_sha1_save_hw_context(ctx) != 1) {
183+
return; // return HASH_BUSY timeout Error here
184+
}
153185
}
154186

155187
#endif /*MBEDTLS_SHA1_ALT*/

features/mbedtls/targets/TARGET_STM/sha1_alt.h

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

35-
#define ST_SHA1_BLOCK_SIZE (64) // HW handles 512 bits, ie 64 bytes
35+
#define ST_SHA1_BLOCK_SIZE ((size_t) 64) // HW handles 512 bits, ie 64 bytes
36+
#define ST_SHA1_TIMEOUT ((uint32_t) 3)
3637
/**
3738
* \brief SHA-1 context structure
3839
* \note HAL_HASH_SHA1_Accumulate will accumulate 512 bits packets, unless it is the last call to the function

0 commit comments

Comments
 (0)