7 #ifndef CRYPTOPP_IMPORTS
8 #ifndef CRYPTOPP_GENERATE_X64_MASM
13 NAMESPACE_BEGIN(CryptoPP)
15 word16
GCM_Base::s_reductionTable[256];
16 volatile
bool GCM_Base::s_reductionTableInitialized = false;
18 void GCM_Base::GCTR::IncrementCounterBy256()
20 IncrementCounterByOne(m_counterArray+
BlockSize()-4, 3);
25 void gcm_gf_mult(
const unsigned char *a,
const unsigned char *b,
unsigned char *c)
27 word64 Z0=0, Z1=0, V0, V1;
30 Block::Get(a)(V0)(V1);
32 for (
int i=0; i<16; i++)
34 for (
int j=0x80; j!=0; j>>=1)
40 V1 = (V1>>1) | (V0<<63);
41 V0 = (V0>>1) ^ (x ? W64LIT(0xe1) << 56 : 0);
47 __m128i _mm_clmulepi64_si128(
const __m128i &a,
const __m128i &b,
int i)
49 word64 A[1] = {ByteReverse(((word64*)&a)[i&1])};
50 word64 B[1] = {ByteReverse(((word64*)&b)[i>>4])};
57 for (
int i=0; i<16; i++)
58 ((byte *)&output)[i] = c.
GetByte(i);
63 #if CRYPTOPP_BOOL_SSE2_INTRINSICS_AVAILABLE || CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE
64 inline static void SSE2_Xor16(byte *a,
const byte *b,
const byte *c)
66 #if CRYPTOPP_BOOL_SSE2_INTRINSICS_AVAILABLE
67 *(__m128i *)a = _mm_xor_si128(*(__m128i *)b, *(__m128i *)c);
69 asm (
"movdqa %1, %%xmm0; pxor %2, %%xmm0; movdqa %%xmm0, %0;" :
"=m" (a[0]) :
"m"(b[0]),
"m"(c[0]));
74 inline static void Xor16(byte *a,
const byte *b,
const byte *c)
76 ((word64 *)a)[0] = ((word64 *)b)[0] ^ ((word64 *)c)[0];
77 ((word64 *)a)[1] = ((word64 *)b)[1] ^ ((word64 *)c)[1];
80 #if CRYPTOPP_BOOL_AESNI_INTRINSICS_AVAILABLE
81 static CRYPTOPP_ALIGN_DATA(16) const word64 s_clmulConstants64[] = {
82 W64LIT(0xe100000000000000), W64LIT(0xc200000000000000),
83 W64LIT(0x08090a0b0c0d0e0f), W64LIT(0x0001020304050607),
84 W64LIT(0x0001020304050607), W64LIT(0x08090a0b0c0d0e0f)};
85 static const __m128i *s_clmulConstants = (
const __m128i *)s_clmulConstants64;
86 static const unsigned int s_clmulTableSizeInBlocks = 8;
88 inline __m128i CLMUL_Reduce(__m128i c0, __m128i c1, __m128i c2,
const __m128i &r)
102 #if 0 // MSVC 2010 workaround: see http://connect.microsoft.com/VisualStudio/feedback/details/575301
103 c2 = _mm_xor_si128(c2, _mm_move_epi64(c0));
105 c1 = _mm_xor_si128(c1, _mm_slli_si128(c0, 8));
107 c1 = _mm_xor_si128(c1, _mm_clmulepi64_si128(c0, r, 0x10));
108 c0 = _mm_srli_si128(c0, 8);
109 c0 = _mm_xor_si128(c0, c1);
110 c0 = _mm_slli_epi64(c0, 1);
111 c0 = _mm_clmulepi64_si128(c0, r, 0);
112 c2 = _mm_xor_si128(c2, c0);
113 c2 = _mm_xor_si128(c2, _mm_srli_si128(c1, 8));
114 c1 = _mm_unpacklo_epi64(c1, c2);
115 c1 = _mm_srli_epi64(c1, 63);
116 c2 = _mm_slli_epi64(c2, 1);
117 return _mm_xor_si128(c2, c1);
120 inline __m128i CLMUL_GF_Mul(
const __m128i &x,
const __m128i &h,
const __m128i &r)
122 __m128i c0 = _mm_clmulepi64_si128(x,h,0);
123 __m128i c1 = _mm_xor_si128(_mm_clmulepi64_si128(x,h,1), _mm_clmulepi64_si128(x,h,0x10));
124 __m128i c2 = _mm_clmulepi64_si128(x,h,0x11);
126 return CLMUL_Reduce(c0, c1, c2, r);
130 void GCM_Base::SetKeyWithoutResync(
const byte *userKey,
size_t keylength,
const NameValuePairs ¶ms)
133 blockCipher.
SetKey(userKey, keylength, params);
135 if (blockCipher.
BlockSize() != REQUIRED_BLOCKSIZE)
138 int tableSize, i, j, k;
140 #if CRYPTOPP_BOOL_AESNI_INTRINSICS_AVAILABLE
144 tableSize = s_clmulTableSizeInBlocks * REQUIRED_BLOCKSIZE;
150 tableSize = (tableSize >= 64*1024) ? 64*1024 : 2*1024;
152 tableSize = (GetTablesOption() == GCM_64K_Tables) ? 64*1024 : 2*1024;
154 #if defined(_MSC_VER) && (_MSC_VER >= 1300 && _MSC_VER < 1400)
160 m_buffer.
resize(3*REQUIRED_BLOCKSIZE + tableSize);
161 byte *table = MulTable();
162 byte *hashKey = HashKey();
163 memset(hashKey, 0, REQUIRED_BLOCKSIZE);
166 #if CRYPTOPP_BOOL_AESNI_INTRINSICS_AVAILABLE
169 const __m128i r = s_clmulConstants[0];
170 __m128i h0 = _mm_shuffle_epi8(_mm_load_si128((__m128i *)hashKey), s_clmulConstants[1]);
173 for (i=0; i<tableSize; i+=32)
175 __m128i h1 = CLMUL_GF_Mul(h, h0, r);
176 _mm_storel_epi64((__m128i *)(table+i), h);
177 _mm_storeu_si128((__m128i *)(table+i+16), h1);
178 _mm_storeu_si128((__m128i *)(table+i+8), h);
179 _mm_storel_epi64((__m128i *)(table+i+8), h1);
180 h = CLMUL_GF_Mul(h1, h0, r);
189 Block::Get(hashKey)(V0)(V1);
191 if (tableSize == 64*1024)
193 for (i=0; i<128; i++)
196 Block::Put(NULL, table+(i/8)*256*16+(
size_t(1)<<(11-k)))(V0)(V1);
199 V1 = (V1>>1) | (V0<<63);
200 V0 = (V0>>1) ^ (x ? W64LIT(0xe1) << 56 : 0);
205 memset(table+i*256*16, 0, 16);
206 #if CRYPTOPP_BOOL_SSE2_INTRINSICS_AVAILABLE || CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE
208 for (j=2; j<=0x80; j*=2)
210 SSE2_Xor16(table+i*256*16+(j+k)*16, table+i*256*16+j*16, table+i*256*16+k*16);
213 for (j=2; j<=0x80; j*=2)
215 Xor16(table+i*256*16+(j+k)*16, table+i*256*16+j*16, table+i*256*16+k*16);
220 if (!s_reductionTableInitialized)
222 s_reductionTable[0] = 0;
224 s_reductionTable[1] = ByteReverse(x);
225 for (
int i=2; i<=0x80; i*=2)
228 s_reductionTable[i] = ByteReverse(x);
229 for (
int j=1; j<i; j++)
230 s_reductionTable[i+j] = s_reductionTable[i] ^ s_reductionTable[j];
232 s_reductionTableInitialized =
true;
235 for (i=0; i<128-24; i++)
239 Block::Put(NULL, table+1024+(i/32)*256+(
size_t(1)<<(7-k)))(V0)(V1);
241 Block::Put(NULL, table+(i/32)*256+(
size_t(1)<<(11-k)))(V0)(V1);
244 V1 = (V1>>1) | (V0<<63);
245 V0 = (V0>>1) ^ (x ? W64LIT(0xe1) << 56 : 0);
250 memset(table+i*256, 0, 16);
251 memset(table+1024+i*256, 0, 16);
252 #if CRYPTOPP_BOOL_SSE2_INTRINSICS_AVAILABLE || CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE
254 for (j=2; j<=8; j*=2)
257 SSE2_Xor16(table+i*256+(j+k)*16, table+i*256+j*16, table+i*256+k*16);
258 SSE2_Xor16(table+1024+i*256+(j+k)*16, table+1024+i*256+j*16, table+1024+i*256+k*16);
262 for (j=2; j<=8; j*=2)
265 Xor16(table+i*256+(j+k)*16, table+i*256+j*16, table+i*256+k*16);
266 Xor16(table+1024+i*256+(j+k)*16, table+1024+i*256+j*16, table+1024+i*256+k*16);
272 inline void GCM_Base::ReverseHashBufferIfNeeded()
274 #if CRYPTOPP_BOOL_AESNI_INTRINSICS_AVAILABLE
277 __m128i &x = *(__m128i *)HashBuffer();
278 x = _mm_shuffle_epi8(x, s_clmulConstants[1]);
283 void GCM_Base::Resync(
const byte *iv,
size_t len)
286 byte *hashBuffer = HashBuffer();
290 memcpy(hashBuffer, iv, len);
291 memset(hashBuffer+len, 0, 3);
292 hashBuffer[len+3] = 1;
296 size_t origLen = len;
297 memset(hashBuffer, 0, HASH_BLOCKSIZE);
299 if (len >= HASH_BLOCKSIZE)
301 len = GCM_Base::AuthenticateBlocks(iv, len);
302 iv += (origLen - len);
307 memcpy(m_buffer, iv, len);
308 memset(m_buffer+len, 0, HASH_BLOCKSIZE-len);
309 GCM_Base::AuthenticateBlocks(m_buffer, HASH_BLOCKSIZE);
313 GCM_Base::AuthenticateBlocks(m_buffer, HASH_BLOCKSIZE);
315 ReverseHashBufferIfNeeded();
318 if (m_state >= State_IVSet)
319 m_ctr.Resynchronize(hashBuffer, REQUIRED_BLOCKSIZE);
321 m_ctr.SetCipherWithIV(cipher, hashBuffer);
323 m_ctr.Seek(HASH_BLOCKSIZE);
325 memset(hashBuffer, 0, HASH_BLOCKSIZE);
331 #if CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE || defined(CRYPTOPP_X64_MASM_AVAILABLE)
337 #pragma warning(disable: 4731) // frame pointer register 'ebp' modified by inline assembly code
339 #endif // #ifndef CRYPTOPP_GENERATE_X64_MASM
341 #ifdef CRYPTOPP_X64_MASM_AVAILABLE
343 void GCM_AuthenticateBlocks_2K(
const byte *data,
size_t blocks, word64 *hashBuffer,
const word16 *reductionTable);
344 void GCM_AuthenticateBlocks_64K(
const byte *data,
size_t blocks, word64 *hashBuffer);
348 #ifndef CRYPTOPP_GENERATE_X64_MASM
350 size_t GCM_Base::AuthenticateBlocks(
const byte *data,
size_t len)
352 #if CRYPTOPP_BOOL_AESNI_INTRINSICS_AVAILABLE
355 const __m128i *table = (
const __m128i *)MulTable();
356 __m128i x = _mm_load_si128((__m128i *)HashBuffer());
357 const __m128i r = s_clmulConstants[0], bswapMask = s_clmulConstants[1], bswapMask2 = s_clmulConstants[2];
361 size_t s = UnsignedMin(len/16, s_clmulTableSizeInBlocks), i=0;
362 __m128i d, d2 = _mm_shuffle_epi8(_mm_loadu_si128((
const __m128i *)(data+(s-1)*16)), bswapMask2);;
363 __m128i c0 = _mm_setzero_si128();
364 __m128i c1 = _mm_setzero_si128();
365 __m128i c2 = _mm_setzero_si128();
369 __m128i h0 = _mm_load_si128(table+i);
370 __m128i h1 = _mm_load_si128(table+i+1);
371 __m128i h01 = _mm_xor_si128(h0, h1);
375 d = _mm_shuffle_epi8(_mm_loadu_si128((
const __m128i *)data), bswapMask);
376 d = _mm_xor_si128(d, x);
377 c0 = _mm_xor_si128(c0, _mm_clmulepi64_si128(d, h0, 0));
378 c2 = _mm_xor_si128(c2, _mm_clmulepi64_si128(d, h1, 1));
379 d = _mm_xor_si128(d, _mm_shuffle_epi32(d, _MM_SHUFFLE(1, 0, 3, 2)));
380 c1 = _mm_xor_si128(c1, _mm_clmulepi64_si128(d, h01, 0));
384 d = _mm_shuffle_epi8(_mm_loadu_si128((
const __m128i *)(data+(s-i)*16-8)), bswapMask2);
385 c0 = _mm_xor_si128(c0, _mm_clmulepi64_si128(d2, h0, 1));
386 c2 = _mm_xor_si128(c2, _mm_clmulepi64_si128(d, h1, 1));
387 d2 = _mm_xor_si128(d2, d);
388 c1 = _mm_xor_si128(c1, _mm_clmulepi64_si128(d2, h01, 1));
392 d = _mm_shuffle_epi8(_mm_loadu_si128((
const __m128i *)data), bswapMask);
393 d = _mm_xor_si128(d, x);
394 c0 = _mm_xor_si128(c0, _mm_clmulepi64_si128(d, h0, 0x10));
395 c2 = _mm_xor_si128(c2, _mm_clmulepi64_si128(d, h1, 0x11));
396 d = _mm_xor_si128(d, _mm_shuffle_epi32(d, _MM_SHUFFLE(1, 0, 3, 2)));
397 c1 = _mm_xor_si128(c1, _mm_clmulepi64_si128(d, h01, 0x10));
401 d2 = _mm_shuffle_epi8(_mm_loadu_si128((
const __m128i *)(data+(s-i)*16-8)), bswapMask);
402 c0 = _mm_xor_si128(c0, _mm_clmulepi64_si128(d, h0, 0x10));
403 c2 = _mm_xor_si128(c2, _mm_clmulepi64_si128(d2, h1, 0x10));
404 d = _mm_xor_si128(d, d2);
405 c1 = _mm_xor_si128(c1, _mm_clmulepi64_si128(d, h01, 0x10));
410 c1 = _mm_xor_si128(_mm_xor_si128(c1, c0), c2);
411 x = CLMUL_Reduce(c0, c1, c2, r);
414 _mm_store_si128((__m128i *)HashBuffer(), x);
420 word64 *hashBuffer = (word64 *)HashBuffer();
422 switch (2*(m_buffer.size()>=64*1024)
423 #
if CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE || defined(CRYPTOPP_X64_MASM_AVAILABLE)
430 byte *table = MulTable();
431 word64 x0 = hashBuffer[0], x1 = hashBuffer[1];
435 word64 y0, y1, a0, a1, b0, b1, c0, c1, d0, d1;
436 Block::Get(data)(y0)(y1);
440 data += HASH_BLOCKSIZE;
441 len -= HASH_BLOCKSIZE;
443 #define READ_TABLE_WORD64_COMMON(a, b, c, d) *(word64 *)(table+(a*1024)+(b*256)+c+d*8)
445 #ifdef IS_LITTLE_ENDIAN
446 #if CRYPTOPP_BOOL_SLOW_WORD64
447 word32 z0 = (word32)x0;
448 word32 z1 = (word32)(x0>>32);
449 word32 z2 = (word32)x1;
450 word32 z3 = (word32)(x1>>32);
451 #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)
453 #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)
455 #define GF_MOST_SIG_8BITS(a) (a##1 >> 7*8)
456 #define GF_SHIFT_8(a) a##1 = (a##1 << 8) ^ (a##0 >> 7*8); a##0 <<= 8;
458 #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)
459 #define GF_MOST_SIG_8BITS(a) (a##1 & 0xff)
460 #define GF_SHIFT_8(a) a##1 = (a##1 >> 8) ^ (a##0 << 7*8); a##0 >>= 8;
463 #define GF_MUL_32BY128(op, a, b, c) \
464 a0 op READ_TABLE_WORD64(a, b, c, 0, 0) ^ READ_TABLE_WORD64(a, b, c, 1, 0);\
465 a1 op READ_TABLE_WORD64(a, b, c, 0, 1) ^ READ_TABLE_WORD64(a, b, c, 1, 1);\
466 b0 op READ_TABLE_WORD64(a, b, c, 2, 0) ^ READ_TABLE_WORD64(a, b, c, 3, 0);\
467 b1 op READ_TABLE_WORD64(a, b, c, 2, 1) ^ READ_TABLE_WORD64(a, b, c, 3, 1);\
468 c0 op READ_TABLE_WORD64(a, b, c, 4, 0) ^ READ_TABLE_WORD64(a, b, c, 5, 0);\
469 c1 op READ_TABLE_WORD64(a, b, c, 4, 1) ^ READ_TABLE_WORD64(a, b, c, 5, 1);\
470 d0 op READ_TABLE_WORD64(a, b, c, 6, 0) ^ READ_TABLE_WORD64(a, b, c, 7, 0);\
471 d1 op READ_TABLE_WORD64(a, b, c, 6, 1) ^ READ_TABLE_WORD64(a, b, c, 7, 1);\
473 GF_MUL_32BY128(=, 0, 0, 0)
474 GF_MUL_32BY128(^=, 0, 1, 1)
475 GF_MUL_32BY128(^=, 1, 0, 2)
476 GF_MUL_32BY128(^=, 1, 1, 3)
478 word32 r = (word32)s_reductionTable[GF_MOST_SIG_8BITS(d)] << 16;
481 r ^= (word32)s_reductionTable[GF_MOST_SIG_8BITS(c)] << 8;
484 r ^= s_reductionTable[GF_MOST_SIG_8BITS(b)];
487 a0 ^= ConditionalByteReverse<word64>(LITTLE_ENDIAN_ORDER, r);
490 while (len >= HASH_BLOCKSIZE);
492 hashBuffer[0] = x0; hashBuffer[1] = x1;
498 byte *table = MulTable();
499 word64 x0 = hashBuffer[0], x1 = hashBuffer[1];
503 word64 y0, y1, a0, a1;
504 Block::Get(data)(y0)(y1);
508 data += HASH_BLOCKSIZE;
509 len -= HASH_BLOCKSIZE;
511 #undef READ_TABLE_WORD64_COMMON
512 #undef READ_TABLE_WORD64
514 #define READ_TABLE_WORD64_COMMON(a, c, d) *(word64 *)(table+(a)*256*16+(c)+(d)*8)
516 #ifdef IS_LITTLE_ENDIAN
517 #if CRYPTOPP_BOOL_SLOW_WORD64
518 word32 z0 = (word32)x0;
519 word32 z1 = (word32)(x0>>32);
520 word32 z2 = (word32)x1;
521 word32 z3 = (word32)(x1>>32);
522 #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)
524 #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)
527 #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)
530 #define GF_MUL_8BY128(op, b, c, d) \
531 a0 op READ_TABLE_WORD64(b, c, d, 0);\
532 a1 op READ_TABLE_WORD64(b, c, d, 1);\
534 GF_MUL_8BY128(=, 0, 0, 0)
535 GF_MUL_8BY128(^=, 0, 0, 1)
536 GF_MUL_8BY128(^=, 0, 0, 2)
537 GF_MUL_8BY128(^=, 0, 0, 3)
538 GF_MUL_8BY128(^=, 0, 1, 0)
539 GF_MUL_8BY128(^=, 0, 1, 1)
540 GF_MUL_8BY128(^=, 0, 1, 2)
541 GF_MUL_8BY128(^=, 0, 1, 3)
542 GF_MUL_8BY128(^=, 1, 2, 0)
543 GF_MUL_8BY128(^=, 1, 2, 1)
544 GF_MUL_8BY128(^=, 1, 2, 2)
545 GF_MUL_8BY128(^=, 1, 2, 3)
546 GF_MUL_8BY128(^=, 1, 3, 0)
547 GF_MUL_8BY128(^=, 1, 3, 1)
548 GF_MUL_8BY128(^=, 1, 3, 2)
549 GF_MUL_8BY128(^=, 1, 3, 3)
553 while (len >= HASH_BLOCKSIZE);
555 hashBuffer[0] = x0; hashBuffer[1] = x1;
558 #endif // #ifndef CRYPTOPP_GENERATE_X64_MASM
560 #ifdef CRYPTOPP_X64_MASM_AVAILABLE
562 GCM_AuthenticateBlocks_2K(data, len/16, hashBuffer, s_reductionTable);
565 GCM_AuthenticateBlocks_64K(data, len/16, hashBuffer);
569 #if CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE
575 ".intel_syntax noprefix;"
576 #elif defined(CRYPTOPP_GENERATE_X64_MASM)
578 GCM_AuthenticateBlocks_2K PROC FRAME
586 AS2( mov WORD_REG(cx), data )
587 AS2( mov WORD_REG(dx), len )
588 AS2( mov WORD_REG(si), hashBuffer )
589 AS2( shr WORD_REG(dx), 4 )
596 AS2( mov AS_REG_7, WORD_REG(di))
597 #elif CRYPTOPP_BOOL_X86
598 AS2( lea AS_REG_7, s_reductionTable)
601 AS2( movdqa xmm0, [WORD_REG(si)] )
603 #define MUL_TABLE_0 WORD_REG(si) + 32
604 #define MUL_TABLE_1 WORD_REG(si) + 32 + 1024
605 #define RED_TABLE AS_REG_7
608 AS2( movdqu xmm4, [WORD_REG(cx)] )
609 AS2( pxor xmm0, xmm4 )
611 AS2( movd ebx, xmm0 )
612 AS2( mov eax, AS_HEX(f0f0f0f0) )
615 AS2( and ebx, AS_HEX(f0f0f0f0) )
617 AS2( movdqa xmm5, XMMWORD_PTR [MUL_TABLE_1 + WORD_REG(di)] )
619 AS2( movdqa xmm4, XMMWORD_PTR [MUL_TABLE_1 + WORD_REG(di)] )
622 AS2( movdqa xmm3, XMMWORD_PTR [MUL_TABLE_1 + WORD_REG(di)] )
624 AS2( movdqa xmm2, XMMWORD_PTR [MUL_TABLE_1 + WORD_REG(di)] )
626 #define SSE2_MUL_32BITS(i) \
627 AS2( psrldq xmm0, 4 )\
628 AS2( movd eax, xmm0 )\
629 AS2( and eax, AS_HEX(f0f0f0f0) )\
630 AS2( movzx edi, bh )\
631 AS2( pxor xmm5, XMMWORD_PTR [MUL_TABLE_0 + (i-1)*256 + WORD_REG(di)] )\
632 AS2( movzx edi, bl )\
633 AS2( pxor xmm4, XMMWORD_PTR [MUL_TABLE_0 + (i-1)*256 + WORD_REG(di)] )\
635 AS2( movzx edi, bh )\
636 AS2( pxor xmm3, XMMWORD_PTR [MUL_TABLE_0 + (i-1)*256 + WORD_REG(di)] )\
637 AS2( movzx edi, bl )\
638 AS2( pxor xmm2, XMMWORD_PTR [MUL_TABLE_0 + (i-1)*256 + WORD_REG(di)] )\
639 AS2( movd ebx, xmm0 )\
641 AS2( and ebx, AS_HEX(f0f0f0f0) )\
642 AS2( movzx edi, ah )\
643 AS2( pxor xmm5, XMMWORD_PTR [MUL_TABLE_1 + i*256 + WORD_REG(di)] )\
644 AS2( movzx edi, al )\
645 AS2( pxor xmm4, XMMWORD_PTR [MUL_TABLE_1 + i*256 + WORD_REG(di)] )\
647 AS2( movzx edi, ah )\
648 AS2( pxor xmm3, XMMWORD_PTR [MUL_TABLE_1 + i*256 + WORD_REG(di)] )\
649 AS2( movzx edi, al )\
650 AS2( pxor xmm2, XMMWORD_PTR [MUL_TABLE_1 + i*256 + WORD_REG(di)] )\
657 AS2( pxor xmm5, XMMWORD_PTR [MUL_TABLE_0 + 3*256 + WORD_REG(di)] )
659 AS2( pxor xmm4, XMMWORD_PTR [MUL_TABLE_0 + 3*256 + WORD_REG(di)] )
662 AS2( pxor xmm3, XMMWORD_PTR [MUL_TABLE_0 + 3*256 + WORD_REG(di)] )
664 AS2( pxor xmm2, XMMWORD_PTR [MUL_TABLE_0 + 3*256 + WORD_REG(di)] )
666 AS2( movdqa xmm0, xmm3 )
667 AS2( pslldq xmm3, 1 )
668 AS2( pxor xmm2, xmm3 )
669 AS2( movdqa xmm1, xmm2 )
670 AS2( pslldq xmm2, 1 )
671 AS2( pxor xmm5, xmm2 )
673 AS2( psrldq xmm0, 15 )
674 AS2( movd WORD_REG(di), xmm0 )
675 AS2( movzx eax, WORD PTR [RED_TABLE + WORD_REG(di)*2] )
678 AS2( movdqa xmm0, xmm5 )
679 AS2( pslldq xmm5, 1 )
680 AS2( pxor xmm4, xmm5 )
682 AS2( psrldq xmm1, 15 )
683 AS2( movd WORD_REG(di), xmm1 )
684 AS2( xor ax, WORD PTR [RED_TABLE + WORD_REG(di)*2] )
687 AS2( psrldq xmm0, 15 )
688 AS2( movd WORD_REG(di), xmm0 )
689 AS2( xor ax, WORD PTR [RED_TABLE + WORD_REG(di)*2] )
691 AS2( movd xmm0, eax )
692 AS2( pxor xmm0, xmm4 )
694 AS2( add WORD_REG(cx), 16 )
695 AS2( sub WORD_REG(dx), 1 )
697 AS2( movdqa [WORD_REG(si)], xmm0 )
703 ".att_syntax prefix;"
705 :
"c" (data),
"d" (len/16),
"S" (hashBuffer),
"D" (s_reductionTable)
706 :
"memory",
"cc",
"%eax"
707 #
if CRYPTOPP_BOOL_X64
711 #elif defined(CRYPTOPP_GENERATE_X64_MASM)
716 GCM_AuthenticateBlocks_2K ENDP
726 ".intel_syntax noprefix;"
727 #elif defined(CRYPTOPP_GENERATE_X64_MASM)
729 GCM_AuthenticateBlocks_64K PROC FRAME
735 AS2( mov WORD_REG(cx), data )
736 AS2( mov WORD_REG(dx), len )
737 AS2( mov WORD_REG(si), hashBuffer )
738 AS2( shr WORD_REG(dx), 4 )
741 AS2( movdqa xmm0, [WORD_REG(si)] )
744 #define MUL_TABLE(i,j) WORD_REG(si) + 32 + (i*4+j)*256*16
747 AS2( movdqu xmm1, [WORD_REG(cx)] )
748 AS2( pxor xmm1, xmm0 )
749 AS2( pxor xmm0, xmm0 )
751 #undef SSE2_MUL_32BITS
752 #define SSE2_MUL_32BITS(i) \
753 AS2( movd eax, xmm1 )\
754 AS2( psrldq xmm1, 4 )\
755 AS2( movzx edi, al )\
756 AS2( add WORD_REG(di), WORD_REG(di) )\
757 AS2( pxor xmm0, [MUL_TABLE(i,0) + WORD_REG(di)*8] )\
758 AS2( movzx edi, ah )\
759 AS2( add WORD_REG(di), WORD_REG(di) )\
760 AS2( pxor xmm0, [MUL_TABLE(i,1) + WORD_REG(di)*8] )\
762 AS2( movzx edi, al )\
763 AS2( add WORD_REG(di), WORD_REG(di) )\
764 AS2( pxor xmm0, [MUL_TABLE(i,2) + WORD_REG(di)*8] )\
765 AS2( movzx edi, ah )\
766 AS2( add WORD_REG(di), WORD_REG(di) )\
767 AS2( pxor xmm0, [MUL_TABLE(i,3) + WORD_REG(di)*8] )\
774 AS2( add WORD_REG(cx), 16 )
775 AS2( sub WORD_REG(dx), 1 )
777 AS2( movdqa [WORD_REG(si)], xmm0 )
780 ".att_syntax prefix;"
782 :
"c" (data),
"d" (len/16),
"S" (hashBuffer)
783 :
"memory",
"cc",
"%edi",
"%eax"
785 #elif defined(CRYPTOPP_GENERATE_X64_MASM)
789 GCM_AuthenticateBlocks_64K ENDP
795 #ifndef CRYPTOPP_GENERATE_X64_MASM
801 void GCM_Base::AuthenticateLastHeaderBlock()
803 if (m_bufferedDataLength > 0)
805 memset(m_buffer+m_bufferedDataLength, 0, HASH_BLOCKSIZE-m_bufferedDataLength);
806 m_bufferedDataLength = 0;
807 GCM_Base::AuthenticateBlocks(m_buffer, HASH_BLOCKSIZE);
811 void GCM_Base::AuthenticateLastConfidentialBlock()
813 GCM_Base::AuthenticateLastHeaderBlock();
815 GCM_Base::AuthenticateBlocks(m_buffer, HASH_BLOCKSIZE);
818 void GCM_Base::AuthenticateLastFooterBlock(byte *mac,
size_t macSize)
821 ReverseHashBufferIfNeeded();
822 m_ctr.ProcessData(mac, HashBuffer(), macSize);
827 #endif // #ifndef CRYPTOPP_GENERATE_X64_MASM