xref: /openbmc/linux/security/keys/dh.c (revision 5a1ea477)
1 /* 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 
11 #include <linux/slab.h>
12 #include <linux/uaccess.h>
13 #include <linux/scatterlist.h>
14 #include <linux/crypto.h>
15 #include <crypto/hash.h>
16 #include <crypto/kpp.h>
17 #include <crypto/dh.h>
18 #include <keys/user-type.h>
19 #include "internal.h"
20 
21 static ssize_t dh_data_from_key(key_serial_t keyid, void **data)
22 {
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;
42 			uint8_t *duplicate;
43 
44 			payload = user_key_payload_locked(key);
45 
46 			duplicate = kmemdup(payload->data, payload->datalen,
47 					    GFP_KERNEL);
48 			if (duplicate) {
49 				*data = duplicate;
50 				ret = payload->datalen;
51 			} else {
52 				ret = -ENOMEM;
53 			}
54 		}
55 		up_read(&key->sem);
56 	}
57 
58 	key_put(key);
59 error:
60 	return ret;
61 }
62 
63 static void dh_free_data(struct dh *dh)
64 {
65 	kzfree(dh->key);
66 	kzfree(dh->p);
67 	kzfree(dh->g);
68 }
69 
70 struct dh_completion {
71 	struct completion completion;
72 	int err;
73 };
74 
75 static 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 
86 struct kdf_sdesc {
87 	struct shash_desc shash;
88 	char ctx[];
89 };
90 
91 static int kdf_alloc(struct kdf_sdesc **sdesc_ret, char *hashname)
92 {
93 	struct crypto_shash *tfm;
94 	struct kdf_sdesc *sdesc;
95 	int size;
96 	int err;
97 
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 
105 	err = -EINVAL;
106 	if (crypto_shash_digestsize(tfm) == 0)
107 		goto out_free_tfm;
108 
109 	err = -ENOMEM;
110 	size = sizeof(struct shash_desc) + crypto_shash_descsize(tfm);
111 	sdesc = kmalloc(size, GFP_KERNEL);
112 	if (!sdesc)
113 		goto out_free_tfm;
114 	sdesc->shash.tfm = tfm;
115 
116 	*sdesc_ret = sdesc;
117 
118 	return 0;
119 
120 out_free_tfm:
121 	crypto_free_shash(tfm);
122 	return err;
123 }
124 
125 static void kdf_dealloc(struct kdf_sdesc *sdesc)
126 {
127 	if (!sdesc)
128 		return;
129 
130 	if (sdesc->shash.tfm)
131 		crypto_free_shash(sdesc->shash.tfm);
132 
133 	kzfree(sdesc);
134 }
135 
136 /*
137  * Implementation of the KDF in counter mode according to SP800-108 section 5.1
138  * as well as SP800-56A section 5.8.1 (Single-step KDF).
139  *
140  * SP800-56A:
141  * The src pointer is defined as Z || other info where Z is the shared secret
142  * from DH and other info is an arbitrary string (see SP800-56A section
143  * 5.8.1.2).
144  *
145  * 'dlen' must be a multiple of the digest size.
146  */
147 static int kdf_ctr(struct kdf_sdesc *sdesc, const u8 *src, unsigned int slen,
148 		   u8 *dst, unsigned int dlen, unsigned int zlen)
149 {
150 	struct shash_desc *desc = &sdesc->shash;
151 	unsigned int h = crypto_shash_digestsize(desc->tfm);
152 	int err = 0;
153 	u8 *dst_orig = dst;
154 	__be32 counter = cpu_to_be32(1);
155 
156 	while (dlen) {
157 		err = crypto_shash_init(desc);
158 		if (err)
159 			goto err;
160 
161 		err = crypto_shash_update(desc, (u8 *)&counter, sizeof(__be32));
162 		if (err)
163 			goto err;
164 
165 		if (zlen && h) {
166 			u8 tmpbuffer[32];
167 			size_t chunk = min_t(size_t, zlen, sizeof(tmpbuffer));
168 			memset(tmpbuffer, 0, chunk);
169 
170 			do {
171 				err = crypto_shash_update(desc, tmpbuffer,
172 							  chunk);
173 				if (err)
174 					goto err;
175 
176 				zlen -= chunk;
177 				chunk = min_t(size_t, zlen, sizeof(tmpbuffer));
178 			} while (zlen);
179 		}
180 
181 		if (src && slen) {
182 			err = crypto_shash_update(desc, src, slen);
183 			if (err)
184 				goto err;
185 		}
186 
187 		err = crypto_shash_final(desc, dst);
188 		if (err)
189 			goto err;
190 
191 		dlen -= h;
192 		dst += h;
193 		counter = cpu_to_be32(be32_to_cpu(counter) + 1);
194 	}
195 
196 	return 0;
197 
198 err:
199 	memzero_explicit(dst_orig, dlen);
200 	return err;
201 }
202 
203 static int keyctl_dh_compute_kdf(struct kdf_sdesc *sdesc,
204 				 char __user *buffer, size_t buflen,
205 				 uint8_t *kbuf, size_t kbuflen, size_t lzero)
206 {
207 	uint8_t *outbuf = NULL;
208 	int ret;
209 	size_t outbuf_len = roundup(buflen,
210 				    crypto_shash_digestsize(sdesc->shash.tfm));
211 
212 	outbuf = kmalloc(outbuf_len, GFP_KERNEL);
213 	if (!outbuf) {
214 		ret = -ENOMEM;
215 		goto err;
216 	}
217 
218 	ret = kdf_ctr(sdesc, kbuf, kbuflen, outbuf, outbuf_len, lzero);
219 	if (ret)
220 		goto err;
221 
222 	ret = buflen;
223 	if (copy_to_user(buffer, outbuf, buflen) != 0)
224 		ret = -EFAULT;
225 
226 err:
227 	kzfree(outbuf);
228 	return ret;
229 }
230 
231 long __keyctl_dh_compute(struct keyctl_dh_params __user *params,
232 			 char __user *buffer, size_t buflen,
233 			 struct keyctl_kdf_params *kdfcopy)
234 {
235 	long ret;
236 	ssize_t dlen;
237 	int secretlen;
238 	int outlen;
239 	struct keyctl_dh_params pcopy;
240 	struct dh dh_inputs;
241 	struct scatterlist outsg;
242 	struct dh_completion compl;
243 	struct crypto_kpp *tfm;
244 	struct kpp_request *req;
245 	uint8_t *secret;
246 	uint8_t *outbuf;
247 	struct kdf_sdesc *sdesc = NULL;
248 
249 	if (!params || (!buffer && buflen)) {
250 		ret = -EINVAL;
251 		goto out1;
252 	}
253 	if (copy_from_user(&pcopy, params, sizeof(pcopy)) != 0) {
254 		ret = -EFAULT;
255 		goto out1;
256 	}
257 
258 	if (kdfcopy) {
259 		char *hashname;
260 
261 		if (memchr_inv(kdfcopy->__spare, 0, sizeof(kdfcopy->__spare))) {
262 			ret = -EINVAL;
263 			goto out1;
264 		}
265 
266 		if (buflen > KEYCTL_KDF_MAX_OUTPUT_LEN ||
267 		    kdfcopy->otherinfolen > KEYCTL_KDF_MAX_OI_LEN) {
268 			ret = -EMSGSIZE;
269 			goto out1;
270 		}
271 
272 		/* get KDF name string */
273 		hashname = strndup_user(kdfcopy->hashname, CRYPTO_MAX_ALG_NAME);
274 		if (IS_ERR(hashname)) {
275 			ret = PTR_ERR(hashname);
276 			goto out1;
277 		}
278 
279 		/* allocate KDF from the kernel crypto API */
280 		ret = kdf_alloc(&sdesc, hashname);
281 		kfree(hashname);
282 		if (ret)
283 			goto out1;
284 	}
285 
286 	memset(&dh_inputs, 0, sizeof(dh_inputs));
287 
288 	dlen = dh_data_from_key(pcopy.prime, &dh_inputs.p);
289 	if (dlen < 0) {
290 		ret = dlen;
291 		goto out1;
292 	}
293 	dh_inputs.p_size = dlen;
294 
295 	dlen = dh_data_from_key(pcopy.base, &dh_inputs.g);
296 	if (dlen < 0) {
297 		ret = dlen;
298 		goto out2;
299 	}
300 	dh_inputs.g_size = dlen;
301 
302 	dlen = dh_data_from_key(pcopy.private, &dh_inputs.key);
303 	if (dlen < 0) {
304 		ret = dlen;
305 		goto out2;
306 	}
307 	dh_inputs.key_size = dlen;
308 
309 	secretlen = crypto_dh_key_len(&dh_inputs);
310 	secret = kmalloc(secretlen, GFP_KERNEL);
311 	if (!secret) {
312 		ret = -ENOMEM;
313 		goto out2;
314 	}
315 	ret = crypto_dh_encode_key(secret, secretlen, &dh_inputs);
316 	if (ret)
317 		goto out3;
318 
319 	tfm = crypto_alloc_kpp("dh", 0, 0);
320 	if (IS_ERR(tfm)) {
321 		ret = PTR_ERR(tfm);
322 		goto out3;
323 	}
324 
325 	ret = crypto_kpp_set_secret(tfm, secret, secretlen);
326 	if (ret)
327 		goto out4;
328 
329 	outlen = crypto_kpp_maxsize(tfm);
330 
331 	if (!kdfcopy) {
332 		/*
333 		 * When not using a KDF, buflen 0 is used to read the
334 		 * required buffer length
335 		 */
336 		if (buflen == 0) {
337 			ret = outlen;
338 			goto out4;
339 		} else if (outlen > buflen) {
340 			ret = -EOVERFLOW;
341 			goto out4;
342 		}
343 	}
344 
345 	outbuf = kzalloc(kdfcopy ? (outlen + kdfcopy->otherinfolen) : outlen,
346 			 GFP_KERNEL);
347 	if (!outbuf) {
348 		ret = -ENOMEM;
349 		goto out4;
350 	}
351 
352 	sg_init_one(&outsg, outbuf, outlen);
353 
354 	req = kpp_request_alloc(tfm, GFP_KERNEL);
355 	if (!req) {
356 		ret = -ENOMEM;
357 		goto out5;
358 	}
359 
360 	kpp_request_set_input(req, NULL, 0);
361 	kpp_request_set_output(req, &outsg, outlen);
362 	init_completion(&compl.completion);
363 	kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
364 				 CRYPTO_TFM_REQ_MAY_SLEEP,
365 				 dh_crypto_done, &compl);
366 
367 	/*
368 	 * For DH, generate_public_key and generate_shared_secret are
369 	 * the same calculation
370 	 */
371 	ret = crypto_kpp_generate_public_key(req);
372 	if (ret == -EINPROGRESS) {
373 		wait_for_completion(&compl.completion);
374 		ret = compl.err;
375 		if (ret)
376 			goto out6;
377 	}
378 
379 	if (kdfcopy) {
380 		/*
381 		 * Concatenate SP800-56A otherinfo past DH shared secret -- the
382 		 * input to the KDF is (DH shared secret || otherinfo)
383 		 */
384 		if (copy_from_user(outbuf + req->dst_len, kdfcopy->otherinfo,
385 				   kdfcopy->otherinfolen) != 0) {
386 			ret = -EFAULT;
387 			goto out6;
388 		}
389 
390 		ret = keyctl_dh_compute_kdf(sdesc, buffer, buflen, outbuf,
391 					    req->dst_len + kdfcopy->otherinfolen,
392 					    outlen - req->dst_len);
393 	} else if (copy_to_user(buffer, outbuf, req->dst_len) == 0) {
394 		ret = req->dst_len;
395 	} else {
396 		ret = -EFAULT;
397 	}
398 
399 out6:
400 	kpp_request_free(req);
401 out5:
402 	kzfree(outbuf);
403 out4:
404 	crypto_free_kpp(tfm);
405 out3:
406 	kzfree(secret);
407 out2:
408 	dh_free_data(&dh_inputs);
409 out1:
410 	kdf_dealloc(sdesc);
411 	return ret;
412 }
413 
414 long keyctl_dh_compute(struct keyctl_dh_params __user *params,
415 		       char __user *buffer, size_t buflen,
416 		       struct keyctl_kdf_params __user *kdf)
417 {
418 	struct keyctl_kdf_params kdfcopy;
419 
420 	if (!kdf)
421 		return __keyctl_dh_compute(params, buffer, buflen, NULL);
422 
423 	if (copy_from_user(&kdfcopy, kdf, sizeof(kdfcopy)) != 0)
424 		return -EFAULT;
425 
426 	return __keyctl_dh_compute(params, buffer, buflen, &kdfcopy);
427 }
428