Protocol.cpp 3.4 KB

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