Skip to content

Commit 88c3b3e

Browse files
committed
Remove last code redundancy
1 parent 53027fd commit 88c3b3e

File tree

1 file changed

+30
-32
lines changed

1 file changed

+30
-32
lines changed

features/mbedtls/targets/TARGET_STM/md5_alt.c

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -127,42 +127,40 @@ void mbedtls_md5_process( mbedtls_md5_context *ctx, const unsigned char data[ST_
127127
void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, size_t ilen )
128128
{
129129
size_t currentlen = ilen;
130-
if (st_md5_restore_hw_context(ctx) != 1) {
131-
return; // Return HASH_BUSY timout error here
132-
}
133-
// store mechanism to accumulate ST_MD5_BLOCK_SIZE bytes (512 bits) in the HW
134-
if (currentlen == 0){ // only change HW status is size if 0
135-
if(ctx->hhash_md5.Phase == HAL_HASH_PHASE_READY) {
136-
/* Select the MD5 mode and reset the HASH processor core, so that the HASH will be ready to compute
137-
the message digest of a new message */
138-
HASH->CR |= HASH_ALGOSELECTION_MD5 | HASH_CR_INIT;
130+
/* If ilen = 0 : do nothing */
131+
if (currentlen != 0) {
132+
if (st_md5_restore_hw_context(ctx) != 1) {
133+
return; // Return HASH_BUSY timout error here
139134
}
140-
ctx->hhash_md5.Phase = HAL_HASH_PHASE_PROCESS;
141-
} else if (currentlen < (ST_MD5_BLOCK_SIZE - ctx->sbuf_len)) {
142-
// only buffurize
143-
memcpy(ctx->sbuf+ctx->sbuf_len, input, currentlen);
144-
ctx->sbuf_len += currentlen;
145-
} else {
146-
// fill buffer and process it
147-
memcpy(ctx->sbuf + ctx->sbuf_len, input, (ST_MD5_BLOCK_SIZE - ctx->sbuf_len));
148-
currentlen -= (ST_MD5_BLOCK_SIZE - ctx->sbuf_len);
149-
mbedtls_md5_process(ctx, ctx->sbuf);
150-
// Process every input as long as it is %64 bytes, ie 512 bits
151-
size_t iter = currentlen / ST_MD5_BLOCK_SIZE;
152-
if (iter !=0) {
153-
if (HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, (uint8_t *)(input + ST_MD5_BLOCK_SIZE - ctx->sbuf_len), (iter * ST_MD5_BLOCK_SIZE)) != 0) {
154-
return; // Return error code here
135+
136+
// store mechanism to accumulate ST_MD5_BLOCK_SIZE bytes (512 bits) in the HW
137+
if (currentlen < (ST_MD5_BLOCK_SIZE - ctx->sbuf_len)) {
138+
// only buffurize
139+
memcpy(ctx->sbuf+ctx->sbuf_len, input, currentlen);
140+
ctx->sbuf_len += currentlen;
141+
} else {
142+
// fill buffer and process it
143+
memcpy(ctx->sbuf + ctx->sbuf_len, input, (ST_MD5_BLOCK_SIZE - ctx->sbuf_len));
144+
currentlen -= (ST_MD5_BLOCK_SIZE - ctx->sbuf_len);
145+
mbedtls_md5_process(ctx, ctx->sbuf);
146+
// Process every input as long as it is %64 bytes, ie 512 bits
147+
size_t iter = currentlen / ST_MD5_BLOCK_SIZE;
148+
if (iter !=0) {
149+
if (HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, (uint8_t *)(input + ST_MD5_BLOCK_SIZE - ctx->sbuf_len), (iter * ST_MD5_BLOCK_SIZE)) != 0) {
150+
return; // Return error code here
151+
}
152+
}
153+
// sbuf is completely accumulated, now copy up to 63 remaining bytes
154+
ctx->sbuf_len = currentlen % ST_MD5_BLOCK_SIZE;
155+
if (ctx->sbuf_len !=0) {
156+
memcpy(ctx->sbuf, input + ilen - ctx->sbuf_len, ctx->sbuf_len);
155157
}
156158
}
157-
// sbuf is completely accumulated, now copy up to 63 remaining bytes
158-
ctx->sbuf_len = currentlen % ST_MD5_BLOCK_SIZE;
159-
if (ctx->sbuf_len !=0) {
160-
memcpy(ctx->sbuf, input + ilen - ctx->sbuf_len, ctx->sbuf_len);
159+
160+
if (st_md5_save_hw_context(ctx) != 1) {
161+
return; // return HASH_BUSY timeout Error here
161162
}
162163
}
163-
if (st_md5_save_hw_context(ctx) != 1) {
164-
return; // return HASH_BUSY timeout Error here
165-
}
166164
}
167165

168166
void mbedtls_md5_finish( mbedtls_md5_context *ctx, unsigned char output[16] )
@@ -171,7 +169,7 @@ void mbedtls_md5_finish( mbedtls_md5_context *ctx, unsigned char output[16] )
171169
return; // Return HASH_BUSY timout error here
172170
}
173171
/* Last accumulation for extra bytes in sbuf_len */
174-
/* This allows the HW flags to be in place in case mbedtls_sha256_update has not been called yet */
172+
/* This sets HW flags in case mbedtls_md5_update has not been called yet */
175173
if (HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, ctx->sbuf, ctx->sbuf_len) != 0) {
176174
return; // Return error code here
177175
}

0 commit comments

Comments
 (0)