8#include <botan/internal/ghash.h>
10#include <botan/internal/isa_extn.h>
11#include <botan/internal/simd_4x32.h>
12#include <botan/internal/target_info.h>
14#if defined(BOTAN_SIMD_USE_SSSE3)
15 #include <immintrin.h>
16 #include <wmmintrin.h>
26#if defined(BOTAN_SIMD_USE_SSSE3)
27 const __m128i BSWAP_MASK = _mm_set_epi8(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
28 return SIMD_4x32(_mm_shuffle_epi8(in.raw(), BSWAP_MASK));
29#elif defined(BOTAN_SIMD_USE_NEON)
30 const uint8_t maskb[16] = {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
31 const uint8x16_t mask = vld1q_u8(maskb);
32 return SIMD_4x32(vreinterpretq_u32_u8(vqtbl1q_u8(vreinterpretq_u8_u32(in.raw()), mask)));
33#elif defined(BOTAN_SIMD_USE_ALTIVEC)
34 const __vector
unsigned char mask = {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
35 return SIMD_4x32(vec_perm(in.raw(), in.raw(), mask));
41 static_assert(M == 0x00 || M == 0x01 || M == 0x10 || M == 0x11,
"Valid clmul mode");
43#if defined(BOTAN_SIMD_USE_SSSE3)
44 return SIMD_4x32(_mm_clmulepi64_si128(x.raw(), H.raw(), M));
45#elif defined(BOTAN_SIMD_USE_NEON)
46 const uint64_t a = vgetq_lane_u64(vreinterpretq_u64_u32(x.raw()), M & 0x01);
47 const uint64_t b = vgetq_lane_u64(vreinterpretq_u64_u32(H.raw()), (M & 0x10) >> 4);
49 #if defined(BOTAN_BUILD_COMPILER_IS_MSVC)
50 __n64 a1 = {a}, b1 = {b};
53 return SIMD_4x32(
reinterpret_cast<uint32x4_t
>(vmull_p64(a, b)));
56#elif defined(BOTAN_SIMD_USE_ALTIVEC)
58 constexpr uint8_t flip = (std::endian::native == std::endian::big) ? 0x11 : 0x00;
63 if constexpr(std::endian::native == std::endian::big) {
64 i1 = reverse_vector(i1).bswap();
65 i2 = reverse_vector(i2).bswap();
68 if constexpr(M == (0x11 ^ flip)) {
71 }
else if constexpr(M == (0x10 ^ flip)) {
72 i1 = i1.shift_elems_left<2>();
73 }
else if constexpr(M == (0x01 ^ flip)) {
74 i2 = i2.shift_elems_left<2>();
75 }
else if constexpr(M == (0x00 ^ flip)) {
76 i1 = mask_lo.andc(i1);
77 i2 = mask_lo.andc(i2);
80 auto i1v =
reinterpret_cast<__vector
unsigned long long>(i1.raw());
81 auto i2v =
reinterpret_cast<__vector
unsigned long long>(i2.raw());
83 #if BOTAN_COMPILER_HAS_BUILTIN(__builtin_crypto_vpmsumd)
84 auto rv = __builtin_crypto_vpmsumd(i1v, i2v);
86 auto rv = __builtin_altivec_crypto_vpmsumd(i1v, i2v);
89 auto z =
SIMD_4x32(
reinterpret_cast<__vector
unsigned int>(rv));
91 if constexpr(std::endian::native == std::endian::big) {
92 z = reverse_vector(z).bswap();
111 X0 = X1.
shl<31>() ^ X1.
shl<30>() ^ X1.
shl<25>();
116 X0 ^= X1.
shr<7>() ^ X1.
shr<2>() ^ X1.
shr<1>();
127 T0 ^= T1.shift_elems_right<2>();
128 T3 ^= T1.shift_elems_left<2>();
130 return gcm_reduce(T0, T3);
146 const SIMD_4x32 lo = (clmul<0x00>(H1, X1) ^ clmul<0x00>(H2, X2)) ^ (clmul<0x00>(H3, X3) ^ clmul<0x00>(H4, X4));
148 const SIMD_4x32 hi = (clmul<0x11>(H1, X1) ^ clmul<0x11>(H2, X2)) ^ (clmul<0x11>(H3, X3) ^ clmul<0x11>(H4, X4));
152 T ^= clmul<0x00>(H1 ^ H1.shift_elems_right<2>(), X1 ^ X1.shift_elems_right<2>());
153 T ^= clmul<0x00>(H2 ^ H2.shift_elems_right<2>(), X2 ^ X2.shift_elems_right<2>());
154 T ^= clmul<0x00>(H3 ^ H3.shift_elems_right<2>(), X3 ^ X3.shift_elems_right<2>());
155 T ^= clmul<0x00>(H4 ^ H4.shift_elems_right<2>(), X4 ^ X4.shift_elems_right<2>());
159 return gcm_reduce(hi ^ T.shift_elems_right<2>(), lo ^ T.shift_elems_left<2>());
164void BOTAN_FN_ISA_CLMUL GHASH::ghash_precompute_cpu(
const uint8_t H_bytes[16], uint64_t H_pow[4 * 2]) {
166 const SIMD_4x32 H2 = gcm_multiply(H1, H1);
167 const SIMD_4x32 H3 = gcm_multiply(H1, H2);
168 const SIMD_4x32 H4 = gcm_multiply(H2, H2);
171 H2.store_le(H_pow + 2);
172 H3.store_le(H_pow + 4);
173 H4.store_le(H_pow + 6);
176void BOTAN_FN_ISA_CLMUL GHASH::ghash_multiply_cpu(uint8_t x[16],
177 const uint64_t H_pow[8],
178 const uint8_t input[],
199 a = gcm_multiply_x4(H1, H2, H3, H4, m3, m2, m1, a);
206 for(
size_t i = 0; i != blocks; ++i) {
210 a = gcm_multiply(H1, a);
213 a = reverse_vector(a);
static SIMD_4x32 load_le(const void *in) noexcept
SIMD_4x32 shift_elems_left() const noexcept
SIMD_4x32 shr() const noexcept
SIMD_4x32 shl() const noexcept
SIMD_4x32 shift_elems_right() const noexcept
#define BOTAN_FORCE_INLINE