xref: /illumos-gate/usr/src/uts/common/crypto/api/kcf_ctxops.c (revision dd72704bd9e794056c558153663c739e2012d721)
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 #include <sys/errno.h>
27 #include <sys/types.h>
28 #include <sys/systm.h>
29 #include <sys/kmem.h>
30 #include <sys/crypto/common.h>
31 #include <sys/crypto/impl.h>
32 #include <sys/crypto/api.h>
33 #include <sys/crypto/spi.h>
34 #include <sys/crypto/sched_impl.h>
35 
36 /*
37  * Crypto contexts manipulation routines
38  */
39 
40 /*
41  * crypto_create_ctx_template()
42  *
43  * Arguments:
44  *
45  *	mech:	crypto_mechanism_t pointer.
46  *		mech_type is a valid value previously returned by
47  *		crypto_mech2id();
48  *		When the mech's parameter is not NULL, its definition depends
49  *		on the standard definition of the mechanism.
50  *	key:	pointer to a crypto_key_t structure.
51  *	ptmpl:	a storage for the opaque crypto_ctx_template_t, allocated and
52  *		initialized by the software provider this routine is
53  *		dispatched to.
54  *	kmflag:	KM_SLEEP/KM_NOSLEEP mem. alloc. flag.
55  *
56  * Description:
57  *	Redirects the call to the software provider of the specified
58  *	mechanism. That provider will allocate and pre-compute/pre-expand
59  *	the context template, reusable by later calls to crypto_xxx_init().
60  *	The size and address of that provider context template are stored
61  *	in an internal structure, kcf_ctx_template_t. The address of that
62  *	structure is given back to the caller in *ptmpl.
63  *
64  * Context:
65  *	Process or interrupt.
66  *
67  * Returns:
68  *	CRYPTO_SUCCESS when the context template is successfully created.
69  *	CRYPTO_HOST_MEMEORY: mem alloc failure
70  *	CRYPTO_ARGUMENTS_BAD: NULL storage for the ctx template.
71  *	RYPTO_MECHANISM_INVALID: invalid mechanism 'mech'.
72  */
73 int
74 crypto_create_ctx_template(crypto_mechanism_t *mech, crypto_key_t *key,
75     crypto_ctx_template_t *ptmpl, int kmflag)
76 {
77 	int error;
78 	kcf_mech_entry_t *me;
79 	kcf_provider_desc_t *pd;
80 	kcf_ctx_template_t *ctx_tmpl;
81 	crypto_mechanism_t prov_mech;
82 
83 	/* A few args validation */
84 
85 	if (ptmpl == NULL)
86 		return (CRYPTO_ARGUMENTS_BAD);
87 
88 	if (mech == NULL)
89 		return (CRYPTO_MECHANISM_INVALID);
90 
91 	error = kcf_get_sw_prov(mech->cm_type, &pd, &me, B_TRUE);
92 	if (error != CRYPTO_SUCCESS)
93 		return (error);
94 
95 	if ((ctx_tmpl = (kcf_ctx_template_t *)kmem_alloc(
96 	    sizeof (kcf_ctx_template_t), kmflag)) == NULL) {
97 		KCF_PROV_REFRELE(pd);
98 		return (CRYPTO_HOST_MEMORY);
99 	}
100 
101 	/* Pass a mechtype that the provider understands */
102 	prov_mech.cm_type = KCF_TO_PROV_MECHNUM(pd, mech->cm_type);
103 	prov_mech.cm_param = mech->cm_param;
104 	prov_mech.cm_param_len = mech->cm_param_len;
105 
106 	error = KCF_PROV_CREATE_CTX_TEMPLATE(pd, &prov_mech, key,
107 	    &(ctx_tmpl->ct_prov_tmpl), &(ctx_tmpl->ct_size), KCF_RHNDL(kmflag));
108 
109 	if (error == CRYPTO_SUCCESS) {
110 		ctx_tmpl->ct_generation = me->me_gen_swprov;
111 		*ptmpl = ctx_tmpl;
112 	} else {
113 		kmem_free(ctx_tmpl, sizeof (kcf_ctx_template_t));
114 	}
115 	KCF_PROV_REFRELE(pd);
116 
117 	return (error);
118 }
119 
120 /*
121  * crypto_destroy_ctx_template()
122  *
123  * Arguments:
124  *
125  *	tmpl:	an opaque crypto_ctx_template_t previously created by
126  *		crypto_create_ctx_template()
127  *
128  * Description:
129  *	Frees the inbedded crypto_spi_ctx_template_t, then the
130  *	kcf_ctx_template_t.
131  *
132  * Context:
133  *	Process or interrupt.
134  *
135  */
136 void
137 crypto_destroy_ctx_template(crypto_ctx_template_t tmpl)
138 {
139 	kcf_ctx_template_t *ctx_tmpl = (kcf_ctx_template_t *)tmpl;
140 
141 	if (ctx_tmpl == NULL)
142 		return;
143 
144 	ASSERT(ctx_tmpl->ct_prov_tmpl != NULL);
145 
146 	bzero(ctx_tmpl->ct_prov_tmpl, ctx_tmpl->ct_size);
147 	kmem_free(ctx_tmpl->ct_prov_tmpl, ctx_tmpl->ct_size);
148 	kmem_free(ctx_tmpl, sizeof (kcf_ctx_template_t));
149 }
150