Rework hash implementations to provide additional algorithms.
[cumulus.git] / third_party / sha256.cc
1 /* sha256.cc - Message digests for Cumulus using SHA256
2  *
3  * This code is adapted from crypto/sha256_generic.c and include/crypto/sha.n
4  * from the Linux kernel version 3.4.
5  */
6
7 /* SHA-256, as specified in
8  * http://csrc.nist.gov/groups/STM/cavp/documents/shs/sha256-384-512.pdf
9  *
10  * SHA-256 code by Jean-Luc Cooke <jlcooke@certainkey.com>.
11  *
12  * Copyright (c) Jean-Luc Cooke <jlcooke@certainkey.com>
13  * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
14  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
15  * SHA224 Support Copyright 2007 Intel Corporation <jonathan.lynch@intel.com>
16  *
17  * This program is free software; you can redistribute it and/or modify it
18  * under the terms of the GNU General Public License as published by the Free
19  * Software Foundation; either version 2 of the License, or (at your option) 
20  * any later version.
21  *
22  */
23
24 #define _BSD_SOURCE
25 #include <endian.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdint.h>
29 #include <string.h>
30
31 #include "../hash.h"
32
33 /* Adaptation code for a non-kernel build environment. */
34 typedef uint8_t u8;
35 typedef uint32_t u32;
36 typedef uint64_t u64;
37
38 static inline u32 ror32(u32 x, int n)
39 {
40         return (x >> n) | (x << (32 - n));
41 }
42
43 static inline u32 cpu_to_be32(u32 x)
44 {
45         return htobe32(x);
46 }
47
48 static inline u64 cpu_to_be64(u64 x)
49 {
50         return htobe64(x);
51 }
52
53 /* Originally from sha.h. */
54
55 #define SHA224_DIGEST_SIZE      28
56 #define SHA224_BLOCK_SIZE       64
57
58 #define SHA256_DIGEST_SIZE      32
59 #define SHA256_BLOCK_SIZE       64
60
61 #define SHA224_H0       0xc1059ed8UL
62 #define SHA224_H1       0x367cd507UL
63 #define SHA224_H2       0x3070dd17UL
64 #define SHA224_H3       0xf70e5939UL
65 #define SHA224_H4       0xffc00b31UL
66 #define SHA224_H5       0x68581511UL
67 #define SHA224_H6       0x64f98fa7UL
68 #define SHA224_H7       0xbefa4fa4UL
69
70 #define SHA256_H0       0x6a09e667UL
71 #define SHA256_H1       0xbb67ae85UL
72 #define SHA256_H2       0x3c6ef372UL
73 #define SHA256_H3       0xa54ff53aUL
74 #define SHA256_H4       0x510e527fUL
75 #define SHA256_H5       0x9b05688cUL
76 #define SHA256_H6       0x1f83d9abUL
77 #define SHA256_H7       0x5be0cd19UL
78
79 struct sha256_state {
80         u64 count;
81         u32 state[SHA256_DIGEST_SIZE / 4];
82         u8 buf[SHA256_BLOCK_SIZE];
83 };
84
85 /* Originally from sha256_generic.c */
86
87 static inline u32 Ch(u32 x, u32 y, u32 z)
88 {
89         return z ^ (x & (y ^ z));
90 }
91
92 static inline u32 Maj(u32 x, u32 y, u32 z)
93 {
94         return (x & y) | (z & (x | y));
95 }
96
97 #define e0(x)       (ror32(x, 2) ^ ror32(x,13) ^ ror32(x,22))
98 #define e1(x)       (ror32(x, 6) ^ ror32(x,11) ^ ror32(x,25))
99 #define s0(x)       (ror32(x, 7) ^ ror32(x,18) ^ (x >> 3))
100 #define s1(x)       (ror32(x,17) ^ ror32(x,19) ^ (x >> 10))
101
102 static inline void LOAD_OP(int I, u32 *W, const u8 *input)
103 {
104         W[I] = (input[4*I] << 24) | (input[4*I+1] << 16)
105                 | (input[4*I+2] << 8) | input[4*I+3];
106 }
107
108 static inline void BLEND_OP(int I, u32 *W)
109 {
110         W[I] = s1(W[I-2]) + W[I-7] + s0(W[I-15]) + W[I-16];
111 }
112
113 static void sha256_transform(u32 *state, const u8 *input)
114 {
115         u32 a, b, c, d, e, f, g, h, t1, t2;
116         u32 W[64];
117         int i;
118
119         /* load the input */
120         for (i = 0; i < 16; i++)
121                 LOAD_OP(i, W, input);
122
123         /* now blend */
124         for (i = 16; i < 64; i++)
125                 BLEND_OP(i, W);
126
127         /* load the state into our registers */
128         a=state[0];  b=state[1];  c=state[2];  d=state[3];
129         e=state[4];  f=state[5];  g=state[6];  h=state[7];
130
131         /* now iterate */
132         t1 = h + e1(e) + Ch(e,f,g) + 0x428a2f98 + W[ 0];
133         t2 = e0(a) + Maj(a,b,c);    d+=t1;    h=t1+t2;
134         t1 = g + e1(d) + Ch(d,e,f) + 0x71374491 + W[ 1];
135         t2 = e0(h) + Maj(h,a,b);    c+=t1;    g=t1+t2;
136         t1 = f + e1(c) + Ch(c,d,e) + 0xb5c0fbcf + W[ 2];
137         t2 = e0(g) + Maj(g,h,a);    b+=t1;    f=t1+t2;
138         t1 = e + e1(b) + Ch(b,c,d) + 0xe9b5dba5 + W[ 3];
139         t2 = e0(f) + Maj(f,g,h);    a+=t1;    e=t1+t2;
140         t1 = d + e1(a) + Ch(a,b,c) + 0x3956c25b + W[ 4];
141         t2 = e0(e) + Maj(e,f,g);    h+=t1;    d=t1+t2;
142         t1 = c + e1(h) + Ch(h,a,b) + 0x59f111f1 + W[ 5];
143         t2 = e0(d) + Maj(d,e,f);    g+=t1;    c=t1+t2;
144         t1 = b + e1(g) + Ch(g,h,a) + 0x923f82a4 + W[ 6];
145         t2 = e0(c) + Maj(c,d,e);    f+=t1;    b=t1+t2;
146         t1 = a + e1(f) + Ch(f,g,h) + 0xab1c5ed5 + W[ 7];
147         t2 = e0(b) + Maj(b,c,d);    e+=t1;    a=t1+t2;
148
149         t1 = h + e1(e) + Ch(e,f,g) + 0xd807aa98 + W[ 8];
150         t2 = e0(a) + Maj(a,b,c);    d+=t1;    h=t1+t2;
151         t1 = g + e1(d) + Ch(d,e,f) + 0x12835b01 + W[ 9];
152         t2 = e0(h) + Maj(h,a,b);    c+=t1;    g=t1+t2;
153         t1 = f + e1(c) + Ch(c,d,e) + 0x243185be + W[10];
154         t2 = e0(g) + Maj(g,h,a);    b+=t1;    f=t1+t2;
155         t1 = e + e1(b) + Ch(b,c,d) + 0x550c7dc3 + W[11];
156         t2 = e0(f) + Maj(f,g,h);    a+=t1;    e=t1+t2;
157         t1 = d + e1(a) + Ch(a,b,c) + 0x72be5d74 + W[12];
158         t2 = e0(e) + Maj(e,f,g);    h+=t1;    d=t1+t2;
159         t1 = c + e1(h) + Ch(h,a,b) + 0x80deb1fe + W[13];
160         t2 = e0(d) + Maj(d,e,f);    g+=t1;    c=t1+t2;
161         t1 = b + e1(g) + Ch(g,h,a) + 0x9bdc06a7 + W[14];
162         t2 = e0(c) + Maj(c,d,e);    f+=t1;    b=t1+t2;
163         t1 = a + e1(f) + Ch(f,g,h) + 0xc19bf174 + W[15];
164         t2 = e0(b) + Maj(b,c,d);    e+=t1;    a=t1+t2;
165
166         t1 = h + e1(e) + Ch(e,f,g) + 0xe49b69c1 + W[16];
167         t2 = e0(a) + Maj(a,b,c);    d+=t1;    h=t1+t2;
168         t1 = g + e1(d) + Ch(d,e,f) + 0xefbe4786 + W[17];
169         t2 = e0(h) + Maj(h,a,b);    c+=t1;    g=t1+t2;
170         t1 = f + e1(c) + Ch(c,d,e) + 0x0fc19dc6 + W[18];
171         t2 = e0(g) + Maj(g,h,a);    b+=t1;    f=t1+t2;
172         t1 = e + e1(b) + Ch(b,c,d) + 0x240ca1cc + W[19];
173         t2 = e0(f) + Maj(f,g,h);    a+=t1;    e=t1+t2;
174         t1 = d + e1(a) + Ch(a,b,c) + 0x2de92c6f + W[20];
175         t2 = e0(e) + Maj(e,f,g);    h+=t1;    d=t1+t2;
176         t1 = c + e1(h) + Ch(h,a,b) + 0x4a7484aa + W[21];
177         t2 = e0(d) + Maj(d,e,f);    g+=t1;    c=t1+t2;
178         t1 = b + e1(g) + Ch(g,h,a) + 0x5cb0a9dc + W[22];
179         t2 = e0(c) + Maj(c,d,e);    f+=t1;    b=t1+t2;
180         t1 = a + e1(f) + Ch(f,g,h) + 0x76f988da + W[23];
181         t2 = e0(b) + Maj(b,c,d);    e+=t1;    a=t1+t2;
182
183         t1 = h + e1(e) + Ch(e,f,g) + 0x983e5152 + W[24];
184         t2 = e0(a) + Maj(a,b,c);    d+=t1;    h=t1+t2;
185         t1 = g + e1(d) + Ch(d,e,f) + 0xa831c66d + W[25];
186         t2 = e0(h) + Maj(h,a,b);    c+=t1;    g=t1+t2;
187         t1 = f + e1(c) + Ch(c,d,e) + 0xb00327c8 + W[26];
188         t2 = e0(g) + Maj(g,h,a);    b+=t1;    f=t1+t2;
189         t1 = e + e1(b) + Ch(b,c,d) + 0xbf597fc7 + W[27];
190         t2 = e0(f) + Maj(f,g,h);    a+=t1;    e=t1+t2;
191         t1 = d + e1(a) + Ch(a,b,c) + 0xc6e00bf3 + W[28];
192         t2 = e0(e) + Maj(e,f,g);    h+=t1;    d=t1+t2;
193         t1 = c + e1(h) + Ch(h,a,b) + 0xd5a79147 + W[29];
194         t2 = e0(d) + Maj(d,e,f);    g+=t1;    c=t1+t2;
195         t1 = b + e1(g) + Ch(g,h,a) + 0x06ca6351 + W[30];
196         t2 = e0(c) + Maj(c,d,e);    f+=t1;    b=t1+t2;
197         t1 = a + e1(f) + Ch(f,g,h) + 0x14292967 + W[31];
198         t2 = e0(b) + Maj(b,c,d);    e+=t1;    a=t1+t2;
199
200         t1 = h + e1(e) + Ch(e,f,g) + 0x27b70a85 + W[32];
201         t2 = e0(a) + Maj(a,b,c);    d+=t1;    h=t1+t2;
202         t1 = g + e1(d) + Ch(d,e,f) + 0x2e1b2138 + W[33];
203         t2 = e0(h) + Maj(h,a,b);    c+=t1;    g=t1+t2;
204         t1 = f + e1(c) + Ch(c,d,e) + 0x4d2c6dfc + W[34];
205         t2 = e0(g) + Maj(g,h,a);    b+=t1;    f=t1+t2;
206         t1 = e + e1(b) + Ch(b,c,d) + 0x53380d13 + W[35];
207         t2 = e0(f) + Maj(f,g,h);    a+=t1;    e=t1+t2;
208         t1 = d + e1(a) + Ch(a,b,c) + 0x650a7354 + W[36];
209         t2 = e0(e) + Maj(e,f,g);    h+=t1;    d=t1+t2;
210         t1 = c + e1(h) + Ch(h,a,b) + 0x766a0abb + W[37];
211         t2 = e0(d) + Maj(d,e,f);    g+=t1;    c=t1+t2;
212         t1 = b + e1(g) + Ch(g,h,a) + 0x81c2c92e + W[38];
213         t2 = e0(c) + Maj(c,d,e);    f+=t1;    b=t1+t2;
214         t1 = a + e1(f) + Ch(f,g,h) + 0x92722c85 + W[39];
215         t2 = e0(b) + Maj(b,c,d);    e+=t1;    a=t1+t2;
216
217         t1 = h + e1(e) + Ch(e,f,g) + 0xa2bfe8a1 + W[40];
218         t2 = e0(a) + Maj(a,b,c);    d+=t1;    h=t1+t2;
219         t1 = g + e1(d) + Ch(d,e,f) + 0xa81a664b + W[41];
220         t2 = e0(h) + Maj(h,a,b);    c+=t1;    g=t1+t2;
221         t1 = f + e1(c) + Ch(c,d,e) + 0xc24b8b70 + W[42];
222         t2 = e0(g) + Maj(g,h,a);    b+=t1;    f=t1+t2;
223         t1 = e + e1(b) + Ch(b,c,d) + 0xc76c51a3 + W[43];
224         t2 = e0(f) + Maj(f,g,h);    a+=t1;    e=t1+t2;
225         t1 = d + e1(a) + Ch(a,b,c) + 0xd192e819 + W[44];
226         t2 = e0(e) + Maj(e,f,g);    h+=t1;    d=t1+t2;
227         t1 = c + e1(h) + Ch(h,a,b) + 0xd6990624 + W[45];
228         t2 = e0(d) + Maj(d,e,f);    g+=t1;    c=t1+t2;
229         t1 = b + e1(g) + Ch(g,h,a) + 0xf40e3585 + W[46];
230         t2 = e0(c) + Maj(c,d,e);    f+=t1;    b=t1+t2;
231         t1 = a + e1(f) + Ch(f,g,h) + 0x106aa070 + W[47];
232         t2 = e0(b) + Maj(b,c,d);    e+=t1;    a=t1+t2;
233
234         t1 = h + e1(e) + Ch(e,f,g) + 0x19a4c116 + W[48];
235         t2 = e0(a) + Maj(a,b,c);    d+=t1;    h=t1+t2;
236         t1 = g + e1(d) + Ch(d,e,f) + 0x1e376c08 + W[49];
237         t2 = e0(h) + Maj(h,a,b);    c+=t1;    g=t1+t2;
238         t1 = f + e1(c) + Ch(c,d,e) + 0x2748774c + W[50];
239         t2 = e0(g) + Maj(g,h,a);    b+=t1;    f=t1+t2;
240         t1 = e + e1(b) + Ch(b,c,d) + 0x34b0bcb5 + W[51];
241         t2 = e0(f) + Maj(f,g,h);    a+=t1;    e=t1+t2;
242         t1 = d + e1(a) + Ch(a,b,c) + 0x391c0cb3 + W[52];
243         t2 = e0(e) + Maj(e,f,g);    h+=t1;    d=t1+t2;
244         t1 = c + e1(h) + Ch(h,a,b) + 0x4ed8aa4a + W[53];
245         t2 = e0(d) + Maj(d,e,f);    g+=t1;    c=t1+t2;
246         t1 = b + e1(g) + Ch(g,h,a) + 0x5b9cca4f + W[54];
247         t2 = e0(c) + Maj(c,d,e);    f+=t1;    b=t1+t2;
248         t1 = a + e1(f) + Ch(f,g,h) + 0x682e6ff3 + W[55];
249         t2 = e0(b) + Maj(b,c,d);    e+=t1;    a=t1+t2;
250
251         t1 = h + e1(e) + Ch(e,f,g) + 0x748f82ee + W[56];
252         t2 = e0(a) + Maj(a,b,c);    d+=t1;    h=t1+t2;
253         t1 = g + e1(d) + Ch(d,e,f) + 0x78a5636f + W[57];
254         t2 = e0(h) + Maj(h,a,b);    c+=t1;    g=t1+t2;
255         t1 = f + e1(c) + Ch(c,d,e) + 0x84c87814 + W[58];
256         t2 = e0(g) + Maj(g,h,a);    b+=t1;    f=t1+t2;
257         t1 = e + e1(b) + Ch(b,c,d) + 0x8cc70208 + W[59];
258         t2 = e0(f) + Maj(f,g,h);    a+=t1;    e=t1+t2;
259         t1 = d + e1(a) + Ch(a,b,c) + 0x90befffa + W[60];
260         t2 = e0(e) + Maj(e,f,g);    h+=t1;    d=t1+t2;
261         t1 = c + e1(h) + Ch(h,a,b) + 0xa4506ceb + W[61];
262         t2 = e0(d) + Maj(d,e,f);    g+=t1;    c=t1+t2;
263         t1 = b + e1(g) + Ch(g,h,a) + 0xbef9a3f7 + W[62];
264         t2 = e0(c) + Maj(c,d,e);    f+=t1;    b=t1+t2;
265         t1 = a + e1(f) + Ch(f,g,h) + 0xc67178f2 + W[63];
266         t2 = e0(b) + Maj(b,c,d);    e+=t1;    a=t1+t2;
267
268         state[0] += a; state[1] += b; state[2] += c; state[3] += d;
269         state[4] += e; state[5] += f; state[6] += g; state[7] += h;
270 }
271
272
273 static int sha224_init(struct sha256_state *sctx)
274 {
275         sctx->state[0] = SHA224_H0;
276         sctx->state[1] = SHA224_H1;
277         sctx->state[2] = SHA224_H2;
278         sctx->state[3] = SHA224_H3;
279         sctx->state[4] = SHA224_H4;
280         sctx->state[5] = SHA224_H5;
281         sctx->state[6] = SHA224_H6;
282         sctx->state[7] = SHA224_H7;
283         sctx->count = 0;
284
285         return 0;
286 }
287
288 static int sha256_init(struct sha256_state *sctx)
289 {
290         sctx->state[0] = SHA256_H0;
291         sctx->state[1] = SHA256_H1;
292         sctx->state[2] = SHA256_H2;
293         sctx->state[3] = SHA256_H3;
294         sctx->state[4] = SHA256_H4;
295         sctx->state[5] = SHA256_H5;
296         sctx->state[6] = SHA256_H6;
297         sctx->state[7] = SHA256_H7;
298         sctx->count = 0;
299
300         return 0;
301 }
302
303 static int sha256_update(struct sha256_state *sctx, const u8 *data,
304                          unsigned int len)
305 {
306         unsigned int partial, done;
307         const u8 *src;
308
309         partial = sctx->count & 0x3f;
310         sctx->count += len;
311         done = 0;
312         src = data;
313
314         if ((partial + len) > 63) {
315                 if (partial) {
316                         done = -partial;
317                         memcpy(sctx->buf + partial, data, done + 64);
318                         src = sctx->buf;
319                 }
320
321                 do {
322                         sha256_transform(sctx->state, src);
323                         done += 64;
324                         src = data + done;
325                 } while (done + 63 < len);
326
327                 partial = 0;
328         }
329         memcpy(sctx->buf + partial, src, len - done);
330
331         return 0;
332 }
333
334 static int sha256_final(struct sha256_state *sctx, u32 *dst)
335 {
336         //__be64 bits;
337         u64 bits;
338         unsigned int index, pad_len;
339         int i;
340         static const u8 padding[64] = { 0x80, };
341
342         /* Save number of bits */
343         bits = cpu_to_be64(sctx->count << 3);
344
345         /* Pad out to 56 mod 64. */
346         index = sctx->count & 0x3f;
347         pad_len = (index < 56) ? (56 - index) : ((64+56) - index);
348         sha256_update(sctx, padding, pad_len);
349
350         /* Append length (before padding) */
351         sha256_update(sctx, (const u8 *)&bits, sizeof(bits));
352
353         /* Store state in digest */
354         for (i = 0; i < 8; i++)
355                 dst[i] = cpu_to_be32(sctx->state[i]);
356
357         return 0;
358 }
359
360 static int sha224_final(struct sha256_state *sctx, u32 *hash)
361 {
362         u32 D[SHA256_DIGEST_SIZE / 4];
363
364         sha256_final(sctx, D);
365
366         memcpy(hash, D, SHA224_DIGEST_SIZE);
367
368         return 0;
369 }
370
371 class SHA256Hash : public Hash {
372 public:
373     SHA256Hash();
374     static Hash *New() { return new SHA256Hash; }
375     virtual void update(const void *data, size_t len);
376     virtual size_t digest_size() const { return SHA256_DIGEST_SIZE; }
377     virtual std::string name() const { return "sha256"; }
378
379 protected:
380     const uint8_t *finalize();
381
382 private:
383     struct sha256_state state;
384     u32 digest_buf[SHA256_DIGEST_SIZE / 4];
385 };
386
387 SHA256Hash::SHA256Hash()
388 {
389     sha256_init(&state);
390 }
391
392 void SHA256Hash::update(const void *data, size_t len)
393 {
394     sha256_update(&state, static_cast<const u8 *>(data), len);
395 }
396
397 const uint8_t *SHA256Hash::finalize()
398 {
399     sha256_final(&state, digest_buf);
400     return reinterpret_cast<uint8_t *>(digest_buf);
401 }
402
403 class SHA224Hash : public Hash {
404 public:
405     SHA224Hash();
406     static Hash *New() { return new SHA224Hash; }
407     virtual void update(const void *data, size_t len);
408     virtual size_t digest_size() const { return SHA224_DIGEST_SIZE; }
409     virtual std::string name() const { return "sha224"; }
410
411 protected:
412     const uint8_t *finalize();
413
414 private:
415     struct sha256_state state;
416     u32 digest_buf[SHA224_DIGEST_SIZE / 4];
417 };
418
419 SHA224Hash::SHA224Hash()
420 {
421     sha224_init(&state);
422 }
423
424 void SHA224Hash::update(const void *data, size_t len)
425 {
426     sha256_update(&state, static_cast<const u8 *>(data), len);
427 }
428
429 const uint8_t *SHA224Hash::finalize()
430 {
431     sha224_final(&state, digest_buf);
432     return reinterpret_cast<uint8_t *>(digest_buf);
433 }
434
435 void sha256_register()
436 {
437     Hash::Register("sha224", SHA224Hash::New);
438     Hash::Register("sha256", SHA256Hash::New);
439 }