From 2f7d3e655ad463480d495b3ce4c24e2a65e76b65 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Wed, 28 Aug 2019 14:26:13 -0700 Subject: [PATCH] . --- node/Packet.cpp | 13 +++++++++++++ node/Packet.hpp | 12 +++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/node/Packet.cpp b/node/Packet.cpp index eb85c6dc2..24994d60e 100644 --- a/node/Packet.cpp +++ b/node/Packet.cpp @@ -18,6 +18,7 @@ #include #include "Packet.hpp" +#include "Mutex.hpp" #ifdef _MSC_VER #define FORCE_INLINE static __forceinline @@ -932,4 +933,16 @@ bool Packet::uncompress() return true; } +uint64_t Packet::nextPacketId() +{ + static uint64_t ctr = 0; + static Mutex lock; + lock.lock(); + if (unlikely(ctr == 0)) + Utils::getSecureRandom(&ctr,sizeof(ctr)); + const uint64_t i = ctr++; + lock.unlock(); + return i; +} + } // namespace ZeroTier diff --git a/node/Packet.hpp b/node/Packet.hpp index 4ed7b8634..bb20ac637 100644 --- a/node/Packet.hpp +++ b/node/Packet.hpp @@ -986,7 +986,7 @@ public: ZT_ALWAYS_INLINE Packet() : Buffer(ZT_PROTO_MIN_PACKET_LENGTH) { - setAt(ZT_PACKET_IDX_IV,Utils::random()); + setAt(ZT_PACKET_IDX_IV,Packet::nextPacketId()); (*this)[ZT_PACKET_IDX_FLAGS] = 0; // zero flags, cipher ID, and hops } @@ -1002,7 +1002,7 @@ public: ZT_ALWAYS_INLINE Packet(const Packet &prototype,const Address &dest) : Buffer(prototype) { - setAt(ZT_PACKET_IDX_IV,Utils::random()); + setAt(ZT_PACKET_IDX_IV,Packet::nextPacketId()); setDestination(dest); } @@ -1016,7 +1016,7 @@ public: ZT_ALWAYS_INLINE Packet(const Address &dest,const Address &source,const Verb v) : Buffer(ZT_PROTO_MIN_PACKET_LENGTH) { - setAt(ZT_PACKET_IDX_IV,Utils::random()); + setAt(ZT_PACKET_IDX_IV,Packet::nextPacketId()); setDestination(dest); setSource(source); (*this)[ZT_PACKET_IDX_FLAGS] = 0; // zero flags and hops @@ -1033,7 +1033,7 @@ public: ZT_ALWAYS_INLINE void reset(const Address &dest,const Address &source,const Verb v) { setSize(ZT_PROTO_MIN_PACKET_LENGTH); - setAt(ZT_PACKET_IDX_IV,Utils::random()); + setAt(ZT_PACKET_IDX_IV,Packet::nextPacketId()); setDestination(dest); setSource(source); (*this)[ZT_PACKET_IDX_FLAGS] = 0; // zero flags, cipher ID, and hops @@ -1047,7 +1047,7 @@ public: * technically different but otherwise identical copies of the same * packet. */ - ZT_ALWAYS_INLINE void newInitializationVector() { setAt(ZT_PACKET_IDX_IV,Utils::random()); } + ZT_ALWAYS_INLINE void newInitializationVector() { setAt(ZT_PACKET_IDX_IV,Packet::nextPacketId()); } /** * Set this packet's destination @@ -1238,6 +1238,8 @@ public: private: static const unsigned char ZERO_KEY[32]; + static uint64_t nextPacketId(); + /** * Deterministically mangle a 256-bit crypto key based on packet *