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> |
| 31 | #include <linux/crypto.h> |
Roberto Sassu | 79a73d1 | 2011-06-27 13:45:44 +0200 | [diff] [blame] | 32 | #include <linux/ctype.h> |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 33 | #include <crypto/hash.h> |
| 34 | #include <crypto/sha.h> |
| 35 | #include <crypto/aes.h> |
| 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 | { |
| 88 | struct crypto_blkcipher *tfm; |
| 89 | |
| 90 | tfm = crypto_alloc_blkcipher(blkcipher_alg, 0, CRYPTO_ALG_ASYNC); |
| 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 | } |
| 96 | ivsize = crypto_blkcipher_ivsize(tfm); |
| 97 | blksize = crypto_blkcipher_blocksize(tfm); |
| 98 | crypto_free_blkcipher(tfm); |
| 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++) |
| 296 | bufp = pack_hex_byte(bufp, iv[i]); |
| 297 | out: |
| 298 | return ascii_buf; |
| 299 | } |
| 300 | |
| 301 | /* |
| 302 | * request_trusted_key - request the trusted key |
| 303 | * |
| 304 | * Trusted keys are sealed to PCRs and other metadata. Although userspace |
| 305 | * manages both trusted/encrypted key-types, like the encrypted key type |
| 306 | * data, trusted key type data is not visible decrypted from userspace. |
| 307 | */ |
| 308 | static struct key *request_trusted_key(const char *trusted_desc, |
Mimi Zohar | 3b1826c | 2010-12-13 16:53:13 -0500 | [diff] [blame] | 309 | u8 **master_key, size_t *master_keylen) |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 310 | { |
| 311 | struct trusted_key_payload *tpayload; |
| 312 | struct key *tkey; |
| 313 | |
| 314 | tkey = request_key(&key_type_trusted, trusted_desc, NULL); |
| 315 | if (IS_ERR(tkey)) |
| 316 | goto error; |
| 317 | |
| 318 | down_read(&tkey->sem); |
| 319 | tpayload = rcu_dereference(tkey->payload.data); |
| 320 | *master_key = tpayload->key; |
| 321 | *master_keylen = tpayload->key_len; |
| 322 | error: |
| 323 | return tkey; |
| 324 | } |
| 325 | |
| 326 | /* |
| 327 | * request_user_key - request the user key |
| 328 | * |
| 329 | * Use a user provided key to encrypt/decrypt an encrypted-key. |
| 330 | */ |
| 331 | static struct key *request_user_key(const char *master_desc, u8 **master_key, |
Mimi Zohar | 3b1826c | 2010-12-13 16:53:13 -0500 | [diff] [blame] | 332 | size_t *master_keylen) |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 333 | { |
| 334 | struct user_key_payload *upayload; |
| 335 | struct key *ukey; |
| 336 | |
| 337 | ukey = request_key(&key_type_user, master_desc, NULL); |
| 338 | if (IS_ERR(ukey)) |
| 339 | goto error; |
| 340 | |
| 341 | down_read(&ukey->sem); |
| 342 | upayload = rcu_dereference(ukey->payload.data); |
| 343 | *master_key = upayload->data; |
| 344 | *master_keylen = upayload->datalen; |
| 345 | error: |
| 346 | return ukey; |
| 347 | } |
| 348 | |
Mimi Zohar | 3b1826c | 2010-12-13 16:53:13 -0500 | [diff] [blame] | 349 | static struct sdesc *alloc_sdesc(struct crypto_shash *alg) |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 350 | { |
| 351 | struct sdesc *sdesc; |
| 352 | int size; |
| 353 | |
| 354 | size = sizeof(struct shash_desc) + crypto_shash_descsize(alg); |
| 355 | sdesc = kmalloc(size, GFP_KERNEL); |
| 356 | if (!sdesc) |
| 357 | return ERR_PTR(-ENOMEM); |
| 358 | sdesc->shash.tfm = alg; |
| 359 | sdesc->shash.flags = 0x0; |
| 360 | return sdesc; |
| 361 | } |
| 362 | |
Mimi Zohar | 3b1826c | 2010-12-13 16:53:13 -0500 | [diff] [blame] | 363 | static int calc_hmac(u8 *digest, const u8 *key, unsigned int keylen, |
| 364 | const u8 *buf, unsigned int buflen) |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 365 | { |
| 366 | struct sdesc *sdesc; |
| 367 | int ret; |
| 368 | |
Mimi Zohar | 3b1826c | 2010-12-13 16:53:13 -0500 | [diff] [blame] | 369 | sdesc = alloc_sdesc(hmacalg); |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 370 | if (IS_ERR(sdesc)) { |
| 371 | pr_info("encrypted_key: can't alloc %s\n", hmac_alg); |
| 372 | return PTR_ERR(sdesc); |
| 373 | } |
| 374 | |
| 375 | ret = crypto_shash_setkey(hmacalg, key, keylen); |
| 376 | if (!ret) |
| 377 | ret = crypto_shash_digest(&sdesc->shash, buf, buflen, digest); |
| 378 | kfree(sdesc); |
| 379 | return ret; |
| 380 | } |
| 381 | |
Mimi Zohar | 3b1826c | 2010-12-13 16:53:13 -0500 | [diff] [blame] | 382 | static int calc_hash(u8 *digest, const u8 *buf, unsigned int buflen) |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 383 | { |
| 384 | struct sdesc *sdesc; |
| 385 | int ret; |
| 386 | |
Mimi Zohar | 3b1826c | 2010-12-13 16:53:13 -0500 | [diff] [blame] | 387 | sdesc = alloc_sdesc(hashalg); |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 388 | if (IS_ERR(sdesc)) { |
| 389 | pr_info("encrypted_key: can't alloc %s\n", hash_alg); |
| 390 | return PTR_ERR(sdesc); |
| 391 | } |
| 392 | |
| 393 | ret = crypto_shash_digest(&sdesc->shash, buf, buflen, digest); |
| 394 | kfree(sdesc); |
| 395 | return ret; |
| 396 | } |
| 397 | |
| 398 | enum derived_key_type { ENC_KEY, AUTH_KEY }; |
| 399 | |
| 400 | /* Derive authentication/encryption key from trusted key */ |
| 401 | 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] | 402 | const u8 *master_key, size_t master_keylen) |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 403 | { |
| 404 | u8 *derived_buf; |
| 405 | unsigned int derived_buf_len; |
| 406 | int ret; |
| 407 | |
| 408 | derived_buf_len = strlen("AUTH_KEY") + 1 + master_keylen; |
| 409 | if (derived_buf_len < HASH_SIZE) |
| 410 | derived_buf_len = HASH_SIZE; |
| 411 | |
| 412 | derived_buf = kzalloc(derived_buf_len, GFP_KERNEL); |
| 413 | if (!derived_buf) { |
| 414 | pr_err("encrypted_key: out of memory\n"); |
| 415 | return -ENOMEM; |
| 416 | } |
| 417 | if (key_type) |
| 418 | strcpy(derived_buf, "AUTH_KEY"); |
| 419 | else |
| 420 | strcpy(derived_buf, "ENC_KEY"); |
| 421 | |
| 422 | memcpy(derived_buf + strlen(derived_buf) + 1, master_key, |
| 423 | master_keylen); |
| 424 | ret = calc_hash(derived_key, derived_buf, derived_buf_len); |
| 425 | kfree(derived_buf); |
| 426 | return ret; |
| 427 | } |
| 428 | |
| 429 | static int init_blkcipher_desc(struct blkcipher_desc *desc, const u8 *key, |
Mimi Zohar | 3b1826c | 2010-12-13 16:53:13 -0500 | [diff] [blame] | 430 | unsigned int key_len, const u8 *iv, |
| 431 | unsigned int ivsize) |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 432 | { |
| 433 | int ret; |
| 434 | |
| 435 | desc->tfm = crypto_alloc_blkcipher(blkcipher_alg, 0, CRYPTO_ALG_ASYNC); |
| 436 | if (IS_ERR(desc->tfm)) { |
| 437 | pr_err("encrypted_key: failed to load %s transform (%ld)\n", |
| 438 | blkcipher_alg, PTR_ERR(desc->tfm)); |
| 439 | return PTR_ERR(desc->tfm); |
| 440 | } |
| 441 | desc->flags = 0; |
| 442 | |
| 443 | ret = crypto_blkcipher_setkey(desc->tfm, key, key_len); |
| 444 | if (ret < 0) { |
| 445 | pr_err("encrypted_key: failed to setkey (%d)\n", ret); |
| 446 | crypto_free_blkcipher(desc->tfm); |
| 447 | return ret; |
| 448 | } |
| 449 | crypto_blkcipher_set_iv(desc->tfm, iv, ivsize); |
| 450 | return 0; |
| 451 | } |
| 452 | |
| 453 | static struct key *request_master_key(struct encrypted_key_payload *epayload, |
Mimi Zohar | 3b1826c | 2010-12-13 16:53:13 -0500 | [diff] [blame] | 454 | u8 **master_key, size_t *master_keylen) |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 455 | { |
| 456 | struct key *mkey = NULL; |
| 457 | |
| 458 | if (!strncmp(epayload->master_desc, KEY_TRUSTED_PREFIX, |
| 459 | KEY_TRUSTED_PREFIX_LEN)) { |
| 460 | mkey = request_trusted_key(epayload->master_desc + |
| 461 | KEY_TRUSTED_PREFIX_LEN, |
| 462 | master_key, master_keylen); |
| 463 | } else if (!strncmp(epayload->master_desc, KEY_USER_PREFIX, |
| 464 | KEY_USER_PREFIX_LEN)) { |
| 465 | mkey = request_user_key(epayload->master_desc + |
| 466 | KEY_USER_PREFIX_LEN, |
| 467 | master_key, master_keylen); |
| 468 | } else |
| 469 | goto out; |
| 470 | |
Roberto Sassu | f91c2c5 | 2011-06-27 13:45:39 +0200 | [diff] [blame] | 471 | if (IS_ERR(mkey)) { |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 472 | pr_info("encrypted_key: key %s not found", |
| 473 | epayload->master_desc); |
Roberto Sassu | f91c2c5 | 2011-06-27 13:45:39 +0200 | [diff] [blame] | 474 | goto out; |
| 475 | } |
| 476 | |
| 477 | dump_master_key(*master_key, *master_keylen); |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 478 | out: |
| 479 | return mkey; |
| 480 | } |
| 481 | |
| 482 | /* Before returning data to userspace, encrypt decrypted data. */ |
| 483 | static int derived_key_encrypt(struct encrypted_key_payload *epayload, |
| 484 | const u8 *derived_key, |
Mimi Zohar | 3b1826c | 2010-12-13 16:53:13 -0500 | [diff] [blame] | 485 | unsigned int derived_keylen) |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 486 | { |
| 487 | struct scatterlist sg_in[2]; |
| 488 | struct scatterlist sg_out[1]; |
| 489 | struct blkcipher_desc desc; |
| 490 | unsigned int encrypted_datalen; |
| 491 | unsigned int padlen; |
| 492 | char pad[16]; |
| 493 | int ret; |
| 494 | |
| 495 | encrypted_datalen = roundup(epayload->decrypted_datalen, blksize); |
| 496 | padlen = encrypted_datalen - epayload->decrypted_datalen; |
| 497 | |
| 498 | ret = init_blkcipher_desc(&desc, derived_key, derived_keylen, |
| 499 | epayload->iv, ivsize); |
| 500 | if (ret < 0) |
| 501 | goto out; |
| 502 | dump_decrypted_data(epayload); |
| 503 | |
| 504 | memset(pad, 0, sizeof pad); |
| 505 | sg_init_table(sg_in, 2); |
| 506 | sg_set_buf(&sg_in[0], epayload->decrypted_data, |
| 507 | epayload->decrypted_datalen); |
| 508 | sg_set_buf(&sg_in[1], pad, padlen); |
| 509 | |
| 510 | sg_init_table(sg_out, 1); |
| 511 | sg_set_buf(sg_out, epayload->encrypted_data, encrypted_datalen); |
| 512 | |
| 513 | ret = crypto_blkcipher_encrypt(&desc, sg_out, sg_in, encrypted_datalen); |
| 514 | crypto_free_blkcipher(desc.tfm); |
| 515 | if (ret < 0) |
| 516 | pr_err("encrypted_key: failed to encrypt (%d)\n", ret); |
| 517 | else |
| 518 | dump_encrypted_data(epayload, encrypted_datalen); |
| 519 | out: |
| 520 | return ret; |
| 521 | } |
| 522 | |
| 523 | static int datablob_hmac_append(struct encrypted_key_payload *epayload, |
Mimi Zohar | 3b1826c | 2010-12-13 16:53:13 -0500 | [diff] [blame] | 524 | const u8 *master_key, size_t master_keylen) |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 525 | { |
| 526 | u8 derived_key[HASH_SIZE]; |
| 527 | u8 *digest; |
| 528 | int ret; |
| 529 | |
| 530 | ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen); |
| 531 | if (ret < 0) |
| 532 | goto out; |
| 533 | |
Roberto Sassu | 4e561d3 | 2011-06-27 13:45:42 +0200 | [diff] [blame] | 534 | digest = epayload->format + epayload->datablob_len; |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 535 | ret = calc_hmac(digest, derived_key, sizeof derived_key, |
Roberto Sassu | 4e561d3 | 2011-06-27 13:45:42 +0200 | [diff] [blame] | 536 | epayload->format, epayload->datablob_len); |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 537 | if (!ret) |
| 538 | dump_hmac(NULL, digest, HASH_SIZE); |
| 539 | out: |
| 540 | return ret; |
| 541 | } |
| 542 | |
| 543 | /* verify HMAC before decrypting encrypted key */ |
| 544 | static int datablob_hmac_verify(struct encrypted_key_payload *epayload, |
Roberto Sassu | 4e561d3 | 2011-06-27 13:45:42 +0200 | [diff] [blame] | 545 | const u8 *format, const u8 *master_key, |
| 546 | size_t master_keylen) |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 547 | { |
| 548 | u8 derived_key[HASH_SIZE]; |
| 549 | u8 digest[HASH_SIZE]; |
| 550 | int ret; |
Roberto Sassu | 4e561d3 | 2011-06-27 13:45:42 +0200 | [diff] [blame] | 551 | char *p; |
| 552 | unsigned short len; |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 553 | |
| 554 | ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen); |
| 555 | if (ret < 0) |
| 556 | goto out; |
| 557 | |
Roberto Sassu | 4e561d3 | 2011-06-27 13:45:42 +0200 | [diff] [blame] | 558 | len = epayload->datablob_len; |
| 559 | if (!format) { |
| 560 | p = epayload->master_desc; |
| 561 | len -= strlen(epayload->format) + 1; |
| 562 | } else |
| 563 | p = epayload->format; |
| 564 | |
| 565 | ret = calc_hmac(digest, derived_key, sizeof derived_key, p, len); |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 566 | if (ret < 0) |
| 567 | goto out; |
Roberto Sassu | 4e561d3 | 2011-06-27 13:45:42 +0200 | [diff] [blame] | 568 | ret = memcmp(digest, epayload->format + epayload->datablob_len, |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 569 | sizeof digest); |
| 570 | if (ret) { |
| 571 | ret = -EINVAL; |
| 572 | dump_hmac("datablob", |
Roberto Sassu | 4e561d3 | 2011-06-27 13:45:42 +0200 | [diff] [blame] | 573 | epayload->format + epayload->datablob_len, |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 574 | HASH_SIZE); |
| 575 | dump_hmac("calc", digest, HASH_SIZE); |
| 576 | } |
| 577 | out: |
| 578 | return ret; |
| 579 | } |
| 580 | |
| 581 | static int derived_key_decrypt(struct encrypted_key_payload *epayload, |
| 582 | const u8 *derived_key, |
Mimi Zohar | 3b1826c | 2010-12-13 16:53:13 -0500 | [diff] [blame] | 583 | unsigned int derived_keylen) |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 584 | { |
| 585 | struct scatterlist sg_in[1]; |
| 586 | struct scatterlist sg_out[2]; |
| 587 | struct blkcipher_desc desc; |
| 588 | unsigned int encrypted_datalen; |
| 589 | char pad[16]; |
| 590 | int ret; |
| 591 | |
| 592 | encrypted_datalen = roundup(epayload->decrypted_datalen, blksize); |
| 593 | ret = init_blkcipher_desc(&desc, derived_key, derived_keylen, |
| 594 | epayload->iv, ivsize); |
| 595 | if (ret < 0) |
| 596 | goto out; |
| 597 | dump_encrypted_data(epayload, encrypted_datalen); |
| 598 | |
| 599 | memset(pad, 0, sizeof pad); |
| 600 | sg_init_table(sg_in, 1); |
| 601 | sg_init_table(sg_out, 2); |
| 602 | sg_set_buf(sg_in, epayload->encrypted_data, encrypted_datalen); |
| 603 | sg_set_buf(&sg_out[0], epayload->decrypted_data, |
Mimi Zohar | 3b1826c | 2010-12-13 16:53:13 -0500 | [diff] [blame] | 604 | epayload->decrypted_datalen); |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 605 | sg_set_buf(&sg_out[1], pad, sizeof pad); |
| 606 | |
| 607 | ret = crypto_blkcipher_decrypt(&desc, sg_out, sg_in, encrypted_datalen); |
| 608 | crypto_free_blkcipher(desc.tfm); |
| 609 | if (ret < 0) |
| 610 | goto out; |
| 611 | dump_decrypted_data(epayload); |
| 612 | out: |
| 613 | return ret; |
| 614 | } |
| 615 | |
| 616 | /* Allocate memory for decrypted key and datablob. */ |
| 617 | static struct encrypted_key_payload *encrypted_key_alloc(struct key *key, |
Roberto Sassu | 4e561d3 | 2011-06-27 13:45:42 +0200 | [diff] [blame] | 618 | const char *format, |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 619 | const char *master_desc, |
| 620 | const char *datalen) |
| 621 | { |
| 622 | struct encrypted_key_payload *epayload = NULL; |
| 623 | unsigned short datablob_len; |
| 624 | unsigned short decrypted_datalen; |
Roberto Sassu | 4e561d3 | 2011-06-27 13:45:42 +0200 | [diff] [blame] | 625 | unsigned short payload_datalen; |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 626 | unsigned int encrypted_datalen; |
Roberto Sassu | 4e561d3 | 2011-06-27 13:45:42 +0200 | [diff] [blame] | 627 | unsigned int format_len; |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 628 | long dlen; |
| 629 | int ret; |
| 630 | |
| 631 | ret = strict_strtol(datalen, 10, &dlen); |
| 632 | if (ret < 0 || dlen < MIN_DATA_SIZE || dlen > MAX_DATA_SIZE) |
| 633 | return ERR_PTR(-EINVAL); |
| 634 | |
Roberto Sassu | 4e561d3 | 2011-06-27 13:45:42 +0200 | [diff] [blame] | 635 | format_len = (!format) ? strlen(key_format_default) : strlen(format); |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 636 | decrypted_datalen = dlen; |
Roberto Sassu | 4e561d3 | 2011-06-27 13:45:42 +0200 | [diff] [blame] | 637 | payload_datalen = decrypted_datalen; |
Roberto Sassu | 79a73d1 | 2011-06-27 13:45:44 +0200 | [diff] [blame] | 638 | if (format && !strcmp(format, key_format_ecryptfs)) { |
| 639 | if (dlen != ECRYPTFS_MAX_KEY_BYTES) { |
| 640 | pr_err("encrypted_key: keylen for the ecryptfs format " |
| 641 | "must be equal to %d bytes\n", |
| 642 | ECRYPTFS_MAX_KEY_BYTES); |
| 643 | return ERR_PTR(-EINVAL); |
| 644 | } |
| 645 | decrypted_datalen = ECRYPTFS_MAX_KEY_BYTES; |
| 646 | payload_datalen = sizeof(struct ecryptfs_auth_tok); |
| 647 | } |
| 648 | |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 649 | encrypted_datalen = roundup(decrypted_datalen, blksize); |
| 650 | |
Roberto Sassu | 4e561d3 | 2011-06-27 13:45:42 +0200 | [diff] [blame] | 651 | datablob_len = format_len + 1 + strlen(master_desc) + 1 |
| 652 | + strlen(datalen) + 1 + ivsize + 1 + encrypted_datalen; |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 653 | |
Roberto Sassu | 4e561d3 | 2011-06-27 13:45:42 +0200 | [diff] [blame] | 654 | ret = key_payload_reserve(key, payload_datalen + datablob_len |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 655 | + HASH_SIZE + 1); |
| 656 | if (ret < 0) |
| 657 | return ERR_PTR(ret); |
| 658 | |
Roberto Sassu | 4e561d3 | 2011-06-27 13:45:42 +0200 | [diff] [blame] | 659 | epayload = kzalloc(sizeof(*epayload) + payload_datalen + |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 660 | datablob_len + HASH_SIZE + 1, GFP_KERNEL); |
| 661 | if (!epayload) |
| 662 | return ERR_PTR(-ENOMEM); |
| 663 | |
Roberto Sassu | 4e561d3 | 2011-06-27 13:45:42 +0200 | [diff] [blame] | 664 | epayload->payload_datalen = payload_datalen; |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 665 | epayload->decrypted_datalen = decrypted_datalen; |
| 666 | epayload->datablob_len = datablob_len; |
| 667 | return epayload; |
| 668 | } |
| 669 | |
| 670 | static int encrypted_key_decrypt(struct encrypted_key_payload *epayload, |
Roberto Sassu | 4e561d3 | 2011-06-27 13:45:42 +0200 | [diff] [blame] | 671 | const char *format, const char *hex_encoded_iv) |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 672 | { |
| 673 | struct key *mkey; |
| 674 | u8 derived_key[HASH_SIZE]; |
| 675 | u8 *master_key; |
| 676 | u8 *hmac; |
Mimi Zohar | 1f35065 | 2010-12-13 16:53:12 -0500 | [diff] [blame] | 677 | const char *hex_encoded_data; |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 678 | unsigned int encrypted_datalen; |
Mimi Zohar | 3b1826c | 2010-12-13 16:53:13 -0500 | [diff] [blame] | 679 | size_t master_keylen; |
Mimi Zohar | 1f35065 | 2010-12-13 16:53:12 -0500 | [diff] [blame] | 680 | size_t asciilen; |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 681 | int ret; |
| 682 | |
| 683 | encrypted_datalen = roundup(epayload->decrypted_datalen, blksize); |
Mimi Zohar | 1f35065 | 2010-12-13 16:53:12 -0500 | [diff] [blame] | 684 | asciilen = (ivsize + 1 + encrypted_datalen + HASH_SIZE) * 2; |
| 685 | if (strlen(hex_encoded_iv) != asciilen) |
| 686 | return -EINVAL; |
| 687 | |
| 688 | hex_encoded_data = hex_encoded_iv + (2 * ivsize) + 2; |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 689 | hex2bin(epayload->iv, hex_encoded_iv, ivsize); |
| 690 | hex2bin(epayload->encrypted_data, hex_encoded_data, encrypted_datalen); |
| 691 | |
Roberto Sassu | 4e561d3 | 2011-06-27 13:45:42 +0200 | [diff] [blame] | 692 | hmac = epayload->format + epayload->datablob_len; |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 693 | hex2bin(hmac, hex_encoded_data + (encrypted_datalen * 2), HASH_SIZE); |
| 694 | |
| 695 | mkey = request_master_key(epayload, &master_key, &master_keylen); |
| 696 | if (IS_ERR(mkey)) |
| 697 | return PTR_ERR(mkey); |
| 698 | |
Roberto Sassu | 4e561d3 | 2011-06-27 13:45:42 +0200 | [diff] [blame] | 699 | ret = datablob_hmac_verify(epayload, format, master_key, master_keylen); |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 700 | if (ret < 0) { |
| 701 | pr_err("encrypted_key: bad hmac (%d)\n", ret); |
| 702 | goto out; |
| 703 | } |
| 704 | |
| 705 | ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen); |
| 706 | if (ret < 0) |
| 707 | goto out; |
| 708 | |
| 709 | ret = derived_key_decrypt(epayload, derived_key, sizeof derived_key); |
| 710 | if (ret < 0) |
| 711 | pr_err("encrypted_key: failed to decrypt key (%d)\n", ret); |
| 712 | out: |
| 713 | up_read(&mkey->sem); |
| 714 | key_put(mkey); |
| 715 | return ret; |
| 716 | } |
| 717 | |
| 718 | static void __ekey_init(struct encrypted_key_payload *epayload, |
Roberto Sassu | 4e561d3 | 2011-06-27 13:45:42 +0200 | [diff] [blame] | 719 | const char *format, const char *master_desc, |
| 720 | const char *datalen) |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 721 | { |
Roberto Sassu | 4e561d3 | 2011-06-27 13:45:42 +0200 | [diff] [blame] | 722 | unsigned int format_len; |
| 723 | |
| 724 | format_len = (!format) ? strlen(key_format_default) : strlen(format); |
| 725 | epayload->format = epayload->payload_data + epayload->payload_datalen; |
| 726 | epayload->master_desc = epayload->format + format_len + 1; |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 727 | epayload->datalen = epayload->master_desc + strlen(master_desc) + 1; |
| 728 | epayload->iv = epayload->datalen + strlen(datalen) + 1; |
| 729 | epayload->encrypted_data = epayload->iv + ivsize + 1; |
Roberto Sassu | 4e561d3 | 2011-06-27 13:45:42 +0200 | [diff] [blame] | 730 | epayload->decrypted_data = epayload->payload_data; |
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 | if (!format) |
| 733 | memcpy(epayload->format, key_format_default, format_len); |
Roberto Sassu | 79a73d1 | 2011-06-27 13:45:44 +0200 | [diff] [blame] | 734 | else { |
| 735 | if (!strcmp(format, key_format_ecryptfs)) |
| 736 | epayload->decrypted_data = |
| 737 | ecryptfs_get_auth_tok_key((struct ecryptfs_auth_tok *)epayload->payload_data); |
| 738 | |
Roberto Sassu | 4e561d3 | 2011-06-27 13:45:42 +0200 | [diff] [blame] | 739 | memcpy(epayload->format, format, format_len); |
Roberto Sassu | 79a73d1 | 2011-06-27 13:45:44 +0200 | [diff] [blame] | 740 | } |
| 741 | |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 742 | memcpy(epayload->master_desc, master_desc, strlen(master_desc)); |
| 743 | memcpy(epayload->datalen, datalen, strlen(datalen)); |
| 744 | } |
| 745 | |
| 746 | /* |
| 747 | * encrypted_init - initialize an encrypted key |
| 748 | * |
| 749 | * For a new key, use a random number for both the iv and data |
| 750 | * itself. For an old key, decrypt the hex encoded data. |
| 751 | */ |
| 752 | static int encrypted_init(struct encrypted_key_payload *epayload, |
Roberto Sassu | 79a73d1 | 2011-06-27 13:45:44 +0200 | [diff] [blame] | 753 | const char *key_desc, const char *format, |
| 754 | const char *master_desc, const char *datalen, |
| 755 | const char *hex_encoded_iv) |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 756 | { |
| 757 | int ret = 0; |
| 758 | |
Roberto Sassu | 79a73d1 | 2011-06-27 13:45:44 +0200 | [diff] [blame] | 759 | if (format && !strcmp(format, key_format_ecryptfs)) { |
| 760 | ret = valid_ecryptfs_desc(key_desc); |
| 761 | if (ret < 0) |
| 762 | return ret; |
| 763 | |
| 764 | ecryptfs_fill_auth_tok((struct ecryptfs_auth_tok *)epayload->payload_data, |
| 765 | key_desc); |
| 766 | } |
| 767 | |
Roberto Sassu | 4e561d3 | 2011-06-27 13:45:42 +0200 | [diff] [blame] | 768 | __ekey_init(epayload, format, master_desc, datalen); |
Mimi Zohar | 1f35065 | 2010-12-13 16:53:12 -0500 | [diff] [blame] | 769 | if (!hex_encoded_iv) { |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 770 | get_random_bytes(epayload->iv, ivsize); |
| 771 | |
| 772 | get_random_bytes(epayload->decrypted_data, |
| 773 | epayload->decrypted_datalen); |
| 774 | } else |
Roberto Sassu | 4e561d3 | 2011-06-27 13:45:42 +0200 | [diff] [blame] | 775 | ret = encrypted_key_decrypt(epayload, format, hex_encoded_iv); |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 776 | return ret; |
| 777 | } |
| 778 | |
| 779 | /* |
| 780 | * encrypted_instantiate - instantiate an encrypted key |
| 781 | * |
| 782 | * Decrypt an existing encrypted datablob or create a new encrypted key |
| 783 | * based on a kernel random number. |
| 784 | * |
| 785 | * On success, return 0. Otherwise return errno. |
| 786 | */ |
| 787 | static int encrypted_instantiate(struct key *key, const void *data, |
| 788 | size_t datalen) |
| 789 | { |
| 790 | struct encrypted_key_payload *epayload = NULL; |
| 791 | char *datablob = NULL; |
Roberto Sassu | 4e561d3 | 2011-06-27 13:45:42 +0200 | [diff] [blame] | 792 | const char *format = NULL; |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 793 | char *master_desc = NULL; |
| 794 | char *decrypted_datalen = NULL; |
| 795 | char *hex_encoded_iv = NULL; |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 796 | int ret; |
| 797 | |
| 798 | if (datalen <= 0 || datalen > 32767 || !data) |
| 799 | return -EINVAL; |
| 800 | |
| 801 | datablob = kmalloc(datalen + 1, GFP_KERNEL); |
| 802 | if (!datablob) |
| 803 | return -ENOMEM; |
| 804 | datablob[datalen] = 0; |
| 805 | memcpy(datablob, data, datalen); |
Roberto Sassu | 4e561d3 | 2011-06-27 13:45:42 +0200 | [diff] [blame] | 806 | ret = datablob_parse(datablob, &format, &master_desc, |
| 807 | &decrypted_datalen, &hex_encoded_iv); |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 808 | if (ret < 0) |
| 809 | goto out; |
| 810 | |
Roberto Sassu | 4e561d3 | 2011-06-27 13:45:42 +0200 | [diff] [blame] | 811 | epayload = encrypted_key_alloc(key, format, master_desc, |
| 812 | decrypted_datalen); |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 813 | if (IS_ERR(epayload)) { |
| 814 | ret = PTR_ERR(epayload); |
| 815 | goto out; |
| 816 | } |
Roberto Sassu | 79a73d1 | 2011-06-27 13:45:44 +0200 | [diff] [blame] | 817 | ret = encrypted_init(epayload, key->description, 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 | kfree(epayload); |
| 821 | goto out; |
| 822 | } |
| 823 | |
| 824 | rcu_assign_pointer(key->payload.data, epayload); |
| 825 | out: |
| 826 | kfree(datablob); |
| 827 | return ret; |
| 828 | } |
| 829 | |
| 830 | static void encrypted_rcu_free(struct rcu_head *rcu) |
| 831 | { |
| 832 | struct encrypted_key_payload *epayload; |
| 833 | |
| 834 | epayload = container_of(rcu, struct encrypted_key_payload, rcu); |
| 835 | memset(epayload->decrypted_data, 0, epayload->decrypted_datalen); |
| 836 | kfree(epayload); |
| 837 | } |
| 838 | |
| 839 | /* |
| 840 | * encrypted_update - update the master key description |
| 841 | * |
| 842 | * Change the master key description for an existing encrypted key. |
| 843 | * The next read will return an encrypted datablob using the new |
| 844 | * master key description. |
| 845 | * |
| 846 | * On success, return 0. Otherwise return errno. |
| 847 | */ |
| 848 | static int encrypted_update(struct key *key, const void *data, size_t datalen) |
| 849 | { |
| 850 | struct encrypted_key_payload *epayload = key->payload.data; |
| 851 | struct encrypted_key_payload *new_epayload; |
| 852 | char *buf; |
| 853 | char *new_master_desc = NULL; |
Roberto Sassu | 4e561d3 | 2011-06-27 13:45:42 +0200 | [diff] [blame] | 854 | const char *format = NULL; |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 855 | int ret = 0; |
| 856 | |
| 857 | if (datalen <= 0 || datalen > 32767 || !data) |
| 858 | return -EINVAL; |
| 859 | |
| 860 | buf = kmalloc(datalen + 1, GFP_KERNEL); |
| 861 | if (!buf) |
| 862 | return -ENOMEM; |
| 863 | |
| 864 | buf[datalen] = 0; |
| 865 | memcpy(buf, data, datalen); |
Roberto Sassu | 4e561d3 | 2011-06-27 13:45:42 +0200 | [diff] [blame] | 866 | ret = datablob_parse(buf, &format, &new_master_desc, NULL, NULL); |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 867 | if (ret < 0) |
| 868 | goto out; |
| 869 | |
| 870 | ret = valid_master_desc(new_master_desc, epayload->master_desc); |
| 871 | if (ret < 0) |
| 872 | goto out; |
| 873 | |
Roberto Sassu | 4e561d3 | 2011-06-27 13:45:42 +0200 | [diff] [blame] | 874 | new_epayload = encrypted_key_alloc(key, epayload->format, |
| 875 | new_master_desc, epayload->datalen); |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 876 | if (IS_ERR(new_epayload)) { |
| 877 | ret = PTR_ERR(new_epayload); |
| 878 | goto out; |
| 879 | } |
| 880 | |
Roberto Sassu | 4e561d3 | 2011-06-27 13:45:42 +0200 | [diff] [blame] | 881 | __ekey_init(new_epayload, epayload->format, new_master_desc, |
| 882 | epayload->datalen); |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 883 | |
| 884 | memcpy(new_epayload->iv, epayload->iv, ivsize); |
Roberto Sassu | 4e561d3 | 2011-06-27 13:45:42 +0200 | [diff] [blame] | 885 | memcpy(new_epayload->payload_data, epayload->payload_data, |
| 886 | epayload->payload_datalen); |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 887 | |
| 888 | rcu_assign_pointer(key->payload.data, new_epayload); |
| 889 | call_rcu(&epayload->rcu, encrypted_rcu_free); |
| 890 | out: |
| 891 | kfree(buf); |
| 892 | return ret; |
| 893 | } |
| 894 | |
| 895 | /* |
| 896 | * encrypted_read - format and copy the encrypted data to userspace |
| 897 | * |
| 898 | * The resulting datablob format is: |
| 899 | * <master-key name> <decrypted data length> <encrypted iv> <encrypted data> |
| 900 | * |
| 901 | * On success, return to userspace the encrypted key datablob size. |
| 902 | */ |
| 903 | static long encrypted_read(const struct key *key, char __user *buffer, |
| 904 | size_t buflen) |
| 905 | { |
| 906 | struct encrypted_key_payload *epayload; |
| 907 | struct key *mkey; |
| 908 | u8 *master_key; |
Mimi Zohar | 3b1826c | 2010-12-13 16:53:13 -0500 | [diff] [blame] | 909 | size_t master_keylen; |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 910 | char derived_key[HASH_SIZE]; |
| 911 | char *ascii_buf; |
| 912 | size_t asciiblob_len; |
| 913 | int ret; |
| 914 | |
David Howells | 633e804 | 2011-03-07 15:05:51 +0000 | [diff] [blame] | 915 | epayload = rcu_dereference_key(key); |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 916 | |
| 917 | /* returns the hex encoded iv, encrypted-data, and hmac as ascii */ |
| 918 | asciiblob_len = epayload->datablob_len + ivsize + 1 |
| 919 | + roundup(epayload->decrypted_datalen, blksize) |
| 920 | + (HASH_SIZE * 2); |
| 921 | |
| 922 | if (!buffer || buflen < asciiblob_len) |
| 923 | return asciiblob_len; |
| 924 | |
| 925 | mkey = request_master_key(epayload, &master_key, &master_keylen); |
| 926 | if (IS_ERR(mkey)) |
| 927 | return PTR_ERR(mkey); |
| 928 | |
| 929 | ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen); |
| 930 | if (ret < 0) |
| 931 | goto out; |
| 932 | |
| 933 | ret = derived_key_encrypt(epayload, derived_key, sizeof derived_key); |
| 934 | if (ret < 0) |
| 935 | goto out; |
| 936 | |
| 937 | ret = datablob_hmac_append(epayload, master_key, master_keylen); |
| 938 | if (ret < 0) |
| 939 | goto out; |
| 940 | |
| 941 | ascii_buf = datablob_format(epayload, asciiblob_len); |
| 942 | if (!ascii_buf) { |
| 943 | ret = -ENOMEM; |
| 944 | goto out; |
| 945 | } |
| 946 | |
| 947 | up_read(&mkey->sem); |
| 948 | key_put(mkey); |
| 949 | |
| 950 | if (copy_to_user(buffer, ascii_buf, asciiblob_len) != 0) |
| 951 | ret = -EFAULT; |
| 952 | kfree(ascii_buf); |
| 953 | |
| 954 | return asciiblob_len; |
| 955 | out: |
| 956 | up_read(&mkey->sem); |
| 957 | key_put(mkey); |
| 958 | return ret; |
| 959 | } |
| 960 | |
| 961 | /* |
| 962 | * encrypted_destroy - before freeing the key, clear the decrypted data |
| 963 | * |
| 964 | * Before freeing the key, clear the memory containing the decrypted |
| 965 | * key data. |
| 966 | */ |
| 967 | static void encrypted_destroy(struct key *key) |
| 968 | { |
| 969 | struct encrypted_key_payload *epayload = key->payload.data; |
| 970 | |
| 971 | if (!epayload) |
| 972 | return; |
| 973 | |
| 974 | memset(epayload->decrypted_data, 0, epayload->decrypted_datalen); |
| 975 | kfree(key->payload.data); |
| 976 | } |
| 977 | |
| 978 | struct key_type key_type_encrypted = { |
| 979 | .name = "encrypted", |
| 980 | .instantiate = encrypted_instantiate, |
| 981 | .update = encrypted_update, |
| 982 | .match = user_match, |
| 983 | .destroy = encrypted_destroy, |
| 984 | .describe = user_describe, |
| 985 | .read = encrypted_read, |
| 986 | }; |
| 987 | EXPORT_SYMBOL_GPL(key_type_encrypted); |
| 988 | |
| 989 | static void encrypted_shash_release(void) |
| 990 | { |
| 991 | if (hashalg) |
| 992 | crypto_free_shash(hashalg); |
| 993 | if (hmacalg) |
| 994 | crypto_free_shash(hmacalg); |
| 995 | } |
| 996 | |
| 997 | static int __init encrypted_shash_alloc(void) |
| 998 | { |
| 999 | int ret; |
| 1000 | |
| 1001 | hmacalg = crypto_alloc_shash(hmac_alg, 0, CRYPTO_ALG_ASYNC); |
| 1002 | if (IS_ERR(hmacalg)) { |
| 1003 | pr_info("encrypted_key: could not allocate crypto %s\n", |
| 1004 | hmac_alg); |
| 1005 | return PTR_ERR(hmacalg); |
| 1006 | } |
| 1007 | |
| 1008 | hashalg = crypto_alloc_shash(hash_alg, 0, CRYPTO_ALG_ASYNC); |
| 1009 | if (IS_ERR(hashalg)) { |
| 1010 | pr_info("encrypted_key: could not allocate crypto %s\n", |
| 1011 | hash_alg); |
| 1012 | ret = PTR_ERR(hashalg); |
| 1013 | goto hashalg_fail; |
| 1014 | } |
| 1015 | |
| 1016 | return 0; |
| 1017 | |
| 1018 | hashalg_fail: |
| 1019 | crypto_free_shash(hmacalg); |
| 1020 | return ret; |
| 1021 | } |
| 1022 | |
| 1023 | static int __init init_encrypted(void) |
| 1024 | { |
| 1025 | int ret; |
| 1026 | |
| 1027 | ret = encrypted_shash_alloc(); |
| 1028 | if (ret < 0) |
| 1029 | return ret; |
| 1030 | ret = register_key_type(&key_type_encrypted); |
| 1031 | if (ret < 0) |
| 1032 | goto out; |
| 1033 | return aes_get_sizes(); |
| 1034 | out: |
| 1035 | encrypted_shash_release(); |
| 1036 | return ret; |
Mimi Zohar | b970344 | 2011-01-18 09:07:12 -0500 | [diff] [blame] | 1037 | |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | static void __exit cleanup_encrypted(void) |
| 1041 | { |
| 1042 | encrypted_shash_release(); |
| 1043 | unregister_key_type(&key_type_encrypted); |
| 1044 | } |
| 1045 | |
| 1046 | late_initcall(init_encrypted); |
| 1047 | module_exit(cleanup_encrypted); |
| 1048 | |
| 1049 | MODULE_LICENSE("GPL"); |