C25519.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (c)2013-2020 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: 2025-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. * The code in C25519.cpp is in the public domain rather than being under
  15. * ZeroTier's license. Other than ZeroTier shims it contains public domain
  16. * C25519/Ed25519 code by D. J. Bernstein and Matthew Dempsky.
  17. */
  18. #ifndef ZT_C25519_HPP
  19. #define ZT_C25519_HPP
  20. #include "Constants.hpp"
  21. #include "Utils.hpp"
  22. namespace ZeroTier {
  23. #define ZT_C25519_ECDH_PUBLIC_KEY_SIZE 32
  24. #define ZT_C25519_ECDH_PRIVATE_KEY_SIZE 32
  25. #define ZT_C25519_COMBINED_PUBLIC_KEY_SIZE 64
  26. #define ZT_C25519_COMBINED_PRIVATE_KEY_SIZE 64
  27. #define ZT_C25519_SIGNATURE_LEN 96
  28. #define ZT_C25519_ECDH_SHARED_SECRET_SIZE 32
  29. /**
  30. * A combined Curve25519 ECDH and Ed25519 signature engine
  31. */
  32. class C25519
  33. {
  34. public:
  35. /**
  36. * Generate a set of two 25519 keys: a C25519 ECDH key pair and an Ed25519 EDDSA key pair.
  37. */
  38. static void generateCombined(uint8_t pub[ZT_C25519_COMBINED_PUBLIC_KEY_SIZE],uint8_t priv[ZT_C25519_COMBINED_PRIVATE_KEY_SIZE]);
  39. /**
  40. * Generate a C25519 ECDH key pair only.
  41. */
  42. static void generateC25519(uint8_t pub[ZT_C25519_ECDH_PUBLIC_KEY_SIZE],uint8_t priv[ZT_C25519_ECDH_PRIVATE_KEY_SIZE]);
  43. /**
  44. * Generate a key pair satisfying a condition
  45. *
  46. * This begins with a random keypair from a random secret key and then
  47. * iteratively increments the random secret until cond(kp) returns true.
  48. * This is used to compute key pairs in which the public key, its hash
  49. * or some other aspect of it satisfies some condition, such as for a
  50. * hashcash criteria.
  51. *
  52. * @param cond Condition function or function object
  53. * @return Key pair where cond(kp) returns true
  54. * @tparam F Type of 'cond'
  55. */
  56. template<typename F>
  57. static ZT_INLINE void generateSatisfying(F cond,uint8_t pub[ZT_C25519_COMBINED_PUBLIC_KEY_SIZE],uint8_t priv[ZT_C25519_COMBINED_PRIVATE_KEY_SIZE])
  58. {
  59. Utils::getSecureRandom(priv,ZT_C25519_COMBINED_PRIVATE_KEY_SIZE);
  60. s_calcPubED(pub, priv); // do Ed25519 key -- bytes 32-63 of pub and priv
  61. do {
  62. ++(((uint64_t *)priv)[1]);
  63. --(((uint64_t *)priv)[2]);
  64. s_calcPubDH(pub, priv); // keep regenerating bytes 0-31 until satisfied
  65. } while (!cond(pub));
  66. }
  67. /**
  68. * Perform C25519 ECC key agreement
  69. *
  70. * Actual key bytes are generated from one or more SHA-512 digests of
  71. * the raw result of key agreement.
  72. *
  73. * @param mine My private key
  74. * @param their Their public key
  75. * @param rawkey Buffer to receive raw (not hashed) agreed upon key
  76. */
  77. static void agree(const uint8_t mine[ZT_C25519_ECDH_PRIVATE_KEY_SIZE],const uint8_t their[ZT_C25519_ECDH_PUBLIC_KEY_SIZE],uint8_t rawkey[ZT_C25519_ECDH_SHARED_SECRET_SIZE]);
  78. /**
  79. * Sign a message with a sender's key pair
  80. *
  81. * LEGACY: ZeroTier's ed25519 signatures contain an extra 32 bytes which are the first
  82. * 32 bytes of SHA512(msg). These exist because an early version of the ZeroTier multicast
  83. * algorithm did a lot of signature verification and we wanted a way to skip the more
  84. * expensive ed25519 verification if the signature was obviously wrong.
  85. *
  86. * This verify() function will accept a 64 or 96 bit signature, checking the last 32
  87. * bytes only if present.
  88. *
  89. * @param myPrivate My private key
  90. * @param myPublic My public key
  91. * @param msg Message to sign
  92. * @param len Length of message in bytes
  93. * @param signature Buffer to fill with signature -- MUST be 96 bytes in length
  94. */
  95. static void sign(const uint8_t myPrivate[ZT_C25519_COMBINED_PRIVATE_KEY_SIZE],const uint8_t myPublic[ZT_C25519_COMBINED_PUBLIC_KEY_SIZE],const void *msg,unsigned int len,void *signature);
  96. /**
  97. * Verify a message's signature
  98. *
  99. * @param their Public key to verify against
  100. * @param msg Message to verify signature integrity against
  101. * @param len Length of message in bytes
  102. * @param signature Signature bytes
  103. * @param siglen Length of signature in bytes
  104. * @return True if signature is valid and the message is authentic and unmodified
  105. */
  106. static bool verify(const uint8_t their[ZT_C25519_COMBINED_PUBLIC_KEY_SIZE],const void *msg,unsigned int len,const void *signature,unsigned int siglen);
  107. private:
  108. // derive first 32 bytes of kp.pub from first 32 bytes of kp.priv
  109. // this is the ECDH key
  110. static void s_calcPubDH(uint8_t *pub, const uint8_t *priv);
  111. // derive 2nd 32 bytes of kp.pub from 2nd 32 bytes of kp.priv
  112. // this is the Ed25519 sign/verify key
  113. static void s_calcPubED(uint8_t *pub, const uint8_t *priv);
  114. };
  115. } // namespace ZeroTier
  116. #endif