Skip to content

Commit 9a0cb66

Browse files
committed
F-2985 - https://fenrir.wolfssl.com/finding/2985 - Add known-answer unit test for wolfTPM2_ComputeName
1 parent 3de7072 commit 9a0cb66

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

tests/unit_tests.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,61 @@ static void test_TPM2_KDFe(void)
979979
}
980980
#endif
981981

982+
/* Known-answer test for wolfTPM2_ComputeName.
983+
* Reference: nameAlg(BE) || SHA256(serialized TPMT_PUBLIC) computed
984+
* independently for an ECC P-256 key with known field values. */
985+
#if !defined(WOLFTPM2_NO_WOLFCRYPT) && defined(HAVE_ECC)
986+
static void test_wolfTPM2_ComputeName(void)
987+
{
988+
int rc;
989+
TPM2B_PUBLIC pub;
990+
TPM2B_NAME name;
991+
static const byte expectedName[] = {
992+
0x00, 0x0b, 0x35, 0xc3, 0x57, 0x9d, 0xf1, 0xb5,
993+
0x24, 0x6a, 0xb7, 0x9a, 0x0a, 0xf2, 0xd5, 0x44,
994+
0xcb, 0x63, 0x2a, 0x80, 0xe2, 0x24, 0x1d, 0xd3,
995+
0x84, 0x06, 0x34, 0xe4, 0x38, 0x00, 0x61, 0xc0,
996+
0x2e, 0x6f
997+
};
998+
999+
XMEMSET(&pub, 0, sizeof(pub));
1000+
pub.publicArea.type = TPM_ALG_ECC;
1001+
pub.publicArea.nameAlg = TPM_ALG_SHA256;
1002+
pub.publicArea.objectAttributes = (TPMA_OBJECT_sign | TPMA_OBJECT_decrypt |
1003+
TPMA_OBJECT_userWithAuth | TPMA_OBJECT_noDA);
1004+
pub.publicArea.authPolicy.size = 0;
1005+
pub.publicArea.parameters.eccDetail.symmetric.algorithm = TPM_ALG_NULL;
1006+
pub.publicArea.parameters.eccDetail.scheme.scheme = TPM_ALG_NULL;
1007+
pub.publicArea.parameters.eccDetail.curveID = TPM_ECC_NIST_P256;
1008+
pub.publicArea.parameters.eccDetail.kdf.scheme = TPM_ALG_NULL;
1009+
pub.publicArea.unique.ecc.x.size = 32;
1010+
XMEMSET(pub.publicArea.unique.ecc.x.buffer, 0x11, 32);
1011+
pub.publicArea.unique.ecc.y.size = 32;
1012+
XMEMSET(pub.publicArea.unique.ecc.y.buffer, 0x22, 32);
1013+
1014+
XMEMSET(&name, 0, sizeof(name));
1015+
rc = wolfTPM2_ComputeName(&pub, &name);
1016+
AssertIntEQ(rc, TPM_RC_SUCCESS);
1017+
AssertIntEQ(name.size, (int)sizeof(expectedName));
1018+
AssertIntEQ(0, XMEMCMP(name.name, expectedName, sizeof(expectedName)));
1019+
1020+
/* Test NULL args */
1021+
rc = wolfTPM2_ComputeName(NULL, &name);
1022+
AssertIntEQ(rc, BAD_FUNC_ARG);
1023+
rc = wolfTPM2_ComputeName(&pub, NULL);
1024+
AssertIntEQ(rc, BAD_FUNC_ARG);
1025+
1026+
/* Test nameAlg = TPM_ALG_NULL returns success with empty name */
1027+
pub.publicArea.nameAlg = TPM_ALG_NULL;
1028+
XMEMSET(&name, 0xFF, sizeof(name));
1029+
rc = wolfTPM2_ComputeName(&pub, &name);
1030+
AssertIntEQ(rc, TPM_RC_SUCCESS);
1031+
AssertIntEQ(name.size, 0);
1032+
1033+
printf("Test TPM Wrapper:\tComputeName:\t\tPassed\n");
1034+
}
1035+
#endif
1036+
9821037
static void test_GetAlgId(void)
9831038
{
9841039
TPM_ALG_ID alg = TPM2_GetAlgId("SHA256");
@@ -1723,6 +1778,7 @@ int unit_tests(int argc, char *argv[])
17231778
test_TPM2_HashNvPublic();
17241779
#if !defined(WOLFTPM2_NO_WOLFCRYPT) && defined(HAVE_ECC)
17251780
test_TPM2_KDFe();
1781+
test_wolfTPM2_ComputeName();
17261782
#endif
17271783
test_GetAlgId();
17281784
test_wolfTPM2_ReadPublicKey();

0 commit comments

Comments
 (0)