Protocol.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 _checkStructureSizing()
  41. {
  42. if (sizeof(Header) != ZT_PROTO_MIN_PACKET_LENGTH)
  43. throw std::runtime_error("sizeof(Header) != ZT_PROTO_MIN_PACKET_LENGTH");
  44. if (sizeof(FragmentHeader) != ZT_PROTO_MIN_FRAGMENT_LENGTH)
  45. throw std::runtime_error("sizeof(FragmentHeader) != ZT_PROTO_MIN_FRAGMENT_LENGTH");
  46. return (uintptr_t)Utils::getSecureRandomU64(); // also prevents compiler from optimizing out
  47. }
  48. } // anonymous namespace
  49. volatile uintptr_t _compileTimeStructCheckHappened = _checkStructureSizing();
  50. uint64_t getPacketId()
  51. {
  52. #ifdef ZT_PACKET_USE_ATOMIC_INTRINSICS
  53. return __sync_add_and_fetch(&_packetIdCtr,1ULL);
  54. #else
  55. return ++_packetIdCtr;
  56. #endif
  57. }
  58. #if 0
  59. void _armor(Buf< Header > &packet,const unsigned int packetSize,const uint8_t key[ZT_PEER_SECRET_KEY_LENGTH],const uint8_t cipherSuite)
  60. {
  61. packet.data.fields.flags = (packet.data.fields.flags & 0xc7U) | ((cipherSuite << 3U) & 0x38U); // FFCCCHHH
  62. if (cipherSuite == ZT_PROTO_CIPHER_SUITE__AES_GCM) {
  63. // TODO
  64. } else if (cipherSuite != ZT_PROTO_CIPHER_SUITE__NONE) {
  65. uint8_t mangledKey[ZT_PEER_SECRET_KEY_LENGTH],macKey[ZT_POLY1305_KEY_LEN];
  66. uint64_t mac[2];
  67. _salsa20MangleKey(key,mangledKey,packet,packetSize);
  68. Salsa20 s20(mangledKey,&(packet.data.fields.packetId));
  69. s20.crypt12(ZEROES32,macKey,sizeof(macKey));
  70. uint8_t *payload = packet.data.bytes + ZT_PROTO_PACKET_ENCRYPTED_SECTION_START;
  71. const unsigned int payloadLen = packetSize - ZT_PROTO_PACKET_ENCRYPTED_SECTION_START;
  72. if (cipherSuite == ZT_PROTO_CIPHER_SUITE__POLY1305_SALSA2012)
  73. s20.crypt12(payload,payload,payloadLen);
  74. poly1305(mac,payload,payloadLen,macKey);
  75. packet.data.fields.mac = mac[0];
  76. }
  77. }
  78. unsigned int _compress(Buf< Header > &packet,const unsigned int packetSize)
  79. {
  80. uint8_t tmp[ZT_BUF_MEM_SIZE + 32];
  81. if ((packet.data.fields.verb & ZT_PROTO_VERB_FLAG_COMPRESSED) != 0) // sanity check for multiple calls to compress()
  82. return packetSize;
  83. const unsigned int uncompressedLen = packetSize - ZT_PROTO_PACKET_PAYLOAD_START;
  84. const int compressedLen = LZ4_compress_fast(
  85. reinterpret_cast<const char *>(packet.data.bytes + ZT_PROTO_PACKET_PAYLOAD_START),
  86. reinterpret_cast<char *>(tmp),
  87. (int)uncompressedLen,
  88. sizeof(tmp) - ZT_PROTO_PACKET_PAYLOAD_START);
  89. if ((compressedLen > 0)&&(compressedLen < uncompressedLen)) {
  90. packet.data.fields.verb |= ZT_PROTO_VERB_FLAG_COMPRESSED;
  91. memcpy(packet.data.bytes + ZT_PROTO_PACKET_PAYLOAD_START,tmp,compressedLen);
  92. return (unsigned int)compressedLen + ZT_PROTO_PACKET_PAYLOAD_START;
  93. }
  94. return packetSize;
  95. }
  96. #endif
  97. } // namespace Protocol
  98. } // namespace ZeroTier