@@ -592,6 +592,7 @@ static void test_wolfTPM2_SetAuthHandle_PolicyAuthOffset(void)
592592static void test_wolfTPM2_StartSession_SaltedEncryptAttrs (void )
593593{
594594#if !defined(WOLFTPM2_NO_WOLFCRYPT )
595+ int rc ;
595596 WOLFTPM2_DEV dev ;
596597 WOLFTPM2_KEY tpmKey ;
597598 WOLFTPM2_SESSION session ;
@@ -601,17 +602,26 @@ static void test_wolfTPM2_StartSession_SaltedEncryptAttrs(void)
601602 XMEMSET (& tpmKey , 0 , sizeof (tpmKey ));
602603 XMEMSET (& session , 0 , sizeof (session ));
603604
605+ /* Initialize so TPM2_GetNonceNoLock and dependent code paths have a
606+ * valid context. Skip if no TPM is reachable. */
607+ rc = wolfTPM2_Init (& dev , TPM2_IoCb , NULL );
608+ if (rc != 0 ) {
609+ printf ("Test TPM Wrapper:\tStartSession salted enc attrs:\tSkipped\n" );
610+ return ;
611+ }
612+
604613 /* tpmKey with a non-NULL handle, no auth */
605614 tpmKey .handle .hndl = 0x80000000 ;
606615
607- /* Best effort - if no TPM is present the call returns early after the
608- * SetAuth path, which is what we want to inspect . */
616+ /* The call will fail later ( no real key with that handle) but the
617+ * SetAuth path that sets sessionAttributes runs first . */
609618 (void )wolfTPM2_StartSession (& dev , & session , & tpmKey , NULL ,
610619 TPM_SE_HMAC , TPM_ALG_CFB );
611620
612621 AssertIntEQ ((int )(dev .session [0 ].sessionAttributes & expected ),
613622 (int )expected );
614623
624+ wolfTPM2_Cleanup (& dev );
615625 printf ("Test TPM Wrapper:\tStartSession salted enc attrs:\tPassed\n" );
616626#endif
617627}
@@ -1902,11 +1912,11 @@ static void test_TPM2_ECC_Parameters_EcdaaResponseParse(void)
19021912 printf ("Test TPM Wrapper:\tEcdaaResponseParse:\t\tPassed\n" );
19031913}
19041914
1905- /* TPM2_Packet_ParseSignature must explicitly recognize TPM_ALG_NULL as a
1906- * zero-payload signature so subsequent fields stay aligned. The previous
1907- * default-fallthrough lumped TPM_ALG_NULL together with unknown algorithms,
1908- * making the property "Parse(Append(NULL signature)) consumes exactly the
1909- * sigAlg bytes" depend on undocumented behavior. */
1915+ /* TPM2_Packet_AppendSignature / ParseSignature must explicitly recognize
1916+ * TPM_ALG_NULL as a zero-payload signature so subsequent fields stay
1917+ * aligned. The previous default-fallthrough lumped TPM_ALG_NULL together
1918+ * with unknown algorithms, making the property "Parse(Append(NULL))
1919+ * consumes exactly the sigAlg bytes" depend on undocumented behavior. */
19101920static void test_TPM2_ParseSignature_NullAlg (void )
19111921{
19121922 TPM2_Packet packet ;
@@ -1936,6 +1946,29 @@ static void test_TPM2_ParseSignature_NullAlg(void)
19361946 sentinel = (UINT16 )((buf [packet .pos ] << 8 ) | buf [packet .pos + 1 ]);
19371947 AssertIntEQ (sentinel , 0xDEAD );
19381948
1949+ /* Round-trip: Append a TPM_ALG_NULL signature into a fresh packet and
1950+ * verify only the 2-byte sigAlg was written. A future regression that
1951+ * drops the explicit case (defaulting to silent fallthrough) would
1952+ * still pass for Parse but the Append side is also locked in here. */
1953+ XMEMSET (buf , 0 , sizeof (buf ));
1954+ XMEMSET (& packet , 0 , sizeof (packet ));
1955+ XMEMSET (& sig , 0 , sizeof (sig ));
1956+ sig .sigAlg = TPM_ALG_NULL ;
1957+ packet .buf = buf ;
1958+ packet .size = sizeof (buf );
1959+ packet .pos = 0 ;
1960+ TPM2_Packet_AppendSignature (& packet , & sig );
1961+ AssertIntEQ (packet .pos , 2 );
1962+ AssertIntEQ (buf [0 ], (byte )((TPM_ALG_NULL >> 8 ) & 0xFF ));
1963+ AssertIntEQ (buf [1 ], (byte )(TPM_ALG_NULL & 0xFF ));
1964+
1965+ /* Re-parse confirms the round-trip. */
1966+ XMEMSET (& sig , 0 , sizeof (sig ));
1967+ packet .pos = 0 ;
1968+ TPM2_Packet_ParseSignature (& packet , & sig );
1969+ AssertIntEQ (sig .sigAlg , TPM_ALG_NULL );
1970+ AssertIntEQ (packet .pos , 2 );
1971+
19391972 printf ("Test TPM Wrapper:\tParseSignature NULL alg:\tPassed\n" );
19401973}
19411974
@@ -2224,6 +2257,44 @@ static void test_wolfTPM2_GetKeyTemplate_KeyedHash_Scheme(void)
22242257#endif
22252258}
22262259
2260+ /* wolfTPM2_VerifyHashTicket must apply the same RSA-strict / ECDSA-permissive
2261+ * digest size policy as wolfTPM2_SignHashScheme. The bounds check fires
2262+ * before any TPM call so this test does not require a working TPM. */
2263+ static void test_wolfTPM2_VerifyHashTicket_DigestSize (void )
2264+ {
2265+ #if !defined(WOLFTPM2_NO_WOLFCRYPT ) && !defined(NO_RSA )
2266+ int rc ;
2267+ WOLFTPM2_DEV dev ;
2268+ WOLFTPM2_KEY key ;
2269+ byte digest [TPM_MAX_DIGEST_SIZE ];
2270+ byte sig [MAX_RSA_KEY_BYTES ];
2271+
2272+ XMEMSET (& dev , 0 , sizeof (dev ));
2273+ XMEMSET (& key , 0 , sizeof (key ));
2274+ XMEMSET (digest , 0xCC , sizeof (digest ));
2275+ XMEMSET (sig , 0 , sizeof (sig ));
2276+ key .handle .hndl = 0x80000000 ;
2277+ key .pub .publicArea .type = TPM_ALG_RSA ;
2278+
2279+ /* SHA-256 digest (32) + hashAlg=SHA512 -> RSA mismatch -> BUFFER_E */
2280+ rc = wolfTPM2_VerifyHashTicket (& dev , & key , sig , 256 , digest , 32 ,
2281+ TPM_ALG_RSASSA , TPM_ALG_SHA512 , NULL );
2282+ AssertIntEQ (rc , BUFFER_E );
2283+
2284+ /* Oversized digest (64) + hashAlg=SHA256 -> BUFFER_E */
2285+ rc = wolfTPM2_VerifyHashTicket (& dev , & key , sig , 256 , digest , 64 ,
2286+ TPM_ALG_RSASSA , TPM_ALG_SHA256 , NULL );
2287+ AssertIntEQ (rc , BUFFER_E );
2288+
2289+ /* Negative digestSz -> BUFFER_E */
2290+ rc = wolfTPM2_VerifyHashTicket (& dev , & key , sig , 256 , digest , -1 ,
2291+ TPM_ALG_RSASSA , TPM_ALG_SHA256 , NULL );
2292+ AssertIntEQ (rc , BUFFER_E );
2293+
2294+ printf ("Test TPM Wrapper:\tVerifyHashTicket size:\t\tPassed\n" );
2295+ #endif
2296+ }
2297+
22272298/* wolfTPM2_NVCreateAuthPolicy must derive nameAlg from authPolicySz so
22282299 * the policy digest hash matches the index's nameAlg. Bug-mode hardcoded
22292300 * SHA-256 nameAlg, which made SHA-384/SHA-512 policies unsatisfiable.
@@ -2354,6 +2425,12 @@ static void test_TPM2_BrainpoolCurveMapping(void)
23542425 AssertIntEQ (TPM2_GetTpmCurve (ECC_SECP256R1 ), TPM_ECC_NIST_P256 );
23552426 AssertIntEQ (TPM2_GetWolfCurve (TPM_ECC_NIST_P256 ), ECC_SECP256R1 );
23562427
2428+ /* TPM2_GetCurveSize must report the correct byte size for the new
2429+ * Brainpool curve IDs (32 / 48 / 64). */
2430+ AssertIntEQ (TPM2_GetCurveSize (TPM_ECC_BP_P256_R1 ), 32 );
2431+ AssertIntEQ (TPM2_GetCurveSize (TPM_ECC_BP_P384_R1 ), 48 );
2432+ AssertIntEQ (TPM2_GetCurveSize (TPM_ECC_BP_P512_R1 ), 64 );
2433+
23572434 printf ("Test TPM Wrapper:\tBrainpool curve mapping:\tPassed\n" );
23582435#endif
23592436}
@@ -3752,6 +3829,7 @@ int unit_tests(int argc, char *argv[])
37523829 test_TPM2_BrainpoolCurveMapping ();
37533830 test_wolfTPM2_RsaEncryptDecrypt_OversizedBufferE ();
37543831 test_wolfTPM2_SignHashScheme_DigestSize ();
3832+ test_wolfTPM2_VerifyHashTicket_DigestSize ();
37553833 test_wolfTPM2_NVCreateAuthPolicy_NameAlg ();
37563834 test_wolfTPM2_GetKeyTemplate_KeyedHash_Scheme ();
37573835 test_wolfTPM2_LoadEccPublicKey_Ex ();
0 commit comments