summaryrefslogtreecommitdiff
path: root/cipher/cipher-gcm-intel-pclmul.c
blob: 334c89cd0ea02f0aafa108f90fa4c2562385a92b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
/* cipher-gcm-intel-pclmul.c  -  Intel PCLMUL accelerated Galois Counter Mode
 *                               implementation
 * Copyright (C) 2013-2014,2019 Jussi Kivilinna <jussi.kivilinna@iki.fi>
 *
 * This file is part of Libgcrypt.
 *
 * Libgcrypt is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser general Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * Libgcrypt is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "g10lib.h"
#include "cipher.h"
#include "bufhelp.h"
#include "./cipher-internal.h"


#ifdef GCM_USE_INTEL_PCLMUL


#if _GCRY_GCC_VERSION >= 40400 /* 4.4 */
/* Prevent compiler from issuing SSE instructions between asm blocks. */
#  pragma GCC target("no-sse")
#endif
#if __clang__
#  pragma clang attribute push (__attribute__((target("no-sse"))), apply_to = function)
#endif


#define ALWAYS_INLINE inline __attribute__((always_inline))
#define NO_INSTRUMENT_FUNCTION __attribute__((no_instrument_function))

#define ASM_FUNC_ATTR        NO_INSTRUMENT_FUNCTION
#define ASM_FUNC_ATTR_INLINE ASM_FUNC_ATTR ALWAYS_INLINE


/*
 Intel PCLMUL ghash based on white paper:
  "Intel® Carry-Less Multiplication Instruction and its Usage for Computing the
   GCM Mode - Rev 2.01"; Shay Gueron, Michael E. Kounavis.
 */
static ASM_FUNC_ATTR_INLINE void reduction(void)
{
  /* input: <xmm1:xmm3> */

  asm volatile (/* first phase of the reduction */
                "movdqa %%xmm3, %%xmm6\n\t"
                "movdqa %%xmm3, %%xmm5\n\t"
                "psllq $1, %%xmm6\n\t"  /* packed right shifting << 63 */
                "pxor %%xmm3, %%xmm6\n\t"
                "psllq $57, %%xmm5\n\t"  /* packed right shifting << 57 */
                "psllq $62, %%xmm6\n\t"  /* packed right shifting << 62 */
                "pxor %%xmm5, %%xmm6\n\t" /* xor the shifted versions */
                "pshufd $0x6a, %%xmm6, %%xmm5\n\t"
                "pshufd $0xae, %%xmm6, %%xmm6\n\t"
                "pxor %%xmm5, %%xmm3\n\t" /* first phase of the reduction
                                             complete */

                /* second phase of the reduction */
                "pxor %%xmm3, %%xmm1\n\t" /* xor the shifted versions */
                "psrlq $1, %%xmm3\n\t"    /* packed left shifting >> 1 */
                "pxor %%xmm3, %%xmm6\n\t"
                "psrlq $1, %%xmm3\n\t"    /* packed left shifting >> 2 */
                "pxor %%xmm3, %%xmm1\n\t"
                "psrlq $5, %%xmm3\n\t"    /* packed left shifting >> 7 */
                "pxor %%xmm3, %%xmm6\n\t"
                "pxor %%xmm6, %%xmm1\n\t" /* the result is in xmm1 */
                ::: "memory" );
}

static ASM_FUNC_ATTR_INLINE void gfmul_pclmul(void)
{
  /* Input: XMM0 and XMM1, Output: XMM1. Input XMM0 stays unmodified.
     Input must be converted to little-endian.
   */
  asm volatile (/* gfmul, xmm0 has operator a and xmm1 has operator b. */
                "pshufd $78, %%xmm0, %%xmm2\n\t"
                "pshufd $78, %%xmm1, %%xmm4\n\t"
                "pxor %%xmm0, %%xmm2\n\t" /* xmm2 holds a0+a1 */
                "pxor %%xmm1, %%xmm4\n\t" /* xmm4 holds b0+b1 */

                "movdqa %%xmm0, %%xmm3\n\t"
                "pclmulqdq $0, %%xmm1, %%xmm3\n\t"  /* xmm3 holds a0*b0 */
                "pclmulqdq $17, %%xmm0, %%xmm1\n\t" /* xmm6 holds a1*b1 */
                "movdqa %%xmm3, %%xmm5\n\t"
                "pclmulqdq $0, %%xmm2, %%xmm4\n\t"  /* xmm4 holds (a0+a1)*(b0+b1) */

                "pxor %%xmm1, %%xmm5\n\t" /* xmm5 holds a0*b0+a1*b1 */
                "pxor %%xmm5, %%xmm4\n\t" /* xmm4 holds a0*b0+a1*b1+(a0+a1)*(b0+b1) */
                "movdqa %%xmm4, %%xmm5\n\t"
                "psrldq $8, %%xmm4\n\t"
                "pslldq $8, %%xmm5\n\t"
                "pxor %%xmm5, %%xmm3\n\t"
                "pxor %%xmm4, %%xmm1\n\t" /* <xmm1:xmm3> holds the result of the
                                             carry-less multiplication of xmm0
                                             by xmm1 */
                ::: "memory" );

  reduction();
}

static ASM_FUNC_ATTR_INLINE void
gfmul_pclmul_aggr4(const void *buf, const void *h_1, const void *h_table,
		   const unsigned char *be_mask)
{
  /* Input:
      Hash: XMM1
     Output:
      Hash: XMM1
   */
  asm volatile (/* perform clmul and merge results... */
                "movdqu 2*16(%[h_table]), %%xmm2\n\t" /* Load H4 */
                "movdqu 0*16(%[buf]), %%xmm5\n\t"
                "pshufb %[be_mask], %%xmm5\n\t" /* be => le */
                "pxor %%xmm5, %%xmm1\n\t"

                "pshufd $78, %%xmm2, %%xmm5\n\t"
                "pshufd $78, %%xmm1, %%xmm4\n\t"
                "pxor %%xmm2, %%xmm5\n\t" /* xmm5 holds 4:a0+a1 */
                "pxor %%xmm1, %%xmm4\n\t" /* xmm4 holds 4:b0+b1 */
                "movdqa %%xmm2, %%xmm3\n\t"
                "pclmulqdq $0, %%xmm1, %%xmm3\n\t"   /* xmm3 holds 4:a0*b0 */
                "pclmulqdq $17, %%xmm2, %%xmm1\n\t"  /* xmm1 holds 4:a1*b1 */
                "pclmulqdq $0, %%xmm5, %%xmm4\n\t"   /* xmm4 holds 4:(a0+a1)*(b0+b1) */

                "movdqu 1*16(%[h_table]), %%xmm5\n\t" /* Load H3 */
                "movdqu 1*16(%[buf]), %%xmm2\n\t"
                "pshufb %[be_mask], %%xmm2\n\t" /* be => le */

                "pshufd $78, %%xmm5, %%xmm0\n\t"
                "pshufd $78, %%xmm2, %%xmm7\n\t"
                "pxor %%xmm5, %%xmm0\n\t" /* xmm0 holds 3:a0+a1 */
                "pxor %%xmm2, %%xmm7\n\t" /* xmm7 holds 3:b0+b1 */
                "movdqa %%xmm5, %%xmm6\n\t"
                "pclmulqdq $0, %%xmm2, %%xmm6\n\t"  /* xmm6 holds 3:a0*b0 */
                "pclmulqdq $17, %%xmm5, %%xmm2\n\t" /* xmm2 holds 3:a1*b1 */
                "pclmulqdq $0, %%xmm0, %%xmm7\n\t" /* xmm7 holds 3:(a0+a1)*(b0+b1) */

                "movdqu 2*16(%[buf]), %%xmm5\n\t"
                "pshufb %[be_mask], %%xmm5\n\t" /* be => le */

                "pxor %%xmm6, %%xmm3\n\t" /* xmm3 holds 3+4:a0*b0 */
                "pxor %%xmm2, %%xmm1\n\t" /* xmm1 holds 3+4:a1*b1 */
                "pxor %%xmm7, %%xmm4\n\t" /* xmm4 holds 3+4:(a0+a1)*(b0+b1) */

                "movdqu 0*16(%[h_table]), %%xmm2\n\t" /* Load H2 */

                "pshufd $78, %%xmm2, %%xmm0\n\t"
                "pshufd $78, %%xmm5, %%xmm7\n\t"
                "pxor %%xmm2, %%xmm0\n\t" /* xmm0 holds 2:a0+a1 */
                "pxor %%xmm5, %%xmm7\n\t" /* xmm7 holds 2:b0+b1 */
                "movdqa %%xmm2, %%xmm6\n\t"
                "pclmulqdq $0, %%xmm5, %%xmm6\n\t"  /* xmm6 holds 2:a0*b0 */
                "pclmulqdq $17, %%xmm2, %%xmm5\n\t" /* xmm5 holds 2:a1*b1 */
                "pclmulqdq $0, %%xmm0, %%xmm7\n\t" /* xmm7 holds 2:(a0+a1)*(b0+b1) */

                "movdqu 3*16(%[buf]), %%xmm2\n\t"
                "pshufb %[be_mask], %%xmm2\n\t" /* be => le */
                :
                : [buf] "r" (buf),
                  [h_table] "r" (h_table),
                  [be_mask] "m" (*be_mask)
                : "memory" );

  asm volatile ("pxor %%xmm6, %%xmm3\n\t" /* xmm3 holds 2+3+4:a0*b0 */
                "pxor %%xmm5, %%xmm1\n\t" /* xmm1 holds 2+3+4:a1*b1 */
                "pxor %%xmm7, %%xmm4\n\t" /* xmm4 holds 2+3+4:(a0+a1)*(b0+b1) */

                "movdqu %[h_1], %%xmm5\n\t" /* Load H1 */

                "pshufd $78, %%xmm5, %%xmm0\n\t"
                "pshufd $78, %%xmm2, %%xmm7\n\t"
                "pxor %%xmm5, %%xmm0\n\t" /* xmm0 holds 1:a0+a1 */
                "pxor %%xmm2, %%xmm7\n\t" /* xmm7 holds 1:b0+b1 */
                "movdqa %%xmm5, %%xmm6\n\t"
                "pclmulqdq $0, %%xmm2, %%xmm6\n\t"  /* xmm6 holds 1:a0*b0 */
                "pclmulqdq $17, %%xmm5, %%xmm2\n\t" /* xmm2 holds 1:a1*b1 */
                "pclmulqdq $0, %%xmm0, %%xmm7\n\t" /* xmm7 holds 1:(a0+a1)*(b0+b1) */

                "pxor %%xmm6, %%xmm3\n\t" /* xmm3 holds 1+2+3+4:a0*b0 */
                "pxor %%xmm2, %%xmm1\n\t" /* xmm1 holds 1+2+3+4:a1*b1 */
                "pxor %%xmm7, %%xmm4\n\t" /* xmm4 holds 1+2+3+4:(a0+a1)*(b0+b1) */

                /* aggregated reduction... */
                "movdqa %%xmm3, %%xmm5\n\t"
                "pxor %%xmm1, %%xmm5\n\t" /* xmm5 holds a0*b0+a1*b1 */
                "pxor %%xmm5, %%xmm4\n\t" /* xmm4 holds a0*b0+a1*b1+(a0+a1)*(b0+b1) */
                "movdqa %%xmm4, %%xmm5\n\t"
                "psrldq $8, %%xmm4\n\t"
                "pslldq $8, %%xmm5\n\t"
                "pxor %%xmm5, %%xmm3\n\t"
                "pxor %%xmm4, %%xmm1\n\t" /* <xmm1:xmm3> holds the result of the
                                             carry-less multiplication of xmm0
                                             by xmm1 */
                :
                : [h_1] "m" (*(const unsigned char *)h_1)
                : "memory" );

  reduction();
}

#ifdef __x86_64__
static ASM_FUNC_ATTR_INLINE void
gfmul_pclmul_aggr8(const void *buf, const void *h_table)
{
  /* Input:
      H¹: XMM0
      bemask: XMM15
      Hash: XMM1
     Output:
      Hash: XMM1
     Inputs XMM0 and XMM15 stays unmodified.
   */
  asm volatile (/* Load H6, H7, H8. */
                "movdqu 6*16(%[h_table]), %%xmm10\n\t"
                "movdqu 5*16(%[h_table]), %%xmm9\n\t"
                "movdqu 4*16(%[h_table]), %%xmm8\n\t"

                /* perform clmul and merge results... */
                "movdqu 0*16(%[buf]), %%xmm5\n\t"
                "movdqu 1*16(%[buf]), %%xmm2\n\t"
                "pshufb %%xmm15, %%xmm5\n\t" /* be => le */
                "pshufb %%xmm15, %%xmm2\n\t" /* be => le */
                "pxor %%xmm5, %%xmm1\n\t"

                "pshufd $78, %%xmm10, %%xmm5\n\t"
                "pshufd $78, %%xmm1, %%xmm4\n\t"
                "pxor %%xmm10, %%xmm5\n\t" /* xmm5 holds 8:a0+a1 */
                "pxor %%xmm1, %%xmm4\n\t"  /* xmm4 holds 8:b0+b1 */
                "movdqa %%xmm10, %%xmm3\n\t"
                "pclmulqdq $0, %%xmm1, %%xmm3\n\t"   /* xmm3 holds 8:a0*b0 */
                "pclmulqdq $17, %%xmm10, %%xmm1\n\t" /* xmm1 holds 8:a1*b1 */
                "pclmulqdq $0, %%xmm5, %%xmm4\n\t"   /* xmm4 holds 8:(a0+a1)*(b0+b1) */

                "pshufd $78, %%xmm9, %%xmm11\n\t"
                "pshufd $78, %%xmm2, %%xmm7\n\t"
                "pxor %%xmm9, %%xmm11\n\t" /* xmm11 holds 7:a0+a1 */
                "pxor %%xmm2, %%xmm7\n\t"  /* xmm7 holds 7:b0+b1 */
                "movdqa %%xmm9, %%xmm6\n\t"
                "pclmulqdq $0, %%xmm2, %%xmm6\n\t"  /* xmm6 holds 7:a0*b0 */
                "pclmulqdq $17, %%xmm9, %%xmm2\n\t" /* xmm2 holds 7:a1*b1 */
                "pclmulqdq $0, %%xmm11, %%xmm7\n\t" /* xmm7 holds 7:(a0+a1)*(b0+b1) */

                "pxor %%xmm6, %%xmm3\n\t" /* xmm3 holds 7+8:a0*b0 */
                "pxor %%xmm2, %%xmm1\n\t" /* xmm1 holds 7+8:a1*b1 */
                "pxor %%xmm7, %%xmm4\n\t" /* xmm4 holds 7+8:(a0+a1)*(b0+b1) */

                "movdqu 2*16(%[buf]), %%xmm5\n\t"
                "movdqu 3*16(%[buf]), %%xmm2\n\t"
                "pshufb %%xmm15, %%xmm5\n\t" /* be => le */
                "pshufb %%xmm15, %%xmm2\n\t" /* be => le */

                "pshufd $78, %%xmm8, %%xmm11\n\t"
                "pshufd $78, %%xmm5, %%xmm7\n\t"
                "pxor %%xmm8, %%xmm11\n\t" /* xmm11 holds 6:a0+a1 */
                "pxor %%xmm5, %%xmm7\n\t"  /* xmm7 holds 6:b0+b1 */
                "movdqa %%xmm8, %%xmm6\n\t"
                "pclmulqdq $0, %%xmm5, %%xmm6\n\t"  /* xmm6 holds 6:a0*b0 */
                "pclmulqdq $17, %%xmm8, %%xmm5\n\t" /* xmm5 holds 6:a1*b1 */
                "pclmulqdq $0, %%xmm11, %%xmm7\n\t" /* xmm7 holds 6:(a0+a1)*(b0+b1) */

                /* Load H3, H4, H5. */
                "movdqu 3*16(%[h_table]), %%xmm10\n\t"
                "movdqu 2*16(%[h_table]), %%xmm9\n\t"
                "movdqu 1*16(%[h_table]), %%xmm8\n\t"

                "pxor %%xmm6, %%xmm3\n\t" /* xmm3 holds 6+7+8:a0*b0 */
                "pxor %%xmm5, %%xmm1\n\t" /* xmm1 holds 6+7+8:a1*b1 */
                "pxor %%xmm7, %%xmm4\n\t" /* xmm4 holds 6+7+8:(a0+a1)*(b0+b1) */

                "pshufd $78, %%xmm10, %%xmm11\n\t"
                "pshufd $78, %%xmm2, %%xmm7\n\t"
                "pxor %%xmm10, %%xmm11\n\t" /* xmm11 holds 5:a0+a1 */
                "pxor %%xmm2, %%xmm7\n\t"   /* xmm7 holds 5:b0+b1 */
                "movdqa %%xmm10, %%xmm6\n\t"
                "pclmulqdq $0, %%xmm2, %%xmm6\n\t"   /* xmm6 holds 5:a0*b0 */
                "pclmulqdq $17, %%xmm10, %%xmm2\n\t" /* xmm2 holds 5:a1*b1 */
                "pclmulqdq $0, %%xmm11, %%xmm7\n\t"  /* xmm7 holds 5:(a0+a1)*(b0+b1) */

                "pxor %%xmm6, %%xmm3\n\t" /* xmm3 holds 5+6+7+8:a0*b0 */
                "pxor %%xmm2, %%xmm1\n\t" /* xmm1 holds 5+6+7+8:a1*b1 */
                "pxor %%xmm7, %%xmm4\n\t" /* xmm4 holds 5+6+7+8:(a0+a1)*(b0+b1) */

                "movdqu 4*16(%[buf]), %%xmm5\n\t"
                "movdqu 5*16(%[buf]), %%xmm2\n\t"
                "pshufb %%xmm15, %%xmm5\n\t" /* be => le */
                "pshufb %%xmm15, %%xmm2\n\t" /* be => le */

                "pshufd $78, %%xmm9, %%xmm11\n\t"
                "pshufd $78, %%xmm5, %%xmm7\n\t"
                "pxor %%xmm9, %%xmm11\n\t" /* xmm11 holds 4:a0+a1 */
                "pxor %%xmm5, %%xmm7\n\t"  /* xmm7 holds 4:b0+b1 */
                "movdqa %%xmm9, %%xmm6\n\t"
                "pclmulqdq $0, %%xmm5, %%xmm6\n\t"  /* xmm6 holds 4:a0*b0 */
                "pclmulqdq $17, %%xmm9, %%xmm5\n\t" /* xmm5 holds 4:a1*b1 */
                "pclmulqdq $0, %%xmm11, %%xmm7\n\t" /* xmm7 holds 4:(a0+a1)*(b0+b1) */

                "pxor %%xmm6, %%xmm3\n\t" /* xmm3 holds 4+5+6+7+8:a0*b0 */
                "pxor %%xmm5, %%xmm1\n\t" /* xmm1 holds 4+5+6+7+8:a1*b1 */
                "pxor %%xmm7, %%xmm4\n\t" /* xmm4 holds 4+5+6+7+8:(a0+a1)*(b0+b1) */

                "pshufd $78, %%xmm8, %%xmm11\n\t"
                "pshufd $78, %%xmm2, %%xmm7\n\t"
                "pxor %%xmm8, %%xmm11\n\t" /* xmm11 holds 3:a0+a1 */
                "pxor %%xmm2, %%xmm7\n\t"  /* xmm7 holds 3:b0+b1 */
                "movdqa %%xmm8, %%xmm6\n\t"
                "pclmulqdq $0, %%xmm2, %%xmm6\n\t"  /* xmm6 holds 3:a0*b0 */
                "pclmulqdq $17, %%xmm8, %%xmm2\n\t" /* xmm2 holds 3:a1*b1 */
                "pclmulqdq $0, %%xmm11, %%xmm7\n\t" /* xmm7 holds 3:(a0+a1)*(b0+b1) */

                "movdqu 0*16(%[h_table]), %%xmm8\n\t" /* Load H2 */

                "pxor %%xmm6, %%xmm3\n\t" /* xmm3 holds 3+4+5+6+7+8:a0*b0 */
                "pxor %%xmm2, %%xmm1\n\t" /* xmm1 holds 3+4+5+6+7+8:a1*b1 */
                "pxor %%xmm7, %%xmm4\n\t" /* xmm4 holds 3+4+5+6+7+8:(a0+a1)*(b0+b1) */

                "movdqu 6*16(%[buf]), %%xmm5\n\t"
                "movdqu 7*16(%[buf]), %%xmm2\n\t"
                "pshufb %%xmm15, %%xmm5\n\t" /* be => le */
                "pshufb %%xmm15, %%xmm2\n\t" /* be => le */

                "pshufd $78, %%xmm8, %%xmm11\n\t"
                "pshufd $78, %%xmm5, %%xmm7\n\t"
                "pxor %%xmm8, %%xmm11\n\t"  /* xmm11 holds 4:a0+a1 */
                "pxor %%xmm5, %%xmm7\n\t"   /* xmm7 holds 4:b0+b1 */
                "movdqa %%xmm8, %%xmm6\n\t"
                "pclmulqdq $0, %%xmm5, %%xmm6\n\t"   /* xmm6 holds 4:a0*b0 */
                "pclmulqdq $17, %%xmm8, %%xmm5\n\t"  /* xmm5 holds 4:a1*b1 */
                "pclmulqdq $0, %%xmm11, %%xmm7\n\t"  /* xmm7 holds 4:(a0+a1)*(b0+b1) */

                "pxor %%xmm6, %%xmm3\n\t" /* xmm3 holds 2+3+4+5+6+7+8:a0*b0 */
                "pxor %%xmm5, %%xmm1\n\t" /* xmm1 holds 2+3+4+5+6+7+8:a1*b1 */
                "pxor %%xmm7, %%xmm4\n\t" /* xmm4 holds 2+3+4+5+6+7+8:(a0+a1)*(b0+b1) */

                "pshufd $78, %%xmm0, %%xmm11\n\t"
                "pshufd $78, %%xmm2, %%xmm7\n\t"
                "pxor %%xmm0, %%xmm11\n\t" /* xmm11 holds 3:a0+a1 */
                "pxor %%xmm2, %%xmm7\n\t"  /* xmm7 holds 3:b0+b1 */
                "movdqa %%xmm0, %%xmm6\n\t"
                "pclmulqdq $0, %%xmm2, %%xmm6\n\t"  /* xmm6 holds 3:a0*b0 */
                "pclmulqdq $17, %%xmm0, %%xmm2\n\t" /* xmm2 holds 3:a1*b1 */
                "pclmulqdq $0, %%xmm11, %%xmm7\n\t" /* xmm7 holds 3:(a0+a1)*(b0+b1) */

                "pxor %%xmm6, %%xmm3\n\t" /* xmm3 holds 1+2+3+3+4+5+6+7+8:a0*b0 */
                "pxor %%xmm2, %%xmm1\n\t" /* xmm1 holds 1+2+3+3+4+5+6+7+8:a1*b1 */
                "pxor %%xmm7, %%xmm4\n\t" /* xmm4 holds 1+2+3+3+4+5+6+7+8:(a0+a1)*(b0+b1) */

                /* aggregated reduction... */
                "movdqa %%xmm3, %%xmm5\n\t"
                "pxor %%xmm1, %%xmm5\n\t" /* xmm5 holds a0*b0+a1*b1 */
                "pxor %%xmm5, %%xmm4\n\t" /* xmm4 holds a0*b0+a1*b1+(a0+a1)*(b0+b1) */
                "movdqa %%xmm4, %%xmm5\n\t"
                "psrldq $8, %%xmm4\n\t"
                "pslldq $8, %%xmm5\n\t"
                "pxor %%xmm5, %%xmm3\n\t"
                "pxor %%xmm4, %%xmm1\n\t" /* <xmm1:xmm3> holds the result of the
                                             carry-less multiplication of xmm0
                                             by xmm1 */
                :
                : [buf] "r" (buf),
                  [h_table] "r" (h_table)
                : "memory" );

  reduction();
}
#endif

static ASM_FUNC_ATTR_INLINE void gcm_lsh(void *h, unsigned int hoffs)
{
  static const u64 pconst[2] __attribute__ ((aligned (16))) =
    { U64_C(0x0000000000000001), U64_C(0xc200000000000000) };

  asm volatile ("movdqu (%[h]), %%xmm2\n\t"
                "pshufd $0xff, %%xmm2, %%xmm3\n\t"
                "movdqa %%xmm2, %%xmm4\n\t"
                "psrad $31, %%xmm3\n\t"
                "pslldq $8, %%xmm4\n\t"
                "pand %[pconst], %%xmm3\n\t"
                "paddq %%xmm2, %%xmm2\n\t"
                "psrlq $63, %%xmm4\n\t"
                "pxor %%xmm3, %%xmm2\n\t"
                "pxor %%xmm4, %%xmm2\n\t"
                "movdqu %%xmm2, (%[h])\n\t"
                :
                : [pconst] "m" (*pconst),
                  [h] "r" ((byte *)h + hoffs)
                : "memory" );
}

void ASM_FUNC_ATTR
_gcry_ghash_setup_intel_pclmul (gcry_cipher_hd_t c)
{
  static const unsigned char be_mask[16] __attribute__ ((aligned (16))) =
    { 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
#if defined(__x86_64__) && defined(__WIN64__)
  char win64tmp[10 * 16];

  /* XMM6-XMM15 need to be restored after use. */
  asm volatile ("movdqu %%xmm6,  0*16(%0)\n\t"
                "movdqu %%xmm7,  1*16(%0)\n\t"
                "movdqu %%xmm8,  2*16(%0)\n\t"
                "movdqu %%xmm9,  3*16(%0)\n\t"
                "movdqu %%xmm10, 4*16(%0)\n\t"
                "movdqu %%xmm11, 5*16(%0)\n\t"
                "movdqu %%xmm12, 6*16(%0)\n\t"
                "movdqu %%xmm13, 7*16(%0)\n\t"
                "movdqu %%xmm14, 8*16(%0)\n\t"
                "movdqu %%xmm15, 9*16(%0)\n\t"
                :
                : "r" (win64tmp)
                : "memory" );
#endif

  /* Swap endianness of hsub. */
  asm volatile ("movdqu (%[key]), %%xmm0\n\t"
                "pshufb %[be_mask], %%xmm0\n\t"
                "movdqu %%xmm0, (%[key])\n\t"
                :
                : [key] "r" (c->u_mode.gcm.u_ghash_key.key),
                  [be_mask] "m" (*be_mask)
                : "memory");

  gcm_lsh(c->u_mode.gcm.u_ghash_key.key, 0); /* H <<< 1 */

  asm volatile ("movdqa %%xmm0, %%xmm1\n\t"
                "movdqu (%[key]), %%xmm0\n\t" /* load H <<< 1 */
                :
                : [key] "r" (c->u_mode.gcm.u_ghash_key.key)
                : "memory");

  gfmul_pclmul (); /* H<<<1•H => H² */

  asm volatile ("movdqu %%xmm1, 0*16(%[h_table])\n\t"
                "movdqa %%xmm1, %%xmm7\n\t"
                :
                : [h_table] "r" (c->u_mode.gcm.gcm_table)
                : "memory");

  gcm_lsh(c->u_mode.gcm.gcm_table, 0 * 16); /* H² <<< 1 */
  gfmul_pclmul (); /* H<<<1•H² => H³ */

  asm volatile ("movdqa %%xmm7, %%xmm0\n\t"
                "movdqu %%xmm1, 1*16(%[h_table])\n\t"
                "movdqu 0*16(%[h_table]), %%xmm1\n\t" /* load H² <<< 1 */
                :
                : [h_table] "r" (c->u_mode.gcm.gcm_table)
                : "memory");

  gfmul_pclmul (); /* H²<<<1•H² => H⁴ */

  asm volatile ("movdqu %%xmm1, 2*16(%[h_table])\n\t"
                "movdqa %%xmm1, %%xmm0\n\t"
                "movdqu (%[key]), %%xmm1\n\t" /* load H <<< 1 */
                :
                : [h_table] "r" (c->u_mode.gcm.gcm_table),
                  [key] "r" (c->u_mode.gcm.u_ghash_key.key)
                : "memory");

  gcm_lsh(c->u_mode.gcm.gcm_table, 1 * 16); /* H³ <<< 1 */
  gcm_lsh(c->u_mode.gcm.gcm_table, 2 * 16); /* H⁴ <<< 1 */

#ifdef __x86_64__
  gfmul_pclmul (); /* H<<<1•H⁴ => H⁵ */

  asm volatile ("movdqu %%xmm1, 3*16(%[h_table])\n\t"
                "movdqu 0*16(%[h_table]), %%xmm1\n\t" /* load H² <<< 1 */
                :
                : [h_table] "r" (c->u_mode.gcm.gcm_table)
                : "memory");

  gfmul_pclmul (); /* H²<<<1•H⁴ => H⁶ */

  asm volatile ("movdqu %%xmm1, 4*16(%[h_table])\n\t"
                "movdqu 1*16(%[h_table]), %%xmm1\n\t" /* load H³ <<< 1 */
                :
                : [h_table] "r" (c->u_mode.gcm.gcm_table)
                : "memory");

  gfmul_pclmul (); /* H³<<<1•H⁴ => H⁷ */

  asm volatile ("movdqu %%xmm1, 5*16(%[h_table])\n\t"
                "movdqu 2*16(%[h_table]), %%xmm1\n\t" /* load H⁴ <<< 1 */
                :
                : [h_table] "r" (c->u_mode.gcm.gcm_table)
                : "memory");

  gfmul_pclmul (); /* H³<<<1•H⁴ => H⁸ */

  asm volatile ("movdqu %%xmm1, 6*16(%[h_table])\n\t"
                :
                : [h_table] "r" (c->u_mode.gcm.gcm_table)
                : "memory");

  gcm_lsh(c->u_mode.gcm.gcm_table, 3 * 16); /* H⁵ <<< 1 */
  gcm_lsh(c->u_mode.gcm.gcm_table, 4 * 16); /* H⁶ <<< 1 */
  gcm_lsh(c->u_mode.gcm.gcm_table, 5 * 16); /* H⁷ <<< 1 */
  gcm_lsh(c->u_mode.gcm.gcm_table, 6 * 16); /* H⁸ <<< 1 */

#ifdef __WIN64__
  /* Clear/restore used registers. */
  asm volatile( "pxor %%xmm0, %%xmm0\n\t"
                "pxor %%xmm1, %%xmm1\n\t"
                "pxor %%xmm2, %%xmm2\n\t"
                "pxor %%xmm3, %%xmm3\n\t"
                "pxor %%xmm4, %%xmm4\n\t"
                "pxor %%xmm5, %%xmm5\n\t"
                "movdqu 0*16(%0), %%xmm6\n\t"
                "movdqu 1*16(%0), %%xmm7\n\t"
                "movdqu 2*16(%0), %%xmm8\n\t"
                "movdqu 3*16(%0), %%xmm9\n\t"
                "movdqu 4*16(%0), %%xmm10\n\t"
                "movdqu 5*16(%0), %%xmm11\n\t"
                "movdqu 6*16(%0), %%xmm12\n\t"
                "movdqu 7*16(%0), %%xmm13\n\t"
                "movdqu 8*16(%0), %%xmm14\n\t"
                "movdqu 9*16(%0), %%xmm15\n\t"
                :
                : "r" (win64tmp)
                : "memory" );
#else
  /* Clear used registers. */
  asm volatile( "pxor %%xmm0, %%xmm0\n\t"
                "pxor %%xmm1, %%xmm1\n\t"
                "pxor %%xmm2, %%xmm2\n\t"
                "pxor %%xmm3, %%xmm3\n\t"
                "pxor %%xmm4, %%xmm4\n\t"
                "pxor %%xmm5, %%xmm5\n\t"
                "pxor %%xmm6, %%xmm6\n\t"
                "pxor %%xmm7, %%xmm7\n\t"
                "pxor %%xmm8, %%xmm8\n\t"
                "pxor %%xmm9, %%xmm9\n\t"
                "pxor %%xmm10, %%xmm10\n\t"
                "pxor %%xmm11, %%xmm11\n\t"
                "pxor %%xmm12, %%xmm12\n\t"
                "pxor %%xmm13, %%xmm13\n\t"
                "pxor %%xmm14, %%xmm14\n\t"
                "pxor %%xmm15, %%xmm15\n\t"
                ::: "memory" );
#endif
#endif
}


unsigned int ASM_FUNC_ATTR
_gcry_ghash_intel_pclmul (gcry_cipher_hd_t c, byte *result, const byte *buf,
                          size_t nblocks)
{
  static const unsigned char be_mask[16] __attribute__ ((aligned (16))) =
    { 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
  const unsigned int blocksize = GCRY_GCM_BLOCK_LEN;
#if defined(__x86_64__) && defined(__WIN64__)
  char win64tmp[10 * 16];
#endif

  if (nblocks == 0)
    return 0;

#if defined(__x86_64__) && defined(__WIN64__)
  /* XMM6-XMM15 need to be restored after use. */
  asm volatile ("movdqu %%xmm6,  0*16(%0)\n\t"
                "movdqu %%xmm7,  1*16(%0)\n\t"
                "movdqu %%xmm8,  2*16(%0)\n\t"
                "movdqu %%xmm9,  3*16(%0)\n\t"
                "movdqu %%xmm10, 4*16(%0)\n\t"
                "movdqu %%xmm11, 5*16(%0)\n\t"
                "movdqu %%xmm12, 6*16(%0)\n\t"
                "movdqu %%xmm13, 7*16(%0)\n\t"
                "movdqu %%xmm14, 8*16(%0)\n\t"
                "movdqu %%xmm15, 9*16(%0)\n\t"
                :
                : "r" (win64tmp)
                : "memory" );
#endif

  /* Preload hash. */
  asm volatile ("movdqa %[be_mask], %%xmm7\n\t"
                "movdqu %[hash], %%xmm1\n\t"
                "pshufb %%xmm7, %%xmm1\n\t" /* be => le */
                :
                : [hash] "m" (*result),
                  [be_mask] "m" (*be_mask)
                : "memory" );

#ifdef __x86_64__
  if (nblocks >= 8)
    {
      /* Preload H1. */
      asm volatile ("movdqa %%xmm7, %%xmm15\n\t"
                    "movdqa %[h_1], %%xmm0\n\t"
                    :
                    : [h_1] "m" (*c->u_mode.gcm.u_ghash_key.key)
                    : "memory" );

      while (nblocks >= 8)
        {
          gfmul_pclmul_aggr8 (buf, c->u_mode.gcm.gcm_table);

          buf += 8 * blocksize;
          nblocks -= 8;
        }
#ifndef __WIN64__
      /* Clear used x86-64/XMM registers. */
      asm volatile( "pxor %%xmm8, %%xmm8\n\t"
                    "pxor %%xmm9, %%xmm9\n\t"
                    "pxor %%xmm10, %%xmm10\n\t"
                    "pxor %%xmm11, %%xmm11\n\t"
                    "pxor %%xmm12, %%xmm12\n\t"
                    "pxor %%xmm13, %%xmm13\n\t"
                    "pxor %%xmm14, %%xmm14\n\t"
                    "pxor %%xmm15, %%xmm15\n\t"
                    ::: "memory" );
#endif
    }
#endif

  while (nblocks >= 4)
    {
      gfmul_pclmul_aggr4 (buf, c->u_mode.gcm.u_ghash_key.key,
                          c->u_mode.gcm.gcm_table, be_mask);

      buf += 4 * blocksize;
      nblocks -= 4;
    }

  if (nblocks)
    {
      /* Preload H1. */
      asm volatile ("movdqa %[h_1], %%xmm0\n\t"
                    :
                    : [h_1] "m" (*c->u_mode.gcm.u_ghash_key.key)
                    : "memory" );

      while (nblocks)
        {
          asm volatile ("movdqu %[buf], %%xmm2\n\t"
                        "pshufb %[be_mask], %%xmm2\n\t" /* be => le */
                        "pxor %%xmm2, %%xmm1\n\t"
                        :
                        : [buf] "m" (*buf), [be_mask] "m" (*be_mask)
                        : "memory" );

          gfmul_pclmul ();

          buf += blocksize;
          nblocks--;
        }
    }

  /* Store hash. */
  asm volatile ("pshufb %[be_mask], %%xmm1\n\t" /* be => le */
                "movdqu %%xmm1, %[hash]\n\t"
                : [hash] "=m" (*result)
                : [be_mask] "m" (*be_mask)
                : "memory" );

#if defined(__x86_64__) && defined(__WIN64__)
  /* Clear/restore used registers. */
  asm volatile( "pxor %%xmm0, %%xmm0\n\t"
                "pxor %%xmm1, %%xmm1\n\t"
                "pxor %%xmm2, %%xmm2\n\t"
                "pxor %%xmm3, %%xmm3\n\t"
                "pxor %%xmm4, %%xmm4\n\t"
                "pxor %%xmm5, %%xmm5\n\t"
                "movdqu 0*16(%0), %%xmm6\n\t"
                "movdqu 1*16(%0), %%xmm7\n\t"
                "movdqu 2*16(%0), %%xmm8\n\t"
                "movdqu 3*16(%0), %%xmm9\n\t"
                "movdqu 4*16(%0), %%xmm10\n\t"
                "movdqu 5*16(%0), %%xmm11\n\t"
                "movdqu 6*16(%0), %%xmm12\n\t"
                "movdqu 7*16(%0), %%xmm13\n\t"
                "movdqu 8*16(%0), %%xmm14\n\t"
                "movdqu 9*16(%0), %%xmm15\n\t"
                :
                : "r" (win64tmp)
                : "memory" );
#else
  /* Clear used registers. */
  asm volatile( "pxor %%xmm0, %%xmm0\n\t"
                "pxor %%xmm1, %%xmm1\n\t"
                "pxor %%xmm2, %%xmm2\n\t"
                "pxor %%xmm3, %%xmm3\n\t"
                "pxor %%xmm4, %%xmm4\n\t"
                "pxor %%xmm5, %%xmm5\n\t"
                "pxor %%xmm6, %%xmm6\n\t"
                "pxor %%xmm7, %%xmm7\n\t"
                ::: "memory" );
#endif

  return 0;
}

#if __clang__
#  pragma clang attribute pop
#endif

#endif /* GCM_USE_INTEL_PCLMUL */