18 #include <linux/module.h>
20 #include <linux/slab.h>
22 #include <linux/string.h>
28 #include <linux/random.h>
32 #include <linux/ctype.h>
34 #include <crypto/sha.h>
35 #include <crypto/aes.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)";
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
86 static int aes_get_sizes(
void)
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);
109 static int valid_ecryptfs_desc(
const char *ecryptfs_desc)
114 pr_err(
"encrypted_key: key description must be %d hexadecimal "
121 pr_err(
"encrypted_key: key description must contain "
122 "only hexadecimal characters\n");
142 static int valid_master_desc(
const char *new_desc,
const char *orig_desc)
177 static int datablob_parse(
char *datablob,
const char **
format,
178 char **master_desc,
char **decrypted_datalen,
179 char **hex_encoded_iv)
187 keyword =
strsep(&datablob,
" \t");
189 pr_info(
"encrypted_key: insufficient parameters specified\n");
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",
276 size_t asciiblob_len)
278 char *ascii_buf, *bufp;
287 ascii_buf[asciiblob_len] =
'\0';
294 bufp = &ascii_buf[len];
295 for (i = 0; i < (asciiblob_len - len) / 2; i++)
296 bufp = hex_byte_pack(bufp, iv[i]);
306 static struct key *request_user_key(
const char *master_desc,
u8 **master_key,
307 size_t *master_keylen)
317 upayload = ukey->payload.
data;
318 *master_key = upayload->
data;
319 *master_keylen = upayload->
datalen;
329 size =
sizeof(
struct shash_desc) + crypto_shash_descsize(alg);
333 sdesc->shash.tfm = alg;
334 sdesc->shash.flags = 0x0;
338 static int calc_hmac(
u8 *
digest,
const u8 *
key,
unsigned int keylen,
344 sdesc = alloc_sdesc(hmacalg);
346 pr_info(
"encrypted_key: can't alloc %s\n", hmac_alg);
347 return PTR_ERR(sdesc);
357 static int calc_hash(
u8 *digest,
const u8 *buf,
unsigned int buflen)
362 sdesc = alloc_sdesc(hashalg);
365 return PTR_ERR(sdesc);
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;
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);
411 if (IS_ERR(desc->
tfm)) {
412 pr_err(
"encrypted_key: failed to load %s transform (%ld)\n",
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);
429 u8 **master_key,
size_t *master_keylen)
437 master_key, master_keylen);
442 master_key, master_keylen);
447 int ret = PTR_ERR(mkey);
450 pr_info(
"encrypted_key: key %s not supported",
453 pr_info(
"encrypted_key: key %s not found",
458 dump_master_key(*master_key, *master_keylen);
465 const u8 *derived_key,
466 unsigned int derived_keylen)
471 unsigned int encrypted_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);
489 sg_set_buf(&sg_in[1], pad, padlen);
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);
505 const u8 *master_key,
size_t master_keylen)
511 ret = get_derived_key(derived_key,
AUTH_KEY, master_key, master_keylen);
516 ret = calc_hmac(digest, derived_key,
sizeof derived_key,
526 const u8 *format,
const u8 *master_key,
527 size_t master_keylen)
535 ret = get_derived_key(derived_key,
AUTH_KEY, master_key, master_keylen);
546 ret = calc_hmac(digest, derived_key,
sizeof derived_key, p, len);
553 dump_hmac(
"datablob",
563 const u8 *derived_key,
564 unsigned int derived_keylen)
569 unsigned int encrypted_datalen;
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);
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);
600 const char *master_desc,
607 unsigned int encrypted_datalen;
608 unsigned int format_len;
617 decrypted_datalen =
dlen;
619 if (format && !
strcmp(format, key_format_ecryptfs)) {
621 pr_err(
"encrypted_key: keylen for the ecryptfs format "
622 "must be equal to %d bytes\n",
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;
640 epayload = kzalloc(
sizeof(*epayload) + payload_datalen +
652 const char *format,
const char *hex_encoded_iv)
658 const char *hex_encoded_data;
659 unsigned int encrypted_datalen;
660 size_t master_keylen;
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 ret =
hex2bin(epayload->
iv, hex_encoded_iv, ivsize);
679 ret =
hex2bin(hmac, hex_encoded_data + (encrypted_datalen * 2),
684 mkey = request_master_key(epayload, &master_key, &master_keylen);
686 return PTR_ERR(mkey);
688 ret = datablob_hmac_verify(epayload, format, master_key, master_keylen);
690 pr_err(
"encrypted_key: bad hmac (%d)\n", ret);
694 ret = get_derived_key(derived_key,
ENC_KEY, master_key, master_keylen);
698 ret = derived_key_decrypt(epayload, derived_key,
sizeof derived_key);
700 pr_err(
"encrypted_key: failed to decrypt key (%d)\n", ret);
708 const char *format,
const char *master_desc,
711 unsigned int format_len;
722 memcpy(epayload->
format, key_format_default, format_len);
724 if (!
strcmp(format, key_format_ecryptfs))
742 const char *key_desc,
const char *format,
743 const char *master_desc,
const char *datalen,
744 const char *hex_encoded_iv)
748 if (format && !
strcmp(format, key_format_ecryptfs)) {
749 ret = valid_ecryptfs_desc(key_desc);
757 __ekey_init(epayload, format, master_desc, datalen);
758 if (!hex_encoded_iv) {
764 ret = encrypted_key_decrypt(epayload, format, hex_encoded_iv);
776 static int encrypted_instantiate(
struct key *key,
777 struct key_preparsed_payload *prep)
780 char *datablob =
NULL;
781 const char *format =
NULL;
782 char *master_desc =
NULL;
783 char *decrypted_datalen =
NULL;
784 char *hex_encoded_iv =
NULL;
785 size_t datalen = prep->datalen;
788 if (datalen <= 0 || datalen > 32767 || !prep->data)
795 memcpy(datablob, prep->data, datalen);
796 ret = datablob_parse(datablob, &format, &master_desc,
797 &decrypted_datalen, &hex_encoded_iv);
801 epayload = encrypted_key_alloc(key, format, master_desc,
803 if (IS_ERR(epayload)) {
804 ret = PTR_ERR(epayload);
807 ret = encrypted_init(epayload, key->description, format, master_desc,
808 decrypted_datalen, hex_encoded_iv);
814 rcu_assign_keypointer(key, epayload);
820 static void encrypted_rcu_free(
struct rcu_head *
rcu)
838 static int encrypted_update(
struct key *key,
struct key_preparsed_payload *prep)
843 char *new_master_desc =
NULL;
844 const char *format =
NULL;
845 size_t datalen = prep->datalen;
848 if (datalen <= 0 || datalen > 32767 || !prep->data)
856 memcpy(buf, prep->data, datalen);
857 ret = datablob_parse(buf, &format, &new_master_desc,
NULL,
NULL);
861 ret = valid_master_desc(new_master_desc, epayload->
master_desc);
865 new_epayload = encrypted_key_alloc(key, epayload->
format,
866 new_master_desc, epayload->
datalen);
867 if (IS_ERR(new_epayload)) {
868 ret = PTR_ERR(new_epayload);
872 __ekey_init(new_epayload, epayload->
format, new_master_desc,
875 memcpy(new_epayload->
iv, epayload->
iv, ivsize);
879 rcu_assign_keypointer(key, new_epayload);
894 static long encrypted_read(
const struct key *key,
char __user *
buffer,
900 size_t master_keylen;
903 size_t asciiblob_len;
906 epayload = rcu_dereference_key(key);
913 if (!buffer || buflen < asciiblob_len)
914 return asciiblob_len;
916 mkey = request_master_key(epayload, &master_key, &master_keylen);
918 return PTR_ERR(mkey);
920 ret = get_derived_key(derived_key,
ENC_KEY, master_key, master_keylen);
924 ret = derived_key_encrypt(epayload, derived_key,
sizeof derived_key);
928 ret = datablob_hmac_append(epayload, master_key, master_keylen);
932 ascii_buf = datablob_format(epayload, asciiblob_len);
941 if (
copy_to_user(buffer, ascii_buf, asciiblob_len) != 0)
945 return asciiblob_len;
958 static void encrypted_destroy(
struct key *key)
966 kfree(key->payload.data);
971 .instantiate = encrypted_instantiate,
972 .update = encrypted_update,
974 .destroy = encrypted_destroy,
976 .read = encrypted_read,
980 static void encrypted_shash_release(
void)
983 crypto_free_shash(hashalg);
985 crypto_free_shash(hmacalg);
988 static int __init encrypted_shash_alloc(
void)
993 if (IS_ERR(hmacalg)) {
994 pr_info(
"encrypted_key: could not allocate crypto %s\n",
996 return PTR_ERR(hmacalg);
1000 if (IS_ERR(hashalg)) {
1001 pr_info(
"encrypted_key: could not allocate crypto %s\n",
1003 ret = PTR_ERR(hashalg);
1010 crypto_free_shash(hmacalg);
1014 static int __init init_encrypted(
void)
1018 ret = encrypted_shash_alloc();
1024 return aes_get_sizes();
1026 encrypted_shash_release();
1031 static void __exit cleanup_encrypted(
void)
1033 encrypted_shash_release();