17
17
* limitations under the License.
18
18
*
19
19
*/
20
-
20
+ #if defined( MBEDTLS_MD5_C )
21
21
#include "mbedtls/md5.h"
22
22
23
23
#if defined(MBEDTLS_MD5_ALT )
@@ -28,6 +28,28 @@ 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 )
32
+ {
33
+ uint32_t i ;
34
+ /* allow multi-instance of HASH use: save context for HASH HW module CR */
35
+ HASH -> STR = ctx -> ctx_save_str ;
36
+ HASH -> CR = (ctx -> ctx_save_cr | HASH_CR_INIT );
37
+ for (i = 0 ;i < 38 ;i ++ ) {
38
+ HASH -> CSR [i ] = ctx -> ctx_save_csr [i ];
39
+ }
40
+ }
41
+
42
+ static void st_md5_save_hw_context (mbedtls_md5_context * ctx )
43
+ {
44
+ uint32_t i ;
45
+ /* allow multi-instance of HASH use: restore context for HASH HW module CR */
46
+ ctx -> ctx_save_cr = HASH -> CR ;
47
+ ctx -> ctx_save_str = HASH -> STR ;
48
+ for (i = 0 ;i < 38 ;i ++ ) {
49
+ ctx -> ctx_save_csr [i ] = HASH -> CSR [i ];
50
+ }
51
+ }
52
+
31
53
void mbedtls_md5_init ( mbedtls_md5_context * ctx )
32
54
{
33
55
mbedtls_zeroize ( ctx , sizeof ( mbedtls_md5_context ) );
@@ -41,13 +63,6 @@ void mbedtls_md5_free( mbedtls_md5_context *ctx )
41
63
{
42
64
if ( ctx == NULL )
43
65
return ;
44
-
45
- /* Force the HASH Periheral Clock Reset */
46
- __HAL_RCC_HASH_FORCE_RESET ();
47
-
48
- /* Release the HASH Periheral Clock Reset */
49
- __HAL_RCC_HASH_RELEASE_RESET ();
50
-
51
66
mbedtls_zeroize ( ctx , sizeof ( mbedtls_md5_context ) );
52
67
}
53
68
@@ -60,65 +75,84 @@ void mbedtls_md5_clone( mbedtls_md5_context *dst,
60
75
void mbedtls_md5_starts ( mbedtls_md5_context * ctx )
61
76
{
62
77
/* HASH IP initialization */
63
- HAL_HASH_DeInit (& ctx -> hhash_md5 );
78
+ if (HAL_HASH_DeInit (& ctx -> hhash_md5 ) != 0 ) {
79
+ // error found to be returned
80
+ return ;
81
+ }
64
82
65
83
/* HASH Configuration */
66
84
ctx -> hhash_md5 .Init .DataType = HASH_DATATYPE_8B ;
67
- if (HAL_HASH_Init (& ctx -> hhash_md5 ) == HAL_ERROR ) {
85
+ if (HAL_HASH_Init (& ctx -> hhash_md5 ) != 0 ) {
68
86
// return error code
69
87
return ;
70
88
}
89
+ st_md5_save_hw_context (ctx );
71
90
}
72
91
73
- void mbedtls_md5_process ( mbedtls_md5_context * ctx , const unsigned char data [MBEDTLS_MD5_BLOCK_SIZE ] )
92
+ void mbedtls_md5_process ( mbedtls_md5_context * ctx , const unsigned char data [ST_MD5_BLOCK_SIZE ] )
74
93
{
75
- HAL_HASH_MD5_Accumulate (& ctx -> hhash_md5 , (uint8_t * )data , MBEDTLS_MD5_BLOCK_SIZE );
94
+ st_md5_restore_hw_context (ctx );
95
+ if (HAL_HASH_MD5_Accumulate (& ctx -> hhash_md5 , (uint8_t * )data , ST_MD5_BLOCK_SIZE ) != 0 ) {
96
+ return ; // Return error code here
97
+ }
98
+ st_md5_save_hw_context (ctx );
76
99
}
77
100
78
101
void mbedtls_md5_update ( mbedtls_md5_context * ctx , const unsigned char * input , size_t ilen )
79
102
{
80
103
size_t currentlen = ilen ;
81
- // store mechanism to handle MBEDTLS_MD5_BLOCK_SIZE bytes per MBEDTLS_MD5_BLOCK_SIZE bytes
104
+ st_md5_restore_hw_context (ctx );
105
+
106
+ // store mechanism to accumulate ST_MD5_BLOCK_SIZE bytes (512 bits) in the HW
82
107
if (currentlen == 0 ){ // only change HW status is size if 0
83
- if (ctx -> hhash_md5 .Phase == HAL_HASH_PHASE_READY )
84
- {
108
+ if (ctx -> hhash_md5 .Phase == HAL_HASH_PHASE_READY ) {
85
109
/* Select the MD5 mode and reset the HASH processor core, so that the HASH will be ready to compute
86
110
the message digest of a new message */
87
111
HASH -> CR |= HASH_ALGOSELECTION_MD5 | HASH_CR_INIT ;
88
112
}
89
113
ctx -> hhash_md5 .Phase = HAL_HASH_PHASE_PROCESS ;
90
- } else if (currentlen < (MBEDTLS_MD5_BLOCK_SIZE - ctx -> sbuf_len )) {
114
+ } else if (currentlen < (ST_MD5_BLOCK_SIZE - ctx -> sbuf_len )) {
91
115
// only buffurize
92
116
memcpy (ctx -> sbuf + ctx -> sbuf_len , input , currentlen );
93
117
ctx -> sbuf_len += currentlen ;
94
118
} else {
95
119
// fill buffer and process it
96
- memcpy (ctx -> sbuf + ctx -> sbuf_len , input , (MBEDTLS_MD5_BLOCK_SIZE - ctx -> sbuf_len ));
97
- currentlen -= (MBEDTLS_MD5_BLOCK_SIZE - ctx -> sbuf_len );
120
+ memcpy (ctx -> sbuf + ctx -> sbuf_len , input , (ST_MD5_BLOCK_SIZE - ctx -> sbuf_len ));
121
+ currentlen -= (ST_MD5_BLOCK_SIZE - ctx -> sbuf_len );
98
122
mbedtls_md5_process (ctx , ctx -> sbuf );
99
- // now process every input as long as it is %4 bytes
100
- size_t iter = currentlen / 4 ;
101
- HAL_HASH_MD5_Accumulate (& ctx -> hhash_md5 , (uint8_t * )(input + MBEDTLS_MD5_BLOCK_SIZE - ctx -> sbuf_len ), (iter * 4 ));
102
- // sbuf is now fully accumulated, now copy 1 / 2 or 3 remaining bytes
103
- ctx -> sbuf_len = currentlen % 4 ;
123
+ // Process every input as long as it is %64 bytes, ie 512 bits
124
+ size_t iter = currentlen / ST_MD5_BLOCK_SIZE ;
125
+ if (iter != 0 ) {
126
+ if (HAL_HASH_MD5_Accumulate (& ctx -> hhash_md5 , (uint8_t * )(input + ST_MD5_BLOCK_SIZE - ctx -> sbuf_len ), (iter * ST_MD5_BLOCK_SIZE )) != 0 ) {
127
+ return ; // Return error code here
128
+ }
129
+ }
130
+ // sbuf is completely accumulated, now copy up to 63 remaining bytes
131
+ ctx -> sbuf_len = currentlen % ST_MD5_BLOCK_SIZE ;
104
132
if (ctx -> sbuf_len != 0 ) {
105
- memcpy (ctx -> sbuf , input + iter , ctx -> sbuf_len );
133
+ memcpy (ctx -> sbuf , input + ilen - ctx -> sbuf_len , ctx -> sbuf_len );
106
134
}
107
135
}
136
+ st_md5_save_hw_context (ctx );
108
137
}
109
138
110
139
void mbedtls_md5_finish ( mbedtls_md5_context * ctx , unsigned char output [16 ] )
111
140
{
141
+ st_md5_restore_hw_context (ctx );
112
142
if (ctx -> sbuf_len > 0 ) {
113
- HAL_HASH_MD5_Accumulate (& ctx -> hhash_md5 , ctx -> sbuf , ctx -> sbuf_len );
143
+ if (HAL_HASH_MD5_Accumulate (& ctx -> hhash_md5 , ctx -> sbuf , ctx -> sbuf_len ) != 0 ) {
144
+ return ; // Return error code here
145
+ }
114
146
}
115
- mbedtls_zeroize ( ctx -> sbuf , MBEDTLS_MD5_BLOCK_SIZE );
147
+ mbedtls_zeroize ( ctx -> sbuf , ST_MD5_BLOCK_SIZE );
116
148
ctx -> sbuf_len = 0 ;
117
149
__HAL_HASH_START_DIGEST ();
118
150
119
151
if (HAL_HASH_MD5_Finish (& ctx -> hhash_md5 , output , 10 )) {
120
152
// error code to be returned
121
153
}
154
+ st_md5_save_hw_context (ctx );
122
155
}
123
156
124
157
#endif /* MBEDTLS_MD5_ALT */
158
+ #endif /* MBEDTLS_MD5_C */
0 commit comments