ECC.hpp 6.7 KB

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