rand.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright 2016-2023 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. * Licensed under the Apache License 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. * https://www.openssl.org/source/license.html
  14. * or in the file LICENSE in the source distribution.
  15. */
  16. #ifndef OSSL_CRYPTO_RAND_H
  17. #define OSSL_CRYPTO_RAND_H
  18. #pragma once
  19. #include <openssl/rand.h>
  20. #include "crypto/rand_pool.h"
  21. #if defined(__APPLE__) && !defined(OPENSSL_NO_APPLE_CRYPTO_RANDOM)
  22. #include <Availability.h>
  23. #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)
  24. #define OPENSSL_APPLE_CRYPTO_RANDOM 1
  25. #include <CommonCrypto/CommonCryptoError.h>
  26. #include <CommonCrypto/CommonRandom.h>
  27. #endif
  28. #endif
  29. /*
  30. * Defines related to seed sources
  31. */
  32. #ifndef DEVRANDOM
  33. /*
  34. * set this to a comma-separated list of 'random' device files to try out. By
  35. * default, we will try to read at least one of these files
  36. */
  37. #define DEVRANDOM "/dev/urandom", "/dev/random", "/dev/hwrng", "/dev/srandom"
  38. #if defined(__linux) && !defined(__ANDROID__)
  39. #ifndef DEVRANDOM_WAIT
  40. #define DEVRANDOM_WAIT "/dev/random"
  41. #endif
  42. /*
  43. * Linux kernels 4.8 and later changes how their random device works and there
  44. * is no reliable way to tell that /dev/urandom has been seeded -- getentropy(2)
  45. * should be used instead.
  46. */
  47. #ifndef DEVRANDOM_SAFE_KERNEL
  48. #define DEVRANDOM_SAFE_KERNEL 4, 8
  49. #endif
  50. /*
  51. * Some operating systems do not permit select(2) on their random devices,
  52. * defining this to zero will force the use of read(2) to extract one byte
  53. * from /dev/random.
  54. */
  55. #ifndef DEVRANDM_WAIT_USE_SELECT
  56. #define DEVRANDM_WAIT_USE_SELECT 1
  57. #endif
  58. /*
  59. * Define the shared memory identifier used to indicate if the operating
  60. * system has properly seeded the DEVRANDOM source.
  61. */
  62. #ifndef OPENSSL_RAND_SEED_DEVRANDOM_SHM_ID
  63. #define OPENSSL_RAND_SEED_DEVRANDOM_SHM_ID 114
  64. #endif
  65. #endif
  66. #endif
  67. #if !defined(OPENSSL_NO_EGD) && !defined(DEVRANDOM_EGD)
  68. /*
  69. * set this to a comma-separated list of 'egd' sockets to try out. These
  70. * sockets will be tried in the order listed in case accessing the device
  71. * files listed in DEVRANDOM did not return enough randomness.
  72. */
  73. #define DEVRANDOM_EGD "/var/run/egd-pool", "/dev/egd-pool", "/etc/egd-pool", "/etc/entropy"
  74. #endif
  75. void ossl_rand_cleanup_int(void);
  76. /*
  77. * Initialise the random pool reseeding sources.
  78. *
  79. * Returns 1 on success and 0 on failure.
  80. */
  81. int ossl_rand_pool_init(void);
  82. /*
  83. * Finalise the random pool reseeding sources.
  84. */
  85. void ossl_rand_pool_cleanup(void);
  86. /*
  87. * Control the random pool use of open file descriptors.
  88. */
  89. void ossl_rand_pool_keep_random_devices_open(int keep);
  90. /*
  91. * Configuration
  92. */
  93. void ossl_random_add_conf_module(void);
  94. /*
  95. * Get and cleanup random seed material.
  96. */
  97. size_t ossl_rand_get_entropy(OSSL_LIB_CTX *ctx,
  98. unsigned char **pout, int entropy,
  99. size_t min_len, size_t max_len);
  100. size_t ossl_rand_get_user_entropy(OSSL_LIB_CTX *ctx,
  101. unsigned char **pout, int entropy,
  102. size_t min_len, size_t max_len);
  103. void ossl_rand_cleanup_entropy(OSSL_LIB_CTX *ctx,
  104. unsigned char *buf, size_t len);
  105. void ossl_rand_cleanup_user_entropy(OSSL_LIB_CTX *ctx,
  106. unsigned char *buf, size_t len);
  107. size_t ossl_rand_get_nonce(OSSL_LIB_CTX *ctx,
  108. unsigned char **pout, size_t min_len, size_t max_len,
  109. const void *salt, size_t salt_len);
  110. size_t ossl_rand_get_user_nonce(OSSL_LIB_CTX *ctx, unsigned char **pout,
  111. size_t min_len, size_t max_len,
  112. const void *salt, size_t salt_len);
  113. void ossl_rand_cleanup_nonce(OSSL_LIB_CTX *ctx,
  114. unsigned char *buf, size_t len);
  115. void ossl_rand_cleanup_user_nonce(OSSL_LIB_CTX *ctx,
  116. unsigned char *buf, size_t len);
  117. /*
  118. * Get seeding material from the operating system sources.
  119. */
  120. size_t ossl_pool_acquire_entropy(RAND_POOL *pool);
  121. int ossl_pool_add_nonce_data(RAND_POOL *pool);
  122. #ifdef FIPS_MODULE
  123. EVP_RAND_CTX *ossl_rand_get0_private_noncreating(OSSL_LIB_CTX *ctx);
  124. #else
  125. EVP_RAND_CTX *ossl_rand_get0_seed_noncreating(OSSL_LIB_CTX *ctx);
  126. #endif
  127. /* Generate a uniformly distributed random integer in the interval [0, upper) */
  128. uint32_t ossl_rand_uniform_uint32(OSSL_LIB_CTX *ctx, uint32_t upper, int *err);
  129. /*
  130. * Generate a uniformly distributed random integer in the interval
  131. * [lower, upper).
  132. */
  133. uint32_t ossl_rand_range_uint32(OSSL_LIB_CTX *ctx, uint32_t lower, uint32_t upper,
  134. int *err);
  135. #endif