trusted-keys: another free memory bugfix
[linux-2.6-block.git] / security / keys / trusted_defined.c
CommitLineData
d00a1c72
MZ
1/*
2 * Copyright (C) 2010 IBM Corporation
3 *
4 * Author:
5 * David Safford <safford@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>
93ae86e7 20#include <linux/err.h>
d00a1c72
MZ
21#include <keys/user-type.h>
22#include <keys/trusted-type.h>
23#include <linux/key-type.h>
24#include <linux/rcupdate.h>
25#include <linux/crypto.h>
26#include <crypto/hash.h>
27#include <crypto/sha.h>
28#include <linux/capability.h>
29#include <linux/tpm.h>
30#include <linux/tpm_command.h>
31
32#include "trusted_defined.h"
33
34static const char hmac_alg[] = "hmac(sha1)";
35static const char hash_alg[] = "sha1";
36
37struct sdesc {
38 struct shash_desc shash;
39 char ctx[];
40};
41
42static struct crypto_shash *hashalg;
43static struct crypto_shash *hmacalg;
44
45static struct sdesc *init_sdesc(struct crypto_shash *alg)
46{
47 struct sdesc *sdesc;
48 int size;
49
50 size = sizeof(struct shash_desc) + crypto_shash_descsize(alg);
51 sdesc = kmalloc(size, GFP_KERNEL);
52 if (!sdesc)
53 return ERR_PTR(-ENOMEM);
54 sdesc->shash.tfm = alg;
55 sdesc->shash.flags = 0x0;
56 return sdesc;
57}
58
1bdbb402 59static int TSS_sha1(const unsigned char *data, unsigned int datalen,
d00a1c72
MZ
60 unsigned char *digest)
61{
62 struct sdesc *sdesc;
63 int ret;
64
65 sdesc = init_sdesc(hashalg);
66 if (IS_ERR(sdesc)) {
67 pr_info("trusted_key: can't alloc %s\n", hash_alg);
68 return PTR_ERR(sdesc);
69 }
70
71 ret = crypto_shash_digest(&sdesc->shash, data, datalen, digest);
72 kfree(sdesc);
73 return ret;
74}
75
76static int TSS_rawhmac(unsigned char *digest, const unsigned char *key,
1bdbb402 77 unsigned int keylen, ...)
d00a1c72
MZ
78{
79 struct sdesc *sdesc;
80 va_list argp;
81 unsigned int dlen;
82 unsigned char *data;
83 int ret;
84
85 sdesc = init_sdesc(hmacalg);
86 if (IS_ERR(sdesc)) {
87 pr_info("trusted_key: can't alloc %s\n", hmac_alg);
88 return PTR_ERR(sdesc);
89 }
90
91 ret = crypto_shash_setkey(hmacalg, key, keylen);
92 if (ret < 0)
93 goto out;
94 ret = crypto_shash_init(&sdesc->shash);
95 if (ret < 0)
96 goto out;
97
98 va_start(argp, keylen);
99 for (;;) {
100 dlen = va_arg(argp, unsigned int);
101 if (dlen == 0)
102 break;
103 data = va_arg(argp, unsigned char *);
35576eab
TH
104 if (data == NULL) {
105 ret = -EINVAL;
106 break;
107 }
d00a1c72
MZ
108 ret = crypto_shash_update(&sdesc->shash, data, dlen);
109 if (ret < 0)
35576eab 110 break;
d00a1c72
MZ
111 }
112 va_end(argp);
bc5e0af0
MZ
113 if (!ret)
114 ret = crypto_shash_final(&sdesc->shash, digest);
d00a1c72
MZ
115out:
116 kfree(sdesc);
117 return ret;
118}
119
120/*
121 * calculate authorization info fields to send to TPM
122 */
bc5e0af0 123static int TSS_authhmac(unsigned char *digest, const unsigned char *key,
1bdbb402 124 unsigned int keylen, unsigned char *h1,
bc5e0af0 125 unsigned char *h2, unsigned char h3, ...)
d00a1c72
MZ
126{
127 unsigned char paramdigest[SHA1_DIGEST_SIZE];
128 struct sdesc *sdesc;
129 unsigned int dlen;
130 unsigned char *data;
131 unsigned char c;
132 int ret;
133 va_list argp;
134
135 sdesc = init_sdesc(hashalg);
136 if (IS_ERR(sdesc)) {
137 pr_info("trusted_key: can't alloc %s\n", hash_alg);
138 return PTR_ERR(sdesc);
139 }
140
141 c = h3;
142 ret = crypto_shash_init(&sdesc->shash);
143 if (ret < 0)
144 goto out;
145 va_start(argp, h3);
146 for (;;) {
147 dlen = va_arg(argp, unsigned int);
148 if (dlen == 0)
149 break;
150 data = va_arg(argp, unsigned char *);
151 ret = crypto_shash_update(&sdesc->shash, data, dlen);
bc5e0af0
MZ
152 if (ret < 0) {
153 va_end(argp);
d00a1c72 154 goto out;
bc5e0af0 155 }
d00a1c72
MZ
156 }
157 va_end(argp);
158 ret = crypto_shash_final(&sdesc->shash, paramdigest);
159 if (!ret)
bc5e0af0
MZ
160 ret = TSS_rawhmac(digest, key, keylen, SHA1_DIGEST_SIZE,
161 paramdigest, TPM_NONCE_SIZE, h1,
162 TPM_NONCE_SIZE, h2, 1, &c, 0, 0);
d00a1c72
MZ
163out:
164 kfree(sdesc);
165 return ret;
166}
167
168/*
169 * verify the AUTH1_COMMAND (Seal) result from TPM
170 */
bc5e0af0
MZ
171static int TSS_checkhmac1(unsigned char *buffer,
172 const uint32_t command,
173 const unsigned char *ononce,
174 const unsigned char *key,
1bdbb402 175 unsigned int keylen, ...)
d00a1c72
MZ
176{
177 uint32_t bufsize;
178 uint16_t tag;
179 uint32_t ordinal;
180 uint32_t result;
181 unsigned char *enonce;
182 unsigned char *continueflag;
183 unsigned char *authdata;
184 unsigned char testhmac[SHA1_DIGEST_SIZE];
185 unsigned char paramdigest[SHA1_DIGEST_SIZE];
186 struct sdesc *sdesc;
187 unsigned int dlen;
188 unsigned int dpos;
189 va_list argp;
190 int ret;
191
192 bufsize = LOAD32(buffer, TPM_SIZE_OFFSET);
193 tag = LOAD16(buffer, 0);
194 ordinal = command;
195 result = LOAD32N(buffer, TPM_RETURN_OFFSET);
196 if (tag == TPM_TAG_RSP_COMMAND)
197 return 0;
198 if (tag != TPM_TAG_RSP_AUTH1_COMMAND)
199 return -EINVAL;
200 authdata = buffer + bufsize - SHA1_DIGEST_SIZE;
201 continueflag = authdata - 1;
202 enonce = continueflag - TPM_NONCE_SIZE;
203
204 sdesc = init_sdesc(hashalg);
205 if (IS_ERR(sdesc)) {
206 pr_info("trusted_key: can't alloc %s\n", hash_alg);
207 return PTR_ERR(sdesc);
208 }
209 ret = crypto_shash_init(&sdesc->shash);
210 if (ret < 0)
211 goto out;
212 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&result,
213 sizeof result);
214 if (ret < 0)
215 goto out;
216 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&ordinal,
217 sizeof ordinal);
218 if (ret < 0)
219 goto out;
220 va_start(argp, keylen);
221 for (;;) {
222 dlen = va_arg(argp, unsigned int);
223 if (dlen == 0)
224 break;
225 dpos = va_arg(argp, unsigned int);
226 ret = crypto_shash_update(&sdesc->shash, buffer + dpos, dlen);
bc5e0af0
MZ
227 if (ret < 0) {
228 va_end(argp);
d00a1c72 229 goto out;
bc5e0af0 230 }
d00a1c72
MZ
231 }
232 va_end(argp);
233 ret = crypto_shash_final(&sdesc->shash, paramdigest);
234 if (ret < 0)
235 goto out;
bc5e0af0 236
d00a1c72
MZ
237 ret = TSS_rawhmac(testhmac, key, keylen, SHA1_DIGEST_SIZE, paramdigest,
238 TPM_NONCE_SIZE, enonce, TPM_NONCE_SIZE, ononce,
239 1, continueflag, 0, 0);
240 if (ret < 0)
241 goto out;
bc5e0af0 242
d00a1c72
MZ
243 if (memcmp(testhmac, authdata, SHA1_DIGEST_SIZE))
244 ret = -EINVAL;
245out:
246 kfree(sdesc);
247 return ret;
248}
249
250/*
251 * verify the AUTH2_COMMAND (unseal) result from TPM
252 */
bc5e0af0
MZ
253static int TSS_checkhmac2(unsigned char *buffer,
254 const uint32_t command,
255 const unsigned char *ononce,
256 const unsigned char *key1,
1bdbb402 257 unsigned int keylen1,
bc5e0af0 258 const unsigned char *key2,
1bdbb402 259 unsigned int keylen2, ...)
d00a1c72
MZ
260{
261 uint32_t bufsize;
262 uint16_t tag;
263 uint32_t ordinal;
264 uint32_t result;
265 unsigned char *enonce1;
266 unsigned char *continueflag1;
267 unsigned char *authdata1;
268 unsigned char *enonce2;
269 unsigned char *continueflag2;
270 unsigned char *authdata2;
271 unsigned char testhmac1[SHA1_DIGEST_SIZE];
272 unsigned char testhmac2[SHA1_DIGEST_SIZE];
273 unsigned char paramdigest[SHA1_DIGEST_SIZE];
274 struct sdesc *sdesc;
275 unsigned int dlen;
276 unsigned int dpos;
277 va_list argp;
278 int ret;
279
280 bufsize = LOAD32(buffer, TPM_SIZE_OFFSET);
281 tag = LOAD16(buffer, 0);
282 ordinal = command;
283 result = LOAD32N(buffer, TPM_RETURN_OFFSET);
284
285 if (tag == TPM_TAG_RSP_COMMAND)
286 return 0;
287 if (tag != TPM_TAG_RSP_AUTH2_COMMAND)
288 return -EINVAL;
289 authdata1 = buffer + bufsize - (SHA1_DIGEST_SIZE + 1
290 + SHA1_DIGEST_SIZE + SHA1_DIGEST_SIZE);
291 authdata2 = buffer + bufsize - (SHA1_DIGEST_SIZE);
292 continueflag1 = authdata1 - 1;
293 continueflag2 = authdata2 - 1;
294 enonce1 = continueflag1 - TPM_NONCE_SIZE;
295 enonce2 = continueflag2 - TPM_NONCE_SIZE;
296
297 sdesc = init_sdesc(hashalg);
298 if (IS_ERR(sdesc)) {
299 pr_info("trusted_key: can't alloc %s\n", hash_alg);
300 return PTR_ERR(sdesc);
301 }
302 ret = crypto_shash_init(&sdesc->shash);
303 if (ret < 0)
304 goto out;
305 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&result,
306 sizeof result);
307 if (ret < 0)
308 goto out;
309 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&ordinal,
310 sizeof ordinal);
311 if (ret < 0)
312 goto out;
313
314 va_start(argp, keylen2);
315 for (;;) {
316 dlen = va_arg(argp, unsigned int);
317 if (dlen == 0)
318 break;
319 dpos = va_arg(argp, unsigned int);
320 ret = crypto_shash_update(&sdesc->shash, buffer + dpos, dlen);
bc5e0af0
MZ
321 if (ret < 0) {
322 va_end(argp);
d00a1c72 323 goto out;
bc5e0af0 324 }
d00a1c72 325 }
bc5e0af0 326 va_end(argp);
d00a1c72
MZ
327 ret = crypto_shash_final(&sdesc->shash, paramdigest);
328 if (ret < 0)
329 goto out;
330
331 ret = TSS_rawhmac(testhmac1, key1, keylen1, SHA1_DIGEST_SIZE,
332 paramdigest, TPM_NONCE_SIZE, enonce1,
333 TPM_NONCE_SIZE, ononce, 1, continueflag1, 0, 0);
bc5e0af0
MZ
334 if (ret < 0)
335 goto out;
d00a1c72
MZ
336 if (memcmp(testhmac1, authdata1, SHA1_DIGEST_SIZE)) {
337 ret = -EINVAL;
338 goto out;
339 }
340 ret = TSS_rawhmac(testhmac2, key2, keylen2, SHA1_DIGEST_SIZE,
341 paramdigest, TPM_NONCE_SIZE, enonce2,
342 TPM_NONCE_SIZE, ononce, 1, continueflag2, 0, 0);
bc5e0af0
MZ
343 if (ret < 0)
344 goto out;
d00a1c72
MZ
345 if (memcmp(testhmac2, authdata2, SHA1_DIGEST_SIZE))
346 ret = -EINVAL;
347out:
348 kfree(sdesc);
349 return ret;
350}
351
352/*
353 * For key specific tpm requests, we will generate and send our
354 * own TPM command packets using the drivers send function.
355 */
356static int trusted_tpm_send(const u32 chip_num, unsigned char *cmd,
357 size_t buflen)
358{
359 int rc;
360
361 dump_tpm_buf(cmd);
362 rc = tpm_send(chip_num, cmd, buflen);
363 dump_tpm_buf(cmd);
364 if (rc > 0)
365 /* Can't return positive return codes values to keyctl */
366 rc = -EPERM;
367 return rc;
368}
369
370/*
371 * get a random value from TPM
372 */
373static int tpm_get_random(struct tpm_buf *tb, unsigned char *buf, uint32_t len)
374{
375 int ret;
376
377 INIT_BUF(tb);
378 store16(tb, TPM_TAG_RQU_COMMAND);
379 store32(tb, TPM_GETRANDOM_SIZE);
380 store32(tb, TPM_ORD_GETRANDOM);
381 store32(tb, len);
382 ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, sizeof tb->data);
bc5e0af0
MZ
383 if (!ret)
384 memcpy(buf, tb->data + TPM_GETRANDOM_SIZE, len);
d00a1c72
MZ
385 return ret;
386}
387
388static int my_get_random(unsigned char *buf, int len)
389{
390 struct tpm_buf *tb;
391 int ret;
392
1bdbb402 393 tb = kmalloc(sizeof *tb, GFP_KERNEL);
d00a1c72
MZ
394 if (!tb)
395 return -ENOMEM;
396 ret = tpm_get_random(tb, buf, len);
397
398 kfree(tb);
399 return ret;
400}
401
402/*
403 * Lock a trusted key, by extending a selected PCR.
404 *
405 * Prevents a trusted key that is sealed to PCRs from being accessed.
406 * This uses the tpm driver's extend function.
407 */
408static int pcrlock(const int pcrnum)
409{
410 unsigned char hash[SHA1_DIGEST_SIZE];
bc5e0af0 411 int ret;
d00a1c72
MZ
412
413 if (!capable(CAP_SYS_ADMIN))
414 return -EPERM;
bc5e0af0
MZ
415 ret = my_get_random(hash, SHA1_DIGEST_SIZE);
416 if (ret < 0)
417 return ret;
d00a1c72
MZ
418 return tpm_pcr_extend(TPM_ANY_NUM, pcrnum, hash) ? -EINVAL : 0;
419}
420
421/*
422 * Create an object specific authorisation protocol (OSAP) session
423 */
424static int osap(struct tpm_buf *tb, struct osapsess *s,
1bdbb402 425 const unsigned char *key, uint16_t type, uint32_t handle)
d00a1c72
MZ
426{
427 unsigned char enonce[TPM_NONCE_SIZE];
428 unsigned char ononce[TPM_NONCE_SIZE];
429 int ret;
430
431 ret = tpm_get_random(tb, ononce, TPM_NONCE_SIZE);
432 if (ret < 0)
433 return ret;
434
435 INIT_BUF(tb);
436 store16(tb, TPM_TAG_RQU_COMMAND);
437 store32(tb, TPM_OSAP_SIZE);
438 store32(tb, TPM_ORD_OSAP);
439 store16(tb, type);
440 store32(tb, handle);
441 storebytes(tb, ononce, TPM_NONCE_SIZE);
442
443 ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, MAX_BUF_SIZE);
444 if (ret < 0)
445 return ret;
446
447 s->handle = LOAD32(tb->data, TPM_DATA_OFFSET);
448 memcpy(s->enonce, &(tb->data[TPM_DATA_OFFSET + sizeof(uint32_t)]),
449 TPM_NONCE_SIZE);
450 memcpy(enonce, &(tb->data[TPM_DATA_OFFSET + sizeof(uint32_t) +
451 TPM_NONCE_SIZE]), TPM_NONCE_SIZE);
bc5e0af0
MZ
452 return TSS_rawhmac(s->secret, key, SHA1_DIGEST_SIZE, TPM_NONCE_SIZE,
453 enonce, TPM_NONCE_SIZE, ononce, 0, 0);
d00a1c72
MZ
454}
455
456/*
457 * Create an object independent authorisation protocol (oiap) session
458 */
459static int oiap(struct tpm_buf *tb, uint32_t *handle, unsigned char *nonce)
460{
461 int ret;
462
463 INIT_BUF(tb);
464 store16(tb, TPM_TAG_RQU_COMMAND);
465 store32(tb, TPM_OIAP_SIZE);
466 store32(tb, TPM_ORD_OIAP);
467 ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, MAX_BUF_SIZE);
468 if (ret < 0)
469 return ret;
470
471 *handle = LOAD32(tb->data, TPM_DATA_OFFSET);
472 memcpy(nonce, &tb->data[TPM_DATA_OFFSET + sizeof(uint32_t)],
473 TPM_NONCE_SIZE);
bc5e0af0 474 return 0;
d00a1c72
MZ
475}
476
477struct tpm_digests {
478 unsigned char encauth[SHA1_DIGEST_SIZE];
479 unsigned char pubauth[SHA1_DIGEST_SIZE];
480 unsigned char xorwork[SHA1_DIGEST_SIZE * 2];
481 unsigned char xorhash[SHA1_DIGEST_SIZE];
482 unsigned char nonceodd[TPM_NONCE_SIZE];
483};
484
485/*
486 * Have the TPM seal(encrypt) the trusted key, possibly based on
487 * Platform Configuration Registers (PCRs). AUTH1 for sealing key.
488 */
1bdbb402
MZ
489static int tpm_seal(struct tpm_buf *tb, uint16_t keytype,
490 uint32_t keyhandle, const unsigned char *keyauth,
491 const unsigned char *data, uint32_t datalen,
d00a1c72
MZ
492 unsigned char *blob, uint32_t *bloblen,
493 const unsigned char *blobauth,
1bdbb402 494 const unsigned char *pcrinfo, uint32_t pcrinfosize)
d00a1c72
MZ
495{
496 struct osapsess sess;
497 struct tpm_digests *td;
498 unsigned char cont;
499 uint32_t ordinal;
500 uint32_t pcrsize;
501 uint32_t datsize;
502 int sealinfosize;
503 int encdatasize;
504 int storedsize;
505 int ret;
506 int i;
507
508 /* alloc some work space for all the hashes */
509 td = kmalloc(sizeof *td, GFP_KERNEL);
510 if (!td)
511 return -ENOMEM;
512
513 /* get session for sealing key */
514 ret = osap(tb, &sess, keyauth, keytype, keyhandle);
515 if (ret < 0)
40c10017 516 goto out;
d00a1c72
MZ
517 dump_sess(&sess);
518
519 /* calculate encrypted authorization value */
520 memcpy(td->xorwork, sess.secret, SHA1_DIGEST_SIZE);
521 memcpy(td->xorwork + SHA1_DIGEST_SIZE, sess.enonce, SHA1_DIGEST_SIZE);
522 ret = TSS_sha1(td->xorwork, SHA1_DIGEST_SIZE * 2, td->xorhash);
523 if (ret < 0)
40c10017 524 goto out;
d00a1c72
MZ
525
526 ret = tpm_get_random(tb, td->nonceodd, TPM_NONCE_SIZE);
527 if (ret < 0)
40c10017 528 goto out;
d00a1c72
MZ
529 ordinal = htonl(TPM_ORD_SEAL);
530 datsize = htonl(datalen);
531 pcrsize = htonl(pcrinfosize);
532 cont = 0;
533
534 /* encrypt data authorization key */
535 for (i = 0; i < SHA1_DIGEST_SIZE; ++i)
536 td->encauth[i] = td->xorhash[i] ^ blobauth[i];
537
538 /* calculate authorization HMAC value */
539 if (pcrinfosize == 0) {
540 /* no pcr info specified */
bc5e0af0
MZ
541 ret = TSS_authhmac(td->pubauth, sess.secret, SHA1_DIGEST_SIZE,
542 sess.enonce, td->nonceodd, cont,
543 sizeof(uint32_t), &ordinal, SHA1_DIGEST_SIZE,
544 td->encauth, sizeof(uint32_t), &pcrsize,
545 sizeof(uint32_t), &datsize, datalen, data, 0,
546 0);
d00a1c72
MZ
547 } else {
548 /* pcr info specified */
bc5e0af0
MZ
549 ret = TSS_authhmac(td->pubauth, sess.secret, SHA1_DIGEST_SIZE,
550 sess.enonce, td->nonceodd, cont,
551 sizeof(uint32_t), &ordinal, SHA1_DIGEST_SIZE,
552 td->encauth, sizeof(uint32_t), &pcrsize,
553 pcrinfosize, pcrinfo, sizeof(uint32_t),
554 &datsize, datalen, data, 0, 0);
d00a1c72 555 }
bc5e0af0 556 if (ret < 0)
40c10017 557 goto out;
d00a1c72
MZ
558
559 /* build and send the TPM request packet */
560 INIT_BUF(tb);
561 store16(tb, TPM_TAG_RQU_AUTH1_COMMAND);
562 store32(tb, TPM_SEAL_SIZE + pcrinfosize + datalen);
563 store32(tb, TPM_ORD_SEAL);
564 store32(tb, keyhandle);
565 storebytes(tb, td->encauth, SHA1_DIGEST_SIZE);
566 store32(tb, pcrinfosize);
567 storebytes(tb, pcrinfo, pcrinfosize);
568 store32(tb, datalen);
569 storebytes(tb, data, datalen);
570 store32(tb, sess.handle);
571 storebytes(tb, td->nonceodd, TPM_NONCE_SIZE);
572 store8(tb, cont);
573 storebytes(tb, td->pubauth, SHA1_DIGEST_SIZE);
574
575 ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, MAX_BUF_SIZE);
576 if (ret < 0)
40c10017 577 goto out;
d00a1c72
MZ
578
579 /* calculate the size of the returned Blob */
580 sealinfosize = LOAD32(tb->data, TPM_DATA_OFFSET + sizeof(uint32_t));
581 encdatasize = LOAD32(tb->data, TPM_DATA_OFFSET + sizeof(uint32_t) +
582 sizeof(uint32_t) + sealinfosize);
583 storedsize = sizeof(uint32_t) + sizeof(uint32_t) + sealinfosize +
584 sizeof(uint32_t) + encdatasize;
585
586 /* check the HMAC in the response */
587 ret = TSS_checkhmac1(tb->data, ordinal, td->nonceodd, sess.secret,
588 SHA1_DIGEST_SIZE, storedsize, TPM_DATA_OFFSET, 0,
589 0);
590
591 /* copy the returned blob to caller */
bc5e0af0
MZ
592 if (!ret) {
593 memcpy(blob, tb->data + TPM_DATA_OFFSET, storedsize);
594 *bloblen = storedsize;
595 }
40c10017
MZ
596out:
597 kfree(td);
d00a1c72
MZ
598 return ret;
599}
600
601/*
602 * use the AUTH2_COMMAND form of unseal, to authorize both key and blob
603 */
604static int tpm_unseal(struct tpm_buf *tb,
1bdbb402
MZ
605 uint32_t keyhandle, const unsigned char *keyauth,
606 const unsigned char *blob, int bloblen,
d00a1c72
MZ
607 const unsigned char *blobauth,
608 unsigned char *data, unsigned int *datalen)
609{
610 unsigned char nonceodd[TPM_NONCE_SIZE];
611 unsigned char enonce1[TPM_NONCE_SIZE];
612 unsigned char enonce2[TPM_NONCE_SIZE];
613 unsigned char authdata1[SHA1_DIGEST_SIZE];
614 unsigned char authdata2[SHA1_DIGEST_SIZE];
615 uint32_t authhandle1 = 0;
616 uint32_t authhandle2 = 0;
617 unsigned char cont = 0;
618 uint32_t ordinal;
619 uint32_t keyhndl;
620 int ret;
621
622 /* sessions for unsealing key and data */
623 ret = oiap(tb, &authhandle1, enonce1);
624 if (ret < 0) {
625 pr_info("trusted_key: oiap failed (%d)\n", ret);
626 return ret;
627 }
628 ret = oiap(tb, &authhandle2, enonce2);
629 if (ret < 0) {
630 pr_info("trusted_key: oiap failed (%d)\n", ret);
631 return ret;
632 }
633
634 ordinal = htonl(TPM_ORD_UNSEAL);
635 keyhndl = htonl(SRKHANDLE);
636 ret = tpm_get_random(tb, nonceodd, TPM_NONCE_SIZE);
637 if (ret < 0) {
638 pr_info("trusted_key: tpm_get_random failed (%d)\n", ret);
639 return ret;
640 }
bc5e0af0
MZ
641 ret = TSS_authhmac(authdata1, keyauth, TPM_NONCE_SIZE,
642 enonce1, nonceodd, cont, sizeof(uint32_t),
643 &ordinal, bloblen, blob, 0, 0);
644 if (ret < 0)
645 return ret;
646 ret = TSS_authhmac(authdata2, blobauth, TPM_NONCE_SIZE,
647 enonce2, nonceodd, cont, sizeof(uint32_t),
648 &ordinal, bloblen, blob, 0, 0);
649 if (ret < 0)
650 return ret;
d00a1c72
MZ
651
652 /* build and send TPM request packet */
653 INIT_BUF(tb);
654 store16(tb, TPM_TAG_RQU_AUTH2_COMMAND);
655 store32(tb, TPM_UNSEAL_SIZE + bloblen);
656 store32(tb, TPM_ORD_UNSEAL);
657 store32(tb, keyhandle);
658 storebytes(tb, blob, bloblen);
659 store32(tb, authhandle1);
660 storebytes(tb, nonceodd, TPM_NONCE_SIZE);
661 store8(tb, cont);
662 storebytes(tb, authdata1, SHA1_DIGEST_SIZE);
663 store32(tb, authhandle2);
664 storebytes(tb, nonceodd, TPM_NONCE_SIZE);
665 store8(tb, cont);
666 storebytes(tb, authdata2, SHA1_DIGEST_SIZE);
667
668 ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, MAX_BUF_SIZE);
669 if (ret < 0) {
670 pr_info("trusted_key: authhmac failed (%d)\n", ret);
671 return ret;
672 }
673
674 *datalen = LOAD32(tb->data, TPM_DATA_OFFSET);
675 ret = TSS_checkhmac2(tb->data, ordinal, nonceodd,
676 keyauth, SHA1_DIGEST_SIZE,
677 blobauth, SHA1_DIGEST_SIZE,
678 sizeof(uint32_t), TPM_DATA_OFFSET,
679 *datalen, TPM_DATA_OFFSET + sizeof(uint32_t), 0,
680 0);
bc5e0af0 681 if (ret < 0) {
d00a1c72 682 pr_info("trusted_key: TSS_checkhmac2 failed (%d)\n", ret);
bc5e0af0
MZ
683 return ret;
684 }
d00a1c72 685 memcpy(data, tb->data + TPM_DATA_OFFSET + sizeof(uint32_t), *datalen);
bc5e0af0 686 return 0;
d00a1c72
MZ
687}
688
689/*
690 * Have the TPM seal(encrypt) the symmetric key
691 */
692static int key_seal(struct trusted_key_payload *p,
693 struct trusted_key_options *o)
694{
695 struct tpm_buf *tb;
696 int ret;
697
698 tb = kzalloc(sizeof *tb, GFP_KERNEL);
699 if (!tb)
700 return -ENOMEM;
701
702 /* include migratable flag at end of sealed key */
703 p->key[p->key_len] = p->migratable;
704
705 ret = tpm_seal(tb, o->keytype, o->keyhandle, o->keyauth,
706 p->key, p->key_len + 1, p->blob, &p->blob_len,
707 o->blobauth, o->pcrinfo, o->pcrinfo_len);
708 if (ret < 0)
709 pr_info("trusted_key: srkseal failed (%d)\n", ret);
710
711 kfree(tb);
712 return ret;
713}
714
715/*
716 * Have the TPM unseal(decrypt) the symmetric key
717 */
718static int key_unseal(struct trusted_key_payload *p,
719 struct trusted_key_options *o)
720{
721 struct tpm_buf *tb;
722 int ret;
723
724 tb = kzalloc(sizeof *tb, GFP_KERNEL);
725 if (!tb)
726 return -ENOMEM;
727
728 ret = tpm_unseal(tb, o->keyhandle, o->keyauth, p->blob, p->blob_len,
729 o->blobauth, p->key, &p->key_len);
d00a1c72
MZ
730 if (ret < 0)
731 pr_info("trusted_key: srkunseal failed (%d)\n", ret);
bc5e0af0
MZ
732 else
733 /* pull migratable flag out of sealed key */
734 p->migratable = p->key[--p->key_len];
d00a1c72
MZ
735
736 kfree(tb);
737 return ret;
738}
739
740enum {
741 Opt_err = -1,
742 Opt_new, Opt_load, Opt_update,
743 Opt_keyhandle, Opt_keyauth, Opt_blobauth,
744 Opt_pcrinfo, Opt_pcrlock, Opt_migratable
745};
746
747static const match_table_t key_tokens = {
748 {Opt_new, "new"},
749 {Opt_load, "load"},
750 {Opt_update, "update"},
751 {Opt_keyhandle, "keyhandle=%s"},
752 {Opt_keyauth, "keyauth=%s"},
753 {Opt_blobauth, "blobauth=%s"},
754 {Opt_pcrinfo, "pcrinfo=%s"},
755 {Opt_pcrlock, "pcrlock=%s"},
756 {Opt_migratable, "migratable=%s"},
757 {Opt_err, NULL}
758};
759
760/* can have zero or more token= options */
761static int getoptions(char *c, struct trusted_key_payload *pay,
762 struct trusted_key_options *opt)
763{
764 substring_t args[MAX_OPT_ARGS];
765 char *p = c;
766 int token;
767 int res;
768 unsigned long handle;
769 unsigned long lock;
770
771 while ((p = strsep(&c, " \t"))) {
772 if (*p == '\0' || *p == ' ' || *p == '\t')
773 continue;
774 token = match_token(p, key_tokens, args);
775
776 switch (token) {
777 case Opt_pcrinfo:
778 opt->pcrinfo_len = strlen(args[0].from) / 2;
779 if (opt->pcrinfo_len > MAX_PCRINFO_SIZE)
780 return -EINVAL;
781 hex2bin(opt->pcrinfo, args[0].from, opt->pcrinfo_len);
782 break;
783 case Opt_keyhandle:
784 res = strict_strtoul(args[0].from, 16, &handle);
785 if (res < 0)
786 return -EINVAL;
787 opt->keytype = SEAL_keytype;
788 opt->keyhandle = handle;
789 break;
790 case Opt_keyauth:
791 if (strlen(args[0].from) != 2 * SHA1_DIGEST_SIZE)
792 return -EINVAL;
793 hex2bin(opt->keyauth, args[0].from, SHA1_DIGEST_SIZE);
794 break;
795 case Opt_blobauth:
796 if (strlen(args[0].from) != 2 * SHA1_DIGEST_SIZE)
797 return -EINVAL;
798 hex2bin(opt->blobauth, args[0].from, SHA1_DIGEST_SIZE);
799 break;
800 case Opt_migratable:
801 if (*args[0].from == '0')
802 pay->migratable = 0;
803 else
804 return -EINVAL;
805 break;
806 case Opt_pcrlock:
807 res = strict_strtoul(args[0].from, 10, &lock);
808 if (res < 0)
809 return -EINVAL;
810 opt->pcrlock = lock;
811 break;
812 default:
813 return -EINVAL;
814 }
815 }
816 return 0;
817}
818
819/*
820 * datablob_parse - parse the keyctl data and fill in the
821 * payload and options structures
822 *
823 * On success returns 0, otherwise -EINVAL.
824 */
825static int datablob_parse(char *datablob, struct trusted_key_payload *p,
826 struct trusted_key_options *o)
827{
828 substring_t args[MAX_OPT_ARGS];
829 long keylen;
830 int ret = -EINVAL;
831 int key_cmd;
832 char *c;
833
834 /* main command */
835 c = strsep(&datablob, " \t");
836 if (!c)
837 return -EINVAL;
838 key_cmd = match_token(c, key_tokens, args);
839 switch (key_cmd) {
840 case Opt_new:
841 /* first argument is key size */
842 c = strsep(&datablob, " \t");
843 if (!c)
844 return -EINVAL;
845 ret = strict_strtol(c, 10, &keylen);
846 if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
847 return -EINVAL;
848 p->key_len = keylen;
849 ret = getoptions(datablob, p, o);
850 if (ret < 0)
851 return ret;
852 ret = Opt_new;
853 break;
854 case Opt_load:
855 /* first argument is sealed blob */
856 c = strsep(&datablob, " \t");
857 if (!c)
858 return -EINVAL;
859 p->blob_len = strlen(c) / 2;
860 if (p->blob_len > MAX_BLOB_SIZE)
861 return -EINVAL;
862 hex2bin(p->blob, c, p->blob_len);
863 ret = getoptions(datablob, p, o);
864 if (ret < 0)
865 return ret;
866 ret = Opt_load;
867 break;
868 case Opt_update:
869 /* all arguments are options */
870 ret = getoptions(datablob, p, o);
871 if (ret < 0)
872 return ret;
873 ret = Opt_update;
874 break;
875 case Opt_err:
876 return -EINVAL;
877 break;
878 }
879 return ret;
880}
881
882static struct trusted_key_options *trusted_options_alloc(void)
883{
884 struct trusted_key_options *options;
885
886 options = kzalloc(sizeof *options, GFP_KERNEL);
bc5e0af0
MZ
887 if (options) {
888 /* set any non-zero defaults */
889 options->keytype = SRK_keytype;
890 options->keyhandle = SRKHANDLE;
891 }
d00a1c72
MZ
892 return options;
893}
894
895static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
896{
897 struct trusted_key_payload *p = NULL;
898 int ret;
899
900 ret = key_payload_reserve(key, sizeof *p);
901 if (ret < 0)
902 return p;
903 p = kzalloc(sizeof *p, GFP_KERNEL);
bc5e0af0
MZ
904 if (p)
905 p->migratable = 1; /* migratable by default */
d00a1c72
MZ
906 return p;
907}
908
909/*
910 * trusted_instantiate - create a new trusted key
911 *
912 * Unseal an existing trusted blob or, for a new key, get a
913 * random key, then seal and create a trusted key-type key,
914 * adding it to the specified keyring.
915 *
916 * On success, return 0. Otherwise return errno.
917 */
918static int trusted_instantiate(struct key *key, const void *data,
1bdbb402 919 size_t datalen)
d00a1c72
MZ
920{
921 struct trusted_key_payload *payload = NULL;
922 struct trusted_key_options *options = NULL;
923 char *datablob;
924 int ret = 0;
925 int key_cmd;
926
927 if (datalen <= 0 || datalen > 32767 || !data)
928 return -EINVAL;
929
930 datablob = kmalloc(datalen + 1, GFP_KERNEL);
931 if (!datablob)
932 return -ENOMEM;
933 memcpy(datablob, data, datalen);
934 datablob[datalen] = '\0';
935
936 options = trusted_options_alloc();
937 if (!options) {
938 ret = -ENOMEM;
939 goto out;
940 }
941 payload = trusted_payload_alloc(key);
942 if (!payload) {
943 ret = -ENOMEM;
944 goto out;
945 }
946
947 key_cmd = datablob_parse(datablob, payload, options);
948 if (key_cmd < 0) {
949 ret = key_cmd;
950 goto out;
951 }
952
953 dump_payload(payload);
954 dump_options(options);
955
956 switch (key_cmd) {
957 case Opt_load:
958 ret = key_unseal(payload, options);
959 dump_payload(payload);
960 dump_options(options);
961 if (ret < 0)
962 pr_info("trusted_key: key_unseal failed (%d)\n", ret);
963 break;
964 case Opt_new:
965 ret = my_get_random(payload->key, payload->key_len);
966 if (ret < 0) {
967 pr_info("trusted_key: key_create failed (%d)\n", ret);
968 goto out;
969 }
970 ret = key_seal(payload, options);
971 if (ret < 0)
972 pr_info("trusted_key: key_seal failed (%d)\n", ret);
973 break;
974 default:
975 ret = -EINVAL;
976 goto out;
977 }
978 if (!ret && options->pcrlock)
979 ret = pcrlock(options->pcrlock);
980out:
981 kfree(datablob);
982 kfree(options);
983 if (!ret)
984 rcu_assign_pointer(key->payload.data, payload);
985 else
986 kfree(payload);
987 return ret;
988}
989
990static void trusted_rcu_free(struct rcu_head *rcu)
991{
992 struct trusted_key_payload *p;
993
994 p = container_of(rcu, struct trusted_key_payload, rcu);
995 memset(p->key, 0, p->key_len);
996 kfree(p);
997}
998
999/*
1000 * trusted_update - reseal an existing key with new PCR values
1001 */
1bdbb402 1002static int trusted_update(struct key *key, const void *data, size_t datalen)
d00a1c72
MZ
1003{
1004 struct trusted_key_payload *p = key->payload.data;
1005 struct trusted_key_payload *new_p;
1006 struct trusted_key_options *new_o;
1007 char *datablob;
1008 int ret = 0;
1009
1010 if (!p->migratable)
1011 return -EPERM;
1012 if (datalen <= 0 || datalen > 32767 || !data)
1013 return -EINVAL;
1014
1015 datablob = kmalloc(datalen + 1, GFP_KERNEL);
1016 if (!datablob)
1017 return -ENOMEM;
1018 new_o = trusted_options_alloc();
1019 if (!new_o) {
1020 ret = -ENOMEM;
1021 goto out;
1022 }
1023 new_p = trusted_payload_alloc(key);
1024 if (!new_p) {
1025 ret = -ENOMEM;
1026 goto out;
1027 }
1028
1029 memcpy(datablob, data, datalen);
1030 datablob[datalen] = '\0';
1031 ret = datablob_parse(datablob, new_p, new_o);
1032 if (ret != Opt_update) {
1033 ret = -EINVAL;
1034 goto out;
1035 }
1036 /* copy old key values, and reseal with new pcrs */
1037 new_p->migratable = p->migratable;
1038 new_p->key_len = p->key_len;
1039 memcpy(new_p->key, p->key, p->key_len);
1040 dump_payload(p);
1041 dump_payload(new_p);
1042
1043 ret = key_seal(new_p, new_o);
1044 if (ret < 0) {
1045 pr_info("trusted_key: key_seal failed (%d)\n", ret);
1046 kfree(new_p);
1047 goto out;
1048 }
1049 if (new_o->pcrlock) {
1050 ret = pcrlock(new_o->pcrlock);
1051 if (ret < 0) {
1052 pr_info("trusted_key: pcrlock failed (%d)\n", ret);
1053 kfree(new_p);
1054 goto out;
1055 }
1056 }
1057 rcu_assign_pointer(key->payload.data, new_p);
1058 call_rcu(&p->rcu, trusted_rcu_free);
1059out:
1060 kfree(datablob);
1061 kfree(new_o);
1062 return ret;
1063}
1064
1065/*
1066 * trusted_read - copy the sealed blob data to userspace in hex.
1067 * On success, return to userspace the trusted key datablob size.
1068 */
1069static long trusted_read(const struct key *key, char __user *buffer,
1070 size_t buflen)
1071{
1072 struct trusted_key_payload *p;
1073 char *ascii_buf;
1074 char *bufp;
1075 int i;
1076
1077 p = rcu_dereference_protected(key->payload.data,
1078 rwsem_is_locked(&((struct key *)key)->sem));
1079 if (!p)
1080 return -EINVAL;
1081 if (!buffer || buflen <= 0)
1082 return 2 * p->blob_len;
1083 ascii_buf = kmalloc(2 * p->blob_len, GFP_KERNEL);
1084 if (!ascii_buf)
1085 return -ENOMEM;
1086
1087 bufp = ascii_buf;
1088 for (i = 0; i < p->blob_len; i++)
1089 bufp = pack_hex_byte(bufp, p->blob[i]);
1090 if ((copy_to_user(buffer, ascii_buf, 2 * p->blob_len)) != 0) {
1091 kfree(ascii_buf);
1092 return -EFAULT;
1093 }
1094 kfree(ascii_buf);
1095 return 2 * p->blob_len;
1096}
1097
1098/*
1099 * trusted_destroy - before freeing the key, clear the decrypted data
1100 */
1101static void trusted_destroy(struct key *key)
1102{
1103 struct trusted_key_payload *p = key->payload.data;
1104
1105 if (!p)
1106 return;
1107 memset(p->key, 0, p->key_len);
1108 kfree(key->payload.data);
1109}
1110
1111struct key_type key_type_trusted = {
1112 .name = "trusted",
1113 .instantiate = trusted_instantiate,
1114 .update = trusted_update,
1115 .match = user_match,
1116 .destroy = trusted_destroy,
1117 .describe = user_describe,
1118 .read = trusted_read,
1119};
1120
1121EXPORT_SYMBOL_GPL(key_type_trusted);
1122
1123static void trusted_shash_release(void)
1124{
1125 if (hashalg)
1126 crypto_free_shash(hashalg);
1127 if (hmacalg)
1128 crypto_free_shash(hmacalg);
1129}
1130
1131static int __init trusted_shash_alloc(void)
1132{
1133 int ret;
1134
1135 hmacalg = crypto_alloc_shash(hmac_alg, 0, CRYPTO_ALG_ASYNC);
1136 if (IS_ERR(hmacalg)) {
1137 pr_info("trusted_key: could not allocate crypto %s\n",
1138 hmac_alg);
1139 return PTR_ERR(hmacalg);
1140 }
1141
1142 hashalg = crypto_alloc_shash(hash_alg, 0, CRYPTO_ALG_ASYNC);
1143 if (IS_ERR(hashalg)) {
1144 pr_info("trusted_key: could not allocate crypto %s\n",
1145 hash_alg);
1146 ret = PTR_ERR(hashalg);
1147 goto hashalg_fail;
1148 }
1149
1150 return 0;
1151
1152hashalg_fail:
1153 crypto_free_shash(hmacalg);
1154 return ret;
1155}
1156
1157static int __init init_trusted(void)
1158{
1159 int ret;
1160
1161 ret = trusted_shash_alloc();
1162 if (ret < 0)
1163 return ret;
1164 ret = register_key_type(&key_type_trusted);
1165 if (ret < 0)
1166 trusted_shash_release();
1167 return ret;
1168}
1169
1170static void __exit cleanup_trusted(void)
1171{
1172 trusted_shash_release();
1173 unregister_key_type(&key_type_trusted);
1174}
1175
1176late_initcall(init_trusted);
1177module_exit(cleanup_trusted);
1178
1179MODULE_LICENSE("GPL");