33
33
#include "mbedtls/platform.h"
34
34
#include "mbedtls/platform_util.h"
35
35
#include "mbedtls/error.h"
36
+
37
+ #if defined(__aarch64__ )
38
+ #if !defined(MBEDTLS_AESCE_C ) && defined(MBEDTLS_AES_USE_HARDWARE_ONLY )
39
+ #error "MBEDTLS_AES_USE_HARDWARE_ONLY defined, but not all prerequisites"
40
+ #endif
41
+ #endif
42
+
43
+ #if defined(__amd64__ ) || defined(__x86_64__ ) || \
44
+ ((defined(_M_X64 ) || defined(_M_AMD64 )) && !defined(_M_ARM64EC ))
45
+ #if !defined(MBEDTLS_AESNI_C ) && defined(MBEDTLS_AES_USE_HARDWARE_ONLY )
46
+ #error "MBEDTLS_AES_USE_HARDWARE_ONLY defined, but not all prerequisites"
47
+ #endif
48
+ #endif
49
+
50
+ #if defined(__i386__ ) || defined(_M_IX86 )
51
+ #if defined(MBEDTLS_AES_USE_HARDWARE_ONLY ) && !defined(MBEDTLS_AESNI_C )
52
+ #error "MBEDTLS_AES_USE_HARDWARE_ONLY defined, but not all prerequisites"
53
+ #endif
54
+
55
+ #if defined(MBEDTLS_PADLOCK_C )
56
+ #if !defined(MBEDTLS_HAVE_ASM )
57
+ #error "MBEDTLS_PADLOCK_C defined, but not all prerequisites"
58
+ #endif
59
+ #if defined(MBEDTLS_AES_USE_HARDWARE_ONLY )
60
+ #error "MBEDTLS_AES_USE_HARDWARE_ONLY cannot be defined when " \
61
+ "MBEDTLS_PADLOCK_C is set"
62
+ #endif
63
+ #endif
64
+ #endif
65
+
36
66
#if defined(MBEDTLS_PADLOCK_C )
37
67
#include "padlock.h"
38
68
#endif
47
77
48
78
#if !defined(MBEDTLS_AES_ALT )
49
79
50
- #if defined(MBEDTLS_PADLOCK_C ) && defined( MBEDTLS_HAVE_X86 )
80
+ #if defined(MBEDTLS_VIA_PADLOCK_HAVE_CODE )
51
81
static int aes_padlock_ace = -1 ;
52
82
#endif
53
83
@@ -542,7 +572,7 @@ void mbedtls_aes_xts_free(mbedtls_aes_xts_context *ctx)
542
572
* Note that the offset is in units of elements of buf, i.e. 32-bit words,
543
573
* i.e. an offset of 1 means 4 bytes and so on.
544
574
*/
545
- #if (defined(MBEDTLS_PADLOCK_C ) && defined( MBEDTLS_HAVE_X86 )) || \
575
+ #if (defined(MBEDTLS_VIA_PADLOCK_HAVE_CODE )) || \
546
576
(defined(MBEDTLS_AESNI_C ) && MBEDTLS_AESNI_HAVE_CODE == 2 )
547
577
#define MAY_NEED_TO_ALIGN
548
578
#endif
@@ -554,7 +584,7 @@ static unsigned mbedtls_aes_rk_offset(uint32_t *buf)
554
584
#if defined(MAY_NEED_TO_ALIGN )
555
585
int align_16_bytes = 0 ;
556
586
557
- #if defined(MBEDTLS_PADLOCK_C ) && defined( MBEDTLS_HAVE_X86 )
587
+ #if defined(MBEDTLS_VIA_PADLOCK_HAVE_CODE )
558
588
if (aes_padlock_ace == -1 ) {
559
589
aes_padlock_ace = mbedtls_padlock_has_support (MBEDTLS_PADLOCK_ACE );
560
590
}
@@ -595,7 +625,6 @@ static unsigned mbedtls_aes_rk_offset(uint32_t *buf)
595
625
int mbedtls_aes_setkey_enc (mbedtls_aes_context * ctx , const unsigned char * key ,
596
626
unsigned int keybits )
597
627
{
598
- unsigned int i ;
599
628
uint32_t * RK ;
600
629
601
630
switch (keybits ) {
@@ -629,14 +658,15 @@ int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx, const unsigned char *key,
629
658
}
630
659
#endif
631
660
632
- for (i = 0 ; i < (keybits >> 5 ); i ++ ) {
661
+ #if !defined(MBEDTLS_AES_USE_HARDWARE_ONLY )
662
+ for (unsigned int i = 0 ; i < (keybits >> 5 ); i ++ ) {
633
663
RK [i ] = MBEDTLS_GET_UINT32_LE (key , i << 2 );
634
664
}
635
665
636
666
switch (ctx -> nr ) {
637
667
case 10 :
638
668
639
- for (i = 0 ; i < 10 ; i ++ , RK += 4 ) {
669
+ for (unsigned int i = 0 ; i < 10 ; i ++ , RK += 4 ) {
640
670
RK [4 ] = RK [0 ] ^ RCON [i ] ^
641
671
((uint32_t ) FSb [MBEDTLS_BYTE_1 (RK [3 ])]) ^
642
672
((uint32_t ) FSb [MBEDTLS_BYTE_2 (RK [3 ])] << 8 ) ^
@@ -652,7 +682,7 @@ int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx, const unsigned char *key,
652
682
#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH )
653
683
case 12 :
654
684
655
- for (i = 0 ; i < 8 ; i ++ , RK += 6 ) {
685
+ for (unsigned int i = 0 ; i < 8 ; i ++ , RK += 6 ) {
656
686
RK [6 ] = RK [0 ] ^ RCON [i ] ^
657
687
((uint32_t ) FSb [MBEDTLS_BYTE_1 (RK [5 ])]) ^
658
688
((uint32_t ) FSb [MBEDTLS_BYTE_2 (RK [5 ])] << 8 ) ^
@@ -669,7 +699,7 @@ int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx, const unsigned char *key,
669
699
670
700
case 14 :
671
701
672
- for (i = 0 ; i < 7 ; i ++ , RK += 8 ) {
702
+ for (unsigned int i = 0 ; i < 7 ; i ++ , RK += 8 ) {
673
703
RK [8 ] = RK [0 ] ^ RCON [i ] ^
674
704
((uint32_t ) FSb [MBEDTLS_BYTE_1 (RK [7 ])]) ^
675
705
((uint32_t ) FSb [MBEDTLS_BYTE_2 (RK [7 ])] << 8 ) ^
@@ -695,6 +725,7 @@ int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx, const unsigned char *key,
695
725
}
696
726
697
727
return 0 ;
728
+ #endif /* !MBEDTLS_AES_USE_HARDWARE_ONLY */
698
729
}
699
730
#endif /* !MBEDTLS_AES_SETKEY_ENC_ALT */
700
731
@@ -705,10 +736,13 @@ int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx, const unsigned char *key,
705
736
int mbedtls_aes_setkey_dec (mbedtls_aes_context * ctx , const unsigned char * key ,
706
737
unsigned int keybits )
707
738
{
708
- int i , j , ret ;
739
+ #if !defined(MBEDTLS_AES_USE_HARDWARE_ONLY )
740
+ uint32_t * SK ;
741
+ #endif
742
+ int ret ;
709
743
mbedtls_aes_context cty ;
710
744
uint32_t * RK ;
711
- uint32_t * SK ;
745
+
712
746
713
747
mbedtls_aes_init (& cty );
714
748
@@ -740,15 +774,16 @@ int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx, const unsigned char *key,
740
774
}
741
775
#endif
742
776
777
+ #if !defined(MBEDTLS_AES_USE_HARDWARE_ONLY )
743
778
SK = cty .buf + cty .rk_offset + cty .nr * 4 ;
744
779
745
780
* RK ++ = * SK ++ ;
746
781
* RK ++ = * SK ++ ;
747
782
* RK ++ = * SK ++ ;
748
783
* RK ++ = * SK ++ ;
749
-
750
- for (i = ctx -> nr - 1 , SK -= 8 ; i > 0 ; i -- , SK -= 8 ) {
751
- for (j = 0 ; j < 4 ; j ++ , SK ++ ) {
784
+ SK -= 8 ;
785
+ for (int i = ctx -> nr - 1 ; i > 0 ; i -- , SK -= 8 ) {
786
+ for (int j = 0 ; j < 4 ; j ++ , SK ++ ) {
752
787
* RK ++ = AES_RT0 (FSb [MBEDTLS_BYTE_0 (* SK )]) ^
753
788
AES_RT1 (FSb [MBEDTLS_BYTE_1 (* SK )]) ^
754
789
AES_RT2 (FSb [MBEDTLS_BYTE_2 (* SK )]) ^
@@ -760,7 +795,7 @@ int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx, const unsigned char *key,
760
795
* RK ++ = * SK ++ ;
761
796
* RK ++ = * SK ++ ;
762
797
* RK ++ = * SK ++ ;
763
-
798
+ #endif /* !MBEDTLS_AES_USE_HARDWARE_ONLY */
764
799
exit :
765
800
mbedtls_aes_free (& cty );
766
801
@@ -1062,17 +1097,20 @@ int mbedtls_aes_crypt_ecb(mbedtls_aes_context *ctx,
1062
1097
}
1063
1098
#endif
1064
1099
1065
- #if defined(MBEDTLS_PADLOCK_C ) && defined( MBEDTLS_HAVE_X86 )
1100
+ #if defined(MBEDTLS_VIA_PADLOCK_HAVE_CODE )
1066
1101
if (aes_padlock_ace > 0 ) {
1067
1102
return mbedtls_padlock_xcryptecb (ctx , mode , input , output );
1068
1103
}
1069
1104
#endif
1070
1105
1106
+ #if !defined(MBEDTLS_AES_USE_HARDWARE_ONLY )
1071
1107
if (mode == MBEDTLS_AES_ENCRYPT ) {
1072
1108
return mbedtls_internal_aes_encrypt (ctx , input , output );
1073
1109
} else {
1074
1110
return mbedtls_internal_aes_decrypt (ctx , input , output );
1075
1111
}
1112
+ #endif
1113
+
1076
1114
}
1077
1115
1078
1116
#if defined(MBEDTLS_CIPHER_MODE_CBC )
@@ -1103,7 +1141,7 @@ int mbedtls_aes_crypt_cbc(mbedtls_aes_context *ctx,
1103
1141
return MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH ;
1104
1142
}
1105
1143
1106
- #if defined(MBEDTLS_PADLOCK_C ) && defined( MBEDTLS_HAVE_X86 )
1144
+ #if defined(MBEDTLS_VIA_PADLOCK_HAVE_CODE )
1107
1145
if (aes_padlock_ace > 0 ) {
1108
1146
if (mbedtls_padlock_xcryptcbc (ctx , mode , length , iv , input , output ) == 0 ) {
1109
1147
return 0 ;
@@ -1855,11 +1893,6 @@ int mbedtls_aes_self_test(int verbose)
1855
1893
#if defined(MBEDTLS_AES_ALT )
1856
1894
mbedtls_printf (" AES note: alternative implementation.\n" );
1857
1895
#else /* MBEDTLS_AES_ALT */
1858
- #if defined(MBEDTLS_PADLOCK_C ) && defined(MBEDTLS_HAVE_X86 )
1859
- if (mbedtls_padlock_has_support (MBEDTLS_PADLOCK_ACE )) {
1860
- mbedtls_printf (" AES note: using VIA Padlock.\n" );
1861
- } else
1862
- #endif
1863
1896
#if defined(MBEDTLS_AESNI_HAVE_CODE )
1864
1897
#if MBEDTLS_AESNI_HAVE_CODE == 1
1865
1898
mbedtls_printf (" AES note: AESNI code present (assembly implementation).\n" );
@@ -1872,12 +1905,21 @@ int mbedtls_aes_self_test(int verbose)
1872
1905
mbedtls_printf (" AES note: using AESNI.\n" );
1873
1906
} else
1874
1907
#endif
1908
+ #if defined(MBEDTLS_VIA_PADLOCK_HAVE_CODE )
1909
+ if (mbedtls_padlock_has_support (MBEDTLS_PADLOCK_ACE )) {
1910
+ mbedtls_printf (" AES note: using VIA Padlock.\n" );
1911
+ } else
1912
+ #endif
1875
1913
#if defined(MBEDTLS_AESCE_C ) && defined(MBEDTLS_HAVE_ARM64 )
1876
1914
if (mbedtls_aesce_has_support ()) {
1877
1915
mbedtls_printf (" AES note: using AESCE.\n" );
1878
1916
} else
1879
1917
#endif
1880
- mbedtls_printf (" AES note: built-in implementation.\n" );
1918
+ {
1919
+ #if !defined(MBEDTLS_AES_USE_HARDWARE_ONLY )
1920
+ mbedtls_printf (" AES note: built-in implementation.\n" );
1921
+ #endif
1922
+ }
1881
1923
#endif /* MBEDTLS_AES_ALT */
1882
1924
}
1883
1925
0 commit comments