xref: /onnv-gate/usr/src/uts/common/crypto/api/kcf_digest.c (revision 4072:3b30e48bc1a6)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51808Smcpowers  * Common Development and Distribution License (the "License").
61808Smcpowers  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
223367Skrishna  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/errno.h>
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/kmem.h>
310Sstevel@tonic-gate #include <sys/cmn_err.h>
32904Smcpowers #include <sys/sysmacros.h>
330Sstevel@tonic-gate #include <sys/crypto/common.h>
340Sstevel@tonic-gate #include <sys/crypto/impl.h>
350Sstevel@tonic-gate #include <sys/crypto/api.h>
360Sstevel@tonic-gate #include <sys/crypto/spi.h>
370Sstevel@tonic-gate #include <sys/crypto/sched_impl.h>
380Sstevel@tonic-gate 
39904Smcpowers #define	CRYPTO_OPS_OFFSET(f)		offsetof(crypto_ops_t, co_##f)
40904Smcpowers #define	CRYPTO_DIGEST_OFFSET(f)		offsetof(crypto_digest_ops_t, f)
41904Smcpowers 
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate  * Message digest routines
440Sstevel@tonic-gate  */
450Sstevel@tonic-gate 
460Sstevel@tonic-gate /*
470Sstevel@tonic-gate  * The following are the possible returned values common to all the routines
480Sstevel@tonic-gate  * below. The applicability of some of these return values depends on the
490Sstevel@tonic-gate  * presence of the arguments.
500Sstevel@tonic-gate  *
510Sstevel@tonic-gate  *	CRYPTO_SUCCESS:	The operation completed successfully.
520Sstevel@tonic-gate  *	CRYPTO_QUEUED:	A request was submitted successfully. The callback
530Sstevel@tonic-gate  *			routine will be called when the operation is done.
540Sstevel@tonic-gate  *	CRYPTO_MECHANISM_INVALID or CRYPTO_INVALID_MECH_PARAM
550Sstevel@tonic-gate  *			for problems with the 'mech'.
560Sstevel@tonic-gate  *	CRYPTO_INVALID_DATA for bogus 'data'
570Sstevel@tonic-gate  *	CRYPTO_HOST_MEMORY for failure to allocate memory to handle this work.
580Sstevel@tonic-gate  *	CRYPTO_INVALID_CONTEXT: Not a valid context.
590Sstevel@tonic-gate  *	CRYPTO_BUSY:	Cannot process the request now. Schedule a
600Sstevel@tonic-gate  *			crypto_bufcall(), or try later.
610Sstevel@tonic-gate  *	CRYPTO_NOT_SUPPORTED and CRYPTO_MECH_NOT_SUPPORTED:
620Sstevel@tonic-gate  *			No provider is capable of a function or a mechanism.
630Sstevel@tonic-gate  */
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /*
670Sstevel@tonic-gate  * crypto_digest_prov()
680Sstevel@tonic-gate  *
690Sstevel@tonic-gate  * Arguments:
700Sstevel@tonic-gate  *	pd:	pointer to the descriptor of the provider to use for this
710Sstevel@tonic-gate  *		operation.
720Sstevel@tonic-gate  *	sid:	provider session id.
730Sstevel@tonic-gate  *	mech:	crypto_mechanism_t pointer.
740Sstevel@tonic-gate  *		mech_type is a valid value previously returned by
750Sstevel@tonic-gate  *		crypto_mech2id();
760Sstevel@tonic-gate  *		When the mech's parameter is not NULL, its definition depends
770Sstevel@tonic-gate  *		on the standard definition of the mechanism.
780Sstevel@tonic-gate  *	data:	The message to be digested.
790Sstevel@tonic-gate  *	digest:	Storage for the digest. The length needed depends on the
800Sstevel@tonic-gate  *		mechanism.
810Sstevel@tonic-gate  *	cr:	crypto_call_req_t calling conditions and call back info.
820Sstevel@tonic-gate  *
830Sstevel@tonic-gate  * Description:
840Sstevel@tonic-gate  *	Asynchronously submits a request for, or synchronously performs the
850Sstevel@tonic-gate  *	digesting operation of 'data' on the specified
860Sstevel@tonic-gate  *	provider with the specified session.
870Sstevel@tonic-gate  *	When complete and successful, 'digest' will contain the digest value.
880Sstevel@tonic-gate  *	The caller should hold a reference on the specified provider
890Sstevel@tonic-gate  *	descriptor before calling this function.
900Sstevel@tonic-gate  *
910Sstevel@tonic-gate  * Context:
920Sstevel@tonic-gate  *	Process or interrupt, according to the semantics dictated by the 'cr'.
930Sstevel@tonic-gate  *
940Sstevel@tonic-gate  * Returns:
950Sstevel@tonic-gate  *	See comment in the beginning of the file.
960Sstevel@tonic-gate  */
970Sstevel@tonic-gate int
98904Smcpowers crypto_digest_prov(crypto_provider_t provider, crypto_session_id_t sid,
99904Smcpowers     crypto_mechanism_t *mech, crypto_data_t *data, crypto_data_t *digest,
100904Smcpowers     crypto_call_req_t *crq)
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate 	kcf_req_params_t params;
103904Smcpowers 	kcf_provider_desc_t *pd = provider;
104904Smcpowers 	kcf_provider_desc_t *real_provider = pd;
105904Smcpowers 	int rv;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	ASSERT(KCF_PROV_REFHELD(pd));
108904Smcpowers 
109904Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
110904Smcpowers 		rv = kcf_get_hardware_provider(mech->cm_type,
1111808Smcpowers 		    CRYPTO_MECH_INVALID, CHECK_RESTRICT(crq),
1121808Smcpowers 		    pd, &real_provider, CRYPTO_FG_DIGEST_ATOMIC);
113904Smcpowers 
114904Smcpowers 		if (rv != CRYPTO_SUCCESS)
115904Smcpowers 			return (rv);
116904Smcpowers 	}
1170Sstevel@tonic-gate 	KCF_WRAP_DIGEST_OPS_PARAMS(&params, KCF_OP_ATOMIC, sid, mech, NULL,
1180Sstevel@tonic-gate 	    data, digest);
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	/* no crypto context to carry between multiple parts. */
121904Smcpowers 	rv = kcf_submit_request(real_provider, NULL, crq, &params, B_FALSE);
122904Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
123904Smcpowers 		KCF_PROV_REFRELE(real_provider);
124904Smcpowers 
125904Smcpowers 	return (rv);
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate /*
1290Sstevel@tonic-gate  * Same as crypto_digest_prov(), but relies on the KCF scheduler to
1300Sstevel@tonic-gate  * choose a provider. See crypto_digest_prov() comments for more information.
1310Sstevel@tonic-gate  */
1320Sstevel@tonic-gate int
1330Sstevel@tonic-gate crypto_digest(crypto_mechanism_t *mech, crypto_data_t *data,
1340Sstevel@tonic-gate     crypto_data_t *digest, crypto_call_req_t *crq)
1350Sstevel@tonic-gate {
1360Sstevel@tonic-gate 	int error;
1370Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
1380Sstevel@tonic-gate 	kcf_req_params_t params;
1390Sstevel@tonic-gate 	kcf_prov_tried_t *list = NULL;
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate retry:
1420Sstevel@tonic-gate 	/* The pd is returned held */
1430Sstevel@tonic-gate 	if ((pd = kcf_get_mech_provider(mech->cm_type, NULL, &error, list,
1440Sstevel@tonic-gate 	    CRYPTO_FG_DIGEST_ATOMIC, CHECK_RESTRICT(crq),
1450Sstevel@tonic-gate 	    data->cd_length)) == NULL) {
1460Sstevel@tonic-gate 		if (list != NULL)
1470Sstevel@tonic-gate 			kcf_free_triedlist(list);
1480Sstevel@tonic-gate 		return (error);
1490Sstevel@tonic-gate 	}
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	/* The fast path for SW providers. */
1520Sstevel@tonic-gate 	if (CHECK_FASTPATH(crq, pd)) {
1530Sstevel@tonic-gate 		crypto_mechanism_t lmech;
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 		lmech = *mech;
1560Sstevel@tonic-gate 		KCF_SET_PROVIDER_MECHNUM(mech->cm_type, pd, &lmech);
1570Sstevel@tonic-gate 		error = KCF_PROV_DIGEST_ATOMIC(pd, pd->pd_sid, &lmech, data,
1580Sstevel@tonic-gate 		    digest, KCF_SWFP_RHNDL(crq));
1590Sstevel@tonic-gate 		KCF_PROV_INCRSTATS(pd, error);
1600Sstevel@tonic-gate 	} else {
161*4072Skrishna 		if (pd->pd_prov_type == CRYPTO_HW_PROVIDER &&
162*4072Skrishna 		    (pd->pd_flags & CRYPTO_HASH_NO_UPDATE) &&
163*4072Skrishna 		    (data->cd_length > pd->pd_hash_limit)) {
164*4072Skrishna 			error = CRYPTO_BUFFER_TOO_BIG;
165*4072Skrishna 		} else {
166*4072Skrishna 			KCF_WRAP_DIGEST_OPS_PARAMS(&params, KCF_OP_ATOMIC,
167*4072Skrishna 			    pd->pd_sid, mech, NULL, data, digest);
1680Sstevel@tonic-gate 
169*4072Skrishna 			/* no crypto context to carry between multiple parts. */
170*4072Skrishna 			error = kcf_submit_request(pd, NULL, crq, &params,
171*4072Skrishna 			    B_FALSE);
172*4072Skrishna 		}
1730Sstevel@tonic-gate 	}
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED &&
1760Sstevel@tonic-gate 	    IS_RECOVERABLE(error)) {
1770Sstevel@tonic-gate 		/* Add pd to the linked list of providers tried. */
1780Sstevel@tonic-gate 		if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL)
1790Sstevel@tonic-gate 			goto retry;
1800Sstevel@tonic-gate 	}
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	if (list != NULL)
1830Sstevel@tonic-gate 		kcf_free_triedlist(list);
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	KCF_PROV_REFRELE(pd);
1860Sstevel@tonic-gate 	return (error);
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate /*
1900Sstevel@tonic-gate  * crypto_digest_init_prov()
1910Sstevel@tonic-gate  *
1920Sstevel@tonic-gate  *	pd:	pointer to the descriptor of the provider to use for this
1930Sstevel@tonic-gate  *		operation.
1940Sstevel@tonic-gate  *	sid:	provider session id.
1950Sstevel@tonic-gate  *	mech:	crypto_mechanism_t pointer.
1960Sstevel@tonic-gate  *		mech_type is a valid value previously returned by
1970Sstevel@tonic-gate  *		crypto_mech2id();
1980Sstevel@tonic-gate  *		When the mech's parameter is not NULL, its definition depends
1990Sstevel@tonic-gate  *		on the standard definition of the mechanism.
2000Sstevel@tonic-gate  *	ctxp:	Pointer to a crypto_context_t.
2010Sstevel@tonic-gate  *	cr:	crypto_call_req_t calling conditions and call back info.
2020Sstevel@tonic-gate  *
2030Sstevel@tonic-gate  * Description:
2040Sstevel@tonic-gate  *	Asynchronously submits a request for, or synchronously performs the
2050Sstevel@tonic-gate  *	initialization of a message digest operation on the specified
2060Sstevel@tonic-gate  *	provider with the specified session.
2070Sstevel@tonic-gate  *	When complete and successful, 'ctxp' will contain a crypto_context_t
2080Sstevel@tonic-gate  *	valid for later calls to digest_update() and digest_final().
2090Sstevel@tonic-gate  *	The caller should hold a reference on the specified provider
2100Sstevel@tonic-gate  *	descriptor before calling this function.
2110Sstevel@tonic-gate  */
2120Sstevel@tonic-gate int
213904Smcpowers crypto_digest_init_prov(crypto_provider_t provider, crypto_session_id_t sid,
214904Smcpowers     crypto_mechanism_t *mech, crypto_context_t *ctxp, crypto_call_req_t  *crq)
2150Sstevel@tonic-gate {
2160Sstevel@tonic-gate 	int error;
2170Sstevel@tonic-gate 	crypto_ctx_t *ctx;
2180Sstevel@tonic-gate 	kcf_req_params_t params;
219904Smcpowers 	kcf_provider_desc_t *pd = provider;
220904Smcpowers 	kcf_provider_desc_t *real_provider = pd;
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	ASSERT(KCF_PROV_REFHELD(pd));
2230Sstevel@tonic-gate 
224904Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
225904Smcpowers 		error = kcf_get_hardware_provider(mech->cm_type,
2261808Smcpowers 		    CRYPTO_MECH_INVALID, CHECK_RESTRICT(crq), pd,
2271808Smcpowers 		    &real_provider, CRYPTO_FG_DIGEST);
228904Smcpowers 
229904Smcpowers 		if (error != CRYPTO_SUCCESS)
230904Smcpowers 			return (error);
231904Smcpowers 	}
232904Smcpowers 
233904Smcpowers 	/* Allocate and initialize the canonical context */
234904Smcpowers 	if ((ctx = kcf_new_ctx(crq, real_provider, sid)) == NULL) {
235904Smcpowers 		if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
236904Smcpowers 			KCF_PROV_REFRELE(real_provider);
2370Sstevel@tonic-gate 		return (CRYPTO_HOST_MEMORY);
238904Smcpowers 	}
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	/* The fast path for SW providers. */
2410Sstevel@tonic-gate 	if (CHECK_FASTPATH(crq, pd)) {
2420Sstevel@tonic-gate 		crypto_mechanism_t lmech;
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 		lmech = *mech;
245904Smcpowers 		KCF_SET_PROVIDER_MECHNUM(mech->cm_type, real_provider, &lmech);
246904Smcpowers 		error = KCF_PROV_DIGEST_INIT(real_provider, ctx, &lmech,
2470Sstevel@tonic-gate 		    KCF_SWFP_RHNDL(crq));
2480Sstevel@tonic-gate 		KCF_PROV_INCRSTATS(pd, error);
2490Sstevel@tonic-gate 	} else {
2500Sstevel@tonic-gate 		KCF_WRAP_DIGEST_OPS_PARAMS(&params, KCF_OP_INIT, sid,
2510Sstevel@tonic-gate 		    mech, NULL, NULL, NULL);
252904Smcpowers 		error = kcf_submit_request(real_provider, ctx, crq, &params,
253904Smcpowers 		    B_FALSE);
2540Sstevel@tonic-gate 	}
2550Sstevel@tonic-gate 
256904Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
257904Smcpowers 		KCF_PROV_REFRELE(real_provider);
258904Smcpowers 
2590Sstevel@tonic-gate 	if ((error == CRYPTO_SUCCESS) || (error == CRYPTO_QUEUED))
2600Sstevel@tonic-gate 		*ctxp = (crypto_context_t)ctx;
2610Sstevel@tonic-gate 	else {
2620Sstevel@tonic-gate 		/* Release the hold done in kcf_new_ctx(). */
2630Sstevel@tonic-gate 		KCF_CONTEXT_REFRELE((kcf_context_t *)ctx->cc_framework_private);
2640Sstevel@tonic-gate 	}
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	return (error);
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate /*
2710Sstevel@tonic-gate  * Same as crypto_digest_init_prov(), but relies on the KCF scheduler
2720Sstevel@tonic-gate  * to choose a provider. See crypto_digest_init_prov() comments for
2730Sstevel@tonic-gate  * more information.
2740Sstevel@tonic-gate  */
2750Sstevel@tonic-gate int
2760Sstevel@tonic-gate crypto_digest_init(crypto_mechanism_t *mech, crypto_context_t *ctxp,
2770Sstevel@tonic-gate     crypto_call_req_t  *crq)
2780Sstevel@tonic-gate {
2790Sstevel@tonic-gate 	int error;
2800Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
2810Sstevel@tonic-gate 	kcf_prov_tried_t *list = NULL;
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate retry:
2840Sstevel@tonic-gate 	/* The pd is returned held */
2850Sstevel@tonic-gate 	if ((pd = kcf_get_mech_provider(mech->cm_type, NULL, &error,
2860Sstevel@tonic-gate 	    list, CRYPTO_FG_DIGEST, CHECK_RESTRICT(crq), 0)) == NULL) {
2870Sstevel@tonic-gate 		if (list != NULL)
2880Sstevel@tonic-gate 			kcf_free_triedlist(list);
2890Sstevel@tonic-gate 		return (error);
2900Sstevel@tonic-gate 	}
2910Sstevel@tonic-gate 
292*4072Skrishna 	if (pd->pd_prov_type == CRYPTO_HW_PROVIDER &&
293*4072Skrishna 	    (pd->pd_flags & CRYPTO_HASH_NO_UPDATE)) {
294*4072Skrishna 		/*
295*4072Skrishna 		 * The hardware provider has limited digest support.
296*4072Skrishna 		 * So, we fallback early here to using a software provider.
297*4072Skrishna 		 *
298*4072Skrishna 		 * XXX - need to enhance to do the fallback later in
299*4072Skrishna 		 * crypto_digest_update() if the size of accumulated input data
300*4072Skrishna 		 * exceeds the maximum size digestable by hardware provider.
301*4072Skrishna 		 */
302*4072Skrishna 		error = CRYPTO_BUFFER_TOO_BIG;
303*4072Skrishna 	} else {
304*4072Skrishna 		error = crypto_digest_init_prov(pd, pd->pd_sid,
305*4072Skrishna 		    mech, ctxp, crq);
306*4072Skrishna 	}
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED &&
3090Sstevel@tonic-gate 	    IS_RECOVERABLE(error)) {
3100Sstevel@tonic-gate 		/* Add pd to the linked list of providers tried. */
3110Sstevel@tonic-gate 		if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL)
3120Sstevel@tonic-gate 			goto retry;
3130Sstevel@tonic-gate 	}
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	if (list != NULL)
3160Sstevel@tonic-gate 		kcf_free_triedlist(list);
3170Sstevel@tonic-gate 	KCF_PROV_REFRELE(pd);
3180Sstevel@tonic-gate 	return (error);
3190Sstevel@tonic-gate }
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate /*
3220Sstevel@tonic-gate  * crypto_digest_update()
3230Sstevel@tonic-gate  *
3240Sstevel@tonic-gate  * Arguments:
3250Sstevel@tonic-gate  *	context: A crypto_context_t initialized by digest_init().
3260Sstevel@tonic-gate  *	data:	The part of message to be digested.
3270Sstevel@tonic-gate  *	cr:	crypto_call_req_t calling conditions and call back info.
3280Sstevel@tonic-gate  *
3290Sstevel@tonic-gate  * Description:
3300Sstevel@tonic-gate  *	Asynchronously submits a request for, or synchronously performs a
3310Sstevel@tonic-gate  *	part of a message digest operation.
3320Sstevel@tonic-gate  *
3330Sstevel@tonic-gate  * Context:
3340Sstevel@tonic-gate  *	Process or interrupt, according to the semantics dictated by the 'cr'.
3350Sstevel@tonic-gate  *
3360Sstevel@tonic-gate  * Returns:
3370Sstevel@tonic-gate  *	See comment in the beginning of the file.
3380Sstevel@tonic-gate  */
3390Sstevel@tonic-gate int
3400Sstevel@tonic-gate crypto_digest_update(crypto_context_t context, crypto_data_t *data,
3410Sstevel@tonic-gate     crypto_call_req_t *cr)
3420Sstevel@tonic-gate {
3430Sstevel@tonic-gate 	crypto_ctx_t *ctx = (crypto_ctx_t *)context;
3440Sstevel@tonic-gate 	kcf_context_t *kcf_ctx;
3450Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
3460Sstevel@tonic-gate 	int error;
3470Sstevel@tonic-gate 	kcf_req_params_t params;
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	if ((ctx == NULL) ||
3500Sstevel@tonic-gate 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
3510Sstevel@tonic-gate 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
3520Sstevel@tonic-gate 		return (CRYPTO_INVALID_CONTEXT);
3530Sstevel@tonic-gate 	}
3540Sstevel@tonic-gate 
355904Smcpowers 	ASSERT(pd->pd_prov_type != CRYPTO_LOGICAL_PROVIDER);
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 	/* The fast path for SW providers. */
3580Sstevel@tonic-gate 	if (CHECK_FASTPATH(cr, pd)) {
3590Sstevel@tonic-gate 		error = KCF_PROV_DIGEST_UPDATE(pd, ctx, data, NULL);
3600Sstevel@tonic-gate 		KCF_PROV_INCRSTATS(pd, error);
3610Sstevel@tonic-gate 	} else {
362904Smcpowers 		KCF_WRAP_DIGEST_OPS_PARAMS(&params, KCF_OP_UPDATE,
363904Smcpowers 		    ctx->cc_session, NULL, NULL, data, NULL);
3640Sstevel@tonic-gate 		error = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
3650Sstevel@tonic-gate 	}
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	return (error);
3680Sstevel@tonic-gate }
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate /*
3710Sstevel@tonic-gate  * crypto_digest_final()
3720Sstevel@tonic-gate  *
3730Sstevel@tonic-gate  * Arguments:
3740Sstevel@tonic-gate  *	context: A crypto_context_t initialized by digest_init().
3750Sstevel@tonic-gate  *	digest:	The storage for the digest.
3760Sstevel@tonic-gate  *	cr:	crypto_call_req_t calling conditions and call back info.
3770Sstevel@tonic-gate  *
3780Sstevel@tonic-gate  * Description:
3790Sstevel@tonic-gate  *	Asynchronously submits a request for, or synchronously performs the
3800Sstevel@tonic-gate  *	final part of a message digest operation.
3810Sstevel@tonic-gate  *
3820Sstevel@tonic-gate  * Context:
3830Sstevel@tonic-gate  *	Process or interrupt, according to the semantics dictated by the 'cr'.
3840Sstevel@tonic-gate  *
3850Sstevel@tonic-gate  * Returns:
3860Sstevel@tonic-gate  *	See comment in the beginning of the file.
3870Sstevel@tonic-gate  */
3880Sstevel@tonic-gate int
3890Sstevel@tonic-gate crypto_digest_final(crypto_context_t context, crypto_data_t *digest,
3900Sstevel@tonic-gate     crypto_call_req_t *cr)
3910Sstevel@tonic-gate {
3920Sstevel@tonic-gate 	crypto_ctx_t *ctx = (crypto_ctx_t *)context;
3930Sstevel@tonic-gate 	kcf_context_t *kcf_ctx;
3940Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
3950Sstevel@tonic-gate 	int error;
3960Sstevel@tonic-gate 	kcf_req_params_t params;
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	if ((ctx == NULL) ||
3990Sstevel@tonic-gate 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
4000Sstevel@tonic-gate 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
4010Sstevel@tonic-gate 		return (CRYPTO_INVALID_CONTEXT);
4020Sstevel@tonic-gate 	}
4030Sstevel@tonic-gate 
404904Smcpowers 	ASSERT(pd->pd_prov_type != CRYPTO_LOGICAL_PROVIDER);
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	/* The fast path for SW providers. */
4070Sstevel@tonic-gate 	if (CHECK_FASTPATH(cr, pd)) {
4080Sstevel@tonic-gate 		error = KCF_PROV_DIGEST_FINAL(pd, ctx, digest, NULL);
4090Sstevel@tonic-gate 		KCF_PROV_INCRSTATS(pd, error);
4100Sstevel@tonic-gate 	} else {
411904Smcpowers 		KCF_WRAP_DIGEST_OPS_PARAMS(&params, KCF_OP_FINAL,
412904Smcpowers 		    ctx->cc_session, NULL, NULL, NULL, digest);
4130Sstevel@tonic-gate 		error = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
4140Sstevel@tonic-gate 	}
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	/* Release the hold done in kcf_new_ctx() during init step. */
4170Sstevel@tonic-gate 	KCF_CONTEXT_COND_RELEASE(error, kcf_ctx);
4180Sstevel@tonic-gate 	return (error);
4190Sstevel@tonic-gate }
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate /*
4220Sstevel@tonic-gate  * Performs a digest update on the specified key. Note that there is
4230Sstevel@tonic-gate  * no k-API crypto_digest_key() equivalent of this function.
4240Sstevel@tonic-gate  */
4250Sstevel@tonic-gate int
4260Sstevel@tonic-gate crypto_digest_key_prov(crypto_context_t context, crypto_key_t *key,
4270Sstevel@tonic-gate     crypto_call_req_t *cr)
4280Sstevel@tonic-gate {
4290Sstevel@tonic-gate 	crypto_ctx_t *ctx = (crypto_ctx_t *)context;
4300Sstevel@tonic-gate 	kcf_context_t *kcf_ctx;
4310Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
4320Sstevel@tonic-gate 	int error;
4330Sstevel@tonic-gate 	kcf_req_params_t params;
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	if ((ctx == NULL) ||
4360Sstevel@tonic-gate 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
4370Sstevel@tonic-gate 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
4380Sstevel@tonic-gate 		return (CRYPTO_INVALID_CONTEXT);
4390Sstevel@tonic-gate 	}
4400Sstevel@tonic-gate 
441904Smcpowers 	ASSERT(pd->pd_prov_type != CRYPTO_LOGICAL_PROVIDER);
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	/* The fast path for SW providers. */
4440Sstevel@tonic-gate 	if (CHECK_FASTPATH(cr, pd)) {
4450Sstevel@tonic-gate 		error = KCF_PROV_DIGEST_KEY(pd, ctx, key, NULL);
4460Sstevel@tonic-gate 		KCF_PROV_INCRSTATS(pd, error);
4470Sstevel@tonic-gate 	} else {
4480Sstevel@tonic-gate 		KCF_WRAP_DIGEST_OPS_PARAMS(&params, KCF_OP_DIGEST_KEY,
449904Smcpowers 		    ctx->cc_session, NULL, key, NULL, NULL);
4500Sstevel@tonic-gate 		error = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
4510Sstevel@tonic-gate 	}
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	return (error);
4540Sstevel@tonic-gate }
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate /*
4570Sstevel@tonic-gate  * See comments for crypto_digest_update() and crypto_digest_final().
4580Sstevel@tonic-gate  */
4590Sstevel@tonic-gate int
4600Sstevel@tonic-gate crypto_digest_single(crypto_context_t context, crypto_data_t *data,
4610Sstevel@tonic-gate     crypto_data_t *digest, crypto_call_req_t *cr)
4620Sstevel@tonic-gate {
4630Sstevel@tonic-gate 	crypto_ctx_t *ctx = (crypto_ctx_t *)context;
4640Sstevel@tonic-gate 	kcf_context_t *kcf_ctx;
4650Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
4660Sstevel@tonic-gate 	int error;
4670Sstevel@tonic-gate 	kcf_req_params_t params;
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	if ((ctx == NULL) ||
4700Sstevel@tonic-gate 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
4710Sstevel@tonic-gate 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
4720Sstevel@tonic-gate 		return (CRYPTO_INVALID_CONTEXT);
4730Sstevel@tonic-gate 	}
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	/* The fast path for SW providers. */
4770Sstevel@tonic-gate 	if (CHECK_FASTPATH(cr, pd)) {
4780Sstevel@tonic-gate 		error = KCF_PROV_DIGEST(pd, ctx, data, digest, NULL);
4790Sstevel@tonic-gate 		KCF_PROV_INCRSTATS(pd, error);
4800Sstevel@tonic-gate 	} else {
4810Sstevel@tonic-gate 		KCF_WRAP_DIGEST_OPS_PARAMS(&params, KCF_OP_SINGLE, pd->pd_sid,
4820Sstevel@tonic-gate 		    NULL, NULL, data, digest);
4830Sstevel@tonic-gate 		error = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
4840Sstevel@tonic-gate 	}
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	/* Release the hold done in kcf_new_ctx() during init step. */
4870Sstevel@tonic-gate 	KCF_CONTEXT_COND_RELEASE(error, kcf_ctx);
4880Sstevel@tonic-gate 	return (error);
4890Sstevel@tonic-gate }
490