Merge branch 'for-next' of master.kernel.org:/pub/scm/linux/kernel/git/balbi/usb...
[zen-stable.git] / security / keys / encrypted.c
blobb1cba5bf0a5e3d092b57318a242014e915af04ff
1 /*
2 * Copyright (C) 2010 IBM Corporation
4 * Author:
5 * Mimi Zohar <zohar@us.ibm.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, version 2 of the License.
11 * See Documentation/security/keys-trusted-encrypted.txt
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/crypto.h>
29 #include <crypto/hash.h>
30 #include <crypto/sha.h>
31 #include <crypto/aes.h>
33 #include "encrypted.h"
35 static const char KEY_TRUSTED_PREFIX[] = "trusted:";
36 static const char KEY_USER_PREFIX[] = "user:";
37 static const char hash_alg[] = "sha256";
38 static const char hmac_alg[] = "hmac(sha256)";
39 static const char blkcipher_alg[] = "cbc(aes)";
40 static unsigned int ivsize;
41 static int blksize;
43 #define KEY_TRUSTED_PREFIX_LEN (sizeof (KEY_TRUSTED_PREFIX) - 1)
44 #define KEY_USER_PREFIX_LEN (sizeof (KEY_USER_PREFIX) - 1)
45 #define HASH_SIZE SHA256_DIGEST_SIZE
46 #define MAX_DATA_SIZE 4096
47 #define MIN_DATA_SIZE 20
49 struct sdesc {
50 struct shash_desc shash;
51 char ctx[];
54 static struct crypto_shash *hashalg;
55 static struct crypto_shash *hmacalg;
57 enum {
58 Opt_err = -1, Opt_new, Opt_load, Opt_update
61 static const match_table_t key_tokens = {
62 {Opt_new, "new"},
63 {Opt_load, "load"},
64 {Opt_update, "update"},
65 {Opt_err, NULL}
68 static int aes_get_sizes(void)
70 struct crypto_blkcipher *tfm;
72 tfm = crypto_alloc_blkcipher(blkcipher_alg, 0, CRYPTO_ALG_ASYNC);
73 if (IS_ERR(tfm)) {
74 pr_err("encrypted_key: failed to alloc_cipher (%ld)\n",
75 PTR_ERR(tfm));
76 return PTR_ERR(tfm);
78 ivsize = crypto_blkcipher_ivsize(tfm);
79 blksize = crypto_blkcipher_blocksize(tfm);
80 crypto_free_blkcipher(tfm);
81 return 0;
85 * valid_master_desc - verify the 'key-type:desc' of a new/updated master-key
87 * key-type:= "trusted:" | "encrypted:"
88 * desc:= master-key description
90 * Verify that 'key-type' is valid and that 'desc' exists. On key update,
91 * only the master key description is permitted to change, not the key-type.
92 * The key-type remains constant.
94 * On success returns 0, otherwise -EINVAL.
96 static int valid_master_desc(const char *new_desc, const char *orig_desc)
98 if (!memcmp(new_desc, KEY_TRUSTED_PREFIX, KEY_TRUSTED_PREFIX_LEN)) {
99 if (strlen(new_desc) == KEY_TRUSTED_PREFIX_LEN)
100 goto out;
101 if (orig_desc)
102 if (memcmp(new_desc, orig_desc, KEY_TRUSTED_PREFIX_LEN))
103 goto out;
104 } else if (!memcmp(new_desc, KEY_USER_PREFIX, KEY_USER_PREFIX_LEN)) {
105 if (strlen(new_desc) == KEY_USER_PREFIX_LEN)
106 goto out;
107 if (orig_desc)
108 if (memcmp(new_desc, orig_desc, KEY_USER_PREFIX_LEN))
109 goto out;
110 } else
111 goto out;
112 return 0;
113 out:
114 return -EINVAL;
118 * datablob_parse - parse the keyctl data
120 * datablob format:
121 * new <master-key name> <decrypted data length>
122 * load <master-key name> <decrypted data length> <encrypted iv + data>
123 * update <new-master-key name>
125 * Tokenizes a copy of the keyctl data, returning a pointer to each token,
126 * which is null terminated.
128 * On success returns 0, otherwise -EINVAL.
130 static int datablob_parse(char *datablob, char **master_desc,
131 char **decrypted_datalen, char **hex_encoded_iv)
133 substring_t args[MAX_OPT_ARGS];
134 int ret = -EINVAL;
135 int key_cmd;
136 char *p;
138 p = strsep(&datablob, " \t");
139 if (!p)
140 return ret;
141 key_cmd = match_token(p, key_tokens, args);
143 *master_desc = strsep(&datablob, " \t");
144 if (!*master_desc)
145 goto out;
147 if (valid_master_desc(*master_desc, NULL) < 0)
148 goto out;
150 if (decrypted_datalen) {
151 *decrypted_datalen = strsep(&datablob, " \t");
152 if (!*decrypted_datalen)
153 goto out;
156 switch (key_cmd) {
157 case Opt_new:
158 if (!decrypted_datalen)
159 break;
160 ret = 0;
161 break;
162 case Opt_load:
163 if (!decrypted_datalen)
164 break;
165 *hex_encoded_iv = strsep(&datablob, " \t");
166 if (!*hex_encoded_iv)
167 break;
168 ret = 0;
169 break;
170 case Opt_update:
171 if (decrypted_datalen)
172 break;
173 ret = 0;
174 break;
175 case Opt_err:
176 break;
178 out:
179 return ret;
183 * datablob_format - format as an ascii string, before copying to userspace
185 static char *datablob_format(struct encrypted_key_payload *epayload,
186 size_t asciiblob_len)
188 char *ascii_buf, *bufp;
189 u8 *iv = epayload->iv;
190 int len;
191 int i;
193 ascii_buf = kmalloc(asciiblob_len + 1, GFP_KERNEL);
194 if (!ascii_buf)
195 goto out;
197 ascii_buf[asciiblob_len] = '\0';
199 /* copy datablob master_desc and datalen strings */
200 len = sprintf(ascii_buf, "%s %s ", epayload->master_desc,
201 epayload->datalen);
203 /* convert the hex encoded iv, encrypted-data and HMAC to ascii */
204 bufp = &ascii_buf[len];
205 for (i = 0; i < (asciiblob_len - len) / 2; i++)
206 bufp = pack_hex_byte(bufp, iv[i]);
207 out:
208 return ascii_buf;
212 * request_trusted_key - request the trusted key
214 * Trusted keys are sealed to PCRs and other metadata. Although userspace
215 * manages both trusted/encrypted key-types, like the encrypted key type
216 * data, trusted key type data is not visible decrypted from userspace.
218 static struct key *request_trusted_key(const char *trusted_desc,
219 u8 **master_key, size_t *master_keylen)
221 struct trusted_key_payload *tpayload;
222 struct key *tkey;
224 tkey = request_key(&key_type_trusted, trusted_desc, NULL);
225 if (IS_ERR(tkey))
226 goto error;
228 down_read(&tkey->sem);
229 tpayload = rcu_dereference(tkey->payload.data);
230 *master_key = tpayload->key;
231 *master_keylen = tpayload->key_len;
232 error:
233 return tkey;
237 * request_user_key - request the user key
239 * Use a user provided key to encrypt/decrypt an encrypted-key.
241 static struct key *request_user_key(const char *master_desc, u8 **master_key,
242 size_t *master_keylen)
244 struct user_key_payload *upayload;
245 struct key *ukey;
247 ukey = request_key(&key_type_user, master_desc, NULL);
248 if (IS_ERR(ukey))
249 goto error;
251 down_read(&ukey->sem);
252 upayload = rcu_dereference(ukey->payload.data);
253 *master_key = upayload->data;
254 *master_keylen = upayload->datalen;
255 error:
256 return ukey;
259 static struct sdesc *alloc_sdesc(struct crypto_shash *alg)
261 struct sdesc *sdesc;
262 int size;
264 size = sizeof(struct shash_desc) + crypto_shash_descsize(alg);
265 sdesc = kmalloc(size, GFP_KERNEL);
266 if (!sdesc)
267 return ERR_PTR(-ENOMEM);
268 sdesc->shash.tfm = alg;
269 sdesc->shash.flags = 0x0;
270 return sdesc;
273 static int calc_hmac(u8 *digest, const u8 *key, unsigned int keylen,
274 const u8 *buf, unsigned int buflen)
276 struct sdesc *sdesc;
277 int ret;
279 sdesc = alloc_sdesc(hmacalg);
280 if (IS_ERR(sdesc)) {
281 pr_info("encrypted_key: can't alloc %s\n", hmac_alg);
282 return PTR_ERR(sdesc);
285 ret = crypto_shash_setkey(hmacalg, key, keylen);
286 if (!ret)
287 ret = crypto_shash_digest(&sdesc->shash, buf, buflen, digest);
288 kfree(sdesc);
289 return ret;
292 static int calc_hash(u8 *digest, const u8 *buf, unsigned int buflen)
294 struct sdesc *sdesc;
295 int ret;
297 sdesc = alloc_sdesc(hashalg);
298 if (IS_ERR(sdesc)) {
299 pr_info("encrypted_key: can't alloc %s\n", hash_alg);
300 return PTR_ERR(sdesc);
303 ret = crypto_shash_digest(&sdesc->shash, buf, buflen, digest);
304 kfree(sdesc);
305 return ret;
308 enum derived_key_type { ENC_KEY, AUTH_KEY };
310 /* Derive authentication/encryption key from trusted key */
311 static int get_derived_key(u8 *derived_key, enum derived_key_type key_type,
312 const u8 *master_key, size_t master_keylen)
314 u8 *derived_buf;
315 unsigned int derived_buf_len;
316 int ret;
318 derived_buf_len = strlen("AUTH_KEY") + 1 + master_keylen;
319 if (derived_buf_len < HASH_SIZE)
320 derived_buf_len = HASH_SIZE;
322 derived_buf = kzalloc(derived_buf_len, GFP_KERNEL);
323 if (!derived_buf) {
324 pr_err("encrypted_key: out of memory\n");
325 return -ENOMEM;
327 if (key_type)
328 strcpy(derived_buf, "AUTH_KEY");
329 else
330 strcpy(derived_buf, "ENC_KEY");
332 memcpy(derived_buf + strlen(derived_buf) + 1, master_key,
333 master_keylen);
334 ret = calc_hash(derived_key, derived_buf, derived_buf_len);
335 kfree(derived_buf);
336 return ret;
339 static int init_blkcipher_desc(struct blkcipher_desc *desc, const u8 *key,
340 unsigned int key_len, const u8 *iv,
341 unsigned int ivsize)
343 int ret;
345 desc->tfm = crypto_alloc_blkcipher(blkcipher_alg, 0, CRYPTO_ALG_ASYNC);
346 if (IS_ERR(desc->tfm)) {
347 pr_err("encrypted_key: failed to load %s transform (%ld)\n",
348 blkcipher_alg, PTR_ERR(desc->tfm));
349 return PTR_ERR(desc->tfm);
351 desc->flags = 0;
353 ret = crypto_blkcipher_setkey(desc->tfm, key, key_len);
354 if (ret < 0) {
355 pr_err("encrypted_key: failed to setkey (%d)\n", ret);
356 crypto_free_blkcipher(desc->tfm);
357 return ret;
359 crypto_blkcipher_set_iv(desc->tfm, iv, ivsize);
360 return 0;
363 static struct key *request_master_key(struct encrypted_key_payload *epayload,
364 u8 **master_key, size_t *master_keylen)
366 struct key *mkey = NULL;
368 if (!strncmp(epayload->master_desc, KEY_TRUSTED_PREFIX,
369 KEY_TRUSTED_PREFIX_LEN)) {
370 mkey = request_trusted_key(epayload->master_desc +
371 KEY_TRUSTED_PREFIX_LEN,
372 master_key, master_keylen);
373 } else if (!strncmp(epayload->master_desc, KEY_USER_PREFIX,
374 KEY_USER_PREFIX_LEN)) {
375 mkey = request_user_key(epayload->master_desc +
376 KEY_USER_PREFIX_LEN,
377 master_key, master_keylen);
378 } else
379 goto out;
381 if (IS_ERR(mkey))
382 pr_info("encrypted_key: key %s not found",
383 epayload->master_desc);
384 if (mkey)
385 dump_master_key(*master_key, *master_keylen);
386 out:
387 return mkey;
390 /* Before returning data to userspace, encrypt decrypted data. */
391 static int derived_key_encrypt(struct encrypted_key_payload *epayload,
392 const u8 *derived_key,
393 unsigned int derived_keylen)
395 struct scatterlist sg_in[2];
396 struct scatterlist sg_out[1];
397 struct blkcipher_desc desc;
398 unsigned int encrypted_datalen;
399 unsigned int padlen;
400 char pad[16];
401 int ret;
403 encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
404 padlen = encrypted_datalen - epayload->decrypted_datalen;
406 ret = init_blkcipher_desc(&desc, derived_key, derived_keylen,
407 epayload->iv, ivsize);
408 if (ret < 0)
409 goto out;
410 dump_decrypted_data(epayload);
412 memset(pad, 0, sizeof pad);
413 sg_init_table(sg_in, 2);
414 sg_set_buf(&sg_in[0], epayload->decrypted_data,
415 epayload->decrypted_datalen);
416 sg_set_buf(&sg_in[1], pad, padlen);
418 sg_init_table(sg_out, 1);
419 sg_set_buf(sg_out, epayload->encrypted_data, encrypted_datalen);
421 ret = crypto_blkcipher_encrypt(&desc, sg_out, sg_in, encrypted_datalen);
422 crypto_free_blkcipher(desc.tfm);
423 if (ret < 0)
424 pr_err("encrypted_key: failed to encrypt (%d)\n", ret);
425 else
426 dump_encrypted_data(epayload, encrypted_datalen);
427 out:
428 return ret;
431 static int datablob_hmac_append(struct encrypted_key_payload *epayload,
432 const u8 *master_key, size_t master_keylen)
434 u8 derived_key[HASH_SIZE];
435 u8 *digest;
436 int ret;
438 ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen);
439 if (ret < 0)
440 goto out;
442 digest = epayload->master_desc + epayload->datablob_len;
443 ret = calc_hmac(digest, derived_key, sizeof derived_key,
444 epayload->master_desc, epayload->datablob_len);
445 if (!ret)
446 dump_hmac(NULL, digest, HASH_SIZE);
447 out:
448 return ret;
451 /* verify HMAC before decrypting encrypted key */
452 static int datablob_hmac_verify(struct encrypted_key_payload *epayload,
453 const u8 *master_key, size_t master_keylen)
455 u8 derived_key[HASH_SIZE];
456 u8 digest[HASH_SIZE];
457 int ret;
459 ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen);
460 if (ret < 0)
461 goto out;
463 ret = calc_hmac(digest, derived_key, sizeof derived_key,
464 epayload->master_desc, epayload->datablob_len);
465 if (ret < 0)
466 goto out;
467 ret = memcmp(digest, epayload->master_desc + epayload->datablob_len,
468 sizeof digest);
469 if (ret) {
470 ret = -EINVAL;
471 dump_hmac("datablob",
472 epayload->master_desc + epayload->datablob_len,
473 HASH_SIZE);
474 dump_hmac("calc", digest, HASH_SIZE);
476 out:
477 return ret;
480 static int derived_key_decrypt(struct encrypted_key_payload *epayload,
481 const u8 *derived_key,
482 unsigned int derived_keylen)
484 struct scatterlist sg_in[1];
485 struct scatterlist sg_out[2];
486 struct blkcipher_desc desc;
487 unsigned int encrypted_datalen;
488 char pad[16];
489 int ret;
491 encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
492 ret = init_blkcipher_desc(&desc, derived_key, derived_keylen,
493 epayload->iv, ivsize);
494 if (ret < 0)
495 goto out;
496 dump_encrypted_data(epayload, encrypted_datalen);
498 memset(pad, 0, sizeof pad);
499 sg_init_table(sg_in, 1);
500 sg_init_table(sg_out, 2);
501 sg_set_buf(sg_in, epayload->encrypted_data, encrypted_datalen);
502 sg_set_buf(&sg_out[0], epayload->decrypted_data,
503 epayload->decrypted_datalen);
504 sg_set_buf(&sg_out[1], pad, sizeof pad);
506 ret = crypto_blkcipher_decrypt(&desc, sg_out, sg_in, encrypted_datalen);
507 crypto_free_blkcipher(desc.tfm);
508 if (ret < 0)
509 goto out;
510 dump_decrypted_data(epayload);
511 out:
512 return ret;
515 /* Allocate memory for decrypted key and datablob. */
516 static struct encrypted_key_payload *encrypted_key_alloc(struct key *key,
517 const char *master_desc,
518 const char *datalen)
520 struct encrypted_key_payload *epayload = NULL;
521 unsigned short datablob_len;
522 unsigned short decrypted_datalen;
523 unsigned int encrypted_datalen;
524 long dlen;
525 int ret;
527 ret = strict_strtol(datalen, 10, &dlen);
528 if (ret < 0 || dlen < MIN_DATA_SIZE || dlen > MAX_DATA_SIZE)
529 return ERR_PTR(-EINVAL);
531 decrypted_datalen = dlen;
532 encrypted_datalen = roundup(decrypted_datalen, blksize);
534 datablob_len = strlen(master_desc) + 1 + strlen(datalen) + 1
535 + ivsize + 1 + encrypted_datalen;
537 ret = key_payload_reserve(key, decrypted_datalen + datablob_len
538 + HASH_SIZE + 1);
539 if (ret < 0)
540 return ERR_PTR(ret);
542 epayload = kzalloc(sizeof(*epayload) + decrypted_datalen +
543 datablob_len + HASH_SIZE + 1, GFP_KERNEL);
544 if (!epayload)
545 return ERR_PTR(-ENOMEM);
547 epayload->decrypted_datalen = decrypted_datalen;
548 epayload->datablob_len = datablob_len;
549 return epayload;
552 static int encrypted_key_decrypt(struct encrypted_key_payload *epayload,
553 const char *hex_encoded_iv)
555 struct key *mkey;
556 u8 derived_key[HASH_SIZE];
557 u8 *master_key;
558 u8 *hmac;
559 const char *hex_encoded_data;
560 unsigned int encrypted_datalen;
561 size_t master_keylen;
562 size_t asciilen;
563 int ret;
565 encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
566 asciilen = (ivsize + 1 + encrypted_datalen + HASH_SIZE) * 2;
567 if (strlen(hex_encoded_iv) != asciilen)
568 return -EINVAL;
570 hex_encoded_data = hex_encoded_iv + (2 * ivsize) + 2;
571 hex2bin(epayload->iv, hex_encoded_iv, ivsize);
572 hex2bin(epayload->encrypted_data, hex_encoded_data, encrypted_datalen);
574 hmac = epayload->master_desc + epayload->datablob_len;
575 hex2bin(hmac, hex_encoded_data + (encrypted_datalen * 2), HASH_SIZE);
577 mkey = request_master_key(epayload, &master_key, &master_keylen);
578 if (IS_ERR(mkey))
579 return PTR_ERR(mkey);
581 ret = datablob_hmac_verify(epayload, master_key, master_keylen);
582 if (ret < 0) {
583 pr_err("encrypted_key: bad hmac (%d)\n", ret);
584 goto out;
587 ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen);
588 if (ret < 0)
589 goto out;
591 ret = derived_key_decrypt(epayload, derived_key, sizeof derived_key);
592 if (ret < 0)
593 pr_err("encrypted_key: failed to decrypt key (%d)\n", ret);
594 out:
595 up_read(&mkey->sem);
596 key_put(mkey);
597 return ret;
600 static void __ekey_init(struct encrypted_key_payload *epayload,
601 const char *master_desc, const char *datalen)
603 epayload->master_desc = epayload->decrypted_data
604 + epayload->decrypted_datalen;
605 epayload->datalen = epayload->master_desc + strlen(master_desc) + 1;
606 epayload->iv = epayload->datalen + strlen(datalen) + 1;
607 epayload->encrypted_data = epayload->iv + ivsize + 1;
609 memcpy(epayload->master_desc, master_desc, strlen(master_desc));
610 memcpy(epayload->datalen, datalen, strlen(datalen));
614 * encrypted_init - initialize an encrypted key
616 * For a new key, use a random number for both the iv and data
617 * itself. For an old key, decrypt the hex encoded data.
619 static int encrypted_init(struct encrypted_key_payload *epayload,
620 const char *master_desc, const char *datalen,
621 const char *hex_encoded_iv)
623 int ret = 0;
625 __ekey_init(epayload, master_desc, datalen);
626 if (!hex_encoded_iv) {
627 get_random_bytes(epayload->iv, ivsize);
629 get_random_bytes(epayload->decrypted_data,
630 epayload->decrypted_datalen);
631 } else
632 ret = encrypted_key_decrypt(epayload, hex_encoded_iv);
633 return ret;
637 * encrypted_instantiate - instantiate an encrypted key
639 * Decrypt an existing encrypted datablob or create a new encrypted key
640 * based on a kernel random number.
642 * On success, return 0. Otherwise return errno.
644 static int encrypted_instantiate(struct key *key, const void *data,
645 size_t datalen)
647 struct encrypted_key_payload *epayload = NULL;
648 char *datablob = NULL;
649 char *master_desc = NULL;
650 char *decrypted_datalen = NULL;
651 char *hex_encoded_iv = NULL;
652 int ret;
654 if (datalen <= 0 || datalen > 32767 || !data)
655 return -EINVAL;
657 datablob = kmalloc(datalen + 1, GFP_KERNEL);
658 if (!datablob)
659 return -ENOMEM;
660 datablob[datalen] = 0;
661 memcpy(datablob, data, datalen);
662 ret = datablob_parse(datablob, &master_desc, &decrypted_datalen,
663 &hex_encoded_iv);
664 if (ret < 0)
665 goto out;
667 epayload = encrypted_key_alloc(key, master_desc, decrypted_datalen);
668 if (IS_ERR(epayload)) {
669 ret = PTR_ERR(epayload);
670 goto out;
672 ret = encrypted_init(epayload, master_desc, decrypted_datalen,
673 hex_encoded_iv);
674 if (ret < 0) {
675 kfree(epayload);
676 goto out;
679 rcu_assign_pointer(key->payload.data, epayload);
680 out:
681 kfree(datablob);
682 return ret;
685 static void encrypted_rcu_free(struct rcu_head *rcu)
687 struct encrypted_key_payload *epayload;
689 epayload = container_of(rcu, struct encrypted_key_payload, rcu);
690 memset(epayload->decrypted_data, 0, epayload->decrypted_datalen);
691 kfree(epayload);
695 * encrypted_update - update the master key description
697 * Change the master key description for an existing encrypted key.
698 * The next read will return an encrypted datablob using the new
699 * master key description.
701 * On success, return 0. Otherwise return errno.
703 static int encrypted_update(struct key *key, const void *data, size_t datalen)
705 struct encrypted_key_payload *epayload = key->payload.data;
706 struct encrypted_key_payload *new_epayload;
707 char *buf;
708 char *new_master_desc = NULL;
709 int ret = 0;
711 if (datalen <= 0 || datalen > 32767 || !data)
712 return -EINVAL;
714 buf = kmalloc(datalen + 1, GFP_KERNEL);
715 if (!buf)
716 return -ENOMEM;
718 buf[datalen] = 0;
719 memcpy(buf, data, datalen);
720 ret = datablob_parse(buf, &new_master_desc, NULL, NULL);
721 if (ret < 0)
722 goto out;
724 ret = valid_master_desc(new_master_desc, epayload->master_desc);
725 if (ret < 0)
726 goto out;
728 new_epayload = encrypted_key_alloc(key, new_master_desc,
729 epayload->datalen);
730 if (IS_ERR(new_epayload)) {
731 ret = PTR_ERR(new_epayload);
732 goto out;
735 __ekey_init(new_epayload, new_master_desc, epayload->datalen);
737 memcpy(new_epayload->iv, epayload->iv, ivsize);
738 memcpy(new_epayload->decrypted_data, epayload->decrypted_data,
739 epayload->decrypted_datalen);
741 rcu_assign_pointer(key->payload.data, new_epayload);
742 call_rcu(&epayload->rcu, encrypted_rcu_free);
743 out:
744 kfree(buf);
745 return ret;
749 * encrypted_read - format and copy the encrypted data to userspace
751 * The resulting datablob format is:
752 * <master-key name> <decrypted data length> <encrypted iv> <encrypted data>
754 * On success, return to userspace the encrypted key datablob size.
756 static long encrypted_read(const struct key *key, char __user *buffer,
757 size_t buflen)
759 struct encrypted_key_payload *epayload;
760 struct key *mkey;
761 u8 *master_key;
762 size_t master_keylen;
763 char derived_key[HASH_SIZE];
764 char *ascii_buf;
765 size_t asciiblob_len;
766 int ret;
768 epayload = rcu_dereference_key(key);
770 /* returns the hex encoded iv, encrypted-data, and hmac as ascii */
771 asciiblob_len = epayload->datablob_len + ivsize + 1
772 + roundup(epayload->decrypted_datalen, blksize)
773 + (HASH_SIZE * 2);
775 if (!buffer || buflen < asciiblob_len)
776 return asciiblob_len;
778 mkey = request_master_key(epayload, &master_key, &master_keylen);
779 if (IS_ERR(mkey))
780 return PTR_ERR(mkey);
782 ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen);
783 if (ret < 0)
784 goto out;
786 ret = derived_key_encrypt(epayload, derived_key, sizeof derived_key);
787 if (ret < 0)
788 goto out;
790 ret = datablob_hmac_append(epayload, master_key, master_keylen);
791 if (ret < 0)
792 goto out;
794 ascii_buf = datablob_format(epayload, asciiblob_len);
795 if (!ascii_buf) {
796 ret = -ENOMEM;
797 goto out;
800 up_read(&mkey->sem);
801 key_put(mkey);
803 if (copy_to_user(buffer, ascii_buf, asciiblob_len) != 0)
804 ret = -EFAULT;
805 kfree(ascii_buf);
807 return asciiblob_len;
808 out:
809 up_read(&mkey->sem);
810 key_put(mkey);
811 return ret;
815 * encrypted_destroy - before freeing the key, clear the decrypted data
817 * Before freeing the key, clear the memory containing the decrypted
818 * key data.
820 static void encrypted_destroy(struct key *key)
822 struct encrypted_key_payload *epayload = key->payload.data;
824 if (!epayload)
825 return;
827 memset(epayload->decrypted_data, 0, epayload->decrypted_datalen);
828 kfree(key->payload.data);
831 struct key_type key_type_encrypted = {
832 .name = "encrypted",
833 .instantiate = encrypted_instantiate,
834 .update = encrypted_update,
835 .match = user_match,
836 .destroy = encrypted_destroy,
837 .describe = user_describe,
838 .read = encrypted_read,
840 EXPORT_SYMBOL_GPL(key_type_encrypted);
842 static void encrypted_shash_release(void)
844 if (hashalg)
845 crypto_free_shash(hashalg);
846 if (hmacalg)
847 crypto_free_shash(hmacalg);
850 static int __init encrypted_shash_alloc(void)
852 int ret;
854 hmacalg = crypto_alloc_shash(hmac_alg, 0, CRYPTO_ALG_ASYNC);
855 if (IS_ERR(hmacalg)) {
856 pr_info("encrypted_key: could not allocate crypto %s\n",
857 hmac_alg);
858 return PTR_ERR(hmacalg);
861 hashalg = crypto_alloc_shash(hash_alg, 0, CRYPTO_ALG_ASYNC);
862 if (IS_ERR(hashalg)) {
863 pr_info("encrypted_key: could not allocate crypto %s\n",
864 hash_alg);
865 ret = PTR_ERR(hashalg);
866 goto hashalg_fail;
869 return 0;
871 hashalg_fail:
872 crypto_free_shash(hmacalg);
873 return ret;
876 static int __init init_encrypted(void)
878 int ret;
880 ret = encrypted_shash_alloc();
881 if (ret < 0)
882 return ret;
883 ret = register_key_type(&key_type_encrypted);
884 if (ret < 0)
885 goto out;
886 return aes_get_sizes();
887 out:
888 encrypted_shash_release();
889 return ret;
893 static void __exit cleanup_encrypted(void)
895 encrypted_shash_release();
896 unregister_key_type(&key_type_encrypted);
899 late_initcall(init_encrypted);
900 module_exit(cleanup_encrypted);
902 MODULE_LICENSE("GPL");