crypto_ed25519.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* Copyright (c) 2012-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file crypto_ed25519.h
  5. * \brief Header for crypto_ed25519.c
  6. **/
  7. #ifndef TOR_CRYPTO_ED25519_H
  8. #define TOR_CRYPTO_ED25519_H
  9. #include "lib/testsupport/testsupport.h"
  10. #include "lib/cc/torint.h"
  11. #include "lib/crypt_ops/crypto_curve25519.h"
  12. #include "lib/defs/x25519_sizes.h"
  13. /** An Ed25519 signature. */
  14. typedef struct ed25519_signature_t {
  15. uint8_t sig[ED25519_SIG_LEN];
  16. } ed25519_signature_t;
  17. /** An Ed25519 public key */
  18. typedef struct ed25519_public_key_t {
  19. uint8_t pubkey[ED25519_PUBKEY_LEN];
  20. } ed25519_public_key_t;
  21. /** An Ed25519 secret key */
  22. typedef struct ed25519_secret_key_t {
  23. /** Note that we store secret keys in an expanded format that doesn't match
  24. * the format from standard ed25519. Ed25519 stores a 32-byte value k and
  25. * expands it into a 64-byte H(k), using the first 32 bytes for a multiplier
  26. * of the base point, and second 32 bytes as an input to a hash function
  27. * for deriving r. But because we implement key blinding, we need to store
  28. * keys in the 64-byte expanded form. */
  29. uint8_t seckey[ED25519_SECKEY_LEN];
  30. } ed25519_secret_key_t;
  31. /** An Ed25519 keypair. */
  32. typedef struct ed25519_keypair_t {
  33. ed25519_public_key_t pubkey;
  34. ed25519_secret_key_t seckey;
  35. } ed25519_keypair_t;
  36. int ed25519_secret_key_generate(ed25519_secret_key_t *seckey_out,
  37. int extra_strong);
  38. int ed25519_secret_key_from_seed(ed25519_secret_key_t *seckey_out,
  39. const uint8_t *seed);
  40. int ed25519_public_key_generate(ed25519_public_key_t *pubkey_out,
  41. const ed25519_secret_key_t *seckey);
  42. int ed25519_keypair_generate(ed25519_keypair_t *keypair_out, int extra_strong);
  43. int ed25519_sign(ed25519_signature_t *signature_out,
  44. const uint8_t *msg, size_t len,
  45. const ed25519_keypair_t *key);
  46. MOCK_DECL(int,ed25519_checksig,(const ed25519_signature_t *signature,
  47. const uint8_t *msg, size_t len,
  48. const ed25519_public_key_t *pubkey));
  49. MOCK_DECL(int,
  50. ed25519_sign_prefixed,(ed25519_signature_t *signature_out,
  51. const uint8_t *msg, size_t len,
  52. const char *prefix_str,
  53. const ed25519_keypair_t *keypair));
  54. int
  55. ed25519_checksig_prefixed(const ed25519_signature_t *signature,
  56. const uint8_t *msg, size_t len,
  57. const char *prefix_str,
  58. const ed25519_public_key_t *pubkey);
  59. int ed25519_public_key_is_zero(const ed25519_public_key_t *pubkey);
  60. /**
  61. * A collection of information necessary to check an Ed25519 signature. Used
  62. * for batch verification.
  63. */
  64. typedef struct {
  65. /** The public key that supposedly generated the signature. */
  66. const ed25519_public_key_t *pubkey;
  67. /** The signature to check. */
  68. ed25519_signature_t signature;
  69. /** The message that the signature is supposed to have been applied to. */
  70. const uint8_t *msg;
  71. /** The length of the message. */
  72. size_t len;
  73. } ed25519_checkable_t;
  74. MOCK_DECL(int, ed25519_checksig_batch,(int *okay_out,
  75. const ed25519_checkable_t *checkable,
  76. int n_checkable));
  77. int ed25519_keypair_from_curve25519_keypair(ed25519_keypair_t *out,
  78. int *signbit_out,
  79. const curve25519_keypair_t *inp);
  80. int ed25519_public_key_from_curve25519_public_key(ed25519_public_key_t *pubkey,
  81. const curve25519_public_key_t *pubkey_in,
  82. int signbit);
  83. int ed25519_keypair_blind(ed25519_keypair_t *out,
  84. const ed25519_keypair_t *inp,
  85. const uint8_t *param);
  86. int ed25519_public_blind(ed25519_public_key_t *out,
  87. const ed25519_public_key_t *inp,
  88. const uint8_t *param);
  89. /* XXXX read encrypted, write encrypted. */
  90. int ed25519_seckey_write_to_file(const ed25519_secret_key_t *seckey,
  91. const char *filename,
  92. const char *tag);
  93. int ed25519_seckey_read_from_file(ed25519_secret_key_t *seckey_out,
  94. char **tag_out,
  95. const char *filename);
  96. int ed25519_pubkey_write_to_file(const ed25519_public_key_t *pubkey,
  97. const char *filename,
  98. const char *tag);
  99. int ed25519_pubkey_read_from_file(ed25519_public_key_t *pubkey_out,
  100. char **tag_out,
  101. const char *filename);
  102. void ed25519_keypair_free_(ed25519_keypair_t *kp);
  103. #define ed25519_keypair_free(kp) \
  104. FREE_AND_NULL(ed25519_keypair_t, ed25519_keypair_free_, (kp))
  105. int ed25519_pubkey_eq(const ed25519_public_key_t *key1,
  106. const ed25519_public_key_t *key2);
  107. void ed25519_pubkey_copy(ed25519_public_key_t *dest,
  108. const ed25519_public_key_t *src);
  109. void ed25519_set_impl_params(int use_donna);
  110. void ed25519_init(void);
  111. int ed25519_validate_pubkey(const ed25519_public_key_t *pubkey);
  112. #ifdef TOR_UNIT_TESTS
  113. void crypto_ed25519_testing_force_impl(const char *name);
  114. void crypto_ed25519_testing_restore_impl(void);
  115. #endif
  116. #ifdef CRYPTO_ED25519_PRIVATE
  117. MOCK_DECL(STATIC int, ed25519_impl_spot_check, (void));
  118. #endif
  119. #endif /* !defined(TOR_CRYPTO_ED25519_H) */