rand_int.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 OpenSSL licenses, (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 HEADER_RAND_INT_H
  17. # define HEADER_RAND_INT_H
  18. # include <openssl/rand.h>
  19. /* forward declaration */
  20. typedef struct rand_pool_st RAND_POOL;
  21. void rand_cleanup_int(void);
  22. void rand_drbg_cleanup_int(void);
  23. void drbg_delete_thread_state(void);
  24. void rand_fork(void);
  25. /* Hardware-based seeding functions. */
  26. size_t rand_acquire_entropy_from_tsc(RAND_POOL *pool);
  27. size_t rand_acquire_entropy_from_cpu(RAND_POOL *pool);
  28. /* DRBG entropy callbacks. */
  29. size_t rand_drbg_get_entropy(RAND_DRBG *drbg,
  30. unsigned char **pout,
  31. int entropy, size_t min_len, size_t max_len,
  32. int prediction_resistance);
  33. void rand_drbg_cleanup_entropy(RAND_DRBG *drbg,
  34. unsigned char *out, size_t outlen);
  35. size_t rand_drbg_get_nonce(RAND_DRBG *drbg,
  36. unsigned char **pout,
  37. int entropy, size_t min_len, size_t max_len);
  38. void rand_drbg_cleanup_nonce(RAND_DRBG *drbg,
  39. unsigned char *out, size_t outlen);
  40. size_t rand_drbg_get_additional_data(unsigned char **pout, size_t max_len);
  41. void rand_drbg_cleanup_additional_data(unsigned char *out, size_t outlen);
  42. /*
  43. * RAND_POOL functions
  44. */
  45. RAND_POOL *rand_pool_new(int entropy_requested, size_t min_len, size_t max_len);
  46. RAND_POOL *rand_pool_attach(const unsigned char *buffer, size_t len,
  47. size_t entropy);
  48. void rand_pool_free(RAND_POOL *pool);
  49. const unsigned char *rand_pool_buffer(RAND_POOL *pool);
  50. unsigned char *rand_pool_detach(RAND_POOL *pool);
  51. size_t rand_pool_entropy(RAND_POOL *pool);
  52. size_t rand_pool_length(RAND_POOL *pool);
  53. size_t rand_pool_entropy_available(RAND_POOL *pool);
  54. size_t rand_pool_entropy_needed(RAND_POOL *pool);
  55. /* |entropy_factor| expresses how many bits of data contain 1 bit of entropy */
  56. size_t rand_pool_bytes_needed(RAND_POOL *pool, unsigned int entropy_factor);
  57. size_t rand_pool_bytes_remaining(RAND_POOL *pool);
  58. int rand_pool_add(RAND_POOL *pool,
  59. const unsigned char *buffer, size_t len, size_t entropy);
  60. unsigned char *rand_pool_add_begin(RAND_POOL *pool, size_t len);
  61. int rand_pool_add_end(RAND_POOL *pool, size_t len, size_t entropy);
  62. /*
  63. * Add random bytes to the pool to acquire requested amount of entropy
  64. *
  65. * This function is platform specific and tries to acquire the requested
  66. * amount of entropy by polling platform specific entropy sources.
  67. *
  68. * If the function succeeds in acquiring at least |entropy_requested| bits
  69. * of entropy, the total entropy count is returned. If it fails, it returns
  70. * an entropy count of 0.
  71. */
  72. size_t rand_pool_acquire_entropy(RAND_POOL *pool);
  73. /*
  74. * Add some application specific nonce data
  75. *
  76. * This function is platform specific and adds some application specific
  77. * data to the nonce used for instantiating the drbg.
  78. *
  79. * This data currently consists of the process and thread id, and a high
  80. * resolution timestamp. The data does not include an atomic counter,
  81. * because that is added by the calling function rand_drbg_get_nonce().
  82. *
  83. * Returns 1 on success and 0 on failure.
  84. */
  85. int rand_pool_add_nonce_data(RAND_POOL *pool);
  86. /*
  87. * Add some platform specific additional data
  88. *
  89. * This function is platform specific and adds some random noise to the
  90. * additional data used for generating random bytes and for reseeding
  91. * the drbg.
  92. *
  93. * Returns 1 on success and 0 on failure.
  94. */
  95. int rand_pool_add_additional_data(RAND_POOL *pool);
  96. /*
  97. * Initialise the random pool reseeding sources.
  98. *
  99. * Returns 1 on success and 0 on failure.
  100. */
  101. int rand_pool_init(void);
  102. /*
  103. * Finalise the random pool reseeding sources.
  104. */
  105. void rand_pool_cleanup(void);
  106. /*
  107. * Control the random pool use of open file descriptors.
  108. */
  109. void rand_pool_keep_random_devices_open(int keep);
  110. #endif