crypto.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #ifndef __CRYPTO_H
  5. #define __CRYPTO_H
  6. #include <stdio.h>
  7. #include <openssl/rsa.h>
  8. #include <openssl/dh.h>
  9. /* available encryption primitives */
  10. #define CRYPTO_CIPHER_IDENTITY 0
  11. #define CRYPTO_CIPHER_DES 1
  12. #define CRYPTO_CIPHER_RC4 2
  13. #define CRYPTO_CIPHER_3DES 3
  14. #define CRYPTO_CIPHER_AES_CTR 4
  15. #define CRYPTO_PK_RSA 0
  16. #define CRYPTO_SHA1_DIGEST 0
  17. #define CRYPTO_SHA1_DIGEST_LEN 20
  18. typedef struct crypto_pk_env_t crypto_pk_env_t;
  19. typedef struct crypto_cipher_env_t crypto_cipher_env_t;
  20. typedef struct crypto_digest_env_t crypto_digest_env_t;
  21. /* global state */
  22. int crypto_global_init();
  23. int crypto_global_cleanup();
  24. /* environment setup */
  25. crypto_pk_env_t *crypto_new_pk_env(int type);
  26. void crypto_free_pk_env(crypto_pk_env_t *env);
  27. crypto_cipher_env_t *crypto_new_cipher_env(int type);
  28. void crypto_free_cipher_env(crypto_cipher_env_t *env);
  29. /* public key crypto */
  30. int crypto_pk_generate_key(crypto_pk_env_t *env);
  31. int crypto_pk_read_private_key_from_file(crypto_pk_env_t *env, FILE *src);
  32. int crypto_pk_read_public_key_from_file(crypto_pk_env_t *env, FILE *src);
  33. int crypto_pk_write_public_key_to_string(crypto_pk_env_t *env, char **dest, int *len);
  34. int crypto_pk_read_public_key_from_string(crypto_pk_env_t *env, const char *src, int len);
  35. int crypto_pk_write_private_key_to_file(crypto_pk_env_t *env, FILE *dest);
  36. int crypto_pk_write_private_key_to_filename(crypto_pk_env_t *env, const char *fname);
  37. int crypto_pk_write_public_key_to_file(crypto_pk_env_t *env, FILE *dest);
  38. int crypto_pk_check_key(crypto_pk_env_t *env);
  39. int crypto_pk_read_private_key_from_filename(crypto_pk_env_t *env, const char *keyfile);
  40. int crypto_pk_set_key(crypto_pk_env_t *env, unsigned char *key);
  41. int crypto_pk_cmp_keys(crypto_pk_env_t *a, crypto_pk_env_t *b);
  42. crypto_pk_env_t *crypto_pk_dup_key(crypto_pk_env_t *orig);
  43. int crypto_pk_keysize(crypto_pk_env_t *env);
  44. int crypto_pk_public_encrypt(crypto_pk_env_t *env, unsigned char *from, int fromlen, unsigned char *to, int padding);
  45. int crypto_pk_private_decrypt(crypto_pk_env_t *env, unsigned char *from, int fromlen, unsigned char *to, int padding);
  46. int crypto_pk_private_sign(crypto_pk_env_t *env, unsigned char *from, int fromlen, unsigned char *to);
  47. int crypto_pk_private_sign_digest(crypto_pk_env_t *env, unsigned char *from, int fromlen, unsigned char *to);
  48. int crypto_pk_public_checksig(crypto_pk_env_t *env, unsigned char *from, int fromlen, unsigned char *to);
  49. int crypto_pk_public_checksig_digest(crypto_pk_env_t *env, unsigned char *data, int datalen, unsigned char *sig, int siglen);
  50. int crypto_pk_public_hybrid_encrypt(crypto_pk_env_t *env, unsigned char *from,
  51. int fromlen, unsigned char *to,
  52. int padding);
  53. int crypto_pk_private_hybrid_decrypt(crypto_pk_env_t *env, unsigned char *from,
  54. int fromlen, unsigned char *to,
  55. int padding);
  56. #define FINGERPRINT_LEN 49
  57. int crypto_pk_asn1_encode(crypto_pk_env_t *pk, char *dest, int dest_len);
  58. crypto_pk_env_t *crypto_pk_asn1_decode(const char *str, int len);
  59. int crypto_pk_get_digest(crypto_pk_env_t *pk, char *digest_out);
  60. int crypto_pk_get_fingerprint(crypto_pk_env_t *pk, char *fp_out);
  61. int crypto_pk_check_fingerprint_syntax(const char *s);
  62. int base64_encode(char *dest, int destlen, const char *src, int srclen);
  63. int base64_decode(char *dest, int destlen, const char *src, int srclen);
  64. #define BASE32_CHARS "abcdefghijklmnopqrstuvwxyz012345"
  65. int base32_encode(char *dest, int destlen, const char *src, int srclen);
  66. /* Key negotiation */
  67. typedef struct crypto_dh_env_st {
  68. DH *dh;
  69. } crypto_dh_env_t;
  70. /* #define CRYPTO_DH_SIZE (1536 / 8) */
  71. #define CRYPTO_DH_SIZE (1024 / 8)
  72. crypto_dh_env_t *crypto_dh_new();
  73. int crypto_dh_get_bytes(crypto_dh_env_t *dh);
  74. int crypto_dh_generate_public(crypto_dh_env_t *dh);
  75. int crypto_dh_get_public(crypto_dh_env_t *dh, char *pubkey_out,
  76. int pubkey_out_len);
  77. int crypto_dh_compute_secret(crypto_dh_env_t *dh,
  78. char *pubkey, int pubkey_len,
  79. char *secret_out, int secret_out_len);
  80. void crypto_dh_free(crypto_dh_env_t *dh);
  81. /* symmetric crypto */
  82. int crypto_cipher_generate_key(crypto_cipher_env_t *env);
  83. int crypto_cipher_set_iv(crypto_cipher_env_t *env, unsigned char *iv);
  84. int crypto_cipher_set_key(crypto_cipher_env_t *env, unsigned char *key);
  85. int crypto_cipher_encrypt_init_cipher(crypto_cipher_env_t *env);
  86. int crypto_cipher_decrypt_init_cipher(crypto_cipher_env_t *env);
  87. unsigned char *crypto_cipher_get_key(crypto_cipher_env_t *env);
  88. int crypto_cipher_encrypt(crypto_cipher_env_t *env, unsigned char *from, unsigned int fromlen, unsigned char *to);
  89. int crypto_cipher_decrypt(crypto_cipher_env_t *env, unsigned char *from, unsigned int fromlen, unsigned char *to);
  90. /* only implemented for CRYPTO_CIPHER_AES_CTR */
  91. int crypto_cipher_rewind(crypto_cipher_env_t *env, long delta);
  92. int crypto_cipher_advance(crypto_cipher_env_t *env, long delta);
  93. /* convenience function: wraps crypto_create_crypto_env, set_key, set_iv, and init. */
  94. crypto_cipher_env_t *crypto_create_init_cipher(int cipher_type, char *key, char *iv, int encrypt_mode);
  95. /* SHA-1 */
  96. int crypto_SHA_digest(const unsigned char *m, int len, unsigned char *digest);
  97. crypto_digest_env_t *crypto_new_digest_env(int type);
  98. void crypto_free_digest_env(crypto_digest_env_t *digest);
  99. void crypto_digest_add_bytes(crypto_digest_env_t *digest, const char *data,
  100. size_t len);
  101. void crypto_digest_get_digest(crypto_digest_env_t *digest,
  102. char *out, size_t out_len);
  103. crypto_digest_env_t *crypto_digest_dup(const crypto_digest_env_t *digest);
  104. void crypto_digest_assign(crypto_digest_env_t *into,
  105. const crypto_digest_env_t *from);
  106. /* random numbers */
  107. int crypto_seed_rng();
  108. int crypto_rand(unsigned int n, unsigned char *to);
  109. void crypto_pseudo_rand(unsigned int n, unsigned char *to);
  110. int crypto_pseudo_rand_int(unsigned int max);
  111. /* errors */
  112. char *crypto_perror();
  113. #endif