aesni-x86_64.pl 127 KB

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