crypto_ed25519.h 4.2 KB

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