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