Skip to content

Commit 241c631

Browse files
committed
Merge pull request #206 from carbin/arc4random
use arc4random on platforms that support it
2 parents d74907d + ca209b6 commit 241c631

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

3rdparty/libtommath/bn_mp_rand.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ mp_rand (mp_int * a, int digits)
2929

3030
/* first place a random non-zero digit */
3131
do {
32-
d = ((mp_digit) abs (rand ())) & MP_MASK;
32+
d = ((mp_digit) abs (MP_GEN_RANDOM())) & MP_MASK;
3333
} while (d == 0);
3434

3535
if ((res = mp_add_d (a, d, a)) != MP_OKAY) {
@@ -41,7 +41,7 @@ mp_rand (mp_int * a, int digits)
4141
return res;
4242
}
4343

44-
if ((res = mp_add_d (a, ((mp_digit) abs (rand ())), a)) != MP_OKAY) {
44+
if ((res = mp_add_d (a, ((mp_digit) abs (MP_GEN_RANDOM())), a)) != MP_OKAY) {
4545
return res;
4646
}
4747
}

3rdparty/libtommath/tommath.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,18 @@ extern "C" {
127127
#define DIGIT_BIT ((int)((CHAR_BIT * sizeof(mp_digit) - 1))) /* bits per digit */
128128
#endif
129129

130+
/* platforms that can use a better rand function */
131+
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
132+
#define MP_USE_ALT_RAND 1
133+
#endif
134+
135+
/* use arc4random on platforms that support it */
136+
#ifdef MP_USE_ALT_RAND
137+
#define MP_GEN_RANDOM() arc4random()
138+
#else
139+
#define MP_GEN_RANDOM() rand()
140+
#endif
141+
130142
#define MP_DIGIT_BIT DIGIT_BIT
131143
#define MP_MASK ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
132144
#define MP_DIGIT_MAX MP_MASK

0 commit comments

Comments
 (0)