sha2.c revision 162852
1162852Sdes/*	$OpenBSD: sha2.c,v 1.11 2005/08/08 08:05:35 espie Exp $	*/
2162852Sdes
3162852Sdes/*
4162852Sdes * FILE:	sha2.c
5162852Sdes * AUTHOR:	Aaron D. Gifford <me@aarongifford.com>
6162852Sdes *
7162852Sdes * Copyright (c) 2000-2001, Aaron D. Gifford
8162852Sdes * All rights reserved.
9162852Sdes *
10162852Sdes * Redistribution and use in source and binary forms, with or without
11162852Sdes * modification, are permitted provided that the following conditions
12162852Sdes * are met:
13162852Sdes * 1. Redistributions of source code must retain the above copyright
14162852Sdes *    notice, this list of conditions and the following disclaimer.
15162852Sdes * 2. Redistributions in binary form must reproduce the above copyright
16162852Sdes *    notice, this list of conditions and the following disclaimer in the
17162852Sdes *    documentation and/or other materials provided with the distribution.
18162852Sdes * 3. Neither the name of the copyright holder nor the names of contributors
19162852Sdes *    may be used to endorse or promote products derived from this software
20162852Sdes *    without specific prior written permission.
21162852Sdes *
22162852Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
23162852Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24162852Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25162852Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
26162852Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27162852Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28162852Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29162852Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30162852Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31162852Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32162852Sdes * SUCH DAMAGE.
33162852Sdes *
34162852Sdes * $From: sha2.c,v 1.1 2001/11/08 00:01:51 adg Exp adg $
35162852Sdes */
36162852Sdes
37162852Sdes/* OPENBSD ORIGINAL: lib/libc/hash/sha2.c */
38162852Sdes
39162852Sdes#include "includes.h"
40162852Sdes
41162852Sdes#include <openssl/opensslv.h>
42162852Sdes
43162852Sdes#if !defined(HAVE_EVP_SHA256) && !defined(HAVE_SHA256_UPDATE) && \
44162852Sdes    (OPENSSL_VERSION_NUMBER >= 0x00907000L)
45162852Sdes#include <sys/types.h>
46162852Sdes#include <string.h>
47162852Sdes#include "sha2.h"
48162852Sdes
49162852Sdes/*
50162852Sdes * UNROLLED TRANSFORM LOOP NOTE:
51162852Sdes * You can define SHA2_UNROLL_TRANSFORM to use the unrolled transform
52162852Sdes * loop version for the hash transform rounds (defined using macros
53162852Sdes * later in this file).  Either define on the command line, for example:
54162852Sdes *
55162852Sdes *   cc -DSHA2_UNROLL_TRANSFORM -o sha2 sha2.c sha2prog.c
56162852Sdes *
57162852Sdes * or define below:
58162852Sdes *
59162852Sdes *   #define SHA2_UNROLL_TRANSFORM
60162852Sdes *
61162852Sdes */
62162852Sdes
63162852Sdes/*** SHA-256/384/512 Machine Architecture Definitions *****************/
64162852Sdes/*
65162852Sdes * BYTE_ORDER NOTE:
66162852Sdes *
67162852Sdes * Please make sure that your system defines BYTE_ORDER.  If your
68162852Sdes * architecture is little-endian, make sure it also defines
69162852Sdes * LITTLE_ENDIAN and that the two (BYTE_ORDER and LITTLE_ENDIAN) are
70162852Sdes * equivilent.
71162852Sdes *
72162852Sdes * If your system does not define the above, then you can do so by
73162852Sdes * hand like this:
74162852Sdes *
75162852Sdes *   #define LITTLE_ENDIAN 1234
76162852Sdes *   #define BIG_ENDIAN    4321
77162852Sdes *
78162852Sdes * And for little-endian machines, add:
79162852Sdes *
80162852Sdes *   #define BYTE_ORDER LITTLE_ENDIAN
81162852Sdes *
82162852Sdes * Or for big-endian machines:
83162852Sdes *
84162852Sdes *   #define BYTE_ORDER BIG_ENDIAN
85162852Sdes *
86162852Sdes * The FreeBSD machine this was written on defines BYTE_ORDER
87162852Sdes * appropriately by including <sys/types.h> (which in turn includes
88162852Sdes * <machine/endian.h> where the appropriate definitions are actually
89162852Sdes * made).
90162852Sdes */
91162852Sdes#if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN)
92162852Sdes#error Define BYTE_ORDER to be equal to either LITTLE_ENDIAN or BIG_ENDIAN
93162852Sdes#endif
94162852Sdes
95162852Sdes
96162852Sdes/*** SHA-256/384/512 Various Length Definitions ***********************/
97162852Sdes/* NOTE: Most of these are in sha2.h */
98162852Sdes#define SHA256_SHORT_BLOCK_LENGTH	(SHA256_BLOCK_LENGTH - 8)
99162852Sdes#define SHA384_SHORT_BLOCK_LENGTH	(SHA384_BLOCK_LENGTH - 16)
100162852Sdes#define SHA512_SHORT_BLOCK_LENGTH	(SHA512_BLOCK_LENGTH - 16)
101162852Sdes
102162852Sdes/*** ENDIAN SPECIFIC COPY MACROS **************************************/
103162852Sdes#define BE_8_TO_32(dst, cp) do {					\
104162852Sdes	(dst) = (u_int32_t)(cp)[3] | ((u_int32_t)(cp)[2] << 8) |	\
105162852Sdes	    ((u_int32_t)(cp)[1] << 16) | ((u_int32_t)(cp)[0] << 24);	\
106162852Sdes} while(0)
107162852Sdes
108162852Sdes#define BE_8_TO_64(dst, cp) do {					\
109162852Sdes	(dst) = (u_int64_t)(cp)[7] | ((u_int64_t)(cp)[6] << 8) |	\
110162852Sdes	    ((u_int64_t)(cp)[5] << 16) | ((u_int64_t)(cp)[4] << 24) |	\
111162852Sdes	    ((u_int64_t)(cp)[3] << 32) | ((u_int64_t)(cp)[2] << 40) |	\
112162852Sdes	    ((u_int64_t)(cp)[1] << 48) | ((u_int64_t)(cp)[0] << 56);	\
113162852Sdes} while (0)
114162852Sdes
115162852Sdes#define BE_64_TO_8(cp, src) do {					\
116162852Sdes	(cp)[0] = (src) >> 56;						\
117162852Sdes        (cp)[1] = (src) >> 48;						\
118162852Sdes	(cp)[2] = (src) >> 40;						\
119162852Sdes	(cp)[3] = (src) >> 32;						\
120162852Sdes	(cp)[4] = (src) >> 24;						\
121162852Sdes	(cp)[5] = (src) >> 16;						\
122162852Sdes	(cp)[6] = (src) >> 8;						\
123162852Sdes	(cp)[7] = (src);						\
124162852Sdes} while (0)
125162852Sdes
126162852Sdes#define BE_32_TO_8(cp, src) do {					\
127162852Sdes	(cp)[0] = (src) >> 24;						\
128162852Sdes	(cp)[1] = (src) >> 16;						\
129162852Sdes	(cp)[2] = (src) >> 8;						\
130162852Sdes	(cp)[3] = (src);						\
131162852Sdes} while (0)
132162852Sdes
133162852Sdes/*
134162852Sdes * Macro for incrementally adding the unsigned 64-bit integer n to the
135162852Sdes * unsigned 128-bit integer (represented using a two-element array of
136162852Sdes * 64-bit words):
137162852Sdes */
138162852Sdes#define ADDINC128(w,n) do {						\
139162852Sdes	(w)[0] += (u_int64_t)(n);					\
140162852Sdes	if ((w)[0] < (n)) {						\
141162852Sdes		(w)[1]++;						\
142162852Sdes	}								\
143162852Sdes} while (0)
144162852Sdes
145162852Sdes/*** THE SIX LOGICAL FUNCTIONS ****************************************/
146162852Sdes/*
147162852Sdes * Bit shifting and rotation (used by the six SHA-XYZ logical functions:
148162852Sdes *
149162852Sdes *   NOTE:  The naming of R and S appears backwards here (R is a SHIFT and
150162852Sdes *   S is a ROTATION) because the SHA-256/384/512 description document
151162852Sdes *   (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this
152162852Sdes *   same "backwards" definition.
153162852Sdes */
154162852Sdes/* Shift-right (used in SHA-256, SHA-384, and SHA-512): */
155162852Sdes#define R(b,x) 		((x) >> (b))
156162852Sdes/* 32-bit Rotate-right (used in SHA-256): */
157162852Sdes#define S32(b,x)	(((x) >> (b)) | ((x) << (32 - (b))))
158162852Sdes/* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
159162852Sdes#define S64(b,x)	(((x) >> (b)) | ((x) << (64 - (b))))
160162852Sdes
161162852Sdes/* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */
162162852Sdes#define Ch(x,y,z)	(((x) & (y)) ^ ((~(x)) & (z)))
163162852Sdes#define Maj(x,y,z)	(((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
164162852Sdes
165162852Sdes/* Four of six logical functions used in SHA-256: */
166162852Sdes#define Sigma0_256(x)	(S32(2,  (x)) ^ S32(13, (x)) ^ S32(22, (x)))
167162852Sdes#define Sigma1_256(x)	(S32(6,  (x)) ^ S32(11, (x)) ^ S32(25, (x)))
168162852Sdes#define sigma0_256(x)	(S32(7,  (x)) ^ S32(18, (x)) ^ R(3 ,   (x)))
169162852Sdes#define sigma1_256(x)	(S32(17, (x)) ^ S32(19, (x)) ^ R(10,   (x)))
170162852Sdes
171162852Sdes/* Four of six logical functions used in SHA-384 and SHA-512: */
172162852Sdes#define Sigma0_512(x)	(S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))
173162852Sdes#define Sigma1_512(x)	(S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))
174162852Sdes#define sigma0_512(x)	(S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7,   (x)))
175162852Sdes#define sigma1_512(x)	(S64(19, (x)) ^ S64(61, (x)) ^ R( 6,   (x)))
176162852Sdes
177162852Sdes
178162852Sdes/*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
179162852Sdes/* Hash constant words K for SHA-256: */
180162852Sdesconst static u_int32_t K256[64] = {
181162852Sdes	0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
182162852Sdes	0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
183162852Sdes	0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
184162852Sdes	0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,
185162852Sdes	0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
186162852Sdes	0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
187162852Sdes	0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL,
188162852Sdes	0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL,
189162852Sdes	0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,
190162852Sdes	0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
191162852Sdes	0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL,
192162852Sdes	0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
193162852Sdes	0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL,
194162852Sdes	0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,
195162852Sdes	0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
196162852Sdes	0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
197162852Sdes};
198162852Sdes
199162852Sdes/* Initial hash value H for SHA-256: */
200162852Sdesconst static u_int32_t sha256_initial_hash_value[8] = {
201162852Sdes	0x6a09e667UL,
202162852Sdes	0xbb67ae85UL,
203162852Sdes	0x3c6ef372UL,
204162852Sdes	0xa54ff53aUL,
205162852Sdes	0x510e527fUL,
206162852Sdes	0x9b05688cUL,
207162852Sdes	0x1f83d9abUL,
208162852Sdes	0x5be0cd19UL
209162852Sdes};
210162852Sdes
211162852Sdes/* Hash constant words K for SHA-384 and SHA-512: */
212162852Sdesconst static u_int64_t K512[80] = {
213162852Sdes	0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
214162852Sdes	0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
215162852Sdes	0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
216162852Sdes	0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
217162852Sdes	0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
218162852Sdes	0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
219162852Sdes	0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
220162852Sdes	0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
221162852Sdes	0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
222162852Sdes	0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
223162852Sdes	0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
224162852Sdes	0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
225162852Sdes	0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
226162852Sdes	0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
227162852Sdes	0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
228162852Sdes	0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
229162852Sdes	0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
230162852Sdes	0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
231162852Sdes	0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
232162852Sdes	0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
233162852Sdes	0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
234162852Sdes	0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
235162852Sdes	0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
236162852Sdes	0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
237162852Sdes	0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
238162852Sdes	0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
239162852Sdes	0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
240162852Sdes	0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
241162852Sdes	0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
242162852Sdes	0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
243162852Sdes	0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
244162852Sdes	0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
245162852Sdes	0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
246162852Sdes	0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
247162852Sdes	0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
248162852Sdes	0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
249162852Sdes	0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
250162852Sdes	0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
251162852Sdes	0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
252162852Sdes	0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
253162852Sdes};
254162852Sdes
255162852Sdes/* Initial hash value H for SHA-384 */
256162852Sdesconst static u_int64_t sha384_initial_hash_value[8] = {
257162852Sdes	0xcbbb9d5dc1059ed8ULL,
258162852Sdes	0x629a292a367cd507ULL,
259162852Sdes	0x9159015a3070dd17ULL,
260162852Sdes	0x152fecd8f70e5939ULL,
261162852Sdes	0x67332667ffc00b31ULL,
262162852Sdes	0x8eb44a8768581511ULL,
263162852Sdes	0xdb0c2e0d64f98fa7ULL,
264162852Sdes	0x47b5481dbefa4fa4ULL
265162852Sdes};
266162852Sdes
267162852Sdes/* Initial hash value H for SHA-512 */
268162852Sdesconst static u_int64_t sha512_initial_hash_value[8] = {
269162852Sdes	0x6a09e667f3bcc908ULL,
270162852Sdes	0xbb67ae8584caa73bULL,
271162852Sdes	0x3c6ef372fe94f82bULL,
272162852Sdes	0xa54ff53a5f1d36f1ULL,
273162852Sdes	0x510e527fade682d1ULL,
274162852Sdes	0x9b05688c2b3e6c1fULL,
275162852Sdes	0x1f83d9abfb41bd6bULL,
276162852Sdes	0x5be0cd19137e2179ULL
277162852Sdes};
278162852Sdes
279162852Sdes
280162852Sdes/*** SHA-256: *********************************************************/
281162852Sdesvoid
282162852SdesSHA256_Init(SHA256_CTX *context)
283162852Sdes{
284162852Sdes	if (context == NULL)
285162852Sdes		return;
286162852Sdes	memcpy(context->state, sha256_initial_hash_value,
287162852Sdes	    sizeof(sha256_initial_hash_value));
288162852Sdes	memset(context->buffer, 0, sizeof(context->buffer));
289162852Sdes	context->bitcount = 0;
290162852Sdes}
291162852Sdes
292162852Sdes#ifdef SHA2_UNROLL_TRANSFORM
293162852Sdes
294162852Sdes/* Unrolled SHA-256 round macros: */
295162852Sdes
296162852Sdes#define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) do {				    \
297162852Sdes	BE_8_TO_32(W256[j], data);					    \
298162852Sdes	data += 4;							    \
299162852Sdes	T1 = (h) + Sigma1_256((e)) + Ch((e), (f), (g)) + K256[j] + W256[j]; \
300162852Sdes	(d) += T1;							    \
301162852Sdes	(h) = T1 + Sigma0_256((a)) + Maj((a), (b), (c));		    \
302162852Sdes	j++;								    \
303162852Sdes} while(0)
304162852Sdes
305162852Sdes#define ROUND256(a,b,c,d,e,f,g,h) do {					    \
306162852Sdes	s0 = W256[(j+1)&0x0f];						    \
307162852Sdes	s0 = sigma0_256(s0);						    \
308162852Sdes	s1 = W256[(j+14)&0x0f];						    \
309162852Sdes	s1 = sigma1_256(s1);						    \
310162852Sdes	T1 = (h) + Sigma1_256((e)) + Ch((e), (f), (g)) + K256[j] +	    \
311162852Sdes	     (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0);		    \
312162852Sdes	(d) += T1;							    \
313162852Sdes	(h) = T1 + Sigma0_256((a)) + Maj((a), (b), (c));		    \
314162852Sdes	j++;								    \
315162852Sdes} while(0)
316162852Sdes
317162852Sdesvoid
318162852SdesSHA256_Transform(u_int32_t state[8], const u_int8_t data[SHA256_BLOCK_LENGTH])
319162852Sdes{
320162852Sdes	u_int32_t	a, b, c, d, e, f, g, h, s0, s1;
321162852Sdes	u_int32_t	T1, W256[16];
322162852Sdes	int		j;
323162852Sdes
324162852Sdes	/* Initialize registers with the prev. intermediate value */
325162852Sdes	a = state[0];
326162852Sdes	b = state[1];
327162852Sdes	c = state[2];
328162852Sdes	d = state[3];
329162852Sdes	e = state[4];
330162852Sdes	f = state[5];
331162852Sdes	g = state[6];
332162852Sdes	h = state[7];
333162852Sdes
334162852Sdes	j = 0;
335162852Sdes	do {
336162852Sdes		/* Rounds 0 to 15 (unrolled): */
337162852Sdes		ROUND256_0_TO_15(a,b,c,d,e,f,g,h);
338162852Sdes		ROUND256_0_TO_15(h,a,b,c,d,e,f,g);
339162852Sdes		ROUND256_0_TO_15(g,h,a,b,c,d,e,f);
340162852Sdes		ROUND256_0_TO_15(f,g,h,a,b,c,d,e);
341162852Sdes		ROUND256_0_TO_15(e,f,g,h,a,b,c,d);
342162852Sdes		ROUND256_0_TO_15(d,e,f,g,h,a,b,c);
343162852Sdes		ROUND256_0_TO_15(c,d,e,f,g,h,a,b);
344162852Sdes		ROUND256_0_TO_15(b,c,d,e,f,g,h,a);
345162852Sdes	} while (j < 16);
346162852Sdes
347162852Sdes	/* Now for the remaining rounds up to 63: */
348162852Sdes	do {
349162852Sdes		ROUND256(a,b,c,d,e,f,g,h);
350162852Sdes		ROUND256(h,a,b,c,d,e,f,g);
351162852Sdes		ROUND256(g,h,a,b,c,d,e,f);
352162852Sdes		ROUND256(f,g,h,a,b,c,d,e);
353162852Sdes		ROUND256(e,f,g,h,a,b,c,d);
354162852Sdes		ROUND256(d,e,f,g,h,a,b,c);
355162852Sdes		ROUND256(c,d,e,f,g,h,a,b);
356162852Sdes		ROUND256(b,c,d,e,f,g,h,a);
357162852Sdes	} while (j < 64);
358162852Sdes
359162852Sdes	/* Compute the current intermediate hash value */
360162852Sdes	state[0] += a;
361162852Sdes	state[1] += b;
362162852Sdes	state[2] += c;
363162852Sdes	state[3] += d;
364162852Sdes	state[4] += e;
365162852Sdes	state[5] += f;
366162852Sdes	state[6] += g;
367162852Sdes	state[7] += h;
368162852Sdes
369162852Sdes	/* Clean up */
370162852Sdes	a = b = c = d = e = f = g = h = T1 = 0;
371162852Sdes}
372162852Sdes
373162852Sdes#else /* SHA2_UNROLL_TRANSFORM */
374162852Sdes
375162852Sdesvoid
376162852SdesSHA256_Transform(u_int32_t state[8], const u_int8_t data[SHA256_BLOCK_LENGTH])
377162852Sdes{
378162852Sdes	u_int32_t	a, b, c, d, e, f, g, h, s0, s1;
379162852Sdes	u_int32_t	T1, T2, W256[16];
380162852Sdes	int		j;
381162852Sdes
382162852Sdes	/* Initialize registers with the prev. intermediate value */
383162852Sdes	a = state[0];
384162852Sdes	b = state[1];
385162852Sdes	c = state[2];
386162852Sdes	d = state[3];
387162852Sdes	e = state[4];
388162852Sdes	f = state[5];
389162852Sdes	g = state[6];
390162852Sdes	h = state[7];
391162852Sdes
392162852Sdes	j = 0;
393162852Sdes	do {
394162852Sdes		BE_8_TO_32(W256[j], data);
395162852Sdes		data += 4;
396162852Sdes		/* Apply the SHA-256 compression function to update a..h */
397162852Sdes		T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j];
398162852Sdes		T2 = Sigma0_256(a) + Maj(a, b, c);
399162852Sdes		h = g;
400162852Sdes		g = f;
401162852Sdes		f = e;
402162852Sdes		e = d + T1;
403162852Sdes		d = c;
404162852Sdes		c = b;
405162852Sdes		b = a;
406162852Sdes		a = T1 + T2;
407162852Sdes
408162852Sdes		j++;
409162852Sdes	} while (j < 16);
410162852Sdes
411162852Sdes	do {
412162852Sdes		/* Part of the message block expansion: */
413162852Sdes		s0 = W256[(j+1)&0x0f];
414162852Sdes		s0 = sigma0_256(s0);
415162852Sdes		s1 = W256[(j+14)&0x0f];
416162852Sdes		s1 = sigma1_256(s1);
417162852Sdes
418162852Sdes		/* Apply the SHA-256 compression function to update a..h */
419162852Sdes		T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] +
420162852Sdes		     (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0);
421162852Sdes		T2 = Sigma0_256(a) + Maj(a, b, c);
422162852Sdes		h = g;
423162852Sdes		g = f;
424162852Sdes		f = e;
425162852Sdes		e = d + T1;
426162852Sdes		d = c;
427162852Sdes		c = b;
428162852Sdes		b = a;
429162852Sdes		a = T1 + T2;
430162852Sdes
431162852Sdes		j++;
432162852Sdes	} while (j < 64);
433162852Sdes
434162852Sdes	/* Compute the current intermediate hash value */
435162852Sdes	state[0] += a;
436162852Sdes	state[1] += b;
437162852Sdes	state[2] += c;
438162852Sdes	state[3] += d;
439162852Sdes	state[4] += e;
440162852Sdes	state[5] += f;
441162852Sdes	state[6] += g;
442162852Sdes	state[7] += h;
443162852Sdes
444162852Sdes	/* Clean up */
445162852Sdes	a = b = c = d = e = f = g = h = T1 = T2 = 0;
446162852Sdes}
447162852Sdes
448162852Sdes#endif /* SHA2_UNROLL_TRANSFORM */
449162852Sdes
450162852Sdesvoid
451162852SdesSHA256_Update(SHA256_CTX *context, const u_int8_t *data, size_t len)
452162852Sdes{
453162852Sdes	size_t	freespace, usedspace;
454162852Sdes
455162852Sdes	/* Calling with no data is valid (we do nothing) */
456162852Sdes	if (len == 0)
457162852Sdes		return;
458162852Sdes
459162852Sdes	usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
460162852Sdes	if (usedspace > 0) {
461162852Sdes		/* Calculate how much free space is available in the buffer */
462162852Sdes		freespace = SHA256_BLOCK_LENGTH - usedspace;
463162852Sdes
464162852Sdes		if (len >= freespace) {
465162852Sdes			/* Fill the buffer completely and process it */
466162852Sdes			memcpy(&context->buffer[usedspace], data, freespace);
467162852Sdes			context->bitcount += freespace << 3;
468162852Sdes			len -= freespace;
469162852Sdes			data += freespace;
470162852Sdes			SHA256_Transform(context->state, context->buffer);
471162852Sdes		} else {
472162852Sdes			/* The buffer is not yet full */
473162852Sdes			memcpy(&context->buffer[usedspace], data, len);
474162852Sdes			context->bitcount += len << 3;
475162852Sdes			/* Clean up: */
476162852Sdes			usedspace = freespace = 0;
477162852Sdes			return;
478162852Sdes		}
479162852Sdes	}
480162852Sdes	while (len >= SHA256_BLOCK_LENGTH) {
481162852Sdes		/* Process as many complete blocks as we can */
482162852Sdes		SHA256_Transform(context->state, data);
483162852Sdes		context->bitcount += SHA256_BLOCK_LENGTH << 3;
484162852Sdes		len -= SHA256_BLOCK_LENGTH;
485162852Sdes		data += SHA256_BLOCK_LENGTH;
486162852Sdes	}
487162852Sdes	if (len > 0) {
488162852Sdes		/* There's left-overs, so save 'em */
489162852Sdes		memcpy(context->buffer, data, len);
490162852Sdes		context->bitcount += len << 3;
491162852Sdes	}
492162852Sdes	/* Clean up: */
493162852Sdes	usedspace = freespace = 0;
494162852Sdes}
495162852Sdes
496162852Sdesvoid
497162852SdesSHA256_Pad(SHA256_CTX *context)
498162852Sdes{
499162852Sdes	unsigned int	usedspace;
500162852Sdes
501162852Sdes	usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
502162852Sdes	if (usedspace > 0) {
503162852Sdes		/* Begin padding with a 1 bit: */
504162852Sdes		context->buffer[usedspace++] = 0x80;
505162852Sdes
506162852Sdes		if (usedspace <= SHA256_SHORT_BLOCK_LENGTH) {
507162852Sdes			/* Set-up for the last transform: */
508162852Sdes			memset(&context->buffer[usedspace], 0,
509162852Sdes			    SHA256_SHORT_BLOCK_LENGTH - usedspace);
510162852Sdes		} else {
511162852Sdes			if (usedspace < SHA256_BLOCK_LENGTH) {
512162852Sdes				memset(&context->buffer[usedspace], 0,
513162852Sdes				    SHA256_BLOCK_LENGTH - usedspace);
514162852Sdes			}
515162852Sdes			/* Do second-to-last transform: */
516162852Sdes			SHA256_Transform(context->state, context->buffer);
517162852Sdes
518162852Sdes			/* Prepare for last transform: */
519162852Sdes			memset(context->buffer, 0, SHA256_SHORT_BLOCK_LENGTH);
520162852Sdes		}
521162852Sdes	} else {
522162852Sdes		/* Set-up for the last transform: */
523162852Sdes		memset(context->buffer, 0, SHA256_SHORT_BLOCK_LENGTH);
524162852Sdes
525162852Sdes		/* Begin padding with a 1 bit: */
526162852Sdes		*context->buffer = 0x80;
527162852Sdes	}
528162852Sdes	/* Store the length of input data (in bits) in big endian format: */
529162852Sdes	BE_64_TO_8(&context->buffer[SHA256_SHORT_BLOCK_LENGTH],
530162852Sdes	    context->bitcount);
531162852Sdes
532162852Sdes	/* Final transform: */
533162852Sdes	SHA256_Transform(context->state, context->buffer);
534162852Sdes
535162852Sdes	/* Clean up: */
536162852Sdes	usedspace = 0;
537162852Sdes}
538162852Sdes
539162852Sdesvoid
540162852SdesSHA256_Final(u_int8_t digest[SHA256_DIGEST_LENGTH], SHA256_CTX *context)
541162852Sdes{
542162852Sdes	SHA256_Pad(context);
543162852Sdes
544162852Sdes	/* If no digest buffer is passed, we don't bother doing this: */
545162852Sdes	if (digest != NULL) {
546162852Sdes#if BYTE_ORDER == LITTLE_ENDIAN
547162852Sdes		int	i;
548162852Sdes
549162852Sdes		/* Convert TO host byte order */
550162852Sdes		for (i = 0; i < 8; i++)
551162852Sdes			BE_32_TO_8(digest + i * 4, context->state[i]);
552162852Sdes#else
553162852Sdes		memcpy(digest, context->state, SHA256_DIGEST_LENGTH);
554162852Sdes#endif
555162852Sdes		memset(context, 0, sizeof(*context));
556162852Sdes	}
557162852Sdes}
558162852Sdes
559162852Sdes
560162852Sdes/*** SHA-512: *********************************************************/
561162852Sdesvoid
562162852SdesSHA512_Init(SHA512_CTX *context)
563162852Sdes{
564162852Sdes	if (context == NULL)
565162852Sdes		return;
566162852Sdes	memcpy(context->state, sha512_initial_hash_value,
567162852Sdes	    sizeof(sha512_initial_hash_value));
568162852Sdes	memset(context->buffer, 0, sizeof(context->buffer));
569162852Sdes	context->bitcount[0] = context->bitcount[1] =  0;
570162852Sdes}
571162852Sdes
572162852Sdes#ifdef SHA2_UNROLL_TRANSFORM
573162852Sdes
574162852Sdes/* Unrolled SHA-512 round macros: */
575162852Sdes
576162852Sdes#define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) do {				    \
577162852Sdes	BE_8_TO_64(W512[j], data);					    \
578162852Sdes	data += 8;							    \
579162852Sdes	T1 = (h) + Sigma1_512((e)) + Ch((e), (f), (g)) + K512[j] + W512[j]; \
580162852Sdes	(d) += T1;							    \
581162852Sdes	(h) = T1 + Sigma0_512((a)) + Maj((a), (b), (c));		    \
582162852Sdes	j++;								    \
583162852Sdes} while(0)
584162852Sdes
585162852Sdes
586162852Sdes#define ROUND512(a,b,c,d,e,f,g,h) do {					    \
587162852Sdes	s0 = W512[(j+1)&0x0f];						    \
588162852Sdes	s0 = sigma0_512(s0);						    \
589162852Sdes	s1 = W512[(j+14)&0x0f];						    \
590162852Sdes	s1 = sigma1_512(s1);						    \
591162852Sdes	T1 = (h) + Sigma1_512((e)) + Ch((e), (f), (g)) + K512[j] +	    \
592162852Sdes             (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);		    \
593162852Sdes	(d) += T1;							    \
594162852Sdes	(h) = T1 + Sigma0_512((a)) + Maj((a), (b), (c));		    \
595162852Sdes	j++;								    \
596162852Sdes} while(0)
597162852Sdes
598162852Sdesvoid
599162852SdesSHA512_Transform(u_int64_t state[8], const u_int8_t data[SHA512_BLOCK_LENGTH])
600162852Sdes{
601162852Sdes	u_int64_t	a, b, c, d, e, f, g, h, s0, s1;
602162852Sdes	u_int64_t	T1, W512[16];
603162852Sdes	int		j;
604162852Sdes
605162852Sdes	/* Initialize registers with the prev. intermediate value */
606162852Sdes	a = state[0];
607162852Sdes	b = state[1];
608162852Sdes	c = state[2];
609162852Sdes	d = state[3];
610162852Sdes	e = state[4];
611162852Sdes	f = state[5];
612162852Sdes	g = state[6];
613162852Sdes	h = state[7];
614162852Sdes
615162852Sdes	j = 0;
616162852Sdes	do {
617162852Sdes		/* Rounds 0 to 15 (unrolled): */
618162852Sdes		ROUND512_0_TO_15(a,b,c,d,e,f,g,h);
619162852Sdes		ROUND512_0_TO_15(h,a,b,c,d,e,f,g);
620162852Sdes		ROUND512_0_TO_15(g,h,a,b,c,d,e,f);
621162852Sdes		ROUND512_0_TO_15(f,g,h,a,b,c,d,e);
622162852Sdes		ROUND512_0_TO_15(e,f,g,h,a,b,c,d);
623162852Sdes		ROUND512_0_TO_15(d,e,f,g,h,a,b,c);
624162852Sdes		ROUND512_0_TO_15(c,d,e,f,g,h,a,b);
625162852Sdes		ROUND512_0_TO_15(b,c,d,e,f,g,h,a);
626162852Sdes	} while (j < 16);
627162852Sdes
628162852Sdes	/* Now for the remaining rounds up to 79: */
629162852Sdes	do {
630162852Sdes		ROUND512(a,b,c,d,e,f,g,h);
631162852Sdes		ROUND512(h,a,b,c,d,e,f,g);
632162852Sdes		ROUND512(g,h,a,b,c,d,e,f);
633162852Sdes		ROUND512(f,g,h,a,b,c,d,e);
634162852Sdes		ROUND512(e,f,g,h,a,b,c,d);
635162852Sdes		ROUND512(d,e,f,g,h,a,b,c);
636162852Sdes		ROUND512(c,d,e,f,g,h,a,b);
637162852Sdes		ROUND512(b,c,d,e,f,g,h,a);
638162852Sdes	} while (j < 80);
639162852Sdes
640162852Sdes	/* Compute the current intermediate hash value */
641162852Sdes	state[0] += a;
642162852Sdes	state[1] += b;
643162852Sdes	state[2] += c;
644162852Sdes	state[3] += d;
645162852Sdes	state[4] += e;
646162852Sdes	state[5] += f;
647162852Sdes	state[6] += g;
648162852Sdes	state[7] += h;
649162852Sdes
650162852Sdes	/* Clean up */
651162852Sdes	a = b = c = d = e = f = g = h = T1 = 0;
652162852Sdes}
653162852Sdes
654162852Sdes#else /* SHA2_UNROLL_TRANSFORM */
655162852Sdes
656162852Sdesvoid
657162852SdesSHA512_Transform(u_int64_t state[8], const u_int8_t data[SHA512_BLOCK_LENGTH])
658162852Sdes{
659162852Sdes	u_int64_t	a, b, c, d, e, f, g, h, s0, s1;
660162852Sdes	u_int64_t	T1, T2, W512[16];
661162852Sdes	int		j;
662162852Sdes
663162852Sdes	/* Initialize registers with the prev. intermediate value */
664162852Sdes	a = state[0];
665162852Sdes	b = state[1];
666162852Sdes	c = state[2];
667162852Sdes	d = state[3];
668162852Sdes	e = state[4];
669162852Sdes	f = state[5];
670162852Sdes	g = state[6];
671162852Sdes	h = state[7];
672162852Sdes
673162852Sdes	j = 0;
674162852Sdes	do {
675162852Sdes		BE_8_TO_64(W512[j], data);
676162852Sdes		data += 8;
677162852Sdes		/* Apply the SHA-512 compression function to update a..h */
678162852Sdes		T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
679162852Sdes		T2 = Sigma0_512(a) + Maj(a, b, c);
680162852Sdes		h = g;
681162852Sdes		g = f;
682162852Sdes		f = e;
683162852Sdes		e = d + T1;
684162852Sdes		d = c;
685162852Sdes		c = b;
686162852Sdes		b = a;
687162852Sdes		a = T1 + T2;
688162852Sdes
689162852Sdes		j++;
690162852Sdes	} while (j < 16);
691162852Sdes
692162852Sdes	do {
693162852Sdes		/* Part of the message block expansion: */
694162852Sdes		s0 = W512[(j+1)&0x0f];
695162852Sdes		s0 = sigma0_512(s0);
696162852Sdes		s1 = W512[(j+14)&0x0f];
697162852Sdes		s1 =  sigma1_512(s1);
698162852Sdes
699162852Sdes		/* Apply the SHA-512 compression function to update a..h */
700162852Sdes		T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
701162852Sdes		     (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);
702162852Sdes		T2 = Sigma0_512(a) + Maj(a, b, c);
703162852Sdes		h = g;
704162852Sdes		g = f;
705162852Sdes		f = e;
706162852Sdes		e = d + T1;
707162852Sdes		d = c;
708162852Sdes		c = b;
709162852Sdes		b = a;
710162852Sdes		a = T1 + T2;
711162852Sdes
712162852Sdes		j++;
713162852Sdes	} while (j < 80);
714162852Sdes
715162852Sdes	/* Compute the current intermediate hash value */
716162852Sdes	state[0] += a;
717162852Sdes	state[1] += b;
718162852Sdes	state[2] += c;
719162852Sdes	state[3] += d;
720162852Sdes	state[4] += e;
721162852Sdes	state[5] += f;
722162852Sdes	state[6] += g;
723162852Sdes	state[7] += h;
724162852Sdes
725162852Sdes	/* Clean up */
726162852Sdes	a = b = c = d = e = f = g = h = T1 = T2 = 0;
727162852Sdes}
728162852Sdes
729162852Sdes#endif /* SHA2_UNROLL_TRANSFORM */
730162852Sdes
731162852Sdesvoid
732162852SdesSHA512_Update(SHA512_CTX *context, const u_int8_t *data, size_t len)
733162852Sdes{
734162852Sdes	size_t	freespace, usedspace;
735162852Sdes
736162852Sdes	/* Calling with no data is valid (we do nothing) */
737162852Sdes	if (len == 0)
738162852Sdes		return;
739162852Sdes
740162852Sdes	usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
741162852Sdes	if (usedspace > 0) {
742162852Sdes		/* Calculate how much free space is available in the buffer */
743162852Sdes		freespace = SHA512_BLOCK_LENGTH - usedspace;
744162852Sdes
745162852Sdes		if (len >= freespace) {
746162852Sdes			/* Fill the buffer completely and process it */
747162852Sdes			memcpy(&context->buffer[usedspace], data, freespace);
748162852Sdes			ADDINC128(context->bitcount, freespace << 3);
749162852Sdes			len -= freespace;
750162852Sdes			data += freespace;
751162852Sdes			SHA512_Transform(context->state, context->buffer);
752162852Sdes		} else {
753162852Sdes			/* The buffer is not yet full */
754162852Sdes			memcpy(&context->buffer[usedspace], data, len);
755162852Sdes			ADDINC128(context->bitcount, len << 3);
756162852Sdes			/* Clean up: */
757162852Sdes			usedspace = freespace = 0;
758162852Sdes			return;
759162852Sdes		}
760162852Sdes	}
761162852Sdes	while (len >= SHA512_BLOCK_LENGTH) {
762162852Sdes		/* Process as many complete blocks as we can */
763162852Sdes		SHA512_Transform(context->state, data);
764162852Sdes		ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3);
765162852Sdes		len -= SHA512_BLOCK_LENGTH;
766162852Sdes		data += SHA512_BLOCK_LENGTH;
767162852Sdes	}
768162852Sdes	if (len > 0) {
769162852Sdes		/* There's left-overs, so save 'em */
770162852Sdes		memcpy(context->buffer, data, len);
771162852Sdes		ADDINC128(context->bitcount, len << 3);
772162852Sdes	}
773162852Sdes	/* Clean up: */
774162852Sdes	usedspace = freespace = 0;
775162852Sdes}
776162852Sdes
777162852Sdesvoid
778162852SdesSHA512_Pad(SHA512_CTX *context)
779162852Sdes{
780162852Sdes	unsigned int	usedspace;
781162852Sdes
782162852Sdes	usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
783162852Sdes	if (usedspace > 0) {
784162852Sdes		/* Begin padding with a 1 bit: */
785162852Sdes		context->buffer[usedspace++] = 0x80;
786162852Sdes
787162852Sdes		if (usedspace <= SHA512_SHORT_BLOCK_LENGTH) {
788162852Sdes			/* Set-up for the last transform: */
789162852Sdes			memset(&context->buffer[usedspace], 0, SHA512_SHORT_BLOCK_LENGTH - usedspace);
790162852Sdes		} else {
791162852Sdes			if (usedspace < SHA512_BLOCK_LENGTH) {
792162852Sdes				memset(&context->buffer[usedspace], 0, SHA512_BLOCK_LENGTH - usedspace);
793162852Sdes			}
794162852Sdes			/* Do second-to-last transform: */
795162852Sdes			SHA512_Transform(context->state, context->buffer);
796162852Sdes
797162852Sdes			/* And set-up for the last transform: */
798162852Sdes			memset(context->buffer, 0, SHA512_BLOCK_LENGTH - 2);
799162852Sdes		}
800162852Sdes	} else {
801162852Sdes		/* Prepare for final transform: */
802162852Sdes		memset(context->buffer, 0, SHA512_SHORT_BLOCK_LENGTH);
803162852Sdes
804162852Sdes		/* Begin padding with a 1 bit: */
805162852Sdes		*context->buffer = 0x80;
806162852Sdes	}
807162852Sdes	/* Store the length of input data (in bits) in big endian format: */
808162852Sdes	BE_64_TO_8(&context->buffer[SHA512_SHORT_BLOCK_LENGTH],
809162852Sdes	    context->bitcount[1]);
810162852Sdes	BE_64_TO_8(&context->buffer[SHA512_SHORT_BLOCK_LENGTH + 8],
811162852Sdes	    context->bitcount[0]);
812162852Sdes
813162852Sdes	/* Final transform: */
814162852Sdes	SHA512_Transform(context->state, context->buffer);
815162852Sdes
816162852Sdes	/* Clean up: */
817162852Sdes	usedspace = 0;
818162852Sdes}
819162852Sdes
820162852Sdesvoid
821162852SdesSHA512_Final(u_int8_t digest[SHA512_DIGEST_LENGTH], SHA512_CTX *context)
822162852Sdes{
823162852Sdes	SHA512_Pad(context);
824162852Sdes
825162852Sdes	/* If no digest buffer is passed, we don't bother doing this: */
826162852Sdes	if (digest != NULL) {
827162852Sdes#if BYTE_ORDER == LITTLE_ENDIAN
828162852Sdes		int	i;
829162852Sdes
830162852Sdes		/* Convert TO host byte order */
831162852Sdes		for (i = 0; i < 8; i++)
832162852Sdes			BE_64_TO_8(digest + i * 8, context->state[i]);
833162852Sdes#else
834162852Sdes		memcpy(digest, context->state, SHA512_DIGEST_LENGTH);
835162852Sdes#endif
836162852Sdes		memset(context, 0, sizeof(*context));
837162852Sdes	}
838162852Sdes}
839162852Sdes
840162852Sdes
841162852Sdes#if 0
842162852Sdes/*** SHA-384: *********************************************************/
843162852Sdesvoid
844162852SdesSHA384_Init(SHA384_CTX *context)
845162852Sdes{
846162852Sdes	if (context == NULL)
847162852Sdes		return;
848162852Sdes	memcpy(context->state, sha384_initial_hash_value,
849162852Sdes	    sizeof(sha384_initial_hash_value));
850162852Sdes	memset(context->buffer, 0, sizeof(context->buffer));
851162852Sdes	context->bitcount[0] = context->bitcount[1] = 0;
852162852Sdes}
853162852Sdes
854162852Sdes__weak_alias(SHA384_Transform, SHA512_Transform);
855162852Sdes__weak_alias(SHA384_Update, SHA512_Update);
856162852Sdes__weak_alias(SHA384_Pad, SHA512_Pad);
857162852Sdes
858162852Sdesvoid
859162852SdesSHA384_Final(u_int8_t digest[SHA384_DIGEST_LENGTH], SHA384_CTX *context)
860162852Sdes{
861162852Sdes	SHA384_Pad(context);
862162852Sdes
863162852Sdes	/* If no digest buffer is passed, we don't bother doing this: */
864162852Sdes	if (digest != NULL) {
865162852Sdes#if BYTE_ORDER == LITTLE_ENDIAN
866162852Sdes		int	i;
867162852Sdes
868162852Sdes		/* Convert TO host byte order */
869162852Sdes		for (i = 0; i < 6; i++)
870162852Sdes			BE_64_TO_8(digest + i * 8, context->state[i]);
871162852Sdes#else
872162852Sdes		memcpy(digest, context->state, SHA384_DIGEST_LENGTH);
873162852Sdes#endif
874162852Sdes	}
875162852Sdes
876162852Sdes	/* Zero out state data */
877162852Sdes	memset(context, 0, sizeof(*context));
878162852Sdes}
879162852Sdes#endif
880162852Sdes
881162852Sdes#endif /* !defined(HAVE_EVP_SHA256) && !defined(HAVE_SHA256_UPDATE) && \
882162852Sdes    (OPENSSL_VERSION_NUMBER >= 0x00907000L) */
883