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