ghash-x86.pl 41 KB

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