crypto_rand.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* Copyright (c) 2001, Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file crypto_rand.h
  8. *
  9. * \brief Common functions for using (pseudo-)random number generators.
  10. **/
  11. #ifndef TOR_CRYPTO_RAND_H
  12. #define TOR_CRYPTO_RAND_H
  13. #include "lib/cc/compat_compiler.h"
  14. #include "lib/cc/torint.h"
  15. #include "lib/testsupport/testsupport.h"
  16. #include "lib/malloc/malloc.h"
  17. /* random numbers */
  18. int crypto_seed_rng(void) ATTR_WUR;
  19. MOCK_DECL(void,crypto_rand,(char *to, size_t n));
  20. void crypto_rand_unmocked(char *to, size_t n);
  21. void crypto_strongest_rand(uint8_t *out, size_t out_len);
  22. MOCK_DECL(void,crypto_strongest_rand_,(uint8_t *out, size_t out_len));
  23. int crypto_rand_int(unsigned int max);
  24. unsigned crypto_rand_uint(unsigned limit);
  25. int crypto_rand_int_range(unsigned int min, unsigned int max);
  26. uint64_t crypto_rand_uint64_range(uint64_t min, uint64_t max);
  27. time_t crypto_rand_time_range(time_t min, time_t max);
  28. uint32_t crypto_rand_u32(void);
  29. uint64_t crypto_rand_uint64(uint64_t max);
  30. double crypto_rand_double(void);
  31. struct tor_weak_rng_t;
  32. void crypto_seed_weak_rng(struct tor_weak_rng_t *rng);
  33. char *crypto_random_hostname(int min_rand_len, int max_rand_len,
  34. const char *prefix, const char *suffix);
  35. struct smartlist_t;
  36. void *smartlist_choose(const struct smartlist_t *sl);
  37. void smartlist_shuffle(struct smartlist_t *sl);
  38. int crypto_force_rand_ssleay(void);
  39. /**
  40. * A fast PRNG, for use when the PRNG provided by our crypto library isn't
  41. * fast enough. This one _should_ be cryptographically strong, but
  42. * has seen less auditing than the PRNGs in OpenSSL and NSS. Use with
  43. * caution.
  44. *
  45. * Note that this object is NOT thread-safe. If you need a thread-safe
  46. * prng, use crypto_rand(), or wrap this in a mutex.
  47. **/
  48. typedef struct crypto_fast_rng_t crypto_fast_rng_t;
  49. /**
  50. * Number of bytes used to seed a crypto_rand_fast_t.
  51. **/
  52. crypto_fast_rng_t *crypto_fast_rng_new(void);
  53. #define CRYPTO_FAST_RNG_SEED_LEN 48
  54. crypto_fast_rng_t *crypto_fast_rng_new_from_seed(const uint8_t *seed);
  55. void crypto_fast_rng_getbytes(crypto_fast_rng_t *rng, uint8_t *out, size_t n);
  56. void crypto_fast_rng_free_(crypto_fast_rng_t *);
  57. #define crypto_fast_rng_free(c) \
  58. FREE_AND_NULL(crypto_fast_rng_t, crypto_fast_rng_free_, (c))
  59. unsigned crypto_fast_rng_get_uint(crypto_fast_rng_t *rng, unsigned limit);
  60. uint64_t crypto_fast_rng_get_uint64(crypto_fast_rng_t *rng, uint64_t limit);
  61. double crypto_fast_rng_get_double(crypto_fast_rng_t *rng);
  62. #if defined(TOR_UNIT_TESTS)
  63. /* Used for white-box testing */
  64. size_t crypto_fast_rng_get_bytes_used_per_stream(void);
  65. #endif
  66. #ifdef CRYPTO_RAND_PRIVATE
  67. STATIC int crypto_strongest_rand_raw(uint8_t *out, size_t out_len);
  68. #ifdef TOR_UNIT_TESTS
  69. extern int break_strongest_rng_syscall;
  70. extern int break_strongest_rng_fallback;
  71. #endif
  72. #endif /* defined(CRYPTO_RAND_PRIVATE) */
  73. #endif /* !defined(TOR_CRYPTO_RAND_H) */