Skip to content

Commit 2cd78d1

Browse files
committed
STM32L486RG/mbedtls: add aes hw acceleration
1 parent cc58a7f commit 2cd78d1

File tree

3 files changed

+82
-12
lines changed

3 files changed

+82
-12
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* mbedtls_device.h
3+
*******************************************************************************
4+
* Copyright (c) 2017, STMicroelectronics
5+
* SPDX-License-Identifier: Apache-2.0
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
8+
* not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
#ifndef MBEDTLS_DEVICE_H
21+
#define MBEDTLS_DEVICE_H
22+
23+
#define MBEDTLS_AES_ALT
24+
25+
//the following defines are provided to maintain compatibility between STM32 families
26+
#define __HAL_RCC_CRYP_CLK_ENABLE __HAL_RCC_AES_CLK_ENABLE
27+
#define __HAL_RCC_CRYP_FORCE_RESET __HAL_RCC_AES_FORCE_RESET
28+
#define __HAL_RCC_CRYP_RELEASE_RESET __HAL_RCC_AES_RELEASE_RESET
29+
#define CRYP AES
30+
#endif /* MBEDTLS_DEVICE_H */

features/mbedtls/targets/TARGET_STM/aes_alt.c

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,22 @@ static int aes_set_key( mbedtls_aes_context *ctx, const unsigned char *key, unsi
2828
switch( keybits )
2929
{
3030
case 128:
31-
ctx->hcryp_aes.Init.KeySize = CRYP_KEYSIZE_128B;
32-
memcpy(ctx->aes_key, key, 16);
33-
break;
31+
ctx->hcryp_aes.Init.KeySize = CRYP_KEYSIZE_128B;
32+
memcpy(ctx->aes_key, key, 16);
33+
break;
3434
case 192:
35-
ctx->hcryp_aes.Init.KeySize = CRYP_KEYSIZE_192B;
36-
memcpy(ctx->aes_key, key, 24);
37-
break;
35+
#if defined (TARGET_STM32L486xG)
36+
return(MBEDTLS_ERR_AES_INVALID_KEY_LENGTH);
37+
#else
38+
ctx->hcryp_aes.Init.KeySize = CRYP_KEYSIZE_192B;
39+
memcpy(ctx->aes_key, key, 24);
40+
break;
41+
#endif
42+
3843
case 256:
39-
ctx->hcryp_aes.Init.KeySize = CRYP_KEYSIZE_256B;
40-
memcpy(ctx->aes_key, key, 32);
41-
break;
44+
ctx->hcryp_aes.Init.KeySize = CRYP_KEYSIZE_256B;
45+
memcpy(ctx->aes_key, key, 32);
46+
break;
4247
default : return( MBEDTLS_ERR_AES_INVALID_KEY_LENGTH );
4348
}
4449

@@ -52,6 +57,9 @@ static int aes_set_key( mbedtls_aes_context *ctx, const unsigned char *key, unsi
5257
__HAL_RCC_CRYP_CLK_ENABLE();
5358

5459
ctx->hcryp_aes.Init.pKey = ctx->aes_key;
60+
#if defined (TARGET_STM32L486xG)
61+
ctx->hcryp_aes.Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
62+
#endif
5563
if (HAL_CRYP_Init(&ctx->hcryp_aes) == HAL_ERROR)
5664
return (HAL_ERROR);
5765

@@ -148,14 +156,46 @@ int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
148156
if( mode == MBEDTLS_AES_DECRYPT )
149157
{
150158
ctx->hcryp_aes.Init.pInitVect = &iv[0]; // used in process, not in the init
151-
159+
#if defined (TARGET_STM32L486xG)
160+
if ((ctx->hcryp_aes.Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION_DECRYPT) || \
161+
(ctx->hcryp_aes.Init.ChainingMode != CRYP_CHAINMODE_AES_CBC) || \
162+
(ctx->hcryp_aes.Init.KeyWriteFlag != CRYP_KEY_WRITE_ENABLE)) {
163+
/* Re-initialize AES IP with proper parameters */
164+
if (HAL_CRYP_DeInit(&ctx->hcryp_aes) != HAL_OK)
165+
return HAL_ERROR;
166+
ctx->hcryp_aes.Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
167+
ctx->hcryp_aes.Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
168+
ctx->hcryp_aes.Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
169+
if (HAL_CRYP_Init(&ctx->hcryp_aes) != HAL_OK)
170+
return HAL_ERROR;
171+
}
172+
173+
status = HAL_CRYPEx_AES(&ctx->hcryp_aes, (uint8_t *)input, length, (uint8_t *)output, 10);
174+
#else
152175
status = HAL_CRYP_AESCBC_Decrypt(&ctx->hcryp_aes, (uint8_t *)input, length, (uint8_t *)output, 10);
176+
#endif
153177
}
154178
else
155179
{
156180
ctx->hcryp_aes.Init.pInitVect = &iv[0]; // used in process, not in the init
157-
181+
#if defined (TARGET_STM32L486xG)
182+
if ((ctx->hcryp_aes.Init.OperatingMode != CRYP_ALGOMODE_ENCRYPT) || \
183+
(ctx->hcryp_aes.Init.ChainingMode != CRYP_CHAINMODE_AES_CBC) || \
184+
(ctx->hcryp_aes.Init.KeyWriteFlag != CRYP_KEY_WRITE_ENABLE)) {
185+
/* Re-initialize AES IP with proper parameters */
186+
if (HAL_CRYP_DeInit(&ctx->hcryp_aes) != HAL_OK)
187+
return HAL_ERROR;
188+
ctx->hcryp_aes.Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
189+
ctx->hcryp_aes.Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
190+
ctx->hcryp_aes.Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
191+
if (HAL_CRYP_Init(&ctx->hcryp_aes) != HAL_OK)
192+
return HAL_ERROR;
193+
}
194+
195+
status = HAL_CRYPEx_AES(&ctx->hcryp_aes, (uint8_t *)input, length, (uint8_t *)output, 10);
196+
#else
158197
status = HAL_CRYP_AESCBC_Encrypt(&ctx->hcryp_aes, (uint8_t *)input, length, (uint8_t *)output, 10);
198+
#endif
159199
}
160200
return( status );
161201
}

targets/targets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,7 @@
10871087
"supported_toolchains": ["ARM", "uARM", "IAR", "GCC_ARM"],
10881088
"inherits": ["Target"],
10891089
"detect_code": ["0827"],
1090-
"macros": ["TRANSACTION_QUEUE_SIZE_SPI=2","USBHOST_OTHER"],
1090+
"macros": ["TRANSACTION_QUEUE_SIZE_SPI=2","USBHOST_OTHER", "MBEDTLS_CONFIG_HW_SUPPORT"],
10911091
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES", "TRNG"],
10921092
"release_versions": ["2", "5"],
10931093
"device_name": "STM32L486RG"

0 commit comments

Comments
 (0)