Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Telink]: Fix entropy source failed crash on boot #9635

Merged
merged 6 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
bzbarsky-apple marked this conversation as resolved.
Show resolved Hide resolved
#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" ]
}