34#if defined(__GNUC__) && (__GNUC__ < 4)
35# undef CRYPTOPP_SSE2_INTRIN_AVAILABLE
38#if (CRYPTOPP_SSE2_INTRIN_AVAILABLE)
39# include <xmmintrin.h>
40# include <emmintrin.h>
44# include <tmmintrin.h>
48# if defined(CRYPTOPP_GCC_COMPATIBLE)
49# include <x86intrin.h>
51# include <ammintrin.h>
54#if (CRYPTOPP_ARM_NEON_HEADER)
58#if (CRYPTOPP_ARM_ACLE_HEADER)
63#if defined(CRYPTOPP_ALTIVEC_AVAILABLE)
68extern const char CHACHA_SIMD_FNAME[] = __FILE__;
70ANONYMOUS_NAMESPACE_BEGIN
74#if (CRYPTOPP_ARM_NEON_AVAILABLE)
76template <
unsigned int R>
77inline uint32x4_t RotateLeft(
const uint32x4_t& val)
79 return vorrq_u32(vshlq_n_u32(val, R), vshrq_n_u32(val, 32 - R));
82template <
unsigned int R>
83inline uint32x4_t RotateRight(
const uint32x4_t& val)
85 return vorrq_u32(vshlq_n_u32(val, 32 - R), vshrq_n_u32(val, R));
89inline uint32x4_t RotateLeft<8>(
const uint32x4_t& val)
91#if defined(__aarch32__) || defined(__aarch64__)
92 const uint8_t maskb[16] = { 3,0,1,2, 7,4,5,6, 11,8,9,10, 15,12,13,14 };
93 const uint8x16_t mask = vld1q_u8(maskb);
95 return vreinterpretq_u32_u8(
96 vqtbl1q_u8(vreinterpretq_u8_u32(val), mask));
99 return vorrq_u32(vshlq_n_u32(val, 8),
100 vshrq_n_u32(val, 32 - 8));
105inline uint32x4_t RotateLeft<16>(
const uint32x4_t& val)
107#if defined(__aarch32__) || defined(__aarch64__)
108 return vreinterpretq_u32_u16(
109 vrev32q_u16(vreinterpretq_u16_u32(val)));
112 return vorrq_u32(vshlq_n_u32(val, 16),
113 vshrq_n_u32(val, 32 - 16));
118inline uint32x4_t RotateRight<8>(
const uint32x4_t& val)
120#if defined(__aarch32__) || defined(__aarch64__)
121 const uint8_t maskb[16] = { 1,2,3,0, 5,6,7,4, 9,10,11,8, 13,14,15,12 };
122 const uint8x16_t mask = vld1q_u8(maskb);
124 return vreinterpretq_u32_u8(
125 vqtbl1q_u8(vreinterpretq_u8_u32(val), mask));
128 return vorrq_u32(vshrq_n_u32(val, 8),
129 vshlq_n_u32(val, 32 - 8));
134inline uint32x4_t RotateRight<16>(
const uint32x4_t& val)
136#if defined(__aarch32__) || defined(__aarch64__)
137 return vreinterpretq_u32_u16(
138 vrev32q_u16(vreinterpretq_u16_u32(val)));
141 return vorrq_u32(vshrq_n_u32(val, 16),
142 vshlq_n_u32(val, 32 - 16));
151template <
unsigned int S>
152inline uint32x4_t Extract(
const uint32x4_t& val)
154 return vextq_u32(val, val, S);
158inline uint32x4_t Add64(
const uint32x4_t& a,
const uint32x4_t& b)
160 return vreinterpretq_u32_u64(
162 vreinterpretq_u64_u32(a),
163 vreinterpretq_u64_u32(b)));
170#if (CRYPTOPP_SSE2_INTRIN_AVAILABLE)
172template <
unsigned int R>
173inline __m128i RotateLeft(
const __m128i val)
176 return _mm_roti_epi32(val, R);
178 return _mm_or_si128(_mm_slli_epi32(val, R), _mm_srli_epi32(val, 32-R));
183inline __m128i RotateLeft<8>(
const __m128i val)
186 return _mm_roti_epi32(val, 8);
187#elif defined(__SSSE3__)
188 const __m128i mask = _mm_set_epi8(14,13,12,15, 10,9,8,11, 6,5,4,7, 2,1,0,3);
189 return _mm_shuffle_epi8(val, mask);
191 return _mm_or_si128(_mm_slli_epi32(val, 8), _mm_srli_epi32(val, 32-8));
196inline __m128i RotateLeft<16>(
const __m128i val)
199 return _mm_roti_epi32(val, 16);
200#elif defined(__SSSE3__)
201 const __m128i mask = _mm_set_epi8(13,12,15,14, 9,8,11,10, 5,4,7,6, 1,0,3,2);
202 return _mm_shuffle_epi8(val, mask);
204 return _mm_or_si128(_mm_slli_epi32(val, 16), _mm_srli_epi32(val, 32-16));
212#if (CRYPTOPP_ALTIVEC_AVAILABLE)
223using CryptoPP::uint8x16_p;
224using CryptoPP::uint32x4_p;
225using CryptoPP::VecLoad;
226using CryptoPP::VecLoadAligned;
227using CryptoPP::VecStore;
228using CryptoPP::VecPermute;
233inline uint32x4_p VecLoad32LE(
const uint8_t src[16])
235#if (CRYPTOPP_BIG_ENDIAN)
236 const uint8x16_p mask = {3,2,1,0, 7,6,5,4, 11,10,9,8, 15,14,13,12};
247inline void VecStore32LE(uint8_t dest[16],
const uint32x4_p& val)
249#if (CRYPTOPP_BIG_ENDIAN)
250 const uint8x16_p mask = {3,2,1,0, 7,6,5,4, 11,10,9,8, 15,14,13,12};
262template <
unsigned int S>
272 const uint8x16_p mask = {4,5,6,7, 8,9,10,11, 12,13,14,15, 0,1,2,3};
279 const uint8x16_p mask = {8,9,10,11, 12,13,14,15, 0,1,2,3, 4,5,6,7};
286 const uint8x16_p mask = {12,13,14,15, 0,1,2,3, 4,5,6,7, 8,9,10,11};
292ANONYMOUS_NAMESPACE_END
298#if (CRYPTOPP_ARM_NEON_AVAILABLE)
300void ChaCha_OperateKeystream_NEON(
const word32 *state,
const byte* input,
byte *output,
unsigned int rounds)
302 const uint32x4_t state0 = vld1q_u32(state + 0*4);
303 const uint32x4_t state1 = vld1q_u32(state + 1*4);
304 const uint32x4_t state2 = vld1q_u32(state + 2*4);
305 const uint32x4_t state3 = vld1q_u32(state + 3*4);
307 const unsigned int w[] = {1,0,0,0, 2,0,0,0, 3,0,0,0};
308 const uint32x4_t CTRS[3] = {
309 vld1q_u32(w+0), vld1q_u32(w+4), vld1q_u32(w+8)
312 uint32x4_t r0_0 = state0;
313 uint32x4_t r0_1 = state1;
314 uint32x4_t r0_2 = state2;
315 uint32x4_t r0_3 = state3;
317 uint32x4_t r1_0 = state0;
318 uint32x4_t r1_1 = state1;
319 uint32x4_t r1_2 = state2;
320 uint32x4_t r1_3 = Add64(r0_3, CTRS[0]);
322 uint32x4_t r2_0 = state0;
323 uint32x4_t r2_1 = state1;
324 uint32x4_t r2_2 = state2;
325 uint32x4_t r2_3 = Add64(r0_3, CTRS[1]);
327 uint32x4_t r3_0 = state0;
328 uint32x4_t r3_1 = state1;
329 uint32x4_t r3_2 = state2;
330 uint32x4_t r3_3 = Add64(r0_3, CTRS[2]);
332 for (
int i =
static_cast<int>(rounds); i > 0; i -= 2)
334 r0_0 = vaddq_u32(r0_0, r0_1);
335 r1_0 = vaddq_u32(r1_0, r1_1);
336 r2_0 = vaddq_u32(r2_0, r2_1);
337 r3_0 = vaddq_u32(r3_0, r3_1);
339 r0_3 = veorq_u32(r0_3, r0_0);
340 r1_3 = veorq_u32(r1_3, r1_0);
341 r2_3 = veorq_u32(r2_3, r2_0);
342 r3_3 = veorq_u32(r3_3, r3_0);
344 r0_3 = RotateLeft<16>(r0_3);
345 r1_3 = RotateLeft<16>(r1_3);
346 r2_3 = RotateLeft<16>(r2_3);
347 r3_3 = RotateLeft<16>(r3_3);
349 r0_2 = vaddq_u32(r0_2, r0_3);
350 r1_2 = vaddq_u32(r1_2, r1_3);
351 r2_2 = vaddq_u32(r2_2, r2_3);
352 r3_2 = vaddq_u32(r3_2, r3_3);
354 r0_1 = veorq_u32(r0_1, r0_2);
355 r1_1 = veorq_u32(r1_1, r1_2);
356 r2_1 = veorq_u32(r2_1, r2_2);
357 r3_1 = veorq_u32(r3_1, r3_2);
359 r0_1 = RotateLeft<12>(r0_1);
360 r1_1 = RotateLeft<12>(r1_1);
361 r2_1 = RotateLeft<12>(r2_1);
362 r3_1 = RotateLeft<12>(r3_1);
364 r0_0 = vaddq_u32(r0_0, r0_1);
365 r1_0 = vaddq_u32(r1_0, r1_1);
366 r2_0 = vaddq_u32(r2_0, r2_1);
367 r3_0 = vaddq_u32(r3_0, r3_1);
369 r0_3 = veorq_u32(r0_3, r0_0);
370 r1_3 = veorq_u32(r1_3, r1_0);
371 r2_3 = veorq_u32(r2_3, r2_0);
372 r3_3 = veorq_u32(r3_3, r3_0);
374 r0_3 = RotateLeft<8>(r0_3);
375 r1_3 = RotateLeft<8>(r1_3);
376 r2_3 = RotateLeft<8>(r2_3);
377 r3_3 = RotateLeft<8>(r3_3);
379 r0_2 = vaddq_u32(r0_2, r0_3);
380 r1_2 = vaddq_u32(r1_2, r1_3);
381 r2_2 = vaddq_u32(r2_2, r2_3);
382 r3_2 = vaddq_u32(r3_2, r3_3);
384 r0_1 = veorq_u32(r0_1, r0_2);
385 r1_1 = veorq_u32(r1_1, r1_2);
386 r2_1 = veorq_u32(r2_1, r2_2);
387 r3_1 = veorq_u32(r3_1, r3_2);
389 r0_1 = RotateLeft<7>(r0_1);
390 r1_1 = RotateLeft<7>(r1_1);
391 r2_1 = RotateLeft<7>(r2_1);
392 r3_1 = RotateLeft<7>(r3_1);
394 r0_1 = Extract<1>(r0_1);
395 r0_2 = Extract<2>(r0_2);
396 r0_3 = Extract<3>(r0_3);
398 r1_1 = Extract<1>(r1_1);
399 r1_2 = Extract<2>(r1_2);
400 r1_3 = Extract<3>(r1_3);
402 r2_1 = Extract<1>(r2_1);
403 r2_2 = Extract<2>(r2_2);
404 r2_3 = Extract<3>(r2_3);
406 r3_1 = Extract<1>(r3_1);
407 r3_2 = Extract<2>(r3_2);
408 r3_3 = Extract<3>(r3_3);
410 r0_0 = vaddq_u32(r0_0, r0_1);
411 r1_0 = vaddq_u32(r1_0, r1_1);
412 r2_0 = vaddq_u32(r2_0, r2_1);
413 r3_0 = vaddq_u32(r3_0, r3_1);
415 r0_3 = veorq_u32(r0_3, r0_0);
416 r1_3 = veorq_u32(r1_3, r1_0);
417 r2_3 = veorq_u32(r2_3, r2_0);
418 r3_3 = veorq_u32(r3_3, r3_0);
420 r0_3 = RotateLeft<16>(r0_3);
421 r1_3 = RotateLeft<16>(r1_3);
422 r2_3 = RotateLeft<16>(r2_3);
423 r3_3 = RotateLeft<16>(r3_3);
425 r0_2 = vaddq_u32(r0_2, r0_3);
426 r1_2 = vaddq_u32(r1_2, r1_3);
427 r2_2 = vaddq_u32(r2_2, r2_3);
428 r3_2 = vaddq_u32(r3_2, r3_3);
430 r0_1 = veorq_u32(r0_1, r0_2);
431 r1_1 = veorq_u32(r1_1, r1_2);
432 r2_1 = veorq_u32(r2_1, r2_2);
433 r3_1 = veorq_u32(r3_1, r3_2);
435 r0_1 = RotateLeft<12>(r0_1);
436 r1_1 = RotateLeft<12>(r1_1);
437 r2_1 = RotateLeft<12>(r2_1);
438 r3_1 = RotateLeft<12>(r3_1);
440 r0_0 = vaddq_u32(r0_0, r0_1);
441 r1_0 = vaddq_u32(r1_0, r1_1);
442 r2_0 = vaddq_u32(r2_0, r2_1);
443 r3_0 = vaddq_u32(r3_0, r3_1);
445 r0_3 = veorq_u32(r0_3, r0_0);
446 r1_3 = veorq_u32(r1_3, r1_0);
447 r2_3 = veorq_u32(r2_3, r2_0);
448 r3_3 = veorq_u32(r3_3, r3_0);
450 r0_3 = RotateLeft<8>(r0_3);
451 r1_3 = RotateLeft<8>(r1_3);
452 r2_3 = RotateLeft<8>(r2_3);
453 r3_3 = RotateLeft<8>(r3_3);
455 r0_2 = vaddq_u32(r0_2, r0_3);
456 r1_2 = vaddq_u32(r1_2, r1_3);
457 r2_2 = vaddq_u32(r2_2, r2_3);
458 r3_2 = vaddq_u32(r3_2, r3_3);
460 r0_1 = veorq_u32(r0_1, r0_2);
461 r1_1 = veorq_u32(r1_1, r1_2);
462 r2_1 = veorq_u32(r2_1, r2_2);
463 r3_1 = veorq_u32(r3_1, r3_2);
465 r0_1 = RotateLeft<7>(r0_1);
466 r1_1 = RotateLeft<7>(r1_1);
467 r2_1 = RotateLeft<7>(r2_1);
468 r3_1 = RotateLeft<7>(r3_1);
470 r0_1 = Extract<3>(r0_1);
471 r0_2 = Extract<2>(r0_2);
472 r0_3 = Extract<1>(r0_3);
474 r1_1 = Extract<3>(r1_1);
475 r1_2 = Extract<2>(r1_2);
476 r1_3 = Extract<1>(r1_3);
478 r2_1 = Extract<3>(r2_1);
479 r2_2 = Extract<2>(r2_2);
480 r2_3 = Extract<1>(r2_3);
482 r3_1 = Extract<3>(r3_1);
483 r3_2 = Extract<2>(r3_2);
484 r3_3 = Extract<1>(r3_3);
487 r0_0 = vaddq_u32(r0_0, state0);
488 r0_1 = vaddq_u32(r0_1, state1);
489 r0_2 = vaddq_u32(r0_2, state2);
490 r0_3 = vaddq_u32(r0_3, state3);
492 r1_0 = vaddq_u32(r1_0, state0);
493 r1_1 = vaddq_u32(r1_1, state1);
494 r1_2 = vaddq_u32(r1_2, state2);
495 r1_3 = vaddq_u32(r1_3, state3);
496 r1_3 = Add64(r1_3, CTRS[0]);
498 r2_0 = vaddq_u32(r2_0, state0);
499 r2_1 = vaddq_u32(r2_1, state1);
500 r2_2 = vaddq_u32(r2_2, state2);
501 r2_3 = vaddq_u32(r2_3, state3);
502 r2_3 = Add64(r2_3, CTRS[1]);
504 r3_0 = vaddq_u32(r3_0, state0);
505 r3_1 = vaddq_u32(r3_1, state1);
506 r3_2 = vaddq_u32(r3_2, state2);
507 r3_3 = vaddq_u32(r3_3, state3);
508 r3_3 = Add64(r3_3, CTRS[2]);
512 r0_0 = veorq_u32(vreinterpretq_u32_u8(vld1q_u8(input + 0*16)), r0_0);
513 r0_1 = veorq_u32(vreinterpretq_u32_u8(vld1q_u8(input + 1*16)), r0_1);
514 r0_2 = veorq_u32(vreinterpretq_u32_u8(vld1q_u8(input + 2*16)), r0_2);
515 r0_3 = veorq_u32(vreinterpretq_u32_u8(vld1q_u8(input + 3*16)), r0_3);
518 vst1q_u8(output + 0*16, vreinterpretq_u8_u32(r0_0));
519 vst1q_u8(output + 1*16, vreinterpretq_u8_u32(r0_1));
520 vst1q_u8(output + 2*16, vreinterpretq_u8_u32(r0_2));
521 vst1q_u8(output + 3*16, vreinterpretq_u8_u32(r0_3));
525 r1_0 = veorq_u32(vreinterpretq_u32_u8(vld1q_u8(input + 4*16)), r1_0);
526 r1_1 = veorq_u32(vreinterpretq_u32_u8(vld1q_u8(input + 5*16)), r1_1);
527 r1_2 = veorq_u32(vreinterpretq_u32_u8(vld1q_u8(input + 6*16)), r1_2);
528 r1_3 = veorq_u32(vreinterpretq_u32_u8(vld1q_u8(input + 7*16)), r1_3);
531 vst1q_u8(output + 4*16, vreinterpretq_u8_u32(r1_0));
532 vst1q_u8(output + 5*16, vreinterpretq_u8_u32(r1_1));
533 vst1q_u8(output + 6*16, vreinterpretq_u8_u32(r1_2));
534 vst1q_u8(output + 7*16, vreinterpretq_u8_u32(r1_3));
538 r2_0 = veorq_u32(vreinterpretq_u32_u8(vld1q_u8(input + 8*16)), r2_0);
539 r2_1 = veorq_u32(vreinterpretq_u32_u8(vld1q_u8(input + 9*16)), r2_1);
540 r2_2 = veorq_u32(vreinterpretq_u32_u8(vld1q_u8(input + 10*16)), r2_2);
541 r2_3 = veorq_u32(vreinterpretq_u32_u8(vld1q_u8(input + 11*16)), r2_3);
544 vst1q_u8(output + 8*16, vreinterpretq_u8_u32(r2_0));
545 vst1q_u8(output + 9*16, vreinterpretq_u8_u32(r2_1));
546 vst1q_u8(output + 10*16, vreinterpretq_u8_u32(r2_2));
547 vst1q_u8(output + 11*16, vreinterpretq_u8_u32(r2_3));
551 r3_0 = veorq_u32(vreinterpretq_u32_u8(vld1q_u8(input + 12*16)), r3_0);
552 r3_1 = veorq_u32(vreinterpretq_u32_u8(vld1q_u8(input + 13*16)), r3_1);
553 r3_2 = veorq_u32(vreinterpretq_u32_u8(vld1q_u8(input + 14*16)), r3_2);
554 r3_3 = veorq_u32(vreinterpretq_u32_u8(vld1q_u8(input + 15*16)), r3_3);
557 vst1q_u8(output + 12*16, vreinterpretq_u8_u32(r3_0));
558 vst1q_u8(output + 13*16, vreinterpretq_u8_u32(r3_1));
559 vst1q_u8(output + 14*16, vreinterpretq_u8_u32(r3_2));
560 vst1q_u8(output + 15*16, vreinterpretq_u8_u32(r3_3));
567#if (CRYPTOPP_SSE2_INTRIN_AVAILABLE)
569void ChaCha_OperateKeystream_SSE2(
const word32 *state,
const byte* input,
byte *output,
unsigned int rounds)
571 const __m128i state0 = _mm_load_si128(
reinterpret_cast<const __m128i*
>(state+0*4));
572 const __m128i state1 = _mm_load_si128(
reinterpret_cast<const __m128i*
>(state+1*4));
573 const __m128i state2 = _mm_load_si128(
reinterpret_cast<const __m128i*
>(state+2*4));
574 const __m128i state3 = _mm_load_si128(
reinterpret_cast<const __m128i*
>(state+3*4));
576 __m128i r0_0 = state0;
577 __m128i r0_1 = state1;
578 __m128i r0_2 = state2;
579 __m128i r0_3 = state3;
581 __m128i r1_0 = state0;
582 __m128i r1_1 = state1;
583 __m128i r1_2 = state2;
584 __m128i r1_3 = _mm_add_epi64(r0_3, _mm_set_epi32(0, 0, 0, 1));
586 __m128i r2_0 = state0;
587 __m128i r2_1 = state1;
588 __m128i r2_2 = state2;
589 __m128i r2_3 = _mm_add_epi64(r0_3, _mm_set_epi32(0, 0, 0, 2));
591 __m128i r3_0 = state0;
592 __m128i r3_1 = state1;
593 __m128i r3_2 = state2;
594 __m128i r3_3 = _mm_add_epi64(r0_3, _mm_set_epi32(0, 0, 0, 3));
596 for (
int i =
static_cast<int>(rounds); i > 0; i -= 2)
598 r0_0 = _mm_add_epi32(r0_0, r0_1);
599 r1_0 = _mm_add_epi32(r1_0, r1_1);
600 r2_0 = _mm_add_epi32(r2_0, r2_1);
601 r3_0 = _mm_add_epi32(r3_0, r3_1);
603 r0_3 = _mm_xor_si128(r0_3, r0_0);
604 r1_3 = _mm_xor_si128(r1_3, r1_0);
605 r2_3 = _mm_xor_si128(r2_3, r2_0);
606 r3_3 = _mm_xor_si128(r3_3, r3_0);
608 r0_3 = RotateLeft<16>(r0_3);
609 r1_3 = RotateLeft<16>(r1_3);
610 r2_3 = RotateLeft<16>(r2_3);
611 r3_3 = RotateLeft<16>(r3_3);
613 r0_2 = _mm_add_epi32(r0_2, r0_3);
614 r1_2 = _mm_add_epi32(r1_2, r1_3);
615 r2_2 = _mm_add_epi32(r2_2, r2_3);
616 r3_2 = _mm_add_epi32(r3_2, r3_3);
618 r0_1 = _mm_xor_si128(r0_1, r0_2);
619 r1_1 = _mm_xor_si128(r1_1, r1_2);
620 r2_1 = _mm_xor_si128(r2_1, r2_2);
621 r3_1 = _mm_xor_si128(r3_1, r3_2);
623 r0_1 = RotateLeft<12>(r0_1);
624 r1_1 = RotateLeft<12>(r1_1);
625 r2_1 = RotateLeft<12>(r2_1);
626 r3_1 = RotateLeft<12>(r3_1);
628 r0_0 = _mm_add_epi32(r0_0, r0_1);
629 r1_0 = _mm_add_epi32(r1_0, r1_1);
630 r2_0 = _mm_add_epi32(r2_0, r2_1);
631 r3_0 = _mm_add_epi32(r3_0, r3_1);
633 r0_3 = _mm_xor_si128(r0_3, r0_0);
634 r1_3 = _mm_xor_si128(r1_3, r1_0);
635 r2_3 = _mm_xor_si128(r2_3, r2_0);
636 r3_3 = _mm_xor_si128(r3_3, r3_0);
638 r0_3 = RotateLeft<8>(r0_3);
639 r1_3 = RotateLeft<8>(r1_3);
640 r2_3 = RotateLeft<8>(r2_3);
641 r3_3 = RotateLeft<8>(r3_3);
643 r0_2 = _mm_add_epi32(r0_2, r0_3);
644 r1_2 = _mm_add_epi32(r1_2, r1_3);
645 r2_2 = _mm_add_epi32(r2_2, r2_3);
646 r3_2 = _mm_add_epi32(r3_2, r3_3);
648 r0_1 = _mm_xor_si128(r0_1, r0_2);
649 r1_1 = _mm_xor_si128(r1_1, r1_2);
650 r2_1 = _mm_xor_si128(r2_1, r2_2);
651 r3_1 = _mm_xor_si128(r3_1, r3_2);
653 r0_1 = RotateLeft<7>(r0_1);
654 r1_1 = RotateLeft<7>(r1_1);
655 r2_1 = RotateLeft<7>(r2_1);
656 r3_1 = RotateLeft<7>(r3_1);
658 r0_1 = _mm_shuffle_epi32(r0_1, _MM_SHUFFLE(0, 3, 2, 1));
659 r0_2 = _mm_shuffle_epi32(r0_2, _MM_SHUFFLE(1, 0, 3, 2));
660 r0_3 = _mm_shuffle_epi32(r0_3, _MM_SHUFFLE(2, 1, 0, 3));
662 r1_1 = _mm_shuffle_epi32(r1_1, _MM_SHUFFLE(0, 3, 2, 1));
663 r1_2 = _mm_shuffle_epi32(r1_2, _MM_SHUFFLE(1, 0, 3, 2));
664 r1_3 = _mm_shuffle_epi32(r1_3, _MM_SHUFFLE(2, 1, 0, 3));
666 r2_1 = _mm_shuffle_epi32(r2_1, _MM_SHUFFLE(0, 3, 2, 1));
667 r2_2 = _mm_shuffle_epi32(r2_2, _MM_SHUFFLE(1, 0, 3, 2));
668 r2_3 = _mm_shuffle_epi32(r2_3, _MM_SHUFFLE(2, 1, 0, 3));
670 r3_1 = _mm_shuffle_epi32(r3_1, _MM_SHUFFLE(0, 3, 2, 1));
671 r3_2 = _mm_shuffle_epi32(r3_2, _MM_SHUFFLE(1, 0, 3, 2));
672 r3_3 = _mm_shuffle_epi32(r3_3, _MM_SHUFFLE(2, 1, 0, 3));
674 r0_0 = _mm_add_epi32(r0_0, r0_1);
675 r1_0 = _mm_add_epi32(r1_0, r1_1);
676 r2_0 = _mm_add_epi32(r2_0, r2_1);
677 r3_0 = _mm_add_epi32(r3_0, r3_1);
679 r0_3 = _mm_xor_si128(r0_3, r0_0);
680 r1_3 = _mm_xor_si128(r1_3, r1_0);
681 r2_3 = _mm_xor_si128(r2_3, r2_0);
682 r3_3 = _mm_xor_si128(r3_3, r3_0);
684 r0_3 = RotateLeft<16>(r0_3);
685 r1_3 = RotateLeft<16>(r1_3);
686 r2_3 = RotateLeft<16>(r2_3);
687 r3_3 = RotateLeft<16>(r3_3);
689 r0_2 = _mm_add_epi32(r0_2, r0_3);
690 r1_2 = _mm_add_epi32(r1_2, r1_3);
691 r2_2 = _mm_add_epi32(r2_2, r2_3);
692 r3_2 = _mm_add_epi32(r3_2, r3_3);
694 r0_1 = _mm_xor_si128(r0_1, r0_2);
695 r1_1 = _mm_xor_si128(r1_1, r1_2);
696 r2_1 = _mm_xor_si128(r2_1, r2_2);
697 r3_1 = _mm_xor_si128(r3_1, r3_2);
699 r0_1 = RotateLeft<12>(r0_1);
700 r1_1 = RotateLeft<12>(r1_1);
701 r2_1 = RotateLeft<12>(r2_1);
702 r3_1 = RotateLeft<12>(r3_1);
704 r0_0 = _mm_add_epi32(r0_0, r0_1);
705 r1_0 = _mm_add_epi32(r1_0, r1_1);
706 r2_0 = _mm_add_epi32(r2_0, r2_1);
707 r3_0 = _mm_add_epi32(r3_0, r3_1);
709 r0_3 = _mm_xor_si128(r0_3, r0_0);
710 r1_3 = _mm_xor_si128(r1_3, r1_0);
711 r2_3 = _mm_xor_si128(r2_3, r2_0);
712 r3_3 = _mm_xor_si128(r3_3, r3_0);
714 r0_3 = RotateLeft<8>(r0_3);
715 r1_3 = RotateLeft<8>(r1_3);
716 r2_3 = RotateLeft<8>(r2_3);
717 r3_3 = RotateLeft<8>(r3_3);
719 r0_2 = _mm_add_epi32(r0_2, r0_3);
720 r1_2 = _mm_add_epi32(r1_2, r1_3);
721 r2_2 = _mm_add_epi32(r2_2, r2_3);
722 r3_2 = _mm_add_epi32(r3_2, r3_3);
724 r0_1 = _mm_xor_si128(r0_1, r0_2);
725 r1_1 = _mm_xor_si128(r1_1, r1_2);
726 r2_1 = _mm_xor_si128(r2_1, r2_2);
727 r3_1 = _mm_xor_si128(r3_1, r3_2);
729 r0_1 = RotateLeft<7>(r0_1);
730 r1_1 = RotateLeft<7>(r1_1);
731 r2_1 = RotateLeft<7>(r2_1);
732 r3_1 = RotateLeft<7>(r3_1);
734 r0_1 = _mm_shuffle_epi32(r0_1, _MM_SHUFFLE(2, 1, 0, 3));
735 r0_2 = _mm_shuffle_epi32(r0_2, _MM_SHUFFLE(1, 0, 3, 2));
736 r0_3 = _mm_shuffle_epi32(r0_3, _MM_SHUFFLE(0, 3, 2, 1));
738 r1_1 = _mm_shuffle_epi32(r1_1, _MM_SHUFFLE(2, 1, 0, 3));
739 r1_2 = _mm_shuffle_epi32(r1_2, _MM_SHUFFLE(1, 0, 3, 2));
740 r1_3 = _mm_shuffle_epi32(r1_3, _MM_SHUFFLE(0, 3, 2, 1));
742 r2_1 = _mm_shuffle_epi32(r2_1, _MM_SHUFFLE(2, 1, 0, 3));
743 r2_2 = _mm_shuffle_epi32(r2_2, _MM_SHUFFLE(1, 0, 3, 2));
744 r2_3 = _mm_shuffle_epi32(r2_3, _MM_SHUFFLE(0, 3, 2, 1));
746 r3_1 = _mm_shuffle_epi32(r3_1, _MM_SHUFFLE(2, 1, 0, 3));
747 r3_2 = _mm_shuffle_epi32(r3_2, _MM_SHUFFLE(1, 0, 3, 2));
748 r3_3 = _mm_shuffle_epi32(r3_3, _MM_SHUFFLE(0, 3, 2, 1));
751 r0_0 = _mm_add_epi32(r0_0, state0);
752 r0_1 = _mm_add_epi32(r0_1, state1);
753 r0_2 = _mm_add_epi32(r0_2, state2);
754 r0_3 = _mm_add_epi32(r0_3, state3);
756 r1_0 = _mm_add_epi32(r1_0, state0);
757 r1_1 = _mm_add_epi32(r1_1, state1);
758 r1_2 = _mm_add_epi32(r1_2, state2);
759 r1_3 = _mm_add_epi32(r1_3, state3);
760 r1_3 = _mm_add_epi64(r1_3, _mm_set_epi32(0, 0, 0, 1));
762 r2_0 = _mm_add_epi32(r2_0, state0);
763 r2_1 = _mm_add_epi32(r2_1, state1);
764 r2_2 = _mm_add_epi32(r2_2, state2);
765 r2_3 = _mm_add_epi32(r2_3, state3);
766 r2_3 = _mm_add_epi64(r2_3, _mm_set_epi32(0, 0, 0, 2));
768 r3_0 = _mm_add_epi32(r3_0, state0);
769 r3_1 = _mm_add_epi32(r3_1, state1);
770 r3_2 = _mm_add_epi32(r3_2, state2);
771 r3_3 = _mm_add_epi32(r3_3, state3);
772 r3_3 = _mm_add_epi64(r3_3, _mm_set_epi32(0, 0, 0, 3));
776 r0_0 = _mm_xor_si128(_mm_loadu_si128(
reinterpret_cast<const __m128i*
>(input+0*16)), r0_0);
777 r0_1 = _mm_xor_si128(_mm_loadu_si128(
reinterpret_cast<const __m128i*
>(input+1*16)), r0_1);
778 r0_2 = _mm_xor_si128(_mm_loadu_si128(
reinterpret_cast<const __m128i*
>(input+2*16)), r0_2);
779 r0_3 = _mm_xor_si128(_mm_loadu_si128(
reinterpret_cast<const __m128i*
>(input+3*16)), r0_3);
782 _mm_storeu_si128(
reinterpret_cast<__m128i*
>(output+0*16), r0_0);
783 _mm_storeu_si128(
reinterpret_cast<__m128i*
>(output+1*16), r0_1);
784 _mm_storeu_si128(
reinterpret_cast<__m128i*
>(output+2*16), r0_2);
785 _mm_storeu_si128(
reinterpret_cast<__m128i*
>(output+3*16), r0_3);
789 r1_0 = _mm_xor_si128(_mm_loadu_si128(
reinterpret_cast<const __m128i*
>(input+4*16)), r1_0);
790 r1_1 = _mm_xor_si128(_mm_loadu_si128(
reinterpret_cast<const __m128i*
>(input+5*16)), r1_1);
791 r1_2 = _mm_xor_si128(_mm_loadu_si128(
reinterpret_cast<const __m128i*
>(input+6*16)), r1_2);
792 r1_3 = _mm_xor_si128(_mm_loadu_si128(
reinterpret_cast<const __m128i*
>(input+7*16)), r1_3);
795 _mm_storeu_si128(
reinterpret_cast<__m128i*
>(output+4*16), r1_0);
796 _mm_storeu_si128(
reinterpret_cast<__m128i*
>(output+5*16), r1_1);
797 _mm_storeu_si128(
reinterpret_cast<__m128i*
>(output+6*16), r1_2);
798 _mm_storeu_si128(
reinterpret_cast<__m128i*
>(output+7*16), r1_3);
802 r2_0 = _mm_xor_si128(_mm_loadu_si128(
reinterpret_cast<const __m128i*
>(input+ 8*16)), r2_0);
803 r2_1 = _mm_xor_si128(_mm_loadu_si128(
reinterpret_cast<const __m128i*
>(input+ 9*16)), r2_1);
804 r2_2 = _mm_xor_si128(_mm_loadu_si128(
reinterpret_cast<const __m128i*
>(input+10*16)), r2_2);
805 r2_3 = _mm_xor_si128(_mm_loadu_si128(
reinterpret_cast<const __m128i*
>(input+11*16)), r2_3);
808 _mm_storeu_si128(
reinterpret_cast<__m128i*
>(output+ 8*16), r2_0);
809 _mm_storeu_si128(
reinterpret_cast<__m128i*
>(output+ 9*16), r2_1);
810 _mm_storeu_si128(
reinterpret_cast<__m128i*
>(output+10*16), r2_2);
811 _mm_storeu_si128(
reinterpret_cast<__m128i*
>(output+11*16), r2_3);
815 r3_0 = _mm_xor_si128(_mm_loadu_si128(
reinterpret_cast<const __m128i*
>(input+12*16)), r3_0);
816 r3_1 = _mm_xor_si128(_mm_loadu_si128(
reinterpret_cast<const __m128i*
>(input+13*16)), r3_1);
817 r3_2 = _mm_xor_si128(_mm_loadu_si128(
reinterpret_cast<const __m128i*
>(input+14*16)), r3_2);
818 r3_3 = _mm_xor_si128(_mm_loadu_si128(
reinterpret_cast<const __m128i*
>(input+15*16)), r3_3);
821 _mm_storeu_si128(
reinterpret_cast<__m128i*
>(output+12*16), r3_0);
822 _mm_storeu_si128(
reinterpret_cast<__m128i*
>(output+13*16), r3_1);
823 _mm_storeu_si128(
reinterpret_cast<__m128i*
>(output+14*16), r3_2);
824 _mm_storeu_si128(
reinterpret_cast<__m128i*
>(output+15*16), r3_3);
829#if (CRYPTOPP_ALTIVEC_AVAILABLE)
836inline void ChaCha_OperateKeystream_CORE(
const word32 *state,
const byte* input,
byte *output,
unsigned int rounds)
844 {1,0,0,0}, {2,0,0,0}, {3,0,0,0}
867 for (
int i =
static_cast<int>(rounds); i > 0; i -= 2)
869 r0_0 =
VecAdd(r0_0, r0_1);
870 r1_0 =
VecAdd(r1_0, r1_1);
871 r2_0 =
VecAdd(r2_0, r2_1);
872 r3_0 =
VecAdd(r3_0, r3_1);
874 r0_3 =
VecXor(r0_3, r0_0);
875 r1_3 =
VecXor(r1_3, r1_0);
876 r2_3 =
VecXor(r2_3, r2_0);
877 r3_3 =
VecXor(r3_3, r3_0);
879 r0_3 = VecRotateLeft<16>(r0_3);
880 r1_3 = VecRotateLeft<16>(r1_3);
881 r2_3 = VecRotateLeft<16>(r2_3);
882 r3_3 = VecRotateLeft<16>(r3_3);
884 r0_2 =
VecAdd(r0_2, r0_3);
885 r1_2 =
VecAdd(r1_2, r1_3);
886 r2_2 =
VecAdd(r2_2, r2_3);
887 r3_2 =
VecAdd(r3_2, r3_3);
889 r0_1 =
VecXor(r0_1, r0_2);
890 r1_1 =
VecXor(r1_1, r1_2);
891 r2_1 =
VecXor(r2_1, r2_2);
892 r3_1 =
VecXor(r3_1, r3_2);
894 r0_1 = VecRotateLeft<12>(r0_1);
895 r1_1 = VecRotateLeft<12>(r1_1);
896 r2_1 = VecRotateLeft<12>(r2_1);
897 r3_1 = VecRotateLeft<12>(r3_1);
899 r0_0 =
VecAdd(r0_0, r0_1);
900 r1_0 =
VecAdd(r1_0, r1_1);
901 r2_0 =
VecAdd(r2_0, r2_1);
902 r3_0 =
VecAdd(r3_0, r3_1);
904 r0_3 =
VecXor(r0_3, r0_0);
905 r1_3 =
VecXor(r1_3, r1_0);
906 r2_3 =
VecXor(r2_3, r2_0);
907 r3_3 =
VecXor(r3_3, r3_0);
909 r0_3 = VecRotateLeft<8>(r0_3);
910 r1_3 = VecRotateLeft<8>(r1_3);
911 r2_3 = VecRotateLeft<8>(r2_3);
912 r3_3 = VecRotateLeft<8>(r3_3);
914 r0_2 =
VecAdd(r0_2, r0_3);
915 r1_2 =
VecAdd(r1_2, r1_3);
916 r2_2 =
VecAdd(r2_2, r2_3);
917 r3_2 =
VecAdd(r3_2, r3_3);
919 r0_1 =
VecXor(r0_1, r0_2);
920 r1_1 =
VecXor(r1_1, r1_2);
921 r2_1 =
VecXor(r2_1, r2_2);
922 r3_1 =
VecXor(r3_1, r3_2);
924 r0_1 = VecRotateLeft<7>(r0_1);
925 r1_1 = VecRotateLeft<7>(r1_1);
926 r2_1 = VecRotateLeft<7>(r2_1);
927 r3_1 = VecRotateLeft<7>(r3_1);
929 r0_1 = Shuffle<1>(r0_1);
930 r0_2 = Shuffle<2>(r0_2);
931 r0_3 = Shuffle<3>(r0_3);
933 r1_1 = Shuffle<1>(r1_1);
934 r1_2 = Shuffle<2>(r1_2);
935 r1_3 = Shuffle<3>(r1_3);
937 r2_1 = Shuffle<1>(r2_1);
938 r2_2 = Shuffle<2>(r2_2);
939 r2_3 = Shuffle<3>(r2_3);
941 r3_1 = Shuffle<1>(r3_1);
942 r3_2 = Shuffle<2>(r3_2);
943 r3_3 = Shuffle<3>(r3_3);
945 r0_0 =
VecAdd(r0_0, r0_1);
946 r1_0 =
VecAdd(r1_0, r1_1);
947 r2_0 =
VecAdd(r2_0, r2_1);
948 r3_0 =
VecAdd(r3_0, r3_1);
950 r0_3 =
VecXor(r0_3, r0_0);
951 r1_3 =
VecXor(r1_3, r1_0);
952 r2_3 =
VecXor(r2_3, r2_0);
953 r3_3 =
VecXor(r3_3, r3_0);
955 r0_3 = VecRotateLeft<16>(r0_3);
956 r1_3 = VecRotateLeft<16>(r1_3);
957 r2_3 = VecRotateLeft<16>(r2_3);
958 r3_3 = VecRotateLeft<16>(r3_3);
960 r0_2 =
VecAdd(r0_2, r0_3);
961 r1_2 =
VecAdd(r1_2, r1_3);
962 r2_2 =
VecAdd(r2_2, r2_3);
963 r3_2 =
VecAdd(r3_2, r3_3);
965 r0_1 =
VecXor(r0_1, r0_2);
966 r1_1 =
VecXor(r1_1, r1_2);
967 r2_1 =
VecXor(r2_1, r2_2);
968 r3_1 =
VecXor(r3_1, r3_2);
970 r0_1 = VecRotateLeft<12>(r0_1);
971 r1_1 = VecRotateLeft<12>(r1_1);
972 r2_1 = VecRotateLeft<12>(r2_1);
973 r3_1 = VecRotateLeft<12>(r3_1);
975 r0_0 =
VecAdd(r0_0, r0_1);
976 r1_0 =
VecAdd(r1_0, r1_1);
977 r2_0 =
VecAdd(r2_0, r2_1);
978 r3_0 =
VecAdd(r3_0, r3_1);
980 r0_3 =
VecXor(r0_3, r0_0);
981 r1_3 =
VecXor(r1_3, r1_0);
982 r2_3 =
VecXor(r2_3, r2_0);
983 r3_3 =
VecXor(r3_3, r3_0);
985 r0_3 = VecRotateLeft<8>(r0_3);
986 r1_3 = VecRotateLeft<8>(r1_3);
987 r2_3 = VecRotateLeft<8>(r2_3);
988 r3_3 = VecRotateLeft<8>(r3_3);
990 r0_2 =
VecAdd(r0_2, r0_3);
991 r1_2 =
VecAdd(r1_2, r1_3);
992 r2_2 =
VecAdd(r2_2, r2_3);
993 r3_2 =
VecAdd(r3_2, r3_3);
995 r0_1 =
VecXor(r0_1, r0_2);
996 r1_1 =
VecXor(r1_1, r1_2);
997 r2_1 =
VecXor(r2_1, r2_2);
998 r3_1 =
VecXor(r3_1, r3_2);
1000 r0_1 = VecRotateLeft<7>(r0_1);
1001 r1_1 = VecRotateLeft<7>(r1_1);
1002 r2_1 = VecRotateLeft<7>(r2_1);
1003 r3_1 = VecRotateLeft<7>(r3_1);
1005 r0_1 = Shuffle<3>(r0_1);
1006 r0_2 = Shuffle<2>(r0_2);
1007 r0_3 = Shuffle<1>(r0_3);
1009 r1_1 = Shuffle<3>(r1_1);
1010 r1_2 = Shuffle<2>(r1_2);
1011 r1_3 = Shuffle<1>(r1_3);
1013 r2_1 = Shuffle<3>(r2_1);
1014 r2_2 = Shuffle<2>(r2_2);
1015 r2_3 = Shuffle<1>(r2_3);
1017 r3_1 = Shuffle<3>(r3_1);
1018 r3_2 = Shuffle<2>(r3_2);
1019 r3_3 = Shuffle<1>(r3_3);
1022 r0_0 =
VecAdd(r0_0, state0);
1023 r0_1 =
VecAdd(r0_1, state1);
1024 r0_2 =
VecAdd(r0_2, state2);
1025 r0_3 =
VecAdd(r0_3, state3);
1027 r1_0 =
VecAdd(r1_0, state0);
1028 r1_1 =
VecAdd(r1_1, state1);
1029 r1_2 =
VecAdd(r1_2, state2);
1030 r1_3 =
VecAdd(r1_3, state3);
1033 r2_0 =
VecAdd(r2_0, state0);
1034 r2_1 =
VecAdd(r2_1, state1);
1035 r2_2 =
VecAdd(r2_2, state2);
1036 r2_3 =
VecAdd(r2_3, state3);
1039 r3_0 =
VecAdd(r3_0, state0);
1040 r3_1 =
VecAdd(r3_1, state1);
1041 r3_2 =
VecAdd(r3_2, state2);
1042 r3_3 =
VecAdd(r3_3, state3);
1047 r0_0 =
VecXor(VecLoad32LE(input + 0*16), r0_0);
1048 r0_1 =
VecXor(VecLoad32LE(input + 1*16), r0_1);
1049 r0_2 =
VecXor(VecLoad32LE(input + 2*16), r0_2);
1050 r0_3 =
VecXor(VecLoad32LE(input + 3*16), r0_3);
1053 VecStore32LE(output + 0*16, r0_0);
1054 VecStore32LE(output + 1*16, r0_1);
1055 VecStore32LE(output + 2*16, r0_2);
1056 VecStore32LE(output + 3*16, r0_3);
1060 r1_0 =
VecXor(VecLoad32LE(input + 4*16), r1_0);
1061 r1_1 =
VecXor(VecLoad32LE(input + 5*16), r1_1);
1062 r1_2 =
VecXor(VecLoad32LE(input + 6*16), r1_2);
1063 r1_3 =
VecXor(VecLoad32LE(input + 7*16), r1_3);
1066 VecStore32LE(output + 4*16, r1_0);
1067 VecStore32LE(output + 5*16, r1_1);
1068 VecStore32LE(output + 6*16, r1_2);
1069 VecStore32LE(output + 7*16, r1_3);
1073 r2_0 =
VecXor(VecLoad32LE(input + 8*16), r2_0);
1074 r2_1 =
VecXor(VecLoad32LE(input + 9*16), r2_1);
1075 r2_2 =
VecXor(VecLoad32LE(input + 10*16), r2_2);
1076 r2_3 =
VecXor(VecLoad32LE(input + 11*16), r2_3);
1079 VecStore32LE(output + 8*16, r2_0);
1080 VecStore32LE(output + 9*16, r2_1);
1081 VecStore32LE(output + 10*16, r2_2);
1082 VecStore32LE(output + 11*16, r2_3);
1086 r3_0 =
VecXor(VecLoad32LE(input + 12*16), r3_0);
1087 r3_1 =
VecXor(VecLoad32LE(input + 13*16), r3_1);
1088 r3_2 =
VecXor(VecLoad32LE(input + 14*16), r3_2);
1089 r3_3 =
VecXor(VecLoad32LE(input + 15*16), r3_3);
1092 VecStore32LE(output + 12*16, r3_0);
1093 VecStore32LE(output + 13*16, r3_1);
1094 VecStore32LE(output + 14*16, r3_2);
1095 VecStore32LE(output + 15*16, r3_3);
1100#if (CRYPTOPP_ALTIVEC_AVAILABLE)
1102void ChaCha_OperateKeystream_ALTIVEC(
const word32 *state,
const byte* input,
byte *output,
unsigned int rounds)
1104 ChaCha_OperateKeystream_CORE(state, input, output, rounds);
Classes for ChaCha8, ChaCha12 and ChaCha20 stream ciphers.
Library configuration file.
unsigned int word32
32-bit unsigned datatype
Utility functions for the Crypto++ library.
Crypto++ library namespace.
Support functions for PowerPC and vector operations.
uint32x4_p VecLoadAligned(const byte src[16])
Loads a vector from an aligned byte array.
__vector unsigned int uint32x4_p
Vector of 32-bit elements.
T1 VecPermute(const T1 vec, const T2 mask)
Permutes a vector.
__vector unsigned char uint8x16_p
Vector of 8-bit elements.
T1 VecXor(const T1 vec1, const T2 vec2)
XOR two vectors.
T1 VecAdd(const T1 vec1, const T2 vec2)
Add two vectors.
void VecStore(const T data, byte dest[16])
Stores a vector to a byte array.
uint32x4_p VecAdd64(const uint32x4_p &vec1, const uint32x4_p &vec2)
Add two vectors as if uint64x2_p.
uint32x4_p VecLoad(const byte src[16])
Loads a vector from a byte array.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.