Packet.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. namespace ZeroTier {
  19. const unsigned char 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 };
  20. void Packet::armor(const void *key,bool encryptPayload)
  21. {
  22. uint8_t mangledKey[32];
  23. uint8_t *const data = reinterpret_cast<uint8_t *>(unsafeData());
  24. // Set flag now, since it affects key mangle function
  25. setCipher(encryptPayload ? ZT_PROTO_CIPHER_SUITE__POLY1305_SALSA2012 : ZT_PROTO_CIPHER_SUITE__POLY1305_NONE);
  26. _salsa20MangleKey((const unsigned char *)key,mangledKey);
  27. Salsa20 s20(mangledKey,data + ZT_PACKET_IDX_IV);
  28. uint64_t macKey[4];
  29. s20.crypt12(ZERO_KEY,macKey,sizeof(macKey));
  30. uint8_t *const payload = data + ZT_PACKET_IDX_VERB;
  31. const unsigned int payloadLen = size() - ZT_PACKET_IDX_VERB;
  32. if (encryptPayload)
  33. s20.crypt12(payload,payload,payloadLen);
  34. uint64_t mac[2];
  35. poly1305(mac,payload,payloadLen,macKey);
  36. memcpy(data + ZT_PACKET_IDX_MAC,mac,8);
  37. }
  38. bool Packet::dearmor(const void *key)
  39. {
  40. uint8_t mangledKey[32];
  41. uint8_t *const data = reinterpret_cast<uint8_t *>(unsafeData());
  42. const unsigned int payloadLen = size() - ZT_PACKET_IDX_VERB;
  43. unsigned char *const payload = data + ZT_PACKET_IDX_VERB;
  44. const unsigned int cs = cipher();
  45. if ((cs == ZT_PROTO_CIPHER_SUITE__POLY1305_NONE)||(cs == ZT_PROTO_CIPHER_SUITE__POLY1305_SALSA2012)) {
  46. _salsa20MangleKey((const unsigned char *)key,mangledKey);
  47. Salsa20 s20(mangledKey,data + ZT_PACKET_IDX_IV);
  48. uint64_t macKey[4];
  49. s20.crypt12(ZERO_KEY,macKey,sizeof(macKey));
  50. uint64_t mac[2];
  51. poly1305(mac,payload,payloadLen,macKey);
  52. #ifdef ZT_NO_TYPE_PUNNING
  53. if (!Utils::secureEq(mac,data + ZT_PACKET_IDX_MAC,8))
  54. return false;
  55. #else
  56. if ((*reinterpret_cast<const uint64_t *>(data + ZT_PACKET_IDX_MAC)) != mac[0]) // also secure, constant time
  57. return false;
  58. #endif
  59. if (cs == ZT_PROTO_CIPHER_SUITE__POLY1305_SALSA2012)
  60. s20.crypt12(payload,payload,payloadLen);
  61. return true;
  62. } else {
  63. return false; // unrecognized cipher suite
  64. }
  65. }
  66. bool Packet::compress()
  67. {
  68. char *const data = reinterpret_cast<char *>(unsafeData());
  69. char buf[ZT_PROTO_MAX_PACKET_LENGTH * 2];
  70. if ((!compressed())&&(size() > (ZT_PACKET_IDX_PAYLOAD + 64))) { // don't bother compressing tiny packets
  71. int pl = (int)(size() - ZT_PACKET_IDX_PAYLOAD);
  72. int cl = LZ4_compress_fast(data + ZT_PACKET_IDX_PAYLOAD,buf,pl,ZT_PROTO_MAX_PACKET_LENGTH * 2,1);
  73. if ((cl > 0)&&(cl < pl)) {
  74. data[ZT_PACKET_IDX_VERB] |= (char)ZT_PROTO_VERB_FLAG_COMPRESSED;
  75. setSize((unsigned int)cl + ZT_PACKET_IDX_PAYLOAD);
  76. memcpy(data + ZT_PACKET_IDX_PAYLOAD,buf,cl);
  77. return true;
  78. }
  79. }
  80. data[ZT_PACKET_IDX_VERB] &= (char)(~ZT_PROTO_VERB_FLAG_COMPRESSED);
  81. return false;
  82. }
  83. bool Packet::uncompress()
  84. {
  85. char *const data = reinterpret_cast<char *>(unsafeData());
  86. char buf[ZT_PROTO_MAX_PACKET_LENGTH];
  87. if ((compressed())&&(size() >= ZT_PROTO_MIN_PACKET_LENGTH)) {
  88. if (size() > ZT_PACKET_IDX_PAYLOAD) {
  89. unsigned int compLen = size() - ZT_PACKET_IDX_PAYLOAD;
  90. int ucl = LZ4_decompress_safe((const char *)data + ZT_PACKET_IDX_PAYLOAD,buf,compLen,sizeof(buf));
  91. if ((ucl > 0)&&(ucl <= (int)(capacity() - ZT_PACKET_IDX_PAYLOAD))) {
  92. setSize((unsigned int)ucl + ZT_PACKET_IDX_PAYLOAD);
  93. memcpy(data + ZT_PACKET_IDX_PAYLOAD,buf,ucl);
  94. } else {
  95. return false;
  96. }
  97. }
  98. data[ZT_PACKET_IDX_VERB] &= (char)(~ZT_PROTO_VERB_FLAG_COMPRESSED);
  99. }
  100. return true;
  101. }
  102. uint64_t Packet::nextPacketId()
  103. {
  104. // The packet ID which is also the packet's nonce/IV can be sequential but
  105. // it should never repeat. This scheme minimizes the chance of nonce
  106. // repetition if (as will usually be the case) the clock is relatively
  107. // accurate.
  108. static uint64_t ctr = 0;
  109. static Mutex lock;
  110. lock.lock();
  111. while (ctr == 0) {
  112. Utils::getSecureRandom(&ctr,sizeof(ctr));
  113. ctr >>= 32;
  114. ctr |= (((uint64_t)time(nullptr)) & 0xffffffffULL) << 32;
  115. }
  116. const uint64_t i = ctr++;
  117. lock.unlock();
  118. return i;
  119. }
  120. } // namespace ZeroTier