summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bn/bn_rand.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/bn/bn_rand.c')
-rw-r--r--src/lib/libcrypto/bn/bn_rand.c24
1 files changed, 19 insertions, 5 deletions
diff --git a/src/lib/libcrypto/bn/bn_rand.c b/src/lib/libcrypto/bn/bn_rand.c
index 893c9d2af9..f51830b12b 100644
--- a/src/lib/libcrypto/bn/bn_rand.c
+++ b/src/lib/libcrypto/bn/bn_rand.c
@@ -134,13 +134,13 @@ static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)
134 buf=(unsigned char *)OPENSSL_malloc(bytes); 134 buf=(unsigned char *)OPENSSL_malloc(bytes);
135 if (buf == NULL) 135 if (buf == NULL)
136 { 136 {
137 BNerr(BN_F_BN_RAND,ERR_R_MALLOC_FAILURE); 137 BNerr(BN_F_BNRAND,ERR_R_MALLOC_FAILURE);
138 goto err; 138 goto err;
139 } 139 }
140 140
141 /* make a random number and set the top and bottom bits */ 141 /* make a random number and set the top and bottom bits */
142 time(&tim); 142 time(&tim);
143 RAND_add(&tim,sizeof(tim),0); 143 RAND_add(&tim,sizeof(tim),0.0);
144 144
145 if (pseudorand) 145 if (pseudorand)
146 { 146 {
@@ -204,6 +204,7 @@ err:
204 OPENSSL_cleanse(buf,bytes); 204 OPENSSL_cleanse(buf,bytes);
205 OPENSSL_free(buf); 205 OPENSSL_free(buf);
206 } 206 }
207 bn_check_top(rnd);
207 return(ret); 208 return(ret);
208 } 209 }
209 210
@@ -230,6 +231,7 @@ static int bn_rand_range(int pseudo, BIGNUM *r, BIGNUM *range)
230 { 231 {
231 int (*bn_rand)(BIGNUM *, int, int, int) = pseudo ? BN_pseudo_rand : BN_rand; 232 int (*bn_rand)(BIGNUM *, int, int, int) = pseudo ? BN_pseudo_rand : BN_rand;
232 int n; 233 int n;
234 int count = 100;
233 235
234 if (range->neg || BN_is_zero(range)) 236 if (range->neg || BN_is_zero(range))
235 { 237 {
@@ -242,9 +244,7 @@ static int bn_rand_range(int pseudo, BIGNUM *r, BIGNUM *range)
242 /* BN_is_bit_set(range, n - 1) always holds */ 244 /* BN_is_bit_set(range, n - 1) always holds */
243 245
244 if (n == 1) 246 if (n == 1)
245 { 247 BN_zero(r);
246 if (!BN_zero(r)) return 0;
247 }
248 else if (!BN_is_bit_set(range, n - 2) && !BN_is_bit_set(range, n - 3)) 248 else if (!BN_is_bit_set(range, n - 2) && !BN_is_bit_set(range, n - 3))
249 { 249 {
250 /* range = 100..._2, 250 /* range = 100..._2,
@@ -263,6 +263,13 @@ static int bn_rand_range(int pseudo, BIGNUM *r, BIGNUM *range)
263 if (BN_cmp(r, range) >= 0) 263 if (BN_cmp(r, range) >= 0)
264 if (!BN_sub(r, r, range)) return 0; 264 if (!BN_sub(r, r, range)) return 0;
265 } 265 }
266
267 if (!--count)
268 {
269 BNerr(BN_F_BN_RAND_RANGE, BN_R_TOO_MANY_ITERATIONS);
270 return 0;
271 }
272
266 } 273 }
267 while (BN_cmp(r, range) >= 0); 274 while (BN_cmp(r, range) >= 0);
268 } 275 }
@@ -272,10 +279,17 @@ static int bn_rand_range(int pseudo, BIGNUM *r, BIGNUM *range)
272 { 279 {
273 /* range = 11..._2 or range = 101..._2 */ 280 /* range = 11..._2 or range = 101..._2 */
274 if (!bn_rand(r, n, -1, 0)) return 0; 281 if (!bn_rand(r, n, -1, 0)) return 0;
282
283 if (!--count)
284 {
285 BNerr(BN_F_BN_RAND_RANGE, BN_R_TOO_MANY_ITERATIONS);
286 return 0;
287 }
275 } 288 }
276 while (BN_cmp(r, range) >= 0); 289 while (BN_cmp(r, range) >= 0);
277 } 290 }
278 291
292 bn_check_top(r);
279 return 1; 293 return 1;
280 } 294 }
281 295