aes-586.pl 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000
  1. #! /usr/bin/env perl
  2. # Copyright 2004-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the OpenSSL license (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. #
  9. # ====================================================================
  10. # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
  11. # project. The module is, however, dual licensed under OpenSSL and
  12. # CRYPTOGAMS licenses depending on where you obtain it. For further
  13. # details see http://www.openssl.org/~appro/cryptogams/.
  14. # ====================================================================
  15. #
  16. # Version 4.3.
  17. #
  18. # You might fail to appreciate this module performance from the first
  19. # try. If compared to "vanilla" linux-ia32-icc target, i.e. considered
  20. # to be *the* best Intel C compiler without -KPIC, performance appears
  21. # to be virtually identical... But try to re-configure with shared
  22. # library support... Aha! Intel compiler "suddenly" lags behind by 30%
  23. # [on P4, more on others]:-) And if compared to position-independent
  24. # code generated by GNU C, this code performs *more* than *twice* as
  25. # fast! Yes, all this buzz about PIC means that unlike other hand-
  26. # coded implementations, this one was explicitly designed to be safe
  27. # to use even in shared library context... This also means that this
  28. # code isn't necessarily absolutely fastest "ever," because in order
  29. # to achieve position independence an extra register has to be
  30. # off-loaded to stack, which affects the benchmark result.
  31. #
  32. # Special note about instruction choice. Do you recall RC4_INT code
  33. # performing poorly on P4? It might be the time to figure out why.
  34. # RC4_INT code implies effective address calculations in base+offset*4
  35. # form. Trouble is that it seems that offset scaling turned to be
  36. # critical path... At least eliminating scaling resulted in 2.8x RC4
  37. # performance improvement [as you might recall]. As AES code is hungry
  38. # for scaling too, I [try to] avoid the latter by favoring off-by-2
  39. # shifts and masking the result with 0xFF<<2 instead of "boring" 0xFF.
  40. #
  41. # As was shown by Dean Gaudet, the above note turned out to be
  42. # void. Performance improvement with off-by-2 shifts was observed on
  43. # intermediate implementation, which was spilling yet another register
  44. # to stack... Final offset*4 code below runs just a tad faster on P4,
  45. # but exhibits up to 10% improvement on other cores.
  46. #
  47. # Second version is "monolithic" replacement for aes_core.c, which in
  48. # addition to AES_[de|en]crypt implements AES_set_[de|en]cryption_key.
  49. # This made it possible to implement little-endian variant of the
  50. # algorithm without modifying the base C code. Motivating factor for
  51. # the undertaken effort was that it appeared that in tight IA-32
  52. # register window little-endian flavor could achieve slightly higher
  53. # Instruction Level Parallelism, and it indeed resulted in up to 15%
  54. # better performance on most recent µ-archs...
  55. #
  56. # Third version adds AES_cbc_encrypt implementation, which resulted in
  57. # up to 40% performance improvement of CBC benchmark results. 40% was
  58. # observed on P4 core, where "overall" improvement coefficient, i.e. if
  59. # compared to PIC generated by GCC and in CBC mode, was observed to be
  60. # as large as 4x:-) CBC performance is virtually identical to ECB now
  61. # and on some platforms even better, e.g. 17.6 "small" cycles/byte on
  62. # Opteron, because certain function prologues and epilogues are
  63. # effectively taken out of the loop...
  64. #
  65. # Version 3.2 implements compressed tables and prefetch of these tables
  66. # in CBC[!] mode. Former means that 3/4 of table references are now
  67. # misaligned, which unfortunately has negative impact on elder IA-32
  68. # implementations, Pentium suffered 30% penalty, PIII - 10%.
  69. #
  70. # Version 3.3 avoids L1 cache aliasing between stack frame and
  71. # S-boxes, and 3.4 - L1 cache aliasing even between key schedule. The
  72. # latter is achieved by copying the key schedule to controlled place in
  73. # stack. This unfortunately has rather strong impact on small block CBC
  74. # performance, ~2x deterioration on 16-byte block if compared to 3.3.
  75. #
  76. # Version 3.5 checks if there is L1 cache aliasing between user-supplied
  77. # key schedule and S-boxes and abstains from copying the former if
  78. # there is no. This allows end-user to consciously retain small block
  79. # performance by aligning key schedule in specific manner.
  80. #
  81. # Version 3.6 compresses Td4 to 256 bytes and prefetches it in ECB.
  82. #
  83. # Current ECB performance numbers for 128-bit key in CPU cycles per
  84. # processed byte [measure commonly used by AES benchmarkers] are:
  85. #
  86. # small footprint fully unrolled
  87. # P4 24 22
  88. # AMD K8 20 19
  89. # PIII 25 23
  90. # Pentium 81 78
  91. #
  92. # Version 3.7 reimplements outer rounds as "compact." Meaning that
  93. # first and last rounds reference compact 256 bytes S-box. This means
  94. # that first round consumes a lot more CPU cycles and that encrypt
  95. # and decrypt performance becomes asymmetric. Encrypt performance
  96. # drops by 10-12%, while decrypt - by 20-25%:-( 256 bytes S-box is
  97. # aggressively pre-fetched.
  98. #
  99. # Version 4.0 effectively rolls back to 3.6 and instead implements
  100. # additional set of functions, _[x86|sse]_AES_[en|de]crypt_compact,
  101. # which use exclusively 256 byte S-box. These functions are to be
  102. # called in modes not concealing plain text, such as ECB, or when
  103. # we're asked to process smaller amount of data [or unconditionally
  104. # on hyper-threading CPU]. Currently it's called unconditionally from
  105. # AES_[en|de]crypt, which affects all modes, but CBC. CBC routine
  106. # still needs to be modified to switch between slower and faster
  107. # mode when appropriate... But in either case benchmark landscape
  108. # changes dramatically and below numbers are CPU cycles per processed
  109. # byte for 128-bit key.
  110. #
  111. # ECB encrypt ECB decrypt CBC large chunk
  112. # P4 52[54] 83[95] 23
  113. # AMD K8 46[41] 66[70] 18
  114. # PIII 41[50] 60[77] 24
  115. # Core 2 31[36] 45[64] 18.5
  116. # Atom 76[100] 96[138] 60
  117. # Pentium 115 150 77
  118. #
  119. # Version 4.1 switches to compact S-box even in key schedule setup.
  120. #
  121. # Version 4.2 prefetches compact S-box in every SSE round or in other
  122. # words every cache-line is *guaranteed* to be accessed within ~50
  123. # cycles window. Why just SSE? Because it's needed on hyper-threading
  124. # CPU! Which is also why it's prefetched with 64 byte stride. Best
  125. # part is that it has no negative effect on performance:-)
  126. #
  127. # Version 4.3 implements switch between compact and non-compact block
  128. # functions in AES_cbc_encrypt depending on how much data was asked
  129. # to be processed in one stroke.
  130. #
  131. ######################################################################
  132. # Timing attacks are classified in two classes: synchronous when
  133. # attacker consciously initiates cryptographic operation and collects
  134. # timing data of various character afterwards, and asynchronous when
  135. # malicious code is executed on same CPU simultaneously with AES,
  136. # instruments itself and performs statistical analysis of this data.
  137. #
  138. # As far as synchronous attacks go the root to the AES timing
  139. # vulnerability is twofold. Firstly, of 256 S-box elements at most 160
  140. # are referred to in single 128-bit block operation. Well, in C
  141. # implementation with 4 distinct tables it's actually as little as 40
  142. # references per 256 elements table, but anyway... Secondly, even
  143. # though S-box elements are clustered into smaller amount of cache-
  144. # lines, smaller than 160 and even 40, it turned out that for certain
  145. # plain-text pattern[s] or simply put chosen plain-text and given key
  146. # few cache-lines remain unaccessed during block operation. Now, if
  147. # attacker can figure out this access pattern, he can deduct the key
  148. # [or at least part of it]. The natural way to mitigate this kind of
  149. # attacks is to minimize the amount of cache-lines in S-box and/or
  150. # prefetch them to ensure that every one is accessed for more uniform
  151. # timing. But note that *if* plain-text was concealed in such way that
  152. # input to block function is distributed *uniformly*, then attack
  153. # wouldn't apply. Now note that some encryption modes, most notably
  154. # CBC, do mask the plain-text in this exact way [secure cipher output
  155. # is distributed uniformly]. Yes, one still might find input that
  156. # would reveal the information about given key, but if amount of
  157. # candidate inputs to be tried is larger than amount of possible key
  158. # combinations then attack becomes infeasible. This is why revised
  159. # AES_cbc_encrypt "dares" to switch to larger S-box when larger chunk
  160. # of data is to be processed in one stroke. The current size limit of
  161. # 512 bytes is chosen to provide same [diminishingly low] probability
  162. # for cache-line to remain untouched in large chunk operation with
  163. # large S-box as for single block operation with compact S-box and
  164. # surely needs more careful consideration...
  165. #
  166. # As for asynchronous attacks. There are two flavours: attacker code
  167. # being interleaved with AES on hyper-threading CPU at *instruction*
  168. # level, and two processes time sharing single core. As for latter.
  169. # Two vectors. 1. Given that attacker process has higher priority,
  170. # yield execution to process performing AES just before timer fires
  171. # off the scheduler, immediately regain control of CPU and analyze the
  172. # cache state. For this attack to be efficient attacker would have to
  173. # effectively slow down the operation by several *orders* of magnitude,
  174. # by ratio of time slice to duration of handful of AES rounds, which
  175. # unlikely to remain unnoticed. Not to mention that this also means
  176. # that he would spend correspondingly more time to collect enough
  177. # statistical data to mount the attack. It's probably appropriate to
  178. # say that if adversary reckons that this attack is beneficial and
  179. # risks to be noticed, you probably have larger problems having him
  180. # mere opportunity. In other words suggested code design expects you
  181. # to preclude/mitigate this attack by overall system security design.
  182. # 2. Attacker manages to make his code interrupt driven. In order for
  183. # this kind of attack to be feasible, interrupt rate has to be high
  184. # enough, again comparable to duration of handful of AES rounds. But
  185. # is there interrupt source of such rate? Hardly, not even 1Gbps NIC
  186. # generates interrupts at such raging rate...
  187. #
  188. # And now back to the former, hyper-threading CPU or more specifically
  189. # Intel P4. Recall that asynchronous attack implies that malicious
  190. # code instruments itself. And naturally instrumentation granularity
  191. # has be noticeably lower than duration of codepath accessing S-box.
  192. # Given that all cache-lines are accessed during that time that is.
  193. # Current implementation accesses *all* cache-lines within ~50 cycles
  194. # window, which is actually *less* than RDTSC latency on Intel P4!
  195. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  196. push(@INC,"${dir}","${dir}../../perlasm");
  197. require "x86asm.pl";
  198. $output = pop;
  199. open OUT,">$output";
  200. *STDOUT=*OUT;
  201. &asm_init($ARGV[0],$x86only = $ARGV[$#ARGV] eq "386");
  202. &static_label("AES_Te");
  203. &static_label("AES_Td");
  204. $s0="eax";
  205. $s1="ebx";
  206. $s2="ecx";
  207. $s3="edx";
  208. $key="edi";
  209. $acc="esi";
  210. $tbl="ebp";
  211. # stack frame layout in _[x86|sse]_AES_* routines, frame is allocated
  212. # by caller
  213. $__ra=&DWP(0,"esp"); # return address
  214. $__s0=&DWP(4,"esp"); # s0 backing store
  215. $__s1=&DWP(8,"esp"); # s1 backing store
  216. $__s2=&DWP(12,"esp"); # s2 backing store
  217. $__s3=&DWP(16,"esp"); # s3 backing store
  218. $__key=&DWP(20,"esp"); # pointer to key schedule
  219. $__end=&DWP(24,"esp"); # pointer to end of key schedule
  220. $__tbl=&DWP(28,"esp"); # %ebp backing store
  221. # stack frame layout in AES_[en|crypt] routines, which differs from
  222. # above by 4 and overlaps by %ebp backing store
  223. $_tbl=&DWP(24,"esp");
  224. $_esp=&DWP(28,"esp");
  225. sub _data_word() { my $i; while(defined($i=shift)) { &data_word($i,$i); } }
  226. $speed_limit=512; # chunks smaller than $speed_limit are
  227. # processed with compact routine in CBC mode
  228. $small_footprint=1; # $small_footprint=1 code is ~5% slower [on
  229. # recent µ-archs], but ~5 times smaller!
  230. # I favor compact code to minimize cache
  231. # contention and in hope to "collect" 5% back
  232. # in real-life applications...
  233. $vertical_spin=0; # shift "vertically" defaults to 0, because of
  234. # its proof-of-concept status...
  235. # Note that there is no decvert(), as well as last encryption round is
  236. # performed with "horizontal" shifts. This is because this "vertical"
  237. # implementation [one which groups shifts on a given $s[i] to form a
  238. # "column," unlike "horizontal" one, which groups shifts on different
  239. # $s[i] to form a "row"] is work in progress. It was observed to run
  240. # few percents faster on Intel cores, but not AMD. On AMD K8 core it's
  241. # whole 12% slower:-( So we face a trade-off... Shall it be resolved
  242. # some day? Till then the code is considered experimental and by
  243. # default remains dormant...
  244. sub encvert()
  245. { my ($te,@s) = @_;
  246. my ($v0,$v1) = ($acc,$key);
  247. &mov ($v0,$s[3]); # copy s3
  248. &mov (&DWP(4,"esp"),$s[2]); # save s2
  249. &mov ($v1,$s[0]); # copy s0
  250. &mov (&DWP(8,"esp"),$s[1]); # save s1
  251. &movz ($s[2],&HB($s[0]));
  252. &and ($s[0],0xFF);
  253. &mov ($s[0],&DWP(0,$te,$s[0],8)); # s0>>0
  254. &shr ($v1,16);
  255. &mov ($s[3],&DWP(3,$te,$s[2],8)); # s0>>8
  256. &movz ($s[1],&HB($v1));
  257. &and ($v1,0xFF);
  258. &mov ($s[2],&DWP(2,$te,$v1,8)); # s0>>16
  259. &mov ($v1,$v0);
  260. &mov ($s[1],&DWP(1,$te,$s[1],8)); # s0>>24
  261. &and ($v0,0xFF);
  262. &xor ($s[3],&DWP(0,$te,$v0,8)); # s3>>0
  263. &movz ($v0,&HB($v1));
  264. &shr ($v1,16);
  265. &xor ($s[2],&DWP(3,$te,$v0,8)); # s3>>8
  266. &movz ($v0,&HB($v1));
  267. &and ($v1,0xFF);
  268. &xor ($s[1],&DWP(2,$te,$v1,8)); # s3>>16
  269. &mov ($v1,&DWP(4,"esp")); # restore s2
  270. &xor ($s[0],&DWP(1,$te,$v0,8)); # s3>>24
  271. &mov ($v0,$v1);
  272. &and ($v1,0xFF);
  273. &xor ($s[2],&DWP(0,$te,$v1,8)); # s2>>0
  274. &movz ($v1,&HB($v0));
  275. &shr ($v0,16);
  276. &xor ($s[1],&DWP(3,$te,$v1,8)); # s2>>8
  277. &movz ($v1,&HB($v0));
  278. &and ($v0,0xFF);
  279. &xor ($s[0],&DWP(2,$te,$v0,8)); # s2>>16
  280. &mov ($v0,&DWP(8,"esp")); # restore s1
  281. &xor ($s[3],&DWP(1,$te,$v1,8)); # s2>>24
  282. &mov ($v1,$v0);
  283. &and ($v0,0xFF);
  284. &xor ($s[1],&DWP(0,$te,$v0,8)); # s1>>0
  285. &movz ($v0,&HB($v1));
  286. &shr ($v1,16);
  287. &xor ($s[0],&DWP(3,$te,$v0,8)); # s1>>8
  288. &movz ($v0,&HB($v1));
  289. &and ($v1,0xFF);
  290. &xor ($s[3],&DWP(2,$te,$v1,8)); # s1>>16
  291. &mov ($key,$__key); # reincarnate v1 as key
  292. &xor ($s[2],&DWP(1,$te,$v0,8)); # s1>>24
  293. }
  294. # Another experimental routine, which features "horizontal spin," but
  295. # eliminates one reference to stack. Strangely enough runs slower...
  296. sub enchoriz()
  297. { my ($v0,$v1) = ($key,$acc);
  298. &movz ($v0,&LB($s0)); # 3, 2, 1, 0*
  299. &rotr ($s2,8); # 8,11,10, 9
  300. &mov ($v1,&DWP(0,$te,$v0,8)); # 0
  301. &movz ($v0,&HB($s1)); # 7, 6, 5*, 4
  302. &rotr ($s3,16); # 13,12,15,14
  303. &xor ($v1,&DWP(3,$te,$v0,8)); # 5
  304. &movz ($v0,&HB($s2)); # 8,11,10*, 9
  305. &rotr ($s0,16); # 1, 0, 3, 2
  306. &xor ($v1,&DWP(2,$te,$v0,8)); # 10
  307. &movz ($v0,&HB($s3)); # 13,12,15*,14
  308. &xor ($v1,&DWP(1,$te,$v0,8)); # 15, t[0] collected
  309. &mov ($__s0,$v1); # t[0] saved
  310. &movz ($v0,&LB($s1)); # 7, 6, 5, 4*
  311. &shr ($s1,16); # -, -, 7, 6
  312. &mov ($v1,&DWP(0,$te,$v0,8)); # 4
  313. &movz ($v0,&LB($s3)); # 13,12,15,14*
  314. &xor ($v1,&DWP(2,$te,$v0,8)); # 14
  315. &movz ($v0,&HB($s0)); # 1, 0, 3*, 2
  316. &and ($s3,0xffff0000); # 13,12, -, -
  317. &xor ($v1,&DWP(1,$te,$v0,8)); # 3
  318. &movz ($v0,&LB($s2)); # 8,11,10, 9*
  319. &or ($s3,$s1); # 13,12, 7, 6
  320. &xor ($v1,&DWP(3,$te,$v0,8)); # 9, t[1] collected
  321. &mov ($s1,$v1); # s[1]=t[1]
  322. &movz ($v0,&LB($s0)); # 1, 0, 3, 2*
  323. &shr ($s2,16); # -, -, 8,11
  324. &mov ($v1,&DWP(2,$te,$v0,8)); # 2
  325. &movz ($v0,&HB($s3)); # 13,12, 7*, 6
  326. &xor ($v1,&DWP(1,$te,$v0,8)); # 7
  327. &movz ($v0,&HB($s2)); # -, -, 8*,11
  328. &xor ($v1,&DWP(0,$te,$v0,8)); # 8
  329. &mov ($v0,$s3);
  330. &shr ($v0,24); # 13
  331. &xor ($v1,&DWP(3,$te,$v0,8)); # 13, t[2] collected
  332. &movz ($v0,&LB($s2)); # -, -, 8,11*
  333. &shr ($s0,24); # 1*
  334. &mov ($s2,&DWP(1,$te,$v0,8)); # 11
  335. &xor ($s2,&DWP(3,$te,$s0,8)); # 1
  336. &mov ($s0,$__s0); # s[0]=t[0]
  337. &movz ($v0,&LB($s3)); # 13,12, 7, 6*
  338. &shr ($s3,16); # , ,13,12
  339. &xor ($s2,&DWP(2,$te,$v0,8)); # 6
  340. &mov ($key,$__key); # reincarnate v0 as key
  341. &and ($s3,0xff); # , ,13,12*
  342. &mov ($s3,&DWP(0,$te,$s3,8)); # 12
  343. &xor ($s3,$s2); # s[2]=t[3] collected
  344. &mov ($s2,$v1); # s[2]=t[2]
  345. }
  346. # More experimental code... SSE one... Even though this one eliminates
  347. # *all* references to stack, it's not faster...
  348. sub sse_encbody()
  349. {
  350. &movz ($acc,&LB("eax")); # 0
  351. &mov ("ecx",&DWP(0,$tbl,$acc,8)); # 0
  352. &pshufw ("mm2","mm0",0x0d); # 7, 6, 3, 2
  353. &movz ("edx",&HB("eax")); # 1
  354. &mov ("edx",&DWP(3,$tbl,"edx",8)); # 1
  355. &shr ("eax",16); # 5, 4
  356. &movz ($acc,&LB("ebx")); # 10
  357. &xor ("ecx",&DWP(2,$tbl,$acc,8)); # 10
  358. &pshufw ("mm6","mm4",0x08); # 13,12, 9, 8
  359. &movz ($acc,&HB("ebx")); # 11
  360. &xor ("edx",&DWP(1,$tbl,$acc,8)); # 11
  361. &shr ("ebx",16); # 15,14
  362. &movz ($acc,&HB("eax")); # 5
  363. &xor ("ecx",&DWP(3,$tbl,$acc,8)); # 5
  364. &movq ("mm3",QWP(16,$key));
  365. &movz ($acc,&HB("ebx")); # 15
  366. &xor ("ecx",&DWP(1,$tbl,$acc,8)); # 15
  367. &movd ("mm0","ecx"); # t[0] collected
  368. &movz ($acc,&LB("eax")); # 4
  369. &mov ("ecx",&DWP(0,$tbl,$acc,8)); # 4
  370. &movd ("eax","mm2"); # 7, 6, 3, 2
  371. &movz ($acc,&LB("ebx")); # 14
  372. &xor ("ecx",&DWP(2,$tbl,$acc,8)); # 14
  373. &movd ("ebx","mm6"); # 13,12, 9, 8
  374. &movz ($acc,&HB("eax")); # 3
  375. &xor ("ecx",&DWP(1,$tbl,$acc,8)); # 3
  376. &movz ($acc,&HB("ebx")); # 9
  377. &xor ("ecx",&DWP(3,$tbl,$acc,8)); # 9
  378. &movd ("mm1","ecx"); # t[1] collected
  379. &movz ($acc,&LB("eax")); # 2
  380. &mov ("ecx",&DWP(2,$tbl,$acc,8)); # 2
  381. &shr ("eax",16); # 7, 6
  382. &punpckldq ("mm0","mm1"); # t[0,1] collected
  383. &movz ($acc,&LB("ebx")); # 8
  384. &xor ("ecx",&DWP(0,$tbl,$acc,8)); # 8
  385. &shr ("ebx",16); # 13,12
  386. &movz ($acc,&HB("eax")); # 7
  387. &xor ("ecx",&DWP(1,$tbl,$acc,8)); # 7
  388. &pxor ("mm0","mm3");
  389. &movz ("eax",&LB("eax")); # 6
  390. &xor ("edx",&DWP(2,$tbl,"eax",8)); # 6
  391. &pshufw ("mm1","mm0",0x08); # 5, 4, 1, 0
  392. &movz ($acc,&HB("ebx")); # 13
  393. &xor ("ecx",&DWP(3,$tbl,$acc,8)); # 13
  394. &xor ("ecx",&DWP(24,$key)); # t[2]
  395. &movd ("mm4","ecx"); # t[2] collected
  396. &movz ("ebx",&LB("ebx")); # 12
  397. &xor ("edx",&DWP(0,$tbl,"ebx",8)); # 12
  398. &shr ("ecx",16);
  399. &movd ("eax","mm1"); # 5, 4, 1, 0
  400. &mov ("ebx",&DWP(28,$key)); # t[3]
  401. &xor ("ebx","edx");
  402. &movd ("mm5","ebx"); # t[3] collected
  403. &and ("ebx",0xffff0000);
  404. &or ("ebx","ecx");
  405. &punpckldq ("mm4","mm5"); # t[2,3] collected
  406. }
  407. ######################################################################
  408. # "Compact" block function
  409. ######################################################################
  410. sub enccompact()
  411. { my $Fn = \&mov;
  412. while ($#_>5) { pop(@_); $Fn=sub{}; }
  413. my ($i,$te,@s)=@_;
  414. my $tmp = $key;
  415. my $out = $i==3?$s[0]:$acc;
  416. # $Fn is used in first compact round and its purpose is to
  417. # void restoration of some values from stack, so that after
  418. # 4xenccompact with extra argument $key value is left there...
  419. if ($i==3) { &$Fn ($key,$__key); }##%edx
  420. else { &mov ($out,$s[0]); }
  421. &and ($out,0xFF);
  422. if ($i==1) { &shr ($s[0],16); }#%ebx[1]
  423. if ($i==2) { &shr ($s[0],24); }#%ecx[2]
  424. &movz ($out,&BP(-128,$te,$out,1));
  425. if ($i==3) { $tmp=$s[1]; }##%eax
  426. &movz ($tmp,&HB($s[1]));
  427. &movz ($tmp,&BP(-128,$te,$tmp,1));
  428. &shl ($tmp,8);
  429. &xor ($out,$tmp);
  430. if ($i==3) { $tmp=$s[2]; &mov ($s[1],$__s0); }##%ebx
  431. else { &mov ($tmp,$s[2]);
  432. &shr ($tmp,16); }
  433. if ($i==2) { &and ($s[1],0xFF); }#%edx[2]
  434. &and ($tmp,0xFF);
  435. &movz ($tmp,&BP(-128,$te,$tmp,1));
  436. &shl ($tmp,16);
  437. &xor ($out,$tmp);
  438. if ($i==3) { $tmp=$s[3]; &mov ($s[2],$__s1); }##%ecx
  439. elsif($i==2){ &movz ($tmp,&HB($s[3])); }#%ebx[2]
  440. else { &mov ($tmp,$s[3]);
  441. &shr ($tmp,24); }
  442. &movz ($tmp,&BP(-128,$te,$tmp,1));
  443. &shl ($tmp,24);
  444. &xor ($out,$tmp);
  445. if ($i<2) { &mov (&DWP(4+4*$i,"esp"),$out); }
  446. if ($i==3) { &mov ($s[3],$acc); }
  447. &comment();
  448. }
  449. sub enctransform()
  450. { my @s = ($s0,$s1,$s2,$s3);
  451. my $i = shift;
  452. my $tmp = $tbl;
  453. my $r2 = $key ;
  454. &and ($tmp,$s[$i]);
  455. &lea ($r2,&DWP(0,$s[$i],$s[$i]));
  456. &mov ($acc,$tmp);
  457. &shr ($tmp,7);
  458. &and ($r2,0xfefefefe);
  459. &sub ($acc,$tmp);
  460. &mov ($tmp,$s[$i]);
  461. &and ($acc,0x1b1b1b1b);
  462. &rotr ($tmp,16);
  463. &xor ($acc,$r2); # r2
  464. &mov ($r2,$s[$i]);
  465. &xor ($s[$i],$acc); # r0 ^ r2
  466. &rotr ($r2,16+8);
  467. &xor ($acc,$tmp);
  468. &rotl ($s[$i],24);
  469. &xor ($acc,$r2);
  470. &mov ($tmp,0x80808080) if ($i!=1);
  471. &xor ($s[$i],$acc); # ROTATE(r2^r0,24) ^ r2
  472. }
  473. &function_begin_B("_x86_AES_encrypt_compact");
  474. # note that caller is expected to allocate stack frame for me!
  475. &mov ($__key,$key); # save key
  476. &xor ($s0,&DWP(0,$key)); # xor with key
  477. &xor ($s1,&DWP(4,$key));
  478. &xor ($s2,&DWP(8,$key));
  479. &xor ($s3,&DWP(12,$key));
  480. &mov ($acc,&DWP(240,$key)); # load key->rounds
  481. &lea ($acc,&DWP(-2,$acc,$acc));
  482. &lea ($acc,&DWP(0,$key,$acc,8));
  483. &mov ($__end,$acc); # end of key schedule
  484. # prefetch Te4
  485. &mov ($key,&DWP(0-128,$tbl));
  486. &mov ($acc,&DWP(32-128,$tbl));
  487. &mov ($key,&DWP(64-128,$tbl));
  488. &mov ($acc,&DWP(96-128,$tbl));
  489. &mov ($key,&DWP(128-128,$tbl));
  490. &mov ($acc,&DWP(160-128,$tbl));
  491. &mov ($key,&DWP(192-128,$tbl));
  492. &mov ($acc,&DWP(224-128,$tbl));
  493. &set_label("loop",16);
  494. &enccompact(0,$tbl,$s0,$s1,$s2,$s3,1);
  495. &enccompact(1,$tbl,$s1,$s2,$s3,$s0,1);
  496. &enccompact(2,$tbl,$s2,$s3,$s0,$s1,1);
  497. &enccompact(3,$tbl,$s3,$s0,$s1,$s2,1);
  498. &mov ($tbl,0x80808080);
  499. &enctransform(2);
  500. &enctransform(3);
  501. &enctransform(0);
  502. &enctransform(1);
  503. &mov ($key,$__key);
  504. &mov ($tbl,$__tbl);
  505. &add ($key,16); # advance rd_key
  506. &xor ($s0,&DWP(0,$key));
  507. &xor ($s1,&DWP(4,$key));
  508. &xor ($s2,&DWP(8,$key));
  509. &xor ($s3,&DWP(12,$key));
  510. &cmp ($key,$__end);
  511. &mov ($__key,$key);
  512. &jb (&label("loop"));
  513. &enccompact(0,$tbl,$s0,$s1,$s2,$s3);
  514. &enccompact(1,$tbl,$s1,$s2,$s3,$s0);
  515. &enccompact(2,$tbl,$s2,$s3,$s0,$s1);
  516. &enccompact(3,$tbl,$s3,$s0,$s1,$s2);
  517. &xor ($s0,&DWP(16,$key));
  518. &xor ($s1,&DWP(20,$key));
  519. &xor ($s2,&DWP(24,$key));
  520. &xor ($s3,&DWP(28,$key));
  521. &ret ();
  522. &function_end_B("_x86_AES_encrypt_compact");
  523. ######################################################################
  524. # "Compact" SSE block function.
  525. ######################################################################
  526. #
  527. # Performance is not actually extraordinary in comparison to pure
  528. # x86 code. In particular encrypt performance is virtually the same.
  529. # Decrypt performance on the other hand is 15-20% better on newer
  530. # µ-archs [but we're thankful for *any* improvement here], and ~50%
  531. # better on PIII:-) And additionally on the pros side this code
  532. # eliminates redundant references to stack and thus relieves/
  533. # minimizes the pressure on the memory bus.
  534. #
  535. # MMX register layout lsb
  536. # +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  537. # | mm4 | mm0 |
  538. # +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  539. # | s3 | s2 | s1 | s0 |
  540. # +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  541. # |15|14|13|12|11|10| 9| 8| 7| 6| 5| 4| 3| 2| 1| 0|
  542. # +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  543. #
  544. # Indexes translate as s[N/4]>>(8*(N%4)), e.g. 5 means s1>>8.
  545. # In this terms encryption and decryption "compact" permutation
  546. # matrices can be depicted as following:
  547. #
  548. # encryption lsb # decryption lsb
  549. # +----++----+----+----+----+ # +----++----+----+----+----+
  550. # | t0 || 15 | 10 | 5 | 0 | # | t0 || 7 | 10 | 13 | 0 |
  551. # +----++----+----+----+----+ # +----++----+----+----+----+
  552. # | t1 || 3 | 14 | 9 | 4 | # | t1 || 11 | 14 | 1 | 4 |
  553. # +----++----+----+----+----+ # +----++----+----+----+----+
  554. # | t2 || 7 | 2 | 13 | 8 | # | t2 || 15 | 2 | 5 | 8 |
  555. # +----++----+----+----+----+ # +----++----+----+----+----+
  556. # | t3 || 11 | 6 | 1 | 12 | # | t3 || 3 | 6 | 9 | 12 |
  557. # +----++----+----+----+----+ # +----++----+----+----+----+
  558. #
  559. ######################################################################
  560. # Why not xmm registers? Short answer. It was actually tested and
  561. # was not any faster, but *contrary*, most notably on Intel CPUs.
  562. # Longer answer. Main advantage of using mm registers is that movd
  563. # latency is lower, especially on Intel P4. While arithmetic
  564. # instructions are twice as many, they can be scheduled every cycle
  565. # and not every second one when they are operating on xmm register,
  566. # so that "arithmetic throughput" remains virtually the same. And
  567. # finally the code can be executed even on elder SSE-only CPUs:-)
  568. sub sse_enccompact()
  569. {
  570. &pshufw ("mm1","mm0",0x08); # 5, 4, 1, 0
  571. &pshufw ("mm5","mm4",0x0d); # 15,14,11,10
  572. &movd ("eax","mm1"); # 5, 4, 1, 0
  573. &movd ("ebx","mm5"); # 15,14,11,10
  574. &mov ($__key,$key);
  575. &movz ($acc,&LB("eax")); # 0
  576. &movz ("edx",&HB("eax")); # 1
  577. &pshufw ("mm2","mm0",0x0d); # 7, 6, 3, 2
  578. &movz ("ecx",&BP(-128,$tbl,$acc,1)); # 0
  579. &movz ($key,&LB("ebx")); # 10
  580. &movz ("edx",&BP(-128,$tbl,"edx",1)); # 1
  581. &shr ("eax",16); # 5, 4
  582. &shl ("edx",8); # 1
  583. &movz ($acc,&BP(-128,$tbl,$key,1)); # 10
  584. &movz ($key,&HB("ebx")); # 11
  585. &shl ($acc,16); # 10
  586. &pshufw ("mm6","mm4",0x08); # 13,12, 9, 8
  587. &or ("ecx",$acc); # 10
  588. &movz ($acc,&BP(-128,$tbl,$key,1)); # 11
  589. &movz ($key,&HB("eax")); # 5
  590. &shl ($acc,24); # 11
  591. &shr ("ebx",16); # 15,14
  592. &or ("edx",$acc); # 11
  593. &movz ($acc,&BP(-128,$tbl,$key,1)); # 5
  594. &movz ($key,&HB("ebx")); # 15
  595. &shl ($acc,8); # 5
  596. &or ("ecx",$acc); # 5
  597. &movz ($acc,&BP(-128,$tbl,$key,1)); # 15
  598. &movz ($key,&LB("eax")); # 4
  599. &shl ($acc,24); # 15
  600. &or ("ecx",$acc); # 15
  601. &movz ($acc,&BP(-128,$tbl,$key,1)); # 4
  602. &movz ($key,&LB("ebx")); # 14
  603. &movd ("eax","mm2"); # 7, 6, 3, 2
  604. &movd ("mm0","ecx"); # t[0] collected
  605. &movz ("ecx",&BP(-128,$tbl,$key,1)); # 14
  606. &movz ($key,&HB("eax")); # 3
  607. &shl ("ecx",16); # 14
  608. &movd ("ebx","mm6"); # 13,12, 9, 8
  609. &or ("ecx",$acc); # 14
  610. &movz ($acc,&BP(-128,$tbl,$key,1)); # 3
  611. &movz ($key,&HB("ebx")); # 9
  612. &shl ($acc,24); # 3
  613. &or ("ecx",$acc); # 3
  614. &movz ($acc,&BP(-128,$tbl,$key,1)); # 9
  615. &movz ($key,&LB("ebx")); # 8
  616. &shl ($acc,8); # 9
  617. &shr ("ebx",16); # 13,12
  618. &or ("ecx",$acc); # 9
  619. &movz ($acc,&BP(-128,$tbl,$key,1)); # 8
  620. &movz ($key,&LB("eax")); # 2
  621. &shr ("eax",16); # 7, 6
  622. &movd ("mm1","ecx"); # t[1] collected
  623. &movz ("ecx",&BP(-128,$tbl,$key,1)); # 2
  624. &movz ($key,&HB("eax")); # 7
  625. &shl ("ecx",16); # 2
  626. &and ("eax",0xff); # 6
  627. &or ("ecx",$acc); # 2
  628. &punpckldq ("mm0","mm1"); # t[0,1] collected
  629. &movz ($acc,&BP(-128,$tbl,$key,1)); # 7
  630. &movz ($key,&HB("ebx")); # 13
  631. &shl ($acc,24); # 7
  632. &and ("ebx",0xff); # 12
  633. &movz ("eax",&BP(-128,$tbl,"eax",1)); # 6
  634. &or ("ecx",$acc); # 7
  635. &shl ("eax",16); # 6
  636. &movz ($acc,&BP(-128,$tbl,$key,1)); # 13
  637. &or ("edx","eax"); # 6
  638. &shl ($acc,8); # 13
  639. &movz ("ebx",&BP(-128,$tbl,"ebx",1)); # 12
  640. &or ("ecx",$acc); # 13
  641. &or ("edx","ebx"); # 12
  642. &mov ($key,$__key);
  643. &movd ("mm4","ecx"); # t[2] collected
  644. &movd ("mm5","edx"); # t[3] collected
  645. &punpckldq ("mm4","mm5"); # t[2,3] collected
  646. }
  647. if (!$x86only) {
  648. &function_begin_B("_sse_AES_encrypt_compact");
  649. &pxor ("mm0",&QWP(0,$key)); # 7, 6, 5, 4, 3, 2, 1, 0
  650. &pxor ("mm4",&QWP(8,$key)); # 15,14,13,12,11,10, 9, 8
  651. # note that caller is expected to allocate stack frame for me!
  652. &mov ($acc,&DWP(240,$key)); # load key->rounds
  653. &lea ($acc,&DWP(-2,$acc,$acc));
  654. &lea ($acc,&DWP(0,$key,$acc,8));
  655. &mov ($__end,$acc); # end of key schedule
  656. &mov ($s0,0x1b1b1b1b); # magic constant
  657. &mov (&DWP(8,"esp"),$s0);
  658. &mov (&DWP(12,"esp"),$s0);
  659. # prefetch Te4
  660. &mov ($s0,&DWP(0-128,$tbl));
  661. &mov ($s1,&DWP(32-128,$tbl));
  662. &mov ($s2,&DWP(64-128,$tbl));
  663. &mov ($s3,&DWP(96-128,$tbl));
  664. &mov ($s0,&DWP(128-128,$tbl));
  665. &mov ($s1,&DWP(160-128,$tbl));
  666. &mov ($s2,&DWP(192-128,$tbl));
  667. &mov ($s3,&DWP(224-128,$tbl));
  668. &set_label("loop",16);
  669. &sse_enccompact();
  670. &add ($key,16);
  671. &cmp ($key,$__end);
  672. &ja (&label("out"));
  673. &movq ("mm2",&QWP(8,"esp"));
  674. &pxor ("mm3","mm3"); &pxor ("mm7","mm7");
  675. &movq ("mm1","mm0"); &movq ("mm5","mm4"); # r0
  676. &pcmpgtb("mm3","mm0"); &pcmpgtb("mm7","mm4");
  677. &pand ("mm3","mm2"); &pand ("mm7","mm2");
  678. &pshufw ("mm2","mm0",0xb1); &pshufw ("mm6","mm4",0xb1);# ROTATE(r0,16)
  679. &paddb ("mm0","mm0"); &paddb ("mm4","mm4");
  680. &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # = r2
  681. &pshufw ("mm3","mm2",0xb1); &pshufw ("mm7","mm6",0xb1);# r0
  682. &pxor ("mm1","mm0"); &pxor ("mm5","mm4"); # r0^r2
  683. &pxor ("mm0","mm2"); &pxor ("mm4","mm6"); # ^= ROTATE(r0,16)
  684. &movq ("mm2","mm3"); &movq ("mm6","mm7");
  685. &pslld ("mm3",8); &pslld ("mm7",8);
  686. &psrld ("mm2",24); &psrld ("mm6",24);
  687. &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # ^= r0<<8
  688. &pxor ("mm0","mm2"); &pxor ("mm4","mm6"); # ^= r0>>24
  689. &movq ("mm3","mm1"); &movq ("mm7","mm5");
  690. &movq ("mm2",&QWP(0,$key)); &movq ("mm6",&QWP(8,$key));
  691. &psrld ("mm1",8); &psrld ("mm5",8);
  692. &mov ($s0,&DWP(0-128,$tbl));
  693. &pslld ("mm3",24); &pslld ("mm7",24);
  694. &mov ($s1,&DWP(64-128,$tbl));
  695. &pxor ("mm0","mm1"); &pxor ("mm4","mm5"); # ^= (r2^r0)<<8
  696. &mov ($s2,&DWP(128-128,$tbl));
  697. &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # ^= (r2^r0)>>24
  698. &mov ($s3,&DWP(192-128,$tbl));
  699. &pxor ("mm0","mm2"); &pxor ("mm4","mm6");
  700. &jmp (&label("loop"));
  701. &set_label("out",16);
  702. &pxor ("mm0",&QWP(0,$key));
  703. &pxor ("mm4",&QWP(8,$key));
  704. &ret ();
  705. &function_end_B("_sse_AES_encrypt_compact");
  706. }
  707. ######################################################################
  708. # Vanilla block function.
  709. ######################################################################
  710. sub encstep()
  711. { my ($i,$te,@s) = @_;
  712. my $tmp = $key;
  713. my $out = $i==3?$s[0]:$acc;
  714. # lines marked with #%e?x[i] denote "reordered" instructions...
  715. if ($i==3) { &mov ($key,$__key); }##%edx
  716. else { &mov ($out,$s[0]);
  717. &and ($out,0xFF); }
  718. if ($i==1) { &shr ($s[0],16); }#%ebx[1]
  719. if ($i==2) { &shr ($s[0],24); }#%ecx[2]
  720. &mov ($out,&DWP(0,$te,$out,8));
  721. if ($i==3) { $tmp=$s[1]; }##%eax
  722. &movz ($tmp,&HB($s[1]));
  723. &xor ($out,&DWP(3,$te,$tmp,8));
  724. if ($i==3) { $tmp=$s[2]; &mov ($s[1],$__s0); }##%ebx
  725. else { &mov ($tmp,$s[2]);
  726. &shr ($tmp,16); }
  727. if ($i==2) { &and ($s[1],0xFF); }#%edx[2]
  728. &and ($tmp,0xFF);
  729. &xor ($out,&DWP(2,$te,$tmp,8));
  730. if ($i==3) { $tmp=$s[3]; &mov ($s[2],$__s1); }##%ecx
  731. elsif($i==2){ &movz ($tmp,&HB($s[3])); }#%ebx[2]
  732. else { &mov ($tmp,$s[3]);
  733. &shr ($tmp,24) }
  734. &xor ($out,&DWP(1,$te,$tmp,8));
  735. if ($i<2) { &mov (&DWP(4+4*$i,"esp"),$out); }
  736. if ($i==3) { &mov ($s[3],$acc); }
  737. &comment();
  738. }
  739. sub enclast()
  740. { my ($i,$te,@s)=@_;
  741. my $tmp = $key;
  742. my $out = $i==3?$s[0]:$acc;
  743. if ($i==3) { &mov ($key,$__key); }##%edx
  744. else { &mov ($out,$s[0]); }
  745. &and ($out,0xFF);
  746. if ($i==1) { &shr ($s[0],16); }#%ebx[1]
  747. if ($i==2) { &shr ($s[0],24); }#%ecx[2]
  748. &mov ($out,&DWP(2,$te,$out,8));
  749. &and ($out,0x000000ff);
  750. if ($i==3) { $tmp=$s[1]; }##%eax
  751. &movz ($tmp,&HB($s[1]));
  752. &mov ($tmp,&DWP(0,$te,$tmp,8));
  753. &and ($tmp,0x0000ff00);
  754. &xor ($out,$tmp);
  755. if ($i==3) { $tmp=$s[2]; &mov ($s[1],$__s0); }##%ebx
  756. else { &mov ($tmp,$s[2]);
  757. &shr ($tmp,16); }
  758. if ($i==2) { &and ($s[1],0xFF); }#%edx[2]
  759. &and ($tmp,0xFF);
  760. &mov ($tmp,&DWP(0,$te,$tmp,8));
  761. &and ($tmp,0x00ff0000);
  762. &xor ($out,$tmp);
  763. if ($i==3) { $tmp=$s[3]; &mov ($s[2],$__s1); }##%ecx
  764. elsif($i==2){ &movz ($tmp,&HB($s[3])); }#%ebx[2]
  765. else { &mov ($tmp,$s[3]);
  766. &shr ($tmp,24); }
  767. &mov ($tmp,&DWP(2,$te,$tmp,8));
  768. &and ($tmp,0xff000000);
  769. &xor ($out,$tmp);
  770. if ($i<2) { &mov (&DWP(4+4*$i,"esp"),$out); }
  771. if ($i==3) { &mov ($s[3],$acc); }
  772. }
  773. &function_begin_B("_x86_AES_encrypt");
  774. if ($vertical_spin) {
  775. # I need high parts of volatile registers to be accessible...
  776. &exch ($s1="edi",$key="ebx");
  777. &mov ($s2="esi",$acc="ecx");
  778. }
  779. # note that caller is expected to allocate stack frame for me!
  780. &mov ($__key,$key); # save key
  781. &xor ($s0,&DWP(0,$key)); # xor with key
  782. &xor ($s1,&DWP(4,$key));
  783. &xor ($s2,&DWP(8,$key));
  784. &xor ($s3,&DWP(12,$key));
  785. &mov ($acc,&DWP(240,$key)); # load key->rounds
  786. if ($small_footprint) {
  787. &lea ($acc,&DWP(-2,$acc,$acc));
  788. &lea ($acc,&DWP(0,$key,$acc,8));
  789. &mov ($__end,$acc); # end of key schedule
  790. &set_label("loop",16);
  791. if ($vertical_spin) {
  792. &encvert($tbl,$s0,$s1,$s2,$s3);
  793. } else {
  794. &encstep(0,$tbl,$s0,$s1,$s2,$s3);
  795. &encstep(1,$tbl,$s1,$s2,$s3,$s0);
  796. &encstep(2,$tbl,$s2,$s3,$s0,$s1);
  797. &encstep(3,$tbl,$s3,$s0,$s1,$s2);
  798. }
  799. &add ($key,16); # advance rd_key
  800. &xor ($s0,&DWP(0,$key));
  801. &xor ($s1,&DWP(4,$key));
  802. &xor ($s2,&DWP(8,$key));
  803. &xor ($s3,&DWP(12,$key));
  804. &cmp ($key,$__end);
  805. &mov ($__key,$key);
  806. &jb (&label("loop"));
  807. }
  808. else {
  809. &cmp ($acc,10);
  810. &jle (&label("10rounds"));
  811. &cmp ($acc,12);
  812. &jle (&label("12rounds"));
  813. &set_label("14rounds",4);
  814. for ($i=1;$i<3;$i++) {
  815. if ($vertical_spin) {
  816. &encvert($tbl,$s0,$s1,$s2,$s3);
  817. } else {
  818. &encstep(0,$tbl,$s0,$s1,$s2,$s3);
  819. &encstep(1,$tbl,$s1,$s2,$s3,$s0);
  820. &encstep(2,$tbl,$s2,$s3,$s0,$s1);
  821. &encstep(3,$tbl,$s3,$s0,$s1,$s2);
  822. }
  823. &xor ($s0,&DWP(16*$i+0,$key));
  824. &xor ($s1,&DWP(16*$i+4,$key));
  825. &xor ($s2,&DWP(16*$i+8,$key));
  826. &xor ($s3,&DWP(16*$i+12,$key));
  827. }
  828. &add ($key,32);
  829. &mov ($__key,$key); # advance rd_key
  830. &set_label("12rounds",4);
  831. for ($i=1;$i<3;$i++) {
  832. if ($vertical_spin) {
  833. &encvert($tbl,$s0,$s1,$s2,$s3);
  834. } else {
  835. &encstep(0,$tbl,$s0,$s1,$s2,$s3);
  836. &encstep(1,$tbl,$s1,$s2,$s3,$s0);
  837. &encstep(2,$tbl,$s2,$s3,$s0,$s1);
  838. &encstep(3,$tbl,$s3,$s0,$s1,$s2);
  839. }
  840. &xor ($s0,&DWP(16*$i+0,$key));
  841. &xor ($s1,&DWP(16*$i+4,$key));
  842. &xor ($s2,&DWP(16*$i+8,$key));
  843. &xor ($s3,&DWP(16*$i+12,$key));
  844. }
  845. &add ($key,32);
  846. &mov ($__key,$key); # advance rd_key
  847. &set_label("10rounds",4);
  848. for ($i=1;$i<10;$i++) {
  849. if ($vertical_spin) {
  850. &encvert($tbl,$s0,$s1,$s2,$s3);
  851. } else {
  852. &encstep(0,$tbl,$s0,$s1,$s2,$s3);
  853. &encstep(1,$tbl,$s1,$s2,$s3,$s0);
  854. &encstep(2,$tbl,$s2,$s3,$s0,$s1);
  855. &encstep(3,$tbl,$s3,$s0,$s1,$s2);
  856. }
  857. &xor ($s0,&DWP(16*$i+0,$key));
  858. &xor ($s1,&DWP(16*$i+4,$key));
  859. &xor ($s2,&DWP(16*$i+8,$key));
  860. &xor ($s3,&DWP(16*$i+12,$key));
  861. }
  862. }
  863. if ($vertical_spin) {
  864. # "reincarnate" some registers for "horizontal" spin...
  865. &mov ($s1="ebx",$key="edi");
  866. &mov ($s2="ecx",$acc="esi");
  867. }
  868. &enclast(0,$tbl,$s0,$s1,$s2,$s3);
  869. &enclast(1,$tbl,$s1,$s2,$s3,$s0);
  870. &enclast(2,$tbl,$s2,$s3,$s0,$s1);
  871. &enclast(3,$tbl,$s3,$s0,$s1,$s2);
  872. &add ($key,$small_footprint?16:160);
  873. &xor ($s0,&DWP(0,$key));
  874. &xor ($s1,&DWP(4,$key));
  875. &xor ($s2,&DWP(8,$key));
  876. &xor ($s3,&DWP(12,$key));
  877. &ret ();
  878. &set_label("AES_Te",64); # Yes! I keep it in the code segment!
  879. &_data_word(0xa56363c6, 0x847c7cf8, 0x997777ee, 0x8d7b7bf6);
  880. &_data_word(0x0df2f2ff, 0xbd6b6bd6, 0xb16f6fde, 0x54c5c591);
  881. &_data_word(0x50303060, 0x03010102, 0xa96767ce, 0x7d2b2b56);
  882. &_data_word(0x19fefee7, 0x62d7d7b5, 0xe6abab4d, 0x9a7676ec);
  883. &_data_word(0x45caca8f, 0x9d82821f, 0x40c9c989, 0x877d7dfa);
  884. &_data_word(0x15fafaef, 0xeb5959b2, 0xc947478e, 0x0bf0f0fb);
  885. &_data_word(0xecadad41, 0x67d4d4b3, 0xfda2a25f, 0xeaafaf45);
  886. &_data_word(0xbf9c9c23, 0xf7a4a453, 0x967272e4, 0x5bc0c09b);
  887. &_data_word(0xc2b7b775, 0x1cfdfde1, 0xae93933d, 0x6a26264c);
  888. &_data_word(0x5a36366c, 0x413f3f7e, 0x02f7f7f5, 0x4fcccc83);
  889. &_data_word(0x5c343468, 0xf4a5a551, 0x34e5e5d1, 0x08f1f1f9);
  890. &_data_word(0x937171e2, 0x73d8d8ab, 0x53313162, 0x3f15152a);
  891. &_data_word(0x0c040408, 0x52c7c795, 0x65232346, 0x5ec3c39d);
  892. &_data_word(0x28181830, 0xa1969637, 0x0f05050a, 0xb59a9a2f);
  893. &_data_word(0x0907070e, 0x36121224, 0x9b80801b, 0x3de2e2df);
  894. &_data_word(0x26ebebcd, 0x6927274e, 0xcdb2b27f, 0x9f7575ea);
  895. &_data_word(0x1b090912, 0x9e83831d, 0x742c2c58, 0x2e1a1a34);
  896. &_data_word(0x2d1b1b36, 0xb26e6edc, 0xee5a5ab4, 0xfba0a05b);
  897. &_data_word(0xf65252a4, 0x4d3b3b76, 0x61d6d6b7, 0xceb3b37d);
  898. &_data_word(0x7b292952, 0x3ee3e3dd, 0x712f2f5e, 0x97848413);
  899. &_data_word(0xf55353a6, 0x68d1d1b9, 0x00000000, 0x2cededc1);
  900. &_data_word(0x60202040, 0x1ffcfce3, 0xc8b1b179, 0xed5b5bb6);
  901. &_data_word(0xbe6a6ad4, 0x46cbcb8d, 0xd9bebe67, 0x4b393972);
  902. &_data_word(0xde4a4a94, 0xd44c4c98, 0xe85858b0, 0x4acfcf85);
  903. &_data_word(0x6bd0d0bb, 0x2aefefc5, 0xe5aaaa4f, 0x16fbfbed);
  904. &_data_word(0xc5434386, 0xd74d4d9a, 0x55333366, 0x94858511);
  905. &_data_word(0xcf45458a, 0x10f9f9e9, 0x06020204, 0x817f7ffe);
  906. &_data_word(0xf05050a0, 0x443c3c78, 0xba9f9f25, 0xe3a8a84b);
  907. &_data_word(0xf35151a2, 0xfea3a35d, 0xc0404080, 0x8a8f8f05);
  908. &_data_word(0xad92923f, 0xbc9d9d21, 0x48383870, 0x04f5f5f1);
  909. &_data_word(0xdfbcbc63, 0xc1b6b677, 0x75dadaaf, 0x63212142);
  910. &_data_word(0x30101020, 0x1affffe5, 0x0ef3f3fd, 0x6dd2d2bf);
  911. &_data_word(0x4ccdcd81, 0x140c0c18, 0x35131326, 0x2fececc3);
  912. &_data_word(0xe15f5fbe, 0xa2979735, 0xcc444488, 0x3917172e);
  913. &_data_word(0x57c4c493, 0xf2a7a755, 0x827e7efc, 0x473d3d7a);
  914. &_data_word(0xac6464c8, 0xe75d5dba, 0x2b191932, 0x957373e6);
  915. &_data_word(0xa06060c0, 0x98818119, 0xd14f4f9e, 0x7fdcdca3);
  916. &_data_word(0x66222244, 0x7e2a2a54, 0xab90903b, 0x8388880b);
  917. &_data_word(0xca46468c, 0x29eeeec7, 0xd3b8b86b, 0x3c141428);
  918. &_data_word(0x79dedea7, 0xe25e5ebc, 0x1d0b0b16, 0x76dbdbad);
  919. &_data_word(0x3be0e0db, 0x56323264, 0x4e3a3a74, 0x1e0a0a14);
  920. &_data_word(0xdb494992, 0x0a06060c, 0x6c242448, 0xe45c5cb8);
  921. &_data_word(0x5dc2c29f, 0x6ed3d3bd, 0xefacac43, 0xa66262c4);
  922. &_data_word(0xa8919139, 0xa4959531, 0x37e4e4d3, 0x8b7979f2);
  923. &_data_word(0x32e7e7d5, 0x43c8c88b, 0x5937376e, 0xb76d6dda);
  924. &_data_word(0x8c8d8d01, 0x64d5d5b1, 0xd24e4e9c, 0xe0a9a949);
  925. &_data_word(0xb46c6cd8, 0xfa5656ac, 0x07f4f4f3, 0x25eaeacf);
  926. &_data_word(0xaf6565ca, 0x8e7a7af4, 0xe9aeae47, 0x18080810);
  927. &_data_word(0xd5baba6f, 0x887878f0, 0x6f25254a, 0x722e2e5c);
  928. &_data_word(0x241c1c38, 0xf1a6a657, 0xc7b4b473, 0x51c6c697);
  929. &_data_word(0x23e8e8cb, 0x7cdddda1, 0x9c7474e8, 0x211f1f3e);
  930. &_data_word(0xdd4b4b96, 0xdcbdbd61, 0x868b8b0d, 0x858a8a0f);
  931. &_data_word(0x907070e0, 0x423e3e7c, 0xc4b5b571, 0xaa6666cc);
  932. &_data_word(0xd8484890, 0x05030306, 0x01f6f6f7, 0x120e0e1c);
  933. &_data_word(0xa36161c2, 0x5f35356a, 0xf95757ae, 0xd0b9b969);
  934. &_data_word(0x91868617, 0x58c1c199, 0x271d1d3a, 0xb99e9e27);
  935. &_data_word(0x38e1e1d9, 0x13f8f8eb, 0xb398982b, 0x33111122);
  936. &_data_word(0xbb6969d2, 0x70d9d9a9, 0x898e8e07, 0xa7949433);
  937. &_data_word(0xb69b9b2d, 0x221e1e3c, 0x92878715, 0x20e9e9c9);
  938. &_data_word(0x49cece87, 0xff5555aa, 0x78282850, 0x7adfdfa5);
  939. &_data_word(0x8f8c8c03, 0xf8a1a159, 0x80898909, 0x170d0d1a);
  940. &_data_word(0xdabfbf65, 0x31e6e6d7, 0xc6424284, 0xb86868d0);
  941. &_data_word(0xc3414182, 0xb0999929, 0x772d2d5a, 0x110f0f1e);
  942. &_data_word(0xcbb0b07b, 0xfc5454a8, 0xd6bbbb6d, 0x3a16162c);
  943. #Te4 # four copies of Te4 to choose from to avoid L1 aliasing
  944. &data_byte(0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5);
  945. &data_byte(0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76);
  946. &data_byte(0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0);
  947. &data_byte(0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0);
  948. &data_byte(0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc);
  949. &data_byte(0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15);
  950. &data_byte(0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a);
  951. &data_byte(0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75);
  952. &data_byte(0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0);
  953. &data_byte(0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84);
  954. &data_byte(0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b);
  955. &data_byte(0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf);
  956. &data_byte(0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85);
  957. &data_byte(0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8);
  958. &data_byte(0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5);
  959. &data_byte(0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2);
  960. &data_byte(0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17);
  961. &data_byte(0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73);
  962. &data_byte(0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88);
  963. &data_byte(0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb);
  964. &data_byte(0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c);
  965. &data_byte(0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79);
  966. &data_byte(0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9);
  967. &data_byte(0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08);
  968. &data_byte(0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6);
  969. &data_byte(0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a);
  970. &data_byte(0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e);
  971. &data_byte(0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e);
  972. &data_byte(0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94);
  973. &data_byte(0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf);
  974. &data_byte(0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68);
  975. &data_byte(0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16);
  976. &data_byte(0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5);
  977. &data_byte(0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76);
  978. &data_byte(0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0);
  979. &data_byte(0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0);
  980. &data_byte(0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc);
  981. &data_byte(0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15);
  982. &data_byte(0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a);
  983. &data_byte(0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75);
  984. &data_byte(0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0);
  985. &data_byte(0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84);
  986. &data_byte(0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b);
  987. &data_byte(0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf);
  988. &data_byte(0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85);
  989. &data_byte(0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8);
  990. &data_byte(0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5);
  991. &data_byte(0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2);
  992. &data_byte(0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17);
  993. &data_byte(0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73);
  994. &data_byte(0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88);
  995. &data_byte(0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb);
  996. &data_byte(0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c);
  997. &data_byte(0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79);
  998. &data_byte(0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9);
  999. &data_byte(0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08);
  1000. &data_byte(0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6);
  1001. &data_byte(0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a);
  1002. &data_byte(0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e);
  1003. &data_byte(0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e);
  1004. &data_byte(0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94);
  1005. &data_byte(0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf);
  1006. &data_byte(0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68);
  1007. &data_byte(0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16);
  1008. &data_byte(0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5);
  1009. &data_byte(0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76);
  1010. &data_byte(0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0);
  1011. &data_byte(0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0);
  1012. &data_byte(0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc);
  1013. &data_byte(0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15);
  1014. &data_byte(0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a);
  1015. &data_byte(0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75);
  1016. &data_byte(0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0);
  1017. &data_byte(0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84);
  1018. &data_byte(0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b);
  1019. &data_byte(0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf);
  1020. &data_byte(0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85);
  1021. &data_byte(0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8);
  1022. &data_byte(0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5);
  1023. &data_byte(0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2);
  1024. &data_byte(0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17);
  1025. &data_byte(0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73);
  1026. &data_byte(0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88);
  1027. &data_byte(0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb);
  1028. &data_byte(0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c);
  1029. &data_byte(0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79);
  1030. &data_byte(0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9);
  1031. &data_byte(0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08);
  1032. &data_byte(0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6);
  1033. &data_byte(0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a);
  1034. &data_byte(0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e);
  1035. &data_byte(0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e);
  1036. &data_byte(0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94);
  1037. &data_byte(0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf);
  1038. &data_byte(0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68);
  1039. &data_byte(0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16);
  1040. &data_byte(0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5);
  1041. &data_byte(0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76);
  1042. &data_byte(0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0);
  1043. &data_byte(0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0);
  1044. &data_byte(0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc);
  1045. &data_byte(0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15);
  1046. &data_byte(0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a);
  1047. &data_byte(0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75);
  1048. &data_byte(0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0);
  1049. &data_byte(0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84);
  1050. &data_byte(0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b);
  1051. &data_byte(0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf);
  1052. &data_byte(0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85);
  1053. &data_byte(0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8);
  1054. &data_byte(0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5);
  1055. &data_byte(0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2);
  1056. &data_byte(0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17);
  1057. &data_byte(0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73);
  1058. &data_byte(0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88);
  1059. &data_byte(0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb);
  1060. &data_byte(0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c);
  1061. &data_byte(0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79);
  1062. &data_byte(0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9);
  1063. &data_byte(0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08);
  1064. &data_byte(0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6);
  1065. &data_byte(0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a);
  1066. &data_byte(0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e);
  1067. &data_byte(0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e);
  1068. &data_byte(0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94);
  1069. &data_byte(0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf);
  1070. &data_byte(0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68);
  1071. &data_byte(0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16);
  1072. #rcon:
  1073. &data_word(0x00000001, 0x00000002, 0x00000004, 0x00000008);
  1074. &data_word(0x00000010, 0x00000020, 0x00000040, 0x00000080);
  1075. &data_word(0x0000001b, 0x00000036, 0x00000000, 0x00000000);
  1076. &data_word(0x00000000, 0x00000000, 0x00000000, 0x00000000);
  1077. &function_end_B("_x86_AES_encrypt");
  1078. # void AES_encrypt (const void *inp,void *out,const AES_KEY *key);
  1079. &function_begin("AES_encrypt");
  1080. &mov ($acc,&wparam(0)); # load inp
  1081. &mov ($key,&wparam(2)); # load key
  1082. &mov ($s0,"esp");
  1083. &sub ("esp",36);
  1084. &and ("esp",-64); # align to cache-line
  1085. # place stack frame just "above" the key schedule
  1086. &lea ($s1,&DWP(-64-63,$key));
  1087. &sub ($s1,"esp");
  1088. &neg ($s1);
  1089. &and ($s1,0x3C0); # modulo 1024, but aligned to cache-line
  1090. &sub ("esp",$s1);
  1091. &add ("esp",4); # 4 is reserved for caller's return address
  1092. &mov ($_esp,$s0); # save stack pointer
  1093. &call (&label("pic_point")); # make it PIC!
  1094. &set_label("pic_point");
  1095. &blindpop($tbl);
  1096. &picmeup($s0,"OPENSSL_ia32cap_P",$tbl,&label("pic_point")) if (!$x86only);
  1097. &lea ($tbl,&DWP(&label("AES_Te")."-".&label("pic_point"),$tbl));
  1098. # pick Te4 copy which can't "overlap" with stack frame or key schedule
  1099. &lea ($s1,&DWP(768-4,"esp"));
  1100. &sub ($s1,$tbl);
  1101. &and ($s1,0x300);
  1102. &lea ($tbl,&DWP(2048+128,$tbl,$s1));
  1103. if (!$x86only) {
  1104. &bt (&DWP(0,$s0),25); # check for SSE bit
  1105. &jnc (&label("x86"));
  1106. &movq ("mm0",&QWP(0,$acc));
  1107. &movq ("mm4",&QWP(8,$acc));
  1108. &call ("_sse_AES_encrypt_compact");
  1109. &mov ("esp",$_esp); # restore stack pointer
  1110. &mov ($acc,&wparam(1)); # load out
  1111. &movq (&QWP(0,$acc),"mm0"); # write output data
  1112. &movq (&QWP(8,$acc),"mm4");
  1113. &emms ();
  1114. &function_end_A();
  1115. }
  1116. &set_label("x86",16);
  1117. &mov ($_tbl,$tbl);
  1118. &mov ($s0,&DWP(0,$acc)); # load input data
  1119. &mov ($s1,&DWP(4,$acc));
  1120. &mov ($s2,&DWP(8,$acc));
  1121. &mov ($s3,&DWP(12,$acc));
  1122. &call ("_x86_AES_encrypt_compact");
  1123. &mov ("esp",$_esp); # restore stack pointer
  1124. &mov ($acc,&wparam(1)); # load out
  1125. &mov (&DWP(0,$acc),$s0); # write output data
  1126. &mov (&DWP(4,$acc),$s1);
  1127. &mov (&DWP(8,$acc),$s2);
  1128. &mov (&DWP(12,$acc),$s3);
  1129. &function_end("AES_encrypt");
  1130. #--------------------------------------------------------------------#
  1131. ######################################################################
  1132. # "Compact" block function
  1133. ######################################################################
  1134. sub deccompact()
  1135. { my $Fn = \&mov;
  1136. while ($#_>5) { pop(@_); $Fn=sub{}; }
  1137. my ($i,$td,@s)=@_;
  1138. my $tmp = $key;
  1139. my $out = $i==3?$s[0]:$acc;
  1140. # $Fn is used in first compact round and its purpose is to
  1141. # void restoration of some values from stack, so that after
  1142. # 4xdeccompact with extra argument $key, $s0 and $s1 values
  1143. # are left there...
  1144. if($i==3) { &$Fn ($key,$__key); }
  1145. else { &mov ($out,$s[0]); }
  1146. &and ($out,0xFF);
  1147. &movz ($out,&BP(-128,$td,$out,1));
  1148. if ($i==3) { $tmp=$s[1]; }
  1149. &movz ($tmp,&HB($s[1]));
  1150. &movz ($tmp,&BP(-128,$td,$tmp,1));
  1151. &shl ($tmp,8);
  1152. &xor ($out,$tmp);
  1153. if ($i==3) { $tmp=$s[2]; &mov ($s[1],$acc); }
  1154. else { mov ($tmp,$s[2]); }
  1155. &shr ($tmp,16);
  1156. &and ($tmp,0xFF);
  1157. &movz ($tmp,&BP(-128,$td,$tmp,1));
  1158. &shl ($tmp,16);
  1159. &xor ($out,$tmp);
  1160. if ($i==3) { $tmp=$s[3]; &$Fn ($s[2],$__s1); }
  1161. else { &mov ($tmp,$s[3]); }
  1162. &shr ($tmp,24);
  1163. &movz ($tmp,&BP(-128,$td,$tmp,1));
  1164. &shl ($tmp,24);
  1165. &xor ($out,$tmp);
  1166. if ($i<2) { &mov (&DWP(4+4*$i,"esp"),$out); }
  1167. if ($i==3) { &$Fn ($s[3],$__s0); }
  1168. }
  1169. # must be called with 2,3,0,1 as argument sequence!!!
  1170. sub dectransform()
  1171. { my @s = ($s0,$s1,$s2,$s3);
  1172. my $i = shift;
  1173. my $tmp = $key;
  1174. my $tp2 = @s[($i+2)%4]; $tp2 = @s[2] if ($i==1);
  1175. my $tp4 = @s[($i+3)%4]; $tp4 = @s[3] if ($i==1);
  1176. my $tp8 = $tbl;
  1177. &mov ($tmp,0x80808080);
  1178. &and ($tmp,$s[$i]);
  1179. &mov ($acc,$tmp);
  1180. &shr ($tmp,7);
  1181. &lea ($tp2,&DWP(0,$s[$i],$s[$i]));
  1182. &sub ($acc,$tmp);
  1183. &and ($tp2,0xfefefefe);
  1184. &and ($acc,0x1b1b1b1b);
  1185. &xor ($tp2,$acc);
  1186. &mov ($tmp,0x80808080);
  1187. &and ($tmp,$tp2);
  1188. &mov ($acc,$tmp);
  1189. &shr ($tmp,7);
  1190. &lea ($tp4,&DWP(0,$tp2,$tp2));
  1191. &sub ($acc,$tmp);
  1192. &and ($tp4,0xfefefefe);
  1193. &and ($acc,0x1b1b1b1b);
  1194. &xor ($tp2,$s[$i]); # tp2^tp1
  1195. &xor ($tp4,$acc);
  1196. &mov ($tmp,0x80808080);
  1197. &and ($tmp,$tp4);
  1198. &mov ($acc,$tmp);
  1199. &shr ($tmp,7);
  1200. &lea ($tp8,&DWP(0,$tp4,$tp4));
  1201. &sub ($acc,$tmp);
  1202. &and ($tp8,0xfefefefe);
  1203. &and ($acc,0x1b1b1b1b);
  1204. &xor ($tp4,$s[$i]); # tp4^tp1
  1205. &rotl ($s[$i],8); # = ROTATE(tp1,8)
  1206. &xor ($tp8,$acc);
  1207. &xor ($s[$i],$tp2);
  1208. &xor ($tp2,$tp8);
  1209. &xor ($s[$i],$tp4);
  1210. &xor ($tp4,$tp8);
  1211. &rotl ($tp2,24);
  1212. &xor ($s[$i],$tp8); # ^= tp8^(tp4^tp1)^(tp2^tp1)
  1213. &rotl ($tp4,16);
  1214. &xor ($s[$i],$tp2); # ^= ROTATE(tp8^tp2^tp1,24)
  1215. &rotl ($tp8,8);
  1216. &xor ($s[$i],$tp4); # ^= ROTATE(tp8^tp4^tp1,16)
  1217. &mov ($s[0],$__s0) if($i==2); #prefetch $s0
  1218. &mov ($s[1],$__s1) if($i==3); #prefetch $s1
  1219. &mov ($s[2],$__s2) if($i==1);
  1220. &xor ($s[$i],$tp8); # ^= ROTATE(tp8,8)
  1221. &mov ($s[3],$__s3) if($i==1);
  1222. &mov (&DWP(4+4*$i,"esp"),$s[$i]) if($i>=2);
  1223. }
  1224. &function_begin_B("_x86_AES_decrypt_compact");
  1225. # note that caller is expected to allocate stack frame for me!
  1226. &mov ($__key,$key); # save key
  1227. &xor ($s0,&DWP(0,$key)); # xor with key
  1228. &xor ($s1,&DWP(4,$key));
  1229. &xor ($s2,&DWP(8,$key));
  1230. &xor ($s3,&DWP(12,$key));
  1231. &mov ($acc,&DWP(240,$key)); # load key->rounds
  1232. &lea ($acc,&DWP(-2,$acc,$acc));
  1233. &lea ($acc,&DWP(0,$key,$acc,8));
  1234. &mov ($__end,$acc); # end of key schedule
  1235. # prefetch Td4
  1236. &mov ($key,&DWP(0-128,$tbl));
  1237. &mov ($acc,&DWP(32-128,$tbl));
  1238. &mov ($key,&DWP(64-128,$tbl));
  1239. &mov ($acc,&DWP(96-128,$tbl));
  1240. &mov ($key,&DWP(128-128,$tbl));
  1241. &mov ($acc,&DWP(160-128,$tbl));
  1242. &mov ($key,&DWP(192-128,$tbl));
  1243. &mov ($acc,&DWP(224-128,$tbl));
  1244. &set_label("loop",16);
  1245. &deccompact(0,$tbl,$s0,$s3,$s2,$s1,1);
  1246. &deccompact(1,$tbl,$s1,$s0,$s3,$s2,1);
  1247. &deccompact(2,$tbl,$s2,$s1,$s0,$s3,1);
  1248. &deccompact(3,$tbl,$s3,$s2,$s1,$s0,1);
  1249. &dectransform(2);
  1250. &dectransform(3);
  1251. &dectransform(0);
  1252. &dectransform(1);
  1253. &mov ($key,$__key);
  1254. &mov ($tbl,$__tbl);
  1255. &add ($key,16); # advance rd_key
  1256. &xor ($s0,&DWP(0,$key));
  1257. &xor ($s1,&DWP(4,$key));
  1258. &xor ($s2,&DWP(8,$key));
  1259. &xor ($s3,&DWP(12,$key));
  1260. &cmp ($key,$__end);
  1261. &mov ($__key,$key);
  1262. &jb (&label("loop"));
  1263. &deccompact(0,$tbl,$s0,$s3,$s2,$s1);
  1264. &deccompact(1,$tbl,$s1,$s0,$s3,$s2);
  1265. &deccompact(2,$tbl,$s2,$s1,$s0,$s3);
  1266. &deccompact(3,$tbl,$s3,$s2,$s1,$s0);
  1267. &xor ($s0,&DWP(16,$key));
  1268. &xor ($s1,&DWP(20,$key));
  1269. &xor ($s2,&DWP(24,$key));
  1270. &xor ($s3,&DWP(28,$key));
  1271. &ret ();
  1272. &function_end_B("_x86_AES_decrypt_compact");
  1273. ######################################################################
  1274. # "Compact" SSE block function.
  1275. ######################################################################
  1276. sub sse_deccompact()
  1277. {
  1278. &pshufw ("mm1","mm0",0x0c); # 7, 6, 1, 0
  1279. &pshufw ("mm5","mm4",0x09); # 13,12,11,10
  1280. &movd ("eax","mm1"); # 7, 6, 1, 0
  1281. &movd ("ebx","mm5"); # 13,12,11,10
  1282. &mov ($__key,$key);
  1283. &movz ($acc,&LB("eax")); # 0
  1284. &movz ("edx",&HB("eax")); # 1
  1285. &pshufw ("mm2","mm0",0x06); # 3, 2, 5, 4
  1286. &movz ("ecx",&BP(-128,$tbl,$acc,1)); # 0
  1287. &movz ($key,&LB("ebx")); # 10
  1288. &movz ("edx",&BP(-128,$tbl,"edx",1)); # 1
  1289. &shr ("eax",16); # 7, 6
  1290. &shl ("edx",8); # 1
  1291. &movz ($acc,&BP(-128,$tbl,$key,1)); # 10
  1292. &movz ($key,&HB("ebx")); # 11
  1293. &shl ($acc,16); # 10
  1294. &pshufw ("mm6","mm4",0x03); # 9, 8,15,14
  1295. &or ("ecx",$acc); # 10
  1296. &movz ($acc,&BP(-128,$tbl,$key,1)); # 11
  1297. &movz ($key,&HB("eax")); # 7
  1298. &shl ($acc,24); # 11
  1299. &shr ("ebx",16); # 13,12
  1300. &or ("edx",$acc); # 11
  1301. &movz ($acc,&BP(-128,$tbl,$key,1)); # 7
  1302. &movz ($key,&HB("ebx")); # 13
  1303. &shl ($acc,24); # 7
  1304. &or ("ecx",$acc); # 7
  1305. &movz ($acc,&BP(-128,$tbl,$key,1)); # 13
  1306. &movz ($key,&LB("eax")); # 6
  1307. &shl ($acc,8); # 13
  1308. &movd ("eax","mm2"); # 3, 2, 5, 4
  1309. &or ("ecx",$acc); # 13
  1310. &movz ($acc,&BP(-128,$tbl,$key,1)); # 6
  1311. &movz ($key,&LB("ebx")); # 12
  1312. &shl ($acc,16); # 6
  1313. &movd ("ebx","mm6"); # 9, 8,15,14
  1314. &movd ("mm0","ecx"); # t[0] collected
  1315. &movz ("ecx",&BP(-128,$tbl,$key,1)); # 12
  1316. &movz ($key,&LB("eax")); # 4
  1317. &or ("ecx",$acc); # 12
  1318. &movz ($acc,&BP(-128,$tbl,$key,1)); # 4
  1319. &movz ($key,&LB("ebx")); # 14
  1320. &or ("edx",$acc); # 4
  1321. &movz ($acc,&BP(-128,$tbl,$key,1)); # 14
  1322. &movz ($key,&HB("eax")); # 5
  1323. &shl ($acc,16); # 14
  1324. &shr ("eax",16); # 3, 2
  1325. &or ("edx",$acc); # 14
  1326. &movz ($acc,&BP(-128,$tbl,$key,1)); # 5
  1327. &movz ($key,&HB("ebx")); # 15
  1328. &shr ("ebx",16); # 9, 8
  1329. &shl ($acc,8); # 5
  1330. &movd ("mm1","edx"); # t[1] collected
  1331. &movz ("edx",&BP(-128,$tbl,$key,1)); # 15
  1332. &movz ($key,&HB("ebx")); # 9
  1333. &shl ("edx",24); # 15
  1334. &and ("ebx",0xff); # 8
  1335. &or ("edx",$acc); # 15
  1336. &punpckldq ("mm0","mm1"); # t[0,1] collected
  1337. &movz ($acc,&BP(-128,$tbl,$key,1)); # 9
  1338. &movz ($key,&LB("eax")); # 2
  1339. &shl ($acc,8); # 9
  1340. &movz ("eax",&HB("eax")); # 3
  1341. &movz ("ebx",&BP(-128,$tbl,"ebx",1)); # 8
  1342. &or ("ecx",$acc); # 9
  1343. &movz ($acc,&BP(-128,$tbl,$key,1)); # 2
  1344. &or ("edx","ebx"); # 8
  1345. &shl ($acc,16); # 2
  1346. &movz ("eax",&BP(-128,$tbl,"eax",1)); # 3
  1347. &or ("edx",$acc); # 2
  1348. &shl ("eax",24); # 3
  1349. &or ("ecx","eax"); # 3
  1350. &mov ($key,$__key);
  1351. &movd ("mm4","edx"); # t[2] collected
  1352. &movd ("mm5","ecx"); # t[3] collected
  1353. &punpckldq ("mm4","mm5"); # t[2,3] collected
  1354. }
  1355. if (!$x86only) {
  1356. &function_begin_B("_sse_AES_decrypt_compact");
  1357. &pxor ("mm0",&QWP(0,$key)); # 7, 6, 5, 4, 3, 2, 1, 0
  1358. &pxor ("mm4",&QWP(8,$key)); # 15,14,13,12,11,10, 9, 8
  1359. # note that caller is expected to allocate stack frame for me!
  1360. &mov ($acc,&DWP(240,$key)); # load key->rounds
  1361. &lea ($acc,&DWP(-2,$acc,$acc));
  1362. &lea ($acc,&DWP(0,$key,$acc,8));
  1363. &mov ($__end,$acc); # end of key schedule
  1364. &mov ($s0,0x1b1b1b1b); # magic constant
  1365. &mov (&DWP(8,"esp"),$s0);
  1366. &mov (&DWP(12,"esp"),$s0);
  1367. # prefetch Td4
  1368. &mov ($s0,&DWP(0-128,$tbl));
  1369. &mov ($s1,&DWP(32-128,$tbl));
  1370. &mov ($s2,&DWP(64-128,$tbl));
  1371. &mov ($s3,&DWP(96-128,$tbl));
  1372. &mov ($s0,&DWP(128-128,$tbl));
  1373. &mov ($s1,&DWP(160-128,$tbl));
  1374. &mov ($s2,&DWP(192-128,$tbl));
  1375. &mov ($s3,&DWP(224-128,$tbl));
  1376. &set_label("loop",16);
  1377. &sse_deccompact();
  1378. &add ($key,16);
  1379. &cmp ($key,$__end);
  1380. &ja (&label("out"));
  1381. # ROTATE(x^y,N) == ROTATE(x,N)^ROTATE(y,N)
  1382. &movq ("mm3","mm0"); &movq ("mm7","mm4");
  1383. &movq ("mm2","mm0",1); &movq ("mm6","mm4",1);
  1384. &movq ("mm1","mm0"); &movq ("mm5","mm4");
  1385. &pshufw ("mm0","mm0",0xb1); &pshufw ("mm4","mm4",0xb1);# = ROTATE(tp0,16)
  1386. &pslld ("mm2",8); &pslld ("mm6",8);
  1387. &psrld ("mm3",8); &psrld ("mm7",8);
  1388. &pxor ("mm0","mm2"); &pxor ("mm4","mm6"); # ^= tp0<<8
  1389. &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # ^= tp0>>8
  1390. &pslld ("mm2",16); &pslld ("mm6",16);
  1391. &psrld ("mm3",16); &psrld ("mm7",16);
  1392. &pxor ("mm0","mm2"); &pxor ("mm4","mm6"); # ^= tp0<<24
  1393. &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # ^= tp0>>24
  1394. &movq ("mm3",&QWP(8,"esp"));
  1395. &pxor ("mm2","mm2"); &pxor ("mm6","mm6");
  1396. &pcmpgtb("mm2","mm1"); &pcmpgtb("mm6","mm5");
  1397. &pand ("mm2","mm3"); &pand ("mm6","mm3");
  1398. &paddb ("mm1","mm1"); &paddb ("mm5","mm5");
  1399. &pxor ("mm1","mm2"); &pxor ("mm5","mm6"); # tp2
  1400. &movq ("mm3","mm1"); &movq ("mm7","mm5");
  1401. &movq ("mm2","mm1"); &movq ("mm6","mm5");
  1402. &pxor ("mm0","mm1"); &pxor ("mm4","mm5"); # ^= tp2
  1403. &pslld ("mm3",24); &pslld ("mm7",24);
  1404. &psrld ("mm2",8); &psrld ("mm6",8);
  1405. &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # ^= tp2<<24
  1406. &pxor ("mm0","mm2"); &pxor ("mm4","mm6"); # ^= tp2>>8
  1407. &movq ("mm2",&QWP(8,"esp"));
  1408. &pxor ("mm3","mm3"); &pxor ("mm7","mm7");
  1409. &pcmpgtb("mm3","mm1"); &pcmpgtb("mm7","mm5");
  1410. &pand ("mm3","mm2"); &pand ("mm7","mm2");
  1411. &paddb ("mm1","mm1"); &paddb ("mm5","mm5");
  1412. &pxor ("mm1","mm3"); &pxor ("mm5","mm7"); # tp4
  1413. &pshufw ("mm3","mm1",0xb1); &pshufw ("mm7","mm5",0xb1);
  1414. &pxor ("mm0","mm1"); &pxor ("mm4","mm5"); # ^= tp4
  1415. &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # ^= ROTATE(tp4,16)
  1416. &pxor ("mm3","mm3"); &pxor ("mm7","mm7");
  1417. &pcmpgtb("mm3","mm1"); &pcmpgtb("mm7","mm5");
  1418. &pand ("mm3","mm2"); &pand ("mm7","mm2");
  1419. &paddb ("mm1","mm1"); &paddb ("mm5","mm5");
  1420. &pxor ("mm1","mm3"); &pxor ("mm5","mm7"); # tp8
  1421. &pxor ("mm0","mm1"); &pxor ("mm4","mm5"); # ^= tp8
  1422. &movq ("mm3","mm1"); &movq ("mm7","mm5");
  1423. &pshufw ("mm2","mm1",0xb1); &pshufw ("mm6","mm5",0xb1);
  1424. &pxor ("mm0","mm2"); &pxor ("mm4","mm6"); # ^= ROTATE(tp8,16)
  1425. &pslld ("mm1",8); &pslld ("mm5",8);
  1426. &psrld ("mm3",8); &psrld ("mm7",8);
  1427. &movq ("mm2",&QWP(0,$key)); &movq ("mm6",&QWP(8,$key));
  1428. &pxor ("mm0","mm1"); &pxor ("mm4","mm5"); # ^= tp8<<8
  1429. &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # ^= tp8>>8
  1430. &mov ($s0,&DWP(0-128,$tbl));
  1431. &pslld ("mm1",16); &pslld ("mm5",16);
  1432. &mov ($s1,&DWP(64-128,$tbl));
  1433. &psrld ("mm3",16); &psrld ("mm7",16);
  1434. &mov ($s2,&DWP(128-128,$tbl));
  1435. &pxor ("mm0","mm1"); &pxor ("mm4","mm5"); # ^= tp8<<24
  1436. &mov ($s3,&DWP(192-128,$tbl));
  1437. &pxor ("mm0","mm3"); &pxor ("mm4","mm7"); # ^= tp8>>24
  1438. &pxor ("mm0","mm2"); &pxor ("mm4","mm6");
  1439. &jmp (&label("loop"));
  1440. &set_label("out",16);
  1441. &pxor ("mm0",&QWP(0,$key));
  1442. &pxor ("mm4",&QWP(8,$key));
  1443. &ret ();
  1444. &function_end_B("_sse_AES_decrypt_compact");
  1445. }
  1446. ######################################################################
  1447. # Vanilla block function.
  1448. ######################################################################
  1449. sub decstep()
  1450. { my ($i,$td,@s) = @_;
  1451. my $tmp = $key;
  1452. my $out = $i==3?$s[0]:$acc;
  1453. # no instructions are reordered, as performance appears
  1454. # optimal... or rather that all attempts to reorder didn't
  1455. # result in better performance [which by the way is not a
  1456. # bit lower than encryption].
  1457. if($i==3) { &mov ($key,$__key); }
  1458. else { &mov ($out,$s[0]); }
  1459. &and ($out,0xFF);
  1460. &mov ($out,&DWP(0,$td,$out,8));
  1461. if ($i==3) { $tmp=$s[1]; }
  1462. &movz ($tmp,&HB($s[1]));
  1463. &xor ($out,&DWP(3,$td,$tmp,8));
  1464. if ($i==3) { $tmp=$s[2]; &mov ($s[1],$acc); }
  1465. else { &mov ($tmp,$s[2]); }
  1466. &shr ($tmp,16);
  1467. &and ($tmp,0xFF);
  1468. &xor ($out,&DWP(2,$td,$tmp,8));
  1469. if ($i==3) { $tmp=$s[3]; &mov ($s[2],$__s1); }
  1470. else { &mov ($tmp,$s[3]); }
  1471. &shr ($tmp,24);
  1472. &xor ($out,&DWP(1,$td,$tmp,8));
  1473. if ($i<2) { &mov (&DWP(4+4*$i,"esp"),$out); }
  1474. if ($i==3) { &mov ($s[3],$__s0); }
  1475. &comment();
  1476. }
  1477. sub declast()
  1478. { my ($i,$td,@s)=@_;
  1479. my $tmp = $key;
  1480. my $out = $i==3?$s[0]:$acc;
  1481. if($i==0) { &lea ($td,&DWP(2048+128,$td));
  1482. &mov ($tmp,&DWP(0-128,$td));
  1483. &mov ($acc,&DWP(32-128,$td));
  1484. &mov ($tmp,&DWP(64-128,$td));
  1485. &mov ($acc,&DWP(96-128,$td));
  1486. &mov ($tmp,&DWP(128-128,$td));
  1487. &mov ($acc,&DWP(160-128,$td));
  1488. &mov ($tmp,&DWP(192-128,$td));
  1489. &mov ($acc,&DWP(224-128,$td));
  1490. &lea ($td,&DWP(-128,$td)); }
  1491. if($i==3) { &mov ($key,$__key); }
  1492. else { &mov ($out,$s[0]); }
  1493. &and ($out,0xFF);
  1494. &movz ($out,&BP(0,$td,$out,1));
  1495. if ($i==3) { $tmp=$s[1]; }
  1496. &movz ($tmp,&HB($s[1]));
  1497. &movz ($tmp,&BP(0,$td,$tmp,1));
  1498. &shl ($tmp,8);
  1499. &xor ($out,$tmp);
  1500. if ($i==3) { $tmp=$s[2]; &mov ($s[1],$acc); }
  1501. else { mov ($tmp,$s[2]); }
  1502. &shr ($tmp,16);
  1503. &and ($tmp,0xFF);
  1504. &movz ($tmp,&BP(0,$td,$tmp,1));
  1505. &shl ($tmp,16);
  1506. &xor ($out,$tmp);
  1507. if ($i==3) { $tmp=$s[3]; &mov ($s[2],$__s1); }
  1508. else { &mov ($tmp,$s[3]); }
  1509. &shr ($tmp,24);
  1510. &movz ($tmp,&BP(0,$td,$tmp,1));
  1511. &shl ($tmp,24);
  1512. &xor ($out,$tmp);
  1513. if ($i<2) { &mov (&DWP(4+4*$i,"esp"),$out); }
  1514. if ($i==3) { &mov ($s[3],$__s0);
  1515. &lea ($td,&DWP(-2048,$td)); }
  1516. }
  1517. &function_begin_B("_x86_AES_decrypt");
  1518. # note that caller is expected to allocate stack frame for me!
  1519. &mov ($__key,$key); # save key
  1520. &xor ($s0,&DWP(0,$key)); # xor with key
  1521. &xor ($s1,&DWP(4,$key));
  1522. &xor ($s2,&DWP(8,$key));
  1523. &xor ($s3,&DWP(12,$key));
  1524. &mov ($acc,&DWP(240,$key)); # load key->rounds
  1525. if ($small_footprint) {
  1526. &lea ($acc,&DWP(-2,$acc,$acc));
  1527. &lea ($acc,&DWP(0,$key,$acc,8));
  1528. &mov ($__end,$acc); # end of key schedule
  1529. &set_label("loop",16);
  1530. &decstep(0,$tbl,$s0,$s3,$s2,$s1);
  1531. &decstep(1,$tbl,$s1,$s0,$s3,$s2);
  1532. &decstep(2,$tbl,$s2,$s1,$s0,$s3);
  1533. &decstep(3,$tbl,$s3,$s2,$s1,$s0);
  1534. &add ($key,16); # advance rd_key
  1535. &xor ($s0,&DWP(0,$key));
  1536. &xor ($s1,&DWP(4,$key));
  1537. &xor ($s2,&DWP(8,$key));
  1538. &xor ($s3,&DWP(12,$key));
  1539. &cmp ($key,$__end);
  1540. &mov ($__key,$key);
  1541. &jb (&label("loop"));
  1542. }
  1543. else {
  1544. &cmp ($acc,10);
  1545. &jle (&label("10rounds"));
  1546. &cmp ($acc,12);
  1547. &jle (&label("12rounds"));
  1548. &set_label("14rounds",4);
  1549. for ($i=1;$i<3;$i++) {
  1550. &decstep(0,$tbl,$s0,$s3,$s2,$s1);
  1551. &decstep(1,$tbl,$s1,$s0,$s3,$s2);
  1552. &decstep(2,$tbl,$s2,$s1,$s0,$s3);
  1553. &decstep(3,$tbl,$s3,$s2,$s1,$s0);
  1554. &xor ($s0,&DWP(16*$i+0,$key));
  1555. &xor ($s1,&DWP(16*$i+4,$key));
  1556. &xor ($s2,&DWP(16*$i+8,$key));
  1557. &xor ($s3,&DWP(16*$i+12,$key));
  1558. }
  1559. &add ($key,32);
  1560. &mov ($__key,$key); # advance rd_key
  1561. &set_label("12rounds",4);
  1562. for ($i=1;$i<3;$i++) {
  1563. &decstep(0,$tbl,$s0,$s3,$s2,$s1);
  1564. &decstep(1,$tbl,$s1,$s0,$s3,$s2);
  1565. &decstep(2,$tbl,$s2,$s1,$s0,$s3);
  1566. &decstep(3,$tbl,$s3,$s2,$s1,$s0);
  1567. &xor ($s0,&DWP(16*$i+0,$key));
  1568. &xor ($s1,&DWP(16*$i+4,$key));
  1569. &xor ($s2,&DWP(16*$i+8,$key));
  1570. &xor ($s3,&DWP(16*$i+12,$key));
  1571. }
  1572. &add ($key,32);
  1573. &mov ($__key,$key); # advance rd_key
  1574. &set_label("10rounds",4);
  1575. for ($i=1;$i<10;$i++) {
  1576. &decstep(0,$tbl,$s0,$s3,$s2,$s1);
  1577. &decstep(1,$tbl,$s1,$s0,$s3,$s2);
  1578. &decstep(2,$tbl,$s2,$s1,$s0,$s3);
  1579. &decstep(3,$tbl,$s3,$s2,$s1,$s0);
  1580. &xor ($s0,&DWP(16*$i+0,$key));
  1581. &xor ($s1,&DWP(16*$i+4,$key));
  1582. &xor ($s2,&DWP(16*$i+8,$key));
  1583. &xor ($s3,&DWP(16*$i+12,$key));
  1584. }
  1585. }
  1586. &declast(0,$tbl,$s0,$s3,$s2,$s1);
  1587. &declast(1,$tbl,$s1,$s0,$s3,$s2);
  1588. &declast(2,$tbl,$s2,$s1,$s0,$s3);
  1589. &declast(3,$tbl,$s3,$s2,$s1,$s0);
  1590. &add ($key,$small_footprint?16:160);
  1591. &xor ($s0,&DWP(0,$key));
  1592. &xor ($s1,&DWP(4,$key));
  1593. &xor ($s2,&DWP(8,$key));
  1594. &xor ($s3,&DWP(12,$key));
  1595. &ret ();
  1596. &set_label("AES_Td",64); # Yes! I keep it in the code segment!
  1597. &_data_word(0x50a7f451, 0x5365417e, 0xc3a4171a, 0x965e273a);
  1598. &_data_word(0xcb6bab3b, 0xf1459d1f, 0xab58faac, 0x9303e34b);
  1599. &_data_word(0x55fa3020, 0xf66d76ad, 0x9176cc88, 0x254c02f5);
  1600. &_data_word(0xfcd7e54f, 0xd7cb2ac5, 0x80443526, 0x8fa362b5);
  1601. &_data_word(0x495ab1de, 0x671bba25, 0x980eea45, 0xe1c0fe5d);
  1602. &_data_word(0x02752fc3, 0x12f04c81, 0xa397468d, 0xc6f9d36b);
  1603. &_data_word(0xe75f8f03, 0x959c9215, 0xeb7a6dbf, 0xda595295);
  1604. &_data_word(0x2d83bed4, 0xd3217458, 0x2969e049, 0x44c8c98e);
  1605. &_data_word(0x6a89c275, 0x78798ef4, 0x6b3e5899, 0xdd71b927);
  1606. &_data_word(0xb64fe1be, 0x17ad88f0, 0x66ac20c9, 0xb43ace7d);
  1607. &_data_word(0x184adf63, 0x82311ae5, 0x60335197, 0x457f5362);
  1608. &_data_word(0xe07764b1, 0x84ae6bbb, 0x1ca081fe, 0x942b08f9);
  1609. &_data_word(0x58684870, 0x19fd458f, 0x876cde94, 0xb7f87b52);
  1610. &_data_word(0x23d373ab, 0xe2024b72, 0x578f1fe3, 0x2aab5566);
  1611. &_data_word(0x0728ebb2, 0x03c2b52f, 0x9a7bc586, 0xa50837d3);
  1612. &_data_word(0xf2872830, 0xb2a5bf23, 0xba6a0302, 0x5c8216ed);
  1613. &_data_word(0x2b1ccf8a, 0x92b479a7, 0xf0f207f3, 0xa1e2694e);
  1614. &_data_word(0xcdf4da65, 0xd5be0506, 0x1f6234d1, 0x8afea6c4);
  1615. &_data_word(0x9d532e34, 0xa055f3a2, 0x32e18a05, 0x75ebf6a4);
  1616. &_data_word(0x39ec830b, 0xaaef6040, 0x069f715e, 0x51106ebd);
  1617. &_data_word(0xf98a213e, 0x3d06dd96, 0xae053edd, 0x46bde64d);
  1618. &_data_word(0xb58d5491, 0x055dc471, 0x6fd40604, 0xff155060);
  1619. &_data_word(0x24fb9819, 0x97e9bdd6, 0xcc434089, 0x779ed967);
  1620. &_data_word(0xbd42e8b0, 0x888b8907, 0x385b19e7, 0xdbeec879);
  1621. &_data_word(0x470a7ca1, 0xe90f427c, 0xc91e84f8, 0x00000000);
  1622. &_data_word(0x83868009, 0x48ed2b32, 0xac70111e, 0x4e725a6c);
  1623. &_data_word(0xfbff0efd, 0x5638850f, 0x1ed5ae3d, 0x27392d36);
  1624. &_data_word(0x64d90f0a, 0x21a65c68, 0xd1545b9b, 0x3a2e3624);
  1625. &_data_word(0xb1670a0c, 0x0fe75793, 0xd296eeb4, 0x9e919b1b);
  1626. &_data_word(0x4fc5c080, 0xa220dc61, 0x694b775a, 0x161a121c);
  1627. &_data_word(0x0aba93e2, 0xe52aa0c0, 0x43e0223c, 0x1d171b12);
  1628. &_data_word(0x0b0d090e, 0xadc78bf2, 0xb9a8b62d, 0xc8a91e14);
  1629. &_data_word(0x8519f157, 0x4c0775af, 0xbbdd99ee, 0xfd607fa3);
  1630. &_data_word(0x9f2601f7, 0xbcf5725c, 0xc53b6644, 0x347efb5b);
  1631. &_data_word(0x7629438b, 0xdcc623cb, 0x68fcedb6, 0x63f1e4b8);
  1632. &_data_word(0xcadc31d7, 0x10856342, 0x40229713, 0x2011c684);
  1633. &_data_word(0x7d244a85, 0xf83dbbd2, 0x1132f9ae, 0x6da129c7);
  1634. &_data_word(0x4b2f9e1d, 0xf330b2dc, 0xec52860d, 0xd0e3c177);
  1635. &_data_word(0x6c16b32b, 0x99b970a9, 0xfa489411, 0x2264e947);
  1636. &_data_word(0xc48cfca8, 0x1a3ff0a0, 0xd82c7d56, 0xef903322);
  1637. &_data_word(0xc74e4987, 0xc1d138d9, 0xfea2ca8c, 0x360bd498);
  1638. &_data_word(0xcf81f5a6, 0x28de7aa5, 0x268eb7da, 0xa4bfad3f);
  1639. &_data_word(0xe49d3a2c, 0x0d927850, 0x9bcc5f6a, 0x62467e54);
  1640. &_data_word(0xc2138df6, 0xe8b8d890, 0x5ef7392e, 0xf5afc382);
  1641. &_data_word(0xbe805d9f, 0x7c93d069, 0xa92dd56f, 0xb31225cf);
  1642. &_data_word(0x3b99acc8, 0xa77d1810, 0x6e639ce8, 0x7bbb3bdb);
  1643. &_data_word(0x097826cd, 0xf418596e, 0x01b79aec, 0xa89a4f83);
  1644. &_data_word(0x656e95e6, 0x7ee6ffaa, 0x08cfbc21, 0xe6e815ef);
  1645. &_data_word(0xd99be7ba, 0xce366f4a, 0xd4099fea, 0xd67cb029);
  1646. &_data_word(0xafb2a431, 0x31233f2a, 0x3094a5c6, 0xc066a235);
  1647. &_data_word(0x37bc4e74, 0xa6ca82fc, 0xb0d090e0, 0x15d8a733);
  1648. &_data_word(0x4a9804f1, 0xf7daec41, 0x0e50cd7f, 0x2ff69117);
  1649. &_data_word(0x8dd64d76, 0x4db0ef43, 0x544daacc, 0xdf0496e4);
  1650. &_data_word(0xe3b5d19e, 0x1b886a4c, 0xb81f2cc1, 0x7f516546);
  1651. &_data_word(0x04ea5e9d, 0x5d358c01, 0x737487fa, 0x2e410bfb);
  1652. &_data_word(0x5a1d67b3, 0x52d2db92, 0x335610e9, 0x1347d66d);
  1653. &_data_word(0x8c61d79a, 0x7a0ca137, 0x8e14f859, 0x893c13eb);
  1654. &_data_word(0xee27a9ce, 0x35c961b7, 0xede51ce1, 0x3cb1477a);
  1655. &_data_word(0x59dfd29c, 0x3f73f255, 0x79ce1418, 0xbf37c773);
  1656. &_data_word(0xeacdf753, 0x5baafd5f, 0x146f3ddf, 0x86db4478);
  1657. &_data_word(0x81f3afca, 0x3ec468b9, 0x2c342438, 0x5f40a3c2);
  1658. &_data_word(0x72c31d16, 0x0c25e2bc, 0x8b493c28, 0x41950dff);
  1659. &_data_word(0x7101a839, 0xdeb30c08, 0x9ce4b4d8, 0x90c15664);
  1660. &_data_word(0x6184cb7b, 0x70b632d5, 0x745c6c48, 0x4257b8d0);
  1661. #Td4: # four copies of Td4 to choose from to avoid L1 aliasing
  1662. &data_byte(0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38);
  1663. &data_byte(0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb);
  1664. &data_byte(0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87);
  1665. &data_byte(0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb);
  1666. &data_byte(0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d);
  1667. &data_byte(0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e);
  1668. &data_byte(0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2);
  1669. &data_byte(0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25);
  1670. &data_byte(0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16);
  1671. &data_byte(0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92);
  1672. &data_byte(0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda);
  1673. &data_byte(0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84);
  1674. &data_byte(0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a);
  1675. &data_byte(0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06);
  1676. &data_byte(0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02);
  1677. &data_byte(0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b);
  1678. &data_byte(0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea);
  1679. &data_byte(0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73);
  1680. &data_byte(0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85);
  1681. &data_byte(0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e);
  1682. &data_byte(0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89);
  1683. &data_byte(0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b);
  1684. &data_byte(0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20);
  1685. &data_byte(0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4);
  1686. &data_byte(0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31);
  1687. &data_byte(0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f);
  1688. &data_byte(0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d);
  1689. &data_byte(0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef);
  1690. &data_byte(0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0);
  1691. &data_byte(0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61);
  1692. &data_byte(0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26);
  1693. &data_byte(0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d);
  1694. &data_byte(0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38);
  1695. &data_byte(0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb);
  1696. &data_byte(0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87);
  1697. &data_byte(0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb);
  1698. &data_byte(0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d);
  1699. &data_byte(0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e);
  1700. &data_byte(0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2);
  1701. &data_byte(0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25);
  1702. &data_byte(0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16);
  1703. &data_byte(0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92);
  1704. &data_byte(0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda);
  1705. &data_byte(0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84);
  1706. &data_byte(0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a);
  1707. &data_byte(0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06);
  1708. &data_byte(0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02);
  1709. &data_byte(0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b);
  1710. &data_byte(0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea);
  1711. &data_byte(0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73);
  1712. &data_byte(0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85);
  1713. &data_byte(0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e);
  1714. &data_byte(0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89);
  1715. &data_byte(0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b);
  1716. &data_byte(0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20);
  1717. &data_byte(0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4);
  1718. &data_byte(0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31);
  1719. &data_byte(0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f);
  1720. &data_byte(0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d);
  1721. &data_byte(0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef);
  1722. &data_byte(0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0);
  1723. &data_byte(0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61);
  1724. &data_byte(0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26);
  1725. &data_byte(0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d);
  1726. &data_byte(0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38);
  1727. &data_byte(0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb);
  1728. &data_byte(0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87);
  1729. &data_byte(0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb);
  1730. &data_byte(0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d);
  1731. &data_byte(0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e);
  1732. &data_byte(0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2);
  1733. &data_byte(0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25);
  1734. &data_byte(0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16);
  1735. &data_byte(0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92);
  1736. &data_byte(0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda);
  1737. &data_byte(0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84);
  1738. &data_byte(0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a);
  1739. &data_byte(0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06);
  1740. &data_byte(0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02);
  1741. &data_byte(0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b);
  1742. &data_byte(0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea);
  1743. &data_byte(0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73);
  1744. &data_byte(0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85);
  1745. &data_byte(0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e);
  1746. &data_byte(0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89);
  1747. &data_byte(0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b);
  1748. &data_byte(0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20);
  1749. &data_byte(0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4);
  1750. &data_byte(0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31);
  1751. &data_byte(0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f);
  1752. &data_byte(0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d);
  1753. &data_byte(0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef);
  1754. &data_byte(0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0);
  1755. &data_byte(0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61);
  1756. &data_byte(0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26);
  1757. &data_byte(0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d);
  1758. &data_byte(0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38);
  1759. &data_byte(0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb);
  1760. &data_byte(0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87);
  1761. &data_byte(0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb);
  1762. &data_byte(0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d);
  1763. &data_byte(0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e);
  1764. &data_byte(0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2);
  1765. &data_byte(0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25);
  1766. &data_byte(0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16);
  1767. &data_byte(0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92);
  1768. &data_byte(0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda);
  1769. &data_byte(0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84);
  1770. &data_byte(0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a);
  1771. &data_byte(0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06);
  1772. &data_byte(0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02);
  1773. &data_byte(0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b);
  1774. &data_byte(0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea);
  1775. &data_byte(0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73);
  1776. &data_byte(0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85);
  1777. &data_byte(0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e);
  1778. &data_byte(0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89);
  1779. &data_byte(0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b);
  1780. &data_byte(0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20);
  1781. &data_byte(0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4);
  1782. &data_byte(0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31);
  1783. &data_byte(0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f);
  1784. &data_byte(0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d);
  1785. &data_byte(0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef);
  1786. &data_byte(0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0);
  1787. &data_byte(0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61);
  1788. &data_byte(0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26);
  1789. &data_byte(0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d);
  1790. &function_end_B("_x86_AES_decrypt");
  1791. # void AES_decrypt (const void *inp,void *out,const AES_KEY *key);
  1792. &function_begin("AES_decrypt");
  1793. &mov ($acc,&wparam(0)); # load inp
  1794. &mov ($key,&wparam(2)); # load key
  1795. &mov ($s0,"esp");
  1796. &sub ("esp",36);
  1797. &and ("esp",-64); # align to cache-line
  1798. # place stack frame just "above" the key schedule
  1799. &lea ($s1,&DWP(-64-63,$key));
  1800. &sub ($s1,"esp");
  1801. &neg ($s1);
  1802. &and ($s1,0x3C0); # modulo 1024, but aligned to cache-line
  1803. &sub ("esp",$s1);
  1804. &add ("esp",4); # 4 is reserved for caller's return address
  1805. &mov ($_esp,$s0); # save stack pointer
  1806. &call (&label("pic_point")); # make it PIC!
  1807. &set_label("pic_point");
  1808. &blindpop($tbl);
  1809. &picmeup($s0,"OPENSSL_ia32cap_P",$tbl,&label("pic_point")) if(!$x86only);
  1810. &lea ($tbl,&DWP(&label("AES_Td")."-".&label("pic_point"),$tbl));
  1811. # pick Td4 copy which can't "overlap" with stack frame or key schedule
  1812. &lea ($s1,&DWP(768-4,"esp"));
  1813. &sub ($s1,$tbl);
  1814. &and ($s1,0x300);
  1815. &lea ($tbl,&DWP(2048+128,$tbl,$s1));
  1816. if (!$x86only) {
  1817. &bt (&DWP(0,$s0),25); # check for SSE bit
  1818. &jnc (&label("x86"));
  1819. &movq ("mm0",&QWP(0,$acc));
  1820. &movq ("mm4",&QWP(8,$acc));
  1821. &call ("_sse_AES_decrypt_compact");
  1822. &mov ("esp",$_esp); # restore stack pointer
  1823. &mov ($acc,&wparam(1)); # load out
  1824. &movq (&QWP(0,$acc),"mm0"); # write output data
  1825. &movq (&QWP(8,$acc),"mm4");
  1826. &emms ();
  1827. &function_end_A();
  1828. }
  1829. &set_label("x86",16);
  1830. &mov ($_tbl,$tbl);
  1831. &mov ($s0,&DWP(0,$acc)); # load input data
  1832. &mov ($s1,&DWP(4,$acc));
  1833. &mov ($s2,&DWP(8,$acc));
  1834. &mov ($s3,&DWP(12,$acc));
  1835. &call ("_x86_AES_decrypt_compact");
  1836. &mov ("esp",$_esp); # restore stack pointer
  1837. &mov ($acc,&wparam(1)); # load out
  1838. &mov (&DWP(0,$acc),$s0); # write output data
  1839. &mov (&DWP(4,$acc),$s1);
  1840. &mov (&DWP(8,$acc),$s2);
  1841. &mov (&DWP(12,$acc),$s3);
  1842. &function_end("AES_decrypt");
  1843. # void AES_cbc_encrypt (const void char *inp, unsigned char *out,
  1844. # size_t length, const AES_KEY *key,
  1845. # unsigned char *ivp,const int enc);
  1846. {
  1847. # stack frame layout
  1848. # -4(%esp) # return address 0(%esp)
  1849. # 0(%esp) # s0 backing store 4(%esp)
  1850. # 4(%esp) # s1 backing store 8(%esp)
  1851. # 8(%esp) # s2 backing store 12(%esp)
  1852. # 12(%esp) # s3 backing store 16(%esp)
  1853. # 16(%esp) # key backup 20(%esp)
  1854. # 20(%esp) # end of key schedule 24(%esp)
  1855. # 24(%esp) # %ebp backup 28(%esp)
  1856. # 28(%esp) # %esp backup
  1857. my $_inp=&DWP(32,"esp"); # copy of wparam(0)
  1858. my $_out=&DWP(36,"esp"); # copy of wparam(1)
  1859. my $_len=&DWP(40,"esp"); # copy of wparam(2)
  1860. my $_key=&DWP(44,"esp"); # copy of wparam(3)
  1861. my $_ivp=&DWP(48,"esp"); # copy of wparam(4)
  1862. my $_tmp=&DWP(52,"esp"); # volatile variable
  1863. #
  1864. my $ivec=&DWP(60,"esp"); # ivec[16]
  1865. my $aes_key=&DWP(76,"esp"); # copy of aes_key
  1866. my $mark=&DWP(76+240,"esp"); # copy of aes_key->rounds
  1867. &function_begin("AES_cbc_encrypt");
  1868. &mov ($s2 eq "ecx"? $s2 : "",&wparam(2)); # load len
  1869. &cmp ($s2,0);
  1870. &je (&label("drop_out"));
  1871. &call (&label("pic_point")); # make it PIC!
  1872. &set_label("pic_point");
  1873. &blindpop($tbl);
  1874. &picmeup($s0,"OPENSSL_ia32cap_P",$tbl,&label("pic_point")) if(!$x86only);
  1875. &cmp (&wparam(5),0);
  1876. &lea ($tbl,&DWP(&label("AES_Te")."-".&label("pic_point"),$tbl));
  1877. &jne (&label("picked_te"));
  1878. &lea ($tbl,&DWP(&label("AES_Td")."-".&label("AES_Te"),$tbl));
  1879. &set_label("picked_te");
  1880. # one can argue if this is required
  1881. &pushf ();
  1882. &cld ();
  1883. &cmp ($s2,$speed_limit);
  1884. &jb (&label("slow_way"));
  1885. &test ($s2,15);
  1886. &jnz (&label("slow_way"));
  1887. if (!$x86only) {
  1888. &bt (&DWP(0,$s0),28); # check for hyper-threading bit
  1889. &jc (&label("slow_way"));
  1890. }
  1891. # pre-allocate aligned stack frame...
  1892. &lea ($acc,&DWP(-80-244,"esp"));
  1893. &and ($acc,-64);
  1894. # ... and make sure it doesn't alias with $tbl modulo 4096
  1895. &mov ($s0,$tbl);
  1896. &lea ($s1,&DWP(2048+256,$tbl));
  1897. &mov ($s3,$acc);
  1898. &and ($s0,0xfff); # s = %ebp&0xfff
  1899. &and ($s1,0xfff); # e = (%ebp+2048+256)&0xfff
  1900. &and ($s3,0xfff); # p = %esp&0xfff
  1901. &cmp ($s3,$s1); # if (p>=e) %esp =- (p-e);
  1902. &jb (&label("tbl_break_out"));
  1903. &sub ($s3,$s1);
  1904. &sub ($acc,$s3);
  1905. &jmp (&label("tbl_ok"));
  1906. &set_label("tbl_break_out",4); # else %esp -= (p-s)&0xfff + framesz;
  1907. &sub ($s3,$s0);
  1908. &and ($s3,0xfff);
  1909. &add ($s3,384);
  1910. &sub ($acc,$s3);
  1911. &set_label("tbl_ok",4);
  1912. &lea ($s3,&wparam(0)); # obtain pointer to parameter block
  1913. &exch ("esp",$acc); # allocate stack frame
  1914. &add ("esp",4); # reserve for return address!
  1915. &mov ($_tbl,$tbl); # save %ebp
  1916. &mov ($_esp,$acc); # save %esp
  1917. &mov ($s0,&DWP(0,$s3)); # load inp
  1918. &mov ($s1,&DWP(4,$s3)); # load out
  1919. #&mov ($s2,&DWP(8,$s3)); # load len
  1920. &mov ($key,&DWP(12,$s3)); # load key
  1921. &mov ($acc,&DWP(16,$s3)); # load ivp
  1922. &mov ($s3,&DWP(20,$s3)); # load enc flag
  1923. &mov ($_inp,$s0); # save copy of inp
  1924. &mov ($_out,$s1); # save copy of out
  1925. &mov ($_len,$s2); # save copy of len
  1926. &mov ($_key,$key); # save copy of key
  1927. &mov ($_ivp,$acc); # save copy of ivp
  1928. &mov ($mark,0); # copy of aes_key->rounds = 0;
  1929. # do we copy key schedule to stack?
  1930. &mov ($s1 eq "ebx" ? $s1 : "",$key);
  1931. &mov ($s2 eq "ecx" ? $s2 : "",244/4);
  1932. &sub ($s1,$tbl);
  1933. &mov ("esi",$key);
  1934. &and ($s1,0xfff);
  1935. &lea ("edi",$aes_key);
  1936. &cmp ($s1,2048+256);
  1937. &jb (&label("do_copy"));
  1938. &cmp ($s1,4096-244);
  1939. &jb (&label("skip_copy"));
  1940. &set_label("do_copy",4);
  1941. &mov ($_key,"edi");
  1942. &data_word(0xA5F3F689); # rep movsd
  1943. &set_label("skip_copy");
  1944. &mov ($key,16);
  1945. &set_label("prefetch_tbl",4);
  1946. &mov ($s0,&DWP(0,$tbl));
  1947. &mov ($s1,&DWP(32,$tbl));
  1948. &mov ($s2,&DWP(64,$tbl));
  1949. &mov ($acc,&DWP(96,$tbl));
  1950. &lea ($tbl,&DWP(128,$tbl));
  1951. &sub ($key,1);
  1952. &jnz (&label("prefetch_tbl"));
  1953. &sub ($tbl,2048);
  1954. &mov ($acc,$_inp);
  1955. &mov ($key,$_ivp);
  1956. &cmp ($s3,0);
  1957. &je (&label("fast_decrypt"));
  1958. #----------------------------- ENCRYPT -----------------------------#
  1959. &mov ($s0,&DWP(0,$key)); # load iv
  1960. &mov ($s1,&DWP(4,$key));
  1961. &set_label("fast_enc_loop",16);
  1962. &mov ($s2,&DWP(8,$key));
  1963. &mov ($s3,&DWP(12,$key));
  1964. &xor ($s0,&DWP(0,$acc)); # xor input data
  1965. &xor ($s1,&DWP(4,$acc));
  1966. &xor ($s2,&DWP(8,$acc));
  1967. &xor ($s3,&DWP(12,$acc));
  1968. &mov ($key,$_key); # load key
  1969. &call ("_x86_AES_encrypt");
  1970. &mov ($acc,$_inp); # load inp
  1971. &mov ($key,$_out); # load out
  1972. &mov (&DWP(0,$key),$s0); # save output data
  1973. &mov (&DWP(4,$key),$s1);
  1974. &mov (&DWP(8,$key),$s2);
  1975. &mov (&DWP(12,$key),$s3);
  1976. &lea ($acc,&DWP(16,$acc)); # advance inp
  1977. &mov ($s2,$_len); # load len
  1978. &mov ($_inp,$acc); # save inp
  1979. &lea ($s3,&DWP(16,$key)); # advance out
  1980. &mov ($_out,$s3); # save out
  1981. &sub ($s2,16); # decrease len
  1982. &mov ($_len,$s2); # save len
  1983. &jnz (&label("fast_enc_loop"));
  1984. &mov ($acc,$_ivp); # load ivp
  1985. &mov ($s2,&DWP(8,$key)); # restore last 2 dwords
  1986. &mov ($s3,&DWP(12,$key));
  1987. &mov (&DWP(0,$acc),$s0); # save ivec
  1988. &mov (&DWP(4,$acc),$s1);
  1989. &mov (&DWP(8,$acc),$s2);
  1990. &mov (&DWP(12,$acc),$s3);
  1991. &cmp ($mark,0); # was the key schedule copied?
  1992. &mov ("edi",$_key);
  1993. &je (&label("skip_ezero"));
  1994. # zero copy of key schedule
  1995. &mov ("ecx",240/4);
  1996. &xor ("eax","eax");
  1997. &align (4);
  1998. &data_word(0xABF3F689); # rep stosd
  1999. &set_label("skip_ezero");
  2000. &mov ("esp",$_esp);
  2001. &popf ();
  2002. &set_label("drop_out");
  2003. &function_end_A();
  2004. &pushf (); # kludge, never executed
  2005. #----------------------------- DECRYPT -----------------------------#
  2006. &set_label("fast_decrypt",16);
  2007. &cmp ($acc,$_out);
  2008. &je (&label("fast_dec_in_place")); # in-place processing...
  2009. &mov ($_tmp,$key);
  2010. &align (4);
  2011. &set_label("fast_dec_loop",16);
  2012. &mov ($s0,&DWP(0,$acc)); # read input
  2013. &mov ($s1,&DWP(4,$acc));
  2014. &mov ($s2,&DWP(8,$acc));
  2015. &mov ($s3,&DWP(12,$acc));
  2016. &mov ($key,$_key); # load key
  2017. &call ("_x86_AES_decrypt");
  2018. &mov ($key,$_tmp); # load ivp
  2019. &mov ($acc,$_len); # load len
  2020. &xor ($s0,&DWP(0,$key)); # xor iv
  2021. &xor ($s1,&DWP(4,$key));
  2022. &xor ($s2,&DWP(8,$key));
  2023. &xor ($s3,&DWP(12,$key));
  2024. &mov ($key,$_out); # load out
  2025. &mov ($acc,$_inp); # load inp
  2026. &mov (&DWP(0,$key),$s0); # write output
  2027. &mov (&DWP(4,$key),$s1);
  2028. &mov (&DWP(8,$key),$s2);
  2029. &mov (&DWP(12,$key),$s3);
  2030. &mov ($s2,$_len); # load len
  2031. &mov ($_tmp,$acc); # save ivp
  2032. &lea ($acc,&DWP(16,$acc)); # advance inp
  2033. &mov ($_inp,$acc); # save inp
  2034. &lea ($key,&DWP(16,$key)); # advance out
  2035. &mov ($_out,$key); # save out
  2036. &sub ($s2,16); # decrease len
  2037. &mov ($_len,$s2); # save len
  2038. &jnz (&label("fast_dec_loop"));
  2039. &mov ($key,$_tmp); # load temp ivp
  2040. &mov ($acc,$_ivp); # load user ivp
  2041. &mov ($s0,&DWP(0,$key)); # load iv
  2042. &mov ($s1,&DWP(4,$key));
  2043. &mov ($s2,&DWP(8,$key));
  2044. &mov ($s3,&DWP(12,$key));
  2045. &mov (&DWP(0,$acc),$s0); # copy back to user
  2046. &mov (&DWP(4,$acc),$s1);
  2047. &mov (&DWP(8,$acc),$s2);
  2048. &mov (&DWP(12,$acc),$s3);
  2049. &jmp (&label("fast_dec_out"));
  2050. &set_label("fast_dec_in_place",16);
  2051. &set_label("fast_dec_in_place_loop");
  2052. &mov ($s0,&DWP(0,$acc)); # read input
  2053. &mov ($s1,&DWP(4,$acc));
  2054. &mov ($s2,&DWP(8,$acc));
  2055. &mov ($s3,&DWP(12,$acc));
  2056. &lea ($key,$ivec);
  2057. &mov (&DWP(0,$key),$s0); # copy to temp
  2058. &mov (&DWP(4,$key),$s1);
  2059. &mov (&DWP(8,$key),$s2);
  2060. &mov (&DWP(12,$key),$s3);
  2061. &mov ($key,$_key); # load key
  2062. &call ("_x86_AES_decrypt");
  2063. &mov ($key,$_ivp); # load ivp
  2064. &mov ($acc,$_out); # load out
  2065. &xor ($s0,&DWP(0,$key)); # xor iv
  2066. &xor ($s1,&DWP(4,$key));
  2067. &xor ($s2,&DWP(8,$key));
  2068. &xor ($s3,&DWP(12,$key));
  2069. &mov (&DWP(0,$acc),$s0); # write output
  2070. &mov (&DWP(4,$acc),$s1);
  2071. &mov (&DWP(8,$acc),$s2);
  2072. &mov (&DWP(12,$acc),$s3);
  2073. &lea ($acc,&DWP(16,$acc)); # advance out
  2074. &mov ($_out,$acc); # save out
  2075. &lea ($acc,$ivec);
  2076. &mov ($s0,&DWP(0,$acc)); # read temp
  2077. &mov ($s1,&DWP(4,$acc));
  2078. &mov ($s2,&DWP(8,$acc));
  2079. &mov ($s3,&DWP(12,$acc));
  2080. &mov (&DWP(0,$key),$s0); # copy iv
  2081. &mov (&DWP(4,$key),$s1);
  2082. &mov (&DWP(8,$key),$s2);
  2083. &mov (&DWP(12,$key),$s3);
  2084. &mov ($acc,$_inp); # load inp
  2085. &mov ($s2,$_len); # load len
  2086. &lea ($acc,&DWP(16,$acc)); # advance inp
  2087. &mov ($_inp,$acc); # save inp
  2088. &sub ($s2,16); # decrease len
  2089. &mov ($_len,$s2); # save len
  2090. &jnz (&label("fast_dec_in_place_loop"));
  2091. &set_label("fast_dec_out",4);
  2092. &cmp ($mark,0); # was the key schedule copied?
  2093. &mov ("edi",$_key);
  2094. &je (&label("skip_dzero"));
  2095. # zero copy of key schedule
  2096. &mov ("ecx",240/4);
  2097. &xor ("eax","eax");
  2098. &align (4);
  2099. &data_word(0xABF3F689); # rep stosd
  2100. &set_label("skip_dzero");
  2101. &mov ("esp",$_esp);
  2102. &popf ();
  2103. &function_end_A();
  2104. &pushf (); # kludge, never executed
  2105. #--------------------------- SLOW ROUTINE ---------------------------#
  2106. &set_label("slow_way",16);
  2107. &mov ($s0,&DWP(0,$s0)) if (!$x86only);# load OPENSSL_ia32cap
  2108. &mov ($key,&wparam(3)); # load key
  2109. # pre-allocate aligned stack frame...
  2110. &lea ($acc,&DWP(-80,"esp"));
  2111. &and ($acc,-64);
  2112. # ... and make sure it doesn't alias with $key modulo 1024
  2113. &lea ($s1,&DWP(-80-63,$key));
  2114. &sub ($s1,$acc);
  2115. &neg ($s1);
  2116. &and ($s1,0x3C0); # modulo 1024, but aligned to cache-line
  2117. &sub ($acc,$s1);
  2118. # pick S-box copy which can't overlap with stack frame or $key
  2119. &lea ($s1,&DWP(768,$acc));
  2120. &sub ($s1,$tbl);
  2121. &and ($s1,0x300);
  2122. &lea ($tbl,&DWP(2048+128,$tbl,$s1));
  2123. &lea ($s3,&wparam(0)); # pointer to parameter block
  2124. &exch ("esp",$acc);
  2125. &add ("esp",4); # reserve for return address!
  2126. &mov ($_tbl,$tbl); # save %ebp
  2127. &mov ($_esp,$acc); # save %esp
  2128. &mov ($_tmp,$s0); # save OPENSSL_ia32cap
  2129. &mov ($s0,&DWP(0,$s3)); # load inp
  2130. &mov ($s1,&DWP(4,$s3)); # load out
  2131. #&mov ($s2,&DWP(8,$s3)); # load len
  2132. #&mov ($key,&DWP(12,$s3)); # load key
  2133. &mov ($acc,&DWP(16,$s3)); # load ivp
  2134. &mov ($s3,&DWP(20,$s3)); # load enc flag
  2135. &mov ($_inp,$s0); # save copy of inp
  2136. &mov ($_out,$s1); # save copy of out
  2137. &mov ($_len,$s2); # save copy of len
  2138. &mov ($_key,$key); # save copy of key
  2139. &mov ($_ivp,$acc); # save copy of ivp
  2140. &mov ($key,$acc);
  2141. &mov ($acc,$s0);
  2142. &cmp ($s3,0);
  2143. &je (&label("slow_decrypt"));
  2144. #--------------------------- SLOW ENCRYPT ---------------------------#
  2145. &cmp ($s2,16);
  2146. &mov ($s3,$s1);
  2147. &jb (&label("slow_enc_tail"));
  2148. if (!$x86only) {
  2149. &bt ($_tmp,25); # check for SSE bit
  2150. &jnc (&label("slow_enc_x86"));
  2151. &movq ("mm0",&QWP(0,$key)); # load iv
  2152. &movq ("mm4",&QWP(8,$key));
  2153. &set_label("slow_enc_loop_sse",16);
  2154. &pxor ("mm0",&QWP(0,$acc)); # xor input data
  2155. &pxor ("mm4",&QWP(8,$acc));
  2156. &mov ($key,$_key);
  2157. &call ("_sse_AES_encrypt_compact");
  2158. &mov ($acc,$_inp); # load inp
  2159. &mov ($key,$_out); # load out
  2160. &mov ($s2,$_len); # load len
  2161. &movq (&QWP(0,$key),"mm0"); # save output data
  2162. &movq (&QWP(8,$key),"mm4");
  2163. &lea ($acc,&DWP(16,$acc)); # advance inp
  2164. &mov ($_inp,$acc); # save inp
  2165. &lea ($s3,&DWP(16,$key)); # advance out
  2166. &mov ($_out,$s3); # save out
  2167. &sub ($s2,16); # decrease len
  2168. &cmp ($s2,16);
  2169. &mov ($_len,$s2); # save len
  2170. &jae (&label("slow_enc_loop_sse"));
  2171. &test ($s2,15);
  2172. &jnz (&label("slow_enc_tail"));
  2173. &mov ($acc,$_ivp); # load ivp
  2174. &movq (&QWP(0,$acc),"mm0"); # save ivec
  2175. &movq (&QWP(8,$acc),"mm4");
  2176. &emms ();
  2177. &mov ("esp",$_esp);
  2178. &popf ();
  2179. &function_end_A();
  2180. &pushf (); # kludge, never executed
  2181. }
  2182. &set_label("slow_enc_x86",16);
  2183. &mov ($s0,&DWP(0,$key)); # load iv
  2184. &mov ($s1,&DWP(4,$key));
  2185. &set_label("slow_enc_loop_x86",4);
  2186. &mov ($s2,&DWP(8,$key));
  2187. &mov ($s3,&DWP(12,$key));
  2188. &xor ($s0,&DWP(0,$acc)); # xor input data
  2189. &xor ($s1,&DWP(4,$acc));
  2190. &xor ($s2,&DWP(8,$acc));
  2191. &xor ($s3,&DWP(12,$acc));
  2192. &mov ($key,$_key); # load key
  2193. &call ("_x86_AES_encrypt_compact");
  2194. &mov ($acc,$_inp); # load inp
  2195. &mov ($key,$_out); # load out
  2196. &mov (&DWP(0,$key),$s0); # save output data
  2197. &mov (&DWP(4,$key),$s1);
  2198. &mov (&DWP(8,$key),$s2);
  2199. &mov (&DWP(12,$key),$s3);
  2200. &mov ($s2,$_len); # load len
  2201. &lea ($acc,&DWP(16,$acc)); # advance inp
  2202. &mov ($_inp,$acc); # save inp
  2203. &lea ($s3,&DWP(16,$key)); # advance out
  2204. &mov ($_out,$s3); # save out
  2205. &sub ($s2,16); # decrease len
  2206. &cmp ($s2,16);
  2207. &mov ($_len,$s2); # save len
  2208. &jae (&label("slow_enc_loop_x86"));
  2209. &test ($s2,15);
  2210. &jnz (&label("slow_enc_tail"));
  2211. &mov ($acc,$_ivp); # load ivp
  2212. &mov ($s2,&DWP(8,$key)); # restore last dwords
  2213. &mov ($s3,&DWP(12,$key));
  2214. &mov (&DWP(0,$acc),$s0); # save ivec
  2215. &mov (&DWP(4,$acc),$s1);
  2216. &mov (&DWP(8,$acc),$s2);
  2217. &mov (&DWP(12,$acc),$s3);
  2218. &mov ("esp",$_esp);
  2219. &popf ();
  2220. &function_end_A();
  2221. &pushf (); # kludge, never executed
  2222. &set_label("slow_enc_tail",16);
  2223. &emms () if (!$x86only);
  2224. &mov ($key eq "edi"? $key:"",$s3); # load out to edi
  2225. &mov ($s1,16);
  2226. &sub ($s1,$s2);
  2227. &cmp ($key,$acc eq "esi"? $acc:""); # compare with inp
  2228. &je (&label("enc_in_place"));
  2229. &align (4);
  2230. &data_word(0xA4F3F689); # rep movsb # copy input
  2231. &jmp (&label("enc_skip_in_place"));
  2232. &set_label("enc_in_place");
  2233. &lea ($key,&DWP(0,$key,$s2));
  2234. &set_label("enc_skip_in_place");
  2235. &mov ($s2,$s1);
  2236. &xor ($s0,$s0);
  2237. &align (4);
  2238. &data_word(0xAAF3F689); # rep stosb # zero tail
  2239. &mov ($key,$_ivp); # restore ivp
  2240. &mov ($acc,$s3); # output as input
  2241. &mov ($s0,&DWP(0,$key));
  2242. &mov ($s1,&DWP(4,$key));
  2243. &mov ($_len,16); # len=16
  2244. &jmp (&label("slow_enc_loop_x86")); # one more spin...
  2245. #--------------------------- SLOW DECRYPT ---------------------------#
  2246. &set_label("slow_decrypt",16);
  2247. if (!$x86only) {
  2248. &bt ($_tmp,25); # check for SSE bit
  2249. &jnc (&label("slow_dec_loop_x86"));
  2250. &set_label("slow_dec_loop_sse",4);
  2251. &movq ("mm0",&QWP(0,$acc)); # read input
  2252. &movq ("mm4",&QWP(8,$acc));
  2253. &mov ($key,$_key);
  2254. &call ("_sse_AES_decrypt_compact");
  2255. &mov ($acc,$_inp); # load inp
  2256. &lea ($s0,$ivec);
  2257. &mov ($s1,$_out); # load out
  2258. &mov ($s2,$_len); # load len
  2259. &mov ($key,$_ivp); # load ivp
  2260. &movq ("mm1",&QWP(0,$acc)); # re-read input
  2261. &movq ("mm5",&QWP(8,$acc));
  2262. &pxor ("mm0",&QWP(0,$key)); # xor iv
  2263. &pxor ("mm4",&QWP(8,$key));
  2264. &movq (&QWP(0,$key),"mm1"); # copy input to iv
  2265. &movq (&QWP(8,$key),"mm5");
  2266. &sub ($s2,16); # decrease len
  2267. &jc (&label("slow_dec_partial_sse"));
  2268. &movq (&QWP(0,$s1),"mm0"); # write output
  2269. &movq (&QWP(8,$s1),"mm4");
  2270. &lea ($s1,&DWP(16,$s1)); # advance out
  2271. &mov ($_out,$s1); # save out
  2272. &lea ($acc,&DWP(16,$acc)); # advance inp
  2273. &mov ($_inp,$acc); # save inp
  2274. &mov ($_len,$s2); # save len
  2275. &jnz (&label("slow_dec_loop_sse"));
  2276. &emms ();
  2277. &mov ("esp",$_esp);
  2278. &popf ();
  2279. &function_end_A();
  2280. &pushf (); # kludge, never executed
  2281. &set_label("slow_dec_partial_sse",16);
  2282. &movq (&QWP(0,$s0),"mm0"); # save output to temp
  2283. &movq (&QWP(8,$s0),"mm4");
  2284. &emms ();
  2285. &add ($s2 eq "ecx" ? "ecx":"",16);
  2286. &mov ("edi",$s1); # out
  2287. &mov ("esi",$s0); # temp
  2288. &align (4);
  2289. &data_word(0xA4F3F689); # rep movsb # copy partial output
  2290. &mov ("esp",$_esp);
  2291. &popf ();
  2292. &function_end_A();
  2293. &pushf (); # kludge, never executed
  2294. }
  2295. &set_label("slow_dec_loop_x86",16);
  2296. &mov ($s0,&DWP(0,$acc)); # read input
  2297. &mov ($s1,&DWP(4,$acc));
  2298. &mov ($s2,&DWP(8,$acc));
  2299. &mov ($s3,&DWP(12,$acc));
  2300. &lea ($key,$ivec);
  2301. &mov (&DWP(0,$key),$s0); # copy to temp
  2302. &mov (&DWP(4,$key),$s1);
  2303. &mov (&DWP(8,$key),$s2);
  2304. &mov (&DWP(12,$key),$s3);
  2305. &mov ($key,$_key); # load key
  2306. &call ("_x86_AES_decrypt_compact");
  2307. &mov ($key,$_ivp); # load ivp
  2308. &mov ($acc,$_len); # load len
  2309. &xor ($s0,&DWP(0,$key)); # xor iv
  2310. &xor ($s1,&DWP(4,$key));
  2311. &xor ($s2,&DWP(8,$key));
  2312. &xor ($s3,&DWP(12,$key));
  2313. &sub ($acc,16);
  2314. &jc (&label("slow_dec_partial_x86"));
  2315. &mov ($_len,$acc); # save len
  2316. &mov ($acc,$_out); # load out
  2317. &mov (&DWP(0,$acc),$s0); # write output
  2318. &mov (&DWP(4,$acc),$s1);
  2319. &mov (&DWP(8,$acc),$s2);
  2320. &mov (&DWP(12,$acc),$s3);
  2321. &lea ($acc,&DWP(16,$acc)); # advance out
  2322. &mov ($_out,$acc); # save out
  2323. &lea ($acc,$ivec);
  2324. &mov ($s0,&DWP(0,$acc)); # read temp
  2325. &mov ($s1,&DWP(4,$acc));
  2326. &mov ($s2,&DWP(8,$acc));
  2327. &mov ($s3,&DWP(12,$acc));
  2328. &mov (&DWP(0,$key),$s0); # copy it to iv
  2329. &mov (&DWP(4,$key),$s1);
  2330. &mov (&DWP(8,$key),$s2);
  2331. &mov (&DWP(12,$key),$s3);
  2332. &mov ($acc,$_inp); # load inp
  2333. &lea ($acc,&DWP(16,$acc)); # advance inp
  2334. &mov ($_inp,$acc); # save inp
  2335. &jnz (&label("slow_dec_loop_x86"));
  2336. &mov ("esp",$_esp);
  2337. &popf ();
  2338. &function_end_A();
  2339. &pushf (); # kludge, never executed
  2340. &set_label("slow_dec_partial_x86",16);
  2341. &lea ($acc,$ivec);
  2342. &mov (&DWP(0,$acc),$s0); # save output to temp
  2343. &mov (&DWP(4,$acc),$s1);
  2344. &mov (&DWP(8,$acc),$s2);
  2345. &mov (&DWP(12,$acc),$s3);
  2346. &mov ($acc,$_inp);
  2347. &mov ($s0,&DWP(0,$acc)); # re-read input
  2348. &mov ($s1,&DWP(4,$acc));
  2349. &mov ($s2,&DWP(8,$acc));
  2350. &mov ($s3,&DWP(12,$acc));
  2351. &mov (&DWP(0,$key),$s0); # copy it to iv
  2352. &mov (&DWP(4,$key),$s1);
  2353. &mov (&DWP(8,$key),$s2);
  2354. &mov (&DWP(12,$key),$s3);
  2355. &mov ("ecx",$_len);
  2356. &mov ("edi",$_out);
  2357. &lea ("esi",$ivec);
  2358. &align (4);
  2359. &data_word(0xA4F3F689); # rep movsb # copy partial output
  2360. &mov ("esp",$_esp);
  2361. &popf ();
  2362. &function_end("AES_cbc_encrypt");
  2363. }
  2364. #------------------------------------------------------------------#
  2365. sub enckey()
  2366. {
  2367. &movz ("esi",&LB("edx")); # rk[i]>>0
  2368. &movz ("ebx",&BP(-128,$tbl,"esi",1));
  2369. &movz ("esi",&HB("edx")); # rk[i]>>8
  2370. &shl ("ebx",24);
  2371. &xor ("eax","ebx");
  2372. &movz ("ebx",&BP(-128,$tbl,"esi",1));
  2373. &shr ("edx",16);
  2374. &movz ("esi",&LB("edx")); # rk[i]>>16
  2375. &xor ("eax","ebx");
  2376. &movz ("ebx",&BP(-128,$tbl,"esi",1));
  2377. &movz ("esi",&HB("edx")); # rk[i]>>24
  2378. &shl ("ebx",8);
  2379. &xor ("eax","ebx");
  2380. &movz ("ebx",&BP(-128,$tbl,"esi",1));
  2381. &shl ("ebx",16);
  2382. &xor ("eax","ebx");
  2383. &xor ("eax",&DWP(1024-128,$tbl,"ecx",4)); # rcon
  2384. }
  2385. &function_begin("_x86_AES_set_encrypt_key");
  2386. &mov ("esi",&wparam(1)); # user supplied key
  2387. &mov ("edi",&wparam(3)); # private key schedule
  2388. &test ("esi",-1);
  2389. &jz (&label("badpointer"));
  2390. &test ("edi",-1);
  2391. &jz (&label("badpointer"));
  2392. &call (&label("pic_point"));
  2393. &set_label("pic_point");
  2394. &blindpop($tbl);
  2395. &lea ($tbl,&DWP(&label("AES_Te")."-".&label("pic_point"),$tbl));
  2396. &lea ($tbl,&DWP(2048+128,$tbl));
  2397. # prefetch Te4
  2398. &mov ("eax",&DWP(0-128,$tbl));
  2399. &mov ("ebx",&DWP(32-128,$tbl));
  2400. &mov ("ecx",&DWP(64-128,$tbl));
  2401. &mov ("edx",&DWP(96-128,$tbl));
  2402. &mov ("eax",&DWP(128-128,$tbl));
  2403. &mov ("ebx",&DWP(160-128,$tbl));
  2404. &mov ("ecx",&DWP(192-128,$tbl));
  2405. &mov ("edx",&DWP(224-128,$tbl));
  2406. &mov ("ecx",&wparam(2)); # number of bits in key
  2407. &cmp ("ecx",128);
  2408. &je (&label("10rounds"));
  2409. &cmp ("ecx",192);
  2410. &je (&label("12rounds"));
  2411. &cmp ("ecx",256);
  2412. &je (&label("14rounds"));
  2413. &mov ("eax",-2); # invalid number of bits
  2414. &jmp (&label("exit"));
  2415. &set_label("10rounds");
  2416. &mov ("eax",&DWP(0,"esi")); # copy first 4 dwords
  2417. &mov ("ebx",&DWP(4,"esi"));
  2418. &mov ("ecx",&DWP(8,"esi"));
  2419. &mov ("edx",&DWP(12,"esi"));
  2420. &mov (&DWP(0,"edi"),"eax");
  2421. &mov (&DWP(4,"edi"),"ebx");
  2422. &mov (&DWP(8,"edi"),"ecx");
  2423. &mov (&DWP(12,"edi"),"edx");
  2424. &xor ("ecx","ecx");
  2425. &jmp (&label("10shortcut"));
  2426. &align (4);
  2427. &set_label("10loop");
  2428. &mov ("eax",&DWP(0,"edi")); # rk[0]
  2429. &mov ("edx",&DWP(12,"edi")); # rk[3]
  2430. &set_label("10shortcut");
  2431. &enckey ();
  2432. &mov (&DWP(16,"edi"),"eax"); # rk[4]
  2433. &xor ("eax",&DWP(4,"edi"));
  2434. &mov (&DWP(20,"edi"),"eax"); # rk[5]
  2435. &xor ("eax",&DWP(8,"edi"));
  2436. &mov (&DWP(24,"edi"),"eax"); # rk[6]
  2437. &xor ("eax",&DWP(12,"edi"));
  2438. &mov (&DWP(28,"edi"),"eax"); # rk[7]
  2439. &inc ("ecx");
  2440. &add ("edi",16);
  2441. &cmp ("ecx",10);
  2442. &jl (&label("10loop"));
  2443. &mov (&DWP(80,"edi"),10); # setup number of rounds
  2444. &xor ("eax","eax");
  2445. &jmp (&label("exit"));
  2446. &set_label("12rounds");
  2447. &mov ("eax",&DWP(0,"esi")); # copy first 6 dwords
  2448. &mov ("ebx",&DWP(4,"esi"));
  2449. &mov ("ecx",&DWP(8,"esi"));
  2450. &mov ("edx",&DWP(12,"esi"));
  2451. &mov (&DWP(0,"edi"),"eax");
  2452. &mov (&DWP(4,"edi"),"ebx");
  2453. &mov (&DWP(8,"edi"),"ecx");
  2454. &mov (&DWP(12,"edi"),"edx");
  2455. &mov ("ecx",&DWP(16,"esi"));
  2456. &mov ("edx",&DWP(20,"esi"));
  2457. &mov (&DWP(16,"edi"),"ecx");
  2458. &mov (&DWP(20,"edi"),"edx");
  2459. &xor ("ecx","ecx");
  2460. &jmp (&label("12shortcut"));
  2461. &align (4);
  2462. &set_label("12loop");
  2463. &mov ("eax",&DWP(0,"edi")); # rk[0]
  2464. &mov ("edx",&DWP(20,"edi")); # rk[5]
  2465. &set_label("12shortcut");
  2466. &enckey ();
  2467. &mov (&DWP(24,"edi"),"eax"); # rk[6]
  2468. &xor ("eax",&DWP(4,"edi"));
  2469. &mov (&DWP(28,"edi"),"eax"); # rk[7]
  2470. &xor ("eax",&DWP(8,"edi"));
  2471. &mov (&DWP(32,"edi"),"eax"); # rk[8]
  2472. &xor ("eax",&DWP(12,"edi"));
  2473. &mov (&DWP(36,"edi"),"eax"); # rk[9]
  2474. &cmp ("ecx",7);
  2475. &je (&label("12break"));
  2476. &inc ("ecx");
  2477. &xor ("eax",&DWP(16,"edi"));
  2478. &mov (&DWP(40,"edi"),"eax"); # rk[10]
  2479. &xor ("eax",&DWP(20,"edi"));
  2480. &mov (&DWP(44,"edi"),"eax"); # rk[11]
  2481. &add ("edi",24);
  2482. &jmp (&label("12loop"));
  2483. &set_label("12break");
  2484. &mov (&DWP(72,"edi"),12); # setup number of rounds
  2485. &xor ("eax","eax");
  2486. &jmp (&label("exit"));
  2487. &set_label("14rounds");
  2488. &mov ("eax",&DWP(0,"esi")); # copy first 8 dwords
  2489. &mov ("ebx",&DWP(4,"esi"));
  2490. &mov ("ecx",&DWP(8,"esi"));
  2491. &mov ("edx",&DWP(12,"esi"));
  2492. &mov (&DWP(0,"edi"),"eax");
  2493. &mov (&DWP(4,"edi"),"ebx");
  2494. &mov (&DWP(8,"edi"),"ecx");
  2495. &mov (&DWP(12,"edi"),"edx");
  2496. &mov ("eax",&DWP(16,"esi"));
  2497. &mov ("ebx",&DWP(20,"esi"));
  2498. &mov ("ecx",&DWP(24,"esi"));
  2499. &mov ("edx",&DWP(28,"esi"));
  2500. &mov (&DWP(16,"edi"),"eax");
  2501. &mov (&DWP(20,"edi"),"ebx");
  2502. &mov (&DWP(24,"edi"),"ecx");
  2503. &mov (&DWP(28,"edi"),"edx");
  2504. &xor ("ecx","ecx");
  2505. &jmp (&label("14shortcut"));
  2506. &align (4);
  2507. &set_label("14loop");
  2508. &mov ("edx",&DWP(28,"edi")); # rk[7]
  2509. &set_label("14shortcut");
  2510. &mov ("eax",&DWP(0,"edi")); # rk[0]
  2511. &enckey ();
  2512. &mov (&DWP(32,"edi"),"eax"); # rk[8]
  2513. &xor ("eax",&DWP(4,"edi"));
  2514. &mov (&DWP(36,"edi"),"eax"); # rk[9]
  2515. &xor ("eax",&DWP(8,"edi"));
  2516. &mov (&DWP(40,"edi"),"eax"); # rk[10]
  2517. &xor ("eax",&DWP(12,"edi"));
  2518. &mov (&DWP(44,"edi"),"eax"); # rk[11]
  2519. &cmp ("ecx",6);
  2520. &je (&label("14break"));
  2521. &inc ("ecx");
  2522. &mov ("edx","eax");
  2523. &mov ("eax",&DWP(16,"edi")); # rk[4]
  2524. &movz ("esi",&LB("edx")); # rk[11]>>0
  2525. &movz ("ebx",&BP(-128,$tbl,"esi",1));
  2526. &movz ("esi",&HB("edx")); # rk[11]>>8
  2527. &xor ("eax","ebx");
  2528. &movz ("ebx",&BP(-128,$tbl,"esi",1));
  2529. &shr ("edx",16);
  2530. &shl ("ebx",8);
  2531. &movz ("esi",&LB("edx")); # rk[11]>>16
  2532. &xor ("eax","ebx");
  2533. &movz ("ebx",&BP(-128,$tbl,"esi",1));
  2534. &movz ("esi",&HB("edx")); # rk[11]>>24
  2535. &shl ("ebx",16);
  2536. &xor ("eax","ebx");
  2537. &movz ("ebx",&BP(-128,$tbl,"esi",1));
  2538. &shl ("ebx",24);
  2539. &xor ("eax","ebx");
  2540. &mov (&DWP(48,"edi"),"eax"); # rk[12]
  2541. &xor ("eax",&DWP(20,"edi"));
  2542. &mov (&DWP(52,"edi"),"eax"); # rk[13]
  2543. &xor ("eax",&DWP(24,"edi"));
  2544. &mov (&DWP(56,"edi"),"eax"); # rk[14]
  2545. &xor ("eax",&DWP(28,"edi"));
  2546. &mov (&DWP(60,"edi"),"eax"); # rk[15]
  2547. &add ("edi",32);
  2548. &jmp (&label("14loop"));
  2549. &set_label("14break");
  2550. &mov (&DWP(48,"edi"),14); # setup number of rounds
  2551. &xor ("eax","eax");
  2552. &jmp (&label("exit"));
  2553. &set_label("badpointer");
  2554. &mov ("eax",-1);
  2555. &set_label("exit");
  2556. &function_end("_x86_AES_set_encrypt_key");
  2557. # int AES_set_encrypt_key(const unsigned char *userKey, const int bits,
  2558. # AES_KEY *key)
  2559. &function_begin_B("AES_set_encrypt_key");
  2560. &call ("_x86_AES_set_encrypt_key");
  2561. &ret ();
  2562. &function_end_B("AES_set_encrypt_key");
  2563. sub deckey()
  2564. { my ($i,$key,$tp1,$tp2,$tp4,$tp8) = @_;
  2565. my $tmp = $tbl;
  2566. &mov ($tmp,0x80808080);
  2567. &and ($tmp,$tp1);
  2568. &lea ($tp2,&DWP(0,$tp1,$tp1));
  2569. &mov ($acc,$tmp);
  2570. &shr ($tmp,7);
  2571. &sub ($acc,$tmp);
  2572. &and ($tp2,0xfefefefe);
  2573. &and ($acc,0x1b1b1b1b);
  2574. &xor ($tp2,$acc);
  2575. &mov ($tmp,0x80808080);
  2576. &and ($tmp,$tp2);
  2577. &lea ($tp4,&DWP(0,$tp2,$tp2));
  2578. &mov ($acc,$tmp);
  2579. &shr ($tmp,7);
  2580. &sub ($acc,$tmp);
  2581. &and ($tp4,0xfefefefe);
  2582. &and ($acc,0x1b1b1b1b);
  2583. &xor ($tp2,$tp1); # tp2^tp1
  2584. &xor ($tp4,$acc);
  2585. &mov ($tmp,0x80808080);
  2586. &and ($tmp,$tp4);
  2587. &lea ($tp8,&DWP(0,$tp4,$tp4));
  2588. &mov ($acc,$tmp);
  2589. &shr ($tmp,7);
  2590. &xor ($tp4,$tp1); # tp4^tp1
  2591. &sub ($acc,$tmp);
  2592. &and ($tp8,0xfefefefe);
  2593. &and ($acc,0x1b1b1b1b);
  2594. &rotl ($tp1,8); # = ROTATE(tp1,8)
  2595. &xor ($tp8,$acc);
  2596. &mov ($tmp,&DWP(4*($i+1),$key)); # modulo-scheduled load
  2597. &xor ($tp1,$tp2);
  2598. &xor ($tp2,$tp8);
  2599. &xor ($tp1,$tp4);
  2600. &rotl ($tp2,24);
  2601. &xor ($tp4,$tp8);
  2602. &xor ($tp1,$tp8); # ^= tp8^(tp4^tp1)^(tp2^tp1)
  2603. &rotl ($tp4,16);
  2604. &xor ($tp1,$tp2); # ^= ROTATE(tp8^tp2^tp1,24)
  2605. &rotl ($tp8,8);
  2606. &xor ($tp1,$tp4); # ^= ROTATE(tp8^tp4^tp1,16)
  2607. &mov ($tp2,$tmp);
  2608. &xor ($tp1,$tp8); # ^= ROTATE(tp8,8)
  2609. &mov (&DWP(4*$i,$key),$tp1);
  2610. }
  2611. # int AES_set_decrypt_key(const unsigned char *userKey, const int bits,
  2612. # AES_KEY *key)
  2613. &function_begin_B("AES_set_decrypt_key");
  2614. &call ("_x86_AES_set_encrypt_key");
  2615. &cmp ("eax",0);
  2616. &je (&label("proceed"));
  2617. &ret ();
  2618. &set_label("proceed");
  2619. &push ("ebp");
  2620. &push ("ebx");
  2621. &push ("esi");
  2622. &push ("edi");
  2623. &mov ("esi",&wparam(2));
  2624. &mov ("ecx",&DWP(240,"esi")); # pull number of rounds
  2625. &lea ("ecx",&DWP(0,"","ecx",4));
  2626. &lea ("edi",&DWP(0,"esi","ecx",4)); # pointer to last chunk
  2627. &set_label("invert",4); # invert order of chunks
  2628. &mov ("eax",&DWP(0,"esi"));
  2629. &mov ("ebx",&DWP(4,"esi"));
  2630. &mov ("ecx",&DWP(0,"edi"));
  2631. &mov ("edx",&DWP(4,"edi"));
  2632. &mov (&DWP(0,"edi"),"eax");
  2633. &mov (&DWP(4,"edi"),"ebx");
  2634. &mov (&DWP(0,"esi"),"ecx");
  2635. &mov (&DWP(4,"esi"),"edx");
  2636. &mov ("eax",&DWP(8,"esi"));
  2637. &mov ("ebx",&DWP(12,"esi"));
  2638. &mov ("ecx",&DWP(8,"edi"));
  2639. &mov ("edx",&DWP(12,"edi"));
  2640. &mov (&DWP(8,"edi"),"eax");
  2641. &mov (&DWP(12,"edi"),"ebx");
  2642. &mov (&DWP(8,"esi"),"ecx");
  2643. &mov (&DWP(12,"esi"),"edx");
  2644. &add ("esi",16);
  2645. &sub ("edi",16);
  2646. &cmp ("esi","edi");
  2647. &jne (&label("invert"));
  2648. &mov ($key,&wparam(2));
  2649. &mov ($acc,&DWP(240,$key)); # pull number of rounds
  2650. &lea ($acc,&DWP(-2,$acc,$acc));
  2651. &lea ($acc,&DWP(0,$key,$acc,8));
  2652. &mov (&wparam(2),$acc);
  2653. &mov ($s0,&DWP(16,$key)); # modulo-scheduled load
  2654. &set_label("permute",4); # permute the key schedule
  2655. &add ($key,16);
  2656. &deckey (0,$key,$s0,$s1,$s2,$s3);
  2657. &deckey (1,$key,$s1,$s2,$s3,$s0);
  2658. &deckey (2,$key,$s2,$s3,$s0,$s1);
  2659. &deckey (3,$key,$s3,$s0,$s1,$s2);
  2660. &cmp ($key,&wparam(2));
  2661. &jb (&label("permute"));
  2662. &xor ("eax","eax"); # return success
  2663. &function_end("AES_set_decrypt_key");
  2664. &asciz("AES for x86, CRYPTOGAMS by <appro\@openssl.org>");
  2665. &asm_finish();
  2666. close STDOUT;