crypto_curve25519.h 3.2 KB

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