aesni-x86_64.pl 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068
  1. #!/usr/bin/env perl
  2. #
  3. # ====================================================================
  4. # Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
  5. # project. The module is, however, dual licensed under OpenSSL and
  6. # CRYPTOGAMS licenses depending on where you obtain it. For further
  7. # details see http://www.openssl.org/~appro/cryptogams/.
  8. # ====================================================================
  9. #
  10. # This module implements support for Intel AES-NI extension. In
  11. # OpenSSL context it's used with Intel engine, but can also be used as
  12. # drop-in replacement for crypto/aes/asm/aes-x86_64.pl [see below for
  13. # details].
  14. #
  15. # Performance.
  16. #
  17. # Given aes(enc|dec) instructions' latency asymptotic performance for
  18. # non-parallelizable modes such as CBC encrypt is 3.75 cycles per byte
  19. # processed with 128-bit key. And given their throughput asymptotic
  20. # performance for parallelizable modes is 1.25 cycles per byte. Being
  21. # asymptotic limit it's not something you commonly achieve in reality,
  22. # but how close does one get? Below are results collected for
  23. # different modes and block sized. Pairs of numbers are for en-/
  24. # decryption.
  25. #
  26. # 16-byte 64-byte 256-byte 1-KB 8-KB
  27. # ECB 4.25/4.25 1.38/1.38 1.28/1.28 1.26/1.26 1.26/1.26
  28. # CTR 5.42/5.42 1.92/1.92 1.44/1.44 1.28/1.28 1.26/1.26
  29. # CBC 4.38/4.43 4.15/1.43 4.07/1.32 4.07/1.29 4.06/1.28
  30. # CCM 5.66/9.42 4.42/5.41 4.16/4.40 4.09/4.15 4.06/4.07
  31. # OFB 5.42/5.42 4.64/4.64 4.44/4.44 4.39/4.39 4.38/4.38
  32. # CFB 5.73/5.85 5.56/5.62 5.48/5.56 5.47/5.55 5.47/5.55
  33. #
  34. # ECB, CTR, CBC and CCM results are free from EVP overhead. This means
  35. # that otherwise used 'openssl speed -evp aes-128-??? -engine aesni
  36. # [-decrypt]' will exhibit 10-15% worse results for smaller blocks.
  37. # The results were collected with specially crafted speed.c benchmark
  38. # in order to compare them with results reported in "Intel Advanced
  39. # Encryption Standard (AES) New Instruction Set" White Paper Revision
  40. # 3.0 dated May 2010. All above results are consistently better. This
  41. # module also provides better performance for block sizes smaller than
  42. # 128 bytes in points *not* represented in the above table.
  43. #
  44. # Looking at the results for 8-KB buffer.
  45. #
  46. # CFB and OFB results are far from the limit, because implementation
  47. # uses "generic" CRYPTO_[c|o]fb128_encrypt interfaces relying on
  48. # single-block aesni_encrypt, which is not the most optimal way to go.
  49. # CBC encrypt result is unexpectedly high and there is no documented
  50. # explanation for it. Seemingly there is a small penalty for feeding
  51. # the result back to AES unit the way it's done in CBC mode. There is
  52. # nothing one can do and the result appears optimal. CCM result is
  53. # identical to CBC, because CBC-MAC is essentially CBC encrypt without
  54. # saving output. CCM CTR "stays invisible," because it's neatly
  55. # interleaved wih CBC-MAC. This provides ~30% improvement over
  56. # "straghtforward" CCM implementation with CTR and CBC-MAC performed
  57. # disjointly. Parallelizable modes practically achieve the theoretical
  58. # limit.
  59. #
  60. # Looking at how results vary with buffer size.
  61. #
  62. # Curves are practically saturated at 1-KB buffer size. In most cases
  63. # "256-byte" performance is >95%, and "64-byte" is ~90% of "8-KB" one.
  64. # CTR curve doesn't follow this pattern and is "slowest" changing one
  65. # with "256-byte" result being 87% of "8-KB." This is because overhead
  66. # in CTR mode is most computationally intensive. Small-block CCM
  67. # decrypt is slower than encrypt, because first CTR and last CBC-MAC
  68. # iterations can't be interleaved.
  69. #
  70. # Results for 192- and 256-bit keys.
  71. #
  72. # EVP-free results were observed to scale perfectly with number of
  73. # rounds for larger block sizes, i.e. 192-bit result being 10/12 times
  74. # lower and 256-bit one - 10/14. Well, in CBC encrypt case differences
  75. # are a tad smaller, because the above mentioned penalty biases all
  76. # results by same constant value. In similar way function call
  77. # overhead affects small-block performance, as well as OFB and CFB
  78. # results. Differences are not large, most common coefficients are
  79. # 10/11.7 and 10/13.4 (as opposite to 10/12.0 and 10/14.0), but one
  80. # observe even 10/11.2 and 10/12.4 (CTR, OFB, CFB)...
  81. # January 2011
  82. #
  83. # While Westmere processor features 6 cycles latency for aes[enc|dec]
  84. # instructions, which can be scheduled every second cycle, Sandy
  85. # Bridge spends 8 cycles per instruction, but it can schedule them
  86. # every cycle. This means that code targeting Westmere would perform
  87. # suboptimally on Sandy Bridge. Therefore this update.
  88. #
  89. # In addition, non-parallelizable CBC encrypt (as well as CCM) is
  90. # optimized. Relative improvement might appear modest, 8% on Westmere,
  91. # but in absolute terms it's 3.77 cycles per byte encrypted with
  92. # 128-bit key on Westmere, and 5.07 - on Sandy Bridge. These numbers
  93. # should be compared to asymptotic limits of 3.75 for Westmere and
  94. # 5.00 for Sandy Bridge. Actually, the fact that they get this close
  95. # to asymptotic limits is quite amazing. Indeed, the limit is
  96. # calculated as latency times number of rounds, 10 for 128-bit key,
  97. # and divided by 16, the number of bytes in block, or in other words
  98. # it accounts *solely* for aesenc instructions. But there are extra
  99. # instructions, and numbers so close to the asymptotic limits mean
  100. # that it's as if it takes as little as *one* additional cycle to
  101. # execute all of them. How is it possible? It is possible thanks to
  102. # out-of-order execution logic, which manages to overlap post-
  103. # processing of previous block, things like saving the output, with
  104. # actual encryption of current block, as well as pre-processing of
  105. # current block, things like fetching input and xor-ing it with
  106. # 0-round element of the key schedule, with actual encryption of
  107. # previous block. Keep this in mind...
  108. #
  109. # For parallelizable modes, such as ECB, CBC decrypt, CTR, higher
  110. # performance is achieved by interleaving instructions working on
  111. # independent blocks. In which case asymptotic limit for such modes
  112. # can be obtained by dividing above mentioned numbers by AES
  113. # instructions' interleave factor. Westmere can execute at most 3
  114. # instructions at a time, meaning that optimal interleave factor is 3,
  115. # and that's where the "magic" number of 1.25 come from. "Optimal
  116. # interleave factor" means that increase of interleave factor does
  117. # not improve performance. The formula has proven to reflect reality
  118. # pretty well on Westmere... Sandy Bridge on the other hand can
  119. # execute up to 8 AES instructions at a time, so how does varying
  120. # interleave factor affect the performance? Here is table for ECB
  121. # (numbers are cycles per byte processed with 128-bit key):
  122. #
  123. # instruction interleave factor 3x 6x 8x
  124. # theoretical asymptotic limit 1.67 0.83 0.625
  125. # measured performance for 8KB block 1.05 0.86 0.84
  126. #
  127. # "as if" interleave factor 4.7x 5.8x 6.0x
  128. #
  129. # Further data for other parallelizable modes:
  130. #
  131. # CBC decrypt 1.16 0.93 0.93
  132. # CTR 1.14 0.91 n/a
  133. #
  134. # Well, given 3x column it's probably inappropriate to call the limit
  135. # asymptotic, if it can be surpassed, isn't it? What happens there?
  136. # Rewind to CBC paragraph for the answer. Yes, out-of-order execution
  137. # magic is responsible for this. Processor overlaps not only the
  138. # additional instructions with AES ones, but even AES instuctions
  139. # processing adjacent triplets of independent blocks. In the 6x case
  140. # additional instructions still claim disproportionally small amount
  141. # of additional cycles, but in 8x case number of instructions must be
  142. # a tad too high for out-of-order logic to cope with, and AES unit
  143. # remains underutilized... As you can see 8x interleave is hardly
  144. # justifiable, so there no need to feel bad that 32-bit aesni-x86.pl
  145. # utilizies 6x interleave because of limited register bank capacity.
  146. #
  147. # Higher interleave factors do have negative impact on Westmere
  148. # performance. While for ECB mode it's negligible ~1.5%, other
  149. # parallelizables perform ~5% worse, which is outweighed by ~25%
  150. # improvement on Sandy Bridge. To balance regression on Westmere
  151. # CTR mode was implemented with 6x aesenc interleave factor.
  152. # April 2011
  153. #
  154. # Add aesni_xts_[en|de]crypt. Westmere spends 1.33 cycles processing
  155. # one byte out of 8KB with 128-bit key, Sandy Bridge - 0.97. Just like
  156. # in CTR mode AES instruction interleave factor was chosen to be 6x.
  157. $PREFIX="aesni"; # if $PREFIX is set to "AES", the script
  158. # generates drop-in replacement for
  159. # crypto/aes/asm/aes-x86_64.pl:-)
  160. $flavour = shift;
  161. $output = shift;
  162. if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
  163. $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
  164. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  165. ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
  166. ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
  167. die "can't locate x86_64-xlate.pl";
  168. open STDOUT,"| $^X $xlate $flavour $output";
  169. $movkey = $PREFIX eq "aesni" ? "movups" : "movups";
  170. @_4args=$win64? ("%rcx","%rdx","%r8", "%r9") : # Win64 order
  171. ("%rdi","%rsi","%rdx","%rcx"); # Unix order
  172. $code=".text\n";
  173. $rounds="%eax"; # input to and changed by aesni_[en|de]cryptN !!!
  174. # this is natural Unix argument order for public $PREFIX_[ecb|cbc]_encrypt ...
  175. $inp="%rdi";
  176. $out="%rsi";
  177. $len="%rdx";
  178. $key="%rcx"; # input to and changed by aesni_[en|de]cryptN !!!
  179. $ivp="%r8"; # cbc, ctr, ...
  180. $rnds_="%r10d"; # backup copy for $rounds
  181. $key_="%r11"; # backup copy for $key
  182. # %xmm register layout
  183. $rndkey0="%xmm0"; $rndkey1="%xmm1";
  184. $inout0="%xmm2"; $inout1="%xmm3";
  185. $inout2="%xmm4"; $inout3="%xmm5";
  186. $inout4="%xmm6"; $inout5="%xmm7";
  187. $inout6="%xmm8"; $inout7="%xmm9";
  188. $in2="%xmm6"; $in1="%xmm7"; # used in CBC decrypt, CTR, ...
  189. $in0="%xmm8"; $iv="%xmm9";
  190. # Inline version of internal aesni_[en|de]crypt1.
  191. #
  192. # Why folded loop? Because aes[enc|dec] is slow enough to accommodate
  193. # cycles which take care of loop variables...
  194. { my $sn;
  195. sub aesni_generate1 {
  196. my ($p,$key,$rounds,$inout,$ivec)=@_; $inout=$inout0 if (!defined($inout));
  197. ++$sn;
  198. $code.=<<___;
  199. $movkey ($key),$rndkey0
  200. $movkey 16($key),$rndkey1
  201. ___
  202. $code.=<<___ if (defined($ivec));
  203. xorps $rndkey0,$ivec
  204. lea 32($key),$key
  205. xorps $ivec,$inout
  206. ___
  207. $code.=<<___ if (!defined($ivec));
  208. lea 32($key),$key
  209. xorps $rndkey0,$inout
  210. ___
  211. $code.=<<___;
  212. .Loop_${p}1_$sn:
  213. aes${p} $rndkey1,$inout
  214. dec $rounds
  215. $movkey ($key),$rndkey1
  216. lea 16($key),$key
  217. jnz .Loop_${p}1_$sn # loop body is 16 bytes
  218. aes${p}last $rndkey1,$inout
  219. ___
  220. }}
  221. # void $PREFIX_[en|de]crypt (const void *inp,void *out,const AES_KEY *key);
  222. #
  223. { my ($inp,$out,$key) = @_4args;
  224. $code.=<<___;
  225. .globl ${PREFIX}_encrypt
  226. .type ${PREFIX}_encrypt,\@abi-omnipotent
  227. .align 16
  228. ${PREFIX}_encrypt:
  229. movups ($inp),$inout0 # load input
  230. mov 240($key),$rounds # key->rounds
  231. ___
  232. &aesni_generate1("enc",$key,$rounds);
  233. $code.=<<___;
  234. movups $inout0,($out) # output
  235. ret
  236. .size ${PREFIX}_encrypt,.-${PREFIX}_encrypt
  237. .globl ${PREFIX}_decrypt
  238. .type ${PREFIX}_decrypt,\@abi-omnipotent
  239. .align 16
  240. ${PREFIX}_decrypt:
  241. movups ($inp),$inout0 # load input
  242. mov 240($key),$rounds # key->rounds
  243. ___
  244. &aesni_generate1("dec",$key,$rounds);
  245. $code.=<<___;
  246. movups $inout0,($out) # output
  247. ret
  248. .size ${PREFIX}_decrypt, .-${PREFIX}_decrypt
  249. ___
  250. }
  251. # _aesni_[en|de]cryptN are private interfaces, N denotes interleave
  252. # factor. Why 3x subroutine were originally used in loops? Even though
  253. # aes[enc|dec] latency was originally 6, it could be scheduled only
  254. # every *2nd* cycle. Thus 3x interleave was the one providing optimal
  255. # utilization, i.e. when subroutine's throughput is virtually same as
  256. # of non-interleaved subroutine [for number of input blocks up to 3].
  257. # This is why it makes no sense to implement 2x subroutine.
  258. # aes[enc|dec] latency in next processor generation is 8, but the
  259. # instructions can be scheduled every cycle. Optimal interleave for
  260. # new processor is therefore 8x...
  261. sub aesni_generate3 {
  262. my $dir=shift;
  263. # As already mentioned it takes in $key and $rounds, which are *not*
  264. # preserved. $inout[0-2] is cipher/clear text...
  265. $code.=<<___;
  266. .type _aesni_${dir}rypt3,\@abi-omnipotent
  267. .align 16
  268. _aesni_${dir}rypt3:
  269. $movkey ($key),$rndkey0
  270. shr \$1,$rounds
  271. $movkey 16($key),$rndkey1
  272. lea 32($key),$key
  273. xorps $rndkey0,$inout0
  274. xorps $rndkey0,$inout1
  275. xorps $rndkey0,$inout2
  276. $movkey ($key),$rndkey0
  277. .L${dir}_loop3:
  278. aes${dir} $rndkey1,$inout0
  279. aes${dir} $rndkey1,$inout1
  280. dec $rounds
  281. aes${dir} $rndkey1,$inout2
  282. $movkey 16($key),$rndkey1
  283. aes${dir} $rndkey0,$inout0
  284. aes${dir} $rndkey0,$inout1
  285. lea 32($key),$key
  286. aes${dir} $rndkey0,$inout2
  287. $movkey ($key),$rndkey0
  288. jnz .L${dir}_loop3
  289. aes${dir} $rndkey1,$inout0
  290. aes${dir} $rndkey1,$inout1
  291. aes${dir} $rndkey1,$inout2
  292. aes${dir}last $rndkey0,$inout0
  293. aes${dir}last $rndkey0,$inout1
  294. aes${dir}last $rndkey0,$inout2
  295. ret
  296. .size _aesni_${dir}rypt3,.-_aesni_${dir}rypt3
  297. ___
  298. }
  299. # 4x interleave is implemented to improve small block performance,
  300. # most notably [and naturally] 4 block by ~30%. One can argue that one
  301. # should have implemented 5x as well, but improvement would be <20%,
  302. # so it's not worth it...
  303. sub aesni_generate4 {
  304. my $dir=shift;
  305. # As already mentioned it takes in $key and $rounds, which are *not*
  306. # preserved. $inout[0-3] is cipher/clear text...
  307. $code.=<<___;
  308. .type _aesni_${dir}rypt4,\@abi-omnipotent
  309. .align 16
  310. _aesni_${dir}rypt4:
  311. $movkey ($key),$rndkey0
  312. shr \$1,$rounds
  313. $movkey 16($key),$rndkey1
  314. lea 32($key),$key
  315. xorps $rndkey0,$inout0
  316. xorps $rndkey0,$inout1
  317. xorps $rndkey0,$inout2
  318. xorps $rndkey0,$inout3
  319. $movkey ($key),$rndkey0
  320. .L${dir}_loop4:
  321. aes${dir} $rndkey1,$inout0
  322. aes${dir} $rndkey1,$inout1
  323. dec $rounds
  324. aes${dir} $rndkey1,$inout2
  325. aes${dir} $rndkey1,$inout3
  326. $movkey 16($key),$rndkey1
  327. aes${dir} $rndkey0,$inout0
  328. aes${dir} $rndkey0,$inout1
  329. lea 32($key),$key
  330. aes${dir} $rndkey0,$inout2
  331. aes${dir} $rndkey0,$inout3
  332. $movkey ($key),$rndkey0
  333. jnz .L${dir}_loop4
  334. aes${dir} $rndkey1,$inout0
  335. aes${dir} $rndkey1,$inout1
  336. aes${dir} $rndkey1,$inout2
  337. aes${dir} $rndkey1,$inout3
  338. aes${dir}last $rndkey0,$inout0
  339. aes${dir}last $rndkey0,$inout1
  340. aes${dir}last $rndkey0,$inout2
  341. aes${dir}last $rndkey0,$inout3
  342. ret
  343. .size _aesni_${dir}rypt4,.-_aesni_${dir}rypt4
  344. ___
  345. }
  346. sub aesni_generate6 {
  347. my $dir=shift;
  348. # As already mentioned it takes in $key and $rounds, which are *not*
  349. # preserved. $inout[0-5] is cipher/clear text...
  350. $code.=<<___;
  351. .type _aesni_${dir}rypt6,\@abi-omnipotent
  352. .align 16
  353. _aesni_${dir}rypt6:
  354. $movkey ($key),$rndkey0
  355. shr \$1,$rounds
  356. $movkey 16($key),$rndkey1
  357. lea 32($key),$key
  358. xorps $rndkey0,$inout0
  359. pxor $rndkey0,$inout1
  360. aes${dir} $rndkey1,$inout0
  361. pxor $rndkey0,$inout2
  362. aes${dir} $rndkey1,$inout1
  363. pxor $rndkey0,$inout3
  364. aes${dir} $rndkey1,$inout2
  365. pxor $rndkey0,$inout4
  366. aes${dir} $rndkey1,$inout3
  367. pxor $rndkey0,$inout5
  368. dec $rounds
  369. aes${dir} $rndkey1,$inout4
  370. $movkey ($key),$rndkey0
  371. aes${dir} $rndkey1,$inout5
  372. jmp .L${dir}_loop6_enter
  373. .align 16
  374. .L${dir}_loop6:
  375. aes${dir} $rndkey1,$inout0
  376. aes${dir} $rndkey1,$inout1
  377. dec $rounds
  378. aes${dir} $rndkey1,$inout2
  379. aes${dir} $rndkey1,$inout3
  380. aes${dir} $rndkey1,$inout4
  381. aes${dir} $rndkey1,$inout5
  382. .L${dir}_loop6_enter: # happens to be 16-byte aligned
  383. $movkey 16($key),$rndkey1
  384. aes${dir} $rndkey0,$inout0
  385. aes${dir} $rndkey0,$inout1
  386. lea 32($key),$key
  387. aes${dir} $rndkey0,$inout2
  388. aes${dir} $rndkey0,$inout3
  389. aes${dir} $rndkey0,$inout4
  390. aes${dir} $rndkey0,$inout5
  391. $movkey ($key),$rndkey0
  392. jnz .L${dir}_loop6
  393. aes${dir} $rndkey1,$inout0
  394. aes${dir} $rndkey1,$inout1
  395. aes${dir} $rndkey1,$inout2
  396. aes${dir} $rndkey1,$inout3
  397. aes${dir} $rndkey1,$inout4
  398. aes${dir} $rndkey1,$inout5
  399. aes${dir}last $rndkey0,$inout0
  400. aes${dir}last $rndkey0,$inout1
  401. aes${dir}last $rndkey0,$inout2
  402. aes${dir}last $rndkey0,$inout3
  403. aes${dir}last $rndkey0,$inout4
  404. aes${dir}last $rndkey0,$inout5
  405. ret
  406. .size _aesni_${dir}rypt6,.-_aesni_${dir}rypt6
  407. ___
  408. }
  409. sub aesni_generate8 {
  410. my $dir=shift;
  411. # As already mentioned it takes in $key and $rounds, which are *not*
  412. # preserved. $inout[0-7] is cipher/clear text...
  413. $code.=<<___;
  414. .type _aesni_${dir}rypt8,\@abi-omnipotent
  415. .align 16
  416. _aesni_${dir}rypt8:
  417. $movkey ($key),$rndkey0
  418. shr \$1,$rounds
  419. $movkey 16($key),$rndkey1
  420. lea 32($key),$key
  421. xorps $rndkey0,$inout0
  422. xorps $rndkey0,$inout1
  423. aes${dir} $rndkey1,$inout0
  424. pxor $rndkey0,$inout2
  425. aes${dir} $rndkey1,$inout1
  426. pxor $rndkey0,$inout3
  427. aes${dir} $rndkey1,$inout2
  428. pxor $rndkey0,$inout4
  429. aes${dir} $rndkey1,$inout3
  430. pxor $rndkey0,$inout5
  431. dec $rounds
  432. aes${dir} $rndkey1,$inout4
  433. pxor $rndkey0,$inout6
  434. aes${dir} $rndkey1,$inout5
  435. pxor $rndkey0,$inout7
  436. $movkey ($key),$rndkey0
  437. aes${dir} $rndkey1,$inout6
  438. aes${dir} $rndkey1,$inout7
  439. $movkey 16($key),$rndkey1
  440. jmp .L${dir}_loop8_enter
  441. .align 16
  442. .L${dir}_loop8:
  443. aes${dir} $rndkey1,$inout0
  444. aes${dir} $rndkey1,$inout1
  445. dec $rounds
  446. aes${dir} $rndkey1,$inout2
  447. aes${dir} $rndkey1,$inout3
  448. aes${dir} $rndkey1,$inout4
  449. aes${dir} $rndkey1,$inout5
  450. aes${dir} $rndkey1,$inout6
  451. aes${dir} $rndkey1,$inout7
  452. $movkey 16($key),$rndkey1
  453. .L${dir}_loop8_enter: # happens to be 16-byte aligned
  454. aes${dir} $rndkey0,$inout0
  455. aes${dir} $rndkey0,$inout1
  456. lea 32($key),$key
  457. aes${dir} $rndkey0,$inout2
  458. aes${dir} $rndkey0,$inout3
  459. aes${dir} $rndkey0,$inout4
  460. aes${dir} $rndkey0,$inout5
  461. aes${dir} $rndkey0,$inout6
  462. aes${dir} $rndkey0,$inout7
  463. $movkey ($key),$rndkey0
  464. jnz .L${dir}_loop8
  465. aes${dir} $rndkey1,$inout0
  466. aes${dir} $rndkey1,$inout1
  467. aes${dir} $rndkey1,$inout2
  468. aes${dir} $rndkey1,$inout3
  469. aes${dir} $rndkey1,$inout4
  470. aes${dir} $rndkey1,$inout5
  471. aes${dir} $rndkey1,$inout6
  472. aes${dir} $rndkey1,$inout7
  473. aes${dir}last $rndkey0,$inout0
  474. aes${dir}last $rndkey0,$inout1
  475. aes${dir}last $rndkey0,$inout2
  476. aes${dir}last $rndkey0,$inout3
  477. aes${dir}last $rndkey0,$inout4
  478. aes${dir}last $rndkey0,$inout5
  479. aes${dir}last $rndkey0,$inout6
  480. aes${dir}last $rndkey0,$inout7
  481. ret
  482. .size _aesni_${dir}rypt8,.-_aesni_${dir}rypt8
  483. ___
  484. }
  485. &aesni_generate3("enc") if ($PREFIX eq "aesni");
  486. &aesni_generate3("dec");
  487. &aesni_generate4("enc") if ($PREFIX eq "aesni");
  488. &aesni_generate4("dec");
  489. &aesni_generate6("enc") if ($PREFIX eq "aesni");
  490. &aesni_generate6("dec");
  491. &aesni_generate8("enc") if ($PREFIX eq "aesni");
  492. &aesni_generate8("dec");
  493. if ($PREFIX eq "aesni") {
  494. ########################################################################
  495. # void aesni_ecb_encrypt (const void *in, void *out,
  496. # size_t length, const AES_KEY *key,
  497. # int enc);
  498. $code.=<<___;
  499. .globl aesni_ecb_encrypt
  500. .type aesni_ecb_encrypt,\@function,5
  501. .align 16
  502. aesni_ecb_encrypt:
  503. and \$-16,$len
  504. jz .Lecb_ret
  505. mov 240($key),$rounds # key->rounds
  506. $movkey ($key),$rndkey0
  507. mov $key,$key_ # backup $key
  508. mov $rounds,$rnds_ # backup $rounds
  509. test %r8d,%r8d # 5th argument
  510. jz .Lecb_decrypt
  511. #--------------------------- ECB ENCRYPT ------------------------------#
  512. cmp \$0x80,$len
  513. jb .Lecb_enc_tail
  514. movdqu ($inp),$inout0
  515. movdqu 0x10($inp),$inout1
  516. movdqu 0x20($inp),$inout2
  517. movdqu 0x30($inp),$inout3
  518. movdqu 0x40($inp),$inout4
  519. movdqu 0x50($inp),$inout5
  520. movdqu 0x60($inp),$inout6
  521. movdqu 0x70($inp),$inout7
  522. lea 0x80($inp),$inp
  523. sub \$0x80,$len
  524. jmp .Lecb_enc_loop8_enter
  525. .align 16
  526. .Lecb_enc_loop8:
  527. movups $inout0,($out)
  528. mov $key_,$key # restore $key
  529. movdqu ($inp),$inout0
  530. mov $rnds_,$rounds # restore $rounds
  531. movups $inout1,0x10($out)
  532. movdqu 0x10($inp),$inout1
  533. movups $inout2,0x20($out)
  534. movdqu 0x20($inp),$inout2
  535. movups $inout3,0x30($out)
  536. movdqu 0x30($inp),$inout3
  537. movups $inout4,0x40($out)
  538. movdqu 0x40($inp),$inout4
  539. movups $inout5,0x50($out)
  540. movdqu 0x50($inp),$inout5
  541. movups $inout6,0x60($out)
  542. movdqu 0x60($inp),$inout6
  543. movups $inout7,0x70($out)
  544. lea 0x80($out),$out
  545. movdqu 0x70($inp),$inout7
  546. lea 0x80($inp),$inp
  547. .Lecb_enc_loop8_enter:
  548. call _aesni_encrypt8
  549. sub \$0x80,$len
  550. jnc .Lecb_enc_loop8
  551. movups $inout0,($out)
  552. mov $key_,$key # restore $key
  553. movups $inout1,0x10($out)
  554. mov $rnds_,$rounds # restore $rounds
  555. movups $inout2,0x20($out)
  556. movups $inout3,0x30($out)
  557. movups $inout4,0x40($out)
  558. movups $inout5,0x50($out)
  559. movups $inout6,0x60($out)
  560. movups $inout7,0x70($out)
  561. lea 0x80($out),$out
  562. add \$0x80,$len
  563. jz .Lecb_ret
  564. .Lecb_enc_tail:
  565. movups ($inp),$inout0
  566. cmp \$0x20,$len
  567. jb .Lecb_enc_one
  568. movups 0x10($inp),$inout1
  569. je .Lecb_enc_two
  570. movups 0x20($inp),$inout2
  571. cmp \$0x40,$len
  572. jb .Lecb_enc_three
  573. movups 0x30($inp),$inout3
  574. je .Lecb_enc_four
  575. movups 0x40($inp),$inout4
  576. cmp \$0x60,$len
  577. jb .Lecb_enc_five
  578. movups 0x50($inp),$inout5
  579. je .Lecb_enc_six
  580. movdqu 0x60($inp),$inout6
  581. call _aesni_encrypt8
  582. movups $inout0,($out)
  583. movups $inout1,0x10($out)
  584. movups $inout2,0x20($out)
  585. movups $inout3,0x30($out)
  586. movups $inout4,0x40($out)
  587. movups $inout5,0x50($out)
  588. movups $inout6,0x60($out)
  589. jmp .Lecb_ret
  590. .align 16
  591. .Lecb_enc_one:
  592. ___
  593. &aesni_generate1("enc",$key,$rounds);
  594. $code.=<<___;
  595. movups $inout0,($out)
  596. jmp .Lecb_ret
  597. .align 16
  598. .Lecb_enc_two:
  599. xorps $inout2,$inout2
  600. call _aesni_encrypt3
  601. movups $inout0,($out)
  602. movups $inout1,0x10($out)
  603. jmp .Lecb_ret
  604. .align 16
  605. .Lecb_enc_three:
  606. call _aesni_encrypt3
  607. movups $inout0,($out)
  608. movups $inout1,0x10($out)
  609. movups $inout2,0x20($out)
  610. jmp .Lecb_ret
  611. .align 16
  612. .Lecb_enc_four:
  613. call _aesni_encrypt4
  614. movups $inout0,($out)
  615. movups $inout1,0x10($out)
  616. movups $inout2,0x20($out)
  617. movups $inout3,0x30($out)
  618. jmp .Lecb_ret
  619. .align 16
  620. .Lecb_enc_five:
  621. xorps $inout5,$inout5
  622. call _aesni_encrypt6
  623. movups $inout0,($out)
  624. movups $inout1,0x10($out)
  625. movups $inout2,0x20($out)
  626. movups $inout3,0x30($out)
  627. movups $inout4,0x40($out)
  628. jmp .Lecb_ret
  629. .align 16
  630. .Lecb_enc_six:
  631. call _aesni_encrypt6
  632. movups $inout0,($out)
  633. movups $inout1,0x10($out)
  634. movups $inout2,0x20($out)
  635. movups $inout3,0x30($out)
  636. movups $inout4,0x40($out)
  637. movups $inout5,0x50($out)
  638. jmp .Lecb_ret
  639. #--------------------------- ECB DECRYPT ------------------------------#
  640. .align 16
  641. .Lecb_decrypt:
  642. cmp \$0x80,$len
  643. jb .Lecb_dec_tail
  644. movdqu ($inp),$inout0
  645. movdqu 0x10($inp),$inout1
  646. movdqu 0x20($inp),$inout2
  647. movdqu 0x30($inp),$inout3
  648. movdqu 0x40($inp),$inout4
  649. movdqu 0x50($inp),$inout5
  650. movdqu 0x60($inp),$inout6
  651. movdqu 0x70($inp),$inout7
  652. lea 0x80($inp),$inp
  653. sub \$0x80,$len
  654. jmp .Lecb_dec_loop8_enter
  655. .align 16
  656. .Lecb_dec_loop8:
  657. movups $inout0,($out)
  658. mov $key_,$key # restore $key
  659. movdqu ($inp),$inout0
  660. mov $rnds_,$rounds # restore $rounds
  661. movups $inout1,0x10($out)
  662. movdqu 0x10($inp),$inout1
  663. movups $inout2,0x20($out)
  664. movdqu 0x20($inp),$inout2
  665. movups $inout3,0x30($out)
  666. movdqu 0x30($inp),$inout3
  667. movups $inout4,0x40($out)
  668. movdqu 0x40($inp),$inout4
  669. movups $inout5,0x50($out)
  670. movdqu 0x50($inp),$inout5
  671. movups $inout6,0x60($out)
  672. movdqu 0x60($inp),$inout6
  673. movups $inout7,0x70($out)
  674. lea 0x80($out),$out
  675. movdqu 0x70($inp),$inout7
  676. lea 0x80($inp),$inp
  677. .Lecb_dec_loop8_enter:
  678. call _aesni_decrypt8
  679. $movkey ($key_),$rndkey0
  680. sub \$0x80,$len
  681. jnc .Lecb_dec_loop8
  682. movups $inout0,($out)
  683. mov $key_,$key # restore $key
  684. movups $inout1,0x10($out)
  685. mov $rnds_,$rounds # restore $rounds
  686. movups $inout2,0x20($out)
  687. movups $inout3,0x30($out)
  688. movups $inout4,0x40($out)
  689. movups $inout5,0x50($out)
  690. movups $inout6,0x60($out)
  691. movups $inout7,0x70($out)
  692. lea 0x80($out),$out
  693. add \$0x80,$len
  694. jz .Lecb_ret
  695. .Lecb_dec_tail:
  696. movups ($inp),$inout0
  697. cmp \$0x20,$len
  698. jb .Lecb_dec_one
  699. movups 0x10($inp),$inout1
  700. je .Lecb_dec_two
  701. movups 0x20($inp),$inout2
  702. cmp \$0x40,$len
  703. jb .Lecb_dec_three
  704. movups 0x30($inp),$inout3
  705. je .Lecb_dec_four
  706. movups 0x40($inp),$inout4
  707. cmp \$0x60,$len
  708. jb .Lecb_dec_five
  709. movups 0x50($inp),$inout5
  710. je .Lecb_dec_six
  711. movups 0x60($inp),$inout6
  712. $movkey ($key),$rndkey0
  713. call _aesni_decrypt8
  714. movups $inout0,($out)
  715. movups $inout1,0x10($out)
  716. movups $inout2,0x20($out)
  717. movups $inout3,0x30($out)
  718. movups $inout4,0x40($out)
  719. movups $inout5,0x50($out)
  720. movups $inout6,0x60($out)
  721. jmp .Lecb_ret
  722. .align 16
  723. .Lecb_dec_one:
  724. ___
  725. &aesni_generate1("dec",$key,$rounds);
  726. $code.=<<___;
  727. movups $inout0,($out)
  728. jmp .Lecb_ret
  729. .align 16
  730. .Lecb_dec_two:
  731. xorps $inout2,$inout2
  732. call _aesni_decrypt3
  733. movups $inout0,($out)
  734. movups $inout1,0x10($out)
  735. jmp .Lecb_ret
  736. .align 16
  737. .Lecb_dec_three:
  738. call _aesni_decrypt3
  739. movups $inout0,($out)
  740. movups $inout1,0x10($out)
  741. movups $inout2,0x20($out)
  742. jmp .Lecb_ret
  743. .align 16
  744. .Lecb_dec_four:
  745. call _aesni_decrypt4
  746. movups $inout0,($out)
  747. movups $inout1,0x10($out)
  748. movups $inout2,0x20($out)
  749. movups $inout3,0x30($out)
  750. jmp .Lecb_ret
  751. .align 16
  752. .Lecb_dec_five:
  753. xorps $inout5,$inout5
  754. call _aesni_decrypt6
  755. movups $inout0,($out)
  756. movups $inout1,0x10($out)
  757. movups $inout2,0x20($out)
  758. movups $inout3,0x30($out)
  759. movups $inout4,0x40($out)
  760. jmp .Lecb_ret
  761. .align 16
  762. .Lecb_dec_six:
  763. call _aesni_decrypt6
  764. movups $inout0,($out)
  765. movups $inout1,0x10($out)
  766. movups $inout2,0x20($out)
  767. movups $inout3,0x30($out)
  768. movups $inout4,0x40($out)
  769. movups $inout5,0x50($out)
  770. .Lecb_ret:
  771. ret
  772. .size aesni_ecb_encrypt,.-aesni_ecb_encrypt
  773. ___
  774. {
  775. ######################################################################
  776. # void aesni_ccm64_[en|de]crypt_blocks (const void *in, void *out,
  777. # size_t blocks, const AES_KEY *key,
  778. # const char *ivec,char *cmac);
  779. #
  780. # Handles only complete blocks, operates on 64-bit counter and
  781. # does not update *ivec! Nor does it finalize CMAC value
  782. # (see engine/eng_aesni.c for details)
  783. #
  784. {
  785. my $cmac="%r9"; # 6th argument
  786. my $increment="%xmm6";
  787. my $bswap_mask="%xmm7";
  788. $code.=<<___;
  789. .globl aesni_ccm64_encrypt_blocks
  790. .type aesni_ccm64_encrypt_blocks,\@function,6
  791. .align 16
  792. aesni_ccm64_encrypt_blocks:
  793. ___
  794. $code.=<<___ if ($win64);
  795. lea -0x58(%rsp),%rsp
  796. movaps %xmm6,(%rsp)
  797. movaps %xmm7,0x10(%rsp)
  798. movaps %xmm8,0x20(%rsp)
  799. movaps %xmm9,0x30(%rsp)
  800. .Lccm64_enc_body:
  801. ___
  802. $code.=<<___;
  803. mov 240($key),$rounds # key->rounds
  804. movdqu ($ivp),$iv
  805. movdqa .Lincrement64(%rip),$increment
  806. movdqa .Lbswap_mask(%rip),$bswap_mask
  807. shr \$1,$rounds
  808. lea 0($key),$key_
  809. movdqu ($cmac),$inout1
  810. movdqa $iv,$inout0
  811. mov $rounds,$rnds_
  812. pshufb $bswap_mask,$iv
  813. jmp .Lccm64_enc_outer
  814. .align 16
  815. .Lccm64_enc_outer:
  816. $movkey ($key_),$rndkey0
  817. mov $rnds_,$rounds
  818. movups ($inp),$in0 # load inp
  819. xorps $rndkey0,$inout0 # counter
  820. $movkey 16($key_),$rndkey1
  821. xorps $in0,$rndkey0
  822. lea 32($key_),$key
  823. xorps $rndkey0,$inout1 # cmac^=inp
  824. $movkey ($key),$rndkey0
  825. .Lccm64_enc2_loop:
  826. aesenc $rndkey1,$inout0
  827. dec $rounds
  828. aesenc $rndkey1,$inout1
  829. $movkey 16($key),$rndkey1
  830. aesenc $rndkey0,$inout0
  831. lea 32($key),$key
  832. aesenc $rndkey0,$inout1
  833. $movkey 0($key),$rndkey0
  834. jnz .Lccm64_enc2_loop
  835. aesenc $rndkey1,$inout0
  836. aesenc $rndkey1,$inout1
  837. paddq $increment,$iv
  838. aesenclast $rndkey0,$inout0
  839. aesenclast $rndkey0,$inout1
  840. dec $len
  841. lea 16($inp),$inp
  842. xorps $inout0,$in0 # inp ^= E(iv)
  843. movdqa $iv,$inout0
  844. movups $in0,($out) # save output
  845. lea 16($out),$out
  846. pshufb $bswap_mask,$inout0
  847. jnz .Lccm64_enc_outer
  848. movups $inout1,($cmac)
  849. ___
  850. $code.=<<___ if ($win64);
  851. movaps (%rsp),%xmm6
  852. movaps 0x10(%rsp),%xmm7
  853. movaps 0x20(%rsp),%xmm8
  854. movaps 0x30(%rsp),%xmm9
  855. lea 0x58(%rsp),%rsp
  856. .Lccm64_enc_ret:
  857. ___
  858. $code.=<<___;
  859. ret
  860. .size aesni_ccm64_encrypt_blocks,.-aesni_ccm64_encrypt_blocks
  861. ___
  862. ######################################################################
  863. $code.=<<___;
  864. .globl aesni_ccm64_decrypt_blocks
  865. .type aesni_ccm64_decrypt_blocks,\@function,6
  866. .align 16
  867. aesni_ccm64_decrypt_blocks:
  868. ___
  869. $code.=<<___ if ($win64);
  870. lea -0x58(%rsp),%rsp
  871. movaps %xmm6,(%rsp)
  872. movaps %xmm7,0x10(%rsp)
  873. movaps %xmm8,0x20(%rsp)
  874. movaps %xmm9,0x30(%rsp)
  875. .Lccm64_dec_body:
  876. ___
  877. $code.=<<___;
  878. mov 240($key),$rounds # key->rounds
  879. movups ($ivp),$iv
  880. movdqu ($cmac),$inout1
  881. movdqa .Lincrement64(%rip),$increment
  882. movdqa .Lbswap_mask(%rip),$bswap_mask
  883. movaps $iv,$inout0
  884. mov $rounds,$rnds_
  885. mov $key,$key_
  886. pshufb $bswap_mask,$iv
  887. ___
  888. &aesni_generate1("enc",$key,$rounds);
  889. $code.=<<___;
  890. movups ($inp),$in0 # load inp
  891. paddq $increment,$iv
  892. lea 16($inp),$inp
  893. jmp .Lccm64_dec_outer
  894. .align 16
  895. .Lccm64_dec_outer:
  896. xorps $inout0,$in0 # inp ^= E(iv)
  897. movdqa $iv,$inout0
  898. mov $rnds_,$rounds
  899. movups $in0,($out) # save output
  900. lea 16($out),$out
  901. pshufb $bswap_mask,$inout0
  902. sub \$1,$len
  903. jz .Lccm64_dec_break
  904. $movkey ($key_),$rndkey0
  905. shr \$1,$rounds
  906. $movkey 16($key_),$rndkey1
  907. xorps $rndkey0,$in0
  908. lea 32($key_),$key
  909. xorps $rndkey0,$inout0
  910. xorps $in0,$inout1 # cmac^=out
  911. $movkey ($key),$rndkey0
  912. .Lccm64_dec2_loop:
  913. aesenc $rndkey1,$inout0
  914. dec $rounds
  915. aesenc $rndkey1,$inout1
  916. $movkey 16($key),$rndkey1
  917. aesenc $rndkey0,$inout0
  918. lea 32($key),$key
  919. aesenc $rndkey0,$inout1
  920. $movkey 0($key),$rndkey0
  921. jnz .Lccm64_dec2_loop
  922. movups ($inp),$in0 # load inp
  923. paddq $increment,$iv
  924. aesenc $rndkey1,$inout0
  925. aesenc $rndkey1,$inout1
  926. lea 16($inp),$inp
  927. aesenclast $rndkey0,$inout0
  928. aesenclast $rndkey0,$inout1
  929. jmp .Lccm64_dec_outer
  930. .align 16
  931. .Lccm64_dec_break:
  932. #xorps $in0,$inout1 # cmac^=out
  933. ___
  934. &aesni_generate1("enc",$key_,$rounds,$inout1,$in0);
  935. $code.=<<___;
  936. movups $inout1,($cmac)
  937. ___
  938. $code.=<<___ if ($win64);
  939. movaps (%rsp),%xmm6
  940. movaps 0x10(%rsp),%xmm7
  941. movaps 0x20(%rsp),%xmm8
  942. movaps 0x30(%rsp),%xmm9
  943. lea 0x58(%rsp),%rsp
  944. .Lccm64_dec_ret:
  945. ___
  946. $code.=<<___;
  947. ret
  948. .size aesni_ccm64_decrypt_blocks,.-aesni_ccm64_decrypt_blocks
  949. ___
  950. }
  951. ######################################################################
  952. # void aesni_ctr32_encrypt_blocks (const void *in, void *out,
  953. # size_t blocks, const AES_KEY *key,
  954. # const char *ivec);
  955. #
  956. # Handles only complete blocks, operates on 32-bit counter and
  957. # does not update *ivec! (see engine/eng_aesni.c for details)
  958. #
  959. {
  960. my $reserved = $win64?0:-0x28;
  961. my ($in0,$in1,$in2,$in3)=map("%xmm$_",(8..11));
  962. my ($iv0,$iv1,$ivec)=("%xmm12","%xmm13","%xmm14");
  963. my $bswap_mask="%xmm15";
  964. $code.=<<___;
  965. .globl aesni_ctr32_encrypt_blocks
  966. .type aesni_ctr32_encrypt_blocks,\@function,5
  967. .align 16
  968. aesni_ctr32_encrypt_blocks:
  969. ___
  970. $code.=<<___ if ($win64);
  971. lea -0xc8(%rsp),%rsp
  972. movaps %xmm6,0x20(%rsp)
  973. movaps %xmm7,0x30(%rsp)
  974. movaps %xmm8,0x40(%rsp)
  975. movaps %xmm9,0x50(%rsp)
  976. movaps %xmm10,0x60(%rsp)
  977. movaps %xmm11,0x70(%rsp)
  978. movaps %xmm12,0x80(%rsp)
  979. movaps %xmm13,0x90(%rsp)
  980. movaps %xmm14,0xa0(%rsp)
  981. movaps %xmm15,0xb0(%rsp)
  982. .Lctr32_body:
  983. ___
  984. $code.=<<___;
  985. cmp \$1,$len
  986. je .Lctr32_one_shortcut
  987. movdqu ($ivp),$ivec
  988. movdqa .Lbswap_mask(%rip),$bswap_mask
  989. xor $rounds,$rounds
  990. pextrd \$3,$ivec,$rnds_ # pull 32-bit counter
  991. pinsrd \$3,$rounds,$ivec # wipe 32-bit counter
  992. mov 240($key),$rounds # key->rounds
  993. bswap $rnds_
  994. pxor $iv0,$iv0 # vector of 3 32-bit counters
  995. pxor $iv1,$iv1 # vector of 3 32-bit counters
  996. pinsrd \$0,$rnds_,$iv0
  997. lea 3($rnds_),$key_
  998. pinsrd \$0,$key_,$iv1
  999. inc $rnds_
  1000. pinsrd \$1,$rnds_,$iv0
  1001. inc $key_
  1002. pinsrd \$1,$key_,$iv1
  1003. inc $rnds_
  1004. pinsrd \$2,$rnds_,$iv0
  1005. inc $key_
  1006. pinsrd \$2,$key_,$iv1
  1007. movdqa $iv0,$reserved(%rsp)
  1008. pshufb $bswap_mask,$iv0
  1009. movdqa $iv1,`$reserved+0x10`(%rsp)
  1010. pshufb $bswap_mask,$iv1
  1011. pshufd \$`3<<6`,$iv0,$inout0 # place counter to upper dword
  1012. pshufd \$`2<<6`,$iv0,$inout1
  1013. pshufd \$`1<<6`,$iv0,$inout2
  1014. cmp \$6,$len
  1015. jb .Lctr32_tail
  1016. shr \$1,$rounds
  1017. mov $key,$key_ # backup $key
  1018. mov $rounds,$rnds_ # backup $rounds
  1019. sub \$6,$len
  1020. jmp .Lctr32_loop6
  1021. .align 16
  1022. .Lctr32_loop6:
  1023. pshufd \$`3<<6`,$iv1,$inout3
  1024. por $ivec,$inout0 # merge counter-less ivec
  1025. $movkey ($key_),$rndkey0
  1026. pshufd \$`2<<6`,$iv1,$inout4
  1027. por $ivec,$inout1
  1028. $movkey 16($key_),$rndkey1
  1029. pshufd \$`1<<6`,$iv1,$inout5
  1030. por $ivec,$inout2
  1031. por $ivec,$inout3
  1032. xorps $rndkey0,$inout0
  1033. por $ivec,$inout4
  1034. por $ivec,$inout5
  1035. # inline _aesni_encrypt6 and interleave last rounds
  1036. # with own code...
  1037. pxor $rndkey0,$inout1
  1038. aesenc $rndkey1,$inout0
  1039. lea 32($key_),$key
  1040. pxor $rndkey0,$inout2
  1041. aesenc $rndkey1,$inout1
  1042. movdqa .Lincrement32(%rip),$iv1
  1043. pxor $rndkey0,$inout3
  1044. aesenc $rndkey1,$inout2
  1045. movdqa $reserved(%rsp),$iv0
  1046. pxor $rndkey0,$inout4
  1047. aesenc $rndkey1,$inout3
  1048. pxor $rndkey0,$inout5
  1049. $movkey ($key),$rndkey0
  1050. dec $rounds
  1051. aesenc $rndkey1,$inout4
  1052. aesenc $rndkey1,$inout5
  1053. jmp .Lctr32_enc_loop6_enter
  1054. .align 16
  1055. .Lctr32_enc_loop6:
  1056. aesenc $rndkey1,$inout0
  1057. aesenc $rndkey1,$inout1
  1058. dec $rounds
  1059. aesenc $rndkey1,$inout2
  1060. aesenc $rndkey1,$inout3
  1061. aesenc $rndkey1,$inout4
  1062. aesenc $rndkey1,$inout5
  1063. .Lctr32_enc_loop6_enter:
  1064. $movkey 16($key),$rndkey1
  1065. aesenc $rndkey0,$inout0
  1066. aesenc $rndkey0,$inout1
  1067. lea 32($key),$key
  1068. aesenc $rndkey0,$inout2
  1069. aesenc $rndkey0,$inout3
  1070. aesenc $rndkey0,$inout4
  1071. aesenc $rndkey0,$inout5
  1072. $movkey ($key),$rndkey0
  1073. jnz .Lctr32_enc_loop6
  1074. aesenc $rndkey1,$inout0
  1075. paddd $iv1,$iv0 # increment counter vector
  1076. aesenc $rndkey1,$inout1
  1077. paddd `$reserved+0x10`(%rsp),$iv1
  1078. aesenc $rndkey1,$inout2
  1079. movdqa $iv0,$reserved(%rsp) # save counter vector
  1080. aesenc $rndkey1,$inout3
  1081. movdqa $iv1,`$reserved+0x10`(%rsp)
  1082. aesenc $rndkey1,$inout4
  1083. pshufb $bswap_mask,$iv0 # byte swap
  1084. aesenc $rndkey1,$inout5
  1085. pshufb $bswap_mask,$iv1
  1086. aesenclast $rndkey0,$inout0
  1087. movups ($inp),$in0 # load input
  1088. aesenclast $rndkey0,$inout1
  1089. movups 0x10($inp),$in1
  1090. aesenclast $rndkey0,$inout2
  1091. movups 0x20($inp),$in2
  1092. aesenclast $rndkey0,$inout3
  1093. movups 0x30($inp),$in3
  1094. aesenclast $rndkey0,$inout4
  1095. movups 0x40($inp),$rndkey1
  1096. aesenclast $rndkey0,$inout5
  1097. movups 0x50($inp),$rndkey0
  1098. lea 0x60($inp),$inp
  1099. xorps $inout0,$in0 # xor
  1100. pshufd \$`3<<6`,$iv0,$inout0
  1101. xorps $inout1,$in1
  1102. pshufd \$`2<<6`,$iv0,$inout1
  1103. movups $in0,($out) # store output
  1104. xorps $inout2,$in2
  1105. pshufd \$`1<<6`,$iv0,$inout2
  1106. movups $in1,0x10($out)
  1107. xorps $inout3,$in3
  1108. movups $in2,0x20($out)
  1109. xorps $inout4,$rndkey1
  1110. movups $in3,0x30($out)
  1111. xorps $inout5,$rndkey0
  1112. movups $rndkey1,0x40($out)
  1113. movups $rndkey0,0x50($out)
  1114. lea 0x60($out),$out
  1115. mov $rnds_,$rounds
  1116. sub \$6,$len
  1117. jnc .Lctr32_loop6
  1118. add \$6,$len
  1119. jz .Lctr32_done
  1120. mov $key_,$key # restore $key
  1121. lea 1($rounds,$rounds),$rounds # restore original value
  1122. .Lctr32_tail:
  1123. por $ivec,$inout0
  1124. movups ($inp),$in0
  1125. cmp \$2,$len
  1126. jb .Lctr32_one
  1127. por $ivec,$inout1
  1128. movups 0x10($inp),$in1
  1129. je .Lctr32_two
  1130. pshufd \$`3<<6`,$iv1,$inout3
  1131. por $ivec,$inout2
  1132. movups 0x20($inp),$in2
  1133. cmp \$4,$len
  1134. jb .Lctr32_three
  1135. pshufd \$`2<<6`,$iv1,$inout4
  1136. por $ivec,$inout3
  1137. movups 0x30($inp),$in3
  1138. je .Lctr32_four
  1139. por $ivec,$inout4
  1140. xorps $inout5,$inout5
  1141. call _aesni_encrypt6
  1142. movups 0x40($inp),$rndkey1
  1143. xorps $inout0,$in0
  1144. xorps $inout1,$in1
  1145. movups $in0,($out)
  1146. xorps $inout2,$in2
  1147. movups $in1,0x10($out)
  1148. xorps $inout3,$in3
  1149. movups $in2,0x20($out)
  1150. xorps $inout4,$rndkey1
  1151. movups $in3,0x30($out)
  1152. movups $rndkey1,0x40($out)
  1153. jmp .Lctr32_done
  1154. .align 16
  1155. .Lctr32_one_shortcut:
  1156. movups ($ivp),$inout0
  1157. movups ($inp),$in0
  1158. mov 240($key),$rounds # key->rounds
  1159. .Lctr32_one:
  1160. ___
  1161. &aesni_generate1("enc",$key,$rounds);
  1162. $code.=<<___;
  1163. xorps $inout0,$in0
  1164. movups $in0,($out)
  1165. jmp .Lctr32_done
  1166. .align 16
  1167. .Lctr32_two:
  1168. xorps $inout2,$inout2
  1169. call _aesni_encrypt3
  1170. xorps $inout0,$in0
  1171. xorps $inout1,$in1
  1172. movups $in0,($out)
  1173. movups $in1,0x10($out)
  1174. jmp .Lctr32_done
  1175. .align 16
  1176. .Lctr32_three:
  1177. call _aesni_encrypt3
  1178. xorps $inout0,$in0
  1179. xorps $inout1,$in1
  1180. movups $in0,($out)
  1181. xorps $inout2,$in2
  1182. movups $in1,0x10($out)
  1183. movups $in2,0x20($out)
  1184. jmp .Lctr32_done
  1185. .align 16
  1186. .Lctr32_four:
  1187. call _aesni_encrypt4
  1188. xorps $inout0,$in0
  1189. xorps $inout1,$in1
  1190. movups $in0,($out)
  1191. xorps $inout2,$in2
  1192. movups $in1,0x10($out)
  1193. xorps $inout3,$in3
  1194. movups $in2,0x20($out)
  1195. movups $in3,0x30($out)
  1196. .Lctr32_done:
  1197. ___
  1198. $code.=<<___ if ($win64);
  1199. movaps 0x20(%rsp),%xmm6
  1200. movaps 0x30(%rsp),%xmm7
  1201. movaps 0x40(%rsp),%xmm8
  1202. movaps 0x50(%rsp),%xmm9
  1203. movaps 0x60(%rsp),%xmm10
  1204. movaps 0x70(%rsp),%xmm11
  1205. movaps 0x80(%rsp),%xmm12
  1206. movaps 0x90(%rsp),%xmm13
  1207. movaps 0xa0(%rsp),%xmm14
  1208. movaps 0xb0(%rsp),%xmm15
  1209. lea 0xc8(%rsp),%rsp
  1210. .Lctr32_ret:
  1211. ___
  1212. $code.=<<___;
  1213. ret
  1214. .size aesni_ctr32_encrypt_blocks,.-aesni_ctr32_encrypt_blocks
  1215. ___
  1216. }
  1217. ######################################################################
  1218. # void aesni_xts_[en|de]crypt(const char *inp,char *out,size_t len,
  1219. # const AES_KEY *key1, const AES_KEY *key2
  1220. # const unsigned char iv[16]);
  1221. #
  1222. {
  1223. my @tweak=map("%xmm$_",(10..15));
  1224. my ($twmask,$twres,$twtmp)=("%xmm8","%xmm9",@tweak[4]);
  1225. my ($key2,$ivp,$len_)=("%r8","%r9","%r9");
  1226. my $frame_size = 0x68 + ($win64?160:0);
  1227. $code.=<<___;
  1228. .globl aesni_xts_encrypt
  1229. .type aesni_xts_encrypt,\@function,6
  1230. .align 16
  1231. aesni_xts_encrypt:
  1232. lea -$frame_size(%rsp),%rsp
  1233. ___
  1234. $code.=<<___ if ($win64);
  1235. movaps %xmm6,0x60(%rsp)
  1236. movaps %xmm7,0x70(%rsp)
  1237. movaps %xmm8,0x80(%rsp)
  1238. movaps %xmm9,0x90(%rsp)
  1239. movaps %xmm10,0xa0(%rsp)
  1240. movaps %xmm11,0xb0(%rsp)
  1241. movaps %xmm12,0xc0(%rsp)
  1242. movaps %xmm13,0xd0(%rsp)
  1243. movaps %xmm14,0xe0(%rsp)
  1244. movaps %xmm15,0xf0(%rsp)
  1245. .Lxts_enc_body:
  1246. ___
  1247. $code.=<<___;
  1248. movups ($ivp),@tweak[5] # load clear-text tweak
  1249. mov 240(%r8),$rounds # key2->rounds
  1250. mov 240($key),$rnds_ # key1->rounds
  1251. ___
  1252. # generate the tweak
  1253. &aesni_generate1("enc",$key2,$rounds,@tweak[5]);
  1254. $code.=<<___;
  1255. mov $key,$key_ # backup $key
  1256. mov $rnds_,$rounds # backup $rounds
  1257. mov $len,$len_ # backup $len
  1258. and \$-16,$len
  1259. movdqa .Lxts_magic(%rip),$twmask
  1260. pxor $twtmp,$twtmp
  1261. pcmpgtd @tweak[5],$twtmp # broadcast upper bits
  1262. ___
  1263. for ($i=0;$i<4;$i++) {
  1264. $code.=<<___;
  1265. pshufd \$0x13,$twtmp,$twres
  1266. pxor $twtmp,$twtmp
  1267. movdqa @tweak[5],@tweak[$i]
  1268. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1269. pand $twmask,$twres # isolate carry and residue
  1270. pcmpgtd @tweak[5],$twtmp # broadcat upper bits
  1271. pxor $twres,@tweak[5]
  1272. ___
  1273. }
  1274. $code.=<<___;
  1275. sub \$16*6,$len
  1276. jc .Lxts_enc_short
  1277. shr \$1,$rounds
  1278. sub \$1,$rounds
  1279. mov $rounds,$rnds_
  1280. jmp .Lxts_enc_grandloop
  1281. .align 16
  1282. .Lxts_enc_grandloop:
  1283. pshufd \$0x13,$twtmp,$twres
  1284. movdqa @tweak[5],@tweak[4]
  1285. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1286. movdqu `16*0`($inp),$inout0 # load input
  1287. pand $twmask,$twres # isolate carry and residue
  1288. movdqu `16*1`($inp),$inout1
  1289. pxor $twres,@tweak[5]
  1290. movdqu `16*2`($inp),$inout2
  1291. pxor @tweak[0],$inout0 # input^=tweak
  1292. movdqu `16*3`($inp),$inout3
  1293. pxor @tweak[1],$inout1
  1294. movdqu `16*4`($inp),$inout4
  1295. pxor @tweak[2],$inout2
  1296. movdqu `16*5`($inp),$inout5
  1297. lea `16*6`($inp),$inp
  1298. pxor @tweak[3],$inout3
  1299. $movkey ($key_),$rndkey0
  1300. pxor @tweak[4],$inout4
  1301. pxor @tweak[5],$inout5
  1302. # inline _aesni_encrypt6 and interleave first and last rounds
  1303. # with own code...
  1304. $movkey 16($key_),$rndkey1
  1305. pxor $rndkey0,$inout0
  1306. pxor $rndkey0,$inout1
  1307. movdqa @tweak[0],`16*0`(%rsp) # put aside tweaks
  1308. aesenc $rndkey1,$inout0
  1309. lea 32($key_),$key
  1310. pxor $rndkey0,$inout2
  1311. movdqa @tweak[1],`16*1`(%rsp)
  1312. aesenc $rndkey1,$inout1
  1313. pxor $rndkey0,$inout3
  1314. movdqa @tweak[2],`16*2`(%rsp)
  1315. aesenc $rndkey1,$inout2
  1316. pxor $rndkey0,$inout4
  1317. movdqa @tweak[3],`16*3`(%rsp)
  1318. aesenc $rndkey1,$inout3
  1319. pxor $rndkey0,$inout5
  1320. $movkey ($key),$rndkey0
  1321. dec $rounds
  1322. movdqa @tweak[4],`16*4`(%rsp)
  1323. aesenc $rndkey1,$inout4
  1324. movdqa @tweak[5],`16*5`(%rsp)
  1325. aesenc $rndkey1,$inout5
  1326. pxor $twtmp,$twtmp
  1327. pcmpgtd @tweak[5],$twtmp
  1328. jmp .Lxts_enc_loop6_enter
  1329. .align 16
  1330. .Lxts_enc_loop6:
  1331. aesenc $rndkey1,$inout0
  1332. aesenc $rndkey1,$inout1
  1333. dec $rounds
  1334. aesenc $rndkey1,$inout2
  1335. aesenc $rndkey1,$inout3
  1336. aesenc $rndkey1,$inout4
  1337. aesenc $rndkey1,$inout5
  1338. .Lxts_enc_loop6_enter:
  1339. $movkey 16($key),$rndkey1
  1340. aesenc $rndkey0,$inout0
  1341. aesenc $rndkey0,$inout1
  1342. lea 32($key),$key
  1343. aesenc $rndkey0,$inout2
  1344. aesenc $rndkey0,$inout3
  1345. aesenc $rndkey0,$inout4
  1346. aesenc $rndkey0,$inout5
  1347. $movkey ($key),$rndkey0
  1348. jnz .Lxts_enc_loop6
  1349. pshufd \$0x13,$twtmp,$twres
  1350. pxor $twtmp,$twtmp
  1351. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1352. aesenc $rndkey1,$inout0
  1353. pand $twmask,$twres # isolate carry and residue
  1354. aesenc $rndkey1,$inout1
  1355. pcmpgtd @tweak[5],$twtmp # broadcast upper bits
  1356. aesenc $rndkey1,$inout2
  1357. pxor $twres,@tweak[5]
  1358. aesenc $rndkey1,$inout3
  1359. aesenc $rndkey1,$inout4
  1360. aesenc $rndkey1,$inout5
  1361. $movkey 16($key),$rndkey1
  1362. pshufd \$0x13,$twtmp,$twres
  1363. pxor $twtmp,$twtmp
  1364. movdqa @tweak[5],@tweak[0]
  1365. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1366. aesenc $rndkey0,$inout0
  1367. pand $twmask,$twres # isolate carry and residue
  1368. aesenc $rndkey0,$inout1
  1369. pcmpgtd @tweak[5],$twtmp # broadcat upper bits
  1370. aesenc $rndkey0,$inout2
  1371. pxor $twres,@tweak[5]
  1372. aesenc $rndkey0,$inout3
  1373. aesenc $rndkey0,$inout4
  1374. aesenc $rndkey0,$inout5
  1375. $movkey 32($key),$rndkey0
  1376. pshufd \$0x13,$twtmp,$twres
  1377. pxor $twtmp,$twtmp
  1378. movdqa @tweak[5],@tweak[1]
  1379. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1380. aesenc $rndkey1,$inout0
  1381. pand $twmask,$twres # isolate carry and residue
  1382. aesenc $rndkey1,$inout1
  1383. pcmpgtd @tweak[5],$twtmp # broadcat upper bits
  1384. aesenc $rndkey1,$inout2
  1385. pxor $twres,@tweak[5]
  1386. aesenc $rndkey1,$inout3
  1387. aesenc $rndkey1,$inout4
  1388. aesenc $rndkey1,$inout5
  1389. pshufd \$0x13,$twtmp,$twres
  1390. pxor $twtmp,$twtmp
  1391. movdqa @tweak[5],@tweak[2]
  1392. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1393. aesenclast $rndkey0,$inout0
  1394. pand $twmask,$twres # isolate carry and residue
  1395. aesenclast $rndkey0,$inout1
  1396. pcmpgtd @tweak[5],$twtmp # broadcat upper bits
  1397. aesenclast $rndkey0,$inout2
  1398. pxor $twres,@tweak[5]
  1399. aesenclast $rndkey0,$inout3
  1400. aesenclast $rndkey0,$inout4
  1401. aesenclast $rndkey0,$inout5
  1402. pshufd \$0x13,$twtmp,$twres
  1403. pxor $twtmp,$twtmp
  1404. movdqa @tweak[5],@tweak[3]
  1405. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1406. xorps `16*0`(%rsp),$inout0 # output^=tweak
  1407. pand $twmask,$twres # isolate carry and residue
  1408. xorps `16*1`(%rsp),$inout1
  1409. pcmpgtd @tweak[5],$twtmp # broadcat upper bits
  1410. pxor $twres,@tweak[5]
  1411. xorps `16*2`(%rsp),$inout2
  1412. movups $inout0,`16*0`($out) # write output
  1413. xorps `16*3`(%rsp),$inout3
  1414. movups $inout1,`16*1`($out)
  1415. xorps `16*4`(%rsp),$inout4
  1416. movups $inout2,`16*2`($out)
  1417. xorps `16*5`(%rsp),$inout5
  1418. movups $inout3,`16*3`($out)
  1419. mov $rnds_,$rounds # restore $rounds
  1420. movups $inout4,`16*4`($out)
  1421. movups $inout5,`16*5`($out)
  1422. lea `16*6`($out),$out
  1423. sub \$16*6,$len
  1424. jnc .Lxts_enc_grandloop
  1425. lea 3($rounds,$rounds),$rounds # restore original value
  1426. mov $key_,$key # restore $key
  1427. mov $rounds,$rnds_ # backup $rounds
  1428. .Lxts_enc_short:
  1429. add \$16*6,$len
  1430. jz .Lxts_enc_done
  1431. cmp \$0x20,$len
  1432. jb .Lxts_enc_one
  1433. je .Lxts_enc_two
  1434. cmp \$0x40,$len
  1435. jb .Lxts_enc_three
  1436. je .Lxts_enc_four
  1437. pshufd \$0x13,$twtmp,$twres
  1438. movdqa @tweak[5],@tweak[4]
  1439. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1440. movdqu ($inp),$inout0
  1441. pand $twmask,$twres # isolate carry and residue
  1442. movdqu 16*1($inp),$inout1
  1443. pxor $twres,@tweak[5]
  1444. movdqu 16*2($inp),$inout2
  1445. pxor @tweak[0],$inout0
  1446. movdqu 16*3($inp),$inout3
  1447. pxor @tweak[1],$inout1
  1448. movdqu 16*4($inp),$inout4
  1449. lea 16*5($inp),$inp
  1450. pxor @tweak[2],$inout2
  1451. pxor @tweak[3],$inout3
  1452. pxor @tweak[4],$inout4
  1453. call _aesni_encrypt6
  1454. xorps @tweak[0],$inout0
  1455. movdqa @tweak[5],@tweak[0]
  1456. xorps @tweak[1],$inout1
  1457. xorps @tweak[2],$inout2
  1458. movdqu $inout0,($out)
  1459. xorps @tweak[3],$inout3
  1460. movdqu $inout1,16*1($out)
  1461. xorps @tweak[4],$inout4
  1462. movdqu $inout2,16*2($out)
  1463. movdqu $inout3,16*3($out)
  1464. movdqu $inout4,16*4($out)
  1465. lea 16*5($out),$out
  1466. jmp .Lxts_enc_done
  1467. .align 16
  1468. .Lxts_enc_one:
  1469. movups ($inp),$inout0
  1470. lea 16*1($inp),$inp
  1471. xorps @tweak[0],$inout0
  1472. ___
  1473. &aesni_generate1("enc",$key,$rounds);
  1474. $code.=<<___;
  1475. xorps @tweak[0],$inout0
  1476. movdqa @tweak[1],@tweak[0]
  1477. movups $inout0,($out)
  1478. lea 16*1($out),$out
  1479. jmp .Lxts_enc_done
  1480. .align 16
  1481. .Lxts_enc_two:
  1482. movups ($inp),$inout0
  1483. movups 16($inp),$inout1
  1484. lea 32($inp),$inp
  1485. xorps @tweak[0],$inout0
  1486. xorps @tweak[1],$inout1
  1487. call _aesni_encrypt3
  1488. xorps @tweak[0],$inout0
  1489. movdqa @tweak[2],@tweak[0]
  1490. xorps @tweak[1],$inout1
  1491. movups $inout0,($out)
  1492. movups $inout1,16*1($out)
  1493. lea 16*2($out),$out
  1494. jmp .Lxts_enc_done
  1495. .align 16
  1496. .Lxts_enc_three:
  1497. movups ($inp),$inout0
  1498. movups 16*1($inp),$inout1
  1499. movups 16*2($inp),$inout2
  1500. lea 16*3($inp),$inp
  1501. xorps @tweak[0],$inout0
  1502. xorps @tweak[1],$inout1
  1503. xorps @tweak[2],$inout2
  1504. call _aesni_encrypt3
  1505. xorps @tweak[0],$inout0
  1506. movdqa @tweak[3],@tweak[0]
  1507. xorps @tweak[1],$inout1
  1508. xorps @tweak[2],$inout2
  1509. movups $inout0,($out)
  1510. movups $inout1,16*1($out)
  1511. movups $inout2,16*2($out)
  1512. lea 16*3($out),$out
  1513. jmp .Lxts_enc_done
  1514. .align 16
  1515. .Lxts_enc_four:
  1516. movups ($inp),$inout0
  1517. movups 16*1($inp),$inout1
  1518. movups 16*2($inp),$inout2
  1519. xorps @tweak[0],$inout0
  1520. movups 16*3($inp),$inout3
  1521. lea 16*4($inp),$inp
  1522. xorps @tweak[1],$inout1
  1523. xorps @tweak[2],$inout2
  1524. xorps @tweak[3],$inout3
  1525. call _aesni_encrypt4
  1526. xorps @tweak[0],$inout0
  1527. movdqa @tweak[5],@tweak[0]
  1528. xorps @tweak[1],$inout1
  1529. xorps @tweak[2],$inout2
  1530. movups $inout0,($out)
  1531. xorps @tweak[3],$inout3
  1532. movups $inout1,16*1($out)
  1533. movups $inout2,16*2($out)
  1534. movups $inout3,16*3($out)
  1535. lea 16*4($out),$out
  1536. jmp .Lxts_enc_done
  1537. .align 16
  1538. .Lxts_enc_done:
  1539. and \$15,$len_
  1540. jz .Lxts_enc_ret
  1541. mov $len_,$len
  1542. .Lxts_enc_steal:
  1543. movzb ($inp),%eax # borrow $rounds ...
  1544. movzb -16($out),%ecx # ... and $key
  1545. lea 1($inp),$inp
  1546. mov %al,-16($out)
  1547. mov %cl,0($out)
  1548. lea 1($out),$out
  1549. sub \$1,$len
  1550. jnz .Lxts_enc_steal
  1551. sub $len_,$out # rewind $out
  1552. mov $key_,$key # restore $key
  1553. mov $rnds_,$rounds # restore $rounds
  1554. movups -16($out),$inout0
  1555. xorps @tweak[0],$inout0
  1556. ___
  1557. &aesni_generate1("enc",$key,$rounds);
  1558. $code.=<<___;
  1559. xorps @tweak[0],$inout0
  1560. movups $inout0,-16($out)
  1561. .Lxts_enc_ret:
  1562. ___
  1563. $code.=<<___ if ($win64);
  1564. movaps 0x60(%rsp),%xmm6
  1565. movaps 0x70(%rsp),%xmm7
  1566. movaps 0x80(%rsp),%xmm8
  1567. movaps 0x90(%rsp),%xmm9
  1568. movaps 0xa0(%rsp),%xmm10
  1569. movaps 0xb0(%rsp),%xmm11
  1570. movaps 0xc0(%rsp),%xmm12
  1571. movaps 0xd0(%rsp),%xmm13
  1572. movaps 0xe0(%rsp),%xmm14
  1573. movaps 0xf0(%rsp),%xmm15
  1574. ___
  1575. $code.=<<___;
  1576. lea $frame_size(%rsp),%rsp
  1577. .Lxts_enc_epilogue:
  1578. ret
  1579. .size aesni_xts_encrypt,.-aesni_xts_encrypt
  1580. ___
  1581. $code.=<<___;
  1582. .globl aesni_xts_decrypt
  1583. .type aesni_xts_decrypt,\@function,6
  1584. .align 16
  1585. aesni_xts_decrypt:
  1586. lea -$frame_size(%rsp),%rsp
  1587. ___
  1588. $code.=<<___ if ($win64);
  1589. movaps %xmm6,0x60(%rsp)
  1590. movaps %xmm7,0x70(%rsp)
  1591. movaps %xmm8,0x80(%rsp)
  1592. movaps %xmm9,0x90(%rsp)
  1593. movaps %xmm10,0xa0(%rsp)
  1594. movaps %xmm11,0xb0(%rsp)
  1595. movaps %xmm12,0xc0(%rsp)
  1596. movaps %xmm13,0xd0(%rsp)
  1597. movaps %xmm14,0xe0(%rsp)
  1598. movaps %xmm15,0xf0(%rsp)
  1599. .Lxts_dec_body:
  1600. ___
  1601. $code.=<<___;
  1602. movups ($ivp),@tweak[5] # load clear-text tweak
  1603. mov 240($key2),$rounds # key2->rounds
  1604. mov 240($key),$rnds_ # key1->rounds
  1605. ___
  1606. # generate the tweak
  1607. &aesni_generate1("enc",$key2,$rounds,@tweak[5]);
  1608. $code.=<<___;
  1609. xor %eax,%eax # if ($len%16) len-=16;
  1610. test \$15,$len
  1611. setnz %al
  1612. shl \$4,%rax
  1613. sub %rax,$len
  1614. mov $key,$key_ # backup $key
  1615. mov $rnds_,$rounds # backup $rounds
  1616. mov $len,$len_ # backup $len
  1617. and \$-16,$len
  1618. movdqa .Lxts_magic(%rip),$twmask
  1619. pxor $twtmp,$twtmp
  1620. pcmpgtd @tweak[5],$twtmp # broadcast upper bits
  1621. ___
  1622. for ($i=0;$i<4;$i++) {
  1623. $code.=<<___;
  1624. pshufd \$0x13,$twtmp,$twres
  1625. pxor $twtmp,$twtmp
  1626. movdqa @tweak[5],@tweak[$i]
  1627. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1628. pand $twmask,$twres # isolate carry and residue
  1629. pcmpgtd @tweak[5],$twtmp # broadcat upper bits
  1630. pxor $twres,@tweak[5]
  1631. ___
  1632. }
  1633. $code.=<<___;
  1634. sub \$16*6,$len
  1635. jc .Lxts_dec_short
  1636. shr \$1,$rounds
  1637. sub \$1,$rounds
  1638. mov $rounds,$rnds_
  1639. jmp .Lxts_dec_grandloop
  1640. .align 16
  1641. .Lxts_dec_grandloop:
  1642. pshufd \$0x13,$twtmp,$twres
  1643. movdqa @tweak[5],@tweak[4]
  1644. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1645. movdqu `16*0`($inp),$inout0 # load input
  1646. pand $twmask,$twres # isolate carry and residue
  1647. movdqu `16*1`($inp),$inout1
  1648. pxor $twres,@tweak[5]
  1649. movdqu `16*2`($inp),$inout2
  1650. pxor @tweak[0],$inout0 # input^=tweak
  1651. movdqu `16*3`($inp),$inout3
  1652. pxor @tweak[1],$inout1
  1653. movdqu `16*4`($inp),$inout4
  1654. pxor @tweak[2],$inout2
  1655. movdqu `16*5`($inp),$inout5
  1656. lea `16*6`($inp),$inp
  1657. pxor @tweak[3],$inout3
  1658. $movkey ($key_),$rndkey0
  1659. pxor @tweak[4],$inout4
  1660. pxor @tweak[5],$inout5
  1661. # inline _aesni_decrypt6 and interleave first and last rounds
  1662. # with own code...
  1663. $movkey 16($key_),$rndkey1
  1664. pxor $rndkey0,$inout0
  1665. pxor $rndkey0,$inout1
  1666. movdqa @tweak[0],`16*0`(%rsp) # put aside tweaks
  1667. aesdec $rndkey1,$inout0
  1668. lea 32($key_),$key
  1669. pxor $rndkey0,$inout2
  1670. movdqa @tweak[1],`16*1`(%rsp)
  1671. aesdec $rndkey1,$inout1
  1672. pxor $rndkey0,$inout3
  1673. movdqa @tweak[2],`16*2`(%rsp)
  1674. aesdec $rndkey1,$inout2
  1675. pxor $rndkey0,$inout4
  1676. movdqa @tweak[3],`16*3`(%rsp)
  1677. aesdec $rndkey1,$inout3
  1678. pxor $rndkey0,$inout5
  1679. $movkey ($key),$rndkey0
  1680. dec $rounds
  1681. movdqa @tweak[4],`16*4`(%rsp)
  1682. aesdec $rndkey1,$inout4
  1683. movdqa @tweak[5],`16*5`(%rsp)
  1684. aesdec $rndkey1,$inout5
  1685. pxor $twtmp,$twtmp
  1686. pcmpgtd @tweak[5],$twtmp
  1687. jmp .Lxts_dec_loop6_enter
  1688. .align 16
  1689. .Lxts_dec_loop6:
  1690. aesdec $rndkey1,$inout0
  1691. aesdec $rndkey1,$inout1
  1692. dec $rounds
  1693. aesdec $rndkey1,$inout2
  1694. aesdec $rndkey1,$inout3
  1695. aesdec $rndkey1,$inout4
  1696. aesdec $rndkey1,$inout5
  1697. .Lxts_dec_loop6_enter:
  1698. $movkey 16($key),$rndkey1
  1699. aesdec $rndkey0,$inout0
  1700. aesdec $rndkey0,$inout1
  1701. lea 32($key),$key
  1702. aesdec $rndkey0,$inout2
  1703. aesdec $rndkey0,$inout3
  1704. aesdec $rndkey0,$inout4
  1705. aesdec $rndkey0,$inout5
  1706. $movkey ($key),$rndkey0
  1707. jnz .Lxts_dec_loop6
  1708. pshufd \$0x13,$twtmp,$twres
  1709. pxor $twtmp,$twtmp
  1710. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1711. aesdec $rndkey1,$inout0
  1712. pand $twmask,$twres # isolate carry and residue
  1713. aesdec $rndkey1,$inout1
  1714. pcmpgtd @tweak[5],$twtmp # broadcast upper bits
  1715. aesdec $rndkey1,$inout2
  1716. pxor $twres,@tweak[5]
  1717. aesdec $rndkey1,$inout3
  1718. aesdec $rndkey1,$inout4
  1719. aesdec $rndkey1,$inout5
  1720. $movkey 16($key),$rndkey1
  1721. pshufd \$0x13,$twtmp,$twres
  1722. pxor $twtmp,$twtmp
  1723. movdqa @tweak[5],@tweak[0]
  1724. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1725. aesdec $rndkey0,$inout0
  1726. pand $twmask,$twres # isolate carry and residue
  1727. aesdec $rndkey0,$inout1
  1728. pcmpgtd @tweak[5],$twtmp # broadcat upper bits
  1729. aesdec $rndkey0,$inout2
  1730. pxor $twres,@tweak[5]
  1731. aesdec $rndkey0,$inout3
  1732. aesdec $rndkey0,$inout4
  1733. aesdec $rndkey0,$inout5
  1734. $movkey 32($key),$rndkey0
  1735. pshufd \$0x13,$twtmp,$twres
  1736. pxor $twtmp,$twtmp
  1737. movdqa @tweak[5],@tweak[1]
  1738. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1739. aesdec $rndkey1,$inout0
  1740. pand $twmask,$twres # isolate carry and residue
  1741. aesdec $rndkey1,$inout1
  1742. pcmpgtd @tweak[5],$twtmp # broadcat upper bits
  1743. aesdec $rndkey1,$inout2
  1744. pxor $twres,@tweak[5]
  1745. aesdec $rndkey1,$inout3
  1746. aesdec $rndkey1,$inout4
  1747. aesdec $rndkey1,$inout5
  1748. pshufd \$0x13,$twtmp,$twres
  1749. pxor $twtmp,$twtmp
  1750. movdqa @tweak[5],@tweak[2]
  1751. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1752. aesdeclast $rndkey0,$inout0
  1753. pand $twmask,$twres # isolate carry and residue
  1754. aesdeclast $rndkey0,$inout1
  1755. pcmpgtd @tweak[5],$twtmp # broadcat upper bits
  1756. aesdeclast $rndkey0,$inout2
  1757. pxor $twres,@tweak[5]
  1758. aesdeclast $rndkey0,$inout3
  1759. aesdeclast $rndkey0,$inout4
  1760. aesdeclast $rndkey0,$inout5
  1761. pshufd \$0x13,$twtmp,$twres
  1762. pxor $twtmp,$twtmp
  1763. movdqa @tweak[5],@tweak[3]
  1764. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1765. xorps `16*0`(%rsp),$inout0 # output^=tweak
  1766. pand $twmask,$twres # isolate carry and residue
  1767. xorps `16*1`(%rsp),$inout1
  1768. pcmpgtd @tweak[5],$twtmp # broadcat upper bits
  1769. pxor $twres,@tweak[5]
  1770. xorps `16*2`(%rsp),$inout2
  1771. movups $inout0,`16*0`($out) # write output
  1772. xorps `16*3`(%rsp),$inout3
  1773. movups $inout1,`16*1`($out)
  1774. xorps `16*4`(%rsp),$inout4
  1775. movups $inout2,`16*2`($out)
  1776. xorps `16*5`(%rsp),$inout5
  1777. movups $inout3,`16*3`($out)
  1778. mov $rnds_,$rounds # restore $rounds
  1779. movups $inout4,`16*4`($out)
  1780. movups $inout5,`16*5`($out)
  1781. lea `16*6`($out),$out
  1782. sub \$16*6,$len
  1783. jnc .Lxts_dec_grandloop
  1784. lea 3($rounds,$rounds),$rounds # restore original value
  1785. mov $key_,$key # restore $key
  1786. mov $rounds,$rnds_ # backup $rounds
  1787. .Lxts_dec_short:
  1788. add \$16*6,$len
  1789. jz .Lxts_dec_done
  1790. cmp \$0x20,$len
  1791. jb .Lxts_dec_one
  1792. je .Lxts_dec_two
  1793. cmp \$0x40,$len
  1794. jb .Lxts_dec_three
  1795. je .Lxts_dec_four
  1796. pshufd \$0x13,$twtmp,$twres
  1797. movdqa @tweak[5],@tweak[4]
  1798. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1799. movdqu ($inp),$inout0
  1800. pand $twmask,$twres # isolate carry and residue
  1801. movdqu 16*1($inp),$inout1
  1802. pxor $twres,@tweak[5]
  1803. movdqu 16*2($inp),$inout2
  1804. pxor @tweak[0],$inout0
  1805. movdqu 16*3($inp),$inout3
  1806. pxor @tweak[1],$inout1
  1807. movdqu 16*4($inp),$inout4
  1808. lea 16*5($inp),$inp
  1809. pxor @tweak[2],$inout2
  1810. pxor @tweak[3],$inout3
  1811. pxor @tweak[4],$inout4
  1812. call _aesni_decrypt6
  1813. xorps @tweak[0],$inout0
  1814. xorps @tweak[1],$inout1
  1815. xorps @tweak[2],$inout2
  1816. movdqu $inout0,($out)
  1817. xorps @tweak[3],$inout3
  1818. movdqu $inout1,16*1($out)
  1819. xorps @tweak[4],$inout4
  1820. movdqu $inout2,16*2($out)
  1821. pxor $twtmp,$twtmp
  1822. movdqu $inout3,16*3($out)
  1823. pcmpgtd @tweak[5],$twtmp
  1824. movdqu $inout4,16*4($out)
  1825. lea 16*5($out),$out
  1826. pshufd \$0x13,$twtmp,@tweak[1] # $twres
  1827. and \$15,$len_
  1828. jz .Lxts_dec_ret
  1829. movdqa @tweak[5],@tweak[0]
  1830. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1831. pand $twmask,@tweak[1] # isolate carry and residue
  1832. pxor @tweak[5],@tweak[1]
  1833. jmp .Lxts_dec_done2
  1834. .align 16
  1835. .Lxts_dec_one:
  1836. movups ($inp),$inout0
  1837. lea 16*1($inp),$inp
  1838. xorps @tweak[0],$inout0
  1839. ___
  1840. &aesni_generate1("dec",$key,$rounds);
  1841. $code.=<<___;
  1842. xorps @tweak[0],$inout0
  1843. movdqa @tweak[1],@tweak[0]
  1844. movups $inout0,($out)
  1845. movdqa @tweak[2],@tweak[1]
  1846. lea 16*1($out),$out
  1847. jmp .Lxts_dec_done
  1848. .align 16
  1849. .Lxts_dec_two:
  1850. movups ($inp),$inout0
  1851. movups 16($inp),$inout1
  1852. lea 32($inp),$inp
  1853. xorps @tweak[0],$inout0
  1854. xorps @tweak[1],$inout1
  1855. call _aesni_decrypt3
  1856. xorps @tweak[0],$inout0
  1857. movdqa @tweak[2],@tweak[0]
  1858. xorps @tweak[1],$inout1
  1859. movdqa @tweak[3],@tweak[1]
  1860. movups $inout0,($out)
  1861. movups $inout1,16*1($out)
  1862. lea 16*2($out),$out
  1863. jmp .Lxts_dec_done
  1864. .align 16
  1865. .Lxts_dec_three:
  1866. movups ($inp),$inout0
  1867. movups 16*1($inp),$inout1
  1868. movups 16*2($inp),$inout2
  1869. lea 16*3($inp),$inp
  1870. xorps @tweak[0],$inout0
  1871. xorps @tweak[1],$inout1
  1872. xorps @tweak[2],$inout2
  1873. call _aesni_decrypt3
  1874. xorps @tweak[0],$inout0
  1875. movdqa @tweak[3],@tweak[0]
  1876. xorps @tweak[1],$inout1
  1877. movdqa @tweak[5],@tweak[1]
  1878. xorps @tweak[2],$inout2
  1879. movups $inout0,($out)
  1880. movups $inout1,16*1($out)
  1881. movups $inout2,16*2($out)
  1882. lea 16*3($out),$out
  1883. jmp .Lxts_dec_done
  1884. .align 16
  1885. .Lxts_dec_four:
  1886. pshufd \$0x13,$twtmp,$twres
  1887. movdqa @tweak[5],@tweak[4]
  1888. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1889. movups ($inp),$inout0
  1890. pand $twmask,$twres # isolate carry and residue
  1891. movups 16*1($inp),$inout1
  1892. pxor $twres,@tweak[5]
  1893. movups 16*2($inp),$inout2
  1894. xorps @tweak[0],$inout0
  1895. movups 16*3($inp),$inout3
  1896. lea 16*4($inp),$inp
  1897. xorps @tweak[1],$inout1
  1898. xorps @tweak[2],$inout2
  1899. xorps @tweak[3],$inout3
  1900. call _aesni_decrypt4
  1901. xorps @tweak[0],$inout0
  1902. movdqa @tweak[4],@tweak[0]
  1903. xorps @tweak[1],$inout1
  1904. movdqa @tweak[5],@tweak[1]
  1905. xorps @tweak[2],$inout2
  1906. movups $inout0,($out)
  1907. xorps @tweak[3],$inout3
  1908. movups $inout1,16*1($out)
  1909. movups $inout2,16*2($out)
  1910. movups $inout3,16*3($out)
  1911. lea 16*4($out),$out
  1912. jmp .Lxts_dec_done
  1913. .align 16
  1914. .Lxts_dec_done:
  1915. and \$15,$len_
  1916. jz .Lxts_dec_ret
  1917. .Lxts_dec_done2:
  1918. mov $len_,$len
  1919. mov $key_,$key # restore $key
  1920. mov $rnds_,$rounds # restore $rounds
  1921. movups ($inp),$inout0
  1922. xorps @tweak[1],$inout0
  1923. ___
  1924. &aesni_generate1("dec",$key,$rounds);
  1925. $code.=<<___;
  1926. xorps @tweak[1],$inout0
  1927. movups $inout0,($out)
  1928. .Lxts_dec_steal:
  1929. movzb 16($inp),%eax # borrow $rounds ...
  1930. movzb ($out),%ecx # ... and $key
  1931. lea 1($inp),$inp
  1932. mov %al,($out)
  1933. mov %cl,16($out)
  1934. lea 1($out),$out
  1935. sub \$1,$len
  1936. jnz .Lxts_dec_steal
  1937. sub $len_,$out # rewind $out
  1938. mov $key_,$key # restore $key
  1939. mov $rnds_,$rounds # restore $rounds
  1940. movups ($out),$inout0
  1941. xorps @tweak[0],$inout0
  1942. ___
  1943. &aesni_generate1("dec",$key,$rounds);
  1944. $code.=<<___;
  1945. xorps @tweak[0],$inout0
  1946. movups $inout0,($out)
  1947. .Lxts_dec_ret:
  1948. ___
  1949. $code.=<<___ if ($win64);
  1950. movaps 0x60(%rsp),%xmm6
  1951. movaps 0x70(%rsp),%xmm7
  1952. movaps 0x80(%rsp),%xmm8
  1953. movaps 0x90(%rsp),%xmm9
  1954. movaps 0xa0(%rsp),%xmm10
  1955. movaps 0xb0(%rsp),%xmm11
  1956. movaps 0xc0(%rsp),%xmm12
  1957. movaps 0xd0(%rsp),%xmm13
  1958. movaps 0xe0(%rsp),%xmm14
  1959. movaps 0xf0(%rsp),%xmm15
  1960. ___
  1961. $code.=<<___;
  1962. lea $frame_size(%rsp),%rsp
  1963. .Lxts_dec_epilogue:
  1964. ret
  1965. .size aesni_xts_decrypt,.-aesni_xts_decrypt
  1966. ___
  1967. } }}
  1968. ########################################################################
  1969. # void $PREFIX_cbc_encrypt (const void *inp, void *out,
  1970. # size_t length, const AES_KEY *key,
  1971. # unsigned char *ivp,const int enc);
  1972. {
  1973. my $reserved = $win64?0x40:-0x18; # used in decrypt
  1974. $code.=<<___;
  1975. .globl ${PREFIX}_cbc_encrypt
  1976. .type ${PREFIX}_cbc_encrypt,\@function,6
  1977. .align 16
  1978. ${PREFIX}_cbc_encrypt:
  1979. test $len,$len # check length
  1980. jz .Lcbc_ret
  1981. mov 240($key),$rnds_ # key->rounds
  1982. mov $key,$key_ # backup $key
  1983. test %r9d,%r9d # 6th argument
  1984. jz .Lcbc_decrypt
  1985. #--------------------------- CBC ENCRYPT ------------------------------#
  1986. movups ($ivp),$inout0 # load iv as initial state
  1987. mov $rnds_,$rounds
  1988. cmp \$16,$len
  1989. jb .Lcbc_enc_tail
  1990. sub \$16,$len
  1991. jmp .Lcbc_enc_loop
  1992. .align 16
  1993. .Lcbc_enc_loop:
  1994. movups ($inp),$inout1 # load input
  1995. lea 16($inp),$inp
  1996. #xorps $inout1,$inout0
  1997. ___
  1998. &aesni_generate1("enc",$key,$rounds,$inout0,$inout1);
  1999. $code.=<<___;
  2000. mov $rnds_,$rounds # restore $rounds
  2001. mov $key_,$key # restore $key
  2002. movups $inout0,0($out) # store output
  2003. lea 16($out),$out
  2004. sub \$16,$len
  2005. jnc .Lcbc_enc_loop
  2006. add \$16,$len
  2007. jnz .Lcbc_enc_tail
  2008. movups $inout0,($ivp)
  2009. jmp .Lcbc_ret
  2010. .Lcbc_enc_tail:
  2011. mov $len,%rcx # zaps $key
  2012. xchg $inp,$out # $inp is %rsi and $out is %rdi now
  2013. .long 0x9066A4F3 # rep movsb
  2014. mov \$16,%ecx # zero tail
  2015. sub $len,%rcx
  2016. xor %eax,%eax
  2017. .long 0x9066AAF3 # rep stosb
  2018. lea -16(%rdi),%rdi # rewind $out by 1 block
  2019. mov $rnds_,$rounds # restore $rounds
  2020. mov %rdi,%rsi # $inp and $out are the same
  2021. mov $key_,$key # restore $key
  2022. xor $len,$len # len=16
  2023. jmp .Lcbc_enc_loop # one more spin
  2024. #--------------------------- CBC DECRYPT ------------------------------#
  2025. .align 16
  2026. .Lcbc_decrypt:
  2027. ___
  2028. $code.=<<___ if ($win64);
  2029. lea -0x58(%rsp),%rsp
  2030. movaps %xmm6,(%rsp)
  2031. movaps %xmm7,0x10(%rsp)
  2032. movaps %xmm8,0x20(%rsp)
  2033. movaps %xmm9,0x30(%rsp)
  2034. .Lcbc_decrypt_body:
  2035. ___
  2036. $code.=<<___;
  2037. movups ($ivp),$iv
  2038. mov $rnds_,$rounds
  2039. cmp \$0x70,$len
  2040. jbe .Lcbc_dec_tail
  2041. shr \$1,$rnds_
  2042. sub \$0x70,$len
  2043. mov $rnds_,$rounds
  2044. movaps $iv,$reserved(%rsp)
  2045. jmp .Lcbc_dec_loop8_enter
  2046. .align 16
  2047. .Lcbc_dec_loop8:
  2048. movaps $rndkey0,$reserved(%rsp) # save IV
  2049. movups $inout7,($out)
  2050. lea 0x10($out),$out
  2051. .Lcbc_dec_loop8_enter:
  2052. $movkey ($key),$rndkey0
  2053. movups ($inp),$inout0 # load input
  2054. movups 0x10($inp),$inout1
  2055. $movkey 16($key),$rndkey1
  2056. lea 32($key),$key
  2057. movdqu 0x20($inp),$inout2
  2058. xorps $rndkey0,$inout0
  2059. movdqu 0x30($inp),$inout3
  2060. xorps $rndkey0,$inout1
  2061. movdqu 0x40($inp),$inout4
  2062. aesdec $rndkey1,$inout0
  2063. pxor $rndkey0,$inout2
  2064. movdqu 0x50($inp),$inout5
  2065. aesdec $rndkey1,$inout1
  2066. pxor $rndkey0,$inout3
  2067. movdqu 0x60($inp),$inout6
  2068. aesdec $rndkey1,$inout2
  2069. pxor $rndkey0,$inout4
  2070. movdqu 0x70($inp),$inout7
  2071. aesdec $rndkey1,$inout3
  2072. pxor $rndkey0,$inout5
  2073. dec $rounds
  2074. aesdec $rndkey1,$inout4
  2075. pxor $rndkey0,$inout6
  2076. aesdec $rndkey1,$inout5
  2077. pxor $rndkey0,$inout7
  2078. $movkey ($key),$rndkey0
  2079. aesdec $rndkey1,$inout6
  2080. aesdec $rndkey1,$inout7
  2081. $movkey 16($key),$rndkey1
  2082. call .Ldec_loop8_enter
  2083. movups ($inp),$rndkey1 # re-load input
  2084. movups 0x10($inp),$rndkey0
  2085. xorps $reserved(%rsp),$inout0 # ^= IV
  2086. xorps $rndkey1,$inout1
  2087. movups 0x20($inp),$rndkey1
  2088. xorps $rndkey0,$inout2
  2089. movups 0x30($inp),$rndkey0
  2090. xorps $rndkey1,$inout3
  2091. movups 0x40($inp),$rndkey1
  2092. xorps $rndkey0,$inout4
  2093. movups 0x50($inp),$rndkey0
  2094. xorps $rndkey1,$inout5
  2095. movups 0x60($inp),$rndkey1
  2096. xorps $rndkey0,$inout6
  2097. movups 0x70($inp),$rndkey0 # IV
  2098. xorps $rndkey1,$inout7
  2099. movups $inout0,($out)
  2100. movups $inout1,0x10($out)
  2101. movups $inout2,0x20($out)
  2102. movups $inout3,0x30($out)
  2103. mov $rnds_,$rounds # restore $rounds
  2104. movups $inout4,0x40($out)
  2105. mov $key_,$key # restore $key
  2106. movups $inout5,0x50($out)
  2107. lea 0x80($inp),$inp
  2108. movups $inout6,0x60($out)
  2109. lea 0x70($out),$out
  2110. sub \$0x80,$len
  2111. ja .Lcbc_dec_loop8
  2112. movaps $inout7,$inout0
  2113. movaps $rndkey0,$iv
  2114. add \$0x70,$len
  2115. jle .Lcbc_dec_tail_collected
  2116. movups $inout0,($out)
  2117. lea 1($rnds_,$rnds_),$rounds
  2118. lea 0x10($out),$out
  2119. .Lcbc_dec_tail:
  2120. movups ($inp),$inout0
  2121. movaps $inout0,$in0
  2122. cmp \$0x10,$len
  2123. jbe .Lcbc_dec_one
  2124. movups 0x10($inp),$inout1
  2125. movaps $inout1,$in1
  2126. cmp \$0x20,$len
  2127. jbe .Lcbc_dec_two
  2128. movups 0x20($inp),$inout2
  2129. movaps $inout2,$in2
  2130. cmp \$0x30,$len
  2131. jbe .Lcbc_dec_three
  2132. movups 0x30($inp),$inout3
  2133. cmp \$0x40,$len
  2134. jbe .Lcbc_dec_four
  2135. movups 0x40($inp),$inout4
  2136. cmp \$0x50,$len
  2137. jbe .Lcbc_dec_five
  2138. movups 0x50($inp),$inout5
  2139. cmp \$0x60,$len
  2140. jbe .Lcbc_dec_six
  2141. movups 0x60($inp),$inout6
  2142. movaps $iv,$reserved(%rsp) # save IV
  2143. call _aesni_decrypt8
  2144. movups ($inp),$rndkey1
  2145. movups 0x10($inp),$rndkey0
  2146. xorps $reserved(%rsp),$inout0 # ^= IV
  2147. xorps $rndkey1,$inout1
  2148. movups 0x20($inp),$rndkey1
  2149. xorps $rndkey0,$inout2
  2150. movups 0x30($inp),$rndkey0
  2151. xorps $rndkey1,$inout3
  2152. movups 0x40($inp),$rndkey1
  2153. xorps $rndkey0,$inout4
  2154. movups 0x50($inp),$rndkey0
  2155. xorps $rndkey1,$inout5
  2156. movups 0x60($inp),$iv # IV
  2157. xorps $rndkey0,$inout6
  2158. movups $inout0,($out)
  2159. movups $inout1,0x10($out)
  2160. movups $inout2,0x20($out)
  2161. movups $inout3,0x30($out)
  2162. movups $inout4,0x40($out)
  2163. movups $inout5,0x50($out)
  2164. lea 0x60($out),$out
  2165. movaps $inout6,$inout0
  2166. sub \$0x70,$len
  2167. jmp .Lcbc_dec_tail_collected
  2168. .align 16
  2169. .Lcbc_dec_one:
  2170. ___
  2171. &aesni_generate1("dec",$key,$rounds);
  2172. $code.=<<___;
  2173. xorps $iv,$inout0
  2174. movaps $in0,$iv
  2175. sub \$0x10,$len
  2176. jmp .Lcbc_dec_tail_collected
  2177. .align 16
  2178. .Lcbc_dec_two:
  2179. xorps $inout2,$inout2
  2180. call _aesni_decrypt3
  2181. xorps $iv,$inout0
  2182. xorps $in0,$inout1
  2183. movups $inout0,($out)
  2184. movaps $in1,$iv
  2185. movaps $inout1,$inout0
  2186. lea 0x10($out),$out
  2187. sub \$0x20,$len
  2188. jmp .Lcbc_dec_tail_collected
  2189. .align 16
  2190. .Lcbc_dec_three:
  2191. call _aesni_decrypt3
  2192. xorps $iv,$inout0
  2193. xorps $in0,$inout1
  2194. movups $inout0,($out)
  2195. xorps $in1,$inout2
  2196. movups $inout1,0x10($out)
  2197. movaps $in2,$iv
  2198. movaps $inout2,$inout0
  2199. lea 0x20($out),$out
  2200. sub \$0x30,$len
  2201. jmp .Lcbc_dec_tail_collected
  2202. .align 16
  2203. .Lcbc_dec_four:
  2204. call _aesni_decrypt4
  2205. xorps $iv,$inout0
  2206. movups 0x30($inp),$iv
  2207. xorps $in0,$inout1
  2208. movups $inout0,($out)
  2209. xorps $in1,$inout2
  2210. movups $inout1,0x10($out)
  2211. xorps $in2,$inout3
  2212. movups $inout2,0x20($out)
  2213. movaps $inout3,$inout0
  2214. lea 0x30($out),$out
  2215. sub \$0x40,$len
  2216. jmp .Lcbc_dec_tail_collected
  2217. .align 16
  2218. .Lcbc_dec_five:
  2219. xorps $inout5,$inout5
  2220. call _aesni_decrypt6
  2221. movups 0x10($inp),$rndkey1
  2222. movups 0x20($inp),$rndkey0
  2223. xorps $iv,$inout0
  2224. xorps $in0,$inout1
  2225. xorps $rndkey1,$inout2
  2226. movups 0x30($inp),$rndkey1
  2227. xorps $rndkey0,$inout3
  2228. movups 0x40($inp),$iv
  2229. xorps $rndkey1,$inout4
  2230. movups $inout0,($out)
  2231. movups $inout1,0x10($out)
  2232. movups $inout2,0x20($out)
  2233. movups $inout3,0x30($out)
  2234. lea 0x40($out),$out
  2235. movaps $inout4,$inout0
  2236. sub \$0x50,$len
  2237. jmp .Lcbc_dec_tail_collected
  2238. .align 16
  2239. .Lcbc_dec_six:
  2240. call _aesni_decrypt6
  2241. movups 0x10($inp),$rndkey1
  2242. movups 0x20($inp),$rndkey0
  2243. xorps $iv,$inout0
  2244. xorps $in0,$inout1
  2245. xorps $rndkey1,$inout2
  2246. movups 0x30($inp),$rndkey1
  2247. xorps $rndkey0,$inout3
  2248. movups 0x40($inp),$rndkey0
  2249. xorps $rndkey1,$inout4
  2250. movups 0x50($inp),$iv
  2251. xorps $rndkey0,$inout5
  2252. movups $inout0,($out)
  2253. movups $inout1,0x10($out)
  2254. movups $inout2,0x20($out)
  2255. movups $inout3,0x30($out)
  2256. movups $inout4,0x40($out)
  2257. lea 0x50($out),$out
  2258. movaps $inout5,$inout0
  2259. sub \$0x60,$len
  2260. jmp .Lcbc_dec_tail_collected
  2261. .align 16
  2262. .Lcbc_dec_tail_collected:
  2263. and \$15,$len
  2264. movups $iv,($ivp)
  2265. jnz .Lcbc_dec_tail_partial
  2266. movups $inout0,($out)
  2267. jmp .Lcbc_dec_ret
  2268. .align 16
  2269. .Lcbc_dec_tail_partial:
  2270. movaps $inout0,$reserved(%rsp)
  2271. mov \$16,%rcx
  2272. mov $out,%rdi
  2273. sub $len,%rcx
  2274. lea $reserved(%rsp),%rsi
  2275. .long 0x9066A4F3 # rep movsb
  2276. .Lcbc_dec_ret:
  2277. ___
  2278. $code.=<<___ if ($win64);
  2279. movaps (%rsp),%xmm6
  2280. movaps 0x10(%rsp),%xmm7
  2281. movaps 0x20(%rsp),%xmm8
  2282. movaps 0x30(%rsp),%xmm9
  2283. lea 0x58(%rsp),%rsp
  2284. ___
  2285. $code.=<<___;
  2286. .Lcbc_ret:
  2287. ret
  2288. .size ${PREFIX}_cbc_encrypt,.-${PREFIX}_cbc_encrypt
  2289. ___
  2290. }
  2291. # int $PREFIX_set_[en|de]crypt_key (const unsigned char *userKey,
  2292. # int bits, AES_KEY *key)
  2293. { my ($inp,$bits,$key) = @_4args;
  2294. $bits =~ s/%r/%e/;
  2295. $code.=<<___;
  2296. .globl ${PREFIX}_set_decrypt_key
  2297. .type ${PREFIX}_set_decrypt_key,\@abi-omnipotent
  2298. .align 16
  2299. ${PREFIX}_set_decrypt_key:
  2300. .byte 0x48,0x83,0xEC,0x08 # sub rsp,8
  2301. call __aesni_set_encrypt_key
  2302. shl \$4,$bits # rounds-1 after _aesni_set_encrypt_key
  2303. test %eax,%eax
  2304. jnz .Ldec_key_ret
  2305. lea 16($key,$bits),$inp # points at the end of key schedule
  2306. $movkey ($key),%xmm0 # just swap
  2307. $movkey ($inp),%xmm1
  2308. $movkey %xmm0,($inp)
  2309. $movkey %xmm1,($key)
  2310. lea 16($key),$key
  2311. lea -16($inp),$inp
  2312. .Ldec_key_inverse:
  2313. $movkey ($key),%xmm0 # swap and inverse
  2314. $movkey ($inp),%xmm1
  2315. aesimc %xmm0,%xmm0
  2316. aesimc %xmm1,%xmm1
  2317. lea 16($key),$key
  2318. lea -16($inp),$inp
  2319. $movkey %xmm0,16($inp)
  2320. $movkey %xmm1,-16($key)
  2321. cmp $key,$inp
  2322. ja .Ldec_key_inverse
  2323. $movkey ($key),%xmm0 # inverse middle
  2324. aesimc %xmm0,%xmm0
  2325. $movkey %xmm0,($inp)
  2326. .Ldec_key_ret:
  2327. add \$8,%rsp
  2328. ret
  2329. .LSEH_end_set_decrypt_key:
  2330. .size ${PREFIX}_set_decrypt_key,.-${PREFIX}_set_decrypt_key
  2331. ___
  2332. # This is based on submission by
  2333. #
  2334. # Huang Ying <ying.huang@intel.com>
  2335. # Vinodh Gopal <vinodh.gopal@intel.com>
  2336. # Kahraman Akdemir
  2337. #
  2338. # Agressively optimized in respect to aeskeygenassist's critical path
  2339. # and is contained in %xmm0-5 to meet Win64 ABI requirement.
  2340. #
  2341. $code.=<<___;
  2342. .globl ${PREFIX}_set_encrypt_key
  2343. .type ${PREFIX}_set_encrypt_key,\@abi-omnipotent
  2344. .align 16
  2345. ${PREFIX}_set_encrypt_key:
  2346. __aesni_set_encrypt_key:
  2347. .byte 0x48,0x83,0xEC,0x08 # sub rsp,8
  2348. mov \$-1,%rax
  2349. test $inp,$inp
  2350. jz .Lenc_key_ret
  2351. test $key,$key
  2352. jz .Lenc_key_ret
  2353. movups ($inp),%xmm0 # pull first 128 bits of *userKey
  2354. xorps %xmm4,%xmm4 # low dword of xmm4 is assumed 0
  2355. lea 16($key),%rax
  2356. cmp \$256,$bits
  2357. je .L14rounds
  2358. cmp \$192,$bits
  2359. je .L12rounds
  2360. cmp \$128,$bits
  2361. jne .Lbad_keybits
  2362. .L10rounds:
  2363. mov \$9,$bits # 10 rounds for 128-bit key
  2364. $movkey %xmm0,($key) # round 0
  2365. aeskeygenassist \$0x1,%xmm0,%xmm1 # round 1
  2366. call .Lkey_expansion_128_cold
  2367. aeskeygenassist \$0x2,%xmm0,%xmm1 # round 2
  2368. call .Lkey_expansion_128
  2369. aeskeygenassist \$0x4,%xmm0,%xmm1 # round 3
  2370. call .Lkey_expansion_128
  2371. aeskeygenassist \$0x8,%xmm0,%xmm1 # round 4
  2372. call .Lkey_expansion_128
  2373. aeskeygenassist \$0x10,%xmm0,%xmm1 # round 5
  2374. call .Lkey_expansion_128
  2375. aeskeygenassist \$0x20,%xmm0,%xmm1 # round 6
  2376. call .Lkey_expansion_128
  2377. aeskeygenassist \$0x40,%xmm0,%xmm1 # round 7
  2378. call .Lkey_expansion_128
  2379. aeskeygenassist \$0x80,%xmm0,%xmm1 # round 8
  2380. call .Lkey_expansion_128
  2381. aeskeygenassist \$0x1b,%xmm0,%xmm1 # round 9
  2382. call .Lkey_expansion_128
  2383. aeskeygenassist \$0x36,%xmm0,%xmm1 # round 10
  2384. call .Lkey_expansion_128
  2385. $movkey %xmm0,(%rax)
  2386. mov $bits,80(%rax) # 240(%rdx)
  2387. xor %eax,%eax
  2388. jmp .Lenc_key_ret
  2389. .align 16
  2390. .L12rounds:
  2391. movq 16($inp),%xmm2 # remaining 1/3 of *userKey
  2392. mov \$11,$bits # 12 rounds for 192
  2393. $movkey %xmm0,($key) # round 0
  2394. aeskeygenassist \$0x1,%xmm2,%xmm1 # round 1,2
  2395. call .Lkey_expansion_192a_cold
  2396. aeskeygenassist \$0x2,%xmm2,%xmm1 # round 2,3
  2397. call .Lkey_expansion_192b
  2398. aeskeygenassist \$0x4,%xmm2,%xmm1 # round 4,5
  2399. call .Lkey_expansion_192a
  2400. aeskeygenassist \$0x8,%xmm2,%xmm1 # round 5,6
  2401. call .Lkey_expansion_192b
  2402. aeskeygenassist \$0x10,%xmm2,%xmm1 # round 7,8
  2403. call .Lkey_expansion_192a
  2404. aeskeygenassist \$0x20,%xmm2,%xmm1 # round 8,9
  2405. call .Lkey_expansion_192b
  2406. aeskeygenassist \$0x40,%xmm2,%xmm1 # round 10,11
  2407. call .Lkey_expansion_192a
  2408. aeskeygenassist \$0x80,%xmm2,%xmm1 # round 11,12
  2409. call .Lkey_expansion_192b
  2410. $movkey %xmm0,(%rax)
  2411. mov $bits,48(%rax) # 240(%rdx)
  2412. xor %rax, %rax
  2413. jmp .Lenc_key_ret
  2414. .align 16
  2415. .L14rounds:
  2416. movups 16($inp),%xmm2 # remaning half of *userKey
  2417. mov \$13,$bits # 14 rounds for 256
  2418. lea 16(%rax),%rax
  2419. $movkey %xmm0,($key) # round 0
  2420. $movkey %xmm2,16($key) # round 1
  2421. aeskeygenassist \$0x1,%xmm2,%xmm1 # round 2
  2422. call .Lkey_expansion_256a_cold
  2423. aeskeygenassist \$0x1,%xmm0,%xmm1 # round 3
  2424. call .Lkey_expansion_256b
  2425. aeskeygenassist \$0x2,%xmm2,%xmm1 # round 4
  2426. call .Lkey_expansion_256a
  2427. aeskeygenassist \$0x2,%xmm0,%xmm1 # round 5
  2428. call .Lkey_expansion_256b
  2429. aeskeygenassist \$0x4,%xmm2,%xmm1 # round 6
  2430. call .Lkey_expansion_256a
  2431. aeskeygenassist \$0x4,%xmm0,%xmm1 # round 7
  2432. call .Lkey_expansion_256b
  2433. aeskeygenassist \$0x8,%xmm2,%xmm1 # round 8
  2434. call .Lkey_expansion_256a
  2435. aeskeygenassist \$0x8,%xmm0,%xmm1 # round 9
  2436. call .Lkey_expansion_256b
  2437. aeskeygenassist \$0x10,%xmm2,%xmm1 # round 10
  2438. call .Lkey_expansion_256a
  2439. aeskeygenassist \$0x10,%xmm0,%xmm1 # round 11
  2440. call .Lkey_expansion_256b
  2441. aeskeygenassist \$0x20,%xmm2,%xmm1 # round 12
  2442. call .Lkey_expansion_256a
  2443. aeskeygenassist \$0x20,%xmm0,%xmm1 # round 13
  2444. call .Lkey_expansion_256b
  2445. aeskeygenassist \$0x40,%xmm2,%xmm1 # round 14
  2446. call .Lkey_expansion_256a
  2447. $movkey %xmm0,(%rax)
  2448. mov $bits,16(%rax) # 240(%rdx)
  2449. xor %rax,%rax
  2450. jmp .Lenc_key_ret
  2451. .align 16
  2452. .Lbad_keybits:
  2453. mov \$-2,%rax
  2454. .Lenc_key_ret:
  2455. add \$8,%rsp
  2456. ret
  2457. .LSEH_end_set_encrypt_key:
  2458. .align 16
  2459. .Lkey_expansion_128:
  2460. $movkey %xmm0,(%rax)
  2461. lea 16(%rax),%rax
  2462. .Lkey_expansion_128_cold:
  2463. shufps \$0b00010000,%xmm0,%xmm4
  2464. xorps %xmm4, %xmm0
  2465. shufps \$0b10001100,%xmm0,%xmm4
  2466. xorps %xmm4, %xmm0
  2467. shufps \$0b11111111,%xmm1,%xmm1 # critical path
  2468. xorps %xmm1,%xmm0
  2469. ret
  2470. .align 16
  2471. .Lkey_expansion_192a:
  2472. $movkey %xmm0,(%rax)
  2473. lea 16(%rax),%rax
  2474. .Lkey_expansion_192a_cold:
  2475. movaps %xmm2, %xmm5
  2476. .Lkey_expansion_192b_warm:
  2477. shufps \$0b00010000,%xmm0,%xmm4
  2478. movdqa %xmm2,%xmm3
  2479. xorps %xmm4,%xmm0
  2480. shufps \$0b10001100,%xmm0,%xmm4
  2481. pslldq \$4,%xmm3
  2482. xorps %xmm4,%xmm0
  2483. pshufd \$0b01010101,%xmm1,%xmm1 # critical path
  2484. pxor %xmm3,%xmm2
  2485. pxor %xmm1,%xmm0
  2486. pshufd \$0b11111111,%xmm0,%xmm3
  2487. pxor %xmm3,%xmm2
  2488. ret
  2489. .align 16
  2490. .Lkey_expansion_192b:
  2491. movaps %xmm0,%xmm3
  2492. shufps \$0b01000100,%xmm0,%xmm5
  2493. $movkey %xmm5,(%rax)
  2494. shufps \$0b01001110,%xmm2,%xmm3
  2495. $movkey %xmm3,16(%rax)
  2496. lea 32(%rax),%rax
  2497. jmp .Lkey_expansion_192b_warm
  2498. .align 16
  2499. .Lkey_expansion_256a:
  2500. $movkey %xmm2,(%rax)
  2501. lea 16(%rax),%rax
  2502. .Lkey_expansion_256a_cold:
  2503. shufps \$0b00010000,%xmm0,%xmm4
  2504. xorps %xmm4,%xmm0
  2505. shufps \$0b10001100,%xmm0,%xmm4
  2506. xorps %xmm4,%xmm0
  2507. shufps \$0b11111111,%xmm1,%xmm1 # critical path
  2508. xorps %xmm1,%xmm0
  2509. ret
  2510. .align 16
  2511. .Lkey_expansion_256b:
  2512. $movkey %xmm0,(%rax)
  2513. lea 16(%rax),%rax
  2514. shufps \$0b00010000,%xmm2,%xmm4
  2515. xorps %xmm4,%xmm2
  2516. shufps \$0b10001100,%xmm2,%xmm4
  2517. xorps %xmm4,%xmm2
  2518. shufps \$0b10101010,%xmm1,%xmm1 # critical path
  2519. xorps %xmm1,%xmm2
  2520. ret
  2521. .size ${PREFIX}_set_encrypt_key,.-${PREFIX}_set_encrypt_key
  2522. .size __aesni_set_encrypt_key,.-__aesni_set_encrypt_key
  2523. ___
  2524. }
  2525. $code.=<<___;
  2526. .align 64
  2527. .Lbswap_mask:
  2528. .byte 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
  2529. .Lincrement32:
  2530. .long 6,6,6,0
  2531. .Lincrement64:
  2532. .long 1,0,0,0
  2533. .Lxts_magic:
  2534. .long 0x87,0,1,0
  2535. .asciz "AES for Intel AES-NI, CRYPTOGAMS by <appro\@openssl.org>"
  2536. .align 64
  2537. ___
  2538. # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
  2539. # CONTEXT *context,DISPATCHER_CONTEXT *disp)
  2540. if ($win64) {
  2541. $rec="%rcx";
  2542. $frame="%rdx";
  2543. $context="%r8";
  2544. $disp="%r9";
  2545. $code.=<<___;
  2546. .extern __imp_RtlVirtualUnwind
  2547. ___
  2548. $code.=<<___ if ($PREFIX eq "aesni");
  2549. .type ecb_se_handler,\@abi-omnipotent
  2550. .align 16
  2551. ecb_se_handler:
  2552. push %rsi
  2553. push %rdi
  2554. push %rbx
  2555. push %rbp
  2556. push %r12
  2557. push %r13
  2558. push %r14
  2559. push %r15
  2560. pushfq
  2561. sub \$64,%rsp
  2562. mov 152($context),%rax # pull context->Rsp
  2563. jmp .Lcommon_seh_tail
  2564. .size ecb_se_handler,.-ecb_se_handler
  2565. .type ccm64_se_handler,\@abi-omnipotent
  2566. .align 16
  2567. ccm64_se_handler:
  2568. push %rsi
  2569. push %rdi
  2570. push %rbx
  2571. push %rbp
  2572. push %r12
  2573. push %r13
  2574. push %r14
  2575. push %r15
  2576. pushfq
  2577. sub \$64,%rsp
  2578. mov 120($context),%rax # pull context->Rax
  2579. mov 248($context),%rbx # pull context->Rip
  2580. mov 8($disp),%rsi # disp->ImageBase
  2581. mov 56($disp),%r11 # disp->HandlerData
  2582. mov 0(%r11),%r10d # HandlerData[0]
  2583. lea (%rsi,%r10),%r10 # prologue label
  2584. cmp %r10,%rbx # context->Rip<prologue label
  2585. jb .Lcommon_seh_tail
  2586. mov 152($context),%rax # pull context->Rsp
  2587. mov 4(%r11),%r10d # HandlerData[1]
  2588. lea (%rsi,%r10),%r10 # epilogue label
  2589. cmp %r10,%rbx # context->Rip>=epilogue label
  2590. jae .Lcommon_seh_tail
  2591. lea 0(%rax),%rsi # %xmm save area
  2592. lea 512($context),%rdi # &context.Xmm6
  2593. mov \$8,%ecx # 4*sizeof(%xmm0)/sizeof(%rax)
  2594. .long 0xa548f3fc # cld; rep movsq
  2595. lea 0x58(%rax),%rax # adjust stack pointer
  2596. jmp .Lcommon_seh_tail
  2597. .size ccm64_se_handler,.-ccm64_se_handler
  2598. .type ctr32_se_handler,\@abi-omnipotent
  2599. .align 16
  2600. ctr32_se_handler:
  2601. push %rsi
  2602. push %rdi
  2603. push %rbx
  2604. push %rbp
  2605. push %r12
  2606. push %r13
  2607. push %r14
  2608. push %r15
  2609. pushfq
  2610. sub \$64,%rsp
  2611. mov 120($context),%rax # pull context->Rax
  2612. mov 248($context),%rbx # pull context->Rip
  2613. lea .Lctr32_body(%rip),%r10
  2614. cmp %r10,%rbx # context->Rip<"prologue" label
  2615. jb .Lcommon_seh_tail
  2616. mov 152($context),%rax # pull context->Rsp
  2617. lea .Lctr32_ret(%rip),%r10
  2618. cmp %r10,%rbx
  2619. jae .Lcommon_seh_tail
  2620. lea 0x20(%rax),%rsi # %xmm save area
  2621. lea 512($context),%rdi # &context.Xmm6
  2622. mov \$20,%ecx # 10*sizeof(%xmm0)/sizeof(%rax)
  2623. .long 0xa548f3fc # cld; rep movsq
  2624. lea 0xc8(%rax),%rax # adjust stack pointer
  2625. jmp .Lcommon_seh_tail
  2626. .size ctr32_se_handler,.-ctr32_se_handler
  2627. .type xts_se_handler,\@abi-omnipotent
  2628. .align 16
  2629. xts_se_handler:
  2630. push %rsi
  2631. push %rdi
  2632. push %rbx
  2633. push %rbp
  2634. push %r12
  2635. push %r13
  2636. push %r14
  2637. push %r15
  2638. pushfq
  2639. sub \$64,%rsp
  2640. mov 120($context),%rax # pull context->Rax
  2641. mov 248($context),%rbx # pull context->Rip
  2642. mov 8($disp),%rsi # disp->ImageBase
  2643. mov 56($disp),%r11 # disp->HandlerData
  2644. mov 0(%r11),%r10d # HandlerData[0]
  2645. lea (%rsi,%r10),%r10 # prologue lable
  2646. cmp %r10,%rbx # context->Rip<prologue label
  2647. jb .Lcommon_seh_tail
  2648. mov 152($context),%rax # pull context->Rsp
  2649. mov 4(%r11),%r10d # HandlerData[1]
  2650. lea (%rsi,%r10),%r10 # epilogue label
  2651. cmp %r10,%rbx # context->Rip>=epilogue label
  2652. jae .Lcommon_seh_tail
  2653. lea 0x60(%rax),%rsi # %xmm save area
  2654. lea 512($context),%rdi # & context.Xmm6
  2655. mov \$20,%ecx # 10*sizeof(%xmm0)/sizeof(%rax)
  2656. .long 0xa548f3fc # cld; rep movsq
  2657. lea 0x68+160(%rax),%rax # adjust stack pointer
  2658. jmp .Lcommon_seh_tail
  2659. .size xts_se_handler,.-xts_se_handler
  2660. ___
  2661. $code.=<<___;
  2662. .type cbc_se_handler,\@abi-omnipotent
  2663. .align 16
  2664. cbc_se_handler:
  2665. push %rsi
  2666. push %rdi
  2667. push %rbx
  2668. push %rbp
  2669. push %r12
  2670. push %r13
  2671. push %r14
  2672. push %r15
  2673. pushfq
  2674. sub \$64,%rsp
  2675. mov 152($context),%rax # pull context->Rsp
  2676. mov 248($context),%rbx # pull context->Rip
  2677. lea .Lcbc_decrypt(%rip),%r10
  2678. cmp %r10,%rbx # context->Rip<"prologue" label
  2679. jb .Lcommon_seh_tail
  2680. lea .Lcbc_decrypt_body(%rip),%r10
  2681. cmp %r10,%rbx # context->Rip<cbc_decrypt_body
  2682. jb .Lrestore_cbc_rax
  2683. lea .Lcbc_ret(%rip),%r10
  2684. cmp %r10,%rbx # context->Rip>="epilogue" label
  2685. jae .Lcommon_seh_tail
  2686. lea 0(%rax),%rsi # top of stack
  2687. lea 512($context),%rdi # &context.Xmm6
  2688. mov \$8,%ecx # 4*sizeof(%xmm0)/sizeof(%rax)
  2689. .long 0xa548f3fc # cld; rep movsq
  2690. lea 0x58(%rax),%rax # adjust stack pointer
  2691. jmp .Lcommon_seh_tail
  2692. .Lrestore_cbc_rax:
  2693. mov 120($context),%rax
  2694. .Lcommon_seh_tail:
  2695. mov 8(%rax),%rdi
  2696. mov 16(%rax),%rsi
  2697. mov %rax,152($context) # restore context->Rsp
  2698. mov %rsi,168($context) # restore context->Rsi
  2699. mov %rdi,176($context) # restore context->Rdi
  2700. mov 40($disp),%rdi # disp->ContextRecord
  2701. mov $context,%rsi # context
  2702. mov \$154,%ecx # sizeof(CONTEXT)
  2703. .long 0xa548f3fc # cld; rep movsq
  2704. mov $disp,%rsi
  2705. xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
  2706. mov 8(%rsi),%rdx # arg2, disp->ImageBase
  2707. mov 0(%rsi),%r8 # arg3, disp->ControlPc
  2708. mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
  2709. mov 40(%rsi),%r10 # disp->ContextRecord
  2710. lea 56(%rsi),%r11 # &disp->HandlerData
  2711. lea 24(%rsi),%r12 # &disp->EstablisherFrame
  2712. mov %r10,32(%rsp) # arg5
  2713. mov %r11,40(%rsp) # arg6
  2714. mov %r12,48(%rsp) # arg7
  2715. mov %rcx,56(%rsp) # arg8, (NULL)
  2716. call *__imp_RtlVirtualUnwind(%rip)
  2717. mov \$1,%eax # ExceptionContinueSearch
  2718. add \$64,%rsp
  2719. popfq
  2720. pop %r15
  2721. pop %r14
  2722. pop %r13
  2723. pop %r12
  2724. pop %rbp
  2725. pop %rbx
  2726. pop %rdi
  2727. pop %rsi
  2728. ret
  2729. .size cbc_se_handler,.-cbc_se_handler
  2730. .section .pdata
  2731. .align 4
  2732. ___
  2733. $code.=<<___ if ($PREFIX eq "aesni");
  2734. .rva .LSEH_begin_aesni_ecb_encrypt
  2735. .rva .LSEH_end_aesni_ecb_encrypt
  2736. .rva .LSEH_info_ecb
  2737. .rva .LSEH_begin_aesni_ccm64_encrypt_blocks
  2738. .rva .LSEH_end_aesni_ccm64_encrypt_blocks
  2739. .rva .LSEH_info_ccm64_enc
  2740. .rva .LSEH_begin_aesni_ccm64_decrypt_blocks
  2741. .rva .LSEH_end_aesni_ccm64_decrypt_blocks
  2742. .rva .LSEH_info_ccm64_dec
  2743. .rva .LSEH_begin_aesni_ctr32_encrypt_blocks
  2744. .rva .LSEH_end_aesni_ctr32_encrypt_blocks
  2745. .rva .LSEH_info_ctr32
  2746. .rva .LSEH_begin_aesni_xts_encrypt
  2747. .rva .LSEH_end_aesni_xts_encrypt
  2748. .rva .LSEH_info_xts_enc
  2749. .rva .LSEH_begin_aesni_xts_decrypt
  2750. .rva .LSEH_end_aesni_xts_decrypt
  2751. .rva .LSEH_info_xts_dec
  2752. ___
  2753. $code.=<<___;
  2754. .rva .LSEH_begin_${PREFIX}_cbc_encrypt
  2755. .rva .LSEH_end_${PREFIX}_cbc_encrypt
  2756. .rva .LSEH_info_cbc
  2757. .rva ${PREFIX}_set_decrypt_key
  2758. .rva .LSEH_end_set_decrypt_key
  2759. .rva .LSEH_info_key
  2760. .rva ${PREFIX}_set_encrypt_key
  2761. .rva .LSEH_end_set_encrypt_key
  2762. .rva .LSEH_info_key
  2763. .section .xdata
  2764. .align 8
  2765. ___
  2766. $code.=<<___ if ($PREFIX eq "aesni");
  2767. .LSEH_info_ecb:
  2768. .byte 9,0,0,0
  2769. .rva ecb_se_handler
  2770. .LSEH_info_ccm64_enc:
  2771. .byte 9,0,0,0
  2772. .rva ccm64_se_handler
  2773. .rva .Lccm64_enc_body,.Lccm64_enc_ret # HandlerData[]
  2774. .LSEH_info_ccm64_dec:
  2775. .byte 9,0,0,0
  2776. .rva ccm64_se_handler
  2777. .rva .Lccm64_dec_body,.Lccm64_dec_ret # HandlerData[]
  2778. .LSEH_info_ctr32:
  2779. .byte 9,0,0,0
  2780. .rva ctr32_se_handler
  2781. .LSEH_info_xts_enc:
  2782. .byte 9,0,0,0
  2783. .rva xts_se_handler
  2784. .rva .Lxts_enc_body,.Lxts_enc_epilogue # HandlerData[]
  2785. .LSEH_info_xts_dec:
  2786. .byte 9,0,0,0
  2787. .rva xts_se_handler
  2788. .rva .Lxts_dec_body,.Lxts_dec_epilogue # HandlerData[]
  2789. ___
  2790. $code.=<<___;
  2791. .LSEH_info_cbc:
  2792. .byte 9,0,0,0
  2793. .rva cbc_se_handler
  2794. .LSEH_info_key:
  2795. .byte 0x01,0x04,0x01,0x00
  2796. .byte 0x04,0x02,0x00,0x00 # sub rsp,8
  2797. ___
  2798. }
  2799. sub rex {
  2800. local *opcode=shift;
  2801. my ($dst,$src)=@_;
  2802. my $rex=0;
  2803. $rex|=0x04 if($dst>=8);
  2804. $rex|=0x01 if($src>=8);
  2805. push @opcode,$rex|0x40 if($rex);
  2806. }
  2807. sub aesni {
  2808. my $line=shift;
  2809. my @opcode=(0x66);
  2810. if ($line=~/(aeskeygenassist)\s+\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
  2811. rex(\@opcode,$4,$3);
  2812. push @opcode,0x0f,0x3a,0xdf;
  2813. push @opcode,0xc0|($3&7)|(($4&7)<<3); # ModR/M
  2814. my $c=$2;
  2815. push @opcode,$c=~/^0/?oct($c):$c;
  2816. return ".byte\t".join(',',@opcode);
  2817. }
  2818. elsif ($line=~/(aes[a-z]+)\s+%xmm([0-9]+),\s*%xmm([0-9]+)/) {
  2819. my %opcodelet = (
  2820. "aesimc" => 0xdb,
  2821. "aesenc" => 0xdc, "aesenclast" => 0xdd,
  2822. "aesdec" => 0xde, "aesdeclast" => 0xdf
  2823. );
  2824. return undef if (!defined($opcodelet{$1}));
  2825. rex(\@opcode,$3,$2);
  2826. push @opcode,0x0f,0x38,$opcodelet{$1};
  2827. push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
  2828. return ".byte\t".join(',',@opcode);
  2829. }
  2830. return $line;
  2831. }
  2832. $code =~ s/\`([^\`]*)\`/eval($1)/gem;
  2833. $code =~ s/\b(aes.*%xmm[0-9]+).*$/aesni($1)/gem;
  2834. print $code;
  2835. close STDOUT;