C25519.hpp 5.3 KB

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