Skip to content

Commit

Permalink
netbsd: use KERN_ARND sysctl to get entropy
Browse files Browse the repository at this point in the history
PR-URL: libuv#2528
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Saúl Ibarra Corretgé <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Santiago Gimeno <[email protected]>
  • Loading branch information
alarixnia authored and saghul committed Oct 29, 2019
1 parent 2dcf2e8 commit a62f8ce
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/src/misc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
2 changes: 2 additions & 0 deletions src/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions src/unix/netbsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
8 changes: 4 additions & 4 deletions src/unix/random-devurandom.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
1 change: 0 additions & 1 deletion src/unix/random-sysctl-linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit a62f8ce

Please sign in to comment.