diff options
Diffstat (limited to 'src/lib/libcrypto/bn/bn_rand.c')
-rw-r--r-- | src/lib/libcrypto/bn/bn_rand.c | 37 |
1 files changed, 27 insertions, 10 deletions
diff --git a/src/lib/libcrypto/bn/bn_rand.c b/src/lib/libcrypto/bn/bn_rand.c index f68913473f..a5b163c820 100644 --- a/src/lib/libcrypto/bn/bn_rand.c +++ b/src/lib/libcrypto/bn/bn_rand.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: bn_rand.c,v 1.28 2023/07/08 12:21:58 beck Exp $ */ | 1 | /* $OpenBSD: bn_rand.c,v 1.29 2023/08/03 18:53:55 tb Exp $ */ |
2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
3 | * All rights reserved. | 3 | * All rights reserved. |
4 | * | 4 | * |
@@ -284,29 +284,46 @@ BN_rand_range(BIGNUM *r, const BIGNUM *range) | |||
284 | LCRYPTO_ALIAS(BN_rand_range); | 284 | LCRYPTO_ALIAS(BN_rand_range); |
285 | 285 | ||
286 | int | 286 | int |
287 | bn_rand_interval(BIGNUM *rnd, const BIGNUM *lower_inc, const BIGNUM *upper_exc) | 287 | bn_rand_in_range(BIGNUM *rnd, const BIGNUM *lower_inc, const BIGNUM *upper_exc) |
288 | { | 288 | { |
289 | BIGNUM *len = NULL; | 289 | BIGNUM *len; |
290 | int ret = 0; | 290 | int ret = 0; |
291 | 291 | ||
292 | if (BN_cmp(lower_inc, upper_exc) >= 0) | ||
293 | goto err; | ||
294 | |||
295 | if ((len = BN_new()) == NULL) | 292 | if ((len = BN_new()) == NULL) |
296 | goto err; | 293 | goto err; |
297 | |||
298 | if (!BN_sub(len, upper_exc, lower_inc)) | 294 | if (!BN_sub(len, upper_exc, lower_inc)) |
299 | goto err; | 295 | goto err; |
300 | 296 | if (!BN_rand_range(rnd, len)) | |
301 | if (!bn_rand_range(0, rnd, len)) | ||
302 | goto err; | 297 | goto err; |
303 | |||
304 | if (!BN_add(rnd, rnd, lower_inc)) | 298 | if (!BN_add(rnd, rnd, lower_inc)) |
305 | goto err; | 299 | goto err; |
306 | 300 | ||
307 | ret = 1; | 301 | ret = 1; |
302 | |||
308 | err: | 303 | err: |
309 | BN_free(len); | 304 | BN_free(len); |
305 | |||
306 | return ret; | ||
307 | } | ||
308 | |||
309 | int | ||
310 | bn_rand_interval(BIGNUM *rnd, BN_ULONG lower_word, const BIGNUM *upper_exc) | ||
311 | { | ||
312 | BIGNUM *lower_inc = NULL; | ||
313 | int ret = 0; | ||
314 | |||
315 | if ((lower_inc = BN_new()) == NULL) | ||
316 | goto err; | ||
317 | if (!BN_set_word(lower_inc, lower_word)) | ||
318 | goto err; | ||
319 | if (!bn_rand_in_range(rnd, lower_inc, upper_exc)) | ||
320 | goto err; | ||
321 | |||
322 | ret = 1; | ||
323 | |||
324 | err: | ||
325 | BN_free(lower_inc); | ||
326 | |||
310 | return ret; | 327 | return ret; |
311 | } | 328 | } |
312 | 329 | ||