]> git.ozlabs.org Git - ccan/blob - ccan/crypto/sha256/sha256.c
crypto/sha256: new module.
[ccan] / ccan / crypto / sha256 / sha256.c
1 /* MIT (BSD) license - see LICENSE file for details */
2 /* SHA256 core code translated from the Bitcoin project's C++:
3  *
4  * src/crypto/sha256.cpp commit 417532c8acb93c36c2b6fd052b7c11b6a2906aa2
5  * Copyright (c) 2014 The Bitcoin Core developers
6  * Distributed under the MIT software license, see the accompanying
7  * file COPYING or http://www.opensource.org/licenses/mit-license.php.
8  */
9 #include <ccan/crypto/sha256/sha256.h>
10 #include <ccan/endian/endian.h>
11 #include <stdbool.h>
12 #include <assert.h>
13 #include <string.h>
14
15 static void invalidate_sha256(struct sha256_ctx *ctx)
16 {
17 #ifdef CCAN_CRYPTO_SHA256_USE_OPENSSL
18         ctx->c.md_len = 0;
19 #else
20         ctx->bytes = -1ULL;
21 #endif
22 }
23
24 static void check_sha256(struct sha256_ctx *ctx)
25 {
26 #ifdef CCAN_CRYPTO_SHA256_USE_OPENSSL
27         assert(ctx->c.md_len != 0);
28 #else
29         assert(ctx->bytes != -1ULL);
30 #endif
31 }
32
33 #ifdef CCAN_CRYPTO_SHA256_USE_OPENSSL
34 void sha256_init(struct sha256_ctx *ctx)
35 {
36         SHA256_Init(&ctx->c);
37 }
38
39 void sha256_update_arr(struct sha256_ctx *ctx, const void *p,
40                        size_t num, size_t size)
41 {
42         size_t total = num * size;
43
44         /* Don't overflow. */
45         assert(size == 0 || total / size == num);
46         check_sha256(ctx);
47         SHA256_Update(&ctx->c, p, total);
48 }
49
50 void sha256_done(struct sha256_ctx *ctx, struct sha256 *res)
51 {
52         SHA256_Final(res->u.u8, &ctx->c);
53         invalidate_sha256(ctx);
54 }
55 #else
56 static uint32_t Ch(uint32_t x, uint32_t y, uint32_t z)
57 {
58         return z ^ (x & (y ^ z));
59 }
60 static uint32_t Maj(uint32_t x, uint32_t y, uint32_t z)
61 {
62         return (x & y) | (z & (x | y));
63 }
64 static uint32_t Sigma0(uint32_t x)
65 {
66         return (x >> 2 | x << 30) ^ (x >> 13 | x << 19) ^ (x >> 22 | x << 10);
67 }
68 static uint32_t Sigma1(uint32_t x)
69 {
70         return (x >> 6 | x << 26) ^ (x >> 11 | x << 21) ^ (x >> 25 | x << 7);
71 }
72 static uint32_t sigma0(uint32_t x)
73 {
74         return (x >> 7 | x << 25) ^ (x >> 18 | x << 14) ^ (x >> 3);
75 }
76 static uint32_t sigma1(uint32_t x)
77 {
78         return (x >> 17 | x << 15) ^ (x >> 19 | x << 13) ^ (x >> 10);
79 }
80
81 /** One round of SHA-256. */
82 static void Round(uint32_t a, uint32_t b, uint32_t c, uint32_t *d, uint32_t e, uint32_t f, uint32_t g, uint32_t *h, uint32_t k, uint32_t w)
83 {
84         uint32_t t1 = *h + Sigma1(e) + Ch(e, f, g) + k + w;
85         uint32_t t2 = Sigma0(a) + Maj(a, b, c);
86         *d += t1;
87         *h = t1 + t2;
88 }
89
90 /** Perform one SHA-256 transformation, processing a 64-byte chunk. */
91 static void Transform(uint32_t *s, const uint32_t *chunk)
92 {
93         uint32_t a = s[0], b = s[1], c = s[2], d = s[3], e = s[4], f = s[5], g = s[6], h = s[7];
94         uint32_t w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13, w14, w15;
95
96         Round(a, b, c, &d, e, f, g, &h, 0x428a2f98, w0 = be32_to_cpu(chunk[0]));
97         Round(h, a, b, &c, d, e, f, &g, 0x71374491, w1 = be32_to_cpu(chunk[1]));
98         Round(g, h, a, &b, c, d, e, &f, 0xb5c0fbcf, w2 = be32_to_cpu(chunk[2]));
99         Round(f, g, h, &a, b, c, d, &e, 0xe9b5dba5, w3 = be32_to_cpu(chunk[3]));
100         Round(e, f, g, &h, a, b, c, &d, 0x3956c25b, w4 = be32_to_cpu(chunk[4]));
101         Round(d, e, f, &g, h, a, b, &c, 0x59f111f1, w5 = be32_to_cpu(chunk[5]));
102         Round(c, d, e, &f, g, h, a, &b, 0x923f82a4, w6 = be32_to_cpu(chunk[6]));
103         Round(b, c, d, &e, f, g, h, &a, 0xab1c5ed5, w7 = be32_to_cpu(chunk[7]));
104         Round(a, b, c, &d, e, f, g, &h, 0xd807aa98, w8 = be32_to_cpu(chunk[8]));
105         Round(h, a, b, &c, d, e, f, &g, 0x12835b01, w9 = be32_to_cpu(chunk[9]));
106         Round(g, h, a, &b, c, d, e, &f, 0x243185be, w10 = be32_to_cpu(chunk[10]));
107         Round(f, g, h, &a, b, c, d, &e, 0x550c7dc3, w11 = be32_to_cpu(chunk[11]));
108         Round(e, f, g, &h, a, b, c, &d, 0x72be5d74, w12 = be32_to_cpu(chunk[12]));
109         Round(d, e, f, &g, h, a, b, &c, 0x80deb1fe, w13 = be32_to_cpu(chunk[13]));
110         Round(c, d, e, &f, g, h, a, &b, 0x9bdc06a7, w14 = be32_to_cpu(chunk[14]));
111         Round(b, c, d, &e, f, g, h, &a, 0xc19bf174, w15 = be32_to_cpu(chunk[15]));
112
113         Round(a, b, c, &d, e, f, g, &h, 0xe49b69c1, w0 += sigma1(w14) + w9 + sigma0(w1));
114         Round(h, a, b, &c, d, e, f, &g, 0xefbe4786, w1 += sigma1(w15) + w10 + sigma0(w2));
115         Round(g, h, a, &b, c, d, e, &f, 0x0fc19dc6, w2 += sigma1(w0) + w11 + sigma0(w3));
116         Round(f, g, h, &a, b, c, d, &e, 0x240ca1cc, w3 += sigma1(w1) + w12 + sigma0(w4));
117         Round(e, f, g, &h, a, b, c, &d, 0x2de92c6f, w4 += sigma1(w2) + w13 + sigma0(w5));
118         Round(d, e, f, &g, h, a, b, &c, 0x4a7484aa, w5 += sigma1(w3) + w14 + sigma0(w6));
119         Round(c, d, e, &f, g, h, a, &b, 0x5cb0a9dc, w6 += sigma1(w4) + w15 + sigma0(w7));
120         Round(b, c, d, &e, f, g, h, &a, 0x76f988da, w7 += sigma1(w5) + w0 + sigma0(w8));
121         Round(a, b, c, &d, e, f, g, &h, 0x983e5152, w8 += sigma1(w6) + w1 + sigma0(w9));
122         Round(h, a, b, &c, d, e, f, &g, 0xa831c66d, w9 += sigma1(w7) + w2 + sigma0(w10));
123         Round(g, h, a, &b, c, d, e, &f, 0xb00327c8, w10 += sigma1(w8) + w3 + sigma0(w11));
124         Round(f, g, h, &a, b, c, d, &e, 0xbf597fc7, w11 += sigma1(w9) + w4 + sigma0(w12));
125         Round(e, f, g, &h, a, b, c, &d, 0xc6e00bf3, w12 += sigma1(w10) + w5 + sigma0(w13));
126         Round(d, e, f, &g, h, a, b, &c, 0xd5a79147, w13 += sigma1(w11) + w6 + sigma0(w14));
127         Round(c, d, e, &f, g, h, a, &b, 0x06ca6351, w14 += sigma1(w12) + w7 + sigma0(w15));
128         Round(b, c, d, &e, f, g, h, &a, 0x14292967, w15 += sigma1(w13) + w8 + sigma0(w0));
129
130         Round(a, b, c, &d, e, f, g, &h, 0x27b70a85, w0 += sigma1(w14) + w9 + sigma0(w1));
131         Round(h, a, b, &c, d, e, f, &g, 0x2e1b2138, w1 += sigma1(w15) + w10 + sigma0(w2));
132         Round(g, h, a, &b, c, d, e, &f, 0x4d2c6dfc, w2 += sigma1(w0) + w11 + sigma0(w3));
133         Round(f, g, h, &a, b, c, d, &e, 0x53380d13, w3 += sigma1(w1) + w12 + sigma0(w4));
134         Round(e, f, g, &h, a, b, c, &d, 0x650a7354, w4 += sigma1(w2) + w13 + sigma0(w5));
135         Round(d, e, f, &g, h, a, b, &c, 0x766a0abb, w5 += sigma1(w3) + w14 + sigma0(w6));
136         Round(c, d, e, &f, g, h, a, &b, 0x81c2c92e, w6 += sigma1(w4) + w15 + sigma0(w7));
137         Round(b, c, d, &e, f, g, h, &a, 0x92722c85, w7 += sigma1(w5) + w0 + sigma0(w8));
138         Round(a, b, c, &d, e, f, g, &h, 0xa2bfe8a1, w8 += sigma1(w6) + w1 + sigma0(w9));
139         Round(h, a, b, &c, d, e, f, &g, 0xa81a664b, w9 += sigma1(w7) + w2 + sigma0(w10));
140         Round(g, h, a, &b, c, d, e, &f, 0xc24b8b70, w10 += sigma1(w8) + w3 + sigma0(w11));
141         Round(f, g, h, &a, b, c, d, &e, 0xc76c51a3, w11 += sigma1(w9) + w4 + sigma0(w12));
142         Round(e, f, g, &h, a, b, c, &d, 0xd192e819, w12 += sigma1(w10) + w5 + sigma0(w13));
143         Round(d, e, f, &g, h, a, b, &c, 0xd6990624, w13 += sigma1(w11) + w6 + sigma0(w14));
144         Round(c, d, e, &f, g, h, a, &b, 0xf40e3585, w14 += sigma1(w12) + w7 + sigma0(w15));
145         Round(b, c, d, &e, f, g, h, &a, 0x106aa070, w15 += sigma1(w13) + w8 + sigma0(w0));
146
147         Round(a, b, c, &d, e, f, g, &h, 0x19a4c116, w0 += sigma1(w14) + w9 + sigma0(w1));
148         Round(h, a, b, &c, d, e, f, &g, 0x1e376c08, w1 += sigma1(w15) + w10 + sigma0(w2));
149         Round(g, h, a, &b, c, d, e, &f, 0x2748774c, w2 += sigma1(w0) + w11 + sigma0(w3));
150         Round(f, g, h, &a, b, c, d, &e, 0x34b0bcb5, w3 += sigma1(w1) + w12 + sigma0(w4));
151         Round(e, f, g, &h, a, b, c, &d, 0x391c0cb3, w4 += sigma1(w2) + w13 + sigma0(w5));
152         Round(d, e, f, &g, h, a, b, &c, 0x4ed8aa4a, w5 += sigma1(w3) + w14 + sigma0(w6));
153         Round(c, d, e, &f, g, h, a, &b, 0x5b9cca4f, w6 += sigma1(w4) + w15 + sigma0(w7));
154         Round(b, c, d, &e, f, g, h, &a, 0x682e6ff3, w7 += sigma1(w5) + w0 + sigma0(w8));
155         Round(a, b, c, &d, e, f, g, &h, 0x748f82ee, w8 += sigma1(w6) + w1 + sigma0(w9));
156         Round(h, a, b, &c, d, e, f, &g, 0x78a5636f, w9 += sigma1(w7) + w2 + sigma0(w10));
157         Round(g, h, a, &b, c, d, e, &f, 0x84c87814, w10 += sigma1(w8) + w3 + sigma0(w11));
158         Round(f, g, h, &a, b, c, d, &e, 0x8cc70208, w11 += sigma1(w9) + w4 + sigma0(w12));
159         Round(e, f, g, &h, a, b, c, &d, 0x90befffa, w12 += sigma1(w10) + w5 + sigma0(w13));
160         Round(d, e, f, &g, h, a, b, &c, 0xa4506ceb, w13 += sigma1(w11) + w6 + sigma0(w14));
161         Round(c, d, e, &f, g, h, a, &b, 0xbef9a3f7, w14 + sigma1(w12) + w7 + sigma0(w15));
162         Round(b, c, d, &e, f, g, h, &a, 0xc67178f2, w15 + sigma1(w13) + w8 + sigma0(w0));
163
164         s[0] += a;
165         s[1] += b;
166         s[2] += c;
167         s[3] += d;
168         s[4] += e;
169         s[5] += f;
170         s[6] += g;
171         s[7] += h;
172 }
173
174 static bool alignment_ok(const void *p, size_t n)
175 {
176 #if HAVE_UNALIGNED_ACCESS
177         return true;
178 #else
179         return ((size_t)p % n == 0);
180 #endif
181 }
182
183 static void add(struct sha256_ctx *ctx, const void *p, size_t len)
184 {
185         const unsigned char *data = p;
186         size_t bufsize = ctx->bytes % 64;
187
188         if (bufsize + len >= 64) {
189                 // Fill the buffer, and process it.
190                 memcpy(ctx->buf.u8 + bufsize, data, 64 - bufsize);
191                 ctx->bytes += 64 - bufsize;
192                 data += 64 - bufsize;
193                 len -= 64 - bufsize;
194                 Transform(ctx->s, ctx->buf.u32);
195                 bufsize = 0;
196         }
197
198         while (len >= 64) {
199                 // Process full chunks directly from the source.
200                 if (alignment_ok(data, sizeof(uint32_t)))
201                         Transform(ctx->s, (const uint32_t *)data);
202                 else {
203                         memcpy(ctx->buf.u8, data, sizeof(ctx->buf));
204                         Transform(ctx->s, ctx->buf.u32);
205                 }
206                 ctx->bytes += 64;
207                 data += 64;
208                 len -= 64;
209         }
210             
211         if (len) {
212                 // Fill the buffer with what remains.
213                 memcpy(ctx->buf.u8 + bufsize, data, len);
214                 ctx->bytes += len;
215         }
216 }
217
218 void sha256_init(struct sha256_ctx *ctx)
219 {
220         struct sha256_ctx init = SHA256_INIT;
221         *ctx = init;
222 }
223
224 void sha256_update_arr(struct sha256_ctx *ctx, const void *p,
225                        size_t num, size_t size)
226 {
227         size_t total = num * size;
228
229         /* Don't overflow. */
230         assert(size == 0 || total / size == num);
231         check_sha256(ctx);
232         add(ctx, p, total);
233 }
234
235 void sha256_done(struct sha256_ctx *ctx, struct sha256 *res)
236 {
237         static const unsigned char pad[64] = {0x80};
238         uint64_t sizedesc;
239         size_t i;
240
241         sizedesc = cpu_to_be64(ctx->bytes << 3);
242         /* Add '1' bit to terminate, then all 0 bits, up to next block - 8. */
243         add(ctx, pad, 1 + ((119 - (ctx->bytes % 64)) % 64));
244         /* Add number of bits of data (big endian) */
245         add(ctx, &sizedesc, 8);
246         for (i = 0; i < sizeof(ctx->s) / sizeof(ctx->s[0]); i++)
247                 res->u.u32[i] = cpu_to_be32(ctx->s[i]);
248         invalidate_sha256(ctx);
249 }
250 #endif
251
252 void sha256_arr(struct sha256 *sha, const void *p, size_t num, size_t size)
253 {
254         struct sha256_ctx ctx;
255
256         sha256_init(&ctx);
257         sha256_update_arr(&ctx, p, num, size);
258         sha256_done(&ctx, sha);
259 }
260         
261 void sha256_u8(struct sha256_ctx *ctx, uint8_t v)
262 {
263         sha256_update_arr(ctx, &v, sizeof(v), 1);
264 }
265
266 void sha256_u16(struct sha256_ctx *ctx, uint16_t v)
267 {
268         sha256_update_arr(ctx, &v, sizeof(v), 1);
269 }
270
271 void sha256_u32(struct sha256_ctx *ctx, uint32_t v)
272 {
273         sha256_update_arr(ctx, &v, sizeof(v), 1);
274 }
275
276 void sha256_u64(struct sha256_ctx *ctx, uint64_t v)
277 {
278         sha256_update_arr(ctx, &v, sizeof(v), 1);
279 }
280
281 /* Add as little-endian */
282 void sha256_le16(struct sha256_ctx *ctx, uint16_t v)
283 {
284         leint16_t lev = cpu_to_le16(v);
285         sha256_update_arr(ctx, &lev, sizeof(lev), 1);
286 }
287         
288 void sha256_le32(struct sha256_ctx *ctx, uint32_t v)
289 {
290         leint32_t lev = cpu_to_le32(v);
291         sha256_update_arr(ctx, &lev, sizeof(lev), 1);
292 }
293         
294 void sha256_le64(struct sha256_ctx *ctx, uint64_t v)
295 {
296         leint64_t lev = cpu_to_le64(v);
297         sha256_update_arr(ctx, &lev, sizeof(lev), 1);
298 }
299
300 /* Add as big-endian */
301 void sha256_be16(struct sha256_ctx *ctx, uint16_t v)
302 {
303         beint16_t bev = cpu_to_be16(v);
304         sha256_update_arr(ctx, &bev, sizeof(bev), 1);
305 }
306         
307 void sha256_be32(struct sha256_ctx *ctx, uint32_t v)
308 {
309         beint32_t bev = cpu_to_be32(v);
310         sha256_update_arr(ctx, &bev, sizeof(bev), 1);
311 }
312         
313 void sha256_be64(struct sha256_ctx *ctx, uint64_t v)
314 {
315         beint64_t bev = cpu_to_be64(v);
316         sha256_update_arr(ctx, &bev, sizeof(bev), 1);
317 }