112 #if defined( INC_ALL )
118 #if defined( USE_ECDH ) || defined( USE_ECDSA )
122 int generateBignum(
BIGNUM *bn,
const int noBits,
const int high,
const int low );
124 static int bnrand(
int pseudorand,
BIGNUM *rnd,
int bits,
int top,
int bottom)
128 status = generateBignum( rnd, bits, top, bottom );
129 return( status ? 0 : 1 );
133 static int bnrand(
int pseudorand,
BIGNUM *rnd,
int bits,
int top,
int bottom)
135 unsigned char *buf=NULL;
136 int ret=0,bit,bytes,mask;
149 buf=(
unsigned char *)
clBnAlloc(
"bnrand", bytes);
158 RAND_add(&tim,
sizeof(tim),0.0);
162 if (RAND_pseudo_bytes(buf, bytes) == -1)
167 if (RAND_bytes(buf, bytes) <= 0)
179 for (i = 0; i < bytes; i++)
181 RAND_pseudo_bytes(&c, 1);
182 if (c >= 128 && i > 0)
203 buf[0]|=(3<<(bit-1));
229 return bnrand(0, rnd, bits, top, bottom);
234 return bnrand(1, rnd, bits, top, bottom);
240 return bnrand(2, rnd, bits, top, bottom);
246 static int bn_rand_range(
int pseudo,
BIGNUM *r,
const BIGNUM *range)
270 if (!bn_rand(r, n + 1, -1, 0))
return 0;
276 if (
BN_cmp(r ,range) >= 0)
278 if (!
BN_sub(r, r, range))
return 0;
279 if (
BN_cmp(r, range) >= 0)
280 if (!
BN_sub(r, r, range))
return 0;
290 while (
BN_cmp(r, range) >= 0);
297 if (!bn_rand(r, n, -1, 0))
return 0;
305 while (
BN_cmp(r, range) >= 0);
315 return bn_rand_range(0, r, range);
320 return bn_rand_range(1, r, range);