2 * adapted from libtomcrypt by nathan wagner <nw@hydaspes.if.org>
13 #define STORE32H(x, y) \
14 { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \
15 (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); }
17 #define LOAD32H(x, y) \
18 { x = ((unsigned long)((y)[0] & 255)<<24) | \
19 ((unsigned long)((y)[1] & 255)<<16) | \
20 ((unsigned long)((y)[2] & 255)<<8) | \
21 ((unsigned long)((y)[3] & 255)); }
23 #define STORE64H(x, y) \
24 { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
25 (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
26 (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
27 (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }
29 /* rotates the hard way */
30 #define ROL(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
31 #define ROR(x, y) ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | ((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
32 #define ROLc(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
33 #define RORc(x, y) ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | ((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
36 #define MIN(x, y) ( ((x)<(y))?(x):(y) )
40 #define CRYPT_INVALID_ARG 0
44 static const ulong32 K[64] = {
45 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL,
46 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 0xd807aa98UL, 0x12835b01UL,
47 0x243185beUL, 0x550c7dc3UL, 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL,
48 0xc19bf174UL, 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
49 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, 0x983e5152UL,
50 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, 0xc6e00bf3UL, 0xd5a79147UL,
51 0x06ca6351UL, 0x14292967UL, 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL,
52 0x53380d13UL, 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
53 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, 0xd192e819UL,
54 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, 0x19a4c116UL, 0x1e376c08UL,
55 0x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL,
56 0x682e6ff3UL, 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
57 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
61 /* Various logical functions */
62 #define Ch(x,y,z) (z ^ (x & (y ^ z)))
63 #define Maj(x,y,z) (((x | y) & z) | (x & y))
64 #define S(x, n) RORc((x),(n))
65 #define R(x, n) (((x)&0xFFFFFFFFUL)>>(n))
66 #define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
67 #define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25))
68 #define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3))
69 #define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10))
71 /* compress 512-bits */
72 static int sha256_compress(struct sha256_state * md, unsigned char *buf) {
73 uint32_t S[8], W[64], t0, t1;
79 /* copy state into S */
80 for (i = 0; i < 8; i++) {
84 /* copy the state into 512-bits into W[0..15] */
85 for (i = 0; i < 16; i++) {
86 LOAD32H(W[i], buf + (4*i));
90 for (i = 16; i < 64; i++) {
91 W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
96 #define RND(a,b,c,d,e,f,g,h,i) \
97 t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; \
98 t1 = Sigma0(a) + Maj(a, b, c); \
102 for (i = 0; i < 64; ++i) {
103 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],i);
104 t = S[7]; S[7] = S[6]; S[6] = S[5]; S[5] = S[4];
105 S[4] = S[3]; S[3] = S[2]; S[2] = S[1]; S[1] = S[0]; S[0] = t;
108 #define RND(a,b,c,d,e,f,g,h,i,ki) \
109 t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \
110 t1 = Sigma0(a) + Maj(a, b, c); \
114 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98);
115 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x71374491);
116 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcf);
117 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba5);
118 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25b);
119 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1);
120 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4);
121 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5);
122 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98);
123 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b01);
124 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be);
125 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3);
126 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74);
127 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe);
128 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a7);
129 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174);
130 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c1);
131 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786);
132 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc6);
133 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc);
134 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f);
135 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa);
136 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dc);
137 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da);
138 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152);
139 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d);
140 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c8);
141 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7);
142 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf3);
143 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147);
144 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351);
145 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x14292967);
146 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a85);
147 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b2138);
148 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc);
149 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d13);
150 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a7354);
151 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb);
152 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e);
153 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c85);
154 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a1);
155 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664b);
156 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70);
157 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a3);
158 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819);
159 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd6990624);
160 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e3585);
161 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa070);
162 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116);
163 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c08);
164 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774c);
165 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5);
166 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3);
167 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4a);
168 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f);
169 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3);
170 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee);
171 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f);
172 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814);
173 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc70208);
174 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa);
175 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506ceb);
176 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7);
177 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2);
184 for (i = 0; i < 8; i++) {
185 md->state[i] = md->state[i] + S[i];
190 int sha256_init(struct sha256_state *md) {
195 md->state[0] = 0x6A09E667UL;
196 md->state[1] = 0xBB67AE85UL;
197 md->state[2] = 0x3C6EF372UL;
198 md->state[3] = 0xA54FF53AUL;
199 md->state[4] = 0x510E527FUL;
200 md->state[5] = 0x9B05688CUL;
201 md->state[6] = 0x1F83D9ABUL;
202 md->state[7] = 0x5BE0CD19UL;
207 Process a block of memory though the hash
208 @param in The data to hash
209 @param inlen The length of the data (octets)
212 #define SHA256_BLOCK_SIZE 64
213 int sha256_process(struct sha256_state *md, const unsigned char *in, unsigned long inlen) {
217 if (md->curlen > sizeof(md->buf)) {
218 return CRYPT_INVALID_ARG;
222 if (md->curlen == 0 && inlen >= SHA256_BLOCK_SIZE) {
223 if ((err = sha256_compress(md, (unsigned char *)in)) != CRYPT_OK) {
226 md->length += SHA256_BLOCK_SIZE * 8;
227 in += SHA256_BLOCK_SIZE;
228 inlen -= SHA256_BLOCK_SIZE;
230 n = MIN(inlen, (SHA256_BLOCK_SIZE - md->curlen));
231 memcpy(md->buf + md->curlen, in, (size_t)n);
235 if (md->curlen == SHA256_BLOCK_SIZE) {
236 if ((err = sha256_compress (md, md->buf)) != CRYPT_OK) {
239 md->length += 8*SHA256_BLOCK_SIZE;
248 * Terminate the hash to get the digest
249 * out destination of the hash (32 bytes)
251 int sha256_done(struct sha256_state *md, unsigned char *out) {
258 if (md->curlen >= sizeof(md->buf)) {
262 /* increase the length of the message */
263 md->length += md->curlen * 8;
265 /* append the '1' bit */
266 md->buf[md->curlen++] = (unsigned char)0x80;
268 /* if the length is currently above 56 bytes we append zeros
269 * then compress. Then we can fall back to padding zeros and length
270 * encoding like normal.
272 if (md->curlen > 56) {
273 while (md->curlen < 64) {
274 md->buf[md->curlen++] = (unsigned char)0;
276 sha256_compress(md, md->buf);
280 /* pad upto 56 bytes of zeroes */
281 while (md->curlen < 56) {
282 md->buf[md->curlen++] = (unsigned char)0;
286 STORE64H(md->length, md->buf+56);
287 sha256_compress(md, md->buf);
290 for (i = 0; i < 8; i++) {
291 STORE32H(md->state[i], out+(4*i));
293 /* TODO zero the hash state */