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