File tree Expand file tree Collapse file tree 1 file changed +9
-4
lines changed Expand file tree Collapse file tree 1 file changed +9
-4
lines changed Original file line number Diff line number Diff line change @@ -72,15 +72,20 @@ bool common_hal_os_urandom(uint8_t *buffer, uint32_t length) {
72
72
if (HAL_RNG_Init (& handle ) != HAL_OK ) mp_raise_ValueError (translate ("RNG Init Error" ));
73
73
74
74
//Assign bytes
75
- for (uint i = 0 ; i < length ; i ++ ) {
76
- uint32_t temp ;
75
+ uint32_t i = 0 ;
76
+ while (i < length ) {
77
+ uint32_t new_random ;
77
78
uint32_t start = HAL_GetTick ();
78
79
//the HAL function has a timeout, but it isn't long enough, and isn't adjustable
79
80
while (!(__HAL_RNG_GET_FLAG (& handle ,RNG_FLAG_DRDY )) && ((HAL_GetTick () - start ) < RNG_TIMEOUT ));
80
- if (HAL_RNG_GenerateRandomNumber (& handle , & temp ) != HAL_OK ) {
81
+ if (HAL_RNG_GenerateRandomNumber (& handle , & new_random ) != HAL_OK ) {
81
82
mp_raise_ValueError (translate ("Random number generation error" ));
82
83
}
83
- buffer [i ] = (uint8_t )temp ;
84
+ for (int j = 0 ; j < 4 && i < length ; j ++ ) {
85
+ buffer [i ] = new_random & 0xff ;
86
+ i ++ ;
87
+ new_random >>= 8 ;
88
+ }
84
89
}
85
90
86
91
//shut down the peripheral
You can’t perform that action at this time.
0 commit comments