From 0a53555ac7b347a1b6d792dc54a05f8f1e6ae3af Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 14 May 2014 14:54:01 -0400 Subject: [PATCH] one block CBC encryption --- NTCPSession.cpp | 4 ++-- aes.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ aes.h | 2 ++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/NTCPSession.cpp b/NTCPSession.cpp index 88e4eb0a..9930dead 100644 --- a/NTCPSession.cpp +++ b/NTCPSession.cpp @@ -430,7 +430,7 @@ namespace ntcp m_NextMessage = i2p::NewI2NPMessage (); m_NextMessageOffset = 0; - m_Decryption.Decrypt (encrypted, 16, m_NextMessage->buf); + m_Decryption.Decrypt (encrypted, m_NextMessage->buf); uint16_t dataSize = be16toh (*(uint16_t *)m_NextMessage->buf); if (dataSize) { @@ -450,7 +450,7 @@ namespace ntcp } else // message continues { - m_Decryption.Decrypt (encrypted, 16, m_NextMessage->buf + m_NextMessageOffset); + m_Decryption.Decrypt (encrypted, m_NextMessage->buf + m_NextMessageOffset); m_NextMessageOffset += 16; } diff --git a/aes.cpp b/aes.cpp index 3edef651..d1d45462 100644 --- a/aes.cpp +++ b/aes.cpp @@ -204,6 +204,27 @@ namespace crypto return true; } + void CBCEncryption::Encrypt (const uint8_t * in, uint8_t * out) + { +#ifdef __x86_64__ + __asm__ + ( + "movups (%[iv]), %%xmm1 \n" + "movups (%[in]), %%xmm0 \n" + "pxor %%xmm1, %%xmm0 \n" + EncryptAES256 + "movups %%xmm0, (%[out]) \n" + "movups %%xmm0, (%[iv]) \n" + : + : [iv]"r"(&m_LastBlock), [sched]"r"(m_ECBEncryption.GetKeySchedule ()), + [in]"r"(in), [out]"r"(out) + : "%xmm0", "%xmm1", "memory" + ); +#else + Encrypt (1, (const ChipherBlock *)in, (ChipherBlock *)out); +#endif + } + void CBCDecryption::Decrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out) { #ifdef __x86_64__ @@ -245,6 +266,27 @@ namespace crypto Decrypt (d.quot, (const ChipherBlock *)in, (ChipherBlock *)out); return true; } + + void CBCDecryption::Decrypt (const uint8_t * in, uint8_t * out) + { +#ifdef __x86_64__ + __asm__ + ( + "movups (%[iv]), %%xmm1 \n" + "movups (%[in]), %%xmm0 \n" + "movups %%xmm0, (%[iv]) \n" + DecryptAES256 + "pxor %%xmm1, %%xmm0 \n" + "movups %%xmm0, (%[out]) \n" + : + : [iv]"r"(&m_IV), [sched]"r"(m_ECBDecryption.GetKeySchedule ()), + [in]"r"(in), [out]"r"(out) + : "%xmm0", "%xmm1", "memory" + ); +#else + Decrypt (1, (const ChipherBlock *)in, (ChipherBlock *)out); +#endif + } } } diff --git a/aes.h b/aes.h index 5e3fcf4b..3cd905b6 100644 --- a/aes.h +++ b/aes.h @@ -111,6 +111,7 @@ namespace crypto void Encrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out); bool Encrypt (const uint8_t * in, std::size_t len, uint8_t * out); + void Encrypt (const uint8_t * in, uint8_t * out); // one block private: @@ -130,6 +131,7 @@ namespace crypto void Decrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out); bool Decrypt (const uint8_t * in, std::size_t len, uint8_t * out); + void Decrypt (const uint8_t * in, uint8_t * out); // one block private: