Skip to content

Commit

Permalink
small improvements and armv7a alignment fixes (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberjunk authored Nov 13, 2024
1 parent 4aa3009 commit bc80d9f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 87 deletions.
6 changes: 0 additions & 6 deletions include/CppCore/Block.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,6 @@ namespace CppCore
}

public:
INLINE T operator ^ (const T& v) const
{
T r;
CppCore::xor_(v, *(T*)this, r);
return r;
}
INLINE void operator ^= (const T& v)
{
CppCore::xor_(v, *(T*)this, *(T*)this);
Expand Down
35 changes: 24 additions & 11 deletions include/CppCore/Crypto/AES.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,21 @@ namespace CppCore
uint8_t* cenc = (uint8_t*)enc;
if (cin + 8U <= cend)
{
#if defined(CPPCORE_CPU_64BIT)
*(uint64_t*)cout = *(uint64_t*)cenc ^ *(uint64_t*)cin;
cin += 8U;
cout += 8U;
cenc += 8U;
#else
*(uint32_t*)cout = *(uint32_t*)cenc ^ *(uint32_t*)cin;
cin += 4U;
cout += 4U;
cenc += 4U;
*(uint32_t*)cout = *(uint32_t*)cenc ^ *(uint32_t*)cin;
cin += 4U;
cout += 4U;
cenc += 4U;
#endif
}
if (cin + 4U <= cend)
{
Expand Down Expand Up @@ -319,7 +330,7 @@ namespace CppCore
Block t;

// initial round key addition on input
out = in ^ this->ekb[0];
CppCore::xor_(in, this->ekb[0], out);

// n rounds
CPPCORE_UNROLL
Expand Down Expand Up @@ -347,7 +358,7 @@ namespace CppCore
t.u32[3] ^= CppCore::rotl32(te[CppCore::getbits32(out.u32[2], 24, 8)], 24);

// round key addition
out = t ^ this->ekb[i];
CppCore::xor_(t,this->ekb[i],out);
}

// last round
Expand All @@ -372,7 +383,7 @@ namespace CppCore
t.u32[3] |= sbox[CppCore::getbits32(out.u32[2], 24, 8)] << 24;

// last round key addition
out = t ^ this->ekb[N];
CppCore::xor_(t, this->ekb[N], out);
}

/// <summary>
Expand All @@ -383,7 +394,7 @@ namespace CppCore
Block t;

// initial round key addition on input
out = in ^ this->dkb[N];
CppCore::xor_(in, this->dkb[N], out);

// n rounds
CPPCORE_UNROLL
Expand Down Expand Up @@ -411,7 +422,7 @@ namespace CppCore
t.u32[3] ^= CppCore::rotl32(td[CppCore::getbits32(out.u32[0], 24, 8)], 24);

// round key addition
out = t ^ this->dkb[i];
CppCore::xor_(t, this->dkb[i], out);
}

// last round
Expand All @@ -436,17 +447,17 @@ namespace CppCore
t.u32[3] |= isbox[CppCore::getbits32(out.u32[0], 24, 8)] << 24;

// last round key addition
out = t ^ this->dkb[0];
CppCore::xor_(t, this->dkb[0], out);
}

/// <summary>
/// Encrypts one Block in CBC mode
/// </summary>
INLINE void encrypt(const Block& in, Block& out, Block& iv)
{
out = in ^ iv;
CppCore::xor_(in, iv, out);
encrypt(out, out);
iv = out;
CppCore::clone(iv, out);
}

/// <summary>
Expand All @@ -455,8 +466,8 @@ namespace CppCore
INLINE void decrypt(const Block& in, Block& out, Block& iv)
{
decrypt(in, out);
out = out ^ iv;
iv = in;
CppCore::xor_(out, iv, out);
CppCore::clone(iv, in);
}

///////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -549,7 +560,9 @@ namespace CppCore
if (len >= 16U)
{
// xor encrypted iv with input
*bout++ = enc ^ *bin++;
CppCore::xor_(enc, *bin, *bout);
bin++;
bout++;
len -= 16U;
}
else
Expand Down
80 changes: 12 additions & 68 deletions include/CppCore/Encoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ namespace CppCore
/// </summary>
INLINE static uint8_t valueofhexchar(const uint8_t c)
{
static const uint8_t table[] =
CPPCORE_ALIGN64 static constexpr uint8_t table[] =
{
0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // ........
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // ........
Expand Down Expand Up @@ -412,7 +412,7 @@ namespace CppCore
/// </summary>
INLINE static uint16_t bytetohexint16(const uint8_t byte)
{
static const uint16_t table[] =
CPPCORE_ALIGN64 static constexpr uint16_t table[] =
{
0x3030, 0x3130, 0x3230, 0x3330, 0x3430, 0x3530, 0x3630, 0x3730,
0x3830, 0x3930, 0x4130, 0x4230, 0x4330, 0x4430, 0x4530, 0x4630,
Expand Down Expand Up @@ -724,85 +724,29 @@ namespace CppCore
/// Creates hex string for memory of certain length.
/// Requires len*2 (or len*2+1 if writeterm=true) free bytes in parameter s.
/// </summary>
INLINE static void tostring(const void* m, const size_t len, char* s, const bool bigendian = true, const bool writeterm = true)
INLINE static void tostring(const void* m, size_t len, char* s, const bool bigendian = true, const bool writeterm = true)
{
const char* mem = (const char*)m;
uint32_t* p = (uint32_t*)s;
const uint8_t* mem = (const uint8_t*)m;
uint16_t* p16 = (uint16_t*)s;
if (bigendian)
{
const char* end = mem + len;
#if defined(CPPCORE_CPU_64BIT)
while (mem + 8U <= end)
{
const uint64_t t = *((uint64_t*)mem);
*p++ = Util::valuetohexint32(t, 0ULL, 8ULL);
*p++ = Util::valuetohexint32(t, 16ULL, 24ULL);
*p++ = Util::valuetohexint32(t, 32ULL, 40ULL);
*p++ = Util::valuetohexint32(t, 48ULL, 56ULL);
mem += 8U;
}
if (mem + 4U <= end)
#else
while (mem + 4U <= end)
#endif
{
const uint32_t t = *((uint32_t*)mem);
*p++ = Util::valuetohexint32(t, 0U, 8U);
*p++ = Util::valuetohexint32(t, 16U, 24U);
mem += 4U;
}
if (mem + 2U <= end)
{
const uint16_t t = *((uint16_t*)mem);
*p++ = Util::valuetohexint32((uint32_t)t, 0U, 8U);
mem += 2U;
}
if (mem + 1U <= end)
while(len)
{
uint16_t* pt = (uint16_t*)p;
*pt++ = Util::bytetohexint16(*mem);
p = (uint32_t*)pt;
*p16++ = Util::bytetohexint16(*mem++);
len--;
}
}
else
{
const char* end = mem;
mem += len;
#if defined(CPPCORE_CPU_64BIT)
while (mem - 8U >= end)
while(len)
{
mem -= 8U;
const uint64_t t = *((uint64_t*)mem);
*p++ = Util::valuetohexint32(t, 56ULL, 48ULL);
*p++ = Util::valuetohexint32(t, 40ULL, 32ULL);
*p++ = Util::valuetohexint32(t, 24ULL, 16ULL);
*p++ = Util::valuetohexint32(t, 8ULL, 0ULL);
}
if (mem - 4U >= end)
#else
while (mem - 4U >= end)
#endif
{
mem -= 4U;
const uint32_t t = *((uint32_t*)mem);
*p++ = Util::valuetohexint32(t, 24U, 16U);
*p++ = Util::valuetohexint32(t, 8U, 0U);
}
if (mem - 2U >= end)
{
mem -= 2U;
const uint16_t t = *((uint16_t*)mem);
*p++ = Util::valuetohexint32((uint32_t)t, 8U, 0U);
}
if (mem - 1U >= end)
{
uint16_t* pt = (uint16_t*)p;
*pt++ = Util::bytetohexint16(*(mem-1U));
p = (uint32_t*)pt;
*p16++ = Util::bytetohexint16(*--mem);
len--;
}
}
if (writeterm)
*((char*)p) = (char)0x00;
*((uint8_t*)p16) = (uint8_t)0x00;
}

/// <summary>
Expand Down
26 changes: 24 additions & 2 deletions include/CppCore/Memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@
#define CPPCORE_MEMORY_PROCESS_64X8( len, op64, op32, op16, op8) \
CPPCORE_MEMORY_PROCESS_512( len, op64;op64;op64;op64;op64;op64;op64;op64;, op64;op64;op64;op64;, op64;op64;, op64, op32, op16, op8)

// decreases len in 64 byte steps executing sixteen op32 each time
// then handles the 0-63 remaining bytes using op32 to op8
#define CPPCORE_MEMORY_PROCESS_32X16( len, op32, op16, op8) \
CPPCORE_MEMORY_PROCESS_512( len, \
op32;op32;op32;op32;op32;op32;op32;op32;op32;op32;op32;op32;op32;op32;op32;op32;, \
op32;op32;op32;op32;op32;op32;op32;op32;, \
op32;op32;op32;op32;, \
op32;op32, \
op32, \
op16, \
op8)

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace CppCore
Expand Down Expand Up @@ -1972,12 +1984,17 @@ namespace CppCore
*((uint32_t*)memd) = *((uint32_t*)mems); memd += 4U; mems += 4U;,
*((uint16_t*)memd) = *((uint16_t*)mems); memd += 2U; mems += 2U;,
*((uint8_t*) memd) = *((uint8_t*) mems); memd += 1U; mems += 1U;);
#else
#elif defined(CPPCORE_CPU_64BIT)
CPPCORE_MEMORY_PROCESS_64X8(len,
*((uint64_t*)memd) = *((uint64_t*)mems); memd += 8U; mems += 8U;,
*((uint32_t*)memd) = *((uint32_t*)mems); memd += 4U; mems += 4U;,
*((uint16_t*)memd) = *((uint16_t*)mems); memd += 2U; mems += 2U;,
*((uint8_t*) memd) = *((uint8_t*) mems); memd += 1U; mems += 1U;);
#else
CPPCORE_MEMORY_PROCESS_32X16(len,
*((uint32_t*)memd) = *((uint32_t*)mems); memd += 4U; mems += 4U;,
*((uint16_t*)memd) = *((uint16_t*)mems); memd += 2U; mems += 2U;,
*((uint8_t*) memd) = *((uint8_t*) mems); memd += 1U; mems += 1U;);
#endif
}

Expand Down Expand Up @@ -2012,12 +2029,17 @@ namespace CppCore
memd -= 4U; mems -= 4U; *((uint32_t*)memd) = *((uint32_t*)mems);,
memd -= 2U; mems -= 2U; *((uint16_t*)memd) = *((uint16_t*)mems);,
memd -= 1U; mems -= 1U; *((uint8_t*) memd) = *((uint8_t*) mems);)
#else
#elif defined(CPPCORE_CPU_64BIT)
CPPCORE_MEMORY_PROCESS_64X8(len,
memd -= 8U; mems -= 8U; *((uint64_t*)memd) = *((uint64_t*)mems);,
memd -= 4U; mems -= 4U; *((uint32_t*)memd) = *((uint32_t*)mems);,
memd -= 2U; mems -= 2U; *((uint16_t*)memd) = *((uint16_t*)mems);,
memd -= 1U; mems -= 1U; *((uint8_t*) memd) = *((uint8_t*) mems);)
#else
CPPCORE_MEMORY_PROCESS_32X16(len,
memd -= 4U; mems -= 4U; *((uint32_t*)memd) = *((uint32_t*)mems);,
memd -= 2U; mems -= 2U; *((uint16_t*)memd) = *((uint16_t*)mems);,
memd -= 1U; mems -= 1U; *((uint8_t*) memd) = *((uint8_t*) mems);)
#endif
}

Expand Down

0 comments on commit bc80d9f

Please sign in to comment.