aesni-x86_64.pl 126 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125
  1. #! /usr/bin/env perl
  2. # Copyright 2009-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. # This module implements support for Intel AES-NI extension. In
  17. # OpenSSL context it's used with Intel engine, but can also be used as
  18. # drop-in replacement for crypto/aes/asm/aes-x86_64.pl [see below for
  19. # details].
  20. #
  21. # Performance.
  22. #
  23. # Given aes(enc|dec) instructions' latency asymptotic performance for
  24. # non-parallelizable modes such as CBC encrypt is 3.75 cycles per byte
  25. # processed with 128-bit key. And given their throughput asymptotic
  26. # performance for parallelizable modes is 1.25 cycles per byte. Being
  27. # asymptotic limit it's not something you commonly achieve in reality,
  28. # but how close does one get? Below are results collected for
  29. # different modes and block sized. Pairs of numbers are for en-/
  30. # decryption.
  31. #
  32. # 16-byte 64-byte 256-byte 1-KB 8-KB
  33. # ECB 4.25/4.25 1.38/1.38 1.28/1.28 1.26/1.26 1.26/1.26
  34. # CTR 5.42/5.42 1.92/1.92 1.44/1.44 1.28/1.28 1.26/1.26
  35. # CBC 4.38/4.43 4.15/1.43 4.07/1.32 4.07/1.29 4.06/1.28
  36. # CCM 5.66/9.42 4.42/5.41 4.16/4.40 4.09/4.15 4.06/4.07
  37. # OFB 5.42/5.42 4.64/4.64 4.44/4.44 4.39/4.39 4.38/4.38
  38. # CFB 5.73/5.85 5.56/5.62 5.48/5.56 5.47/5.55 5.47/5.55
  39. #
  40. # ECB, CTR, CBC and CCM results are free from EVP overhead. This means
  41. # that otherwise used 'openssl speed -evp aes-128-??? -engine aesni
  42. # [-decrypt]' will exhibit 10-15% worse results for smaller blocks.
  43. # The results were collected with specially crafted speed.c benchmark
  44. # in order to compare them with results reported in "Intel Advanced
  45. # Encryption Standard (AES) New Instruction Set" White Paper Revision
  46. # 3.0 dated May 2010. All above results are consistently better. This
  47. # module also provides better performance for block sizes smaller than
  48. # 128 bytes in points *not* represented in the above table.
  49. #
  50. # Looking at the results for 8-KB buffer.
  51. #
  52. # CFB and OFB results are far from the limit, because implementation
  53. # uses "generic" CRYPTO_[c|o]fb128_encrypt interfaces relying on
  54. # single-block aesni_encrypt, which is not the most optimal way to go.
  55. # CBC encrypt result is unexpectedly high and there is no documented
  56. # explanation for it. Seemingly there is a small penalty for feeding
  57. # the result back to AES unit the way it's done in CBC mode. There is
  58. # nothing one can do and the result appears optimal. CCM result is
  59. # identical to CBC, because CBC-MAC is essentially CBC encrypt without
  60. # saving output. CCM CTR "stays invisible," because it's neatly
  61. # interleaved wih CBC-MAC. This provides ~30% improvement over
  62. # "straightforward" CCM implementation with CTR and CBC-MAC performed
  63. # disjointly. Parallelizable modes practically achieve the theoretical
  64. # limit.
  65. #
  66. # Looking at how results vary with buffer size.
  67. #
  68. # Curves are practically saturated at 1-KB buffer size. In most cases
  69. # "256-byte" performance is >95%, and "64-byte" is ~90% of "8-KB" one.
  70. # CTR curve doesn't follow this pattern and is "slowest" changing one
  71. # with "256-byte" result being 87% of "8-KB." This is because overhead
  72. # in CTR mode is most computationally intensive. Small-block CCM
  73. # decrypt is slower than encrypt, because first CTR and last CBC-MAC
  74. # iterations can't be interleaved.
  75. #
  76. # Results for 192- and 256-bit keys.
  77. #
  78. # EVP-free results were observed to scale perfectly with number of
  79. # rounds for larger block sizes, i.e. 192-bit result being 10/12 times
  80. # lower and 256-bit one - 10/14. Well, in CBC encrypt case differences
  81. # are a tad smaller, because the above mentioned penalty biases all
  82. # results by same constant value. In similar way function call
  83. # overhead affects small-block performance, as well as OFB and CFB
  84. # results. Differences are not large, most common coefficients are
  85. # 10/11.7 and 10/13.4 (as opposite to 10/12.0 and 10/14.0), but one
  86. # observe even 10/11.2 and 10/12.4 (CTR, OFB, CFB)...
  87. # January 2011
  88. #
  89. # While Westmere processor features 6 cycles latency for aes[enc|dec]
  90. # instructions, which can be scheduled every second cycle, Sandy
  91. # Bridge spends 8 cycles per instruction, but it can schedule them
  92. # every cycle. This means that code targeting Westmere would perform
  93. # suboptimally on Sandy Bridge. Therefore this update.
  94. #
  95. # In addition, non-parallelizable CBC encrypt (as well as CCM) is
  96. # optimized. Relative improvement might appear modest, 8% on Westmere,
  97. # but in absolute terms it's 3.77 cycles per byte encrypted with
  98. # 128-bit key on Westmere, and 5.07 - on Sandy Bridge. These numbers
  99. # should be compared to asymptotic limits of 3.75 for Westmere and
  100. # 5.00 for Sandy Bridge. Actually, the fact that they get this close
  101. # to asymptotic limits is quite amazing. Indeed, the limit is
  102. # calculated as latency times number of rounds, 10 for 128-bit key,
  103. # and divided by 16, the number of bytes in block, or in other words
  104. # it accounts *solely* for aesenc instructions. But there are extra
  105. # instructions, and numbers so close to the asymptotic limits mean
  106. # that it's as if it takes as little as *one* additional cycle to
  107. # execute all of them. How is it possible? It is possible thanks to
  108. # out-of-order execution logic, which manages to overlap post-
  109. # processing of previous block, things like saving the output, with
  110. # actual encryption of current block, as well as pre-processing of
  111. # current block, things like fetching input and xor-ing it with
  112. # 0-round element of the key schedule, with actual encryption of
  113. # previous block. Keep this in mind...
  114. #
  115. # For parallelizable modes, such as ECB, CBC decrypt, CTR, higher
  116. # performance is achieved by interleaving instructions working on
  117. # independent blocks. In which case asymptotic limit for such modes
  118. # can be obtained by dividing above mentioned numbers by AES
  119. # instructions' interleave factor. Westmere can execute at most 3
  120. # instructions at a time, meaning that optimal interleave factor is 3,
  121. # and that's where the "magic" number of 1.25 come from. "Optimal
  122. # interleave factor" means that increase of interleave factor does
  123. # not improve performance. The formula has proven to reflect reality
  124. # pretty well on Westmere... Sandy Bridge on the other hand can
  125. # execute up to 8 AES instructions at a time, so how does varying
  126. # interleave factor affect the performance? Here is table for ECB
  127. # (numbers are cycles per byte processed with 128-bit key):
  128. #
  129. # instruction interleave factor 3x 6x 8x
  130. # theoretical asymptotic limit 1.67 0.83 0.625
  131. # measured performance for 8KB block 1.05 0.86 0.84
  132. #
  133. # "as if" interleave factor 4.7x 5.8x 6.0x
  134. #
  135. # Further data for other parallelizable modes:
  136. #
  137. # CBC decrypt 1.16 0.93 0.74
  138. # CTR 1.14 0.91 0.74
  139. #
  140. # Well, given 3x column it's probably inappropriate to call the limit
  141. # asymptotic, if it can be surpassed, isn't it? What happens there?
  142. # Rewind to CBC paragraph for the answer. Yes, out-of-order execution
  143. # magic is responsible for this. Processor overlaps not only the
  144. # additional instructions with AES ones, but even AES instructions
  145. # processing adjacent triplets of independent blocks. In the 6x case
  146. # additional instructions still claim disproportionally small amount
  147. # of additional cycles, but in 8x case number of instructions must be
  148. # a tad too high for out-of-order logic to cope with, and AES unit
  149. # remains underutilized... As you can see 8x interleave is hardly
  150. # justifiable, so there no need to feel bad that 32-bit aesni-x86.pl
  151. # utilizes 6x interleave because of limited register bank capacity.
  152. #
  153. # Higher interleave factors do have negative impact on Westmere
  154. # performance. While for ECB mode it's negligible ~1.5%, other
  155. # parallelizables perform ~5% worse, which is outweighed by ~25%
  156. # improvement on Sandy Bridge. To balance regression on Westmere
  157. # CTR mode was implemented with 6x aesenc interleave factor.
  158. # April 2011
  159. #
  160. # Add aesni_xts_[en|de]crypt. Westmere spends 1.25 cycles processing
  161. # one byte out of 8KB with 128-bit key, Sandy Bridge - 0.90. Just like
  162. # in CTR mode AES instruction interleave factor was chosen to be 6x.
  163. # November 2015
  164. #
  165. # Add aesni_ocb_[en|de]crypt. AES instruction interleave factor was
  166. # chosen to be 6x.
  167. ######################################################################
  168. # Current large-block performance in cycles per byte processed with
  169. # 128-bit key (less is better).
  170. #
  171. # CBC en-/decrypt CTR XTS ECB OCB
  172. # Westmere 3.77/1.25 1.25 1.25 1.26
  173. # * Bridge 5.07/0.74 0.75 0.90 0.85 0.98
  174. # Haswell 4.44/0.63 0.63 0.73 0.63 0.70
  175. # Skylake 2.62/0.63 0.63 0.63 0.63
  176. # Silvermont 5.75/3.54 3.56 4.12 3.87(*) 4.11
  177. # Knights L 2.54/0.77 0.78 0.85 - 1.50
  178. # Goldmont 3.82/1.26 1.26 1.29 1.29 1.50
  179. # Bulldozer 5.77/0.70 0.72 0.90 0.70 0.95
  180. # Ryzen 2.71/0.35 0.35 0.44 0.38 0.49
  181. #
  182. # (*) Atom Silvermont ECB result is suboptimal because of penalties
  183. # incurred by operations on %xmm8-15. As ECB is not considered
  184. # critical, nothing was done to mitigate the problem.
  185. $PREFIX="aesni"; # if $PREFIX is set to "AES", the script
  186. # generates drop-in replacement for
  187. # crypto/aes/asm/aes-x86_64.pl:-)
  188. $flavour = shift;
  189. $output = shift;
  190. if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
  191. $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
  192. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  193. ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
  194. ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
  195. die "can't locate x86_64-xlate.pl";
  196. open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\"";
  197. *STDOUT=*OUT;
  198. $movkey = $PREFIX eq "aesni" ? "movups" : "movups";
  199. @_4args=$win64? ("%rcx","%rdx","%r8", "%r9") : # Win64 order
  200. ("%rdi","%rsi","%rdx","%rcx"); # Unix order
  201. $code=".text\n";
  202. $code.=".extern OPENSSL_ia32cap_P\n";
  203. $rounds="%eax"; # input to and changed by aesni_[en|de]cryptN !!!
  204. # this is natural Unix argument order for public $PREFIX_[ecb|cbc]_encrypt ...
  205. $inp="%rdi";
  206. $out="%rsi";
  207. $len="%rdx";
  208. $key="%rcx"; # input to and changed by aesni_[en|de]cryptN !!!
  209. $ivp="%r8"; # cbc, ctr, ...
  210. $rnds_="%r10d"; # backup copy for $rounds
  211. $key_="%r11"; # backup copy for $key
  212. # %xmm register layout
  213. $rndkey0="%xmm0"; $rndkey1="%xmm1";
  214. $inout0="%xmm2"; $inout1="%xmm3";
  215. $inout2="%xmm4"; $inout3="%xmm5";
  216. $inout4="%xmm6"; $inout5="%xmm7";
  217. $inout6="%xmm8"; $inout7="%xmm9";
  218. $in2="%xmm6"; $in1="%xmm7"; # used in CBC decrypt, CTR, ...
  219. $in0="%xmm8"; $iv="%xmm9";
  220. # Inline version of internal aesni_[en|de]crypt1.
  221. #
  222. # Why folded loop? Because aes[enc|dec] is slow enough to accommodate
  223. # cycles which take care of loop variables...
  224. { my $sn;
  225. sub aesni_generate1 {
  226. my ($p,$key,$rounds,$inout,$ivec)=@_; $inout=$inout0 if (!defined($inout));
  227. ++$sn;
  228. $code.=<<___;
  229. $movkey ($key),$rndkey0
  230. $movkey 16($key),$rndkey1
  231. ___
  232. $code.=<<___ if (defined($ivec));
  233. xorps $rndkey0,$ivec
  234. lea 32($key),$key
  235. xorps $ivec,$inout
  236. ___
  237. $code.=<<___ if (!defined($ivec));
  238. lea 32($key),$key
  239. xorps $rndkey0,$inout
  240. ___
  241. $code.=<<___;
  242. .Loop_${p}1_$sn:
  243. aes${p} $rndkey1,$inout
  244. dec $rounds
  245. $movkey ($key),$rndkey1
  246. lea 16($key),$key
  247. jnz .Loop_${p}1_$sn # loop body is 16 bytes
  248. aes${p}last $rndkey1,$inout
  249. ___
  250. }}
  251. # void $PREFIX_[en|de]crypt (const void *inp,void *out,const AES_KEY *key);
  252. #
  253. { my ($inp,$out,$key) = @_4args;
  254. $code.=<<___;
  255. .globl ${PREFIX}_encrypt
  256. .type ${PREFIX}_encrypt,\@abi-omnipotent
  257. .align 16
  258. ${PREFIX}_encrypt:
  259. movups ($inp),$inout0 # load input
  260. mov 240($key),$rounds # key->rounds
  261. ___
  262. &aesni_generate1("enc",$key,$rounds);
  263. $code.=<<___;
  264. pxor $rndkey0,$rndkey0 # clear register bank
  265. pxor $rndkey1,$rndkey1
  266. movups $inout0,($out) # output
  267. pxor $inout0,$inout0
  268. ret
  269. .size ${PREFIX}_encrypt,.-${PREFIX}_encrypt
  270. .globl ${PREFIX}_decrypt
  271. .type ${PREFIX}_decrypt,\@abi-omnipotent
  272. .align 16
  273. ${PREFIX}_decrypt:
  274. movups ($inp),$inout0 # load input
  275. mov 240($key),$rounds # key->rounds
  276. ___
  277. &aesni_generate1("dec",$key,$rounds);
  278. $code.=<<___;
  279. pxor $rndkey0,$rndkey0 # clear register bank
  280. pxor $rndkey1,$rndkey1
  281. movups $inout0,($out) # output
  282. pxor $inout0,$inout0
  283. ret
  284. .size ${PREFIX}_decrypt, .-${PREFIX}_decrypt
  285. ___
  286. }
  287. # _aesni_[en|de]cryptN are private interfaces, N denotes interleave
  288. # factor. Why 3x subroutine were originally used in loops? Even though
  289. # aes[enc|dec] latency was originally 6, it could be scheduled only
  290. # every *2nd* cycle. Thus 3x interleave was the one providing optimal
  291. # utilization, i.e. when subroutine's throughput is virtually same as
  292. # of non-interleaved subroutine [for number of input blocks up to 3].
  293. # This is why it originally made no sense to implement 2x subroutine.
  294. # But times change and it became appropriate to spend extra 192 bytes
  295. # on 2x subroutine on Atom Silvermont account. For processors that
  296. # can schedule aes[enc|dec] every cycle optimal interleave factor
  297. # equals to corresponding instructions latency. 8x is optimal for
  298. # * Bridge and "super-optimal" for other Intel CPUs...
  299. sub aesni_generate2 {
  300. my $dir=shift;
  301. # As already mentioned it takes in $key and $rounds, which are *not*
  302. # preserved. $inout[0-1] is cipher/clear text...
  303. $code.=<<___;
  304. .type _aesni_${dir}rypt2,\@abi-omnipotent
  305. .align 16
  306. _aesni_${dir}rypt2:
  307. $movkey ($key),$rndkey0
  308. shl \$4,$rounds
  309. $movkey 16($key),$rndkey1
  310. xorps $rndkey0,$inout0
  311. xorps $rndkey0,$inout1
  312. $movkey 32($key),$rndkey0
  313. lea 32($key,$rounds),$key
  314. neg %rax # $rounds
  315. add \$16,%rax
  316. .L${dir}_loop2:
  317. aes${dir} $rndkey1,$inout0
  318. aes${dir} $rndkey1,$inout1
  319. $movkey ($key,%rax),$rndkey1
  320. add \$32,%rax
  321. aes${dir} $rndkey0,$inout0
  322. aes${dir} $rndkey0,$inout1
  323. $movkey -16($key,%rax),$rndkey0
  324. jnz .L${dir}_loop2
  325. aes${dir} $rndkey1,$inout0
  326. aes${dir} $rndkey1,$inout1
  327. aes${dir}last $rndkey0,$inout0
  328. aes${dir}last $rndkey0,$inout1
  329. ret
  330. .size _aesni_${dir}rypt2,.-_aesni_${dir}rypt2
  331. ___
  332. }
  333. sub aesni_generate3 {
  334. my $dir=shift;
  335. # As already mentioned it takes in $key and $rounds, which are *not*
  336. # preserved. $inout[0-2] is cipher/clear text...
  337. $code.=<<___;
  338. .type _aesni_${dir}rypt3,\@abi-omnipotent
  339. .align 16
  340. _aesni_${dir}rypt3:
  341. $movkey ($key),$rndkey0
  342. shl \$4,$rounds
  343. $movkey 16($key),$rndkey1
  344. xorps $rndkey0,$inout0
  345. xorps $rndkey0,$inout1
  346. xorps $rndkey0,$inout2
  347. $movkey 32($key),$rndkey0
  348. lea 32($key,$rounds),$key
  349. neg %rax # $rounds
  350. add \$16,%rax
  351. .L${dir}_loop3:
  352. aes${dir} $rndkey1,$inout0
  353. aes${dir} $rndkey1,$inout1
  354. aes${dir} $rndkey1,$inout2
  355. $movkey ($key,%rax),$rndkey1
  356. add \$32,%rax
  357. aes${dir} $rndkey0,$inout0
  358. aes${dir} $rndkey0,$inout1
  359. aes${dir} $rndkey0,$inout2
  360. $movkey -16($key,%rax),$rndkey0
  361. jnz .L${dir}_loop3
  362. aes${dir} $rndkey1,$inout0
  363. aes${dir} $rndkey1,$inout1
  364. aes${dir} $rndkey1,$inout2
  365. aes${dir}last $rndkey0,$inout0
  366. aes${dir}last $rndkey0,$inout1
  367. aes${dir}last $rndkey0,$inout2
  368. ret
  369. .size _aesni_${dir}rypt3,.-_aesni_${dir}rypt3
  370. ___
  371. }
  372. # 4x interleave is implemented to improve small block performance,
  373. # most notably [and naturally] 4 block by ~30%. One can argue that one
  374. # should have implemented 5x as well, but improvement would be <20%,
  375. # so it's not worth it...
  376. sub aesni_generate4 {
  377. my $dir=shift;
  378. # As already mentioned it takes in $key and $rounds, which are *not*
  379. # preserved. $inout[0-3] is cipher/clear text...
  380. $code.=<<___;
  381. .type _aesni_${dir}rypt4,\@abi-omnipotent
  382. .align 16
  383. _aesni_${dir}rypt4:
  384. $movkey ($key),$rndkey0
  385. shl \$4,$rounds
  386. $movkey 16($key),$rndkey1
  387. xorps $rndkey0,$inout0
  388. xorps $rndkey0,$inout1
  389. xorps $rndkey0,$inout2
  390. xorps $rndkey0,$inout3
  391. $movkey 32($key),$rndkey0
  392. lea 32($key,$rounds),$key
  393. neg %rax # $rounds
  394. .byte 0x0f,0x1f,0x00
  395. add \$16,%rax
  396. .L${dir}_loop4:
  397. aes${dir} $rndkey1,$inout0
  398. aes${dir} $rndkey1,$inout1
  399. aes${dir} $rndkey1,$inout2
  400. aes${dir} $rndkey1,$inout3
  401. $movkey ($key,%rax),$rndkey1
  402. add \$32,%rax
  403. aes${dir} $rndkey0,$inout0
  404. aes${dir} $rndkey0,$inout1
  405. aes${dir} $rndkey0,$inout2
  406. aes${dir} $rndkey0,$inout3
  407. $movkey -16($key,%rax),$rndkey0
  408. jnz .L${dir}_loop4
  409. aes${dir} $rndkey1,$inout0
  410. aes${dir} $rndkey1,$inout1
  411. aes${dir} $rndkey1,$inout2
  412. aes${dir} $rndkey1,$inout3
  413. aes${dir}last $rndkey0,$inout0
  414. aes${dir}last $rndkey0,$inout1
  415. aes${dir}last $rndkey0,$inout2
  416. aes${dir}last $rndkey0,$inout3
  417. ret
  418. .size _aesni_${dir}rypt4,.-_aesni_${dir}rypt4
  419. ___
  420. }
  421. sub aesni_generate6 {
  422. my $dir=shift;
  423. # As already mentioned it takes in $key and $rounds, which are *not*
  424. # preserved. $inout[0-5] is cipher/clear text...
  425. $code.=<<___;
  426. .type _aesni_${dir}rypt6,\@abi-omnipotent
  427. .align 16
  428. _aesni_${dir}rypt6:
  429. $movkey ($key),$rndkey0
  430. shl \$4,$rounds
  431. $movkey 16($key),$rndkey1
  432. xorps $rndkey0,$inout0
  433. pxor $rndkey0,$inout1
  434. pxor $rndkey0,$inout2
  435. aes${dir} $rndkey1,$inout0
  436. lea 32($key,$rounds),$key
  437. neg %rax # $rounds
  438. aes${dir} $rndkey1,$inout1
  439. pxor $rndkey0,$inout3
  440. pxor $rndkey0,$inout4
  441. aes${dir} $rndkey1,$inout2
  442. pxor $rndkey0,$inout5
  443. $movkey ($key,%rax),$rndkey0
  444. add \$16,%rax
  445. jmp .L${dir}_loop6_enter
  446. .align 16
  447. .L${dir}_loop6:
  448. aes${dir} $rndkey1,$inout0
  449. aes${dir} $rndkey1,$inout1
  450. aes${dir} $rndkey1,$inout2
  451. .L${dir}_loop6_enter:
  452. aes${dir} $rndkey1,$inout3
  453. aes${dir} $rndkey1,$inout4
  454. aes${dir} $rndkey1,$inout5
  455. $movkey ($key,%rax),$rndkey1
  456. add \$32,%rax
  457. aes${dir} $rndkey0,$inout0
  458. aes${dir} $rndkey0,$inout1
  459. aes${dir} $rndkey0,$inout2
  460. aes${dir} $rndkey0,$inout3
  461. aes${dir} $rndkey0,$inout4
  462. aes${dir} $rndkey0,$inout5
  463. $movkey -16($key,%rax),$rndkey0
  464. jnz .L${dir}_loop6
  465. aes${dir} $rndkey1,$inout0
  466. aes${dir} $rndkey1,$inout1
  467. aes${dir} $rndkey1,$inout2
  468. aes${dir} $rndkey1,$inout3
  469. aes${dir} $rndkey1,$inout4
  470. aes${dir} $rndkey1,$inout5
  471. aes${dir}last $rndkey0,$inout0
  472. aes${dir}last $rndkey0,$inout1
  473. aes${dir}last $rndkey0,$inout2
  474. aes${dir}last $rndkey0,$inout3
  475. aes${dir}last $rndkey0,$inout4
  476. aes${dir}last $rndkey0,$inout5
  477. ret
  478. .size _aesni_${dir}rypt6,.-_aesni_${dir}rypt6
  479. ___
  480. }
  481. sub aesni_generate8 {
  482. my $dir=shift;
  483. # As already mentioned it takes in $key and $rounds, which are *not*
  484. # preserved. $inout[0-7] is cipher/clear text...
  485. $code.=<<___;
  486. .type _aesni_${dir}rypt8,\@abi-omnipotent
  487. .align 16
  488. _aesni_${dir}rypt8:
  489. $movkey ($key),$rndkey0
  490. shl \$4,$rounds
  491. $movkey 16($key),$rndkey1
  492. xorps $rndkey0,$inout0
  493. xorps $rndkey0,$inout1
  494. pxor $rndkey0,$inout2
  495. pxor $rndkey0,$inout3
  496. pxor $rndkey0,$inout4
  497. lea 32($key,$rounds),$key
  498. neg %rax # $rounds
  499. aes${dir} $rndkey1,$inout0
  500. pxor $rndkey0,$inout5
  501. pxor $rndkey0,$inout6
  502. aes${dir} $rndkey1,$inout1
  503. pxor $rndkey0,$inout7
  504. $movkey ($key,%rax),$rndkey0
  505. add \$16,%rax
  506. jmp .L${dir}_loop8_inner
  507. .align 16
  508. .L${dir}_loop8:
  509. aes${dir} $rndkey1,$inout0
  510. aes${dir} $rndkey1,$inout1
  511. .L${dir}_loop8_inner:
  512. aes${dir} $rndkey1,$inout2
  513. aes${dir} $rndkey1,$inout3
  514. aes${dir} $rndkey1,$inout4
  515. aes${dir} $rndkey1,$inout5
  516. aes${dir} $rndkey1,$inout6
  517. aes${dir} $rndkey1,$inout7
  518. .L${dir}_loop8_enter:
  519. $movkey ($key,%rax),$rndkey1
  520. add \$32,%rax
  521. aes${dir} $rndkey0,$inout0
  522. aes${dir} $rndkey0,$inout1
  523. aes${dir} $rndkey0,$inout2
  524. aes${dir} $rndkey0,$inout3
  525. aes${dir} $rndkey0,$inout4
  526. aes${dir} $rndkey0,$inout5
  527. aes${dir} $rndkey0,$inout6
  528. aes${dir} $rndkey0,$inout7
  529. $movkey -16($key,%rax),$rndkey0
  530. jnz .L${dir}_loop8
  531. aes${dir} $rndkey1,$inout0
  532. aes${dir} $rndkey1,$inout1
  533. aes${dir} $rndkey1,$inout2
  534. aes${dir} $rndkey1,$inout3
  535. aes${dir} $rndkey1,$inout4
  536. aes${dir} $rndkey1,$inout5
  537. aes${dir} $rndkey1,$inout6
  538. aes${dir} $rndkey1,$inout7
  539. aes${dir}last $rndkey0,$inout0
  540. aes${dir}last $rndkey0,$inout1
  541. aes${dir}last $rndkey0,$inout2
  542. aes${dir}last $rndkey0,$inout3
  543. aes${dir}last $rndkey0,$inout4
  544. aes${dir}last $rndkey0,$inout5
  545. aes${dir}last $rndkey0,$inout6
  546. aes${dir}last $rndkey0,$inout7
  547. ret
  548. .size _aesni_${dir}rypt8,.-_aesni_${dir}rypt8
  549. ___
  550. }
  551. &aesni_generate2("enc") if ($PREFIX eq "aesni");
  552. &aesni_generate2("dec");
  553. &aesni_generate3("enc") if ($PREFIX eq "aesni");
  554. &aesni_generate3("dec");
  555. &aesni_generate4("enc") if ($PREFIX eq "aesni");
  556. &aesni_generate4("dec");
  557. &aesni_generate6("enc") if ($PREFIX eq "aesni");
  558. &aesni_generate6("dec");
  559. &aesni_generate8("enc") if ($PREFIX eq "aesni");
  560. &aesni_generate8("dec");
  561. if ($PREFIX eq "aesni") {
  562. ########################################################################
  563. # void aesni_ecb_encrypt (const void *in, void *out,
  564. # size_t length, const AES_KEY *key,
  565. # int enc);
  566. $code.=<<___;
  567. .globl aesni_ecb_encrypt
  568. .type aesni_ecb_encrypt,\@function,5
  569. .align 16
  570. aesni_ecb_encrypt:
  571. ___
  572. $code.=<<___ if ($win64);
  573. lea -0x58(%rsp),%rsp
  574. movaps %xmm6,(%rsp) # offload $inout4..7
  575. movaps %xmm7,0x10(%rsp)
  576. movaps %xmm8,0x20(%rsp)
  577. movaps %xmm9,0x30(%rsp)
  578. .Lecb_enc_body:
  579. ___
  580. $code.=<<___;
  581. and \$-16,$len # if ($len<16)
  582. jz .Lecb_ret # return
  583. mov 240($key),$rounds # key->rounds
  584. $movkey ($key),$rndkey0
  585. mov $key,$key_ # backup $key
  586. mov $rounds,$rnds_ # backup $rounds
  587. test %r8d,%r8d # 5th argument
  588. jz .Lecb_decrypt
  589. #--------------------------- ECB ENCRYPT ------------------------------#
  590. cmp \$0x80,$len # if ($len<8*16)
  591. jb .Lecb_enc_tail # short input
  592. movdqu ($inp),$inout0 # load 8 input blocks
  593. movdqu 0x10($inp),$inout1
  594. movdqu 0x20($inp),$inout2
  595. movdqu 0x30($inp),$inout3
  596. movdqu 0x40($inp),$inout4
  597. movdqu 0x50($inp),$inout5
  598. movdqu 0x60($inp),$inout6
  599. movdqu 0x70($inp),$inout7
  600. lea 0x80($inp),$inp # $inp+=8*16
  601. sub \$0x80,$len # $len-=8*16 (can be zero)
  602. jmp .Lecb_enc_loop8_enter
  603. .align 16
  604. .Lecb_enc_loop8:
  605. movups $inout0,($out) # store 8 output blocks
  606. mov $key_,$key # restore $key
  607. movdqu ($inp),$inout0 # load 8 input blocks
  608. mov $rnds_,$rounds # restore $rounds
  609. movups $inout1,0x10($out)
  610. movdqu 0x10($inp),$inout1
  611. movups $inout2,0x20($out)
  612. movdqu 0x20($inp),$inout2
  613. movups $inout3,0x30($out)
  614. movdqu 0x30($inp),$inout3
  615. movups $inout4,0x40($out)
  616. movdqu 0x40($inp),$inout4
  617. movups $inout5,0x50($out)
  618. movdqu 0x50($inp),$inout5
  619. movups $inout6,0x60($out)
  620. movdqu 0x60($inp),$inout6
  621. movups $inout7,0x70($out)
  622. lea 0x80($out),$out # $out+=8*16
  623. movdqu 0x70($inp),$inout7
  624. lea 0x80($inp),$inp # $inp+=8*16
  625. .Lecb_enc_loop8_enter:
  626. call _aesni_encrypt8
  627. sub \$0x80,$len
  628. jnc .Lecb_enc_loop8 # loop if $len-=8*16 didn't borrow
  629. movups $inout0,($out) # store 8 output blocks
  630. mov $key_,$key # restore $key
  631. movups $inout1,0x10($out)
  632. mov $rnds_,$rounds # restore $rounds
  633. movups $inout2,0x20($out)
  634. movups $inout3,0x30($out)
  635. movups $inout4,0x40($out)
  636. movups $inout5,0x50($out)
  637. movups $inout6,0x60($out)
  638. movups $inout7,0x70($out)
  639. lea 0x80($out),$out # $out+=8*16
  640. add \$0x80,$len # restore real remaining $len
  641. jz .Lecb_ret # done if ($len==0)
  642. .Lecb_enc_tail: # $len is less than 8*16
  643. movups ($inp),$inout0
  644. cmp \$0x20,$len
  645. jb .Lecb_enc_one
  646. movups 0x10($inp),$inout1
  647. je .Lecb_enc_two
  648. movups 0x20($inp),$inout2
  649. cmp \$0x40,$len
  650. jb .Lecb_enc_three
  651. movups 0x30($inp),$inout3
  652. je .Lecb_enc_four
  653. movups 0x40($inp),$inout4
  654. cmp \$0x60,$len
  655. jb .Lecb_enc_five
  656. movups 0x50($inp),$inout5
  657. je .Lecb_enc_six
  658. movdqu 0x60($inp),$inout6
  659. xorps $inout7,$inout7
  660. call _aesni_encrypt8
  661. movups $inout0,($out) # store 7 output blocks
  662. movups $inout1,0x10($out)
  663. movups $inout2,0x20($out)
  664. movups $inout3,0x30($out)
  665. movups $inout4,0x40($out)
  666. movups $inout5,0x50($out)
  667. movups $inout6,0x60($out)
  668. jmp .Lecb_ret
  669. .align 16
  670. .Lecb_enc_one:
  671. ___
  672. &aesni_generate1("enc",$key,$rounds);
  673. $code.=<<___;
  674. movups $inout0,($out) # store one output block
  675. jmp .Lecb_ret
  676. .align 16
  677. .Lecb_enc_two:
  678. call _aesni_encrypt2
  679. movups $inout0,($out) # store 2 output blocks
  680. movups $inout1,0x10($out)
  681. jmp .Lecb_ret
  682. .align 16
  683. .Lecb_enc_three:
  684. call _aesni_encrypt3
  685. movups $inout0,($out) # store 3 output blocks
  686. movups $inout1,0x10($out)
  687. movups $inout2,0x20($out)
  688. jmp .Lecb_ret
  689. .align 16
  690. .Lecb_enc_four:
  691. call _aesni_encrypt4
  692. movups $inout0,($out) # store 4 output blocks
  693. movups $inout1,0x10($out)
  694. movups $inout2,0x20($out)
  695. movups $inout3,0x30($out)
  696. jmp .Lecb_ret
  697. .align 16
  698. .Lecb_enc_five:
  699. xorps $inout5,$inout5
  700. call _aesni_encrypt6
  701. movups $inout0,($out) # store 5 output blocks
  702. movups $inout1,0x10($out)
  703. movups $inout2,0x20($out)
  704. movups $inout3,0x30($out)
  705. movups $inout4,0x40($out)
  706. jmp .Lecb_ret
  707. .align 16
  708. .Lecb_enc_six:
  709. call _aesni_encrypt6
  710. movups $inout0,($out) # store 6 output blocks
  711. movups $inout1,0x10($out)
  712. movups $inout2,0x20($out)
  713. movups $inout3,0x30($out)
  714. movups $inout4,0x40($out)
  715. movups $inout5,0x50($out)
  716. jmp .Lecb_ret
  717. #--------------------------- ECB DECRYPT ------------------------------#
  718. .align 16
  719. .Lecb_decrypt:
  720. cmp \$0x80,$len # if ($len<8*16)
  721. jb .Lecb_dec_tail # short input
  722. movdqu ($inp),$inout0 # load 8 input blocks
  723. movdqu 0x10($inp),$inout1
  724. movdqu 0x20($inp),$inout2
  725. movdqu 0x30($inp),$inout3
  726. movdqu 0x40($inp),$inout4
  727. movdqu 0x50($inp),$inout5
  728. movdqu 0x60($inp),$inout6
  729. movdqu 0x70($inp),$inout7
  730. lea 0x80($inp),$inp # $inp+=8*16
  731. sub \$0x80,$len # $len-=8*16 (can be zero)
  732. jmp .Lecb_dec_loop8_enter
  733. .align 16
  734. .Lecb_dec_loop8:
  735. movups $inout0,($out) # store 8 output blocks
  736. mov $key_,$key # restore $key
  737. movdqu ($inp),$inout0 # load 8 input blocks
  738. mov $rnds_,$rounds # restore $rounds
  739. movups $inout1,0x10($out)
  740. movdqu 0x10($inp),$inout1
  741. movups $inout2,0x20($out)
  742. movdqu 0x20($inp),$inout2
  743. movups $inout3,0x30($out)
  744. movdqu 0x30($inp),$inout3
  745. movups $inout4,0x40($out)
  746. movdqu 0x40($inp),$inout4
  747. movups $inout5,0x50($out)
  748. movdqu 0x50($inp),$inout5
  749. movups $inout6,0x60($out)
  750. movdqu 0x60($inp),$inout6
  751. movups $inout7,0x70($out)
  752. lea 0x80($out),$out # $out+=8*16
  753. movdqu 0x70($inp),$inout7
  754. lea 0x80($inp),$inp # $inp+=8*16
  755. .Lecb_dec_loop8_enter:
  756. call _aesni_decrypt8
  757. $movkey ($key_),$rndkey0
  758. sub \$0x80,$len
  759. jnc .Lecb_dec_loop8 # loop if $len-=8*16 didn't borrow
  760. movups $inout0,($out) # store 8 output blocks
  761. pxor $inout0,$inout0 # clear register bank
  762. mov $key_,$key # restore $key
  763. movups $inout1,0x10($out)
  764. pxor $inout1,$inout1
  765. mov $rnds_,$rounds # restore $rounds
  766. movups $inout2,0x20($out)
  767. pxor $inout2,$inout2
  768. movups $inout3,0x30($out)
  769. pxor $inout3,$inout3
  770. movups $inout4,0x40($out)
  771. pxor $inout4,$inout4
  772. movups $inout5,0x50($out)
  773. pxor $inout5,$inout5
  774. movups $inout6,0x60($out)
  775. pxor $inout6,$inout6
  776. movups $inout7,0x70($out)
  777. pxor $inout7,$inout7
  778. lea 0x80($out),$out # $out+=8*16
  779. add \$0x80,$len # restore real remaining $len
  780. jz .Lecb_ret # done if ($len==0)
  781. .Lecb_dec_tail:
  782. movups ($inp),$inout0
  783. cmp \$0x20,$len
  784. jb .Lecb_dec_one
  785. movups 0x10($inp),$inout1
  786. je .Lecb_dec_two
  787. movups 0x20($inp),$inout2
  788. cmp \$0x40,$len
  789. jb .Lecb_dec_three
  790. movups 0x30($inp),$inout3
  791. je .Lecb_dec_four
  792. movups 0x40($inp),$inout4
  793. cmp \$0x60,$len
  794. jb .Lecb_dec_five
  795. movups 0x50($inp),$inout5
  796. je .Lecb_dec_six
  797. movups 0x60($inp),$inout6
  798. $movkey ($key),$rndkey0
  799. xorps $inout7,$inout7
  800. call _aesni_decrypt8
  801. movups $inout0,($out) # store 7 output blocks
  802. pxor $inout0,$inout0 # clear register bank
  803. movups $inout1,0x10($out)
  804. pxor $inout1,$inout1
  805. movups $inout2,0x20($out)
  806. pxor $inout2,$inout2
  807. movups $inout3,0x30($out)
  808. pxor $inout3,$inout3
  809. movups $inout4,0x40($out)
  810. pxor $inout4,$inout4
  811. movups $inout5,0x50($out)
  812. pxor $inout5,$inout5
  813. movups $inout6,0x60($out)
  814. pxor $inout6,$inout6
  815. pxor $inout7,$inout7
  816. jmp .Lecb_ret
  817. .align 16
  818. .Lecb_dec_one:
  819. ___
  820. &aesni_generate1("dec",$key,$rounds);
  821. $code.=<<___;
  822. movups $inout0,($out) # store one output block
  823. pxor $inout0,$inout0 # clear register bank
  824. jmp .Lecb_ret
  825. .align 16
  826. .Lecb_dec_two:
  827. call _aesni_decrypt2
  828. movups $inout0,($out) # store 2 output blocks
  829. pxor $inout0,$inout0 # clear register bank
  830. movups $inout1,0x10($out)
  831. pxor $inout1,$inout1
  832. jmp .Lecb_ret
  833. .align 16
  834. .Lecb_dec_three:
  835. call _aesni_decrypt3
  836. movups $inout0,($out) # store 3 output blocks
  837. pxor $inout0,$inout0 # clear register bank
  838. movups $inout1,0x10($out)
  839. pxor $inout1,$inout1
  840. movups $inout2,0x20($out)
  841. pxor $inout2,$inout2
  842. jmp .Lecb_ret
  843. .align 16
  844. .Lecb_dec_four:
  845. call _aesni_decrypt4
  846. movups $inout0,($out) # store 4 output blocks
  847. pxor $inout0,$inout0 # clear register bank
  848. movups $inout1,0x10($out)
  849. pxor $inout1,$inout1
  850. movups $inout2,0x20($out)
  851. pxor $inout2,$inout2
  852. movups $inout3,0x30($out)
  853. pxor $inout3,$inout3
  854. jmp .Lecb_ret
  855. .align 16
  856. .Lecb_dec_five:
  857. xorps $inout5,$inout5
  858. call _aesni_decrypt6
  859. movups $inout0,($out) # store 5 output blocks
  860. pxor $inout0,$inout0 # clear register bank
  861. movups $inout1,0x10($out)
  862. pxor $inout1,$inout1
  863. movups $inout2,0x20($out)
  864. pxor $inout2,$inout2
  865. movups $inout3,0x30($out)
  866. pxor $inout3,$inout3
  867. movups $inout4,0x40($out)
  868. pxor $inout4,$inout4
  869. pxor $inout5,$inout5
  870. jmp .Lecb_ret
  871. .align 16
  872. .Lecb_dec_six:
  873. call _aesni_decrypt6
  874. movups $inout0,($out) # store 6 output blocks
  875. pxor $inout0,$inout0 # clear register bank
  876. movups $inout1,0x10($out)
  877. pxor $inout1,$inout1
  878. movups $inout2,0x20($out)
  879. pxor $inout2,$inout2
  880. movups $inout3,0x30($out)
  881. pxor $inout3,$inout3
  882. movups $inout4,0x40($out)
  883. pxor $inout4,$inout4
  884. movups $inout5,0x50($out)
  885. pxor $inout5,$inout5
  886. .Lecb_ret:
  887. xorps $rndkey0,$rndkey0 # %xmm0
  888. pxor $rndkey1,$rndkey1
  889. ___
  890. $code.=<<___ if ($win64);
  891. movaps (%rsp),%xmm6
  892. movaps %xmm0,(%rsp) # clear stack
  893. movaps 0x10(%rsp),%xmm7
  894. movaps %xmm0,0x10(%rsp)
  895. movaps 0x20(%rsp),%xmm8
  896. movaps %xmm0,0x20(%rsp)
  897. movaps 0x30(%rsp),%xmm9
  898. movaps %xmm0,0x30(%rsp)
  899. lea 0x58(%rsp),%rsp
  900. .Lecb_enc_ret:
  901. ___
  902. $code.=<<___;
  903. ret
  904. .size aesni_ecb_encrypt,.-aesni_ecb_encrypt
  905. ___
  906. {
  907. ######################################################################
  908. # void aesni_ccm64_[en|de]crypt_blocks (const void *in, void *out,
  909. # size_t blocks, const AES_KEY *key,
  910. # const char *ivec,char *cmac);
  911. #
  912. # Handles only complete blocks, operates on 64-bit counter and
  913. # does not update *ivec! Nor does it finalize CMAC value
  914. # (see engine/eng_aesni.c for details)
  915. #
  916. {
  917. my $cmac="%r9"; # 6th argument
  918. my $increment="%xmm9";
  919. my $iv="%xmm6";
  920. my $bswap_mask="%xmm7";
  921. $code.=<<___;
  922. .globl aesni_ccm64_encrypt_blocks
  923. .type aesni_ccm64_encrypt_blocks,\@function,6
  924. .align 16
  925. aesni_ccm64_encrypt_blocks:
  926. ___
  927. $code.=<<___ if ($win64);
  928. lea -0x58(%rsp),%rsp
  929. movaps %xmm6,(%rsp) # $iv
  930. movaps %xmm7,0x10(%rsp) # $bswap_mask
  931. movaps %xmm8,0x20(%rsp) # $in0
  932. movaps %xmm9,0x30(%rsp) # $increment
  933. .Lccm64_enc_body:
  934. ___
  935. $code.=<<___;
  936. mov 240($key),$rounds # key->rounds
  937. movdqu ($ivp),$iv
  938. movdqa .Lincrement64(%rip),$increment
  939. movdqa .Lbswap_mask(%rip),$bswap_mask
  940. shl \$4,$rounds
  941. mov \$16,$rnds_
  942. lea 0($key),$key_
  943. movdqu ($cmac),$inout1
  944. movdqa $iv,$inout0
  945. lea 32($key,$rounds),$key # end of key schedule
  946. pshufb $bswap_mask,$iv
  947. sub %rax,%r10 # twisted $rounds
  948. jmp .Lccm64_enc_outer
  949. .align 16
  950. .Lccm64_enc_outer:
  951. $movkey ($key_),$rndkey0
  952. mov %r10,%rax
  953. movups ($inp),$in0 # load inp
  954. xorps $rndkey0,$inout0 # counter
  955. $movkey 16($key_),$rndkey1
  956. xorps $in0,$rndkey0
  957. xorps $rndkey0,$inout1 # cmac^=inp
  958. $movkey 32($key_),$rndkey0
  959. .Lccm64_enc2_loop:
  960. aesenc $rndkey1,$inout0
  961. aesenc $rndkey1,$inout1
  962. $movkey ($key,%rax),$rndkey1
  963. add \$32,%rax
  964. aesenc $rndkey0,$inout0
  965. aesenc $rndkey0,$inout1
  966. $movkey -16($key,%rax),$rndkey0
  967. jnz .Lccm64_enc2_loop
  968. aesenc $rndkey1,$inout0
  969. aesenc $rndkey1,$inout1
  970. paddq $increment,$iv
  971. dec $len # $len-- ($len is in blocks)
  972. aesenclast $rndkey0,$inout0
  973. aesenclast $rndkey0,$inout1
  974. lea 16($inp),$inp
  975. xorps $inout0,$in0 # inp ^= E(iv)
  976. movdqa $iv,$inout0
  977. movups $in0,($out) # save output
  978. pshufb $bswap_mask,$inout0
  979. lea 16($out),$out # $out+=16
  980. jnz .Lccm64_enc_outer # loop if ($len!=0)
  981. pxor $rndkey0,$rndkey0 # clear register bank
  982. pxor $rndkey1,$rndkey1
  983. pxor $inout0,$inout0
  984. movups $inout1,($cmac) # store resulting mac
  985. pxor $inout1,$inout1
  986. pxor $in0,$in0
  987. pxor $iv,$iv
  988. ___
  989. $code.=<<___ if ($win64);
  990. movaps (%rsp),%xmm6
  991. movaps %xmm0,(%rsp) # clear stack
  992. movaps 0x10(%rsp),%xmm7
  993. movaps %xmm0,0x10(%rsp)
  994. movaps 0x20(%rsp),%xmm8
  995. movaps %xmm0,0x20(%rsp)
  996. movaps 0x30(%rsp),%xmm9
  997. movaps %xmm0,0x30(%rsp)
  998. lea 0x58(%rsp),%rsp
  999. .Lccm64_enc_ret:
  1000. ___
  1001. $code.=<<___;
  1002. ret
  1003. .size aesni_ccm64_encrypt_blocks,.-aesni_ccm64_encrypt_blocks
  1004. ___
  1005. ######################################################################
  1006. $code.=<<___;
  1007. .globl aesni_ccm64_decrypt_blocks
  1008. .type aesni_ccm64_decrypt_blocks,\@function,6
  1009. .align 16
  1010. aesni_ccm64_decrypt_blocks:
  1011. ___
  1012. $code.=<<___ if ($win64);
  1013. lea -0x58(%rsp),%rsp
  1014. movaps %xmm6,(%rsp) # $iv
  1015. movaps %xmm7,0x10(%rsp) # $bswap_mask
  1016. movaps %xmm8,0x20(%rsp) # $in8
  1017. movaps %xmm9,0x30(%rsp) # $increment
  1018. .Lccm64_dec_body:
  1019. ___
  1020. $code.=<<___;
  1021. mov 240($key),$rounds # key->rounds
  1022. movups ($ivp),$iv
  1023. movdqu ($cmac),$inout1
  1024. movdqa .Lincrement64(%rip),$increment
  1025. movdqa .Lbswap_mask(%rip),$bswap_mask
  1026. movaps $iv,$inout0
  1027. mov $rounds,$rnds_
  1028. mov $key,$key_
  1029. pshufb $bswap_mask,$iv
  1030. ___
  1031. &aesni_generate1("enc",$key,$rounds);
  1032. $code.=<<___;
  1033. shl \$4,$rnds_
  1034. mov \$16,$rounds
  1035. movups ($inp),$in0 # load inp
  1036. paddq $increment,$iv
  1037. lea 16($inp),$inp # $inp+=16
  1038. sub %r10,%rax # twisted $rounds
  1039. lea 32($key_,$rnds_),$key # end of key schedule
  1040. mov %rax,%r10
  1041. jmp .Lccm64_dec_outer
  1042. .align 16
  1043. .Lccm64_dec_outer:
  1044. xorps $inout0,$in0 # inp ^= E(iv)
  1045. movdqa $iv,$inout0
  1046. movups $in0,($out) # save output
  1047. lea 16($out),$out # $out+=16
  1048. pshufb $bswap_mask,$inout0
  1049. sub \$1,$len # $len-- ($len is in blocks)
  1050. jz .Lccm64_dec_break # if ($len==0) break
  1051. $movkey ($key_),$rndkey0
  1052. mov %r10,%rax
  1053. $movkey 16($key_),$rndkey1
  1054. xorps $rndkey0,$in0
  1055. xorps $rndkey0,$inout0
  1056. xorps $in0,$inout1 # cmac^=out
  1057. $movkey 32($key_),$rndkey0
  1058. jmp .Lccm64_dec2_loop
  1059. .align 16
  1060. .Lccm64_dec2_loop:
  1061. aesenc $rndkey1,$inout0
  1062. aesenc $rndkey1,$inout1
  1063. $movkey ($key,%rax),$rndkey1
  1064. add \$32,%rax
  1065. aesenc $rndkey0,$inout0
  1066. aesenc $rndkey0,$inout1
  1067. $movkey -16($key,%rax),$rndkey0
  1068. jnz .Lccm64_dec2_loop
  1069. movups ($inp),$in0 # load input
  1070. paddq $increment,$iv
  1071. aesenc $rndkey1,$inout0
  1072. aesenc $rndkey1,$inout1
  1073. aesenclast $rndkey0,$inout0
  1074. aesenclast $rndkey0,$inout1
  1075. lea 16($inp),$inp # $inp+=16
  1076. jmp .Lccm64_dec_outer
  1077. .align 16
  1078. .Lccm64_dec_break:
  1079. #xorps $in0,$inout1 # cmac^=out
  1080. mov 240($key_),$rounds
  1081. ___
  1082. &aesni_generate1("enc",$key_,$rounds,$inout1,$in0);
  1083. $code.=<<___;
  1084. pxor $rndkey0,$rndkey0 # clear register bank
  1085. pxor $rndkey1,$rndkey1
  1086. pxor $inout0,$inout0
  1087. movups $inout1,($cmac) # store resulting mac
  1088. pxor $inout1,$inout1
  1089. pxor $in0,$in0
  1090. pxor $iv,$iv
  1091. ___
  1092. $code.=<<___ if ($win64);
  1093. movaps (%rsp),%xmm6
  1094. movaps %xmm0,(%rsp) # clear stack
  1095. movaps 0x10(%rsp),%xmm7
  1096. movaps %xmm0,0x10(%rsp)
  1097. movaps 0x20(%rsp),%xmm8
  1098. movaps %xmm0,0x20(%rsp)
  1099. movaps 0x30(%rsp),%xmm9
  1100. movaps %xmm0,0x30(%rsp)
  1101. lea 0x58(%rsp),%rsp
  1102. .Lccm64_dec_ret:
  1103. ___
  1104. $code.=<<___;
  1105. ret
  1106. .size aesni_ccm64_decrypt_blocks,.-aesni_ccm64_decrypt_blocks
  1107. ___
  1108. }
  1109. ######################################################################
  1110. # void aesni_ctr32_encrypt_blocks (const void *in, void *out,
  1111. # size_t blocks, const AES_KEY *key,
  1112. # const char *ivec);
  1113. #
  1114. # Handles only complete blocks, operates on 32-bit counter and
  1115. # does not update *ivec! (see crypto/modes/ctr128.c for details)
  1116. #
  1117. # Overhaul based on suggestions from Shay Gueron and Vlad Krasnov,
  1118. # http://rt.openssl.org/Ticket/Display.html?id=3021&user=guest&pass=guest.
  1119. # Keywords are full unroll and modulo-schedule counter calculations
  1120. # with zero-round key xor.
  1121. {
  1122. my ($in0,$in1,$in2,$in3,$in4,$in5)=map("%xmm$_",(10..15));
  1123. my ($key0,$ctr)=("%ebp","${ivp}d");
  1124. my $frame_size = 0x80 + ($win64?160:0);
  1125. $code.=<<___;
  1126. .globl aesni_ctr32_encrypt_blocks
  1127. .type aesni_ctr32_encrypt_blocks,\@function,5
  1128. .align 16
  1129. aesni_ctr32_encrypt_blocks:
  1130. .cfi_startproc
  1131. cmp \$1,$len
  1132. jne .Lctr32_bulk
  1133. # handle single block without allocating stack frame,
  1134. # useful when handling edges
  1135. movups ($ivp),$inout0
  1136. movups ($inp),$inout1
  1137. mov 240($key),%edx # key->rounds
  1138. ___
  1139. &aesni_generate1("enc",$key,"%edx");
  1140. $code.=<<___;
  1141. pxor $rndkey0,$rndkey0 # clear register bank
  1142. pxor $rndkey1,$rndkey1
  1143. xorps $inout1,$inout0
  1144. pxor $inout1,$inout1
  1145. movups $inout0,($out)
  1146. xorps $inout0,$inout0
  1147. jmp .Lctr32_epilogue
  1148. .align 16
  1149. .Lctr32_bulk:
  1150. lea (%rsp),$key_ # use $key_ as frame pointer
  1151. .cfi_def_cfa_register $key_
  1152. push %rbp
  1153. .cfi_push %rbp
  1154. sub \$$frame_size,%rsp
  1155. and \$-16,%rsp # Linux kernel stack can be incorrectly seeded
  1156. ___
  1157. $code.=<<___ if ($win64);
  1158. movaps %xmm6,-0xa8($key_) # offload everything
  1159. movaps %xmm7,-0x98($key_)
  1160. movaps %xmm8,-0x88($key_)
  1161. movaps %xmm9,-0x78($key_)
  1162. movaps %xmm10,-0x68($key_)
  1163. movaps %xmm11,-0x58($key_)
  1164. movaps %xmm12,-0x48($key_)
  1165. movaps %xmm13,-0x38($key_)
  1166. movaps %xmm14,-0x28($key_)
  1167. movaps %xmm15,-0x18($key_)
  1168. .Lctr32_body:
  1169. ___
  1170. $code.=<<___;
  1171. # 8 16-byte words on top of stack are counter values
  1172. # xor-ed with zero-round key
  1173. movdqu ($ivp),$inout0
  1174. movdqu ($key),$rndkey0
  1175. mov 12($ivp),$ctr # counter LSB
  1176. pxor $rndkey0,$inout0
  1177. mov 12($key),$key0 # 0-round key LSB
  1178. movdqa $inout0,0x00(%rsp) # populate counter block
  1179. bswap $ctr
  1180. movdqa $inout0,$inout1
  1181. movdqa $inout0,$inout2
  1182. movdqa $inout0,$inout3
  1183. movdqa $inout0,0x40(%rsp)
  1184. movdqa $inout0,0x50(%rsp)
  1185. movdqa $inout0,0x60(%rsp)
  1186. mov %rdx,%r10 # about to borrow %rdx
  1187. movdqa $inout0,0x70(%rsp)
  1188. lea 1($ctr),%rax
  1189. lea 2($ctr),%rdx
  1190. bswap %eax
  1191. bswap %edx
  1192. xor $key0,%eax
  1193. xor $key0,%edx
  1194. pinsrd \$3,%eax,$inout1
  1195. lea 3($ctr),%rax
  1196. movdqa $inout1,0x10(%rsp)
  1197. pinsrd \$3,%edx,$inout2
  1198. bswap %eax
  1199. mov %r10,%rdx # restore %rdx
  1200. lea 4($ctr),%r10
  1201. movdqa $inout2,0x20(%rsp)
  1202. xor $key0,%eax
  1203. bswap %r10d
  1204. pinsrd \$3,%eax,$inout3
  1205. xor $key0,%r10d
  1206. movdqa $inout3,0x30(%rsp)
  1207. lea 5($ctr),%r9
  1208. mov %r10d,0x40+12(%rsp)
  1209. bswap %r9d
  1210. lea 6($ctr),%r10
  1211. mov 240($key),$rounds # key->rounds
  1212. xor $key0,%r9d
  1213. bswap %r10d
  1214. mov %r9d,0x50+12(%rsp)
  1215. xor $key0,%r10d
  1216. lea 7($ctr),%r9
  1217. mov %r10d,0x60+12(%rsp)
  1218. bswap %r9d
  1219. mov OPENSSL_ia32cap_P+4(%rip),%r10d
  1220. xor $key0,%r9d
  1221. and \$`1<<26|1<<22`,%r10d # isolate XSAVE+MOVBE
  1222. mov %r9d,0x70+12(%rsp)
  1223. $movkey 0x10($key),$rndkey1
  1224. movdqa 0x40(%rsp),$inout4
  1225. movdqa 0x50(%rsp),$inout5
  1226. cmp \$8,$len # $len is in blocks
  1227. jb .Lctr32_tail # short input if ($len<8)
  1228. sub \$6,$len # $len is biased by -6
  1229. cmp \$`1<<22`,%r10d # check for MOVBE without XSAVE
  1230. je .Lctr32_6x # [which denotes Atom Silvermont]
  1231. lea 0x80($key),$key # size optimization
  1232. sub \$2,$len # $len is biased by -8
  1233. jmp .Lctr32_loop8
  1234. .align 16
  1235. .Lctr32_6x:
  1236. shl \$4,$rounds
  1237. mov \$48,$rnds_
  1238. bswap $key0
  1239. lea 32($key,$rounds),$key # end of key schedule
  1240. sub %rax,%r10 # twisted $rounds
  1241. jmp .Lctr32_loop6
  1242. .align 16
  1243. .Lctr32_loop6:
  1244. add \$6,$ctr # next counter value
  1245. $movkey -48($key,$rnds_),$rndkey0
  1246. aesenc $rndkey1,$inout0
  1247. mov $ctr,%eax
  1248. xor $key0,%eax
  1249. aesenc $rndkey1,$inout1
  1250. movbe %eax,`0x00+12`(%rsp) # store next counter value
  1251. lea 1($ctr),%eax
  1252. aesenc $rndkey1,$inout2
  1253. xor $key0,%eax
  1254. movbe %eax,`0x10+12`(%rsp)
  1255. aesenc $rndkey1,$inout3
  1256. lea 2($ctr),%eax
  1257. xor $key0,%eax
  1258. aesenc $rndkey1,$inout4
  1259. movbe %eax,`0x20+12`(%rsp)
  1260. lea 3($ctr),%eax
  1261. aesenc $rndkey1,$inout5
  1262. $movkey -32($key,$rnds_),$rndkey1
  1263. xor $key0,%eax
  1264. aesenc $rndkey0,$inout0
  1265. movbe %eax,`0x30+12`(%rsp)
  1266. lea 4($ctr),%eax
  1267. aesenc $rndkey0,$inout1
  1268. xor $key0,%eax
  1269. movbe %eax,`0x40+12`(%rsp)
  1270. aesenc $rndkey0,$inout2
  1271. lea 5($ctr),%eax
  1272. xor $key0,%eax
  1273. aesenc $rndkey0,$inout3
  1274. movbe %eax,`0x50+12`(%rsp)
  1275. mov %r10,%rax # mov $rnds_,$rounds
  1276. aesenc $rndkey0,$inout4
  1277. aesenc $rndkey0,$inout5
  1278. $movkey -16($key,$rnds_),$rndkey0
  1279. call .Lenc_loop6
  1280. movdqu ($inp),$inout6 # load 6 input blocks
  1281. movdqu 0x10($inp),$inout7
  1282. movdqu 0x20($inp),$in0
  1283. movdqu 0x30($inp),$in1
  1284. movdqu 0x40($inp),$in2
  1285. movdqu 0x50($inp),$in3
  1286. lea 0x60($inp),$inp # $inp+=6*16
  1287. $movkey -64($key,$rnds_),$rndkey1
  1288. pxor $inout0,$inout6 # inp^=E(ctr)
  1289. movaps 0x00(%rsp),$inout0 # load next counter [xor-ed with 0 round]
  1290. pxor $inout1,$inout7
  1291. movaps 0x10(%rsp),$inout1
  1292. pxor $inout2,$in0
  1293. movaps 0x20(%rsp),$inout2
  1294. pxor $inout3,$in1
  1295. movaps 0x30(%rsp),$inout3
  1296. pxor $inout4,$in2
  1297. movaps 0x40(%rsp),$inout4
  1298. pxor $inout5,$in3
  1299. movaps 0x50(%rsp),$inout5
  1300. movdqu $inout6,($out) # store 6 output blocks
  1301. movdqu $inout7,0x10($out)
  1302. movdqu $in0,0x20($out)
  1303. movdqu $in1,0x30($out)
  1304. movdqu $in2,0x40($out)
  1305. movdqu $in3,0x50($out)
  1306. lea 0x60($out),$out # $out+=6*16
  1307. sub \$6,$len
  1308. jnc .Lctr32_loop6 # loop if $len-=6 didn't borrow
  1309. add \$6,$len # restore real remaining $len
  1310. jz .Lctr32_done # done if ($len==0)
  1311. lea -48($rnds_),$rounds
  1312. lea -80($key,$rnds_),$key # restore $key
  1313. neg $rounds
  1314. shr \$4,$rounds # restore $rounds
  1315. jmp .Lctr32_tail
  1316. .align 32
  1317. .Lctr32_loop8:
  1318. add \$8,$ctr # next counter value
  1319. movdqa 0x60(%rsp),$inout6
  1320. aesenc $rndkey1,$inout0
  1321. mov $ctr,%r9d
  1322. movdqa 0x70(%rsp),$inout7
  1323. aesenc $rndkey1,$inout1
  1324. bswap %r9d
  1325. $movkey 0x20-0x80($key),$rndkey0
  1326. aesenc $rndkey1,$inout2
  1327. xor $key0,%r9d
  1328. nop
  1329. aesenc $rndkey1,$inout3
  1330. mov %r9d,0x00+12(%rsp) # store next counter value
  1331. lea 1($ctr),%r9
  1332. aesenc $rndkey1,$inout4
  1333. aesenc $rndkey1,$inout5
  1334. aesenc $rndkey1,$inout6
  1335. aesenc $rndkey1,$inout7
  1336. $movkey 0x30-0x80($key),$rndkey1
  1337. ___
  1338. for($i=2;$i<8;$i++) {
  1339. my $rndkeyx = ($i&1)?$rndkey1:$rndkey0;
  1340. $code.=<<___;
  1341. bswap %r9d
  1342. aesenc $rndkeyx,$inout0
  1343. aesenc $rndkeyx,$inout1
  1344. xor $key0,%r9d
  1345. .byte 0x66,0x90
  1346. aesenc $rndkeyx,$inout2
  1347. aesenc $rndkeyx,$inout3
  1348. mov %r9d,`0x10*($i-1)`+12(%rsp)
  1349. lea $i($ctr),%r9
  1350. aesenc $rndkeyx,$inout4
  1351. aesenc $rndkeyx,$inout5
  1352. aesenc $rndkeyx,$inout6
  1353. aesenc $rndkeyx,$inout7
  1354. $movkey `0x20+0x10*$i`-0x80($key),$rndkeyx
  1355. ___
  1356. }
  1357. $code.=<<___;
  1358. bswap %r9d
  1359. aesenc $rndkey0,$inout0
  1360. aesenc $rndkey0,$inout1
  1361. aesenc $rndkey0,$inout2
  1362. xor $key0,%r9d
  1363. movdqu 0x00($inp),$in0 # start loading input
  1364. aesenc $rndkey0,$inout3
  1365. mov %r9d,0x70+12(%rsp)
  1366. cmp \$11,$rounds
  1367. aesenc $rndkey0,$inout4
  1368. aesenc $rndkey0,$inout5
  1369. aesenc $rndkey0,$inout6
  1370. aesenc $rndkey0,$inout7
  1371. $movkey 0xa0-0x80($key),$rndkey0
  1372. jb .Lctr32_enc_done
  1373. aesenc $rndkey1,$inout0
  1374. aesenc $rndkey1,$inout1
  1375. aesenc $rndkey1,$inout2
  1376. aesenc $rndkey1,$inout3
  1377. aesenc $rndkey1,$inout4
  1378. aesenc $rndkey1,$inout5
  1379. aesenc $rndkey1,$inout6
  1380. aesenc $rndkey1,$inout7
  1381. $movkey 0xb0-0x80($key),$rndkey1
  1382. aesenc $rndkey0,$inout0
  1383. aesenc $rndkey0,$inout1
  1384. aesenc $rndkey0,$inout2
  1385. aesenc $rndkey0,$inout3
  1386. aesenc $rndkey0,$inout4
  1387. aesenc $rndkey0,$inout5
  1388. aesenc $rndkey0,$inout6
  1389. aesenc $rndkey0,$inout7
  1390. $movkey 0xc0-0x80($key),$rndkey0
  1391. je .Lctr32_enc_done
  1392. aesenc $rndkey1,$inout0
  1393. aesenc $rndkey1,$inout1
  1394. aesenc $rndkey1,$inout2
  1395. aesenc $rndkey1,$inout3
  1396. aesenc $rndkey1,$inout4
  1397. aesenc $rndkey1,$inout5
  1398. aesenc $rndkey1,$inout6
  1399. aesenc $rndkey1,$inout7
  1400. $movkey 0xd0-0x80($key),$rndkey1
  1401. aesenc $rndkey0,$inout0
  1402. aesenc $rndkey0,$inout1
  1403. aesenc $rndkey0,$inout2
  1404. aesenc $rndkey0,$inout3
  1405. aesenc $rndkey0,$inout4
  1406. aesenc $rndkey0,$inout5
  1407. aesenc $rndkey0,$inout6
  1408. aesenc $rndkey0,$inout7
  1409. $movkey 0xe0-0x80($key),$rndkey0
  1410. jmp .Lctr32_enc_done
  1411. .align 16
  1412. .Lctr32_enc_done:
  1413. movdqu 0x10($inp),$in1
  1414. pxor $rndkey0,$in0 # input^=round[last]
  1415. movdqu 0x20($inp),$in2
  1416. pxor $rndkey0,$in1
  1417. movdqu 0x30($inp),$in3
  1418. pxor $rndkey0,$in2
  1419. movdqu 0x40($inp),$in4
  1420. pxor $rndkey0,$in3
  1421. movdqu 0x50($inp),$in5
  1422. pxor $rndkey0,$in4
  1423. pxor $rndkey0,$in5
  1424. aesenc $rndkey1,$inout0
  1425. aesenc $rndkey1,$inout1
  1426. aesenc $rndkey1,$inout2
  1427. aesenc $rndkey1,$inout3
  1428. aesenc $rndkey1,$inout4
  1429. aesenc $rndkey1,$inout5
  1430. aesenc $rndkey1,$inout6
  1431. aesenc $rndkey1,$inout7
  1432. movdqu 0x60($inp),$rndkey1 # borrow $rndkey1 for inp[6]
  1433. lea 0x80($inp),$inp # $inp+=8*16
  1434. aesenclast $in0,$inout0 # $inN is inp[N]^round[last]
  1435. pxor $rndkey0,$rndkey1 # borrowed $rndkey
  1436. movdqu 0x70-0x80($inp),$in0
  1437. aesenclast $in1,$inout1
  1438. pxor $rndkey0,$in0
  1439. movdqa 0x00(%rsp),$in1 # load next counter block
  1440. aesenclast $in2,$inout2
  1441. aesenclast $in3,$inout3
  1442. movdqa 0x10(%rsp),$in2
  1443. movdqa 0x20(%rsp),$in3
  1444. aesenclast $in4,$inout4
  1445. aesenclast $in5,$inout5
  1446. movdqa 0x30(%rsp),$in4
  1447. movdqa 0x40(%rsp),$in5
  1448. aesenclast $rndkey1,$inout6
  1449. movdqa 0x50(%rsp),$rndkey0
  1450. $movkey 0x10-0x80($key),$rndkey1#real 1st-round key
  1451. aesenclast $in0,$inout7
  1452. movups $inout0,($out) # store 8 output blocks
  1453. movdqa $in1,$inout0
  1454. movups $inout1,0x10($out)
  1455. movdqa $in2,$inout1
  1456. movups $inout2,0x20($out)
  1457. movdqa $in3,$inout2
  1458. movups $inout3,0x30($out)
  1459. movdqa $in4,$inout3
  1460. movups $inout4,0x40($out)
  1461. movdqa $in5,$inout4
  1462. movups $inout5,0x50($out)
  1463. movdqa $rndkey0,$inout5
  1464. movups $inout6,0x60($out)
  1465. movups $inout7,0x70($out)
  1466. lea 0x80($out),$out # $out+=8*16
  1467. sub \$8,$len
  1468. jnc .Lctr32_loop8 # loop if $len-=8 didn't borrow
  1469. add \$8,$len # restore real remaining $len
  1470. jz .Lctr32_done # done if ($len==0)
  1471. lea -0x80($key),$key
  1472. .Lctr32_tail:
  1473. # note that at this point $inout0..5 are populated with
  1474. # counter values xor-ed with 0-round key
  1475. lea 16($key),$key
  1476. cmp \$4,$len
  1477. jb .Lctr32_loop3
  1478. je .Lctr32_loop4
  1479. # if ($len>4) compute 7 E(counter)
  1480. shl \$4,$rounds
  1481. movdqa 0x60(%rsp),$inout6
  1482. pxor $inout7,$inout7
  1483. $movkey 16($key),$rndkey0
  1484. aesenc $rndkey1,$inout0
  1485. aesenc $rndkey1,$inout1
  1486. lea 32-16($key,$rounds),$key# prepare for .Lenc_loop8_enter
  1487. neg %rax
  1488. aesenc $rndkey1,$inout2
  1489. add \$16,%rax # prepare for .Lenc_loop8_enter
  1490. movups ($inp),$in0
  1491. aesenc $rndkey1,$inout3
  1492. aesenc $rndkey1,$inout4
  1493. movups 0x10($inp),$in1 # pre-load input
  1494. movups 0x20($inp),$in2
  1495. aesenc $rndkey1,$inout5
  1496. aesenc $rndkey1,$inout6
  1497. call .Lenc_loop8_enter
  1498. movdqu 0x30($inp),$in3
  1499. pxor $in0,$inout0
  1500. movdqu 0x40($inp),$in0
  1501. pxor $in1,$inout1
  1502. movdqu $inout0,($out) # store output
  1503. pxor $in2,$inout2
  1504. movdqu $inout1,0x10($out)
  1505. pxor $in3,$inout3
  1506. movdqu $inout2,0x20($out)
  1507. pxor $in0,$inout4
  1508. movdqu $inout3,0x30($out)
  1509. movdqu $inout4,0x40($out)
  1510. cmp \$6,$len
  1511. jb .Lctr32_done # $len was 5, stop store
  1512. movups 0x50($inp),$in1
  1513. xorps $in1,$inout5
  1514. movups $inout5,0x50($out)
  1515. je .Lctr32_done # $len was 6, stop store
  1516. movups 0x60($inp),$in2
  1517. xorps $in2,$inout6
  1518. movups $inout6,0x60($out)
  1519. jmp .Lctr32_done # $len was 7, stop store
  1520. .align 32
  1521. .Lctr32_loop4:
  1522. aesenc $rndkey1,$inout0
  1523. lea 16($key),$key
  1524. dec $rounds
  1525. aesenc $rndkey1,$inout1
  1526. aesenc $rndkey1,$inout2
  1527. aesenc $rndkey1,$inout3
  1528. $movkey ($key),$rndkey1
  1529. jnz .Lctr32_loop4
  1530. aesenclast $rndkey1,$inout0
  1531. aesenclast $rndkey1,$inout1
  1532. movups ($inp),$in0 # load input
  1533. movups 0x10($inp),$in1
  1534. aesenclast $rndkey1,$inout2
  1535. aesenclast $rndkey1,$inout3
  1536. movups 0x20($inp),$in2
  1537. movups 0x30($inp),$in3
  1538. xorps $in0,$inout0
  1539. movups $inout0,($out) # store output
  1540. xorps $in1,$inout1
  1541. movups $inout1,0x10($out)
  1542. pxor $in2,$inout2
  1543. movdqu $inout2,0x20($out)
  1544. pxor $in3,$inout3
  1545. movdqu $inout3,0x30($out)
  1546. jmp .Lctr32_done # $len was 4, stop store
  1547. .align 32
  1548. .Lctr32_loop3:
  1549. aesenc $rndkey1,$inout0
  1550. lea 16($key),$key
  1551. dec $rounds
  1552. aesenc $rndkey1,$inout1
  1553. aesenc $rndkey1,$inout2
  1554. $movkey ($key),$rndkey1
  1555. jnz .Lctr32_loop3
  1556. aesenclast $rndkey1,$inout0
  1557. aesenclast $rndkey1,$inout1
  1558. aesenclast $rndkey1,$inout2
  1559. movups ($inp),$in0 # load input
  1560. xorps $in0,$inout0
  1561. movups $inout0,($out) # store output
  1562. cmp \$2,$len
  1563. jb .Lctr32_done # $len was 1, stop store
  1564. movups 0x10($inp),$in1
  1565. xorps $in1,$inout1
  1566. movups $inout1,0x10($out)
  1567. je .Lctr32_done # $len was 2, stop store
  1568. movups 0x20($inp),$in2
  1569. xorps $in2,$inout2
  1570. movups $inout2,0x20($out) # $len was 3, stop store
  1571. .Lctr32_done:
  1572. xorps %xmm0,%xmm0 # clear register bank
  1573. xor $key0,$key0
  1574. pxor %xmm1,%xmm1
  1575. pxor %xmm2,%xmm2
  1576. pxor %xmm3,%xmm3
  1577. pxor %xmm4,%xmm4
  1578. pxor %xmm5,%xmm5
  1579. ___
  1580. $code.=<<___ if (!$win64);
  1581. pxor %xmm6,%xmm6
  1582. pxor %xmm7,%xmm7
  1583. movaps %xmm0,0x00(%rsp) # clear stack
  1584. pxor %xmm8,%xmm8
  1585. movaps %xmm0,0x10(%rsp)
  1586. pxor %xmm9,%xmm9
  1587. movaps %xmm0,0x20(%rsp)
  1588. pxor %xmm10,%xmm10
  1589. movaps %xmm0,0x30(%rsp)
  1590. pxor %xmm11,%xmm11
  1591. movaps %xmm0,0x40(%rsp)
  1592. pxor %xmm12,%xmm12
  1593. movaps %xmm0,0x50(%rsp)
  1594. pxor %xmm13,%xmm13
  1595. movaps %xmm0,0x60(%rsp)
  1596. pxor %xmm14,%xmm14
  1597. movaps %xmm0,0x70(%rsp)
  1598. pxor %xmm15,%xmm15
  1599. ___
  1600. $code.=<<___ if ($win64);
  1601. movaps -0xa8($key_),%xmm6
  1602. movaps %xmm0,-0xa8($key_) # clear stack
  1603. movaps -0x98($key_),%xmm7
  1604. movaps %xmm0,-0x98($key_)
  1605. movaps -0x88($key_),%xmm8
  1606. movaps %xmm0,-0x88($key_)
  1607. movaps -0x78($key_),%xmm9
  1608. movaps %xmm0,-0x78($key_)
  1609. movaps -0x68($key_),%xmm10
  1610. movaps %xmm0,-0x68($key_)
  1611. movaps -0x58($key_),%xmm11
  1612. movaps %xmm0,-0x58($key_)
  1613. movaps -0x48($key_),%xmm12
  1614. movaps %xmm0,-0x48($key_)
  1615. movaps -0x38($key_),%xmm13
  1616. movaps %xmm0,-0x38($key_)
  1617. movaps -0x28($key_),%xmm14
  1618. movaps %xmm0,-0x28($key_)
  1619. movaps -0x18($key_),%xmm15
  1620. movaps %xmm0,-0x18($key_)
  1621. movaps %xmm0,0x00(%rsp)
  1622. movaps %xmm0,0x10(%rsp)
  1623. movaps %xmm0,0x20(%rsp)
  1624. movaps %xmm0,0x30(%rsp)
  1625. movaps %xmm0,0x40(%rsp)
  1626. movaps %xmm0,0x50(%rsp)
  1627. movaps %xmm0,0x60(%rsp)
  1628. movaps %xmm0,0x70(%rsp)
  1629. ___
  1630. $code.=<<___;
  1631. mov -8($key_),%rbp
  1632. .cfi_restore %rbp
  1633. lea ($key_),%rsp
  1634. .cfi_def_cfa_register %rsp
  1635. .Lctr32_epilogue:
  1636. ret
  1637. .cfi_endproc
  1638. .size aesni_ctr32_encrypt_blocks,.-aesni_ctr32_encrypt_blocks
  1639. ___
  1640. }
  1641. ######################################################################
  1642. # void aesni_xts_[en|de]crypt(const char *inp,char *out,size_t len,
  1643. # const AES_KEY *key1, const AES_KEY *key2
  1644. # const unsigned char iv[16]);
  1645. #
  1646. {
  1647. my @tweak=map("%xmm$_",(10..15));
  1648. my ($twmask,$twres,$twtmp)=("%xmm8","%xmm9",@tweak[4]);
  1649. my ($key2,$ivp,$len_)=("%r8","%r9","%r9");
  1650. my $frame_size = 0x70 + ($win64?160:0);
  1651. my $key_ = "%rbp"; # override so that we can use %r11 as FP
  1652. $code.=<<___;
  1653. .globl aesni_xts_encrypt
  1654. .type aesni_xts_encrypt,\@function,6
  1655. .align 16
  1656. aesni_xts_encrypt:
  1657. .cfi_startproc
  1658. lea (%rsp),%r11 # frame pointer
  1659. .cfi_def_cfa_register %r11
  1660. push %rbp
  1661. .cfi_push %rbp
  1662. sub \$$frame_size,%rsp
  1663. and \$-16,%rsp # Linux kernel stack can be incorrectly seeded
  1664. ___
  1665. $code.=<<___ if ($win64);
  1666. movaps %xmm6,-0xa8(%r11) # offload everything
  1667. movaps %xmm7,-0x98(%r11)
  1668. movaps %xmm8,-0x88(%r11)
  1669. movaps %xmm9,-0x78(%r11)
  1670. movaps %xmm10,-0x68(%r11)
  1671. movaps %xmm11,-0x58(%r11)
  1672. movaps %xmm12,-0x48(%r11)
  1673. movaps %xmm13,-0x38(%r11)
  1674. movaps %xmm14,-0x28(%r11)
  1675. movaps %xmm15,-0x18(%r11)
  1676. .Lxts_enc_body:
  1677. ___
  1678. $code.=<<___;
  1679. movups ($ivp),$inout0 # load clear-text tweak
  1680. mov 240(%r8),$rounds # key2->rounds
  1681. mov 240($key),$rnds_ # key1->rounds
  1682. ___
  1683. # generate the tweak
  1684. &aesni_generate1("enc",$key2,$rounds,$inout0);
  1685. $code.=<<___;
  1686. $movkey ($key),$rndkey0 # zero round key
  1687. mov $key,$key_ # backup $key
  1688. mov $rnds_,$rounds # backup $rounds
  1689. shl \$4,$rnds_
  1690. mov $len,$len_ # backup $len
  1691. and \$-16,$len
  1692. $movkey 16($key,$rnds_),$rndkey1 # last round key
  1693. movdqa .Lxts_magic(%rip),$twmask
  1694. movdqa $inout0,@tweak[5]
  1695. pshufd \$0x5f,$inout0,$twres
  1696. pxor $rndkey0,$rndkey1
  1697. ___
  1698. # alternative tweak calculation algorithm is based on suggestions
  1699. # by Shay Gueron. psrad doesn't conflict with AES-NI instructions
  1700. # and should help in the future...
  1701. for ($i=0;$i<4;$i++) {
  1702. $code.=<<___;
  1703. movdqa $twres,$twtmp
  1704. paddd $twres,$twres
  1705. movdqa @tweak[5],@tweak[$i]
  1706. psrad \$31,$twtmp # broadcast upper bits
  1707. paddq @tweak[5],@tweak[5]
  1708. pand $twmask,$twtmp
  1709. pxor $rndkey0,@tweak[$i]
  1710. pxor $twtmp,@tweak[5]
  1711. ___
  1712. }
  1713. $code.=<<___;
  1714. movdqa @tweak[5],@tweak[4]
  1715. psrad \$31,$twres
  1716. paddq @tweak[5],@tweak[5]
  1717. pand $twmask,$twres
  1718. pxor $rndkey0,@tweak[4]
  1719. pxor $twres,@tweak[5]
  1720. movaps $rndkey1,0x60(%rsp) # save round[0]^round[last]
  1721. sub \$16*6,$len
  1722. jc .Lxts_enc_short # if $len-=6*16 borrowed
  1723. mov \$16+96,$rounds
  1724. lea 32($key_,$rnds_),$key # end of key schedule
  1725. sub %r10,%rax # twisted $rounds
  1726. $movkey 16($key_),$rndkey1
  1727. mov %rax,%r10 # backup twisted $rounds
  1728. lea .Lxts_magic(%rip),%r8
  1729. jmp .Lxts_enc_grandloop
  1730. .align 32
  1731. .Lxts_enc_grandloop:
  1732. movdqu `16*0`($inp),$inout0 # load input
  1733. movdqa $rndkey0,$twmask
  1734. movdqu `16*1`($inp),$inout1
  1735. pxor @tweak[0],$inout0 # input^=tweak^round[0]
  1736. movdqu `16*2`($inp),$inout2
  1737. pxor @tweak[1],$inout1
  1738. aesenc $rndkey1,$inout0
  1739. movdqu `16*3`($inp),$inout3
  1740. pxor @tweak[2],$inout2
  1741. aesenc $rndkey1,$inout1
  1742. movdqu `16*4`($inp),$inout4
  1743. pxor @tweak[3],$inout3
  1744. aesenc $rndkey1,$inout2
  1745. movdqu `16*5`($inp),$inout5
  1746. pxor @tweak[5],$twmask # round[0]^=tweak[5]
  1747. movdqa 0x60(%rsp),$twres # load round[0]^round[last]
  1748. pxor @tweak[4],$inout4
  1749. aesenc $rndkey1,$inout3
  1750. $movkey 32($key_),$rndkey0
  1751. lea `16*6`($inp),$inp
  1752. pxor $twmask,$inout5
  1753. pxor $twres,@tweak[0] # calculate tweaks^round[last]
  1754. aesenc $rndkey1,$inout4
  1755. pxor $twres,@tweak[1]
  1756. movdqa @tweak[0],`16*0`(%rsp) # put aside tweaks^round[last]
  1757. aesenc $rndkey1,$inout5
  1758. $movkey 48($key_),$rndkey1
  1759. pxor $twres,@tweak[2]
  1760. aesenc $rndkey0,$inout0
  1761. pxor $twres,@tweak[3]
  1762. movdqa @tweak[1],`16*1`(%rsp)
  1763. aesenc $rndkey0,$inout1
  1764. pxor $twres,@tweak[4]
  1765. movdqa @tweak[2],`16*2`(%rsp)
  1766. aesenc $rndkey0,$inout2
  1767. aesenc $rndkey0,$inout3
  1768. pxor $twres,$twmask
  1769. movdqa @tweak[4],`16*4`(%rsp)
  1770. aesenc $rndkey0,$inout4
  1771. aesenc $rndkey0,$inout5
  1772. $movkey 64($key_),$rndkey0
  1773. movdqa $twmask,`16*5`(%rsp)
  1774. pshufd \$0x5f,@tweak[5],$twres
  1775. jmp .Lxts_enc_loop6
  1776. .align 32
  1777. .Lxts_enc_loop6:
  1778. aesenc $rndkey1,$inout0
  1779. aesenc $rndkey1,$inout1
  1780. aesenc $rndkey1,$inout2
  1781. aesenc $rndkey1,$inout3
  1782. aesenc $rndkey1,$inout4
  1783. aesenc $rndkey1,$inout5
  1784. $movkey -64($key,%rax),$rndkey1
  1785. add \$32,%rax
  1786. aesenc $rndkey0,$inout0
  1787. aesenc $rndkey0,$inout1
  1788. aesenc $rndkey0,$inout2
  1789. aesenc $rndkey0,$inout3
  1790. aesenc $rndkey0,$inout4
  1791. aesenc $rndkey0,$inout5
  1792. $movkey -80($key,%rax),$rndkey0
  1793. jnz .Lxts_enc_loop6
  1794. movdqa (%r8),$twmask # start calculating next tweak
  1795. movdqa $twres,$twtmp
  1796. paddd $twres,$twres
  1797. aesenc $rndkey1,$inout0
  1798. paddq @tweak[5],@tweak[5]
  1799. psrad \$31,$twtmp
  1800. aesenc $rndkey1,$inout1
  1801. pand $twmask,$twtmp
  1802. $movkey ($key_),@tweak[0] # load round[0]
  1803. aesenc $rndkey1,$inout2
  1804. aesenc $rndkey1,$inout3
  1805. aesenc $rndkey1,$inout4
  1806. pxor $twtmp,@tweak[5]
  1807. movaps @tweak[0],@tweak[1] # copy round[0]
  1808. aesenc $rndkey1,$inout5
  1809. $movkey -64($key),$rndkey1
  1810. movdqa $twres,$twtmp
  1811. aesenc $rndkey0,$inout0
  1812. paddd $twres,$twres
  1813. pxor @tweak[5],@tweak[0]
  1814. aesenc $rndkey0,$inout1
  1815. psrad \$31,$twtmp
  1816. paddq @tweak[5],@tweak[5]
  1817. aesenc $rndkey0,$inout2
  1818. aesenc $rndkey0,$inout3
  1819. pand $twmask,$twtmp
  1820. movaps @tweak[1],@tweak[2]
  1821. aesenc $rndkey0,$inout4
  1822. pxor $twtmp,@tweak[5]
  1823. movdqa $twres,$twtmp
  1824. aesenc $rndkey0,$inout5
  1825. $movkey -48($key),$rndkey0
  1826. paddd $twres,$twres
  1827. aesenc $rndkey1,$inout0
  1828. pxor @tweak[5],@tweak[1]
  1829. psrad \$31,$twtmp
  1830. aesenc $rndkey1,$inout1
  1831. paddq @tweak[5],@tweak[5]
  1832. pand $twmask,$twtmp
  1833. aesenc $rndkey1,$inout2
  1834. aesenc $rndkey1,$inout3
  1835. movdqa @tweak[3],`16*3`(%rsp)
  1836. pxor $twtmp,@tweak[5]
  1837. aesenc $rndkey1,$inout4
  1838. movaps @tweak[2],@tweak[3]
  1839. movdqa $twres,$twtmp
  1840. aesenc $rndkey1,$inout5
  1841. $movkey -32($key),$rndkey1
  1842. paddd $twres,$twres
  1843. aesenc $rndkey0,$inout0
  1844. pxor @tweak[5],@tweak[2]
  1845. psrad \$31,$twtmp
  1846. aesenc $rndkey0,$inout1
  1847. paddq @tweak[5],@tweak[5]
  1848. pand $twmask,$twtmp
  1849. aesenc $rndkey0,$inout2
  1850. aesenc $rndkey0,$inout3
  1851. aesenc $rndkey0,$inout4
  1852. pxor $twtmp,@tweak[5]
  1853. movaps @tweak[3],@tweak[4]
  1854. aesenc $rndkey0,$inout5
  1855. movdqa $twres,$rndkey0
  1856. paddd $twres,$twres
  1857. aesenc $rndkey1,$inout0
  1858. pxor @tweak[5],@tweak[3]
  1859. psrad \$31,$rndkey0
  1860. aesenc $rndkey1,$inout1
  1861. paddq @tweak[5],@tweak[5]
  1862. pand $twmask,$rndkey0
  1863. aesenc $rndkey1,$inout2
  1864. aesenc $rndkey1,$inout3
  1865. pxor $rndkey0,@tweak[5]
  1866. $movkey ($key_),$rndkey0
  1867. aesenc $rndkey1,$inout4
  1868. aesenc $rndkey1,$inout5
  1869. $movkey 16($key_),$rndkey1
  1870. pxor @tweak[5],@tweak[4]
  1871. aesenclast `16*0`(%rsp),$inout0
  1872. psrad \$31,$twres
  1873. paddq @tweak[5],@tweak[5]
  1874. aesenclast `16*1`(%rsp),$inout1
  1875. aesenclast `16*2`(%rsp),$inout2
  1876. pand $twmask,$twres
  1877. mov %r10,%rax # restore $rounds
  1878. aesenclast `16*3`(%rsp),$inout3
  1879. aesenclast `16*4`(%rsp),$inout4
  1880. aesenclast `16*5`(%rsp),$inout5
  1881. pxor $twres,@tweak[5]
  1882. lea `16*6`($out),$out # $out+=6*16
  1883. movups $inout0,`-16*6`($out) # store 6 output blocks
  1884. movups $inout1,`-16*5`($out)
  1885. movups $inout2,`-16*4`($out)
  1886. movups $inout3,`-16*3`($out)
  1887. movups $inout4,`-16*2`($out)
  1888. movups $inout5,`-16*1`($out)
  1889. sub \$16*6,$len
  1890. jnc .Lxts_enc_grandloop # loop if $len-=6*16 didn't borrow
  1891. mov \$16+96,$rounds
  1892. sub $rnds_,$rounds
  1893. mov $key_,$key # restore $key
  1894. shr \$4,$rounds # restore original value
  1895. .Lxts_enc_short:
  1896. # at the point @tweak[0..5] are populated with tweak values
  1897. mov $rounds,$rnds_ # backup $rounds
  1898. pxor $rndkey0,@tweak[0]
  1899. add \$16*6,$len # restore real remaining $len
  1900. jz .Lxts_enc_done # done if ($len==0)
  1901. pxor $rndkey0,@tweak[1]
  1902. cmp \$0x20,$len
  1903. jb .Lxts_enc_one # $len is 1*16
  1904. pxor $rndkey0,@tweak[2]
  1905. je .Lxts_enc_two # $len is 2*16
  1906. pxor $rndkey0,@tweak[3]
  1907. cmp \$0x40,$len
  1908. jb .Lxts_enc_three # $len is 3*16
  1909. pxor $rndkey0,@tweak[4]
  1910. je .Lxts_enc_four # $len is 4*16
  1911. movdqu ($inp),$inout0 # $len is 5*16
  1912. movdqu 16*1($inp),$inout1
  1913. movdqu 16*2($inp),$inout2
  1914. pxor @tweak[0],$inout0
  1915. movdqu 16*3($inp),$inout3
  1916. pxor @tweak[1],$inout1
  1917. movdqu 16*4($inp),$inout4
  1918. lea 16*5($inp),$inp # $inp+=5*16
  1919. pxor @tweak[2],$inout2
  1920. pxor @tweak[3],$inout3
  1921. pxor @tweak[4],$inout4
  1922. pxor $inout5,$inout5
  1923. call _aesni_encrypt6
  1924. xorps @tweak[0],$inout0
  1925. movdqa @tweak[5],@tweak[0]
  1926. xorps @tweak[1],$inout1
  1927. xorps @tweak[2],$inout2
  1928. movdqu $inout0,($out) # store 5 output blocks
  1929. xorps @tweak[3],$inout3
  1930. movdqu $inout1,16*1($out)
  1931. xorps @tweak[4],$inout4
  1932. movdqu $inout2,16*2($out)
  1933. movdqu $inout3,16*3($out)
  1934. movdqu $inout4,16*4($out)
  1935. lea 16*5($out),$out # $out+=5*16
  1936. jmp .Lxts_enc_done
  1937. .align 16
  1938. .Lxts_enc_one:
  1939. movups ($inp),$inout0
  1940. lea 16*1($inp),$inp # inp+=1*16
  1941. xorps @tweak[0],$inout0
  1942. ___
  1943. &aesni_generate1("enc",$key,$rounds);
  1944. $code.=<<___;
  1945. xorps @tweak[0],$inout0
  1946. movdqa @tweak[1],@tweak[0]
  1947. movups $inout0,($out) # store one output block
  1948. lea 16*1($out),$out # $out+=1*16
  1949. jmp .Lxts_enc_done
  1950. .align 16
  1951. .Lxts_enc_two:
  1952. movups ($inp),$inout0
  1953. movups 16($inp),$inout1
  1954. lea 32($inp),$inp # $inp+=2*16
  1955. xorps @tweak[0],$inout0
  1956. xorps @tweak[1],$inout1
  1957. call _aesni_encrypt2
  1958. xorps @tweak[0],$inout0
  1959. movdqa @tweak[2],@tweak[0]
  1960. xorps @tweak[1],$inout1
  1961. movups $inout0,($out) # store 2 output blocks
  1962. movups $inout1,16*1($out)
  1963. lea 16*2($out),$out # $out+=2*16
  1964. jmp .Lxts_enc_done
  1965. .align 16
  1966. .Lxts_enc_three:
  1967. movups ($inp),$inout0
  1968. movups 16*1($inp),$inout1
  1969. movups 16*2($inp),$inout2
  1970. lea 16*3($inp),$inp # $inp+=3*16
  1971. xorps @tweak[0],$inout0
  1972. xorps @tweak[1],$inout1
  1973. xorps @tweak[2],$inout2
  1974. call _aesni_encrypt3
  1975. xorps @tweak[0],$inout0
  1976. movdqa @tweak[3],@tweak[0]
  1977. xorps @tweak[1],$inout1
  1978. xorps @tweak[2],$inout2
  1979. movups $inout0,($out) # store 3 output blocks
  1980. movups $inout1,16*1($out)
  1981. movups $inout2,16*2($out)
  1982. lea 16*3($out),$out # $out+=3*16
  1983. jmp .Lxts_enc_done
  1984. .align 16
  1985. .Lxts_enc_four:
  1986. movups ($inp),$inout0
  1987. movups 16*1($inp),$inout1
  1988. movups 16*2($inp),$inout2
  1989. xorps @tweak[0],$inout0
  1990. movups 16*3($inp),$inout3
  1991. lea 16*4($inp),$inp # $inp+=4*16
  1992. xorps @tweak[1],$inout1
  1993. xorps @tweak[2],$inout2
  1994. xorps @tweak[3],$inout3
  1995. call _aesni_encrypt4
  1996. pxor @tweak[0],$inout0
  1997. movdqa @tweak[4],@tweak[0]
  1998. pxor @tweak[1],$inout1
  1999. pxor @tweak[2],$inout2
  2000. movdqu $inout0,($out) # store 4 output blocks
  2001. pxor @tweak[3],$inout3
  2002. movdqu $inout1,16*1($out)
  2003. movdqu $inout2,16*2($out)
  2004. movdqu $inout3,16*3($out)
  2005. lea 16*4($out),$out # $out+=4*16
  2006. jmp .Lxts_enc_done
  2007. .align 16
  2008. .Lxts_enc_done:
  2009. and \$15,$len_ # see if $len%16 is 0
  2010. jz .Lxts_enc_ret
  2011. mov $len_,$len
  2012. .Lxts_enc_steal:
  2013. movzb ($inp),%eax # borrow $rounds ...
  2014. movzb -16($out),%ecx # ... and $key
  2015. lea 1($inp),$inp
  2016. mov %al,-16($out)
  2017. mov %cl,0($out)
  2018. lea 1($out),$out
  2019. sub \$1,$len
  2020. jnz .Lxts_enc_steal
  2021. sub $len_,$out # rewind $out
  2022. mov $key_,$key # restore $key
  2023. mov $rnds_,$rounds # restore $rounds
  2024. movups -16($out),$inout0
  2025. xorps @tweak[0],$inout0
  2026. ___
  2027. &aesni_generate1("enc",$key,$rounds);
  2028. $code.=<<___;
  2029. xorps @tweak[0],$inout0
  2030. movups $inout0,-16($out)
  2031. .Lxts_enc_ret:
  2032. xorps %xmm0,%xmm0 # clear register bank
  2033. pxor %xmm1,%xmm1
  2034. pxor %xmm2,%xmm2
  2035. pxor %xmm3,%xmm3
  2036. pxor %xmm4,%xmm4
  2037. pxor %xmm5,%xmm5
  2038. ___
  2039. $code.=<<___ if (!$win64);
  2040. pxor %xmm6,%xmm6
  2041. pxor %xmm7,%xmm7
  2042. movaps %xmm0,0x00(%rsp) # clear stack
  2043. pxor %xmm8,%xmm8
  2044. movaps %xmm0,0x10(%rsp)
  2045. pxor %xmm9,%xmm9
  2046. movaps %xmm0,0x20(%rsp)
  2047. pxor %xmm10,%xmm10
  2048. movaps %xmm0,0x30(%rsp)
  2049. pxor %xmm11,%xmm11
  2050. movaps %xmm0,0x40(%rsp)
  2051. pxor %xmm12,%xmm12
  2052. movaps %xmm0,0x50(%rsp)
  2053. pxor %xmm13,%xmm13
  2054. movaps %xmm0,0x60(%rsp)
  2055. pxor %xmm14,%xmm14
  2056. pxor %xmm15,%xmm15
  2057. ___
  2058. $code.=<<___ if ($win64);
  2059. movaps -0xa8(%r11),%xmm6
  2060. movaps %xmm0,-0xa8(%r11) # clear stack
  2061. movaps -0x98(%r11),%xmm7
  2062. movaps %xmm0,-0x98(%r11)
  2063. movaps -0x88(%r11),%xmm8
  2064. movaps %xmm0,-0x88(%r11)
  2065. movaps -0x78(%r11),%xmm9
  2066. movaps %xmm0,-0x78(%r11)
  2067. movaps -0x68(%r11),%xmm10
  2068. movaps %xmm0,-0x68(%r11)
  2069. movaps -0x58(%r11),%xmm11
  2070. movaps %xmm0,-0x58(%r11)
  2071. movaps -0x48(%r11),%xmm12
  2072. movaps %xmm0,-0x48(%r11)
  2073. movaps -0x38(%r11),%xmm13
  2074. movaps %xmm0,-0x38(%r11)
  2075. movaps -0x28(%r11),%xmm14
  2076. movaps %xmm0,-0x28(%r11)
  2077. movaps -0x18(%r11),%xmm15
  2078. movaps %xmm0,-0x18(%r11)
  2079. movaps %xmm0,0x00(%rsp)
  2080. movaps %xmm0,0x10(%rsp)
  2081. movaps %xmm0,0x20(%rsp)
  2082. movaps %xmm0,0x30(%rsp)
  2083. movaps %xmm0,0x40(%rsp)
  2084. movaps %xmm0,0x50(%rsp)
  2085. movaps %xmm0,0x60(%rsp)
  2086. ___
  2087. $code.=<<___;
  2088. mov -8(%r11),%rbp
  2089. .cfi_restore %rbp
  2090. lea (%r11),%rsp
  2091. .cfi_def_cfa_register %rsp
  2092. .Lxts_enc_epilogue:
  2093. ret
  2094. .cfi_endproc
  2095. .size aesni_xts_encrypt,.-aesni_xts_encrypt
  2096. ___
  2097. $code.=<<___;
  2098. .globl aesni_xts_decrypt
  2099. .type aesni_xts_decrypt,\@function,6
  2100. .align 16
  2101. aesni_xts_decrypt:
  2102. .cfi_startproc
  2103. lea (%rsp),%r11 # frame pointer
  2104. .cfi_def_cfa_register %r11
  2105. push %rbp
  2106. .cfi_push %rbp
  2107. sub \$$frame_size,%rsp
  2108. and \$-16,%rsp # Linux kernel stack can be incorrectly seeded
  2109. ___
  2110. $code.=<<___ if ($win64);
  2111. movaps %xmm6,-0xa8(%r11) # offload everything
  2112. movaps %xmm7,-0x98(%r11)
  2113. movaps %xmm8,-0x88(%r11)
  2114. movaps %xmm9,-0x78(%r11)
  2115. movaps %xmm10,-0x68(%r11)
  2116. movaps %xmm11,-0x58(%r11)
  2117. movaps %xmm12,-0x48(%r11)
  2118. movaps %xmm13,-0x38(%r11)
  2119. movaps %xmm14,-0x28(%r11)
  2120. movaps %xmm15,-0x18(%r11)
  2121. .Lxts_dec_body:
  2122. ___
  2123. $code.=<<___;
  2124. movups ($ivp),$inout0 # load clear-text tweak
  2125. mov 240($key2),$rounds # key2->rounds
  2126. mov 240($key),$rnds_ # key1->rounds
  2127. ___
  2128. # generate the tweak
  2129. &aesni_generate1("enc",$key2,$rounds,$inout0);
  2130. $code.=<<___;
  2131. xor %eax,%eax # if ($len%16) len-=16;
  2132. test \$15,$len
  2133. setnz %al
  2134. shl \$4,%rax
  2135. sub %rax,$len
  2136. $movkey ($key),$rndkey0 # zero round key
  2137. mov $key,$key_ # backup $key
  2138. mov $rnds_,$rounds # backup $rounds
  2139. shl \$4,$rnds_
  2140. mov $len,$len_ # backup $len
  2141. and \$-16,$len
  2142. $movkey 16($key,$rnds_),$rndkey1 # last round key
  2143. movdqa .Lxts_magic(%rip),$twmask
  2144. movdqa $inout0,@tweak[5]
  2145. pshufd \$0x5f,$inout0,$twres
  2146. pxor $rndkey0,$rndkey1
  2147. ___
  2148. for ($i=0;$i<4;$i++) {
  2149. $code.=<<___;
  2150. movdqa $twres,$twtmp
  2151. paddd $twres,$twres
  2152. movdqa @tweak[5],@tweak[$i]
  2153. psrad \$31,$twtmp # broadcast upper bits
  2154. paddq @tweak[5],@tweak[5]
  2155. pand $twmask,$twtmp
  2156. pxor $rndkey0,@tweak[$i]
  2157. pxor $twtmp,@tweak[5]
  2158. ___
  2159. }
  2160. $code.=<<___;
  2161. movdqa @tweak[5],@tweak[4]
  2162. psrad \$31,$twres
  2163. paddq @tweak[5],@tweak[5]
  2164. pand $twmask,$twres
  2165. pxor $rndkey0,@tweak[4]
  2166. pxor $twres,@tweak[5]
  2167. movaps $rndkey1,0x60(%rsp) # save round[0]^round[last]
  2168. sub \$16*6,$len
  2169. jc .Lxts_dec_short # if $len-=6*16 borrowed
  2170. mov \$16+96,$rounds
  2171. lea 32($key_,$rnds_),$key # end of key schedule
  2172. sub %r10,%rax # twisted $rounds
  2173. $movkey 16($key_),$rndkey1
  2174. mov %rax,%r10 # backup twisted $rounds
  2175. lea .Lxts_magic(%rip),%r8
  2176. jmp .Lxts_dec_grandloop
  2177. .align 32
  2178. .Lxts_dec_grandloop:
  2179. movdqu `16*0`($inp),$inout0 # load input
  2180. movdqa $rndkey0,$twmask
  2181. movdqu `16*1`($inp),$inout1
  2182. pxor @tweak[0],$inout0 # intput^=tweak^round[0]
  2183. movdqu `16*2`($inp),$inout2
  2184. pxor @tweak[1],$inout1
  2185. aesdec $rndkey1,$inout0
  2186. movdqu `16*3`($inp),$inout3
  2187. pxor @tweak[2],$inout2
  2188. aesdec $rndkey1,$inout1
  2189. movdqu `16*4`($inp),$inout4
  2190. pxor @tweak[3],$inout3
  2191. aesdec $rndkey1,$inout2
  2192. movdqu `16*5`($inp),$inout5
  2193. pxor @tweak[5],$twmask # round[0]^=tweak[5]
  2194. movdqa 0x60(%rsp),$twres # load round[0]^round[last]
  2195. pxor @tweak[4],$inout4
  2196. aesdec $rndkey1,$inout3
  2197. $movkey 32($key_),$rndkey0
  2198. lea `16*6`($inp),$inp
  2199. pxor $twmask,$inout5
  2200. pxor $twres,@tweak[0] # calculate tweaks^round[last]
  2201. aesdec $rndkey1,$inout4
  2202. pxor $twres,@tweak[1]
  2203. movdqa @tweak[0],`16*0`(%rsp) # put aside tweaks^last round key
  2204. aesdec $rndkey1,$inout5
  2205. $movkey 48($key_),$rndkey1
  2206. pxor $twres,@tweak[2]
  2207. aesdec $rndkey0,$inout0
  2208. pxor $twres,@tweak[3]
  2209. movdqa @tweak[1],`16*1`(%rsp)
  2210. aesdec $rndkey0,$inout1
  2211. pxor $twres,@tweak[4]
  2212. movdqa @tweak[2],`16*2`(%rsp)
  2213. aesdec $rndkey0,$inout2
  2214. aesdec $rndkey0,$inout3
  2215. pxor $twres,$twmask
  2216. movdqa @tweak[4],`16*4`(%rsp)
  2217. aesdec $rndkey0,$inout4
  2218. aesdec $rndkey0,$inout5
  2219. $movkey 64($key_),$rndkey0
  2220. movdqa $twmask,`16*5`(%rsp)
  2221. pshufd \$0x5f,@tweak[5],$twres
  2222. jmp .Lxts_dec_loop6
  2223. .align 32
  2224. .Lxts_dec_loop6:
  2225. aesdec $rndkey1,$inout0
  2226. aesdec $rndkey1,$inout1
  2227. aesdec $rndkey1,$inout2
  2228. aesdec $rndkey1,$inout3
  2229. aesdec $rndkey1,$inout4
  2230. aesdec $rndkey1,$inout5
  2231. $movkey -64($key,%rax),$rndkey1
  2232. add \$32,%rax
  2233. aesdec $rndkey0,$inout0
  2234. aesdec $rndkey0,$inout1
  2235. aesdec $rndkey0,$inout2
  2236. aesdec $rndkey0,$inout3
  2237. aesdec $rndkey0,$inout4
  2238. aesdec $rndkey0,$inout5
  2239. $movkey -80($key,%rax),$rndkey0
  2240. jnz .Lxts_dec_loop6
  2241. movdqa (%r8),$twmask # start calculating next tweak
  2242. movdqa $twres,$twtmp
  2243. paddd $twres,$twres
  2244. aesdec $rndkey1,$inout0
  2245. paddq @tweak[5],@tweak[5]
  2246. psrad \$31,$twtmp
  2247. aesdec $rndkey1,$inout1
  2248. pand $twmask,$twtmp
  2249. $movkey ($key_),@tweak[0] # load round[0]
  2250. aesdec $rndkey1,$inout2
  2251. aesdec $rndkey1,$inout3
  2252. aesdec $rndkey1,$inout4
  2253. pxor $twtmp,@tweak[5]
  2254. movaps @tweak[0],@tweak[1] # copy round[0]
  2255. aesdec $rndkey1,$inout5
  2256. $movkey -64($key),$rndkey1
  2257. movdqa $twres,$twtmp
  2258. aesdec $rndkey0,$inout0
  2259. paddd $twres,$twres
  2260. pxor @tweak[5],@tweak[0]
  2261. aesdec $rndkey0,$inout1
  2262. psrad \$31,$twtmp
  2263. paddq @tweak[5],@tweak[5]
  2264. aesdec $rndkey0,$inout2
  2265. aesdec $rndkey0,$inout3
  2266. pand $twmask,$twtmp
  2267. movaps @tweak[1],@tweak[2]
  2268. aesdec $rndkey0,$inout4
  2269. pxor $twtmp,@tweak[5]
  2270. movdqa $twres,$twtmp
  2271. aesdec $rndkey0,$inout5
  2272. $movkey -48($key),$rndkey0
  2273. paddd $twres,$twres
  2274. aesdec $rndkey1,$inout0
  2275. pxor @tweak[5],@tweak[1]
  2276. psrad \$31,$twtmp
  2277. aesdec $rndkey1,$inout1
  2278. paddq @tweak[5],@tweak[5]
  2279. pand $twmask,$twtmp
  2280. aesdec $rndkey1,$inout2
  2281. aesdec $rndkey1,$inout3
  2282. movdqa @tweak[3],`16*3`(%rsp)
  2283. pxor $twtmp,@tweak[5]
  2284. aesdec $rndkey1,$inout4
  2285. movaps @tweak[2],@tweak[3]
  2286. movdqa $twres,$twtmp
  2287. aesdec $rndkey1,$inout5
  2288. $movkey -32($key),$rndkey1
  2289. paddd $twres,$twres
  2290. aesdec $rndkey0,$inout0
  2291. pxor @tweak[5],@tweak[2]
  2292. psrad \$31,$twtmp
  2293. aesdec $rndkey0,$inout1
  2294. paddq @tweak[5],@tweak[5]
  2295. pand $twmask,$twtmp
  2296. aesdec $rndkey0,$inout2
  2297. aesdec $rndkey0,$inout3
  2298. aesdec $rndkey0,$inout4
  2299. pxor $twtmp,@tweak[5]
  2300. movaps @tweak[3],@tweak[4]
  2301. aesdec $rndkey0,$inout5
  2302. movdqa $twres,$rndkey0
  2303. paddd $twres,$twres
  2304. aesdec $rndkey1,$inout0
  2305. pxor @tweak[5],@tweak[3]
  2306. psrad \$31,$rndkey0
  2307. aesdec $rndkey1,$inout1
  2308. paddq @tweak[5],@tweak[5]
  2309. pand $twmask,$rndkey0
  2310. aesdec $rndkey1,$inout2
  2311. aesdec $rndkey1,$inout3
  2312. pxor $rndkey0,@tweak[5]
  2313. $movkey ($key_),$rndkey0
  2314. aesdec $rndkey1,$inout4
  2315. aesdec $rndkey1,$inout5
  2316. $movkey 16($key_),$rndkey1
  2317. pxor @tweak[5],@tweak[4]
  2318. aesdeclast `16*0`(%rsp),$inout0
  2319. psrad \$31,$twres
  2320. paddq @tweak[5],@tweak[5]
  2321. aesdeclast `16*1`(%rsp),$inout1
  2322. aesdeclast `16*2`(%rsp),$inout2
  2323. pand $twmask,$twres
  2324. mov %r10,%rax # restore $rounds
  2325. aesdeclast `16*3`(%rsp),$inout3
  2326. aesdeclast `16*4`(%rsp),$inout4
  2327. aesdeclast `16*5`(%rsp),$inout5
  2328. pxor $twres,@tweak[5]
  2329. lea `16*6`($out),$out # $out+=6*16
  2330. movups $inout0,`-16*6`($out) # store 6 output blocks
  2331. movups $inout1,`-16*5`($out)
  2332. movups $inout2,`-16*4`($out)
  2333. movups $inout3,`-16*3`($out)
  2334. movups $inout4,`-16*2`($out)
  2335. movups $inout5,`-16*1`($out)
  2336. sub \$16*6,$len
  2337. jnc .Lxts_dec_grandloop # loop if $len-=6*16 didn't borrow
  2338. mov \$16+96,$rounds
  2339. sub $rnds_,$rounds
  2340. mov $key_,$key # restore $key
  2341. shr \$4,$rounds # restore original value
  2342. .Lxts_dec_short:
  2343. # at the point @tweak[0..5] are populated with tweak values
  2344. mov $rounds,$rnds_ # backup $rounds
  2345. pxor $rndkey0,@tweak[0]
  2346. pxor $rndkey0,@tweak[1]
  2347. add \$16*6,$len # restore real remaining $len
  2348. jz .Lxts_dec_done # done if ($len==0)
  2349. pxor $rndkey0,@tweak[2]
  2350. cmp \$0x20,$len
  2351. jb .Lxts_dec_one # $len is 1*16
  2352. pxor $rndkey0,@tweak[3]
  2353. je .Lxts_dec_two # $len is 2*16
  2354. pxor $rndkey0,@tweak[4]
  2355. cmp \$0x40,$len
  2356. jb .Lxts_dec_three # $len is 3*16
  2357. je .Lxts_dec_four # $len is 4*16
  2358. movdqu ($inp),$inout0 # $len is 5*16
  2359. movdqu 16*1($inp),$inout1
  2360. movdqu 16*2($inp),$inout2
  2361. pxor @tweak[0],$inout0
  2362. movdqu 16*3($inp),$inout3
  2363. pxor @tweak[1],$inout1
  2364. movdqu 16*4($inp),$inout4
  2365. lea 16*5($inp),$inp # $inp+=5*16
  2366. pxor @tweak[2],$inout2
  2367. pxor @tweak[3],$inout3
  2368. pxor @tweak[4],$inout4
  2369. call _aesni_decrypt6
  2370. xorps @tweak[0],$inout0
  2371. xorps @tweak[1],$inout1
  2372. xorps @tweak[2],$inout2
  2373. movdqu $inout0,($out) # store 5 output blocks
  2374. xorps @tweak[3],$inout3
  2375. movdqu $inout1,16*1($out)
  2376. xorps @tweak[4],$inout4
  2377. movdqu $inout2,16*2($out)
  2378. pxor $twtmp,$twtmp
  2379. movdqu $inout3,16*3($out)
  2380. pcmpgtd @tweak[5],$twtmp
  2381. movdqu $inout4,16*4($out)
  2382. lea 16*5($out),$out # $out+=5*16
  2383. pshufd \$0x13,$twtmp,@tweak[1] # $twres
  2384. and \$15,$len_
  2385. jz .Lxts_dec_ret
  2386. movdqa @tweak[5],@tweak[0]
  2387. paddq @tweak[5],@tweak[5] # psllq 1,$tweak
  2388. pand $twmask,@tweak[1] # isolate carry and residue
  2389. pxor @tweak[5],@tweak[1]
  2390. jmp .Lxts_dec_done2
  2391. .align 16
  2392. .Lxts_dec_one:
  2393. movups ($inp),$inout0
  2394. lea 16*1($inp),$inp # $inp+=1*16
  2395. xorps @tweak[0],$inout0
  2396. ___
  2397. &aesni_generate1("dec",$key,$rounds);
  2398. $code.=<<___;
  2399. xorps @tweak[0],$inout0
  2400. movdqa @tweak[1],@tweak[0]
  2401. movups $inout0,($out) # store one output block
  2402. movdqa @tweak[2],@tweak[1]
  2403. lea 16*1($out),$out # $out+=1*16
  2404. jmp .Lxts_dec_done
  2405. .align 16
  2406. .Lxts_dec_two:
  2407. movups ($inp),$inout0
  2408. movups 16($inp),$inout1
  2409. lea 32($inp),$inp # $inp+=2*16
  2410. xorps @tweak[0],$inout0
  2411. xorps @tweak[1],$inout1
  2412. call _aesni_decrypt2
  2413. xorps @tweak[0],$inout0
  2414. movdqa @tweak[2],@tweak[0]
  2415. xorps @tweak[1],$inout1
  2416. movdqa @tweak[3],@tweak[1]
  2417. movups $inout0,($out) # store 2 output blocks
  2418. movups $inout1,16*1($out)
  2419. lea 16*2($out),$out # $out+=2*16
  2420. jmp .Lxts_dec_done
  2421. .align 16
  2422. .Lxts_dec_three:
  2423. movups ($inp),$inout0
  2424. movups 16*1($inp),$inout1
  2425. movups 16*2($inp),$inout2
  2426. lea 16*3($inp),$inp # $inp+=3*16
  2427. xorps @tweak[0],$inout0
  2428. xorps @tweak[1],$inout1
  2429. xorps @tweak[2],$inout2
  2430. call _aesni_decrypt3
  2431. xorps @tweak[0],$inout0
  2432. movdqa @tweak[3],@tweak[0]
  2433. xorps @tweak[1],$inout1
  2434. movdqa @tweak[4],@tweak[1]
  2435. xorps @tweak[2],$inout2
  2436. movups $inout0,($out) # store 3 output blocks
  2437. movups $inout1,16*1($out)
  2438. movups $inout2,16*2($out)
  2439. lea 16*3($out),$out # $out+=3*16
  2440. jmp .Lxts_dec_done
  2441. .align 16
  2442. .Lxts_dec_four:
  2443. movups ($inp),$inout0
  2444. movups 16*1($inp),$inout1
  2445. movups 16*2($inp),$inout2
  2446. xorps @tweak[0],$inout0
  2447. movups 16*3($inp),$inout3
  2448. lea 16*4($inp),$inp # $inp+=4*16
  2449. xorps @tweak[1],$inout1
  2450. xorps @tweak[2],$inout2
  2451. xorps @tweak[3],$inout3
  2452. call _aesni_decrypt4
  2453. pxor @tweak[0],$inout0
  2454. movdqa @tweak[4],@tweak[0]
  2455. pxor @tweak[1],$inout1
  2456. movdqa @tweak[5],@tweak[1]
  2457. pxor @tweak[2],$inout2
  2458. movdqu $inout0,($out) # store 4 output blocks
  2459. pxor @tweak[3],$inout3
  2460. movdqu $inout1,16*1($out)
  2461. movdqu $inout2,16*2($out)
  2462. movdqu $inout3,16*3($out)
  2463. lea 16*4($out),$out # $out+=4*16
  2464. jmp .Lxts_dec_done
  2465. .align 16
  2466. .Lxts_dec_done:
  2467. and \$15,$len_ # see if $len%16 is 0
  2468. jz .Lxts_dec_ret
  2469. .Lxts_dec_done2:
  2470. mov $len_,$len
  2471. mov $key_,$key # restore $key
  2472. mov $rnds_,$rounds # restore $rounds
  2473. movups ($inp),$inout0
  2474. xorps @tweak[1],$inout0
  2475. ___
  2476. &aesni_generate1("dec",$key,$rounds);
  2477. $code.=<<___;
  2478. xorps @tweak[1],$inout0
  2479. movups $inout0,($out)
  2480. .Lxts_dec_steal:
  2481. movzb 16($inp),%eax # borrow $rounds ...
  2482. movzb ($out),%ecx # ... and $key
  2483. lea 1($inp),$inp
  2484. mov %al,($out)
  2485. mov %cl,16($out)
  2486. lea 1($out),$out
  2487. sub \$1,$len
  2488. jnz .Lxts_dec_steal
  2489. sub $len_,$out # rewind $out
  2490. mov $key_,$key # restore $key
  2491. mov $rnds_,$rounds # restore $rounds
  2492. movups ($out),$inout0
  2493. xorps @tweak[0],$inout0
  2494. ___
  2495. &aesni_generate1("dec",$key,$rounds);
  2496. $code.=<<___;
  2497. xorps @tweak[0],$inout0
  2498. movups $inout0,($out)
  2499. .Lxts_dec_ret:
  2500. xorps %xmm0,%xmm0 # clear register bank
  2501. pxor %xmm1,%xmm1
  2502. pxor %xmm2,%xmm2
  2503. pxor %xmm3,%xmm3
  2504. pxor %xmm4,%xmm4
  2505. pxor %xmm5,%xmm5
  2506. ___
  2507. $code.=<<___ if (!$win64);
  2508. pxor %xmm6,%xmm6
  2509. pxor %xmm7,%xmm7
  2510. movaps %xmm0,0x00(%rsp) # clear stack
  2511. pxor %xmm8,%xmm8
  2512. movaps %xmm0,0x10(%rsp)
  2513. pxor %xmm9,%xmm9
  2514. movaps %xmm0,0x20(%rsp)
  2515. pxor %xmm10,%xmm10
  2516. movaps %xmm0,0x30(%rsp)
  2517. pxor %xmm11,%xmm11
  2518. movaps %xmm0,0x40(%rsp)
  2519. pxor %xmm12,%xmm12
  2520. movaps %xmm0,0x50(%rsp)
  2521. pxor %xmm13,%xmm13
  2522. movaps %xmm0,0x60(%rsp)
  2523. pxor %xmm14,%xmm14
  2524. pxor %xmm15,%xmm15
  2525. ___
  2526. $code.=<<___ if ($win64);
  2527. movaps -0xa8(%r11),%xmm6
  2528. movaps %xmm0,-0xa8(%r11) # clear stack
  2529. movaps -0x98(%r11),%xmm7
  2530. movaps %xmm0,-0x98(%r11)
  2531. movaps -0x88(%r11),%xmm8
  2532. movaps %xmm0,-0x88(%r11)
  2533. movaps -0x78(%r11),%xmm9
  2534. movaps %xmm0,-0x78(%r11)
  2535. movaps -0x68(%r11),%xmm10
  2536. movaps %xmm0,-0x68(%r11)
  2537. movaps -0x58(%r11),%xmm11
  2538. movaps %xmm0,-0x58(%r11)
  2539. movaps -0x48(%r11),%xmm12
  2540. movaps %xmm0,-0x48(%r11)
  2541. movaps -0x38(%r11),%xmm13
  2542. movaps %xmm0,-0x38(%r11)
  2543. movaps -0x28(%r11),%xmm14
  2544. movaps %xmm0,-0x28(%r11)
  2545. movaps -0x18(%r11),%xmm15
  2546. movaps %xmm0,-0x18(%r11)
  2547. movaps %xmm0,0x00(%rsp)
  2548. movaps %xmm0,0x10(%rsp)
  2549. movaps %xmm0,0x20(%rsp)
  2550. movaps %xmm0,0x30(%rsp)
  2551. movaps %xmm0,0x40(%rsp)
  2552. movaps %xmm0,0x50(%rsp)
  2553. movaps %xmm0,0x60(%rsp)
  2554. ___
  2555. $code.=<<___;
  2556. mov -8(%r11),%rbp
  2557. .cfi_restore %rbp
  2558. lea (%r11),%rsp
  2559. .cfi_def_cfa_register %rsp
  2560. .Lxts_dec_epilogue:
  2561. ret
  2562. .cfi_endproc
  2563. .size aesni_xts_decrypt,.-aesni_xts_decrypt
  2564. ___
  2565. }
  2566. ######################################################################
  2567. # void aesni_ocb_[en|de]crypt(const char *inp, char *out, size_t blocks,
  2568. # const AES_KEY *key, unsigned int start_block_num,
  2569. # unsigned char offset_i[16], const unsigned char L_[][16],
  2570. # unsigned char checksum[16]);
  2571. #
  2572. {
  2573. my @offset=map("%xmm$_",(10..15));
  2574. my ($checksum,$rndkey0l)=("%xmm8","%xmm9");
  2575. my ($block_num,$offset_p)=("%r8","%r9"); # 5th and 6th arguments
  2576. my ($L_p,$checksum_p) = ("%rbx","%rbp");
  2577. my ($i1,$i3,$i5) = ("%r12","%r13","%r14");
  2578. my $seventh_arg = $win64 ? 56 : 8;
  2579. my $blocks = $len;
  2580. $code.=<<___;
  2581. .globl aesni_ocb_encrypt
  2582. .type aesni_ocb_encrypt,\@function,6
  2583. .align 32
  2584. aesni_ocb_encrypt:
  2585. .cfi_startproc
  2586. lea (%rsp),%rax
  2587. push %rbx
  2588. .cfi_push %rbx
  2589. push %rbp
  2590. .cfi_push %rbp
  2591. push %r12
  2592. .cfi_push %r12
  2593. push %r13
  2594. .cfi_push %r13
  2595. push %r14
  2596. .cfi_push %r14
  2597. ___
  2598. $code.=<<___ if ($win64);
  2599. lea -0xa0(%rsp),%rsp
  2600. movaps %xmm6,0x00(%rsp) # offload everything
  2601. movaps %xmm7,0x10(%rsp)
  2602. movaps %xmm8,0x20(%rsp)
  2603. movaps %xmm9,0x30(%rsp)
  2604. movaps %xmm10,0x40(%rsp)
  2605. movaps %xmm11,0x50(%rsp)
  2606. movaps %xmm12,0x60(%rsp)
  2607. movaps %xmm13,0x70(%rsp)
  2608. movaps %xmm14,0x80(%rsp)
  2609. movaps %xmm15,0x90(%rsp)
  2610. .Locb_enc_body:
  2611. ___
  2612. $code.=<<___;
  2613. mov $seventh_arg(%rax),$L_p # 7th argument
  2614. mov $seventh_arg+8(%rax),$checksum_p# 8th argument
  2615. mov 240($key),$rnds_
  2616. mov $key,$key_
  2617. shl \$4,$rnds_
  2618. $movkey ($key),$rndkey0l # round[0]
  2619. $movkey 16($key,$rnds_),$rndkey1 # round[last]
  2620. movdqu ($offset_p),@offset[5] # load last offset_i
  2621. pxor $rndkey1,$rndkey0l # round[0] ^ round[last]
  2622. pxor $rndkey1,@offset[5] # offset_i ^ round[last]
  2623. mov \$16+32,$rounds
  2624. lea 32($key_,$rnds_),$key
  2625. $movkey 16($key_),$rndkey1 # round[1]
  2626. sub %r10,%rax # twisted $rounds
  2627. mov %rax,%r10 # backup twisted $rounds
  2628. movdqu ($L_p),@offset[0] # L_0 for all odd-numbered blocks
  2629. movdqu ($checksum_p),$checksum # load checksum
  2630. test \$1,$block_num # is first block number odd?
  2631. jnz .Locb_enc_odd
  2632. bsf $block_num,$i1
  2633. add \$1,$block_num
  2634. shl \$4,$i1
  2635. movdqu ($L_p,$i1),$inout5 # borrow
  2636. movdqu ($inp),$inout0
  2637. lea 16($inp),$inp
  2638. call __ocb_encrypt1
  2639. movdqa $inout5,@offset[5]
  2640. movups $inout0,($out)
  2641. lea 16($out),$out
  2642. sub \$1,$blocks
  2643. jz .Locb_enc_done
  2644. .Locb_enc_odd:
  2645. lea 1($block_num),$i1 # even-numbered blocks
  2646. lea 3($block_num),$i3
  2647. lea 5($block_num),$i5
  2648. lea 6($block_num),$block_num
  2649. bsf $i1,$i1 # ntz(block)
  2650. bsf $i3,$i3
  2651. bsf $i5,$i5
  2652. shl \$4,$i1 # ntz(block) -> table offset
  2653. shl \$4,$i3
  2654. shl \$4,$i5
  2655. sub \$6,$blocks
  2656. jc .Locb_enc_short
  2657. jmp .Locb_enc_grandloop
  2658. .align 32
  2659. .Locb_enc_grandloop:
  2660. movdqu `16*0`($inp),$inout0 # load input
  2661. movdqu `16*1`($inp),$inout1
  2662. movdqu `16*2`($inp),$inout2
  2663. movdqu `16*3`($inp),$inout3
  2664. movdqu `16*4`($inp),$inout4
  2665. movdqu `16*5`($inp),$inout5
  2666. lea `16*6`($inp),$inp
  2667. call __ocb_encrypt6
  2668. movups $inout0,`16*0`($out) # store output
  2669. movups $inout1,`16*1`($out)
  2670. movups $inout2,`16*2`($out)
  2671. movups $inout3,`16*3`($out)
  2672. movups $inout4,`16*4`($out)
  2673. movups $inout5,`16*5`($out)
  2674. lea `16*6`($out),$out
  2675. sub \$6,$blocks
  2676. jnc .Locb_enc_grandloop
  2677. .Locb_enc_short:
  2678. add \$6,$blocks
  2679. jz .Locb_enc_done
  2680. movdqu `16*0`($inp),$inout0
  2681. cmp \$2,$blocks
  2682. jb .Locb_enc_one
  2683. movdqu `16*1`($inp),$inout1
  2684. je .Locb_enc_two
  2685. movdqu `16*2`($inp),$inout2
  2686. cmp \$4,$blocks
  2687. jb .Locb_enc_three
  2688. movdqu `16*3`($inp),$inout3
  2689. je .Locb_enc_four
  2690. movdqu `16*4`($inp),$inout4
  2691. pxor $inout5,$inout5
  2692. call __ocb_encrypt6
  2693. movdqa @offset[4],@offset[5]
  2694. movups $inout0,`16*0`($out)
  2695. movups $inout1,`16*1`($out)
  2696. movups $inout2,`16*2`($out)
  2697. movups $inout3,`16*3`($out)
  2698. movups $inout4,`16*4`($out)
  2699. jmp .Locb_enc_done
  2700. .align 16
  2701. .Locb_enc_one:
  2702. movdqa @offset[0],$inout5 # borrow
  2703. call __ocb_encrypt1
  2704. movdqa $inout5,@offset[5]
  2705. movups $inout0,`16*0`($out)
  2706. jmp .Locb_enc_done
  2707. .align 16
  2708. .Locb_enc_two:
  2709. pxor $inout2,$inout2
  2710. pxor $inout3,$inout3
  2711. call __ocb_encrypt4
  2712. movdqa @offset[1],@offset[5]
  2713. movups $inout0,`16*0`($out)
  2714. movups $inout1,`16*1`($out)
  2715. jmp .Locb_enc_done
  2716. .align 16
  2717. .Locb_enc_three:
  2718. pxor $inout3,$inout3
  2719. call __ocb_encrypt4
  2720. movdqa @offset[2],@offset[5]
  2721. movups $inout0,`16*0`($out)
  2722. movups $inout1,`16*1`($out)
  2723. movups $inout2,`16*2`($out)
  2724. jmp .Locb_enc_done
  2725. .align 16
  2726. .Locb_enc_four:
  2727. call __ocb_encrypt4
  2728. movdqa @offset[3],@offset[5]
  2729. movups $inout0,`16*0`($out)
  2730. movups $inout1,`16*1`($out)
  2731. movups $inout2,`16*2`($out)
  2732. movups $inout3,`16*3`($out)
  2733. .Locb_enc_done:
  2734. pxor $rndkey0,@offset[5] # "remove" round[last]
  2735. movdqu $checksum,($checksum_p) # store checksum
  2736. movdqu @offset[5],($offset_p) # store last offset_i
  2737. xorps %xmm0,%xmm0 # clear register bank
  2738. pxor %xmm1,%xmm1
  2739. pxor %xmm2,%xmm2
  2740. pxor %xmm3,%xmm3
  2741. pxor %xmm4,%xmm4
  2742. pxor %xmm5,%xmm5
  2743. ___
  2744. $code.=<<___ if (!$win64);
  2745. pxor %xmm6,%xmm6
  2746. pxor %xmm7,%xmm7
  2747. pxor %xmm8,%xmm8
  2748. pxor %xmm9,%xmm9
  2749. pxor %xmm10,%xmm10
  2750. pxor %xmm11,%xmm11
  2751. pxor %xmm12,%xmm12
  2752. pxor %xmm13,%xmm13
  2753. pxor %xmm14,%xmm14
  2754. pxor %xmm15,%xmm15
  2755. lea 0x28(%rsp),%rax
  2756. .cfi_def_cfa %rax,8
  2757. ___
  2758. $code.=<<___ if ($win64);
  2759. movaps 0x00(%rsp),%xmm6
  2760. movaps %xmm0,0x00(%rsp) # clear stack
  2761. movaps 0x10(%rsp),%xmm7
  2762. movaps %xmm0,0x10(%rsp)
  2763. movaps 0x20(%rsp),%xmm8
  2764. movaps %xmm0,0x20(%rsp)
  2765. movaps 0x30(%rsp),%xmm9
  2766. movaps %xmm0,0x30(%rsp)
  2767. movaps 0x40(%rsp),%xmm10
  2768. movaps %xmm0,0x40(%rsp)
  2769. movaps 0x50(%rsp),%xmm11
  2770. movaps %xmm0,0x50(%rsp)
  2771. movaps 0x60(%rsp),%xmm12
  2772. movaps %xmm0,0x60(%rsp)
  2773. movaps 0x70(%rsp),%xmm13
  2774. movaps %xmm0,0x70(%rsp)
  2775. movaps 0x80(%rsp),%xmm14
  2776. movaps %xmm0,0x80(%rsp)
  2777. movaps 0x90(%rsp),%xmm15
  2778. movaps %xmm0,0x90(%rsp)
  2779. lea 0xa0+0x28(%rsp),%rax
  2780. .Locb_enc_pop:
  2781. ___
  2782. $code.=<<___;
  2783. mov -40(%rax),%r14
  2784. .cfi_restore %r14
  2785. mov -32(%rax),%r13
  2786. .cfi_restore %r13
  2787. mov -24(%rax),%r12
  2788. .cfi_restore %r12
  2789. mov -16(%rax),%rbp
  2790. .cfi_restore %rbp
  2791. mov -8(%rax),%rbx
  2792. .cfi_restore %rbx
  2793. lea (%rax),%rsp
  2794. .cfi_def_cfa_register %rsp
  2795. .Locb_enc_epilogue:
  2796. ret
  2797. .cfi_endproc
  2798. .size aesni_ocb_encrypt,.-aesni_ocb_encrypt
  2799. .type __ocb_encrypt6,\@abi-omnipotent
  2800. .align 32
  2801. __ocb_encrypt6:
  2802. pxor $rndkey0l,@offset[5] # offset_i ^ round[0]
  2803. movdqu ($L_p,$i1),@offset[1]
  2804. movdqa @offset[0],@offset[2]
  2805. movdqu ($L_p,$i3),@offset[3]
  2806. movdqa @offset[0],@offset[4]
  2807. pxor @offset[5],@offset[0]
  2808. movdqu ($L_p,$i5),@offset[5]
  2809. pxor @offset[0],@offset[1]
  2810. pxor $inout0,$checksum # accumulate checksum
  2811. pxor @offset[0],$inout0 # input ^ round[0] ^ offset_i
  2812. pxor @offset[1],@offset[2]
  2813. pxor $inout1,$checksum
  2814. pxor @offset[1],$inout1
  2815. pxor @offset[2],@offset[3]
  2816. pxor $inout2,$checksum
  2817. pxor @offset[2],$inout2
  2818. pxor @offset[3],@offset[4]
  2819. pxor $inout3,$checksum
  2820. pxor @offset[3],$inout3
  2821. pxor @offset[4],@offset[5]
  2822. pxor $inout4,$checksum
  2823. pxor @offset[4],$inout4
  2824. pxor $inout5,$checksum
  2825. pxor @offset[5],$inout5
  2826. $movkey 32($key_),$rndkey0
  2827. lea 1($block_num),$i1 # even-numbered blocks
  2828. lea 3($block_num),$i3
  2829. lea 5($block_num),$i5
  2830. add \$6,$block_num
  2831. pxor $rndkey0l,@offset[0] # offset_i ^ round[last]
  2832. bsf $i1,$i1 # ntz(block)
  2833. bsf $i3,$i3
  2834. bsf $i5,$i5
  2835. aesenc $rndkey1,$inout0
  2836. aesenc $rndkey1,$inout1
  2837. aesenc $rndkey1,$inout2
  2838. aesenc $rndkey1,$inout3
  2839. pxor $rndkey0l,@offset[1]
  2840. pxor $rndkey0l,@offset[2]
  2841. aesenc $rndkey1,$inout4
  2842. pxor $rndkey0l,@offset[3]
  2843. pxor $rndkey0l,@offset[4]
  2844. aesenc $rndkey1,$inout5
  2845. $movkey 48($key_),$rndkey1
  2846. pxor $rndkey0l,@offset[5]
  2847. aesenc $rndkey0,$inout0
  2848. aesenc $rndkey0,$inout1
  2849. aesenc $rndkey0,$inout2
  2850. aesenc $rndkey0,$inout3
  2851. aesenc $rndkey0,$inout4
  2852. aesenc $rndkey0,$inout5
  2853. $movkey 64($key_),$rndkey0
  2854. shl \$4,$i1 # ntz(block) -> table offset
  2855. shl \$4,$i3
  2856. jmp .Locb_enc_loop6
  2857. .align 32
  2858. .Locb_enc_loop6:
  2859. aesenc $rndkey1,$inout0
  2860. aesenc $rndkey1,$inout1
  2861. aesenc $rndkey1,$inout2
  2862. aesenc $rndkey1,$inout3
  2863. aesenc $rndkey1,$inout4
  2864. aesenc $rndkey1,$inout5
  2865. $movkey ($key,%rax),$rndkey1
  2866. add \$32,%rax
  2867. aesenc $rndkey0,$inout0
  2868. aesenc $rndkey0,$inout1
  2869. aesenc $rndkey0,$inout2
  2870. aesenc $rndkey0,$inout3
  2871. aesenc $rndkey0,$inout4
  2872. aesenc $rndkey0,$inout5
  2873. $movkey -16($key,%rax),$rndkey0
  2874. jnz .Locb_enc_loop6
  2875. aesenc $rndkey1,$inout0
  2876. aesenc $rndkey1,$inout1
  2877. aesenc $rndkey1,$inout2
  2878. aesenc $rndkey1,$inout3
  2879. aesenc $rndkey1,$inout4
  2880. aesenc $rndkey1,$inout5
  2881. $movkey 16($key_),$rndkey1
  2882. shl \$4,$i5
  2883. aesenclast @offset[0],$inout0
  2884. movdqu ($L_p),@offset[0] # L_0 for all odd-numbered blocks
  2885. mov %r10,%rax # restore twisted rounds
  2886. aesenclast @offset[1],$inout1
  2887. aesenclast @offset[2],$inout2
  2888. aesenclast @offset[3],$inout3
  2889. aesenclast @offset[4],$inout4
  2890. aesenclast @offset[5],$inout5
  2891. ret
  2892. .size __ocb_encrypt6,.-__ocb_encrypt6
  2893. .type __ocb_encrypt4,\@abi-omnipotent
  2894. .align 32
  2895. __ocb_encrypt4:
  2896. pxor $rndkey0l,@offset[5] # offset_i ^ round[0]
  2897. movdqu ($L_p,$i1),@offset[1]
  2898. movdqa @offset[0],@offset[2]
  2899. movdqu ($L_p,$i3),@offset[3]
  2900. pxor @offset[5],@offset[0]
  2901. pxor @offset[0],@offset[1]
  2902. pxor $inout0,$checksum # accumulate checksum
  2903. pxor @offset[0],$inout0 # input ^ round[0] ^ offset_i
  2904. pxor @offset[1],@offset[2]
  2905. pxor $inout1,$checksum
  2906. pxor @offset[1],$inout1
  2907. pxor @offset[2],@offset[3]
  2908. pxor $inout2,$checksum
  2909. pxor @offset[2],$inout2
  2910. pxor $inout3,$checksum
  2911. pxor @offset[3],$inout3
  2912. $movkey 32($key_),$rndkey0
  2913. pxor $rndkey0l,@offset[0] # offset_i ^ round[last]
  2914. pxor $rndkey0l,@offset[1]
  2915. pxor $rndkey0l,@offset[2]
  2916. pxor $rndkey0l,@offset[3]
  2917. aesenc $rndkey1,$inout0
  2918. aesenc $rndkey1,$inout1
  2919. aesenc $rndkey1,$inout2
  2920. aesenc $rndkey1,$inout3
  2921. $movkey 48($key_),$rndkey1
  2922. aesenc $rndkey0,$inout0
  2923. aesenc $rndkey0,$inout1
  2924. aesenc $rndkey0,$inout2
  2925. aesenc $rndkey0,$inout3
  2926. $movkey 64($key_),$rndkey0
  2927. jmp .Locb_enc_loop4
  2928. .align 32
  2929. .Locb_enc_loop4:
  2930. aesenc $rndkey1,$inout0
  2931. aesenc $rndkey1,$inout1
  2932. aesenc $rndkey1,$inout2
  2933. aesenc $rndkey1,$inout3
  2934. $movkey ($key,%rax),$rndkey1
  2935. add \$32,%rax
  2936. aesenc $rndkey0,$inout0
  2937. aesenc $rndkey0,$inout1
  2938. aesenc $rndkey0,$inout2
  2939. aesenc $rndkey0,$inout3
  2940. $movkey -16($key,%rax),$rndkey0
  2941. jnz .Locb_enc_loop4
  2942. aesenc $rndkey1,$inout0
  2943. aesenc $rndkey1,$inout1
  2944. aesenc $rndkey1,$inout2
  2945. aesenc $rndkey1,$inout3
  2946. $movkey 16($key_),$rndkey1
  2947. mov %r10,%rax # restore twisted rounds
  2948. aesenclast @offset[0],$inout0
  2949. aesenclast @offset[1],$inout1
  2950. aesenclast @offset[2],$inout2
  2951. aesenclast @offset[3],$inout3
  2952. ret
  2953. .size __ocb_encrypt4,.-__ocb_encrypt4
  2954. .type __ocb_encrypt1,\@abi-omnipotent
  2955. .align 32
  2956. __ocb_encrypt1:
  2957. pxor @offset[5],$inout5 # offset_i
  2958. pxor $rndkey0l,$inout5 # offset_i ^ round[0]
  2959. pxor $inout0,$checksum # accumulate checksum
  2960. pxor $inout5,$inout0 # input ^ round[0] ^ offset_i
  2961. $movkey 32($key_),$rndkey0
  2962. aesenc $rndkey1,$inout0
  2963. $movkey 48($key_),$rndkey1
  2964. pxor $rndkey0l,$inout5 # offset_i ^ round[last]
  2965. aesenc $rndkey0,$inout0
  2966. $movkey 64($key_),$rndkey0
  2967. jmp .Locb_enc_loop1
  2968. .align 32
  2969. .Locb_enc_loop1:
  2970. aesenc $rndkey1,$inout0
  2971. $movkey ($key,%rax),$rndkey1
  2972. add \$32,%rax
  2973. aesenc $rndkey0,$inout0
  2974. $movkey -16($key,%rax),$rndkey0
  2975. jnz .Locb_enc_loop1
  2976. aesenc $rndkey1,$inout0
  2977. $movkey 16($key_),$rndkey1 # redundant in tail
  2978. mov %r10,%rax # restore twisted rounds
  2979. aesenclast $inout5,$inout0
  2980. ret
  2981. .size __ocb_encrypt1,.-__ocb_encrypt1
  2982. .globl aesni_ocb_decrypt
  2983. .type aesni_ocb_decrypt,\@function,6
  2984. .align 32
  2985. aesni_ocb_decrypt:
  2986. .cfi_startproc
  2987. lea (%rsp),%rax
  2988. push %rbx
  2989. .cfi_push %rbx
  2990. push %rbp
  2991. .cfi_push %rbp
  2992. push %r12
  2993. .cfi_push %r12
  2994. push %r13
  2995. .cfi_push %r13
  2996. push %r14
  2997. .cfi_push %r14
  2998. ___
  2999. $code.=<<___ if ($win64);
  3000. lea -0xa0(%rsp),%rsp
  3001. movaps %xmm6,0x00(%rsp) # offload everything
  3002. movaps %xmm7,0x10(%rsp)
  3003. movaps %xmm8,0x20(%rsp)
  3004. movaps %xmm9,0x30(%rsp)
  3005. movaps %xmm10,0x40(%rsp)
  3006. movaps %xmm11,0x50(%rsp)
  3007. movaps %xmm12,0x60(%rsp)
  3008. movaps %xmm13,0x70(%rsp)
  3009. movaps %xmm14,0x80(%rsp)
  3010. movaps %xmm15,0x90(%rsp)
  3011. .Locb_dec_body:
  3012. ___
  3013. $code.=<<___;
  3014. mov $seventh_arg(%rax),$L_p # 7th argument
  3015. mov $seventh_arg+8(%rax),$checksum_p# 8th argument
  3016. mov 240($key),$rnds_
  3017. mov $key,$key_
  3018. shl \$4,$rnds_
  3019. $movkey ($key),$rndkey0l # round[0]
  3020. $movkey 16($key,$rnds_),$rndkey1 # round[last]
  3021. movdqu ($offset_p),@offset[5] # load last offset_i
  3022. pxor $rndkey1,$rndkey0l # round[0] ^ round[last]
  3023. pxor $rndkey1,@offset[5] # offset_i ^ round[last]
  3024. mov \$16+32,$rounds
  3025. lea 32($key_,$rnds_),$key
  3026. $movkey 16($key_),$rndkey1 # round[1]
  3027. sub %r10,%rax # twisted $rounds
  3028. mov %rax,%r10 # backup twisted $rounds
  3029. movdqu ($L_p),@offset[0] # L_0 for all odd-numbered blocks
  3030. movdqu ($checksum_p),$checksum # load checksum
  3031. test \$1,$block_num # is first block number odd?
  3032. jnz .Locb_dec_odd
  3033. bsf $block_num,$i1
  3034. add \$1,$block_num
  3035. shl \$4,$i1
  3036. movdqu ($L_p,$i1),$inout5 # borrow
  3037. movdqu ($inp),$inout0
  3038. lea 16($inp),$inp
  3039. call __ocb_decrypt1
  3040. movdqa $inout5,@offset[5]
  3041. movups $inout0,($out)
  3042. xorps $inout0,$checksum # accumulate checksum
  3043. lea 16($out),$out
  3044. sub \$1,$blocks
  3045. jz .Locb_dec_done
  3046. .Locb_dec_odd:
  3047. lea 1($block_num),$i1 # even-numbered blocks
  3048. lea 3($block_num),$i3
  3049. lea 5($block_num),$i5
  3050. lea 6($block_num),$block_num
  3051. bsf $i1,$i1 # ntz(block)
  3052. bsf $i3,$i3
  3053. bsf $i5,$i5
  3054. shl \$4,$i1 # ntz(block) -> table offset
  3055. shl \$4,$i3
  3056. shl \$4,$i5
  3057. sub \$6,$blocks
  3058. jc .Locb_dec_short
  3059. jmp .Locb_dec_grandloop
  3060. .align 32
  3061. .Locb_dec_grandloop:
  3062. movdqu `16*0`($inp),$inout0 # load input
  3063. movdqu `16*1`($inp),$inout1
  3064. movdqu `16*2`($inp),$inout2
  3065. movdqu `16*3`($inp),$inout3
  3066. movdqu `16*4`($inp),$inout4
  3067. movdqu `16*5`($inp),$inout5
  3068. lea `16*6`($inp),$inp
  3069. call __ocb_decrypt6
  3070. movups $inout0,`16*0`($out) # store output
  3071. pxor $inout0,$checksum # accumulate checksum
  3072. movups $inout1,`16*1`($out)
  3073. pxor $inout1,$checksum
  3074. movups $inout2,`16*2`($out)
  3075. pxor $inout2,$checksum
  3076. movups $inout3,`16*3`($out)
  3077. pxor $inout3,$checksum
  3078. movups $inout4,`16*4`($out)
  3079. pxor $inout4,$checksum
  3080. movups $inout5,`16*5`($out)
  3081. pxor $inout5,$checksum
  3082. lea `16*6`($out),$out
  3083. sub \$6,$blocks
  3084. jnc .Locb_dec_grandloop
  3085. .Locb_dec_short:
  3086. add \$6,$blocks
  3087. jz .Locb_dec_done
  3088. movdqu `16*0`($inp),$inout0
  3089. cmp \$2,$blocks
  3090. jb .Locb_dec_one
  3091. movdqu `16*1`($inp),$inout1
  3092. je .Locb_dec_two
  3093. movdqu `16*2`($inp),$inout2
  3094. cmp \$4,$blocks
  3095. jb .Locb_dec_three
  3096. movdqu `16*3`($inp),$inout3
  3097. je .Locb_dec_four
  3098. movdqu `16*4`($inp),$inout4
  3099. pxor $inout5,$inout5
  3100. call __ocb_decrypt6
  3101. movdqa @offset[4],@offset[5]
  3102. movups $inout0,`16*0`($out) # store output
  3103. pxor $inout0,$checksum # accumulate checksum
  3104. movups $inout1,`16*1`($out)
  3105. pxor $inout1,$checksum
  3106. movups $inout2,`16*2`($out)
  3107. pxor $inout2,$checksum
  3108. movups $inout3,`16*3`($out)
  3109. pxor $inout3,$checksum
  3110. movups $inout4,`16*4`($out)
  3111. pxor $inout4,$checksum
  3112. jmp .Locb_dec_done
  3113. .align 16
  3114. .Locb_dec_one:
  3115. movdqa @offset[0],$inout5 # borrow
  3116. call __ocb_decrypt1
  3117. movdqa $inout5,@offset[5]
  3118. movups $inout0,`16*0`($out) # store output
  3119. xorps $inout0,$checksum # accumulate checksum
  3120. jmp .Locb_dec_done
  3121. .align 16
  3122. .Locb_dec_two:
  3123. pxor $inout2,$inout2
  3124. pxor $inout3,$inout3
  3125. call __ocb_decrypt4
  3126. movdqa @offset[1],@offset[5]
  3127. movups $inout0,`16*0`($out) # store output
  3128. xorps $inout0,$checksum # accumulate checksum
  3129. movups $inout1,`16*1`($out)
  3130. xorps $inout1,$checksum
  3131. jmp .Locb_dec_done
  3132. .align 16
  3133. .Locb_dec_three:
  3134. pxor $inout3,$inout3
  3135. call __ocb_decrypt4
  3136. movdqa @offset[2],@offset[5]
  3137. movups $inout0,`16*0`($out) # store output
  3138. xorps $inout0,$checksum # accumulate checksum
  3139. movups $inout1,`16*1`($out)
  3140. xorps $inout1,$checksum
  3141. movups $inout2,`16*2`($out)
  3142. xorps $inout2,$checksum
  3143. jmp .Locb_dec_done
  3144. .align 16
  3145. .Locb_dec_four:
  3146. call __ocb_decrypt4
  3147. movdqa @offset[3],@offset[5]
  3148. movups $inout0,`16*0`($out) # store output
  3149. pxor $inout0,$checksum # accumulate checksum
  3150. movups $inout1,`16*1`($out)
  3151. pxor $inout1,$checksum
  3152. movups $inout2,`16*2`($out)
  3153. pxor $inout2,$checksum
  3154. movups $inout3,`16*3`($out)
  3155. pxor $inout3,$checksum
  3156. .Locb_dec_done:
  3157. pxor $rndkey0,@offset[5] # "remove" round[last]
  3158. movdqu $checksum,($checksum_p) # store checksum
  3159. movdqu @offset[5],($offset_p) # store last offset_i
  3160. xorps %xmm0,%xmm0 # clear register bank
  3161. pxor %xmm1,%xmm1
  3162. pxor %xmm2,%xmm2
  3163. pxor %xmm3,%xmm3
  3164. pxor %xmm4,%xmm4
  3165. pxor %xmm5,%xmm5
  3166. ___
  3167. $code.=<<___ if (!$win64);
  3168. pxor %xmm6,%xmm6
  3169. pxor %xmm7,%xmm7
  3170. pxor %xmm8,%xmm8
  3171. pxor %xmm9,%xmm9
  3172. pxor %xmm10,%xmm10
  3173. pxor %xmm11,%xmm11
  3174. pxor %xmm12,%xmm12
  3175. pxor %xmm13,%xmm13
  3176. pxor %xmm14,%xmm14
  3177. pxor %xmm15,%xmm15
  3178. lea 0x28(%rsp),%rax
  3179. .cfi_def_cfa %rax,8
  3180. ___
  3181. $code.=<<___ if ($win64);
  3182. movaps 0x00(%rsp),%xmm6
  3183. movaps %xmm0,0x00(%rsp) # clear stack
  3184. movaps 0x10(%rsp),%xmm7
  3185. movaps %xmm0,0x10(%rsp)
  3186. movaps 0x20(%rsp),%xmm8
  3187. movaps %xmm0,0x20(%rsp)
  3188. movaps 0x30(%rsp),%xmm9
  3189. movaps %xmm0,0x30(%rsp)
  3190. movaps 0x40(%rsp),%xmm10
  3191. movaps %xmm0,0x40(%rsp)
  3192. movaps 0x50(%rsp),%xmm11
  3193. movaps %xmm0,0x50(%rsp)
  3194. movaps 0x60(%rsp),%xmm12
  3195. movaps %xmm0,0x60(%rsp)
  3196. movaps 0x70(%rsp),%xmm13
  3197. movaps %xmm0,0x70(%rsp)
  3198. movaps 0x80(%rsp),%xmm14
  3199. movaps %xmm0,0x80(%rsp)
  3200. movaps 0x90(%rsp),%xmm15
  3201. movaps %xmm0,0x90(%rsp)
  3202. lea 0xa0+0x28(%rsp),%rax
  3203. .Locb_dec_pop:
  3204. ___
  3205. $code.=<<___;
  3206. mov -40(%rax),%r14
  3207. .cfi_restore %r14
  3208. mov -32(%rax),%r13
  3209. .cfi_restore %r13
  3210. mov -24(%rax),%r12
  3211. .cfi_restore %r12
  3212. mov -16(%rax),%rbp
  3213. .cfi_restore %rbp
  3214. mov -8(%rax),%rbx
  3215. .cfi_restore %rbx
  3216. lea (%rax),%rsp
  3217. .cfi_def_cfa_register %rsp
  3218. .Locb_dec_epilogue:
  3219. ret
  3220. .cfi_endproc
  3221. .size aesni_ocb_decrypt,.-aesni_ocb_decrypt
  3222. .type __ocb_decrypt6,\@abi-omnipotent
  3223. .align 32
  3224. __ocb_decrypt6:
  3225. pxor $rndkey0l,@offset[5] # offset_i ^ round[0]
  3226. movdqu ($L_p,$i1),@offset[1]
  3227. movdqa @offset[0],@offset[2]
  3228. movdqu ($L_p,$i3),@offset[3]
  3229. movdqa @offset[0],@offset[4]
  3230. pxor @offset[5],@offset[0]
  3231. movdqu ($L_p,$i5),@offset[5]
  3232. pxor @offset[0],@offset[1]
  3233. pxor @offset[0],$inout0 # input ^ round[0] ^ offset_i
  3234. pxor @offset[1],@offset[2]
  3235. pxor @offset[1],$inout1
  3236. pxor @offset[2],@offset[3]
  3237. pxor @offset[2],$inout2
  3238. pxor @offset[3],@offset[4]
  3239. pxor @offset[3],$inout3
  3240. pxor @offset[4],@offset[5]
  3241. pxor @offset[4],$inout4
  3242. pxor @offset[5],$inout5
  3243. $movkey 32($key_),$rndkey0
  3244. lea 1($block_num),$i1 # even-numbered blocks
  3245. lea 3($block_num),$i3
  3246. lea 5($block_num),$i5
  3247. add \$6,$block_num
  3248. pxor $rndkey0l,@offset[0] # offset_i ^ round[last]
  3249. bsf $i1,$i1 # ntz(block)
  3250. bsf $i3,$i3
  3251. bsf $i5,$i5
  3252. aesdec $rndkey1,$inout0
  3253. aesdec $rndkey1,$inout1
  3254. aesdec $rndkey1,$inout2
  3255. aesdec $rndkey1,$inout3
  3256. pxor $rndkey0l,@offset[1]
  3257. pxor $rndkey0l,@offset[2]
  3258. aesdec $rndkey1,$inout4
  3259. pxor $rndkey0l,@offset[3]
  3260. pxor $rndkey0l,@offset[4]
  3261. aesdec $rndkey1,$inout5
  3262. $movkey 48($key_),$rndkey1
  3263. pxor $rndkey0l,@offset[5]
  3264. aesdec $rndkey0,$inout0
  3265. aesdec $rndkey0,$inout1
  3266. aesdec $rndkey0,$inout2
  3267. aesdec $rndkey0,$inout3
  3268. aesdec $rndkey0,$inout4
  3269. aesdec $rndkey0,$inout5
  3270. $movkey 64($key_),$rndkey0
  3271. shl \$4,$i1 # ntz(block) -> table offset
  3272. shl \$4,$i3
  3273. jmp .Locb_dec_loop6
  3274. .align 32
  3275. .Locb_dec_loop6:
  3276. aesdec $rndkey1,$inout0
  3277. aesdec $rndkey1,$inout1
  3278. aesdec $rndkey1,$inout2
  3279. aesdec $rndkey1,$inout3
  3280. aesdec $rndkey1,$inout4
  3281. aesdec $rndkey1,$inout5
  3282. $movkey ($key,%rax),$rndkey1
  3283. add \$32,%rax
  3284. aesdec $rndkey0,$inout0
  3285. aesdec $rndkey0,$inout1
  3286. aesdec $rndkey0,$inout2
  3287. aesdec $rndkey0,$inout3
  3288. aesdec $rndkey0,$inout4
  3289. aesdec $rndkey0,$inout5
  3290. $movkey -16($key,%rax),$rndkey0
  3291. jnz .Locb_dec_loop6
  3292. aesdec $rndkey1,$inout0
  3293. aesdec $rndkey1,$inout1
  3294. aesdec $rndkey1,$inout2
  3295. aesdec $rndkey1,$inout3
  3296. aesdec $rndkey1,$inout4
  3297. aesdec $rndkey1,$inout5
  3298. $movkey 16($key_),$rndkey1
  3299. shl \$4,$i5
  3300. aesdeclast @offset[0],$inout0
  3301. movdqu ($L_p),@offset[0] # L_0 for all odd-numbered blocks
  3302. mov %r10,%rax # restore twisted rounds
  3303. aesdeclast @offset[1],$inout1
  3304. aesdeclast @offset[2],$inout2
  3305. aesdeclast @offset[3],$inout3
  3306. aesdeclast @offset[4],$inout4
  3307. aesdeclast @offset[5],$inout5
  3308. ret
  3309. .size __ocb_decrypt6,.-__ocb_decrypt6
  3310. .type __ocb_decrypt4,\@abi-omnipotent
  3311. .align 32
  3312. __ocb_decrypt4:
  3313. pxor $rndkey0l,@offset[5] # offset_i ^ round[0]
  3314. movdqu ($L_p,$i1),@offset[1]
  3315. movdqa @offset[0],@offset[2]
  3316. movdqu ($L_p,$i3),@offset[3]
  3317. pxor @offset[5],@offset[0]
  3318. pxor @offset[0],@offset[1]
  3319. pxor @offset[0],$inout0 # input ^ round[0] ^ offset_i
  3320. pxor @offset[1],@offset[2]
  3321. pxor @offset[1],$inout1
  3322. pxor @offset[2],@offset[3]
  3323. pxor @offset[2],$inout2
  3324. pxor @offset[3],$inout3
  3325. $movkey 32($key_),$rndkey0
  3326. pxor $rndkey0l,@offset[0] # offset_i ^ round[last]
  3327. pxor $rndkey0l,@offset[1]
  3328. pxor $rndkey0l,@offset[2]
  3329. pxor $rndkey0l,@offset[3]
  3330. aesdec $rndkey1,$inout0
  3331. aesdec $rndkey1,$inout1
  3332. aesdec $rndkey1,$inout2
  3333. aesdec $rndkey1,$inout3
  3334. $movkey 48($key_),$rndkey1
  3335. aesdec $rndkey0,$inout0
  3336. aesdec $rndkey0,$inout1
  3337. aesdec $rndkey0,$inout2
  3338. aesdec $rndkey0,$inout3
  3339. $movkey 64($key_),$rndkey0
  3340. jmp .Locb_dec_loop4
  3341. .align 32
  3342. .Locb_dec_loop4:
  3343. aesdec $rndkey1,$inout0
  3344. aesdec $rndkey1,$inout1
  3345. aesdec $rndkey1,$inout2
  3346. aesdec $rndkey1,$inout3
  3347. $movkey ($key,%rax),$rndkey1
  3348. add \$32,%rax
  3349. aesdec $rndkey0,$inout0
  3350. aesdec $rndkey0,$inout1
  3351. aesdec $rndkey0,$inout2
  3352. aesdec $rndkey0,$inout3
  3353. $movkey -16($key,%rax),$rndkey0
  3354. jnz .Locb_dec_loop4
  3355. aesdec $rndkey1,$inout0
  3356. aesdec $rndkey1,$inout1
  3357. aesdec $rndkey1,$inout2
  3358. aesdec $rndkey1,$inout3
  3359. $movkey 16($key_),$rndkey1
  3360. mov %r10,%rax # restore twisted rounds
  3361. aesdeclast @offset[0],$inout0
  3362. aesdeclast @offset[1],$inout1
  3363. aesdeclast @offset[2],$inout2
  3364. aesdeclast @offset[3],$inout3
  3365. ret
  3366. .size __ocb_decrypt4,.-__ocb_decrypt4
  3367. .type __ocb_decrypt1,\@abi-omnipotent
  3368. .align 32
  3369. __ocb_decrypt1:
  3370. pxor @offset[5],$inout5 # offset_i
  3371. pxor $rndkey0l,$inout5 # offset_i ^ round[0]
  3372. pxor $inout5,$inout0 # input ^ round[0] ^ offset_i
  3373. $movkey 32($key_),$rndkey0
  3374. aesdec $rndkey1,$inout0
  3375. $movkey 48($key_),$rndkey1
  3376. pxor $rndkey0l,$inout5 # offset_i ^ round[last]
  3377. aesdec $rndkey0,$inout0
  3378. $movkey 64($key_),$rndkey0
  3379. jmp .Locb_dec_loop1
  3380. .align 32
  3381. .Locb_dec_loop1:
  3382. aesdec $rndkey1,$inout0
  3383. $movkey ($key,%rax),$rndkey1
  3384. add \$32,%rax
  3385. aesdec $rndkey0,$inout0
  3386. $movkey -16($key,%rax),$rndkey0
  3387. jnz .Locb_dec_loop1
  3388. aesdec $rndkey1,$inout0
  3389. $movkey 16($key_),$rndkey1 # redundant in tail
  3390. mov %r10,%rax # restore twisted rounds
  3391. aesdeclast $inout5,$inout0
  3392. ret
  3393. .size __ocb_decrypt1,.-__ocb_decrypt1
  3394. ___
  3395. } }}
  3396. ########################################################################
  3397. # void $PREFIX_cbc_encrypt (const void *inp, void *out,
  3398. # size_t length, const AES_KEY *key,
  3399. # unsigned char *ivp,const int enc);
  3400. {
  3401. my $frame_size = 0x10 + ($win64?0xa0:0); # used in decrypt
  3402. my ($iv,$in0,$in1,$in2,$in3,$in4)=map("%xmm$_",(10..15));
  3403. $code.=<<___;
  3404. .globl ${PREFIX}_cbc_encrypt
  3405. .type ${PREFIX}_cbc_encrypt,\@function,6
  3406. .align 16
  3407. ${PREFIX}_cbc_encrypt:
  3408. .cfi_startproc
  3409. test $len,$len # check length
  3410. jz .Lcbc_ret
  3411. mov 240($key),$rnds_ # key->rounds
  3412. mov $key,$key_ # backup $key
  3413. test %r9d,%r9d # 6th argument
  3414. jz .Lcbc_decrypt
  3415. #--------------------------- CBC ENCRYPT ------------------------------#
  3416. movups ($ivp),$inout0 # load iv as initial state
  3417. mov $rnds_,$rounds
  3418. cmp \$16,$len
  3419. jb .Lcbc_enc_tail
  3420. sub \$16,$len
  3421. jmp .Lcbc_enc_loop
  3422. .align 16
  3423. .Lcbc_enc_loop:
  3424. movups ($inp),$inout1 # load input
  3425. lea 16($inp),$inp
  3426. #xorps $inout1,$inout0
  3427. ___
  3428. &aesni_generate1("enc",$key,$rounds,$inout0,$inout1);
  3429. $code.=<<___;
  3430. mov $rnds_,$rounds # restore $rounds
  3431. mov $key_,$key # restore $key
  3432. movups $inout0,0($out) # store output
  3433. lea 16($out),$out
  3434. sub \$16,$len
  3435. jnc .Lcbc_enc_loop
  3436. add \$16,$len
  3437. jnz .Lcbc_enc_tail
  3438. pxor $rndkey0,$rndkey0 # clear register bank
  3439. pxor $rndkey1,$rndkey1
  3440. movups $inout0,($ivp)
  3441. pxor $inout0,$inout0
  3442. pxor $inout1,$inout1
  3443. jmp .Lcbc_ret
  3444. .Lcbc_enc_tail:
  3445. mov $len,%rcx # zaps $key
  3446. xchg $inp,$out # $inp is %rsi and $out is %rdi now
  3447. .long 0x9066A4F3 # rep movsb
  3448. mov \$16,%ecx # zero tail
  3449. sub $len,%rcx
  3450. xor %eax,%eax
  3451. .long 0x9066AAF3 # rep stosb
  3452. lea -16(%rdi),%rdi # rewind $out by 1 block
  3453. mov $rnds_,$rounds # restore $rounds
  3454. mov %rdi,%rsi # $inp and $out are the same
  3455. mov $key_,$key # restore $key
  3456. xor $len,$len # len=16
  3457. jmp .Lcbc_enc_loop # one more spin
  3458. #--------------------------- CBC DECRYPT ------------------------------#
  3459. .align 16
  3460. .Lcbc_decrypt:
  3461. cmp \$16,$len
  3462. jne .Lcbc_decrypt_bulk
  3463. # handle single block without allocating stack frame,
  3464. # useful in ciphertext stealing mode
  3465. movdqu ($inp),$inout0 # load input
  3466. movdqu ($ivp),$inout1 # load iv
  3467. movdqa $inout0,$inout2 # future iv
  3468. ___
  3469. &aesni_generate1("dec",$key,$rnds_);
  3470. $code.=<<___;
  3471. pxor $rndkey0,$rndkey0 # clear register bank
  3472. pxor $rndkey1,$rndkey1
  3473. movdqu $inout2,($ivp) # store iv
  3474. xorps $inout1,$inout0 # ^=iv
  3475. pxor $inout1,$inout1
  3476. movups $inout0,($out) # store output
  3477. pxor $inout0,$inout0
  3478. jmp .Lcbc_ret
  3479. .align 16
  3480. .Lcbc_decrypt_bulk:
  3481. lea (%rsp),%r11 # frame pointer
  3482. .cfi_def_cfa_register %r11
  3483. push %rbp
  3484. .cfi_push %rbp
  3485. sub \$$frame_size,%rsp
  3486. and \$-16,%rsp # Linux kernel stack can be incorrectly seeded
  3487. ___
  3488. $code.=<<___ if ($win64);
  3489. movaps %xmm6,0x10(%rsp)
  3490. movaps %xmm7,0x20(%rsp)
  3491. movaps %xmm8,0x30(%rsp)
  3492. movaps %xmm9,0x40(%rsp)
  3493. movaps %xmm10,0x50(%rsp)
  3494. movaps %xmm11,0x60(%rsp)
  3495. movaps %xmm12,0x70(%rsp)
  3496. movaps %xmm13,0x80(%rsp)
  3497. movaps %xmm14,0x90(%rsp)
  3498. movaps %xmm15,0xa0(%rsp)
  3499. .Lcbc_decrypt_body:
  3500. ___
  3501. my $inp_=$key_="%rbp"; # reassign $key_
  3502. $code.=<<___;
  3503. mov $key,$key_ # [re-]backup $key [after reassignment]
  3504. movups ($ivp),$iv
  3505. mov $rnds_,$rounds
  3506. cmp \$0x50,$len
  3507. jbe .Lcbc_dec_tail
  3508. $movkey ($key),$rndkey0
  3509. movdqu 0x00($inp),$inout0 # load input
  3510. movdqu 0x10($inp),$inout1
  3511. movdqa $inout0,$in0
  3512. movdqu 0x20($inp),$inout2
  3513. movdqa $inout1,$in1
  3514. movdqu 0x30($inp),$inout3
  3515. movdqa $inout2,$in2
  3516. movdqu 0x40($inp),$inout4
  3517. movdqa $inout3,$in3
  3518. movdqu 0x50($inp),$inout5
  3519. movdqa $inout4,$in4
  3520. mov OPENSSL_ia32cap_P+4(%rip),%r9d
  3521. cmp \$0x70,$len
  3522. jbe .Lcbc_dec_six_or_seven
  3523. and \$`1<<26|1<<22`,%r9d # isolate XSAVE+MOVBE
  3524. sub \$0x50,$len # $len is biased by -5*16
  3525. cmp \$`1<<22`,%r9d # check for MOVBE without XSAVE
  3526. je .Lcbc_dec_loop6_enter # [which denotes Atom Silvermont]
  3527. sub \$0x20,$len # $len is biased by -7*16
  3528. lea 0x70($key),$key # size optimization
  3529. jmp .Lcbc_dec_loop8_enter
  3530. .align 16
  3531. .Lcbc_dec_loop8:
  3532. movups $inout7,($out)
  3533. lea 0x10($out),$out
  3534. .Lcbc_dec_loop8_enter:
  3535. movdqu 0x60($inp),$inout6
  3536. pxor $rndkey0,$inout0
  3537. movdqu 0x70($inp),$inout7
  3538. pxor $rndkey0,$inout1
  3539. $movkey 0x10-0x70($key),$rndkey1
  3540. pxor $rndkey0,$inout2
  3541. mov \$-1,$inp_
  3542. cmp \$0x70,$len # is there at least 0x60 bytes ahead?
  3543. pxor $rndkey0,$inout3
  3544. pxor $rndkey0,$inout4
  3545. pxor $rndkey0,$inout5
  3546. pxor $rndkey0,$inout6
  3547. aesdec $rndkey1,$inout0
  3548. pxor $rndkey0,$inout7
  3549. $movkey 0x20-0x70($key),$rndkey0
  3550. aesdec $rndkey1,$inout1
  3551. aesdec $rndkey1,$inout2
  3552. aesdec $rndkey1,$inout3
  3553. aesdec $rndkey1,$inout4
  3554. aesdec $rndkey1,$inout5
  3555. aesdec $rndkey1,$inout6
  3556. adc \$0,$inp_
  3557. and \$128,$inp_
  3558. aesdec $rndkey1,$inout7
  3559. add $inp,$inp_
  3560. $movkey 0x30-0x70($key),$rndkey1
  3561. ___
  3562. for($i=1;$i<12;$i++) {
  3563. my $rndkeyx = ($i&1)?$rndkey0:$rndkey1;
  3564. $code.=<<___ if ($i==7);
  3565. cmp \$11,$rounds
  3566. ___
  3567. $code.=<<___;
  3568. aesdec $rndkeyx,$inout0
  3569. aesdec $rndkeyx,$inout1
  3570. aesdec $rndkeyx,$inout2
  3571. aesdec $rndkeyx,$inout3
  3572. aesdec $rndkeyx,$inout4
  3573. aesdec $rndkeyx,$inout5
  3574. aesdec $rndkeyx,$inout6
  3575. aesdec $rndkeyx,$inout7
  3576. $movkey `0x30+0x10*$i`-0x70($key),$rndkeyx
  3577. ___
  3578. $code.=<<___ if ($i<6 || (!($i&1) && $i>7));
  3579. nop
  3580. ___
  3581. $code.=<<___ if ($i==7);
  3582. jb .Lcbc_dec_done
  3583. ___
  3584. $code.=<<___ if ($i==9);
  3585. je .Lcbc_dec_done
  3586. ___
  3587. $code.=<<___ if ($i==11);
  3588. jmp .Lcbc_dec_done
  3589. ___
  3590. }
  3591. $code.=<<___;
  3592. .align 16
  3593. .Lcbc_dec_done:
  3594. aesdec $rndkey1,$inout0
  3595. aesdec $rndkey1,$inout1
  3596. pxor $rndkey0,$iv
  3597. pxor $rndkey0,$in0
  3598. aesdec $rndkey1,$inout2
  3599. aesdec $rndkey1,$inout3
  3600. pxor $rndkey0,$in1
  3601. pxor $rndkey0,$in2
  3602. aesdec $rndkey1,$inout4
  3603. aesdec $rndkey1,$inout5
  3604. pxor $rndkey0,$in3
  3605. pxor $rndkey0,$in4
  3606. aesdec $rndkey1,$inout6
  3607. aesdec $rndkey1,$inout7
  3608. movdqu 0x50($inp),$rndkey1
  3609. aesdeclast $iv,$inout0
  3610. movdqu 0x60($inp),$iv # borrow $iv
  3611. pxor $rndkey0,$rndkey1
  3612. aesdeclast $in0,$inout1
  3613. pxor $rndkey0,$iv
  3614. movdqu 0x70($inp),$rndkey0 # next IV
  3615. aesdeclast $in1,$inout2
  3616. lea 0x80($inp),$inp
  3617. movdqu 0x00($inp_),$in0
  3618. aesdeclast $in2,$inout3
  3619. aesdeclast $in3,$inout4
  3620. movdqu 0x10($inp_),$in1
  3621. movdqu 0x20($inp_),$in2
  3622. aesdeclast $in4,$inout5
  3623. aesdeclast $rndkey1,$inout6
  3624. movdqu 0x30($inp_),$in3
  3625. movdqu 0x40($inp_),$in4
  3626. aesdeclast $iv,$inout7
  3627. movdqa $rndkey0,$iv # return $iv
  3628. movdqu 0x50($inp_),$rndkey1
  3629. $movkey -0x70($key),$rndkey0
  3630. movups $inout0,($out) # store output
  3631. movdqa $in0,$inout0
  3632. movups $inout1,0x10($out)
  3633. movdqa $in1,$inout1
  3634. movups $inout2,0x20($out)
  3635. movdqa $in2,$inout2
  3636. movups $inout3,0x30($out)
  3637. movdqa $in3,$inout3
  3638. movups $inout4,0x40($out)
  3639. movdqa $in4,$inout4
  3640. movups $inout5,0x50($out)
  3641. movdqa $rndkey1,$inout5
  3642. movups $inout6,0x60($out)
  3643. lea 0x70($out),$out
  3644. sub \$0x80,$len
  3645. ja .Lcbc_dec_loop8
  3646. movaps $inout7,$inout0
  3647. lea -0x70($key),$key
  3648. add \$0x70,$len
  3649. jle .Lcbc_dec_clear_tail_collected
  3650. movups $inout7,($out)
  3651. lea 0x10($out),$out
  3652. cmp \$0x50,$len
  3653. jbe .Lcbc_dec_tail
  3654. movaps $in0,$inout0
  3655. .Lcbc_dec_six_or_seven:
  3656. cmp \$0x60,$len
  3657. ja .Lcbc_dec_seven
  3658. movaps $inout5,$inout6
  3659. call _aesni_decrypt6
  3660. pxor $iv,$inout0 # ^= IV
  3661. movaps $inout6,$iv
  3662. pxor $in0,$inout1
  3663. movdqu $inout0,($out)
  3664. pxor $in1,$inout2
  3665. movdqu $inout1,0x10($out)
  3666. pxor $inout1,$inout1 # clear register bank
  3667. pxor $in2,$inout3
  3668. movdqu $inout2,0x20($out)
  3669. pxor $inout2,$inout2
  3670. pxor $in3,$inout4
  3671. movdqu $inout3,0x30($out)
  3672. pxor $inout3,$inout3
  3673. pxor $in4,$inout5
  3674. movdqu $inout4,0x40($out)
  3675. pxor $inout4,$inout4
  3676. lea 0x50($out),$out
  3677. movdqa $inout5,$inout0
  3678. pxor $inout5,$inout5
  3679. jmp .Lcbc_dec_tail_collected
  3680. .align 16
  3681. .Lcbc_dec_seven:
  3682. movups 0x60($inp),$inout6
  3683. xorps $inout7,$inout7
  3684. call _aesni_decrypt8
  3685. movups 0x50($inp),$inout7
  3686. pxor $iv,$inout0 # ^= IV
  3687. movups 0x60($inp),$iv
  3688. pxor $in0,$inout1
  3689. movdqu $inout0,($out)
  3690. pxor $in1,$inout2
  3691. movdqu $inout1,0x10($out)
  3692. pxor $inout1,$inout1 # clear register bank
  3693. pxor $in2,$inout3
  3694. movdqu $inout2,0x20($out)
  3695. pxor $inout2,$inout2
  3696. pxor $in3,$inout4
  3697. movdqu $inout3,0x30($out)
  3698. pxor $inout3,$inout3
  3699. pxor $in4,$inout5
  3700. movdqu $inout4,0x40($out)
  3701. pxor $inout4,$inout4
  3702. pxor $inout7,$inout6
  3703. movdqu $inout5,0x50($out)
  3704. pxor $inout5,$inout5
  3705. lea 0x60($out),$out
  3706. movdqa $inout6,$inout0
  3707. pxor $inout6,$inout6
  3708. pxor $inout7,$inout7
  3709. jmp .Lcbc_dec_tail_collected
  3710. .align 16
  3711. .Lcbc_dec_loop6:
  3712. movups $inout5,($out)
  3713. lea 0x10($out),$out
  3714. movdqu 0x00($inp),$inout0 # load input
  3715. movdqu 0x10($inp),$inout1
  3716. movdqa $inout0,$in0
  3717. movdqu 0x20($inp),$inout2
  3718. movdqa $inout1,$in1
  3719. movdqu 0x30($inp),$inout3
  3720. movdqa $inout2,$in2
  3721. movdqu 0x40($inp),$inout4
  3722. movdqa $inout3,$in3
  3723. movdqu 0x50($inp),$inout5
  3724. movdqa $inout4,$in4
  3725. .Lcbc_dec_loop6_enter:
  3726. lea 0x60($inp),$inp
  3727. movdqa $inout5,$inout6
  3728. call _aesni_decrypt6
  3729. pxor $iv,$inout0 # ^= IV
  3730. movdqa $inout6,$iv
  3731. pxor $in0,$inout1
  3732. movdqu $inout0,($out)
  3733. pxor $in1,$inout2
  3734. movdqu $inout1,0x10($out)
  3735. pxor $in2,$inout3
  3736. movdqu $inout2,0x20($out)
  3737. pxor $in3,$inout4
  3738. mov $key_,$key
  3739. movdqu $inout3,0x30($out)
  3740. pxor $in4,$inout5
  3741. mov $rnds_,$rounds
  3742. movdqu $inout4,0x40($out)
  3743. lea 0x50($out),$out
  3744. sub \$0x60,$len
  3745. ja .Lcbc_dec_loop6
  3746. movdqa $inout5,$inout0
  3747. add \$0x50,$len
  3748. jle .Lcbc_dec_clear_tail_collected
  3749. movups $inout5,($out)
  3750. lea 0x10($out),$out
  3751. .Lcbc_dec_tail:
  3752. movups ($inp),$inout0
  3753. sub \$0x10,$len
  3754. jbe .Lcbc_dec_one # $len is 1*16 or less
  3755. movups 0x10($inp),$inout1
  3756. movaps $inout0,$in0
  3757. sub \$0x10,$len
  3758. jbe .Lcbc_dec_two # $len is 2*16 or less
  3759. movups 0x20($inp),$inout2
  3760. movaps $inout1,$in1
  3761. sub \$0x10,$len
  3762. jbe .Lcbc_dec_three # $len is 3*16 or less
  3763. movups 0x30($inp),$inout3
  3764. movaps $inout2,$in2
  3765. sub \$0x10,$len
  3766. jbe .Lcbc_dec_four # $len is 4*16 or less
  3767. movups 0x40($inp),$inout4 # $len is 5*16 or less
  3768. movaps $inout3,$in3
  3769. movaps $inout4,$in4
  3770. xorps $inout5,$inout5
  3771. call _aesni_decrypt6
  3772. pxor $iv,$inout0
  3773. movaps $in4,$iv
  3774. pxor $in0,$inout1
  3775. movdqu $inout0,($out)
  3776. pxor $in1,$inout2
  3777. movdqu $inout1,0x10($out)
  3778. pxor $inout1,$inout1 # clear register bank
  3779. pxor $in2,$inout3
  3780. movdqu $inout2,0x20($out)
  3781. pxor $inout2,$inout2
  3782. pxor $in3,$inout4
  3783. movdqu $inout3,0x30($out)
  3784. pxor $inout3,$inout3
  3785. lea 0x40($out),$out
  3786. movdqa $inout4,$inout0
  3787. pxor $inout4,$inout4
  3788. pxor $inout5,$inout5
  3789. sub \$0x10,$len
  3790. jmp .Lcbc_dec_tail_collected
  3791. .align 16
  3792. .Lcbc_dec_one:
  3793. movaps $inout0,$in0
  3794. ___
  3795. &aesni_generate1("dec",$key,$rounds);
  3796. $code.=<<___;
  3797. xorps $iv,$inout0
  3798. movaps $in0,$iv
  3799. jmp .Lcbc_dec_tail_collected
  3800. .align 16
  3801. .Lcbc_dec_two:
  3802. movaps $inout1,$in1
  3803. call _aesni_decrypt2
  3804. pxor $iv,$inout0
  3805. movaps $in1,$iv
  3806. pxor $in0,$inout1
  3807. movdqu $inout0,($out)
  3808. movdqa $inout1,$inout0
  3809. pxor $inout1,$inout1 # clear register bank
  3810. lea 0x10($out),$out
  3811. jmp .Lcbc_dec_tail_collected
  3812. .align 16
  3813. .Lcbc_dec_three:
  3814. movaps $inout2,$in2
  3815. call _aesni_decrypt3
  3816. pxor $iv,$inout0
  3817. movaps $in2,$iv
  3818. pxor $in0,$inout1
  3819. movdqu $inout0,($out)
  3820. pxor $in1,$inout2
  3821. movdqu $inout1,0x10($out)
  3822. pxor $inout1,$inout1 # clear register bank
  3823. movdqa $inout2,$inout0
  3824. pxor $inout2,$inout2
  3825. lea 0x20($out),$out
  3826. jmp .Lcbc_dec_tail_collected
  3827. .align 16
  3828. .Lcbc_dec_four:
  3829. movaps $inout3,$in3
  3830. call _aesni_decrypt4
  3831. pxor $iv,$inout0
  3832. movaps $in3,$iv
  3833. pxor $in0,$inout1
  3834. movdqu $inout0,($out)
  3835. pxor $in1,$inout2
  3836. movdqu $inout1,0x10($out)
  3837. pxor $inout1,$inout1 # clear register bank
  3838. pxor $in2,$inout3
  3839. movdqu $inout2,0x20($out)
  3840. pxor $inout2,$inout2
  3841. movdqa $inout3,$inout0
  3842. pxor $inout3,$inout3
  3843. lea 0x30($out),$out
  3844. jmp .Lcbc_dec_tail_collected
  3845. .align 16
  3846. .Lcbc_dec_clear_tail_collected:
  3847. pxor $inout1,$inout1 # clear register bank
  3848. pxor $inout2,$inout2
  3849. pxor $inout3,$inout3
  3850. ___
  3851. $code.=<<___ if (!$win64);
  3852. pxor $inout4,$inout4 # %xmm6..9
  3853. pxor $inout5,$inout5
  3854. pxor $inout6,$inout6
  3855. pxor $inout7,$inout7
  3856. ___
  3857. $code.=<<___;
  3858. .Lcbc_dec_tail_collected:
  3859. movups $iv,($ivp)
  3860. and \$15,$len
  3861. jnz .Lcbc_dec_tail_partial
  3862. movups $inout0,($out)
  3863. pxor $inout0,$inout0
  3864. jmp .Lcbc_dec_ret
  3865. .align 16
  3866. .Lcbc_dec_tail_partial:
  3867. movaps $inout0,(%rsp)
  3868. pxor $inout0,$inout0
  3869. mov \$16,%rcx
  3870. mov $out,%rdi
  3871. sub $len,%rcx
  3872. lea (%rsp),%rsi
  3873. .long 0x9066A4F3 # rep movsb
  3874. movdqa $inout0,(%rsp)
  3875. .Lcbc_dec_ret:
  3876. xorps $rndkey0,$rndkey0 # %xmm0
  3877. pxor $rndkey1,$rndkey1
  3878. ___
  3879. $code.=<<___ if ($win64);
  3880. movaps 0x10(%rsp),%xmm6
  3881. movaps %xmm0,0x10(%rsp) # clear stack
  3882. movaps 0x20(%rsp),%xmm7
  3883. movaps %xmm0,0x20(%rsp)
  3884. movaps 0x30(%rsp),%xmm8
  3885. movaps %xmm0,0x30(%rsp)
  3886. movaps 0x40(%rsp),%xmm9
  3887. movaps %xmm0,0x40(%rsp)
  3888. movaps 0x50(%rsp),%xmm10
  3889. movaps %xmm0,0x50(%rsp)
  3890. movaps 0x60(%rsp),%xmm11
  3891. movaps %xmm0,0x60(%rsp)
  3892. movaps 0x70(%rsp),%xmm12
  3893. movaps %xmm0,0x70(%rsp)
  3894. movaps 0x80(%rsp),%xmm13
  3895. movaps %xmm0,0x80(%rsp)
  3896. movaps 0x90(%rsp),%xmm14
  3897. movaps %xmm0,0x90(%rsp)
  3898. movaps 0xa0(%rsp),%xmm15
  3899. movaps %xmm0,0xa0(%rsp)
  3900. ___
  3901. $code.=<<___;
  3902. mov -8(%r11),%rbp
  3903. .cfi_restore %rbp
  3904. lea (%r11),%rsp
  3905. .cfi_def_cfa_register %rsp
  3906. .Lcbc_ret:
  3907. ret
  3908. .cfi_endproc
  3909. .size ${PREFIX}_cbc_encrypt,.-${PREFIX}_cbc_encrypt
  3910. ___
  3911. }
  3912. # int ${PREFIX}_set_decrypt_key(const unsigned char *inp,
  3913. # int bits, AES_KEY *key)
  3914. #
  3915. # input: $inp user-supplied key
  3916. # $bits $inp length in bits
  3917. # $key pointer to key schedule
  3918. # output: %eax 0 denoting success, -1 or -2 - failure (see C)
  3919. # *$key key schedule
  3920. #
  3921. { my ($inp,$bits,$key) = @_4args;
  3922. $bits =~ s/%r/%e/;
  3923. $code.=<<___;
  3924. .globl ${PREFIX}_set_decrypt_key
  3925. .type ${PREFIX}_set_decrypt_key,\@abi-omnipotent
  3926. .align 16
  3927. ${PREFIX}_set_decrypt_key:
  3928. .cfi_startproc
  3929. .byte 0x48,0x83,0xEC,0x08 # sub rsp,8
  3930. .cfi_adjust_cfa_offset 8
  3931. call __aesni_set_encrypt_key
  3932. shl \$4,$bits # rounds-1 after _aesni_set_encrypt_key
  3933. test %eax,%eax
  3934. jnz .Ldec_key_ret
  3935. lea 16($key,$bits),$inp # points at the end of key schedule
  3936. $movkey ($key),%xmm0 # just swap
  3937. $movkey ($inp),%xmm1
  3938. $movkey %xmm0,($inp)
  3939. $movkey %xmm1,($key)
  3940. lea 16($key),$key
  3941. lea -16($inp),$inp
  3942. .Ldec_key_inverse:
  3943. $movkey ($key),%xmm0 # swap and inverse
  3944. $movkey ($inp),%xmm1
  3945. aesimc %xmm0,%xmm0
  3946. aesimc %xmm1,%xmm1
  3947. lea 16($key),$key
  3948. lea -16($inp),$inp
  3949. $movkey %xmm0,16($inp)
  3950. $movkey %xmm1,-16($key)
  3951. cmp $key,$inp
  3952. ja .Ldec_key_inverse
  3953. $movkey ($key),%xmm0 # inverse middle
  3954. aesimc %xmm0,%xmm0
  3955. pxor %xmm1,%xmm1
  3956. $movkey %xmm0,($inp)
  3957. pxor %xmm0,%xmm0
  3958. .Ldec_key_ret:
  3959. add \$8,%rsp
  3960. .cfi_adjust_cfa_offset -8
  3961. ret
  3962. .cfi_endproc
  3963. .LSEH_end_set_decrypt_key:
  3964. .size ${PREFIX}_set_decrypt_key,.-${PREFIX}_set_decrypt_key
  3965. ___
  3966. # This is based on submission from Intel by
  3967. # Huang Ying
  3968. # Vinodh Gopal
  3969. # Kahraman Akdemir
  3970. #
  3971. # Aggressively optimized in respect to aeskeygenassist's critical path
  3972. # and is contained in %xmm0-5 to meet Win64 ABI requirement.
  3973. #
  3974. # int ${PREFIX}_set_encrypt_key(const unsigned char *inp,
  3975. # int bits, AES_KEY * const key);
  3976. #
  3977. # input: $inp user-supplied key
  3978. # $bits $inp length in bits
  3979. # $key pointer to key schedule
  3980. # output: %eax 0 denoting success, -1 or -2 - failure (see C)
  3981. # $bits rounds-1 (used in aesni_set_decrypt_key)
  3982. # *$key key schedule
  3983. # $key pointer to key schedule (used in
  3984. # aesni_set_decrypt_key)
  3985. #
  3986. # Subroutine is frame-less, which means that only volatile registers
  3987. # are used. Note that it's declared "abi-omnipotent", which means that
  3988. # amount of volatile registers is smaller on Windows.
  3989. #
  3990. $code.=<<___;
  3991. .globl ${PREFIX}_set_encrypt_key
  3992. .type ${PREFIX}_set_encrypt_key,\@abi-omnipotent
  3993. .align 16
  3994. ${PREFIX}_set_encrypt_key:
  3995. __aesni_set_encrypt_key:
  3996. .cfi_startproc
  3997. .byte 0x48,0x83,0xEC,0x08 # sub rsp,8
  3998. .cfi_adjust_cfa_offset 8
  3999. mov \$-1,%rax
  4000. test $inp,$inp
  4001. jz .Lenc_key_ret
  4002. test $key,$key
  4003. jz .Lenc_key_ret
  4004. mov \$`1<<28|1<<11`,%r10d # AVX and XOP bits
  4005. movups ($inp),%xmm0 # pull first 128 bits of *userKey
  4006. xorps %xmm4,%xmm4 # low dword of xmm4 is assumed 0
  4007. and OPENSSL_ia32cap_P+4(%rip),%r10d
  4008. lea 16($key),%rax # %rax is used as modifiable copy of $key
  4009. cmp \$256,$bits
  4010. je .L14rounds
  4011. cmp \$192,$bits
  4012. je .L12rounds
  4013. cmp \$128,$bits
  4014. jne .Lbad_keybits
  4015. .L10rounds:
  4016. mov \$9,$bits # 10 rounds for 128-bit key
  4017. cmp \$`1<<28`,%r10d # AVX, bit no XOP
  4018. je .L10rounds_alt
  4019. $movkey %xmm0,($key) # round 0
  4020. aeskeygenassist \$0x1,%xmm0,%xmm1 # round 1
  4021. call .Lkey_expansion_128_cold
  4022. aeskeygenassist \$0x2,%xmm0,%xmm1 # round 2
  4023. call .Lkey_expansion_128
  4024. aeskeygenassist \$0x4,%xmm0,%xmm1 # round 3
  4025. call .Lkey_expansion_128
  4026. aeskeygenassist \$0x8,%xmm0,%xmm1 # round 4
  4027. call .Lkey_expansion_128
  4028. aeskeygenassist \$0x10,%xmm0,%xmm1 # round 5
  4029. call .Lkey_expansion_128
  4030. aeskeygenassist \$0x20,%xmm0,%xmm1 # round 6
  4031. call .Lkey_expansion_128
  4032. aeskeygenassist \$0x40,%xmm0,%xmm1 # round 7
  4033. call .Lkey_expansion_128
  4034. aeskeygenassist \$0x80,%xmm0,%xmm1 # round 8
  4035. call .Lkey_expansion_128
  4036. aeskeygenassist \$0x1b,%xmm0,%xmm1 # round 9
  4037. call .Lkey_expansion_128
  4038. aeskeygenassist \$0x36,%xmm0,%xmm1 # round 10
  4039. call .Lkey_expansion_128
  4040. $movkey %xmm0,(%rax)
  4041. mov $bits,80(%rax) # 240(%rdx)
  4042. xor %eax,%eax
  4043. jmp .Lenc_key_ret
  4044. .align 16
  4045. .L10rounds_alt:
  4046. movdqa .Lkey_rotate(%rip),%xmm5
  4047. mov \$8,%r10d
  4048. movdqa .Lkey_rcon1(%rip),%xmm4
  4049. movdqa %xmm0,%xmm2
  4050. movdqu %xmm0,($key)
  4051. jmp .Loop_key128
  4052. .align 16
  4053. .Loop_key128:
  4054. pshufb %xmm5,%xmm0
  4055. aesenclast %xmm4,%xmm0
  4056. pslld \$1,%xmm4
  4057. lea 16(%rax),%rax
  4058. movdqa %xmm2,%xmm3
  4059. pslldq \$4,%xmm2
  4060. pxor %xmm2,%xmm3
  4061. pslldq \$4,%xmm2
  4062. pxor %xmm2,%xmm3
  4063. pslldq \$4,%xmm2
  4064. pxor %xmm3,%xmm2
  4065. pxor %xmm2,%xmm0
  4066. movdqu %xmm0,-16(%rax)
  4067. movdqa %xmm0,%xmm2
  4068. dec %r10d
  4069. jnz .Loop_key128
  4070. movdqa .Lkey_rcon1b(%rip),%xmm4
  4071. pshufb %xmm5,%xmm0
  4072. aesenclast %xmm4,%xmm0
  4073. pslld \$1,%xmm4
  4074. movdqa %xmm2,%xmm3
  4075. pslldq \$4,%xmm2
  4076. pxor %xmm2,%xmm3
  4077. pslldq \$4,%xmm2
  4078. pxor %xmm2,%xmm3
  4079. pslldq \$4,%xmm2
  4080. pxor %xmm3,%xmm2
  4081. pxor %xmm2,%xmm0
  4082. movdqu %xmm0,(%rax)
  4083. movdqa %xmm0,%xmm2
  4084. pshufb %xmm5,%xmm0
  4085. aesenclast %xmm4,%xmm0
  4086. movdqa %xmm2,%xmm3
  4087. pslldq \$4,%xmm2
  4088. pxor %xmm2,%xmm3
  4089. pslldq \$4,%xmm2
  4090. pxor %xmm2,%xmm3
  4091. pslldq \$4,%xmm2
  4092. pxor %xmm3,%xmm2
  4093. pxor %xmm2,%xmm0
  4094. movdqu %xmm0,16(%rax)
  4095. mov $bits,96(%rax) # 240($key)
  4096. xor %eax,%eax
  4097. jmp .Lenc_key_ret
  4098. .align 16
  4099. .L12rounds:
  4100. movq 16($inp),%xmm2 # remaining 1/3 of *userKey
  4101. mov \$11,$bits # 12 rounds for 192
  4102. cmp \$`1<<28`,%r10d # AVX, but no XOP
  4103. je .L12rounds_alt
  4104. $movkey %xmm0,($key) # round 0
  4105. aeskeygenassist \$0x1,%xmm2,%xmm1 # round 1,2
  4106. call .Lkey_expansion_192a_cold
  4107. aeskeygenassist \$0x2,%xmm2,%xmm1 # round 2,3
  4108. call .Lkey_expansion_192b
  4109. aeskeygenassist \$0x4,%xmm2,%xmm1 # round 4,5
  4110. call .Lkey_expansion_192a
  4111. aeskeygenassist \$0x8,%xmm2,%xmm1 # round 5,6
  4112. call .Lkey_expansion_192b
  4113. aeskeygenassist \$0x10,%xmm2,%xmm1 # round 7,8
  4114. call .Lkey_expansion_192a
  4115. aeskeygenassist \$0x20,%xmm2,%xmm1 # round 8,9
  4116. call .Lkey_expansion_192b
  4117. aeskeygenassist \$0x40,%xmm2,%xmm1 # round 10,11
  4118. call .Lkey_expansion_192a
  4119. aeskeygenassist \$0x80,%xmm2,%xmm1 # round 11,12
  4120. call .Lkey_expansion_192b
  4121. $movkey %xmm0,(%rax)
  4122. mov $bits,48(%rax) # 240(%rdx)
  4123. xor %rax, %rax
  4124. jmp .Lenc_key_ret
  4125. .align 16
  4126. .L12rounds_alt:
  4127. movdqa .Lkey_rotate192(%rip),%xmm5
  4128. movdqa .Lkey_rcon1(%rip),%xmm4
  4129. mov \$8,%r10d
  4130. movdqu %xmm0,($key)
  4131. jmp .Loop_key192
  4132. .align 16
  4133. .Loop_key192:
  4134. movq %xmm2,0(%rax)
  4135. movdqa %xmm2,%xmm1
  4136. pshufb %xmm5,%xmm2
  4137. aesenclast %xmm4,%xmm2
  4138. pslld \$1, %xmm4
  4139. lea 24(%rax),%rax
  4140. movdqa %xmm0,%xmm3
  4141. pslldq \$4,%xmm0
  4142. pxor %xmm0,%xmm3
  4143. pslldq \$4,%xmm0
  4144. pxor %xmm0,%xmm3
  4145. pslldq \$4,%xmm0
  4146. pxor %xmm3,%xmm0
  4147. pshufd \$0xff,%xmm0,%xmm3
  4148. pxor %xmm1,%xmm3
  4149. pslldq \$4,%xmm1
  4150. pxor %xmm1,%xmm3
  4151. pxor %xmm2,%xmm0
  4152. pxor %xmm3,%xmm2
  4153. movdqu %xmm0,-16(%rax)
  4154. dec %r10d
  4155. jnz .Loop_key192
  4156. mov $bits,32(%rax) # 240($key)
  4157. xor %eax,%eax
  4158. jmp .Lenc_key_ret
  4159. .align 16
  4160. .L14rounds:
  4161. movups 16($inp),%xmm2 # remaining half of *userKey
  4162. mov \$13,$bits # 14 rounds for 256
  4163. lea 16(%rax),%rax
  4164. cmp \$`1<<28`,%r10d # AVX, but no XOP
  4165. je .L14rounds_alt
  4166. $movkey %xmm0,($key) # round 0
  4167. $movkey %xmm2,16($key) # round 1
  4168. aeskeygenassist \$0x1,%xmm2,%xmm1 # round 2
  4169. call .Lkey_expansion_256a_cold
  4170. aeskeygenassist \$0x1,%xmm0,%xmm1 # round 3
  4171. call .Lkey_expansion_256b
  4172. aeskeygenassist \$0x2,%xmm2,%xmm1 # round 4
  4173. call .Lkey_expansion_256a
  4174. aeskeygenassist \$0x2,%xmm0,%xmm1 # round 5
  4175. call .Lkey_expansion_256b
  4176. aeskeygenassist \$0x4,%xmm2,%xmm1 # round 6
  4177. call .Lkey_expansion_256a
  4178. aeskeygenassist \$0x4,%xmm0,%xmm1 # round 7
  4179. call .Lkey_expansion_256b
  4180. aeskeygenassist \$0x8,%xmm2,%xmm1 # round 8
  4181. call .Lkey_expansion_256a
  4182. aeskeygenassist \$0x8,%xmm0,%xmm1 # round 9
  4183. call .Lkey_expansion_256b
  4184. aeskeygenassist \$0x10,%xmm2,%xmm1 # round 10
  4185. call .Lkey_expansion_256a
  4186. aeskeygenassist \$0x10,%xmm0,%xmm1 # round 11
  4187. call .Lkey_expansion_256b
  4188. aeskeygenassist \$0x20,%xmm2,%xmm1 # round 12
  4189. call .Lkey_expansion_256a
  4190. aeskeygenassist \$0x20,%xmm0,%xmm1 # round 13
  4191. call .Lkey_expansion_256b
  4192. aeskeygenassist \$0x40,%xmm2,%xmm1 # round 14
  4193. call .Lkey_expansion_256a
  4194. $movkey %xmm0,(%rax)
  4195. mov $bits,16(%rax) # 240(%rdx)
  4196. xor %rax,%rax
  4197. jmp .Lenc_key_ret
  4198. .align 16
  4199. .L14rounds_alt:
  4200. movdqa .Lkey_rotate(%rip),%xmm5
  4201. movdqa .Lkey_rcon1(%rip),%xmm4
  4202. mov \$7,%r10d
  4203. movdqu %xmm0,0($key)
  4204. movdqa %xmm2,%xmm1
  4205. movdqu %xmm2,16($key)
  4206. jmp .Loop_key256
  4207. .align 16
  4208. .Loop_key256:
  4209. pshufb %xmm5,%xmm2
  4210. aesenclast %xmm4,%xmm2
  4211. movdqa %xmm0,%xmm3
  4212. pslldq \$4,%xmm0
  4213. pxor %xmm0,%xmm3
  4214. pslldq \$4,%xmm0
  4215. pxor %xmm0,%xmm3
  4216. pslldq \$4,%xmm0
  4217. pxor %xmm3,%xmm0
  4218. pslld \$1,%xmm4
  4219. pxor %xmm2,%xmm0
  4220. movdqu %xmm0,(%rax)
  4221. dec %r10d
  4222. jz .Ldone_key256
  4223. pshufd \$0xff,%xmm0,%xmm2
  4224. pxor %xmm3,%xmm3
  4225. aesenclast %xmm3,%xmm2
  4226. movdqa %xmm1,%xmm3
  4227. pslldq \$4,%xmm1
  4228. pxor %xmm1,%xmm3
  4229. pslldq \$4,%xmm1
  4230. pxor %xmm1,%xmm3
  4231. pslldq \$4,%xmm1
  4232. pxor %xmm3,%xmm1
  4233. pxor %xmm1,%xmm2
  4234. movdqu %xmm2,16(%rax)
  4235. lea 32(%rax),%rax
  4236. movdqa %xmm2,%xmm1
  4237. jmp .Loop_key256
  4238. .Ldone_key256:
  4239. mov $bits,16(%rax) # 240($key)
  4240. xor %eax,%eax
  4241. jmp .Lenc_key_ret
  4242. .align 16
  4243. .Lbad_keybits:
  4244. mov \$-2,%rax
  4245. .Lenc_key_ret:
  4246. pxor %xmm0,%xmm0
  4247. pxor %xmm1,%xmm1
  4248. pxor %xmm2,%xmm2
  4249. pxor %xmm3,%xmm3
  4250. pxor %xmm4,%xmm4
  4251. pxor %xmm5,%xmm5
  4252. add \$8,%rsp
  4253. .cfi_adjust_cfa_offset -8
  4254. ret
  4255. .cfi_endproc
  4256. .LSEH_end_set_encrypt_key:
  4257. .align 16
  4258. .Lkey_expansion_128:
  4259. $movkey %xmm0,(%rax)
  4260. lea 16(%rax),%rax
  4261. .Lkey_expansion_128_cold:
  4262. shufps \$0b00010000,%xmm0,%xmm4
  4263. xorps %xmm4, %xmm0
  4264. shufps \$0b10001100,%xmm0,%xmm4
  4265. xorps %xmm4, %xmm0
  4266. shufps \$0b11111111,%xmm1,%xmm1 # critical path
  4267. xorps %xmm1,%xmm0
  4268. ret
  4269. .align 16
  4270. .Lkey_expansion_192a:
  4271. $movkey %xmm0,(%rax)
  4272. lea 16(%rax),%rax
  4273. .Lkey_expansion_192a_cold:
  4274. movaps %xmm2, %xmm5
  4275. .Lkey_expansion_192b_warm:
  4276. shufps \$0b00010000,%xmm0,%xmm4
  4277. movdqa %xmm2,%xmm3
  4278. xorps %xmm4,%xmm0
  4279. shufps \$0b10001100,%xmm0,%xmm4
  4280. pslldq \$4,%xmm3
  4281. xorps %xmm4,%xmm0
  4282. pshufd \$0b01010101,%xmm1,%xmm1 # critical path
  4283. pxor %xmm3,%xmm2
  4284. pxor %xmm1,%xmm0
  4285. pshufd \$0b11111111,%xmm0,%xmm3
  4286. pxor %xmm3,%xmm2
  4287. ret
  4288. .align 16
  4289. .Lkey_expansion_192b:
  4290. movaps %xmm0,%xmm3
  4291. shufps \$0b01000100,%xmm0,%xmm5
  4292. $movkey %xmm5,(%rax)
  4293. shufps \$0b01001110,%xmm2,%xmm3
  4294. $movkey %xmm3,16(%rax)
  4295. lea 32(%rax),%rax
  4296. jmp .Lkey_expansion_192b_warm
  4297. .align 16
  4298. .Lkey_expansion_256a:
  4299. $movkey %xmm2,(%rax)
  4300. lea 16(%rax),%rax
  4301. .Lkey_expansion_256a_cold:
  4302. shufps \$0b00010000,%xmm0,%xmm4
  4303. xorps %xmm4,%xmm0
  4304. shufps \$0b10001100,%xmm0,%xmm4
  4305. xorps %xmm4,%xmm0
  4306. shufps \$0b11111111,%xmm1,%xmm1 # critical path
  4307. xorps %xmm1,%xmm0
  4308. ret
  4309. .align 16
  4310. .Lkey_expansion_256b:
  4311. $movkey %xmm0,(%rax)
  4312. lea 16(%rax),%rax
  4313. shufps \$0b00010000,%xmm2,%xmm4
  4314. xorps %xmm4,%xmm2
  4315. shufps \$0b10001100,%xmm2,%xmm4
  4316. xorps %xmm4,%xmm2
  4317. shufps \$0b10101010,%xmm1,%xmm1 # critical path
  4318. xorps %xmm1,%xmm2
  4319. ret
  4320. .size ${PREFIX}_set_encrypt_key,.-${PREFIX}_set_encrypt_key
  4321. .size __aesni_set_encrypt_key,.-__aesni_set_encrypt_key
  4322. ___
  4323. }
  4324. $code.=<<___;
  4325. .align 64
  4326. .Lbswap_mask:
  4327. .byte 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
  4328. .Lincrement32:
  4329. .long 6,6,6,0
  4330. .Lincrement64:
  4331. .long 1,0,0,0
  4332. .Lxts_magic:
  4333. .long 0x87,0,1,0
  4334. .Lincrement1:
  4335. .byte 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
  4336. .Lkey_rotate:
  4337. .long 0x0c0f0e0d,0x0c0f0e0d,0x0c0f0e0d,0x0c0f0e0d
  4338. .Lkey_rotate192:
  4339. .long 0x04070605,0x04070605,0x04070605,0x04070605
  4340. .Lkey_rcon1:
  4341. .long 1,1,1,1
  4342. .Lkey_rcon1b:
  4343. .long 0x1b,0x1b,0x1b,0x1b
  4344. .asciz "AES for Intel AES-NI, CRYPTOGAMS by <appro\@openssl.org>"
  4345. .align 64
  4346. ___
  4347. # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
  4348. # CONTEXT *context,DISPATCHER_CONTEXT *disp)
  4349. if ($win64) {
  4350. $rec="%rcx";
  4351. $frame="%rdx";
  4352. $context="%r8";
  4353. $disp="%r9";
  4354. $code.=<<___;
  4355. .extern __imp_RtlVirtualUnwind
  4356. ___
  4357. $code.=<<___ if ($PREFIX eq "aesni");
  4358. .type ecb_ccm64_se_handler,\@abi-omnipotent
  4359. .align 16
  4360. ecb_ccm64_se_handler:
  4361. push %rsi
  4362. push %rdi
  4363. push %rbx
  4364. push %rbp
  4365. push %r12
  4366. push %r13
  4367. push %r14
  4368. push %r15
  4369. pushfq
  4370. sub \$64,%rsp
  4371. mov 120($context),%rax # pull context->Rax
  4372. mov 248($context),%rbx # pull context->Rip
  4373. mov 8($disp),%rsi # disp->ImageBase
  4374. mov 56($disp),%r11 # disp->HandlerData
  4375. mov 0(%r11),%r10d # HandlerData[0]
  4376. lea (%rsi,%r10),%r10 # prologue label
  4377. cmp %r10,%rbx # context->Rip<prologue label
  4378. jb .Lcommon_seh_tail
  4379. mov 152($context),%rax # pull context->Rsp
  4380. mov 4(%r11),%r10d # HandlerData[1]
  4381. lea (%rsi,%r10),%r10 # epilogue label
  4382. cmp %r10,%rbx # context->Rip>=epilogue label
  4383. jae .Lcommon_seh_tail
  4384. lea 0(%rax),%rsi # %xmm save area
  4385. lea 512($context),%rdi # &context.Xmm6
  4386. mov \$8,%ecx # 4*sizeof(%xmm0)/sizeof(%rax)
  4387. .long 0xa548f3fc # cld; rep movsq
  4388. lea 0x58(%rax),%rax # adjust stack pointer
  4389. jmp .Lcommon_seh_tail
  4390. .size ecb_ccm64_se_handler,.-ecb_ccm64_se_handler
  4391. .type ctr_xts_se_handler,\@abi-omnipotent
  4392. .align 16
  4393. ctr_xts_se_handler:
  4394. push %rsi
  4395. push %rdi
  4396. push %rbx
  4397. push %rbp
  4398. push %r12
  4399. push %r13
  4400. push %r14
  4401. push %r15
  4402. pushfq
  4403. sub \$64,%rsp
  4404. mov 120($context),%rax # pull context->Rax
  4405. mov 248($context),%rbx # pull context->Rip
  4406. mov 8($disp),%rsi # disp->ImageBase
  4407. mov 56($disp),%r11 # disp->HandlerData
  4408. mov 0(%r11),%r10d # HandlerData[0]
  4409. lea (%rsi,%r10),%r10 # prologue lable
  4410. cmp %r10,%rbx # context->Rip<prologue label
  4411. jb .Lcommon_seh_tail
  4412. mov 152($context),%rax # pull context->Rsp
  4413. mov 4(%r11),%r10d # HandlerData[1]
  4414. lea (%rsi,%r10),%r10 # epilogue label
  4415. cmp %r10,%rbx # context->Rip>=epilogue label
  4416. jae .Lcommon_seh_tail
  4417. mov 208($context),%rax # pull context->R11
  4418. lea -0xa8(%rax),%rsi # %xmm save area
  4419. lea 512($context),%rdi # & context.Xmm6
  4420. mov \$20,%ecx # 10*sizeof(%xmm0)/sizeof(%rax)
  4421. .long 0xa548f3fc # cld; rep movsq
  4422. mov -8(%rax),%rbp # restore saved %rbp
  4423. mov %rbp,160($context) # restore context->Rbp
  4424. jmp .Lcommon_seh_tail
  4425. .size ctr_xts_se_handler,.-ctr_xts_se_handler
  4426. .type ocb_se_handler,\@abi-omnipotent
  4427. .align 16
  4428. ocb_se_handler:
  4429. push %rsi
  4430. push %rdi
  4431. push %rbx
  4432. push %rbp
  4433. push %r12
  4434. push %r13
  4435. push %r14
  4436. push %r15
  4437. pushfq
  4438. sub \$64,%rsp
  4439. mov 120($context),%rax # pull context->Rax
  4440. mov 248($context),%rbx # pull context->Rip
  4441. mov 8($disp),%rsi # disp->ImageBase
  4442. mov 56($disp),%r11 # disp->HandlerData
  4443. mov 0(%r11),%r10d # HandlerData[0]
  4444. lea (%rsi,%r10),%r10 # prologue lable
  4445. cmp %r10,%rbx # context->Rip<prologue label
  4446. jb .Lcommon_seh_tail
  4447. mov 4(%r11),%r10d # HandlerData[1]
  4448. lea (%rsi,%r10),%r10 # epilogue label
  4449. cmp %r10,%rbx # context->Rip>=epilogue label
  4450. jae .Lcommon_seh_tail
  4451. mov 8(%r11),%r10d # HandlerData[2]
  4452. lea (%rsi,%r10),%r10
  4453. cmp %r10,%rbx # context->Rip>=pop label
  4454. jae .Locb_no_xmm
  4455. mov 152($context),%rax # pull context->Rsp
  4456. lea (%rax),%rsi # %xmm save area
  4457. lea 512($context),%rdi # & context.Xmm6
  4458. mov \$20,%ecx # 10*sizeof(%xmm0)/sizeof(%rax)
  4459. .long 0xa548f3fc # cld; rep movsq
  4460. lea 0xa0+0x28(%rax),%rax
  4461. .Locb_no_xmm:
  4462. mov -8(%rax),%rbx
  4463. mov -16(%rax),%rbp
  4464. mov -24(%rax),%r12
  4465. mov -32(%rax),%r13
  4466. mov -40(%rax),%r14
  4467. mov %rbx,144($context) # restore context->Rbx
  4468. mov %rbp,160($context) # restore context->Rbp
  4469. mov %r12,216($context) # restore context->R12
  4470. mov %r13,224($context) # restore context->R13
  4471. mov %r14,232($context) # restore context->R14
  4472. jmp .Lcommon_seh_tail
  4473. .size ocb_se_handler,.-ocb_se_handler
  4474. ___
  4475. $code.=<<___;
  4476. .type cbc_se_handler,\@abi-omnipotent
  4477. .align 16
  4478. cbc_se_handler:
  4479. push %rsi
  4480. push %rdi
  4481. push %rbx
  4482. push %rbp
  4483. push %r12
  4484. push %r13
  4485. push %r14
  4486. push %r15
  4487. pushfq
  4488. sub \$64,%rsp
  4489. mov 152($context),%rax # pull context->Rsp
  4490. mov 248($context),%rbx # pull context->Rip
  4491. lea .Lcbc_decrypt_bulk(%rip),%r10
  4492. cmp %r10,%rbx # context->Rip<"prologue" label
  4493. jb .Lcommon_seh_tail
  4494. mov 120($context),%rax # pull context->Rax
  4495. lea .Lcbc_decrypt_body(%rip),%r10
  4496. cmp %r10,%rbx # context->Rip<cbc_decrypt_body
  4497. jb .Lcommon_seh_tail
  4498. mov 152($context),%rax # pull context->Rsp
  4499. lea .Lcbc_ret(%rip),%r10
  4500. cmp %r10,%rbx # context->Rip>="epilogue" label
  4501. jae .Lcommon_seh_tail
  4502. lea 16(%rax),%rsi # %xmm save area
  4503. lea 512($context),%rdi # &context.Xmm6
  4504. mov \$20,%ecx # 10*sizeof(%xmm0)/sizeof(%rax)
  4505. .long 0xa548f3fc # cld; rep movsq
  4506. mov 208($context),%rax # pull context->R11
  4507. mov -8(%rax),%rbp # restore saved %rbp
  4508. mov %rbp,160($context) # restore context->Rbp
  4509. .Lcommon_seh_tail:
  4510. mov 8(%rax),%rdi
  4511. mov 16(%rax),%rsi
  4512. mov %rax,152($context) # restore context->Rsp
  4513. mov %rsi,168($context) # restore context->Rsi
  4514. mov %rdi,176($context) # restore context->Rdi
  4515. mov 40($disp),%rdi # disp->ContextRecord
  4516. mov $context,%rsi # context
  4517. mov \$154,%ecx # sizeof(CONTEXT)
  4518. .long 0xa548f3fc # cld; rep movsq
  4519. mov $disp,%rsi
  4520. xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
  4521. mov 8(%rsi),%rdx # arg2, disp->ImageBase
  4522. mov 0(%rsi),%r8 # arg3, disp->ControlPc
  4523. mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
  4524. mov 40(%rsi),%r10 # disp->ContextRecord
  4525. lea 56(%rsi),%r11 # &disp->HandlerData
  4526. lea 24(%rsi),%r12 # &disp->EstablisherFrame
  4527. mov %r10,32(%rsp) # arg5
  4528. mov %r11,40(%rsp) # arg6
  4529. mov %r12,48(%rsp) # arg7
  4530. mov %rcx,56(%rsp) # arg8, (NULL)
  4531. call *__imp_RtlVirtualUnwind(%rip)
  4532. mov \$1,%eax # ExceptionContinueSearch
  4533. add \$64,%rsp
  4534. popfq
  4535. pop %r15
  4536. pop %r14
  4537. pop %r13
  4538. pop %r12
  4539. pop %rbp
  4540. pop %rbx
  4541. pop %rdi
  4542. pop %rsi
  4543. ret
  4544. .size cbc_se_handler,.-cbc_se_handler
  4545. .section .pdata
  4546. .align 4
  4547. ___
  4548. $code.=<<___ if ($PREFIX eq "aesni");
  4549. .rva .LSEH_begin_aesni_ecb_encrypt
  4550. .rva .LSEH_end_aesni_ecb_encrypt
  4551. .rva .LSEH_info_ecb
  4552. .rva .LSEH_begin_aesni_ccm64_encrypt_blocks
  4553. .rva .LSEH_end_aesni_ccm64_encrypt_blocks
  4554. .rva .LSEH_info_ccm64_enc
  4555. .rva .LSEH_begin_aesni_ccm64_decrypt_blocks
  4556. .rva .LSEH_end_aesni_ccm64_decrypt_blocks
  4557. .rva .LSEH_info_ccm64_dec
  4558. .rva .LSEH_begin_aesni_ctr32_encrypt_blocks
  4559. .rva .LSEH_end_aesni_ctr32_encrypt_blocks
  4560. .rva .LSEH_info_ctr32
  4561. .rva .LSEH_begin_aesni_xts_encrypt
  4562. .rva .LSEH_end_aesni_xts_encrypt
  4563. .rva .LSEH_info_xts_enc
  4564. .rva .LSEH_begin_aesni_xts_decrypt
  4565. .rva .LSEH_end_aesni_xts_decrypt
  4566. .rva .LSEH_info_xts_dec
  4567. .rva .LSEH_begin_aesni_ocb_encrypt
  4568. .rva .LSEH_end_aesni_ocb_encrypt
  4569. .rva .LSEH_info_ocb_enc
  4570. .rva .LSEH_begin_aesni_ocb_decrypt
  4571. .rva .LSEH_end_aesni_ocb_decrypt
  4572. .rva .LSEH_info_ocb_dec
  4573. ___
  4574. $code.=<<___;
  4575. .rva .LSEH_begin_${PREFIX}_cbc_encrypt
  4576. .rva .LSEH_end_${PREFIX}_cbc_encrypt
  4577. .rva .LSEH_info_cbc
  4578. .rva ${PREFIX}_set_decrypt_key
  4579. .rva .LSEH_end_set_decrypt_key
  4580. .rva .LSEH_info_key
  4581. .rva ${PREFIX}_set_encrypt_key
  4582. .rva .LSEH_end_set_encrypt_key
  4583. .rva .LSEH_info_key
  4584. .section .xdata
  4585. .align 8
  4586. ___
  4587. $code.=<<___ if ($PREFIX eq "aesni");
  4588. .LSEH_info_ecb:
  4589. .byte 9,0,0,0
  4590. .rva ecb_ccm64_se_handler
  4591. .rva .Lecb_enc_body,.Lecb_enc_ret # HandlerData[]
  4592. .LSEH_info_ccm64_enc:
  4593. .byte 9,0,0,0
  4594. .rva ecb_ccm64_se_handler
  4595. .rva .Lccm64_enc_body,.Lccm64_enc_ret # HandlerData[]
  4596. .LSEH_info_ccm64_dec:
  4597. .byte 9,0,0,0
  4598. .rva ecb_ccm64_se_handler
  4599. .rva .Lccm64_dec_body,.Lccm64_dec_ret # HandlerData[]
  4600. .LSEH_info_ctr32:
  4601. .byte 9,0,0,0
  4602. .rva ctr_xts_se_handler
  4603. .rva .Lctr32_body,.Lctr32_epilogue # HandlerData[]
  4604. .LSEH_info_xts_enc:
  4605. .byte 9,0,0,0
  4606. .rva ctr_xts_se_handler
  4607. .rva .Lxts_enc_body,.Lxts_enc_epilogue # HandlerData[]
  4608. .LSEH_info_xts_dec:
  4609. .byte 9,0,0,0
  4610. .rva ctr_xts_se_handler
  4611. .rva .Lxts_dec_body,.Lxts_dec_epilogue # HandlerData[]
  4612. .LSEH_info_ocb_enc:
  4613. .byte 9,0,0,0
  4614. .rva ocb_se_handler
  4615. .rva .Locb_enc_body,.Locb_enc_epilogue # HandlerData[]
  4616. .rva .Locb_enc_pop
  4617. .long 0
  4618. .LSEH_info_ocb_dec:
  4619. .byte 9,0,0,0
  4620. .rva ocb_se_handler
  4621. .rva .Locb_dec_body,.Locb_dec_epilogue # HandlerData[]
  4622. .rva .Locb_dec_pop
  4623. .long 0
  4624. ___
  4625. $code.=<<___;
  4626. .LSEH_info_cbc:
  4627. .byte 9,0,0,0
  4628. .rva cbc_se_handler
  4629. .LSEH_info_key:
  4630. .byte 0x01,0x04,0x01,0x00
  4631. .byte 0x04,0x02,0x00,0x00 # sub rsp,8
  4632. ___
  4633. }
  4634. sub rex {
  4635. local *opcode=shift;
  4636. my ($dst,$src)=@_;
  4637. my $rex=0;
  4638. $rex|=0x04 if($dst>=8);
  4639. $rex|=0x01 if($src>=8);
  4640. push @opcode,$rex|0x40 if($rex);
  4641. }
  4642. sub aesni {
  4643. my $line=shift;
  4644. my @opcode=(0x66);
  4645. if ($line=~/(aeskeygenassist)\s+\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
  4646. rex(\@opcode,$4,$3);
  4647. push @opcode,0x0f,0x3a,0xdf;
  4648. push @opcode,0xc0|($3&7)|(($4&7)<<3); # ModR/M
  4649. my $c=$2;
  4650. push @opcode,$c=~/^0/?oct($c):$c;
  4651. return ".byte\t".join(',',@opcode);
  4652. }
  4653. elsif ($line=~/(aes[a-z]+)\s+%xmm([0-9]+),\s*%xmm([0-9]+)/) {
  4654. my %opcodelet = (
  4655. "aesimc" => 0xdb,
  4656. "aesenc" => 0xdc, "aesenclast" => 0xdd,
  4657. "aesdec" => 0xde, "aesdeclast" => 0xdf
  4658. );
  4659. return undef if (!defined($opcodelet{$1}));
  4660. rex(\@opcode,$3,$2);
  4661. push @opcode,0x0f,0x38,$opcodelet{$1};
  4662. push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
  4663. return ".byte\t".join(',',@opcode);
  4664. }
  4665. elsif ($line=~/(aes[a-z]+)\s+([0x1-9a-fA-F]*)\(%rsp\),\s*%xmm([0-9]+)/) {
  4666. my %opcodelet = (
  4667. "aesenc" => 0xdc, "aesenclast" => 0xdd,
  4668. "aesdec" => 0xde, "aesdeclast" => 0xdf
  4669. );
  4670. return undef if (!defined($opcodelet{$1}));
  4671. my $off = $2;
  4672. push @opcode,0x44 if ($3>=8);
  4673. push @opcode,0x0f,0x38,$opcodelet{$1};
  4674. push @opcode,0x44|(($3&7)<<3),0x24; # ModR/M
  4675. push @opcode,($off=~/^0/?oct($off):$off)&0xff;
  4676. return ".byte\t".join(',',@opcode);
  4677. }
  4678. return $line;
  4679. }
  4680. sub movbe {
  4681. ".byte 0x0f,0x38,0xf1,0x44,0x24,".shift;
  4682. }
  4683. $code =~ s/\`([^\`]*)\`/eval($1)/gem;
  4684. $code =~ s/\b(aes.*%xmm[0-9]+).*$/aesni($1)/gem;
  4685. #$code =~ s/\bmovbe\s+%eax/bswap %eax; mov %eax/gm; # debugging artefact
  4686. $code =~ s/\bmovbe\s+%eax,\s*([0-9]+)\(%rsp\)/movbe($1)/gem;
  4687. print $code;
  4688. close STDOUT;