Skip to content

Commit d2f018b

Browse files
committed
unix,windows: Factor out code that generates random bytes to a new func.
Signed-off-by: Damien George <damien@micropython.org>
1 parent 5bb2a85 commit d2f018b

File tree

4 files changed

+30
-28
lines changed

4 files changed

+30
-28
lines changed

ports/unix/moduos.c

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,12 @@
2525
* THE SOFTWARE.
2626
*/
2727

28-
#include <fcntl.h>
2928
#include <stdlib.h>
3029
#include <string.h>
3130

3231
#include "py/runtime.h"
3332
#include "py/mphal.h"
3433

35-
#if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
36-
#if __GLIBC_PREREQ(2, 25)
37-
#include <sys/random.h>
38-
#define _HAVE_GETRANDOM
39-
#endif
40-
#endif
41-
42-
#if _WIN32
43-
#include <windows.h>
44-
#include <bcrypt.h>
45-
#endif
46-
4734
STATIC mp_obj_t mp_uos_getenv(mp_obj_t var_in) {
4835
const char *s = getenv(mp_obj_str_get_str(var_in));
4936
if (s == NULL) {
@@ -105,21 +92,7 @@ STATIC mp_obj_t mp_uos_urandom(mp_obj_t num) {
10592
mp_int_t n = mp_obj_get_int(num);
10693
vstr_t vstr;
10794
vstr_init_len(&vstr, n);
108-
#if _WIN32
109-
NTSTATUS result = BCryptGenRandom(NULL, (unsigned char *)vstr.buf, n, BCRYPT_USE_SYSTEM_PREFERRED_RNG);
110-
if (!BCRYPT_SUCCESS(result)) {
111-
mp_raise_OSError(errno);
112-
}
113-
#else
114-
#ifdef _HAVE_GETRANDOM
115-
RAISE_ERRNO(getrandom(vstr.buf, n, 0), errno);
116-
#else
117-
int fd = open("/dev/urandom", O_RDONLY);
118-
RAISE_ERRNO(fd, errno);
119-
RAISE_ERRNO(read(fd, vstr.buf, n), errno);
120-
close(fd);
121-
#endif
122-
#endif
95+
mp_hal_get_random(n, vstr.buf);
12396
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
12497
}
12598
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_uos_urandom_obj, mp_uos_urandom);

ports/unix/mphalport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ static inline void mp_hal_delay_us(mp_uint_t us) {
9191
{ if (err_flag == -1) \
9292
{ mp_raise_OSError(error_val); } }
9393

94+
void mp_hal_get_random(size_t n, void *buf);
95+
9496
#if MICROPY_PY_BLUETOOTH
9597
enum {
9698
MP_HAL_MAC_BDADDR,

ports/unix/unix_mphal.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,20 @@
2929
#include <string.h>
3030
#include <time.h>
3131
#include <sys/time.h>
32+
#include <fcntl.h>
3233

3334
#include "py/mphal.h"
3435
#include "py/mpthread.h"
3536
#include "py/runtime.h"
3637
#include "extmod/misc.h"
3738

39+
#if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
40+
#if __GLIBC_PREREQ(2, 25)
41+
#include <sys/random.h>
42+
#define _HAVE_GETRANDOM
43+
#endif
44+
#endif
45+
3846
#ifndef _WIN32
3947
#include <signal.h>
4048

@@ -234,3 +242,14 @@ void mp_hal_delay_ms(mp_uint_t ms) {
234242
usleep(ms * 1000);
235243
#endif
236244
}
245+
246+
void mp_hal_get_random(size_t n, void *buf) {
247+
#ifdef _HAVE_GETRANDOM
248+
RAISE_ERRNO(getrandom(buf, n, 0), errno);
249+
#else
250+
int fd = open("/dev/urandom", O_RDONLY);
251+
RAISE_ERRNO(fd, errno);
252+
RAISE_ERRNO(read(fd, buf, n), errno);
253+
close(fd);
254+
#endif
255+
}

ports/windows/windows_mphal.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <sys/time.h>
3333
#include <windows.h>
3434
#include <unistd.h>
35+
#include <bcrypt.h>
3536

3637
HANDLE std_in = NULL;
3738
HANDLE con_out = NULL;
@@ -286,3 +287,10 @@ void mp_hal_delay_ms(mp_uint_t ms) {
286287
msec_sleep((double)ms);
287288
#endif
288289
}
290+
291+
void mp_hal_get_random(size_t n, void *buf) {
292+
NTSTATUS result = BCryptGenRandom(NULL, (unsigned char *)buf, n, BCRYPT_USE_SYSTEM_PREFERRED_RNG);
293+
if (!BCRYPT_SUCCESS(result)) {
294+
mp_raise_OSError(errno);
295+
}
296+
}

0 commit comments

Comments
 (0)