rand.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include <contrib/libs/openssl/redef.h>
  2. /*
  3. * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
  4. *
  5. * Licensed under the OpenSSL license (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. /*
  11. * Licensed under the OpenSSL licenses, (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.
  16. */
  17. #ifndef OSSL_CRYPTO_RAND_H
  18. # define OSSL_CRYPTO_RAND_H
  19. # include <openssl/rand.h>
  20. # if defined(__APPLE__) && !defined(OPENSSL_NO_APPLE_CRYPTO_RANDOM)
  21. # include <Availability.h>
  22. # if (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200) || \
  23. (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. /* forward declaration */
  30. typedef struct rand_pool_st RAND_POOL;
  31. void rand_cleanup_int(void);
  32. void rand_drbg_cleanup_int(void);
  33. void drbg_delete_thread_state(void);
  34. /* Hardware-based seeding functions. */
  35. size_t rand_acquire_entropy_from_tsc(RAND_POOL *pool);
  36. size_t rand_acquire_entropy_from_cpu(RAND_POOL *pool);
  37. /* DRBG entropy callbacks. */
  38. size_t rand_drbg_get_entropy(RAND_DRBG *drbg,
  39. unsigned char **pout,
  40. int entropy, size_t min_len, size_t max_len,
  41. int prediction_resistance);
  42. void rand_drbg_cleanup_entropy(RAND_DRBG *drbg,
  43. unsigned char *out, size_t outlen);
  44. size_t rand_drbg_get_nonce(RAND_DRBG *drbg,
  45. unsigned char **pout,
  46. int entropy, size_t min_len, size_t max_len);
  47. void rand_drbg_cleanup_nonce(RAND_DRBG *drbg,
  48. unsigned char *out, size_t outlen);
  49. size_t rand_drbg_get_additional_data(RAND_POOL *pool, unsigned char **pout);
  50. void rand_drbg_cleanup_additional_data(RAND_POOL *pool, unsigned char *out);
  51. /*
  52. * RAND_POOL functions
  53. */
  54. RAND_POOL *rand_pool_new(int entropy_requested, int secure,
  55. size_t min_len, size_t max_len);
  56. RAND_POOL *rand_pool_attach(const unsigned char *buffer, size_t len,
  57. size_t entropy);
  58. void rand_pool_free(RAND_POOL *pool);
  59. const unsigned char *rand_pool_buffer(RAND_POOL *pool);
  60. unsigned char *rand_pool_detach(RAND_POOL *pool);
  61. void rand_pool_reattach(RAND_POOL *pool, unsigned char *buffer);
  62. size_t rand_pool_entropy(RAND_POOL *pool);
  63. size_t rand_pool_length(RAND_POOL *pool);
  64. size_t rand_pool_entropy_available(RAND_POOL *pool);
  65. size_t rand_pool_entropy_needed(RAND_POOL *pool);
  66. /* |entropy_factor| expresses how many bits of data contain 1 bit of entropy */
  67. size_t rand_pool_bytes_needed(RAND_POOL *pool, unsigned int entropy_factor);
  68. size_t rand_pool_bytes_remaining(RAND_POOL *pool);
  69. int rand_pool_add(RAND_POOL *pool,
  70. const unsigned char *buffer, size_t len, size_t entropy);
  71. unsigned char *rand_pool_add_begin(RAND_POOL *pool, size_t len);
  72. int rand_pool_add_end(RAND_POOL *pool, size_t len, size_t entropy);
  73. /*
  74. * Add random bytes to the pool to acquire requested amount of entropy
  75. *
  76. * This function is platform specific and tries to acquire the requested
  77. * amount of entropy by polling platform specific entropy sources.
  78. *
  79. * If the function succeeds in acquiring at least |entropy_requested| bits
  80. * of entropy, the total entropy count is returned. If it fails, it returns
  81. * an entropy count of 0.
  82. */
  83. size_t rand_pool_acquire_entropy(RAND_POOL *pool);
  84. /*
  85. * Add some application specific nonce data
  86. *
  87. * This function is platform specific and adds some application specific
  88. * data to the nonce used for instantiating the drbg.
  89. *
  90. * This data currently consists of the process and thread id, and a high
  91. * resolution timestamp. The data does not include an atomic counter,
  92. * because that is added by the calling function rand_drbg_get_nonce().
  93. *
  94. * Returns 1 on success and 0 on failure.
  95. */
  96. int rand_pool_add_nonce_data(RAND_POOL *pool);
  97. /*
  98. * Add some platform specific additional data
  99. *
  100. * This function is platform specific and adds some random noise to the
  101. * additional data used for generating random bytes and for reseeding
  102. * the drbg.
  103. *
  104. * Returns 1 on success and 0 on failure.
  105. */
  106. int rand_pool_add_additional_data(RAND_POOL *pool);
  107. /*
  108. * Initialise the random pool reseeding sources.
  109. *
  110. * Returns 1 on success and 0 on failure.
  111. */
  112. int rand_pool_init(void);
  113. /*
  114. * Finalise the random pool reseeding sources.
  115. */
  116. void rand_pool_cleanup(void);
  117. /*
  118. * Control the random pool use of open file descriptors.
  119. */
  120. void rand_pool_keep_random_devices_open(int keep);
  121. #endif