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.h 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #ifndef OTTERY_H_HEADER_INCLUDED_
  12. #define OTTERY_H_HEADER_INCLUDED_
  13. #include <stdint.h>
  14. #include <sys/types.h>
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #include "ottery_common.h"
  19. /** @file */
  20. struct ottery_config;
  21. /* Functions that use an implicit global state */
  22. /**
  23. * Fill a buffer with random bytes.
  24. *
  25. * @param buf The buffer to fill.
  26. * @param n The number of bytes to write.
  27. */
  28. void ottery_rand_bytes(void *buf, size_t n);
  29. /**
  30. * Generate a random number of type unsigned.
  31. *
  32. * @return A random number between 0 and UINT_MAX included,
  33. * chosen uniformly.
  34. */
  35. unsigned ottery_rand_unsigned(void);
  36. /**
  37. * Generate a random number of type uint32_t.
  38. *
  39. * @return A random number between 0 and UINT32_MAX included,
  40. * chosen uniformly.
  41. */
  42. uint32_t ottery_rand_uint32(void);
  43. /**
  44. * Generate a random number of type uint64_t.
  45. *
  46. * @return A random number between 0 and UINT64_MAX included,
  47. * chosen uniformly.
  48. */
  49. uint64_t ottery_rand_uint64(void);
  50. /**
  51. * Generate a random number of type unsigned in a given range.
  52. *
  53. * @param top The upper bound of the range (inclusive).
  54. * @return A random number no larger than top, and no less than 0,
  55. * chosen uniformly.
  56. */
  57. unsigned ottery_rand_range(unsigned top);
  58. /**
  59. * Generate a random number of type uint64_t in a given range.
  60. *
  61. * @param top The upper bound of the range (inclusive).
  62. * @return A random number no larger than top, and no less than 0,
  63. * chosen uniformly.
  64. */
  65. uint64_t ottery_rand_range64(uint64_t top);
  66. /**
  67. * Initialize the libottery global state.
  68. *
  69. * Most users should not need to use this function. If you use it, you must
  70. * call it before any of: ottery_rand_bytes, ottery_rand_unsigned,
  71. * ottery_rand_uint64, ottery_rand_range, ottery_rand_uint64_range,
  72. * ottery_add_seed, ottery_wipe, ottery_stir.
  73. *
  74. * You would want to use this function if you want to select some non-default
  75. * behavior using an ottery_config structure.
  76. *
  77. * @param cfg Either NULL, or an ottery_config structure that has been
  78. * initialized with ottery_config_init().
  79. * @return Zero on success, or one of the OTTERY_ERR_* error codes on failure.
  80. */
  81. int ottery_init(const struct ottery_config *cfg);
  82. /**
  83. * Add more entropy to the libottery global state.
  84. *
  85. * Calling this function should be needless, if you trust your operating
  86. * system's random number generator and entropy extraction features. You
  87. * would want to use this function if you think the operating system's random
  88. * number generator might be inadequate, and you want to add more entropy from
  89. * EGD or something.
  90. *
  91. * You might also want to call this function if your belief system says that
  92. * it's useful to periodically add more raw entropy to a well-seeded
  93. * cryptographically strong PRNG.
  94. *
  95. * @param seed Bytes to add to the state. If this value is NULL, we take
  96. * more random bytes from the OS.
  97. * @param n The number of bytes to add. If this value is 0, we take more
  98. * random bytes from the OS, regardless of the value of seed.
  99. * @return Zero on success, or one of the OTTERY_ERR_* error codes on failure.
  100. */
  101. int ottery_add_seed(const uint8_t *seed, size_t n);
  102. /**
  103. * Destroy the libottery global state and release any resources that it might
  104. * hold.
  105. *
  106. * Ordinarily, you would only want to call this at exit, if at all.
  107. */
  108. void ottery_wipe(void);
  109. /**
  110. * Explicitly tell libottery to prevent backtracking attacks. (Usually
  111. * needless.)
  112. *
  113. * Once this function has been called, an attacker who compromises the state
  114. * later on will not be able to recover bytes that have previously been
  115. * returned by any of the ottery_rand_* functions.
  116. *
  117. * You should not usually need to call this function: Libottery provides
  118. * backtracking resistance by default, so unless you have manually recompiled
  119. * with the OTTERY_NO_CLEAR_AFTER_YIELD option, this function isn't
  120. * necessary and has no effect. Even *with* OTTERY_NO_CLEAR_AFTER_YIELD,
  121. * this function isn't necessary in ordinary operation: the libottery state is
  122. * implicitly "stirred" every 1k or so.
  123. */
  124. void ottery_prevent_backtracking(void);
  125. #ifdef __cplusplus
  126. }
  127. #endif
  128. #endif