6
6
*
7
7
* SPDX-License-Identifier: Apache-2.0
8
8
*
9
- * SPDX-FileContributor: 2016-2023 Espressif Systems (Shanghai) CO LTD
9
+ * SPDX-FileContributor: 2016-2024 Espressif Systems (Shanghai) CO LTD
10
10
*/
11
11
/*
12
12
* The AES block cipher was designed by Vincent Rijmen and Joan Daemen.
@@ -251,6 +251,27 @@ int esp_aes_gcm_setkey( esp_gcm_context *ctx,
251
251
const unsigned char * key ,
252
252
unsigned int keybits )
253
253
{
254
+ /* Fallback to software implementation of GCM operation when a non-AES
255
+ * cipher is selected, as we support hardware acceleration only for a
256
+ * GCM operation using AES cipher.
257
+ */
258
+ #if defined(MBEDTLS_GCM_NON_AES_CIPHER_SOFT_FALLBACK )
259
+ if (ctx -> ctx_soft != NULL ) {
260
+ mbedtls_gcm_free_soft (ctx -> ctx_soft );
261
+ free (ctx -> ctx_soft );
262
+ ctx -> ctx_soft = NULL ;
263
+ }
264
+
265
+ if (cipher != MBEDTLS_CIPHER_ID_AES ) {
266
+ ctx -> ctx_soft = (mbedtls_gcm_context_soft * ) malloc (sizeof (mbedtls_gcm_context_soft ));
267
+ if (ctx -> ctx_soft == NULL ) {
268
+ return MBEDTLS_ERR_CIPHER_ALLOC_FAILED ;
269
+ }
270
+ mbedtls_gcm_init_soft (ctx -> ctx_soft );
271
+ return mbedtls_gcm_setkey_soft (ctx -> ctx_soft , cipher , key , keybits );
272
+ }
273
+ #endif
274
+
254
275
#if !SOC_AES_SUPPORT_AES_192
255
276
if (keybits == 192 ) {
256
277
return MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED ;
@@ -332,6 +353,14 @@ void esp_aes_gcm_free( esp_gcm_context *ctx)
332
353
if (ctx == NULL ) {
333
354
return ;
334
355
}
356
+ #if defined(MBEDTLS_GCM_NON_AES_CIPHER_SOFT_FALLBACK )
357
+ if (ctx -> ctx_soft != NULL ) {
358
+ mbedtls_gcm_free_soft (ctx -> ctx_soft );
359
+ free (ctx -> ctx_soft );
360
+ /* Note that the value of ctx->ctx_soft should be NULL'ed out
361
+ and here it is taken care by the bzero call below */
362
+ }
363
+ #endif
335
364
bzero (ctx , sizeof (esp_gcm_context ));
336
365
}
337
366
@@ -341,18 +370,24 @@ int esp_aes_gcm_starts( esp_gcm_context *ctx,
341
370
const unsigned char * iv ,
342
371
size_t iv_len )
343
372
{
373
+ if (!ctx ) {
374
+ ESP_LOGE (TAG , "No AES context supplied" );
375
+ return MBEDTLS_ERR_GCM_BAD_INPUT ;
376
+ }
377
+
378
+ #if defined(MBEDTLS_GCM_NON_AES_CIPHER_SOFT_FALLBACK )
379
+ if (ctx -> ctx_soft != NULL ) {
380
+ return mbedtls_gcm_starts_soft (ctx -> ctx_soft , mode , iv , iv_len );
381
+ }
382
+ #endif
383
+
344
384
/* IV is limited to 2^32 bits, so 2^29 bytes */
345
385
/* IV is not allowed to be zero length */
346
386
if ( iv_len == 0 ||
347
387
( (uint32_t ) iv_len ) >> 29 != 0 ) {
348
388
return ( MBEDTLS_ERR_GCM_BAD_INPUT );
349
389
}
350
390
351
- if (!ctx ) {
352
- ESP_LOGE (TAG , "No AES context supplied" );
353
- return MBEDTLS_ERR_GCM_BAD_INPUT ;
354
- }
355
-
356
391
if (!iv ) {
357
392
ESP_LOGE (TAG , "No IV supplied" );
358
393
return MBEDTLS_ERR_GCM_BAD_INPUT ;
@@ -407,16 +442,22 @@ int esp_aes_gcm_update_ad( esp_gcm_context *ctx,
407
442
const unsigned char * aad ,
408
443
size_t aad_len )
409
444
{
410
- /* AD are limited to 2^32 bits, so 2^29 bytes */
411
- if ( ( (uint32_t ) aad_len ) >> 29 != 0 ) {
412
- return ( MBEDTLS_ERR_GCM_BAD_INPUT );
413
- }
414
-
415
445
if (!ctx ) {
416
446
ESP_LOGE (TAG , "No AES context supplied" );
417
447
return MBEDTLS_ERR_GCM_BAD_INPUT ;
418
448
}
419
449
450
+ #if defined(MBEDTLS_GCM_NON_AES_CIPHER_SOFT_FALLBACK )
451
+ if (ctx -> ctx_soft != NULL ) {
452
+ return mbedtls_gcm_update_ad_soft (ctx -> ctx_soft , aad , aad_len );
453
+ }
454
+ #endif
455
+
456
+ /* AD are limited to 2^32 bits, so 2^29 bytes */
457
+ if ( ( (uint32_t ) aad_len ) >> 29 != 0 ) {
458
+ return ( MBEDTLS_ERR_GCM_BAD_INPUT );
459
+ }
460
+
420
461
if ( (aad_len > 0 ) && !aad ) {
421
462
ESP_LOGE (TAG , "No aad supplied" );
422
463
return MBEDTLS_ERR_GCM_BAD_INPUT ;
@@ -442,6 +483,17 @@ int esp_aes_gcm_update( esp_gcm_context *ctx,
442
483
unsigned char * output , size_t output_size ,
443
484
size_t * output_length )
444
485
{
486
+ if (!ctx ) {
487
+ ESP_LOGE (TAG , "No GCM context supplied" );
488
+ return MBEDTLS_ERR_GCM_BAD_INPUT ;
489
+ }
490
+
491
+ #if defined(MBEDTLS_GCM_NON_AES_CIPHER_SOFT_FALLBACK )
492
+ if (ctx -> ctx_soft != NULL ) {
493
+ return mbedtls_gcm_update_soft (ctx -> ctx_soft , input , input_length , output , output_size , output_length );
494
+ }
495
+ #endif
496
+
445
497
size_t nc_off = 0 ;
446
498
uint8_t nonce_counter [AES_BLOCK_BYTES ] = {0 };
447
499
uint8_t stream [AES_BLOCK_BYTES ] = {0 };
@@ -452,10 +504,6 @@ int esp_aes_gcm_update( esp_gcm_context *ctx,
452
504
}
453
505
* output_length = input_length ;
454
506
455
- if (!ctx ) {
456
- ESP_LOGE (TAG , "No GCM context supplied" );
457
- return MBEDTLS_ERR_GCM_BAD_INPUT ;
458
- }
459
507
if (!input ) {
460
508
ESP_LOGE (TAG , "No input supplied" );
461
509
return MBEDTLS_ERR_GCM_BAD_INPUT ;
@@ -512,6 +560,11 @@ int esp_aes_gcm_finish( esp_gcm_context *ctx,
512
560
size_t * output_length ,
513
561
unsigned char * tag , size_t tag_len )
514
562
{
563
+ #if defined(MBEDTLS_GCM_NON_AES_CIPHER_SOFT_FALLBACK )
564
+ if (ctx -> ctx_soft != NULL ) {
565
+ return mbedtls_gcm_finish_soft (ctx -> ctx_soft , output , output_size , output_length , tag , tag_len );
566
+ }
567
+ #endif
515
568
size_t nc_off = 0 ;
516
569
uint8_t len_block [AES_BLOCK_BYTES ] = {0 };
517
570
uint8_t stream [AES_BLOCK_BYTES ] = {0 };
@@ -607,6 +660,16 @@ int esp_aes_gcm_crypt_and_tag( esp_gcm_context *ctx,
607
660
size_t tag_len ,
608
661
unsigned char * tag )
609
662
{
663
+ if (!ctx ) {
664
+ ESP_LOGE (TAG , "No AES context supplied" );
665
+ return MBEDTLS_ERR_GCM_BAD_INPUT ;
666
+ }
667
+
668
+ #if defined(MBEDTLS_GCM_NON_AES_CIPHER_SOFT_FALLBACK )
669
+ if (ctx -> ctx_soft != NULL ) {
670
+ return mbedtls_gcm_crypt_and_tag_soft (ctx -> ctx_soft , mode , length , iv , iv_len , aad , aad_len , input , output , tag_len , tag );
671
+ }
672
+ #endif
610
673
#if CONFIG_MBEDTLS_HARDWARE_GCM
611
674
int ret ;
612
675
lldesc_t aad_desc [2 ] = {};
@@ -635,11 +698,6 @@ int esp_aes_gcm_crypt_and_tag( esp_gcm_context *ctx,
635
698
return ( MBEDTLS_ERR_GCM_BAD_INPUT );
636
699
}
637
700
638
- if (!ctx ) {
639
- ESP_LOGE (TAG , "No AES context supplied" );
640
- return MBEDTLS_ERR_GCM_BAD_INPUT ;
641
- }
642
-
643
701
if (!iv ) {
644
702
ESP_LOGE (TAG , "No IV supplied" );
645
703
return MBEDTLS_ERR_GCM_BAD_INPUT ;
@@ -727,6 +785,11 @@ int esp_aes_gcm_auth_decrypt( esp_gcm_context *ctx,
727
785
const unsigned char * input ,
728
786
unsigned char * output )
729
787
{
788
+ #if defined(MBEDTLS_GCM_NON_AES_CIPHER_SOFT_FALLBACK )
789
+ if (ctx -> ctx_soft != NULL ) {
790
+ return mbedtls_gcm_auth_decrypt_soft (ctx -> ctx_soft , length , iv , iv_len , aad , aad_len , tag , tag_len , input , output );
791
+ }
792
+ #endif
730
793
int ret ;
731
794
unsigned char check_tag [16 ];
732
795
size_t i ;
0 commit comments