xref: /onnv-gate/usr/src/uts/common/crypto/api/kcf_digest.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <sys/errno.h>
30*0Sstevel@tonic-gate #include <sys/types.h>
31*0Sstevel@tonic-gate #include <sys/kmem.h>
32*0Sstevel@tonic-gate #include <sys/cmn_err.h>
33*0Sstevel@tonic-gate #include <sys/crypto/common.h>
34*0Sstevel@tonic-gate #include <sys/crypto/impl.h>
35*0Sstevel@tonic-gate #include <sys/crypto/api.h>
36*0Sstevel@tonic-gate #include <sys/crypto/spi.h>
37*0Sstevel@tonic-gate #include <sys/crypto/sched_impl.h>
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate /*
40*0Sstevel@tonic-gate  * Message digest routines
41*0Sstevel@tonic-gate  */
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate /*
44*0Sstevel@tonic-gate  * The following are the possible returned values common to all the routines
45*0Sstevel@tonic-gate  * below. The applicability of some of these return values depends on the
46*0Sstevel@tonic-gate  * presence of the arguments.
47*0Sstevel@tonic-gate  *
48*0Sstevel@tonic-gate  *	CRYPTO_SUCCESS:	The operation completed successfully.
49*0Sstevel@tonic-gate  *	CRYPTO_QUEUED:	A request was submitted successfully. The callback
50*0Sstevel@tonic-gate  *			routine will be called when the operation is done.
51*0Sstevel@tonic-gate  *	CRYPTO_MECHANISM_INVALID or CRYPTO_INVALID_MECH_PARAM
52*0Sstevel@tonic-gate  *			for problems with the 'mech'.
53*0Sstevel@tonic-gate  *	CRYPTO_INVALID_DATA for bogus 'data'
54*0Sstevel@tonic-gate  *	CRYPTO_HOST_MEMORY for failure to allocate memory to handle this work.
55*0Sstevel@tonic-gate  *	CRYPTO_INVALID_CONTEXT: Not a valid context.
56*0Sstevel@tonic-gate  *	CRYPTO_BUSY:	Cannot process the request now. Schedule a
57*0Sstevel@tonic-gate  *			crypto_bufcall(), or try later.
58*0Sstevel@tonic-gate  *	CRYPTO_NOT_SUPPORTED and CRYPTO_MECH_NOT_SUPPORTED:
59*0Sstevel@tonic-gate  *			No provider is capable of a function or a mechanism.
60*0Sstevel@tonic-gate  */
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate /*
64*0Sstevel@tonic-gate  * crypto_digest_prov()
65*0Sstevel@tonic-gate  *
66*0Sstevel@tonic-gate  * Arguments:
67*0Sstevel@tonic-gate  *	pd:	pointer to the descriptor of the provider to use for this
68*0Sstevel@tonic-gate  *		operation.
69*0Sstevel@tonic-gate  *	sid:	provider session id.
70*0Sstevel@tonic-gate  *	mech:	crypto_mechanism_t pointer.
71*0Sstevel@tonic-gate  *		mech_type is a valid value previously returned by
72*0Sstevel@tonic-gate  *		crypto_mech2id();
73*0Sstevel@tonic-gate  *		When the mech's parameter is not NULL, its definition depends
74*0Sstevel@tonic-gate  *		on the standard definition of the mechanism.
75*0Sstevel@tonic-gate  *	data:	The message to be digested.
76*0Sstevel@tonic-gate  *	digest:	Storage for the digest. The length needed depends on the
77*0Sstevel@tonic-gate  *		mechanism.
78*0Sstevel@tonic-gate  *	cr:	crypto_call_req_t calling conditions and call back info.
79*0Sstevel@tonic-gate  *
80*0Sstevel@tonic-gate  * Description:
81*0Sstevel@tonic-gate  *	Asynchronously submits a request for, or synchronously performs the
82*0Sstevel@tonic-gate  *	digesting operation of 'data' on the specified
83*0Sstevel@tonic-gate  *	provider with the specified session.
84*0Sstevel@tonic-gate  *	When complete and successful, 'digest' will contain the digest value.
85*0Sstevel@tonic-gate  *	The caller should hold a reference on the specified provider
86*0Sstevel@tonic-gate  *	descriptor before calling this function.
87*0Sstevel@tonic-gate  *
88*0Sstevel@tonic-gate  * Context:
89*0Sstevel@tonic-gate  *	Process or interrupt, according to the semantics dictated by the 'cr'.
90*0Sstevel@tonic-gate  *
91*0Sstevel@tonic-gate  * Returns:
92*0Sstevel@tonic-gate  *	See comment in the beginning of the file.
93*0Sstevel@tonic-gate  */
94*0Sstevel@tonic-gate int
95*0Sstevel@tonic-gate crypto_digest_prov(crypto_mechanism_t *mech, crypto_data_t *data,
96*0Sstevel@tonic-gate     crypto_data_t *digest, crypto_call_req_t *crq, kcf_provider_desc_t *pd,
97*0Sstevel@tonic-gate     crypto_session_id_t sid)
98*0Sstevel@tonic-gate {
99*0Sstevel@tonic-gate 	kcf_req_params_t params;
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	ASSERT(KCF_PROV_REFHELD(pd));
102*0Sstevel@tonic-gate 	KCF_WRAP_DIGEST_OPS_PARAMS(&params, KCF_OP_ATOMIC, sid, mech, NULL,
103*0Sstevel@tonic-gate 	    data, digest);
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 	/* no crypto context to carry between multiple parts. */
106*0Sstevel@tonic-gate 	return (kcf_submit_request(pd, NULL, crq, &params, B_FALSE));
107*0Sstevel@tonic-gate }
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate /*
110*0Sstevel@tonic-gate  * Same as crypto_digest_prov(), but relies on the KCF scheduler to
111*0Sstevel@tonic-gate  * choose a provider. See crypto_digest_prov() comments for more information.
112*0Sstevel@tonic-gate  */
113*0Sstevel@tonic-gate int
114*0Sstevel@tonic-gate crypto_digest(crypto_mechanism_t *mech, crypto_data_t *data,
115*0Sstevel@tonic-gate     crypto_data_t *digest, crypto_call_req_t *crq)
116*0Sstevel@tonic-gate {
117*0Sstevel@tonic-gate 	int error;
118*0Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
119*0Sstevel@tonic-gate 	kcf_req_params_t params;
120*0Sstevel@tonic-gate 	kcf_prov_tried_t *list = NULL;
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate retry:
123*0Sstevel@tonic-gate 	/* The pd is returned held */
124*0Sstevel@tonic-gate 	if ((pd = kcf_get_mech_provider(mech->cm_type, NULL, &error, list,
125*0Sstevel@tonic-gate 	    CRYPTO_FG_DIGEST_ATOMIC, CHECK_RESTRICT(crq),
126*0Sstevel@tonic-gate 	    data->cd_length)) == NULL) {
127*0Sstevel@tonic-gate 		if (list != NULL)
128*0Sstevel@tonic-gate 			kcf_free_triedlist(list);
129*0Sstevel@tonic-gate 		return (error);
130*0Sstevel@tonic-gate 	}
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 	/* The fast path for SW providers. */
133*0Sstevel@tonic-gate 	if (CHECK_FASTPATH(crq, pd)) {
134*0Sstevel@tonic-gate 		crypto_mechanism_t lmech;
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 		lmech = *mech;
137*0Sstevel@tonic-gate 		KCF_SET_PROVIDER_MECHNUM(mech->cm_type, pd, &lmech);
138*0Sstevel@tonic-gate 		error = KCF_PROV_DIGEST_ATOMIC(pd, pd->pd_sid, &lmech, data,
139*0Sstevel@tonic-gate 		    digest, KCF_SWFP_RHNDL(crq));
140*0Sstevel@tonic-gate 		KCF_PROV_INCRSTATS(pd, error);
141*0Sstevel@tonic-gate 	} else {
142*0Sstevel@tonic-gate 		KCF_WRAP_DIGEST_OPS_PARAMS(&params, KCF_OP_ATOMIC, pd->pd_sid,
143*0Sstevel@tonic-gate 		    mech, NULL, data, digest);
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 		/* no crypto context to carry between multiple parts. */
146*0Sstevel@tonic-gate 		error = kcf_submit_request(pd, NULL, crq, &params, B_FALSE);
147*0Sstevel@tonic-gate 	}
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 	if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED &&
150*0Sstevel@tonic-gate 	    IS_RECOVERABLE(error)) {
151*0Sstevel@tonic-gate 		/* Add pd to the linked list of providers tried. */
152*0Sstevel@tonic-gate 		if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL)
153*0Sstevel@tonic-gate 			goto retry;
154*0Sstevel@tonic-gate 	}
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	if (list != NULL)
157*0Sstevel@tonic-gate 		kcf_free_triedlist(list);
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 	KCF_PROV_REFRELE(pd);
160*0Sstevel@tonic-gate 	return (error);
161*0Sstevel@tonic-gate }
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate /*
164*0Sstevel@tonic-gate  * crypto_digest_init_prov()
165*0Sstevel@tonic-gate  *
166*0Sstevel@tonic-gate  *	pd:	pointer to the descriptor of the provider to use for this
167*0Sstevel@tonic-gate  *		operation.
168*0Sstevel@tonic-gate  *	sid:	provider session id.
169*0Sstevel@tonic-gate  *	mech:	crypto_mechanism_t pointer.
170*0Sstevel@tonic-gate  *		mech_type is a valid value previously returned by
171*0Sstevel@tonic-gate  *		crypto_mech2id();
172*0Sstevel@tonic-gate  *		When the mech's parameter is not NULL, its definition depends
173*0Sstevel@tonic-gate  *		on the standard definition of the mechanism.
174*0Sstevel@tonic-gate  *	ctxp:	Pointer to a crypto_context_t.
175*0Sstevel@tonic-gate  *	cr:	crypto_call_req_t calling conditions and call back info.
176*0Sstevel@tonic-gate  *
177*0Sstevel@tonic-gate  * Description:
178*0Sstevel@tonic-gate  *	Asynchronously submits a request for, or synchronously performs the
179*0Sstevel@tonic-gate  *	initialization of a message digest operation on the specified
180*0Sstevel@tonic-gate  *	provider with the specified session.
181*0Sstevel@tonic-gate  *	When complete and successful, 'ctxp' will contain a crypto_context_t
182*0Sstevel@tonic-gate  *	valid for later calls to digest_update() and digest_final().
183*0Sstevel@tonic-gate  *	The caller should hold a reference on the specified provider
184*0Sstevel@tonic-gate  *	descriptor before calling this function.
185*0Sstevel@tonic-gate  */
186*0Sstevel@tonic-gate int
187*0Sstevel@tonic-gate crypto_digest_init_prov(kcf_provider_desc_t *pd,
188*0Sstevel@tonic-gate     crypto_session_id_t sid, crypto_mechanism_t *mech,
189*0Sstevel@tonic-gate     crypto_context_t *ctxp, crypto_call_req_t  *crq)
190*0Sstevel@tonic-gate {
191*0Sstevel@tonic-gate 	int error;
192*0Sstevel@tonic-gate 	crypto_ctx_t *ctx;
193*0Sstevel@tonic-gate 	kcf_req_params_t params;
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 	ASSERT(KCF_PROV_REFHELD(pd));
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 	/* First, allocate and initialize the canonical context */
198*0Sstevel@tonic-gate 	if ((ctx = kcf_new_ctx(crq, pd, sid)) == NULL)
199*0Sstevel@tonic-gate 		return (CRYPTO_HOST_MEMORY);
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 	/* The fast path for SW providers. */
202*0Sstevel@tonic-gate 	if (CHECK_FASTPATH(crq, pd)) {
203*0Sstevel@tonic-gate 		crypto_mechanism_t lmech;
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate 		lmech = *mech;
206*0Sstevel@tonic-gate 		KCF_SET_PROVIDER_MECHNUM(mech->cm_type, pd, &lmech);
207*0Sstevel@tonic-gate 		error = KCF_PROV_DIGEST_INIT(pd, ctx, &lmech,
208*0Sstevel@tonic-gate 		    KCF_SWFP_RHNDL(crq));
209*0Sstevel@tonic-gate 		KCF_PROV_INCRSTATS(pd, error);
210*0Sstevel@tonic-gate 	} else {
211*0Sstevel@tonic-gate 		KCF_WRAP_DIGEST_OPS_PARAMS(&params, KCF_OP_INIT, sid,
212*0Sstevel@tonic-gate 		    mech, NULL, NULL, NULL);
213*0Sstevel@tonic-gate 		error = kcf_submit_request(pd, ctx, crq, &params, B_FALSE);
214*0Sstevel@tonic-gate 	}
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate 	if ((error == CRYPTO_SUCCESS) || (error == CRYPTO_QUEUED))
217*0Sstevel@tonic-gate 		*ctxp = (crypto_context_t)ctx;
218*0Sstevel@tonic-gate 	else {
219*0Sstevel@tonic-gate 		/* Release the hold done in kcf_new_ctx(). */
220*0Sstevel@tonic-gate 		KCF_CONTEXT_REFRELE((kcf_context_t *)ctx->cc_framework_private);
221*0Sstevel@tonic-gate 	}
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 	return (error);
224*0Sstevel@tonic-gate }
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate /*
228*0Sstevel@tonic-gate  * Same as crypto_digest_init_prov(), but relies on the KCF scheduler
229*0Sstevel@tonic-gate  * to choose a provider. See crypto_digest_init_prov() comments for
230*0Sstevel@tonic-gate  * more information.
231*0Sstevel@tonic-gate  */
232*0Sstevel@tonic-gate int
233*0Sstevel@tonic-gate crypto_digest_init(crypto_mechanism_t *mech, crypto_context_t *ctxp,
234*0Sstevel@tonic-gate     crypto_call_req_t  *crq)
235*0Sstevel@tonic-gate {
236*0Sstevel@tonic-gate 	int error;
237*0Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
238*0Sstevel@tonic-gate 	kcf_prov_tried_t *list = NULL;
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate retry:
241*0Sstevel@tonic-gate 	/* The pd is returned held */
242*0Sstevel@tonic-gate 	if ((pd = kcf_get_mech_provider(mech->cm_type, NULL, &error,
243*0Sstevel@tonic-gate 	    list, CRYPTO_FG_DIGEST, CHECK_RESTRICT(crq), 0)) == NULL) {
244*0Sstevel@tonic-gate 		if (list != NULL)
245*0Sstevel@tonic-gate 			kcf_free_triedlist(list);
246*0Sstevel@tonic-gate 		return (error);
247*0Sstevel@tonic-gate 	}
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate 	error = crypto_digest_init_prov(pd, pd->pd_sid, mech, ctxp, crq);
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate 	if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED &&
252*0Sstevel@tonic-gate 	    IS_RECOVERABLE(error)) {
253*0Sstevel@tonic-gate 		/* Add pd to the linked list of providers tried. */
254*0Sstevel@tonic-gate 		if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL)
255*0Sstevel@tonic-gate 			goto retry;
256*0Sstevel@tonic-gate 	}
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate 	if (list != NULL)
259*0Sstevel@tonic-gate 		kcf_free_triedlist(list);
260*0Sstevel@tonic-gate 	KCF_PROV_REFRELE(pd);
261*0Sstevel@tonic-gate 	return (error);
262*0Sstevel@tonic-gate }
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate /*
265*0Sstevel@tonic-gate  * crypto_digest_update()
266*0Sstevel@tonic-gate  *
267*0Sstevel@tonic-gate  * Arguments:
268*0Sstevel@tonic-gate  *	context: A crypto_context_t initialized by digest_init().
269*0Sstevel@tonic-gate  *	data:	The part of message to be digested.
270*0Sstevel@tonic-gate  *	cr:	crypto_call_req_t calling conditions and call back info.
271*0Sstevel@tonic-gate  *
272*0Sstevel@tonic-gate  * Description:
273*0Sstevel@tonic-gate  *	Asynchronously submits a request for, or synchronously performs a
274*0Sstevel@tonic-gate  *	part of a message digest operation.
275*0Sstevel@tonic-gate  *
276*0Sstevel@tonic-gate  * Context:
277*0Sstevel@tonic-gate  *	Process or interrupt, according to the semantics dictated by the 'cr'.
278*0Sstevel@tonic-gate  *
279*0Sstevel@tonic-gate  * Returns:
280*0Sstevel@tonic-gate  *	See comment in the beginning of the file.
281*0Sstevel@tonic-gate  */
282*0Sstevel@tonic-gate int
283*0Sstevel@tonic-gate crypto_digest_update(crypto_context_t context, crypto_data_t *data,
284*0Sstevel@tonic-gate     crypto_call_req_t *cr)
285*0Sstevel@tonic-gate {
286*0Sstevel@tonic-gate 	crypto_ctx_t *ctx = (crypto_ctx_t *)context;
287*0Sstevel@tonic-gate 	kcf_context_t *kcf_ctx;
288*0Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
289*0Sstevel@tonic-gate 	int error;
290*0Sstevel@tonic-gate 	kcf_req_params_t params;
291*0Sstevel@tonic-gate 
292*0Sstevel@tonic-gate 	if ((ctx == NULL) ||
293*0Sstevel@tonic-gate 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
294*0Sstevel@tonic-gate 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
295*0Sstevel@tonic-gate 		return (CRYPTO_INVALID_CONTEXT);
296*0Sstevel@tonic-gate 	}
297*0Sstevel@tonic-gate 
298*0Sstevel@tonic-gate 	KCF_PROV_REFHOLD(pd);
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 	/* The fast path for SW providers. */
301*0Sstevel@tonic-gate 	if (CHECK_FASTPATH(cr, pd)) {
302*0Sstevel@tonic-gate 		error = KCF_PROV_DIGEST_UPDATE(pd, ctx, data, NULL);
303*0Sstevel@tonic-gate 		KCF_PROV_INCRSTATS(pd, error);
304*0Sstevel@tonic-gate 	} else {
305*0Sstevel@tonic-gate 		KCF_WRAP_DIGEST_OPS_PARAMS(&params, KCF_OP_UPDATE, pd->pd_sid,
306*0Sstevel@tonic-gate 		    NULL, NULL, data, NULL);
307*0Sstevel@tonic-gate 		error = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
308*0Sstevel@tonic-gate 	}
309*0Sstevel@tonic-gate 
310*0Sstevel@tonic-gate 	KCF_PROV_REFRELE(pd);
311*0Sstevel@tonic-gate 	return (error);
312*0Sstevel@tonic-gate }
313*0Sstevel@tonic-gate 
314*0Sstevel@tonic-gate /*
315*0Sstevel@tonic-gate  * crypto_digest_final()
316*0Sstevel@tonic-gate  *
317*0Sstevel@tonic-gate  * Arguments:
318*0Sstevel@tonic-gate  *	context: A crypto_context_t initialized by digest_init().
319*0Sstevel@tonic-gate  *	digest:	The storage for the digest.
320*0Sstevel@tonic-gate  *	cr:	crypto_call_req_t calling conditions and call back info.
321*0Sstevel@tonic-gate  *
322*0Sstevel@tonic-gate  * Description:
323*0Sstevel@tonic-gate  *	Asynchronously submits a request for, or synchronously performs the
324*0Sstevel@tonic-gate  *	final part of a message digest operation.
325*0Sstevel@tonic-gate  *
326*0Sstevel@tonic-gate  * Context:
327*0Sstevel@tonic-gate  *	Process or interrupt, according to the semantics dictated by the 'cr'.
328*0Sstevel@tonic-gate  *
329*0Sstevel@tonic-gate  * Returns:
330*0Sstevel@tonic-gate  *	See comment in the beginning of the file.
331*0Sstevel@tonic-gate  */
332*0Sstevel@tonic-gate int
333*0Sstevel@tonic-gate crypto_digest_final(crypto_context_t context, crypto_data_t *digest,
334*0Sstevel@tonic-gate     crypto_call_req_t *cr)
335*0Sstevel@tonic-gate {
336*0Sstevel@tonic-gate 	crypto_ctx_t *ctx = (crypto_ctx_t *)context;
337*0Sstevel@tonic-gate 	kcf_context_t *kcf_ctx;
338*0Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
339*0Sstevel@tonic-gate 	int error;
340*0Sstevel@tonic-gate 	kcf_req_params_t params;
341*0Sstevel@tonic-gate 
342*0Sstevel@tonic-gate 	if ((ctx == NULL) ||
343*0Sstevel@tonic-gate 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
344*0Sstevel@tonic-gate 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
345*0Sstevel@tonic-gate 		return (CRYPTO_INVALID_CONTEXT);
346*0Sstevel@tonic-gate 	}
347*0Sstevel@tonic-gate 
348*0Sstevel@tonic-gate 	KCF_PROV_REFHOLD(pd);
349*0Sstevel@tonic-gate 
350*0Sstevel@tonic-gate 	/* The fast path for SW providers. */
351*0Sstevel@tonic-gate 	if (CHECK_FASTPATH(cr, pd)) {
352*0Sstevel@tonic-gate 		error = KCF_PROV_DIGEST_FINAL(pd, ctx, digest, NULL);
353*0Sstevel@tonic-gate 		KCF_PROV_INCRSTATS(pd, error);
354*0Sstevel@tonic-gate 	} else {
355*0Sstevel@tonic-gate 		KCF_WRAP_DIGEST_OPS_PARAMS(&params, KCF_OP_FINAL, pd->pd_sid,
356*0Sstevel@tonic-gate 		    NULL, NULL, NULL, digest);
357*0Sstevel@tonic-gate 		error = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
358*0Sstevel@tonic-gate 	}
359*0Sstevel@tonic-gate 
360*0Sstevel@tonic-gate 	KCF_PROV_REFRELE(pd);
361*0Sstevel@tonic-gate 	/* Release the hold done in kcf_new_ctx() during init step. */
362*0Sstevel@tonic-gate 	KCF_CONTEXT_COND_RELEASE(error, kcf_ctx);
363*0Sstevel@tonic-gate 	return (error);
364*0Sstevel@tonic-gate }
365*0Sstevel@tonic-gate 
366*0Sstevel@tonic-gate /*
367*0Sstevel@tonic-gate  * Performs a digest update on the specified key. Note that there is
368*0Sstevel@tonic-gate  * no k-API crypto_digest_key() equivalent of this function.
369*0Sstevel@tonic-gate  */
370*0Sstevel@tonic-gate int
371*0Sstevel@tonic-gate crypto_digest_key_prov(crypto_context_t context, crypto_key_t *key,
372*0Sstevel@tonic-gate     crypto_call_req_t *cr)
373*0Sstevel@tonic-gate {
374*0Sstevel@tonic-gate 	crypto_ctx_t *ctx = (crypto_ctx_t *)context;
375*0Sstevel@tonic-gate 	kcf_context_t *kcf_ctx;
376*0Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
377*0Sstevel@tonic-gate 	int error;
378*0Sstevel@tonic-gate 	kcf_req_params_t params;
379*0Sstevel@tonic-gate 
380*0Sstevel@tonic-gate 	if ((ctx == NULL) ||
381*0Sstevel@tonic-gate 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
382*0Sstevel@tonic-gate 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
383*0Sstevel@tonic-gate 		return (CRYPTO_INVALID_CONTEXT);
384*0Sstevel@tonic-gate 	}
385*0Sstevel@tonic-gate 
386*0Sstevel@tonic-gate 	KCF_PROV_REFHOLD(pd);
387*0Sstevel@tonic-gate 
388*0Sstevel@tonic-gate 	/* The fast path for SW providers. */
389*0Sstevel@tonic-gate 	if (CHECK_FASTPATH(cr, pd)) {
390*0Sstevel@tonic-gate 		error = KCF_PROV_DIGEST_KEY(pd, ctx, key, NULL);
391*0Sstevel@tonic-gate 		KCF_PROV_INCRSTATS(pd, error);
392*0Sstevel@tonic-gate 	} else {
393*0Sstevel@tonic-gate 		KCF_WRAP_DIGEST_OPS_PARAMS(&params, KCF_OP_DIGEST_KEY,
394*0Sstevel@tonic-gate 		    pd->pd_sid, NULL, key, NULL, NULL);
395*0Sstevel@tonic-gate 		error = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
396*0Sstevel@tonic-gate 	}
397*0Sstevel@tonic-gate 
398*0Sstevel@tonic-gate 	KCF_PROV_REFRELE(pd);
399*0Sstevel@tonic-gate 	return (error);
400*0Sstevel@tonic-gate }
401*0Sstevel@tonic-gate 
402*0Sstevel@tonic-gate /*
403*0Sstevel@tonic-gate  * See comments for crypto_digest_update() and crypto_digest_final().
404*0Sstevel@tonic-gate  */
405*0Sstevel@tonic-gate int
406*0Sstevel@tonic-gate crypto_digest_single(crypto_context_t context, crypto_data_t *data,
407*0Sstevel@tonic-gate     crypto_data_t *digest, crypto_call_req_t *cr)
408*0Sstevel@tonic-gate {
409*0Sstevel@tonic-gate 	crypto_ctx_t *ctx = (crypto_ctx_t *)context;
410*0Sstevel@tonic-gate 	kcf_context_t *kcf_ctx;
411*0Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
412*0Sstevel@tonic-gate 	int error;
413*0Sstevel@tonic-gate 	kcf_req_params_t params;
414*0Sstevel@tonic-gate 
415*0Sstevel@tonic-gate 	if ((ctx == NULL) ||
416*0Sstevel@tonic-gate 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
417*0Sstevel@tonic-gate 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
418*0Sstevel@tonic-gate 		return (CRYPTO_INVALID_CONTEXT);
419*0Sstevel@tonic-gate 	}
420*0Sstevel@tonic-gate 
421*0Sstevel@tonic-gate 	KCF_PROV_REFHOLD(pd);
422*0Sstevel@tonic-gate 
423*0Sstevel@tonic-gate 	/* The fast path for SW providers. */
424*0Sstevel@tonic-gate 	if (CHECK_FASTPATH(cr, pd)) {
425*0Sstevel@tonic-gate 		error = KCF_PROV_DIGEST(pd, ctx, data, digest, NULL);
426*0Sstevel@tonic-gate 		KCF_PROV_INCRSTATS(pd, error);
427*0Sstevel@tonic-gate 	} else {
428*0Sstevel@tonic-gate 		KCF_WRAP_DIGEST_OPS_PARAMS(&params, KCF_OP_SINGLE, pd->pd_sid,
429*0Sstevel@tonic-gate 		    NULL, NULL, data, digest);
430*0Sstevel@tonic-gate 		error = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
431*0Sstevel@tonic-gate 	}
432*0Sstevel@tonic-gate 
433*0Sstevel@tonic-gate 	KCF_PROV_REFRELE(pd);
434*0Sstevel@tonic-gate 	/* Release the hold done in kcf_new_ctx() during init step. */
435*0Sstevel@tonic-gate 	KCF_CONTEXT_COND_RELEASE(error, kcf_ctx);
436*0Sstevel@tonic-gate 	return (error);
437*0Sstevel@tonic-gate }
438