2
0

rc4-md5-x86_64.pl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. #! /usr/bin/env perl
  2. # Copyright 2011-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. #
  9. # ====================================================================
  10. # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
  11. # project. The module is, however, dual licensed under OpenSSL and
  12. # CRYPTOGAMS licenses depending on where you obtain it. For further
  13. # details see http://www.openssl.org/~appro/cryptogams/.
  14. # ====================================================================
  15. # June 2011
  16. #
  17. # This is RC4+MD5 "stitch" implementation. The idea, as spelled in
  18. # http://download.intel.com/design/intarch/papers/323686.pdf, is that
  19. # since both algorithms exhibit instruction-level parallelism, ILP,
  20. # below theoretical maximum, interleaving them would allow to utilize
  21. # processor resources better and achieve better performance. RC4
  22. # instruction sequence is virtually identical to rc4-x86_64.pl, which
  23. # is heavily based on submission by Maxim Perminov, Maxim Locktyukhin
  24. # and Jim Guilford of Intel. MD5 is fresh implementation aiming to
  25. # minimize register usage, which was used as "main thread" with RC4
  26. # weaved into it, one RC4 round per one MD5 round. In addition to the
  27. # stiched subroutine the script can generate standalone replacement
  28. # ossl_md5_block_asm_data_order and RC4. Below are performance numbers in
  29. # cycles per processed byte, less is better, for these the standalone
  30. # subroutines, sum of them, and stitched one:
  31. #
  32. # RC4 MD5 RC4+MD5 stitch gain
  33. # Opteron 6.5(*) 5.4 11.9 7.0 +70%(*)
  34. # Core2 6.5 5.8 12.3 7.7 +60%
  35. # Westmere 4.3 5.2 9.5 7.0 +36%
  36. # Sandy Bridge 4.2 5.5 9.7 6.8 +43%
  37. # Ivy Bridge 4.1 5.2 9.3 6.0 +54%
  38. # Haswell 4.0 5.0 9.0 5.7 +60%
  39. # Skylake 6.3(**) 5.0 11.3 5.3 +110%
  40. # Atom 9.3 6.5 15.8 11.1 +42%
  41. # VIA Nano 6.3 5.4 11.7 8.6 +37%
  42. # Bulldozer 4.5 5.4 9.9 7.7 +29%
  43. #
  44. # (*) rc4-x86_64.pl delivers 5.3 on Opteron, so real improvement
  45. # is +53%...
  46. # (**) unidentified anomaly;
  47. my ($rc4,$md5)=(1,1); # what to generate?
  48. my $D="#" if (!$md5); # if set to "#", MD5 is stitched into RC4(),
  49. # but its result is discarded. Idea here is
  50. # to be able to use 'openssl speed rc4' for
  51. # benchmarking the stitched subroutine...
  52. # $output is the last argument if it looks like a file (it has an extension)
  53. # $flavour is the first argument if it doesn't look like a file
  54. my $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
  55. my $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
  56. my $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
  57. $0 =~ m/(.*[\/\\])[^\/\\]+$/; my $dir=$1; my $xlate;
  58. ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
  59. ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
  60. die "can't locate x86_64-xlate.pl";
  61. open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\""
  62. or die "can't call $xlate: $!";
  63. *STDOUT=*OUT;
  64. my ($dat,$in0,$out,$ctx,$inp,$len, $func,$nargs);
  65. if ($rc4 && !$md5) {
  66. ($dat,$len,$in0,$out) = ("%rdi","%rsi","%rdx","%rcx");
  67. $func="RC4"; $nargs=4;
  68. } elsif ($md5 && !$rc4) {
  69. ($ctx,$inp,$len) = ("%rdi","%rsi","%rdx");
  70. $func="ossl_md5_block_asm_data_order"; $nargs=3;
  71. } else {
  72. ($dat,$in0,$out,$ctx,$inp,$len) = ("%rdi","%rsi","%rdx","%rcx","%r8","%r9");
  73. $func="rc4_md5_enc"; $nargs=6;
  74. # void rc4_md5_enc(
  75. # RC4_KEY *key, #
  76. # const void *in0, # RC4 input
  77. # void *out, # RC4 output
  78. # MD5_CTX *ctx, #
  79. # const void *inp, # MD5 input
  80. # size_t len); # number of 64-byte blocks
  81. }
  82. my @K=( 0xd76aa478,0xe8c7b756,0x242070db,0xc1bdceee,
  83. 0xf57c0faf,0x4787c62a,0xa8304613,0xfd469501,
  84. 0x698098d8,0x8b44f7af,0xffff5bb1,0x895cd7be,
  85. 0x6b901122,0xfd987193,0xa679438e,0x49b40821,
  86. 0xf61e2562,0xc040b340,0x265e5a51,0xe9b6c7aa,
  87. 0xd62f105d,0x02441453,0xd8a1e681,0xe7d3fbc8,
  88. 0x21e1cde6,0xc33707d6,0xf4d50d87,0x455a14ed,
  89. 0xa9e3e905,0xfcefa3f8,0x676f02d9,0x8d2a4c8a,
  90. 0xfffa3942,0x8771f681,0x6d9d6122,0xfde5380c,
  91. 0xa4beea44,0x4bdecfa9,0xf6bb4b60,0xbebfbc70,
  92. 0x289b7ec6,0xeaa127fa,0xd4ef3085,0x04881d05,
  93. 0xd9d4d039,0xe6db99e5,0x1fa27cf8,0xc4ac5665,
  94. 0xf4292244,0x432aff97,0xab9423a7,0xfc93a039,
  95. 0x655b59c3,0x8f0ccc92,0xffeff47d,0x85845dd1,
  96. 0x6fa87e4f,0xfe2ce6e0,0xa3014314,0x4e0811a1,
  97. 0xf7537e82,0xbd3af235,0x2ad7d2bb,0xeb86d391 );
  98. my @V=("%r8d","%r9d","%r10d","%r11d"); # MD5 registers
  99. my $tmp="%r12d";
  100. my @XX=("%rbp","%rsi"); # RC4 registers
  101. my @TX=("%rax","%rbx");
  102. my $YY="%rcx";
  103. my $TY="%rdx";
  104. my $MOD=32; # 16, 32 or 64
  105. $code.=<<___;
  106. .text
  107. .align 16
  108. .globl $func
  109. .type $func,\@function,$nargs
  110. $func:
  111. .cfi_startproc
  112. cmp \$0,$len
  113. je .Labort
  114. push %rbx
  115. .cfi_push %rbx
  116. push %rbp
  117. .cfi_push %rbp
  118. push %r12
  119. .cfi_push %r12
  120. push %r13
  121. .cfi_push %r13
  122. push %r14
  123. .cfi_push %r14
  124. push %r15
  125. .cfi_push %r15
  126. sub \$40,%rsp
  127. .cfi_adjust_cfa_offset 40
  128. .Lbody:
  129. ___
  130. if ($rc4) {
  131. $code.=<<___;
  132. $D#md5# mov $ctx,%r11 # reassign arguments
  133. mov $len,%r12
  134. mov $in0,%r13
  135. mov $out,%r14
  136. $D#md5# mov $inp,%r15
  137. ___
  138. $ctx="%r11" if ($md5); # reassign arguments
  139. $len="%r12";
  140. $in0="%r13";
  141. $out="%r14";
  142. $inp="%r15" if ($md5);
  143. $inp=$in0 if (!$md5);
  144. $code.=<<___;
  145. xor $XX[0],$XX[0]
  146. xor $YY,$YY
  147. lea 8($dat),$dat
  148. mov -8($dat),$XX[0]#b
  149. mov -4($dat),$YY#b
  150. inc $XX[0]#b
  151. sub $in0,$out
  152. movl ($dat,$XX[0],4),$TX[0]#d
  153. ___
  154. $code.=<<___ if (!$md5);
  155. xor $TX[1],$TX[1]
  156. test \$-128,$len
  157. jz .Loop1
  158. sub $XX[0],$TX[1]
  159. and \$`$MOD-1`,$TX[1]
  160. jz .Loop${MOD}_is_hot
  161. sub $TX[1],$len
  162. .Loop${MOD}_warmup:
  163. add $TX[0]#b,$YY#b
  164. movl ($dat,$YY,4),$TY#d
  165. movl $TX[0]#d,($dat,$YY,4)
  166. movl $TY#d,($dat,$XX[0],4)
  167. add $TY#b,$TX[0]#b
  168. inc $XX[0]#b
  169. movl ($dat,$TX[0],4),$TY#d
  170. movl ($dat,$XX[0],4),$TX[0]#d
  171. xorb ($in0),$TY#b
  172. movb $TY#b,($out,$in0)
  173. lea 1($in0),$in0
  174. dec $TX[1]
  175. jnz .Loop${MOD}_warmup
  176. mov $YY,$TX[1]
  177. xor $YY,$YY
  178. mov $TX[1]#b,$YY#b
  179. .Loop${MOD}_is_hot:
  180. mov $len,32(%rsp) # save original $len
  181. shr \$6,$len # number of 64-byte blocks
  182. ___
  183. if ($D && !$md5) { # stitch in dummy MD5
  184. $md5=1;
  185. $ctx="%r11";
  186. $inp="%r15";
  187. $code.=<<___;
  188. mov %rsp,$ctx
  189. mov $in0,$inp
  190. ___
  191. }
  192. }
  193. $code.=<<___;
  194. #rc4# add $TX[0]#b,$YY#b
  195. #rc4# lea ($dat,$XX[0],4),$XX[1]
  196. shl \$6,$len
  197. add $inp,$len # pointer to the end of input
  198. mov $len,16(%rsp)
  199. #md5# mov $ctx,24(%rsp) # save pointer to MD5_CTX
  200. #md5# mov 0*4($ctx),$V[0] # load current hash value from MD5_CTX
  201. #md5# mov 1*4($ctx),$V[1]
  202. #md5# mov 2*4($ctx),$V[2]
  203. #md5# mov 3*4($ctx),$V[3]
  204. jmp .Loop
  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. 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
  240. ___
  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]
  246. ___
  247. $code.=<<___ if ($rc4 && $j==15);
  248. psllq \$8,%xmm1
  249. pxor %xmm0,%xmm2
  250. pxor %xmm1,%xmm2
  251. ___
  252. }
  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
  280. ___
  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]
  286. ___
  287. $code.=<<___ if ($rc4 && $j==15);
  288. psllq \$8,%xmm1
  289. pxor %xmm0,%xmm3
  290. pxor %xmm1,%xmm3
  291. ___
  292. }
  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
  319. ___
  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]
  325. ___
  326. $code.=<<___ if ($rc4 && $j==15);
  327. psllq \$8,%xmm1
  328. pxor %xmm0,%xmm4
  329. pxor %xmm1,%xmm4
  330. ___
  331. }
  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
  359. ___
  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
  371. ___
  372. }
  373. my $i=0;
  374. for(;$i<16;$i++) { R0($i,@V); unshift(@V,pop(@V)); push(@TX,shift(@TX)); }
  375. for(;$i<32;$i++) { R1($i,@V); unshift(@V,pop(@V)); push(@TX,shift(@TX)); }
  376. for(;$i<48;$i++) { R2($i,@V); unshift(@V,pop(@V)); push(@TX,shift(@TX)); }
  377. for(;$i<64;$i++) { R3($i,@V); unshift(@V,pop(@V)); push(@TX,shift(@TX)); }
  378. $code.=<<___;
  379. #md5# add 0*4(%rsp),$V[0] # accumulate hash value
  380. #md5# add 1*4(%rsp),$V[1]
  381. #md5# add 2*4(%rsp),$V[2]
  382. #md5# add 3*4(%rsp),$V[3]
  383. #rc4# movdqu %xmm2,($out,$in0) # write RC4 output
  384. #rc4# movdqu %xmm3,16($out,$in0)
  385. #rc4# movdqu %xmm4,32($out,$in0)
  386. #rc4# movdqu %xmm5,48($out,$in0)
  387. #md5# lea 64($inp),$inp
  388. #rc4# lea 64($in0),$in0
  389. cmp 16(%rsp),$inp # are we done?
  390. jb .Loop
  391. #md5# mov 24(%rsp),$len # restore pointer to MD5_CTX
  392. #rc4# sub $TX[0]#b,$YY#b # correct $YY
  393. #md5# mov $V[0],0*4($len) # write MD5_CTX
  394. #md5# mov $V[1],1*4($len)
  395. #md5# mov $V[2],2*4($len)
  396. #md5# mov $V[3],3*4($len)
  397. ___
  398. $code.=<<___ if ($rc4 && (!$md5 || $D));
  399. mov 32(%rsp),$len # restore original $len
  400. and \$63,$len # remaining bytes
  401. jnz .Loop1
  402. jmp .Ldone
  403. .align 16
  404. .Loop1:
  405. add $TX[0]#b,$YY#b
  406. movl ($dat,$YY,4),$TY#d
  407. movl $TX[0]#d,($dat,$YY,4)
  408. movl $TY#d,($dat,$XX[0],4)
  409. add $TY#b,$TX[0]#b
  410. inc $XX[0]#b
  411. movl ($dat,$TX[0],4),$TY#d
  412. movl ($dat,$XX[0],4),$TX[0]#d
  413. xorb ($in0),$TY#b
  414. movb $TY#b,($out,$in0)
  415. lea 1($in0),$in0
  416. dec $len
  417. jnz .Loop1
  418. .Ldone:
  419. ___
  420. $code.=<<___;
  421. #rc4# sub \$1,$XX[0]#b
  422. #rc4# movl $XX[0]#d,-8($dat)
  423. #rc4# movl $YY#d,-4($dat)
  424. mov 40(%rsp),%r15
  425. .cfi_restore %r15
  426. mov 48(%rsp),%r14
  427. .cfi_restore %r14
  428. mov 56(%rsp),%r13
  429. .cfi_restore %r13
  430. mov 64(%rsp),%r12
  431. .cfi_restore %r12
  432. mov 72(%rsp),%rbp
  433. .cfi_restore %rbp
  434. mov 80(%rsp),%rbx
  435. .cfi_restore %rbx
  436. lea 88(%rsp),%rsp
  437. .cfi_adjust_cfa_offset -88
  438. .Lepilogue:
  439. .Labort:
  440. ret
  441. .cfi_endproc
  442. .size $func,.-$func
  443. ___
  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");
  450. $code.=<<___;
  451. .globl RC4_set_key
  452. .type RC4_set_key,\@function,3
  453. .align 16
  454. RC4_set_key:
  455. .cfi_startproc
  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
  465. .align 16
  466. .Lw1stloop:
  467. mov %eax,($dat,%rax,4)
  468. add \$1,%al
  469. jnc .Lw1stloop
  470. xor $ido,$ido
  471. xor $idx,$idx
  472. .align 16
  473. .Lw2ndloop:
  474. mov ($dat,$ido,4),%r10d
  475. add ($inp,$len,1),$idx#b
  476. add %r10b,$idx#b
  477. add \$1,$len
  478. mov ($dat,$idx,4),%r11d
  479. cmovz %rcx,$len
  480. mov %r10d,($dat,$idx,4)
  481. mov %r11d,($dat,$ido,4)
  482. add \$1,$ido#b
  483. jnc .Lw2ndloop
  484. xor %eax,%eax
  485. mov %eax,-8($dat)
  486. mov %eax,-4($dat)
  487. ret
  488. .cfi_endproc
  489. .size RC4_set_key,.-RC4_set_key
  490. .globl RC4_options
  491. .type RC4_options,\@abi-omnipotent
  492. .align 16
  493. RC4_options:
  494. lea .Lopts(%rip),%rax
  495. ret
  496. .align 64
  497. .Lopts:
  498. .asciz "rc4(64x,int)"
  499. .align 64
  500. .size RC4_options,.-RC4_options
  501. ___
  502. }
  503. # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
  504. # CONTEXT *context,DISPATCHER_CONTEXT *disp)
  505. if ($win64) {
  506. my $rec="%rcx";
  507. my $frame="%rdx";
  508. my $context="%r8";
  509. my $disp="%r9";
  510. $code.=<<___;
  511. .extern __imp_RtlVirtualUnwind
  512. .type se_handler,\@abi-omnipotent
  513. .align 16
  514. se_handler:
  515. push %rsi
  516. push %rdi
  517. push %rbx
  518. push %rbp
  519. push %r12
  520. push %r13
  521. push %r14
  522. push %r15
  523. pushfq
  524. sub \$64,%rsp
  525. mov 120($context),%rax # pull context->Rax
  526. mov 248($context),%rbx # pull context->Rip
  527. lea .Lbody(%rip),%r10
  528. cmp %r10,%rbx # context->Rip<.Lbody
  529. jb .Lin_prologue
  530. mov 152($context),%rax # pull context->Rsp
  531. lea .Lepilogue(%rip),%r10
  532. cmp %r10,%rbx # context->Rip>=.Lepilogue
  533. jae .Lin_prologue
  534. mov 40(%rax),%r15
  535. mov 48(%rax),%r14
  536. mov 56(%rax),%r13
  537. mov 64(%rax),%r12
  538. mov 72(%rax),%rbp
  539. mov 80(%rax),%rbx
  540. lea 88(%rax),%rax
  541. mov %rbx,144($context) # restore context->Rbx
  542. mov %rbp,160($context) # restore context->Rbp
  543. mov %r12,216($context) # restore context->R12
  544. mov %r13,224($context) # restore context->R12
  545. mov %r14,232($context) # restore context->R14
  546. mov %r15,240($context) # restore context->R15
  547. .Lin_prologue:
  548. mov 8(%rax),%rdi
  549. mov 16(%rax),%rsi
  550. mov %rax,152($context) # restore context->Rsp
  551. mov %rsi,168($context) # restore context->Rsi
  552. mov %rdi,176($context) # restore context->Rdi
  553. mov 40($disp),%rdi # disp->ContextRecord
  554. mov $context,%rsi # context
  555. mov \$154,%ecx # sizeof(CONTEXT)
  556. .long 0xa548f3fc # cld; rep movsq
  557. mov $disp,%rsi
  558. xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
  559. mov 8(%rsi),%rdx # arg2, disp->ImageBase
  560. mov 0(%rsi),%r8 # arg3, disp->ControlPc
  561. mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
  562. mov 40(%rsi),%r10 # disp->ContextRecord
  563. lea 56(%rsi),%r11 # &disp->HandlerData
  564. lea 24(%rsi),%r12 # &disp->EstablisherFrame
  565. mov %r10,32(%rsp) # arg5
  566. mov %r11,40(%rsp) # arg6
  567. mov %r12,48(%rsp) # arg7
  568. mov %rcx,56(%rsp) # arg8, (NULL)
  569. call *__imp_RtlVirtualUnwind(%rip)
  570. mov \$1,%eax # ExceptionContinueSearch
  571. add \$64,%rsp
  572. popfq
  573. pop %r15
  574. pop %r14
  575. pop %r13
  576. pop %r12
  577. pop %rbp
  578. pop %rbx
  579. pop %rdi
  580. pop %rsi
  581. ret
  582. .size se_handler,.-se_handler
  583. .section .pdata
  584. .align 4
  585. .rva .LSEH_begin_$func
  586. .rva .LSEH_end_$func
  587. .rva .LSEH_info_$func
  588. .section .xdata
  589. .align 8
  590. .LSEH_info_$func:
  591. .byte 9,0,0,0
  592. .rva se_handler
  593. ___
  594. }
  595. sub reg_part {
  596. my ($reg,$conv)=@_;
  597. if ($reg =~ /%r[0-9]+/) { $reg .= $conv; }
  598. elsif ($conv eq "b") { $reg =~ s/%[er]([^x]+)x?/%$1l/; }
  599. elsif ($conv eq "w") { $reg =~ s/%[er](.+)/%$1/; }
  600. elsif ($conv eq "d") { $reg =~ s/%[er](.+)/%e$1/; }
  601. return $reg;
  602. }
  603. $code =~ s/(%[a-z0-9]+)#([bwd])/reg_part($1,$2)/gem;
  604. $code =~ s/\`([^\`]*)\`/eval $1/gem;
  605. $code =~ s/pinsrw\s+\$0,/movd /gm;
  606. $code =~ s/#md5#//gm if ($md5);
  607. $code =~ s/#rc4#//gm if ($rc4);
  608. print $code;
  609. close STDOUT or die "error closing STDOUT: $!";