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 <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_defined.h"
35 #define KEY_TRUSTED_PREFIX "trusted:"
36 #define KEY_TRUSTED_PREFIX_LEN (sizeof (KEY_TRUSTED_PREFIX) - 1)
37 #define KEY_USER_PREFIX "user:"
38 #define KEY_USER_PREFIX_LEN (sizeof (KEY_USER_PREFIX) - 1)
40 #define HASH_SIZE SHA256_DIGEST_SIZE
41 #define MAX_DATA_SIZE 4096
42 #define MIN_DATA_SIZE 20
44 static const char hash_alg[] = "sha256";
45 static const char hmac_alg[] = "hmac(sha256)";
46 static const char blkcipher_alg[] = "cbc(aes)";
47 static unsigned int ivsize;
51 struct shash_desc shash;
55 static struct crypto_shash *hashalg;
56 static struct crypto_shash *hmacalg;
59 Opt_err = -1, Opt_new, Opt_load, Opt_update
62 static const match_table_t key_tokens = {
65 {Opt_update, "update"},
69 static int aes_get_sizes(void)
71 struct crypto_blkcipher *tfm;
73 tfm = crypto_alloc_blkcipher(blkcipher_alg, 0, CRYPTO_ALG_ASYNC);
75 pr_err("encrypted_key: failed to alloc_cipher (%ld)\n",
79 ivsize = crypto_blkcipher_ivsize(tfm);
80 blksize = crypto_blkcipher_blocksize(tfm);
81 crypto_free_blkcipher(tfm);
86 * valid_master_desc - verify the 'key-type:desc' of a new/updated master-key
88 * key-type:= "trusted:" | "encrypted:"
89 * desc:= master-key description
91 * Verify that 'key-type' is valid and that 'desc' exists. On key update,
92 * only the master key description is permitted to change, not the key-type.
93 * The key-type remains constant.
95 * On success returns 0, otherwise -EINVAL.
97 static int valid_master_desc(const char *new_desc, const char *orig_desc)
99 if (!memcmp(new_desc, KEY_TRUSTED_PREFIX, KEY_TRUSTED_PREFIX_LEN)) {
100 if (strlen(new_desc) == KEY_TRUSTED_PREFIX_LEN)
103 if (memcmp(new_desc, orig_desc, KEY_TRUSTED_PREFIX_LEN))
105 } else if (!memcmp(new_desc, KEY_USER_PREFIX, KEY_USER_PREFIX_LEN)) {
106 if (strlen(new_desc) == KEY_USER_PREFIX_LEN)
109 if (memcmp(new_desc, orig_desc, KEY_USER_PREFIX_LEN))
119 * datablob_parse - parse the keyctl data
122 * new <master-key name> <decrypted data length>
123 * load <master-key name> <decrypted data length> <encrypted iv + data>
124 * update <new-master-key name>
126 * Tokenizes a copy of the keyctl data, returning a pointer to each token,
127 * which is null terminated.
129 * On success returns 0, otherwise -EINVAL.
131 static int datablob_parse(char *datablob, char **master_desc,
132 char **decrypted_datalen, char **hex_encoded_iv)
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)
172 if (decrypted_datalen)
184 * datablob_format - format as an ascii string, before copying to userspace
186 static char *datablob_format(struct encrypted_key_payload *epayload,
187 size_t asciiblob_len)
189 char *ascii_buf, *bufp;
190 u8 *iv = epayload->iv;
194 ascii_buf = kmalloc(asciiblob_len + 1, GFP_KERNEL);
198 ascii_buf[asciiblob_len] = '\0';
200 /* copy datablob master_desc and datalen strings */
201 len = sprintf(ascii_buf, "%s %s ", epayload->master_desc,
204 /* convert the hex encoded iv, encrypted-data and HMAC to ascii */
205 bufp = &ascii_buf[len];
206 for (i = 0; i < (asciiblob_len - len) / 2; i++)
207 bufp = pack_hex_byte(bufp, iv[i]);
213 * request_trusted_key - request the trusted key
215 * Trusted keys are sealed to PCRs and other metadata. Although userspace
216 * manages both trusted/encrypted key-types, like the encrypted key type
217 * data, trusted key type data is not visible decrypted from userspace.
219 static struct key *request_trusted_key(const char *trusted_desc,
221 unsigned int *master_keylen)
223 struct trusted_key_payload *tpayload;
226 tkey = request_key(&key_type_trusted, trusted_desc, NULL);
230 down_read(&tkey->sem);
231 tpayload = rcu_dereference(tkey->payload.data);
232 *master_key = tpayload->key;
233 *master_keylen = tpayload->key_len;
239 * request_user_key - request the user key
241 * Use a user provided key to encrypt/decrypt an encrypted-key.
243 static struct key *request_user_key(const char *master_desc, u8 **master_key,
244 unsigned int *master_keylen)
246 struct user_key_payload *upayload;
249 ukey = request_key(&key_type_user, master_desc, NULL);
253 down_read(&ukey->sem);
254 upayload = rcu_dereference(ukey->payload.data);
255 *master_key = upayload->data;
256 *master_keylen = upayload->datalen;
261 static struct sdesc *init_sdesc(struct crypto_shash *alg)
266 size = sizeof(struct shash_desc) + crypto_shash_descsize(alg);
267 sdesc = kmalloc(size, GFP_KERNEL);
269 return ERR_PTR(-ENOMEM);
270 sdesc->shash.tfm = alg;
271 sdesc->shash.flags = 0x0;
275 static int calc_hmac(u8 *digest, const u8 *key, const unsigned int keylen,
276 const u8 *buf, const unsigned int buflen)
281 sdesc = init_sdesc(hmacalg);
283 pr_info("encrypted_key: can't alloc %s\n", hmac_alg);
284 return PTR_ERR(sdesc);
287 ret = crypto_shash_setkey(hmacalg, key, keylen);
289 ret = crypto_shash_digest(&sdesc->shash, buf, buflen, digest);
294 static int calc_hash(u8 *digest, const u8 *buf, const unsigned int buflen)
299 sdesc = init_sdesc(hashalg);
301 pr_info("encrypted_key: can't alloc %s\n", hash_alg);
302 return PTR_ERR(sdesc);
305 ret = crypto_shash_digest(&sdesc->shash, buf, buflen, digest);
310 enum derived_key_type { ENC_KEY, AUTH_KEY };
312 /* Derive authentication/encryption key from trusted key */
313 static int get_derived_key(u8 *derived_key, enum derived_key_type key_type,
314 const u8 *master_key,
315 const unsigned int master_keylen)
318 unsigned int derived_buf_len;
321 derived_buf_len = strlen("AUTH_KEY") + 1 + master_keylen;
322 if (derived_buf_len < HASH_SIZE)
323 derived_buf_len = HASH_SIZE;
325 derived_buf = kzalloc(derived_buf_len, GFP_KERNEL);
327 pr_err("encrypted_key: out of memory\n");
331 strcpy(derived_buf, "AUTH_KEY");
333 strcpy(derived_buf, "ENC_KEY");
335 memcpy(derived_buf + strlen(derived_buf) + 1, master_key,
337 ret = calc_hash(derived_key, derived_buf, derived_buf_len);
342 static int init_blkcipher_desc(struct blkcipher_desc *desc, const u8 *key,
343 const unsigned int key_len, const u8 *iv,
344 const unsigned int ivsize)
348 desc->tfm = crypto_alloc_blkcipher(blkcipher_alg, 0, CRYPTO_ALG_ASYNC);
349 if (IS_ERR(desc->tfm)) {
350 pr_err("encrypted_key: failed to load %s transform (%ld)\n",
351 blkcipher_alg, PTR_ERR(desc->tfm));
352 return PTR_ERR(desc->tfm);
356 ret = crypto_blkcipher_setkey(desc->tfm, key, key_len);
358 pr_err("encrypted_key: failed to setkey (%d)\n", ret);
359 crypto_free_blkcipher(desc->tfm);
362 crypto_blkcipher_set_iv(desc->tfm, iv, ivsize);
366 static struct key *request_master_key(struct encrypted_key_payload *epayload,
368 unsigned int *master_keylen)
370 struct key *mkey = NULL;
372 if (!strncmp(epayload->master_desc, KEY_TRUSTED_PREFIX,
373 KEY_TRUSTED_PREFIX_LEN)) {
374 mkey = request_trusted_key(epayload->master_desc +
375 KEY_TRUSTED_PREFIX_LEN,
376 master_key, master_keylen);
377 } else if (!strncmp(epayload->master_desc, KEY_USER_PREFIX,
378 KEY_USER_PREFIX_LEN)) {
379 mkey = request_user_key(epayload->master_desc +
381 master_key, master_keylen);
386 pr_info("encrypted_key: key %s not found",
387 epayload->master_desc);
389 dump_master_key(*master_key, *master_keylen);
394 /* Before returning data to userspace, encrypt decrypted data. */
395 static int derived_key_encrypt(struct encrypted_key_payload *epayload,
396 const u8 *derived_key,
397 const unsigned int derived_keylen)
399 struct scatterlist sg_in[2];
400 struct scatterlist sg_out[1];
401 struct blkcipher_desc desc;
402 unsigned int encrypted_datalen;
407 encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
408 padlen = encrypted_datalen - epayload->decrypted_datalen;
410 ret = init_blkcipher_desc(&desc, derived_key, derived_keylen,
411 epayload->iv, ivsize);
414 dump_decrypted_data(epayload);
416 memset(pad, 0, sizeof pad);
417 sg_init_table(sg_in, 2);
418 sg_set_buf(&sg_in[0], epayload->decrypted_data,
419 epayload->decrypted_datalen);
420 sg_set_buf(&sg_in[1], pad, padlen);
422 sg_init_table(sg_out, 1);
423 sg_set_buf(sg_out, epayload->encrypted_data, encrypted_datalen);
425 ret = crypto_blkcipher_encrypt(&desc, sg_out, sg_in, encrypted_datalen);
426 crypto_free_blkcipher(desc.tfm);
428 pr_err("encrypted_key: failed to encrypt (%d)\n", ret);
430 dump_encrypted_data(epayload, encrypted_datalen);
435 static int datablob_hmac_append(struct encrypted_key_payload *epayload,
436 const u8 *master_key,
437 const unsigned int master_keylen)
439 u8 derived_key[HASH_SIZE];
443 ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen);
447 digest = epayload->master_desc + epayload->datablob_len;
448 ret = calc_hmac(digest, derived_key, sizeof derived_key,
449 epayload->master_desc, epayload->datablob_len);
451 dump_hmac(NULL, digest, HASH_SIZE);
456 /* verify HMAC before decrypting encrypted key */
457 static int datablob_hmac_verify(struct encrypted_key_payload *epayload,
458 const u8 *master_key,
459 const unsigned int master_keylen)
461 u8 derived_key[HASH_SIZE];
462 u8 digest[HASH_SIZE];
465 ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen);
469 ret = calc_hmac(digest, derived_key, sizeof derived_key,
470 epayload->master_desc, epayload->datablob_len);
473 ret = memcmp(digest, epayload->master_desc + epayload->datablob_len,
477 dump_hmac("datablob",
478 epayload->master_desc + epayload->datablob_len,
480 dump_hmac("calc", digest, HASH_SIZE);
486 static int derived_key_decrypt(struct encrypted_key_payload *epayload,
487 const u8 *derived_key,
488 const unsigned int derived_keylen)
490 struct scatterlist sg_in[1];
491 struct scatterlist sg_out[2];
492 struct blkcipher_desc desc;
493 unsigned int encrypted_datalen;
497 encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
498 ret = init_blkcipher_desc(&desc, derived_key, derived_keylen,
499 epayload->iv, ivsize);
502 dump_encrypted_data(epayload, encrypted_datalen);
504 memset(pad, 0, sizeof pad);
505 sg_init_table(sg_in, 1);
506 sg_init_table(sg_out, 2);
507 sg_set_buf(sg_in, epayload->encrypted_data, encrypted_datalen);
508 sg_set_buf(&sg_out[0], epayload->decrypted_data,
509 (unsigned int)epayload->decrypted_datalen);
510 sg_set_buf(&sg_out[1], pad, sizeof pad);
512 ret = crypto_blkcipher_decrypt(&desc, sg_out, sg_in, encrypted_datalen);
513 crypto_free_blkcipher(desc.tfm);
516 dump_decrypted_data(epayload);
521 /* Allocate memory for decrypted key and datablob. */
522 static struct encrypted_key_payload *encrypted_key_alloc(struct key *key,
523 const char *master_desc,
526 struct encrypted_key_payload *epayload = NULL;
527 unsigned short datablob_len;
528 unsigned short decrypted_datalen;
529 unsigned int encrypted_datalen;
533 ret = strict_strtol(datalen, 10, &dlen);
534 if (ret < 0 || dlen < MIN_DATA_SIZE || dlen > MAX_DATA_SIZE)
535 return ERR_PTR(-EINVAL);
537 decrypted_datalen = dlen;
538 encrypted_datalen = roundup(decrypted_datalen, blksize);
540 datablob_len = strlen(master_desc) + 1 + strlen(datalen) + 1
541 + ivsize + 1 + encrypted_datalen;
543 ret = key_payload_reserve(key, decrypted_datalen + datablob_len
548 epayload = kzalloc(sizeof(*epayload) + decrypted_datalen +
549 datablob_len + HASH_SIZE + 1, GFP_KERNEL);
551 return ERR_PTR(-ENOMEM);
553 epayload->decrypted_datalen = decrypted_datalen;
554 epayload->datablob_len = datablob_len;
558 static int encrypted_key_decrypt(struct encrypted_key_payload *epayload,
559 const char *hex_encoded_iv)
562 u8 derived_key[HASH_SIZE];
565 const char *hex_encoded_data;
566 unsigned int master_keylen;
567 unsigned int encrypted_datalen;
571 encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
572 asciilen = (ivsize + 1 + encrypted_datalen + HASH_SIZE) * 2;
573 if (strlen(hex_encoded_iv) != asciilen)
576 hex_encoded_data = hex_encoded_iv + (2 * ivsize) + 2;
577 hex2bin(epayload->iv, hex_encoded_iv, ivsize);
578 hex2bin(epayload->encrypted_data, hex_encoded_data, encrypted_datalen);
580 hmac = epayload->master_desc + epayload->datablob_len;
581 hex2bin(hmac, hex_encoded_data + (encrypted_datalen * 2), HASH_SIZE);
583 mkey = request_master_key(epayload, &master_key, &master_keylen);
585 return PTR_ERR(mkey);
587 ret = datablob_hmac_verify(epayload, master_key, master_keylen);
589 pr_err("encrypted_key: bad hmac (%d)\n", ret);
593 ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen);
597 ret = derived_key_decrypt(epayload, derived_key, sizeof derived_key);
599 pr_err("encrypted_key: failed to decrypt key (%d)\n", ret);
606 static void __ekey_init(struct encrypted_key_payload *epayload,
607 const char *master_desc, const char *datalen)
609 epayload->master_desc = epayload->decrypted_data
610 + epayload->decrypted_datalen;
611 epayload->datalen = epayload->master_desc + strlen(master_desc) + 1;
612 epayload->iv = epayload->datalen + strlen(datalen) + 1;
613 epayload->encrypted_data = epayload->iv + ivsize + 1;
615 memcpy(epayload->master_desc, master_desc, strlen(master_desc));
616 memcpy(epayload->datalen, datalen, strlen(datalen));
620 * encrypted_init - initialize an encrypted key
622 * For a new key, use a random number for both the iv and data
623 * itself. For an old key, decrypt the hex encoded data.
625 static int encrypted_init(struct encrypted_key_payload *epayload,
626 const char *master_desc, const char *datalen,
627 const char *hex_encoded_iv)
631 __ekey_init(epayload, master_desc, datalen);
632 if (!hex_encoded_iv) {
633 get_random_bytes(epayload->iv, ivsize);
635 get_random_bytes(epayload->decrypted_data,
636 epayload->decrypted_datalen);
638 ret = encrypted_key_decrypt(epayload, hex_encoded_iv);
643 * encrypted_instantiate - instantiate an encrypted key
645 * Decrypt an existing encrypted datablob or create a new encrypted key
646 * based on a kernel random number.
648 * On success, return 0. Otherwise return errno.
650 static int encrypted_instantiate(struct key *key, const void *data,
653 struct encrypted_key_payload *epayload = NULL;
654 char *datablob = NULL;
655 char *master_desc = NULL;
656 char *decrypted_datalen = NULL;
657 char *hex_encoded_iv = NULL;
660 if (datalen <= 0 || datalen > 32767 || !data)
663 datablob = kmalloc(datalen + 1, GFP_KERNEL);
666 datablob[datalen] = 0;
667 memcpy(datablob, data, datalen);
668 ret = datablob_parse(datablob, &master_desc, &decrypted_datalen,
673 epayload = encrypted_key_alloc(key, master_desc, decrypted_datalen);
674 if (IS_ERR(epayload)) {
675 ret = PTR_ERR(epayload);
678 ret = encrypted_init(epayload, master_desc, decrypted_datalen,
685 rcu_assign_pointer(key->payload.data, epayload);
691 static void encrypted_rcu_free(struct rcu_head *rcu)
693 struct encrypted_key_payload *epayload;
695 epayload = container_of(rcu, struct encrypted_key_payload, rcu);
696 memset(epayload->decrypted_data, 0, epayload->decrypted_datalen);
701 * encrypted_update - update the master key description
703 * Change the master key description for an existing encrypted key.
704 * The next read will return an encrypted datablob using the new
705 * master key description.
707 * On success, return 0. Otherwise return errno.
709 static int encrypted_update(struct key *key, const void *data, size_t datalen)
711 struct encrypted_key_payload *epayload = key->payload.data;
712 struct encrypted_key_payload *new_epayload;
714 char *new_master_desc = NULL;
717 if (datalen <= 0 || datalen > 32767 || !data)
720 buf = kmalloc(datalen + 1, GFP_KERNEL);
725 memcpy(buf, data, datalen);
726 ret = datablob_parse(buf, &new_master_desc, NULL, NULL);
730 ret = valid_master_desc(new_master_desc, epayload->master_desc);
734 new_epayload = encrypted_key_alloc(key, new_master_desc,
736 if (IS_ERR(new_epayload)) {
737 ret = PTR_ERR(new_epayload);
741 __ekey_init(new_epayload, new_master_desc, epayload->datalen);
743 memcpy(new_epayload->iv, epayload->iv, ivsize);
744 memcpy(new_epayload->decrypted_data, epayload->decrypted_data,
745 epayload->decrypted_datalen);
747 rcu_assign_pointer(key->payload.data, new_epayload);
748 call_rcu(&epayload->rcu, encrypted_rcu_free);
755 * encrypted_read - format and copy the encrypted data to userspace
757 * The resulting datablob format is:
758 * <master-key name> <decrypted data length> <encrypted iv> <encrypted data>
760 * On success, return to userspace the encrypted key datablob size.
762 static long encrypted_read(const struct key *key, char __user *buffer,
765 struct encrypted_key_payload *epayload;
768 unsigned int master_keylen;
769 char derived_key[HASH_SIZE];
771 size_t asciiblob_len;
774 epayload = rcu_dereference_protected(key->payload.data,
775 rwsem_is_locked(&((struct key *)key)->sem));
777 /* returns the hex encoded iv, encrypted-data, and hmac as ascii */
778 asciiblob_len = epayload->datablob_len + ivsize + 1
779 + roundup(epayload->decrypted_datalen, blksize)
782 if (!buffer || buflen < asciiblob_len)
783 return asciiblob_len;
785 mkey = request_master_key(epayload, &master_key, &master_keylen);
787 return PTR_ERR(mkey);
789 ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen);
793 ret = derived_key_encrypt(epayload, derived_key, sizeof derived_key);
797 ret = datablob_hmac_append(epayload, master_key, master_keylen);
801 ascii_buf = datablob_format(epayload, asciiblob_len);
810 if (copy_to_user(buffer, ascii_buf, asciiblob_len) != 0)
814 return asciiblob_len;
822 * encrypted_destroy - before freeing the key, clear the decrypted data
824 * Before freeing the key, clear the memory containing the decrypted
827 static void encrypted_destroy(struct key *key)
829 struct encrypted_key_payload *epayload = key->payload.data;
834 memset(epayload->decrypted_data, 0, epayload->decrypted_datalen);
835 kfree(key->payload.data);
838 struct key_type key_type_encrypted = {
840 .instantiate = encrypted_instantiate,
841 .update = encrypted_update,
843 .destroy = encrypted_destroy,
844 .describe = user_describe,
845 .read = encrypted_read,
847 EXPORT_SYMBOL_GPL(key_type_encrypted);
849 static void encrypted_shash_release(void)
852 crypto_free_shash(hashalg);
854 crypto_free_shash(hmacalg);
857 static int __init encrypted_shash_alloc(void)
861 hmacalg = crypto_alloc_shash(hmac_alg, 0, CRYPTO_ALG_ASYNC);
862 if (IS_ERR(hmacalg)) {
863 pr_info("encrypted_key: could not allocate crypto %s\n",
865 return PTR_ERR(hmacalg);
868 hashalg = crypto_alloc_shash(hash_alg, 0, CRYPTO_ALG_ASYNC);
869 if (IS_ERR(hashalg)) {
870 pr_info("encrypted_key: could not allocate crypto %s\n",
872 ret = PTR_ERR(hashalg);
879 crypto_free_shash(hmacalg);
883 static int __init init_encrypted(void)
887 ret = encrypted_shash_alloc();
890 ret = register_key_type(&key_type_encrypted);
893 return aes_get_sizes();
895 encrypted_shash_release();
900 static void __exit cleanup_encrypted(void)
902 encrypted_shash_release();
903 unregister_key_type(&key_type_encrypted);
906 late_initcall(init_encrypted);
907 module_exit(cleanup_encrypted);
909 MODULE_LICENSE("GPL");