crypto_ed25519.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* Copyright (c) 2012-2016, 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. int ed25519_checksig(const ed25519_signature_t *signature,
  46. const uint8_t *msg, size_t len,
  47. const ed25519_public_key_t *pubkey);
  48. /**
  49. * A collection of information necessary to check an Ed25519 signature. Used
  50. * for batch verification.
  51. */
  52. typedef struct {
  53. /** The public key that supposedly generated the signature. */
  54. const ed25519_public_key_t *pubkey;
  55. /** The signature to check. */
  56. ed25519_signature_t signature;
  57. /** The message that the signature is supposed to have been applied to. */
  58. const uint8_t *msg;
  59. /** The length of the message. */
  60. size_t len;
  61. } ed25519_checkable_t;
  62. int ed25519_checksig_batch(int *okay_out,
  63. const ed25519_checkable_t *checkable,
  64. int n_checkable);
  65. int ed25519_keypair_from_curve25519_keypair(ed25519_keypair_t *out,
  66. int *signbit_out,
  67. const curve25519_keypair_t *inp);
  68. int ed25519_public_key_from_curve25519_public_key(ed25519_public_key_t *pubkey,
  69. const curve25519_public_key_t *pubkey_in,
  70. int signbit);
  71. int ed25519_keypair_blind(ed25519_keypair_t *out,
  72. const ed25519_keypair_t *inp,
  73. const uint8_t *param);
  74. int ed25519_public_blind(ed25519_public_key_t *out,
  75. const ed25519_public_key_t *inp,
  76. const uint8_t *param);
  77. /* XXXX read encrypted, write encrypted. */
  78. int ed25519_seckey_write_to_file(const ed25519_secret_key_t *seckey,
  79. const char *filename,
  80. const char *tag);
  81. int ed25519_seckey_read_from_file(ed25519_secret_key_t *seckey_out,
  82. char **tag_out,
  83. const char *filename);
  84. int ed25519_pubkey_write_to_file(const ed25519_public_key_t *pubkey,
  85. const char *filename,
  86. const char *tag);
  87. int ed25519_pubkey_read_from_file(ed25519_public_key_t *pubkey_out,
  88. char **tag_out,
  89. const char *filename);
  90. void ed25519_keypair_free(ed25519_keypair_t *kp);
  91. int ed25519_pubkey_eq(const ed25519_public_key_t *key1,
  92. const ed25519_public_key_t *key2);
  93. void ed25519_set_impl_params(int use_donna);
  94. void ed25519_init(void);
  95. #ifdef TOR_UNIT_TESTS
  96. void crypto_ed25519_testing_force_impl(const char *name);
  97. void crypto_ed25519_testing_restore_impl(void);
  98. #endif
  99. #endif