crypto_curve25519.h 3.1 KB

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