rc4-586.pl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. #!/usr/bin/env perl
  2. # ====================================================================
  3. # [Re]written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
  4. # project. The module is, however, dual licensed under OpenSSL and
  5. # CRYPTOGAMS licenses depending on where you obtain it. For further
  6. # details see http://www.openssl.org/~appro/cryptogams/.
  7. # ====================================================================
  8. # At some point it became apparent that the original SSLeay RC4
  9. # assembler implementation performs suboptimally on latest IA-32
  10. # microarchitectures. After re-tuning performance has changed as
  11. # following:
  12. #
  13. # Pentium -10%
  14. # Pentium III +12%
  15. # AMD +50%(*)
  16. # P4 +250%(**)
  17. #
  18. # (*) This number is actually a trade-off:-) It's possible to
  19. # achieve +72%, but at the cost of -48% off PIII performance.
  20. # In other words code performing further 13% faster on AMD
  21. # would perform almost 2 times slower on Intel PIII...
  22. # For reference! This code delivers ~80% of rc4-amd64.pl
  23. # performance on the same Opteron machine.
  24. # (**) This number requires compressed key schedule set up by
  25. # RC4_set_key [see commentary below for further details].
  26. #
  27. # <appro@fy.chalmers.se>
  28. # May 2011
  29. #
  30. # Optimize for Core2 and Westmere [and incidentally Opteron]. Current
  31. # performance in cycles per processed byte (less is better) and
  32. # improvement relative to previous version of this module is:
  33. #
  34. # Pentium 10.2 # original numbers
  35. # Pentium III 7.8(*)
  36. # Intel P4 7.5
  37. #
  38. # Opteron 6.1/+20% # new MMX numbers
  39. # Core2 5.3/+67%(**)
  40. # Westmere 5.1/+94%(**)
  41. # Sandy Bridge 5.0/+8%
  42. # Atom 12.6/+6%
  43. #
  44. # (*) PIII can actually deliver 6.6 cycles per byte with MMX code,
  45. # but this specific code performs poorly on Core2. And vice
  46. # versa, below MMX/SSE code delivering 5.8/7.1 on Core2 performs
  47. # poorly on PIII, at 8.0/14.5:-( As PIII is not a "hot" CPU
  48. # [anymore], I chose to discard PIII-specific code path and opt
  49. # for original IALU-only code, which is why MMX/SSE code path
  50. # is guarded by SSE2 bit (see below), not MMX/SSE.
  51. # (**) Performance vs. block size on Core2 and Westmere had a maximum
  52. # at ... 64 bytes block size. And it was quite a maximum, 40-60%
  53. # in comparison to largest 8KB block size. Above improvement
  54. # coefficients are for the largest block size.
  55. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  56. push(@INC,"${dir}","${dir}../../perlasm");
  57. require "x86asm.pl";
  58. &asm_init($ARGV[0],"rc4-586.pl",$x86only = $ARGV[$#ARGV] eq "386");
  59. $xx="eax";
  60. $yy="ebx";
  61. $tx="ecx";
  62. $ty="edx";
  63. $inp="esi";
  64. $out="ebp";
  65. $dat="edi";
  66. sub RC4_loop {
  67. my $i=shift;
  68. my $func = ($i==0)?*mov:*or;
  69. &add (&LB($yy),&LB($tx));
  70. &mov ($ty,&DWP(0,$dat,$yy,4));
  71. &mov (&DWP(0,$dat,$yy,4),$tx);
  72. &mov (&DWP(0,$dat,$xx,4),$ty);
  73. &add ($ty,$tx);
  74. &inc (&LB($xx));
  75. &and ($ty,0xff);
  76. &ror ($out,8) if ($i!=0);
  77. if ($i<3) {
  78. &mov ($tx,&DWP(0,$dat,$xx,4));
  79. } else {
  80. &mov ($tx,&wparam(3)); # reload [re-biased] out
  81. }
  82. &$func ($out,&DWP(0,$dat,$ty,4));
  83. }
  84. if ($alt=0) {
  85. # >20% faster on Atom and Sandy Bridge[!], 8% faster on Opteron,
  86. # but ~40% slower on Core2 and Westmere... Attempt to add movz
  87. # brings down Opteron by 25%, Atom and Sandy Bridge by 15%, yet
  88. # on Core2 with movz it's almost 20% slower than below alternative
  89. # code... Yes, it's a total mess...
  90. my @XX=($xx,$out);
  91. $RC4_loop_mmx = sub { # SSE actually...
  92. my $i=shift;
  93. my $j=$i<=0?0:$i>>1;
  94. my $mm=$i<=0?"mm0":"mm".($i&1);
  95. &add (&LB($yy),&LB($tx));
  96. &lea (@XX[1],&DWP(1,@XX[0]));
  97. &pxor ("mm2","mm0") if ($i==0);
  98. &psllq ("mm1",8) if ($i==0);
  99. &and (@XX[1],0xff);
  100. &pxor ("mm0","mm0") if ($i<=0);
  101. &mov ($ty,&DWP(0,$dat,$yy,4));
  102. &mov (&DWP(0,$dat,$yy,4),$tx);
  103. &pxor ("mm1","mm2") if ($i==0);
  104. &mov (&DWP(0,$dat,$XX[0],4),$ty);
  105. &add (&LB($ty),&LB($tx));
  106. &movd (@XX[0],"mm7") if ($i==0);
  107. &mov ($tx,&DWP(0,$dat,@XX[1],4));
  108. &pxor ("mm1","mm1") if ($i==1);
  109. &movq ("mm2",&QWP(0,$inp)) if ($i==1);
  110. &movq (&QWP(-8,(@XX[0],$inp)),"mm1") if ($i==0);
  111. &pinsrw ($mm,&DWP(0,$dat,$ty,4),$j);
  112. push (@XX,shift(@XX)) if ($i>=0);
  113. }
  114. } else {
  115. # Using pinsrw here improves performane on Intel CPUs by 2-3%, but
  116. # brings down AMD by 7%...
  117. $RC4_loop_mmx = sub {
  118. my $i=shift;
  119. &add (&LB($yy),&LB($tx));
  120. &psllq ("mm1",8*(($i-1)&7)) if (abs($i)!=1);
  121. &mov ($ty,&DWP(0,$dat,$yy,4));
  122. &mov (&DWP(0,$dat,$yy,4),$tx);
  123. &mov (&DWP(0,$dat,$xx,4),$ty);
  124. &inc ($xx);
  125. &add ($ty,$tx);
  126. &movz ($xx,&LB($xx)); # (*)
  127. &movz ($ty,&LB($ty)); # (*)
  128. &pxor ("mm2",$i==1?"mm0":"mm1") if ($i>=0);
  129. &movq ("mm0",&QWP(0,$inp)) if ($i<=0);
  130. &movq (&QWP(-8,($out,$inp)),"mm2") if ($i==0);
  131. &mov ($tx,&DWP(0,$dat,$xx,4));
  132. &movd ($i>0?"mm1":"mm2",&DWP(0,$dat,$ty,4));
  133. # (*) This is the key to Core2 and Westmere performance.
  134. # Whithout movz out-of-order execution logic confuses
  135. # itself and fails to reorder loads and stores. Problem
  136. # appears to be fixed in Sandy Bridge...
  137. }
  138. }
  139. &external_label("OPENSSL_ia32cap_P");
  140. # void RC4(RC4_KEY *key,size_t len,const unsigned char *inp,unsigned char *out);
  141. &function_begin("RC4");
  142. &mov ($dat,&wparam(0)); # load key schedule pointer
  143. &mov ($ty, &wparam(1)); # load len
  144. &mov ($inp,&wparam(2)); # load inp
  145. &mov ($out,&wparam(3)); # load out
  146. &xor ($xx,$xx); # avoid partial register stalls
  147. &xor ($yy,$yy);
  148. &cmp ($ty,0); # safety net
  149. &je (&label("abort"));
  150. &mov (&LB($xx),&BP(0,$dat)); # load key->x
  151. &mov (&LB($yy),&BP(4,$dat)); # load key->y
  152. &add ($dat,8);
  153. &lea ($tx,&DWP(0,$inp,$ty));
  154. &sub ($out,$inp); # re-bias out
  155. &mov (&wparam(1),$tx); # save input+len
  156. &inc (&LB($xx));
  157. # detect compressed key schedule...
  158. &cmp (&DWP(256,$dat),-1);
  159. &je (&label("RC4_CHAR"));
  160. &mov ($tx,&DWP(0,$dat,$xx,4));
  161. &and ($ty,-4); # how many 4-byte chunks?
  162. &jz (&label("loop1"));
  163. &mov (&wparam(3),$out); # $out as accumulator in these loops
  164. if ($x86only) {
  165. &jmp (&label("go4loop4"));
  166. } else {
  167. &test ($ty,-8);
  168. &jz (&label("go4loop4"));
  169. &picmeup($out,"OPENSSL_ia32cap_P");
  170. &bt (&DWP(0,$out),26); # check SSE2 bit [could have been MMX]
  171. &jnc (&label("go4loop4"));
  172. &mov ($out,&wparam(3)) if (!$alt);
  173. &movd ("mm7",&wparam(3)) if ($alt);
  174. &and ($ty,-8);
  175. &lea ($ty,&DWP(-8,$inp,$ty));
  176. &mov (&DWP(-4,$dat),$ty); # save input+(len/8)*8-8
  177. &$RC4_loop_mmx(-1);
  178. &jmp(&label("loop_mmx_enter"));
  179. &set_label("loop_mmx",16);
  180. &$RC4_loop_mmx(0);
  181. &set_label("loop_mmx_enter");
  182. for ($i=1;$i<8;$i++) { &$RC4_loop_mmx($i); }
  183. &mov ($ty,$yy);
  184. &xor ($yy,$yy); # this is second key to Core2
  185. &mov (&LB($yy),&LB($ty)); # and Westmere performance...
  186. &cmp ($inp,&DWP(-4,$dat));
  187. &lea ($inp,&DWP(8,$inp));
  188. &jb (&label("loop_mmx"));
  189. if ($alt) {
  190. &movd ($out,"mm7");
  191. &pxor ("mm2","mm0");
  192. &psllq ("mm1",8);
  193. &pxor ("mm1","mm2");
  194. &movq (&QWP(-8,$out,$inp),"mm1");
  195. } else {
  196. &psllq ("mm1",56);
  197. &pxor ("mm2","mm1");
  198. &movq (&QWP(-8,$out,$inp),"mm2");
  199. }
  200. &emms ();
  201. &cmp ($inp,&wparam(1)); # compare to input+len
  202. &je (&label("done"));
  203. &jmp (&label("loop1"));
  204. }
  205. &set_label("go4loop4",16);
  206. &lea ($ty,&DWP(-4,$inp,$ty));
  207. &mov (&wparam(2),$ty); # save input+(len/4)*4-4
  208. &set_label("loop4");
  209. for ($i=0;$i<4;$i++) { RC4_loop($i); }
  210. &ror ($out,8);
  211. &xor ($out,&DWP(0,$inp));
  212. &cmp ($inp,&wparam(2)); # compare to input+(len/4)*4-4
  213. &mov (&DWP(0,$tx,$inp),$out);# $tx holds re-biased out here
  214. &lea ($inp,&DWP(4,$inp));
  215. &mov ($tx,&DWP(0,$dat,$xx,4));
  216. &jb (&label("loop4"));
  217. &cmp ($inp,&wparam(1)); # compare to input+len
  218. &je (&label("done"));
  219. &mov ($out,&wparam(3)); # restore $out
  220. &set_label("loop1",16);
  221. &add (&LB($yy),&LB($tx));
  222. &mov ($ty,&DWP(0,$dat,$yy,4));
  223. &mov (&DWP(0,$dat,$yy,4),$tx);
  224. &mov (&DWP(0,$dat,$xx,4),$ty);
  225. &add ($ty,$tx);
  226. &inc (&LB($xx));
  227. &and ($ty,0xff);
  228. &mov ($ty,&DWP(0,$dat,$ty,4));
  229. &xor (&LB($ty),&BP(0,$inp));
  230. &lea ($inp,&DWP(1,$inp));
  231. &mov ($tx,&DWP(0,$dat,$xx,4));
  232. &cmp ($inp,&wparam(1)); # compare to input+len
  233. &mov (&BP(-1,$out,$inp),&LB($ty));
  234. &jb (&label("loop1"));
  235. &jmp (&label("done"));
  236. # this is essentially Intel P4 specific codepath...
  237. &set_label("RC4_CHAR",16);
  238. &movz ($tx,&BP(0,$dat,$xx));
  239. # strangely enough unrolled loop performs over 20% slower...
  240. &set_label("cloop1");
  241. &add (&LB($yy),&LB($tx));
  242. &movz ($ty,&BP(0,$dat,$yy));
  243. &mov (&BP(0,$dat,$yy),&LB($tx));
  244. &mov (&BP(0,$dat,$xx),&LB($ty));
  245. &add (&LB($ty),&LB($tx));
  246. &movz ($ty,&BP(0,$dat,$ty));
  247. &add (&LB($xx),1);
  248. &xor (&LB($ty),&BP(0,$inp));
  249. &lea ($inp,&DWP(1,$inp));
  250. &movz ($tx,&BP(0,$dat,$xx));
  251. &cmp ($inp,&wparam(1));
  252. &mov (&BP(-1,$out,$inp),&LB($ty));
  253. &jb (&label("cloop1"));
  254. &set_label("done");
  255. &dec (&LB($xx));
  256. &mov (&DWP(-4,$dat),$yy); # save key->y
  257. &mov (&BP(-8,$dat),&LB($xx)); # save key->x
  258. &set_label("abort");
  259. &function_end("RC4");
  260. ########################################################################
  261. $inp="esi";
  262. $out="edi";
  263. $idi="ebp";
  264. $ido="ecx";
  265. $idx="edx";
  266. # void RC4_set_key(RC4_KEY *key,int len,const unsigned char *data);
  267. &function_begin("private_RC4_set_key");
  268. &mov ($out,&wparam(0)); # load key
  269. &mov ($idi,&wparam(1)); # load len
  270. &mov ($inp,&wparam(2)); # load data
  271. &picmeup($idx,"OPENSSL_ia32cap_P");
  272. &lea ($out,&DWP(2*4,$out)); # &key->data
  273. &lea ($inp,&DWP(0,$inp,$idi)); # $inp to point at the end
  274. &neg ($idi);
  275. &xor ("eax","eax");
  276. &mov (&DWP(-4,$out),$idi); # borrow key->y
  277. &bt (&DWP(0,$idx),20); # check for bit#20
  278. &jc (&label("c1stloop"));
  279. &set_label("w1stloop",16);
  280. &mov (&DWP(0,$out,"eax",4),"eax"); # key->data[i]=i;
  281. &add (&LB("eax"),1); # i++;
  282. &jnc (&label("w1stloop"));
  283. &xor ($ido,$ido);
  284. &xor ($idx,$idx);
  285. &set_label("w2ndloop",16);
  286. &mov ("eax",&DWP(0,$out,$ido,4));
  287. &add (&LB($idx),&BP(0,$inp,$idi));
  288. &add (&LB($idx),&LB("eax"));
  289. &add ($idi,1);
  290. &mov ("ebx",&DWP(0,$out,$idx,4));
  291. &jnz (&label("wnowrap"));
  292. &mov ($idi,&DWP(-4,$out));
  293. &set_label("wnowrap");
  294. &mov (&DWP(0,$out,$idx,4),"eax");
  295. &mov (&DWP(0,$out,$ido,4),"ebx");
  296. &add (&LB($ido),1);
  297. &jnc (&label("w2ndloop"));
  298. &jmp (&label("exit"));
  299. # Unlike all other x86 [and x86_64] implementations, Intel P4 core
  300. # [including EM64T] was found to perform poorly with above "32-bit" key
  301. # schedule, a.k.a. RC4_INT. Performance improvement for IA-32 hand-coded
  302. # assembler turned out to be 3.5x if re-coded for compressed 8-bit one,
  303. # a.k.a. RC4_CHAR! It's however inappropriate to just switch to 8-bit
  304. # schedule for x86[_64], because non-P4 implementations suffer from
  305. # significant performance losses then, e.g. PIII exhibits >2x
  306. # deterioration, and so does Opteron. In order to assure optimal
  307. # all-round performance, we detect P4 at run-time and set up compressed
  308. # key schedule, which is recognized by RC4 procedure.
  309. &set_label("c1stloop",16);
  310. &mov (&BP(0,$out,"eax"),&LB("eax")); # key->data[i]=i;
  311. &add (&LB("eax"),1); # i++;
  312. &jnc (&label("c1stloop"));
  313. &xor ($ido,$ido);
  314. &xor ($idx,$idx);
  315. &xor ("ebx","ebx");
  316. &set_label("c2ndloop",16);
  317. &mov (&LB("eax"),&BP(0,$out,$ido));
  318. &add (&LB($idx),&BP(0,$inp,$idi));
  319. &add (&LB($idx),&LB("eax"));
  320. &add ($idi,1);
  321. &mov (&LB("ebx"),&BP(0,$out,$idx));
  322. &jnz (&label("cnowrap"));
  323. &mov ($idi,&DWP(-4,$out));
  324. &set_label("cnowrap");
  325. &mov (&BP(0,$out,$idx),&LB("eax"));
  326. &mov (&BP(0,$out,$ido),&LB("ebx"));
  327. &add (&LB($ido),1);
  328. &jnc (&label("c2ndloop"));
  329. &mov (&DWP(256,$out),-1); # mark schedule as compressed
  330. &set_label("exit");
  331. &xor ("eax","eax");
  332. &mov (&DWP(-8,$out),"eax"); # key->x=0;
  333. &mov (&DWP(-4,$out),"eax"); # key->y=0;
  334. &function_end("private_RC4_set_key");
  335. # const char *RC4_options(void);
  336. &function_begin_B("RC4_options");
  337. &call (&label("pic_point"));
  338. &set_label("pic_point");
  339. &blindpop("eax");
  340. &lea ("eax",&DWP(&label("opts")."-".&label("pic_point"),"eax"));
  341. &picmeup("edx","OPENSSL_ia32cap_P");
  342. &mov ("edx",&DWP(0,"edx"));
  343. &bt ("edx",20);
  344. &jc (&label("1xchar"));
  345. &bt ("edx",26);
  346. &jnc (&label("ret"));
  347. &add ("eax",25);
  348. &ret ();
  349. &set_label("1xchar");
  350. &add ("eax",12);
  351. &set_label("ret");
  352. &ret ();
  353. &set_label("opts",64);
  354. &asciz ("rc4(4x,int)");
  355. &asciz ("rc4(1x,char)");
  356. &asciz ("rc4(8x,mmx)");
  357. &asciz ("RC4 for x86, CRYPTOGAMS by <appro\@openssl.org>");
  358. &align (64);
  359. &function_end_B("RC4_options");
  360. &asm_finish();