crypto.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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-2017, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file crypto.h
  8. *
  9. * \brief Headers for crypto.c
  10. **/
  11. #ifndef TOR_CRYPTO_H
  12. #define TOR_CRYPTO_H
  13. #include "orconfig.h"
  14. #include <stdio.h>
  15. #include "torint.h"
  16. #include "testsupport.h"
  17. #include "compat.h"
  18. #include "util.h"
  19. #include "crypto_rsa.h"
  20. #include "keccak-tiny/keccak-tiny.h"
  21. /** Length of our symmetric cipher's keys of 128-bit. */
  22. #define CIPHER_KEY_LEN 16
  23. /** Length of our symmetric cipher's IV of 128-bit. */
  24. #define CIPHER_IV_LEN 16
  25. /** Length of our symmetric cipher's keys of 256-bit. */
  26. #define CIPHER256_KEY_LEN 32
  27. /** Length of our DH keys. */
  28. #define DH_BYTES (1024/8)
  29. /** Length of encoded public key fingerprints, including space; but not
  30. * including terminating NUL. */
  31. #define FINGERPRINT_LEN 49
  32. typedef struct aes_cnt_cipher crypto_cipher_t;
  33. typedef struct crypto_dh_t crypto_dh_t;
  34. /* global state */
  35. int crypto_early_init(void) ATTR_WUR;
  36. int crypto_global_init(int hardwareAccel,
  37. const char *accelName,
  38. const char *accelPath) ATTR_WUR;
  39. #ifdef USE_DMALLOC
  40. int crypto_use_tor_alloc_functions(void);
  41. #endif
  42. void crypto_thread_cleanup(void);
  43. int crypto_global_cleanup(void);
  44. /* environment setup */
  45. void crypto_set_tls_dh_prime(void);
  46. crypto_cipher_t *crypto_cipher_new(const char *key);
  47. crypto_cipher_t *crypto_cipher_new_with_bits(const char *key, int bits);
  48. crypto_cipher_t *crypto_cipher_new_with_iv(const char *key, const char *iv);
  49. crypto_cipher_t *crypto_cipher_new_with_iv_and_bits(const uint8_t *key,
  50. const uint8_t *iv,
  51. int bits);
  52. void crypto_cipher_free_(crypto_cipher_t *env);
  53. #define crypto_cipher_free(c) \
  54. FREE_AND_NULL(crypto_cipher_t, crypto_cipher_free_, (c))
  55. /* symmetric crypto */
  56. const char *crypto_cipher_get_key(crypto_cipher_t *env);
  57. int crypto_cipher_encrypt(crypto_cipher_t *env, char *to,
  58. const char *from, size_t fromlen);
  59. int crypto_cipher_decrypt(crypto_cipher_t *env, char *to,
  60. const char *from, size_t fromlen);
  61. void crypto_cipher_crypt_inplace(crypto_cipher_t *env, char *d, size_t len);
  62. int crypto_cipher_encrypt_with_iv(const char *key,
  63. char *to, size_t tolen,
  64. const char *from, size_t fromlen);
  65. int crypto_cipher_decrypt_with_iv(const char *key,
  66. char *to, size_t tolen,
  67. const char *from, size_t fromlen);
  68. /* Key negotiation */
  69. #define DH_TYPE_CIRCUIT 1
  70. #define DH_TYPE_REND 2
  71. #define DH_TYPE_TLS 3
  72. crypto_dh_t *crypto_dh_new(int dh_type);
  73. crypto_dh_t *crypto_dh_dup(const crypto_dh_t *dh);
  74. int crypto_dh_get_bytes(crypto_dh_t *dh);
  75. int crypto_dh_generate_public(crypto_dh_t *dh);
  76. int crypto_dh_get_public(crypto_dh_t *dh, char *pubkey_out,
  77. size_t pubkey_out_len);
  78. ssize_t crypto_dh_compute_secret(int severity, crypto_dh_t *dh,
  79. const char *pubkey, size_t pubkey_len,
  80. char *secret_out, size_t secret_out_len);
  81. void crypto_dh_free_(crypto_dh_t *dh);
  82. #define crypto_dh_free(dh) FREE_AND_NULL(crypto_dh_t, crypto_dh_free_, (dh))
  83. int crypto_expand_key_material_TAP(const uint8_t *key_in,
  84. size_t key_in_len,
  85. uint8_t *key_out, size_t key_out_len);
  86. int crypto_expand_key_material_rfc5869_sha256(
  87. const uint8_t *key_in, size_t key_in_len,
  88. const uint8_t *salt_in, size_t salt_in_len,
  89. const uint8_t *info_in, size_t info_in_len,
  90. uint8_t *key_out, size_t key_out_len);
  91. /* random numbers */
  92. int crypto_seed_rng(void) ATTR_WUR;
  93. MOCK_DECL(void,crypto_rand,(char *to, size_t n));
  94. void crypto_rand_unmocked(char *to, size_t n);
  95. void crypto_strongest_rand(uint8_t *out, size_t out_len);
  96. int crypto_rand_int(unsigned int max);
  97. int crypto_rand_int_range(unsigned int min, unsigned int max);
  98. uint64_t crypto_rand_uint64_range(uint64_t min, uint64_t max);
  99. time_t crypto_rand_time_range(time_t min, time_t max);
  100. uint64_t crypto_rand_uint64(uint64_t max);
  101. double crypto_rand_double(void);
  102. struct tor_weak_rng_t;
  103. void crypto_seed_weak_rng(struct tor_weak_rng_t *rng);
  104. int crypto_init_siphash_key(void);
  105. char *crypto_random_hostname(int min_rand_len, int max_rand_len,
  106. const char *prefix, const char *suffix);
  107. struct smartlist_t;
  108. void *smartlist_choose(const struct smartlist_t *sl);
  109. void smartlist_shuffle(struct smartlist_t *sl);
  110. /** OpenSSL-based utility functions. */
  111. void memwipe(void *mem, uint8_t byte, size_t sz);
  112. /* Prototypes for private functions only used by tortls.c, crypto.c, and the
  113. * unit tests. */
  114. struct dh_st;
  115. struct dh_st *crypto_dh_get_dh_(crypto_dh_t *dh);
  116. void crypto_add_spaces_to_fp(char *out, size_t outlen, const char *in);
  117. #ifdef CRYPTO_PRIVATE
  118. STATIC int crypto_force_rand_ssleay(void);
  119. STATIC int crypto_strongest_rand_raw(uint8_t *out, size_t out_len);
  120. #ifdef TOR_UNIT_TESTS
  121. extern int break_strongest_rng_syscall;
  122. extern int break_strongest_rng_fallback;
  123. #endif
  124. #endif /* defined(CRYPTO_PRIVATE) */
  125. #endif /* !defined(TOR_CRYPTO_H) */