From e5431fa6f5505e385c6f9367260717e9c47dc2ee Mon Sep 17 00:00:00 2001 From: Igor Pavlov <87184205+ip7z@users.noreply.github.com> Date: Fri, 29 Nov 2024 00:00:00 +0000 Subject: 24.09 --- C/Sha256.c | 162 ++++++++++++++++++++++++++----------------------------------- 1 file changed, 70 insertions(+), 92 deletions(-) (limited to 'C/Sha256.c') diff --git a/C/Sha256.c b/C/Sha256.c index 14d3be9..ea7ed8e 100644 --- a/C/Sha256.c +++ b/C/Sha256.c @@ -1,18 +1,14 @@ /* Sha256.c -- SHA-256 Hash -2024-03-01 : Igor Pavlov : Public domain +: Igor Pavlov : Public domain This code is based on public domain code from Wei Dai's Crypto++ library. */ #include "Precomp.h" #include -#include "CpuArch.h" -#include "RotateDefs.h" #include "Sha256.h" - -#if defined(_MSC_VER) && (_MSC_VER < 1900) -// #define USE_MY_MM -#endif +#include "RotateDefs.h" +#include "CpuArch.h" #ifdef MY_CPU_X86_OR_AMD64 #if defined(Z7_LLVM_CLANG_VERSION) && (Z7_LLVM_CLANG_VERSION >= 30800) \ @@ -56,7 +52,7 @@ void Z7_FASTCALL Sha256_UpdateBlocks(UInt32 state[8], const Byte *data, size_t n static SHA256_FUNC_UPDATE_BLOCKS g_SHA256_FUNC_UPDATE_BLOCKS = Sha256_UpdateBlocks; static SHA256_FUNC_UPDATE_BLOCKS g_SHA256_FUNC_UPDATE_BLOCKS_HW; - #define SHA256_UPDATE_BLOCKS(p) p->func_UpdateBlocks + #define SHA256_UPDATE_BLOCKS(p) p->v.vars.func_UpdateBlocks #else #define SHA256_UPDATE_BLOCKS(p) Sha256_UpdateBlocks #endif @@ -85,7 +81,7 @@ BoolInt Sha256_SetFunction(CSha256 *p, unsigned algo) return False; #endif - p->func_UpdateBlocks = func; + p->v.vars.func_UpdateBlocks = func; return True; } @@ -111,7 +107,7 @@ BoolInt Sha256_SetFunction(CSha256 *p, unsigned algo) void Sha256_InitState(CSha256 *p) { - p->count = 0; + p->v.vars.count = 0; p->state[0] = 0x6a09e667; p->state[1] = 0xbb67ae85; p->state[2] = 0x3c6ef372; @@ -122,9 +118,16 @@ void Sha256_InitState(CSha256 *p) p->state[7] = 0x5be0cd19; } + + + + + + + void Sha256_Init(CSha256 *p) { - p->func_UpdateBlocks = + p->v.vars.func_UpdateBlocks = #ifdef Z7_COMPILER_SHA256_SUPPORTED g_SHA256_FUNC_UPDATE_BLOCKS; #else @@ -133,10 +136,10 @@ void Sha256_Init(CSha256 *p) Sha256_InitState(p); } -#define S0(x) (rotrFixed(x, 2) ^ rotrFixed(x,13) ^ rotrFixed(x, 22)) -#define S1(x) (rotrFixed(x, 6) ^ rotrFixed(x,11) ^ rotrFixed(x, 25)) +#define S0(x) (rotrFixed(x, 2) ^ rotrFixed(x,13) ^ rotrFixed(x,22)) +#define S1(x) (rotrFixed(x, 6) ^ rotrFixed(x,11) ^ rotrFixed(x,25)) #define s0(x) (rotrFixed(x, 7) ^ rotrFixed(x,18) ^ (x >> 3)) -#define s1(x) (rotrFixed(x,17) ^ rotrFixed(x,19) ^ (x >> 10)) +#define s1(x) (rotrFixed(x,17) ^ rotrFixed(x,19) ^ (x >>10)) #define Ch(x,y,z) (z^(x&(y^z))) #define Maj(x,y,z) ((x&y)|(z&(x|y))) @@ -224,12 +227,10 @@ void Sha256_Init(CSha256 *p) #endif -// static -extern MY_ALIGN(64) -const UInt32 SHA256_K_ARRAY[64]; -MY_ALIGN(64) -const UInt32 SHA256_K_ARRAY[64] = { +extern +MY_ALIGN(64) const UInt32 SHA256_K_ARRAY[64]; +MY_ALIGN(64) const UInt32 SHA256_K_ARRAY[64] = { 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, @@ -248,27 +249,29 @@ const UInt32 SHA256_K_ARRAY[64] = { 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 }; -#define K SHA256_K_ARRAY + + +#define K SHA256_K_ARRAY + Z7_NO_INLINE void Z7_FASTCALL Sha256_UpdateBlocks(UInt32 state[8], const Byte *data, size_t numBlocks) { UInt32 W - #ifdef Z7_SHA256_BIG_W +#ifdef Z7_SHA256_BIG_W [64]; - #else +#else [16]; - #endif - +#endif unsigned j; - UInt32 a,b,c,d,e,f,g,h; - - #if !defined(Z7_SHA256_UNROLL) || (STEP_MAIN <= 4) || (STEP_PRE <= 4) +#if !defined(Z7_SHA256_UNROLL) || (STEP_MAIN <= 4) || (STEP_PRE <= 4) UInt32 tmp; - #endif +#endif + if (numBlocks == 0) return; + a = state[0]; b = state[1]; c = state[2]; @@ -278,7 +281,7 @@ void Z7_FASTCALL Sha256_UpdateBlocks(UInt32 state[8], const Byte *data, size_t n g = state[6]; h = state[7]; - while (numBlocks) + do { for (j = 0; j < 16; j += STEP_PRE) @@ -352,19 +355,11 @@ void Z7_FASTCALL Sha256_UpdateBlocks(UInt32 state[8], const Byte *data, size_t n g += state[6]; state[6] = g; h += state[7]; state[7] = h; - data += 64; - numBlocks--; + data += SHA256_BLOCK_SIZE; } - - /* Wipe variables */ - /* memset(W, 0, sizeof(W)); */ + while (--numBlocks); } -#undef S0 -#undef S1 -#undef s0 -#undef s1 -#undef K #define Sha256_UpdateBlock(p) SHA256_UPDATE_BLOCKS(p)(p->state, p->buffer, 1) @@ -372,20 +367,15 @@ void Sha256_Update(CSha256 *p, const Byte *data, size_t size) { if (size == 0) return; - { - unsigned pos = (unsigned)p->count & 0x3F; - unsigned num; - - p->count += size; - - num = 64 - pos; + const unsigned pos = (unsigned)p->v.vars.count & (SHA256_BLOCK_SIZE - 1); + const unsigned num = SHA256_BLOCK_SIZE - pos; + p->v.vars.count += size; if (num > size) { memcpy(p->buffer + pos, data, size); return; } - if (pos != 0) { size -= num; @@ -395,9 +385,10 @@ void Sha256_Update(CSha256 *p, const Byte *data, size_t size) } } { - size_t numBlocks = size >> 6; + const size_t numBlocks = size >> 6; + // if (numBlocks) SHA256_UPDATE_BLOCKS(p)(p->state, data, numBlocks); - size &= 0x3F; + size &= SHA256_BLOCK_SIZE - 1; if (size == 0) return; data += (numBlocks << 6); @@ -408,82 +399,69 @@ void Sha256_Update(CSha256 *p, const Byte *data, size_t size) void Sha256_Final(CSha256 *p, Byte *digest) { - unsigned pos = (unsigned)p->count & 0x3F; - unsigned i; - + unsigned pos = (unsigned)p->v.vars.count & (SHA256_BLOCK_SIZE - 1); p->buffer[pos++] = 0x80; - - if (pos > (64 - 8)) + if (pos > (SHA256_BLOCK_SIZE - 4 * 2)) { - while (pos != 64) { p->buffer[pos++] = 0; } - // memset(&p->buf.buffer[pos], 0, 64 - pos); + while (pos != SHA256_BLOCK_SIZE) { p->buffer[pos++] = 0; } + // memset(&p->buf.buffer[pos], 0, SHA256_BLOCK_SIZE - pos); Sha256_UpdateBlock(p); pos = 0; } - - /* - if (pos & 3) + memset(&p->buffer[pos], 0, (SHA256_BLOCK_SIZE - 4 * 2) - pos); { - p->buffer[pos] = 0; - p->buffer[pos + 1] = 0; - p->buffer[pos + 2] = 0; - pos += 3; - pos &= ~3; + const UInt64 numBits = p->v.vars.count << 3; + SetBe32(p->buffer + SHA256_BLOCK_SIZE - 4 * 2, (UInt32)(numBits >> 32)) + SetBe32(p->buffer + SHA256_BLOCK_SIZE - 4 * 1, (UInt32)(numBits)) } + Sha256_UpdateBlock(p); +#if 1 && defined(MY_CPU_BE) + memcpy(digest, p->state, SHA256_DIGEST_SIZE); +#else { - for (; pos < 64 - 8; pos += 4) - *(UInt32 *)(&p->buffer[pos]) = 0; + unsigned i; + for (i = 0; i < 8; i += 2) + { + const UInt32 v0 = p->state[i]; + const UInt32 v1 = p->state[(size_t)i + 1]; + SetBe32(digest , v0) + SetBe32(digest + 4, v1) + digest += 4 * 2; + } } - */ - memset(&p->buffer[pos], 0, (64 - 8) - pos); - { - UInt64 numBits = (p->count << 3); - SetBe32(p->buffer + 64 - 8, (UInt32)(numBits >> 32)) - SetBe32(p->buffer + 64 - 4, (UInt32)(numBits)) - } - - Sha256_UpdateBlock(p); - for (i = 0; i < 8; i += 2) - { - UInt32 v0 = p->state[i]; - UInt32 v1 = p->state[(size_t)i + 1]; - SetBe32(digest , v0) - SetBe32(digest + 4, v1) - digest += 8; - } - + +#endif Sha256_InitState(p); } void Sha256Prepare(void) { - #ifdef Z7_COMPILER_SHA256_SUPPORTED +#ifdef Z7_COMPILER_SHA256_SUPPORTED SHA256_FUNC_UPDATE_BLOCKS f, f_hw; f = Sha256_UpdateBlocks; f_hw = NULL; - #ifdef MY_CPU_X86_OR_AMD64 - #ifndef USE_MY_MM +#ifdef MY_CPU_X86_OR_AMD64 if (CPU_IsSupported_SHA() && CPU_IsSupported_SSSE3() - // && CPU_IsSupported_SSE41() ) - #endif - #else +#else if (CPU_IsSupported_SHA2()) - #endif +#endif { // printf("\n========== HW SHA256 ======== \n"); f = f_hw = Sha256_UpdateBlocks_HW; } g_SHA256_FUNC_UPDATE_BLOCKS = f; g_SHA256_FUNC_UPDATE_BLOCKS_HW = f_hw; - #endif +#endif } +#undef U64C +#undef K #undef S0 #undef S1 #undef s0 -- cgit v1.2.3-55-g6feb