C25519.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_C25519_HPP
  27. #define ZT_C25519_HPP
  28. #include "Utils.hpp"
  29. namespace ZeroTier {
  30. #define ZT_C25519_PUBLIC_KEY_LEN 64
  31. #define ZT_C25519_PRIVATE_KEY_LEN 64
  32. #define ZT_C25519_SIGNATURE_LEN 96
  33. #define ZT_C25519_SHARED_KEY_LEN 32
  34. /**
  35. * A combined Curve25519 ECDH and Ed25519 signature engine
  36. */
  37. class C25519
  38. {
  39. public:
  40. /**
  41. * Generate a C25519 elliptic curve key pair
  42. */
  43. static inline void generate(uint8_t pub[ZT_C25519_PUBLIC_KEY_LEN],uint8_t priv[ZT_C25519_PRIVATE_KEY_LEN])
  44. {
  45. Utils::getSecureRandom(priv,ZT_C25519_PRIVATE_KEY_LEN);
  46. _calcPubDH(pub,priv);
  47. _calcPubED(pub,priv);
  48. }
  49. /**
  50. * Generate a key pair satisfying a condition
  51. *
  52. * This begins with a random keypair from a random secret key and then
  53. * iteratively increments the random secret until cond(kp) returns true.
  54. * This is used to compute key pairs in which the public key, its hash
  55. * or some other aspect of it satisfies some condition, such as for a
  56. * hashcash criteria.
  57. *
  58. * @param cond Condition function or function object
  59. * @return Key pair where cond(kp) returns true
  60. * @tparam F Type of 'cond'
  61. */
  62. template<typename F>
  63. static inline void generateSatisfying(F cond,uint8_t pub[ZT_C25519_PUBLIC_KEY_LEN],uint8_t priv[ZT_C25519_PRIVATE_KEY_LEN])
  64. {
  65. Utils::getSecureRandom(priv,ZT_C25519_PRIVATE_KEY_LEN);
  66. _calcPubED(pub,priv); // do Ed25519 key -- bytes 32-63 of pub and priv
  67. do {
  68. ++(((uint64_t *)priv)[1]);
  69. --(((uint64_t *)priv)[2]);
  70. _calcPubDH(pub,priv); // keep regenerating bytes 0-31 until satisfied
  71. } while (!cond(pub));
  72. }
  73. /**
  74. * Perform C25519 ECC key agreement
  75. *
  76. * Actual key bytes are generated from one or more SHA-512 digests of
  77. * the raw result of key agreement.
  78. *
  79. * @param mine My private key
  80. * @param their Their public key
  81. * @param rawkey Buffer to receive raw (not hashed) agreed upon key
  82. */
  83. 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]);
  84. /**
  85. * Sign a message with a sender's key pair
  86. *
  87. * This takes the SHA-521 of msg[] and then signs the first 32 bytes of this
  88. * digest, returning it and the 64-byte ed25519 signature in signature[].
  89. * This results in a signature that verifies both the signer's authenticity
  90. * and the integrity of the message.
  91. *
  92. * This is based on the original ed25519 code from NaCl and the SUPERCOP
  93. * cipher benchmark suite, but with the modification that it always
  94. * produces a signature of fixed 96-byte length based on the hash of an
  95. * arbitrary-length message.
  96. *
  97. * @param myPrivate My private key
  98. * @param myPublic My public key
  99. * @param msg Message to sign
  100. * @param len Length of message in bytes
  101. * @param signature Buffer to fill with signature -- MUST be 96 bytes in length
  102. */
  103. 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);
  104. /**
  105. * Verify a message's signature
  106. *
  107. * @param their Public key to verify against
  108. * @param msg Message to verify signature integrity against
  109. * @param len Length of message in bytes
  110. * @param signature Signature bytes
  111. * @param siglen Length of signature in bytes
  112. * @return True if signature is valid and the message is authentic and unmodified
  113. */
  114. 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);
  115. private:
  116. // derive first 32 bytes of kp.pub from first 32 bytes of kp.priv
  117. // this is the ECDH key
  118. static void _calcPubDH(uint8_t pub[ZT_C25519_PUBLIC_KEY_LEN],const uint8_t priv[ZT_C25519_PRIVATE_KEY_LEN]);
  119. // derive 2nd 32 bytes of kp.pub from 2nd 32 bytes of kp.priv
  120. // this is the Ed25519 sign/verify key
  121. static void _calcPubED(uint8_t pub[ZT_C25519_PUBLIC_KEY_LEN],const uint8_t priv[ZT_C25519_PRIVATE_KEY_LEN]);
  122. };
  123. } // namespace ZeroTier
  124. #endif