diff --git a/features/mbedtls/targets/TARGET_STM/md5_alt.c b/features/mbedtls/targets/TARGET_STM/md5_alt.c index 9b4a701ee86..1d3918e9571 100644 --- a/features/mbedtls/targets/TARGET_STM/md5_alt.c +++ b/features/mbedtls/targets/TARGET_STM/md5_alt.c @@ -70,15 +70,15 @@ void mbedtls_md5_starts( mbedtls_md5_context *ctx ) } } -void mbedtls_md5_process( mbedtls_md5_context *ctx, const unsigned char data[64] ) +void mbedtls_md5_process( mbedtls_md5_context *ctx, const unsigned char data[MBEDTLS_MD5_BLOCK_SIZE] ) { - HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, (uint8_t *)data, 64); + HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, (uint8_t *)data, MBEDTLS_MD5_BLOCK_SIZE); } void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, size_t ilen ) { size_t currentlen = ilen; - // store mechanism to handle 64 bytes per 64 bytes + // store mechanism to handle MBEDTLS_MD5_BLOCK_SIZE bytes per MBEDTLS_MD5_BLOCK_SIZE bytes if (currentlen == 0){ // only change HW status is size if 0 if(ctx->hhash_md5.Phase == HAL_HASH_PHASE_READY) { @@ -87,18 +87,18 @@ void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, s HASH->CR |= HASH_ALGOSELECTION_MD5 | HASH_CR_INIT; } ctx->hhash_md5.Phase = HAL_HASH_PHASE_PROCESS; - } else if (currentlen < (64-ctx->sbuf_len)) { + } else if (currentlen < (MBEDTLS_MD5_BLOCK_SIZE-ctx->sbuf_len)) { // only buffurize memcpy(ctx->sbuf+ctx->sbuf_len, input, currentlen); ctx->sbuf_len += currentlen; } else { // fill buffer and process it - memcpy(ctx->sbuf + ctx->sbuf_len, input, (64-ctx->sbuf_len)); - currentlen -= (64-ctx->sbuf_len); + memcpy(ctx->sbuf + ctx->sbuf_len, input, (MBEDTLS_MD5_BLOCK_SIZE-ctx->sbuf_len)); + currentlen -= (MBEDTLS_MD5_BLOCK_SIZE-ctx->sbuf_len); mbedtls_md5_process(ctx, ctx->sbuf); // now process every input as long as it is %4 bytes size_t iter = currentlen / 4; - HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, (uint8_t *)(input+64-ctx->sbuf_len), (iter*4)); + HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, (uint8_t *)(input+MBEDTLS_MD5_BLOCK_SIZE-ctx->sbuf_len), (iter*4)); // sbuf is now fully accumulated, now copy 1 / 2 or 3 remaining bytes ctx->sbuf_len = currentlen % 4; if (ctx->sbuf_len !=0) { @@ -112,7 +112,7 @@ void mbedtls_md5_finish( mbedtls_md5_context *ctx, unsigned char output[16] ) if (ctx->sbuf_len > 0) { HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, ctx->sbuf, ctx->sbuf_len); } - mbedtls_zeroize( ctx->sbuf, 64); + mbedtls_zeroize( ctx->sbuf, MBEDTLS_MD5_BLOCK_SIZE); ctx->sbuf_len = 0; __HAL_HASH_START_DIGEST(); diff --git a/features/mbedtls/targets/TARGET_STM/md5_alt.h b/features/mbedtls/targets/TARGET_STM/md5_alt.h index 7998167a3d9..5c8daf96791 100644 --- a/features/mbedtls/targets/TARGET_STM/md5_alt.h +++ b/features/mbedtls/targets/TARGET_STM/md5_alt.h @@ -31,16 +31,18 @@ extern "C" { #endif +#define MBEDTLS_MD5_BLOCK_SIZE (64) // must be a multiple of 4 /** * \brief MD5 context structure * \note HAL_HASH_MD5_Accumulate cannot handle less than 4 bytes, unless it is the last call to the function - * A 64 bytes buffer is used to save values and handle the processing 64 bytes per 64 bytes + * A MBEDTLS_MD5_BLOCK_SIZE bytes buffer is used to save values and handle the processing + * MBEDTLS_MD5_BLOCK_SIZE bytes per MBEDTLS_MD5_BLOCK_SIZE bytes * If MD5_finish is called and sbuf_len>0, the remaining bytes are accumulated prior to the call to HAL_HASH_MD5_Finish */ typedef struct { HASH_HandleTypeDef hhash_md5;/*!< ST HAL HASH struct */ - unsigned char sbuf[64]; /*!< 64 buffer to store values so that algorithm is caled once the buffer is filled */ + unsigned char sbuf[MBEDTLS_MD5_BLOCK_SIZE]; /*!< MBEDTLS_MD5_BLOCK_SIZE buffer to store values so that algorithm is caled once the buffer is filled */ unsigned char sbuf_len; /*!< number of bytes to be processed in sbuf */ } mbedtls_md5_context;