Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

126 рядки
4.5 KiB

  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_set_urandom_fd causes the module to use a copy of |fd| for system
  29. // randomness rather opening /dev/urandom internally. The caller retains
  30. // ownership of |fd| and is at liberty to close it at any time. This is useful
  31. // if, due to a sandbox, /dev/urandom isn't available. If used, it must be
  32. // called before the first call to |RAND_bytes|, and it is mutually exclusive
  33. // with |RAND_enable_fork_unsafe_buffering|.
  34. //
  35. // |RAND_set_urandom_fd| does not buffer any entropy, so it is safe to call
  36. // |fork| at any time after calling |RAND_set_urandom_fd|.
  37. OPENSSL_EXPORT void RAND_set_urandom_fd(int fd);
  38. // RAND_enable_fork_unsafe_buffering enables efficient buffered reading of
  39. // /dev/urandom. It adds an overhead of a few KB of memory per thread. It must
  40. // be called before the first call to |RAND_bytes| and it is mutually exclusive
  41. // with calls to |RAND_set_urandom_fd|.
  42. //
  43. // If |fd| is non-negative then a copy of |fd| will be used rather than opening
  44. // /dev/urandom internally. Like |RAND_set_urandom_fd|, the caller retains
  45. // ownership of |fd|. If |fd| is negative then /dev/urandom will be opened and
  46. // any error from open(2) crashes the address space.
  47. //
  48. // It has an unusual name because the buffer is unsafe across calls to |fork|.
  49. // Hence, this function should never be called by libraries.
  50. OPENSSL_EXPORT void RAND_enable_fork_unsafe_buffering(int fd);
  51. #endif
  52. #if defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE)
  53. // RAND_reset_for_fuzzing resets the fuzzer-only deterministic RNG. This
  54. // function is only defined in the fuzzer-only build configuration.
  55. OPENSSL_EXPORT void RAND_reset_for_fuzzing(void);
  56. #endif
  57. // Deprecated functions
  58. // RAND_pseudo_bytes is a wrapper around |RAND_bytes|.
  59. OPENSSL_EXPORT int RAND_pseudo_bytes(uint8_t *buf, size_t len);
  60. // RAND_seed reads a single byte of random data to ensure that any file
  61. // descriptors etc are opened.
  62. OPENSSL_EXPORT void RAND_seed(const void *buf, int num);
  63. // RAND_load_file returns a nonnegative number.
  64. OPENSSL_EXPORT int RAND_load_file(const char *path, long num);
  65. // RAND_file_name returns NULL.
  66. OPENSSL_EXPORT const char *RAND_file_name(char *buf, size_t num);
  67. // RAND_add does nothing.
  68. OPENSSL_EXPORT void RAND_add(const void *buf, int num, double entropy);
  69. // RAND_egd returns 255.
  70. OPENSSL_EXPORT int RAND_egd(const char *);
  71. // RAND_poll returns one.
  72. OPENSSL_EXPORT int RAND_poll(void);
  73. // RAND_status returns one.
  74. OPENSSL_EXPORT int RAND_status(void);
  75. // rand_meth_st is typedefed to |RAND_METHOD| in base.h. It isn't used; it
  76. // exists only to be the return type of |RAND_SSLeay|. It's
  77. // external so that variables of this type can be initialized.
  78. struct rand_meth_st {
  79. void (*seed) (const void *buf, int num);
  80. int (*bytes) (uint8_t *buf, size_t num);
  81. void (*cleanup) (void);
  82. void (*add) (const void *buf, int num, double entropy);
  83. int (*pseudorand) (uint8_t *buf, size_t num);
  84. int (*status) (void);
  85. };
  86. // RAND_SSLeay returns a pointer to a dummy |RAND_METHOD|.
  87. OPENSSL_EXPORT RAND_METHOD *RAND_SSLeay(void);
  88. // RAND_get_rand_method returns |RAND_SSLeay()|.
  89. OPENSSL_EXPORT const RAND_METHOD *RAND_get_rand_method(void);
  90. // RAND_set_rand_method does nothing.
  91. OPENSSL_EXPORT void RAND_set_rand_method(const RAND_METHOD *);
  92. #if defined(__cplusplus)
  93. } // extern C
  94. #endif
  95. #endif // OPENSSL_HEADER_RAND_H