diff --git a/speck-simd.cpp b/speck-simd.cpp index f22a88bf..03145de0 100644 --- a/speck-simd.cpp +++ b/speck-simd.cpp @@ -844,6 +844,239 @@ inline void SPECK64_Dec_6_Blocks(__m128i &block0, __m128i &block1, // ***************************** Power8 ***************************** // +#if defined(CRYPTOPP_POWER8_AVAILABLE) +using CryptoPP::uint8x16_p; +using CryptoPP::uint32x4_p; +using CryptoPP::uint64x2_p; + +using CryptoPP::VectorAdd; +using CryptoPP::VectorSub; +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); +} + +void SPECK64_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); ++i) + { + const uint32x4_p rk = vec_splats(subkeys[i]); + + x1 = RotateRight32<8>(x1); + x1 = VectorAdd(x1, y1); + x1 = VectorXor(x1, rk); + + y1 = RotateLeft32<3>(y1); + y1 = VectorXor(y1, x1); + } + +#if defined(CRYPTOPP_BIG_ENDIAN) + const uint8x16_p m3 = {19,18,17,16, 3,2,1,0, 23,22,21,20, 7,6,5,4}; + const uint8x16_p m4 = {27,26,25,24, 11,10,9,8, 31,30,29,28, 15,14,13,12}; +#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); +} + +void SPECK64_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); + + for (int i = static_cast(rounds-1); i >= 0; --i) + { + const uint32x4_p rk = vec_splats(subkeys[i]); + + y1 = VectorXor(y1, x1); + y1 = RotateRight32<3>(y1); + + x1 = VectorXor(x1, rk); + x1 = VectorSub(x1, y1); + x1 = RotateLeft32<8>(x1); + } + +#if defined(CRYPTOPP_BIG_ENDIAN) + const uint8x16_p m3 = {19,18,17,16, 3,2,1,0, 23,22,21,20, 7,6,5,4}; + const uint8x16_p m4 = {27,26,25,24, 11,10,9,8, 31,30,29,28, 15,14,13,12}; +#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); +} + +void SPECK64_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 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... + 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); ++i) + { + const uint32x4_p rk = vec_splats(subkeys[i]); + + x1 = RotateRight32<8>(x1); + x2 = RotateRight32<8>(x2); + x3 = RotateRight32<8>(x3); + + x1 = VectorAdd(x1, y1); + x2 = VectorAdd(x2, y2); + x3 = VectorAdd(x3, y3); + + x1 = VectorXor(x1, rk); + x2 = VectorXor(x2, rk); + x3 = VectorXor(x3, rk); + + y1 = RotateLeft32<3>(y1); + y2 = RotateLeft32<3>(y2); + y3 = RotateLeft32<3>(y3); + + y1 = VectorXor(y1, x1); + y2 = VectorXor(y2, x2); + y3 = VectorXor(y3, x3); + } + +#if defined(CRYPTOPP_BIG_ENDIAN) + const uint8x16_p m3 = {19,18,17,16, 3,2,1,0, 23,22,21,20, 7,6,5,4}; + const uint8x16_p m4 = {27,26,25,24, 11,10,9,8, 31,30,29,28, 15,14,13,12}; +#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); + 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); +} + +void SPECK64_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 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... + 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 = static_cast(rounds-1); i >= 0; --i) + { + const uint32x4_p rk = vec_splats(subkeys[i]); + + y1 = VectorXor(y1, x1); + y2 = VectorXor(y2, x2); + y3 = VectorXor(y3, x3); + + y1 = RotateRight32<3>(y1); + y2 = RotateRight32<3>(y2); + y3 = RotateRight32<3>(y3); + + x1 = VectorXor(x1, rk); + x2 = VectorXor(x2, rk); + x3 = VectorXor(x3, rk); + + x1 = VectorSub(x1, y1); + x2 = VectorSub(x2, y2); + x3 = VectorSub(x3, y3); + + x1 = RotateLeft32<8>(x1); + x2 = RotateLeft32<8>(x2); + x3 = RotateLeft32<8>(x3); + } + +#if defined(CRYPTOPP_BIG_ENDIAN) + const uint8x16_p m3 = {19,18,17,16, 3,2,1,0, 23,22,21,20, 7,6,5,4}; + const uint8x16_p m4 = {27,26,25,24, 11,10,9,8, 31,30,29,28, 15,14,13,12}; +#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); + 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 // POWER8 + +// ***************************** Power8 ***************************** // + #if defined(CRYPTOPP_POWER8_AVAILABLE) using CryptoPP::uint8x16_p; @@ -853,7 +1086,6 @@ using CryptoPP::uint64x2_p; using CryptoPP::VectorAdd; using CryptoPP::VectorSub; using CryptoPP::VectorXor; -using CryptoPP::VectorSwapWords; // Rotate left by bit count template @@ -1143,6 +1375,20 @@ size_t SPECK128_Dec_AdvancedProcessBlocks_SSSE3(const word64* subKeys, size_t ro // ***************************** Power8 ***************************** // #if defined(CRYPTOPP_POWER8_AVAILABLE) +size_t SPECK64_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(SPECK64_Enc_Block, SPECK64_Enc_6_Blocks, + subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags); +} + +size_t SPECK64_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(SPECK64_Dec_Block, SPECK64_Dec_6_Blocks, + subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags); +} + size_t SPECK128_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/speck.cpp b/speck.cpp index f0bfb8ac..da5d9afd 100644 --- a/speck.cpp +++ b/speck.cpp @@ -201,6 +201,12 @@ extern size_t SPECK128_Dec_AdvancedProcessBlocks_SSSE3(const word64* subKeys, si #endif #if defined(CRYPTOPP_POWER8_AVAILABLE) +extern size_t SPECK64_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 SPECK64_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 SPECK128_Enc_AdvancedProcessBlocks_POWER8(const word64* subKeys, size_t rounds, const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags); @@ -217,6 +223,10 @@ std::string SPECK64::Base::AlgorithmProvider() const #if (CRYPTOPP_ARM_NEON_AVAILABLE) if (HasNEON()) return "NEON"; +#endif +#if (CRYPTOPP_POWER8_AVAILABLE) + if (HasPower8()) + return "Power8"; #endif return "C++"; } @@ -418,6 +428,11 @@ size_t SPECK64::Enc::AdvancedProcessBlocks(const byte *inBlocks, const byte *xor if (HasNEON()) return SPECK64_Enc_AdvancedProcessBlocks_NEON(m_rkeys, (size_t)m_rounds, inBlocks, xorBlocks, outBlocks, length, flags); +#endif +#if (CRYPTOPP_POWER8_AVAILABLE) + if (HasPower8()) + return SPECK64_Enc_AdvancedProcessBlocks_POWER8(m_rkeys, (size_t)m_rounds, + inBlocks, xorBlocks, outBlocks, length, flags); #endif return BlockTransformation::AdvancedProcessBlocks(inBlocks, xorBlocks, outBlocks, length, flags); } @@ -434,6 +449,11 @@ size_t SPECK64::Dec::AdvancedProcessBlocks(const byte *inBlocks, const byte *xor if (HasNEON()) return SPECK64_Dec_AdvancedProcessBlocks_NEON(m_rkeys, (size_t)m_rounds, inBlocks, xorBlocks, outBlocks, length, flags); +#endif +#if (CRYPTOPP_POWER8_AVAILABLE) + if (HasPower8()) + return SPECK64_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/speck.h b/speck.h index fbfc0b43..c0b67315 100644 --- a/speck.h +++ b/speck.h @@ -17,11 +17,15 @@ #include "seckey.h" #include "secblock.h" -#if CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_ARM32 || CRYPTOPP_BOOL_ARM64 +#if CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X86 || \ + CRYPTOPP_BOOL_ARM32 || CRYPTOPP_BOOL_ARM64 || \ + CRYPTOPP_BOOL_PPC32 || CRYPTOPP_BOOL_PPC64 # define CRYPTOPP_SPECK64_ADVANCED_PROCESS_BLOCKS 1 #endif -#if CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_ARM32 || CRYPTOPP_BOOL_ARM64 || CRYPTOPP_BOOL_PPC32 || CRYPTOPP_BOOL_PPC64 +#if CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X86 || \ + CRYPTOPP_BOOL_ARM32 || CRYPTOPP_BOOL_ARM64 || \ + CRYPTOPP_BOOL_PPC32 || CRYPTOPP_BOOL_PPC64 # define CRYPTOPP_SPECK128_ADVANCED_PROCESS_BLOCKS 1 #endif