summaryrefslogtreecommitdiffstats
path: root/security/nss/lib/freebl/intel-gcm-x86-masm.asm
blob: 6362ad85953dd8df6be9580e9ebeaa09c82ce51b (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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
; LICENSE:
; This submission to NSS is to be made available under the terms of the
; Mozilla Public License, v. 2.0. You can obtain one at http:
; //mozilla.org/MPL/2.0/.
;###############################################################################
; Copyright(c) 2014, Intel Corp.
; Developers and authors:
; Shay Gueron and Vlad Krasnov
; Intel Corporation, Israel Development Centre, Haifa, Israel
; Please send feedback directly to crypto.feedback.alias@intel.com


.MODEL FLAT, C
.XMM

.DATA
ALIGN 16
Lone            dq 1,0
Ltwo            dq 2,0
Lbswap_mask     db 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
Lshuff_mask     dq 0f0f0f0f0f0f0f0fh, 0f0f0f0f0f0f0f0fh
Lpoly           dq 01h, 0c200000000000000h

.CODE


GFMUL MACRO DST, SRC1, SRC2, TMP1, TMP2, TMP3, TMP4
    vpclmulqdq  TMP1, SRC2, SRC1, 0h
    vpclmulqdq  TMP4, SRC2, SRC1, 011h

    vpshufd     TMP2, SRC2, 78
    vpshufd     TMP3, SRC1, 78
    vpxor       TMP2, TMP2, SRC2
    vpxor       TMP3, TMP3, SRC1

    vpclmulqdq  TMP2, TMP2, TMP3, 0h
    vpxor       TMP2, TMP2, TMP1
    vpxor       TMP2, TMP2, TMP4

    vpslldq     TMP3, TMP2, 8
    vpsrldq     TMP2, TMP2, 8

    vpxor       TMP1, TMP1, TMP3
    vpxor       TMP4, TMP4, TMP2

    vpclmulqdq  TMP2, TMP1, [Lpoly], 010h
    vpshufd     TMP3, TMP1, 78
    vpxor       TMP1, TMP2, TMP3

    vpclmulqdq  TMP2, TMP1, [Lpoly], 010h
    vpshufd     TMP3, TMP1, 78
    vpxor       TMP1, TMP2, TMP3

    vpxor       DST, TMP1, TMP4

    ENDM

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Generates the final GCM tag
; void intel_aes_gcmTAG(unsigned char Htbl[16*16],
;                       unsigned char *Tp,
;                       unsigned int Mlen,
;                       unsigned int Alen,
;                       unsigned char* X0,
;                       unsigned char* TAG);
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

ALIGN 16
intel_aes_gcmTAG PROC

Htbl    textequ <eax>
Tp      textequ <ecx>
X0      textequ <edx>
TAG     textequ <ebx>

T       textequ <xmm0>
TMP0    textequ <xmm1>

    push    ebx

    mov     Htbl,   [esp + 2*4 + 0*4]
    mov     Tp,     [esp + 2*4 + 1*4]
    mov     X0,     [esp + 2*4 + 4*4]
    mov     TAG,    [esp + 2*4 + 5*4]

    vzeroupper
    vmovdqu T, XMMWORD PTR[Tp]

    vpxor   TMP0, TMP0, TMP0
    vpinsrd TMP0, TMP0, DWORD PTR[esp + 2*4 + 2*4], 0
    vpinsrd TMP0, TMP0, DWORD PTR[esp + 2*4 + 3*4], 2
    vpsllq  TMP0, TMP0, 3

    vpxor   T, T, TMP0
    vmovdqu TMP0, XMMWORD PTR[Htbl]
    GFMUL   T, T, TMP0, xmm2, xmm3, xmm4, xmm5

    vpshufb T, T, [Lbswap_mask]
    vpxor   T, T, [X0]
    vmovdqu XMMWORD PTR[TAG], T
    vzeroupper

    pop ebx

    ret

intel_aes_gcmTAG ENDP

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Generates the H table
; void intel_aes_gcmINIT(unsigned char Htbl[16*16], unsigned char *KS, int NR);
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

ALIGN 16
intel_aes_gcmINIT PROC

Htbl    textequ <eax>
KS      textequ <ecx>
NR      textequ <edx>

T       textequ <xmm0>
TMP0    textequ <xmm1>

    mov     Htbl,   [esp + 4*1 + 0*4]
    mov     KS,     [esp + 4*1 + 1*4]
    mov     NR,     [esp + 4*1 + 2*4]

    vzeroupper
    ; AES-ENC(0)
    vmovdqu T, XMMWORD PTR[KS]
    lea KS, [16 + KS]
    dec NR
Lenc_loop:
        vaesenc T, T, [KS]
        lea KS, [16 + KS]
        dec NR
        jnz Lenc_loop

    vaesenclast T, T, [KS]
    vpshufb T, T, [Lbswap_mask]

    ;Calculate H` = GFMUL(H, 2)
    vpsrad  xmm3, T, 31
    vpshufd xmm3, xmm3, 0ffh
    vpand   xmm5, xmm3, [Lpoly]
    vpsrld  xmm3, T, 31
    vpslld  xmm4, T, 1
    vpslldq xmm3, xmm3, 4
    vpxor   T, xmm4, xmm3
    vpxor   T, T, xmm5

    vmovdqu TMP0, T
    vmovdqu XMMWORD PTR[Htbl + 0*16], T

    vpshufd xmm2, T, 78
    vpxor   xmm2, xmm2, T
    vmovdqu XMMWORD PTR[Htbl + 8*16 + 0*16], xmm2

    i = 1
    WHILE i LT 8
        GFMUL   T, T, TMP0, xmm2, xmm3, xmm4, xmm5
        vmovdqu XMMWORD PTR[Htbl + i*16], T
        vpshufd xmm2, T, 78
        vpxor   xmm2, xmm2, T
        vmovdqu XMMWORD PTR[Htbl + 8*16 + i*16], xmm2
        i = i+1
        ENDM
    vzeroupper
    ret
intel_aes_gcmINIT ENDP


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Authenticate only
; void intel_aes_gcmAAD(unsigned char Htbl[16*16], unsigned char *AAD, unsigned int Alen, unsigned char *Tp);
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

ALIGN 16
intel_aes_gcmAAD PROC

Htbl    textequ <eax>
inp     textequ <ecx>
len     textequ <edx>
Tp      textequ <ebx>
hlp0    textequ <esi>

DATA    textequ <xmm0>
T       textequ <xmm1>
TMP0    textequ <xmm2>
TMP1    textequ <xmm3>
TMP2    textequ <xmm4>
TMP3    textequ <xmm5>
TMP4    textequ <xmm6>
Xhi     textequ <xmm7>

KARATSUBA_AAD MACRO i
    vpclmulqdq  TMP3, DATA, [Htbl + i*16], 0h
    vpxor       TMP0, TMP0, TMP3
    vpclmulqdq  TMP3, DATA, [Htbl + i*16], 011h
    vpxor       TMP1, TMP1, TMP3
    vpshufd     TMP3, DATA, 78
    vpxor       TMP3, TMP3, DATA
    vpclmulqdq  TMP3, TMP3, [Htbl + 8*16 + i*16], 0h
    vpxor       TMP2, TMP2, TMP3
ENDM

    cmp   DWORD PTR[esp + 1*3 + 2*4], 0
    jnz   LbeginAAD
    ret

LbeginAAD:
    push    ebx
    push    esi

    mov     Htbl,   [esp + 4*3 + 0*4]
    mov     inp,    [esp + 4*3 + 1*4]
    mov     len,    [esp + 4*3 + 2*4]
    mov     Tp,     [esp + 4*3 + 3*4]

    vzeroupper

    vpxor   Xhi, Xhi, Xhi

    vmovdqu T, XMMWORD PTR[Tp]
    ;we hash 8 block each iteration, if the total amount of blocks is not a multiple of 8, we hash the first n%8 blocks first
    mov hlp0, len
    and hlp0, 128-1
    jz  Lmod_loop

    and len, -128
    sub hlp0, 16

    ; Prefix block
    vmovdqu DATA, XMMWORD PTR[inp]
    vpshufb DATA, DATA, [Lbswap_mask]
    vpxor   DATA, DATA, T

    vpclmulqdq  TMP0, DATA, XMMWORD PTR[Htbl + hlp0], 0h
    vpclmulqdq  TMP1, DATA, XMMWORD PTR[Htbl + hlp0], 011h
    vpshufd     TMP3, DATA, 78
    vpxor       TMP3, TMP3, DATA
    vpclmulqdq  TMP2, TMP3, XMMWORD PTR[Htbl + 8*16 + hlp0], 0h

    lea     inp, [inp+16]
    test    hlp0, hlp0
    jnz     Lpre_loop
    jmp     Lred1

    ;hash remaining prefix bocks (up to 7 total prefix blocks)
Lpre_loop:

        sub hlp0, 16

        vmovdqu DATA, XMMWORD PTR[inp]
        vpshufb DATA, DATA, [Lbswap_mask]

        vpclmulqdq  TMP3, DATA, XMMWORD PTR[Htbl + hlp0], 0h
        vpxor       TMP0, TMP0, TMP3
        vpclmulqdq  TMP3, DATA, XMMWORD PTR[Htbl + hlp0], 011h
        vpxor       TMP1, TMP1, TMP3
        vpshufd     TMP3, DATA, 78
        vpxor       TMP3, TMP3, DATA
        vpclmulqdq  TMP3, TMP3, XMMWORD PTR[Htbl + 8*16 + hlp0], 0h
        vpxor       TMP2, TMP2, TMP3

        test    hlp0, hlp0
        lea     inp, [inp+16]
        jnz     Lpre_loop

Lred1:

    vpxor       TMP2, TMP2, TMP0
    vpxor       TMP2, TMP2, TMP1
    vpsrldq     TMP3, TMP2, 8
    vpslldq     TMP2, TMP2, 8

    vpxor       Xhi, TMP1, TMP3
    vpxor       T, TMP0, TMP2

Lmod_loop:

        sub len, 16*8
        jb  Ldone
        ; Block #0
        vmovdqu DATA, XMMWORD PTR[inp + 16*7]
        vpshufb DATA, DATA, XMMWORD PTR[Lbswap_mask]

        vpclmulqdq  TMP0, DATA, XMMWORD PTR[Htbl + 0*16], 0h
        vpclmulqdq  TMP1, DATA, XMMWORD PTR[Htbl + 0*16], 011h
        vpshufd     TMP3, DATA, 78
        vpxor       TMP3, TMP3, DATA
        vpclmulqdq  TMP2, TMP3, XMMWORD PTR[Htbl + 8*16 + 0*16], 0h

        ; Block #1
        vmovdqu DATA, XMMWORD PTR[inp + 16*6]
        vpshufb DATA, DATA, [Lbswap_mask]
        KARATSUBA_AAD 1

        ; Block #2
        vmovdqu DATA, XMMWORD PTR[inp + 16*5]
        vpshufb DATA, DATA, [Lbswap_mask]

        vpclmulqdq  TMP4, T, [Lpoly], 010h         ;reduction stage 1a
        vpalignr    T, T, T, 8

        KARATSUBA_AAD 2

        vpxor       T, T, TMP4                          ;reduction stage 1b

        ; Block #3
        vmovdqu DATA, XMMWORD PTR[inp + 16*4]
        vpshufb DATA, DATA, [Lbswap_mask]
        KARATSUBA_AAD 3
        ; Block #4
        vmovdqu DATA, XMMWORD PTR[inp + 16*3]
        vpshufb DATA, DATA, [Lbswap_mask]

        vpclmulqdq  TMP4, T, [Lpoly], 010h        ;reduction stage 2a
        vpalignr    T, T, T, 8

        KARATSUBA_AAD 4

        vpxor       T, T, TMP4                          ;reduction stage 2b
        ; Block #5
        vmovdqu DATA, XMMWORD PTR[inp + 16*2]
        vpshufb DATA, DATA, [Lbswap_mask]
        KARATSUBA_AAD 5

        vpxor   T, T, Xhi                               ;reduction finalize
        ; Block #6
        vmovdqu DATA, XMMWORD PTR[inp + 16*1]
        vpshufb DATA, DATA, [Lbswap_mask]
        KARATSUBA_AAD 6
        ; Block #7
        vmovdqu DATA, XMMWORD PTR[inp + 16*0]
        vpshufb DATA, DATA, [Lbswap_mask]
        vpxor   DATA, DATA, T
        KARATSUBA_AAD 7
        ; Aggregated 8 blocks, now karatsuba fixup
        vpxor   TMP2, TMP2, TMP0
        vpxor   TMP2, TMP2, TMP1
        vpsrldq TMP3, TMP2, 8
        vpslldq TMP2, TMP2, 8

        vpxor   Xhi, TMP1, TMP3
        vpxor   T, TMP0, TMP2

        lea inp, [inp + 16*8]
        jmp Lmod_loop

Ldone:
    vpclmulqdq  TMP4, T, [Lpoly], 010h
    vpalignr    T, T, T, 8
    vpxor       T, T, TMP4

    vpclmulqdq  TMP4, T, [Lpoly], 010h
    vpalignr    T, T, T, 8
    vpxor       T, T, TMP4

    vpxor       T, T, Xhi
    vmovdqu     XMMWORD PTR[Tp], T
    vzeroupper

    pop esi
    pop ebx
    ret

intel_aes_gcmAAD ENDP


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Encrypt and Authenticate
; void intel_aes_gcmENC(unsigned char* PT, unsigned char* CT, void *Gctx, unsigned int len);
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

ALIGN 16
intel_aes_gcmENC PROC

PT      textequ <eax>
CT      textequ <ecx>
Htbl    textequ <edx>
Gctx    textequ <edx>
len     textequ <DWORD PTR[ebp + 5*4 + 3*4]>
KS      textequ <esi>
NR      textequ <DWORD PTR[-40 + KS]>

aluCTR  textequ <ebx>
aluTMP  textequ <edi>

T       textequ <XMMWORD PTR[16*16 + 1*16 + Gctx]>
TMP0    textequ <xmm1>
TMP1    textequ <xmm2>
TMP2    textequ <xmm3>
TMP3    textequ <xmm4>
TMP4    textequ <xmm5>
TMP5    textequ <xmm6>

CTR0    textequ <xmm0>
CTR1    textequ <xmm1>
CTR2    textequ <xmm2>
CTR3    textequ <xmm3>
CTR4    textequ <xmm4>
CTR5    textequ <xmm5>
CTR6    textequ <xmm6>

ROUND MACRO i
    vmovdqu xmm7, XMMWORD PTR[i*16 + KS]
    vaesenc CTR0, CTR0, xmm7
    vaesenc CTR1, CTR1, xmm7
    vaesenc CTR2, CTR2, xmm7
    vaesenc CTR3, CTR3, xmm7
    vaesenc CTR4, CTR4, xmm7
    vaesenc CTR5, CTR5, xmm7
    vaesenc CTR6, CTR6, xmm7
ENDM

KARATSUBA MACRO i
    vpshufd TMP4, TMP5, 78
    vpxor   TMP4, TMP4, TMP5
    vpclmulqdq  TMP3, TMP4, XMMWORD PTR[i*16 + 8*16 + Htbl], 000h
    vpxor       TMP0, TMP0, TMP3
    vmovdqu     TMP4, XMMWORD PTR[i*16 + Htbl]
    vpclmulqdq  TMP3, TMP5, TMP4, 011h
    vpxor       TMP1, TMP1, TMP3
    vpclmulqdq  TMP3, TMP5, TMP4, 000h
    vpxor       TMP2, TMP2, TMP3
ENDM

NEXTCTR MACRO i
    add     aluCTR, 1
    mov     aluTMP, aluCTR
    bswap   aluTMP
    xor     aluTMP, [3*4 + KS]
    mov     [3*4 + 8*16 + i*16 + esp], aluTMP
ENDM

    cmp DWORD PTR[1*4 + 3*4 + esp], 0
    jne LbeginENC
    ret

LbeginENC:

    vzeroupper
    push    ebp
    push    ebx
    push    esi
    push    edi

    mov ebp, esp
    sub esp, 16*16
    and esp, -16

    mov PT, [ebp + 5*4 + 0*4]
    mov CT, [ebp + 5*4 + 1*4]
    mov Gctx, [ebp + 5*4 + 2*4]

    mov     KS, [16*16 + 3*16 + Gctx]
    lea     KS, [44 + KS]

    mov     aluCTR, [16*16 + 2*16 + 3*4 + Gctx]
    bswap   aluCTR


    vmovdqu TMP0, XMMWORD PTR[0*16 + KS]
    vpxor   TMP0, TMP0, XMMWORD PTR[16*16 + 2*16 + Gctx]
    vmovdqu XMMWORD PTR[8*16 + 0*16 + esp], TMP0

    cmp len, 16*7
    jb  LEncDataSingles
; Prepare the "top" counters
    vmovdqu XMMWORD PTR[8*16 + 1*16 + esp], TMP0
    vmovdqu XMMWORD PTR[8*16 + 2*16 + esp], TMP0
    vmovdqu XMMWORD PTR[8*16 + 3*16 + esp], TMP0
    vmovdqu XMMWORD PTR[8*16 + 4*16 + esp], TMP0
    vmovdqu XMMWORD PTR[8*16 + 5*16 + esp], TMP0
    vmovdqu XMMWORD PTR[8*16 + 6*16 + esp], TMP0

    vmovdqu CTR0, XMMWORD PTR[16*16 + 2*16 + Gctx]
    vpshufb CTR0, CTR0, XMMWORD PTR[Lbswap_mask]
; Encrypt the initial 7 blocks
    sub len, 16*7
    vpaddd  CTR1, CTR0, XMMWORD PTR[Lone]
    vpaddd  CTR2, CTR0, XMMWORD PTR[Ltwo]
    vpaddd  CTR3, CTR2, XMMWORD PTR[Lone]
    vpaddd  CTR4, CTR2, XMMWORD PTR[Ltwo]
    vpaddd  CTR5, CTR4, XMMWORD PTR[Lone]
    vpaddd  CTR6, CTR4, XMMWORD PTR[Ltwo]

    vpshufb CTR0, CTR0, XMMWORD PTR[Lbswap_mask]
    vpshufb CTR1, CTR1, XMMWORD PTR[Lbswap_mask]
    vpshufb CTR2, CTR2, XMMWORD PTR[Lbswap_mask]
    vpshufb CTR3, CTR3, XMMWORD PTR[Lbswap_mask]
    vpshufb CTR4, CTR4, XMMWORD PTR[Lbswap_mask]
    vpshufb CTR5, CTR5, XMMWORD PTR[Lbswap_mask]
    vpshufb CTR6, CTR6, XMMWORD PTR[Lbswap_mask]

    vmovdqu xmm7, XMMWORD PTR[0*16 + KS]
    vpxor   CTR0, CTR0, xmm7
    vpxor   CTR1, CTR1, xmm7
    vpxor   CTR2, CTR2, xmm7
    vpxor   CTR3, CTR3, xmm7
    vpxor   CTR4, CTR4, xmm7
    vpxor   CTR5, CTR5, xmm7
    vpxor   CTR6, CTR6, xmm7

    ROUND   1

    add aluCTR, 7
    mov aluTMP, aluCTR
    bswap   aluTMP
    xor aluTMP, [KS + 3*4]
    mov [8*16 + 0*16 + 3*4 + esp], aluTMP

    ROUND   2
    NEXTCTR 1
    ROUND   3
    NEXTCTR 2
    ROUND   4
    NEXTCTR 3
    ROUND   5
    NEXTCTR 4
    ROUND   6
    NEXTCTR 5
    ROUND   7
    NEXTCTR 6
    ROUND   8
    ROUND   9
    vmovdqu xmm7, XMMWORD PTR[10*16 + KS]
    cmp     NR, 10
    je      @f

    ROUND   10
    ROUND   11
    vmovdqu xmm7, XMMWORD PTR[12*16 + KS]
    cmp     NR, 12
    je      @f

    ROUND   12
    ROUND   13
    vmovdqu xmm7, XMMWORD PTR[14*16 + KS]
@@:
    vaesenclast CTR0, CTR0, xmm7
    vaesenclast CTR1, CTR1, xmm7
    vaesenclast CTR2, CTR2, xmm7
    vaesenclast CTR3, CTR3, xmm7
    vaesenclast CTR4, CTR4, xmm7
    vaesenclast CTR5, CTR5, xmm7
    vaesenclast CTR6, CTR6, xmm7

    vpxor   CTR0, CTR0, XMMWORD PTR[0*16 + PT]
    vpxor   CTR1, CTR1, XMMWORD PTR[1*16 + PT]
    vpxor   CTR2, CTR2, XMMWORD PTR[2*16 + PT]
    vpxor   CTR3, CTR3, XMMWORD PTR[3*16 + PT]
    vpxor   CTR4, CTR4, XMMWORD PTR[4*16 + PT]
    vpxor   CTR5, CTR5, XMMWORD PTR[5*16 + PT]
    vpxor   CTR6, CTR6, XMMWORD PTR[6*16 + PT]

    vmovdqu XMMWORD PTR[0*16 + CT], CTR0
    vmovdqu XMMWORD PTR[1*16 + CT], CTR1
    vmovdqu XMMWORD PTR[2*16 + CT], CTR2
    vmovdqu XMMWORD PTR[3*16 + CT], CTR3
    vmovdqu XMMWORD PTR[4*16 + CT], CTR4
    vmovdqu XMMWORD PTR[5*16 + CT], CTR5
    vmovdqu XMMWORD PTR[6*16 + CT], CTR6

    vpshufb CTR0, CTR0, XMMWORD PTR[Lbswap_mask]
    vpshufb CTR1, CTR1, XMMWORD PTR[Lbswap_mask]
    vpshufb CTR2, CTR2, XMMWORD PTR[Lbswap_mask]
    vpshufb CTR3, CTR3, XMMWORD PTR[Lbswap_mask]
    vpshufb CTR4, CTR4, XMMWORD PTR[Lbswap_mask]
    vpshufb CTR5, CTR5, XMMWORD PTR[Lbswap_mask]
    vpshufb TMP5, CTR6, XMMWORD PTR[Lbswap_mask]

    vmovdqa XMMWORD PTR[1*16 + esp], CTR5
    vmovdqa XMMWORD PTR[2*16 + esp], CTR4
    vmovdqa XMMWORD PTR[3*16 + esp], CTR3
    vmovdqa XMMWORD PTR[4*16 + esp], CTR2
    vmovdqa XMMWORD PTR[5*16 + esp], CTR1
    vmovdqa XMMWORD PTR[6*16 + esp], CTR0

    lea CT, [7*16 + CT]
    lea PT, [7*16 + PT]
    jmp LEncData7

LEncData7:
        cmp len, 16*7
        jb  LEndEnc7
        sub len, 16*7

        vpshufd TMP4, TMP5, 78
        vpxor   TMP4, TMP4, TMP5
        vpclmulqdq  TMP0, TMP4, XMMWORD PTR[0*16 + 8*16 + Htbl], 000h
        vmovdqu     TMP4, XMMWORD PTR[0*16 + Htbl]
        vpclmulqdq  TMP1, TMP5, TMP4, 011h
        vpclmulqdq  TMP2, TMP5, TMP4, 000h

        vmovdqu TMP5, XMMWORD PTR[1*16 + esp]
        KARATSUBA 1
        vmovdqu TMP5, XMMWORD PTR[2*16 + esp]
        KARATSUBA 2
        vmovdqu TMP5, XMMWORD PTR[3*16 + esp]
        KARATSUBA 3
        vmovdqu TMP5, XMMWORD PTR[4*16 + esp]
        KARATSUBA 4
        vmovdqu TMP5, XMMWORD PTR[5*16 + esp]
        KARATSUBA 5
        vmovdqu TMP5, XMMWORD PTR[6*16 + esp]
        vpxor   TMP5, TMP5, T
        KARATSUBA 6

        vpxor   TMP0, TMP0, TMP1
        vpxor   TMP0, TMP0, TMP2
        vpsrldq TMP3, TMP0, 8
        vpxor   TMP4, TMP1, TMP3
        vpslldq TMP3, TMP0, 8
        vpxor   TMP5, TMP2, TMP3

        vpclmulqdq  TMP1, TMP5, XMMWORD PTR[Lpoly], 010h
        vpalignr    TMP5,TMP5,TMP5,8
        vpxor       TMP5, TMP5, TMP1

        vpclmulqdq  TMP1, TMP5, XMMWORD PTR[Lpoly], 010h
        vpalignr    TMP5,TMP5,TMP5,8
        vpxor       TMP5, TMP5, TMP1

        vpxor       TMP5, TMP5, TMP4
        vmovdqu     T, TMP5

        vmovdqa CTR0, XMMWORD PTR[8*16 + 0*16 + esp]
        vmovdqa CTR1, XMMWORD PTR[8*16 + 1*16 + esp]
        vmovdqa CTR2, XMMWORD PTR[8*16 + 2*16 + esp]
        vmovdqa CTR3, XMMWORD PTR[8*16 + 3*16 + esp]
        vmovdqa CTR4, XMMWORD PTR[8*16 + 4*16 + esp]
        vmovdqa CTR5, XMMWORD PTR[8*16 + 5*16 + esp]
        vmovdqa CTR6, XMMWORD PTR[8*16 + 6*16 + esp]

        ROUND 1
        NEXTCTR 0
        ROUND 2
        NEXTCTR 1
        ROUND 3
        NEXTCTR 2
        ROUND 4
        NEXTCTR 3
        ROUND 5
        NEXTCTR 4
        ROUND 6
        NEXTCTR 5
        ROUND 7
        NEXTCTR 6

        ROUND 8
        ROUND 9

        vmovdqu     xmm7, XMMWORD PTR[10*16 + KS]
        cmp         NR, 10
        je          @f

        ROUND 10
        ROUND 11
        vmovdqu     xmm7, XMMWORD PTR[12*16 + KS]
        cmp         NR, 12
        je          @f

        ROUND 12
        ROUND 13
        vmovdqu     xmm7, XMMWORD PTR[14*16 + KS]
@@:
        vaesenclast CTR0, CTR0, xmm7
        vaesenclast CTR1, CTR1, xmm7
        vaesenclast CTR2, CTR2, xmm7
        vaesenclast CTR3, CTR3, xmm7
        vaesenclast CTR4, CTR4, xmm7
        vaesenclast CTR5, CTR5, xmm7
        vaesenclast CTR6, CTR6, xmm7

        vpxor   CTR0, CTR0, XMMWORD PTR[0*16 + PT]
        vpxor   CTR1, CTR1, XMMWORD PTR[1*16 + PT]
        vpxor   CTR2, CTR2, XMMWORD PTR[2*16 + PT]
        vpxor   CTR3, CTR3, XMMWORD PTR[3*16 + PT]
        vpxor   CTR4, CTR4, XMMWORD PTR[4*16 + PT]
        vpxor   CTR5, CTR5, XMMWORD PTR[5*16 + PT]
        vpxor   CTR6, CTR6, XMMWORD PTR[6*16 + PT]

        vmovdqu XMMWORD PTR[0*16 + CT], CTR0
        vmovdqu XMMWORD PTR[1*16 + CT], CTR1
        vmovdqu XMMWORD PTR[2*16 + CT], CTR2
        vmovdqu XMMWORD PTR[3*16 + CT], CTR3
        vmovdqu XMMWORD PTR[4*16 + CT], CTR4
        vmovdqu XMMWORD PTR[5*16 + CT], CTR5
        vmovdqu XMMWORD PTR[6*16 + CT], CTR6

        vpshufb CTR0, CTR0, XMMWORD PTR[Lbswap_mask]
        vpshufb CTR1, CTR1, XMMWORD PTR[Lbswap_mask]
        vpshufb CTR2, CTR2, XMMWORD PTR[Lbswap_mask]
        vpshufb CTR3, CTR3, XMMWORD PTR[Lbswap_mask]
        vpshufb CTR4, CTR4, XMMWORD PTR[Lbswap_mask]
        vpshufb CTR5, CTR5, XMMWORD PTR[Lbswap_mask]
        vpshufb TMP5, CTR6, XMMWORD PTR[Lbswap_mask]

        vmovdqa XMMWORD PTR[1*16 + esp], CTR5
        vmovdqa XMMWORD PTR[2*16 + esp], CTR4
        vmovdqa XMMWORD PTR[3*16 + esp], CTR3
        vmovdqa XMMWORD PTR[4*16 + esp], CTR2
        vmovdqa XMMWORD PTR[5*16 + esp], CTR1
        vmovdqa XMMWORD PTR[6*16 + esp], CTR0

        lea CT, [7*16 + CT]
        lea PT, [7*16 + PT]
        jmp LEncData7

LEndEnc7:

    vpshufd TMP4, TMP5, 78
    vpxor   TMP4, TMP4, TMP5
    vpclmulqdq  TMP0, TMP4, XMMWORD PTR[0*16 + 8*16 + Htbl], 000h
    vmovdqu     TMP4, XMMWORD PTR[0*16 + Htbl]
    vpclmulqdq  TMP1, TMP5, TMP4, 011h
    vpclmulqdq  TMP2, TMP5, TMP4, 000h

    vmovdqu TMP5, XMMWORD PTR[1*16 + esp]
    KARATSUBA 1
    vmovdqu TMP5, XMMWORD PTR[2*16 + esp]
    KARATSUBA 2
    vmovdqu TMP5, XMMWORD PTR[3*16 + esp]
    KARATSUBA 3
    vmovdqu TMP5, XMMWORD PTR[4*16 + esp]
    KARATSUBA 4
    vmovdqu TMP5, XMMWORD PTR[5*16 + esp]
    KARATSUBA 5
    vmovdqu TMP5, XMMWORD PTR[6*16 + esp]
    vpxor   TMP5, TMP5, T
    KARATSUBA 6

    vpxor   TMP0, TMP0, TMP1
    vpxor   TMP0, TMP0, TMP2
    vpsrldq TMP3, TMP0, 8
    vpxor   TMP4, TMP1, TMP3
    vpslldq TMP3, TMP0, 8
    vpxor   TMP5, TMP2, TMP3

    vpclmulqdq  TMP1, TMP5, XMMWORD PTR[Lpoly], 010h
    vpalignr    TMP5,TMP5,TMP5,8
    vpxor       TMP5, TMP5, TMP1

    vpclmulqdq  TMP1, TMP5, XMMWORD PTR[Lpoly], 010h
    vpalignr    TMP5,TMP5,TMP5,8
    vpxor       TMP5, TMP5, TMP1

    vpxor       TMP5, TMP5, TMP4
    vmovdqu     T, TMP5

    sub aluCTR, 6

LEncDataSingles:

        cmp len, 16
        jb  LEncDataTail
        sub len, 16

        vmovdqa TMP1, XMMWORD PTR[8*16 + 0*16 + esp]
        NEXTCTR 0

        vaesenc TMP1, TMP1, XMMWORD PTR[1*16 + KS]
        vaesenc TMP1, TMP1, XMMWORD PTR[2*16 + KS]
        vaesenc TMP1, TMP1, XMMWORD PTR[3*16 + KS]
        vaesenc TMP1, TMP1, XMMWORD PTR[4*16 + KS]
        vaesenc TMP1, TMP1, XMMWORD PTR[5*16 + KS]
        vaesenc TMP1, TMP1, XMMWORD PTR[6*16 + KS]
        vaesenc TMP1, TMP1, XMMWORD PTR[7*16 + KS]
        vaesenc TMP1, TMP1, XMMWORD PTR[8*16 + KS]
        vaesenc TMP1, TMP1, XMMWORD PTR[9*16 + KS]
        vmovdqu TMP2, XMMWORD PTR[10*16 + KS]
        cmp NR, 10
        je  @f
        vaesenc TMP1, TMP1, XMMWORD PTR[10*16 + KS]
        vaesenc TMP1, TMP1, XMMWORD PTR[11*16 + KS]
        vmovdqu TMP2, XMMWORD PTR[12*16 + KS]
        cmp NR, 12
        je  @f
        vaesenc TMP1, TMP1, XMMWORD PTR[12*16 + KS]
        vaesenc TMP1, TMP1, XMMWORD PTR[13*16 + KS]
        vmovdqu TMP2, XMMWORD PTR[14*16 + KS]
@@:
        vaesenclast TMP1, TMP1, TMP2
        vpxor   TMP1, TMP1, XMMWORD PTR[PT]
        vmovdqu XMMWORD PTR[CT], TMP1

        lea PT, [16+PT]
        lea CT, [16+CT]

        vpshufb TMP1, TMP1, XMMWORD PTR[Lbswap_mask]
        vpxor   TMP1, TMP1, T

        vmovdqu TMP0, XMMWORD PTR[Htbl]
        GFMUL   TMP1, TMP1, TMP0, TMP5, TMP2, TMP3, TMP4
        vmovdqu T, TMP1

        jmp LEncDataSingles

LEncDataTail:

    cmp len, 0
    je  LEncDataEnd

    vmovdqa TMP1, XMMWORD PTR[8*16 + 0*16 + esp]

    vaesenc TMP1, TMP1, XMMWORD PTR[1*16 + KS]
    vaesenc TMP1, TMP1, XMMWORD PTR[2*16 + KS]
    vaesenc TMP1, TMP1, XMMWORD PTR[3*16 + KS]
    vaesenc TMP1, TMP1, XMMWORD PTR[4*16 + KS]
    vaesenc TMP1, TMP1, XMMWORD PTR[5*16 + KS]
    vaesenc TMP1, TMP1, XMMWORD PTR[6*16 + KS]
    vaesenc TMP1, TMP1, XMMWORD PTR[7*16 + KS]
    vaesenc TMP1, TMP1, XMMWORD PTR[8*16 + KS]
    vaesenc TMP1, TMP1, XMMWORD PTR[9*16 + KS]
    vmovdqu TMP2, XMMWORD PTR[10*16 + KS]
    cmp NR, 10
    je  @f
    vaesenc TMP1, TMP1, XMMWORD PTR[10*16 + KS]
    vaesenc TMP1, TMP1, XMMWORD PTR[11*16 + KS]
    vmovdqu TMP2, XMMWORD PTR[12*16 + KS]
    cmp NR, 12
    je  @f
    vaesenc TMP1, TMP1, XMMWORD PTR[12*16 + KS]
    vaesenc TMP1, TMP1, XMMWORD PTR[13*16 + KS]
    vmovdqu TMP2, XMMWORD PTR[14*16 + KS]
@@:
    vaesenclast TMP1, TMP1, TMP2
; zero a temp location
    vpxor   TMP2, TMP2, TMP2
    vmovdqa XMMWORD PTR[esp], TMP2
; copy as many bytes as needed
    xor KS, KS
    mov aluTMP, edx
@@:
        cmp len, KS
        je  @f
        mov dl, BYTE PTR[PT + KS]
        mov BYTE PTR[esp + KS], dl
        inc KS
        jmp @b
@@:
    vpxor   TMP1, TMP1, XMMWORD PTR[esp]
    vmovdqa XMMWORD PTR[esp], TMP1
    xor KS, KS
@@:
        cmp len, KS
        je  @f
        mov dl, BYTE PTR[esp + KS]
        mov BYTE PTR[CT + KS], dl
        inc KS
        jmp @b
@@:
        cmp KS, 16
        je  @f
        mov BYTE PTR[esp + KS], 0
        inc KS
        jmp @b
@@:
    mov edx, aluTMP
    vmovdqa TMP1, XMMWORD PTR[esp]
    vpshufb TMP1, TMP1, XMMWORD PTR[Lbswap_mask]
    vpxor   TMP1, TMP1, T

    vmovdqu TMP0, XMMWORD PTR[Htbl]
    GFMUL   TMP1, TMP1, TMP0, TMP5, TMP2, TMP3, TMP4
    vmovdqu T, TMP1

LEncDataEnd:
    inc     aluCTR
    bswap   aluCTR
    mov     [16*16 + 2*16 + 3*4 + Gctx], aluCTR

    mov esp, ebp
    pop edi
    pop esi
    pop ebx
    pop ebp


    vzeroupper

    ret
intel_aes_gcmENC ENDP

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Decrypt and Authenticate
; void intel_aes_gcmDEC(uint8_t* PT, uint8_t* CT, void *Gctx, unsigned int len);
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


NEXTCTR MACRO i
    add     aluCTR, 1
    mov     aluTMP, aluCTR
    bswap   aluTMP
    xor     aluTMP, [3*4 + KS]
    mov     [3*4 + i*16 + esp], aluTMP
ENDM

intel_aes_gcmDEC PROC

    cmp DWORD PTR[1*4 + 3*4 + esp], 0
    jne LbeginDEC
    ret

LbeginDEC:

    vzeroupper
    push    ebp
    push    ebx
    push    esi
    push    edi

    mov ebp, esp
    sub esp, 8*16
    and esp, -16

    mov CT, [ebp + 5*4 + 0*4]
    mov PT, [ebp + 5*4 + 1*4]
    mov Gctx, [ebp + 5*4 + 2*4]

    mov     KS, [16*16 + 3*16 + Gctx]
    lea     KS, [44 + KS]

    mov     aluCTR, [16*16 + 2*16 + 3*4 + Gctx]
    bswap   aluCTR


    vmovdqu TMP0, XMMWORD PTR[0*16 + KS]
    vpxor   TMP0, TMP0, XMMWORD PTR[16*16 + 2*16 + Gctx]
    vmovdqu XMMWORD PTR[0*16 + esp], TMP0

    cmp len, 16*7
    jb  LDecDataSingles
    vmovdqu XMMWORD PTR[1*16 + esp], TMP0
    vmovdqu XMMWORD PTR[2*16 + esp], TMP0
    vmovdqu XMMWORD PTR[3*16 + esp], TMP0
    vmovdqu XMMWORD PTR[4*16 + esp], TMP0
    vmovdqu XMMWORD PTR[5*16 + esp], TMP0
    vmovdqu XMMWORD PTR[6*16 + esp], TMP0
    dec aluCTR

LDecData7:
    cmp len, 16*7
    jb  LDecData7End
    sub len, 16*7

    vmovdqu TMP5, XMMWORD PTR[0*16 + CT]
    vpshufb TMP5, TMP5, XMMWORD PTR[Lbswap_mask]
    vpxor   TMP5, TMP5, T
    vpshufd TMP4, TMP5, 78
    vpxor   TMP4, TMP4, TMP5
    vpclmulqdq  TMP0, TMP4, XMMWORD PTR[6*16 + 8*16 + Htbl], 000h
    vmovdqu     TMP4, XMMWORD PTR[6*16 + Htbl]
    vpclmulqdq  TMP1, TMP5, TMP4, 011h
    vpclmulqdq  TMP2, TMP5, TMP4, 000h

    NEXTCTR 0
    vmovdqu TMP5, XMMWORD PTR[1*16 + CT]
    vpshufb TMP5, TMP5, XMMWORD PTR[Lbswap_mask]
    KARATSUBA 5
    NEXTCTR 1
    vmovdqu TMP5, XMMWORD PTR[2*16 + CT]
    vpshufb TMP5, TMP5, XMMWORD PTR[Lbswap_mask]
    KARATSUBA 4
    NEXTCTR 2
    vmovdqu TMP5, XMMWORD PTR[3*16 + CT]
    vpshufb TMP5, TMP5, XMMWORD PTR[Lbswap_mask]
    KARATSUBA 3
    NEXTCTR 3
    vmovdqu TMP5, XMMWORD PTR[4*16 + CT]
    vpshufb TMP5, TMP5, XMMWORD PTR[Lbswap_mask]
    KARATSUBA 2
    NEXTCTR 4
    vmovdqu TMP5, XMMWORD PTR[5*16 + CT]
    vpshufb TMP5, TMP5, XMMWORD PTR[Lbswap_mask]
    KARATSUBA 1
    NEXTCTR 5
    vmovdqu TMP5, XMMWORD PTR[6*16 + CT]
    vpshufb TMP5, TMP5, XMMWORD PTR[Lbswap_mask]
    KARATSUBA 0
    NEXTCTR 6

    vpxor   TMP0, TMP0, TMP1
    vpxor   TMP0, TMP0, TMP2
    vpsrldq TMP3, TMP0, 8
    vpxor   TMP4, TMP1, TMP3
    vpslldq TMP3, TMP0, 8
    vpxor   TMP5, TMP2, TMP3

    vpclmulqdq  TMP1, TMP5, XMMWORD PTR[Lpoly], 010h
    vpalignr    TMP5,TMP5,TMP5,8
    vpxor       TMP5, TMP5, TMP1

    vpclmulqdq  TMP1, TMP5, XMMWORD PTR[Lpoly], 010h
    vpalignr    TMP5,TMP5,TMP5,8
    vpxor       TMP5, TMP5, TMP1

    vpxor       TMP5, TMP5, TMP4
    vmovdqu     T, TMP5

    vmovdqa CTR0, XMMWORD PTR[0*16 + esp]
    vmovdqa CTR1, XMMWORD PTR[1*16 + esp]
    vmovdqa CTR2, XMMWORD PTR[2*16 + esp]
    vmovdqa CTR3, XMMWORD PTR[3*16 + esp]
    vmovdqa CTR4, XMMWORD PTR[4*16 + esp]
    vmovdqa CTR5, XMMWORD PTR[5*16 + esp]
    vmovdqa CTR6, XMMWORD PTR[6*16 + esp]

    ROUND   1
    ROUND   2
    ROUND   3
    ROUND   4
    ROUND   5
    ROUND   6
    ROUND   7
    ROUND   8
    ROUND   9
    vmovdqu xmm7, XMMWORD PTR[10*16 + KS]
    cmp     NR, 10
    je      @f

    ROUND   10
    ROUND   11
    vmovdqu xmm7, XMMWORD PTR[12*16 + KS]
    cmp     NR, 12
    je      @f

    ROUND   12
    ROUND   13
    vmovdqu xmm7, XMMWORD PTR[14*16 + KS]
@@:
    vaesenclast CTR0, CTR0, xmm7
    vaesenclast CTR1, CTR1, xmm7
    vaesenclast CTR2, CTR2, xmm7
    vaesenclast CTR3, CTR3, xmm7
    vaesenclast CTR4, CTR4, xmm7
    vaesenclast CTR5, CTR5, xmm7
    vaesenclast CTR6, CTR6, xmm7

    vpxor   CTR0, CTR0, XMMWORD PTR[0*16 + CT]
    vpxor   CTR1, CTR1, XMMWORD PTR[1*16 + CT]
    vpxor   CTR2, CTR2, XMMWORD PTR[2*16 + CT]
    vpxor   CTR3, CTR3, XMMWORD PTR[3*16 + CT]
    vpxor   CTR4, CTR4, XMMWORD PTR[4*16 + CT]
    vpxor   CTR5, CTR5, XMMWORD PTR[5*16 + CT]
    vpxor   CTR6, CTR6, XMMWORD PTR[6*16 + CT]

    vmovdqu XMMWORD PTR[0*16 + PT], CTR0
    vmovdqu XMMWORD PTR[1*16 + PT], CTR1
    vmovdqu XMMWORD PTR[2*16 + PT], CTR2
    vmovdqu XMMWORD PTR[3*16 + PT], CTR3
    vmovdqu XMMWORD PTR[4*16 + PT], CTR4
    vmovdqu XMMWORD PTR[5*16 + PT], CTR5
    vmovdqu XMMWORD PTR[6*16 + PT], CTR6

    lea CT, [7*16 + CT]
    lea PT, [7*16 + PT]
    jmp LDecData7

LDecData7End:

    NEXTCTR 0

LDecDataSingles:

        cmp len, 16
        jb  LDecDataTail
        sub len, 16

        vmovdqu TMP1, XMMWORD PTR[CT]
        vpshufb TMP1, TMP1, XMMWORD PTR[Lbswap_mask]
        vpxor   TMP1, TMP1, T

        vmovdqu TMP0, XMMWORD PTR[Htbl]
        GFMUL   TMP1, TMP1, TMP0, TMP5, TMP2, TMP3, TMP4
        vmovdqu T, TMP1

        vmovdqa TMP1, XMMWORD PTR[0*16 + esp]
        NEXTCTR 0

        vaesenc TMP1, TMP1, XMMWORD PTR[1*16 + KS]
        vaesenc TMP1, TMP1, XMMWORD PTR[2*16 + KS]
        vaesenc TMP1, TMP1, XMMWORD PTR[3*16 + KS]
        vaesenc TMP1, TMP1, XMMWORD PTR[4*16 + KS]
        vaesenc TMP1, TMP1, XMMWORD PTR[5*16 + KS]
        vaesenc TMP1, TMP1, XMMWORD PTR[6*16 + KS]
        vaesenc TMP1, TMP1, XMMWORD PTR[7*16 + KS]
        vaesenc TMP1, TMP1, XMMWORD PTR[8*16 + KS]
        vaesenc TMP1, TMP1, XMMWORD PTR[9*16 + KS]
        vmovdqu TMP2, XMMWORD PTR[10*16 + KS]
        cmp NR, 10
        je  @f
        vaesenc TMP1, TMP1, XMMWORD PTR[10*16 + KS]
        vaesenc TMP1, TMP1, XMMWORD PTR[11*16 + KS]
        vmovdqu TMP2, XMMWORD PTR[12*16 + KS]
        cmp NR, 12
        je  @f
        vaesenc TMP1, TMP1, XMMWORD PTR[12*16 + KS]
        vaesenc TMP1, TMP1, XMMWORD PTR[13*16 + KS]
        vmovdqu TMP2, XMMWORD PTR[14*16 + KS]
@@:
        vaesenclast TMP1, TMP1, TMP2
        vpxor   TMP1, TMP1, XMMWORD PTR[CT]
        vmovdqu XMMWORD PTR[PT], TMP1

        lea PT, [16+PT]
        lea CT, [16+CT]
        jmp LDecDataSingles

LDecDataTail:

    cmp len, 0
    je  LDecDataEnd

    vmovdqa TMP1, XMMWORD PTR[0*16 + esp]
    inc aluCTR
    vaesenc TMP1, TMP1, XMMWORD PTR[1*16 + KS]
    vaesenc TMP1, TMP1, XMMWORD PTR[2*16 + KS]
    vaesenc TMP1, TMP1, XMMWORD PTR[3*16 + KS]
    vaesenc TMP1, TMP1, XMMWORD PTR[4*16 + KS]
    vaesenc TMP1, TMP1, XMMWORD PTR[5*16 + KS]
    vaesenc TMP1, TMP1, XMMWORD PTR[6*16 + KS]
    vaesenc TMP1, TMP1, XMMWORD PTR[7*16 + KS]
    vaesenc TMP1, TMP1, XMMWORD PTR[8*16 + KS]
    vaesenc TMP1, TMP1, XMMWORD PTR[9*16 + KS]
    vmovdqu TMP2, XMMWORD PTR[10*16 + KS]
    cmp NR, 10
    je  @f
    vaesenc TMP1, TMP1, XMMWORD PTR[10*16 + KS]
    vaesenc TMP1, TMP1, XMMWORD PTR[11*16 + KS]
    vmovdqu TMP2, XMMWORD PTR[12*16 + KS]
    cmp NR, 12
    je  @f
    vaesenc TMP1, TMP1, XMMWORD PTR[12*16 + KS]
    vaesenc TMP1, TMP1, XMMWORD PTR[13*16 + KS]
    vmovdqu TMP2, XMMWORD PTR[14*16 + KS]
@@:
    vaesenclast xmm7, TMP1, TMP2

; copy as many bytes as needed
    xor KS, KS
    mov aluTMP, edx
@@:
        cmp len, KS
        je  @f
        mov dl, BYTE PTR[CT + KS]
        mov BYTE PTR[esp + KS], dl
        inc KS
        jmp @b
@@:
        cmp KS, 16
        je  @f
        mov BYTE PTR[esp + KS], 0
        inc KS
        jmp @b
@@:
    mov edx, aluTMP
    vmovdqa TMP1, XMMWORD PTR[esp]
    vpshufb TMP1, TMP1, XMMWORD PTR[Lbswap_mask]
    vpxor   TMP1, TMP1, T

    vmovdqu TMP0, XMMWORD PTR[Htbl]
    GFMUL   TMP1, TMP1, TMP0, TMP5, TMP2, TMP3, TMP4
    vmovdqu T, TMP1

    vpxor   xmm7, xmm7, XMMWORD PTR[esp]
    vmovdqa XMMWORD PTR[esp], xmm7
    xor     KS, KS
    mov aluTMP, edx
@@:
        cmp len, KS
        je  @f
        mov dl, BYTE PTR[esp + KS]
        mov BYTE PTR[PT + KS], dl
        inc KS
        jmp @b
@@:
    mov edx, aluTMP

LDecDataEnd:

    bswap   aluCTR
    mov     [16*16 + 2*16 + 3*4 + Gctx], aluCTR

    mov esp, ebp
    pop edi
    pop esi
    pop ebx
    pop ebp

    vzeroupper

    ret
intel_aes_gcmDEC ENDP


END