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