mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
195 lines
4.8 KiB
195 lines
4.8 KiB
11 years ago
|
#ifndef AES_H__
|
||
|
#define AES_H__
|
||
|
|
||
|
#include <inttypes.h>
|
||
|
#include <cryptopp/modes.h>
|
||
|
#include <cryptopp/aes.h>
|
||
10 years ago
|
#include "Identity.h"
|
||
11 years ago
|
|
||
|
namespace i2p
|
||
|
{
|
||
|
namespace crypto
|
||
10 years ago
|
{
|
||
9 years ago
|
struct CipherBlock
|
||
10 years ago
|
{
|
||
|
uint8_t buf[16];
|
||
11 years ago
|
|
||
9 years ago
|
void operator^=(const CipherBlock& other) // XOR
|
||
10 years ago
|
{
|
||
10 years ago
|
#if defined(__x86_64__) // for Intel x64
|
||
10 years ago
|
__asm__
|
||
|
(
|
||
|
"movups (%[buf]), %%xmm0 \n"
|
||
|
"movups (%[other]), %%xmm1 \n"
|
||
|
"pxor %%xmm1, %%xmm0 \n"
|
||
|
"movups %%xmm0, (%[buf]) \n"
|
||
|
:
|
||
|
: [buf]"r"(buf), [other]"r"(other.buf)
|
||
|
: "%xmm0", "%xmm1", "memory"
|
||
|
);
|
||
10 years ago
|
#else
|
||
10 years ago
|
// TODO: implement it better
|
||
|
for (int i = 0; i < 16; i++)
|
||
|
buf[i] ^= other.buf[i];
|
||
10 years ago
|
#endif
|
||
10 years ago
|
}
|
||
|
};
|
||
|
|
||
|
typedef i2p::data::Tag<32> AESKey;
|
||
|
|
||
|
template<size_t sz>
|
||
|
class AESAlignedBuffer // 16 bytes alignment
|
||
|
{
|
||
|
public:
|
||
|
|
||
|
AESAlignedBuffer ()
|
||
|
{
|
||
|
m_Buf = m_UnalignedBuffer;
|
||
|
uint8_t rem = ((size_t)m_Buf) & 0x0f;
|
||
|
if (rem)
|
||
|
m_Buf += (16 - rem);
|
||
|
}
|
||
|
|
||
|
operator uint8_t * () { return m_Buf; };
|
||
|
operator const uint8_t * () const { return m_Buf; };
|
||
|
|
||
|
private:
|
||
|
|
||
|
uint8_t m_UnalignedBuffer[sz + 15]; // up to 15 bytes alignment
|
||
|
uint8_t * m_Buf;
|
||
|
};
|
||
10 years ago
|
|
||
|
|
||
11 years ago
|
#ifdef AESNI
|
||
10 years ago
|
class ECBCryptoAESNI
|
||
|
{
|
||
|
public:
|
||
11 years ago
|
|
||
10 years ago
|
uint8_t * GetKeySchedule () { return m_KeySchedule; };
|
||
10 years ago
|
|
||
10 years ago
|
protected:
|
||
11 years ago
|
|
||
10 years ago
|
void ExpandKey (const AESKey& key);
|
||
|
|
||
|
private:
|
||
11 years ago
|
|
||
10 years ago
|
AESAlignedBuffer<240> m_KeySchedule; // 14 rounds for AES-256, 240 bytes
|
||
|
};
|
||
11 years ago
|
|
||
10 years ago
|
class ECBEncryptionAESNI: public ECBCryptoAESNI
|
||
|
{
|
||
|
public:
|
||
|
|
||
|
void SetKey (const AESKey& key) { ExpandKey (key); };
|
||
9 years ago
|
void Encrypt (const CipherBlock * in, CipherBlock * out);
|
||
10 years ago
|
};
|
||
11 years ago
|
|
||
10 years ago
|
class ECBDecryptionAESNI: public ECBCryptoAESNI
|
||
|
{
|
||
|
public:
|
||
|
|
||
|
void SetKey (const AESKey& key);
|
||
9 years ago
|
void Decrypt (const CipherBlock * in, CipherBlock * out);
|
||
10 years ago
|
};
|
||
11 years ago
|
|
||
10 years ago
|
typedef ECBEncryptionAESNI ECBEncryption;
|
||
|
typedef ECBDecryptionAESNI ECBDecryption;
|
||
11 years ago
|
|
||
|
#else // use crypto++
|
||
|
|
||
10 years ago
|
class ECBEncryption
|
||
|
{
|
||
|
public:
|
||
|
|
||
|
void SetKey (const AESKey& key)
|
||
|
{
|
||
|
m_Encryption.SetKey (key, 32);
|
||
|
}
|
||
9 years ago
|
void Encrypt (const CipherBlock * in, CipherBlock * out)
|
||
10 years ago
|
{
|
||
|
m_Encryption.ProcessData (out->buf, in->buf, 16);
|
||
|
}
|
||
|
|
||
|
private:
|
||
|
|
||
|
CryptoPP::ECB_Mode<CryptoPP::AES>::Encryption m_Encryption;
|
||
|
};
|
||
|
|
||
|
class ECBDecryption
|
||
|
{
|
||
|
public:
|
||
|
|
||
|
void SetKey (const AESKey& key)
|
||
|
{
|
||
|
m_Decryption.SetKey (key, 32);
|
||
|
}
|
||
9 years ago
|
void Decrypt (const CipherBlock * in, CipherBlock * out)
|
||
10 years ago
|
{
|
||
|
m_Decryption.ProcessData (out->buf, in->buf, 16);
|
||
|
}
|
||
|
|
||
|
private:
|
||
|
|
||
|
CryptoPP::ECB_Mode<CryptoPP::AES>::Decryption m_Decryption;
|
||
|
};
|
||
|
|
||
|
|
||
|
#endif
|
||
|
|
||
|
class CBCEncryption
|
||
|
{
|
||
|
public:
|
||
|
|
||
|
CBCEncryption () { memset (m_LastBlock.buf, 0, 16); };
|
||
|
CBCEncryption(const AESKey& key, const uint8_t* iv)
|
||
10 years ago
|
: CBCEncryption()
|
||
|
{
|
||
|
SetKey(key);
|
||
|
SetIV(iv);
|
||
9 years ago
|
}
|
||
11 years ago
|
|
||
10 years ago
|
void SetKey (const AESKey& key) { m_ECBEncryption.SetKey (key); }; // 32 bytes
|
||
|
void SetIV (const uint8_t * iv) { memcpy (m_LastBlock.buf, iv, 16); }; // 16 bytes
|
||
11 years ago
|
|
||
9 years ago
|
void Encrypt (int numBlocks, const CipherBlock * in, CipherBlock * out);
|
||
10 years ago
|
void Encrypt (const uint8_t * in, std::size_t len, uint8_t * out);
|
||
|
void Encrypt (const uint8_t * in, uint8_t * out); // one block
|
||
11 years ago
|
|
||
10 years ago
|
private:
|
||
11 years ago
|
|
||
9 years ago
|
CipherBlock m_LastBlock;
|
||
10 years ago
|
|
||
|
ECBEncryption m_ECBEncryption;
|
||
|
};
|
||
11 years ago
|
|
||
10 years ago
|
class CBCDecryption
|
||
|
{
|
||
|
public:
|
||
|
|
||
|
CBCDecryption () { memset (m_IV.buf, 0, 16); };
|
||
11 years ago
|
|
||
9 years ago
|
CBCDecryption(const AESKey& key, const uint8_t* iv)
|
||
|
: CBCDecryption()
|
||
|
{
|
||
|
SetKey(key);
|
||
|
SetIV(iv);
|
||
|
}
|
||
|
|
||
10 years ago
|
void SetKey (const AESKey& key) { m_ECBDecryption.SetKey (key); }; // 32 bytes
|
||
|
void SetIV (const uint8_t * iv) { memcpy (m_IV.buf, iv, 16); }; // 16 bytes
|
||
11 years ago
|
|
||
9 years ago
|
void Decrypt (int numBlocks, const CipherBlock * in, CipherBlock * out);
|
||
10 years ago
|
void Decrypt (const uint8_t * in, std::size_t len, uint8_t * out);
|
||
|
void Decrypt (const uint8_t * in, uint8_t * out); // one block
|
||
11 years ago
|
|
||
10 years ago
|
private:
|
||
11 years ago
|
|
||
9 years ago
|
CipherBlock m_IV;
|
||
10 years ago
|
ECBDecryption m_ECBDecryption;
|
||
|
};
|
||
11 years ago
|
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
|
#endif
|