From 5866852a901d420523f94c505c904826c0286f64 Mon Sep 17 00:00:00 2001 From: Guillaume Revaillot Date: Tue, 5 Feb 2019 17:17:34 +0100 Subject: [PATCH] rng: handle noise source / seed error. If noise source error occurs, flag must be cleared and data register must be discarded (at least 12 reads to flush pipeline on G0). Other device mention start/restart of chip, so, do both (better safe than sorry). Reviewed-on: https://github.com/libopencm3/libopencm3/pull/1062 --- lib/stm32/common/rng_common_v1.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/stm32/common/rng_common_v1.c b/lib/stm32/common/rng_common_v1.c index bffb0c27..e35fc46a 100644 --- a/lib/stm32/common/rng_common_v1.c +++ b/lib/stm32/common/rng_common_v1.c @@ -94,10 +94,16 @@ uint32_t rng_get_random_blocking(void) uint32_t rv; bool done; do { - if (RNG_SR & RNG_SR_SECS) { - rng_disable(); - rng_enable(); + + if (RNG_SR & RNG_SR_SEIS) { + RNG_SR = RNG_SR & ~RNG_SR_SEIS; + for (int i = 12; i != 0; i--) { + rv = RNG_DR; + } + RNG_CR &= ~RNG_CR_RNGEN; + RNG_CR |= RNG_CR_RNGEN; } + done = rng_get_random(&rv); } while (!done);