5 BN_rand_ex, BN_rand, BN_priv_rand_ex, BN_priv_rand, BN_pseudo_rand,
6 BN_rand_range_ex, BN_rand_range, BN_priv_rand_range_ex, BN_priv_rand_range,
8 - generate pseudo-random number
12 #include <openssl/bn.h>
14 int BN_rand_ex(BIGNUM *rnd, int bits, int top, int bottom, BN_CTX *ctx);
15 int BN_rand(BIGNUM *rnd, int bits, int top, int bottom);
17 int BN_priv_rand_ex(BIGNUM *rnd, int bits, int top, int bottom, BN_CTX *ctx);
18 int BN_priv_rand(BIGNUM *rnd, int bits, int top, int bottom);
20 int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom);
22 int BN_rand_range_ex(BIGNUM *rnd, BIGNUM *range, BN_CTX *ctx);
23 int BN_rand_range(BIGNUM *rnd, BIGNUM *range);
25 int BN_priv_rand_range_ex(BIGNUM *rnd, BIGNUM *range, BN_CTX *ctx);
26 int BN_priv_rand_range(BIGNUM *rnd, BIGNUM *range);
28 int BN_pseudo_rand_range(BIGNUM *rnd, BIGNUM *range);
32 BN_rand_ex() generate a cryptographically strong pseudo-random
33 number of B<bits> in length and stores it in B<rnd> using the random number
34 generator for the library context associated with B<ctx>. The parameter B<ctx>
35 may be NULL in which case the default library context is used.
36 If B<bits> is less than zero, or too small to
37 accommodate the requirements specified by the B<top> and B<bottom>
38 parameters, an error is returned.
39 The B<top> parameters specifies
40 requirements on the most significant bit of the generated number.
41 If it is B<BN_RAND_TOP_ANY>, there is no constraint.
42 If it is B<BN_RAND_TOP_ONE>, the top bit must be one.
43 If it is B<BN_RAND_TOP_TWO>, the two most significant bits of
44 the number will be set to 1, so that the product of two such random
45 numbers will always have 2*B<bits> length.
46 If B<bottom> is B<BN_RAND_BOTTOM_ODD>, the number will be odd; if it
47 is B<BN_RAND_BOTTOM_ANY> it can be odd or even.
48 If B<bits> is 1 then B<top> cannot also be B<BN_RAND_FLG_TOPTWO>.
50 BN_rand() is the same as BN_rand_ex() except that the default library context
53 BN_rand_range_ex() generates a cryptographically strong pseudo-random
54 number B<rnd> in the range 0 E<lt>= B<rnd> E<lt> B<range> using the random number
55 generator for the library context associated with B<ctx>. The parameter B<ctx>
56 may be NULL in which case the default library context is used.
58 BN_rand_range() is the same as BN_rand_range_ex() except that the default
59 library context is always used.
61 BN_priv_rand_ex(), BN_priv_rand(), BN_priv_rand_rand_ex() and
62 BN_priv_rand_range() have the same semantics as BN_rand_ex(), BN_rand(),
63 BN_rand_range_ex() and BN_rand_range() respectively. They are intended to be
64 used for generating values that should remain private, and mirror the
65 same difference between L<RAND_bytes(3)> and L<RAND_priv_bytes(3)>.
69 Always check the error return value of these functions and do not take
70 randomness for granted: an error occurs if the CSPRNG has not been
71 seeded with enough randomness to ensure an unpredictable byte sequence.
75 The functions return 1 on success, 0 on error.
76 The error codes can be obtained by L<ERR_get_error(3)>.
83 L<RAND_priv_bytes(3)>,
93 Starting with OpenSSL release 1.1.0, BN_pseudo_rand() has been identical
94 to BN_rand() and BN_pseudo_rand_range() has been identical to
96 The "pseudo" functions should not be used and may be deprecated in
102 BN_priv_rand() and BN_priv_rand_range() functions were added in OpenSSL 1.1.1.
106 The BN_rand_ex(), BN_priv_rand_ex(), BN_rand_range_ex() and
107 BN_priv_rand_range_ex() functions were added in OpenSSL 3.0.
113 Copyright 2000-2019 The OpenSSL Project Authors. All Rights Reserved.
115 Licensed under the Apache License 2.0 (the "License"). You may not use
116 this file except in compliance with the License. You can obtain a copy
117 in the file LICENSE in the source distribution or at
118 L<https://www.openssl.org/source/license.html>.