ports: Refactor os.urandom().

Factor out mp_os_urandom() of each port into extmod/modos.c, which then
calls the port-specific function mp_hal_get_random().

Move mp_hal_get_random() to mphalport where suitable. At the
MIMXRT and SAMD it is left in modos.c, since there are different
implementation depending on the MCU family.

At the ALIF, ESP32, CC3200 and RP2 port the file modos.c was removed,
since it was empty after moving mp_hal_get_random().

Tested for the cc3200, esp32, esp8266, mimxrt, nrf, rp2, samd, stm32
and unix ports. Compiled for the alif and the renesas port.

Signed-off-by: robert-hh <robert@hammelrath.com>
This commit is contained in:
robert-hh
2026-02-17 14:52:02 +01:00
committed by Damien George
parent fdb7c0f747
commit 2e62b56bdd
42 changed files with 130 additions and 296 deletions

View File

@@ -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) },

View File

@@ -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

View File

@@ -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)

View File

@@ -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;
}
}

View File

@@ -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);

View File

@@ -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
******************************************************************************/

View File

@@ -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);

View File

@@ -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);

View File

@@ -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)

View File

@@ -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);

View File

@@ -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)

View File

@@ -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;
}
}

View File

@@ -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

View File

@@ -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;
}
}

View File

@@ -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);

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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) {

View File

@@ -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)

View File

@@ -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;
}
}

View File

@@ -26,3 +26,5 @@
#include <stddef.h>
#include "shared/runtime/interrupt_char.h"
void mp_hal_get_random(size_t n, uint8_t *buf);

View File

@@ -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

View File

@@ -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();

View File

@@ -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);

View File

@@ -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

View File

@@ -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);

View File

@@ -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)

View File

@@ -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));
}
}

View File

@@ -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

View File

@@ -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) {

View File

@@ -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

View File

@@ -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

View File

@@ -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);

View File

@@ -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.

View File

@@ -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);

View File

@@ -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 <stddef.h>
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

View File

@@ -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 {

View File

@@ -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

View File

@@ -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);