diff --git a/features/mbedtls/targets/TARGET_STM/md5_alt.c b/features/mbedtls/targets/TARGET_STM/md5_alt.c index 281e6f5d51..b3903915de 100644 --- a/features/mbedtls/targets/TARGET_STM/md5_alt.c +++ b/features/mbedtls/targets/TARGET_STM/md5_alt.c @@ -170,17 +170,12 @@ void mbedtls_md5_finish( mbedtls_md5_context *ctx, unsigned char output[16] ) if (st_md5_restore_hw_context(ctx) != 1) { return; // Return HASH_BUSY timout error here } - if (ctx->sbuf_len > 0) { - if (HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, ctx->sbuf, ctx->sbuf_len) != 0) { - return; // Return error code here - } - } - /* The following test can happen when the input is empty, and mbedtls_md5_update has never been called */ - if(ctx->hhash_md5.Phase == HAL_HASH_PHASE_READY) { - /* Select the MD5 mode and reset the HASH processor core, so that the HASH will be ready to compute - the message digest of a new message */ - HASH->CR |= HASH_ALGOSELECTION_MD5 | HASH_CR_INIT; + /* Last accumulation for extra bytes in sbuf_len */ + /* This allows the HW flags to be in place in case mbedtls_sha256_update has not been called yet */ + if (HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, ctx->sbuf, ctx->sbuf_len) != 0) { + return; // Return error code here } + mbedtls_zeroize( ctx->sbuf, ST_MD5_BLOCK_SIZE); ctx->sbuf_len = 0; __HAL_HASH_START_DIGEST(); diff --git a/features/mbedtls/targets/TARGET_STM/sha1_alt.c b/features/mbedtls/targets/TARGET_STM/sha1_alt.c index 752125de1f..37b46c1807 100644 --- a/features/mbedtls/targets/TARGET_STM/sha1_alt.c +++ b/features/mbedtls/targets/TARGET_STM/sha1_alt.c @@ -169,16 +169,10 @@ void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] ) return; // Return HASH_BUSY timout error here } - if (ctx->sbuf_len > 0) { - if (HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, ctx->sbuf, ctx->sbuf_len) != 0) { - return; // Return error code here - } - } - /* The following test can happen when the input is empty, and mbedtls_sha1_update has never been called */ - if(ctx->hhash_sha1.Phase == HAL_HASH_PHASE_READY) { - /* Select the SHA1 mode and reset the HASH processor core, so that the HASH will be ready to compute - the message digest of a new message */ - HASH->CR |= HASH_ALGOSELECTION_SHA1 | HASH_CR_INIT; + /* Last accumulation for extra bytes in sbuf_len */ + /* This allows the HW flags to be in place in case mbedtls_sha256_update has not been called yet */ + if (HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, ctx->sbuf, ctx->sbuf_len) != 0) { + return; // Return error code here } mbedtls_zeroize(ctx->sbuf, ST_SHA1_BLOCK_SIZE); ctx->sbuf_len = 0; diff --git a/features/mbedtls/targets/TARGET_STM/sha256_alt.c b/features/mbedtls/targets/TARGET_STM/sha256_alt.c index 4095da1b16..d5f44042f5 100644 --- a/features/mbedtls/targets/TARGET_STM/sha256_alt.c +++ b/features/mbedtls/targets/TARGET_STM/sha256_alt.c @@ -187,29 +187,18 @@ void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char output[32 if (st_sha256_restore_hw_context(ctx) != 1) { return; // Return HASH_BUSY timout error here } - if (ctx->sbuf_len > 0) { - if (ctx->is224 == 0) { - if (HAL_HASHEx_SHA256_Accumulate(&ctx->hhash_sha256, ctx->sbuf, ctx->sbuf_len) != 0) { - return; // Return error code here - } - } else { - if (HAL_HASHEx_SHA224_Accumulate(&ctx->hhash_sha256, ctx->sbuf, ctx->sbuf_len) != 0) { - return; // Return error code here - } - } - } - /* The following test can happen when the input is empty, and mbedtls_sha256_update has never been called */ - if(ctx->hhash_sha256.Phase == HAL_HASH_PHASE_READY) { - if (ctx->is224 == 0) { - /* Select the SHA256 mode and reset the HASH processor core, so that the HASH will be ready to compute - the message digest of a new message */ - HASH->CR |= HASH_ALGOSELECTION_SHA256 | HASH_CR_INIT; - } else { - /* Select the SHA224 mode and reset the HASH processor core, so that the HASH will be ready to compute - the message digest of a new message */ - HASH->CR |= HASH_ALGOSELECTION_SHA224 | HASH_CR_INIT; + /* Last accumulation for extra bytes in sbuf_len */ + /* This allows the HW flags to be in place in case mbedtls_sha256_update has not been called yet */ + if (ctx->is224 == 0) { + if (HAL_HASHEx_SHA256_Accumulate(&ctx->hhash_sha256, ctx->sbuf, ctx->sbuf_len) != 0) { + return; // Return error code here + } + } else { + if (HAL_HASHEx_SHA224_Accumulate(&ctx->hhash_sha256, ctx->sbuf, ctx->sbuf_len) != 0) { + return; // Return error code here } } + mbedtls_zeroize(ctx->sbuf, ST_SHA256_BLOCK_SIZE); ctx->sbuf_len = 0; __HAL_HASH_START_DIGEST();