C25519.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2018 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. static inline void agree(const Pair &mine,const Public &their,void *keybuf,unsigned int keylen) { agree(mine.priv,their,keybuf,keylen); }
  94. /**
  95. * Sign a message with a sender's key pair
  96. *
  97. * This takes the SHA-521 of msg[] and then signs the first 32 bytes of this
  98. * digest, returning it and the 64-byte ed25519 signature in signature[].
  99. * This results in a signature that verifies both the signer's authenticity
  100. * and the integrity of the message.
  101. *
  102. * This is based on the original ed25519 code from NaCl and the SUPERCOP
  103. * cipher benchmark suite, but with the modification that it always
  104. * produces a signature of fixed 96-byte length based on the hash of an
  105. * arbitrary-length message.
  106. *
  107. * @param myPrivate My private key
  108. * @param myPublic My public key
  109. * @param msg Message to sign
  110. * @param len Length of message in bytes
  111. * @param signature Buffer to fill with signature -- MUST be 96 bytes in length
  112. */
  113. static void sign(const Private &myPrivate,const Public &myPublic,const void *msg,unsigned int len,void *signature);
  114. static inline void sign(const Pair &mine,const void *msg,unsigned int len,void *signature) { sign(mine.priv,mine.pub,msg,len,signature); }
  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