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.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;
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
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)
133 substring_t args[MAX_OPT_ARGS];
138 p = strsep(&datablob, " \t");
141 key_cmd = match_token(p, key_tokens, args);
143 *master_desc = strsep(&datablob, " \t");
147 if (valid_master_desc(*master_desc, NULL) < 0)
150 if (decrypted_datalen) {
151 *decrypted_datalen = strsep(&datablob, " \t");
152 if (!*decrypted_datalen)
158 if (!decrypted_datalen)
163 if (!decrypted_datalen)
165 *hex_encoded_iv = strsep(&datablob, " \t");
166 if (!*hex_encoded_iv)
171 if (decrypted_datalen)
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;
193 ascii_buf = kmalloc(asciiblob_len + 1, GFP_KERNEL);
197 ascii_buf[asciiblob_len] = '\0';
199 /* copy datablob master_desc and datalen strings */
200 len = sprintf(ascii_buf, "%s %s ", epayload->master_desc,
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]);
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;
224 tkey = request_key(&key_type_trusted, trusted_desc, NULL);
228 down_read(&tkey->sem);
229 tpayload = rcu_dereference(tkey->payload.data);
230 *master_key = tpayload->key;
231 *master_keylen = tpayload->key_len;
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;
247 ukey = request_key(&key_type_user, master_desc, NULL);
251 down_read(&ukey->sem);
252 upayload = rcu_dereference(ukey->payload.data);
253 *master_key = upayload->data;
254 *master_keylen = upayload->datalen;
259 static struct sdesc *alloc_sdesc(struct crypto_shash *alg)
264 size = sizeof(struct shash_desc) + crypto_shash_descsize(alg);
265 sdesc = kmalloc(size, GFP_KERNEL);
267 return ERR_PTR(-ENOMEM);
268 sdesc->shash.tfm = alg;
269 sdesc->shash.flags = 0x0;
273 static int calc_hmac(u8 *digest, const u8 *key, unsigned int keylen,
274 const u8 *buf, unsigned int buflen)
279 sdesc = alloc_sdesc(hmacalg);
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);
287 ret = crypto_shash_digest(&sdesc->shash, buf, buflen, digest);
292 static int calc_hash(u8 *digest, const u8 *buf, unsigned int buflen)
297 sdesc = alloc_sdesc(hashalg);
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);
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)
315 unsigned int derived_buf_len;
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);
324 pr_err("encrypted_key: out of memory\n");
328 strcpy(derived_buf, "AUTH_KEY");
330 strcpy(derived_buf, "ENC_KEY");
332 memcpy(derived_buf + strlen(derived_buf) + 1, master_key,
334 ret = calc_hash(derived_key, derived_buf, derived_buf_len);
339 static int init_blkcipher_desc(struct blkcipher_desc *desc, const u8 *key,
340 unsigned int key_len, const u8 *iv,
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);
353 ret = crypto_blkcipher_setkey(desc->tfm, key, key_len);
355 pr_err("encrypted_key: failed to setkey (%d)\n", ret);
356 crypto_free_blkcipher(desc->tfm);
359 crypto_blkcipher_set_iv(desc->tfm, iv, ivsize);
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 +
377 master_key, master_keylen);
382 pr_info("encrypted_key: key %s not found",
383 epayload->master_desc);
385 dump_master_key(*master_key, *master_keylen);
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;
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);
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);
424 pr_err("encrypted_key: failed to encrypt (%d)\n", ret);
426 dump_encrypted_data(epayload, encrypted_datalen);
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];
438 ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen);
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);
446 dump_hmac(NULL, digest, HASH_SIZE);
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];
459 ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen);
463 ret = calc_hmac(digest, derived_key, sizeof derived_key,
464 epayload->master_desc, epayload->datablob_len);
467 ret = memcmp(digest, epayload->master_desc + epayload->datablob_len,
471 dump_hmac("datablob",
472 epayload->master_desc + epayload->datablob_len,
474 dump_hmac("calc", digest, HASH_SIZE);
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;
491 encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
492 ret = init_blkcipher_desc(&desc, derived_key, derived_keylen,
493 epayload->iv, ivsize);
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);
510 dump_decrypted_data(epayload);
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,
520 struct encrypted_key_payload *epayload = NULL;
521 unsigned short datablob_len;
522 unsigned short decrypted_datalen;
523 unsigned int encrypted_datalen;
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
542 epayload = kzalloc(sizeof(*epayload) + decrypted_datalen +
543 datablob_len + HASH_SIZE + 1, GFP_KERNEL);
545 return ERR_PTR(-ENOMEM);
547 epayload->decrypted_datalen = decrypted_datalen;
548 epayload->datablob_len = datablob_len;
552 static int encrypted_key_decrypt(struct encrypted_key_payload *epayload,
553 const char *hex_encoded_iv)
556 u8 derived_key[HASH_SIZE];
559 const char *hex_encoded_data;
560 unsigned int encrypted_datalen;
561 size_t master_keylen;
565 encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
566 asciilen = (ivsize + 1 + encrypted_datalen + HASH_SIZE) * 2;
567 if (strlen(hex_encoded_iv) != asciilen)
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);
579 return PTR_ERR(mkey);
581 ret = datablob_hmac_verify(epayload, master_key, master_keylen);
583 pr_err("encrypted_key: bad hmac (%d)\n", ret);
587 ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen);
591 ret = derived_key_decrypt(epayload, derived_key, sizeof derived_key);
593 pr_err("encrypted_key: failed to decrypt key (%d)\n", 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)
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);
632 ret = encrypted_key_decrypt(epayload, hex_encoded_iv);
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,
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;
654 if (datalen <= 0 || datalen > 32767 || !data)
657 datablob = kmalloc(datalen + 1, GFP_KERNEL);
660 datablob[datalen] = 0;
661 memcpy(datablob, data, datalen);
662 ret = datablob_parse(datablob, &master_desc, &decrypted_datalen,
667 epayload = encrypted_key_alloc(key, master_desc, decrypted_datalen);
668 if (IS_ERR(epayload)) {
669 ret = PTR_ERR(epayload);
672 ret = encrypted_init(epayload, master_desc, decrypted_datalen,
679 rcu_assign_pointer(key->payload.data, epayload);
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);
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;
708 char *new_master_desc = NULL;
711 if (datalen <= 0 || datalen > 32767 || !data)
714 buf = kmalloc(datalen + 1, GFP_KERNEL);
719 memcpy(buf, data, datalen);
720 ret = datablob_parse(buf, &new_master_desc, NULL, NULL);
724 ret = valid_master_desc(new_master_desc, epayload->master_desc);
728 new_epayload = encrypted_key_alloc(key, new_master_desc,
730 if (IS_ERR(new_epayload)) {
731 ret = PTR_ERR(new_epayload);
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);
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,
759 struct encrypted_key_payload *epayload;
762 size_t master_keylen;
763 char derived_key[HASH_SIZE];
765 size_t asciiblob_len;
768 epayload = rcu_dereference_protected(key->payload.data,
769 rwsem_is_locked(&((struct key *)key)->sem));
771 /* returns the hex encoded iv, encrypted-data, and hmac as ascii */
772 asciiblob_len = epayload->datablob_len + ivsize + 1
773 + roundup(epayload->decrypted_datalen, blksize)
776 if (!buffer || buflen < asciiblob_len)
777 return asciiblob_len;
779 mkey = request_master_key(epayload, &master_key, &master_keylen);
781 return PTR_ERR(mkey);
783 ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen);
787 ret = derived_key_encrypt(epayload, derived_key, sizeof derived_key);
791 ret = datablob_hmac_append(epayload, master_key, master_keylen);
795 ascii_buf = datablob_format(epayload, asciiblob_len);
804 if (copy_to_user(buffer, ascii_buf, asciiblob_len) != 0)
808 return asciiblob_len;
816 * encrypted_destroy - before freeing the key, clear the decrypted data
818 * Before freeing the key, clear the memory containing the decrypted
821 static void encrypted_destroy(struct key *key)
823 struct encrypted_key_payload *epayload = key->payload.data;
828 memset(epayload->decrypted_data, 0, epayload->decrypted_datalen);
829 kfree(key->payload.data);
832 struct key_type key_type_encrypted = {
834 .instantiate = encrypted_instantiate,
835 .update = encrypted_update,
837 .destroy = encrypted_destroy,
838 .describe = user_describe,
839 .read = encrypted_read,
841 EXPORT_SYMBOL_GPL(key_type_encrypted);
843 static void encrypted_shash_release(void)
846 crypto_free_shash(hashalg);
848 crypto_free_shash(hmacalg);
851 static int __init encrypted_shash_alloc(void)
855 hmacalg = crypto_alloc_shash(hmac_alg, 0, CRYPTO_ALG_ASYNC);
856 if (IS_ERR(hmacalg)) {
857 pr_info("encrypted_key: could not allocate crypto %s\n",
859 return PTR_ERR(hmacalg);
862 hashalg = crypto_alloc_shash(hash_alg, 0, CRYPTO_ALG_ASYNC);
863 if (IS_ERR(hashalg)) {
864 pr_info("encrypted_key: could not allocate crypto %s\n",
866 ret = PTR_ERR(hashalg);
873 crypto_free_shash(hmacalg);
877 static int __init init_encrypted(void)
881 ret = encrypted_shash_alloc();
884 ret = register_key_type(&key_type_encrypted);
887 return aes_get_sizes();
889 encrypted_shash_release();
894 static void __exit cleanup_encrypted(void)
896 encrypted_shash_release();
897 unregister_key_type(&key_type_encrypted);
900 late_initcall(init_encrypted);
901 module_exit(cleanup_encrypted);
903 MODULE_LICENSE("GPL");