From bd1c4f5c623b3a93f14d32c15e48adbc0381e93c Mon Sep 17 00:00:00 2001 From: adustm Date: Thu, 16 Mar 2017 18:24:21 +0100 Subject: [PATCH 01/14] NUCLEO_F756ZG/mbedtls : MD5 hw activation --- .../TARGET_STM32F7/TARGET_NUCLEO_F756ZG/mbedtls_device.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/features/mbedtls/targets/TARGET_STM/TARGET_STM32F7/TARGET_NUCLEO_F756ZG/mbedtls_device.h b/features/mbedtls/targets/TARGET_STM/TARGET_STM32F7/TARGET_NUCLEO_F756ZG/mbedtls_device.h index 1824a47b36a..d9f663b2dd1 100644 --- a/features/mbedtls/targets/TARGET_STM/TARGET_STM32F7/TARGET_NUCLEO_F756ZG/mbedtls_device.h +++ b/features/mbedtls/targets/TARGET_STM/TARGET_STM32F7/TARGET_NUCLEO_F756ZG/mbedtls_device.h @@ -25,5 +25,9 @@ #define MBEDTLS_SHA256_ALT +#define MBEDTLS_MD5_ALT + +#define MBEDTLS_MD5_C + #endif /* MBEDTLS_DEVICE_H */ From c174191eb90e19b300d598b48f133f452a5f9e2a Mon Sep 17 00:00:00 2001 From: adustm Date: Mon, 3 Apr 2017 17:19:20 +0200 Subject: [PATCH 02/14] Move MBEDTLS_MD5_C define from mbedtls_device.h to targets.json --- .../TARGET_STM32F7/TARGET_NUCLEO_F756ZG/mbedtls_device.h | 5 +---- targets/targets.json | 1 + 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/features/mbedtls/targets/TARGET_STM/TARGET_STM32F7/TARGET_NUCLEO_F756ZG/mbedtls_device.h b/features/mbedtls/targets/TARGET_STM/TARGET_STM32F7/TARGET_NUCLEO_F756ZG/mbedtls_device.h index d9f663b2dd1..8382789dfbe 100644 --- a/features/mbedtls/targets/TARGET_STM/TARGET_STM32F7/TARGET_NUCLEO_F756ZG/mbedtls_device.h +++ b/features/mbedtls/targets/TARGET_STM/TARGET_STM32F7/TARGET_NUCLEO_F756ZG/mbedtls_device.h @@ -1,5 +1,5 @@ /* - * mbedtls_device.h + * mbedtls_device.h ******************************************************************************* * Copyright (c) 2017, STMicroelectronics * SPDX-License-Identifier: Apache-2.0 @@ -27,7 +27,4 @@ #define MBEDTLS_SHA256_ALT #define MBEDTLS_MD5_ALT -#define MBEDTLS_MD5_C - - #endif /* MBEDTLS_DEVICE_H */ diff --git a/targets/targets.json b/targets/targets.json index b67a5047517..6c3ba615b90 100644 --- a/targets/targets.json +++ b/targets/targets.json @@ -1192,6 +1192,7 @@ "inherits": ["FAMILY_STM32"], "core": "Cortex-M7F", "extra_labels_add": ["STM32F7", "STM32F756", "STM32F756xG", "STM32F756ZG"], + "macros_add": ["MBEDTLS_CONFIG_HW_SUPPORT", "MBEDTLS_MD5_C"], "config": { "d11_configuration": { "help": "Value: PA_7 for the default board configuration, PB_5 in case of solder bridge update (SB121 off/ SB122 on)", From e126975d9155bce7ff915c78da67116073ba5596 Mon Sep 17 00:00:00 2001 From: adustm Date: Mon, 3 Apr 2017 17:20:59 +0200 Subject: [PATCH 03/14] Remove unnecessary functions in md5_alt.h file --- features/mbedtls/targets/TARGET_STM/md5_alt.h | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 features/mbedtls/targets/TARGET_STM/md5_alt.h diff --git a/features/mbedtls/targets/TARGET_STM/md5_alt.h b/features/mbedtls/targets/TARGET_STM/md5_alt.h new file mode 100644 index 00000000000..91b3c8db9a4 --- /dev/null +++ b/features/mbedtls/targets/TARGET_STM/md5_alt.h @@ -0,0 +1,112 @@ +/** + * \file md5_alt.h + * + * \brief MD5 hw acceleration (hash function) + * + * Copyright (c) 2017, STMicroelectronics + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +#ifndef MBEDTLS_MD5_ALT_H +#define MBEDTLS_MD5_ALT_H + +#if defined(MBEDTLS_MD5_ALT) +#include "mbedtls/platform.h" +#include "mbedtls/config.h" + +#include "cmsis.h" +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * \brief MD5 context structure + */ +typedef struct +{ + uint32_t total[2]; /*!< number of bytes processed */ + uint32_t state[4]; /*!< intermediate digest state */ + unsigned char buffer[64]; /*!< data block being processed */ + HASH_HandleTypeDef hhash_md5; +} +mbedtls_md5_context; + +/** + * \brief Initialize MD5 context + * + * \param ctx MD5 context to be initialized + */ +void mbedtls_md5_init( mbedtls_md5_context *ctx ); + +/** + * \brief Clear MD5 context + * + * \param ctx MD5 context to be cleared + */ +void mbedtls_md5_free( mbedtls_md5_context *ctx ); + +/** + * \brief Clone (the state of) an MD5 context + * + * \param dst The destination context + * \param src The context to be cloned + */ +void mbedtls_md5_clone( mbedtls_md5_context *dst, + const mbedtls_md5_context *src ); + +/** + * \brief MD5 context setup + * + * \param ctx context to be initialized + */ +void mbedtls_md5_starts( mbedtls_md5_context *ctx ); + +/** + * \brief MD5 process buffer + * + * \param ctx MD5 context + * \param input buffer holding the data + * \param ilen length of the input data + */ +void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, size_t ilen ); + +/** + * \brief MD5 final digest + * + * \param ctx MD5 context + * \param output MD5 checksum result + */ +void mbedtls_md5_finish( mbedtls_md5_context *ctx, unsigned char output[16] ); + +/* Internal use */ +void mbedtls_md5_process( mbedtls_md5_context *ctx, const unsigned char data[64] ); + +#ifdef __cplusplus +} +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* MBEDTLS_MD5_ALT */ + +#endif /* md5_alt.h */ From 29114f1a562c70a49e367403e38d1045e498da45 Mon Sep 17 00:00:00 2001 From: adustm Date: Tue, 18 Apr 2017 13:06:01 +0200 Subject: [PATCH 04/14] Remove unused variables in mbedtls_md5_context + remove unnecessary ifdef __cplusplus --- features/mbedtls/targets/TARGET_STM/md5_alt.h | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/features/mbedtls/targets/TARGET_STM/md5_alt.h b/features/mbedtls/targets/TARGET_STM/md5_alt.h index 91b3c8db9a4..744990f5068 100644 --- a/features/mbedtls/targets/TARGET_STM/md5_alt.h +++ b/features/mbedtls/targets/TARGET_STM/md5_alt.h @@ -38,9 +38,6 @@ extern "C" { */ typedef struct { - uint32_t total[2]; /*!< number of bytes processed */ - uint32_t state[4]; /*!< intermediate digest state */ - unsigned char buffer[64]; /*!< data block being processed */ HASH_HandleTypeDef hhash_md5; } mbedtls_md5_context; @@ -99,14 +96,6 @@ void mbedtls_md5_process( mbedtls_md5_context *ctx, const unsigned char data[64] } #endif -#ifdef __cplusplus -extern "C" { -#endif - -#ifdef __cplusplus -} -#endif - #endif /* MBEDTLS_MD5_ALT */ #endif /* md5_alt.h */ From b2092f072f8f359111f2c6585fc9b19c87d798d6 Mon Sep 17 00:00:00 2001 From: adustm Date: Thu, 4 May 2017 18:51:53 +0200 Subject: [PATCH 05/14] Handle 64bytes per 64 bytes + remove unused includes files --- features/mbedtls/targets/TARGET_STM/md5_alt.c | 127 ++++++++++++++++++ features/mbedtls/targets/TARGET_STM/md5_alt.h | 9 +- 2 files changed, 133 insertions(+), 3 deletions(-) create mode 100644 features/mbedtls/targets/TARGET_STM/md5_alt.c diff --git a/features/mbedtls/targets/TARGET_STM/md5_alt.c b/features/mbedtls/targets/TARGET_STM/md5_alt.c new file mode 100644 index 00000000000..8db07f4416b --- /dev/null +++ b/features/mbedtls/targets/TARGET_STM/md5_alt.c @@ -0,0 +1,127 @@ +/* + * MD5 hw acceleration + ******************************************************************************* + * Copyright (c) 2017, STMicroelectronics + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "mbedtls/md5.h" + +#if defined(MBEDTLS_MD5_ALT) +#include "mbedtls/platform.h" + +/* Implementation that should never be optimized out by the compiler */ +static void mbedtls_zeroize( void *v, size_t n ) { + volatile unsigned char *p = v; while( n-- ) *p++ = 0; +} + +void mbedtls_md5_init( mbedtls_md5_context *ctx ) +{ + mbedtls_zeroize( ctx, sizeof( mbedtls_md5_context ) ); + + /* Enable HASH clock */ + __HAL_RCC_HASH_CLK_ENABLE(); + +} + +void mbedtls_md5_free( mbedtls_md5_context *ctx ) +{ + if( ctx == NULL ) + return; + + /* Force the HASH Periheral Clock Reset */ + __HAL_RCC_HASH_FORCE_RESET(); + + /* Release the HASH Periheral Clock Reset */ + __HAL_RCC_HASH_RELEASE_RESET(); + + mbedtls_zeroize( ctx, sizeof( mbedtls_md5_context ) ); +} + +void mbedtls_md5_clone( mbedtls_md5_context *dst, + const mbedtls_md5_context *src ) +{ + *dst = *src; +} + +/* + * MD5 context setup + */ +void mbedtls_md5_starts( mbedtls_md5_context *ctx ) +{ + /* HASH IP initialization */ + HAL_HASH_DeInit(&ctx->hhash_md5); + + /* HASH Configuration */ + ctx->hhash_md5.Init.DataType = HASH_DATATYPE_8B; + if (HAL_HASH_Init(&ctx->hhash_md5) == HAL_ERROR) { + // return error code + return; + } +} + +void mbedtls_md5_process( mbedtls_md5_context *ctx, const unsigned char data[64] ) +{ + HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, (uint8_t *)data, 64); +} + +/* + * MD5 process buffer + */ +void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, size_t ilen ) +{ + unsigned char i=0; + int currentlen = ilen; + /* store mechanism to handle 64 bytes per 64 bytes */ + while ((currentlen+ctx->sbuf_len) >=64) { + if (ctx->sbuf_len ==0) { /* straight forward */ + mbedtls_md5_process(ctx, input+(i*64)); + } else { + memcpy(ctx->sbuf+ctx->sbuf_len, input+(i*64),64-ctx->sbuf_len); + mbedtls_md5_process(ctx, ctx->sbuf); + memcpy(ctx->sbuf,input+(i+1)*64-ctx->sbuf_len, ctx->sbuf_len); + // ctx->sbuf_len remains the same + } + currentlen -= 64; + i++; + } + if (currentlen <0) { + currentlen +=64; + } + /* Store the remaining <64 values */ + memcpy(ctx->sbuf+ctx->sbuf_len, input+(i*64), currentlen); + ctx->sbuf_len += currentlen; + +} + +/* + * MD5 final digest + */ +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); + ctx->sbuf_len = 0; + __HAL_HASH_START_DIGEST(); + + if (HAL_HASH_MD5_Finish(&ctx->hhash_md5, output, 10)) { + // error code to be returned + } +} + +#endif /* MBEDTLS_MD5_ALT */ diff --git a/features/mbedtls/targets/TARGET_STM/md5_alt.h b/features/mbedtls/targets/TARGET_STM/md5_alt.h index 744990f5068..7998167a3d9 100644 --- a/features/mbedtls/targets/TARGET_STM/md5_alt.h +++ b/features/mbedtls/targets/TARGET_STM/md5_alt.h @@ -23,8 +23,6 @@ #define MBEDTLS_MD5_ALT_H #if defined(MBEDTLS_MD5_ALT) -#include "mbedtls/platform.h" -#include "mbedtls/config.h" #include "cmsis.h" #include @@ -35,10 +33,15 @@ extern "C" { /** * \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 + * 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; + 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_len; /*!< number of bytes to be processed in sbuf */ } mbedtls_md5_context; From d39d52b0424511afa9a776c6a4c1b62d5aa78bbe Mon Sep 17 00:00:00 2001 From: adustm Date: Mon, 15 May 2017 13:54:09 +0200 Subject: [PATCH 06/14] Fix use case with size = 0 (md5_selftest #1) --- features/mbedtls/targets/TARGET_STM/md5_alt.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/features/mbedtls/targets/TARGET_STM/md5_alt.c b/features/mbedtls/targets/TARGET_STM/md5_alt.c index 8db07f4416b..f7cf12b0d35 100644 --- a/features/mbedtls/targets/TARGET_STM/md5_alt.c +++ b/features/mbedtls/targets/TARGET_STM/md5_alt.c @@ -86,6 +86,15 @@ void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, s unsigned char i=0; int currentlen = ilen; /* store mechanism to handle 64 bytes per 64 bytes */ + if (currentlen == 0){ // change HW status is size if 0 + 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; + } + ctx->hhash_md5.Phase = HAL_HASH_PHASE_PROCESS; + } while ((currentlen+ctx->sbuf_len) >=64) { if (ctx->sbuf_len ==0) { /* straight forward */ mbedtls_md5_process(ctx, input+(i*64)); From 53a8b75e4ef8d9e7028d5016a1fd6ca937ffe955 Mon Sep 17 00:00:00 2001 From: adustm Date: Thu, 18 May 2017 14:57:23 +0200 Subject: [PATCH 07/14] Improve md5 buffer storing concept --- features/mbedtls/targets/TARGET_STM/md5_alt.c | 50 +++++++------------ 1 file changed, 19 insertions(+), 31 deletions(-) diff --git a/features/mbedtls/targets/TARGET_STM/md5_alt.c b/features/mbedtls/targets/TARGET_STM/md5_alt.c index f7cf12b0d35..9b4a701ee86 100644 --- a/features/mbedtls/targets/TARGET_STM/md5_alt.c +++ b/features/mbedtls/targets/TARGET_STM/md5_alt.c @@ -57,9 +57,6 @@ void mbedtls_md5_clone( mbedtls_md5_context *dst, *dst = *src; } -/* - * MD5 context setup - */ void mbedtls_md5_starts( mbedtls_md5_context *ctx ) { /* HASH IP initialization */ @@ -78,15 +75,11 @@ void mbedtls_md5_process( mbedtls_md5_context *ctx, const unsigned char data[64] HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, (uint8_t *)data, 64); } -/* - * MD5 process buffer - */ void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, size_t ilen ) { - unsigned char i=0; - int currentlen = ilen; - /* store mechanism to handle 64 bytes per 64 bytes */ - if (currentlen == 0){ // change HW status is size if 0 + size_t currentlen = ilen; + // store mechanism to handle 64 bytes per 64 bytes + if (currentlen == 0){ // only change HW status is size if 0 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 @@ -94,31 +87,26 @@ 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; - } - while ((currentlen+ctx->sbuf_len) >=64) { - if (ctx->sbuf_len ==0) { /* straight forward */ - mbedtls_md5_process(ctx, input+(i*64)); - } else { - memcpy(ctx->sbuf+ctx->sbuf_len, input+(i*64),64-ctx->sbuf_len); - mbedtls_md5_process(ctx, ctx->sbuf); - memcpy(ctx->sbuf,input+(i+1)*64-ctx->sbuf_len, ctx->sbuf_len); - // ctx->sbuf_len remains the same + } else if (currentlen < (64-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); + 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)); + // sbuf is now fully accumulated, now copy 1 / 2 or 3 remaining bytes + ctx->sbuf_len = currentlen % 4; + if (ctx->sbuf_len !=0) { + memcpy(ctx->sbuf, input+iter, ctx->sbuf_len); } - currentlen -= 64; - i++; } - if (currentlen <0) { - currentlen +=64; - } - /* Store the remaining <64 values */ - memcpy(ctx->sbuf+ctx->sbuf_len, input+(i*64), currentlen); - ctx->sbuf_len += currentlen; - } -/* - * MD5 final digest - */ void mbedtls_md5_finish( mbedtls_md5_context *ctx, unsigned char output[16] ) { if (ctx->sbuf_len > 0) { From 766e451c88f3df52be67af197a4dfea92f006e83 Mon Sep 17 00:00:00 2001 From: adustm Date: Thu, 18 May 2017 15:12:29 +0200 Subject: [PATCH 08/14] Replace 64 by a define --- features/mbedtls/targets/TARGET_STM/md5_alt.c | 16 ++++++++-------- features/mbedtls/targets/TARGET_STM/md5_alt.h | 6 ++++-- 2 files changed, 12 insertions(+), 10 deletions(-) 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; From de2899279eea8bf9500363738630d5afc27f05f8 Mon Sep 17 00:00:00 2001 From: adustm Date: Thu, 13 Apr 2017 13:58:35 +0200 Subject: [PATCH 09/14] Move MBEDTLS_MD5_C from mbetdls_device.h to targets.json --- .../TARGET_STM32F4/TARGET_STM32F439xI/mbedtls_device.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/features/mbedtls/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/mbedtls_device.h b/features/mbedtls/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/mbedtls_device.h index c78d135083f..8609b4ecc6d 100644 --- a/features/mbedtls/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/mbedtls_device.h +++ b/features/mbedtls/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/mbedtls_device.h @@ -1,5 +1,5 @@ /* - * mbedtls_device.h + * mbedtls_device.h ******************************************************************************* * Copyright (c) 2017, STMicroelectronics * SPDX-License-Identifier: Apache-2.0 @@ -26,4 +26,7 @@ #define MBEDTLS_SHA1_ALT +#define MBEDTLS_MD5_ALT + +#define MBEDTLS_MD5_C #endif /* MBEDTLS_DEVICE_H */ From 9572183b878ae088d07b13a544e365dd236306da Mon Sep 17 00:00:00 2001 From: adustm Date: Mon, 22 May 2017 18:02:13 +0200 Subject: [PATCH 10/14] Remove MBEDTLS_MD5_C --- targets/targets.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/targets/targets.json b/targets/targets.json index 6c3ba615b90..a5363e4c0d2 100644 --- a/targets/targets.json +++ b/targets/targets.json @@ -1192,7 +1192,7 @@ "inherits": ["FAMILY_STM32"], "core": "Cortex-M7F", "extra_labels_add": ["STM32F7", "STM32F756", "STM32F756xG", "STM32F756ZG"], - "macros_add": ["MBEDTLS_CONFIG_HW_SUPPORT", "MBEDTLS_MD5_C"], + "macros_add": ["MBEDTLS_CONFIG_HW_SUPPORT", "MBEDTLS_MD5_C", "MBEDTLS_CONFIG_HW_SUPPORT"], "config": { "d11_configuration": { "help": "Value: PA_7 for the default board configuration, PB_5 in case of solder bridge update (SB121 off/ SB122 on)", From f033c87640edc80416a4733a4b168835280bcafc Mon Sep 17 00:00:00 2001 From: adustm Date: Fri, 2 Jun 2017 17:47:56 +0200 Subject: [PATCH 11/14] Handle context swap + Modify macro name ST_MD5_BLOCK_SIZE --- features/mbedtls/targets/TARGET_STM/md5_alt.c | 86 +++++++++++++------ features/mbedtls/targets/TARGET_STM/md5_alt.h | 19 ++-- 2 files changed, 71 insertions(+), 34 deletions(-) diff --git a/features/mbedtls/targets/TARGET_STM/md5_alt.c b/features/mbedtls/targets/TARGET_STM/md5_alt.c index 1d3918e9571..e6fd97dc140 100644 --- a/features/mbedtls/targets/TARGET_STM/md5_alt.c +++ b/features/mbedtls/targets/TARGET_STM/md5_alt.c @@ -17,7 +17,7 @@ * limitations under the License. * */ - +#if defined(MBEDTLS_MD5_C) #include "mbedtls/md5.h" #if defined(MBEDTLS_MD5_ALT) @@ -28,6 +28,28 @@ static void mbedtls_zeroize( void *v, size_t n ) { volatile unsigned char *p = v; while( n-- ) *p++ = 0; } +static void st_md5_restore_hw_context(mbedtls_md5_context *ctx) +{ + uint32_t i; + /* allow multi-instance of HASH use: save context for HASH HW module CR */ + HASH->STR = ctx->ctx_save_str; + HASH->CR = (ctx->ctx_save_cr | HASH_CR_INIT); + for (i=0;i<38;i++) { + HASH->CSR[i] = ctx->ctx_save_csr[i]; + } +} + +static void st_md5_save_hw_context(mbedtls_md5_context *ctx) +{ + uint32_t i; + /* allow multi-instance of HASH use: restore context for HASH HW module CR */ + ctx->ctx_save_cr = HASH->CR; + ctx->ctx_save_str = HASH->STR; + for (i=0;i<38;i++) { + ctx->ctx_save_csr[i] = HASH->CSR[i]; + } +} + void mbedtls_md5_init( mbedtls_md5_context *ctx ) { mbedtls_zeroize( ctx, sizeof( mbedtls_md5_context ) ); @@ -41,13 +63,6 @@ void mbedtls_md5_free( mbedtls_md5_context *ctx ) { if( ctx == NULL ) return; - - /* Force the HASH Periheral Clock Reset */ - __HAL_RCC_HASH_FORCE_RESET(); - - /* Release the HASH Periheral Clock Reset */ - __HAL_RCC_HASH_RELEASE_RESET(); - mbedtls_zeroize( ctx, sizeof( mbedtls_md5_context ) ); } @@ -60,65 +75,84 @@ void mbedtls_md5_clone( mbedtls_md5_context *dst, void mbedtls_md5_starts( mbedtls_md5_context *ctx ) { /* HASH IP initialization */ - HAL_HASH_DeInit(&ctx->hhash_md5); + if (HAL_HASH_DeInit(&ctx->hhash_md5) != 0) { + // error found to be returned + return; + } /* HASH Configuration */ ctx->hhash_md5.Init.DataType = HASH_DATATYPE_8B; - if (HAL_HASH_Init(&ctx->hhash_md5) == HAL_ERROR) { + if (HAL_HASH_Init(&ctx->hhash_md5) != 0) { // return error code return; } + st_md5_save_hw_context(ctx); } -void mbedtls_md5_process( mbedtls_md5_context *ctx, const unsigned char data[MBEDTLS_MD5_BLOCK_SIZE] ) +void mbedtls_md5_process( mbedtls_md5_context *ctx, const unsigned char data[ST_MD5_BLOCK_SIZE] ) { - HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, (uint8_t *)data, MBEDTLS_MD5_BLOCK_SIZE); + st_md5_restore_hw_context(ctx); + if (HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, (uint8_t *)data, ST_MD5_BLOCK_SIZE) != 0) { + return; // Return error code here + } + st_md5_save_hw_context(ctx); } void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, size_t ilen ) { size_t currentlen = ilen; - // store mechanism to handle MBEDTLS_MD5_BLOCK_SIZE bytes per MBEDTLS_MD5_BLOCK_SIZE bytes + st_md5_restore_hw_context(ctx); + + // store mechanism to accumulate ST_MD5_BLOCK_SIZE bytes (512 bits) in the HW if (currentlen == 0){ // only change HW status is size if 0 - if(ctx->hhash_md5.Phase == HAL_HASH_PHASE_READY) - { + 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; } ctx->hhash_md5.Phase = HAL_HASH_PHASE_PROCESS; - } else if (currentlen < (MBEDTLS_MD5_BLOCK_SIZE-ctx->sbuf_len)) { + } else if (currentlen < (ST_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, (MBEDTLS_MD5_BLOCK_SIZE-ctx->sbuf_len)); - currentlen -= (MBEDTLS_MD5_BLOCK_SIZE-ctx->sbuf_len); + memcpy(ctx->sbuf + ctx->sbuf_len, input, (ST_MD5_BLOCK_SIZE - ctx->sbuf_len)); + currentlen -= (ST_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+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; + // Process every input as long as it is %64 bytes, ie 512 bits + size_t iter = currentlen / ST_MD5_BLOCK_SIZE; + if (iter !=0) { + if (HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, (uint8_t *)(input + ST_MD5_BLOCK_SIZE - ctx->sbuf_len), (iter * ST_MD5_BLOCK_SIZE)) != 0) { + return; // Return error code here + } + } + // sbuf is completely accumulated, now copy up to 63 remaining bytes + ctx->sbuf_len = currentlen % ST_MD5_BLOCK_SIZE; if (ctx->sbuf_len !=0) { - memcpy(ctx->sbuf, input+iter, ctx->sbuf_len); + memcpy(ctx->sbuf, input + ilen - ctx->sbuf_len, ctx->sbuf_len); } } + st_md5_save_hw_context(ctx); } void mbedtls_md5_finish( mbedtls_md5_context *ctx, unsigned char output[16] ) { + st_md5_restore_hw_context(ctx); if (ctx->sbuf_len > 0) { - HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, ctx->sbuf, ctx->sbuf_len); + if (HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, ctx->sbuf, ctx->sbuf_len) != 0) { + return; // Return error code here + } } - mbedtls_zeroize( ctx->sbuf, MBEDTLS_MD5_BLOCK_SIZE); + mbedtls_zeroize( ctx->sbuf, ST_MD5_BLOCK_SIZE); ctx->sbuf_len = 0; __HAL_HASH_START_DIGEST(); if (HAL_HASH_MD5_Finish(&ctx->hhash_md5, output, 10)) { // error code to be returned } + st_md5_save_hw_context(ctx); } #endif /* MBEDTLS_MD5_ALT */ +#endif /* MBEDTLS_MD5_C */ diff --git a/features/mbedtls/targets/TARGET_STM/md5_alt.h b/features/mbedtls/targets/TARGET_STM/md5_alt.h index 5c8daf96791..8c07747dfd4 100644 --- a/features/mbedtls/targets/TARGET_STM/md5_alt.h +++ b/features/mbedtls/targets/TARGET_STM/md5_alt.h @@ -31,19 +31,22 @@ extern "C" { #endif -#define MBEDTLS_MD5_BLOCK_SIZE (64) // must be a multiple of 4 +#define ST_MD5_BLOCK_SIZE ((size_t)(64)) // HW handles 512 bits, ie 64 bytes /** * \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 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 + * \note HAL_HASH_MD5_Accumulate will accumulate 512 bits packets, unless it is the last call to the function + * A ST_MD5_BLOCK_SIZE bytes buffer is used to save values and handle the processing + * ST_MD5_BLOCK_SIZE bytes per ST_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[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 */ + HASH_HandleTypeDef hhash_md5;/*!< ST HAL HASH struct */ + unsigned char sbuf[ST_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 */ + uint32_t ctx_save_cr; + uint32_t ctx_save_str; + uint32_t ctx_save_csr[38]; } mbedtls_md5_context; @@ -95,7 +98,7 @@ void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, s void mbedtls_md5_finish( mbedtls_md5_context *ctx, unsigned char output[16] ); /* Internal use */ -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[ST_MD5_BLOCK_SIZE] ); #ifdef __cplusplus } From 85c8bf87a1b8b6bd584ea70ccaecdee913213439 Mon Sep 17 00:00:00 2001 From: adustm Date: Thu, 8 Jun 2017 14:12:07 +0200 Subject: [PATCH 12/14] Add a check 'non busy' status of the HW before save restore procedures --- features/mbedtls/targets/TARGET_STM/md5_alt.c | 51 +++++++++++++++---- features/mbedtls/targets/TARGET_STM/md5_alt.h | 2 + 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/features/mbedtls/targets/TARGET_STM/md5_alt.c b/features/mbedtls/targets/TARGET_STM/md5_alt.c index e6fd97dc140..5a3e816f933 100644 --- a/features/mbedtls/targets/TARGET_STM/md5_alt.c +++ b/features/mbedtls/targets/TARGET_STM/md5_alt.c @@ -28,26 +28,44 @@ static void mbedtls_zeroize( void *v, size_t n ) { volatile unsigned char *p = v; while( n-- ) *p++ = 0; } -static void st_md5_restore_hw_context(mbedtls_md5_context *ctx) +static int st_md5_restore_hw_context(mbedtls_md5_context *ctx) { uint32_t i; + uint32_t tickstart; /* allow multi-instance of HASH use: save context for HASH HW module CR */ + /* Check that there is no HASH activity on going */ + tickstart = HAL_GetTick(); + while ((HASH->SR & (HASH_FLAG_BUSY | HASH_FLAG_DMAS)) != 0) { + if ((HAL_GetTick() - tickstart) > ST_MD5_TIMEOUT) { + return 0; // timeout: HASH processor is busy + } + } HASH->STR = ctx->ctx_save_str; HASH->CR = (ctx->ctx_save_cr | HASH_CR_INIT); for (i=0;i<38;i++) { HASH->CSR[i] = ctx->ctx_save_csr[i]; } + return 1; } -static void st_md5_save_hw_context(mbedtls_md5_context *ctx) +static int st_md5_save_hw_context(mbedtls_md5_context *ctx) { uint32_t i; + uint32_t tickstart; + /* Check that there is no HASH activity on going */ + tickstart = HAL_GetTick(); + while ((HASH->SR & (HASH_FLAG_BUSY | HASH_FLAG_DMAS)) != 0) { + if ((HAL_GetTick() - tickstart) > ST_MD5_TIMEOUT) { + return 0; // timeout: HASH processor is busy + } + } /* allow multi-instance of HASH use: restore context for HASH HW module CR */ ctx->ctx_save_cr = HASH->CR; ctx->ctx_save_str = HASH->STR; for (i=0;i<38;i++) { ctx->ctx_save_csr[i] = HASH->CSR[i]; } + return 1; } void mbedtls_md5_init( mbedtls_md5_context *ctx ) @@ -86,23 +104,30 @@ void mbedtls_md5_starts( mbedtls_md5_context *ctx ) // return error code return; } - st_md5_save_hw_context(ctx); + if (st_md5_save_hw_context(ctx) != 1) { + return; // return HASH_BUSY timeout Error here + } } void mbedtls_md5_process( mbedtls_md5_context *ctx, const unsigned char data[ST_MD5_BLOCK_SIZE] ) { - st_md5_restore_hw_context(ctx); + if (st_md5_restore_hw_context(ctx) != 1) { + return; // Return HASH_BUSY timout error here + } if (HAL_HASH_MD5_Accumulate(&ctx->hhash_md5, (uint8_t *)data, ST_MD5_BLOCK_SIZE) != 0) { return; // Return error code here } - st_md5_save_hw_context(ctx); + if (st_md5_save_hw_context(ctx) != 1) { + return; // return HASH_BUSY timeout Error here + } } void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, size_t ilen ) { size_t currentlen = ilen; - st_md5_restore_hw_context(ctx); - + if (st_md5_restore_hw_context(ctx) != 1) { + return; // Return HASH_BUSY timout error here + } // store mechanism to accumulate ST_MD5_BLOCK_SIZE bytes (512 bits) in the HW if (currentlen == 0){ // only change HW status is size if 0 if(ctx->hhash_md5.Phase == HAL_HASH_PHASE_READY) { @@ -133,12 +158,16 @@ void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, s memcpy(ctx->sbuf, input + ilen - ctx->sbuf_len, ctx->sbuf_len); } } - st_md5_save_hw_context(ctx); + if (st_md5_save_hw_context(ctx) != 1) { + return; // return HASH_BUSY timeout Error here + } } void mbedtls_md5_finish( mbedtls_md5_context *ctx, unsigned char output[16] ) { - st_md5_restore_hw_context(ctx); + 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 @@ -151,7 +180,9 @@ void mbedtls_md5_finish( mbedtls_md5_context *ctx, unsigned char output[16] ) if (HAL_HASH_MD5_Finish(&ctx->hhash_md5, output, 10)) { // error code to be returned } - st_md5_save_hw_context(ctx); + if (st_md5_save_hw_context(ctx) != 1) { + return; // return HASH_BUSY timeout Error here + } } #endif /* MBEDTLS_MD5_ALT */ diff --git a/features/mbedtls/targets/TARGET_STM/md5_alt.h b/features/mbedtls/targets/TARGET_STM/md5_alt.h index 8c07747dfd4..7ea0df21e19 100644 --- a/features/mbedtls/targets/TARGET_STM/md5_alt.h +++ b/features/mbedtls/targets/TARGET_STM/md5_alt.h @@ -32,6 +32,8 @@ extern "C" { #endif #define ST_MD5_BLOCK_SIZE ((size_t)(64)) // HW handles 512 bits, ie 64 bytes +#define ST_MD5_TIMEOUT ((uint32_t) 3) + /** * \brief MD5 context structure * \note HAL_HASH_MD5_Accumulate will accumulate 512 bits packets, unless it is the last call to the function From d92e4b5fcdcc37131df35f831f4e1e0d7e844746 Mon Sep 17 00:00:00 2001 From: Martin Kojtal <0xc0170@gmail.com> Date: Mon, 3 Jul 2017 18:53:58 +0100 Subject: [PATCH 13/14] STM32F4: remove md5 from the mbedtls config file --- .../TARGET_STM32F4/TARGET_STM32F439xI/mbedtls_device.h | 1 - targets/targets.json | 1 - 2 files changed, 2 deletions(-) diff --git a/features/mbedtls/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/mbedtls_device.h b/features/mbedtls/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/mbedtls_device.h index 8609b4ecc6d..dfbc82055ef 100644 --- a/features/mbedtls/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/mbedtls_device.h +++ b/features/mbedtls/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/mbedtls_device.h @@ -28,5 +28,4 @@ #define MBEDTLS_MD5_ALT -#define MBEDTLS_MD5_C #endif /* MBEDTLS_DEVICE_H */ diff --git a/targets/targets.json b/targets/targets.json index a5363e4c0d2..b67a5047517 100644 --- a/targets/targets.json +++ b/targets/targets.json @@ -1192,7 +1192,6 @@ "inherits": ["FAMILY_STM32"], "core": "Cortex-M7F", "extra_labels_add": ["STM32F7", "STM32F756", "STM32F756xG", "STM32F756ZG"], - "macros_add": ["MBEDTLS_CONFIG_HW_SUPPORT", "MBEDTLS_MD5_C", "MBEDTLS_CONFIG_HW_SUPPORT"], "config": { "d11_configuration": { "help": "Value: PA_7 for the default board configuration, PB_5 in case of solder bridge update (SB121 off/ SB122 on)", From 744c36468301034ee2a05391a2c72a12b1ee2626 Mon Sep 17 00:00:00 2001 From: Martin Kojtal <0xc0170@gmail.com> Date: Wed, 2 Aug 2017 14:04:15 +0100 Subject: [PATCH 14/14] STM mbedtls: clear algo value for md5/sha1 and sha256 The hw block for mbedtls is shared, thus HASH algo value should be cleared in the init. --- features/mbedtls/targets/TARGET_STM/md5_alt.c | 2 ++ features/mbedtls/targets/TARGET_STM/sha1_alt.c | 2 ++ features/mbedtls/targets/TARGET_STM/sha256_alt.c | 2 ++ 3 files changed, 6 insertions(+) diff --git a/features/mbedtls/targets/TARGET_STM/md5_alt.c b/features/mbedtls/targets/TARGET_STM/md5_alt.c index 5a3e816f933..1a954ca2135 100644 --- a/features/mbedtls/targets/TARGET_STM/md5_alt.c +++ b/features/mbedtls/targets/TARGET_STM/md5_alt.c @@ -100,6 +100,8 @@ void mbedtls_md5_starts( mbedtls_md5_context *ctx ) /* HASH Configuration */ ctx->hhash_md5.Init.DataType = HASH_DATATYPE_8B; + /* clear CR ALGO value */ + HASH->CR &= ~HASH_CR_ALGO_Msk; if (HAL_HASH_Init(&ctx->hhash_md5) != 0) { // return error code return; diff --git a/features/mbedtls/targets/TARGET_STM/sha1_alt.c b/features/mbedtls/targets/TARGET_STM/sha1_alt.c index 98317c21fa6..cfd90416f29 100644 --- a/features/mbedtls/targets/TARGET_STM/sha1_alt.c +++ b/features/mbedtls/targets/TARGET_STM/sha1_alt.c @@ -98,6 +98,8 @@ void mbedtls_sha1_starts( mbedtls_sha1_context *ctx ) /* HASH Configuration */ ctx->hhash_sha1.Init.DataType = HASH_DATATYPE_8B; + /* clear CR ALGO value */ + HASH->CR &= ~HASH_CR_ALGO_Msk; if (HAL_HASH_Init(&ctx->hhash_sha1) == HAL_ERROR) { // error found to be returned return; diff --git a/features/mbedtls/targets/TARGET_STM/sha256_alt.c b/features/mbedtls/targets/TARGET_STM/sha256_alt.c index a3354e9968a..6729cdac2e2 100644 --- a/features/mbedtls/targets/TARGET_STM/sha256_alt.c +++ b/features/mbedtls/targets/TARGET_STM/sha256_alt.c @@ -99,6 +99,8 @@ void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 ) ctx->is224 = is224; /* HASH Configuration */ ctx->hhash_sha256.Init.DataType = HASH_DATATYPE_8B; + /* clear CR ALGO value */ + HASH->CR &= ~HASH_CR_ALGO_Msk; if (HAL_HASH_Init(&ctx->hhash_sha256) == HAL_ERROR) { // error found to be returned return;