Skip to content

Commit 43aa6a2

Browse files
Merge pull request #5018 from adustm/f439_aes_tlsclient_fix
Fix use of AES_ALT on STM32F439 for example-tls-client
2 parents b494d33 + e6fa5f0 commit 43aa6a2

File tree

3 files changed

+183
-56
lines changed

3 files changed

+183
-56
lines changed

features/mbedtls/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/mbedtls_device.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
#ifndef MBEDTLS_DEVICE_H
2121
#define MBEDTLS_DEVICE_H
2222

23-
/* FIXME: Don't enable AES hardware acceleration until issue #4928 is fixed.
24-
* (https://github.com/ARMmbed/mbed-os/issues/4928) */
25-
/* #define MBEDTLS_AES_ALT */
23+
#define MBEDTLS_AES_ALT
2624

2725
#define MBEDTLS_SHA256_ALT
2826

features/mbedtls/targets/TARGET_STM/aes_alt.c

Lines changed: 131 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Hardware aes collector for the STM32F4 family
2+
* Hardware aes implementation for STM32F4 STM32F7 and STM32L4 families
33
*******************************************************************************
44
* Copyright (c) 2017, STMicroelectronics
55
* SPDX-License-Identifier: Apache-2.0
@@ -129,15 +129,18 @@ int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
129129

130130
/* allow multi-instance of CRYP use: restore context for CRYP hw module */
131131
ctx->hcryp_aes.Instance->CR = ctx->ctx_save_cr;
132+
ctx->hcryp_aes.Phase = HAL_CRYP_PHASE_READY;
133+
ctx->hcryp_aes.Init.DataType = CRYP_DATATYPE_8B;
134+
ctx->hcryp_aes.Init.pKey = ctx->aes_key;
132135

133136
if(mode == MBEDTLS_AES_DECRYPT) { /* AES decryption */
134-
ctx->hcryp_aes.Init.DataType = CRYP_DATATYPE_8B;
135-
ctx->hcryp_aes.Init.pKey = ctx->aes_key;
136-
mbedtls_aes_decrypt( ctx, input, output );
137+
if (mbedtls_internal_aes_decrypt( ctx, input, output )){
138+
return ST_ERR_AES_BUSY;
139+
}
137140
} else { /* AES encryption */
138-
ctx->hcryp_aes.Init.DataType = CRYP_DATATYPE_8B;
139-
ctx->hcryp_aes.Init.pKey = ctx->aes_key;
140-
mbedtls_aes_encrypt( ctx, input, output );
141+
if (mbedtls_internal_aes_encrypt( ctx, input, output )) {
142+
return ST_ERR_AES_BUSY;
143+
}
141144
}
142145
/* allow multi-instance of CRYP use: save context for CRYP HW module CR */
143146
ctx->ctx_save_cr = ctx->hcryp_aes.Instance->CR;
@@ -147,29 +150,50 @@ int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
147150

148151
#if defined(MBEDTLS_CIPHER_MODE_CBC)
149152
#if defined (TARGET_STM32L486xG)
153+
static int st_cbc_restore_context(mbedtls_aes_context *ctx){
154+
uint32_t tickstart;
155+
tickstart = HAL_GetTick();
156+
while((ctx->hcryp_aes.Instance->SR & AES_SR_BUSY) != 0){
157+
if ((HAL_GetTick() - tickstart) > ST_AES_TIMEOUT) {
158+
return ST_ERR_AES_BUSY; // timeout: CRYP processor is busy
159+
}
160+
}
161+
/* allow multi-instance of CRYP use: restore context for CRYP hw module */
162+
ctx->hcryp_aes.Instance->CR = ctx->ctx_save_cr;
163+
return 0;
164+
}
165+
150166
static int st_hal_cryp_cbc( mbedtls_aes_context *ctx, uint32_t opmode, size_t length,
151167
unsigned char iv[16], uint8_t *input, uint8_t *output)
152168
{
153-
int status = 0;
154169
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+
/* At this moment only, we know we have CBC mode: Re-initialize AES
171+
IP with proper parameters and apply key and IV for multi context usecase */
172+
if (HAL_CRYP_DeInit(&ctx->hcryp_aes) != HAL_OK)
173+
return ST_ERR_AES_BUSY;
174+
ctx->hcryp_aes.Init.OperatingMode = opmode;
175+
ctx->hcryp_aes.Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
176+
ctx->hcryp_aes.Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
177+
if (HAL_CRYP_Init(&ctx->hcryp_aes) != HAL_OK)
178+
return ST_ERR_AES_BUSY;
170179

171-
return status;
180+
if(HAL_CRYPEx_AES(&ctx->hcryp_aes, input, length, output, 10) != 0)
181+
return ST_ERR_AES_BUSY;
182+
return 0;
183+
}
184+
#else /* STM32F4 and STM32F7 */
185+
static int st_cbc_restore_context(mbedtls_aes_context *ctx){
186+
/* allow multi-instance of CRYP use: restore context for CRYP hw module */
187+
ctx->hcryp_aes.Instance->CR = ctx->ctx_save_cr;
188+
/* Re-initialize AES processor with proper parameters
189+
and (re-)apply key and IV for multi context usecases */
190+
if (HAL_CRYP_DeInit(&ctx->hcryp_aes) != HAL_OK)
191+
return ST_ERR_AES_BUSY;
192+
if (HAL_CRYP_Init(&ctx->hcryp_aes) != HAL_OK)
193+
return ST_ERR_AES_BUSY;
194+
return 0;
172195
}
196+
173197
#endif /* TARGET_STM32L486xG */
174198

175199
int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
@@ -179,25 +203,66 @@ int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
179203
const unsigned char *input,
180204
unsigned char *output )
181205
{
182-
int status = 0;
206+
uint32_t tickstart;
207+
uint32_t *iv_ptr = (uint32_t *)&iv[0];
183208
if( length % 16 )
184209
return( MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
210+
ctx->hcryp_aes.Init.pInitVect = &iv[0];
211+
if (st_cbc_restore_context(ctx) != 0)
212+
return (ST_ERR_AES_BUSY);
213+
185214
#if defined (TARGET_STM32L486xG)
215+
186216
if( mode == MBEDTLS_AES_DECRYPT ) {
187-
status = st_hal_cryp_cbc(ctx, CRYP_ALGOMODE_KEYDERIVATION_DECRYPT, length, iv, (uint8_t *)input, (uint8_t *)output);
217+
if (st_hal_cryp_cbc(ctx, CRYP_ALGOMODE_KEYDERIVATION_DECRYPT, length, iv, (uint8_t *)input, (uint8_t *)output) != 0)
218+
return ST_ERR_AES_BUSY;
219+
/* Save the internal IV vector for multi context purpose */
220+
tickstart = HAL_GetTick();
221+
while((ctx->hcryp_aes.Instance->SR & AES_SR_BUSY) != 0){
222+
if ((HAL_GetTick() - tickstart) > ST_AES_TIMEOUT) {
223+
return ST_ERR_AES_BUSY; // timeout: CRYP processor is busy
224+
}
225+
}
226+
ctx->ctx_save_cr = ctx->hcryp_aes.Instance->CR; // save here before overwritten
227+
ctx->hcryp_aes.Instance->CR &= ~AES_CR_EN;
228+
*iv_ptr++ = ctx->hcryp_aes.Instance->IVR3;
229+
*iv_ptr++ = ctx->hcryp_aes.Instance->IVR2;
230+
*iv_ptr++ = ctx->hcryp_aes.Instance->IVR1;
231+
*iv_ptr++ = ctx->hcryp_aes.Instance->IVR0;
188232
} else {
189-
status = st_hal_cryp_cbc(ctx, CRYP_ALGOMODE_ENCRYPT, length, iv, (uint8_t *)input, (uint8_t *)output);
233+
if (st_hal_cryp_cbc(ctx, CRYP_ALGOMODE_ENCRYPT, length, iv, (uint8_t *)input, (uint8_t *)output) != 0)
234+
return ST_ERR_AES_BUSY;
235+
memcpy( iv, output, 16 ); /* current output is the IV vector for the next call */
236+
ctx->ctx_save_cr = ctx->hcryp_aes.Instance->CR;
190237
}
238+
191239
#else
192-
ctx->hcryp_aes.Init.pInitVect = &iv[0];
193-
240+
194241
if( mode == MBEDTLS_AES_DECRYPT ) {
195-
status = HAL_CRYP_AESCBC_Decrypt(&ctx->hcryp_aes, (uint8_t *)input, length, (uint8_t *)output, 10);
242+
if (HAL_CRYP_AESCBC_Decrypt(&ctx->hcryp_aes, (uint8_t *)input, length, (uint8_t *)output, 10) != HAL_OK)
243+
return ST_ERR_AES_BUSY;
244+
/* Save the internal IV vector for multi context purpose */
245+
tickstart = HAL_GetTick();
246+
while((ctx->hcryp_aes.Instance->SR & (CRYP_SR_IFEM | CRYP_SR_OFNE | CRYP_SR_BUSY)) != CRYP_SR_IFEM){
247+
if ((HAL_GetTick() - tickstart) > ST_AES_TIMEOUT) {
248+
return ST_ERR_AES_BUSY; // timeout: CRYP processor is busy
249+
}
250+
}
251+
ctx->ctx_save_cr = ctx->hcryp_aes.Instance->CR; // save here before overwritten
252+
ctx->hcryp_aes.Instance->CR &= ~CRYP_CR_CRYPEN;
253+
*iv_ptr++ = ctx->hcryp_aes.Instance->IV0LR;
254+
*iv_ptr++ = ctx->hcryp_aes.Instance->IV0RR;
255+
*iv_ptr++ = ctx->hcryp_aes.Instance->IV1LR;
256+
*iv_ptr++ = ctx->hcryp_aes.Instance->IV1RR;
196257
} else {
197-
status = HAL_CRYP_AESCBC_Encrypt(&ctx->hcryp_aes, (uint8_t *)input, length, (uint8_t *)output, 10);
258+
if (HAL_CRYP_AESCBC_Encrypt(&ctx->hcryp_aes, (uint8_t *)input, length, (uint8_t *)output, 10) != HAL_OK)
259+
return ST_ERR_AES_BUSY;
260+
memcpy( iv, output, 16 ); /* current output is the IV vector for the next call */
261+
ctx->ctx_save_cr = ctx->hcryp_aes.Instance->CR;
198262
}
263+
199264
#endif
200-
return( status );
265+
return 0;
201266
}
202267
#endif /* MBEDTLS_CIPHER_MODE_CBC */
203268

@@ -216,7 +281,8 @@ int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
216281
if( mode == MBEDTLS_AES_DECRYPT ) {
217282
while( length-- ) {
218283
if( n == 0 )
219-
mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv );
284+
if (mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv ) != 0)
285+
return ST_ERR_AES_BUSY;
220286

221287
c = *input++;
222288
*output++ = (unsigned char)( c ^ iv[n] );
@@ -227,7 +293,8 @@ int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
227293
} else {
228294
while( length-- ) {
229295
if( n == 0 )
230-
mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv );
296+
if (mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv ) != 0)
297+
return ST_ERR_AES_BUSY;
231298

232299
iv[n] = *output++ = (unsigned char)( iv[n] ^ *input++ );
233300

@@ -253,7 +320,8 @@ int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
253320

254321
while( length-- ) {
255322
memcpy( ov, iv, 16 );
256-
mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv );
323+
if (mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv ) != 0)
324+
return ST_ERR_AES_BUSY;
257325

258326
if( mode == MBEDTLS_AES_DECRYPT )
259327
ov[16] = *input;
@@ -286,7 +354,8 @@ int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
286354
while( length-- )
287355
{
288356
if( n == 0 ) {
289-
mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, nonce_counter, stream_block );
357+
if (mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, nonce_counter, stream_block ) != 0)
358+
return ST_ERR_AES_BUSY;
290359

291360
for( i = 16; i > 0; i-- )
292361
if( ++nonce_counter[i - 1] != 0 )
@@ -304,26 +373,42 @@ int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
304373
}
305374
#endif /* MBEDTLS_CIPHER_MODE_CTR */
306375

307-
void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
376+
int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
308377
const unsigned char input[16],
309378
unsigned char output[16] )
310379
{
311-
312-
if (HAL_CRYP_AESECB_Encrypt(&ctx->hcryp_aes, (uint8_t *)input, 16, (uint8_t *)output, 10) !=0) {
313-
// error found to be returned
380+
if (HAL_CRYP_AESECB_Encrypt(&ctx->hcryp_aes, (uint8_t *)input, 16, (uint8_t *)output, 10) != HAL_OK) {
381+
// error found
382+
return ST_ERR_AES_BUSY;
314383
}
384+
return 0;
315385

316386
}
317387

318-
void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
319-
const unsigned char input[16],
320-
unsigned char output[16] )
388+
int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
389+
const unsigned char input[16],
390+
unsigned char output[16] )
321391
{
322-
323-
if(HAL_CRYP_AESECB_Decrypt(&ctx->hcryp_aes, (uint8_t *)input, 16, (uint8_t *)output, 10)) {
324-
// error found to be returned
392+
if(HAL_CRYP_AESECB_Decrypt(&ctx->hcryp_aes, (uint8_t *)input, 16, (uint8_t *)output, 10) != HAL_OK) {
393+
// error found
394+
return ST_ERR_AES_BUSY;
325395
}
396+
return 0;
326397
}
327398

399+
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
400+
void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
401+
const unsigned char input[16],
402+
unsigned char output[16] )
403+
{
404+
mbedtls_internal_aes_encrypt( ctx, input, output );
405+
}
328406

407+
void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
408+
const unsigned char input[16],
409+
unsigned char output[16] )
410+
{
411+
mbedtls_internal_aes_decrypt( ctx, input, output );
412+
}
413+
#endif /* MBEDTLS_DEPRECATED_REMOVED */
329414
#endif /*MBEDTLS_AES_ALT*/

features/mbedtls/targets/TARGET_STM/aes_alt.h

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* aes_alt.h AES block cipher
33
*******************************************************************************
4-
* Copyright (c) 2016, STMicroelectronics
4+
* Copyright (c) 2017, STMicroelectronics
55
* SPDX-License-Identifier: Apache-2.0
66
*
77
* Licensed under the Apache License, Version 2.0 (the "License"); you may
@@ -30,6 +30,9 @@
3030
#ifdef __cplusplus
3131
extern "C" {
3232
#endif
33+
34+
#define ST_AES_TIMEOUT ((uint32_t) 0xFF) /* 255 ms timeout for the crypto processor */
35+
#define ST_ERR_AES_BUSY (-0x0023) /* Crypto processor is busy, timeout occured */
3336
/**
3437
* \brief AES context structure
3538
*
@@ -236,10 +239,12 @@ int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
236239
* \param ctx AES context
237240
* \param input Plaintext block
238241
* \param output Output (ciphertext) block
242+
*
243+
* \return 0 if successful
239244
*/
240-
void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
241-
const unsigned char input[16],
242-
unsigned char output[16] );
245+
int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
246+
const unsigned char input[16],
247+
unsigned char output[16] );
243248

244249
/**
245250
* \brief Internal AES block decryption function
@@ -249,10 +254,49 @@ void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
249254
* \param ctx AES context
250255
* \param input Ciphertext block
251256
* \param output Output (plaintext) block
257+
*
258+
* \return 0 if successful
252259
*/
253-
void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
254-
const unsigned char input[16],
255-
unsigned char output[16] );
260+
int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
261+
const unsigned char input[16],
262+
unsigned char output[16] );
263+
264+
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
265+
#if defined(MBEDTLS_DEPRECATED_WARNING)
266+
#define MBEDTLS_DEPRECATED __attribute__((deprecated))
267+
#else
268+
#define MBEDTLS_DEPRECATED
269+
#endif
270+
/**
271+
* \brief Deprecated internal AES block encryption function
272+
* without return value.
273+
*
274+
* \deprecated Superseded by mbedtls_aes_encrypt_ext() in 2.5.0
275+
*
276+
* \param ctx AES context
277+
* \param input Plaintext block
278+
* \param output Output (ciphertext) block
279+
*/
280+
MBEDTLS_DEPRECATED void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
281+
const unsigned char input[16],
282+
unsigned char output[16] );
283+
284+
/**
285+
* \brief Deprecated internal AES block decryption function
286+
* without return value.
287+
*
288+
* \deprecated Superseded by mbedtls_aes_decrypt_ext() in 2.5.0
289+
*
290+
* \param ctx AES context
291+
* \param input Ciphertext block
292+
* \param output Output (plaintext) block
293+
*/
294+
MBEDTLS_DEPRECATED void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
295+
const unsigned char input[16],
296+
unsigned char output[16] );
297+
298+
#undef MBEDTLS_DEPRECATED
299+
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
256300

257301
#ifdef __cplusplus
258302
}

0 commit comments

Comments
 (0)