Home | History | Annotate | Line # | Download | only in compat
      1      1.1  christos /*
      2      1.1  christos  * FILE:	sha2.c
      3      1.1  christos  * AUTHOR:	Aaron D. Gifford - http://www.aarongifford.com/
      4      1.1  christos  *
      5      1.1  christos  * Copyright (c) 2000-2001, Aaron D. Gifford
      6      1.1  christos  * All rights reserved.
      7      1.1  christos  *
      8      1.1  christos  * Modified by Jelte Jansen to fit in ldns, and not clash with any
      9      1.1  christos  * system-defined SHA code.
     10      1.1  christos  * Changes:
     11      1.1  christos  * - Renamed (external) functions and constants to fit ldns style
     12      1.1  christos  * - Removed _End and _Data functions
     13      1.1  christos  * - Added ldns_shaX(data, len, digest) convenience functions
     14      1.1  christos  * - Removed prototypes of _Transform functions and made those static
     15      1.1  christos  * Modified by Wouter, and trimmed, to provide SHA512 for getentropy_fallback.
     16      1.1  christos  *
     17      1.1  christos  * Redistribution and use in source and binary forms, with or without
     18      1.1  christos  * modification, are permitted provided that the following conditions
     19      1.1  christos  * are met:
     20      1.1  christos  * 1. Redistributions of source code must retain the above copyright
     21      1.1  christos  *    notice, this list of conditions and the following disclaimer.
     22      1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     23      1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     24      1.1  christos  *    documentation and/or other materials provided with the distribution.
     25      1.1  christos  * 3. Neither the name of the copyright holder nor the names of contributors
     26      1.1  christos  *    may be used to endorse or promote products derived from this software
     27      1.1  christos  *    without specific prior written permission.
     28      1.1  christos  *
     29      1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
     30      1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     31      1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     32      1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
     33      1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     34      1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     35      1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     36      1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     37      1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     38      1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     39      1.1  christos  * SUCH DAMAGE.
     40      1.1  christos  *
     41  1.1.1.6  christos  * $Id: sha512.c,v 1.1.1.6 2026/09/17 14:22:45 christos Exp $
     42      1.1  christos  */
     43      1.1  christos #include "config.h"
     44      1.1  christos 
     45      1.1  christos #include <string.h>	/* memcpy()/memset() or bcopy()/bzero() */
     46      1.1  christos #include <assert.h>	/* assert() */
     47      1.1  christos 
     48      1.1  christos /* do we have sha512 header defs */
     49      1.1  christos #ifndef SHA512_DIGEST_LENGTH
     50      1.1  christos #define SHA512_BLOCK_LENGTH		128
     51      1.1  christos #define SHA512_DIGEST_LENGTH		64
     52      1.1  christos #define SHA512_DIGEST_STRING_LENGTH	(SHA512_DIGEST_LENGTH * 2 + 1)
     53      1.1  christos typedef struct _SHA512_CTX {
     54      1.1  christos 	uint64_t	state[8];
     55      1.1  christos 	uint64_t	bitcount[2];
     56      1.1  christos 	uint8_t	buffer[SHA512_BLOCK_LENGTH];
     57      1.1  christos } SHA512_CTX;
     58      1.1  christos #endif /* do we have sha512 header defs */
     59      1.1  christos 
     60      1.1  christos void SHA512_Init(SHA512_CTX*);
     61      1.1  christos void SHA512_Update(SHA512_CTX*, void*, size_t);
     62      1.1  christos void SHA512_Final(uint8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*);
     63      1.1  christos unsigned char *SHA512(void *data, unsigned int data_len, unsigned char *digest);
     64      1.1  christos 
     65      1.1  christos 
     66      1.1  christos /*** SHA-256/384/512 Machine Architecture Definitions *****************/
     67      1.1  christos /*
     68      1.1  christos  * BYTE_ORDER NOTE:
     69      1.1  christos  *
     70      1.1  christos  * Please make sure that your system defines BYTE_ORDER.  If your
     71      1.1  christos  * architecture is little-endian, make sure it also defines
     72      1.1  christos  * LITTLE_ENDIAN and that the two (BYTE_ORDER and LITTLE_ENDIAN) are
     73      1.1  christos  * equivalent.
     74      1.1  christos  *
     75      1.1  christos  * If your system does not define the above, then you can do so by
     76      1.1  christos  * hand like this:
     77      1.1  christos  *
     78      1.1  christos  *   #define LITTLE_ENDIAN 1234
     79      1.1  christos  *   #define BIG_ENDIAN    4321
     80      1.1  christos  *
     81      1.1  christos  * And for little-endian machines, add:
     82      1.1  christos  *
     83      1.1  christos  *   #define BYTE_ORDER LITTLE_ENDIAN
     84      1.1  christos  *
     85      1.1  christos  * Or for big-endian machines:
     86      1.1  christos  *
     87      1.1  christos  *   #define BYTE_ORDER BIG_ENDIAN
     88      1.1  christos  *
     89      1.1  christos  * The FreeBSD machine this was written on defines BYTE_ORDER
     90      1.1  christos  * appropriately by including <sys/types.h> (which in turn includes
     91      1.1  christos  * <machine/endian.h> where the appropriate definitions are actually
     92      1.1  christos  * made).
     93      1.1  christos  */
     94      1.1  christos #if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN)
     95      1.1  christos #error Define BYTE_ORDER to be equal to either LITTLE_ENDIAN or BIG_ENDIAN
     96      1.1  christos #endif
     97      1.1  christos 
     98      1.1  christos typedef uint8_t  sha2_byte;	/* Exactly 1 byte */
     99      1.1  christos typedef uint32_t sha2_word32;	/* Exactly 4 bytes */
    100      1.1  christos #ifdef S_SPLINT_S
    101      1.1  christos typedef unsigned long long sha2_word64; /* lint 8 bytes */
    102      1.1  christos #else
    103      1.1  christos typedef uint64_t sha2_word64;	/* Exactly 8 bytes */
    104      1.1  christos #endif
    105      1.1  christos 
    106      1.1  christos /*** SHA-256/384/512 Various Length Definitions ***********************/
    107      1.1  christos #define SHA512_SHORT_BLOCK_LENGTH	(SHA512_BLOCK_LENGTH - 16)
    108      1.1  christos 
    109      1.1  christos 
    110      1.1  christos /*** ENDIAN REVERSAL MACROS *******************************************/
    111      1.1  christos #if BYTE_ORDER == LITTLE_ENDIAN
    112      1.1  christos #define REVERSE32(w,x)	{ \
    113      1.1  christos 	sha2_word32 tmp = (w); \
    114      1.1  christos 	tmp = (tmp >> 16) | (tmp << 16); \
    115      1.1  christos 	(x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \
    116      1.1  christos }
    117      1.1  christos #ifndef S_SPLINT_S
    118      1.1  christos #define REVERSE64(w,x)	{ \
    119      1.1  christos 	sha2_word64 tmp = (w); \
    120      1.1  christos 	tmp = (tmp >> 32) | (tmp << 32); \
    121      1.1  christos 	tmp = ((tmp & 0xff00ff00ff00ff00ULL) >> 8) | \
    122      1.1  christos 	      ((tmp & 0x00ff00ff00ff00ffULL) << 8); \
    123      1.1  christos 	(x) = ((tmp & 0xffff0000ffff0000ULL) >> 16) | \
    124      1.1  christos 	      ((tmp & 0x0000ffff0000ffffULL) << 16); \
    125      1.1  christos }
    126      1.1  christos #else /* splint */
    127      1.1  christos #define REVERSE64(w,x) /* splint */
    128      1.1  christos #endif /* splint */
    129      1.1  christos #endif /* BYTE_ORDER == LITTLE_ENDIAN */
    130      1.1  christos 
    131      1.1  christos /*
    132      1.1  christos  * Macro for incrementally adding the unsigned 64-bit integer n to the
    133      1.1  christos  * unsigned 128-bit integer (represented using a two-element array of
    134      1.1  christos  * 64-bit words):
    135      1.1  christos  */
    136      1.1  christos #define ADDINC128(w,n)	{ \
    137      1.1  christos 	(w)[0] += (sha2_word64)(n); \
    138      1.1  christos 	if ((w)[0] < (n)) { \
    139      1.1  christos 		(w)[1]++; \
    140      1.1  christos 	} \
    141      1.1  christos }
    142      1.1  christos #ifdef S_SPLINT_S
    143      1.1  christos #undef ADDINC128
    144      1.1  christos #define ADDINC128(w,n) /* splint */
    145      1.1  christos #endif
    146      1.1  christos 
    147      1.1  christos /*
    148      1.1  christos  * Macros for copying blocks of memory and for zeroing out ranges
    149      1.1  christos  * of memory.  Using these macros makes it easy to switch from
    150      1.1  christos  * using memset()/memcpy() and using bzero()/bcopy().
    151      1.1  christos  *
    152      1.1  christos  * Please define either SHA2_USE_MEMSET_MEMCPY or define
    153      1.1  christos  * SHA2_USE_BZERO_BCOPY depending on which function set you
    154      1.1  christos  * choose to use:
    155      1.1  christos  */
    156      1.1  christos #if !defined(SHA2_USE_MEMSET_MEMCPY) && !defined(SHA2_USE_BZERO_BCOPY)
    157      1.1  christos /* Default to memset()/memcpy() if no option is specified */
    158      1.1  christos #define	SHA2_USE_MEMSET_MEMCPY	1
    159      1.1  christos #endif
    160      1.1  christos #if defined(SHA2_USE_MEMSET_MEMCPY) && defined(SHA2_USE_BZERO_BCOPY)
    161      1.1  christos /* Abort with an error if BOTH options are defined */
    162      1.1  christos #error Define either SHA2_USE_MEMSET_MEMCPY or SHA2_USE_BZERO_BCOPY, not both!
    163      1.1  christos #endif
    164      1.1  christos 
    165      1.1  christos #ifdef SHA2_USE_MEMSET_MEMCPY
    166      1.1  christos #define MEMSET_BZERO(p,l)	memset((p), 0, (l))
    167      1.1  christos #define MEMCPY_BCOPY(d,s,l)	memcpy((d), (s), (l))
    168      1.1  christos #endif
    169      1.1  christos #ifdef SHA2_USE_BZERO_BCOPY
    170      1.1  christos #define MEMSET_BZERO(p,l)	bzero((p), (l))
    171      1.1  christos #define MEMCPY_BCOPY(d,s,l)	bcopy((s), (d), (l))
    172      1.1  christos #endif
    173      1.1  christos 
    174      1.1  christos 
    175      1.1  christos /*** THE SIX LOGICAL FUNCTIONS ****************************************/
    176      1.1  christos /*
    177      1.1  christos  * Bit shifting and rotation (used by the six SHA-XYZ logical functions:
    178      1.1  christos  *
    179      1.1  christos  *   NOTE:  The naming of R and S appears backwards here (R is a SHIFT and
    180      1.1  christos  *   S is a ROTATION) because the SHA-256/384/512 description document
    181      1.1  christos  *   (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this
    182      1.1  christos  *   same "backwards" definition.
    183      1.1  christos  */
    184      1.1  christos /* Shift-right (used in SHA-256, SHA-384, and SHA-512): */
    185      1.1  christos #define R(b,x) 		((x) >> (b))
    186      1.1  christos /* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
    187      1.1  christos #define S64(b,x)	(((x) >> (b)) | ((x) << (64 - (b))))
    188      1.1  christos 
    189      1.1  christos /* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */
    190      1.1  christos #define Ch(x,y,z)	(((x) & (y)) ^ ((~(x)) & (z)))
    191      1.1  christos #define Maj(x,y,z)	(((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
    192      1.1  christos 
    193      1.1  christos /* Four of six logical functions used in SHA-384 and SHA-512: */
    194      1.1  christos #define Sigma0_512(x)	(S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))
    195      1.1  christos #define Sigma1_512(x)	(S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))
    196      1.1  christos #define sigma0_512(x)	(S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7,   (x)))
    197      1.1  christos #define sigma1_512(x)	(S64(19, (x)) ^ S64(61, (x)) ^ R( 6,   (x)))
    198      1.1  christos 
    199      1.1  christos /*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
    200      1.1  christos /* Hash constant words K for SHA-384 and SHA-512: */
    201      1.1  christos static const sha2_word64 K512[80] = {
    202      1.1  christos 	0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
    203      1.1  christos 	0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
    204      1.1  christos 	0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
    205      1.1  christos 	0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
    206      1.1  christos 	0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
    207      1.1  christos 	0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
    208      1.1  christos 	0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
    209      1.1  christos 	0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
    210      1.1  christos 	0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
    211      1.1  christos 	0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
    212      1.1  christos 	0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
    213      1.1  christos 	0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
    214      1.1  christos 	0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
    215      1.1  christos 	0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
    216      1.1  christos 	0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
    217      1.1  christos 	0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
    218      1.1  christos 	0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
    219      1.1  christos 	0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
    220      1.1  christos 	0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
    221      1.1  christos 	0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
    222      1.1  christos 	0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
    223      1.1  christos 	0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
    224      1.1  christos 	0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
    225      1.1  christos 	0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
    226      1.1  christos 	0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
    227      1.1  christos 	0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
    228      1.1  christos 	0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
    229      1.1  christos 	0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
    230      1.1  christos 	0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
    231      1.1  christos 	0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
    232      1.1  christos 	0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
    233      1.1  christos 	0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
    234      1.1  christos 	0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
    235      1.1  christos 	0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
    236      1.1  christos 	0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
    237      1.1  christos 	0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
    238      1.1  christos 	0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
    239      1.1  christos 	0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
    240      1.1  christos 	0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
    241      1.1  christos 	0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
    242      1.1  christos };
    243      1.1  christos 
    244      1.1  christos /* initial hash value H for SHA-512 */
    245      1.1  christos static const sha2_word64 sha512_initial_hash_value[8] = {
    246      1.1  christos 	0x6a09e667f3bcc908ULL,
    247      1.1  christos 	0xbb67ae8584caa73bULL,
    248      1.1  christos 	0x3c6ef372fe94f82bULL,
    249      1.1  christos 	0xa54ff53a5f1d36f1ULL,
    250      1.1  christos 	0x510e527fade682d1ULL,
    251      1.1  christos 	0x9b05688c2b3e6c1fULL,
    252      1.1  christos 	0x1f83d9abfb41bd6bULL,
    253      1.1  christos 	0x5be0cd19137e2179ULL
    254      1.1  christos };
    255      1.1  christos 
    256      1.1  christos typedef union _ldns_sha2_buffer_union {
    257      1.1  christos         uint8_t*  theChars;
    258      1.1  christos         uint64_t* theLongs;
    259      1.1  christos } ldns_sha2_buffer_union;
    260      1.1  christos 
    261      1.1  christos /*** SHA-512: *********************************************************/
    262      1.1  christos void SHA512_Init(SHA512_CTX* context) {
    263      1.1  christos 	if (context == (SHA512_CTX*)0) {
    264      1.1  christos 		return;
    265      1.1  christos 	}
    266      1.1  christos 	MEMCPY_BCOPY(context->state, sha512_initial_hash_value, SHA512_DIGEST_LENGTH);
    267      1.1  christos 	MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH);
    268      1.1  christos 	context->bitcount[0] = context->bitcount[1] =  0;
    269      1.1  christos }
    270      1.1  christos 
    271      1.1  christos static void SHA512_Transform(SHA512_CTX* context,
    272      1.1  christos                                   const sha2_word64* data) {
    273      1.1  christos 	sha2_word64	a, b, c, d, e, f, g, h, s0, s1;
    274      1.1  christos 	sha2_word64	T1, T2, *W512 = (sha2_word64*)context->buffer;
    275      1.1  christos 	int		j;
    276      1.1  christos 
    277      1.1  christos 	/* initialize registers with the prev. intermediate value */
    278      1.1  christos 	a = context->state[0];
    279      1.1  christos 	b = context->state[1];
    280      1.1  christos 	c = context->state[2];
    281      1.1  christos 	d = context->state[3];
    282      1.1  christos 	e = context->state[4];
    283      1.1  christos 	f = context->state[5];
    284      1.1  christos 	g = context->state[6];
    285      1.1  christos 	h = context->state[7];
    286      1.1  christos 
    287      1.1  christos 	j = 0;
    288      1.1  christos 	do {
    289      1.1  christos #if BYTE_ORDER == LITTLE_ENDIAN
    290      1.1  christos 		/* Convert TO host byte order */
    291      1.1  christos 		REVERSE64(*data++, W512[j]);
    292      1.1  christos 		/* Apply the SHA-512 compression function to update a..h */
    293      1.1  christos 		T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
    294      1.1  christos #else /* BYTE_ORDER == LITTLE_ENDIAN */
    295      1.1  christos 		/* Apply the SHA-512 compression function to update a..h with copy */
    296      1.1  christos 		T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j] = *data++);
    297      1.1  christos #endif /* BYTE_ORDER == LITTLE_ENDIAN */
    298      1.1  christos 		T2 = Sigma0_512(a) + Maj(a, b, c);
    299      1.1  christos 		h = g;
    300      1.1  christos 		g = f;
    301      1.1  christos 		f = e;
    302      1.1  christos 		e = d + T1;
    303      1.1  christos 		d = c;
    304      1.1  christos 		c = b;
    305      1.1  christos 		b = a;
    306      1.1  christos 		a = T1 + T2;
    307      1.1  christos 
    308      1.1  christos 		j++;
    309      1.1  christos 	} while (j < 16);
    310      1.1  christos 
    311      1.1  christos 	do {
    312      1.1  christos 		/* Part of the message block expansion: */
    313      1.1  christos 		s0 = W512[(j+1)&0x0f];
    314      1.1  christos 		s0 = sigma0_512(s0);
    315      1.1  christos 		s1 = W512[(j+14)&0x0f];
    316      1.1  christos 		s1 =  sigma1_512(s1);
    317      1.1  christos 
    318      1.1  christos 		/* Apply the SHA-512 compression function to update a..h */
    319      1.1  christos 		T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
    320      1.1  christos 		     (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);
    321      1.1  christos 		T2 = Sigma0_512(a) + Maj(a, b, c);
    322      1.1  christos 		h = g;
    323      1.1  christos 		g = f;
    324      1.1  christos 		f = e;
    325      1.1  christos 		e = d + T1;
    326      1.1  christos 		d = c;
    327      1.1  christos 		c = b;
    328      1.1  christos 		b = a;
    329      1.1  christos 		a = T1 + T2;
    330      1.1  christos 
    331      1.1  christos 		j++;
    332      1.1  christos 	} while (j < 80);
    333      1.1  christos 
    334      1.1  christos 	/* Compute the current intermediate hash value */
    335      1.1  christos 	context->state[0] += a;
    336      1.1  christos 	context->state[1] += b;
    337      1.1  christos 	context->state[2] += c;
    338      1.1  christos 	context->state[3] += d;
    339      1.1  christos 	context->state[4] += e;
    340      1.1  christos 	context->state[5] += f;
    341      1.1  christos 	context->state[6] += g;
    342      1.1  christos 	context->state[7] += h;
    343      1.1  christos 
    344      1.1  christos 	/* Clean up */
    345      1.1  christos 	a = b = c = d = e = f = g = h = T1 = T2 = 0;
    346      1.1  christos }
    347      1.1  christos 
    348      1.1  christos void SHA512_Update(SHA512_CTX* context, void *datain, size_t len) {
    349      1.1  christos 	size_t freespace, usedspace;
    350      1.1  christos 	const sha2_byte* data = (const sha2_byte*)datain;
    351      1.1  christos 
    352      1.1  christos 	if (len == 0) {
    353      1.1  christos 		/* Calling with no data is valid - we do nothing */
    354      1.1  christos 		return;
    355      1.1  christos 	}
    356      1.1  christos 
    357      1.1  christos 	/* Sanity check: */
    358      1.1  christos 	assert(context != (SHA512_CTX*)0 && data != (sha2_byte*)0);
    359      1.1  christos 
    360      1.1  christos 	usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
    361      1.1  christos 	if (usedspace > 0) {
    362      1.1  christos 		/* Calculate how much free space is available in the buffer */
    363      1.1  christos 		freespace = SHA512_BLOCK_LENGTH - usedspace;
    364      1.1  christos 
    365      1.1  christos 		if (len >= freespace) {
    366      1.1  christos 			/* Fill the buffer completely and process it */
    367      1.1  christos 			MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace);
    368      1.1  christos 			ADDINC128(context->bitcount, freespace << 3);
    369      1.1  christos 			len -= freespace;
    370      1.1  christos 			data += freespace;
    371      1.1  christos 			SHA512_Transform(context, (sha2_word64*)context->buffer);
    372      1.1  christos 		} else {
    373      1.1  christos 			/* The buffer is not yet full */
    374      1.1  christos 			MEMCPY_BCOPY(&context->buffer[usedspace], data, len);
    375      1.1  christos 			ADDINC128(context->bitcount, len << 3);
    376      1.1  christos 			/* Clean up: */
    377      1.1  christos 			usedspace = freespace = 0;
    378      1.1  christos 			return;
    379      1.1  christos 		}
    380      1.1  christos 	}
    381      1.1  christos 	while (len >= SHA512_BLOCK_LENGTH) {
    382      1.1  christos 		/* Process as many complete blocks as we can */
    383      1.1  christos 		SHA512_Transform(context, (sha2_word64*)data);
    384      1.1  christos 		ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3);
    385      1.1  christos 		len -= SHA512_BLOCK_LENGTH;
    386      1.1  christos 		data += SHA512_BLOCK_LENGTH;
    387      1.1  christos 	}
    388      1.1  christos 	if (len > 0) {
    389      1.1  christos 		/* There's left-overs, so save 'em */
    390      1.1  christos 		MEMCPY_BCOPY(context->buffer, data, len);
    391      1.1  christos 		ADDINC128(context->bitcount, len << 3);
    392      1.1  christos 	}
    393      1.1  christos 	/* Clean up: */
    394      1.1  christos 	usedspace = freespace = 0;
    395      1.1  christos }
    396      1.1  christos 
    397      1.1  christos static void SHA512_Last(SHA512_CTX* context) {
    398      1.1  christos 	size_t usedspace;
    399      1.1  christos 	ldns_sha2_buffer_union cast_var;
    400      1.1  christos 
    401      1.1  christos 	usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
    402      1.1  christos #if BYTE_ORDER == LITTLE_ENDIAN
    403      1.1  christos 	/* Convert FROM host byte order */
    404      1.1  christos 	REVERSE64(context->bitcount[0],context->bitcount[0]);
    405      1.1  christos 	REVERSE64(context->bitcount[1],context->bitcount[1]);
    406      1.1  christos #endif
    407      1.1  christos 	if (usedspace > 0) {
    408      1.1  christos 		/* Begin padding with a 1 bit: */
    409      1.1  christos 		context->buffer[usedspace++] = 0x80;
    410      1.1  christos 
    411      1.1  christos 		if (usedspace <= SHA512_SHORT_BLOCK_LENGTH) {
    412      1.1  christos 			/* Set-up for the last transform: */
    413      1.1  christos 			MEMSET_BZERO(&context->buffer[usedspace], SHA512_SHORT_BLOCK_LENGTH - usedspace);
    414      1.1  christos 		} else {
    415      1.1  christos 			if (usedspace < SHA512_BLOCK_LENGTH) {
    416      1.1  christos 				MEMSET_BZERO(&context->buffer[usedspace], SHA512_BLOCK_LENGTH - usedspace);
    417      1.1  christos 			}
    418      1.1  christos 			/* Do second-to-last transform: */
    419      1.1  christos 			SHA512_Transform(context, (sha2_word64*)context->buffer);
    420      1.1  christos 
    421      1.1  christos 			/* And set-up for the last transform: */
    422      1.1  christos 			MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH - 2);
    423      1.1  christos 		}
    424      1.1  christos 	} else {
    425      1.1  christos 		/* Prepare for final transform: */
    426      1.1  christos 		MEMSET_BZERO(context->buffer, SHA512_SHORT_BLOCK_LENGTH);
    427      1.1  christos 
    428      1.1  christos 		/* Begin padding with a 1 bit: */
    429      1.1  christos 		*context->buffer = 0x80;
    430      1.1  christos 	}
    431      1.1  christos 	/* Store the length of input data (in bits): */
    432      1.1  christos 	cast_var.theChars = context->buffer;
    433      1.1  christos 	cast_var.theLongs[SHA512_SHORT_BLOCK_LENGTH / 8] = context->bitcount[1];
    434      1.1  christos 	cast_var.theLongs[SHA512_SHORT_BLOCK_LENGTH / 8 + 1] = context->bitcount[0];
    435      1.1  christos 
    436      1.1  christos 	/* final transform: */
    437      1.1  christos 	SHA512_Transform(context, (sha2_word64*)context->buffer);
    438      1.1  christos }
    439      1.1  christos 
    440      1.1  christos void SHA512_Final(sha2_byte digest[], SHA512_CTX* context) {
    441      1.1  christos 	sha2_word64	*d = (sha2_word64*)digest;
    442      1.1  christos 
    443      1.1  christos 	/* Sanity check: */
    444      1.1  christos 	assert(context != (SHA512_CTX*)0);
    445      1.1  christos 
    446      1.1  christos 	/* If no digest buffer is passed, we don't bother doing this: */
    447      1.1  christos 	if (digest != (sha2_byte*)0) {
    448      1.1  christos 		SHA512_Last(context);
    449      1.1  christos 
    450      1.1  christos 		/* Save the hash data for output: */
    451      1.1  christos #if BYTE_ORDER == LITTLE_ENDIAN
    452      1.1  christos 		{
    453      1.1  christos 			/* Convert TO host byte order */
    454      1.1  christos 			int	j;
    455      1.1  christos 			for (j = 0; j < 8; j++) {
    456      1.1  christos 				REVERSE64(context->state[j],context->state[j]);
    457      1.1  christos 				*d++ = context->state[j];
    458      1.1  christos 			}
    459      1.1  christos 		}
    460      1.1  christos #else
    461      1.1  christos 		MEMCPY_BCOPY(d, context->state, SHA512_DIGEST_LENGTH);
    462      1.1  christos #endif
    463      1.1  christos 	}
    464      1.1  christos 
    465      1.1  christos 	/* Zero out state data */
    466      1.1  christos 	MEMSET_BZERO(context, sizeof(SHA512_CTX));
    467      1.1  christos }
    468      1.1  christos 
    469      1.1  christos unsigned char *
    470      1.1  christos SHA512(void *data, unsigned int data_len, unsigned char *digest)
    471      1.1  christos {
    472      1.1  christos     SHA512_CTX ctx;
    473      1.1  christos     SHA512_Init(&ctx);
    474      1.1  christos     SHA512_Update(&ctx, data, data_len);
    475      1.1  christos     SHA512_Final(digest, &ctx);
    476      1.1  christos     return digest;
    477      1.1  christos }
    478