rand.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* Copyright (c) 2014, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #ifndef OPENSSL_HEADER_RAND_H
  15. #define OPENSSL_HEADER_RAND_H
  16. #include <openssl/base.h>
  17. #if defined(__cplusplus)
  18. extern "C" {
  19. #endif
  20. // Random number generation.
  21. // RAND_bytes writes |len| bytes of random data to |buf| and returns one.
  22. OPENSSL_EXPORT int RAND_bytes(uint8_t *buf, size_t len);
  23. // RAND_cleanup frees any resources used by the RNG. This is not safe if other
  24. // threads might still be calling |RAND_bytes|.
  25. OPENSSL_EXPORT void RAND_cleanup(void);
  26. // Obscure functions.
  27. #if !defined(OPENSSL_WINDOWS)
  28. // RAND_enable_fork_unsafe_buffering enables efficient buffered reading of
  29. // /dev/urandom. It adds an overhead of a few KB of memory per thread. It must
  30. // be called before the first call to |RAND_bytes|.
  31. //
  32. // |fd| must be -1. We no longer support setting the file descriptor with this
  33. // function.
  34. //
  35. // It has an unusual name because the buffer is unsafe across calls to |fork|.
  36. // Hence, this function should never be called by libraries.
  37. OPENSSL_EXPORT void RAND_enable_fork_unsafe_buffering(int fd);
  38. #endif
  39. #if defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE)
  40. // RAND_reset_for_fuzzing resets the fuzzer-only deterministic RNG. This
  41. // function is only defined in the fuzzer-only build configuration.
  42. OPENSSL_EXPORT void RAND_reset_for_fuzzing(void);
  43. #endif
  44. // Deprecated functions
  45. // RAND_pseudo_bytes is a wrapper around |RAND_bytes|.
  46. OPENSSL_EXPORT int RAND_pseudo_bytes(uint8_t *buf, size_t len);
  47. // RAND_seed reads a single byte of random data to ensure that any file
  48. // descriptors etc are opened.
  49. OPENSSL_EXPORT void RAND_seed(const void *buf, int num);
  50. // RAND_load_file returns a nonnegative number.
  51. OPENSSL_EXPORT int RAND_load_file(const char *path, long num);
  52. // RAND_file_name returns NULL.
  53. OPENSSL_EXPORT const char *RAND_file_name(char *buf, size_t num);
  54. // RAND_add does nothing.
  55. OPENSSL_EXPORT void RAND_add(const void *buf, int num, double entropy);
  56. // RAND_egd returns 255.
  57. OPENSSL_EXPORT int RAND_egd(const char *);
  58. // RAND_poll returns one.
  59. OPENSSL_EXPORT int RAND_poll(void);
  60. // RAND_status returns one.
  61. OPENSSL_EXPORT int RAND_status(void);
  62. // rand_meth_st is typedefed to |RAND_METHOD| in base.h. It isn't used; it
  63. // exists only to be the return type of |RAND_SSLeay|. It's
  64. // external so that variables of this type can be initialized.
  65. struct rand_meth_st {
  66. void (*seed) (const void *buf, int num);
  67. int (*bytes) (uint8_t *buf, size_t num);
  68. void (*cleanup) (void);
  69. void (*add) (const void *buf, int num, double entropy);
  70. int (*pseudorand) (uint8_t *buf, size_t num);
  71. int (*status) (void);
  72. };
  73. // RAND_SSLeay returns a pointer to a dummy |RAND_METHOD|.
  74. OPENSSL_EXPORT RAND_METHOD *RAND_SSLeay(void);
  75. // RAND_OpenSSL returns a pointer to a dummy |RAND_METHOD|.
  76. OPENSSL_EXPORT RAND_METHOD *RAND_OpenSSL(void);
  77. // RAND_get_rand_method returns |RAND_SSLeay()|.
  78. OPENSSL_EXPORT const RAND_METHOD *RAND_get_rand_method(void);
  79. // RAND_set_rand_method returns one.
  80. OPENSSL_EXPORT int RAND_set_rand_method(const RAND_METHOD *);
  81. #if defined(__cplusplus)
  82. } // extern C
  83. #endif
  84. #endif // OPENSSL_HEADER_RAND_H