xref: /onnv-gate/usr/src/uts/common/crypto/api/kcf_mac.c (revision 1808:a9c9c8edb499)
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
5*1808Smcpowers  * Common Development and Distribution License (the "License").
6*1808Smcpowers  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*1808Smcpowers  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/errno.h>
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/kmem.h>
31904Smcpowers #include <sys/sysmacros.h>
320Sstevel@tonic-gate #include <sys/crypto/common.h>
330Sstevel@tonic-gate #include <sys/crypto/impl.h>
340Sstevel@tonic-gate #include <sys/crypto/api.h>
350Sstevel@tonic-gate #include <sys/crypto/spi.h>
360Sstevel@tonic-gate #include <sys/crypto/sched_impl.h>
370Sstevel@tonic-gate 
38904Smcpowers #define	CRYPTO_OPS_OFFSET(f)		offsetof(crypto_ops_t, co_##f)
39904Smcpowers #define	CRYPTO_MAC_OFFSET(f)		offsetof(crypto_mac_ops_t, f)
40904Smcpowers 
410Sstevel@tonic-gate /*
420Sstevel@tonic-gate  * Message authentication codes routines.
430Sstevel@tonic-gate  */
440Sstevel@tonic-gate 
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate  * The following are the possible returned values common to all the routines
470Sstevel@tonic-gate  * below. The applicability of some of these return values depends on the
480Sstevel@tonic-gate  * presence of the arguments.
490Sstevel@tonic-gate  *
500Sstevel@tonic-gate  *	CRYPTO_SUCCESS:	The operation completed successfully.
510Sstevel@tonic-gate  *	CRYPTO_QUEUED:	A request was submitted successfully. The callback
520Sstevel@tonic-gate  *			routine will be called when the operation is done.
530Sstevel@tonic-gate  *	CRYPTO_INVALID_MECH_NUMBER, CRYPTO_INVALID_MECH_PARAM, or
540Sstevel@tonic-gate  *	CRYPTO_INVALID_MECH for problems with the 'mech'.
550Sstevel@tonic-gate  *	CRYPTO_INVALID_DATA for bogus 'data'
560Sstevel@tonic-gate  *	CRYPTO_HOST_MEMORY for failure to allocate memory to handle this work.
570Sstevel@tonic-gate  *	CRYPTO_INVALID_CONTEXT: Not a valid context.
580Sstevel@tonic-gate  *	CRYPTO_BUSY:	Cannot process the request now. Schedule a
590Sstevel@tonic-gate  *			crypto_bufcall(), or try later.
600Sstevel@tonic-gate  *	CRYPTO_NOT_SUPPORTED and CRYPTO_MECH_NOT_SUPPORTED: No provider is
610Sstevel@tonic-gate  *			capable of a function or a mechanism.
620Sstevel@tonic-gate  *	CRYPTO_INVALID_KEY: bogus 'key' argument.
630Sstevel@tonic-gate  *	CRYPTO_INVALID_MAC: bogus 'mac' argument.
640Sstevel@tonic-gate  */
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /*
670Sstevel@tonic-gate  * crypto_mac_prov()
680Sstevel@tonic-gate  *
690Sstevel@tonic-gate  * Arguments:
700Sstevel@tonic-gate  *	mech:	crypto_mechanism_t pointer.
710Sstevel@tonic-gate  *		mech_type is a valid value previously returned by
720Sstevel@tonic-gate  *		crypto_mech2id();
730Sstevel@tonic-gate  *		When the mech's parameter is not NULL, its definition depends
740Sstevel@tonic-gate  *		on the standard definition of the mechanism.
750Sstevel@tonic-gate  *	key:	pointer to a crypto_key_t structure.
760Sstevel@tonic-gate  *	data:	The message to compute the MAC for.
770Sstevel@tonic-gate  *	mac: Storage for the MAC. The length needed depends on the mechanism.
780Sstevel@tonic-gate  *	tmpl:	a crypto_ctx_template_t, opaque template of a context of a
790Sstevel@tonic-gate  *		MAC with the 'mech' using 'key'. 'tmpl' is created by
800Sstevel@tonic-gate  *		a previous call to crypto_create_ctx_template().
810Sstevel@tonic-gate  *	cr:	crypto_call_req_t calling conditions and call back info.
820Sstevel@tonic-gate  *
830Sstevel@tonic-gate  * Description:
840Sstevel@tonic-gate  *	Asynchronously submits a request for, or synchronously performs a
850Sstevel@tonic-gate  *	single-part message authentication of 'data' with the mechanism
860Sstevel@tonic-gate  *	'mech', using *	the key 'key', on the specified provider with
870Sstevel@tonic-gate  *	the specified session id.
880Sstevel@tonic-gate  *	When complete and successful, 'mac' will contain the message
890Sstevel@tonic-gate  *	authentication code.
900Sstevel@tonic-gate  *
910Sstevel@tonic-gate  * Context:
920Sstevel@tonic-gate  *	Process or interrupt, according to the semantics dictated by the 'crq'.
930Sstevel@tonic-gate  *
940Sstevel@tonic-gate  * Returns:
950Sstevel@tonic-gate  *	See comment in the beginning of the file.
960Sstevel@tonic-gate  */
970Sstevel@tonic-gate int
98904Smcpowers crypto_mac_prov(crypto_provider_t provider, crypto_session_id_t sid,
99904Smcpowers     crypto_mechanism_t *mech, crypto_data_t *data, crypto_key_t *key,
100904Smcpowers     crypto_ctx_template_t tmpl, crypto_data_t *mac, crypto_call_req_t *crq)
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate 	kcf_req_params_t params;
103904Smcpowers 	kcf_provider_desc_t *pd = provider;
104904Smcpowers 	kcf_provider_desc_t *real_provider = pd;
105904Smcpowers 	int rv;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	ASSERT(KCF_PROV_REFHELD(pd));
108904Smcpowers 
109904Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
110904Smcpowers 		rv = kcf_get_hardware_provider(mech->cm_type,
111*1808Smcpowers 		    CRYPTO_MECH_INVALID, CHECK_RESTRICT(crq), pd,
112*1808Smcpowers 		    &real_provider, CRYPTO_FG_MAC_ATOMIC);
113904Smcpowers 
114904Smcpowers 		if (rv != CRYPTO_SUCCESS)
115904Smcpowers 			return (rv);
116904Smcpowers 	}
117904Smcpowers 
1180Sstevel@tonic-gate 	KCF_WRAP_MAC_OPS_PARAMS(&params, KCF_OP_ATOMIC, sid, mech, key,
1190Sstevel@tonic-gate 	    data, mac, tmpl);
120904Smcpowers 	rv = kcf_submit_request(real_provider, NULL, crq, &params, B_FALSE);
121904Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
122904Smcpowers 		KCF_PROV_REFRELE(real_provider);
1230Sstevel@tonic-gate 
124904Smcpowers 	return (rv);
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate /*
1280Sstevel@tonic-gate  * Same as crypto_mac_prov(), but relies on the KCF scheduler to choose
1290Sstevel@tonic-gate  * a provider. See crypto_mac() comments for more information.
1300Sstevel@tonic-gate  */
1310Sstevel@tonic-gate int
1320Sstevel@tonic-gate crypto_mac(crypto_mechanism_t *mech, crypto_data_t *data,
1330Sstevel@tonic-gate     crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_data_t *mac,
1340Sstevel@tonic-gate     crypto_call_req_t *crq)
1350Sstevel@tonic-gate {
1360Sstevel@tonic-gate 	int error;
1370Sstevel@tonic-gate 	kcf_mech_entry_t *me;
1380Sstevel@tonic-gate 	kcf_req_params_t params;
1390Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
1400Sstevel@tonic-gate 	kcf_ctx_template_t *ctx_tmpl;
1410Sstevel@tonic-gate 	crypto_spi_ctx_template_t spi_ctx_tmpl = NULL;
1420Sstevel@tonic-gate 	kcf_prov_tried_t *list = NULL;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate retry:
1450Sstevel@tonic-gate 	/* The pd is returned held */
1460Sstevel@tonic-gate 	if ((pd = kcf_get_mech_provider(mech->cm_type, &me, &error,
1470Sstevel@tonic-gate 	    list, CRYPTO_FG_MAC_ATOMIC, CHECK_RESTRICT(crq),
1480Sstevel@tonic-gate 	    data->cd_length)) == NULL) {
1490Sstevel@tonic-gate 		if (list != NULL)
1500Sstevel@tonic-gate 			kcf_free_triedlist(list);
1510Sstevel@tonic-gate 		return (error);
1520Sstevel@tonic-gate 	}
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	/*
1550Sstevel@tonic-gate 	 * For SW providers, check the validity of the context template
1560Sstevel@tonic-gate 	 * It is very rare that the generation number mis-matches, so
1570Sstevel@tonic-gate 	 * is acceptable to fail here, and let the consumer recover by
1580Sstevel@tonic-gate 	 * freeing this tmpl and create a new one for the key and new SW
1590Sstevel@tonic-gate 	 * provider
1600Sstevel@tonic-gate 	 */
1610Sstevel@tonic-gate 	if ((pd->pd_prov_type == CRYPTO_SW_PROVIDER) &&
1620Sstevel@tonic-gate 	    ((ctx_tmpl = (kcf_ctx_template_t *)tmpl) != NULL)) {
1630Sstevel@tonic-gate 		if (ctx_tmpl->ct_generation != me->me_gen_swprov) {
1640Sstevel@tonic-gate 			if (list != NULL)
1650Sstevel@tonic-gate 				kcf_free_triedlist(list);
1660Sstevel@tonic-gate 			KCF_PROV_REFRELE(pd);
1670Sstevel@tonic-gate 			return (CRYPTO_OLD_CTX_TEMPLATE);
1680Sstevel@tonic-gate 		} else {
1690Sstevel@tonic-gate 			spi_ctx_tmpl = ctx_tmpl->ct_prov_tmpl;
1700Sstevel@tonic-gate 		}
1710Sstevel@tonic-gate 	}
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	/* The fast path for SW providers. */
1740Sstevel@tonic-gate 	if (CHECK_FASTPATH(crq, pd)) {
1750Sstevel@tonic-gate 		crypto_mechanism_t lmech;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 		lmech = *mech;
1780Sstevel@tonic-gate 		KCF_SET_PROVIDER_MECHNUM(mech->cm_type, pd, &lmech);
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 		error = KCF_PROV_MAC_ATOMIC(pd, pd->pd_sid, &lmech, key, data,
1810Sstevel@tonic-gate 		    mac, spi_ctx_tmpl, KCF_SWFP_RHNDL(crq));
1820Sstevel@tonic-gate 		KCF_PROV_INCRSTATS(pd, error);
1830Sstevel@tonic-gate 	} else {
1840Sstevel@tonic-gate 		KCF_WRAP_MAC_OPS_PARAMS(&params, KCF_OP_ATOMIC, pd->pd_sid,
1850Sstevel@tonic-gate 		    mech, key, data, mac, spi_ctx_tmpl);
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 		error = kcf_submit_request(pd, NULL, crq, &params,
1880Sstevel@tonic-gate 		    KCF_ISDUALREQ(crq));
1890Sstevel@tonic-gate 	}
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED &&
1920Sstevel@tonic-gate 	    IS_RECOVERABLE(error)) {
1930Sstevel@tonic-gate 		/* Add pd to the linked list of providers tried. */
1940Sstevel@tonic-gate 		if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL)
1950Sstevel@tonic-gate 			goto retry;
1960Sstevel@tonic-gate 	}
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	if (list != NULL)
1990Sstevel@tonic-gate 		kcf_free_triedlist(list);
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	KCF_PROV_REFRELE(pd);
2020Sstevel@tonic-gate 	return (error);
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate /*
2060Sstevel@tonic-gate  * Single part operation to compute the MAC corresponding to the specified
2070Sstevel@tonic-gate  * 'data' and to verify that it matches the MAC specified by 'mac'.
2080Sstevel@tonic-gate  * The other arguments are the same as the function crypto_mac_prov().
2090Sstevel@tonic-gate  */
2100Sstevel@tonic-gate int
211904Smcpowers crypto_mac_verify_prov(crypto_provider_t provider, crypto_session_id_t sid,
212904Smcpowers     crypto_mechanism_t *mech, crypto_data_t *data, crypto_key_t *key,
213904Smcpowers     crypto_ctx_template_t tmpl, crypto_data_t *mac, crypto_call_req_t *crq)
2140Sstevel@tonic-gate {
2150Sstevel@tonic-gate 	kcf_req_params_t params;
216904Smcpowers 	kcf_provider_desc_t *pd = provider;
217904Smcpowers 	kcf_provider_desc_t *real_provider = pd;
218904Smcpowers 	int rv;
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	ASSERT(KCF_PROV_REFHELD(pd));
221904Smcpowers 
222904Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
223904Smcpowers 		rv = kcf_get_hardware_provider(mech->cm_type,
224*1808Smcpowers 		    CRYPTO_MECH_INVALID, CHECK_RESTRICT(crq), pd,
225*1808Smcpowers 		    &real_provider, CRYPTO_FG_MAC_ATOMIC);
226904Smcpowers 
227904Smcpowers 		if (rv != CRYPTO_SUCCESS)
228904Smcpowers 			return (rv);
229904Smcpowers 	}
230904Smcpowers 
2310Sstevel@tonic-gate 	KCF_WRAP_MAC_OPS_PARAMS(&params, KCF_OP_MAC_VERIFY_ATOMIC, sid, mech,
2320Sstevel@tonic-gate 	    key, data, mac, tmpl);
233904Smcpowers 	rv = kcf_submit_request(real_provider, NULL, crq, &params, B_FALSE);
234904Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
235904Smcpowers 		KCF_PROV_REFRELE(real_provider);
2360Sstevel@tonic-gate 
237904Smcpowers 	return (rv);
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate /*
2410Sstevel@tonic-gate  * Same as crypto_mac_verify_prov(), but relies on the KCF scheduler to choose
2420Sstevel@tonic-gate  * a provider. See crypto_mac_verify_prov() comments for more information.
2430Sstevel@tonic-gate  */
2440Sstevel@tonic-gate int
2450Sstevel@tonic-gate crypto_mac_verify(crypto_mechanism_t *mech, crypto_data_t *data,
2460Sstevel@tonic-gate     crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_data_t *mac,
2470Sstevel@tonic-gate     crypto_call_req_t *crq)
2480Sstevel@tonic-gate {
2490Sstevel@tonic-gate 	int error;
2500Sstevel@tonic-gate 	kcf_mech_entry_t *me;
2510Sstevel@tonic-gate 	kcf_req_params_t params;
2520Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
2530Sstevel@tonic-gate 	kcf_ctx_template_t *ctx_tmpl;
2540Sstevel@tonic-gate 	crypto_spi_ctx_template_t spi_ctx_tmpl = NULL;
2550Sstevel@tonic-gate 	kcf_prov_tried_t *list = NULL;
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate retry:
2580Sstevel@tonic-gate 	/* The pd is returned held */
2590Sstevel@tonic-gate 	if ((pd = kcf_get_mech_provider(mech->cm_type, &me, &error,
2600Sstevel@tonic-gate 	    list, CRYPTO_FG_MAC_ATOMIC, CHECK_RESTRICT(crq),
2610Sstevel@tonic-gate 	    data->cd_length)) == NULL) {
2620Sstevel@tonic-gate 		if (list != NULL)
2630Sstevel@tonic-gate 			kcf_free_triedlist(list);
2640Sstevel@tonic-gate 		return (error);
2650Sstevel@tonic-gate 	}
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	/*
2680Sstevel@tonic-gate 	 * For SW providers, check the validity of the context template
2690Sstevel@tonic-gate 	 * It is very rare that the generation number mis-matches, so
2700Sstevel@tonic-gate 	 * is acceptable to fail here, and let the consumer recover by
2710Sstevel@tonic-gate 	 * freeing this tmpl and create a new one for the key and new SW
2720Sstevel@tonic-gate 	 * provider
2730Sstevel@tonic-gate 	 */
2740Sstevel@tonic-gate 	if ((pd->pd_prov_type == CRYPTO_SW_PROVIDER) &&
2750Sstevel@tonic-gate 	    ((ctx_tmpl = (kcf_ctx_template_t *)tmpl) != NULL)) {
2760Sstevel@tonic-gate 		if (ctx_tmpl->ct_generation != me->me_gen_swprov) {
2770Sstevel@tonic-gate 			if (list != NULL)
2780Sstevel@tonic-gate 				kcf_free_triedlist(list);
2790Sstevel@tonic-gate 			KCF_PROV_REFRELE(pd);
2800Sstevel@tonic-gate 			return (CRYPTO_OLD_CTX_TEMPLATE);
2810Sstevel@tonic-gate 		} else {
2820Sstevel@tonic-gate 			spi_ctx_tmpl = ctx_tmpl->ct_prov_tmpl;
2830Sstevel@tonic-gate 		}
2840Sstevel@tonic-gate 	}
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	/* The fast path for SW providers. */
2870Sstevel@tonic-gate 	if (CHECK_FASTPATH(crq, pd)) {
2880Sstevel@tonic-gate 		crypto_mechanism_t lmech;
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 		lmech = *mech;
2910Sstevel@tonic-gate 		KCF_SET_PROVIDER_MECHNUM(mech->cm_type, pd, &lmech);
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 		error = KCF_PROV_MAC_VERIFY_ATOMIC(pd, pd->pd_sid, &lmech, key,
2940Sstevel@tonic-gate 		    data, mac, spi_ctx_tmpl, KCF_SWFP_RHNDL(crq));
2950Sstevel@tonic-gate 		KCF_PROV_INCRSTATS(pd, error);
2960Sstevel@tonic-gate 	} else {
2970Sstevel@tonic-gate 		KCF_WRAP_MAC_OPS_PARAMS(&params, KCF_OP_MAC_VERIFY_ATOMIC,
2980Sstevel@tonic-gate 		    pd->pd_sid, mech, key, data, mac, spi_ctx_tmpl);
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 		error = kcf_submit_request(pd, NULL, crq, &params,
3010Sstevel@tonic-gate 		    KCF_ISDUALREQ(crq));
3020Sstevel@tonic-gate 	}
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED &&
3050Sstevel@tonic-gate 	    IS_RECOVERABLE(error)) {
3060Sstevel@tonic-gate 		/* Add pd to the linked list of providers tried. */
3070Sstevel@tonic-gate 		if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL)
3080Sstevel@tonic-gate 			goto retry;
3090Sstevel@tonic-gate 	}
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	if (list != NULL)
3120Sstevel@tonic-gate 		kcf_free_triedlist(list);
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	KCF_PROV_REFRELE(pd);
3150Sstevel@tonic-gate 	return (error);
3160Sstevel@tonic-gate }
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate /*
3200Sstevel@tonic-gate  * crypto_mac_init_prov()
3210Sstevel@tonic-gate  *
3220Sstevel@tonic-gate  * Arguments:
3230Sstevel@tonic-gate  *	pd:	pointer to the descriptor of the provider to use for this
3240Sstevel@tonic-gate  *		operation.
3250Sstevel@tonic-gate  *	sid:	provider session id.
3260Sstevel@tonic-gate  *	mech:	crypto_mechanism_t pointer.
3270Sstevel@tonic-gate  *		mech_type is a valid value previously returned by
3280Sstevel@tonic-gate  *		crypto_mech2id();
3290Sstevel@tonic-gate  *		When the mech's parameter is not NULL, its definition depends
3300Sstevel@tonic-gate  *		on the standard definition of the mechanism.
3310Sstevel@tonic-gate  *	key:	pointer to a crypto_key_t structure.
3320Sstevel@tonic-gate  *	tmpl:	a crypto_ctx_template_t, opaque template of a context of a
3330Sstevel@tonic-gate  *		MAC with the 'mech' using 'key'. 'tmpl' is created by
3340Sstevel@tonic-gate  *		a previous call to crypto_create_ctx_template().
3350Sstevel@tonic-gate  *	ctxp:	Pointer to a crypto_context_t.
3360Sstevel@tonic-gate  *	cr:	crypto_call_req_t calling conditions and call back info.
3370Sstevel@tonic-gate  *
3380Sstevel@tonic-gate  * Description:
3390Sstevel@tonic-gate  *	Asynchronously submits a request for, or synchronously performs the
3400Sstevel@tonic-gate  *	initialization of a MAC operation on the specified provider with
3410Sstevel@tonic-gate  *	the specified session.
3420Sstevel@tonic-gate  *	When possible and applicable, will internally use the pre-computed MAC
3430Sstevel@tonic-gate  *	context from the context template, tmpl.
3440Sstevel@tonic-gate  *	When complete and successful, 'ctxp' will contain a crypto_context_t
3450Sstevel@tonic-gate  *	valid for later calls to mac_update() and mac_final().
3460Sstevel@tonic-gate  *	The caller should hold a reference on the specified provider
3470Sstevel@tonic-gate  *	descriptor before calling this function.
3480Sstevel@tonic-gate  *
3490Sstevel@tonic-gate  * Context:
3500Sstevel@tonic-gate  *	Process or interrupt, according to the semantics dictated by the 'cr'.
3510Sstevel@tonic-gate  *
3520Sstevel@tonic-gate  * Returns:
3530Sstevel@tonic-gate  *	See comment in the beginning of the file.
3540Sstevel@tonic-gate  */
3550Sstevel@tonic-gate int
356904Smcpowers crypto_mac_init_prov(crypto_provider_t provider, crypto_session_id_t sid,
3570Sstevel@tonic-gate     crypto_mechanism_t *mech, crypto_key_t *key, crypto_spi_ctx_template_t tmpl,
3580Sstevel@tonic-gate     crypto_context_t *ctxp, crypto_call_req_t *crq)
3590Sstevel@tonic-gate {
360904Smcpowers 	int rv;
3610Sstevel@tonic-gate 	crypto_ctx_t *ctx;
3620Sstevel@tonic-gate 	kcf_req_params_t params;
363904Smcpowers 	kcf_provider_desc_t *pd = provider;
364904Smcpowers 	kcf_provider_desc_t *real_provider = pd;
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	ASSERT(KCF_PROV_REFHELD(pd));
3670Sstevel@tonic-gate 
368904Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
369904Smcpowers 		rv = kcf_get_hardware_provider(mech->cm_type,
370*1808Smcpowers 		    CRYPTO_MECH_INVALID, CHECK_RESTRICT(crq), pd,
371*1808Smcpowers 		    &real_provider, CRYPTO_FG_MAC);
372904Smcpowers 
373904Smcpowers 		if (rv != CRYPTO_SUCCESS)
374904Smcpowers 			return (rv);
375904Smcpowers 	}
376904Smcpowers 
377904Smcpowers 	/* Allocate and initialize the canonical context */
378904Smcpowers 	if ((ctx = kcf_new_ctx(crq, real_provider, sid)) == NULL) {
379904Smcpowers 		if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
380904Smcpowers 			KCF_PROV_REFRELE(real_provider);
3810Sstevel@tonic-gate 		return (CRYPTO_HOST_MEMORY);
382904Smcpowers 	}
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	/* The fast path for SW providers. */
3850Sstevel@tonic-gate 	if (CHECK_FASTPATH(crq, pd)) {
3860Sstevel@tonic-gate 		crypto_mechanism_t lmech;
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 		lmech = *mech;
389904Smcpowers 		KCF_SET_PROVIDER_MECHNUM(mech->cm_type, real_provider, &lmech);
390904Smcpowers 		rv = KCF_PROV_MAC_INIT(real_provider, ctx, &lmech, key, tmpl,
3910Sstevel@tonic-gate 		    KCF_SWFP_RHNDL(crq));
392904Smcpowers 		KCF_PROV_INCRSTATS(pd, rv);
3930Sstevel@tonic-gate 	} else {
3940Sstevel@tonic-gate 		KCF_WRAP_MAC_OPS_PARAMS(&params, KCF_OP_INIT, sid, mech, key,
3950Sstevel@tonic-gate 		    NULL, NULL, tmpl);
396904Smcpowers 		rv = kcf_submit_request(real_provider, ctx, crq, &params,
397904Smcpowers 		    B_FALSE);
3980Sstevel@tonic-gate 	}
3990Sstevel@tonic-gate 
400904Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
401904Smcpowers 		KCF_PROV_REFRELE(real_provider);
402904Smcpowers 
403904Smcpowers 	if ((rv == CRYPTO_SUCCESS) || (rv == CRYPTO_QUEUED))
4040Sstevel@tonic-gate 		*ctxp = (crypto_context_t)ctx;
4050Sstevel@tonic-gate 	else {
4060Sstevel@tonic-gate 		/* Release the hold done in kcf_new_ctx(). */
4070Sstevel@tonic-gate 		KCF_CONTEXT_REFRELE((kcf_context_t *)ctx->cc_framework_private);
4080Sstevel@tonic-gate 	}
4090Sstevel@tonic-gate 
410904Smcpowers 	return (rv);
4110Sstevel@tonic-gate }
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate /*
4140Sstevel@tonic-gate  * Same as crypto_mac_init_prov(), but relies on the KCF scheduler to
4150Sstevel@tonic-gate  * choose a provider. See crypto_mac_init_prov() comments for more
4160Sstevel@tonic-gate  * information.
4170Sstevel@tonic-gate  */
4180Sstevel@tonic-gate int
4190Sstevel@tonic-gate crypto_mac_init(crypto_mechanism_t *mech, crypto_key_t *key,
4200Sstevel@tonic-gate     crypto_ctx_template_t tmpl, crypto_context_t *ctxp,
4210Sstevel@tonic-gate     crypto_call_req_t  *crq)
4220Sstevel@tonic-gate {
4230Sstevel@tonic-gate 	int error;
4240Sstevel@tonic-gate 	kcf_mech_entry_t *me;
4250Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
4260Sstevel@tonic-gate 	kcf_ctx_template_t *ctx_tmpl;
4270Sstevel@tonic-gate 	crypto_spi_ctx_template_t spi_ctx_tmpl = NULL;
4280Sstevel@tonic-gate 	kcf_prov_tried_t *list = NULL;
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate retry:
4310Sstevel@tonic-gate 	/* The pd is returned held */
4320Sstevel@tonic-gate 	if ((pd = kcf_get_mech_provider(mech->cm_type, &me, &error,
4330Sstevel@tonic-gate 	    list, CRYPTO_FG_MAC, CHECK_RESTRICT(crq), 0)) == NULL) {
4340Sstevel@tonic-gate 		if (list != NULL)
4350Sstevel@tonic-gate 			kcf_free_triedlist(list);
4360Sstevel@tonic-gate 		return (error);
4370Sstevel@tonic-gate 	}
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	/*
4400Sstevel@tonic-gate 	 * For SW providers, check the validity of the context template
4410Sstevel@tonic-gate 	 * It is very rare that the generation number mis-matches, so
4420Sstevel@tonic-gate 	 * is acceptable to fail here, and let the consumer recover by
4430Sstevel@tonic-gate 	 * freeing this tmpl and create a new one for the key and new SW
4440Sstevel@tonic-gate 	 * provider
4450Sstevel@tonic-gate 	 */
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 	if ((pd->pd_prov_type == CRYPTO_SW_PROVIDER) &&
4480Sstevel@tonic-gate 	    ((ctx_tmpl = (kcf_ctx_template_t *)tmpl) != NULL)) {
4490Sstevel@tonic-gate 		if (ctx_tmpl->ct_generation != me->me_gen_swprov) {
4500Sstevel@tonic-gate 			if (list != NULL)
4510Sstevel@tonic-gate 				kcf_free_triedlist(list);
4520Sstevel@tonic-gate 			KCF_PROV_REFRELE(pd);
4530Sstevel@tonic-gate 			return (CRYPTO_OLD_CTX_TEMPLATE);
4540Sstevel@tonic-gate 		} else {
4550Sstevel@tonic-gate 			spi_ctx_tmpl = ctx_tmpl->ct_prov_tmpl;
4560Sstevel@tonic-gate 		}
4570Sstevel@tonic-gate 	}
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	error = crypto_mac_init_prov(pd, pd->pd_sid, mech, key, spi_ctx_tmpl,
4600Sstevel@tonic-gate 	    ctxp, crq);
4610Sstevel@tonic-gate 	if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED &&
4620Sstevel@tonic-gate 	    IS_RECOVERABLE(error)) {
4630Sstevel@tonic-gate 		/* Add pd to the linked list of providers tried. */
4640Sstevel@tonic-gate 		if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL)
4650Sstevel@tonic-gate 			goto retry;
4660Sstevel@tonic-gate 	}
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	if (list != NULL)
4690Sstevel@tonic-gate 		kcf_free_triedlist(list);
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	KCF_PROV_REFRELE(pd);
4720Sstevel@tonic-gate 	return (error);
4730Sstevel@tonic-gate }
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate /*
4760Sstevel@tonic-gate  * crypto_mac_update()
4770Sstevel@tonic-gate  *
4780Sstevel@tonic-gate  * Arguments:
4790Sstevel@tonic-gate  *	context: A crypto_context_t initialized by mac_init().
4800Sstevel@tonic-gate  *	data: The message part to be MAC'ed
4810Sstevel@tonic-gate  *	cr:	crypto_call_req_t calling conditions and call back info.
4820Sstevel@tonic-gate  *
4830Sstevel@tonic-gate  * Description:
4840Sstevel@tonic-gate  *	Asynchronously submits a request for, or synchronously performs a
4850Sstevel@tonic-gate  *	part of a MAC operation.
4860Sstevel@tonic-gate  *
4870Sstevel@tonic-gate  * Context:
4880Sstevel@tonic-gate  *	Process or interrupt, according to the semantics dictated by the 'cr'.
4890Sstevel@tonic-gate  *
4900Sstevel@tonic-gate  * Returns:
4910Sstevel@tonic-gate  *	See comment in the beginning of the file.
4920Sstevel@tonic-gate  */
4930Sstevel@tonic-gate int
4940Sstevel@tonic-gate crypto_mac_update(crypto_context_t context, crypto_data_t *data,
4950Sstevel@tonic-gate     crypto_call_req_t *cr)
4960Sstevel@tonic-gate {
4970Sstevel@tonic-gate 	crypto_ctx_t *ctx = (crypto_ctx_t *)context;
4980Sstevel@tonic-gate 	kcf_context_t *kcf_ctx;
4990Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
5000Sstevel@tonic-gate 	kcf_req_params_t params;
501904Smcpowers 	int rv;
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	if ((ctx == NULL) ||
5040Sstevel@tonic-gate 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
5050Sstevel@tonic-gate 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
5060Sstevel@tonic-gate 		return (CRYPTO_INVALID_CONTEXT);
5070Sstevel@tonic-gate 	}
5080Sstevel@tonic-gate 
509904Smcpowers 	ASSERT(pd->pd_prov_type != CRYPTO_LOGICAL_PROVIDER);
5100Sstevel@tonic-gate 	KCF_PROV_REFHOLD(pd);
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	/* The fast path for SW providers. */
5130Sstevel@tonic-gate 	if (CHECK_FASTPATH(cr, pd)) {
514904Smcpowers 		rv = KCF_PROV_MAC_UPDATE(pd, ctx, data, NULL);
515904Smcpowers 		KCF_PROV_INCRSTATS(pd, rv);
5160Sstevel@tonic-gate 	} else {
517904Smcpowers 		KCF_WRAP_MAC_OPS_PARAMS(&params, KCF_OP_UPDATE,
518904Smcpowers 		    ctx->cc_session, NULL, NULL, data, NULL, NULL);
519904Smcpowers 		rv = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
5200Sstevel@tonic-gate 	}
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	KCF_PROV_REFRELE(pd);
523904Smcpowers 	return (rv);
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate /*
5270Sstevel@tonic-gate  * crypto_mac_final()
5280Sstevel@tonic-gate  *
5290Sstevel@tonic-gate  * Arguments:
5300Sstevel@tonic-gate  *	context: A crypto_context_t initialized by mac_init().
5310Sstevel@tonic-gate  *	mac: Storage for the message authentication code.
5320Sstevel@tonic-gate  *	cr:	crypto_call_req_t calling conditions and call back info.
5330Sstevel@tonic-gate  *
5340Sstevel@tonic-gate  * Description:
5350Sstevel@tonic-gate  *	Asynchronously submits a request for, or synchronously performs a
5360Sstevel@tonic-gate  *	part of a message authentication operation.
5370Sstevel@tonic-gate  *
5380Sstevel@tonic-gate  * Context:
5390Sstevel@tonic-gate  *	Process or interrupt, according to the semantics dictated by the 'cr'.
5400Sstevel@tonic-gate  *
5410Sstevel@tonic-gate  * Returns:
5420Sstevel@tonic-gate  *	See comment in the beginning of the file.
5430Sstevel@tonic-gate  */
5440Sstevel@tonic-gate int
5450Sstevel@tonic-gate crypto_mac_final(crypto_context_t context, crypto_data_t *mac,
5460Sstevel@tonic-gate     crypto_call_req_t *cr)
5470Sstevel@tonic-gate {
5480Sstevel@tonic-gate 	crypto_ctx_t *ctx = (crypto_ctx_t *)context;
5490Sstevel@tonic-gate 	kcf_context_t *kcf_ctx;
5500Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
5510Sstevel@tonic-gate 	kcf_req_params_t params;
552904Smcpowers 	int rv;
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 	if ((ctx == NULL) ||
5550Sstevel@tonic-gate 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
5560Sstevel@tonic-gate 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
5570Sstevel@tonic-gate 		return (CRYPTO_INVALID_CONTEXT);
5580Sstevel@tonic-gate 	}
5590Sstevel@tonic-gate 
560904Smcpowers 	ASSERT(pd->pd_prov_type != CRYPTO_LOGICAL_PROVIDER);
5610Sstevel@tonic-gate 	KCF_PROV_REFHOLD(pd);
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	/* The fast path for SW providers. */
5640Sstevel@tonic-gate 	if (CHECK_FASTPATH(cr, pd)) {
565904Smcpowers 		rv = KCF_PROV_MAC_FINAL(pd, ctx, mac, NULL);
566904Smcpowers 		KCF_PROV_INCRSTATS(pd, rv);
5670Sstevel@tonic-gate 	} else {
568904Smcpowers 		KCF_WRAP_MAC_OPS_PARAMS(&params, KCF_OP_FINAL,
569904Smcpowers 		    ctx->cc_session, NULL, NULL, NULL, mac, NULL);
570904Smcpowers 		rv = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
5710Sstevel@tonic-gate 	}
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	KCF_PROV_REFRELE(pd);
5740Sstevel@tonic-gate 	/* Release the hold done in kcf_new_ctx() during init step. */
575904Smcpowers 	KCF_CONTEXT_COND_RELEASE(rv, kcf_ctx);
576904Smcpowers 	return (rv);
5770Sstevel@tonic-gate }
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate /*
5800Sstevel@tonic-gate  * See comments for crypto_mac_update() and crypto_mac_final().
5810Sstevel@tonic-gate  */
5820Sstevel@tonic-gate int
5830Sstevel@tonic-gate crypto_mac_single(crypto_context_t context, crypto_data_t *data,
5840Sstevel@tonic-gate     crypto_data_t *mac, crypto_call_req_t *cr)
5850Sstevel@tonic-gate {
5860Sstevel@tonic-gate 	crypto_ctx_t *ctx = (crypto_ctx_t *)context;
5870Sstevel@tonic-gate 	kcf_context_t *kcf_ctx;
5880Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
5890Sstevel@tonic-gate 	int error;
5900Sstevel@tonic-gate 	kcf_req_params_t params;
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 	if ((ctx == NULL) ||
5940Sstevel@tonic-gate 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
5950Sstevel@tonic-gate 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
5960Sstevel@tonic-gate 		return (CRYPTO_INVALID_CONTEXT);
5970Sstevel@tonic-gate 	}
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 	KCF_PROV_REFHOLD(pd);
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 	/* The fast path for SW providers. */
6020Sstevel@tonic-gate 	if (CHECK_FASTPATH(cr, pd)) {
6030Sstevel@tonic-gate 		error = KCF_PROV_MAC(pd, ctx, data, mac, NULL);
6040Sstevel@tonic-gate 		KCF_PROV_INCRSTATS(pd, error);
6050Sstevel@tonic-gate 	} else {
6060Sstevel@tonic-gate 		KCF_WRAP_MAC_OPS_PARAMS(&params, KCF_OP_SINGLE, pd->pd_sid,
6070Sstevel@tonic-gate 		    NULL, NULL, data, mac, NULL);
6080Sstevel@tonic-gate 		error = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
6090Sstevel@tonic-gate 	}
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	KCF_PROV_REFRELE(pd);
6120Sstevel@tonic-gate 	/* Release the hold done in kcf_new_ctx() during init step. */
6130Sstevel@tonic-gate 	KCF_CONTEXT_COND_RELEASE(error, kcf_ctx);
6140Sstevel@tonic-gate 	return (error);
6150Sstevel@tonic-gate }
616