7#include <botan/internal/sm4.h>
9#include <botan/internal/simd_avx2.h>
10#include <botan/internal/simd_avx2_gfni.h>
31 constexpr uint8_t pre_c = 0b00111110;
43 constexpr uint8_t post_c = 0b11010011;
50 SIMD_8x32 sx = sm4_sbox(x);
51 return sx ^ sx.rotl<2>() ^ sx.rotl<10>() ^ sx.rotl<18>() ^ sx.rotl<24>();
55void sm4_gfni_encrypt_8(
const uint8_t ptext[8 * 16], uint8_t ctext[8 * 16], std::span<const uint32_t> RK) {
56 SIMD_8x32 B0 = SIMD_8x32::load_be(ptext);
57 SIMD_8x32 B1 = SIMD_8x32::load_be(ptext + 16 * 2);
58 SIMD_8x32 B2 = SIMD_8x32::load_be(ptext + 16 * 4);
59 SIMD_8x32 B3 = SIMD_8x32::load_be(ptext + 16 * 6);
61 SIMD_8x32::transpose(B0, B1, B2, B3);
68 for(
size_t j = 0; j != 8; ++j) {
69 B0 ^= sm4_f(B1 ^ B2 ^ B3 ^ SIMD_8x32::splat(RK[4 * j]));
70 B1 ^= sm4_f(B2 ^ B3 ^ B0 ^ SIMD_8x32::splat(RK[4 * j + 1]));
71 B2 ^= sm4_f(B3 ^ B0 ^ B1 ^ SIMD_8x32::splat(RK[4 * j + 2]));
72 B3 ^= sm4_f(B0 ^ B1 ^ B2 ^ SIMD_8x32::splat(RK[4 * j + 3]));
75 SIMD_8x32::transpose(B0, B1, B2, B3);
77 B3.rev_words().store_be(ctext);
78 B2.rev_words().store_be(ctext + 16 * 2);
79 B1.rev_words().store_be(ctext + 16 * 4);
80 B0.rev_words().store_be(ctext + 16 * 6);
84void sm4_gfni_decrypt_8(
const uint8_t ctext[8 * 16], uint8_t ptext[8 * 16], std::span<const uint32_t> RK) {
85 SIMD_8x32 B0 = SIMD_8x32::load_be(ctext);
86 SIMD_8x32 B1 = SIMD_8x32::load_be(ctext + 16 * 2);
87 SIMD_8x32 B2 = SIMD_8x32::load_be(ctext + 16 * 4);
88 SIMD_8x32 B3 = SIMD_8x32::load_be(ctext + 16 * 6);
90 SIMD_8x32::transpose(B0, B1, B2, B3);
97 for(
size_t j = 0; j != 8; ++j) {
98 B0 ^= sm4_f(B1 ^ B2 ^ B3 ^ SIMD_8x32::splat(RK[32 - (4 * j + 1)]));
99 B1 ^= sm4_f(B2 ^ B3 ^ B0 ^ SIMD_8x32::splat(RK[32 - (4 * j + 2)]));
100 B2 ^= sm4_f(B3 ^ B0 ^ B1 ^ SIMD_8x32::splat(RK[32 - (4 * j + 3)]));
101 B3 ^= sm4_f(B0 ^ B1 ^ B2 ^ SIMD_8x32::splat(RK[32 - (4 * j + 4)]));
104 SIMD_8x32::transpose(B0, B1, B2, B3);
106 B3.rev_words().store_be(ptext);
107 B2.rev_words().store_be(ptext + 16 * 2);
108 B1.rev_words().store_be(ptext + 16 * 4);
109 B0.rev_words().store_be(ptext + 16 * 6);
114void BOTAN_FUNC_ISA(
"gfni,avx2") SM4::sm4_gfni_encrypt(const uint8_t ptext[], uint8_t ctext[],
size_t blocks)
const {
116 sm4_gfni_encrypt_8(ptext, ctext, m_RK);
123 uint8_t pbuf[8 * 16] = {0};
124 uint8_t cbuf[8 * 16] = {0};
126 sm4_gfni_encrypt_8(pbuf, cbuf, m_RK);
131void BOTAN_FUNC_ISA(
"gfni,avx2") SM4::sm4_gfni_decrypt(const uint8_t ctext[], uint8_t ptext[],
size_t blocks)
const {
133 sm4_gfni_decrypt_8(ctext, ptext, m_RK);
140 uint8_t cbuf[8 * 16] = {0};
141 uint8_t pbuf[8 * 16] = {0};
143 sm4_gfni_decrypt_8(cbuf, pbuf, m_RK);
#define BOTAN_FUNC_ISA(isa)
#define BOTAN_FUNC_ISA_INLINE(isa)
SIMD_8x32 gf2p8affine(const SIMD_8x32 &x)
consteval uint64_t gfni_matrix(std::string_view s)
SIMD_8x32 gf2p8affineinv(const SIMD_8x32 &x)
constexpr void copy_mem(T *out, const T *in, size_t n)