2 * Copyright (C) 2010 IBM Corporation
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/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 <keys/user-type.h>
21 #include <keys/trusted-type.h>
22 #include <keys/encrypted-type.h>
23 #include <linux/key-type.h>
24 #include <linux/random.h>
25 #include <linux/rcupdate.h>
26 #include <linux/scatterlist.h>
27 #include <linux/crypto.h>
28 #include <crypto/hash.h>
29 #include <crypto/sha.h>
30 #include <crypto/aes.h>
32 #include "encrypted_defined.h"
34 #define KEY_TRUSTED_PREFIX "trusted:"
35 #define KEY_TRUSTED_PREFIX_LEN (sizeof (KEY_TRUSTED_PREFIX) - 1)
36 #define KEY_USER_PREFIX "user:"
37 #define KEY_USER_PREFIX_LEN (sizeof (KEY_USER_PREFIX) - 1)
39 #define HASH_SIZE SHA256_DIGEST_SIZE
40 #define MAX_DATA_SIZE 4096
41 #define MIN_DATA_SIZE 20
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 unsigned int ivsize;
50 struct shash_desc shash;
54 static struct crypto_shash *hashalg;
55 static struct crypto_shash *hmacalg;
58 Opt_err = -1, Opt_new, Opt_load, Opt_update
61 static const match_table_t key_tokens = {
64 {Opt_update, "update"},
68 static int aes_get_sizes(void)
70 struct crypto_blkcipher *tfm;
72 tfm = crypto_alloc_blkcipher(blkcipher_alg, 0, CRYPTO_ALG_ASYNC);
74 pr_err("encrypted_key: failed to alloc_cipher (%ld)\n",
78 ivsize = crypto_blkcipher_ivsize(tfm);
79 blksize = crypto_blkcipher_blocksize(tfm);
80 crypto_free_blkcipher(tfm);
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)
102 if (memcmp(new_desc, orig_desc, KEY_TRUSTED_PREFIX_LEN))
104 } else if (!memcmp(new_desc, KEY_USER_PREFIX, KEY_USER_PREFIX_LEN)) {
105 if (strlen(new_desc) == KEY_USER_PREFIX_LEN)
108 if (memcmp(new_desc, orig_desc, KEY_USER_PREFIX_LEN))
118 * datablob_parse - parse the keyctl data
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,
132 char **hex_encoded_data)
134 substring_t args[MAX_OPT_ARGS];
139 p = strsep(&datablob, " \t");
142 key_cmd = match_token(p, key_tokens, args);
144 *master_desc = strsep(&datablob, " \t");
148 if (valid_master_desc(*master_desc, NULL) < 0)
151 if (decrypted_datalen) {
152 *decrypted_datalen = strsep(&datablob, " \t");
153 if (!*decrypted_datalen)
159 if (!decrypted_datalen)
164 if (!decrypted_datalen)
166 *hex_encoded_iv = strsep(&datablob, " \t");
167 if (!*hex_encoded_iv)
169 *hex_encoded_data = *hex_encoded_iv + (2 * ivsize) + 2;
173 if (decrypted_datalen)
185 * datablob_format - format as an ascii string, before copying to userspace
187 static char *datablob_format(struct encrypted_key_payload *epayload,
188 size_t asciiblob_len)
190 char *ascii_buf, *bufp;
191 u8 *iv = epayload->iv;
195 ascii_buf = kmalloc(asciiblob_len + 1, GFP_KERNEL);
199 ascii_buf[asciiblob_len] = '\0';
201 /* copy datablob master_desc and datalen strings */
202 len = sprintf(ascii_buf, "%s %s ", epayload->master_desc,
205 /* convert the hex encoded iv, encrypted-data and HMAC to ascii */
206 bufp = &ascii_buf[len];
207 for (i = 0; i < (asciiblob_len - len) / 2; i++)
208 bufp = pack_hex_byte(bufp, iv[i]);
214 * request_trusted_key - request the trusted key
216 * Trusted keys are sealed to PCRs and other metadata. Although userspace
217 * manages both trusted/encrypted key-types, like the encrypted key type
218 * data, trusted key type data is not visible decrypted from userspace.
220 static struct key *request_trusted_key(const char *trusted_desc,
222 unsigned int *master_keylen)
224 struct trusted_key_payload *tpayload;
227 tkey = request_key(&key_type_trusted, trusted_desc, NULL);
231 down_read(&tkey->sem);
232 tpayload = rcu_dereference(tkey->payload.data);
233 *master_key = tpayload->key;
234 *master_keylen = tpayload->key_len;
240 * request_user_key - request the user key
242 * Use a user provided key to encrypt/decrypt an encrypted-key.
244 static struct key *request_user_key(const char *master_desc, u8 **master_key,
245 unsigned int *master_keylen)
247 struct user_key_payload *upayload;
250 ukey = request_key(&key_type_user, master_desc, NULL);
254 down_read(&ukey->sem);
255 upayload = rcu_dereference(ukey->payload.data);
256 *master_key = upayload->data;
257 *master_keylen = upayload->datalen;
262 static struct sdesc *init_sdesc(struct crypto_shash *alg)
267 size = sizeof(struct shash_desc) + crypto_shash_descsize(alg);
268 sdesc = kmalloc(size, GFP_KERNEL);
270 return ERR_PTR(-ENOMEM);
271 sdesc->shash.tfm = alg;
272 sdesc->shash.flags = 0x0;
276 static int calc_hmac(u8 *digest, const u8 *key, const unsigned int keylen,
277 const u8 *buf, const unsigned int buflen)
282 sdesc = init_sdesc(hmacalg);
284 pr_info("encrypted_key: can't alloc %s\n", hmac_alg);
285 return PTR_ERR(sdesc);
288 ret = crypto_shash_setkey(hmacalg, key, keylen);
290 ret = crypto_shash_digest(&sdesc->shash, buf, buflen, digest);
295 static int calc_hash(u8 *digest, const u8 *buf, const unsigned int buflen)
300 sdesc = init_sdesc(hashalg);
302 pr_info("encrypted_key: can't alloc %s\n", hash_alg);
303 return PTR_ERR(sdesc);
306 ret = crypto_shash_digest(&sdesc->shash, buf, buflen, digest);
311 enum derived_key_type { ENC_KEY, AUTH_KEY };
313 /* Derive authentication/encryption key from trusted key */
314 static int get_derived_key(u8 *derived_key, enum derived_key_type key_type,
315 const u8 *master_key,
316 const unsigned int master_keylen)
319 unsigned int derived_buf_len;
322 derived_buf_len = strlen("AUTH_KEY") + 1 + master_keylen;
323 if (derived_buf_len < HASH_SIZE)
324 derived_buf_len = HASH_SIZE;
326 derived_buf = kzalloc(derived_buf_len, GFP_KERNEL);
328 pr_err("encrypted_key: out of memory\n");
332 strcpy(derived_buf, "AUTH_KEY");
334 strcpy(derived_buf, "ENC_KEY");
336 memcpy(derived_buf + strlen(derived_buf) + 1, master_key,
338 ret = calc_hash(derived_key, derived_buf, derived_buf_len);
343 static int init_blkcipher_desc(struct blkcipher_desc *desc, const u8 *key,
344 const unsigned int key_len, const u8 *iv,
345 const unsigned int ivsize)
349 desc->tfm = crypto_alloc_blkcipher(blkcipher_alg, 0, CRYPTO_ALG_ASYNC);
350 if (IS_ERR(desc->tfm)) {
351 pr_err("encrypted_key: failed to load %s transform (%ld)\n",
352 blkcipher_alg, PTR_ERR(desc->tfm));
353 return PTR_ERR(desc->tfm);
357 ret = crypto_blkcipher_setkey(desc->tfm, key, key_len);
359 pr_err("encrypted_key: failed to setkey (%d)\n", ret);
360 crypto_free_blkcipher(desc->tfm);
363 crypto_blkcipher_set_iv(desc->tfm, iv, ivsize);
367 static struct key *request_master_key(struct encrypted_key_payload *epayload,
369 unsigned int *master_keylen)
371 struct key *mkey = NULL;
373 if (!strncmp(epayload->master_desc, KEY_TRUSTED_PREFIX,
374 KEY_TRUSTED_PREFIX_LEN)) {
375 mkey = request_trusted_key(epayload->master_desc +
376 KEY_TRUSTED_PREFIX_LEN,
377 master_key, master_keylen);
378 } else if (!strncmp(epayload->master_desc, KEY_USER_PREFIX,
379 KEY_USER_PREFIX_LEN)) {
380 mkey = request_user_key(epayload->master_desc +
382 master_key, master_keylen);
387 pr_info("encrypted_key: key %s not found",
388 epayload->master_desc);
390 dump_master_key(*master_key, *master_keylen);
395 /* Before returning data to userspace, encrypt decrypted data. */
396 static int derived_key_encrypt(struct encrypted_key_payload *epayload,
397 const u8 *derived_key,
398 const unsigned int derived_keylen)
400 struct scatterlist sg_in[2];
401 struct scatterlist sg_out[1];
402 struct blkcipher_desc desc;
403 unsigned int encrypted_datalen;
408 encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
409 padlen = encrypted_datalen - epayload->decrypted_datalen;
411 ret = init_blkcipher_desc(&desc, derived_key, derived_keylen,
412 epayload->iv, ivsize);
415 dump_decrypted_data(epayload);
417 memset(pad, 0, sizeof pad);
418 sg_init_table(sg_in, 2);
419 sg_set_buf(&sg_in[0], epayload->decrypted_data,
420 epayload->decrypted_datalen);
421 sg_set_buf(&sg_in[1], pad, padlen);
423 sg_init_table(sg_out, 1);
424 sg_set_buf(sg_out, epayload->encrypted_data, encrypted_datalen);
426 ret = crypto_blkcipher_encrypt(&desc, sg_out, sg_in, encrypted_datalen);
427 crypto_free_blkcipher(desc.tfm);
429 pr_err("encrypted_key: failed to encrypt (%d)\n", ret);
431 dump_encrypted_data(epayload, encrypted_datalen);
436 static int datablob_hmac_append(struct encrypted_key_payload *epayload,
437 const u8 *master_key,
438 const unsigned int master_keylen)
440 u8 derived_key[HASH_SIZE];
444 ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen);
448 digest = epayload->master_desc + epayload->datablob_len;
449 ret = calc_hmac(digest, derived_key, sizeof derived_key,
450 epayload->master_desc, epayload->datablob_len);
452 dump_hmac(NULL, digest, HASH_SIZE);
457 /* verify HMAC before decrypting encrypted key */
458 static int datablob_hmac_verify(struct encrypted_key_payload *epayload,
459 const u8 *master_key,
460 const unsigned int master_keylen)
462 u8 derived_key[HASH_SIZE];
463 u8 digest[HASH_SIZE];
466 ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen);
470 ret = calc_hmac(digest, derived_key, sizeof derived_key,
471 epayload->master_desc, epayload->datablob_len);
474 ret = memcmp(digest, epayload->master_desc + epayload->datablob_len,
478 dump_hmac("datablob",
479 epayload->master_desc + epayload->datablob_len,
481 dump_hmac("calc", digest, HASH_SIZE);
487 static int derived_key_decrypt(struct encrypted_key_payload *epayload,
488 const u8 *derived_key,
489 const unsigned int derived_keylen)
491 struct scatterlist sg_in[1];
492 struct scatterlist sg_out[2];
493 struct blkcipher_desc desc;
494 unsigned int encrypted_datalen;
498 encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
499 ret = init_blkcipher_desc(&desc, derived_key, derived_keylen,
500 epayload->iv, ivsize);
503 dump_encrypted_data(epayload, encrypted_datalen);
505 memset(pad, 0, sizeof pad);
506 sg_init_table(sg_in, 1);
507 sg_init_table(sg_out, 2);
508 sg_set_buf(sg_in, epayload->encrypted_data, encrypted_datalen);
509 sg_set_buf(&sg_out[0], epayload->decrypted_data,
510 (unsigned int)epayload->decrypted_datalen);
511 sg_set_buf(&sg_out[1], pad, sizeof pad);
513 ret = crypto_blkcipher_decrypt(&desc, sg_out, sg_in, encrypted_datalen);
514 crypto_free_blkcipher(desc.tfm);
517 dump_decrypted_data(epayload);
522 /* Allocate memory for decrypted key and datablob. */
523 static struct encrypted_key_payload *encrypted_key_alloc(struct key *key,
524 const char *master_desc,
527 struct encrypted_key_payload *epayload = NULL;
528 unsigned short datablob_len;
529 unsigned short decrypted_datalen;
530 unsigned int encrypted_datalen;
534 ret = strict_strtol(datalen, 10, &dlen);
535 if (ret < 0 || dlen < MIN_DATA_SIZE || dlen > MAX_DATA_SIZE)
536 return ERR_PTR(-EINVAL);
538 decrypted_datalen = dlen;
539 encrypted_datalen = roundup(decrypted_datalen, blksize);
541 datablob_len = strlen(master_desc) + 1 + strlen(datalen) + 1
542 + ivsize + 1 + encrypted_datalen;
544 ret = key_payload_reserve(key, decrypted_datalen + datablob_len
549 epayload = kzalloc(sizeof(*epayload) + decrypted_datalen +
550 datablob_len + HASH_SIZE + 1, GFP_KERNEL);
552 return ERR_PTR(-ENOMEM);
554 epayload->decrypted_datalen = decrypted_datalen;
555 epayload->datablob_len = datablob_len;
559 static int encrypted_key_decrypt(struct encrypted_key_payload *epayload,
560 const char *hex_encoded_iv,
561 const char *hex_encoded_data)
564 u8 derived_key[HASH_SIZE];
567 unsigned int master_keylen;
568 unsigned int encrypted_datalen;
571 encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
572 hex2bin(epayload->iv, hex_encoded_iv, ivsize);
573 hex2bin(epayload->encrypted_data, hex_encoded_data, encrypted_datalen);
575 hmac = epayload->master_desc + epayload->datablob_len;
576 hex2bin(hmac, hex_encoded_data + (encrypted_datalen * 2), HASH_SIZE);
578 mkey = request_master_key(epayload, &master_key, &master_keylen);
580 return PTR_ERR(mkey);
582 ret = datablob_hmac_verify(epayload, master_key, master_keylen);
584 pr_err("encrypted_key: bad hmac (%d)\n", ret);
588 ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen);
592 ret = derived_key_decrypt(epayload, derived_key, sizeof derived_key);
594 pr_err("encrypted_key: failed to decrypt key (%d)\n", ret);
601 static void __ekey_init(struct encrypted_key_payload *epayload,
602 const char *master_desc, const char *datalen)
604 epayload->master_desc = epayload->decrypted_data
605 + epayload->decrypted_datalen;
606 epayload->datalen = epayload->master_desc + strlen(master_desc) + 1;
607 epayload->iv = epayload->datalen + strlen(datalen) + 1;
608 epayload->encrypted_data = epayload->iv + ivsize + 1;
610 memcpy(epayload->master_desc, master_desc, strlen(master_desc));
611 memcpy(epayload->datalen, datalen, strlen(datalen));
615 * encrypted_init - initialize an encrypted key
617 * For a new key, use a random number for both the iv and data
618 * itself. For an old key, decrypt the hex encoded data.
620 static int encrypted_init(struct encrypted_key_payload *epayload,
621 const char *master_desc, const char *datalen,
622 const char *hex_encoded_iv,
623 const char *hex_encoded_data)
627 __ekey_init(epayload, master_desc, datalen);
628 if (!hex_encoded_data) {
629 get_random_bytes(epayload->iv, ivsize);
631 get_random_bytes(epayload->decrypted_data,
632 epayload->decrypted_datalen);
634 ret = encrypted_key_decrypt(epayload, hex_encoded_iv,
640 * encrypted_instantiate - instantiate an encrypted key
642 * Decrypt an existing encrypted datablob or create a new encrypted key
643 * based on a kernel random number.
645 * On success, return 0. Otherwise return errno.
647 static int encrypted_instantiate(struct key *key, const void *data,
650 struct encrypted_key_payload *epayload = NULL;
651 char *datablob = NULL;
652 char *master_desc = NULL;
653 char *decrypted_datalen = NULL;
654 char *hex_encoded_iv = NULL;
655 char *hex_encoded_data = NULL;
658 if (datalen <= 0 || datalen > 32767 || !data)
661 datablob = kmalloc(datalen + 1, GFP_KERNEL);
664 datablob[datalen] = 0;
665 memcpy(datablob, data, datalen);
666 ret = datablob_parse(datablob, &master_desc, &decrypted_datalen,
667 &hex_encoded_iv, &hex_encoded_data);
671 epayload = encrypted_key_alloc(key, master_desc, decrypted_datalen);
672 if (IS_ERR(epayload)) {
673 ret = PTR_ERR(epayload);
676 ret = encrypted_init(epayload, master_desc, decrypted_datalen,
677 hex_encoded_iv, hex_encoded_data);
683 rcu_assign_pointer(key->payload.data, epayload);
689 static void encrypted_rcu_free(struct rcu_head *rcu)
691 struct encrypted_key_payload *epayload;
693 epayload = container_of(rcu, struct encrypted_key_payload, rcu);
694 memset(epayload->decrypted_data, 0, epayload->decrypted_datalen);
699 * encrypted_update - update the master key description
701 * Change the master key description for an existing encrypted key.
702 * The next read will return an encrypted datablob using the new
703 * master key description.
705 * On success, return 0. Otherwise return errno.
707 static int encrypted_update(struct key *key, const void *data, size_t datalen)
709 struct encrypted_key_payload *epayload = key->payload.data;
710 struct encrypted_key_payload *new_epayload;
712 char *new_master_desc = NULL;
715 if (datalen <= 0 || datalen > 32767 || !data)
718 buf = kmalloc(datalen + 1, GFP_KERNEL);
723 memcpy(buf, data, datalen);
724 ret = datablob_parse(buf, &new_master_desc, NULL, NULL, NULL);
728 ret = valid_master_desc(new_master_desc, epayload->master_desc);
732 new_epayload = encrypted_key_alloc(key, new_master_desc,
734 if (IS_ERR(new_epayload)) {
735 ret = PTR_ERR(new_epayload);
739 __ekey_init(new_epayload, new_master_desc, epayload->datalen);
741 memcpy(new_epayload->iv, epayload->iv, ivsize);
742 memcpy(new_epayload->decrypted_data, epayload->decrypted_data,
743 epayload->decrypted_datalen);
745 rcu_assign_pointer(key->payload.data, new_epayload);
746 call_rcu(&epayload->rcu, encrypted_rcu_free);
753 * encrypted_read - format and copy the encrypted data to userspace
755 * The resulting datablob format is:
756 * <master-key name> <decrypted data length> <encrypted iv> <encrypted data>
758 * On success, return to userspace the encrypted key datablob size.
760 static long encrypted_read(const struct key *key, char __user *buffer,
763 struct encrypted_key_payload *epayload;
766 unsigned int master_keylen;
767 char derived_key[HASH_SIZE];
769 size_t asciiblob_len;
772 epayload = rcu_dereference_protected(key->payload.data,
773 rwsem_is_locked(&((struct key *)key)->sem));
775 /* returns the hex encoded iv, encrypted-data, and hmac as ascii */
776 asciiblob_len = epayload->datablob_len + ivsize + 1
777 + roundup(epayload->decrypted_datalen, blksize)
780 if (!buffer || buflen < asciiblob_len)
781 return asciiblob_len;
783 mkey = request_master_key(epayload, &master_key, &master_keylen);
785 return PTR_ERR(mkey);
787 ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen);
791 ret = derived_key_encrypt(epayload, derived_key, sizeof derived_key);
795 ret = datablob_hmac_append(epayload, master_key, master_keylen);
799 ascii_buf = datablob_format(epayload, asciiblob_len);
808 if (copy_to_user(buffer, ascii_buf, asciiblob_len) != 0)
812 return asciiblob_len;
820 * encrypted_destroy - before freeing the key, clear the decrypted data
822 * Before freeing the key, clear the memory containing the decrypted
825 static void encrypted_destroy(struct key *key)
827 struct encrypted_key_payload *epayload = key->payload.data;
832 memset(epayload->decrypted_data, 0, epayload->decrypted_datalen);
833 kfree(key->payload.data);
836 struct key_type key_type_encrypted = {
838 .instantiate = encrypted_instantiate,
839 .update = encrypted_update,
841 .destroy = encrypted_destroy,
842 .describe = user_describe,
843 .read = encrypted_read,
845 EXPORT_SYMBOL_GPL(key_type_encrypted);
847 static void encrypted_shash_release(void)
850 crypto_free_shash(hashalg);
852 crypto_free_shash(hmacalg);
855 static int __init encrypted_shash_alloc(void)
859 hmacalg = crypto_alloc_shash(hmac_alg, 0, CRYPTO_ALG_ASYNC);
860 if (IS_ERR(hmacalg)) {
861 pr_info("encrypted_key: could not allocate crypto %s\n",
863 return PTR_ERR(hmacalg);
866 hashalg = crypto_alloc_shash(hash_alg, 0, CRYPTO_ALG_ASYNC);
867 if (IS_ERR(hashalg)) {
868 pr_info("encrypted_key: could not allocate crypto %s\n",
870 ret = PTR_ERR(hashalg);
877 crypto_free_shash(hmacalg);
881 static int __init init_encrypted(void)
885 ret = encrypted_shash_alloc();
888 ret = register_key_type(&key_type_encrypted);
891 return aes_get_sizes();
893 encrypted_shash_release();
898 static void __exit cleanup_encrypted(void)
900 encrypted_shash_release();
901 unregister_key_type(&key_type_encrypted);
904 late_initcall(init_encrypted);
905 module_exit(cleanup_encrypted);
907 MODULE_LICENSE("GPL");