C25519.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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_PUBLIC_KEY_LEN 64
  21. #define ZT_C25519_PRIVATE_KEY_LEN 64
  22. #define ZT_C25519_SIGNATURE_LEN 96
  23. #define ZT_C25519_SHARED_KEY_LEN 32
  24. /**
  25. * A combined Curve25519 ECDH and Ed25519 signature engine
  26. */
  27. class C25519
  28. {
  29. public:
  30. /**
  31. * Generate a C25519 elliptic curve key pair
  32. */
  33. static void generate(uint8_t pub[ZT_C25519_PUBLIC_KEY_LEN],uint8_t priv[ZT_C25519_PRIVATE_KEY_LEN]);
  34. /**
  35. * Generate a key pair satisfying a condition
  36. *
  37. * This begins with a random keypair from a random secret key and then
  38. * iteratively increments the random secret until cond(kp) returns true.
  39. * This is used to compute key pairs in which the public key, its hash
  40. * or some other aspect of it satisfies some condition, such as for a
  41. * hashcash criteria.
  42. *
  43. * @param cond Condition function or function object
  44. * @return Key pair where cond(kp) returns true
  45. * @tparam F Type of 'cond'
  46. */
  47. template<typename F>
  48. static ZT_INLINE void generateSatisfying(F cond,uint8_t pub[ZT_C25519_PUBLIC_KEY_LEN],uint8_t priv[ZT_C25519_PRIVATE_KEY_LEN])
  49. {
  50. Utils::getSecureRandom(priv,ZT_C25519_PRIVATE_KEY_LEN);
  51. _calcPubED(pub,priv); // do Ed25519 key -- bytes 32-63 of pub and priv
  52. do {
  53. ++(((uint64_t *)priv)[1]);
  54. --(((uint64_t *)priv)[2]);
  55. _calcPubDH(pub,priv); // keep regenerating bytes 0-31 until satisfied
  56. } while (!cond(pub));
  57. }
  58. /**
  59. * Perform C25519 ECC key agreement
  60. *
  61. * Actual key bytes are generated from one or more SHA-512 digests of
  62. * the raw result of key agreement.
  63. *
  64. * @param mine My private key
  65. * @param their Their public key
  66. * @param rawkey Buffer to receive raw (not hashed) agreed upon key
  67. */
  68. static void agree(const uint8_t mine[ZT_C25519_PRIVATE_KEY_LEN],const uint8_t their[ZT_C25519_PUBLIC_KEY_LEN],uint8_t rawkey[ZT_C25519_SHARED_KEY_LEN]);
  69. /**
  70. * Sign a message with a sender's key pair
  71. *
  72. * For legacy reasons ZeroTier ed25519 signatures end with an additional 32 bytes
  73. * that are the first 32 bytes of SHA512(msg). The verify() function considers these
  74. * bytes optional and will accept signatures of 64 or 96 bytes in length, checking
  75. * the hash bytes if they are present.
  76. *
  77. * @param myPrivate My private key
  78. * @param myPublic My public key
  79. * @param msg Message to sign
  80. * @param len Length of message in bytes
  81. * @param signature Buffer to fill with signature -- MUST be 96 bytes in length
  82. */
  83. static void sign(const uint8_t myPrivate[ZT_C25519_PRIVATE_KEY_LEN],const uint8_t myPublic[ZT_C25519_PUBLIC_KEY_LEN],const void *msg,unsigned int len,void *signature);
  84. /**
  85. * Verify a message's signature
  86. *
  87. * @param their Public key to verify against
  88. * @param msg Message to verify signature integrity against
  89. * @param len Length of message in bytes
  90. * @param signature Signature bytes
  91. * @param siglen Length of signature in bytes
  92. * @return True if signature is valid and the message is authentic and unmodified
  93. */
  94. static bool verify(const uint8_t their[ZT_C25519_PUBLIC_KEY_LEN],const void *msg,unsigned int len,const void *signature,const unsigned int siglen);
  95. private:
  96. // derive first 32 bytes of kp.pub from first 32 bytes of kp.priv
  97. // this is the ECDH key
  98. static void _calcPubDH(uint8_t pub[ZT_C25519_PUBLIC_KEY_LEN],const uint8_t priv[ZT_C25519_PRIVATE_KEY_LEN]);
  99. // derive 2nd 32 bytes of kp.pub from 2nd 32 bytes of kp.priv
  100. // this is the Ed25519 sign/verify key
  101. static void _calcPubED(uint8_t pub[ZT_C25519_PUBLIC_KEY_LEN],const uint8_t priv[ZT_C25519_PRIVATE_KEY_LEN]);
  102. };
  103. } // namespace ZeroTier
  104. #endif