Hi,
Working on shuf to validate a 3rd-party bugfix, I realised that
BusyBox uses rand() in "best effort" despite some TODO and warnings
being disseminated around. Others applets that uses rand() are (for
example, but not an exhaustive list) zcip.c, > awk.c, ntpd.c,
telnetd.c and tls.c
Searching srand, and finding that busybox uses it since 2001 despite
the fact that we know that rand() in some cases cannot even fill a 16
bit register.
v5 --> v6:
- even when rand() is limited to 15 bits but properly seeded,
the new function random_in_range() passes PractRand RNG_test
for a size of 8GB which is a dramatic improvement compared
the current use of rand() in BusyBox in terms of randomness.
Paradoxically some people out there that went crazy about uchaos as
RNG, never took into consideration to audit how busybox TLS was using
rand()... LOL
busybox/testsuite$ ./rtest 0 0xffffffff $(( 1 << 31 )) | RNG_test stdin64
Passing 8GB is a good sign of pseudo-RNG and not necessarily a source
of randomness based on true entropy. In fact, rand() requires being
seeded by srand(). Every real-entropy randomness generator is able to
self-seed itself (warm-up, not technically a seeding).
static inline
uint32_t random_in_range(uint32_t min, uint32_t max)
{
uint32_t r = (rand() % RAND_MAX);
/* RAND_MAX can be as small as 32767 */
r ^= (uint32_t)(rand() % RAND_MAX) << 17;
r ^= (uint32_t)(rand() % RAND_MAX) << 7;
r *= murmul1;
r ^= r >> 16;
r %= max-min;
r += min;
return r;
}
static inline
void srand_init(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
srand( ts.tv_sec ^ ts.tv_nsec );
}
Anyway, the combo of random_in_range() and srand_init() are shocking
for their relative simplicity. They are a bit more convoluted than the
srand() / rand() combo but compared to passing 8GB in RNG_test, they
are pretty simple. In fact changing shuf.c required 26 bytes of extra
footprint. Moving these functions in libbb.a for general usage seems
the most reasonable choice, at this point and unless confutation in
some relevant cases (aka downstream testing).
https://github.com/robang74/busybox/
/commit/edff9d2478733a8f0df6fc7d5c1ba0e3defc8db1
So the question is: are there specific cases in which
testsuite/randtest.c fails?
Best regards,
--
Roberto A. Foglietta
+49.176.274.75.661
+39.349.33.30.697