Skip to content

Commit

Permalink
Initialize CHIP stack before running app tests (#29092)
Browse files Browse the repository at this point in the history
* Initialize CHIP stack before running app tests

* Patch add_entropy_source() to make it idempotent

* Do not add entropy source more than once
  • Loading branch information
arkq authored and pull[bot] committed Feb 29, 2024
1 parent 5d942c4 commit 5456873
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/app/tests/AppTestContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()));

Expand All @@ -57,6 +58,7 @@ void AppContext::Shutdown()
Access::ResetAccessControlToDefault();

chip::app::InteractionModelEngine::GetInstance()->Shutdown();
chip::DeviceLayer::PlatformMgr().Shutdown();
Super::Shutdown();
}

Expand Down
7 changes: 7 additions & 0 deletions src/crypto/CHIPCryptoPAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions src/platform/Zephyr/PlatformManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 5456873

Please sign in to comment.