Protocol.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #include "Protocol.hpp"
  14. #include "Buf.hpp"
  15. #include "Utils.hpp"
  16. #include <cstdlib>
  17. #include <stdexcept>
  18. #if defined(__GCC__) && (defined(__amd64) || defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || defined(__AMD64) || defined(__AMD64__) || defined(_M_X64))
  19. #define ZT_PACKET_USE_ATOMIC_INTRINSICS
  20. #endif
  21. #ifndef ZT_PACKET_USE_ATOMIC_INTRINSICS
  22. #include <atomic>
  23. #endif
  24. namespace ZeroTier {
  25. namespace Protocol {
  26. namespace {
  27. unsigned long long _initPacketID()
  28. {
  29. unsigned long long tmp = 0;
  30. Utils::getSecureRandom(&tmp,sizeof(tmp));
  31. tmp >>= 31U;
  32. tmp |= (((uint64_t)time(nullptr)) & 0xffffffffULL) << 33U;
  33. return tmp;
  34. }
  35. #ifdef ZT_PACKET_USE_ATOMIC_INTRINSICS
  36. unsigned long long _packetIdCtr = _initPacketID();
  37. #else
  38. static std::atomic<unsigned long long> _packetIdCtr(_initPacketID());
  39. #endif
  40. uintptr_t _checkSizes()
  41. {
  42. // These are compiled time checked assertions that make sure our platform/compiler is sane
  43. // and that packed structures are working properly.
  44. if (ZT_PROTO_MAX_PACKET_LENGTH > ZT_BUF_MEM_SIZE)
  45. throw std::runtime_error("ZT_PROTO_MAX_PACKET_LENGTH > ZT_BUF_MEM_SIZE");
  46. if (sizeof(Header) != ZT_PROTO_MIN_PACKET_LENGTH)
  47. throw std::runtime_error("sizeof(Header) != ZT_PROTO_MIN_PACKET_LENGTH");
  48. if (sizeof(FragmentHeader) != ZT_PROTO_MIN_FRAGMENT_LENGTH)
  49. throw std::runtime_error("sizeof(FragmentHeader) != ZT_PROTO_MIN_FRAGMENT_LENGTH");
  50. return (uintptr_t)Utils::getSecureRandomU64(); // also prevents compiler from optimizing out
  51. }
  52. } // anonymous namespace
  53. // Make compiler compile and "run" _checkSizes()
  54. volatile uintptr_t _checkSizesIMeanIt = _checkSizes();
  55. uint64_t createProbe(const Identity &sender,const Identity &recipient,const uint8_t key[ZT_PEER_SECRET_KEY_LENGTH]) noexcept
  56. {
  57. uint8_t tmp[ZT_IDENTITY_HASH_SIZE + ZT_IDENTITY_HASH_SIZE];
  58. memcpy(tmp,sender.hash().data(),ZT_IDENTITY_HASH_SIZE);
  59. memcpy(tmp + ZT_IDENTITY_HASH_SIZE,recipient.hash().data(),ZT_IDENTITY_HASH_SIZE);
  60. uint64_t hash[6];
  61. SHA384(hash,tmp,sizeof(tmp),key,ZT_PEER_SECRET_KEY_LENGTH);
  62. return hash[0];
  63. }
  64. uint64_t getPacketId() noexcept
  65. {
  66. #ifdef ZT_PACKET_USE_ATOMIC_INTRINSICS
  67. return __sync_add_and_fetch(&_packetIdCtr,1ULL);
  68. #else
  69. return ++_packetIdCtr;
  70. #endif
  71. }
  72. void armor(Buf &pkt,int packetSize,const uint8_t key[ZT_PEER_SECRET_KEY_LENGTH],uint8_t cipherSuite) noexcept
  73. {
  74. Protocol::Header &ph = pkt.as<Protocol::Header>();
  75. ph.flags = (ph.flags & 0xc7U) | ((cipherSuite << 3U) & 0x38U); // flags: FFCCCHHH where CCC is cipher
  76. switch(cipherSuite) {
  77. case ZT_PROTO_CIPHER_SUITE__POLY1305_NONE: {
  78. uint8_t perPacketKey[ZT_PEER_SECRET_KEY_LENGTH];
  79. salsa2012DeriveKey(key,perPacketKey,pkt,packetSize);
  80. Salsa20 s20(perPacketKey,&ph.packetId);
  81. uint8_t macKey[ZT_POLY1305_KEY_LEN];
  82. s20.crypt12(Utils::ZERO256,macKey,ZT_POLY1305_KEY_LEN);
  83. // only difference here is that we don't encrypt the payload
  84. uint64_t mac[2];
  85. poly1305(mac,pkt.unsafeData + ZT_PROTO_PACKET_ENCRYPTED_SECTION_START,packetSize - ZT_PROTO_PACKET_ENCRYPTED_SECTION_START,macKey);
  86. ph.mac = mac[0];
  87. } break;
  88. case ZT_PROTO_CIPHER_SUITE__POLY1305_SALSA2012: {
  89. uint8_t perPacketKey[ZT_PEER_SECRET_KEY_LENGTH];
  90. salsa2012DeriveKey(key,perPacketKey,pkt,packetSize);
  91. Salsa20 s20(perPacketKey,&ph.packetId);
  92. uint8_t macKey[ZT_POLY1305_KEY_LEN];
  93. s20.crypt12(Utils::ZERO256,macKey,ZT_POLY1305_KEY_LEN);
  94. const unsigned int encLen = packetSize - ZT_PROTO_PACKET_ENCRYPTED_SECTION_START;
  95. s20.crypt12(pkt.unsafeData + ZT_PROTO_PACKET_ENCRYPTED_SECTION_START,pkt.unsafeData + ZT_PROTO_PACKET_ENCRYPTED_SECTION_START,encLen);
  96. uint64_t mac[2];
  97. poly1305(mac,pkt.unsafeData + ZT_PROTO_PACKET_ENCRYPTED_SECTION_START,encLen,macKey);
  98. ph.mac = mac[0];
  99. } break;
  100. case ZT_PROTO_CIPHER_SUITE__AES_GCM_NRH: {
  101. } break;
  102. }
  103. }
  104. int compress(SharedPtr<Buf> &pkt,int packetSize) noexcept
  105. {
  106. if (packetSize <= 128)
  107. return packetSize;
  108. SharedPtr<Buf> pkt2(new Buf());
  109. if (!pkt2) return packetSize;
  110. const int uncompressedLen = packetSize - ZT_PROTO_PACKET_PAYLOAD_START;
  111. const int compressedLen = LZ4_compress_fast(reinterpret_cast<const char *>(pkt->unsafeData + ZT_PROTO_PACKET_PAYLOAD_START),reinterpret_cast<char *>(pkt2->unsafeData + ZT_PROTO_PACKET_PAYLOAD_START),uncompressedLen,ZT_BUF_MEM_SIZE - ZT_PROTO_PACKET_PAYLOAD_START);
  112. if ((compressedLen > 0)&&(compressedLen < uncompressedLen)) {
  113. memcpy(pkt2->unsafeData,pkt->unsafeData,ZT_PROTO_PACKET_PAYLOAD_START);
  114. pkt.swap(pkt2);
  115. pkt->as<Protocol::Header>().verb |= ZT_PROTO_VERB_FLAG_COMPRESSED;
  116. return compressedLen + ZT_PROTO_PACKET_PAYLOAD_START;
  117. }
  118. return packetSize;
  119. }
  120. } // namespace Protocol
  121. } // namespace ZeroTier