@@ -129,6 +129,7 @@ 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 ;
132
133
133
134
if (mode == MBEDTLS_AES_DECRYPT ) { /* AES decryption */
134
135
ctx -> hcryp_aes .Init .DataType = CRYP_DATATYPE_8B ;
@@ -147,31 +148,93 @@ int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
147
148
148
149
#if defined(MBEDTLS_CIPHER_MODE_CBC )
149
150
#if defined (TARGET_STM32L486xG )
151
+ static int st_cbc_restore_context (mbedtls_aes_context * ctx ){
152
+ uint32_t tickstart ;
153
+ tickstart = HAL_GetTick ();
154
+ while ((ctx -> hcryp_aes .Instance -> SR & AES_SR_BUSY ) != 0 ){
155
+ if ((HAL_GetTick () - tickstart ) > ST_AES_TIMEOUT ) {
156
+ return 1 ; // timeout: CRYP processor is busy
157
+ }
158
+ }
159
+ /* allow multi-instance of CRYP use: restore context for CRYP hw module */
160
+ ctx -> hcryp_aes .Instance -> CR = ctx -> ctx_save_cr ;
161
+ return 0 ;
162
+ }
163
+ static int st_cbc_save_context (mbedtls_aes_context * ctx ){
164
+ uint32_t tickstart ;
165
+
166
+ tickstart = HAL_GetTick ();
167
+ while ((ctx -> hcryp_aes .Instance -> SR & AES_SR_BUSY ) != 0 ){
168
+ if ((HAL_GetTick () - tickstart ) > ST_AES_TIMEOUT ) {
169
+ return 1 ; // timeout: CRYP processor is busy
170
+ }
171
+ }
172
+ /* allow multi-instance of CRYP use: save context for CRYP HW module CR */
173
+ ctx -> ctx_save_cr = ctx -> hcryp_aes .Instance -> CR ;
174
+
175
+ return 0 ;
176
+ }
150
177
static int st_hal_cryp_cbc ( mbedtls_aes_context * ctx , uint32_t opmode , size_t length ,
151
178
unsigned char iv [16 ], uint8_t * input , uint8_t * output )
152
179
{
153
180
int status = 0 ;
154
181
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
- }
182
+ /* At this moment only, we know we have CBC mode: Re-initialize AES
183
+ IP with proper parameters and apply key and IV for multi context usecase */
184
+ if (HAL_CRYP_DeInit (& ctx -> hcryp_aes ) != HAL_OK )
185
+ return HAL_ERROR ;
186
+ ctx -> hcryp_aes .Init .OperatingMode = opmode ;
187
+ ctx -> hcryp_aes .Init .ChainingMode = CRYP_CHAINMODE_AES_CBC ;
188
+ ctx -> hcryp_aes .Init .KeyWriteFlag = CRYP_KEY_WRITE_ENABLE ;
189
+ if (HAL_CRYP_Init (& ctx -> hcryp_aes ) != HAL_OK )
190
+ return HAL_ERROR ;
168
191
169
192
status = HAL_CRYPEx_AES (& ctx -> hcryp_aes , input , length , output , 10 );
170
193
171
194
return status ;
172
195
}
196
+ #else
197
+ static int st_cbc_restore_context (mbedtls_aes_context * ctx ){
198
+ uint32_t tickstart ;
199
+ tickstart = HAL_GetTick ();
200
+ while ((ctx -> hcryp_aes .Instance -> SR & (CRYP_SR_IFEM | CRYP_SR_OFNE | CRYP_SR_BUSY )) != CRYP_SR_IFEM ){
201
+ if ((HAL_GetTick () - tickstart ) > ST_AES_TIMEOUT ) {
202
+ return 1 ; // timeout: CRYP processor is busy
203
+ }
204
+ }
205
+ ctx -> hcryp_aes .Instance -> CR &= ~CRYP_CR_CRYPEN ;
206
+ /* save initvector for multi-instance use of CRYP */
207
+ ctx -> hcryp_aes .Instance -> IV1RR = ctx -> save_iv [3 ];
208
+ ctx -> hcryp_aes .Instance -> IV1LR = ctx -> save_iv [2 ];
209
+ ctx -> hcryp_aes .Instance -> IV0RR = ctx -> save_iv [1 ];
210
+ ctx -> hcryp_aes .Instance -> IV0LR = ctx -> save_iv [0 ];
211
+ ctx -> hcryp_aes .Phase = HAL_CRYP_PHASE_READY ;
212
+ /* allow multi-instance of CRYP use: restore context for CRYP hw module */
213
+ ctx -> hcryp_aes .Instance -> CR = ctx -> ctx_save_cr ;
214
+ return 0 ;
215
+ }
216
+ static int st_cbc_save_context (mbedtls_aes_context * ctx ){
217
+ uint32_t tickstart ;
218
+ tickstart = HAL_GetTick ();
219
+ while ((ctx -> hcryp_aes .Instance -> SR & (CRYP_SR_IFEM | CRYP_SR_OFNE | CRYP_SR_BUSY )) != CRYP_SR_IFEM ){
220
+ if ((HAL_GetTick () - tickstart ) > ST_AES_TIMEOUT ) {
221
+ return 1 ; // timeout: CRYP processor is busy
222
+ }
223
+ }
224
+ /* allow multi-instance of CRYP use: save context for CRYP HW module CR */
225
+ ctx -> ctx_save_cr = ctx -> hcryp_aes .Instance -> CR ;
226
+ ctx -> hcryp_aes .Instance -> CR &= ~CRYP_CR_CRYPEN ;
227
+ /* save initvector for multi-instance use of CRYP */
228
+ ctx -> save_iv [3 ] = ctx -> hcryp_aes .Instance -> IV1RR ;
229
+ ctx -> save_iv [2 ] = ctx -> hcryp_aes .Instance -> IV1LR ;
230
+ ctx -> save_iv [1 ] = ctx -> hcryp_aes .Instance -> IV0RR ;
231
+ ctx -> save_iv [0 ] = ctx -> hcryp_aes .Instance -> IV0LR ;
232
+ if ((ctx -> ctx_save_cr & CRYP_CR_CRYPEN ) == CRYP_CR_CRYPEN ) {
233
+ ctx -> hcryp_aes .Instance -> CR &= CRYP_CR_CRYPEN ;
234
+ }
235
+ return 0 ;
236
+ }
173
237
#endif /* TARGET_STM32L486xG */
174
-
175
238
int mbedtls_aes_crypt_cbc ( mbedtls_aes_context * ctx ,
176
239
int mode ,
177
240
size_t length ,
@@ -180,23 +243,43 @@ int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
180
243
unsigned char * output )
181
244
{
182
245
int status = 0 ;
246
+ uint32_t * iv_ptr = (uint32_t * )& iv [0 ];
183
247
if ( length % 16 )
184
248
return ( MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
249
+ ctx -> hcryp_aes .Init .pInitVect = & iv [0 ];
250
+ status |= st_cbc_restore_context (ctx );
185
251
#if defined (TARGET_STM32L486xG )
186
252
if ( mode == MBEDTLS_AES_DECRYPT ) {
187
- status = st_hal_cryp_cbc (ctx , CRYP_ALGOMODE_KEYDERIVATION_DECRYPT , length , iv , (uint8_t * )input , (uint8_t * )output );
253
+ status |= st_hal_cryp_cbc (ctx , CRYP_ALGOMODE_KEYDERIVATION_DECRYPT , length , iv , (uint8_t * )input , (uint8_t * )output );
254
+ // update IV
255
+ uint32_t tickstart ;
256
+ tickstart = HAL_GetTick ();
257
+ while ((ctx -> hcryp_aes .Instance -> SR & AES_SR_BUSY ) != 0 ){
258
+ if ((HAL_GetTick () - tickstart ) > ST_AES_TIMEOUT ) {
259
+ return 1 ; // timeout: CRYP processor is busy
260
+ }
261
+ }
262
+ ctx -> ctx_save_cr = ctx -> hcryp_aes .Instance -> CR ;
263
+ ctx -> hcryp_aes .Instance -> CR &= ~AES_CR_EN ;
264
+ * iv_ptr ++ = ctx -> hcryp_aes .Instance -> IVR3 ;
265
+ * iv_ptr ++ = ctx -> hcryp_aes .Instance -> IVR2 ;
266
+ * iv_ptr ++ = ctx -> hcryp_aes .Instance -> IVR1 ;
267
+ * iv_ptr ++ = ctx -> hcryp_aes .Instance -> IVR0 ;
268
+
188
269
} else {
189
- status = st_hal_cryp_cbc (ctx , CRYP_ALGOMODE_ENCRYPT , length , iv , (uint8_t * )input , (uint8_t * )output );
270
+ status |= st_hal_cryp_cbc (ctx , CRYP_ALGOMODE_ENCRYPT , length , iv , (uint8_t * )input , (uint8_t * )output );
271
+ memcpy ( iv , output , 16 );
190
272
}
191
273
#else
192
- ctx -> hcryp_aes .Init .pInitVect = & iv [0 ];
193
274
194
275
if ( mode == MBEDTLS_AES_DECRYPT ) {
195
- status = HAL_CRYP_AESCBC_Decrypt (& ctx -> hcryp_aes , (uint8_t * )input , length , (uint8_t * )output , 10 );
276
+ status | = HAL_CRYP_AESCBC_Decrypt (& ctx -> hcryp_aes , (uint8_t * )input , length , (uint8_t * )output , 10 );
196
277
} else {
197
- status = HAL_CRYP_AESCBC_Encrypt (& ctx -> hcryp_aes , (uint8_t * )input , length , (uint8_t * )output , 10 );
278
+ status | = HAL_CRYP_AESCBC_Encrypt (& ctx -> hcryp_aes , (uint8_t * )input , length , (uint8_t * )output , 10 );
198
279
}
199
280
#endif
281
+ status |= st_cbc_save_context (ctx );
282
+
200
283
return ( status );
201
284
}
202
285
#endif /* MBEDTLS_CIPHER_MODE_CBC */
@@ -308,7 +391,6 @@ void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
308
391
const unsigned char input [16 ],
309
392
unsigned char output [16 ] )
310
393
{
311
-
312
394
if (HAL_CRYP_AESECB_Encrypt (& ctx -> hcryp_aes , (uint8_t * )input , 16 , (uint8_t * )output , 10 ) != 0 ) {
313
395
// error found to be returned
314
396
}
@@ -319,7 +401,6 @@ void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
319
401
const unsigned char input [16 ],
320
402
unsigned char output [16 ] )
321
403
{
322
-
323
404
if (HAL_CRYP_AESECB_Decrypt (& ctx -> hcryp_aes , (uint8_t * )input , 16 , (uint8_t * )output , 10 )) {
324
405
// error found to be returned
325
406
}
0 commit comments