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