xref: /linux/security/keys/encrypted-keys/encrypted.c (revision 08df80a3c51674ab73ae770885a383ca553fbbbf)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2010 IBM Corporation
4  * Copyright (C) 2010 Politecnico di Torino, Italy
5  *                    TORSEC group -- https://security.polito.it
6  *
7  * Authors:
8  * Mimi Zohar <zohar@us.ibm.com>
9  * Roberto Sassu <roberto.sassu@polito.it>
10  *
11  * See Documentation/security/keys/trusted-encrypted.rst
12  */
13 
14 #include <linux/uaccess.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/parser.h>
19 #include <linux/string.h>
20 #include <linux/err.h>
21 #include <keys/user-type.h>
22 #include <keys/trusted-type.h>
23 #include <keys/encrypted-type.h>
24 #include <linux/key-type.h>
25 #include <linux/random.h>
26 #include <linux/rcupdate.h>
27 #include <linux/scatterlist.h>
28 #include <linux/ctype.h>
29 #include <crypto/aes.h>
30 #include <crypto/hash.h>
31 #include <crypto/sha2.h>
32 #include <crypto/skcipher.h>
33 #include <crypto/utils.h>
34 
35 #include "encrypted.h"
36 #include "ecryptfs_format.h"
37 
38 static const char KEY_TRUSTED_PREFIX[] = "trusted:";
39 static const char KEY_USER_PREFIX[] = "user:";
40 static const char hash_alg[] = "sha256";
41 static const char hmac_alg[] = "hmac(sha256)";
42 static const char blkcipher_alg[] = "cbc(aes)";
43 static const char key_format_default[] = "default";
44 static const char key_format_ecryptfs[] = "ecryptfs";
45 static const char key_format_enc32[] = "enc32";
46 static unsigned int ivsize;
47 static int blksize;
48 
49 #define KEY_TRUSTED_PREFIX_LEN (sizeof (KEY_TRUSTED_PREFIX) - 1)
50 #define KEY_USER_PREFIX_LEN (sizeof (KEY_USER_PREFIX) - 1)
51 #define KEY_ECRYPTFS_DESC_LEN 16
52 #define HASH_SIZE SHA256_DIGEST_SIZE
53 #define MAX_DATA_SIZE 4096
54 #define MIN_DATA_SIZE  20
55 #define KEY_ENC32_PAYLOAD_LEN 32
56 
57 static struct crypto_shash *hash_tfm;
58 
59 enum {
60 	Opt_new, Opt_load, Opt_update, Opt_err
61 };
62 
63 enum {
64 	Opt_default, Opt_ecryptfs, Opt_enc32, Opt_error
65 };
66 
67 static const match_table_t key_format_tokens = {
68 	{Opt_default, "default"},
69 	{Opt_ecryptfs, "ecryptfs"},
70 	{Opt_enc32, "enc32"},
71 	{Opt_error, NULL}
72 };
73 
74 static const match_table_t key_tokens = {
75 	{Opt_new, "new"},
76 	{Opt_load, "load"},
77 	{Opt_update, "update"},
78 	{Opt_err, NULL}
79 };
80 
81 static bool user_decrypted_data = IS_ENABLED(CONFIG_USER_DECRYPTED_DATA);
82 module_param(user_decrypted_data, bool, 0);
83 MODULE_PARM_DESC(user_decrypted_data,
84 	"Allow instantiation of encrypted keys using provided decrypted data");
85 
86 static int aes_get_sizes(void)
87 {
88 	struct crypto_skcipher *tfm;
89 
90 	tfm = crypto_alloc_skcipher(blkcipher_alg, 0, CRYPTO_ALG_ASYNC);
91 	if (IS_ERR(tfm)) {
92 		pr_err("encrypted_key: failed to alloc_cipher (%ld)\n",
93 		       PTR_ERR(tfm));
94 		return PTR_ERR(tfm);
95 	}
96 	ivsize = crypto_skcipher_ivsize(tfm);
97 	blksize = crypto_skcipher_blocksize(tfm);
98 	crypto_free_skcipher(tfm);
99 	return 0;
100 }
101 
102 /*
103  * valid_ecryptfs_desc - verify the description of a new/loaded encrypted key
104  *
105  * The description of a encrypted key with format 'ecryptfs' must contain
106  * exactly 16 hexadecimal characters.
107  *
108  */
109 static int valid_ecryptfs_desc(const char *ecryptfs_desc)
110 {
111 	int i;
112 
113 	if (strlen(ecryptfs_desc) != KEY_ECRYPTFS_DESC_LEN) {
114 		pr_err("encrypted_key: key description must be %d hexadecimal "
115 		       "characters long\n", KEY_ECRYPTFS_DESC_LEN);
116 		return -EINVAL;
117 	}
118 
119 	for (i = 0; i < KEY_ECRYPTFS_DESC_LEN; i++) {
120 		if (!isxdigit(ecryptfs_desc[i])) {
121 			pr_err("encrypted_key: key description must contain "
122 			       "only hexadecimal characters\n");
123 			return -EINVAL;
124 		}
125 	}
126 
127 	return 0;
128 }
129 
130 /*
131  * valid_master_desc - verify the 'key-type:desc' of a new/updated master-key
132  *
133  * key-type:= "trusted:" | "user:"
134  * desc:= master-key description
135  *
136  * Verify that 'key-type' is valid and that 'desc' exists. On key update,
137  * only the master key description is permitted to change, not the key-type.
138  * The key-type remains constant.
139  *
140  * On success returns 0, otherwise -EINVAL.
141  */
142 static int valid_master_desc(const char *new_desc, const char *orig_desc)
143 {
144 	int prefix_len;
145 
146 	if (!strncmp(new_desc, KEY_TRUSTED_PREFIX, KEY_TRUSTED_PREFIX_LEN))
147 		prefix_len = KEY_TRUSTED_PREFIX_LEN;
148 	else if (!strncmp(new_desc, KEY_USER_PREFIX, KEY_USER_PREFIX_LEN))
149 		prefix_len = KEY_USER_PREFIX_LEN;
150 	else
151 		return -EINVAL;
152 
153 	if (!new_desc[prefix_len])
154 		return -EINVAL;
155 
156 	if (orig_desc && strncmp(new_desc, orig_desc, prefix_len))
157 		return -EINVAL;
158 
159 	return 0;
160 }
161 
162 /*
163  * datablob_parse - parse the keyctl data
164  *
165  * datablob format:
166  * new [<format>] <master-key name> <decrypted data length> [<decrypted data>]
167  * load [<format>] <master-key name> <decrypted data length>
168  *     <encrypted iv + data>
169  * update <new-master-key name>
170  *
171  * Tokenizes a copy of the keyctl data, returning a pointer to each token,
172  * which is null terminated.
173  *
174  * On success returns 0, otherwise -EINVAL.
175  */
176 static int datablob_parse(char *datablob, const char **format,
177 			  char **master_desc, char **decrypted_datalen,
178 			  char **hex_encoded_iv, char **decrypted_data)
179 {
180 	substring_t args[MAX_OPT_ARGS];
181 	int ret = -EINVAL;
182 	int key_cmd;
183 	int key_format;
184 	char *p, *keyword;
185 
186 	keyword = strsep(&datablob, " \t");
187 	if (!keyword) {
188 		pr_info("encrypted_key: insufficient parameters specified\n");
189 		return ret;
190 	}
191 	key_cmd = match_token(keyword, key_tokens, args);
192 
193 	/* Get optional format: default | ecryptfs */
194 	p = strsep(&datablob, " \t");
195 	if (!p) {
196 		pr_err("encrypted_key: insufficient parameters specified\n");
197 		return ret;
198 	}
199 
200 	key_format = match_token(p, key_format_tokens, args);
201 	switch (key_format) {
202 	case Opt_ecryptfs:
203 	case Opt_enc32:
204 	case Opt_default:
205 		*format = p;
206 		*master_desc = strsep(&datablob, " \t");
207 		break;
208 	case Opt_error:
209 		*master_desc = p;
210 		break;
211 	}
212 
213 	if (!*master_desc) {
214 		pr_info("encrypted_key: master key parameter is missing\n");
215 		goto out;
216 	}
217 
218 	if (valid_master_desc(*master_desc, NULL) < 0) {
219 		pr_info("encrypted_key: master key parameter \'%s\' "
220 			"is invalid\n", *master_desc);
221 		goto out;
222 	}
223 
224 	if (decrypted_datalen) {
225 		*decrypted_datalen = strsep(&datablob, " \t");
226 		if (!*decrypted_datalen) {
227 			pr_info("encrypted_key: keylen parameter is missing\n");
228 			goto out;
229 		}
230 	}
231 
232 	switch (key_cmd) {
233 	case Opt_new:
234 		if (!decrypted_datalen) {
235 			pr_info("encrypted_key: keyword \'%s\' not allowed "
236 				"when called from .update method\n", keyword);
237 			break;
238 		}
239 		*decrypted_data = strsep(&datablob, " \t");
240 		if (!*decrypted_data) {
241 			pr_info("encrypted_key: decrypted_data is missing\n");
242 			break;
243 		}
244 		ret = 0;
245 		break;
246 	case Opt_load:
247 		if (!decrypted_datalen) {
248 			pr_info("encrypted_key: keyword \'%s\' not allowed "
249 				"when called from .update method\n", keyword);
250 			break;
251 		}
252 		*hex_encoded_iv = strsep(&datablob, " \t");
253 		if (!*hex_encoded_iv) {
254 			pr_info("encrypted_key: hex blob is missing\n");
255 			break;
256 		}
257 		ret = 0;
258 		break;
259 	case Opt_update:
260 		if (decrypted_datalen) {
261 			pr_info("encrypted_key: keyword \'%s\' not allowed "
262 				"when called from .instantiate method\n",
263 				keyword);
264 			break;
265 		}
266 		ret = 0;
267 		break;
268 	case Opt_err:
269 		pr_info("encrypted_key: keyword \'%s\' not recognized\n",
270 			keyword);
271 		break;
272 	}
273 out:
274 	return ret;
275 }
276 
277 /*
278  * datablob_format - format as an ascii string, before copying to userspace
279  */
280 static char *datablob_format(struct encrypted_key_payload *epayload,
281 			     size_t asciiblob_len)
282 {
283 	char *ascii_buf, *bufp;
284 	u8 *iv = epayload->iv;
285 	int len;
286 	int i;
287 
288 	ascii_buf = kmalloc(asciiblob_len + 1, GFP_KERNEL);
289 	if (!ascii_buf)
290 		goto out;
291 
292 	ascii_buf[asciiblob_len] = '\0';
293 
294 	/* copy datablob master_desc and datalen strings */
295 	len = sprintf(ascii_buf, "%s %s %s ", epayload->format,
296 		      epayload->master_desc, epayload->datalen);
297 
298 	/* convert the hex encoded iv, encrypted-data and HMAC to ascii */
299 	bufp = &ascii_buf[len];
300 	for (i = 0; i < (asciiblob_len - len) / 2; i++)
301 		bufp = hex_byte_pack(bufp, iv[i]);
302 out:
303 	return ascii_buf;
304 }
305 
306 /*
307  * request_user_key - request the user key
308  *
309  * Use a user provided key to encrypt/decrypt an encrypted-key.
310  */
311 static struct key *request_user_key(const char *master_desc, const u8 **master_key,
312 				    size_t *master_keylen)
313 {
314 	const struct user_key_payload *upayload;
315 	struct key *ukey;
316 
317 	ukey = request_key(&key_type_user, master_desc, NULL);
318 	if (IS_ERR(ukey))
319 		goto error;
320 
321 	down_read(&ukey->sem);
322 	upayload = user_key_payload_locked(ukey);
323 	if (!upayload) {
324 		/* key was revoked before we acquired its semaphore */
325 		up_read(&ukey->sem);
326 		key_put(ukey);
327 		ukey = ERR_PTR(-EKEYREVOKED);
328 		goto error;
329 	}
330 	*master_key = upayload->data;
331 	*master_keylen = upayload->datalen;
332 error:
333 	return ukey;
334 }
335 
336 static int calc_hmac(u8 *digest, const u8 *key, unsigned int keylen,
337 		     const u8 *buf, unsigned int buflen)
338 {
339 	struct crypto_shash *tfm;
340 	int err;
341 
342 	tfm = crypto_alloc_shash(hmac_alg, 0, 0);
343 	if (IS_ERR(tfm)) {
344 		pr_err("encrypted_key: can't alloc %s transform: %ld\n",
345 		       hmac_alg, PTR_ERR(tfm));
346 		return PTR_ERR(tfm);
347 	}
348 
349 	err = crypto_shash_setkey(tfm, key, keylen);
350 	if (!err)
351 		err = crypto_shash_tfm_digest(tfm, buf, buflen, digest);
352 	crypto_free_shash(tfm);
353 	return err;
354 }
355 
356 enum derived_key_type { ENC_KEY, AUTH_KEY };
357 
358 /* Derive authentication/encryption key from trusted key */
359 static int get_derived_key(u8 *derived_key, enum derived_key_type key_type,
360 			   const u8 *master_key, size_t master_keylen)
361 {
362 	u8 *derived_buf;
363 	unsigned int derived_buf_len;
364 	int ret;
365 
366 	derived_buf_len = strlen("AUTH_KEY") + 1 + master_keylen;
367 	if (derived_buf_len < HASH_SIZE)
368 		derived_buf_len = HASH_SIZE;
369 
370 	derived_buf = kzalloc(derived_buf_len, GFP_KERNEL);
371 	if (!derived_buf)
372 		return -ENOMEM;
373 
374 	if (key_type)
375 		strcpy(derived_buf, "AUTH_KEY");
376 	else
377 		strcpy(derived_buf, "ENC_KEY");
378 
379 	memcpy(derived_buf + strlen(derived_buf) + 1, master_key,
380 	       master_keylen);
381 	ret = crypto_shash_tfm_digest(hash_tfm, derived_buf, derived_buf_len,
382 				      derived_key);
383 	kfree_sensitive(derived_buf);
384 	return ret;
385 }
386 
387 static struct skcipher_request *init_skcipher_req(const u8 *key,
388 						  unsigned int key_len)
389 {
390 	struct skcipher_request *req;
391 	struct crypto_skcipher *tfm;
392 	int ret;
393 
394 	tfm = crypto_alloc_skcipher(blkcipher_alg, 0, CRYPTO_ALG_ASYNC);
395 	if (IS_ERR(tfm)) {
396 		pr_err("encrypted_key: failed to load %s transform (%ld)\n",
397 		       blkcipher_alg, PTR_ERR(tfm));
398 		return ERR_CAST(tfm);
399 	}
400 
401 	ret = crypto_skcipher_setkey(tfm, key, key_len);
402 	if (ret < 0) {
403 		pr_err("encrypted_key: failed to setkey (%d)\n", ret);
404 		crypto_free_skcipher(tfm);
405 		return ERR_PTR(ret);
406 	}
407 
408 	req = skcipher_request_alloc(tfm, GFP_KERNEL);
409 	if (!req) {
410 		pr_err("encrypted_key: failed to allocate request for %s\n",
411 		       blkcipher_alg);
412 		crypto_free_skcipher(tfm);
413 		return ERR_PTR(-ENOMEM);
414 	}
415 
416 	skcipher_request_set_callback(req, 0, NULL, NULL);
417 	return req;
418 }
419 
420 static struct key *request_master_key(struct encrypted_key_payload *epayload,
421 				      const u8 **master_key, size_t *master_keylen)
422 {
423 	struct key *mkey = ERR_PTR(-EINVAL);
424 
425 	if (!strncmp(epayload->master_desc, KEY_TRUSTED_PREFIX,
426 		     KEY_TRUSTED_PREFIX_LEN)) {
427 		mkey = request_trusted_key(epayload->master_desc +
428 					   KEY_TRUSTED_PREFIX_LEN,
429 					   master_key, master_keylen);
430 	} else if (!strncmp(epayload->master_desc, KEY_USER_PREFIX,
431 			    KEY_USER_PREFIX_LEN)) {
432 		mkey = request_user_key(epayload->master_desc +
433 					KEY_USER_PREFIX_LEN,
434 					master_key, master_keylen);
435 	} else
436 		goto out;
437 
438 	if (IS_ERR(mkey)) {
439 		int ret = PTR_ERR(mkey);
440 
441 		if (ret == -ENOTSUPP)
442 			pr_info("encrypted_key: key %s not supported",
443 				epayload->master_desc);
444 		else
445 			pr_info("encrypted_key: key %s not found",
446 				epayload->master_desc);
447 		goto out;
448 	}
449 
450 	dump_master_key(*master_key, *master_keylen);
451 out:
452 	return mkey;
453 }
454 
455 /* Before returning data to userspace, encrypt decrypted data. */
456 static int derived_key_encrypt(struct encrypted_key_payload *epayload,
457 			       const u8 *derived_key,
458 			       unsigned int derived_keylen)
459 {
460 	struct scatterlist sg_in[2];
461 	struct scatterlist sg_out[1];
462 	struct crypto_skcipher *tfm;
463 	struct skcipher_request *req;
464 	unsigned int encrypted_datalen;
465 	u8 iv[AES_BLOCK_SIZE];
466 	int ret;
467 
468 	encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
469 
470 	req = init_skcipher_req(derived_key, derived_keylen);
471 	ret = PTR_ERR(req);
472 	if (IS_ERR(req))
473 		goto out;
474 	dump_decrypted_data(epayload);
475 
476 	sg_init_table(sg_in, 2);
477 	sg_set_buf(&sg_in[0], epayload->decrypted_data,
478 		   epayload->decrypted_datalen);
479 	sg_set_page(&sg_in[1], ZERO_PAGE(0), AES_BLOCK_SIZE, 0);
480 
481 	sg_init_table(sg_out, 1);
482 	sg_set_buf(sg_out, epayload->encrypted_data, encrypted_datalen);
483 
484 	memcpy(iv, epayload->iv, sizeof(iv));
485 	skcipher_request_set_crypt(req, sg_in, sg_out, encrypted_datalen, iv);
486 	ret = crypto_skcipher_encrypt(req);
487 	tfm = crypto_skcipher_reqtfm(req);
488 	skcipher_request_free(req);
489 	crypto_free_skcipher(tfm);
490 	if (ret < 0)
491 		pr_err("encrypted_key: failed to encrypt (%d)\n", ret);
492 	else
493 		dump_encrypted_data(epayload, encrypted_datalen);
494 out:
495 	return ret;
496 }
497 
498 static int datablob_hmac_append(struct encrypted_key_payload *epayload,
499 				const u8 *master_key, size_t master_keylen)
500 {
501 	u8 derived_key[HASH_SIZE];
502 	u8 *digest;
503 	int ret;
504 
505 	ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen);
506 	if (ret < 0)
507 		goto out;
508 
509 	digest = epayload->format + epayload->datablob_len;
510 	ret = calc_hmac(digest, derived_key, sizeof derived_key,
511 			epayload->format, epayload->datablob_len);
512 	if (!ret)
513 		dump_hmac(NULL, digest, HASH_SIZE);
514 out:
515 	memzero_explicit(derived_key, sizeof(derived_key));
516 	return ret;
517 }
518 
519 /* verify HMAC before decrypting encrypted key */
520 static int datablob_hmac_verify(struct encrypted_key_payload *epayload,
521 				const u8 *format, const u8 *master_key,
522 				size_t master_keylen)
523 {
524 	u8 derived_key[HASH_SIZE];
525 	u8 digest[HASH_SIZE];
526 	int ret;
527 	char *p;
528 	unsigned short len;
529 
530 	ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen);
531 	if (ret < 0)
532 		goto out;
533 
534 	len = epayload->datablob_len;
535 	if (!format) {
536 		p = epayload->master_desc;
537 		len -= strlen(epayload->format) + 1;
538 	} else
539 		p = epayload->format;
540 
541 	ret = calc_hmac(digest, derived_key, sizeof derived_key, p, len);
542 	if (ret < 0)
543 		goto out;
544 	ret = crypto_memneq(digest, epayload->format + epayload->datablob_len,
545 			    sizeof(digest));
546 	if (ret) {
547 		ret = -EINVAL;
548 		dump_hmac("datablob",
549 			  epayload->format + epayload->datablob_len,
550 			  HASH_SIZE);
551 		dump_hmac("calc", digest, HASH_SIZE);
552 	}
553 out:
554 	memzero_explicit(derived_key, sizeof(derived_key));
555 	return ret;
556 }
557 
558 static int derived_key_decrypt(struct encrypted_key_payload *epayload,
559 			       const u8 *derived_key,
560 			       unsigned int derived_keylen)
561 {
562 	struct scatterlist sg_in[1];
563 	struct scatterlist sg_out[2];
564 	struct crypto_skcipher *tfm;
565 	struct skcipher_request *req;
566 	unsigned int encrypted_datalen;
567 	u8 iv[AES_BLOCK_SIZE];
568 	u8 *pad;
569 	int ret;
570 
571 	/* Throwaway buffer to hold the unused zero padding at the end */
572 	pad = kmalloc(AES_BLOCK_SIZE, GFP_KERNEL);
573 	if (!pad)
574 		return -ENOMEM;
575 
576 	encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
577 	req = init_skcipher_req(derived_key, derived_keylen);
578 	ret = PTR_ERR(req);
579 	if (IS_ERR(req))
580 		goto out;
581 	dump_encrypted_data(epayload, encrypted_datalen);
582 
583 	sg_init_table(sg_in, 1);
584 	sg_init_table(sg_out, 2);
585 	sg_set_buf(sg_in, epayload->encrypted_data, encrypted_datalen);
586 	sg_set_buf(&sg_out[0], epayload->decrypted_data,
587 		   epayload->decrypted_datalen);
588 	sg_set_buf(&sg_out[1], pad, AES_BLOCK_SIZE);
589 
590 	memcpy(iv, epayload->iv, sizeof(iv));
591 	skcipher_request_set_crypt(req, sg_in, sg_out, encrypted_datalen, iv);
592 	ret = crypto_skcipher_decrypt(req);
593 	tfm = crypto_skcipher_reqtfm(req);
594 	skcipher_request_free(req);
595 	crypto_free_skcipher(tfm);
596 	if (ret < 0)
597 		goto out;
598 	dump_decrypted_data(epayload);
599 out:
600 	kfree(pad);
601 	return ret;
602 }
603 
604 /* Allocate memory for decrypted key and datablob. */
605 static struct encrypted_key_payload *encrypted_key_alloc(struct key *key,
606 							 const char *format,
607 							 const char *master_desc,
608 							 const char *datalen,
609 							 const char *decrypted_data)
610 {
611 	struct encrypted_key_payload *epayload = NULL;
612 	unsigned short datablob_len;
613 	unsigned short decrypted_datalen;
614 	unsigned short payload_datalen;
615 	unsigned int encrypted_datalen;
616 	unsigned int format_len;
617 	long dlen;
618 	int i;
619 	int ret;
620 
621 	ret = kstrtol(datalen, 10, &dlen);
622 	if (ret < 0 || dlen < MIN_DATA_SIZE || dlen > MAX_DATA_SIZE)
623 		return ERR_PTR(-EINVAL);
624 
625 	format_len = (!format) ? strlen(key_format_default) : strlen(format);
626 	decrypted_datalen = dlen;
627 	payload_datalen = decrypted_datalen;
628 
629 	if (decrypted_data) {
630 		if (!user_decrypted_data) {
631 			pr_err("encrypted key: instantiation of keys using provided decrypted data is disabled since CONFIG_USER_DECRYPTED_DATA is set to false\n");
632 			return ERR_PTR(-EINVAL);
633 		}
634 		if (strlen(decrypted_data) != decrypted_datalen * 2) {
635 			pr_err("encrypted key: decrypted data provided does not match decrypted data length provided\n");
636 			return ERR_PTR(-EINVAL);
637 		}
638 		for (i = 0; i < strlen(decrypted_data); i++) {
639 			if (!isxdigit(decrypted_data[i])) {
640 				pr_err("encrypted key: decrypted data provided must contain only hexadecimal characters\n");
641 				return ERR_PTR(-EINVAL);
642 			}
643 		}
644 	}
645 
646 	if (format) {
647 		if (!strcmp(format, key_format_ecryptfs)) {
648 			if (dlen != ECRYPTFS_MAX_KEY_BYTES) {
649 				pr_err("encrypted_key: keylen for the ecryptfs format must be equal to %d bytes\n",
650 					ECRYPTFS_MAX_KEY_BYTES);
651 				return ERR_PTR(-EINVAL);
652 			}
653 			decrypted_datalen = ECRYPTFS_MAX_KEY_BYTES;
654 			payload_datalen = sizeof(struct ecryptfs_auth_tok);
655 		} else if (!strcmp(format, key_format_enc32)) {
656 			if (decrypted_datalen != KEY_ENC32_PAYLOAD_LEN) {
657 				pr_err("encrypted_key: enc32 key payload incorrect length: %d\n",
658 						decrypted_datalen);
659 				return ERR_PTR(-EINVAL);
660 			}
661 		}
662 	}
663 
664 	encrypted_datalen = roundup(decrypted_datalen, blksize);
665 
666 	datablob_len = format_len + 1 + strlen(master_desc) + 1
667 	    + strlen(datalen) + 1 + ivsize + 1 + encrypted_datalen;
668 
669 	ret = key_payload_reserve(key, payload_datalen + datablob_len
670 				  + HASH_SIZE + 1);
671 	if (ret < 0)
672 		return ERR_PTR(ret);
673 
674 	epayload = kzalloc(sizeof(*epayload) + payload_datalen +
675 			   datablob_len + HASH_SIZE + 1, GFP_KERNEL);
676 	if (!epayload)
677 		return ERR_PTR(-ENOMEM);
678 
679 	epayload->payload_datalen = payload_datalen;
680 	epayload->decrypted_datalen = decrypted_datalen;
681 	epayload->datablob_len = datablob_len;
682 	return epayload;
683 }
684 
685 static int encrypted_key_decrypt(struct encrypted_key_payload *epayload,
686 				 const char *format, const char *hex_encoded_iv)
687 {
688 	struct key *mkey;
689 	u8 derived_key[HASH_SIZE];
690 	const u8 *master_key;
691 	u8 *hmac;
692 	const char *hex_encoded_data;
693 	unsigned int encrypted_datalen;
694 	size_t master_keylen;
695 	size_t asciilen;
696 	int ret;
697 
698 	encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
699 	asciilen = (ivsize + 1 + encrypted_datalen + HASH_SIZE) * 2;
700 	if (strlen(hex_encoded_iv) != asciilen)
701 		return -EINVAL;
702 
703 	hex_encoded_data = hex_encoded_iv + (2 * ivsize) + 2;
704 	ret = hex2bin(epayload->iv, hex_encoded_iv, ivsize);
705 	if (ret < 0)
706 		return -EINVAL;
707 	ret = hex2bin(epayload->encrypted_data, hex_encoded_data,
708 		      encrypted_datalen);
709 	if (ret < 0)
710 		return -EINVAL;
711 
712 	hmac = epayload->format + epayload->datablob_len;
713 	ret = hex2bin(hmac, hex_encoded_data + (encrypted_datalen * 2),
714 		      HASH_SIZE);
715 	if (ret < 0)
716 		return -EINVAL;
717 
718 	mkey = request_master_key(epayload, &master_key, &master_keylen);
719 	if (IS_ERR(mkey))
720 		return PTR_ERR(mkey);
721 
722 	ret = datablob_hmac_verify(epayload, format, master_key, master_keylen);
723 	if (ret < 0) {
724 		pr_err("encrypted_key: bad hmac (%d)\n", ret);
725 		goto out;
726 	}
727 
728 	ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen);
729 	if (ret < 0)
730 		goto out;
731 
732 	ret = derived_key_decrypt(epayload, derived_key, sizeof derived_key);
733 	if (ret < 0)
734 		pr_err("encrypted_key: failed to decrypt key (%d)\n", ret);
735 out:
736 	up_read(&mkey->sem);
737 	key_put(mkey);
738 	memzero_explicit(derived_key, sizeof(derived_key));
739 	return ret;
740 }
741 
742 static void __ekey_init(struct encrypted_key_payload *epayload,
743 			const char *format, const char *master_desc,
744 			const char *datalen)
745 {
746 	unsigned int format_len;
747 
748 	format_len = (!format) ? strlen(key_format_default) : strlen(format);
749 	epayload->format = epayload->payload_data + epayload->payload_datalen;
750 	epayload->master_desc = epayload->format + format_len + 1;
751 	epayload->datalen = epayload->master_desc + strlen(master_desc) + 1;
752 	epayload->iv = epayload->datalen + strlen(datalen) + 1;
753 	epayload->encrypted_data = epayload->iv + ivsize + 1;
754 	epayload->decrypted_data = epayload->payload_data;
755 
756 	if (!format)
757 		memcpy(epayload->format, key_format_default, format_len);
758 	else {
759 		if (!strcmp(format, key_format_ecryptfs))
760 			epayload->decrypted_data =
761 				ecryptfs_get_auth_tok_key((struct ecryptfs_auth_tok *)epayload->payload_data);
762 
763 		memcpy(epayload->format, format, format_len);
764 	}
765 
766 	memcpy(epayload->master_desc, master_desc, strlen(master_desc));
767 	memcpy(epayload->datalen, datalen, strlen(datalen));
768 }
769 
770 /*
771  * encrypted_init - initialize an encrypted key
772  *
773  * For a new key, use either a random number or user-provided decrypted data in
774  * case it is provided. A random number is used for the iv in both cases. For
775  * an old key, decrypt the hex encoded data.
776  */
777 static int encrypted_init(struct encrypted_key_payload *epayload,
778 			  const char *key_desc, const char *format,
779 			  const char *master_desc, const char *datalen,
780 			  const char *hex_encoded_iv, const char *decrypted_data)
781 {
782 	int ret = 0;
783 
784 	if (format && !strcmp(format, key_format_ecryptfs)) {
785 		ret = valid_ecryptfs_desc(key_desc);
786 		if (ret < 0)
787 			return ret;
788 
789 		ecryptfs_fill_auth_tok((struct ecryptfs_auth_tok *)epayload->payload_data,
790 				       key_desc);
791 	}
792 
793 	__ekey_init(epayload, format, master_desc, datalen);
794 	if (hex_encoded_iv) {
795 		ret = encrypted_key_decrypt(epayload, format, hex_encoded_iv);
796 	} else if (decrypted_data) {
797 		get_random_bytes(epayload->iv, ivsize);
798 		ret = hex2bin(epayload->decrypted_data, decrypted_data,
799 			      epayload->decrypted_datalen);
800 	} else {
801 		get_random_bytes(epayload->iv, ivsize);
802 		get_random_bytes(epayload->decrypted_data, epayload->decrypted_datalen);
803 	}
804 	return ret;
805 }
806 
807 /*
808  * encrypted_instantiate - instantiate an encrypted key
809  *
810  * Instantiates the key:
811  * - by decrypting an existing encrypted datablob, or
812  * - by creating a new encrypted key based on a kernel random number, or
813  * - using provided decrypted data.
814  *
815  * On success, return 0. Otherwise return errno.
816  */
817 static int encrypted_instantiate(struct key *key,
818 				 struct key_preparsed_payload *prep)
819 {
820 	struct encrypted_key_payload *epayload = NULL;
821 	char *datablob = NULL;
822 	const char *format = NULL;
823 	char *master_desc = NULL;
824 	char *decrypted_datalen = NULL;
825 	char *hex_encoded_iv = NULL;
826 	char *decrypted_data = NULL;
827 	size_t datalen = prep->datalen;
828 	int ret;
829 
830 	if (datalen <= 0 || datalen > 32767 || !prep->data)
831 		return -EINVAL;
832 
833 	datablob = kmalloc(datalen + 1, GFP_KERNEL);
834 	if (!datablob)
835 		return -ENOMEM;
836 	datablob[datalen] = 0;
837 	memcpy(datablob, prep->data, datalen);
838 	ret = datablob_parse(datablob, &format, &master_desc,
839 			     &decrypted_datalen, &hex_encoded_iv, &decrypted_data);
840 	if (ret < 0)
841 		goto out;
842 
843 	epayload = encrypted_key_alloc(key, format, master_desc,
844 				       decrypted_datalen, decrypted_data);
845 	if (IS_ERR(epayload)) {
846 		ret = PTR_ERR(epayload);
847 		goto out;
848 	}
849 	ret = encrypted_init(epayload, key->description, format, master_desc,
850 			     decrypted_datalen, hex_encoded_iv, decrypted_data);
851 	if (ret < 0) {
852 		kfree_sensitive(epayload);
853 		goto out;
854 	}
855 
856 	rcu_assign_keypointer(key, epayload);
857 out:
858 	kfree_sensitive(datablob);
859 	return ret;
860 }
861 
862 static void encrypted_rcu_free(struct rcu_head *rcu)
863 {
864 	struct encrypted_key_payload *epayload;
865 
866 	epayload = container_of(rcu, struct encrypted_key_payload, rcu);
867 	kfree_sensitive(epayload);
868 }
869 
870 /*
871  * encrypted_update - update the master key description
872  *
873  * Change the master key description for an existing encrypted key.
874  * The next read will return an encrypted datablob using the new
875  * master key description.
876  *
877  * On success, return 0. Otherwise return errno.
878  */
879 static int encrypted_update(struct key *key, struct key_preparsed_payload *prep)
880 {
881 	struct encrypted_key_payload *epayload = key->payload.data[0];
882 	struct encrypted_key_payload *new_epayload;
883 	char *buf;
884 	char *new_master_desc = NULL;
885 	const char *format = NULL;
886 	size_t datalen = prep->datalen;
887 	int ret = 0;
888 
889 	if (key_is_negative(key))
890 		return -ENOKEY;
891 	if (datalen <= 0 || datalen > 32767 || !prep->data)
892 		return -EINVAL;
893 
894 	buf = kmalloc(datalen + 1, GFP_KERNEL);
895 	if (!buf)
896 		return -ENOMEM;
897 
898 	buf[datalen] = 0;
899 	memcpy(buf, prep->data, datalen);
900 	ret = datablob_parse(buf, &format, &new_master_desc, NULL, NULL, NULL);
901 	if (ret < 0)
902 		goto out;
903 
904 	ret = valid_master_desc(new_master_desc, epayload->master_desc);
905 	if (ret < 0)
906 		goto out;
907 
908 	new_epayload = encrypted_key_alloc(key, epayload->format,
909 					   new_master_desc, epayload->datalen, NULL);
910 	if (IS_ERR(new_epayload)) {
911 		ret = PTR_ERR(new_epayload);
912 		goto out;
913 	}
914 
915 	__ekey_init(new_epayload, epayload->format, new_master_desc,
916 		    epayload->datalen);
917 
918 	memcpy(new_epayload->iv, epayload->iv, ivsize);
919 	memcpy(new_epayload->payload_data, epayload->payload_data,
920 	       epayload->payload_datalen);
921 
922 	rcu_assign_keypointer(key, new_epayload);
923 	call_rcu(&epayload->rcu, encrypted_rcu_free);
924 out:
925 	kfree_sensitive(buf);
926 	return ret;
927 }
928 
929 /*
930  * encrypted_read - format and copy out the encrypted data
931  *
932  * The resulting datablob format is:
933  * <master-key name> <decrypted data length> <encrypted iv> <encrypted data>
934  *
935  * On success, return to userspace the encrypted key datablob size.
936  */
937 static long encrypted_read(const struct key *key, char *buffer,
938 			   size_t buflen)
939 {
940 	struct encrypted_key_payload *epayload;
941 	struct key *mkey;
942 	const u8 *master_key;
943 	size_t master_keylen;
944 	char derived_key[HASH_SIZE];
945 	char *ascii_buf;
946 	size_t asciiblob_len;
947 	int ret;
948 
949 	epayload = dereference_key_locked(key);
950 
951 	/* returns the hex encoded iv, encrypted-data, and hmac as ascii */
952 	asciiblob_len = epayload->datablob_len + ivsize + 1
953 	    + roundup(epayload->decrypted_datalen, blksize)
954 	    + (HASH_SIZE * 2);
955 
956 	if (!buffer || buflen < asciiblob_len)
957 		return asciiblob_len;
958 
959 	mkey = request_master_key(epayload, &master_key, &master_keylen);
960 	if (IS_ERR(mkey))
961 		return PTR_ERR(mkey);
962 
963 	ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen);
964 	if (ret < 0)
965 		goto out;
966 
967 	ret = derived_key_encrypt(epayload, derived_key, sizeof derived_key);
968 	if (ret < 0)
969 		goto out;
970 
971 	ret = datablob_hmac_append(epayload, master_key, master_keylen);
972 	if (ret < 0)
973 		goto out;
974 
975 	ascii_buf = datablob_format(epayload, asciiblob_len);
976 	if (!ascii_buf) {
977 		ret = -ENOMEM;
978 		goto out;
979 	}
980 
981 	up_read(&mkey->sem);
982 	key_put(mkey);
983 	memzero_explicit(derived_key, sizeof(derived_key));
984 
985 	memcpy(buffer, ascii_buf, asciiblob_len);
986 	kfree_sensitive(ascii_buf);
987 
988 	return asciiblob_len;
989 out:
990 	up_read(&mkey->sem);
991 	key_put(mkey);
992 	memzero_explicit(derived_key, sizeof(derived_key));
993 	return ret;
994 }
995 
996 /*
997  * encrypted_destroy - clear and free the key's payload
998  */
999 static void encrypted_destroy(struct key *key)
1000 {
1001 	kfree_sensitive(key->payload.data[0]);
1002 }
1003 
1004 struct key_type key_type_encrypted = {
1005 	.name = "encrypted",
1006 	.instantiate = encrypted_instantiate,
1007 	.update = encrypted_update,
1008 	.destroy = encrypted_destroy,
1009 	.describe = user_describe,
1010 	.read = encrypted_read,
1011 };
1012 EXPORT_SYMBOL_GPL(key_type_encrypted);
1013 
1014 static int __init init_encrypted(void)
1015 {
1016 	int ret;
1017 
1018 	hash_tfm = crypto_alloc_shash(hash_alg, 0, 0);
1019 	if (IS_ERR(hash_tfm)) {
1020 		pr_err("encrypted_key: can't allocate %s transform: %ld\n",
1021 		       hash_alg, PTR_ERR(hash_tfm));
1022 		return PTR_ERR(hash_tfm);
1023 	}
1024 
1025 	ret = aes_get_sizes();
1026 	if (ret < 0)
1027 		goto out;
1028 	ret = register_key_type(&key_type_encrypted);
1029 	if (ret < 0)
1030 		goto out;
1031 	return 0;
1032 out:
1033 	crypto_free_shash(hash_tfm);
1034 	return ret;
1035 
1036 }
1037 
1038 static void __exit cleanup_encrypted(void)
1039 {
1040 	crypto_free_shash(hash_tfm);
1041 	unregister_key_type(&key_type_encrypted);
1042 }
1043 
1044 late_initcall(init_encrypted);
1045 module_exit(cleanup_encrypted);
1046 
1047 MODULE_LICENSE("GPL");
1048