From 3bee1f57cf9b5f7fe309692d1e13239d1b23b4e2 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Wed, 10 May 2017 06:34:22 -0400 Subject: [PATCH] XOR block in specialized ProcessBlock Use Put and Get blocks consistently --- kalyna.cpp | 168 +++++++++++++++++++++-------------------------------- kalyna.h | 10 ++-- 2 files changed, 72 insertions(+), 106 deletions(-) diff --git a/kalyna.cpp b/kalyna.cpp index 864f139d..eba64846 100644 --- a/kalyna.cpp +++ b/kalyna.cpp @@ -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 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 InBlock; + InBlock iblk(inBlock); + iblk(msg[0])(msg[1]); + + inBlock = msg; if (IsForwardTransformation()) { AddKey<2>(inBlock, t1, m_rkeys); @@ -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 Block; - Block block(t1); - block(outBlock[0])(outBlock[1]); - } -#endif + // Reverse bytes on BigEndian; Align pointer on LittleEndian + typedef PutBlock 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 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 InBlock; + InBlock iblk(inBlock); + iblk(msg[0])(msg[1]); + + inBlock = msg; if (IsForwardTransformation()) { AddKey<2>(inBlock, t1, m_rkeys); @@ -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 Block; - Block block(t1); - block(outBlock[0])(outBlock[1]); - } -#endif + // Reverse bytes on BigEndian; Align pointer on LittleEndian + typedef PutBlock 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 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 InBlock; + InBlock iblk(inBlock); + iblk(msg[0])(msg[1])(msg[2])(msg[3]); + + inBlock = msg; if (IsForwardTransformation()) { AddKey<4>(inBlock, t1, m_rkeys); @@ -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 Block; - Block block(t1); - block(outBlock[0])(outBlock[1])(outBlock[2])(outBlock[3]); - } -#endif + // Reverse bytes on BigEndian; Align pointer on LittleEndian + typedef PutBlock 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 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 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 @@ -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 Block; - Block block(t1); - block(outBlock[0])(outBlock[1])(outBlock[2])(outBlock[3]); - } -#endif + // Reverse bytes on BigEndian; Align pointer on LittleEndian + typedef PutBlock 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 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 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); @@ -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 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 OutBlock; + OutBlock oblk(xorBlock, outBlock); + oblk(t1[0])(t1[1])(t1[2])(t1[3])(t1[4])(t1[5])(t1[6])(t1[7]); } // *********************** Library routines *********************** // @@ -1324,32 +1288,34 @@ void Kalyna::Base::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, const byte* p = reinterpret_cast(KalynaTab::S); for (unsigned int i=0; i<256; i+=cacheLineSize) - u &= *reinterpret_cast(p+i); + u ^= *reinterpret_cast(p+i); m_wspace[0] = u; switch ((m_nb << 8) | m_nk) { case (2 << 8) | 2: - ProcessBlock_22(reinterpret_cast(inBlock), reinterpret_cast(outBlock)); + ProcessBlock_22(reinterpret_cast(inBlock), + reinterpret_cast(xorBlock), reinterpret_cast(outBlock)); break; case (2 << 8) | 4: - ProcessBlock_24(reinterpret_cast(inBlock), reinterpret_cast(outBlock)); + ProcessBlock_24(reinterpret_cast(inBlock), + reinterpret_cast(xorBlock), reinterpret_cast(outBlock)); break; case (4 << 8) | 4: - ProcessBlock_44(reinterpret_cast(inBlock), reinterpret_cast(outBlock)); + ProcessBlock_44(reinterpret_cast(inBlock), + reinterpret_cast(xorBlock), reinterpret_cast(outBlock)); break; case (4 << 8) | 8: - ProcessBlock_48(reinterpret_cast(inBlock), reinterpret_cast(outBlock)); + ProcessBlock_48(reinterpret_cast(inBlock), + reinterpret_cast(xorBlock), reinterpret_cast(outBlock)); break; case (8 << 8) | 8: - ProcessBlock_88(reinterpret_cast(inBlock), reinterpret_cast(outBlock)); + ProcessBlock_88(reinterpret_cast(inBlock), + reinterpret_cast(xorBlock), reinterpret_cast(outBlock)); break; default: CRYPTOPP_ASSERT(0); } - - for (size_t i=0; xorBlock && i // 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 > AlignedSecBlock64;