@@ -932,6 +932,53 @@ static void test_TPM2_HashNvPublic(void)
932932#endif
933933}
934934
935+ /* Known-answer test for TPM2_KDFe (SP800-56A one-step KDF for ECC).
936+ * Reference: independently computed SHA-256(counter || Z || label || U || V)
937+ * with counter starting at 1. */
938+ #if !defined(WOLFTPM2_NO_WOLFCRYPT ) && defined(HAVE_ECC )
939+ static void test_TPM2_KDFe (void )
940+ {
941+ int rc ;
942+ TPM2B_DATA Z , partyU , partyV ;
943+ byte key [WC_SHA256_DIGEST_SIZE ];
944+ int i ;
945+ static const byte expectedKey [] = {
946+ 0x5e , 0x51 , 0xcd , 0x71 , 0xd3 , 0xe1 , 0x58 , 0xfc ,
947+ 0x44 , 0xc1 , 0x5c , 0x41 , 0xac , 0x74 , 0x3d , 0xd0 ,
948+ 0x96 , 0x91 , 0xc8 , 0x99 , 0x32 , 0xe8 , 0x38 , 0x08 ,
949+ 0xd6 , 0x56 , 0x35 , 0xf2 , 0x31 , 0xee , 0xba , 0xea
950+ };
951+
952+ /* Z = 0x01..0x20 (32 bytes) */
953+ Z .size = 32 ;
954+ for (i = 0 ; i < 32 ; i ++ ) Z .buffer [i ] = (byte )(i + 1 );
955+
956+ /* partyU = 0xAA repeated 32 bytes */
957+ partyU .size = 32 ;
958+ XMEMSET (partyU .buffer , 0xAA , 32 );
959+
960+ /* partyV = 0xBB repeated 32 bytes */
961+ partyV .size = 32 ;
962+ XMEMSET (partyV .buffer , 0xBB , 32 );
963+
964+ XMEMSET (key , 0 , sizeof (key ));
965+ rc = TPM2_KDFe (TPM_ALG_SHA256 , & Z , "SECRET" , & partyU , & partyV ,
966+ key , (UINT32 )sizeof (key ));
967+ AssertIntEQ (rc , (int )sizeof (key ));
968+ AssertIntEQ (0 , XMEMCMP (key , expectedKey , sizeof (expectedKey )));
969+
970+ /* Test NULL args */
971+ rc = TPM2_KDFe (TPM_ALG_SHA256 , NULL , "SECRET" , & partyU , & partyV ,
972+ key , sizeof (key ));
973+ AssertIntEQ (rc , BAD_FUNC_ARG );
974+ rc = TPM2_KDFe (TPM_ALG_SHA256 , & Z , "SECRET" , & partyU , & partyV ,
975+ NULL , sizeof (key ));
976+ AssertIntEQ (rc , BAD_FUNC_ARG );
977+
978+ printf ("Test TPM Wrapper:\tKDFe:\t\t\tPassed\n" );
979+ }
980+ #endif
981+
935982static void test_GetAlgId (void )
936983{
937984 TPM_ALG_ID alg = TPM2_GetAlgId ("SHA256" );
@@ -1674,6 +1721,9 @@ int unit_tests(int argc, char *argv[])
16741721 test_TPM2_ParamDec_AESCFB_Roundtrip ();
16751722 test_TPM2_ParamEncDec_Dispatch_Roundtrip ();
16761723 test_TPM2_HashNvPublic ();
1724+ #if !defined(WOLFTPM2_NO_WOLFCRYPT ) && defined(HAVE_ECC )
1725+ test_TPM2_KDFe ();
1726+ #endif
16771727 test_GetAlgId ();
16781728 test_wolfTPM2_ReadPublicKey ();
16791729 test_wolfTPM2_CSR ();
0 commit comments