xref: /netbsd-src/external/bsd/unbound/dist/compat/sha512.c (revision 01049ae6d55a7fce6c6379cd1e0c997c04dc0061)
13b6c3722Schristos /*
23b6c3722Schristos  * FILE:	sha2.c
33b6c3722Schristos  * AUTHOR:	Aaron D. Gifford - http://www.aarongifford.com/
43b6c3722Schristos  *
53b6c3722Schristos  * Copyright (c) 2000-2001, Aaron D. Gifford
63b6c3722Schristos  * All rights reserved.
73b6c3722Schristos  *
83b6c3722Schristos  * Modified by Jelte Jansen to fit in ldns, and not clash with any
93b6c3722Schristos  * system-defined SHA code.
103b6c3722Schristos  * Changes:
113b6c3722Schristos  * - Renamed (external) functions and constants to fit ldns style
123b6c3722Schristos  * - Removed _End and _Data functions
133b6c3722Schristos  * - Added ldns_shaX(data, len, digest) convenience functions
143b6c3722Schristos  * - Removed prototypes of _Transform functions and made those static
153b6c3722Schristos  * Modified by Wouter, and trimmed, to provide SHA512 for getentropy_fallback.
163b6c3722Schristos  *
173b6c3722Schristos  * Redistribution and use in source and binary forms, with or without
183b6c3722Schristos  * modification, are permitted provided that the following conditions
193b6c3722Schristos  * are met:
203b6c3722Schristos  * 1. Redistributions of source code must retain the above copyright
213b6c3722Schristos  *    notice, this list of conditions and the following disclaimer.
223b6c3722Schristos  * 2. Redistributions in binary form must reproduce the above copyright
233b6c3722Schristos  *    notice, this list of conditions and the following disclaimer in the
243b6c3722Schristos  *    documentation and/or other materials provided with the distribution.
253b6c3722Schristos  * 3. Neither the name of the copyright holder nor the names of contributors
263b6c3722Schristos  *    may be used to endorse or promote products derived from this software
273b6c3722Schristos  *    without specific prior written permission.
283b6c3722Schristos  *
293b6c3722Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
303b6c3722Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
313b6c3722Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
323b6c3722Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
333b6c3722Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
343b6c3722Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
353b6c3722Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
363b6c3722Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
373b6c3722Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
383b6c3722Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
393b6c3722Schristos  * SUCH DAMAGE.
403b6c3722Schristos  *
41*01049ae6Schristos  * Id: sha2.c,v 1.1 2001/11/08 00:01:51 adg Exp adg
423b6c3722Schristos  */
433b6c3722Schristos #include "config.h"
443b6c3722Schristos 
453b6c3722Schristos #include <string.h>	/* memcpy()/memset() or bcopy()/bzero() */
463b6c3722Schristos #include <assert.h>	/* assert() */
473b6c3722Schristos 
483b6c3722Schristos /* do we have sha512 header defs */
493b6c3722Schristos #ifndef SHA512_DIGEST_LENGTH
503b6c3722Schristos #define SHA512_BLOCK_LENGTH		128
513b6c3722Schristos #define SHA512_DIGEST_LENGTH		64
523b6c3722Schristos #define SHA512_DIGEST_STRING_LENGTH	(SHA512_DIGEST_LENGTH * 2 + 1)
533b6c3722Schristos typedef struct _SHA512_CTX {
543b6c3722Schristos 	uint64_t	state[8];
553b6c3722Schristos 	uint64_t	bitcount[2];
563b6c3722Schristos 	uint8_t	buffer[SHA512_BLOCK_LENGTH];
573b6c3722Schristos } SHA512_CTX;
583b6c3722Schristos #endif /* do we have sha512 header defs */
593b6c3722Schristos 
603b6c3722Schristos void SHA512_Init(SHA512_CTX*);
613b6c3722Schristos void SHA512_Update(SHA512_CTX*, void*, size_t);
623b6c3722Schristos void SHA512_Final(uint8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*);
633b6c3722Schristos unsigned char *SHA512(void *data, unsigned int data_len, unsigned char *digest);
643b6c3722Schristos 
653b6c3722Schristos 
663b6c3722Schristos /*** SHA-256/384/512 Machine Architecture Definitions *****************/
673b6c3722Schristos /*
683b6c3722Schristos  * BYTE_ORDER NOTE:
693b6c3722Schristos  *
703b6c3722Schristos  * Please make sure that your system defines BYTE_ORDER.  If your
713b6c3722Schristos  * architecture is little-endian, make sure it also defines
723b6c3722Schristos  * LITTLE_ENDIAN and that the two (BYTE_ORDER and LITTLE_ENDIAN) are
733b6c3722Schristos  * equivalent.
743b6c3722Schristos  *
753b6c3722Schristos  * If your system does not define the above, then you can do so by
763b6c3722Schristos  * hand like this:
773b6c3722Schristos  *
783b6c3722Schristos  *   #define LITTLE_ENDIAN 1234
793b6c3722Schristos  *   #define BIG_ENDIAN    4321
803b6c3722Schristos  *
813b6c3722Schristos  * And for little-endian machines, add:
823b6c3722Schristos  *
833b6c3722Schristos  *   #define BYTE_ORDER LITTLE_ENDIAN
843b6c3722Schristos  *
853b6c3722Schristos  * Or for big-endian machines:
863b6c3722Schristos  *
873b6c3722Schristos  *   #define BYTE_ORDER BIG_ENDIAN
883b6c3722Schristos  *
893b6c3722Schristos  * The FreeBSD machine this was written on defines BYTE_ORDER
903b6c3722Schristos  * appropriately by including <sys/types.h> (which in turn includes
913b6c3722Schristos  * <machine/endian.h> where the appropriate definitions are actually
923b6c3722Schristos  * made).
933b6c3722Schristos  */
943b6c3722Schristos #if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN)
953b6c3722Schristos #error Define BYTE_ORDER to be equal to either LITTLE_ENDIAN or BIG_ENDIAN
963b6c3722Schristos #endif
973b6c3722Schristos 
983b6c3722Schristos typedef uint8_t  sha2_byte;	/* Exactly 1 byte */
993b6c3722Schristos typedef uint32_t sha2_word32;	/* Exactly 4 bytes */
1003b6c3722Schristos #ifdef S_SPLINT_S
1013b6c3722Schristos typedef unsigned long long sha2_word64; /* lint 8 bytes */
1023b6c3722Schristos #else
1033b6c3722Schristos typedef uint64_t sha2_word64;	/* Exactly 8 bytes */
1043b6c3722Schristos #endif
1053b6c3722Schristos 
1063b6c3722Schristos /*** SHA-256/384/512 Various Length Definitions ***********************/
1073b6c3722Schristos #define SHA512_SHORT_BLOCK_LENGTH	(SHA512_BLOCK_LENGTH - 16)
1083b6c3722Schristos 
1093b6c3722Schristos 
1103b6c3722Schristos /*** ENDIAN REVERSAL MACROS *******************************************/
1113b6c3722Schristos #if BYTE_ORDER == LITTLE_ENDIAN
1123b6c3722Schristos #define REVERSE32(w,x)	{ \
1133b6c3722Schristos 	sha2_word32 tmp = (w); \
1143b6c3722Schristos 	tmp = (tmp >> 16) | (tmp << 16); \
1153b6c3722Schristos 	(x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \
1163b6c3722Schristos }
1173b6c3722Schristos #ifndef S_SPLINT_S
1183b6c3722Schristos #define REVERSE64(w,x)	{ \
1193b6c3722Schristos 	sha2_word64 tmp = (w); \
1203b6c3722Schristos 	tmp = (tmp >> 32) | (tmp << 32); \
1213b6c3722Schristos 	tmp = ((tmp & 0xff00ff00ff00ff00ULL) >> 8) | \
1223b6c3722Schristos 	      ((tmp & 0x00ff00ff00ff00ffULL) << 8); \
1233b6c3722Schristos 	(x) = ((tmp & 0xffff0000ffff0000ULL) >> 16) | \
1243b6c3722Schristos 	      ((tmp & 0x0000ffff0000ffffULL) << 16); \
1253b6c3722Schristos }
1263b6c3722Schristos #else /* splint */
1273b6c3722Schristos #define REVERSE64(w,x) /* splint */
1283b6c3722Schristos #endif /* splint */
1293b6c3722Schristos #endif /* BYTE_ORDER == LITTLE_ENDIAN */
1303b6c3722Schristos 
1313b6c3722Schristos /*
1323b6c3722Schristos  * Macro for incrementally adding the unsigned 64-bit integer n to the
1333b6c3722Schristos  * unsigned 128-bit integer (represented using a two-element array of
1343b6c3722Schristos  * 64-bit words):
1353b6c3722Schristos  */
1363b6c3722Schristos #define ADDINC128(w,n)	{ \
1373b6c3722Schristos 	(w)[0] += (sha2_word64)(n); \
1383b6c3722Schristos 	if ((w)[0] < (n)) { \
1393b6c3722Schristos 		(w)[1]++; \
1403b6c3722Schristos 	} \
1413b6c3722Schristos }
1423b6c3722Schristos #ifdef S_SPLINT_S
1433b6c3722Schristos #undef ADDINC128
1443b6c3722Schristos #define ADDINC128(w,n) /* splint */
1453b6c3722Schristos #endif
1463b6c3722Schristos 
1473b6c3722Schristos /*
1483b6c3722Schristos  * Macros for copying blocks of memory and for zeroing out ranges
1493b6c3722Schristos  * of memory.  Using these macros makes it easy to switch from
1503b6c3722Schristos  * using memset()/memcpy() and using bzero()/bcopy().
1513b6c3722Schristos  *
1523b6c3722Schristos  * Please define either SHA2_USE_MEMSET_MEMCPY or define
1533b6c3722Schristos  * SHA2_USE_BZERO_BCOPY depending on which function set you
1543b6c3722Schristos  * choose to use:
1553b6c3722Schristos  */
1563b6c3722Schristos #if !defined(SHA2_USE_MEMSET_MEMCPY) && !defined(SHA2_USE_BZERO_BCOPY)
1573b6c3722Schristos /* Default to memset()/memcpy() if no option is specified */
1583b6c3722Schristos #define	SHA2_USE_MEMSET_MEMCPY	1
1593b6c3722Schristos #endif
1603b6c3722Schristos #if defined(SHA2_USE_MEMSET_MEMCPY) && defined(SHA2_USE_BZERO_BCOPY)
1613b6c3722Schristos /* Abort with an error if BOTH options are defined */
1623b6c3722Schristos #error Define either SHA2_USE_MEMSET_MEMCPY or SHA2_USE_BZERO_BCOPY, not both!
1633b6c3722Schristos #endif
1643b6c3722Schristos 
1653b6c3722Schristos #ifdef SHA2_USE_MEMSET_MEMCPY
1663b6c3722Schristos #define MEMSET_BZERO(p,l)	memset((p), 0, (l))
1673b6c3722Schristos #define MEMCPY_BCOPY(d,s,l)	memcpy((d), (s), (l))
1683b6c3722Schristos #endif
1693b6c3722Schristos #ifdef SHA2_USE_BZERO_BCOPY
1703b6c3722Schristos #define MEMSET_BZERO(p,l)	bzero((p), (l))
1713b6c3722Schristos #define MEMCPY_BCOPY(d,s,l)	bcopy((s), (d), (l))
1723b6c3722Schristos #endif
1733b6c3722Schristos 
1743b6c3722Schristos 
1753b6c3722Schristos /*** THE SIX LOGICAL FUNCTIONS ****************************************/
1763b6c3722Schristos /*
1773b6c3722Schristos  * Bit shifting and rotation (used by the six SHA-XYZ logical functions:
1783b6c3722Schristos  *
1793b6c3722Schristos  *   NOTE:  The naming of R and S appears backwards here (R is a SHIFT and
1803b6c3722Schristos  *   S is a ROTATION) because the SHA-256/384/512 description document
1813b6c3722Schristos  *   (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this
1823b6c3722Schristos  *   same "backwards" definition.
1833b6c3722Schristos  */
1843b6c3722Schristos /* Shift-right (used in SHA-256, SHA-384, and SHA-512): */
1853b6c3722Schristos #define R(b,x) 		((x) >> (b))
1863b6c3722Schristos /* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
1873b6c3722Schristos #define S64(b,x)	(((x) >> (b)) | ((x) << (64 - (b))))
1883b6c3722Schristos 
1893b6c3722Schristos /* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */
1903b6c3722Schristos #define Ch(x,y,z)	(((x) & (y)) ^ ((~(x)) & (z)))
1913b6c3722Schristos #define Maj(x,y,z)	(((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
1923b6c3722Schristos 
1933b6c3722Schristos /* Four of six logical functions used in SHA-384 and SHA-512: */
1943b6c3722Schristos #define Sigma0_512(x)	(S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))
1953b6c3722Schristos #define Sigma1_512(x)	(S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))
1963b6c3722Schristos #define sigma0_512(x)	(S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7,   (x)))
1973b6c3722Schristos #define sigma1_512(x)	(S64(19, (x)) ^ S64(61, (x)) ^ R( 6,   (x)))
1983b6c3722Schristos 
1993b6c3722Schristos /*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
2003b6c3722Schristos /* Hash constant words K for SHA-384 and SHA-512: */
2013b6c3722Schristos static const sha2_word64 K512[80] = {
2023b6c3722Schristos 	0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
2033b6c3722Schristos 	0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
2043b6c3722Schristos 	0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
2053b6c3722Schristos 	0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
2063b6c3722Schristos 	0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
2073b6c3722Schristos 	0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
2083b6c3722Schristos 	0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
2093b6c3722Schristos 	0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
2103b6c3722Schristos 	0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
2113b6c3722Schristos 	0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
2123b6c3722Schristos 	0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
2133b6c3722Schristos 	0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
2143b6c3722Schristos 	0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
2153b6c3722Schristos 	0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
2163b6c3722Schristos 	0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
2173b6c3722Schristos 	0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
2183b6c3722Schristos 	0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
2193b6c3722Schristos 	0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
2203b6c3722Schristos 	0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
2213b6c3722Schristos 	0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
2223b6c3722Schristos 	0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
2233b6c3722Schristos 	0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
2243b6c3722Schristos 	0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
2253b6c3722Schristos 	0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
2263b6c3722Schristos 	0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
2273b6c3722Schristos 	0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
2283b6c3722Schristos 	0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
2293b6c3722Schristos 	0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
2303b6c3722Schristos 	0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
2313b6c3722Schristos 	0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
2323b6c3722Schristos 	0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
2333b6c3722Schristos 	0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
2343b6c3722Schristos 	0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
2353b6c3722Schristos 	0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
2363b6c3722Schristos 	0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
2373b6c3722Schristos 	0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
2383b6c3722Schristos 	0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
2393b6c3722Schristos 	0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
2403b6c3722Schristos 	0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
2413b6c3722Schristos 	0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
2423b6c3722Schristos };
2433b6c3722Schristos 
2443b6c3722Schristos /* initial hash value H for SHA-512 */
2453b6c3722Schristos static const sha2_word64 sha512_initial_hash_value[8] = {
2463b6c3722Schristos 	0x6a09e667f3bcc908ULL,
2473b6c3722Schristos 	0xbb67ae8584caa73bULL,
2483b6c3722Schristos 	0x3c6ef372fe94f82bULL,
2493b6c3722Schristos 	0xa54ff53a5f1d36f1ULL,
2503b6c3722Schristos 	0x510e527fade682d1ULL,
2513b6c3722Schristos 	0x9b05688c2b3e6c1fULL,
2523b6c3722Schristos 	0x1f83d9abfb41bd6bULL,
2533b6c3722Schristos 	0x5be0cd19137e2179ULL
2543b6c3722Schristos };
2553b6c3722Schristos 
2563b6c3722Schristos typedef union _ldns_sha2_buffer_union {
2573b6c3722Schristos         uint8_t*  theChars;
2583b6c3722Schristos         uint64_t* theLongs;
2593b6c3722Schristos } ldns_sha2_buffer_union;
2603b6c3722Schristos 
2613b6c3722Schristos /*** SHA-512: *********************************************************/
SHA512_Init(SHA512_CTX * context)2623b6c3722Schristos void SHA512_Init(SHA512_CTX* context) {
2633b6c3722Schristos 	if (context == (SHA512_CTX*)0) {
2643b6c3722Schristos 		return;
2653b6c3722Schristos 	}
2663b6c3722Schristos 	MEMCPY_BCOPY(context->state, sha512_initial_hash_value, SHA512_DIGEST_LENGTH);
2673b6c3722Schristos 	MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH);
2683b6c3722Schristos 	context->bitcount[0] = context->bitcount[1] =  0;
2693b6c3722Schristos }
2703b6c3722Schristos 
SHA512_Transform(SHA512_CTX * context,const sha2_word64 * data)2713b6c3722Schristos static void SHA512_Transform(SHA512_CTX* context,
2723b6c3722Schristos                                   const sha2_word64* data) {
2733b6c3722Schristos 	sha2_word64	a, b, c, d, e, f, g, h, s0, s1;
2743b6c3722Schristos 	sha2_word64	T1, T2, *W512 = (sha2_word64*)context->buffer;
2753b6c3722Schristos 	int		j;
2763b6c3722Schristos 
2773b6c3722Schristos 	/* initialize registers with the prev. intermediate value */
2783b6c3722Schristos 	a = context->state[0];
2793b6c3722Schristos 	b = context->state[1];
2803b6c3722Schristos 	c = context->state[2];
2813b6c3722Schristos 	d = context->state[3];
2823b6c3722Schristos 	e = context->state[4];
2833b6c3722Schristos 	f = context->state[5];
2843b6c3722Schristos 	g = context->state[6];
2853b6c3722Schristos 	h = context->state[7];
2863b6c3722Schristos 
2873b6c3722Schristos 	j = 0;
2883b6c3722Schristos 	do {
2893b6c3722Schristos #if BYTE_ORDER == LITTLE_ENDIAN
2903b6c3722Schristos 		/* Convert TO host byte order */
2913b6c3722Schristos 		REVERSE64(*data++, W512[j]);
2923b6c3722Schristos 		/* Apply the SHA-512 compression function to update a..h */
2933b6c3722Schristos 		T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
2943b6c3722Schristos #else /* BYTE_ORDER == LITTLE_ENDIAN */
2953b6c3722Schristos 		/* Apply the SHA-512 compression function to update a..h with copy */
2963b6c3722Schristos 		T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j] = *data++);
2973b6c3722Schristos #endif /* BYTE_ORDER == LITTLE_ENDIAN */
2983b6c3722Schristos 		T2 = Sigma0_512(a) + Maj(a, b, c);
2993b6c3722Schristos 		h = g;
3003b6c3722Schristos 		g = f;
3013b6c3722Schristos 		f = e;
3023b6c3722Schristos 		e = d + T1;
3033b6c3722Schristos 		d = c;
3043b6c3722Schristos 		c = b;
3053b6c3722Schristos 		b = a;
3063b6c3722Schristos 		a = T1 + T2;
3073b6c3722Schristos 
3083b6c3722Schristos 		j++;
3093b6c3722Schristos 	} while (j < 16);
3103b6c3722Schristos 
3113b6c3722Schristos 	do {
3123b6c3722Schristos 		/* Part of the message block expansion: */
3133b6c3722Schristos 		s0 = W512[(j+1)&0x0f];
3143b6c3722Schristos 		s0 = sigma0_512(s0);
3153b6c3722Schristos 		s1 = W512[(j+14)&0x0f];
3163b6c3722Schristos 		s1 =  sigma1_512(s1);
3173b6c3722Schristos 
3183b6c3722Schristos 		/* Apply the SHA-512 compression function to update a..h */
3193b6c3722Schristos 		T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
3203b6c3722Schristos 		     (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);
3213b6c3722Schristos 		T2 = Sigma0_512(a) + Maj(a, b, c);
3223b6c3722Schristos 		h = g;
3233b6c3722Schristos 		g = f;
3243b6c3722Schristos 		f = e;
3253b6c3722Schristos 		e = d + T1;
3263b6c3722Schristos 		d = c;
3273b6c3722Schristos 		c = b;
3283b6c3722Schristos 		b = a;
3293b6c3722Schristos 		a = T1 + T2;
3303b6c3722Schristos 
3313b6c3722Schristos 		j++;
3323b6c3722Schristos 	} while (j < 80);
3333b6c3722Schristos 
3343b6c3722Schristos 	/* Compute the current intermediate hash value */
3353b6c3722Schristos 	context->state[0] += a;
3363b6c3722Schristos 	context->state[1] += b;
3373b6c3722Schristos 	context->state[2] += c;
3383b6c3722Schristos 	context->state[3] += d;
3393b6c3722Schristos 	context->state[4] += e;
3403b6c3722Schristos 	context->state[5] += f;
3413b6c3722Schristos 	context->state[6] += g;
3423b6c3722Schristos 	context->state[7] += h;
3433b6c3722Schristos 
3443b6c3722Schristos 	/* Clean up */
3453b6c3722Schristos 	a = b = c = d = e = f = g = h = T1 = T2 = 0;
3463b6c3722Schristos }
3473b6c3722Schristos 
SHA512_Update(SHA512_CTX * context,void * datain,size_t len)3483b6c3722Schristos void SHA512_Update(SHA512_CTX* context, void *datain, size_t len) {
3493b6c3722Schristos 	size_t freespace, usedspace;
3503b6c3722Schristos 	const sha2_byte* data = (const sha2_byte*)datain;
3513b6c3722Schristos 
3523b6c3722Schristos 	if (len == 0) {
3533b6c3722Schristos 		/* Calling with no data is valid - we do nothing */
3543b6c3722Schristos 		return;
3553b6c3722Schristos 	}
3563b6c3722Schristos 
3573b6c3722Schristos 	/* Sanity check: */
3583b6c3722Schristos 	assert(context != (SHA512_CTX*)0 && data != (sha2_byte*)0);
3593b6c3722Schristos 
3603b6c3722Schristos 	usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
3613b6c3722Schristos 	if (usedspace > 0) {
3623b6c3722Schristos 		/* Calculate how much free space is available in the buffer */
3633b6c3722Schristos 		freespace = SHA512_BLOCK_LENGTH - usedspace;
3643b6c3722Schristos 
3653b6c3722Schristos 		if (len >= freespace) {
3663b6c3722Schristos 			/* Fill the buffer completely and process it */
3673b6c3722Schristos 			MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace);
3683b6c3722Schristos 			ADDINC128(context->bitcount, freespace << 3);
3693b6c3722Schristos 			len -= freespace;
3703b6c3722Schristos 			data += freespace;
3713b6c3722Schristos 			SHA512_Transform(context, (sha2_word64*)context->buffer);
3723b6c3722Schristos 		} else {
3733b6c3722Schristos 			/* The buffer is not yet full */
3743b6c3722Schristos 			MEMCPY_BCOPY(&context->buffer[usedspace], data, len);
3753b6c3722Schristos 			ADDINC128(context->bitcount, len << 3);
3763b6c3722Schristos 			/* Clean up: */
3773b6c3722Schristos 			usedspace = freespace = 0;
3783b6c3722Schristos 			return;
3793b6c3722Schristos 		}
3803b6c3722Schristos 	}
3813b6c3722Schristos 	while (len >= SHA512_BLOCK_LENGTH) {
3823b6c3722Schristos 		/* Process as many complete blocks as we can */
3833b6c3722Schristos 		SHA512_Transform(context, (sha2_word64*)data);
3843b6c3722Schristos 		ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3);
3853b6c3722Schristos 		len -= SHA512_BLOCK_LENGTH;
3863b6c3722Schristos 		data += SHA512_BLOCK_LENGTH;
3873b6c3722Schristos 	}
3883b6c3722Schristos 	if (len > 0) {
3893b6c3722Schristos 		/* There's left-overs, so save 'em */
3903b6c3722Schristos 		MEMCPY_BCOPY(context->buffer, data, len);
3913b6c3722Schristos 		ADDINC128(context->bitcount, len << 3);
3923b6c3722Schristos 	}
3933b6c3722Schristos 	/* Clean up: */
3943b6c3722Schristos 	usedspace = freespace = 0;
3953b6c3722Schristos }
3963b6c3722Schristos 
SHA512_Last(SHA512_CTX * context)3973b6c3722Schristos static void SHA512_Last(SHA512_CTX* context) {
3983b6c3722Schristos 	size_t usedspace;
3993b6c3722Schristos 	ldns_sha2_buffer_union cast_var;
4003b6c3722Schristos 
4013b6c3722Schristos 	usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
4023b6c3722Schristos #if BYTE_ORDER == LITTLE_ENDIAN
4033b6c3722Schristos 	/* Convert FROM host byte order */
4043b6c3722Schristos 	REVERSE64(context->bitcount[0],context->bitcount[0]);
4053b6c3722Schristos 	REVERSE64(context->bitcount[1],context->bitcount[1]);
4063b6c3722Schristos #endif
4073b6c3722Schristos 	if (usedspace > 0) {
4083b6c3722Schristos 		/* Begin padding with a 1 bit: */
4093b6c3722Schristos 		context->buffer[usedspace++] = 0x80;
4103b6c3722Schristos 
4113b6c3722Schristos 		if (usedspace <= SHA512_SHORT_BLOCK_LENGTH) {
4123b6c3722Schristos 			/* Set-up for the last transform: */
4133b6c3722Schristos 			MEMSET_BZERO(&context->buffer[usedspace], SHA512_SHORT_BLOCK_LENGTH - usedspace);
4143b6c3722Schristos 		} else {
4153b6c3722Schristos 			if (usedspace < SHA512_BLOCK_LENGTH) {
4163b6c3722Schristos 				MEMSET_BZERO(&context->buffer[usedspace], SHA512_BLOCK_LENGTH - usedspace);
4173b6c3722Schristos 			}
4183b6c3722Schristos 			/* Do second-to-last transform: */
4193b6c3722Schristos 			SHA512_Transform(context, (sha2_word64*)context->buffer);
4203b6c3722Schristos 
4213b6c3722Schristos 			/* And set-up for the last transform: */
4223b6c3722Schristos 			MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH - 2);
4233b6c3722Schristos 		}
4243b6c3722Schristos 	} else {
4253b6c3722Schristos 		/* Prepare for final transform: */
4263b6c3722Schristos 		MEMSET_BZERO(context->buffer, SHA512_SHORT_BLOCK_LENGTH);
4273b6c3722Schristos 
4283b6c3722Schristos 		/* Begin padding with a 1 bit: */
4293b6c3722Schristos 		*context->buffer = 0x80;
4303b6c3722Schristos 	}
4313b6c3722Schristos 	/* Store the length of input data (in bits): */
4323b6c3722Schristos 	cast_var.theChars = context->buffer;
4333b6c3722Schristos 	cast_var.theLongs[SHA512_SHORT_BLOCK_LENGTH / 8] = context->bitcount[1];
4343b6c3722Schristos 	cast_var.theLongs[SHA512_SHORT_BLOCK_LENGTH / 8 + 1] = context->bitcount[0];
4353b6c3722Schristos 
4363b6c3722Schristos 	/* final transform: */
4373b6c3722Schristos 	SHA512_Transform(context, (sha2_word64*)context->buffer);
4383b6c3722Schristos }
4393b6c3722Schristos 
SHA512_Final(sha2_byte digest[],SHA512_CTX * context)4403b6c3722Schristos void SHA512_Final(sha2_byte digest[], SHA512_CTX* context) {
4413b6c3722Schristos 	sha2_word64	*d = (sha2_word64*)digest;
4423b6c3722Schristos 
4433b6c3722Schristos 	/* Sanity check: */
4443b6c3722Schristos 	assert(context != (SHA512_CTX*)0);
4453b6c3722Schristos 
4463b6c3722Schristos 	/* If no digest buffer is passed, we don't bother doing this: */
4473b6c3722Schristos 	if (digest != (sha2_byte*)0) {
4483b6c3722Schristos 		SHA512_Last(context);
4493b6c3722Schristos 
4503b6c3722Schristos 		/* Save the hash data for output: */
4513b6c3722Schristos #if BYTE_ORDER == LITTLE_ENDIAN
4523b6c3722Schristos 		{
4533b6c3722Schristos 			/* Convert TO host byte order */
4543b6c3722Schristos 			int	j;
4553b6c3722Schristos 			for (j = 0; j < 8; j++) {
4563b6c3722Schristos 				REVERSE64(context->state[j],context->state[j]);
4573b6c3722Schristos 				*d++ = context->state[j];
4583b6c3722Schristos 			}
4593b6c3722Schristos 		}
4603b6c3722Schristos #else
4613b6c3722Schristos 		MEMCPY_BCOPY(d, context->state, SHA512_DIGEST_LENGTH);
4623b6c3722Schristos #endif
4633b6c3722Schristos 	}
4643b6c3722Schristos 
4653b6c3722Schristos 	/* Zero out state data */
4663b6c3722Schristos 	MEMSET_BZERO(context, sizeof(SHA512_CTX));
4673b6c3722Schristos }
4683b6c3722Schristos 
4693b6c3722Schristos unsigned char *
SHA512(void * data,unsigned int data_len,unsigned char * digest)4703b6c3722Schristos SHA512(void *data, unsigned int data_len, unsigned char *digest)
4713b6c3722Schristos {
4723b6c3722Schristos     SHA512_CTX ctx;
4733b6c3722Schristos     SHA512_Init(&ctx);
4743b6c3722Schristos     SHA512_Update(&ctx, data, data_len);
4753b6c3722Schristos     SHA512_Final(digest, &ctx);
4763b6c3722Schristos     return digest;
4773b6c3722Schristos }
478