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