Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
micropython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
xpstem
micropython
Commits
d2f018bf
Commit
d2f018bf
authored
Jun 02, 2022
by
Damien George
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
unix,windows: Factor out code that generates random bytes to a new func.
Signed-off-by:
Damien George
<
damien@micropython.org
>
parent
5bb2a85d
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
30 additions
and
28 deletions
+30
-28
ports/unix/moduos.c
ports/unix/moduos.c
+1
-28
ports/unix/mphalport.h
ports/unix/mphalport.h
+2
-0
ports/unix/unix_mphal.c
ports/unix/unix_mphal.c
+19
-0
ports/windows/windows_mphal.c
ports/windows/windows_mphal.c
+8
-0
No files found.
ports/unix/moduos.c
View file @
d2f018bf
...
@@ -25,25 +25,12 @@
...
@@ -25,25 +25,12 @@
* THE SOFTWARE.
* THE SOFTWARE.
*/
*/
#include <fcntl.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
#include "py/runtime.h"
#include "py/runtime.h"
#include "py/mphal.h"
#include "py/mphal.h"
#if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
#if __GLIBC_PREREQ(2, 25)
#include <sys/random.h>
#define _HAVE_GETRANDOM
#endif
#endif
#if _WIN32
#include <windows.h>
#include <bcrypt.h>
#endif
STATIC
mp_obj_t
mp_uos_getenv
(
mp_obj_t
var_in
)
{
STATIC
mp_obj_t
mp_uos_getenv
(
mp_obj_t
var_in
)
{
const
char
*
s
=
getenv
(
mp_obj_str_get_str
(
var_in
));
const
char
*
s
=
getenv
(
mp_obj_str_get_str
(
var_in
));
if
(
s
==
NULL
)
{
if
(
s
==
NULL
)
{
...
@@ -105,21 +92,7 @@ STATIC mp_obj_t mp_uos_urandom(mp_obj_t num) {
...
@@ -105,21 +92,7 @@ STATIC mp_obj_t mp_uos_urandom(mp_obj_t num) {
mp_int_t
n
=
mp_obj_get_int
(
num
);
mp_int_t
n
=
mp_obj_get_int
(
num
);
vstr_t
vstr
;
vstr_t
vstr
;
vstr_init_len
(
&
vstr
,
n
);
vstr_init_len
(
&
vstr
,
n
);
#if _WIN32
mp_hal_get_random
(
n
,
vstr
.
buf
);
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
return
mp_obj_new_str_from_vstr
(
&
mp_type_bytes
,
&
vstr
);
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
);
STATIC
MP_DEFINE_CONST_FUN_OBJ_1
(
mp_uos_urandom_obj
,
mp_uos_urandom
);
...
...
ports/unix/mphalport.h
View file @
d2f018bf
...
@@ -91,6 +91,8 @@ static inline void mp_hal_delay_us(mp_uint_t us) {
...
@@ -91,6 +91,8 @@ static inline void mp_hal_delay_us(mp_uint_t us) {
{ if (err_flag == -1) \
{ if (err_flag == -1) \
{ mp_raise_OSError(error_val); } }
{ mp_raise_OSError(error_val); } }
void
mp_hal_get_random
(
size_t
n
,
void
*
buf
);
#if MICROPY_PY_BLUETOOTH
#if MICROPY_PY_BLUETOOTH
enum
{
enum
{
MP_HAL_MAC_BDADDR
,
MP_HAL_MAC_BDADDR
,
...
...
ports/unix/unix_mphal.c
View file @
d2f018bf
...
@@ -29,12 +29,20 @@
...
@@ -29,12 +29,20 @@
#include <string.h>
#include <string.h>
#include <time.h>
#include <time.h>
#include <sys/time.h>
#include <sys/time.h>
#include <fcntl.h>
#include "py/mphal.h"
#include "py/mphal.h"
#include "py/mpthread.h"
#include "py/mpthread.h"
#include "py/runtime.h"
#include "py/runtime.h"
#include "extmod/misc.h"
#include "extmod/misc.h"
#if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
#if __GLIBC_PREREQ(2, 25)
#include <sys/random.h>
#define _HAVE_GETRANDOM
#endif
#endif
#ifndef _WIN32
#ifndef _WIN32
#include <signal.h>
#include <signal.h>
...
@@ -234,3 +242,14 @@ void mp_hal_delay_ms(mp_uint_t ms) {
...
@@ -234,3 +242,14 @@ void mp_hal_delay_ms(mp_uint_t ms) {
usleep
(
ms
*
1000
);
usleep
(
ms
*
1000
);
#endif
#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
}
ports/windows/windows_mphal.c
View file @
d2f018bf
...
@@ -32,6 +32,7 @@
...
@@ -32,6 +32,7 @@
#include <sys/time.h>
#include <sys/time.h>
#include <windows.h>
#include <windows.h>
#include <unistd.h>
#include <unistd.h>
#include <bcrypt.h>
HANDLE
std_in
=
NULL
;
HANDLE
std_in
=
NULL
;
HANDLE
con_out
=
NULL
;
HANDLE
con_out
=
NULL
;
...
@@ -286,3 +287,10 @@ void mp_hal_delay_ms(mp_uint_t ms) {
...
@@ -286,3 +287,10 @@ void mp_hal_delay_ms(mp_uint_t ms) {
msec_sleep
((
double
)
ms
);
msec_sleep
((
double
)
ms
);
#endif
#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
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment