23#if (CRYPTOPP_AESNI_AVAILABLE)
25# include <emmintrin.h>
26# include <tmmintrin.h>
27# include <wmmintrin.h>
31extern const char SM4_SIMD_FNAME[] = __FILE__;
33ANONYMOUS_NAMESPACE_BEGIN
35using CryptoPP::word32;
37#if (CRYPTOPP_AESNI_AVAILABLE)
39template <
unsigned int R>
40inline __m128i ShiftLeft(
const __m128i& val)
42 return _mm_slli_epi32(val, R);
45template <
unsigned int R>
46inline __m128i ShiftRight(
const __m128i& val)
48 return _mm_srli_epi32(val, R);
51template <
unsigned int R>
52inline __m128i ShiftLeft64(
const __m128i& val)
54 return _mm_slli_epi64(val, R);
57template <
unsigned int R>
58inline __m128i ShiftRight64(
const __m128i& val)
60 return _mm_srli_epi64(val, R);
63template <
unsigned int R>
64inline __m128i RotateLeft(
const __m128i& val)
67 _mm_slli_epi32(val, R), _mm_srli_epi32(val, 32-R));
70template <
unsigned int R>
71inline __m128i RotateRight(
const __m128i& val)
74 _mm_slli_epi32(val, 32-R), _mm_srli_epi32(val, R));
78inline __m128i RotateLeft<8>(
const __m128i& val)
80 const __m128i r08 = _mm_set_epi32(0x0E0D0C0F, 0x0A09080B, 0x06050407, 0x02010003);
81 return _mm_shuffle_epi8(val, r08);
85inline __m128i RotateLeft<16>(
const __m128i& val)
87 const __m128i mask = _mm_set_epi32(0x0D0C0F0E, 0x09080B0A, 0x05040706, 0x01000302);
88 return _mm_shuffle_epi8(val, mask);
92inline __m128i RotateLeft<24>(
const __m128i& val)
94 const __m128i mask = _mm_set_epi32(0x0C0F0E0D, 0x080B0A09, 0x04070605, 0x00030201);
95 return _mm_shuffle_epi8(val, mask);
106template <
unsigned int IDX>
107inline __m128i UnpackXMM(
const __m128i& a,
const __m128i& b,
const __m128i& c,
const __m128i& d)
110 CRYPTOPP_UNUSED(a); CRYPTOPP_UNUSED(b);
111 CRYPTOPP_UNUSED(c); CRYPTOPP_UNUSED(d);
113 return _mm_setzero_si128();
117inline __m128i UnpackXMM<0>(
const __m128i& a,
const __m128i& b,
const __m128i& c,
const __m128i& d)
119 const __m128i r1 = _mm_unpacklo_epi32(a, b);
120 const __m128i r2 = _mm_unpacklo_epi32(c, d);
121 return _mm_unpacklo_epi64(r1, r2);
125inline __m128i UnpackXMM<1>(
const __m128i& a,
const __m128i& b,
const __m128i& c,
const __m128i& d)
127 const __m128i r1 = _mm_unpacklo_epi32(a, b);
128 const __m128i r2 = _mm_unpacklo_epi32(c, d);
129 return _mm_unpackhi_epi64(r1, r2);
133inline __m128i UnpackXMM<2>(
const __m128i& a,
const __m128i& b,
const __m128i& c,
const __m128i& d)
135 const __m128i r1 = _mm_unpackhi_epi32(a, b);
136 const __m128i r2 = _mm_unpackhi_epi32(c, d);
137 return _mm_unpacklo_epi64(r1, r2);
141inline __m128i UnpackXMM<3>(
const __m128i& a,
const __m128i& b,
const __m128i& c,
const __m128i& d)
143 const __m128i r1 = _mm_unpackhi_epi32(a, b);
144 const __m128i r2 = _mm_unpackhi_epi32(c, d);
145 return _mm_unpackhi_epi64(r1, r2);
153template <
unsigned int IDX>
154inline __m128i UnpackXMM(
const __m128i& v)
158 return _mm_setzero_si128();
162inline __m128i UnpackXMM<0>(
const __m128i& v)
165 return _mm_shuffle_epi8(v, _mm_set_epi8(3,2,1,0, 3,2,1,0, 3,2,1,0, 3,2,1,0));
169inline __m128i UnpackXMM<1>(
const __m128i& v)
172 return _mm_shuffle_epi8(v, _mm_set_epi8(7,6,5,4, 7,6,5,4, 7,6,5,4, 7,6,5,4));
176inline __m128i UnpackXMM<2>(
const __m128i& v)
179 return _mm_shuffle_epi8(v, _mm_set_epi8(11,10,9,8, 11,10,9,8, 11,10,9,8, 11,10,9,8));
183inline __m128i UnpackXMM<3>(
const __m128i& v)
186 return _mm_shuffle_epi8(v, _mm_set_epi8(15,14,13,12, 15,14,13,12, 15,14,13,12, 15,14,13,12));
189template <
unsigned int IDX>
190inline __m128i RepackXMM(
const __m128i& a,
const __m128i& b,
const __m128i& c,
const __m128i& d)
192 return UnpackXMM<IDX>(a, b, c, d);
195template <
unsigned int IDX>
196inline __m128i RepackXMM(
const __m128i& v)
198 return UnpackXMM<IDX>(v);
201inline void SM4_Encrypt(__m128i &block0, __m128i &block1,
202 __m128i &block2, __m128i &block3,
const word32 *subkeys)
205 const __m128i c0f = _mm_set_epi32(0x0F0F0F0F, 0x0F0F0F0F, 0x0F0F0F0F, 0x0F0F0F0F);
208 const __m128i flp = _mm_set_epi32(0x0C0D0E0F, 0x08090A0B, 0x04050607, 0x00010203);
211 const __m128i shr = _mm_set_epi32(0x0306090C, 0x0F020508, 0x0B0E0104, 0x070A0D00);
214 const __m128i m1l = _mm_set_epi32(0xC7C1B4B2, 0x22245157, 0x9197E2E4, 0x74720701);
215 const __m128i m1h = _mm_set_epi32(0xF052B91B, 0xF95BB012, 0xE240AB09, 0xEB49A200);
218 const __m128i m2l = _mm_set_epi32(0xEDD14478, 0x172BBE82, 0x5B67F2CE, 0xA19D0834);
219 const __m128i m2h = _mm_set_epi32(0x11CDBE62, 0xCC1063BF, 0xAE7201DD, 0x73AFDC00);
221 __m128i t0 = UnpackXMM<0>(block0, block1, block2, block3);
222 __m128i t1 = UnpackXMM<1>(block0, block1, block2, block3);
223 __m128i t2 = UnpackXMM<2>(block0, block1, block2, block3);
224 __m128i t3 = UnpackXMM<3>(block0, block1, block2, block3);
226 t0 = _mm_shuffle_epi8(t0, flp);
227 t1 = _mm_shuffle_epi8(t1, flp);
228 t2 = _mm_shuffle_epi8(t2, flp);
229 t3 = _mm_shuffle_epi8(t3, flp);
231 const unsigned int ROUNDS = 32;
232 for (
unsigned int i = 0; i < ROUNDS; i++)
234 const __m128i k = _mm_shuffle_epi32(_mm_castps_si128(
235 _mm_load_ss((
const float*)(subkeys+i))), _MM_SHUFFLE(0,0,0,0));
238 x = _mm_xor_si128(t1, _mm_xor_si128(t2, _mm_xor_si128(t3, k)));
240 y = _mm_and_si128(x, c0f);
241 y = _mm_shuffle_epi8(m1l, y);
242 x = _mm_and_si128(ShiftRight64<4>(x), c0f);
243 x = _mm_xor_si128(_mm_shuffle_epi8(m1h, x), y);
245 x = _mm_shuffle_epi8(x, shr);
246 x = _mm_aesenclast_si128(x, c0f);
248 y = _mm_andnot_si128(x, c0f);
249 y = _mm_shuffle_epi8(m2l, y);
250 x = _mm_and_si128(ShiftRight64<4>(x), c0f);
251 x = _mm_xor_si128(_mm_shuffle_epi8(m2h, x), y);
254 y = _mm_xor_si128(x, RotateLeft<8>(x));
255 y = _mm_xor_si128(y, RotateLeft<16>(x));
256 y = _mm_xor_si128(ShiftLeft<2>(y), ShiftRight<30>(y));
257 x = _mm_xor_si128(x, _mm_xor_si128(y, RotateLeft<24>(x)));
260 x = _mm_xor_si128(x, t0);
265 t0 = _mm_shuffle_epi8(t0, flp);
266 t1 = _mm_shuffle_epi8(t1, flp);
267 t2 = _mm_shuffle_epi8(t2, flp);
268 t3 = _mm_shuffle_epi8(t3, flp);
270 block0 = RepackXMM<0>(t3,t2,t1,t0);
271 block1 = RepackXMM<1>(t3,t2,t1,t0);
272 block2 = RepackXMM<2>(t3,t2,t1,t0);
273 block3 = RepackXMM<3>(t3,t2,t1,t0);
276inline void SM4_Enc_4_Blocks(__m128i &block0, __m128i &block1,
277 __m128i &block2, __m128i &block3,
const word32 *subkeys,
unsigned int )
279 SM4_Encrypt(block0, block1, block2, block3, subkeys);
282inline void SM4_Dec_4_Blocks(__m128i &block0, __m128i &block1,
283 __m128i &block2, __m128i &block3,
const word32 *subkeys,
unsigned int )
285 SM4_Encrypt(block0, block1, block2, block3, subkeys);
288inline void SM4_Enc_Block(__m128i &block0,
289 const word32 *subkeys,
unsigned int )
291 __m128i t1 = _mm_setzero_si128();
292 __m128i t2 = _mm_setzero_si128();
293 __m128i t3 = _mm_setzero_si128();
295 SM4_Encrypt(block0, t1, t2, t3, subkeys);
298inline void SM4_Dec_Block(__m128i &block0,
299 const word32 *subkeys,
unsigned int )
301 __m128i t1 = _mm_setzero_si128();
302 __m128i t2 = _mm_setzero_si128();
303 __m128i t3 = _mm_setzero_si128();
305 SM4_Encrypt(block0, t1, t2, t3, subkeys);
310ANONYMOUS_NAMESPACE_END
314#if defined(CRYPTOPP_AESNI_AVAILABLE)
315size_t SM4_Enc_AdvancedProcessBlocks_AESNI(
const word32* subKeys,
size_t rounds,
316 const byte *inBlocks,
const byte *xorBlocks,
byte *outBlocks,
size_t length,
word32 flags)
319 subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags);
Template for AdvancedProcessBlocks and SIMD processing.
size_t AdvancedProcessBlocks128_4x1_SSE(F1 func1, F4 func4, const W *subKeys, size_t rounds, const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags)
AdvancedProcessBlocks for 1 and 4 blocks.
Library configuration file.
unsigned int word32
32-bit unsigned datatype
Utility functions for the Crypto++ library.
Crypto++ library namespace.
Classes for the SM4 block cipher.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.