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