Hash :
c8cc3657
Author :
Date :
2019-05-11T09:09:12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include "tommath_private.h"
#ifdef BN_MP_RAND_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
int (*s_mp_rand_source)(void *, size_t) = s_mp_rand_source_platform;
void mp_rand_source(int (*source)(void *out, size_t size))
{
s_mp_rand_source = (source == NULL) ? s_mp_rand_source_platform : source;
}
/* makes a pseudo-random int of a given size */
int mp_rand_digit(mp_digit *r)
{
int ret = s_mp_rand_source(r, sizeof(mp_digit));
*r &= MP_MASK;
return ret;
}
int mp_rand(mp_int *a, int digits)
{
int ret, i;
mp_zero(a);
if (digits <= 0) {
return MP_OKAY;
}
if ((ret = mp_grow(a, digits)) != MP_OKAY) {
return ret;
}
if ((ret = s_mp_rand_source(a->dp, (size_t)digits * sizeof(mp_digit))) != MP_OKAY) {
return ret;
}
/* TODO: We ensure that the highest digit is nonzero. Should this be removed? */
while ((a->dp[digits - 1] & MP_MASK) == 0) {
if ((ret = s_mp_rand_source(a->dp + digits - 1, sizeof(mp_digit))) != MP_OKAY) {
return ret;
}
}
a->used = digits;
for (i = 0; i < digits; ++i) {
a->dp[i] &= MP_MASK;
}
return MP_OKAY;
}
#endif