From 7a43a040488bd302bfbc29dfa655452ccfb3d5a3 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Wed, 15 Aug 2018 09:59:40 -0400 Subject: Split simon-simd.cpp and speck-simd.cpp into separate source files SIMON-64 and SIMON-128 have different ISA requirements. The same applies to SPECK-64 and SPECK-128. GCC generated code that resulted in a SIGILL due to the ISA differences on a down level machine. The instructions was a mtfprwz from POWER8. It was prsent in a function prologue on a POWER7 machine. --- Filelist.txt | 6 +- GNUmakefile | 74 ++- cryptest.nmake | 4 +- cryptlib.vcxproj | 6 +- cryptlib.vcxproj.filters | 10 +- simon-simd.cpp | 1545 ---------------------------------------------- simon64-simd.cpp | 1033 +++++++++++++++++++++++++++++++ speck-simd.cpp | 1410 ------------------------------------------ speck64-simd.cpp | 745 ++++++++++++++++++++++ 9 files changed, 1846 insertions(+), 2987 deletions(-) delete mode 100644 simon-simd.cpp create mode 100644 simon64-simd.cpp delete mode 100644 speck-simd.cpp create mode 100644 speck64-simd.cpp diff --git a/Filelist.txt b/Filelist.txt index d0e25b4b..6b0ffe3d 100644 --- a/Filelist.txt +++ b/Filelist.txt @@ -300,7 +300,8 @@ simeck.cpp simeck-simd.cpp simeck.h simon.cpp -simon-simd.cpp +simon64-simd.cpp +simon128-simd.cpp simon.h skipjack.cpp skipjack.h @@ -315,7 +316,8 @@ socketft.h sosemanuk.cpp sosemanuk.h speck.cpp -speck-simd.cpp +speck64-simd.cpp +speck128-simd.cpp speck.h square.cpp square.h diff --git a/GNUmakefile b/GNUmakefile index 3eca5e16..531b5d19 100755 --- a/GNUmakefile +++ b/GNUmakefile @@ -246,15 +246,17 @@ ifeq ($(findstring -DCRYPTOPP_DISABLE_SSSE3,$(CXXFLAGS)),) LEA_FLAG = -mssse3 SSSE3_FLAG = -mssse3 SIMECK_FLAG = -mssse3 - SIMON_FLAG = -mssse3 - SPECK_FLAG = -mssse3 + SIMON64_FLAG = -mssse3 + SIMON128_FLAG = -mssse3 + SPECK64_FLAG = -mssse3 + SPECK128_FLAG = -mssse3 endif ifeq ($(findstring -DCRYPTOPP_DISABLE_SSE4,$(CXXFLAGS)),) HAVE_SSE4 = $(shell $(CXX) $(CXXFLAGS) -DADHOC_MAIN -msse4.1 -dM -E adhoc.cpp 2>&1 | $(GREP) -i -c __SSE4_1__) ifeq ($(HAVE_SSE4),1) BLAKE2_FLAG = -msse4.1 - SIMON_FLAG = -msse4.1 - SPECK_FLAG = -msse4.1 + SIMON64_FLAG = -msse4.1 + SPECK64_FLAG = -msse4.1 endif HAVE_SSE4 = $(shell $(CXX) $(CXXFLAGS) -DADHOC_MAIN -msse4.2 -dM -E adhoc.cpp 2>&1 | $(GREP) -i -c __SSE4_2__) ifeq ($(HAVE_SSE4),1) @@ -296,15 +298,17 @@ ifeq ($(SUN_COMPILER),1) CHAM_FLAG = -xarch=ssse3 -D__SSSE3__=1 LEA_FLAG = -xarch=ssse3 -D__SSSE3__=1 SIMECK_FLAG = -xarch=ssse3 -D__SSSE3__=1 - SIMON_FLAG = -xarch=ssse3 -D__SSSE3__=1 - SPECK_FLAG = -xarch=ssse3 -D__SSSE3__=1 + SIMON64_FLAG = -xarch=ssse3 -D__SSSE3__=1 + SIMON128_FLAG = -xarch=ssse3 -D__SSSE3__=1 + SPECK64_FLAG = -xarch=ssse3 -D__SSSE3__=1 + SPECK128_FLAG = -xarch=ssse3 -D__SSSE3__=1 LDFLAGS += -xarch=ssse3 endif COUNT := $(shell $(CXX) $(CXXFLAGS) -E -xarch=sse4_1 -xdumpmacros /dev/null 2>&1 | $(GREP) -i -c "illegal") ifeq ($(COUNT),0) BLAKE2_FLAG = -xarch=sse4_1 -D__SSE4_1__=1 - SIMON_FLAG = -xarch=sse4_1 -D__SSE4_1__=1 - SPECK_FLAG = -xarch=sse4_1 -D__SSE4_1__=1 + SIMON64_FLAG = -xarch=sse4_1 -D__SSE4_1__=1 + SPECK64_FLAG = -xarch=sse4_1 -D__SSE4_1__=1 LDFLAGS += -xarch=sse4_1 endif COUNT := $(shell $(CXX) $(CXXFLAGS) -E -xarch=sse4_2 -xdumpmacros /dev/null 2>&1 | $(GREP) -i -c "illegal") @@ -375,8 +379,10 @@ ifeq ($(IS_NEON),1) LEA_FLAG = -march=armv7-a -mfloat-abi=$(FP_ABI) -mfpu=neon SHA_FLAG = -march=armv7-a -mfloat-abi=$(FP_ABI) -mfpu=neon SIMECK_FLAG = -march=armv7-a -mfloat-abi=$(FP_ABI) -mfpu=neon - SIMON_FLAG = -march=armv7-a -mfloat-abi=$(FP_ABI) -mfpu=neon - SPECK_FLAG = -march=armv7-a -mfloat-abi=$(FP_ABI) -mfpu=neon + SIMON64_FLAG = -march=armv7-a -mfloat-abi=$(FP_ABI) -mfpu=neon + SIMON128_FLAG = -march=armv7-a -mfloat-abi=$(FP_ABI) -mfpu=neon + SPECK64_FLAG = -march=armv7-a -mfloat-abi=$(FP_ABI) -mfpu=neon + SPECK128_FLAG = -march=armv7-a -mfloat-abi=$(FP_ABI) -mfpu=neon SM4_FLAG = -march=armv7-a -mfloat-abi=$(FP_ABI) -mfpu=neon endif endif @@ -390,8 +396,10 @@ ifeq ($(IS_ARMV8),1) LEA_FLAG = -march=armv8-a NEON_FLAG = -march=armv8-a SIMECK_FLAG = -march=armv8-a - SIMON_FLAG = -march=armv8-a - SPECK_FLAG = -march=armv8-a + SIMON64_FLAG = -march=armv8-a + SIMON128_FLAG = -march=armv8-a + SPECK64_FLAG = -march=armv8-a + SPECK128_FLAG = -march=armv8-a SM4_FLAG = -march=armv8-a endif HAVE_CRC = $(shell $(CXX) $(CXXFLAGS) -DADHOC_MAIN -march=armv8-a+crc -dM -E adhoc.cpp 2>&1 | $(GREP) -i -c __ARM_FEATURE_CRC32) @@ -422,8 +430,10 @@ ifneq ($(IS_PPC32)$(IS_PPC64),00) GCM_FLAG = $(POWER8_FLAG) SHA_FLAG = $(POWER8_FLAG) SM4_FLAG = $(POWER8_FLAG) - SIMON_FLAG = $(POWER8_FLAG) - SPECK_FLAG = $(POWER8_FLAG) + SIMON64_FLAG = $(POWER8_FLAG) + SIMON128_FLAG = $(POWER8_FLAG) + SPECK64_FLAG = $(POWER8_FLAG) + SPECK128_FLAG = $(POWER8_FLAG) endif # GCC and some compatibles @@ -435,6 +445,8 @@ ifneq ($(IS_PPC32)$(IS_PPC64),00) CHAM_FLAG = $(POWER7_FLAG) LEA_FLAG = $(POWER7_FLAG) SIMECK_FLAG = $(POWER7_FLAG) + SIMON64_FLAG = $(POWER7_FLAG) + SPECK64_FLAG = $(POWER7_FLAG) endif # GCC and some compatibles @@ -451,8 +463,10 @@ ifneq ($(IS_PPC32)$(IS_PPC64),00) GCM_FLAG = $(POWER8_FLAG) SHA_FLAG = $(POWER8_FLAG) SM4_FLAG = $(POWER8_FLAG) - SIMON_FLAG = $(POWER8_FLAG) - SPECK_FLAG = $(POWER8_FLAG) + SIMON64_FLAG = $(POWER8_FLAG) + SIMON128_FLAG = $(POWER8_FLAG) + SPECK64_FLAG = $(POWER8_FLAG) + SPECK128_FLAG = $(POWER8_FLAG) endif # IBM XL C/C++ @@ -464,6 +478,8 @@ ifneq ($(IS_PPC32)$(IS_PPC64),00) CHAM_FLAG = $(POWER7_FLAG) LEA_FLAG = $(POWER7_FLAG) SIMECK_FLAG = $(POWER7_FLAG) + SIMON64_FLAG = $(POWER7_FLAG) + SPECK64_FLAG = $(POWER7_FLAG) endif # IBM XL C/C++ @@ -483,8 +499,10 @@ ifneq ($(IS_PPC32)$(IS_PPC64),00) CHAM_FLAG = $(POWER8_FLAG) LEA_FLAG = $(POWER8_FLAG) SIMECK_FLAG = $(POWER8_FLAG) - SIMON_FLAG = $(POWER8_FLAG) - SPECK_FLAG = $(POWER8_FLAG) + SIMON64_FLAG = $(POWER8_FLAG) + SIMON128_FLAG = $(POWER8_FLAG) + SPECK64_FLAG = $(POWER8_FLAG) + SPECK128_FLAG = $(POWER8_FLAG) ALTIVEC_FLAG = $(POWER8_FLAG) endif @@ -1166,13 +1184,21 @@ shacal2-simd.o : shacal2-simd.cpp simeck-simd.o : simeck-simd.cpp $(CXX) $(strip $(CXXFLAGS) $(SIMECK_FLAG) -c) $< -# SSSE3 or NEON available -simon-simd.o : simon-simd.cpp - $(CXX) $(strip $(CXXFLAGS) $(SIMON_FLAG) -c) $< +# SSE4.1, NEON or POWER7 available +simon64-simd.o : simon64-simd.cpp + $(CXX) $(strip $(CXXFLAGS) $(SIMON64_FLAG) -c) $< -# SSSE3 or NEON available -speck-simd.o : speck-simd.cpp - $(CXX) $(strip $(CXXFLAGS) $(SPECK_FLAG) -c) $< +# SSSE3, NEON or POWER8 available +simon128-simd.o : simon128-simd.cpp + $(CXX) $(strip $(CXXFLAGS) $(SIMON128_FLAG) -c) $< + +# SSE4.1, NEON or POWER7 available +speck64-simd.o : speck64-simd.cpp + $(CXX) $(strip $(CXXFLAGS) $(SPECK64_FLAG) -c) $< + +# SSSE3, NEON or POWER8 available +speck128-simd.o : speck128-simd.cpp + $(CXX) $(strip $(CXXFLAGS) $(SPECK128_FLAG) -c) $< # AESNI available sm4-simd.o : sm4-simd.cpp diff --git a/cryptest.nmake b/cryptest.nmake index e2040ca7..a0db6a29 100644 --- a/cryptest.nmake +++ b/cryptest.nmake @@ -47,9 +47,9 @@ # If you use 'make sources' from Linux makefile, then add 'winpipes.cpp' to the list below. -LIB_SRCS = cryptlib.cpp cpu.cpp integer.cpp 3way.cpp adler32.cpp algebra.cpp algparam.cpp arc4.cpp aria-simd.cpp aria.cpp ariatab.cpp asn.cpp authenc.cpp base32.cpp base64.cpp basecode.cpp bfinit.cpp blake2-simd.cpp blake2.cpp blowfish.cpp blumshub.cpp camellia.cpp cast.cpp casts.cpp cbcmac.cpp ccm.cpp chacha.cpp cham.cpp cham-simd.cpp channels.cpp cmac.cpp crc-simd.cpp crc.cpp default.cpp des.cpp dessp.cpp dh.cpp dh2.cpp dll.cpp dsa.cpp eax.cpp ec2n.cpp eccrypto.cpp ecp.cpp elgamal.cpp emsa2.cpp eprecomp.cpp esign.cpp files.cpp filters.cpp fips140.cpp fipstest.cpp gcm-simd.cpp gcm.cpp gf256.cpp gf2_32.cpp gf2n.cpp gfpcrypt.cpp gost.cpp gzip.cpp hc128.cpp hc256.cpp hex.cpp hight.cpp hmac.cpp hrtimer.cpp ida.cpp idea.cpp iterhash.cpp kalyna.cpp kalynatab.cpp keccak.cpp lea.cpp lea-simd.cpp luc.cpp mars.cpp marss.cpp md2.cpp md4.cpp md5.cpp misc.cpp modes.cpp mqueue.cpp mqv.cpp nbtheory.cpp neon-simd.cpp network.cpp oaep.cpp osrng.cpp padlkrng.cpp panama.cpp pkcspad.cpp poly1305.cpp polynomi.cpp pssr.cpp pubkey.cpp queue.cpp rabin.cpp randpool.cpp rabbit.cpp rc2.cpp rc5.cpp rc6.cpp rdrand.cpp rdtables.cpp rijndael-simd.cpp rijndael.cpp ripemd.cpp rng.cpp rsa.cpp rw.cpp safer.cpp salsa.cpp scrypt.cpp seal.cpp seed.cpp serpent.cpp sha-simd.cpp sha.cpp sha3.cpp shacal2-simd.cpp shacal2.cpp shark.cpp sharkbox.cpp simeck-simd.cpp simeck.cpp simon.cpp simon-simd.cpp skipjack.cpp sm3.cpp sm4.cpp sm4-simd.cpp socketft.cpp sosemanuk.cpp speck.cpp speck-simd.cpp square.cpp squaretb.cpp sse-simd.cpp strciphr.cpp tea.cpp tftables.cpp threefish.cpp tiger.cpp tigertab.cpp trdlocal.cpp ttmac.cpp tweetnacl.cpp twofish.cpp vmac.cpp wait.cpp wake.cpp whrlpool.cpp winpipes.cpp xtr.cpp xtrcrypt.cpp zdeflate.cpp zinflate.cpp zlib.cpp +LIB_SRCS = cryptlib.cpp cpu.cpp integer.cpp 3way.cpp adler32.cpp algebra.cpp algparam.cpp arc4.cpp aria-simd.cpp aria.cpp ariatab.cpp asn.cpp authenc.cpp base32.cpp base64.cpp basecode.cpp bfinit.cpp blake2-simd.cpp blake2.cpp blowfish.cpp blumshub.cpp camellia.cpp cast.cpp casts.cpp cbcmac.cpp ccm.cpp chacha.cpp cham.cpp cham-simd.cpp channels.cpp cmac.cpp crc-simd.cpp crc.cpp default.cpp des.cpp dessp.cpp dh.cpp dh2.cpp dll.cpp dsa.cpp eax.cpp ec2n.cpp eccrypto.cpp ecp.cpp elgamal.cpp emsa2.cpp eprecomp.cpp esign.cpp files.cpp filters.cpp fips140.cpp fipstest.cpp gcm-simd.cpp gcm.cpp gf256.cpp gf2_32.cpp gf2n.cpp gfpcrypt.cpp gost.cpp gzip.cpp hc128.cpp hc256.cpp hex.cpp hight.cpp hmac.cpp hrtimer.cpp ida.cpp idea.cpp iterhash.cpp kalyna.cpp kalynatab.cpp keccak.cpp lea.cpp lea-simd.cpp luc.cpp mars.cpp marss.cpp md2.cpp md4.cpp md5.cpp misc.cpp modes.cpp mqueue.cpp mqv.cpp nbtheory.cpp neon-simd.cpp network.cpp oaep.cpp osrng.cpp padlkrng.cpp panama.cpp pkcspad.cpp poly1305.cpp polynomi.cpp pssr.cpp pubkey.cpp queue.cpp rabin.cpp randpool.cpp rabbit.cpp rc2.cpp rc5.cpp rc6.cpp rdrand.cpp rdtables.cpp rijndael-simd.cpp rijndael.cpp ripemd.cpp rng.cpp rsa.cpp rw.cpp safer.cpp salsa.cpp scrypt.cpp seal.cpp seed.cpp serpent.cpp sha-simd.cpp sha.cpp sha3.cpp shacal2-simd.cpp shacal2.cpp shark.cpp sharkbox.cpp simeck-simd.cpp simeck.cpp simon.cpp simon64-simd.cpp simon128-simd.cpp skipjack.cpp sm3.cpp sm4.cpp sm4-simd.cpp socketft.cpp sosemanuk.cpp speck.cpp speck64-simd.cpp speck128-simd.cpp square.cpp squaretb.cpp sse-simd.cpp strciphr.cpp tea.cpp tftables.cpp threefish.cpp tiger.cpp tigertab.cpp trdlocal.cpp ttmac.cpp tweetnacl.cpp twofish.cpp vmac.cpp wait.cpp wake.cpp whrlpool.cpp winpipes.cpp xtr.cpp xtrcrypt.cpp zdeflate.cpp zinflate.cpp zlib.cpp -LIB_OBJS = cryptlib.obj cpu.obj integer.obj 3way.obj adler32.obj algebra.obj algparam.obj arc4.obj aria-simd.obj aria.obj ariatab.obj asn.obj authenc.obj base32.obj base64.obj basecode.obj bfinit.obj blake2-simd.obj blake2.obj blowfish.obj blumshub.obj camellia.obj cast.obj casts.obj cbcmac.obj ccm.obj chacha.obj cham.obj cham-simd.obj channels.obj cmac.obj crc-simd.obj crc.obj default.obj des.obj dessp.obj dh.obj dh2.obj dll.obj dsa.obj eax.obj ec2n.obj eccrypto.obj ecp.obj elgamal.obj emsa2.obj eprecomp.obj esign.obj files.obj filters.obj fips140.obj fipstest.obj gcm-simd.obj gcm.obj gf256.obj gf2_32.obj gf2n.obj gfpcrypt.obj gost.obj gzip.obj hc128.obj hc256.obj hex.obj hight.obj hmac.obj hrtimer.obj ida.obj idea.obj iterhash.obj kalyna.obj kalynatab.obj keccak.obj lea.obj lea-simd.obj luc.obj mars.obj marss.obj md2.obj md4.obj md5.obj misc.obj modes.obj mqueue.obj mqv.obj nbtheory.obj neon-simd.obj network.obj oaep.obj osrng.obj padlkrng.obj panama.obj pkcspad.obj poly1305.obj polynomi.obj pssr.obj pubkey.obj queue.obj rabin.obj randpool.obj rabbit.obj rc2.obj rc5.obj rc6.obj rdrand.obj rdtables.obj rijndael-simd.obj rijndael.obj ripemd.obj rng.obj rsa.obj rw.obj safer.obj salsa.obj scrypt.obj seal.obj seed.obj serpent.obj sha-simd.obj sha.obj sha3.obj shacal2-simd.obj shacal2.obj shark.obj sharkbox.obj simeck-simd.obj simeck.obj simon.obj simon-simd.obj skipjack.obj sm3.obj sm4.obj sm4-simd.obj socketft.obj sosemanuk.obj speck.obj speck-simd.obj square.obj squaretb.obj sse-simd.obj strciphr.obj tea.obj tftables.obj threefish.obj tiger.obj tigertab.obj trdlocal.obj ttmac.obj tweetnacl.obj twofish.obj vmac.obj wait.obj wake.obj whrlpool.obj winpipes.obj xtr.obj xtrcrypt.obj zdeflate.obj zinflate.obj zlib.obj +LIB_OBJS = cryptlib.obj cpu.obj integer.obj 3way.obj adler32.obj algebra.obj algparam.obj arc4.obj aria-simd.obj aria.obj ariatab.obj asn.obj authenc.obj base32.obj base64.obj basecode.obj bfinit.obj blake2-simd.obj blake2.obj blowfish.obj blumshub.obj camellia.obj cast.obj casts.obj cbcmac.obj ccm.obj chacha.obj cham.obj cham-simd.obj channels.obj cmac.obj crc-simd.obj crc.obj default.obj des.obj dessp.obj dh.obj dh2.obj dll.obj dsa.obj eax.obj ec2n.obj eccrypto.obj ecp.obj elgamal.obj emsa2.obj eprecomp.obj esign.obj files.obj filters.obj fips140.obj fipstest.obj gcm-simd.obj gcm.obj gf256.obj gf2_32.obj gf2n.obj gfpcrypt.obj gost.obj gzip.obj hc128.obj hc256.obj hex.obj hight.obj hmac.obj hrtimer.obj ida.obj idea.obj iterhash.obj kalyna.obj kalynatab.obj keccak.obj lea.obj lea-simd.obj luc.obj mars.obj marss.obj md2.obj md4.obj md5.obj misc.obj modes.obj mqueue.obj mqv.obj nbtheory.obj neon-simd.obj network.obj oaep.obj osrng.obj padlkrng.obj panama.obj pkcspad.obj poly1305.obj polynomi.obj pssr.obj pubkey.obj queue.obj rabin.obj randpool.obj rabbit.obj rc2.obj rc5.obj rc6.obj rdrand.obj rdtables.obj rijndael-simd.obj rijndael.obj ripemd.obj rng.obj rsa.obj rw.obj safer.obj salsa.obj scrypt.obj seal.obj seed.obj serpent.obj sha-simd.obj sha.obj sha3.obj shacal2-simd.obj shacal2.obj shark.obj sharkbox.obj simeck-simd.obj simeck.obj simon.obj simon64-simd.obj simon128-simd.obj skipjack.obj sm3.obj sm4.obj sm4-simd.obj socketft.obj sosemanuk.obj speck.obj speck64-simd.obj speck128-simd.obj square.obj squaretb.obj sse-simd.obj strciphr.obj tea.obj tftables.obj threefish.obj tiger.obj tigertab.obj trdlocal.obj ttmac.obj tweetnacl.obj twofish.obj vmac.obj wait.obj wake.obj whrlpool.obj winpipes.obj xtr.obj xtrcrypt.obj zdeflate.obj zinflate.obj zlib.obj TEST_SRCS = bench1.cpp bench2.cpp bench3.cpp test.cpp validat0.cpp validat1.cpp validat2.cpp validat3.cpp validat4.cpp validat5.cpp validat6.cpp validat7.cpp validat8.cpp validat9.cpp validat10.cpp datatest.cpp regtest1.cpp regtest2.cpp regtest3.cpp regtest4.cpp fipsalgt.cpp dlltest.cpp fipstest.cpp diff --git a/cryptlib.vcxproj b/cryptlib.vcxproj index 4c2b0fb8..042fab7f 100644 --- a/cryptlib.vcxproj +++ b/cryptlib.vcxproj @@ -301,7 +301,8 @@ - + + @@ -310,7 +311,8 @@ - + + diff --git a/cryptlib.vcxproj.filters b/cryptlib.vcxproj.filters index 8e36a50e..6d37edbd 100644 --- a/cryptlib.vcxproj.filters +++ b/cryptlib.vcxproj.filters @@ -395,7 +395,10 @@ Source Files - + + Source Files + + Source Files @@ -416,7 +419,10 @@ Source Files - + + Source Files + + Source Files diff --git a/simon-simd.cpp b/simon-simd.cpp deleted file mode 100644 index 5984967e..00000000 --- a/simon-simd.cpp +++ /dev/null @@ -1,1545 +0,0 @@ -// simon-simd.cpp - written and placed in the public domain by Jeffrey Walton -// -// This source file uses intrinsics and built-ins to gain access to -// SSSE3, ARM NEON and ARMv8a, and Power7 Altivec instructions. A separate -// source file is needed because additional CXXFLAGS are required to enable -// the appropriate instructions sets in some build configurations. - -#include "pch.h" -#include "config.h" - -#include "simon.h" -#include "misc.h" -#include "adv-simd.h" - -// Uncomment for benchmarking C++ against SSE or NEON. -// Do so in both simon.cpp and simon-simd.cpp. -// #undef CRYPTOPP_SSSE3_AVAILABLE -// #undef CRYPTOPP_SSE41_AVAILABLE -// #undef CRYPTOPP_ARM_NEON_AVAILABLE - -#if (CRYPTOPP_SSSE3_AVAILABLE) -# include -# include -#endif - -#if (CRYPTOPP_SSE41_AVAILABLE) -# include -#endif - -#if defined(__AVX512F__) && defined(__AVX512VL__) -# define CRYPTOPP_AVX512_ROTATE 1 -# include -#endif - -#if (CRYPTOPP_ARM_NEON_AVAILABLE) -# include -#endif - -// Can't use CRYPTOPP_ARM_XXX_AVAILABLE because too many -// compilers don't follow ACLE conventions for the include. -#if defined(CRYPTOPP_ARM_ACLE_AVAILABLE) -# include -# include -#endif - -#if defined(CRYPTOPP_POWER7_AVAILABLE) -# include "ppc-simd.h" -#endif - -// Squash MS LNK4221 and libtool warnings -extern const char SIMON_SIMD_FNAME[] = __FILE__; - -ANONYMOUS_NAMESPACE_BEGIN - -using CryptoPP::byte; -using CryptoPP::word32; -using CryptoPP::word64; -using CryptoPP::rotlFixed; -using CryptoPP::rotrFixed; -using CryptoPP::vec_swap; // SunCC - -// *************************** ARM NEON ************************** // - -#if (CRYPTOPP_ARM_NEON_AVAILABLE) - -template -inline T UnpackHigh32(const T& a, const T& b) -{ - const uint32x2_t x(vget_high_u32((uint32x4_t)a)); - const uint32x2_t y(vget_high_u32((uint32x4_t)b)); - const uint32x2x2_t r = vzip_u32(x, y); - return (T)vcombine_u32(r.val[0], r.val[1]); -} - -template -inline T UnpackLow32(const T& a, const T& b) -{ - const uint32x2_t x(vget_low_u32((uint32x4_t)a)); - const uint32x2_t y(vget_low_u32((uint32x4_t)b)); - const uint32x2x2_t r = vzip_u32(x, y); - return (T)vcombine_u32(r.val[0], r.val[1]); -} - -template -inline uint32x4_t RotateLeft32(const uint32x4_t& val) -{ - const uint32x4_t a(vshlq_n_u32(val, R)); - const uint32x4_t b(vshrq_n_u32(val, 32 - R)); - return vorrq_u32(a, b); -} - -template -inline uint32x4_t RotateRight32(const uint32x4_t& val) -{ - const uint32x4_t a(vshlq_n_u32(val, 32 - R)); - const uint32x4_t b(vshrq_n_u32(val, R)); - return vorrq_u32(a, b); -} - -#if defined(__aarch32__) || defined(__aarch64__) -// Faster than two Shifts and an Or. Thanks to Louis Wingers and Bryan Weeks. -template <> -inline uint32x4_t RotateLeft32<8>(const uint32x4_t& val) -{ -#if defined(CRYPTOPP_BIG_ENDIAN) - const uint8_t maskb[16] = { 14,13,12,15, 10,9,8,11, 6,5,4,7, 2,1,0,3 }; - const uint8x16_t mask = vld1q_u8(maskb); -#else - const uint8_t maskb[16] = { 3,0,1,2, 7,4,5,6, 11,8,9,10, 15,12,13,14 }; - const uint8x16_t mask = vld1q_u8(maskb); -#endif - - return vreinterpretq_u32_u8( - vqtbl1q_u8(vreinterpretq_u8_u32(val), mask)); -} - -// Faster than two Shifts and an Or. Thanks to Louis Wingers and Bryan Weeks. -template <> -inline uint32x4_t RotateRight32<8>(const uint32x4_t& val) -{ -#if defined(CRYPTOPP_BIG_ENDIAN) - const uint8_t maskb[16] = { 12,15,14,13, 8,11,10,9, 4,7,6,5, 0,3,2,1 }; - const uint8x16_t mask = vld1q_u8(maskb); -#else - const uint8_t maskb[16] = { 1,2,3,0, 5,6,7,4, 9,10,11,8, 13,14,14,12 }; - const uint8x16_t mask = vld1q_u8(maskb); -#endif - - return vreinterpretq_u32_u8( - vqtbl1q_u8(vreinterpretq_u8_u32(val), mask)); -} -#endif - -inline uint32x4_t SIMON64_f(const uint32x4_t& val) -{ - return veorq_u32(RotateLeft32<2>(val), - vandq_u32(RotateLeft32<1>(val), RotateLeft32<8>(val))); -} - -inline void SIMON64_Enc_Block(uint32x4_t &block1, uint32x4_t &block0, - const word32 *subkeys, unsigned int rounds) -{ - // [A1 A2 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... - uint32x4_t x1 = vuzpq_u32(block0, block1).val[1]; - uint32x4_t y1 = vuzpq_u32(block0, block1).val[0]; - - for (int i = 0; i < static_cast(rounds & ~1)-1; i += 2) - { - const uint32x4_t rk1 = vld1q_dup_u32(subkeys+i); - y1 = veorq_u32(veorq_u32(y1, SIMON64_f(x1)), rk1); - - const uint32x4_t rk2 = vld1q_dup_u32(subkeys+i+1); - x1 = veorq_u32(veorq_u32(x1, SIMON64_f(y1)), rk2); - } - - if (rounds & 1) - { - const uint32x4_t rk = vld1q_dup_u32(subkeys+rounds-1); - - y1 = veorq_u32(veorq_u32(y1, SIMON64_f(x1)), rk); - std::swap(x1, y1); - } - - // [A1 A3 B1 B3][A2 A4 B2 B4] => [A1 A2 A3 A4][B1 B2 B3 B4] - block0 = UnpackLow32(y1, x1); - block1 = UnpackHigh32(y1, x1); -} - -inline void SIMON64_Dec_Block(uint32x4_t &block0, uint32x4_t &block1, - const word32 *subkeys, unsigned int rounds) -{ - // [A1 A2 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... - uint32x4_t x1 = vuzpq_u32(block0, block1).val[1]; - uint32x4_t y1 = vuzpq_u32(block0, block1).val[0]; - - if (rounds & 1) - { - std::swap(x1, y1); - const uint32x4_t rk = vld1q_dup_u32(subkeys + rounds - 1); - - y1 = veorq_u32(veorq_u32(y1, rk), SIMON64_f(x1)); - rounds--; - } - - for (int i = static_cast(rounds-2); i >= 0; i -= 2) - { - const uint32x4_t rk1 = vld1q_dup_u32(subkeys+i+1); - x1 = veorq_u32(veorq_u32(x1, SIMON64_f(y1)), rk1); - - const uint32x4_t rk2 = vld1q_dup_u32(subkeys+i); - y1 = veorq_u32(veorq_u32(y1, SIMON64_f(x1)), rk2); - } - - // [A1 A3 B1 B3][A2 A4 B2 B4] => [A1 A2 A3 A4][B1 B2 B3 B4] - block0 = UnpackLow32(y1, x1); - block1 = UnpackHigh32(y1, x1); -} - -inline void SIMON64_Enc_6_Blocks(uint32x4_t &block0, uint32x4_t &block1, - uint32x4_t &block2, uint32x4_t &block3, uint32x4_t &block4, uint32x4_t &block5, - const word32 *subkeys, unsigned int rounds) -{ - // [A1 A2 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... - uint32x4_t x1 = vuzpq_u32(block0, block1).val[1]; - uint32x4_t y1 = vuzpq_u32(block0, block1).val[0]; - uint32x4_t x2 = vuzpq_u32(block2, block3).val[1]; - uint32x4_t y2 = vuzpq_u32(block2, block3).val[0]; - uint32x4_t x3 = vuzpq_u32(block4, block5).val[1]; - uint32x4_t y3 = vuzpq_u32(block4, block5).val[0]; - - for (int i = 0; i < static_cast(rounds & ~1) - 1; i += 2) - { - const uint32x4_t rk1 = vld1q_dup_u32(subkeys+i); - y1 = veorq_u32(veorq_u32(y1, SIMON64_f(x1)), rk1); - y2 = veorq_u32(veorq_u32(y2, SIMON64_f(x2)), rk1); - y3 = veorq_u32(veorq_u32(y3, SIMON64_f(x3)), rk1); - - const uint32x4_t rk2 = vld1q_dup_u32(subkeys+i+1); - x1 = veorq_u32(veorq_u32(x1, SIMON64_f(y1)), rk2); - x2 = veorq_u32(veorq_u32(x2, SIMON64_f(y2)), rk2); - x3 = veorq_u32(veorq_u32(x3, SIMON64_f(y3)), rk2); - } - - if (rounds & 1) - { - const uint32x4_t rk = vld1q_dup_u32(subkeys + rounds - 1); - - y1 = veorq_u32(veorq_u32(y1, SIMON64_f(x1)), rk); - y2 = veorq_u32(veorq_u32(y2, SIMON64_f(x2)), rk); - y3 = veorq_u32(veorq_u32(y3, SIMON64_f(x3)), rk); - std::swap(x1, y1); std::swap(x2, y2); std::swap(x3, y3); - } - - // [A1 A3 B1 B3][A2 A4 B2 B4] => [A1 A2 A3 A4][B1 B2 B3 B4] - block0 = UnpackLow32(y1, x1); - block1 = UnpackHigh32(y1, x1); - block2 = UnpackLow32(y2, x2); - block3 = UnpackHigh32(y2, x2); - block4 = UnpackLow32(y3, x3); - block5 = UnpackHigh32(y3, x3); -} - -inline void SIMON64_Dec_6_Blocks(uint32x4_t &block0, uint32x4_t &block1, - uint32x4_t &block2, uint32x4_t &block3, uint32x4_t &block4, uint32x4_t &block5, - const word32 *subkeys, unsigned int rounds) -{ - // [A1 A2 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... - uint32x4_t x1 = vuzpq_u32(block0, block1).val[1]; - uint32x4_t y1 = vuzpq_u32(block0, block1).val[0]; - uint32x4_t x2 = vuzpq_u32(block2, block3).val[1]; - uint32x4_t y2 = vuzpq_u32(block2, block3).val[0]; - uint32x4_t x3 = vuzpq_u32(block4, block5).val[1]; - uint32x4_t y3 = vuzpq_u32(block4, block5).val[0]; - - if (rounds & 1) - { - std::swap(x1, y1); std::swap(x2, y2); std::swap(x3, y3); - const uint32x4_t rk = vld1q_dup_u32(subkeys + rounds - 1); - - y1 = veorq_u32(veorq_u32(y1, rk), SIMON64_f(x1)); - y2 = veorq_u32(veorq_u32(y2, rk), SIMON64_f(x2)); - y3 = veorq_u32(veorq_u32(y3, rk), SIMON64_f(x3)); - rounds--; - } - - for (int i = static_cast(rounds-2); i >= 0; i -= 2) - { - const uint32x4_t rk1 = vld1q_dup_u32(subkeys + i + 1); - x1 = veorq_u32(veorq_u32(x1, SIMON64_f(y1)), rk1); - x2 = veorq_u32(veorq_u32(x2, SIMON64_f(y2)), rk1); - x3 = veorq_u32(veorq_u32(x3, SIMON64_f(y3)), rk1); - - const uint32x4_t rk2 = vld1q_dup_u32(subkeys + i); - y1 = veorq_u32(veorq_u32(y1, SIMON64_f(x1)), rk2); - y2 = veorq_u32(veorq_u32(y2, SIMON64_f(x2)), rk2); - y3 = veorq_u32(veorq_u32(y3, SIMON64_f(x3)), rk2); - } - - // [A1 A3 B1 B3][A2 A4 B2 B4] => [A1 A2 A3 A4][B1 B2 B3 B4] - block0 = UnpackLow32(y1, x1); - block1 = UnpackHigh32(y1, x1); - block2 = UnpackLow32(y2, x2); - block3 = UnpackHigh32(y2, x2); - block4 = UnpackLow32(y3, x3); - block5 = UnpackHigh32(y3, x3); -} - -#endif // CRYPTOPP_ARM_NEON_AVAILABLE - -#if (CRYPTOPP_ARM_NEON_AVAILABLE) - -template -inline T UnpackHigh64(const T& a, const T& b) -{ - const uint64x1_t x(vget_high_u64((uint64x2_t)a)); - const uint64x1_t y(vget_high_u64((uint64x2_t)b)); - return (T)vcombine_u64(x, y); -} - -template -inline T UnpackLow64(const T& a, const T& b) -{ - const uint64x1_t x(vget_low_u64((uint64x2_t)a)); - const uint64x1_t y(vget_low_u64((uint64x2_t)b)); - return (T)vcombine_u64(x, y); -} - -template -inline uint64x2_t RotateLeft64(const uint64x2_t& val) -{ - const uint64x2_t a(vshlq_n_u64(val, R)); - const uint64x2_t b(vshrq_n_u64(val, 64 - R)); - return vorrq_u64(a, b); -} - -template -inline uint64x2_t RotateRight64(const uint64x2_t& val) -{ - const uint64x2_t a(vshlq_n_u64(val, 64 - R)); - const uint64x2_t b(vshrq_n_u64(val, R)); - return vorrq_u64(a, b); -} - -#if defined(__aarch32__) || defined(__aarch64__) -// Faster than two Shifts and an Or. Thanks to Louis Wingers and Bryan Weeks. -template <> -inline uint64x2_t RotateLeft64<8>(const uint64x2_t& val) -{ -#if defined(CRYPTOPP_BIG_ENDIAN) - const uint8_t maskb[16] = { 14,13,12,11, 10,9,8,15, 6,5,4,3, 2,1,0,7 }; - const uint8x16_t mask = vld1q_u8(maskb); -#else - const uint8_t maskb[16] = { 7,0,1,2, 3,4,5,6, 15,8,9,10, 11,12,13,14 }; - const uint8x16_t mask = vld1q_u8(maskb); -#endif - - return vreinterpretq_u64_u8( - vqtbl1q_u8(vreinterpretq_u8_u64(val), mask)); -} - -// Faster than two Shifts and an Or. Thanks to Louis Wingers and Bryan Weeks. -template <> -inline uint64x2_t RotateRight64<8>(const uint64x2_t& val) -{ -#if defined(CRYPTOPP_BIG_ENDIAN) - const uint8_t maskb[16] = { 8,15,14,13, 12,11,10,9, 0,7,6,5, 4,3,2,1 }; - const uint8x16_t mask = vld1q_u8(maskb); -#else - const uint8_t maskb[16] = { 1,2,3,4, 5,6,7,0, 9,10,11,12, 13,14,15,8 }; - const uint8x16_t mask = vld1q_u8(maskb); -#endif - - return vreinterpretq_u64_u8( - vqtbl1q_u8(vreinterpretq_u8_u64(val), mask)); -} -#endif - -inline uint64x2_t SIMON128_f(const uint64x2_t& val) -{ - return veorq_u64(RotateLeft64<2>(val), - vandq_u64(RotateLeft64<1>(val), RotateLeft64<8>(val))); -} - -inline void SIMON128_Enc_Block(uint64x2_t &block0, uint64x2_t &block1, - const word64 *subkeys, unsigned int rounds) -{ - // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ... - uint64x2_t x1 = UnpackHigh64(block0, block1); - uint64x2_t y1 = UnpackLow64(block0, block1); - - for (int i = 0; i < static_cast(rounds & ~1)-1; i += 2) - { - const uint64x2_t rk1 = vld1q_dup_u64(subkeys+i); - y1 = veorq_u64(veorq_u64(y1, SIMON128_f(x1)), rk1); - - const uint64x2_t rk2 = vld1q_dup_u64(subkeys+i+1); - x1 = veorq_u64(veorq_u64(x1, SIMON128_f(y1)), rk2); - } - - if (rounds & 1) - { - const uint64x2_t rk = vld1q_dup_u64(subkeys+rounds-1); - - y1 = veorq_u64(veorq_u64(y1, SIMON128_f(x1)), rk); - std::swap(x1, y1); - } - - // [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ... - block0 = UnpackLow64(y1, x1); - block1 = UnpackHigh64(y1, x1); -} - -inline void SIMON128_Enc_6_Blocks(uint64x2_t &block0, uint64x2_t &block1, - uint64x2_t &block2, uint64x2_t &block3, uint64x2_t &block4, uint64x2_t &block5, - const word64 *subkeys, unsigned int rounds) -{ - // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ... - uint64x2_t x1 = UnpackHigh64(block0, block1); - uint64x2_t y1 = UnpackLow64(block0, block1); - uint64x2_t x2 = UnpackHigh64(block2, block3); - uint64x2_t y2 = UnpackLow64(block2, block3); - uint64x2_t x3 = UnpackHigh64(block4, block5); - uint64x2_t y3 = UnpackLow64(block4, block5); - - for (int i = 0; i < static_cast(rounds & ~1) - 1; i += 2) - { - const uint64x2_t rk1 = vld1q_dup_u64(subkeys+i); - y1 = veorq_u64(veorq_u64(y1, SIMON128_f(x1)), rk1); - y2 = veorq_u64(veorq_u64(y2, SIMON128_f(x2)), rk1); - y3 = veorq_u64(veorq_u64(y3, SIMON128_f(x3)), rk1); - - const uint64x2_t rk2 = vld1q_dup_u64(subkeys+i+1); - x1 = veorq_u64(veorq_u64(x1, SIMON128_f(y1)), rk2); - x2 = veorq_u64(veorq_u64(x2, SIMON128_f(y2)), rk2); - x3 = veorq_u64(veorq_u64(x3, SIMON128_f(y3)), rk2); - } - - if (rounds & 1) - { - const uint64x2_t rk = vld1q_dup_u64(subkeys + rounds - 1); - - y1 = veorq_u64(veorq_u64(y1, SIMON128_f(x1)), rk); - y2 = veorq_u64(veorq_u64(y2, SIMON128_f(x2)), rk); - y3 = veorq_u64(veorq_u64(y3, SIMON128_f(x3)), rk); - std::swap(x1, y1); std::swap(x2, y2); std::swap(x3, y3); - } - - // [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ... - block0 = UnpackLow64(y1, x1); - block1 = UnpackHigh64(y1, x1); - block2 = UnpackLow64(y2, x2); - block3 = UnpackHigh64(y2, x2); - block4 = UnpackLow64(y3, x3); - block5 = UnpackHigh64(y3, x3); -} - -inline void SIMON128_Dec_Block(uint64x2_t &block0, uint64x2_t &block1, - const word64 *subkeys, unsigned int rounds) -{ - // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ... - uint64x2_t x1 = UnpackHigh64(block0, block1); - uint64x2_t y1 = UnpackLow64(block0, block1); - - if (rounds & 1) - { - std::swap(x1, y1); - const uint64x2_t rk = vld1q_dup_u64(subkeys + rounds - 1); - - y1 = veorq_u64(veorq_u64(y1, rk), SIMON128_f(x1)); - rounds--; - } - - for (int i = static_cast(rounds-2); i >= 0; i -= 2) - { - const uint64x2_t rk1 = vld1q_dup_u64(subkeys+i+1); - x1 = veorq_u64(veorq_u64(x1, SIMON128_f(y1)), rk1); - - const uint64x2_t rk2 = vld1q_dup_u64(subkeys+i); - y1 = veorq_u64(veorq_u64(y1, SIMON128_f(x1)), rk2); - } - - // [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ... - block0 = UnpackLow64(y1, x1); - block1 = UnpackHigh64(y1, x1); -} - -inline void SIMON128_Dec_6_Blocks(uint64x2_t &block0, uint64x2_t &block1, - uint64x2_t &block2, uint64x2_t &block3, uint64x2_t &block4, uint64x2_t &block5, - const word64 *subkeys, unsigned int rounds) -{ - // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ... - uint64x2_t x1 = UnpackHigh64(block0, block1); - uint64x2_t y1 = UnpackLow64(block0, block1); - uint64x2_t x2 = UnpackHigh64(block2, block3); - uint64x2_t y2 = UnpackLow64(block2, block3); - uint64x2_t x3 = UnpackHigh64(block4, block5); - uint64x2_t y3 = UnpackLow64(block4, block5); - - if (rounds & 1) - { - std::swap(x1, y1); std::swap(x2, y2); std::swap(x3, y3); - const uint64x2_t rk = vld1q_dup_u64(subkeys + rounds - 1); - - y1 = veorq_u64(veorq_u64(y1, rk), SIMON128_f(x1)); - y2 = veorq_u64(veorq_u64(y2, rk), SIMON128_f(x2)); - y3 = veorq_u64(veorq_u64(y3, rk), SIMON128_f(x3)); - rounds--; - } - - for (int i = static_cast(rounds-2); i >= 0; i -= 2) - { - const uint64x2_t rk1 = vld1q_dup_u64(subkeys + i + 1); - x1 = veorq_u64(veorq_u64(x1, SIMON128_f(y1)), rk1); - x2 = veorq_u64(veorq_u64(x2, SIMON128_f(y2)), rk1); - x3 = veorq_u64(veorq_u64(x3, SIMON128_f(y3)), rk1); - - const uint64x2_t rk2 = vld1q_dup_u64(subkeys + i); - y1 = veorq_u64(veorq_u64(y1, SIMON128_f(x1)), rk2); - y2 = veorq_u64(veorq_u64(y2, SIMON128_f(x2)), rk2); - y3 = veorq_u64(veorq_u64(y3, SIMON128_f(x3)), rk2); - } - - // [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ... - block0 = UnpackLow64(y1, x1); - block1 = UnpackHigh64(y1, x1); - block2 = UnpackLow64(y2, x2); - block3 = UnpackHigh64(y2, x2); - block4 = UnpackLow64(y3, x3); - block5 = UnpackHigh64(y3, x3); -} - -#endif // CRYPTOPP_ARM_NEON_AVAILABLE - -// ***************************** IA-32 ***************************** // - -#if defined(CRYPTOPP_SSSE3_AVAILABLE) - -// Clang __m128i casts, http://bugs.llvm.org/show_bug.cgi?id=20670 -#ifndef M128_CAST -# define M128_CAST(x) ((__m128i *)(void *)(x)) -#endif -#ifndef CONST_M128_CAST -# define CONST_M128_CAST(x) ((const __m128i *)(const void *)(x)) -#endif - -// GCC double casts, https://www.spinics.net/lists/gcchelp/msg47735.html -#ifndef DOUBLE_CAST -# define DOUBLE_CAST(x) ((double *)(void *)(x)) -#endif -#ifndef CONST_DOUBLE_CAST -# define CONST_DOUBLE_CAST(x) ((const double *)(const void *)(x)) -#endif - -inline void Swap128(__m128i& a,__m128i& b) -{ -#if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x5120) - // __m128i is an unsigned long long[2], and support for swapping it was not added until C++11. - // SunCC 12.1 - 12.3 fail to consume the swap; while SunCC 12.4 consumes it without -std=c++11. - vec_swap(a, b); -#else - std::swap(a, b); -#endif -} - -template -inline __m128i RotateLeft64(const __m128i& val) -{ -#if defined(CRYPTOPP_AVX512_ROTATE) - return _mm_rol_epi64(val, R); -#else - return _mm_or_si128( - _mm_slli_epi64(val, R), _mm_srli_epi64(val, 64-R)); -#endif -} - -template -inline __m128i RotateRight64(const __m128i& val) -{ -#if defined(CRYPTOPP_AVX512_ROTATE) - return _mm_ror_epi64(val, R); -#else - return _mm_or_si128( - _mm_slli_epi64(val, 64-R), _mm_srli_epi64(val, R)); -#endif -} - -// Faster than two Shifts and an Or. Thanks to Louis Wingers and Bryan Weeks. -template <> -inline __m128i RotateLeft64<8>(const __m128i& val) -{ - const __m128i mask = _mm_set_epi8(14,13,12,11, 10,9,8,15, 6,5,4,3, 2,1,0,7); - return _mm_shuffle_epi8(val, mask); -} - -// Faster than two Shifts and an Or. Thanks to Louis Wingers and Bryan Weeks. -template <> -inline __m128i RotateRight64<8>(const __m128i& val) -{ - const __m128i mask = _mm_set_epi8(8,15,14,13, 12,11,10,9, 0,7,6,5, 4,3,2,1); - return _mm_shuffle_epi8(val, mask); -} - -inline __m128i SIMON128_f(const __m128i& v) -{ - return _mm_xor_si128(RotateLeft64<2>(v), - _mm_and_si128(RotateLeft64<1>(v), RotateLeft64<8>(v))); -} - -inline void SIMON128_Enc_Block(__m128i &block0, __m128i &block1, - const word64 *subkeys, unsigned int rounds) -{ - // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ... - __m128i x1 = _mm_unpackhi_epi64(block0, block1); - __m128i y1 = _mm_unpacklo_epi64(block0, block1); - - for (int i = 0; i < static_cast(rounds & ~1)-1; i += 2) - { - const __m128i rk1 = _mm_castpd_si128( - _mm_loaddup_pd(CONST_DOUBLE_CAST(subkeys+i))); - y1 = _mm_xor_si128(_mm_xor_si128(y1, SIMON128_f(x1)), rk1); - - const __m128i rk2 = _mm_castpd_si128( - _mm_loaddup_pd(CONST_DOUBLE_CAST(subkeys+i+1))); - x1 = _mm_xor_si128(_mm_xor_si128(x1, SIMON128_f(y1)), rk2); - } - - if (rounds & 1) - { - const __m128i rk = _mm_castpd_si128( - _mm_loaddup_pd(CONST_DOUBLE_CAST(subkeys+rounds-1))); - - y1 = _mm_xor_si128(_mm_xor_si128(y1, SIMON128_f(x1)), rk); - Swap128(x1, y1); - } - - // [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ... - block0 = _mm_unpacklo_epi64(y1, x1); - block1 = _mm_unpackhi_epi64(y1, x1); -} - -inline void SIMON128_Enc_6_Blocks(__m128i &block0, __m128i &block1, - __m128i &block2, __m128i &block3, __m128i &block4, __m128i &block5, - const word64 *subkeys, unsigned int rounds) -{ - // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ... - __m128i x1 = _mm_unpackhi_epi64(block0, block1); - __m128i y1 = _mm_unpacklo_epi64(block0, block1); - __m128i x2 = _mm_unpackhi_epi64(block2, block3); - __m128i y2 = _mm_unpacklo_epi64(block2, block3); - __m128i x3 = _mm_unpackhi_epi64(block4, block5); - __m128i y3 = _mm_unpacklo_epi64(block4, block5); - - for (int i = 0; i < static_cast(rounds & ~1) - 1; i += 2) - { - const __m128i rk1 = _mm_castpd_si128( - _mm_loaddup_pd(CONST_DOUBLE_CAST(subkeys + i))); - y1 = _mm_xor_si128(_mm_xor_si128(y1, SIMON128_f(x1)), rk1); - y2 = _mm_xor_si128(_mm_xor_si128(y2, SIMON128_f(x2)), rk1); - y3 = _mm_xor_si128(_mm_xor_si128(y3, SIMON128_f(x3)), rk1); - - const __m128i rk2 = _mm_castpd_si128( - _mm_loaddup_pd(CONST_DOUBLE_CAST(subkeys + i + 1))); - x1 = _mm_xor_si128(_mm_xor_si128(x1, SIMON128_f(y1)), rk2); - x2 = _mm_xor_si128(_mm_xor_si128(x2, SIMON128_f(y2)), rk2); - x3 = _mm_xor_si128(_mm_xor_si128(x3, SIMON128_f(y3)), rk2); - } - - if (rounds & 1) - { - const __m128i rk = _mm_castpd_si128( - _mm_loaddup_pd(CONST_DOUBLE_CAST(subkeys + rounds - 1))); - y1 = _mm_xor_si128(_mm_xor_si128(y1, SIMON128_f(x1)), rk); - y2 = _mm_xor_si128(_mm_xor_si128(y2, SIMON128_f(x2)), rk); - y3 = _mm_xor_si128(_mm_xor_si128(y3, SIMON128_f(x3)), rk); - Swap128(x1, y1); Swap128(x2, y2); Swap128(x3, y3); - } - - // [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ... - block0 = _mm_unpacklo_epi64(y1, x1); - block1 = _mm_unpackhi_epi64(y1, x1); - block2 = _mm_unpacklo_epi64(y2, x2); - block3 = _mm_unpackhi_epi64(y2, x2); - block4 = _mm_unpacklo_epi64(y3, x3); - block5 = _mm_unpackhi_epi64(y3, x3); -} - -inline void SIMON128_Dec_Block(__m128i &block0, __m128i &block1, - const word64 *subkeys, unsigned int rounds) -{ - // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ... - __m128i x1 = _mm_unpackhi_epi64(block0, block1); - __m128i y1 = _mm_unpacklo_epi64(block0, block1); - - if (rounds & 1) - { - const __m128i rk = _mm_castpd_si128( - _mm_loaddup_pd(CONST_DOUBLE_CAST(subkeys + rounds - 1))); - - Swap128(x1, y1); - y1 = _mm_xor_si128(_mm_xor_si128(y1, rk), SIMON128_f(x1)); - rounds--; - } - - for (int i = static_cast(rounds-2); i >= 0; i -= 2) - { - const __m128i rk1 = _mm_castpd_si128( - _mm_loaddup_pd(CONST_DOUBLE_CAST(subkeys+i+1))); - x1 = _mm_xor_si128(_mm_xor_si128(x1, SIMON128_f(y1)), rk1); - - const __m128i rk2 = _mm_castpd_si128( - _mm_loaddup_pd(CONST_DOUBLE_CAST(subkeys+i))); - y1 = _mm_xor_si128(_mm_xor_si128(y1, SIMON128_f(x1)), rk2); - } - - // [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ... - block0 = _mm_unpacklo_epi64(y1, x1); - block1 = _mm_unpackhi_epi64(y1, x1); -} - -inline void SIMON128_Dec_6_Blocks(__m128i &block0, __m128i &block1, - __m128i &block2, __m128i &block3, __m128i &block4, __m128i &block5, - const word64 *subkeys, unsigned int rounds) -{ - // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ... - __m128i x1 = _mm_unpackhi_epi64(block0, block1); - __m128i y1 = _mm_unpacklo_epi64(block0, block1); - __m128i x2 = _mm_unpackhi_epi64(block2, block3); - __m128i y2 = _mm_unpacklo_epi64(block2, block3); - __m128i x3 = _mm_unpackhi_epi64(block4, block5); - __m128i y3 = _mm_unpacklo_epi64(block4, block5); - - if (rounds & 1) - { - const __m128i rk = _mm_castpd_si128( - _mm_loaddup_pd(CONST_DOUBLE_CAST(subkeys + rounds - 1))); - - Swap128(x1, y1); Swap128(x2, y2); Swap128(x3, y3); - y1 = _mm_xor_si128(_mm_xor_si128(y1, rk), SIMON128_f(x1)); - y2 = _mm_xor_si128(_mm_xor_si128(y2, rk), SIMON128_f(x2)); - y3 = _mm_xor_si128(_mm_xor_si128(y3, rk), SIMON128_f(x3)); - rounds--; - } - - for (int i = static_cast(rounds-2); i >= 0; i -= 2) - { - const __m128i rk1 = _mm_castpd_si128( - _mm_loaddup_pd(CONST_DOUBLE_CAST(subkeys + i + 1))); - x1 = _mm_xor_si128(_mm_xor_si128(x1, SIMON128_f(y1)), rk1); - x2 = _mm_xor_si128(_mm_xor_si128(x2, SIMON128_f(y2)), rk1); - x3 = _mm_xor_si128(_mm_xor_si128(x3, SIMON128_f(y3)), rk1); - - const __m128i rk2 = _mm_castpd_si128( - _mm_loaddup_pd(CONST_DOUBLE_CAST(subkeys + i))); - y1 = _mm_xor_si128(_mm_xor_si128(y1, SIMON128_f(x1)), rk2); - y2 = _mm_xor_si128(_mm_xor_si128(y2, SIMON128_f(x2)), rk2); - y3 = _mm_xor_si128(_mm_xor_si128(y3, SIMON128_f(x3)), rk2); - } - - // [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ... - block0 = _mm_unpacklo_epi64(y1, x1); - block1 = _mm_unpackhi_epi64(y1, x1); - block2 = _mm_unpacklo_epi64(y2, x2); - block3 = _mm_unpackhi_epi64(y2, x2); - block4 = _mm_unpacklo_epi64(y3, x3); - block5 = _mm_unpackhi_epi64(y3, x3); -} - -#endif // CRYPTOPP_SSSE3_AVAILABLE - -#if defined(CRYPTOPP_SSE41_AVAILABLE) - -template -inline __m128i RotateLeft32(const __m128i& val) -{ - return _mm_or_si128( - _mm_slli_epi32(val, R), _mm_srli_epi32(val, 32-R)); -} - -template -inline __m128i RotateRight32(const __m128i& val) -{ - return _mm_or_si128( - _mm_slli_epi32(val, 32-R), _mm_srli_epi32(val, R)); -} - -// Faster than two Shifts and an Or. Thanks to Louis Wingers and Bryan Weeks. -template <> -inline __m128i RotateLeft32<8>(const __m128i& val) -{ - const __m128i mask = _mm_set_epi8(14,13,12,15, 10,9,8,11, 6,5,4,7, 2,1,0,3); - return _mm_shuffle_epi8(val, mask); -} - -// Faster than two Shifts and an Or. Thanks to Louis Wingers and Bryan Weeks. -template <> -inline __m128i RotateRight32<8>(const __m128i& val) -{ - const __m128i mask = _mm_set_epi8(12,15,14,13, 8,11,10,9, 4,7,6,5, 0,3,2,1); - return _mm_shuffle_epi8(val, mask); -} - -inline __m128i SIMON64_f(const __m128i& v) -{ - return _mm_xor_si128(RotateLeft32<2>(v), - _mm_and_si128(RotateLeft32<1>(v), RotateLeft32<8>(v))); -} - -inline void SIMON64_Enc_Block(__m128i &block0, __m128i &block1, - const word32 *subkeys, unsigned int rounds) -{ - // [A1 A2 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... - const __m128 t0 = _mm_castsi128_ps(block0); - const __m128 t1 = _mm_castsi128_ps(block1); - __m128i x1 = _mm_castps_si128(_mm_shuffle_ps(t0, t1, _MM_SHUFFLE(3,1,3,1))); - __m128i y1 = _mm_castps_si128(_mm_shuffle_ps(t0, t1, _MM_SHUFFLE(2,0,2,0))); - - for (int i = 0; i < static_cast(rounds & ~1)-1; i += 2) - { - const __m128i rk1 = _mm_set1_epi32(subkeys[i]); - y1 = _mm_xor_si128(_mm_xor_si128(y1, SIMON64_f(x1)), rk1); - - const __m128i rk2 = _mm_set1_epi32(subkeys[i+1]); - x1 = _mm_xor_si128(_mm_xor_si128(x1, SIMON64_f(y1)), rk2); - } - - if (rounds & 1) - { - const __m128i rk = _mm_set1_epi32(subkeys[rounds-1]); - y1 = _mm_xor_si128(_mm_xor_si128(y1, SIMON64_f(x1)), rk); - Swap128(x1, y1); - } - - // [A1 A3 B1 B3][A2 A4 B2 B4] => [A1 A2 A3 A4][B1 B2 B3 B4] - block0 = _mm_unpacklo_epi32(y1, x1); - block1 = _mm_unpackhi_epi32(y1, x1); -} - -inline void SIMON64_Dec_Block(__m128i &block0, __m128i &block1, - const word32 *subkeys, unsigned int rounds) -{ - // [A1 A2 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... - const __m128 t0 = _mm_castsi128_ps(block0); - const __m128 t1 = _mm_castsi128_ps(block1); - __m128i x1 = _mm_castps_si128(_mm_shuffle_ps(t0, t1, _MM_SHUFFLE(3,1,3,1))); - __m128i y1 = _mm_castps_si128(_mm_shuffle_ps(t0, t1, _MM_SHUFFLE(2,0,2,0))); - - if (rounds & 1) - { - Swap128(x1, y1); - const __m128i rk = _mm_set1_epi32(subkeys[rounds-1]); - y1 = _mm_xor_si128(_mm_xor_si128(y1, rk), SIMON64_f(x1)); - rounds--; - } - - for (int i = static_cast(rounds-2); i >= 0; i -= 2) - { - const __m128i rk1 = _mm_set1_epi32(subkeys[i+1]); - x1 = _mm_xor_si128(_mm_xor_si128(x1, SIMON64_f(y1)), rk1); - - const __m128i rk2 = _mm_set1_epi32(subkeys[i]); - y1 = _mm_xor_si128(_mm_xor_si128(y1, SIMON64_f(x1)), rk2); - } - - // [A1 A3 B1 B3][A2 A4 B2 B4] => [A1 A2 A3 A4][B1 B2 B3 B4] - block0 = _mm_unpacklo_epi32(y1, x1); - block1 = _mm_unpackhi_epi32(y1, x1); -} - -inline void SIMON64_Enc_6_Blocks(__m128i &block0, __m128i &block1, - __m128i &block2, __m128i &block3, __m128i &block4, __m128i &block5, - const word32 *subkeys, unsigned int rounds) -{ - // [A1 A2 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... - const __m128 t0 = _mm_castsi128_ps(block0); - const __m128 t1 = _mm_castsi128_ps(block1); - __m128i x1 = _mm_castps_si128(_mm_shuffle_ps(t0, t1, _MM_SHUFFLE(3,1,3,1))); - __m128i y1 = _mm_castps_si128(_mm_shuffle_ps(t0, t1, _MM_SHUFFLE(2,0,2,0))); - - const __m128 t2 = _mm_castsi128_ps(block2); - const __m128 t3 = _mm_castsi128_ps(block3); - __m128i x2 = _mm_castps_si128(_mm_shuffle_ps(t2, t3, _MM_SHUFFLE(3,1,3,1))); - __m128i y2 = _mm_castps_si128(_mm_shuffle_ps(t2, t3, _MM_SHUFFLE(2,0,2,0))); - - const __m128 t4 = _mm_castsi128_ps(block4); - const __m128 t5 = _mm_castsi128_ps(block5); - __m128i x3 = _mm_castps_si128(_mm_shuffle_ps(t4, t5, _MM_SHUFFLE(3,1,3,1))); - __m128i y3 = _mm_castps_si128(_mm_shuffle_ps(t4, t5, _MM_SHUFFLE(2,0,2,0))); - - for (int i = 0; i < static_cast(rounds & ~1)-1; i += 2) - { - const __m128i rk1 = _mm_set1_epi32(subkeys[i]); - y1 = _mm_xor_si128(_mm_xor_si128(y1, SIMON64_f(x1)), rk1); - y2 = _mm_xor_si128(_mm_xor_si128(y2, SIMON64_f(x2)), rk1); - y3 = _mm_xor_si128(_mm_xor_si128(y3, SIMON64_f(x3)), rk1); - - const __m128i rk2 = _mm_set1_epi32(subkeys[i+1]); - x1 = _mm_xor_si128(_mm_xor_si128(x1, SIMON64_f(y1)), rk2); - x2 = _mm_xor_si128(_mm_xor_si128(x2, SIMON64_f(y2)), rk2); - x3 = _mm_xor_si128(_mm_xor_si128(x3, SIMON64_f(y3)), rk2); - } - - if (rounds & 1) - { - const __m128i rk = _mm_set1_epi32(subkeys[rounds-1]); - y1 = _mm_xor_si128(_mm_xor_si128(y1, SIMON64_f(x1)), rk); - y2 = _mm_xor_si128(_mm_xor_si128(y2, SIMON64_f(x2)), rk); - y3 = _mm_xor_si128(_mm_xor_si128(y3, SIMON64_f(x3)), rk); - Swap128(x1, y1); Swap128(x2, y2); Swap128(x3, y3); - } - - // [A1 A3 B1 B3][A2 A4 B2 B4] => [A1 A2 A3 A4][B1 B2 B3 B4] - block0 = _mm_unpacklo_epi32(y1, x1); - block1 = _mm_unpackhi_epi32(y1, x1); - block2 = _mm_unpacklo_epi32(y2, x2); - block3 = _mm_unpackhi_epi32(y2, x2); - block4 = _mm_unpacklo_epi32(y3, x3); - block5 = _mm_unpackhi_epi32(y3, x3); -} - -inline void SIMON64_Dec_6_Blocks(__m128i &block0, __m128i &block1, - __m128i &block2, __m128i &block3, __m128i &block4, __m128i &block5, - const word32 *subkeys, unsigned int rounds) -{ - // [A1 A2 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... - const __m128 t0 = _mm_castsi128_ps(block0); - const __m128 t1 = _mm_castsi128_ps(block1); - __m128i x1 = _mm_castps_si128(_mm_shuffle_ps(t0, t1, _MM_SHUFFLE(3,1,3,1))); - __m128i y1 = _mm_castps_si128(_mm_shuffle_ps(t0, t1, _MM_SHUFFLE(2,0,2,0))); - - const __m128 t2 = _mm_castsi128_ps(block2); - const __m128 t3 = _mm_castsi128_ps(block3); - __m128i x2 = _mm_castps_si128(_mm_shuffle_ps(t2, t3, _MM_SHUFFLE(3,1,3,1))); - __m128i y2 = _mm_castps_si128(_mm_shuffle_ps(t2, t3, _MM_SHUFFLE(2,0,2,0))); - - const __m128 t4 = _mm_castsi128_ps(block4); - const __m128 t5 = _mm_castsi128_ps(block5); - __m128i x3 = _mm_castps_si128(_mm_shuffle_ps(t4, t5, _MM_SHUFFLE(3,1,3,1))); - __m128i y3 = _mm_castps_si128(_mm_shuffle_ps(t4, t5, _MM_SHUFFLE(2,0,2,0))); - - if (rounds & 1) - { - Swap128(x1, y1); Swap128(x2, y2); Swap128(x3, y3); - const __m128i rk = _mm_set1_epi32(subkeys[rounds-1]); - y1 = _mm_xor_si128(_mm_xor_si128(y1, rk), SIMON64_f(x1)); - y2 = _mm_xor_si128(_mm_xor_si128(y2, rk), SIMON64_f(x2)); - y3 = _mm_xor_si128(_mm_xor_si128(y3, rk), SIMON64_f(x3)); - rounds--; - } - - for (int i = static_cast(rounds-2); i >= 0; i -= 2) - { - const __m128i rk1 = _mm_set1_epi32(subkeys[i+1]); - x1 = _mm_xor_si128(_mm_xor_si128(x1, SIMON64_f(y1)), rk1); - x2 = _mm_xor_si128(_mm_xor_si128(x2, SIMON64_f(y2)), rk1); - x3 = _mm_xor_si128(_mm_xor_si128(x3, SIMON64_f(y3)), rk1); - - const __m128i rk2 = _mm_set1_epi32(subkeys[i]); - y1 = _mm_xor_si128(_mm_xor_si128(y1, SIMON64_f(x1)), rk2); - y2 = _mm_xor_si128(_mm_xor_si128(y2, SIMON64_f(x2)), rk2); - y3 = _mm_xor_si128(_mm_xor_si128(y3, SIMON64_f(x3)), rk2); - } - - // [A1 A3 B1 B3][A2 A4 B2 B4] => [A1 A2 A3 A4][B1 B2 B3 B4] - block0 = _mm_unpacklo_epi32(y1, x1); - block1 = _mm_unpackhi_epi32(y1, x1); - block2 = _mm_unpacklo_epi32(y2, x2); - block3 = _mm_unpackhi_epi32(y2, x2); - block4 = _mm_unpacklo_epi32(y3, x3); - block5 = _mm_unpackhi_epi32(y3, x3); -} - -#endif // CRYPTOPP_SSE41_AVAILABLE - -// ***************************** Power7 ***************************** // - -#if defined(CRYPTOPP_POWER7_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 = {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); -} - -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 = {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); -} - -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 = {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 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 = {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 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_POWER7_AVAILABLE - -// ***************************** Power8 ***************************** // - -#if defined(CRYPTOPP_POWER8_AVAILABLE) - -using CryptoPP::uint8x16_p; -using CryptoPP::uint32x4_p; -using CryptoPP::uint64x2_p; - -using CryptoPP::VectorAnd; -using CryptoPP::VectorXor; - -// Rotate left by bit count -template -inline uint64x2_p RotateLeft64(const uint64x2_p val) -{ - const uint64x2_p m = {C, C}; - return vec_rl(val, m); -} - -// Rotate right by bit count -template -inline uint64x2_p RotateRight64(const uint64x2_p val) -{ - const uint64x2_p m = {64-C, 64-C}; - return vec_rl(val, m); -} - -inline uint64x2_p SIMON128_f(const uint64x2_p val) -{ - return VectorXor(RotateLeft64<2>(val), - VectorAnd(RotateLeft64<1>(val), RotateLeft64<8>(val))); -} - -inline void SIMON128_Enc_Block(uint32x4_p &block, const word64 *subkeys, unsigned int rounds) -{ -#if defined(CRYPTOPP_BIG_ENDIAN) - 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}; -#endif - - // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ... - uint64x2_p x1 = (uint64x2_p)vec_perm(block, block, m1); - uint64x2_p y1 = (uint64x2_p)vec_perm(block, block, m2); - - for (int i = 0; i < static_cast(rounds & ~1)-1; i += 2) - { - const uint64x2_p rk1 = vec_splats((unsigned long long)subkeys[i]); - y1 = VectorXor(VectorXor(y1, SIMON128_f(x1)), rk1); - - const uint64x2_p rk2 = vec_splats((unsigned long long)subkeys[i+1]); - x1 = VectorXor(VectorXor(x1, SIMON128_f(y1)), rk2); - } - - if (rounds & 1) - { - const uint64x2_p rk = vec_splats((unsigned long long)subkeys[rounds-1]); - y1 = VectorXor(VectorXor(y1, SIMON128_f(x1)), rk); - std::swap(x1, y1); - } - -#if defined(CRYPTOPP_BIG_ENDIAN) - 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); -} - -inline void SIMON128_Dec_Block(uint32x4_p &block, const word64 *subkeys, unsigned int rounds) -{ -#if defined(CRYPTOPP_BIG_ENDIAN) - 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}; -#endif - - // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ... - uint64x2_p x1 = (uint64x2_p)vec_perm(block, block, m1); - uint64x2_p y1 = (uint64x2_p)vec_perm(block, block, m2); - - if (rounds & 1) - { - std::swap(x1, y1); - const uint64x2_p rk = vec_splats((unsigned long long)subkeys[rounds-1]); - y1 = VectorXor(VectorXor(y1, rk), SIMON128_f(x1)); - rounds--; - } - - for (int i = static_cast(rounds-2); i >= 0; i -= 2) - { - const uint64x2_p rk1 = vec_splats((unsigned long long)subkeys[i+1]); - x1 = VectorXor(VectorXor(x1, SIMON128_f(y1)), rk1); - - const uint64x2_p rk2 = vec_splats((unsigned long long)subkeys[i]); - y1 = VectorXor(VectorXor(y1, SIMON128_f(x1)), rk2); - } - -#if defined(CRYPTOPP_BIG_ENDIAN) - 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); -} - -inline void SIMON128_Enc_6_Blocks(uint32x4_p &block0, uint32x4_p &block1, - uint32x4_p &block2, uint32x4_p &block3, uint32x4_p &block4, - uint32x4_p &block5, const word64 *subkeys, unsigned int rounds) -{ -#if defined(CRYPTOPP_BIG_ENDIAN) - 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}; -#endif - - // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ... - uint64x2_p x1 = (uint64x2_p)vec_perm(block0, block1, m1); - uint64x2_p y1 = (uint64x2_p)vec_perm(block0, block1, m2); - uint64x2_p x2 = (uint64x2_p)vec_perm(block2, block3, m1); - uint64x2_p y2 = (uint64x2_p)vec_perm(block2, block3, m2); - uint64x2_p x3 = (uint64x2_p)vec_perm(block4, block5, m1); - uint64x2_p y3 = (uint64x2_p)vec_perm(block4, block5, m2); - - for (int i = 0; i < static_cast(rounds & ~1)-1; i += 2) - { - const uint64x2_p rk1 = vec_splats((unsigned long long)subkeys[i]); - y1 = VectorXor(VectorXor(y1, SIMON128_f(x1)), rk1); - y2 = VectorXor(VectorXor(y2, SIMON128_f(x2)), rk1); - y3 = VectorXor(VectorXor(y3, SIMON128_f(x3)), rk1); - - const uint64x2_p rk2 = vec_splats((unsigned long long)subkeys[i+1]); - x1 = VectorXor(VectorXor(x1, SIMON128_f(y1)), rk2); - x2 = VectorXor(VectorXor(x2, SIMON128_f(y2)), rk2); - x3 = VectorXor(VectorXor(x3, SIMON128_f(y3)), rk2); - } - - if (rounds & 1) - { - const uint64x2_p rk = vec_splats((unsigned long long)subkeys[rounds-1]); - y1 = VectorXor(VectorXor(y1, SIMON128_f(x1)), rk); - y2 = VectorXor(VectorXor(y2, SIMON128_f(x2)), rk); - y3 = VectorXor(VectorXor(y3, SIMON128_f(x3)), rk); - std::swap(x1, y1); std::swap(x2, y2); std::swap(x3, y3); - } - -#if defined(CRYPTOPP_BIG_ENDIAN) - 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] ... - 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, - uint32x4_p &block2, uint32x4_p &block3, uint32x4_p &block4, - uint32x4_p &block5, const word64 *subkeys, unsigned int rounds) -{ -#if defined(CRYPTOPP_BIG_ENDIAN) - 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}; -#endif - - // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ... - uint64x2_p x1 = (uint64x2_p)vec_perm(block0, block1, m1); - uint64x2_p y1 = (uint64x2_p)vec_perm(block0, block1, m2); - uint64x2_p x2 = (uint64x2_p)vec_perm(block2, block3, m1); - uint64x2_p y2 = (uint64x2_p)vec_perm(block2, block3, m2); - uint64x2_p x3 = (uint64x2_p)vec_perm(block4, block5, m1); - uint64x2_p y3 = (uint64x2_p)vec_perm(block4, block5, m2); - - if (rounds & 1) - { - std::swap(x1, y1); std::swap(x2, y2); std::swap(x3, y3); - const uint64x2_p rk = vec_splats((unsigned long long)subkeys[rounds-1]); - y1 = VectorXor(VectorXor(y1, rk), SIMON128_f(x1)); - y2 = VectorXor(VectorXor(y2, rk), SIMON128_f(x2)); - y3 = VectorXor(VectorXor(y3, rk), SIMON128_f(x3)); - rounds--; - } - - for (int i = static_cast(rounds-2); i >= 0; i -= 2) - { - const uint64x2_p rk1 = vec_splats((unsigned long long)subkeys[i+1]); - x1 = VectorXor(VectorXor(x1, SIMON128_f(y1)), rk1); - x2 = VectorXor(VectorXor(x2, SIMON128_f(y2)), rk1); - x3 = VectorXor(VectorXor(x3, SIMON128_f(y3)), rk1); - - const uint64x2_p rk2 = vec_splats((unsigned long long)subkeys[i]); - y1 = VectorXor(VectorXor(y1, SIMON128_f(x1)), rk2); - y2 = VectorXor(VectorXor(y2, SIMON128_f(x2)), rk2); - y3 = VectorXor(VectorXor(y3, SIMON128_f(x3)), rk2); - } - -#if defined(CRYPTOPP_BIG_ENDIAN) - 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] ... - 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 - -ANONYMOUS_NAMESPACE_END - -/////////////////////////////////////////////////////////////////////// - -NAMESPACE_BEGIN(CryptoPP) - -// *************************** ARM NEON **************************** // - -#if (CRYPTOPP_ARM_NEON_AVAILABLE) -size_t SIMON64_Enc_AdvancedProcessBlocks_NEON(const word32* subKeys, size_t rounds, - const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) -{ - return AdvancedProcessBlocks64_6x2_NEON(SIMON64_Enc_Block, SIMON64_Enc_6_Blocks, - subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags); -} - -size_t SIMON64_Dec_AdvancedProcessBlocks_NEON(const word32* subKeys, size_t rounds, - const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) -{ - return AdvancedProcessBlocks64_6x2_NEON(SIMON64_Dec_Block, SIMON64_Dec_6_Blocks, - subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags); -} -#endif // CRYPTOPP_ARM_NEON_AVAILABLE - -#if (CRYPTOPP_ARM_NEON_AVAILABLE) -size_t SIMON128_Enc_AdvancedProcessBlocks_NEON(const word64* subKeys, size_t rounds, - const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) -{ - return AdvancedProcessBlocks128_6x2_NEON(SIMON128_Enc_Block, SIMON128_Enc_6_Blocks, - subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags); -} - -size_t SIMON128_Dec_AdvancedProcessBlocks_NEON(const word64* subKeys, size_t rounds, - const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) -{ - return AdvancedProcessBlocks128_6x2_NEON(SIMON128_Dec_Block, SIMON128_Dec_6_Blocks, - subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags); -} -#endif // CRYPTOPP_ARM_NEON_AVAILABLE - -// ***************************** IA-32 ***************************** // - -#if defined(CRYPTOPP_SSE41_AVAILABLE) -size_t SIMON64_Enc_AdvancedProcessBlocks_SSE41(const word32* subKeys, size_t rounds, - const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) -{ - return AdvancedProcessBlocks64_6x2_SSE(SIMON64_Enc_Block, SIMON64_Enc_6_Blocks, - subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags); -} - -size_t SIMON64_Dec_AdvancedProcessBlocks_SSE41(const word32* subKeys, size_t rounds, - const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) -{ - return AdvancedProcessBlocks64_6x2_SSE(SIMON64_Dec_Block, SIMON64_Dec_6_Blocks, - subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags); -} -#endif - -#if defined(CRYPTOPP_SSSE3_AVAILABLE) -size_t SIMON128_Enc_AdvancedProcessBlocks_SSSE3(const word64* subKeys, size_t rounds, - const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) -{ - return AdvancedProcessBlocks128_6x2_SSE(SIMON128_Enc_Block, SIMON128_Enc_6_Blocks, - subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags); -} - -size_t SIMON128_Dec_AdvancedProcessBlocks_SSSE3(const word64* subKeys, size_t rounds, - const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) -{ - return AdvancedProcessBlocks128_6x2_SSE(SIMON128_Dec_Block, SIMON128_Dec_6_Blocks, - subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags); -} -#endif // CRYPTOPP_SSSE3_AVAILABLE - -// ***************************** Power7 ***************************** // - -#if defined(CRYPTOPP_POWER7_AVAILABLE) -size_t SIMON64_Enc_AdvancedProcessBlocks_POWER7(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_POWER7(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); -} -#endif - -// ***************************** Power8 ***************************** // - -#if defined(CRYPTOPP_POWER8_AVAILABLE) -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) -{ - return AdvancedProcessBlocks128_6x1_ALTIVEC(SIMON128_Enc_Block, SIMON128_Enc_6_Blocks, - subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags); -} - -size_t SIMON128_Dec_AdvancedProcessBlocks_POWER8(const word64* subKeys, size_t rounds, - const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) -{ - return AdvancedProcessBlocks128_6x1_ALTIVEC(SIMON128_Dec_Block, SIMON128_Dec_6_Blocks, - subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags); -} -#endif // CRYPTOPP_POWER8_AVAILABLE - -NAMESPACE_END diff --git a/simon64-simd.cpp b/simon64-simd.cpp new file mode 100644 index 00000000..b8b6b85a --- /dev/null +++ b/simon64-simd.cpp @@ -0,0 +1,1033 @@ +// simon-simd.cpp - written and placed in the public domain by Jeffrey Walton +// +// This source file uses intrinsics and built-ins to gain access to +// SSSE3, ARM NEON and ARMv8a, and Power7 Altivec instructions. A separate +// source file is needed because additional CXXFLAGS are required to enable +// the appropriate instructions sets in some build configurations. + +#include "pch.h" +#include "config.h" + +#include "simon.h" +#include "misc.h" +#include "adv-simd.h" + +// Uncomment for benchmarking C++ against SSE or NEON. +// Do so in both simon.cpp and simon-simd.cpp. +// #undef CRYPTOPP_SSSE3_AVAILABLE +// #undef CRYPTOPP_SSE41_AVAILABLE +// #undef CRYPTOPP_ARM_NEON_AVAILABLE + +#if (CRYPTOPP_SSSE3_AVAILABLE) +# include +# include +#endif + +#if (CRYPTOPP_SSE41_AVAILABLE) +# include +#endif + +#if defined(__AVX512F__) && defined(__AVX512VL__) +# define CRYPTOPP_AVX512_ROTATE 1 +# include +#endif + +#if (CRYPTOPP_ARM_NEON_AVAILABLE) +# include +#endif + +// Can't use CRYPTOPP_ARM_XXX_AVAILABLE because too many +// compilers don't follow ACLE conventions for the include. +#if defined(CRYPTOPP_ARM_ACLE_AVAILABLE) +# include +# include +#endif + +#if defined(CRYPTOPP_POWER7_AVAILABLE) +# include "ppc-simd.h" +#endif + +// Squash MS LNK4221 and libtool warnings +extern const char SIMON64_SIMD_FNAME[] = __FILE__; + +ANONYMOUS_NAMESPACE_BEGIN + +using CryptoPP::byte; +using CryptoPP::word32; +using CryptoPP::word64; +using CryptoPP::rotlFixed; +using CryptoPP::rotrFixed; +using CryptoPP::vec_swap; // SunCC + +// *************************** ARM NEON ************************** // + +#if (CRYPTOPP_ARM_NEON_AVAILABLE) + +template +inline T UnpackHigh32(const T& a, const T& b) +{ + const uint32x2_t x(vget_high_u32((uint32x4_t)a)); + const uint32x2_t y(vget_high_u32((uint32x4_t)b)); + const uint32x2x2_t r = vzip_u32(x, y); + return (T)vcombine_u32(r.val[0], r.val[1]); +} + +template +inline T UnpackLow32(const T& a, const T& b) +{ + const uint32x2_t x(vget_low_u32((uint32x4_t)a)); + const uint32x2_t y(vget_low_u32((uint32x4_t)b)); + const uint32x2x2_t r = vzip_u32(x, y); + return (T)vcombine_u32(r.val[0], r.val[1]); +} + +template +inline uint32x4_t RotateLeft32(const uint32x4_t& val) +{ + const uint32x4_t a(vshlq_n_u32(val, R)); + const uint32x4_t b(vshrq_n_u32(val, 32 - R)); + return vorrq_u32(a, b); +} + +template +inline uint32x4_t RotateRight32(const uint32x4_t& val) +{ + const uint32x4_t a(vshlq_n_u32(val, 32 - R)); + const uint32x4_t b(vshrq_n_u32(val, R)); + return vorrq_u32(a, b); +} + +#if defined(__aarch32__) || defined(__aarch64__) +// Faster than two Shifts and an Or. Thanks to Louis Wingers and Bryan Weeks. +template <> +inline uint32x4_t RotateLeft32<8>(const uint32x4_t& val) +{ +#if defined(CRYPTOPP_BIG_ENDIAN) + const uint8_t maskb[16] = { 14,13,12,15, 10,9,8,11, 6,5,4,7, 2,1,0,3 }; + const uint8x16_t mask = vld1q_u8(maskb); +#else + const uint8_t maskb[16] = { 3,0,1,2, 7,4,5,6, 11,8,9,10, 15,12,13,14 }; + const uint8x16_t mask = vld1q_u8(maskb); +#endif + + return vreinterpretq_u32_u8( + vqtbl1q_u8(vreinterpretq_u8_u32(val), mask)); +} + +// Faster than two Shifts and an Or. Thanks to Louis Wingers and Bryan Weeks. +template <> +inline uint32x4_t RotateRight32<8>(const uint32x4_t& val) +{ +#if defined(CRYPTOPP_BIG_ENDIAN) + const uint8_t maskb[16] = { 12,15,14,13, 8,11,10,9, 4,7,6,5, 0,3,2,1 }; + const uint8x16_t mask = vld1q_u8(maskb); +#else + const uint8_t maskb[16] = { 1,2,3,0, 5,6,7,4, 9,10,11,8, 13,14,14,12 }; + const uint8x16_t mask = vld1q_u8(maskb); +#endif + + return vreinterpretq_u32_u8( + vqtbl1q_u8(vreinterpretq_u8_u32(val), mask)); +} +#endif + +inline uint32x4_t SIMON64_f(const uint32x4_t& val) +{ + return veorq_u32(RotateLeft32<2>(val), + vandq_u32(RotateLeft32<1>(val), RotateLeft32<8>(val))); +} + +inline void SIMON64_Enc_Block(uint32x4_t &block1, uint32x4_t &block0, + const word32 *subkeys, unsigned int rounds) +{ + // [A1 A2 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... + uint32x4_t x1 = vuzpq_u32(block0, block1).val[1]; + uint32x4_t y1 = vuzpq_u32(block0, block1).val[0]; + + for (int i = 0; i < static_cast(rounds & ~1)-1; i += 2) + { + const uint32x4_t rk1 = vld1q_dup_u32(subkeys+i); + y1 = veorq_u32(veorq_u32(y1, SIMON64_f(x1)), rk1); + + const uint32x4_t rk2 = vld1q_dup_u32(subkeys+i+1); + x1 = veorq_u32(veorq_u32(x1, SIMON64_f(y1)), rk2); + } + + if (rounds & 1) + { + const uint32x4_t rk = vld1q_dup_u32(subkeys+rounds-1); + + y1 = veorq_u32(veorq_u32(y1, SIMON64_f(x1)), rk); + std::swap(x1, y1); + } + + // [A1 A3 B1 B3][A2 A4 B2 B4] => [A1 A2 A3 A4][B1 B2 B3 B4] + block0 = UnpackLow32(y1, x1); + block1 = UnpackHigh32(y1, x1); +} + +inline void SIMON64_Dec_Block(uint32x4_t &block0, uint32x4_t &block1, + const word32 *subkeys, unsigned int rounds) +{ + // [A1 A2 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... + uint32x4_t x1 = vuzpq_u32(block0, block1).val[1]; + uint32x4_t y1 = vuzpq_u32(block0, block1).val[0]; + + if (rounds & 1) + { + std::swap(x1, y1); + const uint32x4_t rk = vld1q_dup_u32(subkeys + rounds - 1); + + y1 = veorq_u32(veorq_u32(y1, rk), SIMON64_f(x1)); + rounds--; + } + + for (int i = static_cast(rounds-2); i >= 0; i -= 2) + { + const uint32x4_t rk1 = vld1q_dup_u32(subkeys+i+1); + x1 = veorq_u32(veorq_u32(x1, SIMON64_f(y1)), rk1); + + const uint32x4_t rk2 = vld1q_dup_u32(subkeys+i); + y1 = veorq_u32(veorq_u32(y1, SIMON64_f(x1)), rk2); + } + + // [A1 A3 B1 B3][A2 A4 B2 B4] => [A1 A2 A3 A4][B1 B2 B3 B4] + block0 = UnpackLow32(y1, x1); + block1 = UnpackHigh32(y1, x1); +} + +inline void SIMON64_Enc_6_Blocks(uint32x4_t &block0, uint32x4_t &block1, + uint32x4_t &block2, uint32x4_t &block3, uint32x4_t &block4, uint32x4_t &block5, + const word32 *subkeys, unsigned int rounds) +{ + // [A1 A2 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... + uint32x4_t x1 = vuzpq_u32(block0, block1).val[1]; + uint32x4_t y1 = vuzpq_u32(block0, block1).val[0]; + uint32x4_t x2 = vuzpq_u32(block2, block3).val[1]; + uint32x4_t y2 = vuzpq_u32(block2, block3).val[0]; + uint32x4_t x3 = vuzpq_u32(block4, block5).val[1]; + uint32x4_t y3 = vuzpq_u32(block4, block5).val[0]; + + for (int i = 0; i < static_cast(rounds & ~1) - 1; i += 2) + { + const uint32x4_t rk1 = vld1q_dup_u32(subkeys+i); + y1 = veorq_u32(veorq_u32(y1, SIMON64_f(x1)), rk1); + y2 = veorq_u32(veorq_u32(y2, SIMON64_f(x2)), rk1); + y3 = veorq_u32(veorq_u32(y3, SIMON64_f(x3)), rk1); + + const uint32x4_t rk2 = vld1q_dup_u32(subkeys+i+1); + x1 = veorq_u32(veorq_u32(x1, SIMON64_f(y1)), rk2); + x2 = veorq_u32(veorq_u32(x2, SIMON64_f(y2)), rk2); + x3 = veorq_u32(veorq_u32(x3, SIMON64_f(y3)), rk2); + } + + if (rounds & 1) + { + const uint32x4_t rk = vld1q_dup_u32(subkeys + rounds - 1); + + y1 = veorq_u32(veorq_u32(y1, SIMON64_f(x1)), rk); + y2 = veorq_u32(veorq_u32(y2, SIMON64_f(x2)), rk); + y3 = veorq_u32(veorq_u32(y3, SIMON64_f(x3)), rk); + std::swap(x1, y1); std::swap(x2, y2); std::swap(x3, y3); + } + + // [A1 A3 B1 B3][A2 A4 B2 B4] => [A1 A2 A3 A4][B1 B2 B3 B4] + block0 = UnpackLow32(y1, x1); + block1 = UnpackHigh32(y1, x1); + block2 = UnpackLow32(y2, x2); + block3 = UnpackHigh32(y2, x2); + block4 = UnpackLow32(y3, x3); + block5 = UnpackHigh32(y3, x3); +} + +inline void SIMON64_Dec_6_Blocks(uint32x4_t &block0, uint32x4_t &block1, + uint32x4_t &block2, uint32x4_t &block3, uint32x4_t &block4, uint32x4_t &block5, + const word32 *subkeys, unsigned int rounds) +{ + // [A1 A2 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... + uint32x4_t x1 = vuzpq_u32(block0, block1).val[1]; + uint32x4_t y1 = vuzpq_u32(block0, block1).val[0]; + uint32x4_t x2 = vuzpq_u32(block2, block3).val[1]; + uint32x4_t y2 = vuzpq_u32(block2, block3).val[0]; + uint32x4_t x3 = vuzpq_u32(block4, block5).val[1]; + uint32x4_t y3 = vuzpq_u32(block4, block5).val[0]; + + if (rounds & 1) + { + std::swap(x1, y1); std::swap(x2, y2); std::swap(x3, y3); + const uint32x4_t rk = vld1q_dup_u32(subkeys + rounds - 1); + + y1 = veorq_u32(veorq_u32(y1, rk), SIMON64_f(x1)); + y2 = veorq_u32(veorq_u32(y2, rk), SIMON64_f(x2)); + y3 = veorq_u32(veorq_u32(y3, rk), SIMON64_f(x3)); + rounds--; + } + + for (int i = static_cast(rounds-2); i >= 0; i -= 2) + { + const uint32x4_t rk1 = vld1q_dup_u32(subkeys + i + 1); + x1 = veorq_u32(veorq_u32(x1, SIMON64_f(y1)), rk1); + x2 = veorq_u32(veorq_u32(x2, SIMON64_f(y2)), rk1); + x3 = veorq_u32(veorq_u32(x3, SIMON64_f(y3)), rk1); + + const uint32x4_t rk2 = vld1q_dup_u32(subkeys + i); + y1 = veorq_u32(veorq_u32(y1, SIMON64_f(x1)), rk2); + y2 = veorq_u32(veorq_u32(y2, SIMON64_f(x2)), rk2); + y3 = veorq_u32(veorq_u32(y3, SIMON64_f(x3)), rk2); + } + + // [A1 A3 B1 B3][A2 A4 B2 B4] => [A1 A2 A3 A4][B1 B2 B3 B4] + block0 = UnpackLow32(y1, x1); + block1 = UnpackHigh32(y1, x1); + block2 = UnpackLow32(y2, x2); + block3 = UnpackHigh32(y2, x2); + block4 = UnpackLow32(y3, x3); + block5 = UnpackHigh32(y3, x3); +} + +#endif // CRYPTOPP_ARM_NEON_AVAILABLE + +// ***************************** IA-32 ***************************** // + +#if (CRYPTOPP_SSSE3_AVAILABLE) + +// Clang __m128i casts, http://bugs.llvm.org/show_bug.cgi?id=20670 +#ifndef M128_CAST +# define M128_CAST(x) ((__m128i *)(void *)(x)) +#endif +#ifndef CONST_M128_CAST +# define CONST_M128_CAST(x) ((const __m128i *)(const void *)(x)) +#endif + +// GCC double casts, https://www.spinics.net/lists/gcchelp/msg47735.html +#ifndef DOUBLE_CAST +# define DOUBLE_CAST(x) ((double *)(void *)(x)) +#endif +#ifndef CONST_DOUBLE_CAST +# define CONST_DOUBLE_CAST(x) ((const double *)(const void *)(x)) +#endif + +inline void Swap128(__m128i& a,__m128i& b) +{ +#if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x5120) + // __m128i is an unsigned long long[2], and support for swapping it was not added until C++11. + // SunCC 12.1 - 12.3 fail to consume the swap; while SunCC 12.4 consumes it without -std=c++11. + vec_swap(a, b); +#else + std::swap(a, b); +#endif +} + +template +inline __m128i RotateLeft64(const __m128i& val) +{ +#if defined(CRYPTOPP_AVX512_ROTATE) + return _mm_rol_epi64(val, R); +#else + return _mm_or_si128( + _mm_slli_epi64(val, R), _mm_srli_epi64(val, 64-R)); +#endif +} + +template +inline __m128i RotateRight64(const __m128i& val) +{ +#if defined(CRYPTOPP_AVX512_ROTATE) + return _mm_ror_epi64(val, R); +#else + return _mm_or_si128( + _mm_slli_epi64(val, 64-R), _mm_srli_epi64(val, R)); +#endif +} + +// Faster than two Shifts and an Or. Thanks to Louis Wingers and Bryan Weeks. +template <> +inline __m128i RotateLeft64<8>(const __m128i& val) +{ + const __m128i mask = _mm_set_epi8(14,13,12,11, 10,9,8,15, 6,5,4,3, 2,1,0,7); + return _mm_shuffle_epi8(val, mask); +} + +// Faster than two Shifts and an Or. Thanks to Louis Wingers and Bryan Weeks. +template <> +inline __m128i RotateRight64<8>(const __m128i& val) +{ + const __m128i mask = _mm_set_epi8(8,15,14,13, 12,11,10,9, 0,7,6,5, 4,3,2,1); + return _mm_shuffle_epi8(val, mask); +} + +inline __m128i SIMON128_f(const __m128i& v) +{ + return _mm_xor_si128(RotateLeft64<2>(v), + _mm_and_si128(RotateLeft64<1>(v), RotateLeft64<8>(v))); +} + +inline void SIMON128_Enc_Block(__m128i &block0, __m128i &block1, + const word64 *subkeys, unsigned int rounds) +{ + // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ... + __m128i x1 = _mm_unpackhi_epi64(block0, block1); + __m128i y1 = _mm_unpacklo_epi64(block0, block1); + + for (int i = 0; i < static_cast(rounds & ~1)-1; i += 2) + { + const __m128i rk1 = _mm_castpd_si128( + _mm_loaddup_pd(CONST_DOUBLE_CAST(subkeys+i))); + y1 = _mm_xor_si128(_mm_xor_si128(y1, SIMON128_f(x1)), rk1); + + const __m128i rk2 = _mm_castpd_si128( + _mm_loaddup_pd(CONST_DOUBLE_CAST(subkeys+i+1))); + x1 = _mm_xor_si128(_mm_xor_si128(x1, SIMON128_f(y1)), rk2); + } + + if (rounds & 1) + { + const __m128i rk = _mm_castpd_si128( + _mm_loaddup_pd(CONST_DOUBLE_CAST(subkeys+rounds-1))); + + y1 = _mm_xor_si128(_mm_xor_si128(y1, SIMON128_f(x1)), rk); + Swap128(x1, y1); + } + + // [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ... + block0 = _mm_unpacklo_epi64(y1, x1); + block1 = _mm_unpackhi_epi64(y1, x1); +} + +inline void SIMON128_Enc_6_Blocks(__m128i &block0, __m128i &block1, + __m128i &block2, __m128i &block3, __m128i &block4, __m128i &block5, + const word64 *subkeys, unsigned int rounds) +{ + // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ... + __m128i x1 = _mm_unpackhi_epi64(block0, block1); + __m128i y1 = _mm_unpacklo_epi64(block0, block1); + __m128i x2 = _mm_unpackhi_epi64(block2, block3); + __m128i y2 = _mm_unpacklo_epi64(block2, block3); + __m128i x3 = _mm_unpackhi_epi64(block4, block5); + __m128i y3 = _mm_unpacklo_epi64(block4, block5); + + for (int i = 0; i < static_cast(rounds & ~1) - 1; i += 2) + { + const __m128i rk1 = _mm_castpd_si128( + _mm_loaddup_pd(CONST_DOUBLE_CAST(subkeys + i))); + y1 = _mm_xor_si128(_mm_xor_si128(y1, SIMON128_f(x1)), rk1); + y2 = _mm_xor_si128(_mm_xor_si128(y2, SIMON128_f(x2)), rk1); + y3 = _mm_xor_si128(_mm_xor_si128(y3, SIMON128_f(x3)), rk1); + + const __m128i rk2 = _mm_castpd_si128( + _mm_loaddup_pd(CONST_DOUBLE_CAST(subkeys + i + 1))); + x1 = _mm_xor_si128(_mm_xor_si128(x1, SIMON128_f(y1)), rk2); + x2 = _mm_xor_si128(_mm_xor_si128(x2, SIMON128_f(y2)), rk2); + x3 = _mm_xor_si128(_mm_xor_si128(x3, SIMON128_f(y3)), rk2); + } + + if (rounds & 1) + { + const __m128i rk = _mm_castpd_si128( + _mm_loaddup_pd(CONST_DOUBLE_CAST(subkeys + rounds - 1))); + y1 = _mm_xor_si128(_mm_xor_si128(y1, SIMON128_f(x1)), rk); + y2 = _mm_xor_si128(_mm_xor_si128(y2, SIMON128_f(x2)), rk); + y3 = _mm_xor_si128(_mm_xor_si128(y3, SIMON128_f(x3)), rk); + Swap128(x1, y1); Swap128(x2, y2); Swap128(x3, y3); + } + + // [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ... + block0 = _mm_unpacklo_epi64(y1, x1); + block1 = _mm_unpackhi_epi64(y1, x1); + block2 = _mm_unpacklo_epi64(y2, x2); + block3 = _mm_unpackhi_epi64(y2, x2); + block4 = _mm_unpacklo_epi64(y3, x3); + block5 = _mm_unpackhi_epi64(y3, x3); +} + +inline void SIMON128_Dec_Block(__m128i &block0, __m128i &block1, + const word64 *subkeys, unsigned int rounds) +{ + // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ... + __m128i x1 = _mm_unpackhi_epi64(block0, block1); + __m128i y1 = _mm_unpacklo_epi64(block0, block1); + + if (rounds & 1) + { + const __m128i rk = _mm_castpd_si128( + _mm_loaddup_pd(CONST_DOUBLE_CAST(subkeys + rounds - 1))); + + Swap128(x1, y1); + y1 = _mm_xor_si128(_mm_xor_si128(y1, rk), SIMON128_f(x1)); + rounds--; + } + + for (int i = static_cast(rounds-2); i >= 0; i -= 2) + { + const __m128i rk1 = _mm_castpd_si128( + _mm_loaddup_pd(CONST_DOUBLE_CAST(subkeys+i+1))); + x1 = _mm_xor_si128(_mm_xor_si128(x1, SIMON128_f(y1)), rk1); + + const __m128i rk2 = _mm_castpd_si128( + _mm_loaddup_pd(CONST_DOUBLE_CAST(subkeys+i))); + y1 = _mm_xor_si128(_mm_xor_si128(y1, SIMON128_f(x1)), rk2); + } + + // [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ... + block0 = _mm_unpacklo_epi64(y1, x1); + block1 = _mm_unpackhi_epi64(y1, x1); +} + +inline void SIMON128_Dec_6_Blocks(__m128i &block0, __m128i &block1, + __m128i &block2, __m128i &block3, __m128i &block4, __m128i &block5, + const word64 *subkeys, unsigned int rounds) +{ + // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ... + __m128i x1 = _mm_unpackhi_epi64(block0, block1); + __m128i y1 = _mm_unpacklo_epi64(block0, block1); + __m128i x2 = _mm_unpackhi_epi64(block2, block3); + __m128i y2 = _mm_unpacklo_epi64(block2, block3); + __m128i x3 = _mm_unpackhi_epi64(block4, block5); + __m128i y3 = _mm_unpacklo_epi64(block4, block5); + + if (rounds & 1) + { + const __m128i rk = _mm_castpd_si128( + _mm_loaddup_pd(CONST_DOUBLE_CAST(subkeys + rounds - 1))); + + Swap128(x1, y1); Swap128(x2, y2); Swap128(x3, y3); + y1 = _mm_xor_si128(_mm_xor_si128(y1, rk), SIMON128_f(x1)); + y2 = _mm_xor_si128(_mm_xor_si128(y2, rk), SIMON128_f(x2)); + y3 = _mm_xor_si128(_mm_xor_si128(y3, rk), SIMON128_f(x3)); + rounds--; + } + + for (int i = static_cast(rounds-2); i >= 0; i -= 2) + { + const __m128i rk1 = _mm_castpd_si128( + _mm_loaddup_pd(CONST_DOUBLE_CAST(subkeys + i + 1))); + x1 = _mm_xor_si128(_mm_xor_si128(x1, SIMON128_f(y1)), rk1); + x2 = _mm_xor_si128(_mm_xor_si128(x2, SIMON128_f(y2)), rk1); + x3 = _mm_xor_si128(_mm_xor_si128(x3, SIMON128_f(y3)), rk1); + + const __m128i rk2 = _mm_castpd_si128( + _mm_loaddup_pd(CONST_DOUBLE_CAST(subkeys + i))); + y1 = _mm_xor_si128(_mm_xor_si128(y1, SIMON128_f(x1)), rk2); + y2 = _mm_xor_si128(_mm_xor_si128(y2, SIMON128_f(x2)), rk2); + y3 = _mm_xor_si128(_mm_xor_si128(y3, SIMON128_f(x3)), rk2); + } + + // [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ... + block0 = _mm_unpacklo_epi64(y1, x1); + block1 = _mm_unpackhi_epi64(y1, x1); + block2 = _mm_unpacklo_epi64(y2, x2); + block3 = _mm_unpackhi_epi64(y2, x2); + block4 = _mm_unpacklo_epi64(y3, x3); + block5 = _mm_unpackhi_epi64(y3, x3); +} + +#endif // CRYPTOPP_SSSE3_AVAILABLE + +#if defined(CRYPTOPP_SSE41_AVAILABLE) + +template +inline __m128i RotateLeft32(const __m128i& val) +{ + return _mm_or_si128( + _mm_slli_epi32(val, R), _mm_srli_epi32(val, 32-R)); +} + +template +inline __m128i RotateRight32(const __m128i& val) +{ + return _mm_or_si128( + _mm_slli_epi32(val, 32-R), _mm_srli_epi32(val, R)); +} + +// Faster than two Shifts and an Or. Thanks to Louis Wingers and Bryan Weeks. +template <> +inline __m128i RotateLeft32<8>(const __m128i& val) +{ + const __m128i mask = _mm_set_epi8(14,13,12,15, 10,9,8,11, 6,5,4,7, 2,1,0,3); + return _mm_shuffle_epi8(val, mask); +} + +// Faster than two Shifts and an Or. Thanks to Louis Wingers and Bryan Weeks. +template <> +inline __m128i RotateRight32<8>(const __m128i& val) +{ + const __m128i mask = _mm_set_epi8(12,15,14,13, 8,11,10,9, 4,7,6,5, 0,3,2,1); + return _mm_shuffle_epi8(val, mask); +} + +inline __m128i SIMON64_f(const __m128i& v) +{ + return _mm_xor_si128(RotateLeft32<2>(v), + _mm_and_si128(RotateLeft32<1>(v), RotateLeft32<8>(v))); +} + +inline void SIMON64_Enc_Block(__m128i &block0, __m128i &block1, + const word32 *subkeys, unsigned int rounds) +{ + // [A1 A2 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... + const __m128 t0 = _mm_castsi128_ps(block0); + const __m128 t1 = _mm_castsi128_ps(block1); + __m128i x1 = _mm_castps_si128(_mm_shuffle_ps(t0, t1, _MM_SHUFFLE(3,1,3,1))); + __m128i y1 = _mm_castps_si128(_mm_shuffle_ps(t0, t1, _MM_SHUFFLE(2,0,2,0))); + + for (int i = 0; i < static_cast(rounds & ~1)-1; i += 2) + { + const __m128i rk1 = _mm_set1_epi32(subkeys[i]); + y1 = _mm_xor_si128(_mm_xor_si128(y1, SIMON64_f(x1)), rk1); + + const __m128i rk2 = _mm_set1_epi32(subkeys[i+1]); + x1 = _mm_xor_si128(_mm_xor_si128(x1, SIMON64_f(y1)), rk2); + } + + if (rounds & 1) + { + const __m128i rk = _mm_set1_epi32(subkeys[rounds-1]); + y1 = _mm_xor_si128(_mm_xor_si128(y1, SIMON64_f(x1)), rk); + Swap128(x1, y1); + } + + // [A1 A3 B1 B3][A2 A4 B2 B4] => [A1 A2 A3 A4][B1 B2 B3 B4] + block0 = _mm_unpacklo_epi32(y1, x1); + block1 = _mm_unpackhi_epi32(y1, x1); +} + +inline void SIMON64_Dec_Block(__m128i &block0, __m128i &block1, + const word32 *subkeys, unsigned int rounds) +{ + // [A1 A2 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... + const __m128 t0 = _mm_castsi128_ps(block0); + const __m128 t1 = _mm_castsi128_ps(block1); + __m128i x1 = _mm_castps_si128(_mm_shuffle_ps(t0, t1, _MM_SHUFFLE(3,1,3,1))); + __m128i y1 = _mm_castps_si128(_mm_shuffle_ps(t0, t1, _MM_SHUFFLE(2,0,2,0))); + + if (rounds & 1) + { + Swap128(x1, y1); + const __m128i rk = _mm_set1_epi32(subkeys[rounds-1]); + y1 = _mm_xor_si128(_mm_xor_si128(y1, rk), SIMON64_f(x1)); + rounds--; + } + + for (int i = static_cast(rounds-2); i >= 0; i -= 2) + { + const __m128i rk1 = _mm_set1_epi32(subkeys[i+1]); + x1 = _mm_xor_si128(_mm_xor_si128(x1, SIMON64_f(y1)), rk1); + + const __m128i rk2 = _mm_set1_epi32(subkeys[i]); + y1 = _mm_xor_si128(_mm_xor_si128(y1, SIMON64_f(x1)), rk2); + } + + // [A1 A3 B1 B3][A2 A4 B2 B4] => [A1 A2 A3 A4][B1 B2 B3 B4] + block0 = _mm_unpacklo_epi32(y1, x1); + block1 = _mm_unpackhi_epi32(y1, x1); +} + +inline void SIMON64_Enc_6_Blocks(__m128i &block0, __m128i &block1, + __m128i &block2, __m128i &block3, __m128i &block4, __m128i &block5, + const word32 *subkeys, unsigned int rounds) +{ + // [A1 A2 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... + const __m128 t0 = _mm_castsi128_ps(block0); + const __m128 t1 = _mm_castsi128_ps(block1); + __m128i x1 = _mm_castps_si128(_mm_shuffle_ps(t0, t1, _MM_SHUFFLE(3,1,3,1))); + __m128i y1 = _mm_castps_si128(_mm_shuffle_ps(t0, t1, _MM_SHUFFLE(2,0,2,0))); + + const __m128 t2 = _mm_castsi128_ps(block2); + const __m128 t3 = _mm_castsi128_ps(block3); + __m128i x2 = _mm_castps_si128(_mm_shuffle_ps(t2, t3, _MM_SHUFFLE(3,1,3,1))); + __m128i y2 = _mm_castps_si128(_mm_shuffle_ps(t2, t3, _MM_SHUFFLE(2,0,2,0))); + + const __m128 t4 = _mm_castsi128_ps(block4); + const __m128 t5 = _mm_castsi128_ps(block5); + __m128i x3 = _mm_castps_si128(_mm_shuffle_ps(t4, t5, _MM_SHUFFLE(3,1,3,1))); + __m128i y3 = _mm_castps_si128(_mm_shuffle_ps(t4, t5, _MM_SHUFFLE(2,0,2,0))); + + for (int i = 0; i < static_cast(rounds & ~1)-1; i += 2) + { + const __m128i rk1 = _mm_set1_epi32(subkeys[i]); + y1 = _mm_xor_si128(_mm_xor_si128(y1, SIMON64_f(x1)), rk1); + y2 = _mm_xor_si128(_mm_xor_si128(y2, SIMON64_f(x2)), rk1); + y3 = _mm_xor_si128(_mm_xor_si128(y3, SIMON64_f(x3)), rk1); + + const __m128i rk2 = _mm_set1_epi32(subkeys[i+1]); + x1 = _mm_xor_si128(_mm_xor_si128(x1, SIMON64_f(y1)), rk2); + x2 = _mm_xor_si128(_mm_xor_si128(x2, SIMON64_f(y2)), rk2); + x3 = _mm_xor_si128(_mm_xor_si128(x3, SIMON64_f(y3)), rk2); + } + + if (rounds & 1) + { + const __m128i rk = _mm_set1_epi32(subkeys[rounds-1]); + y1 = _mm_xor_si128(_mm_xor_si128(y1, SIMON64_f(x1)), rk); + y2 = _mm_xor_si128(_mm_xor_si128(y2, SIMON64_f(x2)), rk); + y3 = _mm_xor_si128(_mm_xor_si128(y3, SIMON64_f(x3)), rk); + Swap128(x1, y1); Swap128(x2, y2); Swap128(x3, y3); + } + + // [A1 A3 B1 B3][A2 A4 B2 B4] => [A1 A2 A3 A4][B1 B2 B3 B4] + block0 = _mm_unpacklo_epi32(y1, x1); + block1 = _mm_unpackhi_epi32(y1, x1); + block2 = _mm_unpacklo_epi32(y2, x2); + block3 = _mm_unpackhi_epi32(y2, x2); + block4 = _mm_unpacklo_epi32(y3, x3); + block5 = _mm_unpackhi_epi32(y3, x3); +} + +inline void SIMON64_Dec_6_Blocks(__m128i &block0, __m128i &block1, + __m128i &block2, __m128i &block3, __m128i &block4, __m128i &block5, + const word32 *subkeys, unsigned int rounds) +{ + // [A1 A2 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... + const __m128 t0 = _mm_castsi128_ps(block0); + const __m128 t1 = _mm_castsi128_ps(block1); + __m128i x1 = _mm_castps_si128(_mm_shuffle_ps(t0, t1, _MM_SHUFFLE(3,1,3,1))); + __m128i y1 = _mm_castps_si128(_mm_shuffle_ps(t0, t1, _MM_SHUFFLE(2,0,2,0))); + + const __m128 t2 = _mm_castsi128_ps(block2); + const __m128 t3 = _mm_castsi128_ps(block3); + __m128i x2 = _mm_castps_si128(_mm_shuffle_ps(t2, t3, _MM_SHUFFLE(3,1,3,1))); + __m128i y2 = _mm_castps_si128(_mm_shuffle_ps(t2, t3, _MM_SHUFFLE(2,0,2,0))); + + const __m128 t4 = _mm_castsi128_ps(block4); + const __m128 t5 = _mm_castsi128_ps(block5); + __m128i x3 = _mm_castps_si128(_mm_shuffle_ps(t4, t5, _MM_SHUFFLE(3,1,3,1))); + __m128i y3 = _mm_castps_si128(_mm_shuffle_ps(t4, t5, _MM_SHUFFLE(2,0,2,0))); + + if (rounds & 1) + { + Swap128(x1, y1); Swap128(x2, y2); Swap128(x3, y3); + const __m128i rk = _mm_set1_epi32(subkeys[rounds-1]); + y1 = _mm_xor_si128(_mm_xor_si128(y1, rk), SIMON64_f(x1)); + y2 = _mm_xor_si128(_mm_xor_si128(y2, rk), SIMON64_f(x2)); + y3 = _mm_xor_si128(_mm_xor_si128(y3, rk), SIMON64_f(x3)); + rounds--; + } + + for (int i = static_cast(rounds-2); i >= 0; i -= 2) + { + const __m128i rk1 = _mm_set1_epi32(subkeys[i+1]); + x1 = _mm_xor_si128(_mm_xor_si128(x1, SIMON64_f(y1)), rk1); + x2 = _mm_xor_si128(_mm_xor_si128(x2, SIMON64_f(y2)), rk1); + x3 = _mm_xor_si128(_mm_xor_si128(x3, SIMON64_f(y3)), rk1); + + const __m128i rk2 = _mm_set1_epi32(subkeys[i]); + y1 = _mm_xor_si128(_mm_xor_si128(y1, SIMON64_f(x1)), rk2); + y2 = _mm_xor_si128(_mm_xor_si128(y2, SIMON64_f(x2)), rk2); + y3 = _mm_xor_si128(_mm_xor_si128(y3, SIMON64_f(x3)), rk2); + } + + // [A1 A3 B1 B3][A2 A4 B2 B4] => [A1 A2 A3 A4][B1 B2 B3 B4] + block0 = _mm_unpacklo_epi32(y1, x1); + block1 = _mm_unpackhi_epi32(y1, x1); + block2 = _mm_unpacklo_epi32(y2, x2); + block3 = _mm_unpackhi_epi32(y2, x2); + block4 = _mm_unpacklo_epi32(y3, x3); + block5 = _mm_unpackhi_epi32(y3, x3); +} + +#endif // CRYPTOPP_SSE41_AVAILABLE + +// ***************************** Power7 ***************************** // + +#if defined(CRYPTOPP_POWER7_AVAILABLE) + +using CryptoPP::uint8x16_p; +using CryptoPP::uint32x4_p; + +using CryptoPP::VectorAnd; +using CryptoPP::VectorXor; +using CryptoPP::VectorLoadBE; + +// 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 = {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); +} + +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 = {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); +} + +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 = {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 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 = {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 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_POWER7_AVAILABLE + +ANONYMOUS_NAMESPACE_END + +/////////////////////////////////////////////////////////////////////// + +NAMESPACE_BEGIN(CryptoPP) + +// *************************** ARM NEON **************************** // + +#if (CRYPTOPP_ARM_NEON_AVAILABLE) +size_t SIMON64_Enc_AdvancedProcessBlocks_NEON(const word32* subKeys, size_t rounds, + const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) +{ + return AdvancedProcessBlocks64_6x2_NEON(SIMON64_Enc_Block, SIMON64_Enc_6_Blocks, + subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags); +} + +size_t SIMON64_Dec_AdvancedProcessBlocks_NEON(const word32* subKeys, size_t rounds, + const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) +{ + return AdvancedProcessBlocks64_6x2_NEON(SIMON64_Dec_Block, SIMON64_Dec_6_Blocks, + subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags); +} +#endif // CRYPTOPP_ARM_NEON_AVAILABLE + +// ***************************** IA-32 ***************************** // + +#if defined(CRYPTOPP_SSE41_AVAILABLE) +size_t SIMON64_Enc_AdvancedProcessBlocks_SSE41(const word32* subKeys, size_t rounds, + const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) +{ + return AdvancedProcessBlocks64_6x2_SSE(SIMON64_Enc_Block, SIMON64_Enc_6_Blocks, + subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags); +} + +size_t SIMON64_Dec_AdvancedProcessBlocks_SSE41(const word32* subKeys, size_t rounds, + const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) +{ + return AdvancedProcessBlocks64_6x2_SSE(SIMON64_Dec_Block, SIMON64_Dec_6_Blocks, + subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags); +} +#endif + +// ***************************** Power7 ***************************** // + +#if defined(CRYPTOPP_POWER7_AVAILABLE) +size_t SIMON64_Enc_AdvancedProcessBlocks_POWER7(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_POWER7(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); +} +#endif + +NAMESPACE_END diff --git a/speck-simd.cpp b/speck-simd.cpp deleted file mode 100644 index d82842a2..00000000 --- a/speck-simd.cpp +++ /dev/null @@ -1,1410 +0,0 @@ -// speck-simd.cpp - written and placed in the public domain by Jeffrey Walton -// -// This source file uses intrinsics and built-ins to gain access to -// SSSE3, ARM NEON and ARMv8a, and Power7 Altivec instructions. A separate -// source file is needed because additional CXXFLAGS are required to enable -// the appropriate instructions sets in some build configurations. - -#include "pch.h" -#include "config.h" - -#include "speck.h" -#include "misc.h" -#include "adv-simd.h" - -// Uncomment for benchmarking C++ against SSE or NEON. -// Do so in both speck.cpp and speck-simd.cpp. -// #undef CRYPTOPP_SSSE3_AVAILABLE -// #undef CRYPTOPP_SSE41_AVAILABLE -// #undef CRYPTOPP_ARM_NEON_AVAILABLE - -#if (CRYPTOPP_SSSE3_AVAILABLE) -# include -# include -#endif - -#if (CRYPTOPP_SSE41_AVAILABLE) -# include -#endif - -#if defined(__AVX512F__) && defined(__AVX512VL__) -# define CRYPTOPP_AVX512_ROTATE 1 -# include -#endif - -#if (CRYPTOPP_ARM_NEON_AVAILABLE) -# include -#endif - -// Can't use CRYPTOPP_ARM_XXX_AVAILABLE because too many -// compilers don't follow ACLE conventions for the include. -#if defined(CRYPTOPP_ARM_ACLE_AVAILABLE) -# include -# include -#endif - -#if defined(CRYPTOPP_POWER7_AVAILABLE) -# include "ppc-simd.h" -#endif - -// Squash MS LNK4221 and libtool warnings -extern const char SPECK_SIMD_FNAME[] = __FILE__; - -ANONYMOUS_NAMESPACE_BEGIN - -using CryptoPP::byte; -using CryptoPP::word32; -using CryptoPP::word64; - -// *************************** ARM NEON ************************** // - -#if (CRYPTOPP_ARM_NEON_AVAILABLE) - -template -inline T UnpackHigh32(const T& a, const T& b) -{ - const uint32x2_t x(vget_high_u32((uint32x4_t)a)); - const uint32x2_t y(vget_high_u32((uint32x4_t)b)); - const uint32x2x2_t r = vzip_u32(x, y); - return (T)vcombine_u32(r.val[0], r.val[1]); -} - -template -inline T UnpackLow32(const T& a, const T& b) -{ - const uint32x2_t x(vget_low_u32((uint32x4_t)a)); - const uint32x2_t y(vget_low_u32((uint32x4_t)b)); - const uint32x2x2_t r = vzip_u32(x, y); - return (T)vcombine_u32(r.val[0], r.val[1]); -} - -template -inline uint32x4_t RotateLeft32(const uint32x4_t& val) -{ - const uint32x4_t a(vshlq_n_u32(val, R)); - const uint32x4_t b(vshrq_n_u32(val, 32 - R)); - return vorrq_u32(a, b); -} - -template -inline uint32x4_t RotateRight32(const uint32x4_t& val) -{ - const uint32x4_t a(vshlq_n_u32(val, 32 - R)); - const uint32x4_t b(vshrq_n_u32(val, R)); - return vorrq_u32(a, b); -} - -#if defined(__aarch32__) || defined(__aarch64__) -// Faster than two Shifts and an Or. Thanks to Louis Wingers and Bryan Weeks. -template <> -inline uint32x4_t RotateLeft32<8>(const uint32x4_t& val) -{ -#if defined(CRYPTOPP_BIG_ENDIAN) - const uint8_t maskb[16] = { 14,13,12,15, 10,9,8,11, 6,5,4,7, 2,1,0,3 }; - const uint8x16_t mask = vld1q_u8(maskb); -#else - const uint8_t maskb[16] = { 3,0,1,2, 7,4,5,6, 11,8,9,10, 15,12,13,14 }; - const uint8x16_t mask = vld1q_u8(maskb); -#endif - - return vreinterpretq_u32_u8( - vqtbl1q_u8(vreinterpretq_u8_u32(val), mask)); -} - -// Faster than two Shifts and an Or. Thanks to Louis Wingers and Bryan Weeks. -template <> -inline uint32x4_t RotateRight32<8>(const uint32x4_t& val) -{ -#if defined(CRYPTOPP_BIG_ENDIAN) - const uint8_t maskb[16] = { 12,15,14,13, 8,11,10,9, 4,7,6,5, 0,3,2,1 }; - const uint8x16_t mask = vld1q_u8(maskb); -#else - const uint8_t maskb[16] = { 1,2,3,0, 5,6,7,4, 9,10,11,8, 13,14,15,12 }; - const uint8x16_t mask = vld1q_u8(maskb); -#endif - - return vreinterpretq_u32_u8( - vqtbl1q_u8(vreinterpretq_u8_u32(val), mask)); -} -#endif // Aarch32 or Aarch64 - -inline void SPECK64_Enc_Block(uint32x4_t &block0, uint32x4_t &block1, - const word32 *subkeys, unsigned int rounds) -{ - // [A1 A2 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... - uint32x4_t x1 = vuzpq_u32(block0, block1).val[1]; - uint32x4_t y1 = vuzpq_u32(block0, block1).val[0]; - - for (int i=0; i < static_cast(rounds); ++i) - { - const uint32x4_t rk = vdupq_n_u32(subkeys[i]); - - x1 = RotateRight32<8>(x1); - x1 = vaddq_u32(x1, y1); - x1 = veorq_u32(x1, rk); - y1 = RotateLeft32<3>(y1); - y1 = veorq_u32(y1, x1); - } - - // [A1 A3 B1 B3][A2 A4 B2 B4] => [A1 A2 A3 A4][B1 B2 B3 B4] - block0 = UnpackLow32(y1, x1); - block1 = UnpackHigh32(y1, x1); -} - -inline void SPECK64_Dec_Block(uint32x4_t &block0, uint32x4_t &block1, - const word32 *subkeys, unsigned int rounds) -{ - // [A1 A2 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... - uint32x4_t x1 = vuzpq_u32(block0, block1).val[1]; - uint32x4_t y1 = vuzpq_u32(block0, block1).val[0]; - - for (int i = static_cast(rounds-1); i >= 0; --i) - { - const uint32x4_t rk = vdupq_n_u32(subkeys[i]); - - y1 = veorq_u32(y1, x1); - y1 = RotateRight32<3>(y1); - x1 = veorq_u32(x1, rk); - x1 = vsubq_u32(x1, y1); - x1 = RotateLeft32<8>(x1); - } - - // [A1 A3 B1 B3][A2 A4 B2 B4] => [A1 A2 A3 A4][B1 B2 B3 B4] - block0 = UnpackLow32(y1, x1); - block1 = UnpackHigh32(y1, x1); -} - -inline void SPECK64_Enc_6_Blocks(uint32x4_t &block0, uint32x4_t &block1, - uint32x4_t &block2, uint32x4_t &block3, uint32x4_t &block4, uint32x4_t &block5, - const word32 *subkeys, unsigned int rounds) -{ - // [A1 A2 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... - uint32x4_t x1 = vuzpq_u32(block0, block1).val[1]; - uint32x4_t y1 = vuzpq_u32(block0, block1).val[0]; - uint32x4_t x2 = vuzpq_u32(block2, block3).val[1]; - uint32x4_t y2 = vuzpq_u32(block2, block3).val[0]; - uint32x4_t x3 = vuzpq_u32(block4, block5).val[1]; - uint32x4_t y3 = vuzpq_u32(block4, block5).val[0]; - - for (int i=0; i < static_cast(rounds); ++i) - { - const uint32x4_t rk = vdupq_n_u32(subkeys[i]); - - x1 = RotateRight32<8>(x1); - x2 = RotateRight32<8>(x2); - x3 = RotateRight32<8>(x3); - x1 = vaddq_u32(x1, y1); - x2 = vaddq_u32(x2, y2); - x3 = vaddq_u32(x3, y3); - x1 = veorq_u32(x1, rk); - x2 = veorq_u32(x2, rk); - x3 = veorq_u32(x3, rk); - y1 = RotateLeft32<3>(y1); - y2 = RotateLeft32<3>(y2); - y3 = RotateLeft32<3>(y3); - y1 = veorq_u32(y1, x1); - y2 = veorq_u32(y2, x2); - y3 = veorq_u32(y3, x3); - } - - // [A1 A3 B1 B3][A2 A4 B2 B4] => [A1 A2 A3 A4][B1 B2 B3 B4] - block0 = UnpackLow32(y1, x1); - block1 = UnpackHigh32(y1, x1); - block2 = UnpackLow32(y2, x2); - block3 = UnpackHigh32(y2, x2); - block4 = UnpackLow32(y3, x3); - block5 = UnpackHigh32(y3, x3); -} - -inline void SPECK64_Dec_6_Blocks(uint32x4_t &block0, uint32x4_t &block1, - uint32x4_t &block2, uint32x4_t &block3, uint32x4_t &block4, uint32x4_t &block5, - const word32 *subkeys, unsigned int rounds) -{ - // [A1 A2 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... - uint32x4_t x1 = vuzpq_u32(block0, block1).val[1]; - uint32x4_t y1 = vuzpq_u32(block0, block1).val[0]; - uint32x4_t x2 = vuzpq_u32(block2, block3).val[1]; - uint32x4_t y2 = vuzpq_u32(block2, block3).val[0]; - uint32x4_t x3 = vuzpq_u32(block4, block5).val[1]; - uint32x4_t y3 = vuzpq_u32(block4, block5).val[0]; - - for (int i = static_cast(rounds-1); i >= 0; --i) - { - const uint32x4_t rk = vdupq_n_u32(subkeys[i]); - - y1 = veorq_u32(y1, x1); - y2 = veorq_u32(y2, x2); - y3 = veorq_u32(y3, x3); - y1 = RotateRight32<3>(y1); - y2 = RotateRight32<3>(y2); - y3 = RotateRight32<3>(y3); - x1 = veorq_u32(x1, rk); - x2 = veorq_u32(x2, rk); - x3 = veorq_u32(x3, rk); - x1 = vsubq_u32(x1, y1); - x2 = vsubq_u32(x2, y2); - x3 = vsubq_u32(x3, y3); - x1 = RotateLeft32<8>(x1); - x2 = RotateLeft32<8>(x2); - x3 = RotateLeft32<8>(x3); - } - - // [A1 A3 B1 B3][A2 A4 B2 B4] => [A1 A2 A3 A4][B1 B2 B3 B4] - block0 = UnpackLow32(y1, x1); - block1 = UnpackHigh32(y1, x1); - block2 = UnpackLow32(y2, x2); - block3 = UnpackHigh32(y2, x2); - block4 = UnpackLow32(y3, x3); - block5 = UnpackHigh32(y3, x3); -} - -#endif // CRYPTOPP_ARM_NEON_AVAILABLE - -#if (CRYPTOPP_ARM_NEON_AVAILABLE) - -template -inline T UnpackHigh64(const T& a, const T& b) -{ - const uint64x1_t x(vget_high_u64((uint64x2_t)a)); - const uint64x1_t y(vget_high_u64((uint64x2_t)b)); - return (T)vcombine_u64(x, y); -} - -template -inline T UnpackLow64(const T& a, const T& b) -{ - const uint64x1_t x(vget_low_u64((uint64x2_t)a)); - const uint64x1_t y(vget_low_u64((uint64x2_t)b)); - return (T)vcombine_u64(x, y); -} - -template -inline uint64x2_t RotateLeft64(const uint64x2_t& val) -{ - const uint64x2_t a(vshlq_n_u64(val, R)); - const uint64x2_t b(vshrq_n_u64(val, 64 - R)); - return vorrq_u64(a, b); -} - -template -inline uint64x2_t RotateRight64(const uint64x2_t& val) -{ - const uint64x2_t a(vshlq_n_u64(val, 64 - R)); - const uint64x2_t b(vshrq_n_u64(val, R)); - return vorrq_u64(a, b); -} - -#if defined(__aarch32__) || defined(__aarch64__) -// Faster than two Shifts and an Or. Thanks to Louis Wingers and Bryan Weeks. -template <> -inline uint64x2_t RotateLeft64<8>(const uint64x2_t& val) -{ -#if defined(CRYPTOPP_BIG_ENDIAN) - const uint8_t maskb[16] = { 14,13,12,11, 10,9,8,15, 6,5,4,3, 2,1,0,7 }; - const uint8x16_t mask = vld1q_u8(maskb); -#else - const uint8_t maskb[16] = { 7,0,1,2, 3,4,5,6, 15,8,9,10, 11,12,13,14 }; - const uint8x16_t mask = vld1q_u8(maskb); -#endif - - return vreinterpretq_u64_u8( - vqtbl1q_u8(vreinterpretq_u8_u64(val), mask)); -} - -// Faster than two Shifts and an Or. Thanks to Louis Wingers and Bryan Weeks. -template <> -inline uint64x2_t RotateRight64<8>(const uint64x2_t& val) -{ -#if defined(CRYPTOPP_BIG_ENDIAN) - const uint8_t maskb[16] = { 8,15,14,13, 12,11,10,9, 0,7,6,5, 4,3,2,1 }; - const uint8x16_t mask = vld1q_u8(maskb); -#else - const uint8_t maskb[16] = { 1,2,3,4, 5,6,7,0, 9,10,11,12, 13,14,15,8 }; - const uint8x16_t mask = vld1q_u8(maskb); -#endif - - return vreinterpretq_u64_u8( - vqtbl1q_u8(vreinterpretq_u8_u64(val), mask)); -} -#endif - -inline void SPECK128_Enc_Block(uint64x2_t &block0, uint64x2_t &block1, - const word64 *subkeys, unsigned int rounds) -{ - // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ... - uint64x2_t x1 = UnpackHigh64(block0, block1); - uint64x2_t y1 = UnpackLow64(block0, block1); - - for (int i=0; i < static_cast(rounds); ++i) - { - const uint64x2_t rk = vld1q_dup_u64(subkeys+i); - - x1 = RotateRight64<8>(x1); - x1 = vaddq_u64(x1, y1); - x1 = veorq_u64(x1, rk); - y1 = RotateLeft64<3>(y1); - y1 = veorq_u64(y1, x1); - } - - // [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ... - block0 = UnpackLow64(y1, x1); - block1 = UnpackHigh64(y1, x1); -} - -inline void SPECK128_Enc_6_Blocks(uint64x2_t &block0, uint64x2_t &block1, - uint64x2_t &block2, uint64x2_t &block3, uint64x2_t &block4, uint64x2_t &block5, - const word64 *subkeys, unsigned int rounds) -{ - // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ... - uint64x2_t x1 = UnpackHigh64(block0, block1); - uint64x2_t y1 = UnpackLow64(block0, block1); - uint64x2_t x2 = UnpackHigh64(block2, block3); - uint64x2_t y2 = UnpackLow64(block2, block3); - uint64x2_t x3 = UnpackHigh64(block4, block5); - uint64x2_t y3 = UnpackLow64(block4, block5); - - for (int i=0; i < static_cast(rounds); ++i) - { - const uint64x2_t rk = vld1q_dup_u64(subkeys+i); - - x1 = RotateRight64<8>(x1); - x2 = RotateRight64<8>(x2); - x3 = RotateRight64<8>(x3); - x1 = vaddq_u64(x1, y1); - x2 = vaddq_u64(x2, y2); - x3 = vaddq_u64(x3, y3); - x1 = veorq_u64(x1, rk); - x2 = veorq_u64(x2, rk); - x3 = veorq_u64(x3, rk); - y1 = RotateLeft64<3>(y1); - y2 = RotateLeft64<3>(y2); - y3 = RotateLeft64<3>(y3); - y1 = veorq_u64(y1, x1); - y2 = veorq_u64(y2, x2); - y3 = veorq_u64(y3, x3); - } - - // [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ... - block0 = UnpackLow64(y1, x1); - block1 = UnpackHigh64(y1, x1); - block2 = UnpackLow64(y2, x2); - block3 = UnpackHigh64(y2, x2); - block4 = UnpackLow64(y3, x3); - block5 = UnpackHigh64(y3, x3); -} - -inline void SPECK128_Dec_Block(uint64x2_t &block0, uint64x2_t &block1, - const word64 *subkeys, unsigned int rounds) -{ - // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ... - uint64x2_t x1 = UnpackHigh64(block0, block1); - uint64x2_t y1 = UnpackLow64(block0, block1); - - for (int i = static_cast(rounds-1); i >= 0; --i) - { - const uint64x2_t rk = vld1q_dup_u64(subkeys+i); - - y1 = veorq_u64(y1, x1); - y1 = RotateRight64<3>(y1); - x1 = veorq_u64(x1, rk); - x1 = vsubq_u64(x1, y1); - x1 = RotateLeft64<8>(x1); - } - - // [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ... - block0 = UnpackLow64(y1, x1); - block1 = UnpackHigh64(y1, x1); -} - -inline void SPECK128_Dec_6_Blocks(uint64x2_t &block0, uint64x2_t &block1, - uint64x2_t &block2, uint64x2_t &block3, uint64x2_t &block4, uint64x2_t &block5, - const word64 *subkeys, unsigned int rounds) -{ - // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ... - uint64x2_t x1 = UnpackHigh64(block0, block1); - uint64x2_t y1 = UnpackLow64(block0, block1); - uint64x2_t x2 = UnpackHigh64(block2, block3); - uint64x2_t y2 = UnpackLow64(block2, block3); - uint64x2_t x3 = UnpackHigh64(block4, block5); - uint64x2_t y3 = UnpackLow64(block4, block5); - - for (int i = static_cast(rounds-1); i >= 0; --i) - { - const uint64x2_t rk = vld1q_dup_u64(subkeys+i); - - y1 = veorq_u64(y1, x1); - y2 = veorq_u64(y2, x2); - y3 = veorq_u64(y3, x3); - y1 = RotateRight64<3>(y1); - y2 = RotateRight64<3>(y2); - y3 = RotateRight64<3>(y3); - x1 = veorq_u64(x1, rk); - x2 = veorq_u64(x2, rk); - x3 = veorq_u64(x3, rk); - x1 = vsubq_u64(x1, y1); - x2 = vsubq_u64(x2, y2); - x3 = vsubq_u64(x3, y3); - x1 = RotateLeft64<8>(x1); - x2 = RotateLeft64<8>(x2); - x3 = RotateLeft64<8>(x3); - } - - // [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ... - block0 = UnpackLow64(y1, x1); - block1 = UnpackHigh64(y1, x1); - block2 = UnpackLow64(y2, x2); - block3 = UnpackHigh64(y2, x2); - block4 = UnpackLow64(y3, x3); - block5 = UnpackHigh64(y3, x3); -} - -#endif // CRYPTOPP_ARM_NEON_AVAILABLE - -// ***************************** IA-32 ***************************** // - -#if defined(CRYPTOPP_SSSE3_AVAILABLE) - -// Clang __m128i casts, http://bugs.llvm.org/show_bug.cgi?id=20670 -#ifndef M128_CAST -# define M128_CAST(x) ((__m128i *)(void *)(x)) -#endif -#ifndef CONST_M128_CAST -# define CONST_M128_CAST(x) ((const __m128i *)(const void *)(x)) -#endif - -// GCC double casts, https://www.spinics.net/lists/gcchelp/msg47735.html -#ifndef DOUBLE_CAST -# define DOUBLE_CAST(x) ((double *)(void *)(x)) -#endif -#ifndef CONST_DOUBLE_CAST -# define CONST_DOUBLE_CAST(x) ((const double *)(const void *)(x)) -#endif - -template -inline __m128i RotateLeft64(const __m128i& val) -{ -#if defined(CRYPTOPP_AVX512_ROTATE) - return _mm_rol_epi64(val, R); -#else - return _mm_or_si128( - _mm_slli_epi64(val, R), _mm_srli_epi64(val, 64-R)); -#endif -} - -template -inline __m128i RotateRight64(const __m128i& val) -{ -#if defined(CRYPTOPP_AVX512_ROTATE) - return _mm_ror_epi64(val, R); -#else - return _mm_or_si128( - _mm_slli_epi64(val, 64-R), _mm_srli_epi64(val, R)); -#endif -} - -// Faster than two Shifts and an Or. Thanks to Louis Wingers and Bryan Weeks. -template <> -inline __m128i RotateLeft64<8>(const __m128i& val) -{ - const __m128i mask = _mm_set_epi8(14,13,12,11, 10,9,8,15, 6,5,4,3, 2,1,0,7); - return _mm_shuffle_epi8(val, mask); -} - -// Faster than two Shifts and an Or. Thanks to Louis Wingers and Bryan Weeks. -template <> -inline __m128i RotateRight64<8>(const __m128i& val) -{ - const __m128i mask = _mm_set_epi8(8,15,14,13, 12,11,10,9, 0,7,6,5, 4,3,2,1); - return _mm_shuffle_epi8(val, mask); -} - -inline void SPECK128_Enc_Block(__m128i &block0, __m128i &block1, - const word64 *subkeys, unsigned int rounds) -{ - // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ... - __m128i x1 = _mm_unpackhi_epi64(block0, block1); - __m128i y1 = _mm_unpacklo_epi64(block0, block1); - - for (int i=0; i < static_cast(rounds); ++i) - { - const __m128i rk = _mm_castpd_si128( - _mm_loaddup_pd(CONST_DOUBLE_CAST(subkeys+i))); - - x1 = RotateRight64<8>(x1); - x1 = _mm_add_epi64(x1, y1); - x1 = _mm_xor_si128(x1, rk); - y1 = RotateLeft64<3>(y1); - y1 = _mm_xor_si128(y1, x1); - } - - // [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ... - block0 = _mm_unpacklo_epi64(y1, x1); - block1 = _mm_unpackhi_epi64(y1, x1); -} - -inline void SPECK128_Enc_6_Blocks(__m128i &block0, __m128i &block1, - __m128i &block2, __m128i &block3, __m128i &block4, __m128i &block5, - const word64 *subkeys, unsigned int rounds) -{ - // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ... - __m128i x1 = _mm_unpackhi_epi64(block0, block1); - __m128i y1 = _mm_unpacklo_epi64(block0, block1); - __m128i x2 = _mm_unpackhi_epi64(block2, block3); - __m128i y2 = _mm_unpacklo_epi64(block2, block3); - __m128i x3 = _mm_unpackhi_epi64(block4, block5); - __m128i y3 = _mm_unpacklo_epi64(block4, block5); - - for (int i=0; i < static_cast(rounds); ++i) - { - const __m128i rk = _mm_castpd_si128( - _mm_loaddup_pd(CONST_DOUBLE_CAST(subkeys+i))); - - x1 = RotateRight64<8>(x1); - x2 = RotateRight64<8>(x2); - x3 = RotateRight64<8>(x3); - x1 = _mm_add_epi64(x1, y1); - x2 = _mm_add_epi64(x2, y2); - x3 = _mm_add_epi64(x3, y3); - x1 = _mm_xor_si128(x1, rk); - x2 = _mm_xor_si128(x2, rk); - x3 = _mm_xor_si128(x3, rk); - y1 = RotateLeft64<3>(y1); - y2 = RotateLeft64<3>(y2); - y3 = RotateLeft64<3>(y3); - y1 = _mm_xor_si128(y1, x1); - y2 = _mm_xor_si128(y2, x2); - y3 = _mm_xor_si128(y3, x3); - } - - // [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ... - block0 = _mm_unpacklo_epi64(y1, x1); - block1 = _mm_unpackhi_epi64(y1, x1); - block2 = _mm_unpacklo_epi64(y2, x2); - block3 = _mm_unpackhi_epi64(y2, x2); - block4 = _mm_unpacklo_epi64(y3, x3); - block5 = _mm_unpackhi_epi64(y3, x3); -} - -inline void SPECK128_Dec_Block(__m128i &block0, __m128i &block1, - const word64 *subkeys, unsigned int rounds) -{ - // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ... - __m128i x1 = _mm_unpackhi_epi64(block0, block1); - __m128i y1 = _mm_unpacklo_epi64(block0, block1); - - for (int i = static_cast(rounds-1); i >= 0; --i) - { - const __m128i rk = _mm_castpd_si128( - _mm_loaddup_pd(CONST_DOUBLE_CAST(subkeys+i))); - - y1 = _mm_xor_si128(y1, x1); - y1 = RotateRight64<3>(y1); - x1 = _mm_xor_si128(x1, rk); - x1 = _mm_sub_epi64(x1, y1); - x1 = RotateLeft64<8>(x1); - } - - // [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ... - block0 = _mm_unpacklo_epi64(y1, x1); - block1 = _mm_unpackhi_epi64(y1, x1); -} - -inline void SPECK128_Dec_6_Blocks(__m128i &block0, __m128i &block1, - __m128i &block2, __m128i &block3, __m128i &block4, __m128i &block5, - const word64 *subkeys, unsigned int rounds) -{ - // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ... - __m128i x1 = _mm_unpackhi_epi64(block0, block1); - __m128i y1 = _mm_unpacklo_epi64(block0, block1); - __m128i x2 = _mm_unpackhi_epi64(block2, block3); - __m128i y2 = _mm_unpacklo_epi64(block2, block3); - __m128i x3 = _mm_unpackhi_epi64(block4, block5); - __m128i y3 = _mm_unpacklo_epi64(block4, block5); - - for (int i = static_cast(rounds-1); i >= 0; --i) - { - const __m128i rk = _mm_castpd_si128( - _mm_loaddup_pd(CONST_DOUBLE_CAST(subkeys+i))); - - y1 = _mm_xor_si128(y1, x1); - y2 = _mm_xor_si128(y2, x2); - y3 = _mm_xor_si128(y3, x3); - y1 = RotateRight64<3>(y1); - y2 = RotateRight64<3>(y2); - y3 = RotateRight64<3>(y3); - x1 = _mm_xor_si128(x1, rk); - x2 = _mm_xor_si128(x2, rk); - x3 = _mm_xor_si128(x3, rk); - x1 = _mm_sub_epi64(x1, y1); - x2 = _mm_sub_epi64(x2, y2); - x3 = _mm_sub_epi64(x3, y3); - x1 = RotateLeft64<8>(x1); - x2 = RotateLeft64<8>(x2); - x3 = RotateLeft64<8>(x3); - } - - // [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ... - block0 = _mm_unpacklo_epi64(y1, x1); - block1 = _mm_unpackhi_epi64(y1, x1); - block2 = _mm_unpacklo_epi64(y2, x2); - block3 = _mm_unpackhi_epi64(y2, x2); - block4 = _mm_unpacklo_epi64(y3, x3); - block5 = _mm_unpackhi_epi64(y3, x3); -} - -#endif // CRYPTOPP_SSSE3_AVAILABLE - -#if defined(CRYPTOPP_SSE41_AVAILABLE) - -template -inline __m128i RotateLeft32(const __m128i& val) -{ - return _mm_or_si128( - _mm_slli_epi32(val, R), _mm_srli_epi32(val, 32-R)); -} - -template -inline __m128i RotateRight32(const __m128i& val) -{ - return _mm_or_si128( - _mm_slli_epi32(val, 32-R), _mm_srli_epi32(val, R)); -} - -// Faster than two Shifts and an Or. Thanks to Louis Wingers and Bryan Weeks. -template <> -inline __m128i RotateLeft32<8>(const __m128i& val) -{ - const __m128i mask = _mm_set_epi8(14,13,12,15, 10,9,8,11, 6,5,4,7, 2,1,0,3); - return _mm_shuffle_epi8(val, mask); -} - -// Faster than two Shifts and an Or. Thanks to Louis Wingers and Bryan Weeks. -template <> -inline __m128i RotateRight32<8>(const __m128i& val) -{ - const __m128i mask = _mm_set_epi8(12,15,14,13, 8,11,10,9, 4,7,6,5, 0,3,2,1); - return _mm_shuffle_epi8(val, mask); -} - -inline void SPECK64_Enc_Block(__m128i &block0, __m128i &block1, - const word32 *subkeys, unsigned int rounds) -{ - // [A1 A2 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... - const __m128 t0 = _mm_castsi128_ps(block0); - const __m128 t1 = _mm_castsi128_ps(block1); - __m128i x1 = _mm_castps_si128(_mm_shuffle_ps(t0, t1, _MM_SHUFFLE(3,1,3,1))); - __m128i y1 = _mm_castps_si128(_mm_shuffle_ps(t0, t1, _MM_SHUFFLE(2,0,2,0))); - - for (int i=0; i < static_cast(rounds); ++i) - { - const __m128i rk = _mm_set1_epi32(subkeys[i]); - - x1 = RotateRight32<8>(x1); - x1 = _mm_add_epi32(x1, y1); - x1 = _mm_xor_si128(x1, rk); - y1 = RotateLeft32<3>(y1); - y1 = _mm_xor_si128(y1, x1); - } - - // The is roughly the SSE equivalent to ARM vzp32 - // [A1 A3 B1 B3][A2 A4 B2 B4] => [A1 A2 A3 A4][B1 B2 B3 B4] - block0 = _mm_unpacklo_epi32(y1, x1); - block1 = _mm_unpackhi_epi32(y1, x1); -} - -inline void SPECK64_Dec_Block(__m128i &block0, __m128i &block1, - const word32 *subkeys, unsigned int rounds) -{ - // [A1 A2 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... - const __m128 t0 = _mm_castsi128_ps(block0); - const __m128 t1 = _mm_castsi128_ps(block1); - __m128i x1 = _mm_castps_si128(_mm_shuffle_ps(t0, t1, _MM_SHUFFLE(3,1,3,1))); - __m128i y1 = _mm_castps_si128(_mm_shuffle_ps(t0, t1, _MM_SHUFFLE(2,0,2,0))); - - for (int i = static_cast(rounds-1); i >= 0; --i) - { - const __m128i rk = _mm_set1_epi32(subkeys[i]); - - y1 = _mm_xor_si128(y1, x1); - y1 = RotateRight32<3>(y1); - x1 = _mm_xor_si128(x1, rk); - x1 = _mm_sub_epi32(x1, y1); - x1 = RotateLeft32<8>(x1); - } - - // The is roughly the SSE equivalent to ARM vzp32 - // [A1 A3 B1 B3][A2 A4 B2 B4] => [A1 A2 A3 A4][B1 B2 B3 B4] - block0 = _mm_unpacklo_epi32(y1, x1); - block1 = _mm_unpackhi_epi32(y1, x1); -} - -inline void SPECK64_Enc_6_Blocks(__m128i &block0, __m128i &block1, - __m128i &block2, __m128i &block3, __m128i &block4, __m128i &block5, - const word32 *subkeys, unsigned int rounds) -{ - // [A1 A2 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... - const __m128 t0 = _mm_castsi128_ps(block0); - const __m128 t1 = _mm_castsi128_ps(block1); - __m128i x1 = _mm_castps_si128(_mm_shuffle_ps(t0, t1, _MM_SHUFFLE(3,1,3,1))); - __m128i y1 = _mm_castps_si128(_mm_shuffle_ps(t0, t1, _MM_SHUFFLE(2,0,2,0))); - - const __m128 t2 = _mm_castsi128_ps(block2); - const __m128 t3 = _mm_castsi128_ps(block3); - __m128i x2 = _mm_castps_si128(_mm_shuffle_ps(t2, t3, _MM_SHUFFLE(3,1,3,1))); - __m128i y2 = _mm_castps_si128(_mm_shuffle_ps(t2, t3, _MM_SHUFFLE(2,0,2,0))); - - const __m128 t4 = _mm_castsi128_ps(block4); - const __m128 t5 = _mm_castsi128_ps(block5); - __m128i x3 = _mm_castps_si128(_mm_shuffle_ps(t4, t5, _MM_SHUFFLE(3,1,3,1))); - __m128i y3 = _mm_castps_si128(_mm_shuffle_ps(t4, t5, _MM_SHUFFLE(2,0,2,0))); - - for (int i=0; i < static_cast(rounds); ++i) - { - const __m128i rk = _mm_set1_epi32(subkeys[i]); - - x1 = RotateRight32<8>(x1); - x2 = RotateRight32<8>(x2); - x3 = RotateRight32<8>(x3); - x1 = _mm_add_epi32(x1, y1); - x2 = _mm_add_epi32(x2, y2); - x3 = _mm_add_epi32(x3, y3); - x1 = _mm_xor_si128(x1, rk); - x2 = _mm_xor_si128(x2, rk); - x3 = _mm_xor_si128(x3, rk); - y1 = RotateLeft32<3>(y1); - y2 = RotateLeft32<3>(y2); - y3 = RotateLeft32<3>(y3); - y1 = _mm_xor_si128(y1, x1); - y2 = _mm_xor_si128(y2, x2); - y3 = _mm_xor_si128(y3, x3); - } - - // The is roughly the SSE equivalent to ARM vzp32 - // [A1 A3 B1 B3][A2 A4 B2 B4] => [A1 A2 A3 A4][B1 B2 B3 B4] - block0 = _mm_unpacklo_epi32(y1, x1); - block1 = _mm_unpackhi_epi32(y1, x1); - block2 = _mm_unpacklo_epi32(y2, x2); - block3 = _mm_unpackhi_epi32(y2, x2); - block4 = _mm_unpacklo_epi32(y3, x3); - block5 = _mm_unpackhi_epi32(y3, x3); -} - -inline void SPECK64_Dec_6_Blocks(__m128i &block0, __m128i &block1, - __m128i &block2, __m128i &block3, __m128i &block4, __m128i &block5, - const word32 *subkeys, unsigned int rounds) -{ - // [A1 A2 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... - const __m128 t0 = _mm_castsi128_ps(block0); - const __m128 t1 = _mm_castsi128_ps(block1); - __m128i x1 = _mm_castps_si128(_mm_shuffle_ps(t0, t1, _MM_SHUFFLE(3,1,3,1))); - __m128i y1 = _mm_castps_si128(_mm_shuffle_ps(t0, t1, _MM_SHUFFLE(2,0,2,0))); - - const __m128 t2 = _mm_castsi128_ps(block2); - const __m128 t3 = _mm_castsi128_ps(block3); - __m128i x2 = _mm_castps_si128(_mm_shuffle_ps(t2, t3, _MM_SHUFFLE(3,1,3,1))); - __m128i y2 = _mm_castps_si128(_mm_shuffle_ps(t2, t3, _MM_SHUFFLE(2,0,2,0))); - - const __m128 t4 = _mm_castsi128_ps(block4); - const __m128 t5 = _mm_castsi128_ps(block5); - __m128i x3 = _mm_castps_si128(_mm_shuffle_ps(t4, t5, _MM_SHUFFLE(3,1,3,1))); - __m128i y3 = _mm_castps_si128(_mm_shuffle_ps(t4, t5, _MM_SHUFFLE(2,0,2,0))); - - for (int i = static_cast(rounds-1); i >= 0; --i) - { - const __m128i rk = _mm_set1_epi32(subkeys[i]); - - y1 = _mm_xor_si128(y1, x1); - y2 = _mm_xor_si128(y2, x2); - y3 = _mm_xor_si128(y3, x3); - y1 = RotateRight32<3>(y1); - y2 = RotateRight32<3>(y2); - y3 = RotateRight32<3>(y3); - x1 = _mm_xor_si128(x1, rk); - x2 = _mm_xor_si128(x2, rk); - x3 = _mm_xor_si128(x3, rk); - x1 = _mm_sub_epi32(x1, y1); - x2 = _mm_sub_epi32(x2, y2); - x3 = _mm_sub_epi32(x3, y3); - x1 = RotateLeft32<8>(x1); - x2 = RotateLeft32<8>(x2); - x3 = RotateLeft32<8>(x3); - } - - // The is roughly the SSE equivalent to ARM vzp32 - // [A1 A3 B1 B3][A2 A4 B2 B4] => [A1 A2 A3 A4][B1 B2 B3 B4] - block0 = _mm_unpacklo_epi32(y1, x1); - block1 = _mm_unpackhi_epi32(y1, x1); - block2 = _mm_unpacklo_epi32(y2, x2); - block3 = _mm_unpackhi_epi32(y2, x2); - block4 = _mm_unpacklo_epi32(y3, x3); - block5 = _mm_unpackhi_epi32(y3, x3); -} - -#endif // CRYPTOPP_SSE41_AVAILABLE - -// ***************************** Power7 ***************************** // - -#if defined(CRYPTOPP_POWER7_AVAILABLE) -using CryptoPP::uint8x16_p; -using CryptoPP::uint32x4_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 // CRYPTOPP_POWER7_AVAILABLE - -// ***************************** 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 uint64x2_p RotateLeft64(const uint64x2_p val) -{ - const uint64x2_p m = {C, C}; - return vec_rl(val, m); -} - -// Rotate right by bit count -template -inline uint64x2_p RotateRight64(const uint64x2_p val) -{ - const uint64x2_p m = {64-C, 64-C}; - return vec_rl(val, m); -} - -void SPECK128_Enc_Block(uint32x4_p &block, const word64 *subkeys, unsigned int rounds) -{ -#if defined(CRYPTOPP_BIG_ENDIAN) - 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}; -#endif - - // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ... - uint64x2_p x1 = (uint64x2_p)vec_perm(block, block, m1); - uint64x2_p y1 = (uint64x2_p)vec_perm(block, block, m2); - - for (int i=0; i < static_cast(rounds); ++i) - { - const uint64x2_p rk = vec_splats((unsigned long long)subkeys[i]); - - x1 = RotateRight64<8>(x1); - x1 = VectorAdd(x1, y1); - x1 = VectorXor(x1, rk); - - y1 = RotateLeft64<3>(y1); - y1 = VectorXor(y1, x1); - } - -#if defined(CRYPTOPP_BIG_ENDIAN) - 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, m3); -} - -void SPECK128_Dec_Block(uint32x4_p &block, const word64 *subkeys, unsigned int rounds) -{ -#if defined(CRYPTOPP_BIG_ENDIAN) - 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}; -#endif - - // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ... - uint64x2_p x1 = (uint64x2_p)vec_perm(block, block, m1); - uint64x2_p y1 = (uint64x2_p)vec_perm(block, block, m2); - - for (int i = static_cast(rounds-1); i >= 0; --i) - { - const uint64x2_p rk = vec_splats((unsigned long long)subkeys[i]); - - y1 = VectorXor(y1, x1); - y1 = RotateRight64<3>(y1); - x1 = VectorXor(x1, rk); - x1 = VectorSub(x1, y1); - x1 = RotateLeft64<8>(x1); - } - -#if defined(CRYPTOPP_BIG_ENDIAN) - 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, m3); -} - -void SPECK128_Enc_6_Blocks(uint32x4_p &block0, uint32x4_p &block1, - uint32x4_p &block2, uint32x4_p &block3, uint32x4_p &block4, - uint32x4_p &block5, const word64 *subkeys, unsigned int rounds) -{ -#if defined(CRYPTOPP_BIG_ENDIAN) - 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}; -#endif - - // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ... - uint64x2_p x1 = (uint64x2_p)vec_perm(block0, block1, m1); - uint64x2_p y1 = (uint64x2_p)vec_perm(block0, block1, m2); - uint64x2_p x2 = (uint64x2_p)vec_perm(block2, block3, m1); - uint64x2_p y2 = (uint64x2_p)vec_perm(block2, block3, m2); - uint64x2_p x3 = (uint64x2_p)vec_perm(block4, block5, m1); - uint64x2_p y3 = (uint64x2_p)vec_perm(block4, block5, m2); - - for (int i=0; i < static_cast(rounds); ++i) - { - const uint64x2_p rk = vec_splats((unsigned long long)subkeys[i]); - - x1 = RotateRight64<8>(x1); - x2 = RotateRight64<8>(x2); - x3 = RotateRight64<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 = RotateLeft64<3>(y1); - y2 = RotateLeft64<3>(y2); - y3 = RotateLeft64<3>(y3); - y1 = VectorXor(y1, x1); - y2 = VectorXor(y2, x2); - y3 = VectorXor(y3, x3); - } - -#if defined(CRYPTOPP_BIG_ENDIAN) - 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] ... - 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 SPECK128_Dec_6_Blocks(uint32x4_p &block0, uint32x4_p &block1, - uint32x4_p &block2, uint32x4_p &block3, uint32x4_p &block4, - uint32x4_p &block5, const word64 *subkeys, unsigned int rounds) -{ -#if defined(CRYPTOPP_BIG_ENDIAN) - 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}; -#endif - - // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ... - uint64x2_p x1 = (uint64x2_p)vec_perm(block0, block1, m1); - uint64x2_p y1 = (uint64x2_p)vec_perm(block0, block1, m2); - uint64x2_p x2 = (uint64x2_p)vec_perm(block2, block3, m1); - uint64x2_p y2 = (uint64x2_p)vec_perm(block2, block3, m2); - uint64x2_p x3 = (uint64x2_p)vec_perm(block4, block5, m1); - uint64x2_p y3 = (uint64x2_p)vec_perm(block4, block5, m2); - - for (int i = static_cast(rounds-1); i >= 0; --i) - { - const uint64x2_p rk = vec_splats((unsigned long long)subkeys[i]); - - y1 = VectorXor(y1, x1); - y2 = VectorXor(y2, x2); - y3 = VectorXor(y3, x3); - y1 = RotateRight64<3>(y1); - y2 = RotateRight64<3>(y2); - y3 = RotateRight64<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 = RotateLeft64<8>(x1); - x2 = RotateLeft64<8>(x2); - x3 = RotateLeft64<8>(x3); - } - -#if defined(CRYPTOPP_BIG_ENDIAN) - 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] ... - 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 - -ANONYMOUS_NAMESPACE_END - -/////////////////////////////////////////////////////////////////////// - -NAMESPACE_BEGIN(CryptoPP) - -// *************************** ARM NEON **************************** // - -#if (CRYPTOPP_ARM_NEON_AVAILABLE) -size_t SPECK64_Enc_AdvancedProcessBlocks_NEON(const word32* subKeys, size_t rounds, - const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) -{ - return AdvancedProcessBlocks64_6x2_NEON(SPECK64_Enc_Block, SPECK64_Enc_6_Blocks, - subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags); -} - -size_t SPECK64_Dec_AdvancedProcessBlocks_NEON(const word32* subKeys, size_t rounds, - const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) -{ - return AdvancedProcessBlocks64_6x2_NEON(SPECK64_Dec_Block, SPECK64_Dec_6_Blocks, - subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags); -} -#endif - -#if (CRYPTOPP_ARM_NEON_AVAILABLE) -size_t SPECK128_Enc_AdvancedProcessBlocks_NEON(const word64* subKeys, size_t rounds, - const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) -{ - return AdvancedProcessBlocks128_6x2_NEON(SPECK128_Enc_Block, SPECK128_Enc_6_Blocks, - subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags); -} - -size_t SPECK128_Dec_AdvancedProcessBlocks_NEON(const word64* subKeys, size_t rounds, - const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) -{ - return AdvancedProcessBlocks128_6x2_NEON(SPECK128_Dec_Block, SPECK128_Dec_6_Blocks, - subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags); -} -#endif // CRYPTOPP_ARM_NEON_AVAILABLE - -// ***************************** IA-32 ***************************** // - -#if defined(CRYPTOPP_SSE41_AVAILABLE) -size_t SPECK64_Enc_AdvancedProcessBlocks_SSE41(const word32* subKeys, size_t rounds, - const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) -{ - return AdvancedProcessBlocks64_6x2_SSE(SPECK64_Enc_Block, SPECK64_Enc_6_Blocks, - subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags); -} - -size_t SPECK64_Dec_AdvancedProcessBlocks_SSE41(const word32* subKeys, size_t rounds, - const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) -{ - return AdvancedProcessBlocks64_6x2_SSE(SPECK64_Dec_Block, SPECK64_Dec_6_Blocks, - subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags); -} -#endif - -#if defined(CRYPTOPP_SSSE3_AVAILABLE) -size_t SPECK128_Enc_AdvancedProcessBlocks_SSSE3(const word64* subKeys, size_t rounds, - const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) -{ - return AdvancedProcessBlocks128_6x2_SSE(SPECK128_Enc_Block, SPECK128_Enc_6_Blocks, - subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags); -} - -size_t SPECK128_Dec_AdvancedProcessBlocks_SSSE3(const word64* subKeys, size_t rounds, - const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) -{ - return AdvancedProcessBlocks128_6x2_SSE(SPECK128_Dec_Block, SPECK128_Dec_6_Blocks, - subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags); -} -#endif // CRYPTOPP_SSSE3_AVAILABLE - -// ***************************** Power7 ***************************** // - -#if defined(CRYPTOPP_POWER7_AVAILABLE) -size_t SPECK64_Enc_AdvancedProcessBlocks_POWER7(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_POWER7(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); -} -#endif - -// ***************************** Power8 ***************************** // - -#if defined(CRYPTOPP_POWER8_AVAILABLE) -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) -{ - return AdvancedProcessBlocks128_6x1_ALTIVEC(SPECK128_Enc_Block, SPECK128_Enc_6_Blocks, - subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags); -} - -size_t SPECK128_Dec_AdvancedProcessBlocks_POWER8(const word64* subKeys, size_t rounds, - const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) -{ - return AdvancedProcessBlocks128_6x1_ALTIVEC(SPECK128_Dec_Block, SPECK128_Dec_6_Blocks, - subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags); -} -#endif // CRYPTOPP_POWER8_AVAILABLE - -NAMESPACE_END diff --git a/speck64-simd.cpp b/speck64-simd.cpp new file mode 100644 index 00000000..73e7ee76 --- /dev/null +++ b/speck64-simd.cpp @@ -0,0 +1,745 @@ +// speck-simd.cpp - written and placed in the public domain by Jeffrey Walton +// +// This source file uses intrinsics and built-ins to gain access to +// SSSE3, ARM NEON and ARMv8a, and Power7 Altivec instructions. A separate +// source file is needed because additional CXXFLAGS are required to enable +// the appropriate instructions sets in some build configurations. + +#include "pch.h" +#include "config.h" + +#include "speck.h" +#include "misc.h" +#include "adv-simd.h" + +// Uncomment for benchmarking C++ against SSE or NEON. +// Do so in both speck.cpp and speck-simd.cpp. +// #undef CRYPTOPP_SSSE3_AVAILABLE +// #undef CRYPTOPP_SSE41_AVAILABLE +// #undef CRYPTOPP_ARM_NEON_AVAILABLE + +#if (CRYPTOPP_SSSE3_AVAILABLE) +# include +# include +#endif + +#if (CRYPTOPP_SSE41_AVAILABLE) +# include +#endif + +#if defined(__AVX512F__) && defined(__AVX512VL__) +# define CRYPTOPP_AVX512_ROTATE 1 +# include +#endif + +#if (CRYPTOPP_ARM_NEON_AVAILABLE) +# include +#endif + +// Can't use CRYPTOPP_ARM_XXX_AVAILABLE because too many +// compilers don't follow ACLE conventions for the include. +#if defined(CRYPTOPP_ARM_ACLE_AVAILABLE) +# include +# include +#endif + +#if defined(CRYPTOPP_POWER7_AVAILABLE) +# include "ppc-simd.h" +#endif + +// Squash MS LNK4221 and libtool warnings +extern const char SPECK64_SIMD_FNAME[] = __FILE__; + +ANONYMOUS_NAMESPACE_BEGIN + +using CryptoPP::byte; +using CryptoPP::word32; +using CryptoPP::word64; + +// *************************** ARM NEON ************************** // + +#if (CRYPTOPP_ARM_NEON_AVAILABLE) + +template +inline T UnpackHigh32(const T& a, const T& b) +{ + const uint32x2_t x(vget_high_u32((uint32x4_t)a)); + const uint32x2_t y(vget_high_u32((uint32x4_t)b)); + const uint32x2x2_t r = vzip_u32(x, y); + return (T)vcombine_u32(r.val[0], r.val[1]); +} + +template +inline T UnpackLow32(const T& a, const T& b) +{ + const uint32x2_t x(vget_low_u32((uint32x4_t)a)); + const uint32x2_t y(vget_low_u32((uint32x4_t)b)); + const uint32x2x2_t r = vzip_u32(x, y); + return (T)vcombine_u32(r.val[0], r.val[1]); +} + +template +inline uint32x4_t RotateLeft32(const uint32x4_t& val) +{ + const uint32x4_t a(vshlq_n_u32(val, R)); + const uint32x4_t b(vshrq_n_u32(val, 32 - R)); + return vorrq_u32(a, b); +} + +template +inline uint32x4_t RotateRight32(const uint32x4_t& val) +{ + const uint32x4_t a(vshlq_n_u32(val, 32 - R)); + const uint32x4_t b(vshrq_n_u32(val, R)); + return vorrq_u32(a, b); +} + +#if defined(__aarch32__) || defined(__aarch64__) +// Faster than two Shifts and an Or. Thanks to Louis Wingers and Bryan Weeks. +template <> +inline uint32x4_t RotateLeft32<8>(const uint32x4_t& val) +{ +#if defined(CRYPTOPP_BIG_ENDIAN) + const uint8_t maskb[16] = { 14,13,12,15, 10,9,8,11, 6,5,4,7, 2,1,0,3 }; + const uint8x16_t mask = vld1q_u8(maskb); +#else + const uint8_t maskb[16] = { 3,0,1,2, 7,4,5,6, 11,8,9,10, 15,12,13,14 }; + const uint8x16_t mask = vld1q_u8(maskb); +#endif + + return vreinterpretq_u32_u8( + vqtbl1q_u8(vreinterpretq_u8_u32(val), mask)); +} + +// Faster than two Shifts and an Or. Thanks to Louis Wingers and Bryan Weeks. +template <> +inline uint32x4_t RotateRight32<8>(const uint32x4_t& val) +{ +#if defined(CRYPTOPP_BIG_ENDIAN) + const uint8_t maskb[16] = { 12,15,14,13, 8,11,10,9, 4,7,6,5, 0,3,2,1 }; + const uint8x16_t mask = vld1q_u8(maskb); +#else + const uint8_t maskb[16] = { 1,2,3,0, 5,6,7,4, 9,10,11,8, 13,14,15,12 }; + const uint8x16_t mask = vld1q_u8(maskb); +#endif + + return vreinterpretq_u32_u8( + vqtbl1q_u8(vreinterpretq_u8_u32(val), mask)); +} +#endif // Aarch32 or Aarch64 + +inline void SPECK64_Enc_Block(uint32x4_t &block0, uint32x4_t &block1, + const word32 *subkeys, unsigned int rounds) +{ + // [A1 A2 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... + uint32x4_t x1 = vuzpq_u32(block0, block1).val[1]; + uint32x4_t y1 = vuzpq_u32(block0, block1).val[0]; + + for (int i=0; i < static_cast(rounds); ++i) + { + const uint32x4_t rk = vdupq_n_u32(subkeys[i]); + + x1 = RotateRight32<8>(x1); + x1 = vaddq_u32(x1, y1); + x1 = veorq_u32(x1, rk); + y1 = RotateLeft32<3>(y1); + y1 = veorq_u32(y1, x1); + } + + // [A1 A3 B1 B3][A2 A4 B2 B4] => [A1 A2 A3 A4][B1 B2 B3 B4] + block0 = UnpackLow32(y1, x1); + block1 = UnpackHigh32(y1, x1); +} + +inline void SPECK64_Dec_Block(uint32x4_t &block0, uint32x4_t &block1, + const word32 *subkeys, unsigned int rounds) +{ + // [A1 A2 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... + uint32x4_t x1 = vuzpq_u32(block0, block1).val[1]; + uint32x4_t y1 = vuzpq_u32(block0, block1).val[0]; + + for (int i = static_cast(rounds-1); i >= 0; --i) + { + const uint32x4_t rk = vdupq_n_u32(subkeys[i]); + + y1 = veorq_u32(y1, x1); + y1 = RotateRight32<3>(y1); + x1 = veorq_u32(x1, rk); + x1 = vsubq_u32(x1, y1); + x1 = RotateLeft32<8>(x1); + } + + // [A1 A3 B1 B3][A2 A4 B2 B4] => [A1 A2 A3 A4][B1 B2 B3 B4] + block0 = UnpackLow32(y1, x1); + block1 = UnpackHigh32(y1, x1); +} + +inline void SPECK64_Enc_6_Blocks(uint32x4_t &block0, uint32x4_t &block1, + uint32x4_t &block2, uint32x4_t &block3, uint32x4_t &block4, uint32x4_t &block5, + const word32 *subkeys, unsigned int rounds) +{ + // [A1 A2 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... + uint32x4_t x1 = vuzpq_u32(block0, block1).val[1]; + uint32x4_t y1 = vuzpq_u32(block0, block1).val[0]; + uint32x4_t x2 = vuzpq_u32(block2, block3).val[1]; + uint32x4_t y2 = vuzpq_u32(block2, block3).val[0]; + uint32x4_t x3 = vuzpq_u32(block4, block5).val[1]; + uint32x4_t y3 = vuzpq_u32(block4, block5).val[0]; + + for (int i=0; i < static_cast(rounds); ++i) + { + const uint32x4_t rk = vdupq_n_u32(subkeys[i]); + + x1 = RotateRight32<8>(x1); + x2 = RotateRight32<8>(x2); + x3 = RotateRight32<8>(x3); + x1 = vaddq_u32(x1, y1); + x2 = vaddq_u32(x2, y2); + x3 = vaddq_u32(x3, y3); + x1 = veorq_u32(x1, rk); + x2 = veorq_u32(x2, rk); + x3 = veorq_u32(x3, rk); + y1 = RotateLeft32<3>(y1); + y2 = RotateLeft32<3>(y2); + y3 = RotateLeft32<3>(y3); + y1 = veorq_u32(y1, x1); + y2 = veorq_u32(y2, x2); + y3 = veorq_u32(y3, x3); + } + + // [A1 A3 B1 B3][A2 A4 B2 B4] => [A1 A2 A3 A4][B1 B2 B3 B4] + block0 = UnpackLow32(y1, x1); + block1 = UnpackHigh32(y1, x1); + block2 = UnpackLow32(y2, x2); + block3 = UnpackHigh32(y2, x2); + block4 = UnpackLow32(y3, x3); + block5 = UnpackHigh32(y3, x3); +} + +inline void SPECK64_Dec_6_Blocks(uint32x4_t &block0, uint32x4_t &block1, + uint32x4_t &block2, uint32x4_t &block3, uint32x4_t &block4, uint32x4_t &block5, + const word32 *subkeys, unsigned int rounds) +{ + // [A1 A2 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... + uint32x4_t x1 = vuzpq_u32(block0, block1).val[1]; + uint32x4_t y1 = vuzpq_u32(block0, block1).val[0]; + uint32x4_t x2 = vuzpq_u32(block2, block3).val[1]; + uint32x4_t y2 = vuzpq_u32(block2, block3).val[0]; + uint32x4_t x3 = vuzpq_u32(block4, block5).val[1]; + uint32x4_t y3 = vuzpq_u32(block4, block5).val[0]; + + for (int i = static_cast(rounds-1); i >= 0; --i) + { + const uint32x4_t rk = vdupq_n_u32(subkeys[i]); + + y1 = veorq_u32(y1, x1); + y2 = veorq_u32(y2, x2); + y3 = veorq_u32(y3, x3); + y1 = RotateRight32<3>(y1); + y2 = RotateRight32<3>(y2); + y3 = RotateRight32<3>(y3); + x1 = veorq_u32(x1, rk); + x2 = veorq_u32(x2, rk); + x3 = veorq_u32(x3, rk); + x1 = vsubq_u32(x1, y1); + x2 = vsubq_u32(x2, y2); + x3 = vsubq_u32(x3, y3); + x1 = RotateLeft32<8>(x1); + x2 = RotateLeft32<8>(x2); + x3 = RotateLeft32<8>(x3); + } + + // [A1 A3 B1 B3][A2 A4 B2 B4] => [A1 A2 A3 A4][B1 B2 B3 B4] + block0 = UnpackLow32(y1, x1); + block1 = UnpackHigh32(y1, x1); + block2 = UnpackLow32(y2, x2); + block3 = UnpackHigh32(y2, x2); + block4 = UnpackLow32(y3, x3); + block5 = UnpackHigh32(y3, x3); +} + +#endif // CRYPTOPP_ARM_NEON_AVAILABLE + +// ***************************** IA-32 ***************************** // + +#if defined(CRYPTOPP_SSE41_AVAILABLE) + +template +inline __m128i RotateLeft32(const __m128i& val) +{ + return _mm_or_si128( + _mm_slli_epi32(val, R), _mm_srli_epi32(val, 32-R)); +} + +template +inline __m128i RotateRight32(const __m128i& val) +{ + return _mm_or_si128( + _mm_slli_epi32(val, 32-R), _mm_srli_epi32(val, R)); +} + +// Faster than two Shifts and an Or. Thanks to Louis Wingers and Bryan Weeks. +template <> +inline __m128i RotateLeft32<8>(const __m128i& val) +{ + const __m128i mask = _mm_set_epi8(14,13,12,15, 10,9,8,11, 6,5,4,7, 2,1,0,3); + return _mm_shuffle_epi8(val, mask); +} + +// Faster than two Shifts and an Or. Thanks to Louis Wingers and Bryan Weeks. +template <> +inline __m128i RotateRight32<8>(const __m128i& val) +{ + const __m128i mask = _mm_set_epi8(12,15,14,13, 8,11,10,9, 4,7,6,5, 0,3,2,1); + return _mm_shuffle_epi8(val, mask); +} + +inline void SPECK64_Enc_Block(__m128i &block0, __m128i &block1, + const word32 *subkeys, unsigned int rounds) +{ + // [A1 A2 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... + const __m128 t0 = _mm_castsi128_ps(block0); + const __m128 t1 = _mm_castsi128_ps(block1); + __m128i x1 = _mm_castps_si128(_mm_shuffle_ps(t0, t1, _MM_SHUFFLE(3,1,3,1))); + __m128i y1 = _mm_castps_si128(_mm_shuffle_ps(t0, t1, _MM_SHUFFLE(2,0,2,0))); + + for (int i=0; i < static_cast(rounds); ++i) + { + const __m128i rk = _mm_set1_epi32(subkeys[i]); + + x1 = RotateRight32<8>(x1); + x1 = _mm_add_epi32(x1, y1); + x1 = _mm_xor_si128(x1, rk); + y1 = RotateLeft32<3>(y1); + y1 = _mm_xor_si128(y1, x1); + } + + // The is roughly the SSE equivalent to ARM vzp32 + // [A1 A3 B1 B3][A2 A4 B2 B4] => [A1 A2 A3 A4][B1 B2 B3 B4] + block0 = _mm_unpacklo_epi32(y1, x1); + block1 = _mm_unpackhi_epi32(y1, x1); +} + +inline void SPECK64_Dec_Block(__m128i &block0, __m128i &block1, + const word32 *subkeys, unsigned int rounds) +{ + // [A1 A2 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... + const __m128 t0 = _mm_castsi128_ps(block0); + const __m128 t1 = _mm_castsi128_ps(block1); + __m128i x1 = _mm_castps_si128(_mm_shuffle_ps(t0, t1, _MM_SHUFFLE(3,1,3,1))); + __m128i y1 = _mm_castps_si128(_mm_shuffle_ps(t0, t1, _MM_SHUFFLE(2,0,2,0))); + + for (int i = static_cast(rounds-1); i >= 0; --i) + { + const __m128i rk = _mm_set1_epi32(subkeys[i]); + + y1 = _mm_xor_si128(y1, x1); + y1 = RotateRight32<3>(y1); + x1 = _mm_xor_si128(x1, rk); + x1 = _mm_sub_epi32(x1, y1); + x1 = RotateLeft32<8>(x1); + } + + // The is roughly the SSE equivalent to ARM vzp32 + // [A1 A3 B1 B3][A2 A4 B2 B4] => [A1 A2 A3 A4][B1 B2 B3 B4] + block0 = _mm_unpacklo_epi32(y1, x1); + block1 = _mm_unpackhi_epi32(y1, x1); +} + +inline void SPECK64_Enc_6_Blocks(__m128i &block0, __m128i &block1, + __m128i &block2, __m128i &block3, __m128i &block4, __m128i &block5, + const word32 *subkeys, unsigned int rounds) +{ + // [A1 A2 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... + const __m128 t0 = _mm_castsi128_ps(block0); + const __m128 t1 = _mm_castsi128_ps(block1); + __m128i x1 = _mm_castps_si128(_mm_shuffle_ps(t0, t1, _MM_SHUFFLE(3,1,3,1))); + __m128i y1 = _mm_castps_si128(_mm_shuffle_ps(t0, t1, _MM_SHUFFLE(2,0,2,0))); + + const __m128 t2 = _mm_castsi128_ps(block2); + const __m128 t3 = _mm_castsi128_ps(block3); + __m128i x2 = _mm_castps_si128(_mm_shuffle_ps(t2, t3, _MM_SHUFFLE(3,1,3,1))); + __m128i y2 = _mm_castps_si128(_mm_shuffle_ps(t2, t3, _MM_SHUFFLE(2,0,2,0))); + + const __m128 t4 = _mm_castsi128_ps(block4); + const __m128 t5 = _mm_castsi128_ps(block5); + __m128i x3 = _mm_castps_si128(_mm_shuffle_ps(t4, t5, _MM_SHUFFLE(3,1,3,1))); + __m128i y3 = _mm_castps_si128(_mm_shuffle_ps(t4, t5, _MM_SHUFFLE(2,0,2,0))); + + for (int i=0; i < static_cast(rounds); ++i) + { + const __m128i rk = _mm_set1_epi32(subkeys[i]); + + x1 = RotateRight32<8>(x1); + x2 = RotateRight32<8>(x2); + x3 = RotateRight32<8>(x3); + x1 = _mm_add_epi32(x1, y1); + x2 = _mm_add_epi32(x2, y2); + x3 = _mm_add_epi32(x3, y3); + x1 = _mm_xor_si128(x1, rk); + x2 = _mm_xor_si128(x2, rk); + x3 = _mm_xor_si128(x3, rk); + y1 = RotateLeft32<3>(y1); + y2 = RotateLeft32<3>(y2); + y3 = RotateLeft32<3>(y3); + y1 = _mm_xor_si128(y1, x1); + y2 = _mm_xor_si128(y2, x2); + y3 = _mm_xor_si128(y3, x3); + } + + // The is roughly the SSE equivalent to ARM vzp32 + // [A1 A3 B1 B3][A2 A4 B2 B4] => [A1 A2 A3 A4][B1 B2 B3 B4] + block0 = _mm_unpacklo_epi32(y1, x1); + block1 = _mm_unpackhi_epi32(y1, x1); + block2 = _mm_unpacklo_epi32(y2, x2); + block3 = _mm_unpackhi_epi32(y2, x2); + block4 = _mm_unpacklo_epi32(y3, x3); + block5 = _mm_unpackhi_epi32(y3, x3); +} + +inline void SPECK64_Dec_6_Blocks(__m128i &block0, __m128i &block1, + __m128i &block2, __m128i &block3, __m128i &block4, __m128i &block5, + const word32 *subkeys, unsigned int rounds) +{ + // [A1 A2 A3 A4][B1 B2 B3 B4] ... => [A1 A3 B1 B3][A2 A4 B2 B4] ... + const __m128 t0 = _mm_castsi128_ps(block0); + const __m128 t1 = _mm_castsi128_ps(block1); + __m128i x1 = _mm_castps_si128(_mm_shuffle_ps(t0, t1, _MM_SHUFFLE(3,1,3,1))); + __m128i y1 = _mm_castps_si128(_mm_shuffle_ps(t0, t1, _MM_SHUFFLE(2,0,2,0))); + + const __m128 t2 = _mm_castsi128_ps(block2); + const __m128 t3 = _mm_castsi128_ps(block3); + __m128i x2 = _mm_castps_si128(_mm_shuffle_ps(t2, t3, _MM_SHUFFLE(3,1,3,1))); + __m128i y2 = _mm_castps_si128(_mm_shuffle_ps(t2, t3, _MM_SHUFFLE(2,0,2,0))); + + const __m128 t4 = _mm_castsi128_ps(block4); + const __m128 t5 = _mm_castsi128_ps(block5); + __m128i x3 = _mm_castps_si128(_mm_shuffle_ps(t4, t5, _MM_SHUFFLE(3,1,3,1))); + __m128i y3 = _mm_castps_si128(_mm_shuffle_ps(t4, t5, _MM_SHUFFLE(2,0,2,0))); + + for (int i = static_cast(rounds-1); i >= 0; --i) + { + const __m128i rk = _mm_set1_epi32(subkeys[i]); + + y1 = _mm_xor_si128(y1, x1); + y2 = _mm_xor_si128(y2, x2); + y3 = _mm_xor_si128(y3, x3); + y1 = RotateRight32<3>(y1); + y2 = RotateRight32<3>(y2); + y3 = RotateRight32<3>(y3); + x1 = _mm_xor_si128(x1, rk); + x2 = _mm_xor_si128(x2, rk); + x3 = _mm_xor_si128(x3, rk); + x1 = _mm_sub_epi32(x1, y1); + x2 = _mm_sub_epi32(x2, y2); + x3 = _mm_sub_epi32(x3, y3); + x1 = RotateLeft32<8>(x1); + x2 = RotateLeft32<8>(x2); + x3 = RotateLeft32<8>(x3); + } + + // The is roughly the SSE equivalent to ARM vzp32 + // [A1 A3 B1 B3][A2 A4 B2 B4] => [A1 A2 A3 A4][B1 B2 B3 B4] + block0 = _mm_unpacklo_epi32(y1, x1); + block1 = _mm_unpackhi_epi32(y1, x1); + block2 = _mm_unpacklo_epi32(y2, x2); + block3 = _mm_unpackhi_epi32(y2, x2); + block4 = _mm_unpacklo_epi32(y3, x3); + block5 = _mm_unpackhi_epi32(y3, x3); +} + +#endif // CRYPTOPP_SSE41_AVAILABLE + +// ***************************** Power7 ***************************** // + +#if defined(CRYPTOPP_POWER7_AVAILABLE) +using CryptoPP::uint8x16_p; +using CryptoPP::uint32x4_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 // CRYPTOPP_POWER7_AVAILABLE + +ANONYMOUS_NAMESPACE_END + +/////////////////////////////////////////////////////////////////////// + +NAMESPACE_BEGIN(CryptoPP) + +// *************************** ARM NEON **************************** // + +#if (CRYPTOPP_ARM_NEON_AVAILABLE) +size_t SPECK64_Enc_AdvancedProcessBlocks_NEON(const word32* subKeys, size_t rounds, + const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) +{ + return AdvancedProcessBlocks64_6x2_NEON(SPECK64_Enc_Block, SPECK64_Enc_6_Blocks, + subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags); +} + +size_t SPECK64_Dec_AdvancedProcessBlocks_NEON(const word32* subKeys, size_t rounds, + const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) +{ + return AdvancedProcessBlocks64_6x2_NEON(SPECK64_Dec_Block, SPECK64_Dec_6_Blocks, + subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags); +} +#endif + +// ***************************** IA-32 ***************************** // + +#if defined(CRYPTOPP_SSE41_AVAILABLE) +size_t SPECK64_Enc_AdvancedProcessBlocks_SSE41(const word32* subKeys, size_t rounds, + const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) +{ + return AdvancedProcessBlocks64_6x2_SSE(SPECK64_Enc_Block, SPECK64_Enc_6_Blocks, + subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags); +} + +size_t SPECK64_Dec_AdvancedProcessBlocks_SSE41(const word32* subKeys, size_t rounds, + const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) +{ + return AdvancedProcessBlocks64_6x2_SSE(SPECK64_Dec_Block, SPECK64_Dec_6_Blocks, + subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags); +} +#endif + +// ***************************** Power7 ***************************** // + +#if defined(CRYPTOPP_POWER7_AVAILABLE) +size_t SPECK64_Enc_AdvancedProcessBlocks_POWER7(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_POWER7(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); +} +#endif + +NAMESPACE_END -- cgit v1.2.1