Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 IBM Corporation |
| 3 | * |
| 4 | * Author: |
| 5 | * Mimi Zohar <zohar@us.ibm.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation, version 2 of the License. |
| 10 | * |
Randy Dunlap | d410fa4 | 2011-05-19 15:59:38 -0700 | [diff] [blame] | 11 | * See Documentation/security/keys-trusted-encrypted.txt |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <linux/uaccess.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/parser.h> |
| 19 | #include <linux/string.h> |
Mimi Zohar | 93ae86e | 2010-11-29 16:20:04 -0500 | [diff] [blame] | 20 | #include <linux/err.h> |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 21 | #include <keys/user-type.h> |
| 22 | #include <keys/trusted-type.h> |
| 23 | #include <keys/encrypted-type.h> |
| 24 | #include <linux/key-type.h> |
| 25 | #include <linux/random.h> |
| 26 | #include <linux/rcupdate.h> |
| 27 | #include <linux/scatterlist.h> |
| 28 | #include <linux/crypto.h> |
| 29 | #include <crypto/hash.h> |
| 30 | #include <crypto/sha.h> |
| 31 | #include <crypto/aes.h> |
| 32 | |
Mimi Zohar | b970344 | 2011-01-18 09:07:12 -0500 | [diff] [blame] | 33 | #include "encrypted.h" |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 34 | |
Mimi Zohar | 3b1826c | 2010-12-13 16:53:13 -0500 | [diff] [blame] | 35 | static const char KEY_TRUSTED_PREFIX[] = "trusted:"; |
| 36 | static const char KEY_USER_PREFIX[] = "user:"; |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 37 | static const char hash_alg[] = "sha256"; |
| 38 | static const char hmac_alg[] = "hmac(sha256)"; |
| 39 | static const char blkcipher_alg[] = "cbc(aes)"; |
| 40 | static unsigned int ivsize; |
| 41 | static int blksize; |
| 42 | |
Mimi Zohar | 3b1826c | 2010-12-13 16:53:13 -0500 | [diff] [blame] | 43 | #define KEY_TRUSTED_PREFIX_LEN (sizeof (KEY_TRUSTED_PREFIX) - 1) |
| 44 | #define KEY_USER_PREFIX_LEN (sizeof (KEY_USER_PREFIX) - 1) |
| 45 | #define HASH_SIZE SHA256_DIGEST_SIZE |
| 46 | #define MAX_DATA_SIZE 4096 |
| 47 | #define MIN_DATA_SIZE 20 |
| 48 | |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 49 | struct sdesc { |
| 50 | struct shash_desc shash; |
| 51 | char ctx[]; |
| 52 | }; |
| 53 | |
| 54 | static struct crypto_shash *hashalg; |
| 55 | static struct crypto_shash *hmacalg; |
| 56 | |
| 57 | enum { |
| 58 | Opt_err = -1, Opt_new, Opt_load, Opt_update |
| 59 | }; |
| 60 | |
| 61 | static const match_table_t key_tokens = { |
| 62 | {Opt_new, "new"}, |
| 63 | {Opt_load, "load"}, |
| 64 | {Opt_update, "update"}, |
| 65 | {Opt_err, NULL} |
| 66 | }; |
| 67 | |
| 68 | static int aes_get_sizes(void) |
| 69 | { |
| 70 | struct crypto_blkcipher *tfm; |
| 71 | |
| 72 | tfm = crypto_alloc_blkcipher(blkcipher_alg, 0, CRYPTO_ALG_ASYNC); |
| 73 | if (IS_ERR(tfm)) { |
| 74 | pr_err("encrypted_key: failed to alloc_cipher (%ld)\n", |
| 75 | PTR_ERR(tfm)); |
| 76 | return PTR_ERR(tfm); |
| 77 | } |
| 78 | ivsize = crypto_blkcipher_ivsize(tfm); |
| 79 | blksize = crypto_blkcipher_blocksize(tfm); |
| 80 | crypto_free_blkcipher(tfm); |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | * valid_master_desc - verify the 'key-type:desc' of a new/updated master-key |
| 86 | * |
| 87 | * key-type:= "trusted:" | "encrypted:" |
| 88 | * desc:= master-key description |
| 89 | * |
| 90 | * Verify that 'key-type' is valid and that 'desc' exists. On key update, |
| 91 | * only the master key description is permitted to change, not the key-type. |
| 92 | * The key-type remains constant. |
| 93 | * |
| 94 | * On success returns 0, otherwise -EINVAL. |
| 95 | */ |
| 96 | static int valid_master_desc(const char *new_desc, const char *orig_desc) |
| 97 | { |
| 98 | if (!memcmp(new_desc, KEY_TRUSTED_PREFIX, KEY_TRUSTED_PREFIX_LEN)) { |
| 99 | if (strlen(new_desc) == KEY_TRUSTED_PREFIX_LEN) |
| 100 | goto out; |
| 101 | if (orig_desc) |
| 102 | if (memcmp(new_desc, orig_desc, KEY_TRUSTED_PREFIX_LEN)) |
| 103 | goto out; |
| 104 | } else if (!memcmp(new_desc, KEY_USER_PREFIX, KEY_USER_PREFIX_LEN)) { |
| 105 | if (strlen(new_desc) == KEY_USER_PREFIX_LEN) |
| 106 | goto out; |
| 107 | if (orig_desc) |
| 108 | if (memcmp(new_desc, orig_desc, KEY_USER_PREFIX_LEN)) |
| 109 | goto out; |
| 110 | } else |
| 111 | goto out; |
| 112 | return 0; |
| 113 | out: |
| 114 | return -EINVAL; |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * datablob_parse - parse the keyctl data |
| 119 | * |
| 120 | * datablob format: |
| 121 | * new <master-key name> <decrypted data length> |
| 122 | * load <master-key name> <decrypted data length> <encrypted iv + data> |
| 123 | * update <new-master-key name> |
| 124 | * |
| 125 | * Tokenizes a copy of the keyctl data, returning a pointer to each token, |
| 126 | * which is null terminated. |
| 127 | * |
| 128 | * On success returns 0, otherwise -EINVAL. |
| 129 | */ |
| 130 | static int datablob_parse(char *datablob, char **master_desc, |
Mimi Zohar | 1f35065 | 2010-12-13 16:53:12 -0500 | [diff] [blame] | 131 | char **decrypted_datalen, char **hex_encoded_iv) |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 132 | { |
| 133 | substring_t args[MAX_OPT_ARGS]; |
| 134 | int ret = -EINVAL; |
| 135 | int key_cmd; |
| 136 | char *p; |
| 137 | |
| 138 | p = strsep(&datablob, " \t"); |
| 139 | if (!p) |
| 140 | return ret; |
| 141 | key_cmd = match_token(p, key_tokens, args); |
| 142 | |
| 143 | *master_desc = strsep(&datablob, " \t"); |
| 144 | if (!*master_desc) |
| 145 | goto out; |
| 146 | |
| 147 | if (valid_master_desc(*master_desc, NULL) < 0) |
| 148 | goto out; |
| 149 | |
| 150 | if (decrypted_datalen) { |
| 151 | *decrypted_datalen = strsep(&datablob, " \t"); |
| 152 | if (!*decrypted_datalen) |
| 153 | goto out; |
| 154 | } |
| 155 | |
| 156 | switch (key_cmd) { |
| 157 | case Opt_new: |
| 158 | if (!decrypted_datalen) |
| 159 | break; |
| 160 | ret = 0; |
| 161 | break; |
| 162 | case Opt_load: |
| 163 | if (!decrypted_datalen) |
| 164 | break; |
| 165 | *hex_encoded_iv = strsep(&datablob, " \t"); |
| 166 | if (!*hex_encoded_iv) |
| 167 | break; |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 168 | ret = 0; |
| 169 | break; |
| 170 | case Opt_update: |
| 171 | if (decrypted_datalen) |
| 172 | break; |
| 173 | ret = 0; |
| 174 | break; |
| 175 | case Opt_err: |
| 176 | break; |
| 177 | } |
| 178 | out: |
| 179 | return ret; |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * datablob_format - format as an ascii string, before copying to userspace |
| 184 | */ |
| 185 | static char *datablob_format(struct encrypted_key_payload *epayload, |
| 186 | size_t asciiblob_len) |
| 187 | { |
| 188 | char *ascii_buf, *bufp; |
| 189 | u8 *iv = epayload->iv; |
| 190 | int len; |
| 191 | int i; |
| 192 | |
| 193 | ascii_buf = kmalloc(asciiblob_len + 1, GFP_KERNEL); |
| 194 | if (!ascii_buf) |
| 195 | goto out; |
| 196 | |
| 197 | ascii_buf[asciiblob_len] = '\0'; |
| 198 | |
| 199 | /* copy datablob master_desc and datalen strings */ |
| 200 | len = sprintf(ascii_buf, "%s %s ", epayload->master_desc, |
| 201 | epayload->datalen); |
| 202 | |
| 203 | /* convert the hex encoded iv, encrypted-data and HMAC to ascii */ |
| 204 | bufp = &ascii_buf[len]; |
| 205 | for (i = 0; i < (asciiblob_len - len) / 2; i++) |
| 206 | bufp = pack_hex_byte(bufp, iv[i]); |
| 207 | out: |
| 208 | return ascii_buf; |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | * request_trusted_key - request the trusted key |
| 213 | * |
| 214 | * Trusted keys are sealed to PCRs and other metadata. Although userspace |
| 215 | * manages both trusted/encrypted key-types, like the encrypted key type |
| 216 | * data, trusted key type data is not visible decrypted from userspace. |
| 217 | */ |
| 218 | static struct key *request_trusted_key(const char *trusted_desc, |
Mimi Zohar | 3b1826c | 2010-12-13 16:53:13 -0500 | [diff] [blame] | 219 | u8 **master_key, size_t *master_keylen) |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 220 | { |
| 221 | struct trusted_key_payload *tpayload; |
| 222 | struct key *tkey; |
| 223 | |
| 224 | tkey = request_key(&key_type_trusted, trusted_desc, NULL); |
| 225 | if (IS_ERR(tkey)) |
| 226 | goto error; |
| 227 | |
| 228 | down_read(&tkey->sem); |
| 229 | tpayload = rcu_dereference(tkey->payload.data); |
| 230 | *master_key = tpayload->key; |
| 231 | *master_keylen = tpayload->key_len; |
| 232 | error: |
| 233 | return tkey; |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * request_user_key - request the user key |
| 238 | * |
| 239 | * Use a user provided key to encrypt/decrypt an encrypted-key. |
| 240 | */ |
| 241 | 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] | 242 | size_t *master_keylen) |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 243 | { |
| 244 | struct user_key_payload *upayload; |
| 245 | struct key *ukey; |
| 246 | |
| 247 | ukey = request_key(&key_type_user, master_desc, NULL); |
| 248 | if (IS_ERR(ukey)) |
| 249 | goto error; |
| 250 | |
| 251 | down_read(&ukey->sem); |
| 252 | upayload = rcu_dereference(ukey->payload.data); |
| 253 | *master_key = upayload->data; |
| 254 | *master_keylen = upayload->datalen; |
| 255 | error: |
| 256 | return ukey; |
| 257 | } |
| 258 | |
Mimi Zohar | 3b1826c | 2010-12-13 16:53:13 -0500 | [diff] [blame] | 259 | static struct sdesc *alloc_sdesc(struct crypto_shash *alg) |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 260 | { |
| 261 | struct sdesc *sdesc; |
| 262 | int size; |
| 263 | |
| 264 | size = sizeof(struct shash_desc) + crypto_shash_descsize(alg); |
| 265 | sdesc = kmalloc(size, GFP_KERNEL); |
| 266 | if (!sdesc) |
| 267 | return ERR_PTR(-ENOMEM); |
| 268 | sdesc->shash.tfm = alg; |
| 269 | sdesc->shash.flags = 0x0; |
| 270 | return sdesc; |
| 271 | } |
| 272 | |
Mimi Zohar | 3b1826c | 2010-12-13 16:53:13 -0500 | [diff] [blame] | 273 | static int calc_hmac(u8 *digest, const u8 *key, unsigned int keylen, |
| 274 | const u8 *buf, unsigned int buflen) |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 275 | { |
| 276 | struct sdesc *sdesc; |
| 277 | int ret; |
| 278 | |
Mimi Zohar | 3b1826c | 2010-12-13 16:53:13 -0500 | [diff] [blame] | 279 | sdesc = alloc_sdesc(hmacalg); |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 280 | if (IS_ERR(sdesc)) { |
| 281 | pr_info("encrypted_key: can't alloc %s\n", hmac_alg); |
| 282 | return PTR_ERR(sdesc); |
| 283 | } |
| 284 | |
| 285 | ret = crypto_shash_setkey(hmacalg, key, keylen); |
| 286 | if (!ret) |
| 287 | ret = crypto_shash_digest(&sdesc->shash, buf, buflen, digest); |
| 288 | kfree(sdesc); |
| 289 | return ret; |
| 290 | } |
| 291 | |
Mimi Zohar | 3b1826c | 2010-12-13 16:53:13 -0500 | [diff] [blame] | 292 | static int calc_hash(u8 *digest, const u8 *buf, unsigned int buflen) |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 293 | { |
| 294 | struct sdesc *sdesc; |
| 295 | int ret; |
| 296 | |
Mimi Zohar | 3b1826c | 2010-12-13 16:53:13 -0500 | [diff] [blame] | 297 | sdesc = alloc_sdesc(hashalg); |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 298 | if (IS_ERR(sdesc)) { |
| 299 | pr_info("encrypted_key: can't alloc %s\n", hash_alg); |
| 300 | return PTR_ERR(sdesc); |
| 301 | } |
| 302 | |
| 303 | ret = crypto_shash_digest(&sdesc->shash, buf, buflen, digest); |
| 304 | kfree(sdesc); |
| 305 | return ret; |
| 306 | } |
| 307 | |
| 308 | enum derived_key_type { ENC_KEY, AUTH_KEY }; |
| 309 | |
| 310 | /* Derive authentication/encryption key from trusted key */ |
| 311 | 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] | 312 | const u8 *master_key, size_t master_keylen) |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 313 | { |
| 314 | u8 *derived_buf; |
| 315 | unsigned int derived_buf_len; |
| 316 | int ret; |
| 317 | |
| 318 | derived_buf_len = strlen("AUTH_KEY") + 1 + master_keylen; |
| 319 | if (derived_buf_len < HASH_SIZE) |
| 320 | derived_buf_len = HASH_SIZE; |
| 321 | |
| 322 | derived_buf = kzalloc(derived_buf_len, GFP_KERNEL); |
| 323 | if (!derived_buf) { |
| 324 | pr_err("encrypted_key: out of memory\n"); |
| 325 | return -ENOMEM; |
| 326 | } |
| 327 | if (key_type) |
| 328 | strcpy(derived_buf, "AUTH_KEY"); |
| 329 | else |
| 330 | strcpy(derived_buf, "ENC_KEY"); |
| 331 | |
| 332 | memcpy(derived_buf + strlen(derived_buf) + 1, master_key, |
| 333 | master_keylen); |
| 334 | ret = calc_hash(derived_key, derived_buf, derived_buf_len); |
| 335 | kfree(derived_buf); |
| 336 | return ret; |
| 337 | } |
| 338 | |
| 339 | static int init_blkcipher_desc(struct blkcipher_desc *desc, const u8 *key, |
Mimi Zohar | 3b1826c | 2010-12-13 16:53:13 -0500 | [diff] [blame] | 340 | unsigned int key_len, const u8 *iv, |
| 341 | unsigned int ivsize) |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 342 | { |
| 343 | int ret; |
| 344 | |
| 345 | desc->tfm = crypto_alloc_blkcipher(blkcipher_alg, 0, CRYPTO_ALG_ASYNC); |
| 346 | if (IS_ERR(desc->tfm)) { |
| 347 | pr_err("encrypted_key: failed to load %s transform (%ld)\n", |
| 348 | blkcipher_alg, PTR_ERR(desc->tfm)); |
| 349 | return PTR_ERR(desc->tfm); |
| 350 | } |
| 351 | desc->flags = 0; |
| 352 | |
| 353 | ret = crypto_blkcipher_setkey(desc->tfm, key, key_len); |
| 354 | if (ret < 0) { |
| 355 | pr_err("encrypted_key: failed to setkey (%d)\n", ret); |
| 356 | crypto_free_blkcipher(desc->tfm); |
| 357 | return ret; |
| 358 | } |
| 359 | crypto_blkcipher_set_iv(desc->tfm, iv, ivsize); |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | static struct key *request_master_key(struct encrypted_key_payload *epayload, |
Mimi Zohar | 3b1826c | 2010-12-13 16:53:13 -0500 | [diff] [blame] | 364 | u8 **master_key, size_t *master_keylen) |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 365 | { |
| 366 | struct key *mkey = NULL; |
| 367 | |
| 368 | if (!strncmp(epayload->master_desc, KEY_TRUSTED_PREFIX, |
| 369 | KEY_TRUSTED_PREFIX_LEN)) { |
| 370 | mkey = request_trusted_key(epayload->master_desc + |
| 371 | KEY_TRUSTED_PREFIX_LEN, |
| 372 | master_key, master_keylen); |
| 373 | } else if (!strncmp(epayload->master_desc, KEY_USER_PREFIX, |
| 374 | KEY_USER_PREFIX_LEN)) { |
| 375 | mkey = request_user_key(epayload->master_desc + |
| 376 | KEY_USER_PREFIX_LEN, |
| 377 | master_key, master_keylen); |
| 378 | } else |
| 379 | goto out; |
| 380 | |
Roberto Sassu | f91c2c5 | 2011-06-27 13:45:39 +0200 | [diff] [blame^] | 381 | if (IS_ERR(mkey)) { |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 382 | pr_info("encrypted_key: key %s not found", |
| 383 | epayload->master_desc); |
Roberto Sassu | f91c2c5 | 2011-06-27 13:45:39 +0200 | [diff] [blame^] | 384 | goto out; |
| 385 | } |
| 386 | |
| 387 | dump_master_key(*master_key, *master_keylen); |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 388 | out: |
| 389 | return mkey; |
| 390 | } |
| 391 | |
| 392 | /* Before returning data to userspace, encrypt decrypted data. */ |
| 393 | static int derived_key_encrypt(struct encrypted_key_payload *epayload, |
| 394 | const u8 *derived_key, |
Mimi Zohar | 3b1826c | 2010-12-13 16:53:13 -0500 | [diff] [blame] | 395 | unsigned int derived_keylen) |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 396 | { |
| 397 | struct scatterlist sg_in[2]; |
| 398 | struct scatterlist sg_out[1]; |
| 399 | struct blkcipher_desc desc; |
| 400 | unsigned int encrypted_datalen; |
| 401 | unsigned int padlen; |
| 402 | char pad[16]; |
| 403 | int ret; |
| 404 | |
| 405 | encrypted_datalen = roundup(epayload->decrypted_datalen, blksize); |
| 406 | padlen = encrypted_datalen - epayload->decrypted_datalen; |
| 407 | |
| 408 | ret = init_blkcipher_desc(&desc, derived_key, derived_keylen, |
| 409 | epayload->iv, ivsize); |
| 410 | if (ret < 0) |
| 411 | goto out; |
| 412 | dump_decrypted_data(epayload); |
| 413 | |
| 414 | memset(pad, 0, sizeof pad); |
| 415 | sg_init_table(sg_in, 2); |
| 416 | sg_set_buf(&sg_in[0], epayload->decrypted_data, |
| 417 | epayload->decrypted_datalen); |
| 418 | sg_set_buf(&sg_in[1], pad, padlen); |
| 419 | |
| 420 | sg_init_table(sg_out, 1); |
| 421 | sg_set_buf(sg_out, epayload->encrypted_data, encrypted_datalen); |
| 422 | |
| 423 | ret = crypto_blkcipher_encrypt(&desc, sg_out, sg_in, encrypted_datalen); |
| 424 | crypto_free_blkcipher(desc.tfm); |
| 425 | if (ret < 0) |
| 426 | pr_err("encrypted_key: failed to encrypt (%d)\n", ret); |
| 427 | else |
| 428 | dump_encrypted_data(epayload, encrypted_datalen); |
| 429 | out: |
| 430 | return ret; |
| 431 | } |
| 432 | |
| 433 | static int datablob_hmac_append(struct encrypted_key_payload *epayload, |
Mimi Zohar | 3b1826c | 2010-12-13 16:53:13 -0500 | [diff] [blame] | 434 | const u8 *master_key, size_t master_keylen) |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 435 | { |
| 436 | u8 derived_key[HASH_SIZE]; |
| 437 | u8 *digest; |
| 438 | int ret; |
| 439 | |
| 440 | ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen); |
| 441 | if (ret < 0) |
| 442 | goto out; |
| 443 | |
| 444 | digest = epayload->master_desc + epayload->datablob_len; |
| 445 | ret = calc_hmac(digest, derived_key, sizeof derived_key, |
| 446 | epayload->master_desc, epayload->datablob_len); |
| 447 | if (!ret) |
| 448 | dump_hmac(NULL, digest, HASH_SIZE); |
| 449 | out: |
| 450 | return ret; |
| 451 | } |
| 452 | |
| 453 | /* verify HMAC before decrypting encrypted key */ |
| 454 | static int datablob_hmac_verify(struct encrypted_key_payload *epayload, |
Mimi Zohar | 3b1826c | 2010-12-13 16:53:13 -0500 | [diff] [blame] | 455 | const u8 *master_key, size_t master_keylen) |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 456 | { |
| 457 | u8 derived_key[HASH_SIZE]; |
| 458 | u8 digest[HASH_SIZE]; |
| 459 | int ret; |
| 460 | |
| 461 | ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen); |
| 462 | if (ret < 0) |
| 463 | goto out; |
| 464 | |
| 465 | ret = calc_hmac(digest, derived_key, sizeof derived_key, |
| 466 | epayload->master_desc, epayload->datablob_len); |
| 467 | if (ret < 0) |
| 468 | goto out; |
| 469 | ret = memcmp(digest, epayload->master_desc + epayload->datablob_len, |
| 470 | sizeof digest); |
| 471 | if (ret) { |
| 472 | ret = -EINVAL; |
| 473 | dump_hmac("datablob", |
| 474 | epayload->master_desc + epayload->datablob_len, |
| 475 | HASH_SIZE); |
| 476 | dump_hmac("calc", digest, HASH_SIZE); |
| 477 | } |
| 478 | out: |
| 479 | return ret; |
| 480 | } |
| 481 | |
| 482 | static int derived_key_decrypt(struct encrypted_key_payload *epayload, |
| 483 | const u8 *derived_key, |
Mimi Zohar | 3b1826c | 2010-12-13 16:53:13 -0500 | [diff] [blame] | 484 | unsigned int derived_keylen) |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 485 | { |
| 486 | struct scatterlist sg_in[1]; |
| 487 | struct scatterlist sg_out[2]; |
| 488 | struct blkcipher_desc desc; |
| 489 | unsigned int encrypted_datalen; |
| 490 | char pad[16]; |
| 491 | int ret; |
| 492 | |
| 493 | encrypted_datalen = roundup(epayload->decrypted_datalen, blksize); |
| 494 | ret = init_blkcipher_desc(&desc, derived_key, derived_keylen, |
| 495 | epayload->iv, ivsize); |
| 496 | if (ret < 0) |
| 497 | goto out; |
| 498 | dump_encrypted_data(epayload, encrypted_datalen); |
| 499 | |
| 500 | memset(pad, 0, sizeof pad); |
| 501 | sg_init_table(sg_in, 1); |
| 502 | sg_init_table(sg_out, 2); |
| 503 | sg_set_buf(sg_in, epayload->encrypted_data, encrypted_datalen); |
| 504 | sg_set_buf(&sg_out[0], epayload->decrypted_data, |
Mimi Zohar | 3b1826c | 2010-12-13 16:53:13 -0500 | [diff] [blame] | 505 | epayload->decrypted_datalen); |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 506 | sg_set_buf(&sg_out[1], pad, sizeof pad); |
| 507 | |
| 508 | ret = crypto_blkcipher_decrypt(&desc, sg_out, sg_in, encrypted_datalen); |
| 509 | crypto_free_blkcipher(desc.tfm); |
| 510 | if (ret < 0) |
| 511 | goto out; |
| 512 | dump_decrypted_data(epayload); |
| 513 | out: |
| 514 | return ret; |
| 515 | } |
| 516 | |
| 517 | /* Allocate memory for decrypted key and datablob. */ |
| 518 | static struct encrypted_key_payload *encrypted_key_alloc(struct key *key, |
| 519 | const char *master_desc, |
| 520 | const char *datalen) |
| 521 | { |
| 522 | struct encrypted_key_payload *epayload = NULL; |
| 523 | unsigned short datablob_len; |
| 524 | unsigned short decrypted_datalen; |
| 525 | unsigned int encrypted_datalen; |
| 526 | long dlen; |
| 527 | int ret; |
| 528 | |
| 529 | ret = strict_strtol(datalen, 10, &dlen); |
| 530 | if (ret < 0 || dlen < MIN_DATA_SIZE || dlen > MAX_DATA_SIZE) |
| 531 | return ERR_PTR(-EINVAL); |
| 532 | |
| 533 | decrypted_datalen = dlen; |
| 534 | encrypted_datalen = roundup(decrypted_datalen, blksize); |
| 535 | |
| 536 | datablob_len = strlen(master_desc) + 1 + strlen(datalen) + 1 |
| 537 | + ivsize + 1 + encrypted_datalen; |
| 538 | |
| 539 | ret = key_payload_reserve(key, decrypted_datalen + datablob_len |
| 540 | + HASH_SIZE + 1); |
| 541 | if (ret < 0) |
| 542 | return ERR_PTR(ret); |
| 543 | |
| 544 | epayload = kzalloc(sizeof(*epayload) + decrypted_datalen + |
| 545 | datablob_len + HASH_SIZE + 1, GFP_KERNEL); |
| 546 | if (!epayload) |
| 547 | return ERR_PTR(-ENOMEM); |
| 548 | |
| 549 | epayload->decrypted_datalen = decrypted_datalen; |
| 550 | epayload->datablob_len = datablob_len; |
| 551 | return epayload; |
| 552 | } |
| 553 | |
| 554 | static int encrypted_key_decrypt(struct encrypted_key_payload *epayload, |
Mimi Zohar | 1f35065 | 2010-12-13 16:53:12 -0500 | [diff] [blame] | 555 | const char *hex_encoded_iv) |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 556 | { |
| 557 | struct key *mkey; |
| 558 | u8 derived_key[HASH_SIZE]; |
| 559 | u8 *master_key; |
| 560 | u8 *hmac; |
Mimi Zohar | 1f35065 | 2010-12-13 16:53:12 -0500 | [diff] [blame] | 561 | const char *hex_encoded_data; |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 562 | unsigned int encrypted_datalen; |
Mimi Zohar | 3b1826c | 2010-12-13 16:53:13 -0500 | [diff] [blame] | 563 | size_t master_keylen; |
Mimi Zohar | 1f35065 | 2010-12-13 16:53:12 -0500 | [diff] [blame] | 564 | size_t asciilen; |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 565 | int ret; |
| 566 | |
| 567 | encrypted_datalen = roundup(epayload->decrypted_datalen, blksize); |
Mimi Zohar | 1f35065 | 2010-12-13 16:53:12 -0500 | [diff] [blame] | 568 | asciilen = (ivsize + 1 + encrypted_datalen + HASH_SIZE) * 2; |
| 569 | if (strlen(hex_encoded_iv) != asciilen) |
| 570 | return -EINVAL; |
| 571 | |
| 572 | hex_encoded_data = hex_encoded_iv + (2 * ivsize) + 2; |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 573 | hex2bin(epayload->iv, hex_encoded_iv, ivsize); |
| 574 | hex2bin(epayload->encrypted_data, hex_encoded_data, encrypted_datalen); |
| 575 | |
| 576 | hmac = epayload->master_desc + epayload->datablob_len; |
| 577 | hex2bin(hmac, hex_encoded_data + (encrypted_datalen * 2), HASH_SIZE); |
| 578 | |
| 579 | mkey = request_master_key(epayload, &master_key, &master_keylen); |
| 580 | if (IS_ERR(mkey)) |
| 581 | return PTR_ERR(mkey); |
| 582 | |
| 583 | ret = datablob_hmac_verify(epayload, master_key, master_keylen); |
| 584 | if (ret < 0) { |
| 585 | pr_err("encrypted_key: bad hmac (%d)\n", ret); |
| 586 | goto out; |
| 587 | } |
| 588 | |
| 589 | ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen); |
| 590 | if (ret < 0) |
| 591 | goto out; |
| 592 | |
| 593 | ret = derived_key_decrypt(epayload, derived_key, sizeof derived_key); |
| 594 | if (ret < 0) |
| 595 | pr_err("encrypted_key: failed to decrypt key (%d)\n", ret); |
| 596 | out: |
| 597 | up_read(&mkey->sem); |
| 598 | key_put(mkey); |
| 599 | return ret; |
| 600 | } |
| 601 | |
| 602 | static void __ekey_init(struct encrypted_key_payload *epayload, |
| 603 | const char *master_desc, const char *datalen) |
| 604 | { |
| 605 | epayload->master_desc = epayload->decrypted_data |
| 606 | + epayload->decrypted_datalen; |
| 607 | epayload->datalen = epayload->master_desc + strlen(master_desc) + 1; |
| 608 | epayload->iv = epayload->datalen + strlen(datalen) + 1; |
| 609 | epayload->encrypted_data = epayload->iv + ivsize + 1; |
| 610 | |
| 611 | memcpy(epayload->master_desc, master_desc, strlen(master_desc)); |
| 612 | memcpy(epayload->datalen, datalen, strlen(datalen)); |
| 613 | } |
| 614 | |
| 615 | /* |
| 616 | * encrypted_init - initialize an encrypted key |
| 617 | * |
| 618 | * For a new key, use a random number for both the iv and data |
| 619 | * itself. For an old key, decrypt the hex encoded data. |
| 620 | */ |
| 621 | static int encrypted_init(struct encrypted_key_payload *epayload, |
| 622 | const char *master_desc, const char *datalen, |
Mimi Zohar | 1f35065 | 2010-12-13 16:53:12 -0500 | [diff] [blame] | 623 | const char *hex_encoded_iv) |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 624 | { |
| 625 | int ret = 0; |
| 626 | |
| 627 | __ekey_init(epayload, master_desc, datalen); |
Mimi Zohar | 1f35065 | 2010-12-13 16:53:12 -0500 | [diff] [blame] | 628 | if (!hex_encoded_iv) { |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 629 | get_random_bytes(epayload->iv, ivsize); |
| 630 | |
| 631 | get_random_bytes(epayload->decrypted_data, |
| 632 | epayload->decrypted_datalen); |
| 633 | } else |
Mimi Zohar | 1f35065 | 2010-12-13 16:53:12 -0500 | [diff] [blame] | 634 | ret = encrypted_key_decrypt(epayload, hex_encoded_iv); |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 635 | return ret; |
| 636 | } |
| 637 | |
| 638 | /* |
| 639 | * encrypted_instantiate - instantiate an encrypted key |
| 640 | * |
| 641 | * Decrypt an existing encrypted datablob or create a new encrypted key |
| 642 | * based on a kernel random number. |
| 643 | * |
| 644 | * On success, return 0. Otherwise return errno. |
| 645 | */ |
| 646 | static int encrypted_instantiate(struct key *key, const void *data, |
| 647 | size_t datalen) |
| 648 | { |
| 649 | struct encrypted_key_payload *epayload = NULL; |
| 650 | char *datablob = NULL; |
| 651 | char *master_desc = NULL; |
| 652 | char *decrypted_datalen = NULL; |
| 653 | char *hex_encoded_iv = NULL; |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 654 | int ret; |
| 655 | |
| 656 | if (datalen <= 0 || datalen > 32767 || !data) |
| 657 | return -EINVAL; |
| 658 | |
| 659 | datablob = kmalloc(datalen + 1, GFP_KERNEL); |
| 660 | if (!datablob) |
| 661 | return -ENOMEM; |
| 662 | datablob[datalen] = 0; |
| 663 | memcpy(datablob, data, datalen); |
| 664 | ret = datablob_parse(datablob, &master_desc, &decrypted_datalen, |
Mimi Zohar | 1f35065 | 2010-12-13 16:53:12 -0500 | [diff] [blame] | 665 | &hex_encoded_iv); |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 666 | if (ret < 0) |
| 667 | goto out; |
| 668 | |
| 669 | epayload = encrypted_key_alloc(key, master_desc, decrypted_datalen); |
| 670 | if (IS_ERR(epayload)) { |
| 671 | ret = PTR_ERR(epayload); |
| 672 | goto out; |
| 673 | } |
| 674 | ret = encrypted_init(epayload, master_desc, decrypted_datalen, |
Mimi Zohar | 1f35065 | 2010-12-13 16:53:12 -0500 | [diff] [blame] | 675 | hex_encoded_iv); |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 676 | if (ret < 0) { |
| 677 | kfree(epayload); |
| 678 | goto out; |
| 679 | } |
| 680 | |
| 681 | rcu_assign_pointer(key->payload.data, epayload); |
| 682 | out: |
| 683 | kfree(datablob); |
| 684 | return ret; |
| 685 | } |
| 686 | |
| 687 | static void encrypted_rcu_free(struct rcu_head *rcu) |
| 688 | { |
| 689 | struct encrypted_key_payload *epayload; |
| 690 | |
| 691 | epayload = container_of(rcu, struct encrypted_key_payload, rcu); |
| 692 | memset(epayload->decrypted_data, 0, epayload->decrypted_datalen); |
| 693 | kfree(epayload); |
| 694 | } |
| 695 | |
| 696 | /* |
| 697 | * encrypted_update - update the master key description |
| 698 | * |
| 699 | * Change the master key description for an existing encrypted key. |
| 700 | * The next read will return an encrypted datablob using the new |
| 701 | * master key description. |
| 702 | * |
| 703 | * On success, return 0. Otherwise return errno. |
| 704 | */ |
| 705 | static int encrypted_update(struct key *key, const void *data, size_t datalen) |
| 706 | { |
| 707 | struct encrypted_key_payload *epayload = key->payload.data; |
| 708 | struct encrypted_key_payload *new_epayload; |
| 709 | char *buf; |
| 710 | char *new_master_desc = NULL; |
| 711 | int ret = 0; |
| 712 | |
| 713 | if (datalen <= 0 || datalen > 32767 || !data) |
| 714 | return -EINVAL; |
| 715 | |
| 716 | buf = kmalloc(datalen + 1, GFP_KERNEL); |
| 717 | if (!buf) |
| 718 | return -ENOMEM; |
| 719 | |
| 720 | buf[datalen] = 0; |
| 721 | memcpy(buf, data, datalen); |
Mimi Zohar | 1f35065 | 2010-12-13 16:53:12 -0500 | [diff] [blame] | 722 | ret = datablob_parse(buf, &new_master_desc, NULL, NULL); |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 723 | if (ret < 0) |
| 724 | goto out; |
| 725 | |
| 726 | ret = valid_master_desc(new_master_desc, epayload->master_desc); |
| 727 | if (ret < 0) |
| 728 | goto out; |
| 729 | |
| 730 | new_epayload = encrypted_key_alloc(key, new_master_desc, |
| 731 | epayload->datalen); |
| 732 | if (IS_ERR(new_epayload)) { |
| 733 | ret = PTR_ERR(new_epayload); |
| 734 | goto out; |
| 735 | } |
| 736 | |
| 737 | __ekey_init(new_epayload, new_master_desc, epayload->datalen); |
| 738 | |
| 739 | memcpy(new_epayload->iv, epayload->iv, ivsize); |
| 740 | memcpy(new_epayload->decrypted_data, epayload->decrypted_data, |
| 741 | epayload->decrypted_datalen); |
| 742 | |
| 743 | rcu_assign_pointer(key->payload.data, new_epayload); |
| 744 | call_rcu(&epayload->rcu, encrypted_rcu_free); |
| 745 | out: |
| 746 | kfree(buf); |
| 747 | return ret; |
| 748 | } |
| 749 | |
| 750 | /* |
| 751 | * encrypted_read - format and copy the encrypted data to userspace |
| 752 | * |
| 753 | * The resulting datablob format is: |
| 754 | * <master-key name> <decrypted data length> <encrypted iv> <encrypted data> |
| 755 | * |
| 756 | * On success, return to userspace the encrypted key datablob size. |
| 757 | */ |
| 758 | static long encrypted_read(const struct key *key, char __user *buffer, |
| 759 | size_t buflen) |
| 760 | { |
| 761 | struct encrypted_key_payload *epayload; |
| 762 | struct key *mkey; |
| 763 | u8 *master_key; |
Mimi Zohar | 3b1826c | 2010-12-13 16:53:13 -0500 | [diff] [blame] | 764 | size_t master_keylen; |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 765 | char derived_key[HASH_SIZE]; |
| 766 | char *ascii_buf; |
| 767 | size_t asciiblob_len; |
| 768 | int ret; |
| 769 | |
David Howells | 633e804 | 2011-03-07 15:05:51 +0000 | [diff] [blame] | 770 | epayload = rcu_dereference_key(key); |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 771 | |
| 772 | /* returns the hex encoded iv, encrypted-data, and hmac as ascii */ |
| 773 | asciiblob_len = epayload->datablob_len + ivsize + 1 |
| 774 | + roundup(epayload->decrypted_datalen, blksize) |
| 775 | + (HASH_SIZE * 2); |
| 776 | |
| 777 | if (!buffer || buflen < asciiblob_len) |
| 778 | return asciiblob_len; |
| 779 | |
| 780 | mkey = request_master_key(epayload, &master_key, &master_keylen); |
| 781 | if (IS_ERR(mkey)) |
| 782 | return PTR_ERR(mkey); |
| 783 | |
| 784 | ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen); |
| 785 | if (ret < 0) |
| 786 | goto out; |
| 787 | |
| 788 | ret = derived_key_encrypt(epayload, derived_key, sizeof derived_key); |
| 789 | if (ret < 0) |
| 790 | goto out; |
| 791 | |
| 792 | ret = datablob_hmac_append(epayload, master_key, master_keylen); |
| 793 | if (ret < 0) |
| 794 | goto out; |
| 795 | |
| 796 | ascii_buf = datablob_format(epayload, asciiblob_len); |
| 797 | if (!ascii_buf) { |
| 798 | ret = -ENOMEM; |
| 799 | goto out; |
| 800 | } |
| 801 | |
| 802 | up_read(&mkey->sem); |
| 803 | key_put(mkey); |
| 804 | |
| 805 | if (copy_to_user(buffer, ascii_buf, asciiblob_len) != 0) |
| 806 | ret = -EFAULT; |
| 807 | kfree(ascii_buf); |
| 808 | |
| 809 | return asciiblob_len; |
| 810 | out: |
| 811 | up_read(&mkey->sem); |
| 812 | key_put(mkey); |
| 813 | return ret; |
| 814 | } |
| 815 | |
| 816 | /* |
| 817 | * encrypted_destroy - before freeing the key, clear the decrypted data |
| 818 | * |
| 819 | * Before freeing the key, clear the memory containing the decrypted |
| 820 | * key data. |
| 821 | */ |
| 822 | static void encrypted_destroy(struct key *key) |
| 823 | { |
| 824 | struct encrypted_key_payload *epayload = key->payload.data; |
| 825 | |
| 826 | if (!epayload) |
| 827 | return; |
| 828 | |
| 829 | memset(epayload->decrypted_data, 0, epayload->decrypted_datalen); |
| 830 | kfree(key->payload.data); |
| 831 | } |
| 832 | |
| 833 | struct key_type key_type_encrypted = { |
| 834 | .name = "encrypted", |
| 835 | .instantiate = encrypted_instantiate, |
| 836 | .update = encrypted_update, |
| 837 | .match = user_match, |
| 838 | .destroy = encrypted_destroy, |
| 839 | .describe = user_describe, |
| 840 | .read = encrypted_read, |
| 841 | }; |
| 842 | EXPORT_SYMBOL_GPL(key_type_encrypted); |
| 843 | |
| 844 | static void encrypted_shash_release(void) |
| 845 | { |
| 846 | if (hashalg) |
| 847 | crypto_free_shash(hashalg); |
| 848 | if (hmacalg) |
| 849 | crypto_free_shash(hmacalg); |
| 850 | } |
| 851 | |
| 852 | static int __init encrypted_shash_alloc(void) |
| 853 | { |
| 854 | int ret; |
| 855 | |
| 856 | hmacalg = crypto_alloc_shash(hmac_alg, 0, CRYPTO_ALG_ASYNC); |
| 857 | if (IS_ERR(hmacalg)) { |
| 858 | pr_info("encrypted_key: could not allocate crypto %s\n", |
| 859 | hmac_alg); |
| 860 | return PTR_ERR(hmacalg); |
| 861 | } |
| 862 | |
| 863 | hashalg = crypto_alloc_shash(hash_alg, 0, CRYPTO_ALG_ASYNC); |
| 864 | if (IS_ERR(hashalg)) { |
| 865 | pr_info("encrypted_key: could not allocate crypto %s\n", |
| 866 | hash_alg); |
| 867 | ret = PTR_ERR(hashalg); |
| 868 | goto hashalg_fail; |
| 869 | } |
| 870 | |
| 871 | return 0; |
| 872 | |
| 873 | hashalg_fail: |
| 874 | crypto_free_shash(hmacalg); |
| 875 | return ret; |
| 876 | } |
| 877 | |
| 878 | static int __init init_encrypted(void) |
| 879 | { |
| 880 | int ret; |
| 881 | |
| 882 | ret = encrypted_shash_alloc(); |
| 883 | if (ret < 0) |
| 884 | return ret; |
| 885 | ret = register_key_type(&key_type_encrypted); |
| 886 | if (ret < 0) |
| 887 | goto out; |
| 888 | return aes_get_sizes(); |
| 889 | out: |
| 890 | encrypted_shash_release(); |
| 891 | return ret; |
Mimi Zohar | b970344 | 2011-01-18 09:07:12 -0500 | [diff] [blame] | 892 | |
Mimi Zohar | 7e70cb4 | 2010-11-23 18:55:35 -0500 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | static void __exit cleanup_encrypted(void) |
| 896 | { |
| 897 | encrypted_shash_release(); |
| 898 | unregister_key_type(&key_type_encrypted); |
| 899 | } |
| 900 | |
| 901 | late_initcall(init_encrypted); |
| 902 | module_exit(cleanup_encrypted); |
| 903 | |
| 904 | MODULE_LICENSE("GPL"); |