crypto.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2007, Roger Dingledine, Nick Mathewson. */
  4. /* See LICENSE for licensing information */
  5. /* $Id$ */
  6. /**
  7. * \file crypto.h
  8. *
  9. * \brief Headers for crypto.c
  10. **/
  11. #ifndef __CRYPTO_H
  12. #define __CRYPTO_H
  13. #define CRYPTO_H_ID "$Id$"
  14. #include <stdio.h>
  15. #include "torint.h"
  16. /** Length of the output of our message digest. */
  17. #define DIGEST_LEN 20
  18. /** Length of our symmetric cipher's keys. */
  19. #define CIPHER_KEY_LEN 16
  20. /** Length of our public keys. */
  21. #define PK_BYTES (1024/8)
  22. /** Length of our DH keys. */
  23. #define DH_BYTES (1024/8)
  24. /** Length of a message digest when encoded in base64 with trailing = signs
  25. * removed. */
  26. #define BASE64_DIGEST_LEN 27
  27. /** Constants used to indicate no padding for public-key encryption */
  28. #define PK_NO_PADDING 60000
  29. /** Constants used to indicate PKCS1 padding for public-key encryption */
  30. #define PK_PKCS1_PADDING 60001
  31. /** Constants used to indicate OAEP padding for public-key encryption */
  32. #define PK_PKCS1_OAEP_PADDING 60002
  33. /** Number of bytes added for PKCS1 padding. */
  34. #define PKCS1_PADDING_OVERHEAD 11
  35. /** Number of bytes added for PKCS1-OAEP padding. */
  36. #define PKCS1_OAEP_PADDING_OVERHEAD 42
  37. /** Length of encoded public key fingerprints, including space; but not
  38. * including terminating NUL. */
  39. #define FINGERPRINT_LEN 49
  40. /** Length of hex encoding of SHA1 digest, not including final NUL. */
  41. #define HEX_DIGEST_LEN 40
  42. typedef struct crypto_pk_env_t crypto_pk_env_t;
  43. typedef struct crypto_cipher_env_t crypto_cipher_env_t;
  44. typedef struct crypto_digest_env_t crypto_digest_env_t;
  45. typedef struct crypto_dh_env_t crypto_dh_env_t;
  46. /* global state */
  47. int crypto_global_init(int hardwareAccel);
  48. void crypto_thread_cleanup(void);
  49. int crypto_global_cleanup(void);
  50. /* environment setup */
  51. crypto_pk_env_t *crypto_new_pk_env(void);
  52. void crypto_free_pk_env(crypto_pk_env_t *env);
  53. /* convenience function: wraps crypto_create_crypto_env, set_key, and init. */
  54. crypto_cipher_env_t *crypto_create_init_cipher(const char *key,
  55. int encrypt_mode);
  56. crypto_cipher_env_t *crypto_new_cipher_env(void);
  57. void crypto_free_cipher_env(crypto_cipher_env_t *env);
  58. /* public key crypto */
  59. int crypto_pk_generate_key(crypto_pk_env_t *env);
  60. int crypto_pk_read_private_key_from_filename(crypto_pk_env_t *env,
  61. const char *keyfile);
  62. int crypto_pk_write_public_key_to_string(crypto_pk_env_t *env,
  63. char **dest, size_t *len);
  64. int crypto_pk_read_public_key_from_string(crypto_pk_env_t *env,
  65. const char *src, size_t len);
  66. int crypto_pk_write_private_key_to_filename(crypto_pk_env_t *env,
  67. const char *fname);
  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. uint64_t crypto_rand_uint64(uint64_t max);
  134. struct smartlist_t;
  135. void *smartlist_choose(const struct smartlist_t *sl);
  136. int base64_encode(char *dest, size_t destlen, const char *src, size_t srclen);
  137. int base64_decode(char *dest, size_t destlen, const char *src, size_t srclen);
  138. /** Characters that can appear (case-insensitively) in a base-32 encoding. */
  139. #define BASE32_CHARS "abcdefghijklmnopqrstuvwxyz234567"
  140. void base32_encode(char *dest, size_t destlen, const char *src, size_t srclen);
  141. int digest_to_base64(char *d64, const char *digest);
  142. int digest_from_base64(char *digest, const char *d64);
  143. /** Length of RFC2440-style S2K specifier: the first 8 bytes are a salt, the
  144. * 9th describes how much iteration to do. */
  145. #define S2K_SPECIFIER_LEN 9
  146. void secret_to_key(char *key_out, size_t key_out_len, const char *secret,
  147. size_t secret_len, const char *s2k_specifier);
  148. #endif