Commit 784077d38042b835679fb95247f5f96d2dede538

Steffen Jaeckel 2014-12-11T14:12:14

Merge remote-tracking branch 'carbin/arc' into develop This closes #28

diff --git a/bn_mp_rand.c b/bn_mp_rand.c
index aba52df..ff5bff3 100644
--- a/bn_mp_rand.c
+++ b/bn_mp_rand.c
@@ -29,7 +29,7 @@ mp_rand (mp_int * a, int digits)
 
   /* first place a random non-zero digit */
   do {
-    d = ((mp_digit) abs (rand ())) & MP_MASK;
+    d = ((mp_digit) abs (MP_GEN_RANDOM())) & MP_MASK;
   } while (d == 0);
 
   if ((res = mp_add_d (a, d, a)) != MP_OKAY) {
@@ -41,7 +41,7 @@ mp_rand (mp_int * a, int digits)
       return res;
     }
 
-    if ((res = mp_add_d (a, ((mp_digit) abs (rand ())), a)) != MP_OKAY) {
+    if ((res = mp_add_d (a, ((mp_digit) abs (MP_GEN_RANDOM())), a)) != MP_OKAY) {
       return res;
     }
   }
diff --git a/tommath.h b/tommath.h
index 8af8af2..d662c3e 100644
--- a/tommath.h
+++ b/tommath.h
@@ -138,6 +138,17 @@ extern "C" {
    typedef mp_digit mp_min_u32;
 #endif
 
+/* platforms that can use a better rand function */
+#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
+    #define MP_USE_ALT_RAND 1
+#endif
+
+/* use arc4random on platforms that support it */
+#ifdef MP_USE_ALT_RAND
+    #define MP_GEN_RANDOM()    arc4random()
+#else
+    #define MP_GEN_RANDOM()    rand()
+#endif
 
 #define MP_DIGIT_BIT     DIGIT_BIT
 #define MP_MASK          ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))