Skip to content

Commit f1507c6

Browse files
committed
Minor cleanups. Fixed test_TPM2_PCRSel test.
1 parent 592210f commit f1507c6

2 files changed

Lines changed: 64 additions & 47 deletions

File tree

src/tpm2_wrap.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3932,8 +3932,6 @@ int wolfTPM2_VerifyHashTicket(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key,
39323932

39333933
XMEMSET(&verifySigIn, 0, sizeof(verifySigIn));
39343934
verifySigIn.keyHandle = key->handle.hndl;
3935-
3936-
39373935
verifySigIn.digest.size = TPM2_GetHashDigestSize(hashAlg);
39383936
if (verifySigIn.digest.size <= 0) {
39393937
return BAD_FUNC_ARG;

tests/unit_tests.c

Lines changed: 64 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ static void test_wolfTPM2_Init(void)
101101
AssertIntNE(rc, 0);
102102
/* Test second argument, TPM2 IO Callbacks */
103103
rc = wolfTPM2_Init(&dev, NULL, NULL);
104-
#if defined(WOLFTPM_LINUX_DEV) || defined(WOLFTPM_SWTPM) || defined(WOLFTPM_WINAPI)
104+
#if defined(WOLFTPM_LINUX_DEV) || defined(WOLFTPM_SWTPM) || \
105+
defined(WOLFTPM_WINAPI)
105106
/* Custom IO Callbacks are not needed for Linux TIS driver */
106107
AssertIntEQ(rc, 0);
107108
#else
@@ -276,11 +277,12 @@ static void test_TPM2_PCRSel(void)
276277

277278
/* Test bad case - invalid PCR */
278279
XMEMSET(&pcr, 0, sizeof(pcr));
279-
pcrArray[0] = PCR_SELECT_MAX+1;
280+
pcrArray[0] = PCR_LAST+1;
280281
TPM2_SetupPCRSelArray(&pcr, TPM_ALG_SHA256, pcrArray, 1);
281282
if (pcr.count != 0) {
282283
rc = BAD_FUNC_ARG;
283284
}
285+
AssertIntEQ(rc, 0);
284286

285287
/* Test bad case - too many hash algorithms */
286288
XMEMSET(&pcr, 0, sizeof(pcr));
@@ -295,6 +297,7 @@ static void test_TPM2_PCRSel(void)
295297
if (pcr.count != HASH_COUNT) {
296298
rc = BAD_FUNC_ARG;
297299
}
300+
AssertIntEQ(rc, 0);
298301

299302
printf("Test TPM Wrapper:\tPCR Select Array:\t%s\n",
300303
rc == 0 ? "Passed" : "Failed");
@@ -346,7 +349,8 @@ static void test_TPM2_KDFa(void)
346349
0xd7, 0x04, 0xb6, 0x9a, 0x90, 0x2e, 0x9a, 0xde, 0x84, 0xc4};
347350
#endif
348351

349-
rc = TPM2_KDFa(TPM_ALG_SHA256, &keyIn, label, &contextU, &contextV, key, keyIn.size);
352+
rc = TPM2_KDFa(TPM_ALG_SHA256, &keyIn, label, &contextU, &contextV, key,
353+
keyIn.size);
350354
#ifdef WOLFTPM2_NO_WOLFCRYPT
351355
AssertIntEQ(NOT_COMPILED_IN, rc);
352356
#else
@@ -399,13 +403,12 @@ static void test_wolfTPM2_CSR(void)
399403

400404
#if !defined(WOLFTPM2_NO_WOLFCRYPT) && defined(HAVE_ECC) && \
401405
!defined(WOLFTPM2_NO_ASN)
402-
static void test_wolfTPM2_EccSignVerifyDig(const byte* digest, int digestSz,
406+
static void test_wolfTPM2_EccSignVerifyDig(WOLFTPM2_DEV* dev,
407+
WOLFTPM2_KEY* storageKey, const byte* digest, int digestSz,
403408
TPM_ECC_CURVE curve, TPMI_ALG_HASH hashAlg)
404409
{
405410
int rc;
406411
int verifyRes = 0;
407-
WOLFTPM2_DEV dev;
408-
WOLFTPM2_KEY storageKey;
409412
WOLFTPM2_KEY eccKey;
410413
TPMT_PUBLIC publicTemplate;
411414
byte sigRs[MAX_ECC_BYTES*2];
@@ -417,28 +420,19 @@ static void test_wolfTPM2_EccSignVerifyDig(const byte* digest, int digestSz,
417420
ecc_key wolfKey;
418421
int curveSize = TPM2_GetCurveSize(curve);
419422

420-
/* Initialize TPM */
421-
rc = wolfTPM2_Init(&dev, TPM2_IoCb, NULL);
422-
AssertIntEQ(rc, 0);
423-
424423
/* -- Use TPM key to sign and verify with wolfCrypt -- */
425-
/* Create storage key */
426-
rc = wolfTPM2_CreateSRK(&dev, &storageKey, TPM_ALG_ECC,
427-
(byte*)gStorageKeyAuth, sizeof(gStorageKeyAuth)-1);
428-
AssertIntEQ(rc, 0);
429-
430424
/* Create ECC key for signing */
431425
rc = wolfTPM2_GetKeyTemplate_ECC_ex(&publicTemplate, hashAlg,
432426
(TPMA_OBJECT_sensitiveDataOrigin | TPMA_OBJECT_userWithAuth |
433427
TPMA_OBJECT_sign | TPMA_OBJECT_noDA),
434428
curve, TPM_ALG_ECDSA, hashAlg);
435429
AssertIntEQ(rc, 0);
436-
rc = wolfTPM2_CreateAndLoadKey(&dev, &eccKey, &storageKey.handle,
430+
rc = wolfTPM2_CreateAndLoadKey(dev, &eccKey, &storageKey->handle,
437431
&publicTemplate, (byte*)gKeyAuth, sizeof(gKeyAuth)-1);
438432
AssertIntEQ(rc, 0);
439433

440434
/* Sign with TPM */
441-
rc = wolfTPM2_SignHashScheme(&dev, &eccKey, digest, digestSz,
435+
rc = wolfTPM2_SignHashScheme(dev, &eccKey, digest, digestSz,
442436
sigRs, (int*)&sigRsSz, TPM_ALG_ECDSA, hashAlg);
443437
AssertIntEQ(rc, 0);
444438

@@ -459,7 +453,7 @@ static void test_wolfTPM2_EccSignVerifyDig(const byte* digest, int digestSz,
459453
AssertIntEQ(rc, 0);
460454

461455
/* Convert TPM key to wolfCrypt key for verification */
462-
rc = wolfTPM2_EccKey_TpmToWolf(&dev, &eccKey, &wolfKey);
456+
rc = wolfTPM2_EccKey_TpmToWolf(dev, &eccKey, &wolfKey);
463457
AssertIntEQ(rc, 0);
464458

465459
/* Verify TPM signature with wolfCrypt */
@@ -469,7 +463,7 @@ static void test_wolfTPM2_EccSignVerifyDig(const byte* digest, int digestSz,
469463

470464
/* Cleanup first wolfCrypt key */
471465
wc_ecc_free(&wolfKey);
472-
wolfTPM2_UnloadHandle(&dev, &eccKey.handle);
466+
wolfTPM2_UnloadHandle(dev, &eccKey.handle);
473467

474468

475469
/* -- Use wolfCrypt key to sign and verify with TPM -- */
@@ -478,13 +472,13 @@ static void test_wolfTPM2_EccSignVerifyDig(const byte* digest, int digestSz,
478472
AssertIntEQ(rc, 0);
479473

480474
/* Generate new ECC key with wolfCrypt */
481-
rc = wc_ecc_make_key(wolfTPM2_GetRng(&dev), curveSize, &wolfKey);
475+
rc = wc_ecc_make_key(wolfTPM2_GetRng(dev), curveSize, &wolfKey);
482476
AssertIntEQ(rc, 0);
483477

484478
/* Sign with wolfCrypt */
485479
sigSz = (word32)sizeof(sig);
486-
rc = wc_ecc_sign_hash(digest, digestSz, sig, &sigSz,
487-
wolfTPM2_GetRng(&dev), &wolfKey);
480+
rc = wc_ecc_sign_hash(digest, digestSz, sig, &sigSz, wolfTPM2_GetRng(dev),
481+
&wolfKey);
488482
AssertIntEQ(rc, 0);
489483

490484
/* Decode ECDSA Header */
@@ -496,7 +490,7 @@ static void test_wolfTPM2_EccSignVerifyDig(const byte* digest, int digestSz,
496490
AssertIntEQ(rc, 0);
497491

498492
/* Convert wolfCrypt key to TPM key for verification */
499-
rc = wolfTPM2_EccKey_WolfToTpm(&dev, &wolfKey, &eccKey);
493+
rc = wolfTPM2_EccKey_WolfToTpm(dev, &wolfKey, &eccKey);
500494
AssertIntEQ(rc, 0);
501495

502496
/* combine R and S at key size (zero pad leading) */
@@ -506,42 +500,66 @@ static void test_wolfTPM2_EccSignVerifyDig(const byte* digest, int digestSz,
506500
XMEMSET(&sigRs[curveSize], 0, curveSize-sLen);
507501

508502
/* Verify wolfCrypt signature with TPM */
509-
rc = wolfTPM2_VerifyHashScheme(&dev, &eccKey, sigRs, curveSize*2,
503+
rc = wolfTPM2_VerifyHashScheme(dev, &eccKey, sigRs, curveSize*2,
510504
digest, digestSz, TPM_ALG_ECDSA, hashAlg);
511505
AssertIntEQ(rc, 0);
512506

513507
/* Cleanup */
514508
wc_ecc_free(&wolfKey);
515-
wolfTPM2_UnloadHandle(&dev, &eccKey.handle);
516-
wolfTPM2_UnloadHandle(&dev, &storageKey.handle);
517-
wolfTPM2_Cleanup(&dev);
509+
wolfTPM2_UnloadHandle(dev, &eccKey.handle);
518510

519-
printf("Test TPM Wrapper:\tSign/Verify Interop (digestSz=%d, curve=%d, hashAlg=%d):\t%s\n",
520-
digestSz, curve, hashAlg, rc == 0 ? "Passed" : "Failed");
511+
printf("Test TPM Wrapper:\t"
512+
"Sign/Verify (DigSz=%d, CurveSz=%d, Hash=%s):"
513+
"\t%s\n",
514+
digestSz, TPM2_GetCurveSize(curve), TPM2_GetAlgName(hashAlg),
515+
rc == 0 ? "Passed" : "Failed");
521516
}
522517

523518
/* Test with smaller, same and larger digest sizes using different ECC curves.
524519
* Interop sign and verify with wolfCrypt and TPM */
525520
static void test_wolfTPM2_EccSignVerify(void)
526521
{
527-
int i;
522+
int rc, i;
528523
byte digest[TPM_MAX_DIGEST_SIZE];
524+
WOLFTPM2_DEV dev;
525+
WOLFTPM2_KEY storageKey;
529526

530-
for (i = 0; i < 64; i++) {
531-
digest[i] = (byte)(0x11 + i);
532-
}
527+
/* Initialize TPM */
528+
rc = wolfTPM2_Init(&dev, TPM2_IoCb, NULL);
529+
AssertIntEQ(rc, 0);
530+
531+
/* Create storage key */
532+
rc = wolfTPM2_CreateSRK(&dev, &storageKey, TPM_ALG_ECC,
533+
(byte*)gStorageKeyAuth, sizeof(gStorageKeyAuth)-1);
534+
AssertIntEQ(rc, 0);
533535

534-
test_wolfTPM2_EccSignVerifyDig(digest, 20, TPM_ECC_NIST_P256, TPM_ALG_SHA256);
535-
test_wolfTPM2_EccSignVerifyDig(digest, 32, TPM_ECC_NIST_P256, TPM_ALG_SHA256);
536-
test_wolfTPM2_EccSignVerifyDig(digest, 48, TPM_ECC_NIST_P256, TPM_ALG_SHA256);
537-
test_wolfTPM2_EccSignVerifyDig(digest, 64, TPM_ECC_NIST_P256, TPM_ALG_SHA256);
538536

539-
#if defined(HAVE_ECC384) && ECC_MIN_KEY_SZ <= 384
540-
test_wolfTPM2_EccSignVerifyDig(digest, 20, TPM_ECC_NIST_P384, TPM_ALG_SHA384);
541-
test_wolfTPM2_EccSignVerifyDig(digest, 32, TPM_ECC_NIST_P384, TPM_ALG_SHA384);
542-
test_wolfTPM2_EccSignVerifyDig(digest, 48, TPM_ECC_NIST_P384, TPM_ALG_SHA384);
543-
test_wolfTPM2_EccSignVerifyDig(digest, 64, TPM_ECC_NIST_P384, TPM_ALG_SHA384);
537+
for (i = 0; i < (int)sizeof(digest); i++) {
538+
digest[i] = (byte)i;
539+
}
540+
541+
test_wolfTPM2_EccSignVerifyDig(&dev, &storageKey, digest, 20,
542+
TPM_ECC_NIST_P256, TPM_ALG_SHA256);
543+
test_wolfTPM2_EccSignVerifyDig(&dev, &storageKey, digest, 32,
544+
TPM_ECC_NIST_P256, TPM_ALG_SHA256);
545+
test_wolfTPM2_EccSignVerifyDig(&dev, &storageKey, digest, 48,
546+
TPM_ECC_NIST_P256, TPM_ALG_SHA256);
547+
test_wolfTPM2_EccSignVerifyDig(&dev, &storageKey, digest, 64,
548+
TPM_ECC_NIST_P256, TPM_ALG_SHA256);
549+
550+
#if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
551+
test_wolfTPM2_EccSignVerifyDig(&dev, &storageKey, digest, 20,
552+
TPM_ECC_NIST_P384, TPM_ALG_SHA384);
553+
test_wolfTPM2_EccSignVerifyDig(&dev, &storageKey, digest, 32,
554+
TPM_ECC_NIST_P384, TPM_ALG_SHA384);
555+
test_wolfTPM2_EccSignVerifyDig(&dev, &storageKey, digest, 48,
556+
TPM_ECC_NIST_P384, TPM_ALG_SHA384);
557+
test_wolfTPM2_EccSignVerifyDig(&dev, &storageKey, digest, 64,
558+
TPM_ECC_NIST_P384, TPM_ALG_SHA384);
544559
#endif
560+
561+
wolfTPM2_UnloadHandle(&dev, &storageKey.handle);
562+
wolfTPM2_Cleanup(&dev);
545563
}
546564
#endif
547565

@@ -628,7 +646,8 @@ static void test_wolfTPM2_PCRPolicy(void)
628646
digest, &digestSz, NULL, 0);
629647
AssertIntEQ(rc, 0);
630648

631-
AssertIntEQ(XMEMCMP(digest, expectedPolicyAuth, sizeof(expectedPolicyAuth)), 0);
649+
AssertIntEQ(XMEMCMP(digest, expectedPolicyAuth, sizeof(expectedPolicyAuth)),
650+
0);
632651

633652
rc = wolfTPM2_ResetPCR(&dev, pcrIndex);
634653
AssertIntEQ(rc, 0);
@@ -828,12 +847,12 @@ int unit_tests(int argc, char *argv[])
828847
#endif
829848
test_wolfTPM2_KeyBlob(TPM_ALG_RSA);
830849
test_wolfTPM2_KeyBlob(TPM_ALG_ECC);
831-
test_wolfTPM2_Cleanup();
832-
test_wolfTPM2_thread_local_storage();
833850
#if !defined(WOLFTPM2_NO_WOLFCRYPT) && defined(HAVE_ECC) && \
834851
!defined(WOLFTPM2_NO_ASN)
835852
test_wolfTPM2_EccSignVerify();
836853
#endif
854+
test_wolfTPM2_Cleanup();
855+
test_wolfTPM2_thread_local_storage();
837856
#endif /* !WOLFTPM2_NO_WRAPPER */
838857

839858
return 0;

0 commit comments

Comments
 (0)