C25519.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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_ALWAYS_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[32]);
  69. /**
  70. * Sign a message with a sender's key pair
  71. *
  72. * This takes the SHA-521 of msg[] and then signs the first 32 bytes of this
  73. * digest, returning it and the 64-byte ed25519 signature in signature[].
  74. * This results in a signature that verifies both the signer's authenticity
  75. * and the integrity of the message.
  76. *
  77. * This is based on the original ed25519 code from NaCl and the SUPERCOP
  78. * cipher benchmark suite, but with the modification that it always
  79. * produces a signature of fixed 96-byte length based on the hash of an
  80. * arbitrary-length message.
  81. *
  82. * @param myPrivate My private key
  83. * @param myPublic My public key
  84. * @param msg Message to sign
  85. * @param len Length of message in bytes
  86. * @param signature Buffer to fill with signature -- MUST be 96 bytes in length
  87. */
  88. 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);
  89. /**
  90. * Verify a message's signature
  91. *
  92. * @param their Public key to verify against
  93. * @param msg Message to verify signature integrity against
  94. * @param len Length of message in bytes
  95. * @param signature Signature bytes
  96. * @param siglen Length of signature in bytes
  97. * @return True if signature is valid and the message is authentic and unmodified
  98. */
  99. 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);
  100. private:
  101. // derive first 32 bytes of kp.pub from first 32 bytes of kp.priv
  102. // this is the ECDH key
  103. static void _calcPubDH(uint8_t pub[ZT_C25519_PUBLIC_KEY_LEN],const uint8_t priv[ZT_C25519_PRIVATE_KEY_LEN]);
  104. // derive 2nd 32 bytes of kp.pub from 2nd 32 bytes of kp.priv
  105. // this is the Ed25519 sign/verify key
  106. static void _calcPubED(uint8_t pub[ZT_C25519_PUBLIC_KEY_LEN],const uint8_t priv[ZT_C25519_PRIVATE_KEY_LEN]);
  107. };
  108. } // namespace ZeroTier
  109. #endif