From a0058e67124b93eb1c8c9c610f823396a7ff7e68 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Tue, 27 Jun 2017 17:37:24 -0700 Subject: Introduce a random module that is a subset of CPython's random. It also initializes in the same way where it takes from a true random source when available through os.urandom(). After initializing, it produces deterministic results until the seed is set. This replaces urandom! Fixes #139. --- esp8266/Makefile | 1 + esp8266/common-hal/os/__init__.c | 18 ++++++++++++++++++ esp8266/mpconfigport.h | 5 +++-- 3 files changed, 22 insertions(+), 2 deletions(-) (limited to 'esp8266') diff --git a/esp8266/Makefile b/esp8266/Makefile index 54c826351..168e74383 100644 --- a/esp8266/Makefile +++ b/esp8266/Makefile @@ -139,6 +139,7 @@ SRC_SHARED_MODULE = \ busio/I2C.c \ busio/OneWire.c \ os/__init__.c \ + random/__init__.c \ storage/__init__.c \ SRC_SHARED_MODULE_EXPANDED = $(addprefix shared-bindings/, $(SRC_SHARED_MODULE)) \ diff --git a/esp8266/common-hal/os/__init__.c b/esp8266/common-hal/os/__init__.c index df241ecae..4f82a79e0 100644 --- a/esp8266/common-hal/os/__init__.c +++ b/esp8266/common-hal/os/__init__.c @@ -27,6 +27,7 @@ #include +#include "etshal.h" #include "py/objtuple.h" #include "py/objstr.h" #include "genhdr/mpversion.h" @@ -60,3 +61,20 @@ mp_obj_t common_hal_os_uname(void) { os_uname_info_obj.items[2] = mp_obj_new_str(ver, strlen(ver), false); return (mp_obj_t)&os_uname_info_obj; } + +static uint32_t last_random; +bool common_hal_os_urandom(uint8_t* buffer, uint32_t length) { + uint32_t i = 0; + while (i < length) { + uint32_t new_random = last_random; + while (new_random == last_random) { + new_random = *WDEV_HWRNG; + } + for (int j = 0; j < 4 && i < length; j++) { + buffer[i] = new_random & 0xff; + i++; + new_random >>= 8; + } + } + return true; +} diff --git a/esp8266/mpconfigport.h b/esp8266/mpconfigport.h index 066788832..faff05bba 100644 --- a/esp8266/mpconfigport.h +++ b/esp8266/mpconfigport.h @@ -68,7 +68,7 @@ #define MICROPY_PY_UHEAPQ (1) #define MICROPY_PY_UTIMEQ (1) #define MICROPY_PY_UJSON (1) -#define MICROPY_PY_URANDOM (1) +#define MICROPY_PY_URANDOM (0) #define MICROPY_PY_URE (1) #define MICROPY_PY_USELECT (1) #define MICROPY_PY_UTIME_MP_HAL (1) @@ -155,8 +155,8 @@ void *esp_native_code_commit(void*, size_t); // extra built in modules to add to the list of known ones extern const struct _mp_obj_module_t esp_module; extern const struct _mp_obj_module_t network_module; -extern const struct _mp_obj_module_t utime_module; extern const struct _mp_obj_module_t os_module; +extern const struct _mp_obj_module_t random_module; extern const struct _mp_obj_module_t storage_module; extern const struct _mp_obj_module_t mp_module_lwip; extern const struct _mp_obj_module_t mp_module_machine; @@ -186,6 +186,7 @@ extern const struct _mp_obj_module_t time_module; { MP_OBJ_NEW_QSTR(MP_QSTR_busio), (mp_obj_t)&busio_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_bitbangio), (mp_obj_t)&bitbangio_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_storage), (mp_obj_t)&storage_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_random), (mp_obj_t)&random_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_module }, \ #define MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS \ -- cgit v1.2.3