Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/include/crypto/rand.h

raw
1 /*
2 * Copyright 2016-2025 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 #if defined(__APPLE__) && !defined(OPENSSL_NO_APPLE_CRYPTO_RANDOM)
26 #if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
27 #include <Availability.h>
28 #else
29 #include <TargetConditionals.h>
30 #include <AvailabilityMacros.h>
31 #endif
32 #if (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200) || (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 80000)
33 #define OPENSSL_APPLE_CRYPTO_RANDOM 1
34 #include <CommonCrypto/CommonCryptoError.h>
35 #include <CommonCrypto/CommonRandom.h>
36 #endif
37 #endif
38
39 /*
40 * Defines related to seed sources
41 */
42 #ifndef DEVRANDOM
43 /*
44 * set this to a comma-separated list of 'random' device files to try out. By
45 * default, we will try to read at least one of these files
46 */
47 #define DEVRANDOM "/dev/urandom", "/dev/random", "/dev/hwrng", "/dev/srandom"
48 #if defined(__linux) && !defined(__ANDROID__)
49 #ifndef DEVRANDOM_WAIT
50 #define DEVRANDOM_WAIT "/dev/random"
51 #endif
52 /*
53 * Linux kernels 4.8 and later changes how their random device works and there
54 * is no reliable way to tell that /dev/urandom has been seeded -- getentropy(2)
55 * should be used instead.
56 */
57 #ifndef DEVRANDOM_SAFE_KERNEL
58 #define DEVRANDOM_SAFE_KERNEL 4, 8
59 #endif
60 /*
61 * Some operating systems do not permit select(2) on their random devices,
62 * defining this to zero will force the use of read(2) to extract one byte
63 * from /dev/random.
64 */
65 #ifndef DEVRANDM_WAIT_USE_SELECT
66 #define DEVRANDM_WAIT_USE_SELECT 1
67 #endif
68 /*
69 * Define the shared memory identifier used to indicate if the operating
70 * system has properly seeded the DEVRANDOM source.
71 */
72 #ifndef OPENSSL_RAND_SEED_DEVRANDOM_SHM_ID
73 #define OPENSSL_RAND_SEED_DEVRANDOM_SHM_ID 114
74 #endif
75
76 #endif
77 #endif
78
79 #if !defined(OPENSSL_NO_EGD) && !defined(DEVRANDOM_EGD)
80 /*
81 * set this to a comma-separated list of 'egd' sockets to try out. These
82 * sockets will be tried in the order listed in case accessing the device
83 * files listed in DEVRANDOM did not return enough randomness.
84 */
85 #define DEVRANDOM_EGD "/var/run/egd-pool", "/dev/egd-pool", "/etc/egd-pool", "/etc/entropy"
86 #endif
87
88 void ossl_rand_cleanup_int(void);
89
90 /*
91 * Initialise the random pool reseeding sources.
92 *
93 * Returns 1 on success and 0 on failure.
94 */
95 int ossl_rand_pool_init(void);
96
97 /*
98 * Finalise the random pool reseeding sources.
99 */
100 void ossl_rand_pool_cleanup(void);
101
102 /*
103 * Control the random pool use of open file descriptors.
104 */
105 void ossl_rand_pool_keep_random_devices_open(int keep);
106
107 /*
108 * Configuration
109 */
110 void ossl_random_add_conf_module(void);
111
112 /*
113 * Get and cleanup random seed material.
114 */
115 size_t ossl_rand_get_entropy(OSSL_LIB_CTX *ctx,
116 unsigned char **pout, int entropy,
117 size_t min_len, size_t max_len);
118 size_t ossl_rand_get_user_entropy(OSSL_LIB_CTX *ctx,
119 unsigned char **pout, int entropy,
120 size_t min_len, size_t max_len);
121 void ossl_rand_cleanup_entropy(OSSL_LIB_CTX *ctx,
122 unsigned char *buf, size_t len);
123 void ossl_rand_cleanup_user_entropy(OSSL_LIB_CTX *ctx,
124 unsigned char *buf, size_t len);
125 size_t ossl_rand_get_nonce(OSSL_LIB_CTX *ctx,
126 unsigned char **pout, size_t min_len, size_t max_len,
127 const void *salt, size_t salt_len);
128 size_t ossl_rand_get_user_nonce(OSSL_LIB_CTX *ctx, unsigned char **pout,
129 size_t min_len, size_t max_len,
130 const void *salt, size_t salt_len);
131 void ossl_rand_cleanup_nonce(OSSL_LIB_CTX *ctx,
132 unsigned char *buf, size_t len);
133 void ossl_rand_cleanup_user_nonce(OSSL_LIB_CTX *ctx,
134 unsigned char *buf, size_t len);
135
136 /*
137 * Get seeding material from the operating system sources.
138 */
139 size_t ossl_pool_acquire_entropy(RAND_POOL *pool);
140 int ossl_pool_add_nonce_data(RAND_POOL *pool);
141
142 #ifdef FIPS_MODULE
143 EVP_RAND_CTX *ossl_rand_get0_private_noncreating(OSSL_LIB_CTX *ctx);
144 #else
145 EVP_RAND_CTX *ossl_rand_get0_seed_noncreating(OSSL_LIB_CTX *ctx);
146 #endif
147
148 /* Generate a uniformly distributed random integer in the interval [0, upper) */
149 uint32_t ossl_rand_uniform_uint32(OSSL_LIB_CTX *ctx, uint32_t upper, int *err);
150
151 /*
152 * Generate a uniformly distributed random integer in the interval
153 * [lower, upper).
154 */
155 uint32_t ossl_rand_range_uint32(OSSL_LIB_CTX *ctx, uint32_t lower, uint32_t upper,
156 int *err);
157
158 /*
159 * Check if the named provider is the nominated entropy/random provider.
160 * If it is, use it.
161 */
162 #ifndef FIPS_MODULE
163 int ossl_rand_check_random_provider_on_load(OSSL_LIB_CTX *ctx,
164 OSSL_PROVIDER *prov);
165 int ossl_rand_check_random_provider_on_unload(OSSL_LIB_CTX *ctx,
166 OSSL_PROVIDER *prov);
167 #endif /* FIPS_MODULE */
168
169 #endif