]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: Use arc4random if present
authorAki Tuomi <aki.tuomi@dovecot.fi>
Sun, 24 Apr 2016 18:06:41 +0000 (21:06 +0300)
committerAki Tuomi <aki.tuomi@dovecot.fi>
Mon, 9 May 2016 07:52:24 +0000 (10:52 +0300)
configure.ac
m4/arc4random.m4 [new file with mode: 0644]
src/lib/rand.c
src/lib/rand.h
src/lib/randgen.c

index 214a7d421d598a6bbac40d150b3a8a04ac3dd3d9..7e8d56fa9aaa3df831128f2bc93631e0d2dcbc98 100644 (file)
@@ -324,6 +324,8 @@ DOVECOT_FDATASYNC
 DOVECOT_LIBCAP
 DOVECOT_LIBWRAP
 
+DOVECOT_ARC4RANDOM
+
 AC_DEFINE(PACKAGE_WEBPAGE, "http://www.dovecot.org/", [Support URL])
 
 dnl * after -lsocket and -lnsl tests, inet_aton() may be in them
diff --git a/m4/arc4random.m4 b/m4/arc4random.m4
new file mode 100644 (file)
index 0000000..a4540c6
--- /dev/null
@@ -0,0 +1,11 @@
+AC_DEFUN([DOVECOT_ARC4RANDOM], [
+  AC_CHECK_FUNC([arc4random], [
+    AC_DEFINE([HAVE_ARC4RANDOM], [1], [Define this if you arc4random()])
+  ], [
+      AC_CHECK_LIB([bsd], [arc4random], [
+        LIBS="$LIBS -lbsd"
+        AC_DEFINE([HAVE_ARC4RANDOM], [1], [Define this if you arc4random()])
+        AC_DEFINE([HAVE_LIBBSD], [1], [Define this if you have libbsd])
+      ])
+  ])
+])
index bdcf6795de5b32855b331a8052d2263049ae7b42..1c338e2b3f7a86f59dbc79fdc3b46d7834bed6a7 100644 (file)
@@ -31,3 +31,14 @@ void rand_set_seed(unsigned int s)
 
        srand(seed);
 }
+
+#ifdef HAVE_ARC4RANDOM
+#ifdef HAVE_LIBBSD
+#include <bsd/stdlib.h>
+#endif
+
+/* this returns [0,RAND_MAX), to keep it compatible with rand() */
+int arc4random_rand(void) {
+       return (int)(arc4random() % ((unsigned)RAND_MAX + 1));
+}
+#endif
index 4382af687bbc18b6a13bd5a0ac69d3f5f734484b..b0de58f514bb35ace481e8408e4ed4fbfd882adc 100644 (file)
@@ -16,4 +16,11 @@ unsigned int rand_get_last_seed(void);
 /* Actually seed the prng (could add char* for name of function?) */
 void rand_set_seed(unsigned int s);
 
+#ifdef HAVE_ARC4RANDOM
+
+int arc4random_rand(void);
+#define rand arc4random_rand
+
+#endif
+
 #endif
index d3e321e05fa8c9b0de932c7a2854dbb3c3744a12..ff73cb486892d46286f13202a28a2cbbd6693feb 100644 (file)
@@ -61,6 +61,18 @@ void random_deinit(void)
        i_close_fd(&urandom_fd);
 }
 
+#ifdef HAVE_ARC4RANDOM
+#ifdef HAVE_LIBBSD
+#include <bsd/stdlib.h>
+#endif
+
+void random_fill_weak(void *buf, size_t size)
+{
+       arc4random_buf(buf, size);
+}
+
+#else
+
 void random_fill_weak(void *buf, size_t size)
 {
        unsigned char *cbuf = buf;
@@ -68,3 +80,5 @@ void random_fill_weak(void *buf, size_t size)
        for (; size > 0; size--)
                *cbuf++ = (unsigned char)rand();
 }
+
+#endif