Skip to content

Commit 074695a

Browse files
committed
Improve md5 buffer storing concept
1 parent d14c928 commit 074695a

File tree

1 file changed

+19
-31
lines changed

1 file changed

+19
-31
lines changed

features/mbedtls/targets/TARGET_STM/md5_alt.c

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ void mbedtls_md5_clone( mbedtls_md5_context *dst,
5757
*dst = *src;
5858
}
5959

60-
/*
61-
* MD5 context setup
62-
*/
6360
void mbedtls_md5_starts( mbedtls_md5_context *ctx )
6461
{
6562
/* HASH IP initialization */
@@ -78,47 +75,38 @@ void mbedtls_md5_process( mbedtls_md5_context *ctx, const unsigned char data[64]
7875
HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, (uint8_t *)data, 64);
7976
}
8077

81-
/*
82-
* MD5 process buffer
83-
*/
8478
void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, size_t ilen )
8579
{
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
9083
if(ctx->hhash_md5.Phase == HAL_HASH_PHASE_READY)
9184
{
9285
/* Select the MD5 mode and reset the HASH processor core, so that the HASH will be ready to compute
9386
the message digest of a new message */
9487
HASH->CR |= HASH_ALGOSELECTION_MD5 | HASH_CR_INIT;
9588
}
9689
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);
106106
}
107-
currentlen -= 64;
108-
i++;
109107
}
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-
117108
}
118109

119-
/*
120-
* MD5 final digest
121-
*/
122110
void mbedtls_md5_finish( mbedtls_md5_context *ctx, unsigned char output[16] )
123111
{
124112
if (ctx->sbuf_len > 0) {

0 commit comments

Comments
 (0)