Debian Codegraph @

upstream/debian-unstable/man3/RAND_bytes.3ssl

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45)
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 3SSL"
58 .TH RAND_BYTES 3SSL 2026-04-07 3.6.2 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, RAND_set1_random_provider \- 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 \&
79 \& int RAND_set1_random_provider(OSSL_LIB_CTX *ctx, OSSL_PROVIDER *p);
80 .Ve
81 .PP
82 The following function has been deprecated since OpenSSL 1.1.0, and can be
83 hidden entirely by defining \fBOPENSSL_API_COMPAT\fR with a suitable version value,
84 see \fBopenssl_user_macros\fR\|(7):
85 .PP
86 .Vb 1
87 \& int RAND_pseudo_bytes(unsigned char *buf, int num);
88 .Ve
89 .SH DESCRIPTION
90 .IX Header "DESCRIPTION"
91 \&\fBRAND_bytes()\fR generates \fBnum\fR random bytes using a cryptographically
92 secure pseudo random generator (CSPRNG) and stores them in \fBbuf\fR. \fBbuf\fR \fBMUST NOT\fR be NULL.
93 .PP
94 \&\fBRAND_priv_bytes()\fR has the same semantics as \fBRAND_bytes()\fR. It is intended to
95 be used for generating values that should remain private. If using the
96 default RAND_METHOD, this function uses a separate "private" PRNG
97 instance so that a compromise of the "public" PRNG instance will not
98 affect the secrecy of these private values, as described in \fBRAND\fR\|(7)
99 and \fBEVP_RAND\fR\|(7).
100 .PP
101 \&\fBRAND_bytes_ex()\fR and \fBRAND_priv_bytes_ex()\fR are the same as \fBRAND_bytes()\fR and
102 \&\fBRAND_priv_bytes()\fR except that they both take additional \fIstrength\fR and
103 \&\fIctx\fR parameters. The bytes generated will have a security strength of at
104 least \fIstrength\fR bits.
105 The DRBG used for the operation is the public or private DRBG associated with
106 the specified \fIctx\fR. The parameter can be NULL, in which case
107 the default library context is used (see \fBOSSL_LIB_CTX\fR\|(3).
108 If the default RAND_METHOD has been changed then for compatibility reasons the
109 RAND_METHOD will be used in preference and the DRBG of the library context
110 ignored.
111 .PP
112 \&\fBRAND_set1_random_provider()\fR specifies a provider, \fIprov\fR, which will be used
113 by the library context \fIctx\fR for all of the generate calls above instead
114 of the built-in in DRBGs and entropy source. Pass NULL for the provider
115 to disable the random provider functionality. In this case, the built-in DRBGs
116 and entropy source will be used. This call should not be considered thread safe.
117 .SH NOTES
118 .IX Header "NOTES"
119 By default, the OpenSSL CSPRNG supports a security level of 256 bits, provided it
120 was able to seed itself from a trusted entropy source.
121 On all major platforms supported by OpenSSL (including the Unix-like platforms
122 and Windows), OpenSSL is configured to automatically seed the CSPRNG on first use
123 using the operating systems's random generator.
124 .PP
125 If the entropy source fails or is not available, the CSPRNG will enter an
126 error state and refuse to generate random bytes. For that reason, it is important
127 to always check the error return value of \fBRAND_bytes()\fR and \fBRAND_priv_bytes()\fR and
128 not take randomness for granted.
129 .PP
130 On other platforms, there might not be a trusted entropy source available
131 or OpenSSL might have been explicitly configured to use different entropy sources.
132 If you are in doubt about the quality of the entropy source, don't hesitate to ask
133 your operating system vendor or post a question on GitHub or the openssl-users
134 mailing list.
135 .SH "RETURN VALUES"
136 .IX Header "RETURN VALUES"
137 \&\fBRAND_bytes()\fR and \fBRAND_priv_bytes()\fR
138 return 1 on success, \-1 if not supported by the current
139 RAND method, or 0 on other failure. The error code can be
140 obtained by \fBERR_get_error\fR\|(3).
141 .PP
142 \&\fBRAND_set1_random_provider()\fR returns 1 on success and 0 on failure.
143 .SH "SEE ALSO"
144 .IX Header "SEE ALSO"
145 \&\fBRAND_add\fR\|(3),
146 \&\fBRAND_bytes\fR\|(3),
147 \&\fBRAND_priv_bytes\fR\|(3),
148 \&\fBERR_get_error\fR\|(3),
149 \&\fBRAND\fR\|(7),
150 \&\fBEVP_RAND\fR\|(7)
151 .SH HISTORY
152 .IX Header "HISTORY"
153 .IP \(bu 2
154 \&\fBRAND_pseudo_bytes()\fR was deprecated in OpenSSL 1.1.0; use \fBRAND_bytes()\fR instead.
155 .IP \(bu 2
156 The \fBRAND_priv_bytes()\fR function was added in OpenSSL 1.1.1.
157 .IP \(bu 2
158 The \fBRAND_bytes_ex()\fR and \fBRAND_priv_bytes_ex()\fR functions were added in OpenSSL 3.0
159 .IP \(bu 2
160 The \fBRAND_set1_random_provider()\fR function was added in OpenSSL 3.5
161 .SH COPYRIGHT
162 .IX Header "COPYRIGHT"
163 Copyright 2000\-2025 The OpenSSL Project Authors. All Rights Reserved.
164 .PP
165 Licensed under the Apache License 2.0 (the "License"). You may not use
166 this file except in compliance with the License. You can obtain a copy
167 in the file LICENSE in the source distribution or at
168 <https://www.openssl.org/source/license.html>.