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/crypto.h>
32 #include <linux/ctype.h>
33 #include <crypto/hash.h>
34 #include <crypto/sha.h>
35 #include <crypto/aes.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_blkcipher
*tfm
;
90 tfm
= crypto_alloc_blkcipher(blkcipher_alg
, 0, CRYPTO_ALG_ASYNC
);
92 pr_err("encrypted_key: failed to alloc_cipher (%ld)\n",
96 ivsize
= crypto_blkcipher_ivsize(tfm
);
97 blksize
= crypto_blkcipher_blocksize(tfm
);
98 crypto_free_blkcipher(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
)
144 if (!memcmp(new_desc
, KEY_TRUSTED_PREFIX
, KEY_TRUSTED_PREFIX_LEN
)) {
145 if (strlen(new_desc
) == KEY_TRUSTED_PREFIX_LEN
)
148 if (memcmp(new_desc
, orig_desc
, KEY_TRUSTED_PREFIX_LEN
))
150 } else if (!memcmp(new_desc
, KEY_USER_PREFIX
, KEY_USER_PREFIX_LEN
)) {
151 if (strlen(new_desc
) == KEY_USER_PREFIX_LEN
)
154 if (memcmp(new_desc
, orig_desc
, KEY_USER_PREFIX_LEN
))
164 * datablob_parse - parse the keyctl data
167 * new [<format>] <master-key name> <decrypted data length>
168 * load [<format>] <master-key name> <decrypted data length>
169 * <encrypted iv + data>
170 * update <new-master-key name>
172 * Tokenizes a copy of the keyctl data, returning a pointer to each token,
173 * which is null terminated.
175 * On success returns 0, otherwise -EINVAL.
177 static int datablob_parse(char *datablob
, const char **format
,
178 char **master_desc
, char **decrypted_datalen
,
179 char **hex_encoded_iv
)
181 substring_t args
[MAX_OPT_ARGS
];
187 keyword
= strsep(&datablob
, " \t");
189 pr_info("encrypted_key: insufficient parameters specified\n");
192 key_cmd
= match_token(keyword
, key_tokens
, args
);
194 /* Get optional format: default | ecryptfs */
195 p
= strsep(&datablob
, " \t");
197 pr_err("encrypted_key: insufficient parameters specified\n");
201 key_format
= match_token(p
, key_format_tokens
, args
);
202 switch (key_format
) {
206 *master_desc
= strsep(&datablob
, " \t");
214 pr_info("encrypted_key: master key parameter is missing\n");
218 if (valid_master_desc(*master_desc
, NULL
) < 0) {
219 pr_info("encrypted_key: master key parameter \'%s\' "
220 "is invalid\n", *master_desc
);
224 if (decrypted_datalen
) {
225 *decrypted_datalen
= strsep(&datablob
, " \t");
226 if (!*decrypted_datalen
) {
227 pr_info("encrypted_key: keylen parameter is missing\n");
234 if (!decrypted_datalen
) {
235 pr_info("encrypted_key: keyword \'%s\' not allowed "
236 "when called from .update method\n", keyword
);
242 if (!decrypted_datalen
) {
243 pr_info("encrypted_key: keyword \'%s\' not allowed "
244 "when called from .update method\n", keyword
);
247 *hex_encoded_iv
= strsep(&datablob
, " \t");
248 if (!*hex_encoded_iv
) {
249 pr_info("encrypted_key: hex blob is missing\n");
255 if (decrypted_datalen
) {
256 pr_info("encrypted_key: keyword \'%s\' not allowed "
257 "when called from .instantiate method\n",
264 pr_info("encrypted_key: keyword \'%s\' not recognized\n",
273 * datablob_format - format as an ascii string, before copying to userspace
275 static char *datablob_format(struct encrypted_key_payload
*epayload
,
276 size_t asciiblob_len
)
278 char *ascii_buf
, *bufp
;
279 u8
*iv
= epayload
->iv
;
283 ascii_buf
= kmalloc(asciiblob_len
+ 1, GFP_KERNEL
);
287 ascii_buf
[asciiblob_len
] = '\0';
289 /* copy datablob master_desc and datalen strings */
290 len
= sprintf(ascii_buf
, "%s %s %s ", epayload
->format
,
291 epayload
->master_desc
, epayload
->datalen
);
293 /* convert the hex encoded iv, encrypted-data and HMAC to ascii */
294 bufp
= &ascii_buf
[len
];
295 for (i
= 0; i
< (asciiblob_len
- len
) / 2; i
++)
296 bufp
= pack_hex_byte(bufp
, iv
[i
]);
302 * request_user_key - request the user key
304 * Use a user provided key to encrypt/decrypt an encrypted-key.
306 static struct key
*request_user_key(const char *master_desc
, u8
**master_key
,
307 size_t *master_keylen
)
309 struct user_key_payload
*upayload
;
312 ukey
= request_key(&key_type_user
, master_desc
, NULL
);
316 down_read(&ukey
->sem
);
317 upayload
= rcu_dereference(ukey
->payload
.data
);
318 *master_key
= upayload
->data
;
319 *master_keylen
= upayload
->datalen
;
324 static struct sdesc
*alloc_sdesc(struct crypto_shash
*alg
)
329 size
= sizeof(struct shash_desc
) + crypto_shash_descsize(alg
);
330 sdesc
= kmalloc(size
, GFP_KERNEL
);
332 return ERR_PTR(-ENOMEM
);
333 sdesc
->shash
.tfm
= alg
;
334 sdesc
->shash
.flags
= 0x0;
338 static int calc_hmac(u8
*digest
, const u8
*key
, unsigned int keylen
,
339 const u8
*buf
, unsigned int buflen
)
344 sdesc
= alloc_sdesc(hmacalg
);
346 pr_info("encrypted_key: can't alloc %s\n", hmac_alg
);
347 return PTR_ERR(sdesc
);
350 ret
= crypto_shash_setkey(hmacalg
, key
, keylen
);
352 ret
= crypto_shash_digest(&sdesc
->shash
, buf
, buflen
, digest
);
357 static int calc_hash(u8
*digest
, const u8
*buf
, unsigned int buflen
)
362 sdesc
= alloc_sdesc(hashalg
);
364 pr_info("encrypted_key: can't alloc %s\n", hash_alg
);
365 return PTR_ERR(sdesc
);
368 ret
= crypto_shash_digest(&sdesc
->shash
, buf
, buflen
, digest
);
373 enum derived_key_type
{ ENC_KEY
, AUTH_KEY
};
375 /* Derive authentication/encryption key from trusted key */
376 static int get_derived_key(u8
*derived_key
, enum derived_key_type key_type
,
377 const u8
*master_key
, size_t master_keylen
)
380 unsigned int derived_buf_len
;
383 derived_buf_len
= strlen("AUTH_KEY") + 1 + master_keylen
;
384 if (derived_buf_len
< HASH_SIZE
)
385 derived_buf_len
= HASH_SIZE
;
387 derived_buf
= kzalloc(derived_buf_len
, GFP_KERNEL
);
389 pr_err("encrypted_key: out of memory\n");
393 strcpy(derived_buf
, "AUTH_KEY");
395 strcpy(derived_buf
, "ENC_KEY");
397 memcpy(derived_buf
+ strlen(derived_buf
) + 1, master_key
,
399 ret
= calc_hash(derived_key
, derived_buf
, derived_buf_len
);
404 static int init_blkcipher_desc(struct blkcipher_desc
*desc
, const u8
*key
,
405 unsigned int key_len
, const u8
*iv
,
410 desc
->tfm
= crypto_alloc_blkcipher(blkcipher_alg
, 0, CRYPTO_ALG_ASYNC
);
411 if (IS_ERR(desc
->tfm
)) {
412 pr_err("encrypted_key: failed to load %s transform (%ld)\n",
413 blkcipher_alg
, PTR_ERR(desc
->tfm
));
414 return PTR_ERR(desc
->tfm
);
418 ret
= crypto_blkcipher_setkey(desc
->tfm
, key
, key_len
);
420 pr_err("encrypted_key: failed to setkey (%d)\n", ret
);
421 crypto_free_blkcipher(desc
->tfm
);
424 crypto_blkcipher_set_iv(desc
->tfm
, iv
, ivsize
);
428 static struct key
*request_master_key(struct encrypted_key_payload
*epayload
,
429 u8
**master_key
, size_t *master_keylen
)
431 struct key
*mkey
= NULL
;
433 if (!strncmp(epayload
->master_desc
, KEY_TRUSTED_PREFIX
,
434 KEY_TRUSTED_PREFIX_LEN
)) {
435 mkey
= request_trusted_key(epayload
->master_desc
+
436 KEY_TRUSTED_PREFIX_LEN
,
437 master_key
, master_keylen
);
438 } else if (!strncmp(epayload
->master_desc
, KEY_USER_PREFIX
,
439 KEY_USER_PREFIX_LEN
)) {
440 mkey
= request_user_key(epayload
->master_desc
+
442 master_key
, master_keylen
);
447 int ret
= PTR_ERR(epayload
);
449 if (ret
== -ENOTSUPP
)
450 pr_info("encrypted_key: key %s not supported",
451 epayload
->master_desc
);
453 pr_info("encrypted_key: key %s not found",
454 epayload
->master_desc
);
458 dump_master_key(*master_key
, *master_keylen
);
463 /* Before returning data to userspace, encrypt decrypted data. */
464 static int derived_key_encrypt(struct encrypted_key_payload
*epayload
,
465 const u8
*derived_key
,
466 unsigned int derived_keylen
)
468 struct scatterlist sg_in
[2];
469 struct scatterlist sg_out
[1];
470 struct blkcipher_desc desc
;
471 unsigned int encrypted_datalen
;
476 encrypted_datalen
= roundup(epayload
->decrypted_datalen
, blksize
);
477 padlen
= encrypted_datalen
- epayload
->decrypted_datalen
;
479 ret
= init_blkcipher_desc(&desc
, derived_key
, derived_keylen
,
480 epayload
->iv
, ivsize
);
483 dump_decrypted_data(epayload
);
485 memset(pad
, 0, sizeof pad
);
486 sg_init_table(sg_in
, 2);
487 sg_set_buf(&sg_in
[0], epayload
->decrypted_data
,
488 epayload
->decrypted_datalen
);
489 sg_set_buf(&sg_in
[1], pad
, padlen
);
491 sg_init_table(sg_out
, 1);
492 sg_set_buf(sg_out
, epayload
->encrypted_data
, encrypted_datalen
);
494 ret
= crypto_blkcipher_encrypt(&desc
, sg_out
, sg_in
, encrypted_datalen
);
495 crypto_free_blkcipher(desc
.tfm
);
497 pr_err("encrypted_key: failed to encrypt (%d)\n", ret
);
499 dump_encrypted_data(epayload
, encrypted_datalen
);
504 static int datablob_hmac_append(struct encrypted_key_payload
*epayload
,
505 const u8
*master_key
, size_t master_keylen
)
507 u8 derived_key
[HASH_SIZE
];
511 ret
= get_derived_key(derived_key
, AUTH_KEY
, master_key
, master_keylen
);
515 digest
= epayload
->format
+ epayload
->datablob_len
;
516 ret
= calc_hmac(digest
, derived_key
, sizeof derived_key
,
517 epayload
->format
, epayload
->datablob_len
);
519 dump_hmac(NULL
, digest
, HASH_SIZE
);
524 /* verify HMAC before decrypting encrypted key */
525 static int datablob_hmac_verify(struct encrypted_key_payload
*epayload
,
526 const u8
*format
, const u8
*master_key
,
527 size_t master_keylen
)
529 u8 derived_key
[HASH_SIZE
];
530 u8 digest
[HASH_SIZE
];
535 ret
= get_derived_key(derived_key
, AUTH_KEY
, master_key
, master_keylen
);
539 len
= epayload
->datablob_len
;
541 p
= epayload
->master_desc
;
542 len
-= strlen(epayload
->format
) + 1;
544 p
= epayload
->format
;
546 ret
= calc_hmac(digest
, derived_key
, sizeof derived_key
, p
, len
);
549 ret
= memcmp(digest
, epayload
->format
+ epayload
->datablob_len
,
553 dump_hmac("datablob",
554 epayload
->format
+ epayload
->datablob_len
,
556 dump_hmac("calc", digest
, HASH_SIZE
);
562 static int derived_key_decrypt(struct encrypted_key_payload
*epayload
,
563 const u8
*derived_key
,
564 unsigned int derived_keylen
)
566 struct scatterlist sg_in
[1];
567 struct scatterlist sg_out
[2];
568 struct blkcipher_desc desc
;
569 unsigned int encrypted_datalen
;
573 encrypted_datalen
= roundup(epayload
->decrypted_datalen
, blksize
);
574 ret
= init_blkcipher_desc(&desc
, derived_key
, derived_keylen
,
575 epayload
->iv
, ivsize
);
578 dump_encrypted_data(epayload
, encrypted_datalen
);
580 memset(pad
, 0, sizeof pad
);
581 sg_init_table(sg_in
, 1);
582 sg_init_table(sg_out
, 2);
583 sg_set_buf(sg_in
, epayload
->encrypted_data
, encrypted_datalen
);
584 sg_set_buf(&sg_out
[0], epayload
->decrypted_data
,
585 epayload
->decrypted_datalen
);
586 sg_set_buf(&sg_out
[1], pad
, sizeof pad
);
588 ret
= crypto_blkcipher_decrypt(&desc
, sg_out
, sg_in
, encrypted_datalen
);
589 crypto_free_blkcipher(desc
.tfm
);
592 dump_decrypted_data(epayload
);
597 /* Allocate memory for decrypted key and datablob. */
598 static struct encrypted_key_payload
*encrypted_key_alloc(struct key
*key
,
600 const char *master_desc
,
603 struct encrypted_key_payload
*epayload
= NULL
;
604 unsigned short datablob_len
;
605 unsigned short decrypted_datalen
;
606 unsigned short payload_datalen
;
607 unsigned int encrypted_datalen
;
608 unsigned int format_len
;
612 ret
= strict_strtol(datalen
, 10, &dlen
);
613 if (ret
< 0 || dlen
< MIN_DATA_SIZE
|| dlen
> MAX_DATA_SIZE
)
614 return ERR_PTR(-EINVAL
);
616 format_len
= (!format
) ? strlen(key_format_default
) : strlen(format
);
617 decrypted_datalen
= dlen
;
618 payload_datalen
= decrypted_datalen
;
619 if (format
&& !strcmp(format
, key_format_ecryptfs
)) {
620 if (dlen
!= ECRYPTFS_MAX_KEY_BYTES
) {
621 pr_err("encrypted_key: keylen for the ecryptfs format "
622 "must be equal to %d bytes\n",
623 ECRYPTFS_MAX_KEY_BYTES
);
624 return ERR_PTR(-EINVAL
);
626 decrypted_datalen
= ECRYPTFS_MAX_KEY_BYTES
;
627 payload_datalen
= sizeof(struct ecryptfs_auth_tok
);
630 encrypted_datalen
= roundup(decrypted_datalen
, blksize
);
632 datablob_len
= format_len
+ 1 + strlen(master_desc
) + 1
633 + strlen(datalen
) + 1 + ivsize
+ 1 + encrypted_datalen
;
635 ret
= key_payload_reserve(key
, payload_datalen
+ datablob_len
640 epayload
= kzalloc(sizeof(*epayload
) + payload_datalen
+
641 datablob_len
+ HASH_SIZE
+ 1, GFP_KERNEL
);
643 return ERR_PTR(-ENOMEM
);
645 epayload
->payload_datalen
= payload_datalen
;
646 epayload
->decrypted_datalen
= decrypted_datalen
;
647 epayload
->datablob_len
= datablob_len
;
651 static int encrypted_key_decrypt(struct encrypted_key_payload
*epayload
,
652 const char *format
, const char *hex_encoded_iv
)
655 u8 derived_key
[HASH_SIZE
];
658 const char *hex_encoded_data
;
659 unsigned int encrypted_datalen
;
660 size_t master_keylen
;
664 encrypted_datalen
= roundup(epayload
->decrypted_datalen
, blksize
);
665 asciilen
= (ivsize
+ 1 + encrypted_datalen
+ HASH_SIZE
) * 2;
666 if (strlen(hex_encoded_iv
) != asciilen
)
669 hex_encoded_data
= hex_encoded_iv
+ (2 * ivsize
) + 2;
670 hex2bin(epayload
->iv
, hex_encoded_iv
, ivsize
);
671 hex2bin(epayload
->encrypted_data
, hex_encoded_data
, encrypted_datalen
);
673 hmac
= epayload
->format
+ epayload
->datablob_len
;
674 hex2bin(hmac
, hex_encoded_data
+ (encrypted_datalen
* 2), HASH_SIZE
);
676 mkey
= request_master_key(epayload
, &master_key
, &master_keylen
);
678 return PTR_ERR(mkey
);
680 ret
= datablob_hmac_verify(epayload
, format
, master_key
, master_keylen
);
682 pr_err("encrypted_key: bad hmac (%d)\n", ret
);
686 ret
= get_derived_key(derived_key
, ENC_KEY
, master_key
, master_keylen
);
690 ret
= derived_key_decrypt(epayload
, derived_key
, sizeof derived_key
);
692 pr_err("encrypted_key: failed to decrypt key (%d)\n", ret
);
699 static void __ekey_init(struct encrypted_key_payload
*epayload
,
700 const char *format
, const char *master_desc
,
703 unsigned int format_len
;
705 format_len
= (!format
) ? strlen(key_format_default
) : strlen(format
);
706 epayload
->format
= epayload
->payload_data
+ epayload
->payload_datalen
;
707 epayload
->master_desc
= epayload
->format
+ format_len
+ 1;
708 epayload
->datalen
= epayload
->master_desc
+ strlen(master_desc
) + 1;
709 epayload
->iv
= epayload
->datalen
+ strlen(datalen
) + 1;
710 epayload
->encrypted_data
= epayload
->iv
+ ivsize
+ 1;
711 epayload
->decrypted_data
= epayload
->payload_data
;
714 memcpy(epayload
->format
, key_format_default
, format_len
);
716 if (!strcmp(format
, key_format_ecryptfs
))
717 epayload
->decrypted_data
=
718 ecryptfs_get_auth_tok_key((struct ecryptfs_auth_tok
*)epayload
->payload_data
);
720 memcpy(epayload
->format
, format
, format_len
);
723 memcpy(epayload
->master_desc
, master_desc
, strlen(master_desc
));
724 memcpy(epayload
->datalen
, datalen
, strlen(datalen
));
728 * encrypted_init - initialize an encrypted key
730 * For a new key, use a random number for both the iv and data
731 * itself. For an old key, decrypt the hex encoded data.
733 static int encrypted_init(struct encrypted_key_payload
*epayload
,
734 const char *key_desc
, const char *format
,
735 const char *master_desc
, const char *datalen
,
736 const char *hex_encoded_iv
)
740 if (format
&& !strcmp(format
, key_format_ecryptfs
)) {
741 ret
= valid_ecryptfs_desc(key_desc
);
745 ecryptfs_fill_auth_tok((struct ecryptfs_auth_tok
*)epayload
->payload_data
,
749 __ekey_init(epayload
, format
, master_desc
, datalen
);
750 if (!hex_encoded_iv
) {
751 get_random_bytes(epayload
->iv
, ivsize
);
753 get_random_bytes(epayload
->decrypted_data
,
754 epayload
->decrypted_datalen
);
756 ret
= encrypted_key_decrypt(epayload
, format
, hex_encoded_iv
);
761 * encrypted_instantiate - instantiate an encrypted key
763 * Decrypt an existing encrypted datablob or create a new encrypted key
764 * based on a kernel random number.
766 * On success, return 0. Otherwise return errno.
768 static int encrypted_instantiate(struct key
*key
, const void *data
,
771 struct encrypted_key_payload
*epayload
= NULL
;
772 char *datablob
= NULL
;
773 const char *format
= NULL
;
774 char *master_desc
= NULL
;
775 char *decrypted_datalen
= NULL
;
776 char *hex_encoded_iv
= NULL
;
779 if (datalen
<= 0 || datalen
> 32767 || !data
)
782 datablob
= kmalloc(datalen
+ 1, GFP_KERNEL
);
785 datablob
[datalen
] = 0;
786 memcpy(datablob
, data
, datalen
);
787 ret
= datablob_parse(datablob
, &format
, &master_desc
,
788 &decrypted_datalen
, &hex_encoded_iv
);
792 epayload
= encrypted_key_alloc(key
, format
, master_desc
,
794 if (IS_ERR(epayload
)) {
795 ret
= PTR_ERR(epayload
);
798 ret
= encrypted_init(epayload
, key
->description
, format
, master_desc
,
799 decrypted_datalen
, hex_encoded_iv
);
805 rcu_assign_pointer(key
->payload
.data
, epayload
);
811 static void encrypted_rcu_free(struct rcu_head
*rcu
)
813 struct encrypted_key_payload
*epayload
;
815 epayload
= container_of(rcu
, struct encrypted_key_payload
, rcu
);
816 memset(epayload
->decrypted_data
, 0, epayload
->decrypted_datalen
);
821 * encrypted_update - update the master key description
823 * Change the master key description for an existing encrypted key.
824 * The next read will return an encrypted datablob using the new
825 * master key description.
827 * On success, return 0. Otherwise return errno.
829 static int encrypted_update(struct key
*key
, const void *data
, size_t datalen
)
831 struct encrypted_key_payload
*epayload
= key
->payload
.data
;
832 struct encrypted_key_payload
*new_epayload
;
834 char *new_master_desc
= NULL
;
835 const char *format
= NULL
;
838 if (datalen
<= 0 || datalen
> 32767 || !data
)
841 buf
= kmalloc(datalen
+ 1, GFP_KERNEL
);
846 memcpy(buf
, data
, datalen
);
847 ret
= datablob_parse(buf
, &format
, &new_master_desc
, NULL
, NULL
);
851 ret
= valid_master_desc(new_master_desc
, epayload
->master_desc
);
855 new_epayload
= encrypted_key_alloc(key
, epayload
->format
,
856 new_master_desc
, epayload
->datalen
);
857 if (IS_ERR(new_epayload
)) {
858 ret
= PTR_ERR(new_epayload
);
862 __ekey_init(new_epayload
, epayload
->format
, new_master_desc
,
865 memcpy(new_epayload
->iv
, epayload
->iv
, ivsize
);
866 memcpy(new_epayload
->payload_data
, epayload
->payload_data
,
867 epayload
->payload_datalen
);
869 rcu_assign_pointer(key
->payload
.data
, new_epayload
);
870 call_rcu(&epayload
->rcu
, encrypted_rcu_free
);
877 * encrypted_read - format and copy the encrypted data to userspace
879 * The resulting datablob format is:
880 * <master-key name> <decrypted data length> <encrypted iv> <encrypted data>
882 * On success, return to userspace the encrypted key datablob size.
884 static long encrypted_read(const struct key
*key
, char __user
*buffer
,
887 struct encrypted_key_payload
*epayload
;
890 size_t master_keylen
;
891 char derived_key
[HASH_SIZE
];
893 size_t asciiblob_len
;
896 epayload
= rcu_dereference_key(key
);
898 /* returns the hex encoded iv, encrypted-data, and hmac as ascii */
899 asciiblob_len
= epayload
->datablob_len
+ ivsize
+ 1
900 + roundup(epayload
->decrypted_datalen
, blksize
)
903 if (!buffer
|| buflen
< asciiblob_len
)
904 return asciiblob_len
;
906 mkey
= request_master_key(epayload
, &master_key
, &master_keylen
);
908 return PTR_ERR(mkey
);
910 ret
= get_derived_key(derived_key
, ENC_KEY
, master_key
, master_keylen
);
914 ret
= derived_key_encrypt(epayload
, derived_key
, sizeof derived_key
);
918 ret
= datablob_hmac_append(epayload
, master_key
, master_keylen
);
922 ascii_buf
= datablob_format(epayload
, asciiblob_len
);
931 if (copy_to_user(buffer
, ascii_buf
, asciiblob_len
) != 0)
935 return asciiblob_len
;
943 * encrypted_destroy - before freeing the key, clear the decrypted data
945 * Before freeing the key, clear the memory containing the decrypted
948 static void encrypted_destroy(struct key
*key
)
950 struct encrypted_key_payload
*epayload
= key
->payload
.data
;
955 memset(epayload
->decrypted_data
, 0, epayload
->decrypted_datalen
);
956 kfree(key
->payload
.data
);
959 struct key_type key_type_encrypted
= {
961 .instantiate
= encrypted_instantiate
,
962 .update
= encrypted_update
,
964 .destroy
= encrypted_destroy
,
965 .describe
= user_describe
,
966 .read
= encrypted_read
,
968 EXPORT_SYMBOL_GPL(key_type_encrypted
);
970 static void encrypted_shash_release(void)
973 crypto_free_shash(hashalg
);
975 crypto_free_shash(hmacalg
);
978 static int __init
encrypted_shash_alloc(void)
982 hmacalg
= crypto_alloc_shash(hmac_alg
, 0, CRYPTO_ALG_ASYNC
);
983 if (IS_ERR(hmacalg
)) {
984 pr_info("encrypted_key: could not allocate crypto %s\n",
986 return PTR_ERR(hmacalg
);
989 hashalg
= crypto_alloc_shash(hash_alg
, 0, CRYPTO_ALG_ASYNC
);
990 if (IS_ERR(hashalg
)) {
991 pr_info("encrypted_key: could not allocate crypto %s\n",
993 ret
= PTR_ERR(hashalg
);
1000 crypto_free_shash(hmacalg
);
1004 static int __init
init_encrypted(void)
1008 ret
= encrypted_shash_alloc();
1011 ret
= register_key_type(&key_type_encrypted
);
1014 return aes_get_sizes();
1016 encrypted_shash_release();
1021 static void __exit
cleanup_encrypted(void)
1023 encrypted_shash_release();
1024 unregister_key_type(&key_type_encrypted
);
1027 late_initcall(init_encrypted
);
1028 module_exit(cleanup_encrypted
);
1030 MODULE_LICENSE("GPL");