Debian Codegraph @

libs/pplib/pplib-src/src/util/utilsha.c

raw
1 /* sha2 implementation excerpted from code by Aaron D. Gifford */
2
3 /*
4 * AUTHOR: Aaron D. Gifford - http://www.aarongifford.com/
5 *
6 * Copyright (c) 2000-2001, Aaron D. Gifford
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the copyright holder nor the names of contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * $Id: sha2.c,v 1.1 2001/11/08 00:01:51 adg Exp adg $
34 */
35
36 #include <stdio.h> /* FILE */
37 #include <string.h> /* memcpy()/memset() or bcopy()/bzero() */
38 //#include <assert.h> /* assert() */
39 #include "utilsha.h"
40
41 /*
42 * UNROLLED TRANSFORM LOOP NOTE:
43 * You can define SHA2_UNROLL_TRANSFORM to use the unrolled transform
44 * loop version for the hash transform rounds (defined using macros
45 * later in this file). Either define on the command line, for example:
46 *
47 * cc -DSHA2_UNROLL_TRANSFORM -o sha2 sha2.c sha2prog.c
48 *
49 * or define below:
50 *
51 * #define SHA2_UNROLL_TRANSFORM
52 *
53 */
54
55 /*** SHA-256/384/512 Machine Architecture Definitions *****************/
56 /*
57 * BYTE_ORDER NOTE:
58 *
59 * Please make sure that your system defines BYTE_ORDER. If your
60 * architecture is little-endian, make sure it also defines
61 * LITTLE_ENDIAN and that the two (BYTE_ORDER and LITTLE_ENDIAN) are
62 * equivilent.
63 *
64 * If your system does not define the above, then you can do so by
65 * hand like this:
66 *
67 * #define LITTLE_ENDIAN 1234
68 * #define BIG_ENDIAN 4321
69 *
70 * And for little-endian machines, add:
71 *
72 * #define BYTE_ORDER LITTLE_ENDIAN
73 *
74 * Or for big-endian machines:
75 *
76 * #define BYTE_ORDER BIG_ENDIAN
77 *
78 * The FreeBSD machine this was written on defines BYTE_ORDER
79 * appropriately by including <sys/types.h> (which in turn includes
80 * <machine/endian.h> where the appropriate definitions are actually
81 * made).
82 */
83
84 #ifndef BYTE_ORDER
85 #define BYTE_ORDER LITTLE_ENDIAN
86 #endif
87
88 //#if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN)
89 //#error Define BYTE_ORDER to be equal to either LITTLE_ENDIAN or BIG_ENDIAN
90 //#endif
91
92 /*
93 * Define the following sha2_* types to types of the correct length on
94 * the native archtecture. Most BSD systems and Linux define u_intXX_t
95 * types. Machines with very recent ANSI C headers, can use the
96 * uintXX_t definintions from inttypes.h by defining SHA2_USE_INTTYPES_H
97 * during compile or in the sha.h header file.
98 *
99 * Machines that support neither u_intXX_t nor inttypes.h's uintXX_t
100 * will need to define these three typedefs below (and the appropriate
101 * ones in sha.h too) by hand according to their system architecture.
102 *
103 * Thank you, Jun-ichiro itojun Hagino, for suggesting using u_intXX_t
104 * types and pointing out recent ANSI C support for uintXX_t in inttypes.h.
105 *
106 * PJ: replace by uintX_t
107 */
108
109 //typedef uint8_t sha2_byte; /* Exactly 1 byte */
110 //typedef uint32_t sha2_word32; /* Exactly 4 bytes */
111 //typedef uint64_t sha2_word64; /* Exactly 8 bytes */
112
113 /*** SHA-256/384/512 Various Length Definitions ***********************/
114 /* NOTE: Most of these are in header */
115 #define SHA256_SHORT_BLOCK_LENGTH (SHA256_BLOCK_LENGTH - 8)
116 #define SHA384_SHORT_BLOCK_LENGTH (SHA384_BLOCK_LENGTH - 16)
117 #define SHA512_SHORT_BLOCK_LENGTH (SHA512_BLOCK_LENGTH - 16)
118
119
120 /*** ENDIAN REVERSAL MACROS *******************************************/
121 #if BYTE_ORDER == LITTLE_ENDIAN
122 #define REVERSE32(w, x) { \
123 uint32_t tmp = (w); \
124 tmp = (tmp >> 16) | (tmp << 16); \
125 (x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \
126 }
127 #define REVERSE64(w, x) { \
128 uint64_t tmp = (w); \
129 tmp = (tmp >> 32) | (tmp << 32); \
130 tmp = ((tmp & 0xff00ff00ff00ff00ULL) >> 8) | \
131 ((tmp & 0x00ff00ff00ff00ffULL) << 8); \
132 (x) = ((tmp & 0xffff0000ffff0000ULL) >> 16) | \
133 ((tmp & 0x0000ffff0000ffffULL) << 16); \
134 }
135 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
136
137 /*
138 * Macro for incrementally adding the unsigned 64-bit integer n to the
139 * unsigned 128-bit integer (represented using a two-element array of
140 * 64-bit words):
141 */
142 #define ADDINC128(w,n) { \
143 (w)[0] += (uint64_t)(n); \
144 if ((w)[0] < (n)) { \
145 (w)[1]++; \
146 } \
147 }
148
149 #define MEMSET_BZERO(p,l) memset((p), 0, (l))
150 #define MEMCPY_BCOPY(d,s,l) memcpy((d), (s), (l))
151
152 /*** THE SIX LOGICAL FUNCTIONS ****************************************/
153 /*
154 * Bit shifting and rotation (used by the six SHA-XYZ logical functions:
155 *
156 * NOTE: The naming of R and S appears backwards here (R is a SHIFT and
157 * S is a ROTATION) because the SHA-256/384/512 description document
158 * (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this
159 * same "backwards" definition.
160 */
161 /* Shift-right (used in SHA-256, SHA-384, and SHA-512): */
162 #define R(b,x) ((x) >> (b))
163 /* 32-bit Rotate-right (used in SHA-256): */
164 #define S32(b,x) (((x) >> (b)) | ((x) << (32 - (b))))
165 /* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
166 #define S64(b,x) (((x) >> (b)) | ((x) << (64 - (b))))
167
168 /* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */
169 #define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
170 #define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
171
172 /* Four of six logical functions used in SHA-256: */
173 #define Sigma0_256(x) (S32(2, (x)) ^ S32(13, (x)) ^ S32(22, (x)))
174 #define Sigma1_256(x) (S32(6, (x)) ^ S32(11, (x)) ^ S32(25, (x)))
175 #define sigma0_256(x) (S32(7, (x)) ^ S32(18, (x)) ^ R(3 , (x)))
176 #define sigma1_256(x) (S32(17, (x)) ^ S32(19, (x)) ^ R(10, (x)))
177
178 /* Four of six logical functions used in SHA-384 and SHA-512: */
179 #define Sigma0_512(x) (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))
180 #define Sigma1_512(x) (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))
181 #define sigma0_512(x) (S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7, (x)))
182 #define sigma1_512(x) (S64(19, (x)) ^ S64(61, (x)) ^ R( 6, (x)))
183
184 static void sha512_last (sha512_state *state);
185 static void sha256_transform (sha256_state *state, const uint32_t idata[16]);
186 static void sha512_transform (sha512_state *state, const uint64_t idata[16]);
187
188 /*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
189 /* Hash constant words K for SHA-256: */
190 static const uint32_t K256[64] = {
191 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
192 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
193 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
194 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,
195 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
196 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
197 0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL,
198 0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL,
199 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,
200 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
201 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL,
202 0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
203 0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL,
204 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,
205 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
206 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
207 };
208
209 /* Initial hash value H for SHA-256: */
210 static const uint32_t sha256_initial_hash_value[8] = {
211 0x6a09e667UL,
212 0xbb67ae85UL,
213 0x3c6ef372UL,
214 0xa54ff53aUL,
215 0x510e527fUL,
216 0x9b05688cUL,
217 0x1f83d9abUL,
218 0x5be0cd19UL
219 };
220
221 /* Hash constant words K for SHA-384 and SHA-512: */
222 static const uint64_t K512[80] = {
223 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
224 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
225 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
226 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
227 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
228 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
229 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
230 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
231 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
232 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
233 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
234 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
235 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
236 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
237 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
238 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
239 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
240 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
241 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
242 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
243 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
244 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
245 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
246 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
247 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
248 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
249 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
250 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
251 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
252 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
253 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
254 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
255 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
256 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
257 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
258 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
259 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
260 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
261 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
262 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
263 };
264
265 /* Initial hash value H for SHA-384 */
266 static const uint64_t sha384_initial_hash_value[8] = {
267 0xcbbb9d5dc1059ed8ULL,
268 0x629a292a367cd507ULL,
269 0x9159015a3070dd17ULL,
270 0x152fecd8f70e5939ULL,
271 0x67332667ffc00b31ULL,
272 0x8eb44a8768581511ULL,
273 0xdb0c2e0d64f98fa7ULL,
274 0x47b5481dbefa4fa4ULL
275 };
276
277 /* Initial hash value H for SHA-512 */
278 static const uint64_t sha512_initial_hash_value[8] = {
279 0x6a09e667f3bcc908ULL,
280 0xbb67ae8584caa73bULL,
281 0x3c6ef372fe94f82bULL,
282 0xa54ff53a5f1d36f1ULL,
283 0x510e527fade682d1ULL,
284 0x9b05688c2b3e6c1fULL,
285 0x1f83d9abfb41bd6bULL,
286 0x5be0cd19137e2179ULL
287 };
288
289 /*** SHA-256: *********************************************************/
290 sha256_state * sha256_digest_init (sha256_state *state)
291 {
292 MEMCPY_BCOPY(state->words, sha256_initial_hash_value, SHA256_DIGEST_LENGTH);
293 MEMSET_BZERO(state->buffer, SHA256_BLOCK_LENGTH);
294 state->bitcount = 0;
295 return state;
296 }
297
298 #ifdef SHA2_UNROLL_TRANSFORM
299
300 /* Unrolled SHA-256 round macros: */
301
302 #if BYTE_ORDER == LITTLE_ENDIAN
303
304 #define ROUND256_0_TO_15(v, a, b, c, d, e, f, g, h) \
305 REVERSE32(v, W256[j]); \
306 T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + K256[j] + W256[j]; \
307 (d) += T1; \
308 (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c))
309
310 #else /* BYTE_ORDER == LITTLE_ENDIAN */
311
312 #define ROUND256_0_TO_15(v, a, b, c, d, e, f, g, h) \
313 T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + K256[j] + (W256[j] = v); \
314 (d) += T1; \
315 (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c))
316
317 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
318
319 #define ROUND256(a, b, c, d, e, f, g, h) \
320 s0 = W256[(j+1)&0x0f]; \
321 s0 = sigma0_256(s0); \
322 s1 = W256[(j+14)&0x0f]; \
323 s1 = sigma1_256(s1); \
324 T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + K256[j] + (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0); \
325 (d) += T1; \
326 (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c))
327
328 static void sha256_transform (sha256_state *state, const uint32_t idata[16]) {
329 uint32_t a, b, c, d, e, f, g, h, s0, s1;
330 uint32_t T1, *W256, v;
331 int j;
332
333 W256 = state->buffer32;
334
335 /* Initialize registers with the prev. intermediate value */
336 a = state->words[0];
337 b = state->words[1];
338 c = state->words[2];
339 d = state->words[3];
340 e = state->words[4];
341 f = state->words[5];
342 g = state->words[6];
343 h = state->words[7];
344
345 j = 0;
346 do {
347 /* Rounds 0 to 15 (unrolled): */
348 v = idata[j]; ROUND256_0_TO_15(v, a, b, c, d, e, f, g, h); ++j;
349 v = idata[j]; ROUND256_0_TO_15(v, h, a, b, c, d, e, f, g); ++j;
350 v = idata[j]; ROUND256_0_TO_15(v, g, h, a, b, c, d, e, f); ++j;
351 v = idata[j]; ROUND256_0_TO_15(v, f, g, h, a, b, c, d, e); ++j;
352 v = idata[j]; ROUND256_0_TO_15(v, e, f, g, h, a, b, c, d); ++j;
353 v = idata[j]; ROUND256_0_TO_15(v, d, e, f, g, h, a, b, c); ++j;
354 v = idata[j]; ROUND256_0_TO_15(v, c, d, e, f, g, h, a, b); ++j;
355 v = idata[j]; ROUND256_0_TO_15(v, b, c, d, e, f, g, h, a); ++j;
356 } while (j < 16);
357
358 /* Now for the remaining rounds to 64: */
359 do {
360 ROUND256(a, b, c, d, e, f, g, h); ++j;
361 ROUND256(h, a, b, c, d, e, f, g); ++j;
362 ROUND256(g, h, a, b, c, d, e, f); ++j;
363 ROUND256(f, g, h, a, b, c, d, e); ++j;
364 ROUND256(e, f, g, h, a, b, c, d); ++j;
365 ROUND256(d, e, f, g, h, a, b, c); ++j;
366 ROUND256(c, d, e, f, g, h, a, b); ++j;
367 ROUND256(b, c, d, e, f, g, h, a); ++j;
368 } while (j < 64);
369
370 /* Compute the current intermediate hash value */
371 state->words[0] += a;
372 state->words[1] += b;
373 state->words[2] += c;
374 state->words[3] += d;
375 state->words[4] += e;
376 state->words[5] += f;
377 state->words[6] += g;
378 state->words[7] += h;
379 }
380
381 #else /* SHA2_UNROLL_TRANSFORM */
382
383 static void sha256_transform (sha256_state *state, const uint32_t idata[16]) {
384 uint32_t a, b, c, d, e, f, g, h, s0, s1;
385 uint32_t T1, T2, *W256, v;
386 int j;
387
388 W256 = state->buffer32;
389
390 /* Initialize registers with the prev. intermediate value */
391 a = state->words[0];
392 b = state->words[1];
393 c = state->words[2];
394 d = state->words[3];
395 e = state->words[4];
396 f = state->words[5];
397 g = state->words[6];
398 h = state->words[7];
399
400 j = 0;
401 do {
402 v = idata[j];
403 #if BYTE_ORDER == LITTLE_ENDIAN
404 /* Copy data while converting to host byte order */
405 REVERSE32(v, W256[j]);
406 /* Apply the SHA-256 compression function to update a..h */
407 T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j];
408 #else /* BYTE_ORDER == LITTLE_ENDIAN */
409 /* Apply the SHA-256 compression function to update a..h with copy */
410 T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + (W256[j] = v);
411 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
412 T2 = Sigma0_256(a) + Maj(a, b, c);
413 h = g;
414 g = f;
415 f = e;
416 e = d + T1;
417 d = c;
418 c = b;
419 b = a;
420 a = T1 + T2;
421
422 j++;
423 } while (j < 16);
424
425 do {
426 /* Part of the message block expansion: */
427 s0 = W256[(j+1)&0x0f];
428 s0 = sigma0_256(s0);
429 s1 = W256[(j+14)&0x0f];
430 s1 = sigma1_256(s1);
431
432 /* Apply the SHA-256 compression function to update a..h */
433 T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0);
434 T2 = Sigma0_256(a) + Maj(a, b, c);
435 h = g;
436 g = f;
437 f = e;
438 e = d + T1;
439 d = c;
440 c = b;
441 b = a;
442 a = T1 + T2;
443
444 j++;
445 } while (j < 64);
446
447 /* Compute the current intermediate hash value */
448 state->words[0] += a;
449 state->words[1] += b;
450 state->words[2] += c;
451 state->words[3] += d;
452 state->words[4] += e;
453 state->words[5] += f;
454 state->words[6] += g;
455 state->words[7] += h;
456 }
457
458 #endif /* SHA2_UNROLL_TRANSFORM */
459
460 /* PJ: alignment-safe version */
461
462 #define data_aligned4(data) (((data - (const uint8_t *)(0UL)) & 3) == 0)
463 #define data_aligned8(data) (((data - (const uint8_t *)(0ULL)) & 7) == 0)
464
465 static void sha256_transform_aligned (sha256_state *state, const uint8_t *data) {
466 if (data_aligned4(data))
467 {
468 sha256_transform(state, (const uint32_t *)((const void *)data)); // alignment ok
469 }
470 else
471 {
472 uint32_t idata[16];
473 memcpy(&idata[0], data, 16 * sizeof(uint32_t));
474 sha256_transform(state, idata);
475 }
476 }
477
478 void sha256_digest_add (sha256_state *state, const void *vdata, size_t len)
479 {
480 unsigned int freespace, usedspace;
481 const uint8_t *data;
482
483 if (len == 0) /* Calling with no data is valid - we do nothing */
484 return;
485
486 data = (const uint8_t *)vdata;
487
488 usedspace = (state->bitcount >> 3) % SHA256_BLOCK_LENGTH;
489 if (usedspace > 0)
490 {
491 /* Calculate how much free space is available in the buffer */
492 freespace = SHA256_BLOCK_LENGTH - usedspace;
493
494 if (len >= freespace)
495 {
496 /* Fill the buffer completely and process it */
497 MEMCPY_BCOPY(&state->buffer[usedspace], data, freespace);
498 state->bitcount += freespace << 3;
499 len -= freespace;
500 data += freespace;
501 sha256_transform(state, state->buffer32);
502 }
503 else
504 {
505 /* The buffer is not yet full */
506 MEMCPY_BCOPY(&state->buffer[usedspace], data, len);
507 state->bitcount += len << 3;
508 return;
509 }
510 }
511 while (len >= SHA256_BLOCK_LENGTH)
512 {
513 /* Process as many complete blocks as we can */
514 sha256_transform_aligned(state, data);
515
516 state->bitcount += SHA256_BLOCK_LENGTH << 3;
517 len -= SHA256_BLOCK_LENGTH;
518 data += SHA256_BLOCK_LENGTH;
519 }
520 if (len > 0)
521 {
522 /* There's left-overs, so save 'em */
523 MEMCPY_BCOPY(state->buffer, data, len);
524 state->bitcount += len << 3;
525 }
526 }
527
528 static void digest_hex (uint8_t digest[], const void *data, size_t size, int flags);
529
530 void sha256_digest_get (sha256_state *state, uint8_t digest[], int flags) {
531 unsigned int usedspace;
532
533 usedspace = (state->bitcount >> 3) % SHA256_BLOCK_LENGTH;
534 #if BYTE_ORDER == LITTLE_ENDIAN
535 /* Convert FROM host byte order */
536 REVERSE64(state->bitcount,state->bitcount);
537 #endif
538 if (usedspace > 0)
539 {
540 /* Begin padding with a 1 bit: */
541 state->buffer[usedspace++] = 0x80;
542
543 if (usedspace <= SHA256_SHORT_BLOCK_LENGTH) {
544 /* Set-up for the last transform: */
545 MEMSET_BZERO(&state->buffer[usedspace], SHA256_SHORT_BLOCK_LENGTH - usedspace);
546 } else {
547 if (usedspace < SHA256_BLOCK_LENGTH) {
548 MEMSET_BZERO(&state->buffer[usedspace], SHA256_BLOCK_LENGTH - usedspace);
549 }
550 /* Do second-to-last transform: */
551 sha256_transform(state, state->buffer32);
552
553 /* And set-up for the last transform: */
554 MEMSET_BZERO(state->buffer, SHA256_SHORT_BLOCK_LENGTH);
555 }
556 }
557 else
558 {
559 /* Set-up for the last transform: */
560 MEMSET_BZERO(state->buffer, SHA256_SHORT_BLOCK_LENGTH);
561
562 /* Begin padding with a 1 bit: */
563 *state->buffer = 0x80;
564 }
565 /* Set the bit count: */
566 //*(uint64_t*)&state->buffer[SHA256_SHORT_BLOCK_LENGTH] = state->bitcount; // aliasing violation warning
567 state->buffer64[SHA256_SHORT_BLOCK_LENGTH / sizeof(uint64_t)] = state->bitcount;
568
569 /* Final transform: */
570 sha256_transform(state, state->buffer32);
571
572 #if BYTE_ORDER == LITTLE_ENDIAN
573 {
574 /* Convert TO host byte order */
575 int j;
576 for (j = 0; j < 8; j++)
577 {
578 REVERSE32(state->words[j], state->words[j]);
579 }
580 }
581 #endif
582 if (flags & SHA_HEX)
583 digest_hex(digest, state->words, SHA256_DIGEST_LENGTH, flags);
584 else
585 memcpy(digest, state->words, SHA256_DIGEST_LENGTH);
586 }
587
588 /*** SHA-512: *********************************************************/
589 sha512_state * sha512_digest_init (sha512_state *state)
590 {
591 MEMCPY_BCOPY(state->words, sha512_initial_hash_value, SHA512_DIGEST_LENGTH);
592 MEMSET_BZERO(state->buffer, SHA512_BLOCK_LENGTH);
593 state->bitcount[0] = 0;
594 state->bitcount[1] = 0;
595 return state;
596 }
597
598 #ifdef SHA2_UNROLL_TRANSFORM
599
600 /* PJ: ++ operations moved out of macros! */
601
602 /* Unrolled SHA-512 round macros: */
603 #if BYTE_ORDER == LITTLE_ENDIAN
604
605 #define ROUND512_0_TO_15(v, a, b, c, d, e, f, g, h) \
606 REVERSE64(v, W512[j]); \
607 T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] + W512[j]; \
608 (d) += T1; \
609 (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c))
610
611 #else /* BYTE_ORDER == LITTLE_ENDIAN */
612
613 #define ROUND512_0_TO_15(v, a, b, c, d, e, f, g, h) \
614 T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] + (W512[j] = v); \
615 (d) += T1; \
616 (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c))
617
618 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
619
620 #define ROUND512(a, b, c, d, e, f, g, h) \
621 s0 = W512[(j+1)&0x0f]; \
622 s0 = sigma0_512(s0); \
623 s1 = W512[(j+14)&0x0f]; \
624 s1 = sigma1_512(s1); \
625 T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] + (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); \
626 (d) += T1; \
627 (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c))
628
629 static void sha512_transform (sha512_state *state, const uint64_t idata[16])
630 {
631 uint64_t a, b, c, d, e, f, g, h, s0, s1;
632 uint64_t T1, *W512, v;
633 int j;
634
635 W512 = state->buffer64;
636
637 /* Initialize registers with the prev. intermediate value */
638 a = state->words[0];
639 b = state->words[1];
640 c = state->words[2];
641 d = state->words[3];
642 e = state->words[4];
643 f = state->words[5];
644 g = state->words[6];
645 h = state->words[7];
646
647 j = 0;
648 do {
649 v = idata[j]; ROUND512_0_TO_15(v, a, b, c, d, e, f, g, h); ++j;
650 v = idata[j]; ROUND512_0_TO_15(v, h, a, b, c, d, e, f, g); ++j;
651 v = idata[j]; ROUND512_0_TO_15(v, g, h, a, b, c, d, e, f); ++j;
652 v = idata[j]; ROUND512_0_TO_15(v, f, g, h, a, b, c, d, e); ++j;
653 v = idata[j]; ROUND512_0_TO_15(v, e, f, g, h, a, b, c, d); ++j;
654 v = idata[j]; ROUND512_0_TO_15(v, d, e, f, g, h, a, b, c); ++j;
655 v = idata[j]; ROUND512_0_TO_15(v, c, d, e, f, g, h, a, b); ++j;
656 v = idata[j]; ROUND512_0_TO_15(v, b, c, d, e, f, g, h, a); ++j;
657 } while (j < 16);
658
659 /* Now for the remaining rounds up to 79: */
660 do {
661 ROUND512(a, b, c, d, e, f, g, h); ++j;
662 ROUND512(h, a, b, c, d, e, f, g); ++j;
663 ROUND512(g, h, a, b, c, d, e, f); ++j;
664 ROUND512(f, g, h, a, b, c, d, e); ++j;
665 ROUND512(e, f, g, h, a, b, c, d); ++j;
666 ROUND512(d, e, f, g, h, a, b, c); ++j;
667 ROUND512(c, d, e, f, g, h, a, b); ++j;
668 ROUND512(b, c, d, e, f, g, h, a); ++j;
669 } while (j < 80);
670
671 /* Compute the current intermediate hash value */
672 state->words[0] += a;
673 state->words[1] += b;
674 state->words[2] += c;
675 state->words[3] += d;
676 state->words[4] += e;
677 state->words[5] += f;
678 state->words[6] += g;
679 state->words[7] += h;
680 }
681
682 #else /* SHA2_UNROLL_TRANSFORM */
683
684 static void sha512_transform (sha512_state *state, const uint64_t idata[16])
685 {
686 uint64_t a, b, c, d, e, f, g, h, s0, s1;
687 uint64_t T1, T2, *W512, v;
688 int j;
689
690 W512 = state->buffer64;
691
692 /* Initialize registers with the prev. intermediate value */
693 a = state->words[0];
694 b = state->words[1];
695 c = state->words[2];
696 d = state->words[3];
697 e = state->words[4];
698 f = state->words[5];
699 g = state->words[6];
700 h = state->words[7];
701
702 j = 0;
703 do {
704 v = idata[j];
705 #if BYTE_ORDER == LITTLE_ENDIAN
706 /* Convert TO host byte order */
707 REVERSE64(v, W512[j]);
708 /* Apply the SHA-512 compression function to update a..h */
709 T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
710 #else /* BYTE_ORDER == LITTLE_ENDIAN */
711 /* Apply the SHA-512 compression function to update a..h with copy */
712 T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j] = v);
713 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
714 T2 = Sigma0_512(a) + Maj(a, b, c);
715 h = g;
716 g = f;
717 f = e;
718 e = d + T1;
719 d = c;
720 c = b;
721 b = a;
722 a = T1 + T2;
723
724 j++;
725 } while (j < 16);
726
727 do {
728 /* Part of the message block expansion: */
729 s0 = W512[(j+1)&0x0f];
730 s0 = sigma0_512(s0);
731 s1 = W512[(j+14)&0x0f];
732 s1 = sigma1_512(s1);
733
734 /* Apply the SHA-512 compression function to update a..h */
735 T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);
736 T2 = Sigma0_512(a) + Maj(a, b, c);
737 h = g;
738 g = f;
739 f = e;
740 e = d + T1;
741 d = c;
742 c = b;
743 b = a;
744 a = T1 + T2;
745
746 j++;
747 } while (j < 80);
748
749 /* Compute the current intermediate hash value */
750 state->words[0] += a;
751 state->words[1] += b;
752 state->words[2] += c;
753 state->words[3] += d;
754 state->words[4] += e;
755 state->words[5] += f;
756 state->words[6] += g;
757 state->words[7] += h;
758 }
759
760 #endif /* SHA2_UNROLL_TRANSFORM */
761
762 static void sha512_transform_aligned (sha512_state *state, const uint8_t *data)
763 {
764 if (data_aligned8(data))
765 {
766 sha512_transform(state, (const uint64_t *)((const void *)data)); // alignment ok
767 }
768 else
769 {
770 uint64_t idata[16];
771 memcpy(&idata[0], data, 16 * sizeof(uint64_t));
772 sha512_transform(state, idata);
773 }
774 }
775
776 void sha512_digest_add (sha512_state *state, const void *vdata, size_t len)
777 {
778 unsigned int freespace, usedspace;
779 const uint8_t *data;
780
781 if (len == 0) /* Calling with no data is valid - we do nothing */
782 return;
783
784 /* Sanity check: */
785 data = (const uint8_t *)vdata;
786
787 usedspace = (state->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
788 if (usedspace > 0)
789 {
790 /* Calculate how much free space is available in the buffer */
791 freespace = SHA512_BLOCK_LENGTH - usedspace;
792
793 if (len >= freespace)
794 {
795 /* Fill the buffer completely and process it */
796 MEMCPY_BCOPY(&state->buffer[usedspace], data, freespace);
797 ADDINC128(state->bitcount, freespace << 3);
798 len -= freespace;
799 data += freespace;
800 sha512_transform(state, state->buffer64);
801 }
802 else
803 {
804 /* The buffer is not yet full */
805 MEMCPY_BCOPY(&state->buffer[usedspace], data, len);
806 ADDINC128(state->bitcount, len << 3);
807 return;
808 }
809 }
810 while (len >= SHA512_BLOCK_LENGTH)
811 {
812 /* Process as many complete blocks as we can */
813 sha512_transform_aligned(state, data);
814
815 ADDINC128(state->bitcount, SHA512_BLOCK_LENGTH << 3);
816 len -= SHA512_BLOCK_LENGTH;
817 data += SHA512_BLOCK_LENGTH;
818 }
819 if (len > 0)
820 {
821 /* There's left-overs, so save 'em */
822 MEMCPY_BCOPY(state->buffer, data, len);
823 ADDINC128(state->bitcount, len << 3);
824 }
825 }
826
827 static void sha512_last (sha512_state *state)
828 {
829 unsigned int usedspace;
830
831 usedspace = (state->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
832 #if BYTE_ORDER == LITTLE_ENDIAN
833 /* Convert FROM host byte order */
834 REVERSE64(state->bitcount[0],state->bitcount[0]);
835 REVERSE64(state->bitcount[1],state->bitcount[1]);
836 #endif
837 if (usedspace > 0)
838 {
839 /* Begin padding with a 1 bit: */
840 state->buffer[usedspace++] = 0x80;
841
842 if (usedspace <= SHA512_SHORT_BLOCK_LENGTH) {
843 /* Set-up for the last transform: */
844 MEMSET_BZERO(&state->buffer[usedspace], SHA512_SHORT_BLOCK_LENGTH - usedspace);
845 } else {
846 if (usedspace < SHA512_BLOCK_LENGTH) {
847 MEMSET_BZERO(&state->buffer[usedspace], SHA512_BLOCK_LENGTH - usedspace);
848 }
849 /* Do second-to-last transform: */
850 sha512_transform(state, state->buffer64);
851
852 /* And set-up for the last transform: */
853 //MEMSET_BZERO(state->buffer, SHA512_BLOCK_LENGTH - 2); // seems a typo, we overwrite last 16 bytes below
854 MEMSET_BZERO(state->buffer, SHA512_SHORT_BLOCK_LENGTH);
855 }
856 }
857 else
858 {
859 /* Prepare for final transform: */
860 MEMSET_BZERO(state->buffer, SHA512_SHORT_BLOCK_LENGTH);
861
862 /* Begin padding with a 1 bit: */
863 *state->buffer = 0x80;
864 }
865 /* Store the length of input data (in bits): */
866 //*(uint64_t*)&state->buffer[SHA512_SHORT_BLOCK_LENGTH] = state->bitcount[1]; // aliasing violation warning
867 //*(uint64_t*)&state->buffer[SHA512_SHORT_BLOCK_LENGTH+8] = state->bitcount[0];
868 state->buffer64[SHA512_SHORT_BLOCK_LENGTH / sizeof(uint64_t)] = state->bitcount[1];
869 state->buffer64[SHA512_SHORT_BLOCK_LENGTH / sizeof(uint64_t) + 1] = state->bitcount[0];
870
871 /* Final transform: */
872 sha512_transform(state, state->buffer64);
873 }
874
875 void sha512_digest_get (sha512_state *state, uint8_t digest[], int flags)
876 {
877 /* If no digest buffer is passed, we don't bother doing this: */
878 sha512_last(state);
879
880 /* Save the hash data for output: */
881 #if BYTE_ORDER == LITTLE_ENDIAN
882 {
883 /* Convert TO host byte order */
884 int j;
885 for (j = 0; j < 8; j++)
886 {
887 REVERSE64(state->words[j], state->words[j]);
888 }
889 }
890 #endif
891 if (flags & SHA_HEX)
892 digest_hex(digest, state->words, SHA512_DIGEST_LENGTH, flags);
893 else
894 memcpy(digest, state->words, SHA512_DIGEST_LENGTH);
895 }
896
897 /*** SHA-384: *********************************************************/
898 sha384_state * sha384_digest_init (sha384_state *state)
899 {
900 MEMCPY_BCOPY(state->words, sha384_initial_hash_value, SHA512_DIGEST_LENGTH);
901 MEMSET_BZERO(state->buffer, SHA384_BLOCK_LENGTH);
902 state->bitcount[0] = state->bitcount[1] = 0;
903 return state;
904 }
905
906 void sha384_digest_add (sha384_state *state, const void *data, size_t len)
907 {
908 sha512_digest_add((sha512_state *)state, data, len);
909 }
910
911 void sha384_digest_get (sha384_state *state, uint8_t digest[], int flags)
912 {
913 sha512_last((sha512_state *)state);
914
915 /* Save the hash data for output: */
916 #if BYTE_ORDER == LITTLE_ENDIAN
917 {
918 /* Convert TO host byte order */
919 int j;
920 for (j = 0; j < 6; j++)
921 {
922 REVERSE64(state->words[j], state->words[j]);
923 }
924 }
925 #endif
926 if (flags & SHA_HEX)
927 digest_hex(digest, state->words, SHA384_DIGEST_LENGTH, flags);
928 else
929 memcpy(digest, state->words, SHA384_DIGEST_LENGTH);
930 }
931
932 /* hex output */
933
934 static void digest_hex (uint8_t digest[], const void *data, size_t size, int flags)
935 {
936 const char *alphabet;
937 const uint8_t *bytes;
938 size_t i;
939
940 bytes = (const uint8_t *)data;
941 alphabet = (flags & SHA_LCHEX) ? "0123456789abcdef" : "0123456789ABCDEF";
942 for (i = 0; i < size; ++i, ++bytes)
943 {
944 *digest++ = (uint8_t)alphabet[(*bytes) >> 4];
945 *digest++ = (uint8_t)alphabet[(*bytes) & 15];
946 }
947 *digest = 0;
948 }
949
950 /* string checksum */
951
952 void sha256_digest (const void *data, size_t size, uint8_t digest[], int flags)
953 {
954 sha256_state state;
955 sha256_digest_init(&state);
956 sha256_digest_add(&state, data, size);
957 sha256_digest_get(&state, digest, flags);
958 }
959
960 void sha384_digest (const void *data, size_t size, uint8_t digest[], int flags)
961 {
962 sha384_state state;
963 sha384_digest_init(&state);
964 sha384_digest_add(&state, data, size);
965 sha384_digest_get(&state, digest, flags);
966 }
967
968 void sha512_digest (const void *data, size_t size, uint8_t digest[], int flags)
969 {
970 sha512_state state;
971 sha512_digest_init(&state);
972 sha512_digest_add(&state, data, size);
973 sha512_digest_get(&state, digest, flags);
974 }
975
976 /* file checksum */
977
978 #define DIGEST_BUFFER_SIZE 4096
979
980 int sha256_digest_add_file (sha256_state *state, const char *filename)
981 {
982 FILE *fh;
983 uint8_t buffer[DIGEST_BUFFER_SIZE];
984 size_t read;
985
986 if ((fh = fopen(filename, "rb")) == NULL)
987 return 0;
988 do {
989 read = fread(buffer, 1, DIGEST_BUFFER_SIZE, fh);
990 sha256_digest_add(state, buffer, read);
991 } while (read == DIGEST_BUFFER_SIZE);
992 fclose(fh);
993 return 1;
994 }
995
996 int sha256_digest_file (const char *filename, uint8_t digest[], int flags)
997 {
998 sha256_state state;
999
1000 sha256_digest_init(&state);
1001 if (sha256_digest_add_file(&state, filename))
1002 {
1003 sha256_digest_get(&state, digest, flags);
1004 return 1;
1005 }
1006 return 0;
1007 }
1008
1009 int sha384_digest_add_file (sha384_state *state, const char *filename)
1010 {
1011 FILE *fh;
1012 uint8_t buffer[DIGEST_BUFFER_SIZE];
1013 size_t read;
1014
1015 if ((fh = fopen(filename, "rb")) == NULL)
1016 return 0;
1017 do {
1018 read = fread(buffer, 1, DIGEST_BUFFER_SIZE, fh);
1019 sha384_digest_add(state, buffer, read);
1020 } while (read == DIGEST_BUFFER_SIZE);
1021 fclose(fh);
1022 return 1;
1023 }
1024
1025 int sha384_digest_file (const char *filename, uint8_t digest[], int flags)
1026 {
1027 sha384_state state;
1028
1029 sha384_digest_init(&state);
1030 if (sha384_digest_add_file(&state, filename))
1031 {
1032 sha384_digest_get(&state, digest, flags);
1033 return 1;
1034 }
1035 return 0;
1036 }
1037
1038 int sha512_digest_add_file (sha512_state *state, const char *filename)
1039 {
1040 FILE *fh;
1041 uint8_t buffer[DIGEST_BUFFER_SIZE];
1042 size_t read;
1043
1044 if ((fh = fopen(filename, "rb")) == NULL)
1045 return 0;
1046 do {
1047 read = fread(buffer, 1, DIGEST_BUFFER_SIZE, fh);
1048 sha512_digest_add(state, buffer, read);
1049 } while (read == DIGEST_BUFFER_SIZE);
1050 fclose(fh);
1051 return 1;
1052 }
1053
1054 int sha512_digest_file (const char *filename, uint8_t digest[], int flags)
1055 {
1056 sha512_state state;
1057
1058 sha512_digest_init(&state);
1059 if (sha512_digest_add_file(&state, filename))
1060 {
1061 sha512_digest_get(&state, digest, flags);
1062 return 1;
1063 }
1064 return 0;
1065 }