Cleanup SIMON-128 code

The check-in provides more unification and sets the pattern used with SIMON-64
This commit is contained in:
Jeffrey Walton 2018-08-14 01:45:23 -04:00
parent 9d60081619
commit b35632e89e
No known key found for this signature in database
GPG Key ID: B36AB348921B1838

View File

@ -1021,7 +1021,6 @@ using CryptoPP::uint64x2_p;
using CryptoPP::VectorAnd;
using CryptoPP::VectorXor;
using CryptoPP::VectorSwapWords;
// Rotate left by bit count
template<unsigned int C>
@ -1039,11 +1038,6 @@ inline uint64x2_p RotateRight64(const uint64x2_p val)
return vec_rl(val, m);
}
inline uint64x2_p SwapWords(const uint64x2_p val)
{
return VectorSwapWords(val);
}
inline uint64x2_p SIMON128_f(const uint64x2_p val)
{
return VectorXor(RotateLeft64<2>(val),
@ -1053,8 +1047,8 @@ inline uint64x2_p SIMON128_f(const uint64x2_p val)
inline void SIMON128_Enc_Block(uint32x4_p &block, const word64 *subkeys, unsigned int rounds)
{
#if defined(CRYPTOPP_BIG_ENDIAN)
const uint8x16_p m1 = {15,14,13,12,11,10,9,8, 31,30,29,28,27,26,25,24};
const uint8x16_p m2 = {7,6,5,4,3,2,1,0, 23,22,21,20,19,18,17,16};
const uint8x16_p m1 = {31,30,29,28,27,26,25,24, 15,14,13,12,11,10,9,8};
const uint8x16_p m2 = {23,22,21,20,19,18,17,16, 7,6,5,4,3,2,1,0};
#else
const uint8x16_p m1 = {7,6,5,4,3,2,1,0, 23,22,21,20,19,18,17,16};
const uint8x16_p m2 = {15,14,13,12,11,10,9,8, 31,30,29,28,27,26,25,24};
@ -1064,11 +1058,6 @@ inline void SIMON128_Enc_Block(uint32x4_p &block, const word64 *subkeys, unsigne
uint64x2_p x1 = (uint64x2_p)vec_perm(block, block, m1);
uint64x2_p y1 = (uint64x2_p)vec_perm(block, block, m2);
#if defined(CRYPTOPP_BIG_ENDIAN)
x1 = SwapWords(x1);
y1 = SwapWords(y1);
#endif
for (int i = 0; i < static_cast<int>(rounds & ~1)-1; i += 2)
{
const uint64x2_p rk1 = vec_splats((unsigned long long)subkeys[i]);
@ -1086,19 +1075,22 @@ inline void SIMON128_Enc_Block(uint32x4_p &block, const word64 *subkeys, unsigne
}
#if defined(CRYPTOPP_BIG_ENDIAN)
// [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ...
block = (uint32x4_p)vec_perm(y1, x1, m1);
const uint8x16_p m3 = {31,30,29,28,27,26,25,24, 15,14,13,12,11,10,9,8};
//const uint8x16_p m4 = {23,22,21,20,19,18,17,16, 7,6,5,4,3,2,1,0};
#else
const uint8x16_p m3 = {7,6,5,4,3,2,1,0, 23,22,21,20,19,18,17,16};
//const uint8x16_p m4 = {15,14,13,12,11,10,9,8, 31,30,29,28,27,26,25,24};
#endif
// [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ...
block = (uint32x4_p)vec_perm(x1, y1, m1);
#endif
}
inline void SIMON128_Dec_Block(uint32x4_p &block, const word64 *subkeys, unsigned int rounds)
{
#if defined(CRYPTOPP_BIG_ENDIAN)
const uint8x16_p m1 = {15,14,13,12,11,10,9,8, 31,30,29,28,27,26,25,24};
const uint8x16_p m2 = {7,6,5,4,3,2,1,0, 23,22,21,20,19,18,17,16};
const uint8x16_p m1 = {31,30,29,28,27,26,25,24, 15,14,13,12,11,10,9,8};
const uint8x16_p m2 = {23,22,21,20,19,18,17,16, 7,6,5,4,3,2,1,0};
#else
const uint8x16_p m1 = {7,6,5,4,3,2,1,0, 23,22,21,20,19,18,17,16};
const uint8x16_p m2 = {15,14,13,12,11,10,9,8, 31,30,29,28,27,26,25,24};
@ -1108,12 +1100,6 @@ inline void SIMON128_Dec_Block(uint32x4_p &block, const word64 *subkeys, unsigne
uint64x2_p x1 = (uint64x2_p)vec_perm(block, block, m1);
uint64x2_p y1 = (uint64x2_p)vec_perm(block, block, m2);
#if defined(CRYPTOPP_BIG_ENDIAN)
x1 = SwapWords(x1);
y1 = SwapWords(y1);
#endif
if (rounds & 1)
{
std::swap(x1, y1);
@ -1132,12 +1118,15 @@ inline void SIMON128_Dec_Block(uint32x4_p &block, const word64 *subkeys, unsigne
}
#if defined(CRYPTOPP_BIG_ENDIAN)
// [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ...
block = (uint32x4_p)vec_perm(y1, x1, m1);
const uint8x16_p m3 = {31,30,29,28,27,26,25,24, 15,14,13,12,11,10,9,8};
//const uint8x16_p m4 = {23,22,21,20,19,18,17,16, 7,6,5,4,3,2,1,0};
#else
const uint8x16_p m3 = {7,6,5,4,3,2,1,0, 23,22,21,20,19,18,17,16};
//const uint8x16_p m4 = {15,14,13,12,11,10,9,8, 31,30,29,28,27,26,25,24};
#endif
// [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ...
block = (uint32x4_p)vec_perm(x1, y1, m1);
#endif
}
inline void SIMON128_Enc_6_Blocks(uint32x4_p &block0, uint32x4_p &block1,
@ -1145,8 +1134,8 @@ inline void SIMON128_Enc_6_Blocks(uint32x4_p &block0, uint32x4_p &block1,
uint32x4_p &block5, const word64 *subkeys, unsigned int rounds)
{
#if defined(CRYPTOPP_BIG_ENDIAN)
const uint8x16_p m1 = {15,14,13,12,11,10,9,8, 31,30,29,28,27,26,25,24};
const uint8x16_p m2 = {7,6,5,4,3,2,1,0, 23,22,21,20,19,18,17,16};
const uint8x16_p m1 = {31,30,29,28,27,26,25,24, 15,14,13,12,11,10,9,8};
const uint8x16_p m2 = {23,22,21,20,19,18,17,16, 7,6,5,4,3,2,1,0};
#else
const uint8x16_p m1 = {7,6,5,4,3,2,1,0, 23,22,21,20,19,18,17,16};
const uint8x16_p m2 = {15,14,13,12,11,10,9,8, 31,30,29,28,27,26,25,24};
@ -1160,11 +1149,6 @@ inline void SIMON128_Enc_6_Blocks(uint32x4_p &block0, uint32x4_p &block1,
uint64x2_p x3 = (uint64x2_p)vec_perm(block4, block5, m1);
uint64x2_p y3 = (uint64x2_p)vec_perm(block4, block5, m2);
#if defined(CRYPTOPP_BIG_ENDIAN)
x1 = SwapWords(x1); x2 = SwapWords(x2); x3 = SwapWords(x3);
y1 = SwapWords(y1); y2 = SwapWords(y2); y3 = SwapWords(y3);
#endif
for (int i = 0; i < static_cast<int>(rounds & ~1)-1; i += 2)
{
const uint64x2_p rk1 = vec_splats((unsigned long long)subkeys[i]);
@ -1188,22 +1172,20 @@ inline void SIMON128_Enc_6_Blocks(uint32x4_p &block0, uint32x4_p &block1,
}
#if defined(CRYPTOPP_BIG_ENDIAN)
// [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ...
block0 = (uint32x4_p)vec_perm(y1, x1, m1);
block1 = (uint32x4_p)vec_perm(y1, x1, m2);
block2 = (uint32x4_p)vec_perm(y2, x2, m1);
block3 = (uint32x4_p)vec_perm(y2, x2, m2);
block4 = (uint32x4_p)vec_perm(y3, x3, m1);
block5 = (uint32x4_p)vec_perm(y3, x3, m2);
const uint8x16_p m3 = {31,30,29,28,27,26,25,24, 15,14,13,12,11,10,9,8};
const uint8x16_p m4 = {23,22,21,20,19,18,17,16, 7,6,5,4,3,2,1,0};
#else
// [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ...
block0 = (uint32x4_p)vec_perm(x1, y1, m1);
block1 = (uint32x4_p)vec_perm(x1, y1, m2);
block2 = (uint32x4_p)vec_perm(x2, y2, m1);
block3 = (uint32x4_p)vec_perm(x2, y2, m2);
block4 = (uint32x4_p)vec_perm(x3, y3, m1);
block5 = (uint32x4_p)vec_perm(x3, y3, m2);
const uint8x16_p m3 = {7,6,5,4,3,2,1,0, 23,22,21,20,19,18,17,16};
const uint8x16_p m4 = {15,14,13,12,11,10,9,8, 31,30,29,28,27,26,25,24};
#endif
// [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ...
block0 = (uint32x4_p)vec_perm(x1, y1, m3);
block1 = (uint32x4_p)vec_perm(x1, y1, m4);
block2 = (uint32x4_p)vec_perm(x2, y2, m3);
block3 = (uint32x4_p)vec_perm(x2, y2, m4);
block4 = (uint32x4_p)vec_perm(x3, y3, m3);
block5 = (uint32x4_p)vec_perm(x3, y3, m4);
}
inline void SIMON128_Dec_6_Blocks(uint32x4_p &block0, uint32x4_p &block1,
@ -1211,8 +1193,8 @@ inline void SIMON128_Dec_6_Blocks(uint32x4_p &block0, uint32x4_p &block1,
uint32x4_p &block5, const word64 *subkeys, unsigned int rounds)
{
#if defined(CRYPTOPP_BIG_ENDIAN)
const uint8x16_p m1 = {15,14,13,12,11,10,9,8, 31,30,29,28,27,26,25,24};
const uint8x16_p m2 = {7,6,5,4,3,2,1,0, 23,22,21,20,19,18,17,16};
const uint8x16_p m1 = {31,30,29,28,27,26,25,24, 15,14,13,12,11,10,9,8};
const uint8x16_p m2 = {23,22,21,20,19,18,17,16, 7,6,5,4,3,2,1,0};
#else
const uint8x16_p m1 = {7,6,5,4,3,2,1,0, 23,22,21,20,19,18,17,16};
const uint8x16_p m2 = {15,14,13,12,11,10,9,8, 31,30,29,28,27,26,25,24};
@ -1226,11 +1208,6 @@ inline void SIMON128_Dec_6_Blocks(uint32x4_p &block0, uint32x4_p &block1,
uint64x2_p x3 = (uint64x2_p)vec_perm(block4, block5, m1);
uint64x2_p y3 = (uint64x2_p)vec_perm(block4, block5, m2);
#if defined(CRYPTOPP_BIG_ENDIAN)
x1 = SwapWords(x1); x2 = SwapWords(x2); x3 = SwapWords(x3);
y1 = SwapWords(y1); y2 = SwapWords(y2); y3 = SwapWords(y3);
#endif
if (rounds & 1)
{
std::swap(x1, y1); std::swap(x2, y2); std::swap(x3, y3);
@ -1255,22 +1232,20 @@ inline void SIMON128_Dec_6_Blocks(uint32x4_p &block0, uint32x4_p &block1,
}
#if defined(CRYPTOPP_BIG_ENDIAN)
// [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ...
block0 = (uint32x4_p)vec_perm(y1, x1, m1);
block1 = (uint32x4_p)vec_perm(y1, x1, m2);
block2 = (uint32x4_p)vec_perm(y2, x2, m1);
block3 = (uint32x4_p)vec_perm(y2, x2, m2);
block4 = (uint32x4_p)vec_perm(y3, x3, m1);
block5 = (uint32x4_p)vec_perm(y3, x3, m2);
const uint8x16_p m3 = {31,30,29,28,27,26,25,24, 15,14,13,12,11,10,9,8};
const uint8x16_p m4 = {23,22,21,20,19,18,17,16, 7,6,5,4,3,2,1,0};
#else
// [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ...
block0 = (uint32x4_p)vec_perm(x1, y1, m1);
block1 = (uint32x4_p)vec_perm(x1, y1, m2);
block2 = (uint32x4_p)vec_perm(x2, y2, m1);
block3 = (uint32x4_p)vec_perm(x2, y2, m2);
block4 = (uint32x4_p)vec_perm(x3, y3, m1);
block5 = (uint32x4_p)vec_perm(x3, y3, m2);
const uint8x16_p m3 = {7,6,5,4,3,2,1,0, 23,22,21,20,19,18,17,16};
const uint8x16_p m4 = {15,14,13,12,11,10,9,8, 31,30,29,28,27,26,25,24};
#endif
// [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ...
block0 = (uint32x4_p)vec_perm(x1, y1, m3);
block1 = (uint32x4_p)vec_perm(x1, y1, m4);
block2 = (uint32x4_p)vec_perm(x2, y2, m3);
block3 = (uint32x4_p)vec_perm(x2, y2, m4);
block4 = (uint32x4_p)vec_perm(x3, y3, m3);
block5 = (uint32x4_p)vec_perm(x3, y3, m4);
}
#endif // CRYPTOPP_POWER8_AVAILABLE