aboutsummaryrefslogtreecommitdiff
path: root/C/Aes.c
diff options
context:
space:
mode:
Diffstat (limited to 'C/Aes.c')
-rw-r--r--C/Aes.c56
1 files changed, 46 insertions, 10 deletions
diff --git a/C/Aes.c b/C/Aes.c
index bcaafab..abc5d24 100644
--- a/C/Aes.c
+++ b/C/Aes.c
@@ -1,5 +1,5 @@
1/* Aes.c -- AES encryption / decryption 1/* Aes.c -- AES encryption / decryption
22023-04-02 : Igor Pavlov : Public domain */ 22024-03-01 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
@@ -13,7 +13,9 @@ AES_CODE_FUNC g_AesCtr_Code;
13UInt32 g_Aes_SupportedFunctions_Flags; 13UInt32 g_Aes_SupportedFunctions_Flags;
14#endif 14#endif
15 15
16MY_ALIGN(64)
16static UInt32 T[256 * 4]; 17static UInt32 T[256 * 4];
18MY_ALIGN(64)
17static const Byte Sbox[256] = { 19static const Byte Sbox[256] = {
18 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, 20 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
19 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, 21 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
@@ -33,7 +35,9 @@ static const Byte Sbox[256] = {
33 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16}; 35 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16};
34 36
35 37
38MY_ALIGN(64)
36static UInt32 D[256 * 4]; 39static UInt32 D[256 * 4];
40MY_ALIGN(64)
37static Byte InvS[256]; 41static Byte InvS[256];
38 42
39#define xtime(x) ((((x) << 1) ^ (((x) & 0x80) != 0 ? 0x1B : 0)) & 0xFF) 43#define xtime(x) ((((x) << 1) ^ (((x) & 0x80) != 0 ? 0x1B : 0)) & 0xFF)
@@ -54,24 +58,54 @@ static Byte InvS[256];
54// #define Z7_SHOW_AES_STATUS 58// #define Z7_SHOW_AES_STATUS
55 59
56#ifdef MY_CPU_X86_OR_AMD64 60#ifdef MY_CPU_X86_OR_AMD64
57 #define USE_HW_AES 61
58#elif defined(MY_CPU_ARM_OR_ARM64) && defined(MY_CPU_LE) 62 #if defined(__INTEL_COMPILER)
59 #if defined(__clang__) 63 #if (__INTEL_COMPILER >= 1110)
60 #if (__clang_major__ >= 8) // fix that check
61 #define USE_HW_AES
62 #endif
63 #elif defined(__GNUC__)
64 #if (__GNUC__ >= 6) // fix that check
65 #define USE_HW_AES 64 #define USE_HW_AES
65 #if (__INTEL_COMPILER >= 1900)
66 #define USE_HW_VAES
67 #endif
66 #endif 68 #endif
69 #elif defined(Z7_CLANG_VERSION) && (Z7_CLANG_VERSION >= 30800) \
70 || defined(Z7_GCC_VERSION) && (Z7_GCC_VERSION >= 40400)
71 #define USE_HW_AES
72 #if defined(__clang__) && (__clang_major__ >= 8) \
73 || defined(__GNUC__) && (__GNUC__ >= 8)
74 #define USE_HW_VAES
75 #endif
67 #elif defined(_MSC_VER) 76 #elif defined(_MSC_VER)
68 #if _MSC_VER >= 1910 77 #define USE_HW_AES
78 #define USE_HW_VAES
79 #endif
80
81#elif defined(MY_CPU_ARM_OR_ARM64) && defined(MY_CPU_LE)
82
83 #if defined(__ARM_FEATURE_AES) \
84 || defined(__ARM_FEATURE_CRYPTO)
85 #define USE_HW_AES
86 #else
87 #if defined(MY_CPU_ARM64) \
88 || defined(__ARM_ARCH) && (__ARM_ARCH >= 4) \
89 || defined(Z7_MSC_VER_ORIGINAL)
90 #if defined(__ARM_FP) && \
91 ( defined(Z7_CLANG_VERSION) && (Z7_CLANG_VERSION >= 30800) \
92 || defined(__GNUC__) && (__GNUC__ >= 6) \
93 ) \
94 || defined(Z7_MSC_VER_ORIGINAL) && (_MSC_VER >= 1910)
95 #if defined(MY_CPU_ARM64) \
96 || !defined(Z7_CLANG_VERSION) \
97 || defined(__ARM_NEON) && \
98 (Z7_CLANG_VERSION < 170000 || \
99 Z7_CLANG_VERSION > 170001)
69 #define USE_HW_AES 100 #define USE_HW_AES
70 #endif 101 #endif
102 #endif
103 #endif
71 #endif 104 #endif
72#endif 105#endif
73 106
74#ifdef USE_HW_AES 107#ifdef USE_HW_AES
108// #pragma message("=== Aes.c USE_HW_AES === ")
75#ifdef Z7_SHOW_AES_STATUS 109#ifdef Z7_SHOW_AES_STATUS
76#include <stdio.h> 110#include <stdio.h>
77#define PRF(x) x 111#define PRF(x) x
@@ -136,6 +170,7 @@ void AesGenTables(void)
136 #endif 170 #endif
137 171
138 #ifdef MY_CPU_X86_OR_AMD64 172 #ifdef MY_CPU_X86_OR_AMD64
173 #ifdef USE_HW_VAES
139 if (CPU_IsSupported_VAES_AVX2()) 174 if (CPU_IsSupported_VAES_AVX2())
140 { 175 {
141 PRF(printf("\n===vaes avx2\n")); 176 PRF(printf("\n===vaes avx2\n"));
@@ -146,6 +181,7 @@ void AesGenTables(void)
146 #endif 181 #endif
147 } 182 }
148 #endif 183 #endif
184 #endif
149 } 185 }
150 #endif 186 #endif
151 187