aboutsummaryrefslogtreecommitdiff
path: root/C/Sha1.c
diff options
context:
space:
mode:
authorIgor Pavlov <87184205+ip7z@users.noreply.github.com>2023-06-21 00:00:00 +0000
committerIgor Pavlov <87184205+ip7z@users.noreply.github.com>2023-12-17 14:59:19 +0500
commit5b39dc76f1bc82f941d5c800ab9f34407a06b53a (patch)
treefe5e17420300b715021a76328444088d32047963 /C/Sha1.c
parent93be7d4abfd4233228f58ee1fbbcd76d91be66a4 (diff)
download7zip-5b39dc76f1bc82f941d5c800ab9f34407a06b53a.tar.gz
7zip-5b39dc76f1bc82f941d5c800ab9f34407a06b53a.tar.bz2
7zip-5b39dc76f1bc82f941d5c800ab9f34407a06b53a.zip
23.0123.01
Diffstat (limited to 'C/Sha1.c')
-rw-r--r--C/Sha1.c165
1 files changed, 95 insertions, 70 deletions
diff --git a/C/Sha1.c b/C/Sha1.c
index 9665b5b..fd6c018 100644
--- a/C/Sha1.c
+++ b/C/Sha1.c
@@ -1,5 +1,5 @@
1/* Sha1.c -- SHA-1 Hash 1/* Sha1.c -- SHA-1 Hash
22021-07-13 : Igor Pavlov : Public domain 22023-04-02 : Igor Pavlov : Public domain
3This code is based on public domain code of Steve Reid from Wei Dai's Crypto++ library. */ 3This code is based on public domain code of Steve Reid 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 of Steve Reid from Wei Dai's Crypto++ l
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_SHA1_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_SHA1_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_SHA1_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_SHA1_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 && _MSC_VER >= 1929 && _MSC_FULL_VER >= 192930037 37 #if _MSC_VER >= 1910 && _MSC_VER >= 1929 && _MSC_FULL_VER >= 192930037
38 #define _SHA_SUPPORTED 38 #define Z7_COMPILER_SHA1_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_SHA1_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_SHA1_SUPPORTED
47 #endif 47 #endif
48 #endif 48 #endif
49#endif 49#endif
50 50
51void MY_FAST_CALL Sha1_UpdateBlocks(UInt32 state[5], const Byte *data, size_t numBlocks); 51void Z7_FASTCALL Sha1_UpdateBlocks(UInt32 state[5], const Byte *data, size_t numBlocks);
52 52
53#ifdef _SHA_SUPPORTED 53#ifdef Z7_COMPILER_SHA1_SUPPORTED
54 void MY_FAST_CALL Sha1_UpdateBlocks_HW(UInt32 state[5], const Byte *data, size_t numBlocks); 54 void Z7_FASTCALL Sha1_UpdateBlocks_HW(UInt32 state[5], const Byte *data, size_t numBlocks);
55 55
56 static SHA1_FUNC_UPDATE_BLOCKS g_FUNC_UPDATE_BLOCKS = Sha1_UpdateBlocks; 56 static SHA1_FUNC_UPDATE_BLOCKS g_SHA1_FUNC_UPDATE_BLOCKS = Sha1_UpdateBlocks;
57 static SHA1_FUNC_UPDATE_BLOCKS g_FUNC_UPDATE_BLOCKS_HW; 57 static SHA1_FUNC_UPDATE_BLOCKS g_SHA1_FUNC_UPDATE_BLOCKS_HW;
58 58
59 #define UPDATE_BLOCKS(p) p->func_UpdateBlocks 59 #define SHA1_UPDATE_BLOCKS(p) p->func_UpdateBlocks
60#else 60#else
61 #define UPDATE_BLOCKS(p) Sha1_UpdateBlocks 61 #define SHA1_UPDATE_BLOCKS(p) Sha1_UpdateBlocks
62#endif 62#endif
63 63
64 64
@@ -66,16 +66,16 @@ BoolInt Sha1_SetFunction(CSha1 *p, unsigned algo)
66{ 66{
67 SHA1_FUNC_UPDATE_BLOCKS func = Sha1_UpdateBlocks; 67 SHA1_FUNC_UPDATE_BLOCKS func = Sha1_UpdateBlocks;
68 68
69 #ifdef _SHA_SUPPORTED 69 #ifdef Z7_COMPILER_SHA1_SUPPORTED
70 if (algo != SHA1_ALGO_SW) 70 if (algo != SHA1_ALGO_SW)
71 { 71 {
72 if (algo == SHA1_ALGO_DEFAULT) 72 if (algo == SHA1_ALGO_DEFAULT)
73 func = g_FUNC_UPDATE_BLOCKS; 73 func = g_SHA1_FUNC_UPDATE_BLOCKS;
74 else 74 else
75 { 75 {
76 if (algo != SHA1_ALGO_HW) 76 if (algo != SHA1_ALGO_HW)
77 return False; 77 return False;
78 func = g_FUNC_UPDATE_BLOCKS_HW; 78 func = g_SHA1_FUNC_UPDATE_BLOCKS_HW;
79 if (!func) 79 if (!func)
80 return False; 80 return False;
81 } 81 }
@@ -91,21 +91,22 @@ BoolInt Sha1_SetFunction(CSha1 *p, unsigned algo)
91 91
92 92
93/* define it for speed optimization */ 93/* define it for speed optimization */
94// #define _SHA1_UNROLL 94// #define Z7_SHA1_UNROLL
95 95
96// allowed unroll steps: (1, 2, 4, 5, 20) 96// allowed unroll steps: (1, 2, 4, 5, 20)
97 97
98#ifdef _SHA1_UNROLL 98#undef Z7_SHA1_BIG_W
99#ifdef Z7_SHA1_UNROLL
99 #define STEP_PRE 20 100 #define STEP_PRE 20
100 #define STEP_MAIN 20 101 #define STEP_MAIN 20
101#else 102#else
102 #define _SHA1_BIG_W 103 #define Z7_SHA1_BIG_W
103 #define STEP_PRE 5 104 #define STEP_PRE 5
104 #define STEP_MAIN 5 105 #define STEP_MAIN 5
105#endif 106#endif
106 107
107 108
108#ifdef _SHA1_BIG_W 109#ifdef Z7_SHA1_BIG_W
109 #define kNumW 80 110 #define kNumW 80
110 #define w(i) W[i] 111 #define w(i) W[i]
111#else 112#else
@@ -150,11 +151,11 @@ BoolInt Sha1_SetFunction(CSha1 *p, unsigned algo)
150*/ 151*/
151 152
152#define M5(i, fx, wx0, wx1) \ 153#define M5(i, fx, wx0, wx1) \
153 T5 ( a,b,c,d,e, fx, wx0((i) ) ); \ 154 T5 ( a,b,c,d,e, fx, wx0((i) ) ) \
154 T5 ( e,a,b,c,d, fx, wx1((i)+1) ); \ 155 T5 ( e,a,b,c,d, fx, wx1((i)+1) ) \
155 T5 ( d,e,a,b,c, fx, wx1((i)+2) ); \ 156 T5 ( d,e,a,b,c, fx, wx1((i)+2) ) \
156 T5 ( c,d,e,a,b, fx, wx1((i)+3) ); \ 157 T5 ( c,d,e,a,b, fx, wx1((i)+3) ) \
157 T5 ( b,c,d,e,a, fx, wx1((i)+4) ); \ 158 T5 ( b,c,d,e,a, fx, wx1((i)+4) ) \
158 159
159#define R5(i, fx, wx) \ 160#define R5(i, fx, wx) \
160 M5 ( i, fx, wx, wx) \ 161 M5 ( i, fx, wx, wx) \
@@ -163,17 +164,17 @@ BoolInt Sha1_SetFunction(CSha1 *p, unsigned algo)
163#if STEP_PRE > 5 164#if STEP_PRE > 5
164 165
165 #define R20_START \ 166 #define R20_START \
166 R5 ( 0, f0, w0); \ 167 R5 ( 0, f0, w0) \
167 R5 ( 5, f0, w0); \ 168 R5 ( 5, f0, w0) \
168 R5 ( 10, f0, w0); \ 169 R5 ( 10, f0, w0) \
169 M5 ( 15, f0, w0, w1); \ 170 M5 ( 15, f0, w0, w1) \
170 171
171 #elif STEP_PRE == 5 172 #elif STEP_PRE == 5
172 173
173 #define R20_START \ 174 #define R20_START \
174 { size_t i; for (i = 0; i < 15; i += STEP_PRE) \ 175 { size_t i; for (i = 0; i < 15; i += STEP_PRE) \
175 { R5(i, f0, w0); } } \ 176 { R5(i, f0, w0) } } \
176 M5 ( 15, f0, w0, w1); \ 177 M5 ( 15, f0, w0, w1) \
177 178
178#else 179#else
179 180
@@ -187,8 +188,8 @@ BoolInt Sha1_SetFunction(CSha1 *p, unsigned algo)
187 188
188 #define R20_START \ 189 #define R20_START \
189 { size_t i; for (i = 0; i < 16; i += STEP_PRE) \ 190 { size_t i; for (i = 0; i < 16; i += STEP_PRE) \
190 { R_PRE(i, f0, w0); } } \ 191 { R_PRE(i, f0, w0) } } \
191 R4 ( 16, f0, w1); \ 192 R4 ( 16, f0, w1) \
192 193
193#endif 194#endif
194 195
@@ -197,10 +198,10 @@ BoolInt Sha1_SetFunction(CSha1 *p, unsigned algo)
197#if STEP_MAIN > 5 198#if STEP_MAIN > 5
198 199
199 #define R20(ii, fx) \ 200 #define R20(ii, fx) \
200 R5 ( (ii) , fx, w1); \ 201 R5 ( (ii) , fx, w1) \
201 R5 ( (ii) + 5 , fx, w1); \ 202 R5 ( (ii) + 5 , fx, w1) \
202 R5 ( (ii) + 10, fx, w1); \ 203 R5 ( (ii) + 10, fx, w1) \
203 R5 ( (ii) + 15, fx, w1); \ 204 R5 ( (ii) + 15, fx, w1) \
204 205
205#else 206#else
206 207
@@ -216,7 +217,7 @@ BoolInt Sha1_SetFunction(CSha1 *p, unsigned algo)
216 217
217 #define R20(ii, fx) \ 218 #define R20(ii, fx) \
218 { size_t i; for (i = (ii); i < (ii) + 20; i += STEP_MAIN) \ 219 { size_t i; for (i = (ii); i < (ii) + 20; i += STEP_MAIN) \
219 { R_MAIN(i, fx, w1); } } \ 220 { R_MAIN(i, fx, w1) } } \
220 221
221#endif 222#endif
222 223
@@ -235,8 +236,8 @@ void Sha1_InitState(CSha1 *p)
235void Sha1_Init(CSha1 *p) 236void Sha1_Init(CSha1 *p)
236{ 237{
237 p->func_UpdateBlocks = 238 p->func_UpdateBlocks =
238 #ifdef _SHA_SUPPORTED 239 #ifdef Z7_COMPILER_SHA1_SUPPORTED
239 g_FUNC_UPDATE_BLOCKS; 240 g_SHA1_FUNC_UPDATE_BLOCKS;
240 #else 241 #else
241 NULL; 242 NULL;
242 #endif 243 #endif
@@ -244,8 +245,8 @@ void Sha1_Init(CSha1 *p)
244} 245}
245 246
246 247
247MY_NO_INLINE 248Z7_NO_INLINE
248void MY_FAST_CALL Sha1_UpdateBlocks(UInt32 state[5], const Byte *data, size_t numBlocks) 249void Z7_FASTCALL Sha1_UpdateBlocks(UInt32 state[5], const Byte *data, size_t numBlocks)
249{ 250{
250 UInt32 a, b, c, d, e; 251 UInt32 a, b, c, d, e;
251 UInt32 W[kNumW]; 252 UInt32 W[kNumW];
@@ -266,9 +267,9 @@ void MY_FAST_CALL Sha1_UpdateBlocks(UInt32 state[5], const Byte *data, size_t nu
266 #endif 267 #endif
267 268
268 R20_START 269 R20_START
269 R20(20, f1); 270 R20(20, f1)
270 R20(40, f2); 271 R20(40, f2)
271 R20(60, f3); 272 R20(60, f3)
272 273
273 a += state[0]; 274 a += state[0];
274 b += state[1]; 275 b += state[1];
@@ -288,7 +289,7 @@ void MY_FAST_CALL Sha1_UpdateBlocks(UInt32 state[5], const Byte *data, size_t nu
288} 289}
289 290
290 291
291#define Sha1_UpdateBlock(p) UPDATE_BLOCKS(p)(p->state, p->buffer, 1) 292#define Sha1_UpdateBlock(p) SHA1_UPDATE_BLOCKS(p)(p->state, p->buffer, 1)
292 293
293void Sha1_Update(CSha1 *p, const Byte *data, size_t size) 294void Sha1_Update(CSha1 *p, const Byte *data, size_t size)
294{ 295{
@@ -318,7 +319,7 @@ void Sha1_Update(CSha1 *p, const Byte *data, size_t size)
318 } 319 }
319 { 320 {
320 size_t numBlocks = size >> 6; 321 size_t numBlocks = size >> 6;
321 UPDATE_BLOCKS(p)(p->state, data, numBlocks); 322 SHA1_UPDATE_BLOCKS(p)(p->state, data, numBlocks);
322 size &= 0x3F; 323 size &= 0x3F;
323 if (size == 0) 324 if (size == 0)
324 return; 325 return;
@@ -361,18 +362,18 @@ void Sha1_Final(CSha1 *p, Byte *digest)
361 memset(&p->buffer[pos], 0, (64 - 8) - pos); 362 memset(&p->buffer[pos], 0, (64 - 8) - pos);
362 363
363 { 364 {
364 UInt64 numBits = (p->count << 3); 365 const UInt64 numBits = (p->count << 3);
365 SetBe32(p->buffer + 64 - 8, (UInt32)(numBits >> 32)); 366 SetBe32(p->buffer + 64 - 8, (UInt32)(numBits >> 32))
366 SetBe32(p->buffer + 64 - 4, (UInt32)(numBits)); 367 SetBe32(p->buffer + 64 - 4, (UInt32)(numBits))
367 } 368 }
368 369
369 Sha1_UpdateBlock(p); 370 Sha1_UpdateBlock(p);
370 371
371 SetBe32(digest, p->state[0]); 372 SetBe32(digest, p->state[0])
372 SetBe32(digest + 4, p->state[1]); 373 SetBe32(digest + 4, p->state[1])
373 SetBe32(digest + 8, p->state[2]); 374 SetBe32(digest + 8, p->state[2])
374 SetBe32(digest + 12, p->state[3]); 375 SetBe32(digest + 12, p->state[3])
375 SetBe32(digest + 16, p->state[4]); 376 SetBe32(digest + 16, p->state[4])
376 377
377 378
378 379
@@ -384,10 +385,10 @@ void Sha1_Final(CSha1 *p, Byte *digest)
384void Sha1_PrepareBlock(const CSha1 *p, Byte *block, unsigned size) 385void Sha1_PrepareBlock(const CSha1 *p, Byte *block, unsigned size)
385{ 386{
386 const UInt64 numBits = (p->count + size) << 3; 387 const UInt64 numBits = (p->count + size) << 3;
387 SetBe32(&((UInt32 *)(void *)block)[SHA1_NUM_BLOCK_WORDS - 2], (UInt32)(numBits >> 32)); 388 SetBe32(&((UInt32 *)(void *)block)[SHA1_NUM_BLOCK_WORDS - 2], (UInt32)(numBits >> 32))
388 SetBe32(&((UInt32 *)(void *)block)[SHA1_NUM_BLOCK_WORDS - 1], (UInt32)(numBits)); 389 SetBe32(&((UInt32 *)(void *)block)[SHA1_NUM_BLOCK_WORDS - 1], (UInt32)(numBits))
389 // SetBe32((UInt32 *)(block + size), 0x80000000); 390 // SetBe32((UInt32 *)(block + size), 0x80000000);
390 SetUi32((UInt32 *)(void *)(block + size), 0x80); 391 SetUi32((UInt32 *)(void *)(block + size), 0x80)
391 size += 4; 392 size += 4;
392 while (size != (SHA1_NUM_BLOCK_WORDS - 2) * 4) 393 while (size != (SHA1_NUM_BLOCK_WORDS - 2) * 4)
393 { 394 {
@@ -407,19 +408,19 @@ void Sha1_GetBlockDigest(const CSha1 *p, const Byte *data, Byte *destDigest)
407 st[3] = p->state[3]; 408 st[3] = p->state[3];
408 st[4] = p->state[4]; 409 st[4] = p->state[4];
409 410
410 UPDATE_BLOCKS(p)(st, data, 1); 411 SHA1_UPDATE_BLOCKS(p)(st, data, 1);
411 412
412 SetBe32(destDigest + 0 , st[0]); 413 SetBe32(destDigest + 0 , st[0])
413 SetBe32(destDigest + 1 * 4, st[1]); 414 SetBe32(destDigest + 1 * 4, st[1])
414 SetBe32(destDigest + 2 * 4, st[2]); 415 SetBe32(destDigest + 2 * 4, st[2])
415 SetBe32(destDigest + 3 * 4, st[3]); 416 SetBe32(destDigest + 3 * 4, st[3])
416 SetBe32(destDigest + 4 * 4, st[4]); 417 SetBe32(destDigest + 4 * 4, st[4])
417} 418}
418 419
419 420
420void Sha1Prepare() 421void Sha1Prepare(void)
421{ 422{
422 #ifdef _SHA_SUPPORTED 423 #ifdef Z7_COMPILER_SHA1_SUPPORTED
423 SHA1_FUNC_UPDATE_BLOCKS f, f_hw; 424 SHA1_FUNC_UPDATE_BLOCKS f, f_hw;
424 f = Sha1_UpdateBlocks; 425 f = Sha1_UpdateBlocks;
425 f_hw = NULL; 426 f_hw = NULL;
@@ -467,7 +468,31 @@ void Sha1Prepare()
467 f = f_hw = Sha1_UpdateBlocks_HW; 468 f = f_hw = Sha1_UpdateBlocks_HW;
468 } 469 }
469 } 470 }
470 g_FUNC_UPDATE_BLOCKS = f; 471 g_SHA1_FUNC_UPDATE_BLOCKS = f;
471 g_FUNC_UPDATE_BLOCKS_HW = f_hw; 472 g_SHA1_FUNC_UPDATE_BLOCKS_HW = f_hw;
472 #endif 473 #endif
473} 474}
475
476#undef kNumW
477#undef w
478#undef w0
479#undef w1
480#undef f0
481#undef f1
482#undef f2
483#undef f3
484#undef T1
485#undef T5
486#undef M5
487#undef R1
488#undef R2
489#undef R4
490#undef R5
491#undef R20_START
492#undef R_PRE
493#undef R_MAIN
494#undef STEP_PRE
495#undef STEP_MAIN
496#undef Z7_SHA1_BIG_W
497#undef Z7_SHA1_UNROLL
498#undef Z7_COMPILER_SHA1_SUPPORTED