From 5d1096b0bdb01414f0721a1e6dc2ed5bd6f685e3 Mon Sep 17 00:00:00 2001 From: Carlin Date: Fri, 12 Dec 2014 01:42:30 +1300 Subject: [PATCH] use arc4random() instead of rand() on *BSD --- bn_mp_rand.c | 4 ++-- tommath.h | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) 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))