Browse Source

aes key expansion

pull/72/head
orignal 11 years ago
parent
commit
527ac413b1
  1. 70
      aes.cpp
  2. 15
      aes.h

70
aes.cpp

@ -5,6 +5,76 @@ namespace i2p @@ -5,6 +5,76 @@ namespace i2p
{
namespace crypto
{
#ifdef __x86_64__
#define KeyExpansion256 \
"pshufd $0xff, %%xmm2, %%xmm2 \n" \
"movaps %%xmm1, %%xmm4 \n" \
"pslldq $4, %%xmm4 \n" \
"pxor %%xmm4, %%xmm1 \n" \
"pslldq $4, %%xmm4 \n" \
"pxor %%xmm4, %%xmm1 \n" \
"pslldq $4, %%xmm4 \n" \
"pxor %%xmm4, %%xmm1 \n" \
"pxor %%xmm2, %%xmm1 \n" \
"movups %%xmm1, (%%rcx) \n" \
"aeskeygenassist $0, %%xmm1, %%xmm4 \n" \
"pshufd $0xaa, %%xmm4, %%xmm2 \n" \
"movaps %%xmm3, %%xmm4 \n" \
"pslldq $4, %%xmm4 \n" \
"pxor %%xmm4, %%xmm3 \n" \
"pslldq $4, %%xmm4 \n" \
"pxor %%xmm4, %%xmm3 \n" \
"pslldq $4, %%xmm4 \n" \
"pxor %%xmm4, %%xmm3 \n" \
"pxor %%xmm2, %%xmm3 \n" \
"movups %%xmm3, 16(%%rcx) \n" \
"add $32, %%rcx \n"
void ECNEncryptionAESNI::SetKey (const uint8_t * key)
{
__asm__
(
"movups (%%rsi), %%xmm1 \n"
"movups 16(%%rsi), %%xmm3 \n"
"movups %%xmm1, (%%rdi) \n"
"movups %%xmm3, 16(%%rdi) \n"
"lea 32(%%rdi), %%rcx \n"
"aeskeygenassist $1, %%xmm3, %%xmm2 \n"
KeyExpansion256
"aeskeygenassist $2, %%xmm3, %%xmm2 \n"
KeyExpansion256
"aeskeygenassist $4, %%xmm3, %%xmm2 \n"
KeyExpansion256
"aeskeygenassist $8, %%xmm3, %%xmm2 \n"
KeyExpansion256
"aeskeygenassist $10, %%xmm3, %%xmm2 \n"
KeyExpansion256
"aeskeygenassist $20, %%xmm3, %%xmm2 \n"
KeyExpansion256
"aeskeygenassist $40, %%xmm3, %%xmm2 \n"
// key expansion final
"pshufd $0xff, %%xmm2, %%xmm2 \n"
"movaps %%xmm1, %%xmm4 \n"
"pslldq $4, %%xmm4 \n"
"pxor %%xmm4, %%xmm1 \n"
"pslldq $4, %%xmm4 \n"
"pxor %%xmm4, %%xmm1 \n"
"pslldq $4, %%xmm4 \n"
"pxor %%xmm4, %%xmm1 \n"
"pxor %%xmm2, %%xmm1 \n"
"movups %%xmm1, (%%rcx) \n"
: // output
: "S" (key), "D" (m_KeySchedule) // input
: "%rcx" // modified
);
}
#endif
void CBCEncryption::Encrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out)
{
for (int i = 0; i < numBlocks; i++)

15
aes.h

@ -15,6 +15,21 @@ namespace crypto @@ -15,6 +15,21 @@ namespace crypto
uint64_t ll[2];
};
#ifdef __x86_64__
// AES-NI assumed
class ECNEncryptionAESNI
{
public:
void SetKey (const uint8_t * key);
private:
uint32_t m_KeySchedule[4*(14+1)]; // 14 rounds for AES-256
};
#endif
class CBCEncryption
{
public:

Loading…
Cancel
Save