aes key expansion

This commit is contained in:
orignal 2014-05-07 14:48:37 -04:00
parent 6bf5d98c4d
commit 527ac413b1
2 changed files with 85 additions and 0 deletions

70
aes.cpp
View File

@ -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
View File

@ -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: