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