8 #if CRYPTOPP_MSC_VERSION
9 # pragma warning(disable: 4189)
12 #ifndef CRYPTOPP_IMPORTS
13 #ifndef CRYPTOPP_GENERATE_X64_MASM
16 #if (defined(CRYPTOPP_LLVM_CLANG_VERSION) && (CRYPTOPP_LLVM_CLANG_VERSION < 30400))
17 # undef CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE
22 #if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x513)
23 # undef CRYPTOPP_BOOL_AESNI_INTRINSICS_AVAILABLE
32 #if (CRYPTOPP_LLVM_CLANG_VERSION >= 30600) || (CRYPTOPP_APPLE_CLANG_VERSION >= 70000) || defined(CRYPTOPP_CLANG_INTEGRATED_ASSEMBLER)
34 # define USE_MOVD_REG32 1
35 #elif (defined(CRYPTOPP_LLVM_CLANG_VERSION) || defined(CRYPTOPP_APPLE_CLANG_VERSION)) && defined(CRYPTOPP_X64_ASM_AVAILABLE)
37 # define USE_MOVD_REG32_OR_REG64 1
38 #elif defined(__GNUC__) || defined(_MSC_VER)
40 # define USE_MOVD_REG32_OR_REG64 1
43 # define USE_MOV_REG32_OR_REG64 1
46 word16 GCM_Base::s_reductionTable[256];
47 volatile bool GCM_Base::s_reductionTableInitialized =
false;
49 void GCM_Base::GCTR::IncrementCounterBy256()
56 void gcm_gf_mult(
const unsigned char *a,
const unsigned char *b,
unsigned char *c)
58 word64 Z0=0, Z1=0, V0, V1;
61 Block::Get(a)(V0)(V1);
63 for (
int i=0; i<16; i++)
65 for (
int j=0x80; j!=0; j>>=1)
71 V1 = (V1>>1) | (V0<<63);
72 V0 = (V0>>1) ^ (x ? W64LIT(0xe1) << 56 : 0);
78 __m128i _mm_clmulepi64_si128(
const __m128i &a,
const __m128i &b,
int i)
88 for (
int i=0; i<16; i++)
89 ((
byte *)&output)[i] = c.GetByte(i);
94 #if CRYPTOPP_BOOL_SSE2_INTRINSICS_AVAILABLE || CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE
95 inline static void SSE2_Xor16(
byte *a,
const byte *b,
const byte *c)
100 *(__m128i *)(
void *)a = _mm_xor_si128(*(__m128i *)(
void *)b, *(__m128i *)(
void *)c);
101 # elif CRYPTOPP_BOOL_SSE2_INTRINSICS_AVAILABLE
105 *(__m128i *)(
void *)a = _mm_xor_si128(*(__m128i *)(
void *)b, *(__m128i *)(
void *)c);
107 asm (
"movdqa %1, %%xmm0; pxor %2, %%xmm0; movdqa %%xmm0, %0;" :
"=m" (a[0]) :
"m"(b[0]),
"m"(c[0]));
112 #if CRYPTOPP_BOOL_NEON_INTRINSICS_AVAILABLE
113 inline static void NEON_Xor16(
byte *a,
const byte *b,
const byte *c)
118 *(uint64x2_t*)a = veorq_u64(*(uint64x2_t*)b, *(uint64x2_t*)c);
122 inline static void Xor16(
byte *a,
const byte *b,
const byte *c)
127 ((word64 *)(
void *)a)[0] = ((word64 *)(
void *)b)[0] ^ ((word64 *)(
void *)c)[0];
128 ((word64 *)(
void *)a)[1] = ((word64 *)(
void *)b)[1] ^ ((word64 *)(
void *)c)[1];
131 #if CRYPTOPP_BOOL_AESNI_INTRINSICS_AVAILABLE
132 CRYPTOPP_ALIGN_DATA(16)
133 static const word64 s_clmulConstants64[] = {
134 W64LIT(0xe100000000000000), W64LIT(0xc200000000000000),
135 W64LIT(0x08090a0b0c0d0e0f), W64LIT(0x0001020304050607),
136 W64LIT(0x0001020304050607), W64LIT(0x08090a0b0c0d0e0f)};
138 static const __m128i *s_clmulConstants = (
const __m128i *)(
const void *)s_clmulConstants64;
139 static const unsigned int s_clmulTableSizeInBlocks = 8;
141 inline __m128i CLMUL_Reduce(__m128i c0, __m128i c1, __m128i c2,
const __m128i &r)
155 #if 0 // MSVC 2010 workaround: see http://connect.microsoft.com/VisualStudio/feedback/details/575301
156 c2 = _mm_xor_si128(c2, _mm_move_epi64(c0));
158 c1 = _mm_xor_si128(c1, _mm_slli_si128(c0, 8));
160 c1 = _mm_xor_si128(c1, _mm_clmulepi64_si128(c0, r, 0x10));
161 c0 = _mm_srli_si128(c0, 8);
162 c0 = _mm_xor_si128(c0, c1);
163 c0 = _mm_slli_epi64(c0, 1);
164 c0 = _mm_clmulepi64_si128(c0, r, 0);
165 c2 = _mm_xor_si128(c2, c0);
166 c2 = _mm_xor_si128(c2, _mm_srli_si128(c1, 8));
167 c1 = _mm_unpacklo_epi64(c1, c2);
168 c1 = _mm_srli_epi64(c1, 63);
169 c2 = _mm_slli_epi64(c2, 1);
170 return _mm_xor_si128(c2, c1);
173 inline __m128i CLMUL_GF_Mul(
const __m128i &x,
const __m128i &h,
const __m128i &r)
175 const __m128i c0 = _mm_clmulepi64_si128(x,h,0);
176 const __m128i c1 = _mm_xor_si128(_mm_clmulepi64_si128(x,h,1), _mm_clmulepi64_si128(x,h,0x10));
177 const __m128i c2 = _mm_clmulepi64_si128(x,h,0x11);
179 return CLMUL_Reduce(c0, c1, c2, r);
183 #if CRYPTOPP_BOOL_ARM_CRYPTO_INTRINSICS_AVAILABLE
185 CRYPTOPP_ALIGN_DATA(16)
186 static const word64 s_clmulConstants64[] = {
187 W64LIT(0xe100000000000000), W64LIT(0xc200000000000000),
188 W64LIT(0x08090a0b0c0d0e0f), W64LIT(0x0001020304050607),
189 W64LIT(0x0001020304050607), W64LIT(0x08090a0b0c0d0e0f)
192 static const uint64x2_t *s_clmulConstants = (
const uint64x2_t *)s_clmulConstants64;
193 static const unsigned int s_clmulTableSizeInBlocks = 8;
195 inline uint64x2_t PMULL_Reduce(uint64x2_t c0, uint64x2_t c1, uint64x2_t c2,
const uint64x2_t &r)
199 c1 = veorq_u64(c1, (uint64x2_t)vextq_u8(vdupq_n_u8(0), (uint8x16_t)c0, 8));
200 c1 = veorq_u64(c1, (uint64x2_t)vmull_p64(vgetq_lane_u64(c0, 0), vgetq_lane_u64(r, 1)));
201 c0 = (uint64x2_t)vextq_u8((uint8x16_t)c0, vdupq_n_u8(0), 8);
202 c0 = veorq_u64(c0, c1);
203 c0 = vshlq_n_u64(c0, 1);
204 c0 = (uint64x2_t)vmull_p64(vgetq_lane_u64(c0, 0), vgetq_lane_u64(r, 0));
205 c2 = veorq_u64(c2, c0);
206 c2 = veorq_u64(c2, (uint64x2_t)vextq_u8((uint8x16_t)c1, vdupq_n_u8(0), 8));
207 c1 = vcombine_u64(vget_low_u64(c1), vget_low_u64(c2));
208 c1 = vshrq_n_u64(c1, 63);
209 c2 = vshlq_n_u64(c2, 1);
211 return veorq_u64(c2, c1);
214 inline uint64x2_t PMULL_GF_Mul(
const uint64x2_t &x,
const uint64x2_t &h,
const uint64x2_t &r)
216 const uint64x2_t c0 = (uint64x2_t)vmull_p64(vgetq_lane_u64(x, 0), vgetq_lane_u64(h, 0));
217 const uint64x2_t c1 = veorq_u64((uint64x2_t)vmull_p64(vgetq_lane_u64(x, 1), vgetq_lane_u64(h,0)),
218 (uint64x2_t)vmull_p64(vgetq_lane_u64(x, 0), vgetq_lane_u64(h, 1)));
219 const uint64x2_t c2 = (uint64x2_t)vmull_high_p64((poly64x2_t)x, (poly64x2_t)h);
221 return PMULL_Reduce(c0, c1, c2, r);
225 void GCM_Base::SetKeyWithoutResync(
const byte *userKey,
size_t keylength,
const NameValuePairs ¶ms)
228 blockCipher.
SetKey(userKey, keylength, params);
230 if (blockCipher.
BlockSize() != REQUIRED_BLOCKSIZE)
233 int tableSize, i, j, k;
235 #if CRYPTOPP_BOOL_AESNI_INTRINSICS_AVAILABLE
239 (void)params.
GetIntValue(Name::TableSize(), tableSize);
240 tableSize = s_clmulTableSizeInBlocks * REQUIRED_BLOCKSIZE;
243 #elif CRYPTOPP_BOOL_ARM_CRYPTO_INTRINSICS_AVAILABLE
247 (void)params.
GetIntValue(Name::TableSize(), tableSize);
248 tableSize = s_clmulTableSizeInBlocks * REQUIRED_BLOCKSIZE;
253 if (params.
GetIntValue(Name::TableSize(), tableSize))
254 tableSize = (tableSize >= 64*1024) ? 64*1024 : 2*1024;
256 tableSize = (GetTablesOption() ==
GCM_64K_Tables) ? 64*1024 : 2*1024;
258 #if defined(_MSC_VER) && (_MSC_VER >= 1300 && _MSC_VER < 1400)
264 m_buffer.resize(3*REQUIRED_BLOCKSIZE + tableSize);
265 byte *table = MulTable();
266 byte *hashKey = HashKey();
267 memset(hashKey, 0, REQUIRED_BLOCKSIZE);
270 #if CRYPTOPP_BOOL_AESNI_INTRINSICS_AVAILABLE
273 const __m128i r = s_clmulConstants[0];
274 __m128i h0 = _mm_shuffle_epi8(_mm_load_si128((__m128i *)(
void *)hashKey), s_clmulConstants[1]);
277 for (i=0; i<tableSize; i+=32)
279 __m128i h1 = CLMUL_GF_Mul(h, h0, r);
280 _mm_storel_epi64((__m128i *)(
void *)(table+i), h);
281 _mm_storeu_si128((__m128i *)(
void *)(table+i+16), h1);
282 _mm_storeu_si128((__m128i *)(
void *)(table+i+8), h);
283 _mm_storel_epi64((__m128i *)(
void *)(table+i+8), h1);
284 h = CLMUL_GF_Mul(h1, h0, r);
289 #elif CRYPTOPP_BOOL_ARM_CRYPTO_INTRINSICS_AVAILABLE
292 const uint64x2_t r = s_clmulConstants[0];
293 const uint64x2_t t = vld1q_u64((uint64_t *)hashKey);
294 const uint64x2_t h0 = (uint64x2_t)vrev64q_u8((uint8x16_t)vcombine_u64(vget_high_u64(t), vget_low_u64(t)));
297 for (i=0; i<tableSize-32; i+=32)
299 const uint64x2_t h1 = PMULL_GF_Mul(h, h0, r);
300 vst1_u64((uint64_t *)(table+i), vget_low_u64(h));
301 vst1q_u64((uint64_t *)(table+i+16), h1);
302 vst1q_u64((uint64_t *)(table+i+8), h);
303 vst1_u64((uint64_t *)(table+i+8), vget_low_u64(h1));
304 h = PMULL_GF_Mul(h1, h0, r);
307 const uint64x2_t h1 = PMULL_GF_Mul(h, h0, r);
308 vst1_u64((uint64_t *)(table+i), vget_low_u64(h));
309 vst1q_u64((uint64_t *)(table+i+16), h1);
310 vst1q_u64((uint64_t *)(table+i+8), h);
311 vst1_u64((uint64_t *)(table+i+8), vget_low_u64(h1));
319 Block::Get(hashKey)(V0)(V1);
321 if (tableSize == 64*1024)
323 for (i=0; i<128; i++)
326 Block::Put(NULL, table+(i/8)*256*16+(
size_t(1)<<(11-k)))(V0)(V1);
329 V1 = (V1>>1) | (V0<<63);
330 V0 = (V0>>1) ^ (x ? W64LIT(0xe1) << 56 : 0);
335 memset(table+i*256*16, 0, 16);
336 #if CRYPTOPP_BOOL_SSE2_INTRINSICS_AVAILABLE || CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE
338 for (j=2; j<=0x80; j*=2)
340 SSE2_Xor16(table+i*256*16+(j+k)*16, table+i*256*16+j*16, table+i*256*16+k*16);
342 #elif CRYPTOPP_BOOL_NEON_INTRINSICS_AVAILABLE
344 for (j=2; j<=0x80; j*=2)
346 NEON_Xor16(table+i*256*16+(j+k)*16, table+i*256*16+j*16, table+i*256*16+k*16);
349 for (j=2; j<=0x80; j*=2)
351 Xor16(table+i*256*16+(j+k)*16, table+i*256*16+j*16, table+i*256*16+k*16);
356 if (!s_reductionTableInitialized)
358 s_reductionTable[0] = 0;
361 for (
unsigned int ii=2; ii<=0x80; ii*=2)
365 for (
unsigned int jj=1; jj<ii; jj++)
366 s_reductionTable[ii+jj] = s_reductionTable[ii] ^ s_reductionTable[jj];
368 s_reductionTableInitialized =
true;
371 for (i=0; i<128-24; i++)
375 Block::Put(NULL, table+1024+(i/32)*256+(
size_t(1)<<(7-k)))(V0)(V1);
377 Block::Put(NULL, table+(i/32)*256+(
size_t(1)<<(11-k)))(V0)(V1);
380 V1 = (V1>>1) | (V0<<63);
381 V0 = (V0>>1) ^ (x ? W64LIT(0xe1) << 56 : 0);
386 memset(table+i*256, 0, 16);
387 memset(table+1024+i*256, 0, 16);
388 #if CRYPTOPP_BOOL_SSE2_INTRINSICS_AVAILABLE || CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE
390 for (j=2; j<=8; j*=2)
393 SSE2_Xor16(table+i*256+(j+k)*16, table+i*256+j*16, table+i*256+k*16);
394 SSE2_Xor16(table+1024+i*256+(j+k)*16, table+1024+i*256+j*16, table+1024+i*256+k*16);
397 #elif CRYPTOPP_BOOL_NEON_INTRINSICS_AVAILABLE
399 for (j=2; j<=8; j*=2)
402 NEON_Xor16(table+i*256+(j+k)*16, table+i*256+j*16, table+i*256+k*16);
403 NEON_Xor16(table+1024+i*256+(j+k)*16, table+1024+i*256+j*16, table+1024+i*256+k*16);
407 for (j=2; j<=8; j*=2)
410 Xor16(table+i*256+(j+k)*16, table+i*256+j*16, table+i*256+k*16);
411 Xor16(table+1024+i*256+(j+k)*16, table+1024+i*256+j*16, table+1024+i*256+k*16);
417 inline void GCM_Base::ReverseHashBufferIfNeeded()
419 #if CRYPTOPP_BOOL_AESNI_INTRINSICS_AVAILABLE
422 __m128i &x = *(__m128i *)(
void *)HashBuffer();
423 x = _mm_shuffle_epi8(x, s_clmulConstants[1]);
425 #elif CRYPTOPP_BOOL_ARM_CRYPTO_INTRINSICS_AVAILABLE
430 const uint8x16_t x = vrev64q_u8(vld1q_u8(HashBuffer()));
431 vst1q_u8(HashBuffer(), (uint8x16_t)vcombine_u64(vget_high_u64((uint64x2_t)x), vget_low_u64((uint64x2_t)x)));
437 void GCM_Base::Resync(
const byte *iv,
size_t len)
440 byte *hashBuffer = HashBuffer();
444 memcpy(hashBuffer, iv, len);
445 memset(hashBuffer+len, 0, 3);
446 hashBuffer[len+3] = 1;
450 size_t origLen = len;
451 memset(hashBuffer, 0, HASH_BLOCKSIZE);
453 if (len >= HASH_BLOCKSIZE)
455 len = GCM_Base::AuthenticateBlocks(iv, len);
456 iv += (origLen - len);
461 memcpy(m_buffer, iv, len);
462 memset(m_buffer+len, 0, HASH_BLOCKSIZE-len);
463 GCM_Base::AuthenticateBlocks(m_buffer, HASH_BLOCKSIZE);
467 GCM_Base::AuthenticateBlocks(m_buffer, HASH_BLOCKSIZE);
469 ReverseHashBufferIfNeeded();
472 if (m_state >= State_IVSet)
473 m_ctr.Resynchronize(hashBuffer, REQUIRED_BLOCKSIZE);
475 m_ctr.SetCipherWithIV(cipher, hashBuffer);
477 m_ctr.Seek(HASH_BLOCKSIZE);
479 memset(hashBuffer, 0, HASH_BLOCKSIZE);
485 #if CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE || defined(CRYPTOPP_X64_MASM_AVAILABLE)
487 #elif CRYPTOPP_BOOL_NEON_INTRINSICS_AVAILABLE
493 #if CRYPTOPP_MSC_VERSION
494 # pragma warning(disable: 4731) // frame pointer register 'ebp' modified by inline assembly code
497 #endif // #ifndef CRYPTOPP_GENERATE_X64_MASM
499 #ifdef CRYPTOPP_X64_MASM_AVAILABLE
501 void GCM_AuthenticateBlocks_2K(
const byte *data,
size_t blocks, word64 *hashBuffer,
const word16 *reductionTable);
502 void GCM_AuthenticateBlocks_64K(
const byte *data,
size_t blocks, word64 *hashBuffer);
506 #ifndef CRYPTOPP_GENERATE_X64_MASM
508 size_t GCM_Base::AuthenticateBlocks(
const byte *data,
size_t len)
510 #if CRYPTOPP_BOOL_AESNI_INTRINSICS_AVAILABLE
513 const __m128i *table = (
const __m128i *)(
const void *)MulTable();
514 __m128i x = _mm_load_si128((__m128i *)(
void *)HashBuffer());
515 const __m128i r = s_clmulConstants[0], mask1 = s_clmulConstants[1], mask2 = s_clmulConstants[2];
519 size_t s =
UnsignedMin(len/16, s_clmulTableSizeInBlocks), i=0;
520 __m128i d, d2 = _mm_shuffle_epi8(_mm_loadu_si128((
const __m128i *)(
const void *)(data+(s-1)*16)), mask2);;
521 __m128i c0 = _mm_setzero_si128();
522 __m128i c1 = _mm_setzero_si128();
523 __m128i c2 = _mm_setzero_si128();
527 __m128i h0 = _mm_load_si128(table+i);
528 __m128i h1 = _mm_load_si128(table+i+1);
529 __m128i h2 = _mm_xor_si128(h0, h1);
533 d = _mm_shuffle_epi8(_mm_loadu_si128((
const __m128i *)(
const void *)data), mask1);
534 d = _mm_xor_si128(d, x);
535 c0 = _mm_xor_si128(c0, _mm_clmulepi64_si128(d, h0, 0));
536 c2 = _mm_xor_si128(c2, _mm_clmulepi64_si128(d, h1, 1));
537 d = _mm_xor_si128(d, _mm_shuffle_epi32(d, _MM_SHUFFLE(1, 0, 3, 2)));
538 c1 = _mm_xor_si128(c1, _mm_clmulepi64_si128(d, h2, 0));
542 d = _mm_shuffle_epi8(_mm_loadu_si128((
const __m128i *)(
const void *)(data+(s-i)*16-8)), mask2);
543 c0 = _mm_xor_si128(c0, _mm_clmulepi64_si128(d2, h0, 1));
544 c2 = _mm_xor_si128(c2, _mm_clmulepi64_si128(d, h1, 1));
545 d2 = _mm_xor_si128(d2, d);
546 c1 = _mm_xor_si128(c1, _mm_clmulepi64_si128(d2, h2, 1));
550 d = _mm_shuffle_epi8(_mm_loadu_si128((
const __m128i *)(
const void *)data), mask1);
551 d = _mm_xor_si128(d, x);
552 c0 = _mm_xor_si128(c0, _mm_clmulepi64_si128(d, h0, 0x10));
553 c2 = _mm_xor_si128(c2, _mm_clmulepi64_si128(d, h1, 0x11));
554 d = _mm_xor_si128(d, _mm_shuffle_epi32(d, _MM_SHUFFLE(1, 0, 3, 2)));
555 c1 = _mm_xor_si128(c1, _mm_clmulepi64_si128(d, h2, 0x10));
559 d2 = _mm_shuffle_epi8(_mm_loadu_si128((
const __m128i *)(
const void *)(data+(s-i)*16-8)), mask1);
560 c0 = _mm_xor_si128(c0, _mm_clmulepi64_si128(d, h0, 0x10));
561 c2 = _mm_xor_si128(c2, _mm_clmulepi64_si128(d2, h1, 0x10));
562 d = _mm_xor_si128(d, d2);
563 c1 = _mm_xor_si128(c1, _mm_clmulepi64_si128(d, h2, 0x10));
568 c1 = _mm_xor_si128(_mm_xor_si128(c1, c0), c2);
569 x = CLMUL_Reduce(c0, c1, c2, r);
572 _mm_store_si128((__m128i *)(
void *)HashBuffer(), x);
575 #elif CRYPTOPP_BOOL_ARM_CRYPTO_INTRINSICS_AVAILABLE
578 const uint64x2_t *table = (
const uint64x2_t *)MulTable();
579 uint64x2_t x = vld1q_u64((
const uint64_t*)HashBuffer());
580 const uint64x2_t r = s_clmulConstants[0];
584 size_t s =
UnsignedMin(len/16, s_clmulTableSizeInBlocks), i=0;
585 uint64x2_t d, d2 = (uint64x2_t)vrev64q_u8((uint8x16_t)vld1q_u64((
const uint64_t *)(data+(s-1)*16)));
586 uint64x2_t c0 = vdupq_n_u64(0);
587 uint64x2_t c1 = vdupq_n_u64(0);
588 uint64x2_t c2 = vdupq_n_u64(0);
592 const uint64x2_t h0 = vld1q_u64((
const uint64_t*)(table+i));
593 const uint64x2_t h1 = vld1q_u64((
const uint64_t*)(table+i+1));
594 const uint64x2_t h2 = veorq_u64(h0, h1);
598 const uint64x2_t t1 = vld1q_u64((
const uint64_t *)data);
599 d = veorq_u64((uint64x2_t)vrev64q_u8((uint8x16_t)vcombine_u64(vget_high_u64(t1), vget_low_u64(t1))), x);
600 c0 = veorq_u64(c0, (uint64x2_t)vmull_p64(vgetq_lane_u64(d, 0), vgetq_lane_u64(h0, 0)));
601 c2 = veorq_u64(c2, (uint64x2_t)vmull_p64(vgetq_lane_u64(d, 1), vgetq_lane_u64(h1, 0)));
602 d = veorq_u64(d, (uint64x2_t)vcombine_u32(vget_high_u32((uint32x4_t)d), vget_low_u32((uint32x4_t)d)));
603 c1 = veorq_u64(c1, (uint64x2_t)vmull_p64(vgetq_lane_u64(d, 0), vgetq_lane_u64(h2, 0)));
608 d = (uint64x2_t)vrev64q_u8((uint8x16_t)vld1q_u64((
const uint64_t *)(data+(s-i)*16-8)));
609 c0 = veorq_u64(c0, (uint64x2_t)vmull_p64(vgetq_lane_u64(d2, 1), vgetq_lane_u64(h0, 0)));
610 c2 = veorq_u64(c2, (uint64x2_t)vmull_p64(vgetq_lane_u64(d, 1), vgetq_lane_u64(h1, 0)));
611 d2 = veorq_u64(d2, d);
612 c1 = veorq_u64(c1, (uint64x2_t)vmull_p64(vgetq_lane_u64(d2, 1), vgetq_lane_u64(h2, 0)));
617 const uint64x2_t t2 = vld1q_u64((
const uint64_t *)data);
618 d = veorq_u64((uint64x2_t)vrev64q_u8((uint8x16_t)vcombine_u64(vget_high_u64(t2), vget_low_u64(t2))), x);
619 c0 = veorq_u64(c0, (uint64x2_t)vmull_p64(vgetq_lane_u64(d, 0), vgetq_lane_u64(h0, 1)));
620 c2 = veorq_u64(c2, (uint64x2_t)vmull_high_p64((poly64x2_t)d, (poly64x2_t)h1));
621 d = veorq_u64(d, (uint64x2_t)vcombine_u32(vget_high_u32((uint32x4_t)d), vget_low_u32((uint32x4_t)d)));
622 c1 = veorq_u64(c1, (uint64x2_t)vmull_p64(vgetq_lane_u64(d, 0), vgetq_lane_u64(h2, 1)));
627 const uint64x2_t t3 = vld1q_u64((uint64_t *)(data+(s-i)*16-8));
628 d2 = (uint64x2_t)vrev64q_u8((uint8x16_t)vcombine_u64(vget_high_u64(t3), vget_low_u64(t3)));
629 c0 = veorq_u64(c0, (uint64x2_t)vmull_p64(vgetq_lane_u64(d, 0), vgetq_lane_u64(h0, 1)));
630 c2 = veorq_u64(c2, (uint64x2_t)vmull_p64(vgetq_lane_u64(d2, 0), vgetq_lane_u64(h1, 1)));
631 d = veorq_u64(d, d2);
632 c1 = veorq_u64(c1, (uint64x2_t)vmull_p64(vgetq_lane_u64(d, 0), vgetq_lane_u64(h2, 1)));
637 c1 = veorq_u64(veorq_u64(c1, c0), c2);
638 x = PMULL_Reduce(c0, c1, c2, r);
641 vst1q_u64((uint64_t *)HashBuffer(), x);
647 word64 *hashBuffer = (word64 *)(
void *)HashBuffer();
650 switch (2*(m_buffer.size()>=64*1024)
651 #
if CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE || defined(CRYPTOPP_X64_MASM_AVAILABLE)
660 byte *table = MulTable();
661 word64 x0 = hashBuffer[0], x1 = hashBuffer[1];
665 word64 y0, y1, a0, a1, b0, b1, c0, c1, d0, d1;
666 Block::Get(data)(y0)(y1);
670 data += HASH_BLOCKSIZE;
671 len -= HASH_BLOCKSIZE;
673 #define READ_TABLE_WORD64_COMMON(a, b, c, d) *(word64 *)(void *)(table+(a*1024)+(b*256)+c+d*8)
675 #ifdef IS_LITTLE_ENDIAN
676 #if CRYPTOPP_BOOL_SLOW_WORD64
677 word32 z0 = (word32)x0;
678 word32 z1 = (word32)(x0>>32);
679 word32 z2 = (word32)x1;
680 word32 z3 = (word32)(x1>>32);
681 #define READ_TABLE_WORD64(a, b, c, d, e) READ_TABLE_WORD64_COMMON((d%2), c, (d?(z##c>>((d?d-1:0)*4))&0xf0:(z##c&0xf)<<4), e)
683 #define READ_TABLE_WORD64(a, b, c, d, e) READ_TABLE_WORD64_COMMON((d%2), c, ((d+8*b)?(x##a>>(((d+8*b)?(d+8*b)-1:1)*4))&0xf0:(x##a&0xf)<<4), e)
685 #define GF_MOST_SIG_8BITS(a) (a##1 >> 7*8)
686 #define GF_SHIFT_8(a) a##1 = (a##1 << 8) ^ (a##0 >> 7*8); a##0 <<= 8;
688 #define READ_TABLE_WORD64(a, b, c, d, e) READ_TABLE_WORD64_COMMON((1-d%2), c, ((15-d-8*b)?(x##a>>(((15-d-8*b)?(15-d-8*b)-1:0)*4))&0xf0:(x##a&0xf)<<4), e)
689 #define GF_MOST_SIG_8BITS(a) (a##1 & 0xff)
690 #define GF_SHIFT_8(a) a##1 = (a##1 >> 8) ^ (a##0 << 7*8); a##0 >>= 8;
693 #define GF_MUL_32BY128(op, a, b, c) \
694 a0 op READ_TABLE_WORD64(a, b, c, 0, 0) ^ READ_TABLE_WORD64(a, b, c, 1, 0);\
695 a1 op READ_TABLE_WORD64(a, b, c, 0, 1) ^ READ_TABLE_WORD64(a, b, c, 1, 1);\
696 b0 op READ_TABLE_WORD64(a, b, c, 2, 0) ^ READ_TABLE_WORD64(a, b, c, 3, 0);\
697 b1 op READ_TABLE_WORD64(a, b, c, 2, 1) ^ READ_TABLE_WORD64(a, b, c, 3, 1);\
698 c0 op READ_TABLE_WORD64(a, b, c, 4, 0) ^ READ_TABLE_WORD64(a, b, c, 5, 0);\
699 c1 op READ_TABLE_WORD64(a, b, c, 4, 1) ^ READ_TABLE_WORD64(a, b, c, 5, 1);\
700 d0 op READ_TABLE_WORD64(a, b, c, 6, 0) ^ READ_TABLE_WORD64(a, b, c, 7, 0);\
701 d1 op READ_TABLE_WORD64(a, b, c, 6, 1) ^ READ_TABLE_WORD64(a, b, c, 7, 1);\
703 GF_MUL_32BY128(=, 0, 0, 0)
704 GF_MUL_32BY128(^=, 0, 1, 1)
705 GF_MUL_32BY128(^=, 1, 0, 2)
706 GF_MUL_32BY128(^=, 1, 1, 3)
708 word32 r = (word32)s_reductionTable[GF_MOST_SIG_8BITS(d)] << 16;
711 r ^= (word32)s_reductionTable[GF_MOST_SIG_8BITS(c)] << 8;
714 r ^= s_reductionTable[GF_MOST_SIG_8BITS(b)];
720 while (len >= HASH_BLOCKSIZE);
722 hashBuffer[0] = x0; hashBuffer[1] = x1;
728 byte *table = MulTable();
729 word64 x0 = hashBuffer[0], x1 = hashBuffer[1];
733 word64 y0, y1, a0, a1;
734 Block::Get(data)(y0)(y1);
738 data += HASH_BLOCKSIZE;
739 len -= HASH_BLOCKSIZE;
741 #undef READ_TABLE_WORD64_COMMON
742 #undef READ_TABLE_WORD64
744 #define READ_TABLE_WORD64_COMMON(a, c, d) *(word64 *)(void *)(table+(a)*256*16+(c)+(d)*8)
746 #ifdef IS_LITTLE_ENDIAN
747 #if CRYPTOPP_BOOL_SLOW_WORD64
748 word32 z0 = (word32)x0;
749 word32 z1 = (word32)(x0>>32);
750 word32 z2 = (word32)x1;
751 word32 z3 = (word32)(x1>>32);
752 #define READ_TABLE_WORD64(b, c, d, e) READ_TABLE_WORD64_COMMON(c*4+d, (d?(z##c>>((d?d:1)*8-4))&0xff0:(z##c&0xff)<<4), e)
754 #define READ_TABLE_WORD64(b, c, d, e) READ_TABLE_WORD64_COMMON(c*4+d, ((d+4*(c%2))?(x##b>>(((d+4*(c%2))?(d+4*(c%2)):1)*8-4))&0xff0:(x##b&0xff)<<4), e)
757 #define READ_TABLE_WORD64(b, c, d, e) READ_TABLE_WORD64_COMMON(c*4+d, ((7-d-4*(c%2))?(x##b>>(((7-d-4*(c%2))?(7-d-4*(c%2)):1)*8-4))&0xff0:(x##b&0xff)<<4), e)
760 #define GF_MUL_8BY128(op, b, c, d) \
761 a0 op READ_TABLE_WORD64(b, c, d, 0);\
762 a1 op READ_TABLE_WORD64(b, c, d, 1);\
764 GF_MUL_8BY128(=, 0, 0, 0)
765 GF_MUL_8BY128(^=, 0, 0, 1)
766 GF_MUL_8BY128(^=, 0, 0, 2)
767 GF_MUL_8BY128(^=, 0, 0, 3)
768 GF_MUL_8BY128(^=, 0, 1, 0)
769 GF_MUL_8BY128(^=, 0, 1, 1)
770 GF_MUL_8BY128(^=, 0, 1, 2)
771 GF_MUL_8BY128(^=, 0, 1, 3)
772 GF_MUL_8BY128(^=, 1, 2, 0)
773 GF_MUL_8BY128(^=, 1, 2, 1)
774 GF_MUL_8BY128(^=, 1, 2, 2)
775 GF_MUL_8BY128(^=, 1, 2, 3)
776 GF_MUL_8BY128(^=, 1, 3, 0)
777 GF_MUL_8BY128(^=, 1, 3, 1)
778 GF_MUL_8BY128(^=, 1, 3, 2)
779 GF_MUL_8BY128(^=, 1, 3, 3)
783 while (len >= HASH_BLOCKSIZE);
785 hashBuffer[0] = x0; hashBuffer[1] = x1;
788 #endif // #ifndef CRYPTOPP_GENERATE_X64_MASM
790 #ifdef CRYPTOPP_X64_MASM_AVAILABLE
792 GCM_AuthenticateBlocks_2K(data, len/16, hashBuffer, s_reductionTable);
795 GCM_AuthenticateBlocks_64K(data, len/16, hashBuffer);
799 #if CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE
806 #elif defined(CRYPTOPP_GENERATE_X64_MASM)
808 GCM_AuthenticateBlocks_2K PROC FRAME
816 AS2( mov WORD_REG(cx), data )
817 AS2( mov WORD_REG(dx), len )
818 AS2( mov WORD_REG(si), hashBuffer )
819 AS2( shr WORD_REG(dx), 4 )
822 #
if CRYPTOPP_BOOL_X32
831 AS2( mov AS_REG_7, WORD_REG(di))
832 #elif CRYPTOPP_BOOL_X86
833 AS2( lea AS_REG_7, s_reductionTable)
836 AS2( movdqa xmm0, [WORD_REG(si)] )
838 #define MUL_TABLE_0 WORD_REG(si) + 32
839 #define MUL_TABLE_1 WORD_REG(si) + 32 + 1024
840 #define RED_TABLE AS_REG_7
843 AS2( movdqu xmm4, [WORD_REG(cx)] )
844 AS2( pxor xmm0, xmm4 )
846 AS2( movd ebx, xmm0 )
847 AS2( mov eax, AS_HEX(f0f0f0f0) )
850 AS2( and ebx, AS_HEX(f0f0f0f0) )
852 AS2( movdqa xmm5, XMMWORD_PTR [MUL_TABLE_1 + WORD_REG(di)] )
854 AS2( movdqa xmm4, XMMWORD_PTR [MUL_TABLE_1 + WORD_REG(di)] )
857 AS2( movdqa xmm3, XMMWORD_PTR [MUL_TABLE_1 + WORD_REG(di)] )
859 AS2( movdqa xmm2, XMMWORD_PTR [MUL_TABLE_1 + WORD_REG(di)] )
861 #define SSE2_MUL_32BITS(i) \
862 AS2( psrldq xmm0, 4 )\
863 AS2( movd eax, xmm0 )\
864 AS2( and eax, AS_HEX(f0f0f0f0) )\
865 AS2( movzx edi, bh )\
866 AS2( pxor xmm5, XMMWORD_PTR [MUL_TABLE_0 + (i-1)*256 + WORD_REG(di)] )\
867 AS2( movzx edi, bl )\
868 AS2( pxor xmm4, XMMWORD_PTR [MUL_TABLE_0 + (i-1)*256 + WORD_REG(di)] )\
870 AS2( movzx edi, bh )\
871 AS2( pxor xmm3, XMMWORD_PTR [MUL_TABLE_0 + (i-1)*256 + WORD_REG(di)] )\
872 AS2( movzx edi, bl )\
873 AS2( pxor xmm2, XMMWORD_PTR [MUL_TABLE_0 + (i-1)*256 + WORD_REG(di)] )\
874 AS2( movd ebx, xmm0 )\
876 AS2( and ebx, AS_HEX(f0f0f0f0) )\
877 AS2( movzx edi, ah )\
878 AS2( pxor xmm5, XMMWORD_PTR [MUL_TABLE_1 + i*256 + WORD_REG(di)] )\
879 AS2( movzx edi, al )\
880 AS2( pxor xmm4, XMMWORD_PTR [MUL_TABLE_1 + i*256 + WORD_REG(di)] )\
882 AS2( movzx edi, ah )\
883 AS2( pxor xmm3, XMMWORD_PTR [MUL_TABLE_1 + i*256 + WORD_REG(di)] )\
884 AS2( movzx edi, al )\
885 AS2( pxor xmm2, XMMWORD_PTR [MUL_TABLE_1 + i*256 + WORD_REG(di)] )\
892 AS2( pxor xmm5, XMMWORD_PTR [MUL_TABLE_0 + 3*256 + WORD_REG(di)] )
894 AS2( pxor xmm4, XMMWORD_PTR [MUL_TABLE_0 + 3*256 + WORD_REG(di)] )
897 AS2( pxor xmm3, XMMWORD_PTR [MUL_TABLE_0 + 3*256 + WORD_REG(di)] )
899 AS2( pxor xmm2, XMMWORD_PTR [MUL_TABLE_0 + 3*256 + WORD_REG(di)] )
901 AS2( movdqa xmm0, xmm3 )
902 AS2( pslldq xmm3, 1 )
903 AS2( pxor xmm2, xmm3 )
904 AS2( movdqa xmm1, xmm2 )
905 AS2( pslldq xmm2, 1 )
906 AS2( pxor xmm5, xmm2 )
908 AS2( psrldq xmm0, 15 )
910 AS2( movd edi, xmm0 )
911 #elif USE_MOV_REG32_OR_REG64
912 AS2( mov WORD_REG(di), xmm0 )
914 AS2( movd WORD_REG(di), xmm0 )
916 AS2( movzx eax, WORD PTR [RED_TABLE + WORD_REG(di)*2] )
919 AS2( movdqa xmm0, xmm5 )
920 AS2( pslldq xmm5, 1 )
921 AS2( pxor xmm4, xmm5 )
923 AS2( psrldq xmm1, 15 )
925 AS2( movd edi, xmm1 )
926 #elif USE_MOV_REG32_OR_REG64
927 AS2( mov WORD_REG(di), xmm1 )
929 AS2( movd WORD_REG(di), xmm1 )
931 AS2( xor ax, WORD PTR [RED_TABLE + WORD_REG(di)*2] )
934 AS2( psrldq xmm0, 15 )
936 AS2( movd edi, xmm0 )
937 #elif USE_MOV_REG32_OR_REG64
938 AS2( mov WORD_REG(di), xmm0 )
940 AS2( movd WORD_REG(di), xmm0 )
942 AS2( xor ax, WORD PTR [RED_TABLE + WORD_REG(di)*2] )
944 AS2( movd xmm0, eax )
945 AS2( pxor xmm0, xmm4 )
947 AS2( add WORD_REG(cx), 16 )
948 AS2( sub WORD_REG(dx), 1 )
952 AS2( movdqa [WORD_REG(si)], xmm0 )
954 #
if CRYPTOPP_BOOL_X32
965 :
"c" (data),
"d" (len/16),
"S" (hashBuffer),
"D" (s_reductionTable)
966 :
"memory",
"cc",
"%eax"
967 #
if CRYPTOPP_BOOL_X64
971 #elif defined(CRYPTOPP_GENERATE_X64_MASM)
976 GCM_AuthenticateBlocks_2K ENDP
987 #elif defined(CRYPTOPP_GENERATE_X64_MASM)
989 GCM_AuthenticateBlocks_64K PROC FRAME
995 AS2( mov WORD_REG(cx), data )
996 AS2( mov WORD_REG(dx), len )
997 AS2( mov WORD_REG(si), hashBuffer )
998 AS2( shr WORD_REG(dx), 4 )
1001 AS2( movdqa xmm0, [WORD_REG(si)] )
1004 #define MUL_TABLE(i,j) WORD_REG(si) + 32 + (i*4+j)*256*16
1007 AS2( movdqu xmm1, [WORD_REG(cx)] )
1008 AS2( pxor xmm1, xmm0 )
1009 AS2( pxor xmm0, xmm0 )
1011 #undef SSE2_MUL_32BITS
1012 #define SSE2_MUL_32BITS(i) \
1013 AS2( movd eax, xmm1 )\
1014 AS2( psrldq xmm1, 4 )\
1015 AS2( movzx edi, al )\
1016 AS2( add WORD_REG(di), WORD_REG(di) )\
1017 AS2( pxor xmm0, [MUL_TABLE(i,0) + WORD_REG(di)*8] )\
1018 AS2( movzx edi, ah )\
1019 AS2( add WORD_REG(di), WORD_REG(di) )\
1020 AS2( pxor xmm0, [MUL_TABLE(i,1) + WORD_REG(di)*8] )\
1022 AS2( movzx edi, al )\
1023 AS2( add WORD_REG(di), WORD_REG(di) )\
1024 AS2( pxor xmm0, [MUL_TABLE(i,2) + WORD_REG(di)*8] )\
1025 AS2( movzx edi, ah )\
1026 AS2( add WORD_REG(di), WORD_REG(di) )\
1027 AS2( pxor xmm0, [MUL_TABLE(i,3) + WORD_REG(di)*8] )\
1034 AS2( add WORD_REG(cx), 16 )
1035 AS2( sub WORD_REG(dx), 1 )
1039 AS2( movdqa [WORD_REG(si)], xmm0 )
1044 :
"c" (data),
"d" (len/16),
"S" (hashBuffer)
1045 :
"memory",
"cc",
"%edi",
"%eax"
1047 #elif defined(CRYPTOPP_GENERATE_X64_MASM)
1051 GCM_AuthenticateBlocks_64K ENDP
1057 #ifndef CRYPTOPP_GENERATE_X64_MASM
1063 void GCM_Base::AuthenticateLastHeaderBlock()
1065 if (m_bufferedDataLength > 0)
1067 memset(m_buffer+m_bufferedDataLength, 0, HASH_BLOCKSIZE-m_bufferedDataLength);
1068 m_bufferedDataLength = 0;
1069 GCM_Base::AuthenticateBlocks(m_buffer, HASH_BLOCKSIZE);
1073 void GCM_Base::AuthenticateLastConfidentialBlock()
1075 GCM_Base::AuthenticateLastHeaderBlock();
1077 GCM_Base::AuthenticateBlocks(m_buffer, HASH_BLOCKSIZE);
1080 void GCM_Base::AuthenticateLastFooterBlock(
byte *mac,
size_t macSize)
1083 ReverseHashBufferIfNeeded();
1084 m_ctr.ProcessData(mac, HashBuffer(), macSize);
1089 #endif // #ifndef CRYPTOPP_GENERATE_X64_MASM