netbsd: use KERN_ARND sysctl to get entropy

PR-URL: https://github.com/libuv/libuv/pull/2528
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Saúl Ibarra Corretgé <s@saghul.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
This commit is contained in:
nia 2019-10-21 20:33:48 +01:00 committed by Saúl Ibarra Corretgé
parent 2dcf2e8188
commit a62f8ced7a
5 changed files with 30 additions and 5 deletions

View File

@ -679,6 +679,7 @@ API
:man:`sysctl(2)`.
- FreeBSD: `getrandom(2) <https://www.freebsd.org/cgi/man.cgi?query=getrandom&sektion=2>_`,
or `/dev/urandom` after reading from `/dev/random` once.
- NetBSD: `KERN_ARND` `sysctl(3) <https://netbsd.gw.com/cgi-bin/man-cgi?sysctl+3+NetBSD-current>_`
- macOS, OpenBSD: `getentropy(2) <https://man.openbsd.org/getentropy.2>_`
if available, or `/dev/urandom` after reading from `/dev/random` once.
- AIX: `/dev/random`.

View File

@ -40,6 +40,8 @@ static int uv__random(void* buf, size_t buflen) {
rc = uv__random_getentropy(buf, buflen);
if (rc == UV_ENOSYS)
rc = uv__random_devurandom(buf, buflen);
#elif defined(__NetBSD__)
rc = uv__random_sysctl(buf, buflen);
#elif defined(__FreeBSD__) || defined(__linux__)
rc = uv__random_getrandom(buf, buflen);
if (rc == UV_ENOSYS)

View File

@ -234,3 +234,26 @@ int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
uv__free(cp_times);
return 0;
}
int uv__random_sysctl(void* buf, size_t len) {
static int name[] = {CTL_KERN, KERN_ARND};
size_t count, req;
unsigned char* p;
p = buf;
while (len) {
req = len < 32 ? len : 32;
count = req;
if (sysctl(name, ARRAY_SIZE(name), p, &count, NULL, 0) == -1)
return UV__ERR(errno);
if (count != req)
return UV_EIO; /* Can't happen. */
p += count;
len -= count;
}
return 0;
}

View File

@ -74,10 +74,10 @@ int uv__random_readpath(const char* path, void* buf, size_t buflen) {
static void uv__random_devurandom_init(void) {
char c;
/* Linux's and NetBSD's random(4) man page suggests applications should read
* at least once from /dev/random before switching to /dev/urandom in order
* to seed the system RNG. Reads from /dev/random can of course block
* indefinitely until entropy is available but that's the point.
/* Linux's random(4) man page suggests applications should read at least
* once from /dev/random before switching to /dev/urandom in order to seed
* the system RNG. Reads from /dev/random can of course block indefinitely
* until entropy is available but that's the point.
*/
status = uv__random_readpath("/dev/random", &c, 1);
}

View File

@ -40,7 +40,6 @@ struct uv__sysctl_args {
};
/* TODO(bnoordhuis) Use {CTL_KERN, KERN_ARND} on FreeBSD (and NetBSD?) */
int uv__random_sysctl(void* buf, size_t buflen) {
static int name[] = {1 /*CTL_KERN*/, 40 /*KERN_RANDOM*/, 6 /*RANDOM_UUID*/};
struct uv__sysctl_args args;