diff options
author | Igor Pavlov <87184205+ip7z@users.noreply.github.com> | 2023-06-21 00:00:00 +0000 |
---|---|---|
committer | Igor Pavlov <87184205+ip7z@users.noreply.github.com> | 2023-12-17 14:59:19 +0500 |
commit | 5b39dc76f1bc82f941d5c800ab9f34407a06b53a (patch) | |
tree | fe5e17420300b715021a76328444088d32047963 /C/Aes.c | |
parent | 93be7d4abfd4233228f58ee1fbbcd76d91be66a4 (diff) | |
download | 7zip-5b39dc76f1bc82f941d5c800ab9f34407a06b53a.tar.gz 7zip-5b39dc76f1bc82f941d5c800ab9f34407a06b53a.tar.bz2 7zip-5b39dc76f1bc82f941d5c800ab9f34407a06b53a.zip |
23.0123.01
Diffstat (limited to 'C/Aes.c')
-rw-r--r-- | C/Aes.c | 108 |
1 files changed, 63 insertions, 45 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* Aes.c -- AES encryption / decryption | 1 | /* Aes.c -- AES encryption / decryption |
2 | 2021-05-13 : Igor Pavlov : Public domain */ | 2 | 2023-04-02 : Igor Pavlov : Public domain */ |
3 | 3 | ||
4 | #include "Precomp.h" | 4 | #include "Precomp.h" |
5 | 5 | ||
@@ -7,7 +7,7 @@ | |||
7 | #include "Aes.h" | 7 | #include "Aes.h" |
8 | 8 | ||
9 | AES_CODE_FUNC g_AesCbc_Decode; | 9 | AES_CODE_FUNC g_AesCbc_Decode; |
10 | #ifndef _SFX | 10 | #ifndef Z7_SFX |
11 | AES_CODE_FUNC g_AesCbc_Encode; | 11 | AES_CODE_FUNC g_AesCbc_Encode; |
12 | AES_CODE_FUNC g_AesCtr_Code; | 12 | AES_CODE_FUNC g_AesCtr_Code; |
13 | UInt32 g_Aes_SupportedFunctions_Flags; | 13 | UInt32 g_Aes_SupportedFunctions_Flags; |
@@ -51,7 +51,7 @@ static Byte InvS[256]; | |||
51 | #define DD(x) (D + (x << 8)) | 51 | #define DD(x) (D + (x << 8)) |
52 | 52 | ||
53 | 53 | ||
54 | // #define _SHOW_AES_STATUS | 54 | // #define Z7_SHOW_AES_STATUS |
55 | 55 | ||
56 | #ifdef MY_CPU_X86_OR_AMD64 | 56 | #ifdef MY_CPU_X86_OR_AMD64 |
57 | #define USE_HW_AES | 57 | #define USE_HW_AES |
@@ -72,11 +72,11 @@ static Byte InvS[256]; | |||
72 | #endif | 72 | #endif |
73 | 73 | ||
74 | #ifdef USE_HW_AES | 74 | #ifdef USE_HW_AES |
75 | #ifdef _SHOW_AES_STATUS | 75 | #ifdef Z7_SHOW_AES_STATUS |
76 | #include <stdio.h> | 76 | #include <stdio.h> |
77 | #define _PRF(x) x | 77 | #define PRF(x) x |
78 | #else | 78 | #else |
79 | #define _PRF(x) | 79 | #define PRF(x) |
80 | #endif | 80 | #endif |
81 | #endif | 81 | #endif |
82 | 82 | ||
@@ -90,23 +90,23 @@ void AesGenTables(void) | |||
90 | for (i = 0; i < 256; i++) | 90 | for (i = 0; i < 256; i++) |
91 | { | 91 | { |
92 | { | 92 | { |
93 | UInt32 a1 = Sbox[i]; | 93 | const UInt32 a1 = Sbox[i]; |
94 | UInt32 a2 = xtime(a1); | 94 | const UInt32 a2 = xtime(a1); |
95 | UInt32 a3 = a2 ^ a1; | 95 | const UInt32 a3 = a2 ^ a1; |
96 | TT(0)[i] = Ui32(a2, a1, a1, a3); | 96 | TT(0)[i] = Ui32(a2, a1, a1, a3); |
97 | TT(1)[i] = Ui32(a3, a2, a1, a1); | 97 | TT(1)[i] = Ui32(a3, a2, a1, a1); |
98 | TT(2)[i] = Ui32(a1, a3, a2, a1); | 98 | TT(2)[i] = Ui32(a1, a3, a2, a1); |
99 | TT(3)[i] = Ui32(a1, a1, a3, a2); | 99 | TT(3)[i] = Ui32(a1, a1, a3, a2); |
100 | } | 100 | } |
101 | { | 101 | { |
102 | UInt32 a1 = InvS[i]; | 102 | const UInt32 a1 = InvS[i]; |
103 | UInt32 a2 = xtime(a1); | 103 | const UInt32 a2 = xtime(a1); |
104 | UInt32 a4 = xtime(a2); | 104 | const UInt32 a4 = xtime(a2); |
105 | UInt32 a8 = xtime(a4); | 105 | const UInt32 a8 = xtime(a4); |
106 | UInt32 a9 = a8 ^ a1; | 106 | const UInt32 a9 = a8 ^ a1; |
107 | UInt32 aB = a8 ^ a2 ^ a1; | 107 | const UInt32 aB = a8 ^ a2 ^ a1; |
108 | UInt32 aD = a8 ^ a4 ^ a1; | 108 | const UInt32 aD = a8 ^ a4 ^ a1; |
109 | UInt32 aE = a8 ^ a4 ^ a2; | 109 | const UInt32 aE = a8 ^ a4 ^ a2; |
110 | DD(0)[i] = Ui32(aE, a9, aD, aB); | 110 | DD(0)[i] = Ui32(aE, a9, aD, aB); |
111 | DD(1)[i] = Ui32(aB, aE, a9, aD); | 111 | DD(1)[i] = Ui32(aB, aE, a9, aD); |
112 | DD(2)[i] = Ui32(aD, aB, aE, a9); | 112 | DD(2)[i] = Ui32(aD, aB, aE, a9); |
@@ -116,7 +116,7 @@ void AesGenTables(void) | |||
116 | 116 | ||
117 | { | 117 | { |
118 | AES_CODE_FUNC d = AesCbc_Decode; | 118 | AES_CODE_FUNC d = AesCbc_Decode; |
119 | #ifndef _SFX | 119 | #ifndef Z7_SFX |
120 | AES_CODE_FUNC e = AesCbc_Encode; | 120 | AES_CODE_FUNC e = AesCbc_Encode; |
121 | AES_CODE_FUNC c = AesCtr_Code; | 121 | AES_CODE_FUNC c = AesCtr_Code; |
122 | UInt32 flags = 0; | 122 | UInt32 flags = 0; |
@@ -126,10 +126,10 @@ void AesGenTables(void) | |||
126 | if (CPU_IsSupported_AES()) | 126 | if (CPU_IsSupported_AES()) |
127 | { | 127 | { |
128 | // #pragma message ("AES HW") | 128 | // #pragma message ("AES HW") |
129 | _PRF(printf("\n===AES HW\n")); | 129 | PRF(printf("\n===AES HW\n")); |
130 | d = AesCbc_Decode_HW; | 130 | d = AesCbc_Decode_HW; |
131 | 131 | ||
132 | #ifndef _SFX | 132 | #ifndef Z7_SFX |
133 | e = AesCbc_Encode_HW; | 133 | e = AesCbc_Encode_HW; |
134 | c = AesCtr_Code_HW; | 134 | c = AesCtr_Code_HW; |
135 | flags = k_Aes_SupportedFunctions_HW; | 135 | flags = k_Aes_SupportedFunctions_HW; |
@@ -138,9 +138,9 @@ void AesGenTables(void) | |||
138 | #ifdef MY_CPU_X86_OR_AMD64 | 138 | #ifdef MY_CPU_X86_OR_AMD64 |
139 | if (CPU_IsSupported_VAES_AVX2()) | 139 | if (CPU_IsSupported_VAES_AVX2()) |
140 | { | 140 | { |
141 | _PRF(printf("\n===vaes avx2\n")); | 141 | PRF(printf("\n===vaes avx2\n")); |
142 | d = AesCbc_Decode_HW_256; | 142 | d = AesCbc_Decode_HW_256; |
143 | #ifndef _SFX | 143 | #ifndef Z7_SFX |
144 | c = AesCtr_Code_HW_256; | 144 | c = AesCtr_Code_HW_256; |
145 | flags |= k_Aes_SupportedFunctions_HW_256; | 145 | flags |= k_Aes_SupportedFunctions_HW_256; |
146 | #endif | 146 | #endif |
@@ -150,7 +150,7 @@ void AesGenTables(void) | |||
150 | #endif | 150 | #endif |
151 | 151 | ||
152 | g_AesCbc_Decode = d; | 152 | g_AesCbc_Decode = d; |
153 | #ifndef _SFX | 153 | #ifndef Z7_SFX |
154 | g_AesCbc_Encode = e; | 154 | g_AesCbc_Encode = e; |
155 | g_AesCtr_Code = c; | 155 | g_AesCtr_Code = c; |
156 | g_Aes_SupportedFunctions_Flags = flags; | 156 | g_Aes_SupportedFunctions_Flags = flags; |
@@ -194,7 +194,7 @@ void AesGenTables(void) | |||
194 | #define FD(i, x) InvS[gb(x, m[(i - x) & 3])] | 194 | #define FD(i, x) InvS[gb(x, m[(i - x) & 3])] |
195 | #define FD4(i) dest[i] = Ui32(FD(i, 0), FD(i, 1), FD(i, 2), FD(i, 3)) ^ w[i]; | 195 | #define FD4(i) dest[i] = Ui32(FD(i, 0), FD(i, 1), FD(i, 2), FD(i, 3)) ^ w[i]; |
196 | 196 | ||
197 | void MY_FAST_CALL Aes_SetKey_Enc(UInt32 *w, const Byte *key, unsigned keySize) | 197 | void Z7_FASTCALL Aes_SetKey_Enc(UInt32 *w, const Byte *key, unsigned keySize) |
198 | { | 198 | { |
199 | unsigned i, m; | 199 | unsigned i, m; |
200 | const UInt32 *wLim; | 200 | const UInt32 *wLim; |
@@ -230,7 +230,7 @@ void MY_FAST_CALL Aes_SetKey_Enc(UInt32 *w, const Byte *key, unsigned keySize) | |||
230 | while (++w != wLim); | 230 | while (++w != wLim); |
231 | } | 231 | } |
232 | 232 | ||
233 | void MY_FAST_CALL Aes_SetKey_Dec(UInt32 *w, const Byte *key, unsigned keySize) | 233 | void Z7_FASTCALL Aes_SetKey_Dec(UInt32 *w, const Byte *key, unsigned keySize) |
234 | { | 234 | { |
235 | unsigned i, num; | 235 | unsigned i, num; |
236 | Aes_SetKey_Enc(w, key, keySize); | 236 | Aes_SetKey_Enc(w, key, keySize); |
@@ -251,7 +251,7 @@ void MY_FAST_CALL Aes_SetKey_Dec(UInt32 *w, const Byte *key, unsigned keySize) | |||
251 | src and dest are pointers to 4 UInt32 words. | 251 | src and dest are pointers to 4 UInt32 words. |
252 | src and dest can point to same block */ | 252 | src and dest can point to same block */ |
253 | 253 | ||
254 | // MY_FORCE_INLINE | 254 | // Z7_FORCE_INLINE |
255 | static void Aes_Encode(const UInt32 *w, UInt32 *dest, const UInt32 *src) | 255 | static void Aes_Encode(const UInt32 *w, UInt32 *dest, const UInt32 *src) |
256 | { | 256 | { |
257 | UInt32 s[4]; | 257 | UInt32 s[4]; |
@@ -265,17 +265,20 @@ static void Aes_Encode(const UInt32 *w, UInt32 *dest, const UInt32 *src) | |||
265 | w += 4; | 265 | w += 4; |
266 | for (;;) | 266 | for (;;) |
267 | { | 267 | { |
268 | HT16(m, s, 0); | 268 | HT16(m, s, 0) |
269 | if (--numRounds2 == 0) | 269 | if (--numRounds2 == 0) |
270 | break; | 270 | break; |
271 | HT16(s, m, 4); | 271 | HT16(s, m, 4) |
272 | w += 8; | 272 | w += 8; |
273 | } | 273 | } |
274 | w += 4; | 274 | w += 4; |
275 | FT4(0); FT4(1); FT4(2); FT4(3); | 275 | FT4(0) |
276 | FT4(1) | ||
277 | FT4(2) | ||
278 | FT4(3) | ||
276 | } | 279 | } |
277 | 280 | ||
278 | MY_FORCE_INLINE | 281 | Z7_FORCE_INLINE |
279 | static void Aes_Decode(const UInt32 *w, UInt32 *dest, const UInt32 *src) | 282 | static void Aes_Decode(const UInt32 *w, UInt32 *dest, const UInt32 *src) |
280 | { | 283 | { |
281 | UInt32 s[4]; | 284 | UInt32 s[4]; |
@@ -289,12 +292,15 @@ static void Aes_Decode(const UInt32 *w, UInt32 *dest, const UInt32 *src) | |||
289 | for (;;) | 292 | for (;;) |
290 | { | 293 | { |
291 | w -= 8; | 294 | w -= 8; |
292 | HD16(m, s, 4); | 295 | HD16(m, s, 4) |
293 | if (--numRounds2 == 0) | 296 | if (--numRounds2 == 0) |
294 | break; | 297 | break; |
295 | HD16(s, m, 0); | 298 | HD16(s, m, 0) |
296 | } | 299 | } |
297 | FD4(0); FD4(1); FD4(2); FD4(3); | 300 | FD4(0) |
301 | FD4(1) | ||
302 | FD4(2) | ||
303 | FD4(3) | ||
298 | } | 304 | } |
299 | 305 | ||
300 | void AesCbc_Init(UInt32 *p, const Byte *iv) | 306 | void AesCbc_Init(UInt32 *p, const Byte *iv) |
@@ -304,7 +310,7 @@ void AesCbc_Init(UInt32 *p, const Byte *iv) | |||
304 | p[i] = GetUi32(iv + i * 4); | 310 | p[i] = GetUi32(iv + i * 4); |
305 | } | 311 | } |
306 | 312 | ||
307 | void MY_FAST_CALL AesCbc_Encode(UInt32 *p, Byte *data, size_t numBlocks) | 313 | void Z7_FASTCALL AesCbc_Encode(UInt32 *p, Byte *data, size_t numBlocks) |
308 | { | 314 | { |
309 | for (; numBlocks != 0; numBlocks--, data += AES_BLOCK_SIZE) | 315 | for (; numBlocks != 0; numBlocks--, data += AES_BLOCK_SIZE) |
310 | { | 316 | { |
@@ -315,14 +321,14 @@ void MY_FAST_CALL AesCbc_Encode(UInt32 *p, Byte *data, size_t numBlocks) | |||
315 | 321 | ||
316 | Aes_Encode(p + 4, p, p); | 322 | Aes_Encode(p + 4, p, p); |
317 | 323 | ||
318 | SetUi32(data, p[0]); | 324 | SetUi32(data, p[0]) |
319 | SetUi32(data + 4, p[1]); | 325 | SetUi32(data + 4, p[1]) |
320 | SetUi32(data + 8, p[2]); | 326 | SetUi32(data + 8, p[2]) |
321 | SetUi32(data + 12, p[3]); | 327 | SetUi32(data + 12, p[3]) |
322 | } | 328 | } |
323 | } | 329 | } |
324 | 330 | ||
325 | void MY_FAST_CALL AesCbc_Decode(UInt32 *p, Byte *data, size_t numBlocks) | 331 | void Z7_FASTCALL AesCbc_Decode(UInt32 *p, Byte *data, size_t numBlocks) |
326 | { | 332 | { |
327 | UInt32 in[4], out[4]; | 333 | UInt32 in[4], out[4]; |
328 | for (; numBlocks != 0; numBlocks--, data += AES_BLOCK_SIZE) | 334 | for (; numBlocks != 0; numBlocks--, data += AES_BLOCK_SIZE) |
@@ -334,10 +340,10 @@ void MY_FAST_CALL AesCbc_Decode(UInt32 *p, Byte *data, size_t numBlocks) | |||
334 | 340 | ||
335 | Aes_Decode(p + 4, out, in); | 341 | Aes_Decode(p + 4, out, in); |
336 | 342 | ||
337 | SetUi32(data, p[0] ^ out[0]); | 343 | SetUi32(data, p[0] ^ out[0]) |
338 | SetUi32(data + 4, p[1] ^ out[1]); | 344 | SetUi32(data + 4, p[1] ^ out[1]) |
339 | SetUi32(data + 8, p[2] ^ out[2]); | 345 | SetUi32(data + 8, p[2] ^ out[2]) |
340 | SetUi32(data + 12, p[3] ^ out[3]); | 346 | SetUi32(data + 12, p[3] ^ out[3]) |
341 | 347 | ||
342 | p[0] = in[0]; | 348 | p[0] = in[0]; |
343 | p[1] = in[1]; | 349 | p[1] = in[1]; |
@@ -346,7 +352,7 @@ void MY_FAST_CALL AesCbc_Decode(UInt32 *p, Byte *data, size_t numBlocks) | |||
346 | } | 352 | } |
347 | } | 353 | } |
348 | 354 | ||
349 | void MY_FAST_CALL AesCtr_Code(UInt32 *p, Byte *data, size_t numBlocks) | 355 | void Z7_FASTCALL AesCtr_Code(UInt32 *p, Byte *data, size_t numBlocks) |
350 | { | 356 | { |
351 | for (; numBlocks != 0; numBlocks--) | 357 | for (; numBlocks != 0; numBlocks--) |
352 | { | 358 | { |
@@ -360,7 +366,7 @@ void MY_FAST_CALL AesCtr_Code(UInt32 *p, Byte *data, size_t numBlocks) | |||
360 | 366 | ||
361 | for (i = 0; i < 4; i++, data += 4) | 367 | for (i = 0; i < 4; i++, data += 4) |
362 | { | 368 | { |
363 | UInt32 t = temp[i]; | 369 | const UInt32 t = temp[i]; |
364 | 370 | ||
365 | #ifdef MY_CPU_LE_UNALIGN | 371 | #ifdef MY_CPU_LE_UNALIGN |
366 | *((UInt32 *)(void *)data) ^= t; | 372 | *((UInt32 *)(void *)data) ^= t; |
@@ -373,3 +379,15 @@ void MY_FAST_CALL AesCtr_Code(UInt32 *p, Byte *data, size_t numBlocks) | |||
373 | } | 379 | } |
374 | } | 380 | } |
375 | } | 381 | } |
382 | |||
383 | #undef xtime | ||
384 | #undef Ui32 | ||
385 | #undef gb0 | ||
386 | #undef gb1 | ||
387 | #undef gb2 | ||
388 | #undef gb3 | ||
389 | #undef gb | ||
390 | #undef TT | ||
391 | #undef DD | ||
392 | #undef USE_HW_AES | ||
393 | #undef PRF | ||