ghash-x86_64.pl 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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, 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
  14. # it uses 256 bytes per-key table [+128 bytes shared table]. GHASH
  15. # function features so called "528B" variant utilizing additional
  16. # 256+16 bytes of per-key storage [+512 bytes shared table].
  17. # Performance results are for this streamed GHASH subroutine and are
  18. # expressed in cycles per processed byte, less is better:
  19. #
  20. # gcc 3.4.x(*) assembler
  21. #
  22. # P4 28.6 14.0 +100%
  23. # Opteron 19.3 7.7 +150%
  24. # Core2 17.8 8.1(**) +120%
  25. #
  26. # (*) comparison is not completely fair, because C results are
  27. # for vanilla "256B" implementation, while assembler results
  28. # are for "528B";-)
  29. # (**) it's mystery [to me] why Core2 result is not same as for
  30. # Opteron;
  31. # May 2010
  32. #
  33. # Add PCLMULQDQ version performing at 2.02 cycles per processed byte.
  34. # See ghash-x86.pl for background information and details about coding
  35. # techniques.
  36. #
  37. # Special thanks to David Woodhouse <dwmw2@infradead.org> for
  38. # providing access to a Westmere-based system on behalf of Intel
  39. # Open Source Technology Centre.
  40. $flavour = shift;
  41. $output = shift;
  42. if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
  43. $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
  44. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  45. ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
  46. ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
  47. die "can't locate x86_64-xlate.pl";
  48. open STDOUT,"| $^X $xlate $flavour $output";
  49. # common register layout
  50. $nlo="%rax";
  51. $nhi="%rbx";
  52. $Zlo="%r8";
  53. $Zhi="%r9";
  54. $tmp="%r10";
  55. $rem_4bit = "%r11";
  56. $Xi="%rdi";
  57. $Htbl="%rsi";
  58. # per-function register layout
  59. $cnt="%rcx";
  60. $rem="%rdx";
  61. sub LB() { my $r=shift; $r =~ s/%[er]([a-d])x/%\1l/ or
  62. $r =~ s/%[er]([sd]i)/%\1l/ or
  63. $r =~ s/%[er](bp)/%\1l/ or
  64. $r =~ s/%(r[0-9]+)[d]?/%\1b/; $r; }
  65. sub AUTOLOAD() # thunk [simplified] 32-bit style perlasm
  66. { my $opcode = $AUTOLOAD; $opcode =~ s/.*:://;
  67. my $arg = pop;
  68. $arg = "\$$arg" if ($arg*1 eq $arg);
  69. $code .= "\t$opcode\t".join(',',$arg,reverse @_)."\n";
  70. }
  71. { my $N;
  72. sub loop() {
  73. my $inp = shift;
  74. $N++;
  75. $code.=<<___;
  76. xor $nlo,$nlo
  77. xor $nhi,$nhi
  78. mov `&LB("$Zlo")`,`&LB("$nlo")`
  79. mov `&LB("$Zlo")`,`&LB("$nhi")`
  80. shl \$4,`&LB("$nlo")`
  81. mov \$14,$cnt
  82. mov 8($Htbl,$nlo),$Zlo
  83. mov ($Htbl,$nlo),$Zhi
  84. and \$0xf0,`&LB("$nhi")`
  85. mov $Zlo,$rem
  86. jmp .Loop$N
  87. .align 16
  88. .Loop$N:
  89. shr \$4,$Zlo
  90. and \$0xf,$rem
  91. mov $Zhi,$tmp
  92. mov ($inp,$cnt),`&LB("$nlo")`
  93. shr \$4,$Zhi
  94. xor 8($Htbl,$nhi),$Zlo
  95. shl \$60,$tmp
  96. xor ($Htbl,$nhi),$Zhi
  97. mov `&LB("$nlo")`,`&LB("$nhi")`
  98. xor ($rem_4bit,$rem,8),$Zhi
  99. mov $Zlo,$rem
  100. shl \$4,`&LB("$nlo")`
  101. xor $tmp,$Zlo
  102. dec $cnt
  103. js .Lbreak$N
  104. shr \$4,$Zlo
  105. and \$0xf,$rem
  106. mov $Zhi,$tmp
  107. shr \$4,$Zhi
  108. xor 8($Htbl,$nlo),$Zlo
  109. shl \$60,$tmp
  110. xor ($Htbl,$nlo),$Zhi
  111. and \$0xf0,`&LB("$nhi")`
  112. xor ($rem_4bit,$rem,8),$Zhi
  113. mov $Zlo,$rem
  114. xor $tmp,$Zlo
  115. jmp .Loop$N
  116. .align 16
  117. .Lbreak$N:
  118. shr \$4,$Zlo
  119. and \$0xf,$rem
  120. mov $Zhi,$tmp
  121. shr \$4,$Zhi
  122. xor 8($Htbl,$nlo),$Zlo
  123. shl \$60,$tmp
  124. xor ($Htbl,$nlo),$Zhi
  125. and \$0xf0,`&LB("$nhi")`
  126. xor ($rem_4bit,$rem,8),$Zhi
  127. mov $Zlo,$rem
  128. xor $tmp,$Zlo
  129. shr \$4,$Zlo
  130. and \$0xf,$rem
  131. mov $Zhi,$tmp
  132. shr \$4,$Zhi
  133. xor 8($Htbl,$nhi),$Zlo
  134. shl \$60,$tmp
  135. xor ($Htbl,$nhi),$Zhi
  136. xor $tmp,$Zlo
  137. xor ($rem_4bit,$rem,8),$Zhi
  138. bswap $Zlo
  139. bswap $Zhi
  140. ___
  141. }}
  142. $code=<<___;
  143. .text
  144. .globl gcm_gmult_4bit
  145. .type gcm_gmult_4bit,\@function,2
  146. .align 16
  147. gcm_gmult_4bit:
  148. push %rbx
  149. push %rbp # %rbp and %r12 are pushed exclusively in
  150. push %r12 # order to reuse Win64 exception handler...
  151. .Lgmult_prologue:
  152. movzb 15($Xi),$Zlo
  153. lea .Lrem_4bit(%rip),$rem_4bit
  154. ___
  155. &loop ($Xi);
  156. $code.=<<___;
  157. mov $Zlo,8($Xi)
  158. mov $Zhi,($Xi)
  159. mov 16(%rsp),%rbx
  160. lea 24(%rsp),%rsp
  161. .Lgmult_epilogue:
  162. ret
  163. .size gcm_gmult_4bit,.-gcm_gmult_4bit
  164. ___
  165. # per-function register layout
  166. $inp="%rdx";
  167. $len="%rcx";
  168. $rem_8bit=$rem_4bit;
  169. $code.=<<___;
  170. .globl gcm_ghash_4bit
  171. .type gcm_ghash_4bit,\@function,4
  172. .align 16
  173. gcm_ghash_4bit:
  174. push %rbx
  175. push %rbp
  176. push %r12
  177. push %r13
  178. push %r14
  179. push %r15
  180. sub \$280,%rsp
  181. .Lghash_prologue:
  182. mov $inp,%r14 # reassign couple of args
  183. mov $len,%r15
  184. ___
  185. { my $inp="%r14";
  186. my $dat="%edx";
  187. my $len="%r15";
  188. my @nhi=("%ebx","%ecx");
  189. my @rem=("%r12","%r13");
  190. my $Hshr4="%rbp";
  191. &sub ($Htbl,-128); # size optimization
  192. &lea ($Hshr4,"16+128(%rsp)");
  193. { my @lo =($nlo,$nhi);
  194. my @hi =($Zlo,$Zhi);
  195. &xor ($dat,$dat);
  196. for ($i=0,$j=-2;$i<18;$i++,$j++) {
  197. &mov ("$j(%rsp)",&LB($dat)) if ($i>1);
  198. &or ($lo[0],$tmp) if ($i>1);
  199. &mov (&LB($dat),&LB($lo[1])) if ($i>0 && $i<17);
  200. &shr ($lo[1],4) if ($i>0 && $i<17);
  201. &mov ($tmp,$hi[1]) if ($i>0 && $i<17);
  202. &shr ($hi[1],4) if ($i>0 && $i<17);
  203. &mov ("8*$j($Hshr4)",$hi[0]) if ($i>1);
  204. &mov ($hi[0],"16*$i+0-128($Htbl)") if ($i<16);
  205. &shl (&LB($dat),4) if ($i>0 && $i<17);
  206. &mov ("8*$j-128($Hshr4)",$lo[0]) if ($i>1);
  207. &mov ($lo[0],"16*$i+8-128($Htbl)") if ($i<16);
  208. &shl ($tmp,60) if ($i>0 && $i<17);
  209. push (@lo,shift(@lo));
  210. push (@hi,shift(@hi));
  211. }
  212. }
  213. &add ($Htbl,-128);
  214. &mov ($Zlo,"8($Xi)");
  215. &mov ($Zhi,"0($Xi)");
  216. &add ($len,$inp); # pointer to the end of data
  217. &lea ($rem_8bit,".Lrem_8bit(%rip)");
  218. &jmp (".Louter_loop");
  219. $code.=".align 16\n.Louter_loop:\n";
  220. &xor ($Zhi,"($inp)");
  221. &mov ("%rdx","8($inp)");
  222. &lea ($inp,"16($inp)");
  223. &xor ("%rdx",$Zlo);
  224. &mov ("($Xi)",$Zhi);
  225. &mov ("8($Xi)","%rdx");
  226. &shr ("%rdx",32);
  227. &xor ($nlo,$nlo);
  228. &rol ($dat,8);
  229. &mov (&LB($nlo),&LB($dat));
  230. &movz ($nhi[0],&LB($dat));
  231. &shl (&LB($nlo),4);
  232. &shr ($nhi[0],4);
  233. for ($j=11,$i=0;$i<15;$i++) {
  234. &rol ($dat,8);
  235. &xor ($Zlo,"8($Htbl,$nlo)") if ($i>0);
  236. &xor ($Zhi,"($Htbl,$nlo)") if ($i>0);
  237. &mov ($Zlo,"8($Htbl,$nlo)") if ($i==0);
  238. &mov ($Zhi,"($Htbl,$nlo)") if ($i==0);
  239. &mov (&LB($nlo),&LB($dat));
  240. &xor ($Zlo,$tmp) if ($i>0);
  241. &movzw ($rem[1],"($rem_8bit,$rem[1],2)") if ($i>0);
  242. &movz ($nhi[1],&LB($dat));
  243. &shl (&LB($nlo),4);
  244. &movzb ($rem[0],"(%rsp,$nhi[0])");
  245. &shr ($nhi[1],4) if ($i<14);
  246. &and ($nhi[1],0xf0) if ($i==14);
  247. &shl ($rem[1],48) if ($i>0);
  248. &xor ($rem[0],$Zlo);
  249. &mov ($tmp,$Zhi);
  250. &xor ($Zhi,$rem[1]) if ($i>0);
  251. &shr ($Zlo,8);
  252. &movz ($rem[0],&LB($rem[0]));
  253. &mov ($dat,"$j($Xi)") if (--$j%4==0);
  254. &shr ($Zhi,8);
  255. &xor ($Zlo,"-128($Hshr4,$nhi[0],8)");
  256. &shl ($tmp,56);
  257. &xor ($Zhi,"($Hshr4,$nhi[0],8)");
  258. unshift (@nhi,pop(@nhi)); # "rotate" registers
  259. unshift (@rem,pop(@rem));
  260. }
  261. &movzw ($rem[1],"($rem_8bit,$rem[1],2)");
  262. &xor ($Zlo,"8($Htbl,$nlo)");
  263. &xor ($Zhi,"($Htbl,$nlo)");
  264. &shl ($rem[1],48);
  265. &xor ($Zlo,$tmp);
  266. &xor ($Zhi,$rem[1]);
  267. &movz ($rem[0],&LB($Zlo));
  268. &shr ($Zlo,4);
  269. &mov ($tmp,$Zhi);
  270. &shl (&LB($rem[0]),4);
  271. &shr ($Zhi,4);
  272. &xor ($Zlo,"8($Htbl,$nhi[0])");
  273. &movzw ($rem[0],"($rem_8bit,$rem[0],2)");
  274. &shl ($tmp,60);
  275. &xor ($Zhi,"($Htbl,$nhi[0])");
  276. &xor ($Zlo,$tmp);
  277. &shl ($rem[0],48);
  278. &bswap ($Zlo);
  279. &xor ($Zhi,$rem[0]);
  280. &bswap ($Zhi);
  281. &cmp ($inp,$len);
  282. &jb (".Louter_loop");
  283. }
  284. $code.=<<___;
  285. mov $Zlo,8($Xi)
  286. mov $Zhi,($Xi)
  287. lea 280(%rsp),%rsi
  288. mov 0(%rsi),%r15
  289. mov 8(%rsi),%r14
  290. mov 16(%rsi),%r13
  291. mov 24(%rsi),%r12
  292. mov 32(%rsi),%rbp
  293. mov 40(%rsi),%rbx
  294. lea 48(%rsi),%rsp
  295. .Lghash_epilogue:
  296. ret
  297. .size gcm_ghash_4bit,.-gcm_ghash_4bit
  298. ___
  299. ######################################################################
  300. # PCLMULQDQ version.
  301. @_4args=$win64? ("%rcx","%rdx","%r8", "%r9") : # Win64 order
  302. ("%rdi","%rsi","%rdx","%rcx"); # Unix order
  303. ($Xi,$Xhi)=("%xmm0","%xmm1"); $Hkey="%xmm2";
  304. ($T1,$T2,$T3)=("%xmm3","%xmm4","%xmm5");
  305. sub clmul64x64_T2 { # minimal register pressure
  306. my ($Xhi,$Xi,$Hkey,$modulo)=@_;
  307. $code.=<<___ if (!defined($modulo));
  308. movdqa $Xi,$Xhi #
  309. pshufd \$0b01001110,$Xi,$T1
  310. pshufd \$0b01001110,$Hkey,$T2
  311. pxor $Xi,$T1 #
  312. pxor $Hkey,$T2
  313. ___
  314. $code.=<<___;
  315. pclmulqdq \$0x00,$Hkey,$Xi #######
  316. pclmulqdq \$0x11,$Hkey,$Xhi #######
  317. pclmulqdq \$0x00,$T2,$T1 #######
  318. pxor $Xi,$T1 #
  319. pxor $Xhi,$T1 #
  320. movdqa $T1,$T2 #
  321. psrldq \$8,$T1
  322. pslldq \$8,$T2 #
  323. pxor $T1,$Xhi
  324. pxor $T2,$Xi #
  325. ___
  326. }
  327. sub reduction_alg9 { # 17/13 times faster than Intel version
  328. my ($Xhi,$Xi) = @_;
  329. $code.=<<___;
  330. # 1st phase
  331. movdqa $Xi,$T1 #
  332. psllq \$1,$Xi
  333. pxor $T1,$Xi #
  334. psllq \$5,$Xi #
  335. pxor $T1,$Xi #
  336. psllq \$57,$Xi #
  337. movdqa $Xi,$T2 #
  338. pslldq \$8,$Xi
  339. psrldq \$8,$T2 #
  340. pxor $T1,$Xi
  341. pxor $T2,$Xhi #
  342. # 2nd phase
  343. movdqa $Xi,$T2
  344. psrlq \$5,$Xi
  345. pxor $T2,$Xi #
  346. psrlq \$1,$Xi #
  347. pxor $T2,$Xi #
  348. pxor $Xhi,$T2
  349. psrlq \$1,$Xi #
  350. pxor $T2,$Xi #
  351. ___
  352. }
  353. { my ($Htbl,$Xip)=@_4args;
  354. $code.=<<___;
  355. .globl gcm_init_clmul
  356. .type gcm_init_clmul,\@abi-omnipotent
  357. .align 16
  358. gcm_init_clmul:
  359. movdqu ($Xip),$Hkey
  360. pshufd \$0b01001110,$Hkey,$Hkey # dword swap
  361. # <<1 twist
  362. pshufd \$0b11111111,$Hkey,$T2 # broadcast uppermost dword
  363. movdqa $Hkey,$T1
  364. psllq \$1,$Hkey
  365. pxor $T3,$T3 #
  366. psrlq \$63,$T1
  367. pcmpgtd $T2,$T3 # broadcast carry bit
  368. pslldq \$8,$T1
  369. por $T1,$Hkey # H<<=1
  370. # magic reduction
  371. pand .L0x1c2_polynomial(%rip),$T3
  372. pxor $T3,$Hkey # if(carry) H^=0x1c2_polynomial
  373. # calculate H^2
  374. movdqa $Hkey,$Xi
  375. ___
  376. &clmul64x64_T2 ($Xhi,$Xi,$Hkey);
  377. &reduction_alg9 ($Xhi,$Xi);
  378. $code.=<<___;
  379. movdqu $Hkey,($Htbl) # save H
  380. movdqu $Xi,16($Htbl) # save H^2
  381. ret
  382. .size gcm_init_clmul,.-gcm_init_clmul
  383. ___
  384. }
  385. { my ($Xip,$Htbl)=@_4args;
  386. $code.=<<___;
  387. .globl gcm_gmult_clmul
  388. .type gcm_gmult_clmul,\@abi-omnipotent
  389. .align 16
  390. gcm_gmult_clmul:
  391. movdqu ($Xip),$Xi
  392. movdqa .Lbswap_mask(%rip),$T3
  393. movdqu ($Htbl),$Hkey
  394. pshufb $T3,$Xi
  395. ___
  396. &clmul64x64_T2 ($Xhi,$Xi,$Hkey);
  397. &reduction_alg9 ($Xhi,$Xi);
  398. $code.=<<___;
  399. pshufb $T3,$Xi
  400. movdqu $Xi,($Xip)
  401. ret
  402. .size gcm_gmult_clmul,.-gcm_gmult_clmul
  403. ___
  404. }
  405. { my ($Xip,$Htbl,$inp,$len)=@_4args;
  406. my $Xn="%xmm6";
  407. my $Xhn="%xmm7";
  408. my $Hkey2="%xmm8";
  409. my $T1n="%xmm9";
  410. my $T2n="%xmm10";
  411. $code.=<<___;
  412. .globl gcm_ghash_clmul
  413. .type gcm_ghash_clmul,\@abi-omnipotent
  414. .align 16
  415. gcm_ghash_clmul:
  416. ___
  417. $code.=<<___ if ($win64);
  418. .LSEH_begin_gcm_ghash_clmul:
  419. # I can't trust assembler to use specific encoding:-(
  420. .byte 0x48,0x83,0xec,0x58 #sub \$0x58,%rsp
  421. .byte 0x0f,0x29,0x34,0x24 #movaps %xmm6,(%rsp)
  422. .byte 0x0f,0x29,0x7c,0x24,0x10 #movdqa %xmm7,0x10(%rsp)
  423. .byte 0x44,0x0f,0x29,0x44,0x24,0x20 #movaps %xmm8,0x20(%rsp)
  424. .byte 0x44,0x0f,0x29,0x4c,0x24,0x30 #movaps %xmm9,0x30(%rsp)
  425. .byte 0x44,0x0f,0x29,0x54,0x24,0x40 #movaps %xmm10,0x40(%rsp)
  426. ___
  427. $code.=<<___;
  428. movdqa .Lbswap_mask(%rip),$T3
  429. movdqu ($Xip),$Xi
  430. movdqu ($Htbl),$Hkey
  431. pshufb $T3,$Xi
  432. sub \$0x10,$len
  433. jz .Lodd_tail
  434. movdqu 16($Htbl),$Hkey2
  435. #######
  436. # Xi+2 =[H*(Ii+1 + Xi+1)] mod P =
  437. # [(H*Ii+1) + (H*Xi+1)] mod P =
  438. # [(H*Ii+1) + H^2*(Ii+Xi)] mod P
  439. #
  440. movdqu ($inp),$T1 # Ii
  441. movdqu 16($inp),$Xn # Ii+1
  442. pshufb $T3,$T1
  443. pshufb $T3,$Xn
  444. pxor $T1,$Xi # Ii+Xi
  445. ___
  446. &clmul64x64_T2 ($Xhn,$Xn,$Hkey); # H*Ii+1
  447. $code.=<<___;
  448. movdqa $Xi,$Xhi #
  449. pshufd \$0b01001110,$Xi,$T1
  450. pshufd \$0b01001110,$Hkey2,$T2
  451. pxor $Xi,$T1 #
  452. pxor $Hkey2,$T2
  453. lea 32($inp),$inp # i+=2
  454. sub \$0x20,$len
  455. jbe .Leven_tail
  456. .Lmod_loop:
  457. ___
  458. &clmul64x64_T2 ($Xhi,$Xi,$Hkey2,1); # H^2*(Ii+Xi)
  459. $code.=<<___;
  460. movdqu ($inp),$T1 # Ii
  461. pxor $Xn,$Xi # (H*Ii+1) + H^2*(Ii+Xi)
  462. pxor $Xhn,$Xhi
  463. movdqu 16($inp),$Xn # Ii+1
  464. pshufb $T3,$T1
  465. pshufb $T3,$Xn
  466. movdqa $Xn,$Xhn #
  467. pshufd \$0b01001110,$Xn,$T1n
  468. pshufd \$0b01001110,$Hkey,$T2n
  469. pxor $Xn,$T1n #
  470. pxor $Hkey,$T2n
  471. pxor $T1,$Xhi # "Ii+Xi", consume early
  472. movdqa $Xi,$T1 # 1st phase
  473. psllq \$1,$Xi
  474. pxor $T1,$Xi #
  475. psllq \$5,$Xi #
  476. pxor $T1,$Xi #
  477. pclmulqdq \$0x00,$Hkey,$Xn #######
  478. psllq \$57,$Xi #
  479. movdqa $Xi,$T2 #
  480. pslldq \$8,$Xi
  481. psrldq \$8,$T2 #
  482. pxor $T1,$Xi
  483. pxor $T2,$Xhi #
  484. pclmulqdq \$0x11,$Hkey,$Xhn #######
  485. movdqa $Xi,$T2 # 2nd phase
  486. psrlq \$5,$Xi
  487. pxor $T2,$Xi #
  488. psrlq \$1,$Xi #
  489. pxor $T2,$Xi #
  490. pxor $Xhi,$T2
  491. psrlq \$1,$Xi #
  492. pxor $T2,$Xi #
  493. pclmulqdq \$0x00,$T2n,$T1n #######
  494. movdqa $Xi,$Xhi #
  495. pshufd \$0b01001110,$Xi,$T1
  496. pshufd \$0b01001110,$Hkey2,$T2
  497. pxor $Xi,$T1 #
  498. pxor $Hkey2,$T2
  499. pxor $Xn,$T1n #
  500. pxor $Xhn,$T1n #
  501. movdqa $T1n,$T2n #
  502. psrldq \$8,$T1n
  503. pslldq \$8,$T2n #
  504. pxor $T1n,$Xhn
  505. pxor $T2n,$Xn #
  506. lea 32($inp),$inp
  507. sub \$0x20,$len
  508. ja .Lmod_loop
  509. .Leven_tail:
  510. ___
  511. &clmul64x64_T2 ($Xhi,$Xi,$Hkey2,1); # H^2*(Ii+Xi)
  512. $code.=<<___;
  513. pxor $Xn,$Xi # (H*Ii+1) + H^2*(Ii+Xi)
  514. pxor $Xhn,$Xhi
  515. ___
  516. &reduction_alg9 ($Xhi,$Xi);
  517. $code.=<<___;
  518. test $len,$len
  519. jnz .Ldone
  520. .Lodd_tail:
  521. movdqu ($inp),$T1 # Ii
  522. pshufb $T3,$T1
  523. pxor $T1,$Xi # Ii+Xi
  524. ___
  525. &clmul64x64_T2 ($Xhi,$Xi,$Hkey); # H*(Ii+Xi)
  526. &reduction_alg9 ($Xhi,$Xi);
  527. $code.=<<___;
  528. .Ldone:
  529. pshufb $T3,$Xi
  530. movdqu $Xi,($Xip)
  531. ___
  532. $code.=<<___ if ($win64);
  533. movaps (%rsp),%xmm6
  534. movaps 0x10(%rsp),%xmm7
  535. movaps 0x20(%rsp),%xmm8
  536. movaps 0x30(%rsp),%xmm9
  537. movaps 0x40(%rsp),%xmm10
  538. add \$0x58,%rsp
  539. ___
  540. $code.=<<___;
  541. ret
  542. .LSEH_end_gcm_ghash_clmul:
  543. .size gcm_ghash_clmul,.-gcm_ghash_clmul
  544. ___
  545. }
  546. $code.=<<___;
  547. .align 64
  548. .Lbswap_mask:
  549. .byte 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
  550. .L0x1c2_polynomial:
  551. .byte 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xc2
  552. .align 64
  553. .type .Lrem_4bit,\@object
  554. .Lrem_4bit:
  555. .long 0,`0x0000<<16`,0,`0x1C20<<16`,0,`0x3840<<16`,0,`0x2460<<16`
  556. .long 0,`0x7080<<16`,0,`0x6CA0<<16`,0,`0x48C0<<16`,0,`0x54E0<<16`
  557. .long 0,`0xE100<<16`,0,`0xFD20<<16`,0,`0xD940<<16`,0,`0xC560<<16`
  558. .long 0,`0x9180<<16`,0,`0x8DA0<<16`,0,`0xA9C0<<16`,0,`0xB5E0<<16`
  559. .type .Lrem_8bit,\@object
  560. .Lrem_8bit:
  561. .value 0x0000,0x01C2,0x0384,0x0246,0x0708,0x06CA,0x048C,0x054E
  562. .value 0x0E10,0x0FD2,0x0D94,0x0C56,0x0918,0x08DA,0x0A9C,0x0B5E
  563. .value 0x1C20,0x1DE2,0x1FA4,0x1E66,0x1B28,0x1AEA,0x18AC,0x196E
  564. .value 0x1230,0x13F2,0x11B4,0x1076,0x1538,0x14FA,0x16BC,0x177E
  565. .value 0x3840,0x3982,0x3BC4,0x3A06,0x3F48,0x3E8A,0x3CCC,0x3D0E
  566. .value 0x3650,0x3792,0x35D4,0x3416,0x3158,0x309A,0x32DC,0x331E
  567. .value 0x2460,0x25A2,0x27E4,0x2626,0x2368,0x22AA,0x20EC,0x212E
  568. .value 0x2A70,0x2BB2,0x29F4,0x2836,0x2D78,0x2CBA,0x2EFC,0x2F3E
  569. .value 0x7080,0x7142,0x7304,0x72C6,0x7788,0x764A,0x740C,0x75CE
  570. .value 0x7E90,0x7F52,0x7D14,0x7CD6,0x7998,0x785A,0x7A1C,0x7BDE
  571. .value 0x6CA0,0x6D62,0x6F24,0x6EE6,0x6BA8,0x6A6A,0x682C,0x69EE
  572. .value 0x62B0,0x6372,0x6134,0x60F6,0x65B8,0x647A,0x663C,0x67FE
  573. .value 0x48C0,0x4902,0x4B44,0x4A86,0x4FC8,0x4E0A,0x4C4C,0x4D8E
  574. .value 0x46D0,0x4712,0x4554,0x4496,0x41D8,0x401A,0x425C,0x439E
  575. .value 0x54E0,0x5522,0x5764,0x56A6,0x53E8,0x522A,0x506C,0x51AE
  576. .value 0x5AF0,0x5B32,0x5974,0x58B6,0x5DF8,0x5C3A,0x5E7C,0x5FBE
  577. .value 0xE100,0xE0C2,0xE284,0xE346,0xE608,0xE7CA,0xE58C,0xE44E
  578. .value 0xEF10,0xEED2,0xEC94,0xED56,0xE818,0xE9DA,0xEB9C,0xEA5E
  579. .value 0xFD20,0xFCE2,0xFEA4,0xFF66,0xFA28,0xFBEA,0xF9AC,0xF86E
  580. .value 0xF330,0xF2F2,0xF0B4,0xF176,0xF438,0xF5FA,0xF7BC,0xF67E
  581. .value 0xD940,0xD882,0xDAC4,0xDB06,0xDE48,0xDF8A,0xDDCC,0xDC0E
  582. .value 0xD750,0xD692,0xD4D4,0xD516,0xD058,0xD19A,0xD3DC,0xD21E
  583. .value 0xC560,0xC4A2,0xC6E4,0xC726,0xC268,0xC3AA,0xC1EC,0xC02E
  584. .value 0xCB70,0xCAB2,0xC8F4,0xC936,0xCC78,0xCDBA,0xCFFC,0xCE3E
  585. .value 0x9180,0x9042,0x9204,0x93C6,0x9688,0x974A,0x950C,0x94CE
  586. .value 0x9F90,0x9E52,0x9C14,0x9DD6,0x9898,0x995A,0x9B1C,0x9ADE
  587. .value 0x8DA0,0x8C62,0x8E24,0x8FE6,0x8AA8,0x8B6A,0x892C,0x88EE
  588. .value 0x83B0,0x8272,0x8034,0x81F6,0x84B8,0x857A,0x873C,0x86FE
  589. .value 0xA9C0,0xA802,0xAA44,0xAB86,0xAEC8,0xAF0A,0xAD4C,0xAC8E
  590. .value 0xA7D0,0xA612,0xA454,0xA596,0xA0D8,0xA11A,0xA35C,0xA29E
  591. .value 0xB5E0,0xB422,0xB664,0xB7A6,0xB2E8,0xB32A,0xB16C,0xB0AE
  592. .value 0xBBF0,0xBA32,0xB874,0xB9B6,0xBCF8,0xBD3A,0xBF7C,0xBEBE
  593. .asciz "GHASH for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
  594. .align 64
  595. ___
  596. # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
  597. # CONTEXT *context,DISPATCHER_CONTEXT *disp)
  598. if ($win64) {
  599. $rec="%rcx";
  600. $frame="%rdx";
  601. $context="%r8";
  602. $disp="%r9";
  603. $code.=<<___;
  604. .extern __imp_RtlVirtualUnwind
  605. .type se_handler,\@abi-omnipotent
  606. .align 16
  607. se_handler:
  608. push %rsi
  609. push %rdi
  610. push %rbx
  611. push %rbp
  612. push %r12
  613. push %r13
  614. push %r14
  615. push %r15
  616. pushfq
  617. sub \$64,%rsp
  618. mov 120($context),%rax # pull context->Rax
  619. mov 248($context),%rbx # pull context->Rip
  620. mov 8($disp),%rsi # disp->ImageBase
  621. mov 56($disp),%r11 # disp->HandlerData
  622. mov 0(%r11),%r10d # HandlerData[0]
  623. lea (%rsi,%r10),%r10 # prologue label
  624. cmp %r10,%rbx # context->Rip<prologue label
  625. jb .Lin_prologue
  626. mov 152($context),%rax # pull context->Rsp
  627. mov 4(%r11),%r10d # HandlerData[1]
  628. lea (%rsi,%r10),%r10 # epilogue label
  629. cmp %r10,%rbx # context->Rip>=epilogue label
  630. jae .Lin_prologue
  631. lea 24(%rax),%rax # adjust "rsp"
  632. mov -8(%rax),%rbx
  633. mov -16(%rax),%rbp
  634. mov -24(%rax),%r12
  635. mov %rbx,144($context) # restore context->Rbx
  636. mov %rbp,160($context) # restore context->Rbp
  637. mov %r12,216($context) # restore context->R12
  638. .Lin_prologue:
  639. mov 8(%rax),%rdi
  640. mov 16(%rax),%rsi
  641. mov %rax,152($context) # restore context->Rsp
  642. mov %rsi,168($context) # restore context->Rsi
  643. mov %rdi,176($context) # restore context->Rdi
  644. mov 40($disp),%rdi # disp->ContextRecord
  645. mov $context,%rsi # context
  646. mov \$`1232/8`,%ecx # sizeof(CONTEXT)
  647. .long 0xa548f3fc # cld; rep movsq
  648. mov $disp,%rsi
  649. xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
  650. mov 8(%rsi),%rdx # arg2, disp->ImageBase
  651. mov 0(%rsi),%r8 # arg3, disp->ControlPc
  652. mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
  653. mov 40(%rsi),%r10 # disp->ContextRecord
  654. lea 56(%rsi),%r11 # &disp->HandlerData
  655. lea 24(%rsi),%r12 # &disp->EstablisherFrame
  656. mov %r10,32(%rsp) # arg5
  657. mov %r11,40(%rsp) # arg6
  658. mov %r12,48(%rsp) # arg7
  659. mov %rcx,56(%rsp) # arg8, (NULL)
  660. call *__imp_RtlVirtualUnwind(%rip)
  661. mov \$1,%eax # ExceptionContinueSearch
  662. add \$64,%rsp
  663. popfq
  664. pop %r15
  665. pop %r14
  666. pop %r13
  667. pop %r12
  668. pop %rbp
  669. pop %rbx
  670. pop %rdi
  671. pop %rsi
  672. ret
  673. .size se_handler,.-se_handler
  674. .section .pdata
  675. .align 4
  676. .rva .LSEH_begin_gcm_gmult_4bit
  677. .rva .LSEH_end_gcm_gmult_4bit
  678. .rva .LSEH_info_gcm_gmult_4bit
  679. .rva .LSEH_begin_gcm_ghash_4bit
  680. .rva .LSEH_end_gcm_ghash_4bit
  681. .rva .LSEH_info_gcm_ghash_4bit
  682. .rva .LSEH_begin_gcm_ghash_clmul
  683. .rva .LSEH_end_gcm_ghash_clmul
  684. .rva .LSEH_info_gcm_ghash_clmul
  685. .section .xdata
  686. .align 8
  687. .LSEH_info_gcm_gmult_4bit:
  688. .byte 9,0,0,0
  689. .rva se_handler
  690. .rva .Lgmult_prologue,.Lgmult_epilogue # HandlerData
  691. .LSEH_info_gcm_ghash_4bit:
  692. .byte 9,0,0,0
  693. .rva se_handler
  694. .rva .Lghash_prologue,.Lghash_epilogue # HandlerData
  695. .LSEH_info_gcm_ghash_clmul:
  696. .byte 0x01,0x1f,0x0b,0x00
  697. .byte 0x1f,0xa8,0x04,0x00 #movaps 0x40(rsp),xmm10
  698. .byte 0x19,0x98,0x03,0x00 #movaps 0x30(rsp),xmm9
  699. .byte 0x13,0x88,0x02,0x00 #movaps 0x20(rsp),xmm8
  700. .byte 0x0d,0x78,0x01,0x00 #movaps 0x10(rsp),xmm7
  701. .byte 0x08,0x68,0x00,0x00 #movaps (rsp),xmm6
  702. .byte 0x04,0xa2,0x00,0x00 #sub rsp,0x58
  703. ___
  704. }
  705. $code =~ s/\`([^\`]*)\`/eval($1)/gem;
  706. print $code;
  707. close STDOUT;