Merge pull request #147 from czurnieden/moarvm_workarounds

workaround for the MoarVM problem with MP_GEN_RAND et al.
(cherry picked from commit 11d6e03914)
This commit is contained in:
Steffen Jaeckel 2019-01-15 18:20:24 +01:00 committed by Steffen Jaeckel
parent e4e2da5143
commit 1c8c534885
4 changed files with 15 additions and 10 deletions

View File

@ -172,7 +172,7 @@ static int s_rand_digit(mp_digit *p)
} }
/* makes a pseudo-random int of a given size */ /* makes a pseudo-random int of a given size */
static int s_gen_random(mp_digit *r) int mp_rand_digit(mp_digit *r)
{ {
int ret = s_rand_digit(r); int ret = s_rand_digit(r);
*r &= MP_MASK; *r &= MP_MASK;
@ -191,7 +191,7 @@ int mp_rand(mp_int *a, int digits)
/* first place a random non-zero digit */ /* first place a random non-zero digit */
do { do {
if (s_gen_random(&d) != MP_OKAY) { if (mp_rand_digit(&d) != MP_OKAY) {
return MP_VAL; return MP_VAL;
} }
} while (d == 0u); } while (d == 0u);
@ -205,7 +205,7 @@ int mp_rand(mp_int *a, int digits)
return res; return res;
} }
if (s_gen_random(&d) != MP_OKAY) { if (mp_rand_digit(&d) != MP_OKAY) {
return MP_VAL; return MP_VAL;
} }
if ((res = mp_add_d(a, d, a)) != MP_OKAY) { if ((res = mp_add_d(a, d, a)) != MP_OKAY) {

View File

@ -2003,14 +2003,19 @@ mp\_prime\_random().
\chapter{Random Number Generation} \chapter{Random Number Generation}
\section{PRNG} \section{PRNG}
\index{mp\_rand\_digit}
\begin{alltt}
int mp_rand_digit(mp_digit *r)
\end{alltt}
This function generates a random number in \texttt{r} of the size given in \texttt{r} (that is, the variable is used for in- and output) but not more than \texttt{MP_MASK} bits.
\index{mp\_rand} \index{mp\_rand}
\begin{alltt} \begin{alltt}
int mp_rand(mp_int *a, int digits) int mp_rand(mp_int *a, int digits)
\end{alltt} \end{alltt}
The function generates a random number of \texttt{digits} bits. This function generates a random number of \texttt{digits} bits.
This random number is cryptographically secure if the source of random numbers the operating systems offers is cryptographically secure. The random number generated with these two functions is cryptographically secure if the source of random numbers the operating systems offers is cryptographically secure. It will use \texttt{arc4random()} if the OS is a BSD flavor, Wincrypt on Windows, or \texttt{\dev\urandom} on all operating systems that have it.
It will use \texttt{arc4random()} if the OS is a BSD flavor, Wincrypt on Windows, and \texttt{\\dev\\urandom} on all operating systems that have it.
\chapter{Input and Output} \chapter{Input and Output}

View File

@ -277,8 +277,10 @@ int mp_cnt_lsb(const mp_int *a);
/* I Love Earth! */ /* I Love Earth! */
/* makes a pseudo-random int of a given size */ /* makes a pseudo-random mp_int of a given size */
int mp_rand(mp_int *a, int digits); int mp_rand(mp_int *a, int digits);
/* makes a pseudo-random small int of a given size */
int mp_rand_digit(mp_digit *r);
#ifdef MP_PRNG_ENABLE_LTM_RNG #ifdef MP_PRNG_ENABLE_LTM_RNG
/* A last resort to provide random data on systems without any of the other /* A last resort to provide random data on systems without any of the other

View File

@ -9,7 +9,6 @@
* *
* SPDX-License-Identifier: Unlicense * SPDX-License-Identifier: Unlicense
*/ */
#if !(defined(LTM1) && defined(LTM2) && defined(LTM3)) #if !(defined(LTM1) && defined(LTM2) && defined(LTM3))
#if defined(LTM2) #if defined(LTM2)
# define LTM3 # define LTM3
@ -18,7 +17,6 @@
# define LTM2 # define LTM2
#endif #endif
#define LTM1 #define LTM1
#if defined(LTM_ALL) #if defined(LTM_ALL)
# define BN_ERROR_C # define BN_ERROR_C
# define BN_FAST_MP_INVMOD_C # define BN_FAST_MP_INVMOD_C
@ -160,7 +158,6 @@
# define BN_S_MP_SUB_C # define BN_S_MP_SUB_C
# define BNCORE_C # define BNCORE_C
#endif #endif
#if defined(BN_ERROR_C) #if defined(BN_ERROR_C)
# define BN_MP_ERROR_TO_STRING_C # define BN_MP_ERROR_TO_STRING_C
#endif #endif
@ -828,6 +825,7 @@
#endif #endif
#if defined(BN_MP_RAND_C) #if defined(BN_MP_RAND_C)
# define BN_MP_RAND_DIGIT_C
# define BN_MP_ZERO_C # define BN_MP_ZERO_C
# define BN_MP_ADD_D_C # define BN_MP_ADD_D_C
# define BN_MP_LSHD_C # define BN_MP_LSHD_C