xref: /illumos-gate/usr/src/uts/common/crypto/api/kcf_ctxops.c (revision 34a0f871d192b33b865455a8812a3d34c1866315)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/errno.h>
29 #include <sys/types.h>
30 #include <sys/systm.h>
31 #include <sys/kmem.h>
32 #include <sys/crypto/common.h>
33 #include <sys/crypto/impl.h>
34 #include <sys/crypto/api.h>
35 #include <sys/crypto/spi.h>
36 #include <sys/crypto/sched_impl.h>
37 
38 /*
39  * Crypto contexts manipulation routines
40  */
41 
42 /*
43  * crypto_create_ctx_template()
44  *
45  * Arguments:
46  *
47  *	mech:	crypto_mechanism_t pointer.
48  *		mech_type is a valid value previously returned by
49  *		crypto_mech2id();
50  *		When the mech's parameter is not NULL, its definition depends
51  *		on the standard definition of the mechanism.
52  *	key:	pointer to a crypto_key_t structure.
53  *	ptmpl:	a storage for the opaque crypto_ctx_template_t, allocated and
54  *		initialized by the software provider this routine is
55  *		dispatched to.
56  *	kmflag:	KM_SLEEP/KM_NOSLEEP mem. alloc. flag.
57  *
58  * Description:
59  *	Redirects the call to the software provider of the specified
60  *	mechanism. That provider will allocate and pre-compute/pre-expand
61  *	the context template, reusable by later calls to crypto_xxx_init().
62  *	The size and address of that provider context template are stored
63  *	in an internal structure, kcf_ctx_template_t. The address of that
64  *	structure is given back to the caller in *ptmpl.
65  *
66  * Context:
67  *	Process or interrupt.
68  *
69  * Returns:
70  *	CRYPTO_SUCCESS when the context template is successfully created.
71  *	CRYPTO_HOST_MEMEORY: mem alloc failure
72  *	CRYPTO_ARGUMENTS_BAD: NULL storage for the ctx template.
73  *	RYPTO_MECHANISM_INVALID: invalid mechanism 'mech'.
74  */
75 int
76 crypto_create_ctx_template(crypto_mechanism_t *mech, crypto_key_t *key,
77     crypto_ctx_template_t *ptmpl, int kmflag)
78 {
79 	int error;
80 	kcf_mech_entry_t *me;
81 	kcf_provider_desc_t *pd;
82 	kcf_ctx_template_t *ctx_tmpl;
83 	crypto_mechanism_t prov_mech;
84 
85 	/* A few args validation */
86 
87 	if (ptmpl == NULL)
88 		return (CRYPTO_ARGUMENTS_BAD);
89 
90 	if (mech == NULL)
91 		return (CRYPTO_MECHANISM_INVALID);
92 
93 	error = kcf_get_sw_prov(mech->cm_type, &pd, &me, B_TRUE);
94 	if (error != CRYPTO_SUCCESS)
95 		return (error);
96 
97 	if ((ctx_tmpl = (kcf_ctx_template_t *)kmem_alloc(
98 	    sizeof (kcf_ctx_template_t), kmflag)) == NULL) {
99 		KCF_PROV_REFRELE(pd);
100 		return (CRYPTO_HOST_MEMORY);
101 	}
102 
103 	/* Pass a mechtype that the provider understands */
104 	prov_mech.cm_type = KCF_TO_PROV_MECHNUM(pd, mech->cm_type);
105 	prov_mech.cm_param = mech->cm_param;
106 	prov_mech.cm_param_len = mech->cm_param_len;
107 
108 	error = KCF_PROV_CREATE_CTX_TEMPLATE(pd, &prov_mech, key,
109 	    &(ctx_tmpl->ct_prov_tmpl), &(ctx_tmpl->ct_size), KCF_RHNDL(kmflag));
110 
111 	if (error == CRYPTO_SUCCESS) {
112 		ctx_tmpl->ct_generation = me->me_gen_swprov;
113 		*ptmpl = ctx_tmpl;
114 	} else {
115 		kmem_free(ctx_tmpl, sizeof (kcf_ctx_template_t));
116 	}
117 	KCF_PROV_REFRELE(pd);
118 
119 	return (error);
120 }
121 
122 /*
123  * crypto_destroy_ctx_template()
124  *
125  * Arguments:
126  *
127  *	tmpl:	an opaque crypto_ctx_template_t previously created by
128  *		crypto_create_ctx_template()
129  *
130  * Description:
131  *	Frees the inbedded crypto_spi_ctx_template_t, then the
132  *	kcf_ctx_template_t.
133  *
134  * Context:
135  *	Process or interrupt.
136  *
137  */
138 void
139 crypto_destroy_ctx_template(crypto_ctx_template_t tmpl)
140 {
141 	kcf_ctx_template_t *ctx_tmpl = (kcf_ctx_template_t *)tmpl;
142 
143 	if (ctx_tmpl == NULL)
144 		return;
145 
146 	ASSERT(ctx_tmpl->ct_prov_tmpl != NULL);
147 
148 	bzero(ctx_tmpl->ct_prov_tmpl, ctx_tmpl->ct_size);
149 	kmem_free(ctx_tmpl->ct_prov_tmpl, ctx_tmpl->ct_size);
150 	kmem_free(ctx_tmpl, sizeof (kcf_ctx_template_t));
151 }
152