#include <isc/types.h>
#include <isc/util.h>
+#if defined(_WIN32) || defined(_WIN64)
+#include <isc/once.h>
+#endif
+
#if defined(__linux__)
# ifdef HAVE_GETRANDOM
# define have_getrandom() 1
(unsigned int)flags)
static unsigned
-have_getrandom(void)
-{
+have_getrandom(void) {
uint16_t buf;
ssize_t ret;
ret = getrandom(&buf, sizeof(buf), 1 /*GRND_NONBLOCK*/);
# endif /* ifdef HAVE_GETRANDOM */
static int
-getrandom_buf(void *buf, size_t buflen)
-{
+getrandom_buf(void *buf, size_t buflen) {
size_t left = buflen;
ssize_t ret;
uint8_t *p = buf;
static HCRYPTPROV isc_random_hcryptprov;
static void isc_random_initialize(void) {
- RUNTIME_CHECK(CryptAcquireContext(&hcryptprov, NULL, NULL, PROV_RSA_FULL,
+ RUNTIME_CHECK(CryptAcquireContext(&isc_random_hcryptprov,
+ NULL, NULL, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT|CRYPT_SILENT));
}
#endif /* defined(_WIN32) || defined(_WIN64) */
uint32_t
-isc_random(void)
-{
+isc_random(void) {
#if defined(HAVE_ARC4RANDOM)
return(arc4random());
#else /* HAVE_ARC4RANDOM */
* Fill the region buf of length buflen with random data.
*/
void
-isc_random_buf(void *buf, size_t buflen)
-{
+isc_random_buf(void *buf, size_t buflen) {
REQUIRE(buf);
REQUIRE(buflen > 0);
#if defined(_WIN32) || defined(_WIN64)
- RUNTIME_CHECK(isc_once_do(&once, initialize_rand) == ISC_R_SUCCESS);
- RUNTIME_CHECK(CryptGenRandom(isc_random_hcryptprov, (DWORD)buflen, buf));
+ RUNTIME_CHECK(isc_once_do(&isc_random_once,
+ isc_random_initialize) == ISC_R_SUCCESS);
+ RUNTIME_CHECK(CryptGenRandom(isc_random_hcryptprov,
+ (DWORD)buflen, buf));
return;
#elif defined(HAVE_ARC4RANDOM_BUF)
arc4random_buf(buf, buflen);
#else
# if defined(__linux__)
- /* We need to check the availability of the SYS_getrandom syscall at runtime
- * and fall back to crypto library provider if not available
+ /*
+ * We need to check the availability of the SYS_getrandom
+ * syscall at runtime and fall back to crypto library provider
+ * if not available
*/
if (have_getrandom()) {
getrandom_buf(buf, buflen);
}
uint32_t
-isc_random_uniform(uint32_t upper_bound)
-{
+isc_random_uniform(uint32_t upper_bound) {
#if defined(HAVE_ARC4RANDOM_UNIFORM)
return(arc4random_uniform(upper_bound));
#else /* if defined(HAVE_ARC4RANDOM_UNIFORM) */
/* Copy of arc4random_uniform from OpenBSD */
- u_int32_t r, min;
+ uint32_t r, min;
if (upper_bound < 2) {
return (0);