@@ -26,26 +26,44 @@ static void mbedtls_zeroize( void *v, size_t n ) {
26
26
volatile unsigned char * p = (unsigned char * )v ; while ( n -- ) * p ++ = 0 ;
27
27
}
28
28
29
- static void st_sha1_restore_hw_context (mbedtls_sha1_context * ctx )
29
+ static int st_sha1_restore_hw_context (mbedtls_sha1_context * ctx )
30
30
{
31
31
uint32_t i ;
32
+ uint32_t tickstart ;
32
33
/* 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
+ }
33
41
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 );
35
43
for (i = 0 ;i < 38 ;i ++ ) {
36
44
HASH -> CSR [i ] = ctx -> ctx_save_csr [i ];
37
45
}
46
+ return 1 ;
38
47
}
39
48
40
- static void st_sha1_save_hw_context (mbedtls_sha1_context * ctx )
49
+ static int st_sha1_save_hw_context (mbedtls_sha1_context * ctx )
41
50
{
42
51
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
+ }
43
60
/* allow multi-instance of HASH use: restore context for HASH HW module CR */
44
61
ctx -> ctx_save_cr = HASH -> CR ;
45
62
ctx -> ctx_save_str = HASH -> STR ;
46
63
for (i = 0 ;i < 38 ;i ++ ) {
47
64
ctx -> ctx_save_csr [i ] = HASH -> CSR [i ];
48
65
}
66
+ return 1 ;
49
67
}
50
68
51
69
void mbedtls_sha1_init ( mbedtls_sha1_context * ctx )
@@ -84,23 +102,31 @@ void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
84
102
// error found to be returned
85
103
return ;
86
104
}
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
+ }
88
108
}
89
109
90
110
void mbedtls_sha1_process ( mbedtls_sha1_context * ctx , const unsigned char data [ST_SHA1_BLOCK_SIZE ] )
91
111
{
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
+ }
93
115
if (HAL_HASH_SHA1_Accumulate (& ctx -> hhash_sha1 , (uint8_t * ) data , ST_SHA1_BLOCK_SIZE ) != 0 ) {
94
116
return ; // Return error code
95
117
}
96
118
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
+ }
98
122
}
99
123
100
124
void mbedtls_sha1_update ( mbedtls_sha1_context * ctx , const unsigned char * input , size_t ilen )
101
125
{
102
126
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
+ }
104
130
105
131
// store mechanism to accumulate ST_SHA1_BLOCK_SIZE bytes (512 bits) in the HW
106
132
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,
130
156
memcpy (ctx -> sbuf , input + ilen - ctx -> sbuf_len , ctx -> sbuf_len );
131
157
}
132
158
}
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
+ }
134
162
}
135
163
136
164
void mbedtls_sha1_finish ( mbedtls_sha1_context * ctx , unsigned char output [20 ] )
137
165
{
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
+ }
139
169
140
170
if (ctx -> sbuf_len > 0 ) {
141
171
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] )
149
179
if (HAL_HASH_SHA1_Finish (& ctx -> hhash_sha1 , output , 10 ) != 0 ){
150
180
return ; // error code to be returned
151
181
}
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
+ }
153
185
}
154
186
155
187
#endif /*MBEDTLS_SHA1_ALT*/
0 commit comments