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 #pragma ident "%Z%%M% %I% %E% SMI"
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate #include <sys/errno.h>
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/systm.h>
317c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
327c478bd9Sstevel@tonic-gate #include <sys/crypto/common.h>
337c478bd9Sstevel@tonic-gate #include <sys/crypto/impl.h>
347c478bd9Sstevel@tonic-gate #include <sys/crypto/api.h>
357c478bd9Sstevel@tonic-gate #include <sys/crypto/spi.h>
367c478bd9Sstevel@tonic-gate #include <sys/crypto/sched_impl.h>
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate * Crypto contexts manipulation routines
407c478bd9Sstevel@tonic-gate */
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate * crypto_create_ctx_template()
447c478bd9Sstevel@tonic-gate *
457c478bd9Sstevel@tonic-gate * Arguments:
467c478bd9Sstevel@tonic-gate *
477c478bd9Sstevel@tonic-gate * mech: crypto_mechanism_t pointer.
487c478bd9Sstevel@tonic-gate * mech_type is a valid value previously returned by
497c478bd9Sstevel@tonic-gate * crypto_mech2id();
507c478bd9Sstevel@tonic-gate * When the mech's parameter is not NULL, its definition depends
517c478bd9Sstevel@tonic-gate * on the standard definition of the mechanism.
527c478bd9Sstevel@tonic-gate * key: pointer to a crypto_key_t structure.
537c478bd9Sstevel@tonic-gate * ptmpl: a storage for the opaque crypto_ctx_template_t, allocated and
547c478bd9Sstevel@tonic-gate * initialized by the software provider this routine is
557c478bd9Sstevel@tonic-gate * dispatched to.
567c478bd9Sstevel@tonic-gate * kmflag: KM_SLEEP/KM_NOSLEEP mem. alloc. flag.
577c478bd9Sstevel@tonic-gate *
587c478bd9Sstevel@tonic-gate * Description:
597c478bd9Sstevel@tonic-gate * Redirects the call to the software provider of the specified
607c478bd9Sstevel@tonic-gate * mechanism. That provider will allocate and pre-compute/pre-expand
617c478bd9Sstevel@tonic-gate * the context template, reusable by later calls to crypto_xxx_init().
627c478bd9Sstevel@tonic-gate * The size and address of that provider context template are stored
637c478bd9Sstevel@tonic-gate * in an internal structure, kcf_ctx_template_t. The address of that
647c478bd9Sstevel@tonic-gate * structure is given back to the caller in *ptmpl.
657c478bd9Sstevel@tonic-gate *
667c478bd9Sstevel@tonic-gate * Context:
677c478bd9Sstevel@tonic-gate * Process or interrupt.
687c478bd9Sstevel@tonic-gate *
697c478bd9Sstevel@tonic-gate * Returns:
707c478bd9Sstevel@tonic-gate * CRYPTO_SUCCESS when the context template is successfully created.
717c478bd9Sstevel@tonic-gate * CRYPTO_HOST_MEMEORY: mem alloc failure
727c478bd9Sstevel@tonic-gate * CRYPTO_ARGUMENTS_BAD: NULL storage for the ctx template.
737c478bd9Sstevel@tonic-gate * RYPTO_MECHANISM_INVALID: invalid mechanism 'mech'.
747c478bd9Sstevel@tonic-gate */
757c478bd9Sstevel@tonic-gate int
crypto_create_ctx_template(crypto_mechanism_t * mech,crypto_key_t * key,crypto_ctx_template_t * ptmpl,int kmflag)767c478bd9Sstevel@tonic-gate crypto_create_ctx_template(crypto_mechanism_t *mech, crypto_key_t *key,
777c478bd9Sstevel@tonic-gate crypto_ctx_template_t *ptmpl, int kmflag)
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate int error;
807c478bd9Sstevel@tonic-gate kcf_mech_entry_t *me;
817c478bd9Sstevel@tonic-gate kcf_provider_desc_t *pd;
827c478bd9Sstevel@tonic-gate kcf_ctx_template_t *ctx_tmpl;
837c478bd9Sstevel@tonic-gate crypto_mechanism_t prov_mech;
847c478bd9Sstevel@tonic-gate
857c478bd9Sstevel@tonic-gate /* A few args validation */
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate if (ptmpl == NULL)
887c478bd9Sstevel@tonic-gate return (CRYPTO_ARGUMENTS_BAD);
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate if (mech == NULL)
917c478bd9Sstevel@tonic-gate return (CRYPTO_MECHANISM_INVALID);
927c478bd9Sstevel@tonic-gate
93*6a1073f8Skrishna error = kcf_get_sw_prov(mech->cm_type, &pd, &me, B_TRUE);
94*6a1073f8Skrishna if (error != CRYPTO_SUCCESS)
95*6a1073f8Skrishna return (error);
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate if ((ctx_tmpl = (kcf_ctx_template_t *)kmem_alloc(
987c478bd9Sstevel@tonic-gate sizeof (kcf_ctx_template_t), kmflag)) == NULL) {
997c478bd9Sstevel@tonic-gate KCF_PROV_REFRELE(pd);
1007c478bd9Sstevel@tonic-gate return (CRYPTO_HOST_MEMORY);
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate /* Pass a mechtype that the provider understands */
104*6a1073f8Skrishna prov_mech.cm_type = KCF_TO_PROV_MECHNUM(pd, mech->cm_type);
1057c478bd9Sstevel@tonic-gate prov_mech.cm_param = mech->cm_param;
1067c478bd9Sstevel@tonic-gate prov_mech.cm_param_len = mech->cm_param_len;
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate error = KCF_PROV_CREATE_CTX_TEMPLATE(pd, &prov_mech, key,
1097c478bd9Sstevel@tonic-gate &(ctx_tmpl->ct_prov_tmpl), &(ctx_tmpl->ct_size), KCF_RHNDL(kmflag));
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate if (error == CRYPTO_SUCCESS) {
1127c478bd9Sstevel@tonic-gate ctx_tmpl->ct_generation = me->me_gen_swprov;
1137c478bd9Sstevel@tonic-gate *ptmpl = ctx_tmpl;
1147c478bd9Sstevel@tonic-gate } else {
1157c478bd9Sstevel@tonic-gate kmem_free(ctx_tmpl, sizeof (kcf_ctx_template_t));
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate KCF_PROV_REFRELE(pd);
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate return (error);
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate /*
1237c478bd9Sstevel@tonic-gate * crypto_destroy_ctx_template()
1247c478bd9Sstevel@tonic-gate *
1257c478bd9Sstevel@tonic-gate * Arguments:
1267c478bd9Sstevel@tonic-gate *
1277c478bd9Sstevel@tonic-gate * tmpl: an opaque crypto_ctx_template_t previously created by
1287c478bd9Sstevel@tonic-gate * crypto_create_ctx_template()
1297c478bd9Sstevel@tonic-gate *
1307c478bd9Sstevel@tonic-gate * Description:
1317c478bd9Sstevel@tonic-gate * Frees the inbedded crypto_spi_ctx_template_t, then the
1327c478bd9Sstevel@tonic-gate * kcf_ctx_template_t.
1337c478bd9Sstevel@tonic-gate *
1347c478bd9Sstevel@tonic-gate * Context:
1357c478bd9Sstevel@tonic-gate * Process or interrupt.
1367c478bd9Sstevel@tonic-gate *
1377c478bd9Sstevel@tonic-gate */
1387c478bd9Sstevel@tonic-gate void
crypto_destroy_ctx_template(crypto_ctx_template_t tmpl)1397c478bd9Sstevel@tonic-gate crypto_destroy_ctx_template(crypto_ctx_template_t tmpl)
1407c478bd9Sstevel@tonic-gate {
1417c478bd9Sstevel@tonic-gate kcf_ctx_template_t *ctx_tmpl = (kcf_ctx_template_t *)tmpl;
1427c478bd9Sstevel@tonic-gate
1437c478bd9Sstevel@tonic-gate if (ctx_tmpl == NULL)
1447c478bd9Sstevel@tonic-gate return;
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate ASSERT(ctx_tmpl->ct_prov_tmpl != NULL);
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate bzero(ctx_tmpl->ct_prov_tmpl, ctx_tmpl->ct_size);
1497c478bd9Sstevel@tonic-gate kmem_free(ctx_tmpl->ct_prov_tmpl, ctx_tmpl->ct_size);
1507c478bd9Sstevel@tonic-gate kmem_free(ctx_tmpl, sizeof (kcf_ctx_template_t));
1517c478bd9Sstevel@tonic-gate }
152