@@ -27,26 +27,44 @@ static void mbedtls_zeroize( void *v, size_t n ) {
27
27
volatile unsigned char * p = v ; while ( n -- ) * p ++ = 0 ;
28
28
}
29
29
30
- static void st_sha256_restore_hw_context (mbedtls_sha256_context * ctx )
30
+ static int st_sha256_restore_hw_context (mbedtls_sha256_context * ctx )
31
31
{
32
32
uint32_t i ;
33
+ uint32_t tickstart ;
33
34
/* 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
+ }
34
42
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 );
36
44
for (i = 0 ;i < 38 ;i ++ ) {
37
45
HASH -> CSR [i ] = ctx -> ctx_save_csr [i ];
38
46
}
47
+ return 1 ;
39
48
}
40
49
41
- static void st_sha256_save_hw_context (mbedtls_sha256_context * ctx )
50
+ static int st_sha256_save_hw_context (mbedtls_sha256_context * ctx )
42
51
{
43
52
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
+ }
44
61
/* allow multi-instance of HASH use: restore context for HASH HW module CR */
45
62
ctx -> ctx_save_cr = HASH -> CR ;
46
63
ctx -> ctx_save_str = HASH -> STR ;
47
64
for (i = 0 ;i < 38 ;i ++ ) {
48
65
ctx -> ctx_save_csr [i ] = HASH -> CSR [i ];
49
66
}
67
+ return 1 ;
50
68
}
51
69
52
70
void mbedtls_sha256_init ( mbedtls_sha256_context * ctx )
@@ -85,12 +103,16 @@ void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 )
85
103
// error found to be returned
86
104
return ;
87
105
}
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
+ }
89
109
}
90
110
91
111
void mbedtls_sha256_process ( mbedtls_sha256_context * ctx , const unsigned char data [ST_SHA256_BLOCK_SIZE ] )
92
112
{
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
+ }
94
116
if (ctx -> is224 == 0 ) {
95
117
if (HAL_HASHEx_SHA256_Accumulate (& ctx -> hhash_sha256 , (uint8_t * ) data , ST_SHA256_BLOCK_SIZE ) != 0 ) {
96
118
return ; // Return error code
@@ -101,16 +123,20 @@ void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char da
101
123
}
102
124
}
103
125
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
+ }
105
129
}
106
130
107
131
void mbedtls_sha256_update ( mbedtls_sha256_context * ctx , const unsigned char * input , size_t ilen )
108
132
{
109
133
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
+ }
111
137
112
138
// 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
114
140
if (ctx -> hhash_sha256 .Phase == HAL_HASH_PHASE_READY ) {
115
141
/* Select the SHA256 or SHA224 mode and reset the HASH processor core, so that the HASH will be ready to compute
116
142
the message digest of a new message */
@@ -149,12 +175,16 @@ void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *in
149
175
memcpy (ctx -> sbuf , input + ilen - ctx -> sbuf_len , ctx -> sbuf_len );
150
176
}
151
177
}
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
+ }
153
181
}
154
182
155
183
void mbedtls_sha256_finish ( mbedtls_sha256_context * ctx , unsigned char output [32 ] )
156
184
{
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
+ }
158
188
if (ctx -> sbuf_len > 0 ) {
159
189
if (ctx -> is224 == 0 ) {
160
190
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
179
209
return ; // Return error code here
180
210
}
181
211
}
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
+ }
183
215
}
184
216
185
217
#endif /*MBEDTLS_SHA256_ALT*/
0 commit comments