xref: /titanic_53/usr/src/common/crypto/sha2/sha2.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * The basic framework for this code came from the reference
32*7c478bd9Sstevel@tonic-gate  * implementation for MD5.  That implementation is Copyright (C)
33*7c478bd9Sstevel@tonic-gate  * 1991-2, RSA Data Security, Inc. Created 1991. All rights reserved.
34*7c478bd9Sstevel@tonic-gate  *
35*7c478bd9Sstevel@tonic-gate  * License to copy and use this software is granted provided that it
36*7c478bd9Sstevel@tonic-gate  * is identified as the "RSA Data Security, Inc. MD5 Message-Digest
37*7c478bd9Sstevel@tonic-gate  * Algorithm" in all material mentioning or referencing this software
38*7c478bd9Sstevel@tonic-gate  * or this function.
39*7c478bd9Sstevel@tonic-gate  *
40*7c478bd9Sstevel@tonic-gate  * License is also granted to make and use derivative works provided
41*7c478bd9Sstevel@tonic-gate  * that such works are identified as "derived from the RSA Data
42*7c478bd9Sstevel@tonic-gate  * Security, Inc. MD5 Message-Digest Algorithm" in all material
43*7c478bd9Sstevel@tonic-gate  * mentioning or referencing the derived work.
44*7c478bd9Sstevel@tonic-gate  *
45*7c478bd9Sstevel@tonic-gate  * RSA Data Security, Inc. makes no representations concerning either
46*7c478bd9Sstevel@tonic-gate  * the merchantability of this software or the suitability of this
47*7c478bd9Sstevel@tonic-gate  * software for any particular purpose. It is provided "as is"
48*7c478bd9Sstevel@tonic-gate  * without express or implied warranty of any kind.
49*7c478bd9Sstevel@tonic-gate  *
50*7c478bd9Sstevel@tonic-gate  * These notices must be retained in any copies of any part of this
51*7c478bd9Sstevel@tonic-gate  * documentation and/or software.
52*7c478bd9Sstevel@tonic-gate  *
53*7c478bd9Sstevel@tonic-gate  * NOTE: Cleaned-up and optimized, version of SHA2, based on the FIPS 180-2
54*7c478bd9Sstevel@tonic-gate  * standard, available at http://www.itl.nist.gov/div897/pubs/fip180-2.htm
55*7c478bd9Sstevel@tonic-gate  * Not as fast as one would like -- further optimizations are encouraged
56*7c478bd9Sstevel@tonic-gate  * and appreciated.
57*7c478bd9Sstevel@tonic-gate  */
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
60*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
61*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
62*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
63*7c478bd9Sstevel@tonic-gate #include <sys/sha2.h>
64*7c478bd9Sstevel@tonic-gate #include <sys/sha2_consts.h>
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate #ifdef _KERNEL
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
69*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
70*7c478bd9Sstevel@tonic-gate #include <sys/crypto/common.h>
71*7c478bd9Sstevel@tonic-gate #include <sys/crypto/spi.h>
72*7c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate /*
75*7c478bd9Sstevel@tonic-gate  * The sha2 module is created with two modlinkages:
76*7c478bd9Sstevel@tonic-gate  * - a modlmisc that allows consumers to directly call the entry points
77*7c478bd9Sstevel@tonic-gate  *   SHA2Init, SHA2Update, and SHA2Final.
78*7c478bd9Sstevel@tonic-gate  * - a modlcrypto that allows the module to register with the Kernel
79*7c478bd9Sstevel@tonic-gate  *   Cryptographic Framework (KCF) as a software provider for the SHA2
80*7c478bd9Sstevel@tonic-gate  *   mechanisms.
81*7c478bd9Sstevel@tonic-gate  */
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate #else
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate #include <strings.h>
86*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
87*7c478bd9Sstevel@tonic-gate #include <errno.h>
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate #endif	/* !_KERNEL */
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate static void Encode(uint8_t *, uint32_t *, size_t);
93*7c478bd9Sstevel@tonic-gate static void Encode64(uint8_t *, uint64_t *, size_t);
94*7c478bd9Sstevel@tonic-gate static void SHA256Transform(SHA2_CTX *, const uint8_t *);
95*7c478bd9Sstevel@tonic-gate static void SHA512Transform(SHA2_CTX *, const uint8_t *);
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate static uint8_t PADDING[128] = { 0x80, /* all zeros */ };
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate /* Ch and Maj are the basic SHA2 functions. */
100*7c478bd9Sstevel@tonic-gate #define	Ch(b, c, d)	(((b) & (c)) ^ ((~b) & (d)))
101*7c478bd9Sstevel@tonic-gate #define	Maj(b, c, d)	(((b) & (c)) ^ ((b) & (d)) ^ ((c) & (d)))
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate /* Rotates x right n bits. */
104*7c478bd9Sstevel@tonic-gate #define	ROTR(x, n)	\
105*7c478bd9Sstevel@tonic-gate 	(((x) >> (n)) | ((x) << ((sizeof (x) * NBBY)-(n))))
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate /* Shift x right n bits */
108*7c478bd9Sstevel@tonic-gate #define	SHR(x, n)	((x) >> (n))
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate /* SHA256 Functions */
111*7c478bd9Sstevel@tonic-gate #define	BIGSIGMA0_256(x)	(ROTR((x), 2) ^ ROTR((x), 13) ^ ROTR((x), 22))
112*7c478bd9Sstevel@tonic-gate #define	BIGSIGMA1_256(x)	(ROTR((x), 6) ^ ROTR((x), 11) ^ ROTR((x), 25))
113*7c478bd9Sstevel@tonic-gate #define	SIGMA0_256(x)		(ROTR((x), 7) ^ ROTR((x), 18) ^ SHR((x), 3))
114*7c478bd9Sstevel@tonic-gate #define	SIGMA1_256(x)		(ROTR((x), 17) ^ ROTR((x), 19) ^ SHR((x), 10))
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate #define	SHA256ROUND(a, b, c, d, e, f, g, h, i, w)			\
117*7c478bd9Sstevel@tonic-gate 	T1 = h + BIGSIGMA1_256(e) + Ch(e, f, g) + SHA256_CONST(i) + w;	\
118*7c478bd9Sstevel@tonic-gate 	d += T1;							\
119*7c478bd9Sstevel@tonic-gate 	T2 = BIGSIGMA0_256(a) + Maj(a, b, c);				\
120*7c478bd9Sstevel@tonic-gate 	h = T1 + T2
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate /* SHA384/512 Functions */
123*7c478bd9Sstevel@tonic-gate #define	BIGSIGMA0(x)	(ROTR((x), 28) ^ ROTR((x), 34) ^ ROTR((x), 39))
124*7c478bd9Sstevel@tonic-gate #define	BIGSIGMA1(x)	(ROTR((x), 14) ^ ROTR((x), 18) ^ ROTR((x), 41))
125*7c478bd9Sstevel@tonic-gate #define	SIGMA0(x)	(ROTR((x), 1) ^ ROTR((x), 8) ^ SHR((x), 7))
126*7c478bd9Sstevel@tonic-gate #define	SIGMA1(x)	(ROTR((x), 19) ^ ROTR((x), 61) ^ SHR((x), 6))
127*7c478bd9Sstevel@tonic-gate #define	SHA512ROUND(a, b, c, d, e, f, g, h, i, w)			\
128*7c478bd9Sstevel@tonic-gate 	T1 = h + BIGSIGMA1(e) + Ch(e, f, g) + SHA512_CONST(i) + w;	\
129*7c478bd9Sstevel@tonic-gate 	d += T1;							\
130*7c478bd9Sstevel@tonic-gate 	T2 = BIGSIGMA0(a) + Maj(a, b, c);				\
131*7c478bd9Sstevel@tonic-gate 	h = T1 + T2
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate #ifdef _KERNEL
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate static struct modlmisc modlmisc = {
136*7c478bd9Sstevel@tonic-gate 	&mod_miscops,
137*7c478bd9Sstevel@tonic-gate 	"SHA2 Message-Digest Algorithm"
138*7c478bd9Sstevel@tonic-gate };
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate static struct modlcrypto modlcrypto = {
141*7c478bd9Sstevel@tonic-gate 	&mod_cryptoops,
142*7c478bd9Sstevel@tonic-gate 	"SHA2 Kernel SW Provider %I%"
143*7c478bd9Sstevel@tonic-gate };
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
146*7c478bd9Sstevel@tonic-gate 	MODREV_1, &modlmisc, &modlcrypto, NULL
147*7c478bd9Sstevel@tonic-gate };
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate /*
150*7c478bd9Sstevel@tonic-gate  * CSPI information (entry points, provider info, etc.)
151*7c478bd9Sstevel@tonic-gate  */
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate /*
156*7c478bd9Sstevel@tonic-gate  * List of support mechanisms in this module.
157*7c478bd9Sstevel@tonic-gate  *
158*7c478bd9Sstevel@tonic-gate  * It is important to note that in the module, division or modulus calculations
159*7c478bd9Sstevel@tonic-gate  * are used on the enumerated type to determine which mechanism is being used;
160*7c478bd9Sstevel@tonic-gate  * therefore, changing the order or additional mechanisms should be done
161*7c478bd9Sstevel@tonic-gate  * carefully
162*7c478bd9Sstevel@tonic-gate  */
163*7c478bd9Sstevel@tonic-gate typedef enum sha2_mech_type {
164*7c478bd9Sstevel@tonic-gate 	SHA256_MECH_INFO_TYPE,		/* SUN_CKM_SHA256 */
165*7c478bd9Sstevel@tonic-gate 	SHA256_HMAC_MECH_INFO_TYPE,	/* SUN_CKM_SHA256_HMAC */
166*7c478bd9Sstevel@tonic-gate 	SHA256_HMAC_GEN_MECH_INFO_TYPE,	/* SUN_CKM_SHA256_HMAC_GENERAL */
167*7c478bd9Sstevel@tonic-gate 	SHA384_MECH_INFO_TYPE,		/* SUN_CKM_SHA384 */
168*7c478bd9Sstevel@tonic-gate 	SHA384_HMAC_MECH_INFO_TYPE,	/* SUN_CKM_SHA384_HMAC */
169*7c478bd9Sstevel@tonic-gate 	SHA384_HMAC_GEN_MECH_INFO_TYPE,	/* SUN_CKM_SHA384_HMAC_GENERAL */
170*7c478bd9Sstevel@tonic-gate 	SHA512_MECH_INFO_TYPE,		/* SUN_CKM_SHA512 */
171*7c478bd9Sstevel@tonic-gate 	SHA512_HMAC_MECH_INFO_TYPE,	/* SUN_CKM_SHA512_HMAC */
172*7c478bd9Sstevel@tonic-gate 	SHA512_HMAC_GEN_MECH_INFO_TYPE	/* SUN_CKM_SHA512_HMAC_GENERAL */
173*7c478bd9Sstevel@tonic-gate } sha2_mech_type_t;
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate #ifdef _KERNEL
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate #define	SHA2_HMAC_MIN_KEY_LEN	8	/* SHA2-HMAC min key length in bits */
178*7c478bd9Sstevel@tonic-gate #define	SHA2_HMAC_MAX_KEY_LEN	INT_MAX /* SHA2-HMAC max key length in bits */
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate #define	SHA256_DIGEST_LENGTH	32	/* SHA256 digest length in bytes */
181*7c478bd9Sstevel@tonic-gate #define	SHA384_DIGEST_LENGTH	48	/* SHA384 digest length in bytes */
182*7c478bd9Sstevel@tonic-gate #define	SHA512_DIGEST_LENGTH	64	/* SHA512 digest length in bytes */
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate #define	SHA256_HMAC_BLOCK_SIZE	64	/* SHA256-HMAC block size */
185*7c478bd9Sstevel@tonic-gate #define	SHA512_HMAC_BLOCK_SIZE	128	/* SHA512-HMAC block size */
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate /*
188*7c478bd9Sstevel@tonic-gate  * Context for SHA2 mechanism.
189*7c478bd9Sstevel@tonic-gate  */
190*7c478bd9Sstevel@tonic-gate typedef struct sha2_ctx {
191*7c478bd9Sstevel@tonic-gate 	sha2_mech_type_t	sc_mech_type;	/* type of context */
192*7c478bd9Sstevel@tonic-gate 	SHA2_CTX		sc_sha2_ctx;	/* SHA2 context */
193*7c478bd9Sstevel@tonic-gate } sha2_ctx_t;
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate /*
196*7c478bd9Sstevel@tonic-gate  * Context for SHA2 HMAC and HMAC GENERAL mechanisms.
197*7c478bd9Sstevel@tonic-gate  */
198*7c478bd9Sstevel@tonic-gate typedef struct sha2_hmac_ctx {
199*7c478bd9Sstevel@tonic-gate 	sha2_mech_type_t	hc_mech_type;	/* type of context */
200*7c478bd9Sstevel@tonic-gate 	uint32_t		hc_digest_len;	/* digest len in bytes */
201*7c478bd9Sstevel@tonic-gate 	SHA2_CTX		hc_icontext;	/* inner SHA2 context */
202*7c478bd9Sstevel@tonic-gate 	SHA2_CTX		hc_ocontext;	/* outer SHA2 context */
203*7c478bd9Sstevel@tonic-gate } sha2_hmac_ctx_t;
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate /*
206*7c478bd9Sstevel@tonic-gate  * Macros to access the SHA2 or SHA2-HMAC contexts from a context passed
207*7c478bd9Sstevel@tonic-gate  * by KCF to one of the entry points.
208*7c478bd9Sstevel@tonic-gate  */
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate #define	PROV_SHA2_CTX(ctx)	((sha2_ctx_t *)(ctx)->cc_provider_private)
211*7c478bd9Sstevel@tonic-gate #define	PROV_SHA2_HMAC_CTX(ctx)	((sha2_hmac_ctx_t *)(ctx)->cc_provider_private)
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate /* to extract the digest length passed as mechanism parameter */
214*7c478bd9Sstevel@tonic-gate #define	PROV_SHA2_GET_DIGEST_LEN(m, len) {				\
215*7c478bd9Sstevel@tonic-gate 	if (IS_P2ALIGNED((m)->cm_param, sizeof (ulong_t)))		\
216*7c478bd9Sstevel@tonic-gate 		(len) = (uint32_t)*((ulong_t *)(m)->cm_param);	\
217*7c478bd9Sstevel@tonic-gate 	else {								\
218*7c478bd9Sstevel@tonic-gate 		ulong_t tmp_ulong;					\
219*7c478bd9Sstevel@tonic-gate 		bcopy((m)->cm_param, &tmp_ulong, sizeof (ulong_t));	\
220*7c478bd9Sstevel@tonic-gate 		(len) = (uint32_t)tmp_ulong;				\
221*7c478bd9Sstevel@tonic-gate 	}								\
222*7c478bd9Sstevel@tonic-gate }
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate #define	PROV_SHA2_DIGEST_KEY(mech, ctx, key, len, digest) {	\
225*7c478bd9Sstevel@tonic-gate 	SHA2Init(mech, ctx);				\
226*7c478bd9Sstevel@tonic-gate 	SHA2Update(ctx, key, len);			\
227*7c478bd9Sstevel@tonic-gate 	SHA2Final(digest, ctx);				\
228*7c478bd9Sstevel@tonic-gate }
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate /*
231*7c478bd9Sstevel@tonic-gate  * Mechanism info structure passed to KCF during registration.
232*7c478bd9Sstevel@tonic-gate  */
233*7c478bd9Sstevel@tonic-gate static crypto_mech_info_t sha2_mech_info_tab[] = {
234*7c478bd9Sstevel@tonic-gate 	/* SHA256 */
235*7c478bd9Sstevel@tonic-gate 	{SUN_CKM_SHA256, SHA256_MECH_INFO_TYPE,
236*7c478bd9Sstevel@tonic-gate 	    CRYPTO_FG_DIGEST | CRYPTO_FG_DIGEST_ATOMIC,
237*7c478bd9Sstevel@tonic-gate 	    0, 0, CRYPTO_KEYSIZE_UNIT_IN_BITS},
238*7c478bd9Sstevel@tonic-gate 	/* SHA256-HMAC */
239*7c478bd9Sstevel@tonic-gate 	{SUN_CKM_SHA256_HMAC, SHA256_HMAC_MECH_INFO_TYPE,
240*7c478bd9Sstevel@tonic-gate 	    CRYPTO_FG_MAC | CRYPTO_FG_MAC_ATOMIC,
241*7c478bd9Sstevel@tonic-gate 	    SHA2_HMAC_MIN_KEY_LEN, SHA2_HMAC_MAX_KEY_LEN,
242*7c478bd9Sstevel@tonic-gate 	    CRYPTO_KEYSIZE_UNIT_IN_BITS},
243*7c478bd9Sstevel@tonic-gate 	/* SHA256-HMAC GENERAL */
244*7c478bd9Sstevel@tonic-gate 	{SUN_CKM_SHA256_HMAC_GENERAL, SHA256_HMAC_GEN_MECH_INFO_TYPE,
245*7c478bd9Sstevel@tonic-gate 	    CRYPTO_FG_MAC | CRYPTO_FG_MAC_ATOMIC,
246*7c478bd9Sstevel@tonic-gate 	    SHA2_HMAC_MIN_KEY_LEN, SHA2_HMAC_MAX_KEY_LEN,
247*7c478bd9Sstevel@tonic-gate 	    CRYPTO_KEYSIZE_UNIT_IN_BITS},
248*7c478bd9Sstevel@tonic-gate 	/* SHA384 */
249*7c478bd9Sstevel@tonic-gate 	{SUN_CKM_SHA384, SHA384_MECH_INFO_TYPE,
250*7c478bd9Sstevel@tonic-gate 	    CRYPTO_FG_DIGEST | CRYPTO_FG_DIGEST_ATOMIC,
251*7c478bd9Sstevel@tonic-gate 	    0, 0, CRYPTO_KEYSIZE_UNIT_IN_BITS},
252*7c478bd9Sstevel@tonic-gate 	/* SHA384-HMAC */
253*7c478bd9Sstevel@tonic-gate 	{SUN_CKM_SHA384_HMAC, SHA384_HMAC_MECH_INFO_TYPE,
254*7c478bd9Sstevel@tonic-gate 	    CRYPTO_FG_MAC | CRYPTO_FG_MAC_ATOMIC,
255*7c478bd9Sstevel@tonic-gate 	    SHA2_HMAC_MIN_KEY_LEN, SHA2_HMAC_MAX_KEY_LEN,
256*7c478bd9Sstevel@tonic-gate 	    CRYPTO_KEYSIZE_UNIT_IN_BITS},
257*7c478bd9Sstevel@tonic-gate 	/* SHA384-HMAC GENERAL */
258*7c478bd9Sstevel@tonic-gate 	{SUN_CKM_SHA384_HMAC_GENERAL, SHA384_HMAC_GEN_MECH_INFO_TYPE,
259*7c478bd9Sstevel@tonic-gate 	    CRYPTO_FG_MAC | CRYPTO_FG_MAC_ATOMIC,
260*7c478bd9Sstevel@tonic-gate 	    SHA2_HMAC_MIN_KEY_LEN, SHA2_HMAC_MAX_KEY_LEN,
261*7c478bd9Sstevel@tonic-gate 	    CRYPTO_KEYSIZE_UNIT_IN_BITS},
262*7c478bd9Sstevel@tonic-gate 	/* SHA512 */
263*7c478bd9Sstevel@tonic-gate 	{SUN_CKM_SHA512, SHA512_MECH_INFO_TYPE,
264*7c478bd9Sstevel@tonic-gate 	    CRYPTO_FG_DIGEST | CRYPTO_FG_DIGEST_ATOMIC,
265*7c478bd9Sstevel@tonic-gate 	    0, 0, CRYPTO_KEYSIZE_UNIT_IN_BITS},
266*7c478bd9Sstevel@tonic-gate 	/* SHA512-HMAC */
267*7c478bd9Sstevel@tonic-gate 	{SUN_CKM_SHA512_HMAC, SHA512_HMAC_MECH_INFO_TYPE,
268*7c478bd9Sstevel@tonic-gate 	    CRYPTO_FG_MAC | CRYPTO_FG_MAC_ATOMIC,
269*7c478bd9Sstevel@tonic-gate 	    SHA2_HMAC_MIN_KEY_LEN, SHA2_HMAC_MAX_KEY_LEN,
270*7c478bd9Sstevel@tonic-gate 	    CRYPTO_KEYSIZE_UNIT_IN_BITS},
271*7c478bd9Sstevel@tonic-gate 	/* SHA512-HMAC GENERAL */
272*7c478bd9Sstevel@tonic-gate 	{SUN_CKM_SHA512_HMAC_GENERAL, SHA512_HMAC_GEN_MECH_INFO_TYPE,
273*7c478bd9Sstevel@tonic-gate 	    CRYPTO_FG_MAC | CRYPTO_FG_MAC_ATOMIC,
274*7c478bd9Sstevel@tonic-gate 	    SHA2_HMAC_MIN_KEY_LEN, SHA2_HMAC_MAX_KEY_LEN,
275*7c478bd9Sstevel@tonic-gate 	    CRYPTO_KEYSIZE_UNIT_IN_BITS}
276*7c478bd9Sstevel@tonic-gate };
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate void SHA2Init(uint64_t, SHA2_CTX *);
279*7c478bd9Sstevel@tonic-gate void SHA2Update(SHA2_CTX *, const uint8_t *, uint32_t);
280*7c478bd9Sstevel@tonic-gate void SHA2Final(uint8_t *, SHA2_CTX *);
281*7c478bd9Sstevel@tonic-gate 
282*7c478bd9Sstevel@tonic-gate static void sha2_provider_status(crypto_provider_handle_t, uint_t *);
283*7c478bd9Sstevel@tonic-gate 
284*7c478bd9Sstevel@tonic-gate static crypto_control_ops_t sha2_control_ops = {
285*7c478bd9Sstevel@tonic-gate 	sha2_provider_status
286*7c478bd9Sstevel@tonic-gate };
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate static int sha2_digest_init(crypto_ctx_t *, crypto_mechanism_t *,
289*7c478bd9Sstevel@tonic-gate     crypto_req_handle_t);
290*7c478bd9Sstevel@tonic-gate static int sha2_digest(crypto_ctx_t *, crypto_data_t *, crypto_data_t *,
291*7c478bd9Sstevel@tonic-gate     crypto_req_handle_t);
292*7c478bd9Sstevel@tonic-gate static int sha2_digest_update(crypto_ctx_t *, crypto_data_t *,
293*7c478bd9Sstevel@tonic-gate     crypto_req_handle_t);
294*7c478bd9Sstevel@tonic-gate static int sha2_digest_final(crypto_ctx_t *, crypto_data_t *,
295*7c478bd9Sstevel@tonic-gate     crypto_req_handle_t);
296*7c478bd9Sstevel@tonic-gate static int sha2_digest_atomic(crypto_provider_handle_t, crypto_session_id_t,
297*7c478bd9Sstevel@tonic-gate     crypto_mechanism_t *, crypto_data_t *, crypto_data_t *,
298*7c478bd9Sstevel@tonic-gate     crypto_req_handle_t);
299*7c478bd9Sstevel@tonic-gate 
300*7c478bd9Sstevel@tonic-gate static crypto_digest_ops_t sha2_digest_ops = {
301*7c478bd9Sstevel@tonic-gate 	sha2_digest_init,
302*7c478bd9Sstevel@tonic-gate 	sha2_digest,
303*7c478bd9Sstevel@tonic-gate 	sha2_digest_update,
304*7c478bd9Sstevel@tonic-gate 	NULL,
305*7c478bd9Sstevel@tonic-gate 	sha2_digest_final,
306*7c478bd9Sstevel@tonic-gate 	sha2_digest_atomic
307*7c478bd9Sstevel@tonic-gate };
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate static int sha2_mac_init(crypto_ctx_t *, crypto_mechanism_t *, crypto_key_t *,
310*7c478bd9Sstevel@tonic-gate     crypto_spi_ctx_template_t, crypto_req_handle_t);
311*7c478bd9Sstevel@tonic-gate static int sha2_mac_update(crypto_ctx_t *, crypto_data_t *,
312*7c478bd9Sstevel@tonic-gate     crypto_req_handle_t);
313*7c478bd9Sstevel@tonic-gate static int sha2_mac_final(crypto_ctx_t *, crypto_data_t *, crypto_req_handle_t);
314*7c478bd9Sstevel@tonic-gate static int sha2_mac_atomic(crypto_provider_handle_t, crypto_session_id_t,
315*7c478bd9Sstevel@tonic-gate     crypto_mechanism_t *, crypto_key_t *, crypto_data_t *, crypto_data_t *,
316*7c478bd9Sstevel@tonic-gate     crypto_spi_ctx_template_t, crypto_req_handle_t);
317*7c478bd9Sstevel@tonic-gate static int sha2_mac_verify_atomic(crypto_provider_handle_t, crypto_session_id_t,
318*7c478bd9Sstevel@tonic-gate     crypto_mechanism_t *, crypto_key_t *, crypto_data_t *, crypto_data_t *,
319*7c478bd9Sstevel@tonic-gate     crypto_spi_ctx_template_t, crypto_req_handle_t);
320*7c478bd9Sstevel@tonic-gate 
321*7c478bd9Sstevel@tonic-gate static crypto_mac_ops_t sha2_mac_ops = {
322*7c478bd9Sstevel@tonic-gate 	sha2_mac_init,
323*7c478bd9Sstevel@tonic-gate 	NULL,
324*7c478bd9Sstevel@tonic-gate 	sha2_mac_update,
325*7c478bd9Sstevel@tonic-gate 	sha2_mac_final,
326*7c478bd9Sstevel@tonic-gate 	sha2_mac_atomic,
327*7c478bd9Sstevel@tonic-gate 	sha2_mac_verify_atomic
328*7c478bd9Sstevel@tonic-gate };
329*7c478bd9Sstevel@tonic-gate 
330*7c478bd9Sstevel@tonic-gate static int sha2_create_ctx_template(crypto_provider_handle_t,
331*7c478bd9Sstevel@tonic-gate     crypto_mechanism_t *, crypto_key_t *, crypto_spi_ctx_template_t *,
332*7c478bd9Sstevel@tonic-gate     size_t *, crypto_req_handle_t);
333*7c478bd9Sstevel@tonic-gate static int sha2_free_context(crypto_ctx_t *);
334*7c478bd9Sstevel@tonic-gate 
335*7c478bd9Sstevel@tonic-gate static crypto_ctx_ops_t sha2_ctx_ops = {
336*7c478bd9Sstevel@tonic-gate 	sha2_create_ctx_template,
337*7c478bd9Sstevel@tonic-gate 	sha2_free_context
338*7c478bd9Sstevel@tonic-gate };
339*7c478bd9Sstevel@tonic-gate 
340*7c478bd9Sstevel@tonic-gate static crypto_ops_t sha2_crypto_ops = {
341*7c478bd9Sstevel@tonic-gate 	&sha2_control_ops,
342*7c478bd9Sstevel@tonic-gate 	&sha2_digest_ops,
343*7c478bd9Sstevel@tonic-gate 	NULL,
344*7c478bd9Sstevel@tonic-gate 	&sha2_mac_ops,
345*7c478bd9Sstevel@tonic-gate 	NULL,
346*7c478bd9Sstevel@tonic-gate 	NULL,
347*7c478bd9Sstevel@tonic-gate 	NULL,
348*7c478bd9Sstevel@tonic-gate 	NULL,
349*7c478bd9Sstevel@tonic-gate 	NULL,
350*7c478bd9Sstevel@tonic-gate 	NULL,
351*7c478bd9Sstevel@tonic-gate 	NULL,
352*7c478bd9Sstevel@tonic-gate 	NULL,
353*7c478bd9Sstevel@tonic-gate 	NULL,
354*7c478bd9Sstevel@tonic-gate 	&sha2_ctx_ops
355*7c478bd9Sstevel@tonic-gate };
356*7c478bd9Sstevel@tonic-gate 
357*7c478bd9Sstevel@tonic-gate static crypto_provider_info_t sha2_prov_info = {
358*7c478bd9Sstevel@tonic-gate 	CRYPTO_SPI_VERSION_1,
359*7c478bd9Sstevel@tonic-gate 	"SHA2 Software Provider",
360*7c478bd9Sstevel@tonic-gate 	CRYPTO_SW_PROVIDER,
361*7c478bd9Sstevel@tonic-gate 	{&modlinkage},
362*7c478bd9Sstevel@tonic-gate 	NULL,
363*7c478bd9Sstevel@tonic-gate 	&sha2_crypto_ops,
364*7c478bd9Sstevel@tonic-gate 	sizeof (sha2_mech_info_tab)/sizeof (crypto_mech_info_t),
365*7c478bd9Sstevel@tonic-gate 	sha2_mech_info_tab
366*7c478bd9Sstevel@tonic-gate };
367*7c478bd9Sstevel@tonic-gate 
368*7c478bd9Sstevel@tonic-gate static crypto_kcf_provider_handle_t sha2_prov_handle = NULL;
369*7c478bd9Sstevel@tonic-gate 
370*7c478bd9Sstevel@tonic-gate int
371*7c478bd9Sstevel@tonic-gate _init()
372*7c478bd9Sstevel@tonic-gate {
373*7c478bd9Sstevel@tonic-gate 	int ret;
374*7c478bd9Sstevel@tonic-gate 
375*7c478bd9Sstevel@tonic-gate 	if ((ret = mod_install(&modlinkage)) != 0)
376*7c478bd9Sstevel@tonic-gate 		return (ret);
377*7c478bd9Sstevel@tonic-gate 
378*7c478bd9Sstevel@tonic-gate 	/*
379*7c478bd9Sstevel@tonic-gate 	 * Register with KCF. If the registration fails, log an
380*7c478bd9Sstevel@tonic-gate 	 * error but do not uninstall the module, since the functionality
381*7c478bd9Sstevel@tonic-gate 	 * provided by misc/sha2 should still be available.
382*7c478bd9Sstevel@tonic-gate 	 */
383*7c478bd9Sstevel@tonic-gate 	if ((ret = crypto_register_provider(&sha2_prov_info,
384*7c478bd9Sstevel@tonic-gate 	    &sha2_prov_handle)) != CRYPTO_SUCCESS)
385*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "sha2 _init: "
386*7c478bd9Sstevel@tonic-gate 		    "crypto_register_provider() failed (0x%x)", ret);
387*7c478bd9Sstevel@tonic-gate 
388*7c478bd9Sstevel@tonic-gate 	return (0);
389*7c478bd9Sstevel@tonic-gate }
390*7c478bd9Sstevel@tonic-gate 
391*7c478bd9Sstevel@tonic-gate int
392*7c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
393*7c478bd9Sstevel@tonic-gate {
394*7c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
395*7c478bd9Sstevel@tonic-gate }
396*7c478bd9Sstevel@tonic-gate 
397*7c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
398*7c478bd9Sstevel@tonic-gate 
399*7c478bd9Sstevel@tonic-gate 
400*7c478bd9Sstevel@tonic-gate /*
401*7c478bd9Sstevel@tonic-gate  * sparc optimization:
402*7c478bd9Sstevel@tonic-gate  *
403*7c478bd9Sstevel@tonic-gate  * on the sparc, we can load big endian 32-bit data easily.  note that
404*7c478bd9Sstevel@tonic-gate  * special care must be taken to ensure the address is 32-bit aligned.
405*7c478bd9Sstevel@tonic-gate  * in the interest of speed, we don't check to make sure, since
406*7c478bd9Sstevel@tonic-gate  * careful programming can guarantee this for us.
407*7c478bd9Sstevel@tonic-gate  */
408*7c478bd9Sstevel@tonic-gate 
409*7c478bd9Sstevel@tonic-gate #if	defined(_BIG_ENDIAN)
410*7c478bd9Sstevel@tonic-gate 
411*7c478bd9Sstevel@tonic-gate #define	LOAD_BIG_32(addr)	(*(uint32_t *)(addr))
412*7c478bd9Sstevel@tonic-gate 
413*7c478bd9Sstevel@tonic-gate #else	/* little endian -- will work on big endian, but slowly */
414*7c478bd9Sstevel@tonic-gate 
415*7c478bd9Sstevel@tonic-gate #define	LOAD_BIG_32(addr)	\
416*7c478bd9Sstevel@tonic-gate 	(((addr)[0] << 24) | ((addr)[1] << 16) | ((addr)[2] << 8) | (addr)[3])
417*7c478bd9Sstevel@tonic-gate #endif
418*7c478bd9Sstevel@tonic-gate 
419*7c478bd9Sstevel@tonic-gate 
420*7c478bd9Sstevel@tonic-gate #if	defined(_BIG_ENDIAN)
421*7c478bd9Sstevel@tonic-gate 
422*7c478bd9Sstevel@tonic-gate #define	LOAD_BIG_64(addr)	(*(uint64_t *)(addr))
423*7c478bd9Sstevel@tonic-gate 
424*7c478bd9Sstevel@tonic-gate #else	/* little endian -- will work on big endian, but slowly */
425*7c478bd9Sstevel@tonic-gate 
426*7c478bd9Sstevel@tonic-gate #define	LOAD_BIG_64(addr)	\
427*7c478bd9Sstevel@tonic-gate 	(((uint64_t)(addr)[0] << 56) | ((uint64_t)(addr)[1] << 48) |	\
428*7c478bd9Sstevel@tonic-gate 	    ((uint64_t)(addr)[2] << 40) | ((uint64_t)(addr)[3] << 32) |	\
429*7c478bd9Sstevel@tonic-gate 	    ((uint64_t)(addr)[4] << 24) | ((uint64_t)(addr)[5] << 16) |	\
430*7c478bd9Sstevel@tonic-gate 	    ((uint64_t)(addr)[6] << 8) | (uint64_t)(addr)[7])
431*7c478bd9Sstevel@tonic-gate 
432*7c478bd9Sstevel@tonic-gate #endif
433*7c478bd9Sstevel@tonic-gate 
434*7c478bd9Sstevel@tonic-gate 
435*7c478bd9Sstevel@tonic-gate /* SHA256 Transform */
436*7c478bd9Sstevel@tonic-gate 
437*7c478bd9Sstevel@tonic-gate static void
438*7c478bd9Sstevel@tonic-gate SHA256Transform(SHA2_CTX *ctx, const uint8_t *blk)
439*7c478bd9Sstevel@tonic-gate {
440*7c478bd9Sstevel@tonic-gate 
441*7c478bd9Sstevel@tonic-gate 	uint32_t a = ctx->state.s32[0];
442*7c478bd9Sstevel@tonic-gate 	uint32_t b = ctx->state.s32[1];
443*7c478bd9Sstevel@tonic-gate 	uint32_t c = ctx->state.s32[2];
444*7c478bd9Sstevel@tonic-gate 	uint32_t d = ctx->state.s32[3];
445*7c478bd9Sstevel@tonic-gate 	uint32_t e = ctx->state.s32[4];
446*7c478bd9Sstevel@tonic-gate 	uint32_t f = ctx->state.s32[5];
447*7c478bd9Sstevel@tonic-gate 	uint32_t g = ctx->state.s32[6];
448*7c478bd9Sstevel@tonic-gate 	uint32_t h = ctx->state.s32[7];
449*7c478bd9Sstevel@tonic-gate 
450*7c478bd9Sstevel@tonic-gate 	uint32_t w0, w1, w2, w3, w4, w5, w6, w7;
451*7c478bd9Sstevel@tonic-gate 	uint32_t w8, w9, w10, w11, w12, w13, w14, w15;
452*7c478bd9Sstevel@tonic-gate 	uint32_t T1, T2;
453*7c478bd9Sstevel@tonic-gate 
454*7c478bd9Sstevel@tonic-gate #if	defined(__sparc)
455*7c478bd9Sstevel@tonic-gate 	static const uint32_t sha256_consts[] = {
456*7c478bd9Sstevel@tonic-gate 		SHA256_CONST_0, SHA256_CONST_1, SHA256_CONST_2,
457*7c478bd9Sstevel@tonic-gate 		SHA256_CONST_3, SHA256_CONST_4, SHA256_CONST_5,
458*7c478bd9Sstevel@tonic-gate 		SHA256_CONST_6, SHA256_CONST_7, SHA256_CONST_8,
459*7c478bd9Sstevel@tonic-gate 		SHA256_CONST_9, SHA256_CONST_10, SHA256_CONST_11,
460*7c478bd9Sstevel@tonic-gate 		SHA256_CONST_12, SHA256_CONST_13, SHA256_CONST_14,
461*7c478bd9Sstevel@tonic-gate 		SHA256_CONST_15, SHA256_CONST_16, SHA256_CONST_17,
462*7c478bd9Sstevel@tonic-gate 		SHA256_CONST_18, SHA256_CONST_19, SHA256_CONST_20,
463*7c478bd9Sstevel@tonic-gate 		SHA256_CONST_21, SHA256_CONST_22, SHA256_CONST_23,
464*7c478bd9Sstevel@tonic-gate 		SHA256_CONST_24, SHA256_CONST_25, SHA256_CONST_26,
465*7c478bd9Sstevel@tonic-gate 		SHA256_CONST_27, SHA256_CONST_28, SHA256_CONST_29,
466*7c478bd9Sstevel@tonic-gate 		SHA256_CONST_30, SHA256_CONST_31, SHA256_CONST_32,
467*7c478bd9Sstevel@tonic-gate 		SHA256_CONST_33, SHA256_CONST_34, SHA256_CONST_35,
468*7c478bd9Sstevel@tonic-gate 		SHA256_CONST_36, SHA256_CONST_37, SHA256_CONST_38,
469*7c478bd9Sstevel@tonic-gate 		SHA256_CONST_39, SHA256_CONST_40, SHA256_CONST_41,
470*7c478bd9Sstevel@tonic-gate 		SHA256_CONST_42, SHA256_CONST_43, SHA256_CONST_44,
471*7c478bd9Sstevel@tonic-gate 		SHA256_CONST_45, SHA256_CONST_46, SHA256_CONST_47,
472*7c478bd9Sstevel@tonic-gate 		SHA256_CONST_48, SHA256_CONST_49, SHA256_CONST_50,
473*7c478bd9Sstevel@tonic-gate 		SHA256_CONST_51, SHA256_CONST_52, SHA256_CONST_53,
474*7c478bd9Sstevel@tonic-gate 		SHA256_CONST_54, SHA256_CONST_55, SHA256_CONST_56,
475*7c478bd9Sstevel@tonic-gate 		SHA256_CONST_57, SHA256_CONST_58, SHA256_CONST_59,
476*7c478bd9Sstevel@tonic-gate 		SHA256_CONST_60, SHA256_CONST_61, SHA256_CONST_62,
477*7c478bd9Sstevel@tonic-gate 		SHA256_CONST_63
478*7c478bd9Sstevel@tonic-gate 	};
479*7c478bd9Sstevel@tonic-gate #endif
480*7c478bd9Sstevel@tonic-gate 
481*7c478bd9Sstevel@tonic-gate 	if ((uintptr_t)blk & 0x3) {		/* not 4-byte aligned? */
482*7c478bd9Sstevel@tonic-gate 		bcopy(blk, ctx->buf_un.buf32,  sizeof (ctx->buf_un.buf32));
483*7c478bd9Sstevel@tonic-gate 		blk = (uint8_t *)ctx->buf_un.buf32;
484*7c478bd9Sstevel@tonic-gate 	}
485*7c478bd9Sstevel@tonic-gate 
486*7c478bd9Sstevel@tonic-gate 	w0 =  LOAD_BIG_32(blk + 4 * 0);
487*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(a, b, c, d, e, f, g, h, 0, w0);
488*7c478bd9Sstevel@tonic-gate 	w1 =  LOAD_BIG_32(blk + 4 * 1);
489*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(h, a, b, c, d, e, f, g, 1, w1);
490*7c478bd9Sstevel@tonic-gate 	w2 =  LOAD_BIG_32(blk + 4 * 2);
491*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(g, h, a, b, c, d, e, f, 2, w2);
492*7c478bd9Sstevel@tonic-gate 	w3 =  LOAD_BIG_32(blk + 4 * 3);
493*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(f, g, h, a, b, c, d, e, 3, w3);
494*7c478bd9Sstevel@tonic-gate 	w4 =  LOAD_BIG_32(blk + 4 * 4);
495*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(e, f, g, h, a, b, c, d, 4, w4);
496*7c478bd9Sstevel@tonic-gate 	w5 =  LOAD_BIG_32(blk + 4 * 5);
497*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(d, e, f, g, h, a, b, c, 5, w5);
498*7c478bd9Sstevel@tonic-gate 	w6 =  LOAD_BIG_32(blk + 4 * 6);
499*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(c, d, e, f, g, h, a, b, 6, w6);
500*7c478bd9Sstevel@tonic-gate 	w7 =  LOAD_BIG_32(blk + 4 * 7);
501*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(b, c, d, e, f, g, h, a, 7, w7);
502*7c478bd9Sstevel@tonic-gate 	w8 =  LOAD_BIG_32(blk + 4 * 8);
503*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(a, b, c, d, e, f, g, h, 8, w8);
504*7c478bd9Sstevel@tonic-gate 	w9 =  LOAD_BIG_32(blk + 4 * 9);
505*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(h, a, b, c, d, e, f, g, 9, w9);
506*7c478bd9Sstevel@tonic-gate 	w10 =  LOAD_BIG_32(blk + 4 * 10);
507*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(g, h, a, b, c, d, e, f, 10, w10);
508*7c478bd9Sstevel@tonic-gate 	w11 =  LOAD_BIG_32(blk + 4 * 11);
509*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(f, g, h, a, b, c, d, e, 11, w11);
510*7c478bd9Sstevel@tonic-gate 	w12 =  LOAD_BIG_32(blk + 4 * 12);
511*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(e, f, g, h, a, b, c, d, 12, w12);
512*7c478bd9Sstevel@tonic-gate 	w13 =  LOAD_BIG_32(blk + 4 * 13);
513*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(d, e, f, g, h, a, b, c, 13, w13);
514*7c478bd9Sstevel@tonic-gate 	w14 =  LOAD_BIG_32(blk + 4 * 14);
515*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(c, d, e, f, g, h, a, b, 14, w14);
516*7c478bd9Sstevel@tonic-gate 	w15 =  LOAD_BIG_32(blk + 4 * 15);
517*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(b, c, d, e, f, g, h, a, 15, w15);
518*7c478bd9Sstevel@tonic-gate 
519*7c478bd9Sstevel@tonic-gate 	w0 = SIGMA1_256(w14) + w9 + SIGMA0_256(w1) + w0;
520*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(a, b, c, d, e, f, g, h, 16, w0);
521*7c478bd9Sstevel@tonic-gate 	w1 = SIGMA1_256(w15) + w10 + SIGMA0_256(w2) + w1;
522*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(h, a, b, c, d, e, f, g, 17, w1);
523*7c478bd9Sstevel@tonic-gate 	w2 = SIGMA1_256(w0) + w11 + SIGMA0_256(w3) + w2;
524*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(g, h, a, b, c, d, e, f, 18, w2);
525*7c478bd9Sstevel@tonic-gate 	w3 = SIGMA1_256(w1) + w12 + SIGMA0_256(w4) + w3;
526*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(f, g, h, a, b, c, d, e, 19, w3);
527*7c478bd9Sstevel@tonic-gate 	w4 = SIGMA1_256(w2) + w13 + SIGMA0_256(w5) + w4;
528*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(e, f, g, h, a, b, c, d, 20, w4);
529*7c478bd9Sstevel@tonic-gate 	w5 = SIGMA1_256(w3) + w14 + SIGMA0_256(w6) + w5;
530*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(d, e, f, g, h, a, b, c, 21, w5);
531*7c478bd9Sstevel@tonic-gate 	w6 = SIGMA1_256(w4) + w15 + SIGMA0_256(w7) + w6;
532*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(c, d, e, f, g, h, a, b, 22, w6);
533*7c478bd9Sstevel@tonic-gate 	w7 = SIGMA1_256(w5) + w0 + SIGMA0_256(w8) + w7;
534*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(b, c, d, e, f, g, h, a, 23, w7);
535*7c478bd9Sstevel@tonic-gate 	w8 = SIGMA1_256(w6) + w1 + SIGMA0_256(w9) + w8;
536*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(a, b, c, d, e, f, g, h, 24, w8);
537*7c478bd9Sstevel@tonic-gate 	w9 = SIGMA1_256(w7) + w2 + SIGMA0_256(w10) + w9;
538*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(h, a, b, c, d, e, f, g, 25, w9);
539*7c478bd9Sstevel@tonic-gate 	w10 = SIGMA1_256(w8) + w3 + SIGMA0_256(w11) + w10;
540*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(g, h, a, b, c, d, e, f, 26, w10);
541*7c478bd9Sstevel@tonic-gate 	w11 = SIGMA1_256(w9) + w4 + SIGMA0_256(w12) + w11;
542*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(f, g, h, a, b, c, d, e, 27, w11);
543*7c478bd9Sstevel@tonic-gate 	w12 = SIGMA1_256(w10) + w5 + SIGMA0_256(w13) + w12;
544*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(e, f, g, h, a, b, c, d, 28, w12);
545*7c478bd9Sstevel@tonic-gate 	w13 = SIGMA1_256(w11) + w6 + SIGMA0_256(w14) + w13;
546*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(d, e, f, g, h, a, b, c, 29, w13);
547*7c478bd9Sstevel@tonic-gate 	w14 = SIGMA1_256(w12) + w7 + SIGMA0_256(w15) + w14;
548*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(c, d, e, f, g, h, a, b, 30, w14);
549*7c478bd9Sstevel@tonic-gate 	w15 = SIGMA1_256(w13) + w8 + SIGMA0_256(w0) + w15;
550*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(b, c, d, e, f, g, h, a, 31, w15);
551*7c478bd9Sstevel@tonic-gate 
552*7c478bd9Sstevel@tonic-gate 	w0 = SIGMA1_256(w14) + w9 + SIGMA0_256(w1) + w0;
553*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(a, b, c, d, e, f, g, h, 32, w0);
554*7c478bd9Sstevel@tonic-gate 	w1 = SIGMA1_256(w15) + w10 + SIGMA0_256(w2) + w1;
555*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(h, a, b, c, d, e, f, g, 33, w1);
556*7c478bd9Sstevel@tonic-gate 	w2 = SIGMA1_256(w0) + w11 + SIGMA0_256(w3) + w2;
557*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(g, h, a, b, c, d, e, f, 34, w2);
558*7c478bd9Sstevel@tonic-gate 	w3 = SIGMA1_256(w1) + w12 + SIGMA0_256(w4) + w3;
559*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(f, g, h, a, b, c, d, e, 35, w3);
560*7c478bd9Sstevel@tonic-gate 	w4 = SIGMA1_256(w2) + w13 + SIGMA0_256(w5) + w4;
561*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(e, f, g, h, a, b, c, d, 36, w4);
562*7c478bd9Sstevel@tonic-gate 	w5 = SIGMA1_256(w3) + w14 + SIGMA0_256(w6) + w5;
563*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(d, e, f, g, h, a, b, c, 37, w5);
564*7c478bd9Sstevel@tonic-gate 	w6 = SIGMA1_256(w4) + w15 + SIGMA0_256(w7) + w6;
565*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(c, d, e, f, g, h, a, b, 38, w6);
566*7c478bd9Sstevel@tonic-gate 	w7 = SIGMA1_256(w5) + w0 + SIGMA0_256(w8) + w7;
567*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(b, c, d, e, f, g, h, a, 39, w7);
568*7c478bd9Sstevel@tonic-gate 	w8 = SIGMA1_256(w6) + w1 + SIGMA0_256(w9) + w8;
569*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(a, b, c, d, e, f, g, h, 40, w8);
570*7c478bd9Sstevel@tonic-gate 	w9 = SIGMA1_256(w7) + w2 + SIGMA0_256(w10) + w9;
571*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(h, a, b, c, d, e, f, g, 41, w9);
572*7c478bd9Sstevel@tonic-gate 	w10 = SIGMA1_256(w8) + w3 + SIGMA0_256(w11) + w10;
573*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(g, h, a, b, c, d, e, f, 42, w10);
574*7c478bd9Sstevel@tonic-gate 	w11 = SIGMA1_256(w9) + w4 + SIGMA0_256(w12) + w11;
575*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(f, g, h, a, b, c, d, e, 43, w11);
576*7c478bd9Sstevel@tonic-gate 	w12 = SIGMA1_256(w10) + w5 + SIGMA0_256(w13) + w12;
577*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(e, f, g, h, a, b, c, d, 44, w12);
578*7c478bd9Sstevel@tonic-gate 	w13 = SIGMA1_256(w11) + w6 + SIGMA0_256(w14) + w13;
579*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(d, e, f, g, h, a, b, c, 45, w13);
580*7c478bd9Sstevel@tonic-gate 	w14 = SIGMA1_256(w12) + w7 + SIGMA0_256(w15) + w14;
581*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(c, d, e, f, g, h, a, b, 46, w14);
582*7c478bd9Sstevel@tonic-gate 	w15 = SIGMA1_256(w13) + w8 + SIGMA0_256(w0) + w15;
583*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(b, c, d, e, f, g, h, a, 47, w15);
584*7c478bd9Sstevel@tonic-gate 
585*7c478bd9Sstevel@tonic-gate 	w0 = SIGMA1_256(w14) + w9 + SIGMA0_256(w1) + w0;
586*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(a, b, c, d, e, f, g, h, 48, w0);
587*7c478bd9Sstevel@tonic-gate 	w1 = SIGMA1_256(w15) + w10 + SIGMA0_256(w2) + w1;
588*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(h, a, b, c, d, e, f, g, 49, w1);
589*7c478bd9Sstevel@tonic-gate 	w2 = SIGMA1_256(w0) + w11 + SIGMA0_256(w3) + w2;
590*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(g, h, a, b, c, d, e, f, 50, w2);
591*7c478bd9Sstevel@tonic-gate 	w3 = SIGMA1_256(w1) + w12 + SIGMA0_256(w4) + w3;
592*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(f, g, h, a, b, c, d, e, 51, w3);
593*7c478bd9Sstevel@tonic-gate 	w4 = SIGMA1_256(w2) + w13 + SIGMA0_256(w5) + w4;
594*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(e, f, g, h, a, b, c, d, 52, w4);
595*7c478bd9Sstevel@tonic-gate 	w5 = SIGMA1_256(w3) + w14 + SIGMA0_256(w6) + w5;
596*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(d, e, f, g, h, a, b, c, 53, w5);
597*7c478bd9Sstevel@tonic-gate 	w6 = SIGMA1_256(w4) + w15 + SIGMA0_256(w7) + w6;
598*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(c, d, e, f, g, h, a, b, 54, w6);
599*7c478bd9Sstevel@tonic-gate 	w7 = SIGMA1_256(w5) + w0 + SIGMA0_256(w8) + w7;
600*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(b, c, d, e, f, g, h, a, 55, w7);
601*7c478bd9Sstevel@tonic-gate 	w8 = SIGMA1_256(w6) + w1 + SIGMA0_256(w9) + w8;
602*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(a, b, c, d, e, f, g, h, 56, w8);
603*7c478bd9Sstevel@tonic-gate 	w9 = SIGMA1_256(w7) + w2 + SIGMA0_256(w10) + w9;
604*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(h, a, b, c, d, e, f, g, 57, w9);
605*7c478bd9Sstevel@tonic-gate 	w10 = SIGMA1_256(w8) + w3 + SIGMA0_256(w11) + w10;
606*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(g, h, a, b, c, d, e, f, 58, w10);
607*7c478bd9Sstevel@tonic-gate 	w11 = SIGMA1_256(w9) + w4 + SIGMA0_256(w12) + w11;
608*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(f, g, h, a, b, c, d, e, 59, w11);
609*7c478bd9Sstevel@tonic-gate 	w12 = SIGMA1_256(w10) + w5 + SIGMA0_256(w13) + w12;
610*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(e, f, g, h, a, b, c, d, 60, w12);
611*7c478bd9Sstevel@tonic-gate 	w13 = SIGMA1_256(w11) + w6 + SIGMA0_256(w14) + w13;
612*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(d, e, f, g, h, a, b, c, 61, w13);
613*7c478bd9Sstevel@tonic-gate 	w14 = SIGMA1_256(w12) + w7 + SIGMA0_256(w15) + w14;
614*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(c, d, e, f, g, h, a, b, 62, w14);
615*7c478bd9Sstevel@tonic-gate 	w15 = SIGMA1_256(w13) + w8 + SIGMA0_256(w0) + w15;
616*7c478bd9Sstevel@tonic-gate 	SHA256ROUND(b, c, d, e, f, g, h, a, 63, w15);
617*7c478bd9Sstevel@tonic-gate 
618*7c478bd9Sstevel@tonic-gate 	ctx->state.s32[0] += a;
619*7c478bd9Sstevel@tonic-gate 	ctx->state.s32[1] += b;
620*7c478bd9Sstevel@tonic-gate 	ctx->state.s32[2] += c;
621*7c478bd9Sstevel@tonic-gate 	ctx->state.s32[3] += d;
622*7c478bd9Sstevel@tonic-gate 	ctx->state.s32[4] += e;
623*7c478bd9Sstevel@tonic-gate 	ctx->state.s32[5] += f;
624*7c478bd9Sstevel@tonic-gate 	ctx->state.s32[6] += g;
625*7c478bd9Sstevel@tonic-gate 	ctx->state.s32[7] += h;
626*7c478bd9Sstevel@tonic-gate }
627*7c478bd9Sstevel@tonic-gate 
628*7c478bd9Sstevel@tonic-gate 
629*7c478bd9Sstevel@tonic-gate /* SHA384 and SHA512 Transform */
630*7c478bd9Sstevel@tonic-gate 
631*7c478bd9Sstevel@tonic-gate static void
632*7c478bd9Sstevel@tonic-gate SHA512Transform(SHA2_CTX *ctx, const uint8_t *blk)
633*7c478bd9Sstevel@tonic-gate {
634*7c478bd9Sstevel@tonic-gate 
635*7c478bd9Sstevel@tonic-gate 	uint64_t a = ctx->state.s64[0];
636*7c478bd9Sstevel@tonic-gate 	uint64_t b = ctx->state.s64[1];
637*7c478bd9Sstevel@tonic-gate 	uint64_t c = ctx->state.s64[2];
638*7c478bd9Sstevel@tonic-gate 	uint64_t d = ctx->state.s64[3];
639*7c478bd9Sstevel@tonic-gate 	uint64_t e = ctx->state.s64[4];
640*7c478bd9Sstevel@tonic-gate 	uint64_t f = ctx->state.s64[5];
641*7c478bd9Sstevel@tonic-gate 	uint64_t g = ctx->state.s64[6];
642*7c478bd9Sstevel@tonic-gate 	uint64_t h = ctx->state.s64[7];
643*7c478bd9Sstevel@tonic-gate 
644*7c478bd9Sstevel@tonic-gate 	uint64_t w0, w1, w2, w3, w4, w5, w6, w7;
645*7c478bd9Sstevel@tonic-gate 	uint64_t w8, w9, w10, w11, w12, w13, w14, w15;
646*7c478bd9Sstevel@tonic-gate 	uint64_t T1, T2;
647*7c478bd9Sstevel@tonic-gate 
648*7c478bd9Sstevel@tonic-gate #if	defined(__sparc)
649*7c478bd9Sstevel@tonic-gate 	static const uint64_t sha512_consts[] = {
650*7c478bd9Sstevel@tonic-gate 		SHA512_CONST_0, SHA512_CONST_1, SHA512_CONST_2,
651*7c478bd9Sstevel@tonic-gate 		SHA512_CONST_3, SHA512_CONST_4, SHA512_CONST_5,
652*7c478bd9Sstevel@tonic-gate 		SHA512_CONST_6, SHA512_CONST_7, SHA512_CONST_8,
653*7c478bd9Sstevel@tonic-gate 		SHA512_CONST_9, SHA512_CONST_10, SHA512_CONST_11,
654*7c478bd9Sstevel@tonic-gate 		SHA512_CONST_12, SHA512_CONST_13, SHA512_CONST_14,
655*7c478bd9Sstevel@tonic-gate 		SHA512_CONST_15, SHA512_CONST_16, SHA512_CONST_17,
656*7c478bd9Sstevel@tonic-gate 		SHA512_CONST_18, SHA512_CONST_19, SHA512_CONST_20,
657*7c478bd9Sstevel@tonic-gate 		SHA512_CONST_21, SHA512_CONST_22, SHA512_CONST_23,
658*7c478bd9Sstevel@tonic-gate 		SHA512_CONST_24, SHA512_CONST_25, SHA512_CONST_26,
659*7c478bd9Sstevel@tonic-gate 		SHA512_CONST_27, SHA512_CONST_28, SHA512_CONST_29,
660*7c478bd9Sstevel@tonic-gate 		SHA512_CONST_30, SHA512_CONST_31, SHA512_CONST_32,
661*7c478bd9Sstevel@tonic-gate 		SHA512_CONST_33, SHA512_CONST_34, SHA512_CONST_35,
662*7c478bd9Sstevel@tonic-gate 		SHA512_CONST_36, SHA512_CONST_37, SHA512_CONST_38,
663*7c478bd9Sstevel@tonic-gate 		SHA512_CONST_39, SHA512_CONST_40, SHA512_CONST_41,
664*7c478bd9Sstevel@tonic-gate 		SHA512_CONST_42, SHA512_CONST_43, SHA512_CONST_44,
665*7c478bd9Sstevel@tonic-gate 		SHA512_CONST_45, SHA512_CONST_46, SHA512_CONST_47,
666*7c478bd9Sstevel@tonic-gate 		SHA512_CONST_48, SHA512_CONST_49, SHA512_CONST_50,
667*7c478bd9Sstevel@tonic-gate 		SHA512_CONST_51, SHA512_CONST_52, SHA512_CONST_53,
668*7c478bd9Sstevel@tonic-gate 		SHA512_CONST_54, SHA512_CONST_55, SHA512_CONST_56,
669*7c478bd9Sstevel@tonic-gate 		SHA512_CONST_57, SHA512_CONST_58, SHA512_CONST_59,
670*7c478bd9Sstevel@tonic-gate 		SHA512_CONST_60, SHA512_CONST_61, SHA512_CONST_62,
671*7c478bd9Sstevel@tonic-gate 		SHA512_CONST_63, SHA512_CONST_64, SHA512_CONST_65,
672*7c478bd9Sstevel@tonic-gate 		SHA512_CONST_66, SHA512_CONST_67, SHA512_CONST_68,
673*7c478bd9Sstevel@tonic-gate 		SHA512_CONST_69, SHA512_CONST_70, SHA512_CONST_71,
674*7c478bd9Sstevel@tonic-gate 		SHA512_CONST_72, SHA512_CONST_73, SHA512_CONST_74,
675*7c478bd9Sstevel@tonic-gate 		SHA512_CONST_75, SHA512_CONST_76, SHA512_CONST_77,
676*7c478bd9Sstevel@tonic-gate 		SHA512_CONST_78, SHA512_CONST_79
677*7c478bd9Sstevel@tonic-gate 	};
678*7c478bd9Sstevel@tonic-gate #endif
679*7c478bd9Sstevel@tonic-gate 
680*7c478bd9Sstevel@tonic-gate 
681*7c478bd9Sstevel@tonic-gate 	if ((uintptr_t)blk & 0x7) {		/* not 8-byte aligned? */
682*7c478bd9Sstevel@tonic-gate 		bcopy(blk, ctx->buf_un.buf64,  sizeof (ctx->buf_un.buf64));
683*7c478bd9Sstevel@tonic-gate 		blk = (uint8_t *)ctx->buf_un.buf64;
684*7c478bd9Sstevel@tonic-gate 	}
685*7c478bd9Sstevel@tonic-gate 
686*7c478bd9Sstevel@tonic-gate 	w0 =  LOAD_BIG_64(blk + 8 * 0);
687*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(a, b, c, d, e, f, g, h, 0, w0);
688*7c478bd9Sstevel@tonic-gate 	w1 =  LOAD_BIG_64(blk + 8 * 1);
689*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(h, a, b, c, d, e, f, g, 1, w1);
690*7c478bd9Sstevel@tonic-gate 	w2 =  LOAD_BIG_64(blk + 8 * 2);
691*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(g, h, a, b, c, d, e, f, 2, w2);
692*7c478bd9Sstevel@tonic-gate 	w3 =  LOAD_BIG_64(blk + 8 * 3);
693*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(f, g, h, a, b, c, d, e, 3, w3);
694*7c478bd9Sstevel@tonic-gate 	w4 =  LOAD_BIG_64(blk + 8 * 4);
695*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(e, f, g, h, a, b, c, d, 4, w4);
696*7c478bd9Sstevel@tonic-gate 	w5 =  LOAD_BIG_64(blk + 8 * 5);
697*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(d, e, f, g, h, a, b, c, 5, w5);
698*7c478bd9Sstevel@tonic-gate 	w6 =  LOAD_BIG_64(blk + 8 * 6);
699*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(c, d, e, f, g, h, a, b, 6, w6);
700*7c478bd9Sstevel@tonic-gate 	w7 =  LOAD_BIG_64(blk + 8 * 7);
701*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(b, c, d, e, f, g, h, a, 7, w7);
702*7c478bd9Sstevel@tonic-gate 	w8 =  LOAD_BIG_64(blk + 8 * 8);
703*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(a, b, c, d, e, f, g, h, 8, w8);
704*7c478bd9Sstevel@tonic-gate 	w9 =  LOAD_BIG_64(blk + 8 * 9);
705*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(h, a, b, c, d, e, f, g, 9, w9);
706*7c478bd9Sstevel@tonic-gate 	w10 =  LOAD_BIG_64(blk + 8 * 10);
707*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(g, h, a, b, c, d, e, f, 10, w10);
708*7c478bd9Sstevel@tonic-gate 	w11 =  LOAD_BIG_64(blk + 8 * 11);
709*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(f, g, h, a, b, c, d, e, 11, w11);
710*7c478bd9Sstevel@tonic-gate 	w12 =  LOAD_BIG_64(blk + 8 * 12);
711*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(e, f, g, h, a, b, c, d, 12, w12);
712*7c478bd9Sstevel@tonic-gate 	w13 =  LOAD_BIG_64(blk + 8 * 13);
713*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(d, e, f, g, h, a, b, c, 13, w13);
714*7c478bd9Sstevel@tonic-gate 	w14 =  LOAD_BIG_64(blk + 8 * 14);
715*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(c, d, e, f, g, h, a, b, 14, w14);
716*7c478bd9Sstevel@tonic-gate 	w15 =  LOAD_BIG_64(blk + 8 * 15);
717*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(b, c, d, e, f, g, h, a, 15, w15);
718*7c478bd9Sstevel@tonic-gate 
719*7c478bd9Sstevel@tonic-gate 	w0 = SIGMA1(w14) + w9 + SIGMA0(w1) + w0;
720*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(a, b, c, d, e, f, g, h, 16, w0);
721*7c478bd9Sstevel@tonic-gate 	w1 = SIGMA1(w15) + w10 + SIGMA0(w2) + w1;
722*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(h, a, b, c, d, e, f, g, 17, w1);
723*7c478bd9Sstevel@tonic-gate 	w2 = SIGMA1(w0) + w11 + SIGMA0(w3) + w2;
724*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(g, h, a, b, c, d, e, f, 18, w2);
725*7c478bd9Sstevel@tonic-gate 	w3 = SIGMA1(w1) + w12 + SIGMA0(w4) + w3;
726*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(f, g, h, a, b, c, d, e, 19, w3);
727*7c478bd9Sstevel@tonic-gate 	w4 = SIGMA1(w2) + w13 + SIGMA0(w5) + w4;
728*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(e, f, g, h, a, b, c, d, 20, w4);
729*7c478bd9Sstevel@tonic-gate 	w5 = SIGMA1(w3) + w14 + SIGMA0(w6) + w5;
730*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(d, e, f, g, h, a, b, c, 21, w5);
731*7c478bd9Sstevel@tonic-gate 	w6 = SIGMA1(w4) + w15 + SIGMA0(w7) + w6;
732*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(c, d, e, f, g, h, a, b, 22, w6);
733*7c478bd9Sstevel@tonic-gate 	w7 = SIGMA1(w5) + w0 + SIGMA0(w8) + w7;
734*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(b, c, d, e, f, g, h, a, 23, w7);
735*7c478bd9Sstevel@tonic-gate 	w8 = SIGMA1(w6) + w1 + SIGMA0(w9) + w8;
736*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(a, b, c, d, e, f, g, h, 24, w8);
737*7c478bd9Sstevel@tonic-gate 	w9 = SIGMA1(w7) + w2 + SIGMA0(w10) + w9;
738*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(h, a, b, c, d, e, f, g, 25, w9);
739*7c478bd9Sstevel@tonic-gate 	w10 = SIGMA1(w8) + w3 + SIGMA0(w11) + w10;
740*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(g, h, a, b, c, d, e, f, 26, w10);
741*7c478bd9Sstevel@tonic-gate 	w11 = SIGMA1(w9) + w4 + SIGMA0(w12) + w11;
742*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(f, g, h, a, b, c, d, e, 27, w11);
743*7c478bd9Sstevel@tonic-gate 	w12 = SIGMA1(w10) + w5 + SIGMA0(w13) + w12;
744*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(e, f, g, h, a, b, c, d, 28, w12);
745*7c478bd9Sstevel@tonic-gate 	w13 = SIGMA1(w11) + w6 + SIGMA0(w14) + w13;
746*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(d, e, f, g, h, a, b, c, 29, w13);
747*7c478bd9Sstevel@tonic-gate 	w14 = SIGMA1(w12) + w7 + SIGMA0(w15) + w14;
748*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(c, d, e, f, g, h, a, b, 30, w14);
749*7c478bd9Sstevel@tonic-gate 	w15 = SIGMA1(w13) + w8 + SIGMA0(w0) + w15;
750*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(b, c, d, e, f, g, h, a, 31, w15);
751*7c478bd9Sstevel@tonic-gate 
752*7c478bd9Sstevel@tonic-gate 	w0 = SIGMA1(w14) + w9 + SIGMA0(w1) + w0;
753*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(a, b, c, d, e, f, g, h, 32, w0);
754*7c478bd9Sstevel@tonic-gate 	w1 = SIGMA1(w15) + w10 + SIGMA0(w2) + w1;
755*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(h, a, b, c, d, e, f, g, 33, w1);
756*7c478bd9Sstevel@tonic-gate 	w2 = SIGMA1(w0) + w11 + SIGMA0(w3) + w2;
757*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(g, h, a, b, c, d, e, f, 34, w2);
758*7c478bd9Sstevel@tonic-gate 	w3 = SIGMA1(w1) + w12 + SIGMA0(w4) + w3;
759*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(f, g, h, a, b, c, d, e, 35, w3);
760*7c478bd9Sstevel@tonic-gate 	w4 = SIGMA1(w2) + w13 + SIGMA0(w5) + w4;
761*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(e, f, g, h, a, b, c, d, 36, w4);
762*7c478bd9Sstevel@tonic-gate 	w5 = SIGMA1(w3) + w14 + SIGMA0(w6) + w5;
763*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(d, e, f, g, h, a, b, c, 37, w5);
764*7c478bd9Sstevel@tonic-gate 	w6 = SIGMA1(w4) + w15 + SIGMA0(w7) + w6;
765*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(c, d, e, f, g, h, a, b, 38, w6);
766*7c478bd9Sstevel@tonic-gate 	w7 = SIGMA1(w5) + w0 + SIGMA0(w8) + w7;
767*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(b, c, d, e, f, g, h, a, 39, w7);
768*7c478bd9Sstevel@tonic-gate 	w8 = SIGMA1(w6) + w1 + SIGMA0(w9) + w8;
769*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(a, b, c, d, e, f, g, h, 40, w8);
770*7c478bd9Sstevel@tonic-gate 	w9 = SIGMA1(w7) + w2 + SIGMA0(w10) + w9;
771*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(h, a, b, c, d, e, f, g, 41, w9);
772*7c478bd9Sstevel@tonic-gate 	w10 = SIGMA1(w8) + w3 + SIGMA0(w11) + w10;
773*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(g, h, a, b, c, d, e, f, 42, w10);
774*7c478bd9Sstevel@tonic-gate 	w11 = SIGMA1(w9) + w4 + SIGMA0(w12) + w11;
775*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(f, g, h, a, b, c, d, e, 43, w11);
776*7c478bd9Sstevel@tonic-gate 	w12 = SIGMA1(w10) + w5 + SIGMA0(w13) + w12;
777*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(e, f, g, h, a, b, c, d, 44, w12);
778*7c478bd9Sstevel@tonic-gate 	w13 = SIGMA1(w11) + w6 + SIGMA0(w14) + w13;
779*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(d, e, f, g, h, a, b, c, 45, w13);
780*7c478bd9Sstevel@tonic-gate 	w14 = SIGMA1(w12) + w7 + SIGMA0(w15) + w14;
781*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(c, d, e, f, g, h, a, b, 46, w14);
782*7c478bd9Sstevel@tonic-gate 	w15 = SIGMA1(w13) + w8 + SIGMA0(w0) + w15;
783*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(b, c, d, e, f, g, h, a, 47, w15);
784*7c478bd9Sstevel@tonic-gate 
785*7c478bd9Sstevel@tonic-gate 	w0 = SIGMA1(w14) + w9 + SIGMA0(w1) + w0;
786*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(a, b, c, d, e, f, g, h, 48, w0);
787*7c478bd9Sstevel@tonic-gate 	w1 = SIGMA1(w15) + w10 + SIGMA0(w2) + w1;
788*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(h, a, b, c, d, e, f, g, 49, w1);
789*7c478bd9Sstevel@tonic-gate 	w2 = SIGMA1(w0) + w11 + SIGMA0(w3) + w2;
790*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(g, h, a, b, c, d, e, f, 50, w2);
791*7c478bd9Sstevel@tonic-gate 	w3 = SIGMA1(w1) + w12 + SIGMA0(w4) + w3;
792*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(f, g, h, a, b, c, d, e, 51, w3);
793*7c478bd9Sstevel@tonic-gate 	w4 = SIGMA1(w2) + w13 + SIGMA0(w5) + w4;
794*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(e, f, g, h, a, b, c, d, 52, w4);
795*7c478bd9Sstevel@tonic-gate 	w5 = SIGMA1(w3) + w14 + SIGMA0(w6) + w5;
796*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(d, e, f, g, h, a, b, c, 53, w5);
797*7c478bd9Sstevel@tonic-gate 	w6 = SIGMA1(w4) + w15 + SIGMA0(w7) + w6;
798*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(c, d, e, f, g, h, a, b, 54, w6);
799*7c478bd9Sstevel@tonic-gate 	w7 = SIGMA1(w5) + w0 + SIGMA0(w8) + w7;
800*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(b, c, d, e, f, g, h, a, 55, w7);
801*7c478bd9Sstevel@tonic-gate 	w8 = SIGMA1(w6) + w1 + SIGMA0(w9) + w8;
802*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(a, b, c, d, e, f, g, h, 56, w8);
803*7c478bd9Sstevel@tonic-gate 	w9 = SIGMA1(w7) + w2 + SIGMA0(w10) + w9;
804*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(h, a, b, c, d, e, f, g, 57, w9);
805*7c478bd9Sstevel@tonic-gate 	w10 = SIGMA1(w8) + w3 + SIGMA0(w11) + w10;
806*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(g, h, a, b, c, d, e, f, 58, w10);
807*7c478bd9Sstevel@tonic-gate 	w11 = SIGMA1(w9) + w4 + SIGMA0(w12) + w11;
808*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(f, g, h, a, b, c, d, e, 59, w11);
809*7c478bd9Sstevel@tonic-gate 	w12 = SIGMA1(w10) + w5 + SIGMA0(w13) + w12;
810*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(e, f, g, h, a, b, c, d, 60, w12);
811*7c478bd9Sstevel@tonic-gate 	w13 = SIGMA1(w11) + w6 + SIGMA0(w14) + w13;
812*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(d, e, f, g, h, a, b, c, 61, w13);
813*7c478bd9Sstevel@tonic-gate 	w14 = SIGMA1(w12) + w7 + SIGMA0(w15) + w14;
814*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(c, d, e, f, g, h, a, b, 62, w14);
815*7c478bd9Sstevel@tonic-gate 	w15 = SIGMA1(w13) + w8 + SIGMA0(w0) + w15;
816*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(b, c, d, e, f, g, h, a, 63, w15);
817*7c478bd9Sstevel@tonic-gate 
818*7c478bd9Sstevel@tonic-gate 	w0 = SIGMA1(w14) + w9 + SIGMA0(w1) + w0;
819*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(a, b, c, d, e, f, g, h, 64, w0);
820*7c478bd9Sstevel@tonic-gate 	w1 = SIGMA1(w15) + w10 + SIGMA0(w2) + w1;
821*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(h, a, b, c, d, e, f, g, 65, w1);
822*7c478bd9Sstevel@tonic-gate 	w2 = SIGMA1(w0) + w11 + SIGMA0(w3) + w2;
823*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(g, h, a, b, c, d, e, f, 66, w2);
824*7c478bd9Sstevel@tonic-gate 	w3 = SIGMA1(w1) + w12 + SIGMA0(w4) + w3;
825*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(f, g, h, a, b, c, d, e, 67, w3);
826*7c478bd9Sstevel@tonic-gate 	w4 = SIGMA1(w2) + w13 + SIGMA0(w5) + w4;
827*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(e, f, g, h, a, b, c, d, 68, w4);
828*7c478bd9Sstevel@tonic-gate 	w5 = SIGMA1(w3) + w14 + SIGMA0(w6) + w5;
829*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(d, e, f, g, h, a, b, c, 69, w5);
830*7c478bd9Sstevel@tonic-gate 	w6 = SIGMA1(w4) + w15 + SIGMA0(w7) + w6;
831*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(c, d, e, f, g, h, a, b, 70, w6);
832*7c478bd9Sstevel@tonic-gate 	w7 = SIGMA1(w5) + w0 + SIGMA0(w8) + w7;
833*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(b, c, d, e, f, g, h, a, 71, w7);
834*7c478bd9Sstevel@tonic-gate 	w8 = SIGMA1(w6) + w1 + SIGMA0(w9) + w8;
835*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(a, b, c, d, e, f, g, h, 72, w8);
836*7c478bd9Sstevel@tonic-gate 	w9 = SIGMA1(w7) + w2 + SIGMA0(w10) + w9;
837*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(h, a, b, c, d, e, f, g, 73, w9);
838*7c478bd9Sstevel@tonic-gate 	w10 = SIGMA1(w8) + w3 + SIGMA0(w11) + w10;
839*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(g, h, a, b, c, d, e, f, 74, w10);
840*7c478bd9Sstevel@tonic-gate 	w11 = SIGMA1(w9) + w4 + SIGMA0(w12) + w11;
841*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(f, g, h, a, b, c, d, e, 75, w11);
842*7c478bd9Sstevel@tonic-gate 	w12 = SIGMA1(w10) + w5 + SIGMA0(w13) + w12;
843*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(e, f, g, h, a, b, c, d, 76, w12);
844*7c478bd9Sstevel@tonic-gate 	w13 = SIGMA1(w11) + w6 + SIGMA0(w14) + w13;
845*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(d, e, f, g, h, a, b, c, 77, w13);
846*7c478bd9Sstevel@tonic-gate 	w14 = SIGMA1(w12) + w7 + SIGMA0(w15) + w14;
847*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(c, d, e, f, g, h, a, b, 78, w14);
848*7c478bd9Sstevel@tonic-gate 	w15 = SIGMA1(w13) + w8 + SIGMA0(w0) + w15;
849*7c478bd9Sstevel@tonic-gate 	SHA512ROUND(b, c, d, e, f, g, h, a, 79, w15);
850*7c478bd9Sstevel@tonic-gate 
851*7c478bd9Sstevel@tonic-gate 	ctx->state.s64[0] += a;
852*7c478bd9Sstevel@tonic-gate 	ctx->state.s64[1] += b;
853*7c478bd9Sstevel@tonic-gate 	ctx->state.s64[2] += c;
854*7c478bd9Sstevel@tonic-gate 	ctx->state.s64[3] += d;
855*7c478bd9Sstevel@tonic-gate 	ctx->state.s64[4] += e;
856*7c478bd9Sstevel@tonic-gate 	ctx->state.s64[5] += f;
857*7c478bd9Sstevel@tonic-gate 	ctx->state.s64[6] += g;
858*7c478bd9Sstevel@tonic-gate 	ctx->state.s64[7] += h;
859*7c478bd9Sstevel@tonic-gate 
860*7c478bd9Sstevel@tonic-gate }
861*7c478bd9Sstevel@tonic-gate 
862*7c478bd9Sstevel@tonic-gate 
863*7c478bd9Sstevel@tonic-gate /*
864*7c478bd9Sstevel@tonic-gate  * devpro compiler optimization:
865*7c478bd9Sstevel@tonic-gate  *
866*7c478bd9Sstevel@tonic-gate  * the compiler can generate better code if it knows that `input' and
867*7c478bd9Sstevel@tonic-gate  * `output' do not point to the same source.  there is no portable
868*7c478bd9Sstevel@tonic-gate  * way to tell the compiler this, but the sun compiler recognizes the
869*7c478bd9Sstevel@tonic-gate  * `_Restrict' keyword to indicate this condition.  use it if possible.
870*7c478bd9Sstevel@tonic-gate  */
871*7c478bd9Sstevel@tonic-gate 
872*7c478bd9Sstevel@tonic-gate #ifdef	__RESTRICT
873*7c478bd9Sstevel@tonic-gate #define	restrict	_Restrict
874*7c478bd9Sstevel@tonic-gate #else
875*7c478bd9Sstevel@tonic-gate #define	restrict	/* nothing */
876*7c478bd9Sstevel@tonic-gate #endif
877*7c478bd9Sstevel@tonic-gate 
878*7c478bd9Sstevel@tonic-gate /*
879*7c478bd9Sstevel@tonic-gate  * Encode()
880*7c478bd9Sstevel@tonic-gate  *
881*7c478bd9Sstevel@tonic-gate  * purpose: to convert a list of numbers from little endian to big endian
882*7c478bd9Sstevel@tonic-gate  *   input: uint8_t *	: place to store the converted big endian numbers
883*7c478bd9Sstevel@tonic-gate  *	    uint32_t *	: place to get numbers to convert from
884*7c478bd9Sstevel@tonic-gate  *          size_t	: the length of the input in bytes
885*7c478bd9Sstevel@tonic-gate  *  output: void
886*7c478bd9Sstevel@tonic-gate  */
887*7c478bd9Sstevel@tonic-gate 
888*7c478bd9Sstevel@tonic-gate static void
889*7c478bd9Sstevel@tonic-gate Encode(uint8_t *restrict output, uint32_t *restrict input, size_t len)
890*7c478bd9Sstevel@tonic-gate {
891*7c478bd9Sstevel@tonic-gate 	size_t		i, j;
892*7c478bd9Sstevel@tonic-gate 
893*7c478bd9Sstevel@tonic-gate #if	defined(__sparc)
894*7c478bd9Sstevel@tonic-gate 	if (IS_P2ALIGNED(output, sizeof (uint32_t))) {
895*7c478bd9Sstevel@tonic-gate 		for (i = 0, j = 0; j < len; i++, j += 4) {
896*7c478bd9Sstevel@tonic-gate 			/* LINTED: pointer alignment */
897*7c478bd9Sstevel@tonic-gate 			*((uint32_t *)(output + j)) = input[i];
898*7c478bd9Sstevel@tonic-gate 		}
899*7c478bd9Sstevel@tonic-gate 	} else {
900*7c478bd9Sstevel@tonic-gate #endif	/* little endian -- will work on big endian, but slowly */
901*7c478bd9Sstevel@tonic-gate 		for (i = 0, j = 0; j < len; i++, j += 4) {
902*7c478bd9Sstevel@tonic-gate 			output[j]	= (input[i] >> 24) & 0xff;
903*7c478bd9Sstevel@tonic-gate 			output[j + 1]	= (input[i] >> 16) & 0xff;
904*7c478bd9Sstevel@tonic-gate 			output[j + 2]	= (input[i] >>  8) & 0xff;
905*7c478bd9Sstevel@tonic-gate 			output[j + 3]	= input[i] & 0xff;
906*7c478bd9Sstevel@tonic-gate 		}
907*7c478bd9Sstevel@tonic-gate #if	defined(__sparc)
908*7c478bd9Sstevel@tonic-gate 	}
909*7c478bd9Sstevel@tonic-gate #endif
910*7c478bd9Sstevel@tonic-gate }
911*7c478bd9Sstevel@tonic-gate 
912*7c478bd9Sstevel@tonic-gate static void
913*7c478bd9Sstevel@tonic-gate Encode64(uint8_t *restrict output, uint64_t *restrict input, size_t len)
914*7c478bd9Sstevel@tonic-gate {
915*7c478bd9Sstevel@tonic-gate 	size_t		i, j;
916*7c478bd9Sstevel@tonic-gate 
917*7c478bd9Sstevel@tonic-gate #if	defined(__sparc)
918*7c478bd9Sstevel@tonic-gate 	if (IS_P2ALIGNED(output, sizeof (uint64_t))) {
919*7c478bd9Sstevel@tonic-gate 		for (i = 0, j = 0; j < len; i++, j += 8) {
920*7c478bd9Sstevel@tonic-gate 			/* LINTED: pointer alignment */
921*7c478bd9Sstevel@tonic-gate 			*((uint64_t *)(output + j)) = input[i];
922*7c478bd9Sstevel@tonic-gate 		}
923*7c478bd9Sstevel@tonic-gate 	} else {
924*7c478bd9Sstevel@tonic-gate #endif	/* little endian -- will work on big endian, but slowly */
925*7c478bd9Sstevel@tonic-gate 		for (i = 0, j = 0; j < len; i++, j += 8) {
926*7c478bd9Sstevel@tonic-gate 
927*7c478bd9Sstevel@tonic-gate 			output[j]	= (input[i] >> 56) & 0xff;
928*7c478bd9Sstevel@tonic-gate 			output[j + 1]	= (input[i] >> 48) & 0xff;
929*7c478bd9Sstevel@tonic-gate 			output[j + 2]	= (input[i] >> 40) & 0xff;
930*7c478bd9Sstevel@tonic-gate 			output[j + 3]	= (input[i] >> 32) & 0xff;
931*7c478bd9Sstevel@tonic-gate 			output[j + 4]	= (input[i] >> 24) & 0xff;
932*7c478bd9Sstevel@tonic-gate 			output[j + 5]	= (input[i] >> 16) & 0xff;
933*7c478bd9Sstevel@tonic-gate 			output[j + 6]	= (input[i] >>  8) & 0xff;
934*7c478bd9Sstevel@tonic-gate 			output[j + 7]	= input[i] & 0xff;
935*7c478bd9Sstevel@tonic-gate 		}
936*7c478bd9Sstevel@tonic-gate #if	defined(__sparc)
937*7c478bd9Sstevel@tonic-gate 	}
938*7c478bd9Sstevel@tonic-gate #endif
939*7c478bd9Sstevel@tonic-gate }
940*7c478bd9Sstevel@tonic-gate 
941*7c478bd9Sstevel@tonic-gate 
942*7c478bd9Sstevel@tonic-gate #ifdef _KERNEL
943*7c478bd9Sstevel@tonic-gate 
944*7c478bd9Sstevel@tonic-gate /*
945*7c478bd9Sstevel@tonic-gate  * KCF software provider control entry points.
946*7c478bd9Sstevel@tonic-gate  */
947*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
948*7c478bd9Sstevel@tonic-gate static void
949*7c478bd9Sstevel@tonic-gate sha2_provider_status(crypto_provider_handle_t provider, uint_t *status)
950*7c478bd9Sstevel@tonic-gate {
951*7c478bd9Sstevel@tonic-gate 	*status = CRYPTO_PROVIDER_READY;
952*7c478bd9Sstevel@tonic-gate }
953*7c478bd9Sstevel@tonic-gate 
954*7c478bd9Sstevel@tonic-gate /*
955*7c478bd9Sstevel@tonic-gate  * KCF software provider digest entry points.
956*7c478bd9Sstevel@tonic-gate  */
957*7c478bd9Sstevel@tonic-gate 
958*7c478bd9Sstevel@tonic-gate static int
959*7c478bd9Sstevel@tonic-gate sha2_digest_init(crypto_ctx_t *ctx, crypto_mechanism_t *mechanism,
960*7c478bd9Sstevel@tonic-gate     crypto_req_handle_t req)
961*7c478bd9Sstevel@tonic-gate {
962*7c478bd9Sstevel@tonic-gate 
963*7c478bd9Sstevel@tonic-gate 	/*
964*7c478bd9Sstevel@tonic-gate 	 * Allocate and initialize SHA2 context.
965*7c478bd9Sstevel@tonic-gate 	 */
966*7c478bd9Sstevel@tonic-gate 	ctx->cc_provider_private = kmem_alloc(sizeof (sha2_ctx_t),
967*7c478bd9Sstevel@tonic-gate 	    crypto_kmflag(req));
968*7c478bd9Sstevel@tonic-gate 	if (ctx->cc_provider_private == NULL)
969*7c478bd9Sstevel@tonic-gate 		return (CRYPTO_HOST_MEMORY);
970*7c478bd9Sstevel@tonic-gate 
971*7c478bd9Sstevel@tonic-gate 	PROV_SHA2_CTX(ctx)->sc_mech_type = mechanism->cm_type;
972*7c478bd9Sstevel@tonic-gate 	SHA2Init(mechanism->cm_type, &PROV_SHA2_CTX(ctx)->sc_sha2_ctx);
973*7c478bd9Sstevel@tonic-gate 
974*7c478bd9Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
975*7c478bd9Sstevel@tonic-gate }
976*7c478bd9Sstevel@tonic-gate 
977*7c478bd9Sstevel@tonic-gate /*
978*7c478bd9Sstevel@tonic-gate  * Helper SHA2 digest update function for uio data.
979*7c478bd9Sstevel@tonic-gate  */
980*7c478bd9Sstevel@tonic-gate static int
981*7c478bd9Sstevel@tonic-gate sha2_digest_update_uio(SHA2_CTX *sha2_ctx, crypto_data_t *data)
982*7c478bd9Sstevel@tonic-gate {
983*7c478bd9Sstevel@tonic-gate 	off_t offset = data->cd_offset;
984*7c478bd9Sstevel@tonic-gate 	size_t length = data->cd_length;
985*7c478bd9Sstevel@tonic-gate 	uint_t vec_idx;
986*7c478bd9Sstevel@tonic-gate 	size_t cur_len;
987*7c478bd9Sstevel@tonic-gate 
988*7c478bd9Sstevel@tonic-gate 	/* we support only kernel buffer */
989*7c478bd9Sstevel@tonic-gate 	if (data->cd_uio->uio_segflg != UIO_SYSSPACE)
990*7c478bd9Sstevel@tonic-gate 		return (CRYPTO_ARGUMENTS_BAD);
991*7c478bd9Sstevel@tonic-gate 
992*7c478bd9Sstevel@tonic-gate 	/*
993*7c478bd9Sstevel@tonic-gate 	 * Jump to the first iovec containing data to be
994*7c478bd9Sstevel@tonic-gate 	 * digested.
995*7c478bd9Sstevel@tonic-gate 	 */
996*7c478bd9Sstevel@tonic-gate 	for (vec_idx = 0; vec_idx < data->cd_uio->uio_iovcnt &&
997*7c478bd9Sstevel@tonic-gate 	    offset >= data->cd_uio->uio_iov[vec_idx].iov_len;
998*7c478bd9Sstevel@tonic-gate 	    offset -= data->cd_uio->uio_iov[vec_idx++].iov_len);
999*7c478bd9Sstevel@tonic-gate 	if (vec_idx == data->cd_uio->uio_iovcnt) {
1000*7c478bd9Sstevel@tonic-gate 		/*
1001*7c478bd9Sstevel@tonic-gate 		 * The caller specified an offset that is larger than the
1002*7c478bd9Sstevel@tonic-gate 		 * total size of the buffers it provided.
1003*7c478bd9Sstevel@tonic-gate 		 */
1004*7c478bd9Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
1005*7c478bd9Sstevel@tonic-gate 	}
1006*7c478bd9Sstevel@tonic-gate 
1007*7c478bd9Sstevel@tonic-gate 	/*
1008*7c478bd9Sstevel@tonic-gate 	 * Now do the digesting on the iovecs.
1009*7c478bd9Sstevel@tonic-gate 	 */
1010*7c478bd9Sstevel@tonic-gate 	while (vec_idx < data->cd_uio->uio_iovcnt && length > 0) {
1011*7c478bd9Sstevel@tonic-gate 		cur_len = MIN(data->cd_uio->uio_iov[vec_idx].iov_len -
1012*7c478bd9Sstevel@tonic-gate 		    offset, length);
1013*7c478bd9Sstevel@tonic-gate 
1014*7c478bd9Sstevel@tonic-gate 		SHA2Update(sha2_ctx, (uint8_t *)data->cd_uio->
1015*7c478bd9Sstevel@tonic-gate 		    uio_iov[vec_idx].iov_base + offset, cur_len);
1016*7c478bd9Sstevel@tonic-gate 		length -= cur_len;
1017*7c478bd9Sstevel@tonic-gate 		vec_idx++;
1018*7c478bd9Sstevel@tonic-gate 		offset = 0;
1019*7c478bd9Sstevel@tonic-gate 	}
1020*7c478bd9Sstevel@tonic-gate 
1021*7c478bd9Sstevel@tonic-gate 	if (vec_idx == data->cd_uio->uio_iovcnt && length > 0) {
1022*7c478bd9Sstevel@tonic-gate 		/*
1023*7c478bd9Sstevel@tonic-gate 		 * The end of the specified iovec's was reached but
1024*7c478bd9Sstevel@tonic-gate 		 * the length requested could not be processed, i.e.
1025*7c478bd9Sstevel@tonic-gate 		 * The caller requested to digest more data than it provided.
1026*7c478bd9Sstevel@tonic-gate 		 */
1027*7c478bd9Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
1028*7c478bd9Sstevel@tonic-gate 	}
1029*7c478bd9Sstevel@tonic-gate 
1030*7c478bd9Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
1031*7c478bd9Sstevel@tonic-gate }
1032*7c478bd9Sstevel@tonic-gate 
1033*7c478bd9Sstevel@tonic-gate /*
1034*7c478bd9Sstevel@tonic-gate  * Helper SHA2 digest final function for uio data.
1035*7c478bd9Sstevel@tonic-gate  * digest_len is the length of the desired digest. If digest_len
1036*7c478bd9Sstevel@tonic-gate  * is smaller than the default SHA2 digest length, the caller
1037*7c478bd9Sstevel@tonic-gate  * must pass a scratch buffer, digest_scratch, which must
1038*7c478bd9Sstevel@tonic-gate  * be at least the algorithm's digest length bytes.
1039*7c478bd9Sstevel@tonic-gate  */
1040*7c478bd9Sstevel@tonic-gate static int
1041*7c478bd9Sstevel@tonic-gate sha2_digest_final_uio(SHA2_CTX *sha2_ctx, crypto_data_t *digest,
1042*7c478bd9Sstevel@tonic-gate     ulong_t digest_len, uchar_t *digest_scratch)
1043*7c478bd9Sstevel@tonic-gate {
1044*7c478bd9Sstevel@tonic-gate 	off_t offset = digest->cd_offset;
1045*7c478bd9Sstevel@tonic-gate 	uint_t vec_idx;
1046*7c478bd9Sstevel@tonic-gate 
1047*7c478bd9Sstevel@tonic-gate 	/* we support only kernel buffer */
1048*7c478bd9Sstevel@tonic-gate 	if (digest->cd_uio->uio_segflg != UIO_SYSSPACE)
1049*7c478bd9Sstevel@tonic-gate 		return (CRYPTO_ARGUMENTS_BAD);
1050*7c478bd9Sstevel@tonic-gate 
1051*7c478bd9Sstevel@tonic-gate 	/*
1052*7c478bd9Sstevel@tonic-gate 	 * Jump to the first iovec containing ptr to the digest to
1053*7c478bd9Sstevel@tonic-gate 	 * be returned.
1054*7c478bd9Sstevel@tonic-gate 	 */
1055*7c478bd9Sstevel@tonic-gate 	for (vec_idx = 0; offset >= digest->cd_uio->uio_iov[vec_idx].iov_len &&
1056*7c478bd9Sstevel@tonic-gate 	    vec_idx < digest->cd_uio->uio_iovcnt;
1057*7c478bd9Sstevel@tonic-gate 	    offset -= digest->cd_uio->uio_iov[vec_idx++].iov_len);
1058*7c478bd9Sstevel@tonic-gate 	if (vec_idx == digest->cd_uio->uio_iovcnt) {
1059*7c478bd9Sstevel@tonic-gate 		/*
1060*7c478bd9Sstevel@tonic-gate 		 * The caller specified an offset that is
1061*7c478bd9Sstevel@tonic-gate 		 * larger than the total size of the buffers
1062*7c478bd9Sstevel@tonic-gate 		 * it provided.
1063*7c478bd9Sstevel@tonic-gate 		 */
1064*7c478bd9Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
1065*7c478bd9Sstevel@tonic-gate 	}
1066*7c478bd9Sstevel@tonic-gate 
1067*7c478bd9Sstevel@tonic-gate 	if (offset + digest_len <=
1068*7c478bd9Sstevel@tonic-gate 	    digest->cd_uio->uio_iov[vec_idx].iov_len) {
1069*7c478bd9Sstevel@tonic-gate 		/*
1070*7c478bd9Sstevel@tonic-gate 		 * The computed SHA2 digest will fit in the current
1071*7c478bd9Sstevel@tonic-gate 		 * iovec.
1072*7c478bd9Sstevel@tonic-gate 		 */
1073*7c478bd9Sstevel@tonic-gate 		if (((sha2_ctx->algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE) &&
1074*7c478bd9Sstevel@tonic-gate 		    (digest_len != SHA256_DIGEST_LENGTH)) ||
1075*7c478bd9Sstevel@tonic-gate 		    ((sha2_ctx->algotype > SHA256_HMAC_GEN_MECH_INFO_TYPE) &&
1076*7c478bd9Sstevel@tonic-gate 			(digest_len != SHA512_DIGEST_LENGTH))) {
1077*7c478bd9Sstevel@tonic-gate 			/*
1078*7c478bd9Sstevel@tonic-gate 			 * The caller requested a short digest. Digest
1079*7c478bd9Sstevel@tonic-gate 			 * into a scratch buffer and return to
1080*7c478bd9Sstevel@tonic-gate 			 * the user only what was requested.
1081*7c478bd9Sstevel@tonic-gate 			 */
1082*7c478bd9Sstevel@tonic-gate 			SHA2Final(digest_scratch, sha2_ctx);
1083*7c478bd9Sstevel@tonic-gate 
1084*7c478bd9Sstevel@tonic-gate 			bcopy(digest_scratch, (uchar_t *)digest->
1085*7c478bd9Sstevel@tonic-gate 			    cd_uio->uio_iov[vec_idx].iov_base + offset,
1086*7c478bd9Sstevel@tonic-gate 			    digest_len);
1087*7c478bd9Sstevel@tonic-gate 		} else {
1088*7c478bd9Sstevel@tonic-gate 			SHA2Final((uchar_t *)digest->
1089*7c478bd9Sstevel@tonic-gate 			    cd_uio->uio_iov[vec_idx].iov_base + offset,
1090*7c478bd9Sstevel@tonic-gate 			    sha2_ctx);
1091*7c478bd9Sstevel@tonic-gate 
1092*7c478bd9Sstevel@tonic-gate 		}
1093*7c478bd9Sstevel@tonic-gate 	} else {
1094*7c478bd9Sstevel@tonic-gate 		/*
1095*7c478bd9Sstevel@tonic-gate 		 * The computed digest will be crossing one or more iovec's.
1096*7c478bd9Sstevel@tonic-gate 		 * This is bad performance-wise but we need to support it.
1097*7c478bd9Sstevel@tonic-gate 		 * Allocate a small scratch buffer on the stack and
1098*7c478bd9Sstevel@tonic-gate 		 * copy it piece meal to the specified digest iovec's.
1099*7c478bd9Sstevel@tonic-gate 		 */
1100*7c478bd9Sstevel@tonic-gate 		uchar_t digest_tmp[SHA512_DIGEST_LENGTH];
1101*7c478bd9Sstevel@tonic-gate 		off_t scratch_offset = 0;
1102*7c478bd9Sstevel@tonic-gate 		size_t length = digest_len;
1103*7c478bd9Sstevel@tonic-gate 		size_t cur_len;
1104*7c478bd9Sstevel@tonic-gate 
1105*7c478bd9Sstevel@tonic-gate 		SHA2Final(digest_tmp, sha2_ctx);
1106*7c478bd9Sstevel@tonic-gate 
1107*7c478bd9Sstevel@tonic-gate 		while (vec_idx < digest->cd_uio->uio_iovcnt && length > 0) {
1108*7c478bd9Sstevel@tonic-gate 			cur_len =
1109*7c478bd9Sstevel@tonic-gate 			    MIN(digest->cd_uio->uio_iov[vec_idx].iov_len -
1110*7c478bd9Sstevel@tonic-gate 				    offset, length);
1111*7c478bd9Sstevel@tonic-gate 			bcopy(digest_tmp + scratch_offset,
1112*7c478bd9Sstevel@tonic-gate 			    digest->cd_uio->uio_iov[vec_idx].iov_base + offset,
1113*7c478bd9Sstevel@tonic-gate 			    cur_len);
1114*7c478bd9Sstevel@tonic-gate 
1115*7c478bd9Sstevel@tonic-gate 			length -= cur_len;
1116*7c478bd9Sstevel@tonic-gate 			vec_idx++;
1117*7c478bd9Sstevel@tonic-gate 			scratch_offset += cur_len;
1118*7c478bd9Sstevel@tonic-gate 			offset = 0;
1119*7c478bd9Sstevel@tonic-gate 		}
1120*7c478bd9Sstevel@tonic-gate 
1121*7c478bd9Sstevel@tonic-gate 		if (vec_idx == digest->cd_uio->uio_iovcnt && length > 0) {
1122*7c478bd9Sstevel@tonic-gate 			/*
1123*7c478bd9Sstevel@tonic-gate 			 * The end of the specified iovec's was reached but
1124*7c478bd9Sstevel@tonic-gate 			 * the length requested could not be processed, i.e.
1125*7c478bd9Sstevel@tonic-gate 			 * The caller requested to digest more data than it
1126*7c478bd9Sstevel@tonic-gate 			 * provided.
1127*7c478bd9Sstevel@tonic-gate 			 */
1128*7c478bd9Sstevel@tonic-gate 			return (CRYPTO_DATA_LEN_RANGE);
1129*7c478bd9Sstevel@tonic-gate 		}
1130*7c478bd9Sstevel@tonic-gate 	}
1131*7c478bd9Sstevel@tonic-gate 
1132*7c478bd9Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
1133*7c478bd9Sstevel@tonic-gate }
1134*7c478bd9Sstevel@tonic-gate 
1135*7c478bd9Sstevel@tonic-gate /*
1136*7c478bd9Sstevel@tonic-gate  * Helper SHA2 digest update for mblk's.
1137*7c478bd9Sstevel@tonic-gate  */
1138*7c478bd9Sstevel@tonic-gate static int
1139*7c478bd9Sstevel@tonic-gate sha2_digest_update_mblk(SHA2_CTX *sha2_ctx, crypto_data_t *data)
1140*7c478bd9Sstevel@tonic-gate {
1141*7c478bd9Sstevel@tonic-gate 	off_t offset = data->cd_offset;
1142*7c478bd9Sstevel@tonic-gate 	size_t length = data->cd_length;
1143*7c478bd9Sstevel@tonic-gate 	mblk_t *mp;
1144*7c478bd9Sstevel@tonic-gate 	size_t cur_len;
1145*7c478bd9Sstevel@tonic-gate 
1146*7c478bd9Sstevel@tonic-gate 	/*
1147*7c478bd9Sstevel@tonic-gate 	 * Jump to the first mblk_t containing data to be digested.
1148*7c478bd9Sstevel@tonic-gate 	 */
1149*7c478bd9Sstevel@tonic-gate 	for (mp = data->cd_mp; mp != NULL && offset >= MBLKL(mp);
1150*7c478bd9Sstevel@tonic-gate 	    offset -= MBLKL(mp), mp = mp->b_cont);
1151*7c478bd9Sstevel@tonic-gate 	if (mp == NULL) {
1152*7c478bd9Sstevel@tonic-gate 		/*
1153*7c478bd9Sstevel@tonic-gate 		 * The caller specified an offset that is larger than the
1154*7c478bd9Sstevel@tonic-gate 		 * total size of the buffers it provided.
1155*7c478bd9Sstevel@tonic-gate 		 */
1156*7c478bd9Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
1157*7c478bd9Sstevel@tonic-gate 	}
1158*7c478bd9Sstevel@tonic-gate 
1159*7c478bd9Sstevel@tonic-gate 	/*
1160*7c478bd9Sstevel@tonic-gate 	 * Now do the digesting on the mblk chain.
1161*7c478bd9Sstevel@tonic-gate 	 */
1162*7c478bd9Sstevel@tonic-gate 	while (mp != NULL && length > 0) {
1163*7c478bd9Sstevel@tonic-gate 		cur_len = MIN(MBLKL(mp) - offset, length);
1164*7c478bd9Sstevel@tonic-gate 		SHA2Update(sha2_ctx, mp->b_rptr + offset, cur_len);
1165*7c478bd9Sstevel@tonic-gate 		length -= cur_len;
1166*7c478bd9Sstevel@tonic-gate 		offset = 0;
1167*7c478bd9Sstevel@tonic-gate 		mp = mp->b_cont;
1168*7c478bd9Sstevel@tonic-gate 	}
1169*7c478bd9Sstevel@tonic-gate 
1170*7c478bd9Sstevel@tonic-gate 	if (mp == NULL && length > 0) {
1171*7c478bd9Sstevel@tonic-gate 		/*
1172*7c478bd9Sstevel@tonic-gate 		 * The end of the mblk was reached but the length requested
1173*7c478bd9Sstevel@tonic-gate 		 * could not be processed, i.e. The caller requested
1174*7c478bd9Sstevel@tonic-gate 		 * to digest more data than it provided.
1175*7c478bd9Sstevel@tonic-gate 		 */
1176*7c478bd9Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
1177*7c478bd9Sstevel@tonic-gate 	}
1178*7c478bd9Sstevel@tonic-gate 
1179*7c478bd9Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
1180*7c478bd9Sstevel@tonic-gate }
1181*7c478bd9Sstevel@tonic-gate 
1182*7c478bd9Sstevel@tonic-gate /*
1183*7c478bd9Sstevel@tonic-gate  * Helper SHA2 digest final for mblk's.
1184*7c478bd9Sstevel@tonic-gate  * digest_len is the length of the desired digest. If digest_len
1185*7c478bd9Sstevel@tonic-gate  * is smaller than the default SHA2 digest length, the caller
1186*7c478bd9Sstevel@tonic-gate  * must pass a scratch buffer, digest_scratch, which must
1187*7c478bd9Sstevel@tonic-gate  * be at least the algorithm's digest length bytes.
1188*7c478bd9Sstevel@tonic-gate  */
1189*7c478bd9Sstevel@tonic-gate static int
1190*7c478bd9Sstevel@tonic-gate sha2_digest_final_mblk(SHA2_CTX *sha2_ctx, crypto_data_t *digest,
1191*7c478bd9Sstevel@tonic-gate     ulong_t digest_len, uchar_t *digest_scratch)
1192*7c478bd9Sstevel@tonic-gate {
1193*7c478bd9Sstevel@tonic-gate 	off_t offset = digest->cd_offset;
1194*7c478bd9Sstevel@tonic-gate 	mblk_t *mp;
1195*7c478bd9Sstevel@tonic-gate 
1196*7c478bd9Sstevel@tonic-gate 	/*
1197*7c478bd9Sstevel@tonic-gate 	 * Jump to the first mblk_t that will be used to store the digest.
1198*7c478bd9Sstevel@tonic-gate 	 */
1199*7c478bd9Sstevel@tonic-gate 	for (mp = digest->cd_mp; mp != NULL && offset >= MBLKL(mp);
1200*7c478bd9Sstevel@tonic-gate 	    offset -= MBLKL(mp), mp = mp->b_cont);
1201*7c478bd9Sstevel@tonic-gate 	if (mp == NULL) {
1202*7c478bd9Sstevel@tonic-gate 		/*
1203*7c478bd9Sstevel@tonic-gate 		 * The caller specified an offset that is larger than the
1204*7c478bd9Sstevel@tonic-gate 		 * total size of the buffers it provided.
1205*7c478bd9Sstevel@tonic-gate 		 */
1206*7c478bd9Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
1207*7c478bd9Sstevel@tonic-gate 	}
1208*7c478bd9Sstevel@tonic-gate 
1209*7c478bd9Sstevel@tonic-gate 	if (offset + digest_len <= MBLKL(mp)) {
1210*7c478bd9Sstevel@tonic-gate 		/*
1211*7c478bd9Sstevel@tonic-gate 		 * The computed SHA2 digest will fit in the current mblk.
1212*7c478bd9Sstevel@tonic-gate 		 * Do the SHA2Final() in-place.
1213*7c478bd9Sstevel@tonic-gate 		 */
1214*7c478bd9Sstevel@tonic-gate 		if (((sha2_ctx->algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE) &&
1215*7c478bd9Sstevel@tonic-gate 		    (digest_len != SHA256_DIGEST_LENGTH)) ||
1216*7c478bd9Sstevel@tonic-gate 		    ((sha2_ctx->algotype > SHA256_HMAC_GEN_MECH_INFO_TYPE) &&
1217*7c478bd9Sstevel@tonic-gate 			(digest_len != SHA512_DIGEST_LENGTH))) {
1218*7c478bd9Sstevel@tonic-gate 			/*
1219*7c478bd9Sstevel@tonic-gate 			 * The caller requested a short digest. Digest
1220*7c478bd9Sstevel@tonic-gate 			 * into a scratch buffer and return to
1221*7c478bd9Sstevel@tonic-gate 			 * the user only what was requested.
1222*7c478bd9Sstevel@tonic-gate 			 */
1223*7c478bd9Sstevel@tonic-gate 			SHA2Final(digest_scratch, sha2_ctx);
1224*7c478bd9Sstevel@tonic-gate 			bcopy(digest_scratch, mp->b_rptr + offset, digest_len);
1225*7c478bd9Sstevel@tonic-gate 		} else {
1226*7c478bd9Sstevel@tonic-gate 			SHA2Final(mp->b_rptr + offset, sha2_ctx);
1227*7c478bd9Sstevel@tonic-gate 		}
1228*7c478bd9Sstevel@tonic-gate 	} else {
1229*7c478bd9Sstevel@tonic-gate 		/*
1230*7c478bd9Sstevel@tonic-gate 		 * The computed digest will be crossing one or more mblk's.
1231*7c478bd9Sstevel@tonic-gate 		 * This is bad performance-wise but we need to support it.
1232*7c478bd9Sstevel@tonic-gate 		 * Allocate a small scratch buffer on the stack and
1233*7c478bd9Sstevel@tonic-gate 		 * copy it piece meal to the specified digest iovec's.
1234*7c478bd9Sstevel@tonic-gate 		 */
1235*7c478bd9Sstevel@tonic-gate 		uchar_t digest_tmp[SHA512_DIGEST_LENGTH];
1236*7c478bd9Sstevel@tonic-gate 		off_t scratch_offset = 0;
1237*7c478bd9Sstevel@tonic-gate 		size_t length = digest_len;
1238*7c478bd9Sstevel@tonic-gate 		size_t cur_len;
1239*7c478bd9Sstevel@tonic-gate 
1240*7c478bd9Sstevel@tonic-gate 		SHA2Final(digest_tmp, sha2_ctx);
1241*7c478bd9Sstevel@tonic-gate 
1242*7c478bd9Sstevel@tonic-gate 		while (mp != NULL && length > 0) {
1243*7c478bd9Sstevel@tonic-gate 			cur_len = MIN(MBLKL(mp) - offset, length);
1244*7c478bd9Sstevel@tonic-gate 			bcopy(digest_tmp + scratch_offset,
1245*7c478bd9Sstevel@tonic-gate 			    mp->b_rptr + offset, cur_len);
1246*7c478bd9Sstevel@tonic-gate 
1247*7c478bd9Sstevel@tonic-gate 			length -= cur_len;
1248*7c478bd9Sstevel@tonic-gate 			mp = mp->b_cont;
1249*7c478bd9Sstevel@tonic-gate 			scratch_offset += cur_len;
1250*7c478bd9Sstevel@tonic-gate 			offset = 0;
1251*7c478bd9Sstevel@tonic-gate 		}
1252*7c478bd9Sstevel@tonic-gate 
1253*7c478bd9Sstevel@tonic-gate 		if (mp == NULL && length > 0) {
1254*7c478bd9Sstevel@tonic-gate 			/*
1255*7c478bd9Sstevel@tonic-gate 			 * The end of the specified mblk was reached but
1256*7c478bd9Sstevel@tonic-gate 			 * the length requested could not be processed, i.e.
1257*7c478bd9Sstevel@tonic-gate 			 * The caller requested to digest more data than it
1258*7c478bd9Sstevel@tonic-gate 			 * provided.
1259*7c478bd9Sstevel@tonic-gate 			 */
1260*7c478bd9Sstevel@tonic-gate 			return (CRYPTO_DATA_LEN_RANGE);
1261*7c478bd9Sstevel@tonic-gate 		}
1262*7c478bd9Sstevel@tonic-gate 	}
1263*7c478bd9Sstevel@tonic-gate 
1264*7c478bd9Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
1265*7c478bd9Sstevel@tonic-gate }
1266*7c478bd9Sstevel@tonic-gate 
1267*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
1268*7c478bd9Sstevel@tonic-gate static int
1269*7c478bd9Sstevel@tonic-gate sha2_digest(crypto_ctx_t *ctx, crypto_data_t *data, crypto_data_t *digest,
1270*7c478bd9Sstevel@tonic-gate     crypto_req_handle_t req)
1271*7c478bd9Sstevel@tonic-gate {
1272*7c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
1273*7c478bd9Sstevel@tonic-gate 	uint_t sha_digest_len;
1274*7c478bd9Sstevel@tonic-gate 
1275*7c478bd9Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
1276*7c478bd9Sstevel@tonic-gate 
1277*7c478bd9Sstevel@tonic-gate 	switch (PROV_SHA2_CTX(ctx)->sc_mech_type) {
1278*7c478bd9Sstevel@tonic-gate 	case SHA256_MECH_INFO_TYPE:
1279*7c478bd9Sstevel@tonic-gate 		sha_digest_len = SHA256_DIGEST_LENGTH;
1280*7c478bd9Sstevel@tonic-gate 		break;
1281*7c478bd9Sstevel@tonic-gate 	case SHA384_MECH_INFO_TYPE:
1282*7c478bd9Sstevel@tonic-gate 		sha_digest_len = SHA384_DIGEST_LENGTH;
1283*7c478bd9Sstevel@tonic-gate 		break;
1284*7c478bd9Sstevel@tonic-gate 	case SHA512_MECH_INFO_TYPE:
1285*7c478bd9Sstevel@tonic-gate 		sha_digest_len = SHA512_DIGEST_LENGTH;
1286*7c478bd9Sstevel@tonic-gate 		break;
1287*7c478bd9Sstevel@tonic-gate 	default:
1288*7c478bd9Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
1289*7c478bd9Sstevel@tonic-gate 	}
1290*7c478bd9Sstevel@tonic-gate 
1291*7c478bd9Sstevel@tonic-gate 	/*
1292*7c478bd9Sstevel@tonic-gate 	 * We need to just return the length needed to store the output.
1293*7c478bd9Sstevel@tonic-gate 	 * We should not destroy the context for the following cases.
1294*7c478bd9Sstevel@tonic-gate 	 */
1295*7c478bd9Sstevel@tonic-gate 	if ((digest->cd_length == 0) ||
1296*7c478bd9Sstevel@tonic-gate 	    (digest->cd_length < sha_digest_len)) {
1297*7c478bd9Sstevel@tonic-gate 		digest->cd_length = sha_digest_len;
1298*7c478bd9Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
1299*7c478bd9Sstevel@tonic-gate 	}
1300*7c478bd9Sstevel@tonic-gate 
1301*7c478bd9Sstevel@tonic-gate 	/*
1302*7c478bd9Sstevel@tonic-gate 	 * Do the SHA2 update on the specified input data.
1303*7c478bd9Sstevel@tonic-gate 	 */
1304*7c478bd9Sstevel@tonic-gate 	switch (data->cd_format) {
1305*7c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
1306*7c478bd9Sstevel@tonic-gate 		SHA2Update(&PROV_SHA2_CTX(ctx)->sc_sha2_ctx,
1307*7c478bd9Sstevel@tonic-gate 		    (uint8_t *)data->cd_raw.iov_base + data->cd_offset,
1308*7c478bd9Sstevel@tonic-gate 		    data->cd_length);
1309*7c478bd9Sstevel@tonic-gate 		break;
1310*7c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
1311*7c478bd9Sstevel@tonic-gate 		ret = sha2_digest_update_uio(&PROV_SHA2_CTX(ctx)->sc_sha2_ctx,
1312*7c478bd9Sstevel@tonic-gate 		    data);
1313*7c478bd9Sstevel@tonic-gate 		break;
1314*7c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
1315*7c478bd9Sstevel@tonic-gate 		ret = sha2_digest_update_mblk(&PROV_SHA2_CTX(ctx)->sc_sha2_ctx,
1316*7c478bd9Sstevel@tonic-gate 		    data);
1317*7c478bd9Sstevel@tonic-gate 		break;
1318*7c478bd9Sstevel@tonic-gate 	default:
1319*7c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
1320*7c478bd9Sstevel@tonic-gate 	}
1321*7c478bd9Sstevel@tonic-gate 
1322*7c478bd9Sstevel@tonic-gate 	if (ret != CRYPTO_SUCCESS) {
1323*7c478bd9Sstevel@tonic-gate 		/* the update failed, free context and bail */
1324*7c478bd9Sstevel@tonic-gate 		bzero(&PROV_SHA2_CTX(ctx)->sc_sha2_ctx, sizeof (SHA2_CTX));
1325*7c478bd9Sstevel@tonic-gate 		kmem_free(ctx->cc_provider_private, sizeof (sha2_ctx_t));
1326*7c478bd9Sstevel@tonic-gate 		ctx->cc_provider_private = NULL;
1327*7c478bd9Sstevel@tonic-gate 		digest->cd_length = 0;
1328*7c478bd9Sstevel@tonic-gate 		return (ret);
1329*7c478bd9Sstevel@tonic-gate 	}
1330*7c478bd9Sstevel@tonic-gate 
1331*7c478bd9Sstevel@tonic-gate 	/*
1332*7c478bd9Sstevel@tonic-gate 	 * Do a SHA2 final, must be done separately since the digest
1333*7c478bd9Sstevel@tonic-gate 	 * type can be different than the input data type.
1334*7c478bd9Sstevel@tonic-gate 	 */
1335*7c478bd9Sstevel@tonic-gate 	switch (digest->cd_format) {
1336*7c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
1337*7c478bd9Sstevel@tonic-gate 		SHA2Final((unsigned char *)digest->cd_raw.iov_base +
1338*7c478bd9Sstevel@tonic-gate 		    digest->cd_offset, &PROV_SHA2_CTX(ctx)->sc_sha2_ctx);
1339*7c478bd9Sstevel@tonic-gate 		break;
1340*7c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
1341*7c478bd9Sstevel@tonic-gate 		ret = sha2_digest_final_uio(&PROV_SHA2_CTX(ctx)->sc_sha2_ctx,
1342*7c478bd9Sstevel@tonic-gate 		    digest, sha_digest_len, NULL);
1343*7c478bd9Sstevel@tonic-gate 		break;
1344*7c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
1345*7c478bd9Sstevel@tonic-gate 		ret = sha2_digest_final_mblk(&PROV_SHA2_CTX(ctx)->sc_sha2_ctx,
1346*7c478bd9Sstevel@tonic-gate 		    digest, sha_digest_len, NULL);
1347*7c478bd9Sstevel@tonic-gate 		break;
1348*7c478bd9Sstevel@tonic-gate 	default:
1349*7c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
1350*7c478bd9Sstevel@tonic-gate 	}
1351*7c478bd9Sstevel@tonic-gate 
1352*7c478bd9Sstevel@tonic-gate 	/* all done, free context and return */
1353*7c478bd9Sstevel@tonic-gate 
1354*7c478bd9Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
1355*7c478bd9Sstevel@tonic-gate 		digest->cd_length = sha_digest_len;
1356*7c478bd9Sstevel@tonic-gate 	} else {
1357*7c478bd9Sstevel@tonic-gate 		/*
1358*7c478bd9Sstevel@tonic-gate 		 * Only bzero context on failure, since SHA2Final()
1359*7c478bd9Sstevel@tonic-gate 		 * does it for us.
1360*7c478bd9Sstevel@tonic-gate 		 */
1361*7c478bd9Sstevel@tonic-gate 		bzero(&PROV_SHA2_CTX(ctx)->sc_sha2_ctx, sizeof (SHA2_CTX));
1362*7c478bd9Sstevel@tonic-gate 		digest->cd_length = 0;
1363*7c478bd9Sstevel@tonic-gate 	}
1364*7c478bd9Sstevel@tonic-gate 
1365*7c478bd9Sstevel@tonic-gate 	kmem_free(ctx->cc_provider_private, sizeof (sha2_ctx_t));
1366*7c478bd9Sstevel@tonic-gate 	ctx->cc_provider_private = NULL;
1367*7c478bd9Sstevel@tonic-gate 	return (ret);
1368*7c478bd9Sstevel@tonic-gate }
1369*7c478bd9Sstevel@tonic-gate 
1370*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
1371*7c478bd9Sstevel@tonic-gate static int
1372*7c478bd9Sstevel@tonic-gate sha2_digest_update(crypto_ctx_t *ctx, crypto_data_t *data,
1373*7c478bd9Sstevel@tonic-gate     crypto_req_handle_t req)
1374*7c478bd9Sstevel@tonic-gate {
1375*7c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
1376*7c478bd9Sstevel@tonic-gate 
1377*7c478bd9Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
1378*7c478bd9Sstevel@tonic-gate 
1379*7c478bd9Sstevel@tonic-gate 	/*
1380*7c478bd9Sstevel@tonic-gate 	 * Do the SHA2 update on the specified input data.
1381*7c478bd9Sstevel@tonic-gate 	 */
1382*7c478bd9Sstevel@tonic-gate 	switch (data->cd_format) {
1383*7c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
1384*7c478bd9Sstevel@tonic-gate 		SHA2Update(&PROV_SHA2_CTX(ctx)->sc_sha2_ctx,
1385*7c478bd9Sstevel@tonic-gate 		    (uint8_t *)data->cd_raw.iov_base + data->cd_offset,
1386*7c478bd9Sstevel@tonic-gate 		    data->cd_length);
1387*7c478bd9Sstevel@tonic-gate 		break;
1388*7c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
1389*7c478bd9Sstevel@tonic-gate 		ret = sha2_digest_update_uio(&PROV_SHA2_CTX(ctx)->sc_sha2_ctx,
1390*7c478bd9Sstevel@tonic-gate 		    data);
1391*7c478bd9Sstevel@tonic-gate 		break;
1392*7c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
1393*7c478bd9Sstevel@tonic-gate 		ret = sha2_digest_update_mblk(&PROV_SHA2_CTX(ctx)->sc_sha2_ctx,
1394*7c478bd9Sstevel@tonic-gate 		    data);
1395*7c478bd9Sstevel@tonic-gate 		break;
1396*7c478bd9Sstevel@tonic-gate 	default:
1397*7c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
1398*7c478bd9Sstevel@tonic-gate 	}
1399*7c478bd9Sstevel@tonic-gate 
1400*7c478bd9Sstevel@tonic-gate 	return (ret);
1401*7c478bd9Sstevel@tonic-gate }
1402*7c478bd9Sstevel@tonic-gate 
1403*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
1404*7c478bd9Sstevel@tonic-gate static int
1405*7c478bd9Sstevel@tonic-gate sha2_digest_final(crypto_ctx_t *ctx, crypto_data_t *digest,
1406*7c478bd9Sstevel@tonic-gate     crypto_req_handle_t req)
1407*7c478bd9Sstevel@tonic-gate {
1408*7c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
1409*7c478bd9Sstevel@tonic-gate 	uint_t sha_digest_len;
1410*7c478bd9Sstevel@tonic-gate 
1411*7c478bd9Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
1412*7c478bd9Sstevel@tonic-gate 
1413*7c478bd9Sstevel@tonic-gate 	switch (PROV_SHA2_CTX(ctx)->sc_mech_type) {
1414*7c478bd9Sstevel@tonic-gate 	case SHA256_MECH_INFO_TYPE:
1415*7c478bd9Sstevel@tonic-gate 		sha_digest_len = SHA256_DIGEST_LENGTH;
1416*7c478bd9Sstevel@tonic-gate 		break;
1417*7c478bd9Sstevel@tonic-gate 	case SHA384_MECH_INFO_TYPE:
1418*7c478bd9Sstevel@tonic-gate 		sha_digest_len = SHA384_DIGEST_LENGTH;
1419*7c478bd9Sstevel@tonic-gate 		break;
1420*7c478bd9Sstevel@tonic-gate 	case SHA512_MECH_INFO_TYPE:
1421*7c478bd9Sstevel@tonic-gate 		sha_digest_len = SHA512_DIGEST_LENGTH;
1422*7c478bd9Sstevel@tonic-gate 		break;
1423*7c478bd9Sstevel@tonic-gate 	default:
1424*7c478bd9Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
1425*7c478bd9Sstevel@tonic-gate 	}
1426*7c478bd9Sstevel@tonic-gate 
1427*7c478bd9Sstevel@tonic-gate 	/*
1428*7c478bd9Sstevel@tonic-gate 	 * We need to just return the length needed to store the output.
1429*7c478bd9Sstevel@tonic-gate 	 * We should not destroy the context for the following cases.
1430*7c478bd9Sstevel@tonic-gate 	 */
1431*7c478bd9Sstevel@tonic-gate 	if ((digest->cd_length == 0) ||
1432*7c478bd9Sstevel@tonic-gate 	    (digest->cd_length < sha_digest_len)) {
1433*7c478bd9Sstevel@tonic-gate 		digest->cd_length = sha_digest_len;
1434*7c478bd9Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
1435*7c478bd9Sstevel@tonic-gate 	}
1436*7c478bd9Sstevel@tonic-gate 
1437*7c478bd9Sstevel@tonic-gate 	/*
1438*7c478bd9Sstevel@tonic-gate 	 * Do a SHA2 final.
1439*7c478bd9Sstevel@tonic-gate 	 */
1440*7c478bd9Sstevel@tonic-gate 	switch (digest->cd_format) {
1441*7c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
1442*7c478bd9Sstevel@tonic-gate 		SHA2Final((unsigned char *)digest->cd_raw.iov_base +
1443*7c478bd9Sstevel@tonic-gate 		    digest->cd_offset, &PROV_SHA2_CTX(ctx)->sc_sha2_ctx);
1444*7c478bd9Sstevel@tonic-gate 		break;
1445*7c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
1446*7c478bd9Sstevel@tonic-gate 		ret = sha2_digest_final_uio(&PROV_SHA2_CTX(ctx)->sc_sha2_ctx,
1447*7c478bd9Sstevel@tonic-gate 		    digest, sha_digest_len, NULL);
1448*7c478bd9Sstevel@tonic-gate 		break;
1449*7c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
1450*7c478bd9Sstevel@tonic-gate 		ret = sha2_digest_final_mblk(&PROV_SHA2_CTX(ctx)->sc_sha2_ctx,
1451*7c478bd9Sstevel@tonic-gate 		    digest, sha_digest_len, NULL);
1452*7c478bd9Sstevel@tonic-gate 		break;
1453*7c478bd9Sstevel@tonic-gate 	default:
1454*7c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
1455*7c478bd9Sstevel@tonic-gate 	}
1456*7c478bd9Sstevel@tonic-gate 
1457*7c478bd9Sstevel@tonic-gate 	/* all done, free context and return */
1458*7c478bd9Sstevel@tonic-gate 
1459*7c478bd9Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
1460*7c478bd9Sstevel@tonic-gate 		digest->cd_length = sha_digest_len;
1461*7c478bd9Sstevel@tonic-gate 	} else {
1462*7c478bd9Sstevel@tonic-gate 		/*
1463*7c478bd9Sstevel@tonic-gate 		 * Only bzero context this on failure, since SHA2Final()
1464*7c478bd9Sstevel@tonic-gate 		 * does it for us.
1465*7c478bd9Sstevel@tonic-gate 		 */
1466*7c478bd9Sstevel@tonic-gate 		bzero(&PROV_SHA2_CTX(ctx)->sc_sha2_ctx, sizeof (SHA2_CTX));
1467*7c478bd9Sstevel@tonic-gate 		digest->cd_length = 0;
1468*7c478bd9Sstevel@tonic-gate 	}
1469*7c478bd9Sstevel@tonic-gate 
1470*7c478bd9Sstevel@tonic-gate 	kmem_free(ctx->cc_provider_private, sizeof (sha2_ctx_t));
1471*7c478bd9Sstevel@tonic-gate 	ctx->cc_provider_private = NULL;
1472*7c478bd9Sstevel@tonic-gate 
1473*7c478bd9Sstevel@tonic-gate 	return (ret);
1474*7c478bd9Sstevel@tonic-gate }
1475*7c478bd9Sstevel@tonic-gate 
1476*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
1477*7c478bd9Sstevel@tonic-gate static int
1478*7c478bd9Sstevel@tonic-gate sha2_digest_atomic(crypto_provider_handle_t provider,
1479*7c478bd9Sstevel@tonic-gate     crypto_session_id_t session_id, crypto_mechanism_t *mechanism,
1480*7c478bd9Sstevel@tonic-gate     crypto_data_t *data, crypto_data_t *digest,
1481*7c478bd9Sstevel@tonic-gate     crypto_req_handle_t req)
1482*7c478bd9Sstevel@tonic-gate {
1483*7c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
1484*7c478bd9Sstevel@tonic-gate 	SHA2_CTX sha2_ctx;
1485*7c478bd9Sstevel@tonic-gate 	uint32_t sha_digest_len;
1486*7c478bd9Sstevel@tonic-gate 
1487*7c478bd9Sstevel@tonic-gate 	/*
1488*7c478bd9Sstevel@tonic-gate 	 * Do the SHA inits.
1489*7c478bd9Sstevel@tonic-gate 	 */
1490*7c478bd9Sstevel@tonic-gate 
1491*7c478bd9Sstevel@tonic-gate 	SHA2Init(mechanism->cm_type, &sha2_ctx);
1492*7c478bd9Sstevel@tonic-gate 
1493*7c478bd9Sstevel@tonic-gate 	switch (data->cd_format) {
1494*7c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
1495*7c478bd9Sstevel@tonic-gate 		SHA2Update(&sha2_ctx, (uint8_t *)data->
1496*7c478bd9Sstevel@tonic-gate 		    cd_raw.iov_base + data->cd_offset, data->cd_length);
1497*7c478bd9Sstevel@tonic-gate 		break;
1498*7c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
1499*7c478bd9Sstevel@tonic-gate 		ret = sha2_digest_update_uio(&sha2_ctx, data);
1500*7c478bd9Sstevel@tonic-gate 		break;
1501*7c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
1502*7c478bd9Sstevel@tonic-gate 		ret = sha2_digest_update_mblk(&sha2_ctx, data);
1503*7c478bd9Sstevel@tonic-gate 		break;
1504*7c478bd9Sstevel@tonic-gate 	default:
1505*7c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
1506*7c478bd9Sstevel@tonic-gate 	}
1507*7c478bd9Sstevel@tonic-gate 
1508*7c478bd9Sstevel@tonic-gate 	/*
1509*7c478bd9Sstevel@tonic-gate 	 * Do the SHA updates on the specified input data.
1510*7c478bd9Sstevel@tonic-gate 	 */
1511*7c478bd9Sstevel@tonic-gate 
1512*7c478bd9Sstevel@tonic-gate 	if (ret != CRYPTO_SUCCESS) {
1513*7c478bd9Sstevel@tonic-gate 		/* the update failed, bail */
1514*7c478bd9Sstevel@tonic-gate 		bzero(&sha2_ctx, sizeof (SHA2_CTX));
1515*7c478bd9Sstevel@tonic-gate 		digest->cd_length = 0;
1516*7c478bd9Sstevel@tonic-gate 		return (ret);
1517*7c478bd9Sstevel@tonic-gate 
1518*7c478bd9Sstevel@tonic-gate 	}
1519*7c478bd9Sstevel@tonic-gate 
1520*7c478bd9Sstevel@tonic-gate 	if (mechanism->cm_type <= SHA256_HMAC_GEN_MECH_INFO_TYPE)
1521*7c478bd9Sstevel@tonic-gate 		sha_digest_len = SHA256_DIGEST_LENGTH;
1522*7c478bd9Sstevel@tonic-gate 	else
1523*7c478bd9Sstevel@tonic-gate 		sha_digest_len = SHA512_DIGEST_LENGTH;
1524*7c478bd9Sstevel@tonic-gate 
1525*7c478bd9Sstevel@tonic-gate 	/*
1526*7c478bd9Sstevel@tonic-gate 	 * Do a SHA2 final, must be done separately since the digest
1527*7c478bd9Sstevel@tonic-gate 	 * type can be different than the input data type.
1528*7c478bd9Sstevel@tonic-gate 	 */
1529*7c478bd9Sstevel@tonic-gate 	switch (digest->cd_format) {
1530*7c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
1531*7c478bd9Sstevel@tonic-gate 		SHA2Final((unsigned char *)digest->cd_raw.iov_base +
1532*7c478bd9Sstevel@tonic-gate 		    digest->cd_offset, &sha2_ctx);
1533*7c478bd9Sstevel@tonic-gate 		break;
1534*7c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
1535*7c478bd9Sstevel@tonic-gate 		ret = sha2_digest_final_uio(&sha2_ctx, digest,
1536*7c478bd9Sstevel@tonic-gate 		    sha_digest_len, NULL);
1537*7c478bd9Sstevel@tonic-gate 		break;
1538*7c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
1539*7c478bd9Sstevel@tonic-gate 		ret = sha2_digest_final_mblk(&sha2_ctx, digest,
1540*7c478bd9Sstevel@tonic-gate 		    sha_digest_len, NULL);
1541*7c478bd9Sstevel@tonic-gate 		break;
1542*7c478bd9Sstevel@tonic-gate 	default:
1543*7c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
1544*7c478bd9Sstevel@tonic-gate 	}
1545*7c478bd9Sstevel@tonic-gate 
1546*7c478bd9Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
1547*7c478bd9Sstevel@tonic-gate 		digest->cd_length = sha_digest_len;
1548*7c478bd9Sstevel@tonic-gate 	} else {
1549*7c478bd9Sstevel@tonic-gate 		/*
1550*7c478bd9Sstevel@tonic-gate 		 * Only bzero context on failure, since SHA2Final()
1551*7c478bd9Sstevel@tonic-gate 		 * does it for us.
1552*7c478bd9Sstevel@tonic-gate 		 */
1553*7c478bd9Sstevel@tonic-gate 		bzero(&sha2_ctx, sizeof (SHA2_CTX));
1554*7c478bd9Sstevel@tonic-gate 		digest->cd_length = 0;
1555*7c478bd9Sstevel@tonic-gate 	}
1556*7c478bd9Sstevel@tonic-gate 
1557*7c478bd9Sstevel@tonic-gate 	return (ret);
1558*7c478bd9Sstevel@tonic-gate }
1559*7c478bd9Sstevel@tonic-gate 
1560*7c478bd9Sstevel@tonic-gate /*
1561*7c478bd9Sstevel@tonic-gate  * KCF software provider mac entry points.
1562*7c478bd9Sstevel@tonic-gate  *
1563*7c478bd9Sstevel@tonic-gate  * SHA2 HMAC is: SHA2(key XOR opad, SHA2(key XOR ipad, text))
1564*7c478bd9Sstevel@tonic-gate  *
1565*7c478bd9Sstevel@tonic-gate  * Init:
1566*7c478bd9Sstevel@tonic-gate  * The initialization routine initializes what we denote
1567*7c478bd9Sstevel@tonic-gate  * as the inner and outer contexts by doing
1568*7c478bd9Sstevel@tonic-gate  * - for inner context: SHA2(key XOR ipad)
1569*7c478bd9Sstevel@tonic-gate  * - for outer context: SHA2(key XOR opad)
1570*7c478bd9Sstevel@tonic-gate  *
1571*7c478bd9Sstevel@tonic-gate  * Update:
1572*7c478bd9Sstevel@tonic-gate  * Each subsequent SHA2 HMAC update will result in an
1573*7c478bd9Sstevel@tonic-gate  * update of the inner context with the specified data.
1574*7c478bd9Sstevel@tonic-gate  *
1575*7c478bd9Sstevel@tonic-gate  * Final:
1576*7c478bd9Sstevel@tonic-gate  * The SHA2 HMAC final will do a SHA2 final operation on the
1577*7c478bd9Sstevel@tonic-gate  * inner context, and the resulting digest will be used
1578*7c478bd9Sstevel@tonic-gate  * as the data for an update on the outer context. Last
1579*7c478bd9Sstevel@tonic-gate  * but not least, a SHA2 final on the outer context will
1580*7c478bd9Sstevel@tonic-gate  * be performed to obtain the SHA2 HMAC digest to return
1581*7c478bd9Sstevel@tonic-gate  * to the user.
1582*7c478bd9Sstevel@tonic-gate  */
1583*7c478bd9Sstevel@tonic-gate 
1584*7c478bd9Sstevel@tonic-gate /*
1585*7c478bd9Sstevel@tonic-gate  * Initialize a SHA2-HMAC context.
1586*7c478bd9Sstevel@tonic-gate  */
1587*7c478bd9Sstevel@tonic-gate static void
1588*7c478bd9Sstevel@tonic-gate sha2_mac_init_ctx(sha2_hmac_ctx_t *ctx, void *keyval, uint_t length_in_bytes)
1589*7c478bd9Sstevel@tonic-gate {
1590*7c478bd9Sstevel@tonic-gate 	uint64_t ipad[SHA512_HMAC_BLOCK_SIZE / sizeof (uint64_t)];
1591*7c478bd9Sstevel@tonic-gate 	uint64_t opad[SHA512_HMAC_BLOCK_SIZE / sizeof (uint64_t)];
1592*7c478bd9Sstevel@tonic-gate 	int i, block_size, blocks_per_int64;
1593*7c478bd9Sstevel@tonic-gate 
1594*7c478bd9Sstevel@tonic-gate 	/* Determine the block size */
1595*7c478bd9Sstevel@tonic-gate 	if (ctx->hc_mech_type <= SHA256_HMAC_GEN_MECH_INFO_TYPE) {
1596*7c478bd9Sstevel@tonic-gate 		block_size = SHA256_HMAC_BLOCK_SIZE;
1597*7c478bd9Sstevel@tonic-gate 		blocks_per_int64 = SHA256_HMAC_BLOCK_SIZE / sizeof (uint64_t);
1598*7c478bd9Sstevel@tonic-gate 	} else {
1599*7c478bd9Sstevel@tonic-gate 		block_size = SHA512_HMAC_BLOCK_SIZE;
1600*7c478bd9Sstevel@tonic-gate 		blocks_per_int64 = SHA512_HMAC_BLOCK_SIZE / sizeof (uint64_t);
1601*7c478bd9Sstevel@tonic-gate 	}
1602*7c478bd9Sstevel@tonic-gate 
1603*7c478bd9Sstevel@tonic-gate 	(void) bzero(ipad, block_size);
1604*7c478bd9Sstevel@tonic-gate 	(void) bzero(opad, block_size);
1605*7c478bd9Sstevel@tonic-gate 	(void) bcopy(keyval, ipad, length_in_bytes);
1606*7c478bd9Sstevel@tonic-gate 	(void) bcopy(keyval, opad, length_in_bytes);
1607*7c478bd9Sstevel@tonic-gate 
1608*7c478bd9Sstevel@tonic-gate 	/* XOR key with ipad (0x36) and opad (0x5c) */
1609*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < blocks_per_int64; i ++) {
1610*7c478bd9Sstevel@tonic-gate 		ipad[i] ^= 0x3636363636363636;
1611*7c478bd9Sstevel@tonic-gate 		opad[i] ^= 0x5c5c5c5c5c5c5c5c;
1612*7c478bd9Sstevel@tonic-gate 	}
1613*7c478bd9Sstevel@tonic-gate 
1614*7c478bd9Sstevel@tonic-gate 	/* perform SHA2 on ipad */
1615*7c478bd9Sstevel@tonic-gate 	SHA2Init(ctx->hc_mech_type, &ctx->hc_icontext);
1616*7c478bd9Sstevel@tonic-gate 	SHA2Update(&ctx->hc_icontext, (uint8_t *)ipad, block_size);
1617*7c478bd9Sstevel@tonic-gate 
1618*7c478bd9Sstevel@tonic-gate 	/* perform SHA2 on opad */
1619*7c478bd9Sstevel@tonic-gate 	SHA2Init(ctx->hc_mech_type, &ctx->hc_ocontext);
1620*7c478bd9Sstevel@tonic-gate 	SHA2Update(&ctx->hc_ocontext, (uint8_t *)opad, block_size);
1621*7c478bd9Sstevel@tonic-gate 
1622*7c478bd9Sstevel@tonic-gate }
1623*7c478bd9Sstevel@tonic-gate 
1624*7c478bd9Sstevel@tonic-gate /*
1625*7c478bd9Sstevel@tonic-gate  */
1626*7c478bd9Sstevel@tonic-gate static int
1627*7c478bd9Sstevel@tonic-gate sha2_mac_init(crypto_ctx_t *ctx, crypto_mechanism_t *mechanism,
1628*7c478bd9Sstevel@tonic-gate     crypto_key_t *key, crypto_spi_ctx_template_t ctx_template,
1629*7c478bd9Sstevel@tonic-gate     crypto_req_handle_t req)
1630*7c478bd9Sstevel@tonic-gate {
1631*7c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
1632*7c478bd9Sstevel@tonic-gate 	uint_t keylen_in_bytes = CRYPTO_BITS2BYTES(key->ck_length);
1633*7c478bd9Sstevel@tonic-gate 	uint_t sha_digest_len, sha_hmac_block_size;
1634*7c478bd9Sstevel@tonic-gate 
1635*7c478bd9Sstevel@tonic-gate 	/*
1636*7c478bd9Sstevel@tonic-gate 	 * Set the digest length and block size to values approriate to the
1637*7c478bd9Sstevel@tonic-gate 	 * mechanism
1638*7c478bd9Sstevel@tonic-gate 	 */
1639*7c478bd9Sstevel@tonic-gate 	switch (mechanism->cm_type) {
1640*7c478bd9Sstevel@tonic-gate 	case SHA256_HMAC_MECH_INFO_TYPE:
1641*7c478bd9Sstevel@tonic-gate 	case SHA256_HMAC_GEN_MECH_INFO_TYPE:
1642*7c478bd9Sstevel@tonic-gate 		sha_digest_len = SHA256_DIGEST_LENGTH;
1643*7c478bd9Sstevel@tonic-gate 		sha_hmac_block_size = SHA256_HMAC_BLOCK_SIZE;
1644*7c478bd9Sstevel@tonic-gate 		break;
1645*7c478bd9Sstevel@tonic-gate 	case SHA384_HMAC_MECH_INFO_TYPE:
1646*7c478bd9Sstevel@tonic-gate 	case SHA384_HMAC_GEN_MECH_INFO_TYPE:
1647*7c478bd9Sstevel@tonic-gate 	case SHA512_HMAC_MECH_INFO_TYPE:
1648*7c478bd9Sstevel@tonic-gate 	case SHA512_HMAC_GEN_MECH_INFO_TYPE:
1649*7c478bd9Sstevel@tonic-gate 		sha_digest_len = SHA512_DIGEST_LENGTH;
1650*7c478bd9Sstevel@tonic-gate 		sha_hmac_block_size = SHA512_HMAC_BLOCK_SIZE;
1651*7c478bd9Sstevel@tonic-gate 		break;
1652*7c478bd9Sstevel@tonic-gate 	default:
1653*7c478bd9Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
1654*7c478bd9Sstevel@tonic-gate 	}
1655*7c478bd9Sstevel@tonic-gate 
1656*7c478bd9Sstevel@tonic-gate 	if (key->ck_format != CRYPTO_KEY_RAW)
1657*7c478bd9Sstevel@tonic-gate 		return (CRYPTO_ARGUMENTS_BAD);
1658*7c478bd9Sstevel@tonic-gate 
1659*7c478bd9Sstevel@tonic-gate 	ctx->cc_provider_private = kmem_alloc(sizeof (sha2_hmac_ctx_t),
1660*7c478bd9Sstevel@tonic-gate 	    crypto_kmflag(req));
1661*7c478bd9Sstevel@tonic-gate 	if (ctx->cc_provider_private == NULL)
1662*7c478bd9Sstevel@tonic-gate 		return (CRYPTO_HOST_MEMORY);
1663*7c478bd9Sstevel@tonic-gate 
1664*7c478bd9Sstevel@tonic-gate 	if (ctx_template != NULL) {
1665*7c478bd9Sstevel@tonic-gate 		/* reuse context template */
1666*7c478bd9Sstevel@tonic-gate 		bcopy(ctx_template, PROV_SHA2_HMAC_CTX(ctx),
1667*7c478bd9Sstevel@tonic-gate 		    sizeof (sha2_hmac_ctx_t));
1668*7c478bd9Sstevel@tonic-gate 	} else {
1669*7c478bd9Sstevel@tonic-gate 		/* no context template, compute context */
1670*7c478bd9Sstevel@tonic-gate 		if (keylen_in_bytes > sha_hmac_block_size) {
1671*7c478bd9Sstevel@tonic-gate 			uchar_t digested_key[SHA512_DIGEST_LENGTH];
1672*7c478bd9Sstevel@tonic-gate 			sha2_hmac_ctx_t *hmac_ctx = ctx->cc_provider_private;
1673*7c478bd9Sstevel@tonic-gate 
1674*7c478bd9Sstevel@tonic-gate 			/*
1675*7c478bd9Sstevel@tonic-gate 			 * Hash the passed-in key to get a smaller key.
1676*7c478bd9Sstevel@tonic-gate 			 * The inner context is used since it hasn't been
1677*7c478bd9Sstevel@tonic-gate 			 * initialized yet.
1678*7c478bd9Sstevel@tonic-gate 			 */
1679*7c478bd9Sstevel@tonic-gate 			PROV_SHA2_DIGEST_KEY(mechanism->cm_type / 3,
1680*7c478bd9Sstevel@tonic-gate 			    &hmac_ctx->hc_icontext,
1681*7c478bd9Sstevel@tonic-gate 			    key->ck_data, keylen_in_bytes, digested_key);
1682*7c478bd9Sstevel@tonic-gate 			sha2_mac_init_ctx(PROV_SHA2_HMAC_CTX(ctx),
1683*7c478bd9Sstevel@tonic-gate 			    digested_key, sha_digest_len);
1684*7c478bd9Sstevel@tonic-gate 		} else {
1685*7c478bd9Sstevel@tonic-gate 			sha2_mac_init_ctx(PROV_SHA2_HMAC_CTX(ctx),
1686*7c478bd9Sstevel@tonic-gate 			    key->ck_data, keylen_in_bytes);
1687*7c478bd9Sstevel@tonic-gate 		}
1688*7c478bd9Sstevel@tonic-gate 	}
1689*7c478bd9Sstevel@tonic-gate 
1690*7c478bd9Sstevel@tonic-gate 	/*
1691*7c478bd9Sstevel@tonic-gate 	 * Get the mechanism parameters, if applicable.
1692*7c478bd9Sstevel@tonic-gate 	 */
1693*7c478bd9Sstevel@tonic-gate 	PROV_SHA2_HMAC_CTX(ctx)->hc_mech_type = mechanism->cm_type;
1694*7c478bd9Sstevel@tonic-gate 	if (mechanism->cm_type % 3 == 2) {
1695*7c478bd9Sstevel@tonic-gate 		if (mechanism->cm_param == NULL ||
1696*7c478bd9Sstevel@tonic-gate 		    mechanism->cm_param_len != sizeof (ulong_t))
1697*7c478bd9Sstevel@tonic-gate 			ret = CRYPTO_MECHANISM_PARAM_INVALID;
1698*7c478bd9Sstevel@tonic-gate 		PROV_SHA2_GET_DIGEST_LEN(mechanism,
1699*7c478bd9Sstevel@tonic-gate 		    PROV_SHA2_HMAC_CTX(ctx)->hc_digest_len);
1700*7c478bd9Sstevel@tonic-gate 		if (PROV_SHA2_HMAC_CTX(ctx)->hc_digest_len > sha_digest_len)
1701*7c478bd9Sstevel@tonic-gate 			ret = CRYPTO_MECHANISM_PARAM_INVALID;
1702*7c478bd9Sstevel@tonic-gate 	}
1703*7c478bd9Sstevel@tonic-gate 
1704*7c478bd9Sstevel@tonic-gate 	if (ret != CRYPTO_SUCCESS) {
1705*7c478bd9Sstevel@tonic-gate 		bzero(ctx->cc_provider_private, sizeof (sha2_hmac_ctx_t));
1706*7c478bd9Sstevel@tonic-gate 		kmem_free(ctx->cc_provider_private, sizeof (sha2_hmac_ctx_t));
1707*7c478bd9Sstevel@tonic-gate 		ctx->cc_provider_private = NULL;
1708*7c478bd9Sstevel@tonic-gate 	}
1709*7c478bd9Sstevel@tonic-gate 
1710*7c478bd9Sstevel@tonic-gate 	return (ret);
1711*7c478bd9Sstevel@tonic-gate }
1712*7c478bd9Sstevel@tonic-gate 
1713*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
1714*7c478bd9Sstevel@tonic-gate static int
1715*7c478bd9Sstevel@tonic-gate sha2_mac_update(crypto_ctx_t *ctx, crypto_data_t *data,
1716*7c478bd9Sstevel@tonic-gate     crypto_req_handle_t req)
1717*7c478bd9Sstevel@tonic-gate {
1718*7c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
1719*7c478bd9Sstevel@tonic-gate 
1720*7c478bd9Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
1721*7c478bd9Sstevel@tonic-gate 
1722*7c478bd9Sstevel@tonic-gate 	/*
1723*7c478bd9Sstevel@tonic-gate 	 * Do a SHA2 update of the inner context using the specified
1724*7c478bd9Sstevel@tonic-gate 	 * data.
1725*7c478bd9Sstevel@tonic-gate 	 */
1726*7c478bd9Sstevel@tonic-gate 	switch (data->cd_format) {
1727*7c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
1728*7c478bd9Sstevel@tonic-gate 		SHA2Update(&PROV_SHA2_HMAC_CTX(ctx)->hc_icontext,
1729*7c478bd9Sstevel@tonic-gate 		    (uint8_t *)data->cd_raw.iov_base + data->cd_offset,
1730*7c478bd9Sstevel@tonic-gate 		    data->cd_length);
1731*7c478bd9Sstevel@tonic-gate 		break;
1732*7c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
1733*7c478bd9Sstevel@tonic-gate 		ret = sha2_digest_update_uio(
1734*7c478bd9Sstevel@tonic-gate 		    &PROV_SHA2_HMAC_CTX(ctx)->hc_icontext, data);
1735*7c478bd9Sstevel@tonic-gate 		break;
1736*7c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
1737*7c478bd9Sstevel@tonic-gate 		ret = sha2_digest_update_mblk(
1738*7c478bd9Sstevel@tonic-gate 		    &PROV_SHA2_HMAC_CTX(ctx)->hc_icontext, data);
1739*7c478bd9Sstevel@tonic-gate 		break;
1740*7c478bd9Sstevel@tonic-gate 	default:
1741*7c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
1742*7c478bd9Sstevel@tonic-gate 	}
1743*7c478bd9Sstevel@tonic-gate 
1744*7c478bd9Sstevel@tonic-gate 	return (ret);
1745*7c478bd9Sstevel@tonic-gate }
1746*7c478bd9Sstevel@tonic-gate 
1747*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
1748*7c478bd9Sstevel@tonic-gate static int
1749*7c478bd9Sstevel@tonic-gate sha2_mac_final(crypto_ctx_t *ctx, crypto_data_t *mac, crypto_req_handle_t req)
1750*7c478bd9Sstevel@tonic-gate {
1751*7c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
1752*7c478bd9Sstevel@tonic-gate 	uchar_t digest[SHA512_DIGEST_LENGTH];
1753*7c478bd9Sstevel@tonic-gate 	uint32_t digest_len, sha_digest_len;
1754*7c478bd9Sstevel@tonic-gate 
1755*7c478bd9Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
1756*7c478bd9Sstevel@tonic-gate 
1757*7c478bd9Sstevel@tonic-gate 	/* Set the digest lengths to values approriate to the mechanism */
1758*7c478bd9Sstevel@tonic-gate 	switch (PROV_SHA2_HMAC_CTX(ctx)->hc_mech_type) {
1759*7c478bd9Sstevel@tonic-gate 	case SHA256_HMAC_MECH_INFO_TYPE:
1760*7c478bd9Sstevel@tonic-gate 		sha_digest_len = digest_len = SHA256_DIGEST_LENGTH;
1761*7c478bd9Sstevel@tonic-gate 		break;
1762*7c478bd9Sstevel@tonic-gate 	case SHA384_HMAC_MECH_INFO_TYPE:
1763*7c478bd9Sstevel@tonic-gate 	case SHA512_HMAC_MECH_INFO_TYPE:
1764*7c478bd9Sstevel@tonic-gate 		sha_digest_len = digest_len = SHA512_DIGEST_LENGTH;
1765*7c478bd9Sstevel@tonic-gate 		break;
1766*7c478bd9Sstevel@tonic-gate 	case SHA256_HMAC_GEN_MECH_INFO_TYPE:
1767*7c478bd9Sstevel@tonic-gate 		sha_digest_len = SHA256_DIGEST_LENGTH;
1768*7c478bd9Sstevel@tonic-gate 		digest_len = PROV_SHA2_HMAC_CTX(ctx)->hc_digest_len;
1769*7c478bd9Sstevel@tonic-gate 		break;
1770*7c478bd9Sstevel@tonic-gate 	case SHA384_HMAC_GEN_MECH_INFO_TYPE:
1771*7c478bd9Sstevel@tonic-gate 	case SHA512_HMAC_GEN_MECH_INFO_TYPE:
1772*7c478bd9Sstevel@tonic-gate 		sha_digest_len = SHA512_DIGEST_LENGTH;
1773*7c478bd9Sstevel@tonic-gate 		digest_len = PROV_SHA2_HMAC_CTX(ctx)->hc_digest_len;
1774*7c478bd9Sstevel@tonic-gate 		break;
1775*7c478bd9Sstevel@tonic-gate 	}
1776*7c478bd9Sstevel@tonic-gate 
1777*7c478bd9Sstevel@tonic-gate 	/*
1778*7c478bd9Sstevel@tonic-gate 	 * We need to just return the length needed to store the output.
1779*7c478bd9Sstevel@tonic-gate 	 * We should not destroy the context for the following cases.
1780*7c478bd9Sstevel@tonic-gate 	 */
1781*7c478bd9Sstevel@tonic-gate 	if ((mac->cd_length == 0) || (mac->cd_length < digest_len)) {
1782*7c478bd9Sstevel@tonic-gate 		mac->cd_length = digest_len;
1783*7c478bd9Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
1784*7c478bd9Sstevel@tonic-gate 	}
1785*7c478bd9Sstevel@tonic-gate 
1786*7c478bd9Sstevel@tonic-gate 	/*
1787*7c478bd9Sstevel@tonic-gate 	 * Do a SHA2 final on the inner context.
1788*7c478bd9Sstevel@tonic-gate 	 */
1789*7c478bd9Sstevel@tonic-gate 	SHA2Final(digest, &PROV_SHA2_HMAC_CTX(ctx)->hc_icontext);
1790*7c478bd9Sstevel@tonic-gate 
1791*7c478bd9Sstevel@tonic-gate 	/*
1792*7c478bd9Sstevel@tonic-gate 	 * Do a SHA2 update on the outer context, feeding the inner
1793*7c478bd9Sstevel@tonic-gate 	 * digest as data.
1794*7c478bd9Sstevel@tonic-gate 	 */
1795*7c478bd9Sstevel@tonic-gate 	SHA2Update(&PROV_SHA2_HMAC_CTX(ctx)->hc_ocontext, digest,
1796*7c478bd9Sstevel@tonic-gate 	    sha_digest_len);
1797*7c478bd9Sstevel@tonic-gate 
1798*7c478bd9Sstevel@tonic-gate 	/*
1799*7c478bd9Sstevel@tonic-gate 	 * Do a SHA2 final on the outer context, storing the computing
1800*7c478bd9Sstevel@tonic-gate 	 * digest in the users buffer.
1801*7c478bd9Sstevel@tonic-gate 	 */
1802*7c478bd9Sstevel@tonic-gate 	switch (mac->cd_format) {
1803*7c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
1804*7c478bd9Sstevel@tonic-gate 		if (digest_len != sha_digest_len) {
1805*7c478bd9Sstevel@tonic-gate 			/*
1806*7c478bd9Sstevel@tonic-gate 			 * The caller requested a short digest. Digest
1807*7c478bd9Sstevel@tonic-gate 			 * into a scratch buffer and return to
1808*7c478bd9Sstevel@tonic-gate 			 * the user only what was requested.
1809*7c478bd9Sstevel@tonic-gate 			 */
1810*7c478bd9Sstevel@tonic-gate 			SHA2Final(digest,
1811*7c478bd9Sstevel@tonic-gate 			    &PROV_SHA2_HMAC_CTX(ctx)->hc_ocontext);
1812*7c478bd9Sstevel@tonic-gate 			bcopy(digest, (unsigned char *)mac->cd_raw.iov_base +
1813*7c478bd9Sstevel@tonic-gate 			    mac->cd_offset, digest_len);
1814*7c478bd9Sstevel@tonic-gate 		} else {
1815*7c478bd9Sstevel@tonic-gate 			SHA2Final((unsigned char *)mac->cd_raw.iov_base +
1816*7c478bd9Sstevel@tonic-gate 			    mac->cd_offset,
1817*7c478bd9Sstevel@tonic-gate 			    &PROV_SHA2_HMAC_CTX(ctx)->hc_ocontext);
1818*7c478bd9Sstevel@tonic-gate 		}
1819*7c478bd9Sstevel@tonic-gate 		break;
1820*7c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
1821*7c478bd9Sstevel@tonic-gate 		ret = sha2_digest_final_uio(
1822*7c478bd9Sstevel@tonic-gate 		    &PROV_SHA2_HMAC_CTX(ctx)->hc_ocontext, mac,
1823*7c478bd9Sstevel@tonic-gate 		    digest_len, digest);
1824*7c478bd9Sstevel@tonic-gate 		break;
1825*7c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
1826*7c478bd9Sstevel@tonic-gate 		ret = sha2_digest_final_mblk(
1827*7c478bd9Sstevel@tonic-gate 		    &PROV_SHA2_HMAC_CTX(ctx)->hc_ocontext, mac,
1828*7c478bd9Sstevel@tonic-gate 		    digest_len, digest);
1829*7c478bd9Sstevel@tonic-gate 		break;
1830*7c478bd9Sstevel@tonic-gate 	default:
1831*7c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
1832*7c478bd9Sstevel@tonic-gate 	}
1833*7c478bd9Sstevel@tonic-gate 
1834*7c478bd9Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
1835*7c478bd9Sstevel@tonic-gate 		mac->cd_length = digest_len;
1836*7c478bd9Sstevel@tonic-gate 	} else {
1837*7c478bd9Sstevel@tonic-gate 		/*
1838*7c478bd9Sstevel@tonic-gate 		 * Only bzero outer context on failure, since SHA2Final()
1839*7c478bd9Sstevel@tonic-gate 		 * does it for us.
1840*7c478bd9Sstevel@tonic-gate 		 * We don't have to bzero the inner context since we
1841*7c478bd9Sstevel@tonic-gate 		 * always invoke a SHA2Final() on it.
1842*7c478bd9Sstevel@tonic-gate 		 */
1843*7c478bd9Sstevel@tonic-gate 		bzero(&PROV_SHA2_HMAC_CTX(ctx)->hc_ocontext,
1844*7c478bd9Sstevel@tonic-gate 		    sizeof (SHA2_CTX));
1845*7c478bd9Sstevel@tonic-gate 		mac->cd_length = 0;
1846*7c478bd9Sstevel@tonic-gate 	}
1847*7c478bd9Sstevel@tonic-gate 
1848*7c478bd9Sstevel@tonic-gate 	kmem_free(ctx->cc_provider_private, sizeof (sha2_hmac_ctx_t));
1849*7c478bd9Sstevel@tonic-gate 	ctx->cc_provider_private = NULL;
1850*7c478bd9Sstevel@tonic-gate 
1851*7c478bd9Sstevel@tonic-gate 	return (ret);
1852*7c478bd9Sstevel@tonic-gate }
1853*7c478bd9Sstevel@tonic-gate 
1854*7c478bd9Sstevel@tonic-gate #define	SHA2_MAC_UPDATE(data, ctx, ret) {				\
1855*7c478bd9Sstevel@tonic-gate 	switch (data->cd_format) {					\
1856*7c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:						\
1857*7c478bd9Sstevel@tonic-gate 		SHA2Update(&(ctx).hc_icontext,				\
1858*7c478bd9Sstevel@tonic-gate 		    (uint8_t *)data->cd_raw.iov_base +			\
1859*7c478bd9Sstevel@tonic-gate 		    data->cd_offset, data->cd_length);			\
1860*7c478bd9Sstevel@tonic-gate 		break;							\
1861*7c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:						\
1862*7c478bd9Sstevel@tonic-gate 		ret = sha2_digest_update_uio(&(ctx).hc_icontext, data);	\
1863*7c478bd9Sstevel@tonic-gate 		break;							\
1864*7c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:						\
1865*7c478bd9Sstevel@tonic-gate 		ret = sha2_digest_update_mblk(&(ctx).hc_icontext,	\
1866*7c478bd9Sstevel@tonic-gate 		    data);						\
1867*7c478bd9Sstevel@tonic-gate 		break;							\
1868*7c478bd9Sstevel@tonic-gate 	default:							\
1869*7c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;				\
1870*7c478bd9Sstevel@tonic-gate 	}								\
1871*7c478bd9Sstevel@tonic-gate }
1872*7c478bd9Sstevel@tonic-gate 
1873*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
1874*7c478bd9Sstevel@tonic-gate static int
1875*7c478bd9Sstevel@tonic-gate sha2_mac_atomic(crypto_provider_handle_t provider,
1876*7c478bd9Sstevel@tonic-gate     crypto_session_id_t session_id, crypto_mechanism_t *mechanism,
1877*7c478bd9Sstevel@tonic-gate     crypto_key_t *key, crypto_data_t *data, crypto_data_t *mac,
1878*7c478bd9Sstevel@tonic-gate     crypto_spi_ctx_template_t ctx_template, crypto_req_handle_t req)
1879*7c478bd9Sstevel@tonic-gate {
1880*7c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
1881*7c478bd9Sstevel@tonic-gate 	uchar_t digest[SHA512_DIGEST_LENGTH];
1882*7c478bd9Sstevel@tonic-gate 	sha2_hmac_ctx_t sha2_hmac_ctx;
1883*7c478bd9Sstevel@tonic-gate 	uint32_t sha_digest_len, digest_len, sha_hmac_block_size;
1884*7c478bd9Sstevel@tonic-gate 	uint_t keylen_in_bytes = CRYPTO_BITS2BYTES(key->ck_length);
1885*7c478bd9Sstevel@tonic-gate 
1886*7c478bd9Sstevel@tonic-gate 	/*
1887*7c478bd9Sstevel@tonic-gate 	 * Set the digest length and block size to values approriate to the
1888*7c478bd9Sstevel@tonic-gate 	 * mechanism
1889*7c478bd9Sstevel@tonic-gate 	 */
1890*7c478bd9Sstevel@tonic-gate 	switch (mechanism->cm_type) {
1891*7c478bd9Sstevel@tonic-gate 	case SHA256_HMAC_MECH_INFO_TYPE:
1892*7c478bd9Sstevel@tonic-gate 	case SHA256_HMAC_GEN_MECH_INFO_TYPE:
1893*7c478bd9Sstevel@tonic-gate 		sha_digest_len = digest_len = SHA256_DIGEST_LENGTH;
1894*7c478bd9Sstevel@tonic-gate 		sha_hmac_block_size = SHA256_HMAC_BLOCK_SIZE;
1895*7c478bd9Sstevel@tonic-gate 		break;
1896*7c478bd9Sstevel@tonic-gate 	case SHA384_HMAC_MECH_INFO_TYPE:
1897*7c478bd9Sstevel@tonic-gate 	case SHA384_HMAC_GEN_MECH_INFO_TYPE:
1898*7c478bd9Sstevel@tonic-gate 	case SHA512_HMAC_MECH_INFO_TYPE:
1899*7c478bd9Sstevel@tonic-gate 	case SHA512_HMAC_GEN_MECH_INFO_TYPE:
1900*7c478bd9Sstevel@tonic-gate 		sha_digest_len = digest_len = SHA512_DIGEST_LENGTH;
1901*7c478bd9Sstevel@tonic-gate 		sha_hmac_block_size = SHA512_HMAC_BLOCK_SIZE;
1902*7c478bd9Sstevel@tonic-gate 		break;
1903*7c478bd9Sstevel@tonic-gate 	default:
1904*7c478bd9Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
1905*7c478bd9Sstevel@tonic-gate 	}
1906*7c478bd9Sstevel@tonic-gate 
1907*7c478bd9Sstevel@tonic-gate 	/* Add support for key by attributes (RFE 4706552) */
1908*7c478bd9Sstevel@tonic-gate 	if (key->ck_format != CRYPTO_KEY_RAW)
1909*7c478bd9Sstevel@tonic-gate 		return (CRYPTO_ARGUMENTS_BAD);
1910*7c478bd9Sstevel@tonic-gate 
1911*7c478bd9Sstevel@tonic-gate 	if (ctx_template != NULL) {
1912*7c478bd9Sstevel@tonic-gate 		/* reuse context template */
1913*7c478bd9Sstevel@tonic-gate 		bcopy(ctx_template, &sha2_hmac_ctx, sizeof (sha2_hmac_ctx_t));
1914*7c478bd9Sstevel@tonic-gate 	} else {
1915*7c478bd9Sstevel@tonic-gate 		sha2_hmac_ctx.hc_mech_type = mechanism->cm_type;
1916*7c478bd9Sstevel@tonic-gate 		/* no context template, initialize context */
1917*7c478bd9Sstevel@tonic-gate 		if (keylen_in_bytes > sha_hmac_block_size) {
1918*7c478bd9Sstevel@tonic-gate 			/*
1919*7c478bd9Sstevel@tonic-gate 			 * Hash the passed-in key to get a smaller key.
1920*7c478bd9Sstevel@tonic-gate 			 * The inner context is used since it hasn't been
1921*7c478bd9Sstevel@tonic-gate 			 * initialized yet.
1922*7c478bd9Sstevel@tonic-gate 			 */
1923*7c478bd9Sstevel@tonic-gate 			PROV_SHA2_DIGEST_KEY(mechanism->cm_type / 3,
1924*7c478bd9Sstevel@tonic-gate 			    &sha2_hmac_ctx.hc_icontext,
1925*7c478bd9Sstevel@tonic-gate 			    key->ck_data, keylen_in_bytes, digest);
1926*7c478bd9Sstevel@tonic-gate 			sha2_mac_init_ctx(&sha2_hmac_ctx, digest,
1927*7c478bd9Sstevel@tonic-gate 			    sha_digest_len);
1928*7c478bd9Sstevel@tonic-gate 		} else {
1929*7c478bd9Sstevel@tonic-gate 			sha2_mac_init_ctx(&sha2_hmac_ctx, key->ck_data,
1930*7c478bd9Sstevel@tonic-gate 			    keylen_in_bytes);
1931*7c478bd9Sstevel@tonic-gate 		}
1932*7c478bd9Sstevel@tonic-gate 	}
1933*7c478bd9Sstevel@tonic-gate 
1934*7c478bd9Sstevel@tonic-gate 	/* get the mechanism parameters, if applicable */
1935*7c478bd9Sstevel@tonic-gate 	if ((mechanism->cm_type % 3) == 2) {
1936*7c478bd9Sstevel@tonic-gate 		if (mechanism->cm_param == NULL ||
1937*7c478bd9Sstevel@tonic-gate 		    mechanism->cm_param_len != sizeof (ulong_t)) {
1938*7c478bd9Sstevel@tonic-gate 			ret = CRYPTO_MECHANISM_PARAM_INVALID;
1939*7c478bd9Sstevel@tonic-gate 			goto bail;
1940*7c478bd9Sstevel@tonic-gate 		}
1941*7c478bd9Sstevel@tonic-gate 		PROV_SHA2_GET_DIGEST_LEN(mechanism, digest_len);
1942*7c478bd9Sstevel@tonic-gate 		if (digest_len > sha_digest_len) {
1943*7c478bd9Sstevel@tonic-gate 			ret = CRYPTO_MECHANISM_PARAM_INVALID;
1944*7c478bd9Sstevel@tonic-gate 			goto bail;
1945*7c478bd9Sstevel@tonic-gate 		}
1946*7c478bd9Sstevel@tonic-gate 	}
1947*7c478bd9Sstevel@tonic-gate 
1948*7c478bd9Sstevel@tonic-gate 	/* do a SHA2 update of the inner context using the specified data */
1949*7c478bd9Sstevel@tonic-gate 	SHA2_MAC_UPDATE(data, sha2_hmac_ctx, ret);
1950*7c478bd9Sstevel@tonic-gate 	if (ret != CRYPTO_SUCCESS)
1951*7c478bd9Sstevel@tonic-gate 		/* the update failed, free context and bail */
1952*7c478bd9Sstevel@tonic-gate 		goto bail;
1953*7c478bd9Sstevel@tonic-gate 
1954*7c478bd9Sstevel@tonic-gate 	/*
1955*7c478bd9Sstevel@tonic-gate 	 * Do a SHA2 final on the inner context.
1956*7c478bd9Sstevel@tonic-gate 	 */
1957*7c478bd9Sstevel@tonic-gate 	SHA2Final(digest, &sha2_hmac_ctx.hc_icontext);
1958*7c478bd9Sstevel@tonic-gate 
1959*7c478bd9Sstevel@tonic-gate 	/*
1960*7c478bd9Sstevel@tonic-gate 	 * Do an SHA2 update on the outer context, feeding the inner
1961*7c478bd9Sstevel@tonic-gate 	 * digest as data.
1962*7c478bd9Sstevel@tonic-gate 	 *
1963*7c478bd9Sstevel@tonic-gate 	 * Make sure that SHA384 is handled special because
1964*7c478bd9Sstevel@tonic-gate 	 * it cannot feed a 60-byte inner hash to the outer
1965*7c478bd9Sstevel@tonic-gate 	 */
1966*7c478bd9Sstevel@tonic-gate 	if (mechanism->cm_type == SHA384_HMAC_MECH_INFO_TYPE ||
1967*7c478bd9Sstevel@tonic-gate 	    mechanism->cm_type == SHA384_HMAC_GEN_MECH_INFO_TYPE)
1968*7c478bd9Sstevel@tonic-gate 		SHA2Update(&sha2_hmac_ctx.hc_ocontext, digest,
1969*7c478bd9Sstevel@tonic-gate 		    SHA384_DIGEST_LENGTH);
1970*7c478bd9Sstevel@tonic-gate 	else
1971*7c478bd9Sstevel@tonic-gate 		SHA2Update(&sha2_hmac_ctx.hc_ocontext, digest, sha_digest_len);
1972*7c478bd9Sstevel@tonic-gate 
1973*7c478bd9Sstevel@tonic-gate 	/*
1974*7c478bd9Sstevel@tonic-gate 	 * Do a SHA2 final on the outer context, storing the computed
1975*7c478bd9Sstevel@tonic-gate 	 * digest in the users buffer.
1976*7c478bd9Sstevel@tonic-gate 	 */
1977*7c478bd9Sstevel@tonic-gate 	switch (mac->cd_format) {
1978*7c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
1979*7c478bd9Sstevel@tonic-gate 		if (digest_len != sha_digest_len) {
1980*7c478bd9Sstevel@tonic-gate 			/*
1981*7c478bd9Sstevel@tonic-gate 			 * The caller requested a short digest. Digest
1982*7c478bd9Sstevel@tonic-gate 			 * into a scratch buffer and return to
1983*7c478bd9Sstevel@tonic-gate 			 * the user only what was requested.
1984*7c478bd9Sstevel@tonic-gate 			 */
1985*7c478bd9Sstevel@tonic-gate 			SHA2Final(digest, &sha2_hmac_ctx.hc_ocontext);
1986*7c478bd9Sstevel@tonic-gate 			bcopy(digest, (unsigned char *)mac->cd_raw.iov_base +
1987*7c478bd9Sstevel@tonic-gate 			    mac->cd_offset, digest_len);
1988*7c478bd9Sstevel@tonic-gate 		} else {
1989*7c478bd9Sstevel@tonic-gate 			SHA2Final((unsigned char *)mac->cd_raw.iov_base +
1990*7c478bd9Sstevel@tonic-gate 			    mac->cd_offset, &sha2_hmac_ctx.hc_ocontext);
1991*7c478bd9Sstevel@tonic-gate 		}
1992*7c478bd9Sstevel@tonic-gate 		break;
1993*7c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
1994*7c478bd9Sstevel@tonic-gate 		ret = sha2_digest_final_uio(&sha2_hmac_ctx.hc_ocontext, mac,
1995*7c478bd9Sstevel@tonic-gate 		    digest_len, digest);
1996*7c478bd9Sstevel@tonic-gate 		break;
1997*7c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
1998*7c478bd9Sstevel@tonic-gate 		ret = sha2_digest_final_mblk(&sha2_hmac_ctx.hc_ocontext, mac,
1999*7c478bd9Sstevel@tonic-gate 		    digest_len, digest);
2000*7c478bd9Sstevel@tonic-gate 		break;
2001*7c478bd9Sstevel@tonic-gate 	default:
2002*7c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
2003*7c478bd9Sstevel@tonic-gate 	}
2004*7c478bd9Sstevel@tonic-gate 
2005*7c478bd9Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
2006*7c478bd9Sstevel@tonic-gate 		mac->cd_length = digest_len;
2007*7c478bd9Sstevel@tonic-gate 	} else {
2008*7c478bd9Sstevel@tonic-gate 		/*
2009*7c478bd9Sstevel@tonic-gate 		 * Only bzero outer context on failure, since SHA2Final()
2010*7c478bd9Sstevel@tonic-gate 		 * does it for us.
2011*7c478bd9Sstevel@tonic-gate 		 * We don't have to bzero the inner context since we
2012*7c478bd9Sstevel@tonic-gate 		 * always invoke a SHA2Final() on it.
2013*7c478bd9Sstevel@tonic-gate 		 */
2014*7c478bd9Sstevel@tonic-gate 		bzero(&sha2_hmac_ctx.hc_ocontext, sizeof (SHA2_CTX));
2015*7c478bd9Sstevel@tonic-gate 		mac->cd_length = 0;
2016*7c478bd9Sstevel@tonic-gate 	}
2017*7c478bd9Sstevel@tonic-gate 
2018*7c478bd9Sstevel@tonic-gate 	return (ret);
2019*7c478bd9Sstevel@tonic-gate bail:
2020*7c478bd9Sstevel@tonic-gate 	bzero(&sha2_hmac_ctx, sizeof (sha2_hmac_ctx_t));
2021*7c478bd9Sstevel@tonic-gate 	mac->cd_length = 0;
2022*7c478bd9Sstevel@tonic-gate 	return (ret);
2023*7c478bd9Sstevel@tonic-gate }
2024*7c478bd9Sstevel@tonic-gate 
2025*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
2026*7c478bd9Sstevel@tonic-gate static int
2027*7c478bd9Sstevel@tonic-gate sha2_mac_verify_atomic(crypto_provider_handle_t provider,
2028*7c478bd9Sstevel@tonic-gate     crypto_session_id_t session_id, crypto_mechanism_t *mechanism,
2029*7c478bd9Sstevel@tonic-gate     crypto_key_t *key, crypto_data_t *data, crypto_data_t *mac,
2030*7c478bd9Sstevel@tonic-gate     crypto_spi_ctx_template_t ctx_template, crypto_req_handle_t req)
2031*7c478bd9Sstevel@tonic-gate {
2032*7c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
2033*7c478bd9Sstevel@tonic-gate 	uchar_t digest[SHA512_DIGEST_LENGTH];
2034*7c478bd9Sstevel@tonic-gate 	sha2_hmac_ctx_t sha2_hmac_ctx;
2035*7c478bd9Sstevel@tonic-gate 	uint32_t sha_digest_len, digest_len, sha_hmac_block_size;
2036*7c478bd9Sstevel@tonic-gate 	uint_t keylen_in_bytes = CRYPTO_BITS2BYTES(key->ck_length);
2037*7c478bd9Sstevel@tonic-gate 
2038*7c478bd9Sstevel@tonic-gate 	/*
2039*7c478bd9Sstevel@tonic-gate 	 * Set the digest length and block size to values approriate to the
2040*7c478bd9Sstevel@tonic-gate 	 * mechanism
2041*7c478bd9Sstevel@tonic-gate 	 */
2042*7c478bd9Sstevel@tonic-gate 	switch (mechanism->cm_type) {
2043*7c478bd9Sstevel@tonic-gate 	case SHA256_HMAC_MECH_INFO_TYPE:
2044*7c478bd9Sstevel@tonic-gate 	case SHA256_HMAC_GEN_MECH_INFO_TYPE:
2045*7c478bd9Sstevel@tonic-gate 		sha_digest_len = digest_len = SHA256_DIGEST_LENGTH;
2046*7c478bd9Sstevel@tonic-gate 		sha_hmac_block_size = SHA256_HMAC_BLOCK_SIZE;
2047*7c478bd9Sstevel@tonic-gate 		break;
2048*7c478bd9Sstevel@tonic-gate 	case SHA384_HMAC_MECH_INFO_TYPE:
2049*7c478bd9Sstevel@tonic-gate 	case SHA384_HMAC_GEN_MECH_INFO_TYPE:
2050*7c478bd9Sstevel@tonic-gate 	case SHA512_HMAC_MECH_INFO_TYPE:
2051*7c478bd9Sstevel@tonic-gate 	case SHA512_HMAC_GEN_MECH_INFO_TYPE:
2052*7c478bd9Sstevel@tonic-gate 		sha_digest_len = digest_len = SHA512_DIGEST_LENGTH;
2053*7c478bd9Sstevel@tonic-gate 		sha_hmac_block_size = SHA512_HMAC_BLOCK_SIZE;
2054*7c478bd9Sstevel@tonic-gate 		break;
2055*7c478bd9Sstevel@tonic-gate 	default:
2056*7c478bd9Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
2057*7c478bd9Sstevel@tonic-gate 	}
2058*7c478bd9Sstevel@tonic-gate 
2059*7c478bd9Sstevel@tonic-gate 	/* Add support for key by attributes (RFE 4706552) */
2060*7c478bd9Sstevel@tonic-gate 	if (key->ck_format != CRYPTO_KEY_RAW)
2061*7c478bd9Sstevel@tonic-gate 		return (CRYPTO_ARGUMENTS_BAD);
2062*7c478bd9Sstevel@tonic-gate 
2063*7c478bd9Sstevel@tonic-gate 	if (ctx_template != NULL) {
2064*7c478bd9Sstevel@tonic-gate 		/* reuse context template */
2065*7c478bd9Sstevel@tonic-gate 		bcopy(ctx_template, &sha2_hmac_ctx, sizeof (sha2_hmac_ctx_t));
2066*7c478bd9Sstevel@tonic-gate 	} else {
2067*7c478bd9Sstevel@tonic-gate 		/* no context template, initialize context */
2068*7c478bd9Sstevel@tonic-gate 		if (keylen_in_bytes > sha_hmac_block_size) {
2069*7c478bd9Sstevel@tonic-gate 			/*
2070*7c478bd9Sstevel@tonic-gate 			 * Hash the passed-in key to get a smaller key.
2071*7c478bd9Sstevel@tonic-gate 			 * The inner context is used since it hasn't been
2072*7c478bd9Sstevel@tonic-gate 			 * initialized yet.
2073*7c478bd9Sstevel@tonic-gate 			 */
2074*7c478bd9Sstevel@tonic-gate 			PROV_SHA2_DIGEST_KEY(mechanism->cm_type / 3,
2075*7c478bd9Sstevel@tonic-gate 			    &sha2_hmac_ctx.hc_icontext,
2076*7c478bd9Sstevel@tonic-gate 			    key->ck_data, keylen_in_bytes, digest);
2077*7c478bd9Sstevel@tonic-gate 			sha2_mac_init_ctx(&sha2_hmac_ctx, digest,
2078*7c478bd9Sstevel@tonic-gate 			    sha_digest_len);
2079*7c478bd9Sstevel@tonic-gate 		} else {
2080*7c478bd9Sstevel@tonic-gate 			sha2_mac_init_ctx(&sha2_hmac_ctx, key->ck_data,
2081*7c478bd9Sstevel@tonic-gate 			    keylen_in_bytes);
2082*7c478bd9Sstevel@tonic-gate 		}
2083*7c478bd9Sstevel@tonic-gate 	}
2084*7c478bd9Sstevel@tonic-gate 
2085*7c478bd9Sstevel@tonic-gate 	/* get the mechanism parameters, if applicable */
2086*7c478bd9Sstevel@tonic-gate 	if (mechanism->cm_type % 3 == 2) {
2087*7c478bd9Sstevel@tonic-gate 		if (mechanism->cm_param == NULL ||
2088*7c478bd9Sstevel@tonic-gate 		    mechanism->cm_param_len != sizeof (ulong_t)) {
2089*7c478bd9Sstevel@tonic-gate 			ret = CRYPTO_MECHANISM_PARAM_INVALID;
2090*7c478bd9Sstevel@tonic-gate 			goto bail;
2091*7c478bd9Sstevel@tonic-gate 		}
2092*7c478bd9Sstevel@tonic-gate 		PROV_SHA2_GET_DIGEST_LEN(mechanism, digest_len);
2093*7c478bd9Sstevel@tonic-gate 		if (digest_len > sha_digest_len) {
2094*7c478bd9Sstevel@tonic-gate 			ret = CRYPTO_MECHANISM_PARAM_INVALID;
2095*7c478bd9Sstevel@tonic-gate 			goto bail;
2096*7c478bd9Sstevel@tonic-gate 		}
2097*7c478bd9Sstevel@tonic-gate 	}
2098*7c478bd9Sstevel@tonic-gate 
2099*7c478bd9Sstevel@tonic-gate 	if (mac->cd_length != digest_len) {
2100*7c478bd9Sstevel@tonic-gate 		ret = CRYPTO_INVALID_MAC;
2101*7c478bd9Sstevel@tonic-gate 		goto bail;
2102*7c478bd9Sstevel@tonic-gate 	}
2103*7c478bd9Sstevel@tonic-gate 
2104*7c478bd9Sstevel@tonic-gate 	/* do a SHA2 update of the inner context using the specified data */
2105*7c478bd9Sstevel@tonic-gate 	SHA2_MAC_UPDATE(data, sha2_hmac_ctx, ret);
2106*7c478bd9Sstevel@tonic-gate 	if (ret != CRYPTO_SUCCESS)
2107*7c478bd9Sstevel@tonic-gate 		/* the update failed, free context and bail */
2108*7c478bd9Sstevel@tonic-gate 		goto bail;
2109*7c478bd9Sstevel@tonic-gate 
2110*7c478bd9Sstevel@tonic-gate 	/* do a SHA2 final on the inner context */
2111*7c478bd9Sstevel@tonic-gate 	SHA2Final(digest, &sha2_hmac_ctx.hc_icontext);
2112*7c478bd9Sstevel@tonic-gate 
2113*7c478bd9Sstevel@tonic-gate 	/*
2114*7c478bd9Sstevel@tonic-gate 	 * Do an SHA2 update on the outer context, feeding the inner
2115*7c478bd9Sstevel@tonic-gate 	 * digest as data.
2116*7c478bd9Sstevel@tonic-gate 	 */
2117*7c478bd9Sstevel@tonic-gate 	SHA2Update(&sha2_hmac_ctx.hc_ocontext, digest, sha_digest_len);
2118*7c478bd9Sstevel@tonic-gate 
2119*7c478bd9Sstevel@tonic-gate 	/*
2120*7c478bd9Sstevel@tonic-gate 	 * Do a SHA2 final on the outer context, storing the computed
2121*7c478bd9Sstevel@tonic-gate 	 * digest in the users buffer.
2122*7c478bd9Sstevel@tonic-gate 	 */
2123*7c478bd9Sstevel@tonic-gate 	SHA2Final(digest, &sha2_hmac_ctx.hc_ocontext);
2124*7c478bd9Sstevel@tonic-gate 
2125*7c478bd9Sstevel@tonic-gate 	/*
2126*7c478bd9Sstevel@tonic-gate 	 * Compare the computed digest against the expected digest passed
2127*7c478bd9Sstevel@tonic-gate 	 * as argument.
2128*7c478bd9Sstevel@tonic-gate 	 */
2129*7c478bd9Sstevel@tonic-gate 
2130*7c478bd9Sstevel@tonic-gate 	switch (mac->cd_format) {
2131*7c478bd9Sstevel@tonic-gate 
2132*7c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
2133*7c478bd9Sstevel@tonic-gate 		if (bcmp(digest, (unsigned char *)mac->cd_raw.iov_base +
2134*7c478bd9Sstevel@tonic-gate 		    mac->cd_offset, digest_len) != 0)
2135*7c478bd9Sstevel@tonic-gate 			ret = CRYPTO_INVALID_MAC;
2136*7c478bd9Sstevel@tonic-gate 		break;
2137*7c478bd9Sstevel@tonic-gate 
2138*7c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO: {
2139*7c478bd9Sstevel@tonic-gate 		off_t offset = mac->cd_offset;
2140*7c478bd9Sstevel@tonic-gate 		uint_t vec_idx;
2141*7c478bd9Sstevel@tonic-gate 		off_t scratch_offset = 0;
2142*7c478bd9Sstevel@tonic-gate 		size_t length = digest_len;
2143*7c478bd9Sstevel@tonic-gate 		size_t cur_len;
2144*7c478bd9Sstevel@tonic-gate 
2145*7c478bd9Sstevel@tonic-gate 		/* we support only kernel buffer */
2146*7c478bd9Sstevel@tonic-gate 		if (mac->cd_uio->uio_segflg != UIO_SYSSPACE)
2147*7c478bd9Sstevel@tonic-gate 			return (CRYPTO_ARGUMENTS_BAD);
2148*7c478bd9Sstevel@tonic-gate 
2149*7c478bd9Sstevel@tonic-gate 		/* jump to the first iovec containing the expected digest */
2150*7c478bd9Sstevel@tonic-gate 		for (vec_idx = 0;
2151*7c478bd9Sstevel@tonic-gate 		    offset >= mac->cd_uio->uio_iov[vec_idx].iov_len &&
2152*7c478bd9Sstevel@tonic-gate 		    vec_idx < mac->cd_uio->uio_iovcnt;
2153*7c478bd9Sstevel@tonic-gate 		    offset -= mac->cd_uio->uio_iov[vec_idx++].iov_len);
2154*7c478bd9Sstevel@tonic-gate 		if (vec_idx == mac->cd_uio->uio_iovcnt) {
2155*7c478bd9Sstevel@tonic-gate 			/*
2156*7c478bd9Sstevel@tonic-gate 			 * The caller specified an offset that is
2157*7c478bd9Sstevel@tonic-gate 			 * larger than the total size of the buffers
2158*7c478bd9Sstevel@tonic-gate 			 * it provided.
2159*7c478bd9Sstevel@tonic-gate 			 */
2160*7c478bd9Sstevel@tonic-gate 			ret = CRYPTO_DATA_LEN_RANGE;
2161*7c478bd9Sstevel@tonic-gate 			break;
2162*7c478bd9Sstevel@tonic-gate 		}
2163*7c478bd9Sstevel@tonic-gate 
2164*7c478bd9Sstevel@tonic-gate 		/* do the comparison of computed digest vs specified one */
2165*7c478bd9Sstevel@tonic-gate 		while (vec_idx < mac->cd_uio->uio_iovcnt && length > 0) {
2166*7c478bd9Sstevel@tonic-gate 			cur_len = MIN(mac->cd_uio->uio_iov[vec_idx].iov_len -
2167*7c478bd9Sstevel@tonic-gate 			    offset, length);
2168*7c478bd9Sstevel@tonic-gate 
2169*7c478bd9Sstevel@tonic-gate 			if (bcmp(digest + scratch_offset,
2170*7c478bd9Sstevel@tonic-gate 			    mac->cd_uio->uio_iov[vec_idx].iov_base + offset,
2171*7c478bd9Sstevel@tonic-gate 			    cur_len) != 0) {
2172*7c478bd9Sstevel@tonic-gate 				ret = CRYPTO_INVALID_MAC;
2173*7c478bd9Sstevel@tonic-gate 				break;
2174*7c478bd9Sstevel@tonic-gate 			}
2175*7c478bd9Sstevel@tonic-gate 
2176*7c478bd9Sstevel@tonic-gate 			length -= cur_len;
2177*7c478bd9Sstevel@tonic-gate 			vec_idx++;
2178*7c478bd9Sstevel@tonic-gate 			scratch_offset += cur_len;
2179*7c478bd9Sstevel@tonic-gate 			offset = 0;
2180*7c478bd9Sstevel@tonic-gate 		}
2181*7c478bd9Sstevel@tonic-gate 		break;
2182*7c478bd9Sstevel@tonic-gate 	}
2183*7c478bd9Sstevel@tonic-gate 
2184*7c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK: {
2185*7c478bd9Sstevel@tonic-gate 		off_t offset = mac->cd_offset;
2186*7c478bd9Sstevel@tonic-gate 		mblk_t *mp;
2187*7c478bd9Sstevel@tonic-gate 		off_t scratch_offset = 0;
2188*7c478bd9Sstevel@tonic-gate 		size_t length = digest_len;
2189*7c478bd9Sstevel@tonic-gate 		size_t cur_len;
2190*7c478bd9Sstevel@tonic-gate 
2191*7c478bd9Sstevel@tonic-gate 		/* jump to the first mblk_t containing the expected digest */
2192*7c478bd9Sstevel@tonic-gate 		for (mp = mac->cd_mp; mp != NULL && offset >= MBLKL(mp);
2193*7c478bd9Sstevel@tonic-gate 		    offset -= MBLKL(mp), mp = mp->b_cont);
2194*7c478bd9Sstevel@tonic-gate 		if (mp == NULL) {
2195*7c478bd9Sstevel@tonic-gate 			/*
2196*7c478bd9Sstevel@tonic-gate 			 * The caller specified an offset that is larger than
2197*7c478bd9Sstevel@tonic-gate 			 * the total size of the buffers it provided.
2198*7c478bd9Sstevel@tonic-gate 			 */
2199*7c478bd9Sstevel@tonic-gate 			ret = CRYPTO_DATA_LEN_RANGE;
2200*7c478bd9Sstevel@tonic-gate 			break;
2201*7c478bd9Sstevel@tonic-gate 		}
2202*7c478bd9Sstevel@tonic-gate 
2203*7c478bd9Sstevel@tonic-gate 		while (mp != NULL && length > 0) {
2204*7c478bd9Sstevel@tonic-gate 			cur_len = MIN(MBLKL(mp) - offset, length);
2205*7c478bd9Sstevel@tonic-gate 			if (bcmp(digest + scratch_offset,
2206*7c478bd9Sstevel@tonic-gate 			    mp->b_rptr + offset, cur_len) != 0) {
2207*7c478bd9Sstevel@tonic-gate 				ret = CRYPTO_INVALID_MAC;
2208*7c478bd9Sstevel@tonic-gate 				break;
2209*7c478bd9Sstevel@tonic-gate 			}
2210*7c478bd9Sstevel@tonic-gate 
2211*7c478bd9Sstevel@tonic-gate 			length -= cur_len;
2212*7c478bd9Sstevel@tonic-gate 			mp = mp->b_cont;
2213*7c478bd9Sstevel@tonic-gate 			scratch_offset += cur_len;
2214*7c478bd9Sstevel@tonic-gate 			offset = 0;
2215*7c478bd9Sstevel@tonic-gate 		}
2216*7c478bd9Sstevel@tonic-gate 		break;
2217*7c478bd9Sstevel@tonic-gate 	}
2218*7c478bd9Sstevel@tonic-gate 
2219*7c478bd9Sstevel@tonic-gate 	default:
2220*7c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
2221*7c478bd9Sstevel@tonic-gate 	}
2222*7c478bd9Sstevel@tonic-gate 
2223*7c478bd9Sstevel@tonic-gate 	return (ret);
2224*7c478bd9Sstevel@tonic-gate bail:
2225*7c478bd9Sstevel@tonic-gate 	bzero(&sha2_hmac_ctx, sizeof (sha2_hmac_ctx_t));
2226*7c478bd9Sstevel@tonic-gate 	mac->cd_length = 0;
2227*7c478bd9Sstevel@tonic-gate 	return (ret);
2228*7c478bd9Sstevel@tonic-gate }
2229*7c478bd9Sstevel@tonic-gate 
2230*7c478bd9Sstevel@tonic-gate /*
2231*7c478bd9Sstevel@tonic-gate  * KCF software provider context management entry points.
2232*7c478bd9Sstevel@tonic-gate  */
2233*7c478bd9Sstevel@tonic-gate 
2234*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
2235*7c478bd9Sstevel@tonic-gate static int
2236*7c478bd9Sstevel@tonic-gate sha2_create_ctx_template(crypto_provider_handle_t provider,
2237*7c478bd9Sstevel@tonic-gate     crypto_mechanism_t *mechanism, crypto_key_t *key,
2238*7c478bd9Sstevel@tonic-gate     crypto_spi_ctx_template_t *ctx_template, size_t *ctx_template_size,
2239*7c478bd9Sstevel@tonic-gate     crypto_req_handle_t req)
2240*7c478bd9Sstevel@tonic-gate {
2241*7c478bd9Sstevel@tonic-gate 	sha2_hmac_ctx_t *sha2_hmac_ctx_tmpl;
2242*7c478bd9Sstevel@tonic-gate 	uint_t keylen_in_bytes = CRYPTO_BITS2BYTES(key->ck_length);
2243*7c478bd9Sstevel@tonic-gate 	uint32_t sha_digest_len, sha_hmac_block_size;
2244*7c478bd9Sstevel@tonic-gate 
2245*7c478bd9Sstevel@tonic-gate 	/*
2246*7c478bd9Sstevel@tonic-gate 	 * Set the digest length and block size to values approriate to the
2247*7c478bd9Sstevel@tonic-gate 	 * mechanism
2248*7c478bd9Sstevel@tonic-gate 	 */
2249*7c478bd9Sstevel@tonic-gate 	switch (mechanism->cm_type) {
2250*7c478bd9Sstevel@tonic-gate 	case SHA256_HMAC_MECH_INFO_TYPE:
2251*7c478bd9Sstevel@tonic-gate 	case SHA256_HMAC_GEN_MECH_INFO_TYPE:
2252*7c478bd9Sstevel@tonic-gate 		sha_digest_len = SHA256_DIGEST_LENGTH;
2253*7c478bd9Sstevel@tonic-gate 		sha_hmac_block_size = SHA256_HMAC_BLOCK_SIZE;
2254*7c478bd9Sstevel@tonic-gate 		break;
2255*7c478bd9Sstevel@tonic-gate 	case SHA384_HMAC_MECH_INFO_TYPE:
2256*7c478bd9Sstevel@tonic-gate 	case SHA384_HMAC_GEN_MECH_INFO_TYPE:
2257*7c478bd9Sstevel@tonic-gate 	case SHA512_HMAC_MECH_INFO_TYPE:
2258*7c478bd9Sstevel@tonic-gate 	case SHA512_HMAC_GEN_MECH_INFO_TYPE:
2259*7c478bd9Sstevel@tonic-gate 		sha_digest_len = SHA512_DIGEST_LENGTH;
2260*7c478bd9Sstevel@tonic-gate 		sha_hmac_block_size = SHA512_HMAC_BLOCK_SIZE;
2261*7c478bd9Sstevel@tonic-gate 		break;
2262*7c478bd9Sstevel@tonic-gate 	default:
2263*7c478bd9Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
2264*7c478bd9Sstevel@tonic-gate 	}
2265*7c478bd9Sstevel@tonic-gate 
2266*7c478bd9Sstevel@tonic-gate 	/* Add support for key by attributes (RFE 4706552) */
2267*7c478bd9Sstevel@tonic-gate 	if (key->ck_format != CRYPTO_KEY_RAW)
2268*7c478bd9Sstevel@tonic-gate 		return (CRYPTO_ARGUMENTS_BAD);
2269*7c478bd9Sstevel@tonic-gate 
2270*7c478bd9Sstevel@tonic-gate 	/*
2271*7c478bd9Sstevel@tonic-gate 	 * Allocate and initialize SHA2 context.
2272*7c478bd9Sstevel@tonic-gate 	 */
2273*7c478bd9Sstevel@tonic-gate 	sha2_hmac_ctx_tmpl = kmem_alloc(sizeof (sha2_hmac_ctx_t),
2274*7c478bd9Sstevel@tonic-gate 	    crypto_kmflag(req));
2275*7c478bd9Sstevel@tonic-gate 	if (sha2_hmac_ctx_tmpl == NULL)
2276*7c478bd9Sstevel@tonic-gate 		return (CRYPTO_HOST_MEMORY);
2277*7c478bd9Sstevel@tonic-gate 
2278*7c478bd9Sstevel@tonic-gate 	sha2_hmac_ctx_tmpl->hc_mech_type = mechanism->cm_type;
2279*7c478bd9Sstevel@tonic-gate 
2280*7c478bd9Sstevel@tonic-gate 	if (keylen_in_bytes > sha_hmac_block_size) {
2281*7c478bd9Sstevel@tonic-gate 		uchar_t digested_key[SHA512_DIGEST_LENGTH];
2282*7c478bd9Sstevel@tonic-gate 
2283*7c478bd9Sstevel@tonic-gate 		/*
2284*7c478bd9Sstevel@tonic-gate 		 * Hash the passed-in key to get a smaller key.
2285*7c478bd9Sstevel@tonic-gate 		 * The inner context is used since it hasn't been
2286*7c478bd9Sstevel@tonic-gate 		 * initialized yet.
2287*7c478bd9Sstevel@tonic-gate 		 */
2288*7c478bd9Sstevel@tonic-gate 		PROV_SHA2_DIGEST_KEY(mechanism->cm_type / 3,
2289*7c478bd9Sstevel@tonic-gate 		    &sha2_hmac_ctx_tmpl->hc_icontext,
2290*7c478bd9Sstevel@tonic-gate 		    key->ck_data, keylen_in_bytes, digested_key);
2291*7c478bd9Sstevel@tonic-gate 		sha2_mac_init_ctx(sha2_hmac_ctx_tmpl, digested_key,
2292*7c478bd9Sstevel@tonic-gate 		    sha_digest_len);
2293*7c478bd9Sstevel@tonic-gate 	} else {
2294*7c478bd9Sstevel@tonic-gate 		sha2_mac_init_ctx(sha2_hmac_ctx_tmpl, key->ck_data,
2295*7c478bd9Sstevel@tonic-gate 		    keylen_in_bytes);
2296*7c478bd9Sstevel@tonic-gate 	}
2297*7c478bd9Sstevel@tonic-gate 
2298*7c478bd9Sstevel@tonic-gate 	*ctx_template = (crypto_spi_ctx_template_t)sha2_hmac_ctx_tmpl;
2299*7c478bd9Sstevel@tonic-gate 	*ctx_template_size = sizeof (sha2_hmac_ctx_t);
2300*7c478bd9Sstevel@tonic-gate 
2301*7c478bd9Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
2302*7c478bd9Sstevel@tonic-gate }
2303*7c478bd9Sstevel@tonic-gate 
2304*7c478bd9Sstevel@tonic-gate static int
2305*7c478bd9Sstevel@tonic-gate sha2_free_context(crypto_ctx_t *ctx)
2306*7c478bd9Sstevel@tonic-gate {
2307*7c478bd9Sstevel@tonic-gate 	uint_t ctx_len;
2308*7c478bd9Sstevel@tonic-gate 
2309*7c478bd9Sstevel@tonic-gate 	if (ctx->cc_provider_private == NULL)
2310*7c478bd9Sstevel@tonic-gate 		return (CRYPTO_SUCCESS);
2311*7c478bd9Sstevel@tonic-gate 
2312*7c478bd9Sstevel@tonic-gate 	/*
2313*7c478bd9Sstevel@tonic-gate 	 * We have to free either SHA2 or SHA2-HMAC contexts, which
2314*7c478bd9Sstevel@tonic-gate 	 * have different lengths.
2315*7c478bd9Sstevel@tonic-gate 	 *
2316*7c478bd9Sstevel@tonic-gate 	 * Note: Below is dependent on the mechanism ordering.
2317*7c478bd9Sstevel@tonic-gate 	 */
2318*7c478bd9Sstevel@tonic-gate 
2319*7c478bd9Sstevel@tonic-gate 	if (PROV_SHA2_CTX(ctx)->sc_mech_type % 3 == 0)
2320*7c478bd9Sstevel@tonic-gate 		ctx_len = sizeof (sha2_ctx_t);
2321*7c478bd9Sstevel@tonic-gate 	else
2322*7c478bd9Sstevel@tonic-gate 		ctx_len = sizeof (sha2_hmac_ctx_t);
2323*7c478bd9Sstevel@tonic-gate 
2324*7c478bd9Sstevel@tonic-gate 	bzero(ctx->cc_provider_private, ctx_len);
2325*7c478bd9Sstevel@tonic-gate 	kmem_free(ctx->cc_provider_private, ctx_len);
2326*7c478bd9Sstevel@tonic-gate 	ctx->cc_provider_private = NULL;
2327*7c478bd9Sstevel@tonic-gate 
2328*7c478bd9Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
2329*7c478bd9Sstevel@tonic-gate }
2330*7c478bd9Sstevel@tonic-gate 
2331*7c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
2332*7c478bd9Sstevel@tonic-gate 
2333*7c478bd9Sstevel@tonic-gate void
2334*7c478bd9Sstevel@tonic-gate SHA2Init(uint64_t mech, SHA2_CTX *ctx)
2335*7c478bd9Sstevel@tonic-gate {
2336*7c478bd9Sstevel@tonic-gate 
2337*7c478bd9Sstevel@tonic-gate 	switch (mech) {
2338*7c478bd9Sstevel@tonic-gate 	case SHA256_MECH_INFO_TYPE:
2339*7c478bd9Sstevel@tonic-gate 	case SHA256_HMAC_MECH_INFO_TYPE:
2340*7c478bd9Sstevel@tonic-gate 	case SHA256_HMAC_GEN_MECH_INFO_TYPE:
2341*7c478bd9Sstevel@tonic-gate 		ctx->state.s32[0] = 0x6a09e667U;
2342*7c478bd9Sstevel@tonic-gate 		ctx->state.s32[1] = 0xbb67ae85U;
2343*7c478bd9Sstevel@tonic-gate 		ctx->state.s32[2] = 0x3c6ef372U;
2344*7c478bd9Sstevel@tonic-gate 		ctx->state.s32[3] = 0xa54ff53aU;
2345*7c478bd9Sstevel@tonic-gate 		ctx->state.s32[4] = 0x510e527fU;
2346*7c478bd9Sstevel@tonic-gate 		ctx->state.s32[5] = 0x9b05688cU;
2347*7c478bd9Sstevel@tonic-gate 		ctx->state.s32[6] = 0x1f83d9abU;
2348*7c478bd9Sstevel@tonic-gate 		ctx->state.s32[7] = 0x5be0cd19U;
2349*7c478bd9Sstevel@tonic-gate 		break;
2350*7c478bd9Sstevel@tonic-gate 	case SHA384_MECH_INFO_TYPE:
2351*7c478bd9Sstevel@tonic-gate 	case SHA384_HMAC_MECH_INFO_TYPE:
2352*7c478bd9Sstevel@tonic-gate 	case SHA384_HMAC_GEN_MECH_INFO_TYPE:
2353*7c478bd9Sstevel@tonic-gate 		ctx->state.s64[0] = 0xcbbb9d5dc1059ed8ULL;
2354*7c478bd9Sstevel@tonic-gate 		ctx->state.s64[1] = 0x629a292a367cd507ULL;
2355*7c478bd9Sstevel@tonic-gate 		ctx->state.s64[2] = 0x9159015a3070dd17ULL;
2356*7c478bd9Sstevel@tonic-gate 		ctx->state.s64[3] = 0x152fecd8f70e5939ULL;
2357*7c478bd9Sstevel@tonic-gate 		ctx->state.s64[4] = 0x67332667ffc00b31ULL;
2358*7c478bd9Sstevel@tonic-gate 		ctx->state.s64[5] = 0x8eb44a8768581511ULL;
2359*7c478bd9Sstevel@tonic-gate 		ctx->state.s64[6] = 0xdb0c2e0d64f98fa7ULL;
2360*7c478bd9Sstevel@tonic-gate 		ctx->state.s64[7] = 0x47b5481dbefa4fa4ULL;
2361*7c478bd9Sstevel@tonic-gate 		break;
2362*7c478bd9Sstevel@tonic-gate 	case SHA512_MECH_INFO_TYPE:
2363*7c478bd9Sstevel@tonic-gate 	case SHA512_HMAC_MECH_INFO_TYPE:
2364*7c478bd9Sstevel@tonic-gate 	case SHA512_HMAC_GEN_MECH_INFO_TYPE:
2365*7c478bd9Sstevel@tonic-gate 		ctx->state.s64[0] = 0x6a09e667f3bcc908ULL;
2366*7c478bd9Sstevel@tonic-gate 		ctx->state.s64[1] = 0xbb67ae8584caa73bULL;
2367*7c478bd9Sstevel@tonic-gate 		ctx->state.s64[2] = 0x3c6ef372fe94f82bULL;
2368*7c478bd9Sstevel@tonic-gate 		ctx->state.s64[3] = 0xa54ff53a5f1d36f1ULL;
2369*7c478bd9Sstevel@tonic-gate 		ctx->state.s64[4] = 0x510e527fade682d1ULL;
2370*7c478bd9Sstevel@tonic-gate 		ctx->state.s64[5] = 0x9b05688c2b3e6c1fULL;
2371*7c478bd9Sstevel@tonic-gate 		ctx->state.s64[6] = 0x1f83d9abfb41bd6bULL;
2372*7c478bd9Sstevel@tonic-gate 		ctx->state.s64[7] = 0x5be0cd19137e2179ULL;
2373*7c478bd9Sstevel@tonic-gate 		break;
2374*7c478bd9Sstevel@tonic-gate #ifdef _KERNEL
2375*7c478bd9Sstevel@tonic-gate 	default:
2376*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "sha2_init: "
2377*7c478bd9Sstevel@tonic-gate 		    "failed to find a supported algorithm: 0x%x",
2378*7c478bd9Sstevel@tonic-gate 		    (uint32_t)mech);
2379*7c478bd9Sstevel@tonic-gate 
2380*7c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
2381*7c478bd9Sstevel@tonic-gate 	}
2382*7c478bd9Sstevel@tonic-gate 
2383*7c478bd9Sstevel@tonic-gate 	ctx->algotype = mech;
2384*7c478bd9Sstevel@tonic-gate 	ctx->count.c64[0] = ctx->count.c64[1] = 0;
2385*7c478bd9Sstevel@tonic-gate }
2386*7c478bd9Sstevel@tonic-gate 
2387*7c478bd9Sstevel@tonic-gate /*
2388*7c478bd9Sstevel@tonic-gate  * SHA2Update()
2389*7c478bd9Sstevel@tonic-gate  *
2390*7c478bd9Sstevel@tonic-gate  * purpose: continues an sha2 digest operation, using the message block
2391*7c478bd9Sstevel@tonic-gate  *          to update the context.
2392*7c478bd9Sstevel@tonic-gate  *   input: SHA2_CTX *	: the context to update
2393*7c478bd9Sstevel@tonic-gate  *          uint8_t *	: the message block
2394*7c478bd9Sstevel@tonic-gate  *          uint32_t    : the length of the message block in bytes
2395*7c478bd9Sstevel@tonic-gate  *  output: void
2396*7c478bd9Sstevel@tonic-gate  */
2397*7c478bd9Sstevel@tonic-gate 
2398*7c478bd9Sstevel@tonic-gate void
2399*7c478bd9Sstevel@tonic-gate SHA2Update(SHA2_CTX *ctx, const uint8_t *input, uint32_t input_len)
2400*7c478bd9Sstevel@tonic-gate {
2401*7c478bd9Sstevel@tonic-gate 	uint32_t i, buf_index, buf_len, buf_limit;
2402*7c478bd9Sstevel@tonic-gate 
2403*7c478bd9Sstevel@tonic-gate 	/* check for noop */
2404*7c478bd9Sstevel@tonic-gate 	if (input_len == 0)
2405*7c478bd9Sstevel@tonic-gate 		return;
2406*7c478bd9Sstevel@tonic-gate 
2407*7c478bd9Sstevel@tonic-gate 	if (ctx->algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE) {
2408*7c478bd9Sstevel@tonic-gate 		buf_limit = 64;
2409*7c478bd9Sstevel@tonic-gate 
2410*7c478bd9Sstevel@tonic-gate 		/* compute number of bytes mod 64 */
2411*7c478bd9Sstevel@tonic-gate 		buf_index = (ctx->count.c32[1] >> 3) & 0x3F;
2412*7c478bd9Sstevel@tonic-gate 
2413*7c478bd9Sstevel@tonic-gate 		/* update number of bits */
2414*7c478bd9Sstevel@tonic-gate 		if ((ctx->count.c32[1] += (input_len << 3)) < (input_len << 3))
2415*7c478bd9Sstevel@tonic-gate 			ctx->count.c32[0]++;
2416*7c478bd9Sstevel@tonic-gate 
2417*7c478bd9Sstevel@tonic-gate 		ctx->count.c32[0] += (input_len >> 29);
2418*7c478bd9Sstevel@tonic-gate 
2419*7c478bd9Sstevel@tonic-gate 	} else {
2420*7c478bd9Sstevel@tonic-gate 		buf_limit = 128;
2421*7c478bd9Sstevel@tonic-gate 
2422*7c478bd9Sstevel@tonic-gate 		/* compute number of bytes mod 128 */
2423*7c478bd9Sstevel@tonic-gate 		buf_index = (ctx->count.c64[1] >> 3) & 0x7F;
2424*7c478bd9Sstevel@tonic-gate 
2425*7c478bd9Sstevel@tonic-gate 		/* update number of bits */
2426*7c478bd9Sstevel@tonic-gate 		if ((ctx->count.c64[1] += (input_len << 3)) < (input_len << 3))
2427*7c478bd9Sstevel@tonic-gate 			ctx->count.c64[0]++;
2428*7c478bd9Sstevel@tonic-gate 
2429*7c478bd9Sstevel@tonic-gate 		ctx->count.c64[0] += (input_len >> 29);
2430*7c478bd9Sstevel@tonic-gate 	}
2431*7c478bd9Sstevel@tonic-gate 
2432*7c478bd9Sstevel@tonic-gate 	buf_len = buf_limit - buf_index;
2433*7c478bd9Sstevel@tonic-gate 
2434*7c478bd9Sstevel@tonic-gate 	/* transform as many times as possible */
2435*7c478bd9Sstevel@tonic-gate 	i = 0;
2436*7c478bd9Sstevel@tonic-gate 	if (input_len >= buf_len) {
2437*7c478bd9Sstevel@tonic-gate 
2438*7c478bd9Sstevel@tonic-gate 		/*
2439*7c478bd9Sstevel@tonic-gate 		 * general optimization:
2440*7c478bd9Sstevel@tonic-gate 		 *
2441*7c478bd9Sstevel@tonic-gate 		 * only do initial bcopy() and SHA2Transform() if
2442*7c478bd9Sstevel@tonic-gate 		 * buf_index != 0.  if buf_index == 0, we're just
2443*7c478bd9Sstevel@tonic-gate 		 * wasting our time doing the bcopy() since there
2444*7c478bd9Sstevel@tonic-gate 		 * wasn't any data left over from a previous call to
2445*7c478bd9Sstevel@tonic-gate 		 * SHA2Update().
2446*7c478bd9Sstevel@tonic-gate 		 */
2447*7c478bd9Sstevel@tonic-gate 		if (buf_index) {
2448*7c478bd9Sstevel@tonic-gate 			bcopy(input, &ctx->buf_un.buf8[buf_index], buf_len);
2449*7c478bd9Sstevel@tonic-gate 			if (ctx->algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE)
2450*7c478bd9Sstevel@tonic-gate 				SHA256Transform(ctx, ctx->buf_un.buf8);
2451*7c478bd9Sstevel@tonic-gate 			else
2452*7c478bd9Sstevel@tonic-gate 				SHA512Transform(ctx, ctx->buf_un.buf8);
2453*7c478bd9Sstevel@tonic-gate 
2454*7c478bd9Sstevel@tonic-gate 			i = buf_len;
2455*7c478bd9Sstevel@tonic-gate 		}
2456*7c478bd9Sstevel@tonic-gate 
2457*7c478bd9Sstevel@tonic-gate 
2458*7c478bd9Sstevel@tonic-gate 		for (; i + buf_limit - 1 < input_len; i += buf_limit) {
2459*7c478bd9Sstevel@tonic-gate 			if (ctx->algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE)
2460*7c478bd9Sstevel@tonic-gate 				SHA256Transform(ctx, &input[i]);
2461*7c478bd9Sstevel@tonic-gate 			else
2462*7c478bd9Sstevel@tonic-gate 				SHA512Transform(ctx, &input[i]);
2463*7c478bd9Sstevel@tonic-gate 		}
2464*7c478bd9Sstevel@tonic-gate 
2465*7c478bd9Sstevel@tonic-gate 		/*
2466*7c478bd9Sstevel@tonic-gate 		 * general optimization:
2467*7c478bd9Sstevel@tonic-gate 		 *
2468*7c478bd9Sstevel@tonic-gate 		 * if i and input_len are the same, return now instead
2469*7c478bd9Sstevel@tonic-gate 		 * of calling bcopy(), since the bcopy() in this case
2470*7c478bd9Sstevel@tonic-gate 		 * will be an expensive nop.
2471*7c478bd9Sstevel@tonic-gate 		 */
2472*7c478bd9Sstevel@tonic-gate 
2473*7c478bd9Sstevel@tonic-gate 		if (input_len == i)
2474*7c478bd9Sstevel@tonic-gate 			return;
2475*7c478bd9Sstevel@tonic-gate 
2476*7c478bd9Sstevel@tonic-gate 		buf_index = 0;
2477*7c478bd9Sstevel@tonic-gate 	}
2478*7c478bd9Sstevel@tonic-gate 
2479*7c478bd9Sstevel@tonic-gate 	/* buffer remaining input */
2480*7c478bd9Sstevel@tonic-gate 	bcopy(&input[i], &ctx->buf_un.buf8[buf_index], input_len - i);
2481*7c478bd9Sstevel@tonic-gate }
2482*7c478bd9Sstevel@tonic-gate 
2483*7c478bd9Sstevel@tonic-gate 
2484*7c478bd9Sstevel@tonic-gate /*
2485*7c478bd9Sstevel@tonic-gate  * SHA2Final()
2486*7c478bd9Sstevel@tonic-gate  *
2487*7c478bd9Sstevel@tonic-gate  * purpose: ends an sha2 digest operation, finalizing the message digest and
2488*7c478bd9Sstevel@tonic-gate  *          zeroing the context.
2489*7c478bd9Sstevel@tonic-gate  *   input: uint8_t *	: a buffer to store the digest in
2490*7c478bd9Sstevel@tonic-gate  *          SHA2_CTX *  : the context to finalize, save, and zero
2491*7c478bd9Sstevel@tonic-gate  *  output: void
2492*7c478bd9Sstevel@tonic-gate  */
2493*7c478bd9Sstevel@tonic-gate 
2494*7c478bd9Sstevel@tonic-gate 
2495*7c478bd9Sstevel@tonic-gate void
2496*7c478bd9Sstevel@tonic-gate SHA2Final(uint8_t *digest, SHA2_CTX *ctx)
2497*7c478bd9Sstevel@tonic-gate {
2498*7c478bd9Sstevel@tonic-gate 	uint8_t		bitcount_be[sizeof (ctx->count.c32)];
2499*7c478bd9Sstevel@tonic-gate 	uint8_t		bitcount_be64[sizeof (ctx->count.c64)];
2500*7c478bd9Sstevel@tonic-gate 	uint32_t	index;
2501*7c478bd9Sstevel@tonic-gate 
2502*7c478bd9Sstevel@tonic-gate 
2503*7c478bd9Sstevel@tonic-gate 	if (ctx->algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE) {
2504*7c478bd9Sstevel@tonic-gate 		index  = (ctx->count.c32[1] >> 3) & 0x3f;
2505*7c478bd9Sstevel@tonic-gate 		Encode(bitcount_be, ctx->count.c32, sizeof (bitcount_be));
2506*7c478bd9Sstevel@tonic-gate 		SHA2Update(ctx, PADDING, ((index < 56) ? 56 : 120) - index);
2507*7c478bd9Sstevel@tonic-gate 		SHA2Update(ctx, bitcount_be, sizeof (bitcount_be));
2508*7c478bd9Sstevel@tonic-gate 		Encode(digest, ctx->state.s32, sizeof (ctx->state.s32));
2509*7c478bd9Sstevel@tonic-gate 
2510*7c478bd9Sstevel@tonic-gate 	} else {
2511*7c478bd9Sstevel@tonic-gate 		index  = (ctx->count.c64[1] >> 3) & 0x7f;
2512*7c478bd9Sstevel@tonic-gate 		Encode64(bitcount_be64, ctx->count.c64,
2513*7c478bd9Sstevel@tonic-gate 		    sizeof (bitcount_be64));
2514*7c478bd9Sstevel@tonic-gate 		SHA2Update(ctx, PADDING, ((index < 112) ? 112 : 240) - index);
2515*7c478bd9Sstevel@tonic-gate 		SHA2Update(ctx, bitcount_be64, sizeof (bitcount_be64));
2516*7c478bd9Sstevel@tonic-gate 		if (ctx->algotype <= SHA384_HMAC_GEN_MECH_INFO_TYPE) {
2517*7c478bd9Sstevel@tonic-gate 			ctx->state.s64[6] = ctx->state.s64[7] = 0;
2518*7c478bd9Sstevel@tonic-gate 			Encode64(digest, ctx->state.s64,
2519*7c478bd9Sstevel@tonic-gate 			    sizeof (uint64_t) * 6);
2520*7c478bd9Sstevel@tonic-gate 		} else
2521*7c478bd9Sstevel@tonic-gate 			Encode64(digest, ctx->state.s64,
2522*7c478bd9Sstevel@tonic-gate 			    sizeof (ctx->state.s64));
2523*7c478bd9Sstevel@tonic-gate 	}
2524*7c478bd9Sstevel@tonic-gate }
2525