C25519.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. {
  64. Pair kp;
  65. Utils::getSecureRandom(kp.priv.data,(unsigned int)kp.priv.size());
  66. _calcPubDH(kp);
  67. _calcPubED(kp);
  68. return kp;
  69. }
  70. /**
  71. * Generate a key pair satisfying a condition
  72. *
  73. * This begins with a random keypair from a random secret key and then
  74. * iteratively increments the random secret until cond(kp) returns true.
  75. * This is used to compute key pairs in which the public key, its hash
  76. * or some other aspect of it satisfies some condition, such as for a
  77. * hashcash criteria.
  78. *
  79. * @param cond Condition function or function object
  80. * @return Key pair where cond(kp) returns true
  81. * @tparam F Type of 'cond'
  82. */
  83. template<typename F>
  84. static inline Pair generateSatisfying(F cond)
  85. {
  86. Pair kp;
  87. void *const priv = (void *)kp.priv.data;
  88. Utils::getSecureRandom(priv,(unsigned int)kp.priv.size());
  89. _calcPubED(kp); // do Ed25519 key -- bytes 32-63 of pub and priv
  90. do {
  91. ++(((uint64_t *)priv)[1]);
  92. --(((uint64_t *)priv)[2]);
  93. _calcPubDH(kp); // keep regenerating bytes 0-31 until satisfied
  94. } while (!cond(kp));
  95. return kp;
  96. }
  97. /**
  98. * Perform C25519 ECC key agreement
  99. *
  100. * Actual key bytes are generated from one or more SHA-512 digests of
  101. * the raw result of key agreement.
  102. *
  103. * @param mine My private key
  104. * @param their Their public key
  105. * @param keybuf Buffer to fill
  106. * @param keylen Number of key bytes to generate
  107. */
  108. static void agree(const Private &mine,const Public &their,void *keybuf,unsigned int keylen);
  109. static inline void agree(const Pair &mine,const Public &their,void *keybuf,unsigned int keylen) { agree(mine.priv,their,keybuf,keylen); }
  110. /**
  111. * Sign a message with a sender's key pair
  112. *
  113. * This takes the SHA-521 of msg[] and then signs the first 32 bytes of this
  114. * digest, returning it and the 64-byte ed25519 signature in signature[].
  115. * This results in a signature that verifies both the signer's authenticity
  116. * and the integrity of the message.
  117. *
  118. * This is based on the original ed25519 code from NaCl and the SUPERCOP
  119. * cipher benchmark suite, but with the modification that it always
  120. * produces a signature of fixed 96-byte length based on the hash of an
  121. * arbitrary-length message.
  122. *
  123. * @param myPrivate My private key
  124. * @param myPublic My public key
  125. * @param msg Message to sign
  126. * @param len Length of message in bytes
  127. * @param signature Buffer to fill with signature -- MUST be 96 bytes in length
  128. */
  129. static void sign(const Private &myPrivate,const Public &myPublic,const void *msg,unsigned int len,void *signature);
  130. static inline void sign(const Pair &mine,const void *msg,unsigned int len,void *signature) { sign(mine.priv,mine.pub,msg,len,signature); }
  131. /**
  132. * Sign a message with a sender's key pair
  133. *
  134. * @param myPrivate My private key
  135. * @param myPublic My public key
  136. * @param msg Message to sign
  137. * @param len Length of message in bytes
  138. * @return Signature
  139. */
  140. static inline Signature sign(const Private &myPrivate,const Public &myPublic,const void *msg,unsigned int len)
  141. {
  142. Signature sig;
  143. sign(myPrivate,myPublic,msg,len,sig.data);
  144. return sig;
  145. }
  146. static inline Signature sign(const Pair &mine,const void *msg,unsigned int len)
  147. {
  148. Signature sig;
  149. sign(mine.priv,mine.pub,msg,len,sig.data);
  150. return sig;
  151. }
  152. /**
  153. * Verify a message's signature
  154. *
  155. * @param their Public key to verify against
  156. * @param msg Message to verify signature integrity against
  157. * @param len Length of message in bytes
  158. * @param signature 96-byte signature
  159. * @return True if signature is valid and the message is authentic and unmodified
  160. */
  161. static bool verify(const Public &their,const void *msg,unsigned int len,const void *signature);
  162. /**
  163. * Verify a message's signature
  164. *
  165. * @param their Public key to verify against
  166. * @param msg Message to verify signature integrity against
  167. * @param len Length of message in bytes
  168. * @param signature 96-byte signature
  169. * @return True if signature is valid and the message is authentic and unmodified
  170. */
  171. static inline bool verify(const Public &their,const void *msg,unsigned int len,const Signature &signature)
  172. {
  173. return verify(their,msg,len,signature.data);
  174. }
  175. private:
  176. // derive first 32 bytes of kp.pub from first 32 bytes of kp.priv
  177. // this is the ECDH key
  178. static void _calcPubDH(Pair &kp);
  179. // derive 2nd 32 bytes of kp.pub from 2nd 32 bytes of kp.priv
  180. // this is the Ed25519 sign/verify key
  181. static void _calcPubED(Pair &kp);
  182. };
  183. } // namespace ZeroTier
  184. #endif