C25519.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_C25519_HPP
  27. #define ZT_C25519_HPP
  28. #include "Utils.hpp"
  29. namespace ZeroTier {
  30. #define ZT_C25519_PUBLIC_KEY_LEN 64
  31. #define ZT_C25519_PRIVATE_KEY_LEN 64
  32. #define ZT_C25519_SIGNATURE_LEN 96
  33. /**
  34. * A combined Curve25519 ECDH and Ed25519 signature engine
  35. */
  36. class C25519
  37. {
  38. public:
  39. struct Public { uint8_t data[ZT_C25519_PUBLIC_KEY_LEN]; };
  40. struct Private { uint8_t data[ZT_C25519_PRIVATE_KEY_LEN]; };
  41. struct Signature { uint8_t data[ZT_C25519_SIGNATURE_LEN]; };
  42. struct Pair { Public pub; Private priv; };
  43. /**
  44. * Generate a C25519 elliptic curve key pair
  45. */
  46. static inline Pair generate()
  47. {
  48. Pair kp;
  49. Utils::getSecureRandom(kp.priv.data,ZT_C25519_PRIVATE_KEY_LEN);
  50. _calcPubDH(kp);
  51. _calcPubED(kp);
  52. return kp;
  53. }
  54. /**
  55. * Generate a key pair satisfying a condition
  56. *
  57. * This begins with a random keypair from a random secret key and then
  58. * iteratively increments the random secret until cond(kp) returns true.
  59. * This is used to compute key pairs in which the public key, its hash
  60. * or some other aspect of it satisfies some condition, such as for a
  61. * hashcash criteria.
  62. *
  63. * @param cond Condition function or function object
  64. * @return Key pair where cond(kp) returns true
  65. * @tparam F Type of 'cond'
  66. */
  67. template<typename F>
  68. static inline Pair generateSatisfying(F cond)
  69. {
  70. Pair kp;
  71. void *const priv = (void *)kp.priv.data;
  72. Utils::getSecureRandom(priv,ZT_C25519_PRIVATE_KEY_LEN);
  73. _calcPubED(kp); // do Ed25519 key -- bytes 32-63 of pub and priv
  74. do {
  75. ++(((uint64_t *)priv)[1]);
  76. --(((uint64_t *)priv)[2]);
  77. _calcPubDH(kp); // keep regenerating bytes 0-31 until satisfied
  78. } while (!cond(kp));
  79. return kp;
  80. }
  81. /**
  82. * Perform C25519 ECC key agreement
  83. *
  84. * Actual key bytes are generated from one or more SHA-512 digests of
  85. * the raw result of key agreement.
  86. *
  87. * @param mine My private key
  88. * @param their Their public key
  89. * @param keybuf Buffer to fill
  90. * @param keylen Number of key bytes to generate
  91. */
  92. static void agree(const Private &mine,const Public &their,void *keybuf,unsigned int keylen);
  93. /**
  94. * Sign a message with a sender's key pair
  95. *
  96. * This takes the SHA-521 of msg[] and then signs the first 32 bytes of this
  97. * digest, returning it and the 64-byte ed25519 signature in signature[].
  98. * This results in a signature that verifies both the signer's authenticity
  99. * and the integrity of the message.
  100. *
  101. * This is based on the original ed25519 code from NaCl and the SUPERCOP
  102. * cipher benchmark suite, but with the modification that it always
  103. * produces a signature of fixed 96-byte length based on the hash of an
  104. * arbitrary-length message.
  105. *
  106. * @param myPrivate My private key
  107. * @param myPublic My public key
  108. * @param msg Message to sign
  109. * @param len Length of message in bytes
  110. * @param signature Buffer to fill with signature -- MUST be 96 bytes in length
  111. */
  112. static void sign(const Private &myPrivate,const Public &myPublic,const void *msg,unsigned int len,void *signature);
  113. /**
  114. * Sign a message with a sender's key pair
  115. *
  116. * Note that this generates a 96-byte signature that contains an extra 32 bytes
  117. * of hash data. This data is included for historical reasons and is optional. The
  118. * verify function here will take the first 64 bytes only (normal ed25519 signature)
  119. * or a 96-byte length signature with the extra input hash data.
  120. *
  121. * @param myPrivate My private key
  122. * @param myPublic My public key
  123. * @param msg Message to sign
  124. * @param len Length of message in bytes
  125. * @return Signature
  126. */
  127. static inline Signature sign(const Private &myPrivate,const Public &myPublic,const void *msg,unsigned int len)
  128. {
  129. Signature sig;
  130. sign(myPrivate,myPublic,msg,len,sig.data);
  131. return sig;
  132. }
  133. static inline Signature sign(const Pair &mine,const void *msg,unsigned int len)
  134. {
  135. Signature sig;
  136. sign(mine.priv,mine.pub,msg,len,sig.data);
  137. return sig;
  138. }
  139. /**
  140. * Verify a message's signature
  141. *
  142. * @param their Public key to verify against
  143. * @param msg Message to verify signature integrity against
  144. * @param len Length of message in bytes
  145. * @param signature Signature bytes
  146. * @param siglen Length of signature in bytes
  147. * @return True if signature is valid and the message is authentic and unmodified
  148. */
  149. static bool verify(const Public &their,const void *msg,unsigned int len,const void *signature,const unsigned int siglen);
  150. /**
  151. * Verify a message's signature
  152. *
  153. * @param their Public key to verify against
  154. * @param msg Message to verify signature integrity against
  155. * @param len Length of message in bytes
  156. * @param signature 96-byte signature
  157. * @return True if signature is valid and the message is authentic and unmodified
  158. */
  159. static inline bool verify(const Public &their,const void *msg,unsigned int len,const Signature &signature) { return verify(their,msg,len,signature.data,96); }
  160. private:
  161. // derive first 32 bytes of kp.pub from first 32 bytes of kp.priv
  162. // this is the ECDH key
  163. static void _calcPubDH(Pair &kp);
  164. // derive 2nd 32 bytes of kp.pub from 2nd 32 bytes of kp.priv
  165. // this is the Ed25519 sign/verify key
  166. static void _calcPubED(Pair &kp);
  167. };
  168. } // namespace ZeroTier
  169. #endif