ghash-x86.pl 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  1. #!/usr/bin/env perl
  2. #
  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. # ====================================================================
  9. #
  10. # March, May, June 2010
  11. #
  12. # The module implements "4-bit" GCM GHASH function and underlying
  13. # single multiplication operation in GF(2^128). "4-bit" means that it
  14. # uses 256 bytes per-key table [+64/128 bytes fixed table]. It has two
  15. # code paths: vanilla x86 and vanilla MMX. Former will be executed on
  16. # 486 and Pentium, latter on all others. MMX GHASH features so called
  17. # "528B" variant of "4-bit" method utilizing additional 256+16 bytes
  18. # of per-key storage [+512 bytes shared table]. Performance results
  19. # are for streamed GHASH subroutine and are expressed in cycles per
  20. # processed byte, less is better:
  21. #
  22. # gcc 2.95.3(*) MMX assembler x86 assembler
  23. #
  24. # Pentium 105/111(**) - 50
  25. # PIII 68 /75 12.2 24
  26. # P4 125/125 17.8 84(***)
  27. # Opteron 66 /70 10.1 30
  28. # Core2 54 /67 8.4 18
  29. #
  30. # (*) gcc 3.4.x was observed to generate few percent slower code,
  31. # which is one of reasons why 2.95.3 results were chosen,
  32. # another reason is lack of 3.4.x results for older CPUs;
  33. # comparison with MMX results is not completely fair, because C
  34. # results are for vanilla "256B" implementation, while
  35. # assembler results are for "528B";-)
  36. # (**) second number is result for code compiled with -fPIC flag,
  37. # which is actually more relevant, because assembler code is
  38. # position-independent;
  39. # (***) see comment in non-MMX routine for further details;
  40. #
  41. # To summarize, it's >2-5 times faster than gcc-generated code. To
  42. # anchor it to something else SHA1 assembler processes one byte in
  43. # 11-13 cycles on contemporary x86 cores. As for choice of MMX in
  44. # particular, see comment at the end of the file...
  45. # May 2010
  46. #
  47. # Add PCLMULQDQ version performing at 2.10 cycles per processed byte.
  48. # The question is how close is it to theoretical limit? The pclmulqdq
  49. # instruction latency appears to be 14 cycles and there can't be more
  50. # than 2 of them executing at any given time. This means that single
  51. # Karatsuba multiplication would take 28 cycles *plus* few cycles for
  52. # pre- and post-processing. Then multiplication has to be followed by
  53. # modulo-reduction. Given that aggregated reduction method [see
  54. # "Carry-less Multiplication and Its Usage for Computing the GCM Mode"
  55. # white paper by Intel] allows you to perform reduction only once in
  56. # a while we can assume that asymptotic performance can be estimated
  57. # as (28+Tmod/Naggr)/16, where Tmod is time to perform reduction
  58. # and Naggr is the aggregation factor.
  59. #
  60. # Before we proceed to this implementation let's have closer look at
  61. # the best-performing code suggested by Intel in their white paper.
  62. # By tracing inter-register dependencies Tmod is estimated as ~19
  63. # cycles and Naggr chosen by Intel is 4, resulting in 2.05 cycles per
  64. # processed byte. As implied, this is quite optimistic estimate,
  65. # because it does not account for Karatsuba pre- and post-processing,
  66. # which for a single multiplication is ~5 cycles. Unfortunately Intel
  67. # does not provide performance data for GHASH alone. But benchmarking
  68. # AES_GCM_encrypt ripped out of Fig. 15 of the white paper with aadt
  69. # alone resulted in 2.46 cycles per byte of out 16KB buffer. Note that
  70. # the result accounts even for pre-computing of degrees of the hash
  71. # key H, but its portion is negligible at 16KB buffer size.
  72. #
  73. # Moving on to the implementation in question. Tmod is estimated as
  74. # ~13 cycles and Naggr is 2, giving asymptotic performance of ...
  75. # 2.16. How is it possible that measured performance is better than
  76. # optimistic theoretical estimate? There is one thing Intel failed
  77. # to recognize. By serializing GHASH with CTR in same subroutine
  78. # former's performance is really limited to above (Tmul + Tmod/Naggr)
  79. # equation. But if GHASH procedure is detached, the modulo-reduction
  80. # can be interleaved with Naggr-1 multiplications at instruction level
  81. # and under ideal conditions even disappear from the equation. So that
  82. # optimistic theoretical estimate for this implementation is ...
  83. # 28/16=1.75, and not 2.16. Well, it's probably way too optimistic,
  84. # at least for such small Naggr. I'd argue that (28+Tproc/Naggr),
  85. # where Tproc is time required for Karatsuba pre- and post-processing,
  86. # is more realistic estimate. In this case it gives ... 1.91 cycles.
  87. # Or in other words, depending on how well we can interleave reduction
  88. # and one of the two multiplications the performance should be betwen
  89. # 1.91 and 2.16. As already mentioned, this implementation processes
  90. # one byte out of 8KB buffer in 2.10 cycles, while x86_64 counterpart
  91. # - in 2.02. x86_64 performance is better, because larger register
  92. # bank allows to interleave reduction and multiplication better.
  93. #
  94. # Does it make sense to increase Naggr? To start with it's virtually
  95. # impossible in 32-bit mode, because of limited register bank
  96. # capacity. Otherwise improvement has to be weighed agiainst slower
  97. # setup, as well as code size and complexity increase. As even
  98. # optimistic estimate doesn't promise 30% performance improvement,
  99. # there are currently no plans to increase Naggr.
  100. #
  101. # Special thanks to David Woodhouse <dwmw2@infradead.org> for
  102. # providing access to a Westmere-based system on behalf of Intel
  103. # Open Source Technology Centre.
  104. # January 2010
  105. #
  106. # Tweaked to optimize transitions between integer and FP operations
  107. # on same XMM register, PCLMULQDQ subroutine was measured to process
  108. # one byte in 2.07 cycles on Sandy Bridge, and in 2.12 - on Westmere.
  109. # The minor regression on Westmere is outweighed by ~15% improvement
  110. # on Sandy Bridge. Strangely enough attempt to modify 64-bit code in
  111. # similar manner resulted in almost 20% degradation on Sandy Bridge,
  112. # where original 64-bit code processes one byte in 1.95 cycles.
  113. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  114. push(@INC,"${dir}","${dir}../../perlasm");
  115. require "x86asm.pl";
  116. &asm_init($ARGV[0],"ghash-x86.pl",$x86only = $ARGV[$#ARGV] eq "386");
  117. $sse2=0;
  118. for (@ARGV) { $sse2=1 if (/-DOPENSSL_IA32_SSE2/); }
  119. ($Zhh,$Zhl,$Zlh,$Zll) = ("ebp","edx","ecx","ebx");
  120. $inp = "edi";
  121. $Htbl = "esi";
  122. $unroll = 0; # Affects x86 loop. Folded loop performs ~7% worse
  123. # than unrolled, which has to be weighted against
  124. # 2.5x x86-specific code size reduction.
  125. sub x86_loop {
  126. my $off = shift;
  127. my $rem = "eax";
  128. &mov ($Zhh,&DWP(4,$Htbl,$Zll));
  129. &mov ($Zhl,&DWP(0,$Htbl,$Zll));
  130. &mov ($Zlh,&DWP(12,$Htbl,$Zll));
  131. &mov ($Zll,&DWP(8,$Htbl,$Zll));
  132. &xor ($rem,$rem); # avoid partial register stalls on PIII
  133. # shrd practically kills P4, 2.5x deterioration, but P4 has
  134. # MMX code-path to execute. shrd runs tad faster [than twice
  135. # the shifts, move's and or's] on pre-MMX Pentium (as well as
  136. # PIII and Core2), *but* minimizes code size, spares register
  137. # and thus allows to fold the loop...
  138. if (!$unroll) {
  139. my $cnt = $inp;
  140. &mov ($cnt,15);
  141. &jmp (&label("x86_loop"));
  142. &set_label("x86_loop",16);
  143. for($i=1;$i<=2;$i++) {
  144. &mov (&LB($rem),&LB($Zll));
  145. &shrd ($Zll,$Zlh,4);
  146. &and (&LB($rem),0xf);
  147. &shrd ($Zlh,$Zhl,4);
  148. &shrd ($Zhl,$Zhh,4);
  149. &shr ($Zhh,4);
  150. &xor ($Zhh,&DWP($off+16,"esp",$rem,4));
  151. &mov (&LB($rem),&BP($off,"esp",$cnt));
  152. if ($i&1) {
  153. &and (&LB($rem),0xf0);
  154. } else {
  155. &shl (&LB($rem),4);
  156. }
  157. &xor ($Zll,&DWP(8,$Htbl,$rem));
  158. &xor ($Zlh,&DWP(12,$Htbl,$rem));
  159. &xor ($Zhl,&DWP(0,$Htbl,$rem));
  160. &xor ($Zhh,&DWP(4,$Htbl,$rem));
  161. if ($i&1) {
  162. &dec ($cnt);
  163. &js (&label("x86_break"));
  164. } else {
  165. &jmp (&label("x86_loop"));
  166. }
  167. }
  168. &set_label("x86_break",16);
  169. } else {
  170. for($i=1;$i<32;$i++) {
  171. &comment($i);
  172. &mov (&LB($rem),&LB($Zll));
  173. &shrd ($Zll,$Zlh,4);
  174. &and (&LB($rem),0xf);
  175. &shrd ($Zlh,$Zhl,4);
  176. &shrd ($Zhl,$Zhh,4);
  177. &shr ($Zhh,4);
  178. &xor ($Zhh,&DWP($off+16,"esp",$rem,4));
  179. if ($i&1) {
  180. &mov (&LB($rem),&BP($off+15-($i>>1),"esp"));
  181. &and (&LB($rem),0xf0);
  182. } else {
  183. &mov (&LB($rem),&BP($off+15-($i>>1),"esp"));
  184. &shl (&LB($rem),4);
  185. }
  186. &xor ($Zll,&DWP(8,$Htbl,$rem));
  187. &xor ($Zlh,&DWP(12,$Htbl,$rem));
  188. &xor ($Zhl,&DWP(0,$Htbl,$rem));
  189. &xor ($Zhh,&DWP(4,$Htbl,$rem));
  190. }
  191. }
  192. &bswap ($Zll);
  193. &bswap ($Zlh);
  194. &bswap ($Zhl);
  195. if (!$x86only) {
  196. &bswap ($Zhh);
  197. } else {
  198. &mov ("eax",$Zhh);
  199. &bswap ("eax");
  200. &mov ($Zhh,"eax");
  201. }
  202. }
  203. if ($unroll) {
  204. &function_begin_B("_x86_gmult_4bit_inner");
  205. &x86_loop(4);
  206. &ret ();
  207. &function_end_B("_x86_gmult_4bit_inner");
  208. }
  209. sub deposit_rem_4bit {
  210. my $bias = shift;
  211. &mov (&DWP($bias+0, "esp"),0x0000<<16);
  212. &mov (&DWP($bias+4, "esp"),0x1C20<<16);
  213. &mov (&DWP($bias+8, "esp"),0x3840<<16);
  214. &mov (&DWP($bias+12,"esp"),0x2460<<16);
  215. &mov (&DWP($bias+16,"esp"),0x7080<<16);
  216. &mov (&DWP($bias+20,"esp"),0x6CA0<<16);
  217. &mov (&DWP($bias+24,"esp"),0x48C0<<16);
  218. &mov (&DWP($bias+28,"esp"),0x54E0<<16);
  219. &mov (&DWP($bias+32,"esp"),0xE100<<16);
  220. &mov (&DWP($bias+36,"esp"),0xFD20<<16);
  221. &mov (&DWP($bias+40,"esp"),0xD940<<16);
  222. &mov (&DWP($bias+44,"esp"),0xC560<<16);
  223. &mov (&DWP($bias+48,"esp"),0x9180<<16);
  224. &mov (&DWP($bias+52,"esp"),0x8DA0<<16);
  225. &mov (&DWP($bias+56,"esp"),0xA9C0<<16);
  226. &mov (&DWP($bias+60,"esp"),0xB5E0<<16);
  227. }
  228. $suffix = $x86only ? "" : "_x86";
  229. &function_begin("gcm_gmult_4bit".$suffix);
  230. &stack_push(16+4+1); # +1 for stack alignment
  231. &mov ($inp,&wparam(0)); # load Xi
  232. &mov ($Htbl,&wparam(1)); # load Htable
  233. &mov ($Zhh,&DWP(0,$inp)); # load Xi[16]
  234. &mov ($Zhl,&DWP(4,$inp));
  235. &mov ($Zlh,&DWP(8,$inp));
  236. &mov ($Zll,&DWP(12,$inp));
  237. &deposit_rem_4bit(16);
  238. &mov (&DWP(0,"esp"),$Zhh); # copy Xi[16] on stack
  239. &mov (&DWP(4,"esp"),$Zhl);
  240. &mov (&DWP(8,"esp"),$Zlh);
  241. &mov (&DWP(12,"esp"),$Zll);
  242. &shr ($Zll,20);
  243. &and ($Zll,0xf0);
  244. if ($unroll) {
  245. &call ("_x86_gmult_4bit_inner");
  246. } else {
  247. &x86_loop(0);
  248. &mov ($inp,&wparam(0));
  249. }
  250. &mov (&DWP(12,$inp),$Zll);
  251. &mov (&DWP(8,$inp),$Zlh);
  252. &mov (&DWP(4,$inp),$Zhl);
  253. &mov (&DWP(0,$inp),$Zhh);
  254. &stack_pop(16+4+1);
  255. &function_end("gcm_gmult_4bit".$suffix);
  256. &function_begin("gcm_ghash_4bit".$suffix);
  257. &stack_push(16+4+1); # +1 for 64-bit alignment
  258. &mov ($Zll,&wparam(0)); # load Xi
  259. &mov ($Htbl,&wparam(1)); # load Htable
  260. &mov ($inp,&wparam(2)); # load in
  261. &mov ("ecx",&wparam(3)); # load len
  262. &add ("ecx",$inp);
  263. &mov (&wparam(3),"ecx");
  264. &mov ($Zhh,&DWP(0,$Zll)); # load Xi[16]
  265. &mov ($Zhl,&DWP(4,$Zll));
  266. &mov ($Zlh,&DWP(8,$Zll));
  267. &mov ($Zll,&DWP(12,$Zll));
  268. &deposit_rem_4bit(16);
  269. &set_label("x86_outer_loop",16);
  270. &xor ($Zll,&DWP(12,$inp)); # xor with input
  271. &xor ($Zlh,&DWP(8,$inp));
  272. &xor ($Zhl,&DWP(4,$inp));
  273. &xor ($Zhh,&DWP(0,$inp));
  274. &mov (&DWP(12,"esp"),$Zll); # dump it on stack
  275. &mov (&DWP(8,"esp"),$Zlh);
  276. &mov (&DWP(4,"esp"),$Zhl);
  277. &mov (&DWP(0,"esp"),$Zhh);
  278. &shr ($Zll,20);
  279. &and ($Zll,0xf0);
  280. if ($unroll) {
  281. &call ("_x86_gmult_4bit_inner");
  282. } else {
  283. &x86_loop(0);
  284. &mov ($inp,&wparam(2));
  285. }
  286. &lea ($inp,&DWP(16,$inp));
  287. &cmp ($inp,&wparam(3));
  288. &mov (&wparam(2),$inp) if (!$unroll);
  289. &jb (&label("x86_outer_loop"));
  290. &mov ($inp,&wparam(0)); # load Xi
  291. &mov (&DWP(12,$inp),$Zll);
  292. &mov (&DWP(8,$inp),$Zlh);
  293. &mov (&DWP(4,$inp),$Zhl);
  294. &mov (&DWP(0,$inp),$Zhh);
  295. &stack_pop(16+4+1);
  296. &function_end("gcm_ghash_4bit".$suffix);
  297. if (!$x86only) {{{
  298. &static_label("rem_4bit");
  299. if (0) {{ # "May" MMX version is kept for reference...
  300. $S=12; # shift factor for rem_4bit
  301. &function_begin_B("_mmx_gmult_4bit_inner");
  302. # MMX version performs 3.5 times better on P4 (see comment in non-MMX
  303. # routine for further details), 100% better on Opteron, ~70% better
  304. # on Core2 and PIII... In other words effort is considered to be well
  305. # spent... Since initial release the loop was unrolled in order to
  306. # "liberate" register previously used as loop counter. Instead it's
  307. # used to optimize critical path in 'Z.hi ^= rem_4bit[Z.lo&0xf]'.
  308. # The path involves move of Z.lo from MMX to integer register,
  309. # effective address calculation and finally merge of value to Z.hi.
  310. # Reference to rem_4bit is scheduled so late that I had to >>4
  311. # rem_4bit elements. This resulted in 20-45% procent improvement
  312. # on contemporary µ-archs.
  313. {
  314. my $cnt;
  315. my $rem_4bit = "eax";
  316. my @rem = ($Zhh,$Zll);
  317. my $nhi = $Zhl;
  318. my $nlo = $Zlh;
  319. my ($Zlo,$Zhi) = ("mm0","mm1");
  320. my $tmp = "mm2";
  321. &xor ($nlo,$nlo); # avoid partial register stalls on PIII
  322. &mov ($nhi,$Zll);
  323. &mov (&LB($nlo),&LB($nhi));
  324. &shl (&LB($nlo),4);
  325. &and ($nhi,0xf0);
  326. &movq ($Zlo,&QWP(8,$Htbl,$nlo));
  327. &movq ($Zhi,&QWP(0,$Htbl,$nlo));
  328. &movd ($rem[0],$Zlo);
  329. for ($cnt=28;$cnt>=-2;$cnt--) {
  330. my $odd = $cnt&1;
  331. my $nix = $odd ? $nlo : $nhi;
  332. &shl (&LB($nlo),4) if ($odd);
  333. &psrlq ($Zlo,4);
  334. &movq ($tmp,$Zhi);
  335. &psrlq ($Zhi,4);
  336. &pxor ($Zlo,&QWP(8,$Htbl,$nix));
  337. &mov (&LB($nlo),&BP($cnt/2,$inp)) if (!$odd && $cnt>=0);
  338. &psllq ($tmp,60);
  339. &and ($nhi,0xf0) if ($odd);
  340. &pxor ($Zhi,&QWP(0,$rem_4bit,$rem[1],8)) if ($cnt<28);
  341. &and ($rem[0],0xf);
  342. &pxor ($Zhi,&QWP(0,$Htbl,$nix));
  343. &mov ($nhi,$nlo) if (!$odd && $cnt>=0);
  344. &movd ($rem[1],$Zlo);
  345. &pxor ($Zlo,$tmp);
  346. push (@rem,shift(@rem)); # "rotate" registers
  347. }
  348. &mov ($inp,&DWP(4,$rem_4bit,$rem[1],8)); # last rem_4bit[rem]
  349. &psrlq ($Zlo,32); # lower part of Zlo is already there
  350. &movd ($Zhl,$Zhi);
  351. &psrlq ($Zhi,32);
  352. &movd ($Zlh,$Zlo);
  353. &movd ($Zhh,$Zhi);
  354. &shl ($inp,4); # compensate for rem_4bit[i] being >>4
  355. &bswap ($Zll);
  356. &bswap ($Zhl);
  357. &bswap ($Zlh);
  358. &xor ($Zhh,$inp);
  359. &bswap ($Zhh);
  360. &ret ();
  361. }
  362. &function_end_B("_mmx_gmult_4bit_inner");
  363. &function_begin("gcm_gmult_4bit_mmx");
  364. &mov ($inp,&wparam(0)); # load Xi
  365. &mov ($Htbl,&wparam(1)); # load Htable
  366. &call (&label("pic_point"));
  367. &set_label("pic_point");
  368. &blindpop("eax");
  369. &lea ("eax",&DWP(&label("rem_4bit")."-".&label("pic_point"),"eax"));
  370. &movz ($Zll,&BP(15,$inp));
  371. &call ("_mmx_gmult_4bit_inner");
  372. &mov ($inp,&wparam(0)); # load Xi
  373. &emms ();
  374. &mov (&DWP(12,$inp),$Zll);
  375. &mov (&DWP(4,$inp),$Zhl);
  376. &mov (&DWP(8,$inp),$Zlh);
  377. &mov (&DWP(0,$inp),$Zhh);
  378. &function_end("gcm_gmult_4bit_mmx");
  379. # Streamed version performs 20% better on P4, 7% on Opteron,
  380. # 10% on Core2 and PIII...
  381. &function_begin("gcm_ghash_4bit_mmx");
  382. &mov ($Zhh,&wparam(0)); # load Xi
  383. &mov ($Htbl,&wparam(1)); # load Htable
  384. &mov ($inp,&wparam(2)); # load in
  385. &mov ($Zlh,&wparam(3)); # load len
  386. &call (&label("pic_point"));
  387. &set_label("pic_point");
  388. &blindpop("eax");
  389. &lea ("eax",&DWP(&label("rem_4bit")."-".&label("pic_point"),"eax"));
  390. &add ($Zlh,$inp);
  391. &mov (&wparam(3),$Zlh); # len to point at the end of input
  392. &stack_push(4+1); # +1 for stack alignment
  393. &mov ($Zll,&DWP(12,$Zhh)); # load Xi[16]
  394. &mov ($Zhl,&DWP(4,$Zhh));
  395. &mov ($Zlh,&DWP(8,$Zhh));
  396. &mov ($Zhh,&DWP(0,$Zhh));
  397. &jmp (&label("mmx_outer_loop"));
  398. &set_label("mmx_outer_loop",16);
  399. &xor ($Zll,&DWP(12,$inp));
  400. &xor ($Zhl,&DWP(4,$inp));
  401. &xor ($Zlh,&DWP(8,$inp));
  402. &xor ($Zhh,&DWP(0,$inp));
  403. &mov (&wparam(2),$inp);
  404. &mov (&DWP(12,"esp"),$Zll);
  405. &mov (&DWP(4,"esp"),$Zhl);
  406. &mov (&DWP(8,"esp"),$Zlh);
  407. &mov (&DWP(0,"esp"),$Zhh);
  408. &mov ($inp,"esp");
  409. &shr ($Zll,24);
  410. &call ("_mmx_gmult_4bit_inner");
  411. &mov ($inp,&wparam(2));
  412. &lea ($inp,&DWP(16,$inp));
  413. &cmp ($inp,&wparam(3));
  414. &jb (&label("mmx_outer_loop"));
  415. &mov ($inp,&wparam(0)); # load Xi
  416. &emms ();
  417. &mov (&DWP(12,$inp),$Zll);
  418. &mov (&DWP(4,$inp),$Zhl);
  419. &mov (&DWP(8,$inp),$Zlh);
  420. &mov (&DWP(0,$inp),$Zhh);
  421. &stack_pop(4+1);
  422. &function_end("gcm_ghash_4bit_mmx");
  423. }} else {{ # "June" MMX version...
  424. # ... has slower "April" gcm_gmult_4bit_mmx with folded
  425. # loop. This is done to conserve code size...
  426. $S=16; # shift factor for rem_4bit
  427. sub mmx_loop() {
  428. # MMX version performs 2.8 times better on P4 (see comment in non-MMX
  429. # routine for further details), 40% better on Opteron and Core2, 50%
  430. # better on PIII... In other words effort is considered to be well
  431. # spent...
  432. my $inp = shift;
  433. my $rem_4bit = shift;
  434. my $cnt = $Zhh;
  435. my $nhi = $Zhl;
  436. my $nlo = $Zlh;
  437. my $rem = $Zll;
  438. my ($Zlo,$Zhi) = ("mm0","mm1");
  439. my $tmp = "mm2";
  440. &xor ($nlo,$nlo); # avoid partial register stalls on PIII
  441. &mov ($nhi,$Zll);
  442. &mov (&LB($nlo),&LB($nhi));
  443. &mov ($cnt,14);
  444. &shl (&LB($nlo),4);
  445. &and ($nhi,0xf0);
  446. &movq ($Zlo,&QWP(8,$Htbl,$nlo));
  447. &movq ($Zhi,&QWP(0,$Htbl,$nlo));
  448. &movd ($rem,$Zlo);
  449. &jmp (&label("mmx_loop"));
  450. &set_label("mmx_loop",16);
  451. &psrlq ($Zlo,4);
  452. &and ($rem,0xf);
  453. &movq ($tmp,$Zhi);
  454. &psrlq ($Zhi,4);
  455. &pxor ($Zlo,&QWP(8,$Htbl,$nhi));
  456. &mov (&LB($nlo),&BP(0,$inp,$cnt));
  457. &psllq ($tmp,60);
  458. &pxor ($Zhi,&QWP(0,$rem_4bit,$rem,8));
  459. &dec ($cnt);
  460. &movd ($rem,$Zlo);
  461. &pxor ($Zhi,&QWP(0,$Htbl,$nhi));
  462. &mov ($nhi,$nlo);
  463. &pxor ($Zlo,$tmp);
  464. &js (&label("mmx_break"));
  465. &shl (&LB($nlo),4);
  466. &and ($rem,0xf);
  467. &psrlq ($Zlo,4);
  468. &and ($nhi,0xf0);
  469. &movq ($tmp,$Zhi);
  470. &psrlq ($Zhi,4);
  471. &pxor ($Zlo,&QWP(8,$Htbl,$nlo));
  472. &psllq ($tmp,60);
  473. &pxor ($Zhi,&QWP(0,$rem_4bit,$rem,8));
  474. &movd ($rem,$Zlo);
  475. &pxor ($Zhi,&QWP(0,$Htbl,$nlo));
  476. &pxor ($Zlo,$tmp);
  477. &jmp (&label("mmx_loop"));
  478. &set_label("mmx_break",16);
  479. &shl (&LB($nlo),4);
  480. &and ($rem,0xf);
  481. &psrlq ($Zlo,4);
  482. &and ($nhi,0xf0);
  483. &movq ($tmp,$Zhi);
  484. &psrlq ($Zhi,4);
  485. &pxor ($Zlo,&QWP(8,$Htbl,$nlo));
  486. &psllq ($tmp,60);
  487. &pxor ($Zhi,&QWP(0,$rem_4bit,$rem,8));
  488. &movd ($rem,$Zlo);
  489. &pxor ($Zhi,&QWP(0,$Htbl,$nlo));
  490. &pxor ($Zlo,$tmp);
  491. &psrlq ($Zlo,4);
  492. &and ($rem,0xf);
  493. &movq ($tmp,$Zhi);
  494. &psrlq ($Zhi,4);
  495. &pxor ($Zlo,&QWP(8,$Htbl,$nhi));
  496. &psllq ($tmp,60);
  497. &pxor ($Zhi,&QWP(0,$rem_4bit,$rem,8));
  498. &movd ($rem,$Zlo);
  499. &pxor ($Zhi,&QWP(0,$Htbl,$nhi));
  500. &pxor ($Zlo,$tmp);
  501. &psrlq ($Zlo,32); # lower part of Zlo is already there
  502. &movd ($Zhl,$Zhi);
  503. &psrlq ($Zhi,32);
  504. &movd ($Zlh,$Zlo);
  505. &movd ($Zhh,$Zhi);
  506. &bswap ($Zll);
  507. &bswap ($Zhl);
  508. &bswap ($Zlh);
  509. &bswap ($Zhh);
  510. }
  511. &function_begin("gcm_gmult_4bit_mmx");
  512. &mov ($inp,&wparam(0)); # load Xi
  513. &mov ($Htbl,&wparam(1)); # load Htable
  514. &call (&label("pic_point"));
  515. &set_label("pic_point");
  516. &blindpop("eax");
  517. &lea ("eax",&DWP(&label("rem_4bit")."-".&label("pic_point"),"eax"));
  518. &movz ($Zll,&BP(15,$inp));
  519. &mmx_loop($inp,"eax");
  520. &emms ();
  521. &mov (&DWP(12,$inp),$Zll);
  522. &mov (&DWP(4,$inp),$Zhl);
  523. &mov (&DWP(8,$inp),$Zlh);
  524. &mov (&DWP(0,$inp),$Zhh);
  525. &function_end("gcm_gmult_4bit_mmx");
  526. ######################################################################
  527. # Below subroutine is "528B" variant of "4-bit" GCM GHASH function
  528. # (see gcm128.c for details). It provides further 20-40% performance
  529. # improvement over above mentioned "May" version.
  530. &static_label("rem_8bit");
  531. &function_begin("gcm_ghash_4bit_mmx");
  532. { my ($Zlo,$Zhi) = ("mm7","mm6");
  533. my $rem_8bit = "esi";
  534. my $Htbl = "ebx";
  535. # parameter block
  536. &mov ("eax",&wparam(0)); # Xi
  537. &mov ("ebx",&wparam(1)); # Htable
  538. &mov ("ecx",&wparam(2)); # inp
  539. &mov ("edx",&wparam(3)); # len
  540. &mov ("ebp","esp"); # original %esp
  541. &call (&label("pic_point"));
  542. &set_label ("pic_point");
  543. &blindpop ($rem_8bit);
  544. &lea ($rem_8bit,&DWP(&label("rem_8bit")."-".&label("pic_point"),$rem_8bit));
  545. &sub ("esp",512+16+16); # allocate stack frame...
  546. &and ("esp",-64); # ...and align it
  547. &sub ("esp",16); # place for (u8)(H[]<<4)
  548. &add ("edx","ecx"); # pointer to the end of input
  549. &mov (&DWP(528+16+0,"esp"),"eax"); # save Xi
  550. &mov (&DWP(528+16+8,"esp"),"edx"); # save inp+len
  551. &mov (&DWP(528+16+12,"esp"),"ebp"); # save original %esp
  552. { my @lo = ("mm0","mm1","mm2");
  553. my @hi = ("mm3","mm4","mm5");
  554. my @tmp = ("mm6","mm7");
  555. my $off1=0,$off2=0,$i;
  556. &add ($Htbl,128); # optimize for size
  557. &lea ("edi",&DWP(16+128,"esp"));
  558. &lea ("ebp",&DWP(16+256+128,"esp"));
  559. # decompose Htable (low and high parts are kept separately),
  560. # generate Htable[]>>4, (u8)(Htable[]<<4), save to stack...
  561. for ($i=0;$i<18;$i++) {
  562. &mov ("edx",&DWP(16*$i+8-128,$Htbl)) if ($i<16);
  563. &movq ($lo[0],&QWP(16*$i+8-128,$Htbl)) if ($i<16);
  564. &psllq ($tmp[1],60) if ($i>1);
  565. &movq ($hi[0],&QWP(16*$i+0-128,$Htbl)) if ($i<16);
  566. &por ($lo[2],$tmp[1]) if ($i>1);
  567. &movq (&QWP($off1-128,"edi"),$lo[1]) if ($i>0 && $i<17);
  568. &psrlq ($lo[1],4) if ($i>0 && $i<17);
  569. &movq (&QWP($off1,"edi"),$hi[1]) if ($i>0 && $i<17);
  570. &movq ($tmp[0],$hi[1]) if ($i>0 && $i<17);
  571. &movq (&QWP($off2-128,"ebp"),$lo[2]) if ($i>1);
  572. &psrlq ($hi[1],4) if ($i>0 && $i<17);
  573. &movq (&QWP($off2,"ebp"),$hi[2]) if ($i>1);
  574. &shl ("edx",4) if ($i<16);
  575. &mov (&BP($i,"esp"),&LB("edx")) if ($i<16);
  576. unshift (@lo,pop(@lo)); # "rotate" registers
  577. unshift (@hi,pop(@hi));
  578. unshift (@tmp,pop(@tmp));
  579. $off1 += 8 if ($i>0);
  580. $off2 += 8 if ($i>1);
  581. }
  582. }
  583. &movq ($Zhi,&QWP(0,"eax"));
  584. &mov ("ebx",&DWP(8,"eax"));
  585. &mov ("edx",&DWP(12,"eax")); # load Xi
  586. &set_label("outer",16);
  587. { my $nlo = "eax";
  588. my $dat = "edx";
  589. my @nhi = ("edi","ebp");
  590. my @rem = ("ebx","ecx");
  591. my @red = ("mm0","mm1","mm2");
  592. my $tmp = "mm3";
  593. &xor ($dat,&DWP(12,"ecx")); # merge input data
  594. &xor ("ebx",&DWP(8,"ecx"));
  595. &pxor ($Zhi,&QWP(0,"ecx"));
  596. &lea ("ecx",&DWP(16,"ecx")); # inp+=16
  597. #&mov (&DWP(528+12,"esp"),$dat); # save inp^Xi
  598. &mov (&DWP(528+8,"esp"),"ebx");
  599. &movq (&QWP(528+0,"esp"),$Zhi);
  600. &mov (&DWP(528+16+4,"esp"),"ecx"); # save inp
  601. &xor ($nlo,$nlo);
  602. &rol ($dat,8);
  603. &mov (&LB($nlo),&LB($dat));
  604. &mov ($nhi[1],$nlo);
  605. &and (&LB($nlo),0x0f);
  606. &shr ($nhi[1],4);
  607. &pxor ($red[0],$red[0]);
  608. &rol ($dat,8); # next byte
  609. &pxor ($red[1],$red[1]);
  610. &pxor ($red[2],$red[2]);
  611. # Just like in "May" verson modulo-schedule for critical path in
  612. # 'Z.hi ^= rem_8bit[Z.lo&0xff^((u8)H[nhi]<<4)]<<48'. Final 'pxor'
  613. # is scheduled so late that rem_8bit[] has to be shifted *right*
  614. # by 16, which is why last argument to pinsrw is 2, which
  615. # corresponds to <<32=<<48>>16...
  616. for ($j=11,$i=0;$i<15;$i++) {
  617. if ($i>0) {
  618. &pxor ($Zlo,&QWP(16,"esp",$nlo,8)); # Z^=H[nlo]
  619. &rol ($dat,8); # next byte
  620. &pxor ($Zhi,&QWP(16+128,"esp",$nlo,8));
  621. &pxor ($Zlo,$tmp);
  622. &pxor ($Zhi,&QWP(16+256+128,"esp",$nhi[0],8));
  623. &xor (&LB($rem[1]),&BP(0,"esp",$nhi[0])); # rem^(H[nhi]<<4)
  624. } else {
  625. &movq ($Zlo,&QWP(16,"esp",$nlo,8));
  626. &movq ($Zhi,&QWP(16+128,"esp",$nlo,8));
  627. }
  628. &mov (&LB($nlo),&LB($dat));
  629. &mov ($dat,&DWP(528+$j,"esp")) if (--$j%4==0);
  630. &movd ($rem[0],$Zlo);
  631. &movz ($rem[1],&LB($rem[1])) if ($i>0);
  632. &psrlq ($Zlo,8); # Z>>=8
  633. &movq ($tmp,$Zhi);
  634. &mov ($nhi[0],$nlo);
  635. &psrlq ($Zhi,8);
  636. &pxor ($Zlo,&QWP(16+256+0,"esp",$nhi[1],8)); # Z^=H[nhi]>>4
  637. &and (&LB($nlo),0x0f);
  638. &psllq ($tmp,56);
  639. &pxor ($Zhi,$red[1]) if ($i>1);
  640. &shr ($nhi[0],4);
  641. &pinsrw ($red[0],&WP(0,$rem_8bit,$rem[1],2),2) if ($i>0);
  642. unshift (@red,pop(@red)); # "rotate" registers
  643. unshift (@rem,pop(@rem));
  644. unshift (@nhi,pop(@nhi));
  645. }
  646. &pxor ($Zlo,&QWP(16,"esp",$nlo,8)); # Z^=H[nlo]
  647. &pxor ($Zhi,&QWP(16+128,"esp",$nlo,8));
  648. &xor (&LB($rem[1]),&BP(0,"esp",$nhi[0])); # rem^(H[nhi]<<4)
  649. &pxor ($Zlo,$tmp);
  650. &pxor ($Zhi,&QWP(16+256+128,"esp",$nhi[0],8));
  651. &movz ($rem[1],&LB($rem[1]));
  652. &pxor ($red[2],$red[2]); # clear 2nd word
  653. &psllq ($red[1],4);
  654. &movd ($rem[0],$Zlo);
  655. &psrlq ($Zlo,4); # Z>>=4
  656. &movq ($tmp,$Zhi);
  657. &psrlq ($Zhi,4);
  658. &shl ($rem[0],4); # rem<<4
  659. &pxor ($Zlo,&QWP(16,"esp",$nhi[1],8)); # Z^=H[nhi]
  660. &psllq ($tmp,60);
  661. &movz ($rem[0],&LB($rem[0]));
  662. &pxor ($Zlo,$tmp);
  663. &pxor ($Zhi,&QWP(16+128,"esp",$nhi[1],8));
  664. &pinsrw ($red[0],&WP(0,$rem_8bit,$rem[1],2),2);
  665. &pxor ($Zhi,$red[1]);
  666. &movd ($dat,$Zlo);
  667. &pinsrw ($red[2],&WP(0,$rem_8bit,$rem[0],2),3); # last is <<48
  668. &psllq ($red[0],12); # correct by <<16>>4
  669. &pxor ($Zhi,$red[0]);
  670. &psrlq ($Zlo,32);
  671. &pxor ($Zhi,$red[2]);
  672. &mov ("ecx",&DWP(528+16+4,"esp")); # restore inp
  673. &movd ("ebx",$Zlo);
  674. &movq ($tmp,$Zhi); # 01234567
  675. &psllw ($Zhi,8); # 1.3.5.7.
  676. &psrlw ($tmp,8); # .0.2.4.6
  677. &por ($Zhi,$tmp); # 10325476
  678. &bswap ($dat);
  679. &pshufw ($Zhi,$Zhi,0b00011011); # 76543210
  680. &bswap ("ebx");
  681. &cmp ("ecx",&DWP(528+16+8,"esp")); # are we done?
  682. &jne (&label("outer"));
  683. }
  684. &mov ("eax",&DWP(528+16+0,"esp")); # restore Xi
  685. &mov (&DWP(12,"eax"),"edx");
  686. &mov (&DWP(8,"eax"),"ebx");
  687. &movq (&QWP(0,"eax"),$Zhi);
  688. &mov ("esp",&DWP(528+16+12,"esp")); # restore original %esp
  689. &emms ();
  690. }
  691. &function_end("gcm_ghash_4bit_mmx");
  692. }}
  693. if ($sse2) {{
  694. ######################################################################
  695. # PCLMULQDQ version.
  696. $Xip="eax";
  697. $Htbl="edx";
  698. $const="ecx";
  699. $inp="esi";
  700. $len="ebx";
  701. ($Xi,$Xhi)=("xmm0","xmm1"); $Hkey="xmm2";
  702. ($T1,$T2,$T3)=("xmm3","xmm4","xmm5");
  703. ($Xn,$Xhn)=("xmm6","xmm7");
  704. &static_label("bswap");
  705. sub clmul64x64_T2 { # minimal "register" pressure
  706. my ($Xhi,$Xi,$Hkey)=@_;
  707. &movdqa ($Xhi,$Xi); #
  708. &pshufd ($T1,$Xi,0b01001110);
  709. &pshufd ($T2,$Hkey,0b01001110);
  710. &pxor ($T1,$Xi); #
  711. &pxor ($T2,$Hkey);
  712. &pclmulqdq ($Xi,$Hkey,0x00); #######
  713. &pclmulqdq ($Xhi,$Hkey,0x11); #######
  714. &pclmulqdq ($T1,$T2,0x00); #######
  715. &xorps ($T1,$Xi); #
  716. &xorps ($T1,$Xhi); #
  717. &movdqa ($T2,$T1); #
  718. &psrldq ($T1,8);
  719. &pslldq ($T2,8); #
  720. &pxor ($Xhi,$T1);
  721. &pxor ($Xi,$T2); #
  722. }
  723. sub clmul64x64_T3 {
  724. # Even though this subroutine offers visually better ILP, it
  725. # was empirically found to be a tad slower than above version.
  726. # At least in gcm_ghash_clmul context. But it's just as well,
  727. # because loop modulo-scheduling is possible only thanks to
  728. # minimized "register" pressure...
  729. my ($Xhi,$Xi,$Hkey)=@_;
  730. &movdqa ($T1,$Xi); #
  731. &movdqa ($Xhi,$Xi);
  732. &pclmulqdq ($Xi,$Hkey,0x00); #######
  733. &pclmulqdq ($Xhi,$Hkey,0x11); #######
  734. &pshufd ($T2,$T1,0b01001110); #
  735. &pshufd ($T3,$Hkey,0b01001110);
  736. &pxor ($T2,$T1); #
  737. &pxor ($T3,$Hkey);
  738. &pclmulqdq ($T2,$T3,0x00); #######
  739. &pxor ($T2,$Xi); #
  740. &pxor ($T2,$Xhi); #
  741. &movdqa ($T3,$T2); #
  742. &psrldq ($T2,8);
  743. &pslldq ($T3,8); #
  744. &pxor ($Xhi,$T2);
  745. &pxor ($Xi,$T3); #
  746. }
  747. if (1) { # Algorithm 9 with <<1 twist.
  748. # Reduction is shorter and uses only two
  749. # temporary registers, which makes it better
  750. # candidate for interleaving with 64x64
  751. # multiplication. Pre-modulo-scheduled loop
  752. # was found to be ~20% faster than Algorithm 5
  753. # below. Algorithm 9 was therefore chosen for
  754. # further optimization...
  755. sub reduction_alg9 { # 17/13 times faster than Intel version
  756. my ($Xhi,$Xi) = @_;
  757. # 1st phase
  758. &movdqa ($T1,$Xi) #
  759. &psllq ($Xi,1);
  760. &pxor ($Xi,$T1); #
  761. &psllq ($Xi,5); #
  762. &pxor ($Xi,$T1); #
  763. &psllq ($Xi,57); #
  764. &movdqa ($T2,$Xi); #
  765. &pslldq ($Xi,8);
  766. &psrldq ($T2,8); #
  767. &pxor ($Xi,$T1);
  768. &pxor ($Xhi,$T2); #
  769. # 2nd phase
  770. &movdqa ($T2,$Xi);
  771. &psrlq ($Xi,5);
  772. &pxor ($Xi,$T2); #
  773. &psrlq ($Xi,1); #
  774. &pxor ($Xi,$T2); #
  775. &pxor ($T2,$Xhi);
  776. &psrlq ($Xi,1); #
  777. &pxor ($Xi,$T2); #
  778. }
  779. &function_begin_B("gcm_init_clmul");
  780. &mov ($Htbl,&wparam(0));
  781. &mov ($Xip,&wparam(1));
  782. &call (&label("pic"));
  783. &set_label("pic");
  784. &blindpop ($const);
  785. &lea ($const,&DWP(&label("bswap")."-".&label("pic"),$const));
  786. &movdqu ($Hkey,&QWP(0,$Xip));
  787. &pshufd ($Hkey,$Hkey,0b01001110);# dword swap
  788. # <<1 twist
  789. &pshufd ($T2,$Hkey,0b11111111); # broadcast uppermost dword
  790. &movdqa ($T1,$Hkey);
  791. &psllq ($Hkey,1);
  792. &pxor ($T3,$T3); #
  793. &psrlq ($T1,63);
  794. &pcmpgtd ($T3,$T2); # broadcast carry bit
  795. &pslldq ($T1,8);
  796. &por ($Hkey,$T1); # H<<=1
  797. # magic reduction
  798. &pand ($T3,&QWP(16,$const)); # 0x1c2_polynomial
  799. &pxor ($Hkey,$T3); # if(carry) H^=0x1c2_polynomial
  800. # calculate H^2
  801. &movdqa ($Xi,$Hkey);
  802. &clmul64x64_T2 ($Xhi,$Xi,$Hkey);
  803. &reduction_alg9 ($Xhi,$Xi);
  804. &movdqu (&QWP(0,$Htbl),$Hkey); # save H
  805. &movdqu (&QWP(16,$Htbl),$Xi); # save H^2
  806. &ret ();
  807. &function_end_B("gcm_init_clmul");
  808. &function_begin_B("gcm_gmult_clmul");
  809. &mov ($Xip,&wparam(0));
  810. &mov ($Htbl,&wparam(1));
  811. &call (&label("pic"));
  812. &set_label("pic");
  813. &blindpop ($const);
  814. &lea ($const,&DWP(&label("bswap")."-".&label("pic"),$const));
  815. &movdqu ($Xi,&QWP(0,$Xip));
  816. &movdqa ($T3,&QWP(0,$const));
  817. &movups ($Hkey,&QWP(0,$Htbl));
  818. &pshufb ($Xi,$T3);
  819. &clmul64x64_T2 ($Xhi,$Xi,$Hkey);
  820. &reduction_alg9 ($Xhi,$Xi);
  821. &pshufb ($Xi,$T3);
  822. &movdqu (&QWP(0,$Xip),$Xi);
  823. &ret ();
  824. &function_end_B("gcm_gmult_clmul");
  825. &function_begin("gcm_ghash_clmul");
  826. &mov ($Xip,&wparam(0));
  827. &mov ($Htbl,&wparam(1));
  828. &mov ($inp,&wparam(2));
  829. &mov ($len,&wparam(3));
  830. &call (&label("pic"));
  831. &set_label("pic");
  832. &blindpop ($const);
  833. &lea ($const,&DWP(&label("bswap")."-".&label("pic"),$const));
  834. &movdqu ($Xi,&QWP(0,$Xip));
  835. &movdqa ($T3,&QWP(0,$const));
  836. &movdqu ($Hkey,&QWP(0,$Htbl));
  837. &pshufb ($Xi,$T3);
  838. &sub ($len,0x10);
  839. &jz (&label("odd_tail"));
  840. #######
  841. # Xi+2 =[H*(Ii+1 + Xi+1)] mod P =
  842. # [(H*Ii+1) + (H*Xi+1)] mod P =
  843. # [(H*Ii+1) + H^2*(Ii+Xi)] mod P
  844. #
  845. &movdqu ($T1,&QWP(0,$inp)); # Ii
  846. &movdqu ($Xn,&QWP(16,$inp)); # Ii+1
  847. &pshufb ($T1,$T3);
  848. &pshufb ($Xn,$T3);
  849. &pxor ($Xi,$T1); # Ii+Xi
  850. &clmul64x64_T2 ($Xhn,$Xn,$Hkey); # H*Ii+1
  851. &movups ($Hkey,&QWP(16,$Htbl)); # load H^2
  852. &lea ($inp,&DWP(32,$inp)); # i+=2
  853. &sub ($len,0x20);
  854. &jbe (&label("even_tail"));
  855. &set_label("mod_loop");
  856. &clmul64x64_T2 ($Xhi,$Xi,$Hkey); # H^2*(Ii+Xi)
  857. &movdqu ($T1,&QWP(0,$inp)); # Ii
  858. &movups ($Hkey,&QWP(0,$Htbl)); # load H
  859. &pxor ($Xi,$Xn); # (H*Ii+1) + H^2*(Ii+Xi)
  860. &pxor ($Xhi,$Xhn);
  861. &movdqu ($Xn,&QWP(16,$inp)); # Ii+1
  862. &pshufb ($T1,$T3);
  863. &pshufb ($Xn,$T3);
  864. &movdqa ($T3,$Xn); #&clmul64x64_TX ($Xhn,$Xn,$Hkey); H*Ii+1
  865. &movdqa ($Xhn,$Xn);
  866. &pxor ($Xhi,$T1); # "Ii+Xi", consume early
  867. &movdqa ($T1,$Xi) #&reduction_alg9($Xhi,$Xi); 1st phase
  868. &psllq ($Xi,1);
  869. &pxor ($Xi,$T1); #
  870. &psllq ($Xi,5); #
  871. &pxor ($Xi,$T1); #
  872. &pclmulqdq ($Xn,$Hkey,0x00); #######
  873. &psllq ($Xi,57); #
  874. &movdqa ($T2,$Xi); #
  875. &pslldq ($Xi,8);
  876. &psrldq ($T2,8); #
  877. &pxor ($Xi,$T1);
  878. &pshufd ($T1,$T3,0b01001110);
  879. &pxor ($Xhi,$T2); #
  880. &pxor ($T1,$T3);
  881. &pshufd ($T3,$Hkey,0b01001110);
  882. &pxor ($T3,$Hkey); #
  883. &pclmulqdq ($Xhn,$Hkey,0x11); #######
  884. &movdqa ($T2,$Xi); # 2nd phase
  885. &psrlq ($Xi,5);
  886. &pxor ($Xi,$T2); #
  887. &psrlq ($Xi,1); #
  888. &pxor ($Xi,$T2); #
  889. &pxor ($T2,$Xhi);
  890. &psrlq ($Xi,1); #
  891. &pxor ($Xi,$T2); #
  892. &pclmulqdq ($T1,$T3,0x00); #######
  893. &movups ($Hkey,&QWP(16,$Htbl)); # load H^2
  894. &xorps ($T1,$Xn); #
  895. &xorps ($T1,$Xhn); #
  896. &movdqa ($T3,$T1); #
  897. &psrldq ($T1,8);
  898. &pslldq ($T3,8); #
  899. &pxor ($Xhn,$T1);
  900. &pxor ($Xn,$T3); #
  901. &movdqa ($T3,&QWP(0,$const));
  902. &lea ($inp,&DWP(32,$inp));
  903. &sub ($len,0x20);
  904. &ja (&label("mod_loop"));
  905. &set_label("even_tail");
  906. &clmul64x64_T2 ($Xhi,$Xi,$Hkey); # H^2*(Ii+Xi)
  907. &pxor ($Xi,$Xn); # (H*Ii+1) + H^2*(Ii+Xi)
  908. &pxor ($Xhi,$Xhn);
  909. &reduction_alg9 ($Xhi,$Xi);
  910. &test ($len,$len);
  911. &jnz (&label("done"));
  912. &movups ($Hkey,&QWP(0,$Htbl)); # load H
  913. &set_label("odd_tail");
  914. &movdqu ($T1,&QWP(0,$inp)); # Ii
  915. &pshufb ($T1,$T3);
  916. &pxor ($Xi,$T1); # Ii+Xi
  917. &clmul64x64_T2 ($Xhi,$Xi,$Hkey); # H*(Ii+Xi)
  918. &reduction_alg9 ($Xhi,$Xi);
  919. &set_label("done");
  920. &pshufb ($Xi,$T3);
  921. &movdqu (&QWP(0,$Xip),$Xi);
  922. &function_end("gcm_ghash_clmul");
  923. } else { # Algorith 5. Kept for reference purposes.
  924. sub reduction_alg5 { # 19/16 times faster than Intel version
  925. my ($Xhi,$Xi)=@_;
  926. # <<1
  927. &movdqa ($T1,$Xi); #
  928. &movdqa ($T2,$Xhi);
  929. &pslld ($Xi,1);
  930. &pslld ($Xhi,1); #
  931. &psrld ($T1,31);
  932. &psrld ($T2,31); #
  933. &movdqa ($T3,$T1);
  934. &pslldq ($T1,4);
  935. &psrldq ($T3,12); #
  936. &pslldq ($T2,4);
  937. &por ($Xhi,$T3); #
  938. &por ($Xi,$T1);
  939. &por ($Xhi,$T2); #
  940. # 1st phase
  941. &movdqa ($T1,$Xi);
  942. &movdqa ($T2,$Xi);
  943. &movdqa ($T3,$Xi); #
  944. &pslld ($T1,31);
  945. &pslld ($T2,30);
  946. &pslld ($Xi,25); #
  947. &pxor ($T1,$T2);
  948. &pxor ($T1,$Xi); #
  949. &movdqa ($T2,$T1); #
  950. &pslldq ($T1,12);
  951. &psrldq ($T2,4); #
  952. &pxor ($T3,$T1);
  953. # 2nd phase
  954. &pxor ($Xhi,$T3); #
  955. &movdqa ($Xi,$T3);
  956. &movdqa ($T1,$T3);
  957. &psrld ($Xi,1); #
  958. &psrld ($T1,2);
  959. &psrld ($T3,7); #
  960. &pxor ($Xi,$T1);
  961. &pxor ($Xhi,$T2);
  962. &pxor ($Xi,$T3); #
  963. &pxor ($Xi,$Xhi); #
  964. }
  965. &function_begin_B("gcm_init_clmul");
  966. &mov ($Htbl,&wparam(0));
  967. &mov ($Xip,&wparam(1));
  968. &call (&label("pic"));
  969. &set_label("pic");
  970. &blindpop ($const);
  971. &lea ($const,&DWP(&label("bswap")."-".&label("pic"),$const));
  972. &movdqu ($Hkey,&QWP(0,$Xip));
  973. &pshufd ($Hkey,$Hkey,0b01001110);# dword swap
  974. # calculate H^2
  975. &movdqa ($Xi,$Hkey);
  976. &clmul64x64_T3 ($Xhi,$Xi,$Hkey);
  977. &reduction_alg5 ($Xhi,$Xi);
  978. &movdqu (&QWP(0,$Htbl),$Hkey); # save H
  979. &movdqu (&QWP(16,$Htbl),$Xi); # save H^2
  980. &ret ();
  981. &function_end_B("gcm_init_clmul");
  982. &function_begin_B("gcm_gmult_clmul");
  983. &mov ($Xip,&wparam(0));
  984. &mov ($Htbl,&wparam(1));
  985. &call (&label("pic"));
  986. &set_label("pic");
  987. &blindpop ($const);
  988. &lea ($const,&DWP(&label("bswap")."-".&label("pic"),$const));
  989. &movdqu ($Xi,&QWP(0,$Xip));
  990. &movdqa ($Xn,&QWP(0,$const));
  991. &movdqu ($Hkey,&QWP(0,$Htbl));
  992. &pshufb ($Xi,$Xn);
  993. &clmul64x64_T3 ($Xhi,$Xi,$Hkey);
  994. &reduction_alg5 ($Xhi,$Xi);
  995. &pshufb ($Xi,$Xn);
  996. &movdqu (&QWP(0,$Xip),$Xi);
  997. &ret ();
  998. &function_end_B("gcm_gmult_clmul");
  999. &function_begin("gcm_ghash_clmul");
  1000. &mov ($Xip,&wparam(0));
  1001. &mov ($Htbl,&wparam(1));
  1002. &mov ($inp,&wparam(2));
  1003. &mov ($len,&wparam(3));
  1004. &call (&label("pic"));
  1005. &set_label("pic");
  1006. &blindpop ($const);
  1007. &lea ($const,&DWP(&label("bswap")."-".&label("pic"),$const));
  1008. &movdqu ($Xi,&QWP(0,$Xip));
  1009. &movdqa ($T3,&QWP(0,$const));
  1010. &movdqu ($Hkey,&QWP(0,$Htbl));
  1011. &pshufb ($Xi,$T3);
  1012. &sub ($len,0x10);
  1013. &jz (&label("odd_tail"));
  1014. #######
  1015. # Xi+2 =[H*(Ii+1 + Xi+1)] mod P =
  1016. # [(H*Ii+1) + (H*Xi+1)] mod P =
  1017. # [(H*Ii+1) + H^2*(Ii+Xi)] mod P
  1018. #
  1019. &movdqu ($T1,&QWP(0,$inp)); # Ii
  1020. &movdqu ($Xn,&QWP(16,$inp)); # Ii+1
  1021. &pshufb ($T1,$T3);
  1022. &pshufb ($Xn,$T3);
  1023. &pxor ($Xi,$T1); # Ii+Xi
  1024. &clmul64x64_T3 ($Xhn,$Xn,$Hkey); # H*Ii+1
  1025. &movdqu ($Hkey,&QWP(16,$Htbl)); # load H^2
  1026. &sub ($len,0x20);
  1027. &lea ($inp,&DWP(32,$inp)); # i+=2
  1028. &jbe (&label("even_tail"));
  1029. &set_label("mod_loop");
  1030. &clmul64x64_T3 ($Xhi,$Xi,$Hkey); # H^2*(Ii+Xi)
  1031. &movdqu ($Hkey,&QWP(0,$Htbl)); # load H
  1032. &pxor ($Xi,$Xn); # (H*Ii+1) + H^2*(Ii+Xi)
  1033. &pxor ($Xhi,$Xhn);
  1034. &reduction_alg5 ($Xhi,$Xi);
  1035. #######
  1036. &movdqa ($T3,&QWP(0,$const));
  1037. &movdqu ($T1,&QWP(0,$inp)); # Ii
  1038. &movdqu ($Xn,&QWP(16,$inp)); # Ii+1
  1039. &pshufb ($T1,$T3);
  1040. &pshufb ($Xn,$T3);
  1041. &pxor ($Xi,$T1); # Ii+Xi
  1042. &clmul64x64_T3 ($Xhn,$Xn,$Hkey); # H*Ii+1
  1043. &movdqu ($Hkey,&QWP(16,$Htbl)); # load H^2
  1044. &sub ($len,0x20);
  1045. &lea ($inp,&DWP(32,$inp));
  1046. &ja (&label("mod_loop"));
  1047. &set_label("even_tail");
  1048. &clmul64x64_T3 ($Xhi,$Xi,$Hkey); # H^2*(Ii+Xi)
  1049. &pxor ($Xi,$Xn); # (H*Ii+1) + H^2*(Ii+Xi)
  1050. &pxor ($Xhi,$Xhn);
  1051. &reduction_alg5 ($Xhi,$Xi);
  1052. &movdqa ($T3,&QWP(0,$const));
  1053. &test ($len,$len);
  1054. &jnz (&label("done"));
  1055. &movdqu ($Hkey,&QWP(0,$Htbl)); # load H
  1056. &set_label("odd_tail");
  1057. &movdqu ($T1,&QWP(0,$inp)); # Ii
  1058. &pshufb ($T1,$T3);
  1059. &pxor ($Xi,$T1); # Ii+Xi
  1060. &clmul64x64_T3 ($Xhi,$Xi,$Hkey); # H*(Ii+Xi)
  1061. &reduction_alg5 ($Xhi,$Xi);
  1062. &movdqa ($T3,&QWP(0,$const));
  1063. &set_label("done");
  1064. &pshufb ($Xi,$T3);
  1065. &movdqu (&QWP(0,$Xip),$Xi);
  1066. &function_end("gcm_ghash_clmul");
  1067. }
  1068. &set_label("bswap",64);
  1069. &data_byte(15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0);
  1070. &data_byte(1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xc2); # 0x1c2_polynomial
  1071. }} # $sse2
  1072. &set_label("rem_4bit",64);
  1073. &data_word(0,0x0000<<$S,0,0x1C20<<$S,0,0x3840<<$S,0,0x2460<<$S);
  1074. &data_word(0,0x7080<<$S,0,0x6CA0<<$S,0,0x48C0<<$S,0,0x54E0<<$S);
  1075. &data_word(0,0xE100<<$S,0,0xFD20<<$S,0,0xD940<<$S,0,0xC560<<$S);
  1076. &data_word(0,0x9180<<$S,0,0x8DA0<<$S,0,0xA9C0<<$S,0,0xB5E0<<$S);
  1077. &set_label("rem_8bit",64);
  1078. &data_short(0x0000,0x01C2,0x0384,0x0246,0x0708,0x06CA,0x048C,0x054E);
  1079. &data_short(0x0E10,0x0FD2,0x0D94,0x0C56,0x0918,0x08DA,0x0A9C,0x0B5E);
  1080. &data_short(0x1C20,0x1DE2,0x1FA4,0x1E66,0x1B28,0x1AEA,0x18AC,0x196E);
  1081. &data_short(0x1230,0x13F2,0x11B4,0x1076,0x1538,0x14FA,0x16BC,0x177E);
  1082. &data_short(0x3840,0x3982,0x3BC4,0x3A06,0x3F48,0x3E8A,0x3CCC,0x3D0E);
  1083. &data_short(0x3650,0x3792,0x35D4,0x3416,0x3158,0x309A,0x32DC,0x331E);
  1084. &data_short(0x2460,0x25A2,0x27E4,0x2626,0x2368,0x22AA,0x20EC,0x212E);
  1085. &data_short(0x2A70,0x2BB2,0x29F4,0x2836,0x2D78,0x2CBA,0x2EFC,0x2F3E);
  1086. &data_short(0x7080,0x7142,0x7304,0x72C6,0x7788,0x764A,0x740C,0x75CE);
  1087. &data_short(0x7E90,0x7F52,0x7D14,0x7CD6,0x7998,0x785A,0x7A1C,0x7BDE);
  1088. &data_short(0x6CA0,0x6D62,0x6F24,0x6EE6,0x6BA8,0x6A6A,0x682C,0x69EE);
  1089. &data_short(0x62B0,0x6372,0x6134,0x60F6,0x65B8,0x647A,0x663C,0x67FE);
  1090. &data_short(0x48C0,0x4902,0x4B44,0x4A86,0x4FC8,0x4E0A,0x4C4C,0x4D8E);
  1091. &data_short(0x46D0,0x4712,0x4554,0x4496,0x41D8,0x401A,0x425C,0x439E);
  1092. &data_short(0x54E0,0x5522,0x5764,0x56A6,0x53E8,0x522A,0x506C,0x51AE);
  1093. &data_short(0x5AF0,0x5B32,0x5974,0x58B6,0x5DF8,0x5C3A,0x5E7C,0x5FBE);
  1094. &data_short(0xE100,0xE0C2,0xE284,0xE346,0xE608,0xE7CA,0xE58C,0xE44E);
  1095. &data_short(0xEF10,0xEED2,0xEC94,0xED56,0xE818,0xE9DA,0xEB9C,0xEA5E);
  1096. &data_short(0xFD20,0xFCE2,0xFEA4,0xFF66,0xFA28,0xFBEA,0xF9AC,0xF86E);
  1097. &data_short(0xF330,0xF2F2,0xF0B4,0xF176,0xF438,0xF5FA,0xF7BC,0xF67E);
  1098. &data_short(0xD940,0xD882,0xDAC4,0xDB06,0xDE48,0xDF8A,0xDDCC,0xDC0E);
  1099. &data_short(0xD750,0xD692,0xD4D4,0xD516,0xD058,0xD19A,0xD3DC,0xD21E);
  1100. &data_short(0xC560,0xC4A2,0xC6E4,0xC726,0xC268,0xC3AA,0xC1EC,0xC02E);
  1101. &data_short(0xCB70,0xCAB2,0xC8F4,0xC936,0xCC78,0xCDBA,0xCFFC,0xCE3E);
  1102. &data_short(0x9180,0x9042,0x9204,0x93C6,0x9688,0x974A,0x950C,0x94CE);
  1103. &data_short(0x9F90,0x9E52,0x9C14,0x9DD6,0x9898,0x995A,0x9B1C,0x9ADE);
  1104. &data_short(0x8DA0,0x8C62,0x8E24,0x8FE6,0x8AA8,0x8B6A,0x892C,0x88EE);
  1105. &data_short(0x83B0,0x8272,0x8034,0x81F6,0x84B8,0x857A,0x873C,0x86FE);
  1106. &data_short(0xA9C0,0xA802,0xAA44,0xAB86,0xAEC8,0xAF0A,0xAD4C,0xAC8E);
  1107. &data_short(0xA7D0,0xA612,0xA454,0xA596,0xA0D8,0xA11A,0xA35C,0xA29E);
  1108. &data_short(0xB5E0,0xB422,0xB664,0xB7A6,0xB2E8,0xB32A,0xB16C,0xB0AE);
  1109. &data_short(0xBBF0,0xBA32,0xB874,0xB9B6,0xBCF8,0xBD3A,0xBF7C,0xBEBE);
  1110. }}} # !$x86only
  1111. &asciz("GHASH for x86, CRYPTOGAMS by <appro\@openssl.org>");
  1112. &asm_finish();
  1113. # A question was risen about choice of vanilla MMX. Or rather why wasn't
  1114. # SSE2 chosen instead? In addition to the fact that MMX runs on legacy
  1115. # CPUs such as PIII, "4-bit" MMX version was observed to provide better
  1116. # performance than *corresponding* SSE2 one even on contemporary CPUs.
  1117. # SSE2 results were provided by Peter-Michael Hager. He maintains SSE2
  1118. # implementation featuring full range of lookup-table sizes, but with
  1119. # per-invocation lookup table setup. Latter means that table size is
  1120. # chosen depending on how much data is to be hashed in every given call,
  1121. # more data - larger table. Best reported result for Core2 is ~4 cycles
  1122. # per processed byte out of 64KB block. This number accounts even for
  1123. # 64KB table setup overhead. As discussed in gcm128.c we choose to be
  1124. # more conservative in respect to lookup table sizes, but how do the
  1125. # results compare? Minimalistic "256B" MMX version delivers ~11 cycles
  1126. # on same platform. As also discussed in gcm128.c, next in line "8-bit
  1127. # Shoup's" or "4KB" method should deliver twice the performance of
  1128. # "256B" one, in other words not worse than ~6 cycles per byte. It
  1129. # should be also be noted that in SSE2 case improvement can be "super-
  1130. # linear," i.e. more than twice, mostly because >>8 maps to single
  1131. # instruction on SSE2 register. This is unlike "4-bit" case when >>4
  1132. # maps to same amount of instructions in both MMX and SSE2 cases.
  1133. # Bottom line is that switch to SSE2 is considered to be justifiable
  1134. # only in case we choose to implement "8-bit" method...