12345678910111213141516171819202122232425262728293031323334 | // Wrapper logic around different systems' random functions.
//
// This is the one place that decides how to get secure random bytes on each
// platform. The helpers are `static inline` rather than `static` so a file
// that needs only getrandom() does not warn about the other going unused.
#pragma once
#include <assert.h>
#include <stdint.h>
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
#include <stdlib.h>
static inline ssize_t getrandom(void *buf, size_t buflen, unsigned int flags) {
(void)flags;
arc4random_buf(buf, buflen);
return buflen;
}
#elif defined(__linux__)
// Use getrandom()
#include <sys/random.h>
#else
#error "Unsupported platform for secure random number generation"
#endif
static inline int64_t random_range(int64_t low, int64_t high) {
uint64_t range = (uint64_t)high - (uint64_t)low + 1;
uint64_t min_r = -range % range;
uint64_t r = 0;
do {
assert(getrandom(&r, sizeof(r), 0) == sizeof(r));
} while (r < min_r);
return (int64_t)((uint64_t)low + (r % range));
}
|