Protocol.cpp 3.5 KB

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