1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2010 IBM Corporation 4 * Copyright (c) 2019-2021, Linaro Limited 5 * 6 * See Documentation/security/keys/trusted-encrypted.rst 7 */ 8 9 #include <crypto/hash_info.h> 10 #include <crypto/sha1.h> 11 #include <crypto/utils.h> 12 #include <linux/hex.h> 13 #include <linux/init.h> 14 #include <linux/slab.h> 15 #include <linux/parser.h> 16 #include <linux/string.h> 17 #include <linux/err.h> 18 #include <keys/trusted-type.h> 19 #include <linux/key-type.h> 20 #include <linux/tpm.h> 21 #include <linux/tpm_command.h> 22 23 #include <keys/trusted_tpm.h> 24 25 static struct tpm_chip *chip; 26 static struct tpm_digest *digests; 27 28 /* implementation specific TPM constants */ 29 #define TPM_SIZE_OFFSET 2 30 #define TPM_RETURN_OFFSET 6 31 #define TPM_DATA_OFFSET 10 32 33 #define LOAD32(buffer, offset) (ntohl(*(uint32_t *)&buffer[offset])) 34 #define LOAD32N(buffer, offset) (*(uint32_t *)&buffer[offset]) 35 #define LOAD16(buffer, offset) (ntohs(*(uint16_t *)&buffer[offset])) 36 37 struct osapsess { 38 uint32_t handle; 39 unsigned char secret[SHA1_DIGEST_SIZE]; 40 unsigned char enonce[TPM_NONCE_SIZE]; 41 }; 42 43 /* discrete values, but have to store in uint16_t for TPM use */ 44 enum { 45 SEAL_keytype = 1, 46 SRK_keytype = 4 47 }; 48 49 #ifdef CONFIG_TRUSTED_KEYS_DEBUG 50 static inline void dump_options(struct trusted_key_options *o) 51 { 52 if (!trusted_debug) 53 return; 54 55 pr_debug("sealing key type %d\n", o->keytype); 56 pr_debug("sealing key handle %0X\n", o->keyhandle); 57 pr_debug("pcrlock %d\n", o->pcrlock); 58 pr_debug("pcrinfo %d\n", o->pcrinfo_len); 59 print_hex_dump_debug("pcrinfo ", DUMP_PREFIX_NONE, 60 16, 1, o->pcrinfo, o->pcrinfo_len, 0); 61 } 62 63 static inline void dump_sess(struct osapsess *s) 64 { 65 if (!trusted_debug) 66 return; 67 68 print_hex_dump_debug("trusted-key: handle ", DUMP_PREFIX_NONE, 69 16, 1, &s->handle, 4, 0); 70 pr_debug("secret:\n"); 71 print_hex_dump_debug("", DUMP_PREFIX_NONE, 72 16, 1, &s->secret, SHA1_DIGEST_SIZE, 0); 73 pr_debug("trusted-key: enonce:\n"); 74 print_hex_dump_debug("", DUMP_PREFIX_NONE, 75 16, 1, &s->enonce, SHA1_DIGEST_SIZE, 0); 76 } 77 78 static inline void dump_tpm_buf(unsigned char *buf) 79 { 80 int len; 81 82 if (!trusted_debug) 83 return; 84 pr_debug("\ntpm buffer\n"); 85 len = LOAD32(buf, TPM_SIZE_OFFSET); 86 print_hex_dump_debug("", DUMP_PREFIX_NONE, 16, 1, buf, len, 0); 87 } 88 #else 89 static inline void dump_options(struct trusted_key_options *o) 90 { 91 } 92 93 static inline void dump_sess(struct osapsess *s) 94 { 95 } 96 97 static inline void dump_tpm_buf(unsigned char *buf) 98 { 99 } 100 #endif 101 102 static int TSS_rawhmac(unsigned char *digest, const unsigned char *key, 103 unsigned int keylen, ...) 104 { 105 struct hmac_sha1_ctx hmac_ctx; 106 va_list argp; 107 unsigned int dlen; 108 unsigned char *data; 109 int ret = 0; 110 111 hmac_sha1_init_usingrawkey(&hmac_ctx, key, keylen); 112 113 va_start(argp, keylen); 114 for (;;) { 115 dlen = va_arg(argp, unsigned int); 116 if (dlen == 0) 117 break; 118 data = va_arg(argp, unsigned char *); 119 if (data == NULL) { 120 ret = -EINVAL; 121 break; 122 } 123 hmac_sha1_update(&hmac_ctx, data, dlen); 124 } 125 va_end(argp); 126 if (!ret) 127 hmac_sha1_final(&hmac_ctx, digest); 128 return ret; 129 } 130 131 /* 132 * calculate authorization info fields to send to TPM 133 */ 134 static int TSS_authhmac(unsigned char *digest, const unsigned char *key, 135 unsigned int keylen, unsigned char *h1, 136 unsigned char *h2, unsigned int h3, ...) 137 { 138 unsigned char paramdigest[SHA1_DIGEST_SIZE]; 139 struct sha1_ctx sha_ctx; 140 unsigned int dlen; 141 unsigned char *data; 142 unsigned char c; 143 int ret = 0; 144 va_list argp; 145 146 if (!chip) 147 return -ENODEV; 148 149 c = !!h3; 150 sha1_init(&sha_ctx); 151 va_start(argp, h3); 152 for (;;) { 153 dlen = va_arg(argp, unsigned int); 154 if (dlen == 0) 155 break; 156 data = va_arg(argp, unsigned char *); 157 if (!data) { 158 ret = -EINVAL; 159 break; 160 } 161 sha1_update(&sha_ctx, data, dlen); 162 } 163 va_end(argp); 164 if (!ret) 165 sha1_final(&sha_ctx, paramdigest); 166 if (!ret) 167 ret = TSS_rawhmac(digest, key, keylen, SHA1_DIGEST_SIZE, 168 paramdigest, TPM_NONCE_SIZE, h1, 169 TPM_NONCE_SIZE, h2, 1, &c, 0, 0); 170 return ret; 171 } 172 173 /* 174 * verify the AUTH1_COMMAND (Seal) result from TPM 175 */ 176 static int TSS_checkhmac1(unsigned char *buffer, 177 const uint32_t command, 178 const unsigned char *ononce, 179 const unsigned char *key, 180 unsigned int keylen, ...) 181 { 182 uint32_t bufsize; 183 uint16_t tag; 184 uint32_t ordinal; 185 uint32_t result; 186 unsigned char *enonce; 187 unsigned char *continueflag; 188 unsigned char *authdata; 189 unsigned char testhmac[SHA1_DIGEST_SIZE]; 190 unsigned char paramdigest[SHA1_DIGEST_SIZE]; 191 struct sha1_ctx sha_ctx; 192 unsigned int dlen; 193 unsigned int dpos; 194 va_list argp; 195 int ret; 196 197 if (!chip) 198 return -ENODEV; 199 200 bufsize = LOAD32(buffer, TPM_SIZE_OFFSET); 201 tag = LOAD16(buffer, 0); 202 ordinal = command; 203 result = LOAD32N(buffer, TPM_RETURN_OFFSET); 204 if (tag == TPM_TAG_RSP_COMMAND) 205 return 0; 206 if (tag != TPM_TAG_RSP_AUTH1_COMMAND) 207 return -EINVAL; 208 authdata = buffer + bufsize - SHA1_DIGEST_SIZE; 209 continueflag = authdata - 1; 210 enonce = continueflag - TPM_NONCE_SIZE; 211 212 sha1_init(&sha_ctx); 213 sha1_update(&sha_ctx, (const u8 *)&result, sizeof(result)); 214 sha1_update(&sha_ctx, (const u8 *)&ordinal, sizeof(ordinal)); 215 va_start(argp, keylen); 216 for (;;) { 217 dlen = va_arg(argp, unsigned int); 218 if (dlen == 0) 219 break; 220 dpos = va_arg(argp, unsigned int); 221 sha1_update(&sha_ctx, buffer + dpos, dlen); 222 } 223 va_end(argp); 224 sha1_final(&sha_ctx, paramdigest); 225 226 ret = TSS_rawhmac(testhmac, key, keylen, SHA1_DIGEST_SIZE, paramdigest, 227 TPM_NONCE_SIZE, enonce, TPM_NONCE_SIZE, ononce, 228 1, continueflag, 0, 0); 229 if (ret < 0) 230 return ret; 231 232 if (crypto_memneq(testhmac, authdata, SHA1_DIGEST_SIZE)) 233 return -EINVAL; 234 return 0; 235 } 236 237 /* 238 * verify the AUTH2_COMMAND (unseal) result from TPM 239 */ 240 static int TSS_checkhmac2(unsigned char *buffer, 241 const uint32_t command, 242 const unsigned char *ononce, 243 const unsigned char *key1, 244 unsigned int keylen1, 245 const unsigned char *key2, 246 unsigned int keylen2, ...) 247 { 248 uint32_t bufsize; 249 uint16_t tag; 250 uint32_t ordinal; 251 uint32_t result; 252 unsigned char *enonce1; 253 unsigned char *continueflag1; 254 unsigned char *authdata1; 255 unsigned char *enonce2; 256 unsigned char *continueflag2; 257 unsigned char *authdata2; 258 unsigned char testhmac1[SHA1_DIGEST_SIZE]; 259 unsigned char testhmac2[SHA1_DIGEST_SIZE]; 260 unsigned char paramdigest[SHA1_DIGEST_SIZE]; 261 struct sha1_ctx sha_ctx; 262 unsigned int dlen; 263 unsigned int dpos; 264 va_list argp; 265 int ret; 266 267 bufsize = LOAD32(buffer, TPM_SIZE_OFFSET); 268 tag = LOAD16(buffer, 0); 269 ordinal = command; 270 result = LOAD32N(buffer, TPM_RETURN_OFFSET); 271 272 if (tag == TPM_TAG_RSP_COMMAND) 273 return 0; 274 if (tag != TPM_TAG_RSP_AUTH2_COMMAND) 275 return -EINVAL; 276 authdata1 = buffer + bufsize - (SHA1_DIGEST_SIZE + 1 277 + SHA1_DIGEST_SIZE + SHA1_DIGEST_SIZE); 278 authdata2 = buffer + bufsize - (SHA1_DIGEST_SIZE); 279 continueflag1 = authdata1 - 1; 280 continueflag2 = authdata2 - 1; 281 enonce1 = continueflag1 - TPM_NONCE_SIZE; 282 enonce2 = continueflag2 - TPM_NONCE_SIZE; 283 284 sha1_init(&sha_ctx); 285 sha1_update(&sha_ctx, (const u8 *)&result, sizeof(result)); 286 sha1_update(&sha_ctx, (const u8 *)&ordinal, sizeof(ordinal)); 287 288 va_start(argp, keylen2); 289 for (;;) { 290 dlen = va_arg(argp, unsigned int); 291 if (dlen == 0) 292 break; 293 dpos = va_arg(argp, unsigned int); 294 sha1_update(&sha_ctx, buffer + dpos, dlen); 295 } 296 va_end(argp); 297 sha1_final(&sha_ctx, paramdigest); 298 299 ret = TSS_rawhmac(testhmac1, key1, keylen1, SHA1_DIGEST_SIZE, 300 paramdigest, TPM_NONCE_SIZE, enonce1, 301 TPM_NONCE_SIZE, ononce, 1, continueflag1, 0, 0); 302 if (ret < 0) 303 return ret; 304 if (crypto_memneq(testhmac1, authdata1, SHA1_DIGEST_SIZE)) 305 return -EINVAL; 306 ret = TSS_rawhmac(testhmac2, key2, keylen2, SHA1_DIGEST_SIZE, 307 paramdigest, TPM_NONCE_SIZE, enonce2, 308 TPM_NONCE_SIZE, ononce, 1, continueflag2, 0, 0); 309 if (ret < 0) 310 return ret; 311 if (crypto_memneq(testhmac2, authdata2, SHA1_DIGEST_SIZE)) 312 return -EINVAL; 313 return 0; 314 } 315 316 /* 317 * For key specific tpm requests, we will generate and send our 318 * own TPM command packets using the drivers send function. 319 */ 320 static int trusted_tpm_send(unsigned char *cmd, size_t buflen) 321 { 322 struct tpm_buf buf; 323 int rc; 324 325 if (!chip) 326 return -ENODEV; 327 328 rc = tpm_try_get_ops(chip); 329 if (rc) 330 return rc; 331 332 buf.flags = 0; 333 buf.length = buflen; 334 buf.data = cmd; 335 dump_tpm_buf(cmd); 336 rc = tpm_transmit_cmd(chip, &buf, 4, "sending data"); 337 dump_tpm_buf(cmd); 338 339 if (rc > 0) 340 /* TPM error */ 341 rc = -EPERM; 342 343 tpm_put_ops(chip); 344 return rc; 345 } 346 347 /* 348 * Lock a trusted key, by extending a selected PCR. 349 * 350 * Prevents a trusted key that is sealed to PCRs from being accessed. 351 * This uses the tpm driver's extend function. 352 */ 353 static int pcrlock(const int pcrnum) 354 { 355 if (!capable(CAP_SYS_ADMIN)) 356 return -EPERM; 357 358 return tpm_pcr_extend(chip, pcrnum, digests) ? -EINVAL : 0; 359 } 360 361 /* 362 * Create an object specific authorisation protocol (OSAP) session 363 */ 364 static int osap(struct tpm_buf *tb, struct osapsess *s, 365 const unsigned char *key, uint16_t type, uint32_t handle) 366 { 367 unsigned char enonce[TPM_NONCE_SIZE]; 368 unsigned char ononce[TPM_NONCE_SIZE]; 369 int ret; 370 371 ret = tpm_get_random(chip, ononce, TPM_NONCE_SIZE); 372 if (ret < 0) 373 return ret; 374 375 if (ret != TPM_NONCE_SIZE) 376 return -EIO; 377 378 tpm_buf_reset(tb, TPM_TAG_RQU_COMMAND, TPM_ORD_OSAP); 379 tpm_buf_append_u16(tb, type); 380 tpm_buf_append_u32(tb, handle); 381 tpm_buf_append(tb, ononce, TPM_NONCE_SIZE); 382 383 ret = trusted_tpm_send(tb->data, tb->length); 384 if (ret < 0) 385 return ret; 386 387 s->handle = LOAD32(tb->data, TPM_DATA_OFFSET); 388 memcpy(s->enonce, &(tb->data[TPM_DATA_OFFSET + sizeof(uint32_t)]), 389 TPM_NONCE_SIZE); 390 memcpy(enonce, &(tb->data[TPM_DATA_OFFSET + sizeof(uint32_t) + 391 TPM_NONCE_SIZE]), TPM_NONCE_SIZE); 392 return TSS_rawhmac(s->secret, key, SHA1_DIGEST_SIZE, TPM_NONCE_SIZE, 393 enonce, TPM_NONCE_SIZE, ononce, 0, 0); 394 } 395 396 /* 397 * Create an object independent authorisation protocol (oiap) session 398 */ 399 static int oiap(struct tpm_buf *tb, uint32_t *handle, unsigned char *nonce) 400 { 401 int ret; 402 403 if (!chip) 404 return -ENODEV; 405 406 tpm_buf_reset(tb, TPM_TAG_RQU_COMMAND, TPM_ORD_OIAP); 407 ret = trusted_tpm_send(tb->data, tb->length); 408 if (ret < 0) 409 return ret; 410 411 *handle = LOAD32(tb->data, TPM_DATA_OFFSET); 412 memcpy(nonce, &tb->data[TPM_DATA_OFFSET + sizeof(uint32_t)], 413 TPM_NONCE_SIZE); 414 return 0; 415 } 416 417 struct tpm_digests { 418 unsigned char encauth[SHA1_DIGEST_SIZE]; 419 unsigned char pubauth[SHA1_DIGEST_SIZE]; 420 unsigned char xorwork[SHA1_DIGEST_SIZE * 2]; 421 unsigned char xorhash[SHA1_DIGEST_SIZE]; 422 unsigned char nonceodd[TPM_NONCE_SIZE]; 423 }; 424 425 /* 426 * Have the TPM seal(encrypt) the trusted key, possibly based on 427 * Platform Configuration Registers (PCRs). AUTH1 for sealing key. 428 */ 429 static int tpm_seal(struct tpm_buf *tb, uint16_t keytype, 430 uint32_t keyhandle, const unsigned char *keyauth, 431 const unsigned char *data, uint32_t datalen, 432 unsigned char *blob, uint32_t *bloblen, 433 const unsigned char *blobauth, 434 const unsigned char *pcrinfo, uint32_t pcrinfosize) 435 { 436 struct osapsess sess; 437 struct tpm_digests *td; 438 unsigned char cont; 439 uint32_t ordinal; 440 uint32_t pcrsize; 441 uint32_t datsize; 442 int sealinfosize; 443 int encdatasize; 444 int storedsize; 445 int ret; 446 int i; 447 448 /* alloc some work space for all the hashes */ 449 td = kmalloc_obj(*td); 450 if (!td) 451 return -ENOMEM; 452 453 /* get session for sealing key */ 454 ret = osap(tb, &sess, keyauth, keytype, keyhandle); 455 if (ret < 0) 456 goto out; 457 dump_sess(&sess); 458 459 /* calculate encrypted authorization value */ 460 memcpy(td->xorwork, sess.secret, SHA1_DIGEST_SIZE); 461 memcpy(td->xorwork + SHA1_DIGEST_SIZE, sess.enonce, SHA1_DIGEST_SIZE); 462 sha1(td->xorwork, SHA1_DIGEST_SIZE * 2, td->xorhash); 463 464 ret = tpm_get_random(chip, td->nonceodd, TPM_NONCE_SIZE); 465 if (ret < 0) 466 goto out; 467 468 if (ret != TPM_NONCE_SIZE) { 469 ret = -EIO; 470 goto out; 471 } 472 473 ordinal = htonl(TPM_ORD_SEAL); 474 datsize = htonl(datalen); 475 pcrsize = htonl(pcrinfosize); 476 cont = 0; 477 478 /* encrypt data authorization key */ 479 for (i = 0; i < SHA1_DIGEST_SIZE; ++i) 480 td->encauth[i] = td->xorhash[i] ^ blobauth[i]; 481 482 /* calculate authorization HMAC value */ 483 if (pcrinfosize == 0) { 484 /* no pcr info specified */ 485 ret = TSS_authhmac(td->pubauth, sess.secret, SHA1_DIGEST_SIZE, 486 sess.enonce, td->nonceodd, cont, 487 sizeof(uint32_t), &ordinal, SHA1_DIGEST_SIZE, 488 td->encauth, sizeof(uint32_t), &pcrsize, 489 sizeof(uint32_t), &datsize, datalen, data, 0, 490 0); 491 } else { 492 /* pcr info specified */ 493 ret = TSS_authhmac(td->pubauth, sess.secret, SHA1_DIGEST_SIZE, 494 sess.enonce, td->nonceodd, cont, 495 sizeof(uint32_t), &ordinal, SHA1_DIGEST_SIZE, 496 td->encauth, sizeof(uint32_t), &pcrsize, 497 pcrinfosize, pcrinfo, sizeof(uint32_t), 498 &datsize, datalen, data, 0, 0); 499 } 500 if (ret < 0) 501 goto out; 502 503 /* build and send the TPM request packet */ 504 tpm_buf_reset(tb, TPM_TAG_RQU_AUTH1_COMMAND, TPM_ORD_SEAL); 505 tpm_buf_append_u32(tb, keyhandle); 506 tpm_buf_append(tb, td->encauth, SHA1_DIGEST_SIZE); 507 tpm_buf_append_u32(tb, pcrinfosize); 508 tpm_buf_append(tb, pcrinfo, pcrinfosize); 509 tpm_buf_append_u32(tb, datalen); 510 tpm_buf_append(tb, data, datalen); 511 tpm_buf_append_u32(tb, sess.handle); 512 tpm_buf_append(tb, td->nonceodd, TPM_NONCE_SIZE); 513 tpm_buf_append_u8(tb, cont); 514 tpm_buf_append(tb, td->pubauth, SHA1_DIGEST_SIZE); 515 516 ret = trusted_tpm_send(tb->data, tb->length); 517 if (ret < 0) 518 goto out; 519 520 /* calculate the size of the returned Blob */ 521 sealinfosize = LOAD32(tb->data, TPM_DATA_OFFSET + sizeof(uint32_t)); 522 encdatasize = LOAD32(tb->data, TPM_DATA_OFFSET + sizeof(uint32_t) + 523 sizeof(uint32_t) + sealinfosize); 524 storedsize = sizeof(uint32_t) + sizeof(uint32_t) + sealinfosize + 525 sizeof(uint32_t) + encdatasize; 526 527 /* check the HMAC in the response */ 528 ret = TSS_checkhmac1(tb->data, ordinal, td->nonceodd, sess.secret, 529 SHA1_DIGEST_SIZE, storedsize, TPM_DATA_OFFSET, 0, 530 0); 531 532 /* copy the returned blob to caller */ 533 if (!ret) { 534 memcpy(blob, tb->data + TPM_DATA_OFFSET, storedsize); 535 *bloblen = storedsize; 536 } 537 out: 538 kfree_sensitive(td); 539 return ret; 540 } 541 542 /* 543 * use the AUTH2_COMMAND form of unseal, to authorize both key and blob 544 */ 545 static int tpm_unseal(struct tpm_buf *tb, 546 uint32_t keyhandle, const unsigned char *keyauth, 547 const unsigned char *blob, int bloblen, 548 const unsigned char *blobauth, 549 unsigned char *data, unsigned int *datalen) 550 { 551 unsigned char nonceodd[TPM_NONCE_SIZE]; 552 unsigned char enonce1[TPM_NONCE_SIZE]; 553 unsigned char enonce2[TPM_NONCE_SIZE]; 554 unsigned char authdata1[SHA1_DIGEST_SIZE]; 555 unsigned char authdata2[SHA1_DIGEST_SIZE]; 556 uint32_t authhandle1 = 0; 557 uint32_t authhandle2 = 0; 558 unsigned char cont = 0; 559 uint32_t ordinal; 560 int ret; 561 562 /* sessions for unsealing key and data */ 563 ret = oiap(tb, &authhandle1, enonce1); 564 if (ret < 0) { 565 pr_info("oiap failed (%d)\n", ret); 566 return ret; 567 } 568 ret = oiap(tb, &authhandle2, enonce2); 569 if (ret < 0) { 570 pr_info("oiap failed (%d)\n", ret); 571 return ret; 572 } 573 574 ordinal = htonl(TPM_ORD_UNSEAL); 575 ret = tpm_get_random(chip, nonceodd, TPM_NONCE_SIZE); 576 if (ret < 0) 577 return ret; 578 579 if (ret != TPM_NONCE_SIZE) { 580 pr_info("tpm_get_random failed (%d)\n", ret); 581 return -EIO; 582 } 583 ret = TSS_authhmac(authdata1, keyauth, TPM_NONCE_SIZE, 584 enonce1, nonceodd, cont, sizeof(uint32_t), 585 &ordinal, bloblen, blob, 0, 0); 586 if (ret < 0) 587 return ret; 588 ret = TSS_authhmac(authdata2, blobauth, TPM_NONCE_SIZE, 589 enonce2, nonceodd, cont, sizeof(uint32_t), 590 &ordinal, bloblen, blob, 0, 0); 591 if (ret < 0) 592 return ret; 593 594 /* build and send TPM request packet */ 595 tpm_buf_reset(tb, TPM_TAG_RQU_AUTH2_COMMAND, TPM_ORD_UNSEAL); 596 tpm_buf_append_u32(tb, keyhandle); 597 tpm_buf_append(tb, blob, bloblen); 598 tpm_buf_append_u32(tb, authhandle1); 599 tpm_buf_append(tb, nonceodd, TPM_NONCE_SIZE); 600 tpm_buf_append_u8(tb, cont); 601 tpm_buf_append(tb, authdata1, SHA1_DIGEST_SIZE); 602 tpm_buf_append_u32(tb, authhandle2); 603 tpm_buf_append(tb, nonceodd, TPM_NONCE_SIZE); 604 tpm_buf_append_u8(tb, cont); 605 tpm_buf_append(tb, authdata2, SHA1_DIGEST_SIZE); 606 607 ret = trusted_tpm_send(tb->data, tb->length); 608 if (ret < 0) { 609 pr_info("authhmac failed (%d)\n", ret); 610 return ret; 611 } 612 613 *datalen = LOAD32(tb->data, TPM_DATA_OFFSET); 614 ret = TSS_checkhmac2(tb->data, ordinal, nonceodd, 615 keyauth, SHA1_DIGEST_SIZE, 616 blobauth, SHA1_DIGEST_SIZE, 617 sizeof(uint32_t), TPM_DATA_OFFSET, 618 *datalen, TPM_DATA_OFFSET + sizeof(uint32_t), 0, 619 0); 620 if (ret < 0) { 621 pr_info("TSS_checkhmac2 failed (%d)\n", ret); 622 return ret; 623 } 624 memcpy(data, tb->data + TPM_DATA_OFFSET + sizeof(uint32_t), *datalen); 625 return 0; 626 } 627 628 /* 629 * Have the TPM seal(encrypt) the symmetric key 630 */ 631 static int key_seal(struct trusted_key_payload *p, 632 struct trusted_key_options *o) 633 { 634 struct tpm_buf tb; 635 int ret; 636 637 ret = tpm_buf_init(&tb, 0, 0); 638 if (ret) 639 return ret; 640 641 /* include migratable flag at end of sealed key */ 642 p->key[p->key_len] = p->migratable; 643 644 ret = tpm_seal(&tb, o->keytype, o->keyhandle, o->keyauth, 645 p->key, p->key_len + 1, p->blob, &p->blob_len, 646 o->blobauth, o->pcrinfo, o->pcrinfo_len); 647 if (ret < 0) 648 pr_info("srkseal failed (%d)\n", ret); 649 650 tpm_buf_destroy(&tb); 651 return ret; 652 } 653 654 /* 655 * Have the TPM unseal(decrypt) the symmetric key 656 */ 657 static int key_unseal(struct trusted_key_payload *p, 658 struct trusted_key_options *o) 659 { 660 struct tpm_buf tb; 661 int ret; 662 663 ret = tpm_buf_init(&tb, 0, 0); 664 if (ret) 665 return ret; 666 667 ret = tpm_unseal(&tb, o->keyhandle, o->keyauth, p->blob, p->blob_len, 668 o->blobauth, p->key, &p->key_len); 669 if (ret < 0) 670 pr_info("srkunseal failed (%d)\n", ret); 671 else 672 /* pull migratable flag out of sealed key */ 673 p->migratable = p->key[--p->key_len]; 674 675 tpm_buf_destroy(&tb); 676 return ret; 677 } 678 679 enum { 680 Opt_err, 681 Opt_keyhandle, Opt_keyauth, Opt_blobauth, 682 Opt_pcrinfo, Opt_pcrlock, Opt_migratable, 683 Opt_hash, 684 Opt_policydigest, 685 Opt_policyhandle, 686 }; 687 688 static const match_table_t key_tokens = { 689 {Opt_keyhandle, "keyhandle=%s"}, 690 {Opt_keyauth, "keyauth=%s"}, 691 {Opt_blobauth, "blobauth=%s"}, 692 {Opt_pcrinfo, "pcrinfo=%s"}, 693 {Opt_pcrlock, "pcrlock=%s"}, 694 {Opt_migratable, "migratable=%s"}, 695 {Opt_hash, "hash=%s"}, 696 {Opt_policydigest, "policydigest=%s"}, 697 {Opt_policyhandle, "policyhandle=%s"}, 698 {Opt_err, NULL} 699 }; 700 701 /* can have zero or more token= options */ 702 static int getoptions(char *c, struct trusted_key_payload *pay, 703 struct trusted_key_options *opt) 704 { 705 substring_t args[MAX_OPT_ARGS]; 706 char *p = c; 707 int token; 708 int res; 709 unsigned long handle; 710 unsigned long lock; 711 unsigned long token_mask = 0; 712 unsigned int digest_len; 713 int i; 714 int tpm2; 715 716 tpm2 = tpm_is_tpm2(chip); 717 if (tpm2 < 0) 718 return tpm2; 719 720 opt->hash = tpm2 ? HASH_ALGO_SHA256 : HASH_ALGO_SHA1; 721 722 if (!c) 723 return 0; 724 725 while ((p = strsep(&c, " \t"))) { 726 if (*p == '\0' || *p == ' ' || *p == '\t') 727 continue; 728 token = match_token(p, key_tokens, args); 729 if (test_and_set_bit(token, &token_mask)) 730 return -EINVAL; 731 732 switch (token) { 733 case Opt_pcrinfo: 734 opt->pcrinfo_len = strlen(args[0].from) / 2; 735 if (opt->pcrinfo_len > MAX_PCRINFO_SIZE) 736 return -EINVAL; 737 res = hex2bin(opt->pcrinfo, args[0].from, 738 opt->pcrinfo_len); 739 if (res < 0) 740 return -EINVAL; 741 break; 742 case Opt_keyhandle: 743 res = kstrtoul(args[0].from, 16, &handle); 744 if (res < 0) 745 return -EINVAL; 746 opt->keytype = SEAL_keytype; 747 opt->keyhandle = handle; 748 break; 749 case Opt_keyauth: 750 if (strlen(args[0].from) != 2 * SHA1_DIGEST_SIZE) 751 return -EINVAL; 752 res = hex2bin(opt->keyauth, args[0].from, 753 SHA1_DIGEST_SIZE); 754 if (res < 0) 755 return -EINVAL; 756 break; 757 case Opt_blobauth: 758 /* 759 * TPM 1.2 authorizations are sha1 hashes passed in as 760 * hex strings. TPM 2.0 authorizations are simple 761 * passwords (although it can take a hash as well) 762 */ 763 opt->blobauth_len = strlen(args[0].from); 764 765 if (opt->blobauth_len == 2 * TPM_DIGEST_SIZE) { 766 res = hex2bin(opt->blobauth, args[0].from, 767 TPM_DIGEST_SIZE); 768 if (res < 0) 769 return -EINVAL; 770 771 opt->blobauth_len = TPM_DIGEST_SIZE; 772 break; 773 } 774 775 if (tpm2 && opt->blobauth_len <= sizeof(opt->blobauth)) { 776 memcpy(opt->blobauth, args[0].from, 777 opt->blobauth_len); 778 break; 779 } 780 781 return -EINVAL; 782 783 break; 784 785 case Opt_migratable: 786 if (*args[0].from == '0') 787 pay->migratable = 0; 788 else if (*args[0].from != '1') 789 return -EINVAL; 790 break; 791 case Opt_pcrlock: 792 res = kstrtoul(args[0].from, 10, &lock); 793 if (res < 0) 794 return -EINVAL; 795 opt->pcrlock = lock; 796 break; 797 case Opt_hash: 798 if (test_bit(Opt_policydigest, &token_mask)) 799 return -EINVAL; 800 for (i = 0; i < HASH_ALGO__LAST; i++) { 801 if (!strcmp(args[0].from, hash_algo_name[i])) { 802 opt->hash = i; 803 break; 804 } 805 } 806 if (i == HASH_ALGO__LAST) 807 return -EINVAL; 808 if (!tpm2 && i != HASH_ALGO_SHA1) { 809 pr_info("TPM 1.x only supports SHA-1.\n"); 810 return -EINVAL; 811 } 812 break; 813 case Opt_policydigest: 814 digest_len = hash_digest_size[opt->hash]; 815 if (!tpm2 || strlen(args[0].from) != (2 * digest_len)) 816 return -EINVAL; 817 res = hex2bin(opt->policydigest, args[0].from, 818 digest_len); 819 if (res < 0) 820 return -EINVAL; 821 opt->policydigest_len = digest_len; 822 break; 823 case Opt_policyhandle: 824 if (!tpm2) 825 return -EINVAL; 826 res = kstrtoul(args[0].from, 16, &handle); 827 if (res < 0) 828 return -EINVAL; 829 opt->policyhandle = handle; 830 break; 831 default: 832 return -EINVAL; 833 } 834 } 835 return 0; 836 } 837 838 static struct trusted_key_options *trusted_options_alloc(void) 839 { 840 struct trusted_key_options *options; 841 int tpm2; 842 843 tpm2 = tpm_is_tpm2(chip); 844 if (tpm2 < 0) 845 return NULL; 846 847 options = kzalloc_obj(*options); 848 if (options) { 849 /* set any non-zero defaults */ 850 options->keytype = SRK_keytype; 851 852 if (!tpm2) 853 options->keyhandle = SRKHANDLE; 854 } 855 return options; 856 } 857 858 static int trusted_tpm_seal(struct trusted_key_payload *p, char *datablob) 859 { 860 struct trusted_key_options *options = NULL; 861 int ret = 0; 862 int tpm2; 863 864 tpm2 = tpm_is_tpm2(chip); 865 if (tpm2 < 0) 866 return tpm2; 867 868 options = trusted_options_alloc(); 869 if (!options) 870 return -ENOMEM; 871 872 ret = getoptions(datablob, p, options); 873 if (ret < 0) 874 goto out; 875 dump_options(options); 876 877 if (!options->keyhandle && !tpm2) { 878 ret = -EINVAL; 879 goto out; 880 } 881 882 if (tpm2) 883 ret = tpm2_seal_trusted(chip, p, options); 884 else 885 ret = key_seal(p, options); 886 if (ret < 0) { 887 pr_info("key_seal failed (%d)\n", ret); 888 goto out; 889 } 890 891 if (options->pcrlock) { 892 ret = pcrlock(options->pcrlock); 893 if (ret < 0) { 894 pr_info("pcrlock failed (%d)\n", ret); 895 goto out; 896 } 897 } 898 out: 899 kfree_sensitive(options); 900 return ret; 901 } 902 903 static int trusted_tpm_unseal(struct trusted_key_payload *p, char *datablob) 904 { 905 struct trusted_key_options *options = NULL; 906 int ret = 0; 907 int tpm2; 908 909 tpm2 = tpm_is_tpm2(chip); 910 if (tpm2 < 0) 911 return tpm2; 912 913 options = trusted_options_alloc(); 914 if (!options) 915 return -ENOMEM; 916 917 ret = getoptions(datablob, p, options); 918 if (ret < 0) 919 goto out; 920 dump_options(options); 921 922 if (!options->keyhandle && !tpm2) { 923 ret = -EINVAL; 924 goto out; 925 } 926 927 if (tpm2) 928 ret = tpm2_unseal_trusted(chip, p, options); 929 else 930 ret = key_unseal(p, options); 931 if (ret < 0) 932 pr_info("key_unseal failed (%d)\n", ret); 933 934 if (options->pcrlock) { 935 ret = pcrlock(options->pcrlock); 936 if (ret < 0) { 937 pr_info("pcrlock failed (%d)\n", ret); 938 goto out; 939 } 940 } 941 out: 942 kfree_sensitive(options); 943 return ret; 944 } 945 946 static int trusted_tpm_get_random(unsigned char *key, size_t key_len) 947 { 948 return tpm_get_random(chip, key, key_len); 949 } 950 951 static int __init init_digests(void) 952 { 953 int i; 954 955 digests = kzalloc_objs(*digests, chip->nr_allocated_banks); 956 if (!digests) 957 return -ENOMEM; 958 959 for (i = 0; i < chip->nr_allocated_banks; i++) 960 digests[i].alg_id = chip->allocated_banks[i].alg_id; 961 962 return 0; 963 } 964 965 static int __init trusted_tpm_init(void) 966 { 967 int ret; 968 969 chip = tpm_default_chip(); 970 if (!chip) 971 return -ENODEV; 972 973 ret = init_digests(); 974 if (ret < 0) 975 goto err_put; 976 ret = register_key_type(&key_type_trusted); 977 if (ret < 0) 978 goto err_free; 979 return 0; 980 err_free: 981 kfree(digests); 982 err_put: 983 put_device(&chip->dev); 984 return ret; 985 } 986 987 static void trusted_tpm_exit(void) 988 { 989 if (chip) { 990 put_device(&chip->dev); 991 kfree(digests); 992 unregister_key_type(&key_type_trusted); 993 } 994 } 995 996 struct trusted_key_ops trusted_key_tpm_ops = { 997 .migratable = 1, /* migratable by default */ 998 .init = trusted_tpm_init, 999 .seal = trusted_tpm_seal, 1000 .unseal = trusted_tpm_unseal, 1001 .get_random = trusted_tpm_get_random, 1002 .exit = trusted_tpm_exit, 1003 }; 1004