1
1
/*
2
- * Hardware aes collector for the STM32F4 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
@@ -129,15 +129,18 @@ int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
129
129
130
130
/* allow multi-instance of CRYP use: restore context for CRYP hw module */
131
131
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 ;
132
135
133
136
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
+ }
137
140
} 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
+ }
141
144
}
142
145
/* allow multi-instance of CRYP use: save context for CRYP HW module CR */
143
146
ctx -> ctx_save_cr = ctx -> hcryp_aes .Instance -> CR ;
@@ -147,29 +150,50 @@ int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
147
150
148
151
#if defined(MBEDTLS_CIPHER_MODE_CBC )
149
152
#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
+
150
166
static int st_hal_cryp_cbc ( mbedtls_aes_context * ctx , uint32_t opmode , size_t length ,
151
167
unsigned char iv [16 ], uint8_t * input , uint8_t * output )
152
168
{
153
- int status = 0 ;
154
169
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 ;
170
179
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 ;
172
195
}
196
+
173
197
#endif /* TARGET_STM32L486xG */
174
198
175
199
int mbedtls_aes_crypt_cbc ( mbedtls_aes_context * ctx ,
@@ -179,25 +203,66 @@ int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
179
203
const unsigned char * input ,
180
204
unsigned char * output )
181
205
{
182
- int status = 0 ;
206
+ uint32_t tickstart ;
207
+ uint32_t * iv_ptr = (uint32_t * )& iv [0 ];
183
208
if ( length % 16 )
184
209
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
+
185
214
#if defined (TARGET_STM32L486xG )
215
+
186
216
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 ;
188
232
} 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 ;
190
237
}
238
+
191
239
#else
192
- ctx -> hcryp_aes .Init .pInitVect = & iv [0 ];
193
-
240
+
194
241
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 ;
196
257
} 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 ;
198
262
}
263
+
199
264
#endif
200
- return ( status ) ;
265
+ return 0 ;
201
266
}
202
267
#endif /* MBEDTLS_CIPHER_MODE_CBC */
203
268
@@ -216,7 +281,8 @@ int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
216
281
if ( mode == MBEDTLS_AES_DECRYPT ) {
217
282
while ( length -- ) {
218
283
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 ;
220
286
221
287
c = * input ++ ;
222
288
* output ++ = (unsigned char )( c ^ iv [n ] );
@@ -227,7 +293,8 @@ int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
227
293
} else {
228
294
while ( length -- ) {
229
295
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 ;
231
298
232
299
iv [n ] = * output ++ = (unsigned char )( iv [n ] ^ * input ++ );
233
300
@@ -253,7 +320,8 @@ int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
253
320
254
321
while ( length -- ) {
255
322
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 ;
257
325
258
326
if ( mode == MBEDTLS_AES_DECRYPT )
259
327
ov [16 ] = * input ;
@@ -286,7 +354,8 @@ int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
286
354
while ( length -- )
287
355
{
288
356
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 ;
290
359
291
360
for ( i = 16 ; i > 0 ; i -- )
292
361
if ( ++ nonce_counter [i - 1 ] != 0 )
@@ -304,26 +373,42 @@ int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
304
373
}
305
374
#endif /* MBEDTLS_CIPHER_MODE_CTR */
306
375
307
- void mbedtls_aes_encrypt ( mbedtls_aes_context * ctx ,
376
+ int mbedtls_internal_aes_encrypt ( mbedtls_aes_context * ctx ,
308
377
const unsigned char input [16 ],
309
378
unsigned char output [16 ] )
310
379
{
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 ;
314
383
}
384
+ return 0 ;
315
385
316
386
}
317
387
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 ] )
321
391
{
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 ;
325
395
}
396
+ return 0 ;
326
397
}
327
398
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
+ }
328
406
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 */
329
414
#endif /*MBEDTLS_AES_ALT*/
0 commit comments