Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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 | |
David Benjamin | 8f64778 | 2015-04-09 11:37:10 -0400 | [diff] [blame] | 25 | /* Random number generation. */ |
| 26 | |
| 27 | |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 28 | /* RAND_bytes writes |len| bytes of random data to |buf| and returns one. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 29 | OPENSSL_EXPORT int RAND_bytes(uint8_t *buf, size_t len); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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|. */ |
David Benjamin | c44d2f4 | 2014-08-20 16:24:00 -0400 | [diff] [blame] | 33 | OPENSSL_EXPORT void RAND_cleanup(void); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 34 | |
| 35 | |
David Benjamin | de24aad | 2015-06-30 13:52:59 -0400 | [diff] [blame] | 36 | /* Obscure functions. */ |
| 37 | |
| 38 | #if !defined(OPENSSL_WINDOWS) |
| 39 | /* RAND_set_urandom_fd causes the module to use a copy of |fd| for system |
| 40 | * randomness rather opening /dev/urandom internally. The caller retains |
| 41 | * ownership of |fd| and is at liberty to close it at any time. This is useful |
| 42 | * if, due to a sandbox, /dev/urandom isn't available. If used, it must be |
| 43 | * called before |RAND_bytes| is called in the current address space. |
| 44 | * |
| 45 | * |RAND_set_urandom_fd| does not buffer any entropy, so it is safe to call |
| 46 | * |fork| between |RAND_set_urandom_fd| and the first call to |RAND_bytes|. */ |
| 47 | OPENSSL_EXPORT void RAND_set_urandom_fd(int fd); |
| 48 | #endif |
| 49 | |
| 50 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 51 | /* Deprecated functions */ |
| 52 | |
| 53 | /* RAND_pseudo_bytes is a wrapper around |RAND_bytes|. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 54 | OPENSSL_EXPORT int RAND_pseudo_bytes(uint8_t *buf, size_t len); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 55 | |
| 56 | /* RAND_seed does nothing. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 57 | OPENSSL_EXPORT void RAND_seed(const void *buf, int num); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 58 | |
Matt Braithwaite | af3d5bd | 2015-04-24 17:40:19 -0700 | [diff] [blame] | 59 | /* RAND_load_file returns a nonnegative number. */ |
| 60 | OPENSSL_EXPORT int RAND_load_file(const char *path, long num); |
| 61 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 62 | /* RAND_add does nothing. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 63 | OPENSSL_EXPORT void RAND_add(const void *buf, int num, double entropy); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 64 | |
Matt Braithwaite | 3e5e99d | 2015-06-24 16:41:10 -0700 | [diff] [blame] | 65 | /* RAND_egd returns 255. */ |
| 66 | OPENSSL_EXPORT int RAND_egd(const char *); |
| 67 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 68 | /* RAND_poll returns one. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 69 | OPENSSL_EXPORT int RAND_poll(void); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 70 | |
Adam Langley | c3ef76f | 2015-04-13 14:34:17 -0700 | [diff] [blame] | 71 | /* RAND_status returns one. */ |
| 72 | OPENSSL_EXPORT int RAND_status(void); |
| 73 | |
Matt Braithwaite | 3e5e99d | 2015-06-24 16:41:10 -0700 | [diff] [blame] | 74 | /* rand_meth_st is typedefed to |RAND_METHOD| in base.h. It isn't used; it |
| 75 | * exists only to be the return type of |RAND_SSLeay|. It's |
| 76 | * external so that variables of this type can be initialized. */ |
| 77 | struct rand_meth_st { |
| 78 | void (*seed) (const void *buf, int num); |
| 79 | int (*bytes) (unsigned char *buf, int num); |
| 80 | void (*cleanup) (void); |
| 81 | void (*add) (const void *buf, int num, double entropy); |
| 82 | int (*pseudorand) (unsigned char *buf, int num); |
| 83 | int (*status) (void); |
| 84 | }; |
| 85 | |
| 86 | /* RAND_SSLeay returns a pointer to a dummy |RAND_METHOD|. */ |
| 87 | OPENSSL_EXPORT RAND_METHOD *RAND_SSLeay(void); |
| 88 | |
| 89 | /* RAND_set_rand_method does nothing. */ |
Adam Langley | a59347e | 2015-06-24 17:02:15 -0700 | [diff] [blame] | 90 | OPENSSL_EXPORT void RAND_set_rand_method(const RAND_METHOD *); |
Matt Braithwaite | 3e5e99d | 2015-06-24 16:41:10 -0700 | [diff] [blame] | 91 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 92 | |
| 93 | #if defined(__cplusplus) |
| 94 | } /* extern C */ |
| 95 | #endif |
| 96 | |
| 97 | #endif /* OPENSSL_HEADER_RAND_H */ |