File tree Expand file tree Collapse file tree 3 files changed +25
-5
lines changed Expand file tree Collapse file tree 3 files changed +25
-5
lines changed Original file line number Diff line number Diff line change @@ -66,8 +66,24 @@ bool common_hal_os_urandom(uint8_t *buffer, uint32_t length) {
66
66
uint8_t sd_en = 0 ;
67
67
(void ) sd_softdevice_is_enabled (& sd_en );
68
68
69
- if (sd_en )
70
- return NRF_SUCCESS == sd_rand_application_vector_get (buffer , length );
69
+ if (sd_en ) {
70
+ while (length != 0 ) {
71
+ uint8_t available = 0 ;
72
+ sd_rand_application_bytes_available_get (& available );
73
+ if (available ) {
74
+ uint32_t request = MIN (length , available );
75
+ uint32_t result = sd_rand_application_vector_get (buffer , request );
76
+ if (result != NRF_SUCCESS ) {
77
+ return false;
78
+ }
79
+ buffer += request ;
80
+ length -= request ;
81
+ } else {
82
+ RUN_BACKGROUND_TASKS ;
83
+ }
84
+ }
85
+ return true;
86
+ }
71
87
#endif
72
88
73
89
nrf_rng_event_clear (NRF_RNG , NRF_RNG_EVENT_VALRDY );
Original file line number Diff line number Diff line change @@ -50,6 +50,9 @@ void vstr_init(vstr_t *vstr, size_t alloc) {
50
50
// Init the vstr so it allocs exactly enough ram to hold a null-terminated
51
51
// string of the given length, and set the length.
52
52
void vstr_init_len (vstr_t * vstr , size_t len ) {
53
+ if (len == SIZE_MAX ) {
54
+ m_malloc_fail (len );
55
+ }
53
56
vstr_init (vstr , len + 1 );
54
57
vstr -> len = len ;
55
58
}
Original file line number Diff line number Diff line change 33
33
#include "lib/oofatfs/diskio.h"
34
34
#include "py/mpstate.h"
35
35
#include "py/obj.h"
36
+ #include "py/objstr.h"
36
37
#include "py/runtime.h"
37
38
#include "shared-bindings/os/__init__.h"
38
39
@@ -195,11 +196,11 @@ MP_DEFINE_CONST_FUN_OBJ_0(os_sync_obj, os_sync);
195
196
//|
196
197
STATIC mp_obj_t os_urandom (mp_obj_t size_in ) {
197
198
mp_int_t size = mp_obj_get_int (size_in );
198
- uint8_t tmp [ size ] ;
199
- if (!common_hal_os_urandom (tmp , size )) {
199
+ mp_obj_str_t * result = MP_OBJ_TO_PTR ( mp_obj_new_bytes_of_zeros ( size )) ;
200
+ if (!common_hal_os_urandom (( uint8_t * ) result -> data , size )) {
200
201
mp_raise_NotImplementedError (translate ("No hardware random available" ));
201
202
}
202
- return mp_obj_new_bytes ( tmp , size ) ;
203
+ return result ;
203
204
}
204
205
MP_DEFINE_CONST_FUN_OBJ_1 (os_urandom_obj , os_urandom );
205
206
You can’t perform that action at this time.
0 commit comments