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_SIGN_OFFSET(f)		offsetof(crypto_sign_ops_t, f)
37894b2776Smcpowers 
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate  * Sign 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_sign_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_sign_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_SIGN);
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_SIGN_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 int
crypto_sign_init(crypto_mechanism_t * mech,crypto_key_t * key,crypto_ctx_template_t tmpl,crypto_context_t * ctxp,crypto_call_req_t * crq)917c478bd9Sstevel@tonic-gate crypto_sign_init(crypto_mechanism_t *mech, crypto_key_t *key,
927c478bd9Sstevel@tonic-gate     crypto_ctx_template_t tmpl, crypto_context_t *ctxp, crypto_call_req_t *crq)
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate 	int error;
957c478bd9Sstevel@tonic-gate 	kcf_mech_entry_t *me;
967c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
977c478bd9Sstevel@tonic-gate 	kcf_prov_tried_t *list = NULL;
987c478bd9Sstevel@tonic-gate 	kcf_ctx_template_t *ctx_tmpl;
997c478bd9Sstevel@tonic-gate 	crypto_spi_ctx_template_t spi_ctx_tmpl = NULL;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate retry:
1027c478bd9Sstevel@tonic-gate 	/* The pd is returned held */
103436935a1SVladimir Kotal 	if ((pd = kcf_get_mech_provider(mech->cm_type, key, &me, &error,
104*9b009fc1SValerie Bubb Fenwick 	    list, CRYPTO_FG_SIGN, 0)) == NULL) {
1057c478bd9Sstevel@tonic-gate 		if (list != NULL)
1067c478bd9Sstevel@tonic-gate 			kcf_free_triedlist(list);
1077c478bd9Sstevel@tonic-gate 		return (error);
1087c478bd9Sstevel@tonic-gate 	}
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	/*
1117c478bd9Sstevel@tonic-gate 	 * For SW providers, check the validity of the context template
1127c478bd9Sstevel@tonic-gate 	 * It is very rare that the generation number mis-matches, so
1137c478bd9Sstevel@tonic-gate 	 * it is acceptable to fail here, and let the consumer recover by
1147c478bd9Sstevel@tonic-gate 	 * freeing this tmpl and create a new one for the key and new SW
1157c478bd9Sstevel@tonic-gate 	 * provider.
1167c478bd9Sstevel@tonic-gate 	 */
1177c478bd9Sstevel@tonic-gate 	if ((pd->pd_prov_type == CRYPTO_SW_PROVIDER) &&
1187c478bd9Sstevel@tonic-gate 	    ((ctx_tmpl = (kcf_ctx_template_t *)tmpl) != NULL)) {
1197c478bd9Sstevel@tonic-gate 		if (ctx_tmpl->ct_generation != me->me_gen_swprov) {
1207c478bd9Sstevel@tonic-gate 			if (list != NULL)
1217c478bd9Sstevel@tonic-gate 				kcf_free_triedlist(list);
1227c478bd9Sstevel@tonic-gate 			KCF_PROV_REFRELE(pd);
1237c478bd9Sstevel@tonic-gate 			return (CRYPTO_OLD_CTX_TEMPLATE);
1247c478bd9Sstevel@tonic-gate 		} else {
1257c478bd9Sstevel@tonic-gate 			spi_ctx_tmpl = ctx_tmpl->ct_prov_tmpl;
1267c478bd9Sstevel@tonic-gate 		}
1277c478bd9Sstevel@tonic-gate 	}
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	error = crypto_sign_init_prov(pd, pd->pd_sid, mech, key, spi_ctx_tmpl,
1307c478bd9Sstevel@tonic-gate 	    ctxp, crq);
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED &&
1337c478bd9Sstevel@tonic-gate 	    IS_RECOVERABLE(error)) {
1347c478bd9Sstevel@tonic-gate 		/* Add pd to the linked list of providers tried. */
1357c478bd9Sstevel@tonic-gate 		if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL)
1367c478bd9Sstevel@tonic-gate 			goto retry;
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	if (list != NULL)
1407c478bd9Sstevel@tonic-gate 		kcf_free_triedlist(list);
1417c478bd9Sstevel@tonic-gate 	KCF_PROV_REFRELE(pd);
1427c478bd9Sstevel@tonic-gate 	return (error);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate int
crypto_sign_single(crypto_context_t context,crypto_data_t * data,crypto_data_t * signature,crypto_call_req_t * cr)1467c478bd9Sstevel@tonic-gate crypto_sign_single(crypto_context_t context, crypto_data_t *data,
1477c478bd9Sstevel@tonic-gate     crypto_data_t *signature, crypto_call_req_t *cr)
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate 	crypto_ctx_t *ctx = (crypto_ctx_t *)context;
1507c478bd9Sstevel@tonic-gate 	kcf_context_t *kcf_ctx;
1517c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
1527c478bd9Sstevel@tonic-gate 	int error;
1537c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	if ((ctx == NULL) ||
1567c478bd9Sstevel@tonic-gate 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
1577c478bd9Sstevel@tonic-gate 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
1587c478bd9Sstevel@tonic-gate 		return (CRYPTO_INVALID_CONTEXT);
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	KCF_WRAP_SIGN_OPS_PARAMS(&params, KCF_OP_SINGLE, 0, NULL,
1627c478bd9Sstevel@tonic-gate 	    NULL, data, signature, NULL);
1637c478bd9Sstevel@tonic-gate 	error = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	/* Release the hold done in kcf_new_ctx() during init step. */
1667c478bd9Sstevel@tonic-gate 	KCF_CONTEXT_COND_RELEASE(error, kcf_ctx);
1677c478bd9Sstevel@tonic-gate 	return (error);
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate /*
1717c478bd9Sstevel@tonic-gate  * See comments for crypto_digest_update().
1727c478bd9Sstevel@tonic-gate  */
1737c478bd9Sstevel@tonic-gate int
crypto_sign_update(crypto_context_t context,crypto_data_t * data,crypto_call_req_t * cr)1747c478bd9Sstevel@tonic-gate crypto_sign_update(crypto_context_t context, crypto_data_t *data,
1757c478bd9Sstevel@tonic-gate     crypto_call_req_t *cr)
1767c478bd9Sstevel@tonic-gate {
1777c478bd9Sstevel@tonic-gate 	crypto_ctx_t *ctx = (crypto_ctx_t *)context;
1787c478bd9Sstevel@tonic-gate 	kcf_context_t *kcf_ctx;
1797c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
1807c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
181894b2776Smcpowers 	int rv;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	if ((ctx == NULL) ||
1847c478bd9Sstevel@tonic-gate 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
1857c478bd9Sstevel@tonic-gate 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
1867c478bd9Sstevel@tonic-gate 		return (CRYPTO_INVALID_CONTEXT);
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 
189894b2776Smcpowers 	ASSERT(pd->pd_prov_type != CRYPTO_LOGICAL_PROVIDER);
190894b2776Smcpowers 	KCF_WRAP_SIGN_OPS_PARAMS(&params, KCF_OP_UPDATE, ctx->cc_session, NULL,
1917c478bd9Sstevel@tonic-gate 	    NULL, data, NULL, NULL);
192894b2776Smcpowers 	rv = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
1937c478bd9Sstevel@tonic-gate 
194894b2776Smcpowers 	return (rv);
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate /*
1987c478bd9Sstevel@tonic-gate  * See comments for crypto_digest_final().
1997c478bd9Sstevel@tonic-gate  */
2007c478bd9Sstevel@tonic-gate int
crypto_sign_final(crypto_context_t context,crypto_data_t * signature,crypto_call_req_t * cr)2017c478bd9Sstevel@tonic-gate crypto_sign_final(crypto_context_t context, crypto_data_t *signature,
2027c478bd9Sstevel@tonic-gate     crypto_call_req_t *cr)
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate 	crypto_ctx_t *ctx = (crypto_ctx_t *)context;
2057c478bd9Sstevel@tonic-gate 	kcf_context_t *kcf_ctx;
2067c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
207894b2776Smcpowers 	int rv;
2087c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	if ((ctx == NULL) ||
2117c478bd9Sstevel@tonic-gate 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
2127c478bd9Sstevel@tonic-gate 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
2137c478bd9Sstevel@tonic-gate 		return (CRYPTO_INVALID_CONTEXT);
2147c478bd9Sstevel@tonic-gate 	}
2157c478bd9Sstevel@tonic-gate 
216894b2776Smcpowers 	ASSERT(pd->pd_prov_type != CRYPTO_LOGICAL_PROVIDER);
217894b2776Smcpowers 	KCF_WRAP_SIGN_OPS_PARAMS(&params, KCF_OP_FINAL, ctx->cc_session, NULL,
2187c478bd9Sstevel@tonic-gate 	    NULL, NULL, signature, NULL);
219894b2776Smcpowers 	rv = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	/* Release the hold done in kcf_new_ctx() during init step. */
222894b2776Smcpowers 	KCF_CONTEXT_COND_RELEASE(rv, kcf_ctx);
223894b2776Smcpowers 	return (rv);
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate int
crypto_sign_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)227894b2776Smcpowers crypto_sign_prov(crypto_provider_t provider, crypto_session_id_t sid,
2287c478bd9Sstevel@tonic-gate     crypto_mechanism_t *mech, crypto_key_t *key, crypto_data_t *data,
2297c478bd9Sstevel@tonic-gate     crypto_ctx_template_t tmpl, crypto_data_t *signature,
2307c478bd9Sstevel@tonic-gate     crypto_call_req_t *crq)
2317c478bd9Sstevel@tonic-gate {
2327c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
233894b2776Smcpowers 	kcf_provider_desc_t *pd = provider;
234894b2776Smcpowers 	kcf_provider_desc_t *real_provider = pd;
235894b2776Smcpowers 	int rv;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	ASSERT(KCF_PROV_REFHELD(pd));
238894b2776Smcpowers 
239894b2776Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
240436935a1SVladimir Kotal 		rv = kcf_get_hardware_provider(mech->cm_type, key,
241*9b009fc1SValerie Bubb Fenwick 		    CRYPTO_MECH_INVALID, NULL, pd, &real_provider,
242*9b009fc1SValerie Bubb Fenwick 		    CRYPTO_FG_SIGN_ATOMIC);
243894b2776Smcpowers 
244894b2776Smcpowers 		if (rv != CRYPTO_SUCCESS)
245894b2776Smcpowers 			return (rv);
246894b2776Smcpowers 	}
2477c478bd9Sstevel@tonic-gate 	KCF_WRAP_SIGN_OPS_PARAMS(&params, KCF_OP_ATOMIC, sid, mech,
2487c478bd9Sstevel@tonic-gate 	    key, data, signature, tmpl);
249894b2776Smcpowers 	rv = kcf_submit_request(real_provider, NULL, crq, &params, B_FALSE);
250894b2776Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
251894b2776Smcpowers 		KCF_PROV_REFRELE(real_provider);
2527c478bd9Sstevel@tonic-gate 
253894b2776Smcpowers 	return (rv);
2547c478bd9Sstevel@tonic-gate }
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate static int
sign_sr_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)2577c478bd9Sstevel@tonic-gate sign_sr_atomic_common(crypto_mechanism_t *mech, crypto_key_t *key,
2587c478bd9Sstevel@tonic-gate     crypto_data_t *data, crypto_ctx_template_t tmpl, crypto_data_t *signature,
2597c478bd9Sstevel@tonic-gate     crypto_call_req_t *crq, crypto_func_group_t fg)
2607c478bd9Sstevel@tonic-gate {
2617c478bd9Sstevel@tonic-gate 	int error;
2627c478bd9Sstevel@tonic-gate 	kcf_mech_entry_t *me;
2637c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
2647c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
2657c478bd9Sstevel@tonic-gate 	kcf_prov_tried_t *list = NULL;
2667c478bd9Sstevel@tonic-gate 	kcf_ctx_template_t *ctx_tmpl;
2677c478bd9Sstevel@tonic-gate 	crypto_spi_ctx_template_t spi_ctx_tmpl = NULL;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate retry:
2707c478bd9Sstevel@tonic-gate 	/* The pd is returned held */
271436935a1SVladimir Kotal 	if ((pd = kcf_get_mech_provider(mech->cm_type, key, &me, &error,
272*9b009fc1SValerie Bubb Fenwick 	    list, fg, data->cd_length)) == NULL) {
2737c478bd9Sstevel@tonic-gate 		if (list != NULL)
2747c478bd9Sstevel@tonic-gate 			kcf_free_triedlist(list);
2757c478bd9Sstevel@tonic-gate 		return (error);
2767c478bd9Sstevel@tonic-gate 	}
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	/*
2797c478bd9Sstevel@tonic-gate 	 * For SW providers, check the validity of the context template
2807c478bd9Sstevel@tonic-gate 	 * It is very rare that the generation number mis-matches, so
2817c478bd9Sstevel@tonic-gate 	 * it is acceptable to fail here, and let the consumer recover by
2827c478bd9Sstevel@tonic-gate 	 * freeing this tmpl and create a new one for the key and new SW
2837c478bd9Sstevel@tonic-gate 	 * provider.
2847c478bd9Sstevel@tonic-gate 	 */
2857c478bd9Sstevel@tonic-gate 	if ((pd->pd_prov_type == CRYPTO_SW_PROVIDER) &&
2867c478bd9Sstevel@tonic-gate 	    ((ctx_tmpl = (kcf_ctx_template_t *)tmpl) != NULL)) {
2877c478bd9Sstevel@tonic-gate 		if (ctx_tmpl->ct_generation != me->me_gen_swprov) {
2887c478bd9Sstevel@tonic-gate 			if (list != NULL)
2897c478bd9Sstevel@tonic-gate 				kcf_free_triedlist(list);
2907c478bd9Sstevel@tonic-gate 			KCF_PROV_REFRELE(pd);
2917c478bd9Sstevel@tonic-gate 			return (CRYPTO_OLD_CTX_TEMPLATE);
2927c478bd9Sstevel@tonic-gate 		} else {
2937c478bd9Sstevel@tonic-gate 			spi_ctx_tmpl = ctx_tmpl->ct_prov_tmpl;
2947c478bd9Sstevel@tonic-gate 		}
2957c478bd9Sstevel@tonic-gate 	}
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	/* The fast path for SW providers. */
2987c478bd9Sstevel@tonic-gate 	if (CHECK_FASTPATH(crq, pd)) {
2997c478bd9Sstevel@tonic-gate 		crypto_mechanism_t lmech;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 		lmech = *mech;
3027c478bd9Sstevel@tonic-gate 		KCF_SET_PROVIDER_MECHNUM(mech->cm_type, pd, &lmech);
3037c478bd9Sstevel@tonic-gate 		if (fg == CRYPTO_FG_SIGN_ATOMIC)
3047c478bd9Sstevel@tonic-gate 			error = KCF_PROV_SIGN_ATOMIC(pd, pd->pd_sid, &lmech,
3057c478bd9Sstevel@tonic-gate 			    key, data, spi_ctx_tmpl, signature,
3067c478bd9Sstevel@tonic-gate 			    KCF_SWFP_RHNDL(crq));
3077c478bd9Sstevel@tonic-gate 		else
3087c478bd9Sstevel@tonic-gate 			error = KCF_PROV_SIGN_RECOVER_ATOMIC(pd, pd->pd_sid,
3097c478bd9Sstevel@tonic-gate 			    &lmech, key, data, spi_ctx_tmpl, signature,
3107c478bd9Sstevel@tonic-gate 			    KCF_SWFP_RHNDL(crq));
3117c478bd9Sstevel@tonic-gate 		KCF_PROV_INCRSTATS(pd, error);
3127c478bd9Sstevel@tonic-gate 	} else {
3137c478bd9Sstevel@tonic-gate 		kcf_op_type_t op = ((fg == CRYPTO_FG_SIGN_ATOMIC) ?
3147c478bd9Sstevel@tonic-gate 		    KCF_OP_ATOMIC : KCF_OP_SIGN_RECOVER_ATOMIC);
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 		KCF_WRAP_SIGN_OPS_PARAMS(&params, op, pd->pd_sid,
3177c478bd9Sstevel@tonic-gate 		    mech, key, data, signature, spi_ctx_tmpl);
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 		/* no crypto context to carry between multiple parts. */
3207c478bd9Sstevel@tonic-gate 		error = kcf_submit_request(pd, NULL, crq, &params, B_FALSE);
3217c478bd9Sstevel@tonic-gate 	}
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED &&
3247c478bd9Sstevel@tonic-gate 	    IS_RECOVERABLE(error)) {
3257c478bd9Sstevel@tonic-gate 		/* Add pd to the linked list of providers tried. */
3267c478bd9Sstevel@tonic-gate 		if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL)
3277c478bd9Sstevel@tonic-gate 			goto retry;
3287c478bd9Sstevel@tonic-gate 	}
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	if (list != NULL)
3317c478bd9Sstevel@tonic-gate 		kcf_free_triedlist(list);
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	KCF_PROV_REFRELE(pd);
3347c478bd9Sstevel@tonic-gate 	return (error);
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate int
crypto_sign(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)3387c478bd9Sstevel@tonic-gate crypto_sign(crypto_mechanism_t *mech, crypto_key_t *key, crypto_data_t *data,
3397c478bd9Sstevel@tonic-gate     crypto_ctx_template_t tmpl, crypto_data_t *signature,
3407c478bd9Sstevel@tonic-gate     crypto_call_req_t *crq)
3417c478bd9Sstevel@tonic-gate {
3427c478bd9Sstevel@tonic-gate 	return (sign_sr_atomic_common(mech, key, data, tmpl, signature, crq,
3437c478bd9Sstevel@tonic-gate 	    CRYPTO_FG_SIGN_ATOMIC));
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate int
crypto_sign_recover_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)347894b2776Smcpowers crypto_sign_recover_prov(crypto_provider_t provider, crypto_session_id_t sid,
348894b2776Smcpowers     crypto_mechanism_t *mech, crypto_key_t *key, crypto_data_t *data,
349894b2776Smcpowers     crypto_ctx_template_t tmpl, crypto_data_t *signature,
3507c478bd9Sstevel@tonic-gate     crypto_call_req_t *crq)
3517c478bd9Sstevel@tonic-gate {
3527c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
353894b2776Smcpowers 	kcf_provider_desc_t *pd = provider;
354894b2776Smcpowers 	kcf_provider_desc_t *real_provider = pd;
355894b2776Smcpowers 	int rv;
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	ASSERT(KCF_PROV_REFHELD(pd));
358894b2776Smcpowers 
359894b2776Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
360436935a1SVladimir Kotal 		rv = kcf_get_hardware_provider(mech->cm_type, key,
361*9b009fc1SValerie Bubb Fenwick 		    CRYPTO_MECH_INVALID, NULL, pd, &real_provider,
362*9b009fc1SValerie Bubb Fenwick 		    CRYPTO_FG_SIGN_RECOVER_ATOMIC);
363894b2776Smcpowers 
364894b2776Smcpowers 		if (rv != CRYPTO_SUCCESS)
365894b2776Smcpowers 			return (rv);
366894b2776Smcpowers 	}
3677c478bd9Sstevel@tonic-gate 	KCF_WRAP_SIGN_OPS_PARAMS(&params, KCF_OP_SIGN_RECOVER_ATOMIC, sid, mech,
3687c478bd9Sstevel@tonic-gate 	    key, data, signature, tmpl);
369894b2776Smcpowers 	rv = kcf_submit_request(real_provider, NULL, crq, &params, B_FALSE);
370894b2776Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
371894b2776Smcpowers 		KCF_PROV_REFRELE(real_provider);
3727c478bd9Sstevel@tonic-gate 
373894b2776Smcpowers 	return (rv);
3747c478bd9Sstevel@tonic-gate }
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate int
crypto_sign_recover(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)3777c478bd9Sstevel@tonic-gate crypto_sign_recover(crypto_mechanism_t *mech, crypto_key_t *key,
3787c478bd9Sstevel@tonic-gate     crypto_data_t *data, crypto_ctx_template_t tmpl, crypto_data_t *signature,
3797c478bd9Sstevel@tonic-gate     crypto_call_req_t *crq)
3807c478bd9Sstevel@tonic-gate {
3817c478bd9Sstevel@tonic-gate 	return (sign_sr_atomic_common(mech, key, data, tmpl, signature, crq,
3827c478bd9Sstevel@tonic-gate 	    CRYPTO_FG_SIGN_RECOVER_ATOMIC));
3837c478bd9Sstevel@tonic-gate }
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate int
crypto_sign_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)386894b2776Smcpowers crypto_sign_recover_init_prov(crypto_provider_t provider,
387894b2776Smcpowers     crypto_session_id_t sid, crypto_mechanism_t *mech, crypto_key_t *key,
388894b2776Smcpowers     crypto_ctx_template_t tmpl, crypto_context_t *ctxp, crypto_call_req_t *crq)
3897c478bd9Sstevel@tonic-gate {
390894b2776Smcpowers 	int rv;
3917c478bd9Sstevel@tonic-gate 	crypto_ctx_t *ctx;
3927c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
393894b2776Smcpowers 	kcf_provider_desc_t *pd = provider;
394894b2776Smcpowers 	kcf_provider_desc_t *real_provider = pd;
395894b2776Smcpowers 
396894b2776Smcpowers 	ASSERT(KCF_PROV_REFHELD(pd));
397894b2776Smcpowers 
398894b2776Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
399436935a1SVladimir Kotal 		rv = kcf_get_hardware_provider(mech->cm_type, key,
400*9b009fc1SValerie Bubb Fenwick 		    CRYPTO_MECH_INVALID, NULL, pd, &real_provider,
401*9b009fc1SValerie Bubb Fenwick 		    CRYPTO_FG_SIGN_RECOVER);
4027c478bd9Sstevel@tonic-gate 
403894b2776Smcpowers 		if (rv != CRYPTO_SUCCESS)
404894b2776Smcpowers 			return (rv);
405894b2776Smcpowers 	}
406894b2776Smcpowers 
407894b2776Smcpowers 	/* Allocate and initialize the canonical context */
408894b2776Smcpowers 	if ((ctx = kcf_new_ctx(crq, real_provider, sid)) == NULL) {
409894b2776Smcpowers 		if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
410894b2776Smcpowers 			KCF_PROV_REFRELE(real_provider);
4117c478bd9Sstevel@tonic-gate 		return (CRYPTO_HOST_MEMORY);
412894b2776Smcpowers 	}
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	KCF_WRAP_SIGN_OPS_PARAMS(&params, KCF_OP_SIGN_RECOVER_INIT, sid, mech,
4157c478bd9Sstevel@tonic-gate 	    key, NULL, NULL, tmpl);
416894b2776Smcpowers 	rv = kcf_submit_request(real_provider, ctx, crq, &params, B_FALSE);
417894b2776Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
418894b2776Smcpowers 		KCF_PROV_REFRELE(real_provider);
4197c478bd9Sstevel@tonic-gate 
420894b2776Smcpowers 	if ((rv == CRYPTO_SUCCESS) || (rv == CRYPTO_QUEUED))
4217c478bd9Sstevel@tonic-gate 		*ctxp = (crypto_context_t)ctx;
4227c478bd9Sstevel@tonic-gate 	else {
4237c478bd9Sstevel@tonic-gate 		/* Release the hold done in kcf_new_ctx(). */
4247c478bd9Sstevel@tonic-gate 		KCF_CONTEXT_REFRELE((kcf_context_t *)ctx->cc_framework_private);
4257c478bd9Sstevel@tonic-gate 	}
4267c478bd9Sstevel@tonic-gate 
427894b2776Smcpowers 	return (rv);
4287c478bd9Sstevel@tonic-gate }
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate int
crypto_sign_recover_single(crypto_context_t context,crypto_data_t * data,crypto_data_t * signature,crypto_call_req_t * cr)4317c478bd9Sstevel@tonic-gate crypto_sign_recover_single(crypto_context_t context, crypto_data_t *data,
4327c478bd9Sstevel@tonic-gate     crypto_data_t *signature, crypto_call_req_t *cr)
4337c478bd9Sstevel@tonic-gate {
4347c478bd9Sstevel@tonic-gate 	crypto_ctx_t *ctx = (crypto_ctx_t *)context;
4357c478bd9Sstevel@tonic-gate 	kcf_context_t *kcf_ctx;
4367c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
4377c478bd9Sstevel@tonic-gate 	int error;
4387c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	if ((ctx == NULL) ||
4417c478bd9Sstevel@tonic-gate 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
4427c478bd9Sstevel@tonic-gate 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
4437c478bd9Sstevel@tonic-gate 		return (CRYPTO_INVALID_CONTEXT);
4447c478bd9Sstevel@tonic-gate 	}
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	KCF_WRAP_SIGN_OPS_PARAMS(&params, KCF_OP_SIGN_RECOVER, 0, NULL,
4477c478bd9Sstevel@tonic-gate 	    NULL, data, signature, NULL);
4487c478bd9Sstevel@tonic-gate 	error = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	/* Release the hold done in kcf_new_ctx() during init step. */
4517c478bd9Sstevel@tonic-gate 	KCF_CONTEXT_COND_RELEASE(error, kcf_ctx);
4527c478bd9Sstevel@tonic-gate 	return (error);
4537c478bd9Sstevel@tonic-gate }
454