You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ottery_global.c 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* Libottery by Nick Mathewson.
  2. This software has been dedicated to the public domain under the CC0
  3. public domain dedication.
  4. To the extent possible under law, the person who associated CC0 with
  5. libottery has waived all copyright and related or neighboring rights
  6. to libottery.
  7. You should have received a copy of the CC0 legalcode along with this
  8. work in doc/cc0.txt. If not, see
  9. <http://creativecommons.org/publicdomain/zero/1.0/>.
  10. */
  11. #define OTTERY_INTERNAL
  12. #include <stdlib.h>
  13. #include "ottery-internal.h"
  14. #include "ottery.h"
  15. #include "ottery_st.h"
  16. /** Flag: true iff ottery_global_state_ is initialized. */
  17. static int ottery_global_state_initialized_ = 0;
  18. int ottery_valgrind_ = 0;
  19. /** A global state to use for the ottery_* functions that don't take a
  20. * state. */
  21. static struct ottery_state ottery_global_state_;
  22. /** Initialize ottery_global_state_ if it has not been initialize. */
  23. #define CHECK_INIT(rv) do { \
  24. if (UNLIKELY(!ottery_global_state_initialized_)) { \
  25. int err; \
  26. if ((err = ottery_init(NULL))) { \
  27. ottery_fatal_error_(OTTERY_ERR_FLAG_GLOBAL_PRNG_INIT|err); \
  28. return rv; \
  29. } \
  30. } \
  31. } while (0)
  32. int
  33. ottery_init(const struct ottery_config *cfg)
  34. {
  35. if (getenv("VALGRIND")) {
  36. ottery_valgrind_ = 1;
  37. }
  38. int n = ottery_st_init(&ottery_global_state_, cfg);
  39. if (n == 0)
  40. ottery_global_state_initialized_ = 1;
  41. return n;
  42. }
  43. int
  44. ottery_add_seed(const uint8_t *seed, size_t n)
  45. {
  46. CHECK_INIT(0);
  47. return ottery_st_add_seed(&ottery_global_state_, seed, n);
  48. }
  49. void
  50. ottery_wipe(void)
  51. {
  52. if (ottery_global_state_initialized_) {
  53. ottery_global_state_initialized_ = 0;
  54. ottery_st_wipe(&ottery_global_state_);
  55. }
  56. }
  57. void
  58. ottery_prevent_backtracking(void)
  59. {
  60. CHECK_INIT();
  61. ottery_st_prevent_backtracking(&ottery_global_state_);
  62. }
  63. void
  64. ottery_rand_bytes(void *out, size_t n)
  65. {
  66. CHECK_INIT();
  67. ottery_st_rand_bytes(&ottery_global_state_, out, n);
  68. }
  69. unsigned
  70. ottery_rand_unsigned(void)
  71. {
  72. CHECK_INIT(0);
  73. return ottery_st_rand_unsigned(&ottery_global_state_);
  74. }
  75. uint32_t
  76. ottery_rand_uint32(void)
  77. {
  78. CHECK_INIT(0);
  79. return ottery_st_rand_uint32(&ottery_global_state_);
  80. }
  81. uint64_t
  82. ottery_rand_uint64(void)
  83. {
  84. CHECK_INIT(0);
  85. return ottery_st_rand_uint64(&ottery_global_state_);
  86. }
  87. unsigned
  88. ottery_rand_range(unsigned top)
  89. {
  90. CHECK_INIT(0);
  91. return ottery_st_rand_range(&ottery_global_state_, top);
  92. }
  93. uint64_t
  94. ottery_rand_range64(uint64_t top)
  95. {
  96. CHECK_INIT(0);
  97. return ottery_st_rand_range64(&ottery_global_state_, top);
  98. }
  99. const char *ottery_get_impl_name(void)
  100. {
  101. CHECK_INIT(0);
  102. return ottery_global_state_.prf.name;
  103. }