Skip to content

Commit e6fa5f0

Browse files
committed
Change after code review : standardize calls among ST families
Check return values in alignment with MBEDTLS error codes
1 parent cd1a18f commit e6fa5f0

File tree

2 files changed

+72
-87
lines changed

2 files changed

+72
-87
lines changed

features/mbedtls/targets/TARGET_STM/aes_alt.c

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

136136
if(mode == MBEDTLS_AES_DECRYPT) { /* AES decryption */
137137
if (mbedtls_internal_aes_decrypt( ctx, input, output )){
138-
return 1;
138+
return ST_ERR_AES_BUSY;
139139
}
140140
} else { /* AES encryption */
141141
if (mbedtls_internal_aes_encrypt( ctx, input, output )) {
142-
return 1;
142+
return ST_ERR_AES_BUSY;
143143
}
144144
}
145145
/* allow multi-instance of CRYP use: save context for CRYP HW module CR */
@@ -155,134 +155,114 @@ static int st_cbc_restore_context(mbedtls_aes_context *ctx){
155155
tickstart = HAL_GetTick();
156156
while((ctx->hcryp_aes.Instance->SR & AES_SR_BUSY) != 0){
157157
if ((HAL_GetTick() - tickstart) > ST_AES_TIMEOUT) {
158-
return 1; // timeout: CRYP processor is busy
158+
return ST_ERR_AES_BUSY; // timeout: CRYP processor is busy
159159
}
160160
}
161161
/* allow multi-instance of CRYP use: restore context for CRYP hw module */
162162
ctx->hcryp_aes.Instance->CR = ctx->ctx_save_cr;
163163
return 0;
164164
}
165-
static int st_cbc_save_context(mbedtls_aes_context *ctx){
166-
uint32_t tickstart;
167-
168-
tickstart = HAL_GetTick();
169-
while((ctx->hcryp_aes.Instance->SR & AES_SR_BUSY) != 0){
170-
if ((HAL_GetTick() - tickstart) > ST_AES_TIMEOUT) {
171-
return 1; // timeout: CRYP processor is busy
172-
}
173-
}
174-
/* allow multi-instance of CRYP use: save context for CRYP HW module CR */
175-
ctx->ctx_save_cr = ctx->hcryp_aes.Instance->CR;
176165

177-
return 0;
178-
}
179166
static int st_hal_cryp_cbc( mbedtls_aes_context *ctx, uint32_t opmode, size_t length,
180167
unsigned char iv[16], uint8_t *input, uint8_t *output)
181168
{
182-
int status = 0;
183169
ctx->hcryp_aes.Init.pInitVect = &iv[0]; // used in process, not in the init
184170
/* At this moment only, we know we have CBC mode: Re-initialize AES
185171
IP with proper parameters and apply key and IV for multi context usecase */
186172
if (HAL_CRYP_DeInit(&ctx->hcryp_aes) != HAL_OK)
187-
return HAL_ERROR;
173+
return ST_ERR_AES_BUSY;
188174
ctx->hcryp_aes.Init.OperatingMode = opmode;
189175
ctx->hcryp_aes.Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
190176
ctx->hcryp_aes.Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
191177
if (HAL_CRYP_Init(&ctx->hcryp_aes) != HAL_OK)
192-
return HAL_ERROR;
178+
return ST_ERR_AES_BUSY;
193179

194-
status = HAL_CRYPEx_AES(&ctx->hcryp_aes, input, length, output, 10);
195-
196-
return status;
180+
if(HAL_CRYPEx_AES(&ctx->hcryp_aes, input, length, output, 10) != 0)
181+
return ST_ERR_AES_BUSY;
182+
return 0;
197183
}
198-
#else
184+
#else /* STM32F4 and STM32F7 */
199185
static int st_cbc_restore_context(mbedtls_aes_context *ctx){
200-
uint32_t tickstart;
201-
tickstart = HAL_GetTick();
202-
while((ctx->hcryp_aes.Instance->SR & (CRYP_SR_IFEM | CRYP_SR_OFNE | CRYP_SR_BUSY)) != CRYP_SR_IFEM){
203-
if ((HAL_GetTick() - tickstart) > ST_AES_TIMEOUT) {
204-
return 1; // timeout: CRYP processor is busy
205-
}
206-
}
207-
ctx->hcryp_aes.Instance->CR &= ~CRYP_CR_CRYPEN;
208-
/* save initvector for multi-instance use of CRYP */
209-
ctx->hcryp_aes.Instance->IV1RR = ctx->save_iv[3];
210-
ctx->hcryp_aes.Instance->IV1LR = ctx->save_iv[2];
211-
ctx->hcryp_aes.Instance->IV0RR = ctx->save_iv[1];
212-
ctx->hcryp_aes.Instance->IV0LR = ctx->save_iv[0];
213-
ctx->hcryp_aes.Phase = HAL_CRYP_PHASE_READY;
214186
/* allow multi-instance of CRYP use: restore context for CRYP hw module */
215187
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;
216194
return 0;
217195
}
218-
static int st_cbc_save_context(mbedtls_aes_context *ctx){
219-
uint32_t tickstart;
220-
tickstart = HAL_GetTick();
221-
while((ctx->hcryp_aes.Instance->SR & (CRYP_SR_IFEM | CRYP_SR_OFNE | CRYP_SR_BUSY)) != CRYP_SR_IFEM){
222-
if ((HAL_GetTick() - tickstart) > ST_AES_TIMEOUT) {
223-
return 1; // timeout: CRYP processor is busy
224-
}
225-
}
226-
/* allow multi-instance of CRYP use: save context for CRYP HW module CR */
227-
ctx->ctx_save_cr = ctx->hcryp_aes.Instance->CR;
228-
ctx->hcryp_aes.Instance->CR &= ~CRYP_CR_CRYPEN;
229-
/* save initvector for multi-instance use of CRYP */
230-
ctx->save_iv[3] = ctx->hcryp_aes.Instance->IV1RR;
231-
ctx->save_iv[2] = ctx->hcryp_aes.Instance->IV1LR;
232-
ctx->save_iv[1] = ctx->hcryp_aes.Instance->IV0RR;
233-
ctx->save_iv[0] = ctx->hcryp_aes.Instance->IV0LR;
234-
if ((ctx->ctx_save_cr & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN) {
235-
ctx->hcryp_aes.Instance->CR &= CRYP_CR_CRYPEN;
236-
}
237-
return 0;
238-
}
196+
239197
#endif /* TARGET_STM32L486xG */
198+
240199
int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
241200
int mode,
242201
size_t length,
243202
unsigned char iv[16],
244203
const unsigned char *input,
245204
unsigned char *output )
246205
{
247-
int status = 0;
206+
uint32_t tickstart;
207+
uint32_t *iv_ptr = (uint32_t *)&iv[0];
248208
if( length % 16 )
249209
return( MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
250210
ctx->hcryp_aes.Init.pInitVect = &iv[0];
251-
status |= st_cbc_restore_context(ctx);
211+
if (st_cbc_restore_context(ctx) != 0)
212+
return (ST_ERR_AES_BUSY);
213+
252214
#if defined (TARGET_STM32L486xG)
253-
uint32_t *iv_ptr = (uint32_t *)&iv[0];
215+
254216
if( mode == MBEDTLS_AES_DECRYPT ) {
255-
status |= st_hal_cryp_cbc(ctx, CRYP_ALGOMODE_KEYDERIVATION_DECRYPT, length, iv, (uint8_t *)input, (uint8_t *)output);
256-
// update IV
257-
uint32_t tickstart;
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 */
258220
tickstart = HAL_GetTick();
259221
while((ctx->hcryp_aes.Instance->SR & AES_SR_BUSY) != 0){
260222
if ((HAL_GetTick() - tickstart) > ST_AES_TIMEOUT) {
261-
return 1; // timeout: CRYP processor is busy
223+
return ST_ERR_AES_BUSY; // timeout: CRYP processor is busy
262224
}
263225
}
264-
ctx->ctx_save_cr = ctx->hcryp_aes.Instance->CR;
226+
ctx->ctx_save_cr = ctx->hcryp_aes.Instance->CR; // save here before overwritten
265227
ctx->hcryp_aes.Instance->CR &= ~AES_CR_EN;
266228
*iv_ptr++ = ctx->hcryp_aes.Instance->IVR3;
267229
*iv_ptr++ = ctx->hcryp_aes.Instance->IVR2;
268230
*iv_ptr++ = ctx->hcryp_aes.Instance->IVR1;
269231
*iv_ptr++ = ctx->hcryp_aes.Instance->IVR0;
270-
271232
} else {
272-
status |= st_hal_cryp_cbc(ctx, CRYP_ALGOMODE_ENCRYPT, length, iv, (uint8_t *)input, (uint8_t *)output);
273-
memcpy( iv, output, 16 );
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;
274237
}
238+
275239
#else
276-
240+
277241
if( mode == MBEDTLS_AES_DECRYPT ) {
278-
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;
279257
} else {
280-
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;
281262
}
282-
#endif
283-
status |= st_cbc_save_context(ctx);
284263

285-
return( status );
264+
#endif
265+
return 0;
286266
}
287267
#endif /* MBEDTLS_CIPHER_MODE_CBC */
288268

@@ -301,7 +281,8 @@ int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
301281
if( mode == MBEDTLS_AES_DECRYPT ) {
302282
while( length-- ) {
303283
if( n == 0 )
304-
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;
305286

306287
c = *input++;
307288
*output++ = (unsigned char)( c ^ iv[n] );
@@ -312,7 +293,8 @@ int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
312293
} else {
313294
while( length-- ) {
314295
if( n == 0 )
315-
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;
316298

317299
iv[n] = *output++ = (unsigned char)( iv[n] ^ *input++ );
318300

@@ -338,7 +320,8 @@ int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
338320

339321
while( length-- ) {
340322
memcpy( ov, iv, 16 );
341-
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;
342325

343326
if( mode == MBEDTLS_AES_DECRYPT )
344327
ov[16] = *input;
@@ -371,7 +354,8 @@ int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
371354
while( length-- )
372355
{
373356
if( n == 0 ) {
374-
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;
375359

376360
for( i = 16; i > 0; i-- )
377361
if( ++nonce_counter[i - 1] != 0 )
@@ -393,9 +377,9 @@ int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
393377
const unsigned char input[16],
394378
unsigned char output[16] )
395379
{
396-
if (HAL_CRYP_AESECB_Encrypt(&ctx->hcryp_aes, (uint8_t *)input, 16, (uint8_t *)output, 10) !=0) {
380+
if (HAL_CRYP_AESECB_Encrypt(&ctx->hcryp_aes, (uint8_t *)input, 16, (uint8_t *)output, 10) != HAL_OK) {
397381
// error found
398-
return 1;
382+
return ST_ERR_AES_BUSY;
399383
}
400384
return 0;
401385

@@ -405,13 +389,14 @@ int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
405389
const unsigned char input[16],
406390
unsigned char output[16] )
407391
{
408-
if(HAL_CRYP_AESECB_Decrypt(&ctx->hcryp_aes, (uint8_t *)input, 16, (uint8_t *)output, 10)) {
392+
if(HAL_CRYP_AESECB_Decrypt(&ctx->hcryp_aes, (uint8_t *)input, 16, (uint8_t *)output, 10) != HAL_OK) {
409393
// error found
410-
return 1;
394+
return ST_ERR_AES_BUSY;
411395
}
412396
return 0;
413397
}
414398

399+
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
415400
void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
416401
const unsigned char input[16],
417402
unsigned char output[16] )
@@ -425,5 +410,5 @@ void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
425410
{
426411
mbedtls_internal_aes_decrypt( ctx, input, output );
427412
}
428-
413+
#endif /* MBEDTLS_DEPRECATED_REMOVED */
429414
#endif /*MBEDTLS_AES_ALT*/

features/mbedtls/targets/TARGET_STM/aes_alt.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
extern "C" {
3232
#endif
3333

34-
#define ST_AES_TIMEOUT ((uint32_t) 3)
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 */
3536
/**
3637
* \brief AES context structure
3738
*
@@ -45,7 +46,6 @@ typedef struct
4546
unsigned char aes_key[32]; /* Decryption key */
4647
CRYP_HandleTypeDef hcryp_aes;
4748
uint32_t ctx_save_cr; /* save context for multi-instance */
48-
uint32_t save_iv[4]; /* save context for multi-instance */
4949
}
5050
mbedtls_aes_context;
5151

0 commit comments

Comments
 (0)