From 54568730c3c2d04f15fe41de72f656712f00ff62 Mon Sep 17 00:00:00 2001 From: Arkadiusz Bokowy Date: Mon, 23 Oct 2023 19:36:34 +0200 Subject: [PATCH] Initialize CHIP stack before running app tests (#29092) * Initialize CHIP stack before running app tests * Patch add_entropy_source() to make it idempotent * Do not add entropy source more than once --- src/app/tests/AppTestContext.cpp | 2 ++ src/crypto/CHIPCryptoPAL.h | 7 +++++++ src/platform/Zephyr/PlatformManagerImpl.cpp | 11 ++++++++--- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/app/tests/AppTestContext.cpp b/src/app/tests/AppTestContext.cpp index ac81702d513aed..ffde9dd26021d4 100644 --- a/src/app/tests/AppTestContext.cpp +++ b/src/app/tests/AppTestContext.cpp @@ -41,6 +41,7 @@ namespace Test { CHIP_ERROR AppContext::Init() { ReturnErrorOnFailure(Super::Init()); + ReturnErrorOnFailure(chip::DeviceLayer::PlatformMgr().InitChipStack()); ReturnErrorOnFailure(chip::app::InteractionModelEngine::GetInstance()->Init(&GetExchangeManager(), &GetFabricTable(), app::reporting::GetDefaultReportScheduler())); @@ -57,6 +58,7 @@ void AppContext::Shutdown() Access::ResetAccessControlToDefault(); chip::app::InteractionModelEngine::GetInstance()->Shutdown(); + chip::DeviceLayer::PlatformMgr().Shutdown(); Super::Shutdown(); } diff --git a/src/crypto/CHIPCryptoPAL.h b/src/crypto/CHIPCryptoPAL.h index 6f6bc6edf1984c..60c88b5e399d49 100644 --- a/src/crypto/CHIPCryptoPAL.h +++ b/src/crypto/CHIPCryptoPAL.h @@ -967,6 +967,13 @@ CHIP_ERROR DRBG_get_bytes(uint8_t * out_buffer, size_t out_length); typedef int (*entropy_source)(void * data, uint8_t * output, size_t len, size_t * olen); /** @brief A function to add entropy sources to crypto library + * + * This function can be called multiple times to add multiple entropy sources. However, + * once the entropy source is added, it cannot be removed. Please make sure that the + * entropy source is valid for the lifetime of the application. Also, make sure that the + * same entropy source is not added multiple times, e.g.: by calling this function + * in class constructor or initialization function. + * * @param fn_source Function pointer to the entropy source * @param p_source Data that should be provided when fn_source is called * @param threshold Minimum required from source before entropy is released diff --git a/src/platform/Zephyr/PlatformManagerImpl.cpp b/src/platform/Zephyr/PlatformManagerImpl.cpp index 3b97a5c4a346e1..59f3a0df45314b 100644 --- a/src/platform/Zephyr/PlatformManagerImpl.cpp +++ b/src/platform/Zephyr/PlatformManagerImpl.cpp @@ -46,6 +46,7 @@ PlatformManagerImpl PlatformManagerImpl::sInstance{ sChipThreadStack }; static k_timer sOperationalHoursSavingTimer; #if !defined(CONFIG_NORDIC_SECURITY_BACKEND) && !defined(CONFIG_MBEDTLS_ZEPHYR_ENTROPY) +static bool sChipStackEntropySourceAdded = false; static int app_entropy_source(void * data, unsigned char * output, size_t len, size_t * olen) { const struct device * entropy = DEVICE_DT_GET(DT_CHOSEN(zephyr_entropy)); @@ -118,9 +119,13 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) SuccessOrExit(err); #if !defined(CONFIG_NORDIC_SECURITY_BACKEND) && !defined(CONFIG_MBEDTLS_ZEPHYR_ENTROPY) - // Add entropy source based on Zephyr entropy driver - err = chip::Crypto::add_entropy_source(app_entropy_source, NULL, kThreshold); - SuccessOrExit(err); + if (!sChipStackEntropySourceAdded) + { + // Add entropy source based on Zephyr entropy driver + err = chip::Crypto::add_entropy_source(app_entropy_source, NULL, kThreshold); + SuccessOrExit(err); + sChipStackEntropySourceAdded = true; + } #endif // !defined(CONFIG_NORDIC_SECURITY_BACKEND) && !defined(CONFIG_MBEDTLS_ZEPHYR_ENTROPY) // Call _InitChipStack() on the generic implementation base class to finish the initialization process.