crypto.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* Copyright 2001,2002 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. /* available encryption primitives */
  9. #define CRYPTO_CIPHER_IDENTITY 0
  10. #define CRYPTO_CIPHER_DES 1
  11. #define CRYPTO_CIPHER_RC4 2
  12. #define CRYPTO_CIPHER_3DES 3
  13. #define CRYPTO_PK_RSA 0
  14. typedef struct
  15. {
  16. int type;
  17. int refs; /* reference counting; so we don't have to copy keys */
  18. unsigned char *key;
  19. /* auxiliary data structure(s) used by the underlying crypto library */
  20. unsigned char *aux;
  21. } crypto_pk_env_t;
  22. typedef struct
  23. {
  24. int type;
  25. unsigned char *key;
  26. unsigned char *iv;
  27. /* auxiliary data structure(s) used by the underlying crypto library */
  28. unsigned char *aux;
  29. } crypto_cipher_env_t;
  30. /* global state */
  31. int crypto_global_init();
  32. int crypto_global_cleanup();
  33. /* environment setup */
  34. crypto_pk_env_t *crypto_new_pk_env(int type);
  35. void crypto_free_pk_env(crypto_pk_env_t *env);
  36. crypto_cipher_env_t *crypto_new_cipher_env(int type);
  37. void crypto_free_cipher_env(crypto_cipher_env_t *env);
  38. /* public key crypto */
  39. int crypto_pk_generate_key(crypto_pk_env_t *env);
  40. int crypto_pk_read_private_key_from_file(crypto_pk_env_t *env, FILE *src);
  41. int crypto_pk_read_public_key_from_file(crypto_pk_env_t *env, FILE *src);
  42. int crypto_pk_write_public_key_to_string(crypto_pk_env_t *env, char **dest, int *len);
  43. int crypto_pk_read_public_key_from_string(crypto_pk_env_t *env, char *src, int len);
  44. int crypto_pk_write_private_key_to_file(crypto_pk_env_t *env, FILE *dest);
  45. int crypto_pk_write_public_key_to_file(crypto_pk_env_t *env, FILE *dest);
  46. int crypto_pk_check_key(crypto_pk_env_t *env);
  47. int crypto_pk_read_private_key_from_filename(crypto_pk_env_t *env, unsigned char *keyfile);
  48. int crypto_pk_set_key(crypto_pk_env_t *env, unsigned char *key);
  49. int crypto_pk_cmp_keys(crypto_pk_env_t *a, crypto_pk_env_t *b);
  50. crypto_pk_env_t *crypto_pk_dup_key(crypto_pk_env_t *orig);
  51. int crypto_pk_keysize(crypto_pk_env_t *env);
  52. int crypto_pk_public_encrypt(crypto_pk_env_t *env, unsigned char *from, int fromlen, unsigned char *to, int padding);
  53. int crypto_pk_private_decrypt(crypto_pk_env_t *env, unsigned char *from, int fromlen, unsigned char *to, int padding);
  54. int crypto_pk_private_sign(crypto_pk_env_t *env, unsigned char *from, int fromlen, unsigned char *to);
  55. int crypto_pk_public_checksig(crypto_pk_env_t *env, unsigned char *from, int fromlen, unsigned char *to);
  56. int base64_encode(char *dest, int destlen, char *src, int srclen);
  57. int base64_decode(char *dest, int destlen, char *src, int srclen);
  58. /* Key negotiation */
  59. typedef struct crypto_dh_env_st crypto_dh_env_t;
  60. /* #define CRYPTO_DH_SIZE (1536 / 8) */
  61. #define CRYPTO_DH_SIZE (1024 / 8)
  62. crypto_dh_env_t *crypto_dh_new();
  63. int crypto_dh_get_bytes(crypto_dh_env_t *dh);
  64. int crypto_dh_get_public(crypto_dh_env_t *dh, char *pubkey_out,
  65. int pubkey_out_len);
  66. int crypto_dh_compute_secret(crypto_dh_env_t *dh,
  67. char *pubkey, int pubkey_len,
  68. char *secret_out);
  69. void crypto_dh_free(crypto_dh_env_t *dh);
  70. /* symmetric crypto */
  71. int crypto_cipher_generate_key(crypto_cipher_env_t *env);
  72. int crypto_cipher_set_iv(crypto_cipher_env_t *env, unsigned char *iv);
  73. int crypto_cipher_set_key(crypto_cipher_env_t *env, unsigned char *key);
  74. int crypto_cipher_encrypt_init_cipher(crypto_cipher_env_t *env);
  75. int crypto_cipher_decrypt_init_cipher(crypto_cipher_env_t *env);
  76. int crypto_cipher_encrypt(crypto_cipher_env_t *env, unsigned char *from, unsigned int fromlen, unsigned char *to);
  77. int crypto_cipher_decrypt(crypto_cipher_env_t *env, unsigned char *from, unsigned int fromlen, unsigned char *to);
  78. /* convenience function: wraps crypto_create_crypto_env, set_key, set_iv, and init. */
  79. crypto_cipher_env_t *crypto_create_init_cipher(int cipher_type, char *key, char *iv, int encrypt_mode);
  80. /* SHA-1 */
  81. int crypto_SHA_digest(unsigned char *m, int len, unsigned char *digest);
  82. /* random numbers */
  83. int crypto_rand(unsigned int n, unsigned char *to);
  84. int crypto_pseudo_rand(unsigned int n, unsigned char *to);
  85. #define CRYPTO_PSEUDO_RAND_INT(v) crypto_pseudo_rand(sizeof(v),(char*)&(v))
  86. /* errors */
  87. char *crypto_perror();
  88. #endif