EphemeralKey.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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: 2025-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_EPHEMERALKEY_HPP
  14. #define ZT_EPHEMERALKEY_HPP
  15. #include "Constants.hpp"
  16. #include "C25519.hpp"
  17. #include "ECC384.hpp"
  18. #include "SHA512.hpp"
  19. #include "Utils.hpp"
  20. #define ZT_EPHEMERALKEY_PUBLIC_SIZE (1 + ZT_C25519_ECDH_PUBLIC_KEY_SIZE + ZT_ECC384_PUBLIC_KEY_SIZE)
  21. namespace ZeroTier {
  22. /**
  23. * Container for ephemeral key pair sets used in forward secrecy negotiation.
  24. *
  25. * The ephemeral public key consists of public key(s) prefixed by a type byte.
  26. * In the current version there are two keys: a Curve25519 ECDH public key and
  27. * a NIST P-384 public key. Both are sent, and key agreement is performed by
  28. * agreeing with both and then hashing the results together with the long-lived
  29. * identity shared secret to produce a shared symmetric ephemeral key.
  30. *
  31. * Unlike identities the private key never leaves this class. It dies when
  32. * a new key pair is generated or when the node is shut down.
  33. *
  34. * Each peer holds a copy of its current ephemeral key. This is re-generated
  35. * after one half ZT_SYMMETRIC_KEY_TTL or after the the symmetric key has
  36. * been used one half of ZT_SYMMETRIC_KEY_TTL_MESSAGES times. Half the total
  37. * TTL is chosen to provide plenty of margin.
  38. */
  39. class EphemeralKey
  40. {
  41. public:
  42. enum Type
  43. {
  44. TYPE_NIL = 0,
  45. TYPE_C25519_P384 = 1
  46. };
  47. /**
  48. * The ephemeral public key(s)
  49. *
  50. * This is sent with HELLO or OK(HELLO) and is re-written when
  51. * generate() is called. Its size is static.
  52. */
  53. const uint8_t pub[ZT_EPHEMERALKEY_PUBLIC_SIZE];
  54. /**
  55. * Create an uninitialized ephemeral key (must call generate())
  56. */
  57. ZT_INLINE EphemeralKey() noexcept:
  58. pub()
  59. {
  60. const_cast<uint8_t *>(pub)[0] = (uint8_t) TYPE_NIL;
  61. Utils::memoryLock(this, sizeof(EphemeralKey));
  62. }
  63. ZT_INLINE ~EphemeralKey() noexcept
  64. {
  65. Utils::burn(m_priv, sizeof(m_priv));
  66. Utils::memoryUnlock(this, sizeof(EphemeralKey));
  67. }
  68. /**
  69. * @return True if this ephemeral key has been initialized with generate()
  70. */
  71. ZT_INLINE operator bool() const noexcept
  72. { return pub[0] != (uint8_t) TYPE_NIL; }
  73. /**
  74. * Generate or re-generate key pair.
  75. */
  76. ZT_INLINE void generate() noexcept
  77. {
  78. uint8_t *const p = const_cast<uint8_t *>(pub);
  79. p[0] = (uint8_t) TYPE_C25519_P384;
  80. C25519::generateC25519(p + 1, m_priv);
  81. ECC384GenerateKey(p + 1 + ZT_C25519_ECDH_PUBLIC_KEY_SIZE, m_priv + ZT_C25519_ECDH_PRIVATE_KEY_SIZE);
  82. }
  83. /**
  84. * Execute key agreement with another ephemeral public key set.
  85. *
  86. * Final key is produced by hashing the two ECDH keys followed by
  87. * the identity secret key with SHA384.
  88. *
  89. * @param identityKey Raw identity key shared between this node and peer
  90. * @param otherPub Other public key (prefixed by type)
  91. * @param key Key buffer to fill with symmetric key
  92. * @return True on success
  93. */
  94. ZT_INLINE bool agree(const uint8_t identityKey[ZT_SYMMETRIC_KEY_SIZE], const uint8_t *otherPub, const unsigned int otherPubLength, uint8_t key[ZT_SYMMETRIC_KEY_SIZE]) const noexcept
  95. {
  96. if ((otherPubLength < ZT_EPHEMERALKEY_PUBLIC_SIZE) || (otherPub[0] != (uint8_t) TYPE_C25519_P384))
  97. return false;
  98. uint8_t tmp[ZT_C25519_ECDH_SHARED_SECRET_SIZE + ZT_ECC384_SHARED_SECRET_SIZE];
  99. C25519::agree(m_priv, otherPub + 1, tmp);
  100. if (!ECC384ECDH(otherPub + 1 + ZT_C25519_ECDH_PUBLIC_KEY_SIZE, m_priv + ZT_C25519_ECDH_PRIVATE_KEY_SIZE, tmp + ZT_C25519_ECDH_SHARED_SECRET_SIZE))
  101. return false;
  102. SHA384(key, tmp, ZT_C25519_ECDH_SHARED_SECRET_SIZE + ZT_ECC384_SHARED_SECRET_SIZE, identityKey, ZT_SYMMETRIC_KEY_SIZE);
  103. Utils::burn(tmp, ZT_C25519_ECDH_SHARED_SECRET_SIZE + ZT_ECC384_SHARED_SECRET_SIZE);
  104. return true;
  105. }
  106. /**
  107. * Check and see if an acknowledgement hash returned via OK(HELLO) matches our public key.
  108. *
  109. * @param ackHash Hash provided in OK(HELLO)
  110. * @return True if this matches the hash of this ephemeral key
  111. */
  112. ZT_INLINE bool acknowledged(const uint8_t ackHash[ZT_SHA384_DIGEST_SIZE]) const noexcept
  113. {
  114. uint8_t h[ZT_SHA384_DIGEST_SIZE];
  115. SHA384(h, pub, ZT_EPHEMERALKEY_PUBLIC_SIZE);
  116. return Utils::secureEq(ackHash, h, ZT_SHA384_DIGEST_SIZE);
  117. }
  118. private:
  119. uint8_t m_priv[ZT_C25519_ECDH_PRIVATE_KEY_SIZE + ZT_ECC384_PRIVATE_KEY_SIZE];
  120. };
  121. } // namespace ZeroTier
  122. #endif