crypto.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 "lib/cc/torint.h"
  16. #include "common/compat.h"
  17. #include "common/util.h"
  18. #include "common/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. void crypto_thread_cleanup(void);
  36. int crypto_global_cleanup(void);
  37. /* environment setup */
  38. crypto_cipher_t *crypto_cipher_new(const char *key);
  39. crypto_cipher_t *crypto_cipher_new_with_bits(const char *key, int bits);
  40. crypto_cipher_t *crypto_cipher_new_with_iv(const char *key, const char *iv);
  41. crypto_cipher_t *crypto_cipher_new_with_iv_and_bits(const uint8_t *key,
  42. const uint8_t *iv,
  43. int bits);
  44. void crypto_cipher_free_(crypto_cipher_t *env);
  45. #define crypto_cipher_free(c) \
  46. FREE_AND_NULL(crypto_cipher_t, crypto_cipher_free_, (c))
  47. /* symmetric crypto */
  48. const char *crypto_cipher_get_key(crypto_cipher_t *env);
  49. int crypto_cipher_encrypt(crypto_cipher_t *env, char *to,
  50. const char *from, size_t fromlen);
  51. int crypto_cipher_decrypt(crypto_cipher_t *env, char *to,
  52. const char *from, size_t fromlen);
  53. void crypto_cipher_crypt_inplace(crypto_cipher_t *env, char *d, size_t len);
  54. int crypto_cipher_encrypt_with_iv(const char *key,
  55. char *to, size_t tolen,
  56. const char *from, size_t fromlen);
  57. int crypto_cipher_decrypt_with_iv(const char *key,
  58. char *to, size_t tolen,
  59. const char *from, size_t fromlen);
  60. void crypto_add_spaces_to_fp(char *out, size_t outlen, const char *in);
  61. #endif /* !defined(TOR_CRYPTO_H) */