random: use arc4random on OpenBSD

Following best practices on OpenBSD. The getentropy(2) man page states:
"getentropy() is not intended for regular code;
 please use the arc4random(3) family of functions instead."
This commit is contained in:
Sebastian Falbesoner 2022-02-02 15:35:26 +01:00
parent a41976ab77
commit 5cd15ffdce

View file

@ -305,16 +305,14 @@ void GetOSRand(unsigned char *ent32)
RandFailure(); RandFailure();
} }
} }
#elif defined(HAVE_GETENTROPY) && defined(__OpenBSD__) #elif defined(__OpenBSD__)
/* On OpenBSD this can return up to 256 bytes of entropy, will return an /* OpenBSD. From the arc4random(3) man page:
* error if more are requested. "Use of these functions is encouraged for almost all random number
* The call cannot return less than the requested number of bytes. consumption because the other interfaces are deficient in either
getentropy is explicitly limited to openbsd here, as a similar (but not quality, portability, standardization, or availability."
the same) function may exist on other platforms via glibc. The function call is always successful.
*/ */
if (getentropy(ent32, NUM_OS_RANDOM_BYTES) != 0) { arc4random_buf(ent32, NUM_OS_RANDOM_BYTES);
RandFailure();
}
// Silence a compiler warning about unused function. // Silence a compiler warning about unused function.
(void)GetDevURandom; (void)GetDevURandom;
#elif defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX) #elif defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX)