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