17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
572eff6e2Smcpowers  * Common Development and Distribution License (the "License").
672eff6e2Smcpowers  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
229b009fc1SValerie Bubb Fenwick  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #include <sys/errno.h>
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
28894b2776Smcpowers #include <sys/sysmacros.h>
297c478bd9Sstevel@tonic-gate #include <sys/crypto/common.h>
307c478bd9Sstevel@tonic-gate #include <sys/crypto/impl.h>
317c478bd9Sstevel@tonic-gate #include <sys/crypto/api.h>
327c478bd9Sstevel@tonic-gate #include <sys/crypto/spi.h>
337c478bd9Sstevel@tonic-gate #include <sys/crypto/sched_impl.h>
347c478bd9Sstevel@tonic-gate 
35894b2776Smcpowers #define	CRYPTO_OPS_OFFSET(f)		offsetof(crypto_ops_t, co_##f)
36894b2776Smcpowers #define	CRYPTO_CIPHER_MAC_OFFSET(f) offsetof(crypto_dual_cipher_mac_ops_t, f)
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate static int crypto_mac_decrypt_common(crypto_mechanism_t *,
397c478bd9Sstevel@tonic-gate     crypto_mechanism_t *, crypto_dual_data_t *, crypto_key_t *, crypto_key_t *,
407c478bd9Sstevel@tonic-gate     crypto_ctx_template_t, crypto_ctx_template_t, crypto_data_t *,
417c478bd9Sstevel@tonic-gate     crypto_data_t *, crypto_call_req_t *, boolean_t);
427c478bd9Sstevel@tonic-gate 
43894b2776Smcpowers static int crypto_mac_decrypt_common_prov(crypto_provider_t provider,
44894b2776Smcpowers     crypto_session_id_t sid, crypto_mechanism_t *, crypto_mechanism_t *,
45894b2776Smcpowers     crypto_dual_data_t *, crypto_key_t *, crypto_key_t *,
46894b2776Smcpowers     crypto_ctx_template_t, crypto_ctx_template_t, crypto_data_t *,
47894b2776Smcpowers     crypto_data_t *, crypto_call_req_t *, boolean_t);
48894b2776Smcpowers 
49894b2776Smcpowers int
crypto_encrypt_mac_prov(crypto_provider_t provider,crypto_session_id_t sid,crypto_mechanism_t * encr_mech,crypto_mechanism_t * mac_mech,crypto_data_t * pt,crypto_key_t * encr_key,crypto_key_t * mac_key,crypto_ctx_template_t encr_tmpl,crypto_ctx_template_t mac_tmpl,crypto_dual_data_t * ct,crypto_data_t * mac,crypto_call_req_t * crq)50894b2776Smcpowers crypto_encrypt_mac_prov(crypto_provider_t provider, crypto_session_id_t sid,
51894b2776Smcpowers     crypto_mechanism_t *encr_mech, crypto_mechanism_t *mac_mech,
52894b2776Smcpowers     crypto_data_t *pt, crypto_key_t *encr_key, crypto_key_t *mac_key,
53894b2776Smcpowers     crypto_ctx_template_t encr_tmpl, crypto_ctx_template_t mac_tmpl,
54894b2776Smcpowers     crypto_dual_data_t *ct, crypto_data_t *mac, crypto_call_req_t *crq)
55894b2776Smcpowers {
56894b2776Smcpowers 	/*
57894b2776Smcpowers 	 * First try to find a provider for the encryption mechanism, that
58894b2776Smcpowers 	 * is also capable of the MAC mechanism.
59894b2776Smcpowers 	 */
60894b2776Smcpowers 	int rv;
61894b2776Smcpowers 	kcf_mech_entry_t *me;
62894b2776Smcpowers 	kcf_provider_desc_t *pd = provider;
63894b2776Smcpowers 	kcf_provider_desc_t *real_provider = pd;
64894b2776Smcpowers 	kcf_ctx_template_t *ctx_encr_tmpl, *ctx_mac_tmpl;
65894b2776Smcpowers 	kcf_req_params_t params;
66894b2776Smcpowers 	kcf_encrypt_mac_ops_params_t *cmops;
67894b2776Smcpowers 	crypto_spi_ctx_template_t spi_encr_tmpl = NULL, spi_mac_tmpl = NULL;
68894b2776Smcpowers 
69894b2776Smcpowers 	ASSERT(KCF_PROV_REFHELD(pd));
70894b2776Smcpowers 
71894b2776Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
72436935a1SVladimir Kotal 		rv = kcf_get_hardware_provider(encr_mech->cm_type, encr_key,
739b009fc1SValerie Bubb Fenwick 		    mac_mech->cm_type, mac_key, pd, &real_provider,
749b009fc1SValerie Bubb Fenwick 		    CRYPTO_FG_ENCRYPT_MAC_ATOMIC);
75894b2776Smcpowers 
76894b2776Smcpowers 		if (rv != CRYPTO_SUCCESS)
77894b2776Smcpowers 			return (rv);
78894b2776Smcpowers 	}
79894b2776Smcpowers 
80894b2776Smcpowers 	/*
81894b2776Smcpowers 	 * For SW providers, check the validity of the context template
82894b2776Smcpowers 	 * It is very rare that the generation number mis-matches, so
83894b2776Smcpowers 	 * is acceptable to fail here, and let the consumer recover by
84894b2776Smcpowers 	 * freeing this tmpl and create a new one for the key and new SW
85894b2776Smcpowers 	 * provider
86894b2776Smcpowers 	 * Warning! will need to change when multiple software providers
87894b2776Smcpowers 	 * per mechanism are supported.
88894b2776Smcpowers 	 */
89894b2776Smcpowers 
90894b2776Smcpowers 	if (real_provider->pd_prov_type == CRYPTO_SW_PROVIDER) {
91894b2776Smcpowers 		if (encr_tmpl != NULL) {
92894b2776Smcpowers 			if (kcf_get_mech_entry(encr_mech->cm_type, &me) !=
93894b2776Smcpowers 			    KCF_SUCCESS) {
94894b2776Smcpowers 				rv = CRYPTO_MECHANISM_INVALID;
95894b2776Smcpowers 				goto out;
96894b2776Smcpowers 			}
97894b2776Smcpowers 			ctx_encr_tmpl = (kcf_ctx_template_t *)encr_tmpl;
98894b2776Smcpowers 			if (ctx_encr_tmpl->ct_generation != me->me_gen_swprov) {
99894b2776Smcpowers 				rv = CRYPTO_OLD_CTX_TEMPLATE;
100894b2776Smcpowers 				goto out;
101894b2776Smcpowers 			}
102894b2776Smcpowers 			spi_encr_tmpl = ctx_encr_tmpl->ct_prov_tmpl;
103894b2776Smcpowers 		}
104894b2776Smcpowers 
105894b2776Smcpowers 		if (mac_tmpl != NULL) {
106894b2776Smcpowers 			if (kcf_get_mech_entry(mac_mech->cm_type, &me) !=
107894b2776Smcpowers 			    KCF_SUCCESS) {
108894b2776Smcpowers 				rv = CRYPTO_MECHANISM_INVALID;
109894b2776Smcpowers 				goto out;
110894b2776Smcpowers 			}
111894b2776Smcpowers 			ctx_mac_tmpl = (kcf_ctx_template_t *)mac_tmpl;
112894b2776Smcpowers 			if (ctx_mac_tmpl->ct_generation != me->me_gen_swprov) {
113894b2776Smcpowers 				rv = CRYPTO_OLD_CTX_TEMPLATE;
114894b2776Smcpowers 				goto out;
115894b2776Smcpowers 			}
116894b2776Smcpowers 			spi_mac_tmpl = ctx_mac_tmpl->ct_prov_tmpl;
117894b2776Smcpowers 		}
118894b2776Smcpowers 	}
119894b2776Smcpowers 
120894b2776Smcpowers 	/* The fast path for SW providers. */
121894b2776Smcpowers 	if (CHECK_FASTPATH(crq, real_provider)) {
122894b2776Smcpowers 		crypto_mechanism_t lencr_mech;
123894b2776Smcpowers 		crypto_mechanism_t lmac_mech;
124894b2776Smcpowers 
125894b2776Smcpowers 		/* careful! structs assignments */
126894b2776Smcpowers 		lencr_mech = *encr_mech;
127894b2776Smcpowers 		KCF_SET_PROVIDER_MECHNUM(encr_mech->cm_type, real_provider,
128894b2776Smcpowers 		    &lencr_mech);
129894b2776Smcpowers 
130894b2776Smcpowers 		lmac_mech = *mac_mech;
131894b2776Smcpowers 		KCF_SET_PROVIDER_MECHNUM(mac_mech->cm_type, real_provider,
132894b2776Smcpowers 		    &lmac_mech);
133894b2776Smcpowers 
134894b2776Smcpowers 		rv = KCF_PROV_ENCRYPT_MAC_ATOMIC(real_provider, sid,
135894b2776Smcpowers 		    &lencr_mech, encr_key, &lmac_mech, mac_key, pt, ct,
136894b2776Smcpowers 		    mac, spi_encr_tmpl, spi_mac_tmpl, KCF_SWFP_RHNDL(crq));
137894b2776Smcpowers 
138894b2776Smcpowers 		KCF_PROV_INCRSTATS(pd, rv);
139894b2776Smcpowers 	} else {
140894b2776Smcpowers 		KCF_WRAP_ENCRYPT_MAC_OPS_PARAMS(&params, KCF_OP_ATOMIC,
141894b2776Smcpowers 		    sid, encr_key, mac_key, pt, ct, mac, spi_encr_tmpl,
142894b2776Smcpowers 		    spi_mac_tmpl);
143894b2776Smcpowers 
144894b2776Smcpowers 		cmops = &(params.rp_u.encrypt_mac_params);
145894b2776Smcpowers 
146894b2776Smcpowers 		/* careful! structs assignments */
147894b2776Smcpowers 		cmops->em_encr_mech = *encr_mech;
148894b2776Smcpowers 		KCF_SET_PROVIDER_MECHNUM(encr_mech->cm_type, real_provider,
149894b2776Smcpowers 		    &cmops->em_encr_mech);
150894b2776Smcpowers 		cmops->em_framework_encr_mechtype = encr_mech->cm_type;
151894b2776Smcpowers 
152894b2776Smcpowers 		cmops->em_mac_mech = *mac_mech;
153894b2776Smcpowers 		KCF_SET_PROVIDER_MECHNUM(mac_mech->cm_type, real_provider,
154894b2776Smcpowers 		    &cmops->em_mac_mech);
155894b2776Smcpowers 		cmops->em_framework_mac_mechtype = mac_mech->cm_type;
156894b2776Smcpowers 
157894b2776Smcpowers 		rv = kcf_submit_request(real_provider, NULL, crq, &params,
158894b2776Smcpowers 		    B_FALSE);
159894b2776Smcpowers 	}
160894b2776Smcpowers 
161894b2776Smcpowers out:
162894b2776Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
163894b2776Smcpowers 		KCF_PROV_REFRELE(real_provider);
164894b2776Smcpowers 	return (rv);
165894b2776Smcpowers }
166894b2776Smcpowers 
1677c478bd9Sstevel@tonic-gate /*
1687c478bd9Sstevel@tonic-gate  * Performs a dual encrypt/mac atomic operation. The provider and session
1697c478bd9Sstevel@tonic-gate  * to use are determined by the KCF dispatcher.
1707c478bd9Sstevel@tonic-gate  */
1717c478bd9Sstevel@tonic-gate int
crypto_encrypt_mac(crypto_mechanism_t * encr_mech,crypto_mechanism_t * mac_mech,crypto_data_t * pt,crypto_key_t * encr_key,crypto_key_t * mac_key,crypto_ctx_template_t encr_tmpl,crypto_ctx_template_t mac_tmpl,crypto_dual_data_t * ct,crypto_data_t * mac,crypto_call_req_t * crq)1727c478bd9Sstevel@tonic-gate crypto_encrypt_mac(crypto_mechanism_t *encr_mech,
1737c478bd9Sstevel@tonic-gate     crypto_mechanism_t *mac_mech, crypto_data_t *pt,
1747c478bd9Sstevel@tonic-gate     crypto_key_t *encr_key, crypto_key_t *mac_key,
1757c478bd9Sstevel@tonic-gate     crypto_ctx_template_t encr_tmpl, crypto_ctx_template_t mac_tmpl,
1767c478bd9Sstevel@tonic-gate     crypto_dual_data_t *ct, crypto_data_t *mac, crypto_call_req_t *crq)
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate 	/*
1797c478bd9Sstevel@tonic-gate 	 * First try to find a provider for the encryption mechanism, that
1807c478bd9Sstevel@tonic-gate 	 * is also capable of the MAC mechanism.
1817c478bd9Sstevel@tonic-gate 	 */
1827c478bd9Sstevel@tonic-gate 	int error;
1837c478bd9Sstevel@tonic-gate 	kcf_mech_entry_t *me;
1847c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
1857c478bd9Sstevel@tonic-gate 	kcf_ctx_template_t *ctx_encr_tmpl, *ctx_mac_tmpl;
1867c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
1877c478bd9Sstevel@tonic-gate 	kcf_encrypt_mac_ops_params_t *cmops;
1887c478bd9Sstevel@tonic-gate 	crypto_spi_ctx_template_t spi_encr_tmpl = NULL, spi_mac_tmpl = NULL;
1897c478bd9Sstevel@tonic-gate 	crypto_mech_type_t prov_encr_mechid, prov_mac_mechid;
1907c478bd9Sstevel@tonic-gate 	kcf_prov_tried_t *list = NULL;
1917c478bd9Sstevel@tonic-gate 	boolean_t encr_tmpl_checked = B_FALSE;
1927c478bd9Sstevel@tonic-gate 	boolean_t mac_tmpl_checked = B_FALSE;
1937c478bd9Sstevel@tonic-gate 	kcf_dual_req_t *next_req = NULL;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate retry:
1967c478bd9Sstevel@tonic-gate 	/* pd is returned held on success */
197436935a1SVladimir Kotal 	pd = kcf_get_dual_provider(encr_mech, encr_key, mac_mech, mac_key,
198436935a1SVladimir Kotal 	    &me, &prov_encr_mechid,
1997c478bd9Sstevel@tonic-gate 	    &prov_mac_mechid, &error, list,
2007c478bd9Sstevel@tonic-gate 	    CRYPTO_FG_ENCRYPT_ATOMIC | CRYPTO_FG_ENCRYPT_MAC_ATOMIC,
2017c478bd9Sstevel@tonic-gate 	    CRYPTO_FG_MAC_ATOMIC | CRYPTO_FG_ENCRYPT_MAC_ATOMIC,
2029b009fc1SValerie Bubb Fenwick 	    ct->dd_len1);
2037c478bd9Sstevel@tonic-gate 	if (pd == NULL) {
2047c478bd9Sstevel@tonic-gate 		if (list != NULL)
2057c478bd9Sstevel@tonic-gate 			kcf_free_triedlist(list);
2067c478bd9Sstevel@tonic-gate 		if (next_req != NULL)
2077c478bd9Sstevel@tonic-gate 			kmem_free(next_req, sizeof (kcf_dual_req_t));
2087c478bd9Sstevel@tonic-gate 		return (error);
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	/*
2127c478bd9Sstevel@tonic-gate 	 * For SW providers, check the validity of the context template
2137c478bd9Sstevel@tonic-gate 	 * It is very rare that the generation number mis-matches, so
2147c478bd9Sstevel@tonic-gate 	 * is acceptable to fail here, and let the consumer recover by
2157c478bd9Sstevel@tonic-gate 	 * freeing this tmpl and create a new one for the key and new SW
2167c478bd9Sstevel@tonic-gate 	 * provider
2177c478bd9Sstevel@tonic-gate 	 * Warning! will need to change when multiple software providers
2187c478bd9Sstevel@tonic-gate 	 * per mechanism are supported.
2197c478bd9Sstevel@tonic-gate 	 */
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	if ((!encr_tmpl_checked) && (pd->pd_prov_type == CRYPTO_SW_PROVIDER)) {
2227c478bd9Sstevel@tonic-gate 		if (encr_tmpl != NULL) {
2237c478bd9Sstevel@tonic-gate 			ctx_encr_tmpl = (kcf_ctx_template_t *)encr_tmpl;
2247c478bd9Sstevel@tonic-gate 			if (ctx_encr_tmpl->ct_generation != me->me_gen_swprov) {
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 				if (next_req != NULL)
2277c478bd9Sstevel@tonic-gate 					kmem_free(next_req,
2287c478bd9Sstevel@tonic-gate 					    sizeof (kcf_dual_req_t));
2297c478bd9Sstevel@tonic-gate 				if (list != NULL)
2307c478bd9Sstevel@tonic-gate 					kcf_free_triedlist(list);
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 				KCF_PROV_REFRELE(pd);
2337c478bd9Sstevel@tonic-gate 				/* Which one is the the old one ? */
2347c478bd9Sstevel@tonic-gate 				return (CRYPTO_OLD_CTX_TEMPLATE);
2357c478bd9Sstevel@tonic-gate 			}
2367c478bd9Sstevel@tonic-gate 			spi_encr_tmpl = ctx_encr_tmpl->ct_prov_tmpl;
2377c478bd9Sstevel@tonic-gate 		}
2387c478bd9Sstevel@tonic-gate 		encr_tmpl_checked = B_TRUE;
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	if (prov_mac_mechid == CRYPTO_MECH_INVALID) {
2427c478bd9Sstevel@tonic-gate 		crypto_call_req_t encr_req;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 		/* Need to emulate with 2 internal calls */
2457c478bd9Sstevel@tonic-gate 		/* Allocate and initialize the MAC req for the callback */
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 		if (crq != NULL) {
2487c478bd9Sstevel@tonic-gate 			if (next_req == NULL) {
2497c478bd9Sstevel@tonic-gate 				next_req = kcf_alloc_req(crq);
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 				if (next_req == NULL) {
2527c478bd9Sstevel@tonic-gate 					KCF_PROV_REFRELE(pd);
2537c478bd9Sstevel@tonic-gate 					if (list != NULL)
2547c478bd9Sstevel@tonic-gate 						kcf_free_triedlist(list);
2557c478bd9Sstevel@tonic-gate 					return (CRYPTO_HOST_MEMORY);
2567c478bd9Sstevel@tonic-gate 				}
2577c478bd9Sstevel@tonic-gate 				/*
2587c478bd9Sstevel@tonic-gate 				 * Careful! we're wrapping-in mac_tmpl instead
2597c478bd9Sstevel@tonic-gate 				 * of an spi_mac_tmpl. The callback routine will
2607c478bd9Sstevel@tonic-gate 				 * have to validate mac_tmpl, and use the
2617c478bd9Sstevel@tonic-gate 				 * mac_ctx_tmpl, once it picks a MAC provider.
2627c478bd9Sstevel@tonic-gate 				 */
2637c478bd9Sstevel@tonic-gate 				KCF_WRAP_MAC_OPS_PARAMS(&(next_req->kr_params),
264*0f677ac9SToomas Soome 				    KCF_OP_ATOMIC, 0, mac_mech, mac_key,
2657c478bd9Sstevel@tonic-gate 				    (crypto_data_t *)ct, mac, mac_tmpl);
2667c478bd9Sstevel@tonic-gate 			}
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 			encr_req.cr_flag = crq->cr_flag;
2697c478bd9Sstevel@tonic-gate 			encr_req.cr_callback_func = kcf_next_req;
2707c478bd9Sstevel@tonic-gate 			encr_req.cr_callback_arg = next_req;
2717c478bd9Sstevel@tonic-gate 		}
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 		if (pt == NULL) {
2747c478bd9Sstevel@tonic-gate 			KCF_WRAP_ENCRYPT_OPS_PARAMS(&params, KCF_OP_ATOMIC,
2757c478bd9Sstevel@tonic-gate 			    pd->pd_sid, encr_mech, encr_key,
2767c478bd9Sstevel@tonic-gate 			    (crypto_data_t *)ct, NULL, spi_encr_tmpl);
2777c478bd9Sstevel@tonic-gate 		} else {
2787c478bd9Sstevel@tonic-gate 			KCF_WRAP_ENCRYPT_OPS_PARAMS(&params, KCF_OP_ATOMIC,
2797c478bd9Sstevel@tonic-gate 			    pd->pd_sid, encr_mech, encr_key, pt,
2807c478bd9Sstevel@tonic-gate 			    (crypto_data_t *)ct, spi_encr_tmpl);
2817c478bd9Sstevel@tonic-gate 		}
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 		error = kcf_submit_request(pd, NULL, (crq == NULL) ? NULL :
2847c478bd9Sstevel@tonic-gate 		    &encr_req, &params, B_TRUE);
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 		switch (error) {
2877c478bd9Sstevel@tonic-gate 		case CRYPTO_SUCCESS: {
2887c478bd9Sstevel@tonic-gate 			off_t saveoffset;
2897c478bd9Sstevel@tonic-gate 			size_t savelen;
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 			/*
2927c478bd9Sstevel@tonic-gate 			 * The encryption step is done. Reuse the encr_req
2937c478bd9Sstevel@tonic-gate 			 * for submitting the MAC step.
2947c478bd9Sstevel@tonic-gate 			 */
2957c478bd9Sstevel@tonic-gate 			if (next_req == NULL) {
2967c478bd9Sstevel@tonic-gate 				saveoffset = ct->dd_offset1;
2977c478bd9Sstevel@tonic-gate 				savelen = ct->dd_len1;
2987c478bd9Sstevel@tonic-gate 			} else {
2997c478bd9Sstevel@tonic-gate 				saveoffset = next_req->kr_saveoffset =
3007c478bd9Sstevel@tonic-gate 				    ct->dd_offset1;
3017c478bd9Sstevel@tonic-gate 				savelen = next_req->kr_savelen = ct->dd_len1;
3027c478bd9Sstevel@tonic-gate 				encr_req.cr_callback_func = kcf_last_req;
3037c478bd9Sstevel@tonic-gate 			}
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 			ct->dd_offset1 = ct->dd_offset2;
3067c478bd9Sstevel@tonic-gate 			ct->dd_len1 = ct->dd_len2;
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 			error = crypto_mac(mac_mech, (crypto_data_t *)ct,
3097c478bd9Sstevel@tonic-gate 			    mac_key, mac_tmpl, mac, (crq == NULL) ? NULL :
3107c478bd9Sstevel@tonic-gate 			    &encr_req);
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 			if (error != CRYPTO_QUEUED) {
3137c478bd9Sstevel@tonic-gate 				ct->dd_offset1 = saveoffset;
3147c478bd9Sstevel@tonic-gate 				ct->dd_len1 = savelen;
3157c478bd9Sstevel@tonic-gate 			}
3167c478bd9Sstevel@tonic-gate 			break;
3177c478bd9Sstevel@tonic-gate 		}
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 		case CRYPTO_QUEUED:
3207c478bd9Sstevel@tonic-gate 			if ((crq != NULL) &&
3217c478bd9Sstevel@tonic-gate 			    !(crq->cr_flag & CRYPTO_SKIP_REQID))
3227c478bd9Sstevel@tonic-gate 				crq->cr_reqid = encr_req.cr_reqid;
3237c478bd9Sstevel@tonic-gate 			break;
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 		default:
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 			/* Add pd to the linked list of providers tried. */
3287c478bd9Sstevel@tonic-gate 			if (IS_RECOVERABLE(error)) {
3297c478bd9Sstevel@tonic-gate 				if (kcf_insert_triedlist(&list, pd,
3307c478bd9Sstevel@tonic-gate 				    KCF_KMFLAG(crq)) != NULL)
3317c478bd9Sstevel@tonic-gate 					goto retry;
3327c478bd9Sstevel@tonic-gate 			}
3337c478bd9Sstevel@tonic-gate 		}
3347c478bd9Sstevel@tonic-gate 		if (error != CRYPTO_QUEUED && next_req != NULL)
3357c478bd9Sstevel@tonic-gate 			kmem_free(next_req, sizeof (kcf_dual_req_t));
3367c478bd9Sstevel@tonic-gate 		if (list != NULL)
3377c478bd9Sstevel@tonic-gate 			kcf_free_triedlist(list);
3387c478bd9Sstevel@tonic-gate 		KCF_PROV_REFRELE(pd);
3397c478bd9Sstevel@tonic-gate 		return (error);
3407c478bd9Sstevel@tonic-gate 	}
3417c478bd9Sstevel@tonic-gate 	if ((!mac_tmpl_checked) && (pd->pd_prov_type == CRYPTO_SW_PROVIDER)) {
3427c478bd9Sstevel@tonic-gate 		if ((mac_tmpl != NULL) &&
3437c478bd9Sstevel@tonic-gate 		    (prov_mac_mechid != CRYPTO_MECH_INVALID)) {
3447c478bd9Sstevel@tonic-gate 			ctx_mac_tmpl = (kcf_ctx_template_t *)mac_tmpl;
3457c478bd9Sstevel@tonic-gate 			if (ctx_mac_tmpl->ct_generation != me->me_gen_swprov) {
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 				if (next_req != NULL)
3487c478bd9Sstevel@tonic-gate 					kmem_free(next_req,
3497c478bd9Sstevel@tonic-gate 					    sizeof (kcf_dual_req_t));
3507c478bd9Sstevel@tonic-gate 				if (list != NULL)
3517c478bd9Sstevel@tonic-gate 					kcf_free_triedlist(list);
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 				KCF_PROV_REFRELE(pd);
3547c478bd9Sstevel@tonic-gate 				/* Which one is the the old one ? */
3557c478bd9Sstevel@tonic-gate 				return (CRYPTO_OLD_CTX_TEMPLATE);
3567c478bd9Sstevel@tonic-gate 			}
3577c478bd9Sstevel@tonic-gate 			spi_mac_tmpl = ctx_mac_tmpl->ct_prov_tmpl;
3587c478bd9Sstevel@tonic-gate 		}
3597c478bd9Sstevel@tonic-gate 		mac_tmpl_checked = B_TRUE;
3607c478bd9Sstevel@tonic-gate 	}
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	/* The fast path for SW providers. */
3637c478bd9Sstevel@tonic-gate 	if (CHECK_FASTPATH(crq, pd)) {
3647c478bd9Sstevel@tonic-gate 		crypto_mechanism_t lencr_mech;
3657c478bd9Sstevel@tonic-gate 		crypto_mechanism_t lmac_mech;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 		/* careful! structs assignments */
3687c478bd9Sstevel@tonic-gate 		lencr_mech = *encr_mech;
3697c478bd9Sstevel@tonic-gate 		lencr_mech.cm_type = prov_encr_mechid;
3707c478bd9Sstevel@tonic-gate 		lmac_mech = *mac_mech;
3717c478bd9Sstevel@tonic-gate 		lmac_mech.cm_type = prov_mac_mechid;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 		error = KCF_PROV_ENCRYPT_MAC_ATOMIC(pd, pd->pd_sid,
3747c478bd9Sstevel@tonic-gate 		    &lencr_mech, encr_key, &lmac_mech, mac_key, pt, ct,
3757c478bd9Sstevel@tonic-gate 		    mac, spi_encr_tmpl, spi_mac_tmpl, KCF_SWFP_RHNDL(crq));
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 		KCF_PROV_INCRSTATS(pd, error);
3787c478bd9Sstevel@tonic-gate 	} else {
3797c478bd9Sstevel@tonic-gate 		KCF_WRAP_ENCRYPT_MAC_OPS_PARAMS(&params, KCF_OP_ATOMIC,
3807c478bd9Sstevel@tonic-gate 		    pd->pd_sid, encr_key, mac_key, pt, ct, mac, spi_encr_tmpl,
3817c478bd9Sstevel@tonic-gate 		    spi_mac_tmpl);
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 		cmops = &(params.rp_u.encrypt_mac_params);
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 		/* careful! structs assignments */
3867c478bd9Sstevel@tonic-gate 		cmops->em_encr_mech = *encr_mech;
3877c478bd9Sstevel@tonic-gate 		cmops->em_encr_mech.cm_type = prov_encr_mechid;
3887c478bd9Sstevel@tonic-gate 		cmops->em_framework_encr_mechtype = encr_mech->cm_type;
3897c478bd9Sstevel@tonic-gate 		cmops->em_mac_mech = *mac_mech;
3907c478bd9Sstevel@tonic-gate 		cmops->em_mac_mech.cm_type = prov_mac_mechid;
3917c478bd9Sstevel@tonic-gate 		cmops->em_framework_mac_mechtype = mac_mech->cm_type;
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 		error = kcf_submit_request(pd, NULL, crq, &params, B_FALSE);
3947c478bd9Sstevel@tonic-gate 	}
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED &&
3977c478bd9Sstevel@tonic-gate 	    IS_RECOVERABLE(error)) {
3987c478bd9Sstevel@tonic-gate 		/* Add pd to the linked list of providers tried. */
3997c478bd9Sstevel@tonic-gate 		if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL)
4007c478bd9Sstevel@tonic-gate 			goto retry;
4017c478bd9Sstevel@tonic-gate 	}
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	if (next_req != NULL)
4047c478bd9Sstevel@tonic-gate 		kmem_free(next_req, sizeof (kcf_dual_req_t));
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	if (list != NULL)
4077c478bd9Sstevel@tonic-gate 		kcf_free_triedlist(list);
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	KCF_PROV_REFRELE(pd);
4107c478bd9Sstevel@tonic-gate 	return (error);
4117c478bd9Sstevel@tonic-gate }
4127c478bd9Sstevel@tonic-gate 
413894b2776Smcpowers int
crypto_encrypt_mac_init_prov(crypto_provider_t provider,crypto_session_id_t sid,crypto_mechanism_t * encr_mech,crypto_mechanism_t * mac_mech,crypto_key_t * encr_key,crypto_key_t * mac_key,crypto_ctx_template_t encr_tmpl,crypto_ctx_template_t mac_tmpl,crypto_context_t * ctxp,crypto_call_req_t * cr)414894b2776Smcpowers crypto_encrypt_mac_init_prov(crypto_provider_t provider,
415894b2776Smcpowers     crypto_session_id_t sid, crypto_mechanism_t *encr_mech,
416894b2776Smcpowers     crypto_mechanism_t *mac_mech, crypto_key_t *encr_key,
417894b2776Smcpowers     crypto_key_t *mac_key, crypto_ctx_template_t encr_tmpl,
418894b2776Smcpowers     crypto_ctx_template_t mac_tmpl, crypto_context_t *ctxp,
419894b2776Smcpowers     crypto_call_req_t *cr)
420894b2776Smcpowers {
421894b2776Smcpowers 	/*
422894b2776Smcpowers 	 * First try to find a provider for the encryption mechanism, that
423894b2776Smcpowers 	 * is also capable of the MAC mechanism.
424894b2776Smcpowers 	 */
425894b2776Smcpowers 	int rv;
426894b2776Smcpowers 	kcf_mech_entry_t *me;
427894b2776Smcpowers 	kcf_provider_desc_t *pd = provider;
428894b2776Smcpowers 	kcf_provider_desc_t *real_provider = pd;
429894b2776Smcpowers 	kcf_ctx_template_t *ctx_encr_tmpl, *ctx_mac_tmpl;
430894b2776Smcpowers 	kcf_req_params_t params;
431894b2776Smcpowers 	kcf_encrypt_mac_ops_params_t *cmops;
432894b2776Smcpowers 	crypto_spi_ctx_template_t spi_encr_tmpl = NULL, spi_mac_tmpl = NULL;
433894b2776Smcpowers 	crypto_ctx_t *ctx;
434894b2776Smcpowers 	kcf_context_t *encr_kcf_context = NULL;
435894b2776Smcpowers 
436894b2776Smcpowers 	ASSERT(KCF_PROV_REFHELD(pd));
437894b2776Smcpowers 
438894b2776Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
439436935a1SVladimir Kotal 		rv = kcf_get_hardware_provider(encr_mech->cm_type, encr_key,
4409b009fc1SValerie Bubb Fenwick 		    mac_mech->cm_type, mac_key, pd, &real_provider,
4419b009fc1SValerie Bubb Fenwick 		    CRYPTO_FG_ENCRYPT_MAC);
442894b2776Smcpowers 
443894b2776Smcpowers 		if (rv != CRYPTO_SUCCESS)
444894b2776Smcpowers 			return (rv);
445894b2776Smcpowers 	}
446894b2776Smcpowers 
447894b2776Smcpowers 	/*
448894b2776Smcpowers 	 * For SW providers, check the validity of the context template
449894b2776Smcpowers 	 * It is very rare that the generation number mis-matches, so
450894b2776Smcpowers 	 * is acceptable to fail here, and let the consumer recover by
451894b2776Smcpowers 	 * freeing this tmpl and create a new one for the key and new SW
452894b2776Smcpowers 	 * provider
453894b2776Smcpowers 	 * Warning! will need to change when multiple software providers
454894b2776Smcpowers 	 * per mechanism are supported.
455894b2776Smcpowers 	 */
456894b2776Smcpowers 
457894b2776Smcpowers 	if (real_provider->pd_prov_type == CRYPTO_SW_PROVIDER) {
458894b2776Smcpowers 		if (encr_tmpl != NULL) {
459894b2776Smcpowers 			if (kcf_get_mech_entry(encr_mech->cm_type, &me) !=
460894b2776Smcpowers 			    KCF_SUCCESS) {
461894b2776Smcpowers 				rv = CRYPTO_MECHANISM_INVALID;
462894b2776Smcpowers 				goto out;
463894b2776Smcpowers 			}
464894b2776Smcpowers 			ctx_encr_tmpl = (kcf_ctx_template_t *)encr_tmpl;
465894b2776Smcpowers 			if (ctx_encr_tmpl->ct_generation != me->me_gen_swprov) {
466894b2776Smcpowers 				rv = CRYPTO_OLD_CTX_TEMPLATE;
467894b2776Smcpowers 				goto out;
468894b2776Smcpowers 			}
469894b2776Smcpowers 			spi_encr_tmpl = ctx_encr_tmpl->ct_prov_tmpl;
470894b2776Smcpowers 		}
471894b2776Smcpowers 
472894b2776Smcpowers 		if (mac_tmpl != NULL) {
473894b2776Smcpowers 			if (kcf_get_mech_entry(mac_mech->cm_type, &me) !=
474894b2776Smcpowers 			    KCF_SUCCESS) {
475894b2776Smcpowers 				rv = CRYPTO_MECHANISM_INVALID;
476894b2776Smcpowers 				goto out;
477894b2776Smcpowers 			}
478894b2776Smcpowers 			ctx_mac_tmpl = (kcf_ctx_template_t *)mac_tmpl;
479894b2776Smcpowers 			if (ctx_mac_tmpl->ct_generation != me->me_gen_swprov) {
480894b2776Smcpowers 				rv = CRYPTO_OLD_CTX_TEMPLATE;
481894b2776Smcpowers 				goto out;
482894b2776Smcpowers 			}
483894b2776Smcpowers 			spi_mac_tmpl = ctx_mac_tmpl->ct_prov_tmpl;
484894b2776Smcpowers 		}
485894b2776Smcpowers 	}
486894b2776Smcpowers 
487894b2776Smcpowers 	ctx = kcf_new_ctx(cr, real_provider, sid);
488894b2776Smcpowers 	if (ctx == NULL) {
489894b2776Smcpowers 		rv = CRYPTO_HOST_MEMORY;
490894b2776Smcpowers 		goto out;
491894b2776Smcpowers 	}
492894b2776Smcpowers 	encr_kcf_context = (kcf_context_t *)ctx->cc_framework_private;
493894b2776Smcpowers 
494894b2776Smcpowers 	/* The fast path for SW providers. */
495894b2776Smcpowers 	if (CHECK_FASTPATH(cr, real_provider)) {
496894b2776Smcpowers 		crypto_mechanism_t lencr_mech;
497894b2776Smcpowers 		crypto_mechanism_t lmac_mech;
498894b2776Smcpowers 
499894b2776Smcpowers 		/* careful! structs assignments */
500894b2776Smcpowers 		lencr_mech = *encr_mech;
501894b2776Smcpowers 		KCF_SET_PROVIDER_MECHNUM(encr_mech->cm_type, real_provider,
502894b2776Smcpowers 		    &lencr_mech);
503894b2776Smcpowers 
504894b2776Smcpowers 		lmac_mech = *mac_mech;
505894b2776Smcpowers 		KCF_SET_PROVIDER_MECHNUM(mac_mech->cm_type, real_provider,
506894b2776Smcpowers 		    &lmac_mech);
507894b2776Smcpowers 
508894b2776Smcpowers 		rv = KCF_PROV_ENCRYPT_MAC_INIT(real_provider, ctx, &lencr_mech,
509894b2776Smcpowers 		    encr_key, &lmac_mech, mac_key, spi_encr_tmpl, spi_mac_tmpl,
510894b2776Smcpowers 		    KCF_SWFP_RHNDL(cr));
511894b2776Smcpowers 
512894b2776Smcpowers 		KCF_PROV_INCRSTATS(pd, rv);
513894b2776Smcpowers 	} else {
514894b2776Smcpowers 		KCF_WRAP_ENCRYPT_MAC_OPS_PARAMS(&params, KCF_OP_INIT,
515894b2776Smcpowers 		    sid, encr_key, mac_key, NULL, NULL, NULL,
516894b2776Smcpowers 		    spi_encr_tmpl, spi_mac_tmpl);
517894b2776Smcpowers 
518894b2776Smcpowers 		cmops = &(params.rp_u.encrypt_mac_params);
519894b2776Smcpowers 
520894b2776Smcpowers 		/* careful! structs assignments */
521894b2776Smcpowers 		cmops->em_encr_mech = *encr_mech;
522894b2776Smcpowers 		KCF_SET_PROVIDER_MECHNUM(encr_mech->cm_type, real_provider,
523894b2776Smcpowers 		    &cmops->em_encr_mech);
524894b2776Smcpowers 		cmops->em_framework_encr_mechtype = encr_mech->cm_type;
525894b2776Smcpowers 
526894b2776Smcpowers 		cmops->em_mac_mech = *mac_mech;
527894b2776Smcpowers 		KCF_SET_PROVIDER_MECHNUM(mac_mech->cm_type, real_provider,
528894b2776Smcpowers 		    &cmops->em_mac_mech);
529894b2776Smcpowers 		cmops->em_framework_mac_mechtype = mac_mech->cm_type;
530894b2776Smcpowers 
531894b2776Smcpowers 		rv = kcf_submit_request(real_provider, ctx, cr, &params,
532894b2776Smcpowers 		    B_FALSE);
533894b2776Smcpowers 	}
534894b2776Smcpowers 
535894b2776Smcpowers 	if (rv != CRYPTO_SUCCESS && rv != CRYPTO_QUEUED) {
536894b2776Smcpowers 		KCF_CONTEXT_REFRELE(encr_kcf_context);
537894b2776Smcpowers 	} else
538894b2776Smcpowers 		*ctxp = (crypto_context_t)ctx;
539894b2776Smcpowers 
540894b2776Smcpowers out:
541894b2776Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
542894b2776Smcpowers 		KCF_PROV_REFRELE(real_provider);
543894b2776Smcpowers 	return (rv);
544894b2776Smcpowers }
545894b2776Smcpowers 
5467c478bd9Sstevel@tonic-gate /*
5477c478bd9Sstevel@tonic-gate  * Starts a multi-part dual encrypt/mac operation. The provider and session
5487c478bd9Sstevel@tonic-gate  * to use are determined by the KCF dispatcher.
5497c478bd9Sstevel@tonic-gate  */
5507c478bd9Sstevel@tonic-gate /* ARGSUSED */
5517c478bd9Sstevel@tonic-gate int
crypto_encrypt_mac_init(crypto_mechanism_t * encr_mech,crypto_mechanism_t * mac_mech,crypto_key_t * encr_key,crypto_key_t * mac_key,crypto_ctx_template_t encr_tmpl,crypto_ctx_template_t mac_tmpl,crypto_context_t * ctxp,crypto_call_req_t * cr)5527c478bd9Sstevel@tonic-gate crypto_encrypt_mac_init(crypto_mechanism_t *encr_mech,
5537c478bd9Sstevel@tonic-gate     crypto_mechanism_t *mac_mech, crypto_key_t *encr_key,
5547c478bd9Sstevel@tonic-gate     crypto_key_t *mac_key, crypto_ctx_template_t encr_tmpl,
5557c478bd9Sstevel@tonic-gate     crypto_ctx_template_t mac_tmpl, crypto_context_t *ctxp,
5567c478bd9Sstevel@tonic-gate     crypto_call_req_t *cr)
5577c478bd9Sstevel@tonic-gate {
5587c478bd9Sstevel@tonic-gate 	/*
5597c478bd9Sstevel@tonic-gate 	 * First try to find a provider for the encryption mechanism, that
5607c478bd9Sstevel@tonic-gate 	 * is also capable of the MAC mechanism.
5617c478bd9Sstevel@tonic-gate 	 */
5627c478bd9Sstevel@tonic-gate 	int error;
5637c478bd9Sstevel@tonic-gate 	kcf_mech_entry_t *me;
5647c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
5657c478bd9Sstevel@tonic-gate 	kcf_ctx_template_t *ctx_encr_tmpl, *ctx_mac_tmpl;
5667c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
5677c478bd9Sstevel@tonic-gate 	kcf_encrypt_mac_ops_params_t *cmops;
5687c478bd9Sstevel@tonic-gate 	crypto_spi_ctx_template_t spi_encr_tmpl = NULL, spi_mac_tmpl = NULL;
5697c478bd9Sstevel@tonic-gate 	crypto_mech_type_t prov_encr_mechid, prov_mac_mechid;
5707c478bd9Sstevel@tonic-gate 	kcf_prov_tried_t *list = NULL;
5717c478bd9Sstevel@tonic-gate 	boolean_t encr_tmpl_checked = B_FALSE;
5727c478bd9Sstevel@tonic-gate 	boolean_t mac_tmpl_checked = B_FALSE;
5737c478bd9Sstevel@tonic-gate 	crypto_ctx_t *ctx = NULL;
5747c478bd9Sstevel@tonic-gate 	kcf_context_t *encr_kcf_context = NULL, *mac_kcf_context;
5757c478bd9Sstevel@tonic-gate 	crypto_call_flag_t save_flag;
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate retry:
5787c478bd9Sstevel@tonic-gate 	/* pd is returned held on success */
579436935a1SVladimir Kotal 	pd = kcf_get_dual_provider(encr_mech, encr_key, mac_mech, mac_key,
580436935a1SVladimir Kotal 	    &me, &prov_encr_mechid,
5817c478bd9Sstevel@tonic-gate 	    &prov_mac_mechid, &error, list,
5829b009fc1SValerie Bubb Fenwick 	    CRYPTO_FG_ENCRYPT | CRYPTO_FG_ENCRYPT_MAC, CRYPTO_FG_MAC, 0);
5837c478bd9Sstevel@tonic-gate 	if (pd == NULL) {
5847c478bd9Sstevel@tonic-gate 		if (list != NULL)
5857c478bd9Sstevel@tonic-gate 			kcf_free_triedlist(list);
5867c478bd9Sstevel@tonic-gate 		return (error);
5877c478bd9Sstevel@tonic-gate 	}
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	/*
5907c478bd9Sstevel@tonic-gate 	 * For SW providers, check the validity of the context template
5917c478bd9Sstevel@tonic-gate 	 * It is very rare that the generation number mis-matches, so
5927c478bd9Sstevel@tonic-gate 	 * is acceptable to fail here, and let the consumer recover by
5937c478bd9Sstevel@tonic-gate 	 * freeing this tmpl and create a new one for the key and new SW
5947c478bd9Sstevel@tonic-gate 	 * provider
5957c478bd9Sstevel@tonic-gate 	 * Warning! will need to change when multiple software providers
5967c478bd9Sstevel@tonic-gate 	 * per mechanism are supported.
5977c478bd9Sstevel@tonic-gate 	 */
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 	if ((!encr_tmpl_checked) && (pd->pd_prov_type == CRYPTO_SW_PROVIDER)) {
6007c478bd9Sstevel@tonic-gate 		if (encr_tmpl != NULL) {
6017c478bd9Sstevel@tonic-gate 			ctx_encr_tmpl = (kcf_ctx_template_t *)encr_tmpl;
6027c478bd9Sstevel@tonic-gate 			if (ctx_encr_tmpl->ct_generation != me->me_gen_swprov) {
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 				if (list != NULL)
6057c478bd9Sstevel@tonic-gate 					kcf_free_triedlist(list);
6067c478bd9Sstevel@tonic-gate 				if (encr_kcf_context != NULL)
6077c478bd9Sstevel@tonic-gate 					KCF_CONTEXT_REFRELE(encr_kcf_context);
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 				KCF_PROV_REFRELE(pd);
6107c478bd9Sstevel@tonic-gate 				/* Which one is the the old one ? */
6117c478bd9Sstevel@tonic-gate 				return (CRYPTO_OLD_CTX_TEMPLATE);
6127c478bd9Sstevel@tonic-gate 			}
6137c478bd9Sstevel@tonic-gate 			spi_encr_tmpl = ctx_encr_tmpl->ct_prov_tmpl;
6147c478bd9Sstevel@tonic-gate 		}
6157c478bd9Sstevel@tonic-gate 		encr_tmpl_checked = B_TRUE;
6167c478bd9Sstevel@tonic-gate 	}
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	if (prov_mac_mechid == CRYPTO_MECH_INVALID) {
6197c478bd9Sstevel@tonic-gate 		/* Need to emulate with 2 internal calls */
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 		/*
6227c478bd9Sstevel@tonic-gate 		 * We avoid code complexity by limiting the pure async.
6237c478bd9Sstevel@tonic-gate 		 * case to be done using only a SW provider.
6247c478bd9Sstevel@tonic-gate 		 * XXX - Redo the emulation code below so that we can
6257c478bd9Sstevel@tonic-gate 		 * remove this limitation.
6267c478bd9Sstevel@tonic-gate 		 */
6277c478bd9Sstevel@tonic-gate 		if (cr != NULL && pd->pd_prov_type == CRYPTO_HW_PROVIDER) {
6287c478bd9Sstevel@tonic-gate 			if ((kcf_insert_triedlist(&list, pd, KCF_KMFLAG(cr))
6297c478bd9Sstevel@tonic-gate 			    != NULL))
6307c478bd9Sstevel@tonic-gate 				goto retry;
6317c478bd9Sstevel@tonic-gate 			if (list != NULL)
6327c478bd9Sstevel@tonic-gate 				kcf_free_triedlist(list);
6337c478bd9Sstevel@tonic-gate 			if (encr_kcf_context != NULL)
6347c478bd9Sstevel@tonic-gate 				KCF_CONTEXT_REFRELE(encr_kcf_context);
6357c478bd9Sstevel@tonic-gate 			KCF_PROV_REFRELE(pd);
6367c478bd9Sstevel@tonic-gate 			return (CRYPTO_HOST_MEMORY);
6377c478bd9Sstevel@tonic-gate 		}
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 		if (ctx == NULL && pd->pd_prov_type == CRYPTO_SW_PROVIDER) {
6407c478bd9Sstevel@tonic-gate 			ctx = kcf_new_ctx(cr, pd, pd->pd_sid);
6417c478bd9Sstevel@tonic-gate 			if (ctx == NULL) {
6427c478bd9Sstevel@tonic-gate 				if (list != NULL)
6437c478bd9Sstevel@tonic-gate 					kcf_free_triedlist(list);
6447c478bd9Sstevel@tonic-gate 				if (encr_kcf_context != NULL)
6457c478bd9Sstevel@tonic-gate 					KCF_CONTEXT_REFRELE(encr_kcf_context);
6467c478bd9Sstevel@tonic-gate 				KCF_PROV_REFRELE(pd);
6477c478bd9Sstevel@tonic-gate 				return (CRYPTO_HOST_MEMORY);
6487c478bd9Sstevel@tonic-gate 			}
6497c478bd9Sstevel@tonic-gate 			encr_kcf_context = (kcf_context_t *)
6507c478bd9Sstevel@tonic-gate 			    ctx->cc_framework_private;
6517c478bd9Sstevel@tonic-gate 		}
6527c478bd9Sstevel@tonic-gate 		/*
6537c478bd9Sstevel@tonic-gate 		 * Trade-off speed vs avoidance of code complexity and
6547c478bd9Sstevel@tonic-gate 		 * duplication:
6557c478bd9Sstevel@tonic-gate 		 * Could do all the combinations of fastpath / synch / asynch
6567c478bd9Sstevel@tonic-gate 		 * for the encryption and the mac steps. Early attempts
6577c478bd9Sstevel@tonic-gate 		 * showed the code grew wild and bug-prone, for little gain.
6587c478bd9Sstevel@tonic-gate 		 * Therefore, the adaptative asynch case is not implemented.
6597c478bd9Sstevel@tonic-gate 		 * It's either pure synchronous, or pure asynchronous.
6607c478bd9Sstevel@tonic-gate 		 * We still preserve a fastpath for the pure synchronous
6617c478bd9Sstevel@tonic-gate 		 * requests to SW providers.
6627c478bd9Sstevel@tonic-gate 		 */
6637c478bd9Sstevel@tonic-gate 		if (cr == NULL) {
6647c478bd9Sstevel@tonic-gate 			crypto_context_t mac_context;
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 			if (pd->pd_prov_type == CRYPTO_SW_PROVIDER) {
6677c478bd9Sstevel@tonic-gate 				crypto_mechanism_t lmech = *encr_mech;
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 				lmech.cm_type = prov_encr_mechid;
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 				error = KCF_PROV_ENCRYPT_INIT(pd, ctx, &lmech,
6727c478bd9Sstevel@tonic-gate 				    encr_key, spi_encr_tmpl,
6737c478bd9Sstevel@tonic-gate 				    KCF_RHNDL(KM_SLEEP));
6747c478bd9Sstevel@tonic-gate 			} else {
6757c478bd9Sstevel@tonic-gate 				/*
6767c478bd9Sstevel@tonic-gate 				 * If we did the 'goto retry' then ctx may not
6777c478bd9Sstevel@tonic-gate 				 * be NULL.  In general, we can't reuse another
6787c478bd9Sstevel@tonic-gate 				 * provider's context, so we free it now so
6797c478bd9Sstevel@tonic-gate 				 * we don't leak it.
6807c478bd9Sstevel@tonic-gate 				 */
6817c478bd9Sstevel@tonic-gate 				if (ctx != NULL) {
6827c478bd9Sstevel@tonic-gate 					KCF_CONTEXT_REFRELE((kcf_context_t *)
6837c478bd9Sstevel@tonic-gate 					    ctx->cc_framework_private);
6847c478bd9Sstevel@tonic-gate 					encr_kcf_context = NULL;
6857c478bd9Sstevel@tonic-gate 				}
6867c478bd9Sstevel@tonic-gate 				error = crypto_encrypt_init_prov(pd, pd->pd_sid,
6877c478bd9Sstevel@tonic-gate 				    encr_mech, encr_key, &encr_tmpl,
6887c478bd9Sstevel@tonic-gate 				    (crypto_context_t *)&ctx, NULL);
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 				if (error == CRYPTO_SUCCESS) {
6917c478bd9Sstevel@tonic-gate 					encr_kcf_context = (kcf_context_t *)
6927c478bd9Sstevel@tonic-gate 					    ctx->cc_framework_private;
6937c478bd9Sstevel@tonic-gate 				}
6947c478bd9Sstevel@tonic-gate 			}
6957c478bd9Sstevel@tonic-gate 			KCF_PROV_INCRSTATS(pd, error);
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 			KCF_PROV_REFRELE(pd);
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 			if (error != CRYPTO_SUCCESS) {
7007c478bd9Sstevel@tonic-gate 				/* Can't be CRYPTO_QUEUED. return the failure */
7017c478bd9Sstevel@tonic-gate 				if (list != NULL)
7027c478bd9Sstevel@tonic-gate 					kcf_free_triedlist(list);
7037c478bd9Sstevel@tonic-gate 				if (encr_kcf_context != NULL)
7047c478bd9Sstevel@tonic-gate 					KCF_CONTEXT_REFRELE(encr_kcf_context);
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 				return (error);
7077c478bd9Sstevel@tonic-gate 			}
7087c478bd9Sstevel@tonic-gate 			error = crypto_mac_init(mac_mech, mac_key, mac_tmpl,
7097c478bd9Sstevel@tonic-gate 			    &mac_context, NULL);
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 			if (list != NULL)
7127c478bd9Sstevel@tonic-gate 				kcf_free_triedlist(list);
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 			if (error != CRYPTO_SUCCESS) {
7157c478bd9Sstevel@tonic-gate 				/* Should this be an ASSERT() ? */
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 				KCF_CONTEXT_REFRELE(encr_kcf_context);
7187c478bd9Sstevel@tonic-gate 			} else {
7197c478bd9Sstevel@tonic-gate 				encr_kcf_context = (kcf_context_t *)
7207c478bd9Sstevel@tonic-gate 				    ctx->cc_framework_private;
7217c478bd9Sstevel@tonic-gate 				mac_kcf_context = (kcf_context_t *)
7227c478bd9Sstevel@tonic-gate 				    ((crypto_ctx_t *)mac_context)->
7237c478bd9Sstevel@tonic-gate 				    cc_framework_private;
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 				encr_kcf_context->kc_secondctx =
7267c478bd9Sstevel@tonic-gate 				    mac_kcf_context;
7277c478bd9Sstevel@tonic-gate 				KCF_CONTEXT_REFHOLD(mac_kcf_context);
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 				*ctxp = (crypto_context_t)ctx;
7307c478bd9Sstevel@tonic-gate 			}
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 			return (error);
7337c478bd9Sstevel@tonic-gate 		}
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 		/* submit a pure asynchronous request. */
7367c478bd9Sstevel@tonic-gate 		save_flag = cr->cr_flag;
7377c478bd9Sstevel@tonic-gate 		cr->cr_flag |= CRYPTO_ALWAYS_QUEUE;
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 		KCF_WRAP_ENCRYPT_MAC_OPS_PARAMS(&params, KCF_OP_INIT,
7407c478bd9Sstevel@tonic-gate 		    pd->pd_sid, encr_key, mac_key, NULL, NULL, NULL,
7417c478bd9Sstevel@tonic-gate 		    spi_encr_tmpl, spi_mac_tmpl);
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 		cmops = &(params.rp_u.encrypt_mac_params);
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 		/* careful! structs assignments */
7467c478bd9Sstevel@tonic-gate 		cmops->em_encr_mech = *encr_mech;
7477c478bd9Sstevel@tonic-gate 		/*
7487c478bd9Sstevel@tonic-gate 		 * cmops->em_encr_mech.cm_type will be set when we get to
7497c478bd9Sstevel@tonic-gate 		 * kcf_emulate_dual() routine.
7507c478bd9Sstevel@tonic-gate 		 */
7517c478bd9Sstevel@tonic-gate 		cmops->em_framework_encr_mechtype = encr_mech->cm_type;
7527c478bd9Sstevel@tonic-gate 		cmops->em_mac_mech = *mac_mech;
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 		/*
7557c478bd9Sstevel@tonic-gate 		 * cmops->em_mac_mech.cm_type will be set when we know the
7567c478bd9Sstevel@tonic-gate 		 * MAC provider.
7577c478bd9Sstevel@tonic-gate 		 */
7587c478bd9Sstevel@tonic-gate 		cmops->em_framework_mac_mechtype = mac_mech->cm_type;
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 		/*
7617c478bd9Sstevel@tonic-gate 		 * non-NULL ctx->kc_secondctx tells common_submit_request
7627c478bd9Sstevel@tonic-gate 		 * that this request uses separate cipher and MAC contexts.
7637c478bd9Sstevel@tonic-gate 		 * That function will set ctx->kc_secondctx to the new
7647c478bd9Sstevel@tonic-gate 		 * MAC context, once it gets one.
7657c478bd9Sstevel@tonic-gate 		 */
7667c478bd9Sstevel@tonic-gate 		encr_kcf_context->kc_secondctx = encr_kcf_context;
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 		error = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 		cr->cr_flag = save_flag;
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 		if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED) {
7737c478bd9Sstevel@tonic-gate 			KCF_CONTEXT_REFRELE(encr_kcf_context);
7747c478bd9Sstevel@tonic-gate 		}
7757c478bd9Sstevel@tonic-gate 		if (list != NULL)
7767c478bd9Sstevel@tonic-gate 			kcf_free_triedlist(list);
7777c478bd9Sstevel@tonic-gate 		*ctxp = (crypto_context_t)ctx;
7787c478bd9Sstevel@tonic-gate 		KCF_PROV_REFRELE(pd);
7797c478bd9Sstevel@tonic-gate 		return (error);
7807c478bd9Sstevel@tonic-gate 	}
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	if ((!mac_tmpl_checked) && (pd->pd_prov_type == CRYPTO_SW_PROVIDER)) {
7837c478bd9Sstevel@tonic-gate 		if ((mac_tmpl != NULL) &&
7847c478bd9Sstevel@tonic-gate 		    (prov_mac_mechid != CRYPTO_MECH_INVALID)) {
7857c478bd9Sstevel@tonic-gate 			ctx_mac_tmpl = (kcf_ctx_template_t *)mac_tmpl;
7867c478bd9Sstevel@tonic-gate 			if (ctx_mac_tmpl->ct_generation != me->me_gen_swprov) {
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 				if (list != NULL)
7897c478bd9Sstevel@tonic-gate 					kcf_free_triedlist(list);
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 				KCF_PROV_REFRELE(pd);
7927c478bd9Sstevel@tonic-gate 				/* Which one is the the old one ? */
7937c478bd9Sstevel@tonic-gate 				return (CRYPTO_OLD_CTX_TEMPLATE);
7947c478bd9Sstevel@tonic-gate 			}
7957c478bd9Sstevel@tonic-gate 			spi_mac_tmpl = ctx_mac_tmpl->ct_prov_tmpl;
7967c478bd9Sstevel@tonic-gate 		}
7977c478bd9Sstevel@tonic-gate 		mac_tmpl_checked = B_TRUE;
7987c478bd9Sstevel@tonic-gate 	}
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	if (ctx == NULL) {
8017c478bd9Sstevel@tonic-gate 		ctx = kcf_new_ctx(cr, pd, pd->pd_sid);
8027c478bd9Sstevel@tonic-gate 		if (ctx == NULL) {
8037c478bd9Sstevel@tonic-gate 			if (list != NULL)
8047c478bd9Sstevel@tonic-gate 				kcf_free_triedlist(list);
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 			KCF_PROV_REFRELE(pd);
8077c478bd9Sstevel@tonic-gate 			return (CRYPTO_HOST_MEMORY);
8087c478bd9Sstevel@tonic-gate 		}
8097c478bd9Sstevel@tonic-gate 		encr_kcf_context = (kcf_context_t *)ctx->cc_framework_private;
8107c478bd9Sstevel@tonic-gate 	}
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 	/* The fast path for SW providers. */
8137c478bd9Sstevel@tonic-gate 	if (CHECK_FASTPATH(cr, pd)) {
8147c478bd9Sstevel@tonic-gate 		crypto_mechanism_t lencr_mech;
8157c478bd9Sstevel@tonic-gate 		crypto_mechanism_t lmac_mech;
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 		/* careful! structs assignments */
8187c478bd9Sstevel@tonic-gate 		lencr_mech = *encr_mech;
8197c478bd9Sstevel@tonic-gate 		lencr_mech.cm_type = prov_encr_mechid;
8207c478bd9Sstevel@tonic-gate 		lmac_mech = *mac_mech;
8217c478bd9Sstevel@tonic-gate 		lmac_mech.cm_type = prov_mac_mechid;
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 		error = KCF_PROV_ENCRYPT_MAC_INIT(pd, ctx, &lencr_mech,
8247c478bd9Sstevel@tonic-gate 		    encr_key, &lmac_mech, mac_key, spi_encr_tmpl, spi_mac_tmpl,
8257c478bd9Sstevel@tonic-gate 		    KCF_SWFP_RHNDL(cr));
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 		KCF_PROV_INCRSTATS(pd, error);
8287c478bd9Sstevel@tonic-gate 	} else {
8297c478bd9Sstevel@tonic-gate 		KCF_WRAP_ENCRYPT_MAC_OPS_PARAMS(&params, KCF_OP_INIT,
8307c478bd9Sstevel@tonic-gate 		    pd->pd_sid, encr_key, mac_key, NULL, NULL, NULL,
8317c478bd9Sstevel@tonic-gate 		    spi_encr_tmpl, spi_mac_tmpl);
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 		cmops = &(params.rp_u.encrypt_mac_params);
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 		/* careful! structs assignments */
8367c478bd9Sstevel@tonic-gate 		cmops->em_encr_mech = *encr_mech;
8377c478bd9Sstevel@tonic-gate 		cmops->em_encr_mech.cm_type = prov_encr_mechid;
8387c478bd9Sstevel@tonic-gate 		cmops->em_framework_encr_mechtype = encr_mech->cm_type;
8397c478bd9Sstevel@tonic-gate 		cmops->em_mac_mech = *mac_mech;
8407c478bd9Sstevel@tonic-gate 		cmops->em_mac_mech.cm_type = prov_mac_mechid;
8417c478bd9Sstevel@tonic-gate 		cmops->em_framework_mac_mechtype = mac_mech->cm_type;
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 		error = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
8447c478bd9Sstevel@tonic-gate 	}
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 	if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED) {
8477c478bd9Sstevel@tonic-gate 		if ((IS_RECOVERABLE(error)) &&
8487c478bd9Sstevel@tonic-gate 		    (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(cr)) != NULL))
8497c478bd9Sstevel@tonic-gate 			goto retry;
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 		KCF_CONTEXT_REFRELE(encr_kcf_context);
8527c478bd9Sstevel@tonic-gate 	} else
8537c478bd9Sstevel@tonic-gate 		*ctxp = (crypto_context_t)ctx;
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	if (list != NULL)
8567c478bd9Sstevel@tonic-gate 		kcf_free_triedlist(list);
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	KCF_PROV_REFRELE(pd);
8597c478bd9Sstevel@tonic-gate 	return (error);
8607c478bd9Sstevel@tonic-gate }
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate /*
8637c478bd9Sstevel@tonic-gate  * Continues a multi-part dual encrypt/mac operation.
8647c478bd9Sstevel@tonic-gate  */
8657c478bd9Sstevel@tonic-gate /* ARGSUSED */
8667c478bd9Sstevel@tonic-gate int
crypto_encrypt_mac_update(crypto_context_t context,crypto_data_t * pt,crypto_dual_data_t * ct,crypto_call_req_t * cr)8677c478bd9Sstevel@tonic-gate crypto_encrypt_mac_update(crypto_context_t context,
8687c478bd9Sstevel@tonic-gate     crypto_data_t *pt, crypto_dual_data_t *ct, crypto_call_req_t *cr)
8697c478bd9Sstevel@tonic-gate {
8707c478bd9Sstevel@tonic-gate 	crypto_ctx_t *ctx = (crypto_ctx_t *)context, *mac_ctx;
8717c478bd9Sstevel@tonic-gate 	kcf_context_t *kcf_ctx, *kcf_mac_ctx;
8727c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
8737c478bd9Sstevel@tonic-gate 	int error;
8747c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 	if ((ctx == NULL) ||
8777c478bd9Sstevel@tonic-gate 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
8787c478bd9Sstevel@tonic-gate 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
8797c478bd9Sstevel@tonic-gate 		return (CRYPTO_INVALID_CONTEXT);
8807c478bd9Sstevel@tonic-gate 	}
8817c478bd9Sstevel@tonic-gate 
882894b2776Smcpowers 	ASSERT(pd->pd_prov_type != CRYPTO_LOGICAL_PROVIDER);
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 	if ((kcf_mac_ctx = kcf_ctx->kc_secondctx) != NULL) {
8857c478bd9Sstevel@tonic-gate 		off_t save_offset;
8867c478bd9Sstevel@tonic-gate 		size_t save_len;
8877c478bd9Sstevel@tonic-gate 		crypto_call_flag_t save_flag;
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 		if (kcf_mac_ctx->kc_prov_desc == NULL) {
8907c478bd9Sstevel@tonic-gate 			error = CRYPTO_INVALID_CONTEXT;
8917c478bd9Sstevel@tonic-gate 			goto out;
8927c478bd9Sstevel@tonic-gate 		}
8937c478bd9Sstevel@tonic-gate 		mac_ctx = &kcf_mac_ctx->kc_glbl_ctx;
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 		/* First we submit the encryption request */
8967c478bd9Sstevel@tonic-gate 		if (cr == NULL) {
8977c478bd9Sstevel@tonic-gate 			/*
8987c478bd9Sstevel@tonic-gate 			 * 'ct' is always not NULL.
8997c478bd9Sstevel@tonic-gate 			 * A NULL 'pt' means in-place.
9007c478bd9Sstevel@tonic-gate 			 */
9017c478bd9Sstevel@tonic-gate 			if (pt == NULL)
9027c478bd9Sstevel@tonic-gate 				error = crypto_encrypt_update(context,
9037c478bd9Sstevel@tonic-gate 				    (crypto_data_t *)ct, NULL, NULL);
9047c478bd9Sstevel@tonic-gate 			else
9057c478bd9Sstevel@tonic-gate 				error = crypto_encrypt_update(context, pt,
9067c478bd9Sstevel@tonic-gate 				    (crypto_data_t *)ct, NULL);
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 			if (error != CRYPTO_SUCCESS)
9097c478bd9Sstevel@tonic-gate 				goto out;
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 			/*
9127c478bd9Sstevel@tonic-gate 			 * call  mac_update when there is data to throw in
9137c478bd9Sstevel@tonic-gate 			 * the mix. Either an explicitly non-zero ct->dd_len2,
9147c478bd9Sstevel@tonic-gate 			 * or the last ciphertext portion.
9157c478bd9Sstevel@tonic-gate 			 */
9167c478bd9Sstevel@tonic-gate 			save_offset = ct->dd_offset1;
9177c478bd9Sstevel@tonic-gate 			save_len = ct->dd_len1;
9187c478bd9Sstevel@tonic-gate 			if (ct->dd_len2 == 0) {
9197c478bd9Sstevel@tonic-gate 				/*
9207c478bd9Sstevel@tonic-gate 				 * The previous encrypt step was an
9217c478bd9Sstevel@tonic-gate 				 * accumulation only and didn't produce any
9227c478bd9Sstevel@tonic-gate 				 * partial output
9237c478bd9Sstevel@tonic-gate 				 */
9247c478bd9Sstevel@tonic-gate 				if (ct->dd_len1 == 0)
9257c478bd9Sstevel@tonic-gate 					goto out;
9267c478bd9Sstevel@tonic-gate 			} else {
9277c478bd9Sstevel@tonic-gate 				ct->dd_offset1 = ct->dd_offset2;
9287c478bd9Sstevel@tonic-gate 				ct->dd_len1 = ct->dd_len2;
9297c478bd9Sstevel@tonic-gate 			}
9307c478bd9Sstevel@tonic-gate 			error = crypto_mac_update((crypto_context_t)mac_ctx,
9317c478bd9Sstevel@tonic-gate 			    (crypto_data_t *)ct, NULL);
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 			ct->dd_offset1 = save_offset;
9347c478bd9Sstevel@tonic-gate 			ct->dd_len1 = save_len;
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 			goto out;
9377c478bd9Sstevel@tonic-gate 		}
9387c478bd9Sstevel@tonic-gate 		/* submit a pure asynchronous request. */
9397c478bd9Sstevel@tonic-gate 		save_flag = cr->cr_flag;
9407c478bd9Sstevel@tonic-gate 		cr->cr_flag |= CRYPTO_ALWAYS_QUEUE;
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 		KCF_WRAP_ENCRYPT_MAC_OPS_PARAMS(&params, KCF_OP_UPDATE,
9437c478bd9Sstevel@tonic-gate 		    pd->pd_sid, NULL, NULL, pt, ct, NULL, NULL, NULL)
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 		error = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 		cr->cr_flag = save_flag;
9497c478bd9Sstevel@tonic-gate 		goto out;
9507c478bd9Sstevel@tonic-gate 	}
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 	/* The fast path for SW providers. */
9537c478bd9Sstevel@tonic-gate 	if (CHECK_FASTPATH(cr, pd)) {
9547c478bd9Sstevel@tonic-gate 		error = KCF_PROV_ENCRYPT_MAC_UPDATE(pd, ctx, pt, ct, NULL);
9557c478bd9Sstevel@tonic-gate 		KCF_PROV_INCRSTATS(pd, error);
9567c478bd9Sstevel@tonic-gate 	} else {
9577c478bd9Sstevel@tonic-gate 		KCF_WRAP_ENCRYPT_MAC_OPS_PARAMS(&params, KCF_OP_UPDATE,
958894b2776Smcpowers 		    ctx->cc_session, NULL, NULL, pt, ct, NULL, NULL, NULL);
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 		error = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
9617c478bd9Sstevel@tonic-gate 	}
9627c478bd9Sstevel@tonic-gate out:
9637c478bd9Sstevel@tonic-gate 	return (error);
9647c478bd9Sstevel@tonic-gate }
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate /*
9677c478bd9Sstevel@tonic-gate  * Terminates a multi-part dual encrypt/mac operation.
9687c478bd9Sstevel@tonic-gate  */
9697c478bd9Sstevel@tonic-gate /* ARGSUSED */
crypto_encrypt_mac_final(crypto_context_t context,crypto_dual_data_t * ct,crypto_data_t * mac,crypto_call_req_t * cr)9707c478bd9Sstevel@tonic-gate int crypto_encrypt_mac_final(crypto_context_t context, crypto_dual_data_t *ct,
9717c478bd9Sstevel@tonic-gate     crypto_data_t *mac, crypto_call_req_t *cr)
9727c478bd9Sstevel@tonic-gate {
9737c478bd9Sstevel@tonic-gate 	crypto_ctx_t *ctx = (crypto_ctx_t *)context, *mac_ctx;
9747c478bd9Sstevel@tonic-gate 	kcf_context_t *kcf_ctx, *kcf_mac_ctx;
9757c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
9767c478bd9Sstevel@tonic-gate 	int error;
9777c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 	if ((ctx == NULL) ||
9807c478bd9Sstevel@tonic-gate 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
9817c478bd9Sstevel@tonic-gate 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
9827c478bd9Sstevel@tonic-gate 		return (CRYPTO_INVALID_CONTEXT);
9837c478bd9Sstevel@tonic-gate 	}
9847c478bd9Sstevel@tonic-gate 
985894b2776Smcpowers 	ASSERT(pd->pd_prov_type != CRYPTO_LOGICAL_PROVIDER);
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 	if ((kcf_mac_ctx = kcf_ctx->kc_secondctx) != NULL) {
9887c478bd9Sstevel@tonic-gate 		off_t save_offset;
9897c478bd9Sstevel@tonic-gate 		size_t save_len;
9907c478bd9Sstevel@tonic-gate 		crypto_context_t mac_context;
9917c478bd9Sstevel@tonic-gate 		crypto_call_flag_t save_flag;
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 		if (kcf_mac_ctx->kc_prov_desc == NULL) {
9947c478bd9Sstevel@tonic-gate 			return (CRYPTO_INVALID_CONTEXT);
9957c478bd9Sstevel@tonic-gate 		}
9967c478bd9Sstevel@tonic-gate 		mac_ctx = &kcf_mac_ctx->kc_glbl_ctx;
9977c478bd9Sstevel@tonic-gate 		mac_context = (crypto_context_t)mac_ctx;
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 		if (cr == NULL) {
10007c478bd9Sstevel@tonic-gate 			/* Get the last chunk of ciphertext */
10017c478bd9Sstevel@tonic-gate 			error = crypto_encrypt_final(context,
10027c478bd9Sstevel@tonic-gate 			    (crypto_data_t *)ct, NULL);
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 			if (error != CRYPTO_SUCCESS)  {
10057c478bd9Sstevel@tonic-gate 				/*
10067c478bd9Sstevel@tonic-gate 				 * Needed here, because the caller of
10077c478bd9Sstevel@tonic-gate 				 * crypto_encrypt_mac_final() lost all
10087c478bd9Sstevel@tonic-gate 				 * refs to the mac_ctx.
10097c478bd9Sstevel@tonic-gate 				 */
10107c478bd9Sstevel@tonic-gate 				crypto_cancel_ctx(mac_context);
10117c478bd9Sstevel@tonic-gate 				return (error);
10127c478bd9Sstevel@tonic-gate 			}
10137c478bd9Sstevel@tonic-gate 			if (ct->dd_len2 > 0) {
10147c478bd9Sstevel@tonic-gate 				save_offset = ct->dd_offset1;
10157c478bd9Sstevel@tonic-gate 				save_len = ct->dd_len1;
10167c478bd9Sstevel@tonic-gate 				ct->dd_offset1 = ct->dd_offset2;
10177c478bd9Sstevel@tonic-gate 				ct->dd_len1 = ct->dd_len2;
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 				error = crypto_mac_update(mac_context,
10207c478bd9Sstevel@tonic-gate 				    (crypto_data_t *)ct, NULL);
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 				ct->dd_offset1 = save_offset;
10237c478bd9Sstevel@tonic-gate 				ct->dd_len1 = save_len;
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 				if (error != CRYPTO_SUCCESS)  {
10267c478bd9Sstevel@tonic-gate 					crypto_cancel_ctx(mac_context);
10277c478bd9Sstevel@tonic-gate 					return (error);
10287c478bd9Sstevel@tonic-gate 				}
10297c478bd9Sstevel@tonic-gate 			}
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 			/* and finally, collect the MAC */
10327c478bd9Sstevel@tonic-gate 			error = crypto_mac_final(mac_context, mac, NULL);
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 			return (error);
10357c478bd9Sstevel@tonic-gate 		}
10367c478bd9Sstevel@tonic-gate 		/* submit a pure asynchronous request. */
10377c478bd9Sstevel@tonic-gate 		save_flag = cr->cr_flag;
10387c478bd9Sstevel@tonic-gate 		cr->cr_flag |= CRYPTO_ALWAYS_QUEUE;
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 		KCF_WRAP_ENCRYPT_MAC_OPS_PARAMS(&params, KCF_OP_FINAL,
10417c478bd9Sstevel@tonic-gate 		    pd->pd_sid, NULL, NULL, NULL, ct, mac, NULL, NULL)
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 		error = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 		cr->cr_flag = save_flag;
10477c478bd9Sstevel@tonic-gate 		return (error);
10487c478bd9Sstevel@tonic-gate 	}
10497c478bd9Sstevel@tonic-gate 	/* The fast path for SW providers. */
10507c478bd9Sstevel@tonic-gate 	if (CHECK_FASTPATH(cr, pd)) {
10517c478bd9Sstevel@tonic-gate 		error = KCF_PROV_ENCRYPT_MAC_FINAL(pd, ctx, ct, mac, NULL);
10527c478bd9Sstevel@tonic-gate 		KCF_PROV_INCRSTATS(pd, error);
10537c478bd9Sstevel@tonic-gate 	} else {
10547c478bd9Sstevel@tonic-gate 		KCF_WRAP_ENCRYPT_MAC_OPS_PARAMS(&params, KCF_OP_FINAL,
1055894b2776Smcpowers 		    ctx->cc_session, NULL, NULL, NULL, ct, mac, NULL, NULL);
10567c478bd9Sstevel@tonic-gate 		error = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
10577c478bd9Sstevel@tonic-gate 	}
10587c478bd9Sstevel@tonic-gate out:
10597c478bd9Sstevel@tonic-gate 	/* Release the hold done in kcf_new_ctx() during init step. */
10607c478bd9Sstevel@tonic-gate 	KCF_CONTEXT_COND_RELEASE(error, kcf_ctx);
10617c478bd9Sstevel@tonic-gate 	return (error);
10627c478bd9Sstevel@tonic-gate }
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate /*
10657c478bd9Sstevel@tonic-gate  * Performs an atomic dual mac/decrypt operation. The provider to use
10667c478bd9Sstevel@tonic-gate  * is determined by the KCF dispatcher.
10677c478bd9Sstevel@tonic-gate  */
10687c478bd9Sstevel@tonic-gate int
crypto_mac_decrypt(crypto_mechanism_t * mac_mech,crypto_mechanism_t * decr_mech,crypto_dual_data_t * ct,crypto_key_t * mac_key,crypto_key_t * decr_key,crypto_ctx_template_t mac_tmpl,crypto_ctx_template_t decr_tmpl,crypto_data_t * mac,crypto_data_t * pt,crypto_call_req_t * crq)10697c478bd9Sstevel@tonic-gate crypto_mac_decrypt(crypto_mechanism_t *mac_mech,
10707c478bd9Sstevel@tonic-gate     crypto_mechanism_t *decr_mech, crypto_dual_data_t *ct,
10717c478bd9Sstevel@tonic-gate     crypto_key_t *mac_key, crypto_key_t *decr_key,
10727c478bd9Sstevel@tonic-gate     crypto_ctx_template_t mac_tmpl, crypto_ctx_template_t decr_tmpl,
10737c478bd9Sstevel@tonic-gate     crypto_data_t *mac, crypto_data_t *pt, crypto_call_req_t *crq)
10747c478bd9Sstevel@tonic-gate {
10757c478bd9Sstevel@tonic-gate 	return (crypto_mac_decrypt_common(mac_mech, decr_mech, ct, mac_key,
10767c478bd9Sstevel@tonic-gate 	    decr_key, mac_tmpl, decr_tmpl, mac, pt, crq, B_FALSE));
10777c478bd9Sstevel@tonic-gate }
10787c478bd9Sstevel@tonic-gate 
1079894b2776Smcpowers int
crypto_mac_decrypt_prov(crypto_provider_t provider,crypto_session_id_t sid,crypto_mechanism_t * mac_mech,crypto_mechanism_t * decr_mech,crypto_dual_data_t * ct,crypto_key_t * mac_key,crypto_key_t * decr_key,crypto_ctx_template_t mac_tmpl,crypto_ctx_template_t decr_tmpl,crypto_data_t * mac,crypto_data_t * pt,crypto_call_req_t * crq)1080894b2776Smcpowers crypto_mac_decrypt_prov(crypto_provider_t provider, crypto_session_id_t sid,
1081894b2776Smcpowers     crypto_mechanism_t *mac_mech, crypto_mechanism_t *decr_mech,
1082894b2776Smcpowers     crypto_dual_data_t *ct, crypto_key_t *mac_key, crypto_key_t *decr_key,
1083894b2776Smcpowers     crypto_ctx_template_t mac_tmpl, crypto_ctx_template_t decr_tmpl,
1084894b2776Smcpowers     crypto_data_t *mac, crypto_data_t *pt, crypto_call_req_t *crq)
1085894b2776Smcpowers {
1086894b2776Smcpowers 	return (crypto_mac_decrypt_common_prov(provider, sid, mac_mech,
1087894b2776Smcpowers 	    decr_mech, ct, mac_key, decr_key, mac_tmpl, decr_tmpl, mac, pt,
1088894b2776Smcpowers 	    crq, B_FALSE));
1089894b2776Smcpowers }
1090894b2776Smcpowers 
10917c478bd9Sstevel@tonic-gate /*
10927c478bd9Sstevel@tonic-gate  * Performs an atomic dual mac/decrypt operation. The provider to use
10937c478bd9Sstevel@tonic-gate  * is determined by the KCF dispatcher. 'mac' specifies the expected
10947c478bd9Sstevel@tonic-gate  * value for the MAC. The decryption is not performed if the computed
10957c478bd9Sstevel@tonic-gate  * MAC does not match the expected MAC.
10967c478bd9Sstevel@tonic-gate  */
10977c478bd9Sstevel@tonic-gate int
crypto_mac_verify_decrypt(crypto_mechanism_t * mac_mech,crypto_mechanism_t * decr_mech,crypto_dual_data_t * ct,crypto_key_t * mac_key,crypto_key_t * decr_key,crypto_ctx_template_t mac_tmpl,crypto_ctx_template_t decr_tmpl,crypto_data_t * mac,crypto_data_t * pt,crypto_call_req_t * crq)10987c478bd9Sstevel@tonic-gate crypto_mac_verify_decrypt(crypto_mechanism_t *mac_mech,
10997c478bd9Sstevel@tonic-gate     crypto_mechanism_t *decr_mech, crypto_dual_data_t *ct,
11007c478bd9Sstevel@tonic-gate     crypto_key_t *mac_key, crypto_key_t *decr_key,
11017c478bd9Sstevel@tonic-gate     crypto_ctx_template_t mac_tmpl, crypto_ctx_template_t decr_tmpl,
11027c478bd9Sstevel@tonic-gate     crypto_data_t *mac, crypto_data_t *pt, crypto_call_req_t *crq)
11037c478bd9Sstevel@tonic-gate {
11047c478bd9Sstevel@tonic-gate 	return (crypto_mac_decrypt_common(mac_mech, decr_mech, ct, mac_key,
11057c478bd9Sstevel@tonic-gate 	    decr_key, mac_tmpl, decr_tmpl, mac, pt, crq, B_TRUE));
11067c478bd9Sstevel@tonic-gate }
11077c478bd9Sstevel@tonic-gate 
1108894b2776Smcpowers int
crypto_mac_verify_decrypt_prov(crypto_provider_t provider,crypto_session_id_t sid,crypto_mechanism_t * mac_mech,crypto_mechanism_t * decr_mech,crypto_dual_data_t * ct,crypto_key_t * mac_key,crypto_key_t * decr_key,crypto_ctx_template_t mac_tmpl,crypto_ctx_template_t decr_tmpl,crypto_data_t * mac,crypto_data_t * pt,crypto_call_req_t * crq)1109894b2776Smcpowers crypto_mac_verify_decrypt_prov(crypto_provider_t provider,
1110894b2776Smcpowers     crypto_session_id_t sid, crypto_mechanism_t *mac_mech,
1111894b2776Smcpowers     crypto_mechanism_t *decr_mech, crypto_dual_data_t *ct,
1112894b2776Smcpowers     crypto_key_t *mac_key, crypto_key_t *decr_key,
1113894b2776Smcpowers     crypto_ctx_template_t mac_tmpl, crypto_ctx_template_t decr_tmpl,
1114894b2776Smcpowers     crypto_data_t *mac, crypto_data_t *pt, crypto_call_req_t *crq)
1115894b2776Smcpowers {
1116894b2776Smcpowers 	return (crypto_mac_decrypt_common_prov(provider, sid, mac_mech,
1117894b2776Smcpowers 	    decr_mech, ct, mac_key, decr_key, mac_tmpl, decr_tmpl, mac, pt,
1118894b2776Smcpowers 	    crq, B_TRUE));
1119894b2776Smcpowers }
1120894b2776Smcpowers 
11217c478bd9Sstevel@tonic-gate /*
11227c478bd9Sstevel@tonic-gate  * Called by both crypto_mac_decrypt() and crypto_mac_verify_decrypt().
11237c478bd9Sstevel@tonic-gate  * optionally verified if the MACs match before calling the decryption step.
11247c478bd9Sstevel@tonic-gate  */
11257c478bd9Sstevel@tonic-gate static int
crypto_mac_decrypt_common(crypto_mechanism_t * mac_mech,crypto_mechanism_t * decr_mech,crypto_dual_data_t * ct,crypto_key_t * mac_key,crypto_key_t * decr_key,crypto_ctx_template_t mac_tmpl,crypto_ctx_template_t decr_tmpl,crypto_data_t * mac,crypto_data_t * pt,crypto_call_req_t * crq,boolean_t do_verify)11267c478bd9Sstevel@tonic-gate crypto_mac_decrypt_common(crypto_mechanism_t *mac_mech,
11277c478bd9Sstevel@tonic-gate     crypto_mechanism_t *decr_mech, crypto_dual_data_t *ct,
11287c478bd9Sstevel@tonic-gate     crypto_key_t *mac_key, crypto_key_t *decr_key,
11297c478bd9Sstevel@tonic-gate     crypto_ctx_template_t mac_tmpl, crypto_ctx_template_t decr_tmpl,
11307c478bd9Sstevel@tonic-gate     crypto_data_t *mac, crypto_data_t *pt, crypto_call_req_t *crq,
11317c478bd9Sstevel@tonic-gate     boolean_t do_verify)
11327c478bd9Sstevel@tonic-gate {
11337c478bd9Sstevel@tonic-gate 	/*
11347c478bd9Sstevel@tonic-gate 	 * First try to find a provider for the decryption mechanism, that
11357c478bd9Sstevel@tonic-gate 	 * is also capable of the MAC mechanism.
11367c478bd9Sstevel@tonic-gate 	 * We still favor optimizing the costlier decryption.
11377c478bd9Sstevel@tonic-gate 	 */
11387c478bd9Sstevel@tonic-gate 	int error;
11397c478bd9Sstevel@tonic-gate 	kcf_mech_entry_t *me;
11407c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
11417c478bd9Sstevel@tonic-gate 	kcf_ctx_template_t *ctx_decr_tmpl, *ctx_mac_tmpl;
11427c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
11437c478bd9Sstevel@tonic-gate 	kcf_mac_decrypt_ops_params_t *cmops;
11447c478bd9Sstevel@tonic-gate 	crypto_spi_ctx_template_t spi_decr_tmpl = NULL, spi_mac_tmpl = NULL;
11457c478bd9Sstevel@tonic-gate 	crypto_mech_type_t prov_decr_mechid, prov_mac_mechid;
11467c478bd9Sstevel@tonic-gate 	kcf_prov_tried_t *list = NULL;
11477c478bd9Sstevel@tonic-gate 	boolean_t decr_tmpl_checked = B_FALSE;
11487c478bd9Sstevel@tonic-gate 	boolean_t mac_tmpl_checked = B_FALSE;
11497c478bd9Sstevel@tonic-gate 	kcf_dual_req_t *next_req = NULL;
11507c478bd9Sstevel@tonic-gate 	crypto_call_req_t mac_req, *mac_reqp = NULL;
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate retry:
11537c478bd9Sstevel@tonic-gate 	/* pd is returned held on success */
1154436935a1SVladimir Kotal 	pd = kcf_get_dual_provider(decr_mech, decr_key, mac_mech, mac_key,
1155436935a1SVladimir Kotal 	    &me, &prov_decr_mechid,
11567c478bd9Sstevel@tonic-gate 	    &prov_mac_mechid, &error, list,
11577c478bd9Sstevel@tonic-gate 	    CRYPTO_FG_DECRYPT_ATOMIC | CRYPTO_FG_MAC_DECRYPT_ATOMIC,
11589b009fc1SValerie Bubb Fenwick 	    CRYPTO_FG_MAC_ATOMIC | CRYPTO_FG_MAC_DECRYPT_ATOMIC, ct->dd_len2);
11597c478bd9Sstevel@tonic-gate 	if (pd == NULL) {
11607c478bd9Sstevel@tonic-gate 		if (list != NULL)
11617c478bd9Sstevel@tonic-gate 			kcf_free_triedlist(list);
11627c478bd9Sstevel@tonic-gate 		if (next_req != NULL)
11637c478bd9Sstevel@tonic-gate 			kmem_free(next_req, sizeof (kcf_dual_req_t));
11647c478bd9Sstevel@tonic-gate 		return (CRYPTO_MECH_NOT_SUPPORTED);
11657c478bd9Sstevel@tonic-gate 	}
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 	/*
11687c478bd9Sstevel@tonic-gate 	 * For SW providers, check the validity of the context template
11697c478bd9Sstevel@tonic-gate 	 * It is very rare that the generation number mis-matches, so
11707c478bd9Sstevel@tonic-gate 	 * is acceptable to fail here, and let the consumer recover by
11717c478bd9Sstevel@tonic-gate 	 * freeing this tmpl and create a new one for the key and new SW
11727c478bd9Sstevel@tonic-gate 	 * provider
11737c478bd9Sstevel@tonic-gate 	 */
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 	if ((!decr_tmpl_checked) && (pd->pd_prov_type == CRYPTO_SW_PROVIDER)) {
11767c478bd9Sstevel@tonic-gate 		if (decr_tmpl != NULL) {
11777c478bd9Sstevel@tonic-gate 			ctx_decr_tmpl = (kcf_ctx_template_t *)decr_tmpl;
11787c478bd9Sstevel@tonic-gate 			if (ctx_decr_tmpl->ct_generation != me->me_gen_swprov) {
11797c478bd9Sstevel@tonic-gate 				if (next_req != NULL)
11807c478bd9Sstevel@tonic-gate 					kmem_free(next_req,
11817c478bd9Sstevel@tonic-gate 					    sizeof (kcf_dual_req_t));
11827c478bd9Sstevel@tonic-gate 				if (list != NULL)
11837c478bd9Sstevel@tonic-gate 					kcf_free_triedlist(list);
11847c478bd9Sstevel@tonic-gate 				KCF_PROV_REFRELE(pd);
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 				/* Which one is the the old one ? */
11877c478bd9Sstevel@tonic-gate 				return (CRYPTO_OLD_CTX_TEMPLATE);
11887c478bd9Sstevel@tonic-gate 			}
11897c478bd9Sstevel@tonic-gate 			spi_decr_tmpl = ctx_decr_tmpl->ct_prov_tmpl;
11907c478bd9Sstevel@tonic-gate 		}
11917c478bd9Sstevel@tonic-gate 		decr_tmpl_checked = B_TRUE;
11927c478bd9Sstevel@tonic-gate 	}
11937c478bd9Sstevel@tonic-gate 	if (prov_mac_mechid == CRYPTO_MECH_INVALID) {
11947c478bd9Sstevel@tonic-gate 		/* Need to emulate with 2 internal calls */
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate 		/* Prepare the call_req to be submitted for the MAC step */
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 		if (crq != NULL) {
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 			if (next_req == NULL) {
12017c478bd9Sstevel@tonic-gate 				/*
12027c478bd9Sstevel@tonic-gate 				 * allocate, initialize and prepare the
12037c478bd9Sstevel@tonic-gate 				 * params for the next step only in the
12047c478bd9Sstevel@tonic-gate 				 * first pass (not on every retry).
12057c478bd9Sstevel@tonic-gate 				 */
12067c478bd9Sstevel@tonic-gate 				next_req = kcf_alloc_req(crq);
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 				if (next_req == NULL) {
12097c478bd9Sstevel@tonic-gate 					KCF_PROV_REFRELE(pd);
12107c478bd9Sstevel@tonic-gate 					if (list != NULL)
12117c478bd9Sstevel@tonic-gate 						kcf_free_triedlist(list);
12127c478bd9Sstevel@tonic-gate 					return (CRYPTO_HOST_MEMORY);
12137c478bd9Sstevel@tonic-gate 				}
12147c478bd9Sstevel@tonic-gate 				KCF_WRAP_DECRYPT_OPS_PARAMS(
12157c478bd9Sstevel@tonic-gate 				    &(next_req->kr_params), KCF_OP_ATOMIC,
1216*0f677ac9SToomas Soome 				    0, decr_mech, decr_key,
12177c478bd9Sstevel@tonic-gate 				    (crypto_data_t *)ct, pt, spi_decr_tmpl);
12187c478bd9Sstevel@tonic-gate 			}
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 			mac_req.cr_flag = (crq != NULL) ? crq->cr_flag : 0;
12217c478bd9Sstevel@tonic-gate 			mac_req.cr_flag |= CRYPTO_SETDUAL;
12227c478bd9Sstevel@tonic-gate 			mac_req.cr_callback_func = kcf_next_req;
12237c478bd9Sstevel@tonic-gate 			mac_req.cr_callback_arg = next_req;
12247c478bd9Sstevel@tonic-gate 			mac_reqp = &mac_req;
12257c478bd9Sstevel@tonic-gate 		}
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate 		/* 'pd' is the decryption provider. */
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate 		if (do_verify)
12307c478bd9Sstevel@tonic-gate 			error = crypto_mac_verify(mac_mech, (crypto_data_t *)ct,
12317c478bd9Sstevel@tonic-gate 			    mac_key, mac_tmpl, mac,
12327c478bd9Sstevel@tonic-gate 			    (crq == NULL) ? NULL : mac_reqp);
12337c478bd9Sstevel@tonic-gate 		else
12347c478bd9Sstevel@tonic-gate 			error = crypto_mac(mac_mech, (crypto_data_t *)ct,
12357c478bd9Sstevel@tonic-gate 			    mac_key, mac_tmpl, mac,
12367c478bd9Sstevel@tonic-gate 			    (crq == NULL) ? NULL : mac_reqp);
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 		switch (error) {
12397c478bd9Sstevel@tonic-gate 		case CRYPTO_SUCCESS: {
12407c478bd9Sstevel@tonic-gate 			off_t saveoffset;
12417c478bd9Sstevel@tonic-gate 			size_t savelen;
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 			if (next_req == NULL) {
12447c478bd9Sstevel@tonic-gate 				saveoffset = ct->dd_offset1;
12457c478bd9Sstevel@tonic-gate 				savelen = ct->dd_len1;
12467c478bd9Sstevel@tonic-gate 			} else {
12477c478bd9Sstevel@tonic-gate 				saveoffset = next_req->kr_saveoffset =
12487c478bd9Sstevel@tonic-gate 				    ct->dd_offset1;
12497c478bd9Sstevel@tonic-gate 				savelen = next_req->kr_savelen = ct->dd_len1;
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate 				ASSERT(mac_reqp != NULL);
12527c478bd9Sstevel@tonic-gate 				mac_req.cr_flag &= ~CRYPTO_SETDUAL;
12537c478bd9Sstevel@tonic-gate 				mac_req.cr_callback_func = kcf_last_req;
12547c478bd9Sstevel@tonic-gate 			}
12557c478bd9Sstevel@tonic-gate 			ct->dd_offset1 = ct->dd_offset2;
12567c478bd9Sstevel@tonic-gate 			ct->dd_len1 = ct->dd_len2;
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate 			if (CHECK_FASTPATH(crq, pd)) {
12597c478bd9Sstevel@tonic-gate 				crypto_mechanism_t lmech;
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 				lmech = *decr_mech;
12627c478bd9Sstevel@tonic-gate 				KCF_SET_PROVIDER_MECHNUM(decr_mech->cm_type,
12637c478bd9Sstevel@tonic-gate 				    pd, &lmech);
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate 				error = KCF_PROV_DECRYPT_ATOMIC(pd, pd->pd_sid,
12667c478bd9Sstevel@tonic-gate 				    &lmech, decr_key, (crypto_data_t *)ct,
12677c478bd9Sstevel@tonic-gate 				    (crypto_data_t *)pt, spi_decr_tmpl,
12687c478bd9Sstevel@tonic-gate 				    KCF_SWFP_RHNDL(mac_reqp));
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate 				KCF_PROV_INCRSTATS(pd, error);
12717c478bd9Sstevel@tonic-gate 			} else {
12727c478bd9Sstevel@tonic-gate 				KCF_WRAP_DECRYPT_OPS_PARAMS(&params,
12737c478bd9Sstevel@tonic-gate 				    KCF_OP_ATOMIC, pd->pd_sid, decr_mech,
12747c478bd9Sstevel@tonic-gate 				    decr_key, (crypto_data_t *)ct, pt,
12757c478bd9Sstevel@tonic-gate 				    spi_decr_tmpl);
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate 				error = kcf_submit_request(pd, NULL,
12787c478bd9Sstevel@tonic-gate 				    (crq == NULL) ? NULL : mac_reqp,
12797c478bd9Sstevel@tonic-gate 				    &params, B_FALSE);
12807c478bd9Sstevel@tonic-gate 			}
12817c478bd9Sstevel@tonic-gate 			if (error != CRYPTO_QUEUED) {
12827c478bd9Sstevel@tonic-gate 				KCF_PROV_INCRSTATS(pd, error);
12837c478bd9Sstevel@tonic-gate 				ct->dd_offset1 = saveoffset;
12847c478bd9Sstevel@tonic-gate 				ct->dd_len1 = savelen;
12857c478bd9Sstevel@tonic-gate 			}
12867c478bd9Sstevel@tonic-gate 			break;
12877c478bd9Sstevel@tonic-gate 		}
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate 		case CRYPTO_QUEUED:
12907c478bd9Sstevel@tonic-gate 			if ((crq != NULL) && (crq->cr_flag & CRYPTO_SKIP_REQID))
12917c478bd9Sstevel@tonic-gate 				crq->cr_reqid = mac_req.cr_reqid;
12927c478bd9Sstevel@tonic-gate 			break;
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate 		default:
12957c478bd9Sstevel@tonic-gate 			if (IS_RECOVERABLE(error)) {
12967c478bd9Sstevel@tonic-gate 				if (kcf_insert_triedlist(&list, pd,
12977c478bd9Sstevel@tonic-gate 				    KCF_KMFLAG(crq)) != NULL)
12987c478bd9Sstevel@tonic-gate 					goto retry;
12997c478bd9Sstevel@tonic-gate 			}
13007c478bd9Sstevel@tonic-gate 		}
13017c478bd9Sstevel@tonic-gate 		if (error != CRYPTO_QUEUED && next_req != NULL)
13027c478bd9Sstevel@tonic-gate 			kmem_free(next_req, sizeof (kcf_dual_req_t));
13037c478bd9Sstevel@tonic-gate 		if (list != NULL)
13047c478bd9Sstevel@tonic-gate 			kcf_free_triedlist(list);
13057c478bd9Sstevel@tonic-gate 		KCF_PROV_REFRELE(pd);
13067c478bd9Sstevel@tonic-gate 		return (error);
13077c478bd9Sstevel@tonic-gate 	}
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate 	if ((!mac_tmpl_checked) && (pd->pd_prov_type == CRYPTO_SW_PROVIDER)) {
13107c478bd9Sstevel@tonic-gate 		if ((mac_tmpl != NULL) &&
13117c478bd9Sstevel@tonic-gate 		    (prov_mac_mechid != CRYPTO_MECH_INVALID)) {
13127c478bd9Sstevel@tonic-gate 			ctx_mac_tmpl = (kcf_ctx_template_t *)mac_tmpl;
13137c478bd9Sstevel@tonic-gate 			if (ctx_mac_tmpl->ct_generation != me->me_gen_swprov) {
13147c478bd9Sstevel@tonic-gate 				if (next_req != NULL)
13157c478bd9Sstevel@tonic-gate 					kmem_free(next_req,
13167c478bd9Sstevel@tonic-gate 					    sizeof (kcf_dual_req_t));
13177c478bd9Sstevel@tonic-gate 				if (list != NULL)
13187c478bd9Sstevel@tonic-gate 					kcf_free_triedlist(list);
13197c478bd9Sstevel@tonic-gate 				KCF_PROV_REFRELE(pd);
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 				/* Which one is the the old one ? */
13227c478bd9Sstevel@tonic-gate 				return (CRYPTO_OLD_CTX_TEMPLATE);
13237c478bd9Sstevel@tonic-gate 			}
13247c478bd9Sstevel@tonic-gate 			spi_mac_tmpl = ctx_mac_tmpl->ct_prov_tmpl;
13257c478bd9Sstevel@tonic-gate 		}
13267c478bd9Sstevel@tonic-gate 		mac_tmpl_checked = B_TRUE;
13277c478bd9Sstevel@tonic-gate 	}
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate 	/* The fast path for SW providers. */
13307c478bd9Sstevel@tonic-gate 	if (CHECK_FASTPATH(crq, pd)) {
13317c478bd9Sstevel@tonic-gate 		crypto_mechanism_t lmac_mech;
13327c478bd9Sstevel@tonic-gate 		crypto_mechanism_t ldecr_mech;
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate 		/* careful! structs assignments */
13357c478bd9Sstevel@tonic-gate 		ldecr_mech = *decr_mech;
13367c478bd9Sstevel@tonic-gate 		ldecr_mech.cm_type = prov_decr_mechid;
13377c478bd9Sstevel@tonic-gate 		lmac_mech = *mac_mech;
13387c478bd9Sstevel@tonic-gate 		lmac_mech.cm_type = prov_mac_mechid;
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate 		if (do_verify)
13417c478bd9Sstevel@tonic-gate 			error = KCF_PROV_MAC_VERIFY_DECRYPT_ATOMIC(pd,
13427c478bd9Sstevel@tonic-gate 			    pd->pd_sid, &lmac_mech, mac_key, &ldecr_mech,
13437c478bd9Sstevel@tonic-gate 			    decr_key, ct, mac, pt, spi_mac_tmpl, spi_decr_tmpl,
13447c478bd9Sstevel@tonic-gate 			    KCF_SWFP_RHNDL(crq));
13457c478bd9Sstevel@tonic-gate 		else
13467c478bd9Sstevel@tonic-gate 			error = KCF_PROV_MAC_DECRYPT_ATOMIC(pd, pd->pd_sid,
13477c478bd9Sstevel@tonic-gate 			    &lmac_mech, mac_key, &ldecr_mech, decr_key,
13487c478bd9Sstevel@tonic-gate 			    ct, mac, pt, spi_mac_tmpl, spi_decr_tmpl,
13497c478bd9Sstevel@tonic-gate 			    KCF_SWFP_RHNDL(crq));
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate 		KCF_PROV_INCRSTATS(pd, error);
13527c478bd9Sstevel@tonic-gate 	} else {
13537c478bd9Sstevel@tonic-gate 		KCF_WRAP_MAC_DECRYPT_OPS_PARAMS(&params,
13547c478bd9Sstevel@tonic-gate 		    (do_verify) ? KCF_OP_MAC_VERIFY_DECRYPT_ATOMIC :
13557c478bd9Sstevel@tonic-gate 		    KCF_OP_ATOMIC, pd->pd_sid, mac_key, decr_key, ct, mac, pt,
13567c478bd9Sstevel@tonic-gate 		    spi_mac_tmpl, spi_decr_tmpl);
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate 		cmops = &(params.rp_u.mac_decrypt_params);
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate 		/* careful! structs assignments */
13617c478bd9Sstevel@tonic-gate 		cmops->md_decr_mech = *decr_mech;
13627c478bd9Sstevel@tonic-gate 		cmops->md_decr_mech.cm_type = prov_decr_mechid;
13637c478bd9Sstevel@tonic-gate 		cmops->md_framework_decr_mechtype = decr_mech->cm_type;
13647c478bd9Sstevel@tonic-gate 		cmops->md_mac_mech = *mac_mech;
13657c478bd9Sstevel@tonic-gate 		cmops->md_mac_mech.cm_type = prov_mac_mechid;
13667c478bd9Sstevel@tonic-gate 		cmops->md_framework_mac_mechtype = mac_mech->cm_type;
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate 		error = kcf_submit_request(pd, NULL, crq, &params, B_FALSE);
13697c478bd9Sstevel@tonic-gate 	}
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate 	if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED &&
13727c478bd9Sstevel@tonic-gate 	    IS_RECOVERABLE(error)) {
13737c478bd9Sstevel@tonic-gate 		/* Add pd to the linked list of providers tried. */
13747c478bd9Sstevel@tonic-gate 		if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL)
13757c478bd9Sstevel@tonic-gate 			goto retry;
13767c478bd9Sstevel@tonic-gate 	}
13777c478bd9Sstevel@tonic-gate 
13787c478bd9Sstevel@tonic-gate 	if (list != NULL)
13797c478bd9Sstevel@tonic-gate 		kcf_free_triedlist(list);
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate 	if (next_req != NULL)
13827c478bd9Sstevel@tonic-gate 		kmem_free(next_req, sizeof (kcf_dual_req_t));
13837c478bd9Sstevel@tonic-gate 	KCF_PROV_REFRELE(pd);
13847c478bd9Sstevel@tonic-gate 	return (error);
13857c478bd9Sstevel@tonic-gate }
13867c478bd9Sstevel@tonic-gate 
1387894b2776Smcpowers static int
crypto_mac_decrypt_common_prov(crypto_provider_t provider,crypto_session_id_t sid,crypto_mechanism_t * mac_mech,crypto_mechanism_t * decr_mech,crypto_dual_data_t * ct,crypto_key_t * mac_key,crypto_key_t * decr_key,crypto_ctx_template_t mac_tmpl,crypto_ctx_template_t decr_tmpl,crypto_data_t * mac,crypto_data_t * pt,crypto_call_req_t * crq,boolean_t do_verify)1388894b2776Smcpowers crypto_mac_decrypt_common_prov(crypto_provider_t provider,
1389894b2776Smcpowers     crypto_session_id_t sid, crypto_mechanism_t *mac_mech,
1390894b2776Smcpowers     crypto_mechanism_t *decr_mech, crypto_dual_data_t *ct,
1391894b2776Smcpowers     crypto_key_t *mac_key, crypto_key_t *decr_key,
1392894b2776Smcpowers     crypto_ctx_template_t mac_tmpl, crypto_ctx_template_t decr_tmpl,
1393894b2776Smcpowers     crypto_data_t *mac, crypto_data_t *pt, crypto_call_req_t *crq,
1394894b2776Smcpowers     boolean_t do_verify)
1395894b2776Smcpowers {
1396894b2776Smcpowers 	/*
1397894b2776Smcpowers 	 * First try to find a provider for the decryption mechanism, that
1398894b2776Smcpowers 	 * is also capable of the MAC mechanism.
1399894b2776Smcpowers 	 * We still favor optimizing the costlier decryption.
1400894b2776Smcpowers 	 */
1401894b2776Smcpowers 	int error;
1402894b2776Smcpowers 	kcf_mech_entry_t *me;
1403894b2776Smcpowers 	kcf_provider_desc_t *pd = provider;
1404894b2776Smcpowers 	kcf_provider_desc_t *real_provider = pd;
1405894b2776Smcpowers 	kcf_ctx_template_t *ctx_decr_tmpl, *ctx_mac_tmpl;
1406894b2776Smcpowers 	kcf_req_params_t params;
1407894b2776Smcpowers 	kcf_mac_decrypt_ops_params_t *cmops;
1408894b2776Smcpowers 	crypto_spi_ctx_template_t spi_decr_tmpl = NULL, spi_mac_tmpl = NULL;
1409894b2776Smcpowers 
1410894b2776Smcpowers 	ASSERT(KCF_PROV_REFHELD(pd));
1411894b2776Smcpowers 
1412894b2776Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
1413436935a1SVladimir Kotal 		error = kcf_get_hardware_provider(decr_mech->cm_type, decr_key,
14149b009fc1SValerie Bubb Fenwick 		    mac_mech->cm_type, mac_key, pd, &real_provider,
14159b009fc1SValerie Bubb Fenwick 		    CRYPTO_FG_MAC_DECRYPT_ATOMIC);
1416894b2776Smcpowers 
1417894b2776Smcpowers 		if (error != CRYPTO_SUCCESS)
1418894b2776Smcpowers 			return (error);
1419894b2776Smcpowers 	}
1420894b2776Smcpowers 
1421894b2776Smcpowers 	/*
1422894b2776Smcpowers 	 * For SW providers, check the validity of the context template
1423894b2776Smcpowers 	 * It is very rare that the generation number mis-matches, so
1424894b2776Smcpowers 	 * is acceptable to fail here, and let the consumer recover by
1425894b2776Smcpowers 	 * freeing this tmpl and create a new one for the key and new SW
1426894b2776Smcpowers 	 * provider
1427894b2776Smcpowers 	 */
1428894b2776Smcpowers 
1429894b2776Smcpowers 	if (real_provider->pd_prov_type == CRYPTO_SW_PROVIDER) {
1430894b2776Smcpowers 		if (decr_tmpl != NULL) {
1431894b2776Smcpowers 			if (kcf_get_mech_entry(decr_mech->cm_type, &me) !=
1432894b2776Smcpowers 			    KCF_SUCCESS) {
1433894b2776Smcpowers 				error = CRYPTO_MECHANISM_INVALID;
1434894b2776Smcpowers 				goto out;
1435894b2776Smcpowers 			}
1436894b2776Smcpowers 			ctx_decr_tmpl = (kcf_ctx_template_t *)decr_tmpl;
1437894b2776Smcpowers 			if (ctx_decr_tmpl->ct_generation != me->me_gen_swprov) {
1438894b2776Smcpowers 				error = CRYPTO_OLD_CTX_TEMPLATE;
1439894b2776Smcpowers 				goto out;
1440894b2776Smcpowers 			}
1441894b2776Smcpowers 			spi_decr_tmpl = ctx_decr_tmpl->ct_prov_tmpl;
1442894b2776Smcpowers 		}
1443894b2776Smcpowers 
1444894b2776Smcpowers 		if (mac_tmpl != NULL) {
1445894b2776Smcpowers 			if (kcf_get_mech_entry(mac_mech->cm_type, &me) !=
1446894b2776Smcpowers 			    KCF_SUCCESS) {
1447894b2776Smcpowers 				error = CRYPTO_MECHANISM_INVALID;
1448894b2776Smcpowers 				goto out;
1449894b2776Smcpowers 			}
1450894b2776Smcpowers 			ctx_mac_tmpl = (kcf_ctx_template_t *)mac_tmpl;
1451894b2776Smcpowers 			if (ctx_mac_tmpl->ct_generation != me->me_gen_swprov) {
1452894b2776Smcpowers 				error = CRYPTO_OLD_CTX_TEMPLATE;
1453894b2776Smcpowers 				goto out;
1454894b2776Smcpowers 			}
1455894b2776Smcpowers 			spi_mac_tmpl = ctx_mac_tmpl->ct_prov_tmpl;
1456894b2776Smcpowers 		}
1457894b2776Smcpowers 	}
1458894b2776Smcpowers 
1459894b2776Smcpowers 	/* The fast path for SW providers. */
1460894b2776Smcpowers 	if (CHECK_FASTPATH(crq, pd)) {
1461894b2776Smcpowers 		crypto_mechanism_t lmac_mech;
1462894b2776Smcpowers 		crypto_mechanism_t ldecr_mech;
1463894b2776Smcpowers 
1464894b2776Smcpowers 		/* careful! structs assignments */
1465894b2776Smcpowers 		ldecr_mech = *decr_mech;
1466894b2776Smcpowers 		KCF_SET_PROVIDER_MECHNUM(decr_mech->cm_type, real_provider,
1467894b2776Smcpowers 		    &ldecr_mech);
1468894b2776Smcpowers 
1469894b2776Smcpowers 		lmac_mech = *mac_mech;
1470894b2776Smcpowers 		KCF_SET_PROVIDER_MECHNUM(mac_mech->cm_type, real_provider,
1471894b2776Smcpowers 		    &lmac_mech);
1472894b2776Smcpowers 
1473894b2776Smcpowers 		if (do_verify)
1474894b2776Smcpowers 			error = KCF_PROV_MAC_VERIFY_DECRYPT_ATOMIC(
1475894b2776Smcpowers 			    real_provider, sid, &lmac_mech, mac_key,
1476894b2776Smcpowers 			    &ldecr_mech, decr_key, ct, mac, pt, spi_mac_tmpl,
1477894b2776Smcpowers 			    spi_decr_tmpl, KCF_SWFP_RHNDL(crq));
1478894b2776Smcpowers 		else
1479894b2776Smcpowers 			error = KCF_PROV_MAC_DECRYPT_ATOMIC(real_provider, sid,
1480894b2776Smcpowers 			    &lmac_mech, mac_key, &ldecr_mech, decr_key,
1481894b2776Smcpowers 			    ct, mac, pt, spi_mac_tmpl, spi_decr_tmpl,
1482894b2776Smcpowers 			    KCF_SWFP_RHNDL(crq));
1483894b2776Smcpowers 
1484894b2776Smcpowers 		KCF_PROV_INCRSTATS(pd, error);
1485894b2776Smcpowers 	} else {
1486894b2776Smcpowers 		KCF_WRAP_MAC_DECRYPT_OPS_PARAMS(&params,
1487894b2776Smcpowers 		    (do_verify) ? KCF_OP_MAC_VERIFY_DECRYPT_ATOMIC :
1488894b2776Smcpowers 		    KCF_OP_ATOMIC, sid, mac_key, decr_key, ct, mac, pt,
1489894b2776Smcpowers 		    spi_mac_tmpl, spi_decr_tmpl);
1490894b2776Smcpowers 
1491894b2776Smcpowers 		cmops = &(params.rp_u.mac_decrypt_params);
1492894b2776Smcpowers 
1493894b2776Smcpowers 		/* careful! structs assignments */
1494894b2776Smcpowers 		cmops->md_decr_mech = *decr_mech;
1495894b2776Smcpowers 		KCF_SET_PROVIDER_MECHNUM(decr_mech->cm_type, real_provider,
1496894b2776Smcpowers 		    &cmops->md_decr_mech);
1497894b2776Smcpowers 		cmops->md_framework_decr_mechtype = decr_mech->cm_type;
1498894b2776Smcpowers 
1499894b2776Smcpowers 		cmops->md_mac_mech = *mac_mech;
1500894b2776Smcpowers 		KCF_SET_PROVIDER_MECHNUM(mac_mech->cm_type, real_provider,
1501894b2776Smcpowers 		    &cmops->md_mac_mech);
1502894b2776Smcpowers 		cmops->md_framework_mac_mechtype = mac_mech->cm_type;
1503894b2776Smcpowers 
1504894b2776Smcpowers 		error = kcf_submit_request(real_provider, NULL, crq, &params,
1505894b2776Smcpowers 		    B_FALSE);
1506894b2776Smcpowers 	}
1507894b2776Smcpowers 
1508894b2776Smcpowers out:
1509894b2776Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
1510894b2776Smcpowers 		KCF_PROV_REFRELE(real_provider);
1511894b2776Smcpowers 	return (error);
1512894b2776Smcpowers }
1513894b2776Smcpowers 
15147c478bd9Sstevel@tonic-gate /*
15157c478bd9Sstevel@tonic-gate  * Starts a multi-part dual mac/decrypt operation. The provider to
15167c478bd9Sstevel@tonic-gate  * use is determined by the KCF dispatcher.
15177c478bd9Sstevel@tonic-gate  */
15187c478bd9Sstevel@tonic-gate /* ARGSUSED */
15197c478bd9Sstevel@tonic-gate int
crypto_mac_decrypt_init(crypto_mechanism_t * mac_mech,crypto_mechanism_t * decr_mech,crypto_key_t * mac_key,crypto_key_t * decr_key,crypto_ctx_template_t mac_tmpl,crypto_ctx_template_t decr_tmpl,crypto_context_t * ctxp,crypto_call_req_t * cr)15207c478bd9Sstevel@tonic-gate crypto_mac_decrypt_init(crypto_mechanism_t *mac_mech,
15217c478bd9Sstevel@tonic-gate     crypto_mechanism_t *decr_mech, crypto_key_t *mac_key,
15227c478bd9Sstevel@tonic-gate     crypto_key_t *decr_key, crypto_ctx_template_t mac_tmpl,
15237c478bd9Sstevel@tonic-gate     crypto_ctx_template_t decr_tmpl, crypto_context_t *ctxp,
15247c478bd9Sstevel@tonic-gate     crypto_call_req_t *cr)
15257c478bd9Sstevel@tonic-gate {
15267c478bd9Sstevel@tonic-gate 	/*
15277c478bd9Sstevel@tonic-gate 	 * First try to find a provider for the decryption mechanism, that
15287c478bd9Sstevel@tonic-gate 	 * is also capable of the MAC mechanism.
15297c478bd9Sstevel@tonic-gate 	 * We still favor optimizing the costlier decryption.
15307c478bd9Sstevel@tonic-gate 	 */
15317c478bd9Sstevel@tonic-gate 	int error;
15327c478bd9Sstevel@tonic-gate 	kcf_mech_entry_t *me;
15337c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
15347c478bd9Sstevel@tonic-gate 	kcf_ctx_template_t *ctx_decr_tmpl, *ctx_mac_tmpl;
15357c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
15367c478bd9Sstevel@tonic-gate 	kcf_mac_decrypt_ops_params_t *mdops;
15377c478bd9Sstevel@tonic-gate 	crypto_spi_ctx_template_t spi_decr_tmpl = NULL, spi_mac_tmpl = NULL;
15387c478bd9Sstevel@tonic-gate 	crypto_mech_type_t prov_decr_mechid, prov_mac_mechid;
15397c478bd9Sstevel@tonic-gate 	kcf_prov_tried_t *list = NULL;
15407c478bd9Sstevel@tonic-gate 	boolean_t decr_tmpl_checked = B_FALSE;
15417c478bd9Sstevel@tonic-gate 	boolean_t mac_tmpl_checked = B_FALSE;
15427c478bd9Sstevel@tonic-gate 	crypto_ctx_t *ctx = NULL;
15437c478bd9Sstevel@tonic-gate 	kcf_context_t *decr_kcf_context = NULL, *mac_kcf_context = NULL;
15447c478bd9Sstevel@tonic-gate 	crypto_call_flag_t save_flag;
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate retry:
15477c478bd9Sstevel@tonic-gate 	/* pd is returned held on success */
1548436935a1SVladimir Kotal 	pd = kcf_get_dual_provider(decr_mech, decr_key, mac_mech, mac_key,
1549436935a1SVladimir Kotal 	    &me, &prov_decr_mechid,
15507c478bd9Sstevel@tonic-gate 	    &prov_mac_mechid, &error, list,
15519b009fc1SValerie Bubb Fenwick 	    CRYPTO_FG_DECRYPT | CRYPTO_FG_MAC_DECRYPT, CRYPTO_FG_MAC, 0);
15527c478bd9Sstevel@tonic-gate 	if (pd == NULL) {
15537c478bd9Sstevel@tonic-gate 		if (list != NULL)
15547c478bd9Sstevel@tonic-gate 			kcf_free_triedlist(list);
15557c478bd9Sstevel@tonic-gate 		return (error);
15567c478bd9Sstevel@tonic-gate 	}
15577c478bd9Sstevel@tonic-gate 
15587c478bd9Sstevel@tonic-gate 	/*
15597c478bd9Sstevel@tonic-gate 	 * For SW providers, check the validity of the context template
15607c478bd9Sstevel@tonic-gate 	 * It is very rare that the generation number mis-matches, so
15617c478bd9Sstevel@tonic-gate 	 * is acceptable to fail here, and let the consumer recover by
15627c478bd9Sstevel@tonic-gate 	 * freeing this tmpl and create a new one for the key and new SW
15637c478bd9Sstevel@tonic-gate 	 * provider
15647c478bd9Sstevel@tonic-gate 	 * Warning! will need to change when multiple software providers
15657c478bd9Sstevel@tonic-gate 	 * per mechanism are supported.
15667c478bd9Sstevel@tonic-gate 	 */
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate 	if ((!decr_tmpl_checked) && (pd->pd_prov_type == CRYPTO_SW_PROVIDER)) {
15697c478bd9Sstevel@tonic-gate 		if (decr_tmpl != NULL) {
15707c478bd9Sstevel@tonic-gate 			ctx_decr_tmpl = (kcf_ctx_template_t *)decr_tmpl;
15717c478bd9Sstevel@tonic-gate 			if (ctx_decr_tmpl->ct_generation != me->me_gen_swprov) {
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 				if (list != NULL)
15747c478bd9Sstevel@tonic-gate 					kcf_free_triedlist(list);
15757c478bd9Sstevel@tonic-gate 				if (decr_kcf_context != NULL)
15767c478bd9Sstevel@tonic-gate 					KCF_CONTEXT_REFRELE(decr_kcf_context);
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate 				KCF_PROV_REFRELE(pd);
15797c478bd9Sstevel@tonic-gate 				/* Which one is the the old one ? */
15807c478bd9Sstevel@tonic-gate 				return (CRYPTO_OLD_CTX_TEMPLATE);
15817c478bd9Sstevel@tonic-gate 			}
15827c478bd9Sstevel@tonic-gate 			spi_decr_tmpl = ctx_decr_tmpl->ct_prov_tmpl;
15837c478bd9Sstevel@tonic-gate 		}
15847c478bd9Sstevel@tonic-gate 		decr_tmpl_checked = B_TRUE;
15857c478bd9Sstevel@tonic-gate 	}
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 	if (prov_mac_mechid == CRYPTO_MECH_INVALID) {
15887c478bd9Sstevel@tonic-gate 		/* Need to emulate with 2 internal calls */
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate 		/*
15917c478bd9Sstevel@tonic-gate 		 * We avoid code complexity by limiting the pure async.
15927c478bd9Sstevel@tonic-gate 		 * case to be done using only a SW provider.
15937c478bd9Sstevel@tonic-gate 		 * XXX - Redo the emulation code below so that we can
15947c478bd9Sstevel@tonic-gate 		 * remove this limitation.
15957c478bd9Sstevel@tonic-gate 		 */
15967c478bd9Sstevel@tonic-gate 		if (cr != NULL && pd->pd_prov_type == CRYPTO_HW_PROVIDER) {
15977c478bd9Sstevel@tonic-gate 			if ((kcf_insert_triedlist(&list, pd, KCF_KMFLAG(cr))
15987c478bd9Sstevel@tonic-gate 			    != NULL))
15997c478bd9Sstevel@tonic-gate 				goto retry;
16007c478bd9Sstevel@tonic-gate 			if (list != NULL)
16017c478bd9Sstevel@tonic-gate 				kcf_free_triedlist(list);
16027c478bd9Sstevel@tonic-gate 			if (decr_kcf_context != NULL)
16037c478bd9Sstevel@tonic-gate 				KCF_CONTEXT_REFRELE(decr_kcf_context);
16047c478bd9Sstevel@tonic-gate 			KCF_PROV_REFRELE(pd);
16057c478bd9Sstevel@tonic-gate 			return (CRYPTO_HOST_MEMORY);
16067c478bd9Sstevel@tonic-gate 		}
16077c478bd9Sstevel@tonic-gate 
16087c478bd9Sstevel@tonic-gate 		if (ctx == NULL && pd->pd_prov_type == CRYPTO_SW_PROVIDER) {
16097c478bd9Sstevel@tonic-gate 			ctx = kcf_new_ctx(cr, pd, pd->pd_sid);
16107c478bd9Sstevel@tonic-gate 			if (ctx == NULL) {
16117c478bd9Sstevel@tonic-gate 				if (list != NULL)
16127c478bd9Sstevel@tonic-gate 					kcf_free_triedlist(list);
16137c478bd9Sstevel@tonic-gate 				if (decr_kcf_context != NULL)
16147c478bd9Sstevel@tonic-gate 					KCF_CONTEXT_REFRELE(decr_kcf_context);
16157c478bd9Sstevel@tonic-gate 				KCF_PROV_REFRELE(pd);
16167c478bd9Sstevel@tonic-gate 				return (CRYPTO_HOST_MEMORY);
16177c478bd9Sstevel@tonic-gate 			}
16187c478bd9Sstevel@tonic-gate 			decr_kcf_context = (kcf_context_t *)
16197c478bd9Sstevel@tonic-gate 			    ctx->cc_framework_private;
16207c478bd9Sstevel@tonic-gate 		}
16217c478bd9Sstevel@tonic-gate 		/*
16227c478bd9Sstevel@tonic-gate 		 * Trade-off speed vs avoidance of code complexity and
16237c478bd9Sstevel@tonic-gate 		 * duplication:
16247c478bd9Sstevel@tonic-gate 		 * Could do all the combinations of fastpath / synch / asynch
16257c478bd9Sstevel@tonic-gate 		 * for the decryption and the mac steps. Early attempts
16267c478bd9Sstevel@tonic-gate 		 * showed the code grew wild and bug-prone, for little gain.
16277c478bd9Sstevel@tonic-gate 		 * Therefore, the adaptative asynch case is not implemented.
16287c478bd9Sstevel@tonic-gate 		 * It's either pure synchronous, or pure asynchronous.
16297c478bd9Sstevel@tonic-gate 		 * We still preserve a fastpath for the pure synchronous
16307c478bd9Sstevel@tonic-gate 		 * requests to SW providers.
16317c478bd9Sstevel@tonic-gate 		 */
16327c478bd9Sstevel@tonic-gate 		if (cr == NULL) {
16337c478bd9Sstevel@tonic-gate 			crypto_context_t mac_context;
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate 			error = crypto_mac_init(mac_mech, mac_key, mac_tmpl,
16367c478bd9Sstevel@tonic-gate 			    &mac_context, NULL);
16377c478bd9Sstevel@tonic-gate 
16387c478bd9Sstevel@tonic-gate 			if (error != CRYPTO_SUCCESS) {
16397c478bd9Sstevel@tonic-gate 				/* Can't be CRYPTO_QUEUED. return the failure */
16407c478bd9Sstevel@tonic-gate 				if (list != NULL)
16417c478bd9Sstevel@tonic-gate 					kcf_free_triedlist(list);
16427c478bd9Sstevel@tonic-gate 
16437c478bd9Sstevel@tonic-gate 				if (decr_kcf_context != NULL)
16447c478bd9Sstevel@tonic-gate 					KCF_CONTEXT_REFRELE(decr_kcf_context);
16457c478bd9Sstevel@tonic-gate 				return (error);
16467c478bd9Sstevel@tonic-gate 			}
16477c478bd9Sstevel@tonic-gate 			if (pd->pd_prov_type == CRYPTO_SW_PROVIDER) {
16487c478bd9Sstevel@tonic-gate 				crypto_mechanism_t lmech = *decr_mech;
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate 				lmech.cm_type = prov_decr_mechid;
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate 				error = KCF_PROV_DECRYPT_INIT(pd, ctx, &lmech,
16537c478bd9Sstevel@tonic-gate 				    decr_key, spi_decr_tmpl,
16547c478bd9Sstevel@tonic-gate 				    KCF_RHNDL(KM_SLEEP));
16557c478bd9Sstevel@tonic-gate 			} else {
16567c478bd9Sstevel@tonic-gate 				/*
16577c478bd9Sstevel@tonic-gate 				 * If we did the 'goto retry' then ctx may not
16587c478bd9Sstevel@tonic-gate 				 * be NULL.  In general, we can't reuse another
16597c478bd9Sstevel@tonic-gate 				 * provider's context, so we free it now so
16607c478bd9Sstevel@tonic-gate 				 * we don't leak it.
16617c478bd9Sstevel@tonic-gate 				 */
16627c478bd9Sstevel@tonic-gate 				if (ctx != NULL) {
16637c478bd9Sstevel@tonic-gate 					KCF_CONTEXT_REFRELE((kcf_context_t *)
16647c478bd9Sstevel@tonic-gate 					    ctx->cc_framework_private);
16657c478bd9Sstevel@tonic-gate 					decr_kcf_context = NULL;
16667c478bd9Sstevel@tonic-gate 				}
16677c478bd9Sstevel@tonic-gate 				error = crypto_decrypt_init_prov(pd, pd->pd_sid,
16687c478bd9Sstevel@tonic-gate 				    decr_mech, decr_key, &decr_tmpl,
16697c478bd9Sstevel@tonic-gate 				    (crypto_context_t *)&ctx, NULL);
16707c478bd9Sstevel@tonic-gate 
16717c478bd9Sstevel@tonic-gate 				if (error == CRYPTO_SUCCESS) {
16727c478bd9Sstevel@tonic-gate 					decr_kcf_context = (kcf_context_t *)
16737c478bd9Sstevel@tonic-gate 					    ctx->cc_framework_private;
16747c478bd9Sstevel@tonic-gate 				}
16757c478bd9Sstevel@tonic-gate 			}
16767c478bd9Sstevel@tonic-gate 
16777c478bd9Sstevel@tonic-gate 			KCF_PROV_INCRSTATS(pd, error);
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate 			KCF_PROV_REFRELE(pd);
16807c478bd9Sstevel@tonic-gate 
16817c478bd9Sstevel@tonic-gate 			if (error != CRYPTO_SUCCESS) {
16827c478bd9Sstevel@tonic-gate 				/* Can't be CRYPTO_QUEUED. return the failure */
16837c478bd9Sstevel@tonic-gate 				if (list != NULL)
16847c478bd9Sstevel@tonic-gate 					kcf_free_triedlist(list);
16857c478bd9Sstevel@tonic-gate 				if (mac_kcf_context != NULL)
16867c478bd9Sstevel@tonic-gate 					KCF_CONTEXT_REFRELE(mac_kcf_context);
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate 				return (error);
16897c478bd9Sstevel@tonic-gate 			}
16907c478bd9Sstevel@tonic-gate 			mac_kcf_context = (kcf_context_t *)
16917c478bd9Sstevel@tonic-gate 			    ((crypto_ctx_t *)mac_context)->
16927c478bd9Sstevel@tonic-gate 			    cc_framework_private;
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate 			decr_kcf_context = (kcf_context_t *)
16957c478bd9Sstevel@tonic-gate 			    ctx->cc_framework_private;
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate 			/*
16987c478bd9Sstevel@tonic-gate 			 * Here also, the mac context is second. The callback
16997c478bd9Sstevel@tonic-gate 			 * case can't overwrite the context returned to
17007c478bd9Sstevel@tonic-gate 			 * the caller.
17017c478bd9Sstevel@tonic-gate 			 */
17027c478bd9Sstevel@tonic-gate 			decr_kcf_context->kc_secondctx = mac_kcf_context;
17037c478bd9Sstevel@tonic-gate 			KCF_CONTEXT_REFHOLD(mac_kcf_context);
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate 			*ctxp = (crypto_context_t)ctx;
17067c478bd9Sstevel@tonic-gate 
17077c478bd9Sstevel@tonic-gate 			return (error);
17087c478bd9Sstevel@tonic-gate 		}
17097c478bd9Sstevel@tonic-gate 		/* submit a pure asynchronous request. */
17107c478bd9Sstevel@tonic-gate 		save_flag = cr->cr_flag;
17117c478bd9Sstevel@tonic-gate 		cr->cr_flag |= CRYPTO_ALWAYS_QUEUE;
17127c478bd9Sstevel@tonic-gate 
17137c478bd9Sstevel@tonic-gate 		KCF_WRAP_MAC_DECRYPT_OPS_PARAMS(&params, KCF_OP_INIT,
17147c478bd9Sstevel@tonic-gate 		    pd->pd_sid, mac_key, decr_key, NULL, NULL, NULL,
17157c478bd9Sstevel@tonic-gate 		    spi_mac_tmpl, spi_decr_tmpl);
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate 		mdops = &(params.rp_u.mac_decrypt_params);
17187c478bd9Sstevel@tonic-gate 
17197c478bd9Sstevel@tonic-gate 		/* careful! structs assignments */
17207c478bd9Sstevel@tonic-gate 		mdops->md_decr_mech = *decr_mech;
17217c478bd9Sstevel@tonic-gate 		/*
17227c478bd9Sstevel@tonic-gate 		 * mdops->md_decr_mech.cm_type will be set when we get to
17237c478bd9Sstevel@tonic-gate 		 * kcf_emulate_dual() routine.
17247c478bd9Sstevel@tonic-gate 		 */
17257c478bd9Sstevel@tonic-gate 		mdops->md_framework_decr_mechtype = decr_mech->cm_type;
17267c478bd9Sstevel@tonic-gate 		mdops->md_mac_mech = *mac_mech;
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate 		/*
17297c478bd9Sstevel@tonic-gate 		 * mdops->md_mac_mech.cm_type will be set when we know the
17307c478bd9Sstevel@tonic-gate 		 * MAC provider.
17317c478bd9Sstevel@tonic-gate 		 */
17327c478bd9Sstevel@tonic-gate 		mdops->md_framework_mac_mechtype = mac_mech->cm_type;
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate 		/*
17357c478bd9Sstevel@tonic-gate 		 * non-NULL ctx->kc_secondctx tells common_submit_request
17367c478bd9Sstevel@tonic-gate 		 * that this request uses separate cipher and MAC contexts.
17377c478bd9Sstevel@tonic-gate 		 * That function will set the MAC context's kc_secondctx to
17387c478bd9Sstevel@tonic-gate 		 * this decrypt context.
17397c478bd9Sstevel@tonic-gate 		 */
17407c478bd9Sstevel@tonic-gate 		decr_kcf_context->kc_secondctx = decr_kcf_context;
17417c478bd9Sstevel@tonic-gate 
17427c478bd9Sstevel@tonic-gate 		error = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 		cr->cr_flag = save_flag;
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate 		if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED) {
17477c478bd9Sstevel@tonic-gate 			KCF_CONTEXT_REFRELE(decr_kcf_context);
17487c478bd9Sstevel@tonic-gate 		}
17497c478bd9Sstevel@tonic-gate 		if (list != NULL)
17507c478bd9Sstevel@tonic-gate 			kcf_free_triedlist(list);
17517c478bd9Sstevel@tonic-gate 		*ctxp =  ctx;
17527c478bd9Sstevel@tonic-gate 		KCF_PROV_REFRELE(pd);
17537c478bd9Sstevel@tonic-gate 		return (error);
17547c478bd9Sstevel@tonic-gate 	}
17557c478bd9Sstevel@tonic-gate 
17567c478bd9Sstevel@tonic-gate 	if ((!mac_tmpl_checked) && (pd->pd_prov_type == CRYPTO_SW_PROVIDER)) {
17577c478bd9Sstevel@tonic-gate 		if ((mac_tmpl != NULL) &&
17587c478bd9Sstevel@tonic-gate 		    (prov_mac_mechid != CRYPTO_MECH_INVALID)) {
17597c478bd9Sstevel@tonic-gate 			ctx_mac_tmpl = (kcf_ctx_template_t *)mac_tmpl;
17607c478bd9Sstevel@tonic-gate 			if (ctx_mac_tmpl->ct_generation != me->me_gen_swprov) {
17617c478bd9Sstevel@tonic-gate 
17627c478bd9Sstevel@tonic-gate 				if (list != NULL)
17637c478bd9Sstevel@tonic-gate 					kcf_free_triedlist(list);
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate 				KCF_PROV_REFRELE(pd);
17667c478bd9Sstevel@tonic-gate 				/* Which one is the the old one ? */
17677c478bd9Sstevel@tonic-gate 				return (CRYPTO_OLD_CTX_TEMPLATE);
17687c478bd9Sstevel@tonic-gate 			}
17697c478bd9Sstevel@tonic-gate 			spi_mac_tmpl = ctx_mac_tmpl->ct_prov_tmpl;
17707c478bd9Sstevel@tonic-gate 		}
17717c478bd9Sstevel@tonic-gate 		mac_tmpl_checked = B_TRUE;
17727c478bd9Sstevel@tonic-gate 	}
17737c478bd9Sstevel@tonic-gate 
17747c478bd9Sstevel@tonic-gate 	if (ctx == NULL) {
17757c478bd9Sstevel@tonic-gate 		ctx = kcf_new_ctx(cr, pd, pd->pd_sid);
17767c478bd9Sstevel@tonic-gate 		if (ctx == NULL) {
17777c478bd9Sstevel@tonic-gate 			error = CRYPTO_HOST_MEMORY;
17787c478bd9Sstevel@tonic-gate 			if (list != NULL)
17797c478bd9Sstevel@tonic-gate 				kcf_free_triedlist(list);
17807c478bd9Sstevel@tonic-gate 			return (CRYPTO_HOST_MEMORY);
17817c478bd9Sstevel@tonic-gate 		}
17827c478bd9Sstevel@tonic-gate 		decr_kcf_context = (kcf_context_t *)ctx->cc_framework_private;
17837c478bd9Sstevel@tonic-gate 	}
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate 	/* The fast path for SW providers. */
17867c478bd9Sstevel@tonic-gate 	if (CHECK_FASTPATH(cr, pd)) {
17877c478bd9Sstevel@tonic-gate 		crypto_mechanism_t ldecr_mech;
17887c478bd9Sstevel@tonic-gate 		crypto_mechanism_t lmac_mech;
17897c478bd9Sstevel@tonic-gate 
17907c478bd9Sstevel@tonic-gate 		/* careful! structs assignments */
17917c478bd9Sstevel@tonic-gate 		ldecr_mech = *decr_mech;
17927c478bd9Sstevel@tonic-gate 		ldecr_mech.cm_type = prov_decr_mechid;
17937c478bd9Sstevel@tonic-gate 		lmac_mech = *mac_mech;
17947c478bd9Sstevel@tonic-gate 		lmac_mech.cm_type = prov_mac_mechid;
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate 		error = KCF_PROV_MAC_DECRYPT_INIT(pd, ctx, &lmac_mech,
17977c478bd9Sstevel@tonic-gate 		    mac_key, &ldecr_mech, decr_key, spi_mac_tmpl, spi_decr_tmpl,
17987c478bd9Sstevel@tonic-gate 		    KCF_SWFP_RHNDL(cr));
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate 		KCF_PROV_INCRSTATS(pd, error);
18017c478bd9Sstevel@tonic-gate 	} else {
18027c478bd9Sstevel@tonic-gate 		KCF_WRAP_MAC_DECRYPT_OPS_PARAMS(&params, KCF_OP_INIT,
18037c478bd9Sstevel@tonic-gate 		    pd->pd_sid, mac_key, decr_key, NULL, NULL, NULL,
18047c478bd9Sstevel@tonic-gate 		    spi_mac_tmpl, spi_decr_tmpl);
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate 		mdops = &(params.rp_u.mac_decrypt_params);
18077c478bd9Sstevel@tonic-gate 
18087c478bd9Sstevel@tonic-gate 		/* careful! structs assignments */
18097c478bd9Sstevel@tonic-gate 		mdops->md_decr_mech = *decr_mech;
18107c478bd9Sstevel@tonic-gate 		mdops->md_decr_mech.cm_type = prov_decr_mechid;
18117c478bd9Sstevel@tonic-gate 		mdops->md_framework_decr_mechtype = decr_mech->cm_type;
18127c478bd9Sstevel@tonic-gate 		mdops->md_mac_mech = *mac_mech;
18137c478bd9Sstevel@tonic-gate 		mdops->md_mac_mech.cm_type = prov_mac_mechid;
18147c478bd9Sstevel@tonic-gate 		mdops->md_framework_mac_mechtype = mac_mech->cm_type;
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate 		error = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
18177c478bd9Sstevel@tonic-gate 	}
18187c478bd9Sstevel@tonic-gate 
18197c478bd9Sstevel@tonic-gate 	if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED) {
18207c478bd9Sstevel@tonic-gate 		if ((IS_RECOVERABLE(error)) &&
18217c478bd9Sstevel@tonic-gate 		    (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(cr)) != NULL))
18227c478bd9Sstevel@tonic-gate 			goto retry;
18237c478bd9Sstevel@tonic-gate 
18247c478bd9Sstevel@tonic-gate 		KCF_CONTEXT_REFRELE(decr_kcf_context);
18257c478bd9Sstevel@tonic-gate 	} else
18267c478bd9Sstevel@tonic-gate 		*ctxp = (crypto_context_t)ctx;
18277c478bd9Sstevel@tonic-gate 
18287c478bd9Sstevel@tonic-gate 	if (list != NULL)
18297c478bd9Sstevel@tonic-gate 		kcf_free_triedlist(list);
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate 	KCF_PROV_REFRELE(pd);
18327c478bd9Sstevel@tonic-gate 	return (error);
18337c478bd9Sstevel@tonic-gate }
18347c478bd9Sstevel@tonic-gate 
1835894b2776Smcpowers int
crypto_mac_decrypt_init_prov(crypto_provider_t provider,crypto_session_id_t sid,crypto_mechanism_t * mac_mech,crypto_mechanism_t * decr_mech,crypto_key_t * mac_key,crypto_key_t * decr_key,crypto_ctx_template_t mac_tmpl,crypto_ctx_template_t decr_tmpl,crypto_context_t * ctxp,crypto_call_req_t * cr)1836894b2776Smcpowers crypto_mac_decrypt_init_prov(crypto_provider_t provider,
1837894b2776Smcpowers     crypto_session_id_t sid, crypto_mechanism_t *mac_mech,
1838894b2776Smcpowers     crypto_mechanism_t *decr_mech, crypto_key_t *mac_key,
1839894b2776Smcpowers     crypto_key_t *decr_key, crypto_ctx_template_t mac_tmpl,
1840894b2776Smcpowers     crypto_ctx_template_t decr_tmpl, crypto_context_t *ctxp,
1841894b2776Smcpowers     crypto_call_req_t *cr)
1842894b2776Smcpowers {
1843894b2776Smcpowers 	/*
1844894b2776Smcpowers 	 * First try to find a provider for the decryption mechanism, that
1845894b2776Smcpowers 	 * is also capable of the MAC mechanism.
1846894b2776Smcpowers 	 * We still favor optimizing the costlier decryption.
1847894b2776Smcpowers 	 */
1848894b2776Smcpowers 	int rv;
1849894b2776Smcpowers 	kcf_mech_entry_t *me;
1850894b2776Smcpowers 	kcf_provider_desc_t *pd = provider;
1851894b2776Smcpowers 	kcf_provider_desc_t *real_provider = pd;
1852894b2776Smcpowers 	kcf_ctx_template_t *ctx_decr_tmpl, *ctx_mac_tmpl;
1853894b2776Smcpowers 	kcf_req_params_t params;
1854894b2776Smcpowers 	kcf_mac_decrypt_ops_params_t *mdops;
1855894b2776Smcpowers 	crypto_spi_ctx_template_t spi_decr_tmpl = NULL, spi_mac_tmpl = NULL;
1856894b2776Smcpowers 	crypto_ctx_t *ctx;
1857894b2776Smcpowers 	kcf_context_t *decr_kcf_context = NULL;
1858894b2776Smcpowers 
1859894b2776Smcpowers 	ASSERT(KCF_PROV_REFHELD(pd));
1860894b2776Smcpowers 
1861894b2776Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
1862436935a1SVladimir Kotal 		rv = kcf_get_hardware_provider(decr_mech->cm_type, decr_key,
18639b009fc1SValerie Bubb Fenwick 		    mac_mech->cm_type, mac_key, pd, &real_provider,
18649b009fc1SValerie Bubb Fenwick 		    CRYPTO_FG_MAC_DECRYPT);
1865894b2776Smcpowers 
1866894b2776Smcpowers 		if (rv != CRYPTO_SUCCESS)
1867894b2776Smcpowers 			return (rv);
1868894b2776Smcpowers 	}
1869894b2776Smcpowers 
1870894b2776Smcpowers 	/*
1871894b2776Smcpowers 	 * For SW providers, check the validity of the context template
1872894b2776Smcpowers 	 * It is very rare that the generation number mis-matches, so
1873894b2776Smcpowers 	 * is acceptable to fail here, and let the consumer recover by
1874894b2776Smcpowers 	 * freeing this tmpl and create a new one for the key and new SW
1875894b2776Smcpowers 	 * provider
1876894b2776Smcpowers 	 * Warning! will need to change when multiple software providers
1877894b2776Smcpowers 	 * per mechanism are supported.
1878894b2776Smcpowers 	 */
1879894b2776Smcpowers 
1880894b2776Smcpowers 	if (real_provider->pd_prov_type == CRYPTO_SW_PROVIDER) {
1881894b2776Smcpowers 		if (decr_tmpl != NULL) {
1882894b2776Smcpowers 			if (kcf_get_mech_entry(decr_mech->cm_type, &me) !=
1883894b2776Smcpowers 			    KCF_SUCCESS) {
1884894b2776Smcpowers 				rv = CRYPTO_MECHANISM_INVALID;
1885894b2776Smcpowers 				goto out;
1886894b2776Smcpowers 			}
1887894b2776Smcpowers 			ctx_decr_tmpl = (kcf_ctx_template_t *)decr_tmpl;
1888894b2776Smcpowers 			if (ctx_decr_tmpl->ct_generation != me->me_gen_swprov) {
1889894b2776Smcpowers 				rv = CRYPTO_OLD_CTX_TEMPLATE;
1890894b2776Smcpowers 				goto out;
1891894b2776Smcpowers 			}
1892894b2776Smcpowers 			spi_decr_tmpl = ctx_decr_tmpl->ct_prov_tmpl;
1893894b2776Smcpowers 		}
1894894b2776Smcpowers 
1895894b2776Smcpowers 		if (mac_tmpl != NULL) {
1896894b2776Smcpowers 			if (kcf_get_mech_entry(mac_mech->cm_type, &me) !=
1897894b2776Smcpowers 			    KCF_SUCCESS) {
1898894b2776Smcpowers 				rv = CRYPTO_MECHANISM_INVALID;
1899894b2776Smcpowers 				goto out;
1900894b2776Smcpowers 			}
1901894b2776Smcpowers 			ctx_mac_tmpl = (kcf_ctx_template_t *)mac_tmpl;
1902894b2776Smcpowers 			if (ctx_mac_tmpl->ct_generation != me->me_gen_swprov) {
1903894b2776Smcpowers 				rv = CRYPTO_OLD_CTX_TEMPLATE;
1904894b2776Smcpowers 				goto out;
1905894b2776Smcpowers 			}
1906894b2776Smcpowers 			spi_mac_tmpl = ctx_mac_tmpl->ct_prov_tmpl;
1907894b2776Smcpowers 		}
1908894b2776Smcpowers 	}
1909894b2776Smcpowers 
1910894b2776Smcpowers 	ctx = kcf_new_ctx(cr, real_provider, sid);
1911894b2776Smcpowers 	if (ctx == NULL) {
1912894b2776Smcpowers 		rv = CRYPTO_HOST_MEMORY;
1913894b2776Smcpowers 		goto out;
1914894b2776Smcpowers 	}
1915894b2776Smcpowers 	decr_kcf_context = (kcf_context_t *)ctx->cc_framework_private;
1916894b2776Smcpowers 
1917894b2776Smcpowers 	/* The fast path for SW providers. */
1918894b2776Smcpowers 	if (CHECK_FASTPATH(cr, pd)) {
1919894b2776Smcpowers 		crypto_mechanism_t ldecr_mech;
1920894b2776Smcpowers 		crypto_mechanism_t lmac_mech;
1921894b2776Smcpowers 
1922894b2776Smcpowers 		/* careful! structs assignments */
1923894b2776Smcpowers 		ldecr_mech = *decr_mech;
1924894b2776Smcpowers 		KCF_SET_PROVIDER_MECHNUM(decr_mech->cm_type, real_provider,
1925894b2776Smcpowers 		    &ldecr_mech);
1926894b2776Smcpowers 
1927894b2776Smcpowers 		lmac_mech = *mac_mech;
1928894b2776Smcpowers 		KCF_SET_PROVIDER_MECHNUM(mac_mech->cm_type, real_provider,
1929894b2776Smcpowers 		    &lmac_mech);
1930894b2776Smcpowers 
1931894b2776Smcpowers 		rv = KCF_PROV_MAC_DECRYPT_INIT(real_provider, ctx, &lmac_mech,
1932894b2776Smcpowers 		    mac_key, &ldecr_mech, decr_key, spi_mac_tmpl, spi_decr_tmpl,
1933894b2776Smcpowers 		    KCF_SWFP_RHNDL(cr));
1934894b2776Smcpowers 
1935894b2776Smcpowers 		KCF_PROV_INCRSTATS(pd, rv);
1936894b2776Smcpowers 	} else {
1937894b2776Smcpowers 		KCF_WRAP_MAC_DECRYPT_OPS_PARAMS(&params, KCF_OP_INIT,
1938894b2776Smcpowers 		    sid, mac_key, decr_key, NULL, NULL, NULL,
1939894b2776Smcpowers 		    spi_mac_tmpl, spi_decr_tmpl);
1940894b2776Smcpowers 
1941894b2776Smcpowers 		mdops = &(params.rp_u.mac_decrypt_params);
1942894b2776Smcpowers 
1943894b2776Smcpowers 		/* careful! structs assignments */
1944894b2776Smcpowers 		mdops->md_decr_mech = *decr_mech;
1945894b2776Smcpowers 		KCF_SET_PROVIDER_MECHNUM(decr_mech->cm_type, real_provider,
1946894b2776Smcpowers 		    &mdops->md_decr_mech);
1947894b2776Smcpowers 		mdops->md_framework_decr_mechtype = decr_mech->cm_type;
1948894b2776Smcpowers 
1949894b2776Smcpowers 		mdops->md_mac_mech = *mac_mech;
1950894b2776Smcpowers 		KCF_SET_PROVIDER_MECHNUM(mac_mech->cm_type, real_provider,
1951894b2776Smcpowers 		    &mdops->md_mac_mech);
1952894b2776Smcpowers 		mdops->md_framework_mac_mechtype = mac_mech->cm_type;
1953894b2776Smcpowers 
1954894b2776Smcpowers 		rv = kcf_submit_request(real_provider, ctx, cr, &params,
1955894b2776Smcpowers 		    B_FALSE);
1956894b2776Smcpowers 	}
1957894b2776Smcpowers 
1958894b2776Smcpowers 	if (rv != CRYPTO_SUCCESS && rv != CRYPTO_QUEUED) {
1959894b2776Smcpowers 		KCF_CONTEXT_REFRELE(decr_kcf_context);
1960894b2776Smcpowers 	} else
1961894b2776Smcpowers 		*ctxp = (crypto_context_t)ctx;
1962894b2776Smcpowers 
1963894b2776Smcpowers out:
1964894b2776Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
1965894b2776Smcpowers 		KCF_PROV_REFRELE(real_provider);
1966894b2776Smcpowers 	return (rv);
1967894b2776Smcpowers }
19687c478bd9Sstevel@tonic-gate /*
19697c478bd9Sstevel@tonic-gate  * Continues a multi-part dual mac/decrypt operation.
19707c478bd9Sstevel@tonic-gate  */
19717c478bd9Sstevel@tonic-gate /* ARGSUSED */
19727c478bd9Sstevel@tonic-gate int
crypto_mac_decrypt_update(crypto_context_t context,crypto_dual_data_t * ct,crypto_data_t * pt,crypto_call_req_t * cr)19737c478bd9Sstevel@tonic-gate crypto_mac_decrypt_update(crypto_context_t context,
19747c478bd9Sstevel@tonic-gate     crypto_dual_data_t *ct, crypto_data_t *pt, crypto_call_req_t *cr)
19757c478bd9Sstevel@tonic-gate {
19767c478bd9Sstevel@tonic-gate 	crypto_ctx_t *ctx = (crypto_ctx_t *)context, *mac_ctx;
19777c478bd9Sstevel@tonic-gate 	kcf_context_t *kcf_ctx, *kcf_mac_ctx;
19787c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
19797c478bd9Sstevel@tonic-gate 	int error;
19807c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate 	if ((ctx == NULL) ||
19837c478bd9Sstevel@tonic-gate 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
19847c478bd9Sstevel@tonic-gate 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
19857c478bd9Sstevel@tonic-gate 		return (CRYPTO_INVALID_CONTEXT);
19867c478bd9Sstevel@tonic-gate 	}
19877c478bd9Sstevel@tonic-gate 
1988894b2776Smcpowers 	ASSERT(pd->pd_prov_type != CRYPTO_LOGICAL_PROVIDER);
19897c478bd9Sstevel@tonic-gate 
19907c478bd9Sstevel@tonic-gate 	if ((kcf_mac_ctx = kcf_ctx->kc_secondctx) != NULL) {
19917c478bd9Sstevel@tonic-gate 		off_t save_offset;
19927c478bd9Sstevel@tonic-gate 		size_t save_len;
19937c478bd9Sstevel@tonic-gate 		crypto_call_flag_t save_flag;
19947c478bd9Sstevel@tonic-gate 
19957c478bd9Sstevel@tonic-gate 		if (kcf_mac_ctx->kc_prov_desc == NULL) {
19967c478bd9Sstevel@tonic-gate 			error = CRYPTO_INVALID_CONTEXT;
19977c478bd9Sstevel@tonic-gate 			goto out;
19987c478bd9Sstevel@tonic-gate 		}
19997c478bd9Sstevel@tonic-gate 		mac_ctx = &kcf_mac_ctx->kc_glbl_ctx;
20007c478bd9Sstevel@tonic-gate 
20017c478bd9Sstevel@tonic-gate 		/* First we submit the MAC request */
20027c478bd9Sstevel@tonic-gate 		if (cr == NULL) {
20037c478bd9Sstevel@tonic-gate 			/*
20047c478bd9Sstevel@tonic-gate 			 * 'ct' is always not NULL.
20057c478bd9Sstevel@tonic-gate 			 */
20067c478bd9Sstevel@tonic-gate 			error = crypto_mac_update((crypto_context_t)mac_ctx,
20077c478bd9Sstevel@tonic-gate 			    (crypto_data_t *)ct, NULL);
20087c478bd9Sstevel@tonic-gate 
20097c478bd9Sstevel@tonic-gate 			if (error != CRYPTO_SUCCESS)
20107c478bd9Sstevel@tonic-gate 				goto out;
20117c478bd9Sstevel@tonic-gate 
20127c478bd9Sstevel@tonic-gate 			/* Decrypt a different length only when told so */
20137c478bd9Sstevel@tonic-gate 
20147c478bd9Sstevel@tonic-gate 			save_offset = ct->dd_offset1;
20157c478bd9Sstevel@tonic-gate 			save_len = ct->dd_len1;
20167c478bd9Sstevel@tonic-gate 
20177c478bd9Sstevel@tonic-gate 			if (ct->dd_len2 > 0) {
20187c478bd9Sstevel@tonic-gate 				ct->dd_offset1 = ct->dd_offset2;
20197c478bd9Sstevel@tonic-gate 				ct->dd_len1 = ct->dd_len2;
20207c478bd9Sstevel@tonic-gate 			}
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate 			error = crypto_decrypt_update(context,
20237c478bd9Sstevel@tonic-gate 			    (crypto_data_t *)ct, pt, NULL);
20247c478bd9Sstevel@tonic-gate 
20257c478bd9Sstevel@tonic-gate 			ct->dd_offset1 = save_offset;
20267c478bd9Sstevel@tonic-gate 			ct->dd_len1 = save_len;
20277c478bd9Sstevel@tonic-gate 
20287c478bd9Sstevel@tonic-gate 			goto out;
20297c478bd9Sstevel@tonic-gate 		}
20307c478bd9Sstevel@tonic-gate 		/* submit a pure asynchronous request. */
20317c478bd9Sstevel@tonic-gate 		save_flag = cr->cr_flag;
20327c478bd9Sstevel@tonic-gate 		cr->cr_flag |= CRYPTO_ALWAYS_QUEUE;
20337c478bd9Sstevel@tonic-gate 
20347c478bd9Sstevel@tonic-gate 		KCF_WRAP_MAC_DECRYPT_OPS_PARAMS(&params, KCF_OP_UPDATE,
20357c478bd9Sstevel@tonic-gate 		    pd->pd_sid, NULL, NULL, ct, NULL, pt, NULL, NULL)
20367c478bd9Sstevel@tonic-gate 
20377c478bd9Sstevel@tonic-gate 
20387c478bd9Sstevel@tonic-gate 		error = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
20397c478bd9Sstevel@tonic-gate 
20407c478bd9Sstevel@tonic-gate 		cr->cr_flag = save_flag;
20417c478bd9Sstevel@tonic-gate 		goto out;
20427c478bd9Sstevel@tonic-gate 	}
20437c478bd9Sstevel@tonic-gate 
20447c478bd9Sstevel@tonic-gate 	/* The fast path for SW providers. */
20457c478bd9Sstevel@tonic-gate 	if (CHECK_FASTPATH(cr, pd)) {
20467c478bd9Sstevel@tonic-gate 		error = KCF_PROV_MAC_DECRYPT_UPDATE(pd, ctx, ct, pt, NULL);
20477c478bd9Sstevel@tonic-gate 		KCF_PROV_INCRSTATS(pd, error);
20487c478bd9Sstevel@tonic-gate 	} else {
20497c478bd9Sstevel@tonic-gate 		KCF_WRAP_MAC_DECRYPT_OPS_PARAMS(&params, KCF_OP_UPDATE,
2050894b2776Smcpowers 		    ctx->cc_session, NULL, NULL, ct, NULL, pt, NULL, NULL);
20517c478bd9Sstevel@tonic-gate 
20527c478bd9Sstevel@tonic-gate 		error = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
20537c478bd9Sstevel@tonic-gate 	}
20547c478bd9Sstevel@tonic-gate out:
20557c478bd9Sstevel@tonic-gate 	return (error);
20567c478bd9Sstevel@tonic-gate }
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate /*
20597c478bd9Sstevel@tonic-gate  * Terminates a multi-part dual mac/decrypt operation.
20607c478bd9Sstevel@tonic-gate  */
20617c478bd9Sstevel@tonic-gate /* ARGSUSED */
20627c478bd9Sstevel@tonic-gate int
crypto_mac_decrypt_final(crypto_context_t context,crypto_data_t * mac,crypto_data_t * pt,crypto_call_req_t * cr)20637c478bd9Sstevel@tonic-gate crypto_mac_decrypt_final(crypto_context_t context, crypto_data_t *mac,
20647c478bd9Sstevel@tonic-gate     crypto_data_t *pt, crypto_call_req_t *cr)
20657c478bd9Sstevel@tonic-gate {
20667c478bd9Sstevel@tonic-gate 	crypto_ctx_t *ctx = (crypto_ctx_t *)context, *mac_ctx;
20677c478bd9Sstevel@tonic-gate 	kcf_context_t *kcf_ctx, *kcf_mac_ctx;
20687c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
20697c478bd9Sstevel@tonic-gate 	int error;
20707c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
20717c478bd9Sstevel@tonic-gate 
20727c478bd9Sstevel@tonic-gate 	if ((ctx == NULL) ||
20737c478bd9Sstevel@tonic-gate 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
20747c478bd9Sstevel@tonic-gate 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
20757c478bd9Sstevel@tonic-gate 		return (CRYPTO_INVALID_CONTEXT);
20767c478bd9Sstevel@tonic-gate 	}
20777c478bd9Sstevel@tonic-gate 
2078894b2776Smcpowers 	ASSERT(pd->pd_prov_type != CRYPTO_LOGICAL_PROVIDER);
20797c478bd9Sstevel@tonic-gate 
20807c478bd9Sstevel@tonic-gate 	if ((kcf_mac_ctx = kcf_ctx->kc_secondctx) != NULL) {
20817c478bd9Sstevel@tonic-gate 		crypto_call_flag_t save_flag;
20827c478bd9Sstevel@tonic-gate 
20837c478bd9Sstevel@tonic-gate 		if (kcf_mac_ctx->kc_prov_desc == NULL) {
20847c478bd9Sstevel@tonic-gate 			error = CRYPTO_INVALID_CONTEXT;
20857c478bd9Sstevel@tonic-gate 			goto out;
20867c478bd9Sstevel@tonic-gate 		}
20877c478bd9Sstevel@tonic-gate 		mac_ctx = &kcf_mac_ctx->kc_glbl_ctx;
20887c478bd9Sstevel@tonic-gate 
20897c478bd9Sstevel@tonic-gate 		/* First we collect the MAC */
20907c478bd9Sstevel@tonic-gate 		if (cr == NULL) {
20917c478bd9Sstevel@tonic-gate 
20927c478bd9Sstevel@tonic-gate 			error = crypto_mac_final((crypto_context_t)mac_ctx,
20937c478bd9Sstevel@tonic-gate 			    mac, NULL);
20947c478bd9Sstevel@tonic-gate 
20957c478bd9Sstevel@tonic-gate 			if (error != CRYPTO_SUCCESS) {
20967c478bd9Sstevel@tonic-gate 				crypto_cancel_ctx(ctx);
20977c478bd9Sstevel@tonic-gate 			} else {
20987c478bd9Sstevel@tonic-gate 				/* Get the last chunk of plaintext */
20997c478bd9Sstevel@tonic-gate 				error = crypto_decrypt_final(context, pt, NULL);
21007c478bd9Sstevel@tonic-gate 			}
21017c478bd9Sstevel@tonic-gate 
21027c478bd9Sstevel@tonic-gate 			return (error);
21037c478bd9Sstevel@tonic-gate 		}
21047c478bd9Sstevel@tonic-gate 		/* submit a pure asynchronous request. */
21057c478bd9Sstevel@tonic-gate 		save_flag = cr->cr_flag;
21067c478bd9Sstevel@tonic-gate 		cr->cr_flag |= CRYPTO_ALWAYS_QUEUE;
21077c478bd9Sstevel@tonic-gate 
21087c478bd9Sstevel@tonic-gate 		KCF_WRAP_MAC_DECRYPT_OPS_PARAMS(&params, KCF_OP_FINAL,
21097c478bd9Sstevel@tonic-gate 		    pd->pd_sid, NULL, NULL, NULL, mac, pt, NULL, NULL)
21107c478bd9Sstevel@tonic-gate 
21117c478bd9Sstevel@tonic-gate 
21127c478bd9Sstevel@tonic-gate 		error = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
21137c478bd9Sstevel@tonic-gate 
21147c478bd9Sstevel@tonic-gate 		cr->cr_flag = save_flag;
21157c478bd9Sstevel@tonic-gate 
21167c478bd9Sstevel@tonic-gate 		return (error);
21177c478bd9Sstevel@tonic-gate 	}
21187c478bd9Sstevel@tonic-gate 
21197c478bd9Sstevel@tonic-gate 	/* The fast path for SW providers. */
21207c478bd9Sstevel@tonic-gate 	if (CHECK_FASTPATH(cr, pd)) {
21217c478bd9Sstevel@tonic-gate 		error = KCF_PROV_MAC_DECRYPT_FINAL(pd, ctx, mac, pt, NULL);
21227c478bd9Sstevel@tonic-gate 		KCF_PROV_INCRSTATS(pd, error);
21237c478bd9Sstevel@tonic-gate 	} else {
21247c478bd9Sstevel@tonic-gate 		KCF_WRAP_MAC_DECRYPT_OPS_PARAMS(&params, KCF_OP_FINAL,
2125894b2776Smcpowers 		    ctx->cc_session, NULL, NULL, NULL, mac, pt, NULL, NULL);
21267c478bd9Sstevel@tonic-gate 
21277c478bd9Sstevel@tonic-gate 		error = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
21287c478bd9Sstevel@tonic-gate 	}
21297c478bd9Sstevel@tonic-gate out:
21307c478bd9Sstevel@tonic-gate 	/* Release the hold done in kcf_new_ctx() during init step. */
21317c478bd9Sstevel@tonic-gate 	KCF_CONTEXT_COND_RELEASE(error, kcf_ctx);
21327c478bd9Sstevel@tonic-gate 	return (error);
21337c478bd9Sstevel@tonic-gate }
21347c478bd9Sstevel@tonic-gate 
21357c478bd9Sstevel@tonic-gate /*
21367c478bd9Sstevel@tonic-gate  * Digest/Encrypt dual operation. Project-private entry point, not part of
21377c478bd9Sstevel@tonic-gate  * the k-API.
21387c478bd9Sstevel@tonic-gate  */
21397c478bd9Sstevel@tonic-gate /* ARGSUSED */
21407c478bd9Sstevel@tonic-gate int
crypto_digest_encrypt_update(crypto_context_t digest_ctx,crypto_context_t encrypt_ctx,crypto_data_t * plaintext,crypto_data_t * ciphertext,crypto_call_req_t * crq)21417c478bd9Sstevel@tonic-gate crypto_digest_encrypt_update(crypto_context_t digest_ctx,
21427c478bd9Sstevel@tonic-gate     crypto_context_t encrypt_ctx, crypto_data_t *plaintext,
21437c478bd9Sstevel@tonic-gate     crypto_data_t *ciphertext, crypto_call_req_t *crq)
21447c478bd9Sstevel@tonic-gate {
21457c478bd9Sstevel@tonic-gate 	/*
21467c478bd9Sstevel@tonic-gate 	 * RFE 4688647:
21477c478bd9Sstevel@tonic-gate 	 * core functions needed by ioctl interface missing from impl.h
21487c478bd9Sstevel@tonic-gate 	 */
21497c478bd9Sstevel@tonic-gate 	return (CRYPTO_NOT_SUPPORTED);
21507c478bd9Sstevel@tonic-gate }
21517c478bd9Sstevel@tonic-gate 
21527c478bd9Sstevel@tonic-gate /*
21537c478bd9Sstevel@tonic-gate  * Decrypt/Digest dual operation. Project-private entry point, not part of
21547c478bd9Sstevel@tonic-gate  * the k-API.
21557c478bd9Sstevel@tonic-gate  */
21567c478bd9Sstevel@tonic-gate /* ARGSUSED */
21577c478bd9Sstevel@tonic-gate int
crypto_decrypt_digest_update(crypto_context_t decryptctx,crypto_context_t encrypt_ctx,crypto_data_t * ciphertext,crypto_data_t * plaintext,crypto_call_req_t * crq)21587c478bd9Sstevel@tonic-gate crypto_decrypt_digest_update(crypto_context_t decryptctx,
21597c478bd9Sstevel@tonic-gate     crypto_context_t encrypt_ctx, crypto_data_t *ciphertext,
21607c478bd9Sstevel@tonic-gate     crypto_data_t *plaintext, crypto_call_req_t *crq)
21617c478bd9Sstevel@tonic-gate {
21627c478bd9Sstevel@tonic-gate 	/*
21637c478bd9Sstevel@tonic-gate 	 * RFE 4688647:
21647c478bd9Sstevel@tonic-gate 	 * core functions needed by ioctl interface missing from impl.h
21657c478bd9Sstevel@tonic-gate 	 */
21667c478bd9Sstevel@tonic-gate 	return (CRYPTO_NOT_SUPPORTED);
21677c478bd9Sstevel@tonic-gate }
21687c478bd9Sstevel@tonic-gate 
21697c478bd9Sstevel@tonic-gate /*
21707c478bd9Sstevel@tonic-gate  * Sign/Encrypt dual operation. Project-private entry point, not part of
21717c478bd9Sstevel@tonic-gate  * the k-API.
21727c478bd9Sstevel@tonic-gate  */
21737c478bd9Sstevel@tonic-gate /* ARGSUSED */
21747c478bd9Sstevel@tonic-gate int
crypto_sign_encrypt_update(crypto_context_t sign_ctx,crypto_context_t encrypt_ctx,crypto_data_t * plaintext,crypto_data_t * ciphertext,crypto_call_req_t * crq)21757c478bd9Sstevel@tonic-gate crypto_sign_encrypt_update(crypto_context_t sign_ctx,
21767c478bd9Sstevel@tonic-gate     crypto_context_t encrypt_ctx, crypto_data_t *plaintext,
21777c478bd9Sstevel@tonic-gate     crypto_data_t *ciphertext, crypto_call_req_t *crq)
21787c478bd9Sstevel@tonic-gate {
21797c478bd9Sstevel@tonic-gate 	/*
21807c478bd9Sstevel@tonic-gate 	 * RFE 4688647:
21817c478bd9Sstevel@tonic-gate 	 * core functions needed by ioctl interface missing from impl.h
21827c478bd9Sstevel@tonic-gate 	 */
21837c478bd9Sstevel@tonic-gate 	return (CRYPTO_NOT_SUPPORTED);
21847c478bd9Sstevel@tonic-gate }
21857c478bd9Sstevel@tonic-gate 
21867c478bd9Sstevel@tonic-gate /*
21877c478bd9Sstevel@tonic-gate  * Decrypt/Verify dual operation. Project-private entry point, not part of
21887c478bd9Sstevel@tonic-gate  * the k-API.
21897c478bd9Sstevel@tonic-gate  */
21907c478bd9Sstevel@tonic-gate /* ARGSUSED */
21917c478bd9Sstevel@tonic-gate int
crypto_decrypt_verify_update(crypto_context_t decrypt_ctx,crypto_context_t verify_ctx,crypto_data_t * ciphertext,crypto_data_t * plaintext,crypto_call_req_t * crq)21927c478bd9Sstevel@tonic-gate crypto_decrypt_verify_update(crypto_context_t decrypt_ctx,
21937c478bd9Sstevel@tonic-gate     crypto_context_t verify_ctx, crypto_data_t *ciphertext,
21947c478bd9Sstevel@tonic-gate     crypto_data_t *plaintext, crypto_call_req_t *crq)
21957c478bd9Sstevel@tonic-gate {
21967c478bd9Sstevel@tonic-gate 	/*
21977c478bd9Sstevel@tonic-gate 	 * RFE 4688647:
21987c478bd9Sstevel@tonic-gate 	 * core functions needed by ioctl interface missing from impl.h
21997c478bd9Sstevel@tonic-gate 	 */
22007c478bd9Sstevel@tonic-gate 	return (CRYPTO_NOT_SUPPORTED);
22017c478bd9Sstevel@tonic-gate }
2202