Skip to content

Commit 5cd15ff

Browse files
committed
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."
1 parent a41976a commit 5cd15ff

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/random.cpp

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

0 commit comments

Comments
 (0)