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