crypto_rsa.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* Copyright (c) 2001, Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file crypto_rsa.h
  8. *
  9. * \brief Headers for crypto_rsa.c
  10. **/
  11. #ifndef TOR_CRYPTO_RSA_H
  12. #define TOR_CRYPTO_RSA_H
  13. #include "orconfig.h"
  14. #include "lib/crypt_ops/crypto_digest.h"
  15. #include "lib/cc/torint.h"
  16. #include "lib/testsupport/testsupport.h"
  17. #include "lib/log/log.h"
  18. /** Length of our public keys. */
  19. #define PK_BYTES (1024/8)
  20. /** Constant used to indicate OAEP padding for public-key encryption */
  21. #define PK_PKCS1_OAEP_PADDING 60002
  22. /** Number of bytes added for PKCS1-OAEP padding. */
  23. #define PKCS1_OAEP_PADDING_OVERHEAD 42
  24. /** Length of encoded public key fingerprints, including space; but not
  25. * including terminating NUL. */
  26. #define FINGERPRINT_LEN 49
  27. /** Value of 'e' to use in our public keys */
  28. #define TOR_RSA_EXPONENT 65537
  29. /** A public key, or a public/private key-pair. */
  30. typedef struct crypto_pk_t crypto_pk_t;
  31. /* RSA environment setup */
  32. MOCK_DECL(crypto_pk_t *,crypto_pk_new,(void));
  33. void crypto_pk_free_(crypto_pk_t *env);
  34. #define crypto_pk_free(pk) FREE_AND_NULL(crypto_pk_t, crypto_pk_free_, (pk))
  35. int crypto_get_rsa_padding_overhead(int padding);
  36. int crypto_get_rsa_padding(int padding);
  37. /* public key crypto */
  38. MOCK_DECL(int, crypto_pk_generate_key_with_bits,(crypto_pk_t *env, int bits));
  39. #define crypto_pk_generate_key(env) \
  40. crypto_pk_generate_key_with_bits((env), (PK_BYTES*8))
  41. int crypto_pk_read_private_key_from_filename(crypto_pk_t *env,
  42. const char *keyfile);
  43. int crypto_pk_write_public_key_to_string(crypto_pk_t *env,
  44. char **dest, size_t *len);
  45. int crypto_pk_write_private_key_to_string(crypto_pk_t *env,
  46. char **dest, size_t *len);
  47. int crypto_pk_read_public_key_from_string(crypto_pk_t *env,
  48. const char *src, size_t len);
  49. int crypto_pk_read_private_key_from_string(crypto_pk_t *env,
  50. const char *s, ssize_t len);
  51. int crypto_pk_write_private_key_to_filename(crypto_pk_t *env,
  52. const char *fname);
  53. int crypto_pk_is_valid_private_key(const crypto_pk_t *env);
  54. int crypto_pk_cmp_keys(const crypto_pk_t *a, const crypto_pk_t *b);
  55. int crypto_pk_eq_keys(const crypto_pk_t *a, const crypto_pk_t *b);
  56. size_t crypto_pk_keysize(const crypto_pk_t *env);
  57. int crypto_pk_num_bits(crypto_pk_t *env);
  58. crypto_pk_t *crypto_pk_dup_key(crypto_pk_t *orig);
  59. crypto_pk_t *crypto_pk_copy_full(crypto_pk_t *orig);
  60. int crypto_pk_key_is_private(const crypto_pk_t *key);
  61. int crypto_pk_public_exponent_ok(const crypto_pk_t *env);
  62. int crypto_pk_obsolete_public_hybrid_encrypt(crypto_pk_t *env, char *to,
  63. size_t tolen,
  64. const char *from, size_t fromlen,
  65. int padding, int force);
  66. int crypto_pk_obsolete_private_hybrid_decrypt(crypto_pk_t *env, char *to,
  67. size_t tolen,
  68. const char *from, size_t fromlen,
  69. int padding, int warnOnFailure);
  70. int crypto_pk_public_encrypt(crypto_pk_t *env, char *to, size_t tolen,
  71. const char *from, size_t fromlen, int padding);
  72. int crypto_pk_private_decrypt(crypto_pk_t *env, char *to, size_t tolen,
  73. const char *from, size_t fromlen,
  74. int padding, int warnOnFailure);
  75. MOCK_DECL(int, crypto_pk_public_checksig,(const crypto_pk_t *env,
  76. char *to, size_t tolen,
  77. const char *from, size_t fromlen));
  78. int crypto_pk_private_sign(const crypto_pk_t *env, char *to, size_t tolen,
  79. const char *from, size_t fromlen);
  80. int crypto_pk_asn1_encode(const crypto_pk_t *pk, char *dest, size_t dest_len);
  81. crypto_pk_t *crypto_pk_asn1_decode(const char *str, size_t len);
  82. int crypto_pk_asn1_encode_private(const crypto_pk_t *pk,
  83. char *dest, size_t dest_len);
  84. crypto_pk_t *crypto_pk_asn1_decode_private(const char *str, size_t len);
  85. int crypto_pk_get_fingerprint(crypto_pk_t *pk, char *fp_out,int add_space);
  86. int crypto_pk_get_hashed_fingerprint(crypto_pk_t *pk, char *fp_out);
  87. void crypto_add_spaces_to_fp(char *out, size_t outlen, const char *in);
  88. MOCK_DECL(int, crypto_pk_public_checksig_digest,(crypto_pk_t *env,
  89. const char *data, size_t datalen, const char *sig, size_t siglen));
  90. int crypto_pk_private_sign_digest(crypto_pk_t *env, char *to, size_t tolen,
  91. const char *from, size_t fromlen);
  92. int crypto_pk_get_digest(const crypto_pk_t *pk, char *digest_out);
  93. int crypto_pk_get_common_digests(crypto_pk_t *pk,
  94. common_digests_t *digests_out);
  95. int crypto_pk_base64_encode_private(const crypto_pk_t *pk, char **priv_out);
  96. crypto_pk_t *crypto_pk_base64_decode_private(const char *str, size_t len);
  97. #ifdef ENABLE_OPENSSL
  98. /* Prototypes for private functions only used by tortls.c, crypto.c, and the
  99. * unit tests. */
  100. struct rsa_st;
  101. struct evp_pkey_st;
  102. struct rsa_st *crypto_pk_get_openssl_rsa_(crypto_pk_t *env);
  103. crypto_pk_t *crypto_new_pk_from_openssl_rsa_(struct rsa_st *rsa);
  104. MOCK_DECL(struct evp_pkey_st *, crypto_pk_get_openssl_evp_pkey_,(
  105. crypto_pk_t *env,int private));
  106. #endif
  107. #ifdef ENABLE_NSS
  108. struct SECKEYPublicKeyStr;
  109. struct SECKEYPrivateKeyStr;
  110. crypto_pk_t *crypto_pk_new_from_nss_pubkey(struct SECKEYPublicKeyStr *pub);
  111. const struct SECKEYPublicKeyStr *crypto_pk_get_nss_pubkey(
  112. const crypto_pk_t *key);
  113. const struct SECKEYPrivateKeyStr *crypto_pk_get_nss_privkey(
  114. const crypto_pk_t *key);
  115. #endif
  116. void crypto_pk_assign_public(crypto_pk_t *dest, const crypto_pk_t *src);
  117. void crypto_pk_assign_private(crypto_pk_t *dest, const crypto_pk_t *src);
  118. #ifdef TOR_UNIT_TESTS
  119. #ifdef ENABLE_NSS
  120. struct SECItemStr;
  121. STATIC int secitem_uint_cmp(const struct SECItemStr *a,
  122. const struct SECItemStr *b);
  123. #endif
  124. #endif
  125. #endif