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