crypto.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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-2018, 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 encoded public key fingerprints, including space; but not
  26. * including terminating NUL. */
  27. #define FINGERPRINT_LEN 49
  28. typedef struct aes_cnt_cipher crypto_cipher_t;
  29. /* global state */
  30. int crypto_init_siphash_key(void);
  31. int crypto_early_init(void) ATTR_WUR;
  32. int crypto_global_init(int hardwareAccel,
  33. const char *accelName,
  34. const char *accelPath) ATTR_WUR;
  35. #ifdef USE_DMALLOC
  36. int crypto_use_tor_alloc_functions(void);
  37. #endif
  38. void crypto_thread_cleanup(void);
  39. int crypto_global_cleanup(void);
  40. /* environment setup */
  41. crypto_cipher_t *crypto_cipher_new(const char *key);
  42. crypto_cipher_t *crypto_cipher_new_with_bits(const char *key, int bits);
  43. crypto_cipher_t *crypto_cipher_new_with_iv(const char *key, const char *iv);
  44. crypto_cipher_t *crypto_cipher_new_with_iv_and_bits(const uint8_t *key,
  45. const uint8_t *iv,
  46. int bits);
  47. void crypto_cipher_free_(crypto_cipher_t *env);
  48. #define crypto_cipher_free(c) \
  49. FREE_AND_NULL(crypto_cipher_t, crypto_cipher_free_, (c))
  50. /* symmetric crypto */
  51. const char *crypto_cipher_get_key(crypto_cipher_t *env);
  52. int crypto_cipher_encrypt(crypto_cipher_t *env, char *to,
  53. const char *from, size_t fromlen);
  54. int crypto_cipher_decrypt(crypto_cipher_t *env, char *to,
  55. const char *from, size_t fromlen);
  56. void crypto_cipher_crypt_inplace(crypto_cipher_t *env, char *d, size_t len);
  57. int crypto_cipher_encrypt_with_iv(const char *key,
  58. char *to, size_t tolen,
  59. const char *from, size_t fromlen);
  60. int crypto_cipher_decrypt_with_iv(const char *key,
  61. char *to, size_t tolen,
  62. const char *from, size_t fromlen);
  63. void crypto_add_spaces_to_fp(char *out, size_t outlen, const char *in);
  64. #endif /* !defined(TOR_CRYPTO_H) */