Changeset View
Changeset View
Standalone View
Standalone View
src/random.cpp
Show First 20 Lines • Show All 352 Lines • ▼ Show 20 Lines | #elif defined(HAVE_GETENTROPY) && defined(__OpenBSD__) | ||||
* error if more are requested. | * error if more are requested. | ||||
* The call cannot return less than the requested number of bytes. | * The call cannot return less than the requested number of bytes. | ||||
* getentropy is explicitly limited to openbsd here, as a similar (but not | * getentropy is explicitly limited to openbsd here, as a similar (but not | ||||
* the same) function may exist on other platforms via glibc. | * the same) function may exist on other platforms via glibc. | ||||
*/ | */ | ||||
if (getentropy(ent32, NUM_OS_RANDOM_BYTES) != 0) { | if (getentropy(ent32, NUM_OS_RANDOM_BYTES) != 0) { | ||||
RandFailure(); | RandFailure(); | ||||
} | } | ||||
// Silence a compiler warning about unused function. | |||||
(void)GetDevURandom; | |||||
#elif defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX) | #elif defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX) | ||||
/** | /** | ||||
* getentropy() is available on macOS 10.12 and later. | * getentropy() is available on macOS 10.12 and later. | ||||
*/ | */ | ||||
if (getentropy(ent32, NUM_OS_RANDOM_BYTES) != 0) { | if (getentropy(ent32, NUM_OS_RANDOM_BYTES) != 0) { | ||||
RandFailure(); | RandFailure(); | ||||
} | } | ||||
// Silence a compiler warning about unused function. | |||||
(void)GetDevURandom; | |||||
#elif defined(HAVE_SYSCTL_ARND) | #elif defined(HAVE_SYSCTL_ARND) | ||||
/** | /** | ||||
* FreeBSD and similar. It is possible for the call to return less bytes | * FreeBSD and similar. It is possible for the call to return less bytes | ||||
* than requested, so need to read in a loop. | * than requested, so need to read in a loop. | ||||
*/ | */ | ||||
static const int name[2] = {CTL_KERN, KERN_ARND}; | static const int name[2] = {CTL_KERN, KERN_ARND}; | ||||
int have = 0; | int have = 0; | ||||
do { | do { | ||||
size_t len = NUM_OS_RANDOM_BYTES - have; | size_t len = NUM_OS_RANDOM_BYTES - have; | ||||
if (sysctl(name, ARRAYLEN(name), ent32 + have, &len, nullptr, 0) != 0) { | if (sysctl(name, ARRAYLEN(name), ent32 + have, &len, nullptr, 0) != 0) { | ||||
RandFailure(); | RandFailure(); | ||||
} | } | ||||
have += len; | have += len; | ||||
} while (have < NUM_OS_RANDOM_BYTES); | } while (have < NUM_OS_RANDOM_BYTES); | ||||
// Silence a compiler warning about unused function. | |||||
(void)GetDevURandom; | |||||
#else | #else | ||||
/** | /** | ||||
* Fall back to /dev/urandom if there is no specific method implemented to | * Fall back to /dev/urandom if there is no specific method implemented to | ||||
* get system entropy for this OS. | * get system entropy for this OS. | ||||
*/ | */ | ||||
GetDevURandom(ent32); | GetDevURandom(ent32); | ||||
#endif | #endif | ||||
} | } | ||||
▲ Show 20 Lines • Show All 395 Lines • Show Last 20 Lines |