C25519.hpp 6.1 KB

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