| 
									
										
										
										
											2016-05-18 03:38:09 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2021-02-18 22:57:13 +08:00
										 |  |  |  * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2016-05-18 03:38:09 +08:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-12-06 20:12:35 +08:00
										 |  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use | 
					
						
							| 
									
										
										
										
											2016-05-18 03:38:09 +08:00
										 |  |  |  * this file except in compliance with the License.  You can obtain a copy | 
					
						
							|  |  |  |  * in the file LICENSE in the source distribution or at | 
					
						
							|  |  |  |  * https://www.openssl.org/source/license.html
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-06 18:19:55 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2018-12-06 20:12:35 +08:00
										 |  |  |  * Licensed under the Apache License 2.0 (the "License"); | 
					
						
							| 
									
										
										
										
											2016-04-06 18:19:55 +08:00
										 |  |  |  * you may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  * You may obtain a copy of the License at | 
					
						
							|  |  |  |  * https://www.openssl.org/source/license.html
 | 
					
						
							|  |  |  |  * or in the file LICENSE in the source distribution. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-28 06:45:57 +08:00
										 |  |  | #ifndef OSSL_CRYPTO_RAND_H
 | 
					
						
							|  |  |  | # define OSSL_CRYPTO_RAND_H
 | 
					
						
							| 
									
										
										
										
											2021-02-07 05:36:46 +08:00
										 |  |  | # pragma once
 | 
					
						
							| 
									
										
										
										
											2018-03-06 06:45:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | # include <openssl/rand.h>
 | 
					
						
							| 
									
										
										
										
											2020-10-30 13:53:22 +08:00
										 |  |  | # include "crypto/rand_pool.h"
 | 
					
						
							| 
									
										
										
										
											2018-03-06 06:45:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-06 08:50:43 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Defines related to seed sources | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | #ifndef DEVRANDOM
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * set this to a comma-separated list of 'random' device files to try out. By | 
					
						
							|  |  |  |  * default, we will try to read at least one of these files | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | # define DEVRANDOM "/dev/urandom", "/dev/random", "/dev/hwrng", "/dev/srandom"
 | 
					
						
							|  |  |  | # if defined(__linux) && !defined(__ANDROID__)
 | 
					
						
							|  |  |  | #  ifndef DEVRANDOM_WAIT
 | 
					
						
							|  |  |  | #   define DEVRANDOM_WAIT   "/dev/random"
 | 
					
						
							|  |  |  | #  endif
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Linux kernels 4.8 and later changes how their random device works and there | 
					
						
							|  |  |  |  * is no reliable way to tell that /dev/urandom has been seeded -- getentropy(2) | 
					
						
							|  |  |  |  * should be used instead. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | #  ifndef DEVRANDOM_SAFE_KERNEL
 | 
					
						
							|  |  |  | #   define DEVRANDOM_SAFE_KERNEL        4, 8
 | 
					
						
							|  |  |  | #  endif
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Some operating systems do not permit select(2) on their random devices, | 
					
						
							|  |  |  |  * defining this to zero will force the use of read(2) to extract one byte | 
					
						
							|  |  |  |  * from /dev/random. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | #  ifndef DEVRANDM_WAIT_USE_SELECT
 | 
					
						
							|  |  |  | #   define DEVRANDM_WAIT_USE_SELECT     1
 | 
					
						
							|  |  |  | #  endif
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Define the shared memory identifier used to indicate if the operating | 
					
						
							|  |  |  |  * system has properly seeded the DEVRANDOM source. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | #  ifndef OPENSSL_RAND_SEED_DEVRANDOM_SHM_ID
 | 
					
						
							|  |  |  | #   define OPENSSL_RAND_SEED_DEVRANDOM_SHM_ID 114
 | 
					
						
							|  |  |  | #  endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # endif
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if !defined(OPENSSL_NO_EGD) && !defined(DEVRANDOM_EGD)
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * set this to a comma-separated list of 'egd' sockets to try out. These | 
					
						
							|  |  |  |  * sockets will be tried in the order listed in case accessing the device | 
					
						
							|  |  |  |  * files listed in DEVRANDOM did not return enough randomness. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | # define DEVRANDOM_EGD "/var/run/egd-pool", "/dev/egd-pool", "/etc/egd-pool", "/etc/entropy"
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 17:17:53 +08:00
										 |  |  | void ossl_rand_cleanup_int(void); | 
					
						
							| 
									
										
										
										
											2018-03-06 06:45:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-07 07:31:44 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Initialise the random pool reseeding sources. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Returns 1 on success and 0 on failure. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2021-03-08 17:17:53 +08:00
										 |  |  | int ossl_rand_pool_init(void); | 
					
						
							| 
									
										
										
										
											2018-06-07 07:31:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Finalise the random pool reseeding sources. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2021-03-08 17:17:53 +08:00
										 |  |  | void ossl_rand_pool_cleanup(void); | 
					
						
							| 
									
										
										
										
											2018-06-07 07:31:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Control the random pool use of open file descriptors. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2021-03-08 17:17:53 +08:00
										 |  |  | void ossl_rand_pool_keep_random_devices_open(int keep); | 
					
						
							| 
									
										
										
										
											2018-06-07 07:31:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-21 14:07:34 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Configuration | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | void ossl_random_add_conf_module(void); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-30 13:53:22 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Get and cleanup random seed material. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | size_t ossl_rand_get_entropy(ossl_unused OSSL_CORE_HANDLE *handle, | 
					
						
							|  |  |  |                              unsigned char **pout, int entropy, | 
					
						
							|  |  |  |                              size_t min_len, size_t max_len); | 
					
						
							|  |  |  | void ossl_rand_cleanup_entropy(ossl_unused OSSL_CORE_HANDLE *handle, | 
					
						
							|  |  |  |                                unsigned char *buf, size_t len); | 
					
						
							|  |  |  | size_t ossl_rand_get_nonce(ossl_unused OSSL_CORE_HANDLE *handle, | 
					
						
							|  |  |  |                            unsigned char **pout, size_t min_len, size_t max_len, | 
					
						
							|  |  |  |                            const void *salt, size_t salt_len); | 
					
						
							|  |  |  | void ossl_rand_cleanup_nonce(ossl_unused OSSL_CORE_HANDLE *handle, | 
					
						
							|  |  |  |                              unsigned char *buf, size_t len); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Get seeding material from the operating system sources. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | size_t ossl_pool_acquire_entropy(RAND_POOL *pool); | 
					
						
							|  |  |  | int ossl_pool_add_nonce_data(RAND_POOL *pool); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-06 06:45:44 +08:00
										 |  |  | #endif
 |