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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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_INTERNAL_H_HEADER_INCLUDED_
  12. #define OTTERY_INTERNAL_H_HEADER_INCLUDED_
  13. #include <stdint.h>
  14. #include <sys/types.h>
  15. #include "config.h"
  16. #include "ottery-threading.h"
  17. /**
  18. * Version number for Libottery. The first three bytes are the major number,
  19. * minor number, and patch-level respectively. The final byte is 0 for a
  20. * released version, and nonzero otherwise.
  21. */
  22. #define OTTERY_VERSION 0x00000001
  23. /**
  24. * Human-readable string representing the Libottery version.
  25. */
  26. #define OTTERY_VERSION_STRING "0.0.0"
  27. /** Largest possible state_bytes value. */
  28. #define MAX_STATE_BYTES 64
  29. /** Largest possible state_len value. */
  30. #define MAX_STATE_LEN 256
  31. /** Largest possible output_len value. */
  32. #define MAX_OUTPUT_LEN 1024
  33. /**
  34. * @brief Flags for external entropy sources.
  35. *
  36. * @{ */
  37. /** An RNG that probably provides strong entropy. */
  38. #define OTTERY_ENTROPY_FL_STRONG 0x000001
  39. /** An RNG that runs very quickly. */
  40. #define OTTERY_ENTROPY_FL_FAST 0x000002
  41. /** @} */
  42. /**
  43. * @brief Identifying external entropy domains.
  44. */
  45. /** An RNG provided by the operating system. */
  46. #define OTTERY_ENTROPY_DOM_OS 0x000100
  47. /** An RNG provided by the CPU. */
  48. #define OTTERY_ENTROPY_DOM_CPU 0x000200
  49. /** An EGD-style entropy source */
  50. #define OTTERY_ENTROPY_DOM_EGD 0x000400
  51. /** @} */
  52. #define OTTERY_ENTROPY_FLAG_MASK 0x000000ff
  53. #define OTTERY_ENTROPY_DOM_MASK 0x0000ff00
  54. #define OTTERY_ENTROPY_ALL_SOURCES 0x0fff0000
  55. struct sockaddr;
  56. /** Configuration for the strong RNG the we use for entropy. */
  57. struct ottery_entropy_config {
  58. /** The filename to use as /dev/urandom. Ignored if this
  59. * is not a unix-like operating system. If this is NULL, we use
  60. * the default value. */
  61. const char *urandom_fname;
  62. /** An fd to use to access /dev/urandom. -1 if not set. Overrides
  63. * urandom_fname. */
  64. int urandom_fd;
  65. /** True if urandom_fd has been set. */
  66. unsigned urandom_fd_is_set;
  67. /** Socket for egd */
  68. const struct sockaddr *egd_sockaddr;
  69. /** Socklen for egd_sockaddr. */
  70. int egd_socklen;
  71. /** Bitmask of sources to disable. */
  72. uint32_t disabled_sources;
  73. /** Bitmask of sources to consider weak. */
  74. uint32_t weak_sources;
  75. /** If true, we don't enforce that urandom_fname must be a device file.
  76. * This is for testing, and is not exposed to user code.
  77. */
  78. unsigned allow_nondev_urandom;
  79. };
  80. struct ottery_entropy_state {
  81. /* Cached value for the inode of the urandom device. If this value changes,
  82. * we assume that somebody messed with the fd by accident. */
  83. uint64_t urandom_fd_inode;
  84. };
  85. /**
  86. * Return the buffer size to allocate when getting at least n bytes from each
  87. * entropy source. We might not actually need so many. */
  88. size_t ottery_get_entropy_bufsize_(size_t n);
  89. /**
  90. * Interface to underlying strong RNGs. If this were fast, we'd just use it
  91. * for everything, and forget about having a userspace PRNG. Unfortunately,
  92. * it typically isn't.
  93. *
  94. * @param config A correctly set-up ottery_entropy_config.
  95. * @param state A correctly set-up ottery_entropy_state.
  96. * @param require_flags Only run entropy sources with *all* of these
  97. * OTTERY_ENTROPY_* flags set. Set this to 0 to use all the sources
  98. * that work.
  99. * @param bytes A buffer to receive random bytes.
  100. * @param n The number of bytes to try to get from each entropy source.
  101. * @param bufsize The number of bytes available in the buffer; modified
  102. * to hold the number of bytes actually written.
  103. * @param flags_out Set to a bitwise OR of all of the OTTERY_ENTROPY_* flags
  104. * for sources in the result.
  105. * @return Zero on success, or an error code on failure. On failure, it is not
  106. * safe to treat the contents of the buffer as random at all.
  107. */
  108. int ottery_get_entropy_(const struct ottery_entropy_config *config,
  109. struct ottery_entropy_state *state,
  110. uint32_t require_flags,
  111. uint8_t *bytes, size_t n, size_t *bufsize,
  112. uint32_t *flags_out);
  113. /**
  114. * Clear all bytes stored in a structure. Unlike memset, the compiler is not
  115. * going to optimize this out of existence because the target is about to go
  116. * out of scope.
  117. *
  118. * @param mem Pointer to the memory to erase.
  119. * @param len The number of bytes to erase.
  120. */
  121. void ottery_memclear_(void *mem, size_t len);
  122. /**
  123. * Information on a single pseudorandom function that we can use to generate
  124. * a bytestream which (we hope) an observer can't distinguish from random
  125. * bytes.
  126. *
  127. * Broadly speaking, every ottery_prf has an underlying function from an
  128. * (state_bytes)-byte state and a 4 byte counter to an output_len-byte
  129. * output block.
  130. **/
  131. struct ottery_prf {
  132. /** The name of this algorithm. */
  133. const char *name;
  134. /** The name of the implementation of this algorithm*/
  135. const char *impl;
  136. /** The name of the flavor of the implementation of this algorithm*/
  137. const char *flav;
  138. /** The length of the object that's used to hold the state (keys, nonces,
  139. * subkeys as needed, etc) for this PRF. This can be longer than
  140. * state_bytes because of key expansion or structure padding. It must be
  141. * no greater than MAX_STATE_LEN. */
  142. unsigned state_len;
  143. /** The number of bytes used to generate a state object. It must be no
  144. * greater than MAX_STATE_BYTES. It must be no grater than output_len. */
  145. unsigned state_bytes;
  146. /** The number of bytes generated by a single call to the generate
  147. * function. It must be no larger than MAX_OUTPUT_LEN.
  148. */
  149. unsigned output_len;
  150. /** Bitmask of CPU flags required to run this PRF. */
  151. uint32_t required_cpucap;
  152. /** Pointer to a function to intialize a state structure for the PRF.
  153. *
  154. * @param state An object of size at least (state_len) that will
  155. * hold the state and any derived values. It must be aligned to
  156. * a 16-byte boundary.
  157. * @param bytes An array of (state_bytes) random bytes.
  158. */
  159. void (*setup)(void *state, const uint8_t *bytes);
  160. /** Pointer to a function that calculates the PRF.
  161. *
  162. * @param state A state object previously initialized by the setup
  163. * function.
  164. * @param output An array of (output_len) bytes in which to store the
  165. * result of the function
  166. * @param idx A counter value for the function.
  167. */
  168. void (*generate)(void *state, uint8_t *output, uint32_t idx);
  169. };
  170. #ifdef OTTERY_INTERNAL
  171. struct ottery_config {
  172. /** The PRF that we should use. If NULL, we use the default. */
  173. const struct ottery_prf *impl;
  174. /** Configuration for how we will set up our entropy sources. */
  175. struct ottery_entropy_config entropy_config;
  176. };
  177. #define ottery_state_nolock ottery_state
  178. struct __attribute__((aligned(16))) ottery_state {
  179. /**
  180. * Holds up to prf.output_len bytes that have been generated by the
  181. * pseudorandom function. */
  182. __attribute__ ((aligned (16))) uint8_t buffer[MAX_OUTPUT_LEN];
  183. /**
  184. * Holds the state information (typically nonces and keys) used by the
  185. * pseudorandom function. */
  186. __attribute__ ((aligned (16))) uint8_t state[MAX_STATE_LEN];
  187. /**
  188. * Parameters and function pointers for the cryptographic pseudorandom
  189. * function that we're using. */
  190. struct ottery_prf prf;
  191. /**
  192. * Index of the *next* block counter to use when generating random bytes
  193. * with prf. When this equals or exceeds prf.stir_after, we should stir
  194. * the PRNG. */
  195. uint32_t block_counter;
  196. /**
  197. * Magic number; used to tell whether this state is initialized.
  198. */
  199. uint32_t magic;
  200. /**
  201. * Index of the next byte in (buffer) to yield to the user.
  202. *
  203. * Invariant: this is less than prf.output_len. */
  204. uint16_t pos;
  205. /**
  206. * The pid of the process in which this PRF was most recently seeded
  207. * from the OS. We use this to avoid use-after-fork problems; see
  208. * ottery_st_rand_lock_and_check(). */
  209. pid_t pid;
  210. /**
  211. * Combined flags_out results from all calls to the entropy source that
  212. * have influenced our current state.
  213. */
  214. uint32_t entropy_src_flags;
  215. /**
  216. * flags_out result from our last call to the entropy source.
  217. */
  218. uint32_t last_entropy_flags;
  219. /**
  220. * Configuration for the entropy source.
  221. */
  222. struct ottery_entropy_config entropy_config;
  223. /** State for the entropy source.
  224. */
  225. struct ottery_entropy_state entropy_state;
  226. /**
  227. * @brief Locks for this structure.
  228. *
  229. * This lock will not necessarily be recursive. It's probably a
  230. * spinlock.
  231. *
  232. * @{
  233. */
  234. DECL_LOCK(mutex)
  235. /**@}*/
  236. };
  237. #endif
  238. struct ottery_config;
  239. /**
  240. * For testing: manually supply a PRF.
  241. */
  242. void ottery_config_set_manual_prf_(struct ottery_config *cfg,
  243. const struct ottery_prf *prf);
  244. /** Called when a fatal error has occurred: Die horribly, or invoke
  245. * ottery_fatal_handler. */
  246. void ottery_fatal_error_(int error);
  247. #define OTTERY_CPUCAP_SIMD (1<<0)
  248. #define OTTERY_CPUCAP_SSSE3 (1<<1)
  249. #define OTTERY_CPUCAP_AES (1<<2)
  250. #define OTTERY_CPUCAP_RAND (1<<3)
  251. /** Return a mask of OTTERY_CPUCAP_* for what the CPU will offer us. */
  252. uint32_t ottery_get_cpu_capabilities_(void);
  253. /** Tell ottery_get_cpu_capabilities to never report certain capabilities as
  254. * present. */
  255. void ottery_disable_cpu_capabilities_(uint32_t disable);
  256. /**
  257. * @brief pure-C portable ChaCha implementations.
  258. *
  259. * @{
  260. */
  261. extern const struct ottery_prf ottery_prf_chacha8_merged_;
  262. extern const struct ottery_prf ottery_prf_chacha12_merged_;
  263. extern const struct ottery_prf ottery_prf_chacha20_merged_;
  264. /**@}*/
  265. /**
  266. * @brief SIMD-basd ChaCha implementations.
  267. *
  268. * These are much, much faster.
  269. *
  270. * @{ */
  271. #ifdef HAVE_SIMD_CHACHA
  272. extern const struct ottery_prf ottery_prf_chacha8_krovetz_1_;
  273. extern const struct ottery_prf ottery_prf_chacha12_krovetz_1_;
  274. extern const struct ottery_prf ottery_prf_chacha20_krovetz_1_;
  275. #endif
  276. #ifdef HAVE_SIMD_CHACHA_2
  277. extern const struct ottery_prf ottery_prf_chacha8_krovetz_2_;
  278. extern const struct ottery_prf ottery_prf_chacha12_krovetz_2_;
  279. extern const struct ottery_prf ottery_prf_chacha20_krovetz_2_;
  280. #endif
  281. /** @} */
  282. #endif