^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2010 IBM Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2010 Politecnico di Torino, Italy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * TORSEC group -- https://security.polito.it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Authors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Mimi Zohar <zohar@us.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Roberto Sassu <roberto.sassu@polito.it>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * See Documentation/security/keys/trusted-encrypted.rst
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/parser.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <keys/user-type.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <keys/trusted-type.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <keys/encrypted-type.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/key-type.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/rcupdate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/scatterlist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <crypto/aes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <crypto/algapi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <crypto/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <crypto/sha.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <crypto/skcipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include "encrypted.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include "ecryptfs_format.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static const char KEY_TRUSTED_PREFIX[] = "trusted:";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static const char KEY_USER_PREFIX[] = "user:";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static const char hash_alg[] = "sha256";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static const char hmac_alg[] = "hmac(sha256)";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static const char blkcipher_alg[] = "cbc(aes)";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static const char key_format_default[] = "default";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static const char key_format_ecryptfs[] = "ecryptfs";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static const char key_format_enc32[] = "enc32";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static unsigned int ivsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static int blksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #define KEY_TRUSTED_PREFIX_LEN (sizeof (KEY_TRUSTED_PREFIX) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #define KEY_USER_PREFIX_LEN (sizeof (KEY_USER_PREFIX) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #define KEY_ECRYPTFS_DESC_LEN 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #define HASH_SIZE SHA256_DIGEST_SIZE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #define MAX_DATA_SIZE 4096
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #define MIN_DATA_SIZE 20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #define KEY_ENC32_PAYLOAD_LEN 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static struct crypto_shash *hash_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) Opt_new, Opt_load, Opt_update, Opt_err
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) Opt_default, Opt_ecryptfs, Opt_enc32, Opt_error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static const match_table_t key_format_tokens = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) {Opt_default, "default"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {Opt_ecryptfs, "ecryptfs"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {Opt_enc32, "enc32"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) {Opt_error, NULL}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) static const match_table_t key_tokens = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) {Opt_new, "new"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) {Opt_load, "load"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {Opt_update, "update"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {Opt_err, NULL}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static int aes_get_sizes(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct crypto_skcipher *tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) tfm = crypto_alloc_skcipher(blkcipher_alg, 0, CRYPTO_ALG_ASYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (IS_ERR(tfm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) pr_err("encrypted_key: failed to alloc_cipher (%ld)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) PTR_ERR(tfm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return PTR_ERR(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) ivsize = crypto_skcipher_ivsize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) blksize = crypto_skcipher_blocksize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) crypto_free_skcipher(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * valid_ecryptfs_desc - verify the description of a new/loaded encrypted key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * The description of a encrypted key with format 'ecryptfs' must contain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * exactly 16 hexadecimal characters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static int valid_ecryptfs_desc(const char *ecryptfs_desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (strlen(ecryptfs_desc) != KEY_ECRYPTFS_DESC_LEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) pr_err("encrypted_key: key description must be %d hexadecimal "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) "characters long\n", KEY_ECRYPTFS_DESC_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) for (i = 0; i < KEY_ECRYPTFS_DESC_LEN; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (!isxdigit(ecryptfs_desc[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) pr_err("encrypted_key: key description must contain "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) "only hexadecimal characters\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * valid_master_desc - verify the 'key-type:desc' of a new/updated master-key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * key-type:= "trusted:" | "user:"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * desc:= master-key description
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * Verify that 'key-type' is valid and that 'desc' exists. On key update,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * only the master key description is permitted to change, not the key-type.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * The key-type remains constant.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * On success returns 0, otherwise -EINVAL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static int valid_master_desc(const char *new_desc, const char *orig_desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) int prefix_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (!strncmp(new_desc, KEY_TRUSTED_PREFIX, KEY_TRUSTED_PREFIX_LEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) prefix_len = KEY_TRUSTED_PREFIX_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) else if (!strncmp(new_desc, KEY_USER_PREFIX, KEY_USER_PREFIX_LEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) prefix_len = KEY_USER_PREFIX_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if (!new_desc[prefix_len])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (orig_desc && strncmp(new_desc, orig_desc, prefix_len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * datablob_parse - parse the keyctl data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * datablob format:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * new [<format>] <master-key name> <decrypted data length>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * load [<format>] <master-key name> <decrypted data length>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * <encrypted iv + data>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * update <new-master-key name>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * Tokenizes a copy of the keyctl data, returning a pointer to each token,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * which is null terminated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * On success returns 0, otherwise -EINVAL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static int datablob_parse(char *datablob, const char **format,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) char **master_desc, char **decrypted_datalen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) char **hex_encoded_iv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) substring_t args[MAX_OPT_ARGS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) int ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) int key_cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) int key_format;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) char *p, *keyword;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) keyword = strsep(&datablob, " \t");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (!keyword) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) pr_info("encrypted_key: insufficient parameters specified\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) key_cmd = match_token(keyword, key_tokens, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) /* Get optional format: default | ecryptfs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) p = strsep(&datablob, " \t");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (!p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) pr_err("encrypted_key: insufficient parameters specified\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) key_format = match_token(p, key_format_tokens, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) switch (key_format) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) case Opt_ecryptfs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) case Opt_enc32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) case Opt_default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) *format = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) *master_desc = strsep(&datablob, " \t");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) case Opt_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) *master_desc = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) if (!*master_desc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) pr_info("encrypted_key: master key parameter is missing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if (valid_master_desc(*master_desc, NULL) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) pr_info("encrypted_key: master key parameter \'%s\' "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) "is invalid\n", *master_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (decrypted_datalen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) *decrypted_datalen = strsep(&datablob, " \t");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) if (!*decrypted_datalen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) pr_info("encrypted_key: keylen parameter is missing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) switch (key_cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) case Opt_new:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (!decrypted_datalen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) pr_info("encrypted_key: keyword \'%s\' not allowed "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) "when called from .update method\n", keyword);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) case Opt_load:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (!decrypted_datalen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) pr_info("encrypted_key: keyword \'%s\' not allowed "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) "when called from .update method\n", keyword);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) *hex_encoded_iv = strsep(&datablob, " \t");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (!*hex_encoded_iv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) pr_info("encrypted_key: hex blob is missing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) case Opt_update:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (decrypted_datalen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) pr_info("encrypted_key: keyword \'%s\' not allowed "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) "when called from .instantiate method\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) keyword);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) case Opt_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) pr_info("encrypted_key: keyword \'%s\' not recognized\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) keyword);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) * datablob_format - format as an ascii string, before copying to userspace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) static char *datablob_format(struct encrypted_key_payload *epayload,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) size_t asciiblob_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) char *ascii_buf, *bufp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) u8 *iv = epayload->iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) ascii_buf = kmalloc(asciiblob_len + 1, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) if (!ascii_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) ascii_buf[asciiblob_len] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) /* copy datablob master_desc and datalen strings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) len = sprintf(ascii_buf, "%s %s %s ", epayload->format,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) epayload->master_desc, epayload->datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) /* convert the hex encoded iv, encrypted-data and HMAC to ascii */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) bufp = &ascii_buf[len];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) for (i = 0; i < (asciiblob_len - len) / 2; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) bufp = hex_byte_pack(bufp, iv[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) return ascii_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) * request_user_key - request the user key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) * Use a user provided key to encrypt/decrypt an encrypted-key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) static struct key *request_user_key(const char *master_desc, const u8 **master_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) size_t *master_keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) const struct user_key_payload *upayload;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) struct key *ukey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) ukey = request_key(&key_type_user, master_desc, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) if (IS_ERR(ukey))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) down_read(&ukey->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) upayload = user_key_payload_locked(ukey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) if (!upayload) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) /* key was revoked before we acquired its semaphore */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) up_read(&ukey->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) key_put(ukey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) ukey = ERR_PTR(-EKEYREVOKED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) *master_key = upayload->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) *master_keylen = upayload->datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) return ukey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) static int calc_hmac(u8 *digest, const u8 *key, unsigned int keylen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) const u8 *buf, unsigned int buflen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) struct crypto_shash *tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) tfm = crypto_alloc_shash(hmac_alg, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) if (IS_ERR(tfm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) pr_err("encrypted_key: can't alloc %s transform: %ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) hmac_alg, PTR_ERR(tfm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return PTR_ERR(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) err = crypto_shash_setkey(tfm, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) err = crypto_shash_tfm_digest(tfm, buf, buflen, digest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) crypto_free_shash(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) enum derived_key_type { ENC_KEY, AUTH_KEY };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) /* Derive authentication/encryption key from trusted key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) static int get_derived_key(u8 *derived_key, enum derived_key_type key_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) const u8 *master_key, size_t master_keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) u8 *derived_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) unsigned int derived_buf_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) derived_buf_len = strlen("AUTH_KEY") + 1 + master_keylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) if (derived_buf_len < HASH_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) derived_buf_len = HASH_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) derived_buf = kzalloc(derived_buf_len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) if (!derived_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) if (key_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) strcpy(derived_buf, "AUTH_KEY");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) strcpy(derived_buf, "ENC_KEY");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) memcpy(derived_buf + strlen(derived_buf) + 1, master_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) master_keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) ret = crypto_shash_tfm_digest(hash_tfm, derived_buf, derived_buf_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) derived_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) kfree_sensitive(derived_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) static struct skcipher_request *init_skcipher_req(const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) unsigned int key_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) struct skcipher_request *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) struct crypto_skcipher *tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) tfm = crypto_alloc_skcipher(blkcipher_alg, 0, CRYPTO_ALG_ASYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) if (IS_ERR(tfm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) pr_err("encrypted_key: failed to load %s transform (%ld)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) blkcipher_alg, PTR_ERR(tfm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) return ERR_CAST(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) ret = crypto_skcipher_setkey(tfm, key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) pr_err("encrypted_key: failed to setkey (%d)\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) crypto_free_skcipher(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) req = skcipher_request_alloc(tfm, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) if (!req) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) pr_err("encrypted_key: failed to allocate request for %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) blkcipher_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) crypto_free_skcipher(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) skcipher_request_set_callback(req, 0, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) return req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) static struct key *request_master_key(struct encrypted_key_payload *epayload,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) const u8 **master_key, size_t *master_keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) struct key *mkey = ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) if (!strncmp(epayload->master_desc, KEY_TRUSTED_PREFIX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) KEY_TRUSTED_PREFIX_LEN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) mkey = request_trusted_key(epayload->master_desc +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) KEY_TRUSTED_PREFIX_LEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) master_key, master_keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) } else if (!strncmp(epayload->master_desc, KEY_USER_PREFIX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) KEY_USER_PREFIX_LEN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) mkey = request_user_key(epayload->master_desc +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) KEY_USER_PREFIX_LEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) master_key, master_keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) if (IS_ERR(mkey)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) int ret = PTR_ERR(mkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) if (ret == -ENOTSUPP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) pr_info("encrypted_key: key %s not supported",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) epayload->master_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) pr_info("encrypted_key: key %s not found",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) epayload->master_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) dump_master_key(*master_key, *master_keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) return mkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) /* Before returning data to userspace, encrypt decrypted data. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) static int derived_key_encrypt(struct encrypted_key_payload *epayload,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) const u8 *derived_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) unsigned int derived_keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) struct scatterlist sg_in[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) struct scatterlist sg_out[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) struct crypto_skcipher *tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) struct skcipher_request *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) unsigned int encrypted_datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) u8 iv[AES_BLOCK_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) req = init_skcipher_req(derived_key, derived_keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) ret = PTR_ERR(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) if (IS_ERR(req))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) dump_decrypted_data(epayload);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) sg_init_table(sg_in, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) sg_set_buf(&sg_in[0], epayload->decrypted_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) epayload->decrypted_datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) sg_set_page(&sg_in[1], ZERO_PAGE(0), AES_BLOCK_SIZE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) sg_init_table(sg_out, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) sg_set_buf(sg_out, epayload->encrypted_data, encrypted_datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) memcpy(iv, epayload->iv, sizeof(iv));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) skcipher_request_set_crypt(req, sg_in, sg_out, encrypted_datalen, iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) ret = crypto_skcipher_encrypt(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) skcipher_request_free(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) crypto_free_skcipher(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) pr_err("encrypted_key: failed to encrypt (%d)\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) dump_encrypted_data(epayload, encrypted_datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) static int datablob_hmac_append(struct encrypted_key_payload *epayload,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) const u8 *master_key, size_t master_keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) u8 derived_key[HASH_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) u8 *digest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) digest = epayload->format + epayload->datablob_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) ret = calc_hmac(digest, derived_key, sizeof derived_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) epayload->format, epayload->datablob_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) dump_hmac(NULL, digest, HASH_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) memzero_explicit(derived_key, sizeof(derived_key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) /* verify HMAC before decrypting encrypted key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) static int datablob_hmac_verify(struct encrypted_key_payload *epayload,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) const u8 *format, const u8 *master_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) size_t master_keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) u8 derived_key[HASH_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) u8 digest[HASH_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) unsigned short len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) len = epayload->datablob_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) if (!format) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) p = epayload->master_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) len -= strlen(epayload->format) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) p = epayload->format;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) ret = calc_hmac(digest, derived_key, sizeof derived_key, p, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) ret = crypto_memneq(digest, epayload->format + epayload->datablob_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) sizeof(digest));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) dump_hmac("datablob",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) epayload->format + epayload->datablob_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) HASH_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) dump_hmac("calc", digest, HASH_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) memzero_explicit(derived_key, sizeof(derived_key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) static int derived_key_decrypt(struct encrypted_key_payload *epayload,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) const u8 *derived_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) unsigned int derived_keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) struct scatterlist sg_in[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) struct scatterlist sg_out[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) struct crypto_skcipher *tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) struct skcipher_request *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) unsigned int encrypted_datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) u8 iv[AES_BLOCK_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) u8 *pad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) /* Throwaway buffer to hold the unused zero padding at the end */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) pad = kmalloc(AES_BLOCK_SIZE, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) if (!pad)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) req = init_skcipher_req(derived_key, derived_keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) ret = PTR_ERR(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) if (IS_ERR(req))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) dump_encrypted_data(epayload, encrypted_datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) sg_init_table(sg_in, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) sg_init_table(sg_out, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) sg_set_buf(sg_in, epayload->encrypted_data, encrypted_datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) sg_set_buf(&sg_out[0], epayload->decrypted_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) epayload->decrypted_datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) sg_set_buf(&sg_out[1], pad, AES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) memcpy(iv, epayload->iv, sizeof(iv));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) skcipher_request_set_crypt(req, sg_in, sg_out, encrypted_datalen, iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) ret = crypto_skcipher_decrypt(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) skcipher_request_free(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) crypto_free_skcipher(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) dump_decrypted_data(epayload);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) kfree(pad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) /* Allocate memory for decrypted key and datablob. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) static struct encrypted_key_payload *encrypted_key_alloc(struct key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) const char *format,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) const char *master_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) const char *datalen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) struct encrypted_key_payload *epayload = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) unsigned short datablob_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) unsigned short decrypted_datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) unsigned short payload_datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) unsigned int encrypted_datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) unsigned int format_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) long dlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) ret = kstrtol(datalen, 10, &dlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) if (ret < 0 || dlen < MIN_DATA_SIZE || dlen > MAX_DATA_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) format_len = (!format) ? strlen(key_format_default) : strlen(format);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) decrypted_datalen = dlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) payload_datalen = decrypted_datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) if (format) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) if (!strcmp(format, key_format_ecryptfs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) if (dlen != ECRYPTFS_MAX_KEY_BYTES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) pr_err("encrypted_key: keylen for the ecryptfs format must be equal to %d bytes\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) ECRYPTFS_MAX_KEY_BYTES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) decrypted_datalen = ECRYPTFS_MAX_KEY_BYTES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) payload_datalen = sizeof(struct ecryptfs_auth_tok);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) } else if (!strcmp(format, key_format_enc32)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) if (decrypted_datalen != KEY_ENC32_PAYLOAD_LEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) pr_err("encrypted_key: enc32 key payload incorrect length: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) decrypted_datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) encrypted_datalen = roundup(decrypted_datalen, blksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) datablob_len = format_len + 1 + strlen(master_desc) + 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) + strlen(datalen) + 1 + ivsize + 1 + encrypted_datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) ret = key_payload_reserve(key, payload_datalen + datablob_len
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) + HASH_SIZE + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) epayload = kzalloc(sizeof(*epayload) + payload_datalen +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) datablob_len + HASH_SIZE + 1, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) if (!epayload)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) epayload->payload_datalen = payload_datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) epayload->decrypted_datalen = decrypted_datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) epayload->datablob_len = datablob_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) return epayload;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) static int encrypted_key_decrypt(struct encrypted_key_payload *epayload,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) const char *format, const char *hex_encoded_iv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) struct key *mkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) u8 derived_key[HASH_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) const u8 *master_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) u8 *hmac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) const char *hex_encoded_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) unsigned int encrypted_datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) size_t master_keylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) size_t asciilen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) asciilen = (ivsize + 1 + encrypted_datalen + HASH_SIZE) * 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) if (strlen(hex_encoded_iv) != asciilen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) hex_encoded_data = hex_encoded_iv + (2 * ivsize) + 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) ret = hex2bin(epayload->iv, hex_encoded_iv, ivsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) ret = hex2bin(epayload->encrypted_data, hex_encoded_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) encrypted_datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) hmac = epayload->format + epayload->datablob_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) ret = hex2bin(hmac, hex_encoded_data + (encrypted_datalen * 2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) HASH_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) mkey = request_master_key(epayload, &master_key, &master_keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) if (IS_ERR(mkey))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) return PTR_ERR(mkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) ret = datablob_hmac_verify(epayload, format, master_key, master_keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) pr_err("encrypted_key: bad hmac (%d)\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) ret = derived_key_decrypt(epayload, derived_key, sizeof derived_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) pr_err("encrypted_key: failed to decrypt key (%d)\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) up_read(&mkey->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) key_put(mkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) memzero_explicit(derived_key, sizeof(derived_key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) static void __ekey_init(struct encrypted_key_payload *epayload,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) const char *format, const char *master_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) const char *datalen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) unsigned int format_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) format_len = (!format) ? strlen(key_format_default) : strlen(format);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) epayload->format = epayload->payload_data + epayload->payload_datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) epayload->master_desc = epayload->format + format_len + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) epayload->datalen = epayload->master_desc + strlen(master_desc) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) epayload->iv = epayload->datalen + strlen(datalen) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) epayload->encrypted_data = epayload->iv + ivsize + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) epayload->decrypted_data = epayload->payload_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) if (!format)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) memcpy(epayload->format, key_format_default, format_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) if (!strcmp(format, key_format_ecryptfs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) epayload->decrypted_data =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) ecryptfs_get_auth_tok_key((struct ecryptfs_auth_tok *)epayload->payload_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) memcpy(epayload->format, format, format_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) memcpy(epayload->master_desc, master_desc, strlen(master_desc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) memcpy(epayload->datalen, datalen, strlen(datalen));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) * encrypted_init - initialize an encrypted key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) * For a new key, use a random number for both the iv and data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) * itself. For an old key, decrypt the hex encoded data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) static int encrypted_init(struct encrypted_key_payload *epayload,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) const char *key_desc, const char *format,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) const char *master_desc, const char *datalen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) const char *hex_encoded_iv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) if (format && !strcmp(format, key_format_ecryptfs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) ret = valid_ecryptfs_desc(key_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) ecryptfs_fill_auth_tok((struct ecryptfs_auth_tok *)epayload->payload_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) key_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) __ekey_init(epayload, format, master_desc, datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) if (!hex_encoded_iv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) get_random_bytes(epayload->iv, ivsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) get_random_bytes(epayload->decrypted_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) epayload->decrypted_datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) ret = encrypted_key_decrypt(epayload, format, hex_encoded_iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) * encrypted_instantiate - instantiate an encrypted key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) * Decrypt an existing encrypted datablob or create a new encrypted key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) * based on a kernel random number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) * On success, return 0. Otherwise return errno.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) static int encrypted_instantiate(struct key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) struct key_preparsed_payload *prep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) struct encrypted_key_payload *epayload = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) char *datablob = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) const char *format = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) char *master_desc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) char *decrypted_datalen = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) char *hex_encoded_iv = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) size_t datalen = prep->datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) if (datalen <= 0 || datalen > 32767 || !prep->data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) datablob = kmalloc(datalen + 1, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) if (!datablob)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) datablob[datalen] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) memcpy(datablob, prep->data, datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) ret = datablob_parse(datablob, &format, &master_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) &decrypted_datalen, &hex_encoded_iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) epayload = encrypted_key_alloc(key, format, master_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) decrypted_datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) if (IS_ERR(epayload)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) ret = PTR_ERR(epayload);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) ret = encrypted_init(epayload, key->description, format, master_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) decrypted_datalen, hex_encoded_iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) kfree_sensitive(epayload);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) rcu_assign_keypointer(key, epayload);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) kfree_sensitive(datablob);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) static void encrypted_rcu_free(struct rcu_head *rcu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) struct encrypted_key_payload *epayload;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) epayload = container_of(rcu, struct encrypted_key_payload, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) kfree_sensitive(epayload);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) * encrypted_update - update the master key description
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) * Change the master key description for an existing encrypted key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) * The next read will return an encrypted datablob using the new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) * master key description.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) * On success, return 0. Otherwise return errno.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) static int encrypted_update(struct key *key, struct key_preparsed_payload *prep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) struct encrypted_key_payload *epayload = key->payload.data[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) struct encrypted_key_payload *new_epayload;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) char *new_master_desc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) const char *format = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) size_t datalen = prep->datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) if (key_is_negative(key))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) return -ENOKEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) if (datalen <= 0 || datalen > 32767 || !prep->data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) buf = kmalloc(datalen + 1, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) buf[datalen] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) memcpy(buf, prep->data, datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) ret = datablob_parse(buf, &format, &new_master_desc, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) ret = valid_master_desc(new_master_desc, epayload->master_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) new_epayload = encrypted_key_alloc(key, epayload->format,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) new_master_desc, epayload->datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) if (IS_ERR(new_epayload)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) ret = PTR_ERR(new_epayload);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) __ekey_init(new_epayload, epayload->format, new_master_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) epayload->datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) memcpy(new_epayload->iv, epayload->iv, ivsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) memcpy(new_epayload->payload_data, epayload->payload_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) epayload->payload_datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) rcu_assign_keypointer(key, new_epayload);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) call_rcu(&epayload->rcu, encrypted_rcu_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) kfree_sensitive(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) * encrypted_read - format and copy out the encrypted data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) * The resulting datablob format is:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) * <master-key name> <decrypted data length> <encrypted iv> <encrypted data>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) * On success, return to userspace the encrypted key datablob size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) static long encrypted_read(const struct key *key, char *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) size_t buflen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) struct encrypted_key_payload *epayload;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) struct key *mkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) const u8 *master_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) size_t master_keylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) char derived_key[HASH_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) char *ascii_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) size_t asciiblob_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) epayload = dereference_key_locked(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) /* returns the hex encoded iv, encrypted-data, and hmac as ascii */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) asciiblob_len = epayload->datablob_len + ivsize + 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) + roundup(epayload->decrypted_datalen, blksize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) + (HASH_SIZE * 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) if (!buffer || buflen < asciiblob_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) return asciiblob_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) mkey = request_master_key(epayload, &master_key, &master_keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) if (IS_ERR(mkey))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) return PTR_ERR(mkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) ret = derived_key_encrypt(epayload, derived_key, sizeof derived_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) ret = datablob_hmac_append(epayload, master_key, master_keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) ascii_buf = datablob_format(epayload, asciiblob_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) if (!ascii_buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) up_read(&mkey->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) key_put(mkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) memzero_explicit(derived_key, sizeof(derived_key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) memcpy(buffer, ascii_buf, asciiblob_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) kfree_sensitive(ascii_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) return asciiblob_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) up_read(&mkey->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) key_put(mkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) memzero_explicit(derived_key, sizeof(derived_key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) * encrypted_destroy - clear and free the key's payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) static void encrypted_destroy(struct key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) kfree_sensitive(key->payload.data[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) struct key_type key_type_encrypted = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) .name = "encrypted",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) .instantiate = encrypted_instantiate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) .update = encrypted_update,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) .destroy = encrypted_destroy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) .describe = user_describe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) .read = encrypted_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) EXPORT_SYMBOL_GPL(key_type_encrypted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) static int __init init_encrypted(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) hash_tfm = crypto_alloc_shash(hash_alg, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) if (IS_ERR(hash_tfm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) pr_err("encrypted_key: can't allocate %s transform: %ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) hash_alg, PTR_ERR(hash_tfm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) return PTR_ERR(hash_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) ret = aes_get_sizes();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) ret = register_key_type(&key_type_encrypted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) crypto_free_shash(hash_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) static void __exit cleanup_encrypted(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) crypto_free_shash(hash_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) unregister_key_type(&key_type_encrypted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) late_initcall(init_encrypted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) module_exit(cleanup_encrypted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) MODULE_LICENSE("GPL");