Skip to content

Commit

Permalink
one-time aes garlic encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Jul 28, 2014
1 parent e77f625 commit 750d6fb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
29 changes: 22 additions & 7 deletions Garlic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace i2p
{
namespace garlic
{
GarlicRoutingSession::GarlicRoutingSession (const i2p::data::RoutingDestination& destination, int numTags):
GarlicRoutingSession::GarlicRoutingSession (const i2p::data::RoutingDestination * destination, int numTags):
m_Destination (destination), m_FirstMsgID (0), m_IsAcknowledged (false),
m_NumTags (numTags), m_NextTag (-1), m_SessionTags (0), m_TagsCreationTime (0)
{
Expand All @@ -30,6 +30,16 @@ namespace garlic
m_SessionTags = nullptr;
}

GarlicRoutingSession::GarlicRoutingSession (const uint8_t * sessionKey, const uint8_t * sessionTag):
m_Destination (nullptr), m_FirstMsgID (0), m_IsAcknowledged (true), m_NumTags (1), m_NextTag (0)
{
memcpy (m_SessionKey, sessionKey, 32);
m_Encryption.SetKey (m_SessionKey);
m_SessionTags = new uint8_t[1]; // 1 tag
memcpy (m_SessionTags, sessionTag, 32);
m_TagsCreationTime = i2p::util::GetSecondsSinceEpoch ();
}

GarlicRoutingSession::~GarlicRoutingSession ()
{
delete[] m_SessionTags;
Expand Down Expand Up @@ -71,13 +81,18 @@ namespace garlic
// create message
if (m_NextTag < 0 || !m_NumTags) // new session
{
if (!m_Destination)
{
LogPrint ("Can't use ElGamal for unknown destination");
return nullptr;
}
// 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);
m_Destination.GetElGamalEncryption ()->Encrypt ((uint8_t *)&elGamal, sizeof(elGamal), buf, true);
m_Destination->GetElGamalEncryption ()->Encrypt ((uint8_t *)&elGamal, sizeof(elGamal), buf, true);
m_Encryption.SetIV (iv);
buf += 514;
len += 514;
Expand Down Expand Up @@ -161,7 +176,7 @@ namespace garlic
}
if (msg) // clove message ifself if presented
{
size += CreateGarlicClove (payload + size, msg, m_Destination.IsDestination ());
size += CreateGarlicClove (payload + size, msg, m_Destination ? m_Destination->IsDestination () : false);
(*numCloves)++;
}

Expand All @@ -178,11 +193,11 @@ namespace garlic
{
uint64_t ts = i2p::util::GetMillisecondsSinceEpoch () + 5000; // 5 sec
size_t size = 0;
if (isDestination)
if (isDestination && m_Destination)
{
buf[size] = eGarlicDeliveryTypeDestination << 5;// delivery instructions flag destination
size++;
memcpy (buf + size, m_Destination.GetIdentHash (), 32);
memcpy (buf + size, m_Destination->GetIdentHash (), 32);
size += 32;
}
else
Expand Down Expand Up @@ -269,7 +284,7 @@ namespace garlic
delete it->second;
m_Sessions.erase (it);
}
GarlicRoutingSession * session = new GarlicRoutingSession (destination, 0); // not follow-on messages expected
GarlicRoutingSession * session = new GarlicRoutingSession (&destination, 0); // not follow-on messages expected
m_Sessions[destination.GetIdentHash ()] = session;

return session->WrapSingleMessage (msg, nullptr);
Expand All @@ -284,7 +299,7 @@ namespace garlic
session = it->second;
if (!session)
{
session = new GarlicRoutingSession (destination, 32);
session = new GarlicRoutingSession (&destination, 32);
m_Sessions[destination.GetIdentHash ()] = session;
}

Expand Down
6 changes: 4 additions & 2 deletions Garlic.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ namespace garlic
#pragma pack()

const int TAGS_EXPIRATION_TIMEOUT = 900; // 15 minutes

class GarlicRoutingSession
{
public:

GarlicRoutingSession (const i2p::data::RoutingDestination& destination, int numTags);
GarlicRoutingSession (const i2p::data::RoutingDestination * destination, int numTags);
GarlicRoutingSession (const uint8_t * sessionKey, const uint8_t * sessionTag); // one time encryption
~GarlicRoutingSession ();
I2NPMessage * WrapSingleMessage (I2NPMessage * msg, const I2NPMessage * leaseSet);
int GetNextTag () const { return m_NextTag; };
Expand All @@ -60,7 +62,7 @@ namespace garlic

private:

const i2p::data::RoutingDestination& m_Destination;
const i2p::data::RoutingDestination * m_Destination;
uint8_t m_SessionKey[32];
uint32_t m_FirstMsgID; // first message ID
bool m_IsAcknowledged;
Expand Down

0 comments on commit 750d6fb

Please sign in to comment.