Browse Source

aes added

pull/72/head
orignal 11 years ago
parent
commit
d8d8c752b1
  1. 2
      Makefile
  2. 30
      aes.cpp
  3. 55
      aes.h

2
Makefile

@ -6,7 +6,7 @@ OBJECTS = obj/CryptoConst.o obj/base64.o obj/NTCPSession.o obj/RouterInfo.o obj/ @@ -6,7 +6,7 @@ OBJECTS = obj/CryptoConst.o obj/base64.o obj/NTCPSession.o obj/RouterInfo.o obj/
obj/TunnelGateway.o obj/TransitTunnel.o obj/I2NPProtocol.o obj/Log.o obj/Garlic.o \
obj/HTTPServer.o obj/Streaming.o obj/Identity.o obj/SSU.o obj/util.o obj/Reseed.o \
obj/UPnP.o obj/TunnelPool.o obj/HTTPProxy.o obj/AddressBook.o obj/Daemon.o \
obj/DaemonLinux.o obj/SSUData.o obj/i2p.o
obj/DaemonLinux.o obj/SSUData.o obj/i2p.o obj/aes.o
INCFLAGS =
LDFLAGS = -Wl,-rpath,/usr/local/lib -lcryptopp -lboost_system -lboost_filesystem -lboost_regex -lboost_program_options -lpthread
LIBS =

30
aes.cpp

@ -0,0 +1,30 @@ @@ -0,0 +1,30 @@
#include "aes.h"
namespace i2p
{
namespace crypto
{
void CBCEncryption::Encrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out)
{
for (int i = 0; i < numBlocks; i++)
{
m_LastBlock.ll[0] ^= in[i].ll[0];
m_LastBlock.ll[1] ^= in[i].ll[1];
m_ECBEncryption.ProcessData (m_LastBlock.buf, m_LastBlock.buf, 16);
out[i] = m_LastBlock;
}
}
void CBCDecryption::Decrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out)
{
for (int i = 0; i < numBlocks; i++)
{
m_ECBDecryption.ProcessData (out[i].buf, in[i].buf, 16);
out[i].ll[0] ^= m_IV.ll[0];
out[i].ll[1] ^= m_IV.ll[1];
m_IV = in[i];
}
}
}
}

55
aes.h

@ -0,0 +1,55 @@ @@ -0,0 +1,55 @@
#ifndef AES_H__
#define AES_H__
#include <inttypes.h>
#include <cryptopp/modes.h>
#include <cryptopp/aes.h>
namespace i2p
{
namespace crypto
{
union ChipherBlock
{
uint8_t buf[16];
uint64_t ll[2];
};
class CBCEncryption
{
public:
CBCEncryption () { memset (m_LastBlock.buf, 0, 16); };
void SetKey (uint8_t * key) { m_ECBEncryption.SetKey (key, 32); }; // 32 bytes
void SetIV (uint8_t * iv) { memcpy (m_LastBlock.buf, iv, 16); }; // 16 bytes
void Encrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out);
private:
ChipherBlock m_LastBlock;
CryptoPP::ECB_Mode<CryptoPP::AES>::Encryption m_ECBEncryption;
};
class CBCDecryption
{
public:
CBCDecryption () { memset (m_IV.buf, 0, 16); };
void SetKey (uint8_t * key) { m_ECBDecryption.SetKey (key, 32); }; // 32 bytes
void SetIV (uint8_t * iv) { memcpy (m_IV.buf, iv, 16); }; // 16 bytes
void Decrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out);
private:
ChipherBlock m_IV;
CryptoPP::ECB_Mode<CryptoPP::AES>::Decryption m_ECBDecryption;
};
}
}
#endif
Loading…
Cancel
Save