aesni-x86_64.pl 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069
  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. jmp .Lccm64_enc_outer
  813. .align 16
  814. .Lccm64_enc_outer:
  815. $movkey ($key_),$rndkey0
  816. mov $rnds_,$rounds
  817. movups ($inp),$in0 # load inp
  818. xorps $rndkey0,$inout0 # counter
  819. $movkey 16($key_),$rndkey1
  820. xorps $in0,$rndkey0
  821. lea 32($key_),$key
  822. xorps $rndkey0,$inout1 # cmac^=inp
  823. $movkey ($key),$rndkey0
  824. .Lccm64_enc2_loop:
  825. aesenc $rndkey1,$inout0
  826. dec $rounds
  827. aesenc $rndkey1,$inout1
  828. $movkey 16($key),$rndkey1
  829. aesenc $rndkey0,$inout0
  830. lea 32($key),$key
  831. aesenc $rndkey0,$inout1
  832. $movkey 0($key),$rndkey0
  833. jnz .Lccm64_enc2_loop
  834. pshufb $bswap_mask,$iv
  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,$iv
  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. pshufb $bswap_mask,$iv
  893. lea 16($inp),$inp
  894. jmp .Lccm64_dec_outer
  895. .align 16
  896. .Lccm64_dec_outer:
  897. xorps $inout0,$in0 # inp ^= E(iv)
  898. movdqa $iv,$inout0
  899. mov $rnds_,$rounds
  900. movups $in0,($out) # save output
  901. lea 16($out),$out
  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. pshufb $bswap_mask,$iv
  927. lea 16($inp),$inp
  928. aesenclast $rndkey0,$inout0
  929. aesenclast $rndkey0,$inout1
  930. jmp .Lccm64_dec_outer
  931. .align 16
  932. .Lccm64_dec_break:
  933. #xorps $in0,$inout1 # cmac^=out
  934. ___
  935. &aesni_generate1("enc",$key_,$rounds,$inout1,$in0);
  936. $code.=<<___;
  937. movups $inout1,($cmac)
  938. ___
  939. $code.=<<___ if ($win64);
  940. movaps (%rsp),%xmm6
  941. movaps 0x10(%rsp),%xmm7
  942. movaps 0x20(%rsp),%xmm8
  943. movaps 0x30(%rsp),%xmm9
  944. lea 0x58(%rsp),%rsp
  945. .Lccm64_dec_ret:
  946. ___
  947. $code.=<<___;
  948. ret
  949. .size aesni_ccm64_decrypt_blocks,.-aesni_ccm64_decrypt_blocks
  950. ___
  951. }
  952. ######################################################################
  953. # void aesni_ctr32_encrypt_blocks (const void *in, void *out,
  954. # size_t blocks, const AES_KEY *key,
  955. # const char *ivec);
  956. #
  957. # Handles only complete blocks, operates on 32-bit counter and
  958. # does not update *ivec! (see engine/eng_aesni.c for details)
  959. #
  960. {
  961. my $reserved = $win64?0:-0x28;
  962. my ($in0,$in1,$in2,$in3)=map("%xmm$_",(8..11));
  963. my ($iv0,$iv1,$ivec)=("%xmm12","%xmm13","%xmm14");
  964. my $bswap_mask="%xmm15";
  965. $code.=<<___;
  966. .globl aesni_ctr32_encrypt_blocks
  967. .type aesni_ctr32_encrypt_blocks,\@function,5
  968. .align 16
  969. aesni_ctr32_encrypt_blocks:
  970. ___
  971. $code.=<<___ if ($win64);
  972. lea -0xc8(%rsp),%rsp
  973. movaps %xmm6,0x20(%rsp)
  974. movaps %xmm7,0x30(%rsp)
  975. movaps %xmm8,0x40(%rsp)
  976. movaps %xmm9,0x50(%rsp)
  977. movaps %xmm10,0x60(%rsp)
  978. movaps %xmm11,0x70(%rsp)
  979. movaps %xmm12,0x80(%rsp)
  980. movaps %xmm13,0x90(%rsp)
  981. movaps %xmm14,0xa0(%rsp)
  982. movaps %xmm15,0xb0(%rsp)
  983. .Lctr32_body:
  984. ___
  985. $code.=<<___;
  986. cmp \$1,$len
  987. je .Lctr32_one_shortcut
  988. movdqu ($ivp),$ivec
  989. movdqa .Lbswap_mask(%rip),$bswap_mask
  990. xor $rounds,$rounds
  991. pextrd \$3,$ivec,$rnds_ # pull 32-bit counter
  992. pinsrd \$3,$rounds,$ivec # wipe 32-bit counter
  993. mov 240($key),$rounds # key->rounds
  994. bswap $rnds_
  995. pxor $iv0,$iv0 # vector of 3 32-bit counters
  996. pxor $iv1,$iv1 # vector of 3 32-bit counters
  997. pinsrd \$0,$rnds_,$iv0
  998. lea 3($rnds_),$key_
  999. pinsrd \$0,$key_,$iv1
  1000. inc $rnds_
  1001. pinsrd \$1,$rnds_,$iv0
  1002. inc $key_
  1003. pinsrd \$1,$key_,$iv1
  1004. inc $rnds_
  1005. pinsrd \$2,$rnds_,$iv0
  1006. inc $key_
  1007. pinsrd \$2,$key_,$iv1
  1008. movdqa $iv0,$reserved(%rsp)
  1009. pshufb $bswap_mask,$iv0
  1010. movdqa $iv1,`$reserved+0x10`(%rsp)
  1011. pshufb $bswap_mask,$iv1
  1012. pshufd \$`3<<6`,$iv0,$inout0 # place counter to upper dword
  1013. pshufd \$`2<<6`,$iv0,$inout1
  1014. pshufd \$`1<<6`,$iv0,$inout2
  1015. cmp \$6,$len
  1016. jb .Lctr32_tail
  1017. shr \$1,$rounds
  1018. mov $key,$key_ # backup $key
  1019. mov $rounds,$rnds_ # backup $rounds
  1020. sub \$6,$len
  1021. jmp .Lctr32_loop6
  1022. .align 16
  1023. .Lctr32_loop6:
  1024. pshufd \$`3<<6`,$iv1,$inout3
  1025. por $ivec,$inout0 # merge counter-less ivec
  1026. $movkey ($key_),$rndkey0
  1027. pshufd \$`2<<6`,$iv1,$inout4
  1028. por $ivec,$inout1
  1029. $movkey 16($key_),$rndkey1
  1030. pshufd \$`1<<6`,$iv1,$inout5
  1031. por $ivec,$inout2
  1032. por $ivec,$inout3
  1033. xorps $rndkey0,$inout0
  1034. por $ivec,$inout4
  1035. por $ivec,$inout5
  1036. # inline _aesni_encrypt6 and interleave last rounds
  1037. # with own code...
  1038. pxor $rndkey0,$inout1
  1039. aesenc $rndkey1,$inout0
  1040. lea 32($key_),$key
  1041. pxor $rndkey0,$inout2
  1042. aesenc $rndkey1,$inout1
  1043. movdqa .Lincrement32(%rip),$iv1
  1044. pxor $rndkey0,$inout3
  1045. aesenc $rndkey1,$inout2
  1046. movdqa $reserved(%rsp),$iv0
  1047. pxor $rndkey0,$inout4
  1048. aesenc $rndkey1,$inout3
  1049. pxor $rndkey0,$inout5
  1050. $movkey ($key),$rndkey0
  1051. dec $rounds
  1052. aesenc $rndkey1,$inout4
  1053. aesenc $rndkey1,$inout5
  1054. jmp .Lctr32_enc_loop6_enter
  1055. .align 16
  1056. .Lctr32_enc_loop6:
  1057. aesenc $rndkey1,$inout0
  1058. aesenc $rndkey1,$inout1
  1059. dec $rounds
  1060. aesenc $rndkey1,$inout2
  1061. aesenc $rndkey1,$inout3
  1062. aesenc $rndkey1,$inout4
  1063. aesenc $rndkey1,$inout5
  1064. .Lctr32_enc_loop6_enter:
  1065. $movkey 16($key),$rndkey1
  1066. aesenc $rndkey0,$inout0
  1067. aesenc $rndkey0,$inout1
  1068. lea 32($key),$key
  1069. aesenc $rndkey0,$inout2
  1070. aesenc $rndkey0,$inout3
  1071. aesenc $rndkey0,$inout4
  1072. aesenc $rndkey0,$inout5
  1073. $movkey ($key),$rndkey0
  1074. jnz .Lctr32_enc_loop6
  1075. aesenc $rndkey1,$inout0
  1076. paddd $iv1,$iv0 # increment counter vector
  1077. aesenc $rndkey1,$inout1
  1078. paddd `$reserved+0x10`(%rsp),$iv1
  1079. aesenc $rndkey1,$inout2
  1080. movdqa $iv0,$reserved(%rsp) # save counter vector
  1081. aesenc $rndkey1,$inout3
  1082. movdqa $iv1,`$reserved+0x10`(%rsp)
  1083. aesenc $rndkey1,$inout4
  1084. pshufb $bswap_mask,$iv0 # byte swap
  1085. aesenc $rndkey1,$inout5
  1086. pshufb $bswap_mask,$iv1
  1087. aesenclast $rndkey0,$inout0
  1088. movups ($inp),$in0 # load input
  1089. aesenclast $rndkey0,$inout1
  1090. movups 0x10($inp),$in1
  1091. aesenclast $rndkey0,$inout2
  1092. movups 0x20($inp),$in2
  1093. aesenclast $rndkey0,$inout3
  1094. movups 0x30($inp),$in3
  1095. aesenclast $rndkey0,$inout4
  1096. movups 0x40($inp),$rndkey1
  1097. aesenclast $rndkey0,$inout5
  1098. movups 0x50($inp),$rndkey0
  1099. lea 0x60($inp),$inp
  1100. xorps $inout0,$in0 # xor
  1101. pshufd \$`3<<6`,$iv0,$inout0
  1102. xorps $inout1,$in1
  1103. pshufd \$`2<<6`,$iv0,$inout1
  1104. movups $in0,($out) # store output
  1105. xorps $inout2,$in2
  1106. pshufd \$`1<<6`,$iv0,$inout2
  1107. movups $in1,0x10($out)
  1108. xorps $inout3,$in3
  1109. movups $in2,0x20($out)
  1110. xorps $inout4,$rndkey1
  1111. movups $in3,0x30($out)
  1112. xorps $inout5,$rndkey0
  1113. movups $rndkey1,0x40($out)
  1114. movups $rndkey0,0x50($out)
  1115. lea 0x60($out),$out
  1116. mov $rnds_,$rounds
  1117. sub \$6,$len
  1118. jnc .Lctr32_loop6
  1119. add \$6,$len
  1120. jz .Lctr32_done
  1121. mov $key_,$key # restore $key
  1122. lea 1($rounds,$rounds),$rounds # restore original value
  1123. .Lctr32_tail:
  1124. por $ivec,$inout0
  1125. movups ($inp),$in0
  1126. cmp \$2,$len
  1127. jb .Lctr32_one
  1128. por $ivec,$inout1
  1129. movups 0x10($inp),$in1
  1130. je .Lctr32_two
  1131. pshufd \$`3<<6`,$iv1,$inout3
  1132. por $ivec,$inout2
  1133. movups 0x20($inp),$in2
  1134. cmp \$4,$len
  1135. jb .Lctr32_three
  1136. pshufd \$`2<<6`,$iv1,$inout4
  1137. por $ivec,$inout3
  1138. movups 0x30($inp),$in3
  1139. je .Lctr32_four
  1140. por $ivec,$inout4
  1141. xorps $inout5,$inout5
  1142. call _aesni_encrypt6
  1143. movups 0x40($inp),$rndkey1
  1144. xorps $inout0,$in0
  1145. xorps $inout1,$in1
  1146. movups $in0,($out)
  1147. xorps $inout2,$in2
  1148. movups $in1,0x10($out)
  1149. xorps $inout3,$in3
  1150. movups $in2,0x20($out)
  1151. xorps $inout4,$rndkey1
  1152. movups $in3,0x30($out)
  1153. movups $rndkey1,0x40($out)
  1154. jmp .Lctr32_done
  1155. .align 16
  1156. .Lctr32_one_shortcut:
  1157. movups ($ivp),$inout0
  1158. movups ($inp),$in0
  1159. mov 240($key),$rounds # key->rounds
  1160. .Lctr32_one:
  1161. ___
  1162. &aesni_generate1("enc",$key,$rounds);
  1163. $code.=<<___;
  1164. xorps $inout0,$in0
  1165. movups $in0,($out)
  1166. jmp .Lctr32_done
  1167. .align 16
  1168. .Lctr32_two:
  1169. xorps $inout2,$inout2
  1170. call _aesni_encrypt3
  1171. xorps $inout0,$in0
  1172. xorps $inout1,$in1
  1173. movups $in0,($out)
  1174. movups $in1,0x10($out)
  1175. jmp .Lctr32_done
  1176. .align 16
  1177. .Lctr32_three:
  1178. call _aesni_encrypt3
  1179. xorps $inout0,$in0
  1180. xorps $inout1,$in1
  1181. movups $in0,($out)
  1182. xorps $inout2,$in2
  1183. movups $in1,0x10($out)
  1184. movups $in2,0x20($out)
  1185. jmp .Lctr32_done
  1186. .align 16
  1187. .Lctr32_four:
  1188. call _aesni_encrypt4
  1189. xorps $inout0,$in0
  1190. xorps $inout1,$in1
  1191. movups $in0,($out)
  1192. xorps $inout2,$in2
  1193. movups $in1,0x10($out)
  1194. xorps $inout3,$in3
  1195. movups $in2,0x20($out)
  1196. movups $in3,0x30($out)
  1197. .Lctr32_done:
  1198. ___
  1199. $code.=<<___ if ($win64);
  1200. movaps 0x20(%rsp),%xmm6
  1201. movaps 0x30(%rsp),%xmm7
  1202. movaps 0x40(%rsp),%xmm8
  1203. movaps 0x50(%rsp),%xmm9
  1204. movaps 0x60(%rsp),%xmm10
  1205. movaps 0x70(%rsp),%xmm11
  1206. movaps 0x80(%rsp),%xmm12
  1207. movaps 0x90(%rsp),%xmm13
  1208. movaps 0xa0(%rsp),%xmm14
  1209. movaps 0xb0(%rsp),%xmm15
  1210. lea 0xc8(%rsp),%rsp
  1211. .Lctr32_ret:
  1212. ___
  1213. $code.=<<___;
  1214. ret
  1215. .size aesni_ctr32_encrypt_blocks,.-aesni_ctr32_encrypt_blocks
  1216. ___
  1217. }
  1218. ######################################################################
  1219. # void aesni_xts_[en|de]crypt(const char *inp,char *out,size_t len,
  1220. # const AES_KEY *key1, const AES_KEY *key2
  1221. # const unsigned char iv[16]);
  1222. #
  1223. {
  1224. my @tweak=map("%xmm$_",(10..15));
  1225. my ($twmask,$twres,$twtmp)=("%xmm8","%xmm9",@tweak[4]);
  1226. my ($key2,$ivp,$len_)=("%r8","%r9","%r9");
  1227. my $frame_size = 0x68 + ($win64?160:0);
  1228. $code.=<<___;
  1229. .globl aesni_xts_encrypt
  1230. .type aesni_xts_encrypt,\@function,6
  1231. .align 16
  1232. aesni_xts_encrypt:
  1233. lea -$frame_size(%rsp),%rsp
  1234. ___
  1235. $code.=<<___ if ($win64);
  1236. movaps %xmm6,0x60(%rsp)
  1237. movaps %xmm7,0x70(%rsp)
  1238. movaps %xmm8,0x80(%rsp)
  1239. movaps %xmm9,0x90(%rsp)
  1240. movaps %xmm10,0xa0(%rsp)
  1241. movaps %xmm11,0xb0(%rsp)
  1242. movaps %xmm12,0xc0(%rsp)
  1243. movaps %xmm13,0xd0(%rsp)
  1244. movaps %xmm14,0xe0(%rsp)
  1245. movaps %xmm15,0xf0(%rsp)
  1246. .Lxts_enc_body:
  1247. ___
  1248. $code.=<<___;
  1249. movups ($ivp),@tweak[5] # load clear-text tweak
  1250. mov 240(%r8),$rounds # key2->rounds
  1251. mov 240($key),$rnds_ # key1->rounds
  1252. ___
  1253. # generate the tweak
  1254. &aesni_generate1("enc",$key2,$rounds,@tweak[5]);
  1255. $code.=<<___;
  1256. mov $key,$key_ # backup $key
  1257. mov $rnds_,$rounds # backup $rounds
  1258. mov $len,$len_ # backup $len
  1259. and \$-16,$len
  1260. movdqa .Lxts_magic(%rip),$twmask
  1261. pxor $twtmp,$twtmp
  1262. pcmpgtd @tweak[5],$twtmp # broadcast upper bits
  1263. ___
  1264. for ($i=0;$i<4;$i++) {
  1265. $code.=<<___;
  1266. pshufd \$0x13,$twtmp,$twres
  1267. pxor $twtmp,$twtmp
  1268. movdqa @tweak[5],@tweak[$i]
  1269. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1270. pand $twmask,$twres # isolate carry and residue
  1271. pcmpgtd @tweak[5],$twtmp # broadcat upper bits
  1272. pxor $twres,@tweak[5]
  1273. ___
  1274. }
  1275. $code.=<<___;
  1276. sub \$16*6,$len
  1277. jc .Lxts_enc_short
  1278. shr \$1,$rounds
  1279. sub \$1,$rounds
  1280. mov $rounds,$rnds_
  1281. jmp .Lxts_enc_grandloop
  1282. .align 16
  1283. .Lxts_enc_grandloop:
  1284. pshufd \$0x13,$twtmp,$twres
  1285. movdqa @tweak[5],@tweak[4]
  1286. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1287. movdqu `16*0`($inp),$inout0 # load input
  1288. pand $twmask,$twres # isolate carry and residue
  1289. movdqu `16*1`($inp),$inout1
  1290. pxor $twres,@tweak[5]
  1291. movdqu `16*2`($inp),$inout2
  1292. pxor @tweak[0],$inout0 # input^=tweak
  1293. movdqu `16*3`($inp),$inout3
  1294. pxor @tweak[1],$inout1
  1295. movdqu `16*4`($inp),$inout4
  1296. pxor @tweak[2],$inout2
  1297. movdqu `16*5`($inp),$inout5
  1298. lea `16*6`($inp),$inp
  1299. pxor @tweak[3],$inout3
  1300. $movkey ($key_),$rndkey0
  1301. pxor @tweak[4],$inout4
  1302. pxor @tweak[5],$inout5
  1303. # inline _aesni_encrypt6 and interleave first and last rounds
  1304. # with own code...
  1305. $movkey 16($key_),$rndkey1
  1306. pxor $rndkey0,$inout0
  1307. pxor $rndkey0,$inout1
  1308. movdqa @tweak[0],`16*0`(%rsp) # put aside tweaks
  1309. aesenc $rndkey1,$inout0
  1310. lea 32($key_),$key
  1311. pxor $rndkey0,$inout2
  1312. movdqa @tweak[1],`16*1`(%rsp)
  1313. aesenc $rndkey1,$inout1
  1314. pxor $rndkey0,$inout3
  1315. movdqa @tweak[2],`16*2`(%rsp)
  1316. aesenc $rndkey1,$inout2
  1317. pxor $rndkey0,$inout4
  1318. movdqa @tweak[3],`16*3`(%rsp)
  1319. aesenc $rndkey1,$inout3
  1320. pxor $rndkey0,$inout5
  1321. $movkey ($key),$rndkey0
  1322. dec $rounds
  1323. movdqa @tweak[4],`16*4`(%rsp)
  1324. aesenc $rndkey1,$inout4
  1325. movdqa @tweak[5],`16*5`(%rsp)
  1326. aesenc $rndkey1,$inout5
  1327. pxor $twtmp,$twtmp
  1328. pcmpgtd @tweak[5],$twtmp
  1329. jmp .Lxts_enc_loop6_enter
  1330. .align 16
  1331. .Lxts_enc_loop6:
  1332. aesenc $rndkey1,$inout0
  1333. aesenc $rndkey1,$inout1
  1334. dec $rounds
  1335. aesenc $rndkey1,$inout2
  1336. aesenc $rndkey1,$inout3
  1337. aesenc $rndkey1,$inout4
  1338. aesenc $rndkey1,$inout5
  1339. .Lxts_enc_loop6_enter:
  1340. $movkey 16($key),$rndkey1
  1341. aesenc $rndkey0,$inout0
  1342. aesenc $rndkey0,$inout1
  1343. lea 32($key),$key
  1344. aesenc $rndkey0,$inout2
  1345. aesenc $rndkey0,$inout3
  1346. aesenc $rndkey0,$inout4
  1347. aesenc $rndkey0,$inout5
  1348. $movkey ($key),$rndkey0
  1349. jnz .Lxts_enc_loop6
  1350. pshufd \$0x13,$twtmp,$twres
  1351. pxor $twtmp,$twtmp
  1352. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1353. aesenc $rndkey1,$inout0
  1354. pand $twmask,$twres # isolate carry and residue
  1355. aesenc $rndkey1,$inout1
  1356. pcmpgtd @tweak[5],$twtmp # broadcast upper bits
  1357. aesenc $rndkey1,$inout2
  1358. pxor $twres,@tweak[5]
  1359. aesenc $rndkey1,$inout3
  1360. aesenc $rndkey1,$inout4
  1361. aesenc $rndkey1,$inout5
  1362. $movkey 16($key),$rndkey1
  1363. pshufd \$0x13,$twtmp,$twres
  1364. pxor $twtmp,$twtmp
  1365. movdqa @tweak[5],@tweak[0]
  1366. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1367. aesenc $rndkey0,$inout0
  1368. pand $twmask,$twres # isolate carry and residue
  1369. aesenc $rndkey0,$inout1
  1370. pcmpgtd @tweak[5],$twtmp # broadcat upper bits
  1371. aesenc $rndkey0,$inout2
  1372. pxor $twres,@tweak[5]
  1373. aesenc $rndkey0,$inout3
  1374. aesenc $rndkey0,$inout4
  1375. aesenc $rndkey0,$inout5
  1376. $movkey 32($key),$rndkey0
  1377. pshufd \$0x13,$twtmp,$twres
  1378. pxor $twtmp,$twtmp
  1379. movdqa @tweak[5],@tweak[1]
  1380. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1381. aesenc $rndkey1,$inout0
  1382. pand $twmask,$twres # isolate carry and residue
  1383. aesenc $rndkey1,$inout1
  1384. pcmpgtd @tweak[5],$twtmp # broadcat upper bits
  1385. aesenc $rndkey1,$inout2
  1386. pxor $twres,@tweak[5]
  1387. aesenc $rndkey1,$inout3
  1388. aesenc $rndkey1,$inout4
  1389. aesenc $rndkey1,$inout5
  1390. pshufd \$0x13,$twtmp,$twres
  1391. pxor $twtmp,$twtmp
  1392. movdqa @tweak[5],@tweak[2]
  1393. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1394. aesenclast $rndkey0,$inout0
  1395. pand $twmask,$twres # isolate carry and residue
  1396. aesenclast $rndkey0,$inout1
  1397. pcmpgtd @tweak[5],$twtmp # broadcat upper bits
  1398. aesenclast $rndkey0,$inout2
  1399. pxor $twres,@tweak[5]
  1400. aesenclast $rndkey0,$inout3
  1401. aesenclast $rndkey0,$inout4
  1402. aesenclast $rndkey0,$inout5
  1403. pshufd \$0x13,$twtmp,$twres
  1404. pxor $twtmp,$twtmp
  1405. movdqa @tweak[5],@tweak[3]
  1406. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1407. xorps `16*0`(%rsp),$inout0 # output^=tweak
  1408. pand $twmask,$twres # isolate carry and residue
  1409. xorps `16*1`(%rsp),$inout1
  1410. pcmpgtd @tweak[5],$twtmp # broadcat upper bits
  1411. pxor $twres,@tweak[5]
  1412. xorps `16*2`(%rsp),$inout2
  1413. movups $inout0,`16*0`($out) # write output
  1414. xorps `16*3`(%rsp),$inout3
  1415. movups $inout1,`16*1`($out)
  1416. xorps `16*4`(%rsp),$inout4
  1417. movups $inout2,`16*2`($out)
  1418. xorps `16*5`(%rsp),$inout5
  1419. movups $inout3,`16*3`($out)
  1420. mov $rnds_,$rounds # restore $rounds
  1421. movups $inout4,`16*4`($out)
  1422. movups $inout5,`16*5`($out)
  1423. lea `16*6`($out),$out
  1424. sub \$16*6,$len
  1425. jnc .Lxts_enc_grandloop
  1426. lea 3($rounds,$rounds),$rounds # restore original value
  1427. mov $key_,$key # restore $key
  1428. mov $rounds,$rnds_ # backup $rounds
  1429. .Lxts_enc_short:
  1430. add \$16*6,$len
  1431. jz .Lxts_enc_done
  1432. cmp \$0x20,$len
  1433. jb .Lxts_enc_one
  1434. je .Lxts_enc_two
  1435. cmp \$0x40,$len
  1436. jb .Lxts_enc_three
  1437. je .Lxts_enc_four
  1438. pshufd \$0x13,$twtmp,$twres
  1439. movdqa @tweak[5],@tweak[4]
  1440. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1441. movdqu ($inp),$inout0
  1442. pand $twmask,$twres # isolate carry and residue
  1443. movdqu 16*1($inp),$inout1
  1444. pxor $twres,@tweak[5]
  1445. movdqu 16*2($inp),$inout2
  1446. pxor @tweak[0],$inout0
  1447. movdqu 16*3($inp),$inout3
  1448. pxor @tweak[1],$inout1
  1449. movdqu 16*4($inp),$inout4
  1450. lea 16*5($inp),$inp
  1451. pxor @tweak[2],$inout2
  1452. pxor @tweak[3],$inout3
  1453. pxor @tweak[4],$inout4
  1454. call _aesni_encrypt6
  1455. xorps @tweak[0],$inout0
  1456. movdqa @tweak[5],@tweak[0]
  1457. xorps @tweak[1],$inout1
  1458. xorps @tweak[2],$inout2
  1459. movdqu $inout0,($out)
  1460. xorps @tweak[3],$inout3
  1461. movdqu $inout1,16*1($out)
  1462. xorps @tweak[4],$inout4
  1463. movdqu $inout2,16*2($out)
  1464. movdqu $inout3,16*3($out)
  1465. movdqu $inout4,16*4($out)
  1466. lea 16*5($out),$out
  1467. jmp .Lxts_enc_done
  1468. .align 16
  1469. .Lxts_enc_one:
  1470. movups ($inp),$inout0
  1471. lea 16*1($inp),$inp
  1472. xorps @tweak[0],$inout0
  1473. ___
  1474. &aesni_generate1("enc",$key,$rounds);
  1475. $code.=<<___;
  1476. xorps @tweak[0],$inout0
  1477. movdqa @tweak[1],@tweak[0]
  1478. movups $inout0,($out)
  1479. lea 16*1($out),$out
  1480. jmp .Lxts_enc_done
  1481. .align 16
  1482. .Lxts_enc_two:
  1483. movups ($inp),$inout0
  1484. movups 16($inp),$inout1
  1485. lea 32($inp),$inp
  1486. xorps @tweak[0],$inout0
  1487. xorps @tweak[1],$inout1
  1488. call _aesni_encrypt3
  1489. xorps @tweak[0],$inout0
  1490. movdqa @tweak[2],@tweak[0]
  1491. xorps @tweak[1],$inout1
  1492. movups $inout0,($out)
  1493. movups $inout1,16*1($out)
  1494. lea 16*2($out),$out
  1495. jmp .Lxts_enc_done
  1496. .align 16
  1497. .Lxts_enc_three:
  1498. movups ($inp),$inout0
  1499. movups 16*1($inp),$inout1
  1500. movups 16*2($inp),$inout2
  1501. lea 16*3($inp),$inp
  1502. xorps @tweak[0],$inout0
  1503. xorps @tweak[1],$inout1
  1504. xorps @tweak[2],$inout2
  1505. call _aesni_encrypt3
  1506. xorps @tweak[0],$inout0
  1507. movdqa @tweak[3],@tweak[0]
  1508. xorps @tweak[1],$inout1
  1509. xorps @tweak[2],$inout2
  1510. movups $inout0,($out)
  1511. movups $inout1,16*1($out)
  1512. movups $inout2,16*2($out)
  1513. lea 16*3($out),$out
  1514. jmp .Lxts_enc_done
  1515. .align 16
  1516. .Lxts_enc_four:
  1517. movups ($inp),$inout0
  1518. movups 16*1($inp),$inout1
  1519. movups 16*2($inp),$inout2
  1520. xorps @tweak[0],$inout0
  1521. movups 16*3($inp),$inout3
  1522. lea 16*4($inp),$inp
  1523. xorps @tweak[1],$inout1
  1524. xorps @tweak[2],$inout2
  1525. xorps @tweak[3],$inout3
  1526. call _aesni_encrypt4
  1527. xorps @tweak[0],$inout0
  1528. movdqa @tweak[5],@tweak[0]
  1529. xorps @tweak[1],$inout1
  1530. xorps @tweak[2],$inout2
  1531. movups $inout0,($out)
  1532. xorps @tweak[3],$inout3
  1533. movups $inout1,16*1($out)
  1534. movups $inout2,16*2($out)
  1535. movups $inout3,16*3($out)
  1536. lea 16*4($out),$out
  1537. jmp .Lxts_enc_done
  1538. .align 16
  1539. .Lxts_enc_done:
  1540. and \$15,$len_
  1541. jz .Lxts_enc_ret
  1542. mov $len_,$len
  1543. .Lxts_enc_steal:
  1544. movzb ($inp),%eax # borrow $rounds ...
  1545. movzb -16($out),%ecx # ... and $key
  1546. lea 1($inp),$inp
  1547. mov %al,-16($out)
  1548. mov %cl,0($out)
  1549. lea 1($out),$out
  1550. sub \$1,$len
  1551. jnz .Lxts_enc_steal
  1552. sub $len_,$out # rewind $out
  1553. mov $key_,$key # restore $key
  1554. mov $rnds_,$rounds # restore $rounds
  1555. movups -16($out),$inout0
  1556. xorps @tweak[0],$inout0
  1557. ___
  1558. &aesni_generate1("enc",$key,$rounds);
  1559. $code.=<<___;
  1560. xorps @tweak[0],$inout0
  1561. movups $inout0,-16($out)
  1562. .Lxts_enc_ret:
  1563. ___
  1564. $code.=<<___ if ($win64);
  1565. movaps 0x60(%rsp),%xmm6
  1566. movaps 0x70(%rsp),%xmm7
  1567. movaps 0x80(%rsp),%xmm8
  1568. movaps 0x90(%rsp),%xmm9
  1569. movaps 0xa0(%rsp),%xmm10
  1570. movaps 0xb0(%rsp),%xmm11
  1571. movaps 0xc0(%rsp),%xmm12
  1572. movaps 0xd0(%rsp),%xmm13
  1573. movaps 0xe0(%rsp),%xmm14
  1574. movaps 0xf0(%rsp),%xmm15
  1575. ___
  1576. $code.=<<___;
  1577. lea $frame_size(%rsp),%rsp
  1578. .Lxts_enc_epilogue:
  1579. ret
  1580. .size aesni_xts_encrypt,.-aesni_xts_encrypt
  1581. ___
  1582. $code.=<<___;
  1583. .globl aesni_xts_decrypt
  1584. .type aesni_xts_decrypt,\@function,6
  1585. .align 16
  1586. aesni_xts_decrypt:
  1587. lea -$frame_size(%rsp),%rsp
  1588. ___
  1589. $code.=<<___ if ($win64);
  1590. movaps %xmm6,0x60(%rsp)
  1591. movaps %xmm7,0x70(%rsp)
  1592. movaps %xmm8,0x80(%rsp)
  1593. movaps %xmm9,0x90(%rsp)
  1594. movaps %xmm10,0xa0(%rsp)
  1595. movaps %xmm11,0xb0(%rsp)
  1596. movaps %xmm12,0xc0(%rsp)
  1597. movaps %xmm13,0xd0(%rsp)
  1598. movaps %xmm14,0xe0(%rsp)
  1599. movaps %xmm15,0xf0(%rsp)
  1600. .Lxts_dec_body:
  1601. ___
  1602. $code.=<<___;
  1603. movups ($ivp),@tweak[5] # load clear-text tweak
  1604. mov 240($key2),$rounds # key2->rounds
  1605. mov 240($key),$rnds_ # key1->rounds
  1606. ___
  1607. # generate the tweak
  1608. &aesni_generate1("enc",$key2,$rounds,@tweak[5]);
  1609. $code.=<<___;
  1610. xor %eax,%eax # if ($len%16) len-=16;
  1611. test \$15,$len
  1612. setnz %al
  1613. shl \$4,%rax
  1614. sub %rax,$len
  1615. mov $key,$key_ # backup $key
  1616. mov $rnds_,$rounds # backup $rounds
  1617. mov $len,$len_ # backup $len
  1618. and \$-16,$len
  1619. movdqa .Lxts_magic(%rip),$twmask
  1620. pxor $twtmp,$twtmp
  1621. pcmpgtd @tweak[5],$twtmp # broadcast upper bits
  1622. ___
  1623. for ($i=0;$i<4;$i++) {
  1624. $code.=<<___;
  1625. pshufd \$0x13,$twtmp,$twres
  1626. pxor $twtmp,$twtmp
  1627. movdqa @tweak[5],@tweak[$i]
  1628. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1629. pand $twmask,$twres # isolate carry and residue
  1630. pcmpgtd @tweak[5],$twtmp # broadcat upper bits
  1631. pxor $twres,@tweak[5]
  1632. ___
  1633. }
  1634. $code.=<<___;
  1635. sub \$16*6,$len
  1636. jc .Lxts_dec_short
  1637. shr \$1,$rounds
  1638. sub \$1,$rounds
  1639. mov $rounds,$rnds_
  1640. jmp .Lxts_dec_grandloop
  1641. .align 16
  1642. .Lxts_dec_grandloop:
  1643. pshufd \$0x13,$twtmp,$twres
  1644. movdqa @tweak[5],@tweak[4]
  1645. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1646. movdqu `16*0`($inp),$inout0 # load input
  1647. pand $twmask,$twres # isolate carry and residue
  1648. movdqu `16*1`($inp),$inout1
  1649. pxor $twres,@tweak[5]
  1650. movdqu `16*2`($inp),$inout2
  1651. pxor @tweak[0],$inout0 # input^=tweak
  1652. movdqu `16*3`($inp),$inout3
  1653. pxor @tweak[1],$inout1
  1654. movdqu `16*4`($inp),$inout4
  1655. pxor @tweak[2],$inout2
  1656. movdqu `16*5`($inp),$inout5
  1657. lea `16*6`($inp),$inp
  1658. pxor @tweak[3],$inout3
  1659. $movkey ($key_),$rndkey0
  1660. pxor @tweak[4],$inout4
  1661. pxor @tweak[5],$inout5
  1662. # inline _aesni_decrypt6 and interleave first and last rounds
  1663. # with own code...
  1664. $movkey 16($key_),$rndkey1
  1665. pxor $rndkey0,$inout0
  1666. pxor $rndkey0,$inout1
  1667. movdqa @tweak[0],`16*0`(%rsp) # put aside tweaks
  1668. aesdec $rndkey1,$inout0
  1669. lea 32($key_),$key
  1670. pxor $rndkey0,$inout2
  1671. movdqa @tweak[1],`16*1`(%rsp)
  1672. aesdec $rndkey1,$inout1
  1673. pxor $rndkey0,$inout3
  1674. movdqa @tweak[2],`16*2`(%rsp)
  1675. aesdec $rndkey1,$inout2
  1676. pxor $rndkey0,$inout4
  1677. movdqa @tweak[3],`16*3`(%rsp)
  1678. aesdec $rndkey1,$inout3
  1679. pxor $rndkey0,$inout5
  1680. $movkey ($key),$rndkey0
  1681. dec $rounds
  1682. movdqa @tweak[4],`16*4`(%rsp)
  1683. aesdec $rndkey1,$inout4
  1684. movdqa @tweak[5],`16*5`(%rsp)
  1685. aesdec $rndkey1,$inout5
  1686. pxor $twtmp,$twtmp
  1687. pcmpgtd @tweak[5],$twtmp
  1688. jmp .Lxts_dec_loop6_enter
  1689. .align 16
  1690. .Lxts_dec_loop6:
  1691. aesdec $rndkey1,$inout0
  1692. aesdec $rndkey1,$inout1
  1693. dec $rounds
  1694. aesdec $rndkey1,$inout2
  1695. aesdec $rndkey1,$inout3
  1696. aesdec $rndkey1,$inout4
  1697. aesdec $rndkey1,$inout5
  1698. .Lxts_dec_loop6_enter:
  1699. $movkey 16($key),$rndkey1
  1700. aesdec $rndkey0,$inout0
  1701. aesdec $rndkey0,$inout1
  1702. lea 32($key),$key
  1703. aesdec $rndkey0,$inout2
  1704. aesdec $rndkey0,$inout3
  1705. aesdec $rndkey0,$inout4
  1706. aesdec $rndkey0,$inout5
  1707. $movkey ($key),$rndkey0
  1708. jnz .Lxts_dec_loop6
  1709. pshufd \$0x13,$twtmp,$twres
  1710. pxor $twtmp,$twtmp
  1711. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1712. aesdec $rndkey1,$inout0
  1713. pand $twmask,$twres # isolate carry and residue
  1714. aesdec $rndkey1,$inout1
  1715. pcmpgtd @tweak[5],$twtmp # broadcast upper bits
  1716. aesdec $rndkey1,$inout2
  1717. pxor $twres,@tweak[5]
  1718. aesdec $rndkey1,$inout3
  1719. aesdec $rndkey1,$inout4
  1720. aesdec $rndkey1,$inout5
  1721. $movkey 16($key),$rndkey1
  1722. pshufd \$0x13,$twtmp,$twres
  1723. pxor $twtmp,$twtmp
  1724. movdqa @tweak[5],@tweak[0]
  1725. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1726. aesdec $rndkey0,$inout0
  1727. pand $twmask,$twres # isolate carry and residue
  1728. aesdec $rndkey0,$inout1
  1729. pcmpgtd @tweak[5],$twtmp # broadcat upper bits
  1730. aesdec $rndkey0,$inout2
  1731. pxor $twres,@tweak[5]
  1732. aesdec $rndkey0,$inout3
  1733. aesdec $rndkey0,$inout4
  1734. aesdec $rndkey0,$inout5
  1735. $movkey 32($key),$rndkey0
  1736. pshufd \$0x13,$twtmp,$twres
  1737. pxor $twtmp,$twtmp
  1738. movdqa @tweak[5],@tweak[1]
  1739. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1740. aesdec $rndkey1,$inout0
  1741. pand $twmask,$twres # isolate carry and residue
  1742. aesdec $rndkey1,$inout1
  1743. pcmpgtd @tweak[5],$twtmp # broadcat upper bits
  1744. aesdec $rndkey1,$inout2
  1745. pxor $twres,@tweak[5]
  1746. aesdec $rndkey1,$inout3
  1747. aesdec $rndkey1,$inout4
  1748. aesdec $rndkey1,$inout5
  1749. pshufd \$0x13,$twtmp,$twres
  1750. pxor $twtmp,$twtmp
  1751. movdqa @tweak[5],@tweak[2]
  1752. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1753. aesdeclast $rndkey0,$inout0
  1754. pand $twmask,$twres # isolate carry and residue
  1755. aesdeclast $rndkey0,$inout1
  1756. pcmpgtd @tweak[5],$twtmp # broadcat upper bits
  1757. aesdeclast $rndkey0,$inout2
  1758. pxor $twres,@tweak[5]
  1759. aesdeclast $rndkey0,$inout3
  1760. aesdeclast $rndkey0,$inout4
  1761. aesdeclast $rndkey0,$inout5
  1762. pshufd \$0x13,$twtmp,$twres
  1763. pxor $twtmp,$twtmp
  1764. movdqa @tweak[5],@tweak[3]
  1765. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1766. xorps `16*0`(%rsp),$inout0 # output^=tweak
  1767. pand $twmask,$twres # isolate carry and residue
  1768. xorps `16*1`(%rsp),$inout1
  1769. pcmpgtd @tweak[5],$twtmp # broadcat upper bits
  1770. pxor $twres,@tweak[5]
  1771. xorps `16*2`(%rsp),$inout2
  1772. movups $inout0,`16*0`($out) # write output
  1773. xorps `16*3`(%rsp),$inout3
  1774. movups $inout1,`16*1`($out)
  1775. xorps `16*4`(%rsp),$inout4
  1776. movups $inout2,`16*2`($out)
  1777. xorps `16*5`(%rsp),$inout5
  1778. movups $inout3,`16*3`($out)
  1779. mov $rnds_,$rounds # restore $rounds
  1780. movups $inout4,`16*4`($out)
  1781. movups $inout5,`16*5`($out)
  1782. lea `16*6`($out),$out
  1783. sub \$16*6,$len
  1784. jnc .Lxts_dec_grandloop
  1785. lea 3($rounds,$rounds),$rounds # restore original value
  1786. mov $key_,$key # restore $key
  1787. mov $rounds,$rnds_ # backup $rounds
  1788. .Lxts_dec_short:
  1789. add \$16*6,$len
  1790. jz .Lxts_dec_done
  1791. cmp \$0x20,$len
  1792. jb .Lxts_dec_one
  1793. je .Lxts_dec_two
  1794. cmp \$0x40,$len
  1795. jb .Lxts_dec_three
  1796. je .Lxts_dec_four
  1797. pshufd \$0x13,$twtmp,$twres
  1798. movdqa @tweak[5],@tweak[4]
  1799. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1800. movdqu ($inp),$inout0
  1801. pand $twmask,$twres # isolate carry and residue
  1802. movdqu 16*1($inp),$inout1
  1803. pxor $twres,@tweak[5]
  1804. movdqu 16*2($inp),$inout2
  1805. pxor @tweak[0],$inout0
  1806. movdqu 16*3($inp),$inout3
  1807. pxor @tweak[1],$inout1
  1808. movdqu 16*4($inp),$inout4
  1809. lea 16*5($inp),$inp
  1810. pxor @tweak[2],$inout2
  1811. pxor @tweak[3],$inout3
  1812. pxor @tweak[4],$inout4
  1813. call _aesni_decrypt6
  1814. xorps @tweak[0],$inout0
  1815. xorps @tweak[1],$inout1
  1816. xorps @tweak[2],$inout2
  1817. movdqu $inout0,($out)
  1818. xorps @tweak[3],$inout3
  1819. movdqu $inout1,16*1($out)
  1820. xorps @tweak[4],$inout4
  1821. movdqu $inout2,16*2($out)
  1822. pxor $twtmp,$twtmp
  1823. movdqu $inout3,16*3($out)
  1824. pcmpgtd @tweak[5],$twtmp
  1825. movdqu $inout4,16*4($out)
  1826. lea 16*5($out),$out
  1827. pshufd \$0x13,$twtmp,@tweak[1] # $twres
  1828. and \$15,$len_
  1829. jz .Lxts_dec_ret
  1830. movdqa @tweak[5],@tweak[0]
  1831. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1832. pand $twmask,@tweak[1] # isolate carry and residue
  1833. pxor @tweak[5],@tweak[1]
  1834. jmp .Lxts_dec_done2
  1835. .align 16
  1836. .Lxts_dec_one:
  1837. movups ($inp),$inout0
  1838. lea 16*1($inp),$inp
  1839. xorps @tweak[0],$inout0
  1840. ___
  1841. &aesni_generate1("dec",$key,$rounds);
  1842. $code.=<<___;
  1843. xorps @tweak[0],$inout0
  1844. movdqa @tweak[1],@tweak[0]
  1845. movups $inout0,($out)
  1846. movdqa @tweak[2],@tweak[1]
  1847. lea 16*1($out),$out
  1848. jmp .Lxts_dec_done
  1849. .align 16
  1850. .Lxts_dec_two:
  1851. movups ($inp),$inout0
  1852. movups 16($inp),$inout1
  1853. lea 32($inp),$inp
  1854. xorps @tweak[0],$inout0
  1855. xorps @tweak[1],$inout1
  1856. call _aesni_decrypt3
  1857. xorps @tweak[0],$inout0
  1858. movdqa @tweak[2],@tweak[0]
  1859. xorps @tweak[1],$inout1
  1860. movdqa @tweak[3],@tweak[1]
  1861. movups $inout0,($out)
  1862. movups $inout1,16*1($out)
  1863. lea 16*2($out),$out
  1864. jmp .Lxts_dec_done
  1865. .align 16
  1866. .Lxts_dec_three:
  1867. movups ($inp),$inout0
  1868. movups 16*1($inp),$inout1
  1869. movups 16*2($inp),$inout2
  1870. lea 16*3($inp),$inp
  1871. xorps @tweak[0],$inout0
  1872. xorps @tweak[1],$inout1
  1873. xorps @tweak[2],$inout2
  1874. call _aesni_decrypt3
  1875. xorps @tweak[0],$inout0
  1876. movdqa @tweak[3],@tweak[0]
  1877. xorps @tweak[1],$inout1
  1878. movdqa @tweak[5],@tweak[1]
  1879. xorps @tweak[2],$inout2
  1880. movups $inout0,($out)
  1881. movups $inout1,16*1($out)
  1882. movups $inout2,16*2($out)
  1883. lea 16*3($out),$out
  1884. jmp .Lxts_dec_done
  1885. .align 16
  1886. .Lxts_dec_four:
  1887. pshufd \$0x13,$twtmp,$twres
  1888. movdqa @tweak[5],@tweak[4]
  1889. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  1890. movups ($inp),$inout0
  1891. pand $twmask,$twres # isolate carry and residue
  1892. movups 16*1($inp),$inout1
  1893. pxor $twres,@tweak[5]
  1894. movups 16*2($inp),$inout2
  1895. xorps @tweak[0],$inout0
  1896. movups 16*3($inp),$inout3
  1897. lea 16*4($inp),$inp
  1898. xorps @tweak[1],$inout1
  1899. xorps @tweak[2],$inout2
  1900. xorps @tweak[3],$inout3
  1901. call _aesni_decrypt4
  1902. xorps @tweak[0],$inout0
  1903. movdqa @tweak[4],@tweak[0]
  1904. xorps @tweak[1],$inout1
  1905. movdqa @tweak[5],@tweak[1]
  1906. xorps @tweak[2],$inout2
  1907. movups $inout0,($out)
  1908. xorps @tweak[3],$inout3
  1909. movups $inout1,16*1($out)
  1910. movups $inout2,16*2($out)
  1911. movups $inout3,16*3($out)
  1912. lea 16*4($out),$out
  1913. jmp .Lxts_dec_done
  1914. .align 16
  1915. .Lxts_dec_done:
  1916. and \$15,$len_
  1917. jz .Lxts_dec_ret
  1918. .Lxts_dec_done2:
  1919. mov $len_,$len
  1920. mov $key_,$key # restore $key
  1921. mov $rnds_,$rounds # restore $rounds
  1922. movups ($inp),$inout0
  1923. xorps @tweak[1],$inout0
  1924. ___
  1925. &aesni_generate1("dec",$key,$rounds);
  1926. $code.=<<___;
  1927. xorps @tweak[1],$inout0
  1928. movups $inout0,($out)
  1929. .Lxts_dec_steal:
  1930. movzb 16($inp),%eax # borrow $rounds ...
  1931. movzb ($out),%ecx # ... and $key
  1932. lea 1($inp),$inp
  1933. mov %al,($out)
  1934. mov %cl,16($out)
  1935. lea 1($out),$out
  1936. sub \$1,$len
  1937. jnz .Lxts_dec_steal
  1938. sub $len_,$out # rewind $out
  1939. mov $key_,$key # restore $key
  1940. mov $rnds_,$rounds # restore $rounds
  1941. movups ($out),$inout0
  1942. xorps @tweak[0],$inout0
  1943. ___
  1944. &aesni_generate1("dec",$key,$rounds);
  1945. $code.=<<___;
  1946. xorps @tweak[0],$inout0
  1947. movups $inout0,($out)
  1948. .Lxts_dec_ret:
  1949. ___
  1950. $code.=<<___ if ($win64);
  1951. movaps 0x60(%rsp),%xmm6
  1952. movaps 0x70(%rsp),%xmm7
  1953. movaps 0x80(%rsp),%xmm8
  1954. movaps 0x90(%rsp),%xmm9
  1955. movaps 0xa0(%rsp),%xmm10
  1956. movaps 0xb0(%rsp),%xmm11
  1957. movaps 0xc0(%rsp),%xmm12
  1958. movaps 0xd0(%rsp),%xmm13
  1959. movaps 0xe0(%rsp),%xmm14
  1960. movaps 0xf0(%rsp),%xmm15
  1961. ___
  1962. $code.=<<___;
  1963. lea $frame_size(%rsp),%rsp
  1964. .Lxts_dec_epilogue:
  1965. ret
  1966. .size aesni_xts_decrypt,.-aesni_xts_decrypt
  1967. ___
  1968. } }}
  1969. ########################################################################
  1970. # void $PREFIX_cbc_encrypt (const void *inp, void *out,
  1971. # size_t length, const AES_KEY *key,
  1972. # unsigned char *ivp,const int enc);
  1973. {
  1974. my $reserved = $win64?0x40:-0x18; # used in decrypt
  1975. $code.=<<___;
  1976. .globl ${PREFIX}_cbc_encrypt
  1977. .type ${PREFIX}_cbc_encrypt,\@function,6
  1978. .align 16
  1979. ${PREFIX}_cbc_encrypt:
  1980. test $len,$len # check length
  1981. jz .Lcbc_ret
  1982. mov 240($key),$rnds_ # key->rounds
  1983. mov $key,$key_ # backup $key
  1984. test %r9d,%r9d # 6th argument
  1985. jz .Lcbc_decrypt
  1986. #--------------------------- CBC ENCRYPT ------------------------------#
  1987. movups ($ivp),$inout0 # load iv as initial state
  1988. mov $rnds_,$rounds
  1989. cmp \$16,$len
  1990. jb .Lcbc_enc_tail
  1991. sub \$16,$len
  1992. jmp .Lcbc_enc_loop
  1993. .align 16
  1994. .Lcbc_enc_loop:
  1995. movups ($inp),$inout1 # load input
  1996. lea 16($inp),$inp
  1997. #xorps $inout1,$inout0
  1998. ___
  1999. &aesni_generate1("enc",$key,$rounds,$inout0,$inout1);
  2000. $code.=<<___;
  2001. mov $rnds_,$rounds # restore $rounds
  2002. mov $key_,$key # restore $key
  2003. movups $inout0,0($out) # store output
  2004. lea 16($out),$out
  2005. sub \$16,$len
  2006. jnc .Lcbc_enc_loop
  2007. add \$16,$len
  2008. jnz .Lcbc_enc_tail
  2009. movups $inout0,($ivp)
  2010. jmp .Lcbc_ret
  2011. .Lcbc_enc_tail:
  2012. mov $len,%rcx # zaps $key
  2013. xchg $inp,$out # $inp is %rsi and $out is %rdi now
  2014. .long 0x9066A4F3 # rep movsb
  2015. mov \$16,%ecx # zero tail
  2016. sub $len,%rcx
  2017. xor %eax,%eax
  2018. .long 0x9066AAF3 # rep stosb
  2019. lea -16(%rdi),%rdi # rewind $out by 1 block
  2020. mov $rnds_,$rounds # restore $rounds
  2021. mov %rdi,%rsi # $inp and $out are the same
  2022. mov $key_,$key # restore $key
  2023. xor $len,$len # len=16
  2024. jmp .Lcbc_enc_loop # one more spin
  2025. #--------------------------- CBC DECRYPT ------------------------------#
  2026. .align 16
  2027. .Lcbc_decrypt:
  2028. ___
  2029. $code.=<<___ if ($win64);
  2030. lea -0x58(%rsp),%rsp
  2031. movaps %xmm6,(%rsp)
  2032. movaps %xmm7,0x10(%rsp)
  2033. movaps %xmm8,0x20(%rsp)
  2034. movaps %xmm9,0x30(%rsp)
  2035. .Lcbc_decrypt_body:
  2036. ___
  2037. $code.=<<___;
  2038. movups ($ivp),$iv
  2039. mov $rnds_,$rounds
  2040. cmp \$0x70,$len
  2041. jbe .Lcbc_dec_tail
  2042. shr \$1,$rnds_
  2043. sub \$0x70,$len
  2044. mov $rnds_,$rounds
  2045. movaps $iv,$reserved(%rsp)
  2046. jmp .Lcbc_dec_loop8_enter
  2047. .align 16
  2048. .Lcbc_dec_loop8:
  2049. movaps $rndkey0,$reserved(%rsp) # save IV
  2050. movups $inout7,($out)
  2051. lea 0x10($out),$out
  2052. .Lcbc_dec_loop8_enter:
  2053. $movkey ($key),$rndkey0
  2054. movups ($inp),$inout0 # load input
  2055. movups 0x10($inp),$inout1
  2056. $movkey 16($key),$rndkey1
  2057. lea 32($key),$key
  2058. movdqu 0x20($inp),$inout2
  2059. xorps $rndkey0,$inout0
  2060. movdqu 0x30($inp),$inout3
  2061. xorps $rndkey0,$inout1
  2062. movdqu 0x40($inp),$inout4
  2063. aesdec $rndkey1,$inout0
  2064. pxor $rndkey0,$inout2
  2065. movdqu 0x50($inp),$inout5
  2066. aesdec $rndkey1,$inout1
  2067. pxor $rndkey0,$inout3
  2068. movdqu 0x60($inp),$inout6
  2069. aesdec $rndkey1,$inout2
  2070. pxor $rndkey0,$inout4
  2071. movdqu 0x70($inp),$inout7
  2072. aesdec $rndkey1,$inout3
  2073. pxor $rndkey0,$inout5
  2074. dec $rounds
  2075. aesdec $rndkey1,$inout4
  2076. pxor $rndkey0,$inout6
  2077. aesdec $rndkey1,$inout5
  2078. pxor $rndkey0,$inout7
  2079. $movkey ($key),$rndkey0
  2080. aesdec $rndkey1,$inout6
  2081. aesdec $rndkey1,$inout7
  2082. $movkey 16($key),$rndkey1
  2083. call .Ldec_loop8_enter
  2084. movups ($inp),$rndkey1 # re-load input
  2085. movups 0x10($inp),$rndkey0
  2086. xorps $reserved(%rsp),$inout0 # ^= IV
  2087. xorps $rndkey1,$inout1
  2088. movups 0x20($inp),$rndkey1
  2089. xorps $rndkey0,$inout2
  2090. movups 0x30($inp),$rndkey0
  2091. xorps $rndkey1,$inout3
  2092. movups 0x40($inp),$rndkey1
  2093. xorps $rndkey0,$inout4
  2094. movups 0x50($inp),$rndkey0
  2095. xorps $rndkey1,$inout5
  2096. movups 0x60($inp),$rndkey1
  2097. xorps $rndkey0,$inout6
  2098. movups 0x70($inp),$rndkey0 # IV
  2099. xorps $rndkey1,$inout7
  2100. movups $inout0,($out)
  2101. movups $inout1,0x10($out)
  2102. movups $inout2,0x20($out)
  2103. movups $inout3,0x30($out)
  2104. mov $rnds_,$rounds # restore $rounds
  2105. movups $inout4,0x40($out)
  2106. mov $key_,$key # restore $key
  2107. movups $inout5,0x50($out)
  2108. lea 0x80($inp),$inp
  2109. movups $inout6,0x60($out)
  2110. lea 0x70($out),$out
  2111. sub \$0x80,$len
  2112. ja .Lcbc_dec_loop8
  2113. movaps $inout7,$inout0
  2114. movaps $rndkey0,$iv
  2115. add \$0x70,$len
  2116. jle .Lcbc_dec_tail_collected
  2117. movups $inout0,($out)
  2118. lea 1($rnds_,$rnds_),$rounds
  2119. lea 0x10($out),$out
  2120. .Lcbc_dec_tail:
  2121. movups ($inp),$inout0
  2122. movaps $inout0,$in0
  2123. cmp \$0x10,$len
  2124. jbe .Lcbc_dec_one
  2125. movups 0x10($inp),$inout1
  2126. movaps $inout1,$in1
  2127. cmp \$0x20,$len
  2128. jbe .Lcbc_dec_two
  2129. movups 0x20($inp),$inout2
  2130. movaps $inout2,$in2
  2131. cmp \$0x30,$len
  2132. jbe .Lcbc_dec_three
  2133. movups 0x30($inp),$inout3
  2134. cmp \$0x40,$len
  2135. jbe .Lcbc_dec_four
  2136. movups 0x40($inp),$inout4
  2137. cmp \$0x50,$len
  2138. jbe .Lcbc_dec_five
  2139. movups 0x50($inp),$inout5
  2140. cmp \$0x60,$len
  2141. jbe .Lcbc_dec_six
  2142. movups 0x60($inp),$inout6
  2143. movaps $iv,$reserved(%rsp) # save IV
  2144. call _aesni_decrypt8
  2145. movups ($inp),$rndkey1
  2146. movups 0x10($inp),$rndkey0
  2147. xorps $reserved(%rsp),$inout0 # ^= IV
  2148. xorps $rndkey1,$inout1
  2149. movups 0x20($inp),$rndkey1
  2150. xorps $rndkey0,$inout2
  2151. movups 0x30($inp),$rndkey0
  2152. xorps $rndkey1,$inout3
  2153. movups 0x40($inp),$rndkey1
  2154. xorps $rndkey0,$inout4
  2155. movups 0x50($inp),$rndkey0
  2156. xorps $rndkey1,$inout5
  2157. movups 0x60($inp),$iv # IV
  2158. xorps $rndkey0,$inout6
  2159. movups $inout0,($out)
  2160. movups $inout1,0x10($out)
  2161. movups $inout2,0x20($out)
  2162. movups $inout3,0x30($out)
  2163. movups $inout4,0x40($out)
  2164. movups $inout5,0x50($out)
  2165. lea 0x60($out),$out
  2166. movaps $inout6,$inout0
  2167. sub \$0x70,$len
  2168. jmp .Lcbc_dec_tail_collected
  2169. .align 16
  2170. .Lcbc_dec_one:
  2171. ___
  2172. &aesni_generate1("dec",$key,$rounds);
  2173. $code.=<<___;
  2174. xorps $iv,$inout0
  2175. movaps $in0,$iv
  2176. sub \$0x10,$len
  2177. jmp .Lcbc_dec_tail_collected
  2178. .align 16
  2179. .Lcbc_dec_two:
  2180. xorps $inout2,$inout2
  2181. call _aesni_decrypt3
  2182. xorps $iv,$inout0
  2183. xorps $in0,$inout1
  2184. movups $inout0,($out)
  2185. movaps $in1,$iv
  2186. movaps $inout1,$inout0
  2187. lea 0x10($out),$out
  2188. sub \$0x20,$len
  2189. jmp .Lcbc_dec_tail_collected
  2190. .align 16
  2191. .Lcbc_dec_three:
  2192. call _aesni_decrypt3
  2193. xorps $iv,$inout0
  2194. xorps $in0,$inout1
  2195. movups $inout0,($out)
  2196. xorps $in1,$inout2
  2197. movups $inout1,0x10($out)
  2198. movaps $in2,$iv
  2199. movaps $inout2,$inout0
  2200. lea 0x20($out),$out
  2201. sub \$0x30,$len
  2202. jmp .Lcbc_dec_tail_collected
  2203. .align 16
  2204. .Lcbc_dec_four:
  2205. call _aesni_decrypt4
  2206. xorps $iv,$inout0
  2207. movups 0x30($inp),$iv
  2208. xorps $in0,$inout1
  2209. movups $inout0,($out)
  2210. xorps $in1,$inout2
  2211. movups $inout1,0x10($out)
  2212. xorps $in2,$inout3
  2213. movups $inout2,0x20($out)
  2214. movaps $inout3,$inout0
  2215. lea 0x30($out),$out
  2216. sub \$0x40,$len
  2217. jmp .Lcbc_dec_tail_collected
  2218. .align 16
  2219. .Lcbc_dec_five:
  2220. xorps $inout5,$inout5
  2221. call _aesni_decrypt6
  2222. movups 0x10($inp),$rndkey1
  2223. movups 0x20($inp),$rndkey0
  2224. xorps $iv,$inout0
  2225. xorps $in0,$inout1
  2226. xorps $rndkey1,$inout2
  2227. movups 0x30($inp),$rndkey1
  2228. xorps $rndkey0,$inout3
  2229. movups 0x40($inp),$iv
  2230. xorps $rndkey1,$inout4
  2231. movups $inout0,($out)
  2232. movups $inout1,0x10($out)
  2233. movups $inout2,0x20($out)
  2234. movups $inout3,0x30($out)
  2235. lea 0x40($out),$out
  2236. movaps $inout4,$inout0
  2237. sub \$0x50,$len
  2238. jmp .Lcbc_dec_tail_collected
  2239. .align 16
  2240. .Lcbc_dec_six:
  2241. call _aesni_decrypt6
  2242. movups 0x10($inp),$rndkey1
  2243. movups 0x20($inp),$rndkey0
  2244. xorps $iv,$inout0
  2245. xorps $in0,$inout1
  2246. xorps $rndkey1,$inout2
  2247. movups 0x30($inp),$rndkey1
  2248. xorps $rndkey0,$inout3
  2249. movups 0x40($inp),$rndkey0
  2250. xorps $rndkey1,$inout4
  2251. movups 0x50($inp),$iv
  2252. xorps $rndkey0,$inout5
  2253. movups $inout0,($out)
  2254. movups $inout1,0x10($out)
  2255. movups $inout2,0x20($out)
  2256. movups $inout3,0x30($out)
  2257. movups $inout4,0x40($out)
  2258. lea 0x50($out),$out
  2259. movaps $inout5,$inout0
  2260. sub \$0x60,$len
  2261. jmp .Lcbc_dec_tail_collected
  2262. .align 16
  2263. .Lcbc_dec_tail_collected:
  2264. and \$15,$len
  2265. movups $iv,($ivp)
  2266. jnz .Lcbc_dec_tail_partial
  2267. movups $inout0,($out)
  2268. jmp .Lcbc_dec_ret
  2269. .align 16
  2270. .Lcbc_dec_tail_partial:
  2271. movaps $inout0,$reserved(%rsp)
  2272. mov \$16,%rcx
  2273. mov $out,%rdi
  2274. sub $len,%rcx
  2275. lea $reserved(%rsp),%rsi
  2276. .long 0x9066A4F3 # rep movsb
  2277. .Lcbc_dec_ret:
  2278. ___
  2279. $code.=<<___ if ($win64);
  2280. movaps (%rsp),%xmm6
  2281. movaps 0x10(%rsp),%xmm7
  2282. movaps 0x20(%rsp),%xmm8
  2283. movaps 0x30(%rsp),%xmm9
  2284. lea 0x58(%rsp),%rsp
  2285. ___
  2286. $code.=<<___;
  2287. .Lcbc_ret:
  2288. ret
  2289. .size ${PREFIX}_cbc_encrypt,.-${PREFIX}_cbc_encrypt
  2290. ___
  2291. }
  2292. # int $PREFIX_set_[en|de]crypt_key (const unsigned char *userKey,
  2293. # int bits, AES_KEY *key)
  2294. { my ($inp,$bits,$key) = @_4args;
  2295. $bits =~ s/%r/%e/;
  2296. $code.=<<___;
  2297. .globl ${PREFIX}_set_decrypt_key
  2298. .type ${PREFIX}_set_decrypt_key,\@abi-omnipotent
  2299. .align 16
  2300. ${PREFIX}_set_decrypt_key:
  2301. .byte 0x48,0x83,0xEC,0x08 # sub rsp,8
  2302. call __aesni_set_encrypt_key
  2303. shl \$4,$bits # rounds-1 after _aesni_set_encrypt_key
  2304. test %eax,%eax
  2305. jnz .Ldec_key_ret
  2306. lea 16($key,$bits),$inp # points at the end of key schedule
  2307. $movkey ($key),%xmm0 # just swap
  2308. $movkey ($inp),%xmm1
  2309. $movkey %xmm0,($inp)
  2310. $movkey %xmm1,($key)
  2311. lea 16($key),$key
  2312. lea -16($inp),$inp
  2313. .Ldec_key_inverse:
  2314. $movkey ($key),%xmm0 # swap and inverse
  2315. $movkey ($inp),%xmm1
  2316. aesimc %xmm0,%xmm0
  2317. aesimc %xmm1,%xmm1
  2318. lea 16($key),$key
  2319. lea -16($inp),$inp
  2320. $movkey %xmm0,16($inp)
  2321. $movkey %xmm1,-16($key)
  2322. cmp $key,$inp
  2323. ja .Ldec_key_inverse
  2324. $movkey ($key),%xmm0 # inverse middle
  2325. aesimc %xmm0,%xmm0
  2326. $movkey %xmm0,($inp)
  2327. .Ldec_key_ret:
  2328. add \$8,%rsp
  2329. ret
  2330. .LSEH_end_set_decrypt_key:
  2331. .size ${PREFIX}_set_decrypt_key,.-${PREFIX}_set_decrypt_key
  2332. ___
  2333. # This is based on submission by
  2334. #
  2335. # Huang Ying <ying.huang@intel.com>
  2336. # Vinodh Gopal <vinodh.gopal@intel.com>
  2337. # Kahraman Akdemir
  2338. #
  2339. # Agressively optimized in respect to aeskeygenassist's critical path
  2340. # and is contained in %xmm0-5 to meet Win64 ABI requirement.
  2341. #
  2342. $code.=<<___;
  2343. .globl ${PREFIX}_set_encrypt_key
  2344. .type ${PREFIX}_set_encrypt_key,\@abi-omnipotent
  2345. .align 16
  2346. ${PREFIX}_set_encrypt_key:
  2347. __aesni_set_encrypt_key:
  2348. .byte 0x48,0x83,0xEC,0x08 # sub rsp,8
  2349. mov \$-1,%rax
  2350. test $inp,$inp
  2351. jz .Lenc_key_ret
  2352. test $key,$key
  2353. jz .Lenc_key_ret
  2354. movups ($inp),%xmm0 # pull first 128 bits of *userKey
  2355. xorps %xmm4,%xmm4 # low dword of xmm4 is assumed 0
  2356. lea 16($key),%rax
  2357. cmp \$256,$bits
  2358. je .L14rounds
  2359. cmp \$192,$bits
  2360. je .L12rounds
  2361. cmp \$128,$bits
  2362. jne .Lbad_keybits
  2363. .L10rounds:
  2364. mov \$9,$bits # 10 rounds for 128-bit key
  2365. $movkey %xmm0,($key) # round 0
  2366. aeskeygenassist \$0x1,%xmm0,%xmm1 # round 1
  2367. call .Lkey_expansion_128_cold
  2368. aeskeygenassist \$0x2,%xmm0,%xmm1 # round 2
  2369. call .Lkey_expansion_128
  2370. aeskeygenassist \$0x4,%xmm0,%xmm1 # round 3
  2371. call .Lkey_expansion_128
  2372. aeskeygenassist \$0x8,%xmm0,%xmm1 # round 4
  2373. call .Lkey_expansion_128
  2374. aeskeygenassist \$0x10,%xmm0,%xmm1 # round 5
  2375. call .Lkey_expansion_128
  2376. aeskeygenassist \$0x20,%xmm0,%xmm1 # round 6
  2377. call .Lkey_expansion_128
  2378. aeskeygenassist \$0x40,%xmm0,%xmm1 # round 7
  2379. call .Lkey_expansion_128
  2380. aeskeygenassist \$0x80,%xmm0,%xmm1 # round 8
  2381. call .Lkey_expansion_128
  2382. aeskeygenassist \$0x1b,%xmm0,%xmm1 # round 9
  2383. call .Lkey_expansion_128
  2384. aeskeygenassist \$0x36,%xmm0,%xmm1 # round 10
  2385. call .Lkey_expansion_128
  2386. $movkey %xmm0,(%rax)
  2387. mov $bits,80(%rax) # 240(%rdx)
  2388. xor %eax,%eax
  2389. jmp .Lenc_key_ret
  2390. .align 16
  2391. .L12rounds:
  2392. movq 16($inp),%xmm2 # remaining 1/3 of *userKey
  2393. mov \$11,$bits # 12 rounds for 192
  2394. $movkey %xmm0,($key) # round 0
  2395. aeskeygenassist \$0x1,%xmm2,%xmm1 # round 1,2
  2396. call .Lkey_expansion_192a_cold
  2397. aeskeygenassist \$0x2,%xmm2,%xmm1 # round 2,3
  2398. call .Lkey_expansion_192b
  2399. aeskeygenassist \$0x4,%xmm2,%xmm1 # round 4,5
  2400. call .Lkey_expansion_192a
  2401. aeskeygenassist \$0x8,%xmm2,%xmm1 # round 5,6
  2402. call .Lkey_expansion_192b
  2403. aeskeygenassist \$0x10,%xmm2,%xmm1 # round 7,8
  2404. call .Lkey_expansion_192a
  2405. aeskeygenassist \$0x20,%xmm2,%xmm1 # round 8,9
  2406. call .Lkey_expansion_192b
  2407. aeskeygenassist \$0x40,%xmm2,%xmm1 # round 10,11
  2408. call .Lkey_expansion_192a
  2409. aeskeygenassist \$0x80,%xmm2,%xmm1 # round 11,12
  2410. call .Lkey_expansion_192b
  2411. $movkey %xmm0,(%rax)
  2412. mov $bits,48(%rax) # 240(%rdx)
  2413. xor %rax, %rax
  2414. jmp .Lenc_key_ret
  2415. .align 16
  2416. .L14rounds:
  2417. movups 16($inp),%xmm2 # remaning half of *userKey
  2418. mov \$13,$bits # 14 rounds for 256
  2419. lea 16(%rax),%rax
  2420. $movkey %xmm0,($key) # round 0
  2421. $movkey %xmm2,16($key) # round 1
  2422. aeskeygenassist \$0x1,%xmm2,%xmm1 # round 2
  2423. call .Lkey_expansion_256a_cold
  2424. aeskeygenassist \$0x1,%xmm0,%xmm1 # round 3
  2425. call .Lkey_expansion_256b
  2426. aeskeygenassist \$0x2,%xmm2,%xmm1 # round 4
  2427. call .Lkey_expansion_256a
  2428. aeskeygenassist \$0x2,%xmm0,%xmm1 # round 5
  2429. call .Lkey_expansion_256b
  2430. aeskeygenassist \$0x4,%xmm2,%xmm1 # round 6
  2431. call .Lkey_expansion_256a
  2432. aeskeygenassist \$0x4,%xmm0,%xmm1 # round 7
  2433. call .Lkey_expansion_256b
  2434. aeskeygenassist \$0x8,%xmm2,%xmm1 # round 8
  2435. call .Lkey_expansion_256a
  2436. aeskeygenassist \$0x8,%xmm0,%xmm1 # round 9
  2437. call .Lkey_expansion_256b
  2438. aeskeygenassist \$0x10,%xmm2,%xmm1 # round 10
  2439. call .Lkey_expansion_256a
  2440. aeskeygenassist \$0x10,%xmm0,%xmm1 # round 11
  2441. call .Lkey_expansion_256b
  2442. aeskeygenassist \$0x20,%xmm2,%xmm1 # round 12
  2443. call .Lkey_expansion_256a
  2444. aeskeygenassist \$0x20,%xmm0,%xmm1 # round 13
  2445. call .Lkey_expansion_256b
  2446. aeskeygenassist \$0x40,%xmm2,%xmm1 # round 14
  2447. call .Lkey_expansion_256a
  2448. $movkey %xmm0,(%rax)
  2449. mov $bits,16(%rax) # 240(%rdx)
  2450. xor %rax,%rax
  2451. jmp .Lenc_key_ret
  2452. .align 16
  2453. .Lbad_keybits:
  2454. mov \$-2,%rax
  2455. .Lenc_key_ret:
  2456. add \$8,%rsp
  2457. ret
  2458. .LSEH_end_set_encrypt_key:
  2459. .align 16
  2460. .Lkey_expansion_128:
  2461. $movkey %xmm0,(%rax)
  2462. lea 16(%rax),%rax
  2463. .Lkey_expansion_128_cold:
  2464. shufps \$0b00010000,%xmm0,%xmm4
  2465. xorps %xmm4, %xmm0
  2466. shufps \$0b10001100,%xmm0,%xmm4
  2467. xorps %xmm4, %xmm0
  2468. shufps \$0b11111111,%xmm1,%xmm1 # critical path
  2469. xorps %xmm1,%xmm0
  2470. ret
  2471. .align 16
  2472. .Lkey_expansion_192a:
  2473. $movkey %xmm0,(%rax)
  2474. lea 16(%rax),%rax
  2475. .Lkey_expansion_192a_cold:
  2476. movaps %xmm2, %xmm5
  2477. .Lkey_expansion_192b_warm:
  2478. shufps \$0b00010000,%xmm0,%xmm4
  2479. movdqa %xmm2,%xmm3
  2480. xorps %xmm4,%xmm0
  2481. shufps \$0b10001100,%xmm0,%xmm4
  2482. pslldq \$4,%xmm3
  2483. xorps %xmm4,%xmm0
  2484. pshufd \$0b01010101,%xmm1,%xmm1 # critical path
  2485. pxor %xmm3,%xmm2
  2486. pxor %xmm1,%xmm0
  2487. pshufd \$0b11111111,%xmm0,%xmm3
  2488. pxor %xmm3,%xmm2
  2489. ret
  2490. .align 16
  2491. .Lkey_expansion_192b:
  2492. movaps %xmm0,%xmm3
  2493. shufps \$0b01000100,%xmm0,%xmm5
  2494. $movkey %xmm5,(%rax)
  2495. shufps \$0b01001110,%xmm2,%xmm3
  2496. $movkey %xmm3,16(%rax)
  2497. lea 32(%rax),%rax
  2498. jmp .Lkey_expansion_192b_warm
  2499. .align 16
  2500. .Lkey_expansion_256a:
  2501. $movkey %xmm2,(%rax)
  2502. lea 16(%rax),%rax
  2503. .Lkey_expansion_256a_cold:
  2504. shufps \$0b00010000,%xmm0,%xmm4
  2505. xorps %xmm4,%xmm0
  2506. shufps \$0b10001100,%xmm0,%xmm4
  2507. xorps %xmm4,%xmm0
  2508. shufps \$0b11111111,%xmm1,%xmm1 # critical path
  2509. xorps %xmm1,%xmm0
  2510. ret
  2511. .align 16
  2512. .Lkey_expansion_256b:
  2513. $movkey %xmm0,(%rax)
  2514. lea 16(%rax),%rax
  2515. shufps \$0b00010000,%xmm2,%xmm4
  2516. xorps %xmm4,%xmm2
  2517. shufps \$0b10001100,%xmm2,%xmm4
  2518. xorps %xmm4,%xmm2
  2519. shufps \$0b10101010,%xmm1,%xmm1 # critical path
  2520. xorps %xmm1,%xmm2
  2521. ret
  2522. .size ${PREFIX}_set_encrypt_key,.-${PREFIX}_set_encrypt_key
  2523. .size __aesni_set_encrypt_key,.-__aesni_set_encrypt_key
  2524. ___
  2525. }
  2526. $code.=<<___;
  2527. .align 64
  2528. .Lbswap_mask:
  2529. .byte 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
  2530. .Lincrement32:
  2531. .long 6,6,6,0
  2532. .Lincrement64:
  2533. .long 1,0,0,0
  2534. .Lxts_magic:
  2535. .long 0x87,0,1,0
  2536. .asciz "AES for Intel AES-NI, CRYPTOGAMS by <appro\@openssl.org>"
  2537. .align 64
  2538. ___
  2539. # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
  2540. # CONTEXT *context,DISPATCHER_CONTEXT *disp)
  2541. if ($win64) {
  2542. $rec="%rcx";
  2543. $frame="%rdx";
  2544. $context="%r8";
  2545. $disp="%r9";
  2546. $code.=<<___;
  2547. .extern __imp_RtlVirtualUnwind
  2548. ___
  2549. $code.=<<___ if ($PREFIX eq "aesni");
  2550. .type ecb_se_handler,\@abi-omnipotent
  2551. .align 16
  2552. ecb_se_handler:
  2553. push %rsi
  2554. push %rdi
  2555. push %rbx
  2556. push %rbp
  2557. push %r12
  2558. push %r13
  2559. push %r14
  2560. push %r15
  2561. pushfq
  2562. sub \$64,%rsp
  2563. mov 152($context),%rax # pull context->Rsp
  2564. jmp .Lcommon_seh_tail
  2565. .size ecb_se_handler,.-ecb_se_handler
  2566. .type ccm64_se_handler,\@abi-omnipotent
  2567. .align 16
  2568. ccm64_se_handler:
  2569. push %rsi
  2570. push %rdi
  2571. push %rbx
  2572. push %rbp
  2573. push %r12
  2574. push %r13
  2575. push %r14
  2576. push %r15
  2577. pushfq
  2578. sub \$64,%rsp
  2579. mov 120($context),%rax # pull context->Rax
  2580. mov 248($context),%rbx # pull context->Rip
  2581. mov 8($disp),%rsi # disp->ImageBase
  2582. mov 56($disp),%r11 # disp->HandlerData
  2583. mov 0(%r11),%r10d # HandlerData[0]
  2584. lea (%rsi,%r10),%r10 # prologue label
  2585. cmp %r10,%rbx # context->Rip<prologue label
  2586. jb .Lcommon_seh_tail
  2587. mov 152($context),%rax # pull context->Rsp
  2588. mov 4(%r11),%r10d # HandlerData[1]
  2589. lea (%rsi,%r10),%r10 # epilogue label
  2590. cmp %r10,%rbx # context->Rip>=epilogue label
  2591. jae .Lcommon_seh_tail
  2592. lea 0(%rax),%rsi # %xmm save area
  2593. lea 512($context),%rdi # &context.Xmm6
  2594. mov \$8,%ecx # 4*sizeof(%xmm0)/sizeof(%rax)
  2595. .long 0xa548f3fc # cld; rep movsq
  2596. lea 0x58(%rax),%rax # adjust stack pointer
  2597. jmp .Lcommon_seh_tail
  2598. .size ccm64_se_handler,.-ccm64_se_handler
  2599. .type ctr32_se_handler,\@abi-omnipotent
  2600. .align 16
  2601. ctr32_se_handler:
  2602. push %rsi
  2603. push %rdi
  2604. push %rbx
  2605. push %rbp
  2606. push %r12
  2607. push %r13
  2608. push %r14
  2609. push %r15
  2610. pushfq
  2611. sub \$64,%rsp
  2612. mov 120($context),%rax # pull context->Rax
  2613. mov 248($context),%rbx # pull context->Rip
  2614. lea .Lctr32_body(%rip),%r10
  2615. cmp %r10,%rbx # context->Rip<"prologue" label
  2616. jb .Lcommon_seh_tail
  2617. mov 152($context),%rax # pull context->Rsp
  2618. lea .Lctr32_ret(%rip),%r10
  2619. cmp %r10,%rbx
  2620. jae .Lcommon_seh_tail
  2621. lea 0x20(%rax),%rsi # %xmm save area
  2622. lea 512($context),%rdi # &context.Xmm6
  2623. mov \$20,%ecx # 10*sizeof(%xmm0)/sizeof(%rax)
  2624. .long 0xa548f3fc # cld; rep movsq
  2625. lea 0xc8(%rax),%rax # adjust stack pointer
  2626. jmp .Lcommon_seh_tail
  2627. .size ctr32_se_handler,.-ctr32_se_handler
  2628. .type xts_se_handler,\@abi-omnipotent
  2629. .align 16
  2630. xts_se_handler:
  2631. push %rsi
  2632. push %rdi
  2633. push %rbx
  2634. push %rbp
  2635. push %r12
  2636. push %r13
  2637. push %r14
  2638. push %r15
  2639. pushfq
  2640. sub \$64,%rsp
  2641. mov 120($context),%rax # pull context->Rax
  2642. mov 248($context),%rbx # pull context->Rip
  2643. mov 8($disp),%rsi # disp->ImageBase
  2644. mov 56($disp),%r11 # disp->HandlerData
  2645. mov 0(%r11),%r10d # HandlerData[0]
  2646. lea (%rsi,%r10),%r10 # prologue lable
  2647. cmp %r10,%rbx # context->Rip<prologue label
  2648. jb .Lcommon_seh_tail
  2649. mov 152($context),%rax # pull context->Rsp
  2650. mov 4(%r11),%r10d # HandlerData[1]
  2651. lea (%rsi,%r10),%r10 # epilogue label
  2652. cmp %r10,%rbx # context->Rip>=epilogue label
  2653. jae .Lcommon_seh_tail
  2654. lea 0x60(%rax),%rsi # %xmm save area
  2655. lea 512($context),%rdi # & context.Xmm6
  2656. mov \$20,%ecx # 10*sizeof(%xmm0)/sizeof(%rax)
  2657. .long 0xa548f3fc # cld; rep movsq
  2658. lea 0x68+160(%rax),%rax # adjust stack pointer
  2659. jmp .Lcommon_seh_tail
  2660. .size xts_se_handler,.-xts_se_handler
  2661. ___
  2662. $code.=<<___;
  2663. .type cbc_se_handler,\@abi-omnipotent
  2664. .align 16
  2665. cbc_se_handler:
  2666. push %rsi
  2667. push %rdi
  2668. push %rbx
  2669. push %rbp
  2670. push %r12
  2671. push %r13
  2672. push %r14
  2673. push %r15
  2674. pushfq
  2675. sub \$64,%rsp
  2676. mov 152($context),%rax # pull context->Rsp
  2677. mov 248($context),%rbx # pull context->Rip
  2678. lea .Lcbc_decrypt(%rip),%r10
  2679. cmp %r10,%rbx # context->Rip<"prologue" label
  2680. jb .Lcommon_seh_tail
  2681. lea .Lcbc_decrypt_body(%rip),%r10
  2682. cmp %r10,%rbx # context->Rip<cbc_decrypt_body
  2683. jb .Lrestore_cbc_rax
  2684. lea .Lcbc_ret(%rip),%r10
  2685. cmp %r10,%rbx # context->Rip>="epilogue" label
  2686. jae .Lcommon_seh_tail
  2687. lea 0(%rax),%rsi # top of stack
  2688. lea 512($context),%rdi # &context.Xmm6
  2689. mov \$8,%ecx # 4*sizeof(%xmm0)/sizeof(%rax)
  2690. .long 0xa548f3fc # cld; rep movsq
  2691. lea 0x58(%rax),%rax # adjust stack pointer
  2692. jmp .Lcommon_seh_tail
  2693. .Lrestore_cbc_rax:
  2694. mov 120($context),%rax
  2695. .Lcommon_seh_tail:
  2696. mov 8(%rax),%rdi
  2697. mov 16(%rax),%rsi
  2698. mov %rax,152($context) # restore context->Rsp
  2699. mov %rsi,168($context) # restore context->Rsi
  2700. mov %rdi,176($context) # restore context->Rdi
  2701. mov 40($disp),%rdi # disp->ContextRecord
  2702. mov $context,%rsi # context
  2703. mov \$154,%ecx # sizeof(CONTEXT)
  2704. .long 0xa548f3fc # cld; rep movsq
  2705. mov $disp,%rsi
  2706. xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
  2707. mov 8(%rsi),%rdx # arg2, disp->ImageBase
  2708. mov 0(%rsi),%r8 # arg3, disp->ControlPc
  2709. mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
  2710. mov 40(%rsi),%r10 # disp->ContextRecord
  2711. lea 56(%rsi),%r11 # &disp->HandlerData
  2712. lea 24(%rsi),%r12 # &disp->EstablisherFrame
  2713. mov %r10,32(%rsp) # arg5
  2714. mov %r11,40(%rsp) # arg6
  2715. mov %r12,48(%rsp) # arg7
  2716. mov %rcx,56(%rsp) # arg8, (NULL)
  2717. call *__imp_RtlVirtualUnwind(%rip)
  2718. mov \$1,%eax # ExceptionContinueSearch
  2719. add \$64,%rsp
  2720. popfq
  2721. pop %r15
  2722. pop %r14
  2723. pop %r13
  2724. pop %r12
  2725. pop %rbp
  2726. pop %rbx
  2727. pop %rdi
  2728. pop %rsi
  2729. ret
  2730. .size cbc_se_handler,.-cbc_se_handler
  2731. .section .pdata
  2732. .align 4
  2733. ___
  2734. $code.=<<___ if ($PREFIX eq "aesni");
  2735. .rva .LSEH_begin_aesni_ecb_encrypt
  2736. .rva .LSEH_end_aesni_ecb_encrypt
  2737. .rva .LSEH_info_ecb
  2738. .rva .LSEH_begin_aesni_ccm64_encrypt_blocks
  2739. .rva .LSEH_end_aesni_ccm64_encrypt_blocks
  2740. .rva .LSEH_info_ccm64_enc
  2741. .rva .LSEH_begin_aesni_ccm64_decrypt_blocks
  2742. .rva .LSEH_end_aesni_ccm64_decrypt_blocks
  2743. .rva .LSEH_info_ccm64_dec
  2744. .rva .LSEH_begin_aesni_ctr32_encrypt_blocks
  2745. .rva .LSEH_end_aesni_ctr32_encrypt_blocks
  2746. .rva .LSEH_info_ctr32
  2747. .rva .LSEH_begin_aesni_xts_encrypt
  2748. .rva .LSEH_end_aesni_xts_encrypt
  2749. .rva .LSEH_info_xts_enc
  2750. .rva .LSEH_begin_aesni_xts_decrypt
  2751. .rva .LSEH_end_aesni_xts_decrypt
  2752. .rva .LSEH_info_xts_dec
  2753. ___
  2754. $code.=<<___;
  2755. .rva .LSEH_begin_${PREFIX}_cbc_encrypt
  2756. .rva .LSEH_end_${PREFIX}_cbc_encrypt
  2757. .rva .LSEH_info_cbc
  2758. .rva ${PREFIX}_set_decrypt_key
  2759. .rva .LSEH_end_set_decrypt_key
  2760. .rva .LSEH_info_key
  2761. .rva ${PREFIX}_set_encrypt_key
  2762. .rva .LSEH_end_set_encrypt_key
  2763. .rva .LSEH_info_key
  2764. .section .xdata
  2765. .align 8
  2766. ___
  2767. $code.=<<___ if ($PREFIX eq "aesni");
  2768. .LSEH_info_ecb:
  2769. .byte 9,0,0,0
  2770. .rva ecb_se_handler
  2771. .LSEH_info_ccm64_enc:
  2772. .byte 9,0,0,0
  2773. .rva ccm64_se_handler
  2774. .rva .Lccm64_enc_body,.Lccm64_enc_ret # HandlerData[]
  2775. .LSEH_info_ccm64_dec:
  2776. .byte 9,0,0,0
  2777. .rva ccm64_se_handler
  2778. .rva .Lccm64_dec_body,.Lccm64_dec_ret # HandlerData[]
  2779. .LSEH_info_ctr32:
  2780. .byte 9,0,0,0
  2781. .rva ctr32_se_handler
  2782. .LSEH_info_xts_enc:
  2783. .byte 9,0,0,0
  2784. .rva xts_se_handler
  2785. .rva .Lxts_enc_body,.Lxts_enc_epilogue # HandlerData[]
  2786. .LSEH_info_xts_dec:
  2787. .byte 9,0,0,0
  2788. .rva xts_se_handler
  2789. .rva .Lxts_dec_body,.Lxts_dec_epilogue # HandlerData[]
  2790. ___
  2791. $code.=<<___;
  2792. .LSEH_info_cbc:
  2793. .byte 9,0,0,0
  2794. .rva cbc_se_handler
  2795. .LSEH_info_key:
  2796. .byte 0x01,0x04,0x01,0x00
  2797. .byte 0x04,0x02,0x00,0x00 # sub rsp,8
  2798. ___
  2799. }
  2800. sub rex {
  2801. local *opcode=shift;
  2802. my ($dst,$src)=@_;
  2803. my $rex=0;
  2804. $rex|=0x04 if($dst>=8);
  2805. $rex|=0x01 if($src>=8);
  2806. push @opcode,$rex|0x40 if($rex);
  2807. }
  2808. sub aesni {
  2809. my $line=shift;
  2810. my @opcode=(0x66);
  2811. if ($line=~/(aeskeygenassist)\s+\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
  2812. rex(\@opcode,$4,$3);
  2813. push @opcode,0x0f,0x3a,0xdf;
  2814. push @opcode,0xc0|($3&7)|(($4&7)<<3); # ModR/M
  2815. my $c=$2;
  2816. push @opcode,$c=~/^0/?oct($c):$c;
  2817. return ".byte\t".join(',',@opcode);
  2818. }
  2819. elsif ($line=~/(aes[a-z]+)\s+%xmm([0-9]+),\s*%xmm([0-9]+)/) {
  2820. my %opcodelet = (
  2821. "aesimc" => 0xdb,
  2822. "aesenc" => 0xdc, "aesenclast" => 0xdd,
  2823. "aesdec" => 0xde, "aesdeclast" => 0xdf
  2824. );
  2825. return undef if (!defined($opcodelet{$1}));
  2826. rex(\@opcode,$3,$2);
  2827. push @opcode,0x0f,0x38,$opcodelet{$1};
  2828. push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
  2829. return ".byte\t".join(',',@opcode);
  2830. }
  2831. return $line;
  2832. }
  2833. $code =~ s/\`([^\`]*)\`/eval($1)/gem;
  2834. $code =~ s/\b(aes.*%xmm[0-9]+).*$/aesni($1)/gem;
  2835. print $code;
  2836. close STDOUT;