1238104Sdes/*
2238104Sdes * FILE:	sha2.c
3238104Sdes * AUTHOR:	Aaron D. Gifford - http://www.aarongifford.com/
4238104Sdes *
5238104Sdes * Copyright (c) 2000-2001, Aaron D. Gifford
6238104Sdes * All rights reserved.
7238104Sdes *
8238104Sdes * Modified by Jelte Jansen to fit in ldns, and not clash with any
9238104Sdes * system-defined SHA code.
10238104Sdes * Changes:
11238104Sdes * - Renamed (external) functions and constants to fit ldns style
12238104Sdes * - Removed _End and _Data functions
13238104Sdes * - Added ldns_shaX(data, len, digest) convenience functions
14238104Sdes * - Removed prototypes of _Transform functions and made those static
15238104Sdes *
16238104Sdes * Redistribution and use in source and binary forms, with or without
17238104Sdes * modification, are permitted provided that the following conditions
18238104Sdes * are met:
19238104Sdes * 1. Redistributions of source code must retain the above copyright
20238104Sdes *    notice, this list of conditions and the following disclaimer.
21238104Sdes * 2. Redistributions in binary form must reproduce the above copyright
22238104Sdes *    notice, this list of conditions and the following disclaimer in the
23238104Sdes *    documentation and/or other materials provided with the distribution.
24238104Sdes * 3. Neither the name of the copyright holder nor the names of contributors
25238104Sdes *    may be used to endorse or promote products derived from this software
26238104Sdes *    without specific prior written permission.
27238104Sdes *
28238104Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
29238104Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30238104Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31238104Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
32238104Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33238104Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34238104Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35238104Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36238104Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37238104Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38238104Sdes * SUCH DAMAGE.
39238104Sdes *
40238104Sdes * $Id: sha2.c,v 1.1 2001/11/08 00:01:51 adg Exp adg $
41238104Sdes */
42238104Sdes
43238104Sdes#include <ldns/config.h>
44238104Sdes#include <string.h>	/* memcpy()/memset() or bcopy()/bzero() */
45238104Sdes#include <assert.h>	/* assert() */
46238104Sdes#include <ldns/sha2.h>
47238104Sdes
48238104Sdes/*
49238104Sdes * ASSERT NOTE:
50238104Sdes * Some sanity checking code is included using assert().  On my FreeBSD
51238104Sdes * system, this additional code can be removed by compiling with NDEBUG
52238104Sdes * defined.  Check your own systems manpage on assert() to see how to
53238104Sdes * compile WITHOUT the sanity checking code on your system.
54238104Sdes *
55238104Sdes * UNROLLED TRANSFORM LOOP NOTE:
56238104Sdes * You can define SHA2_UNROLL_TRANSFORM to use the unrolled transform
57238104Sdes * loop version for the hash transform rounds (defined using macros
58238104Sdes * later in this file).  Either define on the command line, for example:
59238104Sdes *
60238104Sdes *   cc -DSHA2_UNROLL_TRANSFORM -o sha2 sha2.c sha2prog.c
61238104Sdes *
62238104Sdes * or define below:
63238104Sdes *
64238104Sdes *   #define SHA2_UNROLL_TRANSFORM
65238104Sdes *
66238104Sdes */
67238104Sdes
68238104Sdes
69238104Sdes/*** SHA-256/384/512 Machine Architecture Definitions *****************/
70238104Sdes/*
71238104Sdes * BYTE_ORDER NOTE:
72238104Sdes *
73238104Sdes * Please make sure that your system defines BYTE_ORDER.  If your
74238104Sdes * architecture is little-endian, make sure it also defines
75238104Sdes * LITTLE_ENDIAN and that the two (BYTE_ORDER and LITTLE_ENDIAN) are
76238104Sdes * equivilent.
77238104Sdes *
78238104Sdes * If your system does not define the above, then you can do so by
79238104Sdes * hand like this:
80238104Sdes *
81238104Sdes *   #define LITTLE_ENDIAN 1234
82238104Sdes *   #define BIG_ENDIAN    4321
83238104Sdes *
84238104Sdes * And for little-endian machines, add:
85238104Sdes *
86238104Sdes *   #define BYTE_ORDER LITTLE_ENDIAN
87238104Sdes *
88238104Sdes * Or for big-endian machines:
89238104Sdes *
90238104Sdes *   #define BYTE_ORDER BIG_ENDIAN
91238104Sdes *
92238104Sdes * The FreeBSD machine this was written on defines BYTE_ORDER
93238104Sdes * appropriately by including <sys/types.h> (which in turn includes
94238104Sdes * <machine/endian.h> where the appropriate definitions are actually
95238104Sdes * made).
96238104Sdes */
97238104Sdes#if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN)
98238104Sdes#error Define BYTE_ORDER to be equal to either LITTLE_ENDIAN or BIG_ENDIAN
99238104Sdes#endif
100238104Sdes
101238104Sdestypedef uint8_t  sha2_byte;	/* Exactly 1 byte */
102238104Sdestypedef uint32_t sha2_word32;	/* Exactly 4 bytes */
103238104Sdes#ifdef S_SPLINT_S
104238104Sdestypedef unsigned long long sha2_word64; /* lint 8 bytes */
105238104Sdes#else
106238104Sdestypedef uint64_t sha2_word64;	/* Exactly 8 bytes */
107238104Sdes#endif
108238104Sdes
109238104Sdes/*** SHA-256/384/512 Various Length Definitions ***********************/
110238104Sdes/* NOTE: Most of these are in sha2.h */
111238104Sdes#define ldns_sha256_SHORT_BLOCK_LENGTH	(LDNS_SHA256_BLOCK_LENGTH - 8)
112238104Sdes#define ldns_sha384_SHORT_BLOCK_LENGTH	(LDNS_SHA384_BLOCK_LENGTH - 16)
113238104Sdes#define ldns_sha512_SHORT_BLOCK_LENGTH	(LDNS_SHA512_BLOCK_LENGTH - 16)
114238104Sdes
115238104Sdes
116238104Sdes/*** ENDIAN REVERSAL MACROS *******************************************/
117238104Sdes#if BYTE_ORDER == LITTLE_ENDIAN
118238104Sdes#define REVERSE32(w,x)	{ \
119238104Sdes	sha2_word32 tmp = (w); \
120238104Sdes	tmp = (tmp >> 16) | (tmp << 16); \
121238104Sdes	(x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \
122238104Sdes}
123238104Sdes#ifndef S_SPLINT_S
124238104Sdes#define REVERSE64(w,x)	{ \
125238104Sdes	sha2_word64 tmp = (w); \
126238104Sdes	tmp = (tmp >> 32) | (tmp << 32); \
127238104Sdes	tmp = ((tmp & 0xff00ff00ff00ff00ULL) >> 8) | \
128238104Sdes	      ((tmp & 0x00ff00ff00ff00ffULL) << 8); \
129238104Sdes	(x) = ((tmp & 0xffff0000ffff0000ULL) >> 16) | \
130238104Sdes	      ((tmp & 0x0000ffff0000ffffULL) << 16); \
131238104Sdes}
132238104Sdes#else /* splint */
133238104Sdes#define REVERSE64(w,x) /* splint */
134238104Sdes#endif /* splint */
135238104Sdes#endif /* BYTE_ORDER == LITTLE_ENDIAN */
136238104Sdes
137238104Sdes/*
138238104Sdes * Macro for incrementally adding the unsigned 64-bit integer n to the
139238104Sdes * unsigned 128-bit integer (represented using a two-element array of
140238104Sdes * 64-bit words):
141238104Sdes */
142238104Sdes#define ADDINC128(w,n)	{ \
143238104Sdes	(w)[0] += (sha2_word64)(n); \
144238104Sdes	if ((w)[0] < (n)) { \
145238104Sdes		(w)[1]++; \
146238104Sdes	} \
147238104Sdes}
148238104Sdes#ifdef S_SPLINT_S
149238104Sdes#undef ADDINC128
150238104Sdes#define ADDINC128(w,n) /* splint */
151238104Sdes#endif
152238104Sdes
153238104Sdes/*
154238104Sdes * Macros for copying blocks of memory and for zeroing out ranges
155238104Sdes * of memory.  Using these macros makes it easy to switch from
156238104Sdes * using memset()/memcpy() and using bzero()/bcopy().
157238104Sdes *
158238104Sdes * Please define either SHA2_USE_MEMSET_MEMCPY or define
159238104Sdes * SHA2_USE_BZERO_BCOPY depending on which function set you
160238104Sdes * choose to use:
161238104Sdes */
162238104Sdes#if !defined(SHA2_USE_MEMSET_MEMCPY) && !defined(SHA2_USE_BZERO_BCOPY)
163238104Sdes/* Default to memset()/memcpy() if no option is specified */
164238104Sdes#define	SHA2_USE_MEMSET_MEMCPY	1
165238104Sdes#endif
166238104Sdes#if defined(SHA2_USE_MEMSET_MEMCPY) && defined(SHA2_USE_BZERO_BCOPY)
167238104Sdes/* Abort with an error if BOTH options are defined */
168238104Sdes#error Define either SHA2_USE_MEMSET_MEMCPY or SHA2_USE_BZERO_BCOPY, not both!
169238104Sdes#endif
170238104Sdes
171238104Sdes#ifdef SHA2_USE_MEMSET_MEMCPY
172238104Sdes#define MEMSET_BZERO(p,l)	memset((p), 0, (l))
173238104Sdes#define MEMCPY_BCOPY(d,s,l)	memcpy((d), (s), (l))
174238104Sdes#endif
175238104Sdes#ifdef SHA2_USE_BZERO_BCOPY
176238104Sdes#define MEMSET_BZERO(p,l)	bzero((p), (l))
177238104Sdes#define MEMCPY_BCOPY(d,s,l)	bcopy((s), (d), (l))
178238104Sdes#endif
179238104Sdes
180238104Sdes
181238104Sdes/*** THE SIX LOGICAL FUNCTIONS ****************************************/
182238104Sdes/*
183238104Sdes * Bit shifting and rotation (used by the six SHA-XYZ logical functions:
184238104Sdes *
185238104Sdes *   NOTE:  The naming of R and S appears backwards here (R is a SHIFT and
186238104Sdes *   S is a ROTATION) because the SHA-256/384/512 description document
187238104Sdes *   (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this
188238104Sdes *   same "backwards" definition.
189238104Sdes */
190238104Sdes/* Shift-right (used in SHA-256, SHA-384, and SHA-512): */
191238104Sdes#define R(b,x) 		((x) >> (b))
192238104Sdes/* 32-bit Rotate-right (used in SHA-256): */
193238104Sdes#define S32(b,x)	(((x) >> (b)) | ((x) << (32 - (b))))
194238104Sdes/* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
195238104Sdes#define S64(b,x)	(((x) >> (b)) | ((x) << (64 - (b))))
196238104Sdes
197238104Sdes/* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */
198238104Sdes#define Ch(x,y,z)	(((x) & (y)) ^ ((~(x)) & (z)))
199238104Sdes#define Maj(x,y,z)	(((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
200238104Sdes
201238104Sdes/* Four of six logical functions used in SHA-256: */
202238104Sdes#define Sigma0_256(x)	(S32(2,  (x)) ^ S32(13, (x)) ^ S32(22, (x)))
203238104Sdes#define Sigma1_256(x)	(S32(6,  (x)) ^ S32(11, (x)) ^ S32(25, (x)))
204238104Sdes#define sigma0_256(x)	(S32(7,  (x)) ^ S32(18, (x)) ^ R(3 ,   (x)))
205238104Sdes#define sigma1_256(x)	(S32(17, (x)) ^ S32(19, (x)) ^ R(10,   (x)))
206238104Sdes
207238104Sdes/* Four of six logical functions used in SHA-384 and SHA-512: */
208238104Sdes#define Sigma0_512(x)	(S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))
209238104Sdes#define Sigma1_512(x)	(S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))
210238104Sdes#define sigma0_512(x)	(S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7,   (x)))
211238104Sdes#define sigma1_512(x)	(S64(19, (x)) ^ S64(61, (x)) ^ R( 6,   (x)))
212238104Sdes
213238104Sdes/*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
214238104Sdes/* Hash constant words K for SHA-256: */
215238104Sdesstatic const sha2_word32 K256[64] = {
216238104Sdes	0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
217238104Sdes	0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
218238104Sdes	0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
219238104Sdes	0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,
220238104Sdes	0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
221238104Sdes	0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
222238104Sdes	0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL,
223238104Sdes	0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL,
224238104Sdes	0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,
225238104Sdes	0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
226238104Sdes	0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL,
227238104Sdes	0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
228238104Sdes	0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL,
229238104Sdes	0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,
230238104Sdes	0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
231238104Sdes	0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
232238104Sdes};
233238104Sdes
234238104Sdes/* initial hash value H for SHA-256: */
235238104Sdesstatic const sha2_word32 ldns_sha256_initial_hash_value[8] = {
236238104Sdes	0x6a09e667UL,
237238104Sdes	0xbb67ae85UL,
238238104Sdes	0x3c6ef372UL,
239238104Sdes	0xa54ff53aUL,
240238104Sdes	0x510e527fUL,
241238104Sdes	0x9b05688cUL,
242238104Sdes	0x1f83d9abUL,
243238104Sdes	0x5be0cd19UL
244238104Sdes};
245238104Sdes
246238104Sdes/* Hash constant words K for SHA-384 and SHA-512: */
247238104Sdesstatic const sha2_word64 K512[80] = {
248238104Sdes	0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
249238104Sdes	0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
250238104Sdes	0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
251238104Sdes	0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
252238104Sdes	0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
253238104Sdes	0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
254238104Sdes	0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
255238104Sdes	0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
256238104Sdes	0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
257238104Sdes	0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
258238104Sdes	0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
259238104Sdes	0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
260238104Sdes	0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
261238104Sdes	0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
262238104Sdes	0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
263238104Sdes	0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
264238104Sdes	0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
265238104Sdes	0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
266238104Sdes	0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
267238104Sdes	0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
268238104Sdes	0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
269238104Sdes	0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
270238104Sdes	0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
271238104Sdes	0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
272238104Sdes	0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
273238104Sdes	0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
274238104Sdes	0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
275238104Sdes	0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
276238104Sdes	0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
277238104Sdes	0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
278238104Sdes	0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
279238104Sdes	0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
280238104Sdes	0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
281238104Sdes	0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
282238104Sdes	0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
283238104Sdes	0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
284238104Sdes	0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
285238104Sdes	0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
286238104Sdes	0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
287238104Sdes	0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
288238104Sdes};
289238104Sdes
290238104Sdes/* initial hash value H for SHA-384 */
291238104Sdesstatic const sha2_word64 sha384_initial_hash_value[8] = {
292238104Sdes	0xcbbb9d5dc1059ed8ULL,
293238104Sdes	0x629a292a367cd507ULL,
294238104Sdes	0x9159015a3070dd17ULL,
295238104Sdes	0x152fecd8f70e5939ULL,
296238104Sdes	0x67332667ffc00b31ULL,
297238104Sdes	0x8eb44a8768581511ULL,
298238104Sdes	0xdb0c2e0d64f98fa7ULL,
299238104Sdes	0x47b5481dbefa4fa4ULL
300238104Sdes};
301238104Sdes
302238104Sdes/* initial hash value H for SHA-512 */
303238104Sdesstatic const sha2_word64 sha512_initial_hash_value[8] = {
304238104Sdes	0x6a09e667f3bcc908ULL,
305238104Sdes	0xbb67ae8584caa73bULL,
306238104Sdes	0x3c6ef372fe94f82bULL,
307238104Sdes	0xa54ff53a5f1d36f1ULL,
308238104Sdes	0x510e527fade682d1ULL,
309238104Sdes	0x9b05688c2b3e6c1fULL,
310238104Sdes	0x1f83d9abfb41bd6bULL,
311238104Sdes	0x5be0cd19137e2179ULL
312238104Sdes};
313238104Sdes
314238104Sdes/*** SHA-256: *********************************************************/
315238104Sdesvoid ldns_sha256_init(ldns_sha256_CTX* context) {
316238104Sdes	if (context == (ldns_sha256_CTX*)0) {
317238104Sdes		return;
318238104Sdes	}
319238104Sdes	MEMCPY_BCOPY(context->state, ldns_sha256_initial_hash_value, LDNS_SHA256_DIGEST_LENGTH);
320238104Sdes	MEMSET_BZERO(context->buffer, LDNS_SHA256_BLOCK_LENGTH);
321238104Sdes	context->bitcount = 0;
322238104Sdes}
323238104Sdes
324238104Sdes#ifdef SHA2_UNROLL_TRANSFORM
325238104Sdes
326238104Sdes/* Unrolled SHA-256 round macros: */
327238104Sdes
328238104Sdes#if BYTE_ORDER == LITTLE_ENDIAN
329238104Sdes
330238104Sdes#define ROUND256_0_TO_15(a,b,c,d,e,f,g,h)	\
331238104Sdes	REVERSE32(*data++, W256[j]); \
332238104Sdes	T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
333238104Sdes             K256[j] + W256[j]; \
334238104Sdes	(d) += T1; \
335238104Sdes	(h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
336238104Sdes	j++
337238104Sdes
338238104Sdes
339238104Sdes#else /* BYTE_ORDER == LITTLE_ENDIAN */
340238104Sdes
341238104Sdes#define ROUND256_0_TO_15(a,b,c,d,e,f,g,h)	\
342238104Sdes	T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
343238104Sdes	     K256[j] + (W256[j] = *data++); \
344238104Sdes	(d) += T1; \
345238104Sdes	(h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
346238104Sdes	j++
347238104Sdes
348238104Sdes#endif /* BYTE_ORDER == LITTLE_ENDIAN */
349238104Sdes
350238104Sdes#define ROUND256(a,b,c,d,e,f,g,h)	\
351238104Sdes	s0 = W256[(j+1)&0x0f]; \
352238104Sdes	s0 = sigma0_256(s0); \
353238104Sdes	s1 = W256[(j+14)&0x0f]; \
354238104Sdes	s1 = sigma1_256(s1); \
355238104Sdes	T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + K256[j] + \
356238104Sdes	     (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0); \
357238104Sdes	(d) += T1; \
358238104Sdes	(h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
359238104Sdes	j++
360238104Sdes
361238104Sdesstatic void ldns_sha256_Transform(ldns_sha256_CTX* context,
362238104Sdes                                  const sha2_word32* data) {
363238104Sdes	sha2_word32	a, b, c, d, e, f, g, h, s0, s1;
364238104Sdes	sha2_word32	T1, *W256;
365238104Sdes	int		j;
366238104Sdes
367238104Sdes	W256 = (sha2_word32*)context->buffer;
368238104Sdes
369238104Sdes	/* initialize registers with the prev. intermediate value */
370238104Sdes	a = context->state[0];
371238104Sdes	b = context->state[1];
372238104Sdes	c = context->state[2];
373238104Sdes	d = context->state[3];
374238104Sdes	e = context->state[4];
375238104Sdes	f = context->state[5];
376238104Sdes	g = context->state[6];
377238104Sdes	h = context->state[7];
378238104Sdes
379238104Sdes	j = 0;
380238104Sdes	do {
381238104Sdes		/* Rounds 0 to 15 (unrolled): */
382238104Sdes		ROUND256_0_TO_15(a,b,c,d,e,f,g,h);
383238104Sdes		ROUND256_0_TO_15(h,a,b,c,d,e,f,g);
384238104Sdes		ROUND256_0_TO_15(g,h,a,b,c,d,e,f);
385238104Sdes		ROUND256_0_TO_15(f,g,h,a,b,c,d,e);
386238104Sdes		ROUND256_0_TO_15(e,f,g,h,a,b,c,d);
387238104Sdes		ROUND256_0_TO_15(d,e,f,g,h,a,b,c);
388238104Sdes		ROUND256_0_TO_15(c,d,e,f,g,h,a,b);
389238104Sdes		ROUND256_0_TO_15(b,c,d,e,f,g,h,a);
390238104Sdes	} while (j < 16);
391238104Sdes
392238104Sdes	/* Now for the remaining rounds to 64: */
393238104Sdes	do {
394238104Sdes		ROUND256(a,b,c,d,e,f,g,h);
395238104Sdes		ROUND256(h,a,b,c,d,e,f,g);
396238104Sdes		ROUND256(g,h,a,b,c,d,e,f);
397238104Sdes		ROUND256(f,g,h,a,b,c,d,e);
398238104Sdes		ROUND256(e,f,g,h,a,b,c,d);
399238104Sdes		ROUND256(d,e,f,g,h,a,b,c);
400238104Sdes		ROUND256(c,d,e,f,g,h,a,b);
401238104Sdes		ROUND256(b,c,d,e,f,g,h,a);
402238104Sdes	} while (j < 64);
403238104Sdes
404238104Sdes	/* Compute the current intermediate hash value */
405238104Sdes	context->state[0] += a;
406238104Sdes	context->state[1] += b;
407238104Sdes	context->state[2] += c;
408238104Sdes	context->state[3] += d;
409238104Sdes	context->state[4] += e;
410238104Sdes	context->state[5] += f;
411238104Sdes	context->state[6] += g;
412238104Sdes	context->state[7] += h;
413238104Sdes
414238104Sdes	/* Clean up */
415238104Sdes	a = b = c = d = e = f = g = h = T1 = 0;
416238104Sdes}
417238104Sdes
418238104Sdes#else /* SHA2_UNROLL_TRANSFORM */
419238104Sdes
420238104Sdesstatic void ldns_sha256_Transform(ldns_sha256_CTX* context,
421238104Sdes                                  const sha2_word32* data) {
422238104Sdes	sha2_word32	a, b, c, d, e, f, g, h, s0, s1;
423238104Sdes	sha2_word32	T1, T2, *W256;
424238104Sdes	int		j;
425238104Sdes
426238104Sdes	W256 = (sha2_word32*)context->buffer;
427238104Sdes
428238104Sdes	/* initialize registers with the prev. intermediate value */
429238104Sdes	a = context->state[0];
430238104Sdes	b = context->state[1];
431238104Sdes	c = context->state[2];
432238104Sdes	d = context->state[3];
433238104Sdes	e = context->state[4];
434238104Sdes	f = context->state[5];
435238104Sdes	g = context->state[6];
436238104Sdes	h = context->state[7];
437238104Sdes
438238104Sdes	j = 0;
439238104Sdes	do {
440238104Sdes#if BYTE_ORDER == LITTLE_ENDIAN
441238104Sdes		/* Copy data while converting to host byte order */
442238104Sdes		REVERSE32(*data++,W256[j]);
443238104Sdes		/* Apply the SHA-256 compression function to update a..h */
444238104Sdes		T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j];
445238104Sdes#else /* BYTE_ORDER == LITTLE_ENDIAN */
446238104Sdes		/* Apply the SHA-256 compression function to update a..h with copy */
447238104Sdes		T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + (W256[j] = *data++);
448238104Sdes#endif /* BYTE_ORDER == LITTLE_ENDIAN */
449238104Sdes		T2 = Sigma0_256(a) + Maj(a, b, c);
450238104Sdes		h = g;
451238104Sdes		g = f;
452238104Sdes		f = e;
453238104Sdes		e = d + T1;
454238104Sdes		d = c;
455238104Sdes		c = b;
456238104Sdes		b = a;
457238104Sdes		a = T1 + T2;
458238104Sdes
459238104Sdes		j++;
460238104Sdes	} while (j < 16);
461238104Sdes
462238104Sdes	do {
463238104Sdes		/* Part of the message block expansion: */
464238104Sdes		s0 = W256[(j+1)&0x0f];
465238104Sdes		s0 = sigma0_256(s0);
466238104Sdes		s1 = W256[(j+14)&0x0f];
467238104Sdes		s1 = sigma1_256(s1);
468238104Sdes
469238104Sdes		/* Apply the SHA-256 compression function to update a..h */
470238104Sdes		T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] +
471238104Sdes		     (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0);
472238104Sdes		T2 = Sigma0_256(a) + Maj(a, b, c);
473238104Sdes		h = g;
474238104Sdes		g = f;
475238104Sdes		f = e;
476238104Sdes		e = d + T1;
477238104Sdes		d = c;
478238104Sdes		c = b;
479238104Sdes		b = a;
480238104Sdes		a = T1 + T2;
481238104Sdes
482238104Sdes		j++;
483238104Sdes	} while (j < 64);
484238104Sdes
485238104Sdes	/* Compute the current intermediate hash value */
486238104Sdes	context->state[0] += a;
487238104Sdes	context->state[1] += b;
488238104Sdes	context->state[2] += c;
489238104Sdes	context->state[3] += d;
490238104Sdes	context->state[4] += e;
491238104Sdes	context->state[5] += f;
492238104Sdes	context->state[6] += g;
493238104Sdes	context->state[7] += h;
494238104Sdes
495238104Sdes	/* Clean up */
496238104Sdes	a = b = c = d = e = f = g = h = T1 = T2 = 0;
497238104Sdes}
498238104Sdes
499238104Sdes#endif /* SHA2_UNROLL_TRANSFORM */
500238104Sdes
501238104Sdesvoid ldns_sha256_update(ldns_sha256_CTX* context, const sha2_byte *data, size_t len) {
502238104Sdes	size_t freespace, usedspace;
503238104Sdes
504238104Sdes	if (len == 0) {
505238104Sdes		/* Calling with no data is valid - we do nothing */
506238104Sdes		return;
507238104Sdes	}
508238104Sdes
509238104Sdes	/* Sanity check: */
510238104Sdes	assert(context != (ldns_sha256_CTX*)0 && data != (sha2_byte*)0);
511238104Sdes
512238104Sdes	usedspace = (context->bitcount >> 3) % LDNS_SHA256_BLOCK_LENGTH;
513238104Sdes	if (usedspace > 0) {
514238104Sdes		/* Calculate how much free space is available in the buffer */
515238104Sdes		freespace = LDNS_SHA256_BLOCK_LENGTH - usedspace;
516238104Sdes
517238104Sdes		if (len >= freespace) {
518238104Sdes			/* Fill the buffer completely and process it */
519238104Sdes			MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace);
520238104Sdes			context->bitcount += freespace << 3;
521238104Sdes			len -= freespace;
522238104Sdes			data += freespace;
523238104Sdes			ldns_sha256_Transform(context, (sha2_word32*)context->buffer);
524238104Sdes		} else {
525238104Sdes			/* The buffer is not yet full */
526238104Sdes			MEMCPY_BCOPY(&context->buffer[usedspace], data, len);
527238104Sdes			context->bitcount += len << 3;
528238104Sdes			/* Clean up: */
529238104Sdes			usedspace = freespace = 0;
530238104Sdes			return;
531238104Sdes		}
532238104Sdes	}
533238104Sdes	while (len >= LDNS_SHA256_BLOCK_LENGTH) {
534238104Sdes		/* Process as many complete blocks as we can */
535238104Sdes		ldns_sha256_Transform(context, (sha2_word32*)data);
536238104Sdes		context->bitcount += LDNS_SHA256_BLOCK_LENGTH << 3;
537238104Sdes		len -= LDNS_SHA256_BLOCK_LENGTH;
538238104Sdes		data += LDNS_SHA256_BLOCK_LENGTH;
539238104Sdes	}
540238104Sdes	if (len > 0) {
541238104Sdes		/* There's left-overs, so save 'em */
542238104Sdes		MEMCPY_BCOPY(context->buffer, data, len);
543238104Sdes		context->bitcount += len << 3;
544238104Sdes	}
545238104Sdes	/* Clean up: */
546238104Sdes	usedspace = freespace = 0;
547238104Sdes}
548238104Sdes
549269257Sdestypedef union _ldns_sha2_buffer_union {
550269257Sdes        uint8_t*  theChars;
551269257Sdes        uint64_t* theLongs;
552269257Sdes} ldns_sha2_buffer_union;
553269257Sdes
554238104Sdesvoid ldns_sha256_final(sha2_byte digest[], ldns_sha256_CTX* context) {
555238104Sdes	sha2_word32	*d = (sha2_word32*)digest;
556238104Sdes	size_t usedspace;
557269257Sdes	ldns_sha2_buffer_union cast_var;
558238104Sdes
559238104Sdes	/* Sanity check: */
560238104Sdes	assert(context != (ldns_sha256_CTX*)0);
561238104Sdes
562238104Sdes	/* If no digest buffer is passed, we don't bother doing this: */
563238104Sdes	if (digest != (sha2_byte*)0) {
564238104Sdes		usedspace = (context->bitcount >> 3) % LDNS_SHA256_BLOCK_LENGTH;
565238104Sdes#if BYTE_ORDER == LITTLE_ENDIAN
566238104Sdes		/* Convert FROM host byte order */
567238104Sdes		REVERSE64(context->bitcount,context->bitcount);
568238104Sdes#endif
569238104Sdes		if (usedspace > 0) {
570238104Sdes			/* Begin padding with a 1 bit: */
571238104Sdes			context->buffer[usedspace++] = 0x80;
572238104Sdes
573238104Sdes			if (usedspace <= ldns_sha256_SHORT_BLOCK_LENGTH) {
574238104Sdes				/* Set-up for the last transform: */
575238104Sdes				MEMSET_BZERO(&context->buffer[usedspace], ldns_sha256_SHORT_BLOCK_LENGTH - usedspace);
576238104Sdes			} else {
577238104Sdes				if (usedspace < LDNS_SHA256_BLOCK_LENGTH) {
578238104Sdes					MEMSET_BZERO(&context->buffer[usedspace], LDNS_SHA256_BLOCK_LENGTH - usedspace);
579238104Sdes				}
580238104Sdes				/* Do second-to-last transform: */
581238104Sdes				ldns_sha256_Transform(context, (sha2_word32*)context->buffer);
582238104Sdes
583238104Sdes				/* And set-up for the last transform: */
584238104Sdes				MEMSET_BZERO(context->buffer, ldns_sha256_SHORT_BLOCK_LENGTH);
585238104Sdes			}
586238104Sdes		} else {
587238104Sdes			/* Set-up for the last transform: */
588238104Sdes			MEMSET_BZERO(context->buffer, ldns_sha256_SHORT_BLOCK_LENGTH);
589238104Sdes
590238104Sdes			/* Begin padding with a 1 bit: */
591238104Sdes			*context->buffer = 0x80;
592238104Sdes		}
593238104Sdes		/* Set the bit count: */
594269257Sdes		cast_var.theChars = context->buffer;
595269257Sdes		cast_var.theLongs[ldns_sha256_SHORT_BLOCK_LENGTH / 8] = context->bitcount;
596238104Sdes
597238104Sdes		/* final transform: */
598238104Sdes		ldns_sha256_Transform(context, (sha2_word32*)context->buffer);
599238104Sdes
600238104Sdes#if BYTE_ORDER == LITTLE_ENDIAN
601238104Sdes		{
602238104Sdes			/* Convert TO host byte order */
603238104Sdes			int	j;
604238104Sdes			for (j = 0; j < 8; j++) {
605238104Sdes				REVERSE32(context->state[j],context->state[j]);
606238104Sdes				*d++ = context->state[j];
607238104Sdes			}
608238104Sdes		}
609238104Sdes#else
610238104Sdes		MEMCPY_BCOPY(d, context->state, LDNS_SHA256_DIGEST_LENGTH);
611238104Sdes#endif
612238104Sdes	}
613238104Sdes
614238104Sdes	/* Clean up state data: */
615238104Sdes	MEMSET_BZERO(context, sizeof(ldns_sha256_CTX));
616238104Sdes	usedspace = 0;
617238104Sdes}
618238104Sdes
619238104Sdesunsigned char *
620238104Sdesldns_sha256(unsigned char *data, unsigned int data_len, unsigned char *digest)
621238104Sdes{
622238104Sdes    ldns_sha256_CTX ctx;
623238104Sdes    ldns_sha256_init(&ctx);
624238104Sdes    ldns_sha256_update(&ctx, data, data_len);
625238104Sdes    ldns_sha256_final(digest, &ctx);
626238104Sdes    return digest;
627238104Sdes}
628238104Sdes
629238104Sdes/*** SHA-512: *********************************************************/
630238104Sdesvoid ldns_sha512_init(ldns_sha512_CTX* context) {
631238104Sdes	if (context == (ldns_sha512_CTX*)0) {
632238104Sdes		return;
633238104Sdes	}
634238104Sdes	MEMCPY_BCOPY(context->state, sha512_initial_hash_value, LDNS_SHA512_DIGEST_LENGTH);
635238104Sdes	MEMSET_BZERO(context->buffer, LDNS_SHA512_BLOCK_LENGTH);
636238104Sdes	context->bitcount[0] = context->bitcount[1] =  0;
637238104Sdes}
638238104Sdes
639238104Sdes#ifdef SHA2_UNROLL_TRANSFORM
640238104Sdes
641238104Sdes/* Unrolled SHA-512 round macros: */
642238104Sdes#if BYTE_ORDER == LITTLE_ENDIAN
643238104Sdes
644238104Sdes#define ROUND512_0_TO_15(a,b,c,d,e,f,g,h)	\
645238104Sdes	REVERSE64(*data++, W512[j]); \
646238104Sdes	T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
647238104Sdes             K512[j] + W512[j]; \
648238104Sdes	(d) += T1, \
649238104Sdes	(h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)), \
650238104Sdes	j++
651238104Sdes
652238104Sdes
653238104Sdes#else /* BYTE_ORDER == LITTLE_ENDIAN */
654238104Sdes
655238104Sdes#define ROUND512_0_TO_15(a,b,c,d,e,f,g,h)	\
656238104Sdes	T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
657238104Sdes             K512[j] + (W512[j] = *data++); \
658238104Sdes	(d) += T1; \
659238104Sdes	(h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
660238104Sdes	j++
661238104Sdes
662238104Sdes#endif /* BYTE_ORDER == LITTLE_ENDIAN */
663238104Sdes
664238104Sdes#define ROUND512(a,b,c,d,e,f,g,h)	\
665238104Sdes	s0 = W512[(j+1)&0x0f]; \
666238104Sdes	s0 = sigma0_512(s0); \
667238104Sdes	s1 = W512[(j+14)&0x0f]; \
668238104Sdes	s1 = sigma1_512(s1); \
669238104Sdes	T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] + \
670238104Sdes             (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); \
671238104Sdes	(d) += T1; \
672238104Sdes	(h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
673238104Sdes	j++
674238104Sdes
675238104Sdesstatic void ldns_sha512_Transform(ldns_sha512_CTX* context,
676238104Sdes                                  const sha2_word64* data) {
677238104Sdes	sha2_word64	a, b, c, d, e, f, g, h, s0, s1;
678238104Sdes	sha2_word64	T1, *W512 = (sha2_word64*)context->buffer;
679238104Sdes	int		j;
680238104Sdes
681238104Sdes	/* initialize registers with the prev. intermediate value */
682238104Sdes	a = context->state[0];
683238104Sdes	b = context->state[1];
684238104Sdes	c = context->state[2];
685238104Sdes	d = context->state[3];
686238104Sdes	e = context->state[4];
687238104Sdes	f = context->state[5];
688238104Sdes	g = context->state[6];
689238104Sdes	h = context->state[7];
690238104Sdes
691238104Sdes	j = 0;
692238104Sdes	do {
693238104Sdes		ROUND512_0_TO_15(a,b,c,d,e,f,g,h);
694238104Sdes		ROUND512_0_TO_15(h,a,b,c,d,e,f,g);
695238104Sdes		ROUND512_0_TO_15(g,h,a,b,c,d,e,f);
696238104Sdes		ROUND512_0_TO_15(f,g,h,a,b,c,d,e);
697238104Sdes		ROUND512_0_TO_15(e,f,g,h,a,b,c,d);
698238104Sdes		ROUND512_0_TO_15(d,e,f,g,h,a,b,c);
699238104Sdes		ROUND512_0_TO_15(c,d,e,f,g,h,a,b);
700238104Sdes		ROUND512_0_TO_15(b,c,d,e,f,g,h,a);
701238104Sdes	} while (j < 16);
702238104Sdes
703238104Sdes	/* Now for the remaining rounds up to 79: */
704238104Sdes	do {
705238104Sdes		ROUND512(a,b,c,d,e,f,g,h);
706238104Sdes		ROUND512(h,a,b,c,d,e,f,g);
707238104Sdes		ROUND512(g,h,a,b,c,d,e,f);
708238104Sdes		ROUND512(f,g,h,a,b,c,d,e);
709238104Sdes		ROUND512(e,f,g,h,a,b,c,d);
710238104Sdes		ROUND512(d,e,f,g,h,a,b,c);
711238104Sdes		ROUND512(c,d,e,f,g,h,a,b);
712238104Sdes		ROUND512(b,c,d,e,f,g,h,a);
713238104Sdes	} while (j < 80);
714238104Sdes
715238104Sdes	/* Compute the current intermediate hash value */
716238104Sdes	context->state[0] += a;
717238104Sdes	context->state[1] += b;
718238104Sdes	context->state[2] += c;
719238104Sdes	context->state[3] += d;
720238104Sdes	context->state[4] += e;
721238104Sdes	context->state[5] += f;
722238104Sdes	context->state[6] += g;
723238104Sdes	context->state[7] += h;
724238104Sdes
725238104Sdes	/* Clean up */
726238104Sdes	a = b = c = d = e = f = g = h = T1 = 0;
727238104Sdes}
728238104Sdes
729238104Sdes#else /* SHA2_UNROLL_TRANSFORM */
730238104Sdes
731238104Sdesstatic void ldns_sha512_Transform(ldns_sha512_CTX* context,
732238104Sdes                                  const sha2_word64* data) {
733238104Sdes	sha2_word64	a, b, c, d, e, f, g, h, s0, s1;
734238104Sdes	sha2_word64	T1, T2, *W512 = (sha2_word64*)context->buffer;
735238104Sdes	int		j;
736238104Sdes
737238104Sdes	/* initialize registers with the prev. intermediate value */
738238104Sdes	a = context->state[0];
739238104Sdes	b = context->state[1];
740238104Sdes	c = context->state[2];
741238104Sdes	d = context->state[3];
742238104Sdes	e = context->state[4];
743238104Sdes	f = context->state[5];
744238104Sdes	g = context->state[6];
745238104Sdes	h = context->state[7];
746238104Sdes
747238104Sdes	j = 0;
748238104Sdes	do {
749238104Sdes#if BYTE_ORDER == LITTLE_ENDIAN
750238104Sdes		/* Convert TO host byte order */
751238104Sdes		REVERSE64(*data++, W512[j]);
752238104Sdes		/* Apply the SHA-512 compression function to update a..h */
753238104Sdes		T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
754238104Sdes#else /* BYTE_ORDER == LITTLE_ENDIAN */
755238104Sdes		/* Apply the SHA-512 compression function to update a..h with copy */
756238104Sdes		T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j] = *data++);
757238104Sdes#endif /* BYTE_ORDER == LITTLE_ENDIAN */
758238104Sdes		T2 = Sigma0_512(a) + Maj(a, b, c);
759238104Sdes		h = g;
760238104Sdes		g = f;
761238104Sdes		f = e;
762238104Sdes		e = d + T1;
763238104Sdes		d = c;
764238104Sdes		c = b;
765238104Sdes		b = a;
766238104Sdes		a = T1 + T2;
767238104Sdes
768238104Sdes		j++;
769238104Sdes	} while (j < 16);
770238104Sdes
771238104Sdes	do {
772238104Sdes		/* Part of the message block expansion: */
773238104Sdes		s0 = W512[(j+1)&0x0f];
774238104Sdes		s0 = sigma0_512(s0);
775238104Sdes		s1 = W512[(j+14)&0x0f];
776238104Sdes		s1 =  sigma1_512(s1);
777238104Sdes
778238104Sdes		/* Apply the SHA-512 compression function to update a..h */
779238104Sdes		T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
780238104Sdes		     (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);
781238104Sdes		T2 = Sigma0_512(a) + Maj(a, b, c);
782238104Sdes		h = g;
783238104Sdes		g = f;
784238104Sdes		f = e;
785238104Sdes		e = d + T1;
786238104Sdes		d = c;
787238104Sdes		c = b;
788238104Sdes		b = a;
789238104Sdes		a = T1 + T2;
790238104Sdes
791238104Sdes		j++;
792238104Sdes	} while (j < 80);
793238104Sdes
794238104Sdes	/* Compute the current intermediate hash value */
795238104Sdes	context->state[0] += a;
796238104Sdes	context->state[1] += b;
797238104Sdes	context->state[2] += c;
798238104Sdes	context->state[3] += d;
799238104Sdes	context->state[4] += e;
800238104Sdes	context->state[5] += f;
801238104Sdes	context->state[6] += g;
802238104Sdes	context->state[7] += h;
803238104Sdes
804238104Sdes	/* Clean up */
805238104Sdes	a = b = c = d = e = f = g = h = T1 = T2 = 0;
806238104Sdes}
807238104Sdes
808238104Sdes#endif /* SHA2_UNROLL_TRANSFORM */
809238104Sdes
810238104Sdesvoid ldns_sha512_update(ldns_sha512_CTX* context, const sha2_byte *data, size_t len) {
811238104Sdes	size_t freespace, usedspace;
812238104Sdes
813238104Sdes	if (len == 0) {
814238104Sdes		/* Calling with no data is valid - we do nothing */
815238104Sdes		return;
816238104Sdes	}
817238104Sdes
818238104Sdes	/* Sanity check: */
819238104Sdes	assert(context != (ldns_sha512_CTX*)0 && data != (sha2_byte*)0);
820238104Sdes
821238104Sdes	usedspace = (context->bitcount[0] >> 3) % LDNS_SHA512_BLOCK_LENGTH;
822238104Sdes	if (usedspace > 0) {
823238104Sdes		/* Calculate how much free space is available in the buffer */
824238104Sdes		freespace = LDNS_SHA512_BLOCK_LENGTH - usedspace;
825238104Sdes
826238104Sdes		if (len >= freespace) {
827238104Sdes			/* Fill the buffer completely and process it */
828238104Sdes			MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace);
829238104Sdes			ADDINC128(context->bitcount, freespace << 3);
830238104Sdes			len -= freespace;
831238104Sdes			data += freespace;
832238104Sdes			ldns_sha512_Transform(context, (sha2_word64*)context->buffer);
833238104Sdes		} else {
834238104Sdes			/* The buffer is not yet full */
835238104Sdes			MEMCPY_BCOPY(&context->buffer[usedspace], data, len);
836238104Sdes			ADDINC128(context->bitcount, len << 3);
837238104Sdes			/* Clean up: */
838238104Sdes			usedspace = freespace = 0;
839238104Sdes			return;
840238104Sdes		}
841238104Sdes	}
842238104Sdes	while (len >= LDNS_SHA512_BLOCK_LENGTH) {
843238104Sdes		/* Process as many complete blocks as we can */
844238104Sdes		ldns_sha512_Transform(context, (sha2_word64*)data);
845238104Sdes		ADDINC128(context->bitcount, LDNS_SHA512_BLOCK_LENGTH << 3);
846238104Sdes		len -= LDNS_SHA512_BLOCK_LENGTH;
847238104Sdes		data += LDNS_SHA512_BLOCK_LENGTH;
848238104Sdes	}
849238104Sdes	if (len > 0) {
850238104Sdes		/* There's left-overs, so save 'em */
851238104Sdes		MEMCPY_BCOPY(context->buffer, data, len);
852238104Sdes		ADDINC128(context->bitcount, len << 3);
853238104Sdes	}
854238104Sdes	/* Clean up: */
855238104Sdes	usedspace = freespace = 0;
856238104Sdes}
857238104Sdes
858238104Sdesstatic void ldns_sha512_Last(ldns_sha512_CTX* context) {
859238104Sdes	size_t usedspace;
860269257Sdes	ldns_sha2_buffer_union cast_var;
861238104Sdes
862238104Sdes	usedspace = (context->bitcount[0] >> 3) % LDNS_SHA512_BLOCK_LENGTH;
863238104Sdes#if BYTE_ORDER == LITTLE_ENDIAN
864238104Sdes	/* Convert FROM host byte order */
865238104Sdes	REVERSE64(context->bitcount[0],context->bitcount[0]);
866238104Sdes	REVERSE64(context->bitcount[1],context->bitcount[1]);
867238104Sdes#endif
868238104Sdes	if (usedspace > 0) {
869238104Sdes		/* Begin padding with a 1 bit: */
870238104Sdes		context->buffer[usedspace++] = 0x80;
871238104Sdes
872238104Sdes		if (usedspace <= ldns_sha512_SHORT_BLOCK_LENGTH) {
873238104Sdes			/* Set-up for the last transform: */
874238104Sdes			MEMSET_BZERO(&context->buffer[usedspace], ldns_sha512_SHORT_BLOCK_LENGTH - usedspace);
875238104Sdes		} else {
876238104Sdes			if (usedspace < LDNS_SHA512_BLOCK_LENGTH) {
877238104Sdes				MEMSET_BZERO(&context->buffer[usedspace], LDNS_SHA512_BLOCK_LENGTH - usedspace);
878238104Sdes			}
879238104Sdes			/* Do second-to-last transform: */
880238104Sdes			ldns_sha512_Transform(context, (sha2_word64*)context->buffer);
881238104Sdes
882238104Sdes			/* And set-up for the last transform: */
883238104Sdes			MEMSET_BZERO(context->buffer, LDNS_SHA512_BLOCK_LENGTH - 2);
884238104Sdes		}
885238104Sdes	} else {
886238104Sdes		/* Prepare for final transform: */
887238104Sdes		MEMSET_BZERO(context->buffer, ldns_sha512_SHORT_BLOCK_LENGTH);
888238104Sdes
889238104Sdes		/* Begin padding with a 1 bit: */
890238104Sdes		*context->buffer = 0x80;
891238104Sdes	}
892238104Sdes	/* Store the length of input data (in bits): */
893269257Sdes	cast_var.theChars = context->buffer;
894269257Sdes	cast_var.theLongs[ldns_sha512_SHORT_BLOCK_LENGTH / 8] = context->bitcount[1];
895269257Sdes	cast_var.theLongs[ldns_sha512_SHORT_BLOCK_LENGTH / 8 + 1] = context->bitcount[0];
896238104Sdes
897238104Sdes	/* final transform: */
898238104Sdes	ldns_sha512_Transform(context, (sha2_word64*)context->buffer);
899238104Sdes}
900238104Sdes
901238104Sdesvoid ldns_sha512_final(sha2_byte digest[], ldns_sha512_CTX* context) {
902238104Sdes	sha2_word64	*d = (sha2_word64*)digest;
903238104Sdes
904238104Sdes	/* Sanity check: */
905238104Sdes	assert(context != (ldns_sha512_CTX*)0);
906238104Sdes
907238104Sdes	/* If no digest buffer is passed, we don't bother doing this: */
908238104Sdes	if (digest != (sha2_byte*)0) {
909238104Sdes		ldns_sha512_Last(context);
910238104Sdes
911238104Sdes		/* Save the hash data for output: */
912238104Sdes#if BYTE_ORDER == LITTLE_ENDIAN
913238104Sdes		{
914238104Sdes			/* Convert TO host byte order */
915238104Sdes			int	j;
916238104Sdes			for (j = 0; j < 8; j++) {
917238104Sdes				REVERSE64(context->state[j],context->state[j]);
918238104Sdes				*d++ = context->state[j];
919238104Sdes			}
920238104Sdes		}
921238104Sdes#else
922238104Sdes		MEMCPY_BCOPY(d, context->state, LDNS_SHA512_DIGEST_LENGTH);
923238104Sdes#endif
924238104Sdes	}
925238104Sdes
926238104Sdes	/* Zero out state data */
927238104Sdes	MEMSET_BZERO(context, sizeof(ldns_sha512_CTX));
928238104Sdes}
929238104Sdes
930238104Sdesunsigned char *
931238104Sdesldns_sha512(unsigned char *data, unsigned int data_len, unsigned char *digest)
932238104Sdes{
933238104Sdes    ldns_sha512_CTX ctx;
934238104Sdes    ldns_sha512_init(&ctx);
935238104Sdes    ldns_sha512_update(&ctx, data, data_len);
936238104Sdes    ldns_sha512_final(digest, &ctx);
937238104Sdes    return digest;
938238104Sdes}
939238104Sdes
940238104Sdes/*** SHA-384: *********************************************************/
941238104Sdesvoid ldns_sha384_init(ldns_sha384_CTX* context) {
942238104Sdes	if (context == (ldns_sha384_CTX*)0) {
943238104Sdes		return;
944238104Sdes	}
945238104Sdes	MEMCPY_BCOPY(context->state, sha384_initial_hash_value, LDNS_SHA512_DIGEST_LENGTH);
946238104Sdes	MEMSET_BZERO(context->buffer, LDNS_SHA384_BLOCK_LENGTH);
947238104Sdes	context->bitcount[0] = context->bitcount[1] = 0;
948238104Sdes}
949238104Sdes
950238104Sdesvoid ldns_sha384_update(ldns_sha384_CTX* context, const sha2_byte* data, size_t len) {
951238104Sdes	ldns_sha512_update((ldns_sha512_CTX*)context, data, len);
952238104Sdes}
953238104Sdes
954238104Sdesvoid ldns_sha384_final(sha2_byte digest[], ldns_sha384_CTX* context) {
955238104Sdes	sha2_word64	*d = (sha2_word64*)digest;
956238104Sdes
957238104Sdes	/* Sanity check: */
958238104Sdes	assert(context != (ldns_sha384_CTX*)0);
959238104Sdes
960238104Sdes	/* If no digest buffer is passed, we don't bother doing this: */
961238104Sdes	if (digest != (sha2_byte*)0) {
962238104Sdes		ldns_sha512_Last((ldns_sha512_CTX*)context);
963238104Sdes
964238104Sdes		/* Save the hash data for output: */
965238104Sdes#if BYTE_ORDER == LITTLE_ENDIAN
966238104Sdes		{
967238104Sdes			/* Convert TO host byte order */
968238104Sdes			int	j;
969238104Sdes			for (j = 0; j < 6; j++) {
970238104Sdes				REVERSE64(context->state[j],context->state[j]);
971238104Sdes				*d++ = context->state[j];
972238104Sdes			}
973238104Sdes		}
974238104Sdes#else
975238104Sdes		MEMCPY_BCOPY(d, context->state, LDNS_SHA384_DIGEST_LENGTH);
976238104Sdes#endif
977238104Sdes	}
978238104Sdes
979238104Sdes	/* Zero out state data */
980238104Sdes	MEMSET_BZERO(context, sizeof(ldns_sha384_CTX));
981238104Sdes}
982238104Sdes
983238104Sdesunsigned char *
984238104Sdesldns_sha384(unsigned char *data, unsigned int data_len, unsigned char *digest)
985238104Sdes{
986238104Sdes    ldns_sha384_CTX ctx;
987238104Sdes    ldns_sha384_init(&ctx);
988238104Sdes    ldns_sha384_update(&ctx, data, data_len);
989238104Sdes    ldns_sha384_final(digest, &ctx);
990238104Sdes    return digest;
991238104Sdes}
992