2 * Copyright (C) 2010 IBM Corporation
3 * Copyright (C) 2010 Politecnico di Torino, Italy
4 * TORSEC group -- http://security.polito.it
7 * Mimi Zohar <zohar@us.ibm.com>
8 * Roberto Sassu <roberto.sassu@polito.it>
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.
14 * See Documentation/security/keys-trusted-encrypted.txt
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/hash.h>
34 #include <crypto/sha.h>
35 #include <crypto/skcipher.h>
37 #include "encrypted.h"
38 #include "ecryptfs_format.h"
40 static const char KEY_TRUSTED_PREFIX[] = "trusted:";
41 static const char KEY_USER_PREFIX[] = "user:";
42 static const char hash_alg[] = "sha256";
43 static const char hmac_alg[] = "hmac(sha256)";
44 static const char blkcipher_alg[] = "cbc(aes)";
45 static const char key_format_default[] = "default";
46 static const char key_format_ecryptfs[] = "ecryptfs";
47 static unsigned int ivsize;
50 #define KEY_TRUSTED_PREFIX_LEN (sizeof (KEY_TRUSTED_PREFIX) - 1)
51 #define KEY_USER_PREFIX_LEN (sizeof (KEY_USER_PREFIX) - 1)
52 #define KEY_ECRYPTFS_DESC_LEN 16
53 #define HASH_SIZE SHA256_DIGEST_SIZE
54 #define MAX_DATA_SIZE 4096
55 #define MIN_DATA_SIZE 20
58 struct shash_desc shash;
62 static struct crypto_shash *hashalg;
63 static struct crypto_shash *hmacalg;
66 Opt_err = -1, Opt_new, Opt_load, Opt_update
70 Opt_error = -1, Opt_default, Opt_ecryptfs
73 static const match_table_t key_format_tokens = {
74 {Opt_default, "default"},
75 {Opt_ecryptfs, "ecryptfs"},
79 static const match_table_t key_tokens = {
82 {Opt_update, "update"},
86 static int aes_get_sizes(void)
88 struct crypto_skcipher *tfm;
90 tfm = crypto_alloc_skcipher(blkcipher_alg, 0, CRYPTO_ALG_ASYNC);
92 pr_err("encrypted_key: failed to alloc_cipher (%ld)\n",
96 ivsize = crypto_skcipher_ivsize(tfm);
97 blksize = crypto_skcipher_blocksize(tfm);
98 crypto_free_skcipher(tfm);
103 * valid_ecryptfs_desc - verify the description of a new/loaded encrypted key
105 * The description of a encrypted key with format 'ecryptfs' must contain
106 * exactly 16 hexadecimal characters.
109 static int valid_ecryptfs_desc(const char *ecryptfs_desc)
113 if (strlen(ecryptfs_desc) != KEY_ECRYPTFS_DESC_LEN) {
114 pr_err("encrypted_key: key description must be %d hexadecimal "
115 "characters long\n", KEY_ECRYPTFS_DESC_LEN);
119 for (i = 0; i < KEY_ECRYPTFS_DESC_LEN; i++) {
120 if (!isxdigit(ecryptfs_desc[i])) {
121 pr_err("encrypted_key: key description must contain "
122 "only hexadecimal characters\n");
131 * valid_master_desc - verify the 'key-type:desc' of a new/updated master-key
133 * key-type:= "trusted:" | "user:"
134 * desc:= master-key description
136 * Verify that 'key-type' is valid and that 'desc' exists. On key update,
137 * only the master key description is permitted to change, not the key-type.
138 * The key-type remains constant.
140 * On success returns 0, otherwise -EINVAL.
142 static int valid_master_desc(const char *new_desc, const char *orig_desc)
146 if (!strncmp(new_desc, KEY_TRUSTED_PREFIX, KEY_TRUSTED_PREFIX_LEN))
147 prefix_len = KEY_TRUSTED_PREFIX_LEN;
148 else if (!strncmp(new_desc, KEY_USER_PREFIX, KEY_USER_PREFIX_LEN))
149 prefix_len = KEY_USER_PREFIX_LEN;
153 if (!new_desc[prefix_len])
156 if (orig_desc && strncmp(new_desc, orig_desc, prefix_len))
163 * datablob_parse - parse the keyctl data
166 * new [<format>] <master-key name> <decrypted data length>
167 * load [<format>] <master-key name> <decrypted data length>
168 * <encrypted iv + data>
169 * update <new-master-key name>
171 * Tokenizes a copy of the keyctl data, returning a pointer to each token,
172 * which is null terminated.
174 * On success returns 0, otherwise -EINVAL.
176 static int datablob_parse(char *datablob, const char **format,
177 char **master_desc, char **decrypted_datalen,
178 char **hex_encoded_iv)
180 substring_t args[MAX_OPT_ARGS];
186 keyword = strsep(&datablob, " \t");
188 pr_info("encrypted_key: insufficient parameters specified\n");
191 key_cmd = match_token(keyword, key_tokens, args);
193 /* Get optional format: default | ecryptfs */
194 p = strsep(&datablob, " \t");
196 pr_err("encrypted_key: insufficient parameters specified\n");
200 key_format = match_token(p, key_format_tokens, args);
201 switch (key_format) {
205 *master_desc = strsep(&datablob, " \t");
213 pr_info("encrypted_key: master key parameter is missing\n");
217 if (valid_master_desc(*master_desc, NULL) < 0) {
218 pr_info("encrypted_key: master key parameter \'%s\' "
219 "is invalid\n", *master_desc);
223 if (decrypted_datalen) {
224 *decrypted_datalen = strsep(&datablob, " \t");
225 if (!*decrypted_datalen) {
226 pr_info("encrypted_key: keylen parameter is missing\n");
233 if (!decrypted_datalen) {
234 pr_info("encrypted_key: keyword \'%s\' not allowed "
235 "when called from .update method\n", keyword);
241 if (!decrypted_datalen) {
242 pr_info("encrypted_key: keyword \'%s\' not allowed "
243 "when called from .update method\n", keyword);
246 *hex_encoded_iv = strsep(&datablob, " \t");
247 if (!*hex_encoded_iv) {
248 pr_info("encrypted_key: hex blob is missing\n");
254 if (decrypted_datalen) {
255 pr_info("encrypted_key: keyword \'%s\' not allowed "
256 "when called from .instantiate method\n",
263 pr_info("encrypted_key: keyword \'%s\' not recognized\n",
272 * datablob_format - format as an ascii string, before copying to userspace
274 static char *datablob_format(struct encrypted_key_payload *epayload,
275 size_t asciiblob_len)
277 char *ascii_buf, *bufp;
278 u8 *iv = epayload->iv;
282 ascii_buf = kmalloc(asciiblob_len + 1, GFP_KERNEL);
286 ascii_buf[asciiblob_len] = '\0';
288 /* copy datablob master_desc and datalen strings */
289 len = sprintf(ascii_buf, "%s %s %s ", epayload->format,
290 epayload->master_desc, epayload->datalen);
292 /* convert the hex encoded iv, encrypted-data and HMAC to ascii */
293 bufp = &ascii_buf[len];
294 for (i = 0; i < (asciiblob_len - len) / 2; i++)
295 bufp = hex_byte_pack(bufp, iv[i]);
301 * request_user_key - request the user key
303 * Use a user provided key to encrypt/decrypt an encrypted-key.
305 static struct key *request_user_key(const char *master_desc, const u8 **master_key,
306 size_t *master_keylen)
308 const struct user_key_payload *upayload;
311 ukey = request_key(&key_type_user, master_desc, NULL);
315 down_read(&ukey->sem);
316 upayload = user_key_payload(ukey);
318 /* key was revoked before we acquired its semaphore */
321 ukey = ERR_PTR(-EKEYREVOKED);
324 *master_key = upayload->data;
325 *master_keylen = upayload->datalen;
330 static struct sdesc *alloc_sdesc(struct crypto_shash *alg)
335 size = sizeof(struct shash_desc) + crypto_shash_descsize(alg);
336 sdesc = kmalloc(size, GFP_KERNEL);
338 return ERR_PTR(-ENOMEM);
339 sdesc->shash.tfm = alg;
340 sdesc->shash.flags = 0x0;
344 static int calc_hmac(u8 *digest, const u8 *key, unsigned int keylen,
345 const u8 *buf, unsigned int buflen)
350 sdesc = alloc_sdesc(hmacalg);
352 pr_info("encrypted_key: can't alloc %s\n", hmac_alg);
353 return PTR_ERR(sdesc);
356 ret = crypto_shash_setkey(hmacalg, key, keylen);
358 ret = crypto_shash_digest(&sdesc->shash, buf, buflen, digest);
363 static int calc_hash(u8 *digest, const u8 *buf, unsigned int buflen)
368 sdesc = alloc_sdesc(hashalg);
370 pr_info("encrypted_key: can't alloc %s\n", hash_alg);
371 return PTR_ERR(sdesc);
374 ret = crypto_shash_digest(&sdesc->shash, buf, buflen, digest);
379 enum derived_key_type { ENC_KEY, AUTH_KEY };
381 /* Derive authentication/encryption key from trusted key */
382 static int get_derived_key(u8 *derived_key, enum derived_key_type key_type,
383 const u8 *master_key, size_t master_keylen)
386 unsigned int derived_buf_len;
389 derived_buf_len = strlen("AUTH_KEY") + 1 + master_keylen;
390 if (derived_buf_len < HASH_SIZE)
391 derived_buf_len = HASH_SIZE;
393 derived_buf = kzalloc(derived_buf_len, GFP_KERNEL);
395 pr_err("encrypted_key: out of memory\n");
399 strcpy(derived_buf, "AUTH_KEY");
401 strcpy(derived_buf, "ENC_KEY");
403 memcpy(derived_buf + strlen(derived_buf) + 1, master_key,
405 ret = calc_hash(derived_key, derived_buf, derived_buf_len);
410 static struct skcipher_request *init_skcipher_req(const u8 *key,
411 unsigned int key_len)
413 struct skcipher_request *req;
414 struct crypto_skcipher *tfm;
417 tfm = crypto_alloc_skcipher(blkcipher_alg, 0, CRYPTO_ALG_ASYNC);
419 pr_err("encrypted_key: failed to load %s transform (%ld)\n",
420 blkcipher_alg, PTR_ERR(tfm));
421 return ERR_CAST(tfm);
424 ret = crypto_skcipher_setkey(tfm, key, key_len);
426 pr_err("encrypted_key: failed to setkey (%d)\n", ret);
427 crypto_free_skcipher(tfm);
431 req = skcipher_request_alloc(tfm, GFP_KERNEL);
433 pr_err("encrypted_key: failed to allocate request for %s\n",
435 crypto_free_skcipher(tfm);
436 return ERR_PTR(-ENOMEM);
439 skcipher_request_set_callback(req, 0, NULL, NULL);
443 static struct key *request_master_key(struct encrypted_key_payload *epayload,
444 const u8 **master_key, size_t *master_keylen)
446 struct key *mkey = ERR_PTR(-EINVAL);
448 if (!strncmp(epayload->master_desc, KEY_TRUSTED_PREFIX,
449 KEY_TRUSTED_PREFIX_LEN)) {
450 mkey = request_trusted_key(epayload->master_desc +
451 KEY_TRUSTED_PREFIX_LEN,
452 master_key, master_keylen);
453 } else if (!strncmp(epayload->master_desc, KEY_USER_PREFIX,
454 KEY_USER_PREFIX_LEN)) {
455 mkey = request_user_key(epayload->master_desc +
457 master_key, master_keylen);
462 int ret = PTR_ERR(mkey);
464 if (ret == -ENOTSUPP)
465 pr_info("encrypted_key: key %s not supported",
466 epayload->master_desc);
468 pr_info("encrypted_key: key %s not found",
469 epayload->master_desc);
473 dump_master_key(*master_key, *master_keylen);
478 /* Before returning data to userspace, encrypt decrypted data. */
479 static int derived_key_encrypt(struct encrypted_key_payload *epayload,
480 const u8 *derived_key,
481 unsigned int derived_keylen)
483 struct scatterlist sg_in[2];
484 struct scatterlist sg_out[1];
485 struct crypto_skcipher *tfm;
486 struct skcipher_request *req;
487 unsigned int encrypted_datalen;
488 u8 iv[AES_BLOCK_SIZE];
491 encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
493 req = init_skcipher_req(derived_key, derived_keylen);
497 dump_decrypted_data(epayload);
499 sg_init_table(sg_in, 2);
500 sg_set_buf(&sg_in[0], epayload->decrypted_data,
501 epayload->decrypted_datalen);
502 sg_set_page(&sg_in[1], ZERO_PAGE(0), AES_BLOCK_SIZE, 0);
504 sg_init_table(sg_out, 1);
505 sg_set_buf(sg_out, epayload->encrypted_data, encrypted_datalen);
507 memcpy(iv, epayload->iv, sizeof(iv));
508 skcipher_request_set_crypt(req, sg_in, sg_out, encrypted_datalen, iv);
509 ret = crypto_skcipher_encrypt(req);
510 tfm = crypto_skcipher_reqtfm(req);
511 skcipher_request_free(req);
512 crypto_free_skcipher(tfm);
514 pr_err("encrypted_key: failed to encrypt (%d)\n", ret);
516 dump_encrypted_data(epayload, encrypted_datalen);
521 static int datablob_hmac_append(struct encrypted_key_payload *epayload,
522 const u8 *master_key, size_t master_keylen)
524 u8 derived_key[HASH_SIZE];
528 ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen);
532 digest = epayload->format + epayload->datablob_len;
533 ret = calc_hmac(digest, derived_key, sizeof derived_key,
534 epayload->format, epayload->datablob_len);
536 dump_hmac(NULL, digest, HASH_SIZE);
541 /* verify HMAC before decrypting encrypted key */
542 static int datablob_hmac_verify(struct encrypted_key_payload *epayload,
543 const u8 *format, const u8 *master_key,
544 size_t master_keylen)
546 u8 derived_key[HASH_SIZE];
547 u8 digest[HASH_SIZE];
552 ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen);
556 len = epayload->datablob_len;
558 p = epayload->master_desc;
559 len -= strlen(epayload->format) + 1;
561 p = epayload->format;
563 ret = calc_hmac(digest, derived_key, sizeof derived_key, p, len);
566 ret = memcmp(digest, epayload->format + epayload->datablob_len,
570 dump_hmac("datablob",
571 epayload->format + epayload->datablob_len,
573 dump_hmac("calc", digest, HASH_SIZE);
579 static int derived_key_decrypt(struct encrypted_key_payload *epayload,
580 const u8 *derived_key,
581 unsigned int derived_keylen)
583 struct scatterlist sg_in[1];
584 struct scatterlist sg_out[2];
585 struct crypto_skcipher *tfm;
586 struct skcipher_request *req;
587 unsigned int encrypted_datalen;
588 u8 iv[AES_BLOCK_SIZE];
592 /* Throwaway buffer to hold the unused zero padding at the end */
593 pad = kmalloc(AES_BLOCK_SIZE, GFP_KERNEL);
597 encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
598 req = init_skcipher_req(derived_key, derived_keylen);
602 dump_encrypted_data(epayload, encrypted_datalen);
604 sg_init_table(sg_in, 1);
605 sg_init_table(sg_out, 2);
606 sg_set_buf(sg_in, epayload->encrypted_data, encrypted_datalen);
607 sg_set_buf(&sg_out[0], epayload->decrypted_data,
608 epayload->decrypted_datalen);
609 sg_set_buf(&sg_out[1], pad, AES_BLOCK_SIZE);
611 memcpy(iv, epayload->iv, sizeof(iv));
612 skcipher_request_set_crypt(req, sg_in, sg_out, encrypted_datalen, iv);
613 ret = crypto_skcipher_decrypt(req);
614 tfm = crypto_skcipher_reqtfm(req);
615 skcipher_request_free(req);
616 crypto_free_skcipher(tfm);
619 dump_decrypted_data(epayload);
625 /* Allocate memory for decrypted key and datablob. */
626 static struct encrypted_key_payload *encrypted_key_alloc(struct key *key,
628 const char *master_desc,
631 struct encrypted_key_payload *epayload = NULL;
632 unsigned short datablob_len;
633 unsigned short decrypted_datalen;
634 unsigned short payload_datalen;
635 unsigned int encrypted_datalen;
636 unsigned int format_len;
640 ret = kstrtol(datalen, 10, &dlen);
641 if (ret < 0 || dlen < MIN_DATA_SIZE || dlen > MAX_DATA_SIZE)
642 return ERR_PTR(-EINVAL);
644 format_len = (!format) ? strlen(key_format_default) : strlen(format);
645 decrypted_datalen = dlen;
646 payload_datalen = decrypted_datalen;
647 if (format && !strcmp(format, key_format_ecryptfs)) {
648 if (dlen != ECRYPTFS_MAX_KEY_BYTES) {
649 pr_err("encrypted_key: keylen for the ecryptfs format "
650 "must be equal to %d bytes\n",
651 ECRYPTFS_MAX_KEY_BYTES);
652 return ERR_PTR(-EINVAL);
654 decrypted_datalen = ECRYPTFS_MAX_KEY_BYTES;
655 payload_datalen = sizeof(struct ecryptfs_auth_tok);
658 encrypted_datalen = roundup(decrypted_datalen, blksize);
660 datablob_len = format_len + 1 + strlen(master_desc) + 1
661 + strlen(datalen) + 1 + ivsize + 1 + encrypted_datalen;
663 ret = key_payload_reserve(key, payload_datalen + datablob_len
668 epayload = kzalloc(sizeof(*epayload) + payload_datalen +
669 datablob_len + HASH_SIZE + 1, GFP_KERNEL);
671 return ERR_PTR(-ENOMEM);
673 epayload->payload_datalen = payload_datalen;
674 epayload->decrypted_datalen = decrypted_datalen;
675 epayload->datablob_len = datablob_len;
679 static int encrypted_key_decrypt(struct encrypted_key_payload *epayload,
680 const char *format, const char *hex_encoded_iv)
683 u8 derived_key[HASH_SIZE];
684 const u8 *master_key;
686 const char *hex_encoded_data;
687 unsigned int encrypted_datalen;
688 size_t master_keylen;
692 encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
693 asciilen = (ivsize + 1 + encrypted_datalen + HASH_SIZE) * 2;
694 if (strlen(hex_encoded_iv) != asciilen)
697 hex_encoded_data = hex_encoded_iv + (2 * ivsize) + 2;
698 ret = hex2bin(epayload->iv, hex_encoded_iv, ivsize);
701 ret = hex2bin(epayload->encrypted_data, hex_encoded_data,
706 hmac = epayload->format + epayload->datablob_len;
707 ret = hex2bin(hmac, hex_encoded_data + (encrypted_datalen * 2),
712 mkey = request_master_key(epayload, &master_key, &master_keylen);
714 return PTR_ERR(mkey);
716 ret = datablob_hmac_verify(epayload, format, master_key, master_keylen);
718 pr_err("encrypted_key: bad hmac (%d)\n", ret);
722 ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen);
726 ret = derived_key_decrypt(epayload, derived_key, sizeof derived_key);
728 pr_err("encrypted_key: failed to decrypt key (%d)\n", ret);
735 static void __ekey_init(struct encrypted_key_payload *epayload,
736 const char *format, const char *master_desc,
739 unsigned int format_len;
741 format_len = (!format) ? strlen(key_format_default) : strlen(format);
742 epayload->format = epayload->payload_data + epayload->payload_datalen;
743 epayload->master_desc = epayload->format + format_len + 1;
744 epayload->datalen = epayload->master_desc + strlen(master_desc) + 1;
745 epayload->iv = epayload->datalen + strlen(datalen) + 1;
746 epayload->encrypted_data = epayload->iv + ivsize + 1;
747 epayload->decrypted_data = epayload->payload_data;
750 memcpy(epayload->format, key_format_default, format_len);
752 if (!strcmp(format, key_format_ecryptfs))
753 epayload->decrypted_data =
754 ecryptfs_get_auth_tok_key((struct ecryptfs_auth_tok *)epayload->payload_data);
756 memcpy(epayload->format, format, format_len);
759 memcpy(epayload->master_desc, master_desc, strlen(master_desc));
760 memcpy(epayload->datalen, datalen, strlen(datalen));
764 * encrypted_init - initialize an encrypted key
766 * For a new key, use a random number for both the iv and data
767 * itself. For an old key, decrypt the hex encoded data.
769 static int encrypted_init(struct encrypted_key_payload *epayload,
770 const char *key_desc, const char *format,
771 const char *master_desc, const char *datalen,
772 const char *hex_encoded_iv)
776 if (format && !strcmp(format, key_format_ecryptfs)) {
777 ret = valid_ecryptfs_desc(key_desc);
781 ecryptfs_fill_auth_tok((struct ecryptfs_auth_tok *)epayload->payload_data,
785 __ekey_init(epayload, format, master_desc, datalen);
786 if (!hex_encoded_iv) {
787 get_random_bytes(epayload->iv, ivsize);
789 get_random_bytes(epayload->decrypted_data,
790 epayload->decrypted_datalen);
792 ret = encrypted_key_decrypt(epayload, format, hex_encoded_iv);
797 * encrypted_instantiate - instantiate an encrypted key
799 * Decrypt an existing encrypted datablob or create a new encrypted key
800 * based on a kernel random number.
802 * On success, return 0. Otherwise return errno.
804 static int encrypted_instantiate(struct key *key,
805 struct key_preparsed_payload *prep)
807 struct encrypted_key_payload *epayload = NULL;
808 char *datablob = NULL;
809 const char *format = NULL;
810 char *master_desc = NULL;
811 char *decrypted_datalen = NULL;
812 char *hex_encoded_iv = NULL;
813 size_t datalen = prep->datalen;
816 if (datalen <= 0 || datalen > 32767 || !prep->data)
819 datablob = kmalloc(datalen + 1, GFP_KERNEL);
822 datablob[datalen] = 0;
823 memcpy(datablob, prep->data, datalen);
824 ret = datablob_parse(datablob, &format, &master_desc,
825 &decrypted_datalen, &hex_encoded_iv);
829 epayload = encrypted_key_alloc(key, format, master_desc,
831 if (IS_ERR(epayload)) {
832 ret = PTR_ERR(epayload);
835 ret = encrypted_init(epayload, key->description, format, master_desc,
836 decrypted_datalen, hex_encoded_iv);
842 rcu_assign_keypointer(key, epayload);
848 static void encrypted_rcu_free(struct rcu_head *rcu)
850 struct encrypted_key_payload *epayload;
852 epayload = container_of(rcu, struct encrypted_key_payload, rcu);
853 memset(epayload->decrypted_data, 0, epayload->decrypted_datalen);
858 * encrypted_update - update the master key description
860 * Change the master key description for an existing encrypted key.
861 * The next read will return an encrypted datablob using the new
862 * master key description.
864 * On success, return 0. Otherwise return errno.
866 static int encrypted_update(struct key *key, struct key_preparsed_payload *prep)
868 struct encrypted_key_payload *epayload = key->payload.data[0];
869 struct encrypted_key_payload *new_epayload;
871 char *new_master_desc = NULL;
872 const char *format = NULL;
873 size_t datalen = prep->datalen;
876 if (key_is_negative(key))
878 if (datalen <= 0 || datalen > 32767 || !prep->data)
881 buf = kmalloc(datalen + 1, GFP_KERNEL);
886 memcpy(buf, prep->data, datalen);
887 ret = datablob_parse(buf, &format, &new_master_desc, NULL, NULL);
891 ret = valid_master_desc(new_master_desc, epayload->master_desc);
895 new_epayload = encrypted_key_alloc(key, epayload->format,
896 new_master_desc, epayload->datalen);
897 if (IS_ERR(new_epayload)) {
898 ret = PTR_ERR(new_epayload);
902 __ekey_init(new_epayload, epayload->format, new_master_desc,
905 memcpy(new_epayload->iv, epayload->iv, ivsize);
906 memcpy(new_epayload->payload_data, epayload->payload_data,
907 epayload->payload_datalen);
909 rcu_assign_keypointer(key, new_epayload);
910 call_rcu(&epayload->rcu, encrypted_rcu_free);
917 * encrypted_read - format and copy the encrypted data to userspace
919 * The resulting datablob format is:
920 * <master-key name> <decrypted data length> <encrypted iv> <encrypted data>
922 * On success, return to userspace the encrypted key datablob size.
924 static long encrypted_read(const struct key *key, char __user *buffer,
927 struct encrypted_key_payload *epayload;
929 const u8 *master_key;
930 size_t master_keylen;
931 char derived_key[HASH_SIZE];
933 size_t asciiblob_len;
936 epayload = rcu_dereference_key(key);
938 /* returns the hex encoded iv, encrypted-data, and hmac as ascii */
939 asciiblob_len = epayload->datablob_len + ivsize + 1
940 + roundup(epayload->decrypted_datalen, blksize)
943 if (!buffer || buflen < asciiblob_len)
944 return asciiblob_len;
946 mkey = request_master_key(epayload, &master_key, &master_keylen);
948 return PTR_ERR(mkey);
950 ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen);
954 ret = derived_key_encrypt(epayload, derived_key, sizeof derived_key);
958 ret = datablob_hmac_append(epayload, master_key, master_keylen);
962 ascii_buf = datablob_format(epayload, asciiblob_len);
971 if (copy_to_user(buffer, ascii_buf, asciiblob_len) != 0)
975 return asciiblob_len;
983 * encrypted_destroy - before freeing the key, clear the decrypted data
985 * Before freeing the key, clear the memory containing the decrypted
988 static void encrypted_destroy(struct key *key)
990 struct encrypted_key_payload *epayload = key->payload.data[0];
995 memset(epayload->decrypted_data, 0, epayload->decrypted_datalen);
996 kfree(key->payload.data[0]);
999 struct key_type key_type_encrypted = {
1000 .name = "encrypted",
1001 .instantiate = encrypted_instantiate,
1002 .update = encrypted_update,
1003 .destroy = encrypted_destroy,
1004 .describe = user_describe,
1005 .read = encrypted_read,
1007 EXPORT_SYMBOL_GPL(key_type_encrypted);
1009 static void encrypted_shash_release(void)
1012 crypto_free_shash(hashalg);
1014 crypto_free_shash(hmacalg);
1017 static int __init encrypted_shash_alloc(void)
1021 hmacalg = crypto_alloc_shash(hmac_alg, 0, CRYPTO_ALG_ASYNC);
1022 if (IS_ERR(hmacalg)) {
1023 pr_info("encrypted_key: could not allocate crypto %s\n",
1025 return PTR_ERR(hmacalg);
1028 hashalg = crypto_alloc_shash(hash_alg, 0, CRYPTO_ALG_ASYNC);
1029 if (IS_ERR(hashalg)) {
1030 pr_info("encrypted_key: could not allocate crypto %s\n",
1032 ret = PTR_ERR(hashalg);
1039 crypto_free_shash(hmacalg);
1043 static int __init init_encrypted(void)
1047 ret = encrypted_shash_alloc();
1050 ret = aes_get_sizes();
1053 ret = register_key_type(&key_type_encrypted);
1058 encrypted_shash_release();
1063 static void __exit cleanup_encrypted(void)
1065 encrypted_shash_release();
1066 unregister_key_type(&key_type_encrypted);
1069 late_initcall(init_encrypted);
1070 module_exit(cleanup_encrypted);
1072 MODULE_LICENSE("GPL");