diff --git a/aes.cpp b/aes.cpp index 14a5b762..2eedf0ad 100644 --- a/aes.cpp +++ b/aes.cpp @@ -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++) diff --git a/aes.h b/aes.h index 4cfc37f6..365c01fd 100644 --- a/aes.h +++ b/aes.h @@ -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: