Browse Source

use AES-NI in new CBC encryption

pull/72/head
orignal 11 years ago
parent
commit
95013e95a9
  1. 12
      aes.cpp
  2. 15
      aes.h

12
aes.cpp

@ -8,6 +8,14 @@ namespace crypto
#ifdef __x86_64__ #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 \ #define KeyExpansion256 \
"pshufd $0xff, %%xmm2, %%xmm2 \n" \ "pshufd $0xff, %%xmm2, %%xmm2 \n" \
"movaps %%xmm1, %%xmm4 \n" \ "movaps %%xmm1, %%xmm4 \n" \
@ -159,7 +167,7 @@ namespace crypto
{ {
m_LastBlock.ll[0] ^= in[i].ll[0]; m_LastBlock.ll[0] ^= in[i].ll[0];
m_LastBlock.ll[1] ^= in[i].ll[1]; 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; out[i] = m_LastBlock;
} }
} }
@ -177,7 +185,7 @@ namespace crypto
for (int i = 0; i < numBlocks; i++) for (int i = 0; i < numBlocks; i++)
{ {
ChipherBlock tmp = in[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[0] ^= m_IV.ll[0];
out[i].ll[1] ^= m_IV.ll[1]; out[i].ll[1] ^= m_IV.ll[1];
m_IV = tmp; m_IV = tmp;

15
aes.h

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

Loading…
Cancel
Save