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 # ====================================================================
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:
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%
34 # (*) rc4-x86_64.pl delivers 5.3 on Opteron, so real improvement
37 my ($rc4,$md5)=(1,1); # what to generate?
38 my $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...
45 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
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
50 die "can't locate x86_64-xlate.pl";
52 open OUT
,"| \"$^X\" $xlate $flavour $output";
55 my ($dat,$in0,$out,$ctx,$inp,$len, $func,$nargs);
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;
64 ($dat,$in0,$out,$ctx,$inp,$len) = ("%rdi","%rsi","%rdx","%rcx","%r8","%r9");
65 $func="rc4_md5_enc"; $nargs=6;
68 # const void *in0, # RC4 input
69 # void *out, # RC4 output
71 # const void *inp, # MD5 input
72 # size_t len); # number of 64-byte blocks
75 my @K=( 0xd76aa478,0xe8c7b756,0x242070db,0xc1bdceee,
76 0xf57c0faf,0x4787c62a,0xa8304613,0xfd469501,
77 0x698098d8,0x8b44f7af,0xffff5bb1,0x895cd7be,
78 0x6b901122,0xfd987193,0xa679438e,0x49b40821,
80 0xf61e2562,0xc040b340,0x265e5a51,0xe9b6c7aa,
81 0xd62f105d,0x02441453,0xd8a1e681,0xe7d3fbc8,
82 0x21e1cde6,0xc33707d6,0xf4d50d87,0x455a14ed,
83 0xa9e3e905,0xfcefa3f8,0x676f02d9,0x8d2a4c8a,
85 0xfffa3942,0x8771f681,0x6d9d6122,0xfde5380c,
86 0xa4beea44,0x4bdecfa9,0xf6bb4b60,0xbebfbc70,
87 0x289b7ec6,0xeaa127fa,0xd4ef3085,0x04881d05,
88 0xd9d4d039,0xe6db99e5,0x1fa27cf8,0xc4ac5665,
90 0xf4292244,0x432aff97,0xab9423a7,0xfc93a039,
91 0x655b59c3,0x8f0ccc92,0xffeff47d,0x85845dd1,
92 0x6fa87e4f,0xfe2ce6e0,0xa3014314,0x4e0811a1,
93 0xf7537e82,0xbd3af235,0x2ad7d2bb,0xeb86d391 );
95 my @V=("%r8d","%r9d","%r10d","%r11d"); # MD5 registers
98 my @XX=("%rbp","%rsi"); # RC4 registers
99 my @TX=("%rax","%rbx");
103 my $MOD=32; # 16, 32 or 64
110 .type
$func,\
@function,$nargs
125 $D#md5# mov $ctx,%r11 # reassign arguments
129 $D#md5# mov $inp,%r15
131 $ctx="%r11" if ($md5); # reassign arguments
135 $inp="%r15" if ($md5);
136 $inp=$in0 if (!$md5);
142 mov
-8($dat),$XX[0]#b
147 movl
($dat,$XX[0],4),$TX[0]#d
149 $code.=<<___
if (!$md5);
154 and \
$`$MOD-1`,$TX[1]
155 jz
.Loop
${MOD
}_is_hot
159 movl
($dat,$YY,4),$TY#d
160 movl
$TX[0]#d,($dat,$YY,4)
161 movl
$TY#d,($dat,$XX[0],4)
164 movl
($dat,$TX[0],4),$TY#d
165 movl
($dat,$XX[0],4),$TX[0]#d
167 movb
$TY#b,($out,$in0)
170 jnz
.Loop
${MOD
}_warmup
177 mov
$len,32(%rsp) # save original $len
178 shr \
$6,$len # number of 64-byte blocks
180 if ($D && !$md5) { # stitch in dummy MD5
191 #rc4# add $TX[0]#b,$YY#b
192 #rc4# lea ($dat,$XX[0],4),$XX[1]
194 add
$inp,$len # pointer to the end of input
197 #md5# mov $ctx,24(%rsp) # save pointer to MD5_CTX
198 #md5# mov 0*4($ctx),$V[0] # load current hash value from MD5_CTX
199 #md5# mov 1*4($ctx),$V[1]
200 #md5# mov 2*4($ctx),$V[2]
201 #md5# mov 3*4($ctx),$V[3]
206 #md5# mov $V[0],0*4(%rsp) # put aside current hash value
207 #md5# mov $V[1],1*4(%rsp)
208 #md5# mov $V[2],2*4(%rsp)
209 #md5# mov $V[3],$tmp # forward reference
210 #md5# mov $V[3],3*4(%rsp)
214 my ($i,$a,$b,$c,$d)=@_;
215 my @rot0=(7,12,17,22);
218 my $xmm="%xmm".($j&1);
219 $code.=" movdqu ($in0),%xmm2\n" if ($rc4 && $j==15);
220 $code.=" add \$$MOD,$XX[0]#b\n" if ($rc4 && $j==15 && $k==$MOD-1);
221 $code.=" pxor $xmm,$xmm\n" if ($rc4 && $j<=1);
223 #rc4# movl ($dat,$YY,4),$TY#d
225 #rc4# movl $TX[0]#d,($dat,$YY,4)
227 #md5# add 4*`$j`($inp),$a
228 #rc4# add $TY#b,$TX[0]#b
229 #rc4# movl `4*(($k+1)%$MOD)`(`$k==$MOD-1?"$dat,$XX[0],4":"$XX[1]"`),$TX[1]#d
230 #md5# add \$$K[$i],$a
232 #rc4# movz $TX[0]#b,$TX[0]#d
233 #rc4# movl $TY#d,4*$k($XX[1])
235 #rc4# add $TX[1]#b,$YY#b
236 #md5# rol \$$rot0[$j%4],$a
237 #md5# mov `$j==15?"$b":"$c"`,$tmp # forward reference
238 #rc4# pinsrw \$`($j>>1)&7`,($dat,$TX[0],4),$xmm\n
241 $code.=<<___
if ($rc4 && $j==15 && $k==$MOD-1);
243 xor $YY,$YY # keyword to partial register
245 lea
($dat,$XX[0],4),$XX[1]
247 $code.=<<___
if ($rc4 && $j==15);
254 my ($i,$a,$b,$c,$d)=@_;
255 my @rot1=(5,9,14,20);
258 my $xmm="%xmm".($j&1);
259 $code.=" movdqu 16($in0),%xmm3\n" if ($rc4 && $j==15);
260 $code.=" add \$$MOD,$XX[0]#b\n" if ($rc4 && $j==15 && $k==$MOD-1);
261 $code.=" pxor $xmm,$xmm\n" if ($rc4 && $j<=1);
263 #rc4# movl ($dat,$YY,4),$TY#d
265 #rc4# movl $TX[0]#d,($dat,$YY,4)
267 #md5# add 4*`((1+5*$j)%16)`($inp),$a
268 #rc4# add $TY#b,$TX[0]#b
269 #rc4# movl `4*(($k+1)%$MOD)`(`$k==$MOD-1?"$dat,$XX[0],4":"$XX[1]"`),$TX[1]#d
270 #md5# add \$$K[$i],$a
272 #rc4# movz $TX[0]#b,$TX[0]#d
273 #rc4# movl $TY#d,4*$k($XX[1])
275 #rc4# add $TX[1]#b,$YY#b
276 #md5# rol \$$rot1[$j%4],$a
277 #md5# mov `$j==15?"$c":"$b"`,$tmp # forward reference
278 #rc4# pinsrw \$`($j>>1)&7`,($dat,$TX[0],4),$xmm\n
281 $code.=<<___
if ($rc4 && $j==15 && $k==$MOD-1);
283 xor $YY,$YY # keyword to partial register
285 lea
($dat,$XX[0],4),$XX[1]
287 $code.=<<___
if ($rc4 && $j==15);
294 my ($i,$a,$b,$c,$d)=@_;
295 my @rot2=(4,11,16,23);
298 my $xmm="%xmm".($j&1);
299 $code.=" movdqu 32($in0),%xmm4\n" if ($rc4 && $j==15);
300 $code.=" add \$$MOD,$XX[0]#b\n" if ($rc4 && $j==15 && $k==$MOD-1);
301 $code.=" pxor $xmm,$xmm\n" if ($rc4 && $j<=1);
303 #rc4# movl ($dat,$YY,4),$TY#d
305 #rc4# movl $TX[0]#d,($dat,$YY,4)
307 #md5# add 4*`((5+3*$j)%16)`($inp),$a
308 #rc4# add $TY#b,$TX[0]#b
309 #rc4# movl `4*(($k+1)%$MOD)`(`$k==$MOD-1?"$dat,$XX[0],4":"$XX[1]"`),$TX[1]#d
310 #md5# add \$$K[$i],$a
311 #rc4# movz $TX[0]#b,$TX[0]#d
313 #rc4# movl $TY#d,4*$k($XX[1])
314 #rc4# add $TX[1]#b,$YY#b
315 #md5# rol \$$rot2[$j%4],$a
316 #md5# mov `$j==15?"\\\$-1":"$c"`,$tmp # forward reference
317 #rc4# pinsrw \$`($j>>1)&7`,($dat,$TX[0],4),$xmm\n
320 $code.=<<___
if ($rc4 && $j==15 && $k==$MOD-1);
322 xor $YY,$YY # keyword to partial register
324 lea
($dat,$XX[0],4),$XX[1]
326 $code.=<<___
if ($rc4 && $j==15);
333 my ($i,$a,$b,$c,$d)=@_;
334 my @rot3=(6,10,15,21);
337 my $xmm="%xmm".($j&1);
338 $code.=" movdqu 48($in0),%xmm5\n" if ($rc4 && $j==15);
339 $code.=" add \$$MOD,$XX[0]#b\n" if ($rc4 && $j==15 && $k==$MOD-1);
340 $code.=" pxor $xmm,$xmm\n" if ($rc4 && $j<=1);
342 #rc4# movl ($dat,$YY,4),$TY#d
344 #rc4# movl $TX[0]#d,($dat,$YY,4)
346 #md5# add 4*`((7*$j)%16)`($inp),$a
347 #rc4# add $TY#b,$TX[0]#b
348 #rc4# movl `4*(($k+1)%$MOD)`(`$k==$MOD-1?"$dat,$XX[0],4":"$XX[1]"`),$TX[1]#d
349 #md5# add \$$K[$i],$a
350 #rc4# movz $TX[0]#b,$TX[0]#d
352 #rc4# movl $TY#d,4*$k($XX[1])
354 #rc4# add $TX[1]#b,$YY#b
355 #md5# rol \$$rot3[$j%4],$a
356 #md5# mov \$-1,$tmp # forward reference
357 #rc4# pinsrw \$`($j>>1)&7`,($dat,$TX[0],4),$xmm\n
360 $code.=<<___
if ($rc4 && $j==15);
362 xor $XX[0],$XX[0] # keyword to partial register
363 mov
$XX[1]#b,$XX[0]#b
365 xor $YY,$YY # keyword to partial register
367 lea
($dat,$XX[0],4),$XX[1]
375 for(;$i<16;$i++) { R0
($i,@V); unshift(@V,pop(@V)); push(@TX,shift(@TX)); }
376 for(;$i<32;$i++) { R1
($i,@V); unshift(@V,pop(@V)); push(@TX,shift(@TX)); }
377 for(;$i<48;$i++) { R2
($i,@V); unshift(@V,pop(@V)); push(@TX,shift(@TX)); }
378 for(;$i<64;$i++) { R3
($i,@V); unshift(@V,pop(@V)); push(@TX,shift(@TX)); }
381 #md5# add 0*4(%rsp),$V[0] # accumulate hash value
382 #md5# add 1*4(%rsp),$V[1]
383 #md5# add 2*4(%rsp),$V[2]
384 #md5# add 3*4(%rsp),$V[3]
386 #rc4# movdqu %xmm2,($out,$in0) # write RC4 output
387 #rc4# movdqu %xmm3,16($out,$in0)
388 #rc4# movdqu %xmm4,32($out,$in0)
389 #rc4# movdqu %xmm5,48($out,$in0)
390 #md5# lea 64($inp),$inp
391 #rc4# lea 64($in0),$in0
392 cmp 16(%rsp),$inp # are we done?
395 #md5# mov 24(%rsp),$len # restore pointer to MD5_CTX
396 #rc4# sub $TX[0]#b,$YY#b # correct $YY
397 #md5# mov $V[0],0*4($len) # write MD5_CTX
398 #md5# mov $V[1],1*4($len)
399 #md5# mov $V[2],2*4($len)
400 #md5# mov $V[3],3*4($len)
402 $code.=<<___
if ($rc4 && (!$md5 || $D));
403 mov
32(%rsp),$len # restore original $len
404 and \
$63,$len # remaining bytes
411 movl
($dat,$YY,4),$TY#d
412 movl
$TX[0]#d,($dat,$YY,4)
413 movl
$TY#d,($dat,$XX[0],4)
416 movl
($dat,$TX[0],4),$TY#d
417 movl
($dat,$XX[0],4),$TX[0]#d
419 movb
$TY#b,($out,$in0)
427 #rc4# sub \$1,$XX[0]#b
428 #rc4# movl $XX[0]#d,-8($dat)
429 #rc4# movl $YY#d,-4($dat)
444 if ($rc4 && $D) { # sole purpose of this section is to provide
445 # option to use the generated module as drop-in
446 # replacement for rc4-x86_64.pl for debugging
447 # and testing purposes...
448 my ($idx,$ido)=("%r8","%r9");
449 my ($dat,$len,$inp)=("%rdi","%rsi","%rdx");
453 .type RC4_set_key
,\
@function,3
468 mov
%eax,($dat,%rax,4)
476 mov
($dat,$ido,4),%r10d
477 add
($inp,$len,1),$idx#b
480 mov
($dat,$idx,4),%r11d
482 mov
%r10d,($dat,$idx,4)
483 mov
%r11d,($dat,$ido,4)
491 .size RC4_set_key
,.-RC4_set_key
494 .type RC4_options
,\
@abi-omnipotent
497 lea
.Lopts
(%rip),%rax
501 .asciz
"rc4(64x,int)"
503 .size RC4_options
,.-RC4_options
509 if ($reg =~ /%r[0-9]+/) { $reg .= $conv; }
510 elsif ($conv eq "b") { $reg =~ s/%[er]([^x]+)x?/%$1l/; }
511 elsif ($conv eq "w") { $reg =~ s/%[er](.+)/%$1/; }
512 elsif ($conv eq "d") { $reg =~ s/%[er](.+)/%e$1/; }
516 $code =~ s/(%[a-z0-9]+)#([bwd])/reg_part($1,$2)/gem;
517 $code =~ s/\`([^\`]*)\`/eval $1/gem;
518 $code =~ s/pinsrw\s+\$0,/movd /gm;
520 $code =~ s/#md5#//gm if ($md5);
521 $code =~ s/#rc4#//gm if ($rc4);