i2pd/Garlic.cpp

163 lines
4.7 KiB
C++
Raw Normal View History

2013-11-23 21:35:15 +00:00
#include <inttypes.h>
2013-12-12 03:25:01 +00:00
#include <endian.h>
2013-11-23 21:35:15 +00:00
#include <map>
#include <string>
2013-11-24 23:10:27 +00:00
#include "ElGamal.h"
2013-11-23 21:35:15 +00:00
#include "RouterContext.h"
#include "Timestamp.h"
#include "Garlic.h"
namespace i2p
{
2013-11-24 23:10:27 +00:00
namespace garlic
{
GarlicRoutingSession::GarlicRoutingSession (const i2p::data::RoutingDestination * destination, int numTags):
m_Destination (destination), m_NumTags (numTags), m_NextTag (-1), m_SessionTags (0)
2013-11-23 21:35:15 +00:00
{
2013-11-24 23:10:27 +00:00
m_Rnd.GenerateBlock (m_SessionKey, 32);
if (m_NumTags > 0)
{
m_SessionTags = new uint8_t[m_NumTags*32];
for (int i = 0; i < m_NumTags; i++)
m_Rnd.GenerateBlock (m_SessionTags + i*32, 32);
}
}
GarlicRoutingSession::~GarlicRoutingSession ()
{
delete[] m_SessionTags;
}
2013-11-23 21:35:15 +00:00
2013-11-24 23:10:27 +00:00
I2NPMessage * GarlicRoutingSession::WrapSingleMessage (I2NPMessage * msg)
{
2013-11-23 21:35:15 +00:00
I2NPMessage * m = NewI2NPMessage ();
2013-11-24 23:10:27 +00:00
size_t len = 0;
2013-11-23 21:35:15 +00:00
uint8_t * buf = m->GetPayload () + 4; // 4 bytes for length
2013-11-24 23:10:27 +00:00
if (m_NextTag < 0) // new session
{
// create ElGamal block
ElGamalBlock elGamal;
memcpy (elGamal.sessionKey, m_SessionKey, 32);
m_Rnd.GenerateBlock (elGamal.preIV, 32); // Pre-IV
uint8_t iv[32]; // IV is first 16 bytes
CryptoPP::SHA256().CalculateDigest(iv, elGamal.preIV, 32);
i2p::crypto::ElGamalEncrypt (m_Destination->GetEncryptionPublicKey (), (uint8_t *)&elGamal, sizeof(elGamal), buf, true);
buf += 514;
// AES block
m_Encryption.SetKeyWithIV (m_SessionKey, 32, iv);
len += 514 + CreateAESBlock (buf, msg);
}
else // existing session
{
// session tag
memcpy (buf, m_SessionTags + m_NextTag*32, 32);
buf += 32;
uint8_t iv[32]; // IV is first 16 bytes
CryptoPP::SHA256().CalculateDigest(iv, m_SessionTags + m_NextTag*32, 32);
m_Encryption.SetKeyWithIV (m_SessionKey, 32, iv);
// AES block
len += 32 + CreateAESBlock (buf, msg);
}
m_NextTag++;
*(uint32_t *)(m->GetPayload ()) = htobe32 (len);
m->len += len + 4;
FillI2NPMessageHeader (m, eI2NPGarlic);
DeleteI2NPMessage (msg);
return m;
}
size_t GarlicRoutingSession::CreateAESBlock (uint8_t * buf, I2NPMessage * msg)
{
2013-11-23 21:35:15 +00:00
size_t blockSize = 0;
2013-11-24 23:10:27 +00:00
*(uint16_t *)buf = htobe16 (m_NumTags); // tag count
2013-11-23 21:35:15 +00:00
blockSize += 2;
2013-11-24 23:10:27 +00:00
memcpy (buf + blockSize, m_SessionTags, m_NumTags*32); // tags
blockSize += m_NumTags*32;
2013-11-23 21:35:15 +00:00
uint32_t * payloadSize = (uint32_t *)(buf + blockSize);
blockSize += 4;
uint8_t * payloadHash = buf + blockSize;
blockSize += 32;
buf[blockSize] = 0; // flag
blockSize++;
size_t len = CreateGarlicPayload (buf + blockSize, msg);
*payloadSize = htobe32 (len);
CryptoPP::SHA256().CalculateDigest(payloadHash, buf + blockSize, len);
blockSize += len;
size_t rem = blockSize % 16;
if (rem)
blockSize += (16-rem); //padding
2013-11-24 23:10:27 +00:00
m_Encryption.ProcessData(buf, buf, blockSize);
return blockSize;
2013-11-23 21:35:15 +00:00
}
2013-11-24 23:10:27 +00:00
size_t GarlicRoutingSession::CreateGarlicPayload (uint8_t * payload, I2NPMessage * msg)
2013-11-23 21:35:15 +00:00
{
2013-11-24 23:10:27 +00:00
uint64_t ts = i2p::util::GetMillisecondsSinceEpoch () + 5000; // 5 sec
2013-11-23 21:35:15 +00:00
size_t size = 0;
payload[size] = 1; // 1 clove
size++;
if (m_Destination->IsDestination ())
{
payload[size] = eGarlicDeliveryTypeDestination << 5;// delivery instructions flag destination
size++;
memcpy (payload + size, m_Destination->GetIdentHash (), 32);
size += 32;
}
else
{
payload[size] = 0;// delivery instructions flag local
size++;
}
2013-11-23 21:35:15 +00:00
memcpy (payload + size, msg->GetBuffer (), msg->GetLength ());
size += msg->GetLength ();
2013-11-24 23:10:27 +00:00
*(uint32_t *)(payload + size) = htobe32 (m_Rnd.GenerateWord32 ()); // CloveID
2013-11-23 21:35:15 +00:00
size += 4;
*(uint64_t *)(payload + size) = htobe64 (ts); // Expiration of clove
size += 8;
memset (payload + size, 0, 3); // certificate of clove
size += 3;
memset (payload + size, 0, 3); // certificate of message
size += 3;
2013-11-24 23:10:27 +00:00
*(uint32_t *)(payload + size) = htobe32 (m_Rnd.GenerateWord32 ()); // MessageID
2013-11-23 21:35:15 +00:00
size += 4;
*(uint64_t *)(payload + size) = htobe64 (ts); // Expiration of message
size += 8;
return size;
}
2013-11-24 23:10:27 +00:00
GarlicRouting routing;
GarlicRouting::GarlicRouting ()
{
}
GarlicRouting::~GarlicRouting ()
{
for (auto it: m_Sessions)
delete it.second;
m_Sessions.clear ();
}
I2NPMessage * GarlicRouting::WrapSingleMessage (const i2p::data::RoutingDestination * destination, I2NPMessage * msg)
{
if (!destination) return nullptr;
2013-12-10 13:10:49 +00:00
auto it = m_Sessions.find (destination->GetIdentHash ());
2013-11-24 23:10:27 +00:00
GarlicRoutingSession * session = nullptr;
if (it != m_Sessions.end ())
session = it->second;
if (!session)
{
session = new GarlicRoutingSession (destination, 4); // TODO: change it later
2013-12-10 13:10:49 +00:00
m_Sessions[destination->GetIdentHash ()] = session;
2013-11-24 23:10:27 +00:00
}
I2NPMessage * ret = session->WrapSingleMessage (msg);
if (session->GetNumRemainingSessionTags () <= 0)
{
2013-12-10 13:10:49 +00:00
m_Sessions.erase (destination->GetIdentHash ());
2013-11-24 23:10:27 +00:00
delete session;
}
return ret;
}
}
}