Protocol.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. uint64_t createProbe(const Identity &sender,const Identity &recipient,const uint8_t key[ZT_PEER_SECRET_KEY_LENGTH]) noexcept
  26. {
  27. uint8_t tmp[ZT_IDENTITY_HASH_SIZE + ZT_IDENTITY_HASH_SIZE];
  28. memcpy(tmp,sender.fingerprint().data(),ZT_IDENTITY_HASH_SIZE);
  29. memcpy(tmp + ZT_IDENTITY_HASH_SIZE,recipient.fingerprint().data(),ZT_IDENTITY_HASH_SIZE);
  30. uint64_t hash[6];
  31. SHA384(hash,tmp,sizeof(tmp),key,ZT_PEER_SECRET_KEY_LENGTH);
  32. return hash[0];
  33. }
  34. std::atomic<uint64_t> _s_packetIdCtr((uint64_t)time(nullptr) << 32U);
  35. void armor(Buf &pkt,int packetSize,const uint8_t key[ZT_PEER_SECRET_KEY_LENGTH],uint8_t cipherSuite) noexcept
  36. {
  37. Protocol::Header &ph = pkt.as<Protocol::Header>();
  38. ph.flags = (ph.flags & 0xc7U) | ((cipherSuite << 3U) & 0x38U); // flags: FFCCCHHH where CCC is cipher
  39. switch(cipherSuite) {
  40. case ZT_PROTO_CIPHER_SUITE__POLY1305_NONE: {
  41. uint8_t perPacketKey[ZT_PEER_SECRET_KEY_LENGTH];
  42. salsa2012DeriveKey(key,perPacketKey,pkt,packetSize);
  43. Salsa20 s20(perPacketKey,&ph.packetId);
  44. uint8_t macKey[ZT_POLY1305_KEY_LEN];
  45. s20.crypt12(Utils::ZERO256,macKey,ZT_POLY1305_KEY_LEN);
  46. // only difference here is that we don't encrypt the payload
  47. uint64_t mac[2];
  48. poly1305(mac,pkt.unsafeData + ZT_PROTO_PACKET_ENCRYPTED_SECTION_START,packetSize - ZT_PROTO_PACKET_ENCRYPTED_SECTION_START,macKey);
  49. ph.mac = mac[0];
  50. } break;
  51. case ZT_PROTO_CIPHER_SUITE__POLY1305_SALSA2012: {
  52. uint8_t perPacketKey[ZT_PEER_SECRET_KEY_LENGTH];
  53. salsa2012DeriveKey(key,perPacketKey,pkt,packetSize);
  54. Salsa20 s20(perPacketKey,&ph.packetId);
  55. uint8_t macKey[ZT_POLY1305_KEY_LEN];
  56. s20.crypt12(Utils::ZERO256,macKey,ZT_POLY1305_KEY_LEN);
  57. const unsigned int encLen = packetSize - ZT_PROTO_PACKET_ENCRYPTED_SECTION_START;
  58. s20.crypt12(pkt.unsafeData + ZT_PROTO_PACKET_ENCRYPTED_SECTION_START,pkt.unsafeData + ZT_PROTO_PACKET_ENCRYPTED_SECTION_START,encLen);
  59. uint64_t mac[2];
  60. poly1305(mac,pkt.unsafeData + ZT_PROTO_PACKET_ENCRYPTED_SECTION_START,encLen,macKey);
  61. ph.mac = mac[0];
  62. } break;
  63. case ZT_PROTO_CIPHER_SUITE__AES_GCM_NRH: {
  64. } break;
  65. }
  66. }
  67. int compress(SharedPtr<Buf> &pkt,int packetSize) noexcept
  68. {
  69. if (packetSize <= 128)
  70. return packetSize;
  71. SharedPtr<Buf> pkt2(new Buf());
  72. if (!pkt2) return packetSize;
  73. const int uncompressedLen = packetSize - ZT_PROTO_PACKET_PAYLOAD_START;
  74. 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);
  75. if ((compressedLen > 0)&&(compressedLen < uncompressedLen)) {
  76. memcpy(pkt2->unsafeData,pkt->unsafeData,ZT_PROTO_PACKET_PAYLOAD_START);
  77. pkt.swap(pkt2);
  78. pkt->as<Protocol::Header>().verb |= ZT_PROTO_VERB_FLAG_COMPRESSED;
  79. return compressedLen + ZT_PROTO_PACKET_PAYLOAD_START;
  80. }
  81. return packetSize;
  82. }
  83. } // namespace Protocol
  84. } // namespace ZeroTier