summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorjsing <>2024-03-27 12:42:30 +0000
committerjsing <>2024-03-27 12:42:30 +0000
commitfa47f9b2c9e10ca57d1333a9d9d84269455b0b4d (patch)
tree65ee5f8991e523a0a3415264e4f25955c160cd47 /src/lib
parentaeea5ed9e2aa0a1700015a429fd111890bae7a68 (diff)
downloadopenbsd-fa47f9b2c9e10ca57d1333a9d9d84269455b0b4d.tar.gz
openbsd-fa47f9b2c9e10ca57d1333a9d9d84269455b0b4d.tar.bz2
openbsd-fa47f9b2c9e10ca57d1333a9d9d84269455b0b4d.zip
Remove assembly for stitched modes.
The stitched modes have been removed, so having assembly for them is of little use.
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libcrypto/aes/asm/aesni-sha1-x86_64.pl1237
-rw-r--r--src/lib/libcrypto/arch/amd64/Makefile.inc5
-rw-r--r--src/lib/libcrypto/rc4/asm/rc4-md5-x86_64.pl515
3 files changed, 1 insertions, 1756 deletions
diff --git a/src/lib/libcrypto/aes/asm/aesni-sha1-x86_64.pl b/src/lib/libcrypto/aes/asm/aesni-sha1-x86_64.pl
deleted file mode 100644
index 5eb5b7bf65..0000000000
--- a/src/lib/libcrypto/aes/asm/aesni-sha1-x86_64.pl
+++ /dev/null
@@ -1,1237 +0,0 @@
1#!/usr/bin/env perl
2#
3# ====================================================================
4# Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
5# project. The module is, however, dual licensed under OpenSSL and
6# CRYPTOGAMS licenses depending on where you obtain it. For further
7# details see http://www.openssl.org/~appro/cryptogams/.
8# ====================================================================
9#
10# June 2011
11#
12# This is AESNI-CBC+SHA1 "stitch" implementation. The idea, as spelled
13# in http://download.intel.com/design/intarch/papers/323686.pdf, is
14# that since AESNI-CBC encrypt exhibit *very* low instruction-level
15# parallelism, interleaving it with another algorithm would allow to
16# utilize processor resources better and achieve better performance.
17# SHA1 instruction sequences(*) are taken from sha1-x86_64.pl and
18# AESNI code is weaved into it. Below are performance numbers in
19# cycles per processed byte, less is better, for standalone AESNI-CBC
20# encrypt, sum of the latter and standalone SHA1, and "stitched"
21# subroutine:
22#
23# AES-128-CBC +SHA1 stitch gain
24# Westmere 3.77[+5.6] 9.37 6.65 +41%
25# Sandy Bridge 5.05[+5.2(6.3)] 10.25(11.35) 6.16(7.08) +67%(+60%)
26#
27# AES-192-CBC
28# Westmere 4.51 10.11 6.97 +45%
29# Sandy Bridge 6.05 11.25(12.35) 6.34(7.27) +77%(+70%)
30#
31# AES-256-CBC
32# Westmere 5.25 10.85 7.25 +50%
33# Sandy Bridge 7.05 12.25(13.35) 7.06(7.70) +74%(+73%)
34#
35# (*) There are two code paths: SSSE3 and AVX. See sha1-568.pl for
36# background information. Above numbers in parentheses are SSSE3
37# results collected on AVX-capable CPU, i.e. apply on OSes that
38# don't support AVX.
39#
40# Needless to mention that it makes no sense to implement "stitched"
41# *decrypt* subroutine. Because *both* AESNI-CBC decrypt and SHA1
42# fully utilize parallelism, so stitching would not give any gain
43# anyway. Well, there might be some, e.g. because of better cache
44# locality... For reference, here are performance results for
45# standalone AESNI-CBC decrypt:
46#
47# AES-128-CBC AES-192-CBC AES-256-CBC
48# Westmere 1.31 1.55 1.80
49# Sandy Bridge 0.93 1.06 1.22
50
51$flavour = shift;
52$output = shift;
53if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
54
55$win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
56
57$0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
58( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
59( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
60die "can't locate x86_64-xlate.pl";
61
62$avx=1 if (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
63 =~ /GNU assembler version ([2-9]\.[0-9]+)/ &&
64 $1>=2.19);
65$avx=1 if (!$avx && $win64 && ($flavour =~ /nasm/ || $ENV{ASM} =~ /nasm/) &&
66 `nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)/ &&
67 $1>=2.09);
68$avx=1 if (!$avx && $win64 && ($flavour =~ /masm/ || $ENV{ASM} =~ /ml64/) &&
69 `ml64 2>&1` =~ /Version ([0-9]+)\./ &&
70 $1>=10);
71
72open OUT,"| \"$^X\" $xlate $flavour $output";
73*STDOUT=*OUT;
74
75# void aesni_cbc_sha1_enc(const void *inp,
76# void *out,
77# size_t length,
78# const AES_KEY *key,
79# unsigned char *iv,
80# SHA_CTX *ctx,
81# const void *in0);
82
83$code.=<<___;
84.text
85.extern OPENSSL_ia32cap_P
86.hidden OPENSSL_ia32cap_P
87
88.globl aesni_cbc_sha1_enc
89.type aesni_cbc_sha1_enc,\@abi-omnipotent
90.align 16
91aesni_cbc_sha1_enc:
92 _CET_ENDBR
93 # caller should check for SSSE3 and AES-NI bits
94 mov OPENSSL_ia32cap_P+0(%rip),%r10d
95 mov OPENSSL_ia32cap_P+4(%rip),%r11d
96___
97$code.=<<___ if ($avx);
98 and \$IA32CAP_MASK1_AVX,%r11d # mask AVX bit
99 and \$IA32CAP_MASK0_INTEL,%r10d # mask "Intel CPU" bit
100 or %r11d,%r10d
101 cmp \$(IA32CAP_MASK1_AVX|IA32CAP_MASK0_INTEL),%r10d
102 je aesni_cbc_sha1_enc_avx
103___
104$code.=<<___;
105 jmp aesni_cbc_sha1_enc_ssse3
106 ret
107.size aesni_cbc_sha1_enc,.-aesni_cbc_sha1_enc
108___
109
110my ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
111
112my $Xi=4;
113my @X=map("%xmm$_",(4..7,0..3));
114my @Tx=map("%xmm$_",(8..10));
115my @V=($A,$B,$C,$D,$E)=("%eax","%ebx","%ecx","%edx","%ebp"); # size optimization
116my @T=("%esi","%edi");
117my $j=0; my $jj=0; my $r=0; my $sn=0;
118my $K_XX_XX="%r11";
119my ($iv,$in,$rndkey0)=map("%xmm$_",(11..13));
120my @rndkey=("%xmm14","%xmm15");
121
122sub AUTOLOAD() # thunk [simplified] 32-bit style perlasm
123{ my $opcode = $AUTOLOAD; $opcode =~ s/.*:://;
124 my $arg = pop;
125 $arg = "\$$arg" if ($arg*1 eq $arg);
126 $code .= "\t$opcode\t".join(',',$arg,reverse @_)."\n";
127}
128
129my $_rol=sub { &rol(@_) };
130my $_ror=sub { &ror(@_) };
131
132$code.=<<___;
133.type aesni_cbc_sha1_enc_ssse3,\@function,6
134.align 16
135aesni_cbc_sha1_enc_ssse3:
136 _CET_ENDBR
137 mov `($win64?56:8)`(%rsp),$inp # load 7th argument
138 #shr \$6,$len # debugging artefact
139 #jz .Lepilogue_ssse3 # debugging artefact
140 push %rbx
141 push %rbp
142 push %r12
143 push %r13
144 push %r14
145 push %r15
146 lea `-104-($win64?10*16:0)`(%rsp),%rsp
147 #mov $in0,$inp # debugging artefact
148 #lea 64(%rsp),$ctx # debugging artefact
149___
150$code.=<<___ if ($win64);
151 movaps %xmm6,96+0(%rsp)
152 movaps %xmm7,96+16(%rsp)
153 movaps %xmm8,96+32(%rsp)
154 movaps %xmm9,96+48(%rsp)
155 movaps %xmm10,96+64(%rsp)
156 movaps %xmm11,96+80(%rsp)
157 movaps %xmm12,96+96(%rsp)
158 movaps %xmm13,96+112(%rsp)
159 movaps %xmm14,96+128(%rsp)
160 movaps %xmm15,96+144(%rsp)
161.Lprologue_ssse3:
162___
163$code.=<<___;
164 mov $in0,%r12 # reassign arguments
165 mov $out,%r13
166 mov $len,%r14
167 mov $key,%r15
168 movdqu ($ivp),$iv # load IV
169 mov $ivp,88(%rsp) # save $ivp
170___
171my ($in0,$out,$len,$key)=map("%r$_",(12..15)); # reassign arguments
172my $rounds="${ivp}d";
173$code.=<<___;
174 shl \$6,$len
175 sub $in0,$out
176 mov 240($key),$rounds
177 add $inp,$len # end of input
178
179 lea K_XX_XX(%rip),$K_XX_XX
180 mov 0($ctx),$A # load context
181 mov 4($ctx),$B
182 mov 8($ctx),$C
183 mov 12($ctx),$D
184 mov $B,@T[0] # magic seed
185 mov 16($ctx),$E
186
187 movdqa 64($K_XX_XX),@X[2] # pbswap mask
188 movdqa 0($K_XX_XX),@Tx[1] # K_00_19
189 movdqu 0($inp),@X[-4&7] # load input to %xmm[0-3]
190 movdqu 16($inp),@X[-3&7]
191 movdqu 32($inp),@X[-2&7]
192 movdqu 48($inp),@X[-1&7]
193 pshufb @X[2],@X[-4&7] # byte swap
194 add \$64,$inp
195 pshufb @X[2],@X[-3&7]
196 pshufb @X[2],@X[-2&7]
197 pshufb @X[2],@X[-1&7]
198 paddd @Tx[1],@X[-4&7] # add K_00_19
199 paddd @Tx[1],@X[-3&7]
200 paddd @Tx[1],@X[-2&7]
201 movdqa @X[-4&7],0(%rsp) # X[]+K xfer to IALU
202 psubd @Tx[1],@X[-4&7] # restore X[]
203 movdqa @X[-3&7],16(%rsp)
204 psubd @Tx[1],@X[-3&7]
205 movdqa @X[-2&7],32(%rsp)
206 psubd @Tx[1],@X[-2&7]
207 movups ($key),$rndkey0 # $key[0]
208 movups 16($key),$rndkey[0] # forward reference
209 jmp .Loop_ssse3
210___
211
212my $aesenc=sub {
213 use integer;
214 my ($n,$k)=($r/10,$r%10);
215 if ($k==0) {
216 $code.=<<___;
217 movups `16*$n`($in0),$in # load input
218 xorps $rndkey0,$in
219___
220 $code.=<<___ if ($n);
221 movups $iv,`16*($n-1)`($out,$in0) # write output
222___
223 $code.=<<___;
224 xorps $in,$iv
225 aesenc $rndkey[0],$iv
226 movups `32+16*$k`($key),$rndkey[1]
227___
228 } elsif ($k==9) {
229 $sn++;
230 $code.=<<___;
231 cmp \$11,$rounds
232 jb .Laesenclast$sn
233 movups `32+16*($k+0)`($key),$rndkey[1]
234 aesenc $rndkey[0],$iv
235 movups `32+16*($k+1)`($key),$rndkey[0]
236 aesenc $rndkey[1],$iv
237 je .Laesenclast$sn
238 movups `32+16*($k+2)`($key),$rndkey[1]
239 aesenc $rndkey[0],$iv
240 movups `32+16*($k+3)`($key),$rndkey[0]
241 aesenc $rndkey[1],$iv
242.Laesenclast$sn:
243 aesenclast $rndkey[0],$iv
244 movups 16($key),$rndkey[1] # forward reference
245___
246 } else {
247 $code.=<<___;
248 aesenc $rndkey[0],$iv
249 movups `32+16*$k`($key),$rndkey[1]
250___
251 }
252 $r++; unshift(@rndkey,pop(@rndkey));
253};
254
255sub Xupdate_ssse3_16_31() # recall that $Xi starts with 4
256{ use integer;
257 my $body = shift;
258 my @insns = (&$body,&$body,&$body,&$body); # 40 instructions
259 my ($a,$b,$c,$d,$e);
260
261 &movdqa (@X[0],@X[-3&7]);
262 eval(shift(@insns));
263 eval(shift(@insns));
264 &movdqa (@Tx[0],@X[-1&7]);
265 &palignr(@X[0],@X[-4&7],8); # compose "X[-14]" in "X[0]"
266 eval(shift(@insns));
267 eval(shift(@insns));
268
269 &paddd (@Tx[1],@X[-1&7]);
270 eval(shift(@insns));
271 eval(shift(@insns));
272 &psrldq (@Tx[0],4); # "X[-3]", 3 dwords
273 eval(shift(@insns));
274 eval(shift(@insns));
275 &pxor (@X[0],@X[-4&7]); # "X[0]"^="X[-16]"
276 eval(shift(@insns));
277 eval(shift(@insns));
278
279 &pxor (@Tx[0],@X[-2&7]); # "X[-3]"^"X[-8]"
280 eval(shift(@insns));
281 eval(shift(@insns));
282 eval(shift(@insns));
283 eval(shift(@insns));
284
285 &pxor (@X[0],@Tx[0]); # "X[0]"^="X[-3]"^"X[-8]"
286 eval(shift(@insns));
287 eval(shift(@insns));
288 &movdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
289 eval(shift(@insns));
290 eval(shift(@insns));
291
292 &movdqa (@Tx[2],@X[0]);
293 &movdqa (@Tx[0],@X[0]);
294 eval(shift(@insns));
295 eval(shift(@insns));
296 eval(shift(@insns));
297 eval(shift(@insns));
298
299 &pslldq (@Tx[2],12); # "X[0]"<<96, extract one dword
300 &paddd (@X[0],@X[0]);
301 eval(shift(@insns));
302 eval(shift(@insns));
303 eval(shift(@insns));
304 eval(shift(@insns));
305
306 &psrld (@Tx[0],31);
307 eval(shift(@insns));
308 eval(shift(@insns));
309 &movdqa (@Tx[1],@Tx[2]);
310 eval(shift(@insns));
311 eval(shift(@insns));
312
313 &psrld (@Tx[2],30);
314 &por (@X[0],@Tx[0]); # "X[0]"<<<=1
315 eval(shift(@insns));
316 eval(shift(@insns));
317 eval(shift(@insns));
318 eval(shift(@insns));
319
320 &pslld (@Tx[1],2);
321 &pxor (@X[0],@Tx[2]);
322 eval(shift(@insns));
323 eval(shift(@insns));
324 &movdqa (@Tx[2],eval(16*(($Xi)/5))."($K_XX_XX)"); # K_XX_XX
325 eval(shift(@insns));
326 eval(shift(@insns));
327
328 &pxor (@X[0],@Tx[1]); # "X[0]"^=("X[0]">>96)<<<2
329
330 foreach (@insns) { eval; } # remaining instructions [if any]
331
332 $Xi++; push(@X,shift(@X)); # "rotate" X[]
333 push(@Tx,shift(@Tx));
334}
335
336sub Xupdate_ssse3_32_79()
337{ use integer;
338 my $body = shift;
339 my @insns = (&$body,&$body,&$body,&$body); # 32 to 48 instructions
340 my ($a,$b,$c,$d,$e);
341
342 &movdqa (@Tx[0],@X[-1&7]) if ($Xi==8);
343 eval(shift(@insns)); # body_20_39
344 &pxor (@X[0],@X[-4&7]); # "X[0]"="X[-32]"^"X[-16]"
345 &palignr(@Tx[0],@X[-2&7],8); # compose "X[-6]"
346 eval(shift(@insns));
347 eval(shift(@insns));
348 eval(shift(@insns)); # rol
349
350 &pxor (@X[0],@X[-7&7]); # "X[0]"^="X[-28]"
351 eval(shift(@insns));
352 eval(shift(@insns)) if (@insns[0] !~ /&ro[rl]/);
353 if ($Xi%5) {
354 &movdqa (@Tx[2],@Tx[1]);# "perpetuate" K_XX_XX...
355 } else { # ... or load next one
356 &movdqa (@Tx[2],eval(16*($Xi/5))."($K_XX_XX)");
357 }
358 &paddd (@Tx[1],@X[-1&7]);
359 eval(shift(@insns)); # ror
360 eval(shift(@insns));
361
362 &pxor (@X[0],@Tx[0]); # "X[0]"^="X[-6]"
363 eval(shift(@insns)); # body_20_39
364 eval(shift(@insns));
365 eval(shift(@insns));
366 eval(shift(@insns)); # rol
367
368 &movdqa (@Tx[0],@X[0]);
369 &movdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
370 eval(shift(@insns));
371 eval(shift(@insns));
372 eval(shift(@insns)); # ror
373 eval(shift(@insns));
374
375 &pslld (@X[0],2);
376 eval(shift(@insns)); # body_20_39
377 eval(shift(@insns));
378 &psrld (@Tx[0],30);
379 eval(shift(@insns));
380 eval(shift(@insns)); # rol
381 eval(shift(@insns));
382 eval(shift(@insns));
383 eval(shift(@insns)); # ror
384 eval(shift(@insns));
385
386 &por (@X[0],@Tx[0]); # "X[0]"<<<=2
387 eval(shift(@insns)); # body_20_39
388 eval(shift(@insns));
389 &movdqa (@Tx[1],@X[0]) if ($Xi<19);
390 eval(shift(@insns));
391 eval(shift(@insns)); # rol
392 eval(shift(@insns));
393 eval(shift(@insns));
394 eval(shift(@insns)); # rol
395 eval(shift(@insns));
396
397 foreach (@insns) { eval; } # remaining instructions
398
399 $Xi++; push(@X,shift(@X)); # "rotate" X[]
400 push(@Tx,shift(@Tx));
401}
402
403sub Xuplast_ssse3_80()
404{ use integer;
405 my $body = shift;
406 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
407 my ($a,$b,$c,$d,$e);
408
409 eval(shift(@insns));
410 &paddd (@Tx[1],@X[-1&7]);
411 eval(shift(@insns));
412 eval(shift(@insns));
413 eval(shift(@insns));
414 eval(shift(@insns));
415
416 &movdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer IALU
417
418 foreach (@insns) { eval; } # remaining instructions
419
420 &cmp ($inp,$len);
421 &je (".Ldone_ssse3");
422
423 unshift(@Tx,pop(@Tx));
424
425 &movdqa (@X[2],"64($K_XX_XX)"); # pbswap mask
426 &movdqa (@Tx[1],"0($K_XX_XX)"); # K_00_19
427 &movdqu (@X[-4&7],"0($inp)"); # load input
428 &movdqu (@X[-3&7],"16($inp)");
429 &movdqu (@X[-2&7],"32($inp)");
430 &movdqu (@X[-1&7],"48($inp)");
431 &pshufb (@X[-4&7],@X[2]); # byte swap
432 &add ($inp,64);
433
434 $Xi=0;
435}
436
437sub Xloop_ssse3()
438{ use integer;
439 my $body = shift;
440 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
441 my ($a,$b,$c,$d,$e);
442
443 eval(shift(@insns));
444 eval(shift(@insns));
445 &pshufb (@X[($Xi-3)&7],@X[2]);
446 eval(shift(@insns));
447 eval(shift(@insns));
448 &paddd (@X[($Xi-4)&7],@Tx[1]);
449 eval(shift(@insns));
450 eval(shift(@insns));
451 eval(shift(@insns));
452 eval(shift(@insns));
453 &movdqa (eval(16*$Xi)."(%rsp)",@X[($Xi-4)&7]); # X[]+K xfer to IALU
454 eval(shift(@insns));
455 eval(shift(@insns));
456 &psubd (@X[($Xi-4)&7],@Tx[1]);
457
458 foreach (@insns) { eval; }
459 $Xi++;
460}
461
462sub Xtail_ssse3()
463{ use integer;
464 my $body = shift;
465 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
466 my ($a,$b,$c,$d,$e);
467
468 foreach (@insns) { eval; }
469}
470
471sub body_00_19 () {
472 use integer;
473 my ($k,$n);
474 my @r=(
475 '($a,$b,$c,$d,$e)=@V;'.
476 '&add ($e,eval(4*($j&15))."(%rsp)");', # X[]+K xfer
477 '&xor ($c,$d);',
478 '&mov (@T[1],$a);', # $b in next round
479 '&$_rol ($a,5);',
480 '&and (@T[0],$c);', # ($b&($c^$d))
481 '&xor ($c,$d);', # restore $c
482 '&xor (@T[0],$d);',
483 '&add ($e,$a);',
484 '&$_ror ($b,$j?7:2);', # $b>>>2
485 '&add ($e,@T[0]);' .'$j++; unshift(@V,pop(@V)); unshift(@T,pop(@T));'
486 );
487 $n = scalar(@r);
488 $k = (($jj+1)*12/20)*20*$n/12; # 12 aesencs per these 20 rounds
489 @r[$k%$n].='&$aesenc();' if ($jj==$k/$n);
490 $jj++;
491 return @r;
492}
493
494sub body_20_39 () {
495 use integer;
496 my ($k,$n);
497 my @r=(
498 '($a,$b,$c,$d,$e)=@V;'.
499 '&add ($e,eval(4*($j++&15))."(%rsp)");', # X[]+K xfer
500 '&xor (@T[0],$d);', # ($b^$d)
501 '&mov (@T[1],$a);', # $b in next round
502 '&$_rol ($a,5);',
503 '&xor (@T[0],$c);', # ($b^$d^$c)
504 '&add ($e,$a);',
505 '&$_ror ($b,7);', # $b>>>2
506 '&add ($e,@T[0]);' .'unshift(@V,pop(@V)); unshift(@T,pop(@T));'
507 );
508 $n = scalar(@r);
509 $k = (($jj+1)*8/20)*20*$n/8; # 8 aesencs per these 20 rounds
510 @r[$k%$n].='&$aesenc();' if ($jj==$k/$n);
511 $jj++;
512 return @r;
513}
514
515sub body_40_59 () {
516 use integer;
517 my ($k,$n);
518 my @r=(
519 '($a,$b,$c,$d,$e)=@V;'.
520 '&mov (@T[1],$c);',
521 '&xor ($c,$d);',
522 '&add ($e,eval(4*($j++&15))."(%rsp)");', # X[]+K xfer
523 '&and (@T[1],$d);',
524 '&and (@T[0],$c);', # ($b&($c^$d))
525 '&$_ror ($b,7);', # $b>>>2
526 '&add ($e,@T[1]);',
527 '&mov (@T[1],$a);', # $b in next round
528 '&$_rol ($a,5);',
529 '&add ($e,@T[0]);',
530 '&xor ($c,$d);', # restore $c
531 '&add ($e,$a);' .'unshift(@V,pop(@V)); unshift(@T,pop(@T));'
532 );
533 $n = scalar(@r);
534 $k=(($jj+1)*12/20)*20*$n/12; # 12 aesencs per these 20 rounds
535 @r[$k%$n].='&$aesenc();' if ($jj==$k/$n);
536 $jj++;
537 return @r;
538}
539$code.=<<___;
540.align 16
541.Loop_ssse3:
542___
543 &Xupdate_ssse3_16_31(\&body_00_19);
544 &Xupdate_ssse3_16_31(\&body_00_19);
545 &Xupdate_ssse3_16_31(\&body_00_19);
546 &Xupdate_ssse3_16_31(\&body_00_19);
547 &Xupdate_ssse3_32_79(\&body_00_19);
548 &Xupdate_ssse3_32_79(\&body_20_39);
549 &Xupdate_ssse3_32_79(\&body_20_39);
550 &Xupdate_ssse3_32_79(\&body_20_39);
551 &Xupdate_ssse3_32_79(\&body_20_39);
552 &Xupdate_ssse3_32_79(\&body_20_39);
553 &Xupdate_ssse3_32_79(\&body_40_59);
554 &Xupdate_ssse3_32_79(\&body_40_59);
555 &Xupdate_ssse3_32_79(\&body_40_59);
556 &Xupdate_ssse3_32_79(\&body_40_59);
557 &Xupdate_ssse3_32_79(\&body_40_59);
558 &Xupdate_ssse3_32_79(\&body_20_39);
559 &Xuplast_ssse3_80(\&body_20_39); # can jump to "done"
560
561 $saved_j=$j; @saved_V=@V;
562 $saved_r=$r; @saved_rndkey=@rndkey;
563
564 &Xloop_ssse3(\&body_20_39);
565 &Xloop_ssse3(\&body_20_39);
566 &Xloop_ssse3(\&body_20_39);
567
568$code.=<<___;
569 movups $iv,48($out,$in0) # write output
570 lea 64($in0),$in0
571
572 add 0($ctx),$A # update context
573 add 4($ctx),@T[0]
574 add 8($ctx),$C
575 add 12($ctx),$D
576 mov $A,0($ctx)
577 add 16($ctx),$E
578 mov @T[0],4($ctx)
579 mov @T[0],$B # magic seed
580 mov $C,8($ctx)
581 mov $D,12($ctx)
582 mov $E,16($ctx)
583 jmp .Loop_ssse3
584
585.align 16
586.Ldone_ssse3:
587___
588 $jj=$j=$saved_j; @V=@saved_V;
589 $r=$saved_r; @rndkey=@saved_rndkey;
590
591 &Xtail_ssse3(\&body_20_39);
592 &Xtail_ssse3(\&body_20_39);
593 &Xtail_ssse3(\&body_20_39);
594
595$code.=<<___;
596 movups $iv,48($out,$in0) # write output
597 mov 88(%rsp),$ivp # restore $ivp
598
599 add 0($ctx),$A # update context
600 add 4($ctx),@T[0]
601 add 8($ctx),$C
602 mov $A,0($ctx)
603 add 12($ctx),$D
604 mov @T[0],4($ctx)
605 add 16($ctx),$E
606 mov $C,8($ctx)
607 mov $D,12($ctx)
608 mov $E,16($ctx)
609 movups $iv,($ivp) # write IV
610___
611$code.=<<___ if ($win64);
612 movaps 96+0(%rsp),%xmm6
613 movaps 96+16(%rsp),%xmm7
614 movaps 96+32(%rsp),%xmm8
615 movaps 96+48(%rsp),%xmm9
616 movaps 96+64(%rsp),%xmm10
617 movaps 96+80(%rsp),%xmm11
618 movaps 96+96(%rsp),%xmm12
619 movaps 96+112(%rsp),%xmm13
620 movaps 96+128(%rsp),%xmm14
621 movaps 96+144(%rsp),%xmm15
622___
623$code.=<<___;
624 lea `104+($win64?10*16:0)`(%rsp),%rsi
625 mov 0(%rsi),%r15
626 mov 8(%rsi),%r14
627 mov 16(%rsi),%r13
628 mov 24(%rsi),%r12
629 mov 32(%rsi),%rbp
630 mov 40(%rsi),%rbx
631 lea 48(%rsi),%rsp
632.Lepilogue_ssse3:
633 ret
634.size aesni_cbc_sha1_enc_ssse3,.-aesni_cbc_sha1_enc_ssse3
635___
636
637$j=$jj=$r=$sn=0;
638
639if ($avx) {
640my ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
641
642my $Xi=4;
643my @X=map("%xmm$_",(4..7,0..3));
644my @Tx=map("%xmm$_",(8..10));
645my @V=($A,$B,$C,$D,$E)=("%eax","%ebx","%ecx","%edx","%ebp"); # size optimization
646my @T=("%esi","%edi");
647
648my $_rol=sub { &shld(@_[0],@_) };
649my $_ror=sub { &shrd(@_[0],@_) };
650
651$code.=<<___;
652.type aesni_cbc_sha1_enc_avx,\@function,6
653.align 16
654aesni_cbc_sha1_enc_avx:
655 _CET_ENDBR
656 mov `($win64?56:8)`(%rsp),$inp # load 7th argument
657 #shr \$6,$len # debugging artefact
658 #jz .Lepilogue_avx # debugging artefact
659 push %rbx
660 push %rbp
661 push %r12
662 push %r13
663 push %r14
664 push %r15
665 lea `-104-($win64?10*16:0)`(%rsp),%rsp
666 #mov $in0,$inp # debugging artefact
667 #lea 64(%rsp),$ctx # debugging artefact
668___
669$code.=<<___ if ($win64);
670 movaps %xmm6,96+0(%rsp)
671 movaps %xmm7,96+16(%rsp)
672 movaps %xmm8,96+32(%rsp)
673 movaps %xmm9,96+48(%rsp)
674 movaps %xmm10,96+64(%rsp)
675 movaps %xmm11,96+80(%rsp)
676 movaps %xmm12,96+96(%rsp)
677 movaps %xmm13,96+112(%rsp)
678 movaps %xmm14,96+128(%rsp)
679 movaps %xmm15,96+144(%rsp)
680.Lprologue_avx:
681___
682$code.=<<___;
683 vzeroall
684 mov $in0,%r12 # reassign arguments
685 mov $out,%r13
686 mov $len,%r14
687 mov $key,%r15
688 vmovdqu ($ivp),$iv # load IV
689 mov $ivp,88(%rsp) # save $ivp
690___
691my ($in0,$out,$len,$key)=map("%r$_",(12..15)); # reassign arguments
692my $rounds="${ivp}d";
693$code.=<<___;
694 shl \$6,$len
695 sub $in0,$out
696 mov 240($key),$rounds
697 add \$112,$key # size optimization
698 add $inp,$len # end of input
699
700 lea K_XX_XX(%rip),$K_XX_XX
701 mov 0($ctx),$A # load context
702 mov 4($ctx),$B
703 mov 8($ctx),$C
704 mov 12($ctx),$D
705 mov $B,@T[0] # magic seed
706 mov 16($ctx),$E
707
708 vmovdqa 64($K_XX_XX),@X[2] # pbswap mask
709 vmovdqa 0($K_XX_XX),@Tx[1] # K_00_19
710 vmovdqu 0($inp),@X[-4&7] # load input to %xmm[0-3]
711 vmovdqu 16($inp),@X[-3&7]
712 vmovdqu 32($inp),@X[-2&7]
713 vmovdqu 48($inp),@X[-1&7]
714 vpshufb @X[2],@X[-4&7],@X[-4&7] # byte swap
715 add \$64,$inp
716 vpshufb @X[2],@X[-3&7],@X[-3&7]
717 vpshufb @X[2],@X[-2&7],@X[-2&7]
718 vpshufb @X[2],@X[-1&7],@X[-1&7]
719 vpaddd @Tx[1],@X[-4&7],@X[0] # add K_00_19
720 vpaddd @Tx[1],@X[-3&7],@X[1]
721 vpaddd @Tx[1],@X[-2&7],@X[2]
722 vmovdqa @X[0],0(%rsp) # X[]+K xfer to IALU
723 vmovdqa @X[1],16(%rsp)
724 vmovdqa @X[2],32(%rsp)
725 vmovups -112($key),$rndkey0 # $key[0]
726 vmovups 16-112($key),$rndkey[0] # forward reference
727 jmp .Loop_avx
728___
729
730my $aesenc=sub {
731 use integer;
732 my ($n,$k)=($r/10,$r%10);
733 if ($k==0) {
734 $code.=<<___;
735 vmovups `16*$n`($in0),$in # load input
736 vxorps $rndkey0,$in,$in
737___
738 $code.=<<___ if ($n);
739 vmovups $iv,`16*($n-1)`($out,$in0) # write output
740___
741 $code.=<<___;
742 vxorps $in,$iv,$iv
743 vaesenc $rndkey[0],$iv,$iv
744 vmovups `32+16*$k-112`($key),$rndkey[1]
745___
746 } elsif ($k==9) {
747 $sn++;
748 $code.=<<___;
749 cmp \$11,$rounds
750 jb .Lvaesenclast$sn
751 vaesenc $rndkey[0],$iv,$iv
752 vmovups `32+16*($k+0)-112`($key),$rndkey[1]
753 vaesenc $rndkey[1],$iv,$iv
754 vmovups `32+16*($k+1)-112`($key),$rndkey[0]
755 je .Lvaesenclast$sn
756 vaesenc $rndkey[0],$iv,$iv
757 vmovups `32+16*($k+2)-112`($key),$rndkey[1]
758 vaesenc $rndkey[1],$iv,$iv
759 vmovups `32+16*($k+3)-112`($key),$rndkey[0]
760.Lvaesenclast$sn:
761 vaesenclast $rndkey[0],$iv,$iv
762 vmovups 16-112($key),$rndkey[1] # forward reference
763___
764 } else {
765 $code.=<<___;
766 vaesenc $rndkey[0],$iv,$iv
767 vmovups `32+16*$k-112`($key),$rndkey[1]
768___
769 }
770 $r++; unshift(@rndkey,pop(@rndkey));
771};
772
773sub Xupdate_avx_16_31() # recall that $Xi starts with 4
774{ use integer;
775 my $body = shift;
776 my @insns = (&$body,&$body,&$body,&$body); # 40 instructions
777 my ($a,$b,$c,$d,$e);
778
779 eval(shift(@insns));
780 eval(shift(@insns));
781 &vpalignr(@X[0],@X[-3&7],@X[-4&7],8); # compose "X[-14]" in "X[0]"
782 eval(shift(@insns));
783 eval(shift(@insns));
784
785 &vpaddd (@Tx[1],@Tx[1],@X[-1&7]);
786 eval(shift(@insns));
787 eval(shift(@insns));
788 &vpsrldq(@Tx[0],@X[-1&7],4); # "X[-3]", 3 dwords
789 eval(shift(@insns));
790 eval(shift(@insns));
791 &vpxor (@X[0],@X[0],@X[-4&7]); # "X[0]"^="X[-16]"
792 eval(shift(@insns));
793 eval(shift(@insns));
794
795 &vpxor (@Tx[0],@Tx[0],@X[-2&7]); # "X[-3]"^"X[-8]"
796 eval(shift(@insns));
797 eval(shift(@insns));
798 eval(shift(@insns));
799 eval(shift(@insns));
800
801 &vpxor (@X[0],@X[0],@Tx[0]); # "X[0]"^="X[-3]"^"X[-8]"
802 eval(shift(@insns));
803 eval(shift(@insns));
804 &vmovdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
805 eval(shift(@insns));
806 eval(shift(@insns));
807
808 &vpsrld (@Tx[0],@X[0],31);
809 eval(shift(@insns));
810 eval(shift(@insns));
811 eval(shift(@insns));
812 eval(shift(@insns));
813
814 &vpslldq(@Tx[2],@X[0],12); # "X[0]"<<96, extract one dword
815 &vpaddd (@X[0],@X[0],@X[0]);
816 eval(shift(@insns));
817 eval(shift(@insns));
818 eval(shift(@insns));
819 eval(shift(@insns));
820
821 &vpsrld (@Tx[1],@Tx[2],30);
822 &vpor (@X[0],@X[0],@Tx[0]); # "X[0]"<<<=1
823 eval(shift(@insns));
824 eval(shift(@insns));
825 eval(shift(@insns));
826 eval(shift(@insns));
827
828 &vpslld (@Tx[2],@Tx[2],2);
829 &vpxor (@X[0],@X[0],@Tx[1]);
830 eval(shift(@insns));
831 eval(shift(@insns));
832 eval(shift(@insns));
833 eval(shift(@insns));
834
835 &vpxor (@X[0],@X[0],@Tx[2]); # "X[0]"^=("X[0]">>96)<<<2
836 eval(shift(@insns));
837 eval(shift(@insns));
838 &vmovdqa (@Tx[2],eval(16*(($Xi)/5))."($K_XX_XX)"); # K_XX_XX
839 eval(shift(@insns));
840 eval(shift(@insns));
841
842
843 foreach (@insns) { eval; } # remaining instructions [if any]
844
845 $Xi++; push(@X,shift(@X)); # "rotate" X[]
846 push(@Tx,shift(@Tx));
847}
848
849sub Xupdate_avx_32_79()
850{ use integer;
851 my $body = shift;
852 my @insns = (&$body,&$body,&$body,&$body); # 32 to 48 instructions
853 my ($a,$b,$c,$d,$e);
854
855 &vpalignr(@Tx[0],@X[-1&7],@X[-2&7],8); # compose "X[-6]"
856 &vpxor (@X[0],@X[0],@X[-4&7]); # "X[0]"="X[-32]"^"X[-16]"
857 eval(shift(@insns)); # body_20_39
858 eval(shift(@insns));
859 eval(shift(@insns));
860 eval(shift(@insns)); # rol
861
862 &vpxor (@X[0],@X[0],@X[-7&7]); # "X[0]"^="X[-28]"
863 eval(shift(@insns));
864 eval(shift(@insns)) if (@insns[0] !~ /&ro[rl]/);
865 if ($Xi%5) {
866 &vmovdqa (@Tx[2],@Tx[1]);# "perpetuate" K_XX_XX...
867 } else { # ... or load next one
868 &vmovdqa (@Tx[2],eval(16*($Xi/5))."($K_XX_XX)");
869 }
870 &vpaddd (@Tx[1],@Tx[1],@X[-1&7]);
871 eval(shift(@insns)); # ror
872 eval(shift(@insns));
873
874 &vpxor (@X[0],@X[0],@Tx[0]); # "X[0]"^="X[-6]"
875 eval(shift(@insns)); # body_20_39
876 eval(shift(@insns));
877 eval(shift(@insns));
878 eval(shift(@insns)); # rol
879
880 &vpsrld (@Tx[0],@X[0],30);
881 &vmovdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
882 eval(shift(@insns));
883 eval(shift(@insns));
884 eval(shift(@insns)); # ror
885 eval(shift(@insns));
886
887 &vpslld (@X[0],@X[0],2);
888 eval(shift(@insns)); # body_20_39
889 eval(shift(@insns));
890 eval(shift(@insns));
891 eval(shift(@insns)); # rol
892 eval(shift(@insns));
893 eval(shift(@insns));
894 eval(shift(@insns)); # ror
895 eval(shift(@insns));
896
897 &vpor (@X[0],@X[0],@Tx[0]); # "X[0]"<<<=2
898 eval(shift(@insns)); # body_20_39
899 eval(shift(@insns));
900 &vmovdqa (@Tx[1],@X[0]) if ($Xi<19);
901 eval(shift(@insns));
902 eval(shift(@insns)); # rol
903 eval(shift(@insns));
904 eval(shift(@insns));
905 eval(shift(@insns)); # rol
906 eval(shift(@insns));
907
908 foreach (@insns) { eval; } # remaining instructions
909
910 $Xi++; push(@X,shift(@X)); # "rotate" X[]
911 push(@Tx,shift(@Tx));
912}
913
914sub Xuplast_avx_80()
915{ use integer;
916 my $body = shift;
917 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
918 my ($a,$b,$c,$d,$e);
919
920 eval(shift(@insns));
921 &vpaddd (@Tx[1],@Tx[1],@X[-1&7]);
922 eval(shift(@insns));
923 eval(shift(@insns));
924 eval(shift(@insns));
925 eval(shift(@insns));
926
927 &movdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer IALU
928
929 foreach (@insns) { eval; } # remaining instructions
930
931 &cmp ($inp,$len);
932 &je (".Ldone_avx");
933
934 unshift(@Tx,pop(@Tx));
935
936 &vmovdqa(@X[2],"64($K_XX_XX)"); # pbswap mask
937 &vmovdqa(@Tx[1],"0($K_XX_XX)"); # K_00_19
938 &vmovdqu(@X[-4&7],"0($inp)"); # load input
939 &vmovdqu(@X[-3&7],"16($inp)");
940 &vmovdqu(@X[-2&7],"32($inp)");
941 &vmovdqu(@X[-1&7],"48($inp)");
942 &vpshufb(@X[-4&7],@X[-4&7],@X[2]); # byte swap
943 &add ($inp,64);
944
945 $Xi=0;
946}
947
948sub Xloop_avx()
949{ use integer;
950 my $body = shift;
951 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
952 my ($a,$b,$c,$d,$e);
953
954 eval(shift(@insns));
955 eval(shift(@insns));
956 &vpshufb(@X[($Xi-3)&7],@X[($Xi-3)&7],@X[2]);
957 eval(shift(@insns));
958 eval(shift(@insns));
959 &vpaddd (@X[$Xi&7],@X[($Xi-4)&7],@Tx[1]);
960 eval(shift(@insns));
961 eval(shift(@insns));
962 eval(shift(@insns));
963 eval(shift(@insns));
964 &vmovdqa(eval(16*$Xi)."(%rsp)",@X[$Xi&7]); # X[]+K xfer to IALU
965 eval(shift(@insns));
966 eval(shift(@insns));
967
968 foreach (@insns) { eval; }
969 $Xi++;
970}
971
972sub Xtail_avx()
973{ use integer;
974 my $body = shift;
975 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
976 my ($a,$b,$c,$d,$e);
977
978 foreach (@insns) { eval; }
979}
980
981$code.=<<___;
982.align 16
983.Loop_avx:
984___
985 &Xupdate_avx_16_31(\&body_00_19);
986 &Xupdate_avx_16_31(\&body_00_19);
987 &Xupdate_avx_16_31(\&body_00_19);
988 &Xupdate_avx_16_31(\&body_00_19);
989 &Xupdate_avx_32_79(\&body_00_19);
990 &Xupdate_avx_32_79(\&body_20_39);
991 &Xupdate_avx_32_79(\&body_20_39);
992 &Xupdate_avx_32_79(\&body_20_39);
993 &Xupdate_avx_32_79(\&body_20_39);
994 &Xupdate_avx_32_79(\&body_20_39);
995 &Xupdate_avx_32_79(\&body_40_59);
996 &Xupdate_avx_32_79(\&body_40_59);
997 &Xupdate_avx_32_79(\&body_40_59);
998 &Xupdate_avx_32_79(\&body_40_59);
999 &Xupdate_avx_32_79(\&body_40_59);
1000 &Xupdate_avx_32_79(\&body_20_39);
1001 &Xuplast_avx_80(\&body_20_39); # can jump to "done"
1002
1003 $saved_j=$j; @saved_V=@V;
1004 $saved_r=$r; @saved_rndkey=@rndkey;
1005
1006 &Xloop_avx(\&body_20_39);
1007 &Xloop_avx(\&body_20_39);
1008 &Xloop_avx(\&body_20_39);
1009
1010$code.=<<___;
1011 vmovups $iv,48($out,$in0) # write output
1012 lea 64($in0),$in0
1013
1014 add 0($ctx),$A # update context
1015 add 4($ctx),@T[0]
1016 add 8($ctx),$C
1017 add 12($ctx),$D
1018 mov $A,0($ctx)
1019 add 16($ctx),$E
1020 mov @T[0],4($ctx)
1021 mov @T[0],$B # magic seed
1022 mov $C,8($ctx)
1023 mov $D,12($ctx)
1024 mov $E,16($ctx)
1025 jmp .Loop_avx
1026
1027.align 16
1028.Ldone_avx:
1029___
1030 $jj=$j=$saved_j; @V=@saved_V;
1031 $r=$saved_r; @rndkey=@saved_rndkey;
1032
1033 &Xtail_avx(\&body_20_39);
1034 &Xtail_avx(\&body_20_39);
1035 &Xtail_avx(\&body_20_39);
1036
1037$code.=<<___;
1038 vmovups $iv,48($out,$in0) # write output
1039 mov 88(%rsp),$ivp # restore $ivp
1040
1041 add 0($ctx),$A # update context
1042 add 4($ctx),@T[0]
1043 add 8($ctx),$C
1044 mov $A,0($ctx)
1045 add 12($ctx),$D
1046 mov @T[0],4($ctx)
1047 add 16($ctx),$E
1048 mov $C,8($ctx)
1049 mov $D,12($ctx)
1050 mov $E,16($ctx)
1051 vmovups $iv,($ivp) # write IV
1052 vzeroall
1053___
1054$code.=<<___ if ($win64);
1055 movaps 96+0(%rsp),%xmm6
1056 movaps 96+16(%rsp),%xmm7
1057 movaps 96+32(%rsp),%xmm8
1058 movaps 96+48(%rsp),%xmm9
1059 movaps 96+64(%rsp),%xmm10
1060 movaps 96+80(%rsp),%xmm11
1061 movaps 96+96(%rsp),%xmm12
1062 movaps 96+112(%rsp),%xmm13
1063 movaps 96+128(%rsp),%xmm14
1064 movaps 96+144(%rsp),%xmm15
1065___
1066$code.=<<___;
1067 lea `104+($win64?10*16:0)`(%rsp),%rsi
1068 mov 0(%rsi),%r15
1069 mov 8(%rsi),%r14
1070 mov 16(%rsi),%r13
1071 mov 24(%rsi),%r12
1072 mov 32(%rsi),%rbp
1073 mov 40(%rsi),%rbx
1074 lea 48(%rsi),%rsp
1075.Lepilogue_avx:
1076 ret
1077.size aesni_cbc_sha1_enc_avx,.-aesni_cbc_sha1_enc_avx
1078___
1079}
1080$code.=<<___;
1081.section .rodata
1082.align 64
1083K_XX_XX:
1084.long 0x5a827999,0x5a827999,0x5a827999,0x5a827999 # K_00_19
1085.long 0x6ed9eba1,0x6ed9eba1,0x6ed9eba1,0x6ed9eba1 # K_20_39
1086.long 0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc # K_40_59
1087.long 0xca62c1d6,0xca62c1d6,0xca62c1d6,0xca62c1d6 # K_60_79
1088.long 0x00010203,0x04050607,0x08090a0b,0x0c0d0e0f # pbswap mask
1089.align 64
1090.text
1091___
1092
1093# EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
1094# CONTEXT *context,DISPATCHER_CONTEXT *disp)
1095if ($win64) {
1096$rec="%rcx";
1097$frame="%rdx";
1098$context="%r8";
1099$disp="%r9";
1100
1101$code.=<<___;
1102.extern __imp_RtlVirtualUnwind
1103.type ssse3_handler,\@abi-omnipotent
1104.align 16
1105ssse3_handler:
1106 _CET_ENDBR
1107 push %rsi
1108 push %rdi
1109 push %rbx
1110 push %rbp
1111 push %r12
1112 push %r13
1113 push %r14
1114 push %r15
1115 pushfq
1116 sub \$64,%rsp
1117
1118 mov 120($context),%rax # pull context->Rax
1119 mov 248($context),%rbx # pull context->Rip
1120
1121 mov 8($disp),%rsi # disp->ImageBase
1122 mov 56($disp),%r11 # disp->HandlerData
1123
1124 mov 0(%r11),%r10d # HandlerData[0]
1125 lea (%rsi,%r10),%r10 # prologue label
1126 cmp %r10,%rbx # context->Rip<prologue label
1127 jb .Lcommon_seh_tail
1128
1129 mov 152($context),%rax # pull context->Rsp
1130
1131 mov 4(%r11),%r10d # HandlerData[1]
1132 lea (%rsi,%r10),%r10 # epilogue label
1133 cmp %r10,%rbx # context->Rip>=epilogue label
1134 jae .Lcommon_seh_tail
1135
1136 lea 96(%rax),%rsi
1137 lea 512($context),%rdi # &context.Xmm6
1138 mov \$20,%ecx
1139 .long 0xa548f3fc # cld; rep movsq
1140 lea `104+10*16`(%rax),%rax # adjust stack pointer
1141
1142 mov 0(%rax),%r15
1143 mov 8(%rax),%r14
1144 mov 16(%rax),%r13
1145 mov 24(%rax),%r12
1146 mov 32(%rax),%rbp
1147 mov 40(%rax),%rbx
1148 lea 48(%rax),%rax
1149 mov %rbx,144($context) # restore context->Rbx
1150 mov %rbp,160($context) # restore context->Rbp
1151 mov %r12,216($context) # restore context->R12
1152 mov %r13,224($context) # restore context->R13
1153 mov %r14,232($context) # restore context->R14
1154 mov %r15,240($context) # restore context->R15
1155
1156.Lcommon_seh_tail:
1157 mov 8(%rax),%rdi
1158 mov 16(%rax),%rsi
1159 mov %rax,152($context) # restore context->Rsp
1160 mov %rsi,168($context) # restore context->Rsi
1161 mov %rdi,176($context) # restore context->Rdi
1162
1163 mov 40($disp),%rdi # disp->ContextRecord
1164 mov $context,%rsi # context
1165 mov \$154,%ecx # sizeof(CONTEXT)
1166 .long 0xa548f3fc # cld; rep movsq
1167
1168 mov $disp,%rsi
1169 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
1170 mov 8(%rsi),%rdx # arg2, disp->ImageBase
1171 mov 0(%rsi),%r8 # arg3, disp->ControlPc
1172 mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
1173 mov 40(%rsi),%r10 # disp->ContextRecord
1174 lea 56(%rsi),%r11 # &disp->HandlerData
1175 lea 24(%rsi),%r12 # &disp->EstablisherFrame
1176 mov %r10,32(%rsp) # arg5
1177 mov %r11,40(%rsp) # arg6
1178 mov %r12,48(%rsp) # arg7
1179 mov %rcx,56(%rsp) # arg8, (NULL)
1180 call *__imp_RtlVirtualUnwind(%rip)
1181
1182 mov \$1,%eax # ExceptionContinueSearch
1183 add \$64,%rsp
1184 popfq
1185 pop %r15
1186 pop %r14
1187 pop %r13
1188 pop %r12
1189 pop %rbp
1190 pop %rbx
1191 pop %rdi
1192 pop %rsi
1193 ret
1194.size ssse3_handler,.-ssse3_handler
1195
1196.section .pdata
1197.align 4
1198 .rva .LSEH_begin_aesni_cbc_sha1_enc_ssse3
1199 .rva .LSEH_end_aesni_cbc_sha1_enc_ssse3
1200 .rva .LSEH_info_aesni_cbc_sha1_enc_ssse3
1201___
1202$code.=<<___ if ($avx);
1203 .rva .LSEH_begin_aesni_cbc_sha1_enc_avx
1204 .rva .LSEH_end_aesni_cbc_sha1_enc_avx
1205 .rva .LSEH_info_aesni_cbc_sha1_enc_avx
1206___
1207$code.=<<___;
1208.section .xdata
1209.align 8
1210.LSEH_info_aesni_cbc_sha1_enc_ssse3:
1211 .byte 9,0,0,0
1212 .rva ssse3_handler
1213 .rva .Lprologue_ssse3,.Lepilogue_ssse3 # HandlerData[]
1214___
1215$code.=<<___ if ($avx);
1216.LSEH_info_aesni_cbc_sha1_enc_avx:
1217 .byte 9,0,0,0
1218 .rva ssse3_handler
1219 .rva .Lprologue_avx,.Lepilogue_avx # HandlerData[]
1220___
1221}
1222
1223####################################################################
1224sub rex {
1225 local *opcode=shift;
1226 my ($dst,$src)=@_;
1227 my $rex=0;
1228
1229 $rex|=0x04 if($dst>=8);
1230 $rex|=0x01 if($src>=8);
1231 push @opcode,$rex|0x40 if($rex);
1232}
1233
1234$code =~ s/\`([^\`]*)\`/eval($1)/gem;
1235
1236print $code;
1237close STDOUT;
diff --git a/src/lib/libcrypto/arch/amd64/Makefile.inc b/src/lib/libcrypto/arch/amd64/Makefile.inc
index 878965f566..ab10e43a14 100644
--- a/src/lib/libcrypto/arch/amd64/Makefile.inc
+++ b/src/lib/libcrypto/arch/amd64/Makefile.inc
@@ -1,4 +1,4 @@
1# $OpenBSD: Makefile.inc,v 1.14 2024/03/27 11:12:08 jsing Exp $ 1# $OpenBSD: Makefile.inc,v 1.15 2024/03/27 12:42:30 jsing Exp $
2 2
3# amd64-specific libcrypto build rules 3# amd64-specific libcrypto build rules
4 4
@@ -13,7 +13,6 @@ SSLASM+= aes bsaes-x86_64
13CFLAGS+= -DVPAES_ASM 13CFLAGS+= -DVPAES_ASM
14SSLASM+= aes vpaes-x86_64 14SSLASM+= aes vpaes-x86_64
15SSLASM+= aes aesni-x86_64 15SSLASM+= aes aesni-x86_64
16SSLASM+= aes aesni-sha1-x86_64
17# bn 16# bn
18CFLAGS+= -DOPENSSL_IA32_SSE2 17CFLAGS+= -DOPENSSL_IA32_SSE2
19CFLAGS+= -DRSA_ASM 18CFLAGS+= -DRSA_ASM
@@ -49,9 +48,7 @@ SSLASM+= md5 md5-x86_64
49CFLAGS+= -DGHASH_ASM 48CFLAGS+= -DGHASH_ASM
50SSLASM+= modes ghash-x86_64 49SSLASM+= modes ghash-x86_64
51# rc4 50# rc4
52CFLAGS+= -DRC4_MD5_ASM
53SSLASM+= rc4 rc4-x86_64 51SSLASM+= rc4 rc4-x86_64
54SSLASM+= rc4 rc4-md5-x86_64
55# ripemd 52# ripemd
56# sha 53# sha
57CFLAGS+= -DSHA1_ASM 54CFLAGS+= -DSHA1_ASM
diff --git a/src/lib/libcrypto/rc4/asm/rc4-md5-x86_64.pl b/src/lib/libcrypto/rc4/asm/rc4-md5-x86_64.pl
deleted file mode 100644
index e5e8aa08a1..0000000000
--- a/src/lib/libcrypto/rc4/asm/rc4-md5-x86_64.pl
+++ /dev/null
@@ -1,515 +0,0 @@
1#!/usr/bin/env perl
2#
3# ====================================================================
4# Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
5# project. The module is, however, dual licensed under OpenSSL and
6# CRYPTOGAMS licenses depending on where you obtain it. For further
7# details see http://www.openssl.org/~appro/cryptogams/.
8# ====================================================================
9
10# June 2011
11#
12# This is RC4+MD5 "stitch" implementation. The idea, as spelled in
13# http://download.intel.com/design/intarch/papers/323686.pdf, is that
14# since both algorithms exhibit instruction-level parallelism, ILP,
15# below theoretical maximum, interleaving them would allow to utilize
16# processor resources better and achieve better performance. RC4
17# instruction sequence is virtually identical to rc4-x86_64.pl, which
18# is heavily based on submission by Maxim Perminov, Maxim Locktyukhin
19# and Jim Guilford of Intel. MD5 is fresh implementation aiming to
20# minimize register usage, which was used as "main thread" with RC4
21# weaved into it, one RC4 round per one MD5 round. In addition to the
22# stiched subroutine the script can generate standalone replacement
23# md5_block_asm_data_order and RC4. Below are performance numbers in
24# cycles per processed byte, less is better, for these the standalone
25# subroutines, sum of them, and stitched one:
26#
27# RC4 MD5 RC4+MD5 stitch gain
28# Opteron 6.5(*) 5.4 11.9 7.0 +70%(*)
29# Core2 6.5 5.8 12.3 7.7 +60%
30# Westmere 4.3 5.2 9.5 7.0 +36%
31# Sandy Bridge 4.2 5.5 9.7 6.8 +43%
32# Atom 9.3 6.5 15.8 11.1 +42%
33#
34# (*) rc4-x86_64.pl delivers 5.3 on Opteron, so real improvement
35# is +53%...
36
37my ($rc4,$md5)=(1,1); # what to generate?
38my $D="#" if (!$md5); # if set to "#", MD5 is stitched into RC4(),
39 # but its result is discarded. Idea here is
40 # to be able to use 'openssl speed rc4' for
41 # benchmarking the stitched subroutine...
42
43my $flavour = shift;
44my $output = shift;
45if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
46
47$0 =~ m/(.*[\/\\])[^\/\\]+$/; my $dir=$1; my $xlate;
48( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
49( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
50die "can't locate x86_64-xlate.pl";
51
52open OUT,"| \"$^X\" $xlate $flavour $output";
53*STDOUT=*OUT;
54
55my ($dat,$in0,$out,$ctx,$inp,$len, $func,$nargs);
56
57if ($rc4 && !$md5) {
58 ($dat,$len,$in0,$out) = ("%rdi","%rsi","%rdx","%rcx");
59 $func="RC4"; $nargs=4;
60} elsif ($md5 && !$rc4) {
61 ($ctx,$inp,$len) = ("%rdi","%rsi","%rdx");
62 $func="md5_block_asm_data_order"; $nargs=3;
63} else {
64 ($dat,$in0,$out,$ctx,$inp,$len) = ("%rdi","%rsi","%rdx","%rcx","%r8","%r9");
65 $func="rc4_md5_enc"; $nargs=6;
66 # void rc4_md5_enc(
67 # RC4_KEY *key, #
68 # const void *in0, # RC4 input
69 # void *out, # RC4 output
70 # MD5_CTX *ctx, #
71 # const void *inp, # MD5 input
72 # size_t len); # number of 64-byte blocks
73}
74
75my @K=( 0xd76aa478,0xe8c7b756,0x242070db,0xc1bdceee,
76 0xf57c0faf,0x4787c62a,0xa8304613,0xfd469501,
77 0x698098d8,0x8b44f7af,0xffff5bb1,0x895cd7be,
78 0x6b901122,0xfd987193,0xa679438e,0x49b40821,
79
80 0xf61e2562,0xc040b340,0x265e5a51,0xe9b6c7aa,
81 0xd62f105d,0x02441453,0xd8a1e681,0xe7d3fbc8,
82 0x21e1cde6,0xc33707d6,0xf4d50d87,0x455a14ed,
83 0xa9e3e905,0xfcefa3f8,0x676f02d9,0x8d2a4c8a,
84
85 0xfffa3942,0x8771f681,0x6d9d6122,0xfde5380c,
86 0xa4beea44,0x4bdecfa9,0xf6bb4b60,0xbebfbc70,
87 0x289b7ec6,0xeaa127fa,0xd4ef3085,0x04881d05,
88 0xd9d4d039,0xe6db99e5,0x1fa27cf8,0xc4ac5665,
89
90 0xf4292244,0x432aff97,0xab9423a7,0xfc93a039,
91 0x655b59c3,0x8f0ccc92,0xffeff47d,0x85845dd1,
92 0x6fa87e4f,0xfe2ce6e0,0xa3014314,0x4e0811a1,
93 0xf7537e82,0xbd3af235,0x2ad7d2bb,0xeb86d391 );
94
95my @V=("%r8d","%r9d","%r10d","%r11d"); # MD5 registers
96my $tmp="%r12d";
97
98my @XX=("%rbp","%rsi"); # RC4 registers
99my @TX=("%rax","%rbx");
100my $YY="%rcx";
101my $TY="%rdx";
102
103my $MOD=32; # 16, 32 or 64
104
105$code.=<<___;
106.text
107.align 16
108
109.globl $func
110.type $func,\@function,$nargs
111$func:
112 _CET_ENDBR
113 cmp \$0,$len
114 je .Labort
115 push %rbx
116 push %rbp
117 push %r12
118 push %r13
119 push %r14
120 push %r15
121 sub \$40,%rsp
122.Lbody:
123___
124if ($rc4) {
125$code.=<<___;
126$D#md5# mov $ctx,%r11 # reassign arguments
127 mov $len,%r12
128 mov $in0,%r13
129 mov $out,%r14
130$D#md5# mov $inp,%r15
131___
132 $ctx="%r11" if ($md5); # reassign arguments
133 $len="%r12";
134 $in0="%r13";
135 $out="%r14";
136 $inp="%r15" if ($md5);
137 $inp=$in0 if (!$md5);
138$code.=<<___;
139 xor $XX[0],$XX[0]
140 xor $YY,$YY
141
142 lea 8($dat),$dat
143 mov -8($dat),$XX[0]#b
144 mov -4($dat),$YY#b
145
146 inc $XX[0]#b
147 sub $in0,$out
148 movl ($dat,$XX[0],4),$TX[0]#d
149___
150$code.=<<___ if (!$md5);
151 xor $TX[1],$TX[1]
152 test \$-128,$len
153 jz .Loop1
154 sub $XX[0],$TX[1]
155 and \$`$MOD-1`,$TX[1]
156 jz .Loop${MOD}_is_hot
157 sub $TX[1],$len
158.Loop${MOD}_warmup:
159 add $TX[0]#b,$YY#b
160 movl ($dat,$YY,4),$TY#d
161 movl $TX[0]#d,($dat,$YY,4)
162 movl $TY#d,($dat,$XX[0],4)
163 add $TY#b,$TX[0]#b
164 inc $XX[0]#b
165 movl ($dat,$TX[0],4),$TY#d
166 movl ($dat,$XX[0],4),$TX[0]#d
167 xorb ($in0),$TY#b
168 movb $TY#b,($out,$in0)
169 lea 1($in0),$in0
170 dec $TX[1]
171 jnz .Loop${MOD}_warmup
172
173 mov $YY,$TX[1]
174 xor $YY,$YY
175 mov $TX[1]#b,$YY#b
176
177.Loop${MOD}_is_hot:
178 mov $len,32(%rsp) # save original $len
179 shr \$6,$len # number of 64-byte blocks
180___
181 if ($D && !$md5) { # stitch in dummy MD5
182 $md5=1;
183 $ctx="%r11";
184 $inp="%r15";
185 $code.=<<___;
186 mov %rsp,$ctx
187 mov $in0,$inp
188___
189 }
190}
191$code.=<<___;
192#rc4# add $TX[0]#b,$YY#b
193#rc4# lea ($dat,$XX[0],4),$XX[1]
194 shl \$6,$len
195 add $inp,$len # pointer to the end of input
196 mov $len,16(%rsp)
197
198#md5# mov $ctx,24(%rsp) # save pointer to MD5_CTX
199#md5# mov 0*4($ctx),$V[0] # load current hash value from MD5_CTX
200#md5# mov 1*4($ctx),$V[1]
201#md5# mov 2*4($ctx),$V[2]
202#md5# mov 3*4($ctx),$V[3]
203 jmp .Loop
204
205.align 16
206.Loop:
207#md5# mov $V[0],0*4(%rsp) # put aside current hash value
208#md5# mov $V[1],1*4(%rsp)
209#md5# mov $V[2],2*4(%rsp)
210#md5# mov $V[3],$tmp # forward reference
211#md5# mov $V[3],3*4(%rsp)
212___
213
214sub R0 {
215 my ($i,$a,$b,$c,$d)=@_;
216 my @rot0=(7,12,17,22);
217 my $j=$i%16;
218 my $k=$i%$MOD;
219 my $xmm="%xmm".($j&1);
220 $code.=" movdqu ($in0),%xmm2\n" if ($rc4 && $j==15);
221 $code.=" add \$$MOD,$XX[0]#b\n" if ($rc4 && $j==15 && $k==$MOD-1);
222 $code.=" pxor $xmm,$xmm\n" if ($rc4 && $j<=1);
223 $code.=<<___;
224#rc4# movl ($dat,$YY,4),$TY#d
225#md5# xor $c,$tmp
226#rc4# movl $TX[0]#d,($dat,$YY,4)
227#md5# and $b,$tmp
228#md5# add 4*`$j`($inp),$a
229#rc4# add $TY#b,$TX[0]#b
230#rc4# movl `4*(($k+1)%$MOD)`(`$k==$MOD-1?"$dat,$XX[0],4":"$XX[1]"`),$TX[1]#d
231#md5# add \$$K[$i],$a
232#md5# xor $d,$tmp
233#rc4# movz $TX[0]#b,$TX[0]#d
234#rc4# movl $TY#d,4*$k($XX[1])
235#md5# add $tmp,$a
236#rc4# add $TX[1]#b,$YY#b
237#md5# rol \$$rot0[$j%4],$a
238#md5# mov `$j==15?"$b":"$c"`,$tmp # forward reference
239#rc4# pinsrw \$`($j>>1)&7`,($dat,$TX[0],4),$xmm\n
240#md5# add $b,$a
241___
242 $code.=<<___ if ($rc4 && $j==15 && $k==$MOD-1);
243 mov $YY,$XX[1]
244 xor $YY,$YY # keyword to partial register
245 mov $XX[1]#b,$YY#b
246 lea ($dat,$XX[0],4),$XX[1]
247___
248 $code.=<<___ if ($rc4 && $j==15);
249 psllq \$8,%xmm1
250 pxor %xmm0,%xmm2
251 pxor %xmm1,%xmm2
252___
253}
254sub R1 {
255 my ($i,$a,$b,$c,$d)=@_;
256 my @rot1=(5,9,14,20);
257 my $j=$i%16;
258 my $k=$i%$MOD;
259 my $xmm="%xmm".($j&1);
260 $code.=" movdqu 16($in0),%xmm3\n" if ($rc4 && $j==15);
261 $code.=" add \$$MOD,$XX[0]#b\n" if ($rc4 && $j==15 && $k==$MOD-1);
262 $code.=" pxor $xmm,$xmm\n" if ($rc4 && $j<=1);
263 $code.=<<___;
264#rc4# movl ($dat,$YY,4),$TY#d
265#md5# xor $b,$tmp
266#rc4# movl $TX[0]#d,($dat,$YY,4)
267#md5# and $d,$tmp
268#md5# add 4*`((1+5*$j)%16)`($inp),$a
269#rc4# add $TY#b,$TX[0]#b
270#rc4# movl `4*(($k+1)%$MOD)`(`$k==$MOD-1?"$dat,$XX[0],4":"$XX[1]"`),$TX[1]#d
271#md5# add \$$K[$i],$a
272#md5# xor $c,$tmp
273#rc4# movz $TX[0]#b,$TX[0]#d
274#rc4# movl $TY#d,4*$k($XX[1])
275#md5# add $tmp,$a
276#rc4# add $TX[1]#b,$YY#b
277#md5# rol \$$rot1[$j%4],$a
278#md5# mov `$j==15?"$c":"$b"`,$tmp # forward reference
279#rc4# pinsrw \$`($j>>1)&7`,($dat,$TX[0],4),$xmm\n
280#md5# add $b,$a
281___
282 $code.=<<___ if ($rc4 && $j==15 && $k==$MOD-1);
283 mov $YY,$XX[1]
284 xor $YY,$YY # keyword to partial register
285 mov $XX[1]#b,$YY#b
286 lea ($dat,$XX[0],4),$XX[1]
287___
288 $code.=<<___ if ($rc4 && $j==15);
289 psllq \$8,%xmm1
290 pxor %xmm0,%xmm3
291 pxor %xmm1,%xmm3
292___
293}
294sub R2 {
295 my ($i,$a,$b,$c,$d)=@_;
296 my @rot2=(4,11,16,23);
297 my $j=$i%16;
298 my $k=$i%$MOD;
299 my $xmm="%xmm".($j&1);
300 $code.=" movdqu 32($in0),%xmm4\n" if ($rc4 && $j==15);
301 $code.=" add \$$MOD,$XX[0]#b\n" if ($rc4 && $j==15 && $k==$MOD-1);
302 $code.=" pxor $xmm,$xmm\n" if ($rc4 && $j<=1);
303 $code.=<<___;
304#rc4# movl ($dat,$YY,4),$TY#d
305#md5# xor $c,$tmp
306#rc4# movl $TX[0]#d,($dat,$YY,4)
307#md5# xor $b,$tmp
308#md5# add 4*`((5+3*$j)%16)`($inp),$a
309#rc4# add $TY#b,$TX[0]#b
310#rc4# movl `4*(($k+1)%$MOD)`(`$k==$MOD-1?"$dat,$XX[0],4":"$XX[1]"`),$TX[1]#d
311#md5# add \$$K[$i],$a
312#rc4# movz $TX[0]#b,$TX[0]#d
313#md5# add $tmp,$a
314#rc4# movl $TY#d,4*$k($XX[1])
315#rc4# add $TX[1]#b,$YY#b
316#md5# rol \$$rot2[$j%4],$a
317#md5# mov `$j==15?"\\\$-1":"$c"`,$tmp # forward reference
318#rc4# pinsrw \$`($j>>1)&7`,($dat,$TX[0],4),$xmm\n
319#md5# add $b,$a
320___
321 $code.=<<___ if ($rc4 && $j==15 && $k==$MOD-1);
322 mov $YY,$XX[1]
323 xor $YY,$YY # keyword to partial register
324 mov $XX[1]#b,$YY#b
325 lea ($dat,$XX[0],4),$XX[1]
326___
327 $code.=<<___ if ($rc4 && $j==15);
328 psllq \$8,%xmm1
329 pxor %xmm0,%xmm4
330 pxor %xmm1,%xmm4
331___
332}
333sub R3 {
334 my ($i,$a,$b,$c,$d)=@_;
335 my @rot3=(6,10,15,21);
336 my $j=$i%16;
337 my $k=$i%$MOD;
338 my $xmm="%xmm".($j&1);
339 $code.=" movdqu 48($in0),%xmm5\n" if ($rc4 && $j==15);
340 $code.=" add \$$MOD,$XX[0]#b\n" if ($rc4 && $j==15 && $k==$MOD-1);
341 $code.=" pxor $xmm,$xmm\n" if ($rc4 && $j<=1);
342 $code.=<<___;
343#rc4# movl ($dat,$YY,4),$TY#d
344#md5# xor $d,$tmp
345#rc4# movl $TX[0]#d,($dat,$YY,4)
346#md5# or $b,$tmp
347#md5# add 4*`((7*$j)%16)`($inp),$a
348#rc4# add $TY#b,$TX[0]#b
349#rc4# movl `4*(($k+1)%$MOD)`(`$k==$MOD-1?"$dat,$XX[0],4":"$XX[1]"`),$TX[1]#d
350#md5# add \$$K[$i],$a
351#rc4# movz $TX[0]#b,$TX[0]#d
352#md5# xor $c,$tmp
353#rc4# movl $TY#d,4*$k($XX[1])
354#md5# add $tmp,$a
355#rc4# add $TX[1]#b,$YY#b
356#md5# rol \$$rot3[$j%4],$a
357#md5# mov \$-1,$tmp # forward reference
358#rc4# pinsrw \$`($j>>1)&7`,($dat,$TX[0],4),$xmm\n
359#md5# add $b,$a
360___
361 $code.=<<___ if ($rc4 && $j==15);
362 mov $XX[0],$XX[1]
363 xor $XX[0],$XX[0] # keyword to partial register
364 mov $XX[1]#b,$XX[0]#b
365 mov $YY,$XX[1]
366 xor $YY,$YY # keyword to partial register
367 mov $XX[1]#b,$YY#b
368 lea ($dat,$XX[0],4),$XX[1]
369 psllq \$8,%xmm1
370 pxor %xmm0,%xmm5
371 pxor %xmm1,%xmm5
372___
373}
374
375my $i=0;
376for(;$i<16;$i++) { R0($i,@V); unshift(@V,pop(@V)); push(@TX,shift(@TX)); }
377for(;$i<32;$i++) { R1($i,@V); unshift(@V,pop(@V)); push(@TX,shift(@TX)); }
378for(;$i<48;$i++) { R2($i,@V); unshift(@V,pop(@V)); push(@TX,shift(@TX)); }
379for(;$i<64;$i++) { R3($i,@V); unshift(@V,pop(@V)); push(@TX,shift(@TX)); }
380
381$code.=<<___;
382#md5# add 0*4(%rsp),$V[0] # accumulate hash value
383#md5# add 1*4(%rsp),$V[1]
384#md5# add 2*4(%rsp),$V[2]
385#md5# add 3*4(%rsp),$V[3]
386
387#rc4# movdqu %xmm2,($out,$in0) # write RC4 output
388#rc4# movdqu %xmm3,16($out,$in0)
389#rc4# movdqu %xmm4,32($out,$in0)
390#rc4# movdqu %xmm5,48($out,$in0)
391#md5# lea 64($inp),$inp
392#rc4# lea 64($in0),$in0
393 cmp 16(%rsp),$inp # are we done?
394 jb .Loop
395
396#md5# mov 24(%rsp),$len # restore pointer to MD5_CTX
397#rc4# sub $TX[0]#b,$YY#b # correct $YY
398#md5# mov $V[0],0*4($len) # write MD5_CTX
399#md5# mov $V[1],1*4($len)
400#md5# mov $V[2],2*4($len)
401#md5# mov $V[3],3*4($len)
402___
403$code.=<<___ if ($rc4 && (!$md5 || $D));
404 mov 32(%rsp),$len # restore original $len
405 and \$63,$len # remaining bytes
406 jnz .Loop1
407 jmp .Ldone
408
409.align 16
410.Loop1:
411 add $TX[0]#b,$YY#b
412 movl ($dat,$YY,4),$TY#d
413 movl $TX[0]#d,($dat,$YY,4)
414 movl $TY#d,($dat,$XX[0],4)
415 add $TY#b,$TX[0]#b
416 inc $XX[0]#b
417 movl ($dat,$TX[0],4),$TY#d
418 movl ($dat,$XX[0],4),$TX[0]#d
419 xorb ($in0),$TY#b
420 movb $TY#b,($out,$in0)
421 lea 1($in0),$in0
422 dec $len
423 jnz .Loop1
424
425.Ldone:
426___
427$code.=<<___;
428#rc4# sub \$1,$XX[0]#b
429#rc4# movl $XX[0]#d,-8($dat)
430#rc4# movl $YY#d,-4($dat)
431
432 mov 40(%rsp),%r15
433 mov 48(%rsp),%r14
434 mov 56(%rsp),%r13
435 mov 64(%rsp),%r12
436 mov 72(%rsp),%rbp
437 mov 80(%rsp),%rbx
438 lea 88(%rsp),%rsp
439.Lepilogue:
440.Labort:
441 ret
442.size $func,.-$func
443___
444
445if ($rc4 && $D) { # sole purpose of this section is to provide
446 # option to use the generated module as drop-in
447 # replacement for rc4-x86_64.pl for debugging
448 # and testing purposes...
449my ($idx,$ido)=("%r8","%r9");
450my ($dat,$len,$inp)=("%rdi","%rsi","%rdx");
451
452$code.=<<___;
453.globl RC4_set_key
454.type RC4_set_key,\@function,3
455.align 16
456RC4_set_key:
457 _CET_ENDBR
458 lea 8($dat),$dat
459 lea ($inp,$len),$inp
460 neg $len
461 mov $len,%rcx
462 xor %eax,%eax
463 xor $ido,$ido
464 xor %r10,%r10
465 xor %r11,%r11
466 jmp .Lw1stloop
467
468.align 16
469.Lw1stloop:
470 mov %eax,($dat,%rax,4)
471 add \$1,%al
472 jnc .Lw1stloop
473
474 xor $ido,$ido
475 xor $idx,$idx
476.align 16
477.Lw2ndloop:
478 mov ($dat,$ido,4),%r10d
479 add ($inp,$len,1),$idx#b
480 add %r10b,$idx#b
481 add \$1,$len
482 mov ($dat,$idx,4),%r11d
483 cmovz %rcx,$len
484 mov %r10d,($dat,$idx,4)
485 mov %r11d,($dat,$ido,4)
486 add \$1,$ido#b
487 jnc .Lw2ndloop
488
489 xor %eax,%eax
490 mov %eax,-8($dat)
491 mov %eax,-4($dat)
492 ret
493.size RC4_set_key,.-RC4_set_key
494___
495}
496
497sub reg_part {
498my ($reg,$conv)=@_;
499 if ($reg =~ /%r[0-9]+/) { $reg .= $conv; }
500 elsif ($conv eq "b") { $reg =~ s/%[er]([^x]+)x?/%$1l/; }
501 elsif ($conv eq "w") { $reg =~ s/%[er](.+)/%$1/; }
502 elsif ($conv eq "d") { $reg =~ s/%[er](.+)/%e$1/; }
503 return $reg;
504}
505
506$code =~ s/(%[a-z0-9]+)#([bwd])/reg_part($1,$2)/gem;
507$code =~ s/\`([^\`]*)\`/eval $1/gem;
508$code =~ s/pinsrw\s+\$0,/movd /gm;
509
510$code =~ s/#md5#//gm if ($md5);
511$code =~ s/#rc4#//gm if ($rc4);
512
513print $code;
514
515close STDOUT;