xref: /onnv-gate/usr/src/uts/common/crypto/api/kcf_mac.c (revision 11304:3092d1e303d6)
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
51808Smcpowers  * Common Development and Distribution License (the "License").
61808Smcpowers  * 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 /*
2210444SVladimir.Kotal@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/errno.h>
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/kmem.h>
29904Smcpowers #include <sys/sysmacros.h>
300Sstevel@tonic-gate #include <sys/crypto/common.h>
310Sstevel@tonic-gate #include <sys/crypto/impl.h>
320Sstevel@tonic-gate #include <sys/crypto/api.h>
330Sstevel@tonic-gate #include <sys/crypto/spi.h>
340Sstevel@tonic-gate #include <sys/crypto/sched_impl.h>
350Sstevel@tonic-gate 
36904Smcpowers #define	CRYPTO_OPS_OFFSET(f)		offsetof(crypto_ops_t, co_##f)
37904Smcpowers #define	CRYPTO_MAC_OFFSET(f)		offsetof(crypto_mac_ops_t, f)
38904Smcpowers 
390Sstevel@tonic-gate /*
400Sstevel@tonic-gate  * Message authentication codes routines.
410Sstevel@tonic-gate  */
420Sstevel@tonic-gate 
430Sstevel@tonic-gate /*
440Sstevel@tonic-gate  * The following are the possible returned values common to all the routines
450Sstevel@tonic-gate  * below. The applicability of some of these return values depends on the
460Sstevel@tonic-gate  * presence of the arguments.
470Sstevel@tonic-gate  *
480Sstevel@tonic-gate  *	CRYPTO_SUCCESS:	The operation completed successfully.
490Sstevel@tonic-gate  *	CRYPTO_QUEUED:	A request was submitted successfully. The callback
500Sstevel@tonic-gate  *			routine will be called when the operation is done.
510Sstevel@tonic-gate  *	CRYPTO_INVALID_MECH_NUMBER, CRYPTO_INVALID_MECH_PARAM, or
520Sstevel@tonic-gate  *	CRYPTO_INVALID_MECH for problems with the 'mech'.
530Sstevel@tonic-gate  *	CRYPTO_INVALID_DATA for bogus 'data'
540Sstevel@tonic-gate  *	CRYPTO_HOST_MEMORY for failure to allocate memory to handle this work.
550Sstevel@tonic-gate  *	CRYPTO_INVALID_CONTEXT: Not a valid context.
560Sstevel@tonic-gate  *	CRYPTO_BUSY:	Cannot process the request now. Schedule a
570Sstevel@tonic-gate  *			crypto_bufcall(), or try later.
580Sstevel@tonic-gate  *	CRYPTO_NOT_SUPPORTED and CRYPTO_MECH_NOT_SUPPORTED: No provider is
590Sstevel@tonic-gate  *			capable of a function or a mechanism.
600Sstevel@tonic-gate  *	CRYPTO_INVALID_KEY: bogus 'key' argument.
610Sstevel@tonic-gate  *	CRYPTO_INVALID_MAC: bogus 'mac' argument.
620Sstevel@tonic-gate  */
630Sstevel@tonic-gate 
640Sstevel@tonic-gate /*
650Sstevel@tonic-gate  * crypto_mac_prov()
660Sstevel@tonic-gate  *
670Sstevel@tonic-gate  * Arguments:
680Sstevel@tonic-gate  *	mech:	crypto_mechanism_t pointer.
690Sstevel@tonic-gate  *		mech_type is a valid value previously returned by
700Sstevel@tonic-gate  *		crypto_mech2id();
710Sstevel@tonic-gate  *		When the mech's parameter is not NULL, its definition depends
720Sstevel@tonic-gate  *		on the standard definition of the mechanism.
730Sstevel@tonic-gate  *	key:	pointer to a crypto_key_t structure.
740Sstevel@tonic-gate  *	data:	The message to compute the MAC for.
750Sstevel@tonic-gate  *	mac: Storage for the MAC. The length needed depends on the mechanism.
760Sstevel@tonic-gate  *	tmpl:	a crypto_ctx_template_t, opaque template of a context of a
770Sstevel@tonic-gate  *		MAC with the 'mech' using 'key'. 'tmpl' is created by
780Sstevel@tonic-gate  *		a previous call to crypto_create_ctx_template().
790Sstevel@tonic-gate  *	cr:	crypto_call_req_t calling conditions and call back info.
800Sstevel@tonic-gate  *
810Sstevel@tonic-gate  * Description:
820Sstevel@tonic-gate  *	Asynchronously submits a request for, or synchronously performs a
830Sstevel@tonic-gate  *	single-part message authentication of 'data' with the mechanism
840Sstevel@tonic-gate  *	'mech', using *	the key 'key', on the specified provider with
850Sstevel@tonic-gate  *	the specified session id.
860Sstevel@tonic-gate  *	When complete and successful, 'mac' will contain the message
870Sstevel@tonic-gate  *	authentication code.
880Sstevel@tonic-gate  *
890Sstevel@tonic-gate  * Context:
900Sstevel@tonic-gate  *	Process or interrupt, according to the semantics dictated by the 'crq'.
910Sstevel@tonic-gate  *
920Sstevel@tonic-gate  * Returns:
930Sstevel@tonic-gate  *	See comment in the beginning of the file.
940Sstevel@tonic-gate  */
950Sstevel@tonic-gate int
96904Smcpowers crypto_mac_prov(crypto_provider_t provider, crypto_session_id_t sid,
97904Smcpowers     crypto_mechanism_t *mech, crypto_data_t *data, crypto_key_t *key,
98904Smcpowers     crypto_ctx_template_t tmpl, crypto_data_t *mac, crypto_call_req_t *crq)
990Sstevel@tonic-gate {
1000Sstevel@tonic-gate 	kcf_req_params_t params;
101904Smcpowers 	kcf_provider_desc_t *pd = provider;
102904Smcpowers 	kcf_provider_desc_t *real_provider = pd;
103904Smcpowers 	int rv;
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	ASSERT(KCF_PROV_REFHELD(pd));
106904Smcpowers 
107904Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
10810444SVladimir.Kotal@Sun.COM 		rv = kcf_get_hardware_provider(mech->cm_type, key,
10910444SVladimir.Kotal@Sun.COM 		    CRYPTO_MECH_INVALID, NULL, CHECK_RESTRICT(crq), pd,
1101808Smcpowers 		    &real_provider, CRYPTO_FG_MAC_ATOMIC);
111904Smcpowers 
112904Smcpowers 		if (rv != CRYPTO_SUCCESS)
113904Smcpowers 			return (rv);
114904Smcpowers 	}
115904Smcpowers 
1160Sstevel@tonic-gate 	KCF_WRAP_MAC_OPS_PARAMS(&params, KCF_OP_ATOMIC, sid, mech, key,
1170Sstevel@tonic-gate 	    data, mac, tmpl);
118904Smcpowers 	rv = kcf_submit_request(real_provider, NULL, crq, &params, B_FALSE);
119904Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
120904Smcpowers 		KCF_PROV_REFRELE(real_provider);
1210Sstevel@tonic-gate 
122904Smcpowers 	return (rv);
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate /*
1260Sstevel@tonic-gate  * Same as crypto_mac_prov(), but relies on the KCF scheduler to choose
1270Sstevel@tonic-gate  * a provider. See crypto_mac() comments for more information.
1280Sstevel@tonic-gate  */
1290Sstevel@tonic-gate int
1300Sstevel@tonic-gate crypto_mac(crypto_mechanism_t *mech, crypto_data_t *data,
1310Sstevel@tonic-gate     crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_data_t *mac,
1320Sstevel@tonic-gate     crypto_call_req_t *crq)
1330Sstevel@tonic-gate {
1340Sstevel@tonic-gate 	int error;
1350Sstevel@tonic-gate 	kcf_mech_entry_t *me;
1360Sstevel@tonic-gate 	kcf_req_params_t params;
1370Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
1380Sstevel@tonic-gate 	kcf_ctx_template_t *ctx_tmpl;
1390Sstevel@tonic-gate 	crypto_spi_ctx_template_t spi_ctx_tmpl = NULL;
1400Sstevel@tonic-gate 	kcf_prov_tried_t *list = NULL;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate retry:
1430Sstevel@tonic-gate 	/* The pd is returned held */
14410444SVladimir.Kotal@Sun.COM 	if ((pd = kcf_get_mech_provider(mech->cm_type, key, &me, &error,
1450Sstevel@tonic-gate 	    list, CRYPTO_FG_MAC_ATOMIC, CHECK_RESTRICT(crq),
1460Sstevel@tonic-gate 	    data->cd_length)) == NULL) {
1470Sstevel@tonic-gate 		if (list != NULL)
1480Sstevel@tonic-gate 			kcf_free_triedlist(list);
1490Sstevel@tonic-gate 		return (error);
1500Sstevel@tonic-gate 	}
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	/*
1530Sstevel@tonic-gate 	 * For SW providers, check the validity of the context template
1540Sstevel@tonic-gate 	 * It is very rare that the generation number mis-matches, so
1550Sstevel@tonic-gate 	 * is acceptable to fail here, and let the consumer recover by
1560Sstevel@tonic-gate 	 * freeing this tmpl and create a new one for the key and new SW
1570Sstevel@tonic-gate 	 * provider
1580Sstevel@tonic-gate 	 */
1590Sstevel@tonic-gate 	if ((pd->pd_prov_type == CRYPTO_SW_PROVIDER) &&
1600Sstevel@tonic-gate 	    ((ctx_tmpl = (kcf_ctx_template_t *)tmpl) != NULL)) {
1610Sstevel@tonic-gate 		if (ctx_tmpl->ct_generation != me->me_gen_swprov) {
1620Sstevel@tonic-gate 			if (list != NULL)
1630Sstevel@tonic-gate 				kcf_free_triedlist(list);
1640Sstevel@tonic-gate 			KCF_PROV_REFRELE(pd);
1650Sstevel@tonic-gate 			return (CRYPTO_OLD_CTX_TEMPLATE);
1660Sstevel@tonic-gate 		} else {
1670Sstevel@tonic-gate 			spi_ctx_tmpl = ctx_tmpl->ct_prov_tmpl;
1680Sstevel@tonic-gate 		}
1690Sstevel@tonic-gate 	}
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	/* The fast path for SW providers. */
1720Sstevel@tonic-gate 	if (CHECK_FASTPATH(crq, pd)) {
1730Sstevel@tonic-gate 		crypto_mechanism_t lmech;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 		lmech = *mech;
1760Sstevel@tonic-gate 		KCF_SET_PROVIDER_MECHNUM(mech->cm_type, pd, &lmech);
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 		error = KCF_PROV_MAC_ATOMIC(pd, pd->pd_sid, &lmech, key, data,
1790Sstevel@tonic-gate 		    mac, spi_ctx_tmpl, KCF_SWFP_RHNDL(crq));
1800Sstevel@tonic-gate 		KCF_PROV_INCRSTATS(pd, error);
1810Sstevel@tonic-gate 	} else {
1824072Skrishna 		if (pd->pd_prov_type == CRYPTO_HW_PROVIDER &&
183*11304SJanie.Lu@Sun.COM 		    (pd->pd_flags & CRYPTO_HMAC_NO_UPDATE) &&
184*11304SJanie.Lu@Sun.COM 		    (data->cd_length > pd->pd_hmac_limit)) {
1854072Skrishna 			/*
1864072Skrishna 			 * XXX - We need a check to see if this is indeed
1874072Skrishna 			 * a HMAC. So far, all kernel clients use
1884072Skrishna 			 * this interface only for HMAC. So, this is fine
1894072Skrishna 			 * for now.
1904072Skrishna 			 */
1914072Skrishna 			error = CRYPTO_BUFFER_TOO_BIG;
1924072Skrishna 		} else {
1934072Skrishna 			KCF_WRAP_MAC_OPS_PARAMS(&params, KCF_OP_ATOMIC,
1944072Skrishna 			    pd->pd_sid, mech, key, data, mac, spi_ctx_tmpl);
1950Sstevel@tonic-gate 
1964072Skrishna 			error = kcf_submit_request(pd, NULL, crq, &params,
1974072Skrishna 			    KCF_ISDUALREQ(crq));
1984072Skrishna 		}
1990Sstevel@tonic-gate 	}
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED &&
2020Sstevel@tonic-gate 	    IS_RECOVERABLE(error)) {
2030Sstevel@tonic-gate 		/* Add pd to the linked list of providers tried. */
2040Sstevel@tonic-gate 		if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL)
2050Sstevel@tonic-gate 			goto retry;
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	if (list != NULL)
2090Sstevel@tonic-gate 		kcf_free_triedlist(list);
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	KCF_PROV_REFRELE(pd);
2120Sstevel@tonic-gate 	return (error);
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate /*
2160Sstevel@tonic-gate  * Single part operation to compute the MAC corresponding to the specified
2170Sstevel@tonic-gate  * 'data' and to verify that it matches the MAC specified by 'mac'.
2180Sstevel@tonic-gate  * The other arguments are the same as the function crypto_mac_prov().
2190Sstevel@tonic-gate  */
2200Sstevel@tonic-gate int
221904Smcpowers crypto_mac_verify_prov(crypto_provider_t provider, crypto_session_id_t sid,
222904Smcpowers     crypto_mechanism_t *mech, crypto_data_t *data, crypto_key_t *key,
223904Smcpowers     crypto_ctx_template_t tmpl, crypto_data_t *mac, crypto_call_req_t *crq)
2240Sstevel@tonic-gate {
2250Sstevel@tonic-gate 	kcf_req_params_t params;
226904Smcpowers 	kcf_provider_desc_t *pd = provider;
227904Smcpowers 	kcf_provider_desc_t *real_provider = pd;
228904Smcpowers 	int rv;
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	ASSERT(KCF_PROV_REFHELD(pd));
231904Smcpowers 
232904Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
23310444SVladimir.Kotal@Sun.COM 		rv = kcf_get_hardware_provider(mech->cm_type, key,
23410444SVladimir.Kotal@Sun.COM 		    CRYPTO_MECH_INVALID, NULL, CHECK_RESTRICT(crq), pd,
2351808Smcpowers 		    &real_provider, CRYPTO_FG_MAC_ATOMIC);
236904Smcpowers 
237904Smcpowers 		if (rv != CRYPTO_SUCCESS)
238904Smcpowers 			return (rv);
239904Smcpowers 	}
240904Smcpowers 
2410Sstevel@tonic-gate 	KCF_WRAP_MAC_OPS_PARAMS(&params, KCF_OP_MAC_VERIFY_ATOMIC, sid, mech,
2420Sstevel@tonic-gate 	    key, data, mac, tmpl);
243904Smcpowers 	rv = kcf_submit_request(real_provider, NULL, crq, &params, B_FALSE);
244904Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
245904Smcpowers 		KCF_PROV_REFRELE(real_provider);
2460Sstevel@tonic-gate 
247904Smcpowers 	return (rv);
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate /*
2510Sstevel@tonic-gate  * Same as crypto_mac_verify_prov(), but relies on the KCF scheduler to choose
2520Sstevel@tonic-gate  * a provider. See crypto_mac_verify_prov() comments for more information.
2530Sstevel@tonic-gate  */
2540Sstevel@tonic-gate int
2550Sstevel@tonic-gate crypto_mac_verify(crypto_mechanism_t *mech, crypto_data_t *data,
2560Sstevel@tonic-gate     crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_data_t *mac,
2570Sstevel@tonic-gate     crypto_call_req_t *crq)
2580Sstevel@tonic-gate {
2590Sstevel@tonic-gate 	int error;
2600Sstevel@tonic-gate 	kcf_mech_entry_t *me;
2610Sstevel@tonic-gate 	kcf_req_params_t params;
2620Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
2630Sstevel@tonic-gate 	kcf_ctx_template_t *ctx_tmpl;
2640Sstevel@tonic-gate 	crypto_spi_ctx_template_t spi_ctx_tmpl = NULL;
2650Sstevel@tonic-gate 	kcf_prov_tried_t *list = NULL;
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate retry:
2680Sstevel@tonic-gate 	/* The pd is returned held */
26910444SVladimir.Kotal@Sun.COM 	if ((pd = kcf_get_mech_provider(mech->cm_type, key, &me, &error,
2700Sstevel@tonic-gate 	    list, CRYPTO_FG_MAC_ATOMIC, CHECK_RESTRICT(crq),
2710Sstevel@tonic-gate 	    data->cd_length)) == NULL) {
2720Sstevel@tonic-gate 		if (list != NULL)
2730Sstevel@tonic-gate 			kcf_free_triedlist(list);
2740Sstevel@tonic-gate 		return (error);
2750Sstevel@tonic-gate 	}
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	/*
2780Sstevel@tonic-gate 	 * For SW providers, check the validity of the context template
2790Sstevel@tonic-gate 	 * It is very rare that the generation number mis-matches, so
2800Sstevel@tonic-gate 	 * is acceptable to fail here, and let the consumer recover by
2810Sstevel@tonic-gate 	 * freeing this tmpl and create a new one for the key and new SW
2820Sstevel@tonic-gate 	 * provider
2830Sstevel@tonic-gate 	 */
2840Sstevel@tonic-gate 	if ((pd->pd_prov_type == CRYPTO_SW_PROVIDER) &&
2850Sstevel@tonic-gate 	    ((ctx_tmpl = (kcf_ctx_template_t *)tmpl) != NULL)) {
2860Sstevel@tonic-gate 		if (ctx_tmpl->ct_generation != me->me_gen_swprov) {
2870Sstevel@tonic-gate 			if (list != NULL)
2880Sstevel@tonic-gate 				kcf_free_triedlist(list);
2890Sstevel@tonic-gate 			KCF_PROV_REFRELE(pd);
2900Sstevel@tonic-gate 			return (CRYPTO_OLD_CTX_TEMPLATE);
2910Sstevel@tonic-gate 		} else {
2920Sstevel@tonic-gate 			spi_ctx_tmpl = ctx_tmpl->ct_prov_tmpl;
2930Sstevel@tonic-gate 		}
2940Sstevel@tonic-gate 	}
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	/* The fast path for SW providers. */
2970Sstevel@tonic-gate 	if (CHECK_FASTPATH(crq, pd)) {
2980Sstevel@tonic-gate 		crypto_mechanism_t lmech;
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 		lmech = *mech;
3010Sstevel@tonic-gate 		KCF_SET_PROVIDER_MECHNUM(mech->cm_type, pd, &lmech);
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 		error = KCF_PROV_MAC_VERIFY_ATOMIC(pd, pd->pd_sid, &lmech, key,
3040Sstevel@tonic-gate 		    data, mac, spi_ctx_tmpl, KCF_SWFP_RHNDL(crq));
3050Sstevel@tonic-gate 		KCF_PROV_INCRSTATS(pd, error);
3060Sstevel@tonic-gate 	} else {
3074072Skrishna 		if (pd->pd_prov_type == CRYPTO_HW_PROVIDER &&
308*11304SJanie.Lu@Sun.COM 		    (pd->pd_flags & CRYPTO_HMAC_NO_UPDATE) &&
309*11304SJanie.Lu@Sun.COM 		    (data->cd_length > pd->pd_hmac_limit)) {
3104072Skrishna 			/* see comments in crypto_mac() */
3114072Skrishna 			error = CRYPTO_BUFFER_TOO_BIG;
3124072Skrishna 		} else {
3134072Skrishna 			KCF_WRAP_MAC_OPS_PARAMS(&params,
3144072Skrishna 			    KCF_OP_MAC_VERIFY_ATOMIC, pd->pd_sid, mech,
3154072Skrishna 			    key, data, mac, spi_ctx_tmpl);
3160Sstevel@tonic-gate 
3174072Skrishna 			error = kcf_submit_request(pd, NULL, crq, &params,
3184072Skrishna 			    KCF_ISDUALREQ(crq));
3194072Skrishna 		}
3200Sstevel@tonic-gate 	}
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED &&
3230Sstevel@tonic-gate 	    IS_RECOVERABLE(error)) {
3240Sstevel@tonic-gate 		/* Add pd to the linked list of providers tried. */
3250Sstevel@tonic-gate 		if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL)
3260Sstevel@tonic-gate 			goto retry;
3270Sstevel@tonic-gate 	}
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	if (list != NULL)
3300Sstevel@tonic-gate 		kcf_free_triedlist(list);
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	KCF_PROV_REFRELE(pd);
3330Sstevel@tonic-gate 	return (error);
3340Sstevel@tonic-gate }
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate /*
3380Sstevel@tonic-gate  * crypto_mac_init_prov()
3390Sstevel@tonic-gate  *
3400Sstevel@tonic-gate  * Arguments:
3410Sstevel@tonic-gate  *	pd:	pointer to the descriptor of the provider to use for this
3420Sstevel@tonic-gate  *		operation.
3430Sstevel@tonic-gate  *	sid:	provider session id.
3440Sstevel@tonic-gate  *	mech:	crypto_mechanism_t pointer.
3450Sstevel@tonic-gate  *		mech_type is a valid value previously returned by
3460Sstevel@tonic-gate  *		crypto_mech2id();
3470Sstevel@tonic-gate  *		When the mech's parameter is not NULL, its definition depends
3480Sstevel@tonic-gate  *		on the standard definition of the mechanism.
3490Sstevel@tonic-gate  *	key:	pointer to a crypto_key_t structure.
3500Sstevel@tonic-gate  *	tmpl:	a crypto_ctx_template_t, opaque template of a context of a
3510Sstevel@tonic-gate  *		MAC with the 'mech' using 'key'. 'tmpl' is created by
3520Sstevel@tonic-gate  *		a previous call to crypto_create_ctx_template().
3530Sstevel@tonic-gate  *	ctxp:	Pointer to a crypto_context_t.
3540Sstevel@tonic-gate  *	cr:	crypto_call_req_t calling conditions and call back info.
3550Sstevel@tonic-gate  *
3560Sstevel@tonic-gate  * Description:
3570Sstevel@tonic-gate  *	Asynchronously submits a request for, or synchronously performs the
3580Sstevel@tonic-gate  *	initialization of a MAC operation on the specified provider with
3590Sstevel@tonic-gate  *	the specified session.
3600Sstevel@tonic-gate  *	When possible and applicable, will internally use the pre-computed MAC
3610Sstevel@tonic-gate  *	context from the context template, tmpl.
3620Sstevel@tonic-gate  *	When complete and successful, 'ctxp' will contain a crypto_context_t
3630Sstevel@tonic-gate  *	valid for later calls to mac_update() and mac_final().
3640Sstevel@tonic-gate  *	The caller should hold a reference on the specified provider
3650Sstevel@tonic-gate  *	descriptor before calling this function.
3660Sstevel@tonic-gate  *
3670Sstevel@tonic-gate  * Context:
3680Sstevel@tonic-gate  *	Process or interrupt, according to the semantics dictated by the 'cr'.
3690Sstevel@tonic-gate  *
3700Sstevel@tonic-gate  * Returns:
3710Sstevel@tonic-gate  *	See comment in the beginning of the file.
3720Sstevel@tonic-gate  */
3730Sstevel@tonic-gate int
374904Smcpowers crypto_mac_init_prov(crypto_provider_t provider, crypto_session_id_t sid,
3750Sstevel@tonic-gate     crypto_mechanism_t *mech, crypto_key_t *key, crypto_spi_ctx_template_t tmpl,
3760Sstevel@tonic-gate     crypto_context_t *ctxp, crypto_call_req_t *crq)
3770Sstevel@tonic-gate {
378904Smcpowers 	int rv;
3790Sstevel@tonic-gate 	crypto_ctx_t *ctx;
3800Sstevel@tonic-gate 	kcf_req_params_t params;
381904Smcpowers 	kcf_provider_desc_t *pd = provider;
382904Smcpowers 	kcf_provider_desc_t *real_provider = pd;
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	ASSERT(KCF_PROV_REFHELD(pd));
3850Sstevel@tonic-gate 
386904Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
38710444SVladimir.Kotal@Sun.COM 		rv = kcf_get_hardware_provider(mech->cm_type, key,
38810444SVladimir.Kotal@Sun.COM 		    CRYPTO_MECH_INVALID, NULL, CHECK_RESTRICT(crq), pd,
3891808Smcpowers 		    &real_provider, CRYPTO_FG_MAC);
390904Smcpowers 
391904Smcpowers 		if (rv != CRYPTO_SUCCESS)
392904Smcpowers 			return (rv);
393904Smcpowers 	}
394904Smcpowers 
395904Smcpowers 	/* Allocate and initialize the canonical context */
396904Smcpowers 	if ((ctx = kcf_new_ctx(crq, real_provider, sid)) == NULL) {
397904Smcpowers 		if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
398904Smcpowers 			KCF_PROV_REFRELE(real_provider);
3990Sstevel@tonic-gate 		return (CRYPTO_HOST_MEMORY);
400904Smcpowers 	}
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	/* The fast path for SW providers. */
4030Sstevel@tonic-gate 	if (CHECK_FASTPATH(crq, pd)) {
4040Sstevel@tonic-gate 		crypto_mechanism_t lmech;
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 		lmech = *mech;
407904Smcpowers 		KCF_SET_PROVIDER_MECHNUM(mech->cm_type, real_provider, &lmech);
408904Smcpowers 		rv = KCF_PROV_MAC_INIT(real_provider, ctx, &lmech, key, tmpl,
4090Sstevel@tonic-gate 		    KCF_SWFP_RHNDL(crq));
410904Smcpowers 		KCF_PROV_INCRSTATS(pd, rv);
4110Sstevel@tonic-gate 	} else {
4120Sstevel@tonic-gate 		KCF_WRAP_MAC_OPS_PARAMS(&params, KCF_OP_INIT, sid, mech, key,
4130Sstevel@tonic-gate 		    NULL, NULL, tmpl);
414904Smcpowers 		rv = kcf_submit_request(real_provider, ctx, crq, &params,
415904Smcpowers 		    B_FALSE);
4160Sstevel@tonic-gate 	}
4170Sstevel@tonic-gate 
418904Smcpowers 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
419904Smcpowers 		KCF_PROV_REFRELE(real_provider);
420904Smcpowers 
421904Smcpowers 	if ((rv == CRYPTO_SUCCESS) || (rv == CRYPTO_QUEUED))
4220Sstevel@tonic-gate 		*ctxp = (crypto_context_t)ctx;
4230Sstevel@tonic-gate 	else {
4240Sstevel@tonic-gate 		/* Release the hold done in kcf_new_ctx(). */
4250Sstevel@tonic-gate 		KCF_CONTEXT_REFRELE((kcf_context_t *)ctx->cc_framework_private);
4260Sstevel@tonic-gate 	}
4270Sstevel@tonic-gate 
428904Smcpowers 	return (rv);
4290Sstevel@tonic-gate }
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate /*
4320Sstevel@tonic-gate  * Same as crypto_mac_init_prov(), but relies on the KCF scheduler to
4330Sstevel@tonic-gate  * choose a provider. See crypto_mac_init_prov() comments for more
4340Sstevel@tonic-gate  * information.
4350Sstevel@tonic-gate  */
4360Sstevel@tonic-gate int
4370Sstevel@tonic-gate crypto_mac_init(crypto_mechanism_t *mech, crypto_key_t *key,
4380Sstevel@tonic-gate     crypto_ctx_template_t tmpl, crypto_context_t *ctxp,
4390Sstevel@tonic-gate     crypto_call_req_t  *crq)
4400Sstevel@tonic-gate {
4410Sstevel@tonic-gate 	int error;
4420Sstevel@tonic-gate 	kcf_mech_entry_t *me;
4430Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
4440Sstevel@tonic-gate 	kcf_ctx_template_t *ctx_tmpl;
4450Sstevel@tonic-gate 	crypto_spi_ctx_template_t spi_ctx_tmpl = NULL;
4460Sstevel@tonic-gate 	kcf_prov_tried_t *list = NULL;
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate retry:
4490Sstevel@tonic-gate 	/* The pd is returned held */
45010444SVladimir.Kotal@Sun.COM 	if ((pd = kcf_get_mech_provider(mech->cm_type, key, &me, &error,
4510Sstevel@tonic-gate 	    list, CRYPTO_FG_MAC, CHECK_RESTRICT(crq), 0)) == NULL) {
4520Sstevel@tonic-gate 		if (list != NULL)
4530Sstevel@tonic-gate 			kcf_free_triedlist(list);
4540Sstevel@tonic-gate 		return (error);
4550Sstevel@tonic-gate 	}
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	/*
4580Sstevel@tonic-gate 	 * For SW providers, check the validity of the context template
4590Sstevel@tonic-gate 	 * It is very rare that the generation number mis-matches, so
4600Sstevel@tonic-gate 	 * is acceptable to fail here, and let the consumer recover by
4610Sstevel@tonic-gate 	 * freeing this tmpl and create a new one for the key and new SW
4620Sstevel@tonic-gate 	 * provider
4630Sstevel@tonic-gate 	 */
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	if ((pd->pd_prov_type == CRYPTO_SW_PROVIDER) &&
4660Sstevel@tonic-gate 	    ((ctx_tmpl = (kcf_ctx_template_t *)tmpl) != NULL)) {
4670Sstevel@tonic-gate 		if (ctx_tmpl->ct_generation != me->me_gen_swprov) {
4680Sstevel@tonic-gate 			if (list != NULL)
4690Sstevel@tonic-gate 				kcf_free_triedlist(list);
4700Sstevel@tonic-gate 			KCF_PROV_REFRELE(pd);
4710Sstevel@tonic-gate 			return (CRYPTO_OLD_CTX_TEMPLATE);
4720Sstevel@tonic-gate 		} else {
4730Sstevel@tonic-gate 			spi_ctx_tmpl = ctx_tmpl->ct_prov_tmpl;
4740Sstevel@tonic-gate 		}
4750Sstevel@tonic-gate 	}
4760Sstevel@tonic-gate 
4774072Skrishna 	if (pd->pd_prov_type == CRYPTO_HW_PROVIDER &&
478*11304SJanie.Lu@Sun.COM 	    (pd->pd_flags & CRYPTO_HMAC_NO_UPDATE)) {
4794072Skrishna 		/*
4804072Skrishna 		 * The hardware provider has limited HMAC support.
4814072Skrishna 		 * So, we fallback early here to using a software provider.
4824072Skrishna 		 *
4834072Skrishna 		 * XXX - need to enhance to do the fallback later in
4844072Skrishna 		 * crypto_mac_update() if the size of accumulated input data
4854072Skrishna 		 * exceeds the maximum size digestable by hardware provider.
4864072Skrishna 		 */
4874072Skrishna 		error = CRYPTO_BUFFER_TOO_BIG;
4884072Skrishna 	} else {
4894072Skrishna 		error = crypto_mac_init_prov(pd, pd->pd_sid, mech, key,
4904072Skrishna 		    spi_ctx_tmpl, ctxp, crq);
4914072Skrishna 	}
4920Sstevel@tonic-gate 	if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED &&
4930Sstevel@tonic-gate 	    IS_RECOVERABLE(error)) {
4940Sstevel@tonic-gate 		/* Add pd to the linked list of providers tried. */
4950Sstevel@tonic-gate 		if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL)
4960Sstevel@tonic-gate 			goto retry;
4970Sstevel@tonic-gate 	}
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	if (list != NULL)
5000Sstevel@tonic-gate 		kcf_free_triedlist(list);
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	KCF_PROV_REFRELE(pd);
5030Sstevel@tonic-gate 	return (error);
5040Sstevel@tonic-gate }
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate /*
5070Sstevel@tonic-gate  * crypto_mac_update()
5080Sstevel@tonic-gate  *
5090Sstevel@tonic-gate  * Arguments:
5100Sstevel@tonic-gate  *	context: A crypto_context_t initialized by mac_init().
5110Sstevel@tonic-gate  *	data: The message part to be MAC'ed
5120Sstevel@tonic-gate  *	cr:	crypto_call_req_t calling conditions and call back info.
5130Sstevel@tonic-gate  *
5140Sstevel@tonic-gate  * Description:
5150Sstevel@tonic-gate  *	Asynchronously submits a request for, or synchronously performs a
5160Sstevel@tonic-gate  *	part of a MAC operation.
5170Sstevel@tonic-gate  *
5180Sstevel@tonic-gate  * Context:
5190Sstevel@tonic-gate  *	Process or interrupt, according to the semantics dictated by the 'cr'.
5200Sstevel@tonic-gate  *
5210Sstevel@tonic-gate  * Returns:
5220Sstevel@tonic-gate  *	See comment in the beginning of the file.
5230Sstevel@tonic-gate  */
5240Sstevel@tonic-gate int
5250Sstevel@tonic-gate crypto_mac_update(crypto_context_t context, crypto_data_t *data,
5260Sstevel@tonic-gate     crypto_call_req_t *cr)
5270Sstevel@tonic-gate {
5280Sstevel@tonic-gate 	crypto_ctx_t *ctx = (crypto_ctx_t *)context;
5290Sstevel@tonic-gate 	kcf_context_t *kcf_ctx;
5300Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
5310Sstevel@tonic-gate 	kcf_req_params_t params;
532904Smcpowers 	int rv;
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	if ((ctx == NULL) ||
5350Sstevel@tonic-gate 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
5360Sstevel@tonic-gate 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
5370Sstevel@tonic-gate 		return (CRYPTO_INVALID_CONTEXT);
5380Sstevel@tonic-gate 	}
5390Sstevel@tonic-gate 
540904Smcpowers 	ASSERT(pd->pd_prov_type != CRYPTO_LOGICAL_PROVIDER);
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	/* The fast path for SW providers. */
5430Sstevel@tonic-gate 	if (CHECK_FASTPATH(cr, pd)) {
544904Smcpowers 		rv = KCF_PROV_MAC_UPDATE(pd, ctx, data, NULL);
545904Smcpowers 		KCF_PROV_INCRSTATS(pd, rv);
5460Sstevel@tonic-gate 	} else {
547904Smcpowers 		KCF_WRAP_MAC_OPS_PARAMS(&params, KCF_OP_UPDATE,
548904Smcpowers 		    ctx->cc_session, NULL, NULL, data, NULL, NULL);
549904Smcpowers 		rv = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
5500Sstevel@tonic-gate 	}
5510Sstevel@tonic-gate 
552904Smcpowers 	return (rv);
5530Sstevel@tonic-gate }
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate /*
5560Sstevel@tonic-gate  * crypto_mac_final()
5570Sstevel@tonic-gate  *
5580Sstevel@tonic-gate  * Arguments:
5590Sstevel@tonic-gate  *	context: A crypto_context_t initialized by mac_init().
5600Sstevel@tonic-gate  *	mac: Storage for the message authentication code.
5610Sstevel@tonic-gate  *	cr:	crypto_call_req_t calling conditions and call back info.
5620Sstevel@tonic-gate  *
5630Sstevel@tonic-gate  * Description:
5640Sstevel@tonic-gate  *	Asynchronously submits a request for, or synchronously performs a
5650Sstevel@tonic-gate  *	part of a message authentication operation.
5660Sstevel@tonic-gate  *
5670Sstevel@tonic-gate  * Context:
5680Sstevel@tonic-gate  *	Process or interrupt, according to the semantics dictated by the 'cr'.
5690Sstevel@tonic-gate  *
5700Sstevel@tonic-gate  * Returns:
5710Sstevel@tonic-gate  *	See comment in the beginning of the file.
5720Sstevel@tonic-gate  */
5730Sstevel@tonic-gate int
5740Sstevel@tonic-gate crypto_mac_final(crypto_context_t context, crypto_data_t *mac,
5750Sstevel@tonic-gate     crypto_call_req_t *cr)
5760Sstevel@tonic-gate {
5770Sstevel@tonic-gate 	crypto_ctx_t *ctx = (crypto_ctx_t *)context;
5780Sstevel@tonic-gate 	kcf_context_t *kcf_ctx;
5790Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
5800Sstevel@tonic-gate 	kcf_req_params_t params;
581904Smcpowers 	int rv;
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	if ((ctx == NULL) ||
5840Sstevel@tonic-gate 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
5850Sstevel@tonic-gate 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
5860Sstevel@tonic-gate 		return (CRYPTO_INVALID_CONTEXT);
5870Sstevel@tonic-gate 	}
5880Sstevel@tonic-gate 
589904Smcpowers 	ASSERT(pd->pd_prov_type != CRYPTO_LOGICAL_PROVIDER);
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	/* The fast path for SW providers. */
5920Sstevel@tonic-gate 	if (CHECK_FASTPATH(cr, pd)) {
593904Smcpowers 		rv = KCF_PROV_MAC_FINAL(pd, ctx, mac, NULL);
594904Smcpowers 		KCF_PROV_INCRSTATS(pd, rv);
5950Sstevel@tonic-gate 	} else {
596904Smcpowers 		KCF_WRAP_MAC_OPS_PARAMS(&params, KCF_OP_FINAL,
597904Smcpowers 		    ctx->cc_session, NULL, NULL, NULL, mac, NULL);
598904Smcpowers 		rv = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
5990Sstevel@tonic-gate 	}
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 	/* Release the hold done in kcf_new_ctx() during init step. */
602904Smcpowers 	KCF_CONTEXT_COND_RELEASE(rv, kcf_ctx);
603904Smcpowers 	return (rv);
6040Sstevel@tonic-gate }
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate /*
6070Sstevel@tonic-gate  * See comments for crypto_mac_update() and crypto_mac_final().
6080Sstevel@tonic-gate  */
6090Sstevel@tonic-gate int
6100Sstevel@tonic-gate crypto_mac_single(crypto_context_t context, crypto_data_t *data,
6110Sstevel@tonic-gate     crypto_data_t *mac, crypto_call_req_t *cr)
6120Sstevel@tonic-gate {
6130Sstevel@tonic-gate 	crypto_ctx_t *ctx = (crypto_ctx_t *)context;
6140Sstevel@tonic-gate 	kcf_context_t *kcf_ctx;
6150Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
6160Sstevel@tonic-gate 	int error;
6170Sstevel@tonic-gate 	kcf_req_params_t params;
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	if ((ctx == NULL) ||
6210Sstevel@tonic-gate 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
6220Sstevel@tonic-gate 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
6230Sstevel@tonic-gate 		return (CRYPTO_INVALID_CONTEXT);
6240Sstevel@tonic-gate 	}
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	/* The fast path for SW providers. */
6280Sstevel@tonic-gate 	if (CHECK_FASTPATH(cr, pd)) {
6290Sstevel@tonic-gate 		error = KCF_PROV_MAC(pd, ctx, data, mac, NULL);
6300Sstevel@tonic-gate 		KCF_PROV_INCRSTATS(pd, error);
6310Sstevel@tonic-gate 	} else {
6320Sstevel@tonic-gate 		KCF_WRAP_MAC_OPS_PARAMS(&params, KCF_OP_SINGLE, pd->pd_sid,
6330Sstevel@tonic-gate 		    NULL, NULL, data, mac, NULL);
6340Sstevel@tonic-gate 		error = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
6350Sstevel@tonic-gate 	}
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 	/* Release the hold done in kcf_new_ctx() during init step. */
6380Sstevel@tonic-gate 	KCF_CONTEXT_COND_RELEASE(error, kcf_ctx);
6390Sstevel@tonic-gate 	return (error);
6400Sstevel@tonic-gate }
641