]> git.ipfire.org Git - thirdparty/libbsd.git/commitdiff
Add arc4random_stir and arc4random_addrandom functions
authorGuillem Jover <guillem@hadrons.org>
Wed, 18 Jun 2008 05:44:18 +0000 (08:44 +0300)
committerGuillem Jover <guillem@hadrons.org>
Wed, 18 Jun 2008 05:44:18 +0000 (08:44 +0300)
Makefile
Versions
include/bsd/random.h
src/arc4random.c

index ca511237e32b63818d2a58f1ed4e99fe70e1fbd3..48a2c4e146c3da6f1c22cff495b2a39e19f1bd3b 100644 (file)
--- 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)
 
index 2bec4c585cccfd026b36896b41b1fb0b9d00713f..b4e8392703baeb54d2cd66b50dcb0cbe76bb79c0 100644 (file)
--- 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;
index f3ab000b4858b74895d506ca573f19d324b3ed5f..9999a8d28b3909ad0faed40211505385a979f36d 100644 (file)
@@ -30,6 +30,8 @@
 #include <sys/types.h>
 
 u_int32_t arc4random();
+void arc4random_stir();
+void arc4random_addrandom(u_char *dat, int datlen);
 
 #endif
 
index 22dae59e82c4c6f39db0222667a29838eccc4305..5c1837ef5086fb81ef3149f9e885764f7ddefd05 100644 (file)
@@ -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()
 {