ECC.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  4. *
  5. * (c) ZeroTier, Inc.
  6. * https://www.zerotier.com/
  7. */
  8. /*
  9. * This file defines the elliptic curve crypto used for ZeroTier V1. The normal
  10. * public version uses C25519 and Ed25519, while the FIPS version uses NIST.
  11. * FIPS builds are completely incompatible with regular ZeroTier, but that's
  12. * fine since FIPS users typically want a fully isolated private network. If you
  13. * are not such a user you probably don't want this.
  14. */
  15. #ifndef ZT_ECC_HPP
  16. #define ZT_ECC_HPP
  17. #include "Utils.hpp"
  18. #ifdef ZT_FIPS
  19. /* FIPS140/NIST ECC cryptography */
  20. /* Note that to be FIPS we also need to link against a FIPS-certified library. */
  21. #include <openssl/bn.h>
  22. #include <openssl/ec.h>
  23. #include <openssl/err.h>
  24. #include <openssl/evp.h>
  25. #include <openssl/pem.h>
  26. #define ZT_ECC_EPHEMERAL_PUBLIC_KEY_LEN 97 /* Single ECC P-384 key */
  27. #define ZT_ECC_PUBLIC_KEY_SET_LEN (97 * 2) /* Two ECC P-384 keys */
  28. #define ZT_ECC_PRIVATE_KEY_SET_LEN (48 * 2) /* Two ECC P-384 secret keys */
  29. #define ZT_ECC_SIGNATURE_LEN 96 /* NIST P-384 ECDSA signature */
  30. class ECC {
  31. public:
  32. struct Public {
  33. uint8_t data[ZT_ECC_PUBLIC_KEY_SET_LEN];
  34. };
  35. struct Private {
  36. uint8_t data[ZT_ECC_PRIVATE_KEY_SET_LEN];
  37. };
  38. struct Signature {
  39. uint8_t data[ZT_ECC_SIGNATURE_LEN];
  40. };
  41. struct Pair {
  42. Public pub;
  43. Private priv;
  44. };
  45. };
  46. #else // Curve25519 / Ed25519
  47. namespace ZeroTier {
  48. #define ZT_ECC_EPHEMERAL_PUBLIC_KEY_LEN 32 /* Single C25519 ECDH key */
  49. #define ZT_ECC_PUBLIC_KEY_SET_LEN 64 /* C25519 and Ed25519 keys */
  50. #define ZT_ECC_PRIVATE_KEY_SET_LEN 64 /* C25519 and Ed25519 secret keys */
  51. #define ZT_ECC_SIGNATURE_LEN 96 /* Ed25519 signature plus (not necessary) hash */
  52. class ECC {
  53. public:
  54. struct Public {
  55. uint8_t data[ZT_ECC_PUBLIC_KEY_SET_LEN];
  56. };
  57. struct Private {
  58. uint8_t data[ZT_ECC_PRIVATE_KEY_SET_LEN];
  59. };
  60. struct Signature {
  61. uint8_t data[ZT_ECC_SIGNATURE_LEN];
  62. };
  63. struct Pair {
  64. Public pub;
  65. Private priv;
  66. };
  67. /**
  68. * Generate an elliptic curve key pair
  69. */
  70. static inline Pair generate()
  71. {
  72. Pair kp;
  73. Utils::getSecureRandom(kp.priv.data, ZT_ECC_PRIVATE_KEY_SET_LEN);
  74. _calcPubDH(kp);
  75. _calcPubED(kp);
  76. return kp;
  77. }
  78. /**
  79. * Generate a key pair satisfying a condition
  80. *
  81. * This begins with a random keypair from a random secret key and then
  82. * iteratively increments the random secret until cond(kp) returns true.
  83. * This is used to compute key pairs in which the public key, its hash
  84. * or some other aspect of it satisfies some condition, such as for a
  85. * hashcash criteria.
  86. *
  87. * @param cond Condition function or function object
  88. * @return Key pair where cond(kp) returns true
  89. * @tparam F Type of 'cond'
  90. */
  91. template <typename F> static inline Pair generateSatisfying(F cond)
  92. {
  93. Pair kp;
  94. void* const priv = (void*)kp.priv.data;
  95. Utils::getSecureRandom(priv, ZT_ECC_PRIVATE_KEY_SET_LEN);
  96. _calcPubED(kp); // do Ed25519 key -- bytes 32-63 of pub and priv
  97. do {
  98. ++(((uint64_t*)priv)[1]);
  99. --(((uint64_t*)priv)[2]);
  100. _calcPubDH(kp); // keep regenerating bytes 0-31 until satisfied
  101. } while (! cond(kp));
  102. return kp;
  103. }
  104. /**
  105. * Perform C25519 ECC key agreement
  106. *
  107. * Actual key bytes are generated from one or more SHA-512 digests of
  108. * the raw result of key agreement.
  109. *
  110. * @param mine My private key
  111. * @param their Their public key
  112. * @param keybuf Buffer to fill
  113. * @param keylen Number of key bytes to generate
  114. */
  115. static void agree(const Private& mine, const Public& their, void* keybuf, unsigned int keylen);
  116. static inline void agree(const Pair& mine, const Public& their, void* keybuf, unsigned int keylen)
  117. {
  118. agree(mine.priv, their, keybuf, keylen);
  119. }
  120. /**
  121. * Sign a message with a sender's key pair
  122. *
  123. * This takes the SHA-521 of msg[] and then signs the first 32 bytes of this
  124. * digest, returning it and the 64-byte ed25519 signature in signature[].
  125. * This results in a signature that verifies both the signer's authenticity
  126. * and the integrity of the message.
  127. *
  128. * This is based on the original ed25519 code from NaCl and the SUPERCOP
  129. * cipher benchmark suite, but with the modification that it always
  130. * produces a signature of fixed 96-byte length based on the hash of an
  131. * arbitrary-length message.
  132. *
  133. * @param myPrivate My private key
  134. * @param myPublic My public key
  135. * @param msg Message to sign
  136. * @param len Length of message in bytes
  137. * @param signature Buffer to fill with signature -- MUST be 96 bytes in length
  138. */
  139. static void sign(const Private& myPrivate, const Public& myPublic, const void* msg, unsigned int len, void* signature);
  140. static inline void sign(const Pair& mine, const void* msg, unsigned int len, void* signature)
  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. {
  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. {
  161. Signature sig;
  162. sign(mine.priv, mine.pub, msg, len, sig.data);
  163. return sig;
  164. }
  165. /**
  166. * Verify a message's signature
  167. *
  168. * @param their Public key to verify against
  169. * @param msg Message to verify signature integrity against
  170. * @param len Length of message in bytes
  171. * @param signature 96-byte signature
  172. * @return True if signature is valid and the message is authentic and unmodified
  173. */
  174. static bool verify(const Public& their, const void* msg, unsigned int len, const void* signature);
  175. /**
  176. * Verify a message's signature
  177. *
  178. * @param their Public key to verify against
  179. * @param msg Message to verify signature integrity against
  180. * @param len Length of message in bytes
  181. * @param signature 96-byte signature
  182. * @return True if signature is valid and the message is authentic and unmodified
  183. */
  184. static inline bool verify(const Public& their, const void* msg, unsigned int len, const Signature& signature)
  185. {
  186. return verify(their, msg, len, signature.data);
  187. }
  188. private:
  189. // derive first 32 bytes of kp.pub from first 32 bytes of kp.priv
  190. // this is the ECDH key
  191. static void _calcPubDH(Pair& kp);
  192. // derive 2nd 32 bytes of kp.pub from 2nd 32 bytes of kp.priv
  193. // this is the Ed25519 sign/verify key
  194. static void _calcPubED(Pair& kp);
  195. };
  196. } // namespace ZeroTier
  197. #endif
  198. #endif