dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / lib / libcrypto / rc4 / asm / rc4-md5-x86_64.pl
blob501d9e936bb1da46cb7df1acad30c6f28c1ba769
1 #!/usr/bin/env perl
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 # ====================================================================
10 # June 2011
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
35 # is +53%...
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...
43 my $flavour = shift;
44 my $output = shift;
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";
53 *STDOUT=*OUT;
55 my ($dat,$in0,$out,$ctx,$inp,$len, $func,$nargs);
57 if ($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
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
96 my $tmp="%r12d";
98 my @XX=("%rbp","%rsi"); # RC4 registers
99 my @TX=("%rax","%rbx");
100 my $YY="%rcx";
101 my $TY="%rdx";
103 my $MOD=32; # 16, 32 or 64
105 $code.=<<___;
106 .text
107 .align 16
109 .globl $func
110 .type $func,\@function,$nargs
111 $func:
112 cmp \$0,$len
113 je .Labort
114 push %rbx
115 push %rbp
116 push %r12
117 push %r13
118 push %r14
119 push %r15
120 sub \$40,%rsp
121 .Lbody:
123 if ($rc4) {
124 $code.=<<___;
125 $D#md5# mov $ctx,%r11 # reassign arguments
126 mov $len,%r12
127 mov $in0,%r13
128 mov $out,%r14
129 $D#md5# mov $inp,%r15
131 $ctx="%r11" if ($md5); # reassign arguments
132 $len="%r12";
133 $in0="%r13";
134 $out="%r14";
135 $inp="%r15" if ($md5);
136 $inp=$in0 if (!$md5);
137 $code.=<<___;
138 xor $XX[0],$XX[0]
139 xor $YY,$YY
141 lea 8($dat),$dat
142 mov -8($dat),$XX[0]#b
143 mov -4($dat),$YY#b
145 inc $XX[0]#b
146 sub $in0,$out
147 movl ($dat,$XX[0],4),$TX[0]#d
149 $code.=<<___ if (!$md5);
150 xor $TX[1],$TX[1]
151 test \$-128,$len
152 jz .Loop1
153 sub $XX[0],$TX[1]
154 and \$`$MOD-1`,$TX[1]
155 jz .Loop${MOD}_is_hot
156 sub $TX[1],$len
157 .Loop${MOD}_warmup:
158 add $TX[0]#b,$YY#b
159 movl ($dat,$YY,4),$TY#d
160 movl $TX[0]#d,($dat,$YY,4)
161 movl $TY#d,($dat,$XX[0],4)
162 add $TY#b,$TX[0]#b
163 inc $XX[0]#b
164 movl ($dat,$TX[0],4),$TY#d
165 movl ($dat,$XX[0],4),$TX[0]#d
166 xorb ($in0),$TY#b
167 movb $TY#b,($out,$in0)
168 lea 1($in0),$in0
169 dec $TX[1]
170 jnz .Loop${MOD}_warmup
172 mov $YY,$TX[1]
173 xor $YY,$YY
174 mov $TX[1]#b,$YY#b
176 .Loop${MOD}_is_hot:
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
181 $md5=1;
182 $ctx="%r11";
183 $inp="%r15";
184 $code.=<<___;
185 mov %rsp,$ctx
186 mov $in0,$inp
190 $code.=<<___;
191 #rc4# add $TX[0]#b,$YY#b
192 #rc4# lea ($dat,$XX[0],4),$XX[1]
193 shl \$6,$len
194 add $inp,$len # pointer to the end of input
195 mov $len,16(%rsp)
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]
202 jmp .Loop
204 .align 16
205 .Loop:
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)
213 sub R0 {
214 my ($i,$a,$b,$c,$d)=@_;
215 my @rot0=(7,12,17,22);
216 my $j=$i%16;
217 my $k=$i%$MOD;
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);
222 $code.=<<___;
223 #rc4# movl ($dat,$YY,4),$TY#d
224 #md5# xor $c,$tmp
225 #rc4# movl $TX[0]#d,($dat,$YY,4)
226 #md5# and $b,$tmp
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
231 #md5# xor $d,$tmp
232 #rc4# movz $TX[0]#b,$TX[0]#d
233 #rc4# movl $TY#d,4*$k($XX[1])
234 #md5# add $tmp,$a
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
239 #md5# add $b,$a
241 $code.=<<___ if ($rc4 && $j==15 && $k==$MOD-1);
242 mov $YY,$XX[1]
243 xor $YY,$YY # keyword to partial register
244 mov $XX[1]#b,$YY#b
245 lea ($dat,$XX[0],4),$XX[1]
247 $code.=<<___ if ($rc4 && $j==15);
248 psllq \$8,%xmm1
249 pxor %xmm0,%xmm2
250 pxor %xmm1,%xmm2
253 sub R1 {
254 my ($i,$a,$b,$c,$d)=@_;
255 my @rot1=(5,9,14,20);
256 my $j=$i%16;
257 my $k=$i%$MOD;
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);
262 $code.=<<___;
263 #rc4# movl ($dat,$YY,4),$TY#d
264 #md5# xor $b,$tmp
265 #rc4# movl $TX[0]#d,($dat,$YY,4)
266 #md5# and $d,$tmp
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
271 #md5# xor $c,$tmp
272 #rc4# movz $TX[0]#b,$TX[0]#d
273 #rc4# movl $TY#d,4*$k($XX[1])
274 #md5# add $tmp,$a
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
279 #md5# add $b,$a
281 $code.=<<___ if ($rc4 && $j==15 && $k==$MOD-1);
282 mov $YY,$XX[1]
283 xor $YY,$YY # keyword to partial register
284 mov $XX[1]#b,$YY#b
285 lea ($dat,$XX[0],4),$XX[1]
287 $code.=<<___ if ($rc4 && $j==15);
288 psllq \$8,%xmm1
289 pxor %xmm0,%xmm3
290 pxor %xmm1,%xmm3
293 sub R2 {
294 my ($i,$a,$b,$c,$d)=@_;
295 my @rot2=(4,11,16,23);
296 my $j=$i%16;
297 my $k=$i%$MOD;
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);
302 $code.=<<___;
303 #rc4# movl ($dat,$YY,4),$TY#d
304 #md5# xor $c,$tmp
305 #rc4# movl $TX[0]#d,($dat,$YY,4)
306 #md5# xor $b,$tmp
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
312 #md5# add $tmp,$a
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
318 #md5# add $b,$a
320 $code.=<<___ if ($rc4 && $j==15 && $k==$MOD-1);
321 mov $YY,$XX[1]
322 xor $YY,$YY # keyword to partial register
323 mov $XX[1]#b,$YY#b
324 lea ($dat,$XX[0],4),$XX[1]
326 $code.=<<___ if ($rc4 && $j==15);
327 psllq \$8,%xmm1
328 pxor %xmm0,%xmm4
329 pxor %xmm1,%xmm4
332 sub R3 {
333 my ($i,$a,$b,$c,$d)=@_;
334 my @rot3=(6,10,15,21);
335 my $j=$i%16;
336 my $k=$i%$MOD;
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);
341 $code.=<<___;
342 #rc4# movl ($dat,$YY,4),$TY#d
343 #md5# xor $d,$tmp
344 #rc4# movl $TX[0]#d,($dat,$YY,4)
345 #md5# or $b,$tmp
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
351 #md5# xor $c,$tmp
352 #rc4# movl $TY#d,4*$k($XX[1])
353 #md5# add $tmp,$a
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
358 #md5# add $b,$a
360 $code.=<<___ if ($rc4 && $j==15);
361 mov $XX[0],$XX[1]
362 xor $XX[0],$XX[0] # keyword to partial register
363 mov $XX[1]#b,$XX[0]#b
364 mov $YY,$XX[1]
365 xor $YY,$YY # keyword to partial register
366 mov $XX[1]#b,$YY#b
367 lea ($dat,$XX[0],4),$XX[1]
368 psllq \$8,%xmm1
369 pxor %xmm0,%xmm5
370 pxor %xmm1,%xmm5
374 my $i=0;
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)); }
380 $code.=<<___;
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?
393 jb .Loop
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
405 jnz .Loop1
406 jmp .Ldone
408 .align 16
409 .Loop1:
410 add $TX[0]#b,$YY#b
411 movl ($dat,$YY,4),$TY#d
412 movl $TX[0]#d,($dat,$YY,4)
413 movl $TY#d,($dat,$XX[0],4)
414 add $TY#b,$TX[0]#b
415 inc $XX[0]#b
416 movl ($dat,$TX[0],4),$TY#d
417 movl ($dat,$XX[0],4),$TX[0]#d
418 xorb ($in0),$TY#b
419 movb $TY#b,($out,$in0)
420 lea 1($in0),$in0
421 dec $len
422 jnz .Loop1
424 .Ldone:
426 $code.=<<___;
427 #rc4# sub \$1,$XX[0]#b
428 #rc4# movl $XX[0]#d,-8($dat)
429 #rc4# movl $YY#d,-4($dat)
431 mov 40(%rsp),%r15
432 mov 48(%rsp),%r14
433 mov 56(%rsp),%r13
434 mov 64(%rsp),%r12
435 mov 72(%rsp),%rbp
436 mov 80(%rsp),%rbx
437 lea 88(%rsp),%rsp
438 .Lepilogue:
439 .Labort:
441 .size $func,.-$func
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");
451 $code.=<<___;
452 .globl RC4_set_key
453 .type RC4_set_key,\@function,3
454 .align 16
455 RC4_set_key:
456 lea 8($dat),$dat
457 lea ($inp,$len),$inp
458 neg $len
459 mov $len,%rcx
460 xor %eax,%eax
461 xor $ido,$ido
462 xor %r10,%r10
463 xor %r11,%r11
464 jmp .Lw1stloop
466 .align 16
467 .Lw1stloop:
468 mov %eax,($dat,%rax,4)
469 add \$1,%al
470 jnc .Lw1stloop
472 xor $ido,$ido
473 xor $idx,$idx
474 .align 16
475 .Lw2ndloop:
476 mov ($dat,$ido,4),%r10d
477 add ($inp,$len,1),$idx#b
478 add %r10b,$idx#b
479 add \$1,$len
480 mov ($dat,$idx,4),%r11d
481 cmovz %rcx,$len
482 mov %r10d,($dat,$idx,4)
483 mov %r11d,($dat,$ido,4)
484 add \$1,$ido#b
485 jnc .Lw2ndloop
487 xor %eax,%eax
488 mov %eax,-8($dat)
489 mov %eax,-4($dat)
491 .size RC4_set_key,.-RC4_set_key
493 .globl RC4_options
494 .type RC4_options,\@abi-omnipotent
495 .align 16
496 RC4_options:
497 lea .Lopts(%rip),%rax
499 .align 64
500 .Lopts:
501 .asciz "rc4(64x,int)"
502 .align 64
503 .size RC4_options,.-RC4_options
507 sub reg_part {
508 my ($reg,$conv)=@_;
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/; }
513 return $reg;
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);
523 print $code;
525 close STDOUT;