1 /* SHA-256, as specified in
2 * http://csrc.nist.gov/groups/STM/cavp/documents/shs/sha256-384-512.pdf
3 * part of Cumulus: Efficient Filesystem Backup to the Cloud
5 * Copyright (c) Jean-Luc Cooke <jlcooke@certainkey.com>
6 * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
7 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
8 * SHA224 Support Copyright 2007 Intel Corporation <jonathan.lynch@intel.com>
9 * Copyright (C) 2012 The Cumulus Developers
10 * See the AUTHORS file for a list of Cumulus contributors.
12 * This code is adapted from crypto/sha256_generic.c and include/crypto/sha.h
13 * from the Linux kernel version 3.4, and reworked to fit within Cumulus.
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
39 /* Adaptation code for a non-kernel build environment. */
44 static inline u32 ror32(u32 x, int n)
46 return (x >> n) | (x << (32 - n));
49 static inline u32 cpu_to_be32(u32 x)
54 static inline u64 cpu_to_be64(u64 x)
59 /* Originally from sha.h. */
61 #define SHA224_DIGEST_SIZE 28
62 #define SHA224_BLOCK_SIZE 64
64 #define SHA256_DIGEST_SIZE 32
65 #define SHA256_BLOCK_SIZE 64
67 #define SHA224_H0 0xc1059ed8UL
68 #define SHA224_H1 0x367cd507UL
69 #define SHA224_H2 0x3070dd17UL
70 #define SHA224_H3 0xf70e5939UL
71 #define SHA224_H4 0xffc00b31UL
72 #define SHA224_H5 0x68581511UL
73 #define SHA224_H6 0x64f98fa7UL
74 #define SHA224_H7 0xbefa4fa4UL
76 #define SHA256_H0 0x6a09e667UL
77 #define SHA256_H1 0xbb67ae85UL
78 #define SHA256_H2 0x3c6ef372UL
79 #define SHA256_H3 0xa54ff53aUL
80 #define SHA256_H4 0x510e527fUL
81 #define SHA256_H5 0x9b05688cUL
82 #define SHA256_H6 0x1f83d9abUL
83 #define SHA256_H7 0x5be0cd19UL
87 u32 state[SHA256_DIGEST_SIZE / 4];
88 u8 buf[SHA256_BLOCK_SIZE];
91 /* Originally from sha256_generic.c */
93 static inline u32 Ch(u32 x, u32 y, u32 z)
95 return z ^ (x & (y ^ z));
98 static inline u32 Maj(u32 x, u32 y, u32 z)
100 return (x & y) | (z & (x | y));
103 #define e0(x) (ror32(x, 2) ^ ror32(x,13) ^ ror32(x,22))
104 #define e1(x) (ror32(x, 6) ^ ror32(x,11) ^ ror32(x,25))
105 #define s0(x) (ror32(x, 7) ^ ror32(x,18) ^ (x >> 3))
106 #define s1(x) (ror32(x,17) ^ ror32(x,19) ^ (x >> 10))
108 static inline void LOAD_OP(int I, u32 *W, const u8 *input)
110 W[I] = (input[4*I] << 24) | (input[4*I+1] << 16)
111 | (input[4*I+2] << 8) | input[4*I+3];
114 static inline void BLEND_OP(int I, u32 *W)
116 W[I] = s1(W[I-2]) + W[I-7] + s0(W[I-15]) + W[I-16];
119 static void sha256_transform(u32 *state, const u8 *input)
121 u32 a, b, c, d, e, f, g, h, t1, t2;
126 for (i = 0; i < 16; i++)
127 LOAD_OP(i, W, input);
130 for (i = 16; i < 64; i++)
133 /* load the state into our registers */
134 a=state[0]; b=state[1]; c=state[2]; d=state[3];
135 e=state[4]; f=state[5]; g=state[6]; h=state[7];
138 t1 = h + e1(e) + Ch(e,f,g) + 0x428a2f98 + W[ 0];
139 t2 = e0(a) + Maj(a,b,c); d+=t1; h=t1+t2;
140 t1 = g + e1(d) + Ch(d,e,f) + 0x71374491 + W[ 1];
141 t2 = e0(h) + Maj(h,a,b); c+=t1; g=t1+t2;
142 t1 = f + e1(c) + Ch(c,d,e) + 0xb5c0fbcf + W[ 2];
143 t2 = e0(g) + Maj(g,h,a); b+=t1; f=t1+t2;
144 t1 = e + e1(b) + Ch(b,c,d) + 0xe9b5dba5 + W[ 3];
145 t2 = e0(f) + Maj(f,g,h); a+=t1; e=t1+t2;
146 t1 = d + e1(a) + Ch(a,b,c) + 0x3956c25b + W[ 4];
147 t2 = e0(e) + Maj(e,f,g); h+=t1; d=t1+t2;
148 t1 = c + e1(h) + Ch(h,a,b) + 0x59f111f1 + W[ 5];
149 t2 = e0(d) + Maj(d,e,f); g+=t1; c=t1+t2;
150 t1 = b + e1(g) + Ch(g,h,a) + 0x923f82a4 + W[ 6];
151 t2 = e0(c) + Maj(c,d,e); f+=t1; b=t1+t2;
152 t1 = a + e1(f) + Ch(f,g,h) + 0xab1c5ed5 + W[ 7];
153 t2 = e0(b) + Maj(b,c,d); e+=t1; a=t1+t2;
155 t1 = h + e1(e) + Ch(e,f,g) + 0xd807aa98 + W[ 8];
156 t2 = e0(a) + Maj(a,b,c); d+=t1; h=t1+t2;
157 t1 = g + e1(d) + Ch(d,e,f) + 0x12835b01 + W[ 9];
158 t2 = e0(h) + Maj(h,a,b); c+=t1; g=t1+t2;
159 t1 = f + e1(c) + Ch(c,d,e) + 0x243185be + W[10];
160 t2 = e0(g) + Maj(g,h,a); b+=t1; f=t1+t2;
161 t1 = e + e1(b) + Ch(b,c,d) + 0x550c7dc3 + W[11];
162 t2 = e0(f) + Maj(f,g,h); a+=t1; e=t1+t2;
163 t1 = d + e1(a) + Ch(a,b,c) + 0x72be5d74 + W[12];
164 t2 = e0(e) + Maj(e,f,g); h+=t1; d=t1+t2;
165 t1 = c + e1(h) + Ch(h,a,b) + 0x80deb1fe + W[13];
166 t2 = e0(d) + Maj(d,e,f); g+=t1; c=t1+t2;
167 t1 = b + e1(g) + Ch(g,h,a) + 0x9bdc06a7 + W[14];
168 t2 = e0(c) + Maj(c,d,e); f+=t1; b=t1+t2;
169 t1 = a + e1(f) + Ch(f,g,h) + 0xc19bf174 + W[15];
170 t2 = e0(b) + Maj(b,c,d); e+=t1; a=t1+t2;
172 t1 = h + e1(e) + Ch(e,f,g) + 0xe49b69c1 + W[16];
173 t2 = e0(a) + Maj(a,b,c); d+=t1; h=t1+t2;
174 t1 = g + e1(d) + Ch(d,e,f) + 0xefbe4786 + W[17];
175 t2 = e0(h) + Maj(h,a,b); c+=t1; g=t1+t2;
176 t1 = f + e1(c) + Ch(c,d,e) + 0x0fc19dc6 + W[18];
177 t2 = e0(g) + Maj(g,h,a); b+=t1; f=t1+t2;
178 t1 = e + e1(b) + Ch(b,c,d) + 0x240ca1cc + W[19];
179 t2 = e0(f) + Maj(f,g,h); a+=t1; e=t1+t2;
180 t1 = d + e1(a) + Ch(a,b,c) + 0x2de92c6f + W[20];
181 t2 = e0(e) + Maj(e,f,g); h+=t1; d=t1+t2;
182 t1 = c + e1(h) + Ch(h,a,b) + 0x4a7484aa + W[21];
183 t2 = e0(d) + Maj(d,e,f); g+=t1; c=t1+t2;
184 t1 = b + e1(g) + Ch(g,h,a) + 0x5cb0a9dc + W[22];
185 t2 = e0(c) + Maj(c,d,e); f+=t1; b=t1+t2;
186 t1 = a + e1(f) + Ch(f,g,h) + 0x76f988da + W[23];
187 t2 = e0(b) + Maj(b,c,d); e+=t1; a=t1+t2;
189 t1 = h + e1(e) + Ch(e,f,g) + 0x983e5152 + W[24];
190 t2 = e0(a) + Maj(a,b,c); d+=t1; h=t1+t2;
191 t1 = g + e1(d) + Ch(d,e,f) + 0xa831c66d + W[25];
192 t2 = e0(h) + Maj(h,a,b); c+=t1; g=t1+t2;
193 t1 = f + e1(c) + Ch(c,d,e) + 0xb00327c8 + W[26];
194 t2 = e0(g) + Maj(g,h,a); b+=t1; f=t1+t2;
195 t1 = e + e1(b) + Ch(b,c,d) + 0xbf597fc7 + W[27];
196 t2 = e0(f) + Maj(f,g,h); a+=t1; e=t1+t2;
197 t1 = d + e1(a) + Ch(a,b,c) + 0xc6e00bf3 + W[28];
198 t2 = e0(e) + Maj(e,f,g); h+=t1; d=t1+t2;
199 t1 = c + e1(h) + Ch(h,a,b) + 0xd5a79147 + W[29];
200 t2 = e0(d) + Maj(d,e,f); g+=t1; c=t1+t2;
201 t1 = b + e1(g) + Ch(g,h,a) + 0x06ca6351 + W[30];
202 t2 = e0(c) + Maj(c,d,e); f+=t1; b=t1+t2;
203 t1 = a + e1(f) + Ch(f,g,h) + 0x14292967 + W[31];
204 t2 = e0(b) + Maj(b,c,d); e+=t1; a=t1+t2;
206 t1 = h + e1(e) + Ch(e,f,g) + 0x27b70a85 + W[32];
207 t2 = e0(a) + Maj(a,b,c); d+=t1; h=t1+t2;
208 t1 = g + e1(d) + Ch(d,e,f) + 0x2e1b2138 + W[33];
209 t2 = e0(h) + Maj(h,a,b); c+=t1; g=t1+t2;
210 t1 = f + e1(c) + Ch(c,d,e) + 0x4d2c6dfc + W[34];
211 t2 = e0(g) + Maj(g,h,a); b+=t1; f=t1+t2;
212 t1 = e + e1(b) + Ch(b,c,d) + 0x53380d13 + W[35];
213 t2 = e0(f) + Maj(f,g,h); a+=t1; e=t1+t2;
214 t1 = d + e1(a) + Ch(a,b,c) + 0x650a7354 + W[36];
215 t2 = e0(e) + Maj(e,f,g); h+=t1; d=t1+t2;
216 t1 = c + e1(h) + Ch(h,a,b) + 0x766a0abb + W[37];
217 t2 = e0(d) + Maj(d,e,f); g+=t1; c=t1+t2;
218 t1 = b + e1(g) + Ch(g,h,a) + 0x81c2c92e + W[38];
219 t2 = e0(c) + Maj(c,d,e); f+=t1; b=t1+t2;
220 t1 = a + e1(f) + Ch(f,g,h) + 0x92722c85 + W[39];
221 t2 = e0(b) + Maj(b,c,d); e+=t1; a=t1+t2;
223 t1 = h + e1(e) + Ch(e,f,g) + 0xa2bfe8a1 + W[40];
224 t2 = e0(a) + Maj(a,b,c); d+=t1; h=t1+t2;
225 t1 = g + e1(d) + Ch(d,e,f) + 0xa81a664b + W[41];
226 t2 = e0(h) + Maj(h,a,b); c+=t1; g=t1+t2;
227 t1 = f + e1(c) + Ch(c,d,e) + 0xc24b8b70 + W[42];
228 t2 = e0(g) + Maj(g,h,a); b+=t1; f=t1+t2;
229 t1 = e + e1(b) + Ch(b,c,d) + 0xc76c51a3 + W[43];
230 t2 = e0(f) + Maj(f,g,h); a+=t1; e=t1+t2;
231 t1 = d + e1(a) + Ch(a,b,c) + 0xd192e819 + W[44];
232 t2 = e0(e) + Maj(e,f,g); h+=t1; d=t1+t2;
233 t1 = c + e1(h) + Ch(h,a,b) + 0xd6990624 + W[45];
234 t2 = e0(d) + Maj(d,e,f); g+=t1; c=t1+t2;
235 t1 = b + e1(g) + Ch(g,h,a) + 0xf40e3585 + W[46];
236 t2 = e0(c) + Maj(c,d,e); f+=t1; b=t1+t2;
237 t1 = a + e1(f) + Ch(f,g,h) + 0x106aa070 + W[47];
238 t2 = e0(b) + Maj(b,c,d); e+=t1; a=t1+t2;
240 t1 = h + e1(e) + Ch(e,f,g) + 0x19a4c116 + W[48];
241 t2 = e0(a) + Maj(a,b,c); d+=t1; h=t1+t2;
242 t1 = g + e1(d) + Ch(d,e,f) + 0x1e376c08 + W[49];
243 t2 = e0(h) + Maj(h,a,b); c+=t1; g=t1+t2;
244 t1 = f + e1(c) + Ch(c,d,e) + 0x2748774c + W[50];
245 t2 = e0(g) + Maj(g,h,a); b+=t1; f=t1+t2;
246 t1 = e + e1(b) + Ch(b,c,d) + 0x34b0bcb5 + W[51];
247 t2 = e0(f) + Maj(f,g,h); a+=t1; e=t1+t2;
248 t1 = d + e1(a) + Ch(a,b,c) + 0x391c0cb3 + W[52];
249 t2 = e0(e) + Maj(e,f,g); h+=t1; d=t1+t2;
250 t1 = c + e1(h) + Ch(h,a,b) + 0x4ed8aa4a + W[53];
251 t2 = e0(d) + Maj(d,e,f); g+=t1; c=t1+t2;
252 t1 = b + e1(g) + Ch(g,h,a) + 0x5b9cca4f + W[54];
253 t2 = e0(c) + Maj(c,d,e); f+=t1; b=t1+t2;
254 t1 = a + e1(f) + Ch(f,g,h) + 0x682e6ff3 + W[55];
255 t2 = e0(b) + Maj(b,c,d); e+=t1; a=t1+t2;
257 t1 = h + e1(e) + Ch(e,f,g) + 0x748f82ee + W[56];
258 t2 = e0(a) + Maj(a,b,c); d+=t1; h=t1+t2;
259 t1 = g + e1(d) + Ch(d,e,f) + 0x78a5636f + W[57];
260 t2 = e0(h) + Maj(h,a,b); c+=t1; g=t1+t2;
261 t1 = f + e1(c) + Ch(c,d,e) + 0x84c87814 + W[58];
262 t2 = e0(g) + Maj(g,h,a); b+=t1; f=t1+t2;
263 t1 = e + e1(b) + Ch(b,c,d) + 0x8cc70208 + W[59];
264 t2 = e0(f) + Maj(f,g,h); a+=t1; e=t1+t2;
265 t1 = d + e1(a) + Ch(a,b,c) + 0x90befffa + W[60];
266 t2 = e0(e) + Maj(e,f,g); h+=t1; d=t1+t2;
267 t1 = c + e1(h) + Ch(h,a,b) + 0xa4506ceb + W[61];
268 t2 = e0(d) + Maj(d,e,f); g+=t1; c=t1+t2;
269 t1 = b + e1(g) + Ch(g,h,a) + 0xbef9a3f7 + W[62];
270 t2 = e0(c) + Maj(c,d,e); f+=t1; b=t1+t2;
271 t1 = a + e1(f) + Ch(f,g,h) + 0xc67178f2 + W[63];
272 t2 = e0(b) + Maj(b,c,d); e+=t1; a=t1+t2;
274 state[0] += a; state[1] += b; state[2] += c; state[3] += d;
275 state[4] += e; state[5] += f; state[6] += g; state[7] += h;
279 static int sha224_init(struct sha256_state *sctx)
281 sctx->state[0] = SHA224_H0;
282 sctx->state[1] = SHA224_H1;
283 sctx->state[2] = SHA224_H2;
284 sctx->state[3] = SHA224_H3;
285 sctx->state[4] = SHA224_H4;
286 sctx->state[5] = SHA224_H5;
287 sctx->state[6] = SHA224_H6;
288 sctx->state[7] = SHA224_H7;
294 static int sha256_init(struct sha256_state *sctx)
296 sctx->state[0] = SHA256_H0;
297 sctx->state[1] = SHA256_H1;
298 sctx->state[2] = SHA256_H2;
299 sctx->state[3] = SHA256_H3;
300 sctx->state[4] = SHA256_H4;
301 sctx->state[5] = SHA256_H5;
302 sctx->state[6] = SHA256_H6;
303 sctx->state[7] = SHA256_H7;
309 static int sha256_update(struct sha256_state *sctx, const u8 *data,
312 unsigned int partial, done;
315 partial = sctx->count & 0x3f;
320 if ((partial + len) > 63) {
323 memcpy(sctx->buf + partial, data, done + 64);
328 sha256_transform(sctx->state, src);
331 } while (done + 63 < len);
335 memcpy(sctx->buf + partial, src, len - done);
340 static int sha256_final(struct sha256_state *sctx, u32 *dst)
344 unsigned int index, pad_len;
346 static const u8 padding[64] = { 0x80, };
348 /* Save number of bits */
349 bits = cpu_to_be64(sctx->count << 3);
351 /* Pad out to 56 mod 64. */
352 index = sctx->count & 0x3f;
353 pad_len = (index < 56) ? (56 - index) : ((64+56) - index);
354 sha256_update(sctx, padding, pad_len);
356 /* Append length (before padding) */
357 sha256_update(sctx, (const u8 *)&bits, sizeof(bits));
359 /* Store state in digest */
360 for (i = 0; i < 8; i++)
361 dst[i] = cpu_to_be32(sctx->state[i]);
366 static int sha224_final(struct sha256_state *sctx, u32 *hash)
368 u32 D[SHA256_DIGEST_SIZE / 4];
370 sha256_final(sctx, D);
372 memcpy(hash, D, SHA224_DIGEST_SIZE);
377 class SHA256Hash : public Hash {
380 static Hash *New() { return new SHA256Hash; }
381 virtual void update(const void *data, size_t len);
382 virtual size_t digest_size() const { return SHA256_DIGEST_SIZE; }
383 virtual std::string name() const { return "sha256"; }
386 const uint8_t *finalize();
389 struct sha256_state state;
390 u32 digest_buf[SHA256_DIGEST_SIZE / 4];
393 SHA256Hash::SHA256Hash()
398 void SHA256Hash::update(const void *data, size_t len)
400 sha256_update(&state, static_cast<const u8 *>(data), len);
403 const uint8_t *SHA256Hash::finalize()
405 sha256_final(&state, digest_buf);
406 return reinterpret_cast<uint8_t *>(digest_buf);
409 class SHA224Hash : public Hash {
412 static Hash *New() { return new SHA224Hash; }
413 virtual void update(const void *data, size_t len);
414 virtual size_t digest_size() const { return SHA224_DIGEST_SIZE; }
415 virtual std::string name() const { return "sha224"; }
418 const uint8_t *finalize();
421 struct sha256_state state;
422 u32 digest_buf[SHA224_DIGEST_SIZE / 4];
425 SHA224Hash::SHA224Hash()
430 void SHA224Hash::update(const void *data, size_t len)
432 sha256_update(&state, static_cast<const u8 *>(data), len);
435 const uint8_t *SHA224Hash::finalize()
437 sha224_final(&state, digest_buf);
438 return reinterpret_cast<uint8_t *>(digest_buf);
441 void sha256_register()
443 Hash::Register("sha224", SHA224Hash::New);
444 Hash::Register("sha256", SHA256Hash::New);