C25519.hpp 4.6 KB

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