1 /*	$OpenBSD: sha2.c,v 1.17 2014/12/31 16:56:55 tedu Exp $	*/
2 
3 /*
4  * FILE:	sha2.c
5  * AUTHOR:	Aaron D. Gifford <me@aarongifford.com>
6  *
7  * Copyright (c) 2000-2001, Aaron D. Gifford
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the copyright holder nor the names of contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * $From: sha2.c,v 1.1 2001/11/08 00:01:51 adg Exp adg $
35  */
36 
37 #ifndef _OS_LINUX
38 #include <sys/endian.h>
39 #endif
40 #include <sys/param.h>
41 #include <sys/time.h>
42 #include <strings.h>
43 #include <string.h>
44 #include <stdint.h>
45 #include "sha2.h"
46 void	explicit_bzero(void *, size_t);
47 
48 /*
49  * UNROLLED TRANSFORM LOOP NOTE:
50  * You can define SHA2_UNROLL_TRANSFORM to use the unrolled transform
51  * loop version for the hash transform rounds (defined using macros
52  * later in this file).  Either define on the command line, for example:
53  *
54  *   cc -DSHA2_UNROLL_TRANSFORM -o sha2 sha2.c sha2prog.c
55  *
56  * or define below:
57  *
58  *   #define SHA2_UNROLL_TRANSFORM
59  *
60  */
61 #ifndef SMALL_KERNEL
62 #if defined(__amd64__) || defined(__i386__)
63 #define SHA2_UNROLL_TRANSFORM
64 #endif
65 #endif
66 
67 /*** SHA-256/384/512 Machine Architecture Definitions *****************/
68 /*
69  * BYTE_ORDER NOTE:
70  *
71  * Please make sure that your system defines BYTE_ORDER.  If your
72  * architecture is little-endian, make sure it also defines
73  * LITTLE_ENDIAN and that the two (BYTE_ORDER and LITTLE_ENDIAN) are
74  * equivilent.
75  *
76  * If your system does not define the above, then you can do so by
77  * hand like this:
78  *
79  *   #define LITTLE_ENDIAN 1234
80  *   #define BIG_ENDIAN    4321
81  *
82  * And for little-endian machines, add:
83  *
84  *   #define BYTE_ORDER LITTLE_ENDIAN
85  *
86  * Or for big-endian machines:
87  *
88  *   #define BYTE_ORDER BIG_ENDIAN
89  *
90  * The FreeBSD machine this was written on defines BYTE_ORDER
91  * appropriately by including <sys/types.h> (which in turn includes
92  * <machine/endian.h> where the appropriate definitions are actually
93  * made).
94  */
95 #if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN)
96 #error Define BYTE_ORDER to be equal to either LITTLE_ENDIAN or BIG_ENDIAN
97 #endif
98 
99 
100 /*** SHA-256/384/512 Various Length Definitions ***********************/
101 /* NOTE: Most of these are in sha2.h */
102 #define SHA256_SHORT_BLOCK_LENGTH	(SHA256_BLOCK_LENGTH - 8)
103 #define SHA384_SHORT_BLOCK_LENGTH	(SHA384_BLOCK_LENGTH - 16)
104 #define SHA512_SHORT_BLOCK_LENGTH	(SHA512_BLOCK_LENGTH - 16)
105 
106 /*
107  * Macro for incrementally adding the unsigned 64-bit integer n to the
108  * unsigned 128-bit integer (represented using a two-element array of
109  * 64-bit words):
110  */
111 #define ADDINC128(w,n)	{ \
112 	(w)[0] += (u_int64_t)(n); \
113 	if ((w)[0] < (n)) { \
114 		(w)[1]++; \
115 	} \
116 }
117 
118 /*** THE SIX LOGICAL FUNCTIONS ****************************************/
119 /*
120  * Bit shifting and rotation (used by the six SHA-XYZ logical functions:
121  *
122  *   NOTE:  The naming of R and S appears backwards here (R is a SHIFT and
123  *   S is a ROTATION) because the SHA-256/384/512 description document
124  *   (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this
125  *   same "backwards" definition.
126  */
127 /* Shift-right (used in SHA-256, SHA-384, and SHA-512): */
128 #define R(b,x) 		((x) >> (b))
129 /* 32-bit Rotate-right (used in SHA-256): */
130 #define S32(b,x)	(((x) >> (b)) | ((x) << (32 - (b))))
131 /* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
132 #define S64(b,x)	(((x) >> (b)) | ((x) << (64 - (b))))
133 
134 /* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */
135 #define Ch(x,y,z)	(((x) & (y)) ^ ((~(x)) & (z)))
136 #define Maj(x,y,z)	(((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
137 
138 /* Four of six logical functions used in SHA-256: */
139 #define Sigma0_256(x)	(S32(2,  (x)) ^ S32(13, (x)) ^ S32(22, (x)))
140 #define Sigma1_256(x)	(S32(6,  (x)) ^ S32(11, (x)) ^ S32(25, (x)))
141 #define sigma0_256(x)	(S32(7,  (x)) ^ S32(18, (x)) ^ R(3 ,   (x)))
142 #define sigma1_256(x)	(S32(17, (x)) ^ S32(19, (x)) ^ R(10,   (x)))
143 
144 /* Four of six logical functions used in SHA-384 and SHA-512: */
145 #define Sigma0_512(x)	(S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))
146 #define Sigma1_512(x)	(S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))
147 #define sigma0_512(x)	(S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7,   (x)))
148 #define sigma1_512(x)	(S64(19, (x)) ^ S64(61, (x)) ^ R( 6,   (x)))
149 
150 /*** INTERNAL FUNCTION PROTOTYPES *************************************/
151 /* NOTE: These should not be accessed directly from outside this
152  * library -- they are intended for private internal visibility/use
153  * only.
154  */
155 void SHA512Last(SHA2_CTX *);
156 void SHA256Transform(u_int32_t *, const u_int8_t *);
157 void SHA512Transform(u_int64_t *, const u_int8_t *);
158 
159 
160 /*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
161 /* Hash constant words K for SHA-256: */
162 const static u_int32_t K256[64] = {
163 	0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
164 	0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
165 	0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
166 	0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,
167 	0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
168 	0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
169 	0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL,
170 	0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL,
171 	0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,
172 	0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
173 	0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL,
174 	0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
175 	0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL,
176 	0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,
177 	0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
178 	0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
179 };
180 
181 /* Initial hash value H for SHA-256: */
182 const static u_int32_t sha256_initial_hash_value[8] = {
183 	0x6a09e667UL,
184 	0xbb67ae85UL,
185 	0x3c6ef372UL,
186 	0xa54ff53aUL,
187 	0x510e527fUL,
188 	0x9b05688cUL,
189 	0x1f83d9abUL,
190 	0x5be0cd19UL
191 };
192 
193 /* Hash constant words K for SHA-384 and SHA-512: */
194 const static u_int64_t K512[80] = {
195 	0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
196 	0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
197 	0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
198 	0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
199 	0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
200 	0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
201 	0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
202 	0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
203 	0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
204 	0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
205 	0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
206 	0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
207 	0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
208 	0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
209 	0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
210 	0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
211 	0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
212 	0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
213 	0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
214 	0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
215 	0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
216 	0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
217 	0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
218 	0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
219 	0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
220 	0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
221 	0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
222 	0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
223 	0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
224 	0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
225 	0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
226 	0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
227 	0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
228 	0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
229 	0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
230 	0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
231 	0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
232 	0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
233 	0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
234 	0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
235 };
236 
237 /* Initial hash value H for SHA-384 */
238 const static u_int64_t sha384_initial_hash_value[8] = {
239 	0xcbbb9d5dc1059ed8ULL,
240 	0x629a292a367cd507ULL,
241 	0x9159015a3070dd17ULL,
242 	0x152fecd8f70e5939ULL,
243 	0x67332667ffc00b31ULL,
244 	0x8eb44a8768581511ULL,
245 	0xdb0c2e0d64f98fa7ULL,
246 	0x47b5481dbefa4fa4ULL
247 };
248 
249 /* Initial hash value H for SHA-512 */
250 const static u_int64_t sha512_initial_hash_value[8] = {
251 	0x6a09e667f3bcc908ULL,
252 	0xbb67ae8584caa73bULL,
253 	0x3c6ef372fe94f82bULL,
254 	0xa54ff53a5f1d36f1ULL,
255 	0x510e527fade682d1ULL,
256 	0x9b05688c2b3e6c1fULL,
257 	0x1f83d9abfb41bd6bULL,
258 	0x5be0cd19137e2179ULL
259 };
260 
261 
262 /*** SHA-256: *********************************************************/
263 void
SHA256Init(SHA2_CTX * context)264 SHA256Init(SHA2_CTX *context)
265 {
266 	memcpy(context->state.st32, sha256_initial_hash_value,
267 	    SHA256_DIGEST_LENGTH);
268 	memset(context->buffer, 0, SHA256_BLOCK_LENGTH);
269 	context->bitcount[0] = 0;
270 }
271 
272 #ifdef SHA2_UNROLL_TRANSFORM
273 
274 /* Unrolled SHA-256 round macros: */
275 
276 #define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) do {				    \
277 	W256[j] = (u_int32_t)data[3] | ((u_int32_t)data[2] << 8) |	    \
278 	    ((u_int32_t)data[1] << 16) | ((u_int32_t)data[0] << 24);	    \
279 	data += 4;							    \
280 	T1 = (h) + Sigma1_256((e)) + Ch((e), (f), (g)) + K256[j] + W256[j]; \
281 	(d) += T1;							    \
282 	(h) = T1 + Sigma0_256((a)) + Maj((a), (b), (c));		    \
283 	j++;								    \
284 } while(0)
285 
286 #define ROUND256(a,b,c,d,e,f,g,h) do {					    \
287 	s0 = W256[(j+1)&0x0f];						    \
288 	s0 = sigma0_256(s0);						    \
289 	s1 = W256[(j+14)&0x0f];						    \
290 	s1 = sigma1_256(s1);						    \
291 	T1 = (h) + Sigma1_256((e)) + Ch((e), (f), (g)) + K256[j] +	    \
292 	     (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0);		    \
293 	(d) += T1;							    \
294 	(h) = T1 + Sigma0_256((a)) + Maj((a), (b), (c));		    \
295 	j++;								    \
296 } while(0)
297 
298 void
SHA256Transform(u_int32_t * state,const u_int8_t * data)299 SHA256Transform(u_int32_t *state, const u_int8_t *data)
300 {
301 	u_int32_t	a, b, c, d, e, f, g, h, s0, s1;
302 	u_int32_t	T1, W256[16];
303 	int		j;
304 
305 	/* Initialize registers with the prev. intermediate value */
306 	a = state[0];
307 	b = state[1];
308 	c = state[2];
309 	d = state[3];
310 	e = state[4];
311 	f = state[5];
312 	g = state[6];
313 	h = state[7];
314 
315 	j = 0;
316 	do {
317 		/* Rounds 0 to 15 (unrolled): */
318 		ROUND256_0_TO_15(a,b,c,d,e,f,g,h);
319 		ROUND256_0_TO_15(h,a,b,c,d,e,f,g);
320 		ROUND256_0_TO_15(g,h,a,b,c,d,e,f);
321 		ROUND256_0_TO_15(f,g,h,a,b,c,d,e);
322 		ROUND256_0_TO_15(e,f,g,h,a,b,c,d);
323 		ROUND256_0_TO_15(d,e,f,g,h,a,b,c);
324 		ROUND256_0_TO_15(c,d,e,f,g,h,a,b);
325 		ROUND256_0_TO_15(b,c,d,e,f,g,h,a);
326 	} while (j < 16);
327 
328 	/* Now for the remaining rounds to 64: */
329 	do {
330 		ROUND256(a,b,c,d,e,f,g,h);
331 		ROUND256(h,a,b,c,d,e,f,g);
332 		ROUND256(g,h,a,b,c,d,e,f);
333 		ROUND256(f,g,h,a,b,c,d,e);
334 		ROUND256(e,f,g,h,a,b,c,d);
335 		ROUND256(d,e,f,g,h,a,b,c);
336 		ROUND256(c,d,e,f,g,h,a,b);
337 		ROUND256(b,c,d,e,f,g,h,a);
338 	} while (j < 64);
339 
340 	/* Compute the current intermediate hash value */
341 	state[0] += a;
342 	state[1] += b;
343 	state[2] += c;
344 	state[3] += d;
345 	state[4] += e;
346 	state[5] += f;
347 	state[6] += g;
348 	state[7] += h;
349 
350 	/* Clean up */
351 	a = b = c = d = e = f = g = h = T1 = 0;
352 }
353 
354 #else /* SHA2_UNROLL_TRANSFORM */
355 
356 void
SHA256Transform(u_int32_t * state,const u_int8_t * data)357 SHA256Transform(u_int32_t *state, const u_int8_t *data)
358 {
359 	u_int32_t	a, b, c, d, e, f, g, h, s0, s1;
360 	u_int32_t	T1, T2, W256[16];
361 	int		j;
362 
363 	/* Initialize registers with the prev. intermediate value */
364 	a = state[0];
365 	b = state[1];
366 	c = state[2];
367 	d = state[3];
368 	e = state[4];
369 	f = state[5];
370 	g = state[6];
371 	h = state[7];
372 
373 	j = 0;
374 	do {
375 		W256[j] = (u_int32_t)data[3] | ((u_int32_t)data[2] << 8) |
376 		    ((u_int32_t)data[1] << 16) | ((u_int32_t)data[0] << 24);
377 		data += 4;
378 		/* Apply the SHA-256 compression function to update a..h */
379 		T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j];
380 		T2 = Sigma0_256(a) + Maj(a, b, c);
381 		h = g;
382 		g = f;
383 		f = e;
384 		e = d + T1;
385 		d = c;
386 		c = b;
387 		b = a;
388 		a = T1 + T2;
389 
390 		j++;
391 	} while (j < 16);
392 
393 	do {
394 		/* Part of the message block expansion: */
395 		s0 = W256[(j+1)&0x0f];
396 		s0 = sigma0_256(s0);
397 		s1 = W256[(j+14)&0x0f];
398 		s1 = sigma1_256(s1);
399 
400 		/* Apply the SHA-256 compression function to update a..h */
401 		T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] +
402 		     (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0);
403 		T2 = Sigma0_256(a) + Maj(a, b, c);
404 		h = g;
405 		g = f;
406 		f = e;
407 		e = d + T1;
408 		d = c;
409 		c = b;
410 		b = a;
411 		a = T1 + T2;
412 
413 		j++;
414 	} while (j < 64);
415 
416 	/* Compute the current intermediate hash value */
417 	state[0] += a;
418 	state[1] += b;
419 	state[2] += c;
420 	state[3] += d;
421 	state[4] += e;
422 	state[5] += f;
423 	state[6] += g;
424 	state[7] += h;
425 
426 	/* Clean up */
427 	a = b = c = d = e = f = g = h = T1 = T2 = 0;
428 }
429 
430 #endif /* SHA2_UNROLL_TRANSFORM */
431 
432 void
SHA256Update(SHA2_CTX * context,const void * dataptr,size_t len)433 SHA256Update(SHA2_CTX *context, const void *dataptr, size_t len)
434 {
435 	const u_int8_t *data = dataptr;
436 	size_t	freespace, usedspace;
437 
438 	/* Calling with no data is valid (we do nothing) */
439 	if (len == 0)
440 		return;
441 
442 	usedspace = (context->bitcount[0] >> 3) % SHA256_BLOCK_LENGTH;
443 	if (usedspace > 0) {
444 		/* Calculate how much free space is available in the buffer */
445 		freespace = SHA256_BLOCK_LENGTH - usedspace;
446 
447 		if (len >= freespace) {
448 			/* Fill the buffer completely and process it */
449 			memcpy(&context->buffer[usedspace], data, freespace);
450 			context->bitcount[0] += freespace << 3;
451 			len -= freespace;
452 			data += freespace;
453 			SHA256Transform(context->state.st32, context->buffer);
454 		} else {
455 			/* The buffer is not yet full */
456 			memcpy(&context->buffer[usedspace], data, len);
457 			context->bitcount[0] += len << 3;
458 			/* Clean up: */
459 			usedspace = freespace = 0;
460 			return;
461 		}
462 	}
463 	while (len >= SHA256_BLOCK_LENGTH) {
464 		/* Process as many complete blocks as we can */
465 		SHA256Transform(context->state.st32, data);
466 		context->bitcount[0] += SHA256_BLOCK_LENGTH << 3;
467 		len -= SHA256_BLOCK_LENGTH;
468 		data += SHA256_BLOCK_LENGTH;
469 	}
470 	if (len > 0) {
471 		/* There's left-overs, so save 'em */
472 		memcpy(context->buffer, data, len);
473 		context->bitcount[0] += len << 3;
474 	}
475 	/* Clean up: */
476 	usedspace = freespace = 0;
477 }
478 
479 void
SHA256Final(u_int8_t digest[],SHA2_CTX * context)480 SHA256Final(u_int8_t digest[], SHA2_CTX *context)
481 {
482 	unsigned int	usedspace;
483 
484 	usedspace = (context->bitcount[0] >> 3) % SHA256_BLOCK_LENGTH;
485 #if BYTE_ORDER == LITTLE_ENDIAN
486 	/* Convert FROM host byte order */
487 	context->bitcount[0] = htobe64(context->bitcount[0]);
488 #endif
489 	if (usedspace > 0) {
490 		/* Begin padding with a 1 bit: */
491 		context->buffer[usedspace++] = 0x80;
492 
493 		if (usedspace <= SHA256_SHORT_BLOCK_LENGTH) {
494 			/* Set-up for the last transform: */
495 			memset(&context->buffer[usedspace], 0,
496 			    SHA256_SHORT_BLOCK_LENGTH - usedspace);
497 		} else {
498 			if (usedspace < SHA256_BLOCK_LENGTH) {
499 				memset(&context->buffer[usedspace], 0,
500 				    SHA256_BLOCK_LENGTH - usedspace);
501 			}
502 			/* Do second-to-last transform: */
503 			SHA256Transform(context->state.st32, context->buffer);
504 
505 			/* And set-up for the last transform: */
506 			memset(context->buffer, 0,
507 			    SHA256_SHORT_BLOCK_LENGTH);
508 		}
509 	} else {
510 		/* Set-up for the last transform: */
511 		memset(context->buffer, 0, SHA256_SHORT_BLOCK_LENGTH);
512 
513 		/* Begin padding with a 1 bit: */
514 		*context->buffer = 0x80;
515 	}
516 	/* Set the bit count: */
517 	*(u_int64_t *)&context->buffer[SHA256_SHORT_BLOCK_LENGTH] = context->bitcount[0];
518 
519 	/* Final transform: */
520 	SHA256Transform(context->state.st32, context->buffer);
521 
522 #if BYTE_ORDER == LITTLE_ENDIAN
523 	{
524 		/* Convert TO host byte order */
525 		int	j;
526 		for (j = 0; j < 8; j++) {
527 			context->state.st32[j] = be32toh(context->state.st32[j]);
528 		}
529 	}
530 #endif
531 	memcpy(digest, context->state.st32, SHA256_DIGEST_LENGTH);
532 	/* Clean up state data: */
533 	explicit_bzero(context, sizeof(*context));
534 	usedspace = 0;
535 }
536 
537 
538 /*** SHA-512: *********************************************************/
539 void
SHA512Init(SHA2_CTX * context)540 SHA512Init(SHA2_CTX *context)
541 {
542 	memcpy(context->state.st64, sha512_initial_hash_value,
543 	    SHA512_DIGEST_LENGTH);
544 	memset(context->buffer, 0, SHA512_BLOCK_LENGTH);
545 	context->bitcount[0] = context->bitcount[1] =  0;
546 }
547 
548 #ifdef SHA2_UNROLL_TRANSFORM
549 
550 /* Unrolled SHA-512 round macros: */
551 
552 #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) do {				    \
553 	W512[j] = (u_int64_t)data[7] | ((u_int64_t)data[6] << 8) |	    \
554 	    ((u_int64_t)data[5] << 16) | ((u_int64_t)data[4] << 24) |	    \
555 	    ((u_int64_t)data[3] << 32) | ((u_int64_t)data[2] << 40) |	    \
556 	    ((u_int64_t)data[1] << 48) | ((u_int64_t)data[0] << 56);	    \
557 	data += 8;							    \
558 	T1 = (h) + Sigma1_512((e)) + Ch((e), (f), (g)) + K512[j] + W512[j]; \
559 	(d) += T1;							    \
560 	(h) = T1 + Sigma0_512((a)) + Maj((a), (b), (c));		    \
561 	j++;								    \
562 } while(0)
563 
564 
565 #define ROUND512(a,b,c,d,e,f,g,h) do {					    \
566 	s0 = W512[(j+1)&0x0f];						    \
567 	s0 = sigma0_512(s0);						    \
568 	s1 = W512[(j+14)&0x0f];						    \
569 	s1 = sigma1_512(s1);						    \
570 	T1 = (h) + Sigma1_512((e)) + Ch((e), (f), (g)) + K512[j] +	    \
571              (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);		    \
572 	(d) += T1;							    \
573 	(h) = T1 + Sigma0_512((a)) + Maj((a), (b), (c));		    \
574 	j++;								    \
575 } while(0)
576 
577 void
SHA512Transform(u_int64_t * state,const u_int8_t * data)578 SHA512Transform(u_int64_t *state, const u_int8_t *data)
579 {
580 	u_int64_t	a, b, c, d, e, f, g, h, s0, s1;
581 	u_int64_t	T1, W512[16];
582 	int		j;
583 
584 	/* Initialize registers with the prev. intermediate value */
585 	a = state[0];
586 	b = state[1];
587 	c = state[2];
588 	d = state[3];
589 	e = state[4];
590 	f = state[5];
591 	g = state[6];
592 	h = state[7];
593 
594 	j = 0;
595 	do {
596 		ROUND512_0_TO_15(a,b,c,d,e,f,g,h);
597 		ROUND512_0_TO_15(h,a,b,c,d,e,f,g);
598 		ROUND512_0_TO_15(g,h,a,b,c,d,e,f);
599 		ROUND512_0_TO_15(f,g,h,a,b,c,d,e);
600 		ROUND512_0_TO_15(e,f,g,h,a,b,c,d);
601 		ROUND512_0_TO_15(d,e,f,g,h,a,b,c);
602 		ROUND512_0_TO_15(c,d,e,f,g,h,a,b);
603 		ROUND512_0_TO_15(b,c,d,e,f,g,h,a);
604 	} while (j < 16);
605 
606 	/* Now for the remaining rounds up to 79: */
607 	do {
608 		ROUND512(a,b,c,d,e,f,g,h);
609 		ROUND512(h,a,b,c,d,e,f,g);
610 		ROUND512(g,h,a,b,c,d,e,f);
611 		ROUND512(f,g,h,a,b,c,d,e);
612 		ROUND512(e,f,g,h,a,b,c,d);
613 		ROUND512(d,e,f,g,h,a,b,c);
614 		ROUND512(c,d,e,f,g,h,a,b);
615 		ROUND512(b,c,d,e,f,g,h,a);
616 	} while (j < 80);
617 
618 	/* Compute the current intermediate hash value */
619 	state[0] += a;
620 	state[1] += b;
621 	state[2] += c;
622 	state[3] += d;
623 	state[4] += e;
624 	state[5] += f;
625 	state[6] += g;
626 	state[7] += h;
627 
628 	/* Clean up */
629 	a = b = c = d = e = f = g = h = T1 = 0;
630 }
631 
632 #else /* SHA2_UNROLL_TRANSFORM */
633 
634 void
SHA512Transform(u_int64_t * state,const u_int8_t * data)635 SHA512Transform(u_int64_t *state, const u_int8_t *data)
636 {
637 	u_int64_t	a, b, c, d, e, f, g, h, s0, s1;
638 	u_int64_t	T1, T2, W512[16];
639 	int		j;
640 
641 	/* Initialize registers with the prev. intermediate value */
642 	a = state[0];
643 	b = state[1];
644 	c = state[2];
645 	d = state[3];
646 	e = state[4];
647 	f = state[5];
648 	g = state[6];
649 	h = state[7];
650 
651 	j = 0;
652 	do {
653 		W512[j] = (u_int64_t)data[7] | ((u_int64_t)data[6] << 8) |
654 		    ((u_int64_t)data[5] << 16) | ((u_int64_t)data[4] << 24) |
655 		    ((u_int64_t)data[3] << 32) | ((u_int64_t)data[2] << 40) |
656 		    ((u_int64_t)data[1] << 48) | ((u_int64_t)data[0] << 56);
657 		data += 8;
658 		/* Apply the SHA-512 compression function to update a..h */
659 		T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
660 		T2 = Sigma0_512(a) + Maj(a, b, c);
661 		h = g;
662 		g = f;
663 		f = e;
664 		e = d + T1;
665 		d = c;
666 		c = b;
667 		b = a;
668 		a = T1 + T2;
669 
670 		j++;
671 	} while (j < 16);
672 
673 	do {
674 		/* Part of the message block expansion: */
675 		s0 = W512[(j+1)&0x0f];
676 		s0 = sigma0_512(s0);
677 		s1 = W512[(j+14)&0x0f];
678 		s1 =  sigma1_512(s1);
679 
680 		/* Apply the SHA-512 compression function to update a..h */
681 		T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
682 		     (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);
683 		T2 = Sigma0_512(a) + Maj(a, b, c);
684 		h = g;
685 		g = f;
686 		f = e;
687 		e = d + T1;
688 		d = c;
689 		c = b;
690 		b = a;
691 		a = T1 + T2;
692 
693 		j++;
694 	} while (j < 80);
695 
696 	/* Compute the current intermediate hash value */
697 	state[0] += a;
698 	state[1] += b;
699 	state[2] += c;
700 	state[3] += d;
701 	state[4] += e;
702 	state[5] += f;
703 	state[6] += g;
704 	state[7] += h;
705 
706 	/* Clean up */
707 	a = b = c = d = e = f = g = h = T1 = T2 = 0;
708 }
709 
710 #endif /* SHA2_UNROLL_TRANSFORM */
711 
712 void
SHA512Update(SHA2_CTX * context,const void * dataptr,size_t len)713 SHA512Update(SHA2_CTX *context, const void *dataptr, size_t len)
714 {
715 	const uint8_t *data = dataptr;
716 	size_t	freespace, usedspace;
717 
718 	/* Calling with no data is valid (we do nothing) */
719 	if (len == 0)
720 		return;
721 
722 	usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
723 	if (usedspace > 0) {
724 		/* Calculate how much free space is available in the buffer */
725 		freespace = SHA512_BLOCK_LENGTH - usedspace;
726 
727 		if (len >= freespace) {
728 			/* Fill the buffer completely and process it */
729 			memcpy(&context->buffer[usedspace], data, freespace);
730 			ADDINC128(context->bitcount, freespace << 3);
731 			len -= freespace;
732 			data += freespace;
733 			SHA512Transform(context->state.st64, context->buffer);
734 		} else {
735 			/* The buffer is not yet full */
736 			memcpy(&context->buffer[usedspace], data, len);
737 			ADDINC128(context->bitcount, len << 3);
738 			/* Clean up: */
739 			usedspace = freespace = 0;
740 			return;
741 		}
742 	}
743 	while (len >= SHA512_BLOCK_LENGTH) {
744 		/* Process as many complete blocks as we can */
745 		SHA512Transform(context->state.st64, data);
746 		ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3);
747 		len -= SHA512_BLOCK_LENGTH;
748 		data += SHA512_BLOCK_LENGTH;
749 	}
750 	if (len > 0) {
751 		/* There's left-overs, so save 'em */
752 		memcpy(context->buffer, data, len);
753 		ADDINC128(context->bitcount, len << 3);
754 	}
755 	/* Clean up: */
756 	usedspace = freespace = 0;
757 }
758 
759 void
SHA512Last(SHA2_CTX * context)760 SHA512Last(SHA2_CTX *context)
761 {
762 	unsigned int	usedspace;
763 
764 	usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
765 #if BYTE_ORDER == LITTLE_ENDIAN
766 	/* Convert FROM host byte order */
767 	context->bitcount[0] = htobe64(context->bitcount[0]);
768 	context->bitcount[1] = htobe64(context->bitcount[1]);
769 #endif
770 	if (usedspace > 0) {
771 		/* Begin padding with a 1 bit: */
772 		context->buffer[usedspace++] = 0x80;
773 
774 		if (usedspace <= SHA512_SHORT_BLOCK_LENGTH) {
775 			/* Set-up for the last transform: */
776 			memset(&context->buffer[usedspace], 0,
777 			    SHA512_SHORT_BLOCK_LENGTH - usedspace);
778 		} else {
779 			if (usedspace < SHA512_BLOCK_LENGTH) {
780 				memset(&context->buffer[usedspace], 0,
781 				    SHA512_BLOCK_LENGTH - usedspace);
782 			}
783 			/* Do second-to-last transform: */
784 			SHA512Transform(context->state.st64, context->buffer);
785 
786 			/* And set-up for the last transform: */
787 			memset(context->buffer, 0, SHA512_BLOCK_LENGTH - 2);
788 		}
789 	} else {
790 		/* Prepare for final transform: */
791 		memset(context->buffer, 0, SHA512_SHORT_BLOCK_LENGTH);
792 
793 		/* Begin padding with a 1 bit: */
794 		*context->buffer = 0x80;
795 	}
796 	/* Store the length of input data (in bits): */
797 	*(u_int64_t *)&context->buffer[SHA512_SHORT_BLOCK_LENGTH] = context->bitcount[1];
798 	*(u_int64_t *)&context->buffer[SHA512_SHORT_BLOCK_LENGTH+8] = context->bitcount[0];
799 
800 	/* Final transform: */
801 	SHA512Transform(context->state.st64, context->buffer);
802 }
803 
804 void
SHA512Final(u_int8_t digest[],SHA2_CTX * context)805 SHA512Final(u_int8_t digest[], SHA2_CTX *context)
806 {
807 
808 	SHA512Last(context);
809 
810 	/* Save the hash data for output: */
811 #if BYTE_ORDER == LITTLE_ENDIAN
812 	{
813 		/* Convert TO host byte order */
814 		int	j;
815 		for (j = 0; j < 8; j++) {
816 			context->state.st64[j] = be64toh(context->state.st64[j]);
817 		}
818 	}
819 #endif
820 	memcpy(digest, context->state.st64, SHA512_DIGEST_LENGTH);
821 
822 	/* Zero out state data */
823 	explicit_bzero(context, sizeof(*context));
824 }
825 
826 
827 /*** SHA-384: *********************************************************/
828 void
SHA384Init(SHA2_CTX * context)829 SHA384Init(SHA2_CTX *context)
830 {
831 	memcpy(context->state.st64, sha384_initial_hash_value,
832 	    SHA512_DIGEST_LENGTH);
833 	memset(context->buffer, 0, SHA384_BLOCK_LENGTH);
834 	context->bitcount[0] = context->bitcount[1] = 0;
835 }
836 
837 void
SHA384Update(SHA2_CTX * context,const void * data,size_t len)838 SHA384Update(SHA2_CTX *context, const void *data, size_t len)
839 {
840 	SHA512Update(context, data, len);
841 }
842 
843 void
SHA384Final(u_int8_t digest[],SHA2_CTX * context)844 SHA384Final(u_int8_t digest[], SHA2_CTX *context)
845 {
846 
847 	SHA512Last(context);
848 
849 	/* Save the hash data for output: */
850 #if BYTE_ORDER == LITTLE_ENDIAN
851 	{
852 		/* Convert TO host byte order */
853 		int	j;
854 		for (j = 0; j < 6; j++) {
855 			context->state.st64[j] = be64toh(context->state.st64[j]);
856 		}
857 	}
858 #endif
859 	memcpy(digest, context->state.st64, SHA384_DIGEST_LENGTH);
860 	/* Zero out state data */
861 	explicit_bzero(context, sizeof(*context));
862 }
863