bsaes-x86_64.pl 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233
  1. #! /usr/bin/env perl
  2. # Copyright 2011-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the OpenSSL license (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. lea .LBS0(%rip), $const # constants table
  748. movdqa ($key), @XMM[9] # round 0 key
  749. lea 0x10($key), $key
  750. movdqa 0x50($const), @XMM[8] # .LM0SR
  751. pxor @XMM[9], @XMM[0] # xor with round0 key
  752. pxor @XMM[9], @XMM[1]
  753. pxor @XMM[9], @XMM[2]
  754. pxor @XMM[9], @XMM[3]
  755. pshufb @XMM[8], @XMM[0]
  756. pshufb @XMM[8], @XMM[1]
  757. pxor @XMM[9], @XMM[4]
  758. pxor @XMM[9], @XMM[5]
  759. pshufb @XMM[8], @XMM[2]
  760. pshufb @XMM[8], @XMM[3]
  761. pxor @XMM[9], @XMM[6]
  762. pxor @XMM[9], @XMM[7]
  763. pshufb @XMM[8], @XMM[4]
  764. pshufb @XMM[8], @XMM[5]
  765. pshufb @XMM[8], @XMM[6]
  766. pshufb @XMM[8], @XMM[7]
  767. _bsaes_encrypt8_bitslice:
  768. ___
  769. &bitslice (@XMM[0..7, 8..11]);
  770. $code.=<<___;
  771. dec $rounds
  772. jmp .Lenc_sbox
  773. .align 16
  774. .Lenc_loop:
  775. ___
  776. &ShiftRows (@XMM[0..7, 8]);
  777. $code.=".Lenc_sbox:\n";
  778. &Sbox (@XMM[0..7, 8..15]);
  779. $code.=<<___;
  780. dec $rounds
  781. jl .Lenc_done
  782. ___
  783. &MixColumns (@XMM[0,1,4,6,3,7,2,5, 8..15]);
  784. $code.=<<___;
  785. movdqa 0x30($const), @XMM[8] # .LSR
  786. jnz .Lenc_loop
  787. movdqa 0x40($const), @XMM[8] # .LSRM0
  788. jmp .Lenc_loop
  789. .align 16
  790. .Lenc_done:
  791. ___
  792. # output in lsb > [t0, t1, t4, t6, t3, t7, t2, t5] < msb
  793. &bitslice (@XMM[0,1,4,6,3,7,2,5, 8..11]);
  794. $code.=<<___;
  795. movdqa ($key), @XMM[8] # last round key
  796. pxor @XMM[8], @XMM[4]
  797. pxor @XMM[8], @XMM[6]
  798. pxor @XMM[8], @XMM[3]
  799. pxor @XMM[8], @XMM[7]
  800. pxor @XMM[8], @XMM[2]
  801. pxor @XMM[8], @XMM[5]
  802. pxor @XMM[8], @XMM[0]
  803. pxor @XMM[8], @XMM[1]
  804. ret
  805. .size _bsaes_encrypt8,.-_bsaes_encrypt8
  806. .type _bsaes_decrypt8,\@abi-omnipotent
  807. .align 64
  808. _bsaes_decrypt8:
  809. lea .LBS0(%rip), $const # constants table
  810. movdqa ($key), @XMM[9] # round 0 key
  811. lea 0x10($key), $key
  812. movdqa -0x30($const), @XMM[8] # .LM0ISR
  813. pxor @XMM[9], @XMM[0] # xor with round0 key
  814. pxor @XMM[9], @XMM[1]
  815. pxor @XMM[9], @XMM[2]
  816. pxor @XMM[9], @XMM[3]
  817. pshufb @XMM[8], @XMM[0]
  818. pshufb @XMM[8], @XMM[1]
  819. pxor @XMM[9], @XMM[4]
  820. pxor @XMM[9], @XMM[5]
  821. pshufb @XMM[8], @XMM[2]
  822. pshufb @XMM[8], @XMM[3]
  823. pxor @XMM[9], @XMM[6]
  824. pxor @XMM[9], @XMM[7]
  825. pshufb @XMM[8], @XMM[4]
  826. pshufb @XMM[8], @XMM[5]
  827. pshufb @XMM[8], @XMM[6]
  828. pshufb @XMM[8], @XMM[7]
  829. ___
  830. &bitslice (@XMM[0..7, 8..11]);
  831. $code.=<<___;
  832. dec $rounds
  833. jmp .Ldec_sbox
  834. .align 16
  835. .Ldec_loop:
  836. ___
  837. &ShiftRows (@XMM[0..7, 8]);
  838. $code.=".Ldec_sbox:\n";
  839. &InvSbox (@XMM[0..7, 8..15]);
  840. $code.=<<___;
  841. dec $rounds
  842. jl .Ldec_done
  843. ___
  844. &InvMixColumns (@XMM[0,1,6,4,2,7,3,5, 8..15]);
  845. $code.=<<___;
  846. movdqa -0x10($const), @XMM[8] # .LISR
  847. jnz .Ldec_loop
  848. movdqa -0x20($const), @XMM[8] # .LISRM0
  849. jmp .Ldec_loop
  850. .align 16
  851. .Ldec_done:
  852. ___
  853. &bitslice (@XMM[0,1,6,4,2,7,3,5, 8..11]);
  854. $code.=<<___;
  855. movdqa ($key), @XMM[8] # last round key
  856. pxor @XMM[8], @XMM[6]
  857. pxor @XMM[8], @XMM[4]
  858. pxor @XMM[8], @XMM[2]
  859. pxor @XMM[8], @XMM[7]
  860. pxor @XMM[8], @XMM[3]
  861. pxor @XMM[8], @XMM[5]
  862. pxor @XMM[8], @XMM[0]
  863. pxor @XMM[8], @XMM[1]
  864. ret
  865. .size _bsaes_decrypt8,.-_bsaes_decrypt8
  866. ___
  867. }
  868. {
  869. my ($out,$inp,$rounds,$const)=("%rax","%rcx","%r10d","%r11");
  870. sub bitslice_key {
  871. my @x=reverse(@_[0..7]);
  872. my ($bs0,$bs1,$bs2,$t2,$t3)=@_[8..12];
  873. &swapmove (@x[0,1],1,$bs0,$t2,$t3);
  874. $code.=<<___;
  875. #&swapmove(@x[2,3],1,$t0,$t2,$t3);
  876. movdqa @x[0], @x[2]
  877. movdqa @x[1], @x[3]
  878. ___
  879. #&swapmove2x(@x[4,5,6,7],1,$t0,$t2,$t3);
  880. &swapmove2x (@x[0,2,1,3],2,$bs1,$t2,$t3);
  881. $code.=<<___;
  882. #&swapmove2x(@x[4,6,5,7],2,$t1,$t2,$t3);
  883. movdqa @x[0], @x[4]
  884. movdqa @x[2], @x[6]
  885. movdqa @x[1], @x[5]
  886. movdqa @x[3], @x[7]
  887. ___
  888. &swapmove2x (@x[0,4,1,5],4,$bs2,$t2,$t3);
  889. &swapmove2x (@x[2,6,3,7],4,$bs2,$t2,$t3);
  890. }
  891. $code.=<<___;
  892. .type _bsaes_key_convert,\@abi-omnipotent
  893. .align 16
  894. _bsaes_key_convert:
  895. lea .Lmasks(%rip), $const
  896. movdqu ($inp), %xmm7 # load round 0 key
  897. lea 0x10($inp), $inp
  898. movdqa 0x00($const), %xmm0 # 0x01...
  899. movdqa 0x10($const), %xmm1 # 0x02...
  900. movdqa 0x20($const), %xmm2 # 0x04...
  901. movdqa 0x30($const), %xmm3 # 0x08...
  902. movdqa 0x40($const), %xmm4 # .LM0
  903. pcmpeqd %xmm5, %xmm5 # .LNOT
  904. movdqu ($inp), %xmm6 # load round 1 key
  905. movdqa %xmm7, ($out) # save round 0 key
  906. lea 0x10($out), $out
  907. dec $rounds
  908. jmp .Lkey_loop
  909. .align 16
  910. .Lkey_loop:
  911. pshufb %xmm4, %xmm6 # .LM0
  912. movdqa %xmm0, %xmm8
  913. movdqa %xmm1, %xmm9
  914. pand %xmm6, %xmm8
  915. pand %xmm6, %xmm9
  916. movdqa %xmm2, %xmm10
  917. pcmpeqb %xmm0, %xmm8
  918. psllq \$4, %xmm0 # 0x10...
  919. movdqa %xmm3, %xmm11
  920. pcmpeqb %xmm1, %xmm9
  921. psllq \$4, %xmm1 # 0x20...
  922. pand %xmm6, %xmm10
  923. pand %xmm6, %xmm11
  924. movdqa %xmm0, %xmm12
  925. pcmpeqb %xmm2, %xmm10
  926. psllq \$4, %xmm2 # 0x40...
  927. movdqa %xmm1, %xmm13
  928. pcmpeqb %xmm3, %xmm11
  929. psllq \$4, %xmm3 # 0x80...
  930. movdqa %xmm2, %xmm14
  931. movdqa %xmm3, %xmm15
  932. pxor %xmm5, %xmm8 # "pnot"
  933. pxor %xmm5, %xmm9
  934. pand %xmm6, %xmm12
  935. pand %xmm6, %xmm13
  936. movdqa %xmm8, 0x00($out) # write bit-sliced round key
  937. pcmpeqb %xmm0, %xmm12
  938. psrlq \$4, %xmm0 # 0x01...
  939. movdqa %xmm9, 0x10($out)
  940. pcmpeqb %xmm1, %xmm13
  941. psrlq \$4, %xmm1 # 0x02...
  942. lea 0x10($inp), $inp
  943. pand %xmm6, %xmm14
  944. pand %xmm6, %xmm15
  945. movdqa %xmm10, 0x20($out)
  946. pcmpeqb %xmm2, %xmm14
  947. psrlq \$4, %xmm2 # 0x04...
  948. movdqa %xmm11, 0x30($out)
  949. pcmpeqb %xmm3, %xmm15
  950. psrlq \$4, %xmm3 # 0x08...
  951. movdqu ($inp), %xmm6 # load next round key
  952. pxor %xmm5, %xmm13 # "pnot"
  953. pxor %xmm5, %xmm14
  954. movdqa %xmm12, 0x40($out)
  955. movdqa %xmm13, 0x50($out)
  956. movdqa %xmm14, 0x60($out)
  957. movdqa %xmm15, 0x70($out)
  958. lea 0x80($out),$out
  959. dec $rounds
  960. jnz .Lkey_loop
  961. movdqa 0x50($const), %xmm7 # .L63
  962. #movdqa %xmm6, ($out) # don't save last round key
  963. ret
  964. .size _bsaes_key_convert,.-_bsaes_key_convert
  965. ___
  966. }
  967. if (0 && !$win64) { # following four functions are unsupported interface
  968. # used for benchmarking...
  969. $code.=<<___;
  970. .globl bsaes_enc_key_convert
  971. .type bsaes_enc_key_convert,\@function,2
  972. .align 16
  973. bsaes_enc_key_convert:
  974. mov 240($inp),%r10d # pass rounds
  975. mov $inp,%rcx # pass key
  976. mov $out,%rax # pass key schedule
  977. call _bsaes_key_convert
  978. pxor %xmm6,%xmm7 # fix up last round key
  979. movdqa %xmm7,(%rax) # save last round key
  980. ret
  981. .size bsaes_enc_key_convert,.-bsaes_enc_key_convert
  982. .globl bsaes_encrypt_128
  983. .type bsaes_encrypt_128,\@function,4
  984. .align 16
  985. bsaes_encrypt_128:
  986. .Lenc128_loop:
  987. movdqu 0x00($inp), @XMM[0] # load input
  988. movdqu 0x10($inp), @XMM[1]
  989. movdqu 0x20($inp), @XMM[2]
  990. movdqu 0x30($inp), @XMM[3]
  991. movdqu 0x40($inp), @XMM[4]
  992. movdqu 0x50($inp), @XMM[5]
  993. movdqu 0x60($inp), @XMM[6]
  994. movdqu 0x70($inp), @XMM[7]
  995. mov $key, %rax # pass the $key
  996. lea 0x80($inp), $inp
  997. mov \$10,%r10d
  998. call _bsaes_encrypt8
  999. movdqu @XMM[0], 0x00($out) # write output
  1000. movdqu @XMM[1], 0x10($out)
  1001. movdqu @XMM[4], 0x20($out)
  1002. movdqu @XMM[6], 0x30($out)
  1003. movdqu @XMM[3], 0x40($out)
  1004. movdqu @XMM[7], 0x50($out)
  1005. movdqu @XMM[2], 0x60($out)
  1006. movdqu @XMM[5], 0x70($out)
  1007. lea 0x80($out), $out
  1008. sub \$0x80,$len
  1009. ja .Lenc128_loop
  1010. ret
  1011. .size bsaes_encrypt_128,.-bsaes_encrypt_128
  1012. .globl bsaes_dec_key_convert
  1013. .type bsaes_dec_key_convert,\@function,2
  1014. .align 16
  1015. bsaes_dec_key_convert:
  1016. mov 240($inp),%r10d # pass rounds
  1017. mov $inp,%rcx # pass key
  1018. mov $out,%rax # pass key schedule
  1019. call _bsaes_key_convert
  1020. pxor ($out),%xmm7 # fix up round 0 key
  1021. movdqa %xmm6,(%rax) # save last round key
  1022. movdqa %xmm7,($out)
  1023. ret
  1024. .size bsaes_dec_key_convert,.-bsaes_dec_key_convert
  1025. .globl bsaes_decrypt_128
  1026. .type bsaes_decrypt_128,\@function,4
  1027. .align 16
  1028. bsaes_decrypt_128:
  1029. .Ldec128_loop:
  1030. movdqu 0x00($inp), @XMM[0] # load input
  1031. movdqu 0x10($inp), @XMM[1]
  1032. movdqu 0x20($inp), @XMM[2]
  1033. movdqu 0x30($inp), @XMM[3]
  1034. movdqu 0x40($inp), @XMM[4]
  1035. movdqu 0x50($inp), @XMM[5]
  1036. movdqu 0x60($inp), @XMM[6]
  1037. movdqu 0x70($inp), @XMM[7]
  1038. mov $key, %rax # pass the $key
  1039. lea 0x80($inp), $inp
  1040. mov \$10,%r10d
  1041. call _bsaes_decrypt8
  1042. movdqu @XMM[0], 0x00($out) # write output
  1043. movdqu @XMM[1], 0x10($out)
  1044. movdqu @XMM[6], 0x20($out)
  1045. movdqu @XMM[4], 0x30($out)
  1046. movdqu @XMM[2], 0x40($out)
  1047. movdqu @XMM[7], 0x50($out)
  1048. movdqu @XMM[3], 0x60($out)
  1049. movdqu @XMM[5], 0x70($out)
  1050. lea 0x80($out), $out
  1051. sub \$0x80,$len
  1052. ja .Ldec128_loop
  1053. ret
  1054. .size bsaes_decrypt_128,.-bsaes_decrypt_128
  1055. ___
  1056. }
  1057. {
  1058. ######################################################################
  1059. #
  1060. # OpenSSL interface
  1061. #
  1062. my ($arg1,$arg2,$arg3,$arg4,$arg5,$arg6)=$win64 ? ("%rcx","%rdx","%r8","%r9","%r10","%r11d")
  1063. : ("%rdi","%rsi","%rdx","%rcx","%r8","%r9d");
  1064. my ($inp,$out,$len,$key)=("%r12","%r13","%r14","%r15");
  1065. if ($ecb) {
  1066. $code.=<<___;
  1067. .globl bsaes_ecb_encrypt_blocks
  1068. .type bsaes_ecb_encrypt_blocks,\@abi-omnipotent
  1069. .align 16
  1070. bsaes_ecb_encrypt_blocks:
  1071. .cfi_startproc
  1072. mov %rsp, %rax
  1073. .Lecb_enc_prologue:
  1074. push %rbp
  1075. .cfi_push %rbp
  1076. push %rbx
  1077. .cfi_push %rbx
  1078. push %r12
  1079. .cfi_push %r12
  1080. push %r13
  1081. .cfi_push %r13
  1082. push %r14
  1083. .cfi_push %r14
  1084. push %r15
  1085. .cfi_push %r15
  1086. lea -0x48(%rsp),%rsp
  1087. .cfi_adjust_cfa_offset 0x48
  1088. ___
  1089. $code.=<<___ if ($win64);
  1090. lea -0xa0(%rsp), %rsp
  1091. movaps %xmm6, 0x40(%rsp)
  1092. movaps %xmm7, 0x50(%rsp)
  1093. movaps %xmm8, 0x60(%rsp)
  1094. movaps %xmm9, 0x70(%rsp)
  1095. movaps %xmm10, 0x80(%rsp)
  1096. movaps %xmm11, 0x90(%rsp)
  1097. movaps %xmm12, 0xa0(%rsp)
  1098. movaps %xmm13, 0xb0(%rsp)
  1099. movaps %xmm14, 0xc0(%rsp)
  1100. movaps %xmm15, 0xd0(%rsp)
  1101. .Lecb_enc_body:
  1102. ___
  1103. $code.=<<___;
  1104. mov %rsp,%rbp # backup %rsp
  1105. .cfi_def_cfa_register %rbp
  1106. mov 240($arg4),%eax # rounds
  1107. mov $arg1,$inp # backup arguments
  1108. mov $arg2,$out
  1109. mov $arg3,$len
  1110. mov $arg4,$key
  1111. cmp \$8,$arg3
  1112. jb .Lecb_enc_short
  1113. mov %eax,%ebx # backup rounds
  1114. shl \$7,%rax # 128 bytes per inner round key
  1115. sub \$`128-32`,%rax # size of bit-sliced key schedule
  1116. sub %rax,%rsp
  1117. mov %rsp,%rax # pass key schedule
  1118. mov $key,%rcx # pass key
  1119. mov %ebx,%r10d # pass rounds
  1120. call _bsaes_key_convert
  1121. pxor %xmm6,%xmm7 # fix up last round key
  1122. movdqa %xmm7,(%rax) # save last round key
  1123. sub \$8,$len
  1124. .Lecb_enc_loop:
  1125. movdqu 0x00($inp), @XMM[0] # load input
  1126. movdqu 0x10($inp), @XMM[1]
  1127. movdqu 0x20($inp), @XMM[2]
  1128. movdqu 0x30($inp), @XMM[3]
  1129. movdqu 0x40($inp), @XMM[4]
  1130. movdqu 0x50($inp), @XMM[5]
  1131. mov %rsp, %rax # pass key schedule
  1132. movdqu 0x60($inp), @XMM[6]
  1133. mov %ebx,%r10d # pass rounds
  1134. movdqu 0x70($inp), @XMM[7]
  1135. lea 0x80($inp), $inp
  1136. call _bsaes_encrypt8
  1137. movdqu @XMM[0], 0x00($out) # write output
  1138. movdqu @XMM[1], 0x10($out)
  1139. movdqu @XMM[4], 0x20($out)
  1140. movdqu @XMM[6], 0x30($out)
  1141. movdqu @XMM[3], 0x40($out)
  1142. movdqu @XMM[7], 0x50($out)
  1143. movdqu @XMM[2], 0x60($out)
  1144. movdqu @XMM[5], 0x70($out)
  1145. lea 0x80($out), $out
  1146. sub \$8,$len
  1147. jnc .Lecb_enc_loop
  1148. add \$8,$len
  1149. jz .Lecb_enc_done
  1150. movdqu 0x00($inp), @XMM[0] # load input
  1151. mov %rsp, %rax # pass key schedule
  1152. mov %ebx,%r10d # pass rounds
  1153. cmp \$2,$len
  1154. jb .Lecb_enc_one
  1155. movdqu 0x10($inp), @XMM[1]
  1156. je .Lecb_enc_two
  1157. movdqu 0x20($inp), @XMM[2]
  1158. cmp \$4,$len
  1159. jb .Lecb_enc_three
  1160. movdqu 0x30($inp), @XMM[3]
  1161. je .Lecb_enc_four
  1162. movdqu 0x40($inp), @XMM[4]
  1163. cmp \$6,$len
  1164. jb .Lecb_enc_five
  1165. movdqu 0x50($inp), @XMM[5]
  1166. je .Lecb_enc_six
  1167. movdqu 0x60($inp), @XMM[6]
  1168. call _bsaes_encrypt8
  1169. movdqu @XMM[0], 0x00($out) # write output
  1170. movdqu @XMM[1], 0x10($out)
  1171. movdqu @XMM[4], 0x20($out)
  1172. movdqu @XMM[6], 0x30($out)
  1173. movdqu @XMM[3], 0x40($out)
  1174. movdqu @XMM[7], 0x50($out)
  1175. movdqu @XMM[2], 0x60($out)
  1176. jmp .Lecb_enc_done
  1177. .align 16
  1178. .Lecb_enc_six:
  1179. call _bsaes_encrypt8
  1180. movdqu @XMM[0], 0x00($out) # write output
  1181. movdqu @XMM[1], 0x10($out)
  1182. movdqu @XMM[4], 0x20($out)
  1183. movdqu @XMM[6], 0x30($out)
  1184. movdqu @XMM[3], 0x40($out)
  1185. movdqu @XMM[7], 0x50($out)
  1186. jmp .Lecb_enc_done
  1187. .align 16
  1188. .Lecb_enc_five:
  1189. call _bsaes_encrypt8
  1190. movdqu @XMM[0], 0x00($out) # write output
  1191. movdqu @XMM[1], 0x10($out)
  1192. movdqu @XMM[4], 0x20($out)
  1193. movdqu @XMM[6], 0x30($out)
  1194. movdqu @XMM[3], 0x40($out)
  1195. jmp .Lecb_enc_done
  1196. .align 16
  1197. .Lecb_enc_four:
  1198. call _bsaes_encrypt8
  1199. movdqu @XMM[0], 0x00($out) # write output
  1200. movdqu @XMM[1], 0x10($out)
  1201. movdqu @XMM[4], 0x20($out)
  1202. movdqu @XMM[6], 0x30($out)
  1203. jmp .Lecb_enc_done
  1204. .align 16
  1205. .Lecb_enc_three:
  1206. call _bsaes_encrypt8
  1207. movdqu @XMM[0], 0x00($out) # write output
  1208. movdqu @XMM[1], 0x10($out)
  1209. movdqu @XMM[4], 0x20($out)
  1210. jmp .Lecb_enc_done
  1211. .align 16
  1212. .Lecb_enc_two:
  1213. call _bsaes_encrypt8
  1214. movdqu @XMM[0], 0x00($out) # write output
  1215. movdqu @XMM[1], 0x10($out)
  1216. jmp .Lecb_enc_done
  1217. .align 16
  1218. .Lecb_enc_one:
  1219. call _bsaes_encrypt8
  1220. movdqu @XMM[0], 0x00($out) # write output
  1221. jmp .Lecb_enc_done
  1222. .align 16
  1223. .Lecb_enc_short:
  1224. lea ($inp), $arg1
  1225. lea ($out), $arg2
  1226. lea ($key), $arg3
  1227. call asm_AES_encrypt
  1228. lea 16($inp), $inp
  1229. lea 16($out), $out
  1230. dec $len
  1231. jnz .Lecb_enc_short
  1232. .Lecb_enc_done:
  1233. lea (%rsp),%rax
  1234. pxor %xmm0, %xmm0
  1235. .Lecb_enc_bzero: # wipe key schedule [if any]
  1236. movdqa %xmm0, 0x00(%rax)
  1237. movdqa %xmm0, 0x10(%rax)
  1238. lea 0x20(%rax), %rax
  1239. cmp %rax, %rbp
  1240. jb .Lecb_enc_bzero
  1241. lea 0x78(%rbp),%rax
  1242. .cfi_def_cfa %rax,8
  1243. ___
  1244. $code.=<<___ if ($win64);
  1245. movaps 0x40(%rbp), %xmm6
  1246. movaps 0x50(%rbp), %xmm7
  1247. movaps 0x60(%rbp), %xmm8
  1248. movaps 0x70(%rbp), %xmm9
  1249. movaps 0x80(%rbp), %xmm10
  1250. movaps 0x90(%rbp), %xmm11
  1251. movaps 0xa0(%rbp), %xmm12
  1252. movaps 0xb0(%rbp), %xmm13
  1253. movaps 0xc0(%rbp), %xmm14
  1254. movaps 0xd0(%rbp), %xmm15
  1255. lea 0xa0(%rax), %rax
  1256. .Lecb_enc_tail:
  1257. ___
  1258. $code.=<<___;
  1259. mov -48(%rax), %r15
  1260. .cfi_restore %r15
  1261. mov -40(%rax), %r14
  1262. .cfi_restore %r14
  1263. mov -32(%rax), %r13
  1264. .cfi_restore %r13
  1265. mov -24(%rax), %r12
  1266. .cfi_restore %r12
  1267. mov -16(%rax), %rbx
  1268. .cfi_restore %rbx
  1269. mov -8(%rax), %rbp
  1270. .cfi_restore %rbp
  1271. lea (%rax), %rsp # restore %rsp
  1272. .cfi_def_cfa_register %rsp
  1273. .Lecb_enc_epilogue:
  1274. ret
  1275. .cfi_endproc
  1276. .size bsaes_ecb_encrypt_blocks,.-bsaes_ecb_encrypt_blocks
  1277. .globl bsaes_ecb_decrypt_blocks
  1278. .type bsaes_ecb_decrypt_blocks,\@abi-omnipotent
  1279. .align 16
  1280. bsaes_ecb_decrypt_blocks:
  1281. .cfi_startproc
  1282. mov %rsp, %rax
  1283. .Lecb_dec_prologue:
  1284. push %rbp
  1285. .cfi_push %rbp
  1286. push %rbx
  1287. .cfi_push %rbx
  1288. push %r12
  1289. .cfi_push %r12
  1290. push %r13
  1291. .cfi_push %r13
  1292. push %r14
  1293. .cfi_push %r14
  1294. push %r15
  1295. .cfi_push %r15
  1296. lea -0x48(%rsp),%rsp
  1297. .cfi_adjust_cfa_offset 0x48
  1298. ___
  1299. $code.=<<___ if ($win64);
  1300. lea -0xa0(%rsp), %rsp
  1301. movaps %xmm6, 0x40(%rsp)
  1302. movaps %xmm7, 0x50(%rsp)
  1303. movaps %xmm8, 0x60(%rsp)
  1304. movaps %xmm9, 0x70(%rsp)
  1305. movaps %xmm10, 0x80(%rsp)
  1306. movaps %xmm11, 0x90(%rsp)
  1307. movaps %xmm12, 0xa0(%rsp)
  1308. movaps %xmm13, 0xb0(%rsp)
  1309. movaps %xmm14, 0xc0(%rsp)
  1310. movaps %xmm15, 0xd0(%rsp)
  1311. .Lecb_dec_body:
  1312. ___
  1313. $code.=<<___;
  1314. mov %rsp,%rbp # backup %rsp
  1315. .cfi_def_cfa_register %rbp
  1316. mov 240($arg4),%eax # rounds
  1317. mov $arg1,$inp # backup arguments
  1318. mov $arg2,$out
  1319. mov $arg3,$len
  1320. mov $arg4,$key
  1321. cmp \$8,$arg3
  1322. jb .Lecb_dec_short
  1323. mov %eax,%ebx # backup rounds
  1324. shl \$7,%rax # 128 bytes per inner round key
  1325. sub \$`128-32`,%rax # size of bit-sliced key schedule
  1326. sub %rax,%rsp
  1327. mov %rsp,%rax # pass key schedule
  1328. mov $key,%rcx # pass key
  1329. mov %ebx,%r10d # pass rounds
  1330. call _bsaes_key_convert
  1331. pxor (%rsp),%xmm7 # fix up 0 round key
  1332. movdqa %xmm6,(%rax) # save last round key
  1333. movdqa %xmm7,(%rsp)
  1334. sub \$8,$len
  1335. .Lecb_dec_loop:
  1336. movdqu 0x00($inp), @XMM[0] # load input
  1337. movdqu 0x10($inp), @XMM[1]
  1338. movdqu 0x20($inp), @XMM[2]
  1339. movdqu 0x30($inp), @XMM[3]
  1340. movdqu 0x40($inp), @XMM[4]
  1341. movdqu 0x50($inp), @XMM[5]
  1342. mov %rsp, %rax # pass key schedule
  1343. movdqu 0x60($inp), @XMM[6]
  1344. mov %ebx,%r10d # pass rounds
  1345. movdqu 0x70($inp), @XMM[7]
  1346. lea 0x80($inp), $inp
  1347. call _bsaes_decrypt8
  1348. movdqu @XMM[0], 0x00($out) # write output
  1349. movdqu @XMM[1], 0x10($out)
  1350. movdqu @XMM[6], 0x20($out)
  1351. movdqu @XMM[4], 0x30($out)
  1352. movdqu @XMM[2], 0x40($out)
  1353. movdqu @XMM[7], 0x50($out)
  1354. movdqu @XMM[3], 0x60($out)
  1355. movdqu @XMM[5], 0x70($out)
  1356. lea 0x80($out), $out
  1357. sub \$8,$len
  1358. jnc .Lecb_dec_loop
  1359. add \$8,$len
  1360. jz .Lecb_dec_done
  1361. movdqu 0x00($inp), @XMM[0] # load input
  1362. mov %rsp, %rax # pass key schedule
  1363. mov %ebx,%r10d # pass rounds
  1364. cmp \$2,$len
  1365. jb .Lecb_dec_one
  1366. movdqu 0x10($inp), @XMM[1]
  1367. je .Lecb_dec_two
  1368. movdqu 0x20($inp), @XMM[2]
  1369. cmp \$4,$len
  1370. jb .Lecb_dec_three
  1371. movdqu 0x30($inp), @XMM[3]
  1372. je .Lecb_dec_four
  1373. movdqu 0x40($inp), @XMM[4]
  1374. cmp \$6,$len
  1375. jb .Lecb_dec_five
  1376. movdqu 0x50($inp), @XMM[5]
  1377. je .Lecb_dec_six
  1378. movdqu 0x60($inp), @XMM[6]
  1379. call _bsaes_decrypt8
  1380. movdqu @XMM[0], 0x00($out) # write output
  1381. movdqu @XMM[1], 0x10($out)
  1382. movdqu @XMM[6], 0x20($out)
  1383. movdqu @XMM[4], 0x30($out)
  1384. movdqu @XMM[2], 0x40($out)
  1385. movdqu @XMM[7], 0x50($out)
  1386. movdqu @XMM[3], 0x60($out)
  1387. jmp .Lecb_dec_done
  1388. .align 16
  1389. .Lecb_dec_six:
  1390. call _bsaes_decrypt8
  1391. movdqu @XMM[0], 0x00($out) # write output
  1392. movdqu @XMM[1], 0x10($out)
  1393. movdqu @XMM[6], 0x20($out)
  1394. movdqu @XMM[4], 0x30($out)
  1395. movdqu @XMM[2], 0x40($out)
  1396. movdqu @XMM[7], 0x50($out)
  1397. jmp .Lecb_dec_done
  1398. .align 16
  1399. .Lecb_dec_five:
  1400. call _bsaes_decrypt8
  1401. movdqu @XMM[0], 0x00($out) # write output
  1402. movdqu @XMM[1], 0x10($out)
  1403. movdqu @XMM[6], 0x20($out)
  1404. movdqu @XMM[4], 0x30($out)
  1405. movdqu @XMM[2], 0x40($out)
  1406. jmp .Lecb_dec_done
  1407. .align 16
  1408. .Lecb_dec_four:
  1409. call _bsaes_decrypt8
  1410. movdqu @XMM[0], 0x00($out) # write output
  1411. movdqu @XMM[1], 0x10($out)
  1412. movdqu @XMM[6], 0x20($out)
  1413. movdqu @XMM[4], 0x30($out)
  1414. jmp .Lecb_dec_done
  1415. .align 16
  1416. .Lecb_dec_three:
  1417. call _bsaes_decrypt8
  1418. movdqu @XMM[0], 0x00($out) # write output
  1419. movdqu @XMM[1], 0x10($out)
  1420. movdqu @XMM[6], 0x20($out)
  1421. jmp .Lecb_dec_done
  1422. .align 16
  1423. .Lecb_dec_two:
  1424. call _bsaes_decrypt8
  1425. movdqu @XMM[0], 0x00($out) # write output
  1426. movdqu @XMM[1], 0x10($out)
  1427. jmp .Lecb_dec_done
  1428. .align 16
  1429. .Lecb_dec_one:
  1430. call _bsaes_decrypt8
  1431. movdqu @XMM[0], 0x00($out) # write output
  1432. jmp .Lecb_dec_done
  1433. .align 16
  1434. .Lecb_dec_short:
  1435. lea ($inp), $arg1
  1436. lea ($out), $arg2
  1437. lea ($key), $arg3
  1438. call asm_AES_decrypt
  1439. lea 16($inp), $inp
  1440. lea 16($out), $out
  1441. dec $len
  1442. jnz .Lecb_dec_short
  1443. .Lecb_dec_done:
  1444. lea (%rsp),%rax
  1445. pxor %xmm0, %xmm0
  1446. .Lecb_dec_bzero: # wipe key schedule [if any]
  1447. movdqa %xmm0, 0x00(%rax)
  1448. movdqa %xmm0, 0x10(%rax)
  1449. lea 0x20(%rax), %rax
  1450. cmp %rax, %rbp
  1451. jb .Lecb_dec_bzero
  1452. lea 0x78(%rbp),%rax
  1453. .cfi_def_cfa %rax,8
  1454. ___
  1455. $code.=<<___ if ($win64);
  1456. movaps 0x40(%rbp), %xmm6
  1457. movaps 0x50(%rbp), %xmm7
  1458. movaps 0x60(%rbp), %xmm8
  1459. movaps 0x70(%rbp), %xmm9
  1460. movaps 0x80(%rbp), %xmm10
  1461. movaps 0x90(%rbp), %xmm11
  1462. movaps 0xa0(%rbp), %xmm12
  1463. movaps 0xb0(%rbp), %xmm13
  1464. movaps 0xc0(%rbp), %xmm14
  1465. movaps 0xd0(%rbp), %xmm15
  1466. lea 0xa0(%rax), %rax
  1467. .Lecb_dec_tail:
  1468. ___
  1469. $code.=<<___;
  1470. mov -48(%rax), %r15
  1471. .cfi_restore %r15
  1472. mov -40(%rax), %r14
  1473. .cfi_restore %r14
  1474. mov -32(%rax), %r13
  1475. .cfi_restore %r13
  1476. mov -24(%rax), %r12
  1477. .cfi_restore %r12
  1478. mov -16(%rax), %rbx
  1479. .cfi_restore %rbx
  1480. mov -8(%rax), %rbp
  1481. .cfi_restore %rbp
  1482. lea (%rax), %rsp # restore %rsp
  1483. .cfi_def_cfa_register %rsp
  1484. .Lecb_dec_epilogue:
  1485. ret
  1486. .cfi_endproc
  1487. .size bsaes_ecb_decrypt_blocks,.-bsaes_ecb_decrypt_blocks
  1488. ___
  1489. }
  1490. $code.=<<___;
  1491. .extern asm_AES_cbc_encrypt
  1492. .globl bsaes_cbc_encrypt
  1493. .type bsaes_cbc_encrypt,\@abi-omnipotent
  1494. .align 16
  1495. bsaes_cbc_encrypt:
  1496. .cfi_startproc
  1497. ___
  1498. $code.=<<___ if ($win64);
  1499. mov 48(%rsp),$arg6 # pull direction flag
  1500. ___
  1501. $code.=<<___;
  1502. cmp \$0,$arg6
  1503. jne asm_AES_cbc_encrypt
  1504. cmp \$128,$arg3
  1505. jb asm_AES_cbc_encrypt
  1506. mov %rsp, %rax
  1507. .Lcbc_dec_prologue:
  1508. push %rbp
  1509. .cfi_push %rbp
  1510. push %rbx
  1511. .cfi_push %rbx
  1512. push %r12
  1513. .cfi_push %r12
  1514. push %r13
  1515. .cfi_push %r13
  1516. push %r14
  1517. .cfi_push %r14
  1518. push %r15
  1519. .cfi_push %r15
  1520. lea -0x48(%rsp), %rsp
  1521. .cfi_adjust_cfa_offset 0x48
  1522. ___
  1523. $code.=<<___ if ($win64);
  1524. mov 0xa0(%rsp),$arg5 # pull ivp
  1525. lea -0xa0(%rsp), %rsp
  1526. movaps %xmm6, 0x40(%rsp)
  1527. movaps %xmm7, 0x50(%rsp)
  1528. movaps %xmm8, 0x60(%rsp)
  1529. movaps %xmm9, 0x70(%rsp)
  1530. movaps %xmm10, 0x80(%rsp)
  1531. movaps %xmm11, 0x90(%rsp)
  1532. movaps %xmm12, 0xa0(%rsp)
  1533. movaps %xmm13, 0xb0(%rsp)
  1534. movaps %xmm14, 0xc0(%rsp)
  1535. movaps %xmm15, 0xd0(%rsp)
  1536. .Lcbc_dec_body:
  1537. ___
  1538. $code.=<<___;
  1539. mov %rsp, %rbp # backup %rsp
  1540. .cfi_def_cfa_register %rbp
  1541. mov 240($arg4), %eax # rounds
  1542. mov $arg1, $inp # backup arguments
  1543. mov $arg2, $out
  1544. mov $arg3, $len
  1545. mov $arg4, $key
  1546. mov $arg5, %rbx
  1547. shr \$4, $len # bytes to blocks
  1548. mov %eax, %edx # rounds
  1549. shl \$7, %rax # 128 bytes per inner round key
  1550. sub \$`128-32`, %rax # size of bit-sliced key schedule
  1551. sub %rax, %rsp
  1552. mov %rsp, %rax # pass key schedule
  1553. mov $key, %rcx # pass key
  1554. mov %edx, %r10d # pass rounds
  1555. call _bsaes_key_convert
  1556. pxor (%rsp),%xmm7 # fix up 0 round key
  1557. movdqa %xmm6,(%rax) # save last round key
  1558. movdqa %xmm7,(%rsp)
  1559. movdqu (%rbx), @XMM[15] # load IV
  1560. sub \$8,$len
  1561. .Lcbc_dec_loop:
  1562. movdqu 0x00($inp), @XMM[0] # load input
  1563. movdqu 0x10($inp), @XMM[1]
  1564. movdqu 0x20($inp), @XMM[2]
  1565. movdqu 0x30($inp), @XMM[3]
  1566. movdqu 0x40($inp), @XMM[4]
  1567. movdqu 0x50($inp), @XMM[5]
  1568. mov %rsp, %rax # pass key schedule
  1569. movdqu 0x60($inp), @XMM[6]
  1570. mov %edx,%r10d # pass rounds
  1571. movdqu 0x70($inp), @XMM[7]
  1572. movdqa @XMM[15], 0x20(%rbp) # put aside IV
  1573. call _bsaes_decrypt8
  1574. pxor 0x20(%rbp), @XMM[0] # ^= IV
  1575. movdqu 0x00($inp), @XMM[8] # re-load input
  1576. movdqu 0x10($inp), @XMM[9]
  1577. pxor @XMM[8], @XMM[1]
  1578. movdqu 0x20($inp), @XMM[10]
  1579. pxor @XMM[9], @XMM[6]
  1580. movdqu 0x30($inp), @XMM[11]
  1581. pxor @XMM[10], @XMM[4]
  1582. movdqu 0x40($inp), @XMM[12]
  1583. pxor @XMM[11], @XMM[2]
  1584. movdqu 0x50($inp), @XMM[13]
  1585. pxor @XMM[12], @XMM[7]
  1586. movdqu 0x60($inp), @XMM[14]
  1587. pxor @XMM[13], @XMM[3]
  1588. movdqu 0x70($inp), @XMM[15] # IV
  1589. pxor @XMM[14], @XMM[5]
  1590. movdqu @XMM[0], 0x00($out) # write output
  1591. lea 0x80($inp), $inp
  1592. movdqu @XMM[1], 0x10($out)
  1593. movdqu @XMM[6], 0x20($out)
  1594. movdqu @XMM[4], 0x30($out)
  1595. movdqu @XMM[2], 0x40($out)
  1596. movdqu @XMM[7], 0x50($out)
  1597. movdqu @XMM[3], 0x60($out)
  1598. movdqu @XMM[5], 0x70($out)
  1599. lea 0x80($out), $out
  1600. sub \$8,$len
  1601. jnc .Lcbc_dec_loop
  1602. add \$8,$len
  1603. jz .Lcbc_dec_done
  1604. movdqu 0x00($inp), @XMM[0] # load input
  1605. mov %rsp, %rax # pass key schedule
  1606. mov %edx, %r10d # pass rounds
  1607. cmp \$2,$len
  1608. jb .Lcbc_dec_one
  1609. movdqu 0x10($inp), @XMM[1]
  1610. je .Lcbc_dec_two
  1611. movdqu 0x20($inp), @XMM[2]
  1612. cmp \$4,$len
  1613. jb .Lcbc_dec_three
  1614. movdqu 0x30($inp), @XMM[3]
  1615. je .Lcbc_dec_four
  1616. movdqu 0x40($inp), @XMM[4]
  1617. cmp \$6,$len
  1618. jb .Lcbc_dec_five
  1619. movdqu 0x50($inp), @XMM[5]
  1620. je .Lcbc_dec_six
  1621. movdqu 0x60($inp), @XMM[6]
  1622. movdqa @XMM[15], 0x20(%rbp) # put aside IV
  1623. call _bsaes_decrypt8
  1624. pxor 0x20(%rbp), @XMM[0] # ^= IV
  1625. movdqu 0x00($inp), @XMM[8] # re-load input
  1626. movdqu 0x10($inp), @XMM[9]
  1627. pxor @XMM[8], @XMM[1]
  1628. movdqu 0x20($inp), @XMM[10]
  1629. pxor @XMM[9], @XMM[6]
  1630. movdqu 0x30($inp), @XMM[11]
  1631. pxor @XMM[10], @XMM[4]
  1632. movdqu 0x40($inp), @XMM[12]
  1633. pxor @XMM[11], @XMM[2]
  1634. movdqu 0x50($inp), @XMM[13]
  1635. pxor @XMM[12], @XMM[7]
  1636. movdqu 0x60($inp), @XMM[15] # IV
  1637. pxor @XMM[13], @XMM[3]
  1638. movdqu @XMM[0], 0x00($out) # write output
  1639. movdqu @XMM[1], 0x10($out)
  1640. movdqu @XMM[6], 0x20($out)
  1641. movdqu @XMM[4], 0x30($out)
  1642. movdqu @XMM[2], 0x40($out)
  1643. movdqu @XMM[7], 0x50($out)
  1644. movdqu @XMM[3], 0x60($out)
  1645. jmp .Lcbc_dec_done
  1646. .align 16
  1647. .Lcbc_dec_six:
  1648. movdqa @XMM[15], 0x20(%rbp) # put aside IV
  1649. call _bsaes_decrypt8
  1650. pxor 0x20(%rbp), @XMM[0] # ^= IV
  1651. movdqu 0x00($inp), @XMM[8] # re-load input
  1652. movdqu 0x10($inp), @XMM[9]
  1653. pxor @XMM[8], @XMM[1]
  1654. movdqu 0x20($inp), @XMM[10]
  1655. pxor @XMM[9], @XMM[6]
  1656. movdqu 0x30($inp), @XMM[11]
  1657. pxor @XMM[10], @XMM[4]
  1658. movdqu 0x40($inp), @XMM[12]
  1659. pxor @XMM[11], @XMM[2]
  1660. movdqu 0x50($inp), @XMM[15] # IV
  1661. pxor @XMM[12], @XMM[7]
  1662. movdqu @XMM[0], 0x00($out) # write output
  1663. movdqu @XMM[1], 0x10($out)
  1664. movdqu @XMM[6], 0x20($out)
  1665. movdqu @XMM[4], 0x30($out)
  1666. movdqu @XMM[2], 0x40($out)
  1667. movdqu @XMM[7], 0x50($out)
  1668. jmp .Lcbc_dec_done
  1669. .align 16
  1670. .Lcbc_dec_five:
  1671. movdqa @XMM[15], 0x20(%rbp) # put aside IV
  1672. call _bsaes_decrypt8
  1673. pxor 0x20(%rbp), @XMM[0] # ^= IV
  1674. movdqu 0x00($inp), @XMM[8] # re-load input
  1675. movdqu 0x10($inp), @XMM[9]
  1676. pxor @XMM[8], @XMM[1]
  1677. movdqu 0x20($inp), @XMM[10]
  1678. pxor @XMM[9], @XMM[6]
  1679. movdqu 0x30($inp), @XMM[11]
  1680. pxor @XMM[10], @XMM[4]
  1681. movdqu 0x40($inp), @XMM[15] # IV
  1682. pxor @XMM[11], @XMM[2]
  1683. movdqu @XMM[0], 0x00($out) # write output
  1684. movdqu @XMM[1], 0x10($out)
  1685. movdqu @XMM[6], 0x20($out)
  1686. movdqu @XMM[4], 0x30($out)
  1687. movdqu @XMM[2], 0x40($out)
  1688. jmp .Lcbc_dec_done
  1689. .align 16
  1690. .Lcbc_dec_four:
  1691. movdqa @XMM[15], 0x20(%rbp) # put aside IV
  1692. call _bsaes_decrypt8
  1693. pxor 0x20(%rbp), @XMM[0] # ^= IV
  1694. movdqu 0x00($inp), @XMM[8] # re-load input
  1695. movdqu 0x10($inp), @XMM[9]
  1696. pxor @XMM[8], @XMM[1]
  1697. movdqu 0x20($inp), @XMM[10]
  1698. pxor @XMM[9], @XMM[6]
  1699. movdqu 0x30($inp), @XMM[15] # IV
  1700. pxor @XMM[10], @XMM[4]
  1701. movdqu @XMM[0], 0x00($out) # write output
  1702. movdqu @XMM[1], 0x10($out)
  1703. movdqu @XMM[6], 0x20($out)
  1704. movdqu @XMM[4], 0x30($out)
  1705. jmp .Lcbc_dec_done
  1706. .align 16
  1707. .Lcbc_dec_three:
  1708. movdqa @XMM[15], 0x20(%rbp) # put aside IV
  1709. call _bsaes_decrypt8
  1710. pxor 0x20(%rbp), @XMM[0] # ^= IV
  1711. movdqu 0x00($inp), @XMM[8] # re-load input
  1712. movdqu 0x10($inp), @XMM[9]
  1713. pxor @XMM[8], @XMM[1]
  1714. movdqu 0x20($inp), @XMM[15] # IV
  1715. pxor @XMM[9], @XMM[6]
  1716. movdqu @XMM[0], 0x00($out) # write output
  1717. movdqu @XMM[1], 0x10($out)
  1718. movdqu @XMM[6], 0x20($out)
  1719. jmp .Lcbc_dec_done
  1720. .align 16
  1721. .Lcbc_dec_two:
  1722. movdqa @XMM[15], 0x20(%rbp) # put aside IV
  1723. call _bsaes_decrypt8
  1724. pxor 0x20(%rbp), @XMM[0] # ^= IV
  1725. movdqu 0x00($inp), @XMM[8] # re-load input
  1726. movdqu 0x10($inp), @XMM[15] # IV
  1727. pxor @XMM[8], @XMM[1]
  1728. movdqu @XMM[0], 0x00($out) # write output
  1729. movdqu @XMM[1], 0x10($out)
  1730. jmp .Lcbc_dec_done
  1731. .align 16
  1732. .Lcbc_dec_one:
  1733. lea ($inp), $arg1
  1734. lea 0x20(%rbp), $arg2 # buffer output
  1735. lea ($key), $arg3
  1736. call asm_AES_decrypt # doesn't touch %xmm
  1737. pxor 0x20(%rbp), @XMM[15] # ^= IV
  1738. movdqu @XMM[15], ($out) # write output
  1739. movdqa @XMM[0], @XMM[15] # IV
  1740. .Lcbc_dec_done:
  1741. movdqu @XMM[15], (%rbx) # return IV
  1742. lea (%rsp), %rax
  1743. pxor %xmm0, %xmm0
  1744. .Lcbc_dec_bzero: # wipe key schedule [if any]
  1745. movdqa %xmm0, 0x00(%rax)
  1746. movdqa %xmm0, 0x10(%rax)
  1747. lea 0x20(%rax), %rax
  1748. cmp %rax, %rbp
  1749. ja .Lcbc_dec_bzero
  1750. lea 0x78(%rbp),%rax
  1751. .cfi_def_cfa %rax,8
  1752. ___
  1753. $code.=<<___ if ($win64);
  1754. movaps 0x40(%rbp), %xmm6
  1755. movaps 0x50(%rbp), %xmm7
  1756. movaps 0x60(%rbp), %xmm8
  1757. movaps 0x70(%rbp), %xmm9
  1758. movaps 0x80(%rbp), %xmm10
  1759. movaps 0x90(%rbp), %xmm11
  1760. movaps 0xa0(%rbp), %xmm12
  1761. movaps 0xb0(%rbp), %xmm13
  1762. movaps 0xc0(%rbp), %xmm14
  1763. movaps 0xd0(%rbp), %xmm15
  1764. lea 0xa0(%rax), %rax
  1765. .Lcbc_dec_tail:
  1766. ___
  1767. $code.=<<___;
  1768. mov -48(%rax), %r15
  1769. .cfi_restore %r15
  1770. mov -40(%rax), %r14
  1771. .cfi_restore %r14
  1772. mov -32(%rax), %r13
  1773. .cfi_restore %r13
  1774. mov -24(%rax), %r12
  1775. .cfi_restore %r12
  1776. mov -16(%rax), %rbx
  1777. .cfi_restore %rbx
  1778. mov -8(%rax), %rbp
  1779. .cfi_restore %rbp
  1780. lea (%rax), %rsp # restore %rsp
  1781. .cfi_def_cfa_register %rsp
  1782. .Lcbc_dec_epilogue:
  1783. ret
  1784. .cfi_endproc
  1785. .size bsaes_cbc_encrypt,.-bsaes_cbc_encrypt
  1786. .globl bsaes_ctr32_encrypt_blocks
  1787. .type bsaes_ctr32_encrypt_blocks,\@abi-omnipotent
  1788. .align 16
  1789. bsaes_ctr32_encrypt_blocks:
  1790. .cfi_startproc
  1791. mov %rsp, %rax
  1792. .Lctr_enc_prologue:
  1793. push %rbp
  1794. .cfi_push %rbp
  1795. push %rbx
  1796. .cfi_push %rbx
  1797. push %r12
  1798. .cfi_push %r12
  1799. push %r13
  1800. .cfi_push %r13
  1801. push %r14
  1802. .cfi_push %r14
  1803. push %r15
  1804. .cfi_push %r15
  1805. lea -0x48(%rsp), %rsp
  1806. .cfi_adjust_cfa_offset 0x48
  1807. ___
  1808. $code.=<<___ if ($win64);
  1809. mov 0xa0(%rsp),$arg5 # pull ivp
  1810. lea -0xa0(%rsp), %rsp
  1811. movaps %xmm6, 0x40(%rsp)
  1812. movaps %xmm7, 0x50(%rsp)
  1813. movaps %xmm8, 0x60(%rsp)
  1814. movaps %xmm9, 0x70(%rsp)
  1815. movaps %xmm10, 0x80(%rsp)
  1816. movaps %xmm11, 0x90(%rsp)
  1817. movaps %xmm12, 0xa0(%rsp)
  1818. movaps %xmm13, 0xb0(%rsp)
  1819. movaps %xmm14, 0xc0(%rsp)
  1820. movaps %xmm15, 0xd0(%rsp)
  1821. .Lctr_enc_body:
  1822. ___
  1823. $code.=<<___;
  1824. mov %rsp, %rbp # backup %rsp
  1825. .cfi_def_cfa_register %rbp
  1826. movdqu ($arg5), %xmm0 # load counter
  1827. mov 240($arg4), %eax # rounds
  1828. mov $arg1, $inp # backup arguments
  1829. mov $arg2, $out
  1830. mov $arg3, $len
  1831. mov $arg4, $key
  1832. movdqa %xmm0, 0x20(%rbp) # copy counter
  1833. cmp \$8, $arg3
  1834. jb .Lctr_enc_short
  1835. mov %eax, %ebx # rounds
  1836. shl \$7, %rax # 128 bytes per inner round key
  1837. sub \$`128-32`, %rax # size of bit-sliced key schedule
  1838. sub %rax, %rsp
  1839. mov %rsp, %rax # pass key schedule
  1840. mov $key, %rcx # pass key
  1841. mov %ebx, %r10d # pass rounds
  1842. call _bsaes_key_convert
  1843. pxor %xmm6,%xmm7 # fix up last round key
  1844. movdqa %xmm7,(%rax) # save last round key
  1845. movdqa (%rsp), @XMM[9] # load round0 key
  1846. lea .LADD1(%rip), %r11
  1847. movdqa 0x20(%rbp), @XMM[0] # counter copy
  1848. movdqa -0x20(%r11), @XMM[8] # .LSWPUP
  1849. pshufb @XMM[8], @XMM[9] # byte swap upper part
  1850. pshufb @XMM[8], @XMM[0]
  1851. movdqa @XMM[9], (%rsp) # save adjusted round0 key
  1852. jmp .Lctr_enc_loop
  1853. .align 16
  1854. .Lctr_enc_loop:
  1855. movdqa @XMM[0], 0x20(%rbp) # save counter
  1856. movdqa @XMM[0], @XMM[1] # prepare 8 counter values
  1857. movdqa @XMM[0], @XMM[2]
  1858. paddd 0x00(%r11), @XMM[1] # .LADD1
  1859. movdqa @XMM[0], @XMM[3]
  1860. paddd 0x10(%r11), @XMM[2] # .LADD2
  1861. movdqa @XMM[0], @XMM[4]
  1862. paddd 0x20(%r11), @XMM[3] # .LADD3
  1863. movdqa @XMM[0], @XMM[5]
  1864. paddd 0x30(%r11), @XMM[4] # .LADD4
  1865. movdqa @XMM[0], @XMM[6]
  1866. paddd 0x40(%r11), @XMM[5] # .LADD5
  1867. movdqa @XMM[0], @XMM[7]
  1868. paddd 0x50(%r11), @XMM[6] # .LADD6
  1869. paddd 0x60(%r11), @XMM[7] # .LADD7
  1870. # Borrow prologue from _bsaes_encrypt8 to use the opportunity
  1871. # to flip byte order in 32-bit counter
  1872. movdqa (%rsp), @XMM[9] # round 0 key
  1873. lea 0x10(%rsp), %rax # pass key schedule
  1874. movdqa -0x10(%r11), @XMM[8] # .LSWPUPM0SR
  1875. pxor @XMM[9], @XMM[0] # xor with round0 key
  1876. pxor @XMM[9], @XMM[1]
  1877. pxor @XMM[9], @XMM[2]
  1878. pxor @XMM[9], @XMM[3]
  1879. pshufb @XMM[8], @XMM[0]
  1880. pshufb @XMM[8], @XMM[1]
  1881. pxor @XMM[9], @XMM[4]
  1882. pxor @XMM[9], @XMM[5]
  1883. pshufb @XMM[8], @XMM[2]
  1884. pshufb @XMM[8], @XMM[3]
  1885. pxor @XMM[9], @XMM[6]
  1886. pxor @XMM[9], @XMM[7]
  1887. pshufb @XMM[8], @XMM[4]
  1888. pshufb @XMM[8], @XMM[5]
  1889. pshufb @XMM[8], @XMM[6]
  1890. pshufb @XMM[8], @XMM[7]
  1891. lea .LBS0(%rip), %r11 # constants table
  1892. mov %ebx,%r10d # pass rounds
  1893. call _bsaes_encrypt8_bitslice
  1894. sub \$8,$len
  1895. jc .Lctr_enc_loop_done
  1896. movdqu 0x00($inp), @XMM[8] # load input
  1897. movdqu 0x10($inp), @XMM[9]
  1898. movdqu 0x20($inp), @XMM[10]
  1899. movdqu 0x30($inp), @XMM[11]
  1900. movdqu 0x40($inp), @XMM[12]
  1901. movdqu 0x50($inp), @XMM[13]
  1902. movdqu 0x60($inp), @XMM[14]
  1903. movdqu 0x70($inp), @XMM[15]
  1904. lea 0x80($inp),$inp
  1905. pxor @XMM[0], @XMM[8]
  1906. movdqa 0x20(%rbp), @XMM[0] # load counter
  1907. pxor @XMM[9], @XMM[1]
  1908. movdqu @XMM[8], 0x00($out) # write output
  1909. pxor @XMM[10], @XMM[4]
  1910. movdqu @XMM[1], 0x10($out)
  1911. pxor @XMM[11], @XMM[6]
  1912. movdqu @XMM[4], 0x20($out)
  1913. pxor @XMM[12], @XMM[3]
  1914. movdqu @XMM[6], 0x30($out)
  1915. pxor @XMM[13], @XMM[7]
  1916. movdqu @XMM[3], 0x40($out)
  1917. pxor @XMM[14], @XMM[2]
  1918. movdqu @XMM[7], 0x50($out)
  1919. pxor @XMM[15], @XMM[5]
  1920. movdqu @XMM[2], 0x60($out)
  1921. lea .LADD1(%rip), %r11
  1922. movdqu @XMM[5], 0x70($out)
  1923. lea 0x80($out), $out
  1924. paddd 0x70(%r11), @XMM[0] # .LADD8
  1925. jnz .Lctr_enc_loop
  1926. jmp .Lctr_enc_done
  1927. .align 16
  1928. .Lctr_enc_loop_done:
  1929. add \$8, $len
  1930. movdqu 0x00($inp), @XMM[8] # load input
  1931. pxor @XMM[8], @XMM[0]
  1932. movdqu @XMM[0], 0x00($out) # write output
  1933. cmp \$2,$len
  1934. jb .Lctr_enc_done
  1935. movdqu 0x10($inp), @XMM[9]
  1936. pxor @XMM[9], @XMM[1]
  1937. movdqu @XMM[1], 0x10($out)
  1938. je .Lctr_enc_done
  1939. movdqu 0x20($inp), @XMM[10]
  1940. pxor @XMM[10], @XMM[4]
  1941. movdqu @XMM[4], 0x20($out)
  1942. cmp \$4,$len
  1943. jb .Lctr_enc_done
  1944. movdqu 0x30($inp), @XMM[11]
  1945. pxor @XMM[11], @XMM[6]
  1946. movdqu @XMM[6], 0x30($out)
  1947. je .Lctr_enc_done
  1948. movdqu 0x40($inp), @XMM[12]
  1949. pxor @XMM[12], @XMM[3]
  1950. movdqu @XMM[3], 0x40($out)
  1951. cmp \$6,$len
  1952. jb .Lctr_enc_done
  1953. movdqu 0x50($inp), @XMM[13]
  1954. pxor @XMM[13], @XMM[7]
  1955. movdqu @XMM[7], 0x50($out)
  1956. je .Lctr_enc_done
  1957. movdqu 0x60($inp), @XMM[14]
  1958. pxor @XMM[14], @XMM[2]
  1959. movdqu @XMM[2], 0x60($out)
  1960. jmp .Lctr_enc_done
  1961. .align 16
  1962. .Lctr_enc_short:
  1963. lea 0x20(%rbp), $arg1
  1964. lea 0x30(%rbp), $arg2
  1965. lea ($key), $arg3
  1966. call asm_AES_encrypt
  1967. movdqu ($inp), @XMM[1]
  1968. lea 16($inp), $inp
  1969. mov 0x2c(%rbp), %eax # load 32-bit counter
  1970. bswap %eax
  1971. pxor 0x30(%rbp), @XMM[1]
  1972. inc %eax # increment
  1973. movdqu @XMM[1], ($out)
  1974. bswap %eax
  1975. lea 16($out), $out
  1976. mov %eax, 0x2c(%rsp) # save 32-bit counter
  1977. dec $len
  1978. jnz .Lctr_enc_short
  1979. .Lctr_enc_done:
  1980. lea (%rsp), %rax
  1981. pxor %xmm0, %xmm0
  1982. .Lctr_enc_bzero: # wipe key schedule [if any]
  1983. movdqa %xmm0, 0x00(%rax)
  1984. movdqa %xmm0, 0x10(%rax)
  1985. lea 0x20(%rax), %rax
  1986. cmp %rax, %rbp
  1987. ja .Lctr_enc_bzero
  1988. lea 0x78(%rbp),%rax
  1989. .cfi_def_cfa %rax,8
  1990. ___
  1991. $code.=<<___ if ($win64);
  1992. movaps 0x40(%rbp), %xmm6
  1993. movaps 0x50(%rbp), %xmm7
  1994. movaps 0x60(%rbp), %xmm8
  1995. movaps 0x70(%rbp), %xmm9
  1996. movaps 0x80(%rbp), %xmm10
  1997. movaps 0x90(%rbp), %xmm11
  1998. movaps 0xa0(%rbp), %xmm12
  1999. movaps 0xb0(%rbp), %xmm13
  2000. movaps 0xc0(%rbp), %xmm14
  2001. movaps 0xd0(%rbp), %xmm15
  2002. lea 0xa0(%rax), %rax
  2003. .Lctr_enc_tail:
  2004. ___
  2005. $code.=<<___;
  2006. mov -48(%rax), %r15
  2007. .cfi_restore %r15
  2008. mov -40(%rax), %r14
  2009. .cfi_restore %r14
  2010. mov -32(%rax), %r13
  2011. .cfi_restore %r13
  2012. mov -24(%rax), %r12
  2013. .cfi_restore %r12
  2014. mov -16(%rax), %rbx
  2015. .cfi_restore %rbx
  2016. mov -8(%rax), %rbp
  2017. .cfi_restore %rbp
  2018. lea (%rax), %rsp # restore %rsp
  2019. .cfi_def_cfa_register %rsp
  2020. .Lctr_enc_epilogue:
  2021. ret
  2022. .cfi_endproc
  2023. .size bsaes_ctr32_encrypt_blocks,.-bsaes_ctr32_encrypt_blocks
  2024. ___
  2025. ######################################################################
  2026. # void bsaes_xts_[en|de]crypt(const char *inp,char *out,size_t len,
  2027. # const AES_KEY *key1, const AES_KEY *key2,
  2028. # const unsigned char iv[16]);
  2029. #
  2030. my ($twmask,$twres,$twtmp)=@XMM[13..15];
  2031. $arg6=~s/d$//;
  2032. $code.=<<___;
  2033. .globl bsaes_xts_encrypt
  2034. .type bsaes_xts_encrypt,\@abi-omnipotent
  2035. .align 16
  2036. bsaes_xts_encrypt:
  2037. .cfi_startproc
  2038. mov %rsp, %rax
  2039. .Lxts_enc_prologue:
  2040. push %rbp
  2041. .cfi_push %rbp
  2042. push %rbx
  2043. .cfi_push %rbx
  2044. push %r12
  2045. .cfi_push %r12
  2046. push %r13
  2047. .cfi_push %r13
  2048. push %r14
  2049. .cfi_push %r14
  2050. push %r15
  2051. .cfi_push %r15
  2052. lea -0x48(%rsp), %rsp
  2053. .cfi_adjust_cfa_offset 0x48
  2054. ___
  2055. $code.=<<___ if ($win64);
  2056. mov 0xa0(%rsp),$arg5 # pull key2
  2057. mov 0xa8(%rsp),$arg6 # pull ivp
  2058. lea -0xa0(%rsp), %rsp
  2059. movaps %xmm6, 0x40(%rsp)
  2060. movaps %xmm7, 0x50(%rsp)
  2061. movaps %xmm8, 0x60(%rsp)
  2062. movaps %xmm9, 0x70(%rsp)
  2063. movaps %xmm10, 0x80(%rsp)
  2064. movaps %xmm11, 0x90(%rsp)
  2065. movaps %xmm12, 0xa0(%rsp)
  2066. movaps %xmm13, 0xb0(%rsp)
  2067. movaps %xmm14, 0xc0(%rsp)
  2068. movaps %xmm15, 0xd0(%rsp)
  2069. .Lxts_enc_body:
  2070. ___
  2071. $code.=<<___;
  2072. mov %rsp, %rbp # backup %rsp
  2073. .cfi_def_cfa_register %rbp
  2074. mov $arg1, $inp # backup arguments
  2075. mov $arg2, $out
  2076. mov $arg3, $len
  2077. mov $arg4, $key
  2078. lea ($arg6), $arg1
  2079. lea 0x20(%rbp), $arg2
  2080. lea ($arg5), $arg3
  2081. call asm_AES_encrypt # generate initial tweak
  2082. mov 240($key), %eax # rounds
  2083. mov $len, %rbx # backup $len
  2084. mov %eax, %edx # rounds
  2085. shl \$7, %rax # 128 bytes per inner round key
  2086. sub \$`128-32`, %rax # size of bit-sliced key schedule
  2087. sub %rax, %rsp
  2088. mov %rsp, %rax # pass key schedule
  2089. mov $key, %rcx # pass key
  2090. mov %edx, %r10d # pass rounds
  2091. call _bsaes_key_convert
  2092. pxor %xmm6, %xmm7 # fix up last round key
  2093. movdqa %xmm7, (%rax) # save last round key
  2094. and \$-16, $len
  2095. sub \$0x80, %rsp # place for tweak[8]
  2096. movdqa 0x20(%rbp), @XMM[7] # initial tweak
  2097. pxor $twtmp, $twtmp
  2098. movdqa .Lxts_magic(%rip), $twmask
  2099. pcmpgtd @XMM[7], $twtmp # broadcast upper bits
  2100. sub \$0x80, $len
  2101. jc .Lxts_enc_short
  2102. jmp .Lxts_enc_loop
  2103. .align 16
  2104. .Lxts_enc_loop:
  2105. ___
  2106. for ($i=0;$i<7;$i++) {
  2107. $code.=<<___;
  2108. pshufd \$0x13, $twtmp, $twres
  2109. pxor $twtmp, $twtmp
  2110. movdqa @XMM[7], @XMM[$i]
  2111. movdqa @XMM[7], `0x10*$i`(%rsp)# save tweak[$i]
  2112. paddq @XMM[7], @XMM[7] # psllq 1,$tweak
  2113. pand $twmask, $twres # isolate carry and residue
  2114. pcmpgtd @XMM[7], $twtmp # broadcast upper bits
  2115. pxor $twres, @XMM[7]
  2116. ___
  2117. $code.=<<___ if ($i>=1);
  2118. movdqu `0x10*($i-1)`($inp), @XMM[8+$i-1]
  2119. ___
  2120. $code.=<<___ if ($i>=2);
  2121. pxor @XMM[8+$i-2], @XMM[$i-2]# input[] ^ tweak[]
  2122. ___
  2123. }
  2124. $code.=<<___;
  2125. movdqu 0x60($inp), @XMM[8+6]
  2126. pxor @XMM[8+5], @XMM[5]
  2127. movdqu 0x70($inp), @XMM[8+7]
  2128. lea 0x80($inp), $inp
  2129. movdqa @XMM[7], 0x70(%rsp)
  2130. pxor @XMM[8+6], @XMM[6]
  2131. lea 0x80(%rsp), %rax # pass key schedule
  2132. pxor @XMM[8+7], @XMM[7]
  2133. mov %edx, %r10d # pass rounds
  2134. call _bsaes_encrypt8
  2135. pxor 0x00(%rsp), @XMM[0] # ^= tweak[]
  2136. pxor 0x10(%rsp), @XMM[1]
  2137. movdqu @XMM[0], 0x00($out) # write output
  2138. pxor 0x20(%rsp), @XMM[4]
  2139. movdqu @XMM[1], 0x10($out)
  2140. pxor 0x30(%rsp), @XMM[6]
  2141. movdqu @XMM[4], 0x20($out)
  2142. pxor 0x40(%rsp), @XMM[3]
  2143. movdqu @XMM[6], 0x30($out)
  2144. pxor 0x50(%rsp), @XMM[7]
  2145. movdqu @XMM[3], 0x40($out)
  2146. pxor 0x60(%rsp), @XMM[2]
  2147. movdqu @XMM[7], 0x50($out)
  2148. pxor 0x70(%rsp), @XMM[5]
  2149. movdqu @XMM[2], 0x60($out)
  2150. movdqu @XMM[5], 0x70($out)
  2151. lea 0x80($out), $out
  2152. movdqa 0x70(%rsp), @XMM[7] # prepare next iteration tweak
  2153. pxor $twtmp, $twtmp
  2154. movdqa .Lxts_magic(%rip), $twmask
  2155. pcmpgtd @XMM[7], $twtmp
  2156. pshufd \$0x13, $twtmp, $twres
  2157. pxor $twtmp, $twtmp
  2158. paddq @XMM[7], @XMM[7] # psllq 1,$tweak
  2159. pand $twmask, $twres # isolate carry and residue
  2160. pcmpgtd @XMM[7], $twtmp # broadcast upper bits
  2161. pxor $twres, @XMM[7]
  2162. sub \$0x80,$len
  2163. jnc .Lxts_enc_loop
  2164. .Lxts_enc_short:
  2165. add \$0x80, $len
  2166. jz .Lxts_enc_done
  2167. ___
  2168. for ($i=0;$i<7;$i++) {
  2169. $code.=<<___;
  2170. pshufd \$0x13, $twtmp, $twres
  2171. pxor $twtmp, $twtmp
  2172. movdqa @XMM[7], @XMM[$i]
  2173. movdqa @XMM[7], `0x10*$i`(%rsp)# save tweak[$i]
  2174. paddq @XMM[7], @XMM[7] # psllq 1,$tweak
  2175. pand $twmask, $twres # isolate carry and residue
  2176. pcmpgtd @XMM[7], $twtmp # broadcast upper bits
  2177. pxor $twres, @XMM[7]
  2178. ___
  2179. $code.=<<___ if ($i>=1);
  2180. movdqu `0x10*($i-1)`($inp), @XMM[8+$i-1]
  2181. cmp \$`0x10*$i`,$len
  2182. je .Lxts_enc_$i
  2183. ___
  2184. $code.=<<___ if ($i>=2);
  2185. pxor @XMM[8+$i-2], @XMM[$i-2]# input[] ^ tweak[]
  2186. ___
  2187. }
  2188. $code.=<<___;
  2189. movdqu 0x60($inp), @XMM[8+6]
  2190. pxor @XMM[8+5], @XMM[5]
  2191. movdqa @XMM[7], 0x70(%rsp)
  2192. lea 0x70($inp), $inp
  2193. pxor @XMM[8+6], @XMM[6]
  2194. lea 0x80(%rsp), %rax # pass key schedule
  2195. mov %edx, %r10d # pass rounds
  2196. call _bsaes_encrypt8
  2197. pxor 0x00(%rsp), @XMM[0] # ^= tweak[]
  2198. pxor 0x10(%rsp), @XMM[1]
  2199. movdqu @XMM[0], 0x00($out) # write output
  2200. pxor 0x20(%rsp), @XMM[4]
  2201. movdqu @XMM[1], 0x10($out)
  2202. pxor 0x30(%rsp), @XMM[6]
  2203. movdqu @XMM[4], 0x20($out)
  2204. pxor 0x40(%rsp), @XMM[3]
  2205. movdqu @XMM[6], 0x30($out)
  2206. pxor 0x50(%rsp), @XMM[7]
  2207. movdqu @XMM[3], 0x40($out)
  2208. pxor 0x60(%rsp), @XMM[2]
  2209. movdqu @XMM[7], 0x50($out)
  2210. movdqu @XMM[2], 0x60($out)
  2211. lea 0x70($out), $out
  2212. movdqa 0x70(%rsp), @XMM[7] # next iteration tweak
  2213. jmp .Lxts_enc_done
  2214. .align 16
  2215. .Lxts_enc_6:
  2216. pxor @XMM[8+4], @XMM[4]
  2217. lea 0x60($inp), $inp
  2218. pxor @XMM[8+5], @XMM[5]
  2219. lea 0x80(%rsp), %rax # pass key schedule
  2220. mov %edx, %r10d # pass rounds
  2221. call _bsaes_encrypt8
  2222. pxor 0x00(%rsp), @XMM[0] # ^= tweak[]
  2223. pxor 0x10(%rsp), @XMM[1]
  2224. movdqu @XMM[0], 0x00($out) # write output
  2225. pxor 0x20(%rsp), @XMM[4]
  2226. movdqu @XMM[1], 0x10($out)
  2227. pxor 0x30(%rsp), @XMM[6]
  2228. movdqu @XMM[4], 0x20($out)
  2229. pxor 0x40(%rsp), @XMM[3]
  2230. movdqu @XMM[6], 0x30($out)
  2231. pxor 0x50(%rsp), @XMM[7]
  2232. movdqu @XMM[3], 0x40($out)
  2233. movdqu @XMM[7], 0x50($out)
  2234. lea 0x60($out), $out
  2235. movdqa 0x60(%rsp), @XMM[7] # next iteration tweak
  2236. jmp .Lxts_enc_done
  2237. .align 16
  2238. .Lxts_enc_5:
  2239. pxor @XMM[8+3], @XMM[3]
  2240. lea 0x50($inp), $inp
  2241. pxor @XMM[8+4], @XMM[4]
  2242. lea 0x80(%rsp), %rax # pass key schedule
  2243. mov %edx, %r10d # pass rounds
  2244. call _bsaes_encrypt8
  2245. pxor 0x00(%rsp), @XMM[0] # ^= tweak[]
  2246. pxor 0x10(%rsp), @XMM[1]
  2247. movdqu @XMM[0], 0x00($out) # write output
  2248. pxor 0x20(%rsp), @XMM[4]
  2249. movdqu @XMM[1], 0x10($out)
  2250. pxor 0x30(%rsp), @XMM[6]
  2251. movdqu @XMM[4], 0x20($out)
  2252. pxor 0x40(%rsp), @XMM[3]
  2253. movdqu @XMM[6], 0x30($out)
  2254. movdqu @XMM[3], 0x40($out)
  2255. lea 0x50($out), $out
  2256. movdqa 0x50(%rsp), @XMM[7] # next iteration tweak
  2257. jmp .Lxts_enc_done
  2258. .align 16
  2259. .Lxts_enc_4:
  2260. pxor @XMM[8+2], @XMM[2]
  2261. lea 0x40($inp), $inp
  2262. pxor @XMM[8+3], @XMM[3]
  2263. lea 0x80(%rsp), %rax # pass key schedule
  2264. mov %edx, %r10d # pass rounds
  2265. call _bsaes_encrypt8
  2266. pxor 0x00(%rsp), @XMM[0] # ^= tweak[]
  2267. pxor 0x10(%rsp), @XMM[1]
  2268. movdqu @XMM[0], 0x00($out) # write output
  2269. pxor 0x20(%rsp), @XMM[4]
  2270. movdqu @XMM[1], 0x10($out)
  2271. pxor 0x30(%rsp), @XMM[6]
  2272. movdqu @XMM[4], 0x20($out)
  2273. movdqu @XMM[6], 0x30($out)
  2274. lea 0x40($out), $out
  2275. movdqa 0x40(%rsp), @XMM[7] # next iteration tweak
  2276. jmp .Lxts_enc_done
  2277. .align 16
  2278. .Lxts_enc_3:
  2279. pxor @XMM[8+1], @XMM[1]
  2280. lea 0x30($inp), $inp
  2281. pxor @XMM[8+2], @XMM[2]
  2282. lea 0x80(%rsp), %rax # pass key schedule
  2283. mov %edx, %r10d # pass rounds
  2284. call _bsaes_encrypt8
  2285. pxor 0x00(%rsp), @XMM[0] # ^= tweak[]
  2286. pxor 0x10(%rsp), @XMM[1]
  2287. movdqu @XMM[0], 0x00($out) # write output
  2288. pxor 0x20(%rsp), @XMM[4]
  2289. movdqu @XMM[1], 0x10($out)
  2290. movdqu @XMM[4], 0x20($out)
  2291. lea 0x30($out), $out
  2292. movdqa 0x30(%rsp), @XMM[7] # next iteration tweak
  2293. jmp .Lxts_enc_done
  2294. .align 16
  2295. .Lxts_enc_2:
  2296. pxor @XMM[8+0], @XMM[0]
  2297. lea 0x20($inp), $inp
  2298. pxor @XMM[8+1], @XMM[1]
  2299. lea 0x80(%rsp), %rax # pass key schedule
  2300. mov %edx, %r10d # pass rounds
  2301. call _bsaes_encrypt8
  2302. pxor 0x00(%rsp), @XMM[0] # ^= tweak[]
  2303. pxor 0x10(%rsp), @XMM[1]
  2304. movdqu @XMM[0], 0x00($out) # write output
  2305. movdqu @XMM[1], 0x10($out)
  2306. lea 0x20($out), $out
  2307. movdqa 0x20(%rsp), @XMM[7] # next iteration tweak
  2308. jmp .Lxts_enc_done
  2309. .align 16
  2310. .Lxts_enc_1:
  2311. pxor @XMM[0], @XMM[8]
  2312. lea 0x10($inp), $inp
  2313. movdqa @XMM[8], 0x20(%rbp)
  2314. lea 0x20(%rbp), $arg1
  2315. lea 0x20(%rbp), $arg2
  2316. lea ($key), $arg3
  2317. call asm_AES_encrypt # doesn't touch %xmm
  2318. pxor 0x20(%rbp), @XMM[0] # ^= tweak[]
  2319. #pxor @XMM[8], @XMM[0]
  2320. #lea 0x80(%rsp), %rax # pass key schedule
  2321. #mov %edx, %r10d # pass rounds
  2322. #call _bsaes_encrypt8
  2323. #pxor 0x00(%rsp), @XMM[0] # ^= tweak[]
  2324. movdqu @XMM[0], 0x00($out) # write output
  2325. lea 0x10($out), $out
  2326. movdqa 0x10(%rsp), @XMM[7] # next iteration tweak
  2327. .Lxts_enc_done:
  2328. and \$15, %ebx
  2329. jz .Lxts_enc_ret
  2330. mov $out, %rdx
  2331. .Lxts_enc_steal:
  2332. movzb ($inp), %eax
  2333. movzb -16(%rdx), %ecx
  2334. lea 1($inp), $inp
  2335. mov %al, -16(%rdx)
  2336. mov %cl, 0(%rdx)
  2337. lea 1(%rdx), %rdx
  2338. sub \$1,%ebx
  2339. jnz .Lxts_enc_steal
  2340. movdqu -16($out), @XMM[0]
  2341. lea 0x20(%rbp), $arg1
  2342. pxor @XMM[7], @XMM[0]
  2343. lea 0x20(%rbp), $arg2
  2344. movdqa @XMM[0], 0x20(%rbp)
  2345. lea ($key), $arg3
  2346. call asm_AES_encrypt # doesn't touch %xmm
  2347. pxor 0x20(%rbp), @XMM[7]
  2348. movdqu @XMM[7], -16($out)
  2349. .Lxts_enc_ret:
  2350. lea (%rsp), %rax
  2351. pxor %xmm0, %xmm0
  2352. .Lxts_enc_bzero: # wipe key schedule [if any]
  2353. movdqa %xmm0, 0x00(%rax)
  2354. movdqa %xmm0, 0x10(%rax)
  2355. lea 0x20(%rax), %rax
  2356. cmp %rax, %rbp
  2357. ja .Lxts_enc_bzero
  2358. lea 0x78(%rbp),%rax
  2359. .cfi_def_cfa %rax,8
  2360. ___
  2361. $code.=<<___ if ($win64);
  2362. movaps 0x40(%rbp), %xmm6
  2363. movaps 0x50(%rbp), %xmm7
  2364. movaps 0x60(%rbp), %xmm8
  2365. movaps 0x70(%rbp), %xmm9
  2366. movaps 0x80(%rbp), %xmm10
  2367. movaps 0x90(%rbp), %xmm11
  2368. movaps 0xa0(%rbp), %xmm12
  2369. movaps 0xb0(%rbp), %xmm13
  2370. movaps 0xc0(%rbp), %xmm14
  2371. movaps 0xd0(%rbp), %xmm15
  2372. lea 0xa0(%rax), %rax
  2373. .Lxts_enc_tail:
  2374. ___
  2375. $code.=<<___;
  2376. mov -48(%rax), %r15
  2377. .cfi_restore %r15
  2378. mov -40(%rax), %r14
  2379. .cfi_restore %r14
  2380. mov -32(%rax), %r13
  2381. .cfi_restore %r13
  2382. mov -24(%rax), %r12
  2383. .cfi_restore %r12
  2384. mov -16(%rax), %rbx
  2385. .cfi_restore %rbx
  2386. mov -8(%rax), %rbp
  2387. .cfi_restore %rbp
  2388. lea (%rax), %rsp # restore %rsp
  2389. .cfi_def_cfa_register %rsp
  2390. .Lxts_enc_epilogue:
  2391. ret
  2392. .cfi_endproc
  2393. .size bsaes_xts_encrypt,.-bsaes_xts_encrypt
  2394. .globl bsaes_xts_decrypt
  2395. .type bsaes_xts_decrypt,\@abi-omnipotent
  2396. .align 16
  2397. bsaes_xts_decrypt:
  2398. .cfi_startproc
  2399. mov %rsp, %rax
  2400. .Lxts_dec_prologue:
  2401. push %rbp
  2402. .cfi_push %rbp
  2403. push %rbx
  2404. .cfi_push %rbx
  2405. push %r12
  2406. .cfi_push %r12
  2407. push %r13
  2408. .cfi_push %r13
  2409. push %r14
  2410. .cfi_push %r14
  2411. push %r15
  2412. .cfi_push %r15
  2413. lea -0x48(%rsp), %rsp
  2414. .cfi_adjust_cfa_offset 0x48
  2415. ___
  2416. $code.=<<___ if ($win64);
  2417. mov 0xa0(%rsp),$arg5 # pull key2
  2418. mov 0xa8(%rsp),$arg6 # pull ivp
  2419. lea -0xa0(%rsp), %rsp
  2420. movaps %xmm6, 0x40(%rsp)
  2421. movaps %xmm7, 0x50(%rsp)
  2422. movaps %xmm8, 0x60(%rsp)
  2423. movaps %xmm9, 0x70(%rsp)
  2424. movaps %xmm10, 0x80(%rsp)
  2425. movaps %xmm11, 0x90(%rsp)
  2426. movaps %xmm12, 0xa0(%rsp)
  2427. movaps %xmm13, 0xb0(%rsp)
  2428. movaps %xmm14, 0xc0(%rsp)
  2429. movaps %xmm15, 0xd0(%rsp)
  2430. .Lxts_dec_body:
  2431. ___
  2432. $code.=<<___;
  2433. mov %rsp, %rbp # backup %rsp
  2434. mov $arg1, $inp # backup arguments
  2435. mov $arg2, $out
  2436. mov $arg3, $len
  2437. mov $arg4, $key
  2438. lea ($arg6), $arg1
  2439. lea 0x20(%rbp), $arg2
  2440. lea ($arg5), $arg3
  2441. call asm_AES_encrypt # generate initial tweak
  2442. mov 240($key), %eax # rounds
  2443. mov $len, %rbx # backup $len
  2444. mov %eax, %edx # rounds
  2445. shl \$7, %rax # 128 bytes per inner round key
  2446. sub \$`128-32`, %rax # size of bit-sliced key schedule
  2447. sub %rax, %rsp
  2448. mov %rsp, %rax # pass key schedule
  2449. mov $key, %rcx # pass key
  2450. mov %edx, %r10d # pass rounds
  2451. call _bsaes_key_convert
  2452. pxor (%rsp), %xmm7 # fix up round 0 key
  2453. movdqa %xmm6, (%rax) # save last round key
  2454. movdqa %xmm7, (%rsp)
  2455. xor %eax, %eax # if ($len%16) len-=16;
  2456. and \$-16, $len
  2457. test \$15, %ebx
  2458. setnz %al
  2459. shl \$4, %rax
  2460. sub %rax, $len
  2461. sub \$0x80, %rsp # place for tweak[8]
  2462. movdqa 0x20(%rbp), @XMM[7] # initial tweak
  2463. pxor $twtmp, $twtmp
  2464. movdqa .Lxts_magic(%rip), $twmask
  2465. pcmpgtd @XMM[7], $twtmp # broadcast upper bits
  2466. sub \$0x80, $len
  2467. jc .Lxts_dec_short
  2468. jmp .Lxts_dec_loop
  2469. .align 16
  2470. .Lxts_dec_loop:
  2471. ___
  2472. for ($i=0;$i<7;$i++) {
  2473. $code.=<<___;
  2474. pshufd \$0x13, $twtmp, $twres
  2475. pxor $twtmp, $twtmp
  2476. movdqa @XMM[7], @XMM[$i]
  2477. movdqa @XMM[7], `0x10*$i`(%rsp)# save tweak[$i]
  2478. paddq @XMM[7], @XMM[7] # psllq 1,$tweak
  2479. pand $twmask, $twres # isolate carry and residue
  2480. pcmpgtd @XMM[7], $twtmp # broadcast upper bits
  2481. pxor $twres, @XMM[7]
  2482. ___
  2483. $code.=<<___ if ($i>=1);
  2484. movdqu `0x10*($i-1)`($inp), @XMM[8+$i-1]
  2485. ___
  2486. $code.=<<___ if ($i>=2);
  2487. pxor @XMM[8+$i-2], @XMM[$i-2]# input[] ^ tweak[]
  2488. ___
  2489. }
  2490. $code.=<<___;
  2491. movdqu 0x60($inp), @XMM[8+6]
  2492. pxor @XMM[8+5], @XMM[5]
  2493. movdqu 0x70($inp), @XMM[8+7]
  2494. lea 0x80($inp), $inp
  2495. movdqa @XMM[7], 0x70(%rsp)
  2496. pxor @XMM[8+6], @XMM[6]
  2497. lea 0x80(%rsp), %rax # pass key schedule
  2498. pxor @XMM[8+7], @XMM[7]
  2499. mov %edx, %r10d # pass rounds
  2500. call _bsaes_decrypt8
  2501. pxor 0x00(%rsp), @XMM[0] # ^= tweak[]
  2502. pxor 0x10(%rsp), @XMM[1]
  2503. movdqu @XMM[0], 0x00($out) # write output
  2504. pxor 0x20(%rsp), @XMM[6]
  2505. movdqu @XMM[1], 0x10($out)
  2506. pxor 0x30(%rsp), @XMM[4]
  2507. movdqu @XMM[6], 0x20($out)
  2508. pxor 0x40(%rsp), @XMM[2]
  2509. movdqu @XMM[4], 0x30($out)
  2510. pxor 0x50(%rsp), @XMM[7]
  2511. movdqu @XMM[2], 0x40($out)
  2512. pxor 0x60(%rsp), @XMM[3]
  2513. movdqu @XMM[7], 0x50($out)
  2514. pxor 0x70(%rsp), @XMM[5]
  2515. movdqu @XMM[3], 0x60($out)
  2516. movdqu @XMM[5], 0x70($out)
  2517. lea 0x80($out), $out
  2518. movdqa 0x70(%rsp), @XMM[7] # prepare next iteration tweak
  2519. pxor $twtmp, $twtmp
  2520. movdqa .Lxts_magic(%rip), $twmask
  2521. pcmpgtd @XMM[7], $twtmp
  2522. pshufd \$0x13, $twtmp, $twres
  2523. pxor $twtmp, $twtmp
  2524. paddq @XMM[7], @XMM[7] # psllq 1,$tweak
  2525. pand $twmask, $twres # isolate carry and residue
  2526. pcmpgtd @XMM[7], $twtmp # broadcast upper bits
  2527. pxor $twres, @XMM[7]
  2528. sub \$0x80,$len
  2529. jnc .Lxts_dec_loop
  2530. .Lxts_dec_short:
  2531. add \$0x80, $len
  2532. jz .Lxts_dec_done
  2533. ___
  2534. for ($i=0;$i<7;$i++) {
  2535. $code.=<<___;
  2536. pshufd \$0x13, $twtmp, $twres
  2537. pxor $twtmp, $twtmp
  2538. movdqa @XMM[7], @XMM[$i]
  2539. movdqa @XMM[7], `0x10*$i`(%rsp)# save tweak[$i]
  2540. paddq @XMM[7], @XMM[7] # psllq 1,$tweak
  2541. pand $twmask, $twres # isolate carry and residue
  2542. pcmpgtd @XMM[7], $twtmp # broadcast upper bits
  2543. pxor $twres, @XMM[7]
  2544. ___
  2545. $code.=<<___ if ($i>=1);
  2546. movdqu `0x10*($i-1)`($inp), @XMM[8+$i-1]
  2547. cmp \$`0x10*$i`,$len
  2548. je .Lxts_dec_$i
  2549. ___
  2550. $code.=<<___ if ($i>=2);
  2551. pxor @XMM[8+$i-2], @XMM[$i-2]# input[] ^ tweak[]
  2552. ___
  2553. }
  2554. $code.=<<___;
  2555. movdqu 0x60($inp), @XMM[8+6]
  2556. pxor @XMM[8+5], @XMM[5]
  2557. movdqa @XMM[7], 0x70(%rsp)
  2558. lea 0x70($inp), $inp
  2559. pxor @XMM[8+6], @XMM[6]
  2560. lea 0x80(%rsp), %rax # pass key schedule
  2561. mov %edx, %r10d # pass rounds
  2562. call _bsaes_decrypt8
  2563. pxor 0x00(%rsp), @XMM[0] # ^= tweak[]
  2564. pxor 0x10(%rsp), @XMM[1]
  2565. movdqu @XMM[0], 0x00($out) # write output
  2566. pxor 0x20(%rsp), @XMM[6]
  2567. movdqu @XMM[1], 0x10($out)
  2568. pxor 0x30(%rsp), @XMM[4]
  2569. movdqu @XMM[6], 0x20($out)
  2570. pxor 0x40(%rsp), @XMM[2]
  2571. movdqu @XMM[4], 0x30($out)
  2572. pxor 0x50(%rsp), @XMM[7]
  2573. movdqu @XMM[2], 0x40($out)
  2574. pxor 0x60(%rsp), @XMM[3]
  2575. movdqu @XMM[7], 0x50($out)
  2576. movdqu @XMM[3], 0x60($out)
  2577. lea 0x70($out), $out
  2578. movdqa 0x70(%rsp), @XMM[7] # next iteration tweak
  2579. jmp .Lxts_dec_done
  2580. .align 16
  2581. .Lxts_dec_6:
  2582. pxor @XMM[8+4], @XMM[4]
  2583. lea 0x60($inp), $inp
  2584. pxor @XMM[8+5], @XMM[5]
  2585. lea 0x80(%rsp), %rax # pass key schedule
  2586. mov %edx, %r10d # pass rounds
  2587. call _bsaes_decrypt8
  2588. pxor 0x00(%rsp), @XMM[0] # ^= tweak[]
  2589. pxor 0x10(%rsp), @XMM[1]
  2590. movdqu @XMM[0], 0x00($out) # write output
  2591. pxor 0x20(%rsp), @XMM[6]
  2592. movdqu @XMM[1], 0x10($out)
  2593. pxor 0x30(%rsp), @XMM[4]
  2594. movdqu @XMM[6], 0x20($out)
  2595. pxor 0x40(%rsp), @XMM[2]
  2596. movdqu @XMM[4], 0x30($out)
  2597. pxor 0x50(%rsp), @XMM[7]
  2598. movdqu @XMM[2], 0x40($out)
  2599. movdqu @XMM[7], 0x50($out)
  2600. lea 0x60($out), $out
  2601. movdqa 0x60(%rsp), @XMM[7] # next iteration tweak
  2602. jmp .Lxts_dec_done
  2603. .align 16
  2604. .Lxts_dec_5:
  2605. pxor @XMM[8+3], @XMM[3]
  2606. lea 0x50($inp), $inp
  2607. pxor @XMM[8+4], @XMM[4]
  2608. lea 0x80(%rsp), %rax # pass key schedule
  2609. mov %edx, %r10d # pass rounds
  2610. call _bsaes_decrypt8
  2611. pxor 0x00(%rsp), @XMM[0] # ^= tweak[]
  2612. pxor 0x10(%rsp), @XMM[1]
  2613. movdqu @XMM[0], 0x00($out) # write output
  2614. pxor 0x20(%rsp), @XMM[6]
  2615. movdqu @XMM[1], 0x10($out)
  2616. pxor 0x30(%rsp), @XMM[4]
  2617. movdqu @XMM[6], 0x20($out)
  2618. pxor 0x40(%rsp), @XMM[2]
  2619. movdqu @XMM[4], 0x30($out)
  2620. movdqu @XMM[2], 0x40($out)
  2621. lea 0x50($out), $out
  2622. movdqa 0x50(%rsp), @XMM[7] # next iteration tweak
  2623. jmp .Lxts_dec_done
  2624. .align 16
  2625. .Lxts_dec_4:
  2626. pxor @XMM[8+2], @XMM[2]
  2627. lea 0x40($inp), $inp
  2628. pxor @XMM[8+3], @XMM[3]
  2629. lea 0x80(%rsp), %rax # pass key schedule
  2630. mov %edx, %r10d # pass rounds
  2631. call _bsaes_decrypt8
  2632. pxor 0x00(%rsp), @XMM[0] # ^= tweak[]
  2633. pxor 0x10(%rsp), @XMM[1]
  2634. movdqu @XMM[0], 0x00($out) # write output
  2635. pxor 0x20(%rsp), @XMM[6]
  2636. movdqu @XMM[1], 0x10($out)
  2637. pxor 0x30(%rsp), @XMM[4]
  2638. movdqu @XMM[6], 0x20($out)
  2639. movdqu @XMM[4], 0x30($out)
  2640. lea 0x40($out), $out
  2641. movdqa 0x40(%rsp), @XMM[7] # next iteration tweak
  2642. jmp .Lxts_dec_done
  2643. .align 16
  2644. .Lxts_dec_3:
  2645. pxor @XMM[8+1], @XMM[1]
  2646. lea 0x30($inp), $inp
  2647. pxor @XMM[8+2], @XMM[2]
  2648. lea 0x80(%rsp), %rax # pass key schedule
  2649. mov %edx, %r10d # pass rounds
  2650. call _bsaes_decrypt8
  2651. pxor 0x00(%rsp), @XMM[0] # ^= tweak[]
  2652. pxor 0x10(%rsp), @XMM[1]
  2653. movdqu @XMM[0], 0x00($out) # write output
  2654. pxor 0x20(%rsp), @XMM[6]
  2655. movdqu @XMM[1], 0x10($out)
  2656. movdqu @XMM[6], 0x20($out)
  2657. lea 0x30($out), $out
  2658. movdqa 0x30(%rsp), @XMM[7] # next iteration tweak
  2659. jmp .Lxts_dec_done
  2660. .align 16
  2661. .Lxts_dec_2:
  2662. pxor @XMM[8+0], @XMM[0]
  2663. lea 0x20($inp), $inp
  2664. pxor @XMM[8+1], @XMM[1]
  2665. lea 0x80(%rsp), %rax # pass key schedule
  2666. mov %edx, %r10d # pass rounds
  2667. call _bsaes_decrypt8
  2668. pxor 0x00(%rsp), @XMM[0] # ^= tweak[]
  2669. pxor 0x10(%rsp), @XMM[1]
  2670. movdqu @XMM[0], 0x00($out) # write output
  2671. movdqu @XMM[1], 0x10($out)
  2672. lea 0x20($out), $out
  2673. movdqa 0x20(%rsp), @XMM[7] # next iteration tweak
  2674. jmp .Lxts_dec_done
  2675. .align 16
  2676. .Lxts_dec_1:
  2677. pxor @XMM[0], @XMM[8]
  2678. lea 0x10($inp), $inp
  2679. movdqa @XMM[8], 0x20(%rbp)
  2680. lea 0x20(%rbp), $arg1
  2681. lea 0x20(%rbp), $arg2
  2682. lea ($key), $arg3
  2683. call asm_AES_decrypt # doesn't touch %xmm
  2684. pxor 0x20(%rbp), @XMM[0] # ^= tweak[]
  2685. #pxor @XMM[8], @XMM[0]
  2686. #lea 0x80(%rsp), %rax # pass key schedule
  2687. #mov %edx, %r10d # pass rounds
  2688. #call _bsaes_decrypt8
  2689. #pxor 0x00(%rsp), @XMM[0] # ^= tweak[]
  2690. movdqu @XMM[0], 0x00($out) # write output
  2691. lea 0x10($out), $out
  2692. movdqa 0x10(%rsp), @XMM[7] # next iteration tweak
  2693. .Lxts_dec_done:
  2694. and \$15, %ebx
  2695. jz .Lxts_dec_ret
  2696. pxor $twtmp, $twtmp
  2697. movdqa .Lxts_magic(%rip), $twmask
  2698. pcmpgtd @XMM[7], $twtmp
  2699. pshufd \$0x13, $twtmp, $twres
  2700. movdqa @XMM[7], @XMM[6]
  2701. paddq @XMM[7], @XMM[7] # psllq 1,$tweak
  2702. pand $twmask, $twres # isolate carry and residue
  2703. movdqu ($inp), @XMM[0]
  2704. pxor $twres, @XMM[7]
  2705. lea 0x20(%rbp), $arg1
  2706. pxor @XMM[7], @XMM[0]
  2707. lea 0x20(%rbp), $arg2
  2708. movdqa @XMM[0], 0x20(%rbp)
  2709. lea ($key), $arg3
  2710. call asm_AES_decrypt # doesn't touch %xmm
  2711. pxor 0x20(%rbp), @XMM[7]
  2712. mov $out, %rdx
  2713. movdqu @XMM[7], ($out)
  2714. .Lxts_dec_steal:
  2715. movzb 16($inp), %eax
  2716. movzb (%rdx), %ecx
  2717. lea 1($inp), $inp
  2718. mov %al, (%rdx)
  2719. mov %cl, 16(%rdx)
  2720. lea 1(%rdx), %rdx
  2721. sub \$1,%ebx
  2722. jnz .Lxts_dec_steal
  2723. movdqu ($out), @XMM[0]
  2724. lea 0x20(%rbp), $arg1
  2725. pxor @XMM[6], @XMM[0]
  2726. lea 0x20(%rbp), $arg2
  2727. movdqa @XMM[0], 0x20(%rbp)
  2728. lea ($key), $arg3
  2729. call asm_AES_decrypt # doesn't touch %xmm
  2730. pxor 0x20(%rbp), @XMM[6]
  2731. movdqu @XMM[6], ($out)
  2732. .Lxts_dec_ret:
  2733. lea (%rsp), %rax
  2734. pxor %xmm0, %xmm0
  2735. .Lxts_dec_bzero: # wipe key schedule [if any]
  2736. movdqa %xmm0, 0x00(%rax)
  2737. movdqa %xmm0, 0x10(%rax)
  2738. lea 0x20(%rax), %rax
  2739. cmp %rax, %rbp
  2740. ja .Lxts_dec_bzero
  2741. lea 0x78(%rbp),%rax
  2742. .cfi_def_cfa %rax,8
  2743. ___
  2744. $code.=<<___ if ($win64);
  2745. movaps 0x40(%rbp), %xmm6
  2746. movaps 0x50(%rbp), %xmm7
  2747. movaps 0x60(%rbp), %xmm8
  2748. movaps 0x70(%rbp), %xmm9
  2749. movaps 0x80(%rbp), %xmm10
  2750. movaps 0x90(%rbp), %xmm11
  2751. movaps 0xa0(%rbp), %xmm12
  2752. movaps 0xb0(%rbp), %xmm13
  2753. movaps 0xc0(%rbp), %xmm14
  2754. movaps 0xd0(%rbp), %xmm15
  2755. lea 0xa0(%rax), %rax
  2756. .Lxts_dec_tail:
  2757. ___
  2758. $code.=<<___;
  2759. mov -48(%rax), %r15
  2760. .cfi_restore %r15
  2761. mov -40(%rax), %r14
  2762. .cfi_restore %r14
  2763. mov -32(%rax), %r13
  2764. .cfi_restore %r13
  2765. mov -24(%rax), %r12
  2766. .cfi_restore %r12
  2767. mov -16(%rax), %rbx
  2768. .cfi_restore %rbx
  2769. mov -8(%rax), %rbp
  2770. .cfi_restore %rbp
  2771. lea (%rax), %rsp # restore %rsp
  2772. .cfi_def_cfa_register %rsp
  2773. .Lxts_dec_epilogue:
  2774. ret
  2775. .cfi_endproc
  2776. .size bsaes_xts_decrypt,.-bsaes_xts_decrypt
  2777. ___
  2778. }
  2779. $code.=<<___;
  2780. .type _bsaes_const,\@object
  2781. .align 64
  2782. _bsaes_const:
  2783. .LM0ISR: # InvShiftRows constants
  2784. .quad 0x0a0e0206070b0f03, 0x0004080c0d010509
  2785. .LISRM0:
  2786. .quad 0x01040b0e0205080f, 0x0306090c00070a0d
  2787. .LISR:
  2788. .quad 0x0504070602010003, 0x0f0e0d0c080b0a09
  2789. .LBS0: # bit-slice constants
  2790. .quad 0x5555555555555555, 0x5555555555555555
  2791. .LBS1:
  2792. .quad 0x3333333333333333, 0x3333333333333333
  2793. .LBS2:
  2794. .quad 0x0f0f0f0f0f0f0f0f, 0x0f0f0f0f0f0f0f0f
  2795. .LSR: # shiftrows constants
  2796. .quad 0x0504070600030201, 0x0f0e0d0c0a09080b
  2797. .LSRM0:
  2798. .quad 0x0304090e00050a0f, 0x01060b0c0207080d
  2799. .LM0SR:
  2800. .quad 0x0a0e02060f03070b, 0x0004080c05090d01
  2801. .LSWPUP: # byte-swap upper dword
  2802. .quad 0x0706050403020100, 0x0c0d0e0f0b0a0908
  2803. .LSWPUPM0SR:
  2804. .quad 0x0a0d02060c03070b, 0x0004080f05090e01
  2805. .LADD1: # counter increment constants
  2806. .quad 0x0000000000000000, 0x0000000100000000
  2807. .LADD2:
  2808. .quad 0x0000000000000000, 0x0000000200000000
  2809. .LADD3:
  2810. .quad 0x0000000000000000, 0x0000000300000000
  2811. .LADD4:
  2812. .quad 0x0000000000000000, 0x0000000400000000
  2813. .LADD5:
  2814. .quad 0x0000000000000000, 0x0000000500000000
  2815. .LADD6:
  2816. .quad 0x0000000000000000, 0x0000000600000000
  2817. .LADD7:
  2818. .quad 0x0000000000000000, 0x0000000700000000
  2819. .LADD8:
  2820. .quad 0x0000000000000000, 0x0000000800000000
  2821. .Lxts_magic:
  2822. .long 0x87,0,1,0
  2823. .Lmasks:
  2824. .quad 0x0101010101010101, 0x0101010101010101
  2825. .quad 0x0202020202020202, 0x0202020202020202
  2826. .quad 0x0404040404040404, 0x0404040404040404
  2827. .quad 0x0808080808080808, 0x0808080808080808
  2828. .LM0:
  2829. .quad 0x02060a0e03070b0f, 0x0004080c0105090d
  2830. .L63:
  2831. .quad 0x6363636363636363, 0x6363636363636363
  2832. .asciz "Bit-sliced AES for x86_64/SSSE3, Emilia Käsper, Peter Schwabe, Andy Polyakov"
  2833. .align 64
  2834. .size _bsaes_const,.-_bsaes_const
  2835. ___
  2836. # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
  2837. # CONTEXT *context,DISPATCHER_CONTEXT *disp)
  2838. if ($win64) {
  2839. $rec="%rcx";
  2840. $frame="%rdx";
  2841. $context="%r8";
  2842. $disp="%r9";
  2843. $code.=<<___;
  2844. .extern __imp_RtlVirtualUnwind
  2845. .type se_handler,\@abi-omnipotent
  2846. .align 16
  2847. se_handler:
  2848. push %rsi
  2849. push %rdi
  2850. push %rbx
  2851. push %rbp
  2852. push %r12
  2853. push %r13
  2854. push %r14
  2855. push %r15
  2856. pushfq
  2857. sub \$64,%rsp
  2858. mov 120($context),%rax # pull context->Rax
  2859. mov 248($context),%rbx # pull context->Rip
  2860. mov 8($disp),%rsi # disp->ImageBase
  2861. mov 56($disp),%r11 # disp->HandlerData
  2862. mov 0(%r11),%r10d # HandlerData[0]
  2863. lea (%rsi,%r10),%r10 # prologue label
  2864. cmp %r10,%rbx # context->Rip<=prologue label
  2865. jbe .Lin_prologue
  2866. mov 4(%r11),%r10d # HandlerData[1]
  2867. lea (%rsi,%r10),%r10 # epilogue label
  2868. cmp %r10,%rbx # context->Rip>=epilogue label
  2869. jae .Lin_prologue
  2870. mov 8(%r11),%r10d # HandlerData[2]
  2871. lea (%rsi,%r10),%r10 # epilogue label
  2872. cmp %r10,%rbx # context->Rip>=tail label
  2873. jae .Lin_tail
  2874. mov 160($context),%rax # pull context->Rbp
  2875. lea 0x40(%rax),%rsi # %xmm save area
  2876. lea 512($context),%rdi # &context.Xmm6
  2877. mov \$20,%ecx # 10*sizeof(%xmm0)/sizeof(%rax)
  2878. .long 0xa548f3fc # cld; rep movsq
  2879. lea 0xa0+0x78(%rax),%rax # adjust stack pointer
  2880. .Lin_tail:
  2881. mov -48(%rax),%rbp
  2882. mov -40(%rax),%rbx
  2883. mov -32(%rax),%r12
  2884. mov -24(%rax),%r13
  2885. mov -16(%rax),%r14
  2886. mov -8(%rax),%r15
  2887. mov %rbx,144($context) # restore context->Rbx
  2888. mov %rbp,160($context) # restore context->Rbp
  2889. mov %r12,216($context) # restore context->R12
  2890. mov %r13,224($context) # restore context->R13
  2891. mov %r14,232($context) # restore context->R14
  2892. mov %r15,240($context) # restore context->R15
  2893. .Lin_prologue:
  2894. mov %rax,152($context) # restore context->Rsp
  2895. mov 40($disp),%rdi # disp->ContextRecord
  2896. mov $context,%rsi # context
  2897. mov \$`1232/8`,%ecx # sizeof(CONTEXT)
  2898. .long 0xa548f3fc # cld; rep movsq
  2899. mov $disp,%rsi
  2900. xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
  2901. mov 8(%rsi),%rdx # arg2, disp->ImageBase
  2902. mov 0(%rsi),%r8 # arg3, disp->ControlPc
  2903. mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
  2904. mov 40(%rsi),%r10 # disp->ContextRecord
  2905. lea 56(%rsi),%r11 # &disp->HandlerData
  2906. lea 24(%rsi),%r12 # &disp->EstablisherFrame
  2907. mov %r10,32(%rsp) # arg5
  2908. mov %r11,40(%rsp) # arg6
  2909. mov %r12,48(%rsp) # arg7
  2910. mov %rcx,56(%rsp) # arg8, (NULL)
  2911. call *__imp_RtlVirtualUnwind(%rip)
  2912. mov \$1,%eax # ExceptionContinueSearch
  2913. add \$64,%rsp
  2914. popfq
  2915. pop %r15
  2916. pop %r14
  2917. pop %r13
  2918. pop %r12
  2919. pop %rbp
  2920. pop %rbx
  2921. pop %rdi
  2922. pop %rsi
  2923. ret
  2924. .size se_handler,.-se_handler
  2925. .section .pdata
  2926. .align 4
  2927. ___
  2928. $code.=<<___ if ($ecb);
  2929. .rva .Lecb_enc_prologue
  2930. .rva .Lecb_enc_epilogue
  2931. .rva .Lecb_enc_info
  2932. .rva .Lecb_dec_prologue
  2933. .rva .Lecb_dec_epilogue
  2934. .rva .Lecb_dec_info
  2935. ___
  2936. $code.=<<___;
  2937. .rva .Lcbc_dec_prologue
  2938. .rva .Lcbc_dec_epilogue
  2939. .rva .Lcbc_dec_info
  2940. .rva .Lctr_enc_prologue
  2941. .rva .Lctr_enc_epilogue
  2942. .rva .Lctr_enc_info
  2943. .rva .Lxts_enc_prologue
  2944. .rva .Lxts_enc_epilogue
  2945. .rva .Lxts_enc_info
  2946. .rva .Lxts_dec_prologue
  2947. .rva .Lxts_dec_epilogue
  2948. .rva .Lxts_dec_info
  2949. .section .xdata
  2950. .align 8
  2951. ___
  2952. $code.=<<___ if ($ecb);
  2953. .Lecb_enc_info:
  2954. .byte 9,0,0,0
  2955. .rva se_handler
  2956. .rva .Lecb_enc_body,.Lecb_enc_epilogue # HandlerData[]
  2957. .rva .Lecb_enc_tail
  2958. .long 0
  2959. .Lecb_dec_info:
  2960. .byte 9,0,0,0
  2961. .rva se_handler
  2962. .rva .Lecb_dec_body,.Lecb_dec_epilogue # HandlerData[]
  2963. .rva .Lecb_dec_tail
  2964. .long 0
  2965. ___
  2966. $code.=<<___;
  2967. .Lcbc_dec_info:
  2968. .byte 9,0,0,0
  2969. .rva se_handler
  2970. .rva .Lcbc_dec_body,.Lcbc_dec_epilogue # HandlerData[]
  2971. .rva .Lcbc_dec_tail
  2972. .long 0
  2973. .Lctr_enc_info:
  2974. .byte 9,0,0,0
  2975. .rva se_handler
  2976. .rva .Lctr_enc_body,.Lctr_enc_epilogue # HandlerData[]
  2977. .rva .Lctr_enc_tail
  2978. .long 0
  2979. .Lxts_enc_info:
  2980. .byte 9,0,0,0
  2981. .rva se_handler
  2982. .rva .Lxts_enc_body,.Lxts_enc_epilogue # HandlerData[]
  2983. .rva .Lxts_enc_tail
  2984. .long 0
  2985. .Lxts_dec_info:
  2986. .byte 9,0,0,0
  2987. .rva se_handler
  2988. .rva .Lxts_dec_body,.Lxts_dec_epilogue # HandlerData[]
  2989. .rva .Lxts_dec_tail
  2990. .long 0
  2991. ___
  2992. }
  2993. $code =~ s/\`([^\`]*)\`/eval($1)/gem;
  2994. print $code;
  2995. close STDOUT;