2014-05-06 17:26:28 +00:00
|
|
|
#include <stdlib.h>
|
2014-05-06 16:22:22 +00:00
|
|
|
#include "aes.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace crypto
|
|
|
|
{
|
2014-05-07 18:48:37 +00:00
|
|
|
|
|
|
|
#ifdef __x86_64__
|
2014-05-09 01:43:08 +00:00
|
|
|
|
|
|
|
ECBCryptoAESNI::ECBCryptoAESNI ()
|
|
|
|
{
|
|
|
|
m_KeySchedule = m_UnalignedBuffer;
|
|
|
|
uint8_t rem = ((uint64_t)m_KeySchedule) & 0x0f;
|
|
|
|
if (rem)
|
|
|
|
m_KeySchedule += (16 - rem);
|
|
|
|
}
|
|
|
|
|
2014-05-07 18:48:37 +00:00
|
|
|
#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"
|
|
|
|
|
|
|
|
|
2014-05-08 19:11:38 +00:00
|
|
|
void ECBCryptoAESNI::ExpandKey (const uint8_t * key)
|
2014-05-07 18:48:37 +00:00
|
|
|
{
|
|
|
|
__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
|
2014-05-08 15:53:54 +00:00
|
|
|
"aeskeygenassist $16, %%xmm3, %%xmm2 \n"
|
2014-05-07 18:48:37 +00:00
|
|
|
KeyExpansion256
|
2014-05-08 15:53:54 +00:00
|
|
|
"aeskeygenassist $32, %%xmm3, %%xmm2 \n"
|
2014-05-07 18:48:37 +00:00
|
|
|
KeyExpansion256
|
2014-05-08 15:53:54 +00:00
|
|
|
"aeskeygenassist $64, %%xmm3, %%xmm2 \n"
|
2014-05-07 18:48:37 +00: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"
|
|
|
|
"movups %%xmm1, (%%rcx) \n"
|
|
|
|
: // output
|
|
|
|
: "S" (key), "D" (m_KeySchedule) // input
|
2014-05-08 19:11:38 +00:00
|
|
|
: "%rcx" // clogged
|
2014-05-07 18:48:37 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-05-08 19:11:38 +00:00
|
|
|
void ECBEncryptionAESNI::Encrypt (const ChipherBlock * in, ChipherBlock * out)
|
2014-05-07 19:39:30 +00:00
|
|
|
{
|
|
|
|
__asm__
|
|
|
|
(
|
|
|
|
"movups (%%rsi), %%xmm0 \n"
|
|
|
|
"pxor (%%rdx), %%xmm0 \n"
|
|
|
|
"aesenc 16(%%rdx), %%xmm0 \n"
|
|
|
|
"aesenc 32(%%rdx), %%xmm0 \n"
|
2014-05-08 20:16:42 +00:00
|
|
|
"aesenc 48(%%rdx), %%xmm0 \n"
|
2014-05-07 19:39:30 +00:00
|
|
|
"aesenc 64(%%rdx), %%xmm0 \n"
|
|
|
|
"aesenc 80(%%rdx), %%xmm0 \n"
|
|
|
|
"aesenc 96(%%rdx), %%xmm0 \n"
|
|
|
|
"aesenc 112(%%rdx), %%xmm0 \n"
|
|
|
|
"aesenc 128(%%rdx), %%xmm0 \n"
|
|
|
|
"aesenc 144(%%rdx), %%xmm0 \n"
|
|
|
|
"aesenc 160(%%rdx), %%xmm0 \n"
|
|
|
|
"aesenc 176(%%rdx), %%xmm0 \n"
|
|
|
|
"aesenc 192(%%rdx), %%xmm0 \n"
|
|
|
|
"aesenc 208(%%rdx), %%xmm0 \n"
|
|
|
|
"aesenclast 224(%%rdx), %%xmm0 \n"
|
|
|
|
"movups %%xmm0, (%%rdi) \n"
|
2014-05-08 19:11:38 +00:00
|
|
|
: : "d" (m_KeySchedule), "S" (in), "D" (out)
|
2014-05-07 19:39:30 +00:00
|
|
|
);
|
2014-05-08 19:11:38 +00:00
|
|
|
}
|
2014-05-07 19:39:30 +00:00
|
|
|
|
2014-05-08 19:11:38 +00:00
|
|
|
void ECBDecryptionAESNI::Decrypt (const ChipherBlock * in, ChipherBlock * out)
|
2014-05-07 19:39:30 +00:00
|
|
|
{
|
|
|
|
__asm__
|
|
|
|
(
|
|
|
|
"movups (%%rsi), %%xmm0 \n"
|
|
|
|
"pxor 224(%%rdx), %%xmm0 \n"
|
|
|
|
"aesdec 208(%%rdx), %%xmm0 \n"
|
|
|
|
"aesdec 192(%%rdx), %%xmm0 \n"
|
|
|
|
"aesdec 176(%%rdx), %%xmm0 \n"
|
|
|
|
"aesdec 160(%%rdx), %%xmm0 \n"
|
|
|
|
"aesdec 144(%%rdx), %%xmm0 \n"
|
|
|
|
"aesdec 128(%%rdx), %%xmm0 \n"
|
|
|
|
"aesdec 112(%%rdx), %%xmm0 \n"
|
|
|
|
"aesdec 96(%%rdx), %%xmm0 \n"
|
|
|
|
"aesdec 80(%%rdx), %%xmm0 \n"
|
|
|
|
"aesdec 64(%%rdx), %%xmm0 \n"
|
2014-05-08 20:16:42 +00:00
|
|
|
"aesdec 48(%%rdx), %%xmm0 \n"
|
2014-05-07 19:39:30 +00:00
|
|
|
"aesdec 32(%%rdx), %%xmm0 \n"
|
|
|
|
"aesdec 16(%%rdx), %%xmm0 \n"
|
|
|
|
"aesdeclast (%%rdx), %%xmm0 \n"
|
|
|
|
"movups %%xmm0, (%%rdi) \n"
|
2014-05-08 19:11:38 +00:00
|
|
|
: : "d" (m_KeySchedule), "S" (in), "D" (out)
|
2014-05-07 19:39:30 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-05-08 19:11:38 +00:00
|
|
|
#define CallAESIMC(offset) \
|
|
|
|
"movups "#offset"(%%rdx), %%xmm0 \n" \
|
|
|
|
"aesimc %%xmm0, %%xmm0 \n" \
|
|
|
|
"movups %%xmm0, "#offset"(%%rdx) \n"
|
|
|
|
|
|
|
|
void ECBDecryptionAESNI::SetKey (const uint8_t * 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)
|
|
|
|
: : "d" (m_KeySchedule)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-05-07 18:48:37 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2014-05-06 16:22:22 +00:00
|
|
|
void CBCEncryption::Encrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < numBlocks; i++)
|
|
|
|
{
|
|
|
|
m_LastBlock.ll[0] ^= in[i].ll[0];
|
|
|
|
m_LastBlock.ll[1] ^= in[i].ll[1];
|
2014-05-09 01:43:08 +00:00
|
|
|
m_ECBEncryption.Encrypt (&m_LastBlock, &m_LastBlock);
|
2014-05-06 16:22:22 +00:00
|
|
|
out[i] = m_LastBlock;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-06 17:26:28 +00:00
|
|
|
bool CBCEncryption::Encrypt (const uint8_t * in, std::size_t len, uint8_t * out)
|
|
|
|
{
|
|
|
|
div_t d = div (len, 16);
|
|
|
|
if (d.rem) return false; // len is not multipple of 16
|
|
|
|
Encrypt (d.quot, (const ChipherBlock *)in, (ChipherBlock *)out);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-06 16:22:22 +00:00
|
|
|
void CBCDecryption::Decrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < numBlocks; i++)
|
|
|
|
{
|
2014-05-06 17:26:28 +00:00
|
|
|
ChipherBlock tmp = in[i];
|
2014-05-09 01:43:08 +00:00
|
|
|
m_ECBDecryption.Decrypt (in + i, out + i);
|
2014-05-06 16:22:22 +00:00
|
|
|
out[i].ll[0] ^= m_IV.ll[0];
|
|
|
|
out[i].ll[1] ^= m_IV.ll[1];
|
2014-05-06 17:26:28 +00:00
|
|
|
m_IV = tmp;
|
2014-05-06 16:22:22 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-06 17:26:28 +00:00
|
|
|
|
|
|
|
bool CBCDecryption::Decrypt (const uint8_t * in, std::size_t len, uint8_t * out)
|
|
|
|
{
|
|
|
|
div_t d = div (len, 16);
|
|
|
|
if (d.rem) return false; // len is not multipple of 16
|
|
|
|
Decrypt (d.quot, (const ChipherBlock *)in, (ChipherBlock *)out);
|
|
|
|
return true;
|
|
|
|
}
|
2014-05-06 16:22:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|