Browse Source

invert expanded key schedule for decryption

pull/72/head
orignal 11 years ago
parent
commit
4ef971f63c
  1. 42
      aes.cpp
  2. 24
      aes.h

42
aes.cpp

@ -33,7 +33,7 @@ namespace crypto
"add $32, %%rcx \n" "add $32, %%rcx \n"
void ECBCryptoAESNI::SetKey (const uint8_t * key) void ECBCryptoAESNI::ExpandKey (const uint8_t * key)
{ {
__asm__ __asm__
( (
@ -68,11 +68,11 @@ namespace crypto
"movups %%xmm1, (%%rcx) \n" "movups %%xmm1, (%%rcx) \n"
: // output : // output
: "S" (key), "D" (m_KeySchedule) // input : "S" (key), "D" (m_KeySchedule) // input
: "%rcx" // modified : "%rcx" // clogged
); );
} }
void ECBCryptoAESNI::Encrypt (const ChipherBlock * in, ChipherBlock * out) void ECBEncryptionAESNI::Encrypt (const ChipherBlock * in, ChipherBlock * out)
{ {
__asm__ __asm__
( (
@ -92,11 +92,11 @@ namespace crypto
"aesenc 208(%%rdx), %%xmm0 \n" "aesenc 208(%%rdx), %%xmm0 \n"
"aesenclast 224(%%rdx), %%xmm0 \n" "aesenclast 224(%%rdx), %%xmm0 \n"
"movups %%xmm0, (%%rdi) \n" "movups %%xmm0, (%%rdi) \n"
: : "d" ((uint64_t)m_KeySchedule), "S" (in), "D" (out) : : "d" (m_KeySchedule), "S" (in), "D" (out)
); );
} }
void ECBCryptoAESNI::Decrypt (const ChipherBlock * in, ChipherBlock * out) void ECBDecryptionAESNI::Decrypt (const ChipherBlock * in, ChipherBlock * out)
{ {
__asm__ __asm__
( (
@ -116,10 +116,38 @@ namespace crypto
"aesdec 16(%%rdx), %%xmm0 \n" "aesdec 16(%%rdx), %%xmm0 \n"
"aesdeclast (%%rdx), %%xmm0 \n" "aesdeclast (%%rdx), %%xmm0 \n"
"movups %%xmm0, (%%rdi) \n" "movups %%xmm0, (%%rdi) \n"
: : "d" ((uint64_t)m_KeySchedule), "S" (in), "D" (out) : : "d" (m_KeySchedule), "S" (in), "D" (out)
); );
} }
#define CallAESIMC(offset) \
"movups "#offset"(%%rdx), %%xmm0 \n" \
"aesimc %%xmm0, %%xmm0 \n" \
"movups %%xmm0, "#offset"(%%rdx) \n"
void ECBDecryptionAESNI::SetKey (const uint8_t * key)
{
ExpandKey (key); // expand encryption key first
// then invert it using aesimc
__asm__
(
CallAESIMC(16)
CallAESIMC(32)
CallAESIMC(48)
CallAESIMC(64)
CallAESIMC(80)
CallAESIMC(96)
CallAESIMC(112)
CallAESIMC(128)
CallAESIMC(144)
CallAESIMC(160)
CallAESIMC(176)
CallAESIMC(192)
CallAESIMC(208)
: : "d" (m_KeySchedule)
);
}
#endif #endif

24
aes.h

@ -18,16 +18,30 @@ namespace crypto
#ifdef __x86_64__ #ifdef __x86_64__
// AES-NI assumed // AES-NI assumed
class ECBCryptoAESNI class ECBCryptoAESNI
{
protected:
void ExpandKey (const uint8_t * key);
protected:
uint32_t m_KeySchedule[4*(14+1)]; // 14 rounds for AES-256
};
class ECBEncryptionAESNI: public ECBCryptoAESNI
{ {
public: public:
void SetKey (const uint8_t * key); void SetKey (const uint8_t * key) { ExpandKey (key); };
void Encrypt (const ChipherBlock * in, ChipherBlock * out); void Encrypt (const ChipherBlock * in, ChipherBlock * out);
void Decrypt (const ChipherBlock * in, ChipherBlock * out); };
private:
uint32_t m_KeySchedule[4*(14+1)]; // 14 rounds for AES-256 class ECBDecryptionAESNI: public ECBCryptoAESNI
{
public:
void SetKey (const uint8_t * key);
void Decrypt (const ChipherBlock * in, ChipherBlock * out);
}; };
#endif #endif

Loading…
Cancel
Save