1RAND_BYTES(3ossl) OpenSSL RAND_BYTES(3ossl)
2
3
4
6 RAND_bytes, RAND_priv_bytes, RAND_bytes_ex, RAND_priv_bytes_ex,
7 RAND_pseudo_bytes - generate random data
8
10 #include <openssl/rand.h>
11
12 int RAND_bytes(unsigned char *buf, int num);
13 int RAND_priv_bytes(unsigned char *buf, int num);
14
15 int RAND_bytes_ex(OSSL_LIB_CTX *ctx, unsigned char *buf, size_t num,
16 unsigned int strength);
17 int RAND_priv_bytes_ex(OSSL_LIB_CTX *ctx, unsigned char *buf, size_t num,
18 unsigned int strength);
19
20 The following function has been deprecated since OpenSSL 1.1.0, and can
21 be hidden entirely by defining OPENSSL_API_COMPAT with a suitable
22 version value, see openssl_user_macros(7):
23
24 int RAND_pseudo_bytes(unsigned char *buf, int num);
25
27 RAND_bytes() generates num random bytes using a cryptographically
28 secure pseudo random generator (CSPRNG) and stores them in buf.
29
30 RAND_priv_bytes() has the same semantics as RAND_bytes(). It is
31 intended to be used for generating values that should remain private.
32 If using the default RAND_METHOD, this function uses a separate
33 "private" PRNG instance so that a compromise of the "public" PRNG
34 instance will not affect the secrecy of these private values, as
35 described in RAND(7) and EVP_RAND(7).
36
37 RAND_bytes_ex() and RAND_priv_bytes_ex() are the same as RAND_bytes()
38 and RAND_priv_bytes() except that they both take additional strength
39 and ctx parameters. The bytes generated will have a security strength
40 of at least strength bits. The DRBG used for the operation is the
41 public or private DRBG associated with the specified ctx. The parameter
42 can be NULL, in which case the default library context is used (see
43 OSSL_LIB_CTX(3). If the default RAND_METHOD has been changed then for
44 compatibility reasons the RAND_METHOD will be used in preference and
45 the DRBG of the library context ignored.
46
48 By default, the OpenSSL CSPRNG supports a security level of 256 bits,
49 provided it was able to seed itself from a trusted entropy source. On
50 all major platforms supported by OpenSSL (including the Unix-like
51 platforms and Windows), OpenSSL is configured to automatically seed the
52 CSPRNG on first use using the operating systems's random generator.
53
54 If the entropy source fails or is not available, the CSPRNG will enter
55 an error state and refuse to generate random bytes. For that reason, it
56 is important to always check the error return value of RAND_bytes() and
57 RAND_priv_bytes() and not take randomness for granted.
58
59 On other platforms, there might not be a trusted entropy source
60 available or OpenSSL might have been explicitly configured to use
61 different entropy sources. If you are in doubt about the quality of
62 the entropy source, don't hesitate to ask your operating system vendor
63 or post a question on GitHub or the openssl-users mailing list.
64
66 RAND_bytes() and RAND_priv_bytes() return 1 on success, -1 if not
67 supported by the current RAND method, or 0 on other failure. The error
68 code can be obtained by ERR_get_error(3).
69
71 RAND_add(3), RAND_bytes(3), RAND_priv_bytes(3), ERR_get_error(3),
72 RAND(7), EVP_RAND(7)
73
75 • RAND_pseudo_bytes() was deprecated in OpenSSL 1.1.0; use RAND_bytes()
76 instead.
77
78 • The RAND_priv_bytes() function was added in OpenSSL 1.1.1.
79
80 • The RAND_bytes_ex() and RAND_priv_bytes_ex() functions were added in
81 OpenSSL 3.0
82
84 Copyright 2000-2022 The OpenSSL Project Authors. All Rights Reserved.
85
86 Licensed under the Apache License 2.0 (the "License"). You may not use
87 this file except in compliance with the License. You can obtain a copy
88 in the file LICENSE in the source distribution or at
89 <https://www.openssl.org/source/license.html>.
90
91
92
933.1.1 2023-08-31 RAND_BYTES(3ossl)