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/Sha256.c | |
parent | 93be7d4abfd4233228f58ee1fbbcd76d91be66a4 (diff) | |
download | 7zip-5b39dc76f1bc82f941d5c800ab9f34407a06b53a.tar.gz 7zip-5b39dc76f1bc82f941d5c800ab9f34407a06b53a.tar.bz2 7zip-5b39dc76f1bc82f941d5c800ab9f34407a06b53a.zip |
23.0123.01
Diffstat (limited to 'C/Sha256.c')
-rw-r--r-- | C/Sha256.c | 146 |
1 files changed, 88 insertions, 58 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* Sha256.c -- SHA-256 Hash | 1 | /* Sha256.c -- SHA-256 Hash |
2 | 2021-04-01 : Igor Pavlov : Public domain | 2 | 2023-04-02 : Igor Pavlov : Public domain |
3 | This code is based on public domain code from Wei Dai's Crypto++ library. */ | 3 | This code is based on public domain code from Wei Dai's Crypto++ library. */ |
4 | 4 | ||
5 | #include "Precomp.h" | 5 | #include "Precomp.h" |
@@ -17,48 +17,48 @@ This code is based on public domain code from Wei Dai's Crypto++ library. */ | |||
17 | #ifdef MY_CPU_X86_OR_AMD64 | 17 | #ifdef MY_CPU_X86_OR_AMD64 |
18 | #ifdef _MSC_VER | 18 | #ifdef _MSC_VER |
19 | #if _MSC_VER >= 1200 | 19 | #if _MSC_VER >= 1200 |
20 | #define _SHA_SUPPORTED | 20 | #define Z7_COMPILER_SHA256_SUPPORTED |
21 | #endif | 21 | #endif |
22 | #elif defined(__clang__) | 22 | #elif defined(__clang__) |
23 | #if (__clang_major__ >= 8) // fix that check | 23 | #if (__clang_major__ >= 8) // fix that check |
24 | #define _SHA_SUPPORTED | 24 | #define Z7_COMPILER_SHA256_SUPPORTED |
25 | #endif | 25 | #endif |
26 | #elif defined(__GNUC__) | 26 | #elif defined(__GNUC__) |
27 | #if (__GNUC__ >= 8) // fix that check | 27 | #if (__GNUC__ >= 8) // fix that check |
28 | #define _SHA_SUPPORTED | 28 | #define Z7_COMPILER_SHA256_SUPPORTED |
29 | #endif | 29 | #endif |
30 | #elif defined(__INTEL_COMPILER) | 30 | #elif defined(__INTEL_COMPILER) |
31 | #if (__INTEL_COMPILER >= 1800) // fix that check | 31 | #if (__INTEL_COMPILER >= 1800) // fix that check |
32 | #define _SHA_SUPPORTED | 32 | #define Z7_COMPILER_SHA256_SUPPORTED |
33 | #endif | 33 | #endif |
34 | #endif | 34 | #endif |
35 | #elif defined(MY_CPU_ARM_OR_ARM64) | 35 | #elif defined(MY_CPU_ARM_OR_ARM64) |
36 | #ifdef _MSC_VER | 36 | #ifdef _MSC_VER |
37 | #if _MSC_VER >= 1910 | 37 | #if _MSC_VER >= 1910 |
38 | #define _SHA_SUPPORTED | 38 | #define Z7_COMPILER_SHA256_SUPPORTED |
39 | #endif | 39 | #endif |
40 | #elif defined(__clang__) | 40 | #elif defined(__clang__) |
41 | #if (__clang_major__ >= 8) // fix that check | 41 | #if (__clang_major__ >= 8) // fix that check |
42 | #define _SHA_SUPPORTED | 42 | #define Z7_COMPILER_SHA256_SUPPORTED |
43 | #endif | 43 | #endif |
44 | #elif defined(__GNUC__) | 44 | #elif defined(__GNUC__) |
45 | #if (__GNUC__ >= 6) // fix that check | 45 | #if (__GNUC__ >= 6) // fix that check |
46 | #define _SHA_SUPPORTED | 46 | #define Z7_COMPILER_SHA256_SUPPORTED |
47 | #endif | 47 | #endif |
48 | #endif | 48 | #endif |
49 | #endif | 49 | #endif |
50 | 50 | ||
51 | void MY_FAST_CALL Sha256_UpdateBlocks(UInt32 state[8], const Byte *data, size_t numBlocks); | 51 | void Z7_FASTCALL Sha256_UpdateBlocks(UInt32 state[8], const Byte *data, size_t numBlocks); |
52 | 52 | ||
53 | #ifdef _SHA_SUPPORTED | 53 | #ifdef Z7_COMPILER_SHA256_SUPPORTED |
54 | void MY_FAST_CALL Sha256_UpdateBlocks_HW(UInt32 state[8], const Byte *data, size_t numBlocks); | 54 | void Z7_FASTCALL Sha256_UpdateBlocks_HW(UInt32 state[8], const Byte *data, size_t numBlocks); |
55 | 55 | ||
56 | static SHA256_FUNC_UPDATE_BLOCKS g_FUNC_UPDATE_BLOCKS = Sha256_UpdateBlocks; | 56 | static SHA256_FUNC_UPDATE_BLOCKS g_SHA256_FUNC_UPDATE_BLOCKS = Sha256_UpdateBlocks; |
57 | static SHA256_FUNC_UPDATE_BLOCKS g_FUNC_UPDATE_BLOCKS_HW; | 57 | static SHA256_FUNC_UPDATE_BLOCKS g_SHA256_FUNC_UPDATE_BLOCKS_HW; |
58 | 58 | ||
59 | #define UPDATE_BLOCKS(p) p->func_UpdateBlocks | 59 | #define SHA256_UPDATE_BLOCKS(p) p->func_UpdateBlocks |
60 | #else | 60 | #else |
61 | #define UPDATE_BLOCKS(p) Sha256_UpdateBlocks | 61 | #define SHA256_UPDATE_BLOCKS(p) Sha256_UpdateBlocks |
62 | #endif | 62 | #endif |
63 | 63 | ||
64 | 64 | ||
@@ -66,16 +66,16 @@ BoolInt Sha256_SetFunction(CSha256 *p, unsigned algo) | |||
66 | { | 66 | { |
67 | SHA256_FUNC_UPDATE_BLOCKS func = Sha256_UpdateBlocks; | 67 | SHA256_FUNC_UPDATE_BLOCKS func = Sha256_UpdateBlocks; |
68 | 68 | ||
69 | #ifdef _SHA_SUPPORTED | 69 | #ifdef Z7_COMPILER_SHA256_SUPPORTED |
70 | if (algo != SHA256_ALGO_SW) | 70 | if (algo != SHA256_ALGO_SW) |
71 | { | 71 | { |
72 | if (algo == SHA256_ALGO_DEFAULT) | 72 | if (algo == SHA256_ALGO_DEFAULT) |
73 | func = g_FUNC_UPDATE_BLOCKS; | 73 | func = g_SHA256_FUNC_UPDATE_BLOCKS; |
74 | else | 74 | else |
75 | { | 75 | { |
76 | if (algo != SHA256_ALGO_HW) | 76 | if (algo != SHA256_ALGO_HW) |
77 | return False; | 77 | return False; |
78 | func = g_FUNC_UPDATE_BLOCKS_HW; | 78 | func = g_SHA256_FUNC_UPDATE_BLOCKS_HW; |
79 | if (!func) | 79 | if (!func) |
80 | return False; | 80 | return False; |
81 | } | 81 | } |
@@ -92,17 +92,18 @@ BoolInt Sha256_SetFunction(CSha256 *p, unsigned algo) | |||
92 | 92 | ||
93 | /* define it for speed optimization */ | 93 | /* define it for speed optimization */ |
94 | 94 | ||
95 | #ifdef _SFX | 95 | #ifdef Z7_SFX |
96 | #define STEP_PRE 1 | 96 | #define STEP_PRE 1 |
97 | #define STEP_MAIN 1 | 97 | #define STEP_MAIN 1 |
98 | #else | 98 | #else |
99 | #define STEP_PRE 2 | 99 | #define STEP_PRE 2 |
100 | #define STEP_MAIN 4 | 100 | #define STEP_MAIN 4 |
101 | // #define _SHA256_UNROLL | 101 | // #define Z7_SHA256_UNROLL |
102 | #endif | 102 | #endif |
103 | 103 | ||
104 | #undef Z7_SHA256_BIG_W | ||
104 | #if STEP_MAIN != 16 | 105 | #if STEP_MAIN != 16 |
105 | #define _SHA256_BIG_W | 106 | #define Z7_SHA256_BIG_W |
106 | #endif | 107 | #endif |
107 | 108 | ||
108 | 109 | ||
@@ -124,8 +125,8 @@ void Sha256_InitState(CSha256 *p) | |||
124 | void Sha256_Init(CSha256 *p) | 125 | void Sha256_Init(CSha256 *p) |
125 | { | 126 | { |
126 | p->func_UpdateBlocks = | 127 | p->func_UpdateBlocks = |
127 | #ifdef _SHA_SUPPORTED | 128 | #ifdef Z7_COMPILER_SHA256_SUPPORTED |
128 | g_FUNC_UPDATE_BLOCKS; | 129 | g_SHA256_FUNC_UPDATE_BLOCKS; |
129 | #else | 130 | #else |
130 | NULL; | 131 | NULL; |
131 | #endif | 132 | #endif |
@@ -145,7 +146,7 @@ void Sha256_Init(CSha256 *p) | |||
145 | 146 | ||
146 | #define blk2_main(j, i) s1(w(j, (i)-2)) + w(j, (i)-7) + s0(w(j, (i)-15)) | 147 | #define blk2_main(j, i) s1(w(j, (i)-2)) + w(j, (i)-7) + s0(w(j, (i)-15)) |
147 | 148 | ||
148 | #ifdef _SHA256_BIG_W | 149 | #ifdef Z7_SHA256_BIG_W |
149 | // we use +i instead of +(i) to change the order to solve CLANG compiler warning for signed/unsigned. | 150 | // we use +i instead of +(i) to change the order to solve CLANG compiler warning for signed/unsigned. |
150 | #define w(j, i) W[(size_t)(j) + i] | 151 | #define w(j, i) W[(size_t)(j) + i] |
151 | #define blk2(j, i) (w(j, i) = w(j, (i)-16) + blk2_main(j, i)) | 152 | #define blk2(j, i) (w(j, i) = w(j, (i)-16) + blk2_main(j, i)) |
@@ -176,7 +177,7 @@ void Sha256_Init(CSha256 *p) | |||
176 | #define R1_PRE(i) T1( W_PRE, i) | 177 | #define R1_PRE(i) T1( W_PRE, i) |
177 | #define R1_MAIN(i) T1( W_MAIN, i) | 178 | #define R1_MAIN(i) T1( W_MAIN, i) |
178 | 179 | ||
179 | #if (!defined(_SHA256_UNROLL) || STEP_MAIN < 8) && (STEP_MAIN >= 4) | 180 | #if (!defined(Z7_SHA256_UNROLL) || STEP_MAIN < 8) && (STEP_MAIN >= 4) |
180 | #define R2_MAIN(i) \ | 181 | #define R2_MAIN(i) \ |
181 | R1_MAIN(i) \ | 182 | R1_MAIN(i) \ |
182 | R1_MAIN(i + 1) \ | 183 | R1_MAIN(i + 1) \ |
@@ -185,7 +186,7 @@ void Sha256_Init(CSha256 *p) | |||
185 | 186 | ||
186 | 187 | ||
187 | 188 | ||
188 | #if defined(_SHA256_UNROLL) && STEP_MAIN >= 8 | 189 | #if defined(Z7_SHA256_UNROLL) && STEP_MAIN >= 8 |
189 | 190 | ||
190 | #define T4( a,b,c,d,e,f,g,h, wx, i) \ | 191 | #define T4( a,b,c,d,e,f,g,h, wx, i) \ |
191 | h += S1(e) + Ch(e,f,g) + K[(i)+(size_t)(j)] + wx(i); \ | 192 | h += S1(e) + Ch(e,f,g) + K[(i)+(size_t)(j)] + wx(i); \ |
@@ -223,7 +224,7 @@ void Sha256_Init(CSha256 *p) | |||
223 | 224 | ||
224 | #endif | 225 | #endif |
225 | 226 | ||
226 | void MY_FAST_CALL Sha256_UpdateBlocks_HW(UInt32 state[8], const Byte *data, size_t numBlocks); | 227 | void Z7_FASTCALL Sha256_UpdateBlocks_HW(UInt32 state[8], const Byte *data, size_t numBlocks); |
227 | 228 | ||
228 | // static | 229 | // static |
229 | extern MY_ALIGN(64) | 230 | extern MY_ALIGN(64) |
@@ -252,11 +253,11 @@ const UInt32 SHA256_K_ARRAY[64] = { | |||
252 | #define K SHA256_K_ARRAY | 253 | #define K SHA256_K_ARRAY |
253 | 254 | ||
254 | 255 | ||
255 | MY_NO_INLINE | 256 | Z7_NO_INLINE |
256 | void MY_FAST_CALL Sha256_UpdateBlocks(UInt32 state[8], const Byte *data, size_t numBlocks) | 257 | void Z7_FASTCALL Sha256_UpdateBlocks(UInt32 state[8], const Byte *data, size_t numBlocks) |
257 | { | 258 | { |
258 | UInt32 W | 259 | UInt32 W |
259 | #ifdef _SHA256_BIG_W | 260 | #ifdef Z7_SHA256_BIG_W |
260 | [64]; | 261 | [64]; |
261 | #else | 262 | #else |
262 | [16]; | 263 | [16]; |
@@ -266,7 +267,7 @@ void MY_FAST_CALL Sha256_UpdateBlocks(UInt32 state[8], const Byte *data, size_t | |||
266 | 267 | ||
267 | UInt32 a,b,c,d,e,f,g,h; | 268 | UInt32 a,b,c,d,e,f,g,h; |
268 | 269 | ||
269 | #if !defined(_SHA256_UNROLL) || (STEP_MAIN <= 4) || (STEP_PRE <= 4) | 270 | #if !defined(Z7_SHA256_UNROLL) || (STEP_MAIN <= 4) || (STEP_PRE <= 4) |
270 | UInt32 tmp; | 271 | UInt32 tmp; |
271 | #endif | 272 | #endif |
272 | 273 | ||
@@ -297,12 +298,12 @@ void MY_FAST_CALL Sha256_UpdateBlocks(UInt32 state[8], const Byte *data, size_t | |||
297 | 298 | ||
298 | #else | 299 | #else |
299 | 300 | ||
300 | R1_PRE(0); | 301 | R1_PRE(0) |
301 | #if STEP_PRE >= 2 | 302 | #if STEP_PRE >= 2 |
302 | R1_PRE(1); | 303 | R1_PRE(1) |
303 | #if STEP_PRE >= 4 | 304 | #if STEP_PRE >= 4 |
304 | R1_PRE(2); | 305 | R1_PRE(2) |
305 | R1_PRE(3); | 306 | R1_PRE(3) |
306 | #endif | 307 | #endif |
307 | #endif | 308 | #endif |
308 | 309 | ||
@@ -311,32 +312,32 @@ void MY_FAST_CALL Sha256_UpdateBlocks(UInt32 state[8], const Byte *data, size_t | |||
311 | 312 | ||
312 | for (j = 16; j < 64; j += STEP_MAIN) | 313 | for (j = 16; j < 64; j += STEP_MAIN) |
313 | { | 314 | { |
314 | #if defined(_SHA256_UNROLL) && STEP_MAIN >= 8 | 315 | #if defined(Z7_SHA256_UNROLL) && STEP_MAIN >= 8 |
315 | 316 | ||
316 | #if STEP_MAIN < 8 | 317 | #if STEP_MAIN < 8 |
317 | R4_MAIN(0); | 318 | R4_MAIN(0) |
318 | #else | 319 | #else |
319 | R8_MAIN(0); | 320 | R8_MAIN(0) |
320 | #if STEP_MAIN == 16 | 321 | #if STEP_MAIN == 16 |
321 | R8_MAIN(8); | 322 | R8_MAIN(8) |
322 | #endif | 323 | #endif |
323 | #endif | 324 | #endif |
324 | 325 | ||
325 | #else | 326 | #else |
326 | 327 | ||
327 | R1_MAIN(0); | 328 | R1_MAIN(0) |
328 | #if STEP_MAIN >= 2 | 329 | #if STEP_MAIN >= 2 |
329 | R1_MAIN(1); | 330 | R1_MAIN(1) |
330 | #if STEP_MAIN >= 4 | 331 | #if STEP_MAIN >= 4 |
331 | R2_MAIN(2); | 332 | R2_MAIN(2) |
332 | #if STEP_MAIN >= 8 | 333 | #if STEP_MAIN >= 8 |
333 | R2_MAIN(4); | 334 | R2_MAIN(4) |
334 | R2_MAIN(6); | 335 | R2_MAIN(6) |
335 | #if STEP_MAIN >= 16 | 336 | #if STEP_MAIN >= 16 |
336 | R2_MAIN(8); | 337 | R2_MAIN(8) |
337 | R2_MAIN(10); | 338 | R2_MAIN(10) |
338 | R2_MAIN(12); | 339 | R2_MAIN(12) |
339 | R2_MAIN(14); | 340 | R2_MAIN(14) |
340 | #endif | 341 | #endif |
341 | #endif | 342 | #endif |
342 | #endif | 343 | #endif |
@@ -367,7 +368,7 @@ void MY_FAST_CALL Sha256_UpdateBlocks(UInt32 state[8], const Byte *data, size_t | |||
367 | #undef s1 | 368 | #undef s1 |
368 | #undef K | 369 | #undef K |
369 | 370 | ||
370 | #define Sha256_UpdateBlock(p) UPDATE_BLOCKS(p)(p->state, p->buffer, 1) | 371 | #define Sha256_UpdateBlock(p) SHA256_UPDATE_BLOCKS(p)(p->state, p->buffer, 1) |
371 | 372 | ||
372 | void Sha256_Update(CSha256 *p, const Byte *data, size_t size) | 373 | void Sha256_Update(CSha256 *p, const Byte *data, size_t size) |
373 | { | 374 | { |
@@ -397,7 +398,7 @@ void Sha256_Update(CSha256 *p, const Byte *data, size_t size) | |||
397 | } | 398 | } |
398 | { | 399 | { |
399 | size_t numBlocks = size >> 6; | 400 | size_t numBlocks = size >> 6; |
400 | UPDATE_BLOCKS(p)(p->state, data, numBlocks); | 401 | SHA256_UPDATE_BLOCKS(p)(p->state, data, numBlocks); |
401 | size &= 0x3F; | 402 | size &= 0x3F; |
402 | if (size == 0) | 403 | if (size == 0) |
403 | return; | 404 | return; |
@@ -441,8 +442,8 @@ void Sha256_Final(CSha256 *p, Byte *digest) | |||
441 | 442 | ||
442 | { | 443 | { |
443 | UInt64 numBits = (p->count << 3); | 444 | UInt64 numBits = (p->count << 3); |
444 | SetBe32(p->buffer + 64 - 8, (UInt32)(numBits >> 32)); | 445 | SetBe32(p->buffer + 64 - 8, (UInt32)(numBits >> 32)) |
445 | SetBe32(p->buffer + 64 - 4, (UInt32)(numBits)); | 446 | SetBe32(p->buffer + 64 - 4, (UInt32)(numBits)) |
446 | } | 447 | } |
447 | 448 | ||
448 | Sha256_UpdateBlock(p); | 449 | Sha256_UpdateBlock(p); |
@@ -451,8 +452,8 @@ void Sha256_Final(CSha256 *p, Byte *digest) | |||
451 | { | 452 | { |
452 | UInt32 v0 = p->state[i]; | 453 | UInt32 v0 = p->state[i]; |
453 | UInt32 v1 = p->state[(size_t)i + 1]; | 454 | UInt32 v1 = p->state[(size_t)i + 1]; |
454 | SetBe32(digest , v0); | 455 | SetBe32(digest , v0) |
455 | SetBe32(digest + 4, v1); | 456 | SetBe32(digest + 4, v1) |
456 | digest += 8; | 457 | digest += 8; |
457 | } | 458 | } |
458 | 459 | ||
@@ -460,9 +461,9 @@ void Sha256_Final(CSha256 *p, Byte *digest) | |||
460 | } | 461 | } |
461 | 462 | ||
462 | 463 | ||
463 | void Sha256Prepare() | 464 | void Sha256Prepare(void) |
464 | { | 465 | { |
465 | #ifdef _SHA_SUPPORTED | 466 | #ifdef Z7_COMPILER_SHA256_SUPPORTED |
466 | SHA256_FUNC_UPDATE_BLOCKS f, f_hw; | 467 | SHA256_FUNC_UPDATE_BLOCKS f, f_hw; |
467 | f = Sha256_UpdateBlocks; | 468 | f = Sha256_UpdateBlocks; |
468 | f_hw = NULL; | 469 | f_hw = NULL; |
@@ -480,7 +481,36 @@ void Sha256Prepare() | |||
480 | // printf("\n========== HW SHA256 ======== \n"); | 481 | // printf("\n========== HW SHA256 ======== \n"); |
481 | f = f_hw = Sha256_UpdateBlocks_HW; | 482 | f = f_hw = Sha256_UpdateBlocks_HW; |
482 | } | 483 | } |
483 | g_FUNC_UPDATE_BLOCKS = f; | 484 | g_SHA256_FUNC_UPDATE_BLOCKS = f; |
484 | g_FUNC_UPDATE_BLOCKS_HW = f_hw; | 485 | g_SHA256_FUNC_UPDATE_BLOCKS_HW = f_hw; |
485 | #endif | 486 | #endif |
486 | } | 487 | } |
488 | |||
489 | #undef S0 | ||
490 | #undef S1 | ||
491 | #undef s0 | ||
492 | #undef s1 | ||
493 | #undef Ch | ||
494 | #undef Maj | ||
495 | #undef W_MAIN | ||
496 | #undef W_PRE | ||
497 | #undef w | ||
498 | #undef blk2_main | ||
499 | #undef blk2 | ||
500 | #undef T1 | ||
501 | #undef T4 | ||
502 | #undef T8 | ||
503 | #undef R1_PRE | ||
504 | #undef R1_MAIN | ||
505 | #undef R2_MAIN | ||
506 | #undef R4 | ||
507 | #undef R4_PRE | ||
508 | #undef R4_MAIN | ||
509 | #undef R8 | ||
510 | #undef R8_PRE | ||
511 | #undef R8_MAIN | ||
512 | #undef STEP_PRE | ||
513 | #undef STEP_MAIN | ||
514 | #undef Z7_SHA256_BIG_W | ||
515 | #undef Z7_SHA256_UNROLL | ||
516 | #undef Z7_COMPILER_SHA256_SUPPORTED | ||