Skip to content

Commit

Permalink
XOR block in specialized ProcessBlock
Browse files Browse the repository at this point in the history
Use Put and Get blocks consistently
  • Loading branch information
noloader committed May 10, 2017
1 parent 20c3d55 commit 3bee1f5
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 106 deletions.
168 changes: 67 additions & 101 deletions kalyna.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -929,20 +929,16 @@ void Kalyna::Base::SetKey_88(const word64 key[8])

// *********************** ProcessAndXorBlock specializations *********************** //

void Kalyna::Base::ProcessBlock_22(const word64 inBlock[2], word64 outBlock[2]) const
void Kalyna::Base::ProcessBlock_22(const word64 inBlock[2], const word64 xorBlock[2], word64 outBlock[2]) const
{
#if defined(IS_BIG_ENDIAN)
word64 *t1 = m_wspace+0, *t2 = m_wspace+2, *msg = m_wspace+4;
{
typedef GetBlock<word64, LittleEndian, false> Block;
Block block(inBlock);
block(msg[0])(msg[1]);
inBlock = msg;
}
#else
word64 *t1 = outBlock, *t2 = m_wspace+2;
#endif

// Reverse bytes on BigEndian; Align pointer on LittleEndian
typedef GetBlock<word64, LittleEndian, false> InBlock;
InBlock iblk(inBlock);
iblk(msg[0])(msg[1]);

inBlock = msg;
if (IsForwardTransformation())
{
AddKey<2>(inBlock, t1, m_rkeys);
Expand Down Expand Up @@ -973,29 +969,22 @@ void Kalyna::Base::ProcessBlock_22(const word64 inBlock[2], word64 outBlock[2])
IGL128(t2, t1, &m_rkeys[0]);
}

#if defined(IS_BIG_ENDIAN)
{
typedef GetBlock<word64, LittleEndian, false> Block;
Block block(t1);
block(outBlock[0])(outBlock[1]);
}
#endif
// Reverse bytes on BigEndian; Align pointer on LittleEndian
typedef PutBlock<word64, LittleEndian, false> OutBlock;
OutBlock oblk(xorBlock, outBlock);
oblk(t1[0])(t1[1]);
}

void Kalyna::Base::ProcessBlock_24(const word64 inBlock[2], word64 outBlock[2]) const
void Kalyna::Base::ProcessBlock_24(const word64 inBlock[2], const word64 xorBlock[2], word64 outBlock[2]) const
{
#if defined(IS_BIG_ENDIAN)
word64 *t1 = m_wspace+0, *t2 = m_wspace+2, *msg = m_wspace+4;
{
typedef GetBlock<word64, LittleEndian, false> Block;
Block block(inBlock);
block(msg[0])(msg[1]);
inBlock = msg;
}
#else
word64 *t1 = outBlock, *t2 = m_wspace+2;
#endif

// Reverse bytes on BigEndian; Align pointer on LittleEndian
typedef GetBlock<word64, LittleEndian, false> InBlock;
InBlock iblk(inBlock);
iblk(msg[0])(msg[1]);

inBlock = msg;
if (IsForwardTransformation())
{
AddKey<2>(inBlock, t1, m_rkeys);
Expand Down Expand Up @@ -1034,29 +1023,22 @@ void Kalyna::Base::ProcessBlock_24(const word64 inBlock[2], word64 outBlock[2])
IGL128(t2, t1, &m_rkeys[0]);
}

#if defined(IS_BIG_ENDIAN)
{
typedef GetBlock<word64, LittleEndian, false> Block;
Block block(t1);
block(outBlock[0])(outBlock[1]);
}
#endif
// Reverse bytes on BigEndian; Align pointer on LittleEndian
typedef PutBlock<word64, LittleEndian, false> OutBlock;
OutBlock oblk(xorBlock, outBlock);
oblk(t1[0])(t1[1]);
}

void Kalyna::Base::ProcessBlock_44(const word64 inBlock[4], word64 outBlock[4]) const
void Kalyna::Base::ProcessBlock_44(const word64 inBlock[4], const word64 xorBlock[4], word64 outBlock[4]) const
{
#if defined(IS_BIG_ENDIAN)
word64 *t1 = m_wspace+0, *t2 = m_wspace+4, *msg = m_wspace+8;
{
typedef GetBlock<word64, LittleEndian, false> Block;
Block block(inBlock);
block(msg[0])(msg[1])(msg[2])(msg[3]);
inBlock = msg;
}
#else
word64 *t1 = outBlock, *t2 = m_wspace+4;
#endif

// Reverse bytes on BigEndian; Align pointer on LittleEndian
typedef GetBlock<word64, LittleEndian, false> InBlock;
InBlock iblk(inBlock);
iblk(msg[0])(msg[1])(msg[2])(msg[3]);

inBlock = msg;
if (IsForwardTransformation())
{
AddKey<4>(inBlock, t1, m_rkeys);
Expand Down Expand Up @@ -1095,32 +1077,25 @@ void Kalyna::Base::ProcessBlock_44(const word64 inBlock[4], word64 outBlock[4])
IGL256(t2, t1, &m_rkeys[0]);
}

#if defined(IS_BIG_ENDIAN)
{
typedef GetBlock<word64, LittleEndian, false> Block;
Block block(t1);
block(outBlock[0])(outBlock[1])(outBlock[2])(outBlock[3]);
}
#endif
// Reverse bytes on BigEndian; Align pointer on LittleEndian
typedef PutBlock<word64, LittleEndian, false> OutBlock;
OutBlock oblk(xorBlock, outBlock);
oblk(t1[0])(t1[1])(t1[2])(t1[3]);
}

void Kalyna::Base::ProcessBlock_48(const word64 inBlock[4], word64 outBlock[4]) const
void Kalyna::Base::ProcessBlock_48(const word64 inBlock[4], const word64 xorBlock[4], word64 outBlock[4]) const
{
#if defined(IS_BIG_ENDIAN)
word64 *t1 = m_wspace+0, *t2 = m_wspace+4, *msg = m_wspace+8;
{
typedef GetBlock<word64, LittleEndian, false> Block;
Block block(inBlock);
block(msg[0])(msg[1])(msg[2])(msg[3]);
inBlock = msg;
}
#else
word64 *t1 = outBlock, *t2 = m_wspace+4;
#endif

// Reverse bytes on BigEndian; Align pointer on LittleEndian
typedef GetBlock<word64, LittleEndian, false> InBlock;
InBlock iblk(inBlock);
iblk(msg[0])(msg[1])(msg[2])(msg[3]);

inBlock = msg;
if (IsForwardTransformation())
{
AddKey<4>((const word64*)inBlock, t1, m_rkeys);
AddKey<4>(inBlock, t1, m_rkeys);
G256(t1, t2, &m_rkeys[4]); // 1
G256(t2, t1, &m_rkeys[8]); // 2
G256(t1, t2, &m_rkeys[12]); // 3
Expand Down Expand Up @@ -1164,29 +1139,22 @@ void Kalyna::Base::ProcessBlock_48(const word64 inBlock[4], word64 outBlock[4])
IGL256(t2, t1, &m_rkeys[0]);
}

#if defined(IS_BIG_ENDIAN)
{
typedef GetBlock<word64, LittleEndian, false> Block;
Block block(t1);
block(outBlock[0])(outBlock[1])(outBlock[2])(outBlock[3]);
}
#endif
// Reverse bytes on BigEndian; Align pointer on LittleEndian
typedef PutBlock<word64, LittleEndian, false> OutBlock;
OutBlock oblk(xorBlock, outBlock);
oblk(t1[0])(t1[1])(t1[2])(t1[3]);
}

void Kalyna::Base::ProcessBlock_88(const word64 inBlock[8], word64 outBlock[8]) const
void Kalyna::Base::ProcessBlock_88(const word64 inBlock[8], const word64 xorBlock[8], word64 outBlock[8]) const
{
#if defined(IS_BIG_ENDIAN)
word64 *t1 = m_wspace+0, *t2 = m_wspace+8, *msg = m_wspace+16;
{
typedef GetBlock<word64, LittleEndian, false> Block;
Block block(inBlock);
block(msg[0])(msg[1])(msg[2])(msg[3])(msg[4])(msg[5])(msg[6])(msg[7]);
inBlock = msg;
}
#else
word64 *t1 = outBlock, *t2 = m_wspace+8;
#endif

// Reverse bytes on BigEndian; Align pointer on LittleEndian
typedef GetBlock<word64, LittleEndian, false> InBlock;
InBlock iblk(inBlock);
iblk(msg[0])(msg[1])(msg[2])(msg[3])(msg[4])(msg[5])(msg[6])(msg[7]);

inBlock = msg;
if (IsForwardTransformation())
{
AddKey<8>(inBlock, t1, m_rkeys);
Expand Down Expand Up @@ -1233,14 +1201,10 @@ void Kalyna::Base::ProcessBlock_88(const word64 inBlock[8], word64 outBlock[8])
IGL512(t2, t1, &m_rkeys[0]);
}

#if defined(IS_BIG_ENDIAN)
{
typedef GetBlock<word64, LittleEndian, false> Block;
Block block(t1);
block(outBlock[0])(outBlock[1])(outBlock[2])(outBlock[3])
(outBlock[4])(outBlock[5])(outBlock[6])(outBlock[7]);
}
#endif
// Reverse bytes on BigEndian; Align pointer on LittleEndian
typedef PutBlock<word64, LittleEndian, false> OutBlock;
OutBlock oblk(xorBlock, outBlock);
oblk(t1[0])(t1[1])(t1[2])(t1[3])(t1[4])(t1[5])(t1[6])(t1[7]);
}

// *********************** Library routines *********************** //
Expand Down Expand Up @@ -1324,32 +1288,34 @@ void Kalyna::Base::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock,

const byte* p = reinterpret_cast<const byte*>(KalynaTab::S);
for (unsigned int i=0; i<256; i+=cacheLineSize)
u &= *reinterpret_cast<const word64*>(p+i);
u ^= *reinterpret_cast<const word64*>(p+i);
m_wspace[0] = u;

switch ((m_nb << 8) | m_nk)
{
case (2 << 8) | 2:
ProcessBlock_22(reinterpret_cast<const word64*>(inBlock), reinterpret_cast<word64*>(outBlock));
ProcessBlock_22(reinterpret_cast<const word64*>(inBlock),
reinterpret_cast<const word64*>(xorBlock), reinterpret_cast<word64*>(outBlock));
break;
case (2 << 8) | 4:
ProcessBlock_24(reinterpret_cast<const word64*>(inBlock), reinterpret_cast<word64*>(outBlock));
ProcessBlock_24(reinterpret_cast<const word64*>(inBlock),
reinterpret_cast<const word64*>(xorBlock), reinterpret_cast<word64*>(outBlock));
break;
case (4 << 8) | 4:
ProcessBlock_44(reinterpret_cast<const word64*>(inBlock), reinterpret_cast<word64*>(outBlock));
ProcessBlock_44(reinterpret_cast<const word64*>(inBlock),
reinterpret_cast<const word64*>(xorBlock), reinterpret_cast<word64*>(outBlock));
break;
case (4 << 8) | 8:
ProcessBlock_48(reinterpret_cast<const word64*>(inBlock), reinterpret_cast<word64*>(outBlock));
ProcessBlock_48(reinterpret_cast<const word64*>(inBlock),
reinterpret_cast<const word64*>(xorBlock), reinterpret_cast<word64*>(outBlock));
break;
case (8 << 8) | 8:
ProcessBlock_88(reinterpret_cast<const word64*>(inBlock), reinterpret_cast<word64*>(outBlock));
ProcessBlock_88(reinterpret_cast<const word64*>(inBlock),
reinterpret_cast<const word64*>(xorBlock), reinterpret_cast<word64*>(outBlock));
break;
default:
CRYPTOPP_ASSERT(0);
}

for (size_t i=0; xorBlock && i<m_nb; ++i)
outBlock[i] ^= xorBlock[i];
}

NAMESPACE_END
10 changes: 5 additions & 5 deletions kalyna.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@ class Kalyna : public Kalyna_Info, public BlockCipherDocumentation
// Visual Studio and C2910: 'Kalyna::Base::ProcessBlock_Template': cannot be explicitly specialized
//template <unsigned int NB, unsigned int NK>
// void ProcessBlock_Template(const word64 inBlock[NB], const word64 outBlock[NB]) const;
void ProcessBlock_22(const word64 inBlock[2], word64 outBlock[2]) const;
void ProcessBlock_24(const word64 inBlock[2], word64 outBlock[2]) const;
void ProcessBlock_44(const word64 inBlock[4], word64 outBlock[4]) const;
void ProcessBlock_48(const word64 inBlock[4], word64 outBlock[4]) const;
void ProcessBlock_88(const word64 inBlock[8], word64 outBlock[8]) const;
void ProcessBlock_22(const word64 inBlock[2], const word64 xorBlock[2], word64 outBlock[2]) const;
void ProcessBlock_24(const word64 inBlock[2], const word64 xorBlock[2] ,word64 outBlock[2]) const;
void ProcessBlock_44(const word64 inBlock[4], const word64 xorBlock[4], word64 outBlock[4]) const;
void ProcessBlock_48(const word64 inBlock[4], const word64 xorBlock[4], word64 outBlock[4]) const;
void ProcessBlock_88(const word64 inBlock[8], const word64 xorBlock[8], word64 outBlock[8]) const;

private:
typedef SecBlock<word64, AllocatorWithCleanup<word64, true> > AlignedSecBlock64;
Expand Down

0 comments on commit 3bee1f5

Please sign in to comment.