xref: /onnv-gate/usr/src/uts/common/crypto/api/kcf_ctxops.c (revision 3708:75f7822ae9dc)
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*3708Skrishna  * Common Development and Distribution License (the "License").
6*3708Skrishna  * 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*3708Skrishna  * Copyright 2007 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/systm.h>
310Sstevel@tonic-gate #include <sys/kmem.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 
380Sstevel@tonic-gate /*
390Sstevel@tonic-gate  * Crypto contexts manipulation routines
400Sstevel@tonic-gate  */
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate  * crypto_create_ctx_template()
440Sstevel@tonic-gate  *
450Sstevel@tonic-gate  * Arguments:
460Sstevel@tonic-gate  *
470Sstevel@tonic-gate  *	mech:	crypto_mechanism_t pointer.
480Sstevel@tonic-gate  *		mech_type is a valid value previously returned by
490Sstevel@tonic-gate  *		crypto_mech2id();
500Sstevel@tonic-gate  *		When the mech's parameter is not NULL, its definition depends
510Sstevel@tonic-gate  *		on the standard definition of the mechanism.
520Sstevel@tonic-gate  *	key:	pointer to a crypto_key_t structure.
530Sstevel@tonic-gate  *	ptmpl:	a storage for the opaque crypto_ctx_template_t, allocated and
540Sstevel@tonic-gate  *		initialized by the software provider this routine is
550Sstevel@tonic-gate  *		dispatched to.
560Sstevel@tonic-gate  *	kmflag:	KM_SLEEP/KM_NOSLEEP mem. alloc. flag.
570Sstevel@tonic-gate  *
580Sstevel@tonic-gate  * Description:
590Sstevel@tonic-gate  *	Redirects the call to the software provider of the specified
600Sstevel@tonic-gate  *	mechanism. That provider will allocate and pre-compute/pre-expand
610Sstevel@tonic-gate  *	the context template, reusable by later calls to crypto_xxx_init().
620Sstevel@tonic-gate  *	The size and address of that provider context template are stored
630Sstevel@tonic-gate  *	in an internal structure, kcf_ctx_template_t. The address of that
640Sstevel@tonic-gate  *	structure is given back to the caller in *ptmpl.
650Sstevel@tonic-gate  *
660Sstevel@tonic-gate  * Context:
670Sstevel@tonic-gate  *	Process or interrupt.
680Sstevel@tonic-gate  *
690Sstevel@tonic-gate  * Returns:
700Sstevel@tonic-gate  *	CRYPTO_SUCCESS when the context template is successfully created.
710Sstevel@tonic-gate  *	CRYPTO_HOST_MEMEORY: mem alloc failure
720Sstevel@tonic-gate  *	CRYPTO_ARGUMENTS_BAD: NULL storage for the ctx template.
730Sstevel@tonic-gate  *	RYPTO_MECHANISM_INVALID: invalid mechanism 'mech'.
740Sstevel@tonic-gate  */
750Sstevel@tonic-gate int
crypto_create_ctx_template(crypto_mechanism_t * mech,crypto_key_t * key,crypto_ctx_template_t * ptmpl,int kmflag)760Sstevel@tonic-gate crypto_create_ctx_template(crypto_mechanism_t *mech, crypto_key_t *key,
770Sstevel@tonic-gate     crypto_ctx_template_t *ptmpl, int kmflag)
780Sstevel@tonic-gate {
790Sstevel@tonic-gate 	int error;
800Sstevel@tonic-gate 	kcf_mech_entry_t *me;
810Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
820Sstevel@tonic-gate 	kcf_ctx_template_t *ctx_tmpl;
830Sstevel@tonic-gate 	crypto_mechanism_t prov_mech;
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	/* A few args validation */
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	if (ptmpl == NULL)
880Sstevel@tonic-gate 		return (CRYPTO_ARGUMENTS_BAD);
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	if (mech == NULL)
910Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
920Sstevel@tonic-gate 
93*3708Skrishna 	error = kcf_get_sw_prov(mech->cm_type, &pd, &me, B_TRUE);
94*3708Skrishna 	if (error != CRYPTO_SUCCESS)
95*3708Skrishna 		return (error);
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	if ((ctx_tmpl = (kcf_ctx_template_t *)kmem_alloc(
980Sstevel@tonic-gate 	    sizeof (kcf_ctx_template_t), kmflag)) == NULL) {
990Sstevel@tonic-gate 		KCF_PROV_REFRELE(pd);
1000Sstevel@tonic-gate 		return (CRYPTO_HOST_MEMORY);
1010Sstevel@tonic-gate 	}
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	/* Pass a mechtype that the provider understands */
104*3708Skrishna 	prov_mech.cm_type = KCF_TO_PROV_MECHNUM(pd, mech->cm_type);
1050Sstevel@tonic-gate 	prov_mech.cm_param = mech->cm_param;
1060Sstevel@tonic-gate 	prov_mech.cm_param_len = mech->cm_param_len;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	error = KCF_PROV_CREATE_CTX_TEMPLATE(pd, &prov_mech, key,
1090Sstevel@tonic-gate 	    &(ctx_tmpl->ct_prov_tmpl), &(ctx_tmpl->ct_size), KCF_RHNDL(kmflag));
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	if (error == CRYPTO_SUCCESS) {
1120Sstevel@tonic-gate 		ctx_tmpl->ct_generation = me->me_gen_swprov;
1130Sstevel@tonic-gate 		*ptmpl = ctx_tmpl;
1140Sstevel@tonic-gate 	} else {
1150Sstevel@tonic-gate 		kmem_free(ctx_tmpl, sizeof (kcf_ctx_template_t));
1160Sstevel@tonic-gate 	}
1170Sstevel@tonic-gate 	KCF_PROV_REFRELE(pd);
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	return (error);
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate /*
1230Sstevel@tonic-gate  * crypto_destroy_ctx_template()
1240Sstevel@tonic-gate  *
1250Sstevel@tonic-gate  * Arguments:
1260Sstevel@tonic-gate  *
1270Sstevel@tonic-gate  *	tmpl:	an opaque crypto_ctx_template_t previously created by
1280Sstevel@tonic-gate  *		crypto_create_ctx_template()
1290Sstevel@tonic-gate  *
1300Sstevel@tonic-gate  * Description:
1310Sstevel@tonic-gate  *	Frees the inbedded crypto_spi_ctx_template_t, then the
1320Sstevel@tonic-gate  *	kcf_ctx_template_t.
1330Sstevel@tonic-gate  *
1340Sstevel@tonic-gate  * Context:
1350Sstevel@tonic-gate  *	Process or interrupt.
1360Sstevel@tonic-gate  *
1370Sstevel@tonic-gate  */
1380Sstevel@tonic-gate void
crypto_destroy_ctx_template(crypto_ctx_template_t tmpl)1390Sstevel@tonic-gate crypto_destroy_ctx_template(crypto_ctx_template_t tmpl)
1400Sstevel@tonic-gate {
1410Sstevel@tonic-gate 	kcf_ctx_template_t *ctx_tmpl = (kcf_ctx_template_t *)tmpl;
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	if (ctx_tmpl == NULL)
1440Sstevel@tonic-gate 		return;
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	ASSERT(ctx_tmpl->ct_prov_tmpl != NULL);
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	bzero(ctx_tmpl->ct_prov_tmpl, ctx_tmpl->ct_size);
1490Sstevel@tonic-gate 	kmem_free(ctx_tmpl->ct_prov_tmpl, ctx_tmpl->ct_size);
1500Sstevel@tonic-gate 	kmem_free(ctx_tmpl, sizeof (kcf_ctx_template_t));
1510Sstevel@tonic-gate }
152