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