]> Savannah Git Hosting - gnulib.git/commitdiff
getentropy, getrandom: new modules
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 25 May 2020 16:19:56 +0000 (09:19 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Mon, 25 May 2020 19:37:39 +0000 (12:37 -0700)
* MODULES.html.sh (func_all_modules):
* lib/unistd.in.h (getentropy, getrandom):
* m4/unistd_h.m4 (gl_UNISTD_H, gl_UNISTD_H_DEFAULTS):
* modules/unistd (unistd.h):
Add support for getentropy, getrandom.
* doc/glibc-functions/getentropy.texi (getentropy):
* doc/glibc-functions/getrandom.texi (getrandom):
These are now fixed on some platforms.
* lib/getentropy.c, lib/getrandom.c, lib/sys_random.in.h:
* m4/getentropy.m4, m4/getrandom.m4:
* modules/getentropy, modules/getentropy-tests:
* modules/getrandom, modules/getrandom-tests:
* tests/test-getentropy.c, tests/test-getrandom.c:
New files.

16 files changed:
ChangeLog
MODULES.html.sh
doc/glibc-functions/getentropy.texi
doc/glibc-functions/getrandom.texi
lib/getentropy.c [new file with mode: 0644]
lib/getrandom.c [new file with mode: 0644]
lib/sys_random.in.h [new file with mode: 0644]
lib/unistd.in.h
m4/getentropy.m4 [new file with mode: 0644]
m4/getrandom.m4 [new file with mode: 0644]
m4/unistd_h.m4
modules/getentropy [new file with mode: 0644]
modules/getentropy-tests [new file with mode: 0644]
modules/getrandom [new file with mode: 0644]
modules/unistd
tests/test-getentropy.c [new file with mode: 0644]

index 6b2faf17ef7e9d868d35e6888098b6ddd1e03c7e..b1acb99ca4288003ac06a3ce6ebc1cf20c87dc03 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,21 @@
+2020-05-25  Paul Eggert  <eggert@cs.ucla.edu>
+
+       getentropy, getrandom: new modules
+       * MODULES.html.sh (func_all_modules):
+       * lib/unistd.in.h (getentropy, getrandom):
+       * m4/unistd_h.m4 (gl_UNISTD_H, gl_UNISTD_H_DEFAULTS):
+       * modules/unistd (unistd.h):
+       Add support for getentropy, getrandom.
+       * doc/glibc-functions/getentropy.texi (getentropy):
+       * doc/glibc-functions/getrandom.texi (getrandom):
+       These are now fixed on some platforms.
+       * lib/getentropy.c, lib/getrandom.c, lib/sys_random.in.h:
+       * m4/getentropy.m4, m4/getrandom.m4:
+       * modules/getentropy, modules/getentropy-tests:
+       * modules/getrandom, modules/getrandom-tests:
+       * tests/test-getentropy.c, tests/test-getrandom.c:
+       New files.
+
 2020-05-25  Bruno Haible  <bruno@clisp.org>
 
        Add missing C99 dependencies.
index 318a15a1de961c68bae432d237ff2084d3d60f40..4db2a77ec5f318b52dee6efab9a8e32ebd23a825 100755 (executable)
@@ -3484,9 +3484,11 @@ func_all_modules ()
   func_module forkpty
   func_module getdomainname
   func_module xgetdomainname
+  func_module getentropy
   func_module getloadavg
   func_module getpagesize
   func_module getprogname
+  func_module getrandom
   func_module getusershell
   func_module lib-symbol-visibility
   func_module login_tty
index 5ad873a71ff750d00f6c11cc690b95ac2b0157db..5281dcd4ea9acd4233067eb337ac9eaf25ad1280 100644 (file)
@@ -15,15 +15,19 @@ Documentation:
 @uref{https://www.kernel.org/doc/man-pages/online/pages/man3/getentropy.3.html,,man getentropy}.
 @end itemize
 
-Gnulib module: ---
+Gnulib module: getentropy
 
 Portability problems fixed by Gnulib:
 @itemize
+@item
+This function is missing on some platforms:
+glibc 2.24, Mac OS X 10.5, FreeBSD 11.0, NetBSD 5.0, OpenBSD 3.8,
+Solaris 11.0, Android 9.0.
 @end itemize
 
 Portability problems not fixed by Gnulib:
 @itemize
 @item
-This function is missing on many non-glibc platforms:
-glibc 2.24, Mac OS X 10.5, FreeBSD 11.0, NetBSD 5.0, OpenBSD 3.8, Minix 3.1.8, AIX 7.1, HP-UX 11.31, IRIX 6.5, Solaris 11.0, Cygwin, mingw, MSVC 14, Android 9.0.
+This function is missing on some platforms:
+Minix 3.1.8, IRIX 6.5, mingw, MSVC 14.
 @end itemize
index 25e899d63751b8ff69f69837f9d3427b9a96ec0d..2f4debcfefb55a2a85e5db94c86542289c95a0d0 100644 (file)
@@ -19,11 +19,15 @@ Gnulib module: ---
 
 Portability problems fixed by Gnulib:
 @itemize
+@item
+This function is missing on some platforms:
+glibc 2.24, Mac OS X 10.5, FreeBSD 11.0, NetBSD 5.0, OpenBSD 3.8,
+Solaris 11.0, Android 9.0.
 @end itemize
 
 Portability problems not fixed by Gnulib:
 @itemize
 @item
-This function is missing on many non-glibc platforms:
-glibc 2.24, Mac OS X 10.5, FreeBSD 11.0, NetBSD 5.0, OpenBSD 3.8, Minix 3.1.8, AIX 7.1, HP-UX 11.31, IRIX 6.5, Solaris 11.0, Cygwin, mingw, MSVC 14, Android 9.0.
+This function is missing on some platforms:
+Minix 3.1.8, IRIX 6.5, mingw, MSVC 14.
 @end itemize
diff --git a/lib/getentropy.c b/lib/getentropy.c
new file mode 100644 (file)
index 0000000..2b6c3f5
--- /dev/null
@@ -0,0 +1,51 @@
+/* Fill a buffer with random bytes.
+   Copyright 2020 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
+
+/* Written by Paul Eggert.  */
+
+#include <config.h>
+
+#include <unistd.h>
+
+#include <errno.h>
+#include <sys/random.h>
+
+/* Overwrite BUFFER with random data.  BUFFER contains LENGTH bytes.
+   LENGTH must be at most 256.  Return 0 on success, -1 (setting
+   errno) on failure.  */
+int
+getentropy (void *buffer, size_t length)
+{
+  char *buf = buffer;
+
+  if (length <= 256)
+    for (;;)
+      {
+        ssize_t bytes;
+        if (length == 0)
+          return 0;
+        while ((bytes = getrandom (buf, length, 0)) < 0)
+          if (errno != EINTR)
+            return -1;
+        if (bytes == 0)
+          break;
+        buf += bytes;
+        length -= bytes;
+      }
+
+  errno = EIO;
+  return -1;
+}
diff --git a/lib/getrandom.c b/lib/getrandom.c
new file mode 100644 (file)
index 0000000..9b6ecb4
--- /dev/null
@@ -0,0 +1,52 @@
+/* Obtain a series of random bytes.
+
+   Copyright 2020 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
+
+/* Written by Paul Eggert.  */
+
+#include <config.h>
+
+#include <sys/random.h>
+
+#include "minmax.h"
+#include <fcntl.h>
+#include <stdbool.h>
+#include <unistd.h>
+
+/* Set BUFFER (of size LENGTH) to random bytes under the control of FLAGS.
+   Return the number of bytes written, or -1 on error.  */
+ssize_t
+getrandom (void *buffer, size_t length, unsigned int flags)
+{
+  static int randfd[2] = { -1, -1 };
+  bool devrandom = (flags & GRND_RANDOM) != 0;
+  int fd = randfd[devrandom];
+
+  if (fd < 0)
+    {
+      static char const randdevice[][MAX (sizeof NAME_OF_NONCE_DEVICE,
+                                          sizeof NAME_OF_RANDOM_DEVICE)]
+        = { NAME_OF_NONCE_DEVICE, NAME_OF_RANDOM_DEVICE };
+      int oflags = (O_RDONLY + O_CLOEXEC
+                    + (flags & GRND_NONBLOCK ? O_NONBLOCK : 0));
+      fd = open (randdevice[devrandom], oflags);
+      if (fd < 0)
+        return fd;
+      randfd[devrandom] = fd;
+    }
+
+  return read (fd, buffer, length);
+}
diff --git a/lib/sys_random.in.h b/lib/sys_random.in.h
new file mode 100644 (file)
index 0000000..e3f308c
--- /dev/null
@@ -0,0 +1,9 @@
+#ifndef _SYS_RANDOM_H
+#define _SYS_RANDOM_H 1
+
+#include <sys/types.h>
+#define GRND_NONBLOCK 1
+#define GRND_RANDOM 2
+ssize_t getrandom (void *, size_t, unsigned int);
+
+#endif
index 1454d7e8582459679b365b3f197742ea66e752a3..4a0d6655af8cd1cc3cad356e7a9ac5639021ec9d 100644 (file)
@@ -763,6 +763,22 @@ _GL_WARN_ON_USE (getdtablesize, "getdtablesize is unportable - "
 #endif
 
 
+#if @GNULIB_GETENTROPY@
+/* Fill a buffer with random bytes.  */
+# if !@HAVE_GETENTROPY@
+_GL_FUNCDECL_SYS (getentropy, int, (void *buffer, size_t length));
+# endif
+_GL_CXXALIAS_SYS (getentropy, int, (void *buffer, size_t length));
+_GL_CXXALIASWARN (getentropy);
+#elif defined GNULIB_POSIXCHECK
+# undef getentropy
+# if HAVE_RAW_DECL_GETENTROPY
+_GL_WARN_ON_USE (getentropy, "getentropy is unportable - "
+                 "use gnulib module getentropy for portability");
+# endif
+#endif
+
+
 #if @GNULIB_GETGROUPS@
 /* Return the supplemental groups that the current process belongs to.
    It is unspecified whether the effective group id is in the list.
@@ -1014,6 +1030,24 @@ _GL_WARN_ON_USE (getpass, "getpass is unportable - "
 #endif
 
 
+#if @GNULIB_GETRANDOM@
+/* Fill a buffer with random bytes.  */
+# if !@HAVE_GETRANDOM@
+_GL_FUNCDECL_SYS (getrandom, int, (void *buffer, size_t length,
+                                   unsigned int flags));
+# endif
+_GL_CXXALIAS_SYS (getrandom, int, (void *buffer, size_t length,
+                                   unsigned int flags));
+_GL_CXXALIASWARN (getrandom);
+#elif defined GNULIB_POSIXCHECK
+# undef getrandom
+# if HAVE_RAW_DECL_GETRANDOM
+_GL_WARN_ON_USE (getrandom, "getrandom is unportable - "
+                 "use gnulib module getrandom for portability");
+# endif
+#endif
+
+
 #if @GNULIB_GETUSERSHELL@
 /* Return the next valid login shell on the system, or NULL when the end of
    the list has been reached.  */
diff --git a/m4/getentropy.m4 b/m4/getentropy.m4
new file mode 100644 (file)
index 0000000..596544b
--- /dev/null
@@ -0,0 +1,16 @@
+# getentropy.m4
+dnl Copyright 2020 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+dnl Written by Paul Eggert.
+
+AC_DEFUN([gl_FUNC_GETENTROPY],
+[
+  AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
+  AC_CHECK_FUNCS_ONCE([getentropy])
+  if test $ac_cv_func_getentropy = no; then
+    HAVE_GETENTROPY=0
+  fi
+])
diff --git a/m4/getrandom.m4 b/m4/getrandom.m4
new file mode 100644 (file)
index 0000000..ecb44ae
--- /dev/null
@@ -0,0 +1,15 @@
+# getrandom.m4
+dnl Copyright 2020 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+dnl Written by Paul Eggert.
+
+AC_DEFUN([gl_FUNC_GETRANDOM],
+[
+  AC_CHECK_FUNCS_ONCE([getrandom])
+  if test "$ac_cv_func_getrandom" != yes; then
+    HAVE_GETRANDOM=0
+  fi
+])
index e776f3bd445b68d4a29d878bc748dad7e1e12398..1f39d53e1fa8c391554cb3fa5a695e41aeef67ca 100644 (file)
@@ -43,8 +43,8 @@ AC_DEFUN([gl_UNISTD_H],
 #endif
     ]], [access chdir chown dup dup2 dup3 environ euidaccess faccessat fchdir
     fchownat fdatasync fsync ftruncate getcwd getdomainname getdtablesize
-    getgroups gethostname getlogin getlogin_r getpagesize getpass
-    getusershell setusershell endusershell
+    getentropy getgroups gethostname getlogin getlogin_r getpagesize getpass
+    getrandom getusershell setusershell endusershell
     group_member isatty lchown link linkat lseek pipe pipe2 pread pwrite
     readlink readlinkat rmdir sethostname sleep symlink symlinkat
     truncate ttyname_r unlink unlinkat usleep])
@@ -82,6 +82,7 @@ AC_DEFUN([gl_UNISTD_H_DEFAULTS],
   GNULIB_GETCWD=0;               AC_SUBST([GNULIB_GETCWD])
   GNULIB_GETDOMAINNAME=0;        AC_SUBST([GNULIB_GETDOMAINNAME])
   GNULIB_GETDTABLESIZE=0;        AC_SUBST([GNULIB_GETDTABLESIZE])
+  GNULIB_GETENTROPY=0;           AC_SUBST([GNULIB_GETENTROPY])
   GNULIB_GETGROUPS=0;            AC_SUBST([GNULIB_GETGROUPS])
   GNULIB_GETHOSTNAME=0;          AC_SUBST([GNULIB_GETHOSTNAME])
   GNULIB_GETLOGIN=0;             AC_SUBST([GNULIB_GETLOGIN])
@@ -89,6 +90,7 @@ AC_DEFUN([gl_UNISTD_H_DEFAULTS],
   GNULIB_GETOPT_POSIX=0;         AC_SUBST([GNULIB_GETOPT_POSIX])
   GNULIB_GETPAGESIZE=0;          AC_SUBST([GNULIB_GETPAGESIZE])
   GNULIB_GETPASS=0;              AC_SUBST([GNULIB_GETPASS])
+  GNULIB_GETRANDOM=0;            AC_SUBST([GNULIB_GETRANDOM])
   GNULIB_GETUSERSHELL=0;         AC_SUBST([GNULIB_GETUSERSHELL])
   GNULIB_GROUP_MEMBER=0;         AC_SUBST([GNULIB_GROUP_MEMBER])
   GNULIB_ISATTY=0;               AC_SUBST([GNULIB_ISATTY])
@@ -129,11 +131,13 @@ AC_DEFUN([gl_UNISTD_H_DEFAULTS],
   HAVE_FSYNC=1;           AC_SUBST([HAVE_FSYNC])
   HAVE_FTRUNCATE=1;       AC_SUBST([HAVE_FTRUNCATE])
   HAVE_GETDTABLESIZE=1;   AC_SUBST([HAVE_GETDTABLESIZE])
+  HAVE_GETENTROPY=1;      AC_SUBST([HAVE_GETENTROPY])
   HAVE_GETGROUPS=1;       AC_SUBST([HAVE_GETGROUPS])
   HAVE_GETHOSTNAME=1;     AC_SUBST([HAVE_GETHOSTNAME])
   HAVE_GETLOGIN=1;        AC_SUBST([HAVE_GETLOGIN])
   HAVE_GETPAGESIZE=1;     AC_SUBST([HAVE_GETPAGESIZE])
   HAVE_GETPASS=1;         AC_SUBST([HAVE_GETPASS])
+  HAVE_GETRANDOM=1;       AC_SUBST([HAVE_GETRANDOM])
   HAVE_GROUP_MEMBER=1;    AC_SUBST([HAVE_GROUP_MEMBER])
   HAVE_LCHOWN=1;          AC_SUBST([HAVE_LCHOWN])
   HAVE_LINK=1;            AC_SUBST([HAVE_LINK])
diff --git a/modules/getentropy b/modules/getentropy
new file mode 100644 (file)
index 0000000..9696c1e
--- /dev/null
@@ -0,0 +1,29 @@
+Description:
+Fill buffer with random bytes.
+
+Files:
+lib/getentropy.c
+m4/getentropy.m4
+
+Depends-on:
+getrandom      [test $HAVE_GETENTROPY = 0]
+extensions
+unistd
+
+configure.ac:
+gl_FUNC_GETENTROPY
+if test $HAVE_GETENTROPY = 0; then
+  AC_LIBOBJ([getentropy])
+fi
+gl_UNISTD_MODULE_INDICATOR([getentropy])
+
+Makefile.am:
+
+Include:
+<unistd.h>
+
+License:
+LGPL
+
+Maintainer:
+all
diff --git a/modules/getentropy-tests b/modules/getentropy-tests
new file mode 100644 (file)
index 0000000..b5c4b11
--- /dev/null
@@ -0,0 +1,12 @@
+Files:
+tests/test-getentropy.c
+tests/signature.h
+tests/macros.h
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-getentropy
+check_PROGRAMS += test-getentropy
diff --git a/modules/getrandom b/modules/getrandom
new file mode 100644 (file)
index 0000000..2ae374f
--- /dev/null
@@ -0,0 +1,44 @@
+Description:
+Fill buffer with random bytes.
+
+Files:
+lib/getrandom.c
+lib/sys_random.in.h
+m4/getrandom.m4
+
+Depends-on:
+crypto/gc-random    [test $HAVE_GETRANDOM = 0]
+fcntl-h             [test $HAVE_GETRANDOM = 0]
+minmax              [test $HAVE_GETRANDOM = 0]
+open                [test $HAVE_GETRANDOM = 0]
+
+configure.ac:
+gl_FUNC_GETRANDOM
+if test $HAVE_GETRANDOM = 0; then
+  AC_LIBOBJ([getrandom])
+fi
+gl_UNISTD_MODULE_INDICATOR([getrandom])
+
+Makefile.am:
+BUILT_SOURCES += sys/random.h
+
+# We need the following in order to create <sys/random.h> when the system
+# doesn't have one that works with the given compiler.
+sys/random.h: sys_random.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(WARN_ON_USE_H) $(ARG_NONNULL_H)
+       $(AM_V_at)$(MKDIR_P) sys
+       $(AM_V_GEN)rm -f $@-t $@ && \
+       { echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
+         cat $(srcdir)/sys_random.in.h; \
+       } > $@-t && \
+       mv -f $@-t $@
+MOSTLYCLEANFILES += sys/random.h sys/random.h-t
+MOSTLYCLEANDIRS += sys
+
+Include:
+<sys/random.h>
+
+License:
+LGPL
+
+Maintainer:
+all
index 3c28f36ca5f44494faac10f6cb3f6ea3f15ab269..dc32ce056e849aabe13abd33b34878c5e1a50fd1 100644 (file)
@@ -55,6 +55,7 @@ unistd.h: unistd.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H
              -e 's/@''GNULIB_GETCWD''@/$(GNULIB_GETCWD)/g' \
              -e 's/@''GNULIB_GETDOMAINNAME''@/$(GNULIB_GETDOMAINNAME)/g' \
              -e 's/@''GNULIB_GETDTABLESIZE''@/$(GNULIB_GETDTABLESIZE)/g' \
+             -e 's/@''GNULIB_GETENTROPY''@/$(GNULIB_GETENTROPY)/g' \
              -e 's/@''GNULIB_GETGROUPS''@/$(GNULIB_GETGROUPS)/g' \
              -e 's/@''GNULIB_GETHOSTNAME''@/$(GNULIB_GETHOSTNAME)/g' \
              -e 's/@''GNULIB_GETLOGIN''@/$(GNULIB_GETLOGIN)/g' \
@@ -62,6 +63,7 @@ unistd.h: unistd.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H
              -e 's/@''GNULIB_GETOPT_POSIX''@/$(GNULIB_GETOPT_POSIX)/g' \
              -e 's/@''GNULIB_GETPAGESIZE''@/$(GNULIB_GETPAGESIZE)/g' \
              -e 's/@''GNULIB_GETPASS''@/$(GNULIB_GETPASS)/g' \
+             -e 's/@''GNULIB_GETRANDOM''@/$(GNULIB_GETRANDOM)/g' \
              -e 's/@''GNULIB_GETUSERSHELL''@/$(GNULIB_GETUSERSHELL)/g' \
              -e 's/@''GNULIB_GROUP_MEMBER''@/$(GNULIB_GROUP_MEMBER)/g' \
              -e 's/@''GNULIB_ISATTY''@/$(GNULIB_ISATTY)/g' \
@@ -103,10 +105,12 @@ unistd.h: unistd.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H
              -e 's|@''HAVE_FSYNC''@|$(HAVE_FSYNC)|g' \
              -e 's|@''HAVE_FTRUNCATE''@|$(HAVE_FTRUNCATE)|g' \
              -e 's|@''HAVE_GETDTABLESIZE''@|$(HAVE_GETDTABLESIZE)|g' \
+             -e 's|@''HAVE_GETENTROPY''@|$(HAVE_GETENTROPY)|g' \
              -e 's|@''HAVE_GETGROUPS''@|$(HAVE_GETGROUPS)|g' \
              -e 's|@''HAVE_GETHOSTNAME''@|$(HAVE_GETHOSTNAME)|g' \
              -e 's|@''HAVE_GETPAGESIZE''@|$(HAVE_GETPAGESIZE)|g' \
              -e 's|@''HAVE_GETPASS''@|$(HAVE_GETPASS)|g' \
+             -e 's|@''HAVE_GETRANDOM''@|$(HAVE_GETRANDOM)|g' \
              -e 's|@''HAVE_GROUP_MEMBER''@|$(HAVE_GROUP_MEMBER)|g' \
              -e 's|@''HAVE_LCHOWN''@|$(HAVE_LCHOWN)|g' \
              -e 's|@''HAVE_LINK''@|$(HAVE_LINK)|g' \
diff --git a/tests/test-getentropy.c b/tests/test-getentropy.c
new file mode 100644 (file)
index 0000000..845c5dc
--- /dev/null
@@ -0,0 +1,43 @@
+/* Test the getentropy function.
+   Copyright 2020 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
+
+/* Written by Paul Eggert.  */
+
+#include <config.h>
+
+#include <unistd.h>
+
+#include <string.h>
+
+#include "signature.h"
+SIGNATURE_CHECK (getentropy, int, (void *, size_t));
+
+#include "macros.h"
+
+char empty_buf[256];
+char buf[256];
+
+int
+main (int argc, char *argv[])
+{
+  /* If this test fails, there's something wrong with the setup anyway.  */
+  ASSERT (getentropy (buf, sizeof buf) == 0);
+
+  /* This test fails with probability 2**-2048.  (Run it again if so. :-) */
+  ASSERT (memcmp (buf, empty_buf, sizeof buf) != 0);
+
+  return 0;
+}