Skip to content

Commit

Permalink
[Telink]: Fix entropy source failed crash on boot (#9635)
Browse files Browse the repository at this point in the history
* [Telink]: Fix entropy source failed crash on boot

* [Telink]: Fix restyled check fail

* [Telink]: Fix review remarks

* [Telink]: Fix nrfconnect build fail

* Update src/platform/Zephyr/PlatformManagerImpl.cpp

* Update src/platform/Zephyr/PlatformManagerImpl.cpp

Co-authored-by: Boris Zbarsky <[email protected]>
  • Loading branch information
rikorsev and bzbarsky-apple authored Sep 14, 2021
1 parent eef1141 commit 181ab66
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
38 changes: 33 additions & 5 deletions src/platform/Zephyr/PlatformManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
* for Zephyr platforms.
*/

#if !CONFIG_NORDIC_SECURITY_BACKEND
#include <crypto/CHIPCryptoPAL.h> // nogncheck
#endif // !CONFIG_NORDIC_SECURITY_BACKEND

#include <platform/internal/CHIPDeviceLayerInternal.h>

#include <platform/PlatformManager.h>
Expand All @@ -37,19 +41,43 @@ static K_THREAD_STACK_DEFINE(sChipThreadStack, CHIP_DEVICE_CONFIG_CHIP_TASK_STAC

PlatformManagerImpl PlatformManagerImpl::sInstance{ sChipThreadStack };

#if !CONFIG_NORDIC_SECURITY_BACKEND
static int app_entropy_source(void * data, unsigned char * output, size_t len, size_t * olen)
{
const struct device * entropy = device_get_binding(DT_CHOSEN_ZEPHYR_ENTROPY_LABEL);
int ret = entropy_get_entropy(entropy, output, len);

if (ret == 0)
{
*olen = len;
}
else
{
*olen = 0;
}

return ret;
}
#endif // !CONFIG_NORDIC_SECURITY_BACKEND

CHIP_ERROR PlatformManagerImpl::_InitChipStack(void)
{
CHIP_ERROR err;
const struct device * entropy = device_get_binding(DT_CHOSEN_ZEPHYR_ENTROPY_LABEL);
unsigned int seed;

#if !CONFIG_NORDIC_SECURITY_BACKEND
// Minimum required from source before entropy is released ( with mbedtls_entropy_func() ) (in bytes)
const size_t kThreshold = 16;
#endif // !CONFIG_NORDIC_SECURITY_BACKEND

// Initialize the configuration system.
err = Internal::ZephyrConfig::Init();
SuccessOrExit(err);

// Get entropy to initialize seed for pseudorandom operations.
SuccessOrExit(entropy_get_entropy(entropy, reinterpret_cast<uint8_t *>(&seed), sizeof(seed)));
srand(seed);
#if !CONFIG_NORDIC_SECURITY_BACKEND
// Add entropy source based on Zephyr entropy driver
err = chip::Crypto::add_entropy_source(app_entropy_source, NULL, kThreshold);
SuccessOrExit(err);
#endif // !CONFIG_NORDIC_SECURITY_BACKEND

// Call _InitChipStack() on the generic implementation base class to finish the initialization process.
err = Internal::GenericPlatformManagerImpl_Zephyr<PlatformManagerImpl>::_InitChipStack();
Expand Down
2 changes: 2 additions & 0 deletions src/platform/telink/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,6 @@ static_library("telink") {
deps += [ "${chip_root}/src/lib/mdns:platform_header" ]
}
}

public_deps += [ "${chip_root}/src/crypto" ]
}

0 comments on commit 181ab66

Please sign in to comment.