mimxrt/moduos: Seed the PRNG on boot using the TRNG.

This commit is contained in:
robert-hh 2021-06-06 08:11:20 +02:00 committed by Damien George
parent f45412793e
commit a262faa227
2 changed files with 23 additions and 7 deletions

View File

@ -61,21 +61,34 @@ STATIC mp_obj_t os_uname(void) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(os_uname_obj, os_uname);
STATIC mp_obj_t os_urandom(mp_obj_t num) {
mp_int_t n = mp_obj_get_int(num);
static bool initialized = false;
vstr_t vstr;
vstr_init_len(&vstr, n);
static bool initialized = false;
STATIC void trng_start(void) {
trng_config_t trngConfig;
if (!initialized) {
trng_config_t trngConfig;
TRNG_GetDefaultConfig(&trngConfig);
trngConfig.sampleMode = kTRNG_SampleModeVonNeumann;
TRNG_Init(TRNG, &trngConfig);
initialized = true;
}
}
uint32_t trng_random_u32(void) {
uint32_t rngval;
trng_start();
TRNG_GetRandomData(TRNG, (uint8_t *)&rngval, 4);
return rngval;
}
STATIC mp_obj_t os_urandom(mp_obj_t num) {
mp_int_t n = mp_obj_get_int(num);
vstr_t vstr;
vstr_init_len(&vstr, n);
trng_start();
TRNG_GetRandomData(TRNG, vstr.buf, n);
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);

View File

@ -30,6 +30,8 @@
#include "mpconfigboard.h"
#include "fsl_common.h"
uint32_t trng_random_u32(void);
// Memory allocation policies
#define MICROPY_GC_STACK_ENTRY_TYPE uint16_t
#define MICROPY_GC_ALLOC_THRESHOLD (0)
@ -118,6 +120,7 @@
#define MICROPY_PY_UTIME_MP_HAL (1)
#define MICROPY_PY_URANDOM (1)
#define MICROPY_PY_URANDOM_EXTRA_FUNCS (1)
#define MICROPY_PY_URANDOM_SEED_INIT_FUNC (trng_random_u32())
#define MICROPY_PY_USELECT (1)
#define MICROPY_PY_MACHINE (1)
#define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new