BN_rand.pod 4.5 KB

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