Skip to content

Commit f62e384

Browse files
committed
STM32 MBEDTLS: common STM32 API for ST CRYP IP
1 parent e6f4629 commit f62e384

File tree

2 files changed

+203
-0
lines changed

2 files changed

+203
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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 CRYP 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 !(TARGET_STM32L4)
29+
#if defined(MBEDTLS_AES_ALT) || defined(MBEDTLS_CCM_ALT) || defined(MBEDTLS_GCM_ALT)
30+
31+
#include "cryp_stm32.h"
32+
33+
34+
/* Variables -----------------------------------------------------------------*/
35+
/* Mutex protection because of one Crypt Hw instance is shared over several */
36+
/* mode of operations (AES, GCM, CCM implementations may be enabled together) */
37+
#if defined(MBEDTLS_THREADING_C)
38+
mbedtls_threading_mutex_t cryp_mutex;
39+
unsigned char cryp_mutex_started = 0;
40+
#endif /* MBEDTLS_THREADING_C */
41+
42+
unsigned int cryp_context_count = 0;
43+
44+
/* Functions -----------------------------------------------------------------*/
45+
46+
/* Implementation that should never be optimized out by the compiler */
47+
void cryp_zeroize(void *v, size_t n)
48+
{
49+
volatile unsigned char *p = (unsigned char *)v;
50+
while (n--) {
51+
*p++ = 0;
52+
}
53+
}
54+
55+
56+
/* HAL function that should be implemented in the user file */
57+
/**
58+
* @brief CRYP MSP Initialization
59+
* This function configures the hardware resources used in this example:
60+
* - Peripherals clock enable
61+
* @param hcryp: CRYP handle pointer
62+
* @retval None
63+
*/
64+
void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp)
65+
{
66+
#if defined (AES)
67+
/* Enable CRYP clock */
68+
__HAL_RCC_AES_CLK_ENABLE();
69+
70+
/* Force the CRYP Peripheral Clock Reset */
71+
__HAL_RCC_AES_FORCE_RESET();
72+
73+
/* Release the CRYP Peripheral Clock Reset */
74+
__HAL_RCC_AES_RELEASE_RESET();
75+
#elif defined (AES1)
76+
/* Enable CRYP clock */
77+
__HAL_RCC_AES1_CLK_ENABLE();
78+
79+
/* Force the CRYP Peripheral Clock Reset */
80+
__HAL_RCC_AES1_FORCE_RESET();
81+
82+
/* Release the CRYP Peripheral Clock Reset */
83+
__HAL_RCC_AES1_RELEASE_RESET();
84+
#else /* CRYP */
85+
/* Enable CRYP clock */
86+
__HAL_RCC_CRYP_CLK_ENABLE();
87+
88+
/* Force the CRYP Peripheral Clock Reset */
89+
__HAL_RCC_CRYP_FORCE_RESET();
90+
91+
/* Release the CRYP Peripheral Clock Reset */
92+
__HAL_RCC_CRYP_RELEASE_RESET();
93+
#endif /* AES */
94+
}
95+
96+
/**
97+
* @brief CRYP MSP De-Initialization
98+
* This function freeze the hardware resources used in this example:
99+
* - Disable the Peripherals clock
100+
* @param hcryp: CRYP handle pointer
101+
* @retval None
102+
*/
103+
void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp)
104+
{
105+
#if defined (AES)
106+
__HAL_RCC_AES_CLK_DISABLE();
107+
#elif defined (AES1)
108+
__HAL_RCC_AES1_CLK_DISABLE();
109+
#else /* CRYP */
110+
/* Disable CRYP clock */
111+
__HAL_RCC_CRYP_CLK_DISABLE();
112+
#endif /* AES */
113+
}
114+
#endif /* MBEDTLS_AES_ALT or MBEDTLS_CCM_ALT or MBEDTLS_GCM_ALT */
115+
#endif /* ! TARGET_STM32L4 */
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
******************************************************************************
3+
* @brief Header file of mbed TLS HW crypto (CRYP) implementation.
4+
******************************************************************************
5+
* @attention
6+
*
7+
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
8+
* Copyright (C) 2019-2020 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 __CRYP_H
20+
#define __CRYP_H
21+
22+
#if !(TARGET_STM32L4)
23+
#if defined(MBEDTLS_AES_ALT) || defined(MBEDTLS_CCM_ALT) || defined(MBEDTLS_GCM_ALT)
24+
25+
#ifdef __cplusplus
26+
extern "C" {
27+
#endif
28+
29+
/* Includes ------------------------------------------------------------------*/
30+
#include "cmsis.h"
31+
32+
#if defined(MBEDTLS_THREADING_C)
33+
#include "mbedtls/threading.h"
34+
#endif
35+
36+
/* macros --------------------------------------------------------------------*/
37+
/*
38+
* 32-bit integer manipulation macros (big endian)
39+
*/
40+
#ifndef GET_UINT32_BE
41+
#define GET_UINT32_BE(n,b,i) \
42+
do { \
43+
(n) = ( (uint32_t) (b)[(i) ] << 24 ) \
44+
| ( (uint32_t) (b)[(i) + 1] << 16 ) \
45+
| ( (uint32_t) (b)[(i) + 2] << 8 ) \
46+
| ( (uint32_t) (b)[(i) + 3] ); \
47+
} while( 0 )
48+
#endif
49+
50+
#ifndef PUT_UINT32_BE
51+
#define PUT_UINT32_BE(n,b,i) \
52+
do { \
53+
(b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
54+
(b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
55+
(b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
56+
(b)[(i) + 3] = (unsigned char) ( (n) ); \
57+
} while( 0 )
58+
#endif
59+
60+
/* constants -----------------------------------------------------------------*/
61+
#define ST_CRYP_TIMEOUT 1000 /* timeout (in ms) for the crypto processor */
62+
63+
/* defines -------------------------------------------------------------------*/
64+
/* AES 192 bits key length may be optional in the HW */
65+
#if defined CRYP_KEYSIZE_192B
66+
#define USE_AES_KEY192 1
67+
#else
68+
#define USE_AES_KEY192 0
69+
#endif /* USE_AES_KEY192 */
70+
71+
/* variables -----------------------------------------------------------------*/
72+
#if defined(MBEDTLS_THREADING_C)
73+
extern mbedtls_threading_mutex_t cryp_mutex;
74+
extern unsigned char cryp_mutex_started;
75+
#endif /* MBEDTLS_THREADING_C */
76+
77+
extern unsigned int cryp_context_count;
78+
79+
/* functions prototypes ------------------------------------------------------*/
80+
extern void cryp_zeroize(void *v, size_t n);
81+
82+
#ifdef __cplusplus
83+
}
84+
#endif
85+
86+
#endif /* MBEDTLS_AES_ALT or MBEDTLS_CCM_ALT or MBEDTLS_GCM_ALT */
87+
#endif /* ! TARGET_STM32L4 */
88+
#endif /*__CRYP_H */

0 commit comments

Comments
 (0)