crypto.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar.
  2. * Copyright 2004-2005 Roger Dingledine, Nick Mathewson */
  3. /* See LICENSE for licensing information */
  4. /* $Id$ */
  5. /**
  6. * \file crypto.h
  7. *
  8. * \brief Headers for crypto.c
  9. **/
  10. #ifndef __CRYPTO_H
  11. #define __CRYPTO_H
  12. #define CRYPTO_H_ID "$Id$"
  13. #include <stdio.h>
  14. /** Length of the output of our message digest. */
  15. #define DIGEST_LEN 20
  16. /** Length of our symmetric cipher's keys. */
  17. #define CIPHER_KEY_LEN 16
  18. /** Length of our public keys. */
  19. #define PK_BYTES (1024/8)
  20. /** Length of our DH keys. */
  21. #define DH_BYTES (1024/8)
  22. /** Length of a message digest when encoded in base64 with trailing = signs
  23. * removed. */
  24. #define BASE64_DIGEST_LEN 27
  25. /** Constants used to indicate no padding for public-key encryption */
  26. #define PK_NO_PADDING 60000
  27. /** Constants used to indicate PKCS1 padding for public-key encryption */
  28. #define PK_PKCS1_PADDING 60001
  29. /** Constants used to indicate OAEP padding for public-key encryption */
  30. #define PK_PKCS1_OAEP_PADDING 60002
  31. /** Number of bytes added for PKCS1 padding. */
  32. #define PKCS1_PADDING_OVERHEAD 11
  33. /** Number of bytes added for PKCS1-OAEP padding. */
  34. #define PKCS1_OAEP_PADDING_OVERHEAD 42
  35. /** Length of encoded public key fingerprints, including space; but not
  36. * including terminating NUL. */
  37. #define FINGERPRINT_LEN 49
  38. /** Length of hex encoding of SHA1 digest, not including final NUL. */
  39. #define HEX_DIGEST_LEN 40
  40. typedef struct crypto_pk_env_t crypto_pk_env_t;
  41. typedef struct crypto_cipher_env_t crypto_cipher_env_t;
  42. typedef struct crypto_digest_env_t crypto_digest_env_t;
  43. typedef struct crypto_dh_env_t crypto_dh_env_t;
  44. /* global state */
  45. int crypto_global_init(int hardwareAccel);
  46. void crypto_thread_cleanup(void);
  47. int crypto_global_cleanup(void);
  48. /* environment setup */
  49. crypto_pk_env_t *crypto_new_pk_env(void);
  50. void crypto_free_pk_env(crypto_pk_env_t *env);
  51. /* convenience function: wraps crypto_create_crypto_env, set_key, and init. */
  52. crypto_cipher_env_t *crypto_create_init_cipher(const char *key,
  53. int encrypt_mode);
  54. crypto_cipher_env_t *crypto_new_cipher_env(void);
  55. void crypto_free_cipher_env(crypto_cipher_env_t *env);
  56. /* public key crypto */
  57. int crypto_pk_generate_key(crypto_pk_env_t *env);
  58. int crypto_pk_read_private_key_from_filename(crypto_pk_env_t *env,
  59. const char *keyfile);
  60. int crypto_pk_write_public_key_to_string(crypto_pk_env_t *env,
  61. char **dest, size_t *len);
  62. int crypto_pk_read_public_key_from_string(crypto_pk_env_t *env,
  63. const char *src, size_t len);
  64. int crypto_pk_write_private_key_to_filename(crypto_pk_env_t *env,
  65. const char *fname);
  66. int crypto_pk_DER64_encode_public_key(crypto_pk_env_t *env, char **dest);
  67. crypto_pk_env_t *crypto_pk_DER64_decode_public_key(const char *in);
  68. int crypto_pk_check_key(crypto_pk_env_t *env);
  69. int crypto_pk_cmp_keys(crypto_pk_env_t *a, crypto_pk_env_t *b);
  70. size_t crypto_pk_keysize(crypto_pk_env_t *env);
  71. crypto_pk_env_t *crypto_pk_dup_key(crypto_pk_env_t *orig);
  72. int crypto_pk_public_encrypt(crypto_pk_env_t *env, char *to,
  73. const char *from, size_t fromlen, int padding);
  74. int crypto_pk_private_decrypt(crypto_pk_env_t *env, char *to,
  75. const char *from, size_t fromlen,
  76. int padding, int warnOnFailure);
  77. int crypto_pk_public_checksig(crypto_pk_env_t *env, char *to,
  78. const char *from, size_t fromlen);
  79. int crypto_pk_public_checksig_digest(crypto_pk_env_t *env, const char *data,
  80. int datalen, const char *sig, int siglen);
  81. int crypto_pk_private_sign(crypto_pk_env_t *env, char *to,
  82. const char *from, size_t fromlen);
  83. int crypto_pk_private_sign_digest(crypto_pk_env_t *env, char *to,
  84. const char *from, size_t fromlen);
  85. int crypto_pk_public_hybrid_encrypt(crypto_pk_env_t *env, char *to,
  86. const char *from, size_t fromlen,
  87. int padding, int force);
  88. int crypto_pk_private_hybrid_decrypt(crypto_pk_env_t *env, char *to,
  89. const char *from, size_t fromlen,
  90. int padding, int warnOnFailure);
  91. int crypto_pk_asn1_encode(crypto_pk_env_t *pk, char *dest, int dest_len);
  92. crypto_pk_env_t *crypto_pk_asn1_decode(const char *str, size_t len);
  93. int crypto_pk_get_digest(crypto_pk_env_t *pk, char *digest_out);
  94. int crypto_pk_get_fingerprint(crypto_pk_env_t *pk, char *fp_out,int add_space);
  95. int crypto_pk_check_fingerprint_syntax(const char *s);
  96. /* symmetric crypto */
  97. int crypto_cipher_generate_key(crypto_cipher_env_t *env);
  98. int crypto_cipher_set_key(crypto_cipher_env_t *env, const char *key);
  99. const char *crypto_cipher_get_key(crypto_cipher_env_t *env);
  100. int crypto_cipher_encrypt_init_cipher(crypto_cipher_env_t *env);
  101. int crypto_cipher_decrypt_init_cipher(crypto_cipher_env_t *env);
  102. int crypto_cipher_encrypt(crypto_cipher_env_t *env, char *to,
  103. const char *from, size_t fromlen);
  104. int crypto_cipher_decrypt(crypto_cipher_env_t *env, char *to,
  105. const char *from, size_t fromlen);
  106. /* SHA-1 */
  107. int crypto_digest(char *digest, const char *m, size_t len);
  108. crypto_digest_env_t *crypto_new_digest_env(void);
  109. void crypto_free_digest_env(crypto_digest_env_t *digest);
  110. void crypto_digest_add_bytes(crypto_digest_env_t *digest, const char *data,
  111. size_t len);
  112. void crypto_digest_get_digest(crypto_digest_env_t *digest,
  113. char *out, size_t out_len);
  114. crypto_digest_env_t *crypto_digest_dup(const crypto_digest_env_t *digest);
  115. void crypto_digest_assign(crypto_digest_env_t *into,
  116. const crypto_digest_env_t *from);
  117. /* Key negotiation */
  118. crypto_dh_env_t *crypto_dh_new(void);
  119. int crypto_dh_get_bytes(crypto_dh_env_t *dh);
  120. int crypto_dh_generate_public(crypto_dh_env_t *dh);
  121. int crypto_dh_get_public(crypto_dh_env_t *dh, char *pubkey_out,
  122. size_t pubkey_out_len);
  123. int crypto_dh_compute_secret(crypto_dh_env_t *dh,
  124. const char *pubkey, size_t pubkey_len,
  125. char *secret_out, size_t secret_out_len);
  126. void crypto_dh_free(crypto_dh_env_t *dh);
  127. int crypto_expand_key_material(const char *key_in, size_t in_len,
  128. char *key_out, size_t key_out_len);
  129. /* random numbers */
  130. int crypto_seed_rng(void);
  131. int crypto_rand(char *to, size_t n);
  132. int crypto_rand_int(unsigned int max);
  133. struct smartlist_t;
  134. void *smartlist_choose(const struct smartlist_t *sl);
  135. int base64_encode(char *dest, size_t destlen, const char *src, size_t srclen);
  136. int base64_decode(char *dest, size_t destlen, const char *src, size_t srclen);
  137. #define BASE32_CHARS "abcdefghijklmnopqrstuvwxyz234567"
  138. void base32_encode(char *dest, size_t destlen, const char *src, size_t srclen);
  139. int digest_to_base64(char *d64, const char *digest);
  140. int digest_from_base64(char *digest, const char *d64);
  141. #define S2K_SPECIFIER_LEN 9
  142. void secret_to_key(char *key_out, size_t key_out_len, const char *secret,
  143. size_t secret_len, const char *s2k_specifier);
  144. #endif