[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.
This commit is contained in:
Kamil Sroka
2019-09-09 09:23:05 -07:00
committed by Jonathan Hui
parent a154692422
commit ad6af37d99
3 changed files with 14 additions and 2 deletions
+7
View File
@@ -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
+7
View File
@@ -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;
};
-2
View File
@@ -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;