use AES-NI in new CBC encryption

This commit is contained in:
orignal 2014-05-08 21:43:08 -04:00
parent 63bf67ba95
commit 95013e95a9
2 changed files with 21 additions and 8 deletions

14
aes.cpp
View File

@ -7,7 +7,15 @@ namespace crypto
{
#ifdef __x86_64__
ECBCryptoAESNI::ECBCryptoAESNI ()
{
m_KeySchedule = m_UnalignedBuffer;
uint8_t rem = ((uint64_t)m_KeySchedule) & 0x0f;
if (rem)
m_KeySchedule += (16 - rem);
}
#define KeyExpansion256 \
"pshufd $0xff, %%xmm2, %%xmm2 \n" \
"movaps %%xmm1, %%xmm4 \n" \
@ -159,7 +167,7 @@ namespace crypto
{
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);
m_ECBEncryption.Encrypt (&m_LastBlock, &m_LastBlock);
out[i] = m_LastBlock;
}
}
@ -177,7 +185,7 @@ namespace crypto
for (int i = 0; i < numBlocks; i++)
{
ChipherBlock tmp = in[i];
m_ECBDecryption.ProcessData (out[i].buf, in[i].buf, 16);
m_ECBDecryption.Decrypt (in + i, out + i);
out[i].ll[0] ^= m_IV.ll[0];
out[i].ll[1] ^= m_IV.ll[1];
m_IV = tmp;

15
aes.h
View File

@ -19,13 +19,18 @@ namespace crypto
// AES-NI assumed
class ECBCryptoAESNI
{
public:
ECBCryptoAESNI ();
protected:
void ExpandKey (const uint8_t * key);
protected:
uint32_t m_KeySchedule[4*(14+1)]; // 14 rounds for AES-256
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
};
class ECBEncryptionAESNI: public ECBCryptoAESNI
@ -94,7 +99,7 @@ namespace crypto
CBCEncryption () { memset (m_LastBlock.buf, 0, 16); };
void SetKey (const uint8_t * key) { m_ECBEncryption.SetKey (key, 32); }; // 32 bytes
void SetKey (const uint8_t * key) { m_ECBEncryption.SetKey (key); }; // 32 bytes
void SetIV (const uint8_t * iv) { memcpy (m_LastBlock.buf, iv, 16); }; // 16 bytes
void Encrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out);
@ -103,7 +108,7 @@ namespace crypto
private:
ChipherBlock m_LastBlock;
CryptoPP::ECB_Mode<CryptoPP::AES>::Encryption m_ECBEncryption;
ECBEncryption m_ECBEncryption;
};
class CBCDecryption
@ -112,7 +117,7 @@ namespace crypto
CBCDecryption () { memset (m_IV.buf, 0, 16); };
void SetKey (const uint8_t * key) { m_ECBDecryption.SetKey (key, 32); }; // 32 bytes
void SetKey (const uint8_t * key) { m_ECBDecryption.SetKey (key); }; // 32 bytes
void SetIV (const uint8_t * iv) { memcpy (m_IV.buf, iv, 16); }; // 16 bytes
void Decrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out);
@ -121,7 +126,7 @@ namespace crypto
private:
ChipherBlock m_IV;
CryptoPP::ECB_Mode<CryptoPP::AES>::Decryption m_ECBDecryption;
ECBDecryption m_ECBDecryption;
};
}
}