80
80
#define GHASH_MODE (AES_MODE_GHASH << CRPT_AES_CTL_OPMODE_Pos)
81
81
#define CTR_MODE (AES_MODE_CTR << CRPT_AES_CTL_OPMODE_Pos)
82
82
83
+ #define Debug_GCM_Info (x ) {}
84
+ //#define Debug_GCM_Info(x) { printf x; }
83
85
84
86
/*
85
87
* Initialize a context
@@ -351,7 +353,11 @@ int mbedtls_gcm_starts( mbedtls_gcm_context *ctx,
351
353
uint32_t size ;
352
354
size_t * pSz ;
353
355
int32_t ret ;
354
-
356
+
357
+ if ( ctx -> pcntLen == 0 ) ctx -> pcntLen = -1 ;
358
+ ctx -> len = 0x00 ;
359
+
360
+ Debug_GCM_Info (("## FUNC: %s, mode=%s, pcnt=%d, ctx->len=%d\n" , __FUNCTION__ , (mode ) ? "Enc" :"Dec" , ctx -> pcntLen , ctx -> len ));
355
361
/* Acquire ownership of AES H/W */
356
362
crypto_aes_acquire ();
357
363
@@ -387,16 +393,18 @@ int mbedtls_gcm_starts( mbedtls_gcm_context *ctx,
387
393
CRPT -> AES_CNT = ctx -> gcm_buf_bytes ;
388
394
389
395
/* Set a big number for unknown P length */
390
- CRPT -> AES_GCM_PCNT [0 ] = (uint32_t )-1 ;
396
+ CRPT -> AES_GCM_PCNT [0 ] = ctx -> pcntLen ; // (uint32_t)-1;
391
397
CRPT -> AES_GCM_PCNT [1 ] = 0 ;
392
398
393
399
/* Start with cascade mode */
394
- if ((ret = AES_Run (ctx , ctx -> basicOpt | FBOUT )))
400
+ // if((ret = AES_Run(ctx, ctx->basicOpt | FBOUT)))
401
+ if ((ret = AES_Run (ctx , ctx -> basicOpt | GCM_MODE | FBOUT | DMAEN )))
395
402
{
396
403
return ret ;
397
404
}
398
405
399
- ctx -> firstFlag = 1 ;
406
+ ctx -> firstFlag = 1 ;
407
+ ctx -> endFlag = 0 ;
400
408
401
409
return ( 0 );
402
410
}
@@ -413,11 +421,11 @@ int mbedtls_gcm_update( mbedtls_gcm_context *ctx,
413
421
int32_t ret ;
414
422
int32_t len , len_aligned ;
415
423
uint32_t u32Size ;
416
-
424
+ Debug_GCM_Info (( "## FUNC: %s, input_length=%d\n" , __FUNCTION__ , input_length ));
417
425
GCM_VALIDATE_RET ( ctx != NULL );
418
426
GCM_VALIDATE_RET ( input_length == 0 || input != NULL );
419
427
GCM_VALIDATE_RET ( input_length == 0 || output != NULL );
420
-
428
+
421
429
len = (int32_t )input_length ;
422
430
/* Error if length too large */
423
431
if ( (size_t )len != input_length )
@@ -474,16 +482,18 @@ int mbedtls_gcm_update( mbedtls_gcm_context *ctx,
474
482
}
475
483
else
476
484
{
485
+
477
486
/* Over buffer size */
478
487
return (MBEDTLS_ERR_GCM_BAD_INPUT );
479
488
}
480
489
481
490
/* Do GCM with cascade */
482
491
if (len & 0xf )
483
492
{
484
-
485
493
/* No 16 bytes alignment, it should be last */
494
+
486
495
CRPT -> AES_GCM_PCNT [0 ] = ctx -> len ;
496
+ CRPT -> AES_GCM_PCNT [1 ] = 0 ;
487
497
CRPT -> AES_CNT = u32Size ;
488
498
489
499
if ((ret = AES_Run (ctx , ctx -> basicOpt | FBIN | FBOUT | DMACC | DMALAST )))
@@ -526,9 +536,8 @@ int mbedtls_gcm_finish( mbedtls_gcm_context *ctx,
526
536
size_t tag_len )
527
537
{
528
538
529
-
530
539
int32_t ret = 0 ;
531
-
540
+ Debug_GCM_Info (( "## FUNC: %s, tag_len=%d, pcnt=%d\n" , __FUNCTION__ , tag_len , ctx -> len ));
532
541
GCM_VALIDATE_RET ( ctx != NULL );
533
542
GCM_VALIDATE_RET ( tag != NULL );
534
543
@@ -581,6 +590,11 @@ int mbedtls_gcm_crypt_and_tag( mbedtls_gcm_context *ctx,
581
590
size_t tag_len ,
582
591
unsigned char * tag )
583
592
{
593
+ int32_t plen_cur ;
594
+ int32_t len , len_aligned ;
595
+ const uint8_t * pin ;
596
+ uint8_t * pout ;
597
+
584
598
int ret = MBEDTLS_ERR_GCM_AUTH_FAILED ;
585
599
586
600
GCM_VALIDATE_RET ( ctx != NULL );
@@ -589,13 +603,37 @@ int mbedtls_gcm_crypt_and_tag( mbedtls_gcm_context *ctx,
589
603
GCM_VALIDATE_RET ( length == 0 || input != NULL );
590
604
GCM_VALIDATE_RET ( length == 0 || output != NULL );
591
605
GCM_VALIDATE_RET ( tag != NULL );
592
-
606
+ ctx -> pcntLen = length ;
593
607
if ( ( ret = mbedtls_gcm_starts ( ctx , mode , iv , iv_len , add , add_len ) ) != 0 )
594
608
return ( ret );
595
609
596
- if ( ( ret = mbedtls_gcm_update ( ctx , length , input , output ) ) != 0 )
597
- return ( ret );
610
+ if ( length == 0 ) /* if P length > 0, mbedtls_gcm_update not need gcm_buf_bytes for AES_CNT */
611
+ {
612
+ ctx -> gcm_buf_bytes = 16 ;
613
+ }else {
614
+ ctx -> gcm_buf_bytes = 0 ;
615
+ }
616
+ plen_cur = length ;
617
+ pin = input ;
618
+ pout = output ;
619
+ do
620
+ {
621
+ len = plen_cur ;
622
+ if (len > GCM_PBLOCK_SIZE )
623
+ {
624
+ len = GCM_PBLOCK_SIZE ;
625
+ }
626
+ plen_cur -= len ;
627
+
628
+ /* Prepare the blocked buffer for GCM */
629
+ memcpy (ctx -> gcm_buf , pin , len );
598
630
631
+ if ( ( ret = mbedtls_gcm_update ( ctx , len , pin , pout ) ) != 0 )
632
+ return ( ret );
633
+ pin += len ;
634
+ pout += len ;
635
+ }while (plen_cur );
636
+
599
637
if ( ( ret = mbedtls_gcm_finish ( ctx , tag , tag_len ) ) != 0 )
600
638
return ( ret );
601
639
@@ -665,7 +703,8 @@ static int32_t _GCMTag(mbedtls_gcm_context *ctx, const uint8_t *iv, uint32_t ivl
665
703
uint32_t u32OptBasic ;
666
704
uint32_t u32OptKeySize ;
667
705
uint32_t tag [4 ];
668
-
706
+
707
+ Debug_GCM_Info (("## FUNC: %s\n" , __FUNCTION__ ));
669
708
/* Prepare key size option */
670
709
i = ctx -> keySize >> 3 ;
671
710
u32OptKeySize = (((i >> 2 ) << 1 ) | (i & 1 )) << CRPT_AES_CTL_KEYSZ_Pos ;
@@ -712,10 +751,7 @@ static int32_t _GCMTag(mbedtls_gcm_context *ctx, const uint8_t *iv, uint32_t ivl
712
751
CRPT -> AES_DADDR = (uint32_t )& ghashbuf [0 ];
713
752
CRPT -> AES_CNT = len ;
714
753
715
-
716
754
AES_Run (ctx , u32OptBasic | GHASH_MODE | DMAEN /*| DMALAST*/ );
717
-
718
-
719
755
}
720
756
else
721
757
{
@@ -855,7 +891,8 @@ static int32_t _GCMTag(mbedtls_gcm_context *ctx, const uint8_t *iv, uint32_t ivl
855
891
ret = AES_Run (ctx , u32OptBasic | CTR_MODE | DMAEN /*| DMALAST*/ );
856
892
857
893
memcpy (tagbuf , tag , 16 );
858
-
894
+ Debug_GCM_Info (("## FUNC: %s finish tag 0x%x, 0x%x, 0x%x, 0x%x\n" , __FUNCTION__ , tag [0 ], tag [1 ], tag [2 ], tag [3 ]));
895
+
859
896
return ret ;
860
897
}
861
898
@@ -891,7 +928,8 @@ static int32_t _GCM(mbedtls_gcm_context *ctx, const uint8_t *iv, uint32_t ivlen,
891
928
CRPT -> AES_GCM_PCNT [1 ] = 0 ;
892
929
893
930
plen_aligned = (plen & 0xful ) ? ((plen + 16 ) / 16 ) * 16 : plen ;
894
- if (plen <= GCM_PBLOCK_SIZE )
931
+
932
+ if (plen == 0 ) /* For AWS-IoT connection case, force go cascade instead of if(plen <= GCM_PBLOCK_SIZE) */
895
933
{
896
934
/* Just one shot */
897
935
@@ -903,10 +941,10 @@ static int32_t _GCM(mbedtls_gcm_context *ctx, const uint8_t *iv, uint32_t ivlen,
903
941
CRPT -> AES_CNT = size ;
904
942
905
943
ret = AES_Run (ctx , u32OptBasic | GCM_MODE | DMAEN );
906
-
944
+
907
945
memcpy (buf , ctx -> out_buf , plen );
908
946
memcpy (tag , ctx -> out_buf + plen_aligned , tag_len );
909
-
947
+
910
948
}
911
949
else
912
950
{
@@ -980,13 +1018,15 @@ static int32_t _GCM(mbedtls_gcm_context *ctx, const uint8_t *iv, uint32_t ivlen,
980
1018
}
981
1019
982
1020
memcpy (tag , ctx -> out_buf + len_aligned , tag_len );
1021
+ Debug_GCM_Info (("## Tag in FUNC: %s, plen=%d, tag=0x%x, 0x%x, 0x%x, 0x%x\n" , __FUNCTION__ , plen ,
1022
+ * ((uint32_t * )tag ), * ((uint32_t * )(tag + 4 )), * ((uint32_t * )(tag + 8 )), * ((uint32_t * )(tag + 16 )) ));
983
1023
}
984
1024
985
1025
if (ctx -> mode )
986
1026
{
987
1027
/* H/W limitation under plen%16 as 1 or 15, need re-calculate tag by _GCMTag */
988
1028
/* Need to calculate Tag when plen % 16 == 1 or 15 */
989
- if (((plen & 0xf ) == 1 ) || ((plen & 0xf ) == 15 ))
1029
+ if (( (plen & 0xf ) == 1 ) || ((plen & 0xf ) == 15 ))
990
1030
{
991
1031
if ((ret = _GCMTag (ctx , iv , ivlen , A , alen , ctx -> out_buf , plen , tag )))
992
1032
{
@@ -1011,7 +1051,7 @@ int mbedtls_gcm_crypt_and_tag( mbedtls_gcm_context *ctx,
1011
1051
unsigned char * tag )
1012
1052
{
1013
1053
int ret ;
1014
-
1054
+ Debug_GCM_Info (( "## FUNC: %s, mode=%s, length=%d, tag_len=%d, in/out=0x%x/0x%x\n" , __FUNCTION__ , ( mode ) ? "Enc" : "Dec" , length , tag_len , input , output ));
1015
1055
GCM_VALIDATE_RET ( ctx != NULL );
1016
1056
GCM_VALIDATE_RET ( iv != NULL );
1017
1057
GCM_VALIDATE_RET ( add_len == 0 || add != NULL );
@@ -1042,7 +1082,7 @@ int mbedtls_gcm_crypt_and_tag( mbedtls_gcm_context *ctx,
1042
1082
1043
1083
/* Release ownership of AES H/W */
1044
1084
crypto_aes_release ();
1045
-
1085
+
1046
1086
return (ret );
1047
1087
}
1048
1088
@@ -1061,7 +1101,7 @@ int mbedtls_gcm_auth_decrypt( mbedtls_gcm_context *ctx,
1061
1101
unsigned char check_tag [16 ];
1062
1102
size_t i ;
1063
1103
int diff ;
1064
-
1104
+ Debug_GCM_Info (( "## FUNC: %s\n" , __FUNCTION__ ));
1065
1105
GCM_VALIDATE_RET ( ctx != NULL );
1066
1106
GCM_VALIDATE_RET ( iv != NULL );
1067
1107
GCM_VALIDATE_RET ( add_len == 0 || add != NULL );
0 commit comments