crypto.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 "compat.h"
  17. #include "util.h"
  18. #include "crypto_rsa.h"
  19. /** Length of our symmetric cipher's keys of 128-bit. */
  20. #define CIPHER_KEY_LEN 16
  21. /** Length of our symmetric cipher's IV of 128-bit. */
  22. #define CIPHER_IV_LEN 16
  23. /** Length of our symmetric cipher's keys of 256-bit. */
  24. #define CIPHER256_KEY_LEN 32
  25. /** Length of our DH keys. */
  26. #define DH_BYTES (1024/8)
  27. /** Length of encoded public key fingerprints, including space; but not
  28. * including terminating NUL. */
  29. #define FINGERPRINT_LEN 49
  30. typedef struct aes_cnt_cipher crypto_cipher_t;
  31. typedef struct crypto_dh_t crypto_dh_t;
  32. /* global state */
  33. int crypto_init_siphash_key(void);
  34. int crypto_early_init(void) ATTR_WUR;
  35. int crypto_global_init(int hardwareAccel,
  36. const char *accelName,
  37. const char *accelPath) ATTR_WUR;
  38. #ifdef USE_DMALLOC
  39. int crypto_use_tor_alloc_functions(void);
  40. #endif
  41. void crypto_thread_cleanup(void);
  42. int crypto_global_cleanup(void);
  43. /* environment setup */
  44. void crypto_set_tls_dh_prime(void);
  45. crypto_cipher_t *crypto_cipher_new(const char *key);
  46. crypto_cipher_t *crypto_cipher_new_with_bits(const char *key, int bits);
  47. crypto_cipher_t *crypto_cipher_new_with_iv(const char *key, const char *iv);
  48. crypto_cipher_t *crypto_cipher_new_with_iv_and_bits(const uint8_t *key,
  49. const uint8_t *iv,
  50. int bits);
  51. void crypto_cipher_free_(crypto_cipher_t *env);
  52. #define crypto_cipher_free(c) \
  53. FREE_AND_NULL(crypto_cipher_t, crypto_cipher_free_, (c))
  54. /* symmetric crypto */
  55. const char *crypto_cipher_get_key(crypto_cipher_t *env);
  56. int crypto_cipher_encrypt(crypto_cipher_t *env, char *to,
  57. const char *from, size_t fromlen);
  58. int crypto_cipher_decrypt(crypto_cipher_t *env, char *to,
  59. const char *from, size_t fromlen);
  60. void crypto_cipher_crypt_inplace(crypto_cipher_t *env, char *d, size_t len);
  61. int crypto_cipher_encrypt_with_iv(const char *key,
  62. char *to, size_t tolen,
  63. const char *from, size_t fromlen);
  64. int crypto_cipher_decrypt_with_iv(const char *key,
  65. char *to, size_t tolen,
  66. const char *from, size_t fromlen);
  67. /* Key negotiation */
  68. #define DH_TYPE_CIRCUIT 1
  69. #define DH_TYPE_REND 2
  70. #define DH_TYPE_TLS 3
  71. crypto_dh_t *crypto_dh_new(int dh_type);
  72. crypto_dh_t *crypto_dh_dup(const crypto_dh_t *dh);
  73. int crypto_dh_get_bytes(crypto_dh_t *dh);
  74. int crypto_dh_generate_public(crypto_dh_t *dh);
  75. int crypto_dh_get_public(crypto_dh_t *dh, char *pubkey_out,
  76. size_t pubkey_out_len);
  77. ssize_t crypto_dh_compute_secret(int severity, crypto_dh_t *dh,
  78. const char *pubkey, size_t pubkey_len,
  79. char *secret_out, size_t secret_out_len);
  80. void crypto_dh_free_(crypto_dh_t *dh);
  81. #define crypto_dh_free(dh) FREE_AND_NULL(crypto_dh_t, crypto_dh_free_, (dh))
  82. int crypto_expand_key_material_TAP(const uint8_t *key_in,
  83. size_t key_in_len,
  84. uint8_t *key_out, size_t key_out_len);
  85. int crypto_expand_key_material_rfc5869_sha256(
  86. const uint8_t *key_in, size_t key_in_len,
  87. const uint8_t *salt_in, size_t salt_in_len,
  88. const uint8_t *info_in, size_t info_in_len,
  89. uint8_t *key_out, size_t key_out_len);
  90. /* Prototypes for private functions only used by tortls.c, crypto.c, and the
  91. * unit tests. */
  92. struct dh_st;
  93. struct dh_st *crypto_dh_get_dh_(crypto_dh_t *dh);
  94. void crypto_add_spaces_to_fp(char *out, size_t outlen, const char *in);
  95. #endif /* !defined(TOR_CRYPTO_H) */