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