crypto_ed25519.h 5.2 KB

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