xref: /freebsd/sys/contrib/openzfs/module/icp/api/kcf_mac.c (revision 6be3386466ab79a84b48429ae66244f21526d3df)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/zfs_context.h>
27 #include <sys/crypto/common.h>
28 #include <sys/crypto/impl.h>
29 #include <sys/crypto/api.h>
30 #include <sys/crypto/spi.h>
31 #include <sys/crypto/sched_impl.h>
32 
33 /*
34  * Message authentication codes routines.
35  */
36 
37 /*
38  * The following are the possible returned values common to all the routines
39  * below. The applicability of some of these return values depends on the
40  * presence of the arguments.
41  *
42  *	CRYPTO_SUCCESS:	The operation completed successfully.
43  *	CRYPTO_QUEUED:	A request was submitted successfully. The callback
44  *			routine will be called when the operation is done.
45  *	CRYPTO_INVALID_MECH_NUMBER, CRYPTO_INVALID_MECH_PARAM, or
46  *	CRYPTO_INVALID_MECH for problems with the 'mech'.
47  *	CRYPTO_INVALID_DATA for bogus 'data'
48  *	CRYPTO_HOST_MEMORY for failure to allocate memory to handle this work.
49  *	CRYPTO_INVALID_CONTEXT: Not a valid context.
50  *	CRYPTO_BUSY:	Cannot process the request now. Schedule a
51  *			crypto_bufcall(), or try later.
52  *	CRYPTO_NOT_SUPPORTED and CRYPTO_MECH_NOT_SUPPORTED: No provider is
53  *			capable of a function or a mechanism.
54  *	CRYPTO_INVALID_KEY: bogus 'key' argument.
55  *	CRYPTO_INVALID_MAC: bogus 'mac' argument.
56  */
57 
58 /*
59  * crypto_mac_prov()
60  *
61  * Arguments:
62  *	mech:	crypto_mechanism_t pointer.
63  *		mech_type is a valid value previously returned by
64  *		crypto_mech2id();
65  *		When the mech's parameter is not NULL, its definition depends
66  *		on the standard definition of the mechanism.
67  *	key:	pointer to a crypto_key_t structure.
68  *	data:	The message to compute the MAC for.
69  *	mac: Storage for the MAC. The length needed depends on the mechanism.
70  *	tmpl:	a crypto_ctx_template_t, opaque template of a context of a
71  *		MAC with the 'mech' using 'key'. 'tmpl' is created by
72  *		a previous call to crypto_create_ctx_template().
73  *	cr:	crypto_call_req_t calling conditions and call back info.
74  *
75  * Description:
76  *	Asynchronously submits a request for, or synchronously performs a
77  *	single-part message authentication of 'data' with the mechanism
78  *	'mech', using *	the key 'key', on the specified provider with
79  *	the specified session id.
80  *	When complete and successful, 'mac' will contain the message
81  *	authentication code.
82  *
83  * Context:
84  *	Process or interrupt, according to the semantics dictated by the 'crq'.
85  *
86  * Returns:
87  *	See comment in the beginning of the file.
88  */
89 int
90 crypto_mac_prov(crypto_provider_t provider, crypto_session_id_t sid,
91     crypto_mechanism_t *mech, crypto_data_t *data, crypto_key_t *key,
92     crypto_ctx_template_t tmpl, crypto_data_t *mac, crypto_call_req_t *crq)
93 {
94 	kcf_req_params_t params;
95 	kcf_provider_desc_t *pd = provider;
96 	kcf_provider_desc_t *real_provider = pd;
97 	int rv;
98 
99 	ASSERT(KCF_PROV_REFHELD(pd));
100 
101 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
102 		rv = kcf_get_hardware_provider(mech->cm_type,
103 		    CRYPTO_MECH_INVALID, CHECK_RESTRICT(crq), pd,
104 		    &real_provider, CRYPTO_FG_MAC_ATOMIC);
105 
106 		if (rv != CRYPTO_SUCCESS)
107 			return (rv);
108 	}
109 
110 	KCF_WRAP_MAC_OPS_PARAMS(&params, KCF_OP_ATOMIC, sid, mech, key,
111 	    data, mac, tmpl);
112 	rv = kcf_submit_request(real_provider, NULL, crq, &params, B_FALSE);
113 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
114 		KCF_PROV_REFRELE(real_provider);
115 
116 	return (rv);
117 }
118 
119 /*
120  * Same as crypto_mac_prov(), but relies on the KCF scheduler to choose
121  * a provider. See crypto_mac() comments for more information.
122  */
123 int
124 crypto_mac(crypto_mechanism_t *mech, crypto_data_t *data,
125     crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_data_t *mac,
126     crypto_call_req_t *crq)
127 {
128 	int error;
129 	kcf_mech_entry_t *me;
130 	kcf_req_params_t params;
131 	kcf_provider_desc_t *pd;
132 	kcf_ctx_template_t *ctx_tmpl;
133 	crypto_spi_ctx_template_t spi_ctx_tmpl = NULL;
134 	kcf_prov_tried_t *list = NULL;
135 
136 retry:
137 	/* The pd is returned held */
138 	if ((pd = kcf_get_mech_provider(mech->cm_type, &me, &error,
139 	    list, CRYPTO_FG_MAC_ATOMIC, CHECK_RESTRICT(crq),
140 	    data->cd_length)) == NULL) {
141 		if (list != NULL)
142 			kcf_free_triedlist(list);
143 		return (error);
144 	}
145 
146 	/*
147 	 * For SW providers, check the validity of the context template
148 	 * It is very rare that the generation number mis-matches, so
149 	 * is acceptable to fail here, and let the consumer recover by
150 	 * freeing this tmpl and create a new one for the key and new SW
151 	 * provider
152 	 */
153 	if ((pd->pd_prov_type == CRYPTO_SW_PROVIDER) &&
154 	    ((ctx_tmpl = (kcf_ctx_template_t *)tmpl) != NULL)) {
155 		if (ctx_tmpl->ct_generation != me->me_gen_swprov) {
156 			if (list != NULL)
157 				kcf_free_triedlist(list);
158 			KCF_PROV_REFRELE(pd);
159 			return (CRYPTO_OLD_CTX_TEMPLATE);
160 		} else {
161 			spi_ctx_tmpl = ctx_tmpl->ct_prov_tmpl;
162 		}
163 	}
164 
165 	/* The fast path for SW providers. */
166 	if (CHECK_FASTPATH(crq, pd)) {
167 		crypto_mechanism_t lmech;
168 
169 		lmech = *mech;
170 		KCF_SET_PROVIDER_MECHNUM(mech->cm_type, pd, &lmech);
171 
172 		error = KCF_PROV_MAC_ATOMIC(pd, pd->pd_sid, &lmech, key, data,
173 		    mac, spi_ctx_tmpl, KCF_SWFP_RHNDL(crq));
174 		KCF_PROV_INCRSTATS(pd, error);
175 	} else {
176 		if (pd->pd_prov_type == CRYPTO_HW_PROVIDER &&
177 		    (pd->pd_flags & CRYPTO_HASH_NO_UPDATE) &&
178 		    (data->cd_length > pd->pd_hash_limit)) {
179 			/*
180 			 * XXX - We need a check to see if this is indeed
181 			 * a HMAC. So far, all kernel clients use
182 			 * this interface only for HMAC. So, this is fine
183 			 * for now.
184 			 */
185 			error = CRYPTO_BUFFER_TOO_BIG;
186 		} else {
187 			KCF_WRAP_MAC_OPS_PARAMS(&params, KCF_OP_ATOMIC,
188 			    pd->pd_sid, mech, key, data, mac, spi_ctx_tmpl);
189 
190 			error = kcf_submit_request(pd, NULL, crq, &params,
191 			    KCF_ISDUALREQ(crq));
192 		}
193 	}
194 
195 	if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED &&
196 	    IS_RECOVERABLE(error)) {
197 		/* Add pd to the linked list of providers tried. */
198 		if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL)
199 			goto retry;
200 	}
201 
202 	if (list != NULL)
203 		kcf_free_triedlist(list);
204 
205 	KCF_PROV_REFRELE(pd);
206 	return (error);
207 }
208 
209 /*
210  * Single part operation to compute the MAC corresponding to the specified
211  * 'data' and to verify that it matches the MAC specified by 'mac'.
212  * The other arguments are the same as the function crypto_mac_prov().
213  */
214 int
215 crypto_mac_verify_prov(crypto_provider_t provider, crypto_session_id_t sid,
216     crypto_mechanism_t *mech, crypto_data_t *data, crypto_key_t *key,
217     crypto_ctx_template_t tmpl, crypto_data_t *mac, crypto_call_req_t *crq)
218 {
219 	kcf_req_params_t params;
220 	kcf_provider_desc_t *pd = provider;
221 	kcf_provider_desc_t *real_provider = pd;
222 	int rv;
223 
224 	ASSERT(KCF_PROV_REFHELD(pd));
225 
226 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
227 		rv = kcf_get_hardware_provider(mech->cm_type,
228 		    CRYPTO_MECH_INVALID, CHECK_RESTRICT(crq), pd,
229 		    &real_provider, CRYPTO_FG_MAC_ATOMIC);
230 
231 		if (rv != CRYPTO_SUCCESS)
232 			return (rv);
233 	}
234 
235 	KCF_WRAP_MAC_OPS_PARAMS(&params, KCF_OP_MAC_VERIFY_ATOMIC, sid, mech,
236 	    key, data, mac, tmpl);
237 	rv = kcf_submit_request(real_provider, NULL, crq, &params, B_FALSE);
238 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
239 		KCF_PROV_REFRELE(real_provider);
240 
241 	return (rv);
242 }
243 
244 /*
245  * Same as crypto_mac_verify_prov(), but relies on the KCF scheduler to choose
246  * a provider. See crypto_mac_verify_prov() comments for more information.
247  */
248 int
249 crypto_mac_verify(crypto_mechanism_t *mech, crypto_data_t *data,
250     crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_data_t *mac,
251     crypto_call_req_t *crq)
252 {
253 	int error;
254 	kcf_mech_entry_t *me;
255 	kcf_req_params_t params;
256 	kcf_provider_desc_t *pd;
257 	kcf_ctx_template_t *ctx_tmpl;
258 	crypto_spi_ctx_template_t spi_ctx_tmpl = NULL;
259 	kcf_prov_tried_t *list = NULL;
260 
261 retry:
262 	/* The pd is returned held */
263 	if ((pd = kcf_get_mech_provider(mech->cm_type, &me, &error,
264 	    list, CRYPTO_FG_MAC_ATOMIC, CHECK_RESTRICT(crq),
265 	    data->cd_length)) == NULL) {
266 		if (list != NULL)
267 			kcf_free_triedlist(list);
268 		return (error);
269 	}
270 
271 	/*
272 	 * For SW providers, check the validity of the context template
273 	 * It is very rare that the generation number mis-matches, so
274 	 * is acceptable to fail here, and let the consumer recover by
275 	 * freeing this tmpl and create a new one for the key and new SW
276 	 * provider
277 	 */
278 	if ((pd->pd_prov_type == CRYPTO_SW_PROVIDER) &&
279 	    ((ctx_tmpl = (kcf_ctx_template_t *)tmpl) != NULL)) {
280 		if (ctx_tmpl->ct_generation != me->me_gen_swprov) {
281 			if (list != NULL)
282 				kcf_free_triedlist(list);
283 			KCF_PROV_REFRELE(pd);
284 			return (CRYPTO_OLD_CTX_TEMPLATE);
285 		} else {
286 			spi_ctx_tmpl = ctx_tmpl->ct_prov_tmpl;
287 		}
288 	}
289 
290 	/* The fast path for SW providers. */
291 	if (CHECK_FASTPATH(crq, pd)) {
292 		crypto_mechanism_t lmech;
293 
294 		lmech = *mech;
295 		KCF_SET_PROVIDER_MECHNUM(mech->cm_type, pd, &lmech);
296 
297 		error = KCF_PROV_MAC_VERIFY_ATOMIC(pd, pd->pd_sid, &lmech, key,
298 		    data, mac, spi_ctx_tmpl, KCF_SWFP_RHNDL(crq));
299 		KCF_PROV_INCRSTATS(pd, error);
300 	} else {
301 		if (pd->pd_prov_type == CRYPTO_HW_PROVIDER &&
302 		    (pd->pd_flags & CRYPTO_HASH_NO_UPDATE) &&
303 		    (data->cd_length > pd->pd_hash_limit)) {
304 			/* see comments in crypto_mac() */
305 			error = CRYPTO_BUFFER_TOO_BIG;
306 		} else {
307 			KCF_WRAP_MAC_OPS_PARAMS(&params,
308 			    KCF_OP_MAC_VERIFY_ATOMIC, pd->pd_sid, mech,
309 			    key, data, mac, spi_ctx_tmpl);
310 
311 			error = kcf_submit_request(pd, NULL, crq, &params,
312 			    KCF_ISDUALREQ(crq));
313 		}
314 	}
315 
316 	if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED &&
317 	    IS_RECOVERABLE(error)) {
318 		/* Add pd to the linked list of providers tried. */
319 		if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL)
320 			goto retry;
321 	}
322 
323 	if (list != NULL)
324 		kcf_free_triedlist(list);
325 
326 	KCF_PROV_REFRELE(pd);
327 	return (error);
328 }
329 
330 /*
331  * crypto_mac_init_prov()
332  *
333  * Arguments:
334  *	pd:	pointer to the descriptor of the provider to use for this
335  *		operation.
336  *	sid:	provider session id.
337  *	mech:	crypto_mechanism_t pointer.
338  *		mech_type is a valid value previously returned by
339  *		crypto_mech2id();
340  *		When the mech's parameter is not NULL, its definition depends
341  *		on the standard definition of the mechanism.
342  *	key:	pointer to a crypto_key_t structure.
343  *	tmpl:	a crypto_ctx_template_t, opaque template of a context of a
344  *		MAC with the 'mech' using 'key'. 'tmpl' is created by
345  *		a previous call to crypto_create_ctx_template().
346  *	ctxp:	Pointer to a crypto_context_t.
347  *	cr:	crypto_call_req_t calling conditions and call back info.
348  *
349  * Description:
350  *	Asynchronously submits a request for, or synchronously performs the
351  *	initialization of a MAC operation on the specified provider with
352  *	the specified session.
353  *	When possible and applicable, will internally use the pre-computed MAC
354  *	context from the context template, tmpl.
355  *	When complete and successful, 'ctxp' will contain a crypto_context_t
356  *	valid for later calls to mac_update() and mac_final().
357  *	The caller should hold a reference on the specified provider
358  *	descriptor before calling this function.
359  *
360  * Context:
361  *	Process or interrupt, according to the semantics dictated by the 'cr'.
362  *
363  * Returns:
364  *	See comment in the beginning of the file.
365  */
366 int
367 crypto_mac_init_prov(crypto_provider_t provider, crypto_session_id_t sid,
368     crypto_mechanism_t *mech, crypto_key_t *key, crypto_spi_ctx_template_t tmpl,
369     crypto_context_t *ctxp, crypto_call_req_t *crq)
370 {
371 	int rv;
372 	crypto_ctx_t *ctx;
373 	kcf_req_params_t params;
374 	kcf_provider_desc_t *pd = provider;
375 	kcf_provider_desc_t *real_provider = pd;
376 
377 	ASSERT(KCF_PROV_REFHELD(pd));
378 
379 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
380 		rv = kcf_get_hardware_provider(mech->cm_type,
381 		    CRYPTO_MECH_INVALID, CHECK_RESTRICT(crq), pd,
382 		    &real_provider, CRYPTO_FG_MAC);
383 
384 		if (rv != CRYPTO_SUCCESS)
385 			return (rv);
386 	}
387 
388 	/* Allocate and initialize the canonical context */
389 	if ((ctx = kcf_new_ctx(crq, real_provider, sid)) == NULL) {
390 		if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
391 			KCF_PROV_REFRELE(real_provider);
392 		return (CRYPTO_HOST_MEMORY);
393 	}
394 
395 	/* The fast path for SW providers. */
396 	if (CHECK_FASTPATH(crq, pd)) {
397 		crypto_mechanism_t lmech;
398 
399 		lmech = *mech;
400 		KCF_SET_PROVIDER_MECHNUM(mech->cm_type, real_provider, &lmech);
401 		rv = KCF_PROV_MAC_INIT(real_provider, ctx, &lmech, key, tmpl,
402 		    KCF_SWFP_RHNDL(crq));
403 		KCF_PROV_INCRSTATS(pd, rv);
404 	} else {
405 		KCF_WRAP_MAC_OPS_PARAMS(&params, KCF_OP_INIT, sid, mech, key,
406 		    NULL, NULL, tmpl);
407 		rv = kcf_submit_request(real_provider, ctx, crq, &params,
408 		    B_FALSE);
409 	}
410 
411 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
412 		KCF_PROV_REFRELE(real_provider);
413 
414 	if ((rv == CRYPTO_SUCCESS) || (rv == CRYPTO_QUEUED))
415 		*ctxp = (crypto_context_t)ctx;
416 	else {
417 		/* Release the hold done in kcf_new_ctx(). */
418 		KCF_CONTEXT_REFRELE((kcf_context_t *)ctx->cc_framework_private);
419 	}
420 
421 	return (rv);
422 }
423 
424 /*
425  * Same as crypto_mac_init_prov(), but relies on the KCF scheduler to
426  * choose a provider. See crypto_mac_init_prov() comments for more
427  * information.
428  */
429 int
430 crypto_mac_init(crypto_mechanism_t *mech, crypto_key_t *key,
431     crypto_ctx_template_t tmpl, crypto_context_t *ctxp,
432     crypto_call_req_t  *crq)
433 {
434 	int error;
435 	kcf_mech_entry_t *me;
436 	kcf_provider_desc_t *pd;
437 	kcf_ctx_template_t *ctx_tmpl;
438 	crypto_spi_ctx_template_t spi_ctx_tmpl = NULL;
439 	kcf_prov_tried_t *list = NULL;
440 
441 retry:
442 	/* The pd is returned held */
443 	if ((pd = kcf_get_mech_provider(mech->cm_type, &me, &error,
444 	    list, CRYPTO_FG_MAC, CHECK_RESTRICT(crq), 0)) == NULL) {
445 		if (list != NULL)
446 			kcf_free_triedlist(list);
447 		return (error);
448 	}
449 
450 	/*
451 	 * For SW providers, check the validity of the context template
452 	 * It is very rare that the generation number mis-matches, so
453 	 * is acceptable to fail here, and let the consumer recover by
454 	 * freeing this tmpl and create a new one for the key and new SW
455 	 * provider
456 	 */
457 
458 	if ((pd->pd_prov_type == CRYPTO_SW_PROVIDER) &&
459 	    ((ctx_tmpl = (kcf_ctx_template_t *)tmpl) != NULL)) {
460 		if (ctx_tmpl->ct_generation != me->me_gen_swprov) {
461 			if (list != NULL)
462 				kcf_free_triedlist(list);
463 			KCF_PROV_REFRELE(pd);
464 			return (CRYPTO_OLD_CTX_TEMPLATE);
465 		} else {
466 			spi_ctx_tmpl = ctx_tmpl->ct_prov_tmpl;
467 		}
468 	}
469 
470 	if (pd->pd_prov_type == CRYPTO_HW_PROVIDER &&
471 	    (pd->pd_flags & CRYPTO_HASH_NO_UPDATE)) {
472 		/*
473 		 * The hardware provider has limited HMAC support.
474 		 * So, we fallback early here to using a software provider.
475 		 *
476 		 * XXX - need to enhance to do the fallback later in
477 		 * crypto_mac_update() if the size of accumulated input data
478 		 * exceeds the maximum size digestable by hardware provider.
479 		 */
480 		error = CRYPTO_BUFFER_TOO_BIG;
481 	} else {
482 		error = crypto_mac_init_prov(pd, pd->pd_sid, mech, key,
483 		    spi_ctx_tmpl, ctxp, crq);
484 	}
485 	if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED &&
486 	    IS_RECOVERABLE(error)) {
487 		/* Add pd to the linked list of providers tried. */
488 		if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL)
489 			goto retry;
490 	}
491 
492 	if (list != NULL)
493 		kcf_free_triedlist(list);
494 
495 	KCF_PROV_REFRELE(pd);
496 	return (error);
497 }
498 
499 /*
500  * crypto_mac_update()
501  *
502  * Arguments:
503  *	context: A crypto_context_t initialized by mac_init().
504  *	data: The message part to be MAC'ed
505  *	cr:	crypto_call_req_t calling conditions and call back info.
506  *
507  * Description:
508  *	Asynchronously submits a request for, or synchronously performs a
509  *	part of a MAC operation.
510  *
511  * Context:
512  *	Process or interrupt, according to the semantics dictated by the 'cr'.
513  *
514  * Returns:
515  *	See comment in the beginning of the file.
516  */
517 int
518 crypto_mac_update(crypto_context_t context, crypto_data_t *data,
519     crypto_call_req_t *cr)
520 {
521 	crypto_ctx_t *ctx = (crypto_ctx_t *)context;
522 	kcf_context_t *kcf_ctx;
523 	kcf_provider_desc_t *pd;
524 	kcf_req_params_t params;
525 	int rv;
526 
527 	if ((ctx == NULL) ||
528 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
529 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
530 		return (CRYPTO_INVALID_CONTEXT);
531 	}
532 
533 	ASSERT(pd->pd_prov_type != CRYPTO_LOGICAL_PROVIDER);
534 
535 	/* The fast path for SW providers. */
536 	if (CHECK_FASTPATH(cr, pd)) {
537 		rv = KCF_PROV_MAC_UPDATE(pd, ctx, data, NULL);
538 		KCF_PROV_INCRSTATS(pd, rv);
539 	} else {
540 		KCF_WRAP_MAC_OPS_PARAMS(&params, KCF_OP_UPDATE,
541 		    ctx->cc_session, NULL, NULL, data, NULL, NULL);
542 		rv = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
543 	}
544 
545 	return (rv);
546 }
547 
548 /*
549  * crypto_mac_final()
550  *
551  * Arguments:
552  *	context: A crypto_context_t initialized by mac_init().
553  *	mac: Storage for the message authentication code.
554  *	cr:	crypto_call_req_t calling conditions and call back info.
555  *
556  * Description:
557  *	Asynchronously submits a request for, or synchronously performs a
558  *	part of a message authentication operation.
559  *
560  * Context:
561  *	Process or interrupt, according to the semantics dictated by the 'cr'.
562  *
563  * Returns:
564  *	See comment in the beginning of the file.
565  */
566 int
567 crypto_mac_final(crypto_context_t context, crypto_data_t *mac,
568     crypto_call_req_t *cr)
569 {
570 	crypto_ctx_t *ctx = (crypto_ctx_t *)context;
571 	kcf_context_t *kcf_ctx;
572 	kcf_provider_desc_t *pd;
573 	kcf_req_params_t params;
574 	int rv;
575 
576 	if ((ctx == NULL) ||
577 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
578 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
579 		return (CRYPTO_INVALID_CONTEXT);
580 	}
581 
582 	ASSERT(pd->pd_prov_type != CRYPTO_LOGICAL_PROVIDER);
583 
584 	/* The fast path for SW providers. */
585 	if (CHECK_FASTPATH(cr, pd)) {
586 		rv = KCF_PROV_MAC_FINAL(pd, ctx, mac, NULL);
587 		KCF_PROV_INCRSTATS(pd, rv);
588 	} else {
589 		KCF_WRAP_MAC_OPS_PARAMS(&params, KCF_OP_FINAL,
590 		    ctx->cc_session, NULL, NULL, NULL, mac, NULL);
591 		rv = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
592 	}
593 
594 	/* Release the hold done in kcf_new_ctx() during init step. */
595 	KCF_CONTEXT_COND_RELEASE(rv, kcf_ctx);
596 	return (rv);
597 }
598 
599 /*
600  * See comments for crypto_mac_update() and crypto_mac_final().
601  */
602 int
603 crypto_mac_single(crypto_context_t context, crypto_data_t *data,
604     crypto_data_t *mac, crypto_call_req_t *cr)
605 {
606 	crypto_ctx_t *ctx = (crypto_ctx_t *)context;
607 	kcf_context_t *kcf_ctx;
608 	kcf_provider_desc_t *pd;
609 	int error;
610 	kcf_req_params_t params;
611 
612 
613 	if ((ctx == NULL) ||
614 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
615 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
616 		return (CRYPTO_INVALID_CONTEXT);
617 	}
618 
619 
620 	/* The fast path for SW providers. */
621 	if (CHECK_FASTPATH(cr, pd)) {
622 		error = KCF_PROV_MAC(pd, ctx, data, mac, NULL);
623 		KCF_PROV_INCRSTATS(pd, error);
624 	} else {
625 		KCF_WRAP_MAC_OPS_PARAMS(&params, KCF_OP_SINGLE, pd->pd_sid,
626 		    NULL, NULL, data, mac, NULL);
627 		error = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
628 	}
629 
630 	/* Release the hold done in kcf_new_ctx() during init step. */
631 	KCF_CONTEXT_COND_RELEASE(error, kcf_ctx);
632 	return (error);
633 }
634 
635 #if defined(_KERNEL)
636 EXPORT_SYMBOL(crypto_mac_prov);
637 EXPORT_SYMBOL(crypto_mac);
638 EXPORT_SYMBOL(crypto_mac_verify_prov);
639 EXPORT_SYMBOL(crypto_mac_verify);
640 EXPORT_SYMBOL(crypto_mac_init_prov);
641 EXPORT_SYMBOL(crypto_mac_init);
642 EXPORT_SYMBOL(crypto_mac_update);
643 EXPORT_SYMBOL(crypto_mac_final);
644 EXPORT_SYMBOL(crypto_mac_single);
645 #endif
646