24 #if (CRYPTOPP_SSE2_INTRIN_AVAILABLE) 25 # include <xmmintrin.h> 26 # include <emmintrin.h> 29 #if (CRYPTOPP_AESNI_AVAILABLE) 30 # include <tmmintrin.h> 31 # include <wmmintrin.h> 34 #if (CRYPTOPP_ARM_NEON_AVAILABLE) && 0 35 # include <arm_neon.h> 40 #if (CRYPTOPP_ARM_ACLE_AVAILABLE) 42 # include <arm_acle.h> 46 extern const char SM4_SIMD_FNAME[] = __FILE__;
48 ANONYMOUS_NAMESPACE_BEGIN
50 using CryptoPP::word32;
52 #if (CRYPTOPP_AESNI_AVAILABLE) 54 template <
unsigned int R>
55 inline __m128i ShiftLeft(
const __m128i& val)
57 return _mm_slli_epi32(val, R);
60 template <
unsigned int R>
61 inline __m128i ShiftRight(
const __m128i& val)
63 return _mm_srli_epi32(val, R);
66 template <
unsigned int R>
67 inline __m128i ShiftLeft64(
const __m128i& val)
69 return _mm_slli_epi64(val, R);
72 template <
unsigned int R>
73 inline __m128i ShiftRight64(
const __m128i& val)
75 return _mm_srli_epi64(val, R);
78 template <
unsigned int R>
79 inline __m128i RotateLeft(
const __m128i& val)
82 _mm_slli_epi32(val, R), _mm_srli_epi32(val, 32-R));
85 template <
unsigned int R>
86 inline __m128i RotateRight(
const __m128i& val)
89 _mm_slli_epi32(val, 32-R), _mm_srli_epi32(val, R));
93 inline __m128i RotateLeft<8>(
const __m128i& val)
95 const __m128i r08 = _mm_set_epi32(0x0E0D0C0F, 0x0A09080B, 0x06050407, 0x02010003);
96 return _mm_shuffle_epi8(val, r08);
100 inline __m128i RotateLeft<16>(
const __m128i& val)
102 const __m128i mask = _mm_set_epi32(0x0D0C0F0E, 0x09080B0A, 0x05040706, 0x01000302);
103 return _mm_shuffle_epi8(val, mask);
107 inline __m128i RotateLeft<24>(
const __m128i& val)
109 const __m128i mask = _mm_set_epi32(0x0C0F0E0D, 0x080B0A09, 0x04070605, 0x00030201);
110 return _mm_shuffle_epi8(val, mask);
121 template <
unsigned int IDX>
122 inline __m128i UnpackXMM(
const __m128i& a,
const __m128i& b,
const __m128i& c,
const __m128i& d)
125 CRYPTOPP_UNUSED(a); CRYPTOPP_UNUSED(b);
126 CRYPTOPP_UNUSED(c); CRYPTOPP_UNUSED(d);
128 return _mm_setzero_si128();
132 inline __m128i UnpackXMM<0>(
const __m128i& a,
const __m128i& b,
const __m128i& c,
const __m128i& d)
134 const __m128i r1 = _mm_unpacklo_epi32(a, b);
135 const __m128i r2 = _mm_unpacklo_epi32(c, d);
136 return _mm_unpacklo_epi64(r1, r2);
140 inline __m128i UnpackXMM<1>(
const __m128i& a,
const __m128i& b,
const __m128i& c,
const __m128i& d)
142 const __m128i r1 = _mm_unpacklo_epi32(a, b);
143 const __m128i r2 = _mm_unpacklo_epi32(c, d);
144 return _mm_unpackhi_epi64(r1, r2);
148 inline __m128i UnpackXMM<2>(
const __m128i& a,
const __m128i& b,
const __m128i& c,
const __m128i& d)
150 const __m128i r1 = _mm_unpackhi_epi32(a, b);
151 const __m128i r2 = _mm_unpackhi_epi32(c, d);
152 return _mm_unpacklo_epi64(r1, r2);
156 inline __m128i UnpackXMM<3>(
const __m128i& a,
const __m128i& b,
const __m128i& c,
const __m128i& d)
158 const __m128i r1 = _mm_unpackhi_epi32(a, b);
159 const __m128i r2 = _mm_unpackhi_epi32(c, d);
160 return _mm_unpackhi_epi64(r1, r2);
168 template <
unsigned int IDX>
169 inline __m128i UnpackXMM(
const __m128i& v)
173 return _mm_setzero_si128();
177 inline __m128i UnpackXMM<0>(
const __m128i& v)
180 return _mm_shuffle_epi8(v, _mm_set_epi8(3,2,1,0, 3,2,1,0, 3,2,1,0, 3,2,1,0));
184 inline __m128i UnpackXMM<1>(
const __m128i& v)
187 return _mm_shuffle_epi8(v, _mm_set_epi8(7,6,5,4, 7,6,5,4, 7,6,5,4, 7,6,5,4));
191 inline __m128i UnpackXMM<2>(
const __m128i& v)
194 return _mm_shuffle_epi8(v, _mm_set_epi8(11,10,9,8, 11,10,9,8, 11,10,9,8, 11,10,9,8));
198 inline __m128i UnpackXMM<3>(
const __m128i& v)
201 return _mm_shuffle_epi8(v, _mm_set_epi8(15,14,13,12, 15,14,13,12, 15,14,13,12, 15,14,13,12));
204 template <
unsigned int IDX>
205 inline __m128i RepackXMM(
const __m128i& a,
const __m128i& b,
const __m128i& c,
const __m128i& d)
207 return UnpackXMM<IDX>(a, b, c, d);
210 template <
unsigned int IDX>
211 inline __m128i RepackXMM(
const __m128i& v)
213 return UnpackXMM<IDX>(v);
216 inline void SM4_Encrypt(__m128i &block0, __m128i &block1,
217 __m128i &block2, __m128i &block3,
const word32 *subkeys)
220 const __m128i c0f = _mm_set_epi32(0x0F0F0F0F, 0x0F0F0F0F, 0x0F0F0F0F, 0x0F0F0F0F);
223 const __m128i flp = _mm_set_epi32(0x0C0D0E0F, 0x08090A0B, 0x04050607, 0x00010203);
226 const __m128i shr = _mm_set_epi32(0x0306090C, 0x0F020508, 0x0B0E0104, 0x070A0D00);
229 const __m128i m1l = _mm_set_epi32(0xC7C1B4B2, 0x22245157, 0x9197E2E4, 0x74720701);
230 const __m128i m1h = _mm_set_epi32(0xF052B91B, 0xF95BB012, 0xE240AB09, 0xEB49A200);
233 const __m128i m2l = _mm_set_epi32(0xEDD14478, 0x172BBE82, 0x5B67F2CE, 0xA19D0834);
234 const __m128i m2h = _mm_set_epi32(0x11CDBE62, 0xCC1063BF, 0xAE7201DD, 0x73AFDC00);
236 __m128i t0 = UnpackXMM<0>(block0, block1, block2, block3);
237 __m128i t1 = UnpackXMM<1>(block0, block1, block2, block3);
238 __m128i t2 = UnpackXMM<2>(block0, block1, block2, block3);
239 __m128i t3 = UnpackXMM<3>(block0, block1, block2, block3);
241 t0 = _mm_shuffle_epi8(t0, flp);
242 t1 = _mm_shuffle_epi8(t1, flp);
243 t2 = _mm_shuffle_epi8(t2, flp);
244 t3 = _mm_shuffle_epi8(t3, flp);
246 const unsigned int ROUNDS = 32;
247 for (
unsigned int i = 0; i < ROUNDS; i++)
249 const __m128i k = _mm_shuffle_epi32(_mm_castps_si128(
250 _mm_load_ss((
const float*)(subkeys+i))), _MM_SHUFFLE(0,0,0,0));
253 x = _mm_xor_si128(t1, _mm_xor_si128(t2, _mm_xor_si128(t3, k)));
255 y = _mm_and_si128(x, c0f);
256 y = _mm_shuffle_epi8(m1l, y);
257 x = _mm_and_si128(ShiftRight64<4>(x), c0f);
258 x = _mm_xor_si128(_mm_shuffle_epi8(m1h, x), y);
260 x = _mm_shuffle_epi8(x, shr);
261 x = _mm_aesenclast_si128(x, c0f);
263 y = _mm_andnot_si128(x, c0f);
264 y = _mm_shuffle_epi8(m2l, y);
265 x = _mm_and_si128(ShiftRight64<4>(x), c0f);
266 x = _mm_xor_si128(_mm_shuffle_epi8(m2h, x), y);
269 y = _mm_xor_si128(x, RotateLeft<8>(x));
270 y = _mm_xor_si128(y, RotateLeft<16>(x));
271 y = _mm_xor_si128(ShiftLeft<2>(y), ShiftRight<30>(y));
272 x = _mm_xor_si128(x, _mm_xor_si128(y, RotateLeft<24>(x)));
275 x = _mm_xor_si128(x, t0);
280 t0 = _mm_shuffle_epi8(t0, flp);
281 t1 = _mm_shuffle_epi8(t1, flp);
282 t2 = _mm_shuffle_epi8(t2, flp);
283 t3 = _mm_shuffle_epi8(t3, flp);
285 block0 = RepackXMM<0>(t3,t2,t1,t0);
286 block1 = RepackXMM<1>(t3,t2,t1,t0);
287 block2 = RepackXMM<2>(t3,t2,t1,t0);
288 block3 = RepackXMM<3>(t3,t2,t1,t0);
291 inline void SM4_Enc_4_Blocks(__m128i &block0, __m128i &block1,
292 __m128i &block2, __m128i &block3,
const word32 *subkeys,
unsigned int )
294 SM4_Encrypt(block0, block1, block2, block3, subkeys);
297 inline void SM4_Dec_4_Blocks(__m128i &block0, __m128i &block1,
298 __m128i &block2, __m128i &block3,
const word32 *subkeys,
unsigned int )
300 SM4_Encrypt(block0, block1, block2, block3, subkeys);
303 inline void SM4_Enc_Block(__m128i &block0,
304 const word32 *subkeys,
unsigned int )
306 __m128i t1 = _mm_setzero_si128();
307 __m128i t2 = _mm_setzero_si128();
308 __m128i t3 = _mm_setzero_si128();
310 SM4_Encrypt(block0, t1, t2, t3, subkeys);
313 inline void SM4_Dec_Block(__m128i &block0,
314 const word32 *subkeys,
unsigned int )
316 __m128i t1 = _mm_setzero_si128();
317 __m128i t2 = _mm_setzero_si128();
318 __m128i t3 = _mm_setzero_si128();
320 SM4_Encrypt(block0, t1, t2, t3, subkeys);
323 #endif // CRYPTOPP_AESNI_AVAILABLE 325 ANONYMOUS_NAMESPACE_END
329 #if defined(CRYPTOPP_AESNI_AVAILABLE) 330 size_t SM4_Enc_AdvancedProcessBlocks_AESNI(
const word32* subKeys,
size_t rounds,
331 const byte *inBlocks,
const byte *xorBlocks, byte *outBlocks,
size_t length, word32 flags)
333 return AdvancedProcessBlocks128_4x1_SSE(SM4_Enc_Block, SM4_Enc_4_Blocks,
334 subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags);
336 #endif // CRYPTOPP_AESNI_AVAILABLE 338 #if defined(CRYPTOPP_ARM_NEON_AVAILABLE) && 0 339 size_t SM4_Enc_AdvancedProcessBlocks_NEON(
const word32* subKeys,
size_t rounds,
340 const byte *inBlocks,
const byte *xorBlocks, byte *outBlocks,
size_t length, word32 flags)
343 return AdvancedProcessBlocks128_4x1_NEON(SM4_Enc_Block, SM4_Enc_4_Blocks,
344 unused, subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags);
347 size_t SM4_Dec_AdvancedProcessBlocks_NEON(
const word32* subKeys,
size_t rounds,
348 const byte *inBlocks,
const byte *xorBlocks, byte *outBlocks,
size_t length, word32 flags)
351 return AdvancedProcessBlocks128_4x1_NEON(SM4_Dec_Block, SM4_Dec_4_Blocks,
352 unused, subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags);
354 #endif // CRYPTOPP_ARM_NEON_AVAILABLE Utility functions for the Crypto++ library.
Library configuration file.
Template for AdvancedProcessBlocks and SIMD processing.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Classes for the SM4 block cipher.
Crypto++ library namespace.