@@ -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