xref: /illumos-gate/usr/src/uts/common/crypto/api/kcf_ctxops.c (revision 2d6eb4a5e0a47d30189497241345dc5466bb68ab)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6a1073f8Skrishna  * Common Development and Distribution License (the "License").
6*6a1073f8Skrishna  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*6a1073f8Skrishna  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/errno.h>
277c478bd9Sstevel@tonic-gate #include <sys/types.h>
287c478bd9Sstevel@tonic-gate #include <sys/systm.h>
297c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
307c478bd9Sstevel@tonic-gate #include <sys/crypto/common.h>
317c478bd9Sstevel@tonic-gate #include <sys/crypto/impl.h>
327c478bd9Sstevel@tonic-gate #include <sys/crypto/api.h>
337c478bd9Sstevel@tonic-gate #include <sys/crypto/spi.h>
347c478bd9Sstevel@tonic-gate #include <sys/crypto/sched_impl.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate  * Crypto contexts manipulation routines
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * crypto_create_ctx_template()
427c478bd9Sstevel@tonic-gate  *
437c478bd9Sstevel@tonic-gate  * Arguments:
447c478bd9Sstevel@tonic-gate  *
457c478bd9Sstevel@tonic-gate  *	mech:	crypto_mechanism_t pointer.
467c478bd9Sstevel@tonic-gate  *		mech_type is a valid value previously returned by
477c478bd9Sstevel@tonic-gate  *		crypto_mech2id();
487c478bd9Sstevel@tonic-gate  *		When the mech's parameter is not NULL, its definition depends
497c478bd9Sstevel@tonic-gate  *		on the standard definition of the mechanism.
507c478bd9Sstevel@tonic-gate  *	key:	pointer to a crypto_key_t structure.
517c478bd9Sstevel@tonic-gate  *	ptmpl:	a storage for the opaque crypto_ctx_template_t, allocated and
527c478bd9Sstevel@tonic-gate  *		initialized by the software provider this routine is
537c478bd9Sstevel@tonic-gate  *		dispatched to.
547c478bd9Sstevel@tonic-gate  *	kmflag:	KM_SLEEP/KM_NOSLEEP mem. alloc. flag.
557c478bd9Sstevel@tonic-gate  *
567c478bd9Sstevel@tonic-gate  * Description:
577c478bd9Sstevel@tonic-gate  *	Redirects the call to the software provider of the specified
587c478bd9Sstevel@tonic-gate  *	mechanism. That provider will allocate and pre-compute/pre-expand
597c478bd9Sstevel@tonic-gate  *	the context template, reusable by later calls to crypto_xxx_init().
607c478bd9Sstevel@tonic-gate  *	The size and address of that provider context template are stored
617c478bd9Sstevel@tonic-gate  *	in an internal structure, kcf_ctx_template_t. The address of that
627c478bd9Sstevel@tonic-gate  *	structure is given back to the caller in *ptmpl.
637c478bd9Sstevel@tonic-gate  *
647c478bd9Sstevel@tonic-gate  * Context:
657c478bd9Sstevel@tonic-gate  *	Process or interrupt.
667c478bd9Sstevel@tonic-gate  *
677c478bd9Sstevel@tonic-gate  * Returns:
687c478bd9Sstevel@tonic-gate  *	CRYPTO_SUCCESS when the context template is successfully created.
697c478bd9Sstevel@tonic-gate  *	CRYPTO_HOST_MEMEORY: mem alloc failure
707c478bd9Sstevel@tonic-gate  *	CRYPTO_ARGUMENTS_BAD: NULL storage for the ctx template.
717c478bd9Sstevel@tonic-gate  *	RYPTO_MECHANISM_INVALID: invalid mechanism 'mech'.
727c478bd9Sstevel@tonic-gate  */
737c478bd9Sstevel@tonic-gate int
crypto_create_ctx_template(crypto_mechanism_t * mech,crypto_key_t * key,crypto_ctx_template_t * ptmpl,int kmflag)747c478bd9Sstevel@tonic-gate crypto_create_ctx_template(crypto_mechanism_t *mech, crypto_key_t *key,
757c478bd9Sstevel@tonic-gate     crypto_ctx_template_t *ptmpl, int kmflag)
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate 	int error;
787c478bd9Sstevel@tonic-gate 	kcf_mech_entry_t *me;
797c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
807c478bd9Sstevel@tonic-gate 	kcf_ctx_template_t *ctx_tmpl;
817c478bd9Sstevel@tonic-gate 	crypto_mechanism_t prov_mech;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	/* A few args validation */
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	if (ptmpl == NULL)
867c478bd9Sstevel@tonic-gate 		return (CRYPTO_ARGUMENTS_BAD);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	if (mech == NULL)
897c478bd9Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
907c478bd9Sstevel@tonic-gate 
91*6a1073f8Skrishna 	error = kcf_get_sw_prov(mech->cm_type, &pd, &me, B_TRUE);
92*6a1073f8Skrishna 	if (error != CRYPTO_SUCCESS)
93*6a1073f8Skrishna 		return (error);
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	if ((ctx_tmpl = (kcf_ctx_template_t *)kmem_alloc(
967c478bd9Sstevel@tonic-gate 	    sizeof (kcf_ctx_template_t), kmflag)) == NULL) {
977c478bd9Sstevel@tonic-gate 		KCF_PROV_REFRELE(pd);
987c478bd9Sstevel@tonic-gate 		return (CRYPTO_HOST_MEMORY);
997c478bd9Sstevel@tonic-gate 	}
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	/* Pass a mechtype that the provider understands */
102*6a1073f8Skrishna 	prov_mech.cm_type = KCF_TO_PROV_MECHNUM(pd, mech->cm_type);
1037c478bd9Sstevel@tonic-gate 	prov_mech.cm_param = mech->cm_param;
1047c478bd9Sstevel@tonic-gate 	prov_mech.cm_param_len = mech->cm_param_len;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	error = KCF_PROV_CREATE_CTX_TEMPLATE(pd, &prov_mech, key,
1077c478bd9Sstevel@tonic-gate 	    &(ctx_tmpl->ct_prov_tmpl), &(ctx_tmpl->ct_size), KCF_RHNDL(kmflag));
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	if (error == CRYPTO_SUCCESS) {
1107c478bd9Sstevel@tonic-gate 		ctx_tmpl->ct_generation = me->me_gen_swprov;
1117c478bd9Sstevel@tonic-gate 		*ptmpl = ctx_tmpl;
1127c478bd9Sstevel@tonic-gate 	} else {
1137c478bd9Sstevel@tonic-gate 		kmem_free(ctx_tmpl, sizeof (kcf_ctx_template_t));
1147c478bd9Sstevel@tonic-gate 	}
1157c478bd9Sstevel@tonic-gate 	KCF_PROV_REFRELE(pd);
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	return (error);
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate /*
1217c478bd9Sstevel@tonic-gate  * crypto_destroy_ctx_template()
1227c478bd9Sstevel@tonic-gate  *
1237c478bd9Sstevel@tonic-gate  * Arguments:
1247c478bd9Sstevel@tonic-gate  *
1257c478bd9Sstevel@tonic-gate  *	tmpl:	an opaque crypto_ctx_template_t previously created by
1267c478bd9Sstevel@tonic-gate  *		crypto_create_ctx_template()
1277c478bd9Sstevel@tonic-gate  *
1287c478bd9Sstevel@tonic-gate  * Description:
1297c478bd9Sstevel@tonic-gate  *	Frees the inbedded crypto_spi_ctx_template_t, then the
1307c478bd9Sstevel@tonic-gate  *	kcf_ctx_template_t.
1317c478bd9Sstevel@tonic-gate  *
1327c478bd9Sstevel@tonic-gate  * Context:
1337c478bd9Sstevel@tonic-gate  *	Process or interrupt.
1347c478bd9Sstevel@tonic-gate  *
1357c478bd9Sstevel@tonic-gate  */
1367c478bd9Sstevel@tonic-gate void
crypto_destroy_ctx_template(crypto_ctx_template_t tmpl)1377c478bd9Sstevel@tonic-gate crypto_destroy_ctx_template(crypto_ctx_template_t tmpl)
1387c478bd9Sstevel@tonic-gate {
1397c478bd9Sstevel@tonic-gate 	kcf_ctx_template_t *ctx_tmpl = (kcf_ctx_template_t *)tmpl;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	if (ctx_tmpl == NULL)
1427c478bd9Sstevel@tonic-gate 		return;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	ASSERT(ctx_tmpl->ct_prov_tmpl != NULL);
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	bzero(ctx_tmpl->ct_prov_tmpl, ctx_tmpl->ct_size);
1477c478bd9Sstevel@tonic-gate 	kmem_free(ctx_tmpl->ct_prov_tmpl, ctx_tmpl->ct_size);
1487c478bd9Sstevel@tonic-gate 	kmem_free(ctx_tmpl, sizeof (kcf_ctx_template_t));
1497c478bd9Sstevel@tonic-gate }
150