2014-05-06 21:26:28 +04:00
|
|
|
#include <stdlib.h>
|
2014-05-15 19:21:41 +04:00
|
|
|
#include "TunnelBase.h"
|
2014-05-06 20:22:22 +04:00
|
|
|
#include "aes.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace crypto
|
|
|
|
{
|
2014-05-07 22:48:37 +04:00
|
|
|
|
2014-06-02 18:05:04 +04:00
|
|
|
#ifdef AESNI
|
2014-05-09 05:43:08 +04:00
|
|
|
|
2014-05-13 19:03:33 +04:00
|
|
|
#define KeyExpansion256(round0,round1) \
|
2014-05-07 22:48:37 +04:00
|
|
|
"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" \
|
2014-05-13 19:03:33 +04:00
|
|
|
"movaps %%xmm1, "#round0"(%[sched]) \n" \
|
2014-05-07 22:48:37 +04:00
|
|
|
"aeskeygenassist $0, %%xmm1, %%xmm4 \n" \
|
|
|
|
"pshufd $0xaa, %%xmm4, %%xmm2 \n" \
|
|
|
|
"movaps %%xmm3, %%xmm4 \n" \
|
|
|
|
"pslldq $4, %%xmm4 \n" \
|
|
|
|
"pxor %%xmm4, %%xmm3 \n" \
|
2014-12-07 19:37:52 +03:00
|
|
|
"pslldq $4, %%xmm4 \n" \
|
2014-05-07 22:48:37 +04:00
|
|
|
"pxor %%xmm4, %%xmm3 \n" \
|
|
|
|
"pslldq $4, %%xmm4 \n" \
|
|
|
|
"pxor %%xmm4, %%xmm3 \n" \
|
|
|
|
"pxor %%xmm2, %%xmm3 \n" \
|
2014-05-13 19:03:33 +04:00
|
|
|
"movaps %%xmm3, "#round1"(%[sched]) \n"
|
2014-05-07 22:48:37 +04:00
|
|
|
|
2014-11-02 04:53:45 +03:00
|
|
|
void ECBCryptoAESNI::ExpandKey (const AESKey& key)
|
2014-05-07 22:48:37 +04:00
|
|
|
{
|
|
|
|
__asm__
|
|
|
|
(
|
2014-05-09 22:11:05 +04:00
|
|
|
"movups (%[key]), %%xmm1 \n"
|
|
|
|
"movups 16(%[key]), %%xmm3 \n"
|
2014-05-13 06:51:59 +04:00
|
|
|
"movaps %%xmm1, (%[sched]) \n"
|
|
|
|
"movaps %%xmm3, 16(%[sched]) \n"
|
2014-05-07 22:48:37 +04:00
|
|
|
"aeskeygenassist $1, %%xmm3, %%xmm2 \n"
|
2014-05-13 19:03:33 +04:00
|
|
|
KeyExpansion256(32,48)
|
2014-05-07 22:48:37 +04:00
|
|
|
"aeskeygenassist $2, %%xmm3, %%xmm2 \n"
|
2014-05-13 19:03:33 +04:00
|
|
|
KeyExpansion256(64,80)
|
2014-05-07 22:48:37 +04:00
|
|
|
"aeskeygenassist $4, %%xmm3, %%xmm2 \n"
|
2014-05-13 19:03:33 +04:00
|
|
|
KeyExpansion256(96,112)
|
2014-05-07 22:48:37 +04:00
|
|
|
"aeskeygenassist $8, %%xmm3, %%xmm2 \n"
|
2014-05-13 19:03:33 +04:00
|
|
|
KeyExpansion256(128,144)
|
2014-05-08 19:53:54 +04:00
|
|
|
"aeskeygenassist $16, %%xmm3, %%xmm2 \n"
|
2014-05-13 19:03:33 +04:00
|
|
|
KeyExpansion256(160,176)
|
2014-05-08 19:53:54 +04:00
|
|
|
"aeskeygenassist $32, %%xmm3, %%xmm2 \n"
|
2014-05-13 19:03:33 +04:00
|
|
|
KeyExpansion256(192,208)
|
2014-05-08 19:53:54 +04:00
|
|
|
"aeskeygenassist $64, %%xmm3, %%xmm2 \n"
|
2014-05-07 22:48:37 +04:00
|
|
|
// 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"
|
2014-05-13 19:03:33 +04:00
|
|
|
"movups %%xmm1, 224(%[sched]) \n"
|
2014-05-07 22:48:37 +04:00
|
|
|
: // output
|
2014-11-18 20:11:45 +03:00
|
|
|
: [key]"r"((const uint8_t *)key), [sched]"r"(GetKeySchedule ()) // input
|
2014-12-20 01:41:08 +03:00
|
|
|
: "%xmm1", "%xmm2", "%xmm3", "%xmm4", "memory" // clogged
|
2014-05-07 22:48:37 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-05-15 19:00:35 +04:00
|
|
|
#define EncryptAES256(sched) \
|
|
|
|
"pxor (%["#sched"]), %%xmm0 \n" \
|
|
|
|
"aesenc 16(%["#sched"]), %%xmm0 \n" \
|
|
|
|
"aesenc 32(%["#sched"]), %%xmm0 \n" \
|
|
|
|
"aesenc 48(%["#sched"]), %%xmm0 \n" \
|
|
|
|
"aesenc 64(%["#sched"]), %%xmm0 \n" \
|
|
|
|
"aesenc 80(%["#sched"]), %%xmm0 \n" \
|
|
|
|
"aesenc 96(%["#sched"]), %%xmm0 \n" \
|
|
|
|
"aesenc 112(%["#sched"]), %%xmm0 \n" \
|
|
|
|
"aesenc 128(%["#sched"]), %%xmm0 \n" \
|
|
|
|
"aesenc 144(%["#sched"]), %%xmm0 \n" \
|
|
|
|
"aesenc 160(%["#sched"]), %%xmm0 \n" \
|
|
|
|
"aesenc 176(%["#sched"]), %%xmm0 \n" \
|
|
|
|
"aesenc 192(%["#sched"]), %%xmm0 \n" \
|
|
|
|
"aesenc 208(%["#sched"]), %%xmm0 \n" \
|
|
|
|
"aesenclast 224(%["#sched"]), %%xmm0 \n"
|
2014-05-13 06:51:59 +04:00
|
|
|
|
2014-05-08 23:11:38 +04:00
|
|
|
void ECBEncryptionAESNI::Encrypt (const ChipherBlock * in, ChipherBlock * out)
|
2014-05-07 23:39:30 +04:00
|
|
|
{
|
|
|
|
__asm__
|
|
|
|
(
|
2014-05-09 22:11:05 +04:00
|
|
|
"movups (%[in]), %%xmm0 \n"
|
2014-05-15 19:00:35 +04:00
|
|
|
EncryptAES256(sched)
|
2014-05-09 22:11:05 +04:00
|
|
|
"movups %%xmm0, (%[out]) \n"
|
2014-12-20 01:41:08 +03:00
|
|
|
: : [sched]"r"(GetKeySchedule ()), [in]"r"(in), [out]"r"(out) : "%xmm0", "memory"
|
2014-05-07 23:39:30 +04:00
|
|
|
);
|
2014-05-08 23:11:38 +04:00
|
|
|
}
|
2014-05-07 23:39:30 +04:00
|
|
|
|
2014-05-15 19:00:35 +04:00
|
|
|
#define DecryptAES256(sched) \
|
|
|
|
"pxor 224(%["#sched"]), %%xmm0 \n" \
|
|
|
|
"aesdec 208(%["#sched"]), %%xmm0 \n" \
|
|
|
|
"aesdec 192(%["#sched"]), %%xmm0 \n" \
|
|
|
|
"aesdec 176(%["#sched"]), %%xmm0 \n" \
|
|
|
|
"aesdec 160(%["#sched"]), %%xmm0 \n" \
|
|
|
|
"aesdec 144(%["#sched"]), %%xmm0 \n" \
|
|
|
|
"aesdec 128(%["#sched"]), %%xmm0 \n" \
|
|
|
|
"aesdec 112(%["#sched"]), %%xmm0 \n" \
|
|
|
|
"aesdec 96(%["#sched"]), %%xmm0 \n" \
|
|
|
|
"aesdec 80(%["#sched"]), %%xmm0 \n" \
|
|
|
|
"aesdec 64(%["#sched"]), %%xmm0 \n" \
|
|
|
|
"aesdec 48(%["#sched"]), %%xmm0 \n" \
|
|
|
|
"aesdec 32(%["#sched"]), %%xmm0 \n" \
|
|
|
|
"aesdec 16(%["#sched"]), %%xmm0 \n" \
|
|
|
|
"aesdeclast (%["#sched"]), %%xmm0 \n"
|
2014-05-13 06:51:59 +04:00
|
|
|
|
2014-05-08 23:11:38 +04:00
|
|
|
void ECBDecryptionAESNI::Decrypt (const ChipherBlock * in, ChipherBlock * out)
|
2014-05-07 23:39:30 +04:00
|
|
|
{
|
|
|
|
__asm__
|
|
|
|
(
|
2014-05-09 22:11:05 +04:00
|
|
|
"movups (%[in]), %%xmm0 \n"
|
2014-05-15 19:00:35 +04:00
|
|
|
DecryptAES256(sched)
|
2014-05-09 22:11:05 +04:00
|
|
|
"movups %%xmm0, (%[out]) \n"
|
2014-12-20 01:41:08 +03:00
|
|
|
: : [sched]"r"(GetKeySchedule ()), [in]"r"(in), [out]"r"(out) : "%xmm0", "memory"
|
2014-05-07 23:39:30 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-05-08 23:11:38 +04:00
|
|
|
#define CallAESIMC(offset) \
|
2014-05-09 22:11:05 +04:00
|
|
|
"movaps "#offset"(%[shed]), %%xmm0 \n" \
|
2014-05-08 23:11:38 +04:00
|
|
|
"aesimc %%xmm0, %%xmm0 \n" \
|
2014-05-09 22:11:05 +04:00
|
|
|
"movaps %%xmm0, "#offset"(%[shed]) \n"
|
2014-05-08 23:11:38 +04:00
|
|
|
|
2014-11-02 04:53:45 +03:00
|
|
|
void ECBDecryptionAESNI::SetKey (const AESKey& key)
|
2014-05-08 23:11:38 +04:00
|
|
|
{
|
|
|
|
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)
|
2014-12-20 01:41:08 +03:00
|
|
|
: : [shed]"r"(GetKeySchedule ()) : "%xmm0", "memory"
|
2014-05-08 23:11:38 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-05-07 22:48:37 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2014-05-06 20:22:22 +04:00
|
|
|
void CBCEncryption::Encrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out)
|
|
|
|
{
|
2014-06-02 18:05:04 +04:00
|
|
|
#ifdef AESNI
|
2014-05-13 06:51:59 +04:00
|
|
|
__asm__
|
|
|
|
(
|
|
|
|
"movups (%[iv]), %%xmm1 \n"
|
2014-12-07 19:37:52 +03:00
|
|
|
"1: \n"
|
2014-05-13 06:51:59 +04:00
|
|
|
"movups (%[in]), %%xmm0 \n"
|
|
|
|
"pxor %%xmm1, %%xmm0 \n"
|
2014-05-15 19:00:35 +04:00
|
|
|
EncryptAES256(sched)
|
2014-05-13 06:51:59 +04:00
|
|
|
"movaps %%xmm0, %%xmm1 \n"
|
|
|
|
"movups %%xmm0, (%[out]) \n"
|
|
|
|
"add $16, %[in] \n"
|
|
|
|
"add $16, %[out] \n"
|
|
|
|
"dec %[num] \n"
|
2014-12-07 19:37:52 +03:00
|
|
|
"jnz 1b \n"
|
2014-05-13 06:51:59 +04:00
|
|
|
"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"
|
|
|
|
);
|
|
|
|
#else
|
2014-05-06 20:22:22 +04:00
|
|
|
for (int i = 0; i < numBlocks; i++)
|
|
|
|
{
|
2014-05-09 20:05:04 +04:00
|
|
|
m_LastBlock ^= in[i];
|
2014-05-09 05:43:08 +04:00
|
|
|
m_ECBEncryption.Encrypt (&m_LastBlock, &m_LastBlock);
|
2014-05-06 20:22:22 +04:00
|
|
|
out[i] = m_LastBlock;
|
|
|
|
}
|
2014-05-13 06:51:59 +04:00
|
|
|
#endif
|
2014-05-06 20:22:22 +04:00
|
|
|
}
|
|
|
|
|
2014-06-08 15:56:04 +04:00
|
|
|
void CBCEncryption::Encrypt (const uint8_t * in, std::size_t len, uint8_t * out)
|
2014-05-06 21:26:28 +04:00
|
|
|
{
|
2014-06-08 15:56:04 +04:00
|
|
|
// len/16
|
2015-03-30 18:56:24 +03:00
|
|
|
int numBlocks = len >> 4;
|
|
|
|
if (numBlocks > 0)
|
|
|
|
Encrypt (numBlocks, (const ChipherBlock *)in, (ChipherBlock *)out);
|
2014-05-06 21:26:28 +04:00
|
|
|
}
|
|
|
|
|
2014-05-14 22:54:01 +04:00
|
|
|
void CBCEncryption::Encrypt (const uint8_t * in, uint8_t * out)
|
|
|
|
{
|
2014-06-02 18:05:04 +04:00
|
|
|
#ifdef AESNI
|
2014-05-14 22:54:01 +04:00
|
|
|
__asm__
|
|
|
|
(
|
|
|
|
"movups (%[iv]), %%xmm1 \n"
|
|
|
|
"movups (%[in]), %%xmm0 \n"
|
|
|
|
"pxor %%xmm1, %%xmm0 \n"
|
2014-05-15 19:00:35 +04:00
|
|
|
EncryptAES256(sched)
|
2014-05-14 22:54:01 +04:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2014-05-06 20:22:22 +04:00
|
|
|
void CBCDecryption::Decrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out)
|
|
|
|
{
|
2014-06-02 18:05:04 +04:00
|
|
|
#ifdef AESNI
|
2014-05-13 21:43:07 +04:00
|
|
|
__asm__
|
|
|
|
(
|
|
|
|
"movups (%[iv]), %%xmm1 \n"
|
2014-12-07 19:37:52 +03:00
|
|
|
"1: \n"
|
2014-05-13 21:43:07 +04:00
|
|
|
"movups (%[in]), %%xmm0 \n"
|
|
|
|
"movaps %%xmm0, %%xmm2 \n"
|
2014-05-15 19:00:35 +04:00
|
|
|
DecryptAES256(sched)
|
2014-05-13 21:43:07 +04:00
|
|
|
"pxor %%xmm1, %%xmm0 \n"
|
|
|
|
"movups %%xmm0, (%[out]) \n"
|
|
|
|
"movaps %%xmm2, %%xmm1 \n"
|
|
|
|
"add $16, %[in] \n"
|
|
|
|
"add $16, %[out] \n"
|
|
|
|
"dec %[num] \n"
|
2014-12-07 19:37:52 +03:00
|
|
|
"jnz 1b \n"
|
2014-05-13 21:43:07 +04:00
|
|
|
"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"
|
|
|
|
);
|
|
|
|
#else
|
2014-05-06 20:22:22 +04:00
|
|
|
for (int i = 0; i < numBlocks; i++)
|
|
|
|
{
|
2014-05-06 21:26:28 +04:00
|
|
|
ChipherBlock tmp = in[i];
|
2014-05-09 05:43:08 +04:00
|
|
|
m_ECBDecryption.Decrypt (in + i, out + i);
|
2014-05-09 20:05:04 +04:00
|
|
|
out[i] ^= m_IV;
|
2014-05-06 21:26:28 +04:00
|
|
|
m_IV = tmp;
|
2014-05-06 20:22:22 +04:00
|
|
|
}
|
2014-05-13 21:43:07 +04:00
|
|
|
#endif
|
2014-05-06 20:22:22 +04:00
|
|
|
}
|
2014-05-06 21:26:28 +04:00
|
|
|
|
2014-06-08 15:56:04 +04:00
|
|
|
void CBCDecryption::Decrypt (const uint8_t * in, std::size_t len, uint8_t * out)
|
2014-05-06 21:26:28 +04:00
|
|
|
{
|
2015-03-30 18:56:24 +03:00
|
|
|
int numBlocks = len >> 4;
|
|
|
|
if (numBlocks > 0)
|
|
|
|
Decrypt (numBlocks, (const ChipherBlock *)in, (ChipherBlock *)out);
|
2014-05-06 21:26:28 +04:00
|
|
|
}
|
2014-05-14 22:54:01 +04:00
|
|
|
|
|
|
|
void CBCDecryption::Decrypt (const uint8_t * in, uint8_t * out)
|
|
|
|
{
|
2014-06-02 18:05:04 +04:00
|
|
|
#ifdef AESNI
|
2014-05-14 22:54:01 +04:00
|
|
|
__asm__
|
|
|
|
(
|
|
|
|
"movups (%[iv]), %%xmm1 \n"
|
|
|
|
"movups (%[in]), %%xmm0 \n"
|
|
|
|
"movups %%xmm0, (%[iv]) \n"
|
2014-05-15 19:00:35 +04:00
|
|
|
DecryptAES256(sched)
|
2014-05-14 22:54:01 +04:00
|
|
|
"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
|
|
|
|
}
|
2014-05-15 19:21:41 +04:00
|
|
|
|
|
|
|
void TunnelEncryption::Encrypt (uint8_t * payload)
|
|
|
|
{
|
2014-06-02 18:05:04 +04:00
|
|
|
#ifdef AESNI
|
2014-05-15 20:59:07 +04:00
|
|
|
__asm__
|
|
|
|
(
|
|
|
|
// encrypt IV
|
|
|
|
"movups (%[payload]), %%xmm0 \n"
|
|
|
|
EncryptAES256(sched_iv)
|
|
|
|
"movaps %%xmm0, %%xmm1 \n"
|
|
|
|
// double IV encryption
|
|
|
|
EncryptAES256(sched_iv)
|
|
|
|
"movups %%xmm0, (%[payload]) \n"
|
|
|
|
// encrypt data, IV is xmm1
|
2014-12-07 19:37:52 +03:00
|
|
|
"1: \n"
|
2014-05-15 20:59:07 +04:00
|
|
|
"add $16, %[payload] \n"
|
|
|
|
"movups (%[payload]), %%xmm0 \n"
|
|
|
|
"pxor %%xmm1, %%xmm0 \n"
|
|
|
|
EncryptAES256(sched_l)
|
|
|
|
"movaps %%xmm0, %%xmm1 \n"
|
|
|
|
"movups %%xmm0, (%[payload]) \n"
|
|
|
|
"dec %[num] \n"
|
2014-12-07 19:37:52 +03:00
|
|
|
"jnz 1b \n"
|
2014-05-15 20:59:07 +04:00
|
|
|
:
|
|
|
|
: [sched_iv]"r"(m_IVEncryption.GetKeySchedule ()), [sched_l]"r"(m_LayerEncryption.GetKeySchedule ()),
|
|
|
|
[payload]"r"(payload), [num]"r"(63) // 63 blocks = 1008 bytes
|
|
|
|
: "%xmm0", "%xmm1", "cc", "memory"
|
|
|
|
);
|
|
|
|
#else
|
2014-05-15 19:21:41 +04:00
|
|
|
m_IVEncryption.Encrypt ((ChipherBlock *)payload, (ChipherBlock *)payload); // iv
|
2014-06-27 18:56:33 +04:00
|
|
|
m_LayerEncryption.SetIV (payload);
|
2014-05-15 19:21:41 +04:00
|
|
|
m_LayerEncryption.Encrypt (payload + 16, i2p::tunnel::TUNNEL_DATA_ENCRYPTED_SIZE, payload + 16); // data
|
|
|
|
m_IVEncryption.Encrypt ((ChipherBlock *)payload, (ChipherBlock *)payload); // double iv
|
2014-05-15 20:59:07 +04:00
|
|
|
#endif
|
2014-05-15 19:21:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TunnelDecryption::Decrypt (uint8_t * payload)
|
|
|
|
{
|
2014-06-02 18:05:04 +04:00
|
|
|
#ifdef AESNI
|
2014-05-15 21:10:07 +04:00
|
|
|
__asm__
|
|
|
|
(
|
|
|
|
// decrypt IV
|
|
|
|
"movups (%[payload]), %%xmm0 \n"
|
|
|
|
DecryptAES256(sched_iv)
|
|
|
|
"movaps %%xmm0, %%xmm1 \n"
|
|
|
|
// double IV encryption
|
|
|
|
DecryptAES256(sched_iv)
|
|
|
|
"movups %%xmm0, (%[payload]) \n"
|
|
|
|
// decrypt data, IV is xmm1
|
2014-12-07 19:37:52 +03:00
|
|
|
"1: \n"
|
2014-05-15 21:10:07 +04:00
|
|
|
"add $16, %[payload] \n"
|
|
|
|
"movups (%[payload]), %%xmm0 \n"
|
|
|
|
"movaps %%xmm0, %%xmm2 \n"
|
|
|
|
DecryptAES256(sched_l)
|
|
|
|
"pxor %%xmm1, %%xmm0 \n"
|
|
|
|
"movups %%xmm0, (%[payload]) \n"
|
|
|
|
"movaps %%xmm2, %%xmm1 \n"
|
|
|
|
"dec %[num] \n"
|
2014-12-07 19:37:52 +03:00
|
|
|
"jnz 1b \n"
|
2014-05-15 21:10:07 +04:00
|
|
|
:
|
|
|
|
: [sched_iv]"r"(m_IVDecryption.GetKeySchedule ()), [sched_l]"r"(m_LayerDecryption.GetKeySchedule ()),
|
|
|
|
[payload]"r"(payload), [num]"r"(63) // 63 blocks = 1008 bytes
|
|
|
|
: "%xmm0", "%xmm1", "%xmm2", "cc", "memory"
|
|
|
|
);
|
|
|
|
#else
|
2014-05-15 19:21:41 +04:00
|
|
|
m_IVDecryption.Decrypt ((ChipherBlock *)payload, (ChipherBlock *)payload); // iv
|
2014-06-27 18:56:33 +04:00
|
|
|
m_LayerDecryption.SetIV (payload);
|
2014-05-15 19:21:41 +04:00
|
|
|
m_LayerDecryption.Decrypt (payload + 16, i2p::tunnel::TUNNEL_DATA_ENCRYPTED_SIZE, payload + 16); // data
|
|
|
|
m_IVDecryption.Decrypt ((ChipherBlock *)payload, (ChipherBlock *)payload); // double iv
|
2014-05-15 21:10:07 +04:00
|
|
|
#endif
|
2014-05-15 19:21:41 +04:00
|
|
|
}
|
2014-05-06 20:22:22 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|