9#include <botan/internal/sm4.h>
11#include <botan/internal/bit_ops.h>
12#include <botan/internal/ct_utils.h>
13#include <botan/internal/loadstor.h>
14#include <botan/internal/rotate.h>
16#if defined(BOTAN_HAS_CPUID)
17 #include <botan/internal/cpuid.h>
64using sm4_bs_word =
word;
65static_assert(std::same_as<sm4_bs_word, uint32_t> || std::same_as<sm4_bs_word, uint64_t>);
67template <std::
unsigned_
integral W>
68constexpr size_t sm4_bs_lanes() {
69 return 8 *
sizeof(W) / 4;
72constexpr size_t SM4_BITSLICED_BLOCKS = sm4_bs_lanes<sm4_bs_word>();
77template <
size_t N, std::
unsigned_
integral W>
85template <std::
unsigned_
integral W>
90 const W y4 = B[5] ^ B[2];
91 const W y12 = B[6] ^ a0;
92 const W a1 = B[0] ^ y12;
93 const W y6 = B[4] ^ a1;
94 const W a2 = B[7] ^ B[1];
95 const W a3 = B[5] ^ B[1];
96 const W y10 = B[0] ^ y4;
98 const W a4 = B[6] ^ y4;
99 const W y14 = a2 ^ a4;
100 const W y19 = y10 ^ y8;
101 const W y9 = y8 ^ y14;
102 const W y11 = y6 ^ a4;
103 const W y15 = a1 ^ a3;
104 const W y17 = B[4] ^ a0;
105 const W y1 = a4 ^ y17;
106 const W y16 = y15 ^ y17;
107 const W y13 = y12 ^ y14;
108 const W U7 = y6 ^ y15;
109 const W y7 = a4 ^ y15;
110 const W y2 = y4 ^ y9;
111 const W y20 = y9 ^ y11;
112 const W y21 = y16 ^ y13;
113 const W y5 = y13 ^ y2;
114 const W y18 = y7 ^ y2;
115 const W y3 = B[4] ^ y1;
121 const W t2 = y12 & y15;
122 const W t3 = y3 & y6;
123 const W t4 = t3 ^ t2;
124 const W t5 = y4 & U7;
125 const W t6 = t5 ^ t2;
126 const W t7 = y13 & y16;
127 const W t8 = y5 & y1;
128 const W t9 = t8 ^ t7;
129 const W t10 = y2 & y7;
130 const W t11 = t10 ^ t7;
131 const W t12 = y9 & y11;
132 const W t13 = y14 & y17;
133 const W t14 = t13 ^ t12;
134 const W t15 = y8 & y10;
135 const W t16 = t15 ^ t12;
136 const W t17 = t4 ^ y20;
137 const W t18 = t6 ^ t16;
138 const W t19 = t9 ^ t14;
139 const W t20 = t11 ^ t16;
140 const W t21 = t17 ^ t14;
141 const W t22 = t18 ^ y19;
142 const W t23 = t19 ^ y21;
143 const W t24 = t20 ^ y18;
144 const W t25 = t21 ^ t22;
145 const W t26 = t21 & t23;
146 const W t27 = t24 ^ t26;
147 const W t28 = t25 & t27;
148 const W t29 = t28 ^ t22;
149 const W t30 = t23 ^ t24;
150 const W t31 = t22 ^ t26;
151 const W t32 = t31 & t30;
152 const W t33 = t32 ^ t24;
153 const W t34 = t23 ^ t33;
154 const W t35 = t27 ^ t33;
155 const W t36 = t24 & t35;
156 const W t37 = t36 ^ t34;
157 const W t38 = t27 ^ t36;
158 const W t39 = t29 & t38;
159 const W t40 = t25 ^ t39;
160 const W t41 = t40 ^ t37;
161 const W t42 = t29 ^ t33;
162 const W t43 = t29 ^ t40;
163 const W t44 = t33 ^ t37;
164 const W t45 = t42 ^ t41;
165 const W z0 = t44 & y15;
166 const W z1 = t37 & y6;
167 const W z2 = t33 & U7;
168 const W z3 = t43 & y16;
169 const W z4 = t40 & y1;
170 const W z5 = t29 & y7;
171 const W z6 = t42 & y11;
172 const W z7 = t45 & y17;
173 const W z8 = t41 & y10;
174 const W z9 = t44 & y12;
175 const W z10 = t37 & y3;
176 const W z11 = t33 & y4;
177 const W z12 = t43 & y13;
178 const W z13 = t40 & y5;
179 const W z14 = t29 & y2;
180 const W z15 = t42 & y9;
181 const W z16 = t45 & y14;
182 const W z17 = t41 & y8;
186 const W c0 = z0 ^ z7;
187 const W c1 = z2 ^ z4;
188 const W c2 = z11 ^ z14;
190 const W c4 = c0 ^ c1;
191 const W c5 = z15 ^ c3;
192 const W c6 = z3 ^ z8;
193 const W c7 = z10 ^ c6;
194 const W c8 = c4 ^ c7;
195 const W c9 = z5 ^ z6;
196 const W c10 = z13 ^ c2;
197 const W c11 = z16 ^ c5;
198 const W c12 = z12 ^ c2;
199 const W c14 = z14 ^ z17;
200 const W c15 = z13 ^ c14;
201 const W B0 = c8 ^ c11;
202 const W B2 = c8 ^ c10;
204 const W B7 = c9 ^ c16;
205 const W c17 = z9 ^ c12;
206 const W c18 = B0 ^ c17;
207 const W B5 = B7 ^ c18;
208 const W c19 = c5 ^ c15;
209 const W c20 = z6 ^ c0;
210 const W c21 = z1 ^ c20;
211 const W B4 = c17 ^ c21;
212 const W B1 = z10 ^ c19;
213 const W c22 = c19 ^ B4;
214 const W c23 = B7 ^ c22;
215 const W B3 = c11 ^ c23;
216 const W c24 = z4 ^ c10;
217 const W c25 = c11 ^ c24;
218 const W c26 = z0 ^ z1;
219 const W c27 = c25 ^ c26;
220 const W B6 = z3 ^ c27;
242template <std::
unsigned_
integral W>
247 for(
size_t i = 0; i != 8; ++i) {
248 const W r1 = sm4_bs_byte_rotl<1>(T[i]);
249 const W r2 = sm4_bs_byte_rotl<2>(T[i]);
250 const W r3 = sm4_bs_byte_rotl<3>(T[i]);
251 const W r12 = r1 ^ r2;
253 E[i] = (i < 2) ? (r12 ^ r3) : (T[i] ^ r12);
256 for(
size_t i = 0; i != 6; ++i) {
257 X[i] ^= A[i] ^ E[i + 2];
263template <std::
unsigned_
integral W>
264BOTAN_FORCE_INLINE void SM4_round(W X0[8],
const W X1[8],
const W X2[8],
const W X3[8],
const W K[8]) {
266 for(
size_t i = 0; i != 8; ++i) {
267 T[i] = X1[i] ^ X2[i] ^ X3[i] ^ K[i];
279inline void sm4_bit_transpose(uint32_t B[8]) {
303[[maybe_unused]]
inline uint64_t sm4_bs_zip(uint32_t lo, uint32_t hi) {
304 uint64_t x = (
static_cast<uint64_t
>(hi) << 32) | lo;
310[[maybe_unused]]
inline void sm4_bs_unzip(uint64_t x, uint32_t& lo, uint32_t& hi) {
313 lo =
static_cast<uint32_t
>(x);
314 hi =
static_cast<uint32_t
>(x >> 32);
320template <std::
unsigned_
integral W>
321void sm4_bs_load(W P[8],
const uint8_t in[],
size_t w,
size_t n_blocks) {
322 constexpr size_t LANES = sm4_bs_lanes<W>();
324 uint32_t T[LANES] = {0};
325 for(
size_t k = 0; k != n_blocks; ++k) {
329 for(
size_t g = 0; g != LANES; g += 8) {
330 sm4_bit_transpose(&T[g]);
333 for(
size_t i = 0; i != 8; ++i) {
334 if constexpr(std::same_as<W, uint32_t>) {
337 P[i] = sm4_bs_zip(T[i], T[i + 8]);
345template <std::
unsigned_
integral W>
346void sm4_bs_store(uint8_t out[],
const W P[8],
size_t w,
size_t n_blocks) {
347 constexpr size_t LANES = sm4_bs_lanes<W>();
350 for(
size_t i = 0; i != 8; ++i) {
351 if constexpr(std::same_as<W, uint32_t>) {
354 sm4_bs_unzip(P[i], T[i], T[i + 8]);
358 for(
size_t g = 0; g != LANES; g += 8) {
359 sm4_bit_transpose(&T[g]);
362 for(
size_t k = 0; k != n_blocks; ++k) {
363 store_be(T[k], out + 16 * k + 4 * w);
371template <std::
unsigned_
integral W>
372W sm4_bs_key_plane(uint32_t rk,
size_t i) {
373 constexpr size_t LANES = sm4_bs_lanes<W>();
374 constexpr W lane_mask = (
static_cast<W
>(1) << LANES) - 1;
377 for(
size_t j = 0; j != 4; ++j) {
378 const W bit = (rk >> (8 * j + 7 - i)) & 1;
379 p |= (
static_cast<W
>(0) - bit) & (lane_mask << (LANES * j));
384template <
bool DECRYPT, std::
unsigned_
integral W>
385void sm4_bitsliced_blocks(
const uint8_t in[], uint8_t out[],
size_t blocks,
const W KS[32 * 8]) {
389 for(
size_t w = 0; w != 4; ++w) {
390 sm4_bs_load(X[w], in, w, blocks);
395 for(
size_t r = 0; r != 32; r += 4) {
396 auto key = [&](
size_t i) {
return &KS[8 * (DECRYPT ? 31 - i : i)]; };
397 SM4_round(X[0], X[1], X[2], X[3], key(r + 0));
398 SM4_round(X[1], X[2], X[3], X[0], key(r + 1));
399 SM4_round(X[2], X[3], X[0], X[1], key(r + 2));
400 SM4_round(X[3], X[0], X[1], X[2], key(r + 3));
405 for(
size_t w = 0; w != 4; ++w) {
406 sm4_bs_store(out, X[3 - w], w, blocks);
410template <
bool DECRYPT>
415 const size_t this_loop = std::min(blocks, SM4_BITSLICED_BLOCKS);
416 sm4_bitsliced_blocks<DECRYPT>(in, out, this_loop, KS.data());
417 in += this_loop * 16;
418 out += this_loop * 16;
426bool sm4_use_bitsliced() {
427#if defined(BOTAN_HAS_SM4_ARMV8)
433#if defined(BOTAN_HAS_SM4_X86)
439#if defined(BOTAN_HAS_SM4_AVX512_GFNI)
445#if defined(BOTAN_HAS_SM4_GFNI)
451#if defined(BOTAN_HAS_SM4_HWAES)
468#if defined(BOTAN_HAS_SM4_ARMV8)
470 return sm4_armv8_encrypt(in, out, blocks);
474#if defined(BOTAN_HAS_SM4_X86)
476 return sm4_x86_encrypt(in, out, blocks);
480#if defined(BOTAN_HAS_SM4_AVX512_GFNI)
482 return sm4_avx512_gfni_encrypt(in, out, blocks);
486#if defined(BOTAN_HAS_SM4_GFNI)
488 return sm4_gfni_encrypt(in, out, blocks);
492#if defined(BOTAN_HAS_SM4_HWAES)
494 return sm4_hwaes_encrypt(in, out, blocks);
498 sm4_bitsliced<false>(in, out, blocks, m_BSK);
507#if defined(BOTAN_HAS_SM4_ARMV8)
509 return sm4_armv8_decrypt(in, out, blocks);
513#if defined(BOTAN_HAS_SM4_X86)
515 return sm4_x86_decrypt(in, out, blocks);
519#if defined(BOTAN_HAS_SM4_AVX512_GFNI)
521 return sm4_avx512_gfni_decrypt(in, out, blocks);
525#if defined(BOTAN_HAS_SM4_GFNI)
527 return sm4_gfni_decrypt(in, out, blocks);
531#if defined(BOTAN_HAS_SM4_HWAES)
533 return sm4_hwaes_decrypt(in, out, blocks);
537 sm4_bitsliced<true>(in, out, blocks, m_BSK);
541 return !m_RK.empty();
549uint32_t SM4::SM4_Tp(uint32_t b) {
550#if defined(BOTAN_HAS_SM4_HWAES)
552 const uint32_t t = sm4_hwaes_sbox(b);
558 uint32_t T[8] = {b, 0, 0, 0, 0, 0, 0, 0};
559 sm4_bit_transpose(T);
561 sm4_bit_transpose(T);
563 const uint32_t t = T[0];
568void SM4::key_schedule(std::span<const uint8_t> key) {
570 const uint32_t FK[4] = {0xa3b1bac6, 0x56aa3350, 0x677d9197, 0xb27022dc};
572 const uint32_t CK[32] = {0x00070E15, 0x1C232A31, 0x383F464D, 0x545B6269, 0x70777E85, 0x8C939AA1, 0xA8AFB6BD,
573 0xC4CBD2D9, 0xE0E7EEF5, 0xFC030A11, 0x181F262D, 0x343B4249, 0x50575E65, 0x6C737A81,
574 0x888F969D, 0xA4ABB2B9, 0xC0C7CED5, 0xDCE3EAF1, 0xF8FF060D, 0x141B2229, 0x30373E45,
575 0x4C535A61, 0x686F767D, 0x848B9299, 0xA0A7AEB5, 0xBCC3CAD1, 0xD8DFE6ED, 0xF4FB0209,
576 0x10171E25, 0x2C333A41, 0x484F565D, 0x646B7279};
587 for(
size_t i = 0; i != 32; i += 4) {
588 K0 ^= SM4_Tp(K1 ^ K2 ^ K3 ^ CK[i + 0]);
589 K1 ^= SM4_Tp(K0 ^ K2 ^ K3 ^ CK[i + 1]);
590 K2 ^= SM4_Tp(K0 ^ K1 ^ K3 ^ CK[i + 2]);
591 K3 ^= SM4_Tp(K0 ^ K1 ^ K2 ^ CK[i + 3]);
598 if(sm4_use_bitsliced()) {
599 m_BSK.resize(32 * 8);
600 for(
size_t r = 0; r != 32; ++r) {
601 for(
size_t i = 0; i != 8; ++i) {
602 m_BSK[8 * r + i] = sm4_bs_key_plane<sm4_bs_word>(m_RK[r], i);
617#if defined(BOTAN_HAS_SM4_ARMV8)
623#if defined(BOTAN_HAS_SM4_X86)
629#if defined(BOTAN_HAS_SM4_AVX512_GFNI)
635#if defined(BOTAN_HAS_SM4_GFNI)
641#if defined(BOTAN_HAS_SM4_HWAES)
647 return SM4_BITSLICED_BLOCKS;
651#if defined(BOTAN_HAS_SM4_ARMV8)
657#if defined(BOTAN_HAS_SM4_X86)
663#if defined(BOTAN_HAS_SM4_AVX512_GFNI)
669#if defined(BOTAN_HAS_SM4_GFNI)
675#if defined(BOTAN_HAS_SM4_HWAES)
#define BOTAN_ASSERT_NOMSG(expr)
static std::optional< std::string > check(CPUID::Feature feat)
static bool has(CPUID::Feature feat)
std::string provider() const override
size_t parallelism() const override
void decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override
bool has_keying_material() const override
void encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override
void assert_key_material_set() const
#define BOTAN_FORCE_INLINE
constexpr void unpoison(const T *p, size_t n)
constexpr void poison(const T *p, size_t n)
void zap(std::vector< T, Alloc > &vec)
BOTAN_FORCE_INLINE constexpr void swap_bits(T &x, T &y, T mask, size_t shift)
BOTAN_FORCE_INLINE constexpr T bit_permute_step(T x, T mask, size_t shift)
BOTAN_FORCE_INLINE constexpr T rotl(T input)
std::vector< T, secure_allocator< T > > secure_vector
std::conditional_t< HasNative64BitRegisters, std::uint64_t, uint32_t > word
The native machine word, used as the limb type for multiprecision integers.
constexpr auto store_be(ParamTs &&... params)
constexpr auto load_be(ParamTs &&... params)