C25519.hpp 6.5 KB

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