crypto.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. /* Prototypes for private functions only used by tortls.c, crypto.c, and the
  92. * unit tests. */
  93. struct dh_st;
  94. struct dh_st *crypto_dh_get_dh_(crypto_dh_t *dh);
  95. void crypto_add_spaces_to_fp(char *out, size_t outlen, const char *in);
  96. #endif /* !defined(TOR_CRYPTO_H) */