From a71682d7655e6e51208b5104067fc58b9b2cdb62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Duda?= Date: Thu, 26 Sep 2024 23:32:29 +0200 Subject: [PATCH] [crypto] PSA API: align HKDF-SHA256 unit tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit allows running HKDF-SHA256 unit tests using PSA Crypto API. Signed-off-by: Ɓukasz Duda --- tests/unit/test_hkdf_sha256.cpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/tests/unit/test_hkdf_sha256.cpp b/tests/unit/test_hkdf_sha256.cpp index 2b3c01dc3..219272c70 100644 --- a/tests/unit/test_hkdf_sha256.cpp +++ b/tests/unit/test_hkdf_sha256.cpp @@ -134,6 +134,10 @@ void TestHkdfSha256(void) uint8_t outKey[kMaxOuttKey]; Crypto::Key testInputKey; +#if OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE + Crypto::Storage::KeyRef keyRef; +#endif + printf("\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"); DumpBuffer("\nInput Key", test->mInKey, test->mInKeyLength); DumpBuffer("\nSalt", test->mSalt, test->mSaltLength); @@ -141,9 +145,16 @@ void TestHkdfSha256(void) DumpBuffer("\nExpected Output Key", test->mOutKey, test->mOutKeyLength); memset(outKey, kFillByte, sizeof(outKey)); - memset(&testInputKey, 0x00, sizeof(testInputKey)); - testInputKey.mKey = test->mInKey; - testInputKey.mKeyLength = test->mInKeyLength; + +#if OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE + SuccessOrQuit(Crypto::Storage::ImportKey( + keyRef, Crypto::Storage::kKeyTypeDerive, Crypto::Storage::kKeyAlgorithmHkdfSha256, + Crypto::Storage::kUsageDerive, Crypto::Storage::kTypeVolatile, test->mInKey, test->mInKeyLength)); + + testInputKey.SetAsKeyRef(keyRef); +#else + testInputKey.Set(test->mInKey, test->mInKeyLength); +#endif hkdf.Extract(test->mSalt, test->mSaltLength, testInputKey); hkdf.Expand(test->mInfo, test->mInfoLength, outKey, test->mOutKeyLength); @@ -156,6 +167,10 @@ void TestHkdfSha256(void) { VerifyOrQuit(outKey[i] == kFillByte, "HKDF-SHA-256 wrote beyond output key length"); } + +#if OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE + Crypto::Storage::DestroyKey(keyRef); +#endif } testFreeInstance(instance);