2023-09-27 18:23:52 -04:00
|
|
|
/*
|
2023-10-25 12:45:38 -04:00
|
|
|
* Copyright 2016-2023 The OpenSSL Project Authors. All Rights Reserved.
|
2023-09-27 18:23:52 -04:00
|
|
|
*
|
2023-10-11 14:44:50 -04:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2023-09-27 18:23:52 -04: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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2023-10-11 14:44:50 -04:00
|
|
|
* Licensed under the Apache License 2.0 (the "License");
|
2023-09-27 18:23:52 -04: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OSSL_CRYPTO_RAND_H
|
|
|
|
# define OSSL_CRYPTO_RAND_H
|
2023-10-11 14:44:50 -04:00
|
|
|
# pragma once
|
2023-09-27 18:23:52 -04:00
|
|
|
|
|
|
|
# include <openssl/rand.h>
|
2023-10-11 14:44:50 -04:00
|
|
|
# include "crypto/rand_pool.h"
|
2023-09-27 18:23:52 -04:00
|
|
|
|
|
|
|
# if defined(__APPLE__) && !defined(OPENSSL_NO_APPLE_CRYPTO_RANDOM)
|
|
|
|
# include <Availability.h>
|
|
|
|
# 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)
|
|
|
|
# define OPENSSL_APPLE_CRYPTO_RANDOM 1
|
|
|
|
# include <CommonCrypto/CommonCryptoError.h>
|
|
|
|
# include <CommonCrypto/CommonRandom.h>
|
|
|
|
# endif
|
|
|
|
# endif
|
|
|
|
|
|
|
|
/*
|
2023-10-11 14:44:50 -04:00
|
|
|
* Defines related to seed sources
|
2023-09-27 18:23:52 -04:00
|
|
|
*/
|
2023-10-11 14:44:50 -04:00
|
|
|
#ifndef DEVRANDOM
|
2023-09-27 18:23:52 -04:00
|
|
|
/*
|
2023-10-11 14:44:50 -04:00
|
|
|
* 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
|
2023-09-27 18:23:52 -04:00
|
|
|
*/
|
2023-10-11 14:44:50 -04:00
|
|
|
# define DEVRANDOM "/dev/urandom", "/dev/random", "/dev/hwrng", "/dev/srandom"
|
|
|
|
# if defined(__linux) && !defined(__ANDROID__)
|
|
|
|
# ifndef DEVRANDOM_WAIT
|
|
|
|
# define DEVRANDOM_WAIT "/dev/random"
|
|
|
|
# endif
|
2023-09-27 18:23:52 -04:00
|
|
|
/*
|
2023-10-11 14:44:50 -04:00
|
|
|
* 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.
|
2023-09-27 18:23:52 -04:00
|
|
|
*/
|
2023-10-11 14:44:50 -04:00
|
|
|
# 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
|
2023-09-27 18:23:52 -04:00
|
|
|
|
2023-10-11 14:44:50 -04:00
|
|
|
# endif
|
|
|
|
#endif
|
2023-09-27 18:23:52 -04:00
|
|
|
|
2023-10-11 14:44:50 -04:00
|
|
|
#if !defined(OPENSSL_NO_EGD) && !defined(DEVRANDOM_EGD)
|
2023-09-27 18:23:52 -04:00
|
|
|
/*
|
2023-10-11 14:44:50 -04:00
|
|
|
* 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.
|
2023-09-27 18:23:52 -04:00
|
|
|
*/
|
2023-10-11 14:44:50 -04:00
|
|
|
# define DEVRANDOM_EGD "/var/run/egd-pool", "/dev/egd-pool", "/etc/egd-pool", "/etc/entropy"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void ossl_rand_cleanup_int(void);
|
2023-09-27 18:23:52 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialise the random pool reseeding sources.
|
|
|
|
*
|
|
|
|
* Returns 1 on success and 0 on failure.
|
|
|
|
*/
|
2023-10-11 14:44:50 -04:00
|
|
|
int ossl_rand_pool_init(void);
|
2023-09-27 18:23:52 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Finalise the random pool reseeding sources.
|
|
|
|
*/
|
2023-10-11 14:44:50 -04:00
|
|
|
void ossl_rand_pool_cleanup(void);
|
2023-09-27 18:23:52 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Control the random pool use of open file descriptors.
|
|
|
|
*/
|
2023-10-11 14:44:50 -04:00
|
|
|
void ossl_rand_pool_keep_random_devices_open(int keep);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Configuration
|
|
|
|
*/
|
|
|
|
void ossl_random_add_conf_module(void);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get and cleanup random seed material.
|
|
|
|
*/
|
2023-10-25 12:45:38 -04:00
|
|
|
size_t ossl_rand_get_entropy(OSSL_LIB_CTX *ctx,
|
2023-10-11 14:44:50 -04:00
|
|
|
unsigned char **pout, int entropy,
|
|
|
|
size_t min_len, size_t max_len);
|
2023-10-25 12:45:38 -04:00
|
|
|
size_t ossl_rand_get_user_entropy(OSSL_LIB_CTX *ctx,
|
|
|
|
unsigned char **pout, int entropy,
|
|
|
|
size_t min_len, size_t max_len);
|
|
|
|
void ossl_rand_cleanup_entropy(OSSL_LIB_CTX *ctx,
|
2023-10-11 14:44:50 -04:00
|
|
|
unsigned char *buf, size_t len);
|
2023-10-25 12:45:38 -04:00
|
|
|
void ossl_rand_cleanup_user_entropy(OSSL_LIB_CTX *ctx,
|
|
|
|
unsigned char *buf, size_t len);
|
|
|
|
size_t ossl_rand_get_nonce(OSSL_LIB_CTX *ctx,
|
2023-10-11 14:44:50 -04:00
|
|
|
unsigned char **pout, size_t min_len, size_t max_len,
|
|
|
|
const void *salt, size_t salt_len);
|
2023-10-25 12:45:38 -04:00
|
|
|
size_t ossl_rand_get_user_nonce(OSSL_LIB_CTX *ctx, unsigned char **pout,
|
|
|
|
size_t min_len, size_t max_len,
|
|
|
|
const void *salt, size_t salt_len);
|
|
|
|
void ossl_rand_cleanup_nonce(OSSL_LIB_CTX *ctx,
|
2023-10-11 14:44:50 -04:00
|
|
|
unsigned char *buf, size_t len);
|
2023-10-25 12:45:38 -04:00
|
|
|
void ossl_rand_cleanup_user_nonce(OSSL_LIB_CTX *ctx,
|
|
|
|
unsigned char *buf, size_t len);
|
2023-10-11 14:44:50 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
2023-09-27 18:23:52 -04:00
|
|
|
|
2023-10-25 12:45:38 -04:00
|
|
|
# ifdef FIPS_MODULE
|
|
|
|
EVP_RAND_CTX *ossl_rand_get0_private_noncreating(OSSL_LIB_CTX *ctx);
|
|
|
|
# else
|
|
|
|
EVP_RAND_CTX *ossl_rand_get0_seed_noncreating(OSSL_LIB_CTX *ctx);
|
|
|
|
# endif
|
2023-11-25 12:30:55 -05:00
|
|
|
|
|
|
|
/* Generate a uniformly distributed random integer in the interval [0, upper) */
|
|
|
|
uint32_t ossl_rand_uniform_uint32(OSSL_LIB_CTX *ctx, uint32_t upper, int *err);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generate a uniformly distributed random integer in the interval
|
|
|
|
* [lower, upper).
|
|
|
|
*/
|
|
|
|
uint32_t ossl_rand_range_uint32(OSSL_LIB_CTX *ctx, uint32_t lower, uint32_t upper,
|
|
|
|
int *err);
|
|
|
|
|
2023-09-27 18:23:52 -04:00
|
|
|
#endif
|