Skip to content

Commit

Permalink
TunnelDecryption for tunnels
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed May 15, 2014
1 parent 7754968 commit 0d51f24
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
9 changes: 4 additions & 5 deletions I2NPProtocol.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include <string.h>
#include "I2PEndian.h"
#include <cryptopp/sha.h>
#include <cryptopp/modes.h>
#include <cryptopp/aes.h>
#include <cryptopp/gzip.h>
#include "ElGamal.h"
#include "Timestamp.h"
Expand Down Expand Up @@ -259,11 +257,12 @@ namespace i2p
//TODO: fill filler
CryptoPP::SHA256().CalculateDigest(reply->hash, reply->padding, sizeof (reply->padding) + 1); // + 1 byte of ret
// encrypt reply
CryptoPP::CBC_Mode<CryptoPP::AES>::Encryption encryption;
i2p::crypto::CBCEncryption encryption;
for (int j = 0; j < num; j++)
{
encryption.SetKeyWithIV (clearText.replyKey, 32, clearText.replyIV);
encryption.ProcessData((uint8_t *)(records + j), (uint8_t *)(records + j), sizeof (records[j]));
encryption.SetKey (clearText.replyKey);
encryption.SetIV (clearText.replyIV);
encryption.Encrypt((uint8_t *)(records + j), sizeof (records[j]), (uint8_t *)(records + j));
}
return true;
}
Expand Down
28 changes: 13 additions & 15 deletions Tunnel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,17 @@ namespace tunnel
i++;
hop = hop->next;
}


i2p::crypto::CBCDecryption decryption;
hop = m_Config->GetLastHop ()->prev;
size_t ind = numRecords - 1;
while (hop)
{
for (size_t i = ind; i < numRecords; i++)
hop->decryption.Decrypt((uint8_t *)&records[i],
sizeof (I2NPBuildRequestRecordElGamalEncrypted), (uint8_t *)&records[i]);
decryption.SetKey (hop->replyKey);
decryption.SetIV (hop->replyIV);
for (size_t i = ind; i < numRecords; i++)
decryption.Decrypt((uint8_t *)&records[i],
sizeof (I2NPBuildRequestRecordElGamalEncrypted), (uint8_t *)&records[i]);
hop = hop->prev;
ind--;
}
Expand All @@ -74,14 +77,17 @@ namespace tunnel
{
LogPrint ("TunnelBuildResponse ", (int)msg[0], " records.");

i2p::crypto::CBCDecryption decryption;
TunnelHopConfig * hop = m_Config->GetLastHop ();
int num = msg[0];
while (hop)
{
decryption.SetKey (hop->replyKey);
decryption.SetIV (hop->replyIV);
for (int i = 0; i < num; i++)
{
uint8_t * record = msg + 1 + i*sizeof (I2NPBuildResponseRecord);
hop->decryption.Decrypt(record, sizeof (I2NPBuildResponseRecord), record);
decryption.Decrypt(record, sizeof (I2NPBuildResponseRecord), record);
}
hop = hop->prev;
num--;
Expand All @@ -102,8 +108,7 @@ namespace tunnel
TunnelHopConfig * hop = m_Config->GetFirstHop ();
while (hop)
{
hop->decryption.SetKey (hop->layerKey);
hop->ivDecryption.SetKey (hop->ivKey);
hop->decryption.SetKeys (hop->layerKey, hop->ivKey);
hop = hop->next;
}
}
Expand All @@ -116,14 +121,7 @@ namespace tunnel
TunnelHopConfig * hop = m_Config->GetLastHop ();
while (hop)
{
// iv
hop->ivDecryption.Decrypt ((i2p::crypto::ChipherBlock *)payload, (i2p::crypto::ChipherBlock *)payload);
// data
hop->decryption.SetIV (payload);
hop->decryption.Decrypt (payload + 16, TUNNEL_DATA_ENCRYPTED_SIZE, payload+16);
// double iv ecncryption
hop->ivDecryption.Decrypt ((i2p::crypto::ChipherBlock *)payload, (i2p::crypto::ChipherBlock *)payload);

hop->decryption.Decrypt (payload);
hop = hop->prev;
}
}
Expand Down
5 changes: 1 addition & 4 deletions TunnelConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ namespace tunnel
bool isGateway, isEndpoint;

TunnelHopConfig * next, * prev;
i2p::crypto::CBCDecryption decryption;
i2p::crypto::ECBDecryption ivDecryption;
i2p::crypto::TunnelDecryption decryption;

TunnelHopConfig (const i2p::data::RouterInfo * r)
{
Expand All @@ -41,8 +40,6 @@ namespace tunnel

next = 0;
prev = 0;
decryption.SetKey (replyKey);
decryption.SetIV (replyIV);
}

void SetNextRouter (const i2p::data::RouterInfo * r)
Expand Down

0 comments on commit 0d51f24

Please sign in to comment.