1 /* sha256.cc - Message digests for Cumulus using SHA256
3 * This code is adapted from crypto/sha256_generic.c and include/crypto/sha.n
4 * from the Linux kernel version 3.4.
7 /* SHA-256, as specified in
8 * http://csrc.nist.gov/groups/STM/cavp/documents/shs/sha256-384-512.pdf
10 * SHA-256 code by Jean-Luc Cooke <jlcooke@certainkey.com>.
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>
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)
33 /* Adaptation code for a non-kernel build environment. */
38 static inline u32 ror32(u32 x, int n)
40 return (x >> n) | (x << (32 - n));
43 static inline u32 cpu_to_be32(u32 x)
48 static inline u64 cpu_to_be64(u64 x)
53 /* Originally from sha.h. */
55 #define SHA224_DIGEST_SIZE 28
56 #define SHA224_BLOCK_SIZE 64
58 #define SHA256_DIGEST_SIZE 32
59 #define SHA256_BLOCK_SIZE 64
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
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
81 u32 state[SHA256_DIGEST_SIZE / 4];
82 u8 buf[SHA256_BLOCK_SIZE];
85 /* Originally from sha256_generic.c */
87 static inline u32 Ch(u32 x, u32 y, u32 z)
89 return z ^ (x & (y ^ z));
92 static inline u32 Maj(u32 x, u32 y, u32 z)
94 return (x & y) | (z & (x | y));
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))
102 static inline void LOAD_OP(int I, u32 *W, const u8 *input)
104 W[I] = (input[4*I] << 24) | (input[4*I+1] << 16)
105 | (input[4*I+2] << 8) | input[4*I+3];
108 static inline void BLEND_OP(int I, u32 *W)
110 W[I] = s1(W[I-2]) + W[I-7] + s0(W[I-15]) + W[I-16];
113 static void sha256_transform(u32 *state, const u8 *input)
115 u32 a, b, c, d, e, f, g, h, t1, t2;
120 for (i = 0; i < 16; i++)
121 LOAD_OP(i, W, input);
124 for (i = 16; i < 64; i++)
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];
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;
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;
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;
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;
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;
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;
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;
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;
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;
273 static int sha224_init(struct sha256_state *sctx)
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;
288 static int sha256_init(struct sha256_state *sctx)
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;
303 static int sha256_update(struct sha256_state *sctx, const u8 *data,
306 unsigned int partial, done;
309 partial = sctx->count & 0x3f;
314 if ((partial + len) > 63) {
317 memcpy(sctx->buf + partial, data, done + 64);
322 sha256_transform(sctx->state, src);
325 } while (done + 63 < len);
329 memcpy(sctx->buf + partial, src, len - done);
334 static int sha256_final(struct sha256_state *sctx, u32 *dst)
338 unsigned int index, pad_len;
340 static const u8 padding[64] = { 0x80, };
342 /* Save number of bits */
343 bits = cpu_to_be64(sctx->count << 3);
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);
350 /* Append length (before padding) */
351 sha256_update(sctx, (const u8 *)&bits, sizeof(bits));
353 /* Store state in digest */
354 for (i = 0; i < 8; i++)
355 dst[i] = cpu_to_be32(sctx->state[i]);
360 static int sha224_final(struct sha256_state *sctx, u32 *hash)
362 u32 D[SHA256_DIGEST_SIZE / 4];
364 sha256_final(sctx, D);
366 memcpy(hash, D, SHA224_DIGEST_SIZE);
371 class SHA256Hash : public Hash {
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"; }
380 const uint8_t *finalize();
383 struct sha256_state state;
384 u32 digest_buf[SHA256_DIGEST_SIZE / 4];
387 SHA256Hash::SHA256Hash()
392 void SHA256Hash::update(const void *data, size_t len)
394 sha256_update(&state, static_cast<const u8 *>(data), len);
397 const uint8_t *SHA256Hash::finalize()
399 sha256_final(&state, digest_buf);
400 return reinterpret_cast<uint8_t *>(digest_buf);
403 class SHA224Hash : public Hash {
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"; }
412 const uint8_t *finalize();
415 struct sha256_state state;
416 u32 digest_buf[SHA224_DIGEST_SIZE / 4];
419 SHA224Hash::SHA224Hash()
424 void SHA224Hash::update(const void *data, size_t len)
426 sha256_update(&state, static_cast<const u8 *>(data), len);
429 const uint8_t *SHA224Hash::finalize()
431 sha224_final(&state, digest_buf);
432 return reinterpret_cast<uint8_t *>(digest_buf);
435 void sha256_register()
437 Hash::Register("sha224", SHA224Hash::New);
438 Hash::Register("sha256", SHA256Hash::New);