Skip to content

Commit

Permalink
1.0.0.9
Browse files Browse the repository at this point in the history
  • Loading branch information
QRCS-CORP committed Nov 26, 2020
1 parent c1c937f commit 0296b09
Show file tree
Hide file tree
Showing 137 changed files with 52,744 additions and 3,091 deletions.
101 changes: 31 additions & 70 deletions CEX/AHX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class AHX::AhxState

SecureVector<byte> Custom;
std::vector<__m128i> RoundKeys;
#if defined(CEX_HAS_AVX512)
std::vector<__m512i> RoundKeysW;
#endif
size_t Rounds;
BlockCipherExtensions Extension;
bool Destroyed;
Expand All @@ -39,6 +42,9 @@ class AHX::AhxState
{
MemoryTools::Clear(Custom, 0, Custom.size());
MemoryTools::Clear(RoundKeys, 0, RoundKeys.size() * sizeof(uint));
#if defined(CEX_HAS_AVX512)
MemoryTools::Clear(RoundKeysW, 0, RoundKeysW.size() * sizeof(__m512i));
#endif
Rounds = 0;
Extension = BlockCipherExtensions::None;
Destroyed = false;
Expand All @@ -50,6 +56,9 @@ class AHX::AhxState
{
MemoryTools::Clear(Custom, 0, Custom.size());
MemoryTools::Clear(RoundKeys, 0, RoundKeys.size() * sizeof(uint));
#if defined(CEX_HAS_AVX512)
MemoryTools::Clear(RoundKeysW, 0, RoundKeysW.size() * sizeof(__m512i));
#endif
Encryption = false;
Initialized = false;
}
Expand Down Expand Up @@ -255,6 +264,17 @@ void AHX::Initialize(bool Encryption, ISymmetricKey &Parameters)
m_ahxState->RoundKeys[i] = _mm_aesimc_si128(m_ahxState->RoundKeys[i]);
}

#if defined(CEX_HAS_AVX512)
size_t i;

m_ahxState->RoundKeysW.resize(m_ahxState->RoundKeys.size());

for (i = 0; i < m_ahxState->RoundKeys.size(); ++i)
{
m_ahxState->RoundKeysW[i] = Load128To512(m_ahxState->RoundKeys[i]);
}
#endif

// ready to transform data
m_ahxState->Encryption = Encryption;
m_ahxState->Initialized = true;
Expand Down Expand Up @@ -528,31 +548,8 @@ void AHX::Decrypt128(const std::vector<byte> &Input, size_t InOffset, std::vecto

void AHX::Decrypt256(const std::vector<byte> &Input, size_t InOffset, std::vector<byte> &Output, size_t OutOffset)
{
#if defined(CEX_EXTENDED_AESNI)

const size_t RNDCNT = m_ahxState->RoundKeys.size() - 2;
size_t kctr;
__m256i x;

kctr = 0;
x = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(&Input[InOffset]));
x = _mm256_xor_si256(x, Load128To256(m_ahxState->RoundKeys[kctr], m_ahxState->RoundKeys[kctr]));

while (kctr != RNDCNT)
{
++kctr;
x = _mm256_aesdec_epi128(x, Load128To256(m_ahxState->RoundKeys[kctr], m_ahxState->RoundKeys[kctr]));
}

++kctr;
_mm256_storeu_si256(reinterpret_cast<__m256i*>(&Output[OutOffset]), _mm256_aesdeclast_epi128(x, Load128To256(m_ahxState->RoundKeys[kctr], m_ahxState->RoundKeys[kctr])));

#else

Decrypt128(Input, InOffset, Output, OutOffset);
Decrypt128(Input, InOffset + 16, Output, OutOffset + 16);

#endif
}

void AHX::Decrypt512(const std::vector<byte> &Input, size_t InOffset, std::vector<byte> &Output, size_t OutOffset)
Expand All @@ -565,16 +562,16 @@ void AHX::Decrypt512(const std::vector<byte> &Input, size_t InOffset, std::vecto

kctr = 0;
x = _mm512_loadu_si512(reinterpret_cast<const __m512i*>(&Input[InOffset]));
x = _mm512_xor_si512(x, Load128To512(m_ahxState->RoundKeys[kctr], m_ahxState->RoundKeys[kctr], m_ahxState->RoundKeys[kctr], m_ahxState->RoundKeys[kctr]));
x = _mm512_xor_si512(x, m_ahxState->RoundKeysW[kctr]);

while (kctr != RNDCNT)
{
++kctr;
x = _mm512_aesdec_epi128(x, Load128To512(m_ahxState->RoundKeys[kctr], m_ahxState->RoundKeys[kctr], m_ahxState->RoundKeys[kctr], m_ahxState->RoundKeys[kctr]));
x = _mm512_aesdec_epi128(x, m_ahxState->RoundKeysW[kctr]);
}

++kctr;
_mm512_storeu_si512(reinterpret_cast<__m512i*>(&Output[OutOffset]), _mm512_aesdeclast_epi128(x, Load128To512(m_ahxState->RoundKeys[kctr], m_ahxState->RoundKeys[kctr], m_ahxState->RoundKeys[kctr], m_ahxState->RoundKeys[kctr])));
_mm512_storeu_si512(reinterpret_cast<__m512i*>(&Output[OutOffset]), _mm512_aesdeclast_epi128(x, m_ahxState->RoundKeysW[kctr]));

#else

Expand Down Expand Up @@ -617,31 +614,8 @@ void AHX::Encrypt128(const std::vector<byte> &Input, size_t InOffset, std::vecto

void AHX::Encrypt256(const std::vector<byte> &Input, size_t InOffset, std::vector<byte> &Output, size_t OutOffset)
{
#if defined(CEX_EXTENDED_AESNI)

const size_t RNDCNT = m_ahxState->RoundKeys.size() - 2;
size_t kctr;
__m256i x;

kctr = 0;
x = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(&Input[InOffset]));
x = _mm256_xor_si256(x, Load128To256(m_ahxState->RoundKeys[kctr], m_ahxState->RoundKeys[kctr]));

while (kctr != RNDCNT)
{
++kctr;
x = _mm256_aesenc_epi128(x, Load128To256(m_ahxState->RoundKeys[kctr], m_ahxState->RoundKeys[kctr]));
}

++kctr;
_mm256_storeu_si256(reinterpret_cast<__m256i*>(&Output[OutOffset]), _mm256_aesenclast_epi128(x, Load128To256(m_ahxState->RoundKeys[kctr], m_ahxState->RoundKeys[kctr])));

#else

Encrypt128(Input, InOffset, Output, OutOffset);
Encrypt128(Input, InOffset + 16, Output, OutOffset + 16);

#endif
}

void AHX::Encrypt512(const std::vector<byte> &Input, size_t InOffset, std::vector<byte> &Output, size_t OutOffset)
Expand All @@ -654,16 +628,16 @@ void AHX::Encrypt512(const std::vector<byte> &Input, size_t InOffset, std::vecto

kctr = 0;
x = _mm512_loadu_si512(reinterpret_cast<const __m512i*>(&Input[InOffset]));
x = _mm512_xor_si512(x, Load128To512(m_ahxState->RoundKeys[kctr], m_ahxState->RoundKeys[kctr], m_ahxState->RoundKeys[kctr], m_ahxState->RoundKeys[kctr]));
x = _mm512_xor_si512(x, m_ahxState->RoundKeysW[kctr]);

while (kctr != RNDCNT)
{
++kctr;
x = _mm512_aesenc_epi128(x, Load128To512(m_ahxState->RoundKeys[kctr], m_ahxState->RoundKeys[kctr], m_ahxState->RoundKeys[kctr], m_ahxState->RoundKeys[kctr]));
x = _mm512_aesenc_epi128(x, m_ahxState->RoundKeysW[kctr]);
}

++kctr;
_mm512_storeu_si512(reinterpret_cast<__m512i*>(&Output[OutOffset]), _mm512_aesenclast_epi128(x, Load128To512(m_ahxState->RoundKeys[kctr], m_ahxState->RoundKeys[kctr], m_ahxState->RoundKeys[kctr], m_ahxState->RoundKeys[kctr])));
_mm512_storeu_si512(reinterpret_cast<__m512i*>(&Output[OutOffset]), _mm512_aesenclast_epi128(x, m_ahxState->RoundKeysW[kctr]));

#else

Expand Down Expand Up @@ -751,29 +725,16 @@ std::vector<SymmetricKeySize> AHX::CalculateKeySizes(BlockCipherExtensions Exten
return keys;
}

#if defined(CEX_EXTENDED_AESNI)
__m256i AHX::Load128To256(__m128i &A, __m128i &B)
{
__m256i x;

x = _mm256_setzero_si256();
x = _mm256_inserti32x4(x, A, 0);
x = _mm256_inserti32x4(x, B, 1);

return x;
}
#endif

#if defined(CEX_HAS_AVX512)
__m512i AHX::Load128To512(__m128i &A, __m128i &B, __m128i &C, __m128i &D)
__m512i AHX::Load128To512(__m128i &V)
{
__m512i x;

x = _mm512_setzero_si512();
x = _mm512_inserti32x4(x, A, 0);
x = _mm512_inserti32x4(x, B, 1);
x = _mm512_inserti32x4(x, C, 2);
x = _mm512_inserti32x4(x, D, 3);
x = _mm512_inserti32x4(x, V, 0);
x = _mm512_inserti32x4(x, V, 1);
x = _mm512_inserti32x4(x, V, 2);
x = _mm512_inserti32x4(x, V, 3);

return x;
}
Expand Down
3 changes: 1 addition & 2 deletions CEX/AHX.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,8 @@ class AHX final : public IBlockCipher
private:

#if defined(CEX_EXTENDED_AESNI)
static __m256i Load128To256(__m128i &A, __m128i &B);
# if defined(CEX_HAS_AVX512)
static __m512i Load128To512(__m128i &A, __m128i &B, __m128i &C, __m128i &D);
static __m512i Load128To512(__m128i &V);
# endif
#endif
static std::vector<SymmetricKeySize> CalculateKeySizes(BlockCipherExtensions Extension);
Expand Down
66 changes: 36 additions & 30 deletions CEX/CexConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,47 @@
# define CEX_OS_SUNUX
# endif
#endif

#if defined(__posix) || defined(_POSIX_VERSION)
# define CEX_OS_POSIX
# include <unistd.h>
#endif

#if !defined(__clang__) && !defined(__GNUC__)
# ifdef __attribute__
# undef __attribute__
# endif
# define __attribute__(a)
#endif

#if defined(_DLL)
# define CEX_DLL_API
#endif

#if defined(CEX_DLL_API)
# if defined(_MSC_VER)
# if defined(CEX_DLL_IMPORT)
# define CEX_EXPORT_API __declspec(dllimport)
# else
# define CEX_EXPORT_API __declspec(dllexport)
# endif
# else
# if defined(__SUNPRO_C)
# if !defined(__GNU_C__)
# define CEX_EXPORT_API __attribute__ (visibility(__global))
# else
# define CEX_EXPORT_API __attribute__ __global
# endif
# elif defined(_MSG_VER)
# define CEX_EXPORT_API extern __declspec(dllexport)
# else
# define CEX_EXPORT_API __attribute__ ((visibility ("default")))
# endif
# endif
#else
# define CEX_EXPORT_API
#endif

#if defined(CEX_OS_WINDOWS) || defined(CEX_OS_UNIX)
# define CEX_HIGHRES_TIMER
#endif
Expand Down Expand Up @@ -151,36 +187,6 @@
# endif
#endif

#if defined(CEX_STATIC)
# define CEX_EXPORT
# define CEX_EXPORT_WEAK
#else
# if defined(_MSC_VER)
# if defined(CEX_DLL_EXPORT)
# define CEX_EXPORT __declspec(dllexport)
# else
# define CEX_EXPORT __declspec(dllimport)
# endif
# else
# if defined(__SUNPRO_C)
# if defined(__GNU_C__)
# define CEX_EXPORT __attribute__ (visibility(__global))
# else
# define CEX_EXPORT __attribute__ __global
# endif
# elif defined(_MSG_VER)
# define CEX_EXPORT extern __declspec(dllexport)
# else
# define CEX_EXPORT __attribute__ ((visibility ("default")))
# endif
# endif
# if defined(__ELF__) && !defined(CEX_DISABLE_WEAK_FUNCTIONS)
# define CEX_EXPORT_WEAK CEX_EXPORT __attribute__((weak))
# else
# define CEX_EXPORT_WEAK CEX_EXPORT
# endif
#endif

#if !defined(CEX_ALIGN)
# if defined(__INTEL_COMPILER) || defined(_MSC_VER)
# define CEX_ALIGN(x) __declspec(align(x))
Expand Down
Loading

0 comments on commit 0296b09

Please sign in to comment.