C25519.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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: 2023-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. #ifndef ZT_C25519_HPP
  14. #define ZT_C25519_HPP
  15. #include "Utils.hpp"
  16. namespace ZeroTier {
  17. #define ZT_C25519_PUBLIC_KEY_LEN 64
  18. #define ZT_C25519_PRIVATE_KEY_LEN 64
  19. #define ZT_C25519_SIGNATURE_LEN 96
  20. #define ZT_C25519_SHARED_KEY_LEN 32
  21. /**
  22. * A combined Curve25519 ECDH and Ed25519 signature engine
  23. */
  24. class C25519
  25. {
  26. public:
  27. /**
  28. * Generate a C25519 elliptic curve key pair
  29. */
  30. static inline void generate(uint8_t pub[ZT_C25519_PUBLIC_KEY_LEN],uint8_t priv[ZT_C25519_PRIVATE_KEY_LEN])
  31. {
  32. Utils::getSecureRandom(priv,ZT_C25519_PRIVATE_KEY_LEN);
  33. _calcPubDH(pub,priv);
  34. _calcPubED(pub,priv);
  35. }
  36. /**
  37. * Generate a key pair satisfying a condition
  38. *
  39. * This begins with a random keypair from a random secret key and then
  40. * iteratively increments the random secret until cond(kp) returns true.
  41. * This is used to compute key pairs in which the public key, its hash
  42. * or some other aspect of it satisfies some condition, such as for a
  43. * hashcash criteria.
  44. *
  45. * @param cond Condition function or function object
  46. * @return Key pair where cond(kp) returns true
  47. * @tparam F Type of 'cond'
  48. */
  49. template<typename F>
  50. static inline void generateSatisfying(F cond,uint8_t pub[ZT_C25519_PUBLIC_KEY_LEN],uint8_t priv[ZT_C25519_PRIVATE_KEY_LEN])
  51. {
  52. Utils::getSecureRandom(priv,ZT_C25519_PRIVATE_KEY_LEN);
  53. _calcPubED(pub,priv); // do Ed25519 key -- bytes 32-63 of pub and priv
  54. do {
  55. ++(((uint64_t *)priv)[1]);
  56. --(((uint64_t *)priv)[2]);
  57. _calcPubDH(pub,priv); // keep regenerating bytes 0-31 until satisfied
  58. } while (!cond(pub));
  59. }
  60. /**
  61. * Perform C25519 ECC key agreement
  62. *
  63. * Actual key bytes are generated from one or more SHA-512 digests of
  64. * the raw result of key agreement.
  65. *
  66. * @param mine My private key
  67. * @param their Their public key
  68. * @param rawkey Buffer to receive raw (not hashed) agreed upon key
  69. */
  70. 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]);
  71. /**
  72. * Sign a message with a sender's key pair
  73. *
  74. * This takes the SHA-521 of msg[] and then signs the first 32 bytes of this
  75. * digest, returning it and the 64-byte ed25519 signature in signature[].
  76. * This results in a signature that verifies both the signer's authenticity
  77. * and the integrity of the message.
  78. *
  79. * This is based on the original ed25519 code from NaCl and the SUPERCOP
  80. * cipher benchmark suite, but with the modification that it always
  81. * produces a signature of fixed 96-byte length based on the hash of an
  82. * arbitrary-length message.
  83. *
  84. * @param myPrivate My private key
  85. * @param myPublic My public key
  86. * @param msg Message to sign
  87. * @param len Length of message in bytes
  88. * @param signature Buffer to fill with signature -- MUST be 96 bytes in length
  89. */
  90. 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);
  91. /**
  92. * Verify a message's signature
  93. *
  94. * @param their Public key to verify against
  95. * @param msg Message to verify signature integrity against
  96. * @param len Length of message in bytes
  97. * @param signature Signature bytes
  98. * @param siglen Length of signature in bytes
  99. * @return True if signature is valid and the message is authentic and unmodified
  100. */
  101. 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);
  102. private:
  103. // derive first 32 bytes of kp.pub from first 32 bytes of kp.priv
  104. // this is the ECDH key
  105. static void _calcPubDH(uint8_t pub[ZT_C25519_PUBLIC_KEY_LEN],const uint8_t priv[ZT_C25519_PRIVATE_KEY_LEN]);
  106. // derive 2nd 32 bytes of kp.pub from 2nd 32 bytes of kp.priv
  107. // this is the Ed25519 sign/verify key
  108. static void _calcPubED(uint8_t pub[ZT_C25519_PUBLIC_KEY_LEN],const uint8_t priv[ZT_C25519_PRIVATE_KEY_LEN]);
  109. };
  110. } // namespace ZeroTier
  111. #endif