Protocol.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. #if defined(__GCC__) && (defined(__amd64) || defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || defined(__AMD64) || defined(__AMD64__) || defined(_M_X64))
  17. #define ZT_PACKET_USE_ATOMIC_INTRINSICS
  18. #endif
  19. #ifndef ZT_PACKET_USE_ATOMIC_INTRINSICS
  20. #include <atomic>
  21. #endif
  22. namespace ZeroTier {
  23. namespace Protocol {
  24. namespace {
  25. const uint64_t ZEROES32[4] = { 0,0,0,0 };
  26. /**
  27. * Deterministically mangle a 256-bit crypto key based on packet
  28. *
  29. * This uses extra data from the packet to mangle the secret, giving us an
  30. * effective IV that is somewhat more than 64 bits. This is "free" for
  31. * Salsa20 since it has negligible key setup time so using a different
  32. * key each time is fine.
  33. *
  34. * @param in Input key (32 bytes)
  35. * @param out Output buffer (32 bytes)
  36. */
  37. ZT_ALWAYS_INLINE void _salsa20MangleKey(const uint8_t *const in,uint8_t *const out,const Buf< Header > &packet,const unsigned int packetSize)
  38. {
  39. // IV and source/destination addresses. Using the addresses divides the
  40. // key space into two halves-- A->B and B->A (since order will change).
  41. for(int i=0;i<18;++i) // 8 + (ZT_ADDRESS_LENGTH * 2) == 18
  42. out[i] = in[i] ^ packet.data.bytes[i];
  43. // Flags, but with hop count masked off. Hop count is altered by forwarding
  44. // nodes. It's one of the only parts of a packet modifiable by people
  45. // without the key.
  46. out[18] = in[18] ^ (packet.data.fields.flags & 0xf8U);
  47. // Raw packet size in bytes -- thus each packet size defines a new
  48. // key space.
  49. out[19] = in[19] ^ (uint8_t)packetSize;
  50. out[20] = in[20] ^ (uint8_t)(packetSize >> 8U); // little endian
  51. // Rest of raw key is used unchanged
  52. for(int i=21;i<32;++i)
  53. out[i] = in[i];
  54. }
  55. unsigned long long _initPacketID()
  56. {
  57. unsigned long long tmp = 0;
  58. Utils::getSecureRandom(&tmp,sizeof(tmp));
  59. tmp >>= 31U;
  60. tmp |= (((uint64_t)time(nullptr)) & 0xffffffffULL) << 33U;
  61. return tmp;
  62. }
  63. #ifdef ZT_PACKET_USE_ATOMIC_INTRINSICS
  64. unsigned long long _packetIdCtr = _initPacketID();
  65. #else
  66. static std::atomic<unsigned long long> _packetIdCtr(_initPacketID());
  67. #endif
  68. } // anonymous namespace
  69. void _armor(Buf< Header > &packet,const unsigned int packetSize,const uint8_t key[ZT_PEER_SECRET_KEY_LENGTH],const uint8_t cipherSuite)
  70. {
  71. packet.data.fields.flags = (packet.data.fields.flags & 0xc7U) | ((cipherSuite << 3U) & 0x38U); // FFCCCHHH
  72. if (cipherSuite == ZT_PROTO_CIPHER_SUITE__AES_GCM) {
  73. // TODO
  74. } else if (cipherSuite != ZT_PROTO_CIPHER_SUITE__NONE) {
  75. uint8_t mangledKey[ZT_PEER_SECRET_KEY_LENGTH],macKey[ZT_POLY1305_KEY_LEN];
  76. uint64_t mac[2];
  77. _salsa20MangleKey(key,mangledKey,packet,packetSize);
  78. Salsa20 s20(mangledKey,&(packet.data.fields.packetId));
  79. s20.crypt12(ZEROES32,macKey,sizeof(macKey));
  80. uint8_t *payload = packet.data.bytes + ZT_PROTO_PACKET_ENCRYPTED_SECTION_START;
  81. const unsigned int payloadLen = packetSize - ZT_PROTO_PACKET_ENCRYPTED_SECTION_START;
  82. if (cipherSuite == ZT_PROTO_CIPHER_SUITE__POLY1305_SALSA2012)
  83. s20.crypt12(payload,payload,payloadLen);
  84. poly1305(mac,payload,payloadLen,macKey);
  85. packet.data.fields.mac = mac[0];
  86. }
  87. }
  88. int _dearmor(Buf< Header > &packet,const unsigned int packetSize,const uint8_t key[ZT_PEER_SECRET_KEY_LENGTH])
  89. {
  90. const int cipherSuite = (int)(packet.data.fields.flags & 0x38U);
  91. if (cipherSuite == ZT_PROTO_CIPHER_SUITE__AES_GCM) {
  92. // TODO
  93. } else if (cipherSuite != ZT_PROTO_CIPHER_SUITE__NONE) {
  94. uint8_t mangledKey[ZT_PEER_SECRET_KEY_LENGTH],macKey[ZT_POLY1305_KEY_LEN];
  95. uint64_t mac[2];
  96. _salsa20MangleKey(key,mangledKey,packet,packetSize);
  97. Salsa20 s20(mangledKey,&(packet.data.fields.packetId));
  98. s20.crypt12(ZEROES32,macKey,sizeof(macKey));
  99. uint8_t *payload = packet.data.bytes + ZT_PROTO_PACKET_ENCRYPTED_SECTION_START;
  100. const unsigned int payloadLen = packetSize - ZT_PROTO_PACKET_ENCRYPTED_SECTION_START;
  101. if (cipherSuite == ZT_PROTO_CIPHER_SUITE__POLY1305_SALSA2012)
  102. s20.crypt12(payload,payload,payloadLen);
  103. poly1305(mac,payload,payloadLen,macKey);
  104. if (packet.data.fields.mac != mac[0])
  105. return -1;
  106. }
  107. return cipherSuite;
  108. }
  109. unsigned int _compress(Buf< Header > &packet,const unsigned int packetSize)
  110. {
  111. uint8_t tmp[ZT_BUF_MEM_SIZE + 32];
  112. if ((packet.data.fields.verb & ZT_PROTO_VERB_FLAG_COMPRESSED) != 0) // sanity check for multiple calls to compress()
  113. return packetSize;
  114. const unsigned int uncompressedLen = packetSize - ZT_PROTO_PACKET_PAYLOAD_START;
  115. const int compressedLen = LZ4_compress_fast(
  116. reinterpret_cast<const char *>(packet.data.bytes + ZT_PROTO_PACKET_PAYLOAD_START),
  117. reinterpret_cast<char *>(tmp),
  118. (int)uncompressedLen,
  119. sizeof(tmp) - ZT_PROTO_PACKET_PAYLOAD_START);
  120. if ((compressedLen > 0)&&(compressedLen < uncompressedLen)) {
  121. packet.data.fields.verb |= ZT_PROTO_VERB_FLAG_COMPRESSED;
  122. memcpy(packet.data.bytes + ZT_PROTO_PACKET_PAYLOAD_START,tmp,compressedLen);
  123. return (unsigned int)compressedLen + ZT_PROTO_PACKET_PAYLOAD_START;
  124. }
  125. return packetSize;
  126. }
  127. int _uncompress(Buf< Header > &packet,const unsigned int packetSize)
  128. {
  129. uint8_t tmp[ZT_BUF_MEM_SIZE];
  130. if ((packet.data.fields.verb & ZT_PROTO_VERB_FLAG_COMPRESSED) == 0)
  131. return (int)packetSize;
  132. const int uncompressedLen = LZ4_decompress_safe(
  133. reinterpret_cast<const char *>(packet.data.bytes + ZT_PROTO_PACKET_PAYLOAD_START),
  134. reinterpret_cast<char *>(tmp),
  135. (int)(packetSize - ZT_PROTO_PACKET_PAYLOAD_START),
  136. sizeof(tmp) - ZT_PROTO_PACKET_PAYLOAD_START);
  137. if ((uncompressedLen > 0)&&(uncompressedLen <= (sizeof(tmp) - ZT_PROTO_PACKET_PAYLOAD_START))) {
  138. packet.data.fields.verb &= (uint8_t)(~((uint8_t)ZT_PROTO_VERB_FLAG_COMPRESSED));
  139. memcpy(packet.data.bytes + ZT_PROTO_PACKET_PAYLOAD_START,tmp,uncompressedLen);
  140. return uncompressedLen + ZT_PROTO_PACKET_PAYLOAD_START;
  141. }
  142. return -1;
  143. }
  144. uint64_t getPacketId()
  145. {
  146. #ifdef ZT_PACKET_USE_ATOMIC_INTRINSICS
  147. return __sync_add_and_fetch(&_packetIdCtr,1ULL);
  148. #else
  149. return ++_packetIdCtr;
  150. #endif
  151. }
  152. } // namespace Protocol
  153. } // namespace ZeroTier