]> git.ipfire.org Git - thirdparty/unbound.git/commitdiff
- arc4random in compat/ and getentropy, explicit_bzero, chacha for
authorWouter Wijngaards <wouter@nlnetlabs.nl>
Fri, 11 Jul 2014 09:27:24 +0000 (09:27 +0000)
committerWouter Wijngaards <wouter@nlnetlabs.nl>
Fri, 11 Jul 2014 09:27:24 +0000 (09:27 +0000)
  dependencies, from OpenBSD.  arc4_lock and sha512 in compat.
  This makes arc4random available on all platforms, except when
  compiled with LIBNSS (it uses libNSS crypto random).

git-svn-id: file:///svn/unbound/trunk@3158 be551aaa-1e26-0410-a405-d3ace91eadb9

16 files changed:
Makefile.in
compat/arc4_lock.c [new file with mode: 0644]
compat/arc4random.c [new file with mode: 0644]
compat/arc4random_uniform.c [new file with mode: 0644]
compat/chacha_private.h [new file with mode: 0644]
compat/explicit_bzero.c [new file with mode: 0644]
compat/getentropy_linux.c [new file with mode: 0644]
compat/getentropy_osx.c [new file with mode: 0644]
compat/getentropy_solaris.c [new file with mode: 0644]
compat/sha512.c [new file with mode: 0644]
config.h.in
configure
configure.ac
doc/Changelog
smallapp/unbound-anchor.c
util/random.c

index a168e2ff1ad99400fee92d71192b0e39904af9f1..185db6d0eaae32d81d4caf767d15d3675f038d08 100644 (file)
@@ -55,6 +55,7 @@ LIBS=@LIBS@
 LIBOBJS=@LIBOBJS@
 # filter out ctime_r from compat obj.
 LIBOBJ_WITHOUT_CTIME=@LIBOBJ_WITHOUT_CTIME@
+LIBOBJ_WITHOUT_CTIMEARC4=@LIBOBJ_WITHOUT_CTIMEARC4@
 RUNTIME_PATH=@RUNTIME_PATH@
 DEPFLAG=@DEPFLAG@
 DATE=@CONFIG_DATE@
@@ -123,9 +124,13 @@ COMMON_OBJ_ALL_SYMBOLS=@COMMON_OBJ_ALL_SYMBOLS@
 COMPAT_SRC=compat/ctime_r.c compat/fake-rfc2553.c compat/gmtime_r.c \
 compat/inet_aton.c compat/inet_ntop.c compat/inet_pton.c compat/malloc.c \
 compat/memcmp.c compat/memmove.c compat/snprintf.c compat/strlcat.c \
-compat/strlcpy.c compat/strptime.c
+compat/strlcpy.c compat/strptime.c compat/getentropy_linux.c \
+compat/getentropy_osx.c compat/getentropy_solaris.c compat/explicit_bzero.c \
+compat/arc4random.c compat/arc4random_uniform.c compat/arc4_lock.c \
+compat/sha512.c
 COMPAT_OBJ=$(LIBOBJS:.o=.lo)
 COMPAT_OBJ_WITHOUT_CTIME=$(LIBOBJ_WITHOUT_CTIME:.o=.lo)
+COMPAT_OBJ_WITHOUT_CTIMEARC4=$(LIBOBJ_WITHOUT_CTIMEARC4:.o=.lo)
 SLDNS_SRC=ldns/keyraw.c ldns/sbuffer.c ldns/wire2str.c ldns/parse.c \
 ldns/parseutil.c ldns/rrdef.c ldns/str2wire.c
 SLDNS_OBJ=keyraw.lo sbuffer.lo wire2str.lo parse.lo parseutil.lo rrdef.lo \
@@ -155,10 +160,10 @@ CONTROL_OBJ_LINK=$(CONTROL_OBJ) worker_cb.lo $(COMMON_OBJ_ALL_SYMBOLS) \
 $(SLDNS_OBJ) $(COMPAT_OBJ) @WIN_CONTROL_OBJ_LINK@
 HOST_SRC=smallapp/unbound-host.c
 HOST_OBJ=unbound-host.lo
-HOST_OBJ_LINK=$(HOST_OBJ) $(SLDNS_OBJ) $(COMPAT_OBJ_WITHOUT_CTIME) @WIN_HOST_OBJ_LINK@
+HOST_OBJ_LINK=$(HOST_OBJ) $(SLDNS_OBJ) $(COMPAT_OBJ_WITHOUT_CTIMEARC4) @WIN_HOST_OBJ_LINK@
 UBANCHOR_SRC=smallapp/unbound-anchor.c
 UBANCHOR_OBJ=unbound-anchor.lo
-UBANCHOR_OBJ_LINK=$(UBANCHOR_OBJ) \
+UBANCHOR_OBJ_LINK=$(UBANCHOR_OBJ) log.lo locks.lo \
 $(COMPAT_OBJ_WITHOUT_CTIME) @WIN_UBANCHOR_OBJ_LINK@
 TESTBOUND_SRC=testcode/testbound.c testcode/testpkts.c \
 daemon/worker.c daemon/acl_list.c daemon/daemon.c daemon/stats.c \
@@ -172,7 +177,7 @@ LOCKVERIFY_OBJ_LINK=$(LOCKVERIFY_OBJ) worker_cb.lo $(COMMON_OBJ) $(COMPAT_OBJ) \
 $(SLDNS_OBJ)
 PETAL_SRC=testcode/petal.c
 PETAL_OBJ=petal.lo
-PETAL_OBJ_LINK=$(PETAL_OBJ) $(COMPAT_OBJ_WITHOUT_CTIME)
+PETAL_OBJ_LINK=$(PETAL_OBJ) $(COMPAT_OBJ_WITHOUT_CTIMEARC4)
 PKTVIEW_SRC=testcode/pktview.c testcode/readhex.c
 PKTVIEW_OBJ=pktview.lo
 PKTVIEW_OBJ_LINK=$(PKTVIEW_OBJ) worker_cb.lo readhex.lo $(COMMON_OBJ) \
@@ -1165,3 +1170,11 @@ snprintf.lo snprintf.o: $(srcdir)/compat/snprintf.c config.h
 strlcat.lo strlcat.o: $(srcdir)/compat/strlcat.c config.h
 strlcpy.lo strlcpy.o: $(srcdir)/compat/strlcpy.c config.h
 strptime.lo strptime.o: $(srcdir)/compat/strptime.c config.h
+getentropy_linux.lo getentropy_linux.o: $(srcdir)/compat/getentropy_linux.c
+getentropy_osx.lo getentropy_osx.o: $(srcdir)/compat/getentropy_osx.c
+getentropy_solaris.lo getentropy_solaris.o: $(srcdir)/compat/getentropy_solaris.c
+explicit_bzero.lo explicit_bzero.o: $(srcdir)/compat/explicit_bzero.c
+arc4random.lo arc4random.o: $(srcdir)/compat/arc4random.c $(srcdir)/compat/chacha_private.h
+arc4random_uniform.lo arc4random_uniform.o: $(srcdir)/compat/arc4random_uniform.c $(srcdir)/compat/chacha_private.h
+arc4_lock.lo arc4_lock.o: $(srcdir)/compat/arc4_lock.c
+sha512.lo sha512.o: $(srcdir)/compat/sha512.c
diff --git a/compat/arc4_lock.c b/compat/arc4_lock.c
new file mode 100644 (file)
index 0000000..c30bcfd
--- /dev/null
@@ -0,0 +1,64 @@
+/* arc4_lock.c - global lock for arc4random
+*
+ * Copyright (c) 2014, NLnet Labs. All rights reserved.
+ *
+ * This software is open source.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 
+ * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 
+ * 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.
+ * 
+ * Neither the name of the NLNET LABS nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT
+ * HOLDER 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 "config.h"
+#include "util/locks.h"
+
+void _ARC4_LOCK(void);
+void _ARC4_UNLOCK(void);
+
+#ifdef THREADS_DISABLED
+void _ARC4_LOCK(void)
+{
+}
+
+void _ARC4_UNLOCK(void)
+{
+}
+#else /* !THREADS_DISABLED */
+
+static lock_quick_t arc4lock;
+static int arc4lockinit = 0;
+
+void _ARC4_LOCK(void)
+{
+       if(!arc4lockinit)
+               lock_quick_init(&arc4lock);
+       lock_quick_lock(&arc4lock);
+}
+
+void _ARC4_UNLOCK(void)
+{
+       lock_quick_unlock(&arc4lock);
+}
+#endif /* THREADS_DISABLED */
diff --git a/compat/arc4random.c b/compat/arc4random.c
new file mode 100644 (file)
index 0000000..13b94ed
--- /dev/null
@@ -0,0 +1,249 @@
+/*     $OpenBSD: arc4random.c,v 1.40 2014/07/09 16:52:09 bcook Exp $   */
+
+/*
+ * Copyright (c) 1996, David Mazieres <dm@uun.org>
+ * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
+ * Copyright (c) 2013, Markus Friedl <markus@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+/*
+ * ChaCha based random number generator for OpenBSD.
+ */
+
+#include <fcntl.h>
+#include <limits.h>
+#include <signal.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/time.h>
+#include <sys/mman.h>
+
+#include "thread_private.h"
+
+#define KEYSTREAM_ONLY
+#include "chacha_private.h"
+
+#define min(a, b) ((a) < (b) ? (a) : (b))
+#ifdef __GNUC__
+#define inline __inline
+#else                          /* !__GNUC__ */
+#define inline
+#endif                         /* !__GNUC__ */
+
+#define KEYSZ  32
+#define IVSZ   8
+#define BLOCKSZ        64
+#define RSBUFSZ        (16*BLOCKSZ)
+
+/* Marked MAP_INHERIT_ZERO, so zero'd out in fork children. */
+static struct {
+       size_t          rs_have;        /* valid bytes at end of rs_buf */
+       size_t          rs_count;       /* bytes till reseed */
+} *rs;
+
+/* Preserved in fork children. */
+static struct {
+       chacha_ctx      rs_chacha;      /* chacha context for random keystream */
+       u_char          rs_buf[RSBUFSZ];        /* keystream blocks */
+} *rsx;
+
+static inline void _rs_rekey(u_char *dat, size_t datlen);
+
+static inline void
+_rs_init(u_char *buf, size_t n)
+{
+       if (n < KEYSZ + IVSZ)
+               return;
+
+       if (rs == NULL) {
+               if ((rs = mmap(NULL, sizeof(*rs), PROT_READ|PROT_WRITE,
+                   MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED)
+                       abort();
+#ifdef MAP_INHERIT_ZERO
+               if (minherit(rs, sizeof(*rs), MAP_INHERIT_ZERO) == -1)
+                       abort();
+#endif
+       }
+       if (rsx == NULL) {
+               if ((rsx = mmap(NULL, sizeof(*rsx), PROT_READ|PROT_WRITE,
+                   MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED)
+                       abort();
+       }
+
+       chacha_keysetup(&rsx->rs_chacha, buf, KEYSZ * 8, 0);
+       chacha_ivsetup(&rsx->rs_chacha, buf + KEYSZ);
+}
+
+static void
+_rs_stir(void)
+{
+       u_char rnd[KEYSZ + IVSZ];
+
+       if (getentropy(rnd, sizeof rnd) == -1)
+               raise(SIGKILL);
+
+       if (!rs)
+               _rs_init(rnd, sizeof(rnd));
+       else
+               _rs_rekey(rnd, sizeof(rnd));
+       explicit_bzero(rnd, sizeof(rnd));       /* discard source seed */
+
+       /* invalidate rs_buf */
+       rs->rs_have = 0;
+       memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf));
+
+       rs->rs_count = 1600000;
+}
+
+static inline void
+_rs_stir_if_needed(size_t len)
+{
+#ifndef MAP_INHERIT_ZERO
+       static pid_t _rs_pid = 0;
+       pid_t pid = getpid();
+
+       /* If a system lacks MAP_INHERIT_ZERO, resort to getpid() */
+       if (_rs_pid == 0 || _rs_pid != pid) {
+               _rs_pid = pid;
+               if (rs)
+                       rs->rs_count = 0;
+       }
+#endif
+       if (!rs || rs->rs_count <= len)
+               _rs_stir();
+       if (rs->rs_count <= len)
+               rs->rs_count = 0;
+       else
+               rs->rs_count -= len;
+}
+
+static inline void
+_rs_rekey(u_char *dat, size_t datlen)
+{
+#ifndef KEYSTREAM_ONLY
+       memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf));
+#endif
+       /* fill rs_buf with the keystream */
+       chacha_encrypt_bytes(&rsx->rs_chacha, rsx->rs_buf,
+           rsx->rs_buf, sizeof(rsx->rs_buf));
+       /* mix in optional user provided data */
+       if (dat) {
+               size_t i, m;
+
+               m = min(datlen, KEYSZ + IVSZ);
+               for (i = 0; i < m; i++)
+                       rsx->rs_buf[i] ^= dat[i];
+       }
+       /* immediately reinit for backtracking resistance */
+       _rs_init(rsx->rs_buf, KEYSZ + IVSZ);
+       memset(rsx->rs_buf, 0, KEYSZ + IVSZ);
+       rs->rs_have = sizeof(rsx->rs_buf) - KEYSZ - IVSZ;
+}
+
+static inline void
+_rs_random_buf(void *_buf, size_t n)
+{
+       u_char *buf = (u_char *)_buf;
+       u_char *keystream;
+       size_t m;
+
+       _rs_stir_if_needed(n);
+       while (n > 0) {
+               if (rs->rs_have > 0) {
+                       m = min(n, rs->rs_have);
+                       keystream = rsx->rs_buf + sizeof(rsx->rs_buf)
+                           - rs->rs_have;
+                       memcpy(buf, keystream, m);
+                       memset(keystream, 0, m);
+                       buf += m;
+                       n -= m;
+                       rs->rs_have -= m;
+               }
+               if (rs->rs_have == 0)
+                       _rs_rekey(NULL, 0);
+       }
+}
+
+static inline void
+_rs_random_u32(uint32_t *val)
+{
+       u_char *keystream;
+       _rs_stir_if_needed(sizeof(*val));
+       if (rs->rs_have < sizeof(*val))
+               _rs_rekey(NULL, 0);
+       keystream = rsx->rs_buf + sizeof(rsx->rs_buf) - rs->rs_have;
+       memcpy(val, keystream, sizeof(*val));
+       memset(keystream, 0, sizeof(*val));
+       rs->rs_have -= sizeof(*val);
+}
+
+uint32_t
+arc4random(void)
+{
+       uint32_t val;
+
+       _ARC4_LOCK();
+       _rs_random_u32(&val);
+       _ARC4_UNLOCK();
+       return val;
+}
+
+void
+arc4random_buf(void *buf, size_t n)
+{
+       _ARC4_LOCK();
+       _rs_random_buf(buf, n);
+       _ARC4_UNLOCK();
+}
+
+/*
+ * Calculate a uniformly distributed random number less than upper_bound
+ * avoiding "modulo bias".
+ *
+ * Uniformity is achieved by generating new random numbers until the one
+ * returned is outside the range [0, 2**32 % upper_bound).  This
+ * guarantees the selected random number will be inside
+ * [2**32 % upper_bound, 2**32) which maps back to [0, upper_bound)
+ * after reduction modulo upper_bound.
+ */
+uint32_t
+arc4random_uniform(uint32_t upper_bound)
+{
+       uint32_t r, min;
+
+       if (upper_bound < 2)
+               return 0;
+
+       /* 2**32 % x == (2**32 - x) % x */
+       min = -upper_bound % upper_bound;
+
+       /*
+        * This could theoretically loop forever but each retry has
+        * p > 0.5 (worst case, usually far better) of selecting a
+        * number inside the range we need, so it should rarely need
+        * to re-roll.
+        */
+       for (;;) {
+               r = arc4random();
+               if (r >= min)
+                       break;
+       }
+
+       return r % upper_bound;
+}
diff --git a/compat/arc4random_uniform.c b/compat/arc4random_uniform.c
new file mode 100644 (file)
index 0000000..13b94ed
--- /dev/null
@@ -0,0 +1,249 @@
+/*     $OpenBSD: arc4random.c,v 1.40 2014/07/09 16:52:09 bcook Exp $   */
+
+/*
+ * Copyright (c) 1996, David Mazieres <dm@uun.org>
+ * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
+ * Copyright (c) 2013, Markus Friedl <markus@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+/*
+ * ChaCha based random number generator for OpenBSD.
+ */
+
+#include <fcntl.h>
+#include <limits.h>
+#include <signal.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/time.h>
+#include <sys/mman.h>
+
+#include "thread_private.h"
+
+#define KEYSTREAM_ONLY
+#include "chacha_private.h"
+
+#define min(a, b) ((a) < (b) ? (a) : (b))
+#ifdef __GNUC__
+#define inline __inline
+#else                          /* !__GNUC__ */
+#define inline
+#endif                         /* !__GNUC__ */
+
+#define KEYSZ  32
+#define IVSZ   8
+#define BLOCKSZ        64
+#define RSBUFSZ        (16*BLOCKSZ)
+
+/* Marked MAP_INHERIT_ZERO, so zero'd out in fork children. */
+static struct {
+       size_t          rs_have;        /* valid bytes at end of rs_buf */
+       size_t          rs_count;       /* bytes till reseed */
+} *rs;
+
+/* Preserved in fork children. */
+static struct {
+       chacha_ctx      rs_chacha;      /* chacha context for random keystream */
+       u_char          rs_buf[RSBUFSZ];        /* keystream blocks */
+} *rsx;
+
+static inline void _rs_rekey(u_char *dat, size_t datlen);
+
+static inline void
+_rs_init(u_char *buf, size_t n)
+{
+       if (n < KEYSZ + IVSZ)
+               return;
+
+       if (rs == NULL) {
+               if ((rs = mmap(NULL, sizeof(*rs), PROT_READ|PROT_WRITE,
+                   MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED)
+                       abort();
+#ifdef MAP_INHERIT_ZERO
+               if (minherit(rs, sizeof(*rs), MAP_INHERIT_ZERO) == -1)
+                       abort();
+#endif
+       }
+       if (rsx == NULL) {
+               if ((rsx = mmap(NULL, sizeof(*rsx), PROT_READ|PROT_WRITE,
+                   MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED)
+                       abort();
+       }
+
+       chacha_keysetup(&rsx->rs_chacha, buf, KEYSZ * 8, 0);
+       chacha_ivsetup(&rsx->rs_chacha, buf + KEYSZ);
+}
+
+static void
+_rs_stir(void)
+{
+       u_char rnd[KEYSZ + IVSZ];
+
+       if (getentropy(rnd, sizeof rnd) == -1)
+               raise(SIGKILL);
+
+       if (!rs)
+               _rs_init(rnd, sizeof(rnd));
+       else
+               _rs_rekey(rnd, sizeof(rnd));
+       explicit_bzero(rnd, sizeof(rnd));       /* discard source seed */
+
+       /* invalidate rs_buf */
+       rs->rs_have = 0;
+       memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf));
+
+       rs->rs_count = 1600000;
+}
+
+static inline void
+_rs_stir_if_needed(size_t len)
+{
+#ifndef MAP_INHERIT_ZERO
+       static pid_t _rs_pid = 0;
+       pid_t pid = getpid();
+
+       /* If a system lacks MAP_INHERIT_ZERO, resort to getpid() */
+       if (_rs_pid == 0 || _rs_pid != pid) {
+               _rs_pid = pid;
+               if (rs)
+                       rs->rs_count = 0;
+       }
+#endif
+       if (!rs || rs->rs_count <= len)
+               _rs_stir();
+       if (rs->rs_count <= len)
+               rs->rs_count = 0;
+       else
+               rs->rs_count -= len;
+}
+
+static inline void
+_rs_rekey(u_char *dat, size_t datlen)
+{
+#ifndef KEYSTREAM_ONLY
+       memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf));
+#endif
+       /* fill rs_buf with the keystream */
+       chacha_encrypt_bytes(&rsx->rs_chacha, rsx->rs_buf,
+           rsx->rs_buf, sizeof(rsx->rs_buf));
+       /* mix in optional user provided data */
+       if (dat) {
+               size_t i, m;
+
+               m = min(datlen, KEYSZ + IVSZ);
+               for (i = 0; i < m; i++)
+                       rsx->rs_buf[i] ^= dat[i];
+       }
+       /* immediately reinit for backtracking resistance */
+       _rs_init(rsx->rs_buf, KEYSZ + IVSZ);
+       memset(rsx->rs_buf, 0, KEYSZ + IVSZ);
+       rs->rs_have = sizeof(rsx->rs_buf) - KEYSZ - IVSZ;
+}
+
+static inline void
+_rs_random_buf(void *_buf, size_t n)
+{
+       u_char *buf = (u_char *)_buf;
+       u_char *keystream;
+       size_t m;
+
+       _rs_stir_if_needed(n);
+       while (n > 0) {
+               if (rs->rs_have > 0) {
+                       m = min(n, rs->rs_have);
+                       keystream = rsx->rs_buf + sizeof(rsx->rs_buf)
+                           - rs->rs_have;
+                       memcpy(buf, keystream, m);
+                       memset(keystream, 0, m);
+                       buf += m;
+                       n -= m;
+                       rs->rs_have -= m;
+               }
+               if (rs->rs_have == 0)
+                       _rs_rekey(NULL, 0);
+       }
+}
+
+static inline void
+_rs_random_u32(uint32_t *val)
+{
+       u_char *keystream;
+       _rs_stir_if_needed(sizeof(*val));
+       if (rs->rs_have < sizeof(*val))
+               _rs_rekey(NULL, 0);
+       keystream = rsx->rs_buf + sizeof(rsx->rs_buf) - rs->rs_have;
+       memcpy(val, keystream, sizeof(*val));
+       memset(keystream, 0, sizeof(*val));
+       rs->rs_have -= sizeof(*val);
+}
+
+uint32_t
+arc4random(void)
+{
+       uint32_t val;
+
+       _ARC4_LOCK();
+       _rs_random_u32(&val);
+       _ARC4_UNLOCK();
+       return val;
+}
+
+void
+arc4random_buf(void *buf, size_t n)
+{
+       _ARC4_LOCK();
+       _rs_random_buf(buf, n);
+       _ARC4_UNLOCK();
+}
+
+/*
+ * Calculate a uniformly distributed random number less than upper_bound
+ * avoiding "modulo bias".
+ *
+ * Uniformity is achieved by generating new random numbers until the one
+ * returned is outside the range [0, 2**32 % upper_bound).  This
+ * guarantees the selected random number will be inside
+ * [2**32 % upper_bound, 2**32) which maps back to [0, upper_bound)
+ * after reduction modulo upper_bound.
+ */
+uint32_t
+arc4random_uniform(uint32_t upper_bound)
+{
+       uint32_t r, min;
+
+       if (upper_bound < 2)
+               return 0;
+
+       /* 2**32 % x == (2**32 - x) % x */
+       min = -upper_bound % upper_bound;
+
+       /*
+        * This could theoretically loop forever but each retry has
+        * p > 0.5 (worst case, usually far better) of selecting a
+        * number inside the range we need, so it should rarely need
+        * to re-roll.
+        */
+       for (;;) {
+               r = arc4random();
+               if (r >= min)
+                       break;
+       }
+
+       return r % upper_bound;
+}
diff --git a/compat/chacha_private.h b/compat/chacha_private.h
new file mode 100644 (file)
index 0000000..7c3680f
--- /dev/null
@@ -0,0 +1,222 @@
+/*
+chacha-merged.c version 20080118
+D. J. Bernstein
+Public domain.
+*/
+
+/* $OpenBSD: chacha_private.h,v 1.2 2013/10/04 07:02:27 djm Exp $ */
+
+typedef unsigned char u8;
+typedef unsigned int u32;
+
+typedef struct
+{
+  u32 input[16]; /* could be compressed */
+} chacha_ctx;
+
+#define U8C(v) (v##U)
+#define U32C(v) (v##U)
+
+#define U8V(v) ((u8)(v) & U8C(0xFF))
+#define U32V(v) ((u32)(v) & U32C(0xFFFFFFFF))
+
+#define ROTL32(v, n) \
+  (U32V((v) << (n)) | ((v) >> (32 - (n))))
+
+#define U8TO32_LITTLE(p) \
+  (((u32)((p)[0])      ) | \
+   ((u32)((p)[1]) <<  8) | \
+   ((u32)((p)[2]) << 16) | \
+   ((u32)((p)[3]) << 24))
+
+#define U32TO8_LITTLE(p, v) \
+  do { \
+    (p)[0] = U8V((v)      ); \
+    (p)[1] = U8V((v) >>  8); \
+    (p)[2] = U8V((v) >> 16); \
+    (p)[3] = U8V((v) >> 24); \
+  } while (0)
+
+#define ROTATE(v,c) (ROTL32(v,c))
+#define XOR(v,w) ((v) ^ (w))
+#define PLUS(v,w) (U32V((v) + (w)))
+#define PLUSONE(v) (PLUS((v),1))
+
+#define QUARTERROUND(a,b,c,d) \
+  a = PLUS(a,b); d = ROTATE(XOR(d,a),16); \
+  c = PLUS(c,d); b = ROTATE(XOR(b,c),12); \
+  a = PLUS(a,b); d = ROTATE(XOR(d,a), 8); \
+  c = PLUS(c,d); b = ROTATE(XOR(b,c), 7);
+
+static const char sigma[16] = "expand 32-byte k";
+static const char tau[16] = "expand 16-byte k";
+
+static void
+chacha_keysetup(chacha_ctx *x,const u8 *k,u32 kbits,u32 ivbits)
+{
+  const char *constants;
+
+  x->input[4] = U8TO32_LITTLE(k + 0);
+  x->input[5] = U8TO32_LITTLE(k + 4);
+  x->input[6] = U8TO32_LITTLE(k + 8);
+  x->input[7] = U8TO32_LITTLE(k + 12);
+  if (kbits == 256) { /* recommended */
+    k += 16;
+    constants = sigma;
+  } else { /* kbits == 128 */
+    constants = tau;
+  }
+  x->input[8] = U8TO32_LITTLE(k + 0);
+  x->input[9] = U8TO32_LITTLE(k + 4);
+  x->input[10] = U8TO32_LITTLE(k + 8);
+  x->input[11] = U8TO32_LITTLE(k + 12);
+  x->input[0] = U8TO32_LITTLE(constants + 0);
+  x->input[1] = U8TO32_LITTLE(constants + 4);
+  x->input[2] = U8TO32_LITTLE(constants + 8);
+  x->input[3] = U8TO32_LITTLE(constants + 12);
+}
+
+static void
+chacha_ivsetup(chacha_ctx *x,const u8 *iv)
+{
+  x->input[12] = 0;
+  x->input[13] = 0;
+  x->input[14] = U8TO32_LITTLE(iv + 0);
+  x->input[15] = U8TO32_LITTLE(iv + 4);
+}
+
+static void
+chacha_encrypt_bytes(chacha_ctx *x,const u8 *m,u8 *c,u32 bytes)
+{
+  u32 x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
+  u32 j0, j1, j2, j3, j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15;
+  u8 *ctarget = NULL;
+  u8 tmp[64];
+  u_int i;
+
+  if (!bytes) return;
+
+  j0 = x->input[0];
+  j1 = x->input[1];
+  j2 = x->input[2];
+  j3 = x->input[3];
+  j4 = x->input[4];
+  j5 = x->input[5];
+  j6 = x->input[6];
+  j7 = x->input[7];
+  j8 = x->input[8];
+  j9 = x->input[9];
+  j10 = x->input[10];
+  j11 = x->input[11];
+  j12 = x->input[12];
+  j13 = x->input[13];
+  j14 = x->input[14];
+  j15 = x->input[15];
+
+  for (;;) {
+    if (bytes < 64) {
+      for (i = 0;i < bytes;++i) tmp[i] = m[i];
+      m = tmp;
+      ctarget = c;
+      c = tmp;
+    }
+    x0 = j0;
+    x1 = j1;
+    x2 = j2;
+    x3 = j3;
+    x4 = j4;
+    x5 = j5;
+    x6 = j6;
+    x7 = j7;
+    x8 = j8;
+    x9 = j9;
+    x10 = j10;
+    x11 = j11;
+    x12 = j12;
+    x13 = j13;
+    x14 = j14;
+    x15 = j15;
+    for (i = 20;i > 0;i -= 2) {
+      QUARTERROUND( x0, x4, x8,x12)
+      QUARTERROUND( x1, x5, x9,x13)
+      QUARTERROUND( x2, x6,x10,x14)
+      QUARTERROUND( x3, x7,x11,x15)
+      QUARTERROUND( x0, x5,x10,x15)
+      QUARTERROUND( x1, x6,x11,x12)
+      QUARTERROUND( x2, x7, x8,x13)
+      QUARTERROUND( x3, x4, x9,x14)
+    }
+    x0 = PLUS(x0,j0);
+    x1 = PLUS(x1,j1);
+    x2 = PLUS(x2,j2);
+    x3 = PLUS(x3,j3);
+    x4 = PLUS(x4,j4);
+    x5 = PLUS(x5,j5);
+    x6 = PLUS(x6,j6);
+    x7 = PLUS(x7,j7);
+    x8 = PLUS(x8,j8);
+    x9 = PLUS(x9,j9);
+    x10 = PLUS(x10,j10);
+    x11 = PLUS(x11,j11);
+    x12 = PLUS(x12,j12);
+    x13 = PLUS(x13,j13);
+    x14 = PLUS(x14,j14);
+    x15 = PLUS(x15,j15);
+
+#ifndef KEYSTREAM_ONLY
+    x0 = XOR(x0,U8TO32_LITTLE(m + 0));
+    x1 = XOR(x1,U8TO32_LITTLE(m + 4));
+    x2 = XOR(x2,U8TO32_LITTLE(m + 8));
+    x3 = XOR(x3,U8TO32_LITTLE(m + 12));
+    x4 = XOR(x4,U8TO32_LITTLE(m + 16));
+    x5 = XOR(x5,U8TO32_LITTLE(m + 20));
+    x6 = XOR(x6,U8TO32_LITTLE(m + 24));
+    x7 = XOR(x7,U8TO32_LITTLE(m + 28));
+    x8 = XOR(x8,U8TO32_LITTLE(m + 32));
+    x9 = XOR(x9,U8TO32_LITTLE(m + 36));
+    x10 = XOR(x10,U8TO32_LITTLE(m + 40));
+    x11 = XOR(x11,U8TO32_LITTLE(m + 44));
+    x12 = XOR(x12,U8TO32_LITTLE(m + 48));
+    x13 = XOR(x13,U8TO32_LITTLE(m + 52));
+    x14 = XOR(x14,U8TO32_LITTLE(m + 56));
+    x15 = XOR(x15,U8TO32_LITTLE(m + 60));
+#endif
+
+    j12 = PLUSONE(j12);
+    if (!j12) {
+      j13 = PLUSONE(j13);
+      /* stopping at 2^70 bytes per nonce is user's responsibility */
+    }
+
+    U32TO8_LITTLE(c + 0,x0);
+    U32TO8_LITTLE(c + 4,x1);
+    U32TO8_LITTLE(c + 8,x2);
+    U32TO8_LITTLE(c + 12,x3);
+    U32TO8_LITTLE(c + 16,x4);
+    U32TO8_LITTLE(c + 20,x5);
+    U32TO8_LITTLE(c + 24,x6);
+    U32TO8_LITTLE(c + 28,x7);
+    U32TO8_LITTLE(c + 32,x8);
+    U32TO8_LITTLE(c + 36,x9);
+    U32TO8_LITTLE(c + 40,x10);
+    U32TO8_LITTLE(c + 44,x11);
+    U32TO8_LITTLE(c + 48,x12);
+    U32TO8_LITTLE(c + 52,x13);
+    U32TO8_LITTLE(c + 56,x14);
+    U32TO8_LITTLE(c + 60,x15);
+
+    if (bytes <= 64) {
+      if (bytes < 64) {
+        for (i = 0;i < bytes;++i) ctarget[i] = c[i];
+      }
+      x->input[12] = j12;
+      x->input[13] = j13;
+      return;
+    }
+    bytes -= 64;
+    c += 64;
+#ifndef KEYSTREAM_ONLY
+    m += 64;
+#endif
+  }
+}
diff --git a/compat/explicit_bzero.c b/compat/explicit_bzero.c
new file mode 100644 (file)
index 0000000..3e33ca8
--- /dev/null
@@ -0,0 +1,19 @@
+/*     $OpenBSD: explicit_bzero.c,v 1.3 2014/06/21 02:34:26 matthew Exp $ */
+/*
+ * Public domain.
+ * Written by Matthew Dempsky.
+ */
+
+#include <string.h>
+
+__attribute__((weak)) void
+__explicit_bzero_hook(void *buf, size_t len)
+{
+}
+
+void
+explicit_bzero(void *buf, size_t len)
+{
+       memset(buf, 0, len);
+       __explicit_bzero_hook(buf, len);
+}
diff --git a/compat/getentropy_linux.c b/compat/getentropy_linux.c
new file mode 100644 (file)
index 0000000..3fd3031
--- /dev/null
@@ -0,0 +1,494 @@
+/*     $OpenBSD: getentropy_linux.c,v 1.18 2014/07/08 09:38:55 beck Exp $      */
+
+/*
+ * Copyright (c) 2014 Theo de Raadt <deraadt@openbsd.org>
+ * Copyright (c) 2014 Bob Beck <beck@obtuse.com>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#define        _POSIX_C_SOURCE 199309L
+#define        _GNU_SOURCE     1
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/ioctl.h>
+#include <sys/resource.h>
+#include <sys/syscall.h>
+#include <sys/sysctl.h>
+#include <sys/statvfs.h>
+#include <sys/socket.h>
+#include <sys/mount.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <termios.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <time.h>
+#include <openssl/sha.h>
+
+#include <linux/random.h>
+#include <linux/sysctl.h>
+#ifdef HAVE_GETAUXVAL
+#include <sys/auxv.h>
+#endif
+#include <sys/vfs.h>
+
+#define REPEAT 5
+#define min(a, b) (((a) < (b)) ? (a) : (b))
+
+#define HX(a, b) \
+       do { \
+               if ((a)) \
+                       HD(errno); \
+               else \
+                       HD(b); \
+       } while (0)
+
+#define HR(x, l) (SHA512_Update(&ctx, (char *)(x), (l)))
+#define HD(x)   (SHA512_Update(&ctx, (char *)&(x), sizeof (x)))
+
+int    getentropy(void *buf, size_t len);
+
+extern int main(int, char *argv[]);
+static int gotdata(char *buf, size_t len);
+static int getentropy_urandom(void *buf, size_t len);
+#ifdef CTL_MAXNAME
+static int getentropy_sysctl(void *buf, size_t len);
+#endif
+static int getentropy_fallback(void *buf, size_t len);
+
+int
+getentropy(void *buf, size_t len)
+{
+       int ret = -1;
+
+       if (len > 256) {
+               errno = EIO;
+               return -1;
+       }
+
+       /*
+        * Try to get entropy with /dev/urandom
+        *
+        * This can fail if the process is inside a chroot or if file
+        * descriptors are exhausted.
+        */
+       ret = getentropy_urandom(buf, len);
+       if (ret != -1)
+               return (ret);
+
+#ifdef CTL_MAXNAME
+       /*
+        * Try to use sysctl CTL_KERN, KERN_RANDOM, RANDOM_UUID.
+        * sysctl is a failsafe API, so it guarantees a result.  This
+        * should work inside a chroot, or when file descriptors are
+        * exhuasted.
+        *
+        * However this can fail if the Linux kernel removes support
+        * for sysctl.  Starting in 2007, there have been efforts to
+        * deprecate the sysctl API/ABI, and push callers towards use
+        * of the chroot-unavailable fd-using /proc mechanism --
+        * essentially the same problems as /dev/urandom.
+        *
+        * Numerous setbacks have been encountered in their deprecation
+        * schedule, so as of June 2014 the kernel ABI still exists on
+        * most Linux architectures. The sysctl() stub in libc is missing
+        * on some systems.  There are also reports that some kernels
+        * spew messages to the console.
+        */
+       ret = getentropy_sysctl(buf, len);
+       if (ret != -1)
+               return (ret);
+#endif /* CTL_MAXNAME */
+
+       /*
+        * Entropy collection via /dev/urandom and sysctl have failed.
+        *
+        * No other API exists for collecting entropy.  See the large
+        * comment block above.
+        *
+        * We have very few options:
+        *     - Even syslog_r is unsafe to call at this low level, so
+        *       there is no way to alert the user or program.
+        *     - Cannot call abort() because some systems have unsafe
+        *       corefiles.
+        *     - Could raise(SIGKILL) resulting in silent program termination.
+        *     - Return EIO, to hint that arc4random's stir function
+        *       should raise(SIGKILL)
+        *     - Do the best under the circumstances....
+        *
+        * This code path exists to bring light to the issue that Linux
+        * does not provide a failsafe API for entropy collection.
+        *
+        * We hope this demonstrates that Linux should either retain their
+        * sysctl ABI, or consider providing a new failsafe API which
+        * works in a chroot or when file descriptors are exhausted.
+        */
+#undef FAIL_HARD_WHEN_LINUX_DEPRECATES_SYSCTL
+#ifdef FAIL_HARD_WHEN_LINUX_DEPRECATES_SYSCTL
+       raise(SIGKILL);
+#endif
+       ret = getentropy_fallback(buf, len);
+       if (ret != -1)
+               return (ret);
+
+       errno = EIO;
+       return (ret);
+}
+
+/*
+ * Basic sanity checking; wish we could do better.
+ */
+static int
+gotdata(char *buf, size_t len)
+{
+       char    any_set = 0;
+       size_t  i;
+
+       for (i = 0; i < len; ++i)
+               any_set |= buf[i];
+       if (any_set == 0)
+               return -1;
+       return 0;
+}
+
+static int
+getentropy_urandom(void *buf, size_t len)
+{
+       struct stat st;
+       size_t i;
+       int fd, cnt, flags;
+       int save_errno = errno;
+
+start:
+
+        flags = O_RDONLY;
+#ifdef O_NOFOLLOW
+        flags |= O_NOFOLLOW;
+#endif
+#ifdef O_CLOEXEC
+        flags |= O_CLOEXEC;
+#endif
+       fd = open("/dev/urandom", flags, 0);
+       if (fd == -1) {
+               if (errno == EINTR)
+                       goto start;
+               goto nodevrandom;
+       }
+#ifndef O_CLOEXEC
+       fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
+#endif
+
+       /* Lightly verify that the device node looks sane */
+       if (fstat(fd, &st) == -1 || !S_ISCHR(st.st_mode)) {
+               close(fd);
+               goto nodevrandom;
+       }
+       if (ioctl(fd, RNDGETENTCNT, &cnt) == -1) {
+               close(fd);
+               goto nodevrandom;
+       }
+       for (i = 0; i < len; ) {
+               size_t wanted = len - i;
+               ssize_t ret = read(fd, buf + i, wanted);
+
+               if (ret == -1) {
+                       if (errno == EAGAIN || errno == EINTR)
+                               continue;
+                       close(fd);
+                       goto nodevrandom;
+               }
+               i += ret;
+       }
+       close(fd);
+       if (gotdata(buf, len) == 0) {
+               errno = save_errno;
+               return 0;               /* satisfied */
+       }
+nodevrandom:
+       errno = EIO;
+       return -1;
+}
+
+#ifdef CTL_MAXNAME
+static int
+getentropy_sysctl(void *buf, size_t len)
+{
+       static int mib[] = { CTL_KERN, KERN_RANDOM, RANDOM_UUID };
+       size_t i, chunk;
+       int save_errno = errno;
+
+       for (i = 0; i < len; ) {
+               chunk = min(len - i, 16);
+
+               /* SYS__sysctl because some systems already removed sysctl() */
+               struct __sysctl_args args = {
+                       .name = mib,
+                       .nlen = 3,
+                       .oldval = buf + i,
+                       .oldlenp = &chunk,
+               };
+               if (syscall(SYS__sysctl, &args) != 0)
+                       goto sysctlfailed;
+               i += chunk;
+       }
+       if (gotdata(buf, len) == 0) {
+               errno = save_errno;
+               return (0);                     /* satisfied */
+       }
+sysctlfailed:
+       errno = EIO;
+       return -1;
+}
+#endif /* CTL_MAXNAME */
+
+static int cl[] = {
+       CLOCK_REALTIME,
+#ifdef CLOCK_MONOTONIC
+       CLOCK_MONOTONIC,
+#endif
+#ifdef CLOCK_MONOTONIC_RAW
+       CLOCK_MONOTONIC_RAW,
+#endif
+#ifdef CLOCK_TAI
+       CLOCK_TAI,
+#endif
+#ifdef CLOCK_VIRTUAL
+       CLOCK_VIRTUAL,
+#endif
+#ifdef CLOCK_UPTIME
+       CLOCK_UPTIME,
+#endif
+#ifdef CLOCK_PROCESS_CPUTIME_ID
+       CLOCK_PROCESS_CPUTIME_ID,
+#endif
+#ifdef CLOCK_THREAD_CPUTIME_ID
+       CLOCK_THREAD_CPUTIME_ID,
+#endif
+};
+
+static int
+getentropy_fallback(void *buf, size_t len)
+{
+       uint8_t results[SHA512_DIGEST_LENGTH];
+       int save_errno = errno, e, m, pgs = getpagesize(), faster = 0, repeat;
+       static int cnt;
+       struct timespec ts;
+       struct timeval tv;
+       struct rusage ru;
+       sigset_t sigset;
+       struct stat st;
+       SHA512_CTX ctx;
+       static pid_t lastpid;
+       pid_t pid;
+       size_t i, ii;
+       char *p;
+
+       pid = getpid();
+       if (lastpid == pid) {
+               faster = 1;
+               repeat = 2;
+       } else {
+               faster = 0;
+               lastpid = pid;
+               repeat = REPEAT;
+       }
+       for (i = 0; i < len; ) {
+               int j;
+               SHA512_Init(&ctx);
+               for (j = 0; j < repeat; j++) {
+                       HX((e = gettimeofday(&tv, NULL)) == -1, tv);
+                       if (e != -1) {
+                               cnt += (int)tv.tv_sec;
+                               cnt += (int)tv.tv_usec;
+                       }
+
+                       for (ii = 0; ii < sizeof(cl)/sizeof(cl[0]); ii++)
+                               HX(clock_gettime(cl[ii], &ts) == -1, ts);
+
+                       HX((pid = getpid()) == -1, pid);
+                       HX((pid = getsid(pid)) == -1, pid);
+                       HX((pid = getppid()) == -1, pid);
+                       HX((pid = getpgid(0)) == -1, pid);
+                       HX((m = getpriority(0, 0)) == -1, m);
+
+                       if (!faster) {
+                               ts.tv_sec = 0;
+                               ts.tv_nsec = 1;
+                               (void) nanosleep(&ts, NULL);
+                       }
+
+                       HX(sigpending(&sigset) == -1, sigset);
+                       HX(sigprocmask(SIG_BLOCK, NULL, &sigset) == -1,
+                           sigset);
+
+                       HD(main);               /* an addr in program */
+                       HD(getentropy); /* an addr in this library */
+                       HD(printf);             /* an addr in libc */
+                       p = (char *)&p;
+                       HD(p);          /* an addr on stack */
+                       p = (char *)&errno;
+                       HD(p);          /* the addr of errno */
+
+                       if (i == 0) {
+                               struct sockaddr_storage ss;
+                               struct statvfs stvfs;
+                               struct termios tios;
+                               struct statfs stfs;
+                               socklen_t ssl;
+                               off_t off;
+
+                               /*
+                                * Prime-sized mappings encourage fragmentation;
+                                * thus exposing some address entropy.
+                                */
+                               struct mm {
+                                       size_t  npg;
+                                       void    *p;
+                               } mm[] =         {
+                                       { 17, MAP_FAILED }, { 3, MAP_FAILED },
+                                       { 11, MAP_FAILED }, { 2, MAP_FAILED },
+                                       { 5, MAP_FAILED }, { 3, MAP_FAILED },
+                                       { 7, MAP_FAILED }, { 1, MAP_FAILED },
+                                       { 57, MAP_FAILED }, { 3, MAP_FAILED },
+                                       { 131, MAP_FAILED }, { 1, MAP_FAILED },
+                               };
+
+                               for (m = 0; m < sizeof mm/sizeof(mm[0]); m++) {
+                                       HX(mm[m].p = mmap(NULL,
+                                           mm[m].npg * pgs,
+                                           PROT_READ|PROT_WRITE,
+                                           MAP_PRIVATE|MAP_ANON, -1,
+                                           (off_t)0), mm[m].p);
+                                       if (mm[m].p != MAP_FAILED) {
+                                               size_t mo;
+
+                                               /* Touch some memory... */
+                                               p = mm[m].p;
+                                               mo = cnt %
+                                                   (mm[m].npg * pgs - 1);
+                                               p[mo] = 1;
+                                               cnt += (int)((long)(mm[m].p)
+                                                   / pgs);
+                                       }
+
+                                       /* Check cnts and times... */
+                                       for (ii = 0; ii < sizeof(cl)/sizeof(cl[0]);
+                                           ii++) {
+                                               HX((e = clock_gettime(cl[ii],
+                                                   &ts)) == -1, ts);
+                                               if (e != -1)
+                                                       cnt += (int)ts.tv_nsec;
+                                       }
+
+                                       HX((e = getrusage(RUSAGE_SELF,
+                                           &ru)) == -1, ru);
+                                       if (e != -1) {
+                                               cnt += (int)ru.ru_utime.tv_sec;
+                                               cnt += (int)ru.ru_utime.tv_usec;
+                                       }
+                               }
+
+                               for (m = 0; m < sizeof mm/sizeof(mm[0]); m++) {
+                                       if (mm[m].p != MAP_FAILED)
+                                               munmap(mm[m].p, mm[m].npg * pgs);
+                                       mm[m].p = MAP_FAILED;
+                               }
+
+                               HX(stat(".", &st) == -1, st);
+                               HX(statvfs(".", &stvfs) == -1, stvfs);
+                               HX(statfs(".", &stfs) == -1, stfs);
+
+                               HX(stat("/", &st) == -1, st);
+                               HX(statvfs("/", &stvfs) == -1, stvfs);
+                               HX(statfs("/", &stfs) == -1, stfs);
+
+                               HX((e = fstat(0, &st)) == -1, st);
+                               if (e == -1) {
+                                       if (S_ISREG(st.st_mode) ||
+                                           S_ISFIFO(st.st_mode) ||
+                                           S_ISSOCK(st.st_mode)) {
+                                               HX(fstatvfs(0, &stvfs) == -1,
+                                                   stvfs);
+                                               HX(fstatfs(0, &stfs) == -1,
+                                                   stfs);
+                                               HX((off = lseek(0, (off_t)0,
+                                                   SEEK_CUR)) < 0, off);
+                                       }
+                                       if (S_ISCHR(st.st_mode)) {
+                                               HX(tcgetattr(0, &tios) == -1,
+                                                   tios);
+                                       } else if (S_ISSOCK(st.st_mode)) {
+                                               memset(&ss, 0, sizeof ss);
+                                               ssl = sizeof(ss);
+                                               HX(getpeername(0,
+                                                   (void *)&ss, &ssl) == -1,
+                                                   ss);
+                                       }
+                               }
+
+                               HX((e = getrusage(RUSAGE_CHILDREN,
+                                   &ru)) == -1, ru);
+                               if (e != -1) {
+                                       cnt += (int)ru.ru_utime.tv_sec;
+                                       cnt += (int)ru.ru_utime.tv_usec;
+                               }
+                       } else {
+                               /* Subsequent hashes absorb previous result */
+                               HD(results);
+                       }
+
+                       HX((e = gettimeofday(&tv, NULL)) == -1, tv);
+                       if (e != -1) {
+                               cnt += (int)tv.tv_sec;
+                               cnt += (int)tv.tv_usec;
+                       }
+
+                       HD(cnt);
+               }
+#ifdef AT_RANDOM
+               /* Not as random as you think but we take what we are given */
+               p = (char *) getauxval(AT_RANDOM);
+               if (p)
+                       HR(p, 16);
+#endif
+#ifdef AT_SYSINFO_EHDR
+               p = (char *) getauxval(AT_SYSINFO_EHDR);
+               if (p)
+                       HR(p, pgs);
+#endif
+#ifdef AT_BASE
+               p = (char *) getauxval(AT_BASE);
+               if (p)
+                       HD(p);
+#endif
+
+               SHA512_Final(results, &ctx);
+               memcpy(buf + i, results, min(sizeof(results), len - i));
+               i += min(sizeof(results), len - i);
+       }
+       memset(results, 0, sizeof results);
+       if (gotdata(buf, len) == 0) {
+               errno = save_errno;
+               return 0;               /* satisfied */
+       }
+       errno = EIO;
+       return -1;
+}
diff --git a/compat/getentropy_osx.c b/compat/getentropy_osx.c
new file mode 100644 (file)
index 0000000..4b1ca3c
--- /dev/null
@@ -0,0 +1,425 @@
+/*     $OpenBSD: getentropy_osx.c,v 1.2 2014/07/09 13:23:15 bcook Exp $        */
+
+/*
+ * Copyright (c) 2014 Theo de Raadt <deraadt@openbsd.org>
+ * Copyright (c) 2014 Bob Beck <beck@obtuse.com>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/ioctl.h>
+#include <sys/resource.h>
+#include <sys/syscall.h>
+#include <sys/sysctl.h>
+#include <sys/statvfs.h>
+#include <sys/socket.h>
+#include <sys/mount.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <termios.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <time.h>
+#include <mach/mach_time.h>
+#include <mach/mach_host.h>
+#include <mach/host_info.h>
+#include <sys/socketvar.h>
+#include <sys/vmmeter.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#include <netinet/udp.h>
+#include <netinet/ip_var.h>
+#include <netinet/tcp_var.h>
+#include <netinet/udp_var.h>
+#include <CommonCrypto/CommonDigest.h>
+#define SHA512_Update(a, b, c) (CC_SHA512_Update((a), (b), (c)))
+#define SHA512_Init(xxx) (CC_SHA512_Init((xxx)))
+#define SHA512_Final(xxx, yyy) (CC_SHA512_Final((xxx), (yyy)))
+#define SHA512_CTX CC_SHA512_CTX
+#define SHA512_DIGEST_LENGTH CC_SHA512_DIGEST_LENGTH
+
+#define REPEAT 5
+#define min(a, b) (((a) < (b)) ? (a) : (b))
+
+#define HX(a, b) \
+       do { \
+               if ((a)) \
+                       HD(errno); \
+               else \
+                       HD(b); \
+       } while (0)
+#define HR(x, l) (SHA512_Update(&ctx, (char *)(x), (l)))
+#define HD(x)   (SHA512_Update(&ctx, (char *)&(x), sizeof (x)))
+int    getentropy(void *buf, size_t len);
+
+extern int main(int, char *argv[]);
+static int gotdata(char *buf, size_t len);
+static int getentropy_urandom(void *buf, size_t len);
+static int getentropy_fallback(void *buf, size_t len);
+
+int
+getentropy(void *buf, size_t len)
+{
+       int ret = -1;
+
+       if (len > 256) {
+               errno = EIO;
+               return -1;
+       }
+
+       /*
+        * Try to get entropy with /dev/urandom
+        *
+        * This can fail if the process is inside a chroot or if file
+        * descriptors are exhausted.
+        */
+       ret = getentropy_urandom(buf, len);
+       if (ret != -1)
+               return (ret);
+
+       /*
+        * Entropy collection via /dev/urandom and sysctl have failed.
+        *
+        * No other API exists for collecting entropy, and we have
+         * no failsafe way to get it on OSX that is not sensitive
+         * to resource exhaustion.
+        *
+        * We have very few options:
+        *     - Even syslog_r is unsafe to call at this low level, so
+        *       there is no way to alert the user or program.
+        *     - Cannot call abort() because some systems have unsafe
+        *       corefiles.
+        *     - Could raise(SIGKILL) resulting in silent program termination.
+        *     - Return EIO, to hint that arc4random's stir function
+        *       should raise(SIGKILL)
+        *     - Do the best under the circumstances....
+        *
+        * This code path exists to bring light to the issue that OSX
+        * does not provide a failsafe API for entropy collection.
+        *
+        * We hope this demonstrates that OSX should consider
+        * providing a new failsafe API which works in a chroot or
+        * when file descriptors are exhausted.
+        */
+#undef FAIL_WHEN_SYSTEM_ENTROPY_FAILS
+#ifdef FAIL_WHEN_SYSTEM_ENTROPY_FAILS
+       raise(SIGKILL);
+#endif
+       ret = getentropy_fallback(buf, len);
+       if (ret != -1)
+               return (ret);
+
+       errno = EIO;
+       return (ret);
+}
+
+/*
+ * Basic sanity checking; wish we could do better.
+ */
+static int
+gotdata(char *buf, size_t len)
+{
+       char    any_set = 0;
+       size_t  i;
+
+       for (i = 0; i < len; ++i)
+               any_set |= buf[i];
+       if (any_set == 0)
+               return -1;
+       return 0;
+}
+
+static int
+getentropy_urandom(void *buf, size_t len)
+{
+       struct stat st;
+       size_t i;
+       int fd, flags;
+       int save_errno = errno;
+
+start:
+
+        flags = O_RDONLY;
+#ifdef O_NOFOLLOW
+        flags |= O_NOFOLLOW;
+#endif
+#ifdef O_CLOEXEC
+        flags |= O_CLOEXEC;
+#endif
+       fd = open("/dev/urandom", flags, 0);
+       if (fd == -1) {
+               if (errno == EINTR)
+                       goto start;
+               goto nodevrandom;
+       }
+#ifndef O_CLOEXEC
+       fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
+#endif
+
+       /* Lightly verify that the device node looks sane */
+       if (fstat(fd, &st) == -1 || !S_ISCHR(st.st_mode)) {
+               close(fd);
+               goto nodevrandom;
+       }
+       for (i = 0; i < len; ) {
+               size_t wanted = len - i;
+               ssize_t ret = read(fd, buf + i, wanted);
+
+               if (ret == -1) {
+                       if (errno == EAGAIN || errno == EINTR)
+                               continue;
+                       close(fd);
+                       goto nodevrandom;
+               }
+               i += ret;
+       }
+       close(fd);
+       if (gotdata(buf, len) == 0) {
+               errno = save_errno;
+               return 0;               /* satisfied */
+       }
+nodevrandom:
+       errno = EIO;
+       return -1;
+}
+
+static int
+getentropy_fallback(void *buf, size_t len)
+{
+       int tcpmib[] = { CTL_NET, AF_INET, IPPROTO_TCP, TCPCTL_STATS };
+       int udpmib[] = { CTL_NET, AF_INET, IPPROTO_UDP, UDPCTL_STATS };
+       int ipmib[] = { CTL_NET, AF_INET, IPPROTO_IP, IPCTL_STATS };
+       int kmib[] = { CTL_KERN, KERN_USRSTACK };
+       int hwmib[] = { CTL_HW, HW_USERMEM };
+       int save_errno = errno, e, m, pgs = getpagesize(), faster = 0, repeat;
+       uint8_t results[SHA512_DIGEST_LENGTH];
+       struct tcpstat tcpstat;
+       struct udpstat udpstat;
+       struct ipstat ipstat;
+        u_int64_t mach_time;
+       unsigned int idata;
+       struct timespec ts;
+       struct timeval tv;
+       struct rusage ru;
+       sigset_t sigset;
+       struct stat st;
+       static int cnt;
+       SHA512_CTX ctx;
+       static pid_t lastpid;
+       void * addr;
+       pid_t pid;
+       size_t i;
+       char *p;
+
+       pid = getpid();
+       if (lastpid == pid) {
+               faster = 1;
+               repeat = 2;
+       } else {
+               faster = 0;
+               lastpid = pid;
+               repeat = REPEAT;
+       }
+       for (i = 0; i < len; ) {
+               int j;
+               SHA512_Init(&ctx);
+               for (j = 0; j < repeat; j++) {
+                       size_t len;
+                       HX((e = gettimeofday(&tv, NULL)) == -1, tv);
+                       if (e != -1) {
+                               cnt += (int)tv.tv_sec;
+                               cnt += (int)tv.tv_usec;
+                       }
+
+                        mach_time = mach_absolute_time();
+                        HD(mach_time);
+
+                       len = sizeof(addr);
+                       HX(sysctl(kmib, sizeof(kmib) / sizeof(kmib[0]),
+                               &addr, &len, NULL, 0) == -1, addr);
+
+                       len = sizeof(idata);
+                       HX(sysctl(hwmib, sizeof(hwmib) / sizeof(hwmib[0]),
+                               &idata, &len, NULL, 0) == -1, idata);
+
+                       len = sizeof(tcpstat);
+                       HX(sysctl(tcpmib, sizeof(tcpmib) / sizeof(tcpmib[0]),
+                               &tcpstat, &len, NULL, 0) == -1, tcpstat);
+
+                       len = sizeof(udpstat);
+                       HX(sysctl(udpmib, sizeof(udpmib) / sizeof(udpmib[0]),
+                               &udpstat, &len, NULL, 0) == -1, udpstat);
+
+                       len = sizeof(ipstat);
+                       HX(sysctl(ipmib, sizeof(ipmib) / sizeof(ipmib[0]),
+                               &ipstat, &len, NULL, 0) == -1, ipstat);
+
+                       HX((pid = getpid()) == -1, pid);
+                       HX((pid = getsid(pid)) == -1, pid);
+                       HX((pid = getppid()) == -1, pid);
+                       HX((pid = getpgid(0)) == -1, pid);
+                       HX((m = getpriority(0, 0)) == -1, m);
+
+                       if (!faster) {
+                               ts.tv_sec = 0;
+                               ts.tv_nsec = 1;
+                               (void) nanosleep(&ts, NULL);
+                       }
+
+                       HX(sigpending(&sigset) == -1, sigset);
+                       HX(sigprocmask(SIG_BLOCK, NULL, &sigset) == -1,
+                           sigset);
+
+                       HD(main);               /* an addr in program */
+                       HD(getentropy); /* an addr in this library */
+                       HD(printf);             /* an addr in libc */
+                       p = (char *)&p;
+                       HD(p);          /* an addr on stack */
+                       p = (char *)&errno;
+                       HD(p);          /* the addr of errno */
+
+                       if (i == 0) {
+                               struct sockaddr_storage ss;
+                               struct statvfs stvfs;
+                               struct termios tios;
+                               struct statfs stfs;
+                               socklen_t ssl;
+                               off_t off;
+
+                               /*
+                                * Prime-sized mappings encourage fragmentation;
+                                * thus exposing some address entropy.
+                                */
+                               struct mm {
+                                       size_t  npg;
+                                       void    *p;
+                               } mm[] =         {
+                                       { 17, MAP_FAILED }, { 3, MAP_FAILED },
+                                       { 11, MAP_FAILED }, { 2, MAP_FAILED },
+                                       { 5, MAP_FAILED }, { 3, MAP_FAILED },
+                                       { 7, MAP_FAILED }, { 1, MAP_FAILED },
+                                       { 57, MAP_FAILED }, { 3, MAP_FAILED },
+                                       { 131, MAP_FAILED }, { 1, MAP_FAILED },
+                               };
+
+                               for (m = 0; m < sizeof mm/sizeof(mm[0]); m++) {
+                                       HX(mm[m].p = mmap(NULL,
+                                           mm[m].npg * pgs,
+                                           PROT_READ|PROT_WRITE,
+                                           MAP_PRIVATE|MAP_ANON, -1,
+                                           (off_t)0), mm[m].p);
+                                       if (mm[m].p != MAP_FAILED) {
+                                               size_t mo;
+
+                                               /* Touch some memory... */
+                                               p = mm[m].p;
+                                               mo = cnt %
+                                                   (mm[m].npg * pgs - 1);
+                                               p[mo] = 1;
+                                               cnt += (int)((long)(mm[m].p)
+                                                   / pgs);
+                                       }
+
+                                       /* Check cnts and times... */
+                                        mach_time = mach_absolute_time();
+                                        HD(mach_time);
+                                        cnt += (int)mach_time;
+
+                                       HX((e = getrusage(RUSAGE_SELF,
+                                           &ru)) == -1, ru);
+                                       if (e != -1) {
+                                               cnt += (int)ru.ru_utime.tv_sec;
+                                               cnt += (int)ru.ru_utime.tv_usec;
+                                       }
+                               }
+
+                               for (m = 0; m < sizeof mm/sizeof(mm[0]); m++) {
+                                       if (mm[m].p != MAP_FAILED)
+                                               munmap(mm[m].p, mm[m].npg * pgs);
+                                       mm[m].p = MAP_FAILED;
+                               }
+
+                               HX(stat(".", &st) == -1, st);
+                               HX(statvfs(".", &stvfs) == -1, stvfs);
+                               HX(statfs(".", &stfs) == -1, stfs);
+
+                               HX(stat("/", &st) == -1, st);
+                               HX(statvfs("/", &stvfs) == -1, stvfs);
+                               HX(statfs("/", &stfs) == -1, stfs);
+
+                               HX((e = fstat(0, &st)) == -1, st);
+                               if (e == -1) {
+                                       if (S_ISREG(st.st_mode) ||
+                                           S_ISFIFO(st.st_mode) ||
+                                           S_ISSOCK(st.st_mode)) {
+                                               HX(fstatvfs(0, &stvfs) == -1,
+                                                   stvfs);
+                                               HX(fstatfs(0, &stfs) == -1,
+                                                   stfs);
+                                               HX((off = lseek(0, (off_t)0,
+                                                   SEEK_CUR)) < 0, off);
+                                       }
+                                       if (S_ISCHR(st.st_mode)) {
+                                               HX(tcgetattr(0, &tios) == -1,
+                                                   tios);
+                                       } else if (S_ISSOCK(st.st_mode)) {
+                                               memset(&ss, 0, sizeof ss);
+                                               ssl = sizeof(ss);
+                                               HX(getpeername(0,
+                                                   (void *)&ss, &ssl) == -1,
+                                                   ss);
+                                       }
+                               }
+
+                               HX((e = getrusage(RUSAGE_CHILDREN,
+                                   &ru)) == -1, ru);
+                               if (e != -1) {
+                                       cnt += (int)ru.ru_utime.tv_sec;
+                                       cnt += (int)ru.ru_utime.tv_usec;
+                               }
+                       } else {
+                               /* Subsequent hashes absorb previous result */
+                               HD(results);
+                       }
+
+                       HX((e = gettimeofday(&tv, NULL)) == -1, tv);
+                       if (e != -1) {
+                               cnt += (int)tv.tv_sec;
+                               cnt += (int)tv.tv_usec;
+                       }
+
+                       HD(cnt);
+               }
+
+
+               SHA512_Final(results, &ctx);
+               memcpy(buf + i, results, min(sizeof(results), len - i));
+               i += min(sizeof(results), len - i);
+       }
+       memset(results, 0, sizeof results);
+       if (gotdata(buf, len) == 0) {
+               errno = save_errno;
+               return 0;               /* satisfied */
+       }
+       errno = EIO;
+       return -1;
+}
diff --git a/compat/getentropy_solaris.c b/compat/getentropy_solaris.c
new file mode 100644 (file)
index 0000000..6aeb871
--- /dev/null
@@ -0,0 +1,413 @@
+/*     $OpenBSD: getentropy_solaris.c,v 1.1 2014/07/08 10:45:35 beck Exp $     */
+
+/*
+ * Copyright (c) 2014 Theo de Raadt <deraadt@openbsd.org>
+ * Copyright (c) 2014 Bob Beck <beck@obtuse.com>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/ioctl.h>
+#include <sys/resource.h>
+#include <sys/syscall.h>
+#include <sys/statvfs.h>
+#include <sys/socket.h>
+#include <sys/mount.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <termios.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <time.h>
+#include <sys/sha2.h>
+#define SHA512_Init SHA512Init
+#define SHA512_Update SHA512Update
+#define SHA512_Final SHA512Final
+
+
+#include <sys/vfs.h>
+#include <sys/statfs.h>
+#include <sys/loadavg.h>
+
+#define REPEAT 5
+#define min(a, b) (((a) < (b)) ? (a) : (b))
+
+#define HX(a, b) \
+       do { \
+               if ((a)) \
+                       HD(errno); \
+               else \
+                       HD(b); \
+       } while (0)
+
+#define HR(x, l) (SHA512_Update(&ctx, (char *)(x), (l)))
+#define HD(x)   (SHA512_Update(&ctx, (char *)&(x), sizeof (x)))
+
+int    getentropy(void *buf, size_t len);
+
+extern int main(int, char *argv[]);
+static int gotdata(char *buf, size_t len);
+static int getentropy_urandom(void *buf, size_t len);
+static int getentropy_fallback(void *buf, size_t len);
+
+int
+getentropy(void *buf, size_t len)
+{
+       int ret = -1;
+
+       if (len > 256) {
+               errno = EIO;
+               return -1;
+       }
+
+       /*
+        * Try to get entropy with /dev/urandom
+        *
+        * This can fail if the process is inside a chroot or if file
+        * descriptors are exhausted.
+        */
+       ret = getentropy_urandom(buf, len);
+       if (ret != -1)
+               return (ret);
+       /*
+        * Entropy collection via /dev/urandom and sysctl have failed.
+        *
+        * No other API exists for collecting entropy, and we have
+         * no failsafe way to get it on Solaris that is not sensitive
+         * to resource exhaustion.
+        *
+        * We have very few options:
+        *     - Even syslog_r is unsafe to call at this low level, so
+        *       there is no way to alert the user or program.
+        *     - Cannot call abort() because some systems have unsafe
+        *       corefiles.
+        *     - Could raise(SIGKILL) resulting in silent program termination.
+        *     - Return EIO, to hint that arc4random's stir function
+        *       should raise(SIGKILL)
+        *     - Do the best under the circumstances....
+        *
+        * This code path exists to bring light to the issue that Solaris
+        * does not provide a failsafe API for entropy collection.
+        *
+        * We hope this demonstrates that Solaris should consider
+        * providing a new failsafe API which works in a chroot or
+        * when file descriptors are exhausted.
+        */
+#undef FAIL_WHEN_SYSTEM_ENTROPY_FAILS
+#ifdef FAIL_WHEN_SYSTEM_ENTROPY_FAILS
+       raise(SIGKILL);
+#endif
+       ret = getentropy_fallback(buf, len);
+       if (ret != -1)
+               return (ret);
+
+       errno = EIO;
+       return (ret);
+}
+
+/*
+ * Basic sanity checking; wish we could do better.
+ */
+static int
+gotdata(char *buf, size_t len)
+{
+       char    any_set = 0;
+       size_t  i;
+
+       for (i = 0; i < len; ++i)
+               any_set |= buf[i];
+       if (any_set == 0)
+               return -1;
+       return 0;
+}
+
+static int
+getentropy_urandom(void *buf, size_t len)
+{
+       struct stat st;
+       size_t i;
+       int fd, flags;
+       int save_errno = errno;
+
+start:
+
+        flags = O_RDONLY;
+#ifdef O_NOFOLLOW
+        flags |= O_NOFOLLOW;
+#endif
+#ifdef O_CLOEXEC
+        flags |= O_CLOEXEC;
+#endif
+       /* 
+        * Solaris provides /dev/urandom as a symbolic link. 
+        * /devices/pseudo/random@0:urandom should be the
+        * real device path, and we do want O_NOFOLLOW. 
+        */
+       fd = open("/devices/pseudo/random@0:urandom", flags, 0);
+       if (fd == -1) {
+               if (errno == EINTR)
+                       goto start;
+               goto nodevrandom;
+       }
+#ifndef O_CLOEXEC
+       fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
+#endif
+
+       /* Lightly verify that the device node looks sane */
+       if (fstat(fd, &st) == -1 || !S_ISCHR(st.st_mode)) {
+               close(fd);
+               goto nodevrandom;
+       }
+       for (i = 0; i < len; ) {
+               size_t wanted = len - i;
+               ssize_t ret = read(fd, buf + i, wanted);
+
+               if (ret == -1) {
+                       if (errno == EAGAIN || errno == EINTR)
+                               continue;
+                       close(fd);
+                       goto nodevrandom;
+               }
+               i += ret;
+       }
+       close(fd);
+       if (gotdata(buf, len) == 0) {
+               errno = save_errno;
+               return 0;               /* satisfied */
+       }
+nodevrandom:
+       errno = EIO;
+       return -1;
+}
+
+static int cl[] = {
+       CLOCK_REALTIME,
+#ifdef CLOCK_MONOTONIC
+       CLOCK_MONOTONIC,
+#endif
+#ifdef CLOCK_MONOTONIC_RAW
+       CLOCK_MONOTONIC_RAW,
+#endif
+#ifdef CLOCK_TAI
+       CLOCK_TAI,
+#endif
+#ifdef CLOCK_VIRTUAL
+       CLOCK_VIRTUAL,
+#endif
+#ifdef CLOCK_UPTIME
+       CLOCK_UPTIME,
+#endif
+#ifdef CLOCK_PROCESS_CPUTIME_ID
+       CLOCK_PROCESS_CPUTIME_ID,
+#endif
+#ifdef CLOCK_THREAD_CPUTIME_ID
+       CLOCK_THREAD_CPUTIME_ID,
+#endif
+};
+
+static int
+getentropy_fallback(void *buf, size_t len)
+{
+       uint8_t results[SHA512_DIGEST_LENGTH];
+       int save_errno = errno, e, m, pgs = getpagesize(), faster = 0, repeat;
+       static int cnt;
+       struct timespec ts;
+       struct timeval tv;
+       double loadavg[3];
+       struct rusage ru;
+       sigset_t sigset;
+       struct stat st;
+       SHA512_CTX ctx;
+       static pid_t lastpid;
+       pid_t pid;
+       size_t i, ii;
+       char *p;
+
+       pid = getpid();
+       if (lastpid == pid) {
+               faster = 1;
+               repeat = 2;
+       } else {
+               faster = 0;
+               lastpid = pid;
+               repeat = REPEAT;
+       }
+       for (i = 0; i < len; ) {
+               int j;
+               SHA512_Init(&ctx);
+               for (j = 0; j < repeat; j++) {
+                       HX((e = gettimeofday(&tv, NULL)) == -1, tv);
+                       if (e != -1) {
+                               cnt += (int)tv.tv_sec;
+                               cnt += (int)tv.tv_usec;
+                       }
+
+                       for (ii = 0; ii < sizeof(cl)/sizeof(cl[0]); ii++)
+                               HX(clock_gettime(cl[ii], &ts) == -1, ts);
+                       HX((pid = getpid()) == -1, pid);
+                       HX((pid = getsid(pid)) == -1, pid);
+                       HX((pid = getppid()) == -1, pid);
+                       HX((pid = getpgid(0)) == -1, pid);
+                       HX((m = getpriority(0, 0)) == -1, m);
+                       HX((getloadavg(loadavg, 3) == -1), loadavg);
+
+                       if (!faster) {
+                               ts.tv_sec = 0;
+                               ts.tv_nsec = 1;
+                               (void) nanosleep(&ts, NULL);
+                       }
+
+                       HX(sigpending(&sigset) == -1, sigset);
+                       HX(sigprocmask(SIG_BLOCK, NULL, &sigset) == -1,
+                           sigset);
+
+                       HD(main);               /* an addr in program */
+                       HD(getentropy); /* an addr in this library */
+                       HD(printf);             /* an addr in libc */
+                       p = (char *)&p;
+                       HD(p);          /* an addr on stack */
+                       p = (char *)&errno;
+                       HD(p);          /* the addr of errno */
+
+                       if (i == 0) {
+                               struct sockaddr_storage ss;
+                               struct statvfs stvfs;
+                               struct termios tios;
+                               socklen_t ssl;
+                               off_t off;
+
+                               /*
+                                * Prime-sized mappings encourage fragmentation;
+                                * thus exposing some address entropy.
+                                */
+                               struct mm {
+                                       size_t  npg;
+                                       void    *p;
+                               } mm[] =         {
+                                       { 17, MAP_FAILED }, { 3, MAP_FAILED },
+                                       { 11, MAP_FAILED }, { 2, MAP_FAILED },
+                                       { 5, MAP_FAILED }, { 3, MAP_FAILED },
+                                       { 7, MAP_FAILED }, { 1, MAP_FAILED },
+                                       { 57, MAP_FAILED }, { 3, MAP_FAILED },
+                                       { 131, MAP_FAILED }, { 1, MAP_FAILED },
+                               };
+
+                               for (m = 0; m < sizeof mm/sizeof(mm[0]); m++) {
+                                       HX(mm[m].p = mmap(NULL,
+                                           mm[m].npg * pgs,
+                                           PROT_READ|PROT_WRITE,
+                                           MAP_PRIVATE|MAP_ANON, -1,
+                                           (off_t)0), mm[m].p);
+                                       if (mm[m].p != MAP_FAILED) {
+                                               size_t mo;
+
+                                               /* Touch some memory... */
+                                               p = mm[m].p;
+                                               mo = cnt %
+                                                   (mm[m].npg * pgs - 1);
+                                               p[mo] = 1;
+                                               cnt += (int)((long)(mm[m].p)
+                                                   / pgs);
+                                       }
+
+                                       /* Check cnts and times... */
+                                       for (ii = 0; ii < sizeof(cl)/sizeof(cl[0]);
+                                           ii++) {
+                                               HX((e = clock_gettime(cl[ii],
+                                                   &ts)) == -1, ts);
+                                               if (e != -1)
+                                                       cnt += (int)ts.tv_nsec;
+                                       }
+
+                                       HX((e = getrusage(RUSAGE_SELF,
+                                           &ru)) == -1, ru);
+                                       if (e != -1) {
+                                               cnt += (int)ru.ru_utime.tv_sec;
+                                               cnt += (int)ru.ru_utime.tv_usec;
+                                       }
+                               }
+
+                               for (m = 0; m < sizeof mm/sizeof(mm[0]); m++) {
+                                       if (mm[m].p != MAP_FAILED)
+                                               munmap(mm[m].p, mm[m].npg * pgs);
+                                       mm[m].p = MAP_FAILED;
+                               }
+
+                               HX(stat(".", &st) == -1, st);
+                               HX(statvfs(".", &stvfs) == -1, stvfs);
+
+                               HX(stat("/", &st) == -1, st);
+                               HX(statvfs("/", &stvfs) == -1, stvfs);
+
+                               HX((e = fstat(0, &st)) == -1, st);
+                               if (e == -1) {
+                                       if (S_ISREG(st.st_mode) ||
+                                           S_ISFIFO(st.st_mode) ||
+                                           S_ISSOCK(st.st_mode)) {
+                                               HX(fstatvfs(0, &stvfs) == -1,
+                                                   stvfs);
+                                               HX((off = lseek(0, (off_t)0,
+                                                   SEEK_CUR)) < 0, off);
+                                       }
+                                       if (S_ISCHR(st.st_mode)) {
+                                               HX(tcgetattr(0, &tios) == -1,
+                                                   tios);
+                                       } else if (S_ISSOCK(st.st_mode)) {
+                                               memset(&ss, 0, sizeof ss);
+                                               ssl = sizeof(ss);
+                                               HX(getpeername(0,
+                                                   (void *)&ss, &ssl) == -1,
+                                                   ss);
+                                       }
+                               }
+
+                               HX((e = getrusage(RUSAGE_CHILDREN,
+                                   &ru)) == -1, ru);
+                               if (e != -1) {
+                                       cnt += (int)ru.ru_utime.tv_sec;
+                                       cnt += (int)ru.ru_utime.tv_usec;
+                               }
+                       } else {
+                               /* Subsequent hashes absorb previous result */
+                               HD(results);
+                       }
+
+                       HX((e = gettimeofday(&tv, NULL)) == -1, tv);
+                       if (e != -1) {
+                               cnt += (int)tv.tv_sec;
+                               cnt += (int)tv.tv_usec;
+                       }
+
+                       HD(cnt);
+               }
+               SHA512_Final(results, &ctx);
+               memcpy(buf + i, results, min(sizeof(results), len - i));
+               i += min(sizeof(results), len - i);
+       }
+       memset(results, 0, sizeof results);
+       if (gotdata(buf, len) == 0) {
+               errno = save_errno;
+               return 0;               /* satisfied */
+       }
+       errno = EIO;
+       return -1;
+}
diff --git a/compat/sha512.c b/compat/sha512.c
new file mode 100644 (file)
index 0000000..e5db1c9
--- /dev/null
@@ -0,0 +1,468 @@
+/*
+ * FILE:       sha2.c
+ * AUTHOR:     Aaron D. Gifford - http://www.aarongifford.com/
+ * 
+ * Copyright (c) 2000-2001, Aaron D. Gifford
+ * All rights reserved.
+ *
+ * Modified by Jelte Jansen to fit in ldns, and not clash with any
+ * system-defined SHA code.
+ * Changes:
+ * - Renamed (external) functions and constants to fit ldns style
+ * - Removed _End and _Data functions
+ * - Added ldns_shaX(data, len, digest) convenience functions
+ * - Removed prototypes of _Transform functions and made those static
+ * Modified by Wouter, and trimmed, to provide SHA512 for getentropy_fallback.
+ * 
+ * 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.
+ * 3. Neither the name of the copyright holder nor the names of contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``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 AUTHOR OR CONTRIBUTOR(S) 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.
+ *
+ * $Id: sha2.c,v 1.1 2001/11/08 00:01:51 adg Exp adg $
+ */
+#include "config.h"
+
+#include <string.h>    /* memcpy()/memset() or bcopy()/bzero() */
+#include <assert.h>    /* assert() */
+
+#define SHA512_BLOCK_LENGTH            128
+#define SHA512_DIGEST_LENGTH           64
+#define SHA512_DIGEST_STRING_LENGTH    (SHA512_DIGEST_LENGTH * 2 + 1)
+typedef struct _SHA512_CTX {
+       uint64_t        state[8];
+       uint64_t        bitcount[2];
+       uint8_t buffer[SHA512_BLOCK_LENGTH];
+} SHA512_CTX;
+
+void SHA512_init(SHA512_CTX*);
+void SHA512_update(SHA512_CTX*, const uint8_t*, size_t);
+void SHA512_final(uint8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*);
+unsigned char *SHA512(unsigned char *data, unsigned int data_len, unsigned char *digest);
+
+
+/*** SHA-256/384/512 Machine Architecture Definitions *****************/
+/*
+ * BYTE_ORDER NOTE:
+ *
+ * Please make sure that your system defines BYTE_ORDER.  If your
+ * architecture is little-endian, make sure it also defines
+ * LITTLE_ENDIAN and that the two (BYTE_ORDER and LITTLE_ENDIAN) are
+ * equivilent.
+ *
+ * If your system does not define the above, then you can do so by
+ * hand like this:
+ *
+ *   #define LITTLE_ENDIAN 1234
+ *   #define BIG_ENDIAN    4321
+ *
+ * And for little-endian machines, add:
+ *
+ *   #define BYTE_ORDER LITTLE_ENDIAN 
+ *
+ * Or for big-endian machines:
+ *
+ *   #define BYTE_ORDER BIG_ENDIAN
+ *
+ * The FreeBSD machine this was written on defines BYTE_ORDER
+ * appropriately by including <sys/types.h> (which in turn includes
+ * <machine/endian.h> where the appropriate definitions are actually
+ * made).
+ */
+#if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN)
+#error Define BYTE_ORDER to be equal to either LITTLE_ENDIAN or BIG_ENDIAN
+#endif
+
+typedef uint8_t  sha2_byte;    /* Exactly 1 byte */
+typedef uint32_t sha2_word32;  /* Exactly 4 bytes */
+#ifdef S_SPLINT_S
+typedef unsigned long long sha2_word64; /* lint 8 bytes */
+#else
+typedef uint64_t sha2_word64;  /* Exactly 8 bytes */
+#endif
+
+/*** SHA-256/384/512 Various Length Definitions ***********************/
+#define SHA512_SHORT_BLOCK_LENGTH      (SHA512_BLOCK_LENGTH - 16)
+
+
+/*** ENDIAN REVERSAL MACROS *******************************************/
+#if BYTE_ORDER == LITTLE_ENDIAN
+#define REVERSE32(w,x) { \
+       sha2_word32 tmp = (w); \
+       tmp = (tmp >> 16) | (tmp << 16); \
+       (x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \
+}
+#ifndef S_SPLINT_S
+#define REVERSE64(w,x) { \
+       sha2_word64 tmp = (w); \
+       tmp = (tmp >> 32) | (tmp << 32); \
+       tmp = ((tmp & 0xff00ff00ff00ff00ULL) >> 8) | \
+             ((tmp & 0x00ff00ff00ff00ffULL) << 8); \
+       (x) = ((tmp & 0xffff0000ffff0000ULL) >> 16) | \
+             ((tmp & 0x0000ffff0000ffffULL) << 16); \
+}
+#else /* splint */
+#define REVERSE64(w,x) /* splint */
+#endif /* splint */
+#endif /* BYTE_ORDER == LITTLE_ENDIAN */
+
+/*
+ * Macro for incrementally adding the unsigned 64-bit integer n to the
+ * unsigned 128-bit integer (represented using a two-element array of
+ * 64-bit words):
+ */
+#define ADDINC128(w,n) { \
+       (w)[0] += (sha2_word64)(n); \
+       if ((w)[0] < (n)) { \
+               (w)[1]++; \
+       } \
+}
+#ifdef S_SPLINT_S
+#undef ADDINC128
+#define ADDINC128(w,n) /* splint */
+#endif
+
+/*
+ * Macros for copying blocks of memory and for zeroing out ranges
+ * of memory.  Using these macros makes it easy to switch from
+ * using memset()/memcpy() and using bzero()/bcopy().
+ *
+ * Please define either SHA2_USE_MEMSET_MEMCPY or define
+ * SHA2_USE_BZERO_BCOPY depending on which function set you
+ * choose to use:
+ */
+#if !defined(SHA2_USE_MEMSET_MEMCPY) && !defined(SHA2_USE_BZERO_BCOPY)
+/* Default to memset()/memcpy() if no option is specified */
+#define        SHA2_USE_MEMSET_MEMCPY  1
+#endif
+#if defined(SHA2_USE_MEMSET_MEMCPY) && defined(SHA2_USE_BZERO_BCOPY)
+/* Abort with an error if BOTH options are defined */
+#error Define either SHA2_USE_MEMSET_MEMCPY or SHA2_USE_BZERO_BCOPY, not both!
+#endif
+
+#ifdef SHA2_USE_MEMSET_MEMCPY
+#define MEMSET_BZERO(p,l)      memset((p), 0, (l))
+#define MEMCPY_BCOPY(d,s,l)    memcpy((d), (s), (l))
+#endif
+#ifdef SHA2_USE_BZERO_BCOPY
+#define MEMSET_BZERO(p,l)      bzero((p), (l))
+#define MEMCPY_BCOPY(d,s,l)    bcopy((s), (d), (l))
+#endif
+
+
+/*** THE SIX LOGICAL FUNCTIONS ****************************************/
+/*
+ * Bit shifting and rotation (used by the six SHA-XYZ logical functions:
+ *
+ *   NOTE:  The naming of R and S appears backwards here (R is a SHIFT and
+ *   S is a ROTATION) because the SHA-256/384/512 description document
+ *   (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this
+ *   same "backwards" definition.
+ */
+/* Shift-right (used in SHA-256, SHA-384, and SHA-512): */
+#define R(b,x)                 ((x) >> (b))
+/* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
+#define S64(b,x)       (((x) >> (b)) | ((x) << (64 - (b))))
+
+/* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */
+#define Ch(x,y,z)      (((x) & (y)) ^ ((~(x)) & (z)))
+#define Maj(x,y,z)     (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
+
+/* Four of six logical functions used in SHA-384 and SHA-512: */
+#define Sigma0_512(x)  (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))
+#define Sigma1_512(x)  (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))
+#define sigma0_512(x)  (S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7,   (x)))
+#define sigma1_512(x)  (S64(19, (x)) ^ S64(61, (x)) ^ R( 6,   (x)))
+
+/*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
+/* Hash constant words K for SHA-384 and SHA-512: */
+static const sha2_word64 K512[80] = {
+       0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
+       0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
+       0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
+       0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
+       0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
+       0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
+       0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
+       0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
+       0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
+       0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
+       0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
+       0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
+       0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
+       0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
+       0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
+       0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
+       0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
+       0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
+       0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
+       0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
+       0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
+       0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
+       0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
+       0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
+       0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
+       0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
+       0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
+       0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
+       0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
+       0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
+       0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
+       0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
+       0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
+       0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
+       0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
+       0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
+       0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
+       0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
+       0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
+       0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
+};
+
+/* initial hash value H for SHA-512 */
+static const sha2_word64 sha512_initial_hash_value[8] = {
+       0x6a09e667f3bcc908ULL,
+       0xbb67ae8584caa73bULL,
+       0x3c6ef372fe94f82bULL,
+       0xa54ff53a5f1d36f1ULL,
+       0x510e527fade682d1ULL,
+       0x9b05688c2b3e6c1fULL,
+       0x1f83d9abfb41bd6bULL,
+       0x5be0cd19137e2179ULL
+};
+
+/*** SHA-512: *********************************************************/
+void SHA512_Init(SHA512_CTX* context) {
+       if (context == (SHA512_CTX*)0) {
+               return;
+       }
+       MEMCPY_BCOPY(context->state, sha512_initial_hash_value, SHA512_DIGEST_LENGTH);
+       MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH);
+       context->bitcount[0] = context->bitcount[1] =  0;
+}
+
+static void SHA512_Transform(SHA512_CTX* context,
+                                  const sha2_word64* data) {
+       sha2_word64     a, b, c, d, e, f, g, h, s0, s1;
+       sha2_word64     T1, T2, *W512 = (sha2_word64*)context->buffer;
+       int             j;
+
+       /* initialize registers with the prev. intermediate value */
+       a = context->state[0];
+       b = context->state[1];
+       c = context->state[2];
+       d = context->state[3];
+       e = context->state[4];
+       f = context->state[5];
+       g = context->state[6];
+       h = context->state[7];
+
+       j = 0;
+       do {
+#if BYTE_ORDER == LITTLE_ENDIAN
+               /* Convert TO host byte order */
+               REVERSE64(*data++, W512[j]);
+               /* Apply the SHA-512 compression function to update a..h */
+               T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
+#else /* BYTE_ORDER == LITTLE_ENDIAN */
+               /* Apply the SHA-512 compression function to update a..h with copy */
+               T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j] = *data++);
+#endif /* BYTE_ORDER == LITTLE_ENDIAN */
+               T2 = Sigma0_512(a) + Maj(a, b, c);
+               h = g;
+               g = f;
+               f = e;
+               e = d + T1;
+               d = c;
+               c = b;
+               b = a;
+               a = T1 + T2;
+
+               j++;
+       } while (j < 16);
+
+       do {
+               /* Part of the message block expansion: */
+               s0 = W512[(j+1)&0x0f];
+               s0 = sigma0_512(s0);
+               s1 = W512[(j+14)&0x0f];
+               s1 =  sigma1_512(s1);
+
+               /* Apply the SHA-512 compression function to update a..h */
+               T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
+                    (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);
+               T2 = Sigma0_512(a) + Maj(a, b, c);
+               h = g;
+               g = f;
+               f = e;
+               e = d + T1;
+               d = c;
+               c = b;
+               b = a;
+               a = T1 + T2;
+
+               j++;
+       } while (j < 80);
+
+       /* Compute the current intermediate hash value */
+       context->state[0] += a;
+       context->state[1] += b;
+       context->state[2] += c;
+       context->state[3] += d;
+       context->state[4] += e;
+       context->state[5] += f;
+       context->state[6] += g;
+       context->state[7] += h;
+
+       /* Clean up */
+       a = b = c = d = e = f = g = h = T1 = T2 = 0;
+}
+
+void SHA512_Update(SHA512_CTX* context, const sha2_byte *data, size_t len) {
+       size_t freespace, usedspace;
+
+       if (len == 0) {
+               /* Calling with no data is valid - we do nothing */
+               return;
+       }
+
+       /* Sanity check: */
+       assert(context != (SHA512_CTX*)0 && data != (sha2_byte*)0);
+
+       usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
+       if (usedspace > 0) {
+               /* Calculate how much free space is available in the buffer */
+               freespace = SHA512_BLOCK_LENGTH - usedspace;
+
+               if (len >= freespace) {
+                       /* Fill the buffer completely and process it */
+                       MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace);
+                       ADDINC128(context->bitcount, freespace << 3);
+                       len -= freespace;
+                       data += freespace;
+                       SHA512_Transform(context, (sha2_word64*)context->buffer);
+               } else {
+                       /* The buffer is not yet full */
+                       MEMCPY_BCOPY(&context->buffer[usedspace], data, len);
+                       ADDINC128(context->bitcount, len << 3);
+                       /* Clean up: */
+                       usedspace = freespace = 0;
+                       return;
+               }
+       }
+       while (len >= SHA512_BLOCK_LENGTH) {
+               /* Process as many complete blocks as we can */
+               SHA512_Transform(context, (sha2_word64*)data);
+               ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3);
+               len -= SHA512_BLOCK_LENGTH;
+               data += SHA512_BLOCK_LENGTH;
+       }
+       if (len > 0) {
+               /* There's left-overs, so save 'em */
+               MEMCPY_BCOPY(context->buffer, data, len);
+               ADDINC128(context->bitcount, len << 3);
+       }
+       /* Clean up: */
+       usedspace = freespace = 0;
+}
+
+static void SHA512_Last(SHA512_CTX* context) {
+       size_t usedspace;
+       ldns_sha2_buffer_union cast_var;
+
+       usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
+#if BYTE_ORDER == LITTLE_ENDIAN
+       /* Convert FROM host byte order */
+       REVERSE64(context->bitcount[0],context->bitcount[0]);
+       REVERSE64(context->bitcount[1],context->bitcount[1]);
+#endif
+       if (usedspace > 0) {
+               /* Begin padding with a 1 bit: */
+               context->buffer[usedspace++] = 0x80;
+
+               if (usedspace <= SHA512_SHORT_BLOCK_LENGTH) {
+                       /* Set-up for the last transform: */
+                       MEMSET_BZERO(&context->buffer[usedspace], SHA512_SHORT_BLOCK_LENGTH - usedspace);
+               } else {
+                       if (usedspace < SHA512_BLOCK_LENGTH) {
+                               MEMSET_BZERO(&context->buffer[usedspace], SHA512_BLOCK_LENGTH - usedspace);
+                       }
+                       /* Do second-to-last transform: */
+                       SHA512_Transform(context, (sha2_word64*)context->buffer);
+
+                       /* And set-up for the last transform: */
+                       MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH - 2);
+               }
+       } else {
+               /* Prepare for final transform: */
+               MEMSET_BZERO(context->buffer, SHA512_SHORT_BLOCK_LENGTH);
+
+               /* Begin padding with a 1 bit: */
+               *context->buffer = 0x80;
+       }
+       /* Store the length of input data (in bits): */
+       cast_var.theChars = context->buffer;
+       cast_var.theLongs[SHA512_SHORT_BLOCK_LENGTH / 8] = context->bitcount[1];
+       cast_var.theLongs[SHA512_SHORT_BLOCK_LENGTH / 8 + 1] = context->bitcount[0];
+
+       /* final transform: */
+       SHA512_Transform(context, (sha2_word64*)context->buffer);
+}
+
+void SHA512_Final(sha2_byte digest[], SHA512_CTX* context) {
+       sha2_word64     *d = (sha2_word64*)digest;
+
+       /* Sanity check: */
+       assert(context != (SHA512_CTX*)0);
+
+       /* If no digest buffer is passed, we don't bother doing this: */
+       if (digest != (sha2_byte*)0) {
+               SHA512_Last(context);
+
+               /* Save the hash data for output: */
+#if BYTE_ORDER == LITTLE_ENDIAN
+               {
+                       /* Convert TO host byte order */
+                       int     j;
+                       for (j = 0; j < 8; j++) {
+                               REVERSE64(context->state[j],context->state[j]);
+                               *d++ = context->state[j];
+                       }
+               }
+#else
+               MEMCPY_BCOPY(d, context->state, SHA512_DIGEST_LENGTH);
+#endif
+       }
+
+       /* Zero out state data */
+       MEMSET_BZERO(context, sizeof(SHA512_CTX));
+}
+
+unsigned char *
+SHA512(unsigned char *data, unsigned int data_len, unsigned char *digest)
+{
+    SHA512_CTX ctx;
+    SHA512_Init(&ctx);
+    SHA512_Update(&ctx, data, data_len);
+    SHA512_Final(digest, &ctx);
+    return digest;
+}
index 58928e23e2f998afe91f68d1f0f5364ed8cc8f76..c4a81a0a8f61ea0188ed8a04d6d20ac672bfdf15 100644 (file)
@@ -21,6 +21,9 @@
    internal symbols */
 #undef EXPORT_ALL_SYMBOLS
 
+/* Define to 1 if you have the `arc4random' function. */
+#undef HAVE_ARC4RANDOM
+
 /* Define to 1 if you have the `arc4random_uniform' function. */
 #undef HAVE_ARC4RANDOM_UNIFORM
 
 /* Whether getaddrinfo is available */
 #undef HAVE_GETADDRINFO
 
+/* Define to 1 if you have the `getentropy' function. */
+#undef HAVE_GETENTROPY
+
 /* Define to 1 if you have the <getopt.h> header file. */
 #undef HAVE_GETOPT_H
 
@@ -882,6 +888,20 @@ struct tm;
 char *strptime(const char *s, const char *format, struct tm *tm);
 #endif
 
+#if !defined(HAVE_ARC4RANDOM) || !defined(HAVE_ARC4RANDOM_UNIFORM)
+void explicit_bzero(void* buf, size_t len);
+int getentropy(void* buf, size_t len);
+#endif
+#ifndef HAVE_ARC4RANDOM
+uint32_t arc4random(void);
+void arc4random_buf(void* buf, size_t n);
+void _ARC4_LOCK(void);
+void _ARC4_UNLOCK(void);
+#endif
+#ifndef HAVE_ARC4RANDOM_UNIFORM
+uint32_t arc4random_uniform(uint32_t upper_bound);
+#endif
+
 #if defined(HAVE_EVENT_H) && !defined(HAVE_EVENT_BASE_ONCE) && !(defined(HAVE_EV_LOOP) || defined(HAVE_EV_DEFAULT_LOOP)) && (defined(HAVE_PTHREAD) || defined(HAVE_SOLARIS_THREADS))
    /* using version of libevent that is not threadsafe. */
 #  define LIBEVENT_SIGNAL_PROBLEM 1
index 573cfb8c29fd0cf2f58082cc28cf504cbf7748a6..8595704d318a6bb8cc91b2096549da3ace98e805 100755 (executable)
--- a/configure
+++ b/configure
@@ -640,6 +640,7 @@ UBSYMS
 EXTRALINK
 COMMON_OBJ_ALL_SYMBOLS
 LIBOBJ_WITHOUT_CTIME
+LIBOBJ_WITHOUT_CTIMEARC4
 WIN_CHECKCONF_OBJ_LINK
 WIN_CONTROL_OBJ_LINK
 WIN_UBANCHOR_OBJ_LINK
@@ -17935,7 +17936,7 @@ if test "$ac_res" != no; then :
 
 fi
 
-for ac_func in tzset sigprocmask fcntl getpwnam getrlimit setrlimit setsid sbrk chroot kill sleep usleep random srandom arc4random_uniform recvmsg sendmsg writev socketpair glob initgroups strftime localtime_r setusercontext _beginthreadex endservent endprotoent
+for ac_func in tzset sigprocmask fcntl getpwnam getrlimit setrlimit setsid sbrk chroot kill sleep usleep random srandom recvmsg sendmsg writev socketpair glob initgroups strftime localtime_r setusercontext _beginthreadex endservent endprotoent
 do :
   as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
 ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
@@ -18112,6 +18113,92 @@ esac
 fi
 
 
+LIBOBJ_WITHOUT_CTIMEARC4="$LIBOBJS"
+
+if test "$USE_NSS" = "no"; then
+       ac_fn_c_check_func "$LINENO" "arc4random" "ac_cv_func_arc4random"
+if test "x$ac_cv_func_arc4random" = xyes; then :
+  $as_echo "#define HAVE_ARC4RANDOM 1" >>confdefs.h
+
+else
+  case " $LIBOBJS " in
+  *" arc4random.$ac_objext "* ) ;;
+  *) LIBOBJS="$LIBOBJS arc4random.$ac_objext"
+ ;;
+esac
+
+fi
+
+
+       ac_fn_c_check_func "$LINENO" "arc4random_uniform" "ac_cv_func_arc4random_uniform"
+if test "x$ac_cv_func_arc4random_uniform" = xyes; then :
+  $as_echo "#define HAVE_ARC4RANDOM_UNIFORM 1" >>confdefs.h
+
+else
+  case " $LIBOBJS " in
+  *" arc4random_uniform.$ac_objext "* ) ;;
+  *) LIBOBJS="$LIBOBJS arc4random_uniform.$ac_objext"
+ ;;
+esac
+
+fi
+
+
+       if test "$ac_cv_func_arc4random" = "no" -o "$ac_cv_func_arc4random_uniform" = "no"; then
+               case " $LIBOBJS " in
+  *" explicit_bzero.$ac_objext "* ) ;;
+  *) LIBOBJS="$LIBOBJS explicit_bzero.$ac_objext"
+ ;;
+esac
+
+               case " $LIBOBJS " in
+  *" arc4_lock.$ac_objext "* ) ;;
+  *) LIBOBJS="$LIBOBJS arc4_lock.$ac_objext"
+ ;;
+esac
+
+       fi
+       for ac_func in getentropy
+do :
+  ac_fn_c_check_func "$LINENO" "getentropy" "ac_cv_func_getentropy"
+if test "x$ac_cv_func_getentropy" = xyes; then :
+  cat >>confdefs.h <<_ACEOF
+#define HAVE_GETENTROPY 1
+_ACEOF
+
+else
+
+               case `uname` in
+               Darwin)
+                       case " $LIBOBJS " in
+  *" getentropy_osx.$ac_objext "* ) ;;
+  *) LIBOBJS="$LIBOBJS getentropy_osx.$ac_objext"
+ ;;
+esac
+
+               ;;
+               SunOS)
+                       case " $LIBOBJS " in
+  *" getentropy_solaris.$ac_objext "* ) ;;
+  *) LIBOBJS="$LIBOBJS getentropy_solaris.$ac_objext"
+ ;;
+esac
+
+               ;;
+               Linux|*)
+                       case " $LIBOBJS " in
+  *" getentropy_linux.$ac_objext "* ) ;;
+  *) LIBOBJS="$LIBOBJS getentropy_linux.$ac_objext"
+ ;;
+esac
+
+               ;;
+               esac
+
+fi
+done
+
+fi
 LIBOBJ_WITHOUT_CTIME="$LIBOBJS"
 
 ac_fn_c_check_func "$LINENO" "ctime_r" "ac_cv_func_ctime_r"
index 190a3eba6f362a8a798d2a4d119db5911ec58e0d..508d0d288bfe43135f67288fbe23dff78392e9ab 100644 (file)
@@ -952,7 +952,7 @@ AC_INCLUDES_DEFAULT
 #endif
 ])
 AC_SEARCH_LIBS([setusercontext], [util])
-AC_CHECK_FUNCS([tzset sigprocmask fcntl getpwnam getrlimit setrlimit setsid sbrk chroot kill sleep usleep random srandom arc4random_uniform recvmsg sendmsg writev socketpair glob initgroups strftime localtime_r setusercontext _beginthreadex endservent endprotoent])
+AC_CHECK_FUNCS([tzset sigprocmask fcntl getpwnam getrlimit setrlimit setsid sbrk chroot kill sleep usleep random srandom recvmsg sendmsg writev socketpair glob initgroups strftime localtime_r setusercontext _beginthreadex endservent endprotoent])
 AC_CHECK_FUNCS([setresuid],,[AC_CHECK_FUNCS([setreuid])])
 AC_CHECK_FUNCS([setresgid],,[AC_CHECK_FUNCS([setregid])])
 
@@ -968,6 +968,37 @@ AC_REPLACE_FUNCS(strlcat)
 AC_REPLACE_FUNCS(strlcpy)
 AC_REPLACE_FUNCS(memmove)
 AC_REPLACE_FUNCS(gmtime_r)
+LIBOBJ_WITHOUT_CTIMEARC4="$LIBOBJS"
+AC_SUBST(LIBOBJ_WITHOUT_CTIMEARC4)
+if test "$USE_NSS" = "no"; then
+       AC_REPLACE_FUNCS(arc4random)
+       AC_REPLACE_FUNCS(arc4random_uniform)
+       if test "$ac_cv_func_arc4random" = "no" -o "$ac_cv_func_arc4random_uniform" = "no"; then
+               AC_LIBOBJ(explicit_bzero)
+               AC_LIBOBJ(arc4_lock)
+       fi
+       AC_CHECK_FUNCS([getentropy],,[
+               case `uname` in
+               Darwin)
+                       AC_LIBOBJ(getentropy_osx)
+               ;;
+               SunOS)
+                       AC_LIBOBJ(getentropy_solaris)
+                       AC_CHECK_HEADERS([sys/sha2.h],, [
+                               AC_CHECK_FUNCS([SHA512_Update],,[
+                                       AC_LIBOBJ(sha512)
+                               ])
+                       ], [AC_INCLUDES_DEFAULT])
+               ;;
+               Linux|*)
+                       AC_LIBOBJ(getentropy_linux)
+                       AC_CHECK_FUNCS([SHA512_Update],,[
+                               AC_LIBOBJ(sha512)
+                       ])
+               ;;
+               esac
+       ])
+fi
 LIBOBJ_WITHOUT_CTIME="$LIBOBJS"
 AC_SUBST(LIBOBJ_WITHOUT_CTIME)
 AC_REPLACE_FUNCS(ctime_r)
@@ -1161,6 +1192,20 @@ struct tm;
 char *strptime(const char *s, const char *format, struct tm *tm);
 #endif
 
+#if !defined(HAVE_ARC4RANDOM) || !defined(HAVE_ARC4RANDOM_UNIFORM)
+void explicit_bzero(void* buf, size_t len);
+int getentropy(void* buf, size_t len);
+#endif
+#ifndef HAVE_ARC4RANDOM
+uint32_t arc4random(void);
+void arc4random_buf(void* buf, size_t n);
+void _ARC4_LOCK(void);
+void _ARC4_UNLOCK(void);
+#endif
+#ifndef HAVE_ARC4RANDOM_UNIFORM
+uint32_t arc4random_uniform(uint32_t upper_bound);
+#endif
+
 #if defined(HAVE_EVENT_H) && !defined(HAVE_EVENT_BASE_ONCE) && !(defined(HAVE_EV_LOOP) || defined(HAVE_EV_DEFAULT_LOOP)) && (defined(HAVE_PTHREAD) || defined(HAVE_SOLARIS_THREADS))
    /* using version of libevent that is not threadsafe. */
 #  define LIBEVENT_SIGNAL_PROBLEM 1
index 8ea4dc554f83ca3dd4e2ac4746077c98c6815ca0..d5111329d9de1609e1b649ed10d8a98e98be8db5 100644 (file)
@@ -1,3 +1,9 @@
+11 July 2014: Wouter
+       - arc4random in compat/ and getentropy, explicit_bzero, chacha for
+         dependencies, from OpenBSD.  arc4_lock and sha512 in compat.
+         This makes arc4random available on all platforms, except when
+         compiled with LIBNSS (it uses libNSS crypto random).
+
 4 July 2014: Wouter
        - Fix #593: segfault or crash upon rotating logfile.
 
index 33d8c547f4a3ac0b9fb3962349801675b0685fe2..f55edd4a192b94433681bd60cbc17d7bea3cfa07 100644 (file)
@@ -667,15 +667,6 @@ count_unused(struct ip_list* p)
        return num;
 }
 
-static int get_random(void)
-{
-       int r;
-       if (RAND_bytes((unsigned char*)&r, (int)sizeof(r)) == 1) {
-               return r;
-       }
-       return (int)random();
-}
-
 /** pick random unused element from IP list */
 static struct ip_list*
 pick_random_ip(struct ip_list* list)
@@ -685,7 +676,7 @@ pick_random_ip(struct ip_list* list)
        int sel;
        if(num == 0) return NULL;
        /* not perfect, but random enough */
-       sel = get_random() % num;
+       sel = arc4random_uniform(num);
        /* skip over unused elements that we did not select */
        while(sel > 0 && p) {
                if(!p->used) sel--;
index 039af344a22b8015c5fb395a0eab167a3e7d2b4a..a235e722a5dcd43af9a9fd3706f598b5d743c362 100644 (file)
 #include "util/log.h"
 #include <time.h>
 
-#ifdef HAVE_ARC4RANDOM_UNIFORM
-/* no include for arc4_uniform */
-#elif defined(HAVE_SSL)
-#include <openssl/rand.h>
-#include <openssl/rc4.h>
-#include <openssl/err.h>
-#elif defined(HAVE_NSS)
+#ifdef HAVE_NSS
 /* nspr4 */
 #include "prerror.h"
 /* nss3 */
@@ -82,8 +76,7 @@
  */
 #define MAX_VALUE 0x7fffffff
 
-#ifdef HAVE_ARC4RANDOM_UNIFORM
-
+#ifndef HAVE_NSS
 void
 ub_systemseed(unsigned int ATTR_UNUSED(seed))
 {
@@ -109,147 +102,15 @@ ub_random(struct ub_randstate* ATTR_UNUSED(s))
        return (long)arc4random() & MAX_VALUE;
 }
 
-#elif defined(HAVE_SSL)
-/**
- * Struct with per-thread random state.
- * Keeps SSL types away from the header file.
- */
-struct ub_randstate {
-       /** key used for arc4random generation */
-       RC4_KEY rc4;
-       /** keeps track of key usage */
-       int rc4_ready;
-};
-
-/** Size of key to use (must be multiple of 8) */
-#define SEED_SIZE 24
-
-/** Number of bytes to reseed after */
-#define REKEY_BYTES    (1 << 24)
-
-/* (re)setup system seed */
-void
-ub_systemseed(unsigned int seed)
-{
-       /* RAND_ is threadsafe, by the way */
-       if(!RAND_status()) {
-               /* try to seed it */
-               unsigned char buf[256];
-               unsigned int v = seed;
-               size_t i;
-               for(i=0; i<256/sizeof(seed); i++) {
-                       memmove(buf+i*sizeof(seed), &v, sizeof(seed));
-                       v = v*seed + (unsigned int)i;
-               }
-               RAND_seed(buf, 256);
-               if(!RAND_status()) {
-                       log_err("Random generator has no entropy "
-                               "(error %ld)", ERR_get_error());
-               } else {
-                       verbose(VERB_OPS, "openssl has no entropy, "
-                               "seeding with time and pid");
-               }
-       }
-}
-
-/** reseed random generator */
-static void
-ub_arc4random_stir(struct ub_randstate* s, struct ub_randstate* from)
-{
-       /* not as unsigned char, but longerint so that it is
-          aligned properly on alignment sensitive platforms */
-       uint64_t rand_buf[SEED_SIZE/sizeof(uint64_t)];
-       int i;
-
-       memset(&s->rc4, 0, sizeof(s->rc4));
-       memset(rand_buf, 0xc, sizeof(rand_buf));
-       if (from) {
-               uint8_t* rbuf = (uint8_t*)rand_buf;
-               for(i=0; i<SEED_SIZE; i++)
-                       rbuf[i] = (uint8_t)ub_random(from);
-       } else {
-               if(!RAND_status())
-                       ub_systemseed((unsigned)getpid()^(unsigned)time(NULL));
-               if (RAND_bytes((unsigned char*)rand_buf,
-                       (int)sizeof(rand_buf)) <= 0) {
-                       /* very unlikely that this happens, since we seeded
-                        * above, if it does; complain and keep going */
-                       log_err("Couldn't obtain random bytes (error %ld)",
-                                   ERR_get_error());
-                       s->rc4_ready = 256;
-                       return;
-               }
-       }
-#ifdef HAVE_FIPS_MODE
-       if(FIPS_mode()) {
-               /* RC4 is not allowed, get some trustworthy randomness */
-               /* double certainty here, this routine should not be
-                * called in FIPS_mode */
-               memset(rand_buf, 0, sizeof(rand_buf));
-               s->rc4_ready = REKEY_BYTES;
-               return;
-       }
-#endif /* FIPS_MODE */
-       RC4_set_key(&s->rc4, SEED_SIZE, (unsigned char*)rand_buf);
-
-       /*
-        * Discard early keystream, as per recommendations in:
-        * http://www.wisdom.weizmann.ac.il/~itsik/RC4/Papers/Rc4_ksa.ps
-        */
-       for(i = 0; i <= 256; i += sizeof(rand_buf))
-               RC4(&s->rc4, sizeof(rand_buf), (unsigned char*)rand_buf,
-                       (unsigned char*)rand_buf);
-
-       memset(rand_buf, 0, sizeof(rand_buf));
-
-       s->rc4_ready = REKEY_BYTES;
-}
-
-struct ub_randstate* 
-ub_initstate(unsigned int seed, struct ub_randstate* from)
-{
-       struct ub_randstate* s = (struct ub_randstate*)calloc(1, sizeof(*s));
-       if(!s) {
-               log_err("malloc failure in random init");
-               return NULL;
-       }
-       ub_systemseed(seed);
-#ifdef HAVE_FIPS_MODE
-       if(!FIPS_mode())
-#endif
-       ub_arc4random_stir(s, from);
-       return s;
-}
-
-long int 
-ub_random(struct ub_randstate* s)
+long int
+ub_random_max(struct ub_randstate* state, long int x)
 {
-       unsigned int r = 0;
-#ifdef HAVE_FIPS_MODE
-       if(FIPS_mode()) {
-               /* RC4 is not allowed, get some trustworthy randomness */
-               /* we use pseudo bytes: it tries to return secure randomness
-                * but returns 'something' if that fails.  We need something
-                * else if it fails, because we cannot block here */
-               if(RAND_pseudo_bytes((unsigned char*)&r, (int)sizeof(r))
-                       == -1) {
-                       log_err("FIPSmode, no arc4random but RAND failed "
-                               "(error %ld)", ERR_get_error());
-               }
-               return (long int)((r) % (((unsigned)MAX_VALUE + 1)));
-       }
-#endif /* FIPS_MODE */
-       if (s->rc4_ready <= 0) {
-               ub_arc4random_stir(s, NULL);
-       }
-
-       RC4(&s->rc4, sizeof(r), 
-               (unsigned char *)&r, (unsigned char *)&r);
-       s->rc4_ready -= sizeof(r);
-       return (long int)((r) % (((unsigned)MAX_VALUE + 1)));
+       (void)state;
+       /* on OpenBSD, this does not need _seed(), or _stir() calls */
+       return (long)arc4random_uniform(x);
 }
 
-#elif defined(HAVE_NSS)
+#else
 
 /* not much to remember for NSS since we use its pk11_random, placeholder */
 struct ub_randstate {
@@ -283,12 +144,9 @@ long int ub_random(struct ub_randstate* ATTR_UNUSED(state))
        return x & MAX_VALUE;
 }
 
-#endif /* HAVE_ARC4RANDOM_UNIFORM or HAVE_SSL or HAVE_NSS */
-
 long int
 ub_random_max(struct ub_randstate* state, long int x)
 {
-#ifndef HAVE_ARC4RANDOM_UNIFORM
        /* make sure we fetch in a range that is divisible by x. ignore
         * values from d .. MAX_VALUE, instead draw a new number */
        long int d = MAX_VALUE - (MAX_VALUE % x); /* d is divisible by x */
@@ -296,12 +154,8 @@ ub_random_max(struct ub_randstate* state, long int x)
        while(d <= v)
                v = ub_random(state);
        return (v % x);
-#else
-       (void)state;
-       /* on OpenBSD, this does not need _seed(), or _stir() calls */
-       return (long)arc4random_uniform(x);
-#endif
 }
+#endif /* HAVE_NSS */
 
 void 
 ub_randfree(struct ub_randstate* s)