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 enables efficient buffered reading of 37 // /dev/urandom. It adds an overhead of a few KB of memory per thread. It must 38 // be called before the first call to |RAND_bytes|. 39 // 40 // |fd| must be -1. We no longer support setting the file descriptor with this 41 // function. 42 // 43 // It has an unusual name because the buffer is unsafe across calls to |fork|. 44 // Hence, this function should never be called by libraries. 45 OPENSSL_EXPORT void RAND_enable_fork_unsafe_buffering(int fd); 46 47 // RAND_disable_fork_unsafe_buffering disables efficient buffered reading of 48 // /dev/urandom, causing BoringSSL to always draw entropy on every request 49 // for random bytes. 50 OPENSSL_EXPORT void RAND_disable_fork_unsafe_buffering(void); 51 #endif 52 53 #if defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE) 54 // RAND_reset_for_fuzzing resets the fuzzer-only deterministic RNG. This 55 // function is only defined in the fuzzer-only build configuration. 56 OPENSSL_EXPORT void RAND_reset_for_fuzzing(void); 57 #endif 58 59 // RAND_get_system_entropy_for_custom_prng writes |len| bytes of random data 60 // from a system entropy source to |buf|. The maximum length of entropy which 61 // may be requested is 256 bytes. If more than 256 bytes of data is requested, 62 // or if sufficient random data can not be obtained, |abort| is called. 63 // |RAND_bytes| should normally be used instead of this function. This function 64 // should only be used for seed values or where |malloc| should not be called 65 // from BoringSSL. This function is not FIPS compliant. 66 OPENSSL_EXPORT void RAND_get_system_entropy_for_custom_prng(uint8_t *buf, 67 size_t len); 68 69 70 // Deprecated functions 71 72 // RAND_pseudo_bytes is a wrapper around |RAND_bytes|. 73 OPENSSL_EXPORT int RAND_pseudo_bytes(uint8_t *buf, size_t len); 74 75 // RAND_seed reads a single byte of random data to ensure that any file 76 // descriptors etc are opened. 77 OPENSSL_EXPORT void RAND_seed(const void *buf, int num); 78 79 // RAND_load_file returns a nonnegative number. 80 OPENSSL_EXPORT int RAND_load_file(const char *path, long num); 81 82 // RAND_file_name returns NULL. 83 OPENSSL_EXPORT const char *RAND_file_name(char *buf, size_t num); 84 85 // RAND_add does nothing. 86 OPENSSL_EXPORT void RAND_add(const void *buf, int num, double entropy); 87 88 // RAND_egd returns 255. 89 OPENSSL_EXPORT int RAND_egd(const char *); 90 91 // RAND_poll returns one. 92 OPENSSL_EXPORT int RAND_poll(void); 93 94 // RAND_status returns one. 95 OPENSSL_EXPORT int RAND_status(void); 96 97 // RAND_cleanup does nothing. 98 OPENSSL_EXPORT void RAND_cleanup(void); 99 100 // rand_meth_st is typedefed to |RAND_METHOD| in base.h. It isn't used; it 101 // exists only to be the return type of |RAND_SSLeay|. It's 102 // external so that variables of this type can be initialized. 103 struct rand_meth_st { 104 void (*seed) (const void *buf, int num); 105 int (*bytes) (uint8_t *buf, size_t num); 106 void (*cleanup) (void); 107 void (*add) (const void *buf, int num, double entropy); 108 int (*pseudorand) (uint8_t *buf, size_t num); 109 int (*status) (void); 110 }; 111 112 // RAND_SSLeay returns a pointer to a dummy |RAND_METHOD|. 113 OPENSSL_EXPORT RAND_METHOD *RAND_SSLeay(void); 114 115 // RAND_OpenSSL returns a pointer to a dummy |RAND_METHOD|. 116 OPENSSL_EXPORT RAND_METHOD *RAND_OpenSSL(void); 117 118 // RAND_get_rand_method returns |RAND_SSLeay()|. 119 OPENSSL_EXPORT const RAND_METHOD *RAND_get_rand_method(void); 120 121 // RAND_set_rand_method returns one. 122 OPENSSL_EXPORT int RAND_set_rand_method(const RAND_METHOD *); 123 124 125 #if defined(__cplusplus) 126 } // extern C 127 #endif 128 129 #endif // OPENSSL_HEADER_RAND_H 130