Module Name:    src
Committed By:   nia
Date:           Wed May  6 16:17:37 UTC 2020

Modified Files:
        src/distrib/sets/lists/comp: mi
        src/include: unistd.h
        src/lib/libc/gen: Makefile.inc
        src/lib/libc/include: namespace.h
Added Files:
        src/lib/libc/gen: getentropy.3 getentropy.c

Log Message:
Add getentropy() to libc - a simple wrapper to access the kernel CSPRNG.

Posted to tech-userlevel@ a week ago and reviewed by riastradh@.

GETENTROPY(3)              Library Functions Manual              GETENTROPY(3)

NAME
     getentropy - fill a buffer with high quality random data

LIBRARY
     Standard C Library (libc, -lc)

SYNOPSIS
     #include <unistd.h>

     int
     getentropy(void *buf, size_t buflen);

DESCRIPTION
     The getentropy() function fills a buffer with high quality random data,
     suitable for seeding cryptographically secure psuedorandom number
     generators.

     getentropy() is only intended for seeding random number generators and is
     not intended for use by regular code which simply needs secure random
     data.  For this purpose, please use arc4random(3).

     The maximum value for buflen is 256 bytes.

IMPLEMENTATION NOTES
     getentropy() reads from the sysctl(7) variable kern.arandom.

RETURN VALUES
     The getentropy() function returns 0 on success, and -1 if an error
     occurred.

ERRORS
     getentropy() will succeed unless:

     [EFAULT]           The buf argument points to an invalid memory address.

     [EIO]              Too many bytes were requested.

SEE ALSO
     arc4random(3), rnd(4)

STANDARDS
     The getentropy() function is non-standard.

HISTORY
     The getentropy() function first appeared in OpenBSD 5.6, then in
     FreeBSD 12.0, and NetBSD 10.


To generate a diff of this commit:
cvs rdiff -u -r1.2324 -r1.2325 src/distrib/sets/lists/comp/mi
cvs rdiff -u -r1.156 -r1.157 src/include/unistd.h
cvs rdiff -u -r1.201 -r1.202 src/lib/libc/gen/Makefile.inc
cvs rdiff -u -r0 -r1.1 src/lib/libc/gen/getentropy.3 \
    src/lib/libc/gen/getentropy.c
cvs rdiff -u -r1.198 -r1.199 src/lib/libc/include/namespace.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/distrib/sets/lists/comp/mi
diff -u src/distrib/sets/lists/comp/mi:1.2324 src/distrib/sets/lists/comp/mi:1.2325
--- src/distrib/sets/lists/comp/mi:1.2324	Fri May  1 21:43:23 2020
+++ src/distrib/sets/lists/comp/mi	Wed May  6 16:17:36 2020
@@ -1,4 +1,4 @@
-#	$NetBSD: mi,v 1.2324 2020/05/01 21:43:23 jdolecek Exp $
+#	$NetBSD: mi,v 1.2325 2020/05/06 16:17:36 nia Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 ./etc/mtree/set.comp				comp-sys-root
@@ -7273,6 +7273,7 @@
 ./usr/share/man/cat3/getdiskrawname.0		comp-c-catman		.cat
 ./usr/share/man/cat3/getdomainname.0		comp-c-catman		.cat
 ./usr/share/man/cat3/getdtablesize.0		comp-c-catman		.cat
+./usr/share/man/cat3/getentropy.0		comp-c-catman		.cat
 ./usr/share/man/cat3/getenv.0			comp-c-catman		.cat
 ./usr/share/man/cat3/getenv_r.0			comp-c-catman		.cat
 ./usr/share/man/cat3/getfsent.0			comp-c-catman		.cat
@@ -15316,6 +15317,7 @@
 ./usr/share/man/html3/getdiskrawname.html	comp-c-htmlman		html
 ./usr/share/man/html3/getdomainname.html	comp-c-htmlman		html
 ./usr/share/man/html3/getdtablesize.html	comp-c-htmlman		html
+./usr/share/man/html3/getentropy.html		comp-c-htmlman		html
 ./usr/share/man/html3/getenv.html		comp-c-htmlman		html
 ./usr/share/man/html3/getenv_r.html		comp-c-htmlman		html
 ./usr/share/man/html3/getfsent.html		comp-c-htmlman		html
@@ -23297,6 +23299,7 @@
 ./usr/share/man/man3/getdiskrawname.3		comp-c-man		.man
 ./usr/share/man/man3/getdomainname.3		comp-c-man		.man
 ./usr/share/man/man3/getdtablesize.3		comp-c-man		.man
+./usr/share/man/man3/getentropy.3		comp-c-man		.man
 ./usr/share/man/man3/getenv.3			comp-c-man		.man
 ./usr/share/man/man3/getenv_r.3			comp-c-man		.man
 ./usr/share/man/man3/getfsent.3			comp-c-man		.man

Index: src/include/unistd.h
diff -u src/include/unistd.h:1.156 src/include/unistd.h:1.157
--- src/include/unistd.h:1.156	Tue Mar 31 16:50:31 2020
+++ src/include/unistd.h	Wed May  6 16:17:36 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: unistd.h,v 1.156 2020/03/31 16:50:31 kamil Exp $	*/
+/*	$NetBSD: unistd.h,v 1.157 2020/05/06 16:17:36 nia Exp $	*/
 
 /*-
  * Copyright (c) 1998, 1999, 2008 The NetBSD Foundation, Inc.
@@ -338,6 +338,7 @@ int	 fchroot(int);
 int	 fdiscard(int, off_t, off_t);
 int	 fsync_range(int, int, off_t, off_t);
 int	 getdomainname(char *, size_t);
+int	 getentropy(void *, size_t);
 int	 getgrouplist(const char *, gid_t, gid_t *, int *);
 int	 getgroupmembership(const char *, gid_t, gid_t *, int, int *);
 mode_t	 getmode(const void *, mode_t);

Index: src/lib/libc/gen/Makefile.inc
diff -u src/lib/libc/gen/Makefile.inc:1.201 src/lib/libc/gen/Makefile.inc:1.202
--- src/lib/libc/gen/Makefile.inc:1.201	Wed Apr 22 23:32:25 2020
+++ src/lib/libc/gen/Makefile.inc	Wed May  6 16:17:36 2020
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile.inc,v 1.201 2020/04/22 23:32:25 joerg Exp $
+#	$NetBSD: Makefile.inc,v 1.202 2020/05/06 16:17:36 nia Exp $
 #	from: @(#)Makefile.inc	8.6 (Berkeley) 5/4/95
 
 # gen sources
@@ -15,7 +15,7 @@ SRCS+=	alarm.c alphasort.c arc4random.c 
 	errc.c errlist.c errno.c execl.c execle.c execlp.c execv.c execvp.c \
 	exect.c extattr.c fmtcheck.c fmtmsg.c fnmatch.c fstab.c ftok.c \
 	fts.c ftw.c getbsize.c getcap.c getcwd.c \
-	getdevmajor.c getdomainname.c getgrent.c \
+	getdevmajor.c getdomainname.c getentropy.c getgrent.c \
 	getgrouplist.c getgroupmembership.c gethostname.c \
 	getloadavg.c getlogin.c getmntinfo.c \
 	getnetgrent.c getpagesize.c \
@@ -60,7 +60,7 @@ MAN+=	alarm.3 arc4random.3 basename.3 bs
 	fmtcheck.3 fmtmsg.3 fnmatch.3 fpclassify.3 fpgetmask.3 \
 	ftok.3 fts.3 ftw.3 \
 	getbsize.3 cgetcap.3 getcwd.3 getdevmajor.3 \
-	getdomainname.3 getdiskbyname.3 getfsent.3 \
+	getdomainname.3 getdiskbyname.3 getentropy.3 getfsent.3 \
 	getgrent.3 getgrouplist.3 gethostname.3 getlastlogx.3 getloadavg.3 \
 	getmntinfo.3 getnetgrent.3 getpagesize.3 getpass.3 \
 	getprogname.3 getpwent.3 getttyent.3 getusershell.3 glob.3 \

Index: src/lib/libc/include/namespace.h
diff -u src/lib/libc/include/namespace.h:1.198 src/lib/libc/include/namespace.h:1.199
--- src/lib/libc/include/namespace.h:1.198	Sat Apr 18 23:55:50 2020
+++ src/lib/libc/include/namespace.h	Wed May  6 16:17:36 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: namespace.h,v 1.198 2020/04/18 23:55:50 thorpej Exp $	*/
+/*	$NetBSD: namespace.h,v 1.199 2020/05/06 16:17:36 nia Exp $	*/
 
 /*-
  * Copyright (c) 1997-2004 The NetBSD Foundation, Inc.
@@ -362,6 +362,7 @@
 #define getdevmajor		_getdevmajor
 #define getdiskbyname		_getdiskbyname
 #define getdomainname		_getdomainname
+#define getentropy		_getentropy
 #define getfsent		_getfsent
 #define getfsfile		_getfsfile
 #define getfsspec		_getfsspec

Added files:

Index: src/lib/libc/gen/getentropy.3
diff -u /dev/null src/lib/libc/gen/getentropy.3:1.1
--- /dev/null	Wed May  6 16:17:37 2020
+++ src/lib/libc/gen/getentropy.3	Wed May  6 16:17:36 2020
@@ -0,0 +1,93 @@
+.\"	$NetBSD: getentropy.3,v 1.1 2020/05/06 16:17:36 nia Exp $ $
+.\"
+.\" Copyright (c) 2020 The NetBSD Foundation, Inc.
+.\" All rights reserved.
+.\"
+.\" This code is derived from software contributed to The NetBSD Foundation
+.\" by Nia Alarie.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+.\" POSSIBILITY OF SUCH DAMAGE.
+.\"
+.Dd May 1, 2020
+.Dt GETENTROPY 3
+.Os
+.Sh NAME
+.Nm getentropy
+.Nd fill a buffer with high quality random data
+.Sh LIBRARY
+.Lb libc
+.Sh SYNOPSIS
+.In unistd.h
+.Ft int
+.Fn getentropy "void *buf" "size_t buflen"
+.Sh DESCRIPTION
+.Pp
+The
+.Fn getentropy
+function fills a buffer with high quality random data, suitable for seeding
+cryptographically secure psuedorandom number generators.
+.Pp
+.Fn getentropy
+is only intended for seeding random number generators and is not intended
+for use by regular code which simply needs secure random data.  For this
+purpose, please use
+.Xr arc4random 3 .
+.Pp
+The maximum value for
+.Li buflen
+is 256 bytes.
+.Sh IMPLEMENTATION NOTES
+.Fn getentropy
+reads from the
+.Xr sysctl 7
+variable
+.Li kern.arandom .
+.Sh RETURN VALUES
+The
+.Fn getentropy
+function returns 0 on success, and -1 if an error occurred.
+.Sh ERRORS
+.Fn getentropy
+will succeed unless:
+.Bl -tag -width Er
+.It Bq Er EFAULT
+The
+.Fa buf
+argument points to an invalid memory address.
+.It Bq Er EIO
+Too many bytes were requested.
+.Sh SEE ALSO
+.Xr arc4random 3 ,
+.Xr rnd 4
+.Sh STANDARDS
+The
+.Fn getentropy
+function is non-standard.
+.Sh HISTORY
+The
+.Fn getentropy
+function first appeared in
+.Ox 5.6 ,
+then in
+.Fx 12.0 ,
+and
+.Nx 10 .
Index: src/lib/libc/gen/getentropy.c
diff -u /dev/null src/lib/libc/gen/getentropy.c:1.1
--- /dev/null	Wed May  6 16:17:37 2020
+++ src/lib/libc/gen/getentropy.c	Wed May  6 16:17:36 2020
@@ -0,0 +1,64 @@
+/*	$NetBSD: getentropy.c,v 1.1 2020/05/06 16:17:36 nia Exp $	*/
+
+/*-
+ * Copyright (c) 2020 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Nia Alarie.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: getentropy.c,v 1.1 2020/05/06 16:17:36 nia Exp $");
+
+#include "namespace.h"
+
+#include <sys/param.h>
+#include <sys/sysctl.h>
+
+#include <errno.h>
+#include <unistd.h>
+
+#ifdef __weak_alias
+__weak_alias(getentropy,_getentropy)
+#endif
+
+int
+getentropy(void *buf, size_t buflen)
+{
+	size_t len = buflen;
+	int name[2] = { CTL_KERN, KERN_ARND };
+
+	if (buf == NULL && buflen > 0) {
+		errno = EFAULT;
+		return -1;
+	}
+
+	if (buflen > 256) {
+		errno = EIO;
+		return -1;
+	}
+
+	return sysctl(name, 2, buf, &len, NULL, 0);
+}

Reply via email to