Skip to content

Commit 2e62b56

Browse files
robert-hhdpgeorge
authored andcommitted
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>
1 parent fdb7c0f commit 2e62b56

42 files changed

Lines changed: 130 additions & 296 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

extmod/modos.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,18 @@ static mp_obj_t mp_os_dupterm_notify(mp_obj_t obj_in) {
145145
static MP_DEFINE_CONST_FUN_OBJ_1(mp_os_dupterm_notify_obj, mp_os_dupterm_notify);
146146
#endif
147147

148+
#if MICROPY_PY_OS_URANDOM
149+
// This wraps the port-specific mp_hal_get_random(), which is usually defined in mphalport.c.
150+
static mp_obj_t mp_os_urandom(mp_obj_t num) {
151+
mp_int_t n = mp_obj_get_int(num);
152+
vstr_t vstr;
153+
vstr_init_len(&vstr, n);
154+
mp_hal_get_random(n, (uint8_t *)vstr.buf);
155+
return mp_obj_new_bytes_from_vstr(&vstr);
156+
}
157+
static MP_DEFINE_CONST_FUN_OBJ_1(mp_os_urandom_obj, mp_os_urandom);
158+
#endif
159+
148160
static const mp_rom_map_elem_t os_module_globals_table[] = {
149161
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_os) },
150162

ports/alif/modos.c

Lines changed: 0 additions & 49 deletions
This file was deleted.

ports/alif/mpconfigport.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@
113113

114114
// Extended modules
115115
#define MICROPY_EPOCH_IS_1970 (1)
116-
#define MICROPY_PY_OS_INCLUDEFILE "ports/alif/modos.c"
117116
#define MICROPY_PY_OS_DUPTERM (1)
118117
#define MICROPY_PY_OS_SEP (1)
119118
#define MICROPY_PY_OS_SYNC (1)

ports/alif/mphalport.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,17 @@ void mp_hal_get_mac_ascii(int idx, size_t chr_off, size_t chr_len, char *dest) {
269269
*dest++ = hexchr[mac[chr_off >> 1] >> (4 * (1 - (chr_off & 1))) & 0xf];
270270
}
271271
}
272+
273+
void mp_hal_get_random(size_t n, uint8_t *buf) {
274+
uint64_t rnd = 0;
275+
size_t rnd_bits = 0;
276+
for (int i = 0; i < n; i++) {
277+
if (rnd_bits == 0) {
278+
rnd = se_services_rand64();
279+
rnd_bits = 64;
280+
}
281+
buf[i] = rnd;
282+
rnd >>= 8;
283+
rnd_bits -= 8;
284+
}
285+
}

ports/alif/mphalport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,3 +375,5 @@ void mp_hal_get_mac(int idx, uint8_t buf[6]);
375375
void mp_hal_get_mac_ascii(int idx, size_t chr_off, size_t chr_len, char *dest);
376376

377377
uint32_t mp_hal_time_get(uint32_t *microseconds);
378+
379+
void mp_hal_get_random(size_t n, uint8_t *buf);

ports/cc3200/hal/cc3200_hal.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#include "telnet.h"
5050
#include "pybuart.h"
5151
#include "utils.h"
52+
#include "random.h"
5253

5354
#ifdef USE_FREERTOS
5455
#include "FreeRTOS.h"
@@ -168,6 +169,13 @@ int mp_hal_stdin_rx_chr(void) {
168169
}
169170
}
170171

172+
// Fill buf with n random bytes, generated by the hardware random number generator.
173+
void mp_hal_get_random(size_t n, uint8_t *buf) {
174+
for (int i = 0; i < n; i++) {
175+
buf[i] = rng_get();
176+
}
177+
}
178+
171179
/******************************************************************************
172180
DEFINE PRIVATE FUNCTIONS
173181
******************************************************************************/

ports/cc3200/hal/cc3200_hal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,5 @@ extern void mp_hal_set_interrupt_char (int c);
7575
#define mp_hal_delay_us(usec) UtilsDelay(UTILS_DELAY_US_TO_COUNT(usec))
7676
#define mp_hal_ticks_cpu() (SysTickPeriodGet() - SysTickValueGet())
7777
#define mp_hal_time_ns() (0) // not implemented
78+
79+
void mp_hal_get_random(size_t n, uint8_t *buf);

ports/cc3200/mods/modos.c

Lines changed: 0 additions & 43 deletions
This file was deleted.

ports/cc3200/mpconfigport.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@
9595
#define MICROPY_PY_JSON (1)
9696
#define MICROPY_PY_RE (1)
9797
#define MICROPY_PY_OS (1)
98-
#define MICROPY_PY_OS_INCLUDEFILE "ports/cc3200/mods/modos.c"
9998
#define MICROPY_PY_OS_DUPTERM (1)
10099
#define MICROPY_PY_OS_SYNC (1)
101100
#define MICROPY_PY_OS_URANDOM (1)

ports/esp32/modos.c

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)