2 * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
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
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.
18 #ifndef OSSL_CRYPTO_RAND_H
19 # define OSSL_CRYPTO_RAND_H
22 # include <openssl/rand.h>
23 # include "crypto/rand_pool.h"
26 * Defines related to seed sources
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
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"
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.
43 # ifndef DEVRANDOM_SAFE_KERNEL
44 # define DEVRANDOM_SAFE_KERNEL 4, 8
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
51 # ifndef DEVRANDM_WAIT_USE_SELECT
52 # define DEVRANDM_WAIT_USE_SELECT 1
55 * Define the shared memory identifier used to indicate if the operating
56 * system has properly seeded the DEVRANDOM source.
58 # ifndef OPENSSL_RAND_SEED_DEVRANDOM_SHM_ID
59 # define OPENSSL_RAND_SEED_DEVRANDOM_SHM_ID 114
65 #if !defined(OPENSSL_NO_EGD) && !defined(DEVRANDOM_EGD)
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.
71 # define DEVRANDOM_EGD "/var/run/egd-pool", "/dev/egd-pool", "/etc/egd-pool", "/etc/entropy"
74 void rand_cleanup_int(void);
77 * Initialise the random pool reseeding sources.
79 * Returns 1 on success and 0 on failure.
81 int rand_pool_init(void);
84 * Finalise the random pool reseeding sources.
86 void rand_pool_cleanup(void);
89 * Control the random pool use of open file descriptors.
91 void rand_pool_keep_random_devices_open(int keep);
96 void ossl_random_add_conf_module(void);
99 * Get and cleanup random seed material.
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);
113 * Get seeding material from the operating system sources.
115 size_t ossl_pool_acquire_entropy(RAND_POOL *pool);
116 int ossl_pool_add_nonce_data(RAND_POOL *pool);