aboutsummaryrefslogtreecommitdiff
path: root/esp8266
diff options
context:
space:
mode:
authorScott Shawcroft <scott.shawcroft@gmail.com>2017-06-27 17:37:24 -0700
committerScott Shawcroft <scott.shawcroft@gmail.com>2017-06-27 17:37:24 -0700
commita0058e67124b93eb1c8c9c610f823396a7ff7e68 (patch)
tree07f6e65722d34c6f29379b9ffe62ea3798471b61 /esp8266
parent265d5dab05f435ab502303e68b6b61adfd03c870 (diff)
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.
Diffstat (limited to 'esp8266')
-rw-r--r--esp8266/Makefile1
-rw-r--r--esp8266/common-hal/os/__init__.c18
-rw-r--r--esp8266/mpconfigport.h5
3 files changed, 22 insertions, 2 deletions
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 <string.h>
+#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 \