Skip to content

Commit 383bdb4

Browse files
committed
Add test case for early returns from test_wolfTPM2_EncryptSecret
1 parent d97d95d commit 383bdb4

4 files changed

Lines changed: 41 additions & 3 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ pkcs7tpmsignedex.p7s
6060
examples/tls/tls_server
6161
examples/tls/tls_client_notpm
6262
tests/unit.test
63+
tests/unit.log
64+
unit.trs
6365
examples/keygen/create_primary
6466
examples/keygen/keyload
6567
examples/keygen/keygen

src/tpm2_wrap.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,9 +1595,7 @@ static int wolfTPM2_EncryptSecret_RSA(WOLFTPM2_DEV* dev, const WOLFTPM2_KEY* tpm
15951595
}
15961596

15971597
wc_FreeRsaKey(&rsaKey);
1598-
TPM2_ForceZero(&rsaKey, sizeof(rsaKey));
15991598
wc_FreeRng(&rng);
1600-
TPM2_ForceZero(&rng, sizeof(rng));
16011599

16021600
if (rc > 0) {
16031601
rc = (rc == secret->size) ? 0 /* success */ : BUFFER_E /* fail */;

tests/unit_tests.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,43 @@ static void test_TPM2_Policy_NULL_Args(void)
429429
printf("Test TPM2:\t\tPolicy NULL Args:\tPassed\n");
430430
}
431431

432+
static void test_wolfTPM2_EncryptSecret(void)
433+
{
434+
int rc;
435+
WOLFTPM2_DEV dev;
436+
WOLFTPM2_KEY tpmKey;
437+
TPM2B_DATA data;
438+
TPM2B_ENCRYPTED_SECRET secret;
439+
440+
XMEMSET(&tpmKey, 0, sizeof(tpmKey));
441+
XMEMSET(&data, 0, sizeof(data));
442+
XMEMSET(&secret, 0, sizeof(secret));
443+
444+
rc = wolfTPM2_Init(&dev, TPM2_IoCb, NULL);
445+
AssertIntEQ(rc, 0);
446+
447+
/* Test NULL tpmKey returns success (unsalted session) */
448+
rc = wolfTPM2_EncryptSecret(&dev, NULL, &data, &secret, "SECRET");
449+
AssertIntEQ(rc, TPM_RC_SUCCESS);
450+
451+
/* Test NULL dev returns BAD_FUNC_ARG */
452+
rc = wolfTPM2_EncryptSecret(NULL, &tpmKey, &data, &secret, "SECRET");
453+
AssertIntEQ(rc, BAD_FUNC_ARG);
454+
455+
/* Test NULL data returns BAD_FUNC_ARG */
456+
rc = wolfTPM2_EncryptSecret(&dev, &tpmKey, NULL, &secret, "SECRET");
457+
AssertIntEQ(rc, BAD_FUNC_ARG);
458+
459+
/* Test NULL secret returns BAD_FUNC_ARG */
460+
rc = wolfTPM2_EncryptSecret(&dev, &tpmKey, &data, NULL, "SECRET");
461+
AssertIntEQ(rc, BAD_FUNC_ARG);
462+
463+
wolfTPM2_Cleanup(&dev);
464+
465+
printf("Test TPM Wrapper:\tEncryptSecret:\t%s\n",
466+
rc == BAD_FUNC_ARG ? "Passed" : "Failed");
467+
}
468+
432469
static void test_wolfTPM2_Cleanup(void)
433470
{
434471
int rc;
@@ -1047,6 +1084,7 @@ int unit_tests(int argc, char *argv[])
10471084
test_wolfTPM_ImportPublicKey();
10481085
test_wolfTPM2_PCRPolicy();
10491086
#endif
1087+
test_wolfTPM2_EncryptSecret();
10501088
test_wolfTPM2_KeyBlob(TPM_ALG_RSA);
10511089
test_wolfTPM2_KeyBlob(TPM_ALG_ECC);
10521090
#if !defined(WOLFTPM2_NO_WOLFCRYPT) && defined(HAVE_ECC) && \

wolftpm/tpm2_wrap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3402,7 +3402,7 @@ WOLFTPM_API int wolfTPM2_ChangeHierarchyAuth(WOLFTPM2_DEV* dev, WOLFTPM2_SESSION
34023402
#define wolfTPM2_GetCurveSize TPM2_GetCurveSize
34033403

34043404
/* for encrypting secrets (like salt) used in auth sessions and external key import */
3405-
WOLFTPM_LOCAL int wolfTPM2_EncryptSecret(WOLFTPM2_DEV* dev, const WOLFTPM2_KEY* tpmKey,
3405+
WOLFTPM_API int wolfTPM2_EncryptSecret(WOLFTPM2_DEV* dev, const WOLFTPM2_KEY* tpmKey,
34063406
TPM2B_DATA *secret, TPM2B_ENCRYPTED_SECRET *encSecret, const char* label);
34073407

34083408

0 commit comments

Comments
 (0)