xref: /openssl/doc/man3/BN_rand.pod (revision fecb3aae)
1=pod
2
3=head1 NAME
4
5BN_rand_ex, BN_rand, BN_priv_rand_ex, BN_priv_rand, BN_pseudo_rand,
6BN_rand_range_ex, BN_rand_range, BN_priv_rand_range_ex, BN_priv_rand_range,
7BN_pseudo_rand_range
8- generate pseudo-random number
9
10=head1 SYNOPSIS
11
12 #include <openssl/bn.h>
13
14 int BN_rand_ex(BIGNUM *rnd, int bits, int top, int bottom,
15                unsigned int strength, BN_CTX *ctx);
16 int BN_rand(BIGNUM *rnd, int bits, int top, int bottom);
17
18 int BN_priv_rand_ex(BIGNUM *rnd, int bits, int top, int bottom,
19                     unsigned int strength, BN_CTX *ctx);
20 int BN_priv_rand(BIGNUM *rnd, int bits, int top, int bottom);
21
22 int BN_rand_range_ex(BIGNUM *rnd, const BIGNUM *range, unsigned int strength,
23                      BN_CTX *ctx);
24 int BN_rand_range(BIGNUM *rnd, const BIGNUM *range);
25
26 int BN_priv_rand_range_ex(BIGNUM *rnd, const BIGNUM *range, unsigned int strength,
27                           BN_CTX *ctx);
28 int BN_priv_rand_range(BIGNUM *rnd, const BIGNUM *range);
29
30The following functions have been deprecated since OpenSSL 3.0, and can be
31hidden entirely by defining B<OPENSSL_API_COMPAT> with a suitable version value,
32see L<openssl_user_macros(7)>:
33
34 int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom);
35 int BN_pseudo_rand_range(BIGNUM *rnd, const BIGNUM *range);
36
37=head1 DESCRIPTION
38
39BN_rand_ex() generates a cryptographically strong pseudo-random
40number of I<bits> in length and security strength at least I<strength> bits
41using the random number generator for the library context associated with
42I<ctx>. The function stores the generated data in I<rnd>. The parameter I<ctx>
43may be NULL in which case the default library context is used.
44If I<bits> is less than zero, or too small to
45accommodate the requirements specified by the I<top> and I<bottom>
46parameters, an error is returned.
47The I<top> parameters specifies
48requirements on the most significant bit of the generated number.
49If it is B<BN_RAND_TOP_ANY>, there is no constraint.
50If it is B<BN_RAND_TOP_ONE>, the top bit must be one.
51If it is B<BN_RAND_TOP_TWO>, the two most significant bits of
52the number will be set to 1, so that the product of two such random
53numbers will always have 2*I<bits> length.
54If I<bottom> is B<BN_RAND_BOTTOM_ODD>, the number will be odd; if it
55is B<BN_RAND_BOTTOM_ANY> it can be odd or even.
56If I<bits> is 1 then I<top> cannot also be B<BN_RAND_TOP_TWO>.
57
58BN_rand() is the same as BN_rand_ex() except that the default library context
59is always used.
60
61BN_rand_range_ex() generates a cryptographically strong pseudo-random
62number I<rnd>, of security strength at least I<strength> bits,
63in the range 0 E<lt>= I<rnd> E<lt> I<range> using the random number
64generator for the library context associated with I<ctx>. The parameter I<ctx>
65may be NULL in which case the default library context is used.
66
67BN_rand_range() is the same as BN_rand_range_ex() except that the default
68library context is always used.
69
70BN_priv_rand_ex(), BN_priv_rand(), BN_priv_rand_rand_ex() and
71BN_priv_rand_range() have the same semantics as BN_rand_ex(), BN_rand(),
72BN_rand_range_ex() and BN_rand_range() respectively.  They are intended to be
73used for generating values that should remain private, and mirror the
74same difference between L<RAND_bytes(3)> and L<RAND_priv_bytes(3)>.
75
76=head1 NOTES
77
78Always check the error return value of these functions and do not take
79randomness for granted: an error occurs if the CSPRNG has not been
80seeded with enough randomness to ensure an unpredictable byte sequence.
81
82=head1 RETURN VALUES
83
84The functions return 1 on success, 0 on error.
85The error codes can be obtained by L<ERR_get_error(3)>.
86
87=head1 SEE ALSO
88
89L<ERR_get_error(3)>,
90L<RAND_add(3)>,
91L<RAND_bytes(3)>,
92L<RAND_priv_bytes(3)>,
93L<RAND(7)>,
94L<EVP_RAND(7)>
95
96=head1 HISTORY
97
98=over 2
99
100=item *
101
102Starting with OpenSSL release 1.1.0, BN_pseudo_rand() has been identical
103to BN_rand() and BN_pseudo_rand_range() has been identical to
104BN_rand_range().
105The BN_pseudo_rand() and BN_pseudo_rand_range() functions were
106deprecated in OpenSSL 3.0.
107
108=item *
109
110The BN_priv_rand() and BN_priv_rand_range() functions were added in
111OpenSSL 1.1.1.
112
113=item *
114
115The BN_rand_ex(), BN_priv_rand_ex(), BN_rand_range_ex() and
116BN_priv_rand_range_ex() functions were added in OpenSSL 3.0.
117
118=back
119
120=head1 COPYRIGHT
121
122Copyright 2000-2022 The OpenSSL Project Authors. All Rights Reserved.
123
124Licensed under the Apache License 2.0 (the "License").  You may not use
125this file except in compliance with the License.  You can obtain a copy
126in the file LICENSE in the source distribution or at
127L<https://www.openssl.org/source/license.html>.
128
129=cut
130