From ad6af37d9982e8c774b8739bef8a692552ecf286 Mon Sep 17 00:00:00 2001 From: Kamil Sroka Date: Mon, 9 Sep 2019 18:23:05 +0200 Subject: [PATCH] [crypto] don't add entropy source if it's done by mbedTLS (#4140) If platform uses MbedTLS feature to add entropy source during context init (by platform entropy, HAVEGE or MBEDTLS_ENTROPY_HARDWARE_ALT) we shouldn't add second entropy source. This saves time and power as we don't have to poll two different entropy sources/poll same entropy source twice. --- src/core/common/random_manager.cpp | 7 +++++++ src/core/common/random_manager.hpp | 7 +++++++ src/core/meshcop/dtls.hpp | 2 -- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/core/common/random_manager.cpp b/src/core/common/random_manager.cpp index c9602bded..d599224d1 100644 --- a/src/core/common/random_manager.cpp +++ b/src/core/common/random_manager.cpp @@ -149,8 +149,11 @@ uint32_t RandomManager::NonCryptoPrng::GetNext(void) void RandomManager::Entropy::Init(void) { mbedtls_entropy_init(&mEntropyContext); + +#ifndef OT_MBEDTLS_STRONG_DEFAULT_ENTROPY_PRESENT mbedtls_entropy_add_source(&mEntropyContext, &RandomManager::Entropy::HandleMbedtlsEntropyPoll, NULL, MBEDTLS_ENTROPY_MIN_HARDWARE, MBEDTLS_ENTROPY_SOURCE_STRONG); +#endif // OT_MBEDTLS_STRONG_DEFAULT_ENTROPY_PRESENT } void RandomManager::Entropy::Deinit(void) @@ -158,6 +161,8 @@ void RandomManager::Entropy::Deinit(void) mbedtls_entropy_free(&mEntropyContext); } +#ifndef OT_MBEDTLS_STRONG_DEFAULT_ENTROPY_PRESENT + int RandomManager::Entropy::HandleMbedtlsEntropyPoll(void * aData, unsigned char *aOutput, size_t aInLen, @@ -176,6 +181,8 @@ exit: return rval; } +#endif // OT_MBEDTLS_STRONG_DEFAULT_ENTROPY_PRESENT + //------------------------------------------------------------------- // CryptoCtrDrbg diff --git a/src/core/common/random_manager.hpp b/src/core/common/random_manager.hpp index c2e78a308..b586b9eaf 100644 --- a/src/core/common/random_manager.hpp +++ b/src/core/common/random_manager.hpp @@ -45,6 +45,11 @@ #include "utils/wrap_stdint.h" +#if (!defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \ + (!defined(MBEDTLS_NO_PLATFORM_ENTROPY) || defined(MBEDTLS_HAVEGE_C) || defined(MBEDTLS_ENTROPY_HARDWARE_ALT))) +#define OT_MBEDTLS_STRONG_DEFAULT_ENTROPY_PRESENT +#endif + namespace ot { /** @@ -123,7 +128,9 @@ private: mbedtls_entropy_context *GetContext(void) { return &mEntropyContext; } private: +#ifndef OT_MBEDTLS_STRONG_DEFAULT_ENTROPY_PRESENT static int HandleMbedtlsEntropyPoll(void *aData, unsigned char *aOutput, size_t aInLen, size_t *aOutLen); +#endif // OT_MBEDTLS_STRONG_DEFAULT_ENTROPY_PRESENT mbedtls_entropy_context mEntropyContext; }; diff --git a/src/core/meshcop/dtls.hpp b/src/core/meshcop/dtls.hpp index f133eb038..764a3f28b 100644 --- a/src/core/meshcop/dtls.hpp +++ b/src/core/meshcop/dtls.hpp @@ -438,8 +438,6 @@ private: static void HandleUdpTransmit(Tasklet &aTasklet); void HandleUdpTransmit(void); - static int HandleMbedtlsEntropyPoll(void *aData, unsigned char *aOutput, size_t aInLen, size_t *aOutLen); - void Process(void); State mState;