|
28 | 28 | #include <wolftpm/tpm2.h> |
29 | 29 | #include <wolftpm/tpm2_wrap.h> |
30 | 30 | #include <wolftpm/tpm2_param_enc.h> |
| 31 | +#include <wolftpm/tpm2_asn.h> |
31 | 32 |
|
32 | 33 | #include <hal/tpm_io.h> |
33 | 34 | #include <examples/tpm_test.h> |
@@ -396,6 +397,153 @@ static void test_wolfTPM2_CSR(void) |
396 | 397 | #endif |
397 | 398 | } |
398 | 399 |
|
| 400 | +#if !defined(WOLFTPM2_NO_WOLFCRYPT) && defined(HAVE_ECC) |
| 401 | +static void test_wolfTPM2_EccSignVerifyDig(const byte* digest, int digestSz, |
| 402 | + TPM_ECC_CURVE curve, TPMI_ALG_HASH hashAlg) |
| 403 | +{ |
| 404 | + int rc; |
| 405 | + int verifyRes = 0; |
| 406 | + WOLFTPM2_DEV dev; |
| 407 | + WOLFTPM2_KEY storageKey; |
| 408 | + WOLFTPM2_KEY eccKey; |
| 409 | + TPMT_PUBLIC publicTemplate; |
| 410 | + byte sigRs[MAX_ECC_BYTES*2]; |
| 411 | + word32 sigRsSz = (word32)sizeof(sigRs); |
| 412 | + byte sig[ECC_MAX_SIG_SIZE]; |
| 413 | + word32 sigSz; |
| 414 | + byte *r, *s; |
| 415 | + word32 rLen, sLen; |
| 416 | + ecc_key wolfKey; |
| 417 | + int curveSize = TPM2_GetCurveSize(curve); |
| 418 | + |
| 419 | + /* Initialize TPM */ |
| 420 | + rc = wolfTPM2_Init(&dev, TPM2_IoCb, NULL); |
| 421 | + AssertIntEQ(rc, 0); |
| 422 | + |
| 423 | + /* -- Use TPM key to sign and verify with wolfCrypt -- */ |
| 424 | + /* Create storage key */ |
| 425 | + rc = wolfTPM2_CreateSRK(&dev, &storageKey, TPM_ALG_ECC, |
| 426 | + (byte*)gStorageKeyAuth, sizeof(gStorageKeyAuth)-1); |
| 427 | + AssertIntEQ(rc, 0); |
| 428 | + |
| 429 | + /* Create ECC key for signing */ |
| 430 | + rc = wolfTPM2_GetKeyTemplate_ECC_ex(&publicTemplate, hashAlg, |
| 431 | + (TPMA_OBJECT_sensitiveDataOrigin | TPMA_OBJECT_userWithAuth | |
| 432 | + TPMA_OBJECT_sign | TPMA_OBJECT_noDA), |
| 433 | + curve, TPM_ALG_ECDSA, hashAlg); |
| 434 | + AssertIntEQ(rc, 0); |
| 435 | + rc = wolfTPM2_CreateAndLoadKey(&dev, &eccKey, &storageKey.handle, |
| 436 | + &publicTemplate, (byte*)gKeyAuth, sizeof(gKeyAuth)-1); |
| 437 | + AssertIntEQ(rc, 0); |
| 438 | + |
| 439 | + /* Sign with TPM */ |
| 440 | + rc = wolfTPM2_SignHashScheme(&dev, &eccKey, digest, digestSz, |
| 441 | + sigRs, (int*)&sigRsSz, TPM_ALG_ECDSA, hashAlg); |
| 442 | + AssertIntEQ(rc, 0); |
| 443 | + |
| 444 | + /* Make sure leading zero's not required are trimmed */ |
| 445 | + rLen = sLen = sigRsSz / 2; |
| 446 | + r = &sigRs[0]; |
| 447 | + s = &sigRs[rLen]; |
| 448 | + r = TPM2_ASN_TrimZeros(r, &rLen); |
| 449 | + s = TPM2_ASN_TrimZeros(s, &sLen); |
| 450 | + |
| 451 | + /* Encode ECDSA Header */ |
| 452 | + sigSz = (word32)sizeof(sig); |
| 453 | + rc = wc_ecc_rs_raw_to_sig(r, rLen, s, sLen, sig, &sigSz); |
| 454 | + AssertIntEQ(rc, 0); |
| 455 | + |
| 456 | + /* Initialize wolfCrypt ECC key */ |
| 457 | + rc = wc_ecc_init(&wolfKey); |
| 458 | + AssertIntEQ(rc, 0); |
| 459 | + |
| 460 | + /* Convert TPM key to wolfCrypt key for verification */ |
| 461 | + rc = wolfTPM2_EccKey_TpmToWolf(&dev, &eccKey, &wolfKey); |
| 462 | + AssertIntEQ(rc, 0); |
| 463 | + |
| 464 | + /* Verify TPM signature with wolfCrypt */ |
| 465 | + rc = wc_ecc_verify_hash(sig, sigSz, digest, digestSz, &verifyRes, &wolfKey); |
| 466 | + AssertIntEQ(rc, 0); |
| 467 | + AssertIntEQ(verifyRes, 1); /* 1 indicates successful verification */ |
| 468 | + |
| 469 | + /* Cleanup first wolfCrypt key */ |
| 470 | + wc_ecc_free(&wolfKey); |
| 471 | + wolfTPM2_UnloadHandle(&dev, &eccKey.handle); |
| 472 | + |
| 473 | + |
| 474 | + /* -- Use wolfCrypt key to sign and verify with TPM -- */ |
| 475 | + /* Initialize new wolfCrypt ECC key */ |
| 476 | + rc = wc_ecc_init(&wolfKey); |
| 477 | + AssertIntEQ(rc, 0); |
| 478 | + |
| 479 | + /* Generate new ECC key with wolfCrypt */ |
| 480 | + rc = wc_ecc_make_key(wolfTPM2_GetRng(&dev), curveSize, &wolfKey); |
| 481 | + AssertIntEQ(rc, 0); |
| 482 | + |
| 483 | + /* Sign with wolfCrypt */ |
| 484 | + sigSz = (word32)sizeof(sig); |
| 485 | + rc = wc_ecc_sign_hash(digest, digestSz, sig, &sigSz, |
| 486 | + wolfTPM2_GetRng(&dev), &wolfKey); |
| 487 | + AssertIntEQ(rc, 0); |
| 488 | + |
| 489 | + /* Decode ECDSA Header */ |
| 490 | + r = sigRs; |
| 491 | + s = &sigRs[MAX_ECC_BYTES]; |
| 492 | + rLen = sLen = MAX_ECC_BYTES; |
| 493 | + rc = wc_ecc_sig_to_rs(sig, |
| 494 | + sigSz, r, &rLen, s, &sLen); |
| 495 | + AssertIntEQ(rc, 0); |
| 496 | + |
| 497 | + /* Convert wolfCrypt key to TPM key for verification */ |
| 498 | + rc = wolfTPM2_EccKey_WolfToTpm(&dev, &wolfKey, &eccKey); |
| 499 | + AssertIntEQ(rc, 0); |
| 500 | + |
| 501 | + /* combine R and S at key size (zero pad leading) */ |
| 502 | + XMEMCPY(&sigRs[curveSize-rLen], r, rLen); |
| 503 | + XMEMSET(&sigRs[0], 0, curveSize-rLen); |
| 504 | + XMEMCPY(&sigRs[curveSize + (curveSize-sLen)], s, sLen); |
| 505 | + XMEMSET(&sigRs[curveSize], 0, curveSize-sLen); |
| 506 | + |
| 507 | + /* Verify wolfCrypt signature with TPM */ |
| 508 | + rc = wolfTPM2_VerifyHashScheme(&dev, &eccKey, sigRs, curveSize*2, |
| 509 | + digest, digestSz, TPM_ALG_ECDSA, hashAlg); |
| 510 | + AssertIntEQ(rc, 0); |
| 511 | + |
| 512 | + /* Cleanup */ |
| 513 | + wc_ecc_free(&wolfKey); |
| 514 | + wolfTPM2_UnloadHandle(&dev, &eccKey.handle); |
| 515 | + wolfTPM2_UnloadHandle(&dev, &storageKey.handle); |
| 516 | + wolfTPM2_Cleanup(&dev); |
| 517 | + |
| 518 | + printf("Test TPM Wrapper:\tSign/Verify Interop (digestSz=%d, curve=%d, hashAlg=%d):\t%s\n", |
| 519 | + digestSz, curve, hashAlg, rc == 0 ? "Passed" : "Failed"); |
| 520 | +} |
| 521 | + |
| 522 | +/* Test with smaller, same and larger digest sizes using different ECC curves. |
| 523 | + * Interop sign and verify with wolfCrypt and TPM */ |
| 524 | +static void test_wolfTPM2_EccSignVerify(void) |
| 525 | +{ |
| 526 | + int i; |
| 527 | + byte digest[TPM_MAX_DIGEST_SIZE]; |
| 528 | + |
| 529 | + for (i = 0; i < 64; i++) { |
| 530 | + digest[i] = (byte)(0x11 + i); |
| 531 | + } |
| 532 | + |
| 533 | + test_wolfTPM2_EccSignVerifyDig(digest, 20, TPM_ECC_NIST_P256, TPM_ALG_SHA256); |
| 534 | + test_wolfTPM2_EccSignVerifyDig(digest, 32, TPM_ECC_NIST_P256, TPM_ALG_SHA256); |
| 535 | + test_wolfTPM2_EccSignVerifyDig(digest, 48, TPM_ECC_NIST_P256, TPM_ALG_SHA256); |
| 536 | + test_wolfTPM2_EccSignVerifyDig(digest, 64, TPM_ECC_NIST_P256, TPM_ALG_SHA256); |
| 537 | + |
| 538 | +#if defined(HAVE_ECC384) && ECC_MIN_KEY_SZ <= 384 |
| 539 | + test_wolfTPM2_EccSignVerifyDig(digest, 20, TPM_ECC_NIST_P384, TPM_ALG_SHA384); |
| 540 | + test_wolfTPM2_EccSignVerifyDig(digest, 32, TPM_ECC_NIST_P384, TPM_ALG_SHA384); |
| 541 | + test_wolfTPM2_EccSignVerifyDig(digest, 48, TPM_ECC_NIST_P384, TPM_ALG_SHA384); |
| 542 | + test_wolfTPM2_EccSignVerifyDig(digest, 64, TPM_ECC_NIST_P384, TPM_ALG_SHA384); |
| 543 | +#endif |
| 544 | +} |
| 545 | +#endif |
| 546 | + |
399 | 547 | #if !defined(WOLFTPM2_NO_WOLFCRYPT) && defined(WOLFTPM2_PEM_DECODE) && \ |
400 | 548 | !defined(NO_RSA) |
401 | 549 | static WOLFTPM2_KEY authKey; /* also used for test_wolfTPM2_PCRPolicy */ |
@@ -681,6 +829,9 @@ int unit_tests(int argc, char *argv[]) |
681 | 829 | test_wolfTPM2_KeyBlob(TPM_ALG_ECC); |
682 | 830 | test_wolfTPM2_Cleanup(); |
683 | 831 | test_wolfTPM2_thread_local_storage(); |
| 832 | + #if !defined(WOLFTPM2_NO_WOLFCRYPT) && defined(HAVE_ECC) |
| 833 | + test_wolfTPM2_EccSignVerify(); |
| 834 | + #endif |
684 | 835 | #endif /* !WOLFTPM2_NO_WRAPPER */ |
685 | 836 |
|
686 | 837 | return 0; |
|
0 commit comments