File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -60,4 +60,30 @@ STATIC mp_obj_t pyb_rng_get(void) {
6060}
6161MP_DEFINE_CONST_FUN_OBJ_0 (pyb_rng_get_obj , pyb_rng_get );
6262
63+ #else // MICROPY_HW_ENABLE_RNG
64+
65+ // For MCUs that don't have an RNG we still need to provide a rng_get() function,
66+ // eg for lwIP. A pseudo-RNG is not really ideal but we go with it for now. We
67+ // don't want to use urandom's pRNG because then the user won't see a reproducible
68+ // random stream.
69+
70+ // Yasmarang random number generator by Ilya Levin
71+ // http://www.literatecode.com/yasmarang
72+ STATIC uint32_t pyb_rng_yasmarang (void ) {
73+ static uint32_t pad = 0xeda4baba , n = 69 , d = 233 ;
74+ static uint8_t dat = 0 ;
75+
76+ pad += dat + d * n ;
77+ pad = (pad << 3 ) + (pad >> 29 );
78+ n = pad | 2 ;
79+ d ^= (pad << 31 ) + (pad >> 1 );
80+ dat ^= (char )pad ^ (d >> 8 ) ^ 1 ;
81+
82+ return pad ^ (d << 5 ) ^ (pad >> 18 ) ^ (dat << 1 );
83+ }
84+
85+ uint32_t rng_get (void ) {
86+ return pyb_rng_yasmarang ();
87+ }
88+
6389#endif // MICROPY_HW_ENABLE_RNG
You can’t perform that action at this time.
0 commit comments