From bf4c33325cf7c447890c90a4c1361c8bd0a5bada Mon Sep 17 00:00:00 2001 From: orignal Date: Fri, 26 Jun 2015 16:06:59 -0400 Subject: [PATCH] random non-zero padding --- TunnelGateway.cpp | 16 ++++++++++++++-- TunnelGateway.h | 6 +++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/TunnelGateway.cpp b/TunnelGateway.cpp index cedbff10..ffbb8fe3 100644 --- a/TunnelGateway.cpp +++ b/TunnelGateway.cpp @@ -10,6 +10,14 @@ namespace i2p { namespace tunnel { + TunnelGatewayBuffer::TunnelGatewayBuffer (uint32_t tunnelID): m_TunnelID (tunnelID), + m_CurrentTunnelDataMsg (nullptr), m_RemainingSize (0) + { + context.GetRandomNumberGenerator ().GenerateBlock (m_NonZeroRandomBuffer, TUNNEL_DATA_MAX_PAYLOAD_SIZE); + for (size_t i = 0; i < TUNNEL_DATA_MAX_PAYLOAD_SIZE; i++) + if (!m_NonZeroRandomBuffer[i]) m_NonZeroRandomBuffer[i] = 1; + } + TunnelGatewayBuffer::~TunnelGatewayBuffer () { } @@ -160,13 +168,17 @@ namespace tunnel payload[-1] = 0; // zero ptrdiff_t paddingSize = payload - buf - 25; // 25 = 24 + 1 if (paddingSize > 0) - memset (buf + 24, 1, paddingSize); // padding TODO: fill with random data + { + // non-zero padding + auto randomOffset = rnd.GenerateWord32 (0, TUNNEL_DATA_MAX_PAYLOAD_SIZE - paddingSize); + memcpy (buf + 24, m_NonZeroRandomBuffer + randomOffset, paddingSize); + } // we can't fill message header yet because encryption is required m_TunnelDataMsgs.push_back (m_CurrentTunnelDataMsg); m_CurrentTunnelDataMsg = nullptr; } - + void TunnelGateway::SendTunnelDataMsg (const TunnelMessageBlock& block) { if (block.data) diff --git a/TunnelGateway.h b/TunnelGateway.h index cfad17b5..ea88317b 100644 --- a/TunnelGateway.h +++ b/TunnelGateway.h @@ -14,8 +14,7 @@ namespace tunnel class TunnelGatewayBuffer { public: - TunnelGatewayBuffer (uint32_t tunnelID): m_TunnelID (tunnelID), - m_CurrentTunnelDataMsg (nullptr), m_RemainingSize (0) {}; + TunnelGatewayBuffer (uint32_t tunnelID); ~TunnelGatewayBuffer (); void PutI2NPMsg (const TunnelMessageBlock& block); const std::vector >& GetTunnelDataMsgs () const { return m_TunnelDataMsgs; }; @@ -32,13 +31,14 @@ namespace tunnel std::vector > m_TunnelDataMsgs; std::shared_ptr m_CurrentTunnelDataMsg; size_t m_RemainingSize; + uint8_t m_NonZeroRandomBuffer[TUNNEL_DATA_MAX_PAYLOAD_SIZE]; }; class TunnelGateway { public: - TunnelGateway (TunnelBase * tunnel): + TunnelGateway (TunnelBase * tunnel): m_Tunnel (tunnel), m_Buffer (tunnel->GetNextTunnelID ()), m_NumSentBytes (0) {}; void SendTunnelDataMsg (const TunnelMessageBlock& block); void PutTunnelDataMsg (const TunnelMessageBlock& block);