C25519.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2026-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_C25519_HPP
  14. #define ZT_C25519_HPP
  15. #include "Utils.hpp"
  16. namespace ZeroTier {
  17. #define ZT_C25519_PUBLIC_KEY_LEN 64
  18. #define ZT_C25519_PRIVATE_KEY_LEN 64
  19. #define ZT_C25519_SIGNATURE_LEN 96
  20. /**
  21. * A combined Curve25519 ECDH and Ed25519 signature engine
  22. */
  23. class C25519 {
  24. public:
  25. struct Public {
  26. uint8_t data[ZT_C25519_PUBLIC_KEY_LEN];
  27. };
  28. struct Private {
  29. uint8_t data[ZT_C25519_PRIVATE_KEY_LEN];
  30. };
  31. struct Signature {
  32. uint8_t data[ZT_C25519_SIGNATURE_LEN];
  33. };
  34. struct Pair {
  35. Public pub;
  36. Private priv;
  37. };
  38. /**
  39. * Generate a C25519 elliptic curve key pair
  40. */
  41. static inline Pair generate()
  42. {
  43. Pair kp;
  44. Utils::getSecureRandom(kp.priv.data, ZT_C25519_PRIVATE_KEY_LEN);
  45. _calcPubDH(kp);
  46. _calcPubED(kp);
  47. return kp;
  48. }
  49. /**
  50. * Generate a key pair satisfying a condition
  51. *
  52. * This begins with a random keypair from a random secret key and then
  53. * iteratively increments the random secret until cond(kp) returns true.
  54. * This is used to compute key pairs in which the public key, its hash
  55. * or some other aspect of it satisfies some condition, such as for a
  56. * hashcash criteria.
  57. *
  58. * @param cond Condition function or function object
  59. * @return Key pair where cond(kp) returns true
  60. * @tparam F Type of 'cond'
  61. */
  62. template <typename F> static inline Pair generateSatisfying(F cond)
  63. {
  64. Pair kp;
  65. void* const priv = (void*)kp.priv.data;
  66. Utils::getSecureRandom(priv, ZT_C25519_PRIVATE_KEY_LEN);
  67. _calcPubED(kp); // do Ed25519 key -- bytes 32-63 of pub and priv
  68. do {
  69. ++(((uint64_t*)priv)[1]);
  70. --(((uint64_t*)priv)[2]);
  71. _calcPubDH(kp); // keep regenerating bytes 0-31 until satisfied
  72. } while (! cond(kp));
  73. return kp;
  74. }
  75. /**
  76. * Perform C25519 ECC key agreement
  77. *
  78. * Actual key bytes are generated from one or more SHA-512 digests of
  79. * the raw result of key agreement.
  80. *
  81. * @param mine My private key
  82. * @param their Their public key
  83. * @param keybuf Buffer to fill
  84. * @param keylen Number of key bytes to generate
  85. */
  86. static void agree(const Private& mine, const Public& their, void* keybuf, unsigned int keylen);
  87. static inline void agree(const Pair& mine, const Public& their, void* keybuf, unsigned int keylen)
  88. {
  89. agree(mine.priv, their, keybuf, keylen);
  90. }
  91. /**
  92. * Sign a message with a sender's key pair
  93. *
  94. * This takes the SHA-521 of msg[] and then signs the first 32 bytes of this
  95. * digest, returning it and the 64-byte ed25519 signature in signature[].
  96. * This results in a signature that verifies both the signer's authenticity
  97. * and the integrity of the message.
  98. *
  99. * This is based on the original ed25519 code from NaCl and the SUPERCOP
  100. * cipher benchmark suite, but with the modification that it always
  101. * produces a signature of fixed 96-byte length based on the hash of an
  102. * arbitrary-length message.
  103. *
  104. * @param myPrivate My private key
  105. * @param myPublic My public key
  106. * @param msg Message to sign
  107. * @param len Length of message in bytes
  108. * @param signature Buffer to fill with signature -- MUST be 96 bytes in length
  109. */
  110. static void sign(const Private& myPrivate, const Public& myPublic, const void* msg, unsigned int len, void* signature);
  111. static inline void sign(const Pair& mine, const void* msg, unsigned int len, void* signature)
  112. {
  113. sign(mine.priv, mine.pub, msg, len, signature);
  114. }
  115. /**
  116. * Sign a message with a sender's key pair
  117. *
  118. * @param myPrivate My private key
  119. * @param myPublic My public key
  120. * @param msg Message to sign
  121. * @param len Length of message in bytes
  122. * @return Signature
  123. */
  124. static inline Signature sign(const Private& myPrivate, const Public& myPublic, const void* msg, unsigned int len)
  125. {
  126. Signature sig;
  127. sign(myPrivate, myPublic, msg, len, sig.data);
  128. return sig;
  129. }
  130. static inline Signature sign(const Pair& mine, const void* msg, unsigned int len)
  131. {
  132. Signature sig;
  133. sign(mine.priv, mine.pub, msg, len, sig.data);
  134. return sig;
  135. }
  136. /**
  137. * Verify a message's signature
  138. *
  139. * @param their Public key to verify against
  140. * @param msg Message to verify signature integrity against
  141. * @param len Length of message in bytes
  142. * @param signature 96-byte signature
  143. * @return True if signature is valid and the message is authentic and unmodified
  144. */
  145. static bool verify(const Public& their, const void* msg, unsigned int len, const void* signature);
  146. /**
  147. * Verify a message's signature
  148. *
  149. * @param their Public key to verify against
  150. * @param msg Message to verify signature integrity against
  151. * @param len Length of message in bytes
  152. * @param signature 96-byte signature
  153. * @return True if signature is valid and the message is authentic and unmodified
  154. */
  155. static inline bool verify(const Public& their, const void* msg, unsigned int len, const Signature& signature)
  156. {
  157. return verify(their, msg, len, signature.data);
  158. }
  159. private:
  160. // derive first 32 bytes of kp.pub from first 32 bytes of kp.priv
  161. // this is the ECDH key
  162. static void _calcPubDH(Pair& kp);
  163. // derive 2nd 32 bytes of kp.pub from 2nd 32 bytes of kp.priv
  164. // this is the Ed25519 sign/verify key
  165. static void _calcPubED(Pair& kp);
  166. };
  167. } // namespace ZeroTier
  168. #endif