mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
223 lines
5.8 KiB
223 lines
5.8 KiB
11 years ago
|
#include <stdlib.h>
|
||
11 years ago
|
#include "aes.h"
|
||
|
|
||
10 years ago
|
namespace i2p {
|
||
|
namespace crypto {
|
||
11 years ago
|
|
||
11 years ago
|
#ifdef AESNI
|
||
10 years ago
|
#include "AESNIMacros.h"
|
||
11 years ago
|
|
||
10 years ago
|
void ECBCryptoAESNI::ExpandKey (const AESKey& key)
|
||
|
{
|
||
|
__asm__
|
||
|
(
|
||
|
"movups (%[key]), %%xmm1 \n"
|
||
|
"movups 16(%[key]), %%xmm3 \n"
|
||
|
"movaps %%xmm1, (%[sched]) \n"
|
||
|
"movaps %%xmm3, 16(%[sched]) \n"
|
||
|
"aeskeygenassist $1, %%xmm3, %%xmm2 \n"
|
||
|
KeyExpansion256(32,48)
|
||
|
"aeskeygenassist $2, %%xmm3, %%xmm2 \n"
|
||
|
KeyExpansion256(64,80)
|
||
|
"aeskeygenassist $4, %%xmm3, %%xmm2 \n"
|
||
|
KeyExpansion256(96,112)
|
||
|
"aeskeygenassist $8, %%xmm3, %%xmm2 \n"
|
||
|
KeyExpansion256(128,144)
|
||
|
"aeskeygenassist $16, %%xmm3, %%xmm2 \n"
|
||
|
KeyExpansion256(160,176)
|
||
|
"aeskeygenassist $32, %%xmm3, %%xmm2 \n"
|
||
|
KeyExpansion256(192,208)
|
||
|
"aeskeygenassist $64, %%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, 224(%[sched]) \n"
|
||
|
: // output
|
||
|
: [key]"r"((const uint8_t *)key), [sched]"r"(GetKeySchedule ()) // input
|
||
|
: "%xmm1", "%xmm2", "%xmm3", "%xmm4", "memory" // clogged
|
||
|
);
|
||
|
}
|
||
11 years ago
|
|
||
10 years ago
|
void ECBEncryptionAESNI::Encrypt (const CipherBlock * in, CipherBlock * out)
|
||
10 years ago
|
{
|
||
|
__asm__
|
||
|
(
|
||
|
"movups (%[in]), %%xmm0 \n"
|
||
|
EncryptAES256(sched)
|
||
|
"movups %%xmm0, (%[out]) \n"
|
||
|
: : [sched]"r"(GetKeySchedule ()), [in]"r"(in), [out]"r"(out) : "%xmm0", "memory"
|
||
|
);
|
||
|
}
|
||
11 years ago
|
|
||
|
|
||
10 years ago
|
void ECBDecryptionAESNI::Decrypt (const CipherBlock * in, CipherBlock * out)
|
||
10 years ago
|
{
|
||
|
__asm__
|
||
|
(
|
||
|
"movups (%[in]), %%xmm0 \n"
|
||
|
DecryptAES256(sched)
|
||
|
"movups %%xmm0, (%[out]) \n"
|
||
|
: : [sched]"r"(GetKeySchedule ()), [in]"r"(in), [out]"r"(out) : "%xmm0", "memory"
|
||
|
);
|
||
|
}
|
||
11 years ago
|
|
||
10 years ago
|
void ECBDecryptionAESNI::SetKey (const AESKey& 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)
|
||
|
: : [shed]"r"(GetKeySchedule ()) : "%xmm0", "memory"
|
||
|
);
|
||
|
}
|
||
11 years ago
|
|
||
10 years ago
|
#endif
|
||
11 years ago
|
|
||
|
|
||
10 years ago
|
void CBCEncryption::Encrypt (int numBlocks, const CipherBlock * in, CipherBlock * out)
|
||
10 years ago
|
{
|
||
11 years ago
|
#ifdef AESNI
|
||
10 years ago
|
__asm__
|
||
|
(
|
||
|
"movups (%[iv]), %%xmm1 \n"
|
||
|
"1: \n"
|
||
|
"movups (%[in]), %%xmm0 \n"
|
||
|
"pxor %%xmm1, %%xmm0 \n"
|
||
|
EncryptAES256(sched)
|
||
|
"movaps %%xmm0, %%xmm1 \n"
|
||
|
"movups %%xmm0, (%[out]) \n"
|
||
|
"add $16, %[in] \n"
|
||
|
"add $16, %[out] \n"
|
||
|
"dec %[num] \n"
|
||
|
"jnz 1b \n"
|
||
|
"movups %%xmm1, (%[iv]) \n"
|
||
|
:
|
||
|
: [iv]"r"(&m_LastBlock), [sched]"r"(m_ECBEncryption.GetKeySchedule ()),
|
||
|
[in]"r"(in), [out]"r"(out), [num]"r"(numBlocks)
|
||
|
: "%xmm0", "%xmm1", "cc", "memory"
|
||
|
);
|
||
10 years ago
|
#else
|
||
10 years ago
|
for (int i = 0; i < numBlocks; i++)
|
||
10 years ago
|
{
|
||
10 years ago
|
m_LastBlock ^= in[i];
|
||
|
m_ECBEncryption.Encrypt (&m_LastBlock, &m_LastBlock);
|
||
|
out[i] = m_LastBlock;
|
||
10 years ago
|
}
|
||
10 years ago
|
#endif
|
||
|
}
|
||
11 years ago
|
|
||
10 years ago
|
void CBCEncryption::Encrypt (const uint8_t * in, std::size_t len, uint8_t * out)
|
||
|
{
|
||
|
// len/16
|
||
|
int numBlocks = len >> 4;
|
||
|
if (numBlocks > 0)
|
||
10 years ago
|
Encrypt (numBlocks, (const CipherBlock *)in, (CipherBlock *)out);
|
||
10 years ago
|
}
|
||
|
|
||
|
void CBCEncryption::Encrypt (const uint8_t * in, uint8_t * out)
|
||
|
{
|
||
11 years ago
|
#ifdef AESNI
|
||
10 years ago
|
__asm__
|
||
|
(
|
||
|
"movups (%[iv]), %%xmm1 \n"
|
||
|
"movups (%[in]), %%xmm0 \n"
|
||
|
"pxor %%xmm1, %%xmm0 \n"
|
||
|
EncryptAES256(sched)
|
||
|
"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"
|
||
|
);
|
||
11 years ago
|
#else
|
||
10 years ago
|
Encrypt (1, (const CipherBlock *)in, (CipherBlock *)out);
|
||
11 years ago
|
#endif
|
||
10 years ago
|
}
|
||
11 years ago
|
|
||
10 years ago
|
void CBCDecryption::Decrypt (int numBlocks, const CipherBlock * in, CipherBlock * out)
|
||
10 years ago
|
{
|
||
11 years ago
|
#ifdef AESNI
|
||
10 years ago
|
__asm__
|
||
|
(
|
||
|
"movups (%[iv]), %%xmm1 \n"
|
||
|
"1: \n"
|
||
|
"movups (%[in]), %%xmm0 \n"
|
||
|
"movaps %%xmm0, %%xmm2 \n"
|
||
|
DecryptAES256(sched)
|
||
|
"pxor %%xmm1, %%xmm0 \n"
|
||
|
"movups %%xmm0, (%[out]) \n"
|
||
|
"movaps %%xmm2, %%xmm1 \n"
|
||
|
"add $16, %[in] \n"
|
||
|
"add $16, %[out] \n"
|
||
|
"dec %[num] \n"
|
||
|
"jnz 1b \n"
|
||
|
"movups %%xmm1, (%[iv]) \n"
|
||
|
:
|
||
|
: [iv]"r"(&m_IV), [sched]"r"(m_ECBDecryption.GetKeySchedule ()),
|
||
|
[in]"r"(in), [out]"r"(out), [num]"r"(numBlocks)
|
||
|
: "%xmm0", "%xmm1", "%xmm2", "cc", "memory"
|
||
|
);
|
||
11 years ago
|
#else
|
||
10 years ago
|
for (int i = 0; i < numBlocks; i++)
|
||
10 years ago
|
{
|
||
10 years ago
|
CipherBlock tmp = in[i];
|
||
10 years ago
|
m_ECBDecryption.Decrypt (in + i, out + i);
|
||
|
out[i] ^= m_IV;
|
||
|
m_IV = tmp;
|
||
10 years ago
|
}
|
||
11 years ago
|
#endif
|
||
10 years ago
|
}
|
||
11 years ago
|
|
||
10 years ago
|
void CBCDecryption::Decrypt (const uint8_t * in, std::size_t len, uint8_t * out)
|
||
|
{
|
||
|
int numBlocks = len >> 4;
|
||
|
if (numBlocks > 0)
|
||
10 years ago
|
Decrypt (numBlocks, (const CipherBlock *)in, (CipherBlock *)out);
|
||
10 years ago
|
}
|
||
11 years ago
|
|
||
10 years ago
|
void CBCDecryption::Decrypt (const uint8_t * in, uint8_t * out)
|
||
|
{
|
||
11 years ago
|
#ifdef AESNI
|
||
10 years ago
|
__asm__
|
||
|
(
|
||
|
"movups (%[iv]), %%xmm1 \n"
|
||
|
"movups (%[in]), %%xmm0 \n"
|
||
|
"movups %%xmm0, (%[iv]) \n"
|
||
|
DecryptAES256(sched)
|
||
|
"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"
|
||
|
);
|
||
11 years ago
|
#else
|
||
10 years ago
|
Decrypt (1, (const CipherBlock *)in, (CipherBlock *)out);
|
||
11 years ago
|
#endif
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
} // crypto
|
||
|
} // i2p
|
||
|
|