1 /* Copyright (c) 2014, Google Inc. 2 * 3 * Permission to use, copy, modify, and/or distribute this software for any 4 * purpose with or without fee is hereby granted, provided that the above 5 * copyright notice and this permission notice appear in all copies. 6 * 7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ 14 15 #ifndef OPENSSL_HEADER_RAND_H 16 #define OPENSSL_HEADER_RAND_H 17 18 #include <openssl/base.h> 19 20 #if defined(__cplusplus) 21 extern "C" { 22 #endif 23 24 25 // Random number generation. 26 27 28 // RAND_bytes writes |len| bytes of random data to |buf| and returns one. In the 29 // event that sufficient random data can not be obtained, |abort| is called. 30 OPENSSL_EXPORT int RAND_bytes(uint8_t *buf, size_t len); 31 32 33 // Obscure functions. 34 35 #if !defined(OPENSSL_WINDOWS) 36 // RAND_enable_fork_unsafe_buffering indicates that clones of the address space, 37 // e.g. via |fork|, will never call into BoringSSL. It may be used to disable 38 // BoringSSL's more expensive fork-safety measures. However, calling this 39 // function and then using BoringSSL across |fork| calls will leak secret keys. 40 // |fd| must be -1. 41 // 42 // WARNING: This function affects BoringSSL for the entire address space. Thus 43 // this function should never be called by library code, only by code with 44 // global knowledge of the application's use of BoringSSL. 45 // 46 // Do not use this function unless a performance issue was measured with the 47 // default behavior. BoringSSL can efficiently detect forks on most platforms, 48 // in which case this function is a no-op and is unnecessary. In particular, 49 // Linux kernel versions 4.14 or later provide |MADV_WIPEONFORK|. Future 50 // versions of BoringSSL will remove this functionality when older kernels are 51 // sufficiently rare. 52 // 53 // This function has an unusual name because it historically controlled internal 54 // buffers, but no longer does. 55 OPENSSL_EXPORT void RAND_enable_fork_unsafe_buffering(int fd); 56 57 // RAND_disable_fork_unsafe_buffering restores BoringSSL's default fork-safety 58 // protections. See also |RAND_enable_fork_unsafe_buffering|. 59 OPENSSL_EXPORT void RAND_disable_fork_unsafe_buffering(void); 60 #endif 61 62 #if defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE) 63 // RAND_reset_for_fuzzing resets the fuzzer-only deterministic RNG. This 64 // function is only defined in the fuzzer-only build configuration. 65 OPENSSL_EXPORT void RAND_reset_for_fuzzing(void); 66 #endif 67 68 // RAND_get_system_entropy_for_custom_prng writes |len| bytes of random data 69 // from a system entropy source to |buf|. The maximum length of entropy which 70 // may be requested is 256 bytes. If more than 256 bytes of data is requested, 71 // or if sufficient random data can not be obtained, |abort| is called. 72 // |RAND_bytes| should normally be used instead of this function. This function 73 // should only be used for seed values or where |malloc| should not be called 74 // from BoringSSL. This function is not FIPS compliant. 75 OPENSSL_EXPORT void RAND_get_system_entropy_for_custom_prng(uint8_t *buf, 76 size_t len); 77 78 79 // Deprecated functions 80 81 // RAND_pseudo_bytes is a wrapper around |RAND_bytes|. 82 OPENSSL_EXPORT int RAND_pseudo_bytes(uint8_t *buf, size_t len); 83 84 // RAND_seed reads a single byte of random data to ensure that any file 85 // descriptors etc are opened. 86 OPENSSL_EXPORT void RAND_seed(const void *buf, int num); 87 88 // RAND_load_file returns a nonnegative number. 89 OPENSSL_EXPORT int RAND_load_file(const char *path, long num); 90 91 // RAND_file_name returns NULL. 92 OPENSSL_EXPORT const char *RAND_file_name(char *buf, size_t num); 93 94 // RAND_add does nothing. 95 OPENSSL_EXPORT void RAND_add(const void *buf, int num, double entropy); 96 97 // RAND_egd returns 255. 98 OPENSSL_EXPORT int RAND_egd(const char *); 99 100 // RAND_poll returns one. 101 OPENSSL_EXPORT int RAND_poll(void); 102 103 // RAND_status returns one. 104 OPENSSL_EXPORT int RAND_status(void); 105 106 // RAND_cleanup does nothing. 107 OPENSSL_EXPORT void RAND_cleanup(void); 108 109 // rand_meth_st is typedefed to |RAND_METHOD| in base.h. It isn't used; it 110 // exists only to be the return type of |RAND_SSLeay|. It's 111 // external so that variables of this type can be initialized. 112 struct rand_meth_st { 113 void (*seed) (const void *buf, int num); 114 int (*bytes) (uint8_t *buf, size_t num); 115 void (*cleanup) (void); 116 void (*add) (const void *buf, int num, double entropy); 117 int (*pseudorand) (uint8_t *buf, size_t num); 118 int (*status) (void); 119 }; 120 121 // RAND_SSLeay returns a pointer to a dummy |RAND_METHOD|. 122 OPENSSL_EXPORT RAND_METHOD *RAND_SSLeay(void); 123 124 // RAND_OpenSSL returns a pointer to a dummy |RAND_METHOD|. 125 OPENSSL_EXPORT RAND_METHOD *RAND_OpenSSL(void); 126 127 // RAND_get_rand_method returns |RAND_SSLeay()|. 128 OPENSSL_EXPORT const RAND_METHOD *RAND_get_rand_method(void); 129 130 // RAND_set_rand_method returns one. 131 OPENSSL_EXPORT int RAND_set_rand_method(const RAND_METHOD *); 132 133 134 #if defined(__cplusplus) 135 } // extern C 136 #endif 137 138 #endif // OPENSSL_HEADER_RAND_H 139