Add arc4random_stir and arc4random_addrandom functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
diff --git a/Makefile b/Makefile
index ca51123..48a2c4e 100644
--- a/Makefile
+++ b/Makefile
@@ -33,7 +33,7 @@ LIB_SHARED_SO = $(LIB_NAME).so
LIB_SONAME = $(LIB_SHARED_SO).$(LIB_VERSION_MAJOR)
LIB_SHARED = $(LIB_SONAME).$(LIB_VERSION_MINOR)
-MK_CFLAGS = -Iinclude/ -include bsd/bsd.h -D_GNU_SOURCE
+MK_CFLAGS = -Iinclude/ -include bsd/bsd.h -D_GNU_SOURCE -D__REENTRANT
libs: $(LIB_STATIC) $(LIB_SHARED_SO)
diff --git a/Versions b/Versions
index 2bec4c5..b4e8392 100644
--- a/Versions
+++ b/Versions
@@ -1,6 +1,8 @@
LIBBSD_0.0 {
global:
arc4random;
+ arc4random_stir;
+ arc4random_addrandom;
bsd_getopt; optreset;
errc; warnc; verrc; vwarnc;
fgetln;
diff --git a/include/bsd/random.h b/include/bsd/random.h
index f3ab000..9999a8d 100644
--- a/include/bsd/random.h
+++ b/include/bsd/random.h
@@ -30,6 +30,8 @@
#include <sys/types.h>
u_int32_t arc4random();
+void arc4random_stir();
+void arc4random_addrandom(u_char *dat, int datlen);
#endif
diff --git a/src/arc4random.c b/src/arc4random.c
index 22dae59..5c1837e 100644
--- a/src/arc4random.c
+++ b/src/arc4random.c
@@ -31,6 +31,7 @@ __FBSDID("$FreeBSD: src/lib/libc/gen/arc4random.c,v 1.10 2004/03/24 14:44:57 gre
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
+#include <pthread.h>
struct arc4_stream {
u_int8_t i;
@@ -39,8 +40,14 @@ struct arc4_stream {
};
#define RANDOMDEV "/dev/urandom"
+#ifdef __REENTRANT
+static pthread_mutex_t arc4random_mtx = PTHREAD_MUTEX_INITIALIZER;
+#define THREAD_LOCK() pthread_mutex_lock(&arc4random_mtx)
+#define THREAD_UNLOCK() pthread_mutex_unlock(&arc4random_mtx)
+#else
#define THREAD_LOCK()
#define THREAD_UNLOCK()
+#endif
static struct arc4_stream rs;
static int rs_initialized;
@@ -155,6 +162,27 @@ arc4_check_stir(void)
}
}
+void
+arc4random_stir()
+{
+ THREAD_LOCK();
+ arc4_check_init();
+ arc4_stir(&rs);
+ THREAD_UNLOCK();
+}
+
+void
+arc4random_addrandom(dat, datlen)
+ u_char *dat;
+ int datlen;
+{
+ THREAD_LOCK();
+ arc4_check_init();
+ arc4_check_stir();
+ arc4_addrandom(&rs, dat, datlen);
+ THREAD_UNLOCK();
+}
+
u_int32_t
arc4random()
{