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_nolock.h 6.6KB

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