blob: da64c358474b27803a017a6ba9b6793c2c4ca117 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Mat Martineauddbb4112016-04-12 19:54:58 +01002/* Crypto operations using stored keys
3 *
4 * Copyright (c) 2016, Intel Corporation
Mat Martineauddbb4112016-04-12 19:54:58 +01005 */
6
Mat Martineauddbb4112016-04-12 19:54:58 +01007#include <linux/slab.h>
8#include <linux/uaccess.h>
Mat Martineau7cbe0932017-06-08 14:50:11 +01009#include <linux/scatterlist.h>
Stephan Muellerf1c316a2016-08-19 20:39:09 +020010#include <linux/crypto.h>
11#include <crypto/hash.h>
Mat Martineau7cbe0932017-06-08 14:50:11 +010012#include <crypto/kpp.h>
13#include <crypto/dh.h>
Stephan Müllerd3b04a42021-11-19 07:59:09 +010014#include <crypto/kdf_sp800108.h>
Mat Martineauddbb4112016-04-12 19:54:58 +010015#include <keys/user-type.h>
16#include "internal.h"
17
Nicolai Stange215bebc2022-02-21 13:10:50 +010018static ssize_t dh_data_from_key(key_serial_t keyid, const void **data)
Mat Martineauddbb4112016-04-12 19:54:58 +010019{
20 struct key *key;
21 key_ref_t key_ref;
22 long status;
23 ssize_t ret;
24
25 key_ref = lookup_user_key(keyid, 0, KEY_NEED_READ);
26 if (IS_ERR(key_ref)) {
27 ret = -ENOKEY;
28 goto error;
29 }
30
31 key = key_ref_to_ptr(key_ref);
32
33 ret = -EOPNOTSUPP;
34 if (key->type == &key_type_user) {
35 down_read(&key->sem);
36 status = key_validate(key);
37 if (status == 0) {
38 const struct user_key_payload *payload;
Mat Martineau7cbe0932017-06-08 14:50:11 +010039 uint8_t *duplicate;
Mat Martineauddbb4112016-04-12 19:54:58 +010040
David Howells0837e492017-03-01 15:11:23 +000041 payload = user_key_payload_locked(key);
Mat Martineauddbb4112016-04-12 19:54:58 +010042
Mat Martineau7cbe0932017-06-08 14:50:11 +010043 duplicate = kmemdup(payload->data, payload->datalen,
44 GFP_KERNEL);
45 if (duplicate) {
46 *data = duplicate;
Mat Martineauddbb4112016-04-12 19:54:58 +010047 ret = payload->datalen;
Mat Martineauddbb4112016-04-12 19:54:58 +010048 } else {
Mat Martineau7cbe0932017-06-08 14:50:11 +010049 ret = -ENOMEM;
Mat Martineauddbb4112016-04-12 19:54:58 +010050 }
51 }
52 up_read(&key->sem);
53 }
54
55 key_put(key);
56error:
57 return ret;
58}
59
Mat Martineau7cbe0932017-06-08 14:50:11 +010060static void dh_free_data(struct dh *dh)
61{
Waiman Long453431a2020-08-06 23:18:13 -070062 kfree_sensitive(dh->key);
63 kfree_sensitive(dh->p);
64 kfree_sensitive(dh->g);
Mat Martineau7cbe0932017-06-08 14:50:11 +010065}
66
Stephan Müllerd3b04a42021-11-19 07:59:09 +010067static int kdf_alloc(struct crypto_shash **hash, char *hashname)
Stephan Muellerf1c316a2016-08-19 20:39:09 +020068{
69 struct crypto_shash *tfm;
Stephan Muellerf1c316a2016-08-19 20:39:09 +020070
71 /* allocate synchronous hash */
72 tfm = crypto_alloc_shash(hashname, 0, 0);
73 if (IS_ERR(tfm)) {
74 pr_info("could not allocate digest TFM handle %s\n", hashname);
75 return PTR_ERR(tfm);
76 }
77
Stephan Müllerd3b04a42021-11-19 07:59:09 +010078 if (crypto_shash_digestsize(tfm) == 0) {
79 crypto_free_shash(tfm);
80 return -EINVAL;
Stephan Muellerf1c316a2016-08-19 20:39:09 +020081 }
82
Stephan Müllerd3b04a42021-11-19 07:59:09 +010083 *hash = tfm;
Stephan Muellerf1c316a2016-08-19 20:39:09 +020084
Stephan Müllerd3b04a42021-11-19 07:59:09 +010085 return 0;
Stephan Muellerf1c316a2016-08-19 20:39:09 +020086}
87
Stephan Müllerd3b04a42021-11-19 07:59:09 +010088static void kdf_dealloc(struct crypto_shash *hash)
89{
90 if (hash)
91 crypto_free_shash(hash);
92}
93
94static int keyctl_dh_compute_kdf(struct crypto_shash *hash,
Stephan Muellerf1c316a2016-08-19 20:39:09 +020095 char __user *buffer, size_t buflen,
Stephan Müllerd7921342021-11-19 07:58:44 +010096 uint8_t *kbuf, size_t kbuflen)
Stephan Muellerf1c316a2016-08-19 20:39:09 +020097{
Stephan Müllerd3b04a42021-11-19 07:59:09 +010098 struct kvec kbuf_iov = { .iov_base = kbuf, .iov_len = kbuflen };
Stephan Muellerf1c316a2016-08-19 20:39:09 +020099 uint8_t *outbuf = NULL;
100 int ret;
Stephan Müllerd3b04a42021-11-19 07:59:09 +0100101 size_t outbuf_len = roundup(buflen, crypto_shash_digestsize(hash));
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200102
Tycho Andersen383203e2018-04-24 14:26:38 -0600103 outbuf = kmalloc(outbuf_len, GFP_KERNEL);
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200104 if (!outbuf) {
105 ret = -ENOMEM;
106 goto err;
107 }
108
Stephan Müllerd3b04a42021-11-19 07:59:09 +0100109 ret = crypto_kdf108_ctr_generate(hash, &kbuf_iov, 1, outbuf, outbuf_len);
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200110 if (ret)
111 goto err;
112
113 ret = buflen;
114 if (copy_to_user(buffer, outbuf, buflen) != 0)
115 ret = -EFAULT;
116
117err:
Waiman Long453431a2020-08-06 23:18:13 -0700118 kfree_sensitive(outbuf);
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200119 return ret;
120}
121
122long __keyctl_dh_compute(struct keyctl_dh_params __user *params,
123 char __user *buffer, size_t buflen,
124 struct keyctl_kdf_params *kdfcopy)
Mat Martineauddbb4112016-04-12 19:54:58 +0100125{
126 long ret;
Mat Martineau7cbe0932017-06-08 14:50:11 +0100127 ssize_t dlen;
128 int secretlen;
129 int outlen;
Mat Martineauddbb4112016-04-12 19:54:58 +0100130 struct keyctl_dh_params pcopy;
Mat Martineau7cbe0932017-06-08 14:50:11 +0100131 struct dh dh_inputs;
132 struct scatterlist outsg;
Herbert Xu5419f2b2023-02-06 18:22:29 +0800133 DECLARE_CRYPTO_WAIT(compl);
Mat Martineau7cbe0932017-06-08 14:50:11 +0100134 struct crypto_kpp *tfm;
135 struct kpp_request *req;
136 uint8_t *secret;
137 uint8_t *outbuf;
Stephan Müllerd3b04a42021-11-19 07:59:09 +0100138 struct crypto_shash *hash = NULL;
Mat Martineauddbb4112016-04-12 19:54:58 +0100139
140 if (!params || (!buffer && buflen)) {
141 ret = -EINVAL;
Mat Martineau7cbe0932017-06-08 14:50:11 +0100142 goto out1;
Mat Martineauddbb4112016-04-12 19:54:58 +0100143 }
144 if (copy_from_user(&pcopy, params, sizeof(pcopy)) != 0) {
145 ret = -EFAULT;
Mat Martineau7cbe0932017-06-08 14:50:11 +0100146 goto out1;
Mat Martineauddbb4112016-04-12 19:54:58 +0100147 }
148
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200149 if (kdfcopy) {
150 char *hashname;
151
Eric Biggers4f9dabf2017-07-13 13:16:56 +0100152 if (memchr_inv(kdfcopy->__spare, 0, sizeof(kdfcopy->__spare))) {
153 ret = -EINVAL;
154 goto out1;
155 }
156
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200157 if (buflen > KEYCTL_KDF_MAX_OUTPUT_LEN ||
158 kdfcopy->otherinfolen > KEYCTL_KDF_MAX_OI_LEN) {
159 ret = -EMSGSIZE;
Mat Martineau7cbe0932017-06-08 14:50:11 +0100160 goto out1;
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200161 }
162
163 /* get KDF name string */
164 hashname = strndup_user(kdfcopy->hashname, CRYPTO_MAX_ALG_NAME);
165 if (IS_ERR(hashname)) {
166 ret = PTR_ERR(hashname);
Mat Martineau7cbe0932017-06-08 14:50:11 +0100167 goto out1;
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200168 }
169
170 /* allocate KDF from the kernel crypto API */
Stephan Müllerd3b04a42021-11-19 07:59:09 +0100171 ret = kdf_alloc(&hash, hashname);
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200172 kfree(hashname);
173 if (ret)
Mat Martineau7cbe0932017-06-08 14:50:11 +0100174 goto out1;
Stephan Mueller4693fc72016-05-26 23:38:12 +0200175 }
176
Mat Martineau7cbe0932017-06-08 14:50:11 +0100177 memset(&dh_inputs, 0, sizeof(dh_inputs));
178
179 dlen = dh_data_from_key(pcopy.prime, &dh_inputs.p);
180 if (dlen < 0) {
181 ret = dlen;
182 goto out1;
Mat Martineauddbb4112016-04-12 19:54:58 +0100183 }
Mat Martineau7cbe0932017-06-08 14:50:11 +0100184 dh_inputs.p_size = dlen;
Mat Martineauddbb4112016-04-12 19:54:58 +0100185
Mat Martineau7cbe0932017-06-08 14:50:11 +0100186 dlen = dh_data_from_key(pcopy.base, &dh_inputs.g);
187 if (dlen < 0) {
188 ret = dlen;
189 goto out2;
Mat Martineauddbb4112016-04-12 19:54:58 +0100190 }
Mat Martineau7cbe0932017-06-08 14:50:11 +0100191 dh_inputs.g_size = dlen;
Mat Martineauddbb4112016-04-12 19:54:58 +0100192
Lubomir Rintel8c0f9f52018-09-24 13:18:34 +0100193 dlen = dh_data_from_key(pcopy.private, &dh_inputs.key);
Mat Martineau7cbe0932017-06-08 14:50:11 +0100194 if (dlen < 0) {
195 ret = dlen;
196 goto out2;
Mat Martineauddbb4112016-04-12 19:54:58 +0100197 }
Mat Martineau7cbe0932017-06-08 14:50:11 +0100198 dh_inputs.key_size = dlen;
Mat Martineauddbb4112016-04-12 19:54:58 +0100199
Mat Martineau7cbe0932017-06-08 14:50:11 +0100200 secretlen = crypto_dh_key_len(&dh_inputs);
201 secret = kmalloc(secretlen, GFP_KERNEL);
202 if (!secret) {
Mat Martineauddbb4112016-04-12 19:54:58 +0100203 ret = -ENOMEM;
Mat Martineau7cbe0932017-06-08 14:50:11 +0100204 goto out2;
Mat Martineauddbb4112016-04-12 19:54:58 +0100205 }
Mat Martineau7cbe0932017-06-08 14:50:11 +0100206 ret = crypto_dh_encode_key(secret, secretlen, &dh_inputs);
Mat Martineauddbb4112016-04-12 19:54:58 +0100207 if (ret)
Mat Martineau7cbe0932017-06-08 14:50:11 +0100208 goto out3;
Mat Martineauddbb4112016-04-12 19:54:58 +0100209
Eric Biggers85d73112018-06-30 15:16:16 -0700210 tfm = crypto_alloc_kpp("dh", 0, 0);
Mat Martineau7cbe0932017-06-08 14:50:11 +0100211 if (IS_ERR(tfm)) {
212 ret = PTR_ERR(tfm);
213 goto out3;
214 }
215
216 ret = crypto_kpp_set_secret(tfm, secret, secretlen);
217 if (ret)
218 goto out4;
219
220 outlen = crypto_kpp_maxsize(tfm);
221
222 if (!kdfcopy) {
223 /*
224 * When not using a KDF, buflen 0 is used to read the
225 * required buffer length
226 */
227 if (buflen == 0) {
228 ret = outlen;
229 goto out4;
230 } else if (outlen > buflen) {
231 ret = -EOVERFLOW;
232 goto out4;
233 }
234 }
235
236 outbuf = kzalloc(kdfcopy ? (outlen + kdfcopy->otherinfolen) : outlen,
237 GFP_KERNEL);
238 if (!outbuf) {
239 ret = -ENOMEM;
240 goto out4;
241 }
242
243 sg_init_one(&outsg, outbuf, outlen);
244
245 req = kpp_request_alloc(tfm, GFP_KERNEL);
246 if (!req) {
247 ret = -ENOMEM;
248 goto out5;
249 }
250
251 kpp_request_set_input(req, NULL, 0);
252 kpp_request_set_output(req, &outsg, outlen);
Mat Martineau7cbe0932017-06-08 14:50:11 +0100253 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
254 CRYPTO_TFM_REQ_MAY_SLEEP,
Herbert Xu5419f2b2023-02-06 18:22:29 +0800255 crypto_req_done, &compl);
Mat Martineau7cbe0932017-06-08 14:50:11 +0100256
257 /*
258 * For DH, generate_public_key and generate_shared_secret are
259 * the same calculation
260 */
261 ret = crypto_kpp_generate_public_key(req);
Herbert Xu5419f2b2023-02-06 18:22:29 +0800262 ret = crypto_wait_req(ret, &compl);
263 if (ret)
264 goto out6;
Mat Martineauddbb4112016-04-12 19:54:58 +0100265
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200266 if (kdfcopy) {
Mat Martineau7cbe0932017-06-08 14:50:11 +0100267 /*
268 * Concatenate SP800-56A otherinfo past DH shared secret -- the
269 * input to the KDF is (DH shared secret || otherinfo)
270 */
271 if (copy_from_user(outbuf + req->dst_len, kdfcopy->otherinfo,
272 kdfcopy->otherinfolen) != 0) {
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200273 ret = -EFAULT;
Mat Martineau7cbe0932017-06-08 14:50:11 +0100274 goto out6;
275 }
276
Stephan Müllerd3b04a42021-11-19 07:59:09 +0100277 ret = keyctl_dh_compute_kdf(hash, buffer, buflen, outbuf,
Stephan Müllerd7921342021-11-19 07:58:44 +0100278 req->dst_len + kdfcopy->otherinfolen);
Mat Martineau7cbe0932017-06-08 14:50:11 +0100279 } else if (copy_to_user(buffer, outbuf, req->dst_len) == 0) {
280 ret = req->dst_len;
281 } else {
282 ret = -EFAULT;
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200283 }
Mat Martineauddbb4112016-04-12 19:54:58 +0100284
Mat Martineau7cbe0932017-06-08 14:50:11 +0100285out6:
286 kpp_request_free(req);
287out5:
Waiman Long453431a2020-08-06 23:18:13 -0700288 kfree_sensitive(outbuf);
Mat Martineau7cbe0932017-06-08 14:50:11 +0100289out4:
290 crypto_free_kpp(tfm);
291out3:
Waiman Long453431a2020-08-06 23:18:13 -0700292 kfree_sensitive(secret);
Mat Martineau7cbe0932017-06-08 14:50:11 +0100293out2:
294 dh_free_data(&dh_inputs);
295out1:
Stephan Müllerd3b04a42021-11-19 07:59:09 +0100296 kdf_dealloc(hash);
Mat Martineauddbb4112016-04-12 19:54:58 +0100297 return ret;
298}
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200299
300long keyctl_dh_compute(struct keyctl_dh_params __user *params,
301 char __user *buffer, size_t buflen,
302 struct keyctl_kdf_params __user *kdf)
303{
304 struct keyctl_kdf_params kdfcopy;
305
306 if (!kdf)
307 return __keyctl_dh_compute(params, buffer, buflen, NULL);
308
309 if (copy_from_user(&kdfcopy, kdf, sizeof(kdfcopy)) != 0)
310 return -EFAULT;
311
312 return __keyctl_dh_compute(params, buffer, buflen, &kdfcopy);
313}