blob: 5d02e12b331ac9f3abf57161d7060e59ffc4e4cc [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* 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)
21extern "C" {
22#endif
23
24
David Benjamin4512b792017-08-18 19:21:50 -040025// Random number generation.
David Benjamin8f647782015-04-09 11:37:10 -040026
27
David Benjamin4512b792017-08-18 19:21:50 -040028// RAND_bytes writes |len| bytes of random data to |buf| and returns one.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -070029OPENSSL_EXPORT int RAND_bytes(uint8_t *buf, size_t len);
Adam Langley95c29f32014-06-20 12:00:00 -070030
David Benjamin4512b792017-08-18 19:21:50 -040031// RAND_cleanup frees any resources used by the RNG. This is not safe if other
32// threads might still be calling |RAND_bytes|.
David Benjaminc44d2f42014-08-20 16:24:00 -040033OPENSSL_EXPORT void RAND_cleanup(void);
Adam Langley95c29f32014-06-20 12:00:00 -070034
35
David Benjamin4512b792017-08-18 19:21:50 -040036// Obscure functions.
David Benjaminde24aad2015-06-30 13:52:59 -040037
38#if !defined(OPENSSL_WINDOWS)
David Benjamin4512b792017-08-18 19:21:50 -040039// 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 the first call to |RAND_bytes|, and it is mutually exclusive
44// with |RAND_enable_fork_unsafe_buffering|.
45//
46// |RAND_set_urandom_fd| does not buffer any entropy, so it is safe to call
47// |fork| at any time after calling |RAND_set_urandom_fd|.
David Benjaminde24aad2015-06-30 13:52:59 -040048OPENSSL_EXPORT void RAND_set_urandom_fd(int fd);
Matt Braithwaitecc2c7aa2015-09-03 14:18:08 -070049
David Benjamin4512b792017-08-18 19:21:50 -040050// 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| and it is mutually exclusive
53// with calls to |RAND_set_urandom_fd|.
54//
55// If |fd| is non-negative then a copy of |fd| will be used rather than opening
56// /dev/urandom internally. Like |RAND_set_urandom_fd|, the caller retains
57// ownership of |fd|. If |fd| is negative then /dev/urandom will be opened and
58// any error from open(2) crashes the address space.
59//
60// It has an unusual name because the buffer is unsafe across calls to |fork|.
61// Hence, this function should never be called by libraries.
Matt Braithwaitecc2c7aa2015-09-03 14:18:08 -070062OPENSSL_EXPORT void RAND_enable_fork_unsafe_buffering(int fd);
David Benjaminde24aad2015-06-30 13:52:59 -040063#endif
64
David Benjaminec978dd2016-11-04 18:59:33 -040065#if defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE)
David Benjamin4512b792017-08-18 19:21:50 -040066// RAND_reset_for_fuzzing resets the fuzzer-only deterministic RNG. This
67// function is only defined in the fuzzer-only build configuration.
David Benjaminbc5b2a22016-03-01 22:57:32 -050068OPENSSL_EXPORT void RAND_reset_for_fuzzing(void);
69#endif
70
David Benjaminde24aad2015-06-30 13:52:59 -040071
David Benjamin4512b792017-08-18 19:21:50 -040072// Deprecated functions
Adam Langley95c29f32014-06-20 12:00:00 -070073
David Benjamin4512b792017-08-18 19:21:50 -040074// RAND_pseudo_bytes is a wrapper around |RAND_bytes|.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -070075OPENSSL_EXPORT int RAND_pseudo_bytes(uint8_t *buf, size_t len);
Adam Langley95c29f32014-06-20 12:00:00 -070076
David Benjamin4512b792017-08-18 19:21:50 -040077// RAND_seed reads a single byte of random data to ensure that any file
78// descriptors etc are opened.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -070079OPENSSL_EXPORT void RAND_seed(const void *buf, int num);
Adam Langley95c29f32014-06-20 12:00:00 -070080
David Benjamin4512b792017-08-18 19:21:50 -040081// RAND_load_file returns a nonnegative number.
Matt Braithwaiteaf3d5bd2015-04-24 17:40:19 -070082OPENSSL_EXPORT int RAND_load_file(const char *path, long num);
83
David Benjamin4512b792017-08-18 19:21:50 -040084// RAND_file_name returns NULL.
David Benjamine5aa7912016-01-26 01:09:19 -050085OPENSSL_EXPORT const char *RAND_file_name(char *buf, size_t num);
86
David Benjamin4512b792017-08-18 19:21:50 -040087// RAND_add does nothing.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -070088OPENSSL_EXPORT void RAND_add(const void *buf, int num, double entropy);
Adam Langley95c29f32014-06-20 12:00:00 -070089
David Benjamin4512b792017-08-18 19:21:50 -040090// RAND_egd returns 255.
Matt Braithwaite3e5e99d2015-06-24 16:41:10 -070091OPENSSL_EXPORT int RAND_egd(const char *);
92
David Benjamin4512b792017-08-18 19:21:50 -040093// RAND_poll returns one.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -070094OPENSSL_EXPORT int RAND_poll(void);
Adam Langley95c29f32014-06-20 12:00:00 -070095
David Benjamin4512b792017-08-18 19:21:50 -040096// RAND_status returns one.
Adam Langleyc3ef76f2015-04-13 14:34:17 -070097OPENSSL_EXPORT int RAND_status(void);
98
David Benjamin4512b792017-08-18 19:21:50 -040099// rand_meth_st is typedefed to |RAND_METHOD| in base.h. It isn't used; it
100// exists only to be the return type of |RAND_SSLeay|. It's
101// external so that variables of this type can be initialized.
Matt Braithwaite3e5e99d2015-06-24 16:41:10 -0700102struct rand_meth_st {
103 void (*seed) (const void *buf, int num);
Matt Braithwaitebc97c692015-07-22 18:16:18 -0700104 int (*bytes) (uint8_t *buf, size_t num);
Matt Braithwaite3e5e99d2015-06-24 16:41:10 -0700105 void (*cleanup) (void);
106 void (*add) (const void *buf, int num, double entropy);
Matt Braithwaitebc97c692015-07-22 18:16:18 -0700107 int (*pseudorand) (uint8_t *buf, size_t num);
Matt Braithwaite3e5e99d2015-06-24 16:41:10 -0700108 int (*status) (void);
109};
110
David Benjamin4512b792017-08-18 19:21:50 -0400111// RAND_SSLeay returns a pointer to a dummy |RAND_METHOD|.
Matt Braithwaite3e5e99d2015-06-24 16:41:10 -0700112OPENSSL_EXPORT RAND_METHOD *RAND_SSLeay(void);
113
David Benjamin4512b792017-08-18 19:21:50 -0400114// RAND_get_rand_method returns |RAND_SSLeay()|.
Adam Langley4d1b57a2017-07-28 14:07:04 -0700115OPENSSL_EXPORT const RAND_METHOD *RAND_get_rand_method(void);
116
David Benjamin4512b792017-08-18 19:21:50 -0400117// RAND_set_rand_method does nothing.
Adam Langleya59347e2015-06-24 17:02:15 -0700118OPENSSL_EXPORT void RAND_set_rand_method(const RAND_METHOD *);
Matt Braithwaite3e5e99d2015-06-24 16:41:10 -0700119
Adam Langley95c29f32014-06-20 12:00:00 -0700120
121#if defined(__cplusplus)
David Benjamin4512b792017-08-18 19:21:50 -0400122} // extern C
Adam Langley95c29f32014-06-20 12:00:00 -0700123#endif
124
David Benjamin4512b792017-08-18 19:21:50 -0400125#endif // OPENSSL_HEADER_RAND_H