crypto.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #define DIGEST_LEN 20
  8. #define CIPHER_KEY_LEN 16
  9. #define CIPHER_IV_LEN 0
  10. #define PK_BITS 1024
  11. #define PK_BYTES (PK_BITS/8)
  12. #define DH_BITS 1024
  13. #define DH_BYTES (DH_BITS/8)
  14. #define CRYPTO_DH_SIZE DH_BYTES
  15. #define PK_NO_PADDING 60000
  16. #define PK_PKCS1_PADDING 60001
  17. #define PK_PKCS1_OAEP_PADDING 60002
  18. #define PKCS1_PADDING_OVERHEAD 11
  19. #define PKCS1_OAEP_PADDING_OVERHEAD 42
  20. typedef struct crypto_pk_env_t crypto_pk_env_t;
  21. typedef struct crypto_cipher_env_t crypto_cipher_env_t;
  22. typedef struct crypto_digest_env_t crypto_digest_env_t;
  23. typedef struct crypto_dh_env_t crypto_dh_env_t;
  24. /* global state */
  25. int crypto_global_init();
  26. int crypto_global_cleanup();
  27. /* environment setup */
  28. crypto_pk_env_t *crypto_new_pk_env(void);
  29. void crypto_free_pk_env(crypto_pk_env_t *env);
  30. crypto_cipher_env_t *crypto_new_cipher_env(void);
  31. void crypto_free_cipher_env(crypto_cipher_env_t *env);
  32. /* public key crypto */
  33. int crypto_pk_generate_key(crypto_pk_env_t *env);
  34. int crypto_pk_read_private_key_from_file(crypto_pk_env_t *env, FILE *src);
  35. int crypto_pk_read_public_key_from_file(crypto_pk_env_t *env, FILE *src);
  36. int crypto_pk_write_public_key_to_string(crypto_pk_env_t *env, char **dest, int *len);
  37. int crypto_pk_read_public_key_from_string(crypto_pk_env_t *env, const char *src, int len);
  38. int crypto_pk_write_private_key_to_file(crypto_pk_env_t *env, FILE *dest);
  39. int crypto_pk_write_private_key_to_filename(crypto_pk_env_t *env, const char *fname);
  40. int crypto_pk_write_public_key_to_file(crypto_pk_env_t *env, FILE *dest);
  41. int crypto_pk_check_key(crypto_pk_env_t *env);
  42. int crypto_pk_read_private_key_from_filename(crypto_pk_env_t *env, const char *keyfile);
  43. int crypto_pk_cmp_keys(crypto_pk_env_t *a, crypto_pk_env_t *b);
  44. crypto_pk_env_t *crypto_pk_dup_key(crypto_pk_env_t *orig);
  45. int crypto_pk_keysize(crypto_pk_env_t *env);
  46. int crypto_pk_public_encrypt(crypto_pk_env_t *env, const unsigned char *from, int fromlen, unsigned char *to, int padding);
  47. int crypto_pk_private_decrypt(crypto_pk_env_t *env, const unsigned char *from, int fromlen, unsigned char *to, int padding);
  48. int crypto_pk_private_sign(crypto_pk_env_t *env, const unsigned char *from, int fromlen, unsigned char *to);
  49. int crypto_pk_private_sign_digest(crypto_pk_env_t *env, const unsigned char *from, int fromlen, unsigned char *to);
  50. int crypto_pk_public_checksig(crypto_pk_env_t *env, const unsigned char *from, int fromlen, unsigned char *to);
  51. int crypto_pk_public_checksig_digest(crypto_pk_env_t *env, const unsigned char *data, int datalen, const unsigned char *sig, int siglen);
  52. int crypto_pk_public_hybrid_encrypt(crypto_pk_env_t *env,
  53. const unsigned char *from, int fromlen,
  54. unsigned char *to, int padding, int force);
  55. int crypto_pk_private_hybrid_decrypt(crypto_pk_env_t *env,
  56. const unsigned char *from, int fromlen,
  57. unsigned char *to,int padding);
  58. #define FINGERPRINT_LEN 49
  59. int crypto_pk_asn1_encode(crypto_pk_env_t *pk, char *dest, int dest_len);
  60. crypto_pk_env_t *crypto_pk_asn1_decode(const char *str, int len);
  61. int crypto_pk_get_digest(crypto_pk_env_t *pk, char *digest_out);
  62. int crypto_pk_get_fingerprint(crypto_pk_env_t *pk, char *fp_out);
  63. int crypto_pk_check_fingerprint_syntax(const char *s);
  64. int base64_encode(char *dest, int destlen, const char *src, int srclen);
  65. int base64_decode(char *dest, int destlen, const char *src, int srclen);
  66. #define BASE32_CHARS "abcdefghijklmnopqrstuvwxyz012345"
  67. int base32_encode(char *dest, int destlen, const char *src, int srclen);
  68. /* Key negotiation */
  69. crypto_dh_env_t *crypto_dh_new();
  70. int crypto_dh_get_bytes(crypto_dh_env_t *dh);
  71. int crypto_dh_generate_public(crypto_dh_env_t *dh);
  72. int crypto_dh_get_public(crypto_dh_env_t *dh, char *pubkey_out,
  73. int pubkey_out_len);
  74. int crypto_dh_compute_secret(crypto_dh_env_t *dh,
  75. const char *pubkey, int pubkey_len,
  76. char *secret_out, int secret_out_len);
  77. void crypto_dh_free(crypto_dh_env_t *dh);
  78. /* symmetric crypto */
  79. int crypto_cipher_generate_key(crypto_cipher_env_t *env);
  80. int crypto_cipher_set_iv(crypto_cipher_env_t *env, const unsigned char *iv);
  81. int crypto_cipher_set_key(crypto_cipher_env_t *env, const unsigned char *key);
  82. int crypto_cipher_encrypt_init_cipher(crypto_cipher_env_t *env);
  83. int crypto_cipher_decrypt_init_cipher(crypto_cipher_env_t *env);
  84. const unsigned char *crypto_cipher_get_key(crypto_cipher_env_t *env);
  85. int crypto_cipher_encrypt(crypto_cipher_env_t *env, const unsigned char *from, unsigned int fromlen, unsigned char *to);
  86. int crypto_cipher_decrypt(crypto_cipher_env_t *env, const unsigned char *from, unsigned int fromlen, unsigned char *to);
  87. /* only implemented for CRYPTO_CIPHER_AES_CTR */
  88. int crypto_cipher_rewind(crypto_cipher_env_t *env, long delta);
  89. int crypto_cipher_advance(crypto_cipher_env_t *env, long delta);
  90. /* convenience function: wraps crypto_create_crypto_env, set_key, set_iv, and init. */
  91. crypto_cipher_env_t *crypto_create_init_cipher(const char *key, const char *iv, int encrypt_mode);
  92. /* SHA-1 */
  93. int crypto_digest(const unsigned char *m, int len, unsigned char *digest);
  94. crypto_digest_env_t *crypto_new_digest_env();
  95. void crypto_free_digest_env(crypto_digest_env_t *digest);
  96. void crypto_digest_add_bytes(crypto_digest_env_t *digest, const char *data,
  97. size_t len);
  98. void crypto_digest_get_digest(crypto_digest_env_t *digest,
  99. char *out, size_t out_len);
  100. crypto_digest_env_t *crypto_digest_dup(const crypto_digest_env_t *digest);
  101. void crypto_digest_assign(crypto_digest_env_t *into,
  102. const crypto_digest_env_t *from);
  103. /* random numbers */
  104. int crypto_seed_rng();
  105. int crypto_rand(unsigned int n, unsigned char *to);
  106. void crypto_pseudo_rand(unsigned int n, unsigned char *to);
  107. int crypto_pseudo_rand_int(unsigned int max);
  108. /* errors */
  109. const char *crypto_perror();
  110. #endif
  111. /*
  112. Local Variables:
  113. mode:c
  114. indent-tabs-mode:nil
  115. c-basic-offset:2
  116. End:
  117. */