RAND_bytes.3ossl 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. .\" -*- mode: troff; coding: utf-8 -*-
  2. .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43)
  3. .\"
  4. .\" Standard preamble:
  5. .\" ========================================================================
  6. .de Sp \" Vertical space (when we can't use .PP)
  7. .if t .sp .5v
  8. .if n .sp
  9. ..
  10. .de Vb \" Begin verbatim text
  11. .ft CW
  12. .nf
  13. .ne \\$1
  14. ..
  15. .de Ve \" End verbatim text
  16. .ft R
  17. .fi
  18. ..
  19. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
  20. .ie n \{\
  21. . ds C` ""
  22. . ds C' ""
  23. 'br\}
  24. .el\{\
  25. . ds C`
  26. . ds C'
  27. 'br\}
  28. .\"
  29. .\" Escape single quotes in literal strings from groff's Unicode transform.
  30. .ie \n(.g .ds Aq \(aq
  31. .el .ds Aq '
  32. .\"
  33. .\" If the F register is >0, we'll generate index entries on stderr for
  34. .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
  35. .\" entries marked with X<> in POD. Of course, you'll have to process the
  36. .\" output yourself in some meaningful fashion.
  37. .\"
  38. .\" Avoid warning from groff about undefined register 'F'.
  39. .de IX
  40. ..
  41. .nr rF 0
  42. .if \n(.g .if rF .nr rF 1
  43. .if (\n(rF:(\n(.g==0)) \{\
  44. . if \nF \{\
  45. . de IX
  46. . tm Index:\\$1\t\\n%\t"\\$2"
  47. ..
  48. . if !\nF==2 \{\
  49. . nr % 0
  50. . nr F 2
  51. . \}
  52. . \}
  53. .\}
  54. .rr rF
  55. .\" ========================================================================
  56. .\"
  57. .IX Title "RAND_BYTES 3ossl"
  58. .TH RAND_BYTES 3ossl 2025-01-17 3.4.0 OpenSSL
  59. .\" For nroff, turn off justification. Always turn off hyphenation; it makes
  60. .\" way too many mistakes in technical documents.
  61. .if n .ad l
  62. .nh
  63. .SH NAME
  64. RAND_bytes, RAND_priv_bytes, RAND_bytes_ex, RAND_priv_bytes_ex,
  65. RAND_pseudo_bytes \- generate random data
  66. .SH SYNOPSIS
  67. .IX Header "SYNOPSIS"
  68. .Vb 1
  69. \& #include <openssl/rand.h>
  70. \&
  71. \& int RAND_bytes(unsigned char *buf, int num);
  72. \& int RAND_priv_bytes(unsigned char *buf, int num);
  73. \&
  74. \& int RAND_bytes_ex(OSSL_LIB_CTX *ctx, unsigned char *buf, size_t num,
  75. \& unsigned int strength);
  76. \& int RAND_priv_bytes_ex(OSSL_LIB_CTX *ctx, unsigned char *buf, size_t num,
  77. \& unsigned int strength);
  78. .Ve
  79. .PP
  80. The following function has been deprecated since OpenSSL 1.1.0, and can be
  81. hidden entirely by defining \fBOPENSSL_API_COMPAT\fR with a suitable version value,
  82. see \fBopenssl_user_macros\fR\|(7):
  83. .PP
  84. .Vb 1
  85. \& int RAND_pseudo_bytes(unsigned char *buf, int num);
  86. .Ve
  87. .SH DESCRIPTION
  88. .IX Header "DESCRIPTION"
  89. \&\fBRAND_bytes()\fR generates \fBnum\fR random bytes using a cryptographically
  90. secure pseudo random generator (CSPRNG) and stores them in \fBbuf\fR.
  91. .PP
  92. \&\fBRAND_priv_bytes()\fR has the same semantics as \fBRAND_bytes()\fR. It is intended to
  93. be used for generating values that should remain private. If using the
  94. default RAND_METHOD, this function uses a separate "private" PRNG
  95. instance so that a compromise of the "public" PRNG instance will not
  96. affect the secrecy of these private values, as described in \fBRAND\fR\|(7)
  97. and \fBEVP_RAND\fR\|(7).
  98. .PP
  99. \&\fBRAND_bytes_ex()\fR and \fBRAND_priv_bytes_ex()\fR are the same as \fBRAND_bytes()\fR and
  100. \&\fBRAND_priv_bytes()\fR except that they both take additional \fIstrength\fR and
  101. \&\fIctx\fR parameters. The bytes generated will have a security strength of at
  102. least \fIstrength\fR bits.
  103. The DRBG used for the operation is the public or private DRBG associated with
  104. the specified \fIctx\fR. The parameter can be NULL, in which case
  105. the default library context is used (see \fBOSSL_LIB_CTX\fR\|(3).
  106. If the default RAND_METHOD has been changed then for compatibility reasons the
  107. RAND_METHOD will be used in preference and the DRBG of the library context
  108. ignored.
  109. .SH NOTES
  110. .IX Header "NOTES"
  111. By default, the OpenSSL CSPRNG supports a security level of 256 bits, provided it
  112. was able to seed itself from a trusted entropy source.
  113. On all major platforms supported by OpenSSL (including the Unix-like platforms
  114. and Windows), OpenSSL is configured to automatically seed the CSPRNG on first use
  115. using the operating systems's random generator.
  116. .PP
  117. If the entropy source fails or is not available, the CSPRNG will enter an
  118. error state and refuse to generate random bytes. For that reason, it is important
  119. to always check the error return value of \fBRAND_bytes()\fR and \fBRAND_priv_bytes()\fR and
  120. not take randomness for granted.
  121. .PP
  122. On other platforms, there might not be a trusted entropy source available
  123. or OpenSSL might have been explicitly configured to use different entropy sources.
  124. If you are in doubt about the quality of the entropy source, don't hesitate to ask
  125. your operating system vendor or post a question on GitHub or the openssl-users
  126. mailing list.
  127. .SH "RETURN VALUES"
  128. .IX Header "RETURN VALUES"
  129. \&\fBRAND_bytes()\fR and \fBRAND_priv_bytes()\fR
  130. return 1 on success, \-1 if not supported by the current
  131. RAND method, or 0 on other failure. The error code can be
  132. obtained by \fBERR_get_error\fR\|(3).
  133. .SH "SEE ALSO"
  134. .IX Header "SEE ALSO"
  135. \&\fBRAND_add\fR\|(3),
  136. \&\fBRAND_bytes\fR\|(3),
  137. \&\fBRAND_priv_bytes\fR\|(3),
  138. \&\fBERR_get_error\fR\|(3),
  139. \&\fBRAND\fR\|(7),
  140. \&\fBEVP_RAND\fR\|(7)
  141. .SH HISTORY
  142. .IX Header "HISTORY"
  143. .IP \(bu 2
  144. \&\fBRAND_pseudo_bytes()\fR was deprecated in OpenSSL 1.1.0; use \fBRAND_bytes()\fR instead.
  145. .IP \(bu 2
  146. The \fBRAND_priv_bytes()\fR function was added in OpenSSL 1.1.1.
  147. .IP \(bu 2
  148. The \fBRAND_bytes_ex()\fR and \fBRAND_priv_bytes_ex()\fR functions were added in OpenSSL 3.0
  149. .SH COPYRIGHT
  150. .IX Header "COPYRIGHT"
  151. Copyright 2000\-2022 The OpenSSL Project Authors. All Rights Reserved.
  152. .PP
  153. Licensed under the Apache License 2.0 (the "License"). You may not use
  154. this file except in compliance with the License. You can obtain a copy
  155. in the file LICENSE in the source distribution or at
  156. <https://www.openssl.org/source/license.html>.