diff options
Diffstat (limited to '3rdparty/lzma/C/Sha256.c')
-rw-r--r-- | 3rdparty/lzma/C/Sha256.c | 170 |
1 files changed, 107 insertions, 63 deletions
diff --git a/3rdparty/lzma/C/Sha256.c b/3rdparty/lzma/C/Sha256.c index eb4fc61fc9d..b5bc19b9602 100644 --- a/3rdparty/lzma/C/Sha256.c +++ b/3rdparty/lzma/C/Sha256.c @@ -1,12 +1,21 @@ /* Crypto/Sha256.c -- SHA-256 Hash -2010-06-11 : Igor Pavlov : Public domain +2015-11-14 : Igor Pavlov : Public domain This code is based on public domain code from Wei Dai's Crypto++ library. */ +#include "Precomp.h" + +#include <string.h> + +#include "CpuArch.h" #include "RotateDefs.h" #include "Sha256.h" /* define it for speed optimization */ -/* #define _SHA256_UNROLL */ +#ifndef _SFX +#define _SHA256_UNROLL +#define _SHA256_UNROLL2 +#endif + /* #define _SHA256_UNROLL2 */ void Sha256_Init(CSha256 *p) @@ -27,26 +36,18 @@ void Sha256_Init(CSha256 *p) #define s0(x) (rotrFixed(x, 7) ^ rotrFixed(x,18) ^ (x >> 3)) #define s1(x) (rotrFixed(x,17) ^ rotrFixed(x,19) ^ (x >> 10)) -#define blk0(i) (W[i] = data[i]) -#define blk2(i) (W[i&15] += s1(W[(i-2)&15]) + W[(i-7)&15] + s0(W[(i-15)&15])) +#define blk0(i) (W[i]) +#define blk2(i) (W[i] += s1(W[((i)-2)&15]) + W[((i)-7)&15] + s0(W[((i)-15)&15])) #define Ch(x,y,z) (z^(x&(y^z))) #define Maj(x,y,z) ((x&y)|(z&(x|y))) -#define a(i) T[(0-(i))&7] -#define b(i) T[(1-(i))&7] -#define c(i) T[(2-(i))&7] -#define d(i) T[(3-(i))&7] -#define e(i) T[(4-(i))&7] -#define f(i) T[(5-(i))&7] -#define g(i) T[(6-(i))&7] -#define h(i) T[(7-(i))&7] - - #ifdef _SHA256_UNROLL2 -#define R(a,b,c,d,e,f,g,h, i) h += S1(e) + Ch(e,f,g) + K[i+j] + (j?blk2(i):blk0(i));\ - d += h; h += S0(a) + Maj(a, b, c) +#define R(a,b,c,d,e,f,g,h, i) \ + h += S1(e) + Ch(e,f,g) + K[(i)+(j)] + (j ? blk2(i) : blk0(i)); \ + d += h; \ + h += S0(a) + Maj(a, b, c) #define RX_8(i) \ R(a,b,c,d,e,f,g,h, i); \ @@ -58,14 +59,32 @@ void Sha256_Init(CSha256 *p) R(c,d,e,f,g,h,a,b, i+6); \ R(b,c,d,e,f,g,h,a, i+7) +#define RX_16 RX_8(0); RX_8(8); + #else -#define R(i) h(i) += S1(e(i)) + Ch(e(i),f(i),g(i)) + K[i+j] + (j?blk2(i):blk0(i));\ - d(i) += h(i); h(i) += S0(a(i)) + Maj(a(i), b(i), c(i)) +#define a(i) T[(0-(i))&7] +#define b(i) T[(1-(i))&7] +#define c(i) T[(2-(i))&7] +#define d(i) T[(3-(i))&7] +#define e(i) T[(4-(i))&7] +#define f(i) T[(5-(i))&7] +#define g(i) T[(6-(i))&7] +#define h(i) T[(7-(i))&7] + +#define R(i) \ + h(i) += S1(e(i)) + Ch(e(i),f(i),g(i)) + K[(i)+(j)] + (j ? blk2(i) : blk0(i)); \ + d(i) += h(i); \ + h(i) += S0(a(i)) + Maj(a(i), b(i), c(i)) \ #ifdef _SHA256_UNROLL -#define RX_8(i) R(i+0); R(i+1); R(i+2); R(i+3); R(i+4); R(i+5); R(i+6); R(i+7); +#define RX_8(i) R(i+0); R(i+1); R(i+2); R(i+3); R(i+4); R(i+5); R(i+6); R(i+7); +#define RX_16 RX_8(0); RX_8(8); + +#else + +#define RX_16 unsigned i; for (i = 0; i < 16; i++) { R(i); } #endif @@ -90,12 +109,30 @@ static const UInt32 K[64] = { 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 }; -static void Sha256_Transform(UInt32 *state, const UInt32 *data) +static void Sha256_WriteByteBlock(CSha256 *p) { UInt32 W[16]; unsigned j; + UInt32 *state; + #ifdef _SHA256_UNROLL2 UInt32 a,b,c,d,e,f,g,h; + #else + UInt32 T[8]; + #endif + + for (j = 0; j < 16; j += 4) + { + const Byte *ccc = p->buffer + j * 4; + W[j ] = GetBe32(ccc); + W[j + 1] = GetBe32(ccc + 4); + W[j + 2] = GetBe32(ccc + 8); + W[j + 3] = GetBe32(ccc + 12); + } + + state = p->state; + + #ifdef _SHA256_UNROLL2 a = state[0]; b = state[1]; c = state[2]; @@ -105,19 +142,13 @@ static void Sha256_Transform(UInt32 *state, const UInt32 *data) g = state[6]; h = state[7]; #else - UInt32 T[8]; for (j = 0; j < 8; j++) T[j] = state[j]; #endif for (j = 0; j < 64; j += 16) { - #if defined(_SHA256_UNROLL) || defined(_SHA256_UNROLL2) - RX_8(0); RX_8(8); - #else - unsigned i; - for (i = 0; i < 16; i++) { R(i); } - #endif + RX_16 } #ifdef _SHA256_UNROLL2 @@ -144,61 +175,74 @@ static void Sha256_Transform(UInt32 *state, const UInt32 *data) #undef s0 #undef s1 -static void Sha256_WriteByteBlock(CSha256 *p) -{ - UInt32 data32[16]; - unsigned i; - for (i = 0; i < 16; i++) - data32[i] = - ((UInt32)(p->buffer[i * 4 ]) << 24) + - ((UInt32)(p->buffer[i * 4 + 1]) << 16) + - ((UInt32)(p->buffer[i * 4 + 2]) << 8) + - ((UInt32)(p->buffer[i * 4 + 3])); - Sha256_Transform(p->state, data32); -} - void Sha256_Update(CSha256 *p, const Byte *data, size_t size) { - UInt32 curBufferPos = (UInt32)p->count & 0x3F; - while (size > 0) + if (size == 0) + return; + { - p->buffer[curBufferPos++] = *data++; - p->count++; - size--; - if (curBufferPos == 64) + unsigned pos = (unsigned)p->count & 0x3F; + unsigned num; + + p->count += size; + + num = 64 - pos; + if (num > size) { - curBufferPos = 0; - Sha256_WriteByteBlock(p); + memcpy(p->buffer + pos, data, size); + return; } + + size -= num; + memcpy(p->buffer + pos, data, num); + data += num; + } + + for (;;) + { + Sha256_WriteByteBlock(p); + if (size < 64) + break; + size -= 64; + memcpy(p->buffer, data, 64); + data += 64; } + + if (size != 0) + memcpy(p->buffer, data, size); } void Sha256_Final(CSha256 *p, Byte *digest) { - UInt64 lenInBits = (p->count << 3); - UInt32 curBufferPos = (UInt32)p->count & 0x3F; + unsigned pos = (unsigned)p->count & 0x3F; unsigned i; - p->buffer[curBufferPos++] = 0x80; - while (curBufferPos != (64 - 8)) + + p->buffer[pos++] = 0x80; + + while (pos != (64 - 8)) { - curBufferPos &= 0x3F; - if (curBufferPos == 0) + pos &= 0x3F; + if (pos == 0) Sha256_WriteByteBlock(p); - p->buffer[curBufferPos++] = 0; + p->buffer[pos++] = 0; } - for (i = 0; i < 8; i++) + { - p->buffer[curBufferPos++] = (Byte)(lenInBits >> 56); - lenInBits <<= 8; + UInt64 numBits = (p->count << 3); + SetBe32(p->buffer + 64 - 8, (UInt32)(numBits >> 32)); + SetBe32(p->buffer + 64 - 4, (UInt32)(numBits)); } + Sha256_WriteByteBlock(p); - for (i = 0; i < 8; i++) + for (i = 0; i < 8; i += 2) { - *digest++ = (Byte)(p->state[i] >> 24); - *digest++ = (Byte)(p->state[i] >> 16); - *digest++ = (Byte)(p->state[i] >> 8); - *digest++ = (Byte)(p->state[i]); + UInt32 v0 = p->state[i]; + UInt32 v1 = p->state[i + 1]; + SetBe32(digest , v0); + SetBe32(digest + 4, v1); + digest += 8; } + Sha256_Init(p); } |