crypto_curve25519.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* Copyright (c) 2012-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #ifndef TOR_CRYPTO_CURVE25519_H
  4. #define TOR_CRYPTO_CURVE25519_H
  5. #include "testsupport.h"
  6. #include "torint.h"
  7. /** Length of a curve25519 public key when encoded. */
  8. #define CURVE25519_PUBKEY_LEN 32
  9. /** Length of a curve25519 secret key when encoded. */
  10. #define CURVE25519_SECKEY_LEN 32
  11. /** Length of the result of a curve25519 handshake. */
  12. #define CURVE25519_OUTPUT_LEN 32
  13. /** Wrapper type for a curve25519 public key.
  14. *
  15. * (We define a separate type for these to make it less likely that we'll
  16. * mistake them for secret keys.)
  17. * */
  18. typedef struct curve25519_public_key_t {
  19. uint8_t public_key[CURVE25519_PUBKEY_LEN];
  20. } curve25519_public_key_t;
  21. /** Wrapper type for a curve25519 secret key
  22. *
  23. * (We define a separate type for these to make it less likely that we'll
  24. * mistake them for public keys.)
  25. **/
  26. typedef struct curve25519_secret_key_t {
  27. uint8_t secret_key[CURVE25519_SECKEY_LEN];
  28. } curve25519_secret_key_t;
  29. /** A paired public and private key for curve25519. **/
  30. typedef struct curve25519_keypair_t {
  31. curve25519_public_key_t pubkey;
  32. curve25519_secret_key_t seckey;
  33. } curve25519_keypair_t;
  34. /* These functions require that we actually know how to use curve25519 keys.
  35. * The other data structures and functions in this header let us parse them,
  36. * store them, and move them around.
  37. */
  38. int curve25519_public_key_is_ok(const curve25519_public_key_t *);
  39. int curve25519_secret_key_generate(curve25519_secret_key_t *key_out,
  40. int extra_strong);
  41. void curve25519_public_key_generate(curve25519_public_key_t *key_out,
  42. const curve25519_secret_key_t *seckey);
  43. int curve25519_keypair_generate(curve25519_keypair_t *keypair_out,
  44. int extra_strong);
  45. void curve25519_handshake(uint8_t *output,
  46. const curve25519_secret_key_t *,
  47. const curve25519_public_key_t *);
  48. int curve25519_keypair_write_to_file(const curve25519_keypair_t *keypair,
  49. const char *fname,
  50. const char *tag);
  51. int curve25519_keypair_read_from_file(curve25519_keypair_t *keypair_out,
  52. char **tag_out,
  53. const char *fname);
  54. int curve25519_rand_seckey_bytes(uint8_t *out, int extra_strong);
  55. #ifdef CRYPTO_CURVE25519_PRIVATE
  56. STATIC int curve25519_impl(uint8_t *output, const uint8_t *secret,
  57. const uint8_t *basepoint);
  58. STATIC int curve25519_basepoint_impl(uint8_t *output, const uint8_t *secret);
  59. #endif /* defined(CRYPTO_CURVE25519_PRIVATE) */
  60. #define CURVE25519_BASE64_PADDED_LEN 44
  61. int curve25519_public_from_base64(curve25519_public_key_t *pkey,
  62. const char *input);
  63. int curve25519_public_to_base64(char *output,
  64. const curve25519_public_key_t *pkey);
  65. void curve25519_set_impl_params(int use_ed);
  66. void curve25519_init(void);
  67. #endif /* !defined(TOR_CRYPTO_CURVE25519_H) */