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
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
|
#!/usr/bin/env perl
# ====================================================================
# Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
# project. The module is, however, dual licensed under OpenSSL and
# CRYPTOGAMS licenses depending on where you obtain it. For further
# details see http://www.openssl.org/~appro/cryptogams/.
# ====================================================================
# Multi-buffer AES-NI procedures process several independent buffers
# in parallel by interleaving independent instructions.
#
# Cycles per byte for interleave factor 4:
#
# asymptotic measured
# ---------------------------
# Westmere 5.00/4=1.25 5.13/4=1.28
# Atom 15.0/4=3.75 ?15.7/4=3.93
# Sandy Bridge 5.06/4=1.27 5.18/4=1.29
# Ivy Bridge 5.06/4=1.27 5.14/4=1.29
# Haswell 4.44/4=1.11 4.44/4=1.11
# Bulldozer 5.75/4=1.44 5.76/4=1.44
#
# Cycles per byte for interleave factor 8 (not implemented for
# pre-AVX processors, where higher interleave factor incidentally
# doesn't result in improvement):
#
# asymptotic measured
# ---------------------------
# Sandy Bridge 5.06/8=0.64 7.10/8=0.89(*)
# Ivy Bridge 5.06/8=0.64 7.14/8=0.89(*)
# Haswell 5.00/8=0.63 5.00/8=0.63
# Bulldozer 5.75/8=0.72 5.77/8=0.72
#
# (*) Sandy/Ivy Bridge are known to handle high interleave factors
# suboptimally;
$flavour = shift;
$output = shift;
if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
$win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
$0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
die "can't locate x86_64-xlate.pl";
$avx=0;
if (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
=~ /GNU assembler version ([2-9]\.[0-9]+)/) {
$avx = ($1>=2.19) + ($1>=2.22);
}
if (!$avx && $win64 && ($flavour =~ /nasm/ || $ENV{ASM} =~ /nasm/) &&
`nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)/) {
$avx = ($1>=2.09) + ($1>=2.10);
}
if (!$avx && $win64 && ($flavour =~ /masm/ || $ENV{ASM} =~ /ml64/) &&
`ml64 2>&1` =~ /Version ([0-9]+)\./) {
$avx = ($1>=10) + ($1>=11);
}
if (!$avx && `$ENV{CC} -v 2>&1` =~ /(^clang version|based on LLVM) ([3-9]\.[0-9]+)/) {
$avx = ($2>=3.0) + ($2>3.0);
}
open OUT,"| \"$^X\" $xlate $flavour $output";
*STDOUT=*OUT;
# void aesni_multi_cbc_encrypt (
# struct { void *inp,*out; int blocks; double iv[2]; } inp[8];
# const AES_KEY *key,
# int num); /* 1 or 2 */
#
$inp="%rdi"; # 1st arg
$key="%rsi"; # 2nd arg
$num="%edx";
@inptr=map("%r$_",(8..11));
@outptr=map("%r$_",(12..15));
($rndkey0,$rndkey1)=("%xmm0","%xmm1");
@out=map("%xmm$_",(2..5));
@inp=map("%xmm$_",(6..9));
($counters,$mask,$zero)=map("%xmm$_",(10..12));
($rounds,$one,$sink,$offset)=("%eax","%ecx","%rbp","%rbx");
$code.=<<___;
.text
.extern OPENSSL_ia32cap_P
.globl aesni_multi_cbc_encrypt
.type aesni_multi_cbc_encrypt,\@function,3
.align 32
aesni_multi_cbc_encrypt:
___
$code.=<<___ if ($avx);
cmp \$2,$num
jb .Lenc_non_avx
mov OPENSSL_ia32cap_P+4(%rip),%ecx
test \$`1<<28`,%ecx # AVX bit
jnz _avx_cbc_enc_shortcut
jmp .Lenc_non_avx
.align 16
.Lenc_non_avx:
___
$code.=<<___;
mov %rsp,%rax
push %rbx
push %rbp
push %r12
push %r13
push %r14
push %r15
___
$code.=<<___ if ($win64);
lea -0xa8(%rsp),%rsp
movaps %xmm6,(%rsp)
movaps %xmm7,0x10(%rsp)
movaps %xmm8,0x20(%rsp)
movaps %xmm9,0x30(%rsp)
movaps %xmm10,0x40(%rsp)
movaps %xmm11,0x50(%rsp)
movaps %xmm12,0x60(%rsp)
movaps %xmm13,-0x68(%rax) # not used, saved to share se_handler
movaps %xmm14,-0x58(%rax)
movaps %xmm15,-0x48(%rax)
___
$code.=<<___;
# stack layout
#
# +0 output sink
# +16 input sink [original %rsp and $num]
# +32 counters
sub \$48,%rsp
and \$-64,%rsp
mov %rax,16(%rsp) # original %rsp
.Lenc4x_body:
movdqu ($key),$zero # 0-round key
lea 0x78($key),$key # size optimization
lea 40*2($inp),$inp
.Lenc4x_loop_grande:
mov $num,24(%rsp) # original $num
xor $num,$num
___
for($i=0;$i<4;$i++) {
$code.=<<___;
mov `40*$i+16-40*2`($inp),$one # borrow $one for number of blocks
mov `40*$i+0-40*2`($inp),@inptr[$i]
cmp $num,$one
mov `40*$i+8-40*2`($inp),@outptr[$i]
cmovg $one,$num # find maximum
test $one,$one
movdqu `40*$i+24-40*2`($inp),@out[$i] # load IV
mov $one,`32+4*$i`(%rsp) # initialize counters
cmovle %rsp,@inptr[$i] # cancel input
___
}
$code.=<<___;
test $num,$num
jz .Lenc4x_done
movups 0x10-0x78($key),$rndkey1
pxor $zero,@out[0]
movups 0x20-0x78($key),$rndkey0
pxor $zero,@out[1]
mov 0xf0-0x78($key),$rounds
pxor $zero,@out[2]
movdqu (@inptr[0]),@inp[0] # load inputs
pxor $zero,@out[3]
movdqu (@inptr[1]),@inp[1]
pxor @inp[0],@out[0]
movdqu (@inptr[2]),@inp[2]
pxor @inp[1],@out[1]
movdqu (@inptr[3]),@inp[3]
pxor @inp[2],@out[2]
pxor @inp[3],@out[3]
movdqa 32(%rsp),$counters # load counters
xor $offset,$offset
jmp .Loop_enc4x
.align 32
.Loop_enc4x:
add \$16,$offset
lea 16(%rsp),$sink # sink pointer
mov \$1,$one # constant of 1
sub $offset,$sink
aesenc $rndkey1,@out[0]
prefetcht0 31(@inptr[0],$offset) # prefetch input
prefetcht0 31(@inptr[1],$offset)
aesenc $rndkey1,@out[1]
prefetcht0 31(@inptr[2],$offset)
prefetcht0 31(@inptr[2],$offset)
aesenc $rndkey1,@out[2]
aesenc $rndkey1,@out[3]
movups 0x30-0x78($key),$rndkey1
___
for($i=0;$i<4;$i++) {
my $rndkey = ($i&1) ? $rndkey1 : $rndkey0;
$code.=<<___;
cmp `32+4*$i`(%rsp),$one
aesenc $rndkey,@out[0]
aesenc $rndkey,@out[1]
aesenc $rndkey,@out[2]
cmovge $sink,@inptr[$i] # cancel input
cmovg $sink,@outptr[$i] # sink output
aesenc $rndkey,@out[3]
movups `0x40+16*$i-0x78`($key),$rndkey
___
}
$code.=<<___;
movdqa $counters,$mask
aesenc $rndkey0,@out[0]
prefetcht0 15(@outptr[0],$offset) # prefetch output
prefetcht0 15(@outptr[1],$offset)
aesenc $rndkey0,@out[1]
prefetcht0 15(@outptr[2],$offset)
prefetcht0 15(@outptr[3],$offset)
aesenc $rndkey0,@out[2]
aesenc $rndkey0,@out[3]
movups 0x80-0x78($key),$rndkey0
pxor $zero,$zero
aesenc $rndkey1,@out[0]
pcmpgtd $zero,$mask
movdqu -0x78($key),$zero # reload 0-round key
aesenc $rndkey1,@out[1]
paddd $mask,$counters # decrement counters
movdqa $counters,32(%rsp) # update counters
aesenc $rndkey1,@out[2]
aesenc $rndkey1,@out[3]
movups 0x90-0x78($key),$rndkey1
cmp \$11,$rounds
aesenc $rndkey0,@out[0]
aesenc $rndkey0,@out[1]
aesenc $rndkey0,@out[2]
aesenc $rndkey0,@out[3]
movups 0xa0-0x78($key),$rndkey0
jb .Lenc4x_tail
aesenc $rndkey1,@out[0]
aesenc $rndkey1,@out[1]
aesenc $rndkey1,@out[2]
aesenc $rndkey1,@out[3]
movups 0xb0-0x78($key),$rndkey1
aesenc $rndkey0,@out[0]
aesenc $rndkey0,@out[1]
aesenc $rndkey0,@out[2]
aesenc $rndkey0,@out[3]
movups 0xc0-0x78($key),$rndkey0
je .Lenc4x_tail
aesenc $rndkey1,@out[0]
aesenc $rndkey1,@out[1]
aesenc $rndkey1,@out[2]
aesenc $rndkey1,@out[3]
movups 0xd0-0x78($key),$rndkey1
aesenc $rndkey0,@out[0]
aesenc $rndkey0,@out[1]
aesenc $rndkey0,@out[2]
aesenc $rndkey0,@out[3]
movups 0xe0-0x78($key),$rndkey0
jmp .Lenc4x_tail
.align 32
.Lenc4x_tail:
aesenc $rndkey1,@out[0]
aesenc $rndkey1,@out[1]
aesenc $rndkey1,@out[2]
aesenc $rndkey1,@out[3]
movdqu (@inptr[0],$offset),@inp[0]
movdqu 0x10-0x78($key),$rndkey1
aesenclast $rndkey0,@out[0]
movdqu (@inptr[1],$offset),@inp[1]
pxor $zero,@inp[0]
aesenclast $rndkey0,@out[1]
movdqu (@inptr[2],$offset),@inp[2]
pxor $zero,@inp[1]
aesenclast $rndkey0,@out[2]
movdqu (@inptr[3],$offset),@inp[3]
pxor $zero,@inp[2]
aesenclast $rndkey0,@out[3]
movdqu 0x20-0x78($key),$rndkey0
pxor $zero,@inp[3]
movups @out[0],-16(@outptr[0],$offset)
pxor @inp[0],@out[0]
movups @out[1],-16(@outptr[1],$offset)
pxor @inp[1],@out[1]
movups @out[2],-16(@outptr[2],$offset)
pxor @inp[2],@out[2]
movups @out[3],-16(@outptr[3],$offset)
pxor @inp[3],@out[3]
dec $num
jnz .Loop_enc4x
mov 16(%rsp),%rax # original %rsp
mov 24(%rsp),$num
#pxor @inp[0],@out[0]
#pxor @inp[1],@out[1]
#movdqu @out[0],`40*0+24-40*2`($inp) # output iv FIX ME!
#pxor @inp[2],@out[2]
#movdqu @out[1],`40*1+24-40*2`($inp)
#pxor @inp[3],@out[3]
#movdqu @out[2],`40*2+24-40*2`($inp) # won't fix, let caller
#movdqu @out[3],`40*3+24-40*2`($inp) # figure this out...
lea `40*4`($inp),$inp
dec $num
jnz .Lenc4x_loop_grande
.Lenc4x_done:
___
$code.=<<___ if ($win64);
movaps -0xd8(%rax),%xmm6
movaps -0xc8(%rax),%xmm7
movaps -0xb8(%rax),%xmm8
movaps -0xa8(%rax),%xmm9
movaps -0x98(%rax),%xmm10
movaps -0x88(%rax),%xmm11
movaps -0x78(%rax),%xmm12
#movaps -0x68(%rax),%xmm13
#movaps -0x58(%rax),%xmm14
#movaps -0x48(%rax),%xmm15
___
$code.=<<___;
mov -48(%rax),%r15
mov -40(%rax),%r14
mov -32(%rax),%r13
mov -24(%rax),%r12
mov -16(%rax),%rbp
mov -8(%rax),%rbx
lea (%rax),%rsp
.Lenc4x_epilogue:
ret
.size aesni_multi_cbc_encrypt,.-aesni_multi_cbc_encrypt
.globl aesni_multi_cbc_decrypt
.type aesni_multi_cbc_decrypt,\@function,3
.align 32
aesni_multi_cbc_decrypt:
___
$code.=<<___ if ($avx);
cmp \$2,$num
jb .Ldec_non_avx
mov OPENSSL_ia32cap_P+4(%rip),%ecx
test \$`1<<28`,%ecx # AVX bit
jnz _avx_cbc_dec_shortcut
jmp .Ldec_non_avx
.align 16
.Ldec_non_avx:
___
$code.=<<___;
mov %rsp,%rax
push %rbx
push %rbp
push %r12
push %r13
push %r14
push %r15
___
$code.=<<___ if ($win64);
lea -0xa8(%rsp),%rsp
movaps %xmm6,(%rsp)
movaps %xmm7,0x10(%rsp)
movaps %xmm8,0x20(%rsp)
movaps %xmm9,0x30(%rsp)
movaps %xmm10,0x40(%rsp)
movaps %xmm11,0x50(%rsp)
movaps %xmm12,0x60(%rsp)
movaps %xmm13,-0x68(%rax) # not used, saved to share se_handler
movaps %xmm14,-0x58(%rax)
movaps %xmm15,-0x48(%rax)
___
$code.=<<___;
# stack layout
#
# +0 output sink
# +16 input sink [original %rsp and $num]
# +32 counters
sub \$48,%rsp
and \$-64,%rsp
mov %rax,16(%rsp) # original %rsp
.Ldec4x_body:
movdqu ($key),$zero # 0-round key
lea 0x78($key),$key # size optimization
lea 40*2($inp),$inp
.Ldec4x_loop_grande:
mov $num,24(%rsp) # original $num
xor $num,$num
___
for($i=0;$i<4;$i++) {
$code.=<<___;
mov `40*$i+16-40*2`($inp),$one # borrow $one for number of blocks
mov `40*$i+0-40*2`($inp),@inptr[$i]
cmp $num,$one
mov `40*$i+8-40*2`($inp),@outptr[$i]
cmovg $one,$num # find maximum
test $one,$one
movdqu `40*$i+24-40*2`($inp),@inp[$i] # load IV
mov $one,`32+4*$i`(%rsp) # initialize counters
cmovle %rsp,@inptr[$i] # cancel input
___
}
$code.=<<___;
test $num,$num
jz .Ldec4x_done
movups 0x10-0x78($key),$rndkey1
movups 0x20-0x78($key),$rndkey0
mov 0xf0-0x78($key),$rounds
movdqu (@inptr[0]),@out[0] # load inputs
movdqu (@inptr[1]),@out[1]
pxor $zero,@out[0]
movdqu (@inptr[2]),@out[2]
pxor $zero,@out[1]
movdqu (@inptr[3]),@out[3]
pxor $zero,@out[2]
pxor $zero,@out[3]
movdqa 32(%rsp),$counters # load counters
xor $offset,$offset
jmp .Loop_dec4x
.align 32
.Loop_dec4x:
add \$16,$offset
lea 16(%rsp),$sink # sink pointer
mov \$1,$one # constant of 1
sub $offset,$sink
aesdec $rndkey1,@out[0]
prefetcht0 31(@inptr[0],$offset) # prefetch input
prefetcht0 31(@inptr[1],$offset)
aesdec $rndkey1,@out[1]
prefetcht0 31(@inptr[2],$offset)
prefetcht0 31(@inptr[3],$offset)
aesdec $rndkey1,@out[2]
aesdec $rndkey1,@out[3]
movups 0x30-0x78($key),$rndkey1
___
for($i=0;$i<4;$i++) {
my $rndkey = ($i&1) ? $rndkey1 : $rndkey0;
$code.=<<___;
cmp `32+4*$i`(%rsp),$one
aesdec $rndkey,@out[0]
aesdec $rndkey,@out[1]
aesdec $rndkey,@out[2]
cmovge $sink,@inptr[$i] # cancel input
cmovg $sink,@outptr[$i] # sink output
aesdec $rndkey,@out[3]
movups `0x40+16*$i-0x78`($key),$rndkey
___
}
$code.=<<___;
movdqa $counters,$mask
aesdec $rndkey0,@out[0]
prefetcht0 15(@outptr[0],$offset) # prefetch output
prefetcht0 15(@outptr[1],$offset)
aesdec $rndkey0,@out[1]
prefetcht0 15(@outptr[2],$offset)
prefetcht0 15(@outptr[3],$offset)
aesdec $rndkey0,@out[2]
aesdec $rndkey0,@out[3]
movups 0x80-0x78($key),$rndkey0
pxor $zero,$zero
aesdec $rndkey1,@out[0]
pcmpgtd $zero,$mask
movdqu -0x78($key),$zero # reload 0-round key
aesdec $rndkey1,@out[1]
paddd $mask,$counters # decrement counters
movdqa $counters,32(%rsp) # update counters
aesdec $rndkey1,@out[2]
aesdec $rndkey1,@out[3]
movups 0x90-0x78($key),$rndkey1
cmp \$11,$rounds
aesdec $rndkey0,@out[0]
aesdec $rndkey0,@out[1]
aesdec $rndkey0,@out[2]
aesdec $rndkey0,@out[3]
movups 0xa0-0x78($key),$rndkey0
jb .Ldec4x_tail
aesdec $rndkey1,@out[0]
aesdec $rndkey1,@out[1]
aesdec $rndkey1,@out[2]
aesdec $rndkey1,@out[3]
movups 0xb0-0x78($key),$rndkey1
aesdec $rndkey0,@out[0]
aesdec $rndkey0,@out[1]
aesdec $rndkey0,@out[2]
aesdec $rndkey0,@out[3]
movups 0xc0-0x78($key),$rndkey0
je .Ldec4x_tail
aesdec $rndkey1,@out[0]
aesdec $rndkey1,@out[1]
aesdec $rndkey1,@out[2]
aesdec $rndkey1,@out[3]
movups 0xd0-0x78($key),$rndkey1
aesdec $rndkey0,@out[0]
aesdec $rndkey0,@out[1]
aesdec $rndkey0,@out[2]
aesdec $rndkey0,@out[3]
movups 0xe0-0x78($key),$rndkey0
jmp .Ldec4x_tail
.align 32
.Ldec4x_tail:
aesdec $rndkey1,@out[0]
aesdec $rndkey1,@out[1]
aesdec $rndkey1,@out[2]
pxor $rndkey0,@inp[0]
pxor $rndkey0,@inp[1]
aesdec $rndkey1,@out[3]
movdqu 0x10-0x78($key),$rndkey1
pxor $rndkey0,@inp[2]
pxor $rndkey0,@inp[3]
movdqu 0x20-0x78($key),$rndkey0
aesdeclast @inp[0],@out[0]
aesdeclast @inp[1],@out[1]
movdqu -16(@inptr[0],$offset),@inp[0] # load next IV
movdqu -16(@inptr[1],$offset),@inp[1]
aesdeclast @inp[2],@out[2]
aesdeclast @inp[3],@out[3]
movdqu -16(@inptr[2],$offset),@inp[2]
movdqu -16(@inptr[3],$offset),@inp[3]
movups @out[0],-16(@outptr[0],$offset)
movdqu (@inptr[0],$offset),@out[0]
movups @out[1],-16(@outptr[1],$offset)
movdqu (@inptr[1],$offset),@out[1]
pxor $zero,@out[0]
movups @out[2],-16(@outptr[2],$offset)
movdqu (@inptr[2],$offset),@out[2]
pxor $zero,@out[1]
movups @out[3],-16(@outptr[3],$offset)
movdqu (@inptr[3],$offset),@out[3]
pxor $zero,@out[2]
pxor $zero,@out[3]
dec $num
jnz .Loop_dec4x
mov 16(%rsp),%rax # original %rsp
mov 24(%rsp),$num
lea `40*4`($inp),$inp
dec $num
jnz .Ldec4x_loop_grande
.Ldec4x_done:
___
$code.=<<___ if ($win64);
movaps -0xd8(%rax),%xmm6
movaps -0xc8(%rax),%xmm7
movaps -0xb8(%rax),%xmm8
movaps -0xa8(%rax),%xmm9
movaps -0x98(%rax),%xmm10
movaps -0x88(%rax),%xmm11
movaps -0x78(%rax),%xmm12
#movaps -0x68(%rax),%xmm13
#movaps -0x58(%rax),%xmm14
#movaps -0x48(%rax),%xmm15
___
$code.=<<___;
mov -48(%rax),%r15
mov -40(%rax),%r14
mov -32(%rax),%r13
mov -24(%rax),%r12
mov -16(%rax),%rbp
mov -8(%rax),%rbx
lea (%rax),%rsp
.Ldec4x_epilogue:
ret
.size aesni_multi_cbc_decrypt,.-aesni_multi_cbc_decrypt
___
if ($avx) {{{
my @ptr=map("%r$_",(8..15));
my $offload=$sink;
my @out=map("%xmm$_",(2..9));
my @inp=map("%xmm$_",(10..13));
my ($counters,$zero)=("%xmm14","%xmm15");
$code.=<<___;
.type aesni_multi_cbc_encrypt_avx,\@function,3
.align 32
aesni_multi_cbc_encrypt_avx:
_avx_cbc_enc_shortcut:
mov %rsp,%rax
push %rbx
push %rbp
push %r12
push %r13
push %r14
push %r15
___
$code.=<<___ if ($win64);
lea -0xa8(%rsp),%rsp
movaps %xmm6,(%rsp)
movaps %xmm7,0x10(%rsp)
movaps %xmm8,0x20(%rsp)
movaps %xmm9,0x30(%rsp)
movaps %xmm10,0x40(%rsp)
movaps %xmm11,0x50(%rsp)
movaps %xmm12,-0x78(%rax)
movaps %xmm13,-0x68(%rax)
movaps %xmm14,-0x58(%rax)
movaps %xmm15,-0x48(%rax)
___
$code.=<<___;
# stack layout
#
# +0 output sink
# +16 input sink [original %rsp and $num]
# +32 counters
# +64 distances between inputs and outputs
# +128 off-load area for @inp[0..3]
sub \$192,%rsp
and \$-128,%rsp
mov %rax,16(%rsp) # original %rsp
.Lenc8x_body:
vzeroupper
vmovdqu ($key),$zero # 0-round key
lea 0x78($key),$key # size optimization
lea 40*4($inp),$inp
shr \$1,$num
.Lenc8x_loop_grande:
#mov $num,24(%rsp) # original $num
xor $num,$num
___
for($i=0;$i<8;$i++) {
my $temp = $i ? $offload : $offset;
$code.=<<___;
mov `40*$i+16-40*4`($inp),$one # borrow $one for number of blocks
mov `40*$i+0-40*4`($inp),@ptr[$i] # input pointer
cmp $num,$one
mov `40*$i+8-40*4`($inp),$temp # output pointer
cmovg $one,$num # find maximum
test $one,$one
vmovdqu `40*$i+24-40*4`($inp),@out[$i] # load IV
mov $one,`32+4*$i`(%rsp) # initialize counters
cmovle %rsp,@ptr[$i] # cancel input
sub @ptr[$i],$temp # distance between input and output
mov $temp,`64+8*$i`(%rsp) # initialize distances
___
}
$code.=<<___;
test $num,$num
jz .Lenc8x_done
vmovups 0x10-0x78($key),$rndkey1
vmovups 0x20-0x78($key),$rndkey0
mov 0xf0-0x78($key),$rounds
vpxor (@ptr[0]),$zero,@inp[0] # load inputs and xor with 0-round
lea 128(%rsp),$offload # offload area
vpxor (@ptr[1]),$zero,@inp[1]
vpxor (@ptr[2]),$zero,@inp[2]
vpxor (@ptr[3]),$zero,@inp[3]
vpxor @inp[0],@out[0],@out[0]
vpxor (@ptr[4]),$zero,@inp[0]
vpxor @inp[1],@out[1],@out[1]
vpxor (@ptr[5]),$zero,@inp[1]
vpxor @inp[2],@out[2],@out[2]
vpxor (@ptr[6]),$zero,@inp[2]
vpxor @inp[3],@out[3],@out[3]
vpxor (@ptr[7]),$zero,@inp[3]
vpxor @inp[0],@out[4],@out[4]
mov \$1,$one # constant of 1
vpxor @inp[1],@out[5],@out[5]
vpxor @inp[2],@out[6],@out[6]
vpxor @inp[3],@out[7],@out[7]
jmp .Loop_enc8x
.align 32
.Loop_enc8x:
___
for($i=0;$i<8;$i++) {
my $rndkey=($i&1)?$rndkey0:$rndkey1;
$code.=<<___;
vaesenc $rndkey,@out[0],@out[0]
cmp 32+4*$i(%rsp),$one
___
$code.=<<___ if ($i);
mov 64+8*$i(%rsp),$offset
___
$code.=<<___;
vaesenc $rndkey,@out[1],@out[1]
prefetcht0 31(@ptr[$i]) # prefetch input
vaesenc $rndkey,@out[2],@out[2]
___
$code.=<<___ if ($i>1);
prefetcht0 15(@ptr[$i-2]) # prefetch output
___
$code.=<<___;
vaesenc $rndkey,@out[3],@out[3]
lea (@ptr[$i],$offset),$offset
cmovge %rsp,@ptr[$i] # cancel input
vaesenc $rndkey,@out[4],@out[4]
cmovg %rsp,$offset # sink output
vaesenc $rndkey,@out[5],@out[5]
sub @ptr[$i],$offset
vaesenc $rndkey,@out[6],@out[6]
vpxor 16(@ptr[$i]),$zero,@inp[$i%4] # load input and xor with 0-round
mov $offset,64+8*$i(%rsp)
vaesenc $rndkey,@out[7],@out[7]
vmovups `16*(3+$i)-0x78`($key),$rndkey
lea 16(@ptr[$i],$offset),@ptr[$i] # switch to output
___
$code.=<<___ if ($i<4)
vmovdqu @inp[$i%4],`16*$i`($offload) # off-load
___
}
$code.=<<___;
vmovdqu 32(%rsp),$counters
prefetcht0 15(@ptr[$i-2]) # prefetch output
prefetcht0 15(@ptr[$i-1])
cmp \$11,$rounds
jb .Lenc8x_tail
vaesenc $rndkey1,@out[0],@out[0]
vaesenc $rndkey1,@out[1],@out[1]
vaesenc $rndkey1,@out[2],@out[2]
vaesenc $rndkey1,@out[3],@out[3]
vaesenc $rndkey1,@out[4],@out[4]
vaesenc $rndkey1,@out[5],@out[5]
vaesenc $rndkey1,@out[6],@out[6]
vaesenc $rndkey1,@out[7],@out[7]
vmovups 0xb0-0x78($key),$rndkey1
vaesenc $rndkey0,@out[0],@out[0]
vaesenc $rndkey0,@out[1],@out[1]
vaesenc $rndkey0,@out[2],@out[2]
vaesenc $rndkey0,@out[3],@out[3]
vaesenc $rndkey0,@out[4],@out[4]
vaesenc $rndkey0,@out[5],@out[5]
vaesenc $rndkey0,@out[6],@out[6]
vaesenc $rndkey0,@out[7],@out[7]
vmovups 0xc0-0x78($key),$rndkey0
je .Lenc8x_tail
vaesenc $rndkey1,@out[0],@out[0]
vaesenc $rndkey1,@out[1],@out[1]
vaesenc $rndkey1,@out[2],@out[2]
vaesenc $rndkey1,@out[3],@out[3]
vaesenc $rndkey1,@out[4],@out[4]
vaesenc $rndkey1,@out[5],@out[5]
vaesenc $rndkey1,@out[6],@out[6]
vaesenc $rndkey1,@out[7],@out[7]
vmovups 0xd0-0x78($key),$rndkey1
vaesenc $rndkey0,@out[0],@out[0]
vaesenc $rndkey0,@out[1],@out[1]
vaesenc $rndkey0,@out[2],@out[2]
vaesenc $rndkey0,@out[3],@out[3]
vaesenc $rndkey0,@out[4],@out[4]
vaesenc $rndkey0,@out[5],@out[5]
vaesenc $rndkey0,@out[6],@out[6]
vaesenc $rndkey0,@out[7],@out[7]
vmovups 0xe0-0x78($key),$rndkey0
.Lenc8x_tail:
vaesenc $rndkey1,@out[0],@out[0]
vpxor $zero,$zero,$zero
vaesenc $rndkey1,@out[1],@out[1]
vaesenc $rndkey1,@out[2],@out[2]
vpcmpgtd $zero,$counters,$zero
vaesenc $rndkey1,@out[3],@out[3]
vaesenc $rndkey1,@out[4],@out[4]
vpaddd $counters,$zero,$zero # decrement counters
vmovdqu 48(%rsp),$counters
vaesenc $rndkey1,@out[5],@out[5]
mov 64(%rsp),$offset # pre-load 1st offset
vaesenc $rndkey1,@out[6],@out[6]
vaesenc $rndkey1,@out[7],@out[7]
vmovups 0x10-0x78($key),$rndkey1
vaesenclast $rndkey0,@out[0],@out[0]
vmovdqa $zero,32(%rsp) # update counters
vpxor $zero,$zero,$zero
vaesenclast $rndkey0,@out[1],@out[1]
vaesenclast $rndkey0,@out[2],@out[2]
vpcmpgtd $zero,$counters,$zero
vaesenclast $rndkey0,@out[3],@out[3]
vaesenclast $rndkey0,@out[4],@out[4]
vpaddd $zero,$counters,$counters # decrement counters
vmovdqu -0x78($key),$zero # 0-round
vaesenclast $rndkey0,@out[5],@out[5]
vaesenclast $rndkey0,@out[6],@out[6]
vmovdqa $counters,48(%rsp) # update counters
vaesenclast $rndkey0,@out[7],@out[7]
vmovups 0x20-0x78($key),$rndkey0
vmovups @out[0],-16(@ptr[0]) # write output
sub $offset,@ptr[0] # switch to input
vpxor 0x00($offload),@out[0],@out[0]
vmovups @out[1],-16(@ptr[1])
sub `64+1*8`(%rsp),@ptr[1]
vpxor 0x10($offload),@out[1],@out[1]
vmovups @out[2],-16(@ptr[2])
sub `64+2*8`(%rsp),@ptr[2]
vpxor 0x20($offload),@out[2],@out[2]
vmovups @out[3],-16(@ptr[3])
sub `64+3*8`(%rsp),@ptr[3]
vpxor 0x30($offload),@out[3],@out[3]
vmovups @out[4],-16(@ptr[4])
sub `64+4*8`(%rsp),@ptr[4]
vpxor @inp[0],@out[4],@out[4]
vmovups @out[5],-16(@ptr[5])
sub `64+5*8`(%rsp),@ptr[5]
vpxor @inp[1],@out[5],@out[5]
vmovups @out[6],-16(@ptr[6])
sub `64+6*8`(%rsp),@ptr[6]
vpxor @inp[2],@out[6],@out[6]
vmovups @out[7],-16(@ptr[7])
sub `64+7*8`(%rsp),@ptr[7]
vpxor @inp[3],@out[7],@out[7]
dec $num
jnz .Loop_enc8x
mov 16(%rsp),%rax # original %rsp
#mov 24(%rsp),$num
#lea `40*8`($inp),$inp
#dec $num
#jnz .Lenc8x_loop_grande
.Lenc8x_done:
vzeroupper
___
$code.=<<___ if ($win64);
movaps -0xd8(%rax),%xmm6
movaps -0xc8(%rax),%xmm7
movaps -0xb8(%rax),%xmm8
movaps -0xa8(%rax),%xmm9
movaps -0x98(%rax),%xmm10
movaps -0x88(%rax),%xmm11
movaps -0x78(%rax),%xmm12
movaps -0x68(%rax),%xmm13
movaps -0x58(%rax),%xmm14
movaps -0x48(%rax),%xmm15
___
$code.=<<___;
mov -48(%rax),%r15
mov -40(%rax),%r14
mov -32(%rax),%r13
mov -24(%rax),%r12
mov -16(%rax),%rbp
mov -8(%rax),%rbx
lea (%rax),%rsp
.Lenc8x_epilogue:
ret
.size aesni_multi_cbc_encrypt_avx,.-aesni_multi_cbc_encrypt_avx
.type aesni_multi_cbc_decrypt_avx,\@function,3
.align 32
aesni_multi_cbc_decrypt_avx:
_avx_cbc_dec_shortcut:
mov %rsp,%rax
push %rbx
push %rbp
push %r12
push %r13
push %r14
push %r15
___
$code.=<<___ if ($win64);
lea -0xa8(%rsp),%rsp
movaps %xmm6,(%rsp)
movaps %xmm7,0x10(%rsp)
movaps %xmm8,0x20(%rsp)
movaps %xmm9,0x30(%rsp)
movaps %xmm10,0x40(%rsp)
movaps %xmm11,0x50(%rsp)
movaps %xmm12,-0x78(%rax)
movaps %xmm13,-0x68(%rax)
movaps %xmm14,-0x58(%rax)
movaps %xmm15,-0x48(%rax)
___
$code.=<<___;
# stack layout
#
# +0 output sink
# +16 input sink [original %rsp and $num]
# +32 counters
# +64 distances between inputs and outputs
# +128 off-load area for @inp[0..3]
# +192 IV/input offload
sub \$256,%rsp
and \$-256,%rsp
sub \$192,%rsp
mov %rax,16(%rsp) # original %rsp
.Ldec8x_body:
vzeroupper
vmovdqu ($key),$zero # 0-round key
lea 0x78($key),$key # size optimization
lea 40*4($inp),$inp
shr \$1,$num
.Ldec8x_loop_grande:
#mov $num,24(%rsp) # original $num
xor $num,$num
___
for($i=0;$i<8;$i++) {
my $temp = $i ? $offload : $offset;
$code.=<<___;
mov `40*$i+16-40*4`($inp),$one # borrow $one for number of blocks
mov `40*$i+0-40*4`($inp),@ptr[$i] # input pointer
cmp $num,$one
mov `40*$i+8-40*4`($inp),$temp # output pointer
cmovg $one,$num # find maximum
test $one,$one
vmovdqu `40*$i+24-40*4`($inp),@out[$i] # load IV
mov $one,`32+4*$i`(%rsp) # initialize counters
cmovle %rsp,@ptr[$i] # cancel input
sub @ptr[$i],$temp # distance between input and output
mov $temp,`64+8*$i`(%rsp) # initialize distances
vmovdqu @out[$i],`192+16*$i`(%rsp) # offload IV
___
}
$code.=<<___;
test $num,$num
jz .Ldec8x_done
vmovups 0x10-0x78($key),$rndkey1
vmovups 0x20-0x78($key),$rndkey0
mov 0xf0-0x78($key),$rounds
lea 192+128(%rsp),$offload # offload area
vmovdqu (@ptr[0]),@out[0] # load inputs
vmovdqu (@ptr[1]),@out[1]
vmovdqu (@ptr[2]),@out[2]
vmovdqu (@ptr[3]),@out[3]
vmovdqu (@ptr[4]),@out[4]
vmovdqu (@ptr[5]),@out[5]
vmovdqu (@ptr[6]),@out[6]
vmovdqu (@ptr[7]),@out[7]
vmovdqu @out[0],0x00($offload) # offload inputs
vpxor $zero,@out[0],@out[0] # xor inputs with 0-round
vmovdqu @out[1],0x10($offload)
vpxor $zero,@out[1],@out[1]
vmovdqu @out[2],0x20($offload)
vpxor $zero,@out[2],@out[2]
vmovdqu @out[3],0x30($offload)
vpxor $zero,@out[3],@out[3]
vmovdqu @out[4],0x40($offload)
vpxor $zero,@out[4],@out[4]
vmovdqu @out[5],0x50($offload)
vpxor $zero,@out[5],@out[5]
vmovdqu @out[6],0x60($offload)
vpxor $zero,@out[6],@out[6]
vmovdqu @out[7],0x70($offload)
vpxor $zero,@out[7],@out[7]
xor \$0x80,$offload
mov \$1,$one # constant of 1
jmp .Loop_dec8x
.align 32
.Loop_dec8x:
___
for($i=0;$i<8;$i++) {
my $rndkey=($i&1)?$rndkey0:$rndkey1;
$code.=<<___;
vaesdec $rndkey,@out[0],@out[0]
cmp 32+4*$i(%rsp),$one
___
$code.=<<___ if ($i);
mov 64+8*$i(%rsp),$offset
___
$code.=<<___;
vaesdec $rndkey,@out[1],@out[1]
prefetcht0 31(@ptr[$i]) # prefetch input
vaesdec $rndkey,@out[2],@out[2]
___
$code.=<<___ if ($i>1);
prefetcht0 15(@ptr[$i-2]) # prefetch output
___
$code.=<<___;
vaesdec $rndkey,@out[3],@out[3]
lea (@ptr[$i],$offset),$offset
cmovge %rsp,@ptr[$i] # cancel input
vaesdec $rndkey,@out[4],@out[4]
cmovg %rsp,$offset # sink output
vaesdec $rndkey,@out[5],@out[5]
sub @ptr[$i],$offset
vaesdec $rndkey,@out[6],@out[6]
vmovdqu 16(@ptr[$i]),@inp[$i%4] # load input
mov $offset,64+8*$i(%rsp)
vaesdec $rndkey,@out[7],@out[7]
vmovups `16*(3+$i)-0x78`($key),$rndkey
lea 16(@ptr[$i],$offset),@ptr[$i] # switch to output
___
$code.=<<___ if ($i<4);
vmovdqu @inp[$i%4],`128+16*$i`(%rsp) # off-load
___
}
$code.=<<___;
vmovdqu 32(%rsp),$counters
prefetcht0 15(@ptr[$i-2]) # prefetch output
prefetcht0 15(@ptr[$i-1])
cmp \$11,$rounds
jb .Ldec8x_tail
vaesdec $rndkey1,@out[0],@out[0]
vaesdec $rndkey1,@out[1],@out[1]
vaesdec $rndkey1,@out[2],@out[2]
vaesdec $rndkey1,@out[3],@out[3]
vaesdec $rndkey1,@out[4],@out[4]
vaesdec $rndkey1,@out[5],@out[5]
vaesdec $rndkey1,@out[6],@out[6]
vaesdec $rndkey1,@out[7],@out[7]
vmovups 0xb0-0x78($key),$rndkey1
vaesdec $rndkey0,@out[0],@out[0]
vaesdec $rndkey0,@out[1],@out[1]
vaesdec $rndkey0,@out[2],@out[2]
vaesdec $rndkey0,@out[3],@out[3]
vaesdec $rndkey0,@out[4],@out[4]
vaesdec $rndkey0,@out[5],@out[5]
vaesdec $rndkey0,@out[6],@out[6]
vaesdec $rndkey0,@out[7],@out[7]
vmovups 0xc0-0x78($key),$rndkey0
je .Ldec8x_tail
vaesdec $rndkey1,@out[0],@out[0]
vaesdec $rndkey1,@out[1],@out[1]
vaesdec $rndkey1,@out[2],@out[2]
vaesdec $rndkey1,@out[3],@out[3]
vaesdec $rndkey1,@out[4],@out[4]
vaesdec $rndkey1,@out[5],@out[5]
vaesdec $rndkey1,@out[6],@out[6]
vaesdec $rndkey1,@out[7],@out[7]
vmovups 0xd0-0x78($key),$rndkey1
vaesdec $rndkey0,@out[0],@out[0]
vaesdec $rndkey0,@out[1],@out[1]
vaesdec $rndkey0,@out[2],@out[2]
vaesdec $rndkey0,@out[3],@out[3]
vaesdec $rndkey0,@out[4],@out[4]
vaesdec $rndkey0,@out[5],@out[5]
vaesdec $rndkey0,@out[6],@out[6]
vaesdec $rndkey0,@out[7],@out[7]
vmovups 0xe0-0x78($key),$rndkey0
.Ldec8x_tail:
vaesdec $rndkey1,@out[0],@out[0]
vpxor $zero,$zero,$zero
vaesdec $rndkey1,@out[1],@out[1]
vaesdec $rndkey1,@out[2],@out[2]
vpcmpgtd $zero,$counters,$zero
vaesdec $rndkey1,@out[3],@out[3]
vaesdec $rndkey1,@out[4],@out[4]
vpaddd $counters,$zero,$zero # decrement counters
vmovdqu 48(%rsp),$counters
vaesdec $rndkey1,@out[5],@out[5]
mov 64(%rsp),$offset # pre-load 1st offset
vaesdec $rndkey1,@out[6],@out[6]
vaesdec $rndkey1,@out[7],@out[7]
vmovups 0x10-0x78($key),$rndkey1
vaesdeclast $rndkey0,@out[0],@out[0]
vmovdqa $zero,32(%rsp) # update counters
vpxor $zero,$zero,$zero
vaesdeclast $rndkey0,@out[1],@out[1]
vpxor 0x00($offload),@out[0],@out[0] # xor with IV
vaesdeclast $rndkey0,@out[2],@out[2]
vpxor 0x10($offload),@out[1],@out[1]
vpcmpgtd $zero,$counters,$zero
vaesdeclast $rndkey0,@out[3],@out[3]
vpxor 0x20($offload),@out[2],@out[2]
vaesdeclast $rndkey0,@out[4],@out[4]
vpxor 0x30($offload),@out[3],@out[3]
vpaddd $zero,$counters,$counters # decrement counters
vmovdqu -0x78($key),$zero # 0-round
vaesdeclast $rndkey0,@out[5],@out[5]
vpxor 0x40($offload),@out[4],@out[4]
vaesdeclast $rndkey0,@out[6],@out[6]
vpxor 0x50($offload),@out[5],@out[5]
vmovdqa $counters,48(%rsp) # update counters
vaesdeclast $rndkey0,@out[7],@out[7]
vpxor 0x60($offload),@out[6],@out[6]
vmovups 0x20-0x78($key),$rndkey0
vmovups @out[0],-16(@ptr[0]) # write output
sub $offset,@ptr[0] # switch to input
vmovdqu 128+0(%rsp),@out[0]
vpxor 0x70($offload),@out[7],@out[7]
vmovups @out[1],-16(@ptr[1])
sub `64+1*8`(%rsp),@ptr[1]
vmovdqu @out[0],0x00($offload)
vpxor $zero,@out[0],@out[0]
vmovdqu 128+16(%rsp),@out[1]
vmovups @out[2],-16(@ptr[2])
sub `64+2*8`(%rsp),@ptr[2]
vmovdqu @out[1],0x10($offload)
vpxor $zero,@out[1],@out[1]
vmovdqu 128+32(%rsp),@out[2]
vmovups @out[3],-16(@ptr[3])
sub `64+3*8`(%rsp),@ptr[3]
vmovdqu @out[2],0x20($offload)
vpxor $zero,@out[2],@out[2]
vmovdqu 128+48(%rsp),@out[3]
vmovups @out[4],-16(@ptr[4])
sub `64+4*8`(%rsp),@ptr[4]
vmovdqu @out[3],0x30($offload)
vpxor $zero,@out[3],@out[3]
vmovdqu @inp[0],0x40($offload)
vpxor @inp[0],$zero,@out[4]
vmovups @out[5],-16(@ptr[5])
sub `64+5*8`(%rsp),@ptr[5]
vmovdqu @inp[1],0x50($offload)
vpxor @inp[1],$zero,@out[5]
vmovups @out[6],-16(@ptr[6])
sub `64+6*8`(%rsp),@ptr[6]
vmovdqu @inp[2],0x60($offload)
vpxor @inp[2],$zero,@out[6]
vmovups @out[7],-16(@ptr[7])
sub `64+7*8`(%rsp),@ptr[7]
vmovdqu @inp[3],0x70($offload)
vpxor @inp[3],$zero,@out[7]
xor \$128,$offload
dec $num
jnz .Loop_dec8x
mov 16(%rsp),%rax # original %rsp
#mov 24(%rsp),$num
#lea `40*8`($inp),$inp
#dec $num
#jnz .Ldec8x_loop_grande
.Ldec8x_done:
vzeroupper
___
$code.=<<___ if ($win64);
movaps -0xd8(%rax),%xmm6
movaps -0xc8(%rax),%xmm7
movaps -0xb8(%rax),%xmm8
movaps -0xa8(%rax),%xmm9
movaps -0x98(%rax),%xmm10
movaps -0x88(%rax),%xmm11
movaps -0x78(%rax),%xmm12
movaps -0x68(%rax),%xmm13
movaps -0x58(%rax),%xmm14
movaps -0x48(%rax),%xmm15
___
$code.=<<___;
mov -48(%rax),%r15
mov -40(%rax),%r14
mov -32(%rax),%r13
mov -24(%rax),%r12
mov -16(%rax),%rbp
mov -8(%rax),%rbx
lea (%rax),%rsp
.Ldec8x_epilogue:
ret
.size aesni_multi_cbc_decrypt_avx,.-aesni_multi_cbc_decrypt_avx
___
}}}
if ($win64) {
# EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
# CONTEXT *context,DISPATCHER_CONTEXT *disp)
$rec="%rcx";
$frame="%rdx";
$context="%r8";
$disp="%r9";
$code.=<<___;
.extern __imp_RtlVirtualUnwind
.type se_handler,\@abi-omnipotent
.align 16
se_handler:
push %rsi
push %rdi
push %rbx
push %rbp
push %r12
push %r13
push %r14
push %r15
pushfq
sub \$64,%rsp
mov 120($context),%rax # pull context->Rax
mov 248($context),%rbx # pull context->Rip
mov 8($disp),%rsi # disp->ImageBase
mov 56($disp),%r11 # disp->HandlerData
mov 0(%r11),%r10d # HandlerData[0]
lea (%rsi,%r10),%r10 # prologue label
cmp %r10,%rbx # context->Rip<.Lprologue
jb .Lin_prologue
mov 152($context),%rax # pull context->Rsp
mov 4(%r11),%r10d # HandlerData[1]
lea (%rsi,%r10),%r10 # epilogue label
cmp %r10,%rbx # context->Rip>=.Lepilogue
jae .Lin_prologue
mov 16(%rax),%rax # pull saved stack pointer
mov -8(%rax),%rbx
mov -16(%rax),%rbp
mov -24(%rax),%r12
mov -32(%rax),%r13
mov -40(%rax),%r14
mov -48(%rax),%r15
mov %rbx,144($context) # restore context->Rbx
mov %rbp,160($context) # restore context->Rbp
mov %r12,216($context) # restore cotnext->R12
mov %r13,224($context) # restore cotnext->R13
mov %r14,232($context) # restore cotnext->R14
mov %r15,240($context) # restore cotnext->R15
lea -56-10*16(%rax),%rsi
lea 512($context),%rdi # &context.Xmm6
mov \$20,%ecx
.long 0xa548f3fc # cld; rep movsq
.Lin_prologue:
mov 8(%rax),%rdi
mov 16(%rax),%rsi
mov %rax,152($context) # restore context->Rsp
mov %rsi,168($context) # restore context->Rsi
mov %rdi,176($context) # restore context->Rdi
mov 40($disp),%rdi # disp->ContextRecord
mov $context,%rsi # context
mov \$154,%ecx # sizeof(CONTEXT)
.long 0xa548f3fc # cld; rep movsq
mov $disp,%rsi
xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
mov 8(%rsi),%rdx # arg2, disp->ImageBase
mov 0(%rsi),%r8 # arg3, disp->ControlPc
mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
mov 40(%rsi),%r10 # disp->ContextRecord
lea 56(%rsi),%r11 # &disp->HandlerData
lea 24(%rsi),%r12 # &disp->EstablisherFrame
mov %r10,32(%rsp) # arg5
mov %r11,40(%rsp) # arg6
mov %r12,48(%rsp) # arg7
mov %rcx,56(%rsp) # arg8, (NULL)
call *__imp_RtlVirtualUnwind(%rip)
mov \$1,%eax # ExceptionContinueSearch
add \$64,%rsp
popfq
pop %r15
pop %r14
pop %r13
pop %r12
pop %rbp
pop %rbx
pop %rdi
pop %rsi
ret
.size se_handler,.-se_handler
.section .pdata
.align 4
.rva .LSEH_begin_aesni_multi_cbc_encrypt
.rva .LSEH_end_aesni_multi_cbc_encrypt
.rva .LSEH_info_aesni_multi_cbc_encrypt
.rva .LSEH_begin_aesni_multi_cbc_decrypt
.rva .LSEH_end_aesni_multi_cbc_decrypt
.rva .LSEH_info_aesni_multi_cbc_decrypt
___
$code.=<<___ if ($avx);
.rva .LSEH_begin_aesni_multi_cbc_encrypt_avx
.rva .LSEH_end_aesni_multi_cbc_encrypt_avx
.rva .LSEH_info_aesni_multi_cbc_encrypt_avx
.rva .LSEH_begin_aesni_multi_cbc_decrypt_avx
.rva .LSEH_end_aesni_multi_cbc_decrypt_avx
.rva .LSEH_info_aesni_multi_cbc_decrypt_avx
___
$code.=<<___;
.section .xdata
.align 8
.LSEH_info_aesni_multi_cbc_encrypt:
.byte 9,0,0,0
.rva se_handler
.rva .Lenc4x_body,.Lenc4x_epilogue # HandlerData[]
.LSEH_info_aesni_multi_cbc_decrypt:
.byte 9,0,0,0
.rva se_handler
.rva .Ldec4x_body,.Ldec4x_epilogue # HandlerData[]
___
$code.=<<___ if ($avx);
.LSEH_info_aesni_multi_cbc_encrypt_avx:
.byte 9,0,0,0
.rva se_handler
.rva .Lenc8x_body,.Lenc8x_epilogue # HandlerData[]
.LSEH_info_aesni_multi_cbc_decrypt_avx:
.byte 9,0,0,0
.rva se_handler
.rva .Ldec8x_body,.Ldec8x_epilogue # HandlerData[]
___
}
####################################################################
sub rex {
local *opcode=shift;
my ($dst,$src)=@_;
my $rex=0;
$rex|=0x04 if($dst>=8);
$rex|=0x01 if($src>=8);
push @opcode,$rex|0x40 if($rex);
}
sub aesni {
my $line=shift;
my @opcode=(0x66);
if ($line=~/(aeskeygenassist)\s+\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
rex(\@opcode,$4,$3);
push @opcode,0x0f,0x3a,0xdf;
push @opcode,0xc0|($3&7)|(($4&7)<<3); # ModR/M
my $c=$2;
push @opcode,$c=~/^0/?oct($c):$c;
return ".byte\t".join(',',@opcode);
}
elsif ($line=~/(aes[a-z]+)\s+%xmm([0-9]+),\s*%xmm([0-9]+)/) {
my %opcodelet = (
"aesimc" => 0xdb,
"aesenc" => 0xdc, "aesenclast" => 0xdd,
"aesdec" => 0xde, "aesdeclast" => 0xdf
);
return undef if (!defined($opcodelet{$1}));
rex(\@opcode,$3,$2);
push @opcode,0x0f,0x38,$opcodelet{$1};
push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
return ".byte\t".join(',',@opcode);
}
elsif ($line=~/(aes[a-z]+)\s+([0x1-9a-fA-F]*)\(%rsp\),\s*%xmm([0-9]+)/) {
my %opcodelet = (
"aesenc" => 0xdc, "aesenclast" => 0xdd,
"aesdec" => 0xde, "aesdeclast" => 0xdf
);
return undef if (!defined($opcodelet{$1}));
my $off = $2;
push @opcode,0x44 if ($3>=8);
push @opcode,0x0f,0x38,$opcodelet{$1};
push @opcode,0x44|(($3&7)<<3),0x24; # ModR/M
push @opcode,($off=~/^0/?oct($off):$off)&0xff;
return ".byte\t".join(',',@opcode);
}
return $line;
}
$code =~ s/\`([^\`]*)\`/eval($1)/gem;
$code =~ s/\b(aes.*%xmm[0-9]+).*$/aesni($1)/gem;
print $code;
close STDOUT;
|