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