EphemeralKey.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #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 = ZT_CRYPTO_ALG_P384
  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 { return pub[0] != (uint8_t)TYPE_NIL; }
  72. /**
  73. * Generate or re-generate key pair.
  74. */
  75. ZT_INLINE void generate() noexcept
  76. {
  77. uint8_t *const p = const_cast<uint8_t *>(pub);
  78. p[0] = (uint8_t)TYPE_C25519_P384;
  79. C25519::generateC25519(p + 1,m_priv);
  80. ECC384GenerateKey(p + 1 + ZT_C25519_ECDH_PUBLIC_KEY_SIZE,m_priv + ZT_C25519_ECDH_PRIVATE_KEY_SIZE);
  81. }
  82. /**
  83. * Execute key agreement with another ephemeral public key set.
  84. *
  85. * Final key is produced by hashing the two ECDH keys followed by
  86. * the identity secret key with SHA384.
  87. *
  88. * @param identityKey Raw identity key shared between this node and peer
  89. * @param otherPub Other public key (prefixed by type)
  90. * @param key Key buffer to fill with symmetric key
  91. * @return True on success
  92. */
  93. 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
  94. {
  95. if ((otherPubLength < ZT_EPHEMERALKEY_PUBLIC_SIZE)||(otherPub[0] != (uint8_t)TYPE_C25519_P384))
  96. return false;
  97. uint8_t tmp[ZT_C25519_ECDH_SHARED_SECRET_SIZE + ZT_ECC384_SHARED_SECRET_SIZE];
  98. C25519::agree(m_priv,otherPub + 1,tmp);
  99. 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))
  100. return false;
  101. SHA384(key,tmp,ZT_C25519_ECDH_SHARED_SECRET_SIZE + ZT_ECC384_SHARED_SECRET_SIZE,identityKey,ZT_SYMMETRIC_KEY_SIZE);
  102. Utils::burn(tmp,ZT_C25519_ECDH_SHARED_SECRET_SIZE + ZT_ECC384_SHARED_SECRET_SIZE);
  103. return true;
  104. }
  105. /**
  106. * Check and see if an acknowledgement hash returned via OK(HELLO) matches our public key.
  107. *
  108. * @param ackHash Hash provided in OK(HELLO)
  109. * @return True if this matches the hash of this ephemeral key
  110. */
  111. ZT_INLINE bool acknowledged(const uint8_t ackHash[ZT_SHA384_DIGEST_SIZE]) const noexcept
  112. {
  113. uint8_t h[ZT_SHA384_DIGEST_SIZE];
  114. SHA384(h,pub,ZT_EPHEMERALKEY_PUBLIC_SIZE);
  115. return Utils::secureEq(ackHash,h,ZT_SHA384_DIGEST_SIZE);
  116. }
  117. private:
  118. uint8_t m_priv[ZT_C25519_ECDH_PRIVATE_KEY_SIZE + ZT_ECC384_PRIVATE_KEY_SIZE];
  119. };
  120. } // namespace ZeroTier
  121. #endif