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_common.h 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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_COMMON_H_HEADER_INCLUDED_
  12. #define OTTERY_COMMON_H_HEADER_INCLUDED_
  13. #include <stdint.h>
  14. #include <sys/types.h>
  15. /** @file */
  16. struct ottery_config;
  17. /* Error codes */
  18. /**
  19. * @name libottery error codes and flags
  20. *
  21. * @{
  22. */
  23. /** No error has occurred. */
  24. #define OTTERY_ERR_NONE 0x0000
  25. /** We failed to allocate or initialize a lock. */
  26. #define OTTERY_ERR_LOCK_INIT 0x0001
  27. /** An internal error occurrred. This is probably a programming mistake
  28. * in libottery. */
  29. #define OTTERY_ERR_INTERNAL 0x0002
  30. /** We were unable to connect to the operating system's strong RNG. */
  31. #define OTTERY_ERR_INIT_STRONG_RNG 0x0003
  32. /** We were unable to retrieve sufficient random bytes from the
  33. * operating system's strong RNG. */
  34. #define OTTERY_ERR_ACCESS_STRONG_RNG 0x0004
  35. /** At least one argument to the function was invalid. */
  36. #define OTTERY_ERR_INVALID_ARGUMENT 0x0005
  37. /** An ottery_state structure was not aligned to a 16-byte boundary. */
  38. #define OTTERY_ERR_STATE_ALIGNMENT 0x0006
  39. /** FATAL ERROR: An ottery_st function other than ottery_st_init() was
  40. * called on and uninitialized state. */
  41. #define OTTERY_ERR_STATE_INIT 0x1000
  42. /** FLAG; FATAL ERROR: The error occurred while initializing the global
  43. * state during the first call to an ottery_rand_* function. */
  44. #define OTTERY_ERR_FLAG_GLOBAL_PRNG_INIT 0x2000
  45. /** FLAG; FATAL ERROR: The error occurred while reinitializing a state
  46. * after a fork(). (We need to do this, or else both processes would
  47. * generate the same values, which could give dire results.)
  48. */
  49. #define OTTERY_ERR_FLAG_POSTFORK_RESEED 0x4000
  50. /**
  51. * Checks whether an OTTERY_ERR value is a fatal error.
  52. *
  53. * @param err an OTTERY_ERR_* valuer
  54. * @return True if err is fatal; false if it is not fatal.
  55. */
  56. #define OTTERY_ERR_IS_FATAL(err) \
  57. (((err) & ~0xfff) != 0)
  58. /* Functions to interact with the library on a global level */
  59. /**
  60. * Override the behavior of libottery on a fatal error.
  61. *
  62. * By default, libottery will call abort() in a few circumstances, in
  63. * order to keep the program from operating insecurely. If you want,
  64. * you can provide another function to call instead.
  65. *
  66. * If your function does not itself abort() or exit() the process, or throw an
  67. * exception (assuming some C family that has exceptions), libottery will
  68. * continue running insecurely -- it might return predictable random numbers,
  69. * leak secrets, or just return 0 for everything -- so you should really be
  70. * very careful here.
  71. *
  72. * (The alternative to fatal errors would have been having all the
  73. * ottery_rand_* functions able to return an error, and requiring users
  74. * to check those codes. But experience suggests that C programmers
  75. * frequently do not check error codes.)
  76. *
  77. * @param fn A function to call in place of abort(). It will receive as
  78. * its argument one of the OTTERY_ERR_* error codes.
  79. */
  80. void ottery_set_fatal_handler(void (*fn)(int errorcode));
  81. /* Functions to manipulate parameters. */
  82. /**
  83. * @name Names of prfs for use with ottery_config_force_implementation
  84. *
  85. * @{ */
  86. #define OTTERY_PRF_CHACHA "CHACHA"
  87. #define OTTERY_PRF_CHACHA8 "CHACHA8"
  88. #define OTTERY_PRF_CHACHA12 "CHACHA12"
  89. #define OTTERY_PRF_CHACHA20 "CHACHA20"
  90. #define OTTERY_PRF_CHACHA_SIMD "CHACHA-SIMD"
  91. #define OTTERY_PRF_CHACHA8_SIMD "CHACHA8-SIMD"
  92. #define OTTERY_PRF_CHACHA12_SIMD "CHACHA12-SIMD"
  93. #define OTTERY_PRF_CHACHA20_SIMD "CHACHA20-SIMD"
  94. #define OTTERY_PRF_CHACHA_NO_SIMD "CHACHA-NOSIMD"
  95. #define OTTERY_PRF_CHACHA8_NO_SIMD "CHACHA8-NOSIMD"
  96. #define OTTERY_PRF_CHACHA12_NO_SIMD "CHACHA12-NOSIMD"
  97. #define OTTERY_PRF_CHACHA20_NO_SIMD "CHACHA20-NOSIMD"
  98. /** @} */
  99. /**
  100. * Initialize an ottery_config structure.
  101. *
  102. * You must call this function on any ottery_config structure before it
  103. * can be passed to ottery_init() or ottery_st_init().
  104. *
  105. * @param cfg The configuration object to initialize.
  106. * @return Zero on success, or one of the OTTERY_ERR_* error codes on
  107. * failure.
  108. */
  109. int ottery_config_init(struct ottery_config *cfg);
  110. /**
  111. * Try to force the use of a particular pseudorandom function for a given
  112. * libottery instance.
  113. *
  114. * To use this function, you call it on an ottery_config structure after
  115. * ottery_config_init(), and before passing that structure to
  116. * ottery_st_init() or ottery_init().
  117. *
  118. * @param cfg The configuration structure to configure.
  119. * @param impl The name of a pseudorandom function. One of the
  120. * OTTERY_PRF_* values.
  121. * @return Zero on success, or one of the OTTERY_ERR_* error codes on
  122. * failure.
  123. */
  124. int ottery_config_force_implementation(struct ottery_config *cfg,
  125. const char *impl);
  126. /**
  127. * Set a device file to use as a source of strong entropy.
  128. *
  129. * To use this function, you call it on an ottery_config structure after
  130. * ottery_config_init(), and before passing that structure to
  131. * ottery_st_init() or ottery_init().
  132. *
  133. * By default, libottery will try /dev/urandom on Unix-like systems.
  134. *
  135. * @param cfg The configuration structure to configure.
  136. * @param fname The name of the device to use instead of /dev/urandom. This
  137. * pointer is copied around, and must not be freed while any libottery state
  138. * configured using this structure is still in use.
  139. *
  140. */
  141. void ottery_config_set_urandom_device(struct ottery_config *cfg,
  142. const char *fname);
  143. /**
  144. * Set a device file to use as a source of strong entropy from the operating
  145. * system.
  146. *
  147. * To use this function, you call it on an ottery_config structure after
  148. * ottery_config_init(), and before passing that structure to
  149. * ottery_st_init() or ottery_init().
  150. *
  151. * This function overrides the default behavior, and overrides any
  152. * setting in ottery_config_set_urandom_device.
  153. *
  154. * You MUST NOT change the the file descriptor while any libottery PRNG
  155. * configured with it is still running. For example, don't close it, or use
  156. * dup2 to make it refer to a different file, or anything like that.
  157. *
  158. * It is probably a good idea to open the file with the CLOEXEC flag set.
  159. *
  160. * @param cfg The configuration structure to configure.
  161. * @param fd A file descriptor to use as an OS rng source.
  162. */
  163. void ottery_config_set_urandom_fd(struct ottery_config *cfg,
  164. int fd);
  165. struct sockaddr;
  166. /**
  167. * Configure a socket at which to find a local copy of some service
  168. * implementing the EGD (entropy-gathering daemon) protocol.
  169. *
  170. * Unless this function is called, EGD is not used by default.
  171. * To use this function, you call it on an ottery_config structure after
  172. * ottery_config_init(), and before passing that structure to
  173. * ottery_st_init() or ottery_init().
  174. *
  175. * TODO: This is not implemented for Windows yet.
  176. *
  177. * @param cfg The configuration structure to configure.
  178. * @param addr The address of the daemon. Obviously, this should be
  179. * some port on localhost, or a unix socket. This pointer is copied
  180. * around, and must not be freed while any libottery state configured
  181. * using this structure is still in use.
  182. * @param len the length of the address.
  183. *
  184. */
  185. void ottery_config_set_egd_socket(struct ottery_config *cfg,
  186. const struct sockaddr *addr,
  187. int len);
  188. /**
  189. * @brief External entropy sources.
  190. *
  191. * These can be passed as a bitmask to ottery_config_disable_entropy_sources.
  192. *
  193. * @{ */
  194. /** A unix-style /dev/urandom device. */
  195. #define OTTERY_ENTROPY_SRC_RANDOMDEV 0x0010000
  196. /** The Windows CryptGenRandom call. */
  197. #define OTTERY_ENTROPY_SRC_CRYPTGENRANDOM 0x0020000
  198. /** The Intel RDRAND instruction. */
  199. #define OTTERY_ENTROPY_SRC_RDRAND 0x0040000
  200. /** Some local server obeying the EGD protocol. Has no effect unless
  201. * ottery_config_set_egd_socket was called. */
  202. #define OTTERY_ENTROPY_SRC_EGD 0x0080000
  203. /** @} */
  204. /**
  205. * Disable the use of one or more entropy sources.
  206. *
  207. * Note that if enough entropy sources are disabled, the state will
  208. * not be able to get initialized, and libottery might not work.
  209. *
  210. * To use this function, you call it on an ottery_config structure after
  211. * ottery_config_init(), and before passing that structure to
  212. * ottery_st_init() or ottery_init().
  213. *
  214. * @param cfg A configuration in which to disable one or more entropy sources.
  215. * @param disabled_sources a bitwise combination of one or more
  216. * OTTERY_ENTROPY_SRC_* values to disable. This will replace
  217. * any previous bitmask of disabled sources.
  218. *
  219. */
  220. void ottery_config_disable_entropy_sources(struct ottery_config *cfg,
  221. uint32_t disabled_sources);
  222. /**
  223. * Mark one or more entropy sources as "weak".
  224. *
  225. * Unlike a disabled source, we will still try to read entropy from
  226. * a weak source -- but we will fail if _only_ weak sources are available.
  227. *
  228. * Note that if enough entropy sources are disabled and/or weak sources are
  229. * failing, the state will not be able to get initialized, and libottery might
  230. * not work.
  231. *
  232. * To use this function, you call it on an ottery_config structure after
  233. * ottery_config_init(), and before passing that structure to
  234. * ottery_st_init() or ottery_init().
  235. *
  236. * @param cfg A configuration in which to disable one or more entropy sources.
  237. * @param weak_sources a bitwise combination of one or more
  238. * OTTERY_ENTROPY_SRC_* values to mark as weak. This will replace
  239. * any previous bitmask of weak sources.
  240. */
  241. void ottery_config_mark_entropy_sources_weak(struct ottery_config *cfg,
  242. uint32_t weak_source);
  243. /** Size reserved for struct ottery_config */
  244. #define OTTERY_CONFIG_DUMMY_SIZE_ 1024
  245. #ifndef OTTERY_INTERNAL
  246. /**
  247. * A configuration object for setting up a libottery instance.
  248. *
  249. * An ottery_config structure is initialized with ottery_config_init,
  250. * and passed to ottery_init() or ottery_st_init().
  251. *
  252. * The contents of this structure are opaque; The definition here is
  253. * defined to be large enough so that programs that allocate it will get
  254. * more than enough room.
  255. */
  256. struct ottery_config {
  257. /** Nothing to see here */
  258. uint8_t dummy_[OTTERY_CONFIG_DUMMY_SIZE_];
  259. };
  260. #endif
  261. /**
  262. * Get the minimal size for allocating an ottery_config.
  263. *
  264. * sizeof(ottery_config) will give an overestimate to allow binary
  265. * compatibility with future versions of libottery. Use this function instead
  266. * to get the minimal number of bytes to allocate.
  267. *
  268. * @return The minimal number of bytes to use when allocating an
  269. * ottery_config structure.
  270. */
  271. size_t ottery_get_sizeof_config(void);
  272. /**
  273. * @name libottery build flag
  274. *
  275. * @see ottery_Get_build_flags()
  276. *
  277. * @{
  278. */
  279. /** Set if libottery was built with PID checking disabled. If this option is
  280. * present, fork()ing can be dangerous. */
  281. #define OTTERY_BLDFLG_NO_PID_CHECK 0x00000001
  282. /** Set if libottery was built with initialization checking disabled. If this
  283. * option is present, libottery might use an uninitialized, unseeded PRNGs.
  284. */
  285. #define OTTERY_BLDFLG_NO_INIT_CHECK 0x00000002
  286. /** Set if locking was disabled. If this option is present, no libottery
  287. * state, including the global state, is thread-safe. */
  288. #define OTTERY_BLDFLG_NO_LOCKING 0x00000004
  289. /** Set if the clear-after-yield feature was disabled. If this option is
  290. * present, backtracking-resistance is somewhat compromised. */
  291. #define OTTERY_BLDFLG_NO_CLEAR_AFTER_YIELD 0x00000008
  292. /** Set if the stack-wiping feature was disabled. If this option is
  293. * present, programs which accidentally read uninitialized data from the
  294. * stack may leak some cryptographic state. */
  295. #define OTTERY_BLDFLG_NO_WIPE_STACK 0x00000010
  296. /** Set if SIMD support was disabled. This will make libottery slower. */
  297. #define OTTERY_BLDFLG_NO_SIMD 0x00010000
  298. /** @} */
  299. /** A bitmask of any flags that might affect safe and secure program
  300. * operation. */
  301. #define OTTERY_BLDFLG_MASK_SAFETY 0x0000ffff
  302. /**
  303. * Return a bitmask of flags describing the compile-time options that this
  304. * libottery instance was built with. Some of these flags might make the
  305. * library less safe to use!
  306. */
  307. uint32_t ottery_get_build_flags(void);
  308. /**
  309. * Return a run-time version number for Libottery. The first three bytes are
  310. * the major number, minor number, and patch-level respectively. The final
  311. * byte is 0 for a released version, and nonzero otherwise.
  312. */
  313. uint32_t ottery_get_version(void);
  314. /**
  315. * Return a human-readable string representing the run-time Libottery version.
  316. */
  317. const char *ottery_get_version_string(void);
  318. const char *ottery_get_impl_name(void);
  319. #endif