Skip to content

Commit

Permalink
use AES-NI in new CBC encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed May 9, 2014
1 parent 63bf67b commit 95013e9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
14 changes: 11 additions & 3 deletions aes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" \
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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;
Expand Down
15 changes: 10 additions & 5 deletions aes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -103,7 +108,7 @@ namespace crypto
private:

ChipherBlock m_LastBlock;
CryptoPP::ECB_Mode<CryptoPP::AES>::Encryption m_ECBEncryption;
ECBEncryption m_ECBEncryption;
};

class CBCDecryption
Expand All @@ -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);
Expand All @@ -121,7 +126,7 @@ namespace crypto
private:

ChipherBlock m_IV;
CryptoPP::ECB_Mode<CryptoPP::AES>::Decryption m_ECBDecryption;
ECBDecryption m_ECBDecryption;
};
}
}
Expand Down

0 comments on commit 95013e9

Please sign in to comment.