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_st.h 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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_ST_H_HEADER_INCLUDED_
  12. #define OTTERY_ST_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. struct ottery_state;
  22. /** Size reserved for struct ottery_state */
  23. #define OTTERY_STATE_DUMMY_SIZE_ 1536
  24. #ifndef OTTERY_INTERNAL
  25. /**
  26. * The state for a libottery PRNG.
  27. *
  28. * An ottery_state structure is constucted with ottery_st_init(). It MUST be
  29. * aligned on a 16-byte boundary.
  30. *
  31. * You may not use an ottery_state structure with any other function before
  32. * you have first initialized it with ottery_st_init().
  33. *
  34. * The contents of this structure are opaque; The definition here is
  35. * defined to be large enough so that programs that allocate it will get
  36. * more than enough room.
  37. */
  38. struct __attribute__((aligned(16))) ottery_state {
  39. /** Nothing to see here */
  40. uint8_t dummy_[OTTERY_STATE_DUMMY_SIZE_];
  41. };
  42. #endif
  43. /**
  44. * Get the minimal size for allocating an ottery_state.
  45. *
  46. * sizeof(ottery_state) will give an overestimate to allow binary
  47. * compatibility with future versions of libottery. Use this function instead
  48. * to get the minimal number of bytes to allocate.
  49. *
  50. * @return The minimal number of bytes to use when allocating an
  51. * ottery_state structure.
  52. */
  53. size_t ottery_get_sizeof_state(void);
  54. /**
  55. * Initialize an ottery_state structure.
  56. *
  57. * You must call this function on any ottery_state structure before
  58. * calling any other functions on it.
  59. *
  60. * @param st The ottery_state to initialize.
  61. * @param cfg Either NULL, or an ottery_config structure that has been
  62. * initialized with ottery_config_init().
  63. * @return Zero on success, or one of the OTTERY_ERR_* error codes on failure.
  64. */
  65. int ottery_st_init(struct ottery_state *st, const struct ottery_config *cfg);
  66. /**
  67. * Add more entropy to an ottery_state structure.
  68. *
  69. * Calling this function should be needless, if you trust your operating
  70. * system's random number generator and entropy extraction features. You
  71. * would want to use this function if you think the operating system's random
  72. * number generator might be inadequate, and you want to add more entropy from
  73. * EGD or something.
  74. *
  75. * You might also want to call this function if your belief system says that
  76. * it's useful to periodically add more raw entropy to a well-seeded
  77. * cryptographically strong PRNG.
  78. *
  79. * @param st The state which will receive more entropy.
  80. * @param seed Bytes to add to the state.
  81. * @param n The number of bytes to add.
  82. * @return Zero on success, or one of the OTTERY_ERR_* error codes on failure.
  83. */
  84. int ottery_st_add_seed(struct ottery_state *st, const uint8_t *seed, size_t n);
  85. /**
  86. * Destroy an ottery_state structure and release any resources that it might
  87. * hold.
  88. *
  89. * Ordinarily, you would want to call this at exit, or before freeing an
  90. * ottery_state
  91. *
  92. * @param st The state to wipe.
  93. */
  94. void ottery_st_wipe(struct ottery_state *st);
  95. /**
  96. * Explicitly prevent backtracking attacks. (Usually needless).
  97. *
  98. * Once this function has been called, an attacker who compromises the state
  99. * later on will not be able to recover bytes that have previously been
  100. * returned by any of the ottery_st_rand_* functions.
  101. *
  102. * You should not usually need to call this function: Libottery provides
  103. * backtracking resistance by default, so unless you have manually recompiled
  104. * with the OTTERY_NO_CLEAR_AFTER_YIELD option, this function isn't
  105. * necessary and has no effect. Even *with* OTTERY_NO_CLEAR_AFTER_YIELD,
  106. * this function isn't necessary in ordinary operation: the libottery state is
  107. * implicitly "stirred" every 1k or so.
  108. *
  109. * @param st The state to stir.
  110. */
  111. void ottery_st_prevent_backtracking(struct ottery_state *st);
  112. /**
  113. * Use an ottery_state structure to fill a buffer with random bytes.
  114. *
  115. * @param st The state structure to use.
  116. * @param buf The buffer to fill.
  117. * @param n The number of bytes to write.
  118. */
  119. void ottery_st_rand_bytes(struct ottery_state *st, void *buf, size_t n);
  120. /**
  121. * Use an ottery_state structure to generate a random number of type unsigned.
  122. *
  123. * @param st The state structure to use.
  124. * @return A random number between 0 and UINT_MAX included,
  125. * chosen uniformly.
  126. */
  127. unsigned ottery_st_rand_unsigned(struct ottery_state *st);
  128. /**
  129. * Use an ottery_state structure to generate a random number of type uint32_t.
  130. *
  131. * @param st The state structure to use.
  132. * @return A random number between 0 and UINT32_MAX included,
  133. * chosen uniformly.
  134. */
  135. uint32_t ottery_st_rand_uint32(struct ottery_state *st);
  136. /**
  137. * Use an ottery_state structure to generate a random number of type uint64_t.
  138. *
  139. * @param st The state structure to use.
  140. * @return A random number between 0 and UINT64_MAX included,
  141. * chosen uniformly.
  142. */
  143. uint64_t ottery_st_rand_uint64(struct ottery_state *st);
  144. /**
  145. * Use an ottery_state structure to generate a random number of type unsigned
  146. * in a given range.
  147. *
  148. * @param st The state structure to use.
  149. * @param top The upper bound of the range (inclusive).
  150. * @return A random number no larger than top, and no less than 0,
  151. * chosen uniformly.
  152. */
  153. unsigned ottery_st_rand_range(struct ottery_state *st, unsigned top);
  154. /**
  155. * Use an ottery_state structure to generate a random number of type uint64_t
  156. * in a given range.
  157. *
  158. * @param st The state structure to use.
  159. * @param top The upper bound of the range (inclusive).
  160. * @return A random number no larger than top, and no less than 0,
  161. * chosen uniformly.
  162. */
  163. uint64_t ottery_st_rand_range64(struct ottery_state *st, uint64_t top);
  164. #ifdef __cplusplus
  165. }
  166. #endif
  167. #endif