Home | History | Annotate | Download | only in C
      1 /* Crypto/Sha256.c -- SHA-256 Hash
      2 2010-06-11 : Igor Pavlov : Public domain
      3 This code is based on public domain code from Wei Dai's Crypto++ library. */
      4 
      5 #include "Precomp.h"
      6 
      7 #include "RotateDefs.h"
      8 #include "Sha256.h"
      9 
     10 /* define it for speed optimization */
     11 /* #define _SHA256_UNROLL */
     12 /* #define _SHA256_UNROLL2 */
     13 
     14 void Sha256_Init(CSha256 *p)
     15 {
     16   p->state[0] = 0x6a09e667;
     17   p->state[1] = 0xbb67ae85;
     18   p->state[2] = 0x3c6ef372;
     19   p->state[3] = 0xa54ff53a;
     20   p->state[4] = 0x510e527f;
     21   p->state[5] = 0x9b05688c;
     22   p->state[6] = 0x1f83d9ab;
     23   p->state[7] = 0x5be0cd19;
     24   p->count = 0;
     25 }
     26 
     27 #define S0(x) (rotrFixed(x, 2) ^ rotrFixed(x,13) ^ rotrFixed(x, 22))
     28 #define S1(x) (rotrFixed(x, 6) ^ rotrFixed(x,11) ^ rotrFixed(x, 25))
     29 #define s0(x) (rotrFixed(x, 7) ^ rotrFixed(x,18) ^ (x >> 3))
     30 #define s1(x) (rotrFixed(x,17) ^ rotrFixed(x,19) ^ (x >> 10))
     31 
     32 #define blk0(i) (W[i] = data[i])
     33 #define blk2(i) (W[i&15] += s1(W[(i-2)&15]) + W[(i-7)&15] + s0(W[(i-15)&15]))
     34 
     35 #define Ch(x,y,z) (z^(x&(y^z)))
     36 #define Maj(x,y,z) ((x&y)|(z&(x|y)))
     37 
     38 #define a(i) T[(0-(i))&7]
     39 #define b(i) T[(1-(i))&7]
     40 #define c(i) T[(2-(i))&7]
     41 #define d(i) T[(3-(i))&7]
     42 #define e(i) T[(4-(i))&7]
     43 #define f(i) T[(5-(i))&7]
     44 #define g(i) T[(6-(i))&7]
     45 #define h(i) T[(7-(i))&7]
     46 
     47 
     48 #ifdef _SHA256_UNROLL2
     49 
     50 #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));\
     51   d += h; h += S0(a) + Maj(a, b, c)
     52 
     53 #define RX_8(i) \
     54   R(a,b,c,d,e,f,g,h, i); \
     55   R(h,a,b,c,d,e,f,g, i+1); \
     56   R(g,h,a,b,c,d,e,f, i+2); \
     57   R(f,g,h,a,b,c,d,e, i+3); \
     58   R(e,f,g,h,a,b,c,d, i+4); \
     59   R(d,e,f,g,h,a,b,c, i+5); \
     60   R(c,d,e,f,g,h,a,b, i+6); \
     61   R(b,c,d,e,f,g,h,a, i+7)
     62 
     63 #else
     64 
     65 #define R(i) h(i) += S1(e(i)) + Ch(e(i),f(i),g(i)) + K[i+j] + (j?blk2(i):blk0(i));\
     66   d(i) += h(i); h(i) += S0(a(i)) + Maj(a(i), b(i), c(i))
     67 
     68 #ifdef _SHA256_UNROLL
     69 
     70 #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);
     71 
     72 #endif
     73 
     74 #endif
     75 
     76 static const UInt32 K[64] = {
     77   0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
     78   0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
     79   0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
     80   0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
     81   0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
     82   0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
     83   0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
     84   0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
     85   0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
     86   0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
     87   0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
     88   0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
     89   0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
     90   0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
     91   0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
     92   0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
     93 };
     94 
     95 static void Sha256_Transform(UInt32 *state, const UInt32 *data)
     96 {
     97   UInt32 W[16];
     98   unsigned j;
     99   #ifdef _SHA256_UNROLL2
    100   UInt32 a,b,c,d,e,f,g,h;
    101   a = state[0];
    102   b = state[1];
    103   c = state[2];
    104   d = state[3];
    105   e = state[4];
    106   f = state[5];
    107   g = state[6];
    108   h = state[7];
    109   #else
    110   UInt32 T[8];
    111   for (j = 0; j < 8; j++)
    112     T[j] = state[j];
    113   #endif
    114 
    115   for (j = 0; j < 64; j += 16)
    116   {
    117     #if defined(_SHA256_UNROLL) || defined(_SHA256_UNROLL2)
    118     RX_8(0); RX_8(8);
    119     #else
    120     unsigned i;
    121     for (i = 0; i < 16; i++) { R(i); }
    122     #endif
    123   }
    124 
    125   #ifdef _SHA256_UNROLL2
    126   state[0] += a;
    127   state[1] += b;
    128   state[2] += c;
    129   state[3] += d;
    130   state[4] += e;
    131   state[5] += f;
    132   state[6] += g;
    133   state[7] += h;
    134   #else
    135   for (j = 0; j < 8; j++)
    136     state[j] += T[j];
    137   #endif
    138 
    139   /* Wipe variables */
    140   /* memset(W, 0, sizeof(W)); */
    141   /* memset(T, 0, sizeof(T)); */
    142 }
    143 
    144 #undef S0
    145 #undef S1
    146 #undef s0
    147 #undef s1
    148 
    149 static void Sha256_WriteByteBlock(CSha256 *p)
    150 {
    151   UInt32 data32[16];
    152   unsigned i;
    153   for (i = 0; i < 16; i++)
    154     data32[i] =
    155       ((UInt32)(p->buffer[i * 4    ]) << 24) +
    156       ((UInt32)(p->buffer[i * 4 + 1]) << 16) +
    157       ((UInt32)(p->buffer[i * 4 + 2]) <<  8) +
    158       ((UInt32)(p->buffer[i * 4 + 3]));
    159   Sha256_Transform(p->state, data32);
    160 }
    161 
    162 void Sha256_Update(CSha256 *p, const Byte *data, size_t size)
    163 {
    164   UInt32 curBufferPos = (UInt32)p->count & 0x3F;
    165   while (size > 0)
    166   {
    167     p->buffer[curBufferPos++] = *data++;
    168     p->count++;
    169     size--;
    170     if (curBufferPos == 64)
    171     {
    172       curBufferPos = 0;
    173       Sha256_WriteByteBlock(p);
    174     }
    175   }
    176 }
    177 
    178 void Sha256_Final(CSha256 *p, Byte *digest)
    179 {
    180   UInt64 lenInBits = (p->count << 3);
    181   UInt32 curBufferPos = (UInt32)p->count & 0x3F;
    182   unsigned i;
    183   p->buffer[curBufferPos++] = 0x80;
    184   while (curBufferPos != (64 - 8))
    185   {
    186     curBufferPos &= 0x3F;
    187     if (curBufferPos == 0)
    188       Sha256_WriteByteBlock(p);
    189     p->buffer[curBufferPos++] = 0;
    190   }
    191   for (i = 0; i < 8; i++)
    192   {
    193     p->buffer[curBufferPos++] = (Byte)(lenInBits >> 56);
    194     lenInBits <<= 8;
    195   }
    196   Sha256_WriteByteBlock(p);
    197 
    198   for (i = 0; i < 8; i++)
    199   {
    200     *digest++ = (Byte)(p->state[i] >> 24);
    201     *digest++ = (Byte)(p->state[i] >> 16);
    202     *digest++ = (Byte)(p->state[i] >> 8);
    203     *digest++ = (Byte)(p->state[i]);
    204   }
    205   Sha256_Init(p);
    206 }
    207