diff --git a/features/mbedtls/targets/TARGET_STM/hash_stm32.c b/features/mbedtls/targets/TARGET_STM/hash_stm32.c new file mode 100644 index 0000000000..1830c59fe5 --- /dev/null +++ b/features/mbedtls/targets/TARGET_STM/hash_stm32.c @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved + * Copyright (C) 2019-2020, STMicroelectronics, All Rights Reserved + * 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. + * + * This file implements ST shared HASH services based on API from mbed TLS + */ + +/* Includes ------------------------------------------------------------------*/ +#if !defined(MBEDTLS_CONFIG_FILE) +#include "config.h" +#else +#include MBEDTLS_CONFIG_FILE +#endif + +#if defined(MBEDTLS_SHA1_ALT) || defined(MBEDTLS_SHA256_ALT) || defined(MBEDTLS_MD5_ALT) + +#include "hash_stm32.h" + +/* Variables -----------------------------------------------------------------*/ +/* Mutex protection because of one Hash Hw instance is shared over several */ +/* algorithms (SHA-1, SHA-256, MD5 implementations may be enabled together) */ +#if defined(MBEDTLS_THREADING_C) +mbedtls_threading_mutex_t hash_mutex; +unsigned char hash_mutex_started = 0; +#endif /* MBEDTLS_THREADING_C */ + +unsigned int hash_context_count = 0; + +/* Functions -----------------------------------------------------------------*/ + +/* Implementation that should never be optimized out by the compiler */ +void hash_zeroize(void *v, size_t n) +{ + volatile unsigned char *p = (unsigned char *)v; + while (n--) { + *p++ = 0; + } +} + +/* HAL function that should be implemented in the user file */ +/** +* @brief HASH MSP Initialization +* This function configures the hardware resources used in this example +* @param hhash: HASH handle pointer +* @retval None +*/ +void HAL_HASH_MspInit(HASH_HandleTypeDef *hhash) +{ + /* Peripheral clock enable */ + __HAL_RCC_HASH_CLK_ENABLE(); +} + +/** +* @brief HASH MSP De-Initialization +* This function freeze the hardware resources used in this example +* @param hhash: HASH handle pointer +* @retval None +*/ +void HAL_HASH_MspDeInit(HASH_HandleTypeDef *hhash) +{ + /* Peripheral clock disable */ + __HAL_RCC_HASH_CLK_DISABLE(); +} + +#endif /* MBEDTLS_SHA1_ALT or MBEDTLS_SHA256_ALT or MBEDTLS_MD5_ALT */ \ No newline at end of file diff --git a/features/mbedtls/targets/TARGET_STM/hash_stm32.h b/features/mbedtls/targets/TARGET_STM/hash_stm32.h new file mode 100644 index 0000000000..b91c75615b --- /dev/null +++ b/features/mbedtls/targets/TARGET_STM/hash_stm32.h @@ -0,0 +1,56 @@ +/** + ****************************************************************************** + * @brief Header file of mbed TLS HW crypto (HASH) implementation. + ****************************************************************************** + * @attention + * + * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved + * Copyright (C) 2019 STMicroelectronics, All Rights Reserved + * + * This software component is licensed by ST under Apache 2.0 license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * https://opensource.org/licenses/Apache-2.0 + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __HASH_H +#define __HASH_H + +#if defined(MBEDTLS_SHA1_ALT) || defined(MBEDTLS_SHA256_ALT) || defined(MBEDTLS_MD5_ALT) + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "cmsis.h" + +#if defined(MBEDTLS_THREADING_C) +#include "mbedtls/threading.h" +#endif + +/* macros --------------------------------------------------------------------*/ +/* constants -----------------------------------------------------------------*/ +#define ST_HASH_TIMEOUT ((uint32_t) 1000) /* TO in ms for the hash processor */ + +/* defines -------------------------------------------------------------------*/ +/* variables -----------------------------------------------------------------*/ +#if defined(MBEDTLS_THREADING_C) +extern mbedtls_threading_mutex_t hash_mutex; +extern unsigned char hash_mutex_started; +#endif /* MBEDTLS_THREADING_C */ + +extern unsigned int hash_context_count; + +/* functions prototypes ------------------------------------------------------*/ +extern void hash_zeroize(void *v, size_t n); + +#ifdef __cplusplus +} +#endif + +#endif /* MBEDTLS_SHA1_ALT or MBEDTLS_SHA256_ALT or MBEDTLS_MD5_ALT */ +#endif /*__HASH_H */