Merge branch 'fix/56' into develop

This closes #56 and #57
This commit is contained in:
Steffen Jaeckel 2016-10-10 00:48:15 +02:00
commit b8527e92f8
2 changed files with 29 additions and 2 deletions

View File

@ -15,7 +15,32 @@
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
*/ */
#if MP_GEN_RANDOM_MAX == 0xffffffff
#define MP_GEN_RANDOM_SHIFT 32
#elif MP_GEN_RANDOM_MAX == 32767
/* SHRT_MAX */
#define MP_GEN_RANDOM_SHIFT 15
#elif MP_GEN_RANDOM_MAX == 2147483647
/* INT_MAX */
#define MP_GEN_RANDOM_SHIFT 31
#elif !defined(MP_GEN_RANDOM_SHIFT)
#error Thou shalt define their own valid MP_GEN_RANDOM_SHIFT
#endif
/* makes a pseudo-random int of a given size */ /* makes a pseudo-random int of a given size */
static mp_digit mp_gen_random(void)
{
mp_digit d = 0, msk = 0;
do {
d <<= MP_GEN_RANDOM_SHIFT;
d |= ((mp_digit) MP_GEN_RANDOM());
msk <<= MP_GEN_RANDOM_SHIFT;
msk |= MP_GEN_RANDOM_MAX;
} while ((MP_MASK & msk) != MP_MASK);
d &= MP_MASK;
return d;
}
int int
mp_rand (mp_int * a, int digits) mp_rand (mp_int * a, int digits)
{ {
@ -29,7 +54,7 @@ mp_rand (mp_int * a, int digits)
/* first place a random non-zero digit */ /* first place a random non-zero digit */
do { do {
d = ((mp_digit) abs (MP_GEN_RANDOM())) & MP_MASK; d = mp_gen_random();
} while (d == 0); } while (d == 0);
if ((res = mp_add_d (a, d, a)) != MP_OKAY) { if ((res = mp_add_d (a, d, a)) != MP_OKAY) {
@ -41,7 +66,7 @@ mp_rand (mp_int * a, int digits)
return res; return res;
} }
if ((res = mp_add_d (a, ((mp_digit) abs (MP_GEN_RANDOM())), a)) != MP_OKAY) { if ((res = mp_add_d (a, mp_gen_random(), a)) != MP_OKAY) {
return res; return res;
} }
} }

View File

@ -102,8 +102,10 @@ extern "C" {
/* use arc4random on platforms that support it */ /* use arc4random on platforms that support it */
#ifdef MP_USE_ALT_RAND #ifdef MP_USE_ALT_RAND
#define MP_GEN_RANDOM() arc4random() #define MP_GEN_RANDOM() arc4random()
#define MP_GEN_RANDOM_MAX 0xffffffff
#else #else
#define MP_GEN_RANDOM() rand() #define MP_GEN_RANDOM() rand()
#define MP_GEN_RANDOM_MAX RAND_MAX
#endif #endif
#define MP_DIGIT_BIT DIGIT_BIT #define MP_DIGIT_BIT DIGIT_BIT