crypto_rsa.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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-2017, 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 <stdio.h>
  15. #include "torint.h"
  16. #include "testsupport.h"
  17. #include "compat.h"
  18. #include "util.h"
  19. #include "torlog.h"
  20. #include "crypto_curve25519.h"
  21. /** Length of our public keys. */
  22. #define PK_BYTES (1024/8)
  23. /** Constant used to indicate OAEP padding for public-key encryption */
  24. #define PK_PKCS1_OAEP_PADDING 60002
  25. /** Number of bytes added for PKCS1-OAEP padding. */
  26. #define PKCS1_OAEP_PADDING_OVERHEAD 42
  27. /** A public key, or a public/private key-pair. */
  28. typedef struct crypto_pk_t crypto_pk_t;
  29. /* RSA enviroment setup */
  30. MOCK_DECL(crypto_pk_t *,crypto_pk_new,(void));
  31. void crypto_pk_free_(crypto_pk_t *env);
  32. #define crypto_pk_free(pk) FREE_AND_NULL(crypto_pk_t, crypto_pk_free_, (pk))
  33. int crypto_get_rsa_padding_overhead(int padding);
  34. int crypto_get_rsa_padding(int padding);
  35. /* public key crypto */
  36. MOCK_DECL(int, crypto_pk_generate_key_with_bits,(crypto_pk_t *env, int bits));
  37. #define crypto_pk_generate_key(env) \
  38. crypto_pk_generate_key_with_bits((env), (PK_BYTES*8))
  39. int crypto_pk_read_private_key_from_filename(crypto_pk_t *env,
  40. const char *keyfile);
  41. int crypto_pk_write_public_key_to_string(crypto_pk_t *env,
  42. char **dest, size_t *len);
  43. int crypto_pk_write_private_key_to_string(crypto_pk_t *env,
  44. char **dest, size_t *len);
  45. int crypto_pk_read_public_key_from_string(crypto_pk_t *env,
  46. const char *src, size_t len);
  47. int crypto_pk_read_private_key_from_string(crypto_pk_t *env,
  48. const char *s, ssize_t len);
  49. int crypto_pk_write_private_key_to_filename(crypto_pk_t *env,
  50. const char *fname);
  51. int crypto_pk_check_key(crypto_pk_t *env);
  52. int crypto_pk_cmp_keys(const crypto_pk_t *a, const crypto_pk_t *b);
  53. int crypto_pk_eq_keys(const crypto_pk_t *a, const crypto_pk_t *b);
  54. size_t crypto_pk_keysize(const crypto_pk_t *env);
  55. int crypto_pk_num_bits(crypto_pk_t *env);
  56. crypto_pk_t *crypto_pk_dup_key(crypto_pk_t *orig);
  57. crypto_pk_t *crypto_pk_copy_full(crypto_pk_t *orig);
  58. int crypto_pk_key_is_private(const crypto_pk_t *key);
  59. int crypto_pk_public_exponent_ok(crypto_pk_t *env);
  60. int crypto_pk_public_encrypt(crypto_pk_t *env, char *to, size_t tolen,
  61. const char *from, size_t fromlen, int padding);
  62. int crypto_pk_private_decrypt(crypto_pk_t *env, char *to, size_t tolen,
  63. const char *from, size_t fromlen,
  64. int padding, int warnOnFailure);
  65. MOCK_DECL(int, crypto_pk_public_checksig,(const crypto_pk_t *env,
  66. char *to, size_t tolen,
  67. const char *from, size_t fromlen));
  68. int crypto_pk_private_sign(const crypto_pk_t *env, char *to, size_t tolen,
  69. const char *from, size_t fromlen);
  70. int crypto_pk_asn1_encode(const crypto_pk_t *pk, char *dest, size_t dest_len);
  71. crypto_pk_t *crypto_pk_asn1_decode(const char *str, size_t len);
  72. int crypto_pk_get_fingerprint(crypto_pk_t *pk, char *fp_out,int add_space);
  73. int crypto_pk_get_hashed_fingerprint(crypto_pk_t *pk, char *fp_out);
  74. int crypto_pk_base64_encode(const crypto_pk_t *pk, char **priv_out);
  75. crypto_pk_t *crypto_pk_base64_decode(const char *str, size_t len);
  76. /* Prototypes for private functions only used by tortls.c, crypto.c, and the
  77. * unit tests. */
  78. struct rsa_st;
  79. struct rsa_st *crypto_pk_get_rsa_(crypto_pk_t *env);
  80. crypto_pk_t *crypto_new_pk_from_rsa_(struct rsa_st *rsa);
  81. MOCK_DECL(struct evp_pkey_st *, crypto_pk_get_evp_pkey_,(crypto_pk_t *env,
  82. int private));
  83. struct evp_pkey_st;
  84. #ifdef TOR_UNIT_TESTS
  85. void crypto_pk_assign_(crypto_pk_t *dest, const crypto_pk_t *src);
  86. #endif
  87. #endif