rand.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright 2016-2018 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. # include <openssl/rand.h>
  19. /* forward declaration */
  20. typedef struct rand_pool_st RAND_POOL;
  21. /*
  22. * Defines related to seed sources
  23. */
  24. #ifndef DEVRANDOM
  25. /*
  26. * set this to a comma-separated list of 'random' device files to try out. By
  27. * default, we will try to read at least one of these files
  28. */
  29. # define DEVRANDOM "/dev/urandom", "/dev/random", "/dev/hwrng", "/dev/srandom"
  30. # if defined(__linux) && !defined(__ANDROID__)
  31. # ifndef DEVRANDOM_WAIT
  32. # define DEVRANDOM_WAIT "/dev/random"
  33. # endif
  34. /*
  35. * Linux kernels 4.8 and later changes how their random device works and there
  36. * is no reliable way to tell that /dev/urandom has been seeded -- getentropy(2)
  37. * should be used instead.
  38. */
  39. # ifndef DEVRANDOM_SAFE_KERNEL
  40. # define DEVRANDOM_SAFE_KERNEL 4, 8
  41. # endif
  42. /*
  43. * Some operating systems do not permit select(2) on their random devices,
  44. * defining this to zero will force the use of read(2) to extract one byte
  45. * from /dev/random.
  46. */
  47. # ifndef DEVRANDM_WAIT_USE_SELECT
  48. # define DEVRANDM_WAIT_USE_SELECT 1
  49. # endif
  50. /*
  51. * Define the shared memory identifier used to indicate if the operating
  52. * system has properly seeded the DEVRANDOM source.
  53. */
  54. # ifndef OPENSSL_RAND_SEED_DEVRANDOM_SHM_ID
  55. # define OPENSSL_RAND_SEED_DEVRANDOM_SHM_ID 114
  56. # endif
  57. # endif
  58. #endif
  59. #if !defined(OPENSSL_NO_EGD) && !defined(DEVRANDOM_EGD)
  60. /*
  61. * set this to a comma-separated list of 'egd' sockets to try out. These
  62. * sockets will be tried in the order listed in case accessing the device
  63. * files listed in DEVRANDOM did not return enough randomness.
  64. */
  65. # define DEVRANDOM_EGD "/var/run/egd-pool", "/dev/egd-pool", "/etc/egd-pool", "/etc/entropy"
  66. #endif
  67. void rand_cleanup_int(void);
  68. /* Hardware-based seeding functions. */
  69. size_t rand_acquire_entropy_from_tsc(RAND_POOL *pool);
  70. size_t rand_acquire_entropy_from_cpu(RAND_POOL *pool);
  71. /* DRBG entropy callbacks. */
  72. size_t rand_drbg_get_entropy(RAND_DRBG *drbg,
  73. unsigned char **pout,
  74. int entropy, size_t min_len, size_t max_len,
  75. int prediction_resistance);
  76. void rand_drbg_cleanup_entropy(RAND_DRBG *drbg,
  77. unsigned char *out, size_t outlen);
  78. size_t rand_drbg_get_nonce(RAND_DRBG *drbg,
  79. unsigned char **pout,
  80. int entropy, size_t min_len, size_t max_len);
  81. void rand_drbg_cleanup_nonce(RAND_DRBG *drbg,
  82. unsigned char *out, size_t outlen);
  83. size_t rand_drbg_get_additional_data(RAND_POOL *pool, unsigned char **pout);
  84. void rand_drbg_cleanup_additional_data(RAND_POOL *pool, unsigned char *out);
  85. /* CRNG test entropy filter callbacks. */
  86. size_t rand_crngt_get_entropy(RAND_DRBG *drbg,
  87. unsigned char **pout,
  88. int entropy, size_t min_len, size_t max_len,
  89. int prediction_resistance);
  90. void rand_crngt_cleanup_entropy(RAND_DRBG *drbg,
  91. unsigned char *out, size_t outlen);
  92. /*
  93. * RAND_POOL functions
  94. */
  95. RAND_POOL *rand_pool_new(int entropy_requested, int secure,
  96. size_t min_len, size_t max_len);
  97. RAND_POOL *rand_pool_attach(const unsigned char *buffer, size_t len,
  98. size_t entropy);
  99. void rand_pool_free(RAND_POOL *pool);
  100. const unsigned char *rand_pool_buffer(RAND_POOL *pool);
  101. unsigned char *rand_pool_detach(RAND_POOL *pool);
  102. void rand_pool_reattach(RAND_POOL *pool, unsigned char *buffer);
  103. size_t rand_pool_entropy(RAND_POOL *pool);
  104. size_t rand_pool_length(RAND_POOL *pool);
  105. size_t rand_pool_entropy_available(RAND_POOL *pool);
  106. size_t rand_pool_entropy_needed(RAND_POOL *pool);
  107. /* |entropy_factor| expresses how many bits of data contain 1 bit of entropy */
  108. size_t rand_pool_bytes_needed(RAND_POOL *pool, unsigned int entropy_factor);
  109. size_t rand_pool_bytes_remaining(RAND_POOL *pool);
  110. int rand_pool_add(RAND_POOL *pool,
  111. const unsigned char *buffer, size_t len, size_t entropy);
  112. unsigned char *rand_pool_add_begin(RAND_POOL *pool, size_t len);
  113. int rand_pool_add_end(RAND_POOL *pool, size_t len, size_t entropy);
  114. /*
  115. * Add random bytes to the pool to acquire requested amount of entropy
  116. *
  117. * This function is platform specific and tries to acquire the requested
  118. * amount of entropy by polling platform specific entropy sources.
  119. *
  120. * If the function succeeds in acquiring at least |entropy_requested| bits
  121. * of entropy, the total entropy count is returned. If it fails, it returns
  122. * an entropy count of 0.
  123. */
  124. size_t rand_pool_acquire_entropy(RAND_POOL *pool);
  125. /*
  126. * Add some application specific nonce data
  127. *
  128. * This function is platform specific and adds some application specific
  129. * data to the nonce used for instantiating the drbg.
  130. *
  131. * This data currently consists of the process and thread id, and a high
  132. * resolution timestamp. The data does not include an atomic counter,
  133. * because that is added by the calling function rand_drbg_get_nonce().
  134. *
  135. * Returns 1 on success and 0 on failure.
  136. */
  137. int rand_pool_add_nonce_data(RAND_POOL *pool);
  138. /*
  139. * Add some platform specific additional data
  140. *
  141. * This function is platform specific and adds some random noise to the
  142. * additional data used for generating random bytes and for reseeding
  143. * the drbg.
  144. *
  145. * Returns 1 on success and 0 on failure.
  146. */
  147. int rand_pool_add_additional_data(RAND_POOL *pool);
  148. /*
  149. * Initialise the random pool reseeding sources.
  150. *
  151. * Returns 1 on success and 0 on failure.
  152. */
  153. int rand_pool_init(void);
  154. /*
  155. * Finalise the random pool reseeding sources.
  156. */
  157. void rand_pool_cleanup(void);
  158. /*
  159. * Control the random pool use of open file descriptors.
  160. */
  161. void rand_pool_keep_random_devices_open(int keep);
  162. #endif