From a6e8848a5eb9f037693afe11583857d6a8ff96bc Mon Sep 17 00:00:00 2001 From: Mason Tran Date: Fri, 30 Oct 2020 14:56:59 -0400 Subject: [PATCH] [efr32] call mbedtls_hardware_poll directly in otPlatEntropyGet() (#5744) --- examples/platforms/efr32/src/entropy.c | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/examples/platforms/efr32/src/entropy.c b/examples/platforms/efr32/src/entropy.c index 64b19aadc..3a755e156 100644 --- a/examples/platforms/efr32/src/entropy.c +++ b/examples/platforms/efr32/src/entropy.c @@ -33,29 +33,25 @@ */ #include - #include "mbedtls/entropy_poll.h" #include "utils/code_utils.h" -static uint32_t randomUint32Get(void) -{ - uint32_t random = 0; - size_t ouputLen = 0; - - mbedtls_hardware_poll(NULL, (unsigned char *)&random, sizeof(random), &ouputLen); - - return random; -} - otError otPlatEntropyGet(uint8_t *aOutput, uint16_t aOutputLength) { - otError error = OT_ERROR_NONE; + otError error = OT_ERROR_NONE; + size_t outputLen = 0; otEXPECT_ACTION(aOutput, error = OT_ERROR_INVALID_ARGS); - for (uint16_t length = 0; length < aOutputLength; length++) + for (size_t partialLen = 0; outputLen < aOutputLength; outputLen += partialLen) { - aOutput[length] = (uint8_t)randomUint32Get(); + const uint16_t remaining = aOutputLength - outputLen; + partialLen = 0; + + // Non-zero return values for mbedtls_hardware_poll() signify an error has occurred + otEXPECT_ACTION(0 == mbedtls_hardware_poll(NULL, &aOutput[outputLen], remaining, &partialLen), + error = OT_ERROR_FAILED); + otEXPECT_ACTION(partialLen > 0, error = OT_ERROR_FAILED); } exit: