diff --git a/extmod/modos.c b/extmod/modos.c index 69fdc3fac0..b1dd267f88 100644 --- a/extmod/modos.c +++ b/extmod/modos.c @@ -145,6 +145,18 @@ static mp_obj_t mp_os_dupterm_notify(mp_obj_t obj_in) { static MP_DEFINE_CONST_FUN_OBJ_1(mp_os_dupterm_notify_obj, mp_os_dupterm_notify); #endif +#if MICROPY_PY_OS_URANDOM +// This wraps the port-specific mp_hal_get_random(), which is usually defined in mphalport.c. +static mp_obj_t mp_os_urandom(mp_obj_t num) { + mp_int_t n = mp_obj_get_int(num); + vstr_t vstr; + vstr_init_len(&vstr, n); + mp_hal_get_random(n, (uint8_t *)vstr.buf); + return mp_obj_new_bytes_from_vstr(&vstr); +} +static MP_DEFINE_CONST_FUN_OBJ_1(mp_os_urandom_obj, mp_os_urandom); +#endif + static const mp_rom_map_elem_t os_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_os) }, diff --git a/ports/alif/modos.c b/ports/alif/modos.c deleted file mode 100644 index 9c8c84268c..0000000000 --- a/ports/alif/modos.c +++ /dev/null @@ -1,49 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2024 OpenMV LLC. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include "py/runtime.h" -#include "se_services.h" - -#if MICROPY_PY_OS_URANDOM -static mp_obj_t mp_os_urandom(mp_obj_t num) { - mp_int_t n = mp_obj_get_int(num); - vstr_t vstr; - vstr_init_len(&vstr, n); - uint64_t rnd = 0; - size_t rnd_bits = 0; - for (int i = 0; i < n; i++) { - if (rnd_bits == 0) { - rnd = se_services_rand64(); - rnd_bits = 64; - } - vstr.buf[i] = rnd; - rnd >>= 8; - rnd_bits -= 8; - } - return mp_obj_new_bytes_from_vstr(&vstr); -} -static MP_DEFINE_CONST_FUN_OBJ_1(mp_os_urandom_obj, mp_os_urandom); -#endif diff --git a/ports/alif/mpconfigport.h b/ports/alif/mpconfigport.h index 34214a3f7d..1561afd21f 100644 --- a/ports/alif/mpconfigport.h +++ b/ports/alif/mpconfigport.h @@ -113,7 +113,6 @@ // Extended modules #define MICROPY_EPOCH_IS_1970 (1) -#define MICROPY_PY_OS_INCLUDEFILE "ports/alif/modos.c" #define MICROPY_PY_OS_DUPTERM (1) #define MICROPY_PY_OS_SEP (1) #define MICROPY_PY_OS_SYNC (1) diff --git a/ports/alif/mphalport.c b/ports/alif/mphalport.c index 3bf8bb0aed..04e21aa257 100644 --- a/ports/alif/mphalport.c +++ b/ports/alif/mphalport.c @@ -269,3 +269,17 @@ void mp_hal_get_mac_ascii(int idx, size_t chr_off, size_t chr_len, char *dest) { *dest++ = hexchr[mac[chr_off >> 1] >> (4 * (1 - (chr_off & 1))) & 0xf]; } } + +void mp_hal_get_random(size_t n, uint8_t *buf) { + uint64_t rnd = 0; + size_t rnd_bits = 0; + for (int i = 0; i < n; i++) { + if (rnd_bits == 0) { + rnd = se_services_rand64(); + rnd_bits = 64; + } + buf[i] = rnd; + rnd >>= 8; + rnd_bits -= 8; + } +} diff --git a/ports/alif/mphalport.h b/ports/alif/mphalport.h index 731ac14fc5..c90cbc1664 100644 --- a/ports/alif/mphalport.h +++ b/ports/alif/mphalport.h @@ -375,3 +375,5 @@ void mp_hal_get_mac(int idx, uint8_t buf[6]); void mp_hal_get_mac_ascii(int idx, size_t chr_off, size_t chr_len, char *dest); uint32_t mp_hal_time_get(uint32_t *microseconds); + +void mp_hal_get_random(size_t n, uint8_t *buf); diff --git a/ports/cc3200/hal/cc3200_hal.c b/ports/cc3200/hal/cc3200_hal.c index e149968a51..cc2cd66624 100644 --- a/ports/cc3200/hal/cc3200_hal.c +++ b/ports/cc3200/hal/cc3200_hal.c @@ -49,6 +49,7 @@ #include "telnet.h" #include "pybuart.h" #include "utils.h" +#include "random.h" #ifdef USE_FREERTOS #include "FreeRTOS.h" @@ -168,6 +169,13 @@ int mp_hal_stdin_rx_chr(void) { } } +// Fill buf with n random bytes, generated by the hardware random number generator. +void mp_hal_get_random(size_t n, uint8_t *buf) { + for (int i = 0; i < n; i++) { + buf[i] = rng_get(); + } +} + /****************************************************************************** DEFINE PRIVATE FUNCTIONS ******************************************************************************/ diff --git a/ports/cc3200/hal/cc3200_hal.h b/ports/cc3200/hal/cc3200_hal.h index d07a9a1703..72d2915848 100644 --- a/ports/cc3200/hal/cc3200_hal.h +++ b/ports/cc3200/hal/cc3200_hal.h @@ -75,3 +75,5 @@ extern void mp_hal_set_interrupt_char (int c); #define mp_hal_delay_us(usec) UtilsDelay(UTILS_DELAY_US_TO_COUNT(usec)) #define mp_hal_ticks_cpu() (SysTickPeriodGet() - SysTickValueGet()) #define mp_hal_time_ns() (0) // not implemented + +void mp_hal_get_random(size_t n, uint8_t *buf); diff --git a/ports/cc3200/mods/modos.c b/ports/cc3200/mods/modos.c deleted file mode 100644 index 8c22ad8daf..0000000000 --- a/ports/cc3200/mods/modos.c +++ /dev/null @@ -1,43 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2013-2023 Damien P. George - * Copyright (c) 2015 Daniel Campora - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -// This file is never compiled standalone, it's included directly from -// extmod/modos.c via MICROPY_PY_OS_INCLUDEFILE. - -#include "py/runtime.h" -#include "random.h" - -static mp_obj_t mp_os_urandom(mp_obj_t num) { - mp_int_t n = mp_obj_get_int(num); - vstr_t vstr; - vstr_init_len(&vstr, n); - for (int i = 0; i < n; i++) { - vstr.buf[i] = rng_get(); - } - return mp_obj_new_bytes_from_vstr(&vstr); -} -static MP_DEFINE_CONST_FUN_OBJ_1(mp_os_urandom_obj, mp_os_urandom); diff --git a/ports/cc3200/mpconfigport.h b/ports/cc3200/mpconfigport.h index 38e766121e..f2a00b8300 100644 --- a/ports/cc3200/mpconfigport.h +++ b/ports/cc3200/mpconfigport.h @@ -95,7 +95,6 @@ #define MICROPY_PY_JSON (1) #define MICROPY_PY_RE (1) #define MICROPY_PY_OS (1) -#define MICROPY_PY_OS_INCLUDEFILE "ports/cc3200/mods/modos.c" #define MICROPY_PY_OS_DUPTERM (1) #define MICROPY_PY_OS_SYNC (1) #define MICROPY_PY_OS_URANDOM (1) diff --git a/ports/esp32/modos.c b/ports/esp32/modos.c deleted file mode 100644 index d5ba611e88..0000000000 --- a/ports/esp32/modos.c +++ /dev/null @@ -1,53 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * Development of the code in this file was sponsored by Microbric Pty Ltd - * - * The MIT License (MIT) - * - * Copyright (c) 2015 Josef Gajdusek - * Copyright (c) 2016 Damien P. George - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -// This file is never compiled standalone, it's included directly from -// extmod/modos.c via MICROPY_PY_OS_INCLUDEFILE. - -#include "esp_system.h" - -#include "py/runtime.h" -#include "py/mphal.h" -#include "extmod/misc.h" - -static mp_obj_t mp_os_urandom(mp_obj_t num) { - mp_int_t n = mp_obj_get_int(num); - vstr_t vstr; - vstr_init_len(&vstr, n); - uint32_t r = 0; - for (int i = 0; i < n; i++) { - if ((i & 3) == 0) { - r = esp_random(); // returns 32-bit hardware random number - } - vstr.buf[i] = r; - r >>= 8; - } - return mp_obj_new_bytes_from_vstr(&vstr); -} -static MP_DEFINE_CONST_FUN_OBJ_1(mp_os_urandom_obj, mp_os_urandom); diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index a22b91ed3d..dd8f89c4bc 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -111,7 +111,6 @@ #endif // MICROPY_PY_BLUETOOTH #define MICROPY_PY_RANDOM_SEED_INIT_FUNC (esp_random()) -#define MICROPY_PY_OS_INCLUDEFILE "ports/esp32/modos.c" #define MICROPY_PY_OS_DUPTERM (1) #define MICROPY_PY_OS_DUPTERM_NOTIFY (1) #define MICROPY_PY_OS_SYNC (1) diff --git a/ports/esp32/mphalport.c b/ports/esp32/mphalport.c index 7156895af3..a0a3860f49 100644 --- a/ports/esp32/mphalport.c +++ b/ports/esp32/mphalport.c @@ -272,3 +272,14 @@ void mp_hal_wake_main_task_from_isr(void) { portYIELD_FROM_ISR(); } } + +void mp_hal_get_random(size_t n, uint8_t *buf) { + uint32_t r = 0; + for (int i = 0; i < n; i++) { + if ((i & 3) == 0) { + r = esp_random(); // returns 32-bit hardware random number + } + buf[i] = r; + r >>= 8; + } +} diff --git a/ports/esp32/mphalport.h b/ports/esp32/mphalport.h index a2b11520a5..daacedad69 100644 --- a/ports/esp32/mphalport.h +++ b/ports/esp32/mphalport.h @@ -155,4 +155,6 @@ static inline void mp_hal_pin_write(mp_hal_pin_obj_t pin, int v) { spi_host_device_t machine_hw_spi_get_host(mp_obj_t in); +void mp_hal_get_random(size_t n, uint8_t *buf); + #endif // INCLUDED_MPHALPORT_H diff --git a/ports/esp8266/esp_mphal.c b/ports/esp8266/esp_mphal.c index 3f03c55800..264b1c9441 100644 --- a/ports/esp8266/esp_mphal.c +++ b/ports/esp8266/esp_mphal.c @@ -176,3 +176,10 @@ int ets_esf_free_bufs(int idx) { } return cnt; } + +// Fill buf with n random bytes, generated by the hardware random number generator. +void mp_hal_get_random(size_t n, uint8_t *buf) { + for (int i = 0; i < n; i++) { + buf[i] = *WDEV_HWRNG; + } +} diff --git a/ports/esp8266/esp_mphal.h b/ports/esp8266/esp_mphal.h index 6d2844e37d..7ea00a175a 100644 --- a/ports/esp8266/esp_mphal.h +++ b/ports/esp8266/esp_mphal.h @@ -120,3 +120,5 @@ static inline int mp_hal_pin_read_output(mp_hal_pin_obj_t pin) { void *ets_get_esf_buf_ctlblk(void); int ets_esf_free_bufs(int idx); + +void mp_hal_get_random(size_t n, uint8_t *buf); diff --git a/ports/esp8266/modos.c b/ports/esp8266/modos.c index 8149c087fd..c77430bfdd 100644 --- a/ports/esp8266/modos.c +++ b/ports/esp8266/modos.c @@ -35,17 +35,6 @@ static const char *mp_os_uname_release(void) { return system_get_sdk_version(); } -static mp_obj_t mp_os_urandom(mp_obj_t num) { - mp_int_t n = mp_obj_get_int(num); - vstr_t vstr; - vstr_init_len(&vstr, n); - for (int i = 0; i < n; i++) { - vstr.buf[i] = *WDEV_HWRNG; - } - return mp_obj_new_bytes_from_vstr(&vstr); -} -static MP_DEFINE_CONST_FUN_OBJ_1(mp_os_urandom_obj, mp_os_urandom); - void mp_os_dupterm_stream_detached_attached(mp_obj_t stream_detached, mp_obj_t stream_attached) { if (mp_obj_get_type(stream_attached) == &machine_uart_type) { ++uart_attached_to_dupterm; diff --git a/ports/mimxrt/mbedtls/mbedtls_port.c b/ports/mimxrt/mbedtls/mbedtls_port.c index fa2d81001b..dbc84bacfe 100644 --- a/ports/mimxrt/mbedtls/mbedtls_port.c +++ b/ports/mimxrt/mbedtls/mbedtls_port.c @@ -25,6 +25,7 @@ */ #include "py/runtime.h" +#include "mphalport.h" #ifdef MICROPY_SSL_MBEDTLS @@ -35,13 +36,9 @@ #include "mbedtls/platform_time.h" #endif -void trng_random_data(unsigned char *output, size_t len); - int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t *olen) { - - // assumes that TRNG_Init was called during startup *olen = len; - trng_random_data(output, len); + mp_hal_get_random(len, output); return 0; } diff --git a/ports/mimxrt/modos.c b/ports/mimxrt/modos.c index 0eb32afba3..1f26b7166f 100644 --- a/ports/mimxrt/modos.c +++ b/ports/mimxrt/modos.c @@ -63,9 +63,9 @@ static void trng_start(void) { } } -void trng_random_data(unsigned char *output, size_t len) { +void mp_hal_get_random(size_t n, uint8_t *buf) { trng_start(); - CAAM_RNG_GetRandomData(CAAM, &caam_handle, caam_state_handle, output, len, + CAAM_RNG_GetRandomData(CAAM, &caam_handle, caam_state_handle, buf, n, kCAAM_RngDataAny, NULL); } @@ -83,9 +83,9 @@ static void trng_start(void) { } } -void trng_random_data(unsigned char *output, size_t len) { +void mp_hal_get_random(size_t n, uint8_t *buf) { trng_start(); - TRNG_GetRandomData(TRNG, output, len); + TRNG_GetRandomData(TRNG, buf, n); } #endif @@ -94,20 +94,6 @@ uint32_t trng_random_u32(void) { uint32_t rngval; trng_start(); - trng_random_data((uint8_t *)&rngval, 4); + mp_hal_get_random(4, (uint8_t *)&rngval); return rngval; } - -#if MICROPY_PY_OS_URANDOM -static mp_obj_t mp_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_random_data((uint8_t *)vstr.buf, n); - - return mp_obj_new_bytes_from_vstr(&vstr); -} -static MP_DEFINE_CONST_FUN_OBJ_1(mp_os_urandom_obj, mp_os_urandom); -#endif diff --git a/ports/mimxrt/mphalport.h b/ports/mimxrt/mphalport.h index a0e4575024..a3ce4cbc48 100644 --- a/ports/mimxrt/mphalport.h +++ b/ports/mimxrt/mphalport.h @@ -159,5 +159,6 @@ void mp_hal_generate_laa_mac(int idx, uint8_t buf[6]); void mp_hal_get_mac(int idx, uint8_t buf[6]); void mp_hal_get_mac_ascii(int idx, size_t chr_off, size_t chr_len, char *dest); void mp_hal_get_unique_id(uint8_t id[]); +void mp_hal_get_random(size_t n, uint8_t *buf); #endif // MICROPY_INCLUDED_MIMXRT_MPHALPORT_H diff --git a/ports/nrf/modules/os/modos.c b/ports/nrf/modules/os/modos.c index 97ed1e1bad..e2ba0ddfed 100644 --- a/ports/nrf/modules/os/modos.c +++ b/ports/nrf/modules/os/modos.c @@ -29,23 +29,8 @@ #include "py/runtime.h" #include "extmod/modmachine.h" -#include "drivers/rng.h" #include "modules/machine/uart.h" -#if MICROPY_PY_OS_URANDOM -// Return a bytes object with n random bytes, generated by the hardware random number generator. -static mp_obj_t mp_os_urandom(mp_obj_t num) { - mp_int_t n = mp_obj_get_int(num); - vstr_t vstr; - vstr_init_len(&vstr, n); - for (int i = 0; i < n; i++) { - vstr.buf[i] = (uint8_t)(rng_generate_random_word() & 0xFF); - } - return mp_obj_new_bytes_from_vstr(&vstr); -} -static MP_DEFINE_CONST_FUN_OBJ_1(mp_os_urandom_obj, mp_os_urandom); -#endif - #if MICROPY_PY_OS_DUPTERM void mp_os_dupterm_stream_detached_attached(mp_obj_t stream_detached, mp_obj_t stream_attached) { #if MICROPY_PY_MACHINE_UART diff --git a/ports/nrf/mphalport.c b/ports/nrf/mphalport.c index 3a3d3ad7a6..faeee9aae5 100644 --- a/ports/nrf/mphalport.c +++ b/ports/nrf/mphalport.c @@ -38,6 +38,7 @@ #include "nrfx_config.h" #include "drivers/bluetooth/ble_uart.h" #include "shared/tinyusb/mp_usbd_cdc.h" +#include "drivers/rng.h" #if MICROPY_PY_TIME_TICKS #include "nrfx_rtc.h" @@ -281,6 +282,12 @@ mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) { return did_write ? ret : 0; } +void mp_hal_get_random(size_t n, uint8_t *buf) { + for (int i = 0; i < n; i++) { + buf[i] = (uint8_t)(rng_generate_random_word() & 0xFF); + } +} + #if MICROPY_PY_TIME_TICKS void mp_hal_delay_us(mp_uint_t us) { diff --git a/ports/nrf/mphalport.h b/ports/nrf/mphalport.h index 12e881d7b6..60c651839e 100644 --- a/ports/nrf/mphalport.h +++ b/ports/nrf/mphalport.h @@ -80,6 +80,8 @@ const char *nrfx_error_code_lookup(uint32_t err_code); void mp_nrf_start_lfclk(void); +void mp_hal_get_random(size_t n, uint8_t *buf); + #define MP_HAL_PIN_FMT "%q" #define mp_hal_pin_obj_t const pin_obj_t * #define mp_hal_get_pin_obj(o) pin_find(o) diff --git a/ports/qemu/mphalport.c b/ports/qemu/mphalport.c index a4fa32e297..36175815ec 100644 --- a/ports/qemu/mphalport.c +++ b/ports/qemu/mphalport.c @@ -101,3 +101,12 @@ void mp_hal_delay_us(mp_uint_t us) { mp_uint_t mp_hal_ticks_cpu(void) { return 0; } + +// Provide a dummy version of mp_hal_get_random() using a LCG +static uint32_t random_state; +void mp_hal_get_random(size_t n, uint8_t *buf) { + for (size_t i = 0; i < n; ++i) { + random_state = random_state * 1664525 + 1013904223; + buf[i] = random_state >> 24; + } +} diff --git a/ports/qemu/mphalport.h b/ports/qemu/mphalport.h index 9355107495..49f14fc884 100644 --- a/ports/qemu/mphalport.h +++ b/ports/qemu/mphalport.h @@ -26,3 +26,5 @@ #include #include "shared/runtime/interrupt_char.h" + +void mp_hal_get_random(size_t n, uint8_t *buf); diff --git a/ports/renesas-ra/modos.c b/ports/renesas-ra/modos.c index 2051e9420f..f8ee9cd8ee 100644 --- a/ports/renesas-ra/modos.c +++ b/ports/renesas-ra/modos.c @@ -28,7 +28,6 @@ #include "py/runtime.h" #include "extmod/modmachine.h" #include "uart.h" -#include "rng.h" bool mp_os_dupterm_is_builtin_stream(mp_const_obj_t stream) { const mp_obj_type_t *type = mp_obj_get_type(stream); @@ -44,16 +43,3 @@ void mp_os_dupterm_stream_detached_attached(mp_obj_t stream_detached, mp_obj_t s uart_attach_to_repl(MP_OBJ_TO_PTR(stream_attached), true); } } - -#if MICROPY_PY_OS_URANDOM -static mp_obj_t mp_os_urandom(mp_obj_t num) { - mp_int_t n = mp_obj_get_int(num); - vstr_t vstr; - vstr_init_len(&vstr, n); - for (int i = 0; i < n; i++) { - vstr.buf[i] = rng_read(); - } - return mp_obj_new_bytes_from_vstr(&vstr); -} -static MP_DEFINE_CONST_FUN_OBJ_1(mp_os_urandom_obj, mp_os_urandom); -#endif diff --git a/ports/renesas-ra/mphalport.c b/ports/renesas-ra/mphalport.c index 455b28af19..35d71f04b9 100644 --- a/ports/renesas-ra/mphalport.c +++ b/ports/renesas-ra/mphalport.c @@ -37,6 +37,7 @@ #include "shared/tinyusb/mp_usbd_cdc.h" #include "tusb.h" #include "uart.h" +#include "rng.h" #if MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE void flash_cache_commit(void); @@ -173,6 +174,14 @@ void mp_hal_get_mac_ascii(int idx, size_t chr_off, size_t chr_len, char *dest) { } } +#if MICROPY_HW_ENABLE_RNG +void mp_hal_get_random(size_t n, uint8_t *buf) { + for (int i = 0; i < n; i++) { + buf[i] = rng_read(); + } +} +#endif + #if MICROPY_HW_ENABLE_USBDEV void usbfs_interrupt_handler(void) { IRQn_Type irq = R_FSP_CurrentIrqGet(); diff --git a/ports/renesas-ra/mphalport.h b/ports/renesas-ra/mphalport.h index 4f8be705d1..b014f50fa6 100644 --- a/ports/renesas-ra/mphalport.h +++ b/ports/renesas-ra/mphalport.h @@ -131,3 +131,5 @@ enum { void mp_hal_get_mac(int idx, uint8_t buf[6]); void mp_hal_set_interrupt_char(int c); // -1 to disable + +void mp_hal_get_random(size_t n, uint8_t *buf); diff --git a/ports/rp2/CMakeLists.txt b/ports/rp2/CMakeLists.txt index 8b37bce681..2be74151d8 100644 --- a/ports/rp2/CMakeLists.txt +++ b/ports/rp2/CMakeLists.txt @@ -194,7 +194,6 @@ set(MICROPY_SOURCE_QSTR ${MICROPY_PORT_DIR}/machine_uart.c ${MICROPY_PORT_DIR}/machine_wdt.c ${MICROPY_PORT_DIR}/modrp2.c - ${MICROPY_PORT_DIR}/modos.c ${MICROPY_PORT_DIR}/rp2_flash.c ${MICROPY_PORT_DIR}/rp2_pio.c ${MICROPY_PORT_DIR}/rp2_dma.c diff --git a/ports/rp2/modos.c b/ports/rp2/modos.c deleted file mode 100644 index d4ab2f0819..0000000000 --- a/ports/rp2/modos.c +++ /dev/null @@ -1,40 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2016 Damien P. George - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include "py/runtime.h" -#include "pico/rand.h" - -static mp_obj_t mp_os_urandom(mp_obj_t num) { - mp_int_t n = mp_obj_get_int(num); - vstr_t vstr; - vstr_init_len(&vstr, n); - for (int i = 0; i < n; i += 8) { - uint64_t rand64 = get_rand_64(); - memcpy(vstr.buf + i, &rand64, MIN(n - i, 8)); - } - return mp_obj_new_bytes_from_vstr(&vstr); -} -static MP_DEFINE_CONST_FUN_OBJ_1(mp_os_urandom_obj, mp_os_urandom); diff --git a/ports/rp2/mpconfigport.h b/ports/rp2/mpconfigport.h index fb2d599b91..3c73aa446f 100644 --- a/ports/rp2/mpconfigport.h +++ b/ports/rp2/mpconfigport.h @@ -142,7 +142,6 @@ // Extended modules #define MICROPY_EPOCH_IS_1970 (1) -#define MICROPY_PY_OS_INCLUDEFILE "ports/rp2/modos.c" #ifndef MICROPY_PY_OS_DUPTERM #define MICROPY_PY_OS_DUPTERM (1) #define MICROPY_PY_OS_DUPTERM_NOTIFY (1) diff --git a/ports/rp2/mphalport.c b/ports/rp2/mphalport.c index 4dd510dea4..d46360cb95 100644 --- a/ports/rp2/mphalport.c +++ b/ports/rp2/mphalport.c @@ -39,6 +39,7 @@ #include "hardware/irq.h" #include "pico/unique_id.h" #include "pico/aon_timer.h" +#include "pico/rand.h" #if MICROPY_PY_NETWORK_CYW43 #include "lib/cyw43-driver/src/cyw43.h" @@ -277,3 +278,10 @@ int mp_hal_is_pin_reserved(int n) { return false; #endif } + +void mp_hal_get_random(size_t n, uint8_t *buf) { + for (int i = 0; i < n; i += 8) { + uint64_t rand64 = get_rand_64(); + memcpy(buf + i, &rand64, MIN(n - i, 8)); + } +} diff --git a/ports/rp2/mphalport.h b/ports/rp2/mphalport.h index 5012b682b9..4afc74e6c9 100644 --- a/ports/rp2/mphalport.h +++ b/ports/rp2/mphalport.h @@ -222,5 +222,6 @@ void mp_hal_get_mac(int idx, uint8_t buf[6]); void mp_hal_get_mac_ascii(int idx, size_t chr_off, size_t chr_len, char *dest); void mp_hal_generate_laa_mac(int idx, uint8_t buf[6]); int mp_hal_is_pin_reserved(int n); +void mp_hal_get_random(size_t n, uint8_t *buf); #endif // MICROPY_INCLUDED_RP2_MPHALPORT_H diff --git a/ports/samd/modos.c b/ports/samd/modos.c index 64d40a46cc..dc43359916 100644 --- a/ports/samd/modos.c +++ b/ports/samd/modos.c @@ -72,26 +72,17 @@ uint32_t trng_random_u32(int delay) { #define TRNG_RANDOM_U32 trng_random_u32(10) #endif -#if MICROPY_PY_OS_URANDOM -static mp_obj_t mp_os_urandom(mp_obj_t num) { - mp_int_t n = mp_obj_get_int(num); - vstr_t vstr; - vstr_init_len(&vstr, n); +void mp_hal_get_random(size_t n, uint8_t *buf) { uint32_t rngval = 0; - trng_start(); for (int i = 0; i < n; i++) { if ((i % 4) == 0) { rngval = TRNG_RANDOM_U32; } - vstr.buf[i] = rngval & 0xff; + buf[i] = rngval & 0xff; rngval >>= 8; } - return mp_obj_new_bytes_from_vstr(&vstr); } -static MP_DEFINE_CONST_FUN_OBJ_1(mp_os_urandom_obj, mp_os_urandom); - -#endif // MICROPY_PY_OS_URANDOM #if MICROPY_PY_OS_DUPTERM_BUILTIN_STREAM bool mp_os_dupterm_is_builtin_stream(mp_const_obj_t stream) { diff --git a/ports/samd/mphalport.h b/ports/samd/mphalport.h index 69e2b60640..ed07953016 100644 --- a/ports/samd/mphalport.h +++ b/ports/samd/mphalport.h @@ -171,4 +171,6 @@ static inline void mp_hal_pin_od_high(mp_hal_pin_obj_t pin) { gpio_set_pin_pull_mode(pin, GPIO_PULL_UP); } +void mp_hal_get_random(size_t n, uint8_t *buf); + #endif // MICROPY_INCLUDED_SAMD_MPHALPORT_H diff --git a/ports/stm32/modos.c b/ports/stm32/modos.c index cd918fe715..60afece9bc 100644 --- a/ports/stm32/modos.c +++ b/ports/stm32/modos.c @@ -26,26 +26,9 @@ #include "py/runtime.h" #include "extmod/modmachine.h" -#include "rng.h" #include "usb.h" #include "uart.h" -#if MICROPY_PY_OS_URANDOM -// urandom(n) -// Return a bytes object with n random bytes, generated by the hardware -// random number generator. -static mp_obj_t mp_os_urandom(mp_obj_t num) { - mp_int_t n = mp_obj_get_int(num); - vstr_t vstr; - vstr_init_len(&vstr, n); - for (int i = 0; i < n; i++) { - vstr.buf[i] = rng_get(); - } - return mp_obj_new_bytes_from_vstr(&vstr); -} -static MP_DEFINE_CONST_FUN_OBJ_1(mp_os_urandom_obj, mp_os_urandom); -#endif - bool mp_os_dupterm_is_builtin_stream(mp_const_obj_t stream) { const mp_obj_type_t *type = mp_obj_get_type(stream); return false diff --git a/ports/stm32/mphalport.c b/ports/stm32/mphalport.c index e862b46edc..0481a962f7 100644 --- a/ports/stm32/mphalport.c +++ b/ports/stm32/mphalport.c @@ -7,6 +7,7 @@ #include "extmod/misc.h" #include "usb.h" #include "uart.h" +#include "rng.h" #if MICROPY_HW_TINYUSB_STACK #include "shared/tinyusb/mp_usbd_cdc.h" @@ -221,4 +222,15 @@ void mp_hal_get_mac_ascii(int idx, size_t chr_off, size_t chr_len, char *dest) { } } +void mp_hal_get_random(size_t n, uint8_t *buf) { + uint32_t val = 0; + for (int i = 0; i < n; i++) { + if ((i & 3) == 0) { + val = rng_get(); + } + buf[i] = val; + val >>= 8; + } +} + MP_REGISTER_ROOT_POINTER(struct _machine_uart_obj_t *pyb_stdio_uart); diff --git a/ports/stm32/mphalport.h b/ports/stm32/mphalport.h index a742a92fc2..fe144c6eed 100644 --- a/ports/stm32/mphalport.h +++ b/ports/stm32/mphalport.h @@ -173,6 +173,7 @@ enum { void mp_hal_generate_laa_mac(int idx, uint8_t buf[6]); void mp_hal_get_mac(int idx, uint8_t buf[6]); void mp_hal_get_mac_ascii(int idx, size_t chr_off, size_t chr_len, char *dest); +void mp_hal_get_random(size_t n, uint8_t *buf); static inline void mp_hal_wake_main_task_from_isr(void) { // Defined for tinyusb support, nothing needs to be done here. diff --git a/ports/unix/modos.c b/ports/unix/modos.c index 92211d689c..59f31e2782 100644 --- a/ports/unix/modos.c +++ b/ports/unix/modos.c @@ -92,15 +92,6 @@ static mp_obj_t mp_os_system(mp_obj_t cmd_in) { } static MP_DEFINE_CONST_FUN_OBJ_1(mp_os_system_obj, mp_os_system); -static mp_obj_t mp_os_urandom(mp_obj_t num) { - mp_int_t n = mp_obj_get_int(num); - vstr_t vstr; - vstr_init_len(&vstr, n); - mp_hal_get_random(n, vstr.buf); - return mp_obj_new_bytes_from_vstr(&vstr); -} -static MP_DEFINE_CONST_FUN_OBJ_1(mp_os_urandom_obj, mp_os_urandom); - static mp_obj_t mp_os_errno(size_t n_args, const mp_obj_t *args) { if (n_args == 0) { return MP_OBJ_NEW_SMALL_INT(errno); diff --git a/ports/unix/mpconfigport.h b/ports/unix/mpconfigport.h index e290935bca..44e8402093 100644 --- a/ports/unix/mpconfigport.h +++ b/ports/unix/mpconfigport.h @@ -180,10 +180,10 @@ void mp_unix_free_exec(void *ptr, size_t size); // If enabled, configure how to seed random on init. #ifdef MICROPY_PY_RANDOM_SEED_INIT_FUNC #include -void mp_hal_get_random(size_t n, void *buf); +void mp_hal_get_random(size_t n, uint8_t *buf); static inline unsigned long mp_random_seed_init(void) { unsigned long r; - mp_hal_get_random(sizeof(r), &r); + mp_hal_get_random(sizeof(r), (uint8_t *)&r); return r; } #endif diff --git a/ports/unix/mphalport.h b/ports/unix/mphalport.h index 57184c4c2e..5d8004e5e4 100644 --- a/ports/unix/mphalport.h +++ b/ports/unix/mphalport.h @@ -112,7 +112,7 @@ 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); +void mp_hal_get_random(size_t n, uint8_t *buf); #if MICROPY_PY_BLUETOOTH enum { diff --git a/ports/unix/unix_mphal.c b/ports/unix/unix_mphal.c index 39311c9872..b0861aaa77 100644 --- a/ports/unix/unix_mphal.c +++ b/ports/unix/unix_mphal.c @@ -253,7 +253,7 @@ void mp_hal_delay_ms(mp_uint_t ms) { } #endif -void mp_hal_get_random(size_t n, void *buf) { +void mp_hal_get_random(size_t n, uint8_t *buf) { #ifdef _HAVE_GETRANDOM RAISE_ERRNO(getrandom(buf, n, 0), errno); #else diff --git a/ports/windows/windows_mphal.c b/ports/windows/windows_mphal.c index 5a65b2fd2a..50b7a40bf4 100644 --- a/ports/windows/windows_mphal.c +++ b/ports/windows/windows_mphal.c @@ -289,7 +289,7 @@ void mp_hal_delay_ms(mp_uint_t ms) { #endif } -void mp_hal_get_random(size_t n, void *buf) { +void mp_hal_get_random(size_t n, uint8_t *buf) { NTSTATUS result = BCryptGenRandom(NULL, (unsigned char *)buf, n, BCRYPT_USE_SYSTEM_PREFERRED_RNG); if (!BCRYPT_SUCCESS(result)) { mp_raise_OSError(errno);