Use getrandom(2) instead of /dev/urandom where available

This commit is contained in:
наб 2025-06-23 01:16:00 +02:00
parent 7d3805af9f
commit f5eafc4a10
No known key found for this signature in database
GPG key ID: BCFD0B018D2658F1

View file

@ -65,9 +65,20 @@ static void generate_system_random_bytes(size_t n, void *result) {
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/random.h>
#include <unistd.h>
static void generate_system_random_bytes(size_t n, void *result) {
#if __linux__ || __NetBSD__ || __FreeBSD__ || __DragonFly__
auto got = getrandom(result, n, 0);
if (got != -1) {
result = padd(result, got);
n -= got;
}
#endif
if (n == 0)
return;
int fd;
if ((fd = open("/dev/urandom", O_RDONLY | O_NOCTTY | O_CLOEXEC)) < 0) {
err(EXIT_FAILURE, "open /dev/urandom");