bsaes-x86_64.pl 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239
  1. #! /usr/bin/env perl
  2. # Copyright 2011-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. ###################################################################
  9. ### AES-128 [originally in CTR mode] ###
  10. ### bitsliced implementation for Intel Core 2 processors ###
  11. ### requires support of SSE extensions up to SSSE3 ###
  12. ### Author: Emilia Käsper and Peter Schwabe ###
  13. ### Date: 2009-03-19 ###
  14. ### Public domain ###
  15. ### ###
  16. ### See http://homes.esat.kuleuven.be/~ekasper/#software for ###
  17. ### further information. ###
  18. ###################################################################
  19. #
  20. # September 2011.
  21. #
  22. # Started as transliteration to "perlasm" the original code has
  23. # undergone following changes:
  24. #
  25. # - code was made position-independent;
  26. # - rounds were folded into a loop resulting in >5x size reduction
  27. # from 12.5KB to 2.2KB;
  28. # - above was possibile thanks to mixcolumns() modification that
  29. # allowed to feed its output back to aesenc[last], this was
  30. # achieved at cost of two additional inter-registers moves;
  31. # - some instruction reordering and interleaving;
  32. # - this module doesn't implement key setup subroutine, instead it
  33. # relies on conversion of "conventional" key schedule as returned
  34. # by AES_set_encrypt_key (see discussion below);
  35. # - first and last round keys are treated differently, which allowed
  36. # to skip one shiftrows(), reduce bit-sliced key schedule and
  37. # speed-up conversion by 22%;
  38. # - support for 192- and 256-bit keys was added;
  39. #
  40. # Resulting performance in CPU cycles spent to encrypt one byte out
  41. # of 4096-byte buffer with 128-bit key is:
  42. #
  43. # Emilia's this(*) difference
  44. #
  45. # Core 2 9.30 8.69 +7%
  46. # Nehalem(**) 7.63 6.88 +11%
  47. # Atom 17.1 16.4 +4%
  48. # Silvermont - 12.9
  49. # Goldmont - 8.85
  50. #
  51. # (*) Comparison is not completely fair, because "this" is ECB,
  52. # i.e. no extra processing such as counter values calculation
  53. # and xor-ing input as in Emilia's CTR implementation is
  54. # performed. However, the CTR calculations stand for not more
  55. # than 1% of total time, so comparison is *rather* fair.
  56. #
  57. # (**) Results were collected on Westmere, which is considered to
  58. # be equivalent to Nehalem for this code.
  59. #
  60. # As for key schedule conversion subroutine. Interface to OpenSSL
  61. # relies on per-invocation on-the-fly conversion. This naturally
  62. # has impact on performance, especially for short inputs. Conversion
  63. # time in CPU cycles and its ratio to CPU cycles spent in 8x block
  64. # function is:
  65. #
  66. # conversion conversion/8x block
  67. # Core 2 240 0.22
  68. # Nehalem 180 0.20
  69. # Atom 430 0.20
  70. #
  71. # The ratio values mean that 128-byte blocks will be processed
  72. # 16-18% slower, 256-byte blocks - 9-10%, 384-byte blocks - 6-7%,
  73. # etc. Then keep in mind that input sizes not divisible by 128 are
  74. # *effectively* slower, especially shortest ones, e.g. consecutive
  75. # 144-byte blocks are processed 44% slower than one would expect,
  76. # 272 - 29%, 400 - 22%, etc. Yet, despite all these "shortcomings"
  77. # it's still faster than ["hyper-threading-safe" code path in]
  78. # aes-x86_64.pl on all lengths above 64 bytes...
  79. #
  80. # October 2011.
  81. #
  82. # Add decryption procedure. Performance in CPU cycles spent to decrypt
  83. # one byte out of 4096-byte buffer with 128-bit key is:
  84. #
  85. # Core 2 9.98
  86. # Nehalem 7.80
  87. # Atom 17.9
  88. # Silvermont 14.0
  89. # Goldmont 10.2
  90. #
  91. # November 2011.
  92. #
  93. # Add bsaes_xts_[en|de]crypt. Less-than-80-bytes-block performance is
  94. # suboptimal, but XTS is meant to be used with larger blocks...
  95. #
  96. # <appro@openssl.org>
  97. $flavour = shift;
  98. $output = shift;
  99. if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
  100. $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
  101. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  102. ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
  103. ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
  104. die "can't locate x86_64-xlate.pl";
  105. open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\"";
  106. *STDOUT=*OUT;
  107. my ($inp,$out,$len,$key,$ivp)=("%rdi","%rsi","%rdx","%rcx");
  108. my @XMM=map("%xmm$_",(15,0..14)); # best on Atom, +10% over (0..15)
  109. my $ecb=0; # suppress unreferenced ECB subroutines, spare some space...
  110. {
  111. my ($key,$rounds,$const)=("%rax","%r10d","%r11");
  112. sub Sbox {
  113. # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
  114. # output in lsb > [b0, b1, b4, b6, b3, b7, b2, b5] < msb
  115. my @b=@_[0..7];
  116. my @t=@_[8..11];
  117. my @s=@_[12..15];
  118. &InBasisChange (@b);
  119. &Inv_GF256 (@b[6,5,0,3,7,1,4,2],@t,@s);
  120. &OutBasisChange (@b[7,1,4,2,6,5,0,3]);
  121. }
  122. sub InBasisChange {
  123. # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
  124. # output in lsb > [b6, b5, b0, b3, b7, b1, b4, b2] < msb
  125. my @b=@_[0..7];
  126. $code.=<<___;
  127. pxor @b[6], @b[5]
  128. pxor @b[1], @b[2]
  129. pxor @b[0], @b[3]
  130. pxor @b[2], @b[6]
  131. pxor @b[0], @b[5]
  132. pxor @b[3], @b[6]
  133. pxor @b[7], @b[3]
  134. pxor @b[5], @b[7]
  135. pxor @b[4], @b[3]
  136. pxor @b[5], @b[4]
  137. pxor @b[1], @b[3]
  138. pxor @b[7], @b[2]
  139. pxor @b[5], @b[1]
  140. ___
  141. }
  142. sub OutBasisChange {
  143. # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
  144. # output in lsb > [b6, b1, b2, b4, b7, b0, b3, b5] < msb
  145. my @b=@_[0..7];
  146. $code.=<<___;
  147. pxor @b[6], @b[0]
  148. pxor @b[4], @b[1]
  149. pxor @b[0], @b[2]
  150. pxor @b[6], @b[4]
  151. pxor @b[1], @b[6]
  152. pxor @b[5], @b[1]
  153. pxor @b[3], @b[5]
  154. pxor @b[7], @b[3]
  155. pxor @b[5], @b[7]
  156. pxor @b[5], @b[2]
  157. pxor @b[7], @b[4]
  158. ___
  159. }
  160. sub InvSbox {
  161. # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
  162. # output in lsb > [b0, b1, b6, b4, b2, b7, b3, b5] < msb
  163. my @b=@_[0..7];
  164. my @t=@_[8..11];
  165. my @s=@_[12..15];
  166. &InvInBasisChange (@b);
  167. &Inv_GF256 (@b[5,1,2,6,3,7,0,4],@t,@s);
  168. &InvOutBasisChange (@b[3,7,0,4,5,1,2,6]);
  169. }
  170. sub InvInBasisChange { # OutBasisChange in reverse
  171. my @b=@_[5,1,2,6,3,7,0,4];
  172. $code.=<<___
  173. pxor @b[7], @b[4]
  174. pxor @b[5], @b[7]
  175. pxor @b[5], @b[2]
  176. pxor @b[7], @b[3]
  177. pxor @b[3], @b[5]
  178. pxor @b[5], @b[1]
  179. pxor @b[1], @b[6]
  180. pxor @b[0], @b[2]
  181. pxor @b[6], @b[4]
  182. pxor @b[6], @b[0]
  183. pxor @b[4], @b[1]
  184. ___
  185. }
  186. sub InvOutBasisChange { # InBasisChange in reverse
  187. my @b=@_[2,5,7,3,6,1,0,4];
  188. $code.=<<___;
  189. pxor @b[5], @b[1]
  190. pxor @b[7], @b[2]
  191. pxor @b[1], @b[3]
  192. pxor @b[5], @b[4]
  193. pxor @b[5], @b[7]
  194. pxor @b[4], @b[3]
  195. pxor @b[0], @b[5]
  196. pxor @b[7], @b[3]
  197. pxor @b[2], @b[6]
  198. pxor @b[1], @b[2]
  199. pxor @b[3], @b[6]
  200. pxor @b[0], @b[3]
  201. pxor @b[6], @b[5]
  202. ___
  203. }
  204. sub Mul_GF4 {
  205. #;*************************************************************
  206. #;* Mul_GF4: Input x0-x1,y0-y1 Output x0-x1 Temp t0 (8) *
  207. #;*************************************************************
  208. my ($x0,$x1,$y0,$y1,$t0)=@_;
  209. $code.=<<___;
  210. movdqa $y0, $t0
  211. pxor $y1, $t0
  212. pand $x0, $t0
  213. pxor $x1, $x0
  214. pand $y0, $x1
  215. pand $y1, $x0
  216. pxor $x1, $x0
  217. pxor $t0, $x1
  218. ___
  219. }
  220. sub Mul_GF4_N { # not used, see next subroutine
  221. # multiply and scale by N
  222. my ($x0,$x1,$y0,$y1,$t0)=@_;
  223. $code.=<<___;
  224. movdqa $y0, $t0
  225. pxor $y1, $t0
  226. pand $x0, $t0
  227. pxor $x1, $x0
  228. pand $y0, $x1
  229. pand $y1, $x0
  230. pxor $x0, $x1
  231. pxor $t0, $x0
  232. ___
  233. }
  234. sub Mul_GF4_N_GF4 {
  235. # interleaved Mul_GF4_N and Mul_GF4
  236. my ($x0,$x1,$y0,$y1,$t0,
  237. $x2,$x3,$y2,$y3,$t1)=@_;
  238. $code.=<<___;
  239. movdqa $y0, $t0
  240. movdqa $y2, $t1
  241. pxor $y1, $t0
  242. pxor $y3, $t1
  243. pand $x0, $t0
  244. pand $x2, $t1
  245. pxor $x1, $x0
  246. pxor $x3, $x2
  247. pand $y0, $x1
  248. pand $y2, $x3
  249. pand $y1, $x0
  250. pand $y3, $x2
  251. pxor $x0, $x1
  252. pxor $x3, $x2
  253. pxor $t0, $x0
  254. pxor $t1, $x3
  255. ___
  256. }
  257. sub Mul_GF16_2 {
  258. my @x=@_[0..7];
  259. my @y=@_[8..11];
  260. my @t=@_[12..15];
  261. $code.=<<___;
  262. movdqa @x[0], @t[0]
  263. movdqa @x[1], @t[1]
  264. ___
  265. &Mul_GF4 (@x[0], @x[1], @y[0], @y[1], @t[2]);
  266. $code.=<<___;
  267. pxor @x[2], @t[0]
  268. pxor @x[3], @t[1]
  269. pxor @y[2], @y[0]
  270. pxor @y[3], @y[1]
  271. ___
  272. Mul_GF4_N_GF4 (@t[0], @t[1], @y[0], @y[1], @t[3],
  273. @x[2], @x[3], @y[2], @y[3], @t[2]);
  274. $code.=<<___;
  275. pxor @t[0], @x[0]
  276. pxor @t[0], @x[2]
  277. pxor @t[1], @x[1]
  278. pxor @t[1], @x[3]
  279. movdqa @x[4], @t[0]
  280. movdqa @x[5], @t[1]
  281. pxor @x[6], @t[0]
  282. pxor @x[7], @t[1]
  283. ___
  284. &Mul_GF4_N_GF4 (@t[0], @t[1], @y[0], @y[1], @t[3],
  285. @x[6], @x[7], @y[2], @y[3], @t[2]);
  286. $code.=<<___;
  287. pxor @y[2], @y[0]
  288. pxor @y[3], @y[1]
  289. ___
  290. &Mul_GF4 (@x[4], @x[5], @y[0], @y[1], @t[3]);
  291. $code.=<<___;
  292. pxor @t[0], @x[4]
  293. pxor @t[0], @x[6]
  294. pxor @t[1], @x[5]
  295. pxor @t[1], @x[7]
  296. ___
  297. }
  298. sub Inv_GF256 {
  299. #;********************************************************************
  300. #;* Inv_GF256: Input x0-x7 Output x0-x7 Temp t0-t3,s0-s3 (144) *
  301. #;********************************************************************
  302. my @x=@_[0..7];
  303. my @t=@_[8..11];
  304. my @s=@_[12..15];
  305. # direct optimizations from hardware
  306. $code.=<<___;
  307. movdqa @x[4], @t[3]
  308. movdqa @x[5], @t[2]
  309. movdqa @x[1], @t[1]
  310. movdqa @x[7], @s[1]
  311. movdqa @x[0], @s[0]
  312. pxor @x[6], @t[3]
  313. pxor @x[7], @t[2]
  314. pxor @x[3], @t[1]
  315. movdqa @t[3], @s[2]
  316. pxor @x[6], @s[1]
  317. movdqa @t[2], @t[0]
  318. pxor @x[2], @s[0]
  319. movdqa @t[3], @s[3]
  320. por @t[1], @t[2]
  321. por @s[0], @t[3]
  322. pxor @t[0], @s[3]
  323. pand @s[0], @s[2]
  324. pxor @t[1], @s[0]
  325. pand @t[1], @t[0]
  326. pand @s[0], @s[3]
  327. movdqa @x[3], @s[0]
  328. pxor @x[2], @s[0]
  329. pand @s[0], @s[1]
  330. pxor @s[1], @t[3]
  331. pxor @s[1], @t[2]
  332. movdqa @x[4], @s[1]
  333. movdqa @x[1], @s[0]
  334. pxor @x[5], @s[1]
  335. pxor @x[0], @s[0]
  336. movdqa @s[1], @t[1]
  337. pand @s[0], @s[1]
  338. por @s[0], @t[1]
  339. pxor @s[1], @t[0]
  340. pxor @s[3], @t[3]
  341. pxor @s[2], @t[2]
  342. pxor @s[3], @t[1]
  343. movdqa @x[7], @s[0]
  344. pxor @s[2], @t[0]
  345. movdqa @x[6], @s[1]
  346. pxor @s[2], @t[1]
  347. movdqa @x[5], @s[2]
  348. pand @x[3], @s[0]
  349. movdqa @x[4], @s[3]
  350. pand @x[2], @s[1]
  351. pand @x[1], @s[2]
  352. por @x[0], @s[3]
  353. pxor @s[0], @t[3]
  354. pxor @s[1], @t[2]
  355. pxor @s[2], @t[1]
  356. pxor @s[3], @t[0]
  357. #Inv_GF16 \t0, \t1, \t2, \t3, \s0, \s1, \s2, \s3
  358. # new smaller inversion
  359. movdqa @t[3], @s[0]
  360. pand @t[1], @t[3]
  361. pxor @t[2], @s[0]
  362. movdqa @t[0], @s[2]
  363. movdqa @s[0], @s[3]
  364. pxor @t[3], @s[2]
  365. pand @s[2], @s[3]
  366. movdqa @t[1], @s[1]
  367. pxor @t[2], @s[3]
  368. pxor @t[0], @s[1]
  369. pxor @t[2], @t[3]
  370. pand @t[3], @s[1]
  371. movdqa @s[2], @t[2]
  372. pxor @t[0], @s[1]
  373. pxor @s[1], @t[2]
  374. pxor @s[1], @t[1]
  375. pand @t[0], @t[2]
  376. pxor @t[2], @s[2]
  377. pxor @t[2], @t[1]
  378. pand @s[3], @s[2]
  379. pxor @s[0], @s[2]
  380. ___
  381. # output in s3, s2, s1, t1
  382. # Mul_GF16_2 \x0, \x1, \x2, \x3, \x4, \x5, \x6, \x7, \t2, \t3, \t0, \t1, \s0, \s1, \s2, \s3
  383. # Mul_GF16_2 \x0, \x1, \x2, \x3, \x4, \x5, \x6, \x7, \s3, \s2, \s1, \t1, \s0, \t0, \t2, \t3
  384. &Mul_GF16_2(@x,@s[3,2,1],@t[1],@s[0],@t[0,2,3]);
  385. ### output msb > [x3,x2,x1,x0,x7,x6,x5,x4] < lsb
  386. }
  387. # AES linear components
  388. sub ShiftRows {
  389. my @x=@_[0..7];
  390. my $mask=pop;
  391. $code.=<<___;
  392. pxor 0x00($key),@x[0]
  393. pxor 0x10($key),@x[1]
  394. pxor 0x20($key),@x[2]
  395. pxor 0x30($key),@x[3]
  396. pshufb $mask,@x[0]
  397. pshufb $mask,@x[1]
  398. pxor 0x40($key),@x[4]
  399. pxor 0x50($key),@x[5]
  400. pshufb $mask,@x[2]
  401. pshufb $mask,@x[3]
  402. pxor 0x60($key),@x[6]
  403. pxor 0x70($key),@x[7]
  404. pshufb $mask,@x[4]
  405. pshufb $mask,@x[5]
  406. pshufb $mask,@x[6]
  407. pshufb $mask,@x[7]
  408. lea 0x80($key),$key
  409. ___
  410. }
  411. sub MixColumns {
  412. # modified to emit output in order suitable for feeding back to aesenc[last]
  413. my @x=@_[0..7];
  414. my @t=@_[8..15];
  415. my $inv=@_[16]; # optional
  416. $code.=<<___;
  417. pshufd \$0x93, @x[0], @t[0] # x0 <<< 32
  418. pshufd \$0x93, @x[1], @t[1]
  419. pxor @t[0], @x[0] # x0 ^ (x0 <<< 32)
  420. pshufd \$0x93, @x[2], @t[2]
  421. pxor @t[1], @x[1]
  422. pshufd \$0x93, @x[3], @t[3]
  423. pxor @t[2], @x[2]
  424. pshufd \$0x93, @x[4], @t[4]
  425. pxor @t[3], @x[3]
  426. pshufd \$0x93, @x[5], @t[5]
  427. pxor @t[4], @x[4]
  428. pshufd \$0x93, @x[6], @t[6]
  429. pxor @t[5], @x[5]
  430. pshufd \$0x93, @x[7], @t[7]
  431. pxor @t[6], @x[6]
  432. pxor @t[7], @x[7]
  433. pxor @x[0], @t[1]
  434. pxor @x[7], @t[0]
  435. pxor @x[7], @t[1]
  436. pshufd \$0x4E, @x[0], @x[0] # (x0 ^ (x0 <<< 32)) <<< 64)
  437. pxor @x[1], @t[2]
  438. pshufd \$0x4E, @x[1], @x[1]
  439. pxor @x[4], @t[5]
  440. pxor @t[0], @x[0]
  441. pxor @x[5], @t[6]
  442. pxor @t[1], @x[1]
  443. pxor @x[3], @t[4]
  444. pshufd \$0x4E, @x[4], @t[0]
  445. pxor @x[6], @t[7]
  446. pshufd \$0x4E, @x[5], @t[1]
  447. pxor @x[2], @t[3]
  448. pshufd \$0x4E, @x[3], @x[4]
  449. pxor @x[7], @t[3]
  450. pshufd \$0x4E, @x[7], @x[5]
  451. pxor @x[7], @t[4]
  452. pshufd \$0x4E, @x[6], @x[3]
  453. pxor @t[4], @t[0]
  454. pshufd \$0x4E, @x[2], @x[6]
  455. pxor @t[5], @t[1]
  456. ___
  457. $code.=<<___ if (!$inv);
  458. pxor @t[3], @x[4]
  459. pxor @t[7], @x[5]
  460. pxor @t[6], @x[3]
  461. movdqa @t[0], @x[2]
  462. pxor @t[2], @x[6]
  463. movdqa @t[1], @x[7]
  464. ___
  465. $code.=<<___ if ($inv);
  466. pxor @x[4], @t[3]
  467. pxor @t[7], @x[5]
  468. pxor @x[3], @t[6]
  469. movdqa @t[0], @x[3]
  470. pxor @t[2], @x[6]
  471. movdqa @t[6], @x[2]
  472. movdqa @t[1], @x[7]
  473. movdqa @x[6], @x[4]
  474. movdqa @t[3], @x[6]
  475. ___
  476. }
  477. sub InvMixColumns_orig {
  478. my @x=@_[0..7];
  479. my @t=@_[8..15];
  480. $code.=<<___;
  481. # multiplication by 0x0e
  482. pshufd \$0x93, @x[7], @t[7]
  483. movdqa @x[2], @t[2]
  484. pxor @x[5], @x[7] # 7 5
  485. pxor @x[5], @x[2] # 2 5
  486. pshufd \$0x93, @x[0], @t[0]
  487. movdqa @x[5], @t[5]
  488. pxor @x[0], @x[5] # 5 0 [1]
  489. pxor @x[1], @x[0] # 0 1
  490. pshufd \$0x93, @x[1], @t[1]
  491. pxor @x[2], @x[1] # 1 25
  492. pxor @x[6], @x[0] # 01 6 [2]
  493. pxor @x[3], @x[1] # 125 3 [4]
  494. pshufd \$0x93, @x[3], @t[3]
  495. pxor @x[0], @x[2] # 25 016 [3]
  496. pxor @x[7], @x[3] # 3 75
  497. pxor @x[6], @x[7] # 75 6 [0]
  498. pshufd \$0x93, @x[6], @t[6]
  499. movdqa @x[4], @t[4]
  500. pxor @x[4], @x[6] # 6 4
  501. pxor @x[3], @x[4] # 4 375 [6]
  502. pxor @x[7], @x[3] # 375 756=36
  503. pxor @t[5], @x[6] # 64 5 [7]
  504. pxor @t[2], @x[3] # 36 2
  505. pxor @t[4], @x[3] # 362 4 [5]
  506. pshufd \$0x93, @t[5], @t[5]
  507. ___
  508. my @y = @x[7,5,0,2,1,3,4,6];
  509. $code.=<<___;
  510. # multiplication by 0x0b
  511. pxor @y[0], @y[1]
  512. pxor @t[0], @y[0]
  513. pxor @t[1], @y[1]
  514. pshufd \$0x93, @t[2], @t[2]
  515. pxor @t[5], @y[0]
  516. pxor @t[6], @y[1]
  517. pxor @t[7], @y[0]
  518. pshufd \$0x93, @t[4], @t[4]
  519. pxor @t[6], @t[7] # clobber t[7]
  520. pxor @y[0], @y[1]
  521. pxor @t[0], @y[3]
  522. pshufd \$0x93, @t[0], @t[0]
  523. pxor @t[1], @y[2]
  524. pxor @t[1], @y[4]
  525. pxor @t[2], @y[2]
  526. pshufd \$0x93, @t[1], @t[1]
  527. pxor @t[2], @y[3]
  528. pxor @t[2], @y[5]
  529. pxor @t[7], @y[2]
  530. pshufd \$0x93, @t[2], @t[2]
  531. pxor @t[3], @y[3]
  532. pxor @t[3], @y[6]
  533. pxor @t[3], @y[4]
  534. pshufd \$0x93, @t[3], @t[3]
  535. pxor @t[4], @y[7]
  536. pxor @t[4], @y[5]
  537. pxor @t[7], @y[7]
  538. pxor @t[5], @y[3]
  539. pxor @t[4], @y[4]
  540. pxor @t[5], @t[7] # clobber t[7] even more
  541. pxor @t[7], @y[5]
  542. pshufd \$0x93, @t[4], @t[4]
  543. pxor @t[7], @y[6]
  544. pxor @t[7], @y[4]
  545. pxor @t[5], @t[7]
  546. pshufd \$0x93, @t[5], @t[5]
  547. pxor @t[6], @t[7] # restore t[7]
  548. # multiplication by 0x0d
  549. pxor @y[7], @y[4]
  550. pxor @t[4], @y[7]
  551. pshufd \$0x93, @t[6], @t[6]
  552. pxor @t[0], @y[2]
  553. pxor @t[5], @y[7]
  554. pxor @t[2], @y[2]
  555. pshufd \$0x93, @t[7], @t[7]
  556. pxor @y[1], @y[3]
  557. pxor @t[1], @y[1]
  558. pxor @t[0], @y[0]
  559. pxor @t[0], @y[3]
  560. pxor @t[5], @y[1]
  561. pxor @t[5], @y[0]
  562. pxor @t[7], @y[1]
  563. pshufd \$0x93, @t[0], @t[0]
  564. pxor @t[6], @y[0]
  565. pxor @y[1], @y[3]
  566. pxor @t[1], @y[4]
  567. pshufd \$0x93, @t[1], @t[1]
  568. pxor @t[7], @y[7]
  569. pxor @t[2], @y[4]
  570. pxor @t[2], @y[5]
  571. pshufd \$0x93, @t[2], @t[2]
  572. pxor @t[6], @y[2]
  573. pxor @t[3], @t[6] # clobber t[6]
  574. pxor @y[7], @y[4]
  575. pxor @t[6], @y[3]
  576. pxor @t[6], @y[6]
  577. pxor @t[5], @y[5]
  578. pxor @t[4], @y[6]
  579. pshufd \$0x93, @t[4], @t[4]
  580. pxor @t[6], @y[5]
  581. pxor @t[7], @y[6]
  582. pxor @t[3], @t[6] # restore t[6]
  583. pshufd \$0x93, @t[5], @t[5]
  584. pshufd \$0x93, @t[6], @t[6]
  585. pshufd \$0x93, @t[7], @t[7]
  586. pshufd \$0x93, @t[3], @t[3]
  587. # multiplication by 0x09
  588. pxor @y[1], @y[4]
  589. pxor @y[1], @t[1] # t[1]=y[1]
  590. pxor @t[5], @t[0] # clobber t[0]
  591. pxor @t[5], @t[1]
  592. pxor @t[0], @y[3]
  593. pxor @y[0], @t[0] # t[0]=y[0]
  594. pxor @t[6], @t[1]
  595. pxor @t[7], @t[6] # clobber t[6]
  596. pxor @t[1], @y[4]
  597. pxor @t[4], @y[7]
  598. pxor @y[4], @t[4] # t[4]=y[4]
  599. pxor @t[3], @y[6]
  600. pxor @y[3], @t[3] # t[3]=y[3]
  601. pxor @t[2], @y[5]
  602. pxor @y[2], @t[2] # t[2]=y[2]
  603. pxor @t[7], @t[3]
  604. pxor @y[5], @t[5] # t[5]=y[5]
  605. pxor @t[6], @t[2]
  606. pxor @t[6], @t[5]
  607. pxor @y[6], @t[6] # t[6]=y[6]
  608. pxor @y[7], @t[7] # t[7]=y[7]
  609. movdqa @t[0],@XMM[0]
  610. movdqa @t[1],@XMM[1]
  611. movdqa @t[2],@XMM[2]
  612. movdqa @t[3],@XMM[3]
  613. movdqa @t[4],@XMM[4]
  614. movdqa @t[5],@XMM[5]
  615. movdqa @t[6],@XMM[6]
  616. movdqa @t[7],@XMM[7]
  617. ___
  618. }
  619. sub InvMixColumns {
  620. my @x=@_[0..7];
  621. my @t=@_[8..15];
  622. # Thanks to Jussi Kivilinna for providing pointer to
  623. #
  624. # | 0e 0b 0d 09 | | 02 03 01 01 | | 05 00 04 00 |
  625. # | 09 0e 0b 0d | = | 01 02 03 01 | x | 00 05 00 04 |
  626. # | 0d 09 0e 0b | | 01 01 02 03 | | 04 00 05 00 |
  627. # | 0b 0d 09 0e | | 03 01 01 02 | | 00 04 00 05 |
  628. $code.=<<___;
  629. # multiplication by 0x05-0x00-0x04-0x00
  630. pshufd \$0x4E, @x[0], @t[0]
  631. pshufd \$0x4E, @x[6], @t[6]
  632. pxor @x[0], @t[0]
  633. pshufd \$0x4E, @x[7], @t[7]
  634. pxor @x[6], @t[6]
  635. pshufd \$0x4E, @x[1], @t[1]
  636. pxor @x[7], @t[7]
  637. pshufd \$0x4E, @x[2], @t[2]
  638. pxor @x[1], @t[1]
  639. pshufd \$0x4E, @x[3], @t[3]
  640. pxor @x[2], @t[2]
  641. pxor @t[6], @x[0]
  642. pxor @t[6], @x[1]
  643. pshufd \$0x4E, @x[4], @t[4]
  644. pxor @x[3], @t[3]
  645. pxor @t[0], @x[2]
  646. pxor @t[1], @x[3]
  647. pshufd \$0x4E, @x[5], @t[5]
  648. pxor @x[4], @t[4]
  649. pxor @t[7], @x[1]
  650. pxor @t[2], @x[4]
  651. pxor @x[5], @t[5]
  652. pxor @t[7], @x[2]
  653. pxor @t[6], @x[3]
  654. pxor @t[6], @x[4]
  655. pxor @t[3], @x[5]
  656. pxor @t[4], @x[6]
  657. pxor @t[7], @x[4]
  658. pxor @t[7], @x[5]
  659. pxor @t[5], @x[7]
  660. ___
  661. &MixColumns (@x,@t,1); # flipped 2<->3 and 4<->6
  662. }
  663. sub aesenc { # not used
  664. my @b=@_[0..7];
  665. my @t=@_[8..15];
  666. $code.=<<___;
  667. movdqa 0x30($const),@t[0] # .LSR
  668. ___
  669. &ShiftRows (@b,@t[0]);
  670. &Sbox (@b,@t);
  671. &MixColumns (@b[0,1,4,6,3,7,2,5],@t);
  672. }
  673. sub aesenclast { # not used
  674. my @b=@_[0..7];
  675. my @t=@_[8..15];
  676. $code.=<<___;
  677. movdqa 0x40($const),@t[0] # .LSRM0
  678. ___
  679. &ShiftRows (@b,@t[0]);
  680. &Sbox (@b,@t);
  681. $code.=<<___
  682. pxor 0x00($key),@b[0]
  683. pxor 0x10($key),@b[1]
  684. pxor 0x20($key),@b[4]
  685. pxor 0x30($key),@b[6]
  686. pxor 0x40($key),@b[3]
  687. pxor 0x50($key),@b[7]
  688. pxor 0x60($key),@b[2]
  689. pxor 0x70($key),@b[5]
  690. ___
  691. }
  692. sub swapmove {
  693. my ($a,$b,$n,$mask,$t)=@_;
  694. $code.=<<___;
  695. movdqa $b,$t
  696. psrlq \$$n,$b
  697. pxor $a,$b
  698. pand $mask,$b
  699. pxor $b,$a
  700. psllq \$$n,$b
  701. pxor $t,$b
  702. ___
  703. }
  704. sub swapmove2x {
  705. my ($a0,$b0,$a1,$b1,$n,$mask,$t0,$t1)=@_;
  706. $code.=<<___;
  707. movdqa $b0,$t0
  708. psrlq \$$n,$b0
  709. movdqa $b1,$t1
  710. psrlq \$$n,$b1
  711. pxor $a0,$b0
  712. pxor $a1,$b1
  713. pand $mask,$b0
  714. pand $mask,$b1
  715. pxor $b0,$a0
  716. psllq \$$n,$b0
  717. pxor $b1,$a1
  718. psllq \$$n,$b1
  719. pxor $t0,$b0
  720. pxor $t1,$b1
  721. ___
  722. }
  723. sub bitslice {
  724. my @x=reverse(@_[0..7]);
  725. my ($t0,$t1,$t2,$t3)=@_[8..11];
  726. $code.=<<___;
  727. movdqa 0x00($const),$t0 # .LBS0
  728. movdqa 0x10($const),$t1 # .LBS1
  729. ___
  730. &swapmove2x(@x[0,1,2,3],1,$t0,$t2,$t3);
  731. &swapmove2x(@x[4,5,6,7],1,$t0,$t2,$t3);
  732. $code.=<<___;
  733. movdqa 0x20($const),$t0 # .LBS2
  734. ___
  735. &swapmove2x(@x[0,2,1,3],2,$t1,$t2,$t3);
  736. &swapmove2x(@x[4,6,5,7],2,$t1,$t2,$t3);
  737. &swapmove2x(@x[0,4,1,5],4,$t0,$t2,$t3);
  738. &swapmove2x(@x[2,6,3,7],4,$t0,$t2,$t3);
  739. }
  740. $code.=<<___;
  741. .text
  742. .extern asm_AES_encrypt
  743. .extern asm_AES_decrypt
  744. .type _bsaes_encrypt8,\@abi-omnipotent
  745. .align 64
  746. _bsaes_encrypt8:
  747. .cfi_startproc
  748. lea .LBS0(%rip), $const # constants table
  749. movdqa ($key), @XMM[9] # round 0 key
  750. lea 0x10($key), $key
  751. movdqa 0x50($const), @XMM[8] # .LM0SR
  752. pxor @XMM[9], @XMM[0] # xor with round0 key
  753. pxor @XMM[9], @XMM[1]
  754. pxor @XMM[9], @XMM[2]
  755. pxor @XMM[9], @XMM[3]
  756. pshufb @XMM[8], @XMM[0]
  757. pshufb @XMM[8], @XMM[1]
  758. pxor @XMM[9], @XMM[4]
  759. pxor @XMM[9], @XMM[5]
  760. pshufb @XMM[8], @XMM[2]
  761. pshufb @XMM[8], @XMM[3]
  762. pxor @XMM[9], @XMM[6]
  763. pxor @XMM[9], @XMM[7]
  764. pshufb @XMM[8], @XMM[4]
  765. pshufb @XMM[8], @XMM[5]
  766. pshufb @XMM[8], @XMM[6]
  767. pshufb @XMM[8], @XMM[7]
  768. _bsaes_encrypt8_bitslice:
  769. ___
  770. &bitslice (@XMM[0..7, 8..11]);
  771. $code.=<<___;
  772. dec $rounds
  773. jmp .Lenc_sbox
  774. .align 16
  775. .Lenc_loop:
  776. ___
  777. &ShiftRows (@XMM[0..7, 8]);
  778. $code.=".Lenc_sbox:\n";
  779. &Sbox (@XMM[0..7, 8..15]);
  780. $code.=<<___;
  781. dec $rounds
  782. jl .Lenc_done
  783. ___
  784. &MixColumns (@XMM[0,1,4,6,3,7,2,5, 8..15]);
  785. $code.=<<___;
  786. movdqa 0x30($const), @XMM[8] # .LSR
  787. jnz .Lenc_loop
  788. movdqa 0x40($const), @XMM[8] # .LSRM0
  789. jmp .Lenc_loop
  790. .align 16
  791. .Lenc_done:
  792. ___
  793. # output in lsb > [t0, t1, t4, t6, t3, t7, t2, t5] < msb
  794. &bitslice (@XMM[0,1,4,6,3,7,2,5, 8..11]);
  795. $code.=<<___;
  796. movdqa ($key), @XMM[8] # last round key
  797. pxor @XMM[8], @XMM[4]
  798. pxor @XMM[8], @XMM[6]
  799. pxor @XMM[8], @XMM[3]
  800. pxor @XMM[8], @XMM[7]
  801. pxor @XMM[8], @XMM[2]
  802. pxor @XMM[8], @XMM[5]
  803. pxor @XMM[8], @XMM[0]
  804. pxor @XMM[8], @XMM[1]
  805. ret
  806. .cfi_endproc
  807. .size _bsaes_encrypt8,.-_bsaes_encrypt8
  808. .type _bsaes_decrypt8,\@abi-omnipotent
  809. .align 64
  810. _bsaes_decrypt8:
  811. .cfi_startproc
  812. lea .LBS0(%rip), $const # constants table
  813. movdqa ($key), @XMM[9] # round 0 key
  814. lea 0x10($key), $key
  815. movdqa -0x30($const), @XMM[8] # .LM0ISR
  816. pxor @XMM[9], @XMM[0] # xor with round0 key
  817. pxor @XMM[9], @XMM[1]
  818. pxor @XMM[9], @XMM[2]
  819. pxor @XMM[9], @XMM[3]
  820. pshufb @XMM[8], @XMM[0]
  821. pshufb @XMM[8], @XMM[1]
  822. pxor @XMM[9], @XMM[4]
  823. pxor @XMM[9], @XMM[5]
  824. pshufb @XMM[8], @XMM[2]
  825. pshufb @XMM[8], @XMM[3]
  826. pxor @XMM[9], @XMM[6]
  827. pxor @XMM[9], @XMM[7]
  828. pshufb @XMM[8], @XMM[4]
  829. pshufb @XMM[8], @XMM[5]
  830. pshufb @XMM[8], @XMM[6]
  831. pshufb @XMM[8], @XMM[7]
  832. ___
  833. &bitslice (@XMM[0..7, 8..11]);
  834. $code.=<<___;
  835. dec $rounds
  836. jmp .Ldec_sbox
  837. .align 16
  838. .Ldec_loop:
  839. ___
  840. &ShiftRows (@XMM[0..7, 8]);
  841. $code.=".Ldec_sbox:\n";
  842. &InvSbox (@XMM[0..7, 8..15]);
  843. $code.=<<___;
  844. dec $rounds
  845. jl .Ldec_done
  846. ___
  847. &InvMixColumns (@XMM[0,1,6,4,2,7,3,5, 8..15]);
  848. $code.=<<___;
  849. movdqa -0x10($const), @XMM[8] # .LISR
  850. jnz .Ldec_loop
  851. movdqa -0x20($const), @XMM[8] # .LISRM0
  852. jmp .Ldec_loop
  853. .align 16
  854. .Ldec_done:
  855. ___
  856. &bitslice (@XMM[0,1,6,4,2,7,3,5, 8..11]);
  857. $code.=<<___;
  858. movdqa ($key), @XMM[8] # last round key
  859. pxor @XMM[8], @XMM[6]
  860. pxor @XMM[8], @XMM[4]
  861. pxor @XMM[8], @XMM[2]
  862. pxor @XMM[8], @XMM[7]
  863. pxor @XMM[8], @XMM[3]
  864. pxor @XMM[8], @XMM[5]
  865. pxor @XMM[8], @XMM[0]
  866. pxor @XMM[8], @XMM[1]
  867. ret
  868. .cfi_endproc
  869. .size _bsaes_decrypt8,.-_bsaes_decrypt8
  870. ___
  871. }
  872. {
  873. my ($out,$inp,$rounds,$const)=("%rax","%rcx","%r10d","%r11");
  874. sub bitslice_key {
  875. my @x=reverse(@_[0..7]);
  876. my ($bs0,$bs1,$bs2,$t2,$t3)=@_[8..12];
  877. &swapmove (@x[0,1],1,$bs0,$t2,$t3);
  878. $code.=<<___;
  879. #&swapmove(@x[2,3],1,$t0,$t2,$t3);
  880. movdqa @x[0], @x[2]
  881. movdqa @x[1], @x[3]
  882. ___
  883. #&swapmove2x(@x[4,5,6,7],1,$t0,$t2,$t3);
  884. &swapmove2x (@x[0,2,1,3],2,$bs1,$t2,$t3);
  885. $code.=<<___;
  886. #&swapmove2x(@x[4,6,5,7],2,$t1,$t2,$t3);
  887. movdqa @x[0], @x[4]
  888. movdqa @x[2], @x[6]
  889. movdqa @x[1], @x[5]
  890. movdqa @x[3], @x[7]
  891. ___
  892. &swapmove2x (@x[0,4,1,5],4,$bs2,$t2,$t3);
  893. &swapmove2x (@x[2,6,3,7],4,$bs2,$t2,$t3);
  894. }
  895. $code.=<<___;
  896. .type _bsaes_key_convert,\@abi-omnipotent
  897. .align 16
  898. _bsaes_key_convert:
  899. .cfi_startproc
  900. lea .Lmasks(%rip), $const
  901. movdqu ($inp), %xmm7 # load round 0 key
  902. lea 0x10($inp), $inp
  903. movdqa 0x00($const), %xmm0 # 0x01...
  904. movdqa 0x10($const), %xmm1 # 0x02...
  905. movdqa 0x20($const), %xmm2 # 0x04...
  906. movdqa 0x30($const), %xmm3 # 0x08...
  907. movdqa 0x40($const), %xmm4 # .LM0
  908. pcmpeqd %xmm5, %xmm5 # .LNOT
  909. movdqu ($inp), %xmm6 # load round 1 key
  910. movdqa %xmm7, ($out) # save round 0 key
  911. lea 0x10($out), $out
  912. dec $rounds
  913. jmp .Lkey_loop
  914. .align 16
  915. .Lkey_loop:
  916. pshufb %xmm4, %xmm6 # .LM0
  917. movdqa %xmm0, %xmm8
  918. movdqa %xmm1, %xmm9
  919. pand %xmm6, %xmm8
  920. pand %xmm6, %xmm9
  921. movdqa %xmm2, %xmm10
  922. pcmpeqb %xmm0, %xmm8
  923. psllq \$4, %xmm0 # 0x10...
  924. movdqa %xmm3, %xmm11
  925. pcmpeqb %xmm1, %xmm9
  926. psllq \$4, %xmm1 # 0x20...
  927. pand %xmm6, %xmm10
  928. pand %xmm6, %xmm11
  929. movdqa %xmm0, %xmm12
  930. pcmpeqb %xmm2, %xmm10
  931. psllq \$4, %xmm2 # 0x40...
  932. movdqa %xmm1, %xmm13
  933. pcmpeqb %xmm3, %xmm11
  934. psllq \$4, %xmm3 # 0x80...
  935. movdqa %xmm2, %xmm14
  936. movdqa %xmm3, %xmm15
  937. pxor %xmm5, %xmm8 # "pnot"
  938. pxor %xmm5, %xmm9
  939. pand %xmm6, %xmm12
  940. pand %xmm6, %xmm13
  941. movdqa %xmm8, 0x00($out) # write bit-sliced round key
  942. pcmpeqb %xmm0, %xmm12
  943. psrlq \$4, %xmm0 # 0x01...
  944. movdqa %xmm9, 0x10($out)
  945. pcmpeqb %xmm1, %xmm13
  946. psrlq \$4, %xmm1 # 0x02...
  947. lea 0x10($inp), $inp
  948. pand %xmm6, %xmm14
  949. pand %xmm6, %xmm15
  950. movdqa %xmm10, 0x20($out)
  951. pcmpeqb %xmm2, %xmm14
  952. psrlq \$4, %xmm2 # 0x04...
  953. movdqa %xmm11, 0x30($out)
  954. pcmpeqb %xmm3, %xmm15
  955. psrlq \$4, %xmm3 # 0x08...
  956. movdqu ($inp), %xmm6 # load next round key
  957. pxor %xmm5, %xmm13 # "pnot"
  958. pxor %xmm5, %xmm14
  959. movdqa %xmm12, 0x40($out)
  960. movdqa %xmm13, 0x50($out)
  961. movdqa %xmm14, 0x60($out)
  962. movdqa %xmm15, 0x70($out)
  963. lea 0x80($out),$out
  964. dec $rounds
  965. jnz .Lkey_loop
  966. movdqa 0x50($const), %xmm7 # .L63
  967. #movdqa %xmm6, ($out) # don't save last round key
  968. ret
  969. .cfi_endproc
  970. .size _bsaes_key_convert,.-_bsaes_key_convert
  971. ___
  972. }
  973. if (0 && !$win64) { # following four functions are unsupported interface
  974. # used for benchmarking...
  975. $code.=<<___;
  976. .globl bsaes_enc_key_convert
  977. .type bsaes_enc_key_convert,\@function,2
  978. .align 16
  979. bsaes_enc_key_convert:
  980. mov 240($inp),%r10d # pass rounds
  981. mov $inp,%rcx # pass key
  982. mov $out,%rax # pass key schedule
  983. call _bsaes_key_convert
  984. pxor %xmm6,%xmm7 # fix up last round key
  985. movdqa %xmm7,(%rax) # save last round key
  986. ret
  987. .size bsaes_enc_key_convert,.-bsaes_enc_key_convert
  988. .globl bsaes_encrypt_128
  989. .type bsaes_encrypt_128,\@function,4
  990. .align 16
  991. bsaes_encrypt_128:
  992. .Lenc128_loop:
  993. movdqu 0x00($inp), @XMM[0] # load input
  994. movdqu 0x10($inp), @XMM[1]
  995. movdqu 0x20($inp), @XMM[2]
  996. movdqu 0x30($inp), @XMM[3]
  997. movdqu 0x40($inp), @XMM[4]
  998. movdqu 0x50($inp), @XMM[5]
  999. movdqu 0x60($inp), @XMM[6]
  1000. movdqu 0x70($inp), @XMM[7]
  1001. mov $key, %rax # pass the $key
  1002. lea 0x80($inp), $inp
  1003. mov \$10,%r10d
  1004. call _bsaes_encrypt8
  1005. movdqu @XMM[0], 0x00($out) # write output
  1006. movdqu @XMM[1], 0x10($out)
  1007. movdqu @XMM[4], 0x20($out)
  1008. movdqu @XMM[6], 0x30($out)
  1009. movdqu @XMM[3], 0x40($out)
  1010. movdqu @XMM[7], 0x50($out)
  1011. movdqu @XMM[2], 0x60($out)
  1012. movdqu @XMM[5], 0x70($out)
  1013. lea 0x80($out), $out
  1014. sub \$0x80,$len
  1015. ja .Lenc128_loop
  1016. ret
  1017. .size bsaes_encrypt_128,.-bsaes_encrypt_128
  1018. .globl bsaes_dec_key_convert
  1019. .type bsaes_dec_key_convert,\@function,2
  1020. .align 16
  1021. bsaes_dec_key_convert:
  1022. mov 240($inp),%r10d # pass rounds
  1023. mov $inp,%rcx # pass key
  1024. mov $out,%rax # pass key schedule
  1025. call _bsaes_key_convert
  1026. pxor ($out),%xmm7 # fix up round 0 key
  1027. movdqa %xmm6,(%rax) # save last round key
  1028. movdqa %xmm7,($out)
  1029. ret
  1030. .size bsaes_dec_key_convert,.-bsaes_dec_key_convert
  1031. .globl bsaes_decrypt_128
  1032. .type bsaes_decrypt_128,\@function,4
  1033. .align 16
  1034. bsaes_decrypt_128:
  1035. .Ldec128_loop:
  1036. movdqu 0x00($inp), @XMM[0] # load input
  1037. movdqu 0x10($inp), @XMM[1]
  1038. movdqu 0x20($inp), @XMM[2]
  1039. movdqu 0x30($inp), @XMM[3]
  1040. movdqu 0x40($inp), @XMM[4]
  1041. movdqu 0x50($inp), @XMM[5]
  1042. movdqu 0x60($inp), @XMM[6]
  1043. movdqu 0x70($inp), @XMM[7]
  1044. mov $key, %rax # pass the $key
  1045. lea 0x80($inp), $inp
  1046. mov \$10,%r10d
  1047. call _bsaes_decrypt8
  1048. movdqu @XMM[0], 0x00($out) # write output
  1049. movdqu @XMM[1], 0x10($out)
  1050. movdqu @XMM[6], 0x20($out)
  1051. movdqu @XMM[4], 0x30($out)
  1052. movdqu @XMM[2], 0x40($out)
  1053. movdqu @XMM[7], 0x50($out)
  1054. movdqu @XMM[3], 0x60($out)
  1055. movdqu @XMM[5], 0x70($out)
  1056. lea 0x80($out), $out
  1057. sub \$0x80,$len
  1058. ja .Ldec128_loop
  1059. ret
  1060. .size bsaes_decrypt_128,.-bsaes_decrypt_128
  1061. ___
  1062. }
  1063. {
  1064. ######################################################################
  1065. #
  1066. # OpenSSL interface
  1067. #
  1068. my ($arg1,$arg2,$arg3,$arg4,$arg5,$arg6)=$win64 ? ("%rcx","%rdx","%r8","%r9","%r10","%r11d")
  1069. : ("%rdi","%rsi","%rdx","%rcx","%r8","%r9d");
  1070. my ($inp,$out,$len,$key)=("%r12","%r13","%r14","%r15");
  1071. if ($ecb) {
  1072. $code.=<<___;
  1073. .globl bsaes_ecb_encrypt_blocks
  1074. .type bsaes_ecb_encrypt_blocks,\@abi-omnipotent
  1075. .align 16
  1076. bsaes_ecb_encrypt_blocks:
  1077. .cfi_startproc
  1078. mov %rsp, %rax
  1079. .Lecb_enc_prologue:
  1080. push %rbp
  1081. .cfi_push %rbp
  1082. push %rbx
  1083. .cfi_push %rbx
  1084. push %r12
  1085. .cfi_push %r12
  1086. push %r13
  1087. .cfi_push %r13
  1088. push %r14
  1089. .cfi_push %r14
  1090. push %r15
  1091. .cfi_push %r15
  1092. lea -0x48(%rsp),%rsp
  1093. .cfi_adjust_cfa_offset 0x48
  1094. ___
  1095. $code.=<<___ if ($win64);
  1096. lea -0xa0(%rsp), %rsp
  1097. movaps %xmm6, 0x40(%rsp)
  1098. movaps %xmm7, 0x50(%rsp)
  1099. movaps %xmm8, 0x60(%rsp)
  1100. movaps %xmm9, 0x70(%rsp)
  1101. movaps %xmm10, 0x80(%rsp)
  1102. movaps %xmm11, 0x90(%rsp)
  1103. movaps %xmm12, 0xa0(%rsp)
  1104. movaps %xmm13, 0xb0(%rsp)
  1105. movaps %xmm14, 0xc0(%rsp)
  1106. movaps %xmm15, 0xd0(%rsp)
  1107. .Lecb_enc_body:
  1108. ___
  1109. $code.=<<___;
  1110. mov %rsp,%rbp # backup %rsp
  1111. .cfi_def_cfa_register %rbp
  1112. mov 240($arg4),%eax # rounds
  1113. mov $arg1,$inp # backup arguments
  1114. mov $arg2,$out
  1115. mov $arg3,$len
  1116. mov $arg4,$key
  1117. cmp \$8,$arg3
  1118. jb .Lecb_enc_short
  1119. mov %eax,%ebx # backup rounds
  1120. shl \$7,%rax # 128 bytes per inner round key
  1121. sub \$`128-32`,%rax # size of bit-sliced key schedule
  1122. sub %rax,%rsp
  1123. mov %rsp,%rax # pass key schedule
  1124. mov $key,%rcx # pass key
  1125. mov %ebx,%r10d # pass rounds
  1126. call _bsaes_key_convert
  1127. pxor %xmm6,%xmm7 # fix up last round key
  1128. movdqa %xmm7,(%rax) # save last round key
  1129. sub \$8,$len
  1130. .Lecb_enc_loop:
  1131. movdqu 0x00($inp), @XMM[0] # load input
  1132. movdqu 0x10($inp), @XMM[1]
  1133. movdqu 0x20($inp), @XMM[2]
  1134. movdqu 0x30($inp), @XMM[3]
  1135. movdqu 0x40($inp), @XMM[4]
  1136. movdqu 0x50($inp), @XMM[5]
  1137. mov %rsp, %rax # pass key schedule
  1138. movdqu 0x60($inp), @XMM[6]
  1139. mov %ebx,%r10d # pass rounds
  1140. movdqu 0x70($inp), @XMM[7]
  1141. lea 0x80($inp), $inp
  1142. call _bsaes_encrypt8
  1143. movdqu @XMM[0], 0x00($out) # write output
  1144. movdqu @XMM[1], 0x10($out)
  1145. movdqu @XMM[4], 0x20($out)
  1146. movdqu @XMM[6], 0x30($out)
  1147. movdqu @XMM[3], 0x40($out)
  1148. movdqu @XMM[7], 0x50($out)
  1149. movdqu @XMM[2], 0x60($out)
  1150. movdqu @XMM[5], 0x70($out)
  1151. lea 0x80($out), $out
  1152. sub \$8,$len
  1153. jnc .Lecb_enc_loop
  1154. add \$8,$len
  1155. jz .Lecb_enc_done
  1156. movdqu 0x00($inp), @XMM[0] # load input
  1157. mov %rsp, %rax # pass key schedule
  1158. mov %ebx,%r10d # pass rounds
  1159. cmp \$2,$len
  1160. jb .Lecb_enc_one
  1161. movdqu 0x10($inp), @XMM[1]
  1162. je .Lecb_enc_two
  1163. movdqu 0x20($inp), @XMM[2]
  1164. cmp \$4,$len
  1165. jb .Lecb_enc_three
  1166. movdqu 0x30($inp), @XMM[3]
  1167. je .Lecb_enc_four
  1168. movdqu 0x40($inp), @XMM[4]
  1169. cmp \$6,$len
  1170. jb .Lecb_enc_five
  1171. movdqu 0x50($inp), @XMM[5]
  1172. je .Lecb_enc_six
  1173. movdqu 0x60($inp), @XMM[6]
  1174. call _bsaes_encrypt8
  1175. movdqu @XMM[0], 0x00($out) # write output
  1176. movdqu @XMM[1], 0x10($out)
  1177. movdqu @XMM[4], 0x20($out)
  1178. movdqu @XMM[6], 0x30($out)
  1179. movdqu @XMM[3], 0x40($out)
  1180. movdqu @XMM[7], 0x50($out)
  1181. movdqu @XMM[2], 0x60($out)
  1182. jmp .Lecb_enc_done
  1183. .align 16
  1184. .Lecb_enc_six:
  1185. call _bsaes_encrypt8
  1186. movdqu @XMM[0], 0x00($out) # write output
  1187. movdqu @XMM[1], 0x10($out)
  1188. movdqu @XMM[4], 0x20($out)
  1189. movdqu @XMM[6], 0x30($out)
  1190. movdqu @XMM[3], 0x40($out)
  1191. movdqu @XMM[7], 0x50($out)
  1192. jmp .Lecb_enc_done
  1193. .align 16
  1194. .Lecb_enc_five:
  1195. call _bsaes_encrypt8
  1196. movdqu @XMM[0], 0x00($out) # write output
  1197. movdqu @XMM[1], 0x10($out)
  1198. movdqu @XMM[4], 0x20($out)
  1199. movdqu @XMM[6], 0x30($out)
  1200. movdqu @XMM[3], 0x40($out)
  1201. jmp .Lecb_enc_done
  1202. .align 16
  1203. .Lecb_enc_four:
  1204. call _bsaes_encrypt8
  1205. movdqu @XMM[0], 0x00($out) # write output
  1206. movdqu @XMM[1], 0x10($out)
  1207. movdqu @XMM[4], 0x20($out)
  1208. movdqu @XMM[6], 0x30($out)
  1209. jmp .Lecb_enc_done
  1210. .align 16
  1211. .Lecb_enc_three:
  1212. call _bsaes_encrypt8
  1213. movdqu @XMM[0], 0x00($out) # write output
  1214. movdqu @XMM[1], 0x10($out)
  1215. movdqu @XMM[4], 0x20($out)
  1216. jmp .Lecb_enc_done
  1217. .align 16
  1218. .Lecb_enc_two:
  1219. call _bsaes_encrypt8
  1220. movdqu @XMM[0], 0x00($out) # write output
  1221. movdqu @XMM[1], 0x10($out)
  1222. jmp .Lecb_enc_done
  1223. .align 16
  1224. .Lecb_enc_one:
  1225. call _bsaes_encrypt8
  1226. movdqu @XMM[0], 0x00($out) # write output
  1227. jmp .Lecb_enc_done
  1228. .align 16
  1229. .Lecb_enc_short:
  1230. lea ($inp), $arg1
  1231. lea ($out), $arg2
  1232. lea ($key), $arg3
  1233. call asm_AES_encrypt
  1234. lea 16($inp), $inp
  1235. lea 16($out), $out
  1236. dec $len
  1237. jnz .Lecb_enc_short
  1238. .Lecb_enc_done:
  1239. lea (%rsp),%rax
  1240. pxor %xmm0, %xmm0
  1241. .Lecb_enc_bzero: # wipe key schedule [if any]
  1242. movdqa %xmm0, 0x00(%rax)
  1243. movdqa %xmm0, 0x10(%rax)
  1244. lea 0x20(%rax), %rax
  1245. cmp %rax, %rbp
  1246. jb .Lecb_enc_bzero
  1247. lea 0x78(%rbp),%rax
  1248. .cfi_def_cfa %rax,8
  1249. ___
  1250. $code.=<<___ if ($win64);
  1251. movaps 0x40(%rbp), %xmm6
  1252. movaps 0x50(%rbp), %xmm7
  1253. movaps 0x60(%rbp), %xmm8
  1254. movaps 0x70(%rbp), %xmm9
  1255. movaps 0x80(%rbp), %xmm10
  1256. movaps 0x90(%rbp), %xmm11
  1257. movaps 0xa0(%rbp), %xmm12
  1258. movaps 0xb0(%rbp), %xmm13
  1259. movaps 0xc0(%rbp), %xmm14
  1260. movaps 0xd0(%rbp), %xmm15
  1261. lea 0xa0(%rax), %rax
  1262. .Lecb_enc_tail:
  1263. ___
  1264. $code.=<<___;
  1265. mov -48(%rax), %r15
  1266. .cfi_restore %r15
  1267. mov -40(%rax), %r14
  1268. .cfi_restore %r14
  1269. mov -32(%rax), %r13
  1270. .cfi_restore %r13
  1271. mov -24(%rax), %r12
  1272. .cfi_restore %r12
  1273. mov -16(%rax), %rbx
  1274. .cfi_restore %rbx
  1275. mov -8(%rax), %rbp
  1276. .cfi_restore %rbp
  1277. lea (%rax), %rsp # restore %rsp
  1278. .cfi_def_cfa_register %rsp
  1279. .Lecb_enc_epilogue:
  1280. ret
  1281. .cfi_endproc
  1282. .size bsaes_ecb_encrypt_blocks,.-bsaes_ecb_encrypt_blocks
  1283. .globl bsaes_ecb_decrypt_blocks
  1284. .type bsaes_ecb_decrypt_blocks,\@abi-omnipotent
  1285. .align 16
  1286. bsaes_ecb_decrypt_blocks:
  1287. .cfi_startproc
  1288. mov %rsp, %rax
  1289. .Lecb_dec_prologue:
  1290. push %rbp
  1291. .cfi_push %rbp
  1292. push %rbx
  1293. .cfi_push %rbx
  1294. push %r12
  1295. .cfi_push %r12
  1296. push %r13
  1297. .cfi_push %r13
  1298. push %r14
  1299. .cfi_push %r14
  1300. push %r15
  1301. .cfi_push %r15
  1302. lea -0x48(%rsp),%rsp
  1303. .cfi_adjust_cfa_offset 0x48
  1304. ___
  1305. $code.=<<___ if ($win64);
  1306. lea -0xa0(%rsp), %rsp
  1307. movaps %xmm6, 0x40(%rsp)
  1308. movaps %xmm7, 0x50(%rsp)
  1309. movaps %xmm8, 0x60(%rsp)
  1310. movaps %xmm9, 0x70(%rsp)
  1311. movaps %xmm10, 0x80(%rsp)
  1312. movaps %xmm11, 0x90(%rsp)
  1313. movaps %xmm12, 0xa0(%rsp)
  1314. movaps %xmm13, 0xb0(%rsp)
  1315. movaps %xmm14, 0xc0(%rsp)
  1316. movaps %xmm15, 0xd0(%rsp)
  1317. .Lecb_dec_body:
  1318. ___
  1319. $code.=<<___;
  1320. mov %rsp,%rbp # backup %rsp
  1321. .cfi_def_cfa_register %rbp
  1322. mov 240($arg4),%eax # rounds
  1323. mov $arg1,$inp # backup arguments
  1324. mov $arg2,$out
  1325. mov $arg3,$len
  1326. mov $arg4,$key
  1327. cmp \$8,$arg3
  1328. jb .Lecb_dec_short
  1329. mov %eax,%ebx # backup rounds
  1330. shl \$7,%rax # 128 bytes per inner round key
  1331. sub \$`128-32`,%rax # size of bit-sliced key schedule
  1332. sub %rax,%rsp
  1333. mov %rsp,%rax # pass key schedule
  1334. mov $key,%rcx # pass key
  1335. mov %ebx,%r10d # pass rounds
  1336. call _bsaes_key_convert
  1337. pxor (%rsp),%xmm7 # fix up 0 round key
  1338. movdqa %xmm6,(%rax) # save last round key
  1339. movdqa %xmm7,(%rsp)
  1340. sub \$8,$len
  1341. .Lecb_dec_loop:
  1342. movdqu 0x00($inp), @XMM[0] # load input
  1343. movdqu 0x10($inp), @XMM[1]
  1344. movdqu 0x20($inp), @XMM[2]
  1345. movdqu 0x30($inp), @XMM[3]
  1346. movdqu 0x40($inp), @XMM[4]
  1347. movdqu 0x50($inp), @XMM[5]
  1348. mov %rsp, %rax # pass key schedule
  1349. movdqu 0x60($inp), @XMM[6]
  1350. mov %ebx,%r10d # pass rounds
  1351. movdqu 0x70($inp), @XMM[7]
  1352. lea 0x80($inp), $inp
  1353. call _bsaes_decrypt8
  1354. movdqu @XMM[0], 0x00($out) # write output
  1355. movdqu @XMM[1], 0x10($out)
  1356. movdqu @XMM[6], 0x20($out)
  1357. movdqu @XMM[4], 0x30($out)
  1358. movdqu @XMM[2], 0x40($out)
  1359. movdqu @XMM[7], 0x50($out)
  1360. movdqu @XMM[3], 0x60($out)
  1361. movdqu @XMM[5], 0x70($out)
  1362. lea 0x80($out), $out
  1363. sub \$8,$len
  1364. jnc .Lecb_dec_loop
  1365. add \$8,$len
  1366. jz .Lecb_dec_done
  1367. movdqu 0x00($inp), @XMM[0] # load input
  1368. mov %rsp, %rax # pass key schedule
  1369. mov %ebx,%r10d # pass rounds
  1370. cmp \$2,$len
  1371. jb .Lecb_dec_one
  1372. movdqu 0x10($inp), @XMM[1]
  1373. je .Lecb_dec_two
  1374. movdqu 0x20($inp), @XMM[2]
  1375. cmp \$4,$len
  1376. jb .Lecb_dec_three
  1377. movdqu 0x30($inp), @XMM[3]
  1378. je .Lecb_dec_four
  1379. movdqu 0x40($inp), @XMM[4]
  1380. cmp \$6,$len
  1381. jb .Lecb_dec_five
  1382. movdqu 0x50($inp), @XMM[5]
  1383. je .Lecb_dec_six
  1384. movdqu 0x60($inp), @XMM[6]
  1385. call _bsaes_decrypt8
  1386. movdqu @XMM[0], 0x00($out) # write output
  1387. movdqu @XMM[1], 0x10($out)
  1388. movdqu @XMM[6], 0x20($out)
  1389. movdqu @XMM[4], 0x30($out)
  1390. movdqu @XMM[2], 0x40($out)
  1391. movdqu @XMM[7], 0x50($out)
  1392. movdqu @XMM[3], 0x60($out)
  1393. jmp .Lecb_dec_done
  1394. .align 16
  1395. .Lecb_dec_six:
  1396. call _bsaes_decrypt8
  1397. movdqu @XMM[0], 0x00($out) # write output
  1398. movdqu @XMM[1], 0x10($out)
  1399. movdqu @XMM[6], 0x20($out)
  1400. movdqu @XMM[4], 0x30($out)
  1401. movdqu @XMM[2], 0x40($out)
  1402. movdqu @XMM[7], 0x50($out)
  1403. jmp .Lecb_dec_done
  1404. .align 16
  1405. .Lecb_dec_five:
  1406. call _bsaes_decrypt8
  1407. movdqu @XMM[0], 0x00($out) # write output
  1408. movdqu @XMM[1], 0x10($out)
  1409. movdqu @XMM[6], 0x20($out)
  1410. movdqu @XMM[4], 0x30($out)
  1411. movdqu @XMM[2], 0x40($out)
  1412. jmp .Lecb_dec_done
  1413. .align 16
  1414. .Lecb_dec_four:
  1415. call _bsaes_decrypt8
  1416. movdqu @XMM[0], 0x00($out) # write output
  1417. movdqu @XMM[1], 0x10($out)
  1418. movdqu @XMM[6], 0x20($out)
  1419. movdqu @XMM[4], 0x30($out)
  1420. jmp .Lecb_dec_done
  1421. .align 16
  1422. .Lecb_dec_three:
  1423. call _bsaes_decrypt8
  1424. movdqu @XMM[0], 0x00($out) # write output
  1425. movdqu @XMM[1], 0x10($out)
  1426. movdqu @XMM[6], 0x20($out)
  1427. jmp .Lecb_dec_done
  1428. .align 16
  1429. .Lecb_dec_two:
  1430. call _bsaes_decrypt8
  1431. movdqu @XMM[0], 0x00($out) # write output
  1432. movdqu @XMM[1], 0x10($out)
  1433. jmp .Lecb_dec_done
  1434. .align 16
  1435. .Lecb_dec_one:
  1436. call _bsaes_decrypt8
  1437. movdqu @XMM[0], 0x00($out) # write output
  1438. jmp .Lecb_dec_done
  1439. .align 16
  1440. .Lecb_dec_short:
  1441. lea ($inp), $arg1
  1442. lea ($out), $arg2
  1443. lea ($key), $arg3
  1444. call asm_AES_decrypt
  1445. lea 16($inp), $inp
  1446. lea 16($out), $out
  1447. dec $len
  1448. jnz .Lecb_dec_short
  1449. .Lecb_dec_done:
  1450. lea (%rsp),%rax
  1451. pxor %xmm0, %xmm0
  1452. .Lecb_dec_bzero: # wipe key schedule [if any]
  1453. movdqa %xmm0, 0x00(%rax)
  1454. movdqa %xmm0, 0x10(%rax)
  1455. lea 0x20(%rax), %rax
  1456. cmp %rax, %rbp
  1457. jb .Lecb_dec_bzero
  1458. lea 0x78(%rbp),%rax
  1459. .cfi_def_cfa %rax,8
  1460. ___
  1461. $code.=<<___ if ($win64);
  1462. movaps 0x40(%rbp), %xmm6
  1463. movaps 0x50(%rbp), %xmm7
  1464. movaps 0x60(%rbp), %xmm8
  1465. movaps 0x70(%rbp), %xmm9
  1466. movaps 0x80(%rbp), %xmm10
  1467. movaps 0x90(%rbp), %xmm11
  1468. movaps 0xa0(%rbp), %xmm12
  1469. movaps 0xb0(%rbp), %xmm13
  1470. movaps 0xc0(%rbp), %xmm14
  1471. movaps 0xd0(%rbp), %xmm15
  1472. lea 0xa0(%rax), %rax
  1473. .Lecb_dec_tail:
  1474. ___
  1475. $code.=<<___;
  1476. mov -48(%rax), %r15
  1477. .cfi_restore %r15
  1478. mov -40(%rax), %r14
  1479. .cfi_restore %r14
  1480. mov -32(%rax), %r13
  1481. .cfi_restore %r13
  1482. mov -24(%rax), %r12
  1483. .cfi_restore %r12
  1484. mov -16(%rax), %rbx
  1485. .cfi_restore %rbx
  1486. mov -8(%rax), %rbp
  1487. .cfi_restore %rbp
  1488. lea (%rax), %rsp # restore %rsp
  1489. .cfi_def_cfa_register %rsp
  1490. .Lecb_dec_epilogue:
  1491. ret
  1492. .cfi_endproc
  1493. .size bsaes_ecb_decrypt_blocks,.-bsaes_ecb_decrypt_blocks
  1494. ___
  1495. }
  1496. $code.=<<___;
  1497. .extern asm_AES_cbc_encrypt
  1498. .globl bsaes_cbc_encrypt
  1499. .type bsaes_cbc_encrypt,\@abi-omnipotent
  1500. .align 16
  1501. bsaes_cbc_encrypt:
  1502. .cfi_startproc
  1503. ___
  1504. $code.=<<___ if ($win64);
  1505. mov 48(%rsp),$arg6 # pull direction flag
  1506. ___
  1507. $code.=<<___;
  1508. cmp \$0,$arg6
  1509. jne asm_AES_cbc_encrypt
  1510. cmp \$128,$arg3
  1511. jb asm_AES_cbc_encrypt
  1512. mov %rsp, %rax
  1513. .Lcbc_dec_prologue:
  1514. push %rbp
  1515. .cfi_push %rbp
  1516. push %rbx
  1517. .cfi_push %rbx
  1518. push %r12
  1519. .cfi_push %r12
  1520. push %r13
  1521. .cfi_push %r13
  1522. push %r14
  1523. .cfi_push %r14
  1524. push %r15
  1525. .cfi_push %r15
  1526. lea -0x48(%rsp), %rsp
  1527. .cfi_adjust_cfa_offset 0x48
  1528. ___
  1529. $code.=<<___ if ($win64);
  1530. mov 0xa0(%rsp),$arg5 # pull ivp
  1531. lea -0xa0(%rsp), %rsp
  1532. movaps %xmm6, 0x40(%rsp)
  1533. movaps %xmm7, 0x50(%rsp)
  1534. movaps %xmm8, 0x60(%rsp)
  1535. movaps %xmm9, 0x70(%rsp)
  1536. movaps %xmm10, 0x80(%rsp)
  1537. movaps %xmm11, 0x90(%rsp)
  1538. movaps %xmm12, 0xa0(%rsp)
  1539. movaps %xmm13, 0xb0(%rsp)
  1540. movaps %xmm14, 0xc0(%rsp)
  1541. movaps %xmm15, 0xd0(%rsp)
  1542. .Lcbc_dec_body:
  1543. ___
  1544. $code.=<<___;
  1545. mov %rsp, %rbp # backup %rsp
  1546. .cfi_def_cfa_register %rbp
  1547. mov 240($arg4), %eax # rounds
  1548. mov $arg1, $inp # backup arguments
  1549. mov $arg2, $out
  1550. mov $arg3, $len
  1551. mov $arg4, $key
  1552. mov $arg5, %rbx
  1553. shr \$4, $len # bytes to blocks
  1554. mov %eax, %edx # rounds
  1555. shl \$7, %rax # 128 bytes per inner round key
  1556. sub \$`128-32`, %rax # size of bit-sliced key schedule
  1557. sub %rax, %rsp
  1558. mov %rsp, %rax # pass key schedule
  1559. mov $key, %rcx # pass key
  1560. mov %edx, %r10d # pass rounds
  1561. call _bsaes_key_convert
  1562. pxor (%rsp),%xmm7 # fix up 0 round key
  1563. movdqa %xmm6,(%rax) # save last round key
  1564. movdqa %xmm7,(%rsp)
  1565. movdqu (%rbx), @XMM[15] # load IV
  1566. sub \$8,$len
  1567. .Lcbc_dec_loop:
  1568. movdqu 0x00($inp), @XMM[0] # load input
  1569. movdqu 0x10($inp), @XMM[1]
  1570. movdqu 0x20($inp), @XMM[2]
  1571. movdqu 0x30($inp), @XMM[3]
  1572. movdqu 0x40($inp), @XMM[4]
  1573. movdqu 0x50($inp), @XMM[5]
  1574. mov %rsp, %rax # pass key schedule
  1575. movdqu 0x60($inp), @XMM[6]
  1576. mov %edx,%r10d # pass rounds
  1577. movdqu 0x70($inp), @XMM[7]
  1578. movdqa @XMM[15], 0x20(%rbp) # put aside IV
  1579. call _bsaes_decrypt8
  1580. pxor 0x20(%rbp), @XMM[0] # ^= IV
  1581. movdqu 0x00($inp), @XMM[8] # re-load input
  1582. movdqu 0x10($inp), @XMM[9]
  1583. pxor @XMM[8], @XMM[1]
  1584. movdqu 0x20($inp), @XMM[10]
  1585. pxor @XMM[9], @XMM[6]
  1586. movdqu 0x30($inp), @XMM[11]
  1587. pxor @XMM[10], @XMM[4]
  1588. movdqu 0x40($inp), @XMM[12]
  1589. pxor @XMM[11], @XMM[2]
  1590. movdqu 0x50($inp), @XMM[13]
  1591. pxor @XMM[12], @XMM[7]
  1592. movdqu 0x60($inp), @XMM[14]
  1593. pxor @XMM[13], @XMM[3]
  1594. movdqu 0x70($inp), @XMM[15] # IV
  1595. pxor @XMM[14], @XMM[5]
  1596. movdqu @XMM[0], 0x00($out) # write output
  1597. lea 0x80($inp), $inp
  1598. movdqu @XMM[1], 0x10($out)
  1599. movdqu @XMM[6], 0x20($out)
  1600. movdqu @XMM[4], 0x30($out)
  1601. movdqu @XMM[2], 0x40($out)
  1602. movdqu @XMM[7], 0x50($out)
  1603. movdqu @XMM[3], 0x60($out)
  1604. movdqu @XMM[5], 0x70($out)
  1605. lea 0x80($out), $out
  1606. sub \$8,$len
  1607. jnc .Lcbc_dec_loop
  1608. add \$8,$len
  1609. jz .Lcbc_dec_done
  1610. movdqu 0x00($inp), @XMM[0] # load input
  1611. mov %rsp, %rax # pass key schedule
  1612. mov %edx, %r10d # pass rounds
  1613. cmp \$2,$len
  1614. jb .Lcbc_dec_one
  1615. movdqu 0x10($inp), @XMM[1]
  1616. je .Lcbc_dec_two
  1617. movdqu 0x20($inp), @XMM[2]
  1618. cmp \$4,$len
  1619. jb .Lcbc_dec_three
  1620. movdqu 0x30($inp), @XMM[3]
  1621. je .Lcbc_dec_four
  1622. movdqu 0x40($inp), @XMM[4]
  1623. cmp \$6,$len
  1624. jb .Lcbc_dec_five
  1625. movdqu 0x50($inp), @XMM[5]
  1626. je .Lcbc_dec_six
  1627. movdqu 0x60($inp), @XMM[6]
  1628. movdqa @XMM[15], 0x20(%rbp) # put aside IV
  1629. call _bsaes_decrypt8
  1630. pxor 0x20(%rbp), @XMM[0] # ^= IV
  1631. movdqu 0x00($inp), @XMM[8] # re-load input
  1632. movdqu 0x10($inp), @XMM[9]
  1633. pxor @XMM[8], @XMM[1]
  1634. movdqu 0x20($inp), @XMM[10]
  1635. pxor @XMM[9], @XMM[6]
  1636. movdqu 0x30($inp), @XMM[11]
  1637. pxor @XMM[10], @XMM[4]
  1638. movdqu 0x40($inp), @XMM[12]
  1639. pxor @XMM[11], @XMM[2]
  1640. movdqu 0x50($inp), @XMM[13]
  1641. pxor @XMM[12], @XMM[7]
  1642. movdqu 0x60($inp), @XMM[15] # IV
  1643. pxor @XMM[13], @XMM[3]
  1644. movdqu @XMM[0], 0x00($out) # write output
  1645. movdqu @XMM[1], 0x10($out)
  1646. movdqu @XMM[6], 0x20($out)
  1647. movdqu @XMM[4], 0x30($out)
  1648. movdqu @XMM[2], 0x40($out)
  1649. movdqu @XMM[7], 0x50($out)
  1650. movdqu @XMM[3], 0x60($out)
  1651. jmp .Lcbc_dec_done
  1652. .align 16
  1653. .Lcbc_dec_six:
  1654. movdqa @XMM[15], 0x20(%rbp) # put aside IV
  1655. call _bsaes_decrypt8
  1656. pxor 0x20(%rbp), @XMM[0] # ^= IV
  1657. movdqu 0x00($inp), @XMM[8] # re-load input
  1658. movdqu 0x10($inp), @XMM[9]
  1659. pxor @XMM[8], @XMM[1]
  1660. movdqu 0x20($inp), @XMM[10]
  1661. pxor @XMM[9], @XMM[6]
  1662. movdqu 0x30($inp), @XMM[11]
  1663. pxor @XMM[10], @XMM[4]
  1664. movdqu 0x40($inp), @XMM[12]
  1665. pxor @XMM[11], @XMM[2]
  1666. movdqu 0x50($inp), @XMM[15] # IV
  1667. pxor @XMM[12], @XMM[7]
  1668. movdqu @XMM[0], 0x00($out) # write output
  1669. movdqu @XMM[1], 0x10($out)
  1670. movdqu @XMM[6], 0x20($out)
  1671. movdqu @XMM[4], 0x30($out)
  1672. movdqu @XMM[2], 0x40($out)
  1673. movdqu @XMM[7], 0x50($out)
  1674. jmp .Lcbc_dec_done
  1675. .align 16
  1676. .Lcbc_dec_five:
  1677. movdqa @XMM[15], 0x20(%rbp) # put aside IV
  1678. call _bsaes_decrypt8
  1679. pxor 0x20(%rbp), @XMM[0] # ^= IV
  1680. movdqu 0x00($inp), @XMM[8] # re-load input
  1681. movdqu 0x10($inp), @XMM[9]
  1682. pxor @XMM[8], @XMM[1]
  1683. movdqu 0x20($inp), @XMM[10]
  1684. pxor @XMM[9], @XMM[6]
  1685. movdqu 0x30($inp), @XMM[11]
  1686. pxor @XMM[10], @XMM[4]
  1687. movdqu 0x40($inp), @XMM[15] # IV
  1688. pxor @XMM[11], @XMM[2]
  1689. movdqu @XMM[0], 0x00($out) # write output
  1690. movdqu @XMM[1], 0x10($out)
  1691. movdqu @XMM[6], 0x20($out)
  1692. movdqu @XMM[4], 0x30($out)
  1693. movdqu @XMM[2], 0x40($out)
  1694. jmp .Lcbc_dec_done
  1695. .align 16
  1696. .Lcbc_dec_four:
  1697. movdqa @XMM[15], 0x20(%rbp) # put aside IV
  1698. call _bsaes_decrypt8
  1699. pxor 0x20(%rbp), @XMM[0] # ^= IV
  1700. movdqu 0x00($inp), @XMM[8] # re-load input
  1701. movdqu 0x10($inp), @XMM[9]
  1702. pxor @XMM[8], @XMM[1]
  1703. movdqu 0x20($inp), @XMM[10]
  1704. pxor @XMM[9], @XMM[6]
  1705. movdqu 0x30($inp), @XMM[15] # IV
  1706. pxor @XMM[10], @XMM[4]
  1707. movdqu @XMM[0], 0x00($out) # write output
  1708. movdqu @XMM[1], 0x10($out)
  1709. movdqu @XMM[6], 0x20($out)
  1710. movdqu @XMM[4], 0x30($out)
  1711. jmp .Lcbc_dec_done
  1712. .align 16
  1713. .Lcbc_dec_three:
  1714. movdqa @XMM[15], 0x20(%rbp) # put aside IV
  1715. call _bsaes_decrypt8
  1716. pxor 0x20(%rbp), @XMM[0] # ^= IV
  1717. movdqu 0x00($inp), @XMM[8] # re-load input
  1718. movdqu 0x10($inp), @XMM[9]
  1719. pxor @XMM[8], @XMM[1]
  1720. movdqu 0x20($inp), @XMM[15] # IV
  1721. pxor @XMM[9], @XMM[6]
  1722. movdqu @XMM[0], 0x00($out) # write output
  1723. movdqu @XMM[1], 0x10($out)
  1724. movdqu @XMM[6], 0x20($out)
  1725. jmp .Lcbc_dec_done
  1726. .align 16
  1727. .Lcbc_dec_two:
  1728. movdqa @XMM[15], 0x20(%rbp) # put aside IV
  1729. call _bsaes_decrypt8
  1730. pxor 0x20(%rbp), @XMM[0] # ^= IV
  1731. movdqu 0x00($inp), @XMM[8] # re-load input
  1732. movdqu 0x10($inp), @XMM[15] # IV
  1733. pxor @XMM[8], @XMM[1]
  1734. movdqu @XMM[0], 0x00($out) # write output
  1735. movdqu @XMM[1], 0x10($out)
  1736. jmp .Lcbc_dec_done
  1737. .align 16
  1738. .Lcbc_dec_one:
  1739. lea ($inp), $arg1
  1740. lea 0x20(%rbp), $arg2 # buffer output
  1741. lea ($key), $arg3
  1742. call asm_AES_decrypt # doesn't touch %xmm
  1743. pxor 0x20(%rbp), @XMM[15] # ^= IV
  1744. movdqu @XMM[15], ($out) # write output
  1745. movdqa @XMM[0], @XMM[15] # IV
  1746. .Lcbc_dec_done:
  1747. movdqu @XMM[15], (%rbx) # return IV
  1748. lea (%rsp), %rax
  1749. pxor %xmm0, %xmm0
  1750. .Lcbc_dec_bzero: # wipe key schedule [if any]
  1751. movdqa %xmm0, 0x00(%rax)
  1752. movdqa %xmm0, 0x10(%rax)
  1753. lea 0x20(%rax), %rax
  1754. cmp %rax, %rbp
  1755. ja .Lcbc_dec_bzero
  1756. lea 0x78(%rbp),%rax
  1757. .cfi_def_cfa %rax,8
  1758. ___
  1759. $code.=<<___ if ($win64);
  1760. movaps 0x40(%rbp), %xmm6
  1761. movaps 0x50(%rbp), %xmm7
  1762. movaps 0x60(%rbp), %xmm8
  1763. movaps 0x70(%rbp), %xmm9
  1764. movaps 0x80(%rbp), %xmm10
  1765. movaps 0x90(%rbp), %xmm11
  1766. movaps 0xa0(%rbp), %xmm12
  1767. movaps 0xb0(%rbp), %xmm13
  1768. movaps 0xc0(%rbp), %xmm14
  1769. movaps 0xd0(%rbp), %xmm15
  1770. lea 0xa0(%rax), %rax
  1771. .Lcbc_dec_tail:
  1772. ___
  1773. $code.=<<___;
  1774. mov -48(%rax), %r15
  1775. .cfi_restore %r15
  1776. mov -40(%rax), %r14
  1777. .cfi_restore %r14
  1778. mov -32(%rax), %r13
  1779. .cfi_restore %r13
  1780. mov -24(%rax), %r12
  1781. .cfi_restore %r12
  1782. mov -16(%rax), %rbx
  1783. .cfi_restore %rbx
  1784. mov -8(%rax), %rbp
  1785. .cfi_restore %rbp
  1786. lea (%rax), %rsp # restore %rsp
  1787. .cfi_def_cfa_register %rsp
  1788. .Lcbc_dec_epilogue:
  1789. ret
  1790. .cfi_endproc
  1791. .size bsaes_cbc_encrypt,.-bsaes_cbc_encrypt
  1792. .globl bsaes_ctr32_encrypt_blocks
  1793. .type bsaes_ctr32_encrypt_blocks,\@abi-omnipotent
  1794. .align 16
  1795. bsaes_ctr32_encrypt_blocks:
  1796. .cfi_startproc
  1797. mov %rsp, %rax
  1798. .Lctr_enc_prologue:
  1799. push %rbp
  1800. .cfi_push %rbp
  1801. push %rbx
  1802. .cfi_push %rbx
  1803. push %r12
  1804. .cfi_push %r12
  1805. push %r13
  1806. .cfi_push %r13
  1807. push %r14
  1808. .cfi_push %r14
  1809. push %r15
  1810. .cfi_push %r15
  1811. lea -0x48(%rsp), %rsp
  1812. .cfi_adjust_cfa_offset 0x48
  1813. ___
  1814. $code.=<<___ if ($win64);
  1815. mov 0xa0(%rsp),$arg5 # pull ivp
  1816. lea -0xa0(%rsp), %rsp
  1817. movaps %xmm6, 0x40(%rsp)
  1818. movaps %xmm7, 0x50(%rsp)
  1819. movaps %xmm8, 0x60(%rsp)
  1820. movaps %xmm9, 0x70(%rsp)
  1821. movaps %xmm10, 0x80(%rsp)
  1822. movaps %xmm11, 0x90(%rsp)
  1823. movaps %xmm12, 0xa0(%rsp)
  1824. movaps %xmm13, 0xb0(%rsp)
  1825. movaps %xmm14, 0xc0(%rsp)
  1826. movaps %xmm15, 0xd0(%rsp)
  1827. .Lctr_enc_body:
  1828. ___
  1829. $code.=<<___;
  1830. mov %rsp, %rbp # backup %rsp
  1831. .cfi_def_cfa_register %rbp
  1832. movdqu ($arg5), %xmm0 # load counter
  1833. mov 240($arg4), %eax # rounds
  1834. mov $arg1, $inp # backup arguments
  1835. mov $arg2, $out
  1836. mov $arg3, $len
  1837. mov $arg4, $key
  1838. movdqa %xmm0, 0x20(%rbp) # copy counter
  1839. cmp \$8, $arg3
  1840. jb .Lctr_enc_short
  1841. mov %eax, %ebx # rounds
  1842. shl \$7, %rax # 128 bytes per inner round key
  1843. sub \$`128-32`, %rax # size of bit-sliced key schedule
  1844. sub %rax, %rsp
  1845. mov %rsp, %rax # pass key schedule
  1846. mov $key, %rcx # pass key
  1847. mov %ebx, %r10d # pass rounds
  1848. call _bsaes_key_convert
  1849. pxor %xmm6,%xmm7 # fix up last round key
  1850. movdqa %xmm7,(%rax) # save last round key
  1851. movdqa (%rsp), @XMM[9] # load round0 key
  1852. lea .LADD1(%rip), %r11
  1853. movdqa 0x20(%rbp), @XMM[0] # counter copy
  1854. movdqa -0x20(%r11), @XMM[8] # .LSWPUP
  1855. pshufb @XMM[8], @XMM[9] # byte swap upper part
  1856. pshufb @XMM[8], @XMM[0]
  1857. movdqa @XMM[9], (%rsp) # save adjusted round0 key
  1858. jmp .Lctr_enc_loop
  1859. .align 16
  1860. .Lctr_enc_loop:
  1861. movdqa @XMM[0], 0x20(%rbp) # save counter
  1862. movdqa @XMM[0], @XMM[1] # prepare 8 counter values
  1863. movdqa @XMM[0], @XMM[2]
  1864. paddd 0x00(%r11), @XMM[1] # .LADD1
  1865. movdqa @XMM[0], @XMM[3]
  1866. paddd 0x10(%r11), @XMM[2] # .LADD2
  1867. movdqa @XMM[0], @XMM[4]
  1868. paddd 0x20(%r11), @XMM[3] # .LADD3
  1869. movdqa @XMM[0], @XMM[5]
  1870. paddd 0x30(%r11), @XMM[4] # .LADD4
  1871. movdqa @XMM[0], @XMM[6]
  1872. paddd 0x40(%r11), @XMM[5] # .LADD5
  1873. movdqa @XMM[0], @XMM[7]
  1874. paddd 0x50(%r11), @XMM[6] # .LADD6
  1875. paddd 0x60(%r11), @XMM[7] # .LADD7
  1876. # Borrow prologue from _bsaes_encrypt8 to use the opportunity
  1877. # to flip byte order in 32-bit counter
  1878. movdqa (%rsp), @XMM[9] # round 0 key
  1879. lea 0x10(%rsp), %rax # pass key schedule
  1880. movdqa -0x10(%r11), @XMM[8] # .LSWPUPM0SR
  1881. pxor @XMM[9], @XMM[0] # xor with round0 key
  1882. pxor @XMM[9], @XMM[1]
  1883. pxor @XMM[9], @XMM[2]
  1884. pxor @XMM[9], @XMM[3]
  1885. pshufb @XMM[8], @XMM[0]
  1886. pshufb @XMM[8], @XMM[1]
  1887. pxor @XMM[9], @XMM[4]
  1888. pxor @XMM[9], @XMM[5]
  1889. pshufb @XMM[8], @XMM[2]
  1890. pshufb @XMM[8], @XMM[3]
  1891. pxor @XMM[9], @XMM[6]
  1892. pxor @XMM[9], @XMM[7]
  1893. pshufb @XMM[8], @XMM[4]
  1894. pshufb @XMM[8], @XMM[5]
  1895. pshufb @XMM[8], @XMM[6]
  1896. pshufb @XMM[8], @XMM[7]
  1897. lea .LBS0(%rip), %r11 # constants table
  1898. mov %ebx,%r10d # pass rounds
  1899. call _bsaes_encrypt8_bitslice
  1900. sub \$8,$len
  1901. jc .Lctr_enc_loop_done
  1902. movdqu 0x00($inp), @XMM[8] # load input
  1903. movdqu 0x10($inp), @XMM[9]
  1904. movdqu 0x20($inp), @XMM[10]
  1905. movdqu 0x30($inp), @XMM[11]
  1906. movdqu 0x40($inp), @XMM[12]
  1907. movdqu 0x50($inp), @XMM[13]
  1908. movdqu 0x60($inp), @XMM[14]
  1909. movdqu 0x70($inp), @XMM[15]
  1910. lea 0x80($inp),$inp
  1911. pxor @XMM[0], @XMM[8]
  1912. movdqa 0x20(%rbp), @XMM[0] # load counter
  1913. pxor @XMM[9], @XMM[1]
  1914. movdqu @XMM[8], 0x00($out) # write output
  1915. pxor @XMM[10], @XMM[4]
  1916. movdqu @XMM[1], 0x10($out)
  1917. pxor @XMM[11], @XMM[6]
  1918. movdqu @XMM[4], 0x20($out)
  1919. pxor @XMM[12], @XMM[3]
  1920. movdqu @XMM[6], 0x30($out)
  1921. pxor @XMM[13], @XMM[7]
  1922. movdqu @XMM[3], 0x40($out)
  1923. pxor @XMM[14], @XMM[2]
  1924. movdqu @XMM[7], 0x50($out)
  1925. pxor @XMM[15], @XMM[5]
  1926. movdqu @XMM[2], 0x60($out)
  1927. lea .LADD1(%rip), %r11
  1928. movdqu @XMM[5], 0x70($out)
  1929. lea 0x80($out), $out
  1930. paddd 0x70(%r11), @XMM[0] # .LADD8
  1931. jnz .Lctr_enc_loop
  1932. jmp .Lctr_enc_done
  1933. .align 16
  1934. .Lctr_enc_loop_done:
  1935. add \$8, $len
  1936. movdqu 0x00($inp), @XMM[8] # load input
  1937. pxor @XMM[8], @XMM[0]
  1938. movdqu @XMM[0], 0x00($out) # write output
  1939. cmp \$2,$len
  1940. jb .Lctr_enc_done
  1941. movdqu 0x10($inp), @XMM[9]
  1942. pxor @XMM[9], @XMM[1]
  1943. movdqu @XMM[1], 0x10($out)
  1944. je .Lctr_enc_done
  1945. movdqu 0x20($inp), @XMM[10]
  1946. pxor @XMM[10], @XMM[4]
  1947. movdqu @XMM[4], 0x20($out)
  1948. cmp \$4,$len
  1949. jb .Lctr_enc_done
  1950. movdqu 0x30($inp), @XMM[11]
  1951. pxor @XMM[11], @XMM[6]
  1952. movdqu @XMM[6], 0x30($out)
  1953. je .Lctr_enc_done
  1954. movdqu 0x40($inp), @XMM[12]
  1955. pxor @XMM[12], @XMM[3]
  1956. movdqu @XMM[3], 0x40($out)
  1957. cmp \$6,$len
  1958. jb .Lctr_enc_done
  1959. movdqu 0x50($inp), @XMM[13]
  1960. pxor @XMM[13], @XMM[7]
  1961. movdqu @XMM[7], 0x50($out)
  1962. je .Lctr_enc_done
  1963. movdqu 0x60($inp), @XMM[14]
  1964. pxor @XMM[14], @XMM[2]
  1965. movdqu @XMM[2], 0x60($out)
  1966. jmp .Lctr_enc_done
  1967. .align 16
  1968. .Lctr_enc_short:
  1969. lea 0x20(%rbp), $arg1
  1970. lea 0x30(%rbp), $arg2
  1971. lea ($key), $arg3
  1972. call asm_AES_encrypt
  1973. movdqu ($inp), @XMM[1]
  1974. lea 16($inp), $inp
  1975. mov 0x2c(%rbp), %eax # load 32-bit counter
  1976. bswap %eax
  1977. pxor 0x30(%rbp), @XMM[1]
  1978. inc %eax # increment
  1979. movdqu @XMM[1], ($out)
  1980. bswap %eax
  1981. lea 16($out), $out
  1982. mov %eax, 0x2c(%rsp) # save 32-bit counter
  1983. dec $len
  1984. jnz .Lctr_enc_short
  1985. .Lctr_enc_done:
  1986. lea (%rsp), %rax
  1987. pxor %xmm0, %xmm0
  1988. .Lctr_enc_bzero: # wipe key schedule [if any]
  1989. movdqa %xmm0, 0x00(%rax)
  1990. movdqa %xmm0, 0x10(%rax)
  1991. lea 0x20(%rax), %rax
  1992. cmp %rax, %rbp
  1993. ja .Lctr_enc_bzero
  1994. lea 0x78(%rbp),%rax
  1995. .cfi_def_cfa %rax,8
  1996. ___
  1997. $code.=<<___ if ($win64);
  1998. movaps 0x40(%rbp), %xmm6
  1999. movaps 0x50(%rbp), %xmm7
  2000. movaps 0x60(%rbp), %xmm8
  2001. movaps 0x70(%rbp), %xmm9
  2002. movaps 0x80(%rbp), %xmm10
  2003. movaps 0x90(%rbp), %xmm11
  2004. movaps 0xa0(%rbp), %xmm12
  2005. movaps 0xb0(%rbp), %xmm13
  2006. movaps 0xc0(%rbp), %xmm14
  2007. movaps 0xd0(%rbp), %xmm15
  2008. lea 0xa0(%rax), %rax
  2009. .Lctr_enc_tail:
  2010. ___
  2011. $code.=<<___;
  2012. mov -48(%rax), %r15
  2013. .cfi_restore %r15
  2014. mov -40(%rax), %r14
  2015. .cfi_restore %r14
  2016. mov -32(%rax), %r13
  2017. .cfi_restore %r13
  2018. mov -24(%rax), %r12
  2019. .cfi_restore %r12
  2020. mov -16(%rax), %rbx
  2021. .cfi_restore %rbx
  2022. mov -8(%rax), %rbp
  2023. .cfi_restore %rbp
  2024. lea (%rax), %rsp # restore %rsp
  2025. .cfi_def_cfa_register %rsp
  2026. .Lctr_enc_epilogue:
  2027. ret
  2028. .cfi_endproc
  2029. .size bsaes_ctr32_encrypt_blocks,.-bsaes_ctr32_encrypt_blocks
  2030. ___
  2031. ######################################################################
  2032. # void bsaes_xts_[en|de]crypt(const char *inp,char *out,size_t len,
  2033. # const AES_KEY *key1, const AES_KEY *key2,
  2034. # const unsigned char iv[16]);
  2035. #
  2036. my ($twmask,$twres,$twtmp)=@XMM[13..15];
  2037. $arg6=~s/d$//;
  2038. $code.=<<___;
  2039. .globl bsaes_xts_encrypt
  2040. .type bsaes_xts_encrypt,\@abi-omnipotent
  2041. .align 16
  2042. bsaes_xts_encrypt:
  2043. .cfi_startproc
  2044. mov %rsp, %rax
  2045. .Lxts_enc_prologue:
  2046. push %rbp
  2047. .cfi_push %rbp
  2048. push %rbx
  2049. .cfi_push %rbx
  2050. push %r12
  2051. .cfi_push %r12
  2052. push %r13
  2053. .cfi_push %r13
  2054. push %r14
  2055. .cfi_push %r14
  2056. push %r15
  2057. .cfi_push %r15
  2058. lea -0x48(%rsp), %rsp
  2059. .cfi_adjust_cfa_offset 0x48
  2060. ___
  2061. $code.=<<___ if ($win64);
  2062. mov 0xa0(%rsp),$arg5 # pull key2
  2063. mov 0xa8(%rsp),$arg6 # pull ivp
  2064. lea -0xa0(%rsp), %rsp
  2065. movaps %xmm6, 0x40(%rsp)
  2066. movaps %xmm7, 0x50(%rsp)
  2067. movaps %xmm8, 0x60(%rsp)
  2068. movaps %xmm9, 0x70(%rsp)
  2069. movaps %xmm10, 0x80(%rsp)
  2070. movaps %xmm11, 0x90(%rsp)
  2071. movaps %xmm12, 0xa0(%rsp)
  2072. movaps %xmm13, 0xb0(%rsp)
  2073. movaps %xmm14, 0xc0(%rsp)
  2074. movaps %xmm15, 0xd0(%rsp)
  2075. .Lxts_enc_body:
  2076. ___
  2077. $code.=<<___;
  2078. mov %rsp, %rbp # backup %rsp
  2079. .cfi_def_cfa_register %rbp
  2080. mov $arg1, $inp # backup arguments
  2081. mov $arg2, $out
  2082. mov $arg3, $len
  2083. mov $arg4, $key
  2084. lea ($arg6), $arg1
  2085. lea 0x20(%rbp), $arg2
  2086. lea ($arg5), $arg3
  2087. call asm_AES_encrypt # generate initial tweak
  2088. mov 240($key), %eax # rounds
  2089. mov $len, %rbx # backup $len
  2090. mov %eax, %edx # rounds
  2091. shl \$7, %rax # 128 bytes per inner round key
  2092. sub \$`128-32`, %rax # size of bit-sliced key schedule
  2093. sub %rax, %rsp
  2094. mov %rsp, %rax # pass key schedule
  2095. mov $key, %rcx # pass key
  2096. mov %edx, %r10d # pass rounds
  2097. call _bsaes_key_convert
  2098. pxor %xmm6, %xmm7 # fix up last round key
  2099. movdqa %xmm7, (%rax) # save last round key
  2100. and \$-16, $len
  2101. sub \$0x80, %rsp # place for tweak[8]
  2102. movdqa 0x20(%rbp), @XMM[7] # initial tweak
  2103. pxor $twtmp, $twtmp
  2104. movdqa .Lxts_magic(%rip), $twmask
  2105. pcmpgtd @XMM[7], $twtmp # broadcast upper bits
  2106. sub \$0x80, $len
  2107. jc .Lxts_enc_short
  2108. jmp .Lxts_enc_loop
  2109. .align 16
  2110. .Lxts_enc_loop:
  2111. ___
  2112. for ($i=0;$i<7;$i++) {
  2113. $code.=<<___;
  2114. pshufd \$0x13, $twtmp, $twres
  2115. pxor $twtmp, $twtmp
  2116. movdqa @XMM[7], @XMM[$i]
  2117. movdqa @XMM[7], `0x10*$i`(%rsp)# save tweak[$i]
  2118. paddq @XMM[7], @XMM[7] # psllq 1,$tweak
  2119. pand $twmask, $twres # isolate carry and residue
  2120. pcmpgtd @XMM[7], $twtmp # broadcast upper bits
  2121. pxor $twres, @XMM[7]
  2122. ___
  2123. $code.=<<___ if ($i>=1);
  2124. movdqu `0x10*($i-1)`($inp), @XMM[8+$i-1]
  2125. ___
  2126. $code.=<<___ if ($i>=2);
  2127. pxor @XMM[8+$i-2], @XMM[$i-2]# input[] ^ tweak[]
  2128. ___
  2129. }
  2130. $code.=<<___;
  2131. movdqu 0x60($inp), @XMM[8+6]
  2132. pxor @XMM[8+5], @XMM[5]
  2133. movdqu 0x70($inp), @XMM[8+7]
  2134. lea 0x80($inp), $inp
  2135. movdqa @XMM[7], 0x70(%rsp)
  2136. pxor @XMM[8+6], @XMM[6]
  2137. lea 0x80(%rsp), %rax # pass key schedule
  2138. pxor @XMM[8+7], @XMM[7]
  2139. mov %edx, %r10d # pass rounds
  2140. call _bsaes_encrypt8
  2141. pxor 0x00(%rsp), @XMM[0] # ^= tweak[]
  2142. pxor 0x10(%rsp), @XMM[1]
  2143. movdqu @XMM[0], 0x00($out) # write output
  2144. pxor 0x20(%rsp), @XMM[4]
  2145. movdqu @XMM[1], 0x10($out)
  2146. pxor 0x30(%rsp), @XMM[6]
  2147. movdqu @XMM[4], 0x20($out)
  2148. pxor 0x40(%rsp), @XMM[3]
  2149. movdqu @XMM[6], 0x30($out)
  2150. pxor 0x50(%rsp), @XMM[7]
  2151. movdqu @XMM[3], 0x40($out)
  2152. pxor 0x60(%rsp), @XMM[2]
  2153. movdqu @XMM[7], 0x50($out)
  2154. pxor 0x70(%rsp), @XMM[5]
  2155. movdqu @XMM[2], 0x60($out)
  2156. movdqu @XMM[5], 0x70($out)
  2157. lea 0x80($out), $out
  2158. movdqa 0x70(%rsp), @XMM[7] # prepare next iteration tweak
  2159. pxor $twtmp, $twtmp
  2160. movdqa .Lxts_magic(%rip), $twmask
  2161. pcmpgtd @XMM[7], $twtmp
  2162. pshufd \$0x13, $twtmp, $twres
  2163. pxor $twtmp, $twtmp
  2164. paddq @XMM[7], @XMM[7] # psllq 1,$tweak
  2165. pand $twmask, $twres # isolate carry and residue
  2166. pcmpgtd @XMM[7], $twtmp # broadcast upper bits
  2167. pxor $twres, @XMM[7]
  2168. sub \$0x80,$len
  2169. jnc .Lxts_enc_loop
  2170. .Lxts_enc_short:
  2171. add \$0x80, $len
  2172. jz .Lxts_enc_done
  2173. ___
  2174. for ($i=0;$i<7;$i++) {
  2175. $code.=<<___;
  2176. pshufd \$0x13, $twtmp, $twres
  2177. pxor $twtmp, $twtmp
  2178. movdqa @XMM[7], @XMM[$i]
  2179. movdqa @XMM[7], `0x10*$i`(%rsp)# save tweak[$i]
  2180. paddq @XMM[7], @XMM[7] # psllq 1,$tweak
  2181. pand $twmask, $twres # isolate carry and residue
  2182. pcmpgtd @XMM[7], $twtmp # broadcast upper bits
  2183. pxor $twres, @XMM[7]
  2184. ___
  2185. $code.=<<___ if ($i>=1);
  2186. movdqu `0x10*($i-1)`($inp), @XMM[8+$i-1]
  2187. cmp \$`0x10*$i`,$len
  2188. je .Lxts_enc_$i
  2189. ___
  2190. $code.=<<___ if ($i>=2);
  2191. pxor @XMM[8+$i-2], @XMM[$i-2]# input[] ^ tweak[]
  2192. ___
  2193. }
  2194. $code.=<<___;
  2195. movdqu 0x60($inp), @XMM[8+6]
  2196. pxor @XMM[8+5], @XMM[5]
  2197. movdqa @XMM[7], 0x70(%rsp)
  2198. lea 0x70($inp), $inp
  2199. pxor @XMM[8+6], @XMM[6]
  2200. lea 0x80(%rsp), %rax # pass key schedule
  2201. mov %edx, %r10d # pass rounds
  2202. call _bsaes_encrypt8
  2203. pxor 0x00(%rsp), @XMM[0] # ^= tweak[]
  2204. pxor 0x10(%rsp), @XMM[1]
  2205. movdqu @XMM[0], 0x00($out) # write output
  2206. pxor 0x20(%rsp), @XMM[4]
  2207. movdqu @XMM[1], 0x10($out)
  2208. pxor 0x30(%rsp), @XMM[6]
  2209. movdqu @XMM[4], 0x20($out)
  2210. pxor 0x40(%rsp), @XMM[3]
  2211. movdqu @XMM[6], 0x30($out)
  2212. pxor 0x50(%rsp), @XMM[7]
  2213. movdqu @XMM[3], 0x40($out)
  2214. pxor 0x60(%rsp), @XMM[2]
  2215. movdqu @XMM[7], 0x50($out)
  2216. movdqu @XMM[2], 0x60($out)
  2217. lea 0x70($out), $out
  2218. movdqa 0x70(%rsp), @XMM[7] # next iteration tweak
  2219. jmp .Lxts_enc_done
  2220. .align 16
  2221. .Lxts_enc_6:
  2222. pxor @XMM[8+4], @XMM[4]
  2223. lea 0x60($inp), $inp
  2224. pxor @XMM[8+5], @XMM[5]
  2225. lea 0x80(%rsp), %rax # pass key schedule
  2226. mov %edx, %r10d # pass rounds
  2227. call _bsaes_encrypt8
  2228. pxor 0x00(%rsp), @XMM[0] # ^= tweak[]
  2229. pxor 0x10(%rsp), @XMM[1]
  2230. movdqu @XMM[0], 0x00($out) # write output
  2231. pxor 0x20(%rsp), @XMM[4]
  2232. movdqu @XMM[1], 0x10($out)
  2233. pxor 0x30(%rsp), @XMM[6]
  2234. movdqu @XMM[4], 0x20($out)
  2235. pxor 0x40(%rsp), @XMM[3]
  2236. movdqu @XMM[6], 0x30($out)
  2237. pxor 0x50(%rsp), @XMM[7]
  2238. movdqu @XMM[3], 0x40($out)
  2239. movdqu @XMM[7], 0x50($out)
  2240. lea 0x60($out), $out
  2241. movdqa 0x60(%rsp), @XMM[7] # next iteration tweak
  2242. jmp .Lxts_enc_done
  2243. .align 16
  2244. .Lxts_enc_5:
  2245. pxor @XMM[8+3], @XMM[3]
  2246. lea 0x50($inp), $inp
  2247. pxor @XMM[8+4], @XMM[4]
  2248. lea 0x80(%rsp), %rax # pass key schedule
  2249. mov %edx, %r10d # pass rounds
  2250. call _bsaes_encrypt8
  2251. pxor 0x00(%rsp), @XMM[0] # ^= tweak[]
  2252. pxor 0x10(%rsp), @XMM[1]
  2253. movdqu @XMM[0], 0x00($out) # write output
  2254. pxor 0x20(%rsp), @XMM[4]
  2255. movdqu @XMM[1], 0x10($out)
  2256. pxor 0x30(%rsp), @XMM[6]
  2257. movdqu @XMM[4], 0x20($out)
  2258. pxor 0x40(%rsp), @XMM[3]
  2259. movdqu @XMM[6], 0x30($out)
  2260. movdqu @XMM[3], 0x40($out)
  2261. lea 0x50($out), $out
  2262. movdqa 0x50(%rsp), @XMM[7] # next iteration tweak
  2263. jmp .Lxts_enc_done
  2264. .align 16
  2265. .Lxts_enc_4:
  2266. pxor @XMM[8+2], @XMM[2]
  2267. lea 0x40($inp), $inp
  2268. pxor @XMM[8+3], @XMM[3]
  2269. lea 0x80(%rsp), %rax # pass key schedule
  2270. mov %edx, %r10d # pass rounds
  2271. call _bsaes_encrypt8
  2272. pxor 0x00(%rsp), @XMM[0] # ^= tweak[]
  2273. pxor 0x10(%rsp), @XMM[1]
  2274. movdqu @XMM[0], 0x00($out) # write output
  2275. pxor 0x20(%rsp), @XMM[4]
  2276. movdqu @XMM[1], 0x10($out)
  2277. pxor 0x30(%rsp), @XMM[6]
  2278. movdqu @XMM[4], 0x20($out)
  2279. movdqu @XMM[6], 0x30($out)
  2280. lea 0x40($out), $out
  2281. movdqa 0x40(%rsp), @XMM[7] # next iteration tweak
  2282. jmp .Lxts_enc_done
  2283. .align 16
  2284. .Lxts_enc_3:
  2285. pxor @XMM[8+1], @XMM[1]
  2286. lea 0x30($inp), $inp
  2287. pxor @XMM[8+2], @XMM[2]
  2288. lea 0x80(%rsp), %rax # pass key schedule
  2289. mov %edx, %r10d # pass rounds
  2290. call _bsaes_encrypt8
  2291. pxor 0x00(%rsp), @XMM[0] # ^= tweak[]
  2292. pxor 0x10(%rsp), @XMM[1]
  2293. movdqu @XMM[0], 0x00($out) # write output
  2294. pxor 0x20(%rsp), @XMM[4]
  2295. movdqu @XMM[1], 0x10($out)
  2296. movdqu @XMM[4], 0x20($out)
  2297. lea 0x30($out), $out
  2298. movdqa 0x30(%rsp), @XMM[7] # next iteration tweak
  2299. jmp .Lxts_enc_done
  2300. .align 16
  2301. .Lxts_enc_2:
  2302. pxor @XMM[8+0], @XMM[0]
  2303. lea 0x20($inp), $inp
  2304. pxor @XMM[8+1], @XMM[1]
  2305. lea 0x80(%rsp), %rax # pass key schedule
  2306. mov %edx, %r10d # pass rounds
  2307. call _bsaes_encrypt8
  2308. pxor 0x00(%rsp), @XMM[0] # ^= tweak[]
  2309. pxor 0x10(%rsp), @XMM[1]
  2310. movdqu @XMM[0], 0x00($out) # write output
  2311. movdqu @XMM[1], 0x10($out)
  2312. lea 0x20($out), $out
  2313. movdqa 0x20(%rsp), @XMM[7] # next iteration tweak
  2314. jmp .Lxts_enc_done
  2315. .align 16
  2316. .Lxts_enc_1:
  2317. pxor @XMM[0], @XMM[8]
  2318. lea 0x10($inp), $inp
  2319. movdqa @XMM[8], 0x20(%rbp)
  2320. lea 0x20(%rbp), $arg1
  2321. lea 0x20(%rbp), $arg2
  2322. lea ($key), $arg3
  2323. call asm_AES_encrypt # doesn't touch %xmm
  2324. pxor 0x20(%rbp), @XMM[0] # ^= tweak[]
  2325. #pxor @XMM[8], @XMM[0]
  2326. #lea 0x80(%rsp), %rax # pass key schedule
  2327. #mov %edx, %r10d # pass rounds
  2328. #call _bsaes_encrypt8
  2329. #pxor 0x00(%rsp), @XMM[0] # ^= tweak[]
  2330. movdqu @XMM[0], 0x00($out) # write output
  2331. lea 0x10($out), $out
  2332. movdqa 0x10(%rsp), @XMM[7] # next iteration tweak
  2333. .Lxts_enc_done:
  2334. and \$15, %ebx
  2335. jz .Lxts_enc_ret
  2336. mov $out, %rdx
  2337. .Lxts_enc_steal:
  2338. movzb ($inp), %eax
  2339. movzb -16(%rdx), %ecx
  2340. lea 1($inp), $inp
  2341. mov %al, -16(%rdx)
  2342. mov %cl, 0(%rdx)
  2343. lea 1(%rdx), %rdx
  2344. sub \$1,%ebx
  2345. jnz .Lxts_enc_steal
  2346. movdqu -16($out), @XMM[0]
  2347. lea 0x20(%rbp), $arg1
  2348. pxor @XMM[7], @XMM[0]
  2349. lea 0x20(%rbp), $arg2
  2350. movdqa @XMM[0], 0x20(%rbp)
  2351. lea ($key), $arg3
  2352. call asm_AES_encrypt # doesn't touch %xmm
  2353. pxor 0x20(%rbp), @XMM[7]
  2354. movdqu @XMM[7], -16($out)
  2355. .Lxts_enc_ret:
  2356. lea (%rsp), %rax
  2357. pxor %xmm0, %xmm0
  2358. .Lxts_enc_bzero: # wipe key schedule [if any]
  2359. movdqa %xmm0, 0x00(%rax)
  2360. movdqa %xmm0, 0x10(%rax)
  2361. lea 0x20(%rax), %rax
  2362. cmp %rax, %rbp
  2363. ja .Lxts_enc_bzero
  2364. lea 0x78(%rbp),%rax
  2365. .cfi_def_cfa %rax,8
  2366. ___
  2367. $code.=<<___ if ($win64);
  2368. movaps 0x40(%rbp), %xmm6
  2369. movaps 0x50(%rbp), %xmm7
  2370. movaps 0x60(%rbp), %xmm8
  2371. movaps 0x70(%rbp), %xmm9
  2372. movaps 0x80(%rbp), %xmm10
  2373. movaps 0x90(%rbp), %xmm11
  2374. movaps 0xa0(%rbp), %xmm12
  2375. movaps 0xb0(%rbp), %xmm13
  2376. movaps 0xc0(%rbp), %xmm14
  2377. movaps 0xd0(%rbp), %xmm15
  2378. lea 0xa0(%rax), %rax
  2379. .Lxts_enc_tail:
  2380. ___
  2381. $code.=<<___;
  2382. mov -48(%rax), %r15
  2383. .cfi_restore %r15
  2384. mov -40(%rax), %r14
  2385. .cfi_restore %r14
  2386. mov -32(%rax), %r13
  2387. .cfi_restore %r13
  2388. mov -24(%rax), %r12
  2389. .cfi_restore %r12
  2390. mov -16(%rax), %rbx
  2391. .cfi_restore %rbx
  2392. mov -8(%rax), %rbp
  2393. .cfi_restore %rbp
  2394. lea (%rax), %rsp # restore %rsp
  2395. .cfi_def_cfa_register %rsp
  2396. .Lxts_enc_epilogue:
  2397. ret
  2398. .cfi_endproc
  2399. .size bsaes_xts_encrypt,.-bsaes_xts_encrypt
  2400. .globl bsaes_xts_decrypt
  2401. .type bsaes_xts_decrypt,\@abi-omnipotent
  2402. .align 16
  2403. bsaes_xts_decrypt:
  2404. .cfi_startproc
  2405. mov %rsp, %rax
  2406. .Lxts_dec_prologue:
  2407. push %rbp
  2408. .cfi_push %rbp
  2409. push %rbx
  2410. .cfi_push %rbx
  2411. push %r12
  2412. .cfi_push %r12
  2413. push %r13
  2414. .cfi_push %r13
  2415. push %r14
  2416. .cfi_push %r14
  2417. push %r15
  2418. .cfi_push %r15
  2419. lea -0x48(%rsp), %rsp
  2420. .cfi_adjust_cfa_offset 0x48
  2421. ___
  2422. $code.=<<___ if ($win64);
  2423. mov 0xa0(%rsp),$arg5 # pull key2
  2424. mov 0xa8(%rsp),$arg6 # pull ivp
  2425. lea -0xa0(%rsp), %rsp
  2426. movaps %xmm6, 0x40(%rsp)
  2427. movaps %xmm7, 0x50(%rsp)
  2428. movaps %xmm8, 0x60(%rsp)
  2429. movaps %xmm9, 0x70(%rsp)
  2430. movaps %xmm10, 0x80(%rsp)
  2431. movaps %xmm11, 0x90(%rsp)
  2432. movaps %xmm12, 0xa0(%rsp)
  2433. movaps %xmm13, 0xb0(%rsp)
  2434. movaps %xmm14, 0xc0(%rsp)
  2435. movaps %xmm15, 0xd0(%rsp)
  2436. .Lxts_dec_body:
  2437. ___
  2438. $code.=<<___;
  2439. mov %rsp, %rbp # backup %rsp
  2440. mov $arg1, $inp # backup arguments
  2441. mov $arg2, $out
  2442. mov $arg3, $len
  2443. mov $arg4, $key
  2444. lea ($arg6), $arg1
  2445. lea 0x20(%rbp), $arg2
  2446. lea ($arg5), $arg3
  2447. call asm_AES_encrypt # generate initial tweak
  2448. mov 240($key), %eax # rounds
  2449. mov $len, %rbx # backup $len
  2450. mov %eax, %edx # rounds
  2451. shl \$7, %rax # 128 bytes per inner round key
  2452. sub \$`128-32`, %rax # size of bit-sliced key schedule
  2453. sub %rax, %rsp
  2454. mov %rsp, %rax # pass key schedule
  2455. mov $key, %rcx # pass key
  2456. mov %edx, %r10d # pass rounds
  2457. call _bsaes_key_convert
  2458. pxor (%rsp), %xmm7 # fix up round 0 key
  2459. movdqa %xmm6, (%rax) # save last round key
  2460. movdqa %xmm7, (%rsp)
  2461. xor %eax, %eax # if ($len%16) len-=16;
  2462. and \$-16, $len
  2463. test \$15, %ebx
  2464. setnz %al
  2465. shl \$4, %rax
  2466. sub %rax, $len
  2467. sub \$0x80, %rsp # place for tweak[8]
  2468. movdqa 0x20(%rbp), @XMM[7] # initial tweak
  2469. pxor $twtmp, $twtmp
  2470. movdqa .Lxts_magic(%rip), $twmask
  2471. pcmpgtd @XMM[7], $twtmp # broadcast upper bits
  2472. sub \$0x80, $len
  2473. jc .Lxts_dec_short
  2474. jmp .Lxts_dec_loop
  2475. .align 16
  2476. .Lxts_dec_loop:
  2477. ___
  2478. for ($i=0;$i<7;$i++) {
  2479. $code.=<<___;
  2480. pshufd \$0x13, $twtmp, $twres
  2481. pxor $twtmp, $twtmp
  2482. movdqa @XMM[7], @XMM[$i]
  2483. movdqa @XMM[7], `0x10*$i`(%rsp)# save tweak[$i]
  2484. paddq @XMM[7], @XMM[7] # psllq 1,$tweak
  2485. pand $twmask, $twres # isolate carry and residue
  2486. pcmpgtd @XMM[7], $twtmp # broadcast upper bits
  2487. pxor $twres, @XMM[7]
  2488. ___
  2489. $code.=<<___ if ($i>=1);
  2490. movdqu `0x10*($i-1)`($inp), @XMM[8+$i-1]
  2491. ___
  2492. $code.=<<___ if ($i>=2);
  2493. pxor @XMM[8+$i-2], @XMM[$i-2]# input[] ^ tweak[]
  2494. ___
  2495. }
  2496. $code.=<<___;
  2497. movdqu 0x60($inp), @XMM[8+6]
  2498. pxor @XMM[8+5], @XMM[5]
  2499. movdqu 0x70($inp), @XMM[8+7]
  2500. lea 0x80($inp), $inp
  2501. movdqa @XMM[7], 0x70(%rsp)
  2502. pxor @XMM[8+6], @XMM[6]
  2503. lea 0x80(%rsp), %rax # pass key schedule
  2504. pxor @XMM[8+7], @XMM[7]
  2505. mov %edx, %r10d # pass rounds
  2506. call _bsaes_decrypt8
  2507. pxor 0x00(%rsp), @XMM[0] # ^= tweak[]
  2508. pxor 0x10(%rsp), @XMM[1]
  2509. movdqu @XMM[0], 0x00($out) # write output
  2510. pxor 0x20(%rsp), @XMM[6]
  2511. movdqu @XMM[1], 0x10($out)
  2512. pxor 0x30(%rsp), @XMM[4]
  2513. movdqu @XMM[6], 0x20($out)
  2514. pxor 0x40(%rsp), @XMM[2]
  2515. movdqu @XMM[4], 0x30($out)
  2516. pxor 0x50(%rsp), @XMM[7]
  2517. movdqu @XMM[2], 0x40($out)
  2518. pxor 0x60(%rsp), @XMM[3]
  2519. movdqu @XMM[7], 0x50($out)
  2520. pxor 0x70(%rsp), @XMM[5]
  2521. movdqu @XMM[3], 0x60($out)
  2522. movdqu @XMM[5], 0x70($out)
  2523. lea 0x80($out), $out
  2524. movdqa 0x70(%rsp), @XMM[7] # prepare next iteration tweak
  2525. pxor $twtmp, $twtmp
  2526. movdqa .Lxts_magic(%rip), $twmask
  2527. pcmpgtd @XMM[7], $twtmp
  2528. pshufd \$0x13, $twtmp, $twres
  2529. pxor $twtmp, $twtmp
  2530. paddq @XMM[7], @XMM[7] # psllq 1,$tweak
  2531. pand $twmask, $twres # isolate carry and residue
  2532. pcmpgtd @XMM[7], $twtmp # broadcast upper bits
  2533. pxor $twres, @XMM[7]
  2534. sub \$0x80,$len
  2535. jnc .Lxts_dec_loop
  2536. .Lxts_dec_short:
  2537. add \$0x80, $len
  2538. jz .Lxts_dec_done
  2539. ___
  2540. for ($i=0;$i<7;$i++) {
  2541. $code.=<<___;
  2542. pshufd \$0x13, $twtmp, $twres
  2543. pxor $twtmp, $twtmp
  2544. movdqa @XMM[7], @XMM[$i]
  2545. movdqa @XMM[7], `0x10*$i`(%rsp)# save tweak[$i]
  2546. paddq @XMM[7], @XMM[7] # psllq 1,$tweak
  2547. pand $twmask, $twres # isolate carry and residue
  2548. pcmpgtd @XMM[7], $twtmp # broadcast upper bits
  2549. pxor $twres, @XMM[7]
  2550. ___
  2551. $code.=<<___ if ($i>=1);
  2552. movdqu `0x10*($i-1)`($inp), @XMM[8+$i-1]
  2553. cmp \$`0x10*$i`,$len
  2554. je .Lxts_dec_$i
  2555. ___
  2556. $code.=<<___ if ($i>=2);
  2557. pxor @XMM[8+$i-2], @XMM[$i-2]# input[] ^ tweak[]
  2558. ___
  2559. }
  2560. $code.=<<___;
  2561. movdqu 0x60($inp), @XMM[8+6]
  2562. pxor @XMM[8+5], @XMM[5]
  2563. movdqa @XMM[7], 0x70(%rsp)
  2564. lea 0x70($inp), $inp
  2565. pxor @XMM[8+6], @XMM[6]
  2566. lea 0x80(%rsp), %rax # pass key schedule
  2567. mov %edx, %r10d # pass rounds
  2568. call _bsaes_decrypt8
  2569. pxor 0x00(%rsp), @XMM[0] # ^= tweak[]
  2570. pxor 0x10(%rsp), @XMM[1]
  2571. movdqu @XMM[0], 0x00($out) # write output
  2572. pxor 0x20(%rsp), @XMM[6]
  2573. movdqu @XMM[1], 0x10($out)
  2574. pxor 0x30(%rsp), @XMM[4]
  2575. movdqu @XMM[6], 0x20($out)
  2576. pxor 0x40(%rsp), @XMM[2]
  2577. movdqu @XMM[4], 0x30($out)
  2578. pxor 0x50(%rsp), @XMM[7]
  2579. movdqu @XMM[2], 0x40($out)
  2580. pxor 0x60(%rsp), @XMM[3]
  2581. movdqu @XMM[7], 0x50($out)
  2582. movdqu @XMM[3], 0x60($out)
  2583. lea 0x70($out), $out
  2584. movdqa 0x70(%rsp), @XMM[7] # next iteration tweak
  2585. jmp .Lxts_dec_done
  2586. .align 16
  2587. .Lxts_dec_6:
  2588. pxor @XMM[8+4], @XMM[4]
  2589. lea 0x60($inp), $inp
  2590. pxor @XMM[8+5], @XMM[5]
  2591. lea 0x80(%rsp), %rax # pass key schedule
  2592. mov %edx, %r10d # pass rounds
  2593. call _bsaes_decrypt8
  2594. pxor 0x00(%rsp), @XMM[0] # ^= tweak[]
  2595. pxor 0x10(%rsp), @XMM[1]
  2596. movdqu @XMM[0], 0x00($out) # write output
  2597. pxor 0x20(%rsp), @XMM[6]
  2598. movdqu @XMM[1], 0x10($out)
  2599. pxor 0x30(%rsp), @XMM[4]
  2600. movdqu @XMM[6], 0x20($out)
  2601. pxor 0x40(%rsp), @XMM[2]
  2602. movdqu @XMM[4], 0x30($out)
  2603. pxor 0x50(%rsp), @XMM[7]
  2604. movdqu @XMM[2], 0x40($out)
  2605. movdqu @XMM[7], 0x50($out)
  2606. lea 0x60($out), $out
  2607. movdqa 0x60(%rsp), @XMM[7] # next iteration tweak
  2608. jmp .Lxts_dec_done
  2609. .align 16
  2610. .Lxts_dec_5:
  2611. pxor @XMM[8+3], @XMM[3]
  2612. lea 0x50($inp), $inp
  2613. pxor @XMM[8+4], @XMM[4]
  2614. lea 0x80(%rsp), %rax # pass key schedule
  2615. mov %edx, %r10d # pass rounds
  2616. call _bsaes_decrypt8
  2617. pxor 0x00(%rsp), @XMM[0] # ^= tweak[]
  2618. pxor 0x10(%rsp), @XMM[1]
  2619. movdqu @XMM[0], 0x00($out) # write output
  2620. pxor 0x20(%rsp), @XMM[6]
  2621. movdqu @XMM[1], 0x10($out)
  2622. pxor 0x30(%rsp), @XMM[4]
  2623. movdqu @XMM[6], 0x20($out)
  2624. pxor 0x40(%rsp), @XMM[2]
  2625. movdqu @XMM[4], 0x30($out)
  2626. movdqu @XMM[2], 0x40($out)
  2627. lea 0x50($out), $out
  2628. movdqa 0x50(%rsp), @XMM[7] # next iteration tweak
  2629. jmp .Lxts_dec_done
  2630. .align 16
  2631. .Lxts_dec_4:
  2632. pxor @XMM[8+2], @XMM[2]
  2633. lea 0x40($inp), $inp
  2634. pxor @XMM[8+3], @XMM[3]
  2635. lea 0x80(%rsp), %rax # pass key schedule
  2636. mov %edx, %r10d # pass rounds
  2637. call _bsaes_decrypt8
  2638. pxor 0x00(%rsp), @XMM[0] # ^= tweak[]
  2639. pxor 0x10(%rsp), @XMM[1]
  2640. movdqu @XMM[0], 0x00($out) # write output
  2641. pxor 0x20(%rsp), @XMM[6]
  2642. movdqu @XMM[1], 0x10($out)
  2643. pxor 0x30(%rsp), @XMM[4]
  2644. movdqu @XMM[6], 0x20($out)
  2645. movdqu @XMM[4], 0x30($out)
  2646. lea 0x40($out), $out
  2647. movdqa 0x40(%rsp), @XMM[7] # next iteration tweak
  2648. jmp .Lxts_dec_done
  2649. .align 16
  2650. .Lxts_dec_3:
  2651. pxor @XMM[8+1], @XMM[1]
  2652. lea 0x30($inp), $inp
  2653. pxor @XMM[8+2], @XMM[2]
  2654. lea 0x80(%rsp), %rax # pass key schedule
  2655. mov %edx, %r10d # pass rounds
  2656. call _bsaes_decrypt8
  2657. pxor 0x00(%rsp), @XMM[0] # ^= tweak[]
  2658. pxor 0x10(%rsp), @XMM[1]
  2659. movdqu @XMM[0], 0x00($out) # write output
  2660. pxor 0x20(%rsp), @XMM[6]
  2661. movdqu @XMM[1], 0x10($out)
  2662. movdqu @XMM[6], 0x20($out)
  2663. lea 0x30($out), $out
  2664. movdqa 0x30(%rsp), @XMM[7] # next iteration tweak
  2665. jmp .Lxts_dec_done
  2666. .align 16
  2667. .Lxts_dec_2:
  2668. pxor @XMM[8+0], @XMM[0]
  2669. lea 0x20($inp), $inp
  2670. pxor @XMM[8+1], @XMM[1]
  2671. lea 0x80(%rsp), %rax # pass key schedule
  2672. mov %edx, %r10d # pass rounds
  2673. call _bsaes_decrypt8
  2674. pxor 0x00(%rsp), @XMM[0] # ^= tweak[]
  2675. pxor 0x10(%rsp), @XMM[1]
  2676. movdqu @XMM[0], 0x00($out) # write output
  2677. movdqu @XMM[1], 0x10($out)
  2678. lea 0x20($out), $out
  2679. movdqa 0x20(%rsp), @XMM[7] # next iteration tweak
  2680. jmp .Lxts_dec_done
  2681. .align 16
  2682. .Lxts_dec_1:
  2683. pxor @XMM[0], @XMM[8]
  2684. lea 0x10($inp), $inp
  2685. movdqa @XMM[8], 0x20(%rbp)
  2686. lea 0x20(%rbp), $arg1
  2687. lea 0x20(%rbp), $arg2
  2688. lea ($key), $arg3
  2689. call asm_AES_decrypt # doesn't touch %xmm
  2690. pxor 0x20(%rbp), @XMM[0] # ^= tweak[]
  2691. #pxor @XMM[8], @XMM[0]
  2692. #lea 0x80(%rsp), %rax # pass key schedule
  2693. #mov %edx, %r10d # pass rounds
  2694. #call _bsaes_decrypt8
  2695. #pxor 0x00(%rsp), @XMM[0] # ^= tweak[]
  2696. movdqu @XMM[0], 0x00($out) # write output
  2697. lea 0x10($out), $out
  2698. movdqa 0x10(%rsp), @XMM[7] # next iteration tweak
  2699. .Lxts_dec_done:
  2700. and \$15, %ebx
  2701. jz .Lxts_dec_ret
  2702. pxor $twtmp, $twtmp
  2703. movdqa .Lxts_magic(%rip), $twmask
  2704. pcmpgtd @XMM[7], $twtmp
  2705. pshufd \$0x13, $twtmp, $twres
  2706. movdqa @XMM[7], @XMM[6]
  2707. paddq @XMM[7], @XMM[7] # psllq 1,$tweak
  2708. pand $twmask, $twres # isolate carry and residue
  2709. movdqu ($inp), @XMM[0]
  2710. pxor $twres, @XMM[7]
  2711. lea 0x20(%rbp), $arg1
  2712. pxor @XMM[7], @XMM[0]
  2713. lea 0x20(%rbp), $arg2
  2714. movdqa @XMM[0], 0x20(%rbp)
  2715. lea ($key), $arg3
  2716. call asm_AES_decrypt # doesn't touch %xmm
  2717. pxor 0x20(%rbp), @XMM[7]
  2718. mov $out, %rdx
  2719. movdqu @XMM[7], ($out)
  2720. .Lxts_dec_steal:
  2721. movzb 16($inp), %eax
  2722. movzb (%rdx), %ecx
  2723. lea 1($inp), $inp
  2724. mov %al, (%rdx)
  2725. mov %cl, 16(%rdx)
  2726. lea 1(%rdx), %rdx
  2727. sub \$1,%ebx
  2728. jnz .Lxts_dec_steal
  2729. movdqu ($out), @XMM[0]
  2730. lea 0x20(%rbp), $arg1
  2731. pxor @XMM[6], @XMM[0]
  2732. lea 0x20(%rbp), $arg2
  2733. movdqa @XMM[0], 0x20(%rbp)
  2734. lea ($key), $arg3
  2735. call asm_AES_decrypt # doesn't touch %xmm
  2736. pxor 0x20(%rbp), @XMM[6]
  2737. movdqu @XMM[6], ($out)
  2738. .Lxts_dec_ret:
  2739. lea (%rsp), %rax
  2740. pxor %xmm0, %xmm0
  2741. .Lxts_dec_bzero: # wipe key schedule [if any]
  2742. movdqa %xmm0, 0x00(%rax)
  2743. movdqa %xmm0, 0x10(%rax)
  2744. lea 0x20(%rax), %rax
  2745. cmp %rax, %rbp
  2746. ja .Lxts_dec_bzero
  2747. lea 0x78(%rbp),%rax
  2748. .cfi_def_cfa %rax,8
  2749. ___
  2750. $code.=<<___ if ($win64);
  2751. movaps 0x40(%rbp), %xmm6
  2752. movaps 0x50(%rbp), %xmm7
  2753. movaps 0x60(%rbp), %xmm8
  2754. movaps 0x70(%rbp), %xmm9
  2755. movaps 0x80(%rbp), %xmm10
  2756. movaps 0x90(%rbp), %xmm11
  2757. movaps 0xa0(%rbp), %xmm12
  2758. movaps 0xb0(%rbp), %xmm13
  2759. movaps 0xc0(%rbp), %xmm14
  2760. movaps 0xd0(%rbp), %xmm15
  2761. lea 0xa0(%rax), %rax
  2762. .Lxts_dec_tail:
  2763. ___
  2764. $code.=<<___;
  2765. mov -48(%rax), %r15
  2766. .cfi_restore %r15
  2767. mov -40(%rax), %r14
  2768. .cfi_restore %r14
  2769. mov -32(%rax), %r13
  2770. .cfi_restore %r13
  2771. mov -24(%rax), %r12
  2772. .cfi_restore %r12
  2773. mov -16(%rax), %rbx
  2774. .cfi_restore %rbx
  2775. mov -8(%rax), %rbp
  2776. .cfi_restore %rbp
  2777. lea (%rax), %rsp # restore %rsp
  2778. .cfi_def_cfa_register %rsp
  2779. .Lxts_dec_epilogue:
  2780. ret
  2781. .cfi_endproc
  2782. .size bsaes_xts_decrypt,.-bsaes_xts_decrypt
  2783. ___
  2784. }
  2785. $code.=<<___;
  2786. .type _bsaes_const,\@object
  2787. .align 64
  2788. _bsaes_const:
  2789. .LM0ISR: # InvShiftRows constants
  2790. .quad 0x0a0e0206070b0f03, 0x0004080c0d010509
  2791. .LISRM0:
  2792. .quad 0x01040b0e0205080f, 0x0306090c00070a0d
  2793. .LISR:
  2794. .quad 0x0504070602010003, 0x0f0e0d0c080b0a09
  2795. .LBS0: # bit-slice constants
  2796. .quad 0x5555555555555555, 0x5555555555555555
  2797. .LBS1:
  2798. .quad 0x3333333333333333, 0x3333333333333333
  2799. .LBS2:
  2800. .quad 0x0f0f0f0f0f0f0f0f, 0x0f0f0f0f0f0f0f0f
  2801. .LSR: # shiftrows constants
  2802. .quad 0x0504070600030201, 0x0f0e0d0c0a09080b
  2803. .LSRM0:
  2804. .quad 0x0304090e00050a0f, 0x01060b0c0207080d
  2805. .LM0SR:
  2806. .quad 0x0a0e02060f03070b, 0x0004080c05090d01
  2807. .LSWPUP: # byte-swap upper dword
  2808. .quad 0x0706050403020100, 0x0c0d0e0f0b0a0908
  2809. .LSWPUPM0SR:
  2810. .quad 0x0a0d02060c03070b, 0x0004080f05090e01
  2811. .LADD1: # counter increment constants
  2812. .quad 0x0000000000000000, 0x0000000100000000
  2813. .LADD2:
  2814. .quad 0x0000000000000000, 0x0000000200000000
  2815. .LADD3:
  2816. .quad 0x0000000000000000, 0x0000000300000000
  2817. .LADD4:
  2818. .quad 0x0000000000000000, 0x0000000400000000
  2819. .LADD5:
  2820. .quad 0x0000000000000000, 0x0000000500000000
  2821. .LADD6:
  2822. .quad 0x0000000000000000, 0x0000000600000000
  2823. .LADD7:
  2824. .quad 0x0000000000000000, 0x0000000700000000
  2825. .LADD8:
  2826. .quad 0x0000000000000000, 0x0000000800000000
  2827. .Lxts_magic:
  2828. .long 0x87,0,1,0
  2829. .Lmasks:
  2830. .quad 0x0101010101010101, 0x0101010101010101
  2831. .quad 0x0202020202020202, 0x0202020202020202
  2832. .quad 0x0404040404040404, 0x0404040404040404
  2833. .quad 0x0808080808080808, 0x0808080808080808
  2834. .LM0:
  2835. .quad 0x02060a0e03070b0f, 0x0004080c0105090d
  2836. .L63:
  2837. .quad 0x6363636363636363, 0x6363636363636363
  2838. .asciz "Bit-sliced AES for x86_64/SSSE3, Emilia Käsper, Peter Schwabe, Andy Polyakov"
  2839. .align 64
  2840. .size _bsaes_const,.-_bsaes_const
  2841. ___
  2842. # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
  2843. # CONTEXT *context,DISPATCHER_CONTEXT *disp)
  2844. if ($win64) {
  2845. $rec="%rcx";
  2846. $frame="%rdx";
  2847. $context="%r8";
  2848. $disp="%r9";
  2849. $code.=<<___;
  2850. .extern __imp_RtlVirtualUnwind
  2851. .type se_handler,\@abi-omnipotent
  2852. .align 16
  2853. se_handler:
  2854. push %rsi
  2855. push %rdi
  2856. push %rbx
  2857. push %rbp
  2858. push %r12
  2859. push %r13
  2860. push %r14
  2861. push %r15
  2862. pushfq
  2863. sub \$64,%rsp
  2864. mov 120($context),%rax # pull context->Rax
  2865. mov 248($context),%rbx # pull context->Rip
  2866. mov 8($disp),%rsi # disp->ImageBase
  2867. mov 56($disp),%r11 # disp->HandlerData
  2868. mov 0(%r11),%r10d # HandlerData[0]
  2869. lea (%rsi,%r10),%r10 # prologue label
  2870. cmp %r10,%rbx # context->Rip<=prologue label
  2871. jbe .Lin_prologue
  2872. mov 4(%r11),%r10d # HandlerData[1]
  2873. lea (%rsi,%r10),%r10 # epilogue label
  2874. cmp %r10,%rbx # context->Rip>=epilogue label
  2875. jae .Lin_prologue
  2876. mov 8(%r11),%r10d # HandlerData[2]
  2877. lea (%rsi,%r10),%r10 # epilogue label
  2878. cmp %r10,%rbx # context->Rip>=tail label
  2879. jae .Lin_tail
  2880. mov 160($context),%rax # pull context->Rbp
  2881. lea 0x40(%rax),%rsi # %xmm save area
  2882. lea 512($context),%rdi # &context.Xmm6
  2883. mov \$20,%ecx # 10*sizeof(%xmm0)/sizeof(%rax)
  2884. .long 0xa548f3fc # cld; rep movsq
  2885. lea 0xa0+0x78(%rax),%rax # adjust stack pointer
  2886. .Lin_tail:
  2887. mov -48(%rax),%rbp
  2888. mov -40(%rax),%rbx
  2889. mov -32(%rax),%r12
  2890. mov -24(%rax),%r13
  2891. mov -16(%rax),%r14
  2892. mov -8(%rax),%r15
  2893. mov %rbx,144($context) # restore context->Rbx
  2894. mov %rbp,160($context) # restore context->Rbp
  2895. mov %r12,216($context) # restore context->R12
  2896. mov %r13,224($context) # restore context->R13
  2897. mov %r14,232($context) # restore context->R14
  2898. mov %r15,240($context) # restore context->R15
  2899. .Lin_prologue:
  2900. mov %rax,152($context) # restore context->Rsp
  2901. mov 40($disp),%rdi # disp->ContextRecord
  2902. mov $context,%rsi # context
  2903. mov \$`1232/8`,%ecx # sizeof(CONTEXT)
  2904. .long 0xa548f3fc # cld; rep movsq
  2905. mov $disp,%rsi
  2906. xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
  2907. mov 8(%rsi),%rdx # arg2, disp->ImageBase
  2908. mov 0(%rsi),%r8 # arg3, disp->ControlPc
  2909. mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
  2910. mov 40(%rsi),%r10 # disp->ContextRecord
  2911. lea 56(%rsi),%r11 # &disp->HandlerData
  2912. lea 24(%rsi),%r12 # &disp->EstablisherFrame
  2913. mov %r10,32(%rsp) # arg5
  2914. mov %r11,40(%rsp) # arg6
  2915. mov %r12,48(%rsp) # arg7
  2916. mov %rcx,56(%rsp) # arg8, (NULL)
  2917. call *__imp_RtlVirtualUnwind(%rip)
  2918. mov \$1,%eax # ExceptionContinueSearch
  2919. add \$64,%rsp
  2920. popfq
  2921. pop %r15
  2922. pop %r14
  2923. pop %r13
  2924. pop %r12
  2925. pop %rbp
  2926. pop %rbx
  2927. pop %rdi
  2928. pop %rsi
  2929. ret
  2930. .size se_handler,.-se_handler
  2931. .section .pdata
  2932. .align 4
  2933. ___
  2934. $code.=<<___ if ($ecb);
  2935. .rva .Lecb_enc_prologue
  2936. .rva .Lecb_enc_epilogue
  2937. .rva .Lecb_enc_info
  2938. .rva .Lecb_dec_prologue
  2939. .rva .Lecb_dec_epilogue
  2940. .rva .Lecb_dec_info
  2941. ___
  2942. $code.=<<___;
  2943. .rva .Lcbc_dec_prologue
  2944. .rva .Lcbc_dec_epilogue
  2945. .rva .Lcbc_dec_info
  2946. .rva .Lctr_enc_prologue
  2947. .rva .Lctr_enc_epilogue
  2948. .rva .Lctr_enc_info
  2949. .rva .Lxts_enc_prologue
  2950. .rva .Lxts_enc_epilogue
  2951. .rva .Lxts_enc_info
  2952. .rva .Lxts_dec_prologue
  2953. .rva .Lxts_dec_epilogue
  2954. .rva .Lxts_dec_info
  2955. .section .xdata
  2956. .align 8
  2957. ___
  2958. $code.=<<___ if ($ecb);
  2959. .Lecb_enc_info:
  2960. .byte 9,0,0,0
  2961. .rva se_handler
  2962. .rva .Lecb_enc_body,.Lecb_enc_epilogue # HandlerData[]
  2963. .rva .Lecb_enc_tail
  2964. .long 0
  2965. .Lecb_dec_info:
  2966. .byte 9,0,0,0
  2967. .rva se_handler
  2968. .rva .Lecb_dec_body,.Lecb_dec_epilogue # HandlerData[]
  2969. .rva .Lecb_dec_tail
  2970. .long 0
  2971. ___
  2972. $code.=<<___;
  2973. .Lcbc_dec_info:
  2974. .byte 9,0,0,0
  2975. .rva se_handler
  2976. .rva .Lcbc_dec_body,.Lcbc_dec_epilogue # HandlerData[]
  2977. .rva .Lcbc_dec_tail
  2978. .long 0
  2979. .Lctr_enc_info:
  2980. .byte 9,0,0,0
  2981. .rva se_handler
  2982. .rva .Lctr_enc_body,.Lctr_enc_epilogue # HandlerData[]
  2983. .rva .Lctr_enc_tail
  2984. .long 0
  2985. .Lxts_enc_info:
  2986. .byte 9,0,0,0
  2987. .rva se_handler
  2988. .rva .Lxts_enc_body,.Lxts_enc_epilogue # HandlerData[]
  2989. .rva .Lxts_enc_tail
  2990. .long 0
  2991. .Lxts_dec_info:
  2992. .byte 9,0,0,0
  2993. .rva se_handler
  2994. .rva .Lxts_dec_body,.Lxts_dec_epilogue # HandlerData[]
  2995. .rva .Lxts_dec_tail
  2996. .long 0
  2997. ___
  2998. }
  2999. $code =~ s/\`([^\`]*)\`/eval($1)/gem;
  3000. print $code;
  3001. close STDOUT;