2014-05-06 20:22:22 +04:00
|
|
|
#ifndef AES_H__
|
|
|
|
#define AES_H__
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <cryptopp/modes.h>
|
|
|
|
#include <cryptopp/aes.h>
|
2014-11-01 21:56:13 +03:00
|
|
|
#include "Identity.h"
|
2014-05-06 20:22:22 +04:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace crypto
|
2015-07-17 00:29:52 +03:00
|
|
|
{
|
2015-07-30 23:06:42 +03:00
|
|
|
struct CipherBlock
|
2015-07-17 00:29:52 +03:00
|
|
|
{
|
|
|
|
uint8_t buf[16];
|
2014-05-09 19:44:39 +04:00
|
|
|
|
2015-07-30 23:06:42 +03:00
|
|
|
void operator^=(const CipherBlock& other) // XOR
|
2015-07-17 00:29:52 +03:00
|
|
|
{
|
2014-11-19 19:01:04 +03:00
|
|
|
#if defined(__x86_64__) // for Intel x64
|
2015-07-17 00:29:52 +03:00
|
|
|
__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"
|
|
|
|
);
|
2014-11-19 19:01:04 +03:00
|
|
|
#else
|
2015-07-17 00:29:52 +03:00
|
|
|
// TODO: implement it better
|
|
|
|
for (int i = 0; i < 16; i++)
|
|
|
|
buf[i] ^= other.buf[i];
|
2014-11-19 19:01:04 +03:00
|
|
|
#endif
|
2015-07-17 00:29:52 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
2014-11-18 20:11:45 +03:00
|
|
|
|
|
|
|
|
2014-06-02 18:05:04 +04:00
|
|
|
#ifdef AESNI
|
2015-07-17 00:29:52 +03:00
|
|
|
class ECBCryptoAESNI
|
|
|
|
{
|
|
|
|
public:
|
2014-05-09 05:43:08 +04:00
|
|
|
|
2015-07-17 00:29:52 +03:00
|
|
|
uint8_t * GetKeySchedule () { return m_KeySchedule; };
|
2014-11-18 20:11:45 +03:00
|
|
|
|
2015-07-17 00:29:52 +03:00
|
|
|
protected:
|
2014-05-08 23:11:38 +04:00
|
|
|
|
2015-07-17 00:29:52 +03:00
|
|
|
void ExpandKey (const AESKey& key);
|
|
|
|
|
|
|
|
private:
|
2014-05-08 23:11:38 +04:00
|
|
|
|
2015-07-17 00:29:52 +03:00
|
|
|
AESAlignedBuffer<240> m_KeySchedule; // 14 rounds for AES-256, 240 bytes
|
|
|
|
};
|
2014-05-08 23:11:38 +04:00
|
|
|
|
2015-07-17 00:29:52 +03:00
|
|
|
class ECBEncryptionAESNI: public ECBCryptoAESNI
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
void SetKey (const AESKey& key) { ExpandKey (key); };
|
2015-07-30 23:06:42 +03:00
|
|
|
void Encrypt (const CipherBlock * in, CipherBlock * out);
|
2015-07-17 00:29:52 +03:00
|
|
|
};
|
2014-05-07 22:48:37 +04:00
|
|
|
|
2015-07-17 00:29:52 +03:00
|
|
|
class ECBDecryptionAESNI: public ECBCryptoAESNI
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
void SetKey (const AESKey& key);
|
2015-07-30 23:06:42 +03:00
|
|
|
void Decrypt (const CipherBlock * in, CipherBlock * out);
|
2015-07-17 00:29:52 +03:00
|
|
|
};
|
2014-05-07 22:48:37 +04:00
|
|
|
|
2015-07-17 00:29:52 +03:00
|
|
|
typedef ECBEncryptionAESNI ECBEncryption;
|
|
|
|
typedef ECBDecryptionAESNI ECBDecryption;
|
2014-05-09 00:49:00 +04:00
|
|
|
|
|
|
|
#else // use crypto++
|
|
|
|
|
2015-07-17 00:29:52 +03:00
|
|
|
class ECBEncryption
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
void SetKey (const AESKey& key)
|
|
|
|
{
|
|
|
|
m_Encryption.SetKey (key, 32);
|
|
|
|
}
|
2015-07-30 23:06:42 +03:00
|
|
|
void Encrypt (const CipherBlock * in, CipherBlock * out)
|
2015-07-17 00:29:52 +03:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
2015-07-30 23:06:42 +03:00
|
|
|
void Decrypt (const CipherBlock * in, CipherBlock * out)
|
2015-07-17 00:29:52 +03:00
|
|
|
{
|
|
|
|
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)
|
2015-07-16 18:39:24 +03:00
|
|
|
: CBCEncryption()
|
|
|
|
{
|
|
|
|
SetKey(key);
|
|
|
|
SetIV(iv);
|
2015-07-31 14:01:49 +03:00
|
|
|
}
|
2014-05-06 20:22:22 +04:00
|
|
|
|
2015-07-17 00:29:52 +03:00
|
|
|
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
|
2014-05-06 20:22:22 +04:00
|
|
|
|
2015-07-30 23:06:42 +03:00
|
|
|
void Encrypt (int numBlocks, const CipherBlock * in, CipherBlock * out);
|
2015-07-17 00:29:52 +03:00
|
|
|
void Encrypt (const uint8_t * in, std::size_t len, uint8_t * out);
|
|
|
|
void Encrypt (const uint8_t * in, uint8_t * out); // one block
|
2014-05-06 20:22:22 +04:00
|
|
|
|
2015-07-17 00:29:52 +03:00
|
|
|
private:
|
2014-05-06 20:22:22 +04:00
|
|
|
|
2015-07-30 23:06:42 +03:00
|
|
|
CipherBlock m_LastBlock;
|
2015-07-17 00:29:52 +03:00
|
|
|
|
|
|
|
ECBEncryption m_ECBEncryption;
|
|
|
|
};
|
2014-05-06 20:22:22 +04:00
|
|
|
|
2015-07-17 00:29:52 +03:00
|
|
|
class CBCDecryption
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
CBCDecryption () { memset (m_IV.buf, 0, 16); };
|
2014-05-06 20:22:22 +04:00
|
|
|
|
2015-07-31 14:01:49 +03:00
|
|
|
CBCDecryption(const AESKey& key, const uint8_t* iv)
|
|
|
|
: CBCDecryption()
|
|
|
|
{
|
|
|
|
SetKey(key);
|
|
|
|
SetIV(iv);
|
|
|
|
}
|
|
|
|
|
2015-07-17 00:29:52 +03:00
|
|
|
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
|
2014-05-06 20:22:22 +04:00
|
|
|
|
2015-07-30 23:06:42 +03:00
|
|
|
void Decrypt (int numBlocks, const CipherBlock * in, CipherBlock * out);
|
2015-07-17 00:29:52 +03:00
|
|
|
void Decrypt (const uint8_t * in, std::size_t len, uint8_t * out);
|
|
|
|
void Decrypt (const uint8_t * in, uint8_t * out); // one block
|
2014-05-06 20:22:22 +04:00
|
|
|
|
2015-07-17 00:29:52 +03:00
|
|
|
private:
|
2014-05-06 20:22:22 +04:00
|
|
|
|
2015-07-30 23:06:42 +03:00
|
|
|
CipherBlock m_IV;
|
2015-07-17 00:29:52 +03:00
|
|
|
ECBDecryption m_ECBDecryption;
|
|
|
|
};
|
2014-05-15 19:21:41 +04:00
|
|
|
|
2014-05-06 20:22:22 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|