From 3a3c9c90b04b6ccfb05dfc5ba9216d704a75e0ef Mon Sep 17 00:00:00 2001 From: Alex Kolosov Date: Mon, 13 Sep 2021 16:15:13 +0300 Subject: [PATCH 1/6] [Telink]: Fix entropy source failed crash on boot --- src/platform/Zephyr/PlatformManagerImpl.cpp | 21 ++++++++++++++++----- src/platform/nrfconnect/BUILD.gn | 5 ++++- src/platform/telink/BUILD.gn | 4 ++++ 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/platform/Zephyr/PlatformManagerImpl.cpp b/src/platform/Zephyr/PlatformManagerImpl.cpp index a31916b8fc20c9..c101f9718d257a 100644 --- a/src/platform/Zephyr/PlatformManagerImpl.cpp +++ b/src/platform/Zephyr/PlatformManagerImpl.cpp @@ -21,6 +21,7 @@ * for Zephyr platforms. */ +#include #include #include @@ -37,19 +38,29 @@ static K_THREAD_STACK_DEFINE(sChipThreadStack, CHIP_DEVICE_CONFIG_CHIP_TASK_STAC PlatformManagerImpl PlatformManagerImpl::sInstance{ sChipThreadStack }; +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); + + SuccessOrExit(entropy_get_entropy(entropy, output, len)); + + *olen = len; + +exit: + return 0; +} + CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) { CHIP_ERROR err; - const struct device * entropy = device_get_binding(DT_CHOSEN_ZEPHYR_ENTROPY_LABEL); - unsigned int seed; // 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(&seed), sizeof(seed))); - srand(seed); + // Add entropy source based on Zephyr entropy driver + err = chip::Crypto::add_entropy_source(app_entropy_source, NULL, 16); + SuccessOrExit(err); // Call _InitChipStack() on the generic implementation base class to finish the initialization process. err = Internal::GenericPlatformManagerImpl_Zephyr::_InitChipStack(); diff --git a/src/platform/nrfconnect/BUILD.gn b/src/platform/nrfconnect/BUILD.gn index 77232876401355..21c58be4cd0c6a 100644 --- a/src/platform/nrfconnect/BUILD.gn +++ b/src/platform/nrfconnect/BUILD.gn @@ -46,7 +46,10 @@ static_library("nrfconnect") { deps = [] - public_deps = [ "${chip_root}/src/platform:platform_base" ] + public_deps = [ + "${chip_root}/src/platform:platform_base", + "${chip_root}/src/crypto" + ] if (chip_enable_openthread) { sources += [ diff --git a/src/platform/telink/BUILD.gn b/src/platform/telink/BUILD.gn index 36dbbdaeb9e34f..3a624306cda361 100644 --- a/src/platform/telink/BUILD.gn +++ b/src/platform/telink/BUILD.gn @@ -58,4 +58,8 @@ static_library("telink") { deps += [ "${chip_root}/src/lib/mdns:platform_header" ] } } + + public_deps += [ + "${chip_root}/src/crypto" + ] } From e1aa1a7b4484b489ba7654d9f92f1fe6c2eb110b Mon Sep 17 00:00:00 2001 From: Alex Kolosov Date: Mon, 13 Sep 2021 16:52:37 +0300 Subject: [PATCH 2/6] [Telink]: Fix restyled check fail --- src/platform/nrfconnect/BUILD.gn | 4 ++-- src/platform/telink/BUILD.gn | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/platform/nrfconnect/BUILD.gn b/src/platform/nrfconnect/BUILD.gn index 21c58be4cd0c6a..67055781cee2d8 100644 --- a/src/platform/nrfconnect/BUILD.gn +++ b/src/platform/nrfconnect/BUILD.gn @@ -46,9 +46,9 @@ static_library("nrfconnect") { deps = [] - public_deps = [ + public_deps = [ + "${chip_root}/src/crypto", "${chip_root}/src/platform:platform_base", - "${chip_root}/src/crypto" ] if (chip_enable_openthread) { diff --git a/src/platform/telink/BUILD.gn b/src/platform/telink/BUILD.gn index 3a624306cda361..6ef5bf7162dd5c 100644 --- a/src/platform/telink/BUILD.gn +++ b/src/platform/telink/BUILD.gn @@ -59,7 +59,5 @@ static_library("telink") { } } - public_deps += [ - "${chip_root}/src/crypto" - ] + public_deps += [ "${chip_root}/src/crypto" ] } From 4f1bee1f8209d495b82095eb95428a85521437d3 Mon Sep 17 00:00:00 2001 From: Alex Kolosov Date: Tue, 14 Sep 2021 16:54:28 +0300 Subject: [PATCH 3/6] [Telink]: Fix review remarks --- src/platform/Zephyr/PlatformManagerImpl.cpp | 30 +++++++++++++++------ src/platform/nrfconnect/BUILD.gn | 5 +--- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/platform/Zephyr/PlatformManagerImpl.cpp b/src/platform/Zephyr/PlatformManagerImpl.cpp index c101f9718d257a..0bc316a36ebf71 100644 --- a/src/platform/Zephyr/PlatformManagerImpl.cpp +++ b/src/platform/Zephyr/PlatformManagerImpl.cpp @@ -38,29 +38,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); - - SuccessOrExit(entropy_get_entropy(entropy, output, len)); - - *olen = len; - -exit: - return 0; + 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; +#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); +#if !CONFIG_NORDIC_SECURITY_BACKEND // Add entropy source based on Zephyr entropy driver - err = chip::Crypto::add_entropy_source(app_entropy_source, NULL, 16); + 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::_InitChipStack(); diff --git a/src/platform/nrfconnect/BUILD.gn b/src/platform/nrfconnect/BUILD.gn index 67055781cee2d8..77232876401355 100644 --- a/src/platform/nrfconnect/BUILD.gn +++ b/src/platform/nrfconnect/BUILD.gn @@ -46,10 +46,7 @@ static_library("nrfconnect") { deps = [] - public_deps = [ - "${chip_root}/src/crypto", - "${chip_root}/src/platform:platform_base", - ] + public_deps = [ "${chip_root}/src/platform:platform_base" ] if (chip_enable_openthread) { sources += [ From 1025861339941fe072564edb191dc5ab42c1f2ba Mon Sep 17 00:00:00 2001 From: Alex Kolosov Date: Tue, 14 Sep 2021 18:05:03 +0300 Subject: [PATCH 4/6] [Telink]: Fix nrfconnect build fail --- src/platform/Zephyr/PlatformManagerImpl.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/platform/Zephyr/PlatformManagerImpl.cpp b/src/platform/Zephyr/PlatformManagerImpl.cpp index 0bc316a36ebf71..de345f14279467 100644 --- a/src/platform/Zephyr/PlatformManagerImpl.cpp +++ b/src/platform/Zephyr/PlatformManagerImpl.cpp @@ -21,7 +21,10 @@ * for Zephyr platforms. */ +#if !CONFIG_NORDIC_SECURITY_BACKEND #include +#endif // !CONFIG_NORDIC_SECURITY_BACKEND + #include #include From d4f1e8df4355baea7a4f15db47a229ea78508241 Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Tue, 14 Sep 2021 15:02:48 -0400 Subject: [PATCH 5/6] Update src/platform/Zephyr/PlatformManagerImpl.cpp --- src/platform/Zephyr/PlatformManagerImpl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/platform/Zephyr/PlatformManagerImpl.cpp b/src/platform/Zephyr/PlatformManagerImpl.cpp index de345f14279467..413e306071c873 100644 --- a/src/platform/Zephyr/PlatformManagerImpl.cpp +++ b/src/platform/Zephyr/PlatformManagerImpl.cpp @@ -22,7 +22,7 @@ */ #if !CONFIG_NORDIC_SECURITY_BACKEND -#include +#include // nogncheck #endif // !CONFIG_NORDIC_SECURITY_BACKEND #include From 964c3f2a090821bc726253890aab7bf6011f6e3c Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Tue, 14 Sep 2021 15:50:55 -0400 Subject: [PATCH 6/6] Update src/platform/Zephyr/PlatformManagerImpl.cpp --- src/platform/Zephyr/PlatformManagerImpl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/platform/Zephyr/PlatformManagerImpl.cpp b/src/platform/Zephyr/PlatformManagerImpl.cpp index 413e306071c873..b63fc13c1028d4 100644 --- a/src/platform/Zephyr/PlatformManagerImpl.cpp +++ b/src/platform/Zephyr/PlatformManagerImpl.cpp @@ -23,7 +23,7 @@ #if !CONFIG_NORDIC_SECURITY_BACKEND #include // nogncheck -#endif // !CONFIG_NORDIC_SECURITY_BACKEND +#endif // !CONFIG_NORDIC_SECURITY_BACKEND #include