C25519.hpp 6.5 KB

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