]> pd.if.org Git - zpackage/blob - lib/sha256.c
cleanup atree
[zpackage] / lib / sha256.c
1 /* 
2  * adapted from libtomcrypt by nathan wagner <nw@hydaspes.if.org>
3  *
4  * public domain
5  */
6
7 #include <stdio.h>
8 #include <signal.h>
9 #include <string.h>
10
11 #include "sha256.h"
12
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); }
16
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)); }
22
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); }
28
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)
34
35 #ifndef MIN
36    #define MIN(x, y) ( ((x)<(y))?(x):(y) )
37 #endif
38
39 #define CRYPT_OK 1
40 #define CRYPT_INVALID_ARG 0
41
42 #ifdef LTC_SMALL_CODE
43 /* the K array */
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
58 };
59 #endif
60
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))
70
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;
74 #ifdef LTC_SMALL_CODE
75         uint32_t t;
76 #endif
77         int i;
78
79         /* copy state into S */
80         for (i = 0; i < 8; i++) {
81                 S[i] = md->state[i];
82         }
83
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));
87         }
88
89         /* fill W[16..63] */
90         for (i = 16; i < 64; i++) {
91                 W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
92         }        
93
94         /* Compress */
95 #ifdef LTC_SMALL_CODE   
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);                    \
99         d += t0;                                          \
100         h  = t0 + t1;
101
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;
106         }  
107 #else 
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);                  \
111         d += t0;                                        \
112         h  = t0 + t1;
113
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);
178
179 #undef RND     
180
181 #endif     
182
183         /* feedback */
184         for (i = 0; i < 8; i++) {
185                 md->state[i] = md->state[i] + S[i];
186         }
187         return 1;
188 }
189
190 int sha256_init(struct sha256_state *md) {
191         if (!md) return 0;
192
193         md->curlen = 0;
194         md->length = 0;
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;
203         return 1;
204 }
205
206 /**
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)
210 */
211
212 #define SHA256_BLOCK_SIZE 64
213 int sha256_process(struct sha256_state *md, const unsigned char *in, unsigned long inlen) {
214         unsigned long n;
215         int err;
216
217         if (md->curlen > sizeof(md->buf)) {
218                 return CRYPT_INVALID_ARG;
219         }
220
221         while (inlen > 0) {
222                 if (md->curlen == 0 && inlen >= SHA256_BLOCK_SIZE) {
223                         if ((err = sha256_compress(md, (unsigned char *)in)) != CRYPT_OK) {
224                                 return err;
225                         }
226                         md->length += SHA256_BLOCK_SIZE * 8;
227                         in             += SHA256_BLOCK_SIZE;
228                         inlen          -= SHA256_BLOCK_SIZE;
229                 } else {
230                         n = MIN(inlen, (SHA256_BLOCK_SIZE - md->curlen));
231                         memcpy(md->buf + md->curlen, in, (size_t)n);
232                         md->curlen += n;
233                         in             += n;
234                         inlen          -= n;
235                         if (md->curlen == SHA256_BLOCK_SIZE) {
236                                 if ((err = sha256_compress (md, md->buf)) != CRYPT_OK) {
237                                         return err;
238                                 }
239                                 md->length += 8*SHA256_BLOCK_SIZE;
240                                 md->curlen = 0;
241                         }
242                 }
243         }
244         return CRYPT_OK;
245 }
246
247 /*
248  * Terminate the hash to get the digest
249  * out destination of the hash (32 bytes)
250  */
251 int sha256_done(struct sha256_state *md, unsigned char *out) {
252         int i;
253
254         if (!md || !out) {
255                 return 0;
256         }
257
258         if (md->curlen >= sizeof(md->buf)) {
259                 return 0;
260         }
261
262         /* increase the length of the message */
263         md->length += md->curlen * 8;
264
265         /* append the '1' bit */
266         md->buf[md->curlen++] = (unsigned char)0x80;
267
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.
271          */
272         if (md->curlen > 56) {
273                 while (md->curlen < 64) {
274                         md->buf[md->curlen++] = (unsigned char)0;
275                 }
276                 sha256_compress(md, md->buf);
277                 md->curlen = 0;
278         }
279
280         /* pad upto 56 bytes of zeroes */
281         while (md->curlen < 56) {
282                 md->buf[md->curlen++] = (unsigned char)0;
283         }
284
285         /* store length */
286         STORE64H(md->length, md->buf+56);
287         sha256_compress(md, md->buf);
288
289         /* copy output */
290         for (i = 0; i < 8; i++) {
291                 STORE32H(md->state[i], out+(4*i));
292         }
293         /* TODO zero the hash state */
294         return 1;
295 }