Protocol.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 <ctime>
  18. #ifdef __WINDOWS__
  19. #include <process.h>
  20. #else
  21. #include <unistd.h>
  22. #endif
  23. namespace ZeroTier {
  24. namespace Protocol {
  25. void armor(Buf &pkt,int packetSize,const uint8_t key[ZT_SYMMETRIC_KEY_SIZE],uint8_t cipherSuite) noexcept
  26. {
  27. Protocol::Header &ph = pkt.as<Protocol::Header>(); // NOLINT(hicpp-use-auto,modernize-use-auto)
  28. ph.flags = (ph.flags & 0xc7U) | ((cipherSuite << 3U) & 0x38U); // flags: FFCCCHHH where CCC is cipher
  29. switch(cipherSuite) {
  30. case ZT_PROTO_CIPHER_SUITE__POLY1305_NONE: {
  31. uint8_t perPacketKey[ZT_SYMMETRIC_KEY_SIZE];
  32. salsa2012DeriveKey(key,perPacketKey,pkt,packetSize);
  33. Salsa20 s20(perPacketKey,&ph.packetId);
  34. uint8_t macKey[ZT_POLY1305_KEY_SIZE];
  35. s20.crypt12(Utils::ZERO256,macKey,ZT_POLY1305_KEY_SIZE);
  36. // only difference here is that we don't encrypt the payload
  37. uint64_t mac[2];
  38. poly1305(mac,pkt.unsafeData + ZT_PROTO_PACKET_ENCRYPTED_SECTION_START,packetSize - ZT_PROTO_PACKET_ENCRYPTED_SECTION_START,macKey);
  39. ph.mac = mac[0];
  40. } break;
  41. case ZT_PROTO_CIPHER_SUITE__POLY1305_SALSA2012: {
  42. uint8_t perPacketKey[ZT_SYMMETRIC_KEY_SIZE];
  43. salsa2012DeriveKey(key,perPacketKey,pkt,packetSize);
  44. Salsa20 s20(perPacketKey,&ph.packetId);
  45. uint8_t macKey[ZT_POLY1305_KEY_SIZE];
  46. s20.crypt12(Utils::ZERO256,macKey,ZT_POLY1305_KEY_SIZE);
  47. const unsigned int encLen = packetSize - ZT_PROTO_PACKET_ENCRYPTED_SECTION_START;
  48. s20.crypt12(pkt.unsafeData + ZT_PROTO_PACKET_ENCRYPTED_SECTION_START,pkt.unsafeData + ZT_PROTO_PACKET_ENCRYPTED_SECTION_START,encLen);
  49. uint64_t mac[2];
  50. poly1305(mac,pkt.unsafeData + ZT_PROTO_PACKET_ENCRYPTED_SECTION_START,encLen,macKey);
  51. ph.mac = mac[0];
  52. } break;
  53. case ZT_PROTO_CIPHER_SUITE__AES_GMAC_SIV: {
  54. } break;
  55. }
  56. }
  57. int compress(SharedPtr<Buf> &pkt,int packetSize) noexcept
  58. {
  59. if (packetSize <= 128)
  60. return packetSize;
  61. SharedPtr<Buf> pkt2(new Buf());
  62. if (!pkt2) return packetSize;
  63. const int uncompressedLen = packetSize - ZT_PROTO_PACKET_PAYLOAD_START;
  64. 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);
  65. if ((compressedLen > 0)&&(compressedLen < uncompressedLen)) {
  66. Utils::copy<ZT_PROTO_PACKET_PAYLOAD_START>(pkt2->unsafeData,pkt->unsafeData);
  67. pkt.swap(pkt2);
  68. pkt->as<Protocol::Header>().verb |= ZT_PROTO_VERB_FLAG_COMPRESSED;
  69. return compressedLen + ZT_PROTO_PACKET_PAYLOAD_START;
  70. }
  71. return packetSize;
  72. }
  73. } // namespace Protocol
  74. } // namespace ZeroTier