From 9b91b94b05929277db2dc8dd2d5185adcba98bed Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Tue, 14 Aug 2018 07:14:39 -0400 Subject: [PATCH] Add POWER8 SIMON-64 implementation --- simon-simd.cpp | 256 +++++++++++++++++++++++++++++++++++++++++++++++++ simon.cpp | 20 ++++ simon.h | 3 +- speck-simd.cpp | 1 - 4 files changed, 278 insertions(+), 2 deletions(-) diff --git a/simon-simd.cpp b/simon-simd.cpp index 3a3b412a..966200ec 100644 --- a/simon-simd.cpp +++ b/simon-simd.cpp @@ -1015,6 +1015,248 @@ inline void SIMON64_Dec_6_Blocks(__m128i &block0, __m128i &block1, #if defined(CRYPTOPP_POWER8_AVAILABLE) +using CryptoPP::uint8x16_p; +using CryptoPP::uint32x4_p; + +using CryptoPP::VectorAnd; +using CryptoPP::VectorXor; + +// Rotate left by bit count +template +inline uint32x4_p RotateLeft32(const uint32x4_p val) +{ + const uint32x4_p m = {C, C, C, C}; + return vec_rl(val, m); +} + +// Rotate right by bit count +template +inline uint32x4_p RotateRight32(const uint32x4_p val) +{ + const uint32x4_p m = {32-C, 32-C, 32-C, 32-C}; + return vec_rl(val, m); +} + +inline uint32x4_p SIMON64_f(const uint32x4_p val) +{ + return VectorXor(RotateLeft32<2>(val), + VectorAnd(RotateLeft32<1>(val), RotateLeft32<8>(val))); +} + +inline void SIMON64_Enc_Block(uint32x4_p &block0, uint32x4_p &block1, + const word32 *subkeys, unsigned int rounds) +{ +#if defined(CRYPTOPP_BIG_ENDIAN) + const uint8x16_p m1 = {7,6,5,4, 15,14,13,12, 23,22,21,20, 31,30,29,28}; + const uint8x16_p m2 = {3,2,1,0, 11,10,9,8, 19,18,17,16, 27,26,25,24}; +#else + const uint8x16_p m1 = {3,2,1,0, 11,10,9,8, 19,18,17,16, 27,26,25,24}; + const uint8x16_p m2 = {7,6,5,4, 15,14,13,12, 23,22,21,20, 31,30,29,28}; +#endif + + // [A1 A2 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... + uint32x4_p x1 = vec_perm(block0, block1, m1); + uint32x4_p y1 = vec_perm(block0, block1, m2); + + for (int i = 0; i < static_cast(rounds & ~1)-1; i += 2) + { + const uint32x4_p rk1 = vec_splats(subkeys[i]); + y1 = VectorXor(VectorXor(y1, SIMON64_f(x1)), rk1); + + const uint32x4_p rk2 = vec_splats(subkeys[i+1]); + x1 = VectorXor(VectorXor(x1, SIMON64_f(y1)), rk2); + } + + if (rounds & 1) + { + const uint32x4_p rk = vec_splats(subkeys[rounds-1]); + y1 = VectorXor(VectorXor(y1, SIMON64_f(x1)), rk); + std::swap(x1, y1); + } + +#if defined(CRYPTOPP_BIG_ENDIAN) + const uint8x16_p m3 = {11,10,9,8, 27,26,25,24, 15,14,13,12, 31,30,29,28}; + const uint8x16_p m4 = {3,2,1,0, 19,18,17,16, 7,6,5,4, 23,22,21,20}; +#else + const uint8x16_p m3 = {3,2,1,0, 19,18,17,16, 7,6,5,4, 23,22,21,20}; + const uint8x16_p m4 = {11,10,9,8, 27,26,25,24, 15,14,13,12, 31,30,29,28}; +#endif + + // [A1 A3 B1 B3][A2 A4 B2 B4] => [A1 A2 A3 A4][B1 B2 B3 B4] + block0 = (uint32x4_p)vec_perm(x1, y1, m3); + block1 = (uint32x4_p)vec_perm(x1, y1, m4); +} + +inline void SIMON64_Dec_Block(uint32x4_p &block0, uint32x4_p &block1, + const word32 *subkeys, unsigned int rounds) +{ +#if defined(CRYPTOPP_BIG_ENDIAN) + const uint8x16_p m1 = {7,6,5,4, 15,14,13,12, 23,22,21,20, 31,30,29,28}; + const uint8x16_p m2 = {3,2,1,0, 11,10,9,8, 19,18,17,16, 27,26,25,24}; +#else + const uint8x16_p m1 = {3,2,1,0, 11,10,9,8, 19,18,17,16, 27,26,25,24}; + const uint8x16_p m2 = {7,6,5,4, 15,14,13,12, 23,22,21,20, 31,30,29,28}; +#endif + + // [A1 A2 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... + uint32x4_p x1 = vec_perm(block0, block1, m1); + uint32x4_p y1 = vec_perm(block0, block1, m2); + + if (rounds & 1) + { + std::swap(x1, y1); + const uint32x4_p rk = vec_splats(subkeys[rounds-1]); + y1 = VectorXor(VectorXor(y1, rk), SIMON64_f(x1)); + rounds--; + } + + for (int i = static_cast(rounds-2); i >= 0; i -= 2) + { + const uint32x4_p rk1 = vec_splats(subkeys[i+1]); + x1 = VectorXor(VectorXor(x1, SIMON64_f(y1)), rk1); + + const uint32x4_p rk2 = vec_splats(subkeys[i]); + y1 = VectorXor(VectorXor(y1, SIMON64_f(x1)), rk2); + } + +#if defined(CRYPTOPP_BIG_ENDIAN) + const uint8x16_p m3 = {11,10,9,8, 27,26,25,24, 15,14,13,12, 31,30,29,28}; + const uint8x16_p m4 = {3,2,1,0, 19,18,17,16, 7,6,5,4, 23,22,21,20}; +#else + const uint8x16_p m3 = {3,2,1,0, 19,18,17,16, 7,6,5,4, 23,22,21,20}; + const uint8x16_p m4 = {11,10,9,8, 27,26,25,24, 15,14,13,12, 31,30,29,28}; +#endif + + // [A1 A3 B1 B3][A2 A4 B2 B4] => [A1 A2 A3 A4][B1 B2 B3 B4] + block0 = (uint32x4_p)vec_perm(x1, y1, m3); + block1 = (uint32x4_p)vec_perm(x1, y1, m4); +} + +inline void SIMON64_Enc_6_Blocks(uint32x4_p &block0, uint32x4_p &block1, + uint32x4_p &block2, uint32x4_p &block3, uint32x4_p &block4, + uint32x4_p &block5, const word32 *subkeys, unsigned int rounds) +{ +#if defined(CRYPTOPP_BIG_ENDIAN) + const uint8x16_p m1 = {7,6,5,4, 15,14,13,12, 23,22,21,20, 31,30,29,28}; + const uint8x16_p m2 = {3,2,1,0, 11,10,9,8, 19,18,17,16, 27,26,25,24}; +#else + const uint8x16_p m1 = {3,2,1,0, 11,10,9,8, 19,18,17,16, 27,26,25,24}; + const uint8x16_p m2 = {7,6,5,4, 15,14,13,12, 23,22,21,20, 31,30,29,28}; +#endif + + // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ... + uint32x4_p x1 = (uint32x4_p)vec_perm(block0, block1, m1); + uint32x4_p y1 = (uint32x4_p)vec_perm(block0, block1, m2); + uint32x4_p x2 = (uint32x4_p)vec_perm(block2, block3, m1); + uint32x4_p y2 = (uint32x4_p)vec_perm(block2, block3, m2); + uint32x4_p x3 = (uint32x4_p)vec_perm(block4, block5, m1); + uint32x4_p y3 = (uint32x4_p)vec_perm(block4, block5, m2); + + for (int i = 0; i < static_cast(rounds & ~1)-1; i += 2) + { + const uint32x4_p rk1 = vec_splats(subkeys[i]); + y1 = VectorXor(VectorXor(y1, SIMON64_f(x1)), rk1); + y2 = VectorXor(VectorXor(y2, SIMON64_f(x2)), rk1); + y3 = VectorXor(VectorXor(y3, SIMON64_f(x3)), rk1); + + const uint32x4_p rk2 = vec_splats(subkeys[i+1]); + x1 = VectorXor(VectorXor(x1, SIMON64_f(y1)), rk2); + x2 = VectorXor(VectorXor(x2, SIMON64_f(y2)), rk2); + x3 = VectorXor(VectorXor(x3, SIMON64_f(y3)), rk2); + } + + if (rounds & 1) + { + const uint32x4_p rk = vec_splats(subkeys[rounds-1]); + y1 = VectorXor(VectorXor(y1, SIMON64_f(x1)), rk); + y2 = VectorXor(VectorXor(y2, SIMON64_f(x2)), rk); + y3 = VectorXor(VectorXor(y3, SIMON64_f(x3)), rk); + std::swap(x1, y1); std::swap(x2, y2); std::swap(x3, y3); + } + +#if defined(CRYPTOPP_BIG_ENDIAN) + const uint8x16_p m3 = {11,10,9,8, 27,26,25,24, 15,14,13,12, 31,30,29,28}; + const uint8x16_p m4 = {3,2,1,0, 19,18,17,16, 7,6,5,4, 23,22,21,20}; +#else + const uint8x16_p m3 = {3,2,1,0, 19,18,17,16, 7,6,5,4, 23,22,21,20}; + const uint8x16_p m4 = {11,10,9,8, 27,26,25,24, 15,14,13,12, 31,30,29,28}; +#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 SIMON64_Dec_6_Blocks(uint32x4_p &block0, uint32x4_p &block1, + uint32x4_p &block2, uint32x4_p &block3, uint32x4_p &block4, + uint32x4_p &block5, const word32 *subkeys, unsigned int rounds) +{ +#if defined(CRYPTOPP_BIG_ENDIAN) + const uint8x16_p m1 = {7,6,5,4, 15,14,13,12, 23,22,21,20, 31,30,29,28}; + const uint8x16_p m2 = {3,2,1,0, 11,10,9,8, 19,18,17,16, 27,26,25,24}; +#else + const uint8x16_p m1 = {3,2,1,0, 11,10,9,8, 19,18,17,16, 27,26,25,24}; + const uint8x16_p m2 = {7,6,5,4, 15,14,13,12, 23,22,21,20, 31,30,29,28}; +#endif + + // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ... + uint32x4_p x1 = (uint32x4_p)vec_perm(block0, block1, m1); + uint32x4_p y1 = (uint32x4_p)vec_perm(block0, block1, m2); + uint32x4_p x2 = (uint32x4_p)vec_perm(block2, block3, m1); + uint32x4_p y2 = (uint32x4_p)vec_perm(block2, block3, m2); + uint32x4_p x3 = (uint32x4_p)vec_perm(block4, block5, m1); + uint32x4_p y3 = (uint32x4_p)vec_perm(block4, block5, m2); + + if (rounds & 1) + { + std::swap(x1, y1); std::swap(x2, y2); std::swap(x3, y3); + const uint32x4_p rk = vec_splats(subkeys[rounds-1]); + y1 = VectorXor(VectorXor(y1, rk), SIMON64_f(x1)); + y2 = VectorXor(VectorXor(y2, rk), SIMON64_f(x2)); + y3 = VectorXor(VectorXor(y3, rk), SIMON64_f(x3)); + rounds--; + } + + for (int i = static_cast(rounds-2); i >= 0; i -= 2) + { + const uint32x4_p rk1 = vec_splats(subkeys[i+1]); + x1 = VectorXor(VectorXor(x1, SIMON64_f(y1)), rk1); + x2 = VectorXor(VectorXor(x2, SIMON64_f(y2)), rk1); + x3 = VectorXor(VectorXor(x3, SIMON64_f(y3)), rk1); + + const uint32x4_p rk2 = vec_splats(subkeys[i]); + y1 = VectorXor(VectorXor(y1, SIMON64_f(x1)), rk2); + y2 = VectorXor(VectorXor(y2, SIMON64_f(x2)), rk2); + y3 = VectorXor(VectorXor(y3, SIMON64_f(x3)), rk2); + } + +#if defined(CRYPTOPP_BIG_ENDIAN) + const uint8x16_p m3 = {11,10,9,8, 27,26,25,24, 15,14,13,12, 31,30,29,28}; + const uint8x16_p m4 = {3,2,1,0, 19,18,17,16, 7,6,5,4, 23,22,21,20}; +#else + const uint8x16_p m3 = {3,2,1,0, 19,18,17,16, 7,6,5,4, 23,22,21,20}; + const uint8x16_p m4 = {11,10,9,8, 27,26,25,24, 15,14,13,12, 31,30,29,28}; +#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 + +// ***************************** Power8 ***************************** // + +#if defined(CRYPTOPP_POWER8_AVAILABLE) + using CryptoPP::uint8x16_p; using CryptoPP::uint32x4_p; using CryptoPP::uint64x2_p; @@ -1327,6 +1569,20 @@ size_t SIMON128_Dec_AdvancedProcessBlocks_SSSE3(const word64* subKeys, size_t ro // ***************************** Power8 ***************************** // #if defined(CRYPTOPP_POWER8_AVAILABLE) +size_t SIMON64_Enc_AdvancedProcessBlocks_POWER8(const word32* subKeys, size_t rounds, + const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) +{ + return AdvancedProcessBlocks64_6x2_ALTIVEC(SIMON64_Enc_Block, SIMON64_Enc_6_Blocks, + subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags); +} + +size_t SIMON64_Dec_AdvancedProcessBlocks_POWER8(const word32* subKeys, size_t rounds, + const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) +{ + return AdvancedProcessBlocks64_6x2_ALTIVEC(SIMON64_Dec_Block, SIMON64_Dec_6_Blocks, + subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags); +} + size_t SIMON128_Enc_AdvancedProcessBlocks_POWER8(const word64* subKeys, size_t rounds, const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) { diff --git a/simon.cpp b/simon.cpp index 2f306933..d6c3480c 100644 --- a/simon.cpp +++ b/simon.cpp @@ -226,6 +226,12 @@ extern size_t SIMON128_Dec_AdvancedProcessBlocks_SSSE3(const word64* subKeys, si #endif #if (CRYPTOPP_POWER8_AVAILABLE) +extern size_t SIMON64_Enc_AdvancedProcessBlocks_POWER8(const word32* subKeys, size_t rounds, + const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags); + +extern size_t SIMON64_Dec_AdvancedProcessBlocks_POWER8(const word32* subKeys, size_t rounds, + const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags); + extern size_t SIMON128_Enc_AdvancedProcessBlocks_POWER8(const word64* subKeys, size_t rounds, const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags); @@ -242,6 +248,10 @@ std::string SIMON64::Base::AlgorithmProvider() const #if (CRYPTOPP_ARM_NEON_AVAILABLE) if (HasNEON()) return "NEON"; +#endif +#if (CRYPTOPP_POWER8_AVAILABLE) + if (HasPower8()) + return "Power8"; #endif return "C++"; } @@ -443,6 +453,11 @@ size_t SIMON64::Enc::AdvancedProcessBlocks(const byte *inBlocks, const byte *xor if (HasNEON()) return SIMON64_Enc_AdvancedProcessBlocks_NEON(m_rkeys, (size_t)m_rounds, inBlocks, xorBlocks, outBlocks, length, flags); +#endif +#if (CRYPTOPP_POWER8_AVAILABLE) + if (HasPower8()) + return SIMON64_Enc_AdvancedProcessBlocks_POWER8(m_rkeys, (size_t)m_rounds, + inBlocks, xorBlocks, outBlocks, length, flags); #endif return BlockTransformation::AdvancedProcessBlocks(inBlocks, xorBlocks, outBlocks, length, flags); } @@ -459,6 +474,11 @@ size_t SIMON64::Dec::AdvancedProcessBlocks(const byte *inBlocks, const byte *xor if (HasNEON()) return SIMON64_Dec_AdvancedProcessBlocks_NEON(m_rkeys, (size_t)m_rounds, inBlocks, xorBlocks, outBlocks, length, flags); +#endif +#if (CRYPTOPP_POWER8_AVAILABLE) + if (HasPower8()) + return SIMON64_Dec_AdvancedProcessBlocks_POWER8(m_rkeys, (size_t)m_rounds, + inBlocks, xorBlocks, outBlocks, length, flags); #endif return BlockTransformation::AdvancedProcessBlocks(inBlocks, xorBlocks, outBlocks, length, flags); } diff --git a/simon.h b/simon.h index ab5dfe7f..51a730de 100644 --- a/simon.h +++ b/simon.h @@ -18,7 +18,8 @@ #include "secblock.h" #if CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X86 || \ - CRYPTOPP_BOOL_ARM32 || CRYPTOPP_BOOL_ARM64 + CRYPTOPP_BOOL_ARM32 || CRYPTOPP_BOOL_ARM64 || \ + CRYPTOPP_BOOL_PPC32 || CRYPTOPP_BOOL_PPC64 # define CRYPTOPP_SIMON64_ADVANCED_PROCESS_BLOCKS 1 #endif diff --git a/speck-simd.cpp b/speck-simd.cpp index f1fca163..49863673 100644 --- a/speck-simd.cpp +++ b/speck-simd.cpp @@ -847,7 +847,6 @@ inline void SPECK64_Dec_6_Blocks(__m128i &block0, __m128i &block1, #if defined(CRYPTOPP_POWER8_AVAILABLE) using CryptoPP::uint8x16_p; using CryptoPP::uint32x4_p; -using CryptoPP::uint64x2_p; using CryptoPP::VectorAdd; using CryptoPP::VectorSub;