diff options
author | Damien George <damien.p.george@gmail.com> | 2018-05-22 10:27:21 +1000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2018-05-22 10:36:03 +1000 |
commit | f68e722005c495134065680859c705210d307ccf (patch) | |
tree | 36d03ef13b148ef2b5abddded4351ec43db25e54 | |
parent | cda964198a36e8d1ce4497d90484fae4b9d661a5 (diff) |
stm32/rng: Use Yasmarang for rng_get() if MCU doesn't have HW RNG.
-rw-r--r-- | ports/stm32/rng.c | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/ports/stm32/rng.c b/ports/stm32/rng.c index e70eafae7..b23941998 100644 --- a/ports/stm32/rng.c +++ b/ports/stm32/rng.c @@ -60,4 +60,30 @@ STATIC mp_obj_t pyb_rng_get(void) { } MP_DEFINE_CONST_FUN_OBJ_0(pyb_rng_get_obj, pyb_rng_get); +#else // MICROPY_HW_ENABLE_RNG + +// For MCUs that don't have an RNG we still need to provide a rng_get() function, +// eg for lwIP. A pseudo-RNG is not really ideal but we go with it for now. We +// don't want to use urandom's pRNG because then the user won't see a reproducible +// random stream. + +// Yasmarang random number generator by Ilya Levin +// http://www.literatecode.com/yasmarang +STATIC uint32_t pyb_rng_yasmarang(void) { + static uint32_t pad = 0xeda4baba, n = 69, d = 233; + static uint8_t dat = 0; + + pad += dat + d * n; + pad = (pad << 3) + (pad >> 29); + n = pad | 2; + d ^= (pad << 31) + (pad >> 1); + dat ^= (char)pad ^ (d >> 8) ^ 1; + + return pad ^ (d << 5) ^ (pad >> 18) ^ (dat << 1); +} + +uint32_t rng_get(void) { + return pyb_rng_yasmarang(); +} + #endif // MICROPY_HW_ENABLE_RNG |