Skip to content

Commit e2c96e9

Browse files
committed
Move definitions in aes_alt.c + Factorize code
1 parent d19c9ea commit e2c96e9

File tree

2 files changed

+54
-65
lines changed

2 files changed

+54
-65
lines changed

features/mbedtls/targets/TARGET_STM/TARGET_STM32L4/TARGET_NUCLEO_L486RG/mbedtls_device.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,4 @@
2222

2323
#define MBEDTLS_AES_ALT
2424

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
3025
#endif /* MBEDTLS_DEVICE_H */

features/mbedtls/targets/TARGET_STM/aes_alt.c

Lines changed: 54 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,17 @@
2323

2424
#if defined(MBEDTLS_AES_ALT)
2525

26+
#if defined(TARGET_STM32L486xG)
27+
//the following defines are provided to maintain compatibility between STM32 families
28+
#define __HAL_RCC_CRYP_CLK_ENABLE __HAL_RCC_AES_CLK_ENABLE
29+
#define __HAL_RCC_CRYP_FORCE_RESET __HAL_RCC_AES_FORCE_RESET
30+
#define __HAL_RCC_CRYP_RELEASE_RESET __HAL_RCC_AES_RELEASE_RESET
31+
#define CRYP AES
32+
#endif
33+
2634
static int aes_set_key( mbedtls_aes_context *ctx, const unsigned char *key, unsigned int keybits )
2735
{
28-
switch( keybits )
29-
{
36+
switch( keybits ) {
3037
case 128:
3138
ctx->hcryp_aes.Init.KeySize = CRYP_KEYSIZE_128B;
3239
memcpy(ctx->aes_key, key, 16);
@@ -70,7 +77,8 @@ static int aes_set_key( mbedtls_aes_context *ctx, const unsigned char *key, unsi
7077
}
7178

7279
/* Implementation that should never be optimized out by the compiler */
73-
static void mbedtls_zeroize( void *v, size_t n ) {
80+
static void mbedtls_zeroize( void *v, size_t n )
81+
{
7482
volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
7583
}
7684

@@ -122,14 +130,11 @@ int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
122130
/* allow multi-instance of CRYP use: restore context for CRYP hw module */
123131
ctx->hcryp_aes.Instance->CR = ctx->ctx_save_cr;
124132

125-
if(mode == MBEDTLS_AES_DECRYPT) /* AES decryption */
126-
{
133+
if(mode == MBEDTLS_AES_DECRYPT) { /* AES decryption */
127134
ctx->hcryp_aes.Init.DataType = CRYP_DATATYPE_8B;
128135
ctx->hcryp_aes.Init.pKey = ctx->aes_key;
129136
mbedtls_aes_decrypt( ctx, input, output );
130-
}
131-
else /* AES encryption */
132-
{
137+
} else { /* AES encryption */
133138
ctx->hcryp_aes.Init.DataType = CRYP_DATATYPE_8B;
134139
ctx->hcryp_aes.Init.pKey = ctx->aes_key;
135140
mbedtls_aes_encrypt( ctx, input, output );
@@ -141,6 +146,31 @@ int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
141146
}
142147

143148
#if defined(MBEDTLS_CIPHER_MODE_CBC)
149+
#if defined (TARGET_STM32L486xG)
150+
static int st_hal_cryp_cbc( mbedtls_aes_context *ctx, uint32_t opmode, size_t length,
151+
unsigned char iv[16], uint8_t *input, uint8_t *output)
152+
{
153+
int status = 0;
154+
ctx->hcryp_aes.Init.pInitVect = &iv[0]; // used in process, not in the init
155+
if ((ctx->hcryp_aes.Init.OperatingMode != opmode) || \
156+
(ctx->hcryp_aes.Init.ChainingMode != CRYP_CHAINMODE_AES_CBC) || \
157+
(ctx->hcryp_aes.Init.KeyWriteFlag != CRYP_KEY_WRITE_ENABLE)) {
158+
159+
/* Re-initialize AES IP with proper parameters */
160+
if (HAL_CRYP_DeInit(&ctx->hcryp_aes) != HAL_OK)
161+
return HAL_ERROR;
162+
ctx->hcryp_aes.Init.OperatingMode = opmode;
163+
ctx->hcryp_aes.Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
164+
ctx->hcryp_aes.Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
165+
if (HAL_CRYP_Init(&ctx->hcryp_aes) != HAL_OK)
166+
return HAL_ERROR;
167+
}
168+
169+
status = HAL_CRYPEx_AES(&ctx->hcryp_aes, input, length, output, 10);
170+
171+
return status;
172+
}
173+
#endif /* TARGET_STM32L486xG */
144174

145175
int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
146176
int mode,
@@ -149,54 +179,24 @@ int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
149179
const unsigned char *input,
150180
unsigned char *output )
151181
{
152-
int status=0;
182+
int status = 0;
153183
if( length % 16 )
154184
return( MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
155-
156-
if( mode == MBEDTLS_AES_DECRYPT )
157-
{
158-
ctx->hcryp_aes.Init.pInitVect = &iv[0]; // used in process, not in the init
159185
#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
175-
status = HAL_CRYP_AESCBC_Decrypt(&ctx->hcryp_aes, (uint8_t *)input, length, (uint8_t *)output, 10);
176-
#endif
186+
if( mode == MBEDTLS_AES_DECRYPT ) {
187+
status = st_hal_cryp_cbc(ctx, CRYP_ALGOMODE_KEYDERIVATION_DECRYPT, length, iv, (uint8_t *)input, (uint8_t *)output);
188+
} else {
189+
status = st_hal_cryp_cbc(ctx, CRYP_ALGOMODE_ENCRYPT, length, iv, (uint8_t *)input, (uint8_t *)output);
177190
}
178-
else
179-
{
180-
ctx->hcryp_aes.Init.pInitVect = &iv[0]; // used in process, not in the init
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);
196191
#else
192+
ctx->hcryp_aes.Init.pInitVect = &iv[0];
193+
194+
if( mode == MBEDTLS_AES_DECRYPT ) {
195+
status = HAL_CRYP_AESCBC_Decrypt(&ctx->hcryp_aes, (uint8_t *)input, length, (uint8_t *)output, 10);
196+
} else {
197197
status = HAL_CRYP_AESCBC_Encrypt(&ctx->hcryp_aes, (uint8_t *)input, length, (uint8_t *)output, 10);
198-
#endif
199198
}
199+
#endif
200200
return( status );
201201
}
202202
#endif /* MBEDTLS_CIPHER_MODE_CBC */
@@ -213,10 +213,8 @@ int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
213213
int c;
214214
size_t n = *iv_off;
215215

216-
if( mode == MBEDTLS_AES_DECRYPT )
217-
{
218-
while( length-- )
219-
{
216+
if( mode == MBEDTLS_AES_DECRYPT ) {
217+
while( length-- ) {
220218
if( n == 0 )
221219
mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv );
222220

@@ -226,11 +224,8 @@ int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
226224

227225
n = ( n + 1 ) & 0x0F;
228226
}
229-
}
230-
else
231-
{
232-
while( length-- )
233-
{
227+
} else {
228+
while( length-- ) {
234229
if( n == 0 )
235230
mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv );
236231

@@ -256,8 +251,7 @@ int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
256251
unsigned char c;
257252
unsigned char ov[17];
258253

259-
while( length-- )
260-
{
254+
while( length-- ) {
261255
memcpy( ov, iv, 16 );
262256
mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv );
263257

0 commit comments

Comments
 (0)