From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by mx.groups.io with SMTP id smtpd.web08.4763.1663914316873603685 for ; Thu, 22 Sep 2022 23:25:25 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=GImeUDdP; spf=pass (domain: intel.com, ip: 134.134.136.20, mailfrom: qi1.zhang@intel.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663914325; x=1695450325; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=xM7fflCd72hEoyaQgG34AA+S6o5RywHZS1OjhWMcf3Y=; b=GImeUDdP49LWdYzYsAPuopUni93uV6l93yvCXpBRyM7QmdMKKLhXk8gM tRrEfXNJ8szeVuQmjENWkgWiat42fOxVLCNL8TWtdVAaDLwI+om7eiDs6 uZMzH+MSdntXBStBZeqCM4ixlQFDHWqKd6mKfxUoxBekUoLLpaAk1/UqN WyWiPS0Y60SKkgiCHcFC8RHjaRUW945L2kcbwsaCcacsfyqn/slIhVXEe Gd/upp6ZUtwMP0Ds7fTU+QK3bjc5o8uxU4dYM4Zx2i0yaYOp1Twc/7G+j hRjHMmh5fHPRvGlAJvS34cpVwfqRLdh51aXBD+hlTzowsX5LCoV70yF+k g==; X-IronPort-AV: E=McAfee;i="6500,9779,10478"; a="287636124" X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="287636124" Received: from orsmga006.jf.intel.com ([10.7.209.51]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 23:25:25 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="597761526" Received: from shwdesssddpdqi.ccr.corp.intel.com ([10.239.157.129]) by orsmga006.jf.intel.com with ESMTP; 22 Sep 2022 23:25:23 -0700 From: "Qi Zhang" To: devel@edk2.groups.io Cc: Qi Zhang , Jiewen Yao , Jian J Wang , Xiaoyu Lu , Guomin Jiang Subject: [PATCH V2 4/4] CryptoPkg: add Hkdf UnitTest. Date: Fri, 23 Sep 2022 14:25:09 +0800 Message-Id: <7d12ef7fd8116070184be1146a0c043ab29ab3e4.1663913961.git.qi1.zhang@intel.com> X-Mailer: git-send-email 2.26.2.windows.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D4033 Signed-off-by: Qi Zhang Cc: Jiewen Yao Cc: Jian J Wang Cc: Xiaoyu Lu Cc: Guomin Jiang Reviewed-by: Jiewen Yao --- .../BaseCryptLib/BaseCryptLibUnitTests.c | 29 +-- .../UnitTest/Library/BaseCryptLib/HkdfTests.c | 202 ++++++++++++++++++ .../Library/BaseCryptLib/TestBaseCryptLib.h | 3 + .../BaseCryptLib/TestBaseCryptLibHost.inf | 1 + .../BaseCryptLib/TestBaseCryptLibShell.inf | 1 + 5 files changed, 222 insertions(+), 14 deletions(-) create mode 100644 CryptoPkg/Test/UnitTest/Library/BaseCryptLib/HkdfTests.c diff --git a/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/BaseCryptLibUnitT= ests.c b/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/BaseCryptLibUnitTests= .c index 3c57aead1e..dc81143b43 100644 --- a/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/BaseCryptLibUnitTests.c +++ b/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/BaseCryptLibUnitTests.c @@ -11,20 +11,21 @@ SUITE_DESC mSuiteDesc[] =3D { //=0D // Title--------------------------Package-------------------Sup--Tdn----= TestNum------------TestDesc=0D //=0D - { "EKU verify tests", "CryptoPkg.BaseCryptLib", NULL, NULL, &= mPkcs7EkuTestNum, mPkcs7EkuTest },=0D - { "HASH verify tests", "CryptoPkg.BaseCryptLib", NULL, NULL, &= mHashTestNum, mHashTest },=0D - { "HMAC verify tests", "CryptoPkg.BaseCryptLib", NULL, NULL, &= mHmacTestNum, mHmacTest },=0D - { "BlockCipher verify tests", "CryptoPkg.BaseCryptLib", NULL, NULL, &= mBlockCipherTestNum, mBlockCipherTest },=0D - { "RSA verify tests", "CryptoPkg.BaseCryptLib", NULL, NULL, &= mRsaTestNum, mRsaTest },=0D - { "RSA PSS verify tests", "CryptoPkg.BaseCryptLib", NULL, NULL, &= mRsaPssTestNum, mRsaPssTest },=0D - { "RSACert verify tests", "CryptoPkg.BaseCryptLib", NULL, NULL, &= mRsaCertTestNum, mRsaCertTest },=0D - { "PKCS7 verify tests", "CryptoPkg.BaseCryptLib", NULL, NULL, &= mPkcs7TestNum, mPkcs7Test },=0D - { "PKCS5 verify tests", "CryptoPkg.BaseCryptLib", NULL, NULL, &= mPkcs5TestNum, mPkcs5Test },=0D - { "Authenticode verify tests", "CryptoPkg.BaseCryptLib", NULL, NULL, &= mAuthenticodeTestNum, mAuthenticodeTest },=0D - { "ImageTimestamp verify tests", "CryptoPkg.BaseCryptLib", NULL, NULL, &= mImageTimestampTestNum, mImageTimestampTest },=0D - { "DH verify tests", "CryptoPkg.BaseCryptLib", NULL, NULL, &= mDhTestNum, mDhTest },=0D - { "PRNG verify tests", "CryptoPkg.BaseCryptLib", NULL, NULL, &= mPrngTestNum, mPrngTest },=0D - { "OAEP encrypt verify tests", "CryptoPkg.BaseCryptLib", NULL, NULL, &= mOaepTestNum, mOaepTest },=0D + { "EKU verify tests", "CryptoPkg.BaseCryptLib", NULL, NULL,= &mPkcs7EkuTestNum, mPkcs7EkuTest },=0D + { "HASH verify tests", "CryptoPkg.BaseCryptLib", NULL, NULL,= &mHashTestNum, mHashTest },=0D + { "HMAC verify tests", "CryptoPkg.BaseCryptLib", NULL, NULL,= &mHmacTestNum, mHmacTest },=0D + { "BlockCipher verify tests", "CryptoPkg.BaseCryptLib", NULL, NULL,= &mBlockCipherTestNum, mBlockCipherTest },=0D + { "RSA verify tests", "CryptoPkg.BaseCryptLib", NULL, NULL,= &mRsaTestNum, mRsaTest },=0D + { "RSA PSS verify tests", "CryptoPkg.BaseCryptLib", NULL, NULL,= &mRsaPssTestNum, mRsaPssTest },=0D + { "RSACert verify tests", "CryptoPkg.BaseCryptLib", NULL, NULL,= &mRsaCertTestNum, mRsaCertTest },=0D + { "PKCS7 verify tests", "CryptoPkg.BaseCryptLib", NULL, NULL,= &mPkcs7TestNum, mPkcs7Test },=0D + { "PKCS5 verify tests", "CryptoPkg.BaseCryptLib", NULL, NULL,= &mPkcs5TestNum, mPkcs5Test },=0D + { "Authenticode verify tests", "CryptoPkg.BaseCryptLib", NULL, NULL,= &mAuthenticodeTestNum, mAuthenticodeTest },=0D + { "ImageTimestamp verify tests", "CryptoPkg.BaseCryptLib", NULL, NULL,= &mImageTimestampTestNum, mImageTimestampTest },=0D + { "DH verify tests", "CryptoPkg.BaseCryptLib", NULL, NULL,= &mDhTestNum, mDhTest },=0D + { "PRNG verify tests", "CryptoPkg.BaseCryptLib", NULL, NULL,= &mPrngTestNum, mPrngTest },=0D + { "OAEP encrypt verify tests", "CryptoPkg.BaseCryptLib", NULL, NULL,= &mOaepTestNum, mOaepTest },=0D + { "Hkdf extract and expand tests", "CryptoPkg.BaseCryptLib", NULL, NULL,= &mHkdfTestNum, mHkdfTest },=0D };=0D =0D EFI_STATUS=0D diff --git a/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/HkdfTests.c b/Cry= ptoPkg/Test/UnitTest/Library/BaseCryptLib/HkdfTests.c new file mode 100644 index 0000000000..bb2b5ea1b8 --- /dev/null +++ b/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/HkdfTests.c @@ -0,0 +1,202 @@ +/** @file=0D + Application for Hkdf Primitives Validation.=0D +=0D +Copyright (c) 2022, Intel Corporation. All rights reserved.
=0D +SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include "TestBaseCryptLib.h"=0D +=0D +/**=0D + * HKDF KAT from RFC 5869 Appendix A. Test Vectors=0D + * https://www.rfc-editor.org/rfc/rfc5869.html=0D + **/=0D +UINT8 mHkdfSha256Ikm[22] =3D {=0D + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,=0D + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,=0D + 0x0b, 0x0b=0D +};=0D +=0D +UINT8 mHkdfSha256Salt[13] =3D {=0D + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,=0D + 0x0a, 0x0b, 0x0c,=0D +};=0D +=0D +UINT8 mHkdfSha256Info[10] =3D {=0D + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9,=0D +};=0D +=0D +UINT8 mHkdfSha256Prk[32] =3D {=0D + 0x07, 0x77, 0x09, 0x36, 0x2c, 0x2e, 0x32, 0xdf, 0x0d, 0xdc,=0D + 0x3f, 0x0d, 0xc4, 0x7b, 0xba, 0x63, 0x90, 0xb6, 0xc7, 0x3b,=0D + 0xb5, 0x0f, 0x9c, 0x31, 0x22, 0xec, 0x84, 0x4a, 0xd7, 0xc2,=0D + 0xb3, 0xe5,=0D +};=0D +=0D +UINT8 mHkdfSha256Okm[42] =3D {=0D + 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a, 0x90, 0x43,=0D + 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a, 0x2d, 0x2d, 0x0a, 0x90,=0D + 0xcf, 0x1a, 0x5a, 0x4c, 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4,=0D + 0xc5, 0xbf, 0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18,=0D + 0x58, 0x65,=0D +};=0D +=0D +/**=0D + * This Hkdf-Sha384 test vector is form Project Wycheproof=0D + * developed and maintained by members of Google Security Team.=0D + * https://github.com/google/wycheproof/blob/master/testvectors/hkdf_sha38= 4_test.json=0D + **/=0D +UINT8 mHkdfSha384Ikm[16] =3D {=0D + 0x86, 0x77, 0xdc, 0x79, 0x23, 0x3e, 0xf3, 0x48, 0x07, 0x77,=0D + 0xc4, 0xc6, 0x01, 0xef, 0x4f, 0x0b,=0D +};=0D +=0D +UINT8 mHkdfSha384Salt[16] =3D {=0D + 0xad, 0x88, 0xdb, 0x71, 0x82, 0x44, 0xe2, 0xcb, 0x60, 0xe3,=0D + 0x5f, 0x87, 0x4d, 0x7a, 0xd8, 0x1f,=0D +};=0D +=0D +UINT8 mHkdfSha384Info[20] =3D {=0D + 0xa3, 0x8f, 0x63, 0x4d, 0x94, 0x78, 0x19, 0xa9, 0xbf, 0xa7,=0D + 0x92, 0x17, 0x4b, 0x42, 0xba, 0xa2, 0x0c, 0x9f, 0xce, 0x15,=0D +};=0D +=0D +UINT8 mHkdfSha384Prk[48] =3D {=0D + 0x60, 0xae, 0xa0, 0xde, 0xca, 0x97, 0x62, 0xaa, 0x43, 0xaf,=0D + 0x0e, 0x77, 0xa8, 0x0f, 0xb7, 0x76, 0xd0, 0x08, 0x19, 0x62,=0D + 0xf8, 0x30, 0xb5, 0x0d, 0x92, 0x08, 0x92, 0x7a, 0x8a, 0xd5,=0D + 0x6a, 0x3d, 0xc4, 0x4a, 0x5d, 0xfe, 0xb6, 0xb4, 0x79, 0x2f,=0D + 0x97, 0x92, 0x71, 0xe6, 0xcb, 0x08, 0x86, 0x52,=0D +};=0D +=0D +UINT8 mHkdfSha384Okm[64] =3D {=0D + 0x75, 0x85, 0x46, 0x36, 0x2a, 0x07, 0x0c, 0x0f, 0x13, 0xcb,=0D + 0xfb, 0xf1, 0x75, 0x6e, 0x8f, 0x29, 0xb7, 0x81, 0x9f, 0xb9,=0D + 0x03, 0xc7, 0xed, 0x4f, 0x97, 0xa5, 0x6b, 0xe3, 0xc8, 0xf8,=0D + 0x1e, 0x8c, 0x37, 0xae, 0xf5, 0xc0, 0xf8, 0xe5, 0xd2, 0xb1,=0D + 0x7e, 0xb1, 0xaa, 0x02, 0xec, 0x04, 0xc3, 0x3f, 0x54, 0x6c,=0D + 0xb2, 0xf3, 0xd1, 0x93, 0xe9, 0x30, 0xa9, 0xf8, 0x9e, 0xc9,=0D + 0xce, 0x3a, 0x82, 0xb5=0D +};=0D +=0D +UNIT_TEST_STATUS=0D +EFIAPI=0D +TestVerifyHkdfSha256 (=0D + IN UNIT_TEST_CONTEXT Context=0D + )=0D +{=0D + UINT8 PrkOut[32];=0D + UINT8 Out[42];=0D + BOOLEAN Status;=0D +=0D + /* HKDF-SHA-256 digest Validation*/=0D +=0D + ZeroMem (PrkOut, sizeof (PrkOut));=0D + Status =3D HkdfSha256Extract (=0D + mHkdfSha256Ikm,=0D + sizeof (mHkdfSha256Ikm),=0D + mHkdfSha256Salt,=0D + sizeof (mHkdfSha256Salt),=0D + PrkOut,=0D + sizeof (PrkOut)=0D + );=0D + UT_ASSERT_TRUE (Status);=0D +=0D + UT_ASSERT_MEM_EQUAL (PrkOut, mHkdfSha256Prk, sizeof (mHkdfSha256Prk));=0D +=0D + ZeroMem (Out, sizeof (Out));=0D + Status =3D HkdfSha256Expand (=0D + mHkdfSha256Prk,=0D + sizeof (mHkdfSha256Prk),=0D + mHkdfSha256Info,=0D + sizeof (mHkdfSha256Info),=0D + Out,=0D + sizeof (Out)=0D + );=0D + UT_ASSERT_TRUE (Status);=0D +=0D + UT_ASSERT_MEM_EQUAL (Out, mHkdfSha256Okm, sizeof (mHkdfSha256Okm));=0D +=0D + ZeroMem (Out, sizeof (Out));=0D + Status =3D HkdfSha256ExtractAndExpand (=0D + mHkdfSha256Ikm,=0D + sizeof (mHkdfSha256Ikm),=0D + mHkdfSha256Salt,=0D + sizeof (mHkdfSha256Salt),=0D + mHkdfSha256Info,=0D + sizeof (mHkdfSha256Info),=0D + Out,=0D + sizeof (Out)=0D + );=0D + UT_ASSERT_TRUE (Status);=0D +=0D + UT_ASSERT_MEM_EQUAL (Out, mHkdfSha256Okm, sizeof (mHkdfSha256Okm));=0D +=0D + return UNIT_TEST_PASSED;=0D +}=0D +=0D +UNIT_TEST_STATUS=0D +EFIAPI=0D +TestVerifyHkdfSha384 (=0D + IN UNIT_TEST_CONTEXT Context=0D + )=0D +{=0D + UINT8 PrkOut[48];=0D + UINT8 Out[64];=0D + BOOLEAN Status;=0D +=0D + /* HKDF-SHA-384 digest Validation*/=0D + ZeroMem (PrkOut, sizeof (PrkOut));=0D + Status =3D HkdfSha384Extract (=0D + mHkdfSha384Ikm,=0D + sizeof (mHkdfSha384Ikm),=0D + mHkdfSha384Salt,=0D + sizeof (mHkdfSha384Salt),=0D + PrkOut,=0D + sizeof (PrkOut)=0D + );=0D + UT_ASSERT_TRUE (Status);=0D +=0D + UT_ASSERT_MEM_EQUAL (PrkOut, mHkdfSha384Prk, sizeof (mHkdfSha384Prk));=0D +=0D + ZeroMem (Out, sizeof (Out));=0D + Status =3D HkdfSha384Expand (=0D + mHkdfSha384Prk,=0D + sizeof (mHkdfSha384Prk),=0D + mHkdfSha384Info,=0D + sizeof (mHkdfSha384Info),=0D + Out,=0D + sizeof (Out)=0D + );=0D + UT_ASSERT_TRUE (Status);=0D +=0D + UT_ASSERT_MEM_EQUAL (Out, mHkdfSha384Okm, sizeof (mHkdfSha384Okm));=0D +=0D + ZeroMem (Out, sizeof (Out));=0D + Status =3D HkdfSha384ExtractAndExpand (=0D + mHkdfSha384Ikm,=0D + sizeof (mHkdfSha384Ikm),=0D + mHkdfSha384Salt,=0D + sizeof (mHkdfSha384Salt),=0D + mHkdfSha384Info,=0D + sizeof (mHkdfSha384Info),=0D + Out,=0D + sizeof (Out)=0D + );=0D + UT_ASSERT_TRUE (Status);=0D +=0D + UT_ASSERT_MEM_EQUAL (Out, mHkdfSha384Okm, sizeof (mHkdfSha384Okm));=0D +=0D + return UNIT_TEST_PASSED;=0D +}=0D +=0D +TEST_DESC mHkdfTest[] =3D {=0D + //=0D + // -----Description--------------------------------------Class----------= ------------Function---------------------------------Pre-------------------= --Post---------Context=0D + //=0D + { "TestVerifyHkdfSha256()", "CryptoPkg.BaseCryptLib.Hkdf", TestVerifyHkd= fSha256, NULL, NULL, NULL },=0D + { "TestVerifyHkdfSha384()", "CryptoPkg.BaseCryptLib.Hkdf", TestVerifyHkd= fSha384, NULL, NULL, NULL },=0D +};=0D +=0D +UINTN mHkdfTestNum =3D ARRAY_SIZE (mHkdfTest);=0D diff --git a/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/TestBaseCryptLib.= h b/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/TestBaseCryptLib.h index a6b3482742..b3aff86570 100644 --- a/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/TestBaseCryptLib.h +++ b/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/TestBaseCryptLib.h @@ -86,6 +86,9 @@ extern TEST_DESC mOaepTest[]; extern UINTN mRsaPssTestNum;=0D extern TEST_DESC mRsaPssTest[];=0D =0D +extern UINTN mHkdfTestNum;=0D +extern TEST_DESC mHkdfTest[];=0D +=0D /** Creates a framework you can use */=0D EFI_STATUS=0D EFIAPI=0D diff --git a/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/TestBaseCryptLibH= ost.inf b/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/TestBaseCryptLibHost= .inf index 399db596c2..e51877bded 100644 --- a/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/TestBaseCryptLibHost.inf +++ b/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/TestBaseCryptLibHost.inf @@ -37,6 +37,7 @@ OaepEncryptTests.c=0D RsaPssTests.c=0D ParallelhashTests.c=0D + HkdfTests.c=0D =0D [Packages]=0D MdePkg/MdePkg.dec=0D diff --git a/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/TestBaseCryptLibS= hell.inf b/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/TestBaseCryptLibShe= ll.inf index ca789aa6ad..81469f48e7 100644 --- a/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/TestBaseCryptLibShell.inf +++ b/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/TestBaseCryptLibShell.inf @@ -36,6 +36,7 @@ Pkcs7EkuTests.c=0D OaepEncryptTests.c=0D RsaPssTests.c=0D + HkdfTests.c=0D =0D [Packages]=0D MdePkg/MdePkg.dec=0D --=20 2.26.2.windows.1