crypto.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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, int encrypt_mode);
  53. crypto_cipher_env_t *crypto_new_cipher_env(void);
  54. void crypto_free_cipher_env(crypto_cipher_env_t *env);
  55. /* public key crypto */
  56. int crypto_pk_generate_key(crypto_pk_env_t *env);
  57. int crypto_pk_read_private_key_from_filename(crypto_pk_env_t *env, const char *keyfile);
  58. int crypto_pk_write_public_key_to_string(crypto_pk_env_t *env, char **dest, size_t *len);
  59. int crypto_pk_read_public_key_from_string(crypto_pk_env_t *env, const char *src, size_t len);
  60. int crypto_pk_write_private_key_to_filename(crypto_pk_env_t *env, const char *fname);
  61. int crypto_pk_DER64_encode_public_key(crypto_pk_env_t *env, char **dest);
  62. crypto_pk_env_t *crypto_pk_DER64_decode_public_key(const char *in);
  63. int crypto_pk_check_key(crypto_pk_env_t *env);
  64. int crypto_pk_cmp_keys(crypto_pk_env_t *a, crypto_pk_env_t *b);
  65. size_t crypto_pk_keysize(crypto_pk_env_t *env);
  66. crypto_pk_env_t *crypto_pk_dup_key(crypto_pk_env_t *orig);
  67. int crypto_pk_public_encrypt(crypto_pk_env_t *env, char *to,
  68. const char *from, size_t fromlen, int padding);
  69. int crypto_pk_private_decrypt(crypto_pk_env_t *env, char *to,
  70. const char *from, size_t fromlen,
  71. int padding, int warnOnFailure);
  72. int crypto_pk_public_checksig(crypto_pk_env_t *env, char *to,
  73. const char *from, size_t fromlen);
  74. int crypto_pk_public_checksig_digest(crypto_pk_env_t *env, const char *data,
  75. int datalen, const char *sig, int siglen);
  76. int crypto_pk_private_sign(crypto_pk_env_t *env, char *to,
  77. const char *from, size_t fromlen);
  78. int crypto_pk_private_sign_digest(crypto_pk_env_t *env, char *to,
  79. const char *from, size_t fromlen);
  80. int crypto_pk_public_hybrid_encrypt(crypto_pk_env_t *env, char *to,
  81. const char *from, size_t fromlen,
  82. int padding, int force);
  83. int crypto_pk_private_hybrid_decrypt(crypto_pk_env_t *env, char *to,
  84. const char *from, size_t fromlen,
  85. int padding, int warnOnFailure);
  86. int crypto_pk_asn1_encode(crypto_pk_env_t *pk, char *dest, int dest_len);
  87. crypto_pk_env_t *crypto_pk_asn1_decode(const char *str, size_t len);
  88. int crypto_pk_get_digest(crypto_pk_env_t *pk, char *digest_out);
  89. int crypto_pk_get_fingerprint(crypto_pk_env_t *pk, char *fp_out,int add_space);
  90. int crypto_pk_check_fingerprint_syntax(const char *s);
  91. /* symmetric crypto */
  92. int crypto_cipher_generate_key(crypto_cipher_env_t *env);
  93. int crypto_cipher_set_key(crypto_cipher_env_t *env, const char *key);
  94. const char *crypto_cipher_get_key(crypto_cipher_env_t *env);
  95. int crypto_cipher_encrypt_init_cipher(crypto_cipher_env_t *env);
  96. int crypto_cipher_decrypt_init_cipher(crypto_cipher_env_t *env);
  97. int crypto_cipher_encrypt(crypto_cipher_env_t *env, char *to,
  98. const char *from, size_t fromlen);
  99. int crypto_cipher_decrypt(crypto_cipher_env_t *env, char *to,
  100. const char *from, size_t fromlen);
  101. /* SHA-1 */
  102. int crypto_digest(char *digest, const char *m, size_t len);
  103. crypto_digest_env_t *crypto_new_digest_env(void);
  104. void crypto_free_digest_env(crypto_digest_env_t *digest);
  105. void crypto_digest_add_bytes(crypto_digest_env_t *digest, const char *data,
  106. size_t len);
  107. void crypto_digest_get_digest(crypto_digest_env_t *digest,
  108. char *out, size_t out_len);
  109. crypto_digest_env_t *crypto_digest_dup(const crypto_digest_env_t *digest);
  110. void crypto_digest_assign(crypto_digest_env_t *into,
  111. const crypto_digest_env_t *from);
  112. /* Key negotiation */
  113. crypto_dh_env_t *crypto_dh_new(void);
  114. int crypto_dh_get_bytes(crypto_dh_env_t *dh);
  115. int crypto_dh_generate_public(crypto_dh_env_t *dh);
  116. int crypto_dh_get_public(crypto_dh_env_t *dh, char *pubkey_out,
  117. size_t pubkey_out_len);
  118. int crypto_dh_compute_secret(crypto_dh_env_t *dh,
  119. const char *pubkey, size_t pubkey_len,
  120. char *secret_out, size_t secret_out_len);
  121. void crypto_dh_free(crypto_dh_env_t *dh);
  122. int crypto_expand_key_material(const char *key_in, size_t in_len,
  123. char *key_out, size_t key_out_len);
  124. /* random numbers */
  125. int crypto_seed_rng(void);
  126. int crypto_rand(char *to, size_t n);
  127. int crypto_rand_int(unsigned int max);
  128. struct smartlist_t;
  129. void *smartlist_choose(const struct smartlist_t *sl);
  130. int base64_encode(char *dest, size_t destlen, const char *src, size_t srclen);
  131. int base64_decode(char *dest, size_t destlen, const char *src, size_t srclen);
  132. #define BASE32_CHARS "abcdefghijklmnopqrstuvwxyz234567"
  133. void base32_encode(char *dest, size_t destlen, const char *src, size_t srclen);
  134. int digest_to_base64(char *d64, const char *digest);
  135. int digest_from_base64(char *digest, const char *d64);
  136. #define S2K_SPECIFIER_LEN 9
  137. void secret_to_key(char *key_out, size_t key_out_len, const char *secret,
  138. size_t secret_len, const char *s2k_specifier);
  139. #endif