xref: /titanic_50/usr/src/common/crypto/sha2/sha2.c (revision 45818ee124adeaaf947698996b4f4c722afc6d1f)
17c478bd9Sstevel@tonic-gate /*
28de5c4f4SDan OpenSolaris Anderson  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
5*45818ee1SMatthew Ahrens /*
6*45818ee1SMatthew Ahrens  * Copyright 2013 Saso Kiselkov.  All rights reserved.
7*45818ee1SMatthew Ahrens  */
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate  * The basic framework for this code came from the reference
117c478bd9Sstevel@tonic-gate  * implementation for MD5.  That implementation is Copyright (C)
127c478bd9Sstevel@tonic-gate  * 1991-2, RSA Data Security, Inc. Created 1991. All rights reserved.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * License to copy and use this software is granted provided that it
157c478bd9Sstevel@tonic-gate  * is identified as the "RSA Data Security, Inc. MD5 Message-Digest
167c478bd9Sstevel@tonic-gate  * Algorithm" in all material mentioning or referencing this software
177c478bd9Sstevel@tonic-gate  * or this function.
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * License is also granted to make and use derivative works provided
207c478bd9Sstevel@tonic-gate  * that such works are identified as "derived from the RSA Data
217c478bd9Sstevel@tonic-gate  * Security, Inc. MD5 Message-Digest Algorithm" in all material
227c478bd9Sstevel@tonic-gate  * mentioning or referencing the derived work.
237c478bd9Sstevel@tonic-gate  *
247c478bd9Sstevel@tonic-gate  * RSA Data Security, Inc. makes no representations concerning either
257c478bd9Sstevel@tonic-gate  * the merchantability of this software or the suitability of this
267c478bd9Sstevel@tonic-gate  * software for any particular purpose. It is provided "as is"
277c478bd9Sstevel@tonic-gate  * without express or implied warranty of any kind.
287c478bd9Sstevel@tonic-gate  *
297c478bd9Sstevel@tonic-gate  * These notices must be retained in any copies of any part of this
307c478bd9Sstevel@tonic-gate  * documentation and/or software.
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  * NOTE: Cleaned-up and optimized, version of SHA2, based on the FIPS 180-2
334b56a003SDaniel Anderson  * standard, available at
344b56a003SDaniel Anderson  * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
357c478bd9Sstevel@tonic-gate  * Not as fast as one would like -- further optimizations are encouraged
367c478bd9Sstevel@tonic-gate  * and appreciated.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
398de5c4f4SDan OpenSolaris Anderson #ifndef _KERNEL
408de5c4f4SDan OpenSolaris Anderson #include <stdint.h>
418de5c4f4SDan OpenSolaris Anderson #include <strings.h>
428de5c4f4SDan OpenSolaris Anderson #include <stdlib.h>
438de5c4f4SDan OpenSolaris Anderson #include <errno.h>
448de5c4f4SDan OpenSolaris Anderson #endif /* _KERNEL */
458de5c4f4SDan OpenSolaris Anderson 
467c478bd9Sstevel@tonic-gate #include <sys/types.h>
477c478bd9Sstevel@tonic-gate #include <sys/param.h>
487c478bd9Sstevel@tonic-gate #include <sys/systm.h>
497c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
50734b6a94Sdarrenm #define	_SHA2_IMPL
517c478bd9Sstevel@tonic-gate #include <sys/sha2.h>
527c478bd9Sstevel@tonic-gate #include <sys/sha2_consts.h>
537c478bd9Sstevel@tonic-gate 
5455553f71Sda73024 #ifdef _KERNEL
5555553f71Sda73024 #include <sys/cmn_err.h>
567c478bd9Sstevel@tonic-gate 
5755553f71Sda73024 #else
58734b6a94Sdarrenm #pragma weak SHA256Update = SHA2Update
59734b6a94Sdarrenm #pragma weak SHA384Update = SHA2Update
60734b6a94Sdarrenm #pragma weak SHA512Update = SHA2Update
61734b6a94Sdarrenm 
62734b6a94Sdarrenm #pragma weak SHA256Final = SHA2Final
63734b6a94Sdarrenm #pragma weak SHA384Final = SHA2Final
64734b6a94Sdarrenm #pragma weak SHA512Final = SHA2Final
65734b6a94Sdarrenm 
66734b6a94Sdarrenm #endif	/* _KERNEL */
67734b6a94Sdarrenm 
684b56a003SDaniel Anderson #ifdef _LITTLE_ENDIAN
694b56a003SDaniel Anderson #include <sys/byteorder.h>
704b56a003SDaniel Anderson #define	HAVE_HTONL
714b56a003SDaniel Anderson #endif
724b56a003SDaniel Anderson 
737c478bd9Sstevel@tonic-gate static void Encode(uint8_t *, uint32_t *, size_t);
747c478bd9Sstevel@tonic-gate static void Encode64(uint8_t *, uint64_t *, size_t);
7555553f71Sda73024 
7655553f71Sda73024 #if	defined(__amd64)
7755553f71Sda73024 #define	SHA512Transform(ctx, in) SHA512TransformBlocks((ctx), (in), 1)
7855553f71Sda73024 #define	SHA256Transform(ctx, in) SHA256TransformBlocks((ctx), (in), 1)
7955553f71Sda73024 
8055553f71Sda73024 void SHA512TransformBlocks(SHA2_CTX *ctx, const void *in, size_t num);
8155553f71Sda73024 void SHA256TransformBlocks(SHA2_CTX *ctx, const void *in, size_t num);
8255553f71Sda73024 
8355553f71Sda73024 #else
847c478bd9Sstevel@tonic-gate static void SHA256Transform(SHA2_CTX *, const uint8_t *);
857c478bd9Sstevel@tonic-gate static void SHA512Transform(SHA2_CTX *, const uint8_t *);
8655553f71Sda73024 #endif	/* __amd64 */
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate static uint8_t PADDING[128] = { 0x80, /* all zeros */ };
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /* Ch and Maj are the basic SHA2 functions. */
917c478bd9Sstevel@tonic-gate #define	Ch(b, c, d)	(((b) & (c)) ^ ((~b) & (d)))
927c478bd9Sstevel@tonic-gate #define	Maj(b, c, d)	(((b) & (c)) ^ ((b) & (d)) ^ ((c) & (d)))
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /* Rotates x right n bits. */
957c478bd9Sstevel@tonic-gate #define	ROTR(x, n)	\
967c478bd9Sstevel@tonic-gate 	(((x) >> (n)) | ((x) << ((sizeof (x) * NBBY)-(n))))
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /* Shift x right n bits */
997c478bd9Sstevel@tonic-gate #define	SHR(x, n)	((x) >> (n))
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate /* SHA256 Functions */
1027c478bd9Sstevel@tonic-gate #define	BIGSIGMA0_256(x)	(ROTR((x), 2) ^ ROTR((x), 13) ^ ROTR((x), 22))
1037c478bd9Sstevel@tonic-gate #define	BIGSIGMA1_256(x)	(ROTR((x), 6) ^ ROTR((x), 11) ^ ROTR((x), 25))
1047c478bd9Sstevel@tonic-gate #define	SIGMA0_256(x)		(ROTR((x), 7) ^ ROTR((x), 18) ^ SHR((x), 3))
1057c478bd9Sstevel@tonic-gate #define	SIGMA1_256(x)		(ROTR((x), 17) ^ ROTR((x), 19) ^ SHR((x), 10))
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate #define	SHA256ROUND(a, b, c, d, e, f, g, h, i, w)			\
1087c478bd9Sstevel@tonic-gate 	T1 = h + BIGSIGMA1_256(e) + Ch(e, f, g) + SHA256_CONST(i) + w;	\
1097c478bd9Sstevel@tonic-gate 	d += T1;							\
1107c478bd9Sstevel@tonic-gate 	T2 = BIGSIGMA0_256(a) + Maj(a, b, c);				\
1117c478bd9Sstevel@tonic-gate 	h = T1 + T2
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate /* SHA384/512 Functions */
1147c478bd9Sstevel@tonic-gate #define	BIGSIGMA0(x)	(ROTR((x), 28) ^ ROTR((x), 34) ^ ROTR((x), 39))
1157c478bd9Sstevel@tonic-gate #define	BIGSIGMA1(x)	(ROTR((x), 14) ^ ROTR((x), 18) ^ ROTR((x), 41))
1167c478bd9Sstevel@tonic-gate #define	SIGMA0(x)	(ROTR((x), 1) ^ ROTR((x), 8) ^ SHR((x), 7))
1177c478bd9Sstevel@tonic-gate #define	SIGMA1(x)	(ROTR((x), 19) ^ ROTR((x), 61) ^ SHR((x), 6))
1187c478bd9Sstevel@tonic-gate #define	SHA512ROUND(a, b, c, d, e, f, g, h, i, w)			\
1197c478bd9Sstevel@tonic-gate 	T1 = h + BIGSIGMA1(e) + Ch(e, f, g) + SHA512_CONST(i) + w;	\
1207c478bd9Sstevel@tonic-gate 	d += T1;							\
1217c478bd9Sstevel@tonic-gate 	T2 = BIGSIGMA0(a) + Maj(a, b, c);				\
1227c478bd9Sstevel@tonic-gate 	h = T1 + T2
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate /*
1257c478bd9Sstevel@tonic-gate  * sparc optimization:
1267c478bd9Sstevel@tonic-gate  *
1277c478bd9Sstevel@tonic-gate  * on the sparc, we can load big endian 32-bit data easily.  note that
1287c478bd9Sstevel@tonic-gate  * special care must be taken to ensure the address is 32-bit aligned.
1297c478bd9Sstevel@tonic-gate  * in the interest of speed, we don't check to make sure, since
1307c478bd9Sstevel@tonic-gate  * careful programming can guarantee this for us.
1317c478bd9Sstevel@tonic-gate  */
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate #if	defined(_BIG_ENDIAN)
1347c478bd9Sstevel@tonic-gate #define	LOAD_BIG_32(addr)	(*(uint32_t *)(addr))
1357c478bd9Sstevel@tonic-gate #define	LOAD_BIG_64(addr)	(*(uint64_t *)(addr))
1367c478bd9Sstevel@tonic-gate 
1374b56a003SDaniel Anderson #elif	defined(HAVE_HTONL)
1384b56a003SDaniel Anderson #define	LOAD_BIG_32(addr) htonl(*((uint32_t *)(addr)))
1394b56a003SDaniel Anderson #define	LOAD_BIG_64(addr) htonll(*((uint64_t *)(addr)))
1407c478bd9Sstevel@tonic-gate 
1414b56a003SDaniel Anderson #else
1424b56a003SDaniel Anderson /* little endian -- will work on big endian, but slowly */
1434b56a003SDaniel Anderson #define	LOAD_BIG_32(addr)	\
1444b56a003SDaniel Anderson 	(((addr)[0] << 24) | ((addr)[1] << 16) | ((addr)[2] << 8) | (addr)[3])
1457c478bd9Sstevel@tonic-gate #define	LOAD_BIG_64(addr)	\
1467c478bd9Sstevel@tonic-gate 	(((uint64_t)(addr)[0] << 56) | ((uint64_t)(addr)[1] << 48) |	\
1477c478bd9Sstevel@tonic-gate 	    ((uint64_t)(addr)[2] << 40) | ((uint64_t)(addr)[3] << 32) |	\
1487c478bd9Sstevel@tonic-gate 	    ((uint64_t)(addr)[4] << 24) | ((uint64_t)(addr)[5] << 16) |	\
1497c478bd9Sstevel@tonic-gate 	    ((uint64_t)(addr)[6] << 8) | (uint64_t)(addr)[7])
1504b56a003SDaniel Anderson #endif	/* _BIG_ENDIAN */
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 
15355553f71Sda73024 #if	!defined(__amd64)
1547c478bd9Sstevel@tonic-gate /* SHA256 Transform */
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate static void
SHA256Transform(SHA2_CTX * ctx,const uint8_t * blk)1577c478bd9Sstevel@tonic-gate SHA256Transform(SHA2_CTX *ctx, const uint8_t *blk)
1587c478bd9Sstevel@tonic-gate {
1597c478bd9Sstevel@tonic-gate 	uint32_t a = ctx->state.s32[0];
1607c478bd9Sstevel@tonic-gate 	uint32_t b = ctx->state.s32[1];
1617c478bd9Sstevel@tonic-gate 	uint32_t c = ctx->state.s32[2];
1627c478bd9Sstevel@tonic-gate 	uint32_t d = ctx->state.s32[3];
1637c478bd9Sstevel@tonic-gate 	uint32_t e = ctx->state.s32[4];
1647c478bd9Sstevel@tonic-gate 	uint32_t f = ctx->state.s32[5];
1657c478bd9Sstevel@tonic-gate 	uint32_t g = ctx->state.s32[6];
1667c478bd9Sstevel@tonic-gate 	uint32_t h = ctx->state.s32[7];
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	uint32_t w0, w1, w2, w3, w4, w5, w6, w7;
1697c478bd9Sstevel@tonic-gate 	uint32_t w8, w9, w10, w11, w12, w13, w14, w15;
1707c478bd9Sstevel@tonic-gate 	uint32_t T1, T2;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate #if	defined(__sparc)
1737c478bd9Sstevel@tonic-gate 	static const uint32_t sha256_consts[] = {
1747c478bd9Sstevel@tonic-gate 		SHA256_CONST_0, SHA256_CONST_1, SHA256_CONST_2,
1757c478bd9Sstevel@tonic-gate 		SHA256_CONST_3, SHA256_CONST_4, SHA256_CONST_5,
1767c478bd9Sstevel@tonic-gate 		SHA256_CONST_6, SHA256_CONST_7, SHA256_CONST_8,
1777c478bd9Sstevel@tonic-gate 		SHA256_CONST_9, SHA256_CONST_10, SHA256_CONST_11,
1787c478bd9Sstevel@tonic-gate 		SHA256_CONST_12, SHA256_CONST_13, SHA256_CONST_14,
1797c478bd9Sstevel@tonic-gate 		SHA256_CONST_15, SHA256_CONST_16, SHA256_CONST_17,
1807c478bd9Sstevel@tonic-gate 		SHA256_CONST_18, SHA256_CONST_19, SHA256_CONST_20,
1817c478bd9Sstevel@tonic-gate 		SHA256_CONST_21, SHA256_CONST_22, SHA256_CONST_23,
1827c478bd9Sstevel@tonic-gate 		SHA256_CONST_24, SHA256_CONST_25, SHA256_CONST_26,
1837c478bd9Sstevel@tonic-gate 		SHA256_CONST_27, SHA256_CONST_28, SHA256_CONST_29,
1847c478bd9Sstevel@tonic-gate 		SHA256_CONST_30, SHA256_CONST_31, SHA256_CONST_32,
1857c478bd9Sstevel@tonic-gate 		SHA256_CONST_33, SHA256_CONST_34, SHA256_CONST_35,
1867c478bd9Sstevel@tonic-gate 		SHA256_CONST_36, SHA256_CONST_37, SHA256_CONST_38,
1877c478bd9Sstevel@tonic-gate 		SHA256_CONST_39, SHA256_CONST_40, SHA256_CONST_41,
1887c478bd9Sstevel@tonic-gate 		SHA256_CONST_42, SHA256_CONST_43, SHA256_CONST_44,
1897c478bd9Sstevel@tonic-gate 		SHA256_CONST_45, SHA256_CONST_46, SHA256_CONST_47,
1907c478bd9Sstevel@tonic-gate 		SHA256_CONST_48, SHA256_CONST_49, SHA256_CONST_50,
1917c478bd9Sstevel@tonic-gate 		SHA256_CONST_51, SHA256_CONST_52, SHA256_CONST_53,
1927c478bd9Sstevel@tonic-gate 		SHA256_CONST_54, SHA256_CONST_55, SHA256_CONST_56,
1937c478bd9Sstevel@tonic-gate 		SHA256_CONST_57, SHA256_CONST_58, SHA256_CONST_59,
1947c478bd9Sstevel@tonic-gate 		SHA256_CONST_60, SHA256_CONST_61, SHA256_CONST_62,
1957c478bd9Sstevel@tonic-gate 		SHA256_CONST_63
1967c478bd9Sstevel@tonic-gate 	};
19755553f71Sda73024 #endif	/* __sparc */
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	if ((uintptr_t)blk & 0x3) {		/* not 4-byte aligned? */
2007c478bd9Sstevel@tonic-gate 		bcopy(blk, ctx->buf_un.buf32,  sizeof (ctx->buf_un.buf32));
2017c478bd9Sstevel@tonic-gate 		blk = (uint8_t *)ctx->buf_un.buf32;
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 
204734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
205f66d273dSizick 	w0 =  LOAD_BIG_32(blk + 4 * 0);
206f66d273dSizick 	SHA256ROUND(a, b, c, d, e, f, g, h, 0, w0);
207734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
208f66d273dSizick 	w1 =  LOAD_BIG_32(blk + 4 * 1);
209f66d273dSizick 	SHA256ROUND(h, a, b, c, d, e, f, g, 1, w1);
210734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
211f66d273dSizick 	w2 =  LOAD_BIG_32(blk + 4 * 2);
212f66d273dSizick 	SHA256ROUND(g, h, a, b, c, d, e, f, 2, w2);
213734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
214f66d273dSizick 	w3 =  LOAD_BIG_32(blk + 4 * 3);
215f66d273dSizick 	SHA256ROUND(f, g, h, a, b, c, d, e, 3, w3);
216734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
217f66d273dSizick 	w4 =  LOAD_BIG_32(blk + 4 * 4);
218f66d273dSizick 	SHA256ROUND(e, f, g, h, a, b, c, d, 4, w4);
219734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
220f66d273dSizick 	w5 =  LOAD_BIG_32(blk + 4 * 5);
221f66d273dSizick 	SHA256ROUND(d, e, f, g, h, a, b, c, 5, w5);
222734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
223f66d273dSizick 	w6 =  LOAD_BIG_32(blk + 4 * 6);
224f66d273dSizick 	SHA256ROUND(c, d, e, f, g, h, a, b, 6, w6);
225734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
226f66d273dSizick 	w7 =  LOAD_BIG_32(blk + 4 * 7);
227f66d273dSizick 	SHA256ROUND(b, c, d, e, f, g, h, a, 7, w7);
228734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
229f66d273dSizick 	w8 =  LOAD_BIG_32(blk + 4 * 8);
230f66d273dSizick 	SHA256ROUND(a, b, c, d, e, f, g, h, 8, w8);
231734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
232f66d273dSizick 	w9 =  LOAD_BIG_32(blk + 4 * 9);
233f66d273dSizick 	SHA256ROUND(h, a, b, c, d, e, f, g, 9, w9);
234734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
235f66d273dSizick 	w10 =  LOAD_BIG_32(blk + 4 * 10);
236f66d273dSizick 	SHA256ROUND(g, h, a, b, c, d, e, f, 10, w10);
237734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
238f66d273dSizick 	w11 =  LOAD_BIG_32(blk + 4 * 11);
239f66d273dSizick 	SHA256ROUND(f, g, h, a, b, c, d, e, 11, w11);
240734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
241f66d273dSizick 	w12 =  LOAD_BIG_32(blk + 4 * 12);
242f66d273dSizick 	SHA256ROUND(e, f, g, h, a, b, c, d, 12, w12);
243734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
244f66d273dSizick 	w13 =  LOAD_BIG_32(blk + 4 * 13);
245f66d273dSizick 	SHA256ROUND(d, e, f, g, h, a, b, c, 13, w13);
246734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
247f66d273dSizick 	w14 =  LOAD_BIG_32(blk + 4 * 14);
248f66d273dSizick 	SHA256ROUND(c, d, e, f, g, h, a, b, 14, w14);
249734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
250f66d273dSizick 	w15 =  LOAD_BIG_32(blk + 4 * 15);
251f66d273dSizick 	SHA256ROUND(b, c, d, e, f, g, h, a, 15, w15);
252f66d273dSizick 
2537c478bd9Sstevel@tonic-gate 	w0 = SIGMA1_256(w14) + w9 + SIGMA0_256(w1) + w0;
2547c478bd9Sstevel@tonic-gate 	SHA256ROUND(a, b, c, d, e, f, g, h, 16, w0);
2557c478bd9Sstevel@tonic-gate 	w1 = SIGMA1_256(w15) + w10 + SIGMA0_256(w2) + w1;
2567c478bd9Sstevel@tonic-gate 	SHA256ROUND(h, a, b, c, d, e, f, g, 17, w1);
2577c478bd9Sstevel@tonic-gate 	w2 = SIGMA1_256(w0) + w11 + SIGMA0_256(w3) + w2;
2587c478bd9Sstevel@tonic-gate 	SHA256ROUND(g, h, a, b, c, d, e, f, 18, w2);
2597c478bd9Sstevel@tonic-gate 	w3 = SIGMA1_256(w1) + w12 + SIGMA0_256(w4) + w3;
2607c478bd9Sstevel@tonic-gate 	SHA256ROUND(f, g, h, a, b, c, d, e, 19, w3);
2617c478bd9Sstevel@tonic-gate 	w4 = SIGMA1_256(w2) + w13 + SIGMA0_256(w5) + w4;
2627c478bd9Sstevel@tonic-gate 	SHA256ROUND(e, f, g, h, a, b, c, d, 20, w4);
2637c478bd9Sstevel@tonic-gate 	w5 = SIGMA1_256(w3) + w14 + SIGMA0_256(w6) + w5;
2647c478bd9Sstevel@tonic-gate 	SHA256ROUND(d, e, f, g, h, a, b, c, 21, w5);
2657c478bd9Sstevel@tonic-gate 	w6 = SIGMA1_256(w4) + w15 + SIGMA0_256(w7) + w6;
2667c478bd9Sstevel@tonic-gate 	SHA256ROUND(c, d, e, f, g, h, a, b, 22, w6);
2677c478bd9Sstevel@tonic-gate 	w7 = SIGMA1_256(w5) + w0 + SIGMA0_256(w8) + w7;
2687c478bd9Sstevel@tonic-gate 	SHA256ROUND(b, c, d, e, f, g, h, a, 23, w7);
2697c478bd9Sstevel@tonic-gate 	w8 = SIGMA1_256(w6) + w1 + SIGMA0_256(w9) + w8;
2707c478bd9Sstevel@tonic-gate 	SHA256ROUND(a, b, c, d, e, f, g, h, 24, w8);
2717c478bd9Sstevel@tonic-gate 	w9 = SIGMA1_256(w7) + w2 + SIGMA0_256(w10) + w9;
2727c478bd9Sstevel@tonic-gate 	SHA256ROUND(h, a, b, c, d, e, f, g, 25, w9);
2737c478bd9Sstevel@tonic-gate 	w10 = SIGMA1_256(w8) + w3 + SIGMA0_256(w11) + w10;
2747c478bd9Sstevel@tonic-gate 	SHA256ROUND(g, h, a, b, c, d, e, f, 26, w10);
2757c478bd9Sstevel@tonic-gate 	w11 = SIGMA1_256(w9) + w4 + SIGMA0_256(w12) + w11;
2767c478bd9Sstevel@tonic-gate 	SHA256ROUND(f, g, h, a, b, c, d, e, 27, w11);
2777c478bd9Sstevel@tonic-gate 	w12 = SIGMA1_256(w10) + w5 + SIGMA0_256(w13) + w12;
2787c478bd9Sstevel@tonic-gate 	SHA256ROUND(e, f, g, h, a, b, c, d, 28, w12);
2797c478bd9Sstevel@tonic-gate 	w13 = SIGMA1_256(w11) + w6 + SIGMA0_256(w14) + w13;
2807c478bd9Sstevel@tonic-gate 	SHA256ROUND(d, e, f, g, h, a, b, c, 29, w13);
2817c478bd9Sstevel@tonic-gate 	w14 = SIGMA1_256(w12) + w7 + SIGMA0_256(w15) + w14;
2827c478bd9Sstevel@tonic-gate 	SHA256ROUND(c, d, e, f, g, h, a, b, 30, w14);
2837c478bd9Sstevel@tonic-gate 	w15 = SIGMA1_256(w13) + w8 + SIGMA0_256(w0) + w15;
2847c478bd9Sstevel@tonic-gate 	SHA256ROUND(b, c, d, e, f, g, h, a, 31, w15);
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	w0 = SIGMA1_256(w14) + w9 + SIGMA0_256(w1) + w0;
2877c478bd9Sstevel@tonic-gate 	SHA256ROUND(a, b, c, d, e, f, g, h, 32, w0);
2887c478bd9Sstevel@tonic-gate 	w1 = SIGMA1_256(w15) + w10 + SIGMA0_256(w2) + w1;
2897c478bd9Sstevel@tonic-gate 	SHA256ROUND(h, a, b, c, d, e, f, g, 33, w1);
2907c478bd9Sstevel@tonic-gate 	w2 = SIGMA1_256(w0) + w11 + SIGMA0_256(w3) + w2;
2917c478bd9Sstevel@tonic-gate 	SHA256ROUND(g, h, a, b, c, d, e, f, 34, w2);
2927c478bd9Sstevel@tonic-gate 	w3 = SIGMA1_256(w1) + w12 + SIGMA0_256(w4) + w3;
2937c478bd9Sstevel@tonic-gate 	SHA256ROUND(f, g, h, a, b, c, d, e, 35, w3);
2947c478bd9Sstevel@tonic-gate 	w4 = SIGMA1_256(w2) + w13 + SIGMA0_256(w5) + w4;
2957c478bd9Sstevel@tonic-gate 	SHA256ROUND(e, f, g, h, a, b, c, d, 36, w4);
2967c478bd9Sstevel@tonic-gate 	w5 = SIGMA1_256(w3) + w14 + SIGMA0_256(w6) + w5;
2977c478bd9Sstevel@tonic-gate 	SHA256ROUND(d, e, f, g, h, a, b, c, 37, w5);
2987c478bd9Sstevel@tonic-gate 	w6 = SIGMA1_256(w4) + w15 + SIGMA0_256(w7) + w6;
2997c478bd9Sstevel@tonic-gate 	SHA256ROUND(c, d, e, f, g, h, a, b, 38, w6);
3007c478bd9Sstevel@tonic-gate 	w7 = SIGMA1_256(w5) + w0 + SIGMA0_256(w8) + w7;
3017c478bd9Sstevel@tonic-gate 	SHA256ROUND(b, c, d, e, f, g, h, a, 39, w7);
3027c478bd9Sstevel@tonic-gate 	w8 = SIGMA1_256(w6) + w1 + SIGMA0_256(w9) + w8;
3037c478bd9Sstevel@tonic-gate 	SHA256ROUND(a, b, c, d, e, f, g, h, 40, w8);
3047c478bd9Sstevel@tonic-gate 	w9 = SIGMA1_256(w7) + w2 + SIGMA0_256(w10) + w9;
3057c478bd9Sstevel@tonic-gate 	SHA256ROUND(h, a, b, c, d, e, f, g, 41, w9);
3067c478bd9Sstevel@tonic-gate 	w10 = SIGMA1_256(w8) + w3 + SIGMA0_256(w11) + w10;
3077c478bd9Sstevel@tonic-gate 	SHA256ROUND(g, h, a, b, c, d, e, f, 42, w10);
3087c478bd9Sstevel@tonic-gate 	w11 = SIGMA1_256(w9) + w4 + SIGMA0_256(w12) + w11;
3097c478bd9Sstevel@tonic-gate 	SHA256ROUND(f, g, h, a, b, c, d, e, 43, w11);
3107c478bd9Sstevel@tonic-gate 	w12 = SIGMA1_256(w10) + w5 + SIGMA0_256(w13) + w12;
3117c478bd9Sstevel@tonic-gate 	SHA256ROUND(e, f, g, h, a, b, c, d, 44, w12);
3127c478bd9Sstevel@tonic-gate 	w13 = SIGMA1_256(w11) + w6 + SIGMA0_256(w14) + w13;
3137c478bd9Sstevel@tonic-gate 	SHA256ROUND(d, e, f, g, h, a, b, c, 45, w13);
3147c478bd9Sstevel@tonic-gate 	w14 = SIGMA1_256(w12) + w7 + SIGMA0_256(w15) + w14;
3157c478bd9Sstevel@tonic-gate 	SHA256ROUND(c, d, e, f, g, h, a, b, 46, w14);
3167c478bd9Sstevel@tonic-gate 	w15 = SIGMA1_256(w13) + w8 + SIGMA0_256(w0) + w15;
3177c478bd9Sstevel@tonic-gate 	SHA256ROUND(b, c, d, e, f, g, h, a, 47, w15);
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	w0 = SIGMA1_256(w14) + w9 + SIGMA0_256(w1) + w0;
3207c478bd9Sstevel@tonic-gate 	SHA256ROUND(a, b, c, d, e, f, g, h, 48, w0);
3217c478bd9Sstevel@tonic-gate 	w1 = SIGMA1_256(w15) + w10 + SIGMA0_256(w2) + w1;
3227c478bd9Sstevel@tonic-gate 	SHA256ROUND(h, a, b, c, d, e, f, g, 49, w1);
3237c478bd9Sstevel@tonic-gate 	w2 = SIGMA1_256(w0) + w11 + SIGMA0_256(w3) + w2;
3247c478bd9Sstevel@tonic-gate 	SHA256ROUND(g, h, a, b, c, d, e, f, 50, w2);
3257c478bd9Sstevel@tonic-gate 	w3 = SIGMA1_256(w1) + w12 + SIGMA0_256(w4) + w3;
3267c478bd9Sstevel@tonic-gate 	SHA256ROUND(f, g, h, a, b, c, d, e, 51, w3);
3277c478bd9Sstevel@tonic-gate 	w4 = SIGMA1_256(w2) + w13 + SIGMA0_256(w5) + w4;
3287c478bd9Sstevel@tonic-gate 	SHA256ROUND(e, f, g, h, a, b, c, d, 52, w4);
3297c478bd9Sstevel@tonic-gate 	w5 = SIGMA1_256(w3) + w14 + SIGMA0_256(w6) + w5;
3307c478bd9Sstevel@tonic-gate 	SHA256ROUND(d, e, f, g, h, a, b, c, 53, w5);
3317c478bd9Sstevel@tonic-gate 	w6 = SIGMA1_256(w4) + w15 + SIGMA0_256(w7) + w6;
3327c478bd9Sstevel@tonic-gate 	SHA256ROUND(c, d, e, f, g, h, a, b, 54, w6);
3337c478bd9Sstevel@tonic-gate 	w7 = SIGMA1_256(w5) + w0 + SIGMA0_256(w8) + w7;
3347c478bd9Sstevel@tonic-gate 	SHA256ROUND(b, c, d, e, f, g, h, a, 55, w7);
3357c478bd9Sstevel@tonic-gate 	w8 = SIGMA1_256(w6) + w1 + SIGMA0_256(w9) + w8;
3367c478bd9Sstevel@tonic-gate 	SHA256ROUND(a, b, c, d, e, f, g, h, 56, w8);
3377c478bd9Sstevel@tonic-gate 	w9 = SIGMA1_256(w7) + w2 + SIGMA0_256(w10) + w9;
3387c478bd9Sstevel@tonic-gate 	SHA256ROUND(h, a, b, c, d, e, f, g, 57, w9);
3397c478bd9Sstevel@tonic-gate 	w10 = SIGMA1_256(w8) + w3 + SIGMA0_256(w11) + w10;
3407c478bd9Sstevel@tonic-gate 	SHA256ROUND(g, h, a, b, c, d, e, f, 58, w10);
3417c478bd9Sstevel@tonic-gate 	w11 = SIGMA1_256(w9) + w4 + SIGMA0_256(w12) + w11;
3427c478bd9Sstevel@tonic-gate 	SHA256ROUND(f, g, h, a, b, c, d, e, 59, w11);
3437c478bd9Sstevel@tonic-gate 	w12 = SIGMA1_256(w10) + w5 + SIGMA0_256(w13) + w12;
3447c478bd9Sstevel@tonic-gate 	SHA256ROUND(e, f, g, h, a, b, c, d, 60, w12);
3457c478bd9Sstevel@tonic-gate 	w13 = SIGMA1_256(w11) + w6 + SIGMA0_256(w14) + w13;
3467c478bd9Sstevel@tonic-gate 	SHA256ROUND(d, e, f, g, h, a, b, c, 61, w13);
3477c478bd9Sstevel@tonic-gate 	w14 = SIGMA1_256(w12) + w7 + SIGMA0_256(w15) + w14;
3487c478bd9Sstevel@tonic-gate 	SHA256ROUND(c, d, e, f, g, h, a, b, 62, w14);
3497c478bd9Sstevel@tonic-gate 	w15 = SIGMA1_256(w13) + w8 + SIGMA0_256(w0) + w15;
3507c478bd9Sstevel@tonic-gate 	SHA256ROUND(b, c, d, e, f, g, h, a, 63, w15);
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	ctx->state.s32[0] += a;
3537c478bd9Sstevel@tonic-gate 	ctx->state.s32[1] += b;
3547c478bd9Sstevel@tonic-gate 	ctx->state.s32[2] += c;
3557c478bd9Sstevel@tonic-gate 	ctx->state.s32[3] += d;
3567c478bd9Sstevel@tonic-gate 	ctx->state.s32[4] += e;
3577c478bd9Sstevel@tonic-gate 	ctx->state.s32[5] += f;
3587c478bd9Sstevel@tonic-gate 	ctx->state.s32[6] += g;
3597c478bd9Sstevel@tonic-gate 	ctx->state.s32[7] += h;
3607c478bd9Sstevel@tonic-gate }
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate /* SHA384 and SHA512 Transform */
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate static void
SHA512Transform(SHA2_CTX * ctx,const uint8_t * blk)3667c478bd9Sstevel@tonic-gate SHA512Transform(SHA2_CTX *ctx, const uint8_t *blk)
3677c478bd9Sstevel@tonic-gate {
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	uint64_t a = ctx->state.s64[0];
3707c478bd9Sstevel@tonic-gate 	uint64_t b = ctx->state.s64[1];
3717c478bd9Sstevel@tonic-gate 	uint64_t c = ctx->state.s64[2];
3727c478bd9Sstevel@tonic-gate 	uint64_t d = ctx->state.s64[3];
3737c478bd9Sstevel@tonic-gate 	uint64_t e = ctx->state.s64[4];
3747c478bd9Sstevel@tonic-gate 	uint64_t f = ctx->state.s64[5];
3757c478bd9Sstevel@tonic-gate 	uint64_t g = ctx->state.s64[6];
3767c478bd9Sstevel@tonic-gate 	uint64_t h = ctx->state.s64[7];
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	uint64_t w0, w1, w2, w3, w4, w5, w6, w7;
3797c478bd9Sstevel@tonic-gate 	uint64_t w8, w9, w10, w11, w12, w13, w14, w15;
3807c478bd9Sstevel@tonic-gate 	uint64_t T1, T2;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate #if	defined(__sparc)
3837c478bd9Sstevel@tonic-gate 	static const uint64_t sha512_consts[] = {
3847c478bd9Sstevel@tonic-gate 		SHA512_CONST_0, SHA512_CONST_1, SHA512_CONST_2,
3857c478bd9Sstevel@tonic-gate 		SHA512_CONST_3, SHA512_CONST_4, SHA512_CONST_5,
3867c478bd9Sstevel@tonic-gate 		SHA512_CONST_6, SHA512_CONST_7, SHA512_CONST_8,
3877c478bd9Sstevel@tonic-gate 		SHA512_CONST_9, SHA512_CONST_10, SHA512_CONST_11,
3887c478bd9Sstevel@tonic-gate 		SHA512_CONST_12, SHA512_CONST_13, SHA512_CONST_14,
3897c478bd9Sstevel@tonic-gate 		SHA512_CONST_15, SHA512_CONST_16, SHA512_CONST_17,
3907c478bd9Sstevel@tonic-gate 		SHA512_CONST_18, SHA512_CONST_19, SHA512_CONST_20,
3917c478bd9Sstevel@tonic-gate 		SHA512_CONST_21, SHA512_CONST_22, SHA512_CONST_23,
3927c478bd9Sstevel@tonic-gate 		SHA512_CONST_24, SHA512_CONST_25, SHA512_CONST_26,
3937c478bd9Sstevel@tonic-gate 		SHA512_CONST_27, SHA512_CONST_28, SHA512_CONST_29,
3947c478bd9Sstevel@tonic-gate 		SHA512_CONST_30, SHA512_CONST_31, SHA512_CONST_32,
3957c478bd9Sstevel@tonic-gate 		SHA512_CONST_33, SHA512_CONST_34, SHA512_CONST_35,
3967c478bd9Sstevel@tonic-gate 		SHA512_CONST_36, SHA512_CONST_37, SHA512_CONST_38,
3977c478bd9Sstevel@tonic-gate 		SHA512_CONST_39, SHA512_CONST_40, SHA512_CONST_41,
3987c478bd9Sstevel@tonic-gate 		SHA512_CONST_42, SHA512_CONST_43, SHA512_CONST_44,
3997c478bd9Sstevel@tonic-gate 		SHA512_CONST_45, SHA512_CONST_46, SHA512_CONST_47,
4007c478bd9Sstevel@tonic-gate 		SHA512_CONST_48, SHA512_CONST_49, SHA512_CONST_50,
4017c478bd9Sstevel@tonic-gate 		SHA512_CONST_51, SHA512_CONST_52, SHA512_CONST_53,
4027c478bd9Sstevel@tonic-gate 		SHA512_CONST_54, SHA512_CONST_55, SHA512_CONST_56,
4037c478bd9Sstevel@tonic-gate 		SHA512_CONST_57, SHA512_CONST_58, SHA512_CONST_59,
4047c478bd9Sstevel@tonic-gate 		SHA512_CONST_60, SHA512_CONST_61, SHA512_CONST_62,
4057c478bd9Sstevel@tonic-gate 		SHA512_CONST_63, SHA512_CONST_64, SHA512_CONST_65,
4067c478bd9Sstevel@tonic-gate 		SHA512_CONST_66, SHA512_CONST_67, SHA512_CONST_68,
4077c478bd9Sstevel@tonic-gate 		SHA512_CONST_69, SHA512_CONST_70, SHA512_CONST_71,
4087c478bd9Sstevel@tonic-gate 		SHA512_CONST_72, SHA512_CONST_73, SHA512_CONST_74,
4097c478bd9Sstevel@tonic-gate 		SHA512_CONST_75, SHA512_CONST_76, SHA512_CONST_77,
4107c478bd9Sstevel@tonic-gate 		SHA512_CONST_78, SHA512_CONST_79
4117c478bd9Sstevel@tonic-gate 	};
41255553f71Sda73024 #endif	/* __sparc */
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	if ((uintptr_t)blk & 0x7) {		/* not 8-byte aligned? */
4167c478bd9Sstevel@tonic-gate 		bcopy(blk, ctx->buf_un.buf64,  sizeof (ctx->buf_un.buf64));
4177c478bd9Sstevel@tonic-gate 		blk = (uint8_t *)ctx->buf_un.buf64;
4187c478bd9Sstevel@tonic-gate 	}
4197c478bd9Sstevel@tonic-gate 
420734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
421f66d273dSizick 	w0 =  LOAD_BIG_64(blk + 8 * 0);
422f66d273dSizick 	SHA512ROUND(a, b, c, d, e, f, g, h, 0, w0);
423734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
424f66d273dSizick 	w1 =  LOAD_BIG_64(blk + 8 * 1);
425f66d273dSizick 	SHA512ROUND(h, a, b, c, d, e, f, g, 1, w1);
426734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
427f66d273dSizick 	w2 =  LOAD_BIG_64(blk + 8 * 2);
428f66d273dSizick 	SHA512ROUND(g, h, a, b, c, d, e, f, 2, w2);
429734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
430f66d273dSizick 	w3 =  LOAD_BIG_64(blk + 8 * 3);
431f66d273dSizick 	SHA512ROUND(f, g, h, a, b, c, d, e, 3, w3);
432734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
433f66d273dSizick 	w4 =  LOAD_BIG_64(blk + 8 * 4);
434f66d273dSizick 	SHA512ROUND(e, f, g, h, a, b, c, d, 4, w4);
435734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
436f66d273dSizick 	w5 =  LOAD_BIG_64(blk + 8 * 5);
437f66d273dSizick 	SHA512ROUND(d, e, f, g, h, a, b, c, 5, w5);
438734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
439f66d273dSizick 	w6 =  LOAD_BIG_64(blk + 8 * 6);
440f66d273dSizick 	SHA512ROUND(c, d, e, f, g, h, a, b, 6, w6);
441734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
442f66d273dSizick 	w7 =  LOAD_BIG_64(blk + 8 * 7);
443f66d273dSizick 	SHA512ROUND(b, c, d, e, f, g, h, a, 7, w7);
444734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
445f66d273dSizick 	w8 =  LOAD_BIG_64(blk + 8 * 8);
446f66d273dSizick 	SHA512ROUND(a, b, c, d, e, f, g, h, 8, w8);
447734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
448f66d273dSizick 	w9 =  LOAD_BIG_64(blk + 8 * 9);
449f66d273dSizick 	SHA512ROUND(h, a, b, c, d, e, f, g, 9, w9);
450734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
451f66d273dSizick 	w10 =  LOAD_BIG_64(blk + 8 * 10);
452f66d273dSizick 	SHA512ROUND(g, h, a, b, c, d, e, f, 10, w10);
453734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
454f66d273dSizick 	w11 =  LOAD_BIG_64(blk + 8 * 11);
455f66d273dSizick 	SHA512ROUND(f, g, h, a, b, c, d, e, 11, w11);
456734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
457f66d273dSizick 	w12 =  LOAD_BIG_64(blk + 8 * 12);
458f66d273dSizick 	SHA512ROUND(e, f, g, h, a, b, c, d, 12, w12);
459734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
460f66d273dSizick 	w13 =  LOAD_BIG_64(blk + 8 * 13);
461f66d273dSizick 	SHA512ROUND(d, e, f, g, h, a, b, c, 13, w13);
462734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
463f66d273dSizick 	w14 =  LOAD_BIG_64(blk + 8 * 14);
464f66d273dSizick 	SHA512ROUND(c, d, e, f, g, h, a, b, 14, w14);
465734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
466f66d273dSizick 	w15 =  LOAD_BIG_64(blk + 8 * 15);
467f66d273dSizick 	SHA512ROUND(b, c, d, e, f, g, h, a, 15, w15);
468f66d273dSizick 
4697c478bd9Sstevel@tonic-gate 	w0 = SIGMA1(w14) + w9 + SIGMA0(w1) + w0;
4707c478bd9Sstevel@tonic-gate 	SHA512ROUND(a, b, c, d, e, f, g, h, 16, w0);
4717c478bd9Sstevel@tonic-gate 	w1 = SIGMA1(w15) + w10 + SIGMA0(w2) + w1;
4727c478bd9Sstevel@tonic-gate 	SHA512ROUND(h, a, b, c, d, e, f, g, 17, w1);
4737c478bd9Sstevel@tonic-gate 	w2 = SIGMA1(w0) + w11 + SIGMA0(w3) + w2;
4747c478bd9Sstevel@tonic-gate 	SHA512ROUND(g, h, a, b, c, d, e, f, 18, w2);
4757c478bd9Sstevel@tonic-gate 	w3 = SIGMA1(w1) + w12 + SIGMA0(w4) + w3;
4767c478bd9Sstevel@tonic-gate 	SHA512ROUND(f, g, h, a, b, c, d, e, 19, w3);
4777c478bd9Sstevel@tonic-gate 	w4 = SIGMA1(w2) + w13 + SIGMA0(w5) + w4;
4787c478bd9Sstevel@tonic-gate 	SHA512ROUND(e, f, g, h, a, b, c, d, 20, w4);
4797c478bd9Sstevel@tonic-gate 	w5 = SIGMA1(w3) + w14 + SIGMA0(w6) + w5;
4807c478bd9Sstevel@tonic-gate 	SHA512ROUND(d, e, f, g, h, a, b, c, 21, w5);
4817c478bd9Sstevel@tonic-gate 	w6 = SIGMA1(w4) + w15 + SIGMA0(w7) + w6;
4827c478bd9Sstevel@tonic-gate 	SHA512ROUND(c, d, e, f, g, h, a, b, 22, w6);
4837c478bd9Sstevel@tonic-gate 	w7 = SIGMA1(w5) + w0 + SIGMA0(w8) + w7;
4847c478bd9Sstevel@tonic-gate 	SHA512ROUND(b, c, d, e, f, g, h, a, 23, w7);
4857c478bd9Sstevel@tonic-gate 	w8 = SIGMA1(w6) + w1 + SIGMA0(w9) + w8;
4867c478bd9Sstevel@tonic-gate 	SHA512ROUND(a, b, c, d, e, f, g, h, 24, w8);
4877c478bd9Sstevel@tonic-gate 	w9 = SIGMA1(w7) + w2 + SIGMA0(w10) + w9;
4887c478bd9Sstevel@tonic-gate 	SHA512ROUND(h, a, b, c, d, e, f, g, 25, w9);
4897c478bd9Sstevel@tonic-gate 	w10 = SIGMA1(w8) + w3 + SIGMA0(w11) + w10;
4907c478bd9Sstevel@tonic-gate 	SHA512ROUND(g, h, a, b, c, d, e, f, 26, w10);
4917c478bd9Sstevel@tonic-gate 	w11 = SIGMA1(w9) + w4 + SIGMA0(w12) + w11;
4927c478bd9Sstevel@tonic-gate 	SHA512ROUND(f, g, h, a, b, c, d, e, 27, w11);
4937c478bd9Sstevel@tonic-gate 	w12 = SIGMA1(w10) + w5 + SIGMA0(w13) + w12;
4947c478bd9Sstevel@tonic-gate 	SHA512ROUND(e, f, g, h, a, b, c, d, 28, w12);
4957c478bd9Sstevel@tonic-gate 	w13 = SIGMA1(w11) + w6 + SIGMA0(w14) + w13;
4967c478bd9Sstevel@tonic-gate 	SHA512ROUND(d, e, f, g, h, a, b, c, 29, w13);
4977c478bd9Sstevel@tonic-gate 	w14 = SIGMA1(w12) + w7 + SIGMA0(w15) + w14;
4987c478bd9Sstevel@tonic-gate 	SHA512ROUND(c, d, e, f, g, h, a, b, 30, w14);
4997c478bd9Sstevel@tonic-gate 	w15 = SIGMA1(w13) + w8 + SIGMA0(w0) + w15;
5007c478bd9Sstevel@tonic-gate 	SHA512ROUND(b, c, d, e, f, g, h, a, 31, w15);
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	w0 = SIGMA1(w14) + w9 + SIGMA0(w1) + w0;
5037c478bd9Sstevel@tonic-gate 	SHA512ROUND(a, b, c, d, e, f, g, h, 32, w0);
5047c478bd9Sstevel@tonic-gate 	w1 = SIGMA1(w15) + w10 + SIGMA0(w2) + w1;
5057c478bd9Sstevel@tonic-gate 	SHA512ROUND(h, a, b, c, d, e, f, g, 33, w1);
5067c478bd9Sstevel@tonic-gate 	w2 = SIGMA1(w0) + w11 + SIGMA0(w3) + w2;
5077c478bd9Sstevel@tonic-gate 	SHA512ROUND(g, h, a, b, c, d, e, f, 34, w2);
5087c478bd9Sstevel@tonic-gate 	w3 = SIGMA1(w1) + w12 + SIGMA0(w4) + w3;
5097c478bd9Sstevel@tonic-gate 	SHA512ROUND(f, g, h, a, b, c, d, e, 35, w3);
5107c478bd9Sstevel@tonic-gate 	w4 = SIGMA1(w2) + w13 + SIGMA0(w5) + w4;
5117c478bd9Sstevel@tonic-gate 	SHA512ROUND(e, f, g, h, a, b, c, d, 36, w4);
5127c478bd9Sstevel@tonic-gate 	w5 = SIGMA1(w3) + w14 + SIGMA0(w6) + w5;
5137c478bd9Sstevel@tonic-gate 	SHA512ROUND(d, e, f, g, h, a, b, c, 37, w5);
5147c478bd9Sstevel@tonic-gate 	w6 = SIGMA1(w4) + w15 + SIGMA0(w7) + w6;
5157c478bd9Sstevel@tonic-gate 	SHA512ROUND(c, d, e, f, g, h, a, b, 38, w6);
5167c478bd9Sstevel@tonic-gate 	w7 = SIGMA1(w5) + w0 + SIGMA0(w8) + w7;
5177c478bd9Sstevel@tonic-gate 	SHA512ROUND(b, c, d, e, f, g, h, a, 39, w7);
5187c478bd9Sstevel@tonic-gate 	w8 = SIGMA1(w6) + w1 + SIGMA0(w9) + w8;
5197c478bd9Sstevel@tonic-gate 	SHA512ROUND(a, b, c, d, e, f, g, h, 40, w8);
5207c478bd9Sstevel@tonic-gate 	w9 = SIGMA1(w7) + w2 + SIGMA0(w10) + w9;
5217c478bd9Sstevel@tonic-gate 	SHA512ROUND(h, a, b, c, d, e, f, g, 41, w9);
5227c478bd9Sstevel@tonic-gate 	w10 = SIGMA1(w8) + w3 + SIGMA0(w11) + w10;
5237c478bd9Sstevel@tonic-gate 	SHA512ROUND(g, h, a, b, c, d, e, f, 42, w10);
5247c478bd9Sstevel@tonic-gate 	w11 = SIGMA1(w9) + w4 + SIGMA0(w12) + w11;
5257c478bd9Sstevel@tonic-gate 	SHA512ROUND(f, g, h, a, b, c, d, e, 43, w11);
5267c478bd9Sstevel@tonic-gate 	w12 = SIGMA1(w10) + w5 + SIGMA0(w13) + w12;
5277c478bd9Sstevel@tonic-gate 	SHA512ROUND(e, f, g, h, a, b, c, d, 44, w12);
5287c478bd9Sstevel@tonic-gate 	w13 = SIGMA1(w11) + w6 + SIGMA0(w14) + w13;
5297c478bd9Sstevel@tonic-gate 	SHA512ROUND(d, e, f, g, h, a, b, c, 45, w13);
5307c478bd9Sstevel@tonic-gate 	w14 = SIGMA1(w12) + w7 + SIGMA0(w15) + w14;
5317c478bd9Sstevel@tonic-gate 	SHA512ROUND(c, d, e, f, g, h, a, b, 46, w14);
5327c478bd9Sstevel@tonic-gate 	w15 = SIGMA1(w13) + w8 + SIGMA0(w0) + w15;
5337c478bd9Sstevel@tonic-gate 	SHA512ROUND(b, c, d, e, f, g, h, a, 47, w15);
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	w0 = SIGMA1(w14) + w9 + SIGMA0(w1) + w0;
5367c478bd9Sstevel@tonic-gate 	SHA512ROUND(a, b, c, d, e, f, g, h, 48, w0);
5377c478bd9Sstevel@tonic-gate 	w1 = SIGMA1(w15) + w10 + SIGMA0(w2) + w1;
5387c478bd9Sstevel@tonic-gate 	SHA512ROUND(h, a, b, c, d, e, f, g, 49, w1);
5397c478bd9Sstevel@tonic-gate 	w2 = SIGMA1(w0) + w11 + SIGMA0(w3) + w2;
5407c478bd9Sstevel@tonic-gate 	SHA512ROUND(g, h, a, b, c, d, e, f, 50, w2);
5417c478bd9Sstevel@tonic-gate 	w3 = SIGMA1(w1) + w12 + SIGMA0(w4) + w3;
5427c478bd9Sstevel@tonic-gate 	SHA512ROUND(f, g, h, a, b, c, d, e, 51, w3);
5437c478bd9Sstevel@tonic-gate 	w4 = SIGMA1(w2) + w13 + SIGMA0(w5) + w4;
5447c478bd9Sstevel@tonic-gate 	SHA512ROUND(e, f, g, h, a, b, c, d, 52, w4);
5457c478bd9Sstevel@tonic-gate 	w5 = SIGMA1(w3) + w14 + SIGMA0(w6) + w5;
5467c478bd9Sstevel@tonic-gate 	SHA512ROUND(d, e, f, g, h, a, b, c, 53, w5);
5477c478bd9Sstevel@tonic-gate 	w6 = SIGMA1(w4) + w15 + SIGMA0(w7) + w6;
5487c478bd9Sstevel@tonic-gate 	SHA512ROUND(c, d, e, f, g, h, a, b, 54, w6);
5497c478bd9Sstevel@tonic-gate 	w7 = SIGMA1(w5) + w0 + SIGMA0(w8) + w7;
5507c478bd9Sstevel@tonic-gate 	SHA512ROUND(b, c, d, e, f, g, h, a, 55, w7);
5517c478bd9Sstevel@tonic-gate 	w8 = SIGMA1(w6) + w1 + SIGMA0(w9) + w8;
5527c478bd9Sstevel@tonic-gate 	SHA512ROUND(a, b, c, d, e, f, g, h, 56, w8);
5537c478bd9Sstevel@tonic-gate 	w9 = SIGMA1(w7) + w2 + SIGMA0(w10) + w9;
5547c478bd9Sstevel@tonic-gate 	SHA512ROUND(h, a, b, c, d, e, f, g, 57, w9);
5557c478bd9Sstevel@tonic-gate 	w10 = SIGMA1(w8) + w3 + SIGMA0(w11) + w10;
5567c478bd9Sstevel@tonic-gate 	SHA512ROUND(g, h, a, b, c, d, e, f, 58, w10);
5577c478bd9Sstevel@tonic-gate 	w11 = SIGMA1(w9) + w4 + SIGMA0(w12) + w11;
5587c478bd9Sstevel@tonic-gate 	SHA512ROUND(f, g, h, a, b, c, d, e, 59, w11);
5597c478bd9Sstevel@tonic-gate 	w12 = SIGMA1(w10) + w5 + SIGMA0(w13) + w12;
5607c478bd9Sstevel@tonic-gate 	SHA512ROUND(e, f, g, h, a, b, c, d, 60, w12);
5617c478bd9Sstevel@tonic-gate 	w13 = SIGMA1(w11) + w6 + SIGMA0(w14) + w13;
5627c478bd9Sstevel@tonic-gate 	SHA512ROUND(d, e, f, g, h, a, b, c, 61, w13);
5637c478bd9Sstevel@tonic-gate 	w14 = SIGMA1(w12) + w7 + SIGMA0(w15) + w14;
5647c478bd9Sstevel@tonic-gate 	SHA512ROUND(c, d, e, f, g, h, a, b, 62, w14);
5657c478bd9Sstevel@tonic-gate 	w15 = SIGMA1(w13) + w8 + SIGMA0(w0) + w15;
5667c478bd9Sstevel@tonic-gate 	SHA512ROUND(b, c, d, e, f, g, h, a, 63, w15);
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	w0 = SIGMA1(w14) + w9 + SIGMA0(w1) + w0;
5697c478bd9Sstevel@tonic-gate 	SHA512ROUND(a, b, c, d, e, f, g, h, 64, w0);
5707c478bd9Sstevel@tonic-gate 	w1 = SIGMA1(w15) + w10 + SIGMA0(w2) + w1;
5717c478bd9Sstevel@tonic-gate 	SHA512ROUND(h, a, b, c, d, e, f, g, 65, w1);
5727c478bd9Sstevel@tonic-gate 	w2 = SIGMA1(w0) + w11 + SIGMA0(w3) + w2;
5737c478bd9Sstevel@tonic-gate 	SHA512ROUND(g, h, a, b, c, d, e, f, 66, w2);
5747c478bd9Sstevel@tonic-gate 	w3 = SIGMA1(w1) + w12 + SIGMA0(w4) + w3;
5757c478bd9Sstevel@tonic-gate 	SHA512ROUND(f, g, h, a, b, c, d, e, 67, w3);
5767c478bd9Sstevel@tonic-gate 	w4 = SIGMA1(w2) + w13 + SIGMA0(w5) + w4;
5777c478bd9Sstevel@tonic-gate 	SHA512ROUND(e, f, g, h, a, b, c, d, 68, w4);
5787c478bd9Sstevel@tonic-gate 	w5 = SIGMA1(w3) + w14 + SIGMA0(w6) + w5;
5797c478bd9Sstevel@tonic-gate 	SHA512ROUND(d, e, f, g, h, a, b, c, 69, w5);
5807c478bd9Sstevel@tonic-gate 	w6 = SIGMA1(w4) + w15 + SIGMA0(w7) + w6;
5817c478bd9Sstevel@tonic-gate 	SHA512ROUND(c, d, e, f, g, h, a, b, 70, w6);
5827c478bd9Sstevel@tonic-gate 	w7 = SIGMA1(w5) + w0 + SIGMA0(w8) + w7;
5837c478bd9Sstevel@tonic-gate 	SHA512ROUND(b, c, d, e, f, g, h, a, 71, w7);
5847c478bd9Sstevel@tonic-gate 	w8 = SIGMA1(w6) + w1 + SIGMA0(w9) + w8;
5857c478bd9Sstevel@tonic-gate 	SHA512ROUND(a, b, c, d, e, f, g, h, 72, w8);
5867c478bd9Sstevel@tonic-gate 	w9 = SIGMA1(w7) + w2 + SIGMA0(w10) + w9;
5877c478bd9Sstevel@tonic-gate 	SHA512ROUND(h, a, b, c, d, e, f, g, 73, w9);
5887c478bd9Sstevel@tonic-gate 	w10 = SIGMA1(w8) + w3 + SIGMA0(w11) + w10;
5897c478bd9Sstevel@tonic-gate 	SHA512ROUND(g, h, a, b, c, d, e, f, 74, w10);
5907c478bd9Sstevel@tonic-gate 	w11 = SIGMA1(w9) + w4 + SIGMA0(w12) + w11;
5917c478bd9Sstevel@tonic-gate 	SHA512ROUND(f, g, h, a, b, c, d, e, 75, w11);
5927c478bd9Sstevel@tonic-gate 	w12 = SIGMA1(w10) + w5 + SIGMA0(w13) + w12;
5937c478bd9Sstevel@tonic-gate 	SHA512ROUND(e, f, g, h, a, b, c, d, 76, w12);
5947c478bd9Sstevel@tonic-gate 	w13 = SIGMA1(w11) + w6 + SIGMA0(w14) + w13;
5957c478bd9Sstevel@tonic-gate 	SHA512ROUND(d, e, f, g, h, a, b, c, 77, w13);
5967c478bd9Sstevel@tonic-gate 	w14 = SIGMA1(w12) + w7 + SIGMA0(w15) + w14;
5977c478bd9Sstevel@tonic-gate 	SHA512ROUND(c, d, e, f, g, h, a, b, 78, w14);
5987c478bd9Sstevel@tonic-gate 	w15 = SIGMA1(w13) + w8 + SIGMA0(w0) + w15;
5997c478bd9Sstevel@tonic-gate 	SHA512ROUND(b, c, d, e, f, g, h, a, 79, w15);
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	ctx->state.s64[0] += a;
6027c478bd9Sstevel@tonic-gate 	ctx->state.s64[1] += b;
6037c478bd9Sstevel@tonic-gate 	ctx->state.s64[2] += c;
6047c478bd9Sstevel@tonic-gate 	ctx->state.s64[3] += d;
6057c478bd9Sstevel@tonic-gate 	ctx->state.s64[4] += e;
6067c478bd9Sstevel@tonic-gate 	ctx->state.s64[5] += f;
6077c478bd9Sstevel@tonic-gate 	ctx->state.s64[6] += g;
6087c478bd9Sstevel@tonic-gate 	ctx->state.s64[7] += h;
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate }
61155553f71Sda73024 #endif	/* !__amd64 */
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate /*
6157c478bd9Sstevel@tonic-gate  * Encode()
6167c478bd9Sstevel@tonic-gate  *
6177c478bd9Sstevel@tonic-gate  * purpose: to convert a list of numbers from little endian to big endian
6187c478bd9Sstevel@tonic-gate  *   input: uint8_t *	: place to store the converted big endian numbers
6197c478bd9Sstevel@tonic-gate  *	    uint32_t *	: place to get numbers to convert from
6207c478bd9Sstevel@tonic-gate  *          size_t	: the length of the input in bytes
6217c478bd9Sstevel@tonic-gate  *  output: void
6227c478bd9Sstevel@tonic-gate  */
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate static void
Encode(uint8_t * _RESTRICT_KYWD output,uint32_t * _RESTRICT_KYWD input,size_t len)625734b6a94Sdarrenm Encode(uint8_t *_RESTRICT_KYWD output, uint32_t *_RESTRICT_KYWD input,
626734b6a94Sdarrenm     size_t len)
6277c478bd9Sstevel@tonic-gate {
6287c478bd9Sstevel@tonic-gate 	size_t		i, j;
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate #if	defined(__sparc)
6317c478bd9Sstevel@tonic-gate 	if (IS_P2ALIGNED(output, sizeof (uint32_t))) {
6327c478bd9Sstevel@tonic-gate 		for (i = 0, j = 0; j < len; i++, j += 4) {
6338de5c4f4SDan OpenSolaris Anderson 			/* LINTED E_BAD_PTR_CAST_ALIGN */
6347c478bd9Sstevel@tonic-gate 			*((uint32_t *)(output + j)) = input[i];
6357c478bd9Sstevel@tonic-gate 		}
6367c478bd9Sstevel@tonic-gate 	} else {
6377c478bd9Sstevel@tonic-gate #endif	/* little endian -- will work on big endian, but slowly */
6387c478bd9Sstevel@tonic-gate 		for (i = 0, j = 0; j < len; i++, j += 4) {
6397c478bd9Sstevel@tonic-gate 			output[j]	= (input[i] >> 24) & 0xff;
6407c478bd9Sstevel@tonic-gate 			output[j + 1]	= (input[i] >> 16) & 0xff;
6417c478bd9Sstevel@tonic-gate 			output[j + 2]	= (input[i] >>  8) & 0xff;
6427c478bd9Sstevel@tonic-gate 			output[j + 3]	= input[i] & 0xff;
6437c478bd9Sstevel@tonic-gate 		}
6447c478bd9Sstevel@tonic-gate #if	defined(__sparc)
6457c478bd9Sstevel@tonic-gate 	}
6467c478bd9Sstevel@tonic-gate #endif
6477c478bd9Sstevel@tonic-gate }
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate static void
Encode64(uint8_t * _RESTRICT_KYWD output,uint64_t * _RESTRICT_KYWD input,size_t len)650734b6a94Sdarrenm Encode64(uint8_t *_RESTRICT_KYWD output, uint64_t *_RESTRICT_KYWD input,
651734b6a94Sdarrenm     size_t len)
6527c478bd9Sstevel@tonic-gate {
6537c478bd9Sstevel@tonic-gate 	size_t		i, j;
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate #if	defined(__sparc)
6567c478bd9Sstevel@tonic-gate 	if (IS_P2ALIGNED(output, sizeof (uint64_t))) {
6577c478bd9Sstevel@tonic-gate 		for (i = 0, j = 0; j < len; i++, j += 8) {
6588de5c4f4SDan OpenSolaris Anderson 			/* LINTED E_BAD_PTR_CAST_ALIGN */
6597c478bd9Sstevel@tonic-gate 			*((uint64_t *)(output + j)) = input[i];
6607c478bd9Sstevel@tonic-gate 		}
6617c478bd9Sstevel@tonic-gate 	} else {
6627c478bd9Sstevel@tonic-gate #endif	/* little endian -- will work on big endian, but slowly */
6637c478bd9Sstevel@tonic-gate 		for (i = 0, j = 0; j < len; i++, j += 8) {
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 			output[j]	= (input[i] >> 56) & 0xff;
6667c478bd9Sstevel@tonic-gate 			output[j + 1]	= (input[i] >> 48) & 0xff;
6677c478bd9Sstevel@tonic-gate 			output[j + 2]	= (input[i] >> 40) & 0xff;
6687c478bd9Sstevel@tonic-gate 			output[j + 3]	= (input[i] >> 32) & 0xff;
6697c478bd9Sstevel@tonic-gate 			output[j + 4]	= (input[i] >> 24) & 0xff;
6707c478bd9Sstevel@tonic-gate 			output[j + 5]	= (input[i] >> 16) & 0xff;
6717c478bd9Sstevel@tonic-gate 			output[j + 6]	= (input[i] >>  8) & 0xff;
6727c478bd9Sstevel@tonic-gate 			output[j + 7]	= input[i] & 0xff;
6737c478bd9Sstevel@tonic-gate 		}
6747c478bd9Sstevel@tonic-gate #if	defined(__sparc)
6757c478bd9Sstevel@tonic-gate 	}
6767c478bd9Sstevel@tonic-gate #endif
6777c478bd9Sstevel@tonic-gate }
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate void
SHA2Init(uint64_t mech,SHA2_CTX * ctx)6817c478bd9Sstevel@tonic-gate SHA2Init(uint64_t mech, SHA2_CTX *ctx)
6827c478bd9Sstevel@tonic-gate {
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	switch (mech) {
6857c478bd9Sstevel@tonic-gate 	case SHA256_MECH_INFO_TYPE:
6867c478bd9Sstevel@tonic-gate 	case SHA256_HMAC_MECH_INFO_TYPE:
6877c478bd9Sstevel@tonic-gate 	case SHA256_HMAC_GEN_MECH_INFO_TYPE:
6887c478bd9Sstevel@tonic-gate 		ctx->state.s32[0] = 0x6a09e667U;
6897c478bd9Sstevel@tonic-gate 		ctx->state.s32[1] = 0xbb67ae85U;
6907c478bd9Sstevel@tonic-gate 		ctx->state.s32[2] = 0x3c6ef372U;
6917c478bd9Sstevel@tonic-gate 		ctx->state.s32[3] = 0xa54ff53aU;
6927c478bd9Sstevel@tonic-gate 		ctx->state.s32[4] = 0x510e527fU;
6937c478bd9Sstevel@tonic-gate 		ctx->state.s32[5] = 0x9b05688cU;
6947c478bd9Sstevel@tonic-gate 		ctx->state.s32[6] = 0x1f83d9abU;
6957c478bd9Sstevel@tonic-gate 		ctx->state.s32[7] = 0x5be0cd19U;
6967c478bd9Sstevel@tonic-gate 		break;
6977c478bd9Sstevel@tonic-gate 	case SHA384_MECH_INFO_TYPE:
6987c478bd9Sstevel@tonic-gate 	case SHA384_HMAC_MECH_INFO_TYPE:
6997c478bd9Sstevel@tonic-gate 	case SHA384_HMAC_GEN_MECH_INFO_TYPE:
7007c478bd9Sstevel@tonic-gate 		ctx->state.s64[0] = 0xcbbb9d5dc1059ed8ULL;
7017c478bd9Sstevel@tonic-gate 		ctx->state.s64[1] = 0x629a292a367cd507ULL;
7027c478bd9Sstevel@tonic-gate 		ctx->state.s64[2] = 0x9159015a3070dd17ULL;
7037c478bd9Sstevel@tonic-gate 		ctx->state.s64[3] = 0x152fecd8f70e5939ULL;
7047c478bd9Sstevel@tonic-gate 		ctx->state.s64[4] = 0x67332667ffc00b31ULL;
7057c478bd9Sstevel@tonic-gate 		ctx->state.s64[5] = 0x8eb44a8768581511ULL;
7067c478bd9Sstevel@tonic-gate 		ctx->state.s64[6] = 0xdb0c2e0d64f98fa7ULL;
7077c478bd9Sstevel@tonic-gate 		ctx->state.s64[7] = 0x47b5481dbefa4fa4ULL;
7087c478bd9Sstevel@tonic-gate 		break;
7097c478bd9Sstevel@tonic-gate 	case SHA512_MECH_INFO_TYPE:
7107c478bd9Sstevel@tonic-gate 	case SHA512_HMAC_MECH_INFO_TYPE:
7117c478bd9Sstevel@tonic-gate 	case SHA512_HMAC_GEN_MECH_INFO_TYPE:
7127c478bd9Sstevel@tonic-gate 		ctx->state.s64[0] = 0x6a09e667f3bcc908ULL;
7137c478bd9Sstevel@tonic-gate 		ctx->state.s64[1] = 0xbb67ae8584caa73bULL;
7147c478bd9Sstevel@tonic-gate 		ctx->state.s64[2] = 0x3c6ef372fe94f82bULL;
7157c478bd9Sstevel@tonic-gate 		ctx->state.s64[3] = 0xa54ff53a5f1d36f1ULL;
7167c478bd9Sstevel@tonic-gate 		ctx->state.s64[4] = 0x510e527fade682d1ULL;
7177c478bd9Sstevel@tonic-gate 		ctx->state.s64[5] = 0x9b05688c2b3e6c1fULL;
7187c478bd9Sstevel@tonic-gate 		ctx->state.s64[6] = 0x1f83d9abfb41bd6bULL;
7197c478bd9Sstevel@tonic-gate 		ctx->state.s64[7] = 0x5be0cd19137e2179ULL;
7207c478bd9Sstevel@tonic-gate 		break;
721*45818ee1SMatthew Ahrens 	case SHA512_224_MECH_INFO_TYPE:
722*45818ee1SMatthew Ahrens 		ctx->state.s64[0] = 0x8C3D37C819544DA2ULL;
723*45818ee1SMatthew Ahrens 		ctx->state.s64[1] = 0x73E1996689DCD4D6ULL;
724*45818ee1SMatthew Ahrens 		ctx->state.s64[2] = 0x1DFAB7AE32FF9C82ULL;
725*45818ee1SMatthew Ahrens 		ctx->state.s64[3] = 0x679DD514582F9FCFULL;
726*45818ee1SMatthew Ahrens 		ctx->state.s64[4] = 0x0F6D2B697BD44DA8ULL;
727*45818ee1SMatthew Ahrens 		ctx->state.s64[5] = 0x77E36F7304C48942ULL;
728*45818ee1SMatthew Ahrens 		ctx->state.s64[6] = 0x3F9D85A86A1D36C8ULL;
729*45818ee1SMatthew Ahrens 		ctx->state.s64[7] = 0x1112E6AD91D692A1ULL;
730*45818ee1SMatthew Ahrens 		break;
731*45818ee1SMatthew Ahrens 	case SHA512_256_MECH_INFO_TYPE:
732*45818ee1SMatthew Ahrens 		ctx->state.s64[0] = 0x22312194FC2BF72CULL;
733*45818ee1SMatthew Ahrens 		ctx->state.s64[1] = 0x9F555FA3C84C64C2ULL;
734*45818ee1SMatthew Ahrens 		ctx->state.s64[2] = 0x2393B86B6F53B151ULL;
735*45818ee1SMatthew Ahrens 		ctx->state.s64[3] = 0x963877195940EABDULL;
736*45818ee1SMatthew Ahrens 		ctx->state.s64[4] = 0x96283EE2A88EFFE3ULL;
737*45818ee1SMatthew Ahrens 		ctx->state.s64[5] = 0xBE5E1E2553863992ULL;
738*45818ee1SMatthew Ahrens 		ctx->state.s64[6] = 0x2B0199FC2C85B8AAULL;
739*45818ee1SMatthew Ahrens 		ctx->state.s64[7] = 0x0EB72DDC81C52CA2ULL;
740*45818ee1SMatthew Ahrens 		break;
7417c478bd9Sstevel@tonic-gate #ifdef _KERNEL
7427c478bd9Sstevel@tonic-gate 	default:
7434b56a003SDaniel Anderson 		cmn_err(CE_PANIC,
7444b56a003SDaniel Anderson 		    "sha2_init: failed to find a supported algorithm: 0x%x",
7457c478bd9Sstevel@tonic-gate 		    (uint32_t)mech);
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
7487c478bd9Sstevel@tonic-gate 	}
7497c478bd9Sstevel@tonic-gate 
7508de5c4f4SDan OpenSolaris Anderson 	ctx->algotype = (uint32_t)mech;
7517c478bd9Sstevel@tonic-gate 	ctx->count.c64[0] = ctx->count.c64[1] = 0;
7527c478bd9Sstevel@tonic-gate }
7537c478bd9Sstevel@tonic-gate 
754734b6a94Sdarrenm #ifndef _KERNEL
755734b6a94Sdarrenm 
756734b6a94Sdarrenm #pragma inline(SHA256Init, SHA384Init, SHA512Init)
757734b6a94Sdarrenm void
SHA256Init(SHA256_CTX * ctx)758734b6a94Sdarrenm SHA256Init(SHA256_CTX *ctx)
759734b6a94Sdarrenm {
760734b6a94Sdarrenm 	SHA2Init(SHA256, ctx);
761734b6a94Sdarrenm }
762734b6a94Sdarrenm 
763734b6a94Sdarrenm void
SHA384Init(SHA384_CTX * ctx)764734b6a94Sdarrenm SHA384Init(SHA384_CTX *ctx)
765734b6a94Sdarrenm {
766734b6a94Sdarrenm 	SHA2Init(SHA384, ctx);
767734b6a94Sdarrenm }
768734b6a94Sdarrenm 
769734b6a94Sdarrenm void
SHA512Init(SHA512_CTX * ctx)770734b6a94Sdarrenm SHA512Init(SHA512_CTX *ctx)
771734b6a94Sdarrenm {
772734b6a94Sdarrenm 	SHA2Init(SHA512, ctx);
773734b6a94Sdarrenm }
774734b6a94Sdarrenm 
775734b6a94Sdarrenm #endif /* _KERNEL */
776734b6a94Sdarrenm 
7777c478bd9Sstevel@tonic-gate /*
7787c478bd9Sstevel@tonic-gate  * SHA2Update()
7797c478bd9Sstevel@tonic-gate  *
7807c478bd9Sstevel@tonic-gate  * purpose: continues an sha2 digest operation, using the message block
7817c478bd9Sstevel@tonic-gate  *          to update the context.
7827c478bd9Sstevel@tonic-gate  *   input: SHA2_CTX *	: the context to update
783734b6a94Sdarrenm  *          void *	: the message block
78455553f71Sda73024  *          size_t      : the length of the message block, in bytes
7857c478bd9Sstevel@tonic-gate  *  output: void
7867c478bd9Sstevel@tonic-gate  */
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate void
SHA2Update(SHA2_CTX * ctx,const void * inptr,size_t input_len)789734b6a94Sdarrenm SHA2Update(SHA2_CTX *ctx, const void *inptr, size_t input_len)
7907c478bd9Sstevel@tonic-gate {
7917c478bd9Sstevel@tonic-gate 	uint32_t	i, buf_index, buf_len, buf_limit;
792734b6a94Sdarrenm 	const uint8_t	*input = inptr;
79355553f71Sda73024 	uint32_t	algotype = ctx->algotype;
79455553f71Sda73024 #if defined(__amd64)
79555553f71Sda73024 	uint32_t	block_count;
79655553f71Sda73024 #endif	/* !__amd64 */
79755553f71Sda73024 
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	/* check for noop */
8007c478bd9Sstevel@tonic-gate 	if (input_len == 0)
8017c478bd9Sstevel@tonic-gate 		return;
8027c478bd9Sstevel@tonic-gate 
80355553f71Sda73024 	if (algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE) {
8047c478bd9Sstevel@tonic-gate 		buf_limit = 64;
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 		/* compute number of bytes mod 64 */
8077c478bd9Sstevel@tonic-gate 		buf_index = (ctx->count.c32[1] >> 3) & 0x3F;
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 		/* update number of bits */
8107c478bd9Sstevel@tonic-gate 		if ((ctx->count.c32[1] += (input_len << 3)) < (input_len << 3))
8117c478bd9Sstevel@tonic-gate 			ctx->count.c32[0]++;
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 		ctx->count.c32[0] += (input_len >> 29);
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 	} else {
8167c478bd9Sstevel@tonic-gate 		buf_limit = 128;
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 		/* compute number of bytes mod 128 */
8197c478bd9Sstevel@tonic-gate 		buf_index = (ctx->count.c64[1] >> 3) & 0x7F;
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 		/* update number of bits */
8227c478bd9Sstevel@tonic-gate 		if ((ctx->count.c64[1] += (input_len << 3)) < (input_len << 3))
8237c478bd9Sstevel@tonic-gate 			ctx->count.c64[0]++;
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 		ctx->count.c64[0] += (input_len >> 29);
8267c478bd9Sstevel@tonic-gate 	}
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	buf_len = buf_limit - buf_index;
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 	/* transform as many times as possible */
8317c478bd9Sstevel@tonic-gate 	i = 0;
8327c478bd9Sstevel@tonic-gate 	if (input_len >= buf_len) {
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 		/*
8357c478bd9Sstevel@tonic-gate 		 * general optimization:
8367c478bd9Sstevel@tonic-gate 		 *
8377c478bd9Sstevel@tonic-gate 		 * only do initial bcopy() and SHA2Transform() if
8387c478bd9Sstevel@tonic-gate 		 * buf_index != 0.  if buf_index == 0, we're just
8397c478bd9Sstevel@tonic-gate 		 * wasting our time doing the bcopy() since there
8407c478bd9Sstevel@tonic-gate 		 * wasn't any data left over from a previous call to
8417c478bd9Sstevel@tonic-gate 		 * SHA2Update().
8427c478bd9Sstevel@tonic-gate 		 */
8437c478bd9Sstevel@tonic-gate 		if (buf_index) {
8447c478bd9Sstevel@tonic-gate 			bcopy(input, &ctx->buf_un.buf8[buf_index], buf_len);
84555553f71Sda73024 			if (algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE)
8467c478bd9Sstevel@tonic-gate 				SHA256Transform(ctx, ctx->buf_un.buf8);
8477c478bd9Sstevel@tonic-gate 			else
8487c478bd9Sstevel@tonic-gate 				SHA512Transform(ctx, ctx->buf_un.buf8);
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 			i = buf_len;
8517c478bd9Sstevel@tonic-gate 		}
8527c478bd9Sstevel@tonic-gate 
85355553f71Sda73024 #if !defined(__amd64)
85455553f71Sda73024 		if (algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE) {
8557c478bd9Sstevel@tonic-gate 			for (; i + buf_limit - 1 < input_len; i += buf_limit) {
8567c478bd9Sstevel@tonic-gate 				SHA256Transform(ctx, &input[i]);
85755553f71Sda73024 			}
85855553f71Sda73024 		} else {
85955553f71Sda73024 			for (; i + buf_limit - 1 < input_len; i += buf_limit) {
8607c478bd9Sstevel@tonic-gate 				SHA512Transform(ctx, &input[i]);
8617c478bd9Sstevel@tonic-gate 			}
86255553f71Sda73024 		}
86355553f71Sda73024 
86455553f71Sda73024 #else
86555553f71Sda73024 		if (algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE) {
86655553f71Sda73024 			block_count = (input_len - i) >> 6;
86755553f71Sda73024 			if (block_count > 0) {
86855553f71Sda73024 				SHA256TransformBlocks(ctx, &input[i],
86955553f71Sda73024 				    block_count);
87055553f71Sda73024 				i += block_count << 6;
87155553f71Sda73024 			}
87255553f71Sda73024 		} else {
87355553f71Sda73024 			block_count = (input_len - i) >> 7;
87455553f71Sda73024 			if (block_count > 0) {
87555553f71Sda73024 				SHA512TransformBlocks(ctx, &input[i],
87655553f71Sda73024 				    block_count);
87755553f71Sda73024 				i += block_count << 7;
87855553f71Sda73024 			}
87955553f71Sda73024 		}
88055553f71Sda73024 #endif	/* !__amd64 */
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 		/*
8837c478bd9Sstevel@tonic-gate 		 * general optimization:
8847c478bd9Sstevel@tonic-gate 		 *
8857c478bd9Sstevel@tonic-gate 		 * if i and input_len are the same, return now instead
8867c478bd9Sstevel@tonic-gate 		 * of calling bcopy(), since the bcopy() in this case
88755553f71Sda73024 		 * will be an expensive noop.
8887c478bd9Sstevel@tonic-gate 		 */
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 		if (input_len == i)
8917c478bd9Sstevel@tonic-gate 			return;
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 		buf_index = 0;
8947c478bd9Sstevel@tonic-gate 	}
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	/* buffer remaining input */
8977c478bd9Sstevel@tonic-gate 	bcopy(&input[i], &ctx->buf_un.buf8[buf_index], input_len - i);
8987c478bd9Sstevel@tonic-gate }
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate /*
9027c478bd9Sstevel@tonic-gate  * SHA2Final()
9037c478bd9Sstevel@tonic-gate  *
9047c478bd9Sstevel@tonic-gate  * purpose: ends an sha2 digest operation, finalizing the message digest and
9057c478bd9Sstevel@tonic-gate  *          zeroing the context.
90655553f71Sda73024  *   input: uchar_t *	: a buffer to store the digest
9075151fb12Sdarrenm  *			: The function actually uses void* because many
9085151fb12Sdarrenm  *			: callers pass things other than uchar_t here.
9097c478bd9Sstevel@tonic-gate  *          SHA2_CTX *  : the context to finalize, save, and zero
9107c478bd9Sstevel@tonic-gate  *  output: void
9117c478bd9Sstevel@tonic-gate  */
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate void
SHA2Final(void * digest,SHA2_CTX * ctx)914734b6a94Sdarrenm SHA2Final(void *digest, SHA2_CTX *ctx)
9157c478bd9Sstevel@tonic-gate {
9167c478bd9Sstevel@tonic-gate 	uint8_t		bitcount_be[sizeof (ctx->count.c32)];
9177c478bd9Sstevel@tonic-gate 	uint8_t		bitcount_be64[sizeof (ctx->count.c64)];
9187c478bd9Sstevel@tonic-gate 	uint32_t	index;
91955553f71Sda73024 	uint32_t	algotype = ctx->algotype;
9207c478bd9Sstevel@tonic-gate 
92155553f71Sda73024 	if (algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE) {
9227c478bd9Sstevel@tonic-gate 		index  = (ctx->count.c32[1] >> 3) & 0x3f;
9237c478bd9Sstevel@tonic-gate 		Encode(bitcount_be, ctx->count.c32, sizeof (bitcount_be));
9247c478bd9Sstevel@tonic-gate 		SHA2Update(ctx, PADDING, ((index < 56) ? 56 : 120) - index);
9257c478bd9Sstevel@tonic-gate 		SHA2Update(ctx, bitcount_be, sizeof (bitcount_be));
9267c478bd9Sstevel@tonic-gate 		Encode(digest, ctx->state.s32, sizeof (ctx->state.s32));
9277c478bd9Sstevel@tonic-gate 	} else {
9287c478bd9Sstevel@tonic-gate 		index  = (ctx->count.c64[1] >> 3) & 0x7f;
9297c478bd9Sstevel@tonic-gate 		Encode64(bitcount_be64, ctx->count.c64,
9307c478bd9Sstevel@tonic-gate 		    sizeof (bitcount_be64));
9317c478bd9Sstevel@tonic-gate 		SHA2Update(ctx, PADDING, ((index < 112) ? 112 : 240) - index);
9327c478bd9Sstevel@tonic-gate 		SHA2Update(ctx, bitcount_be64, sizeof (bitcount_be64));
93355553f71Sda73024 		if (algotype <= SHA384_HMAC_GEN_MECH_INFO_TYPE) {
9347c478bd9Sstevel@tonic-gate 			ctx->state.s64[6] = ctx->state.s64[7] = 0;
9357c478bd9Sstevel@tonic-gate 			Encode64(digest, ctx->state.s64,
9367c478bd9Sstevel@tonic-gate 			    sizeof (uint64_t) * 6);
937*45818ee1SMatthew Ahrens 		} else if (algotype == SHA512_224_MECH_INFO_TYPE) {
938*45818ee1SMatthew Ahrens 			uint8_t last[sizeof (uint64_t)];
939*45818ee1SMatthew Ahrens 			/*
940*45818ee1SMatthew Ahrens 			 * Since SHA-512/224 doesn't align well to 64-bit
941*45818ee1SMatthew Ahrens 			 * boundaries, we must do the encoding in three steps:
942*45818ee1SMatthew Ahrens 			 * 1) encode the three 64-bit words that fit neatly
943*45818ee1SMatthew Ahrens 			 * 2) encode the last 64-bit word to a temp buffer
944*45818ee1SMatthew Ahrens 			 * 3) chop out the lower 32-bits from the temp buffer
945*45818ee1SMatthew Ahrens 			 *    and append them to the digest
946*45818ee1SMatthew Ahrens 			 */
947*45818ee1SMatthew Ahrens 			Encode64(digest, ctx->state.s64, sizeof (uint64_t) * 3);
948*45818ee1SMatthew Ahrens 			Encode64(last, &ctx->state.s64[3], sizeof (uint64_t));
949*45818ee1SMatthew Ahrens 			bcopy(last, (uint8_t *)digest + 24, 4);
950*45818ee1SMatthew Ahrens 		} else if (algotype == SHA512_256_MECH_INFO_TYPE) {
951*45818ee1SMatthew Ahrens 			Encode64(digest, ctx->state.s64, sizeof (uint64_t) * 4);
952*45818ee1SMatthew Ahrens 		} else {
9537c478bd9Sstevel@tonic-gate 			Encode64(digest, ctx->state.s64,
9547c478bd9Sstevel@tonic-gate 			    sizeof (ctx->state.s64));
9557c478bd9Sstevel@tonic-gate 		}
956*45818ee1SMatthew Ahrens 	}
957673007c6Sdarrenm 
958673007c6Sdarrenm 	/* zeroize sensitive information */
959673007c6Sdarrenm 	bzero(ctx, sizeof (*ctx));
9607c478bd9Sstevel@tonic-gate }
961