Path: blob/a-new-beginning/SharedDependencies/Sources/cryptopp/blake2s_simd.cpp
2 views
// blake2_simd.cpp - written and placed in the public domain by1// Samuel Neves, Jeffrey Walton, Uri Blumenthal2// and Marcel Raad.3//4// This source file uses intrinsics to gain access to ARMv7a/ARMv8a5// NEON, Power7 and SSE4.1 instructions. A separate source file is6// needed because additional CXXFLAGS are required to enable the7// appropriate instructions sets in some build configurations.89// The BLAKE2b and BLAKE2s numbers are consistent with the BLAKE2 team's10// numbers. However, we have an Altivec implementation of BLAKE2s,11// and a POWER8 implementation of BLAKE2b (BLAKE2 team is missing them).12// Altivec code is about 2x faster than C++ when using GCC 5.0 or13// above. The POWER8 code is about 2.5x faster than C++ when using GCC 5.014// or above. If you use GCC 4.0 (PowerMac) or GCC 4.8 (GCC Compile Farm)15// then the PowerPC code will be slower than C++. Be sure to use GCC 5.016// or above for PowerPC builds or disable Altivec for BLAKE2b and BLAKE2s17// if using the old compilers.1819#include "pch.h"20#include "config.h"21#include "misc.h"22#include "blake2.h"2324// Uncomment for benchmarking C++ against SSE2 or NEON.25// Do so in both blake2.cpp and blake2_simd.cpp.26// #undef CRYPTOPP_SSE41_AVAILABLE27// #undef CRYPTOPP_ARM_NEON_AVAILABLE28// #undef CRYPTOPP_ALTIVEC_AVAILABLE2930// Disable NEON/ASIMD for Cortex-A53 and A57. The shifts are too slow and C/C++ is about31// 3 cpb faster than NEON/ASIMD. Also see http://github.com/weidai11/cryptopp/issues/367.32#if (defined(__aarch32__) || defined(__aarch64__)) && defined(CRYPTOPP_SLOW_ARMV8_SHIFT)33# undef CRYPTOPP_ARM_NEON_AVAILABLE34#endif3536// BLAKE2s bug on AIX 7.1 (POWER7) with XLC 12.0137// https://github.com/weidai11/cryptopp/issues/74338#if defined(__xlC__) && (__xlC__ < 0x0d01)39# define CRYPTOPP_DISABLE_ALTIVEC 140# undef CRYPTOPP_POWER7_AVAILABLE41# undef CRYPTOPP_ALTIVEC_AVAILABLE42#endif4344#if defined(__XOP__)45# if defined(CRYPTOPP_GCC_COMPATIBLE)46# include <x86intrin.h>47# endif48# include <ammintrin.h>49#endif // XOP5051#if (CRYPTOPP_SSE41_AVAILABLE)52# include <emmintrin.h>53# include <tmmintrin.h>54# include <smmintrin.h>55#endif5657#if (CRYPTOPP_ARM_NEON_HEADER)58# include <arm_neon.h>59#endif6061#if (CRYPTOPP_ARM_ACLE_HEADER)62# include <stdint.h>63# include <arm_acle.h>64#endif6566#if (CRYPTOPP_ALTIVEC_AVAILABLE)67# include "ppc_simd.h"68#endif6970#if defined(CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE)71/* Ignore "warning: vec_lvsl is deprecated..." */72# pragma GCC diagnostic ignored "-Wdeprecated"73#endif7475// Squash MS LNK4221 and libtool warnings76extern const char BLAKE2S_SIMD_FNAME[] = __FILE__;7778NAMESPACE_BEGIN(CryptoPP)7980// Exported by blake2.cpp81extern const word32 BLAKE2S_IV[8];82extern const word64 BLAKE2B_IV[8];8384#if CRYPTOPP_SSE41_AVAILABLE8586#define LOADU(p) _mm_loadu_si128((const __m128i *)(const void*)(p))87#define STOREU(p,r) _mm_storeu_si128((__m128i *)(void*)(p), r)88#define TOF(reg) _mm_castsi128_ps((reg))89#define TOI(reg) _mm_castps_si128((reg))9091void BLAKE2_Compress32_SSE4(const byte* input, BLAKE2s_State& state)92{93#define BLAKE2S_LOAD_MSG_0_1(buf) \94buf = TOI(_mm_shuffle_ps(TOF(m0), TOF(m1), _MM_SHUFFLE(2,0,2,0)));9596#define BLAKE2S_LOAD_MSG_0_2(buf) \97buf = TOI(_mm_shuffle_ps(TOF(m0), TOF(m1), _MM_SHUFFLE(3,1,3,1)));9899#define BLAKE2S_LOAD_MSG_0_3(buf) \100t0 = _mm_shuffle_epi32(m2, _MM_SHUFFLE(3,2,0,1)); \101t1 = _mm_shuffle_epi32(m3, _MM_SHUFFLE(0,1,3,2)); \102buf = _mm_blend_epi16(t0, t1, 0xC3);103104#define BLAKE2S_LOAD_MSG_0_4(buf) \105t0 = _mm_blend_epi16(t0, t1, 0x3C); \106buf = _mm_shuffle_epi32(t0, _MM_SHUFFLE(2,3,0,1));107108#define BLAKE2S_LOAD_MSG_1_1(buf) \109t0 = _mm_blend_epi16(m1, m2, 0x0C); \110t1 = _mm_slli_si128(m3, 4); \111t2 = _mm_blend_epi16(t0, t1, 0xF0); \112buf = _mm_shuffle_epi32(t2, _MM_SHUFFLE(2,1,0,3));113114#define BLAKE2S_LOAD_MSG_1_2(buf) \115t0 = _mm_shuffle_epi32(m2,_MM_SHUFFLE(0,0,2,0)); \116t1 = _mm_blend_epi16(m1,m3,0xC0); \117t2 = _mm_blend_epi16(t0, t1, 0xF0); \118buf = _mm_shuffle_epi32(t2, _MM_SHUFFLE(2,3,0,1));119120#define BLAKE2S_LOAD_MSG_1_3(buf) \121t0 = _mm_slli_si128(m1, 4); \122t1 = _mm_blend_epi16(m2, t0, 0x30); \123t2 = _mm_blend_epi16(m0, t1, 0xF0); \124buf = _mm_shuffle_epi32(t2, _MM_SHUFFLE(3,0,1,2));125126#define BLAKE2S_LOAD_MSG_1_4(buf) \127t0 = _mm_unpackhi_epi32(m0,m1); \128t1 = _mm_slli_si128(m3, 4); \129t2 = _mm_blend_epi16(t0, t1, 0x0C); \130buf = _mm_shuffle_epi32(t2, _MM_SHUFFLE(3,0,1,2));131132#define BLAKE2S_LOAD_MSG_2_1(buf) \133t0 = _mm_unpackhi_epi32(m2,m3); \134t1 = _mm_blend_epi16(m3,m1,0x0C); \135t2 = _mm_blend_epi16(t0, t1, 0x0F); \136buf = _mm_shuffle_epi32(t2, _MM_SHUFFLE(3,1,0,2));137138#define BLAKE2S_LOAD_MSG_2_2(buf) \139t0 = _mm_unpacklo_epi32(m2,m0); \140t1 = _mm_blend_epi16(t0, m0, 0xF0); \141t2 = _mm_slli_si128(m3, 8); \142buf = _mm_blend_epi16(t1, t2, 0xC0);143144#define BLAKE2S_LOAD_MSG_2_3(buf) \145t0 = _mm_blend_epi16(m0, m2, 0x3C); \146t1 = _mm_srli_si128(m1, 12); \147t2 = _mm_blend_epi16(t0,t1,0x03); \148buf = _mm_shuffle_epi32(t2, _MM_SHUFFLE(0,3,2,1));149150#define BLAKE2S_LOAD_MSG_2_4(buf) \151t0 = _mm_slli_si128(m3, 4); \152t1 = _mm_blend_epi16(m0, m1, 0x33); \153t2 = _mm_blend_epi16(t1, t0, 0xC0); \154buf = _mm_shuffle_epi32(t2, _MM_SHUFFLE(1,2,3,0));155156#define BLAKE2S_LOAD_MSG_3_1(buf) \157t0 = _mm_unpackhi_epi32(m0,m1); \158t1 = _mm_unpackhi_epi32(t0, m2); \159t2 = _mm_blend_epi16(t1, m3, 0x0C); \160buf = _mm_shuffle_epi32(t2, _MM_SHUFFLE(3,1,0,2));161162#define BLAKE2S_LOAD_MSG_3_2(buf) \163t0 = _mm_slli_si128(m2, 8); \164t1 = _mm_blend_epi16(m3,m0,0x0C); \165t2 = _mm_blend_epi16(t1, t0, 0xC0); \166buf = _mm_shuffle_epi32(t2, _MM_SHUFFLE(2,0,1,3));167168#define BLAKE2S_LOAD_MSG_3_3(buf) \169t0 = _mm_blend_epi16(m0,m1,0x0F); \170t1 = _mm_blend_epi16(t0, m3, 0xC0); \171buf = _mm_shuffle_epi32(t1, _MM_SHUFFLE(0,1,2,3));172173#define BLAKE2S_LOAD_MSG_3_4(buf) \174t0 = _mm_alignr_epi8(m0, m1, 4); \175buf = _mm_blend_epi16(t0, m2, 0x33);176177#define BLAKE2S_LOAD_MSG_4_1(buf) \178t0 = _mm_unpacklo_epi64(m1,m2); \179t1 = _mm_unpackhi_epi64(m0,m2); \180t2 = _mm_blend_epi16(t0,t1,0x33); \181buf = _mm_shuffle_epi32(t2, _MM_SHUFFLE(2,0,1,3));182183#define BLAKE2S_LOAD_MSG_4_2(buf) \184t0 = _mm_unpackhi_epi64(m1,m3); \185t1 = _mm_unpacklo_epi64(m0,m1); \186buf = _mm_blend_epi16(t0,t1,0x33);187188#define BLAKE2S_LOAD_MSG_4_3(buf) \189t0 = _mm_unpackhi_epi64(m3,m1); \190t1 = _mm_unpackhi_epi64(m2,m0); \191t2 = _mm_blend_epi16(t1,t0,0x33); \192buf = _mm_shuffle_epi32(t2, _MM_SHUFFLE(2,1,0,3));193194#define BLAKE2S_LOAD_MSG_4_4(buf) \195t0 = _mm_blend_epi16(m0,m2,0x03); \196t1 = _mm_slli_si128(t0, 8); \197t2 = _mm_blend_epi16(t1,m3,0x0F); \198buf = _mm_shuffle_epi32(t2, _MM_SHUFFLE(2,0,3,1));199200#define BLAKE2S_LOAD_MSG_5_1(buf) \201t0 = _mm_unpackhi_epi32(m0,m1); \202t1 = _mm_unpacklo_epi32(m0,m2); \203buf = _mm_unpacklo_epi64(t0,t1);204205#define BLAKE2S_LOAD_MSG_5_2(buf) \206t0 = _mm_srli_si128(m2, 4); \207t1 = _mm_blend_epi16(m0,m3,0x03); \208buf = _mm_blend_epi16(t1,t0,0x3C);209210#define BLAKE2S_LOAD_MSG_5_3(buf) \211t0 = _mm_blend_epi16(m1,m0,0x0C); \212t1 = _mm_srli_si128(m3, 4); \213t2 = _mm_blend_epi16(t0,t1,0x30); \214buf = _mm_shuffle_epi32(t2, _MM_SHUFFLE(2,3,0,1));215216#define BLAKE2S_LOAD_MSG_5_4(buf) \217t0 = _mm_unpacklo_epi64(m2,m1); \218t1 = _mm_shuffle_epi32(m3, _MM_SHUFFLE(2,0,1,0)); \219t2 = _mm_srli_si128(t0, 4); \220buf = _mm_blend_epi16(t1,t2,0x33);221222#define BLAKE2S_LOAD_MSG_6_1(buf) \223t0 = _mm_slli_si128(m1, 12); \224t1 = _mm_blend_epi16(m0,m3,0x33); \225buf = _mm_blend_epi16(t1,t0,0xC0);226227#define BLAKE2S_LOAD_MSG_6_2(buf) \228t0 = _mm_blend_epi16(m3,m2,0x30); \229t1 = _mm_srli_si128(m1, 4); \230t2 = _mm_blend_epi16(t0,t1,0x03); \231buf = _mm_shuffle_epi32(t2, _MM_SHUFFLE(2,1,3,0));232233#define BLAKE2S_LOAD_MSG_6_3(buf) \234t0 = _mm_unpacklo_epi64(m0,m2); \235t1 = _mm_srli_si128(m1, 4); \236t2 = _mm_blend_epi16(t0,t1,0x0C); \237buf = _mm_shuffle_epi32(t2, _MM_SHUFFLE(3,1,0,2));238239#define BLAKE2S_LOAD_MSG_6_4(buf) \240t0 = _mm_unpackhi_epi32(m1,m2); \241t1 = _mm_unpackhi_epi64(m0,t0); \242buf = _mm_shuffle_epi32(t1, _MM_SHUFFLE(0,1,2,3));243244#define BLAKE2S_LOAD_MSG_7_1(buf) \245t0 = _mm_unpackhi_epi32(m0,m1); \246t1 = _mm_blend_epi16(t0,m3,0x0F); \247buf = _mm_shuffle_epi32(t1,_MM_SHUFFLE(2,0,3,1));248249#define BLAKE2S_LOAD_MSG_7_2(buf) \250t0 = _mm_blend_epi16(m2,m3,0x30); \251t1 = _mm_srli_si128(m0,4); \252t2 = _mm_blend_epi16(t0,t1,0x03); \253buf = _mm_shuffle_epi32(t2, _MM_SHUFFLE(1,0,2,3));254255#define BLAKE2S_LOAD_MSG_7_3(buf) \256t0 = _mm_unpackhi_epi64(m0,m3); \257t1 = _mm_unpacklo_epi64(m1,m2); \258t2 = _mm_blend_epi16(t0,t1,0x3C); \259buf = _mm_shuffle_epi32(t2,_MM_SHUFFLE(2,3,1,0));260261#define BLAKE2S_LOAD_MSG_7_4(buf) \262t0 = _mm_unpacklo_epi32(m0,m1); \263t1 = _mm_unpackhi_epi32(m1,m2); \264t2 = _mm_unpacklo_epi64(t0,t1); \265buf = _mm_shuffle_epi32(t2, _MM_SHUFFLE(2,1,0,3));266267#define BLAKE2S_LOAD_MSG_8_1(buf) \268t0 = _mm_unpackhi_epi32(m1,m3); \269t1 = _mm_unpacklo_epi64(t0,m0); \270t2 = _mm_blend_epi16(t1,m2,0xC0); \271buf = _mm_shufflehi_epi16(t2,_MM_SHUFFLE(1,0,3,2));272273#define BLAKE2S_LOAD_MSG_8_2(buf) \274t0 = _mm_unpackhi_epi32(m0,m3); \275t1 = _mm_blend_epi16(m2,t0,0xF0); \276buf = _mm_shuffle_epi32(t1,_MM_SHUFFLE(0,2,1,3));277278#define BLAKE2S_LOAD_MSG_8_3(buf) \279t0 = _mm_unpacklo_epi64(m0,m3); \280t1 = _mm_srli_si128(m2,8); \281t2 = _mm_blend_epi16(t0,t1,0x03); \282buf = _mm_shuffle_epi32(t2, _MM_SHUFFLE(1,3,2,0));283284#define BLAKE2S_LOAD_MSG_8_4(buf) \285t0 = _mm_blend_epi16(m1,m0,0x30); \286buf = _mm_shuffle_epi32(t0,_MM_SHUFFLE(0,3,2,1));287288#define BLAKE2S_LOAD_MSG_9_1(buf) \289t0 = _mm_blend_epi16(m0,m2,0x03); \290t1 = _mm_blend_epi16(m1,m2,0x30); \291t2 = _mm_blend_epi16(t1,t0,0x0F); \292buf = _mm_shuffle_epi32(t2,_MM_SHUFFLE(1,3,0,2));293294#define BLAKE2S_LOAD_MSG_9_2(buf) \295t0 = _mm_slli_si128(m0,4); \296t1 = _mm_blend_epi16(m1,t0,0xC0); \297buf = _mm_shuffle_epi32(t1,_MM_SHUFFLE(1,2,0,3));298299#define BLAKE2S_LOAD_MSG_9_3(buf) \300t0 = _mm_unpackhi_epi32(m0,m3); \301t1 = _mm_unpacklo_epi32(m2,m3); \302t2 = _mm_unpackhi_epi64(t0,t1); \303buf = _mm_shuffle_epi32(t2,_MM_SHUFFLE(0,2,1,3));304305#define BLAKE2S_LOAD_MSG_9_4(buf) \306t0 = _mm_blend_epi16(m3,m2,0xC0); \307t1 = _mm_unpacklo_epi32(m0,m3); \308t2 = _mm_blend_epi16(t0,t1,0x0F); \309buf = _mm_shuffle_epi32(t2,_MM_SHUFFLE(1,2,3,0));310311#ifdef __XOP__312# define MM_ROTI_EPI32(r, c) \313_mm_roti_epi32(r, c)314#else315# define MM_ROTI_EPI32(r, c) ( \316(8==-(c)) ? _mm_shuffle_epi8(r,r8) \317: (16==-(c)) ? _mm_shuffle_epi8(r,r16) \318: _mm_xor_si128(_mm_srli_epi32((r), -(c)), \319_mm_slli_epi32((r), 32-(-(c)))))320#endif321322#define BLAKE2S_G1(row1,row2,row3,row4,buf) \323row1 = _mm_add_epi32( _mm_add_epi32( row1, buf), row2 ); \324row4 = _mm_xor_si128( row4, row1 ); \325row4 = MM_ROTI_EPI32(row4, -16); \326row3 = _mm_add_epi32( row3, row4 ); \327row2 = _mm_xor_si128( row2, row3 ); \328row2 = MM_ROTI_EPI32(row2, -12);329330#define BLAKE2S_G2(row1,row2,row3,row4,buf) \331row1 = _mm_add_epi32( _mm_add_epi32( row1, buf), row2 ); \332row4 = _mm_xor_si128( row4, row1 ); \333row4 = MM_ROTI_EPI32(row4, -8); \334row3 = _mm_add_epi32( row3, row4 ); \335row2 = _mm_xor_si128( row2, row3 ); \336row2 = MM_ROTI_EPI32(row2, -7);337338#define DIAGONALIZE(row1,row2,row3,row4) \339row1 = _mm_shuffle_epi32( row1, _MM_SHUFFLE(2,1,0,3) ); \340row4 = _mm_shuffle_epi32( row4, _MM_SHUFFLE(1,0,3,2) ); \341row3 = _mm_shuffle_epi32( row3, _MM_SHUFFLE(0,3,2,1) );342343#define UNDIAGONALIZE(row1,row2,row3,row4) \344row1 = _mm_shuffle_epi32( row1, _MM_SHUFFLE(0,3,2,1) ); \345row4 = _mm_shuffle_epi32( row4, _MM_SHUFFLE(1,0,3,2) ); \346row3 = _mm_shuffle_epi32( row3, _MM_SHUFFLE(2,1,0,3) );347348#define BLAKE2S_ROUND(r) \349BLAKE2S_LOAD_MSG_ ##r ##_1(buf1); \350BLAKE2S_G1(row1,row2,row3,row4,buf1); \351BLAKE2S_LOAD_MSG_ ##r ##_2(buf2); \352BLAKE2S_G2(row1,row2,row3,row4,buf2); \353DIAGONALIZE(row1,row2,row3,row4); \354BLAKE2S_LOAD_MSG_ ##r ##_3(buf3); \355BLAKE2S_G1(row1,row2,row3,row4,buf3); \356BLAKE2S_LOAD_MSG_ ##r ##_4(buf4); \357BLAKE2S_G2(row1,row2,row3,row4,buf4); \358UNDIAGONALIZE(row1,row2,row3,row4);359360__m128i row1, row2, row3, row4;361__m128i buf1, buf2, buf3, buf4;362__m128i t0, t1, t2, ff0, ff1;363364const __m128i r8 = _mm_set_epi8(12, 15, 14, 13, 8, 11, 10, 9, 4, 7, 6, 5, 0, 3, 2, 1);365const __m128i r16 = _mm_set_epi8(13, 12, 15, 14, 9, 8, 11, 10, 5, 4, 7, 6, 1, 0, 3, 2);366367const __m128i m0 = LOADU(input + 00);368const __m128i m1 = LOADU(input + 16);369const __m128i m2 = LOADU(input + 32);370const __m128i m3 = LOADU(input + 48);371372row1 = ff0 = LOADU(state.h()+0);373row2 = ff1 = LOADU(state.h()+4);374row3 = LOADU(BLAKE2S_IV+0);375row4 = _mm_xor_si128(LOADU(BLAKE2S_IV+4), LOADU(state.t()+0));376377BLAKE2S_ROUND(0);378BLAKE2S_ROUND(1);379BLAKE2S_ROUND(2);380BLAKE2S_ROUND(3);381BLAKE2S_ROUND(4);382BLAKE2S_ROUND(5);383BLAKE2S_ROUND(6);384BLAKE2S_ROUND(7);385BLAKE2S_ROUND(8);386BLAKE2S_ROUND(9);387388STOREU(state.h()+0, _mm_xor_si128(ff0, _mm_xor_si128(row1, row3)));389STOREU(state.h()+4, _mm_xor_si128(ff1, _mm_xor_si128(row2, row4)));390}391#endif // CRYPTOPP_SSE41_AVAILABLE392393#if CRYPTOPP_ARM_NEON_AVAILABLE394void BLAKE2_Compress32_NEON(const byte* input, BLAKE2s_State& state)395{396#define BLAKE2S_LOAD_MSG_0_1(buf) \397do { uint32x2_t t0, t1; \398t0 = vzip_u32(vget_low_u32(m0), vget_high_u32(m0)).val[0]; \399t1 = vzip_u32(vget_low_u32(m1), vget_high_u32(m1)).val[0]; \400buf = vcombine_u32(t0, t1); } while(0)401402#define BLAKE2S_LOAD_MSG_0_2(buf) \403do { uint32x2_t t0, t1; \404t0 = vzip_u32(vget_low_u32(m0), vget_high_u32(m0)).val[1]; \405t1 = vzip_u32(vget_low_u32(m1), vget_high_u32(m1)).val[1]; \406buf = vcombine_u32(t0, t1); } while(0)407408#define BLAKE2S_LOAD_MSG_0_3(buf) \409do { uint32x2_t t0, t1; \410t0 = vzip_u32(vget_low_u32(m2), vget_high_u32(m2)).val[0]; \411t1 = vzip_u32(vget_low_u32(m3), vget_high_u32(m3)).val[0]; \412buf = vcombine_u32(t0, t1); } while(0)413414#define BLAKE2S_LOAD_MSG_0_4(buf) \415do { uint32x2_t t0, t1; \416t0 = vzip_u32(vget_low_u32(m2), vget_high_u32(m2)).val[1]; \417t1 = vzip_u32(vget_low_u32(m3), vget_high_u32(m3)).val[1]; \418buf = vcombine_u32(t0, t1); } while(0)419420#define BLAKE2S_LOAD_MSG_1_1(buf) \421do { uint32x2_t t0, t1; \422t0 = vzip_u32(vget_high_u32(m3), vget_low_u32(m1)).val[0]; \423t1 = vzip_u32(vget_low_u32(m2), vget_low_u32(m3)).val[1]; \424buf = vcombine_u32(t0, t1); } while(0)425426#define BLAKE2S_LOAD_MSG_1_2(buf) \427do { uint32x2_t t0, t1; \428t0 = vzip_u32(vget_high_u32(m2), vget_low_u32(m2)).val[0]; \429t1 = vext_u32(vget_high_u32(m3), vget_high_u32(m1), 1); \430buf = vcombine_u32(t0, t1); } while(0)431432#define BLAKE2S_LOAD_MSG_1_3(buf) \433do { uint32x2_t t0, t1; \434t0 = vext_u32(vget_low_u32(m0), vget_low_u32(m0), 1); \435t1 = vzip_u32(vget_high_u32(m2), vget_low_u32(m1)).val[1]; \436buf = vcombine_u32(t0, t1); } while(0)437438#define BLAKE2S_LOAD_MSG_1_4(buf) \439do { uint32x2_t t0, t1; \440t0 = vzip_u32(vget_low_u32(m3), vget_high_u32(m0)).val[0]; \441t1 = vzip_u32(vget_high_u32(m1), vget_high_u32(m0)).val[1]; \442buf = vcombine_u32(t0, t1); } while(0)443444#define BLAKE2S_LOAD_MSG_2_1(buf) \445do { uint32x2_t t0, t1; \446t0 = vext_u32(vget_high_u32(m2), vget_low_u32(m3), 1); \447t1 = vzip_u32(vget_low_u32(m1), vget_high_u32(m3)).val[1]; \448buf = vcombine_u32(t0, t1); } while(0)449450#define BLAKE2S_LOAD_MSG_2_2(buf) \451do { uint32x2_t t0, t1; \452t0 = vzip_u32(vget_low_u32(m2), vget_low_u32(m0)).val[0]; \453t1 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_high_u32(m0), vget_low_u32(m3)); \454buf = vcombine_u32(t0, t1); } while(0)455456#define BLAKE2S_LOAD_MSG_2_3(buf) \457do { uint32x2_t t0, t1; \458t0 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_high_u32(m2), vget_high_u32(m0)); \459t1 = vzip_u32(vget_high_u32(m1), vget_low_u32(m2)).val[1]; \460buf = vcombine_u32(t0, t1); } while(0)461462#define BLAKE2S_LOAD_MSG_2_4(buf) \463do { uint32x2_t t0, t1; \464t0 = vzip_u32(vget_high_u32(m3), vget_high_u32(m1)).val[0]; \465t1 = vext_u32(vget_low_u32(m0), vget_low_u32(m1), 1); \466buf = vcombine_u32(t0, t1); } while(0)467468#define BLAKE2S_LOAD_MSG_3_1(buf) \469do { uint32x2_t t0, t1; \470t0 = vzip_u32(vget_high_u32(m1), vget_high_u32(m0)).val[1]; \471t1 = vzip_u32(vget_low_u32(m3), vget_high_u32(m2)).val[1]; \472buf = vcombine_u32(t0, t1); } while(0)473474#define BLAKE2S_LOAD_MSG_3_2(buf) \475do { uint32x2_t t0, t1; \476t0 = vzip_u32(vget_low_u32(m2), vget_low_u32(m0)).val[1]; \477t1 = vzip_u32(vget_low_u32(m3), vget_high_u32(m3)).val[0]; \478buf = vcombine_u32(t0, t1); } while(0)479480#define BLAKE2S_LOAD_MSG_3_3(buf) \481do { uint32x2_t t0, t1; \482t0 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_high_u32(m0), vget_low_u32(m1)); \483t1 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_low_u32(m1), vget_high_u32(m3)); \484buf = vcombine_u32(t0, t1); } while(0)485486#define BLAKE2S_LOAD_MSG_3_4(buf) \487do { uint32x2_t t0, t1; \488t0 = vzip_u32(vget_high_u32(m1), vget_high_u32(m2)).val[0]; \489t1 = vzip_u32(vget_low_u32(m0), vget_low_u32(m2)).val[0]; \490buf = vcombine_u32(t0, t1); } while(0)491492#define BLAKE2S_LOAD_MSG_4_1(buf) \493do { uint32x2_t t0, t1; \494t0 = vzip_u32(vget_low_u32(m2), vget_low_u32(m1)).val[1]; \495t1 = vzip_u32((vget_high_u32(m0)), vget_high_u32(m2)).val[0]; \496buf = vcombine_u32(t0, t1); } while(0)497498#define BLAKE2S_LOAD_MSG_4_2(buf) \499do { uint32x2_t t0, t1; \500t0 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_low_u32(m0), vget_high_u32(m1)); \501t1 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_low_u32(m1), vget_high_u32(m3)); \502buf = vcombine_u32(t0, t1); } while(0)503504#define BLAKE2S_LOAD_MSG_4_3(buf) \505do { uint32x2_t t0, t1; \506t0 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_high_u32(m3), vget_high_u32(m2)); \507t1 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_high_u32(m1), vget_high_u32(m0)); \508buf = vcombine_u32(t0, t1); } while(0)509510#define BLAKE2S_LOAD_MSG_4_4(buf) \511do { uint32x2_t t0, t1; \512t0 = vext_u32(vget_low_u32(m0), vget_low_u32(m3), 1); \513t1 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_low_u32(m2), vget_low_u32(m3)); \514buf = vcombine_u32(t0, t1); } while(0)515516#define BLAKE2S_LOAD_MSG_5_1(buf) \517do { uint32x2_t t0, t1; \518t0 = vzip_u32((vget_high_u32(m0)), vget_high_u32(m1)).val[0]; \519t1 = vzip_u32(vget_low_u32(m0), vget_low_u32(m2)).val[0]; \520buf = vcombine_u32(t0, t1); } while(0)521522#define BLAKE2S_LOAD_MSG_5_2(buf) \523do { uint32x2_t t0, t1; \524t0 = vzip_u32(vget_low_u32(m3), vget_high_u32(m2)).val[0]; \525t1 = vzip_u32(vget_high_u32(m2), vget_high_u32(m0)).val[1]; \526buf = vcombine_u32(t0, t1); } while(0)527528#define BLAKE2S_LOAD_MSG_5_3(buf) \529do { uint32x2_t t0, t1; \530t0 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_low_u32(m1), vget_high_u32(m1)); \531t1 = vzip_u32(vget_high_u32(m3), vget_low_u32(m0)).val[1]; \532buf = vcombine_u32(t0, t1); } while(0)533534#define BLAKE2S_LOAD_MSG_5_4(buf) \535do { uint32x2_t t0, t1; \536t0 = vzip_u32(vget_low_u32(m3), vget_low_u32(m1)).val[1]; \537t1 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_high_u32(m3), vget_low_u32(m2)); \538buf = vcombine_u32(t0, t1); } while(0)539540#define BLAKE2S_LOAD_MSG_6_1(buf) \541do { uint32x2_t t0, t1; \542t0 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_low_u32(m3), vget_low_u32(m0)); \543t1 = vzip_u32(vget_high_u32(m3), vget_low_u32(m1)).val[0]; \544buf = vcombine_u32(t0, t1); } while(0)545546#define BLAKE2S_LOAD_MSG_6_2(buf) \547do { uint32x2_t t0, t1; \548t0 = vzip_u32(vget_low_u32(m1), vget_high_u32(m3)).val[1]; \549t1 = vext_u32(vget_low_u32(m3), vget_high_u32(m2), 1); \550buf = vcombine_u32(t0, t1); } while(0)551552#define BLAKE2S_LOAD_MSG_6_3(buf) \553do { uint32x2_t t0, t1; \554t0 = vzip_u32(vget_low_u32(m0), vget_high_u32(m1)).val[0]; \555t1 = vext_u32(vget_low_u32(m2), vget_low_u32(m2), 1); \556buf = vcombine_u32(t0, t1); } while(0)557558#define BLAKE2S_LOAD_MSG_6_4(buf) \559do { uint32x2_t t0, t1; \560t0 = vzip_u32(vget_high_u32(m1), vget_high_u32(m0)).val[1]; \561t1 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_high_u32(m0), vget_high_u32(m2)); \562buf = vcombine_u32(t0, t1); } while(0)563564#define BLAKE2S_LOAD_MSG_7_1(buf) \565do { uint32x2_t t0, t1; \566t0 = vzip_u32(vget_low_u32(m3), vget_high_u32(m1)).val[1]; \567t1 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_low_u32(m3), vget_high_u32(m0)); \568buf = vcombine_u32(t0, t1); } while(0)569570#define BLAKE2S_LOAD_MSG_7_2(buf) \571do { uint32x2_t t0, t1; \572t0 = vext_u32(vget_high_u32(m2), vget_high_u32(m3), 1); \573t1 = vzip_u32(vget_low_u32(m0), vget_low_u32(m2)).val[1]; \574buf = vcombine_u32(t0, t1); } while(0)575576#define BLAKE2S_LOAD_MSG_7_3(buf) \577do { uint32x2_t t0, t1; \578t0 = vzip_u32(vget_low_u32(m1), vget_high_u32(m3)).val[1]; \579t1 = vzip_u32(vget_low_u32(m2), vget_high_u32(m0)).val[0]; \580buf = vcombine_u32(t0, t1); } while(0)581582#define BLAKE2S_LOAD_MSG_7_4(buf) \583do { uint32x2_t t0, t1; \584t0 = vzip_u32(vget_low_u32(m0), vget_low_u32(m1)).val[0]; \585t1 = vzip_u32(vget_high_u32(m1), vget_high_u32(m2)).val[0]; \586buf = vcombine_u32(t0, t1); } while(0)587588#define BLAKE2S_LOAD_MSG_8_1(buf) \589do { uint32x2_t t0, t1; \590t0 = vzip_u32(vget_high_u32(m1), vget_high_u32(m3)).val[0]; \591t1 = vext_u32(vget_high_u32(m2), vget_low_u32(m0), 1); \592buf = vcombine_u32(t0, t1); } while(0)593594#define BLAKE2S_LOAD_MSG_8_2(buf) \595do { uint32x2_t t0, t1; \596t0 = vzip_u32(vget_high_u32(m3), vget_low_u32(m2)).val[1]; \597t1 = vext_u32(vget_high_u32(m0), vget_low_u32(m2), 1); \598buf = vcombine_u32(t0, t1); } while(0)599600#define BLAKE2S_LOAD_MSG_8_3(buf) \601do { uint32x2_t t0, t1; \602t0 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_low_u32(m3), vget_low_u32(m3)); \603t1 = vext_u32(vget_low_u32(m0), vget_high_u32(m2), 1); \604buf = vcombine_u32(t0, t1); } while(0)605606#define BLAKE2S_LOAD_MSG_8_4(buf) \607do { uint32x2_t t0, t1; \608t0 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_high_u32(m0), vget_high_u32(m1)); \609t1 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_low_u32(m1), vget_low_u32(m1)); \610buf = vcombine_u32(t0, t1); } while(0)611612#define BLAKE2S_LOAD_MSG_9_1(buf) \613do { uint32x2_t t0, t1; \614t0 = vzip_u32(vget_high_u32(m2), vget_low_u32(m2)).val[0]; \615t1 = vzip_u32(vget_high_u32(m1), vget_low_u32(m0)).val[1]; \616buf = vcombine_u32(t0, t1); } while(0)617618#define BLAKE2S_LOAD_MSG_9_2(buf) \619do { uint32x2_t t0, t1; \620t0 = vzip_u32((vget_high_u32(m0)), vget_low_u32(m1)).val[0]; \621t1 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_high_u32(m1), vget_low_u32(m1)); \622buf = vcombine_u32(t0, t1); } while(0)623624#define BLAKE2S_LOAD_MSG_9_3(buf) \625do { uint32x2_t t0, t1; \626t0 = vzip_u32(vget_high_u32(m3), vget_low_u32(m2)).val[1]; \627t1 = vzip_u32((vget_high_u32(m0)), vget_low_u32(m3)).val[1]; \628buf = vcombine_u32(t0, t1); } while(0)629630#define BLAKE2S_LOAD_MSG_9_4(buf) \631do { uint32x2_t t0, t1; \632t0 = vext_u32(vget_high_u32(m2), vget_high_u32(m3), 1); \633t1 = vzip_u32(vget_low_u32(m3), vget_low_u32(m0)).val[0]; \634buf = vcombine_u32(t0, t1); } while(0)635636#define vrorq_n_u32_16(x) vreinterpretq_u32_u16(vrev32q_u16(vreinterpretq_u16_u32(x)))637638#define vrorq_n_u32_8(x) vsriq_n_u32(vshlq_n_u32((x), 24), (x), 8)639640#define vrorq_n_u32(x, c) vsriq_n_u32(vshlq_n_u32((x), 32-(c)), (x), (c))641642#define BLAKE2S_G1(row1,row2,row3,row4,buf) \643do { \644row1 = vaddq_u32(vaddq_u32(row1, buf), row2); row4 = veorq_u32(row4, row1); \645row4 = vrorq_n_u32_16(row4); row3 = vaddq_u32(row3, row4); \646row2 = veorq_u32(row2, row3); row2 = vrorq_n_u32(row2, 12); \647} while(0)648649#define BLAKE2S_G2(row1,row2,row3,row4,buf) \650do { \651row1 = vaddq_u32(vaddq_u32(row1, buf), row2); row4 = veorq_u32(row4, row1); \652row4 = vrorq_n_u32_8(row4); row3 = vaddq_u32(row3, row4); \653row2 = veorq_u32(row2, row3); row2 = vrorq_n_u32(row2, 7); \654} while(0)655656#define BLAKE2S_DIAGONALIZE(row1,row2,row3,row4) \657do { \658row4 = vextq_u32(row4, row4, 3); row3 = vextq_u32(row3, row3, 2); row2 = vextq_u32(row2, row2, 1); \659} while(0)660661#define BLAKE2S_UNDIAGONALIZE(row1,row2,row3,row4) \662do { \663row4 = vextq_u32(row4, row4, 1); \664row3 = vextq_u32(row3, row3, 2); \665row2 = vextq_u32(row2, row2, 3); \666} while(0)667668#define BLAKE2S_ROUND(r) \669do { \670uint32x4_t buf1, buf2, buf3, buf4; \671BLAKE2S_LOAD_MSG_ ##r ##_1(buf1); \672BLAKE2S_G1(row1,row2,row3,row4,buf1); \673BLAKE2S_LOAD_MSG_ ##r ##_2(buf2); \674BLAKE2S_G2(row1,row2,row3,row4,buf2); \675BLAKE2S_DIAGONALIZE(row1,row2,row3,row4); \676BLAKE2S_LOAD_MSG_ ##r ##_3(buf3); \677BLAKE2S_G1(row1,row2,row3,row4,buf3); \678BLAKE2S_LOAD_MSG_ ##r ##_4(buf4); \679BLAKE2S_G2(row1,row2,row3,row4,buf4); \680BLAKE2S_UNDIAGONALIZE(row1,row2,row3,row4); \681} while(0)682683const uint32x4_t m0 = vreinterpretq_u32_u8(vld1q_u8(input + 00));684const uint32x4_t m1 = vreinterpretq_u32_u8(vld1q_u8(input + 16));685const uint32x4_t m2 = vreinterpretq_u32_u8(vld1q_u8(input + 32));686const uint32x4_t m3 = vreinterpretq_u32_u8(vld1q_u8(input + 48));687688uint32x4_t row1, row2, row3, row4;689690const uint32x4_t f0 = row1 = vld1q_u32(state.h()+0);691const uint32x4_t f1 = row2 = vld1q_u32(state.h()+4);692row3 = vld1q_u32(BLAKE2S_IV+0);693row4 = veorq_u32(vld1q_u32(BLAKE2S_IV+4), vld1q_u32(state.t()+0));694695BLAKE2S_ROUND(0);696BLAKE2S_ROUND(1);697BLAKE2S_ROUND(2);698BLAKE2S_ROUND(3);699BLAKE2S_ROUND(4);700BLAKE2S_ROUND(5);701BLAKE2S_ROUND(6);702BLAKE2S_ROUND(7);703BLAKE2S_ROUND(8);704BLAKE2S_ROUND(9);705706vst1q_u32(state.h()+0, veorq_u32(f0, veorq_u32(row1, row3)));707vst1q_u32(state.h()+4, veorq_u32(f1, veorq_u32(row2, row4)));708}709#endif // CRYPTOPP_ARM_NEON_AVAILABLE710711#if (CRYPTOPP_ALTIVEC_AVAILABLE)712713template <class T>714inline uint32x4_p VecLoad32(const T* p)715{716return VecLoad(p);717}718719template <class T>720inline uint32x4_p VecLoad32LE(const T* p, const uint8x16_p le_mask)721{722#if defined(CRYPTOPP_BIG_ENDIAN)723const uint32x4_p v = VecLoad(p);724return VecPermute(v, v, le_mask);725#else726CRYPTOPP_UNUSED(le_mask);727return VecLoad(p);728#endif729}730731template <class T>732inline void VecStore32(T* p, const uint32x4_p x)733{734VecStore(x, p);735}736737template <class T>738inline void VecStore32LE(T* p, const uint32x4_p x, const uint8x16_p le_mask)739{740#if defined(CRYPTOPP_BIG_ENDIAN)741const uint32x4_p v = VecPermute(x, x, le_mask);742VecStore(v, p);743#else744CRYPTOPP_UNUSED(le_mask);745VecStore(x, p);746#endif747}748749template <unsigned int E1, unsigned int E2>750inline uint32x4_p VectorSet32(const uint32x4_p a, const uint32x4_p b)751{752// Re-index. I'd like to use something like Z=Y*4 and then753// VecShiftLeftOctet<Z>(b) but it crashes early Red Hat754// GCC compilers.755enum {X=E1&3, Y=E2&3};756757// Don't care element758const unsigned int DC = 31;759760// Element 0 combinations761if (X == 0 && Y == 0)762{763const uint8x16_p mask = {0,1,2,3, 16,17,18,19, DC,DC,DC,DC, DC,DC,DC,DC};764return VecPermute(a, b, mask);765}766else if (X == 0 && Y == 1)767{768const uint8x16_p mask = {0,1,2,3, 16,17,18,19, DC,DC,DC,DC, DC,DC,DC,DC};769return VecPermute(a, VecShiftLeftOctet<4>(b), mask);770}771else if (X == 0 && Y == 2)772{773const uint8x16_p mask = {0,1,2,3, 16,17,18,19, DC,DC,DC,DC, DC,DC,DC,DC};774return VecPermute(a, VecShiftLeftOctet<8>(b), mask);775}776else if (X == 0 && Y == 3)777{778const uint8x16_p mask = {0,1,2,3, 16,17,18,19, DC,DC,DC,DC, DC,DC,DC,DC};779return VecPermute(a, VecShiftLeftOctet<12>(b), mask);780}781782// Element 1 combinations783else if (X == 1 && Y == 0)784{785const uint8x16_p mask = {4,5,6,7, 16,17,18,19, DC,DC,DC,DC, DC,DC,DC,DC};786return VecPermute(a, b, mask);787}788else if (X == 1 && Y == 1)789{790const uint8x16_p mask = {4,5,6,7, 16,17,18,19, DC,DC,DC,DC, DC,DC,DC,DC};791return VecPermute(a, VecShiftLeftOctet<4>(b), mask);792}793else if (X == 1 && Y == 2)794{795const uint8x16_p mask = {4,5,6,7, 16,17,18,19, DC,DC,DC,DC, DC,DC,DC,DC};796return VecPermute(a, VecShiftLeftOctet<8>(b), mask);797}798else if (X == 1 && Y == 3)799{800const uint8x16_p mask = {4,5,6,7, 16,17,18,19, DC,DC,DC,DC, DC,DC,DC,DC};801return VecPermute(a, VecShiftLeftOctet<12>(b), mask);802}803804// Element 2 combinations805else if (X == 2 && Y == 0)806{807const uint8x16_p mask = {8,9,10,11, 16,17,18,19, DC,DC,DC,DC, DC,DC,DC,DC};808return VecPermute(a, b, mask);809}810else if (X == 2 && Y == 1)811{812const uint8x16_p mask = {8,9,10,11, 16,17,18,19, DC,DC,DC,DC, DC,DC,DC,DC};813return VecPermute(a, VecShiftLeftOctet<4>(b), mask);814}815else if (X == 2 && Y == 2)816{817const uint8x16_p mask = {8,9,10,11, 16,17,18,19, DC,DC,DC,DC, DC,DC,DC,DC};818return VecPermute(a, VecShiftLeftOctet<8>(b), mask);819}820else if (X == 2 && Y == 3)821{822const uint8x16_p mask = {8,9,10,11, 16,17,18,19, DC,DC,DC,DC, DC,DC,DC,DC};823return VecPermute(a, VecShiftLeftOctet<12>(b), mask);824}825826// Element 3 combinations827else if (X == 3 && Y == 0)828{829const uint8x16_p mask = {12,13,14,15, 16,17,18,19, DC,DC,DC,DC, DC,DC,DC,DC};830return VecPermute(a, b, mask);831}832else if (X == 3 && Y == 1)833{834const uint8x16_p mask = {12,13,14,15, 16,17,18,19, DC,DC,DC,DC, DC,DC,DC,DC};835return VecPermute(a, VecShiftLeftOctet<4>(b), mask);836}837else if (X == 3 && Y == 2)838{839const uint8x16_p mask = {12,13,14,15, 16,17,18,19, DC,DC,DC,DC, DC,DC,DC,DC};840return VecPermute(a, VecShiftLeftOctet<8>(b), mask);841}842else if (X == 3 && Y == 3)843{844const uint8x16_p mask = {12,13,14,15, 16,17,18,19, DC,DC,DC,DC, DC,DC,DC,DC};845return VecPermute(a, VecShiftLeftOctet<12>(b), mask);846}847848// Quiet IBM XLC warning849return VecXor(a, a);850}851852template <unsigned int E1, unsigned int E2, unsigned int E3, unsigned int E4>853inline uint32x4_p VectorSet32(const uint32x4_p a, const uint32x4_p b,854const uint32x4_p c, const uint32x4_p d)855{856// Re-index857enum {W=E1&3, X=E2&3, Y=E3&3, Z=E4&3};858859const uint32x4_p t0 = VectorSet32<W,X>(a, b);860const uint32x4_p t1 = VectorSet32<Y,Z>(c, d);861862// PowerPC follows SSE2's implementation, and this is _mm_set_epi32.863const uint8x16_p mask = {20,21,22,23, 16,17,18,19, 4,5,6,7, 0,1,2,3};864return VecPermute(t0, t1, mask);865}866867template<>868uint32x4_p VectorSet32<2,0,2,0>(const uint32x4_p a, const uint32x4_p b,869const uint32x4_p c, const uint32x4_p d)870{871// a=b, c=d, mask is {2,0, 2,0}872CRYPTOPP_UNUSED(b); CRYPTOPP_UNUSED(d);873const uint8x16_p mask = {16,17,18,19, 24,25,26,27, 0,1,2,3, 8,9,10,11};874return VecPermute(a, c, mask);875}876877template<>878uint32x4_p VectorSet32<3,1,3,1>(const uint32x4_p a, const uint32x4_p b,879const uint32x4_p c, const uint32x4_p d)880{881// a=b, c=d, mask is {3,1, 3,1}882CRYPTOPP_UNUSED(b); CRYPTOPP_UNUSED(d);883const uint8x16_p mask = {20,21,22,23, 28,29,30,31, 4,5,6,7, 12,13,14,15};884return VecPermute(a, c, mask);885}886887void BLAKE2_Compress32_ALTIVEC(const byte* input, BLAKE2s_State& state)888{889# define m1 m0890# define m2 m0891# define m3 m0892893# define m5 m4894# define m6 m4895# define m7 m4896897# define m9 m8898# define m10 m8899# define m11 m8900901# define m13 m12902# define m14 m12903# define m15 m12904905// #define BLAKE2S_LOAD_MSG_0_1(buf) buf = VectorSet32<6,4,2,0>(m6,m4,m2,m0);906#define BLAKE2S_LOAD_MSG_0_1(buf) buf = VectorSet32<2,0,2,0>(m6,m4,m2,m0);907// #define BLAKE2S_LOAD_MSG_0_2(buf) buf = VectorSet32<7,5,3,1>(m7,m5,m3,m1);908#define BLAKE2S_LOAD_MSG_0_2(buf) buf = VectorSet32<3,1,3,1>(m7,m5,m3,m1);909// #define BLAKE2S_LOAD_MSG_0_3(buf) buf = VectorSet32<14,12,10,8>(m14,m12,m10,m8);910#define BLAKE2S_LOAD_MSG_0_3(buf) buf = VectorSet32<2,0,2,0>(m14,m12,m10,m8);911// #define BLAKE2S_LOAD_MSG_0_4(buf) buf = VectorSet32<15,13,11,9>(m15,m13,m11,m9);912#define BLAKE2S_LOAD_MSG_0_4(buf) buf = VectorSet32<3,1,3,1>(m15,m13,m11,m9);913914#define BLAKE2S_LOAD_MSG_1_1(buf) buf = VectorSet32<13,9,4,14>(m13,m9,m4,m14);915#define BLAKE2S_LOAD_MSG_1_2(buf) buf = VectorSet32<6,15,8,10>(m6,m15,m8,m10)916#define BLAKE2S_LOAD_MSG_1_3(buf) buf = VectorSet32<5,11,0,1>(m5,m11,m0,m1)917#define BLAKE2S_LOAD_MSG_1_4(buf) buf = VectorSet32<3,7,2,12>(m3,m7,m2,m12)918919#define BLAKE2S_LOAD_MSG_2_1(buf) buf = VectorSet32<15,5,12,11>(m15,m5,m12,m11)920#define BLAKE2S_LOAD_MSG_2_2(buf) buf = VectorSet32<13,2,0,8>(m13,m2,m0,m8)921#define BLAKE2S_LOAD_MSG_2_3(buf) buf = VectorSet32<9,7,3,10>(m9,m7,m3,m10)922#define BLAKE2S_LOAD_MSG_2_4(buf) buf = VectorSet32<4,1,6,14>(m4,m1,m6,m14)923924#define BLAKE2S_LOAD_MSG_3_1(buf) buf = VectorSet32<11,13,3,7>(m11,m13,m3,m7)925#define BLAKE2S_LOAD_MSG_3_2(buf) buf = VectorSet32<14,12,1,9>(m14,m12,m1,m9)926#define BLAKE2S_LOAD_MSG_3_3(buf) buf = VectorSet32<15,4,5,2>(m15,m4,m5,m2)927#define BLAKE2S_LOAD_MSG_3_4(buf) buf = VectorSet32<8,0,10,6>(m8,m0,m10,m6)928929#define BLAKE2S_LOAD_MSG_4_1(buf) buf = VectorSet32<10,2,5,9>(m10,m2,m5,m9)930#define BLAKE2S_LOAD_MSG_4_2(buf) buf = VectorSet32<15,4,7,0>(m15,m4,m7,m0)931#define BLAKE2S_LOAD_MSG_4_3(buf) buf = VectorSet32<3,6,11,14>(m3,m6,m11,m14)932#define BLAKE2S_LOAD_MSG_4_4(buf) buf = VectorSet32<13,8,12,1>(m13,m8,m12,m1)933934#define BLAKE2S_LOAD_MSG_5_1(buf) buf = VectorSet32<8,0,6,2>(m8,m0,m6,m2)935#define BLAKE2S_LOAD_MSG_5_2(buf) buf = VectorSet32<3,11,10,12>(m3,m11,m10,m12)936#define BLAKE2S_LOAD_MSG_5_3(buf) buf = VectorSet32<1,15,7,4>(m1,m15,m7,m4)937#define BLAKE2S_LOAD_MSG_5_4(buf) buf = VectorSet32<9,14,5,13>(m9,m14,m5,m13)938939#define BLAKE2S_LOAD_MSG_6_1(buf) buf = VectorSet32<4,14,1,12>(m4,m14,m1,m12)940#define BLAKE2S_LOAD_MSG_6_2(buf) buf = VectorSet32<10,13,15,5>(m10,m13,m15,m5)941#define BLAKE2S_LOAD_MSG_6_3(buf) buf = VectorSet32<8,9,6,0>(m8,m9,m6,m0)942#define BLAKE2S_LOAD_MSG_6_4(buf) buf = VectorSet32<11,2,3,7>(m11,m2,m3,m7)943944#define BLAKE2S_LOAD_MSG_7_1(buf) buf = VectorSet32<3,12,7,13>(m3,m12,m7,m13)945#define BLAKE2S_LOAD_MSG_7_2(buf) buf = VectorSet32<9,1,14,11>(m9,m1,m14,m11)946#define BLAKE2S_LOAD_MSG_7_3(buf) buf = VectorSet32<2,8,15,5>(m2,m8,m15,m5)947#define BLAKE2S_LOAD_MSG_7_4(buf) buf = VectorSet32<10,6,4,0>(m10,m6,m4,m0)948949#define BLAKE2S_LOAD_MSG_8_1(buf) buf = VectorSet32<0,11,14,6>(m0,m11,m14,m6)950#define BLAKE2S_LOAD_MSG_8_2(buf) buf = VectorSet32<8,3,9,15>(m8,m3,m9,m15)951#define BLAKE2S_LOAD_MSG_8_3(buf) buf = VectorSet32<10,1,13,12>(m10,m1,m13,m12)952#define BLAKE2S_LOAD_MSG_8_4(buf) buf = VectorSet32<5,4,7,2>(m5,m4,m7,m2)953954#define BLAKE2S_LOAD_MSG_9_1(buf) buf = VectorSet32<1,7,8,10>(m1,m7,m8,m10)955#define BLAKE2S_LOAD_MSG_9_2(buf) buf = VectorSet32<5,6,4,2>(m5,m6,m4,m2)956#define BLAKE2S_LOAD_MSG_9_3(buf) buf = VectorSet32<13,3,9,15>(m13,m3,m9,m15)957#define BLAKE2S_LOAD_MSG_9_4(buf) buf = VectorSet32<0,12,14,11>(m0,m12,m14,m11)958959#define vec_ror_16(x) VecRotateRight<16>(x)960#define vec_ror_12(x) VecRotateRight<12>(x)961#define vec_ror_8(x) VecRotateRight<8>(x)962#define vec_ror_7(x) VecRotateRight<7>(x)963964#define BLAKE2S_G1(row1,row2,row3,row4,buf) \965row1 = VecAdd(VecAdd(row1, buf), row2); \966row4 = VecXor(row4, row1); \967row4 = vec_ror_16(row4); \968row3 = VecAdd(row3, row4); \969row2 = VecXor(row2, row3); \970row2 = vec_ror_12(row2);971972#define BLAKE2S_G2(row1,row2,row3,row4,buf) \973row1 = VecAdd(VecAdd(row1, buf), row2); \974row4 = VecXor(row4, row1); \975row4 = vec_ror_8(row4); \976row3 = VecAdd(row3, row4); \977row2 = VecXor(row2, row3); \978row2 = vec_ror_7(row2);979980const uint8x16_p D2103_MASK = {12,13,14,15, 0,1,2,3, 4,5,6,7, 8,9,10,11};981const uint8x16_p D1032_MASK = {8,9,10,11, 12,13,14,15, 0,1,2,3, 4,5,6,7};982const uint8x16_p D0321_MASK = {4,5,6,7, 8,9,10,11, 12,13,14,15, 0,1,2,3};983984#define BLAKE2S_DIAGONALIZE(row1,row2,row3,row4) \985row4 = VecPermute(row4, row4, D2103_MASK); \986row3 = VecPermute(row3, row3, D1032_MASK); \987row2 = VecPermute(row2, row2, D0321_MASK);988989#define BLAKE2S_UNDIAGONALIZE(row1,row2,row3,row4) \990row4 = VecPermute(row4, row4, D0321_MASK); \991row3 = VecPermute(row3, row3, D1032_MASK); \992row2 = VecPermute(row2, row2, D2103_MASK);993994#define BLAKE2S_ROUND(r) \995BLAKE2S_LOAD_MSG_ ##r ##_1(buf1); \996BLAKE2S_G1(row1,row2,row3,row4,buf1); \997BLAKE2S_LOAD_MSG_ ##r ##_2(buf2); \998BLAKE2S_G2(row1,row2,row3,row4,buf2); \999BLAKE2S_DIAGONALIZE(row1,row2,row3,row4); \1000BLAKE2S_LOAD_MSG_ ##r ##_3(buf3); \1001BLAKE2S_G1(row1,row2,row3,row4,buf3); \1002BLAKE2S_LOAD_MSG_ ##r ##_4(buf4); \1003BLAKE2S_G2(row1,row2,row3,row4,buf4); \1004BLAKE2S_UNDIAGONALIZE(row1,row2,row3,row4);10051006// Possibly unaligned user messages1007uint32x4_p m0, m4, m8, m12;1008// Endian conversion mask1009const uint8x16_p le_mask = {3,2,1,0, 7,6,5,4, 11,10,9,8, 15,14,13,12};10101011#if defined(_ARCH_PWR9)1012// POWER9 provides loads for char's and short's1013m0 = (uint32x4_p) vec_xl( 0, CONST_V8_CAST( input ));1014m4 = (uint32x4_p) vec_xl( 16, CONST_V8_CAST( input ));1015m8 = (uint32x4_p) vec_xl( 32, CONST_V8_CAST( input ));1016m12 = (uint32x4_p) vec_xl( 48, CONST_V8_CAST( input ));10171018# if defined(CRYPTOPP_BIG_ENDIAN)1019m0 = vec_perm(m0, m0, le_mask);1020m4 = vec_perm(m4, m4, le_mask);1021m8 = vec_perm(m8, m8, le_mask);1022m12 = vec_perm(m12, m12, le_mask);1023# endif1024#else1025// Altivec only provides 16-byte aligned loads1026// http://www.nxp.com/docs/en/reference-manual/ALTIVECPEM.pdf1027m0 = (uint32x4_p) vec_ld( 0, CONST_V8_CAST( input ));1028m4 = (uint32x4_p) vec_ld( 16, CONST_V8_CAST( input ));1029m8 = (uint32x4_p) vec_ld( 32, CONST_V8_CAST( input ));1030m12 = (uint32x4_p) vec_ld( 48, CONST_V8_CAST( input ));10311032// Alignment check for load of the message buffer1033const uintptr_t addr = (uintptr_t)input;1034if (addr%16 == 0)1035{1036// Already aligned. Perform a little-endian swap as required1037# if defined(CRYPTOPP_BIG_ENDIAN)1038m0 = vec_perm(m0, m0, le_mask);1039m4 = vec_perm(m4, m4, le_mask);1040m8 = vec_perm(m8, m8, le_mask);1041m12 = vec_perm(m12, m12, le_mask);1042# endif1043}1044else1045{1046// Not aligned. Fix vectors and perform a little-endian swap as required1047// http://mirror.informatimago.com/next/developer.apple.com/1048// hardwaredrivers/ve/code_optimization.html1049uint32x4_p ex; uint8x16_p perm;1050ex = (uint32x4_p) vec_ld(48+15, CONST_V8_CAST( input ));1051perm = vec_lvsl(0, CONST_V8_CAST( addr ));10521053# if defined(CRYPTOPP_BIG_ENDIAN)1054// Combine the vector permute with the little-endian swap1055perm = vec_perm(perm, perm, le_mask);1056# endif10571058m0 = vec_perm(m0, m4, perm);1059m4 = vec_perm(m4, m8, perm);1060m8 = vec_perm(m8, m12, perm);1061m12 = vec_perm(m12, ex, perm);1062}1063#endif10641065uint32x4_p row1, row2, row3, row4;1066uint32x4_p buf1, buf2, buf3, buf4;1067uint32x4_p ff0, ff1;10681069row1 = ff0 = VecLoad32LE(state.h()+0, le_mask);1070row2 = ff1 = VecLoad32LE(state.h()+4, le_mask);1071row3 = VecLoad32(BLAKE2S_IV+0);1072row4 = VecXor(VecLoad32(BLAKE2S_IV+4), VecLoad32(state.t()+0));10731074BLAKE2S_ROUND(0);1075BLAKE2S_ROUND(1);1076BLAKE2S_ROUND(2);1077BLAKE2S_ROUND(3);1078BLAKE2S_ROUND(4);1079BLAKE2S_ROUND(5);1080BLAKE2S_ROUND(6);1081BLAKE2S_ROUND(7);1082BLAKE2S_ROUND(8);1083BLAKE2S_ROUND(9);10841085VecStore32LE(state.h()+0, VecXor(ff0, VecXor(row1, row3)), le_mask);1086VecStore32LE(state.h()+4, VecXor(ff1, VecXor(row2, row4)), le_mask);1087}1088#endif // CRYPTOPP_ALTIVEC_AVAILABLE10891090NAMESPACE_END109110921093