89505aa0edb48c856e04107a9a72b4298a7d2d63
[openssl.git] / include / crypto / rand.h
1 /*
2  * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 /*
11  * Licensed under the Apache License 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * https://www.openssl.org/source/license.html
15  * or in the file LICENSE in the source distribution.
16  */
17
18 #ifndef OSSL_CRYPTO_RAND_H
19 # define OSSL_CRYPTO_RAND_H
20 # pragma once
21
22 # include <openssl/rand.h>
23 # include "crypto/rand_pool.h"
24
25 /*
26  * Defines related to seed sources
27  */
28 #ifndef DEVRANDOM
29 /*
30  * set this to a comma-separated list of 'random' device files to try out. By
31  * default, we will try to read at least one of these files
32  */
33 # define DEVRANDOM "/dev/urandom", "/dev/random", "/dev/hwrng", "/dev/srandom"
34 # if defined(__linux) && !defined(__ANDROID__)
35 #  ifndef DEVRANDOM_WAIT
36 #   define DEVRANDOM_WAIT   "/dev/random"
37 #  endif
38 /*
39  * Linux kernels 4.8 and later changes how their random device works and there
40  * is no reliable way to tell that /dev/urandom has been seeded -- getentropy(2)
41  * should be used instead.
42  */
43 #  ifndef DEVRANDOM_SAFE_KERNEL
44 #   define DEVRANDOM_SAFE_KERNEL        4, 8
45 #  endif
46 /*
47  * Some operating systems do not permit select(2) on their random devices,
48  * defining this to zero will force the use of read(2) to extract one byte
49  * from /dev/random.
50  */
51 #  ifndef DEVRANDM_WAIT_USE_SELECT
52 #   define DEVRANDM_WAIT_USE_SELECT     1
53 #  endif
54 /*
55  * Define the shared memory identifier used to indicate if the operating
56  * system has properly seeded the DEVRANDOM source.
57  */
58 #  ifndef OPENSSL_RAND_SEED_DEVRANDOM_SHM_ID
59 #   define OPENSSL_RAND_SEED_DEVRANDOM_SHM_ID 114
60 #  endif
61
62 # endif
63 #endif
64
65 #if !defined(OPENSSL_NO_EGD) && !defined(DEVRANDOM_EGD)
66 /*
67  * set this to a comma-separated list of 'egd' sockets to try out. These
68  * sockets will be tried in the order listed in case accessing the device
69  * files listed in DEVRANDOM did not return enough randomness.
70  */
71 # define DEVRANDOM_EGD "/var/run/egd-pool", "/dev/egd-pool", "/etc/egd-pool", "/etc/entropy"
72 #endif
73
74 void rand_cleanup_int(void);
75
76 /*
77  * Initialise the random pool reseeding sources.
78  *
79  * Returns 1 on success and 0 on failure.
80  */
81 int rand_pool_init(void);
82
83 /*
84  * Finalise the random pool reseeding sources.
85  */
86 void rand_pool_cleanup(void);
87
88 /*
89  * Control the random pool use of open file descriptors.
90  */
91 void rand_pool_keep_random_devices_open(int keep);
92
93 /*
94  * Configuration
95  */
96 void ossl_random_add_conf_module(void);
97
98 /*
99  * Get and cleanup random seed material.
100  */
101 size_t ossl_rand_get_entropy(ossl_unused OSSL_CORE_HANDLE *handle,
102                              unsigned char **pout, int entropy,
103                              size_t min_len, size_t max_len);
104 void ossl_rand_cleanup_entropy(ossl_unused OSSL_CORE_HANDLE *handle,
105                                unsigned char *buf, size_t len);
106 size_t ossl_rand_get_nonce(ossl_unused OSSL_CORE_HANDLE *handle,
107                            unsigned char **pout, size_t min_len, size_t max_len,
108                            const void *salt, size_t salt_len);
109 void ossl_rand_cleanup_nonce(ossl_unused OSSL_CORE_HANDLE *handle,
110                              unsigned char *buf, size_t len);
111
112 /*
113  * Get seeding material from the operating system sources.
114  */
115 size_t ossl_pool_acquire_entropy(RAND_POOL *pool);
116 int ossl_pool_add_nonce_data(RAND_POOL *pool);
117
118 #endif