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