tech-crypto archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: getentropy() support



> Date: Mon, 4 Jun 2018 22:56:41 +0200
> From: Kurt Roeckx <kurt%roeckx.be@localhost>
> 
> Would it be possible to add getentropy() to NetBSD? Most major
> UNIX OSs have added support for it, NetBSD is currently the only
> one I know that doesn't have it. It was introduced by OpenBSD, and
> is available in at least FreeBSD, Solaris, Linux and OSX.
> 
> The reason for getentropy() is that it's a direct system call, there is
> no need to open a file which might not be available in a chroot
> for instance.

Yes, we should have it.

> getentropy() provides the following features:
> - It's a high quality CSRNG, intended to be used by a CSRNG in
>   userspace.
> - It blocks when the kernel CSRNG hasn't been initialized yet.

The blocking behaviour isn't documented in
<http://man.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man2/getentropy.2>,
and I don't think it would be a good idea.  In particular, if a system
call _might_ block, it should do so regularly so that the blocking
code path is exercised, rather than only once in a great while and
especially not during interactive testing.

Here's a candidate (untested) definition we could drop into libc with
no changes to the kernel, if someone wants to beat me to finding the
spare time to do it.  (Unfortunately, it's presumably too late for
netbsd-8.)

int
getentropy(void *buf, size_t buflen)
{
	const int mib[] = { CTL_KERN, KERN_ARND };
	size_t n = buflen;

	if (buflen > 256) {
		errno = EIO;
		return -1;
	}

	if (sysctl(mib, (u_int)__arraycount(mib), buf, &n, NULL, 0) == -1)
		return -1;
	if (n != buflen) {
		errno = EIO;
		return -1;
	}

	return 0;
}


Home | Main Index | Thread Index | Old Index