xref: /titanic_50/usr/src/common/crypto/sha2/sha2.c (revision 734b6a94890be549309b21156f8ed6d4561cac51)
17c478bd9Sstevel@tonic-gate /*
2673007c6Sdarrenm  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
77c478bd9Sstevel@tonic-gate 
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
337c478bd9Sstevel@tonic-gate  * standard, available at http://www.itl.nist.gov/div897/pubs/fip180-2.htm
347c478bd9Sstevel@tonic-gate  * Not as fast as one would like -- further optimizations are encouraged
357c478bd9Sstevel@tonic-gate  * and appreciated.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate #include <sys/param.h>
407c478bd9Sstevel@tonic-gate #include <sys/systm.h>
417c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
42*734b6a94Sdarrenm #define	_SHA2_IMPL
437c478bd9Sstevel@tonic-gate #include <sys/sha2.h>
447c478bd9Sstevel@tonic-gate #include <sys/sha2_consts.h>
457c478bd9Sstevel@tonic-gate 
46*734b6a94Sdarrenm #ifndef _KERNEL
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #include <strings.h>
497c478bd9Sstevel@tonic-gate #include <stdlib.h>
507c478bd9Sstevel@tonic-gate #include <errno.h>
517c478bd9Sstevel@tonic-gate 
52*734b6a94Sdarrenm #pragma weak SHA256Update = SHA2Update
53*734b6a94Sdarrenm #pragma weak SHA384Update = SHA2Update
54*734b6a94Sdarrenm #pragma weak SHA512Update = SHA2Update
55*734b6a94Sdarrenm 
56*734b6a94Sdarrenm #pragma weak SHA256Final = SHA2Final
57*734b6a94Sdarrenm #pragma weak SHA384Final = SHA2Final
58*734b6a94Sdarrenm #pragma weak SHA512Final = SHA2Final
59*734b6a94Sdarrenm 
607c478bd9Sstevel@tonic-gate #endif	/* !_KERNEL */
617c478bd9Sstevel@tonic-gate 
62*734b6a94Sdarrenm #ifdef _KERNEL
63*734b6a94Sdarrenm #include <sys/cmn_err.h>
64*734b6a94Sdarrenm #endif /* _KERNEL */
65*734b6a94Sdarrenm 
667c478bd9Sstevel@tonic-gate static void Encode(uint8_t *, uint32_t *, size_t);
677c478bd9Sstevel@tonic-gate static void Encode64(uint8_t *, uint64_t *, size_t);
687c478bd9Sstevel@tonic-gate static void SHA256Transform(SHA2_CTX *, const uint8_t *);
697c478bd9Sstevel@tonic-gate static void SHA512Transform(SHA2_CTX *, const uint8_t *);
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate static uint8_t PADDING[128] = { 0x80, /* all zeros */ };
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /* Ch and Maj are the basic SHA2 functions. */
747c478bd9Sstevel@tonic-gate #define	Ch(b, c, d)	(((b) & (c)) ^ ((~b) & (d)))
757c478bd9Sstevel@tonic-gate #define	Maj(b, c, d)	(((b) & (c)) ^ ((b) & (d)) ^ ((c) & (d)))
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /* Rotates x right n bits. */
787c478bd9Sstevel@tonic-gate #define	ROTR(x, n)	\
797c478bd9Sstevel@tonic-gate 	(((x) >> (n)) | ((x) << ((sizeof (x) * NBBY)-(n))))
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /* Shift x right n bits */
827c478bd9Sstevel@tonic-gate #define	SHR(x, n)	((x) >> (n))
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /* SHA256 Functions */
857c478bd9Sstevel@tonic-gate #define	BIGSIGMA0_256(x)	(ROTR((x), 2) ^ ROTR((x), 13) ^ ROTR((x), 22))
867c478bd9Sstevel@tonic-gate #define	BIGSIGMA1_256(x)	(ROTR((x), 6) ^ ROTR((x), 11) ^ ROTR((x), 25))
877c478bd9Sstevel@tonic-gate #define	SIGMA0_256(x)		(ROTR((x), 7) ^ ROTR((x), 18) ^ SHR((x), 3))
887c478bd9Sstevel@tonic-gate #define	SIGMA1_256(x)		(ROTR((x), 17) ^ ROTR((x), 19) ^ SHR((x), 10))
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate #define	SHA256ROUND(a, b, c, d, e, f, g, h, i, w)			\
917c478bd9Sstevel@tonic-gate 	T1 = h + BIGSIGMA1_256(e) + Ch(e, f, g) + SHA256_CONST(i) + w;	\
927c478bd9Sstevel@tonic-gate 	d += T1;							\
937c478bd9Sstevel@tonic-gate 	T2 = BIGSIGMA0_256(a) + Maj(a, b, c);				\
947c478bd9Sstevel@tonic-gate 	h = T1 + T2
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /* SHA384/512 Functions */
977c478bd9Sstevel@tonic-gate #define	BIGSIGMA0(x)	(ROTR((x), 28) ^ ROTR((x), 34) ^ ROTR((x), 39))
987c478bd9Sstevel@tonic-gate #define	BIGSIGMA1(x)	(ROTR((x), 14) ^ ROTR((x), 18) ^ ROTR((x), 41))
997c478bd9Sstevel@tonic-gate #define	SIGMA0(x)	(ROTR((x), 1) ^ ROTR((x), 8) ^ SHR((x), 7))
1007c478bd9Sstevel@tonic-gate #define	SIGMA1(x)	(ROTR((x), 19) ^ ROTR((x), 61) ^ SHR((x), 6))
1017c478bd9Sstevel@tonic-gate #define	SHA512ROUND(a, b, c, d, e, f, g, h, i, w)			\
1027c478bd9Sstevel@tonic-gate 	T1 = h + BIGSIGMA1(e) + Ch(e, f, g) + SHA512_CONST(i) + w;	\
1037c478bd9Sstevel@tonic-gate 	d += T1;							\
1047c478bd9Sstevel@tonic-gate 	T2 = BIGSIGMA0(a) + Maj(a, b, c);				\
1057c478bd9Sstevel@tonic-gate 	h = T1 + T2
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate  * sparc optimization:
1097c478bd9Sstevel@tonic-gate  *
1107c478bd9Sstevel@tonic-gate  * on the sparc, we can load big endian 32-bit data easily.  note that
1117c478bd9Sstevel@tonic-gate  * special care must be taken to ensure the address is 32-bit aligned.
1127c478bd9Sstevel@tonic-gate  * in the interest of speed, we don't check to make sure, since
1137c478bd9Sstevel@tonic-gate  * careful programming can guarantee this for us.
1147c478bd9Sstevel@tonic-gate  */
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate #if	defined(_BIG_ENDIAN)
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate #define	LOAD_BIG_32(addr)	(*(uint32_t *)(addr))
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate #else	/* little endian -- will work on big endian, but slowly */
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate #define	LOAD_BIG_32(addr)	\
1237c478bd9Sstevel@tonic-gate 	(((addr)[0] << 24) | ((addr)[1] << 16) | ((addr)[2] << 8) | (addr)[3])
1247c478bd9Sstevel@tonic-gate #endif
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate #if	defined(_BIG_ENDIAN)
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate #define	LOAD_BIG_64(addr)	(*(uint64_t *)(addr))
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate #else	/* little endian -- will work on big endian, but slowly */
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate #define	LOAD_BIG_64(addr)	\
1347c478bd9Sstevel@tonic-gate 	(((uint64_t)(addr)[0] << 56) | ((uint64_t)(addr)[1] << 48) |	\
1357c478bd9Sstevel@tonic-gate 	    ((uint64_t)(addr)[2] << 40) | ((uint64_t)(addr)[3] << 32) |	\
1367c478bd9Sstevel@tonic-gate 	    ((uint64_t)(addr)[4] << 24) | ((uint64_t)(addr)[5] << 16) |	\
1377c478bd9Sstevel@tonic-gate 	    ((uint64_t)(addr)[6] << 8) | (uint64_t)(addr)[7])
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate #endif
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate /* SHA256 Transform */
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate static void
1457c478bd9Sstevel@tonic-gate SHA256Transform(SHA2_CTX *ctx, const uint8_t *blk)
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	uint32_t a = ctx->state.s32[0];
1497c478bd9Sstevel@tonic-gate 	uint32_t b = ctx->state.s32[1];
1507c478bd9Sstevel@tonic-gate 	uint32_t c = ctx->state.s32[2];
1517c478bd9Sstevel@tonic-gate 	uint32_t d = ctx->state.s32[3];
1527c478bd9Sstevel@tonic-gate 	uint32_t e = ctx->state.s32[4];
1537c478bd9Sstevel@tonic-gate 	uint32_t f = ctx->state.s32[5];
1547c478bd9Sstevel@tonic-gate 	uint32_t g = ctx->state.s32[6];
1557c478bd9Sstevel@tonic-gate 	uint32_t h = ctx->state.s32[7];
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	uint32_t w0, w1, w2, w3, w4, w5, w6, w7;
1587c478bd9Sstevel@tonic-gate 	uint32_t w8, w9, w10, w11, w12, w13, w14, w15;
1597c478bd9Sstevel@tonic-gate 	uint32_t T1, T2;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate #if	defined(__sparc)
1627c478bd9Sstevel@tonic-gate 	static const uint32_t sha256_consts[] = {
1637c478bd9Sstevel@tonic-gate 		SHA256_CONST_0, SHA256_CONST_1, SHA256_CONST_2,
1647c478bd9Sstevel@tonic-gate 		SHA256_CONST_3, SHA256_CONST_4, SHA256_CONST_5,
1657c478bd9Sstevel@tonic-gate 		SHA256_CONST_6, SHA256_CONST_7, SHA256_CONST_8,
1667c478bd9Sstevel@tonic-gate 		SHA256_CONST_9, SHA256_CONST_10, SHA256_CONST_11,
1677c478bd9Sstevel@tonic-gate 		SHA256_CONST_12, SHA256_CONST_13, SHA256_CONST_14,
1687c478bd9Sstevel@tonic-gate 		SHA256_CONST_15, SHA256_CONST_16, SHA256_CONST_17,
1697c478bd9Sstevel@tonic-gate 		SHA256_CONST_18, SHA256_CONST_19, SHA256_CONST_20,
1707c478bd9Sstevel@tonic-gate 		SHA256_CONST_21, SHA256_CONST_22, SHA256_CONST_23,
1717c478bd9Sstevel@tonic-gate 		SHA256_CONST_24, SHA256_CONST_25, SHA256_CONST_26,
1727c478bd9Sstevel@tonic-gate 		SHA256_CONST_27, SHA256_CONST_28, SHA256_CONST_29,
1737c478bd9Sstevel@tonic-gate 		SHA256_CONST_30, SHA256_CONST_31, SHA256_CONST_32,
1747c478bd9Sstevel@tonic-gate 		SHA256_CONST_33, SHA256_CONST_34, SHA256_CONST_35,
1757c478bd9Sstevel@tonic-gate 		SHA256_CONST_36, SHA256_CONST_37, SHA256_CONST_38,
1767c478bd9Sstevel@tonic-gate 		SHA256_CONST_39, SHA256_CONST_40, SHA256_CONST_41,
1777c478bd9Sstevel@tonic-gate 		SHA256_CONST_42, SHA256_CONST_43, SHA256_CONST_44,
1787c478bd9Sstevel@tonic-gate 		SHA256_CONST_45, SHA256_CONST_46, SHA256_CONST_47,
1797c478bd9Sstevel@tonic-gate 		SHA256_CONST_48, SHA256_CONST_49, SHA256_CONST_50,
1807c478bd9Sstevel@tonic-gate 		SHA256_CONST_51, SHA256_CONST_52, SHA256_CONST_53,
1817c478bd9Sstevel@tonic-gate 		SHA256_CONST_54, SHA256_CONST_55, SHA256_CONST_56,
1827c478bd9Sstevel@tonic-gate 		SHA256_CONST_57, SHA256_CONST_58, SHA256_CONST_59,
1837c478bd9Sstevel@tonic-gate 		SHA256_CONST_60, SHA256_CONST_61, SHA256_CONST_62,
1847c478bd9Sstevel@tonic-gate 		SHA256_CONST_63
1857c478bd9Sstevel@tonic-gate 	};
1867c478bd9Sstevel@tonic-gate #endif
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	if ((uintptr_t)blk & 0x3) {		/* not 4-byte aligned? */
1897c478bd9Sstevel@tonic-gate 		bcopy(blk, ctx->buf_un.buf32,  sizeof (ctx->buf_un.buf32));
1907c478bd9Sstevel@tonic-gate 		blk = (uint8_t *)ctx->buf_un.buf32;
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate 
193*734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
194f66d273dSizick 	w0 =  LOAD_BIG_32(blk + 4 * 0);
195f66d273dSizick 	SHA256ROUND(a, b, c, d, e, f, g, h, 0, w0);
196*734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
197f66d273dSizick 	w1 =  LOAD_BIG_32(blk + 4 * 1);
198f66d273dSizick 	SHA256ROUND(h, a, b, c, d, e, f, g, 1, w1);
199*734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
200f66d273dSizick 	w2 =  LOAD_BIG_32(blk + 4 * 2);
201f66d273dSizick 	SHA256ROUND(g, h, a, b, c, d, e, f, 2, w2);
202*734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
203f66d273dSizick 	w3 =  LOAD_BIG_32(blk + 4 * 3);
204f66d273dSizick 	SHA256ROUND(f, g, h, a, b, c, d, e, 3, w3);
205*734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
206f66d273dSizick 	w4 =  LOAD_BIG_32(blk + 4 * 4);
207f66d273dSizick 	SHA256ROUND(e, f, g, h, a, b, c, d, 4, w4);
208*734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
209f66d273dSizick 	w5 =  LOAD_BIG_32(blk + 4 * 5);
210f66d273dSizick 	SHA256ROUND(d, e, f, g, h, a, b, c, 5, w5);
211*734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
212f66d273dSizick 	w6 =  LOAD_BIG_32(blk + 4 * 6);
213f66d273dSizick 	SHA256ROUND(c, d, e, f, g, h, a, b, 6, w6);
214*734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
215f66d273dSizick 	w7 =  LOAD_BIG_32(blk + 4 * 7);
216f66d273dSizick 	SHA256ROUND(b, c, d, e, f, g, h, a, 7, w7);
217*734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
218f66d273dSizick 	w8 =  LOAD_BIG_32(blk + 4 * 8);
219f66d273dSizick 	SHA256ROUND(a, b, c, d, e, f, g, h, 8, w8);
220*734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
221f66d273dSizick 	w9 =  LOAD_BIG_32(blk + 4 * 9);
222f66d273dSizick 	SHA256ROUND(h, a, b, c, d, e, f, g, 9, w9);
223*734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
224f66d273dSizick 	w10 =  LOAD_BIG_32(blk + 4 * 10);
225f66d273dSizick 	SHA256ROUND(g, h, a, b, c, d, e, f, 10, w10);
226*734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
227f66d273dSizick 	w11 =  LOAD_BIG_32(blk + 4 * 11);
228f66d273dSizick 	SHA256ROUND(f, g, h, a, b, c, d, e, 11, w11);
229*734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
230f66d273dSizick 	w12 =  LOAD_BIG_32(blk + 4 * 12);
231f66d273dSizick 	SHA256ROUND(e, f, g, h, a, b, c, d, 12, w12);
232*734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
233f66d273dSizick 	w13 =  LOAD_BIG_32(blk + 4 * 13);
234f66d273dSizick 	SHA256ROUND(d, e, f, g, h, a, b, c, 13, w13);
235*734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
236f66d273dSizick 	w14 =  LOAD_BIG_32(blk + 4 * 14);
237f66d273dSizick 	SHA256ROUND(c, d, e, f, g, h, a, b, 14, w14);
238*734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
239f66d273dSizick 	w15 =  LOAD_BIG_32(blk + 4 * 15);
240f66d273dSizick 	SHA256ROUND(b, c, d, e, f, g, h, a, 15, w15);
241f66d273dSizick 
2427c478bd9Sstevel@tonic-gate 	w0 = SIGMA1_256(w14) + w9 + SIGMA0_256(w1) + w0;
2437c478bd9Sstevel@tonic-gate 	SHA256ROUND(a, b, c, d, e, f, g, h, 16, w0);
2447c478bd9Sstevel@tonic-gate 	w1 = SIGMA1_256(w15) + w10 + SIGMA0_256(w2) + w1;
2457c478bd9Sstevel@tonic-gate 	SHA256ROUND(h, a, b, c, d, e, f, g, 17, w1);
2467c478bd9Sstevel@tonic-gate 	w2 = SIGMA1_256(w0) + w11 + SIGMA0_256(w3) + w2;
2477c478bd9Sstevel@tonic-gate 	SHA256ROUND(g, h, a, b, c, d, e, f, 18, w2);
2487c478bd9Sstevel@tonic-gate 	w3 = SIGMA1_256(w1) + w12 + SIGMA0_256(w4) + w3;
2497c478bd9Sstevel@tonic-gate 	SHA256ROUND(f, g, h, a, b, c, d, e, 19, w3);
2507c478bd9Sstevel@tonic-gate 	w4 = SIGMA1_256(w2) + w13 + SIGMA0_256(w5) + w4;
2517c478bd9Sstevel@tonic-gate 	SHA256ROUND(e, f, g, h, a, b, c, d, 20, w4);
2527c478bd9Sstevel@tonic-gate 	w5 = SIGMA1_256(w3) + w14 + SIGMA0_256(w6) + w5;
2537c478bd9Sstevel@tonic-gate 	SHA256ROUND(d, e, f, g, h, a, b, c, 21, w5);
2547c478bd9Sstevel@tonic-gate 	w6 = SIGMA1_256(w4) + w15 + SIGMA0_256(w7) + w6;
2557c478bd9Sstevel@tonic-gate 	SHA256ROUND(c, d, e, f, g, h, a, b, 22, w6);
2567c478bd9Sstevel@tonic-gate 	w7 = SIGMA1_256(w5) + w0 + SIGMA0_256(w8) + w7;
2577c478bd9Sstevel@tonic-gate 	SHA256ROUND(b, c, d, e, f, g, h, a, 23, w7);
2587c478bd9Sstevel@tonic-gate 	w8 = SIGMA1_256(w6) + w1 + SIGMA0_256(w9) + w8;
2597c478bd9Sstevel@tonic-gate 	SHA256ROUND(a, b, c, d, e, f, g, h, 24, w8);
2607c478bd9Sstevel@tonic-gate 	w9 = SIGMA1_256(w7) + w2 + SIGMA0_256(w10) + w9;
2617c478bd9Sstevel@tonic-gate 	SHA256ROUND(h, a, b, c, d, e, f, g, 25, w9);
2627c478bd9Sstevel@tonic-gate 	w10 = SIGMA1_256(w8) + w3 + SIGMA0_256(w11) + w10;
2637c478bd9Sstevel@tonic-gate 	SHA256ROUND(g, h, a, b, c, d, e, f, 26, w10);
2647c478bd9Sstevel@tonic-gate 	w11 = SIGMA1_256(w9) + w4 + SIGMA0_256(w12) + w11;
2657c478bd9Sstevel@tonic-gate 	SHA256ROUND(f, g, h, a, b, c, d, e, 27, w11);
2667c478bd9Sstevel@tonic-gate 	w12 = SIGMA1_256(w10) + w5 + SIGMA0_256(w13) + w12;
2677c478bd9Sstevel@tonic-gate 	SHA256ROUND(e, f, g, h, a, b, c, d, 28, w12);
2687c478bd9Sstevel@tonic-gate 	w13 = SIGMA1_256(w11) + w6 + SIGMA0_256(w14) + w13;
2697c478bd9Sstevel@tonic-gate 	SHA256ROUND(d, e, f, g, h, a, b, c, 29, w13);
2707c478bd9Sstevel@tonic-gate 	w14 = SIGMA1_256(w12) + w7 + SIGMA0_256(w15) + w14;
2717c478bd9Sstevel@tonic-gate 	SHA256ROUND(c, d, e, f, g, h, a, b, 30, w14);
2727c478bd9Sstevel@tonic-gate 	w15 = SIGMA1_256(w13) + w8 + SIGMA0_256(w0) + w15;
2737c478bd9Sstevel@tonic-gate 	SHA256ROUND(b, c, d, e, f, g, h, a, 31, w15);
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	w0 = SIGMA1_256(w14) + w9 + SIGMA0_256(w1) + w0;
2767c478bd9Sstevel@tonic-gate 	SHA256ROUND(a, b, c, d, e, f, g, h, 32, w0);
2777c478bd9Sstevel@tonic-gate 	w1 = SIGMA1_256(w15) + w10 + SIGMA0_256(w2) + w1;
2787c478bd9Sstevel@tonic-gate 	SHA256ROUND(h, a, b, c, d, e, f, g, 33, w1);
2797c478bd9Sstevel@tonic-gate 	w2 = SIGMA1_256(w0) + w11 + SIGMA0_256(w3) + w2;
2807c478bd9Sstevel@tonic-gate 	SHA256ROUND(g, h, a, b, c, d, e, f, 34, w2);
2817c478bd9Sstevel@tonic-gate 	w3 = SIGMA1_256(w1) + w12 + SIGMA0_256(w4) + w3;
2827c478bd9Sstevel@tonic-gate 	SHA256ROUND(f, g, h, a, b, c, d, e, 35, w3);
2837c478bd9Sstevel@tonic-gate 	w4 = SIGMA1_256(w2) + w13 + SIGMA0_256(w5) + w4;
2847c478bd9Sstevel@tonic-gate 	SHA256ROUND(e, f, g, h, a, b, c, d, 36, w4);
2857c478bd9Sstevel@tonic-gate 	w5 = SIGMA1_256(w3) + w14 + SIGMA0_256(w6) + w5;
2867c478bd9Sstevel@tonic-gate 	SHA256ROUND(d, e, f, g, h, a, b, c, 37, w5);
2877c478bd9Sstevel@tonic-gate 	w6 = SIGMA1_256(w4) + w15 + SIGMA0_256(w7) + w6;
2887c478bd9Sstevel@tonic-gate 	SHA256ROUND(c, d, e, f, g, h, a, b, 38, w6);
2897c478bd9Sstevel@tonic-gate 	w7 = SIGMA1_256(w5) + w0 + SIGMA0_256(w8) + w7;
2907c478bd9Sstevel@tonic-gate 	SHA256ROUND(b, c, d, e, f, g, h, a, 39, w7);
2917c478bd9Sstevel@tonic-gate 	w8 = SIGMA1_256(w6) + w1 + SIGMA0_256(w9) + w8;
2927c478bd9Sstevel@tonic-gate 	SHA256ROUND(a, b, c, d, e, f, g, h, 40, w8);
2937c478bd9Sstevel@tonic-gate 	w9 = SIGMA1_256(w7) + w2 + SIGMA0_256(w10) + w9;
2947c478bd9Sstevel@tonic-gate 	SHA256ROUND(h, a, b, c, d, e, f, g, 41, w9);
2957c478bd9Sstevel@tonic-gate 	w10 = SIGMA1_256(w8) + w3 + SIGMA0_256(w11) + w10;
2967c478bd9Sstevel@tonic-gate 	SHA256ROUND(g, h, a, b, c, d, e, f, 42, w10);
2977c478bd9Sstevel@tonic-gate 	w11 = SIGMA1_256(w9) + w4 + SIGMA0_256(w12) + w11;
2987c478bd9Sstevel@tonic-gate 	SHA256ROUND(f, g, h, a, b, c, d, e, 43, w11);
2997c478bd9Sstevel@tonic-gate 	w12 = SIGMA1_256(w10) + w5 + SIGMA0_256(w13) + w12;
3007c478bd9Sstevel@tonic-gate 	SHA256ROUND(e, f, g, h, a, b, c, d, 44, w12);
3017c478bd9Sstevel@tonic-gate 	w13 = SIGMA1_256(w11) + w6 + SIGMA0_256(w14) + w13;
3027c478bd9Sstevel@tonic-gate 	SHA256ROUND(d, e, f, g, h, a, b, c, 45, w13);
3037c478bd9Sstevel@tonic-gate 	w14 = SIGMA1_256(w12) + w7 + SIGMA0_256(w15) + w14;
3047c478bd9Sstevel@tonic-gate 	SHA256ROUND(c, d, e, f, g, h, a, b, 46, w14);
3057c478bd9Sstevel@tonic-gate 	w15 = SIGMA1_256(w13) + w8 + SIGMA0_256(w0) + w15;
3067c478bd9Sstevel@tonic-gate 	SHA256ROUND(b, c, d, e, f, g, h, a, 47, w15);
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	w0 = SIGMA1_256(w14) + w9 + SIGMA0_256(w1) + w0;
3097c478bd9Sstevel@tonic-gate 	SHA256ROUND(a, b, c, d, e, f, g, h, 48, w0);
3107c478bd9Sstevel@tonic-gate 	w1 = SIGMA1_256(w15) + w10 + SIGMA0_256(w2) + w1;
3117c478bd9Sstevel@tonic-gate 	SHA256ROUND(h, a, b, c, d, e, f, g, 49, w1);
3127c478bd9Sstevel@tonic-gate 	w2 = SIGMA1_256(w0) + w11 + SIGMA0_256(w3) + w2;
3137c478bd9Sstevel@tonic-gate 	SHA256ROUND(g, h, a, b, c, d, e, f, 50, w2);
3147c478bd9Sstevel@tonic-gate 	w3 = SIGMA1_256(w1) + w12 + SIGMA0_256(w4) + w3;
3157c478bd9Sstevel@tonic-gate 	SHA256ROUND(f, g, h, a, b, c, d, e, 51, w3);
3167c478bd9Sstevel@tonic-gate 	w4 = SIGMA1_256(w2) + w13 + SIGMA0_256(w5) + w4;
3177c478bd9Sstevel@tonic-gate 	SHA256ROUND(e, f, g, h, a, b, c, d, 52, w4);
3187c478bd9Sstevel@tonic-gate 	w5 = SIGMA1_256(w3) + w14 + SIGMA0_256(w6) + w5;
3197c478bd9Sstevel@tonic-gate 	SHA256ROUND(d, e, f, g, h, a, b, c, 53, w5);
3207c478bd9Sstevel@tonic-gate 	w6 = SIGMA1_256(w4) + w15 + SIGMA0_256(w7) + w6;
3217c478bd9Sstevel@tonic-gate 	SHA256ROUND(c, d, e, f, g, h, a, b, 54, w6);
3227c478bd9Sstevel@tonic-gate 	w7 = SIGMA1_256(w5) + w0 + SIGMA0_256(w8) + w7;
3237c478bd9Sstevel@tonic-gate 	SHA256ROUND(b, c, d, e, f, g, h, a, 55, w7);
3247c478bd9Sstevel@tonic-gate 	w8 = SIGMA1_256(w6) + w1 + SIGMA0_256(w9) + w8;
3257c478bd9Sstevel@tonic-gate 	SHA256ROUND(a, b, c, d, e, f, g, h, 56, w8);
3267c478bd9Sstevel@tonic-gate 	w9 = SIGMA1_256(w7) + w2 + SIGMA0_256(w10) + w9;
3277c478bd9Sstevel@tonic-gate 	SHA256ROUND(h, a, b, c, d, e, f, g, 57, w9);
3287c478bd9Sstevel@tonic-gate 	w10 = SIGMA1_256(w8) + w3 + SIGMA0_256(w11) + w10;
3297c478bd9Sstevel@tonic-gate 	SHA256ROUND(g, h, a, b, c, d, e, f, 58, w10);
3307c478bd9Sstevel@tonic-gate 	w11 = SIGMA1_256(w9) + w4 + SIGMA0_256(w12) + w11;
3317c478bd9Sstevel@tonic-gate 	SHA256ROUND(f, g, h, a, b, c, d, e, 59, w11);
3327c478bd9Sstevel@tonic-gate 	w12 = SIGMA1_256(w10) + w5 + SIGMA0_256(w13) + w12;
3337c478bd9Sstevel@tonic-gate 	SHA256ROUND(e, f, g, h, a, b, c, d, 60, w12);
3347c478bd9Sstevel@tonic-gate 	w13 = SIGMA1_256(w11) + w6 + SIGMA0_256(w14) + w13;
3357c478bd9Sstevel@tonic-gate 	SHA256ROUND(d, e, f, g, h, a, b, c, 61, w13);
3367c478bd9Sstevel@tonic-gate 	w14 = SIGMA1_256(w12) + w7 + SIGMA0_256(w15) + w14;
3377c478bd9Sstevel@tonic-gate 	SHA256ROUND(c, d, e, f, g, h, a, b, 62, w14);
3387c478bd9Sstevel@tonic-gate 	w15 = SIGMA1_256(w13) + w8 + SIGMA0_256(w0) + w15;
3397c478bd9Sstevel@tonic-gate 	SHA256ROUND(b, c, d, e, f, g, h, a, 63, w15);
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	ctx->state.s32[0] += a;
3427c478bd9Sstevel@tonic-gate 	ctx->state.s32[1] += b;
3437c478bd9Sstevel@tonic-gate 	ctx->state.s32[2] += c;
3447c478bd9Sstevel@tonic-gate 	ctx->state.s32[3] += d;
3457c478bd9Sstevel@tonic-gate 	ctx->state.s32[4] += e;
3467c478bd9Sstevel@tonic-gate 	ctx->state.s32[5] += f;
3477c478bd9Sstevel@tonic-gate 	ctx->state.s32[6] += g;
3487c478bd9Sstevel@tonic-gate 	ctx->state.s32[7] += h;
3497c478bd9Sstevel@tonic-gate }
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate /* SHA384 and SHA512 Transform */
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate static void
3557c478bd9Sstevel@tonic-gate SHA512Transform(SHA2_CTX *ctx, const uint8_t *blk)
3567c478bd9Sstevel@tonic-gate {
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	uint64_t a = ctx->state.s64[0];
3597c478bd9Sstevel@tonic-gate 	uint64_t b = ctx->state.s64[1];
3607c478bd9Sstevel@tonic-gate 	uint64_t c = ctx->state.s64[2];
3617c478bd9Sstevel@tonic-gate 	uint64_t d = ctx->state.s64[3];
3627c478bd9Sstevel@tonic-gate 	uint64_t e = ctx->state.s64[4];
3637c478bd9Sstevel@tonic-gate 	uint64_t f = ctx->state.s64[5];
3647c478bd9Sstevel@tonic-gate 	uint64_t g = ctx->state.s64[6];
3657c478bd9Sstevel@tonic-gate 	uint64_t h = ctx->state.s64[7];
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	uint64_t w0, w1, w2, w3, w4, w5, w6, w7;
3687c478bd9Sstevel@tonic-gate 	uint64_t w8, w9, w10, w11, w12, w13, w14, w15;
3697c478bd9Sstevel@tonic-gate 	uint64_t T1, T2;
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate #if	defined(__sparc)
3727c478bd9Sstevel@tonic-gate 	static const uint64_t sha512_consts[] = {
3737c478bd9Sstevel@tonic-gate 		SHA512_CONST_0, SHA512_CONST_1, SHA512_CONST_2,
3747c478bd9Sstevel@tonic-gate 		SHA512_CONST_3, SHA512_CONST_4, SHA512_CONST_5,
3757c478bd9Sstevel@tonic-gate 		SHA512_CONST_6, SHA512_CONST_7, SHA512_CONST_8,
3767c478bd9Sstevel@tonic-gate 		SHA512_CONST_9, SHA512_CONST_10, SHA512_CONST_11,
3777c478bd9Sstevel@tonic-gate 		SHA512_CONST_12, SHA512_CONST_13, SHA512_CONST_14,
3787c478bd9Sstevel@tonic-gate 		SHA512_CONST_15, SHA512_CONST_16, SHA512_CONST_17,
3797c478bd9Sstevel@tonic-gate 		SHA512_CONST_18, SHA512_CONST_19, SHA512_CONST_20,
3807c478bd9Sstevel@tonic-gate 		SHA512_CONST_21, SHA512_CONST_22, SHA512_CONST_23,
3817c478bd9Sstevel@tonic-gate 		SHA512_CONST_24, SHA512_CONST_25, SHA512_CONST_26,
3827c478bd9Sstevel@tonic-gate 		SHA512_CONST_27, SHA512_CONST_28, SHA512_CONST_29,
3837c478bd9Sstevel@tonic-gate 		SHA512_CONST_30, SHA512_CONST_31, SHA512_CONST_32,
3847c478bd9Sstevel@tonic-gate 		SHA512_CONST_33, SHA512_CONST_34, SHA512_CONST_35,
3857c478bd9Sstevel@tonic-gate 		SHA512_CONST_36, SHA512_CONST_37, SHA512_CONST_38,
3867c478bd9Sstevel@tonic-gate 		SHA512_CONST_39, SHA512_CONST_40, SHA512_CONST_41,
3877c478bd9Sstevel@tonic-gate 		SHA512_CONST_42, SHA512_CONST_43, SHA512_CONST_44,
3887c478bd9Sstevel@tonic-gate 		SHA512_CONST_45, SHA512_CONST_46, SHA512_CONST_47,
3897c478bd9Sstevel@tonic-gate 		SHA512_CONST_48, SHA512_CONST_49, SHA512_CONST_50,
3907c478bd9Sstevel@tonic-gate 		SHA512_CONST_51, SHA512_CONST_52, SHA512_CONST_53,
3917c478bd9Sstevel@tonic-gate 		SHA512_CONST_54, SHA512_CONST_55, SHA512_CONST_56,
3927c478bd9Sstevel@tonic-gate 		SHA512_CONST_57, SHA512_CONST_58, SHA512_CONST_59,
3937c478bd9Sstevel@tonic-gate 		SHA512_CONST_60, SHA512_CONST_61, SHA512_CONST_62,
3947c478bd9Sstevel@tonic-gate 		SHA512_CONST_63, SHA512_CONST_64, SHA512_CONST_65,
3957c478bd9Sstevel@tonic-gate 		SHA512_CONST_66, SHA512_CONST_67, SHA512_CONST_68,
3967c478bd9Sstevel@tonic-gate 		SHA512_CONST_69, SHA512_CONST_70, SHA512_CONST_71,
3977c478bd9Sstevel@tonic-gate 		SHA512_CONST_72, SHA512_CONST_73, SHA512_CONST_74,
3987c478bd9Sstevel@tonic-gate 		SHA512_CONST_75, SHA512_CONST_76, SHA512_CONST_77,
3997c478bd9Sstevel@tonic-gate 		SHA512_CONST_78, SHA512_CONST_79
4007c478bd9Sstevel@tonic-gate 	};
4017c478bd9Sstevel@tonic-gate #endif
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	if ((uintptr_t)blk & 0x7) {		/* not 8-byte aligned? */
4057c478bd9Sstevel@tonic-gate 		bcopy(blk, ctx->buf_un.buf64,  sizeof (ctx->buf_un.buf64));
4067c478bd9Sstevel@tonic-gate 		blk = (uint8_t *)ctx->buf_un.buf64;
4077c478bd9Sstevel@tonic-gate 	}
4087c478bd9Sstevel@tonic-gate 
409*734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
410f66d273dSizick 	w0 =  LOAD_BIG_64(blk + 8 * 0);
411f66d273dSizick 	SHA512ROUND(a, b, c, d, e, f, g, h, 0, w0);
412*734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
413f66d273dSizick 	w1 =  LOAD_BIG_64(blk + 8 * 1);
414f66d273dSizick 	SHA512ROUND(h, a, b, c, d, e, f, g, 1, w1);
415*734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
416f66d273dSizick 	w2 =  LOAD_BIG_64(blk + 8 * 2);
417f66d273dSizick 	SHA512ROUND(g, h, a, b, c, d, e, f, 2, w2);
418*734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
419f66d273dSizick 	w3 =  LOAD_BIG_64(blk + 8 * 3);
420f66d273dSizick 	SHA512ROUND(f, g, h, a, b, c, d, e, 3, w3);
421*734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
422f66d273dSizick 	w4 =  LOAD_BIG_64(blk + 8 * 4);
423f66d273dSizick 	SHA512ROUND(e, f, g, h, a, b, c, d, 4, w4);
424*734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
425f66d273dSizick 	w5 =  LOAD_BIG_64(blk + 8 * 5);
426f66d273dSizick 	SHA512ROUND(d, e, f, g, h, a, b, c, 5, w5);
427*734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
428f66d273dSizick 	w6 =  LOAD_BIG_64(blk + 8 * 6);
429f66d273dSizick 	SHA512ROUND(c, d, e, f, g, h, a, b, 6, w6);
430*734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
431f66d273dSizick 	w7 =  LOAD_BIG_64(blk + 8 * 7);
432f66d273dSizick 	SHA512ROUND(b, c, d, e, f, g, h, a, 7, w7);
433*734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
434f66d273dSizick 	w8 =  LOAD_BIG_64(blk + 8 * 8);
435f66d273dSizick 	SHA512ROUND(a, b, c, d, e, f, g, h, 8, w8);
436*734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
437f66d273dSizick 	w9 =  LOAD_BIG_64(blk + 8 * 9);
438f66d273dSizick 	SHA512ROUND(h, a, b, c, d, e, f, g, 9, w9);
439*734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
440f66d273dSizick 	w10 =  LOAD_BIG_64(blk + 8 * 10);
441f66d273dSizick 	SHA512ROUND(g, h, a, b, c, d, e, f, 10, w10);
442*734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
443f66d273dSizick 	w11 =  LOAD_BIG_64(blk + 8 * 11);
444f66d273dSizick 	SHA512ROUND(f, g, h, a, b, c, d, e, 11, w11);
445*734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
446f66d273dSizick 	w12 =  LOAD_BIG_64(blk + 8 * 12);
447f66d273dSizick 	SHA512ROUND(e, f, g, h, a, b, c, d, 12, w12);
448*734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
449f66d273dSizick 	w13 =  LOAD_BIG_64(blk + 8 * 13);
450f66d273dSizick 	SHA512ROUND(d, e, f, g, h, a, b, c, 13, w13);
451*734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
452f66d273dSizick 	w14 =  LOAD_BIG_64(blk + 8 * 14);
453f66d273dSizick 	SHA512ROUND(c, d, e, f, g, h, a, b, 14, w14);
454*734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
455f66d273dSizick 	w15 =  LOAD_BIG_64(blk + 8 * 15);
456f66d273dSizick 	SHA512ROUND(b, c, d, e, f, g, h, a, 15, w15);
457f66d273dSizick 
4587c478bd9Sstevel@tonic-gate 	w0 = SIGMA1(w14) + w9 + SIGMA0(w1) + w0;
4597c478bd9Sstevel@tonic-gate 	SHA512ROUND(a, b, c, d, e, f, g, h, 16, w0);
4607c478bd9Sstevel@tonic-gate 	w1 = SIGMA1(w15) + w10 + SIGMA0(w2) + w1;
4617c478bd9Sstevel@tonic-gate 	SHA512ROUND(h, a, b, c, d, e, f, g, 17, w1);
4627c478bd9Sstevel@tonic-gate 	w2 = SIGMA1(w0) + w11 + SIGMA0(w3) + w2;
4637c478bd9Sstevel@tonic-gate 	SHA512ROUND(g, h, a, b, c, d, e, f, 18, w2);
4647c478bd9Sstevel@tonic-gate 	w3 = SIGMA1(w1) + w12 + SIGMA0(w4) + w3;
4657c478bd9Sstevel@tonic-gate 	SHA512ROUND(f, g, h, a, b, c, d, e, 19, w3);
4667c478bd9Sstevel@tonic-gate 	w4 = SIGMA1(w2) + w13 + SIGMA0(w5) + w4;
4677c478bd9Sstevel@tonic-gate 	SHA512ROUND(e, f, g, h, a, b, c, d, 20, w4);
4687c478bd9Sstevel@tonic-gate 	w5 = SIGMA1(w3) + w14 + SIGMA0(w6) + w5;
4697c478bd9Sstevel@tonic-gate 	SHA512ROUND(d, e, f, g, h, a, b, c, 21, w5);
4707c478bd9Sstevel@tonic-gate 	w6 = SIGMA1(w4) + w15 + SIGMA0(w7) + w6;
4717c478bd9Sstevel@tonic-gate 	SHA512ROUND(c, d, e, f, g, h, a, b, 22, w6);
4727c478bd9Sstevel@tonic-gate 	w7 = SIGMA1(w5) + w0 + SIGMA0(w8) + w7;
4737c478bd9Sstevel@tonic-gate 	SHA512ROUND(b, c, d, e, f, g, h, a, 23, w7);
4747c478bd9Sstevel@tonic-gate 	w8 = SIGMA1(w6) + w1 + SIGMA0(w9) + w8;
4757c478bd9Sstevel@tonic-gate 	SHA512ROUND(a, b, c, d, e, f, g, h, 24, w8);
4767c478bd9Sstevel@tonic-gate 	w9 = SIGMA1(w7) + w2 + SIGMA0(w10) + w9;
4777c478bd9Sstevel@tonic-gate 	SHA512ROUND(h, a, b, c, d, e, f, g, 25, w9);
4787c478bd9Sstevel@tonic-gate 	w10 = SIGMA1(w8) + w3 + SIGMA0(w11) + w10;
4797c478bd9Sstevel@tonic-gate 	SHA512ROUND(g, h, a, b, c, d, e, f, 26, w10);
4807c478bd9Sstevel@tonic-gate 	w11 = SIGMA1(w9) + w4 + SIGMA0(w12) + w11;
4817c478bd9Sstevel@tonic-gate 	SHA512ROUND(f, g, h, a, b, c, d, e, 27, w11);
4827c478bd9Sstevel@tonic-gate 	w12 = SIGMA1(w10) + w5 + SIGMA0(w13) + w12;
4837c478bd9Sstevel@tonic-gate 	SHA512ROUND(e, f, g, h, a, b, c, d, 28, w12);
4847c478bd9Sstevel@tonic-gate 	w13 = SIGMA1(w11) + w6 + SIGMA0(w14) + w13;
4857c478bd9Sstevel@tonic-gate 	SHA512ROUND(d, e, f, g, h, a, b, c, 29, w13);
4867c478bd9Sstevel@tonic-gate 	w14 = SIGMA1(w12) + w7 + SIGMA0(w15) + w14;
4877c478bd9Sstevel@tonic-gate 	SHA512ROUND(c, d, e, f, g, h, a, b, 30, w14);
4887c478bd9Sstevel@tonic-gate 	w15 = SIGMA1(w13) + w8 + SIGMA0(w0) + w15;
4897c478bd9Sstevel@tonic-gate 	SHA512ROUND(b, c, d, e, f, g, h, a, 31, w15);
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	w0 = SIGMA1(w14) + w9 + SIGMA0(w1) + w0;
4927c478bd9Sstevel@tonic-gate 	SHA512ROUND(a, b, c, d, e, f, g, h, 32, w0);
4937c478bd9Sstevel@tonic-gate 	w1 = SIGMA1(w15) + w10 + SIGMA0(w2) + w1;
4947c478bd9Sstevel@tonic-gate 	SHA512ROUND(h, a, b, c, d, e, f, g, 33, w1);
4957c478bd9Sstevel@tonic-gate 	w2 = SIGMA1(w0) + w11 + SIGMA0(w3) + w2;
4967c478bd9Sstevel@tonic-gate 	SHA512ROUND(g, h, a, b, c, d, e, f, 34, w2);
4977c478bd9Sstevel@tonic-gate 	w3 = SIGMA1(w1) + w12 + SIGMA0(w4) + w3;
4987c478bd9Sstevel@tonic-gate 	SHA512ROUND(f, g, h, a, b, c, d, e, 35, w3);
4997c478bd9Sstevel@tonic-gate 	w4 = SIGMA1(w2) + w13 + SIGMA0(w5) + w4;
5007c478bd9Sstevel@tonic-gate 	SHA512ROUND(e, f, g, h, a, b, c, d, 36, w4);
5017c478bd9Sstevel@tonic-gate 	w5 = SIGMA1(w3) + w14 + SIGMA0(w6) + w5;
5027c478bd9Sstevel@tonic-gate 	SHA512ROUND(d, e, f, g, h, a, b, c, 37, w5);
5037c478bd9Sstevel@tonic-gate 	w6 = SIGMA1(w4) + w15 + SIGMA0(w7) + w6;
5047c478bd9Sstevel@tonic-gate 	SHA512ROUND(c, d, e, f, g, h, a, b, 38, w6);
5057c478bd9Sstevel@tonic-gate 	w7 = SIGMA1(w5) + w0 + SIGMA0(w8) + w7;
5067c478bd9Sstevel@tonic-gate 	SHA512ROUND(b, c, d, e, f, g, h, a, 39, w7);
5077c478bd9Sstevel@tonic-gate 	w8 = SIGMA1(w6) + w1 + SIGMA0(w9) + w8;
5087c478bd9Sstevel@tonic-gate 	SHA512ROUND(a, b, c, d, e, f, g, h, 40, w8);
5097c478bd9Sstevel@tonic-gate 	w9 = SIGMA1(w7) + w2 + SIGMA0(w10) + w9;
5107c478bd9Sstevel@tonic-gate 	SHA512ROUND(h, a, b, c, d, e, f, g, 41, w9);
5117c478bd9Sstevel@tonic-gate 	w10 = SIGMA1(w8) + w3 + SIGMA0(w11) + w10;
5127c478bd9Sstevel@tonic-gate 	SHA512ROUND(g, h, a, b, c, d, e, f, 42, w10);
5137c478bd9Sstevel@tonic-gate 	w11 = SIGMA1(w9) + w4 + SIGMA0(w12) + w11;
5147c478bd9Sstevel@tonic-gate 	SHA512ROUND(f, g, h, a, b, c, d, e, 43, w11);
5157c478bd9Sstevel@tonic-gate 	w12 = SIGMA1(w10) + w5 + SIGMA0(w13) + w12;
5167c478bd9Sstevel@tonic-gate 	SHA512ROUND(e, f, g, h, a, b, c, d, 44, w12);
5177c478bd9Sstevel@tonic-gate 	w13 = SIGMA1(w11) + w6 + SIGMA0(w14) + w13;
5187c478bd9Sstevel@tonic-gate 	SHA512ROUND(d, e, f, g, h, a, b, c, 45, w13);
5197c478bd9Sstevel@tonic-gate 	w14 = SIGMA1(w12) + w7 + SIGMA0(w15) + w14;
5207c478bd9Sstevel@tonic-gate 	SHA512ROUND(c, d, e, f, g, h, a, b, 46, w14);
5217c478bd9Sstevel@tonic-gate 	w15 = SIGMA1(w13) + w8 + SIGMA0(w0) + w15;
5227c478bd9Sstevel@tonic-gate 	SHA512ROUND(b, c, d, e, f, g, h, a, 47, w15);
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	w0 = SIGMA1(w14) + w9 + SIGMA0(w1) + w0;
5257c478bd9Sstevel@tonic-gate 	SHA512ROUND(a, b, c, d, e, f, g, h, 48, w0);
5267c478bd9Sstevel@tonic-gate 	w1 = SIGMA1(w15) + w10 + SIGMA0(w2) + w1;
5277c478bd9Sstevel@tonic-gate 	SHA512ROUND(h, a, b, c, d, e, f, g, 49, w1);
5287c478bd9Sstevel@tonic-gate 	w2 = SIGMA1(w0) + w11 + SIGMA0(w3) + w2;
5297c478bd9Sstevel@tonic-gate 	SHA512ROUND(g, h, a, b, c, d, e, f, 50, w2);
5307c478bd9Sstevel@tonic-gate 	w3 = SIGMA1(w1) + w12 + SIGMA0(w4) + w3;
5317c478bd9Sstevel@tonic-gate 	SHA512ROUND(f, g, h, a, b, c, d, e, 51, w3);
5327c478bd9Sstevel@tonic-gate 	w4 = SIGMA1(w2) + w13 + SIGMA0(w5) + w4;
5337c478bd9Sstevel@tonic-gate 	SHA512ROUND(e, f, g, h, a, b, c, d, 52, w4);
5347c478bd9Sstevel@tonic-gate 	w5 = SIGMA1(w3) + w14 + SIGMA0(w6) + w5;
5357c478bd9Sstevel@tonic-gate 	SHA512ROUND(d, e, f, g, h, a, b, c, 53, w5);
5367c478bd9Sstevel@tonic-gate 	w6 = SIGMA1(w4) + w15 + SIGMA0(w7) + w6;
5377c478bd9Sstevel@tonic-gate 	SHA512ROUND(c, d, e, f, g, h, a, b, 54, w6);
5387c478bd9Sstevel@tonic-gate 	w7 = SIGMA1(w5) + w0 + SIGMA0(w8) + w7;
5397c478bd9Sstevel@tonic-gate 	SHA512ROUND(b, c, d, e, f, g, h, a, 55, w7);
5407c478bd9Sstevel@tonic-gate 	w8 = SIGMA1(w6) + w1 + SIGMA0(w9) + w8;
5417c478bd9Sstevel@tonic-gate 	SHA512ROUND(a, b, c, d, e, f, g, h, 56, w8);
5427c478bd9Sstevel@tonic-gate 	w9 = SIGMA1(w7) + w2 + SIGMA0(w10) + w9;
5437c478bd9Sstevel@tonic-gate 	SHA512ROUND(h, a, b, c, d, e, f, g, 57, w9);
5447c478bd9Sstevel@tonic-gate 	w10 = SIGMA1(w8) + w3 + SIGMA0(w11) + w10;
5457c478bd9Sstevel@tonic-gate 	SHA512ROUND(g, h, a, b, c, d, e, f, 58, w10);
5467c478bd9Sstevel@tonic-gate 	w11 = SIGMA1(w9) + w4 + SIGMA0(w12) + w11;
5477c478bd9Sstevel@tonic-gate 	SHA512ROUND(f, g, h, a, b, c, d, e, 59, w11);
5487c478bd9Sstevel@tonic-gate 	w12 = SIGMA1(w10) + w5 + SIGMA0(w13) + w12;
5497c478bd9Sstevel@tonic-gate 	SHA512ROUND(e, f, g, h, a, b, c, d, 60, w12);
5507c478bd9Sstevel@tonic-gate 	w13 = SIGMA1(w11) + w6 + SIGMA0(w14) + w13;
5517c478bd9Sstevel@tonic-gate 	SHA512ROUND(d, e, f, g, h, a, b, c, 61, w13);
5527c478bd9Sstevel@tonic-gate 	w14 = SIGMA1(w12) + w7 + SIGMA0(w15) + w14;
5537c478bd9Sstevel@tonic-gate 	SHA512ROUND(c, d, e, f, g, h, a, b, 62, w14);
5547c478bd9Sstevel@tonic-gate 	w15 = SIGMA1(w13) + w8 + SIGMA0(w0) + w15;
5557c478bd9Sstevel@tonic-gate 	SHA512ROUND(b, c, d, e, f, g, h, a, 63, w15);
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	w0 = SIGMA1(w14) + w9 + SIGMA0(w1) + w0;
5587c478bd9Sstevel@tonic-gate 	SHA512ROUND(a, b, c, d, e, f, g, h, 64, w0);
5597c478bd9Sstevel@tonic-gate 	w1 = SIGMA1(w15) + w10 + SIGMA0(w2) + w1;
5607c478bd9Sstevel@tonic-gate 	SHA512ROUND(h, a, b, c, d, e, f, g, 65, w1);
5617c478bd9Sstevel@tonic-gate 	w2 = SIGMA1(w0) + w11 + SIGMA0(w3) + w2;
5627c478bd9Sstevel@tonic-gate 	SHA512ROUND(g, h, a, b, c, d, e, f, 66, w2);
5637c478bd9Sstevel@tonic-gate 	w3 = SIGMA1(w1) + w12 + SIGMA0(w4) + w3;
5647c478bd9Sstevel@tonic-gate 	SHA512ROUND(f, g, h, a, b, c, d, e, 67, w3);
5657c478bd9Sstevel@tonic-gate 	w4 = SIGMA1(w2) + w13 + SIGMA0(w5) + w4;
5667c478bd9Sstevel@tonic-gate 	SHA512ROUND(e, f, g, h, a, b, c, d, 68, w4);
5677c478bd9Sstevel@tonic-gate 	w5 = SIGMA1(w3) + w14 + SIGMA0(w6) + w5;
5687c478bd9Sstevel@tonic-gate 	SHA512ROUND(d, e, f, g, h, a, b, c, 69, w5);
5697c478bd9Sstevel@tonic-gate 	w6 = SIGMA1(w4) + w15 + SIGMA0(w7) + w6;
5707c478bd9Sstevel@tonic-gate 	SHA512ROUND(c, d, e, f, g, h, a, b, 70, w6);
5717c478bd9Sstevel@tonic-gate 	w7 = SIGMA1(w5) + w0 + SIGMA0(w8) + w7;
5727c478bd9Sstevel@tonic-gate 	SHA512ROUND(b, c, d, e, f, g, h, a, 71, w7);
5737c478bd9Sstevel@tonic-gate 	w8 = SIGMA1(w6) + w1 + SIGMA0(w9) + w8;
5747c478bd9Sstevel@tonic-gate 	SHA512ROUND(a, b, c, d, e, f, g, h, 72, w8);
5757c478bd9Sstevel@tonic-gate 	w9 = SIGMA1(w7) + w2 + SIGMA0(w10) + w9;
5767c478bd9Sstevel@tonic-gate 	SHA512ROUND(h, a, b, c, d, e, f, g, 73, w9);
5777c478bd9Sstevel@tonic-gate 	w10 = SIGMA1(w8) + w3 + SIGMA0(w11) + w10;
5787c478bd9Sstevel@tonic-gate 	SHA512ROUND(g, h, a, b, c, d, e, f, 74, w10);
5797c478bd9Sstevel@tonic-gate 	w11 = SIGMA1(w9) + w4 + SIGMA0(w12) + w11;
5807c478bd9Sstevel@tonic-gate 	SHA512ROUND(f, g, h, a, b, c, d, e, 75, w11);
5817c478bd9Sstevel@tonic-gate 	w12 = SIGMA1(w10) + w5 + SIGMA0(w13) + w12;
5827c478bd9Sstevel@tonic-gate 	SHA512ROUND(e, f, g, h, a, b, c, d, 76, w12);
5837c478bd9Sstevel@tonic-gate 	w13 = SIGMA1(w11) + w6 + SIGMA0(w14) + w13;
5847c478bd9Sstevel@tonic-gate 	SHA512ROUND(d, e, f, g, h, a, b, c, 77, w13);
5857c478bd9Sstevel@tonic-gate 	w14 = SIGMA1(w12) + w7 + SIGMA0(w15) + w14;
5867c478bd9Sstevel@tonic-gate 	SHA512ROUND(c, d, e, f, g, h, a, b, 78, w14);
5877c478bd9Sstevel@tonic-gate 	w15 = SIGMA1(w13) + w8 + SIGMA0(w0) + w15;
5887c478bd9Sstevel@tonic-gate 	SHA512ROUND(b, c, d, e, f, g, h, a, 79, w15);
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	ctx->state.s64[0] += a;
5917c478bd9Sstevel@tonic-gate 	ctx->state.s64[1] += b;
5927c478bd9Sstevel@tonic-gate 	ctx->state.s64[2] += c;
5937c478bd9Sstevel@tonic-gate 	ctx->state.s64[3] += d;
5947c478bd9Sstevel@tonic-gate 	ctx->state.s64[4] += e;
5957c478bd9Sstevel@tonic-gate 	ctx->state.s64[5] += f;
5967c478bd9Sstevel@tonic-gate 	ctx->state.s64[6] += g;
5977c478bd9Sstevel@tonic-gate 	ctx->state.s64[7] += h;
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate }
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate /*
6037c478bd9Sstevel@tonic-gate  * Encode()
6047c478bd9Sstevel@tonic-gate  *
6057c478bd9Sstevel@tonic-gate  * purpose: to convert a list of numbers from little endian to big endian
6067c478bd9Sstevel@tonic-gate  *   input: uint8_t *	: place to store the converted big endian numbers
6077c478bd9Sstevel@tonic-gate  *	    uint32_t *	: place to get numbers to convert from
6087c478bd9Sstevel@tonic-gate  *          size_t	: the length of the input in bytes
6097c478bd9Sstevel@tonic-gate  *  output: void
6107c478bd9Sstevel@tonic-gate  */
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate static void
613*734b6a94Sdarrenm Encode(uint8_t *_RESTRICT_KYWD output, uint32_t *_RESTRICT_KYWD input,
614*734b6a94Sdarrenm     size_t len)
6157c478bd9Sstevel@tonic-gate {
6167c478bd9Sstevel@tonic-gate 	size_t		i, j;
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate #if	defined(__sparc)
6197c478bd9Sstevel@tonic-gate 	if (IS_P2ALIGNED(output, sizeof (uint32_t))) {
6207c478bd9Sstevel@tonic-gate 		for (i = 0, j = 0; j < len; i++, j += 4) {
6217c478bd9Sstevel@tonic-gate 			/* LINTED: pointer alignment */
6227c478bd9Sstevel@tonic-gate 			*((uint32_t *)(output + j)) = input[i];
6237c478bd9Sstevel@tonic-gate 		}
6247c478bd9Sstevel@tonic-gate 	} else {
6257c478bd9Sstevel@tonic-gate #endif	/* little endian -- will work on big endian, but slowly */
6267c478bd9Sstevel@tonic-gate 		for (i = 0, j = 0; j < len; i++, j += 4) {
6277c478bd9Sstevel@tonic-gate 			output[j]	= (input[i] >> 24) & 0xff;
6287c478bd9Sstevel@tonic-gate 			output[j + 1]	= (input[i] >> 16) & 0xff;
6297c478bd9Sstevel@tonic-gate 			output[j + 2]	= (input[i] >>  8) & 0xff;
6307c478bd9Sstevel@tonic-gate 			output[j + 3]	= input[i] & 0xff;
6317c478bd9Sstevel@tonic-gate 		}
6327c478bd9Sstevel@tonic-gate #if	defined(__sparc)
6337c478bd9Sstevel@tonic-gate 	}
6347c478bd9Sstevel@tonic-gate #endif
6357c478bd9Sstevel@tonic-gate }
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate static void
638*734b6a94Sdarrenm Encode64(uint8_t *_RESTRICT_KYWD output, uint64_t *_RESTRICT_KYWD input,
639*734b6a94Sdarrenm     size_t len)
6407c478bd9Sstevel@tonic-gate {
6417c478bd9Sstevel@tonic-gate 	size_t		i, j;
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate #if	defined(__sparc)
6447c478bd9Sstevel@tonic-gate 	if (IS_P2ALIGNED(output, sizeof (uint64_t))) {
6457c478bd9Sstevel@tonic-gate 		for (i = 0, j = 0; j < len; i++, j += 8) {
6467c478bd9Sstevel@tonic-gate 			/* LINTED: pointer alignment */
6477c478bd9Sstevel@tonic-gate 			*((uint64_t *)(output + j)) = input[i];
6487c478bd9Sstevel@tonic-gate 		}
6497c478bd9Sstevel@tonic-gate 	} else {
6507c478bd9Sstevel@tonic-gate #endif	/* little endian -- will work on big endian, but slowly */
6517c478bd9Sstevel@tonic-gate 		for (i = 0, j = 0; j < len; i++, j += 8) {
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 			output[j]	= (input[i] >> 56) & 0xff;
6547c478bd9Sstevel@tonic-gate 			output[j + 1]	= (input[i] >> 48) & 0xff;
6557c478bd9Sstevel@tonic-gate 			output[j + 2]	= (input[i] >> 40) & 0xff;
6567c478bd9Sstevel@tonic-gate 			output[j + 3]	= (input[i] >> 32) & 0xff;
6577c478bd9Sstevel@tonic-gate 			output[j + 4]	= (input[i] >> 24) & 0xff;
6587c478bd9Sstevel@tonic-gate 			output[j + 5]	= (input[i] >> 16) & 0xff;
6597c478bd9Sstevel@tonic-gate 			output[j + 6]	= (input[i] >>  8) & 0xff;
6607c478bd9Sstevel@tonic-gate 			output[j + 7]	= input[i] & 0xff;
6617c478bd9Sstevel@tonic-gate 		}
6627c478bd9Sstevel@tonic-gate #if	defined(__sparc)
6637c478bd9Sstevel@tonic-gate 	}
6647c478bd9Sstevel@tonic-gate #endif
6657c478bd9Sstevel@tonic-gate }
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate void
6697c478bd9Sstevel@tonic-gate SHA2Init(uint64_t mech, SHA2_CTX *ctx)
6707c478bd9Sstevel@tonic-gate {
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	switch (mech) {
6737c478bd9Sstevel@tonic-gate 	case SHA256_MECH_INFO_TYPE:
6747c478bd9Sstevel@tonic-gate 	case SHA256_HMAC_MECH_INFO_TYPE:
6757c478bd9Sstevel@tonic-gate 	case SHA256_HMAC_GEN_MECH_INFO_TYPE:
6767c478bd9Sstevel@tonic-gate 		ctx->state.s32[0] = 0x6a09e667U;
6777c478bd9Sstevel@tonic-gate 		ctx->state.s32[1] = 0xbb67ae85U;
6787c478bd9Sstevel@tonic-gate 		ctx->state.s32[2] = 0x3c6ef372U;
6797c478bd9Sstevel@tonic-gate 		ctx->state.s32[3] = 0xa54ff53aU;
6807c478bd9Sstevel@tonic-gate 		ctx->state.s32[4] = 0x510e527fU;
6817c478bd9Sstevel@tonic-gate 		ctx->state.s32[5] = 0x9b05688cU;
6827c478bd9Sstevel@tonic-gate 		ctx->state.s32[6] = 0x1f83d9abU;
6837c478bd9Sstevel@tonic-gate 		ctx->state.s32[7] = 0x5be0cd19U;
6847c478bd9Sstevel@tonic-gate 		break;
6857c478bd9Sstevel@tonic-gate 	case SHA384_MECH_INFO_TYPE:
6867c478bd9Sstevel@tonic-gate 	case SHA384_HMAC_MECH_INFO_TYPE:
6877c478bd9Sstevel@tonic-gate 	case SHA384_HMAC_GEN_MECH_INFO_TYPE:
6887c478bd9Sstevel@tonic-gate 		ctx->state.s64[0] = 0xcbbb9d5dc1059ed8ULL;
6897c478bd9Sstevel@tonic-gate 		ctx->state.s64[1] = 0x629a292a367cd507ULL;
6907c478bd9Sstevel@tonic-gate 		ctx->state.s64[2] = 0x9159015a3070dd17ULL;
6917c478bd9Sstevel@tonic-gate 		ctx->state.s64[3] = 0x152fecd8f70e5939ULL;
6927c478bd9Sstevel@tonic-gate 		ctx->state.s64[4] = 0x67332667ffc00b31ULL;
6937c478bd9Sstevel@tonic-gate 		ctx->state.s64[5] = 0x8eb44a8768581511ULL;
6947c478bd9Sstevel@tonic-gate 		ctx->state.s64[6] = 0xdb0c2e0d64f98fa7ULL;
6957c478bd9Sstevel@tonic-gate 		ctx->state.s64[7] = 0x47b5481dbefa4fa4ULL;
6967c478bd9Sstevel@tonic-gate 		break;
6977c478bd9Sstevel@tonic-gate 	case SHA512_MECH_INFO_TYPE:
6987c478bd9Sstevel@tonic-gate 	case SHA512_HMAC_MECH_INFO_TYPE:
6997c478bd9Sstevel@tonic-gate 	case SHA512_HMAC_GEN_MECH_INFO_TYPE:
7007c478bd9Sstevel@tonic-gate 		ctx->state.s64[0] = 0x6a09e667f3bcc908ULL;
7017c478bd9Sstevel@tonic-gate 		ctx->state.s64[1] = 0xbb67ae8584caa73bULL;
7027c478bd9Sstevel@tonic-gate 		ctx->state.s64[2] = 0x3c6ef372fe94f82bULL;
7037c478bd9Sstevel@tonic-gate 		ctx->state.s64[3] = 0xa54ff53a5f1d36f1ULL;
7047c478bd9Sstevel@tonic-gate 		ctx->state.s64[4] = 0x510e527fade682d1ULL;
7057c478bd9Sstevel@tonic-gate 		ctx->state.s64[5] = 0x9b05688c2b3e6c1fULL;
7067c478bd9Sstevel@tonic-gate 		ctx->state.s64[6] = 0x1f83d9abfb41bd6bULL;
7077c478bd9Sstevel@tonic-gate 		ctx->state.s64[7] = 0x5be0cd19137e2179ULL;
7087c478bd9Sstevel@tonic-gate 		break;
7097c478bd9Sstevel@tonic-gate #ifdef _KERNEL
7107c478bd9Sstevel@tonic-gate 	default:
711*734b6a94Sdarrenm 		cmn_err(CE_PANIC, "sha2_init: "
7127c478bd9Sstevel@tonic-gate 		    "failed to find a supported algorithm: 0x%x",
7137c478bd9Sstevel@tonic-gate 		    (uint32_t)mech);
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
7167c478bd9Sstevel@tonic-gate 	}
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	ctx->algotype = mech;
7197c478bd9Sstevel@tonic-gate 	ctx->count.c64[0] = ctx->count.c64[1] = 0;
7207c478bd9Sstevel@tonic-gate }
7217c478bd9Sstevel@tonic-gate 
722*734b6a94Sdarrenm #ifndef _KERNEL
723*734b6a94Sdarrenm 
724*734b6a94Sdarrenm #pragma inline(SHA256Init, SHA384Init, SHA512Init)
725*734b6a94Sdarrenm void
726*734b6a94Sdarrenm SHA256Init(SHA256_CTX *ctx)
727*734b6a94Sdarrenm {
728*734b6a94Sdarrenm 	SHA2Init(SHA256, ctx);
729*734b6a94Sdarrenm }
730*734b6a94Sdarrenm 
731*734b6a94Sdarrenm void
732*734b6a94Sdarrenm SHA384Init(SHA384_CTX *ctx)
733*734b6a94Sdarrenm {
734*734b6a94Sdarrenm 	SHA2Init(SHA384, ctx);
735*734b6a94Sdarrenm }
736*734b6a94Sdarrenm 
737*734b6a94Sdarrenm void
738*734b6a94Sdarrenm SHA512Init(SHA512_CTX *ctx)
739*734b6a94Sdarrenm {
740*734b6a94Sdarrenm 	SHA2Init(SHA512, ctx);
741*734b6a94Sdarrenm }
742*734b6a94Sdarrenm 
743*734b6a94Sdarrenm #endif /* _KERNEL */
744*734b6a94Sdarrenm 
7457c478bd9Sstevel@tonic-gate /*
7467c478bd9Sstevel@tonic-gate  * SHA2Update()
7477c478bd9Sstevel@tonic-gate  *
7487c478bd9Sstevel@tonic-gate  * purpose: continues an sha2 digest operation, using the message block
7497c478bd9Sstevel@tonic-gate  *          to update the context.
7507c478bd9Sstevel@tonic-gate  *   input: SHA2_CTX *	: the context to update
751*734b6a94Sdarrenm  *          void *	: the message block
752*734b6a94Sdarrenm  *          size_t    : the length of the message block in bytes
7537c478bd9Sstevel@tonic-gate  *  output: void
7547c478bd9Sstevel@tonic-gate  */
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate void
757*734b6a94Sdarrenm SHA2Update(SHA2_CTX *ctx, const void *inptr, size_t input_len)
7587c478bd9Sstevel@tonic-gate {
7597c478bd9Sstevel@tonic-gate 	uint32_t i, buf_index, buf_len, buf_limit;
760*734b6a94Sdarrenm 	const uint8_t *input = inptr;
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 	/* check for noop */
7637c478bd9Sstevel@tonic-gate 	if (input_len == 0)
7647c478bd9Sstevel@tonic-gate 		return;
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	if (ctx->algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE) {
7677c478bd9Sstevel@tonic-gate 		buf_limit = 64;
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 		/* compute number of bytes mod 64 */
7707c478bd9Sstevel@tonic-gate 		buf_index = (ctx->count.c32[1] >> 3) & 0x3F;
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 		/* update number of bits */
7737c478bd9Sstevel@tonic-gate 		if ((ctx->count.c32[1] += (input_len << 3)) < (input_len << 3))
7747c478bd9Sstevel@tonic-gate 			ctx->count.c32[0]++;
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 		ctx->count.c32[0] += (input_len >> 29);
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	} else {
7797c478bd9Sstevel@tonic-gate 		buf_limit = 128;
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 		/* compute number of bytes mod 128 */
7827c478bd9Sstevel@tonic-gate 		buf_index = (ctx->count.c64[1] >> 3) & 0x7F;
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 		/* update number of bits */
7857c478bd9Sstevel@tonic-gate 		if ((ctx->count.c64[1] += (input_len << 3)) < (input_len << 3))
7867c478bd9Sstevel@tonic-gate 			ctx->count.c64[0]++;
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 		ctx->count.c64[0] += (input_len >> 29);
7897c478bd9Sstevel@tonic-gate 	}
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	buf_len = buf_limit - buf_index;
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	/* transform as many times as possible */
7947c478bd9Sstevel@tonic-gate 	i = 0;
7957c478bd9Sstevel@tonic-gate 	if (input_len >= buf_len) {
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 		/*
7987c478bd9Sstevel@tonic-gate 		 * general optimization:
7997c478bd9Sstevel@tonic-gate 		 *
8007c478bd9Sstevel@tonic-gate 		 * only do initial bcopy() and SHA2Transform() if
8017c478bd9Sstevel@tonic-gate 		 * buf_index != 0.  if buf_index == 0, we're just
8027c478bd9Sstevel@tonic-gate 		 * wasting our time doing the bcopy() since there
8037c478bd9Sstevel@tonic-gate 		 * wasn't any data left over from a previous call to
8047c478bd9Sstevel@tonic-gate 		 * SHA2Update().
8057c478bd9Sstevel@tonic-gate 		 */
8067c478bd9Sstevel@tonic-gate 		if (buf_index) {
8077c478bd9Sstevel@tonic-gate 			bcopy(input, &ctx->buf_un.buf8[buf_index], buf_len);
8087c478bd9Sstevel@tonic-gate 			if (ctx->algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE)
8097c478bd9Sstevel@tonic-gate 				SHA256Transform(ctx, ctx->buf_un.buf8);
8107c478bd9Sstevel@tonic-gate 			else
8117c478bd9Sstevel@tonic-gate 				SHA512Transform(ctx, ctx->buf_un.buf8);
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 			i = buf_len;
8147c478bd9Sstevel@tonic-gate 		}
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 		for (; i + buf_limit - 1 < input_len; i += buf_limit) {
8187c478bd9Sstevel@tonic-gate 			if (ctx->algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE)
8197c478bd9Sstevel@tonic-gate 				SHA256Transform(ctx, &input[i]);
8207c478bd9Sstevel@tonic-gate 			else
8217c478bd9Sstevel@tonic-gate 				SHA512Transform(ctx, &input[i]);
8227c478bd9Sstevel@tonic-gate 		}
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 		/*
8257c478bd9Sstevel@tonic-gate 		 * general optimization:
8267c478bd9Sstevel@tonic-gate 		 *
8277c478bd9Sstevel@tonic-gate 		 * if i and input_len are the same, return now instead
8287c478bd9Sstevel@tonic-gate 		 * of calling bcopy(), since the bcopy() in this case
8297c478bd9Sstevel@tonic-gate 		 * will be an expensive nop.
8307c478bd9Sstevel@tonic-gate 		 */
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 		if (input_len == i)
8337c478bd9Sstevel@tonic-gate 			return;
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 		buf_index = 0;
8367c478bd9Sstevel@tonic-gate 	}
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 	/* buffer remaining input */
8397c478bd9Sstevel@tonic-gate 	bcopy(&input[i], &ctx->buf_un.buf8[buf_index], input_len - i);
8407c478bd9Sstevel@tonic-gate }
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate /*
8447c478bd9Sstevel@tonic-gate  * SHA2Final()
8457c478bd9Sstevel@tonic-gate  *
8467c478bd9Sstevel@tonic-gate  * purpose: ends an sha2 digest operation, finalizing the message digest and
8477c478bd9Sstevel@tonic-gate  *          zeroing the context.
8487c478bd9Sstevel@tonic-gate  *   input: uint8_t *	: a buffer to store the digest in
8497c478bd9Sstevel@tonic-gate  *          SHA2_CTX *  : the context to finalize, save, and zero
8507c478bd9Sstevel@tonic-gate  *  output: void
8517c478bd9Sstevel@tonic-gate  */
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate void
854*734b6a94Sdarrenm SHA2Final(void *digest, SHA2_CTX *ctx)
8557c478bd9Sstevel@tonic-gate {
8567c478bd9Sstevel@tonic-gate 	uint8_t		bitcount_be[sizeof (ctx->count.c32)];
8577c478bd9Sstevel@tonic-gate 	uint8_t		bitcount_be64[sizeof (ctx->count.c64)];
8587c478bd9Sstevel@tonic-gate 	uint32_t	index;
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 	if (ctx->algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE) {
8627c478bd9Sstevel@tonic-gate 		index  = (ctx->count.c32[1] >> 3) & 0x3f;
8637c478bd9Sstevel@tonic-gate 		Encode(bitcount_be, ctx->count.c32, sizeof (bitcount_be));
8647c478bd9Sstevel@tonic-gate 		SHA2Update(ctx, PADDING, ((index < 56) ? 56 : 120) - index);
8657c478bd9Sstevel@tonic-gate 		SHA2Update(ctx, bitcount_be, sizeof (bitcount_be));
8667c478bd9Sstevel@tonic-gate 		Encode(digest, ctx->state.s32, sizeof (ctx->state.s32));
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	} else {
8697c478bd9Sstevel@tonic-gate 		index  = (ctx->count.c64[1] >> 3) & 0x7f;
8707c478bd9Sstevel@tonic-gate 		Encode64(bitcount_be64, ctx->count.c64,
8717c478bd9Sstevel@tonic-gate 		    sizeof (bitcount_be64));
8727c478bd9Sstevel@tonic-gate 		SHA2Update(ctx, PADDING, ((index < 112) ? 112 : 240) - index);
8737c478bd9Sstevel@tonic-gate 		SHA2Update(ctx, bitcount_be64, sizeof (bitcount_be64));
8747c478bd9Sstevel@tonic-gate 		if (ctx->algotype <= SHA384_HMAC_GEN_MECH_INFO_TYPE) {
8757c478bd9Sstevel@tonic-gate 			ctx->state.s64[6] = ctx->state.s64[7] = 0;
8767c478bd9Sstevel@tonic-gate 			Encode64(digest, ctx->state.s64,
8777c478bd9Sstevel@tonic-gate 			    sizeof (uint64_t) * 6);
8787c478bd9Sstevel@tonic-gate 		} else
8797c478bd9Sstevel@tonic-gate 			Encode64(digest, ctx->state.s64,
8807c478bd9Sstevel@tonic-gate 			    sizeof (ctx->state.s64));
8817c478bd9Sstevel@tonic-gate 	}
882673007c6Sdarrenm 
883673007c6Sdarrenm 	/* zeroize sensitive information */
884673007c6Sdarrenm 	bzero(ctx, sizeof (*ctx));
8857c478bd9Sstevel@tonic-gate }
886