i2pd/aes.h

226 lines
4.4 KiB
C
Raw Normal View History

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>
2014-11-01 18:56:13 +00:00
#include "Identity.h"
2014-05-06 16:22:22 +00:00
namespace i2p
{
namespace crypto
{
2014-11-19 16:01:04 +00:00
struct ChipherBlock
2014-05-06 16:22:22 +00:00
{
uint8_t buf[16];
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
{
2014-11-19 16:01:04 +00:00
#if defined(__x86_64__) // for Intel x64
__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"
);
#else
// TODO: implement it better
for (int i = 0; i < 16; i++)
buf[i] ^= other.buf[i];
#endif
2014-05-09 15:44:39 +00:00
}
2014-05-06 16:22:22 +00:00
};
2014-11-01 18:56:13 +00:00
typedef i2p::data::Tag<32> AESKey;
2014-11-18 17:11:45 +00:00
template<size_t sz>
class AESAlignedBuffer // 16 bytes alignment
{
public:
AESAlignedBuffer ()
{
m_Buf = m_UnalignedBuffer;
2014-11-26 16:04:49 +00:00
uint8_t rem = ((size_t)m_Buf) & 0x0f;
2014-11-18 17:11:45 +00:00
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-06-02 14:05:04 +00:00
#ifdef AESNI
2014-05-07 19:40:24 +00:00
class ECBCryptoAESNI
{
2014-05-09 01:43:08 +00:00
public:
2014-05-13 02:51:59 +00:00
uint8_t * GetKeySchedule () { return m_KeySchedule; };
2014-11-18 17:11:45 +00:00
protected:
2014-11-02 01:53:45 +00:00
void ExpandKey (const AESKey& key);
2014-11-18 17:11:45 +00:00
private:
2014-11-18 17:11:45 +00:00
AESAlignedBuffer<240> m_KeySchedule; // 14 rounds for AES-256, 240 bytes
};
class ECBEncryptionAESNI: public ECBCryptoAESNI
2014-05-07 18:48:37 +00:00
{
public:
2014-11-02 01:53:45 +00:00
void SetKey (const AESKey& key) { ExpandKey (key); };
2014-05-07 19:39:30 +00:00
void Encrypt (const ChipherBlock * in, ChipherBlock * out);
};
2014-05-07 18:48:37 +00:00
class ECBDecryptionAESNI: public ECBCryptoAESNI
{
public:
2014-11-02 01:53:45 +00:00
void SetKey (const AESKey& 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:
2014-11-02 01:53:45 +00:00
void SetKey (const AESKey& key)
2014-05-08 20:49:00 +00:00
{
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:
2014-11-02 01:53:45 +00:00
void SetKey (const AESKey& key)
2014-05-08 20:49:00 +00:00
{
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-11-02 01:53:45 +00:00
void SetKey (const AESKey& 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);
void Encrypt (const uint8_t * in, std::size_t len, uint8_t * out);
2014-05-14 18:54:01 +00:00
void Encrypt (const uint8_t * in, uint8_t * out); // one block
2014-05-06 16:22:22 +00:00
private:
ChipherBlock m_LastBlock;
2014-05-13 02:51:59 +00:00
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-11-02 01:53:45 +00:00
void SetKey (const AESKey& 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);
void Decrypt (const uint8_t * in, std::size_t len, uint8_t * out);
2014-05-14 18:54:01 +00:00
void Decrypt (const uint8_t * in, uint8_t * out); // one block
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
};
2014-05-15 15:21:41 +00:00
class TunnelEncryption // with double IV encryption
{
public:
2014-11-02 01:53:45 +00:00
void SetKeys (const AESKey& layerKey, const AESKey& ivKey)
2014-05-15 15:21:41 +00:00
{
m_LayerEncryption.SetKey (layerKey);
m_IVEncryption.SetKey (ivKey);
}
void Encrypt (uint8_t * payload); // 1024 bytes (16 IV + 1008 data)
private:
ECBEncryption m_IVEncryption;
2014-06-02 14:05:04 +00:00
#ifdef AESNI
2014-05-15 16:59:07 +00:00
ECBEncryption m_LayerEncryption;
#else
2014-05-15 15:21:41 +00:00
CBCEncryption m_LayerEncryption;
2014-05-15 16:59:07 +00:00
#endif
2014-05-15 15:21:41 +00:00
};
class TunnelDecryption // with double IV encryption
{
public:
2014-11-02 01:53:45 +00:00
void SetKeys (const AESKey& layerKey, const AESKey& ivKey)
2014-05-15 15:21:41 +00:00
{
m_LayerDecryption.SetKey (layerKey);
m_IVDecryption.SetKey (ivKey);
}
void Decrypt (uint8_t * payload); // 1024 bytes (16 IV + 1008 data)
private:
ECBDecryption m_IVDecryption;
2014-06-02 14:05:04 +00:00
#ifdef AESNI
2014-05-15 17:10:07 +00:00
ECBDecryption m_LayerDecryption;
#else
2014-05-15 15:21:41 +00:00
CBCDecryption m_LayerDecryption;
2014-05-15 17:10:07 +00:00
#endif
2014-05-15 15:21:41 +00:00
};
2014-05-06 16:22:22 +00:00
}
}
#endif