Skip to content

Commit e6f4629

Browse files
committed
STM32 MBEDTLS: common STM32 API for ST HASH IP
1 parent aa09e7d commit e6f4629

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
3+
* Copyright (C) 2019-2020, STMicroelectronics, All Rights Reserved
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* This file implements ST shared HASH services based on API from mbed TLS
19+
*/
20+
21+
/* Includes ------------------------------------------------------------------*/
22+
#if !defined(MBEDTLS_CONFIG_FILE)
23+
#include "config.h"
24+
#else
25+
#include MBEDTLS_CONFIG_FILE
26+
#endif
27+
28+
#if defined(MBEDTLS_SHA1_ALT) || defined(MBEDTLS_SHA256_ALT) || defined(MBEDTLS_MD5_ALT)
29+
30+
#include "hash_stm32.h"
31+
32+
/* Variables -----------------------------------------------------------------*/
33+
/* Mutex protection because of one Hash Hw instance is shared over several */
34+
/* algorithms (SHA-1, SHA-256, MD5 implementations may be enabled together) */
35+
#if defined(MBEDTLS_THREADING_C)
36+
mbedtls_threading_mutex_t hash_mutex;
37+
unsigned char hash_mutex_started = 0;
38+
#endif /* MBEDTLS_THREADING_C */
39+
40+
unsigned int hash_context_count = 0;
41+
42+
/* Functions -----------------------------------------------------------------*/
43+
44+
/* Implementation that should never be optimized out by the compiler */
45+
void hash_zeroize(void *v, size_t n)
46+
{
47+
volatile unsigned char *p = (unsigned char *)v;
48+
while (n--) {
49+
*p++ = 0;
50+
}
51+
}
52+
53+
/* HAL function that should be implemented in the user file */
54+
/**
55+
* @brief HASH MSP Initialization
56+
* This function configures the hardware resources used in this example
57+
* @param hhash: HASH handle pointer
58+
* @retval None
59+
*/
60+
void HAL_HASH_MspInit(HASH_HandleTypeDef *hhash)
61+
{
62+
/* Peripheral clock enable */
63+
__HAL_RCC_HASH_CLK_ENABLE();
64+
}
65+
66+
/**
67+
* @brief HASH MSP De-Initialization
68+
* This function freeze the hardware resources used in this example
69+
* @param hhash: HASH handle pointer
70+
* @retval None
71+
*/
72+
void HAL_HASH_MspDeInit(HASH_HandleTypeDef *hhash)
73+
{
74+
/* Peripheral clock disable */
75+
__HAL_RCC_HASH_CLK_DISABLE();
76+
}
77+
78+
#endif /* MBEDTLS_SHA1_ALT or MBEDTLS_SHA256_ALT or MBEDTLS_MD5_ALT */
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
******************************************************************************
3+
* @brief Header file of mbed TLS HW crypto (HASH) implementation.
4+
******************************************************************************
5+
* @attention
6+
*
7+
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
8+
* Copyright (C) 2019 STMicroelectronics, All Rights Reserved
9+
*
10+
* This software component is licensed by ST under Apache 2.0 license,
11+
* the "License"; You may not use this file except in compliance with the
12+
* License. You may obtain a copy of the License at:
13+
* https://opensource.org/licenses/Apache-2.0
14+
*
15+
******************************************************************************
16+
*/
17+
18+
/* Define to prevent recursive inclusion -------------------------------------*/
19+
#ifndef __HASH_H
20+
#define __HASH_H
21+
22+
#if defined(MBEDTLS_SHA1_ALT) || defined(MBEDTLS_SHA256_ALT) || defined(MBEDTLS_MD5_ALT)
23+
24+
#ifdef __cplusplus
25+
extern "C" {
26+
#endif
27+
28+
/* Includes ------------------------------------------------------------------*/
29+
#include "cmsis.h"
30+
31+
#if defined(MBEDTLS_THREADING_C)
32+
#include "mbedtls/threading.h"
33+
#endif
34+
35+
/* macros --------------------------------------------------------------------*/
36+
/* constants -----------------------------------------------------------------*/
37+
#define ST_HASH_TIMEOUT ((uint32_t) 1000) /* TO in ms for the hash processor */
38+
39+
/* defines -------------------------------------------------------------------*/
40+
/* variables -----------------------------------------------------------------*/
41+
#if defined(MBEDTLS_THREADING_C)
42+
extern mbedtls_threading_mutex_t hash_mutex;
43+
extern unsigned char hash_mutex_started;
44+
#endif /* MBEDTLS_THREADING_C */
45+
46+
extern unsigned int hash_context_count;
47+
48+
/* functions prototypes ------------------------------------------------------*/
49+
extern void hash_zeroize(void *v, size_t n);
50+
51+
#ifdef __cplusplus
52+
}
53+
#endif
54+
55+
#endif /* MBEDTLS_SHA1_ALT or MBEDTLS_SHA256_ALT or MBEDTLS_MD5_ALT */
56+
#endif /*__HASH_H */

0 commit comments

Comments
 (0)