blob: f7403821db7f0aafdec4a2e9a6804b1b8c2a599b [file] [log] [blame]
Mat Martineauddbb4112016-04-12 19:54:58 +01001/* Crypto operations using stored keys
2 *
3 * Copyright (c) 2016, Intel Corporation
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
9 */
10
Mat Martineauddbb4112016-04-12 19:54:58 +010011#include <linux/slab.h>
12#include <linux/uaccess.h>
Mat Martineau7cbe0932017-06-08 14:50:11 +010013#include <linux/scatterlist.h>
Stephan Muellerf1c316a2016-08-19 20:39:09 +020014#include <linux/crypto.h>
15#include <crypto/hash.h>
Mat Martineau7cbe0932017-06-08 14:50:11 +010016#include <crypto/kpp.h>
17#include <crypto/dh.h>
Mat Martineauddbb4112016-04-12 19:54:58 +010018#include <keys/user-type.h>
19#include "internal.h"
20
Mat Martineau7cbe0932017-06-08 14:50:11 +010021static ssize_t dh_data_from_key(key_serial_t keyid, void **data)
Mat Martineauddbb4112016-04-12 19:54:58 +010022{
23 struct key *key;
24 key_ref_t key_ref;
25 long status;
26 ssize_t ret;
27
28 key_ref = lookup_user_key(keyid, 0, KEY_NEED_READ);
29 if (IS_ERR(key_ref)) {
30 ret = -ENOKEY;
31 goto error;
32 }
33
34 key = key_ref_to_ptr(key_ref);
35
36 ret = -EOPNOTSUPP;
37 if (key->type == &key_type_user) {
38 down_read(&key->sem);
39 status = key_validate(key);
40 if (status == 0) {
41 const struct user_key_payload *payload;
Mat Martineau7cbe0932017-06-08 14:50:11 +010042 uint8_t *duplicate;
Mat Martineauddbb4112016-04-12 19:54:58 +010043
David Howells0837e492017-03-01 15:11:23 +000044 payload = user_key_payload_locked(key);
Mat Martineauddbb4112016-04-12 19:54:58 +010045
Mat Martineau7cbe0932017-06-08 14:50:11 +010046 duplicate = kmemdup(payload->data, payload->datalen,
47 GFP_KERNEL);
48 if (duplicate) {
49 *data = duplicate;
Mat Martineauddbb4112016-04-12 19:54:58 +010050 ret = payload->datalen;
Mat Martineauddbb4112016-04-12 19:54:58 +010051 } else {
Mat Martineau7cbe0932017-06-08 14:50:11 +010052 ret = -ENOMEM;
Mat Martineauddbb4112016-04-12 19:54:58 +010053 }
54 }
55 up_read(&key->sem);
56 }
57
58 key_put(key);
59error:
60 return ret;
61}
62
Mat Martineau7cbe0932017-06-08 14:50:11 +010063static void dh_free_data(struct dh *dh)
64{
65 kzfree(dh->key);
66 kzfree(dh->p);
67 kzfree(dh->g);
68}
69
70struct dh_completion {
71 struct completion completion;
72 int err;
73};
74
75static void dh_crypto_done(struct crypto_async_request *req, int err)
76{
77 struct dh_completion *compl = req->data;
78
79 if (err == -EINPROGRESS)
80 return;
81
82 compl->err = err;
83 complete(&compl->completion);
84}
85
Stephan Muellerf1c316a2016-08-19 20:39:09 +020086struct kdf_sdesc {
87 struct shash_desc shash;
88 char ctx[];
89};
90
91static int kdf_alloc(struct kdf_sdesc **sdesc_ret, char *hashname)
92{
93 struct crypto_shash *tfm;
94 struct kdf_sdesc *sdesc;
95 int size;
Eric Biggersbbe24042017-06-08 14:49:34 +010096 int err;
Stephan Muellerf1c316a2016-08-19 20:39:09 +020097
98 /* allocate synchronous hash */
99 tfm = crypto_alloc_shash(hashname, 0, 0);
100 if (IS_ERR(tfm)) {
101 pr_info("could not allocate digest TFM handle %s\n", hashname);
102 return PTR_ERR(tfm);
103 }
104
Eric Biggersbbe24042017-06-08 14:49:34 +0100105 err = -EINVAL;
106 if (crypto_shash_digestsize(tfm) == 0)
107 goto out_free_tfm;
108
109 err = -ENOMEM;
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200110 size = sizeof(struct shash_desc) + crypto_shash_descsize(tfm);
111 sdesc = kmalloc(size, GFP_KERNEL);
112 if (!sdesc)
Eric Biggersbbe24042017-06-08 14:49:34 +0100113 goto out_free_tfm;
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200114 sdesc->shash.tfm = tfm;
115 sdesc->shash.flags = 0x0;
116
117 *sdesc_ret = sdesc;
118
119 return 0;
Eric Biggersbbe24042017-06-08 14:49:34 +0100120
121out_free_tfm:
122 crypto_free_shash(tfm);
123 return err;
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200124}
125
126static void kdf_dealloc(struct kdf_sdesc *sdesc)
127{
128 if (!sdesc)
129 return;
130
131 if (sdesc->shash.tfm)
132 crypto_free_shash(sdesc->shash.tfm);
133
134 kzfree(sdesc);
135}
136
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200137/*
138 * Implementation of the KDF in counter mode according to SP800-108 section 5.1
139 * as well as SP800-56A section 5.8.1 (Single-step KDF).
140 *
141 * SP800-56A:
142 * The src pointer is defined as Z || other info where Z is the shared secret
143 * from DH and other info is an arbitrary string (see SP800-56A section
144 * 5.8.1.2).
145 */
146static int kdf_ctr(struct kdf_sdesc *sdesc, const u8 *src, unsigned int slen,
Mat Martineau7cbe0932017-06-08 14:50:11 +0100147 u8 *dst, unsigned int dlen, unsigned int zlen)
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200148{
149 struct shash_desc *desc = &sdesc->shash;
150 unsigned int h = crypto_shash_digestsize(desc->tfm);
151 int err = 0;
152 u8 *dst_orig = dst;
Eric Biggers0ddd9f12017-06-08 14:49:49 +0100153 __be32 counter = cpu_to_be32(1);
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200154
155 while (dlen) {
156 err = crypto_shash_init(desc);
157 if (err)
158 goto err;
159
Eric Biggers0ddd9f12017-06-08 14:49:49 +0100160 err = crypto_shash_update(desc, (u8 *)&counter, sizeof(__be32));
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200161 if (err)
162 goto err;
163
Mat Martineau7cbe0932017-06-08 14:50:11 +0100164 if (zlen && h) {
Tycho Andersen890e2ab2018-04-24 14:26:39 -0600165 u8 tmpbuffer[32];
166 size_t chunk = min_t(size_t, zlen, sizeof(tmpbuffer));
Mat Martineau7cbe0932017-06-08 14:50:11 +0100167 memset(tmpbuffer, 0, chunk);
168
169 do {
170 err = crypto_shash_update(desc, tmpbuffer,
171 chunk);
172 if (err)
173 goto err;
174
175 zlen -= chunk;
Tycho Andersen890e2ab2018-04-24 14:26:39 -0600176 chunk = min_t(size_t, zlen, sizeof(tmpbuffer));
Mat Martineau7cbe0932017-06-08 14:50:11 +0100177 } while (zlen);
178 }
179
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200180 if (src && slen) {
181 err = crypto_shash_update(desc, src, slen);
182 if (err)
183 goto err;
184 }
185
Tycho Andersen383203e2018-04-24 14:26:38 -0600186 err = crypto_shash_final(desc, dst);
187 if (err)
188 goto err;
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200189
Tycho Andersen383203e2018-04-24 14:26:38 -0600190 dlen -= h;
191 dst += h;
192 counter = cpu_to_be32(be32_to_cpu(counter) + 1);
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200193 }
194
195 return 0;
196
197err:
198 memzero_explicit(dst_orig, dlen);
199 return err;
200}
201
202static int keyctl_dh_compute_kdf(struct kdf_sdesc *sdesc,
203 char __user *buffer, size_t buflen,
Mat Martineau7cbe0932017-06-08 14:50:11 +0100204 uint8_t *kbuf, size_t kbuflen, size_t lzero)
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200205{
206 uint8_t *outbuf = NULL;
207 int ret;
Tycho Andersen383203e2018-04-24 14:26:38 -0600208 size_t outbuf_len = round_up(buflen,
209 crypto_shash_digestsize(sdesc->shash.tfm));
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200210
Tycho Andersen383203e2018-04-24 14:26:38 -0600211 outbuf = kmalloc(outbuf_len, GFP_KERNEL);
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200212 if (!outbuf) {
213 ret = -ENOMEM;
214 goto err;
215 }
216
Tycho Andersen383203e2018-04-24 14:26:38 -0600217 ret = kdf_ctr(sdesc, kbuf, kbuflen, outbuf, outbuf_len, lzero);
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200218 if (ret)
219 goto err;
220
221 ret = buflen;
222 if (copy_to_user(buffer, outbuf, buflen) != 0)
223 ret = -EFAULT;
224
225err:
226 kzfree(outbuf);
227 return ret;
228}
229
230long __keyctl_dh_compute(struct keyctl_dh_params __user *params,
231 char __user *buffer, size_t buflen,
232 struct keyctl_kdf_params *kdfcopy)
Mat Martineauddbb4112016-04-12 19:54:58 +0100233{
234 long ret;
Mat Martineau7cbe0932017-06-08 14:50:11 +0100235 ssize_t dlen;
236 int secretlen;
237 int outlen;
Mat Martineauddbb4112016-04-12 19:54:58 +0100238 struct keyctl_dh_params pcopy;
Mat Martineau7cbe0932017-06-08 14:50:11 +0100239 struct dh dh_inputs;
240 struct scatterlist outsg;
241 struct dh_completion compl;
242 struct crypto_kpp *tfm;
243 struct kpp_request *req;
244 uint8_t *secret;
245 uint8_t *outbuf;
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200246 struct kdf_sdesc *sdesc = NULL;
Mat Martineauddbb4112016-04-12 19:54:58 +0100247
248 if (!params || (!buffer && buflen)) {
249 ret = -EINVAL;
Mat Martineau7cbe0932017-06-08 14:50:11 +0100250 goto out1;
Mat Martineauddbb4112016-04-12 19:54:58 +0100251 }
252 if (copy_from_user(&pcopy, params, sizeof(pcopy)) != 0) {
253 ret = -EFAULT;
Mat Martineau7cbe0932017-06-08 14:50:11 +0100254 goto out1;
Mat Martineauddbb4112016-04-12 19:54:58 +0100255 }
256
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200257 if (kdfcopy) {
258 char *hashname;
259
Eric Biggers4f9dabf2017-07-13 13:16:56 +0100260 if (memchr_inv(kdfcopy->__spare, 0, sizeof(kdfcopy->__spare))) {
261 ret = -EINVAL;
262 goto out1;
263 }
264
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200265 if (buflen > KEYCTL_KDF_MAX_OUTPUT_LEN ||
266 kdfcopy->otherinfolen > KEYCTL_KDF_MAX_OI_LEN) {
267 ret = -EMSGSIZE;
Mat Martineau7cbe0932017-06-08 14:50:11 +0100268 goto out1;
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200269 }
270
271 /* get KDF name string */
272 hashname = strndup_user(kdfcopy->hashname, CRYPTO_MAX_ALG_NAME);
273 if (IS_ERR(hashname)) {
274 ret = PTR_ERR(hashname);
Mat Martineau7cbe0932017-06-08 14:50:11 +0100275 goto out1;
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200276 }
277
278 /* allocate KDF from the kernel crypto API */
279 ret = kdf_alloc(&sdesc, hashname);
280 kfree(hashname);
281 if (ret)
Mat Martineau7cbe0932017-06-08 14:50:11 +0100282 goto out1;
Stephan Mueller4693fc72016-05-26 23:38:12 +0200283 }
284
Mat Martineau7cbe0932017-06-08 14:50:11 +0100285 memset(&dh_inputs, 0, sizeof(dh_inputs));
286
287 dlen = dh_data_from_key(pcopy.prime, &dh_inputs.p);
288 if (dlen < 0) {
289 ret = dlen;
290 goto out1;
Mat Martineauddbb4112016-04-12 19:54:58 +0100291 }
Mat Martineau7cbe0932017-06-08 14:50:11 +0100292 dh_inputs.p_size = dlen;
Mat Martineauddbb4112016-04-12 19:54:58 +0100293
Mat Martineau7cbe0932017-06-08 14:50:11 +0100294 dlen = dh_data_from_key(pcopy.base, &dh_inputs.g);
295 if (dlen < 0) {
296 ret = dlen;
297 goto out2;
Mat Martineauddbb4112016-04-12 19:54:58 +0100298 }
Mat Martineau7cbe0932017-06-08 14:50:11 +0100299 dh_inputs.g_size = dlen;
Mat Martineauddbb4112016-04-12 19:54:58 +0100300
Mat Martineau7cbe0932017-06-08 14:50:11 +0100301 dlen = dh_data_from_key(pcopy.private, &dh_inputs.key);
302 if (dlen < 0) {
303 ret = dlen;
304 goto out2;
Mat Martineauddbb4112016-04-12 19:54:58 +0100305 }
Mat Martineau7cbe0932017-06-08 14:50:11 +0100306 dh_inputs.key_size = dlen;
Mat Martineauddbb4112016-04-12 19:54:58 +0100307
Mat Martineau7cbe0932017-06-08 14:50:11 +0100308 secretlen = crypto_dh_key_len(&dh_inputs);
309 secret = kmalloc(secretlen, GFP_KERNEL);
310 if (!secret) {
Mat Martineauddbb4112016-04-12 19:54:58 +0100311 ret = -ENOMEM;
Mat Martineau7cbe0932017-06-08 14:50:11 +0100312 goto out2;
Mat Martineauddbb4112016-04-12 19:54:58 +0100313 }
Mat Martineau7cbe0932017-06-08 14:50:11 +0100314 ret = crypto_dh_encode_key(secret, secretlen, &dh_inputs);
Mat Martineauddbb4112016-04-12 19:54:58 +0100315 if (ret)
Mat Martineau7cbe0932017-06-08 14:50:11 +0100316 goto out3;
Mat Martineauddbb4112016-04-12 19:54:58 +0100317
Mat Martineau7cbe0932017-06-08 14:50:11 +0100318 tfm = crypto_alloc_kpp("dh", CRYPTO_ALG_TYPE_KPP, 0);
319 if (IS_ERR(tfm)) {
320 ret = PTR_ERR(tfm);
321 goto out3;
322 }
323
324 ret = crypto_kpp_set_secret(tfm, secret, secretlen);
325 if (ret)
326 goto out4;
327
328 outlen = crypto_kpp_maxsize(tfm);
329
330 if (!kdfcopy) {
331 /*
332 * When not using a KDF, buflen 0 is used to read the
333 * required buffer length
334 */
335 if (buflen == 0) {
336 ret = outlen;
337 goto out4;
338 } else if (outlen > buflen) {
339 ret = -EOVERFLOW;
340 goto out4;
341 }
342 }
343
344 outbuf = kzalloc(kdfcopy ? (outlen + kdfcopy->otherinfolen) : outlen,
345 GFP_KERNEL);
346 if (!outbuf) {
347 ret = -ENOMEM;
348 goto out4;
349 }
350
351 sg_init_one(&outsg, outbuf, outlen);
352
353 req = kpp_request_alloc(tfm, GFP_KERNEL);
354 if (!req) {
355 ret = -ENOMEM;
356 goto out5;
357 }
358
359 kpp_request_set_input(req, NULL, 0);
360 kpp_request_set_output(req, &outsg, outlen);
361 init_completion(&compl.completion);
362 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
363 CRYPTO_TFM_REQ_MAY_SLEEP,
364 dh_crypto_done, &compl);
365
366 /*
367 * For DH, generate_public_key and generate_shared_secret are
368 * the same calculation
369 */
370 ret = crypto_kpp_generate_public_key(req);
371 if (ret == -EINPROGRESS) {
372 wait_for_completion(&compl.completion);
373 ret = compl.err;
374 if (ret)
375 goto out6;
376 }
Mat Martineauddbb4112016-04-12 19:54:58 +0100377
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200378 if (kdfcopy) {
Mat Martineau7cbe0932017-06-08 14:50:11 +0100379 /*
380 * Concatenate SP800-56A otherinfo past DH shared secret -- the
381 * input to the KDF is (DH shared secret || otherinfo)
382 */
383 if (copy_from_user(outbuf + req->dst_len, kdfcopy->otherinfo,
384 kdfcopy->otherinfolen) != 0) {
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200385 ret = -EFAULT;
Mat Martineau7cbe0932017-06-08 14:50:11 +0100386 goto out6;
387 }
388
389 ret = keyctl_dh_compute_kdf(sdesc, buffer, buflen, outbuf,
390 req->dst_len + kdfcopy->otherinfolen,
391 outlen - req->dst_len);
392 } else if (copy_to_user(buffer, outbuf, req->dst_len) == 0) {
393 ret = req->dst_len;
394 } else {
395 ret = -EFAULT;
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200396 }
Mat Martineauddbb4112016-04-12 19:54:58 +0100397
Mat Martineau7cbe0932017-06-08 14:50:11 +0100398out6:
399 kpp_request_free(req);
400out5:
401 kzfree(outbuf);
402out4:
403 crypto_free_kpp(tfm);
404out3:
405 kzfree(secret);
406out2:
407 dh_free_data(&dh_inputs);
408out1:
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200409 kdf_dealloc(sdesc);
Mat Martineauddbb4112016-04-12 19:54:58 +0100410 return ret;
411}
Stephan Muellerf1c316a2016-08-19 20:39:09 +0200412
413long keyctl_dh_compute(struct keyctl_dh_params __user *params,
414 char __user *buffer, size_t buflen,
415 struct keyctl_kdf_params __user *kdf)
416{
417 struct keyctl_kdf_params kdfcopy;
418
419 if (!kdf)
420 return __keyctl_dh_compute(params, buffer, buflen, NULL);
421
422 if (copy_from_user(&kdfcopy, kdf, sizeof(kdfcopy)) != 0)
423 return -EFAULT;
424
425 return __keyctl_dh_compute(params, buffer, buflen, &kdfcopy);
426}