Commit 3e455e97 authored by Damien George's avatar Damien George

stm32/rng: Use SysTick+RTC+unique-id to seed pRNG for MCUs without RNG.

The same seed will only occur if the board is the same, the RTC has the
same time (eg freshly powered up) and the first call to this function (eg
via an "import random") is done at exactly the same time since reset.
Signed-off-by: default avatarDamien George <damien@micropython.org>
parent 59019d7f
......@@ -24,6 +24,7 @@
* THE SOFTWARE.
*/
#include "rtc.h"
#include "rng.h"
#if MICROPY_HW_ENABLE_RNG
......@@ -63,16 +64,26 @@ 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
// eg for lwIP and random.seed(). A pseudo-RNG is not really ideal but we go with
// it for now, seeding with numbers which will be somewhat different each time. 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 bool seeded = false;
static uint32_t pad = 0, n = 0, d = 0;
static uint8_t dat = 0;
if (!seeded) {
seeded = true;
rtc_init_finalise();
pad = *(uint32_t *)MP_HAL_UNIQUE_ID_ADDRESS ^ SysTick->VAL;
n = RTC->TR;
d = RTC->SSR;
}
pad += dat + d * n;
pad = (pad << 3) + (pad >> 29);
n = pad | 2;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment