aesni-sha1-x86_64.pl 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150
  1. #! /usr/bin/env perl
  2. # Copyright 2011-2020 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. # June 2011
  17. #
  18. # This is AESNI-CBC+SHA1 "stitch" implementation. The idea, as spelled
  19. # in http://download.intel.com/design/intarch/papers/323686.pdf, is
  20. # that since AESNI-CBC encrypt exhibit *very* low instruction-level
  21. # parallelism, interleaving it with another algorithm would allow to
  22. # utilize processor resources better and achieve better performance.
  23. # SHA1 instruction sequences(*) are taken from sha1-x86_64.pl and
  24. # AESNI code is weaved into it. Below are performance numbers in
  25. # cycles per processed byte, less is better, for standalone AESNI-CBC
  26. # encrypt, sum of the latter and standalone SHA1, and "stitched"
  27. # subroutine:
  28. #
  29. # AES-128-CBC +SHA1 stitch gain
  30. # Westmere 3.77[+5.3] 9.07 6.55 +38%
  31. # Sandy Bridge 5.05[+5.0(6.1)] 10.06(11.15) 5.98(7.05) +68%(+58%)
  32. # Ivy Bridge 5.05[+4.6] 9.65 5.54 +74%
  33. # Haswell 4.43[+3.6(4.2)] 8.00(8.58) 4.55(5.21) +75%(+65%)
  34. # Skylake 2.63[+3.5(4.1)] 6.17(6.69) 4.23(4.44) +46%(+51%)
  35. # Bulldozer 5.77[+6.0] 11.72 6.37 +84%
  36. # Ryzen(**) 2.71[+1.93] 4.64 2.74 +69%
  37. # Goldmont(**) 3.82[+1.70] 5.52 4.20 +31%
  38. #
  39. # AES-192-CBC
  40. # Westmere 4.51 9.81 6.80 +44%
  41. # Sandy Bridge 6.05 11.06(12.15) 6.11(7.19) +81%(+69%)
  42. # Ivy Bridge 6.05 10.65 6.07 +75%
  43. # Haswell 5.29 8.86(9.44) 5.32(5.32) +67%(+77%)
  44. # Bulldozer 6.89 12.84 6.96 +84%
  45. #
  46. # AES-256-CBC
  47. # Westmere 5.25 10.55 7.21 +46%
  48. # Sandy Bridge 7.05 12.06(13.15) 7.12(7.72) +69%(+70%)
  49. # Ivy Bridge 7.05 11.65 7.12 +64%
  50. # Haswell 6.19 9.76(10.34) 6.21(6.25) +57%(+65%)
  51. # Skylake 3.62 7.16(7.68) 4.56(4.76) +57%(+61%)
  52. # Bulldozer 8.00 13.95 8.25 +69%
  53. # Ryzen(**) 3.71 5.64 3.72 +52%
  54. # Goldmont(**) 5.35 7.05 5.76 +22%
  55. #
  56. # (*) There are two code paths: SSSE3 and AVX. See sha1-568.pl for
  57. # background information. Above numbers in parentheses are SSSE3
  58. # results collected on AVX-capable CPU, i.e. apply on OSes that
  59. # don't support AVX.
  60. # (**) SHAEXT results.
  61. #
  62. # Needless to mention that it makes no sense to implement "stitched"
  63. # *decrypt* subroutine. Because *both* AESNI-CBC decrypt and SHA1
  64. # fully utilize parallelism, so stitching would not give any gain
  65. # anyway. Well, there might be some, e.g. because of better cache
  66. # locality... For reference, here are performance results for
  67. # standalone AESNI-CBC decrypt:
  68. #
  69. # AES-128-CBC AES-192-CBC AES-256-CBC
  70. # Westmere 1.25 1.50 1.75
  71. # Sandy Bridge 0.74 0.91 1.09
  72. # Ivy Bridge 0.74 0.90 1.11
  73. # Haswell 0.63 0.76 0.88
  74. # Bulldozer 0.70 0.85 0.99
  75. # And indeed:
  76. #
  77. # AES-256-CBC +SHA1 stitch gain
  78. # Westmere 1.75 7.20 6.68 +7.8%
  79. # Sandy Bridge 1.09 6.09(7.22) 5.82(6.95) +4.6%(+3.9%)
  80. # Ivy Bridge 1.11 5.70 5.45 +4.6%
  81. # Haswell 0.88 4.45(5.00) 4.39(4.69) +1.4%(*)(+6.6%)
  82. # Bulldozer 0.99 6.95 5.95 +17%(**)
  83. #
  84. # (*) Tiny improvement coefficient on Haswell is because we compare
  85. # AVX1 stitch to sum with AVX2 SHA1.
  86. # (**) Execution is fully dominated by integer code sequence and
  87. # SIMD still hardly shows [in single-process benchmark;-]
  88. # $output is the last argument if it looks like a file (it has an extension)
  89. # $flavour is the first argument if it doesn't look like a file
  90. $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
  91. $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
  92. $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
  93. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  94. ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
  95. ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
  96. die "can't locate x86_64-xlate.pl";
  97. $avx=1 if (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
  98. =~ /GNU assembler version ([2-9]\.[0-9]+)/ &&
  99. $1>=2.19);
  100. $avx=1 if (!$avx && $win64 && ($flavour =~ /nasm/ || $ENV{ASM} =~ /nasm/) &&
  101. `nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)/ &&
  102. $1>=2.09);
  103. $avx=1 if (!$avx && $win64 && ($flavour =~ /masm/ || $ENV{ASM} =~ /ml64/) &&
  104. `ml64 2>&1` =~ /Version ([0-9]+)\./ &&
  105. $1>=10);
  106. $avx=1 if (!$avx && `$ENV{CC} -v 2>&1` =~ /((?:clang|LLVM) version|.*based on LLVM) ([0-9]+\.[0-9]+)/ && $2>=3.0);
  107. $shaext=1; ### set to zero if compiling for 1.0.1
  108. $stitched_decrypt=0;
  109. open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\""
  110. or die "can't call $xlate: $!";
  111. *STDOUT=*OUT;
  112. # void aesni_cbc_sha1_enc(const void *inp,
  113. # void *out,
  114. # size_t length,
  115. # const AES_KEY *key,
  116. # unsigned char *iv,
  117. # SHA_CTX *ctx,
  118. # const void *in0);
  119. $code.=<<___;
  120. .text
  121. .extern OPENSSL_ia32cap_P
  122. .globl aesni_cbc_sha1_enc
  123. .type aesni_cbc_sha1_enc,\@abi-omnipotent
  124. .align 32
  125. aesni_cbc_sha1_enc:
  126. .cfi_startproc
  127. # caller should check for SSSE3 and AES-NI bits
  128. mov OPENSSL_ia32cap_P+0(%rip),%r10d
  129. mov OPENSSL_ia32cap_P+4(%rip),%r11
  130. ___
  131. $code.=<<___ if ($shaext);
  132. bt \$61,%r11 # check SHA bit
  133. jc aesni_cbc_sha1_enc_shaext
  134. ___
  135. $code.=<<___ if ($avx);
  136. and \$`1<<28`,%r11d # mask AVX bit
  137. and \$`1<<30`,%r10d # mask "Intel CPU" bit
  138. or %r11d,%r10d
  139. cmp \$`1<<28|1<<30`,%r10d
  140. je aesni_cbc_sha1_enc_avx
  141. ___
  142. $code.=<<___;
  143. jmp aesni_cbc_sha1_enc_ssse3
  144. ret
  145. .cfi_endproc
  146. .size aesni_cbc_sha1_enc,.-aesni_cbc_sha1_enc
  147. ___
  148. my ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
  149. my $Xi=4;
  150. my @X=map("%xmm$_",(4..7,0..3));
  151. my @Tx=map("%xmm$_",(8..10));
  152. my @V=($A,$B,$C,$D,$E)=("%eax","%ebx","%ecx","%edx","%ebp"); # size optimization
  153. my @T=("%esi","%edi");
  154. my $j=0; my $jj=0; my $r=0; my $sn=0; my $rx=0;
  155. my $K_XX_XX="%r11";
  156. my ($rndkey0,$iv,$in)=map("%xmm$_",(11..13)); # for enc
  157. my @rndkey=("%xmm14","%xmm15"); # for enc
  158. my ($inout0,$inout1,$inout2,$inout3)=map("%xmm$_",(12..15)); # for dec
  159. if (1) { # reassign for Atom Silvermont
  160. # The goal is to minimize amount of instructions with more than
  161. # 3 prefix bytes. Or in more practical terms to keep AES-NI *and*
  162. # SSSE3 instructions to upper half of the register bank.
  163. @X=map("%xmm$_",(8..11,4..7));
  164. @Tx=map("%xmm$_",(12,13,3));
  165. ($iv,$in,$rndkey0)=map("%xmm$_",(2,14,15));
  166. @rndkey=("%xmm0","%xmm1");
  167. }
  168. sub AUTOLOAD() # thunk [simplified] 32-bit style perlasm
  169. { my $opcode = $AUTOLOAD; $opcode =~ s/.*:://;
  170. my $arg = pop;
  171. $arg = "\$$arg" if ($arg*1 eq $arg);
  172. $code .= "\t$opcode\t".join(',',$arg,reverse @_)."\n";
  173. }
  174. my $_rol=sub { &rol(@_) };
  175. my $_ror=sub { &ror(@_) };
  176. $code.=<<___;
  177. .type aesni_cbc_sha1_enc_ssse3,\@function,6
  178. .align 32
  179. aesni_cbc_sha1_enc_ssse3:
  180. .cfi_startproc
  181. mov `($win64?56:8)`(%rsp),$inp # load 7th argument
  182. #shr \$6,$len # debugging artefact
  183. #jz .Lepilogue_ssse3 # debugging artefact
  184. push %rbx
  185. .cfi_push %rbx
  186. push %rbp
  187. .cfi_push %rbp
  188. push %r12
  189. .cfi_push %r12
  190. push %r13
  191. .cfi_push %r13
  192. push %r14
  193. .cfi_push %r14
  194. push %r15
  195. .cfi_push %r15
  196. lea `-104-($win64?10*16:0)`(%rsp),%rsp
  197. .cfi_adjust_cfa_offset `104+($win64?10*16:0)`
  198. #mov $in0,$inp # debugging artefact
  199. #lea 64(%rsp),$ctx # debugging artefact
  200. ___
  201. $code.=<<___ if ($win64);
  202. movaps %xmm6,96+0(%rsp)
  203. movaps %xmm7,96+16(%rsp)
  204. movaps %xmm8,96+32(%rsp)
  205. movaps %xmm9,96+48(%rsp)
  206. movaps %xmm10,96+64(%rsp)
  207. movaps %xmm11,96+80(%rsp)
  208. movaps %xmm12,96+96(%rsp)
  209. movaps %xmm13,96+112(%rsp)
  210. movaps %xmm14,96+128(%rsp)
  211. movaps %xmm15,96+144(%rsp)
  212. .Lprologue_ssse3:
  213. ___
  214. $code.=<<___;
  215. mov $in0,%r12 # reassign arguments
  216. mov $out,%r13
  217. mov $len,%r14
  218. lea 112($key),%r15 # size optimization
  219. movdqu ($ivp),$iv # load IV
  220. mov $ivp,88(%rsp) # save $ivp
  221. ___
  222. ($in0,$out,$len,$key)=map("%r$_",(12..15)); # reassign arguments
  223. my $rounds="${ivp}d";
  224. $code.=<<___;
  225. shl \$6,$len
  226. sub $in0,$out
  227. mov 240-112($key),$rounds
  228. add $inp,$len # end of input
  229. lea K_XX_XX(%rip),$K_XX_XX
  230. mov 0($ctx),$A # load context
  231. mov 4($ctx),$B
  232. mov 8($ctx),$C
  233. mov 12($ctx),$D
  234. mov $B,@T[0] # magic seed
  235. mov 16($ctx),$E
  236. mov $C,@T[1]
  237. xor $D,@T[1]
  238. and @T[1],@T[0]
  239. movdqa 64($K_XX_XX),@Tx[2] # pbswap mask
  240. movdqa 0($K_XX_XX),@Tx[1] # K_00_19
  241. movdqu 0($inp),@X[-4&7] # load input to %xmm[0-3]
  242. movdqu 16($inp),@X[-3&7]
  243. movdqu 32($inp),@X[-2&7]
  244. movdqu 48($inp),@X[-1&7]
  245. pshufb @Tx[2],@X[-4&7] # byte swap
  246. pshufb @Tx[2],@X[-3&7]
  247. pshufb @Tx[2],@X[-2&7]
  248. add \$64,$inp
  249. paddd @Tx[1],@X[-4&7] # add K_00_19
  250. pshufb @Tx[2],@X[-1&7]
  251. paddd @Tx[1],@X[-3&7]
  252. paddd @Tx[1],@X[-2&7]
  253. movdqa @X[-4&7],0(%rsp) # X[]+K xfer to IALU
  254. psubd @Tx[1],@X[-4&7] # restore X[]
  255. movdqa @X[-3&7],16(%rsp)
  256. psubd @Tx[1],@X[-3&7]
  257. movdqa @X[-2&7],32(%rsp)
  258. psubd @Tx[1],@X[-2&7]
  259. movups -112($key),$rndkey0 # $key[0]
  260. movups 16-112($key),$rndkey[0] # forward reference
  261. jmp .Loop_ssse3
  262. ___
  263. my $aesenc=sub {
  264. use integer;
  265. my ($n,$k)=($r/10,$r%10);
  266. if ($k==0) {
  267. $code.=<<___;
  268. movups `16*$n`($in0),$in # load input
  269. xorps $rndkey0,$in
  270. ___
  271. $code.=<<___ if ($n);
  272. movups $iv,`16*($n-1)`($out,$in0) # write output
  273. ___
  274. $code.=<<___;
  275. xorps $in,$iv
  276. movups `32+16*$k-112`($key),$rndkey[1]
  277. aesenc $rndkey[0],$iv
  278. ___
  279. } elsif ($k==9) {
  280. $sn++;
  281. $code.=<<___;
  282. cmp \$11,$rounds
  283. jb .Laesenclast$sn
  284. movups `32+16*($k+0)-112`($key),$rndkey[1]
  285. aesenc $rndkey[0],$iv
  286. movups `32+16*($k+1)-112`($key),$rndkey[0]
  287. aesenc $rndkey[1],$iv
  288. je .Laesenclast$sn
  289. movups `32+16*($k+2)-112`($key),$rndkey[1]
  290. aesenc $rndkey[0],$iv
  291. movups `32+16*($k+3)-112`($key),$rndkey[0]
  292. aesenc $rndkey[1],$iv
  293. .Laesenclast$sn:
  294. aesenclast $rndkey[0],$iv
  295. movups 16-112($key),$rndkey[1] # forward reference
  296. ___
  297. } else {
  298. $code.=<<___;
  299. movups `32+16*$k-112`($key),$rndkey[1]
  300. aesenc $rndkey[0],$iv
  301. ___
  302. }
  303. $r++; unshift(@rndkey,pop(@rndkey));
  304. };
  305. sub Xupdate_ssse3_16_31() # recall that $Xi starts with 4
  306. { use integer;
  307. my $body = shift;
  308. my @insns = (&$body,&$body,&$body,&$body); # 40 instructions
  309. my ($a,$b,$c,$d,$e);
  310. eval(shift(@insns)); # ror
  311. &pshufd (@X[0],@X[-4&7],0xee); # was &movdqa (@X[0],@X[-3&7]);
  312. eval(shift(@insns));
  313. &movdqa (@Tx[0],@X[-1&7]);
  314. &paddd (@Tx[1],@X[-1&7]);
  315. eval(shift(@insns));
  316. eval(shift(@insns));
  317. &punpcklqdq(@X[0],@X[-3&7]); # compose "X[-14]" in "X[0]", was &palignr(@X[0],@X[-4&7],8);
  318. eval(shift(@insns));
  319. eval(shift(@insns)); # rol
  320. eval(shift(@insns));
  321. &psrldq (@Tx[0],4); # "X[-3]", 3 dwords
  322. eval(shift(@insns));
  323. eval(shift(@insns));
  324. &pxor (@X[0],@X[-4&7]); # "X[0]"^="X[-16]"
  325. eval(shift(@insns));
  326. eval(shift(@insns)); # ror
  327. &pxor (@Tx[0],@X[-2&7]); # "X[-3]"^"X[-8]"
  328. eval(shift(@insns));
  329. eval(shift(@insns));
  330. eval(shift(@insns));
  331. &pxor (@X[0],@Tx[0]); # "X[0]"^="X[-3]"^"X[-8]"
  332. eval(shift(@insns));
  333. eval(shift(@insns)); # rol
  334. &movdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
  335. eval(shift(@insns));
  336. eval(shift(@insns));
  337. &movdqa (@Tx[2],@X[0]);
  338. eval(shift(@insns));
  339. eval(shift(@insns));
  340. eval(shift(@insns)); # ror
  341. &movdqa (@Tx[0],@X[0]);
  342. eval(shift(@insns));
  343. &pslldq (@Tx[2],12); # "X[0]"<<96, extract one dword
  344. &paddd (@X[0],@X[0]);
  345. eval(shift(@insns));
  346. eval(shift(@insns));
  347. &psrld (@Tx[0],31);
  348. eval(shift(@insns));
  349. eval(shift(@insns)); # rol
  350. eval(shift(@insns));
  351. &movdqa (@Tx[1],@Tx[2]);
  352. eval(shift(@insns));
  353. eval(shift(@insns));
  354. &psrld (@Tx[2],30);
  355. eval(shift(@insns));
  356. eval(shift(@insns)); # ror
  357. &por (@X[0],@Tx[0]); # "X[0]"<<<=1
  358. eval(shift(@insns));
  359. eval(shift(@insns));
  360. eval(shift(@insns));
  361. &pslld (@Tx[1],2);
  362. &pxor (@X[0],@Tx[2]);
  363. eval(shift(@insns));
  364. &movdqa (@Tx[2],eval(16*(($Xi)/5))."($K_XX_XX)"); # K_XX_XX
  365. eval(shift(@insns)); # rol
  366. eval(shift(@insns));
  367. eval(shift(@insns));
  368. &pxor (@X[0],@Tx[1]); # "X[0]"^=("X[0]">>96)<<<2
  369. &pshufd (@Tx[1],@X[-1&7],0xee) if ($Xi==7); # was &movdqa (@Tx[0],@X[-1&7]) in Xupdate_ssse3_32_79
  370. foreach (@insns) { eval; } # remaining instructions [if any]
  371. $Xi++; push(@X,shift(@X)); # "rotate" X[]
  372. push(@Tx,shift(@Tx));
  373. }
  374. sub Xupdate_ssse3_32_79()
  375. { use integer;
  376. my $body = shift;
  377. my @insns = (&$body,&$body,&$body,&$body); # 32 to 44 instructions
  378. my ($a,$b,$c,$d,$e);
  379. eval(shift(@insns)) if ($Xi==8);
  380. &pxor (@X[0],@X[-4&7]); # "X[0]"="X[-32]"^"X[-16]"
  381. eval(shift(@insns)) if ($Xi==8);
  382. eval(shift(@insns)); # body_20_39
  383. eval(shift(@insns));
  384. eval(shift(@insns)) if (@insns[1] =~ /_ror/);
  385. eval(shift(@insns)) if (@insns[0] =~ /_ror/);
  386. &punpcklqdq(@Tx[0],@X[-1&7]); # compose "X[-6]", was &palignr(@Tx[0],@X[-2&7],8);
  387. eval(shift(@insns));
  388. eval(shift(@insns)); # rol
  389. &pxor (@X[0],@X[-7&7]); # "X[0]"^="X[-28]"
  390. eval(shift(@insns));
  391. eval(shift(@insns));
  392. if ($Xi%5) {
  393. &movdqa (@Tx[2],@Tx[1]);# "perpetuate" K_XX_XX...
  394. } else { # ... or load next one
  395. &movdqa (@Tx[2],eval(16*($Xi/5))."($K_XX_XX)");
  396. }
  397. eval(shift(@insns)); # ror
  398. &paddd (@Tx[1],@X[-1&7]);
  399. eval(shift(@insns));
  400. &pxor (@X[0],@Tx[0]); # "X[0]"^="X[-6]"
  401. eval(shift(@insns)); # body_20_39
  402. eval(shift(@insns));
  403. eval(shift(@insns));
  404. eval(shift(@insns)); # rol
  405. eval(shift(@insns)) if (@insns[0] =~ /_ror/);
  406. &movdqa (@Tx[0],@X[0]);
  407. eval(shift(@insns));
  408. eval(shift(@insns));
  409. &movdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
  410. eval(shift(@insns)); # ror
  411. eval(shift(@insns));
  412. eval(shift(@insns)); # body_20_39
  413. &pslld (@X[0],2);
  414. eval(shift(@insns));
  415. eval(shift(@insns));
  416. &psrld (@Tx[0],30);
  417. eval(shift(@insns)) if (@insns[0] =~ /_rol/);# rol
  418. eval(shift(@insns));
  419. eval(shift(@insns));
  420. eval(shift(@insns)); # ror
  421. &por (@X[0],@Tx[0]); # "X[0]"<<<=2
  422. eval(shift(@insns));
  423. eval(shift(@insns)); # body_20_39
  424. eval(shift(@insns)) if (@insns[1] =~ /_rol/);
  425. eval(shift(@insns)) if (@insns[0] =~ /_rol/);
  426. &pshufd(@Tx[1],@X[-1&7],0xee) if ($Xi<19); # was &movdqa (@Tx[1],@X[0])
  427. eval(shift(@insns));
  428. eval(shift(@insns)); # rol
  429. eval(shift(@insns));
  430. eval(shift(@insns));
  431. eval(shift(@insns)); # rol
  432. eval(shift(@insns));
  433. foreach (@insns) { eval; } # remaining instructions
  434. $Xi++; push(@X,shift(@X)); # "rotate" X[]
  435. push(@Tx,shift(@Tx));
  436. }
  437. sub Xuplast_ssse3_80()
  438. { use integer;
  439. my $body = shift;
  440. my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
  441. my ($a,$b,$c,$d,$e);
  442. eval(shift(@insns));
  443. eval(shift(@insns));
  444. eval(shift(@insns));
  445. eval(shift(@insns));
  446. &paddd (@Tx[1],@X[-1&7]);
  447. eval(shift(@insns));
  448. eval(shift(@insns));
  449. &movdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer IALU
  450. foreach (@insns) { eval; } # remaining instructions
  451. &cmp ($inp,$len);
  452. &je (shift);
  453. unshift(@Tx,pop(@Tx));
  454. &movdqa (@Tx[2],"64($K_XX_XX)"); # pbswap mask
  455. &movdqa (@Tx[1],"0($K_XX_XX)"); # K_00_19
  456. &movdqu (@X[-4&7],"0($inp)"); # load input
  457. &movdqu (@X[-3&7],"16($inp)");
  458. &movdqu (@X[-2&7],"32($inp)");
  459. &movdqu (@X[-1&7],"48($inp)");
  460. &pshufb (@X[-4&7],@Tx[2]); # byte swap
  461. &add ($inp,64);
  462. $Xi=0;
  463. }
  464. sub Xloop_ssse3()
  465. { use integer;
  466. my $body = shift;
  467. my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
  468. my ($a,$b,$c,$d,$e);
  469. eval(shift(@insns));
  470. eval(shift(@insns));
  471. eval(shift(@insns));
  472. &pshufb (@X[($Xi-3)&7],@Tx[2]);
  473. eval(shift(@insns));
  474. eval(shift(@insns));
  475. eval(shift(@insns));
  476. eval(shift(@insns));
  477. &paddd (@X[($Xi-4)&7],@Tx[1]);
  478. eval(shift(@insns));
  479. eval(shift(@insns));
  480. eval(shift(@insns));
  481. eval(shift(@insns));
  482. &movdqa (eval(16*$Xi)."(%rsp)",@X[($Xi-4)&7]); # X[]+K xfer to IALU
  483. eval(shift(@insns));
  484. eval(shift(@insns));
  485. eval(shift(@insns));
  486. eval(shift(@insns));
  487. &psubd (@X[($Xi-4)&7],@Tx[1]);
  488. foreach (@insns) { eval; }
  489. $Xi++;
  490. }
  491. sub Xtail_ssse3()
  492. { use integer;
  493. my $body = shift;
  494. my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
  495. my ($a,$b,$c,$d,$e);
  496. foreach (@insns) { eval; }
  497. }
  498. my @body_00_19 = (
  499. '($a,$b,$c,$d,$e)=@V;'.
  500. '&$_ror ($b,$j?7:2);', # $b>>>2
  501. '&xor (@T[0],$d);',
  502. '&mov (@T[1],$a);', # $b for next round
  503. '&add ($e,eval(4*($j&15))."(%rsp)");',# X[]+K xfer
  504. '&xor ($b,$c);', # $c^$d for next round
  505. '&$_rol ($a,5);',
  506. '&add ($e,@T[0]);',
  507. '&and (@T[1],$b);', # ($b&($c^$d)) for next round
  508. '&xor ($b,$c);', # restore $b
  509. '&add ($e,$a);' .'$j++; unshift(@V,pop(@V)); unshift(@T,pop(@T));'
  510. );
  511. sub body_00_19 () { # ((c^d)&b)^d
  512. # on start @T[0]=(c^d)&b
  513. return &body_20_39() if ($rx==19); $rx++;
  514. use integer;
  515. my ($k,$n);
  516. my @r=@body_00_19;
  517. $n = scalar(@r);
  518. $k = (($jj+1)*12/20)*20*$n/12; # 12 aesencs per these 20 rounds
  519. @r[$k%$n].='&$aesenc();' if ($jj==$k/$n);
  520. $jj++;
  521. return @r;
  522. }
  523. my @body_20_39 = (
  524. '($a,$b,$c,$d,$e)=@V;'.
  525. '&add ($e,eval(4*($j&15))."(%rsp)");',# X[]+K xfer
  526. '&xor (@T[0],$d) if($j==19);'.
  527. '&xor (@T[0],$c) if($j> 19);', # ($b^$d^$c)
  528. '&mov (@T[1],$a);', # $b for next round
  529. '&$_rol ($a,5);',
  530. '&add ($e,@T[0]);',
  531. '&xor (@T[1],$c) if ($j< 79);', # $b^$d for next round
  532. '&$_ror ($b,7);', # $b>>>2
  533. '&add ($e,$a);' .'$j++; unshift(@V,pop(@V)); unshift(@T,pop(@T));'
  534. );
  535. sub body_20_39 () { # b^d^c
  536. # on entry @T[0]=b^d
  537. return &body_40_59() if ($rx==39); $rx++;
  538. use integer;
  539. my ($k,$n);
  540. my @r=@body_20_39;
  541. $n = scalar(@r);
  542. $k = (($jj+1)*8/20)*20*$n/8; # 8 aesencs per these 20 rounds
  543. @r[$k%$n].='&$aesenc();' if ($jj==$k/$n && $rx!=20);
  544. $jj++;
  545. return @r;
  546. }
  547. my @body_40_59 = (
  548. '($a,$b,$c,$d,$e)=@V;'.
  549. '&add ($e,eval(4*($j&15))."(%rsp)");',# X[]+K xfer
  550. '&and (@T[0],$c) if ($j>=40);', # (b^c)&(c^d)
  551. '&xor ($c,$d) if ($j>=40);', # restore $c
  552. '&$_ror ($b,7);', # $b>>>2
  553. '&mov (@T[1],$a);', # $b for next round
  554. '&xor (@T[0],$c);',
  555. '&$_rol ($a,5);',
  556. '&add ($e,@T[0]);',
  557. '&xor (@T[1],$c) if ($j==59);'.
  558. '&xor (@T[1],$b) if ($j< 59);', # b^c for next round
  559. '&xor ($b,$c) if ($j< 59);', # c^d for next round
  560. '&add ($e,$a);' .'$j++; unshift(@V,pop(@V)); unshift(@T,pop(@T));'
  561. );
  562. sub body_40_59 () { # ((b^c)&(c^d))^c
  563. # on entry @T[0]=(b^c), (c^=d)
  564. $rx++;
  565. use integer;
  566. my ($k,$n);
  567. my @r=@body_40_59;
  568. $n = scalar(@r);
  569. $k=(($jj+1)*12/20)*20*$n/12; # 12 aesencs per these 20 rounds
  570. @r[$k%$n].='&$aesenc();' if ($jj==$k/$n && $rx!=40);
  571. $jj++;
  572. return @r;
  573. }
  574. $code.=<<___;
  575. .align 32
  576. .Loop_ssse3:
  577. ___
  578. &Xupdate_ssse3_16_31(\&body_00_19);
  579. &Xupdate_ssse3_16_31(\&body_00_19);
  580. &Xupdate_ssse3_16_31(\&body_00_19);
  581. &Xupdate_ssse3_16_31(\&body_00_19);
  582. &Xupdate_ssse3_32_79(\&body_00_19);
  583. &Xupdate_ssse3_32_79(\&body_20_39);
  584. &Xupdate_ssse3_32_79(\&body_20_39);
  585. &Xupdate_ssse3_32_79(\&body_20_39);
  586. &Xupdate_ssse3_32_79(\&body_20_39);
  587. &Xupdate_ssse3_32_79(\&body_20_39);
  588. &Xupdate_ssse3_32_79(\&body_40_59);
  589. &Xupdate_ssse3_32_79(\&body_40_59);
  590. &Xupdate_ssse3_32_79(\&body_40_59);
  591. &Xupdate_ssse3_32_79(\&body_40_59);
  592. &Xupdate_ssse3_32_79(\&body_40_59);
  593. &Xupdate_ssse3_32_79(\&body_20_39);
  594. &Xuplast_ssse3_80(\&body_20_39,".Ldone_ssse3"); # can jump to "done"
  595. $saved_j=$j; @saved_V=@V;
  596. $saved_r=$r; @saved_rndkey=@rndkey;
  597. &Xloop_ssse3(\&body_20_39);
  598. &Xloop_ssse3(\&body_20_39);
  599. &Xloop_ssse3(\&body_20_39);
  600. $code.=<<___;
  601. movups $iv,48($out,$in0) # write output
  602. lea 64($in0),$in0
  603. add 0($ctx),$A # update context
  604. add 4($ctx),@T[0]
  605. add 8($ctx),$C
  606. add 12($ctx),$D
  607. mov $A,0($ctx)
  608. add 16($ctx),$E
  609. mov @T[0],4($ctx)
  610. mov @T[0],$B # magic seed
  611. mov $C,8($ctx)
  612. mov $C,@T[1]
  613. mov $D,12($ctx)
  614. xor $D,@T[1]
  615. mov $E,16($ctx)
  616. and @T[1],@T[0]
  617. jmp .Loop_ssse3
  618. .Ldone_ssse3:
  619. ___
  620. $jj=$j=$saved_j; @V=@saved_V;
  621. $r=$saved_r; @rndkey=@saved_rndkey;
  622. &Xtail_ssse3(\&body_20_39);
  623. &Xtail_ssse3(\&body_20_39);
  624. &Xtail_ssse3(\&body_20_39);
  625. $code.=<<___;
  626. movups $iv,48($out,$in0) # write output
  627. mov 88(%rsp),$ivp # restore $ivp
  628. add 0($ctx),$A # update context
  629. add 4($ctx),@T[0]
  630. add 8($ctx),$C
  631. mov $A,0($ctx)
  632. add 12($ctx),$D
  633. mov @T[0],4($ctx)
  634. add 16($ctx),$E
  635. mov $C,8($ctx)
  636. mov $D,12($ctx)
  637. mov $E,16($ctx)
  638. movups $iv,($ivp) # write IV
  639. ___
  640. $code.=<<___ if ($win64);
  641. movaps 96+0(%rsp),%xmm6
  642. movaps 96+16(%rsp),%xmm7
  643. movaps 96+32(%rsp),%xmm8
  644. movaps 96+48(%rsp),%xmm9
  645. movaps 96+64(%rsp),%xmm10
  646. movaps 96+80(%rsp),%xmm11
  647. movaps 96+96(%rsp),%xmm12
  648. movaps 96+112(%rsp),%xmm13
  649. movaps 96+128(%rsp),%xmm14
  650. movaps 96+144(%rsp),%xmm15
  651. ___
  652. $code.=<<___;
  653. lea `104+($win64?10*16:0)`(%rsp),%rsi
  654. .cfi_def_cfa %rsi,56
  655. mov 0(%rsi),%r15
  656. .cfi_restore %r15
  657. mov 8(%rsi),%r14
  658. .cfi_restore %r14
  659. mov 16(%rsi),%r13
  660. .cfi_restore %r13
  661. mov 24(%rsi),%r12
  662. .cfi_restore %r12
  663. mov 32(%rsi),%rbp
  664. .cfi_restore %rbp
  665. mov 40(%rsi),%rbx
  666. .cfi_restore %rbx
  667. lea 48(%rsi),%rsp
  668. .cfi_def_cfa %rsp,8
  669. .Lepilogue_ssse3:
  670. ret
  671. .cfi_endproc
  672. .size aesni_cbc_sha1_enc_ssse3,.-aesni_cbc_sha1_enc_ssse3
  673. ___
  674. if ($stitched_decrypt) {{{
  675. # reset
  676. ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
  677. $j=$jj=$r=$rx=0;
  678. $Xi=4;
  679. # reassign for Atom Silvermont (see above)
  680. ($inout0,$inout1,$inout2,$inout3,$rndkey0)=map("%xmm$_",(0..4));
  681. @X=map("%xmm$_",(8..13,6,7));
  682. @Tx=map("%xmm$_",(14,15,5));
  683. my @aes256_dec = (
  684. '&movdqu($inout0,"0x00($in0)");',
  685. '&movdqu($inout1,"0x10($in0)"); &pxor ($inout0,$rndkey0);',
  686. '&movdqu($inout2,"0x20($in0)"); &pxor ($inout1,$rndkey0);',
  687. '&movdqu($inout3,"0x30($in0)"); &pxor ($inout2,$rndkey0);',
  688. '&pxor ($inout3,$rndkey0); &movups ($rndkey0,"16-112($key)");',
  689. '&movaps("64(%rsp)",@X[2]);', # save IV, originally @X[3]
  690. undef,undef
  691. );
  692. for ($i=0;$i<13;$i++) {
  693. push (@aes256_dec,(
  694. '&aesdec ($inout0,$rndkey0);',
  695. '&aesdec ($inout1,$rndkey0);',
  696. '&aesdec ($inout2,$rndkey0);',
  697. '&aesdec ($inout3,$rndkey0); &movups($rndkey0,"'.(16*($i+2)-112).'($key)");'
  698. ));
  699. push (@aes256_dec,(undef,undef)) if (($i>=3 && $i<=5) || $i>=11);
  700. push (@aes256_dec,(undef,undef)) if ($i==5);
  701. }
  702. push(@aes256_dec,(
  703. '&aesdeclast ($inout0,$rndkey0); &movups (@X[0],"0x00($in0)");',
  704. '&aesdeclast ($inout1,$rndkey0); &movups (@X[1],"0x10($in0)");',
  705. '&aesdeclast ($inout2,$rndkey0); &movups (@X[2],"0x20($in0)");',
  706. '&aesdeclast ($inout3,$rndkey0); &movups (@X[3],"0x30($in0)");',
  707. '&xorps ($inout0,"64(%rsp)"); &movdqu ($rndkey0,"-112($key)");',
  708. '&xorps ($inout1,@X[0]); &movups ("0x00($out,$in0)",$inout0);',
  709. '&xorps ($inout2,@X[1]); &movups ("0x10($out,$in0)",$inout1);',
  710. '&xorps ($inout3,@X[2]); &movups ("0x20($out,$in0)",$inout2);',
  711. '&movups ("0x30($out,$in0)",$inout3);'
  712. ));
  713. sub body_00_19_dec () { # ((c^d)&b)^d
  714. # on start @T[0]=(c^d)&b
  715. return &body_20_39_dec() if ($rx==19);
  716. my @r=@body_00_19;
  717. unshift (@r,@aes256_dec[$rx]) if (@aes256_dec[$rx]);
  718. $rx++;
  719. return @r;
  720. }
  721. sub body_20_39_dec () { # b^d^c
  722. # on entry @T[0]=b^d
  723. return &body_40_59_dec() if ($rx==39);
  724. my @r=@body_20_39;
  725. unshift (@r,@aes256_dec[$rx]) if (@aes256_dec[$rx]);
  726. $rx++;
  727. return @r;
  728. }
  729. sub body_40_59_dec () { # ((b^c)&(c^d))^c
  730. # on entry @T[0]=(b^c), (c^=d)
  731. my @r=@body_40_59;
  732. unshift (@r,@aes256_dec[$rx]) if (@aes256_dec[$rx]);
  733. $rx++;
  734. return @r;
  735. }
  736. $code.=<<___;
  737. .globl aesni256_cbc_sha1_dec
  738. .type aesni256_cbc_sha1_dec,\@abi-omnipotent
  739. .align 32
  740. aesni256_cbc_sha1_dec:
  741. .cfi_startproc
  742. # caller should check for SSSE3 and AES-NI bits
  743. mov OPENSSL_ia32cap_P+0(%rip),%r10d
  744. mov OPENSSL_ia32cap_P+4(%rip),%r11d
  745. ___
  746. $code.=<<___ if ($avx);
  747. and \$`1<<28`,%r11d # mask AVX bit
  748. and \$`1<<30`,%r10d # mask "Intel CPU" bit
  749. or %r11d,%r10d
  750. cmp \$`1<<28|1<<30`,%r10d
  751. je aesni256_cbc_sha1_dec_avx
  752. ___
  753. $code.=<<___;
  754. jmp aesni256_cbc_sha1_dec_ssse3
  755. ret
  756. .cfi_endproc
  757. .size aesni256_cbc_sha1_dec,.-aesni256_cbc_sha1_dec
  758. .type aesni256_cbc_sha1_dec_ssse3,\@function,6
  759. .align 32
  760. aesni256_cbc_sha1_dec_ssse3:
  761. .cfi_startproc
  762. mov `($win64?56:8)`(%rsp),$inp # load 7th argument
  763. push %rbx
  764. .cfi_push %rbx
  765. push %rbp
  766. .cfi_push %rbp
  767. push %r12
  768. .cfi_push %r12
  769. push %r13
  770. .cfi_push %r13
  771. push %r14
  772. .cfi_push %r14
  773. push %r15
  774. .cfi_push %r15
  775. lea `-104-($win64?10*16:0)`(%rsp),%rsp
  776. .cfi_adjust_cfa_offset `104+($win64?10*16:0)`
  777. ___
  778. $code.=<<___ if ($win64);
  779. movaps %xmm6,96+0(%rsp)
  780. movaps %xmm7,96+16(%rsp)
  781. movaps %xmm8,96+32(%rsp)
  782. movaps %xmm9,96+48(%rsp)
  783. movaps %xmm10,96+64(%rsp)
  784. movaps %xmm11,96+80(%rsp)
  785. movaps %xmm12,96+96(%rsp)
  786. movaps %xmm13,96+112(%rsp)
  787. movaps %xmm14,96+128(%rsp)
  788. movaps %xmm15,96+144(%rsp)
  789. .Lprologue_dec_ssse3:
  790. ___
  791. $code.=<<___;
  792. mov $in0,%r12 # reassign arguments
  793. mov $out,%r13
  794. mov $len,%r14
  795. lea 112($key),%r15 # size optimization
  796. movdqu ($ivp),@X[3] # load IV
  797. #mov $ivp,88(%rsp) # save $ivp
  798. ___
  799. ($in0,$out,$len,$key)=map("%r$_",(12..15)); # reassign arguments
  800. $code.=<<___;
  801. shl \$6,$len
  802. sub $in0,$out
  803. add $inp,$len # end of input
  804. lea K_XX_XX(%rip),$K_XX_XX
  805. mov 0($ctx),$A # load context
  806. mov 4($ctx),$B
  807. mov 8($ctx),$C
  808. mov 12($ctx),$D
  809. mov $B,@T[0] # magic seed
  810. mov 16($ctx),$E
  811. mov $C,@T[1]
  812. xor $D,@T[1]
  813. and @T[1],@T[0]
  814. movdqa 64($K_XX_XX),@Tx[2] # pbswap mask
  815. movdqa 0($K_XX_XX),@Tx[1] # K_00_19
  816. movdqu 0($inp),@X[-4&7] # load input to %xmm[0-3]
  817. movdqu 16($inp),@X[-3&7]
  818. movdqu 32($inp),@X[-2&7]
  819. movdqu 48($inp),@X[-1&7]
  820. pshufb @Tx[2],@X[-4&7] # byte swap
  821. add \$64,$inp
  822. pshufb @Tx[2],@X[-3&7]
  823. pshufb @Tx[2],@X[-2&7]
  824. pshufb @Tx[2],@X[-1&7]
  825. paddd @Tx[1],@X[-4&7] # add K_00_19
  826. paddd @Tx[1],@X[-3&7]
  827. paddd @Tx[1],@X[-2&7]
  828. movdqa @X[-4&7],0(%rsp) # X[]+K xfer to IALU
  829. psubd @Tx[1],@X[-4&7] # restore X[]
  830. movdqa @X[-3&7],16(%rsp)
  831. psubd @Tx[1],@X[-3&7]
  832. movdqa @X[-2&7],32(%rsp)
  833. psubd @Tx[1],@X[-2&7]
  834. movdqu -112($key),$rndkey0 # $key[0]
  835. jmp .Loop_dec_ssse3
  836. .align 32
  837. .Loop_dec_ssse3:
  838. ___
  839. &Xupdate_ssse3_16_31(\&body_00_19_dec);
  840. &Xupdate_ssse3_16_31(\&body_00_19_dec);
  841. &Xupdate_ssse3_16_31(\&body_00_19_dec);
  842. &Xupdate_ssse3_16_31(\&body_00_19_dec);
  843. &Xupdate_ssse3_32_79(\&body_00_19_dec);
  844. &Xupdate_ssse3_32_79(\&body_20_39_dec);
  845. &Xupdate_ssse3_32_79(\&body_20_39_dec);
  846. &Xupdate_ssse3_32_79(\&body_20_39_dec);
  847. &Xupdate_ssse3_32_79(\&body_20_39_dec);
  848. &Xupdate_ssse3_32_79(\&body_20_39_dec);
  849. &Xupdate_ssse3_32_79(\&body_40_59_dec);
  850. &Xupdate_ssse3_32_79(\&body_40_59_dec);
  851. &Xupdate_ssse3_32_79(\&body_40_59_dec);
  852. &Xupdate_ssse3_32_79(\&body_40_59_dec);
  853. &Xupdate_ssse3_32_79(\&body_40_59_dec);
  854. &Xupdate_ssse3_32_79(\&body_20_39_dec);
  855. &Xuplast_ssse3_80(\&body_20_39_dec,".Ldone_dec_ssse3"); # can jump to "done"
  856. $saved_j=$j; @saved_V=@V;
  857. $saved_rx=$rx;
  858. &Xloop_ssse3(\&body_20_39_dec);
  859. &Xloop_ssse3(\&body_20_39_dec);
  860. &Xloop_ssse3(\&body_20_39_dec);
  861. eval(@aes256_dec[-1]); # last store
  862. $code.=<<___;
  863. lea 64($in0),$in0
  864. add 0($ctx),$A # update context
  865. add 4($ctx),@T[0]
  866. add 8($ctx),$C
  867. add 12($ctx),$D
  868. mov $A,0($ctx)
  869. add 16($ctx),$E
  870. mov @T[0],4($ctx)
  871. mov @T[0],$B # magic seed
  872. mov $C,8($ctx)
  873. mov $C,@T[1]
  874. mov $D,12($ctx)
  875. xor $D,@T[1]
  876. mov $E,16($ctx)
  877. and @T[1],@T[0]
  878. jmp .Loop_dec_ssse3
  879. .Ldone_dec_ssse3:
  880. ___
  881. $jj=$j=$saved_j; @V=@saved_V;
  882. $rx=$saved_rx;
  883. &Xtail_ssse3(\&body_20_39_dec);
  884. &Xtail_ssse3(\&body_20_39_dec);
  885. &Xtail_ssse3(\&body_20_39_dec);
  886. eval(@aes256_dec[-1]); # last store
  887. $code.=<<___;
  888. add 0($ctx),$A # update context
  889. add 4($ctx),@T[0]
  890. add 8($ctx),$C
  891. mov $A,0($ctx)
  892. add 12($ctx),$D
  893. mov @T[0],4($ctx)
  894. add 16($ctx),$E
  895. mov $C,8($ctx)
  896. mov $D,12($ctx)
  897. mov $E,16($ctx)
  898. movups @X[3],($ivp) # write IV
  899. ___
  900. $code.=<<___ if ($win64);
  901. movaps 96+0(%rsp),%xmm6
  902. movaps 96+16(%rsp),%xmm7
  903. movaps 96+32(%rsp),%xmm8
  904. movaps 96+48(%rsp),%xmm9
  905. movaps 96+64(%rsp),%xmm10
  906. movaps 96+80(%rsp),%xmm11
  907. movaps 96+96(%rsp),%xmm12
  908. movaps 96+112(%rsp),%xmm13
  909. movaps 96+128(%rsp),%xmm14
  910. movaps 96+144(%rsp),%xmm15
  911. ___
  912. $code.=<<___;
  913. lea `104+($win64?10*16:0)`(%rsp),%rsi
  914. .cfi_cfa_def %rsi,56
  915. mov 0(%rsi),%r15
  916. .cfi_restore %r15
  917. mov 8(%rsi),%r14
  918. .cfi_restore %r14
  919. mov 16(%rsi),%r13
  920. .cfi_restore %r13
  921. mov 24(%rsi),%r12
  922. .cfi_restore %r12
  923. mov 32(%rsi),%rbp
  924. .cfi_restore %rbp
  925. mov 40(%rsi),%rbx
  926. .cfi_restore %rbx
  927. lea 48(%rsi),%rsp
  928. .cfi_cfa_def %rsp,8
  929. .Lepilogue_dec_ssse3:
  930. ret
  931. .cfi_endproc
  932. .size aesni256_cbc_sha1_dec_ssse3,.-aesni256_cbc_sha1_dec_ssse3
  933. ___
  934. }}}
  935. $j=$jj=$r=$rx=0;
  936. if ($avx) {
  937. my ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
  938. my $Xi=4;
  939. my @X=map("%xmm$_",(4..7,0..3));
  940. my @Tx=map("%xmm$_",(8..10));
  941. my @V=($A,$B,$C,$D,$E)=("%eax","%ebx","%ecx","%edx","%ebp"); # size optimization
  942. my @T=("%esi","%edi");
  943. my ($rndkey0,$iv,$in)=map("%xmm$_",(11..13));
  944. my @rndkey=("%xmm14","%xmm15");
  945. my ($inout0,$inout1,$inout2,$inout3)=map("%xmm$_",(12..15)); # for dec
  946. my $Kx=@Tx[2];
  947. my $_rol=sub { &shld(@_[0],@_) };
  948. my $_ror=sub { &shrd(@_[0],@_) };
  949. $code.=<<___;
  950. .type aesni_cbc_sha1_enc_avx,\@function,6
  951. .align 32
  952. aesni_cbc_sha1_enc_avx:
  953. .cfi_startproc
  954. mov `($win64?56:8)`(%rsp),$inp # load 7th argument
  955. #shr \$6,$len # debugging artefact
  956. #jz .Lepilogue_avx # debugging artefact
  957. push %rbx
  958. .cfi_push %rbx
  959. push %rbp
  960. .cfi_push %rbp
  961. push %r12
  962. .cfi_push %r12
  963. push %r13
  964. .cfi_push %r13
  965. push %r14
  966. .cfi_push %r14
  967. push %r15
  968. .cfi_push %r15
  969. lea `-104-($win64?10*16:0)`(%rsp),%rsp
  970. .cfi_adjust_cfa_offset `104+($win64?10*16:0)`
  971. #mov $in0,$inp # debugging artefact
  972. #lea 64(%rsp),$ctx # debugging artefact
  973. ___
  974. $code.=<<___ if ($win64);
  975. movaps %xmm6,96+0(%rsp)
  976. movaps %xmm7,96+16(%rsp)
  977. movaps %xmm8,96+32(%rsp)
  978. movaps %xmm9,96+48(%rsp)
  979. movaps %xmm10,96+64(%rsp)
  980. movaps %xmm11,96+80(%rsp)
  981. movaps %xmm12,96+96(%rsp)
  982. movaps %xmm13,96+112(%rsp)
  983. movaps %xmm14,96+128(%rsp)
  984. movaps %xmm15,96+144(%rsp)
  985. .Lprologue_avx:
  986. ___
  987. $code.=<<___;
  988. vzeroall
  989. mov $in0,%r12 # reassign arguments
  990. mov $out,%r13
  991. mov $len,%r14
  992. lea 112($key),%r15 # size optimization
  993. vmovdqu ($ivp),$iv # load IV
  994. mov $ivp,88(%rsp) # save $ivp
  995. ___
  996. ($in0,$out,$len,$key)=map("%r$_",(12..15)); # reassign arguments
  997. my $rounds="${ivp}d";
  998. $code.=<<___;
  999. shl \$6,$len
  1000. sub $in0,$out
  1001. mov 240-112($key),$rounds
  1002. add $inp,$len # end of input
  1003. lea K_XX_XX(%rip),$K_XX_XX
  1004. mov 0($ctx),$A # load context
  1005. mov 4($ctx),$B
  1006. mov 8($ctx),$C
  1007. mov 12($ctx),$D
  1008. mov $B,@T[0] # magic seed
  1009. mov 16($ctx),$E
  1010. mov $C,@T[1]
  1011. xor $D,@T[1]
  1012. and @T[1],@T[0]
  1013. vmovdqa 64($K_XX_XX),@X[2] # pbswap mask
  1014. vmovdqa 0($K_XX_XX),$Kx # K_00_19
  1015. vmovdqu 0($inp),@X[-4&7] # load input to %xmm[0-3]
  1016. vmovdqu 16($inp),@X[-3&7]
  1017. vmovdqu 32($inp),@X[-2&7]
  1018. vmovdqu 48($inp),@X[-1&7]
  1019. vpshufb @X[2],@X[-4&7],@X[-4&7] # byte swap
  1020. add \$64,$inp
  1021. vpshufb @X[2],@X[-3&7],@X[-3&7]
  1022. vpshufb @X[2],@X[-2&7],@X[-2&7]
  1023. vpshufb @X[2],@X[-1&7],@X[-1&7]
  1024. vpaddd $Kx,@X[-4&7],@X[0] # add K_00_19
  1025. vpaddd $Kx,@X[-3&7],@X[1]
  1026. vpaddd $Kx,@X[-2&7],@X[2]
  1027. vmovdqa @X[0],0(%rsp) # X[]+K xfer to IALU
  1028. vmovdqa @X[1],16(%rsp)
  1029. vmovdqa @X[2],32(%rsp)
  1030. vmovups -112($key),$rndkey[1] # $key[0]
  1031. vmovups 16-112($key),$rndkey[0] # forward reference
  1032. jmp .Loop_avx
  1033. ___
  1034. my $aesenc=sub {
  1035. use integer;
  1036. my ($n,$k)=($r/10,$r%10);
  1037. if ($k==0) {
  1038. $code.=<<___;
  1039. vmovdqu `16*$n`($in0),$in # load input
  1040. vpxor $rndkey[1],$in,$in
  1041. ___
  1042. $code.=<<___ if ($n);
  1043. vmovups $iv,`16*($n-1)`($out,$in0) # write output
  1044. ___
  1045. $code.=<<___;
  1046. vpxor $in,$iv,$iv
  1047. vaesenc $rndkey[0],$iv,$iv
  1048. vmovups `32+16*$k-112`($key),$rndkey[1]
  1049. ___
  1050. } elsif ($k==9) {
  1051. $sn++;
  1052. $code.=<<___;
  1053. cmp \$11,$rounds
  1054. jb .Lvaesenclast$sn
  1055. vaesenc $rndkey[0],$iv,$iv
  1056. vmovups `32+16*($k+0)-112`($key),$rndkey[1]
  1057. vaesenc $rndkey[1],$iv,$iv
  1058. vmovups `32+16*($k+1)-112`($key),$rndkey[0]
  1059. je .Lvaesenclast$sn
  1060. vaesenc $rndkey[0],$iv,$iv
  1061. vmovups `32+16*($k+2)-112`($key),$rndkey[1]
  1062. vaesenc $rndkey[1],$iv,$iv
  1063. vmovups `32+16*($k+3)-112`($key),$rndkey[0]
  1064. .Lvaesenclast$sn:
  1065. vaesenclast $rndkey[0],$iv,$iv
  1066. vmovups -112($key),$rndkey[0]
  1067. vmovups 16-112($key),$rndkey[1] # forward reference
  1068. ___
  1069. } else {
  1070. $code.=<<___;
  1071. vaesenc $rndkey[0],$iv,$iv
  1072. vmovups `32+16*$k-112`($key),$rndkey[1]
  1073. ___
  1074. }
  1075. $r++; unshift(@rndkey,pop(@rndkey));
  1076. };
  1077. sub Xupdate_avx_16_31() # recall that $Xi starts with 4
  1078. { use integer;
  1079. my $body = shift;
  1080. my @insns = (&$body,&$body,&$body,&$body); # 40 instructions
  1081. my ($a,$b,$c,$d,$e);
  1082. eval(shift(@insns));
  1083. eval(shift(@insns));
  1084. &vpalignr(@X[0],@X[-3&7],@X[-4&7],8); # compose "X[-14]" in "X[0]"
  1085. eval(shift(@insns));
  1086. eval(shift(@insns));
  1087. &vpaddd (@Tx[1],$Kx,@X[-1&7]);
  1088. eval(shift(@insns));
  1089. eval(shift(@insns));
  1090. &vpsrldq(@Tx[0],@X[-1&7],4); # "X[-3]", 3 dwords
  1091. eval(shift(@insns));
  1092. eval(shift(@insns));
  1093. &vpxor (@X[0],@X[0],@X[-4&7]); # "X[0]"^="X[-16]"
  1094. eval(shift(@insns));
  1095. eval(shift(@insns));
  1096. &vpxor (@Tx[0],@Tx[0],@X[-2&7]); # "X[-3]"^"X[-8]"
  1097. eval(shift(@insns));
  1098. eval(shift(@insns));
  1099. eval(shift(@insns));
  1100. eval(shift(@insns));
  1101. &vpxor (@X[0],@X[0],@Tx[0]); # "X[0]"^="X[-3]"^"X[-8]"
  1102. eval(shift(@insns));
  1103. eval(shift(@insns));
  1104. &vmovdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
  1105. eval(shift(@insns));
  1106. eval(shift(@insns));
  1107. &vpsrld (@Tx[0],@X[0],31);
  1108. eval(shift(@insns));
  1109. eval(shift(@insns));
  1110. eval(shift(@insns));
  1111. eval(shift(@insns));
  1112. &vpslldq(@Tx[1],@X[0],12); # "X[0]"<<96, extract one dword
  1113. &vpaddd (@X[0],@X[0],@X[0]);
  1114. eval(shift(@insns));
  1115. eval(shift(@insns));
  1116. eval(shift(@insns));
  1117. eval(shift(@insns));
  1118. &vpor (@X[0],@X[0],@Tx[0]); # "X[0]"<<<=1
  1119. &vpsrld (@Tx[0],@Tx[1],30);
  1120. eval(shift(@insns));
  1121. eval(shift(@insns));
  1122. eval(shift(@insns));
  1123. eval(shift(@insns));
  1124. &vpslld (@Tx[1],@Tx[1],2);
  1125. &vpxor (@X[0],@X[0],@Tx[0]);
  1126. eval(shift(@insns));
  1127. eval(shift(@insns));
  1128. eval(shift(@insns));
  1129. eval(shift(@insns));
  1130. &vpxor (@X[0],@X[0],@Tx[1]); # "X[0]"^=("X[0]">>96)<<<2
  1131. eval(shift(@insns));
  1132. eval(shift(@insns));
  1133. &vmovdqa ($Kx,eval(16*(($Xi)/5))."($K_XX_XX)") if ($Xi%5==0); # K_XX_XX
  1134. eval(shift(@insns));
  1135. eval(shift(@insns));
  1136. foreach (@insns) { eval; } # remaining instructions [if any]
  1137. $Xi++; push(@X,shift(@X)); # "rotate" X[]
  1138. }
  1139. sub Xupdate_avx_32_79()
  1140. { use integer;
  1141. my $body = shift;
  1142. my @insns = (&$body,&$body,&$body,&$body); # 32 to 48 instructions
  1143. my ($a,$b,$c,$d,$e);
  1144. &vpalignr(@Tx[0],@X[-1&7],@X[-2&7],8); # compose "X[-6]"
  1145. &vpxor (@X[0],@X[0],@X[-4&7]); # "X[0]"="X[-32]"^"X[-16]"
  1146. eval(shift(@insns)); # body_20_39
  1147. eval(shift(@insns));
  1148. eval(shift(@insns));
  1149. eval(shift(@insns)); # rol
  1150. &vpxor (@X[0],@X[0],@X[-7&7]); # "X[0]"^="X[-28]"
  1151. eval(shift(@insns));
  1152. eval(shift(@insns)) if (@insns[0] !~ /&ro[rl]/);
  1153. &vpaddd (@Tx[1],$Kx,@X[-1&7]);
  1154. &vmovdqa ($Kx,eval(16*($Xi/5))."($K_XX_XX)") if ($Xi%5==0);
  1155. eval(shift(@insns)); # ror
  1156. eval(shift(@insns));
  1157. &vpxor (@X[0],@X[0],@Tx[0]); # "X[0]"^="X[-6]"
  1158. eval(shift(@insns)); # body_20_39
  1159. eval(shift(@insns));
  1160. eval(shift(@insns));
  1161. eval(shift(@insns)); # rol
  1162. &vpsrld (@Tx[0],@X[0],30);
  1163. &vmovdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
  1164. eval(shift(@insns));
  1165. eval(shift(@insns));
  1166. eval(shift(@insns)); # ror
  1167. eval(shift(@insns));
  1168. &vpslld (@X[0],@X[0],2);
  1169. eval(shift(@insns)); # body_20_39
  1170. eval(shift(@insns));
  1171. eval(shift(@insns));
  1172. eval(shift(@insns)); # rol
  1173. eval(shift(@insns));
  1174. eval(shift(@insns));
  1175. eval(shift(@insns)); # ror
  1176. eval(shift(@insns));
  1177. &vpor (@X[0],@X[0],@Tx[0]); # "X[0]"<<<=2
  1178. eval(shift(@insns)); # body_20_39
  1179. eval(shift(@insns));
  1180. eval(shift(@insns));
  1181. eval(shift(@insns)); # rol
  1182. eval(shift(@insns));
  1183. eval(shift(@insns));
  1184. eval(shift(@insns)); # rol
  1185. eval(shift(@insns));
  1186. foreach (@insns) { eval; } # remaining instructions
  1187. $Xi++; push(@X,shift(@X)); # "rotate" X[]
  1188. }
  1189. sub Xuplast_avx_80()
  1190. { use integer;
  1191. my $body = shift;
  1192. my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
  1193. my ($a,$b,$c,$d,$e);
  1194. eval(shift(@insns));
  1195. &vpaddd (@Tx[1],$Kx,@X[-1&7]);
  1196. eval(shift(@insns));
  1197. eval(shift(@insns));
  1198. eval(shift(@insns));
  1199. eval(shift(@insns));
  1200. &vmovdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer IALU
  1201. foreach (@insns) { eval; } # remaining instructions
  1202. &cmp ($inp,$len);
  1203. &je (shift);
  1204. &vmovdqa(@Tx[1],"64($K_XX_XX)"); # pbswap mask
  1205. &vmovdqa($Kx,"0($K_XX_XX)"); # K_00_19
  1206. &vmovdqu(@X[-4&7],"0($inp)"); # load input
  1207. &vmovdqu(@X[-3&7],"16($inp)");
  1208. &vmovdqu(@X[-2&7],"32($inp)");
  1209. &vmovdqu(@X[-1&7],"48($inp)");
  1210. &vpshufb(@X[-4&7],@X[-4&7],@Tx[1]); # byte swap
  1211. &add ($inp,64);
  1212. $Xi=0;
  1213. }
  1214. sub Xloop_avx()
  1215. { use integer;
  1216. my $body = shift;
  1217. my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
  1218. my ($a,$b,$c,$d,$e);
  1219. eval(shift(@insns));
  1220. eval(shift(@insns));
  1221. &vpshufb(@X[($Xi-3)&7],@X[($Xi-3)&7],@Tx[1]);
  1222. eval(shift(@insns));
  1223. eval(shift(@insns));
  1224. &vpaddd (@Tx[0],@X[($Xi-4)&7],$Kx);
  1225. eval(shift(@insns));
  1226. eval(shift(@insns));
  1227. eval(shift(@insns));
  1228. eval(shift(@insns));
  1229. &vmovdqa(eval(16*$Xi)."(%rsp)",@Tx[0]); # X[]+K xfer to IALU
  1230. eval(shift(@insns));
  1231. eval(shift(@insns));
  1232. foreach (@insns) { eval; }
  1233. $Xi++;
  1234. }
  1235. sub Xtail_avx()
  1236. { use integer;
  1237. my $body = shift;
  1238. my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
  1239. my ($a,$b,$c,$d,$e);
  1240. foreach (@insns) { eval; }
  1241. }
  1242. $code.=<<___;
  1243. .align 32
  1244. .Loop_avx:
  1245. ___
  1246. &Xupdate_avx_16_31(\&body_00_19);
  1247. &Xupdate_avx_16_31(\&body_00_19);
  1248. &Xupdate_avx_16_31(\&body_00_19);
  1249. &Xupdate_avx_16_31(\&body_00_19);
  1250. &Xupdate_avx_32_79(\&body_00_19);
  1251. &Xupdate_avx_32_79(\&body_20_39);
  1252. &Xupdate_avx_32_79(\&body_20_39);
  1253. &Xupdate_avx_32_79(\&body_20_39);
  1254. &Xupdate_avx_32_79(\&body_20_39);
  1255. &Xupdate_avx_32_79(\&body_20_39);
  1256. &Xupdate_avx_32_79(\&body_40_59);
  1257. &Xupdate_avx_32_79(\&body_40_59);
  1258. &Xupdate_avx_32_79(\&body_40_59);
  1259. &Xupdate_avx_32_79(\&body_40_59);
  1260. &Xupdate_avx_32_79(\&body_40_59);
  1261. &Xupdate_avx_32_79(\&body_20_39);
  1262. &Xuplast_avx_80(\&body_20_39,".Ldone_avx"); # can jump to "done"
  1263. $saved_j=$j; @saved_V=@V;
  1264. $saved_r=$r; @saved_rndkey=@rndkey;
  1265. &Xloop_avx(\&body_20_39);
  1266. &Xloop_avx(\&body_20_39);
  1267. &Xloop_avx(\&body_20_39);
  1268. $code.=<<___;
  1269. vmovups $iv,48($out,$in0) # write output
  1270. lea 64($in0),$in0
  1271. add 0($ctx),$A # update context
  1272. add 4($ctx),@T[0]
  1273. add 8($ctx),$C
  1274. add 12($ctx),$D
  1275. mov $A,0($ctx)
  1276. add 16($ctx),$E
  1277. mov @T[0],4($ctx)
  1278. mov @T[0],$B # magic seed
  1279. mov $C,8($ctx)
  1280. mov $C,@T[1]
  1281. mov $D,12($ctx)
  1282. xor $D,@T[1]
  1283. mov $E,16($ctx)
  1284. and @T[1],@T[0]
  1285. jmp .Loop_avx
  1286. .Ldone_avx:
  1287. ___
  1288. $jj=$j=$saved_j; @V=@saved_V;
  1289. $r=$saved_r; @rndkey=@saved_rndkey;
  1290. &Xtail_avx(\&body_20_39);
  1291. &Xtail_avx(\&body_20_39);
  1292. &Xtail_avx(\&body_20_39);
  1293. $code.=<<___;
  1294. vmovups $iv,48($out,$in0) # write output
  1295. mov 88(%rsp),$ivp # restore $ivp
  1296. add 0($ctx),$A # update context
  1297. add 4($ctx),@T[0]
  1298. add 8($ctx),$C
  1299. mov $A,0($ctx)
  1300. add 12($ctx),$D
  1301. mov @T[0],4($ctx)
  1302. add 16($ctx),$E
  1303. mov $C,8($ctx)
  1304. mov $D,12($ctx)
  1305. mov $E,16($ctx)
  1306. vmovups $iv,($ivp) # write IV
  1307. vzeroall
  1308. ___
  1309. $code.=<<___ if ($win64);
  1310. movaps 96+0(%rsp),%xmm6
  1311. movaps 96+16(%rsp),%xmm7
  1312. movaps 96+32(%rsp),%xmm8
  1313. movaps 96+48(%rsp),%xmm9
  1314. movaps 96+64(%rsp),%xmm10
  1315. movaps 96+80(%rsp),%xmm11
  1316. movaps 96+96(%rsp),%xmm12
  1317. movaps 96+112(%rsp),%xmm13
  1318. movaps 96+128(%rsp),%xmm14
  1319. movaps 96+144(%rsp),%xmm15
  1320. ___
  1321. $code.=<<___;
  1322. lea `104+($win64?10*16:0)`(%rsp),%rsi
  1323. .cfi_def_cfa %rsi,56
  1324. mov 0(%rsi),%r15
  1325. .cfi_restore %r15
  1326. mov 8(%rsi),%r14
  1327. .cfi_restore %r14
  1328. mov 16(%rsi),%r13
  1329. .cfi_restore %r13
  1330. mov 24(%rsi),%r12
  1331. .cfi_restore %r12
  1332. mov 32(%rsi),%rbp
  1333. .cfi_restore %rbp
  1334. mov 40(%rsi),%rbx
  1335. .cfi_restore %rbx
  1336. lea 48(%rsi),%rsp
  1337. .cfi_def_cfa %rsp,8
  1338. .Lepilogue_avx:
  1339. ret
  1340. .cfi_endproc
  1341. .size aesni_cbc_sha1_enc_avx,.-aesni_cbc_sha1_enc_avx
  1342. ___
  1343. if ($stitched_decrypt) {{{
  1344. # reset
  1345. ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
  1346. $j=$jj=$r=$rx=0;
  1347. $Xi=4;
  1348. @aes256_dec = (
  1349. '&vpxor ($inout0,$rndkey0,"0x00($in0)");',
  1350. '&vpxor ($inout1,$rndkey0,"0x10($in0)");',
  1351. '&vpxor ($inout2,$rndkey0,"0x20($in0)");',
  1352. '&vpxor ($inout3,$rndkey0,"0x30($in0)");',
  1353. '&vmovups($rndkey0,"16-112($key)");',
  1354. '&vmovups("64(%rsp)",@X[2]);', # save IV, originally @X[3]
  1355. undef,undef
  1356. );
  1357. for ($i=0;$i<13;$i++) {
  1358. push (@aes256_dec,(
  1359. '&vaesdec ($inout0,$inout0,$rndkey0);',
  1360. '&vaesdec ($inout1,$inout1,$rndkey0);',
  1361. '&vaesdec ($inout2,$inout2,$rndkey0);',
  1362. '&vaesdec ($inout3,$inout3,$rndkey0); &vmovups($rndkey0,"'.(16*($i+2)-112).'($key)");'
  1363. ));
  1364. push (@aes256_dec,(undef,undef)) if (($i>=3 && $i<=5) || $i>=11);
  1365. push (@aes256_dec,(undef,undef)) if ($i==5);
  1366. }
  1367. push(@aes256_dec,(
  1368. '&vaesdeclast ($inout0,$inout0,$rndkey0); &vmovups(@X[0],"0x00($in0)");',
  1369. '&vaesdeclast ($inout1,$inout1,$rndkey0); &vmovups(@X[1],"0x10($in0)");',
  1370. '&vaesdeclast ($inout2,$inout2,$rndkey0); &vmovups(@X[2],"0x20($in0)");',
  1371. '&vaesdeclast ($inout3,$inout3,$rndkey0); &vmovups(@X[3],"0x30($in0)");',
  1372. '&vxorps ($inout0,$inout0,"64(%rsp)"); &vmovdqu($rndkey0,"-112($key)");',
  1373. '&vxorps ($inout1,$inout1,@X[0]); &vmovups("0x00($out,$in0)",$inout0);',
  1374. '&vxorps ($inout2,$inout2,@X[1]); &vmovups("0x10($out,$in0)",$inout1);',
  1375. '&vxorps ($inout3,$inout3,@X[2]); &vmovups("0x20($out,$in0)",$inout2);',
  1376. '&vmovups ("0x30($out,$in0)",$inout3);'
  1377. ));
  1378. $code.=<<___;
  1379. .type aesni256_cbc_sha1_dec_avx,\@function,6
  1380. .align 32
  1381. aesni256_cbc_sha1_dec_avx:
  1382. .cfi_startproc
  1383. mov `($win64?56:8)`(%rsp),$inp # load 7th argument
  1384. push %rbx
  1385. .cfi_push %rbx
  1386. push %rbp
  1387. .cfi_push %rbp
  1388. push %r12
  1389. .cfi_push %r12
  1390. push %r13
  1391. .cfi_push %r13
  1392. push %r14
  1393. .cfi_push %r14
  1394. push %r15
  1395. .cfi_push %r15
  1396. lea `-104-($win64?10*16:0)`(%rsp),%rsp
  1397. .cfi_adjust_cfa_offset `104+($win64?10*16:0)`
  1398. ___
  1399. $code.=<<___ if ($win64);
  1400. movaps %xmm6,96+0(%rsp)
  1401. movaps %xmm7,96+16(%rsp)
  1402. movaps %xmm8,96+32(%rsp)
  1403. movaps %xmm9,96+48(%rsp)
  1404. movaps %xmm10,96+64(%rsp)
  1405. movaps %xmm11,96+80(%rsp)
  1406. movaps %xmm12,96+96(%rsp)
  1407. movaps %xmm13,96+112(%rsp)
  1408. movaps %xmm14,96+128(%rsp)
  1409. movaps %xmm15,96+144(%rsp)
  1410. .Lprologue_dec_avx:
  1411. ___
  1412. $code.=<<___;
  1413. vzeroall
  1414. mov $in0,%r12 # reassign arguments
  1415. mov $out,%r13
  1416. mov $len,%r14
  1417. lea 112($key),%r15 # size optimization
  1418. vmovdqu ($ivp),@X[3] # load IV
  1419. ___
  1420. ($in0,$out,$len,$key)=map("%r$_",(12..15)); # reassign arguments
  1421. $code.=<<___;
  1422. shl \$6,$len
  1423. sub $in0,$out
  1424. add $inp,$len # end of input
  1425. lea K_XX_XX(%rip),$K_XX_XX
  1426. mov 0($ctx),$A # load context
  1427. mov 4($ctx),$B
  1428. mov 8($ctx),$C
  1429. mov 12($ctx),$D
  1430. mov $B,@T[0] # magic seed
  1431. mov 16($ctx),$E
  1432. mov $C,@T[1]
  1433. xor $D,@T[1]
  1434. and @T[1],@T[0]
  1435. vmovdqa 64($K_XX_XX),@X[2] # pbswap mask
  1436. vmovdqa 0($K_XX_XX),$Kx # K_00_19
  1437. vmovdqu 0($inp),@X[-4&7] # load input to %xmm[0-3]
  1438. vmovdqu 16($inp),@X[-3&7]
  1439. vmovdqu 32($inp),@X[-2&7]
  1440. vmovdqu 48($inp),@X[-1&7]
  1441. vpshufb @X[2],@X[-4&7],@X[-4&7] # byte swap
  1442. add \$64,$inp
  1443. vpshufb @X[2],@X[-3&7],@X[-3&7]
  1444. vpshufb @X[2],@X[-2&7],@X[-2&7]
  1445. vpshufb @X[2],@X[-1&7],@X[-1&7]
  1446. vpaddd $Kx,@X[-4&7],@X[0] # add K_00_19
  1447. vpaddd $Kx,@X[-3&7],@X[1]
  1448. vpaddd $Kx,@X[-2&7],@X[2]
  1449. vmovdqa @X[0],0(%rsp) # X[]+K xfer to IALU
  1450. vmovdqa @X[1],16(%rsp)
  1451. vmovdqa @X[2],32(%rsp)
  1452. vmovups -112($key),$rndkey0 # $key[0]
  1453. jmp .Loop_dec_avx
  1454. .align 32
  1455. .Loop_dec_avx:
  1456. ___
  1457. &Xupdate_avx_16_31(\&body_00_19_dec);
  1458. &Xupdate_avx_16_31(\&body_00_19_dec);
  1459. &Xupdate_avx_16_31(\&body_00_19_dec);
  1460. &Xupdate_avx_16_31(\&body_00_19_dec);
  1461. &Xupdate_avx_32_79(\&body_00_19_dec);
  1462. &Xupdate_avx_32_79(\&body_20_39_dec);
  1463. &Xupdate_avx_32_79(\&body_20_39_dec);
  1464. &Xupdate_avx_32_79(\&body_20_39_dec);
  1465. &Xupdate_avx_32_79(\&body_20_39_dec);
  1466. &Xupdate_avx_32_79(\&body_20_39_dec);
  1467. &Xupdate_avx_32_79(\&body_40_59_dec);
  1468. &Xupdate_avx_32_79(\&body_40_59_dec);
  1469. &Xupdate_avx_32_79(\&body_40_59_dec);
  1470. &Xupdate_avx_32_79(\&body_40_59_dec);
  1471. &Xupdate_avx_32_79(\&body_40_59_dec);
  1472. &Xupdate_avx_32_79(\&body_20_39_dec);
  1473. &Xuplast_avx_80(\&body_20_39_dec,".Ldone_dec_avx"); # can jump to "done"
  1474. $saved_j=$j; @saved_V=@V;
  1475. $saved_rx=$rx;
  1476. &Xloop_avx(\&body_20_39_dec);
  1477. &Xloop_avx(\&body_20_39_dec);
  1478. &Xloop_avx(\&body_20_39_dec);
  1479. eval(@aes256_dec[-1]); # last store
  1480. $code.=<<___;
  1481. lea 64($in0),$in0
  1482. add 0($ctx),$A # update context
  1483. add 4($ctx),@T[0]
  1484. add 8($ctx),$C
  1485. add 12($ctx),$D
  1486. mov $A,0($ctx)
  1487. add 16($ctx),$E
  1488. mov @T[0],4($ctx)
  1489. mov @T[0],$B # magic seed
  1490. mov $C,8($ctx)
  1491. mov $C,@T[1]
  1492. mov $D,12($ctx)
  1493. xor $D,@T[1]
  1494. mov $E,16($ctx)
  1495. and @T[1],@T[0]
  1496. jmp .Loop_dec_avx
  1497. .Ldone_dec_avx:
  1498. ___
  1499. $jj=$j=$saved_j; @V=@saved_V;
  1500. $rx=$saved_rx;
  1501. &Xtail_avx(\&body_20_39_dec);
  1502. &Xtail_avx(\&body_20_39_dec);
  1503. &Xtail_avx(\&body_20_39_dec);
  1504. eval(@aes256_dec[-1]); # last store
  1505. $code.=<<___;
  1506. add 0($ctx),$A # update context
  1507. add 4($ctx),@T[0]
  1508. add 8($ctx),$C
  1509. mov $A,0($ctx)
  1510. add 12($ctx),$D
  1511. mov @T[0],4($ctx)
  1512. add 16($ctx),$E
  1513. mov $C,8($ctx)
  1514. mov $D,12($ctx)
  1515. mov $E,16($ctx)
  1516. vmovups @X[3],($ivp) # write IV
  1517. vzeroall
  1518. ___
  1519. $code.=<<___ if ($win64);
  1520. movaps 96+0(%rsp),%xmm6
  1521. movaps 96+16(%rsp),%xmm7
  1522. movaps 96+32(%rsp),%xmm8
  1523. movaps 96+48(%rsp),%xmm9
  1524. movaps 96+64(%rsp),%xmm10
  1525. movaps 96+80(%rsp),%xmm11
  1526. movaps 96+96(%rsp),%xmm12
  1527. movaps 96+112(%rsp),%xmm13
  1528. movaps 96+128(%rsp),%xmm14
  1529. movaps 96+144(%rsp),%xmm15
  1530. ___
  1531. $code.=<<___;
  1532. lea `104+($win64?10*16:0)`(%rsp),%rsi
  1533. .cfi_def_cfa %rsi,56
  1534. mov 0(%rsi),%r15
  1535. .cfi_restore %r15
  1536. mov 8(%rsi),%r14
  1537. .cfi_restore %r14
  1538. mov 16(%rsi),%r13
  1539. .cfi_restore %r13
  1540. mov 24(%rsi),%r12
  1541. .cfi_restore %r12
  1542. mov 32(%rsi),%rbp
  1543. .cfi_restore %rbp
  1544. mov 40(%rsi),%rbx
  1545. .cfi_restore %rbx
  1546. lea 48(%rsi),%rsp
  1547. .cfi_def_cfa %rsp,8
  1548. .Lepilogue_dec_avx:
  1549. ret
  1550. .cfi_endproc
  1551. .size aesni256_cbc_sha1_dec_avx,.-aesni256_cbc_sha1_dec_avx
  1552. ___
  1553. }}}
  1554. }
  1555. $code.=<<___;
  1556. .section .rodata align=64
  1557. .align 64
  1558. K_XX_XX:
  1559. .long 0x5a827999,0x5a827999,0x5a827999,0x5a827999 # K_00_19
  1560. .long 0x6ed9eba1,0x6ed9eba1,0x6ed9eba1,0x6ed9eba1 # K_20_39
  1561. .long 0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc # K_40_59
  1562. .long 0xca62c1d6,0xca62c1d6,0xca62c1d6,0xca62c1d6 # K_60_79
  1563. .long 0x00010203,0x04050607,0x08090a0b,0x0c0d0e0f # pbswap mask
  1564. .byte 0xf,0xe,0xd,0xc,0xb,0xa,0x9,0x8,0x7,0x6,0x5,0x4,0x3,0x2,0x1,0x0
  1565. .asciz "AESNI-CBC+SHA1 stitch for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
  1566. .align 64
  1567. .previous
  1568. ___
  1569. if ($shaext) {{{
  1570. ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
  1571. $rounds="%r11d";
  1572. ($iv,$in,$rndkey0)=map("%xmm$_",(2,14,15));
  1573. @rndkey=("%xmm0","%xmm1");
  1574. $r=0;
  1575. my ($BSWAP,$ABCD,$E,$E_,$ABCD_SAVE,$E_SAVE)=map("%xmm$_",(7..12));
  1576. my @MSG=map("%xmm$_",(3..6));
  1577. $code.=<<___;
  1578. .type aesni_cbc_sha1_enc_shaext,\@function,6
  1579. .align 32
  1580. aesni_cbc_sha1_enc_shaext:
  1581. .cfi_startproc
  1582. mov `($win64?56:8)`(%rsp),$inp # load 7th argument
  1583. ___
  1584. $code.=<<___ if ($win64);
  1585. lea `-8-10*16`(%rsp),%rsp
  1586. movaps %xmm6,-8-10*16(%rax)
  1587. movaps %xmm7,-8-9*16(%rax)
  1588. movaps %xmm8,-8-8*16(%rax)
  1589. movaps %xmm9,-8-7*16(%rax)
  1590. movaps %xmm10,-8-6*16(%rax)
  1591. movaps %xmm11,-8-5*16(%rax)
  1592. movaps %xmm12,-8-4*16(%rax)
  1593. movaps %xmm13,-8-3*16(%rax)
  1594. movaps %xmm14,-8-2*16(%rax)
  1595. movaps %xmm15,-8-1*16(%rax)
  1596. .Lprologue_shaext:
  1597. ___
  1598. $code.=<<___;
  1599. movdqu ($ctx),$ABCD
  1600. movd 16($ctx),$E
  1601. movdqa K_XX_XX+0x50(%rip),$BSWAP # byte-n-word swap
  1602. mov 240($key),$rounds
  1603. sub $in0,$out
  1604. movups ($key),$rndkey0 # $key[0]
  1605. movups ($ivp),$iv # load IV
  1606. movups 16($key),$rndkey[0] # forward reference
  1607. lea 112($key),$key # size optimization
  1608. pshufd \$0b00011011,$ABCD,$ABCD # flip word order
  1609. pshufd \$0b00011011,$E,$E # flip word order
  1610. jmp .Loop_shaext
  1611. .align 16
  1612. .Loop_shaext:
  1613. ___
  1614. &$aesenc();
  1615. $code.=<<___;
  1616. movdqu ($inp),@MSG[0]
  1617. movdqa $E,$E_SAVE # offload $E
  1618. pshufb $BSWAP,@MSG[0]
  1619. movdqu 0x10($inp),@MSG[1]
  1620. movdqa $ABCD,$ABCD_SAVE # offload $ABCD
  1621. ___
  1622. &$aesenc();
  1623. $code.=<<___;
  1624. pshufb $BSWAP,@MSG[1]
  1625. paddd @MSG[0],$E
  1626. movdqu 0x20($inp),@MSG[2]
  1627. lea 0x40($inp),$inp
  1628. pxor $E_SAVE,@MSG[0] # black magic
  1629. ___
  1630. &$aesenc();
  1631. $code.=<<___;
  1632. pxor $E_SAVE,@MSG[0] # black magic
  1633. movdqa $ABCD,$E_
  1634. pshufb $BSWAP,@MSG[2]
  1635. sha1rnds4 \$0,$E,$ABCD # 0-3
  1636. sha1nexte @MSG[1],$E_
  1637. ___
  1638. &$aesenc();
  1639. $code.=<<___;
  1640. sha1msg1 @MSG[1],@MSG[0]
  1641. movdqu -0x10($inp),@MSG[3]
  1642. movdqa $ABCD,$E
  1643. pshufb $BSWAP,@MSG[3]
  1644. ___
  1645. &$aesenc();
  1646. $code.=<<___;
  1647. sha1rnds4 \$0,$E_,$ABCD # 4-7
  1648. sha1nexte @MSG[2],$E
  1649. pxor @MSG[2],@MSG[0]
  1650. sha1msg1 @MSG[2],@MSG[1]
  1651. ___
  1652. &$aesenc();
  1653. for($i=2;$i<20-4;$i++) {
  1654. $code.=<<___;
  1655. movdqa $ABCD,$E_
  1656. sha1rnds4 \$`int($i/5)`,$E,$ABCD # 8-11
  1657. sha1nexte @MSG[3],$E_
  1658. ___
  1659. &$aesenc();
  1660. $code.=<<___;
  1661. sha1msg2 @MSG[3],@MSG[0]
  1662. pxor @MSG[3],@MSG[1]
  1663. sha1msg1 @MSG[3],@MSG[2]
  1664. ___
  1665. ($E,$E_)=($E_,$E);
  1666. push(@MSG,shift(@MSG));
  1667. &$aesenc();
  1668. }
  1669. $code.=<<___;
  1670. movdqa $ABCD,$E_
  1671. sha1rnds4 \$3,$E,$ABCD # 64-67
  1672. sha1nexte @MSG[3],$E_
  1673. sha1msg2 @MSG[3],@MSG[0]
  1674. pxor @MSG[3],@MSG[1]
  1675. ___
  1676. &$aesenc();
  1677. $code.=<<___;
  1678. movdqa $ABCD,$E
  1679. sha1rnds4 \$3,$E_,$ABCD # 68-71
  1680. sha1nexte @MSG[0],$E
  1681. sha1msg2 @MSG[0],@MSG[1]
  1682. ___
  1683. &$aesenc();
  1684. $code.=<<___;
  1685. movdqa $E_SAVE,@MSG[0]
  1686. movdqa $ABCD,$E_
  1687. sha1rnds4 \$3,$E,$ABCD # 72-75
  1688. sha1nexte @MSG[1],$E_
  1689. ___
  1690. &$aesenc();
  1691. $code.=<<___;
  1692. movdqa $ABCD,$E
  1693. sha1rnds4 \$3,$E_,$ABCD # 76-79
  1694. sha1nexte $MSG[0],$E
  1695. ___
  1696. while($r<40) { &$aesenc(); } # remaining aesenc's
  1697. $code.=<<___;
  1698. dec $len
  1699. paddd $ABCD_SAVE,$ABCD
  1700. movups $iv,48($out,$in0) # write output
  1701. lea 64($in0),$in0
  1702. jnz .Loop_shaext
  1703. pshufd \$0b00011011,$ABCD,$ABCD
  1704. pshufd \$0b00011011,$E,$E
  1705. movups $iv,($ivp) # write IV
  1706. movdqu $ABCD,($ctx)
  1707. movd $E,16($ctx)
  1708. ___
  1709. $code.=<<___ if ($win64);
  1710. movaps -8-10*16(%rax),%xmm6
  1711. movaps -8-9*16(%rax),%xmm7
  1712. movaps -8-8*16(%rax),%xmm8
  1713. movaps -8-7*16(%rax),%xmm9
  1714. movaps -8-6*16(%rax),%xmm10
  1715. movaps -8-5*16(%rax),%xmm11
  1716. movaps -8-4*16(%rax),%xmm12
  1717. movaps -8-3*16(%rax),%xmm13
  1718. movaps -8-2*16(%rax),%xmm14
  1719. movaps -8-1*16(%rax),%xmm15
  1720. mov %rax,%rsp
  1721. .Lepilogue_shaext:
  1722. ___
  1723. $code.=<<___;
  1724. ret
  1725. .cfi_endproc
  1726. .size aesni_cbc_sha1_enc_shaext,.-aesni_cbc_sha1_enc_shaext
  1727. ___
  1728. }}}
  1729. # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
  1730. # CONTEXT *context,DISPATCHER_CONTEXT *disp)
  1731. if ($win64) {
  1732. $rec="%rcx";
  1733. $frame="%rdx";
  1734. $context="%r8";
  1735. $disp="%r9";
  1736. $code.=<<___;
  1737. .extern __imp_RtlVirtualUnwind
  1738. .type ssse3_handler,\@abi-omnipotent
  1739. .align 16
  1740. ssse3_handler:
  1741. push %rsi
  1742. push %rdi
  1743. push %rbx
  1744. push %rbp
  1745. push %r12
  1746. push %r13
  1747. push %r14
  1748. push %r15
  1749. pushfq
  1750. sub \$64,%rsp
  1751. mov 120($context),%rax # pull context->Rax
  1752. mov 248($context),%rbx # pull context->Rip
  1753. mov 8($disp),%rsi # disp->ImageBase
  1754. mov 56($disp),%r11 # disp->HandlerData
  1755. mov 0(%r11),%r10d # HandlerData[0]
  1756. lea (%rsi,%r10),%r10 # prologue label
  1757. cmp %r10,%rbx # context->Rip<prologue label
  1758. jb .Lcommon_seh_tail
  1759. mov 152($context),%rax # pull context->Rsp
  1760. mov 4(%r11),%r10d # HandlerData[1]
  1761. lea (%rsi,%r10),%r10 # epilogue label
  1762. cmp %r10,%rbx # context->Rip>=epilogue label
  1763. jae .Lcommon_seh_tail
  1764. ___
  1765. $code.=<<___ if ($shaext);
  1766. lea aesni_cbc_sha1_enc_shaext(%rip),%r10
  1767. cmp %r10,%rbx
  1768. jb .Lseh_no_shaext
  1769. lea (%rax),%rsi
  1770. lea 512($context),%rdi # &context.Xmm6
  1771. mov \$20,%ecx
  1772. .long 0xa548f3fc # cld; rep movsq
  1773. lea 168(%rax),%rax # adjust stack pointer
  1774. jmp .Lcommon_seh_tail
  1775. .Lseh_no_shaext:
  1776. ___
  1777. $code.=<<___;
  1778. lea 96(%rax),%rsi
  1779. lea 512($context),%rdi # &context.Xmm6
  1780. mov \$20,%ecx
  1781. .long 0xa548f3fc # cld; rep movsq
  1782. lea `104+10*16`(%rax),%rax # adjust stack pointer
  1783. mov 0(%rax),%r15
  1784. mov 8(%rax),%r14
  1785. mov 16(%rax),%r13
  1786. mov 24(%rax),%r12
  1787. mov 32(%rax),%rbp
  1788. mov 40(%rax),%rbx
  1789. lea 48(%rax),%rax
  1790. mov %rbx,144($context) # restore context->Rbx
  1791. mov %rbp,160($context) # restore context->Rbp
  1792. mov %r12,216($context) # restore context->R12
  1793. mov %r13,224($context) # restore context->R13
  1794. mov %r14,232($context) # restore context->R14
  1795. mov %r15,240($context) # restore context->R15
  1796. .Lcommon_seh_tail:
  1797. mov 8(%rax),%rdi
  1798. mov 16(%rax),%rsi
  1799. mov %rax,152($context) # restore context->Rsp
  1800. mov %rsi,168($context) # restore context->Rsi
  1801. mov %rdi,176($context) # restore context->Rdi
  1802. mov 40($disp),%rdi # disp->ContextRecord
  1803. mov $context,%rsi # context
  1804. mov \$154,%ecx # sizeof(CONTEXT)
  1805. .long 0xa548f3fc # cld; rep movsq
  1806. mov $disp,%rsi
  1807. xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
  1808. mov 8(%rsi),%rdx # arg2, disp->ImageBase
  1809. mov 0(%rsi),%r8 # arg3, disp->ControlPc
  1810. mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
  1811. mov 40(%rsi),%r10 # disp->ContextRecord
  1812. lea 56(%rsi),%r11 # &disp->HandlerData
  1813. lea 24(%rsi),%r12 # &disp->EstablisherFrame
  1814. mov %r10,32(%rsp) # arg5
  1815. mov %r11,40(%rsp) # arg6
  1816. mov %r12,48(%rsp) # arg7
  1817. mov %rcx,56(%rsp) # arg8, (NULL)
  1818. call *__imp_RtlVirtualUnwind(%rip)
  1819. mov \$1,%eax # ExceptionContinueSearch
  1820. add \$64,%rsp
  1821. popfq
  1822. pop %r15
  1823. pop %r14
  1824. pop %r13
  1825. pop %r12
  1826. pop %rbp
  1827. pop %rbx
  1828. pop %rdi
  1829. pop %rsi
  1830. ret
  1831. .size ssse3_handler,.-ssse3_handler
  1832. .section .pdata
  1833. .align 4
  1834. .rva .LSEH_begin_aesni_cbc_sha1_enc_ssse3
  1835. .rva .LSEH_end_aesni_cbc_sha1_enc_ssse3
  1836. .rva .LSEH_info_aesni_cbc_sha1_enc_ssse3
  1837. ___
  1838. $code.=<<___ if ($avx);
  1839. .rva .LSEH_begin_aesni_cbc_sha1_enc_avx
  1840. .rva .LSEH_end_aesni_cbc_sha1_enc_avx
  1841. .rva .LSEH_info_aesni_cbc_sha1_enc_avx
  1842. ___
  1843. $code.=<<___ if ($shaext);
  1844. .rva .LSEH_begin_aesni_cbc_sha1_enc_shaext
  1845. .rva .LSEH_end_aesni_cbc_sha1_enc_shaext
  1846. .rva .LSEH_info_aesni_cbc_sha1_enc_shaext
  1847. ___
  1848. $code.=<<___;
  1849. .section .xdata
  1850. .align 8
  1851. .LSEH_info_aesni_cbc_sha1_enc_ssse3:
  1852. .byte 9,0,0,0
  1853. .rva ssse3_handler
  1854. .rva .Lprologue_ssse3,.Lepilogue_ssse3 # HandlerData[]
  1855. ___
  1856. $code.=<<___ if ($avx);
  1857. .LSEH_info_aesni_cbc_sha1_enc_avx:
  1858. .byte 9,0,0,0
  1859. .rva ssse3_handler
  1860. .rva .Lprologue_avx,.Lepilogue_avx # HandlerData[]
  1861. ___
  1862. $code.=<<___ if ($shaext);
  1863. .LSEH_info_aesni_cbc_sha1_enc_shaext:
  1864. .byte 9,0,0,0
  1865. .rva ssse3_handler
  1866. .rva .Lprologue_shaext,.Lepilogue_shaext # HandlerData[]
  1867. ___
  1868. }
  1869. ####################################################################
  1870. sub rex {
  1871. local *opcode=shift;
  1872. my ($dst,$src)=@_;
  1873. my $rex=0;
  1874. $rex|=0x04 if($dst>=8);
  1875. $rex|=0x01 if($src>=8);
  1876. unshift @opcode,$rex|0x40 if($rex);
  1877. }
  1878. sub sha1rnds4 {
  1879. if (@_[0] =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
  1880. my @opcode=(0x0f,0x3a,0xcc);
  1881. rex(\@opcode,$3,$2);
  1882. push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
  1883. my $c=$1;
  1884. push @opcode,$c=~/^0/?oct($c):$c;
  1885. return ".byte\t".join(',',@opcode);
  1886. } else {
  1887. return "sha1rnds4\t".@_[0];
  1888. }
  1889. }
  1890. sub sha1op38 {
  1891. my $instr = shift;
  1892. my %opcodelet = (
  1893. "sha1nexte" => 0xc8,
  1894. "sha1msg1" => 0xc9,
  1895. "sha1msg2" => 0xca );
  1896. if (defined($opcodelet{$instr}) && @_[0] =~ /%xmm([0-9]+),\s*%xmm([0-9]+)/) {
  1897. my @opcode=(0x0f,0x38);
  1898. rex(\@opcode,$2,$1);
  1899. push @opcode,$opcodelet{$instr};
  1900. push @opcode,0xc0|($1&7)|(($2&7)<<3); # ModR/M
  1901. return ".byte\t".join(',',@opcode);
  1902. } else {
  1903. return $instr."\t".@_[0];
  1904. }
  1905. }
  1906. sub aesni {
  1907. my $line=shift;
  1908. my @opcode=(0x0f,0x38);
  1909. if ($line=~/(aes[a-z]+)\s+%xmm([0-9]+),\s*%xmm([0-9]+)/) {
  1910. my %opcodelet = (
  1911. "aesenc" => 0xdc, "aesenclast" => 0xdd,
  1912. "aesdec" => 0xde, "aesdeclast" => 0xdf
  1913. );
  1914. return undef if (!defined($opcodelet{$1}));
  1915. rex(\@opcode,$3,$2);
  1916. push @opcode,$opcodelet{$1},0xc0|($2&7)|(($3&7)<<3); # ModR/M
  1917. unshift @opcode,0x66;
  1918. return ".byte\t".join(',',@opcode);
  1919. }
  1920. return $line;
  1921. }
  1922. foreach (split("\n",$code)) {
  1923. s/\`([^\`]*)\`/eval $1/geo;
  1924. s/\b(sha1rnds4)\s+(.*)/sha1rnds4($2)/geo or
  1925. s/\b(sha1[^\s]*)\s+(.*)/sha1op38($1,$2)/geo or
  1926. s/\b(aes.*%xmm[0-9]+).*$/aesni($1)/geo;
  1927. print $_,"\n";
  1928. }
  1929. close STDOUT or die "error closing STDOUT: $!";