Skip to content

Commit 53027fd

Browse files
committed
Improve fix calling Accumulate function every time in finish function
1 parent cba5388 commit 53027fd

File tree

3 files changed

+18
-40
lines changed

3 files changed

+18
-40
lines changed

features/mbedtls/targets/TARGET_STM/md5_alt.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,17 +170,12 @@ void mbedtls_md5_finish( mbedtls_md5_context *ctx, unsigned char output[16] )
170170
if (st_md5_restore_hw_context(ctx) != 1) {
171171
return; // Return HASH_BUSY timout error here
172172
}
173-
if (ctx->sbuf_len > 0) {
174-
if (HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, ctx->sbuf, ctx->sbuf_len) != 0) {
175-
return; // Return error code here
176-
}
177-
}
178-
/* The following test can happen when the input is empty, and mbedtls_md5_update has never been called */
179-
if(ctx->hhash_md5.Phase == HAL_HASH_PHASE_READY) {
180-
/* Select the MD5 mode and reset the HASH processor core, so that the HASH will be ready to compute
181-
the message digest of a new message */
182-
HASH->CR |= HASH_ALGOSELECTION_MD5 | HASH_CR_INIT;
173+
/* 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 */
175+
if (HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, ctx->sbuf, ctx->sbuf_len) != 0) {
176+
return; // Return error code here
183177
}
178+
184179
mbedtls_zeroize( ctx->sbuf, ST_MD5_BLOCK_SIZE);
185180
ctx->sbuf_len = 0;
186181
__HAL_HASH_START_DIGEST();

features/mbedtls/targets/TARGET_STM/sha1_alt.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -169,16 +169,10 @@ void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] )
169169
return; // Return HASH_BUSY timout error here
170170
}
171171

172-
if (ctx->sbuf_len > 0) {
173-
if (HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, ctx->sbuf, ctx->sbuf_len) != 0) {
174-
return; // Return error code here
175-
}
176-
}
177-
/* The following test can happen when the input is empty, and mbedtls_sha1_update has never been called */
178-
if(ctx->hhash_sha1.Phase == HAL_HASH_PHASE_READY) {
179-
/* Select the SHA1 mode and reset the HASH processor core, so that the HASH will be ready to compute
180-
the message digest of a new message */
181-
HASH->CR |= HASH_ALGOSELECTION_SHA1 | HASH_CR_INIT;
172+
/* Last accumulation for extra bytes in sbuf_len */
173+
/* This allows the HW flags to be in place in case mbedtls_sha256_update has not been called yet */
174+
if (HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, ctx->sbuf, ctx->sbuf_len) != 0) {
175+
return; // Return error code here
182176
}
183177
mbedtls_zeroize(ctx->sbuf, ST_SHA1_BLOCK_SIZE);
184178
ctx->sbuf_len = 0;

features/mbedtls/targets/TARGET_STM/sha256_alt.c

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -187,29 +187,18 @@ void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char output[32
187187
if (st_sha256_restore_hw_context(ctx) != 1) {
188188
return; // Return HASH_BUSY timout error here
189189
}
190-
if (ctx->sbuf_len > 0) {
191-
if (ctx->is224 == 0) {
192-
if (HAL_HASHEx_SHA256_Accumulate(&ctx->hhash_sha256, ctx->sbuf, ctx->sbuf_len) != 0) {
193-
return; // Return error code here
194-
}
195-
} else {
196-
if (HAL_HASHEx_SHA224_Accumulate(&ctx->hhash_sha256, ctx->sbuf, ctx->sbuf_len) != 0) {
197-
return; // Return error code here
198-
}
190+
/* Last accumulation for extra bytes in sbuf_len */
191+
/* This allows the HW flags to be in place in case mbedtls_sha256_update has not been called yet */
192+
if (ctx->is224 == 0) {
193+
if (HAL_HASHEx_SHA256_Accumulate(&ctx->hhash_sha256, ctx->sbuf, ctx->sbuf_len) != 0) {
194+
return; // Return error code here
199195
}
200-
}
201-
/* The following test can happen when the input is empty, and mbedtls_sha256_update has never been called */
202-
if(ctx->hhash_sha256.Phase == HAL_HASH_PHASE_READY) {
203-
if (ctx->is224 == 0) {
204-
/* Select the SHA256 mode and reset the HASH processor core, so that the HASH will be ready to compute
205-
the message digest of a new message */
206-
HASH->CR |= HASH_ALGOSELECTION_SHA256 | HASH_CR_INIT;
207-
} else {
208-
/* Select the SHA224 mode and reset the HASH processor core, so that the HASH will be ready to compute
209-
the message digest of a new message */
210-
HASH->CR |= HASH_ALGOSELECTION_SHA224 | HASH_CR_INIT;
196+
} else {
197+
if (HAL_HASHEx_SHA224_Accumulate(&ctx->hhash_sha256, ctx->sbuf, ctx->sbuf_len) != 0) {
198+
return; // Return error code here
211199
}
212200
}
201+
213202
mbedtls_zeroize(ctx->sbuf, ST_SHA256_BLOCK_SIZE);
214203
ctx->sbuf_len = 0;
215204
__HAL_HASH_START_DIGEST();

0 commit comments

Comments
 (0)