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