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 * Message authentication codes routines.
36eda14cbcSMatt Macy */
37eda14cbcSMatt Macy
38eda14cbcSMatt Macy /*
39eda14cbcSMatt Macy * The following are the possible returned values common to all the routines
40eda14cbcSMatt Macy * below. The applicability of some of these return values depends on the
41eda14cbcSMatt Macy * presence of the arguments.
42eda14cbcSMatt Macy *
43eda14cbcSMatt Macy * CRYPTO_SUCCESS: The operation completed successfully.
44eda14cbcSMatt Macy * CRYPTO_INVALID_MECH_NUMBER, CRYPTO_INVALID_MECH_PARAM, or
45eda14cbcSMatt Macy * CRYPTO_INVALID_MECH for problems with the 'mech'.
46eda14cbcSMatt Macy * CRYPTO_INVALID_DATA for bogus 'data'
47eda14cbcSMatt Macy * CRYPTO_HOST_MEMORY for failure to allocate memory to handle this work.
48eda14cbcSMatt Macy * CRYPTO_INVALID_CONTEXT: Not a valid context.
49c03c5b1cSMartin Matuska * CRYPTO_BUSY: Cannot process the request now. Try later.
50eda14cbcSMatt Macy * CRYPTO_NOT_SUPPORTED and CRYPTO_MECH_NOT_SUPPORTED: No provider is
51eda14cbcSMatt Macy * capable of a function or a mechanism.
52eda14cbcSMatt Macy * CRYPTO_INVALID_KEY: bogus 'key' argument.
53eda14cbcSMatt Macy * CRYPTO_INVALID_MAC: bogus 'mac' argument.
54eda14cbcSMatt Macy */
55eda14cbcSMatt Macy
56eda14cbcSMatt Macy /*
57eda14cbcSMatt Macy * crypto_mac_prov()
58eda14cbcSMatt Macy *
59eda14cbcSMatt Macy * Arguments:
60eda14cbcSMatt Macy * mech: crypto_mechanism_t pointer.
61eda14cbcSMatt Macy * mech_type is a valid value previously returned by
62eda14cbcSMatt Macy * crypto_mech2id();
63eda14cbcSMatt Macy * When the mech's parameter is not NULL, its definition depends
64eda14cbcSMatt Macy * on the standard definition of the mechanism.
65eda14cbcSMatt Macy * key: pointer to a crypto_key_t structure.
66eda14cbcSMatt Macy * data: The message to compute the MAC for.
67eda14cbcSMatt Macy * mac: Storage for the MAC. The length needed depends on the mechanism.
68eda14cbcSMatt Macy * tmpl: a crypto_ctx_template_t, opaque template of a context of a
69eda14cbcSMatt Macy * MAC with the 'mech' using 'key'. 'tmpl' is created by
70eda14cbcSMatt Macy * a previous call to crypto_create_ctx_template().
71eda14cbcSMatt Macy *
72eda14cbcSMatt Macy * Description:
73eda14cbcSMatt Macy * Asynchronously submits a request for, or synchronously performs a
74eda14cbcSMatt Macy * single-part message authentication of 'data' with the mechanism
75eda14cbcSMatt Macy * 'mech', using * the key 'key', on the specified provider with
76eda14cbcSMatt Macy * the specified session id.
77eda14cbcSMatt Macy * When complete and successful, 'mac' will contain the message
78eda14cbcSMatt Macy * authentication code.
79c03c5b1cSMartin Matuska * Relies on the KCF scheduler to choose a provider.
80eda14cbcSMatt Macy *
81eda14cbcSMatt Macy * Returns:
82eda14cbcSMatt Macy * See comment in the beginning of the file.
83eda14cbcSMatt Macy */
84eda14cbcSMatt Macy int
crypto_mac(crypto_mechanism_t * mech,crypto_data_t * data,crypto_key_t * key,crypto_ctx_template_t tmpl,crypto_data_t * mac)85eda14cbcSMatt Macy crypto_mac(crypto_mechanism_t *mech, crypto_data_t *data,
86c03c5b1cSMartin Matuska crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_data_t *mac)
87eda14cbcSMatt Macy {
88eda14cbcSMatt Macy int error;
89eda14cbcSMatt Macy kcf_mech_entry_t *me;
90eda14cbcSMatt Macy kcf_provider_desc_t *pd;
91eda14cbcSMatt Macy kcf_ctx_template_t *ctx_tmpl;
92eda14cbcSMatt Macy crypto_spi_ctx_template_t spi_ctx_tmpl = NULL;
93eda14cbcSMatt Macy kcf_prov_tried_t *list = NULL;
94eda14cbcSMatt Macy
95eda14cbcSMatt Macy retry:
96eda14cbcSMatt Macy /* The pd is returned held */
97eda14cbcSMatt Macy if ((pd = kcf_get_mech_provider(mech->cm_type, &me, &error,
98c03c5b1cSMartin Matuska list, CRYPTO_FG_MAC_ATOMIC)) == NULL) {
99eda14cbcSMatt Macy if (list != NULL)
100eda14cbcSMatt Macy kcf_free_triedlist(list);
101eda14cbcSMatt Macy return (error);
102eda14cbcSMatt Macy }
103eda14cbcSMatt Macy
104c03c5b1cSMartin Matuska if (((ctx_tmpl = (kcf_ctx_template_t *)tmpl) != NULL))
105eda14cbcSMatt Macy spi_ctx_tmpl = ctx_tmpl->ct_prov_tmpl;
106eda14cbcSMatt Macy
107c03c5b1cSMartin Matuska crypto_mechanism_t lmech = *mech;
108eda14cbcSMatt Macy KCF_SET_PROVIDER_MECHNUM(mech->cm_type, pd, &lmech);
109c03c5b1cSMartin Matuska error = KCF_PROV_MAC_ATOMIC(pd, &lmech, key, data,
110c03c5b1cSMartin Matuska mac, spi_ctx_tmpl);
111eda14cbcSMatt Macy
112c03c5b1cSMartin Matuska if (error != CRYPTO_SUCCESS && IS_RECOVERABLE(error)) {
113eda14cbcSMatt Macy /* Add pd to the linked list of providers tried. */
114c03c5b1cSMartin Matuska if (kcf_insert_triedlist(&list, pd, KM_SLEEP) != NULL)
115eda14cbcSMatt Macy goto retry;
116eda14cbcSMatt Macy }
117eda14cbcSMatt Macy
118eda14cbcSMatt Macy if (list != NULL)
119eda14cbcSMatt Macy kcf_free_triedlist(list);
120eda14cbcSMatt Macy
121eda14cbcSMatt Macy KCF_PROV_REFRELE(pd);
122eda14cbcSMatt Macy return (error);
123eda14cbcSMatt Macy }
124eda14cbcSMatt Macy
125eda14cbcSMatt Macy /*
126eda14cbcSMatt Macy * crypto_mac_init_prov()
127eda14cbcSMatt Macy *
128eda14cbcSMatt Macy * Arguments:
129eda14cbcSMatt Macy * pd: pointer to the descriptor of the provider to use for this
130eda14cbcSMatt Macy * operation.
131eda14cbcSMatt Macy * mech: crypto_mechanism_t pointer.
132eda14cbcSMatt Macy * mech_type is a valid value previously returned by
133eda14cbcSMatt Macy * crypto_mech2id();
134eda14cbcSMatt Macy * When the mech's parameter is not NULL, its definition depends
135eda14cbcSMatt Macy * on the standard definition of the mechanism.
136eda14cbcSMatt Macy * key: pointer to a crypto_key_t structure.
137eda14cbcSMatt Macy * tmpl: a crypto_ctx_template_t, opaque template of a context of a
138eda14cbcSMatt Macy * MAC with the 'mech' using 'key'. 'tmpl' is created by
139eda14cbcSMatt Macy * a previous call to crypto_create_ctx_template().
140eda14cbcSMatt Macy * ctxp: Pointer to a crypto_context_t.
141eda14cbcSMatt Macy *
142eda14cbcSMatt Macy * Description:
143eda14cbcSMatt Macy * Asynchronously submits a request for, or synchronously performs the
144eda14cbcSMatt Macy * initialization of a MAC operation on the specified provider with
145eda14cbcSMatt Macy * the specified session.
146eda14cbcSMatt Macy * When possible and applicable, will internally use the pre-computed MAC
147eda14cbcSMatt Macy * context from the context template, tmpl.
148eda14cbcSMatt Macy * When complete and successful, 'ctxp' will contain a crypto_context_t
149eda14cbcSMatt Macy * valid for later calls to mac_update() and mac_final().
150eda14cbcSMatt Macy * The caller should hold a reference on the specified provider
151eda14cbcSMatt Macy * descriptor before calling this function.
152eda14cbcSMatt Macy *
153eda14cbcSMatt Macy * Returns:
154eda14cbcSMatt Macy * See comment in the beginning of the file.
155eda14cbcSMatt Macy */
156c03c5b1cSMartin Matuska static int
crypto_mac_init_prov(kcf_provider_desc_t * pd,crypto_mechanism_t * mech,crypto_key_t * key,crypto_spi_ctx_template_t tmpl,crypto_context_t * ctxp)157c03c5b1cSMartin Matuska crypto_mac_init_prov(kcf_provider_desc_t *pd,
158eda14cbcSMatt Macy crypto_mechanism_t *mech, crypto_key_t *key, crypto_spi_ctx_template_t tmpl,
159c03c5b1cSMartin Matuska crypto_context_t *ctxp)
160eda14cbcSMatt Macy {
161eda14cbcSMatt Macy int rv;
162eda14cbcSMatt Macy crypto_ctx_t *ctx;
163eda14cbcSMatt Macy kcf_provider_desc_t *real_provider = pd;
164eda14cbcSMatt Macy
165eda14cbcSMatt Macy ASSERT(KCF_PROV_REFHELD(pd));
166eda14cbcSMatt Macy
167eda14cbcSMatt Macy /* Allocate and initialize the canonical context */
168c03c5b1cSMartin Matuska if ((ctx = kcf_new_ctx(real_provider)) == NULL)
169eda14cbcSMatt Macy return (CRYPTO_HOST_MEMORY);
170eda14cbcSMatt Macy
171c03c5b1cSMartin Matuska crypto_mechanism_t lmech = *mech;
172eda14cbcSMatt Macy KCF_SET_PROVIDER_MECHNUM(mech->cm_type, real_provider, &lmech);
173c03c5b1cSMartin Matuska rv = KCF_PROV_MAC_INIT(real_provider, ctx, &lmech, key, tmpl);
174eda14cbcSMatt Macy
175c03c5b1cSMartin Matuska if (rv == CRYPTO_SUCCESS)
176eda14cbcSMatt Macy *ctxp = (crypto_context_t)ctx;
177eda14cbcSMatt Macy else {
178eda14cbcSMatt Macy /* Release the hold done in kcf_new_ctx(). */
179eda14cbcSMatt Macy KCF_CONTEXT_REFRELE((kcf_context_t *)ctx->cc_framework_private);
180eda14cbcSMatt Macy }
181eda14cbcSMatt Macy
182eda14cbcSMatt Macy return (rv);
183eda14cbcSMatt Macy }
184eda14cbcSMatt Macy
185eda14cbcSMatt Macy /*
186eda14cbcSMatt Macy * Same as crypto_mac_init_prov(), but relies on the KCF scheduler to
187eda14cbcSMatt Macy * choose a provider. See crypto_mac_init_prov() comments for more
188eda14cbcSMatt Macy * information.
189eda14cbcSMatt Macy */
190eda14cbcSMatt Macy int
crypto_mac_init(crypto_mechanism_t * mech,crypto_key_t * key,crypto_ctx_template_t tmpl,crypto_context_t * ctxp)191eda14cbcSMatt Macy crypto_mac_init(crypto_mechanism_t *mech, crypto_key_t *key,
192c03c5b1cSMartin Matuska crypto_ctx_template_t tmpl, crypto_context_t *ctxp)
193eda14cbcSMatt Macy {
194eda14cbcSMatt Macy int error;
195eda14cbcSMatt Macy kcf_mech_entry_t *me;
196eda14cbcSMatt Macy kcf_provider_desc_t *pd;
197eda14cbcSMatt Macy kcf_ctx_template_t *ctx_tmpl;
198eda14cbcSMatt Macy crypto_spi_ctx_template_t spi_ctx_tmpl = NULL;
199eda14cbcSMatt Macy kcf_prov_tried_t *list = NULL;
200eda14cbcSMatt Macy
201eda14cbcSMatt Macy retry:
202eda14cbcSMatt Macy /* The pd is returned held */
203eda14cbcSMatt Macy if ((pd = kcf_get_mech_provider(mech->cm_type, &me, &error,
204c03c5b1cSMartin Matuska list, CRYPTO_FG_MAC)) == NULL) {
205eda14cbcSMatt Macy if (list != NULL)
206eda14cbcSMatt Macy kcf_free_triedlist(list);
207eda14cbcSMatt Macy return (error);
208eda14cbcSMatt Macy }
209eda14cbcSMatt Macy
210eda14cbcSMatt Macy /*
211c03c5b1cSMartin Matuska * Check the validity of the context template
212eda14cbcSMatt Macy * It is very rare that the generation number mis-matches, so
213eda14cbcSMatt Macy * is acceptable to fail here, and let the consumer recover by
214c03c5b1cSMartin Matuska * freeing this tmpl and create a new one for the key and new provider
215eda14cbcSMatt Macy */
216eda14cbcSMatt Macy
217c03c5b1cSMartin Matuska if (((ctx_tmpl = (kcf_ctx_template_t *)tmpl) != NULL))
218eda14cbcSMatt Macy spi_ctx_tmpl = ctx_tmpl->ct_prov_tmpl;
219eda14cbcSMatt Macy
220c03c5b1cSMartin Matuska error = crypto_mac_init_prov(pd, mech, key,
221c03c5b1cSMartin Matuska spi_ctx_tmpl, ctxp);
222c03c5b1cSMartin Matuska if (error != CRYPTO_SUCCESS && IS_RECOVERABLE(error)) {
223eda14cbcSMatt Macy /* Add pd to the linked list of providers tried. */
224c03c5b1cSMartin Matuska if (kcf_insert_triedlist(&list, pd, KM_SLEEP) != NULL)
225eda14cbcSMatt Macy goto retry;
226eda14cbcSMatt Macy }
227eda14cbcSMatt Macy
228eda14cbcSMatt Macy if (list != NULL)
229eda14cbcSMatt Macy kcf_free_triedlist(list);
230eda14cbcSMatt Macy
231eda14cbcSMatt Macy KCF_PROV_REFRELE(pd);
232eda14cbcSMatt Macy return (error);
233eda14cbcSMatt Macy }
234eda14cbcSMatt Macy
235eda14cbcSMatt Macy /*
236eda14cbcSMatt Macy * crypto_mac_update()
237eda14cbcSMatt Macy *
238eda14cbcSMatt Macy * Arguments:
239eda14cbcSMatt Macy * context: A crypto_context_t initialized by mac_init().
240eda14cbcSMatt Macy * data: The message part to be MAC'ed
241eda14cbcSMatt Macy *
242eda14cbcSMatt Macy * Description:
243c03c5b1cSMartin Matuska * Synchronously performs a part of a MAC operation.
244eda14cbcSMatt Macy *
245eda14cbcSMatt Macy * Returns:
246eda14cbcSMatt Macy * See comment in the beginning of the file.
247eda14cbcSMatt Macy */
248eda14cbcSMatt Macy int
crypto_mac_update(crypto_context_t context,crypto_data_t * data)249c03c5b1cSMartin Matuska crypto_mac_update(crypto_context_t context, crypto_data_t *data)
250eda14cbcSMatt Macy {
251eda14cbcSMatt Macy crypto_ctx_t *ctx = (crypto_ctx_t *)context;
252eda14cbcSMatt Macy kcf_context_t *kcf_ctx;
253eda14cbcSMatt Macy kcf_provider_desc_t *pd;
254eda14cbcSMatt Macy
255eda14cbcSMatt Macy if ((ctx == NULL) ||
256eda14cbcSMatt Macy ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
257eda14cbcSMatt Macy ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
258eda14cbcSMatt Macy return (CRYPTO_INVALID_CONTEXT);
259eda14cbcSMatt Macy }
260eda14cbcSMatt Macy
261c03c5b1cSMartin Matuska return (KCF_PROV_MAC_UPDATE(pd, ctx, data));
262eda14cbcSMatt Macy }
263eda14cbcSMatt Macy
264eda14cbcSMatt Macy /*
265eda14cbcSMatt Macy * crypto_mac_final()
266eda14cbcSMatt Macy *
267eda14cbcSMatt Macy * Arguments:
268eda14cbcSMatt Macy * context: A crypto_context_t initialized by mac_init().
269eda14cbcSMatt Macy * mac: Storage for the message authentication code.
270eda14cbcSMatt Macy *
271eda14cbcSMatt Macy * Description:
272c03c5b1cSMartin Matuska * Synchronously performs a part of a message authentication operation.
273eda14cbcSMatt Macy *
274eda14cbcSMatt Macy * Returns:
275eda14cbcSMatt Macy * See comment in the beginning of the file.
276eda14cbcSMatt Macy */
277eda14cbcSMatt Macy int
crypto_mac_final(crypto_context_t context,crypto_data_t * mac)278c03c5b1cSMartin Matuska crypto_mac_final(crypto_context_t context, crypto_data_t *mac)
279eda14cbcSMatt Macy {
280eda14cbcSMatt Macy crypto_ctx_t *ctx = (crypto_ctx_t *)context;
281eda14cbcSMatt Macy kcf_context_t *kcf_ctx;
282eda14cbcSMatt Macy kcf_provider_desc_t *pd;
283eda14cbcSMatt Macy
284eda14cbcSMatt Macy if ((ctx == NULL) ||
285eda14cbcSMatt Macy ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
286eda14cbcSMatt Macy ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
287eda14cbcSMatt Macy return (CRYPTO_INVALID_CONTEXT);
288eda14cbcSMatt Macy }
289eda14cbcSMatt Macy
290c03c5b1cSMartin Matuska int rv = KCF_PROV_MAC_FINAL(pd, ctx, mac);
291eda14cbcSMatt Macy
292eda14cbcSMatt Macy /* Release the hold done in kcf_new_ctx() during init step. */
293eda14cbcSMatt Macy KCF_CONTEXT_COND_RELEASE(rv, kcf_ctx);
294eda14cbcSMatt Macy return (rv);
295eda14cbcSMatt Macy }
296eda14cbcSMatt Macy
297eda14cbcSMatt Macy #if defined(_KERNEL)
298eda14cbcSMatt Macy EXPORT_SYMBOL(crypto_mac);
299eda14cbcSMatt Macy EXPORT_SYMBOL(crypto_mac_init);
300eda14cbcSMatt Macy EXPORT_SYMBOL(crypto_mac_update);
301eda14cbcSMatt Macy EXPORT_SYMBOL(crypto_mac_final);
302eda14cbcSMatt Macy #endif
303