xref: /titanic_50/usr/src/common/crypto/sha2/sha2.c (revision 55553f719b521a0bb4deab6efc944cd30c1a56aa)
17c478bd9Sstevel@tonic-gate /*
2*55553f71Sda73024  * Copyright 2008 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  * The basic framework for this code came from the reference
107c478bd9Sstevel@tonic-gate  * implementation for MD5.  That implementation is Copyright (C)
117c478bd9Sstevel@tonic-gate  * 1991-2, RSA Data Security, Inc. Created 1991. All rights reserved.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * License to copy and use this software is granted provided that it
147c478bd9Sstevel@tonic-gate  * is identified as the "RSA Data Security, Inc. MD5 Message-Digest
157c478bd9Sstevel@tonic-gate  * Algorithm" in all material mentioning or referencing this software
167c478bd9Sstevel@tonic-gate  * or this function.
177c478bd9Sstevel@tonic-gate  *
187c478bd9Sstevel@tonic-gate  * License is also granted to make and use derivative works provided
197c478bd9Sstevel@tonic-gate  * that such works are identified as "derived from the RSA Data
207c478bd9Sstevel@tonic-gate  * Security, Inc. MD5 Message-Digest Algorithm" in all material
217c478bd9Sstevel@tonic-gate  * mentioning or referencing the derived work.
227c478bd9Sstevel@tonic-gate  *
237c478bd9Sstevel@tonic-gate  * RSA Data Security, Inc. makes no representations concerning either
247c478bd9Sstevel@tonic-gate  * the merchantability of this software or the suitability of this
257c478bd9Sstevel@tonic-gate  * software for any particular purpose. It is provided "as is"
267c478bd9Sstevel@tonic-gate  * without express or implied warranty of any kind.
277c478bd9Sstevel@tonic-gate  *
287c478bd9Sstevel@tonic-gate  * These notices must be retained in any copies of any part of this
297c478bd9Sstevel@tonic-gate  * documentation and/or software.
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * NOTE: Cleaned-up and optimized, version of SHA2, based on the FIPS 180-2
327c478bd9Sstevel@tonic-gate  * standard, available at http://www.itl.nist.gov/div897/pubs/fip180-2.htm
337c478bd9Sstevel@tonic-gate  * Not as fast as one would like -- further optimizations are encouraged
347c478bd9Sstevel@tonic-gate  * and appreciated.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <sys/param.h>
397c478bd9Sstevel@tonic-gate #include <sys/systm.h>
407c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
41734b6a94Sdarrenm #define	_SHA2_IMPL
427c478bd9Sstevel@tonic-gate #include <sys/sha2.h>
437c478bd9Sstevel@tonic-gate #include <sys/sha2_consts.h>
447c478bd9Sstevel@tonic-gate 
45*55553f71Sda73024 #ifdef _KERNEL
46*55553f71Sda73024 #include <sys/cmn_err.h>
477c478bd9Sstevel@tonic-gate 
48*55553f71Sda73024 #else
497c478bd9Sstevel@tonic-gate #include <strings.h>
507c478bd9Sstevel@tonic-gate #include <stdlib.h>
517c478bd9Sstevel@tonic-gate #include <errno.h>
527c478bd9Sstevel@tonic-gate 
53734b6a94Sdarrenm #pragma weak SHA256Update = SHA2Update
54734b6a94Sdarrenm #pragma weak SHA384Update = SHA2Update
55734b6a94Sdarrenm #pragma weak SHA512Update = SHA2Update
56734b6a94Sdarrenm 
57734b6a94Sdarrenm #pragma weak SHA256Final = SHA2Final
58734b6a94Sdarrenm #pragma weak SHA384Final = SHA2Final
59734b6a94Sdarrenm #pragma weak SHA512Final = SHA2Final
60734b6a94Sdarrenm 
61734b6a94Sdarrenm #endif	/* _KERNEL */
62734b6a94Sdarrenm 
637c478bd9Sstevel@tonic-gate static void Encode(uint8_t *, uint32_t *, size_t);
647c478bd9Sstevel@tonic-gate static void Encode64(uint8_t *, uint64_t *, size_t);
65*55553f71Sda73024 
66*55553f71Sda73024 #if	defined(__amd64)
67*55553f71Sda73024 #define	SHA512Transform(ctx, in) SHA512TransformBlocks((ctx), (in), 1)
68*55553f71Sda73024 #define	SHA256Transform(ctx, in) SHA256TransformBlocks((ctx), (in), 1)
69*55553f71Sda73024 
70*55553f71Sda73024 void SHA512TransformBlocks(SHA2_CTX *ctx, const void *in, size_t num);
71*55553f71Sda73024 void SHA256TransformBlocks(SHA2_CTX *ctx, const void *in, size_t num);
72*55553f71Sda73024 
73*55553f71Sda73024 #else
747c478bd9Sstevel@tonic-gate static void SHA256Transform(SHA2_CTX *, const uint8_t *);
757c478bd9Sstevel@tonic-gate static void SHA512Transform(SHA2_CTX *, const uint8_t *);
76*55553f71Sda73024 #endif	/* __amd64 */
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate static uint8_t PADDING[128] = { 0x80, /* all zeros */ };
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /* Ch and Maj are the basic SHA2 functions. */
817c478bd9Sstevel@tonic-gate #define	Ch(b, c, d)	(((b) & (c)) ^ ((~b) & (d)))
827c478bd9Sstevel@tonic-gate #define	Maj(b, c, d)	(((b) & (c)) ^ ((b) & (d)) ^ ((c) & (d)))
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /* Rotates x right n bits. */
857c478bd9Sstevel@tonic-gate #define	ROTR(x, n)	\
867c478bd9Sstevel@tonic-gate 	(((x) >> (n)) | ((x) << ((sizeof (x) * NBBY)-(n))))
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate /* Shift x right n bits */
897c478bd9Sstevel@tonic-gate #define	SHR(x, n)	((x) >> (n))
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate /* SHA256 Functions */
927c478bd9Sstevel@tonic-gate #define	BIGSIGMA0_256(x)	(ROTR((x), 2) ^ ROTR((x), 13) ^ ROTR((x), 22))
937c478bd9Sstevel@tonic-gate #define	BIGSIGMA1_256(x)	(ROTR((x), 6) ^ ROTR((x), 11) ^ ROTR((x), 25))
947c478bd9Sstevel@tonic-gate #define	SIGMA0_256(x)		(ROTR((x), 7) ^ ROTR((x), 18) ^ SHR((x), 3))
957c478bd9Sstevel@tonic-gate #define	SIGMA1_256(x)		(ROTR((x), 17) ^ ROTR((x), 19) ^ SHR((x), 10))
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate #define	SHA256ROUND(a, b, c, d, e, f, g, h, i, w)			\
987c478bd9Sstevel@tonic-gate 	T1 = h + BIGSIGMA1_256(e) + Ch(e, f, g) + SHA256_CONST(i) + w;	\
997c478bd9Sstevel@tonic-gate 	d += T1;							\
1007c478bd9Sstevel@tonic-gate 	T2 = BIGSIGMA0_256(a) + Maj(a, b, c);				\
1017c478bd9Sstevel@tonic-gate 	h = T1 + T2
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate /* SHA384/512 Functions */
1047c478bd9Sstevel@tonic-gate #define	BIGSIGMA0(x)	(ROTR((x), 28) ^ ROTR((x), 34) ^ ROTR((x), 39))
1057c478bd9Sstevel@tonic-gate #define	BIGSIGMA1(x)	(ROTR((x), 14) ^ ROTR((x), 18) ^ ROTR((x), 41))
1067c478bd9Sstevel@tonic-gate #define	SIGMA0(x)	(ROTR((x), 1) ^ ROTR((x), 8) ^ SHR((x), 7))
1077c478bd9Sstevel@tonic-gate #define	SIGMA1(x)	(ROTR((x), 19) ^ ROTR((x), 61) ^ SHR((x), 6))
1087c478bd9Sstevel@tonic-gate #define	SHA512ROUND(a, b, c, d, e, f, g, h, i, w)			\
1097c478bd9Sstevel@tonic-gate 	T1 = h + BIGSIGMA1(e) + Ch(e, f, g) + SHA512_CONST(i) + w;	\
1107c478bd9Sstevel@tonic-gate 	d += T1;							\
1117c478bd9Sstevel@tonic-gate 	T2 = BIGSIGMA0(a) + Maj(a, b, c);				\
1127c478bd9Sstevel@tonic-gate 	h = T1 + T2
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate  * sparc optimization:
1167c478bd9Sstevel@tonic-gate  *
1177c478bd9Sstevel@tonic-gate  * on the sparc, we can load big endian 32-bit data easily.  note that
1187c478bd9Sstevel@tonic-gate  * special care must be taken to ensure the address is 32-bit aligned.
1197c478bd9Sstevel@tonic-gate  * in the interest of speed, we don't check to make sure, since
1207c478bd9Sstevel@tonic-gate  * careful programming can guarantee this for us.
1217c478bd9Sstevel@tonic-gate  */
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate #if	defined(_BIG_ENDIAN)
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate #define	LOAD_BIG_32(addr)	(*(uint32_t *)(addr))
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate #else	/* little endian -- will work on big endian, but slowly */
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate #define	LOAD_BIG_32(addr)	\
1307c478bd9Sstevel@tonic-gate 	(((addr)[0] << 24) | ((addr)[1] << 16) | ((addr)[2] << 8) | (addr)[3])
1317c478bd9Sstevel@tonic-gate #endif
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate #if	defined(_BIG_ENDIAN)
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate #define	LOAD_BIG_64(addr)	(*(uint64_t *)(addr))
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate #else	/* little endian -- will work on big endian, but slowly */
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate #define	LOAD_BIG_64(addr)	\
1417c478bd9Sstevel@tonic-gate 	(((uint64_t)(addr)[0] << 56) | ((uint64_t)(addr)[1] << 48) |	\
1427c478bd9Sstevel@tonic-gate 	    ((uint64_t)(addr)[2] << 40) | ((uint64_t)(addr)[3] << 32) |	\
1437c478bd9Sstevel@tonic-gate 	    ((uint64_t)(addr)[4] << 24) | ((uint64_t)(addr)[5] << 16) |	\
1447c478bd9Sstevel@tonic-gate 	    ((uint64_t)(addr)[6] << 8) | (uint64_t)(addr)[7])
1457c478bd9Sstevel@tonic-gate #endif
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 
148*55553f71Sda73024 #if	!defined(__amd64)
1497c478bd9Sstevel@tonic-gate /* SHA256 Transform */
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate static void
1527c478bd9Sstevel@tonic-gate SHA256Transform(SHA2_CTX *ctx, const uint8_t *blk)
1537c478bd9Sstevel@tonic-gate {
1547c478bd9Sstevel@tonic-gate 	uint32_t a = ctx->state.s32[0];
1557c478bd9Sstevel@tonic-gate 	uint32_t b = ctx->state.s32[1];
1567c478bd9Sstevel@tonic-gate 	uint32_t c = ctx->state.s32[2];
1577c478bd9Sstevel@tonic-gate 	uint32_t d = ctx->state.s32[3];
1587c478bd9Sstevel@tonic-gate 	uint32_t e = ctx->state.s32[4];
1597c478bd9Sstevel@tonic-gate 	uint32_t f = ctx->state.s32[5];
1607c478bd9Sstevel@tonic-gate 	uint32_t g = ctx->state.s32[6];
1617c478bd9Sstevel@tonic-gate 	uint32_t h = ctx->state.s32[7];
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	uint32_t w0, w1, w2, w3, w4, w5, w6, w7;
1647c478bd9Sstevel@tonic-gate 	uint32_t w8, w9, w10, w11, w12, w13, w14, w15;
1657c478bd9Sstevel@tonic-gate 	uint32_t T1, T2;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate #if	defined(__sparc)
1687c478bd9Sstevel@tonic-gate 	static const uint32_t sha256_consts[] = {
1697c478bd9Sstevel@tonic-gate 		SHA256_CONST_0, SHA256_CONST_1, SHA256_CONST_2,
1707c478bd9Sstevel@tonic-gate 		SHA256_CONST_3, SHA256_CONST_4, SHA256_CONST_5,
1717c478bd9Sstevel@tonic-gate 		SHA256_CONST_6, SHA256_CONST_7, SHA256_CONST_8,
1727c478bd9Sstevel@tonic-gate 		SHA256_CONST_9, SHA256_CONST_10, SHA256_CONST_11,
1737c478bd9Sstevel@tonic-gate 		SHA256_CONST_12, SHA256_CONST_13, SHA256_CONST_14,
1747c478bd9Sstevel@tonic-gate 		SHA256_CONST_15, SHA256_CONST_16, SHA256_CONST_17,
1757c478bd9Sstevel@tonic-gate 		SHA256_CONST_18, SHA256_CONST_19, SHA256_CONST_20,
1767c478bd9Sstevel@tonic-gate 		SHA256_CONST_21, SHA256_CONST_22, SHA256_CONST_23,
1777c478bd9Sstevel@tonic-gate 		SHA256_CONST_24, SHA256_CONST_25, SHA256_CONST_26,
1787c478bd9Sstevel@tonic-gate 		SHA256_CONST_27, SHA256_CONST_28, SHA256_CONST_29,
1797c478bd9Sstevel@tonic-gate 		SHA256_CONST_30, SHA256_CONST_31, SHA256_CONST_32,
1807c478bd9Sstevel@tonic-gate 		SHA256_CONST_33, SHA256_CONST_34, SHA256_CONST_35,
1817c478bd9Sstevel@tonic-gate 		SHA256_CONST_36, SHA256_CONST_37, SHA256_CONST_38,
1827c478bd9Sstevel@tonic-gate 		SHA256_CONST_39, SHA256_CONST_40, SHA256_CONST_41,
1837c478bd9Sstevel@tonic-gate 		SHA256_CONST_42, SHA256_CONST_43, SHA256_CONST_44,
1847c478bd9Sstevel@tonic-gate 		SHA256_CONST_45, SHA256_CONST_46, SHA256_CONST_47,
1857c478bd9Sstevel@tonic-gate 		SHA256_CONST_48, SHA256_CONST_49, SHA256_CONST_50,
1867c478bd9Sstevel@tonic-gate 		SHA256_CONST_51, SHA256_CONST_52, SHA256_CONST_53,
1877c478bd9Sstevel@tonic-gate 		SHA256_CONST_54, SHA256_CONST_55, SHA256_CONST_56,
1887c478bd9Sstevel@tonic-gate 		SHA256_CONST_57, SHA256_CONST_58, SHA256_CONST_59,
1897c478bd9Sstevel@tonic-gate 		SHA256_CONST_60, SHA256_CONST_61, SHA256_CONST_62,
1907c478bd9Sstevel@tonic-gate 		SHA256_CONST_63
1917c478bd9Sstevel@tonic-gate 	};
192*55553f71Sda73024 #endif	/* __sparc */
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	if ((uintptr_t)blk & 0x3) {		/* not 4-byte aligned? */
1957c478bd9Sstevel@tonic-gate 		bcopy(blk, ctx->buf_un.buf32,  sizeof (ctx->buf_un.buf32));
1967c478bd9Sstevel@tonic-gate 		blk = (uint8_t *)ctx->buf_un.buf32;
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate 
199734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
200f66d273dSizick 	w0 =  LOAD_BIG_32(blk + 4 * 0);
201f66d273dSizick 	SHA256ROUND(a, b, c, d, e, f, g, h, 0, w0);
202734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
203f66d273dSizick 	w1 =  LOAD_BIG_32(blk + 4 * 1);
204f66d273dSizick 	SHA256ROUND(h, a, b, c, d, e, f, g, 1, w1);
205734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
206f66d273dSizick 	w2 =  LOAD_BIG_32(blk + 4 * 2);
207f66d273dSizick 	SHA256ROUND(g, h, a, b, c, d, e, f, 2, w2);
208734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
209f66d273dSizick 	w3 =  LOAD_BIG_32(blk + 4 * 3);
210f66d273dSizick 	SHA256ROUND(f, g, h, a, b, c, d, e, 3, w3);
211734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
212f66d273dSizick 	w4 =  LOAD_BIG_32(blk + 4 * 4);
213f66d273dSizick 	SHA256ROUND(e, f, g, h, a, b, c, d, 4, w4);
214734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
215f66d273dSizick 	w5 =  LOAD_BIG_32(blk + 4 * 5);
216f66d273dSizick 	SHA256ROUND(d, e, f, g, h, a, b, c, 5, w5);
217734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
218f66d273dSizick 	w6 =  LOAD_BIG_32(blk + 4 * 6);
219f66d273dSizick 	SHA256ROUND(c, d, e, f, g, h, a, b, 6, w6);
220734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
221f66d273dSizick 	w7 =  LOAD_BIG_32(blk + 4 * 7);
222f66d273dSizick 	SHA256ROUND(b, c, d, e, f, g, h, a, 7, w7);
223734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
224f66d273dSizick 	w8 =  LOAD_BIG_32(blk + 4 * 8);
225f66d273dSizick 	SHA256ROUND(a, b, c, d, e, f, g, h, 8, w8);
226734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
227f66d273dSizick 	w9 =  LOAD_BIG_32(blk + 4 * 9);
228f66d273dSizick 	SHA256ROUND(h, a, b, c, d, e, f, g, 9, w9);
229734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
230f66d273dSizick 	w10 =  LOAD_BIG_32(blk + 4 * 10);
231f66d273dSizick 	SHA256ROUND(g, h, a, b, c, d, e, f, 10, w10);
232734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
233f66d273dSizick 	w11 =  LOAD_BIG_32(blk + 4 * 11);
234f66d273dSizick 	SHA256ROUND(f, g, h, a, b, c, d, e, 11, w11);
235734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
236f66d273dSizick 	w12 =  LOAD_BIG_32(blk + 4 * 12);
237f66d273dSizick 	SHA256ROUND(e, f, g, h, a, b, c, d, 12, w12);
238734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
239f66d273dSizick 	w13 =  LOAD_BIG_32(blk + 4 * 13);
240f66d273dSizick 	SHA256ROUND(d, e, f, g, h, a, b, c, 13, w13);
241734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
242f66d273dSizick 	w14 =  LOAD_BIG_32(blk + 4 * 14);
243f66d273dSizick 	SHA256ROUND(c, d, e, f, g, h, a, b, 14, w14);
244734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
245f66d273dSizick 	w15 =  LOAD_BIG_32(blk + 4 * 15);
246f66d273dSizick 	SHA256ROUND(b, c, d, e, f, g, h, a, 15, w15);
247f66d273dSizick 
2487c478bd9Sstevel@tonic-gate 	w0 = SIGMA1_256(w14) + w9 + SIGMA0_256(w1) + w0;
2497c478bd9Sstevel@tonic-gate 	SHA256ROUND(a, b, c, d, e, f, g, h, 16, w0);
2507c478bd9Sstevel@tonic-gate 	w1 = SIGMA1_256(w15) + w10 + SIGMA0_256(w2) + w1;
2517c478bd9Sstevel@tonic-gate 	SHA256ROUND(h, a, b, c, d, e, f, g, 17, w1);
2527c478bd9Sstevel@tonic-gate 	w2 = SIGMA1_256(w0) + w11 + SIGMA0_256(w3) + w2;
2537c478bd9Sstevel@tonic-gate 	SHA256ROUND(g, h, a, b, c, d, e, f, 18, w2);
2547c478bd9Sstevel@tonic-gate 	w3 = SIGMA1_256(w1) + w12 + SIGMA0_256(w4) + w3;
2557c478bd9Sstevel@tonic-gate 	SHA256ROUND(f, g, h, a, b, c, d, e, 19, w3);
2567c478bd9Sstevel@tonic-gate 	w4 = SIGMA1_256(w2) + w13 + SIGMA0_256(w5) + w4;
2577c478bd9Sstevel@tonic-gate 	SHA256ROUND(e, f, g, h, a, b, c, d, 20, w4);
2587c478bd9Sstevel@tonic-gate 	w5 = SIGMA1_256(w3) + w14 + SIGMA0_256(w6) + w5;
2597c478bd9Sstevel@tonic-gate 	SHA256ROUND(d, e, f, g, h, a, b, c, 21, w5);
2607c478bd9Sstevel@tonic-gate 	w6 = SIGMA1_256(w4) + w15 + SIGMA0_256(w7) + w6;
2617c478bd9Sstevel@tonic-gate 	SHA256ROUND(c, d, e, f, g, h, a, b, 22, w6);
2627c478bd9Sstevel@tonic-gate 	w7 = SIGMA1_256(w5) + w0 + SIGMA0_256(w8) + w7;
2637c478bd9Sstevel@tonic-gate 	SHA256ROUND(b, c, d, e, f, g, h, a, 23, w7);
2647c478bd9Sstevel@tonic-gate 	w8 = SIGMA1_256(w6) + w1 + SIGMA0_256(w9) + w8;
2657c478bd9Sstevel@tonic-gate 	SHA256ROUND(a, b, c, d, e, f, g, h, 24, w8);
2667c478bd9Sstevel@tonic-gate 	w9 = SIGMA1_256(w7) + w2 + SIGMA0_256(w10) + w9;
2677c478bd9Sstevel@tonic-gate 	SHA256ROUND(h, a, b, c, d, e, f, g, 25, w9);
2687c478bd9Sstevel@tonic-gate 	w10 = SIGMA1_256(w8) + w3 + SIGMA0_256(w11) + w10;
2697c478bd9Sstevel@tonic-gate 	SHA256ROUND(g, h, a, b, c, d, e, f, 26, w10);
2707c478bd9Sstevel@tonic-gate 	w11 = SIGMA1_256(w9) + w4 + SIGMA0_256(w12) + w11;
2717c478bd9Sstevel@tonic-gate 	SHA256ROUND(f, g, h, a, b, c, d, e, 27, w11);
2727c478bd9Sstevel@tonic-gate 	w12 = SIGMA1_256(w10) + w5 + SIGMA0_256(w13) + w12;
2737c478bd9Sstevel@tonic-gate 	SHA256ROUND(e, f, g, h, a, b, c, d, 28, w12);
2747c478bd9Sstevel@tonic-gate 	w13 = SIGMA1_256(w11) + w6 + SIGMA0_256(w14) + w13;
2757c478bd9Sstevel@tonic-gate 	SHA256ROUND(d, e, f, g, h, a, b, c, 29, w13);
2767c478bd9Sstevel@tonic-gate 	w14 = SIGMA1_256(w12) + w7 + SIGMA0_256(w15) + w14;
2777c478bd9Sstevel@tonic-gate 	SHA256ROUND(c, d, e, f, g, h, a, b, 30, w14);
2787c478bd9Sstevel@tonic-gate 	w15 = SIGMA1_256(w13) + w8 + SIGMA0_256(w0) + w15;
2797c478bd9Sstevel@tonic-gate 	SHA256ROUND(b, c, d, e, f, g, h, a, 31, w15);
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	w0 = SIGMA1_256(w14) + w9 + SIGMA0_256(w1) + w0;
2827c478bd9Sstevel@tonic-gate 	SHA256ROUND(a, b, c, d, e, f, g, h, 32, w0);
2837c478bd9Sstevel@tonic-gate 	w1 = SIGMA1_256(w15) + w10 + SIGMA0_256(w2) + w1;
2847c478bd9Sstevel@tonic-gate 	SHA256ROUND(h, a, b, c, d, e, f, g, 33, w1);
2857c478bd9Sstevel@tonic-gate 	w2 = SIGMA1_256(w0) + w11 + SIGMA0_256(w3) + w2;
2867c478bd9Sstevel@tonic-gate 	SHA256ROUND(g, h, a, b, c, d, e, f, 34, w2);
2877c478bd9Sstevel@tonic-gate 	w3 = SIGMA1_256(w1) + w12 + SIGMA0_256(w4) + w3;
2887c478bd9Sstevel@tonic-gate 	SHA256ROUND(f, g, h, a, b, c, d, e, 35, w3);
2897c478bd9Sstevel@tonic-gate 	w4 = SIGMA1_256(w2) + w13 + SIGMA0_256(w5) + w4;
2907c478bd9Sstevel@tonic-gate 	SHA256ROUND(e, f, g, h, a, b, c, d, 36, w4);
2917c478bd9Sstevel@tonic-gate 	w5 = SIGMA1_256(w3) + w14 + SIGMA0_256(w6) + w5;
2927c478bd9Sstevel@tonic-gate 	SHA256ROUND(d, e, f, g, h, a, b, c, 37, w5);
2937c478bd9Sstevel@tonic-gate 	w6 = SIGMA1_256(w4) + w15 + SIGMA0_256(w7) + w6;
2947c478bd9Sstevel@tonic-gate 	SHA256ROUND(c, d, e, f, g, h, a, b, 38, w6);
2957c478bd9Sstevel@tonic-gate 	w7 = SIGMA1_256(w5) + w0 + SIGMA0_256(w8) + w7;
2967c478bd9Sstevel@tonic-gate 	SHA256ROUND(b, c, d, e, f, g, h, a, 39, w7);
2977c478bd9Sstevel@tonic-gate 	w8 = SIGMA1_256(w6) + w1 + SIGMA0_256(w9) + w8;
2987c478bd9Sstevel@tonic-gate 	SHA256ROUND(a, b, c, d, e, f, g, h, 40, w8);
2997c478bd9Sstevel@tonic-gate 	w9 = SIGMA1_256(w7) + w2 + SIGMA0_256(w10) + w9;
3007c478bd9Sstevel@tonic-gate 	SHA256ROUND(h, a, b, c, d, e, f, g, 41, w9);
3017c478bd9Sstevel@tonic-gate 	w10 = SIGMA1_256(w8) + w3 + SIGMA0_256(w11) + w10;
3027c478bd9Sstevel@tonic-gate 	SHA256ROUND(g, h, a, b, c, d, e, f, 42, w10);
3037c478bd9Sstevel@tonic-gate 	w11 = SIGMA1_256(w9) + w4 + SIGMA0_256(w12) + w11;
3047c478bd9Sstevel@tonic-gate 	SHA256ROUND(f, g, h, a, b, c, d, e, 43, w11);
3057c478bd9Sstevel@tonic-gate 	w12 = SIGMA1_256(w10) + w5 + SIGMA0_256(w13) + w12;
3067c478bd9Sstevel@tonic-gate 	SHA256ROUND(e, f, g, h, a, b, c, d, 44, w12);
3077c478bd9Sstevel@tonic-gate 	w13 = SIGMA1_256(w11) + w6 + SIGMA0_256(w14) + w13;
3087c478bd9Sstevel@tonic-gate 	SHA256ROUND(d, e, f, g, h, a, b, c, 45, w13);
3097c478bd9Sstevel@tonic-gate 	w14 = SIGMA1_256(w12) + w7 + SIGMA0_256(w15) + w14;
3107c478bd9Sstevel@tonic-gate 	SHA256ROUND(c, d, e, f, g, h, a, b, 46, w14);
3117c478bd9Sstevel@tonic-gate 	w15 = SIGMA1_256(w13) + w8 + SIGMA0_256(w0) + w15;
3127c478bd9Sstevel@tonic-gate 	SHA256ROUND(b, c, d, e, f, g, h, a, 47, w15);
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	w0 = SIGMA1_256(w14) + w9 + SIGMA0_256(w1) + w0;
3157c478bd9Sstevel@tonic-gate 	SHA256ROUND(a, b, c, d, e, f, g, h, 48, w0);
3167c478bd9Sstevel@tonic-gate 	w1 = SIGMA1_256(w15) + w10 + SIGMA0_256(w2) + w1;
3177c478bd9Sstevel@tonic-gate 	SHA256ROUND(h, a, b, c, d, e, f, g, 49, w1);
3187c478bd9Sstevel@tonic-gate 	w2 = SIGMA1_256(w0) + w11 + SIGMA0_256(w3) + w2;
3197c478bd9Sstevel@tonic-gate 	SHA256ROUND(g, h, a, b, c, d, e, f, 50, w2);
3207c478bd9Sstevel@tonic-gate 	w3 = SIGMA1_256(w1) + w12 + SIGMA0_256(w4) + w3;
3217c478bd9Sstevel@tonic-gate 	SHA256ROUND(f, g, h, a, b, c, d, e, 51, w3);
3227c478bd9Sstevel@tonic-gate 	w4 = SIGMA1_256(w2) + w13 + SIGMA0_256(w5) + w4;
3237c478bd9Sstevel@tonic-gate 	SHA256ROUND(e, f, g, h, a, b, c, d, 52, w4);
3247c478bd9Sstevel@tonic-gate 	w5 = SIGMA1_256(w3) + w14 + SIGMA0_256(w6) + w5;
3257c478bd9Sstevel@tonic-gate 	SHA256ROUND(d, e, f, g, h, a, b, c, 53, w5);
3267c478bd9Sstevel@tonic-gate 	w6 = SIGMA1_256(w4) + w15 + SIGMA0_256(w7) + w6;
3277c478bd9Sstevel@tonic-gate 	SHA256ROUND(c, d, e, f, g, h, a, b, 54, w6);
3287c478bd9Sstevel@tonic-gate 	w7 = SIGMA1_256(w5) + w0 + SIGMA0_256(w8) + w7;
3297c478bd9Sstevel@tonic-gate 	SHA256ROUND(b, c, d, e, f, g, h, a, 55, w7);
3307c478bd9Sstevel@tonic-gate 	w8 = SIGMA1_256(w6) + w1 + SIGMA0_256(w9) + w8;
3317c478bd9Sstevel@tonic-gate 	SHA256ROUND(a, b, c, d, e, f, g, h, 56, w8);
3327c478bd9Sstevel@tonic-gate 	w9 = SIGMA1_256(w7) + w2 + SIGMA0_256(w10) + w9;
3337c478bd9Sstevel@tonic-gate 	SHA256ROUND(h, a, b, c, d, e, f, g, 57, w9);
3347c478bd9Sstevel@tonic-gate 	w10 = SIGMA1_256(w8) + w3 + SIGMA0_256(w11) + w10;
3357c478bd9Sstevel@tonic-gate 	SHA256ROUND(g, h, a, b, c, d, e, f, 58, w10);
3367c478bd9Sstevel@tonic-gate 	w11 = SIGMA1_256(w9) + w4 + SIGMA0_256(w12) + w11;
3377c478bd9Sstevel@tonic-gate 	SHA256ROUND(f, g, h, a, b, c, d, e, 59, w11);
3387c478bd9Sstevel@tonic-gate 	w12 = SIGMA1_256(w10) + w5 + SIGMA0_256(w13) + w12;
3397c478bd9Sstevel@tonic-gate 	SHA256ROUND(e, f, g, h, a, b, c, d, 60, w12);
3407c478bd9Sstevel@tonic-gate 	w13 = SIGMA1_256(w11) + w6 + SIGMA0_256(w14) + w13;
3417c478bd9Sstevel@tonic-gate 	SHA256ROUND(d, e, f, g, h, a, b, c, 61, w13);
3427c478bd9Sstevel@tonic-gate 	w14 = SIGMA1_256(w12) + w7 + SIGMA0_256(w15) + w14;
3437c478bd9Sstevel@tonic-gate 	SHA256ROUND(c, d, e, f, g, h, a, b, 62, w14);
3447c478bd9Sstevel@tonic-gate 	w15 = SIGMA1_256(w13) + w8 + SIGMA0_256(w0) + w15;
3457c478bd9Sstevel@tonic-gate 	SHA256ROUND(b, c, d, e, f, g, h, a, 63, w15);
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	ctx->state.s32[0] += a;
3487c478bd9Sstevel@tonic-gate 	ctx->state.s32[1] += b;
3497c478bd9Sstevel@tonic-gate 	ctx->state.s32[2] += c;
3507c478bd9Sstevel@tonic-gate 	ctx->state.s32[3] += d;
3517c478bd9Sstevel@tonic-gate 	ctx->state.s32[4] += e;
3527c478bd9Sstevel@tonic-gate 	ctx->state.s32[5] += f;
3537c478bd9Sstevel@tonic-gate 	ctx->state.s32[6] += g;
3547c478bd9Sstevel@tonic-gate 	ctx->state.s32[7] += h;
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate /* SHA384 and SHA512 Transform */
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate static void
3617c478bd9Sstevel@tonic-gate SHA512Transform(SHA2_CTX *ctx, const uint8_t *blk)
3627c478bd9Sstevel@tonic-gate {
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	uint64_t a = ctx->state.s64[0];
3657c478bd9Sstevel@tonic-gate 	uint64_t b = ctx->state.s64[1];
3667c478bd9Sstevel@tonic-gate 	uint64_t c = ctx->state.s64[2];
3677c478bd9Sstevel@tonic-gate 	uint64_t d = ctx->state.s64[3];
3687c478bd9Sstevel@tonic-gate 	uint64_t e = ctx->state.s64[4];
3697c478bd9Sstevel@tonic-gate 	uint64_t f = ctx->state.s64[5];
3707c478bd9Sstevel@tonic-gate 	uint64_t g = ctx->state.s64[6];
3717c478bd9Sstevel@tonic-gate 	uint64_t h = ctx->state.s64[7];
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	uint64_t w0, w1, w2, w3, w4, w5, w6, w7;
3747c478bd9Sstevel@tonic-gate 	uint64_t w8, w9, w10, w11, w12, w13, w14, w15;
3757c478bd9Sstevel@tonic-gate 	uint64_t T1, T2;
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate #if	defined(__sparc)
3787c478bd9Sstevel@tonic-gate 	static const uint64_t sha512_consts[] = {
3797c478bd9Sstevel@tonic-gate 		SHA512_CONST_0, SHA512_CONST_1, SHA512_CONST_2,
3807c478bd9Sstevel@tonic-gate 		SHA512_CONST_3, SHA512_CONST_4, SHA512_CONST_5,
3817c478bd9Sstevel@tonic-gate 		SHA512_CONST_6, SHA512_CONST_7, SHA512_CONST_8,
3827c478bd9Sstevel@tonic-gate 		SHA512_CONST_9, SHA512_CONST_10, SHA512_CONST_11,
3837c478bd9Sstevel@tonic-gate 		SHA512_CONST_12, SHA512_CONST_13, SHA512_CONST_14,
3847c478bd9Sstevel@tonic-gate 		SHA512_CONST_15, SHA512_CONST_16, SHA512_CONST_17,
3857c478bd9Sstevel@tonic-gate 		SHA512_CONST_18, SHA512_CONST_19, SHA512_CONST_20,
3867c478bd9Sstevel@tonic-gate 		SHA512_CONST_21, SHA512_CONST_22, SHA512_CONST_23,
3877c478bd9Sstevel@tonic-gate 		SHA512_CONST_24, SHA512_CONST_25, SHA512_CONST_26,
3887c478bd9Sstevel@tonic-gate 		SHA512_CONST_27, SHA512_CONST_28, SHA512_CONST_29,
3897c478bd9Sstevel@tonic-gate 		SHA512_CONST_30, SHA512_CONST_31, SHA512_CONST_32,
3907c478bd9Sstevel@tonic-gate 		SHA512_CONST_33, SHA512_CONST_34, SHA512_CONST_35,
3917c478bd9Sstevel@tonic-gate 		SHA512_CONST_36, SHA512_CONST_37, SHA512_CONST_38,
3927c478bd9Sstevel@tonic-gate 		SHA512_CONST_39, SHA512_CONST_40, SHA512_CONST_41,
3937c478bd9Sstevel@tonic-gate 		SHA512_CONST_42, SHA512_CONST_43, SHA512_CONST_44,
3947c478bd9Sstevel@tonic-gate 		SHA512_CONST_45, SHA512_CONST_46, SHA512_CONST_47,
3957c478bd9Sstevel@tonic-gate 		SHA512_CONST_48, SHA512_CONST_49, SHA512_CONST_50,
3967c478bd9Sstevel@tonic-gate 		SHA512_CONST_51, SHA512_CONST_52, SHA512_CONST_53,
3977c478bd9Sstevel@tonic-gate 		SHA512_CONST_54, SHA512_CONST_55, SHA512_CONST_56,
3987c478bd9Sstevel@tonic-gate 		SHA512_CONST_57, SHA512_CONST_58, SHA512_CONST_59,
3997c478bd9Sstevel@tonic-gate 		SHA512_CONST_60, SHA512_CONST_61, SHA512_CONST_62,
4007c478bd9Sstevel@tonic-gate 		SHA512_CONST_63, SHA512_CONST_64, SHA512_CONST_65,
4017c478bd9Sstevel@tonic-gate 		SHA512_CONST_66, SHA512_CONST_67, SHA512_CONST_68,
4027c478bd9Sstevel@tonic-gate 		SHA512_CONST_69, SHA512_CONST_70, SHA512_CONST_71,
4037c478bd9Sstevel@tonic-gate 		SHA512_CONST_72, SHA512_CONST_73, SHA512_CONST_74,
4047c478bd9Sstevel@tonic-gate 		SHA512_CONST_75, SHA512_CONST_76, SHA512_CONST_77,
4057c478bd9Sstevel@tonic-gate 		SHA512_CONST_78, SHA512_CONST_79
4067c478bd9Sstevel@tonic-gate 	};
407*55553f71Sda73024 #endif	/* __sparc */
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	if ((uintptr_t)blk & 0x7) {		/* not 8-byte aligned? */
4117c478bd9Sstevel@tonic-gate 		bcopy(blk, ctx->buf_un.buf64,  sizeof (ctx->buf_un.buf64));
4127c478bd9Sstevel@tonic-gate 		blk = (uint8_t *)ctx->buf_un.buf64;
4137c478bd9Sstevel@tonic-gate 	}
4147c478bd9Sstevel@tonic-gate 
415734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
416f66d273dSizick 	w0 =  LOAD_BIG_64(blk + 8 * 0);
417f66d273dSizick 	SHA512ROUND(a, b, c, d, e, f, g, h, 0, w0);
418734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
419f66d273dSizick 	w1 =  LOAD_BIG_64(blk + 8 * 1);
420f66d273dSizick 	SHA512ROUND(h, a, b, c, d, e, f, g, 1, w1);
421734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
422f66d273dSizick 	w2 =  LOAD_BIG_64(blk + 8 * 2);
423f66d273dSizick 	SHA512ROUND(g, h, a, b, c, d, e, f, 2, w2);
424734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
425f66d273dSizick 	w3 =  LOAD_BIG_64(blk + 8 * 3);
426f66d273dSizick 	SHA512ROUND(f, g, h, a, b, c, d, e, 3, w3);
427734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
428f66d273dSizick 	w4 =  LOAD_BIG_64(blk + 8 * 4);
429f66d273dSizick 	SHA512ROUND(e, f, g, h, a, b, c, d, 4, w4);
430734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
431f66d273dSizick 	w5 =  LOAD_BIG_64(blk + 8 * 5);
432f66d273dSizick 	SHA512ROUND(d, e, f, g, h, a, b, c, 5, w5);
433734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
434f66d273dSizick 	w6 =  LOAD_BIG_64(blk + 8 * 6);
435f66d273dSizick 	SHA512ROUND(c, d, e, f, g, h, a, b, 6, w6);
436734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
437f66d273dSizick 	w7 =  LOAD_BIG_64(blk + 8 * 7);
438f66d273dSizick 	SHA512ROUND(b, c, d, e, f, g, h, a, 7, w7);
439734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
440f66d273dSizick 	w8 =  LOAD_BIG_64(blk + 8 * 8);
441f66d273dSizick 	SHA512ROUND(a, b, c, d, e, f, g, h, 8, w8);
442734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
443f66d273dSizick 	w9 =  LOAD_BIG_64(blk + 8 * 9);
444f66d273dSizick 	SHA512ROUND(h, a, b, c, d, e, f, g, 9, w9);
445734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
446f66d273dSizick 	w10 =  LOAD_BIG_64(blk + 8 * 10);
447f66d273dSizick 	SHA512ROUND(g, h, a, b, c, d, e, f, 10, w10);
448734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
449f66d273dSizick 	w11 =  LOAD_BIG_64(blk + 8 * 11);
450f66d273dSizick 	SHA512ROUND(f, g, h, a, b, c, d, e, 11, w11);
451734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
452f66d273dSizick 	w12 =  LOAD_BIG_64(blk + 8 * 12);
453f66d273dSizick 	SHA512ROUND(e, f, g, h, a, b, c, d, 12, w12);
454734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
455f66d273dSizick 	w13 =  LOAD_BIG_64(blk + 8 * 13);
456f66d273dSizick 	SHA512ROUND(d, e, f, g, h, a, b, c, 13, w13);
457734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
458f66d273dSizick 	w14 =  LOAD_BIG_64(blk + 8 * 14);
459f66d273dSizick 	SHA512ROUND(c, d, e, f, g, h, a, b, 14, w14);
460734b6a94Sdarrenm 	/* LINTED E_BAD_PTR_CAST_ALIGN */
461f66d273dSizick 	w15 =  LOAD_BIG_64(blk + 8 * 15);
462f66d273dSizick 	SHA512ROUND(b, c, d, e, f, g, h, a, 15, w15);
463f66d273dSizick 
4647c478bd9Sstevel@tonic-gate 	w0 = SIGMA1(w14) + w9 + SIGMA0(w1) + w0;
4657c478bd9Sstevel@tonic-gate 	SHA512ROUND(a, b, c, d, e, f, g, h, 16, w0);
4667c478bd9Sstevel@tonic-gate 	w1 = SIGMA1(w15) + w10 + SIGMA0(w2) + w1;
4677c478bd9Sstevel@tonic-gate 	SHA512ROUND(h, a, b, c, d, e, f, g, 17, w1);
4687c478bd9Sstevel@tonic-gate 	w2 = SIGMA1(w0) + w11 + SIGMA0(w3) + w2;
4697c478bd9Sstevel@tonic-gate 	SHA512ROUND(g, h, a, b, c, d, e, f, 18, w2);
4707c478bd9Sstevel@tonic-gate 	w3 = SIGMA1(w1) + w12 + SIGMA0(w4) + w3;
4717c478bd9Sstevel@tonic-gate 	SHA512ROUND(f, g, h, a, b, c, d, e, 19, w3);
4727c478bd9Sstevel@tonic-gate 	w4 = SIGMA1(w2) + w13 + SIGMA0(w5) + w4;
4737c478bd9Sstevel@tonic-gate 	SHA512ROUND(e, f, g, h, a, b, c, d, 20, w4);
4747c478bd9Sstevel@tonic-gate 	w5 = SIGMA1(w3) + w14 + SIGMA0(w6) + w5;
4757c478bd9Sstevel@tonic-gate 	SHA512ROUND(d, e, f, g, h, a, b, c, 21, w5);
4767c478bd9Sstevel@tonic-gate 	w6 = SIGMA1(w4) + w15 + SIGMA0(w7) + w6;
4777c478bd9Sstevel@tonic-gate 	SHA512ROUND(c, d, e, f, g, h, a, b, 22, w6);
4787c478bd9Sstevel@tonic-gate 	w7 = SIGMA1(w5) + w0 + SIGMA0(w8) + w7;
4797c478bd9Sstevel@tonic-gate 	SHA512ROUND(b, c, d, e, f, g, h, a, 23, w7);
4807c478bd9Sstevel@tonic-gate 	w8 = SIGMA1(w6) + w1 + SIGMA0(w9) + w8;
4817c478bd9Sstevel@tonic-gate 	SHA512ROUND(a, b, c, d, e, f, g, h, 24, w8);
4827c478bd9Sstevel@tonic-gate 	w9 = SIGMA1(w7) + w2 + SIGMA0(w10) + w9;
4837c478bd9Sstevel@tonic-gate 	SHA512ROUND(h, a, b, c, d, e, f, g, 25, w9);
4847c478bd9Sstevel@tonic-gate 	w10 = SIGMA1(w8) + w3 + SIGMA0(w11) + w10;
4857c478bd9Sstevel@tonic-gate 	SHA512ROUND(g, h, a, b, c, d, e, f, 26, w10);
4867c478bd9Sstevel@tonic-gate 	w11 = SIGMA1(w9) + w4 + SIGMA0(w12) + w11;
4877c478bd9Sstevel@tonic-gate 	SHA512ROUND(f, g, h, a, b, c, d, e, 27, w11);
4887c478bd9Sstevel@tonic-gate 	w12 = SIGMA1(w10) + w5 + SIGMA0(w13) + w12;
4897c478bd9Sstevel@tonic-gate 	SHA512ROUND(e, f, g, h, a, b, c, d, 28, w12);
4907c478bd9Sstevel@tonic-gate 	w13 = SIGMA1(w11) + w6 + SIGMA0(w14) + w13;
4917c478bd9Sstevel@tonic-gate 	SHA512ROUND(d, e, f, g, h, a, b, c, 29, w13);
4927c478bd9Sstevel@tonic-gate 	w14 = SIGMA1(w12) + w7 + SIGMA0(w15) + w14;
4937c478bd9Sstevel@tonic-gate 	SHA512ROUND(c, d, e, f, g, h, a, b, 30, w14);
4947c478bd9Sstevel@tonic-gate 	w15 = SIGMA1(w13) + w8 + SIGMA0(w0) + w15;
4957c478bd9Sstevel@tonic-gate 	SHA512ROUND(b, c, d, e, f, g, h, a, 31, w15);
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	w0 = SIGMA1(w14) + w9 + SIGMA0(w1) + w0;
4987c478bd9Sstevel@tonic-gate 	SHA512ROUND(a, b, c, d, e, f, g, h, 32, w0);
4997c478bd9Sstevel@tonic-gate 	w1 = SIGMA1(w15) + w10 + SIGMA0(w2) + w1;
5007c478bd9Sstevel@tonic-gate 	SHA512ROUND(h, a, b, c, d, e, f, g, 33, w1);
5017c478bd9Sstevel@tonic-gate 	w2 = SIGMA1(w0) + w11 + SIGMA0(w3) + w2;
5027c478bd9Sstevel@tonic-gate 	SHA512ROUND(g, h, a, b, c, d, e, f, 34, w2);
5037c478bd9Sstevel@tonic-gate 	w3 = SIGMA1(w1) + w12 + SIGMA0(w4) + w3;
5047c478bd9Sstevel@tonic-gate 	SHA512ROUND(f, g, h, a, b, c, d, e, 35, w3);
5057c478bd9Sstevel@tonic-gate 	w4 = SIGMA1(w2) + w13 + SIGMA0(w5) + w4;
5067c478bd9Sstevel@tonic-gate 	SHA512ROUND(e, f, g, h, a, b, c, d, 36, w4);
5077c478bd9Sstevel@tonic-gate 	w5 = SIGMA1(w3) + w14 + SIGMA0(w6) + w5;
5087c478bd9Sstevel@tonic-gate 	SHA512ROUND(d, e, f, g, h, a, b, c, 37, w5);
5097c478bd9Sstevel@tonic-gate 	w6 = SIGMA1(w4) + w15 + SIGMA0(w7) + w6;
5107c478bd9Sstevel@tonic-gate 	SHA512ROUND(c, d, e, f, g, h, a, b, 38, w6);
5117c478bd9Sstevel@tonic-gate 	w7 = SIGMA1(w5) + w0 + SIGMA0(w8) + w7;
5127c478bd9Sstevel@tonic-gate 	SHA512ROUND(b, c, d, e, f, g, h, a, 39, w7);
5137c478bd9Sstevel@tonic-gate 	w8 = SIGMA1(w6) + w1 + SIGMA0(w9) + w8;
5147c478bd9Sstevel@tonic-gate 	SHA512ROUND(a, b, c, d, e, f, g, h, 40, w8);
5157c478bd9Sstevel@tonic-gate 	w9 = SIGMA1(w7) + w2 + SIGMA0(w10) + w9;
5167c478bd9Sstevel@tonic-gate 	SHA512ROUND(h, a, b, c, d, e, f, g, 41, w9);
5177c478bd9Sstevel@tonic-gate 	w10 = SIGMA1(w8) + w3 + SIGMA0(w11) + w10;
5187c478bd9Sstevel@tonic-gate 	SHA512ROUND(g, h, a, b, c, d, e, f, 42, w10);
5197c478bd9Sstevel@tonic-gate 	w11 = SIGMA1(w9) + w4 + SIGMA0(w12) + w11;
5207c478bd9Sstevel@tonic-gate 	SHA512ROUND(f, g, h, a, b, c, d, e, 43, w11);
5217c478bd9Sstevel@tonic-gate 	w12 = SIGMA1(w10) + w5 + SIGMA0(w13) + w12;
5227c478bd9Sstevel@tonic-gate 	SHA512ROUND(e, f, g, h, a, b, c, d, 44, w12);
5237c478bd9Sstevel@tonic-gate 	w13 = SIGMA1(w11) + w6 + SIGMA0(w14) + w13;
5247c478bd9Sstevel@tonic-gate 	SHA512ROUND(d, e, f, g, h, a, b, c, 45, w13);
5257c478bd9Sstevel@tonic-gate 	w14 = SIGMA1(w12) + w7 + SIGMA0(w15) + w14;
5267c478bd9Sstevel@tonic-gate 	SHA512ROUND(c, d, e, f, g, h, a, b, 46, w14);
5277c478bd9Sstevel@tonic-gate 	w15 = SIGMA1(w13) + w8 + SIGMA0(w0) + w15;
5287c478bd9Sstevel@tonic-gate 	SHA512ROUND(b, c, d, e, f, g, h, a, 47, w15);
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	w0 = SIGMA1(w14) + w9 + SIGMA0(w1) + w0;
5317c478bd9Sstevel@tonic-gate 	SHA512ROUND(a, b, c, d, e, f, g, h, 48, w0);
5327c478bd9Sstevel@tonic-gate 	w1 = SIGMA1(w15) + w10 + SIGMA0(w2) + w1;
5337c478bd9Sstevel@tonic-gate 	SHA512ROUND(h, a, b, c, d, e, f, g, 49, w1);
5347c478bd9Sstevel@tonic-gate 	w2 = SIGMA1(w0) + w11 + SIGMA0(w3) + w2;
5357c478bd9Sstevel@tonic-gate 	SHA512ROUND(g, h, a, b, c, d, e, f, 50, w2);
5367c478bd9Sstevel@tonic-gate 	w3 = SIGMA1(w1) + w12 + SIGMA0(w4) + w3;
5377c478bd9Sstevel@tonic-gate 	SHA512ROUND(f, g, h, a, b, c, d, e, 51, w3);
5387c478bd9Sstevel@tonic-gate 	w4 = SIGMA1(w2) + w13 + SIGMA0(w5) + w4;
5397c478bd9Sstevel@tonic-gate 	SHA512ROUND(e, f, g, h, a, b, c, d, 52, w4);
5407c478bd9Sstevel@tonic-gate 	w5 = SIGMA1(w3) + w14 + SIGMA0(w6) + w5;
5417c478bd9Sstevel@tonic-gate 	SHA512ROUND(d, e, f, g, h, a, b, c, 53, w5);
5427c478bd9Sstevel@tonic-gate 	w6 = SIGMA1(w4) + w15 + SIGMA0(w7) + w6;
5437c478bd9Sstevel@tonic-gate 	SHA512ROUND(c, d, e, f, g, h, a, b, 54, w6);
5447c478bd9Sstevel@tonic-gate 	w7 = SIGMA1(w5) + w0 + SIGMA0(w8) + w7;
5457c478bd9Sstevel@tonic-gate 	SHA512ROUND(b, c, d, e, f, g, h, a, 55, w7);
5467c478bd9Sstevel@tonic-gate 	w8 = SIGMA1(w6) + w1 + SIGMA0(w9) + w8;
5477c478bd9Sstevel@tonic-gate 	SHA512ROUND(a, b, c, d, e, f, g, h, 56, w8);
5487c478bd9Sstevel@tonic-gate 	w9 = SIGMA1(w7) + w2 + SIGMA0(w10) + w9;
5497c478bd9Sstevel@tonic-gate 	SHA512ROUND(h, a, b, c, d, e, f, g, 57, w9);
5507c478bd9Sstevel@tonic-gate 	w10 = SIGMA1(w8) + w3 + SIGMA0(w11) + w10;
5517c478bd9Sstevel@tonic-gate 	SHA512ROUND(g, h, a, b, c, d, e, f, 58, w10);
5527c478bd9Sstevel@tonic-gate 	w11 = SIGMA1(w9) + w4 + SIGMA0(w12) + w11;
5537c478bd9Sstevel@tonic-gate 	SHA512ROUND(f, g, h, a, b, c, d, e, 59, w11);
5547c478bd9Sstevel@tonic-gate 	w12 = SIGMA1(w10) + w5 + SIGMA0(w13) + w12;
5557c478bd9Sstevel@tonic-gate 	SHA512ROUND(e, f, g, h, a, b, c, d, 60, w12);
5567c478bd9Sstevel@tonic-gate 	w13 = SIGMA1(w11) + w6 + SIGMA0(w14) + w13;
5577c478bd9Sstevel@tonic-gate 	SHA512ROUND(d, e, f, g, h, a, b, c, 61, w13);
5587c478bd9Sstevel@tonic-gate 	w14 = SIGMA1(w12) + w7 + SIGMA0(w15) + w14;
5597c478bd9Sstevel@tonic-gate 	SHA512ROUND(c, d, e, f, g, h, a, b, 62, w14);
5607c478bd9Sstevel@tonic-gate 	w15 = SIGMA1(w13) + w8 + SIGMA0(w0) + w15;
5617c478bd9Sstevel@tonic-gate 	SHA512ROUND(b, c, d, e, f, g, h, a, 63, w15);
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	w0 = SIGMA1(w14) + w9 + SIGMA0(w1) + w0;
5647c478bd9Sstevel@tonic-gate 	SHA512ROUND(a, b, c, d, e, f, g, h, 64, w0);
5657c478bd9Sstevel@tonic-gate 	w1 = SIGMA1(w15) + w10 + SIGMA0(w2) + w1;
5667c478bd9Sstevel@tonic-gate 	SHA512ROUND(h, a, b, c, d, e, f, g, 65, w1);
5677c478bd9Sstevel@tonic-gate 	w2 = SIGMA1(w0) + w11 + SIGMA0(w3) + w2;
5687c478bd9Sstevel@tonic-gate 	SHA512ROUND(g, h, a, b, c, d, e, f, 66, w2);
5697c478bd9Sstevel@tonic-gate 	w3 = SIGMA1(w1) + w12 + SIGMA0(w4) + w3;
5707c478bd9Sstevel@tonic-gate 	SHA512ROUND(f, g, h, a, b, c, d, e, 67, w3);
5717c478bd9Sstevel@tonic-gate 	w4 = SIGMA1(w2) + w13 + SIGMA0(w5) + w4;
5727c478bd9Sstevel@tonic-gate 	SHA512ROUND(e, f, g, h, a, b, c, d, 68, w4);
5737c478bd9Sstevel@tonic-gate 	w5 = SIGMA1(w3) + w14 + SIGMA0(w6) + w5;
5747c478bd9Sstevel@tonic-gate 	SHA512ROUND(d, e, f, g, h, a, b, c, 69, w5);
5757c478bd9Sstevel@tonic-gate 	w6 = SIGMA1(w4) + w15 + SIGMA0(w7) + w6;
5767c478bd9Sstevel@tonic-gate 	SHA512ROUND(c, d, e, f, g, h, a, b, 70, w6);
5777c478bd9Sstevel@tonic-gate 	w7 = SIGMA1(w5) + w0 + SIGMA0(w8) + w7;
5787c478bd9Sstevel@tonic-gate 	SHA512ROUND(b, c, d, e, f, g, h, a, 71, w7);
5797c478bd9Sstevel@tonic-gate 	w8 = SIGMA1(w6) + w1 + SIGMA0(w9) + w8;
5807c478bd9Sstevel@tonic-gate 	SHA512ROUND(a, b, c, d, e, f, g, h, 72, w8);
5817c478bd9Sstevel@tonic-gate 	w9 = SIGMA1(w7) + w2 + SIGMA0(w10) + w9;
5827c478bd9Sstevel@tonic-gate 	SHA512ROUND(h, a, b, c, d, e, f, g, 73, w9);
5837c478bd9Sstevel@tonic-gate 	w10 = SIGMA1(w8) + w3 + SIGMA0(w11) + w10;
5847c478bd9Sstevel@tonic-gate 	SHA512ROUND(g, h, a, b, c, d, e, f, 74, w10);
5857c478bd9Sstevel@tonic-gate 	w11 = SIGMA1(w9) + w4 + SIGMA0(w12) + w11;
5867c478bd9Sstevel@tonic-gate 	SHA512ROUND(f, g, h, a, b, c, d, e, 75, w11);
5877c478bd9Sstevel@tonic-gate 	w12 = SIGMA1(w10) + w5 + SIGMA0(w13) + w12;
5887c478bd9Sstevel@tonic-gate 	SHA512ROUND(e, f, g, h, a, b, c, d, 76, w12);
5897c478bd9Sstevel@tonic-gate 	w13 = SIGMA1(w11) + w6 + SIGMA0(w14) + w13;
5907c478bd9Sstevel@tonic-gate 	SHA512ROUND(d, e, f, g, h, a, b, c, 77, w13);
5917c478bd9Sstevel@tonic-gate 	w14 = SIGMA1(w12) + w7 + SIGMA0(w15) + w14;
5927c478bd9Sstevel@tonic-gate 	SHA512ROUND(c, d, e, f, g, h, a, b, 78, w14);
5937c478bd9Sstevel@tonic-gate 	w15 = SIGMA1(w13) + w8 + SIGMA0(w0) + w15;
5947c478bd9Sstevel@tonic-gate 	SHA512ROUND(b, c, d, e, f, g, h, a, 79, w15);
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	ctx->state.s64[0] += a;
5977c478bd9Sstevel@tonic-gate 	ctx->state.s64[1] += b;
5987c478bd9Sstevel@tonic-gate 	ctx->state.s64[2] += c;
5997c478bd9Sstevel@tonic-gate 	ctx->state.s64[3] += d;
6007c478bd9Sstevel@tonic-gate 	ctx->state.s64[4] += e;
6017c478bd9Sstevel@tonic-gate 	ctx->state.s64[5] += f;
6027c478bd9Sstevel@tonic-gate 	ctx->state.s64[6] += g;
6037c478bd9Sstevel@tonic-gate 	ctx->state.s64[7] += h;
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate }
606*55553f71Sda73024 #endif	/* !__amd64 */
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate /*
6107c478bd9Sstevel@tonic-gate  * Encode()
6117c478bd9Sstevel@tonic-gate  *
6127c478bd9Sstevel@tonic-gate  * purpose: to convert a list of numbers from little endian to big endian
6137c478bd9Sstevel@tonic-gate  *   input: uint8_t *	: place to store the converted big endian numbers
6147c478bd9Sstevel@tonic-gate  *	    uint32_t *	: place to get numbers to convert from
6157c478bd9Sstevel@tonic-gate  *          size_t	: the length of the input in bytes
6167c478bd9Sstevel@tonic-gate  *  output: void
6177c478bd9Sstevel@tonic-gate  */
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate static void
620734b6a94Sdarrenm Encode(uint8_t *_RESTRICT_KYWD output, uint32_t *_RESTRICT_KYWD input,
621734b6a94Sdarrenm     size_t len)
6227c478bd9Sstevel@tonic-gate {
6237c478bd9Sstevel@tonic-gate 	size_t		i, j;
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate #if	defined(__sparc)
6267c478bd9Sstevel@tonic-gate 	if (IS_P2ALIGNED(output, sizeof (uint32_t))) {
6277c478bd9Sstevel@tonic-gate 		for (i = 0, j = 0; j < len; i++, j += 4) {
6287c478bd9Sstevel@tonic-gate 			/* LINTED: pointer alignment */
6297c478bd9Sstevel@tonic-gate 			*((uint32_t *)(output + j)) = input[i];
6307c478bd9Sstevel@tonic-gate 		}
6317c478bd9Sstevel@tonic-gate 	} else {
6327c478bd9Sstevel@tonic-gate #endif	/* little endian -- will work on big endian, but slowly */
6337c478bd9Sstevel@tonic-gate 		for (i = 0, j = 0; j < len; i++, j += 4) {
6347c478bd9Sstevel@tonic-gate 			output[j]	= (input[i] >> 24) & 0xff;
6357c478bd9Sstevel@tonic-gate 			output[j + 1]	= (input[i] >> 16) & 0xff;
6367c478bd9Sstevel@tonic-gate 			output[j + 2]	= (input[i] >>  8) & 0xff;
6377c478bd9Sstevel@tonic-gate 			output[j + 3]	= input[i] & 0xff;
6387c478bd9Sstevel@tonic-gate 		}
6397c478bd9Sstevel@tonic-gate #if	defined(__sparc)
6407c478bd9Sstevel@tonic-gate 	}
6417c478bd9Sstevel@tonic-gate #endif
6427c478bd9Sstevel@tonic-gate }
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate static void
645734b6a94Sdarrenm Encode64(uint8_t *_RESTRICT_KYWD output, uint64_t *_RESTRICT_KYWD input,
646734b6a94Sdarrenm     size_t len)
6477c478bd9Sstevel@tonic-gate {
6487c478bd9Sstevel@tonic-gate 	size_t		i, j;
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate #if	defined(__sparc)
6517c478bd9Sstevel@tonic-gate 	if (IS_P2ALIGNED(output, sizeof (uint64_t))) {
6527c478bd9Sstevel@tonic-gate 		for (i = 0, j = 0; j < len; i++, j += 8) {
6537c478bd9Sstevel@tonic-gate 			/* LINTED: pointer alignment */
6547c478bd9Sstevel@tonic-gate 			*((uint64_t *)(output + j)) = input[i];
6557c478bd9Sstevel@tonic-gate 		}
6567c478bd9Sstevel@tonic-gate 	} else {
6577c478bd9Sstevel@tonic-gate #endif	/* little endian -- will work on big endian, but slowly */
6587c478bd9Sstevel@tonic-gate 		for (i = 0, j = 0; j < len; i++, j += 8) {
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 			output[j]	= (input[i] >> 56) & 0xff;
6617c478bd9Sstevel@tonic-gate 			output[j + 1]	= (input[i] >> 48) & 0xff;
6627c478bd9Sstevel@tonic-gate 			output[j + 2]	= (input[i] >> 40) & 0xff;
6637c478bd9Sstevel@tonic-gate 			output[j + 3]	= (input[i] >> 32) & 0xff;
6647c478bd9Sstevel@tonic-gate 			output[j + 4]	= (input[i] >> 24) & 0xff;
6657c478bd9Sstevel@tonic-gate 			output[j + 5]	= (input[i] >> 16) & 0xff;
6667c478bd9Sstevel@tonic-gate 			output[j + 6]	= (input[i] >>  8) & 0xff;
6677c478bd9Sstevel@tonic-gate 			output[j + 7]	= input[i] & 0xff;
6687c478bd9Sstevel@tonic-gate 		}
6697c478bd9Sstevel@tonic-gate #if	defined(__sparc)
6707c478bd9Sstevel@tonic-gate 	}
6717c478bd9Sstevel@tonic-gate #endif
6727c478bd9Sstevel@tonic-gate }
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate void
6767c478bd9Sstevel@tonic-gate SHA2Init(uint64_t mech, SHA2_CTX *ctx)
6777c478bd9Sstevel@tonic-gate {
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	switch (mech) {
6807c478bd9Sstevel@tonic-gate 	case SHA256_MECH_INFO_TYPE:
6817c478bd9Sstevel@tonic-gate 	case SHA256_HMAC_MECH_INFO_TYPE:
6827c478bd9Sstevel@tonic-gate 	case SHA256_HMAC_GEN_MECH_INFO_TYPE:
6837c478bd9Sstevel@tonic-gate 		ctx->state.s32[0] = 0x6a09e667U;
6847c478bd9Sstevel@tonic-gate 		ctx->state.s32[1] = 0xbb67ae85U;
6857c478bd9Sstevel@tonic-gate 		ctx->state.s32[2] = 0x3c6ef372U;
6867c478bd9Sstevel@tonic-gate 		ctx->state.s32[3] = 0xa54ff53aU;
6877c478bd9Sstevel@tonic-gate 		ctx->state.s32[4] = 0x510e527fU;
6887c478bd9Sstevel@tonic-gate 		ctx->state.s32[5] = 0x9b05688cU;
6897c478bd9Sstevel@tonic-gate 		ctx->state.s32[6] = 0x1f83d9abU;
6907c478bd9Sstevel@tonic-gate 		ctx->state.s32[7] = 0x5be0cd19U;
6917c478bd9Sstevel@tonic-gate 		break;
6927c478bd9Sstevel@tonic-gate 	case SHA384_MECH_INFO_TYPE:
6937c478bd9Sstevel@tonic-gate 	case SHA384_HMAC_MECH_INFO_TYPE:
6947c478bd9Sstevel@tonic-gate 	case SHA384_HMAC_GEN_MECH_INFO_TYPE:
6957c478bd9Sstevel@tonic-gate 		ctx->state.s64[0] = 0xcbbb9d5dc1059ed8ULL;
6967c478bd9Sstevel@tonic-gate 		ctx->state.s64[1] = 0x629a292a367cd507ULL;
6977c478bd9Sstevel@tonic-gate 		ctx->state.s64[2] = 0x9159015a3070dd17ULL;
6987c478bd9Sstevel@tonic-gate 		ctx->state.s64[3] = 0x152fecd8f70e5939ULL;
6997c478bd9Sstevel@tonic-gate 		ctx->state.s64[4] = 0x67332667ffc00b31ULL;
7007c478bd9Sstevel@tonic-gate 		ctx->state.s64[5] = 0x8eb44a8768581511ULL;
7017c478bd9Sstevel@tonic-gate 		ctx->state.s64[6] = 0xdb0c2e0d64f98fa7ULL;
7027c478bd9Sstevel@tonic-gate 		ctx->state.s64[7] = 0x47b5481dbefa4fa4ULL;
7037c478bd9Sstevel@tonic-gate 		break;
7047c478bd9Sstevel@tonic-gate 	case SHA512_MECH_INFO_TYPE:
7057c478bd9Sstevel@tonic-gate 	case SHA512_HMAC_MECH_INFO_TYPE:
7067c478bd9Sstevel@tonic-gate 	case SHA512_HMAC_GEN_MECH_INFO_TYPE:
7077c478bd9Sstevel@tonic-gate 		ctx->state.s64[0] = 0x6a09e667f3bcc908ULL;
7087c478bd9Sstevel@tonic-gate 		ctx->state.s64[1] = 0xbb67ae8584caa73bULL;
7097c478bd9Sstevel@tonic-gate 		ctx->state.s64[2] = 0x3c6ef372fe94f82bULL;
7107c478bd9Sstevel@tonic-gate 		ctx->state.s64[3] = 0xa54ff53a5f1d36f1ULL;
7117c478bd9Sstevel@tonic-gate 		ctx->state.s64[4] = 0x510e527fade682d1ULL;
7127c478bd9Sstevel@tonic-gate 		ctx->state.s64[5] = 0x9b05688c2b3e6c1fULL;
7137c478bd9Sstevel@tonic-gate 		ctx->state.s64[6] = 0x1f83d9abfb41bd6bULL;
7147c478bd9Sstevel@tonic-gate 		ctx->state.s64[7] = 0x5be0cd19137e2179ULL;
7157c478bd9Sstevel@tonic-gate 		break;
7167c478bd9Sstevel@tonic-gate #ifdef _KERNEL
7177c478bd9Sstevel@tonic-gate 	default:
718734b6a94Sdarrenm 		cmn_err(CE_PANIC, "sha2_init: "
7197c478bd9Sstevel@tonic-gate 		    "failed to find a supported algorithm: 0x%x",
7207c478bd9Sstevel@tonic-gate 		    (uint32_t)mech);
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
7237c478bd9Sstevel@tonic-gate 	}
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	ctx->algotype = mech;
7267c478bd9Sstevel@tonic-gate 	ctx->count.c64[0] = ctx->count.c64[1] = 0;
7277c478bd9Sstevel@tonic-gate }
7287c478bd9Sstevel@tonic-gate 
729734b6a94Sdarrenm #ifndef _KERNEL
730734b6a94Sdarrenm 
731734b6a94Sdarrenm #pragma inline(SHA256Init, SHA384Init, SHA512Init)
732734b6a94Sdarrenm void
733734b6a94Sdarrenm SHA256Init(SHA256_CTX *ctx)
734734b6a94Sdarrenm {
735734b6a94Sdarrenm 	SHA2Init(SHA256, ctx);
736734b6a94Sdarrenm }
737734b6a94Sdarrenm 
738734b6a94Sdarrenm void
739734b6a94Sdarrenm SHA384Init(SHA384_CTX *ctx)
740734b6a94Sdarrenm {
741734b6a94Sdarrenm 	SHA2Init(SHA384, ctx);
742734b6a94Sdarrenm }
743734b6a94Sdarrenm 
744734b6a94Sdarrenm void
745734b6a94Sdarrenm SHA512Init(SHA512_CTX *ctx)
746734b6a94Sdarrenm {
747734b6a94Sdarrenm 	SHA2Init(SHA512, ctx);
748734b6a94Sdarrenm }
749734b6a94Sdarrenm 
750734b6a94Sdarrenm #endif /* _KERNEL */
751734b6a94Sdarrenm 
7527c478bd9Sstevel@tonic-gate /*
7537c478bd9Sstevel@tonic-gate  * SHA2Update()
7547c478bd9Sstevel@tonic-gate  *
7557c478bd9Sstevel@tonic-gate  * purpose: continues an sha2 digest operation, using the message block
7567c478bd9Sstevel@tonic-gate  *          to update the context.
7577c478bd9Sstevel@tonic-gate  *   input: SHA2_CTX *	: the context to update
758734b6a94Sdarrenm  *          void *	: the message block
759*55553f71Sda73024  *          size_t      : the length of the message block, in bytes
7607c478bd9Sstevel@tonic-gate  *  output: void
7617c478bd9Sstevel@tonic-gate  */
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate void
764734b6a94Sdarrenm SHA2Update(SHA2_CTX *ctx, const void *inptr, size_t input_len)
7657c478bd9Sstevel@tonic-gate {
7667c478bd9Sstevel@tonic-gate 	uint32_t	i, buf_index, buf_len, buf_limit;
767734b6a94Sdarrenm 	const uint8_t	*input = inptr;
768*55553f71Sda73024 	uint32_t	algotype = ctx->algotype;
769*55553f71Sda73024 #if defined(__amd64)
770*55553f71Sda73024 	uint32_t	block_count;
771*55553f71Sda73024 #endif	/* !__amd64 */
772*55553f71Sda73024 
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 	/* check for noop */
7757c478bd9Sstevel@tonic-gate 	if (input_len == 0)
7767c478bd9Sstevel@tonic-gate 		return;
7777c478bd9Sstevel@tonic-gate 
778*55553f71Sda73024 	if (algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE) {
7797c478bd9Sstevel@tonic-gate 		buf_limit = 64;
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 		/* compute number of bytes mod 64 */
7827c478bd9Sstevel@tonic-gate 		buf_index = (ctx->count.c32[1] >> 3) & 0x3F;
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 		/* update number of bits */
7857c478bd9Sstevel@tonic-gate 		if ((ctx->count.c32[1] += (input_len << 3)) < (input_len << 3))
7867c478bd9Sstevel@tonic-gate 			ctx->count.c32[0]++;
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 		ctx->count.c32[0] += (input_len >> 29);
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 	} else {
7917c478bd9Sstevel@tonic-gate 		buf_limit = 128;
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 		/* compute number of bytes mod 128 */
7947c478bd9Sstevel@tonic-gate 		buf_index = (ctx->count.c64[1] >> 3) & 0x7F;
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 		/* update number of bits */
7977c478bd9Sstevel@tonic-gate 		if ((ctx->count.c64[1] += (input_len << 3)) < (input_len << 3))
7987c478bd9Sstevel@tonic-gate 			ctx->count.c64[0]++;
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 		ctx->count.c64[0] += (input_len >> 29);
8017c478bd9Sstevel@tonic-gate 	}
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 	buf_len = buf_limit - buf_index;
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	/* transform as many times as possible */
8067c478bd9Sstevel@tonic-gate 	i = 0;
8077c478bd9Sstevel@tonic-gate 	if (input_len >= buf_len) {
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 		/*
8107c478bd9Sstevel@tonic-gate 		 * general optimization:
8117c478bd9Sstevel@tonic-gate 		 *
8127c478bd9Sstevel@tonic-gate 		 * only do initial bcopy() and SHA2Transform() if
8137c478bd9Sstevel@tonic-gate 		 * buf_index != 0.  if buf_index == 0, we're just
8147c478bd9Sstevel@tonic-gate 		 * wasting our time doing the bcopy() since there
8157c478bd9Sstevel@tonic-gate 		 * wasn't any data left over from a previous call to
8167c478bd9Sstevel@tonic-gate 		 * SHA2Update().
8177c478bd9Sstevel@tonic-gate 		 */
8187c478bd9Sstevel@tonic-gate 		if (buf_index) {
8197c478bd9Sstevel@tonic-gate 			bcopy(input, &ctx->buf_un.buf8[buf_index], buf_len);
820*55553f71Sda73024 			if (algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE)
8217c478bd9Sstevel@tonic-gate 				SHA256Transform(ctx, ctx->buf_un.buf8);
8227c478bd9Sstevel@tonic-gate 			else
8237c478bd9Sstevel@tonic-gate 				SHA512Transform(ctx, ctx->buf_un.buf8);
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 			i = buf_len;
8267c478bd9Sstevel@tonic-gate 		}
8277c478bd9Sstevel@tonic-gate 
828*55553f71Sda73024 #if !defined(__amd64)
829*55553f71Sda73024 		if (algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE) {
8307c478bd9Sstevel@tonic-gate 			for (; i + buf_limit - 1 < input_len; i += buf_limit) {
8317c478bd9Sstevel@tonic-gate 				SHA256Transform(ctx, &input[i]);
832*55553f71Sda73024 			}
833*55553f71Sda73024 		} else {
834*55553f71Sda73024 			for (; i + buf_limit - 1 < input_len; i += buf_limit) {
8357c478bd9Sstevel@tonic-gate 				SHA512Transform(ctx, &input[i]);
8367c478bd9Sstevel@tonic-gate 			}
837*55553f71Sda73024 		}
838*55553f71Sda73024 
839*55553f71Sda73024 #else
840*55553f71Sda73024 		if (algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE) {
841*55553f71Sda73024 			block_count = (input_len - i) >> 6;
842*55553f71Sda73024 			if (block_count > 0) {
843*55553f71Sda73024 				SHA256TransformBlocks(ctx, &input[i],
844*55553f71Sda73024 				    block_count);
845*55553f71Sda73024 				i += block_count << 6;
846*55553f71Sda73024 			}
847*55553f71Sda73024 		} else {
848*55553f71Sda73024 			block_count = (input_len - i) >> 7;
849*55553f71Sda73024 			if (block_count > 0) {
850*55553f71Sda73024 				SHA512TransformBlocks(ctx, &input[i],
851*55553f71Sda73024 				    block_count);
852*55553f71Sda73024 				i += block_count << 7;
853*55553f71Sda73024 			}
854*55553f71Sda73024 		}
855*55553f71Sda73024 #endif	/* !__amd64 */
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 		/*
8587c478bd9Sstevel@tonic-gate 		 * general optimization:
8597c478bd9Sstevel@tonic-gate 		 *
8607c478bd9Sstevel@tonic-gate 		 * if i and input_len are the same, return now instead
8617c478bd9Sstevel@tonic-gate 		 * of calling bcopy(), since the bcopy() in this case
862*55553f71Sda73024 		 * will be an expensive noop.
8637c478bd9Sstevel@tonic-gate 		 */
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 		if (input_len == i)
8667c478bd9Sstevel@tonic-gate 			return;
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 		buf_index = 0;
8697c478bd9Sstevel@tonic-gate 	}
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 	/* buffer remaining input */
8727c478bd9Sstevel@tonic-gate 	bcopy(&input[i], &ctx->buf_un.buf8[buf_index], input_len - i);
8737c478bd9Sstevel@tonic-gate }
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate /*
8777c478bd9Sstevel@tonic-gate  * SHA2Final()
8787c478bd9Sstevel@tonic-gate  *
8797c478bd9Sstevel@tonic-gate  * purpose: ends an sha2 digest operation, finalizing the message digest and
8807c478bd9Sstevel@tonic-gate  *          zeroing the context.
881*55553f71Sda73024  *   input: uchar_t *	: a buffer to store the digest
8825151fb12Sdarrenm  *			: The function actually uses void* because many
8835151fb12Sdarrenm  *			: callers pass things other than uchar_t here.
8847c478bd9Sstevel@tonic-gate  *          SHA2_CTX *  : the context to finalize, save, and zero
8857c478bd9Sstevel@tonic-gate  *  output: void
8867c478bd9Sstevel@tonic-gate  */
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate void
889734b6a94Sdarrenm SHA2Final(void *digest, SHA2_CTX *ctx)
8907c478bd9Sstevel@tonic-gate {
8917c478bd9Sstevel@tonic-gate 	uint8_t		bitcount_be[sizeof (ctx->count.c32)];
8927c478bd9Sstevel@tonic-gate 	uint8_t		bitcount_be64[sizeof (ctx->count.c64)];
8937c478bd9Sstevel@tonic-gate 	uint32_t	index;
894*55553f71Sda73024 	uint32_t	algotype = ctx->algotype;
8957c478bd9Sstevel@tonic-gate 
896*55553f71Sda73024 	if (algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE) {
8977c478bd9Sstevel@tonic-gate 		index  = (ctx->count.c32[1] >> 3) & 0x3f;
8987c478bd9Sstevel@tonic-gate 		Encode(bitcount_be, ctx->count.c32, sizeof (bitcount_be));
8997c478bd9Sstevel@tonic-gate 		SHA2Update(ctx, PADDING, ((index < 56) ? 56 : 120) - index);
9007c478bd9Sstevel@tonic-gate 		SHA2Update(ctx, bitcount_be, sizeof (bitcount_be));
9017c478bd9Sstevel@tonic-gate 		Encode(digest, ctx->state.s32, sizeof (ctx->state.s32));
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	} else {
9047c478bd9Sstevel@tonic-gate 		index  = (ctx->count.c64[1] >> 3) & 0x7f;
9057c478bd9Sstevel@tonic-gate 		Encode64(bitcount_be64, ctx->count.c64,
9067c478bd9Sstevel@tonic-gate 		    sizeof (bitcount_be64));
9077c478bd9Sstevel@tonic-gate 		SHA2Update(ctx, PADDING, ((index < 112) ? 112 : 240) - index);
9087c478bd9Sstevel@tonic-gate 		SHA2Update(ctx, bitcount_be64, sizeof (bitcount_be64));
909*55553f71Sda73024 		if (algotype <= SHA384_HMAC_GEN_MECH_INFO_TYPE) {
9107c478bd9Sstevel@tonic-gate 			ctx->state.s64[6] = ctx->state.s64[7] = 0;
9117c478bd9Sstevel@tonic-gate 			Encode64(digest, ctx->state.s64,
9127c478bd9Sstevel@tonic-gate 			    sizeof (uint64_t) * 6);
9137c478bd9Sstevel@tonic-gate 		} else
9147c478bd9Sstevel@tonic-gate 			Encode64(digest, ctx->state.s64,
9157c478bd9Sstevel@tonic-gate 			    sizeof (ctx->state.s64));
9167c478bd9Sstevel@tonic-gate 	}
917673007c6Sdarrenm 
918673007c6Sdarrenm 	/* zeroize sensitive information */
919673007c6Sdarrenm 	bzero(ctx, sizeof (*ctx));
9207c478bd9Sstevel@tonic-gate }
921