2014-05-06 16:22:22 +00:00
|
|
|
#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];
|
2014-05-09 15:44:39 +00:00
|
|
|
|
2014-05-09 16:05:04 +00:00
|
|
|
void operator^=(const ChipherBlock& other) // XOR
|
2014-05-09 15:44:39 +00:00
|
|
|
{
|
|
|
|
#ifdef __x86_64__
|
|
|
|
__asm__
|
|
|
|
(
|
|
|
|
"movups (%[b1]), %%xmm0 \n"
|
2014-05-09 16:05:04 +00:00
|
|
|
"movups (%[b2]), %%xmm1 \n" // b2 might not be 16-bytes aligned
|
|
|
|
"pxor %%xmm1, %%xmm0 \n"
|
2014-05-09 15:44:39 +00:00
|
|
|
"movups %%xmm0, (%[b1]) \n"
|
|
|
|
: : [b1]"r"(buf), [b2]"r"(other.buf): "memory", "%xmm0"
|
|
|
|
);
|
|
|
|
#else
|
|
|
|
ll[0] ^= other.ll[0];
|
|
|
|
ll[1] ^= other.ll[1];
|
|
|
|
#endif
|
|
|
|
}
|
2014-05-06 16:22:22 +00:00
|
|
|
};
|
|
|
|
|
2014-05-07 18:48:37 +00:00
|
|
|
#ifdef __x86_64__
|
|
|
|
// AES-NI assumed
|
2014-05-07 19:40:24 +00:00
|
|
|
class ECBCryptoAESNI
|
2014-05-08 19:11:38 +00:00
|
|
|
{
|
2014-05-09 01:43:08 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
ECBCryptoAESNI ();
|
|
|
|
|
2014-05-08 19:11:38 +00:00
|
|
|
protected:
|
|
|
|
|
|
|
|
void ExpandKey (const uint8_t * key);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2014-05-09 01:43:08 +00:00
|
|
|
uint8_t * m_KeySchedule; // start of 16 bytes boundary of m_UnalignedBuffer
|
|
|
|
uint8_t m_UnalignedBuffer[256]; // 14 rounds for AES-256, 240 + 16 bytes
|
2014-05-08 19:11:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ECBEncryptionAESNI: public ECBCryptoAESNI
|
2014-05-07 18:48:37 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2014-05-08 19:11:38 +00:00
|
|
|
void SetKey (const uint8_t * key) { ExpandKey (key); };
|
2014-05-07 19:39:30 +00:00
|
|
|
void Encrypt (const ChipherBlock * in, ChipherBlock * out);
|
2014-05-08 19:11:38 +00:00
|
|
|
};
|
2014-05-07 18:48:37 +00:00
|
|
|
|
2014-05-08 19:11:38 +00:00
|
|
|
class ECBDecryptionAESNI: public ECBCryptoAESNI
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
void SetKey (const uint8_t * key);
|
|
|
|
void Decrypt (const ChipherBlock * in, ChipherBlock * out);
|
2014-05-07 18:48:37 +00:00
|
|
|
};
|
|
|
|
|
2014-05-08 20:49:00 +00:00
|
|
|
typedef ECBEncryptionAESNI ECBEncryption;
|
|
|
|
typedef ECBDecryptionAESNI ECBDecryption;
|
|
|
|
|
|
|
|
#else // use crypto++
|
|
|
|
|
|
|
|
class ECBEncryption
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
void SetKey (const uint8_t * key)
|
|
|
|
{
|
|
|
|
m_Encryption.SetKey (key, 32);
|
|
|
|
}
|
|
|
|
void Encrypt (const ChipherBlock * in, ChipherBlock * out)
|
|
|
|
{
|
|
|
|
m_Encryption.ProcessData (out->buf, in->buf, 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
CryptoPP::ECB_Mode<CryptoPP::AES>::Encryption m_Encryption;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ECBDecryption
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
void SetKey (const uint8_t * key)
|
|
|
|
{
|
|
|
|
m_Decryption.SetKey (key, 32);
|
|
|
|
}
|
|
|
|
void Decrypt (const ChipherBlock * in, ChipherBlock * out)
|
|
|
|
{
|
|
|
|
m_Decryption.ProcessData (out->buf, in->buf, 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
CryptoPP::ECB_Mode<CryptoPP::AES>::Decryption m_Decryption;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-05-07 18:48:37 +00:00
|
|
|
#endif
|
|
|
|
|
2014-05-06 16:22:22 +00:00
|
|
|
class CBCEncryption
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
CBCEncryption () { memset (m_LastBlock.buf, 0, 16); };
|
|
|
|
|
2014-05-09 01:43:08 +00:00
|
|
|
void SetKey (const uint8_t * key) { m_ECBEncryption.SetKey (key); }; // 32 bytes
|
2014-05-07 02:30:09 +00:00
|
|
|
void SetIV (const uint8_t * iv) { memcpy (m_LastBlock.buf, iv, 16); }; // 16 bytes
|
2014-05-06 16:22:22 +00:00
|
|
|
|
|
|
|
void Encrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out);
|
2014-05-06 17:26:28 +00:00
|
|
|
bool Encrypt (const uint8_t * in, std::size_t len, uint8_t * out);
|
2014-05-06 16:22:22 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
ChipherBlock m_LastBlock;
|
2014-05-09 01:43:08 +00:00
|
|
|
ECBEncryption m_ECBEncryption;
|
2014-05-06 16:22:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class CBCDecryption
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
CBCDecryption () { memset (m_IV.buf, 0, 16); };
|
|
|
|
|
2014-05-09 01:43:08 +00:00
|
|
|
void SetKey (const uint8_t * key) { m_ECBDecryption.SetKey (key); }; // 32 bytes
|
2014-05-07 02:30:09 +00:00
|
|
|
void SetIV (const uint8_t * iv) { memcpy (m_IV.buf, iv, 16); }; // 16 bytes
|
2014-05-06 16:22:22 +00:00
|
|
|
|
|
|
|
void Decrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out);
|
2014-05-06 17:26:28 +00:00
|
|
|
bool Decrypt (const uint8_t * in, std::size_t len, uint8_t * out);
|
2014-05-06 16:22:22 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
ChipherBlock m_IV;
|
2014-05-09 01:43:08 +00:00
|
|
|
ECBDecryption m_ECBDecryption;
|
2014-05-06 16:22:22 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|