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 * Encryption and decryption routines.
36eda14cbcSMatt Macy */
37eda14cbcSMatt Macy
38eda14cbcSMatt Macy
39eda14cbcSMatt Macy /*
40c03c5b1cSMartin Matuska * crypto_encrypt()
41eda14cbcSMatt Macy *
42eda14cbcSMatt Macy * Arguments:
43eda14cbcSMatt Macy * sid: session id
44eda14cbcSMatt Macy * mech: crypto_mechanism_t pointer.
45eda14cbcSMatt Macy * mech_type is a valid value previously returned by
46eda14cbcSMatt Macy * crypto_mech2id();
47eda14cbcSMatt Macy * When the mech's parameter is not NULL, its definition depends
48eda14cbcSMatt Macy * on the standard definition of the mechanism.
49eda14cbcSMatt Macy * key: pointer to a crypto_key_t structure.
50eda14cbcSMatt Macy * plaintext: The message to be encrypted
51eda14cbcSMatt Macy * ciphertext: Storage for the encrypted message. The length needed
52eda14cbcSMatt Macy * depends on the mechanism, and the plaintext's size.
53eda14cbcSMatt Macy * tmpl: a crypto_ctx_template_t, opaque template of a context of an
54eda14cbcSMatt Macy * encryption with the 'mech' using 'key'. 'tmpl' is created by
55eda14cbcSMatt Macy * a previous call to crypto_create_ctx_template().
56eda14cbcSMatt Macy *
57eda14cbcSMatt Macy * Description:
58eda14cbcSMatt Macy * Asynchronously submits a request for, or synchronously performs a
59eda14cbcSMatt Macy * single-part encryption of 'plaintext' with the mechanism 'mech', using
60eda14cbcSMatt Macy * the key 'key'.
61eda14cbcSMatt Macy * When complete and successful, 'ciphertext' will contain the encrypted
62eda14cbcSMatt Macy * message.
63c03c5b1cSMartin Matuska * Relies on the KCF scheduler to pick a provider.
64eda14cbcSMatt Macy *
65eda14cbcSMatt Macy * Returns:
66eda14cbcSMatt Macy * See comment in the beginning of the file.
67eda14cbcSMatt Macy */
68eda14cbcSMatt Macy int
crypto_encrypt(crypto_mechanism_t * mech,crypto_data_t * plaintext,crypto_key_t * key,crypto_ctx_template_t tmpl,crypto_data_t * ciphertext)69eda14cbcSMatt Macy crypto_encrypt(crypto_mechanism_t *mech, crypto_data_t *plaintext,
70c03c5b1cSMartin Matuska crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_data_t *ciphertext)
71eda14cbcSMatt Macy {
72eda14cbcSMatt Macy int error;
73eda14cbcSMatt Macy kcf_mech_entry_t *me;
74eda14cbcSMatt Macy kcf_provider_desc_t *pd;
75eda14cbcSMatt Macy kcf_ctx_template_t *ctx_tmpl;
76eda14cbcSMatt Macy crypto_spi_ctx_template_t spi_ctx_tmpl = NULL;
77eda14cbcSMatt Macy kcf_prov_tried_t *list = NULL;
78eda14cbcSMatt Macy
79eda14cbcSMatt Macy retry:
80eda14cbcSMatt Macy /* pd is returned held */
81eda14cbcSMatt Macy if ((pd = kcf_get_mech_provider(mech->cm_type, &me, &error,
82c03c5b1cSMartin Matuska list, CRYPTO_FG_ENCRYPT_ATOMIC)) == NULL) {
83eda14cbcSMatt Macy if (list != NULL)
84eda14cbcSMatt Macy kcf_free_triedlist(list);
85eda14cbcSMatt Macy return (error);
86eda14cbcSMatt Macy }
87eda14cbcSMatt Macy
88c03c5b1cSMartin Matuska if (((ctx_tmpl = (kcf_ctx_template_t *)tmpl) != NULL))
89eda14cbcSMatt Macy spi_ctx_tmpl = ctx_tmpl->ct_prov_tmpl;
90eda14cbcSMatt Macy
91c03c5b1cSMartin Matuska crypto_mechanism_t lmech = *mech;
92eda14cbcSMatt Macy KCF_SET_PROVIDER_MECHNUM(mech->cm_type, pd, &lmech);
93c03c5b1cSMartin Matuska error = KCF_PROV_ENCRYPT_ATOMIC(pd, &lmech, key,
94c03c5b1cSMartin Matuska plaintext, ciphertext, spi_ctx_tmpl);
95eda14cbcSMatt Macy
96c03c5b1cSMartin Matuska if (error != CRYPTO_SUCCESS && IS_RECOVERABLE(error)) {
97eda14cbcSMatt Macy /* Add pd to the linked list of providers tried. */
98c03c5b1cSMartin Matuska if (kcf_insert_triedlist(&list, pd, KM_SLEEP) != NULL)
99eda14cbcSMatt Macy goto retry;
100eda14cbcSMatt Macy }
101eda14cbcSMatt Macy
102eda14cbcSMatt Macy if (list != NULL)
103eda14cbcSMatt Macy kcf_free_triedlist(list);
104eda14cbcSMatt Macy
105eda14cbcSMatt Macy KCF_PROV_REFRELE(pd);
106eda14cbcSMatt Macy return (error);
107eda14cbcSMatt Macy }
108eda14cbcSMatt Macy
109eda14cbcSMatt Macy /*
110eda14cbcSMatt Macy * crypto_decrypt_prov()
111eda14cbcSMatt Macy *
112eda14cbcSMatt Macy * Arguments:
113eda14cbcSMatt Macy * pd: provider descriptor
114eda14cbcSMatt Macy * sid: session id
115eda14cbcSMatt Macy * mech: crypto_mechanism_t pointer.
116eda14cbcSMatt Macy * mech_type is a valid value previously returned by
117eda14cbcSMatt Macy * crypto_mech2id();
118eda14cbcSMatt Macy * When the mech's parameter is not NULL, its definition depends
119eda14cbcSMatt Macy * on the standard definition of the mechanism.
120eda14cbcSMatt Macy * key: pointer to a crypto_key_t structure.
121eda14cbcSMatt Macy * ciphertext: The message to be encrypted
122eda14cbcSMatt Macy * plaintext: Storage for the encrypted message. The length needed
123eda14cbcSMatt Macy * depends on the mechanism, and the plaintext's size.
124eda14cbcSMatt Macy * tmpl: a crypto_ctx_template_t, opaque template of a context of an
125eda14cbcSMatt Macy * encryption with the 'mech' using 'key'. 'tmpl' is created by
126eda14cbcSMatt Macy * a previous call to crypto_create_ctx_template().
127eda14cbcSMatt Macy *
128eda14cbcSMatt Macy * Description:
129eda14cbcSMatt Macy * Asynchronously submits a request for, or synchronously performs a
130eda14cbcSMatt Macy * single-part decryption of 'ciphertext' with the mechanism 'mech', using
131eda14cbcSMatt Macy * the key 'key'.
132eda14cbcSMatt Macy * When complete and successful, 'plaintext' will contain the decrypted
133eda14cbcSMatt Macy * message.
134c03c5b1cSMartin Matuska * Relies on the KCF scheduler to choose a provider.
135eda14cbcSMatt Macy *
136eda14cbcSMatt Macy * Returns:
137eda14cbcSMatt Macy * See comment in the beginning of the file.
138eda14cbcSMatt Macy */
139eda14cbcSMatt Macy int
crypto_decrypt(crypto_mechanism_t * mech,crypto_data_t * ciphertext,crypto_key_t * key,crypto_ctx_template_t tmpl,crypto_data_t * plaintext)140eda14cbcSMatt Macy crypto_decrypt(crypto_mechanism_t *mech, crypto_data_t *ciphertext,
141c03c5b1cSMartin Matuska crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_data_t *plaintext)
142eda14cbcSMatt Macy {
143eda14cbcSMatt Macy int error;
144eda14cbcSMatt Macy kcf_mech_entry_t *me;
145eda14cbcSMatt Macy kcf_provider_desc_t *pd;
146eda14cbcSMatt Macy kcf_ctx_template_t *ctx_tmpl;
147eda14cbcSMatt Macy crypto_spi_ctx_template_t spi_ctx_tmpl = NULL;
148eda14cbcSMatt Macy kcf_prov_tried_t *list = NULL;
149eda14cbcSMatt Macy
150eda14cbcSMatt Macy retry:
151eda14cbcSMatt Macy /* pd is returned held */
152eda14cbcSMatt Macy if ((pd = kcf_get_mech_provider(mech->cm_type, &me, &error,
153c03c5b1cSMartin Matuska list, CRYPTO_FG_DECRYPT_ATOMIC)) == NULL) {
154eda14cbcSMatt Macy if (list != NULL)
155eda14cbcSMatt Macy kcf_free_triedlist(list);
156eda14cbcSMatt Macy return (error);
157eda14cbcSMatt Macy }
158eda14cbcSMatt Macy
159c03c5b1cSMartin Matuska if (((ctx_tmpl = (kcf_ctx_template_t *)tmpl) != NULL))
160eda14cbcSMatt Macy spi_ctx_tmpl = ctx_tmpl->ct_prov_tmpl;
161eda14cbcSMatt Macy
162c03c5b1cSMartin Matuska crypto_mechanism_t lmech = *mech;
163eda14cbcSMatt Macy KCF_SET_PROVIDER_MECHNUM(mech->cm_type, pd, &lmech);
164eda14cbcSMatt Macy
165c03c5b1cSMartin Matuska error = KCF_PROV_DECRYPT_ATOMIC(pd, &lmech, key,
166c03c5b1cSMartin Matuska ciphertext, plaintext, spi_ctx_tmpl);
167eda14cbcSMatt Macy
168c03c5b1cSMartin Matuska if (error != CRYPTO_SUCCESS && IS_RECOVERABLE(error)) {
169eda14cbcSMatt Macy /* Add pd to the linked list of providers tried. */
170c03c5b1cSMartin Matuska if (kcf_insert_triedlist(&list, pd, KM_SLEEP) != NULL)
171eda14cbcSMatt Macy goto retry;
172eda14cbcSMatt Macy }
173eda14cbcSMatt Macy
174eda14cbcSMatt Macy if (list != NULL)
175eda14cbcSMatt Macy kcf_free_triedlist(list);
176eda14cbcSMatt Macy
177eda14cbcSMatt Macy KCF_PROV_REFRELE(pd);
178eda14cbcSMatt Macy return (error);
179eda14cbcSMatt Macy }
180eda14cbcSMatt Macy
181eda14cbcSMatt Macy #if defined(_KERNEL)
182eda14cbcSMatt Macy EXPORT_SYMBOL(crypto_encrypt);
183eda14cbcSMatt Macy EXPORT_SYMBOL(crypto_decrypt);
184eda14cbcSMatt Macy #endif
185