@@ -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