2
0

Packet.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 <cstdint>
  14. #include <cstring>
  15. #include "Packet.hpp"
  16. #include "Mutex.hpp"
  17. #include "LZ4.hpp"
  18. #if (defined(_MSC_VER) || defined(__GNUC__) || defined(__clang)) && (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. const uint8_t Packet::ZERO_KEY[32] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
  26. void Packet::armor(const void *key,bool encryptPayload)
  27. {
  28. uint8_t mangledKey[32];
  29. uint8_t *const data = reinterpret_cast<uint8_t *>(unsafeData());
  30. // Set flag now, since it affects key mangle function
  31. setCipher(encryptPayload ? ZT_PROTO_CIPHER_SUITE__POLY1305_SALSA2012 : ZT_PROTO_CIPHER_SUITE__POLY1305_NONE);
  32. _salsa20MangleKey((const unsigned char *)key,mangledKey);
  33. Salsa20 s20(mangledKey,data + ZT_PACKET_IDX_IV);
  34. uint64_t macKey[4];
  35. s20.crypt12(ZERO_KEY,macKey,sizeof(macKey));
  36. uint8_t *const payload = data + ZT_PACKET_IDX_VERB;
  37. const unsigned int payloadLen = size() - ZT_PACKET_IDX_VERB;
  38. if (encryptPayload)
  39. s20.crypt12(payload,payload,payloadLen);
  40. uint64_t mac[2];
  41. poly1305(mac,payload,payloadLen,macKey);
  42. memcpy(data + ZT_PACKET_IDX_MAC,mac,8);
  43. }
  44. bool Packet::dearmor(const void *key)
  45. {
  46. uint8_t mangledKey[32];
  47. uint8_t *const data = reinterpret_cast<uint8_t *>(unsafeData());
  48. const unsigned int payloadLen = size() - ZT_PACKET_IDX_VERB;
  49. unsigned char *const payload = data + ZT_PACKET_IDX_VERB;
  50. const unsigned int cs = cipher();
  51. if ((cs == ZT_PROTO_CIPHER_SUITE__POLY1305_NONE)||(cs == ZT_PROTO_CIPHER_SUITE__POLY1305_SALSA2012)) {
  52. _salsa20MangleKey((const unsigned char *)key,mangledKey);
  53. Salsa20 s20(mangledKey,data + ZT_PACKET_IDX_IV);
  54. uint64_t macKey[4];
  55. s20.crypt12(ZERO_KEY,macKey,sizeof(macKey));
  56. uint64_t mac[2];
  57. poly1305(mac,payload,payloadLen,macKey);
  58. #ifdef ZT_NO_TYPE_PUNNING
  59. if (!Utils::secureEq(mac,data + ZT_PACKET_IDX_MAC,8))
  60. return false;
  61. #else
  62. if ((*reinterpret_cast<const uint64_t *>(data + ZT_PACKET_IDX_MAC)) != mac[0]) // also secure, constant time
  63. return false;
  64. #endif
  65. if (cs == ZT_PROTO_CIPHER_SUITE__POLY1305_SALSA2012)
  66. s20.crypt12(payload,payload,payloadLen);
  67. return true;
  68. } else {
  69. return false; // unrecognized cipher suite
  70. }
  71. }
  72. bool Packet::compress()
  73. {
  74. char *const data = reinterpret_cast<char *>(unsafeData());
  75. char buf[ZT_PROTO_MAX_PACKET_LENGTH * 2];
  76. if ((!compressed())&&(size() > (ZT_PACKET_IDX_PAYLOAD + 64))) { // don't bother compressing tiny packets
  77. int pl = (int)(size() - ZT_PACKET_IDX_PAYLOAD);
  78. int cl = LZ4_compress_fast(data + ZT_PACKET_IDX_PAYLOAD,buf,pl,ZT_PROTO_MAX_PACKET_LENGTH * 2,1);
  79. if ((cl > 0)&&(cl < pl)) {
  80. data[ZT_PACKET_IDX_VERB] |= (char)ZT_PROTO_VERB_FLAG_COMPRESSED;
  81. setSize((unsigned int)cl + ZT_PACKET_IDX_PAYLOAD);
  82. memcpy(data + ZT_PACKET_IDX_PAYLOAD,buf,cl);
  83. return true;
  84. }
  85. }
  86. data[ZT_PACKET_IDX_VERB] &= (char)(~ZT_PROTO_VERB_FLAG_COMPRESSED);
  87. return false;
  88. }
  89. bool Packet::uncompress()
  90. {
  91. char *const data = reinterpret_cast<char *>(unsafeData());
  92. char buf[ZT_PROTO_MAX_PACKET_LENGTH];
  93. if ((compressed())&&(size() >= ZT_PROTO_MIN_PACKET_LENGTH)) {
  94. if (size() > ZT_PACKET_IDX_PAYLOAD) {
  95. unsigned int compLen = size() - ZT_PACKET_IDX_PAYLOAD;
  96. int ucl = LZ4_decompress_safe((const char *)data + ZT_PACKET_IDX_PAYLOAD,buf,compLen,sizeof(buf));
  97. if ((ucl > 0)&&(ucl <= (int)(capacity() - ZT_PACKET_IDX_PAYLOAD))) {
  98. setSize((unsigned int)ucl + ZT_PACKET_IDX_PAYLOAD);
  99. memcpy(data + ZT_PACKET_IDX_PAYLOAD,buf,ucl);
  100. } else {
  101. return false;
  102. }
  103. }
  104. data[ZT_PACKET_IDX_VERB] &= (char)(~ZT_PROTO_VERB_FLAG_COMPRESSED);
  105. }
  106. return true;
  107. }
  108. static unsigned long long s_initPacketID()
  109. {
  110. unsigned long long tmp = 0;
  111. Utils::getSecureRandom(&tmp,sizeof(tmp));
  112. tmp >>= 31U;
  113. tmp |= (((uint64_t)time(nullptr)) & 0xffffffffULL) << 33U;
  114. return tmp;
  115. }
  116. #ifdef ZT_PACKET_USE_ATOMIC_INTRINSICS
  117. static unsigned long long s_packetIdCtr = s_initPacketID();
  118. #else
  119. static std::atomic<unsigned long long> s_packetIdCtr(s_initPacketID());
  120. #endif
  121. uint64_t Packet::nextPacketId()
  122. {
  123. #ifdef ZT_PACKET_USE_ATOMIC_INTRINSICS
  124. return __sync_add_and_fetch(&s_packetIdCtr,1ULL);
  125. #else
  126. return ++s_packetIdCtr;
  127. #endif
  128. }
  129. } // namespace ZeroTier