1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
261cf45d0SMimi Zohar /*
361cf45d0SMimi Zohar * Copyright (C) 2010 IBM Corporation
461cf45d0SMimi Zohar * Copyright (C) 2010 Politecnico di Torino, Italy
5c9fecf50SAlexander A. Klimov * TORSEC group -- https://security.polito.it
661cf45d0SMimi Zohar *
761cf45d0SMimi Zohar * Authors:
861cf45d0SMimi Zohar * Mimi Zohar <zohar@us.ibm.com>
961cf45d0SMimi Zohar * Roberto Sassu <roberto.sassu@polito.it>
1061cf45d0SMimi Zohar *
115395d312SKees Cook * See Documentation/security/keys/trusted-encrypted.rst
1261cf45d0SMimi Zohar */
1361cf45d0SMimi Zohar
1461cf45d0SMimi Zohar #include <linux/uaccess.h>
1561cf45d0SMimi Zohar #include <linux/module.h>
1661cf45d0SMimi Zohar #include <linux/init.h>
1761cf45d0SMimi Zohar #include <linux/slab.h>
1861cf45d0SMimi Zohar #include <linux/parser.h>
1961cf45d0SMimi Zohar #include <linux/string.h>
2061cf45d0SMimi Zohar #include <linux/err.h>
2161cf45d0SMimi Zohar #include <keys/user-type.h>
2261cf45d0SMimi Zohar #include <keys/trusted-type.h>
2361cf45d0SMimi Zohar #include <keys/encrypted-type.h>
2461cf45d0SMimi Zohar #include <linux/key-type.h>
2561cf45d0SMimi Zohar #include <linux/random.h>
2661cf45d0SMimi Zohar #include <linux/rcupdate.h>
2761cf45d0SMimi Zohar #include <linux/scatterlist.h>
2861cf45d0SMimi Zohar #include <linux/ctype.h>
29456bee98SHerbert Xu #include <crypto/aes.h>
3061cf45d0SMimi Zohar #include <crypto/hash.h>
31a24d22b2SEric Biggers #include <crypto/sha2.h>
32c3917fd9SHerbert Xu #include <crypto/skcipher.h>
33fb3bc06aSHerbert Xu #include <crypto/utils.h>
3461cf45d0SMimi Zohar
3561cf45d0SMimi Zohar #include "encrypted.h"
3661cf45d0SMimi Zohar #include "ecryptfs_format.h"
3761cf45d0SMimi Zohar
3861cf45d0SMimi Zohar static const char KEY_TRUSTED_PREFIX[] = "trusted:";
3961cf45d0SMimi Zohar static const char KEY_USER_PREFIX[] = "user:";
4061cf45d0SMimi Zohar static const char hash_alg[] = "sha256";
4161cf45d0SMimi Zohar static const char hmac_alg[] = "hmac(sha256)";
4261cf45d0SMimi Zohar static const char blkcipher_alg[] = "cbc(aes)";
4361cf45d0SMimi Zohar static const char key_format_default[] = "default";
4461cf45d0SMimi Zohar static const char key_format_ecryptfs[] = "ecryptfs";
459db67581SDave Jiang static const char key_format_enc32[] = "enc32";
4661cf45d0SMimi Zohar static unsigned int ivsize;
4761cf45d0SMimi Zohar static int blksize;
4861cf45d0SMimi Zohar
4961cf45d0SMimi Zohar #define KEY_TRUSTED_PREFIX_LEN (sizeof (KEY_TRUSTED_PREFIX) - 1)
5061cf45d0SMimi Zohar #define KEY_USER_PREFIX_LEN (sizeof (KEY_USER_PREFIX) - 1)
5161cf45d0SMimi Zohar #define KEY_ECRYPTFS_DESC_LEN 16
5261cf45d0SMimi Zohar #define HASH_SIZE SHA256_DIGEST_SIZE
5361cf45d0SMimi Zohar #define MAX_DATA_SIZE 4096
5461cf45d0SMimi Zohar #define MIN_DATA_SIZE 20
559db67581SDave Jiang #define KEY_ENC32_PAYLOAD_LEN 32
5661cf45d0SMimi Zohar
5764d107d3SEric Biggers static struct crypto_shash *hash_tfm;
5861cf45d0SMimi Zohar
5961cf45d0SMimi Zohar enum {
60107dfa2eSMimi Zohar Opt_new, Opt_load, Opt_update, Opt_err
6161cf45d0SMimi Zohar };
6261cf45d0SMimi Zohar
6361cf45d0SMimi Zohar enum {
64107dfa2eSMimi Zohar Opt_default, Opt_ecryptfs, Opt_enc32, Opt_error
6561cf45d0SMimi Zohar };
6661cf45d0SMimi Zohar
6761cf45d0SMimi Zohar static const match_table_t key_format_tokens = {
6861cf45d0SMimi Zohar {Opt_default, "default"},
6961cf45d0SMimi Zohar {Opt_ecryptfs, "ecryptfs"},
709db67581SDave Jiang {Opt_enc32, "enc32"},
7161cf45d0SMimi Zohar {Opt_error, NULL}
7261cf45d0SMimi Zohar };
7361cf45d0SMimi Zohar
7461cf45d0SMimi Zohar static const match_table_t key_tokens = {
7561cf45d0SMimi Zohar {Opt_new, "new"},
7661cf45d0SMimi Zohar {Opt_load, "load"},
7761cf45d0SMimi Zohar {Opt_update, "update"},
7861cf45d0SMimi Zohar {Opt_err, NULL}
7961cf45d0SMimi Zohar };
8061cf45d0SMimi Zohar
81cd3bc044SYael Tzur static bool user_decrypted_data = IS_ENABLED(CONFIG_USER_DECRYPTED_DATA);
82cd3bc044SYael Tzur module_param(user_decrypted_data, bool, 0);
83cd3bc044SYael Tzur MODULE_PARM_DESC(user_decrypted_data,
84cd3bc044SYael Tzur "Allow instantiation of encrypted keys using provided decrypted data");
85cd3bc044SYael Tzur
aes_get_sizes(void)8661cf45d0SMimi Zohar static int aes_get_sizes(void)
8761cf45d0SMimi Zohar {
88c3917fd9SHerbert Xu struct crypto_skcipher *tfm;
8961cf45d0SMimi Zohar
90c3917fd9SHerbert Xu tfm = crypto_alloc_skcipher(blkcipher_alg, 0, CRYPTO_ALG_ASYNC);
9161cf45d0SMimi Zohar if (IS_ERR(tfm)) {
9261cf45d0SMimi Zohar pr_err("encrypted_key: failed to alloc_cipher (%ld)\n",
9361cf45d0SMimi Zohar PTR_ERR(tfm));
9461cf45d0SMimi Zohar return PTR_ERR(tfm);
9561cf45d0SMimi Zohar }
96c3917fd9SHerbert Xu ivsize = crypto_skcipher_ivsize(tfm);
97c3917fd9SHerbert Xu blksize = crypto_skcipher_blocksize(tfm);
98c3917fd9SHerbert Xu crypto_free_skcipher(tfm);
9961cf45d0SMimi Zohar return 0;
10061cf45d0SMimi Zohar }
10161cf45d0SMimi Zohar
10261cf45d0SMimi Zohar /*
10361cf45d0SMimi Zohar * valid_ecryptfs_desc - verify the description of a new/loaded encrypted key
10461cf45d0SMimi Zohar *
10561cf45d0SMimi Zohar * The description of a encrypted key with format 'ecryptfs' must contain
10661cf45d0SMimi Zohar * exactly 16 hexadecimal characters.
10761cf45d0SMimi Zohar *
10861cf45d0SMimi Zohar */
valid_ecryptfs_desc(const char * ecryptfs_desc)10961cf45d0SMimi Zohar static int valid_ecryptfs_desc(const char *ecryptfs_desc)
11061cf45d0SMimi Zohar {
11161cf45d0SMimi Zohar int i;
11261cf45d0SMimi Zohar
11361cf45d0SMimi Zohar if (strlen(ecryptfs_desc) != KEY_ECRYPTFS_DESC_LEN) {
11461cf45d0SMimi Zohar pr_err("encrypted_key: key description must be %d hexadecimal "
11561cf45d0SMimi Zohar "characters long\n", KEY_ECRYPTFS_DESC_LEN);
11661cf45d0SMimi Zohar return -EINVAL;
11761cf45d0SMimi Zohar }
11861cf45d0SMimi Zohar
11961cf45d0SMimi Zohar for (i = 0; i < KEY_ECRYPTFS_DESC_LEN; i++) {
12061cf45d0SMimi Zohar if (!isxdigit(ecryptfs_desc[i])) {
12161cf45d0SMimi Zohar pr_err("encrypted_key: key description must contain "
12261cf45d0SMimi Zohar "only hexadecimal characters\n");
12361cf45d0SMimi Zohar return -EINVAL;
12461cf45d0SMimi Zohar }
12561cf45d0SMimi Zohar }
12661cf45d0SMimi Zohar
12761cf45d0SMimi Zohar return 0;
12861cf45d0SMimi Zohar }
12961cf45d0SMimi Zohar
13061cf45d0SMimi Zohar /*
13161cf45d0SMimi Zohar * valid_master_desc - verify the 'key-type:desc' of a new/updated master-key
13261cf45d0SMimi Zohar *
13361cf45d0SMimi Zohar * key-type:= "trusted:" | "user:"
13461cf45d0SMimi Zohar * desc:= master-key description
13561cf45d0SMimi Zohar *
13661cf45d0SMimi Zohar * Verify that 'key-type' is valid and that 'desc' exists. On key update,
13761cf45d0SMimi Zohar * only the master key description is permitted to change, not the key-type.
13861cf45d0SMimi Zohar * The key-type remains constant.
13961cf45d0SMimi Zohar *
14061cf45d0SMimi Zohar * On success returns 0, otherwise -EINVAL.
14161cf45d0SMimi Zohar */
valid_master_desc(const char * new_desc,const char * orig_desc)14261cf45d0SMimi Zohar static int valid_master_desc(const char *new_desc, const char *orig_desc)
14361cf45d0SMimi Zohar {
144794b4bc2SEric Biggers int prefix_len;
145794b4bc2SEric Biggers
146794b4bc2SEric Biggers if (!strncmp(new_desc, KEY_TRUSTED_PREFIX, KEY_TRUSTED_PREFIX_LEN))
147794b4bc2SEric Biggers prefix_len = KEY_TRUSTED_PREFIX_LEN;
148794b4bc2SEric Biggers else if (!strncmp(new_desc, KEY_USER_PREFIX, KEY_USER_PREFIX_LEN))
149794b4bc2SEric Biggers prefix_len = KEY_USER_PREFIX_LEN;
150794b4bc2SEric Biggers else
15161cf45d0SMimi Zohar return -EINVAL;
152794b4bc2SEric Biggers
153794b4bc2SEric Biggers if (!new_desc[prefix_len])
154794b4bc2SEric Biggers return -EINVAL;
155794b4bc2SEric Biggers
156794b4bc2SEric Biggers if (orig_desc && strncmp(new_desc, orig_desc, prefix_len))
157794b4bc2SEric Biggers return -EINVAL;
158794b4bc2SEric Biggers
159794b4bc2SEric Biggers return 0;
16061cf45d0SMimi Zohar }
16161cf45d0SMimi Zohar
16261cf45d0SMimi Zohar /*
16361cf45d0SMimi Zohar * datablob_parse - parse the keyctl data
16461cf45d0SMimi Zohar *
16561cf45d0SMimi Zohar * datablob format:
166cd3bc044SYael Tzur * new [<format>] <master-key name> <decrypted data length> [<decrypted data>]
16761cf45d0SMimi Zohar * load [<format>] <master-key name> <decrypted data length>
16861cf45d0SMimi Zohar * <encrypted iv + data>
16961cf45d0SMimi Zohar * update <new-master-key name>
17061cf45d0SMimi Zohar *
17161cf45d0SMimi Zohar * Tokenizes a copy of the keyctl data, returning a pointer to each token,
17261cf45d0SMimi Zohar * which is null terminated.
17361cf45d0SMimi Zohar *
17461cf45d0SMimi Zohar * On success returns 0, otherwise -EINVAL.
17561cf45d0SMimi Zohar */
datablob_parse(char * datablob,const char ** format,char ** master_desc,char ** decrypted_datalen,char ** hex_encoded_iv,char ** decrypted_data)17661cf45d0SMimi Zohar static int datablob_parse(char *datablob, const char **format,
17761cf45d0SMimi Zohar char **master_desc, char **decrypted_datalen,
178cd3bc044SYael Tzur char **hex_encoded_iv, char **decrypted_data)
17961cf45d0SMimi Zohar {
18061cf45d0SMimi Zohar substring_t args[MAX_OPT_ARGS];
18161cf45d0SMimi Zohar int ret = -EINVAL;
18261cf45d0SMimi Zohar int key_cmd;
18361cf45d0SMimi Zohar int key_format;
18461cf45d0SMimi Zohar char *p, *keyword;
18561cf45d0SMimi Zohar
18661cf45d0SMimi Zohar keyword = strsep(&datablob, " \t");
18761cf45d0SMimi Zohar if (!keyword) {
18861cf45d0SMimi Zohar pr_info("encrypted_key: insufficient parameters specified\n");
18961cf45d0SMimi Zohar return ret;
19061cf45d0SMimi Zohar }
19161cf45d0SMimi Zohar key_cmd = match_token(keyword, key_tokens, args);
19261cf45d0SMimi Zohar
19361cf45d0SMimi Zohar /* Get optional format: default | ecryptfs */
19461cf45d0SMimi Zohar p = strsep(&datablob, " \t");
19561cf45d0SMimi Zohar if (!p) {
19661cf45d0SMimi Zohar pr_err("encrypted_key: insufficient parameters specified\n");
19761cf45d0SMimi Zohar return ret;
19861cf45d0SMimi Zohar }
19961cf45d0SMimi Zohar
20061cf45d0SMimi Zohar key_format = match_token(p, key_format_tokens, args);
20161cf45d0SMimi Zohar switch (key_format) {
20261cf45d0SMimi Zohar case Opt_ecryptfs:
2039db67581SDave Jiang case Opt_enc32:
20461cf45d0SMimi Zohar case Opt_default:
20561cf45d0SMimi Zohar *format = p;
20661cf45d0SMimi Zohar *master_desc = strsep(&datablob, " \t");
20761cf45d0SMimi Zohar break;
20861cf45d0SMimi Zohar case Opt_error:
20961cf45d0SMimi Zohar *master_desc = p;
21061cf45d0SMimi Zohar break;
21161cf45d0SMimi Zohar }
21261cf45d0SMimi Zohar
21361cf45d0SMimi Zohar if (!*master_desc) {
21461cf45d0SMimi Zohar pr_info("encrypted_key: master key parameter is missing\n");
21561cf45d0SMimi Zohar goto out;
21661cf45d0SMimi Zohar }
21761cf45d0SMimi Zohar
21861cf45d0SMimi Zohar if (valid_master_desc(*master_desc, NULL) < 0) {
21961cf45d0SMimi Zohar pr_info("encrypted_key: master key parameter \'%s\' "
22061cf45d0SMimi Zohar "is invalid\n", *master_desc);
22161cf45d0SMimi Zohar goto out;
22261cf45d0SMimi Zohar }
22361cf45d0SMimi Zohar
22461cf45d0SMimi Zohar if (decrypted_datalen) {
22561cf45d0SMimi Zohar *decrypted_datalen = strsep(&datablob, " \t");
22661cf45d0SMimi Zohar if (!*decrypted_datalen) {
22761cf45d0SMimi Zohar pr_info("encrypted_key: keylen parameter is missing\n");
22861cf45d0SMimi Zohar goto out;
22961cf45d0SMimi Zohar }
23061cf45d0SMimi Zohar }
23161cf45d0SMimi Zohar
23261cf45d0SMimi Zohar switch (key_cmd) {
23361cf45d0SMimi Zohar case Opt_new:
23461cf45d0SMimi Zohar if (!decrypted_datalen) {
23561cf45d0SMimi Zohar pr_info("encrypted_key: keyword \'%s\' not allowed "
23661cf45d0SMimi Zohar "when called from .update method\n", keyword);
23761cf45d0SMimi Zohar break;
23861cf45d0SMimi Zohar }
239cd3bc044SYael Tzur *decrypted_data = strsep(&datablob, " \t");
24061cf45d0SMimi Zohar ret = 0;
24161cf45d0SMimi Zohar break;
24261cf45d0SMimi Zohar case Opt_load:
24361cf45d0SMimi Zohar if (!decrypted_datalen) {
24461cf45d0SMimi Zohar pr_info("encrypted_key: keyword \'%s\' not allowed "
24561cf45d0SMimi Zohar "when called from .update method\n", keyword);
24661cf45d0SMimi Zohar break;
24761cf45d0SMimi Zohar }
24861cf45d0SMimi Zohar *hex_encoded_iv = strsep(&datablob, " \t");
24961cf45d0SMimi Zohar if (!*hex_encoded_iv) {
25061cf45d0SMimi Zohar pr_info("encrypted_key: hex blob is missing\n");
25161cf45d0SMimi Zohar break;
25261cf45d0SMimi Zohar }
25361cf45d0SMimi Zohar ret = 0;
25461cf45d0SMimi Zohar break;
25561cf45d0SMimi Zohar case Opt_update:
25661cf45d0SMimi Zohar if (decrypted_datalen) {
25761cf45d0SMimi Zohar pr_info("encrypted_key: keyword \'%s\' not allowed "
25861cf45d0SMimi Zohar "when called from .instantiate method\n",
25961cf45d0SMimi Zohar keyword);
26061cf45d0SMimi Zohar break;
26161cf45d0SMimi Zohar }
26261cf45d0SMimi Zohar ret = 0;
26361cf45d0SMimi Zohar break;
26461cf45d0SMimi Zohar case Opt_err:
26561cf45d0SMimi Zohar pr_info("encrypted_key: keyword \'%s\' not recognized\n",
26661cf45d0SMimi Zohar keyword);
26761cf45d0SMimi Zohar break;
26861cf45d0SMimi Zohar }
26961cf45d0SMimi Zohar out:
27061cf45d0SMimi Zohar return ret;
27161cf45d0SMimi Zohar }
27261cf45d0SMimi Zohar
27361cf45d0SMimi Zohar /*
27461cf45d0SMimi Zohar * datablob_format - format as an ascii string, before copying to userspace
27561cf45d0SMimi Zohar */
datablob_format(struct encrypted_key_payload * epayload,size_t asciiblob_len)27661cf45d0SMimi Zohar static char *datablob_format(struct encrypted_key_payload *epayload,
27761cf45d0SMimi Zohar size_t asciiblob_len)
27861cf45d0SMimi Zohar {
27961cf45d0SMimi Zohar char *ascii_buf, *bufp;
28061cf45d0SMimi Zohar u8 *iv = epayload->iv;
28161cf45d0SMimi Zohar int len;
28261cf45d0SMimi Zohar int i;
28361cf45d0SMimi Zohar
28461cf45d0SMimi Zohar ascii_buf = kmalloc(asciiblob_len + 1, GFP_KERNEL);
28561cf45d0SMimi Zohar if (!ascii_buf)
28661cf45d0SMimi Zohar goto out;
28761cf45d0SMimi Zohar
28861cf45d0SMimi Zohar ascii_buf[asciiblob_len] = '\0';
28961cf45d0SMimi Zohar
29061cf45d0SMimi Zohar /* copy datablob master_desc and datalen strings */
29161cf45d0SMimi Zohar len = sprintf(ascii_buf, "%s %s %s ", epayload->format,
29261cf45d0SMimi Zohar epayload->master_desc, epayload->datalen);
29361cf45d0SMimi Zohar
29461cf45d0SMimi Zohar /* convert the hex encoded iv, encrypted-data and HMAC to ascii */
29561cf45d0SMimi Zohar bufp = &ascii_buf[len];
29661cf45d0SMimi Zohar for (i = 0; i < (asciiblob_len - len) / 2; i++)
29702473119SAndy Shevchenko bufp = hex_byte_pack(bufp, iv[i]);
29861cf45d0SMimi Zohar out:
29961cf45d0SMimi Zohar return ascii_buf;
30061cf45d0SMimi Zohar }
30161cf45d0SMimi Zohar
30261cf45d0SMimi Zohar /*
30361cf45d0SMimi Zohar * request_user_key - request the user key
30461cf45d0SMimi Zohar *
30561cf45d0SMimi Zohar * Use a user provided key to encrypt/decrypt an encrypted-key.
30661cf45d0SMimi Zohar */
request_user_key(const char * master_desc,const u8 ** master_key,size_t * master_keylen)307146aa8b1SDavid Howells static struct key *request_user_key(const char *master_desc, const u8 **master_key,
30861cf45d0SMimi Zohar size_t *master_keylen)
30961cf45d0SMimi Zohar {
310146aa8b1SDavid Howells const struct user_key_payload *upayload;
31161cf45d0SMimi Zohar struct key *ukey;
31261cf45d0SMimi Zohar
313028db3e2SLinus Torvalds ukey = request_key(&key_type_user, master_desc, NULL);
31461cf45d0SMimi Zohar if (IS_ERR(ukey))
31561cf45d0SMimi Zohar goto error;
31661cf45d0SMimi Zohar
31761cf45d0SMimi Zohar down_read(&ukey->sem);
3180837e49aSDavid Howells upayload = user_key_payload_locked(ukey);
31913923d08SEric Biggers if (!upayload) {
32013923d08SEric Biggers /* key was revoked before we acquired its semaphore */
32113923d08SEric Biggers up_read(&ukey->sem);
32213923d08SEric Biggers key_put(ukey);
32313923d08SEric Biggers ukey = ERR_PTR(-EKEYREVOKED);
32413923d08SEric Biggers goto error;
32513923d08SEric Biggers }
32661cf45d0SMimi Zohar *master_key = upayload->data;
32761cf45d0SMimi Zohar *master_keylen = upayload->datalen;
32861cf45d0SMimi Zohar error:
32961cf45d0SMimi Zohar return ukey;
33061cf45d0SMimi Zohar }
33161cf45d0SMimi Zohar
calc_hmac(u8 * digest,const u8 * key,unsigned int keylen,const u8 * buf,unsigned int buflen)33261cf45d0SMimi Zohar static int calc_hmac(u8 *digest, const u8 *key, unsigned int keylen,
33361cf45d0SMimi Zohar const u8 *buf, unsigned int buflen)
33461cf45d0SMimi Zohar {
33564d107d3SEric Biggers struct crypto_shash *tfm;
33664d107d3SEric Biggers int err;
33761cf45d0SMimi Zohar
3383d234b33SEric Biggers tfm = crypto_alloc_shash(hmac_alg, 0, 0);
33964d107d3SEric Biggers if (IS_ERR(tfm)) {
34064d107d3SEric Biggers pr_err("encrypted_key: can't alloc %s transform: %ld\n",
34164d107d3SEric Biggers hmac_alg, PTR_ERR(tfm));
34264d107d3SEric Biggers return PTR_ERR(tfm);
34361cf45d0SMimi Zohar }
34461cf45d0SMimi Zohar
34564d107d3SEric Biggers err = crypto_shash_setkey(tfm, key, keylen);
34664d107d3SEric Biggers if (!err)
347bce395eeSEric Biggers err = crypto_shash_tfm_digest(tfm, buf, buflen, digest);
34864d107d3SEric Biggers crypto_free_shash(tfm);
34964d107d3SEric Biggers return err;
35061cf45d0SMimi Zohar }
35161cf45d0SMimi Zohar
35261cf45d0SMimi Zohar enum derived_key_type { ENC_KEY, AUTH_KEY };
35361cf45d0SMimi Zohar
35461cf45d0SMimi Zohar /* Derive authentication/encryption key from trusted key */
get_derived_key(u8 * derived_key,enum derived_key_type key_type,const u8 * master_key,size_t master_keylen)35561cf45d0SMimi Zohar static int get_derived_key(u8 *derived_key, enum derived_key_type key_type,
35661cf45d0SMimi Zohar const u8 *master_key, size_t master_keylen)
35761cf45d0SMimi Zohar {
35861cf45d0SMimi Zohar u8 *derived_buf;
35961cf45d0SMimi Zohar unsigned int derived_buf_len;
36061cf45d0SMimi Zohar int ret;
36161cf45d0SMimi Zohar
36261cf45d0SMimi Zohar derived_buf_len = strlen("AUTH_KEY") + 1 + master_keylen;
36361cf45d0SMimi Zohar if (derived_buf_len < HASH_SIZE)
36461cf45d0SMimi Zohar derived_buf_len = HASH_SIZE;
36561cf45d0SMimi Zohar
36661cf45d0SMimi Zohar derived_buf = kzalloc(derived_buf_len, GFP_KERNEL);
36741f1c53eSMarkus Elfring if (!derived_buf)
36861cf45d0SMimi Zohar return -ENOMEM;
36941f1c53eSMarkus Elfring
37061cf45d0SMimi Zohar if (key_type)
37161cf45d0SMimi Zohar strcpy(derived_buf, "AUTH_KEY");
37261cf45d0SMimi Zohar else
37361cf45d0SMimi Zohar strcpy(derived_buf, "ENC_KEY");
37461cf45d0SMimi Zohar
37561cf45d0SMimi Zohar memcpy(derived_buf + strlen(derived_buf) + 1, master_key,
37661cf45d0SMimi Zohar master_keylen);
377bce395eeSEric Biggers ret = crypto_shash_tfm_digest(hash_tfm, derived_buf, derived_buf_len,
378bce395eeSEric Biggers derived_key);
379453431a5SWaiman Long kfree_sensitive(derived_buf);
38061cf45d0SMimi Zohar return ret;
38161cf45d0SMimi Zohar }
38261cf45d0SMimi Zohar
init_skcipher_req(const u8 * key,unsigned int key_len)383c3917fd9SHerbert Xu static struct skcipher_request *init_skcipher_req(const u8 *key,
384c3917fd9SHerbert Xu unsigned int key_len)
38561cf45d0SMimi Zohar {
386c3917fd9SHerbert Xu struct skcipher_request *req;
387c3917fd9SHerbert Xu struct crypto_skcipher *tfm;
38861cf45d0SMimi Zohar int ret;
38961cf45d0SMimi Zohar
390c3917fd9SHerbert Xu tfm = crypto_alloc_skcipher(blkcipher_alg, 0, CRYPTO_ALG_ASYNC);
391c3917fd9SHerbert Xu if (IS_ERR(tfm)) {
39261cf45d0SMimi Zohar pr_err("encrypted_key: failed to load %s transform (%ld)\n",
393c3917fd9SHerbert Xu blkcipher_alg, PTR_ERR(tfm));
394c3917fd9SHerbert Xu return ERR_CAST(tfm);
39561cf45d0SMimi Zohar }
39661cf45d0SMimi Zohar
397c3917fd9SHerbert Xu ret = crypto_skcipher_setkey(tfm, key, key_len);
39861cf45d0SMimi Zohar if (ret < 0) {
39961cf45d0SMimi Zohar pr_err("encrypted_key: failed to setkey (%d)\n", ret);
400c3917fd9SHerbert Xu crypto_free_skcipher(tfm);
401c3917fd9SHerbert Xu return ERR_PTR(ret);
40261cf45d0SMimi Zohar }
403c3917fd9SHerbert Xu
404c3917fd9SHerbert Xu req = skcipher_request_alloc(tfm, GFP_KERNEL);
405c3917fd9SHerbert Xu if (!req) {
406c3917fd9SHerbert Xu pr_err("encrypted_key: failed to allocate request for %s\n",
407c3917fd9SHerbert Xu blkcipher_alg);
408c3917fd9SHerbert Xu crypto_free_skcipher(tfm);
409c3917fd9SHerbert Xu return ERR_PTR(-ENOMEM);
410c3917fd9SHerbert Xu }
411c3917fd9SHerbert Xu
412c3917fd9SHerbert Xu skcipher_request_set_callback(req, 0, NULL, NULL);
413c3917fd9SHerbert Xu return req;
41461cf45d0SMimi Zohar }
41561cf45d0SMimi Zohar
request_master_key(struct encrypted_key_payload * epayload,const u8 ** master_key,size_t * master_keylen)41661cf45d0SMimi Zohar static struct key *request_master_key(struct encrypted_key_payload *epayload,
417146aa8b1SDavid Howells const u8 **master_key, size_t *master_keylen)
41861cf45d0SMimi Zohar {
41957cb17e7SDan Carpenter struct key *mkey = ERR_PTR(-EINVAL);
42061cf45d0SMimi Zohar
42161cf45d0SMimi Zohar if (!strncmp(epayload->master_desc, KEY_TRUSTED_PREFIX,
42261cf45d0SMimi Zohar KEY_TRUSTED_PREFIX_LEN)) {
42361cf45d0SMimi Zohar mkey = request_trusted_key(epayload->master_desc +
42461cf45d0SMimi Zohar KEY_TRUSTED_PREFIX_LEN,
42561cf45d0SMimi Zohar master_key, master_keylen);
42661cf45d0SMimi Zohar } else if (!strncmp(epayload->master_desc, KEY_USER_PREFIX,
42761cf45d0SMimi Zohar KEY_USER_PREFIX_LEN)) {
42861cf45d0SMimi Zohar mkey = request_user_key(epayload->master_desc +
42961cf45d0SMimi Zohar KEY_USER_PREFIX_LEN,
43061cf45d0SMimi Zohar master_key, master_keylen);
43161cf45d0SMimi Zohar } else
43261cf45d0SMimi Zohar goto out;
43361cf45d0SMimi Zohar
43461cf45d0SMimi Zohar if (IS_ERR(mkey)) {
435f4a0d5abSMimi Zohar int ret = PTR_ERR(mkey);
436982e617aSMimi Zohar
437982e617aSMimi Zohar if (ret == -ENOTSUPP)
438982e617aSMimi Zohar pr_info("encrypted_key: key %s not supported",
439982e617aSMimi Zohar epayload->master_desc);
440982e617aSMimi Zohar else
44161cf45d0SMimi Zohar pr_info("encrypted_key: key %s not found",
44261cf45d0SMimi Zohar epayload->master_desc);
44361cf45d0SMimi Zohar goto out;
44461cf45d0SMimi Zohar }
44561cf45d0SMimi Zohar
44661cf45d0SMimi Zohar dump_master_key(*master_key, *master_keylen);
44761cf45d0SMimi Zohar out:
44861cf45d0SMimi Zohar return mkey;
44961cf45d0SMimi Zohar }
45061cf45d0SMimi Zohar
45161cf45d0SMimi Zohar /* Before returning data to userspace, encrypt decrypted data. */
derived_key_encrypt(struct encrypted_key_payload * epayload,const u8 * derived_key,unsigned int derived_keylen)45261cf45d0SMimi Zohar static int derived_key_encrypt(struct encrypted_key_payload *epayload,
45361cf45d0SMimi Zohar const u8 *derived_key,
45461cf45d0SMimi Zohar unsigned int derived_keylen)
45561cf45d0SMimi Zohar {
45661cf45d0SMimi Zohar struct scatterlist sg_in[2];
45761cf45d0SMimi Zohar struct scatterlist sg_out[1];
458c3917fd9SHerbert Xu struct crypto_skcipher *tfm;
459c3917fd9SHerbert Xu struct skcipher_request *req;
46061cf45d0SMimi Zohar unsigned int encrypted_datalen;
461456bee98SHerbert Xu u8 iv[AES_BLOCK_SIZE];
46261cf45d0SMimi Zohar int ret;
46361cf45d0SMimi Zohar
46461cf45d0SMimi Zohar encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
46561cf45d0SMimi Zohar
466c3917fd9SHerbert Xu req = init_skcipher_req(derived_key, derived_keylen);
467c3917fd9SHerbert Xu ret = PTR_ERR(req);
468c3917fd9SHerbert Xu if (IS_ERR(req))
46961cf45d0SMimi Zohar goto out;
47061cf45d0SMimi Zohar dump_decrypted_data(epayload);
47161cf45d0SMimi Zohar
47261cf45d0SMimi Zohar sg_init_table(sg_in, 2);
47361cf45d0SMimi Zohar sg_set_buf(&sg_in[0], epayload->decrypted_data,
47461cf45d0SMimi Zohar epayload->decrypted_datalen);
475e9ff56acSEric Biggers sg_set_page(&sg_in[1], ZERO_PAGE(0), AES_BLOCK_SIZE, 0);
47661cf45d0SMimi Zohar
47761cf45d0SMimi Zohar sg_init_table(sg_out, 1);
47861cf45d0SMimi Zohar sg_set_buf(sg_out, epayload->encrypted_data, encrypted_datalen);
47961cf45d0SMimi Zohar
480456bee98SHerbert Xu memcpy(iv, epayload->iv, sizeof(iv));
481456bee98SHerbert Xu skcipher_request_set_crypt(req, sg_in, sg_out, encrypted_datalen, iv);
482c3917fd9SHerbert Xu ret = crypto_skcipher_encrypt(req);
483c3917fd9SHerbert Xu tfm = crypto_skcipher_reqtfm(req);
484c3917fd9SHerbert Xu skcipher_request_free(req);
485c3917fd9SHerbert Xu crypto_free_skcipher(tfm);
48661cf45d0SMimi Zohar if (ret < 0)
48761cf45d0SMimi Zohar pr_err("encrypted_key: failed to encrypt (%d)\n", ret);
48861cf45d0SMimi Zohar else
48961cf45d0SMimi Zohar dump_encrypted_data(epayload, encrypted_datalen);
49061cf45d0SMimi Zohar out:
49161cf45d0SMimi Zohar return ret;
49261cf45d0SMimi Zohar }
49361cf45d0SMimi Zohar
datablob_hmac_append(struct encrypted_key_payload * epayload,const u8 * master_key,size_t master_keylen)49461cf45d0SMimi Zohar static int datablob_hmac_append(struct encrypted_key_payload *epayload,
49561cf45d0SMimi Zohar const u8 *master_key, size_t master_keylen)
49661cf45d0SMimi Zohar {
49761cf45d0SMimi Zohar u8 derived_key[HASH_SIZE];
49861cf45d0SMimi Zohar u8 *digest;
49961cf45d0SMimi Zohar int ret;
50061cf45d0SMimi Zohar
50161cf45d0SMimi Zohar ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen);
50261cf45d0SMimi Zohar if (ret < 0)
50361cf45d0SMimi Zohar goto out;
50461cf45d0SMimi Zohar
50561cf45d0SMimi Zohar digest = epayload->format + epayload->datablob_len;
50661cf45d0SMimi Zohar ret = calc_hmac(digest, derived_key, sizeof derived_key,
50761cf45d0SMimi Zohar epayload->format, epayload->datablob_len);
50861cf45d0SMimi Zohar if (!ret)
50961cf45d0SMimi Zohar dump_hmac(NULL, digest, HASH_SIZE);
51061cf45d0SMimi Zohar out:
511a9dd74b2SEric Biggers memzero_explicit(derived_key, sizeof(derived_key));
51261cf45d0SMimi Zohar return ret;
51361cf45d0SMimi Zohar }
51461cf45d0SMimi Zohar
51561cf45d0SMimi Zohar /* verify HMAC before decrypting encrypted key */
datablob_hmac_verify(struct encrypted_key_payload * epayload,const u8 * format,const u8 * master_key,size_t master_keylen)51661cf45d0SMimi Zohar static int datablob_hmac_verify(struct encrypted_key_payload *epayload,
51761cf45d0SMimi Zohar const u8 *format, const u8 *master_key,
51861cf45d0SMimi Zohar size_t master_keylen)
51961cf45d0SMimi Zohar {
52061cf45d0SMimi Zohar u8 derived_key[HASH_SIZE];
52161cf45d0SMimi Zohar u8 digest[HASH_SIZE];
52261cf45d0SMimi Zohar int ret;
52361cf45d0SMimi Zohar char *p;
52461cf45d0SMimi Zohar unsigned short len;
52561cf45d0SMimi Zohar
52661cf45d0SMimi Zohar ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen);
52761cf45d0SMimi Zohar if (ret < 0)
52861cf45d0SMimi Zohar goto out;
52961cf45d0SMimi Zohar
53061cf45d0SMimi Zohar len = epayload->datablob_len;
53161cf45d0SMimi Zohar if (!format) {
53261cf45d0SMimi Zohar p = epayload->master_desc;
53361cf45d0SMimi Zohar len -= strlen(epayload->format) + 1;
53461cf45d0SMimi Zohar } else
53561cf45d0SMimi Zohar p = epayload->format;
53661cf45d0SMimi Zohar
53761cf45d0SMimi Zohar ret = calc_hmac(digest, derived_key, sizeof derived_key, p, len);
53861cf45d0SMimi Zohar if (ret < 0)
53961cf45d0SMimi Zohar goto out;
5400f534e4aSEric Biggers ret = crypto_memneq(digest, epayload->format + epayload->datablob_len,
5410f534e4aSEric Biggers sizeof(digest));
54261cf45d0SMimi Zohar if (ret) {
54361cf45d0SMimi Zohar ret = -EINVAL;
54461cf45d0SMimi Zohar dump_hmac("datablob",
54561cf45d0SMimi Zohar epayload->format + epayload->datablob_len,
54661cf45d0SMimi Zohar HASH_SIZE);
54761cf45d0SMimi Zohar dump_hmac("calc", digest, HASH_SIZE);
54861cf45d0SMimi Zohar }
54961cf45d0SMimi Zohar out:
550a9dd74b2SEric Biggers memzero_explicit(derived_key, sizeof(derived_key));
55161cf45d0SMimi Zohar return ret;
55261cf45d0SMimi Zohar }
55361cf45d0SMimi Zohar
derived_key_decrypt(struct encrypted_key_payload * epayload,const u8 * derived_key,unsigned int derived_keylen)55461cf45d0SMimi Zohar static int derived_key_decrypt(struct encrypted_key_payload *epayload,
55561cf45d0SMimi Zohar const u8 *derived_key,
55661cf45d0SMimi Zohar unsigned int derived_keylen)
55761cf45d0SMimi Zohar {
55861cf45d0SMimi Zohar struct scatterlist sg_in[1];
55961cf45d0SMimi Zohar struct scatterlist sg_out[2];
560c3917fd9SHerbert Xu struct crypto_skcipher *tfm;
561c3917fd9SHerbert Xu struct skcipher_request *req;
56261cf45d0SMimi Zohar unsigned int encrypted_datalen;
563456bee98SHerbert Xu u8 iv[AES_BLOCK_SIZE];
564e9ff56acSEric Biggers u8 *pad;
56561cf45d0SMimi Zohar int ret;
56661cf45d0SMimi Zohar
567e9ff56acSEric Biggers /* Throwaway buffer to hold the unused zero padding at the end */
568e9ff56acSEric Biggers pad = kmalloc(AES_BLOCK_SIZE, GFP_KERNEL);
569e9ff56acSEric Biggers if (!pad)
570e9ff56acSEric Biggers return -ENOMEM;
571e9ff56acSEric Biggers
57261cf45d0SMimi Zohar encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
573c3917fd9SHerbert Xu req = init_skcipher_req(derived_key, derived_keylen);
574c3917fd9SHerbert Xu ret = PTR_ERR(req);
575c3917fd9SHerbert Xu if (IS_ERR(req))
57661cf45d0SMimi Zohar goto out;
57761cf45d0SMimi Zohar dump_encrypted_data(epayload, encrypted_datalen);
57861cf45d0SMimi Zohar
57961cf45d0SMimi Zohar sg_init_table(sg_in, 1);
58061cf45d0SMimi Zohar sg_init_table(sg_out, 2);
58161cf45d0SMimi Zohar sg_set_buf(sg_in, epayload->encrypted_data, encrypted_datalen);
58261cf45d0SMimi Zohar sg_set_buf(&sg_out[0], epayload->decrypted_data,
58361cf45d0SMimi Zohar epayload->decrypted_datalen);
584e9ff56acSEric Biggers sg_set_buf(&sg_out[1], pad, AES_BLOCK_SIZE);
58561cf45d0SMimi Zohar
586456bee98SHerbert Xu memcpy(iv, epayload->iv, sizeof(iv));
587456bee98SHerbert Xu skcipher_request_set_crypt(req, sg_in, sg_out, encrypted_datalen, iv);
588c3917fd9SHerbert Xu ret = crypto_skcipher_decrypt(req);
589c3917fd9SHerbert Xu tfm = crypto_skcipher_reqtfm(req);
590c3917fd9SHerbert Xu skcipher_request_free(req);
591c3917fd9SHerbert Xu crypto_free_skcipher(tfm);
59261cf45d0SMimi Zohar if (ret < 0)
59361cf45d0SMimi Zohar goto out;
59461cf45d0SMimi Zohar dump_decrypted_data(epayload);
59561cf45d0SMimi Zohar out:
596e9ff56acSEric Biggers kfree(pad);
59761cf45d0SMimi Zohar return ret;
59861cf45d0SMimi Zohar }
59961cf45d0SMimi Zohar
60061cf45d0SMimi Zohar /* Allocate memory for decrypted key and datablob. */
encrypted_key_alloc(struct key * key,const char * format,const char * master_desc,const char * datalen,const char * decrypted_data)60161cf45d0SMimi Zohar static struct encrypted_key_payload *encrypted_key_alloc(struct key *key,
60261cf45d0SMimi Zohar const char *format,
60361cf45d0SMimi Zohar const char *master_desc,
604cd3bc044SYael Tzur const char *datalen,
605cd3bc044SYael Tzur const char *decrypted_data)
60661cf45d0SMimi Zohar {
60761cf45d0SMimi Zohar struct encrypted_key_payload *epayload = NULL;
60861cf45d0SMimi Zohar unsigned short datablob_len;
60961cf45d0SMimi Zohar unsigned short decrypted_datalen;
61061cf45d0SMimi Zohar unsigned short payload_datalen;
61161cf45d0SMimi Zohar unsigned int encrypted_datalen;
61261cf45d0SMimi Zohar unsigned int format_len;
61361cf45d0SMimi Zohar long dlen;
614cd3bc044SYael Tzur int i;
61561cf45d0SMimi Zohar int ret;
61661cf45d0SMimi Zohar
61729707b20SJingoo Han ret = kstrtol(datalen, 10, &dlen);
61861cf45d0SMimi Zohar if (ret < 0 || dlen < MIN_DATA_SIZE || dlen > MAX_DATA_SIZE)
61961cf45d0SMimi Zohar return ERR_PTR(-EINVAL);
62061cf45d0SMimi Zohar
62161cf45d0SMimi Zohar format_len = (!format) ? strlen(key_format_default) : strlen(format);
62261cf45d0SMimi Zohar decrypted_datalen = dlen;
62361cf45d0SMimi Zohar payload_datalen = decrypted_datalen;
624cd3bc044SYael Tzur
625cd3bc044SYael Tzur if (decrypted_data) {
626cd3bc044SYael Tzur if (!user_decrypted_data) {
627cd3bc044SYael Tzur pr_err("encrypted key: instantiation of keys using provided decrypted data is disabled since CONFIG_USER_DECRYPTED_DATA is set to false\n");
628cd3bc044SYael Tzur return ERR_PTR(-EINVAL);
629cd3bc044SYael Tzur }
6305adedd42SNikolaus Voss if (strlen(decrypted_data) != decrypted_datalen * 2) {
631cd3bc044SYael Tzur pr_err("encrypted key: decrypted data provided does not match decrypted data length provided\n");
632cd3bc044SYael Tzur return ERR_PTR(-EINVAL);
633cd3bc044SYael Tzur }
634cd3bc044SYael Tzur for (i = 0; i < strlen(decrypted_data); i++) {
635cd3bc044SYael Tzur if (!isxdigit(decrypted_data[i])) {
636cd3bc044SYael Tzur pr_err("encrypted key: decrypted data provided must contain only hexadecimal characters\n");
637cd3bc044SYael Tzur return ERR_PTR(-EINVAL);
638cd3bc044SYael Tzur }
639cd3bc044SYael Tzur }
640cd3bc044SYael Tzur }
641cd3bc044SYael Tzur
6429db67581SDave Jiang if (format) {
6439db67581SDave Jiang if (!strcmp(format, key_format_ecryptfs)) {
64461cf45d0SMimi Zohar if (dlen != ECRYPTFS_MAX_KEY_BYTES) {
6459db67581SDave Jiang pr_err("encrypted_key: keylen for the ecryptfs format must be equal to %d bytes\n",
64661cf45d0SMimi Zohar ECRYPTFS_MAX_KEY_BYTES);
64761cf45d0SMimi Zohar return ERR_PTR(-EINVAL);
64861cf45d0SMimi Zohar }
64961cf45d0SMimi Zohar decrypted_datalen = ECRYPTFS_MAX_KEY_BYTES;
65061cf45d0SMimi Zohar payload_datalen = sizeof(struct ecryptfs_auth_tok);
6519db67581SDave Jiang } else if (!strcmp(format, key_format_enc32)) {
6529db67581SDave Jiang if (decrypted_datalen != KEY_ENC32_PAYLOAD_LEN) {
6539db67581SDave Jiang pr_err("encrypted_key: enc32 key payload incorrect length: %d\n",
6549db67581SDave Jiang decrypted_datalen);
6559db67581SDave Jiang return ERR_PTR(-EINVAL);
6569db67581SDave Jiang }
6579db67581SDave Jiang }
65861cf45d0SMimi Zohar }
65961cf45d0SMimi Zohar
66061cf45d0SMimi Zohar encrypted_datalen = roundup(decrypted_datalen, blksize);
66161cf45d0SMimi Zohar
66261cf45d0SMimi Zohar datablob_len = format_len + 1 + strlen(master_desc) + 1
66361cf45d0SMimi Zohar + strlen(datalen) + 1 + ivsize + 1 + encrypted_datalen;
66461cf45d0SMimi Zohar
66561cf45d0SMimi Zohar ret = key_payload_reserve(key, payload_datalen + datablob_len
66661cf45d0SMimi Zohar + HASH_SIZE + 1);
66761cf45d0SMimi Zohar if (ret < 0)
66861cf45d0SMimi Zohar return ERR_PTR(ret);
66961cf45d0SMimi Zohar
67061cf45d0SMimi Zohar epayload = kzalloc(sizeof(*epayload) + payload_datalen +
67161cf45d0SMimi Zohar datablob_len + HASH_SIZE + 1, GFP_KERNEL);
67261cf45d0SMimi Zohar if (!epayload)
67361cf45d0SMimi Zohar return ERR_PTR(-ENOMEM);
67461cf45d0SMimi Zohar
67561cf45d0SMimi Zohar epayload->payload_datalen = payload_datalen;
67661cf45d0SMimi Zohar epayload->decrypted_datalen = decrypted_datalen;
67761cf45d0SMimi Zohar epayload->datablob_len = datablob_len;
67861cf45d0SMimi Zohar return epayload;
67961cf45d0SMimi Zohar }
68061cf45d0SMimi Zohar
encrypted_key_decrypt(struct encrypted_key_payload * epayload,const char * format,const char * hex_encoded_iv)68161cf45d0SMimi Zohar static int encrypted_key_decrypt(struct encrypted_key_payload *epayload,
68261cf45d0SMimi Zohar const char *format, const char *hex_encoded_iv)
68361cf45d0SMimi Zohar {
68461cf45d0SMimi Zohar struct key *mkey;
68561cf45d0SMimi Zohar u8 derived_key[HASH_SIZE];
686146aa8b1SDavid Howells const u8 *master_key;
68761cf45d0SMimi Zohar u8 *hmac;
68861cf45d0SMimi Zohar const char *hex_encoded_data;
68961cf45d0SMimi Zohar unsigned int encrypted_datalen;
69061cf45d0SMimi Zohar size_t master_keylen;
69161cf45d0SMimi Zohar size_t asciilen;
69261cf45d0SMimi Zohar int ret;
69361cf45d0SMimi Zohar
69461cf45d0SMimi Zohar encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
69561cf45d0SMimi Zohar asciilen = (ivsize + 1 + encrypted_datalen + HASH_SIZE) * 2;
69661cf45d0SMimi Zohar if (strlen(hex_encoded_iv) != asciilen)
69761cf45d0SMimi Zohar return -EINVAL;
69861cf45d0SMimi Zohar
69961cf45d0SMimi Zohar hex_encoded_data = hex_encoded_iv + (2 * ivsize) + 2;
7002b3ff631SMimi Zohar ret = hex2bin(epayload->iv, hex_encoded_iv, ivsize);
7012b3ff631SMimi Zohar if (ret < 0)
7022b3ff631SMimi Zohar return -EINVAL;
7032b3ff631SMimi Zohar ret = hex2bin(epayload->encrypted_data, hex_encoded_data,
7042b3ff631SMimi Zohar encrypted_datalen);
7052b3ff631SMimi Zohar if (ret < 0)
7062b3ff631SMimi Zohar return -EINVAL;
70761cf45d0SMimi Zohar
70861cf45d0SMimi Zohar hmac = epayload->format + epayload->datablob_len;
7092b3ff631SMimi Zohar ret = hex2bin(hmac, hex_encoded_data + (encrypted_datalen * 2),
7102b3ff631SMimi Zohar HASH_SIZE);
7112b3ff631SMimi Zohar if (ret < 0)
7122b3ff631SMimi Zohar return -EINVAL;
71361cf45d0SMimi Zohar
71461cf45d0SMimi Zohar mkey = request_master_key(epayload, &master_key, &master_keylen);
71561cf45d0SMimi Zohar if (IS_ERR(mkey))
71661cf45d0SMimi Zohar return PTR_ERR(mkey);
71761cf45d0SMimi Zohar
71861cf45d0SMimi Zohar ret = datablob_hmac_verify(epayload, format, master_key, master_keylen);
71961cf45d0SMimi Zohar if (ret < 0) {
72061cf45d0SMimi Zohar pr_err("encrypted_key: bad hmac (%d)\n", ret);
72161cf45d0SMimi Zohar goto out;
72261cf45d0SMimi Zohar }
72361cf45d0SMimi Zohar
72461cf45d0SMimi Zohar ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen);
72561cf45d0SMimi Zohar if (ret < 0)
72661cf45d0SMimi Zohar goto out;
72761cf45d0SMimi Zohar
72861cf45d0SMimi Zohar ret = derived_key_decrypt(epayload, derived_key, sizeof derived_key);
72961cf45d0SMimi Zohar if (ret < 0)
73061cf45d0SMimi Zohar pr_err("encrypted_key: failed to decrypt key (%d)\n", ret);
73161cf45d0SMimi Zohar out:
73261cf45d0SMimi Zohar up_read(&mkey->sem);
73361cf45d0SMimi Zohar key_put(mkey);
734a9dd74b2SEric Biggers memzero_explicit(derived_key, sizeof(derived_key));
73561cf45d0SMimi Zohar return ret;
73661cf45d0SMimi Zohar }
73761cf45d0SMimi Zohar
__ekey_init(struct encrypted_key_payload * epayload,const char * format,const char * master_desc,const char * datalen)73861cf45d0SMimi Zohar static void __ekey_init(struct encrypted_key_payload *epayload,
73961cf45d0SMimi Zohar const char *format, const char *master_desc,
74061cf45d0SMimi Zohar const char *datalen)
74161cf45d0SMimi Zohar {
74261cf45d0SMimi Zohar unsigned int format_len;
74361cf45d0SMimi Zohar
74461cf45d0SMimi Zohar format_len = (!format) ? strlen(key_format_default) : strlen(format);
74561cf45d0SMimi Zohar epayload->format = epayload->payload_data + epayload->payload_datalen;
74661cf45d0SMimi Zohar epayload->master_desc = epayload->format + format_len + 1;
74761cf45d0SMimi Zohar epayload->datalen = epayload->master_desc + strlen(master_desc) + 1;
74861cf45d0SMimi Zohar epayload->iv = epayload->datalen + strlen(datalen) + 1;
74961cf45d0SMimi Zohar epayload->encrypted_data = epayload->iv + ivsize + 1;
75061cf45d0SMimi Zohar epayload->decrypted_data = epayload->payload_data;
75161cf45d0SMimi Zohar
75261cf45d0SMimi Zohar if (!format)
75361cf45d0SMimi Zohar memcpy(epayload->format, key_format_default, format_len);
75461cf45d0SMimi Zohar else {
75561cf45d0SMimi Zohar if (!strcmp(format, key_format_ecryptfs))
75661cf45d0SMimi Zohar epayload->decrypted_data =
75761cf45d0SMimi Zohar ecryptfs_get_auth_tok_key((struct ecryptfs_auth_tok *)epayload->payload_data);
75861cf45d0SMimi Zohar
75961cf45d0SMimi Zohar memcpy(epayload->format, format, format_len);
76061cf45d0SMimi Zohar }
76161cf45d0SMimi Zohar
76261cf45d0SMimi Zohar memcpy(epayload->master_desc, master_desc, strlen(master_desc));
76361cf45d0SMimi Zohar memcpy(epayload->datalen, datalen, strlen(datalen));
76461cf45d0SMimi Zohar }
76561cf45d0SMimi Zohar
76661cf45d0SMimi Zohar /*
76761cf45d0SMimi Zohar * encrypted_init - initialize an encrypted key
76861cf45d0SMimi Zohar *
769cd3bc044SYael Tzur * For a new key, use either a random number or user-provided decrypted data in
770cd3bc044SYael Tzur * case it is provided. A random number is used for the iv in both cases. For
771cd3bc044SYael Tzur * an old key, decrypt the hex encoded data.
77261cf45d0SMimi Zohar */
encrypted_init(struct encrypted_key_payload * epayload,const char * key_desc,const char * format,const char * master_desc,const char * datalen,const char * hex_encoded_iv,const char * decrypted_data)77361cf45d0SMimi Zohar static int encrypted_init(struct encrypted_key_payload *epayload,
77461cf45d0SMimi Zohar const char *key_desc, const char *format,
77561cf45d0SMimi Zohar const char *master_desc, const char *datalen,
776cd3bc044SYael Tzur const char *hex_encoded_iv, const char *decrypted_data)
77761cf45d0SMimi Zohar {
77861cf45d0SMimi Zohar int ret = 0;
77961cf45d0SMimi Zohar
78061cf45d0SMimi Zohar if (format && !strcmp(format, key_format_ecryptfs)) {
78161cf45d0SMimi Zohar ret = valid_ecryptfs_desc(key_desc);
78261cf45d0SMimi Zohar if (ret < 0)
78361cf45d0SMimi Zohar return ret;
78461cf45d0SMimi Zohar
78561cf45d0SMimi Zohar ecryptfs_fill_auth_tok((struct ecryptfs_auth_tok *)epayload->payload_data,
78661cf45d0SMimi Zohar key_desc);
78761cf45d0SMimi Zohar }
78861cf45d0SMimi Zohar
78961cf45d0SMimi Zohar __ekey_init(epayload, format, master_desc, datalen);
790cd3bc044SYael Tzur if (hex_encoded_iv) {
79161cf45d0SMimi Zohar ret = encrypted_key_decrypt(epayload, format, hex_encoded_iv);
792cd3bc044SYael Tzur } else if (decrypted_data) {
793cd3bc044SYael Tzur get_random_bytes(epayload->iv, ivsize);
7945adedd42SNikolaus Voss ret = hex2bin(epayload->decrypted_data, decrypted_data,
795cd3bc044SYael Tzur epayload->decrypted_datalen);
796cd3bc044SYael Tzur } else {
797cd3bc044SYael Tzur get_random_bytes(epayload->iv, ivsize);
798cd3bc044SYael Tzur get_random_bytes(epayload->decrypted_data, epayload->decrypted_datalen);
799cd3bc044SYael Tzur }
80061cf45d0SMimi Zohar return ret;
80161cf45d0SMimi Zohar }
80261cf45d0SMimi Zohar
80361cf45d0SMimi Zohar /*
80461cf45d0SMimi Zohar * encrypted_instantiate - instantiate an encrypted key
80561cf45d0SMimi Zohar *
806cd3bc044SYael Tzur * Instantiates the key:
807cd3bc044SYael Tzur * - by decrypting an existing encrypted datablob, or
808cd3bc044SYael Tzur * - by creating a new encrypted key based on a kernel random number, or
809cd3bc044SYael Tzur * - using provided decrypted data.
81061cf45d0SMimi Zohar *
81161cf45d0SMimi Zohar * On success, return 0. Otherwise return errno.
81261cf45d0SMimi Zohar */
encrypted_instantiate(struct key * key,struct key_preparsed_payload * prep)813cf7f601cSDavid Howells static int encrypted_instantiate(struct key *key,
814cf7f601cSDavid Howells struct key_preparsed_payload *prep)
81561cf45d0SMimi Zohar {
81661cf45d0SMimi Zohar struct encrypted_key_payload *epayload = NULL;
81761cf45d0SMimi Zohar char *datablob = NULL;
81861cf45d0SMimi Zohar const char *format = NULL;
81961cf45d0SMimi Zohar char *master_desc = NULL;
82061cf45d0SMimi Zohar char *decrypted_datalen = NULL;
82161cf45d0SMimi Zohar char *hex_encoded_iv = NULL;
822cd3bc044SYael Tzur char *decrypted_data = NULL;
823cf7f601cSDavid Howells size_t datalen = prep->datalen;
82461cf45d0SMimi Zohar int ret;
82561cf45d0SMimi Zohar
826cf7f601cSDavid Howells if (datalen <= 0 || datalen > 32767 || !prep->data)
82761cf45d0SMimi Zohar return -EINVAL;
82861cf45d0SMimi Zohar
82961cf45d0SMimi Zohar datablob = kmalloc(datalen + 1, GFP_KERNEL);
83061cf45d0SMimi Zohar if (!datablob)
83161cf45d0SMimi Zohar return -ENOMEM;
83261cf45d0SMimi Zohar datablob[datalen] = 0;
833cf7f601cSDavid Howells memcpy(datablob, prep->data, datalen);
83461cf45d0SMimi Zohar ret = datablob_parse(datablob, &format, &master_desc,
835cd3bc044SYael Tzur &decrypted_datalen, &hex_encoded_iv, &decrypted_data);
83661cf45d0SMimi Zohar if (ret < 0)
83761cf45d0SMimi Zohar goto out;
83861cf45d0SMimi Zohar
83961cf45d0SMimi Zohar epayload = encrypted_key_alloc(key, format, master_desc,
840cd3bc044SYael Tzur decrypted_datalen, decrypted_data);
84161cf45d0SMimi Zohar if (IS_ERR(epayload)) {
84261cf45d0SMimi Zohar ret = PTR_ERR(epayload);
84361cf45d0SMimi Zohar goto out;
84461cf45d0SMimi Zohar }
84561cf45d0SMimi Zohar ret = encrypted_init(epayload, key->description, format, master_desc,
846cd3bc044SYael Tzur decrypted_datalen, hex_encoded_iv, decrypted_data);
84761cf45d0SMimi Zohar if (ret < 0) {
848453431a5SWaiman Long kfree_sensitive(epayload);
84961cf45d0SMimi Zohar goto out;
85061cf45d0SMimi Zohar }
85161cf45d0SMimi Zohar
852b64cc5fbSMimi Zohar rcu_assign_keypointer(key, epayload);
85361cf45d0SMimi Zohar out:
854453431a5SWaiman Long kfree_sensitive(datablob);
85561cf45d0SMimi Zohar return ret;
85661cf45d0SMimi Zohar }
85761cf45d0SMimi Zohar
encrypted_rcu_free(struct rcu_head * rcu)85861cf45d0SMimi Zohar static void encrypted_rcu_free(struct rcu_head *rcu)
85961cf45d0SMimi Zohar {
86061cf45d0SMimi Zohar struct encrypted_key_payload *epayload;
86161cf45d0SMimi Zohar
86261cf45d0SMimi Zohar epayload = container_of(rcu, struct encrypted_key_payload, rcu);
863453431a5SWaiman Long kfree_sensitive(epayload);
86461cf45d0SMimi Zohar }
86561cf45d0SMimi Zohar
86661cf45d0SMimi Zohar /*
86761cf45d0SMimi Zohar * encrypted_update - update the master key description
86861cf45d0SMimi Zohar *
86961cf45d0SMimi Zohar * Change the master key description for an existing encrypted key.
87061cf45d0SMimi Zohar * The next read will return an encrypted datablob using the new
87161cf45d0SMimi Zohar * master key description.
87261cf45d0SMimi Zohar *
87361cf45d0SMimi Zohar * On success, return 0. Otherwise return errno.
87461cf45d0SMimi Zohar */
encrypted_update(struct key * key,struct key_preparsed_payload * prep)875cf7f601cSDavid Howells static int encrypted_update(struct key *key, struct key_preparsed_payload *prep)
87661cf45d0SMimi Zohar {
877146aa8b1SDavid Howells struct encrypted_key_payload *epayload = key->payload.data[0];
87861cf45d0SMimi Zohar struct encrypted_key_payload *new_epayload;
87961cf45d0SMimi Zohar char *buf;
88061cf45d0SMimi Zohar char *new_master_desc = NULL;
88161cf45d0SMimi Zohar const char *format = NULL;
882cf7f601cSDavid Howells size_t datalen = prep->datalen;
88361cf45d0SMimi Zohar int ret = 0;
88461cf45d0SMimi Zohar
885363b02daSDavid Howells if (key_is_negative(key))
886096fe9eaSDavid Howells return -ENOKEY;
887cf7f601cSDavid Howells if (datalen <= 0 || datalen > 32767 || !prep->data)
88861cf45d0SMimi Zohar return -EINVAL;
88961cf45d0SMimi Zohar
89061cf45d0SMimi Zohar buf = kmalloc(datalen + 1, GFP_KERNEL);
89161cf45d0SMimi Zohar if (!buf)
89261cf45d0SMimi Zohar return -ENOMEM;
89361cf45d0SMimi Zohar
89461cf45d0SMimi Zohar buf[datalen] = 0;
895cf7f601cSDavid Howells memcpy(buf, prep->data, datalen);
896cd3bc044SYael Tzur ret = datablob_parse(buf, &format, &new_master_desc, NULL, NULL, NULL);
89761cf45d0SMimi Zohar if (ret < 0)
89861cf45d0SMimi Zohar goto out;
89961cf45d0SMimi Zohar
90061cf45d0SMimi Zohar ret = valid_master_desc(new_master_desc, epayload->master_desc);
90161cf45d0SMimi Zohar if (ret < 0)
90261cf45d0SMimi Zohar goto out;
90361cf45d0SMimi Zohar
90461cf45d0SMimi Zohar new_epayload = encrypted_key_alloc(key, epayload->format,
905cd3bc044SYael Tzur new_master_desc, epayload->datalen, NULL);
90661cf45d0SMimi Zohar if (IS_ERR(new_epayload)) {
90761cf45d0SMimi Zohar ret = PTR_ERR(new_epayload);
90861cf45d0SMimi Zohar goto out;
90961cf45d0SMimi Zohar }
91061cf45d0SMimi Zohar
91161cf45d0SMimi Zohar __ekey_init(new_epayload, epayload->format, new_master_desc,
91261cf45d0SMimi Zohar epayload->datalen);
91361cf45d0SMimi Zohar
91461cf45d0SMimi Zohar memcpy(new_epayload->iv, epayload->iv, ivsize);
91561cf45d0SMimi Zohar memcpy(new_epayload->payload_data, epayload->payload_data,
91661cf45d0SMimi Zohar epayload->payload_datalen);
91761cf45d0SMimi Zohar
918ee0b31a2SMimi Zohar rcu_assign_keypointer(key, new_epayload);
91961cf45d0SMimi Zohar call_rcu(&epayload->rcu, encrypted_rcu_free);
92061cf45d0SMimi Zohar out:
921453431a5SWaiman Long kfree_sensitive(buf);
92261cf45d0SMimi Zohar return ret;
92361cf45d0SMimi Zohar }
92461cf45d0SMimi Zohar
92561cf45d0SMimi Zohar /*
926d3ec10aaSWaiman Long * encrypted_read - format and copy out the encrypted data
92761cf45d0SMimi Zohar *
92861cf45d0SMimi Zohar * The resulting datablob format is:
92961cf45d0SMimi Zohar * <master-key name> <decrypted data length> <encrypted iv> <encrypted data>
93061cf45d0SMimi Zohar *
93161cf45d0SMimi Zohar * On success, return to userspace the encrypted key datablob size.
93261cf45d0SMimi Zohar */
encrypted_read(const struct key * key,char * buffer,size_t buflen)933d3ec10aaSWaiman Long static long encrypted_read(const struct key *key, char *buffer,
93461cf45d0SMimi Zohar size_t buflen)
93561cf45d0SMimi Zohar {
93661cf45d0SMimi Zohar struct encrypted_key_payload *epayload;
93761cf45d0SMimi Zohar struct key *mkey;
938146aa8b1SDavid Howells const u8 *master_key;
93961cf45d0SMimi Zohar size_t master_keylen;
94061cf45d0SMimi Zohar char derived_key[HASH_SIZE];
94161cf45d0SMimi Zohar char *ascii_buf;
94261cf45d0SMimi Zohar size_t asciiblob_len;
94361cf45d0SMimi Zohar int ret;
94461cf45d0SMimi Zohar
9450837e49aSDavid Howells epayload = dereference_key_locked(key);
94661cf45d0SMimi Zohar
94761cf45d0SMimi Zohar /* returns the hex encoded iv, encrypted-data, and hmac as ascii */
94861cf45d0SMimi Zohar asciiblob_len = epayload->datablob_len + ivsize + 1
94961cf45d0SMimi Zohar + roundup(epayload->decrypted_datalen, blksize)
95061cf45d0SMimi Zohar + (HASH_SIZE * 2);
95161cf45d0SMimi Zohar
95261cf45d0SMimi Zohar if (!buffer || buflen < asciiblob_len)
95361cf45d0SMimi Zohar return asciiblob_len;
95461cf45d0SMimi Zohar
95561cf45d0SMimi Zohar mkey = request_master_key(epayload, &master_key, &master_keylen);
95661cf45d0SMimi Zohar if (IS_ERR(mkey))
95761cf45d0SMimi Zohar return PTR_ERR(mkey);
95861cf45d0SMimi Zohar
95961cf45d0SMimi Zohar ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen);
96061cf45d0SMimi Zohar if (ret < 0)
96161cf45d0SMimi Zohar goto out;
96261cf45d0SMimi Zohar
96361cf45d0SMimi Zohar ret = derived_key_encrypt(epayload, derived_key, sizeof derived_key);
96461cf45d0SMimi Zohar if (ret < 0)
96561cf45d0SMimi Zohar goto out;
96661cf45d0SMimi Zohar
96761cf45d0SMimi Zohar ret = datablob_hmac_append(epayload, master_key, master_keylen);
96861cf45d0SMimi Zohar if (ret < 0)
96961cf45d0SMimi Zohar goto out;
97061cf45d0SMimi Zohar
97161cf45d0SMimi Zohar ascii_buf = datablob_format(epayload, asciiblob_len);
97261cf45d0SMimi Zohar if (!ascii_buf) {
97361cf45d0SMimi Zohar ret = -ENOMEM;
97461cf45d0SMimi Zohar goto out;
97561cf45d0SMimi Zohar }
97661cf45d0SMimi Zohar
97761cf45d0SMimi Zohar up_read(&mkey->sem);
97861cf45d0SMimi Zohar key_put(mkey);
979a9dd74b2SEric Biggers memzero_explicit(derived_key, sizeof(derived_key));
98061cf45d0SMimi Zohar
981d3ec10aaSWaiman Long memcpy(buffer, ascii_buf, asciiblob_len);
982453431a5SWaiman Long kfree_sensitive(ascii_buf);
98361cf45d0SMimi Zohar
98461cf45d0SMimi Zohar return asciiblob_len;
98561cf45d0SMimi Zohar out:
98661cf45d0SMimi Zohar up_read(&mkey->sem);
98761cf45d0SMimi Zohar key_put(mkey);
988a9dd74b2SEric Biggers memzero_explicit(derived_key, sizeof(derived_key));
98961cf45d0SMimi Zohar return ret;
99061cf45d0SMimi Zohar }
99161cf45d0SMimi Zohar
99261cf45d0SMimi Zohar /*
993a9dd74b2SEric Biggers * encrypted_destroy - clear and free the key's payload
99461cf45d0SMimi Zohar */
encrypted_destroy(struct key * key)99561cf45d0SMimi Zohar static void encrypted_destroy(struct key *key)
99661cf45d0SMimi Zohar {
997453431a5SWaiman Long kfree_sensitive(key->payload.data[0]);
99861cf45d0SMimi Zohar }
99961cf45d0SMimi Zohar
100061cf45d0SMimi Zohar struct key_type key_type_encrypted = {
100161cf45d0SMimi Zohar .name = "encrypted",
100261cf45d0SMimi Zohar .instantiate = encrypted_instantiate,
100361cf45d0SMimi Zohar .update = encrypted_update,
100461cf45d0SMimi Zohar .destroy = encrypted_destroy,
100561cf45d0SMimi Zohar .describe = user_describe,
100661cf45d0SMimi Zohar .read = encrypted_read,
100761cf45d0SMimi Zohar };
100861cf45d0SMimi Zohar EXPORT_SYMBOL_GPL(key_type_encrypted);
100961cf45d0SMimi Zohar
init_encrypted(void)101061cf45d0SMimi Zohar static int __init init_encrypted(void)
101161cf45d0SMimi Zohar {
101261cf45d0SMimi Zohar int ret;
101361cf45d0SMimi Zohar
10143d234b33SEric Biggers hash_tfm = crypto_alloc_shash(hash_alg, 0, 0);
101564d107d3SEric Biggers if (IS_ERR(hash_tfm)) {
101664d107d3SEric Biggers pr_err("encrypted_key: can't allocate %s transform: %ld\n",
101764d107d3SEric Biggers hash_alg, PTR_ERR(hash_tfm));
101864d107d3SEric Biggers return PTR_ERR(hash_tfm);
101964d107d3SEric Biggers }
102064d107d3SEric Biggers
1021b26bdde5STakashi Iwai ret = aes_get_sizes();
1022b26bdde5STakashi Iwai if (ret < 0)
1023b26bdde5STakashi Iwai goto out;
102461cf45d0SMimi Zohar ret = register_key_type(&key_type_encrypted);
102561cf45d0SMimi Zohar if (ret < 0)
102661cf45d0SMimi Zohar goto out;
1027b26bdde5STakashi Iwai return 0;
102861cf45d0SMimi Zohar out:
102964d107d3SEric Biggers crypto_free_shash(hash_tfm);
103061cf45d0SMimi Zohar return ret;
103161cf45d0SMimi Zohar
103261cf45d0SMimi Zohar }
103361cf45d0SMimi Zohar
cleanup_encrypted(void)103461cf45d0SMimi Zohar static void __exit cleanup_encrypted(void)
103561cf45d0SMimi Zohar {
103664d107d3SEric Biggers crypto_free_shash(hash_tfm);
103761cf45d0SMimi Zohar unregister_key_type(&key_type_encrypted);
103861cf45d0SMimi Zohar }
103961cf45d0SMimi Zohar
104061cf45d0SMimi Zohar late_initcall(init_encrypted);
104161cf45d0SMimi Zohar module_exit(cleanup_encrypted);
104261cf45d0SMimi Zohar
1043*84edd7adSJeff Johnson MODULE_DESCRIPTION("Encrypted key type");
104461cf45d0SMimi Zohar MODULE_LICENSE("GPL");
1045