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