7#include <botan/internal/sm4.h>
9#include <botan/mem_ops.h>
10#include <botan/internal/isa_extn.h>
11#include <botan/internal/simd_avx2.h>
12#include <botan/internal/simd_avx2_gfni.h>
33 constexpr uint8_t pre_c = 0b00111110;
45 constexpr uint8_t post_c = 0b11010011;
53 return sx ^ sx.rotl<2>() ^ sx.rotl<10>() ^ sx.rotl<18>() ^ sx.rotl<24>();
56BOTAN_FORCE_INLINE BOTAN_FN_ISA_AVX2_GFNI
void sm4_gfni_encrypt_8(
const uint8_t ptext[8 * 16],
57 uint8_t ctext[8 * 16],
58 std::span<const uint32_t> RK) {
71 for(
size_t j = 0; j != 8; ++j) {
80 B3.rev_words().store_be(ctext);
81 B2.rev_words().store_be(ctext + 16 * 2);
82 B1.rev_words().store_be(ctext + 16 * 4);
83 B0.rev_words().store_be(ctext + 16 * 6);
86BOTAN_FORCE_INLINE BOTAN_FN_ISA_AVX2_GFNI
void sm4_gfni_decrypt_8(
const uint8_t ctext[8 * 16],
87 uint8_t ptext[8 * 16],
88 std::span<const uint32_t> RK) {
101 for(
size_t j = 0; j != 8; ++j) {
110 B3.rev_words().store_be(ptext);
111 B2.rev_words().store_be(ptext + 16 * 2);
112 B1.rev_words().store_be(ptext + 16 * 4);
113 B0.rev_words().store_be(ptext + 16 * 6);
118void BOTAN_FN_ISA_AVX2_GFNI SM4::sm4_gfni_encrypt(
const uint8_t ptext[], uint8_t ctext[],
size_t blocks)
const {
120 sm4_gfni_encrypt_8(ptext, ctext, m_RK);
127 uint8_t pbuf[8 * 16] = {0};
128 uint8_t cbuf[8 * 16] = {0};
130 sm4_gfni_encrypt_8(pbuf, cbuf, m_RK);
135void BOTAN_FN_ISA_AVX2_GFNI SM4::sm4_gfni_decrypt(
const uint8_t ctext[], uint8_t ptext[],
size_t blocks)
const {
137 sm4_gfni_decrypt_8(ctext, ptext, m_RK);
144 uint8_t cbuf[8 * 16] = {0};
145 uint8_t pbuf[8 * 16] = {0};
147 sm4_gfni_decrypt_8(cbuf, pbuf, m_RK);
static BOTAN_FN_ISA_AVX2 void transpose(SIMD_8x32 &B0, SIMD_8x32 &B1, SIMD_8x32 &B2, SIMD_8x32 &B3) noexcept
static BOTAN_FN_ISA_AVX2 SIMD_8x32 splat(uint32_t B) noexcept
static BOTAN_FN_ISA_AVX2 SIMD_8x32 load_be(const uint8_t *in) noexcept
#define BOTAN_FORCE_INLINE
constexpr void copy_mem(T *out, const T *in, size_t n)
consteval uint64_t gfni_matrix(std::string_view s)
BOTAN_FORCE_INLINE BOTAN_FN_ISA_AVX2_GFNI SIMD_8x32 gf2p8affineinv(const SIMD_8x32 &x)
BOTAN_FORCE_INLINE BOTAN_FN_ISA_AVX2_GFNI SIMD_8x32 gf2p8affine(const SIMD_8x32 &x)