@@ -57,9 +57,6 @@ void mbedtls_md5_clone( mbedtls_md5_context *dst,
57
57
* dst = * src ;
58
58
}
59
59
60
- /*
61
- * MD5 context setup
62
- */
63
60
void mbedtls_md5_starts ( mbedtls_md5_context * ctx )
64
61
{
65
62
/* HASH IP initialization */
@@ -78,47 +75,38 @@ void mbedtls_md5_process( mbedtls_md5_context *ctx, const unsigned char data[64]
78
75
HAL_HASH_MD5_Accumulate (& ctx -> hhash_md5 , (uint8_t * )data , 64 );
79
76
}
80
77
81
- /*
82
- * MD5 process buffer
83
- */
84
78
void mbedtls_md5_update ( mbedtls_md5_context * ctx , const unsigned char * input , size_t ilen )
85
79
{
86
- unsigned char i = 0 ;
87
- int currentlen = ilen ;
88
- /* store mechanism to handle 64 bytes per 64 bytes */
89
- if (currentlen == 0 ){ // change HW status is size if 0
80
+ size_t currentlen = ilen ;
81
+ // store mechanism to handle 64 bytes per 64 bytes
82
+ if (currentlen == 0 ){ // only change HW status is size if 0
90
83
if (ctx -> hhash_md5 .Phase == HAL_HASH_PHASE_READY )
91
84
{
92
85
/* Select the MD5 mode and reset the HASH processor core, so that the HASH will be ready to compute
93
86
the message digest of a new message */
94
87
HASH -> CR |= HASH_ALGOSELECTION_MD5 | HASH_CR_INIT ;
95
88
}
96
89
ctx -> hhash_md5 .Phase = HAL_HASH_PHASE_PROCESS ;
97
- }
98
- while ((currentlen + ctx -> sbuf_len ) >=64 ) {
99
- if (ctx -> sbuf_len == 0 ) { /* straight forward */
100
- mbedtls_md5_process (ctx , input + (i * 64 ));
101
- } else {
102
- memcpy (ctx -> sbuf + ctx -> sbuf_len , input + (i * 64 ),64 - ctx -> sbuf_len );
103
- mbedtls_md5_process (ctx , ctx -> sbuf );
104
- memcpy (ctx -> sbuf ,input + (i + 1 )* 64 - ctx -> sbuf_len , ctx -> sbuf_len );
105
- // ctx->sbuf_len remains the same
90
+ } else if (currentlen < (64 - ctx -> sbuf_len )) {
91
+ // only buffurize
92
+ memcpy (ctx -> sbuf + ctx -> sbuf_len , input , currentlen );
93
+ ctx -> sbuf_len += currentlen ;
94
+ } else {
95
+ // fill buffer and process it
96
+ memcpy (ctx -> sbuf + ctx -> sbuf_len , input , (64 - ctx -> sbuf_len ));
97
+ currentlen -= (64 - ctx -> sbuf_len );
98
+ 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 + 64 - 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 ;
104
+ if (ctx -> sbuf_len != 0 ) {
105
+ memcpy (ctx -> sbuf , input + iter , ctx -> sbuf_len );
106
106
}
107
- currentlen -= 64 ;
108
- i ++ ;
109
107
}
110
- if (currentlen < 0 ) {
111
- currentlen += 64 ;
112
- }
113
- /* Store the remaining <64 values */
114
- memcpy (ctx -> sbuf + ctx -> sbuf_len , input + (i * 64 ), currentlen );
115
- ctx -> sbuf_len += currentlen ;
116
-
117
108
}
118
109
119
- /*
120
- * MD5 final digest
121
- */
122
110
void mbedtls_md5_finish ( mbedtls_md5_context * ctx , unsigned char output [16 ] )
123
111
{
124
112
if (ctx -> sbuf_len > 0 ) {
0 commit comments