From 63e9806d855718d848c1dde953048e4eaba20e8b Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Wed, 4 Jan 2017 16:30:07 +1100 Subject: [PATCH] esp_random: XOR the RNG register value several times before returning it Probably unnecessary, but avoids returning internal RNG state as-is. --- components/esp32/hw_random.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/components/esp32/hw_random.c b/components/esp32/hw_random.c index 11c7af936b..3e5cb81a0a 100644 --- a/components/esp32/hw_random.c +++ b/components/esp32/hw_random.c @@ -29,13 +29,19 @@ uint32_t IRAM_ATTR esp_random(void) * this function needs to wait for at least 16 APB clock cycles after reading * previous word. This implementation may actually wait a bit longer * due to extra time spent in arithmetic and branch statements. + * + * As a (probably unncessary) precaution to avoid returning the + * RNG state as-is, the result is XORed with additional + * WDEV_RND_REG reads while waiting. */ static uint32_t last_ccount = 0; uint32_t ccount; + uint32_t result = 0; do { ccount = XTHAL_GET_CCOUNT(); + result ^= REG_READ(WDEV_RND_REG); } while (ccount - last_ccount < XT_CLOCK_FREQ / APB_CLK_FREQ * 16); last_ccount = ccount; - return REG_READ(WDEV_RND_REG); + return result ^ REG_READ(WDEV_RND_REG); }