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