From d2f018bff1deb78c35fcfb4cee508d4899e03dff Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 2 Jun 2022 17:05:57 +1000 Subject: [PATCH] unix,windows: Factor out code that generates random bytes to a new func. Signed-off-by: Damien George --- ports/unix/moduos.c | 29 +---------------------------- ports/unix/mphalport.h | 2 ++ ports/unix/unix_mphal.c | 19 +++++++++++++++++++ ports/windows/windows_mphal.c | 8 ++++++++ 4 files changed, 30 insertions(+), 28 deletions(-) diff --git a/ports/unix/moduos.c b/ports/unix/moduos.c index c7cd19ca38..848a3612d1 100644 --- a/ports/unix/moduos.c +++ b/ports/unix/moduos.c @@ -25,25 +25,12 @@ * THE SOFTWARE. */ -#include #include #include #include "py/runtime.h" #include "py/mphal.h" -#if defined(__GLIBC__) && defined(__GLIBC_PREREQ) -#if __GLIBC_PREREQ(2, 25) -#include -#define _HAVE_GETRANDOM -#endif -#endif - -#if _WIN32 -#include -#include -#endif - STATIC mp_obj_t mp_uos_getenv(mp_obj_t var_in) { const char *s = getenv(mp_obj_str_get_str(var_in)); if (s == NULL) { @@ -105,21 +92,7 @@ STATIC mp_obj_t mp_uos_urandom(mp_obj_t num) { mp_int_t n = mp_obj_get_int(num); vstr_t vstr; vstr_init_len(&vstr, n); - #if _WIN32 - NTSTATUS result = BCryptGenRandom(NULL, (unsigned char *)vstr.buf, n, BCRYPT_USE_SYSTEM_PREFERRED_RNG); - if (!BCRYPT_SUCCESS(result)) { - mp_raise_OSError(errno); - } - #else - #ifdef _HAVE_GETRANDOM - RAISE_ERRNO(getrandom(vstr.buf, n, 0), errno); - #else - int fd = open("/dev/urandom", O_RDONLY); - RAISE_ERRNO(fd, errno); - RAISE_ERRNO(read(fd, vstr.buf, n), errno); - close(fd); - #endif - #endif + mp_hal_get_random(n, vstr.buf); return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_uos_urandom_obj, mp_uos_urandom); diff --git a/ports/unix/mphalport.h b/ports/unix/mphalport.h index 510f9d939d..724fc8af5f 100644 --- a/ports/unix/mphalport.h +++ b/ports/unix/mphalport.h @@ -91,6 +91,8 @@ static inline void mp_hal_delay_us(mp_uint_t us) { { if (err_flag == -1) \ { mp_raise_OSError(error_val); } } +void mp_hal_get_random(size_t n, void *buf); + #if MICROPY_PY_BLUETOOTH enum { MP_HAL_MAC_BDADDR, diff --git a/ports/unix/unix_mphal.c b/ports/unix/unix_mphal.c index 1d3c52bf16..80a7d1c8f6 100644 --- a/ports/unix/unix_mphal.c +++ b/ports/unix/unix_mphal.c @@ -29,12 +29,20 @@ #include #include #include +#include #include "py/mphal.h" #include "py/mpthread.h" #include "py/runtime.h" #include "extmod/misc.h" +#if defined(__GLIBC__) && defined(__GLIBC_PREREQ) +#if __GLIBC_PREREQ(2, 25) +#include +#define _HAVE_GETRANDOM +#endif +#endif + #ifndef _WIN32 #include @@ -234,3 +242,14 @@ void mp_hal_delay_ms(mp_uint_t ms) { usleep(ms * 1000); #endif } + +void mp_hal_get_random(size_t n, void *buf) { + #ifdef _HAVE_GETRANDOM + RAISE_ERRNO(getrandom(buf, n, 0), errno); + #else + int fd = open("/dev/urandom", O_RDONLY); + RAISE_ERRNO(fd, errno); + RAISE_ERRNO(read(fd, buf, n), errno); + close(fd); + #endif +} diff --git a/ports/windows/windows_mphal.c b/ports/windows/windows_mphal.c index 17b20c3031..d1bbb931af 100644 --- a/ports/windows/windows_mphal.c +++ b/ports/windows/windows_mphal.c @@ -32,6 +32,7 @@ #include #include #include +#include HANDLE std_in = NULL; HANDLE con_out = NULL; @@ -286,3 +287,10 @@ void mp_hal_delay_ms(mp_uint_t ms) { msec_sleep((double)ms); #endif } + +void mp_hal_get_random(size_t n, void *buf) { + NTSTATUS result = BCryptGenRandom(NULL, (unsigned char *)buf, n, BCRYPT_USE_SYSTEM_PREFERRED_RNG); + if (!BCRYPT_SUCCESS(result)) { + mp_raise_OSError(errno); + } +}