crypto_ed25519.h 5.4 KB

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