rc4-md5-x86_64.pl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. #! /usr/bin/env perl
  2. # Copyright 2011-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the OpenSSL license (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. # 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. my $flavour = shift;
  53. my $output = shift;
  54. if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
  55. my $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
  56. $0 =~ m/(.*[\/\\])[^\/\\]+$/; my $dir=$1; my $xlate;
  57. ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
  58. ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
  59. die "can't locate x86_64-xlate.pl";
  60. open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\"";
  61. *STDOUT=*OUT;
  62. my ($dat,$in0,$out,$ctx,$inp,$len, $func,$nargs);
  63. if ($rc4 && !$md5) {
  64. ($dat,$len,$in0,$out) = ("%rdi","%rsi","%rdx","%rcx");
  65. $func="RC4"; $nargs=4;
  66. } elsif ($md5 && !$rc4) {
  67. ($ctx,$inp,$len) = ("%rdi","%rsi","%rdx");
  68. $func="md5_block_asm_data_order"; $nargs=3;
  69. } else {
  70. ($dat,$in0,$out,$ctx,$inp,$len) = ("%rdi","%rsi","%rdx","%rcx","%r8","%r9");
  71. $func="rc4_md5_enc"; $nargs=6;
  72. # void rc4_md5_enc(
  73. # RC4_KEY *key, #
  74. # const void *in0, # RC4 input
  75. # void *out, # RC4 output
  76. # MD5_CTX *ctx, #
  77. # const void *inp, # MD5 input
  78. # size_t len); # number of 64-byte blocks
  79. }
  80. my @K=( 0xd76aa478,0xe8c7b756,0x242070db,0xc1bdceee,
  81. 0xf57c0faf,0x4787c62a,0xa8304613,0xfd469501,
  82. 0x698098d8,0x8b44f7af,0xffff5bb1,0x895cd7be,
  83. 0x6b901122,0xfd987193,0xa679438e,0x49b40821,
  84. 0xf61e2562,0xc040b340,0x265e5a51,0xe9b6c7aa,
  85. 0xd62f105d,0x02441453,0xd8a1e681,0xe7d3fbc8,
  86. 0x21e1cde6,0xc33707d6,0xf4d50d87,0x455a14ed,
  87. 0xa9e3e905,0xfcefa3f8,0x676f02d9,0x8d2a4c8a,
  88. 0xfffa3942,0x8771f681,0x6d9d6122,0xfde5380c,
  89. 0xa4beea44,0x4bdecfa9,0xf6bb4b60,0xbebfbc70,
  90. 0x289b7ec6,0xeaa127fa,0xd4ef3085,0x04881d05,
  91. 0xd9d4d039,0xe6db99e5,0x1fa27cf8,0xc4ac5665,
  92. 0xf4292244,0x432aff97,0xab9423a7,0xfc93a039,
  93. 0x655b59c3,0x8f0ccc92,0xffeff47d,0x85845dd1,
  94. 0x6fa87e4f,0xfe2ce6e0,0xa3014314,0x4e0811a1,
  95. 0xf7537e82,0xbd3af235,0x2ad7d2bb,0xeb86d391 );
  96. my @V=("%r8d","%r9d","%r10d","%r11d"); # MD5 registers
  97. my $tmp="%r12d";
  98. my @XX=("%rbp","%rsi"); # RC4 registers
  99. my @TX=("%rax","%rbx");
  100. my $YY="%rcx";
  101. my $TY="%rdx";
  102. my $MOD=32; # 16, 32 or 64
  103. $code.=<<___;
  104. .text
  105. .align 16
  106. .globl $func
  107. .type $func,\@function,$nargs
  108. $func:
  109. .cfi_startproc
  110. cmp \$0,$len
  111. je .Labort
  112. push %rbx
  113. .cfi_push %rbx
  114. push %rbp
  115. .cfi_push %rbp
  116. push %r12
  117. .cfi_push %r12
  118. push %r13
  119. .cfi_push %r13
  120. push %r14
  121. .cfi_push %r14
  122. push %r15
  123. .cfi_push %r15
  124. sub \$40,%rsp
  125. .cfi_adjust_cfa_offset 40
  126. .Lbody:
  127. ___
  128. if ($rc4) {
  129. $code.=<<___;
  130. $D#md5# mov $ctx,%r11 # reassign arguments
  131. mov $len,%r12
  132. mov $in0,%r13
  133. mov $out,%r14
  134. $D#md5# mov $inp,%r15
  135. ___
  136. $ctx="%r11" if ($md5); # reassign arguments
  137. $len="%r12";
  138. $in0="%r13";
  139. $out="%r14";
  140. $inp="%r15" if ($md5);
  141. $inp=$in0 if (!$md5);
  142. $code.=<<___;
  143. xor $XX[0],$XX[0]
  144. xor $YY,$YY
  145. lea 8($dat),$dat
  146. mov -8($dat),$XX[0]#b
  147. mov -4($dat),$YY#b
  148. inc $XX[0]#b
  149. sub $in0,$out
  150. movl ($dat,$XX[0],4),$TX[0]#d
  151. ___
  152. $code.=<<___ if (!$md5);
  153. xor $TX[1],$TX[1]
  154. test \$-128,$len
  155. jz .Loop1
  156. sub $XX[0],$TX[1]
  157. and \$`$MOD-1`,$TX[1]
  158. jz .Loop${MOD}_is_hot
  159. sub $TX[1],$len
  160. .Loop${MOD}_warmup:
  161. add $TX[0]#b,$YY#b
  162. movl ($dat,$YY,4),$TY#d
  163. movl $TX[0]#d,($dat,$YY,4)
  164. movl $TY#d,($dat,$XX[0],4)
  165. add $TY#b,$TX[0]#b
  166. inc $XX[0]#b
  167. movl ($dat,$TX[0],4),$TY#d
  168. movl ($dat,$XX[0],4),$TX[0]#d
  169. xorb ($in0),$TY#b
  170. movb $TY#b,($out,$in0)
  171. lea 1($in0),$in0
  172. dec $TX[1]
  173. jnz .Loop${MOD}_warmup
  174. mov $YY,$TX[1]
  175. xor $YY,$YY
  176. mov $TX[1]#b,$YY#b
  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. #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
  203. .align 16
  204. .Loop:
  205. #md5# mov $V[0],0*4(%rsp) # put aside current hash value
  206. #md5# mov $V[1],1*4(%rsp)
  207. #md5# mov $V[2],2*4(%rsp)
  208. #md5# mov $V[3],$tmp # forward reference
  209. #md5# mov $V[3],3*4(%rsp)
  210. ___
  211. sub R0 {
  212. my ($i,$a,$b,$c,$d)=@_;
  213. my @rot0=(7,12,17,22);
  214. my $j=$i%16;
  215. my $k=$i%$MOD;
  216. my $xmm="%xmm".($j&1);
  217. $code.=" movdqu ($in0),%xmm2\n" if ($rc4 && $j==15);
  218. $code.=" add \$$MOD,$XX[0]#b\n" if ($rc4 && $j==15 && $k==$MOD-1);
  219. $code.=" pxor $xmm,$xmm\n" if ($rc4 && $j<=1);
  220. $code.=<<___;
  221. #rc4# movl ($dat,$YY,4),$TY#d
  222. #md5# xor $c,$tmp
  223. #rc4# movl $TX[0]#d,($dat,$YY,4)
  224. #md5# and $b,$tmp
  225. #md5# add 4*`$j`($inp),$a
  226. #rc4# add $TY#b,$TX[0]#b
  227. #rc4# movl `4*(($k+1)%$MOD)`(`$k==$MOD-1?"$dat,$XX[0],4":"$XX[1]"`),$TX[1]#d
  228. #md5# add \$$K[$i],$a
  229. #md5# xor $d,$tmp
  230. #rc4# movz $TX[0]#b,$TX[0]#d
  231. #rc4# movl $TY#d,4*$k($XX[1])
  232. #md5# add $tmp,$a
  233. #rc4# add $TX[1]#b,$YY#b
  234. #md5# rol \$$rot0[$j%4],$a
  235. #md5# mov `$j==15?"$b":"$c"`,$tmp # forward reference
  236. #rc4# pinsrw \$`($j>>1)&7`,($dat,$TX[0],4),$xmm\n
  237. #md5# add $b,$a
  238. ___
  239. $code.=<<___ if ($rc4 && $j==15 && $k==$MOD-1);
  240. mov $YY,$XX[1]
  241. xor $YY,$YY # keyword to partial register
  242. mov $XX[1]#b,$YY#b
  243. lea ($dat,$XX[0],4),$XX[1]
  244. ___
  245. $code.=<<___ if ($rc4 && $j==15);
  246. psllq \$8,%xmm1
  247. pxor %xmm0,%xmm2
  248. pxor %xmm1,%xmm2
  249. ___
  250. }
  251. sub R1 {
  252. my ($i,$a,$b,$c,$d)=@_;
  253. my @rot1=(5,9,14,20);
  254. my $j=$i%16;
  255. my $k=$i%$MOD;
  256. my $xmm="%xmm".($j&1);
  257. $code.=" movdqu 16($in0),%xmm3\n" if ($rc4 && $j==15);
  258. $code.=" add \$$MOD,$XX[0]#b\n" if ($rc4 && $j==15 && $k==$MOD-1);
  259. $code.=" pxor $xmm,$xmm\n" if ($rc4 && $j<=1);
  260. $code.=<<___;
  261. #rc4# movl ($dat,$YY,4),$TY#d
  262. #md5# xor $b,$tmp
  263. #rc4# movl $TX[0]#d,($dat,$YY,4)
  264. #md5# and $d,$tmp
  265. #md5# add 4*`((1+5*$j)%16)`($inp),$a
  266. #rc4# add $TY#b,$TX[0]#b
  267. #rc4# movl `4*(($k+1)%$MOD)`(`$k==$MOD-1?"$dat,$XX[0],4":"$XX[1]"`),$TX[1]#d
  268. #md5# add \$$K[$i],$a
  269. #md5# xor $c,$tmp
  270. #rc4# movz $TX[0]#b,$TX[0]#d
  271. #rc4# movl $TY#d,4*$k($XX[1])
  272. #md5# add $tmp,$a
  273. #rc4# add $TX[1]#b,$YY#b
  274. #md5# rol \$$rot1[$j%4],$a
  275. #md5# mov `$j==15?"$c":"$b"`,$tmp # forward reference
  276. #rc4# pinsrw \$`($j>>1)&7`,($dat,$TX[0],4),$xmm\n
  277. #md5# add $b,$a
  278. ___
  279. $code.=<<___ if ($rc4 && $j==15 && $k==$MOD-1);
  280. mov $YY,$XX[1]
  281. xor $YY,$YY # keyword to partial register
  282. mov $XX[1]#b,$YY#b
  283. lea ($dat,$XX[0],4),$XX[1]
  284. ___
  285. $code.=<<___ if ($rc4 && $j==15);
  286. psllq \$8,%xmm1
  287. pxor %xmm0,%xmm3
  288. pxor %xmm1,%xmm3
  289. ___
  290. }
  291. sub R2 {
  292. my ($i,$a,$b,$c,$d)=@_;
  293. my @rot2=(4,11,16,23);
  294. my $j=$i%16;
  295. my $k=$i%$MOD;
  296. my $xmm="%xmm".($j&1);
  297. $code.=" movdqu 32($in0),%xmm4\n" if ($rc4 && $j==15);
  298. $code.=" add \$$MOD,$XX[0]#b\n" if ($rc4 && $j==15 && $k==$MOD-1);
  299. $code.=" pxor $xmm,$xmm\n" if ($rc4 && $j<=1);
  300. $code.=<<___;
  301. #rc4# movl ($dat,$YY,4),$TY#d
  302. #md5# xor $c,$tmp
  303. #rc4# movl $TX[0]#d,($dat,$YY,4)
  304. #md5# xor $b,$tmp
  305. #md5# add 4*`((5+3*$j)%16)`($inp),$a
  306. #rc4# add $TY#b,$TX[0]#b
  307. #rc4# movl `4*(($k+1)%$MOD)`(`$k==$MOD-1?"$dat,$XX[0],4":"$XX[1]"`),$TX[1]#d
  308. #md5# add \$$K[$i],$a
  309. #rc4# movz $TX[0]#b,$TX[0]#d
  310. #md5# add $tmp,$a
  311. #rc4# movl $TY#d,4*$k($XX[1])
  312. #rc4# add $TX[1]#b,$YY#b
  313. #md5# rol \$$rot2[$j%4],$a
  314. #md5# mov `$j==15?"\\\$-1":"$c"`,$tmp # forward reference
  315. #rc4# pinsrw \$`($j>>1)&7`,($dat,$TX[0],4),$xmm\n
  316. #md5# add $b,$a
  317. ___
  318. $code.=<<___ if ($rc4 && $j==15 && $k==$MOD-1);
  319. mov $YY,$XX[1]
  320. xor $YY,$YY # keyword to partial register
  321. mov $XX[1]#b,$YY#b
  322. lea ($dat,$XX[0],4),$XX[1]
  323. ___
  324. $code.=<<___ if ($rc4 && $j==15);
  325. psllq \$8,%xmm1
  326. pxor %xmm0,%xmm4
  327. pxor %xmm1,%xmm4
  328. ___
  329. }
  330. sub R3 {
  331. my ($i,$a,$b,$c,$d)=@_;
  332. my @rot3=(6,10,15,21);
  333. my $j=$i%16;
  334. my $k=$i%$MOD;
  335. my $xmm="%xmm".($j&1);
  336. $code.=" movdqu 48($in0),%xmm5\n" if ($rc4 && $j==15);
  337. $code.=" add \$$MOD,$XX[0]#b\n" if ($rc4 && $j==15 && $k==$MOD-1);
  338. $code.=" pxor $xmm,$xmm\n" if ($rc4 && $j<=1);
  339. $code.=<<___;
  340. #rc4# movl ($dat,$YY,4),$TY#d
  341. #md5# xor $d,$tmp
  342. #rc4# movl $TX[0]#d,($dat,$YY,4)
  343. #md5# or $b,$tmp
  344. #md5# add 4*`((7*$j)%16)`($inp),$a
  345. #rc4# add $TY#b,$TX[0]#b
  346. #rc4# movl `4*(($k+1)%$MOD)`(`$k==$MOD-1?"$dat,$XX[0],4":"$XX[1]"`),$TX[1]#d
  347. #md5# add \$$K[$i],$a
  348. #rc4# movz $TX[0]#b,$TX[0]#d
  349. #md5# xor $c,$tmp
  350. #rc4# movl $TY#d,4*$k($XX[1])
  351. #md5# add $tmp,$a
  352. #rc4# add $TX[1]#b,$YY#b
  353. #md5# rol \$$rot3[$j%4],$a
  354. #md5# mov \$-1,$tmp # forward reference
  355. #rc4# pinsrw \$`($j>>1)&7`,($dat,$TX[0],4),$xmm\n
  356. #md5# add $b,$a
  357. ___
  358. $code.=<<___ if ($rc4 && $j==15);
  359. mov $XX[0],$XX[1]
  360. xor $XX[0],$XX[0] # keyword to partial register
  361. mov $XX[1]#b,$XX[0]#b
  362. mov $YY,$XX[1]
  363. xor $YY,$YY # keyword to partial register
  364. mov $XX[1]#b,$YY#b
  365. lea ($dat,$XX[0],4),$XX[1]
  366. psllq \$8,%xmm1
  367. pxor %xmm0,%xmm5
  368. pxor %xmm1,%xmm5
  369. ___
  370. }
  371. my $i=0;
  372. for(;$i<16;$i++) { R0($i,@V); unshift(@V,pop(@V)); push(@TX,shift(@TX)); }
  373. for(;$i<32;$i++) { R1($i,@V); unshift(@V,pop(@V)); push(@TX,shift(@TX)); }
  374. for(;$i<48;$i++) { R2($i,@V); unshift(@V,pop(@V)); push(@TX,shift(@TX)); }
  375. for(;$i<64;$i++) { R3($i,@V); unshift(@V,pop(@V)); push(@TX,shift(@TX)); }
  376. $code.=<<___;
  377. #md5# add 0*4(%rsp),$V[0] # accumulate hash value
  378. #md5# add 1*4(%rsp),$V[1]
  379. #md5# add 2*4(%rsp),$V[2]
  380. #md5# add 3*4(%rsp),$V[3]
  381. #rc4# movdqu %xmm2,($out,$in0) # write RC4 output
  382. #rc4# movdqu %xmm3,16($out,$in0)
  383. #rc4# movdqu %xmm4,32($out,$in0)
  384. #rc4# movdqu %xmm5,48($out,$in0)
  385. #md5# lea 64($inp),$inp
  386. #rc4# lea 64($in0),$in0
  387. cmp 16(%rsp),$inp # are we done?
  388. jb .Loop
  389. #md5# mov 24(%rsp),$len # restore pointer to MD5_CTX
  390. #rc4# sub $TX[0]#b,$YY#b # correct $YY
  391. #md5# mov $V[0],0*4($len) # write MD5_CTX
  392. #md5# mov $V[1],1*4($len)
  393. #md5# mov $V[2],2*4($len)
  394. #md5# mov $V[3],3*4($len)
  395. ___
  396. $code.=<<___ if ($rc4 && (!$md5 || $D));
  397. mov 32(%rsp),$len # restore original $len
  398. and \$63,$len # remaining bytes
  399. jnz .Loop1
  400. jmp .Ldone
  401. .align 16
  402. .Loop1:
  403. add $TX[0]#b,$YY#b
  404. movl ($dat,$YY,4),$TY#d
  405. movl $TX[0]#d,($dat,$YY,4)
  406. movl $TY#d,($dat,$XX[0],4)
  407. add $TY#b,$TX[0]#b
  408. inc $XX[0]#b
  409. movl ($dat,$TX[0],4),$TY#d
  410. movl ($dat,$XX[0],4),$TX[0]#d
  411. xorb ($in0),$TY#b
  412. movb $TY#b,($out,$in0)
  413. lea 1($in0),$in0
  414. dec $len
  415. jnz .Loop1
  416. .Ldone:
  417. ___
  418. $code.=<<___;
  419. #rc4# sub \$1,$XX[0]#b
  420. #rc4# movl $XX[0]#d,-8($dat)
  421. #rc4# movl $YY#d,-4($dat)
  422. mov 40(%rsp),%r15
  423. .cfi_restore %r15
  424. mov 48(%rsp),%r14
  425. .cfi_restore %r14
  426. mov 56(%rsp),%r13
  427. .cfi_restore %r13
  428. mov 64(%rsp),%r12
  429. .cfi_restore %r12
  430. mov 72(%rsp),%rbp
  431. .cfi_restore %rbp
  432. mov 80(%rsp),%rbx
  433. .cfi_restore %rbx
  434. lea 88(%rsp),%rsp
  435. .cfi_adjust_cfa_offset -88
  436. .Lepilogue:
  437. .Labort:
  438. ret
  439. .cfi_endproc
  440. .size $func,.-$func
  441. ___
  442. if ($rc4 && $D) { # sole purpose of this section is to provide
  443. # option to use the generated module as drop-in
  444. # replacement for rc4-x86_64.pl for debugging
  445. # and testing purposes...
  446. my ($idx,$ido)=("%r8","%r9");
  447. my ($dat,$len,$inp)=("%rdi","%rsi","%rdx");
  448. $code.=<<___;
  449. .globl RC4_set_key
  450. .type RC4_set_key,\@function,3
  451. .align 16
  452. RC4_set_key:
  453. .cfi_startproc
  454. lea 8($dat),$dat
  455. lea ($inp,$len),$inp
  456. neg $len
  457. mov $len,%rcx
  458. xor %eax,%eax
  459. xor $ido,$ido
  460. xor %r10,%r10
  461. xor %r11,%r11
  462. jmp .Lw1stloop
  463. .align 16
  464. .Lw1stloop:
  465. mov %eax,($dat,%rax,4)
  466. add \$1,%al
  467. jnc .Lw1stloop
  468. xor $ido,$ido
  469. xor $idx,$idx
  470. .align 16
  471. .Lw2ndloop:
  472. mov ($dat,$ido,4),%r10d
  473. add ($inp,$len,1),$idx#b
  474. add %r10b,$idx#b
  475. add \$1,$len
  476. mov ($dat,$idx,4),%r11d
  477. cmovz %rcx,$len
  478. mov %r10d,($dat,$idx,4)
  479. mov %r11d,($dat,$ido,4)
  480. add \$1,$ido#b
  481. jnc .Lw2ndloop
  482. xor %eax,%eax
  483. mov %eax,-8($dat)
  484. mov %eax,-4($dat)
  485. ret
  486. .cfi_endproc
  487. .size RC4_set_key,.-RC4_set_key
  488. .globl RC4_options
  489. .type RC4_options,\@abi-omnipotent
  490. .align 16
  491. RC4_options:
  492. lea .Lopts(%rip),%rax
  493. ret
  494. .align 64
  495. .Lopts:
  496. .asciz "rc4(64x,int)"
  497. .align 64
  498. .size RC4_options,.-RC4_options
  499. ___
  500. }
  501. # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
  502. # CONTEXT *context,DISPATCHER_CONTEXT *disp)
  503. if ($win64) {
  504. my $rec="%rcx";
  505. my $frame="%rdx";
  506. my $context="%r8";
  507. my $disp="%r9";
  508. $code.=<<___;
  509. .extern __imp_RtlVirtualUnwind
  510. .type se_handler,\@abi-omnipotent
  511. .align 16
  512. se_handler:
  513. push %rsi
  514. push %rdi
  515. push %rbx
  516. push %rbp
  517. push %r12
  518. push %r13
  519. push %r14
  520. push %r15
  521. pushfq
  522. sub \$64,%rsp
  523. mov 120($context),%rax # pull context->Rax
  524. mov 248($context),%rbx # pull context->Rip
  525. lea .Lbody(%rip),%r10
  526. cmp %r10,%rbx # context->Rip<.Lbody
  527. jb .Lin_prologue
  528. mov 152($context),%rax # pull context->Rsp
  529. lea .Lepilogue(%rip),%r10
  530. cmp %r10,%rbx # context->Rip>=.Lepilogue
  531. jae .Lin_prologue
  532. mov 40(%rax),%r15
  533. mov 48(%rax),%r14
  534. mov 56(%rax),%r13
  535. mov 64(%rax),%r12
  536. mov 72(%rax),%rbp
  537. mov 80(%rax),%rbx
  538. lea 88(%rax),%rax
  539. mov %rbx,144($context) # restore context->Rbx
  540. mov %rbp,160($context) # restore context->Rbp
  541. mov %r12,216($context) # restore context->R12
  542. mov %r13,224($context) # restore context->R12
  543. mov %r14,232($context) # restore context->R14
  544. mov %r15,240($context) # restore context->R15
  545. .Lin_prologue:
  546. mov 8(%rax),%rdi
  547. mov 16(%rax),%rsi
  548. mov %rax,152($context) # restore context->Rsp
  549. mov %rsi,168($context) # restore context->Rsi
  550. mov %rdi,176($context) # restore context->Rdi
  551. mov 40($disp),%rdi # disp->ContextRecord
  552. mov $context,%rsi # context
  553. mov \$154,%ecx # sizeof(CONTEXT)
  554. .long 0xa548f3fc # cld; rep movsq
  555. mov $disp,%rsi
  556. xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
  557. mov 8(%rsi),%rdx # arg2, disp->ImageBase
  558. mov 0(%rsi),%r8 # arg3, disp->ControlPc
  559. mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
  560. mov 40(%rsi),%r10 # disp->ContextRecord
  561. lea 56(%rsi),%r11 # &disp->HandlerData
  562. lea 24(%rsi),%r12 # &disp->EstablisherFrame
  563. mov %r10,32(%rsp) # arg5
  564. mov %r11,40(%rsp) # arg6
  565. mov %r12,48(%rsp) # arg7
  566. mov %rcx,56(%rsp) # arg8, (NULL)
  567. call *__imp_RtlVirtualUnwind(%rip)
  568. mov \$1,%eax # ExceptionContinueSearch
  569. add \$64,%rsp
  570. popfq
  571. pop %r15
  572. pop %r14
  573. pop %r13
  574. pop %r12
  575. pop %rbp
  576. pop %rbx
  577. pop %rdi
  578. pop %rsi
  579. ret
  580. .size se_handler,.-se_handler
  581. .section .pdata
  582. .align 4
  583. .rva .LSEH_begin_$func
  584. .rva .LSEH_end_$func
  585. .rva .LSEH_info_$func
  586. .section .xdata
  587. .align 8
  588. .LSEH_info_$func:
  589. .byte 9,0,0,0
  590. .rva se_handler
  591. ___
  592. }
  593. sub reg_part {
  594. my ($reg,$conv)=@_;
  595. if ($reg =~ /%r[0-9]+/) { $reg .= $conv; }
  596. elsif ($conv eq "b") { $reg =~ s/%[er]([^x]+)x?/%$1l/; }
  597. elsif ($conv eq "w") { $reg =~ s/%[er](.+)/%$1/; }
  598. elsif ($conv eq "d") { $reg =~ s/%[er](.+)/%e$1/; }
  599. return $reg;
  600. }
  601. $code =~ s/(%[a-z0-9]+)#([bwd])/reg_part($1,$2)/gem;
  602. $code =~ s/\`([^\`]*)\`/eval $1/gem;
  603. $code =~ s/pinsrw\s+\$0,/movd /gm;
  604. $code =~ s/#md5#//gm if ($md5);
  605. $code =~ s/#rc4#//gm if ($rc4);
  606. print $code;
  607. close STDOUT or die "error closing STDOUT: $!";