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