1
1
/*
2
- * Hardware aes collector for the STM32F4 F7 and L4 family
2
+ * Hardware aes implementation for STM32F4 STM32F7 and STM32L4 families
3
3
*******************************************************************************
4
4
* Copyright (c) 2017, STMicroelectronics
5
5
* SPDX-License-Identifier: Apache-2.0
@@ -135,11 +135,11 @@ int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
135
135
136
136
if (mode == MBEDTLS_AES_DECRYPT ) { /* AES decryption */
137
137
if (mbedtls_internal_aes_decrypt ( ctx , input , output )){
138
- return 1 ;
138
+ return ST_ERR_AES_BUSY ;
139
139
}
140
140
} else { /* AES encryption */
141
141
if (mbedtls_internal_aes_encrypt ( ctx , input , output )) {
142
- return 1 ;
142
+ return ST_ERR_AES_BUSY ;
143
143
}
144
144
}
145
145
/* 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){
155
155
tickstart = HAL_GetTick ();
156
156
while ((ctx -> hcryp_aes .Instance -> SR & AES_SR_BUSY ) != 0 ){
157
157
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
159
159
}
160
160
}
161
161
/* allow multi-instance of CRYP use: restore context for CRYP hw module */
162
162
ctx -> hcryp_aes .Instance -> CR = ctx -> ctx_save_cr ;
163
163
return 0 ;
164
164
}
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 ;
176
165
177
- return 0 ;
178
- }
179
166
static int st_hal_cryp_cbc ( mbedtls_aes_context * ctx , uint32_t opmode , size_t length ,
180
167
unsigned char iv [16 ], uint8_t * input , uint8_t * output )
181
168
{
182
- int status = 0 ;
183
169
ctx -> hcryp_aes .Init .pInitVect = & iv [0 ]; // used in process, not in the init
184
170
/* At this moment only, we know we have CBC mode: Re-initialize AES
185
171
IP with proper parameters and apply key and IV for multi context usecase */
186
172
if (HAL_CRYP_DeInit (& ctx -> hcryp_aes ) != HAL_OK )
187
- return HAL_ERROR ;
173
+ return ST_ERR_AES_BUSY ;
188
174
ctx -> hcryp_aes .Init .OperatingMode = opmode ;
189
175
ctx -> hcryp_aes .Init .ChainingMode = CRYP_CHAINMODE_AES_CBC ;
190
176
ctx -> hcryp_aes .Init .KeyWriteFlag = CRYP_KEY_WRITE_ENABLE ;
191
177
if (HAL_CRYP_Init (& ctx -> hcryp_aes ) != HAL_OK )
192
- return HAL_ERROR ;
178
+ return ST_ERR_AES_BUSY ;
193
179
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 ;
197
183
}
198
- #else
184
+ #else /* STM32F4 and STM32F7 */
199
185
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 ;
214
186
/* allow multi-instance of CRYP use: restore context for CRYP hw module */
215
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 ;
216
194
return 0 ;
217
195
}
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
+
239
197
#endif /* TARGET_STM32L486xG */
198
+
240
199
int mbedtls_aes_crypt_cbc ( mbedtls_aes_context * ctx ,
241
200
int mode ,
242
201
size_t length ,
243
202
unsigned char iv [16 ],
244
203
const unsigned char * input ,
245
204
unsigned char * output )
246
205
{
247
- int status = 0 ;
206
+ uint32_t tickstart ;
207
+ uint32_t * iv_ptr = (uint32_t * )& iv [0 ];
248
208
if ( length % 16 )
249
209
return ( MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
250
210
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
+
252
214
#if defined (TARGET_STM32L486xG )
253
- uint32_t * iv_ptr = ( uint32_t * ) & iv [ 0 ];
215
+
254
216
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 */
258
220
tickstart = HAL_GetTick ();
259
221
while ((ctx -> hcryp_aes .Instance -> SR & AES_SR_BUSY ) != 0 ){
260
222
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
262
224
}
263
225
}
264
- ctx -> ctx_save_cr = ctx -> hcryp_aes .Instance -> CR ;
226
+ ctx -> ctx_save_cr = ctx -> hcryp_aes .Instance -> CR ; // save here before overwritten
265
227
ctx -> hcryp_aes .Instance -> CR &= ~AES_CR_EN ;
266
228
* iv_ptr ++ = ctx -> hcryp_aes .Instance -> IVR3 ;
267
229
* iv_ptr ++ = ctx -> hcryp_aes .Instance -> IVR2 ;
268
230
* iv_ptr ++ = ctx -> hcryp_aes .Instance -> IVR1 ;
269
231
* iv_ptr ++ = ctx -> hcryp_aes .Instance -> IVR0 ;
270
-
271
232
} 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 ;
274
237
}
238
+
275
239
#else
276
-
240
+
277
241
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 ;
279
257
} 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 ;
281
262
}
282
- #endif
283
- status |= st_cbc_save_context (ctx );
284
263
285
- return ( status );
264
+ #endif
265
+ return 0 ;
286
266
}
287
267
#endif /* MBEDTLS_CIPHER_MODE_CBC */
288
268
@@ -301,7 +281,8 @@ int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
301
281
if ( mode == MBEDTLS_AES_DECRYPT ) {
302
282
while ( length -- ) {
303
283
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 ;
305
286
306
287
c = * input ++ ;
307
288
* output ++ = (unsigned char )( c ^ iv [n ] );
@@ -312,7 +293,8 @@ int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
312
293
} else {
313
294
while ( length -- ) {
314
295
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 ;
316
298
317
299
iv [n ] = * output ++ = (unsigned char )( iv [n ] ^ * input ++ );
318
300
@@ -338,7 +320,8 @@ int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
338
320
339
321
while ( length -- ) {
340
322
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 ;
342
325
343
326
if ( mode == MBEDTLS_AES_DECRYPT )
344
327
ov [16 ] = * input ;
@@ -371,7 +354,8 @@ int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
371
354
while ( length -- )
372
355
{
373
356
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 ;
375
359
376
360
for ( i = 16 ; i > 0 ; i -- )
377
361
if ( ++ nonce_counter [i - 1 ] != 0 )
@@ -393,9 +377,9 @@ int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
393
377
const unsigned char input [16 ],
394
378
unsigned char output [16 ] )
395
379
{
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 ) {
397
381
// error found
398
- return 1 ;
382
+ return ST_ERR_AES_BUSY ;
399
383
}
400
384
return 0 ;
401
385
@@ -405,13 +389,14 @@ int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
405
389
const unsigned char input [16 ],
406
390
unsigned char output [16 ] )
407
391
{
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 ) {
409
393
// error found
410
- return 1 ;
394
+ return ST_ERR_AES_BUSY ;
411
395
}
412
396
return 0 ;
413
397
}
414
398
399
+ #if !defined(MBEDTLS_DEPRECATED_REMOVED )
415
400
void mbedtls_aes_encrypt ( mbedtls_aes_context * ctx ,
416
401
const unsigned char input [16 ],
417
402
unsigned char output [16 ] )
@@ -425,5 +410,5 @@ void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
425
410
{
426
411
mbedtls_internal_aes_decrypt ( ctx , input , output );
427
412
}
428
-
413
+ #endif /* MBEDTLS_DEPRECATED_REMOVED */
429
414
#endif /*MBEDTLS_AES_ALT*/
0 commit comments