diff src/win32/7zip/7z/C/Sha256.c @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/win32/7zip/7z/C/Sha256.c	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,204 @@
     1.4 +/* Crypto/Sha256.c -- SHA-256 Hash function
     1.5 +2008-11-06 : Igor Pavlov : Public domain
     1.6 +This code is based on public domain code from Wei Dai's Crypto++ library. */
     1.7 +
     1.8 +#include "Sha256.h"
     1.9 +#include "RotateDefs.h"
    1.10 +
    1.11 +/* define it for speed optimization */
    1.12 +/* #define _SHA256_UNROLL */
    1.13 +/* #define _SHA256_UNROLL2 */
    1.14 +
    1.15 +void Sha256_Init(CSha256 *p)
    1.16 +{
    1.17 +  p->state[0] = 0x6a09e667;
    1.18 +  p->state[1] = 0xbb67ae85;
    1.19 +  p->state[2] = 0x3c6ef372;
    1.20 +  p->state[3] = 0xa54ff53a;
    1.21 +  p->state[4] = 0x510e527f;
    1.22 +  p->state[5] = 0x9b05688c;
    1.23 +  p->state[6] = 0x1f83d9ab;
    1.24 +  p->state[7] = 0x5be0cd19;
    1.25 +  p->count = 0;
    1.26 +}
    1.27 +
    1.28 +#define S0(x) (rotrFixed(x, 2) ^ rotrFixed(x,13) ^ rotrFixed(x, 22))
    1.29 +#define S1(x) (rotrFixed(x, 6) ^ rotrFixed(x,11) ^ rotrFixed(x, 25))
    1.30 +#define s0(x) (rotrFixed(x, 7) ^ rotrFixed(x,18) ^ (x >> 3))
    1.31 +#define s1(x) (rotrFixed(x,17) ^ rotrFixed(x,19) ^ (x >> 10))
    1.32 +
    1.33 +#define blk0(i) (W[i] = data[i])
    1.34 +#define blk2(i) (W[i&15] += s1(W[(i-2)&15]) + W[(i-7)&15] + s0(W[(i-15)&15]))
    1.35 +
    1.36 +#define Ch(x,y,z) (z^(x&(y^z)))
    1.37 +#define Maj(x,y,z) ((x&y)|(z&(x|y)))
    1.38 +
    1.39 +#define a(i) T[(0-(i))&7]
    1.40 +#define b(i) T[(1-(i))&7]
    1.41 +#define c(i) T[(2-(i))&7]
    1.42 +#define d(i) T[(3-(i))&7]
    1.43 +#define e(i) T[(4-(i))&7]
    1.44 +#define f(i) T[(5-(i))&7]
    1.45 +#define g(i) T[(6-(i))&7]
    1.46 +#define h(i) T[(7-(i))&7]
    1.47 +
    1.48 +
    1.49 +#ifdef _SHA256_UNROLL2
    1.50 +
    1.51 +#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));\
    1.52 +  d += h; h += S0(a) + Maj(a, b, c)
    1.53 +
    1.54 +#define RX_8(i) \
    1.55 +  R(a,b,c,d,e,f,g,h, i); \
    1.56 +  R(h,a,b,c,d,e,f,g, i+1); \
    1.57 +  R(g,h,a,b,c,d,e,f, i+2); \
    1.58 +  R(f,g,h,a,b,c,d,e, i+3); \
    1.59 +  R(e,f,g,h,a,b,c,d, i+4); \
    1.60 +  R(d,e,f,g,h,a,b,c, i+5); \
    1.61 +  R(c,d,e,f,g,h,a,b, i+6); \
    1.62 +  R(b,c,d,e,f,g,h,a, i+7)
    1.63 +
    1.64 +#else
    1.65 +
    1.66 +#define R(i) h(i) += S1(e(i)) + Ch(e(i),f(i),g(i)) + K[i+j] + (j?blk2(i):blk0(i));\
    1.67 +  d(i) += h(i); h(i) += S0(a(i)) + Maj(a(i), b(i), c(i))
    1.68 +
    1.69 +#ifdef _SHA256_UNROLL
    1.70 +
    1.71 +#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);
    1.72 +
    1.73 +#endif
    1.74 +
    1.75 +#endif
    1.76 +
    1.77 +const UInt32 K[64] = {
    1.78 +  0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
    1.79 +  0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
    1.80 +  0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
    1.81 +  0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
    1.82 +  0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
    1.83 +  0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
    1.84 +  0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
    1.85 +  0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
    1.86 +  0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
    1.87 +  0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
    1.88 +  0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
    1.89 +  0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
    1.90 +  0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
    1.91 +  0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
    1.92 +  0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
    1.93 +  0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
    1.94 +};
    1.95 +
    1.96 +static void Sha256_Transform(UInt32 *state, const UInt32 *data)
    1.97 +{
    1.98 +  UInt32 W[16];
    1.99 +  unsigned j;
   1.100 +  #ifdef _SHA256_UNROLL2
   1.101 +  UInt32 a,b,c,d,e,f,g,h;
   1.102 +  a = state[0];
   1.103 +  b = state[1];
   1.104 +  c = state[2];
   1.105 +  d = state[3];
   1.106 +  e = state[4];
   1.107 +  f = state[5];
   1.108 +  g = state[6];
   1.109 +  h = state[7];
   1.110 +  #else
   1.111 +  UInt32 T[8];
   1.112 +  for (j = 0; j < 8; j++)
   1.113 +    T[j] = state[j];
   1.114 +  #endif
   1.115 +
   1.116 +  for (j = 0; j < 64; j += 16)
   1.117 +  {
   1.118 +    #if defined(_SHA256_UNROLL) || defined(_SHA256_UNROLL2)
   1.119 +    RX_8(0); RX_8(8);
   1.120 +    #else
   1.121 +    unsigned i;
   1.122 +    for (i = 0; i < 16; i++) { R(i); }
   1.123 +    #endif
   1.124 +  }
   1.125 +
   1.126 +  #ifdef _SHA256_UNROLL2
   1.127 +  state[0] += a;
   1.128 +  state[1] += b;
   1.129 +  state[2] += c;
   1.130 +  state[3] += d;
   1.131 +  state[4] += e;
   1.132 +  state[5] += f;
   1.133 +  state[6] += g;
   1.134 +  state[7] += h;
   1.135 +  #else
   1.136 +  for (j = 0; j < 8; j++)
   1.137 +    state[j] += T[j];
   1.138 +  #endif
   1.139 +  
   1.140 +  /* Wipe variables */
   1.141 +  /* memset(W, 0, sizeof(W)); */
   1.142 +  /* memset(T, 0, sizeof(T)); */
   1.143 +}
   1.144 +
   1.145 +#undef S0
   1.146 +#undef S1
   1.147 +#undef s0
   1.148 +#undef s1
   1.149 +
   1.150 +static void Sha256_WriteByteBlock(CSha256 *p)
   1.151 +{
   1.152 +  UInt32 data32[16];
   1.153 +  unsigned i;
   1.154 +  for (i = 0; i < 16; i++)
   1.155 +    data32[i] =
   1.156 +      ((UInt32)(p->buffer[i * 4    ]) << 24) +
   1.157 +      ((UInt32)(p->buffer[i * 4 + 1]) << 16) +
   1.158 +      ((UInt32)(p->buffer[i * 4 + 2]) <<  8) +
   1.159 +      ((UInt32)(p->buffer[i * 4 + 3]));
   1.160 +  Sha256_Transform(p->state, data32);
   1.161 +}
   1.162 +
   1.163 +void Sha256_Update(CSha256 *p, const Byte *data, size_t size)
   1.164 +{
   1.165 +  UInt32 curBufferPos = (UInt32)p->count & 0x3F;
   1.166 +  while (size > 0)
   1.167 +  {
   1.168 +    p->buffer[curBufferPos++] = *data++;
   1.169 +    p->count++;
   1.170 +    size--;
   1.171 +    if (curBufferPos == 64)
   1.172 +    {
   1.173 +      curBufferPos = 0;
   1.174 +      Sha256_WriteByteBlock(p);
   1.175 +    }
   1.176 +  }
   1.177 +}
   1.178 +
   1.179 +void Sha256_Final(CSha256 *p, Byte *digest)
   1.180 +{
   1.181 +  UInt64 lenInBits = (p->count << 3);
   1.182 +  UInt32 curBufferPos = (UInt32)p->count & 0x3F;
   1.183 +  unsigned i;
   1.184 +  p->buffer[curBufferPos++] = 0x80;
   1.185 +  while (curBufferPos != (64 - 8))
   1.186 +  {
   1.187 +    curBufferPos &= 0x3F;
   1.188 +    if (curBufferPos == 0)
   1.189 +      Sha256_WriteByteBlock(p);
   1.190 +    p->buffer[curBufferPos++] = 0;
   1.191 +  }
   1.192 +  for (i = 0; i < 8; i++)
   1.193 +  {
   1.194 +    p->buffer[curBufferPos++] = (Byte)(lenInBits >> 56);
   1.195 +    lenInBits <<= 8;
   1.196 +  }
   1.197 +  Sha256_WriteByteBlock(p);
   1.198 +
   1.199 +  for (i = 0; i < 8; i++)
   1.200 +  {
   1.201 +    *digest++ = (Byte)((p->state[i] >> 24) & 0xFF);
   1.202 +    *digest++ = (Byte)((p->state[i] >> 16) & 0xFF);
   1.203 +    *digest++ = (Byte)((p->state[i] >> 8) & 0xFF);
   1.204 +    *digest++ = (Byte)((p->state[i]) & 0xFF);
   1.205 +  }
   1.206 +  Sha256_Init(p);
   1.207 +}