Botan 3.14.1
Crypto and TLS for C&
sm4.cpp
Go to the documentation of this file.
1/*
2* SM4
3* (C) 2017 Ribose Inc
4* (C) 2018,2026 Jack Lloyd
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/internal/sm4.h>
10
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>
15
16#if defined(BOTAN_HAS_CPUID)
17 #include <botan/internal/cpuid.h>
18#endif
19
20namespace Botan {
21
22namespace {
23
24/*
25The baseline implementation is constant time and bitsliced.
26
27The SM4 sbox is an affine equivalent of the AES sbox; both are inversion
28in GF(2^8) composed with affine transformations, and the two fields are
29isomorphic. Following https://eprint.iacr.org/2022/1154 section 3.3 (the
30same decomposition used by sm4_gfni and sm4_hwaes),
31
32 S_SM4(x) = A2 * S_AES(A1 * x ^ 0x3E) ^ 0x6C
33
34where A1 is the input matrix given in the paper and A2 is the paper's
35output matrix composed with the inverse of the AES sbox's own output
36affine transformation.
37
38The sbox circuit is derived from the size 113 AES sbox circuit of the
39"Circuit Minimization Team" that aes.cpp uses, which is a linear top
40layer expanding the 8 inputs to 22 signals, a nonlinear middle section,
41and a linear bottom layer. Affine equivalence only changes the outer
42linear layers, so the middle section is used unchanged, while A1 and A2
43are folded into the top and bottom layers respectively, each re-optimized
44with the Boyar-Peralta SLP heuristic (27 and 35 gates, 124 in total).
45The same construction, applied to the depth 16 variant of the circuit,
46appears in "A Lightweight ISA Extension for AES and SM4"
47(https://arxiv.org/abs/2002.07041).
48
49Bitsliced layout
50
51The state is kept in bitsliced form for the entire cipher. Each of the
52four 32-bit state words is represented by eight plane words; plane i
53holds bit 7-i of every byte. Within a plane, the bits of byte j from all
54blocks in the batch occupy the group of LANES consecutive bits starting
55at bit LANES*j, where LANES is the number of blocks per batch: 8 with
5632-bit words and 16 with 64-bit words.
57
58In this layout the round key becomes a set of plane constants, the sbox
59is the circuit above applied plane-wise, and the linear layer L (rotations
60of the 32-bit word by 2, 10, 18 and 24) becomes a fixed permutation of the
61planes combined with rotations of the plane words by multiples of LANES.
62*/
63
64using sm4_bs_word = word;
65static_assert(std::same_as<sm4_bs_word, uint32_t> || std::same_as<sm4_bs_word, uint64_t>);
66
67template <std::unsigned_integral W>
68constexpr size_t sm4_bs_lanes() {
69 return 8 * sizeof(W) / 4;
70}
71
72constexpr size_t SM4_BITSLICED_BLOCKS = sm4_bs_lanes<sm4_bs_word>();
73
74/**
75* Rotate a plane word by N bytes of the original 32-bit word
76*/
77template <size_t N, std::unsigned_integral W>
78BOTAN_FORCE_INLINE constexpr W sm4_bs_byte_rotl(W x) {
79 return rotl<N * sm4_bs_lanes<W>()>(x);
80}
81
82/*
83* Apply the SM4 sbox to the eight planes B[]
84*/
85template <std::unsigned_integral W>
86BOTAN_FORCE_INLINE void SM4_SBOX(W B[8]) {
87 // Top linear layer: A1 (with constant 0x3E) merged with the AES
88 // circuit's top layer
89 const W a0 = ~B[3];
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;
97 const W y8 = ~B[2];
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;
116
117 // The nonlinear middle section of the AES sbox circuit, from the
118 // "Circuit Minimization Team" group
119 // http://www.cs.yale.edu/homes/peralta/CircuitStuff/CMT.html
120 // http://www.cs.yale.edu/homes/peralta/CircuitStuff/SLP_AES_113.txt
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;
183
184 // Bottom linear layer: the AES circuit's bottom layer merged with A2
185 // (with constant 0x6C)
186 const W c0 = z0 ^ z7;
187 const W c1 = z2 ^ z4;
188 const W c2 = z11 ^ z14;
189 const W c3 = ~z9;
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;
203 const W c16 = ~c4;
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;
221
222 B[0] = B0;
223 B[1] = B1;
224 B[2] = B2;
225 B[3] = B3;
226 B[4] = B4;
227 B[5] = B5;
228 B[6] = B6;
229 B[7] = B7;
230}
231
232/*
233* Apply the linear layer L to the planes T[] and xor the result into X[]
234*
235* Written in terms of rotations of the underlying 32-bit word, L(t) is
236* t ^ rotl<2>(t) ^ rotl<10>(t) ^ rotl<18>(t) ^ rotl<24>(t). A rotation by
237* 8*n moves each byte to byte n higher, which is a rotation of each plane
238* word by n lane groups. A rotation by 2 moves bit b of a byte to bit b+2,
239* which is plane i to plane i-2, except for the top two bits, which move
240* into the next byte and become planes 6 and 7.
241*/
242template <std::unsigned_integral W>
243BOTAN_FORCE_INLINE void SM4_L_xor(W X[8], const W T[8]) {
244 W A[8];
245 W E[8];
246
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;
252 A[i] = T[i] ^ r3;
253 E[i] = (i < 2) ? (r12 ^ r3) : (T[i] ^ r12);
254 }
255
256 for(size_t i = 0; i != 6; ++i) {
257 X[i] ^= A[i] ^ E[i + 2];
258 }
259 X[6] ^= A[6] ^ E[0];
260 X[7] ^= A[7] ^ E[1];
261}
262
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]) {
265 W T[8];
266 for(size_t i = 0; i != 8; ++i) {
267 T[i] = X1[i] ^ X2[i] ^ X3[i] ^ K[i];
268 }
269 SM4_SBOX(T);
270 SM4_L_xor(X0, T);
271}
272
273/*
274* Transpose the 8x8 bit matrix formed by the bytes of B[]. Afterwards,
275* plane B[i] holds bit 7-i of each byte, and the lane order within each
276* byte is reversed; the transpose is an involution so this cancels when
277* converting back.
278*/
279inline void sm4_bit_transpose(uint32_t B[8]) {
280 swap_bits<uint32_t>(B[1], B[0], 0x55555555, 1);
281 swap_bits<uint32_t>(B[3], B[2], 0x55555555, 1);
282 swap_bits<uint32_t>(B[5], B[4], 0x55555555, 1);
283 swap_bits<uint32_t>(B[7], B[6], 0x55555555, 1);
284
285 swap_bits<uint32_t>(B[2], B[0], 0x33333333, 2);
286 swap_bits<uint32_t>(B[3], B[1], 0x33333333, 2);
287 swap_bits<uint32_t>(B[6], B[4], 0x33333333, 2);
288 swap_bits<uint32_t>(B[7], B[5], 0x33333333, 2);
289
290 swap_bits<uint32_t>(B[4], B[0], 0x0F0F0F0F, 4);
291 swap_bits<uint32_t>(B[5], B[1], 0x0F0F0F0F, 4);
292 swap_bits<uint32_t>(B[6], B[2], 0x0F0F0F0F, 4);
293 swap_bits<uint32_t>(B[7], B[3], 0x0F0F0F0F, 4);
294}
295
296/*
297* Interleave the bytes of two 32-bit planes into one 64-bit plane with
298* 16-bit lane groups: byte j of lo becomes the low half of group j and
299* byte j of hi the high half.
300*
301* Only used for 64-bit
302*/
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;
305 x = bit_permute_step<uint64_t>(x, 0x00000000FFFF0000, 16);
306 x = bit_permute_step<uint64_t>(x, 0x0000FF000000FF00, 8);
307 return x;
308}
309
310[[maybe_unused]] inline void sm4_bs_unzip(uint64_t x, uint32_t& lo, uint32_t& hi) {
311 x = bit_permute_step<uint64_t>(x, 0x0000FF000000FF00, 8);
312 x = bit_permute_step<uint64_t>(x, 0x00000000FFFF0000, 16);
313 lo = static_cast<uint32_t>(x);
314 hi = static_cast<uint32_t>(x >> 32);
315}
316
317/*
318* Load word w of each of the n_blocks input blocks into the planes P[]
319*/
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>();
323
324 uint32_t T[LANES] = {0};
325 for(size_t k = 0; k != n_blocks; ++k) {
326 T[k] = load_be<uint32_t>(in + 16 * k, w);
327 }
328
329 for(size_t g = 0; g != LANES; g += 8) {
330 sm4_bit_transpose(&T[g]);
331 }
332
333 for(size_t i = 0; i != 8; ++i) {
334 if constexpr(std::same_as<W, uint32_t>) {
335 P[i] = T[i];
336 } else {
337 P[i] = sm4_bs_zip(T[i], T[i + 8]);
338 }
339 }
340}
341
342/*
343* Store the planes P[] as word w of each of the n_blocks output blocks
344*/
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>();
348
349 uint32_t T[LANES];
350 for(size_t i = 0; i != 8; ++i) {
351 if constexpr(std::same_as<W, uint32_t>) {
352 T[i] = P[i];
353 } else {
354 sm4_bs_unzip(P[i], T[i], T[i + 8]);
355 }
356 }
357
358 for(size_t g = 0; g != LANES; g += 8) {
359 sm4_bit_transpose(&T[g]);
360 }
361
362 for(size_t k = 0; k != n_blocks; ++k) {
363 store_be(T[k], out + 16 * k + 4 * w);
364 }
365}
366
367/*
368* Expand round key rk into plane i: each lane group j is all ones if
369* bit 7-i of byte j of rk is set
370*/
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;
375
376 W p = 0;
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));
380 }
381 return p;
382}
383
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]) {
386 BOTAN_ASSERT_NOMSG(blocks >= 1 && blocks <= sm4_bs_lanes<W>());
387
388 W X[4][8];
389 for(size_t w = 0; w != 4; ++w) {
390 sm4_bs_load(X[w], in, w, blocks);
391 }
392
393 CT::poison(&X[0][0], 4 * 8);
394
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));
401 }
402
403 CT::unpoison(&X[0][0], 4 * 8);
404
405 for(size_t w = 0; w != 4; ++w) {
406 sm4_bs_store(out, X[3 - w], w, blocks);
407 }
408}
409
410template <bool DECRYPT>
411void sm4_bitsliced(const uint8_t in[], uint8_t out[], size_t blocks, const secure_vector<sm4_bs_word>& KS) {
412 BOTAN_ASSERT_NOMSG(KS.size() == 32 * 8);
413
414 while(blocks > 0) {
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;
419 blocks -= this_loop;
420 }
421}
422
423/*
424* Returns true if the bitsliced implementation will be used
425*/
426bool sm4_use_bitsliced() {
427#if defined(BOTAN_HAS_SM4_ARMV8)
429 return false;
430 }
431#endif
432
433#if defined(BOTAN_HAS_SM4_X86)
435 return false;
436 }
437#endif
438
439#if defined(BOTAN_HAS_SM4_AVX512_GFNI)
441 return false;
442 }
443#endif
444
445#if defined(BOTAN_HAS_SM4_GFNI)
447 return false;
448 }
449#endif
450
451#if defined(BOTAN_HAS_SM4_HWAES)
453 return false;
454 }
455#endif
456
457 return true;
458}
459
460} // namespace
461
462/*
463* SM4 Encryption
464*/
465void SM4::encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const {
467
468#if defined(BOTAN_HAS_SM4_ARMV8)
470 return sm4_armv8_encrypt(in, out, blocks);
471 }
472#endif
473
474#if defined(BOTAN_HAS_SM4_X86)
476 return sm4_x86_encrypt(in, out, blocks);
477 }
478#endif
479
480#if defined(BOTAN_HAS_SM4_AVX512_GFNI)
482 return sm4_avx512_gfni_encrypt(in, out, blocks);
483 }
484#endif
485
486#if defined(BOTAN_HAS_SM4_GFNI)
488 return sm4_gfni_encrypt(in, out, blocks);
489 }
490#endif
491
492#if defined(BOTAN_HAS_SM4_HWAES)
494 return sm4_hwaes_encrypt(in, out, blocks);
495 }
496#endif
497
498 sm4_bitsliced<false>(in, out, blocks, m_BSK);
499}
500
501/*
502* SM4 Decryption
503*/
504void SM4::decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const {
506
507#if defined(BOTAN_HAS_SM4_ARMV8)
509 return sm4_armv8_decrypt(in, out, blocks);
510 }
511#endif
512
513#if defined(BOTAN_HAS_SM4_X86)
515 return sm4_x86_decrypt(in, out, blocks);
516 }
517#endif
518
519#if defined(BOTAN_HAS_SM4_AVX512_GFNI)
521 return sm4_avx512_gfni_decrypt(in, out, blocks);
522 }
523#endif
524
525#if defined(BOTAN_HAS_SM4_GFNI)
527 return sm4_gfni_decrypt(in, out, blocks);
528 }
529#endif
530
531#if defined(BOTAN_HAS_SM4_HWAES)
533 return sm4_hwaes_decrypt(in, out, blocks);
534 }
535#endif
536
537 sm4_bitsliced<true>(in, out, blocks, m_BSK);
538}
539
541 return !m_RK.empty();
542}
543
544/*
545* SM4 Key Schedule
546*/
547
548//static
549uint32_t SM4::SM4_Tp(uint32_t b) {
550#if defined(BOTAN_HAS_SM4_HWAES)
552 const uint32_t t = sm4_hwaes_sbox(b);
553 // L' linear transform
554 return t ^ rotl<13>(t) ^ rotl<23>(t);
555 }
556#endif
557
558 uint32_t T[8] = {b, 0, 0, 0, 0, 0, 0, 0};
559 sm4_bit_transpose(T);
560 SM4_SBOX(T);
561 sm4_bit_transpose(T);
562
563 const uint32_t t = T[0];
564 // L' linear transform
565 return t ^ rotl<13>(t) ^ rotl<23>(t);
566}
567
568void SM4::key_schedule(std::span<const uint8_t> key) {
569 // System parameter or family key
570 const uint32_t FK[4] = {0xa3b1bac6, 0x56aa3350, 0x677d9197, 0xb27022dc};
571
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};
577
578 CT::poison(key.data(), key.size());
579
580 uint32_t K0 = load_be<uint32_t>(key.data(), 0) ^ FK[0];
581 uint32_t K1 = load_be<uint32_t>(key.data(), 1) ^ FK[1];
582 uint32_t K2 = load_be<uint32_t>(key.data(), 2) ^ FK[2];
583 uint32_t K3 = load_be<uint32_t>(key.data(), 3) ^ FK[3];
584
585 m_RK.resize(32);
586
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]);
592 m_RK[i + 0] = K0;
593 m_RK[i + 1] = K1;
594 m_RK[i + 2] = K2;
595 m_RK[i + 3] = K3;
596 }
597
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);
603 }
604 }
605 }
606
607 CT::unpoison(m_RK.data(), m_RK.size());
608 CT::unpoison(key.data(), key.size());
609}
610
612 zap(m_RK);
613 zap(m_BSK);
614}
615
616size_t SM4::parallelism() const {
617#if defined(BOTAN_HAS_SM4_ARMV8)
619 return 4;
620 }
621#endif
622
623#if defined(BOTAN_HAS_SM4_X86)
625 return 8;
626 }
627#endif
628
629#if defined(BOTAN_HAS_SM4_AVX512_GFNI)
631 return 16;
632 }
633#endif
634
635#if defined(BOTAN_HAS_SM4_GFNI)
637 return 8;
638 }
639#endif
640
641#if defined(BOTAN_HAS_SM4_HWAES)
643 return 4;
644 }
645#endif
646
647 return SM4_BITSLICED_BLOCKS;
648}
649
650std::string SM4::provider() const {
651#if defined(BOTAN_HAS_SM4_ARMV8)
652 if(auto feat = CPUID::check(CPUID::Feature::SM4)) {
653 return *feat;
654 }
655#endif
656
657#if defined(BOTAN_HAS_SM4_X86)
658 if(auto feat = CPUID::check(CPUID::Feature::SM4)) {
659 return *feat;
660 }
661#endif
662
663#if defined(BOTAN_HAS_SM4_AVX512_GFNI)
665 return *feat;
666 }
667#endif
668
669#if defined(BOTAN_HAS_SM4_GFNI)
670 if(auto feat = CPUID::check(CPUID::Feature::GFNI)) {
671 return *feat;
672 }
673#endif
674
675#if defined(BOTAN_HAS_SM4_HWAES)
676 if(auto feat = CPUID::check(CPUID::Feature::HW_AES)) {
677 return *feat;
678 }
679#endif
680
681 return "base";
682}
683
684} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82
static std::optional< std::string > check(CPUID::Feature feat)
Definition cpuid.h:66
static bool has(CPUID::Feature feat)
Definition cpuid.h:93
std::string provider() const override
Definition sm4.cpp:650
size_t parallelism() const override
Definition sm4.cpp:616
void decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override
Definition sm4.cpp:504
bool has_keying_material() const override
Definition sm4.cpp:540
void encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override
Definition sm4.cpp:465
void clear() override
Definition sm4.cpp:611
#define BOTAN_FORCE_INLINE
Definition compiler.h:86
constexpr void unpoison(const T *p, size_t n)
Definition ct_utils.h:67
constexpr void poison(const T *p, size_t n)
Definition ct_utils.h:56
constexpr uint32_t K1
Definition sha1_f.h:16
constexpr uint32_t K3
Definition sha1_f.h:18
constexpr uint32_t K2
Definition sha1_f.h:17
void zap(std::vector< T, Alloc > &vec)
Definition secmem.h:261
BOTAN_FORCE_INLINE constexpr void swap_bits(T &x, T &y, T mask, size_t shift)
Definition bit_ops.h:203
BOTAN_FORCE_INLINE constexpr T bit_permute_step(T x, T mask, size_t shift)
Definition bit_ops.h:193
BOTAN_FORCE_INLINE constexpr T rotl(T input)
Definition rotate.h:23
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128
std::conditional_t< HasNative64BitRegisters, std::uint64_t, uint32_t > word
The native machine word, used as the limb type for multiprecision integers.
Definition types.h:131
constexpr auto store_be(ParamTs &&... params)
Definition loadstor.h:745
constexpr auto load_be(ParamTs &&... params)
Definition loadstor.h:504