xref: /netbsd-src/crypto/external/bsd/openssl/dist/doc/man3/RAND_bytes.pod (revision 4778aede4608a995eaeedca856a7a71a2fa5c675)
113d40330Schristos=pod
213d40330Schristos
313d40330Schristos=head1 NAME
413d40330Schristos
5b0d17251SchristosRAND_bytes, RAND_priv_bytes, RAND_bytes_ex, RAND_priv_bytes_ex,
6b0d17251SchristosRAND_pseudo_bytes - generate random data
713d40330Schristos
813d40330Schristos=head1 SYNOPSIS
913d40330Schristos
1013d40330Schristos #include <openssl/rand.h>
1113d40330Schristos
1213d40330Schristos int RAND_bytes(unsigned char *buf, int num);
1313d40330Schristos int RAND_priv_bytes(unsigned char *buf, int num);
1413d40330Schristos
15b0d17251Schristos int RAND_bytes_ex(OSSL_LIB_CTX *ctx, unsigned char *buf, size_t num,
16b0d17251Schristos                   unsigned int strength);
17b0d17251Schristos int RAND_priv_bytes_ex(OSSL_LIB_CTX *ctx, unsigned char *buf, size_t num,
18b0d17251Schristos                        unsigned int strength);
1913d40330Schristos
20b0d17251SchristosThe following function has been deprecated since OpenSSL 1.1.0, and can be
21b0d17251Schristoshidden entirely by defining B<OPENSSL_API_COMPAT> with a suitable version value,
22b0d17251Schristossee L<openssl_user_macros(7)>:
23b0d17251Schristos
2413d40330Schristos int RAND_pseudo_bytes(unsigned char *buf, int num);
2513d40330Schristos
2613d40330Schristos=head1 DESCRIPTION
2713d40330Schristos
287d004720SchristosRAND_bytes() generates B<num> random bytes using a cryptographically
297d004720Schristossecure pseudo random generator (CSPRNG) and stores them in B<buf>.
3013d40330Schristos
3113d40330SchristosRAND_priv_bytes() has the same semantics as RAND_bytes().  It is intended to
3213d40330Schristosbe used for generating values that should remain private. If using the
3313d40330Schristosdefault RAND_METHOD, this function uses a separate "private" PRNG
3413d40330Schristosinstance so that a compromise of the "public" PRNG instance will not
3513d40330Schristosaffect the secrecy of these private values, as described in L<RAND(7)>
36b0d17251Schristosand L<EVP_RAND(7)>.
37b0d17251Schristos
38b0d17251SchristosRAND_bytes_ex() and RAND_priv_bytes_ex() are the same as RAND_bytes() and
39b0d17251SchristosRAND_priv_bytes() except that they both take additional I<strength> and
40*4778aedeSchristosI<ctx> parameters. The bytes generated will have a security strength of at
41b0d17251Schristosleast I<strength> bits.
42b0d17251SchristosThe DRBG used for the operation is the public or private DRBG associated with
43b0d17251Schristosthe specified I<ctx>. The parameter can be NULL, in which case
44b0d17251Schristosthe default library context is used (see L<OSSL_LIB_CTX(3)>.
45b0d17251SchristosIf the default RAND_METHOD has been changed then for compatibility reasons the
46b0d17251SchristosRAND_METHOD will be used in preference and the DRBG of the library context
47b0d17251Schristosignored.
4813d40330Schristos
4913d40330Schristos=head1 NOTES
5013d40330Schristos
517d004720SchristosBy default, the OpenSSL CSPRNG supports a security level of 256 bits, provided it
527d004720Schristoswas able to seed itself from a trusted entropy source.
537d004720SchristosOn all major platforms supported by OpenSSL (including the Unix-like platforms
547d004720Schristosand Windows), OpenSSL is configured to automatically seed the CSPRNG on first use
557d004720Schristosusing the operating systems's random generator.
567d004720Schristos
577d004720SchristosIf the entropy source fails or is not available, the CSPRNG will enter an
587d004720Schristoserror state and refuse to generate random bytes. For that reason, it is important
597d004720Schristosto always check the error return value of RAND_bytes() and RAND_priv_bytes() and
607d004720Schristosnot take randomness for granted.
617d004720Schristos
627d004720SchristosOn other platforms, there might not be a trusted entropy source available
637d004720Schristosor OpenSSL might have been explicitly configured to use different entropy sources.
647d004720SchristosIf you are in doubt about the quality of the entropy source, don't hesitate to ask
657d004720Schristosyour operating system vendor or post a question on GitHub or the openssl-users
667d004720Schristosmailing list.
6713d40330Schristos
6813d40330Schristos=head1 RETURN VALUES
6913d40330Schristos
7013d40330SchristosRAND_bytes() and RAND_priv_bytes()
7113d40330Schristosreturn 1 on success, -1 if not supported by the current
7213d40330SchristosRAND method, or 0 on other failure. The error code can be
7313d40330Schristosobtained by L<ERR_get_error(3)>.
7413d40330Schristos
75a3b08d93Schristos=head1 SEE ALSO
76a3b08d93Schristos
77a3b08d93SchristosL<RAND_add(3)>,
78a3b08d93SchristosL<RAND_bytes(3)>,
79a3b08d93SchristosL<RAND_priv_bytes(3)>,
80a3b08d93SchristosL<ERR_get_error(3)>,
81a3b08d93SchristosL<RAND(7)>,
82b0d17251SchristosL<EVP_RAND(7)>
83a3b08d93Schristos
8413d40330Schristos=head1 HISTORY
8513d40330Schristos
8613d40330Schristos=over 2
8713d40330Schristos
8813d40330Schristos=item *
8913d40330Schristos
9013d40330SchristosRAND_pseudo_bytes() was deprecated in OpenSSL 1.1.0; use RAND_bytes() instead.
9113d40330Schristos
9213d40330Schristos=item *
9313d40330Schristos
94b88c74d5SchristosThe RAND_priv_bytes() function was added in OpenSSL 1.1.1.
9513d40330Schristos
96b0d17251Schristos=item *
97b0d17251Schristos
98b0d17251SchristosThe RAND_bytes_ex() and RAND_priv_bytes_ex() functions were added in OpenSSL 3.0
99b0d17251Schristos
10013d40330Schristos=back
10113d40330Schristos
10213d40330Schristos=head1 COPYRIGHT
10313d40330Schristos
104*4778aedeSchristosCopyright 2000-2023 The OpenSSL Project Authors. All Rights Reserved.
10513d40330Schristos
106b0d17251SchristosLicensed under the Apache License 2.0 (the "License").  You may not use
10713d40330Schristosthis file except in compliance with the License.  You can obtain a copy
10813d40330Schristosin the file LICENSE in the source distribution or at
10913d40330SchristosL<https://www.openssl.org/source/license.html>.
11013d40330Schristos
11113d40330Schristos=cut
112