xref: /onnv-gate/usr/src/uts/common/crypto/api/kcf_sign.c (revision 904:4f885c862682)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/errno.h>
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/kmem.h>
32*904Smcpowers #include <sys/sysmacros.h>
330Sstevel@tonic-gate #include <sys/crypto/common.h>
340Sstevel@tonic-gate #include <sys/crypto/impl.h>
350Sstevel@tonic-gate #include <sys/crypto/api.h>
360Sstevel@tonic-gate #include <sys/crypto/spi.h>
370Sstevel@tonic-gate #include <sys/crypto/sched_impl.h>
380Sstevel@tonic-gate 
39*904Smcpowers #define	CRYPTO_OPS_OFFSET(f)		offsetof(crypto_ops_t, co_##f)
40*904Smcpowers #define	CRYPTO_SIGN_OFFSET(f)		offsetof(crypto_sign_ops_t, f)
41*904Smcpowers 
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate  * Sign entry points.
440Sstevel@tonic-gate  */
450Sstevel@tonic-gate 
460Sstevel@tonic-gate /*
470Sstevel@tonic-gate  * See comments for crypto_digest_init_prov().
480Sstevel@tonic-gate  */
490Sstevel@tonic-gate int
50*904Smcpowers crypto_sign_init_prov(crypto_provider_t provider, crypto_session_id_t sid,
510Sstevel@tonic-gate     crypto_mechanism_t *mech, crypto_key_t *key, crypto_ctx_template_t tmpl,
520Sstevel@tonic-gate     crypto_context_t *ctxp, crypto_call_req_t *crq)
530Sstevel@tonic-gate {
54*904Smcpowers 	int rv;
550Sstevel@tonic-gate 	crypto_ctx_t *ctx;
560Sstevel@tonic-gate 	kcf_req_params_t params;
57*904Smcpowers 	kcf_provider_desc_t *pd = provider;
58*904Smcpowers 	kcf_provider_desc_t *real_provider = pd;
590Sstevel@tonic-gate 
60*904Smcpowers 	ASSERT(KCF_PROV_REFHELD(pd));
61*904Smcpowers 
62*904Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
63*904Smcpowers 		rv = kcf_get_hardware_provider(mech->cm_type,
64*904Smcpowers 		    CRYPTO_MECH_INVALID, CRYPTO_OPS_OFFSET(sign_ops),
65*904Smcpowers 		    CRYPTO_SIGN_OFFSET(sign_init),
66*904Smcpowers 		    CHECK_RESTRICT(crq), pd, &real_provider);
67*904Smcpowers 
68*904Smcpowers 		if (rv != CRYPTO_SUCCESS)
69*904Smcpowers 			return (rv);
70*904Smcpowers 	}
71*904Smcpowers 
72*904Smcpowers 	/* Allocate and initialize the canonical context */
73*904Smcpowers 	if ((ctx = kcf_new_ctx(crq, real_provider, sid)) == NULL) {
74*904Smcpowers 		if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
75*904Smcpowers 			KCF_PROV_REFRELE(real_provider);
760Sstevel@tonic-gate 		return (CRYPTO_HOST_MEMORY);
77*904Smcpowers 	}
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	KCF_WRAP_SIGN_OPS_PARAMS(&params, KCF_OP_INIT, sid, mech,
800Sstevel@tonic-gate 	    key, NULL, NULL, tmpl);
81*904Smcpowers 	rv = kcf_submit_request(real_provider, ctx, crq, &params, B_FALSE);
82*904Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
83*904Smcpowers 		KCF_PROV_REFRELE(real_provider);
840Sstevel@tonic-gate 
85*904Smcpowers 	if ((rv == CRYPTO_SUCCESS) || (rv == CRYPTO_QUEUED))
860Sstevel@tonic-gate 		*ctxp = (crypto_context_t)ctx;
870Sstevel@tonic-gate 	else {
880Sstevel@tonic-gate 		/* Release the hold done in kcf_new_ctx(). */
890Sstevel@tonic-gate 		KCF_CONTEXT_REFRELE((kcf_context_t *)ctx->cc_framework_private);
900Sstevel@tonic-gate 	}
910Sstevel@tonic-gate 
92*904Smcpowers 	return (rv);
930Sstevel@tonic-gate }
940Sstevel@tonic-gate 
950Sstevel@tonic-gate int
960Sstevel@tonic-gate crypto_sign_init(crypto_mechanism_t *mech, crypto_key_t *key,
970Sstevel@tonic-gate     crypto_ctx_template_t tmpl, crypto_context_t *ctxp, crypto_call_req_t *crq)
980Sstevel@tonic-gate {
990Sstevel@tonic-gate 	int error;
1000Sstevel@tonic-gate 	kcf_mech_entry_t *me;
1010Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
1020Sstevel@tonic-gate 	kcf_prov_tried_t *list = NULL;
1030Sstevel@tonic-gate 	kcf_ctx_template_t *ctx_tmpl;
1040Sstevel@tonic-gate 	crypto_spi_ctx_template_t spi_ctx_tmpl = NULL;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate retry:
1070Sstevel@tonic-gate 	/* The pd is returned held */
1080Sstevel@tonic-gate 	if ((pd = kcf_get_mech_provider(mech->cm_type, &me, &error,
1090Sstevel@tonic-gate 	    list, CRYPTO_FG_SIGN, CHECK_RESTRICT(crq), 0)) == NULL) {
1100Sstevel@tonic-gate 		if (list != NULL)
1110Sstevel@tonic-gate 			kcf_free_triedlist(list);
1120Sstevel@tonic-gate 		return (error);
1130Sstevel@tonic-gate 	}
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	/*
1160Sstevel@tonic-gate 	 * For SW providers, check the validity of the context template
1170Sstevel@tonic-gate 	 * It is very rare that the generation number mis-matches, so
1180Sstevel@tonic-gate 	 * it is acceptable to fail here, and let the consumer recover by
1190Sstevel@tonic-gate 	 * freeing this tmpl and create a new one for the key and new SW
1200Sstevel@tonic-gate 	 * provider.
1210Sstevel@tonic-gate 	 */
1220Sstevel@tonic-gate 	if ((pd->pd_prov_type == CRYPTO_SW_PROVIDER) &&
1230Sstevel@tonic-gate 	    ((ctx_tmpl = (kcf_ctx_template_t *)tmpl) != NULL)) {
1240Sstevel@tonic-gate 		if (ctx_tmpl->ct_generation != me->me_gen_swprov) {
1250Sstevel@tonic-gate 			if (list != NULL)
1260Sstevel@tonic-gate 				kcf_free_triedlist(list);
1270Sstevel@tonic-gate 			KCF_PROV_REFRELE(pd);
1280Sstevel@tonic-gate 			return (CRYPTO_OLD_CTX_TEMPLATE);
1290Sstevel@tonic-gate 		} else {
1300Sstevel@tonic-gate 			spi_ctx_tmpl = ctx_tmpl->ct_prov_tmpl;
1310Sstevel@tonic-gate 		}
1320Sstevel@tonic-gate 	}
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	error = crypto_sign_init_prov(pd, pd->pd_sid, mech, key, spi_ctx_tmpl,
1350Sstevel@tonic-gate 	    ctxp, crq);
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED &&
1380Sstevel@tonic-gate 	    IS_RECOVERABLE(error)) {
1390Sstevel@tonic-gate 		/* Add pd to the linked list of providers tried. */
1400Sstevel@tonic-gate 		if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL)
1410Sstevel@tonic-gate 			goto retry;
1420Sstevel@tonic-gate 	}
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	if (list != NULL)
1450Sstevel@tonic-gate 		kcf_free_triedlist(list);
1460Sstevel@tonic-gate 	KCF_PROV_REFRELE(pd);
1470Sstevel@tonic-gate 	return (error);
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate int
1510Sstevel@tonic-gate crypto_sign_single(crypto_context_t context, crypto_data_t *data,
1520Sstevel@tonic-gate     crypto_data_t *signature, crypto_call_req_t *cr)
1530Sstevel@tonic-gate {
1540Sstevel@tonic-gate 	crypto_ctx_t *ctx = (crypto_ctx_t *)context;
1550Sstevel@tonic-gate 	kcf_context_t *kcf_ctx;
1560Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
1570Sstevel@tonic-gate 	int error;
1580Sstevel@tonic-gate 	kcf_req_params_t params;
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	if ((ctx == NULL) ||
1610Sstevel@tonic-gate 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
1620Sstevel@tonic-gate 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
1630Sstevel@tonic-gate 		return (CRYPTO_INVALID_CONTEXT);
1640Sstevel@tonic-gate 	}
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	KCF_PROV_REFHOLD(pd);
1670Sstevel@tonic-gate 	KCF_WRAP_SIGN_OPS_PARAMS(&params, KCF_OP_SINGLE, 0, NULL,
1680Sstevel@tonic-gate 	    NULL, data, signature, NULL);
1690Sstevel@tonic-gate 	error = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
1700Sstevel@tonic-gate 	KCF_PROV_REFRELE(pd);
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	/* Release the hold done in kcf_new_ctx() during init step. */
1730Sstevel@tonic-gate 	KCF_CONTEXT_COND_RELEASE(error, kcf_ctx);
1740Sstevel@tonic-gate 	return (error);
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate /*
1780Sstevel@tonic-gate  * See comments for crypto_digest_update().
1790Sstevel@tonic-gate  */
1800Sstevel@tonic-gate int
1810Sstevel@tonic-gate crypto_sign_update(crypto_context_t context, crypto_data_t *data,
1820Sstevel@tonic-gate     crypto_call_req_t *cr)
1830Sstevel@tonic-gate {
1840Sstevel@tonic-gate 	crypto_ctx_t *ctx = (crypto_ctx_t *)context;
1850Sstevel@tonic-gate 	kcf_context_t *kcf_ctx;
1860Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
1870Sstevel@tonic-gate 	kcf_req_params_t params;
188*904Smcpowers 	int rv;
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	if ((ctx == NULL) ||
1910Sstevel@tonic-gate 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
1920Sstevel@tonic-gate 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
1930Sstevel@tonic-gate 		return (CRYPTO_INVALID_CONTEXT);
1940Sstevel@tonic-gate 	}
1950Sstevel@tonic-gate 
196*904Smcpowers 	ASSERT(pd->pd_prov_type != CRYPTO_LOGICAL_PROVIDER);
1970Sstevel@tonic-gate 	KCF_PROV_REFHOLD(pd);
198*904Smcpowers 	KCF_WRAP_SIGN_OPS_PARAMS(&params, KCF_OP_UPDATE, ctx->cc_session, NULL,
1990Sstevel@tonic-gate 	    NULL, data, NULL, NULL);
200*904Smcpowers 	rv = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
2010Sstevel@tonic-gate 	KCF_PROV_REFRELE(pd);
2020Sstevel@tonic-gate 
203*904Smcpowers 	return (rv);
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate /*
2070Sstevel@tonic-gate  * See comments for crypto_digest_final().
2080Sstevel@tonic-gate  */
2090Sstevel@tonic-gate int
2100Sstevel@tonic-gate crypto_sign_final(crypto_context_t context, crypto_data_t *signature,
2110Sstevel@tonic-gate     crypto_call_req_t *cr)
2120Sstevel@tonic-gate {
2130Sstevel@tonic-gate 	crypto_ctx_t *ctx = (crypto_ctx_t *)context;
2140Sstevel@tonic-gate 	kcf_context_t *kcf_ctx;
2150Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
216*904Smcpowers 	int rv;
2170Sstevel@tonic-gate 	kcf_req_params_t params;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	if ((ctx == NULL) ||
2200Sstevel@tonic-gate 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
2210Sstevel@tonic-gate 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
2220Sstevel@tonic-gate 		return (CRYPTO_INVALID_CONTEXT);
2230Sstevel@tonic-gate 	}
2240Sstevel@tonic-gate 
225*904Smcpowers 	ASSERT(pd->pd_prov_type != CRYPTO_LOGICAL_PROVIDER);
2260Sstevel@tonic-gate 	KCF_PROV_REFHOLD(pd);
227*904Smcpowers 	KCF_WRAP_SIGN_OPS_PARAMS(&params, KCF_OP_FINAL, ctx->cc_session, NULL,
2280Sstevel@tonic-gate 	    NULL, NULL, signature, NULL);
229*904Smcpowers 	rv = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
2300Sstevel@tonic-gate 	KCF_PROV_REFRELE(pd);
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	/* Release the hold done in kcf_new_ctx() during init step. */
233*904Smcpowers 	KCF_CONTEXT_COND_RELEASE(rv, kcf_ctx);
234*904Smcpowers 	return (rv);
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate int
238*904Smcpowers crypto_sign_prov(crypto_provider_t provider, crypto_session_id_t sid,
2390Sstevel@tonic-gate     crypto_mechanism_t *mech, crypto_key_t *key, crypto_data_t *data,
2400Sstevel@tonic-gate     crypto_ctx_template_t tmpl, crypto_data_t *signature,
2410Sstevel@tonic-gate     crypto_call_req_t *crq)
2420Sstevel@tonic-gate {
2430Sstevel@tonic-gate 	kcf_req_params_t params;
244*904Smcpowers 	kcf_provider_desc_t *pd = provider;
245*904Smcpowers 	kcf_provider_desc_t *real_provider = pd;
246*904Smcpowers 	int rv;
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	ASSERT(KCF_PROV_REFHELD(pd));
249*904Smcpowers 
250*904Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
251*904Smcpowers 		rv = kcf_get_hardware_provider(mech->cm_type,
252*904Smcpowers 		    CRYPTO_MECH_INVALID, CRYPTO_OPS_OFFSET(sign_ops),
253*904Smcpowers 		    CRYPTO_SIGN_OFFSET(sign_atomic), CHECK_RESTRICT(crq),
254*904Smcpowers 		    pd, &real_provider);
255*904Smcpowers 
256*904Smcpowers 		if (rv != CRYPTO_SUCCESS)
257*904Smcpowers 			return (rv);
258*904Smcpowers 	}
2590Sstevel@tonic-gate 	KCF_WRAP_SIGN_OPS_PARAMS(&params, KCF_OP_ATOMIC, sid, mech,
2600Sstevel@tonic-gate 	    key, data, signature, tmpl);
261*904Smcpowers 	rv = kcf_submit_request(real_provider, NULL, crq, &params, B_FALSE);
262*904Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
263*904Smcpowers 		KCF_PROV_REFRELE(real_provider);
2640Sstevel@tonic-gate 
265*904Smcpowers 	return (rv);
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate static int
2690Sstevel@tonic-gate sign_sr_atomic_common(crypto_mechanism_t *mech, crypto_key_t *key,
2700Sstevel@tonic-gate     crypto_data_t *data, crypto_ctx_template_t tmpl, crypto_data_t *signature,
2710Sstevel@tonic-gate     crypto_call_req_t *crq, crypto_func_group_t fg)
2720Sstevel@tonic-gate {
2730Sstevel@tonic-gate 	int error;
2740Sstevel@tonic-gate 	kcf_mech_entry_t *me;
2750Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
2760Sstevel@tonic-gate 	kcf_req_params_t params;
2770Sstevel@tonic-gate 	kcf_prov_tried_t *list = NULL;
2780Sstevel@tonic-gate 	kcf_ctx_template_t *ctx_tmpl;
2790Sstevel@tonic-gate 	crypto_spi_ctx_template_t spi_ctx_tmpl = NULL;
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate retry:
2820Sstevel@tonic-gate 	/* The pd is returned held */
2830Sstevel@tonic-gate 	if ((pd = kcf_get_mech_provider(mech->cm_type, &me, &error, list, fg,
2840Sstevel@tonic-gate 	    CHECK_RESTRICT(crq), data->cd_length)) == NULL) {
2850Sstevel@tonic-gate 		if (list != NULL)
2860Sstevel@tonic-gate 			kcf_free_triedlist(list);
2870Sstevel@tonic-gate 		return (error);
2880Sstevel@tonic-gate 	}
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	/*
2910Sstevel@tonic-gate 	 * For SW providers, check the validity of the context template
2920Sstevel@tonic-gate 	 * It is very rare that the generation number mis-matches, so
2930Sstevel@tonic-gate 	 * it is acceptable to fail here, and let the consumer recover by
2940Sstevel@tonic-gate 	 * freeing this tmpl and create a new one for the key and new SW
2950Sstevel@tonic-gate 	 * provider.
2960Sstevel@tonic-gate 	 */
2970Sstevel@tonic-gate 	if ((pd->pd_prov_type == CRYPTO_SW_PROVIDER) &&
2980Sstevel@tonic-gate 	    ((ctx_tmpl = (kcf_ctx_template_t *)tmpl) != NULL)) {
2990Sstevel@tonic-gate 		if (ctx_tmpl->ct_generation != me->me_gen_swprov) {
3000Sstevel@tonic-gate 			if (list != NULL)
3010Sstevel@tonic-gate 				kcf_free_triedlist(list);
3020Sstevel@tonic-gate 			KCF_PROV_REFRELE(pd);
3030Sstevel@tonic-gate 			return (CRYPTO_OLD_CTX_TEMPLATE);
3040Sstevel@tonic-gate 		} else {
3050Sstevel@tonic-gate 			spi_ctx_tmpl = ctx_tmpl->ct_prov_tmpl;
3060Sstevel@tonic-gate 		}
3070Sstevel@tonic-gate 	}
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	/* The fast path for SW providers. */
3100Sstevel@tonic-gate 	if (CHECK_FASTPATH(crq, pd)) {
3110Sstevel@tonic-gate 		crypto_mechanism_t lmech;
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 		lmech = *mech;
3140Sstevel@tonic-gate 		KCF_SET_PROVIDER_MECHNUM(mech->cm_type, pd, &lmech);
3150Sstevel@tonic-gate 		if (fg == CRYPTO_FG_SIGN_ATOMIC)
3160Sstevel@tonic-gate 			error = KCF_PROV_SIGN_ATOMIC(pd, pd->pd_sid, &lmech,
3170Sstevel@tonic-gate 			    key, data, spi_ctx_tmpl, signature,
3180Sstevel@tonic-gate 			    KCF_SWFP_RHNDL(crq));
3190Sstevel@tonic-gate 		else
3200Sstevel@tonic-gate 			error = KCF_PROV_SIGN_RECOVER_ATOMIC(pd, pd->pd_sid,
3210Sstevel@tonic-gate 			    &lmech, key, data, spi_ctx_tmpl, signature,
3220Sstevel@tonic-gate 			    KCF_SWFP_RHNDL(crq));
3230Sstevel@tonic-gate 		KCF_PROV_INCRSTATS(pd, error);
3240Sstevel@tonic-gate 	} else {
3250Sstevel@tonic-gate 		kcf_op_type_t op = ((fg == CRYPTO_FG_SIGN_ATOMIC) ?
3260Sstevel@tonic-gate 		    KCF_OP_ATOMIC : KCF_OP_SIGN_RECOVER_ATOMIC);
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 		KCF_WRAP_SIGN_OPS_PARAMS(&params, op, pd->pd_sid,
3290Sstevel@tonic-gate 		    mech, key, data, signature, spi_ctx_tmpl);
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 		/* no crypto context to carry between multiple parts. */
3320Sstevel@tonic-gate 		error = kcf_submit_request(pd, NULL, crq, &params, B_FALSE);
3330Sstevel@tonic-gate 	}
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED &&
3360Sstevel@tonic-gate 	    IS_RECOVERABLE(error)) {
3370Sstevel@tonic-gate 		/* Add pd to the linked list of providers tried. */
3380Sstevel@tonic-gate 		if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL)
3390Sstevel@tonic-gate 			goto retry;
3400Sstevel@tonic-gate 	}
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	if (list != NULL)
3430Sstevel@tonic-gate 		kcf_free_triedlist(list);
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	KCF_PROV_REFRELE(pd);
3460Sstevel@tonic-gate 	return (error);
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate int
3500Sstevel@tonic-gate crypto_sign(crypto_mechanism_t *mech, crypto_key_t *key, crypto_data_t *data,
3510Sstevel@tonic-gate     crypto_ctx_template_t tmpl, crypto_data_t *signature,
3520Sstevel@tonic-gate     crypto_call_req_t *crq)
3530Sstevel@tonic-gate {
3540Sstevel@tonic-gate 	return (sign_sr_atomic_common(mech, key, data, tmpl, signature, crq,
3550Sstevel@tonic-gate 	    CRYPTO_FG_SIGN_ATOMIC));
3560Sstevel@tonic-gate }
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate int
359*904Smcpowers crypto_sign_recover_prov(crypto_provider_t provider, crypto_session_id_t sid,
360*904Smcpowers     crypto_mechanism_t *mech, crypto_key_t *key, crypto_data_t *data,
361*904Smcpowers     crypto_ctx_template_t tmpl, crypto_data_t *signature,
3620Sstevel@tonic-gate     crypto_call_req_t *crq)
3630Sstevel@tonic-gate {
3640Sstevel@tonic-gate 	kcf_req_params_t params;
365*904Smcpowers 	kcf_provider_desc_t *pd = provider;
366*904Smcpowers 	kcf_provider_desc_t *real_provider = pd;
367*904Smcpowers 	int rv;
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	ASSERT(KCF_PROV_REFHELD(pd));
370*904Smcpowers 
371*904Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
372*904Smcpowers 		rv = kcf_get_hardware_provider(mech->cm_type,
373*904Smcpowers 		    CRYPTO_MECH_INVALID, CRYPTO_OPS_OFFSET(sign_ops),
374*904Smcpowers 		    CRYPTO_SIGN_OFFSET(sign_recover_atomic),
375*904Smcpowers 		    CHECK_RESTRICT(crq), pd, &real_provider);
376*904Smcpowers 
377*904Smcpowers 		if (rv != CRYPTO_SUCCESS)
378*904Smcpowers 			return (rv);
379*904Smcpowers 	}
3800Sstevel@tonic-gate 	KCF_WRAP_SIGN_OPS_PARAMS(&params, KCF_OP_SIGN_RECOVER_ATOMIC, sid, mech,
3810Sstevel@tonic-gate 	    key, data, signature, tmpl);
382*904Smcpowers 	rv = kcf_submit_request(real_provider, NULL, crq, &params, B_FALSE);
383*904Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
384*904Smcpowers 		KCF_PROV_REFRELE(real_provider);
3850Sstevel@tonic-gate 
386*904Smcpowers 	return (rv);
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate int
3900Sstevel@tonic-gate crypto_sign_recover(crypto_mechanism_t *mech, crypto_key_t *key,
3910Sstevel@tonic-gate     crypto_data_t *data, crypto_ctx_template_t tmpl, crypto_data_t *signature,
3920Sstevel@tonic-gate     crypto_call_req_t *crq)
3930Sstevel@tonic-gate {
3940Sstevel@tonic-gate 	return (sign_sr_atomic_common(mech, key, data, tmpl, signature, crq,
3950Sstevel@tonic-gate 	    CRYPTO_FG_SIGN_RECOVER_ATOMIC));
3960Sstevel@tonic-gate }
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate int
399*904Smcpowers crypto_sign_recover_init_prov(crypto_provider_t provider,
400*904Smcpowers     crypto_session_id_t sid, crypto_mechanism_t *mech, crypto_key_t *key,
401*904Smcpowers     crypto_ctx_template_t tmpl, crypto_context_t *ctxp, crypto_call_req_t *crq)
4020Sstevel@tonic-gate {
403*904Smcpowers 	int rv;
4040Sstevel@tonic-gate 	crypto_ctx_t *ctx;
4050Sstevel@tonic-gate 	kcf_req_params_t params;
406*904Smcpowers 	kcf_provider_desc_t *pd = provider;
407*904Smcpowers 	kcf_provider_desc_t *real_provider = pd;
4080Sstevel@tonic-gate 
409*904Smcpowers 	ASSERT(KCF_PROV_REFHELD(pd));
410*904Smcpowers 
411*904Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
412*904Smcpowers 		rv = kcf_get_hardware_provider(mech->cm_type,
413*904Smcpowers 		    CRYPTO_MECH_INVALID, CRYPTO_OPS_OFFSET(sign_ops),
414*904Smcpowers 		    CRYPTO_SIGN_OFFSET(sign_recover_init),
415*904Smcpowers 		    CHECK_RESTRICT(crq), pd, &real_provider);
416*904Smcpowers 
417*904Smcpowers 		if (rv != CRYPTO_SUCCESS)
418*904Smcpowers 			return (rv);
419*904Smcpowers 	}
420*904Smcpowers 
421*904Smcpowers 	/* Allocate and initialize the canonical context */
422*904Smcpowers 	if ((ctx = kcf_new_ctx(crq, real_provider, sid)) == NULL) {
423*904Smcpowers 		if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
424*904Smcpowers 			KCF_PROV_REFRELE(real_provider);
4250Sstevel@tonic-gate 		return (CRYPTO_HOST_MEMORY);
426*904Smcpowers 	}
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	KCF_WRAP_SIGN_OPS_PARAMS(&params, KCF_OP_SIGN_RECOVER_INIT, sid, mech,
4290Sstevel@tonic-gate 	    key, NULL, NULL, tmpl);
430*904Smcpowers 	rv = kcf_submit_request(real_provider, ctx, crq, &params, B_FALSE);
431*904Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
432*904Smcpowers 		KCF_PROV_REFRELE(real_provider);
4330Sstevel@tonic-gate 
434*904Smcpowers 	if ((rv == CRYPTO_SUCCESS) || (rv == CRYPTO_QUEUED))
4350Sstevel@tonic-gate 		*ctxp = (crypto_context_t)ctx;
4360Sstevel@tonic-gate 	else {
4370Sstevel@tonic-gate 		/* Release the hold done in kcf_new_ctx(). */
4380Sstevel@tonic-gate 		KCF_CONTEXT_REFRELE((kcf_context_t *)ctx->cc_framework_private);
4390Sstevel@tonic-gate 	}
4400Sstevel@tonic-gate 
441*904Smcpowers 	return (rv);
4420Sstevel@tonic-gate }
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate int
4450Sstevel@tonic-gate crypto_sign_recover_single(crypto_context_t context, crypto_data_t *data,
4460Sstevel@tonic-gate     crypto_data_t *signature, crypto_call_req_t *cr)
4470Sstevel@tonic-gate {
4480Sstevel@tonic-gate 	crypto_ctx_t *ctx = (crypto_ctx_t *)context;
4490Sstevel@tonic-gate 	kcf_context_t *kcf_ctx;
4500Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
4510Sstevel@tonic-gate 	int error;
4520Sstevel@tonic-gate 	kcf_req_params_t params;
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 	if ((ctx == NULL) ||
4550Sstevel@tonic-gate 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
4560Sstevel@tonic-gate 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
4570Sstevel@tonic-gate 		return (CRYPTO_INVALID_CONTEXT);
4580Sstevel@tonic-gate 	}
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	KCF_PROV_REFHOLD(pd);
4610Sstevel@tonic-gate 	KCF_WRAP_SIGN_OPS_PARAMS(&params, KCF_OP_SIGN_RECOVER, 0, NULL,
4620Sstevel@tonic-gate 	    NULL, data, signature, NULL);
4630Sstevel@tonic-gate 	error = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
4640Sstevel@tonic-gate 	KCF_PROV_REFRELE(pd);
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	/* Release the hold done in kcf_new_ctx() during init step. */
4670Sstevel@tonic-gate 	KCF_CONTEXT_COND_RELEASE(error, kcf_ctx);
4680Sstevel@tonic-gate 	return (error);
4690Sstevel@tonic-gate }
470