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 /*
22*9b009fc1SValerie 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_VERIFY_OFFSET(f)		offsetof(crypto_verify_ops_t, f)
37894b2776Smcpowers 
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate  * Verify entry points.
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  * See comments for crypto_digest_init_prov().
447c478bd9Sstevel@tonic-gate  */
457c478bd9Sstevel@tonic-gate int
crypto_verify_init_prov(crypto_provider_t provider,crypto_session_id_t sid,crypto_mechanism_t * mech,crypto_key_t * key,crypto_ctx_template_t tmpl,crypto_context_t * ctxp,crypto_call_req_t * crq)46894b2776Smcpowers crypto_verify_init_prov(crypto_provider_t provider, crypto_session_id_t sid,
477c478bd9Sstevel@tonic-gate     crypto_mechanism_t *mech, crypto_key_t *key, crypto_ctx_template_t tmpl,
487c478bd9Sstevel@tonic-gate     crypto_context_t *ctxp, crypto_call_req_t *crq)
497c478bd9Sstevel@tonic-gate {
50894b2776Smcpowers 	int rv;
517c478bd9Sstevel@tonic-gate 	crypto_ctx_t *ctx;
527c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
53894b2776Smcpowers 	kcf_provider_desc_t *pd = provider;
54894b2776Smcpowers 	kcf_provider_desc_t *real_provider = pd;
55894b2776Smcpowers 
56894b2776Smcpowers 	ASSERT(KCF_PROV_REFHELD(pd));
57894b2776Smcpowers 
58894b2776Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
59436935a1SVladimir Kotal 		rv = kcf_get_hardware_provider(mech->cm_type, key,
60*9b009fc1SValerie Bubb Fenwick 		    CRYPTO_MECH_INVALID, NULL, pd, &real_provider,
61*9b009fc1SValerie Bubb Fenwick 		    CRYPTO_FG_VERIFY);
627c478bd9Sstevel@tonic-gate 
63894b2776Smcpowers 		if (rv != CRYPTO_SUCCESS)
64894b2776Smcpowers 			return (rv);
65894b2776Smcpowers 	}
66894b2776Smcpowers 
67894b2776Smcpowers 	/* Allocate and initialize the canonical context */
68894b2776Smcpowers 	if ((ctx = kcf_new_ctx(crq, real_provider, sid)) == NULL) {
69894b2776Smcpowers 		if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
70894b2776Smcpowers 			KCF_PROV_REFRELE(real_provider);
717c478bd9Sstevel@tonic-gate 		return (CRYPTO_HOST_MEMORY);
72894b2776Smcpowers 	}
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	KCF_WRAP_VERIFY_OPS_PARAMS(&params, KCF_OP_INIT, sid, mech,
757c478bd9Sstevel@tonic-gate 	    key, NULL, NULL, tmpl);
76894b2776Smcpowers 	rv = kcf_submit_request(real_provider, ctx, crq, &params, B_FALSE);
77894b2776Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
78894b2776Smcpowers 		KCF_PROV_REFRELE(real_provider);
797c478bd9Sstevel@tonic-gate 
80894b2776Smcpowers 	if ((rv == CRYPTO_SUCCESS) || (rv == CRYPTO_QUEUED))
817c478bd9Sstevel@tonic-gate 		*ctxp = (crypto_context_t)ctx;
827c478bd9Sstevel@tonic-gate 	else {
837c478bd9Sstevel@tonic-gate 		/* Release the hold done in kcf_new_ctx(). */
847c478bd9Sstevel@tonic-gate 		KCF_CONTEXT_REFRELE((kcf_context_t *)ctx->cc_framework_private);
857c478bd9Sstevel@tonic-gate 	}
867c478bd9Sstevel@tonic-gate 
87894b2776Smcpowers 	return (rv);
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate int
crypto_verify_init(crypto_mechanism_t * mech,crypto_key_t * key,crypto_ctx_template_t tmpl,crypto_context_t * ctxp,crypto_call_req_t * crq)927c478bd9Sstevel@tonic-gate crypto_verify_init(crypto_mechanism_t *mech, crypto_key_t *key,
937c478bd9Sstevel@tonic-gate     crypto_ctx_template_t tmpl, crypto_context_t *ctxp, crypto_call_req_t *crq)
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate 	int error;
967c478bd9Sstevel@tonic-gate 	kcf_mech_entry_t *me;
977c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
987c478bd9Sstevel@tonic-gate 	kcf_prov_tried_t *list = NULL;
997c478bd9Sstevel@tonic-gate 	kcf_ctx_template_t *ctx_tmpl;
1007c478bd9Sstevel@tonic-gate 	crypto_spi_ctx_template_t spi_ctx_tmpl = NULL;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate retry:
1037c478bd9Sstevel@tonic-gate 	/* The pd is returned held */
104436935a1SVladimir Kotal 	if ((pd = kcf_get_mech_provider(mech->cm_type, key, &me, &error,
105*9b009fc1SValerie Bubb Fenwick 	    list, CRYPTO_FG_VERIFY, 0)) == NULL) {
1067c478bd9Sstevel@tonic-gate 		if (list != NULL)
1077c478bd9Sstevel@tonic-gate 			kcf_free_triedlist(list);
1087c478bd9Sstevel@tonic-gate 		return (error);
1097c478bd9Sstevel@tonic-gate 	}
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	/*
1127c478bd9Sstevel@tonic-gate 	 * For SW providers, check the validity of the context template
1137c478bd9Sstevel@tonic-gate 	 * It is very rare that the generation number mis-matches, so
1147c478bd9Sstevel@tonic-gate 	 * it is acceptable to fail here, and let the consumer recover by
1157c478bd9Sstevel@tonic-gate 	 * freeing this tmpl and create a new one for the key and new SW
1167c478bd9Sstevel@tonic-gate 	 * provider.
1177c478bd9Sstevel@tonic-gate 	 */
1187c478bd9Sstevel@tonic-gate 	if ((pd->pd_prov_type == CRYPTO_SW_PROVIDER) &&
1197c478bd9Sstevel@tonic-gate 	    ((ctx_tmpl = (kcf_ctx_template_t *)tmpl) != NULL)) {
1207c478bd9Sstevel@tonic-gate 		if (ctx_tmpl->ct_generation != me->me_gen_swprov) {
1217c478bd9Sstevel@tonic-gate 			if (list != NULL)
1227c478bd9Sstevel@tonic-gate 				kcf_free_triedlist(list);
1237c478bd9Sstevel@tonic-gate 			KCF_PROV_REFRELE(pd);
1247c478bd9Sstevel@tonic-gate 			return (CRYPTO_OLD_CTX_TEMPLATE);
1257c478bd9Sstevel@tonic-gate 		} else {
1267c478bd9Sstevel@tonic-gate 			spi_ctx_tmpl = ctx_tmpl->ct_prov_tmpl;
1277c478bd9Sstevel@tonic-gate 		}
1287c478bd9Sstevel@tonic-gate 	}
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	error = crypto_verify_init_prov(pd, pd->pd_sid, mech, key, spi_ctx_tmpl,
1317c478bd9Sstevel@tonic-gate 	    ctxp, crq);
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED &&
1347c478bd9Sstevel@tonic-gate 	    IS_RECOVERABLE(error)) {
1357c478bd9Sstevel@tonic-gate 		/* Add pd to the linked list of providers tried. */
1367c478bd9Sstevel@tonic-gate 		if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL)
1377c478bd9Sstevel@tonic-gate 			goto retry;
1387c478bd9Sstevel@tonic-gate 	}
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	if (list != NULL)
1417c478bd9Sstevel@tonic-gate 		kcf_free_triedlist(list);
1427c478bd9Sstevel@tonic-gate 	KCF_PROV_REFRELE(pd);
1437c478bd9Sstevel@tonic-gate 	return (error);
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate int
crypto_verify_single(crypto_context_t context,crypto_data_t * data,crypto_data_t * signature,crypto_call_req_t * cr)1477c478bd9Sstevel@tonic-gate crypto_verify_single(crypto_context_t context, crypto_data_t *data,
1487c478bd9Sstevel@tonic-gate     crypto_data_t *signature, crypto_call_req_t *cr)
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate 	crypto_ctx_t *ctx = (crypto_ctx_t *)context;
1517c478bd9Sstevel@tonic-gate 	kcf_context_t *kcf_ctx;
1527c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
1537c478bd9Sstevel@tonic-gate 	int error;
1547c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	if ((ctx == NULL) ||
1577c478bd9Sstevel@tonic-gate 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
1587c478bd9Sstevel@tonic-gate 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
1597c478bd9Sstevel@tonic-gate 		return (CRYPTO_INVALID_CONTEXT);
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	KCF_WRAP_VERIFY_OPS_PARAMS(&params, KCF_OP_SINGLE, 0, NULL,
1637c478bd9Sstevel@tonic-gate 	    NULL, data, signature, NULL);
1647c478bd9Sstevel@tonic-gate 	error = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	/* Release the hold done in kcf_new_ctx() during init step. */
1677c478bd9Sstevel@tonic-gate 	KCF_CONTEXT_COND_RELEASE(error, kcf_ctx);
1687c478bd9Sstevel@tonic-gate 	return (error);
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate /*
1727c478bd9Sstevel@tonic-gate  * See comments for crypto_digest_update().
1737c478bd9Sstevel@tonic-gate  */
1747c478bd9Sstevel@tonic-gate int
crypto_verify_update(crypto_context_t context,crypto_data_t * data,crypto_call_req_t * cr)1757c478bd9Sstevel@tonic-gate crypto_verify_update(crypto_context_t context, crypto_data_t *data,
1767c478bd9Sstevel@tonic-gate     crypto_call_req_t *cr)
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate {
1797c478bd9Sstevel@tonic-gate 	crypto_ctx_t *ctx = (crypto_ctx_t *)context;
1807c478bd9Sstevel@tonic-gate 	kcf_context_t *kcf_ctx;
1817c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
1827c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
183894b2776Smcpowers 	int rv;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	if ((ctx == NULL) ||
1867c478bd9Sstevel@tonic-gate 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
1877c478bd9Sstevel@tonic-gate 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
1887c478bd9Sstevel@tonic-gate 		return (CRYPTO_INVALID_CONTEXT);
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 
191894b2776Smcpowers 	ASSERT(pd->pd_prov_type != CRYPTO_LOGICAL_PROVIDER);
192894b2776Smcpowers 	KCF_WRAP_VERIFY_OPS_PARAMS(&params, KCF_OP_UPDATE, ctx->cc_session,
193894b2776Smcpowers 	    NULL, NULL, data, NULL, NULL);
194894b2776Smcpowers 	rv = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
1957c478bd9Sstevel@tonic-gate 
196894b2776Smcpowers 	return (rv);
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate /*
2007c478bd9Sstevel@tonic-gate  * See comments for crypto_digest_final().
2017c478bd9Sstevel@tonic-gate  */
2027c478bd9Sstevel@tonic-gate int
crypto_verify_final(crypto_context_t context,crypto_data_t * signature,crypto_call_req_t * cr)2037c478bd9Sstevel@tonic-gate crypto_verify_final(crypto_context_t context, crypto_data_t *signature,
2047c478bd9Sstevel@tonic-gate     crypto_call_req_t *cr)
2057c478bd9Sstevel@tonic-gate {
2067c478bd9Sstevel@tonic-gate 	crypto_ctx_t *ctx = (crypto_ctx_t *)context;
2077c478bd9Sstevel@tonic-gate 	kcf_context_t *kcf_ctx;
2087c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
2097c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
210894b2776Smcpowers 	int rv;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	if ((ctx == NULL) ||
2137c478bd9Sstevel@tonic-gate 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
2147c478bd9Sstevel@tonic-gate 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
2157c478bd9Sstevel@tonic-gate 		return (CRYPTO_INVALID_CONTEXT);
2167c478bd9Sstevel@tonic-gate 	}
2177c478bd9Sstevel@tonic-gate 
218894b2776Smcpowers 	ASSERT(pd->pd_prov_type != CRYPTO_LOGICAL_PROVIDER);
219894b2776Smcpowers 	KCF_WRAP_VERIFY_OPS_PARAMS(&params, KCF_OP_FINAL, ctx->cc_session,
220894b2776Smcpowers 	    NULL, NULL, NULL, signature, NULL);
221894b2776Smcpowers 	rv = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	/* Release the hold done in kcf_new_ctx() during init step. */
224894b2776Smcpowers 	KCF_CONTEXT_COND_RELEASE(rv, kcf_ctx);
225894b2776Smcpowers 	return (rv);
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate int
crypto_verify_prov(crypto_provider_t provider,crypto_session_id_t sid,crypto_mechanism_t * mech,crypto_key_t * key,crypto_data_t * data,crypto_ctx_template_t tmpl,crypto_data_t * signature,crypto_call_req_t * crq)229894b2776Smcpowers crypto_verify_prov(crypto_provider_t provider, crypto_session_id_t sid,
230894b2776Smcpowers     crypto_mechanism_t *mech, crypto_key_t *key, crypto_data_t *data,
231894b2776Smcpowers     crypto_ctx_template_t tmpl, crypto_data_t *signature,
2327c478bd9Sstevel@tonic-gate     crypto_call_req_t *crq)
2337c478bd9Sstevel@tonic-gate {
2347c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
235894b2776Smcpowers 	kcf_provider_desc_t *pd = provider;
236894b2776Smcpowers 	kcf_provider_desc_t *real_provider = pd;
237894b2776Smcpowers 	int rv;
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	ASSERT(KCF_PROV_REFHELD(pd));
240894b2776Smcpowers 
241894b2776Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
242436935a1SVladimir Kotal 		rv = kcf_get_hardware_provider(mech->cm_type, key,
243*9b009fc1SValerie Bubb Fenwick 		    CRYPTO_MECH_INVALID, NULL, pd, &real_provider,
244*9b009fc1SValerie Bubb Fenwick 		    CRYPTO_FG_VERIFY_ATOMIC);
245894b2776Smcpowers 
246894b2776Smcpowers 		if (rv != CRYPTO_SUCCESS)
247894b2776Smcpowers 			return (rv);
248894b2776Smcpowers 	}
2497c478bd9Sstevel@tonic-gate 	KCF_WRAP_VERIFY_OPS_PARAMS(&params, KCF_OP_ATOMIC, sid, mech,
2507c478bd9Sstevel@tonic-gate 	    key, data, signature, tmpl);
251894b2776Smcpowers 	rv = kcf_submit_request(real_provider, NULL, crq, &params, B_FALSE);
252894b2776Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
253894b2776Smcpowers 		KCF_PROV_REFRELE(real_provider);
2547c478bd9Sstevel@tonic-gate 
255894b2776Smcpowers 	return (rv);
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate static int
verify_vr_atomic_common(crypto_mechanism_t * mech,crypto_key_t * key,crypto_data_t * data,crypto_ctx_template_t tmpl,crypto_data_t * signature,crypto_call_req_t * crq,crypto_func_group_t fg)2597c478bd9Sstevel@tonic-gate verify_vr_atomic_common(crypto_mechanism_t *mech, crypto_key_t *key,
2607c478bd9Sstevel@tonic-gate     crypto_data_t *data, crypto_ctx_template_t tmpl, crypto_data_t *signature,
2617c478bd9Sstevel@tonic-gate     crypto_call_req_t *crq, crypto_func_group_t fg)
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate 	int error;
2647c478bd9Sstevel@tonic-gate 	kcf_mech_entry_t *me;
2657c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
2667c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
2677c478bd9Sstevel@tonic-gate 	kcf_prov_tried_t *list = NULL;
2687c478bd9Sstevel@tonic-gate 	kcf_ctx_template_t *ctx_tmpl;
2697c478bd9Sstevel@tonic-gate 	crypto_spi_ctx_template_t spi_ctx_tmpl = NULL;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate retry:
2727c478bd9Sstevel@tonic-gate 	/* The pd is returned held */
273436935a1SVladimir Kotal 	if ((pd = kcf_get_mech_provider(mech->cm_type, key, &me, &error,
274*9b009fc1SValerie Bubb Fenwick 	    list, fg, data->cd_length)) == NULL) {
2757c478bd9Sstevel@tonic-gate 		if (list != NULL)
2767c478bd9Sstevel@tonic-gate 			kcf_free_triedlist(list);
2777c478bd9Sstevel@tonic-gate 		return (error);
2787c478bd9Sstevel@tonic-gate 	}
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	/*
2817c478bd9Sstevel@tonic-gate 	 * For SW providers, check the validity of the context template
2827c478bd9Sstevel@tonic-gate 	 * It is very rare that the generation number mis-matches, so
2837c478bd9Sstevel@tonic-gate 	 * it is acceptable to fail here, and let the consumer recover by
2847c478bd9Sstevel@tonic-gate 	 * freeing this tmpl and create a new one for the key and new SW
2857c478bd9Sstevel@tonic-gate 	 * provider.
2867c478bd9Sstevel@tonic-gate 	 */
2877c478bd9Sstevel@tonic-gate 	if ((pd->pd_prov_type == CRYPTO_SW_PROVIDER) &&
2887c478bd9Sstevel@tonic-gate 	    ((ctx_tmpl = (kcf_ctx_template_t *)tmpl) != NULL)) {
2897c478bd9Sstevel@tonic-gate 		if (ctx_tmpl->ct_generation != me->me_gen_swprov) {
2907c478bd9Sstevel@tonic-gate 			if (list != NULL)
2917c478bd9Sstevel@tonic-gate 				kcf_free_triedlist(list);
2927c478bd9Sstevel@tonic-gate 			KCF_PROV_REFRELE(pd);
2937c478bd9Sstevel@tonic-gate 			return (CRYPTO_OLD_CTX_TEMPLATE);
2947c478bd9Sstevel@tonic-gate 		} else {
2957c478bd9Sstevel@tonic-gate 			spi_ctx_tmpl = ctx_tmpl->ct_prov_tmpl;
2967c478bd9Sstevel@tonic-gate 		}
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	/* The fast path for SW providers. */
3007c478bd9Sstevel@tonic-gate 	if (CHECK_FASTPATH(crq, pd)) {
3017c478bd9Sstevel@tonic-gate 		crypto_mechanism_t lmech;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 		lmech = *mech;
3047c478bd9Sstevel@tonic-gate 		KCF_SET_PROVIDER_MECHNUM(mech->cm_type, pd, &lmech);
3057c478bd9Sstevel@tonic-gate 		if (fg == CRYPTO_FG_VERIFY_ATOMIC)
3067c478bd9Sstevel@tonic-gate 			error = KCF_PROV_VERIFY_ATOMIC(pd, pd->pd_sid, &lmech,
3077c478bd9Sstevel@tonic-gate 			    key, data, spi_ctx_tmpl, signature,
3087c478bd9Sstevel@tonic-gate 			    KCF_SWFP_RHNDL(crq));
3097c478bd9Sstevel@tonic-gate 		else
3107c478bd9Sstevel@tonic-gate 			/* Note: The argument order is different from above */
3117c478bd9Sstevel@tonic-gate 			error = KCF_PROV_VERIFY_RECOVER_ATOMIC(pd, pd->pd_sid,
3127c478bd9Sstevel@tonic-gate 			    &lmech, key, signature, spi_ctx_tmpl, data,
3137c478bd9Sstevel@tonic-gate 			    KCF_SWFP_RHNDL(crq));
3147c478bd9Sstevel@tonic-gate 		KCF_PROV_INCRSTATS(pd, error);
3157c478bd9Sstevel@tonic-gate 	} else {
3167c478bd9Sstevel@tonic-gate 		kcf_op_type_t op = ((fg == CRYPTO_FG_VERIFY_ATOMIC) ?
3177c478bd9Sstevel@tonic-gate 		    KCF_OP_ATOMIC : KCF_OP_VERIFY_RECOVER_ATOMIC);
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 		KCF_WRAP_VERIFY_OPS_PARAMS(&params, op, pd->pd_sid,
3207c478bd9Sstevel@tonic-gate 		    mech, key, data, signature, spi_ctx_tmpl);
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 		/* no crypto context to carry between multiple parts. */
3237c478bd9Sstevel@tonic-gate 		error = kcf_submit_request(pd, NULL, crq, &params, B_FALSE);
3247c478bd9Sstevel@tonic-gate 	}
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED &&
3277c478bd9Sstevel@tonic-gate 	    IS_RECOVERABLE(error)) {
3287c478bd9Sstevel@tonic-gate 		/* Add pd to the linked list of providers tried. */
3297c478bd9Sstevel@tonic-gate 		if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL)
3307c478bd9Sstevel@tonic-gate 			goto retry;
3317c478bd9Sstevel@tonic-gate 	}
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	if (list != NULL)
3347c478bd9Sstevel@tonic-gate 		kcf_free_triedlist(list);
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	KCF_PROV_REFRELE(pd);
3377c478bd9Sstevel@tonic-gate 	return (error);
3387c478bd9Sstevel@tonic-gate }
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate int
crypto_verify(crypto_mechanism_t * mech,crypto_key_t * key,crypto_data_t * data,crypto_ctx_template_t tmpl,crypto_data_t * signature,crypto_call_req_t * crq)3417c478bd9Sstevel@tonic-gate crypto_verify(crypto_mechanism_t *mech, crypto_key_t *key, crypto_data_t *data,
3427c478bd9Sstevel@tonic-gate     crypto_ctx_template_t tmpl, crypto_data_t *signature,
3437c478bd9Sstevel@tonic-gate     crypto_call_req_t *crq)
3447c478bd9Sstevel@tonic-gate {
3457c478bd9Sstevel@tonic-gate 	return (verify_vr_atomic_common(mech, key, data, tmpl, signature, crq,
3467c478bd9Sstevel@tonic-gate 	    CRYPTO_FG_VERIFY_ATOMIC));
3477c478bd9Sstevel@tonic-gate }
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate int
crypto_verify_recover_prov(crypto_provider_t provider,crypto_session_id_t sid,crypto_mechanism_t * mech,crypto_key_t * key,crypto_data_t * signature,crypto_ctx_template_t tmpl,crypto_data_t * data,crypto_call_req_t * crq)350894b2776Smcpowers crypto_verify_recover_prov(crypto_provider_t provider, crypto_session_id_t sid,
351894b2776Smcpowers     crypto_mechanism_t *mech, crypto_key_t *key, crypto_data_t *signature,
352894b2776Smcpowers     crypto_ctx_template_t tmpl, crypto_data_t *data, crypto_call_req_t *crq)
3537c478bd9Sstevel@tonic-gate {
3547c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
355894b2776Smcpowers 	kcf_provider_desc_t *pd = provider;
356894b2776Smcpowers 	kcf_provider_desc_t *real_provider = pd;
357894b2776Smcpowers 	int rv;
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	ASSERT(KCF_PROV_REFHELD(pd));
360894b2776Smcpowers 
361894b2776Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
362436935a1SVladimir Kotal 		rv = kcf_get_hardware_provider(mech->cm_type, key,
363*9b009fc1SValerie Bubb Fenwick 		    CRYPTO_MECH_INVALID, NULL, pd, &real_provider,
364*9b009fc1SValerie Bubb Fenwick 		    CRYPTO_FG_VERIFY_RECOVER_ATOMIC);
365894b2776Smcpowers 
366894b2776Smcpowers 		if (rv != CRYPTO_SUCCESS)
367894b2776Smcpowers 			return (rv);
368894b2776Smcpowers 	}
3697c478bd9Sstevel@tonic-gate 	KCF_WRAP_VERIFY_OPS_PARAMS(&params, KCF_OP_VERIFY_RECOVER_ATOMIC,
3707c478bd9Sstevel@tonic-gate 	    sid, mech, key, data, signature, tmpl);
371894b2776Smcpowers 	rv = kcf_submit_request(real_provider, NULL, crq, &params, B_FALSE);
372894b2776Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
373894b2776Smcpowers 		KCF_PROV_REFRELE(real_provider);
3747c478bd9Sstevel@tonic-gate 
375894b2776Smcpowers 	return (rv);
3767c478bd9Sstevel@tonic-gate }
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate int
crypto_verify_recover(crypto_mechanism_t * mech,crypto_key_t * key,crypto_data_t * signature,crypto_ctx_template_t tmpl,crypto_data_t * data,crypto_call_req_t * crq)3797c478bd9Sstevel@tonic-gate crypto_verify_recover(crypto_mechanism_t *mech, crypto_key_t *key,
3807c478bd9Sstevel@tonic-gate     crypto_data_t *signature, crypto_ctx_template_t tmpl, crypto_data_t *data,
3817c478bd9Sstevel@tonic-gate     crypto_call_req_t *crq)
3827c478bd9Sstevel@tonic-gate {
3837c478bd9Sstevel@tonic-gate 	return (verify_vr_atomic_common(mech, key, data, tmpl, signature, crq,
3847c478bd9Sstevel@tonic-gate 	    CRYPTO_FG_VERIFY_RECOVER_ATOMIC));
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate int
crypto_verify_recover_init_prov(crypto_provider_t provider,crypto_session_id_t sid,crypto_mechanism_t * mech,crypto_key_t * key,crypto_ctx_template_t tmpl,crypto_context_t * ctxp,crypto_call_req_t * crq)388894b2776Smcpowers crypto_verify_recover_init_prov(crypto_provider_t provider,
3897c478bd9Sstevel@tonic-gate     crypto_session_id_t sid, crypto_mechanism_t *mech, crypto_key_t *key,
3907c478bd9Sstevel@tonic-gate     crypto_ctx_template_t tmpl, crypto_context_t *ctxp, crypto_call_req_t *crq)
3917c478bd9Sstevel@tonic-gate {
392894b2776Smcpowers 	int rv;
3937c478bd9Sstevel@tonic-gate 	crypto_ctx_t *ctx;
3947c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
395894b2776Smcpowers 	kcf_provider_desc_t *pd = provider;
396894b2776Smcpowers 	kcf_provider_desc_t *real_provider = pd;
397894b2776Smcpowers 
398894b2776Smcpowers 	ASSERT(KCF_PROV_REFHELD(pd));
399894b2776Smcpowers 
400894b2776Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
401436935a1SVladimir Kotal 		rv = kcf_get_hardware_provider(mech->cm_type, key,
402*9b009fc1SValerie Bubb Fenwick 		    CRYPTO_MECH_INVALID, NULL, pd, &real_provider,
403*9b009fc1SValerie Bubb Fenwick 		    CRYPTO_FG_VERIFY_RECOVER);
4047c478bd9Sstevel@tonic-gate 
405894b2776Smcpowers 		if (rv != CRYPTO_SUCCESS)
406894b2776Smcpowers 			return (rv);
407894b2776Smcpowers 	}
408894b2776Smcpowers 
409894b2776Smcpowers 	/* Allocate and initialize the canonical context */
410894b2776Smcpowers 	if ((ctx = kcf_new_ctx(crq, real_provider, sid)) == NULL) {
411894b2776Smcpowers 		if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
412894b2776Smcpowers 			KCF_PROV_REFRELE(real_provider);
4137c478bd9Sstevel@tonic-gate 		return (CRYPTO_HOST_MEMORY);
414894b2776Smcpowers 	}
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	KCF_WRAP_VERIFY_OPS_PARAMS(&params, KCF_OP_VERIFY_RECOVER_INIT,
4177c478bd9Sstevel@tonic-gate 	    sid, mech, key, NULL, NULL, tmpl);
418894b2776Smcpowers 	rv = kcf_submit_request(real_provider, ctx, crq, &params, B_FALSE);
419894b2776Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
420894b2776Smcpowers 		KCF_PROV_REFRELE(real_provider);
4217c478bd9Sstevel@tonic-gate 
422894b2776Smcpowers 	if ((rv == CRYPTO_SUCCESS) || (rv == CRYPTO_QUEUED))
4237c478bd9Sstevel@tonic-gate 		*ctxp = (crypto_context_t)ctx;
4247c478bd9Sstevel@tonic-gate 	else {
4257c478bd9Sstevel@tonic-gate 		/* Release the hold done in kcf_new_ctx(). */
4267c478bd9Sstevel@tonic-gate 		KCF_CONTEXT_REFRELE((kcf_context_t *)ctx->cc_framework_private);
4277c478bd9Sstevel@tonic-gate 	}
4287c478bd9Sstevel@tonic-gate 
429894b2776Smcpowers 	return (rv);
4307c478bd9Sstevel@tonic-gate }
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate int
crypto_verify_recover_single(crypto_context_t context,crypto_data_t * signature,crypto_data_t * data,crypto_call_req_t * cr)4337c478bd9Sstevel@tonic-gate crypto_verify_recover_single(crypto_context_t context, crypto_data_t *signature,
4347c478bd9Sstevel@tonic-gate     crypto_data_t *data, crypto_call_req_t *cr)
4357c478bd9Sstevel@tonic-gate {
4367c478bd9Sstevel@tonic-gate 	crypto_ctx_t *ctx = (crypto_ctx_t *)context;
4377c478bd9Sstevel@tonic-gate 	kcf_context_t *kcf_ctx;
4387c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
4397c478bd9Sstevel@tonic-gate 	int error;
4407c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	if ((ctx == NULL) ||
4437c478bd9Sstevel@tonic-gate 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
4447c478bd9Sstevel@tonic-gate 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
4457c478bd9Sstevel@tonic-gate 		return (CRYPTO_INVALID_CONTEXT);
4467c478bd9Sstevel@tonic-gate 	}
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	KCF_WRAP_VERIFY_OPS_PARAMS(&params, KCF_OP_VERIFY_RECOVER, 0, NULL,
4497c478bd9Sstevel@tonic-gate 	    NULL, data, signature, NULL);
4507c478bd9Sstevel@tonic-gate 	error = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 	/* Release the hold done in kcf_new_ctx() during init step. */
4537c478bd9Sstevel@tonic-gate 	KCF_CONTEXT_COND_RELEASE(error, kcf_ctx);
4547c478bd9Sstevel@tonic-gate 	return (error);
4557c478bd9Sstevel@tonic-gate }
456