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
549238104Sdesvoid ldns_sha256_final(sha2_byte digest[], ldns_sha256_CTX* context) {
550238104Sdes	sha2_word32	*d = (sha2_word32*)digest;
551238104Sdes	size_t usedspace;
552238104Sdes
553238104Sdes	/* Sanity check: */
554238104Sdes	assert(context != (ldns_sha256_CTX*)0);
555238104Sdes
556238104Sdes	/* If no digest buffer is passed, we don't bother doing this: */
557238104Sdes	if (digest != (sha2_byte*)0) {
558238104Sdes		usedspace = (context->bitcount >> 3) % LDNS_SHA256_BLOCK_LENGTH;
559238104Sdes#if BYTE_ORDER == LITTLE_ENDIAN
560238104Sdes		/* Convert FROM host byte order */
561238104Sdes		REVERSE64(context->bitcount,context->bitcount);
562238104Sdes#endif
563238104Sdes		if (usedspace > 0) {
564238104Sdes			/* Begin padding with a 1 bit: */
565238104Sdes			context->buffer[usedspace++] = 0x80;
566238104Sdes
567238104Sdes			if (usedspace <= ldns_sha256_SHORT_BLOCK_LENGTH) {
568238104Sdes				/* Set-up for the last transform: */
569238104Sdes				MEMSET_BZERO(&context->buffer[usedspace], ldns_sha256_SHORT_BLOCK_LENGTH - usedspace);
570238104Sdes			} else {
571238104Sdes				if (usedspace < LDNS_SHA256_BLOCK_LENGTH) {
572238104Sdes					MEMSET_BZERO(&context->buffer[usedspace], LDNS_SHA256_BLOCK_LENGTH - usedspace);
573238104Sdes				}
574238104Sdes				/* Do second-to-last transform: */
575238104Sdes				ldns_sha256_Transform(context, (sha2_word32*)context->buffer);
576238104Sdes
577238104Sdes				/* And set-up for the last transform: */
578238104Sdes				MEMSET_BZERO(context->buffer, ldns_sha256_SHORT_BLOCK_LENGTH);
579238104Sdes			}
580238104Sdes		} else {
581238104Sdes			/* Set-up for the last transform: */
582238104Sdes			MEMSET_BZERO(context->buffer, ldns_sha256_SHORT_BLOCK_LENGTH);
583238104Sdes
584238104Sdes			/* Begin padding with a 1 bit: */
585238104Sdes			*context->buffer = 0x80;
586238104Sdes		}
587238104Sdes		/* Set the bit count: */
588238104Sdes		*(sha2_word64*)&context->buffer[ldns_sha256_SHORT_BLOCK_LENGTH] = context->bitcount;
589238104Sdes
590238104Sdes		/* final transform: */
591238104Sdes		ldns_sha256_Transform(context, (sha2_word32*)context->buffer);
592238104Sdes
593238104Sdes#if BYTE_ORDER == LITTLE_ENDIAN
594238104Sdes		{
595238104Sdes			/* Convert TO host byte order */
596238104Sdes			int	j;
597238104Sdes			for (j = 0; j < 8; j++) {
598238104Sdes				REVERSE32(context->state[j],context->state[j]);
599238104Sdes				*d++ = context->state[j];
600238104Sdes			}
601238104Sdes		}
602238104Sdes#else
603238104Sdes		MEMCPY_BCOPY(d, context->state, LDNS_SHA256_DIGEST_LENGTH);
604238104Sdes#endif
605238104Sdes	}
606238104Sdes
607238104Sdes	/* Clean up state data: */
608238104Sdes	MEMSET_BZERO(context, sizeof(ldns_sha256_CTX));
609238104Sdes	usedspace = 0;
610238104Sdes}
611238104Sdes
612238104Sdesunsigned char *
613238104Sdesldns_sha256(unsigned char *data, unsigned int data_len, unsigned char *digest)
614238104Sdes{
615238104Sdes    ldns_sha256_CTX ctx;
616238104Sdes    ldns_sha256_init(&ctx);
617238104Sdes    ldns_sha256_update(&ctx, data, data_len);
618238104Sdes    ldns_sha256_final(digest, &ctx);
619238104Sdes    return digest;
620238104Sdes}
621238104Sdes
622238104Sdes/*** SHA-512: *********************************************************/
623238104Sdesvoid ldns_sha512_init(ldns_sha512_CTX* context) {
624238104Sdes	if (context == (ldns_sha512_CTX*)0) {
625238104Sdes		return;
626238104Sdes	}
627238104Sdes	MEMCPY_BCOPY(context->state, sha512_initial_hash_value, LDNS_SHA512_DIGEST_LENGTH);
628238104Sdes	MEMSET_BZERO(context->buffer, LDNS_SHA512_BLOCK_LENGTH);
629238104Sdes	context->bitcount[0] = context->bitcount[1] =  0;
630238104Sdes}
631238104Sdes
632238104Sdes#ifdef SHA2_UNROLL_TRANSFORM
633238104Sdes
634238104Sdes/* Unrolled SHA-512 round macros: */
635238104Sdes#if BYTE_ORDER == LITTLE_ENDIAN
636238104Sdes
637238104Sdes#define ROUND512_0_TO_15(a,b,c,d,e,f,g,h)	\
638238104Sdes	REVERSE64(*data++, W512[j]); \
639238104Sdes	T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
640238104Sdes             K512[j] + W512[j]; \
641238104Sdes	(d) += T1, \
642238104Sdes	(h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)), \
643238104Sdes	j++
644238104Sdes
645238104Sdes
646238104Sdes#else /* BYTE_ORDER == LITTLE_ENDIAN */
647238104Sdes
648238104Sdes#define ROUND512_0_TO_15(a,b,c,d,e,f,g,h)	\
649238104Sdes	T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
650238104Sdes             K512[j] + (W512[j] = *data++); \
651238104Sdes	(d) += T1; \
652238104Sdes	(h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
653238104Sdes	j++
654238104Sdes
655238104Sdes#endif /* BYTE_ORDER == LITTLE_ENDIAN */
656238104Sdes
657238104Sdes#define ROUND512(a,b,c,d,e,f,g,h)	\
658238104Sdes	s0 = W512[(j+1)&0x0f]; \
659238104Sdes	s0 = sigma0_512(s0); \
660238104Sdes	s1 = W512[(j+14)&0x0f]; \
661238104Sdes	s1 = sigma1_512(s1); \
662238104Sdes	T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] + \
663238104Sdes             (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); \
664238104Sdes	(d) += T1; \
665238104Sdes	(h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
666238104Sdes	j++
667238104Sdes
668238104Sdesstatic void ldns_sha512_Transform(ldns_sha512_CTX* context,
669238104Sdes                                  const sha2_word64* data) {
670238104Sdes	sha2_word64	a, b, c, d, e, f, g, h, s0, s1;
671238104Sdes	sha2_word64	T1, *W512 = (sha2_word64*)context->buffer;
672238104Sdes	int		j;
673238104Sdes
674238104Sdes	/* initialize registers with the prev. intermediate value */
675238104Sdes	a = context->state[0];
676238104Sdes	b = context->state[1];
677238104Sdes	c = context->state[2];
678238104Sdes	d = context->state[3];
679238104Sdes	e = context->state[4];
680238104Sdes	f = context->state[5];
681238104Sdes	g = context->state[6];
682238104Sdes	h = context->state[7];
683238104Sdes
684238104Sdes	j = 0;
685238104Sdes	do {
686238104Sdes		ROUND512_0_TO_15(a,b,c,d,e,f,g,h);
687238104Sdes		ROUND512_0_TO_15(h,a,b,c,d,e,f,g);
688238104Sdes		ROUND512_0_TO_15(g,h,a,b,c,d,e,f);
689238104Sdes		ROUND512_0_TO_15(f,g,h,a,b,c,d,e);
690238104Sdes		ROUND512_0_TO_15(e,f,g,h,a,b,c,d);
691238104Sdes		ROUND512_0_TO_15(d,e,f,g,h,a,b,c);
692238104Sdes		ROUND512_0_TO_15(c,d,e,f,g,h,a,b);
693238104Sdes		ROUND512_0_TO_15(b,c,d,e,f,g,h,a);
694238104Sdes	} while (j < 16);
695238104Sdes
696238104Sdes	/* Now for the remaining rounds up to 79: */
697238104Sdes	do {
698238104Sdes		ROUND512(a,b,c,d,e,f,g,h);
699238104Sdes		ROUND512(h,a,b,c,d,e,f,g);
700238104Sdes		ROUND512(g,h,a,b,c,d,e,f);
701238104Sdes		ROUND512(f,g,h,a,b,c,d,e);
702238104Sdes		ROUND512(e,f,g,h,a,b,c,d);
703238104Sdes		ROUND512(d,e,f,g,h,a,b,c);
704238104Sdes		ROUND512(c,d,e,f,g,h,a,b);
705238104Sdes		ROUND512(b,c,d,e,f,g,h,a);
706238104Sdes	} while (j < 80);
707238104Sdes
708238104Sdes	/* Compute the current intermediate hash value */
709238104Sdes	context->state[0] += a;
710238104Sdes	context->state[1] += b;
711238104Sdes	context->state[2] += c;
712238104Sdes	context->state[3] += d;
713238104Sdes	context->state[4] += e;
714238104Sdes	context->state[5] += f;
715238104Sdes	context->state[6] += g;
716238104Sdes	context->state[7] += h;
717238104Sdes
718238104Sdes	/* Clean up */
719238104Sdes	a = b = c = d = e = f = g = h = T1 = 0;
720238104Sdes}
721238104Sdes
722238104Sdes#else /* SHA2_UNROLL_TRANSFORM */
723238104Sdes
724238104Sdesstatic void ldns_sha512_Transform(ldns_sha512_CTX* context,
725238104Sdes                                  const sha2_word64* data) {
726238104Sdes	sha2_word64	a, b, c, d, e, f, g, h, s0, s1;
727238104Sdes	sha2_word64	T1, T2, *W512 = (sha2_word64*)context->buffer;
728238104Sdes	int		j;
729238104Sdes
730238104Sdes	/* initialize registers with the prev. intermediate value */
731238104Sdes	a = context->state[0];
732238104Sdes	b = context->state[1];
733238104Sdes	c = context->state[2];
734238104Sdes	d = context->state[3];
735238104Sdes	e = context->state[4];
736238104Sdes	f = context->state[5];
737238104Sdes	g = context->state[6];
738238104Sdes	h = context->state[7];
739238104Sdes
740238104Sdes	j = 0;
741238104Sdes	do {
742238104Sdes#if BYTE_ORDER == LITTLE_ENDIAN
743238104Sdes		/* Convert TO host byte order */
744238104Sdes		REVERSE64(*data++, W512[j]);
745238104Sdes		/* Apply the SHA-512 compression function to update a..h */
746238104Sdes		T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
747238104Sdes#else /* BYTE_ORDER == LITTLE_ENDIAN */
748238104Sdes		/* Apply the SHA-512 compression function to update a..h with copy */
749238104Sdes		T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j] = *data++);
750238104Sdes#endif /* BYTE_ORDER == LITTLE_ENDIAN */
751238104Sdes		T2 = Sigma0_512(a) + Maj(a, b, c);
752238104Sdes		h = g;
753238104Sdes		g = f;
754238104Sdes		f = e;
755238104Sdes		e = d + T1;
756238104Sdes		d = c;
757238104Sdes		c = b;
758238104Sdes		b = a;
759238104Sdes		a = T1 + T2;
760238104Sdes
761238104Sdes		j++;
762238104Sdes	} while (j < 16);
763238104Sdes
764238104Sdes	do {
765238104Sdes		/* Part of the message block expansion: */
766238104Sdes		s0 = W512[(j+1)&0x0f];
767238104Sdes		s0 = sigma0_512(s0);
768238104Sdes		s1 = W512[(j+14)&0x0f];
769238104Sdes		s1 =  sigma1_512(s1);
770238104Sdes
771238104Sdes		/* Apply the SHA-512 compression function to update a..h */
772238104Sdes		T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
773238104Sdes		     (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);
774238104Sdes		T2 = Sigma0_512(a) + Maj(a, b, c);
775238104Sdes		h = g;
776238104Sdes		g = f;
777238104Sdes		f = e;
778238104Sdes		e = d + T1;
779238104Sdes		d = c;
780238104Sdes		c = b;
781238104Sdes		b = a;
782238104Sdes		a = T1 + T2;
783238104Sdes
784238104Sdes		j++;
785238104Sdes	} while (j < 80);
786238104Sdes
787238104Sdes	/* Compute the current intermediate hash value */
788238104Sdes	context->state[0] += a;
789238104Sdes	context->state[1] += b;
790238104Sdes	context->state[2] += c;
791238104Sdes	context->state[3] += d;
792238104Sdes	context->state[4] += e;
793238104Sdes	context->state[5] += f;
794238104Sdes	context->state[6] += g;
795238104Sdes	context->state[7] += h;
796238104Sdes
797238104Sdes	/* Clean up */
798238104Sdes	a = b = c = d = e = f = g = h = T1 = T2 = 0;
799238104Sdes}
800238104Sdes
801238104Sdes#endif /* SHA2_UNROLL_TRANSFORM */
802238104Sdes
803238104Sdesvoid ldns_sha512_update(ldns_sha512_CTX* context, const sha2_byte *data, size_t len) {
804238104Sdes	size_t freespace, usedspace;
805238104Sdes
806238104Sdes	if (len == 0) {
807238104Sdes		/* Calling with no data is valid - we do nothing */
808238104Sdes		return;
809238104Sdes	}
810238104Sdes
811238104Sdes	/* Sanity check: */
812238104Sdes	assert(context != (ldns_sha512_CTX*)0 && data != (sha2_byte*)0);
813238104Sdes
814238104Sdes	usedspace = (context->bitcount[0] >> 3) % LDNS_SHA512_BLOCK_LENGTH;
815238104Sdes	if (usedspace > 0) {
816238104Sdes		/* Calculate how much free space is available in the buffer */
817238104Sdes		freespace = LDNS_SHA512_BLOCK_LENGTH - usedspace;
818238104Sdes
819238104Sdes		if (len >= freespace) {
820238104Sdes			/* Fill the buffer completely and process it */
821238104Sdes			MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace);
822238104Sdes			ADDINC128(context->bitcount, freespace << 3);
823238104Sdes			len -= freespace;
824238104Sdes			data += freespace;
825238104Sdes			ldns_sha512_Transform(context, (sha2_word64*)context->buffer);
826238104Sdes		} else {
827238104Sdes			/* The buffer is not yet full */
828238104Sdes			MEMCPY_BCOPY(&context->buffer[usedspace], data, len);
829238104Sdes			ADDINC128(context->bitcount, len << 3);
830238104Sdes			/* Clean up: */
831238104Sdes			usedspace = freespace = 0;
832238104Sdes			return;
833238104Sdes		}
834238104Sdes	}
835238104Sdes	while (len >= LDNS_SHA512_BLOCK_LENGTH) {
836238104Sdes		/* Process as many complete blocks as we can */
837238104Sdes		ldns_sha512_Transform(context, (sha2_word64*)data);
838238104Sdes		ADDINC128(context->bitcount, LDNS_SHA512_BLOCK_LENGTH << 3);
839238104Sdes		len -= LDNS_SHA512_BLOCK_LENGTH;
840238104Sdes		data += LDNS_SHA512_BLOCK_LENGTH;
841238104Sdes	}
842238104Sdes	if (len > 0) {
843238104Sdes		/* There's left-overs, so save 'em */
844238104Sdes		MEMCPY_BCOPY(context->buffer, data, len);
845238104Sdes		ADDINC128(context->bitcount, len << 3);
846238104Sdes	}
847238104Sdes	/* Clean up: */
848238104Sdes	usedspace = freespace = 0;
849238104Sdes}
850238104Sdes
851238104Sdesstatic void ldns_sha512_Last(ldns_sha512_CTX* context) {
852238104Sdes	size_t usedspace;
853238104Sdes
854238104Sdes	usedspace = (context->bitcount[0] >> 3) % LDNS_SHA512_BLOCK_LENGTH;
855238104Sdes#if BYTE_ORDER == LITTLE_ENDIAN
856238104Sdes	/* Convert FROM host byte order */
857238104Sdes	REVERSE64(context->bitcount[0],context->bitcount[0]);
858238104Sdes	REVERSE64(context->bitcount[1],context->bitcount[1]);
859238104Sdes#endif
860238104Sdes	if (usedspace > 0) {
861238104Sdes		/* Begin padding with a 1 bit: */
862238104Sdes		context->buffer[usedspace++] = 0x80;
863238104Sdes
864238104Sdes		if (usedspace <= ldns_sha512_SHORT_BLOCK_LENGTH) {
865238104Sdes			/* Set-up for the last transform: */
866238104Sdes			MEMSET_BZERO(&context->buffer[usedspace], ldns_sha512_SHORT_BLOCK_LENGTH - usedspace);
867238104Sdes		} else {
868238104Sdes			if (usedspace < LDNS_SHA512_BLOCK_LENGTH) {
869238104Sdes				MEMSET_BZERO(&context->buffer[usedspace], LDNS_SHA512_BLOCK_LENGTH - usedspace);
870238104Sdes			}
871238104Sdes			/* Do second-to-last transform: */
872238104Sdes			ldns_sha512_Transform(context, (sha2_word64*)context->buffer);
873238104Sdes
874238104Sdes			/* And set-up for the last transform: */
875238104Sdes			MEMSET_BZERO(context->buffer, LDNS_SHA512_BLOCK_LENGTH - 2);
876238104Sdes		}
877238104Sdes	} else {
878238104Sdes		/* Prepare for final transform: */
879238104Sdes		MEMSET_BZERO(context->buffer, ldns_sha512_SHORT_BLOCK_LENGTH);
880238104Sdes
881238104Sdes		/* Begin padding with a 1 bit: */
882238104Sdes		*context->buffer = 0x80;
883238104Sdes	}
884238104Sdes	/* Store the length of input data (in bits): */
885238104Sdes	*(sha2_word64*)&context->buffer[ldns_sha512_SHORT_BLOCK_LENGTH] = context->bitcount[1];
886238104Sdes	*(sha2_word64*)&context->buffer[ldns_sha512_SHORT_BLOCK_LENGTH+8] = context->bitcount[0];
887238104Sdes
888238104Sdes	/* final transform: */
889238104Sdes	ldns_sha512_Transform(context, (sha2_word64*)context->buffer);
890238104Sdes}
891238104Sdes
892238104Sdesvoid ldns_sha512_final(sha2_byte digest[], ldns_sha512_CTX* context) {
893238104Sdes	sha2_word64	*d = (sha2_word64*)digest;
894238104Sdes
895238104Sdes	/* Sanity check: */
896238104Sdes	assert(context != (ldns_sha512_CTX*)0);
897238104Sdes
898238104Sdes	/* If no digest buffer is passed, we don't bother doing this: */
899238104Sdes	if (digest != (sha2_byte*)0) {
900238104Sdes		ldns_sha512_Last(context);
901238104Sdes
902238104Sdes		/* Save the hash data for output: */
903238104Sdes#if BYTE_ORDER == LITTLE_ENDIAN
904238104Sdes		{
905238104Sdes			/* Convert TO host byte order */
906238104Sdes			int	j;
907238104Sdes			for (j = 0; j < 8; j++) {
908238104Sdes				REVERSE64(context->state[j],context->state[j]);
909238104Sdes				*d++ = context->state[j];
910238104Sdes			}
911238104Sdes		}
912238104Sdes#else
913238104Sdes		MEMCPY_BCOPY(d, context->state, LDNS_SHA512_DIGEST_LENGTH);
914238104Sdes#endif
915238104Sdes	}
916238104Sdes
917238104Sdes	/* Zero out state data */
918238104Sdes	MEMSET_BZERO(context, sizeof(ldns_sha512_CTX));
919238104Sdes}
920238104Sdes
921238104Sdesunsigned char *
922238104Sdesldns_sha512(unsigned char *data, unsigned int data_len, unsigned char *digest)
923238104Sdes{
924238104Sdes    ldns_sha512_CTX ctx;
925238104Sdes    ldns_sha512_init(&ctx);
926238104Sdes    ldns_sha512_update(&ctx, data, data_len);
927238104Sdes    ldns_sha512_final(digest, &ctx);
928238104Sdes    return digest;
929238104Sdes}
930238104Sdes
931238104Sdes/*** SHA-384: *********************************************************/
932238104Sdesvoid ldns_sha384_init(ldns_sha384_CTX* context) {
933238104Sdes	if (context == (ldns_sha384_CTX*)0) {
934238104Sdes		return;
935238104Sdes	}
936238104Sdes	MEMCPY_BCOPY(context->state, sha384_initial_hash_value, LDNS_SHA512_DIGEST_LENGTH);
937238104Sdes	MEMSET_BZERO(context->buffer, LDNS_SHA384_BLOCK_LENGTH);
938238104Sdes	context->bitcount[0] = context->bitcount[1] = 0;
939238104Sdes}
940238104Sdes
941238104Sdesvoid ldns_sha384_update(ldns_sha384_CTX* context, const sha2_byte* data, size_t len) {
942238104Sdes	ldns_sha512_update((ldns_sha512_CTX*)context, data, len);
943238104Sdes}
944238104Sdes
945238104Sdesvoid ldns_sha384_final(sha2_byte digest[], ldns_sha384_CTX* context) {
946238104Sdes	sha2_word64	*d = (sha2_word64*)digest;
947238104Sdes
948238104Sdes	/* Sanity check: */
949238104Sdes	assert(context != (ldns_sha384_CTX*)0);
950238104Sdes
951238104Sdes	/* If no digest buffer is passed, we don't bother doing this: */
952238104Sdes	if (digest != (sha2_byte*)0) {
953238104Sdes		ldns_sha512_Last((ldns_sha512_CTX*)context);
954238104Sdes
955238104Sdes		/* Save the hash data for output: */
956238104Sdes#if BYTE_ORDER == LITTLE_ENDIAN
957238104Sdes		{
958238104Sdes			/* Convert TO host byte order */
959238104Sdes			int	j;
960238104Sdes			for (j = 0; j < 6; j++) {
961238104Sdes				REVERSE64(context->state[j],context->state[j]);
962238104Sdes				*d++ = context->state[j];
963238104Sdes			}
964238104Sdes		}
965238104Sdes#else
966238104Sdes		MEMCPY_BCOPY(d, context->state, LDNS_SHA384_DIGEST_LENGTH);
967238104Sdes#endif
968238104Sdes	}
969238104Sdes
970238104Sdes	/* Zero out state data */
971238104Sdes	MEMSET_BZERO(context, sizeof(ldns_sha384_CTX));
972238104Sdes}
973238104Sdes
974238104Sdesunsigned char *
975238104Sdesldns_sha384(unsigned char *data, unsigned int data_len, unsigned char *digest)
976238104Sdes{
977238104Sdes    ldns_sha384_CTX ctx;
978238104Sdes    ldns_sha384_init(&ctx);
979238104Sdes    ldns_sha384_update(&ctx, data, data_len);
980238104Sdes    ldns_sha384_final(digest, &ctx);
981238104Sdes    return digest;
982238104Sdes}
983