Skip to content

Commit 9beaf2f

Browse files
committed
F-3501 - https://fenrir.wolfssl.com/finding/3501 - wolfTPM2_GetKeyTemplate_KeyedHash: scheme defaults to TPM_ALG_NULL when neither sign nor decrypt is requested
1 parent 5f7a00e commit 9beaf2f

2 files changed

Lines changed: 46 additions & 3 deletions

File tree

src/tpm2_wrap.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7372,9 +7372,20 @@ int wolfTPM2_GetKeyTemplate_KeyedHash(TPMT_PUBLIC* publicTemplate,
73727372
TPMA_OBJECT_noDA |
73737373
(isSign ? TPMA_OBJECT_sign : 0) |
73747374
(isDecrypt ? TPMA_OBJECT_decrypt : 0));
7375-
publicTemplate->parameters.keyedHashDetail.scheme.scheme = TPM_ALG_HMAC;
7376-
publicTemplate->parameters.keyedHashDetail.scheme.details.hmac.hashAlg =
7377-
hashAlg;
7375+
/* HMAC scheme requires the sign attribute. When the caller asks for
7376+
* neither sign nor decrypt, treat this as a data/seal-style keyed-hash
7377+
* object and use TPM_ALG_NULL so the template is loadable and not
7378+
* stuck with an unusable HMAC binding. */
7379+
if (isSign || isDecrypt) {
7380+
publicTemplate->parameters.keyedHashDetail.scheme.scheme =
7381+
TPM_ALG_HMAC;
7382+
publicTemplate->parameters.keyedHashDetail.scheme.details.hmac.hashAlg
7383+
= hashAlg;
7384+
}
7385+
else {
7386+
publicTemplate->parameters.keyedHashDetail.scheme.scheme =
7387+
TPM_ALG_NULL;
7388+
}
73787389
return TPM_RC_SUCCESS;
73797390
}
73807391

tests/unit_tests.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2156,6 +2156,37 @@ static void test_wolfTPM2_LoadEccPublicKey_Ex(void)
21562156
#endif
21572157
}
21582158

2159+
/* wolfTPM2_GetKeyTemplate_KeyedHash must default scheme to TPM_ALG_NULL
2160+
* when neither isSign nor isDecrypt is set; an HMAC scheme without the
2161+
* sign attribute produces an unusable keyed-hash object. */
2162+
static void test_wolfTPM2_GetKeyTemplate_KeyedHash_Scheme(void)
2163+
{
2164+
#if !defined(WOLFTPM2_NO_WOLFCRYPT)
2165+
int rc;
2166+
TPMT_PUBLIC tpl;
2167+
2168+
/* Data/seal-style: isSign=0, isDecrypt=0 -> scheme must be NULL */
2169+
XMEMSET(&tpl, 0, sizeof(tpl));
2170+
rc = wolfTPM2_GetKeyTemplate_KeyedHash(&tpl, TPM_ALG_SHA256, 0, 0);
2171+
AssertIntEQ(rc, TPM_RC_SUCCESS);
2172+
AssertIntEQ(tpl.parameters.keyedHashDetail.scheme.scheme, TPM_ALG_NULL);
2173+
AssertIntEQ((int)(tpl.objectAttributes & TPMA_OBJECT_sign), 0);
2174+
AssertIntEQ((int)(tpl.objectAttributes & TPMA_OBJECT_decrypt), 0);
2175+
2176+
/* HMAC-style: isSign=1 -> scheme HMAC + hashAlg + sign attribute */
2177+
XMEMSET(&tpl, 0, sizeof(tpl));
2178+
rc = wolfTPM2_GetKeyTemplate_KeyedHash(&tpl, TPM_ALG_SHA256, 1, 0);
2179+
AssertIntEQ(rc, TPM_RC_SUCCESS);
2180+
AssertIntEQ(tpl.parameters.keyedHashDetail.scheme.scheme, TPM_ALG_HMAC);
2181+
AssertIntEQ(tpl.parameters.keyedHashDetail.scheme.details.hmac.hashAlg,
2182+
TPM_ALG_SHA256);
2183+
AssertIntEQ((int)(tpl.objectAttributes & TPMA_OBJECT_sign),
2184+
(int)TPMA_OBJECT_sign);
2185+
2186+
printf("Test TPM Wrapper:\tKeyedHash template scheme:\tPassed\n");
2187+
#endif
2188+
}
2189+
21592190
/* wolfTPM2_NVCreateAuthPolicy must derive nameAlg from authPolicySz so
21602191
* the policy digest hash matches the index's nameAlg. Bug-mode hardcoded
21612192
* SHA-256 nameAlg, which made SHA-384/SHA-512 policies unsatisfiable.
@@ -3684,6 +3715,7 @@ int unit_tests(int argc, char *argv[])
36843715
test_wolfTPM2_RsaEncryptDecrypt_OversizedBufferE();
36853716
test_wolfTPM2_SignHashScheme_DigestSize();
36863717
test_wolfTPM2_NVCreateAuthPolicy_NameAlg();
3718+
test_wolfTPM2_GetKeyTemplate_KeyedHash_Scheme();
36873719
test_wolfTPM2_LoadEccPublicKey_Ex();
36883720
test_TPM2_KeyedHashScheme_XorSerialize();
36893721
test_TPM2_Signature_EcSchnorrSm2Serialize();

0 commit comments

Comments
 (0)