crypto.h 7.0 KB

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