@@ -28,26 +28,44 @@ static void mbedtls_zeroize( void *v, size_t n ) {
28
28
volatile unsigned char * p = v ; while ( n -- ) * p ++ = 0 ;
29
29
}
30
30
31
- static void st_md5_restore_hw_context (mbedtls_md5_context * ctx )
31
+ static int st_md5_restore_hw_context (mbedtls_md5_context * ctx )
32
32
{
33
33
uint32_t i ;
34
+ uint32_t tickstart ;
34
35
/* 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
+ }
35
43
HASH -> STR = ctx -> ctx_save_str ;
36
44
HASH -> CR = (ctx -> ctx_save_cr | HASH_CR_INIT );
37
45
for (i = 0 ;i < 38 ;i ++ ) {
38
46
HASH -> CSR [i ] = ctx -> ctx_save_csr [i ];
39
47
}
48
+ return 1 ;
40
49
}
41
50
42
- static void st_md5_save_hw_context (mbedtls_md5_context * ctx )
51
+ static int st_md5_save_hw_context (mbedtls_md5_context * ctx )
43
52
{
44
53
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
+ }
45
62
/* allow multi-instance of HASH use: restore context for HASH HW module CR */
46
63
ctx -> ctx_save_cr = HASH -> CR ;
47
64
ctx -> ctx_save_str = HASH -> STR ;
48
65
for (i = 0 ;i < 38 ;i ++ ) {
49
66
ctx -> ctx_save_csr [i ] = HASH -> CSR [i ];
50
67
}
68
+ return 1 ;
51
69
}
52
70
53
71
void mbedtls_md5_init ( mbedtls_md5_context * ctx )
@@ -86,23 +104,30 @@ void mbedtls_md5_starts( mbedtls_md5_context *ctx )
86
104
// return error code
87
105
return ;
88
106
}
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
+ }
90
110
}
91
111
92
112
void mbedtls_md5_process ( mbedtls_md5_context * ctx , const unsigned char data [ST_MD5_BLOCK_SIZE ] )
93
113
{
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
+ }
95
117
if (HAL_HASH_MD5_Accumulate (& ctx -> hhash_md5 , (uint8_t * )data , ST_MD5_BLOCK_SIZE ) != 0 ) {
96
118
return ; // Return error code here
97
119
}
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
+ }
99
123
}
100
124
101
125
void mbedtls_md5_update ( mbedtls_md5_context * ctx , const unsigned char * input , size_t ilen )
102
126
{
103
127
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
+ }
106
131
// store mechanism to accumulate ST_MD5_BLOCK_SIZE bytes (512 bits) in the HW
107
132
if (currentlen == 0 ){ // only change HW status is size if 0
108
133
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
133
158
memcpy (ctx -> sbuf , input + ilen - ctx -> sbuf_len , ctx -> sbuf_len );
134
159
}
135
160
}
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
+ }
137
164
}
138
165
139
166
void mbedtls_md5_finish ( mbedtls_md5_context * ctx , unsigned char output [16 ] )
140
167
{
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
+ }
142
171
if (ctx -> sbuf_len > 0 ) {
143
172
if (HAL_HASH_MD5_Accumulate (& ctx -> hhash_md5 , ctx -> sbuf , ctx -> sbuf_len ) != 0 ) {
144
173
return ; // Return error code here
@@ -151,7 +180,9 @@ void mbedtls_md5_finish( mbedtls_md5_context *ctx, unsigned char output[16] )
151
180
if (HAL_HASH_MD5_Finish (& ctx -> hhash_md5 , output , 10 )) {
152
181
// error code to be returned
153
182
}
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
+ }
155
186
}
156
187
157
188
#endif /* MBEDTLS_MD5_ALT */
0 commit comments