aesp8-ppc.pl 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811
  1. #! /usr/bin/env perl
  2. # Copyright 2014-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 AES instructions as per PowerISA
  17. # specification version 2.07, first implemented by POWER8 processor.
  18. # The module is endian-agnostic in sense that it supports both big-
  19. # and little-endian cases. Data alignment in parallelizable modes is
  20. # handled with VSX loads and stores, which implies MSR.VSX flag being
  21. # set. It should also be noted that ISA specification doesn't prohibit
  22. # alignment exceptions for these instructions on page boundaries.
  23. # Initially alignment was handled in pure AltiVec/VMX way [when data
  24. # is aligned programmatically, which in turn guarantees exception-
  25. # free execution], but it turned to hamper performance when vcipher
  26. # instructions are interleaved. It's reckoned that eventual
  27. # misalignment penalties at page boundaries are in average lower
  28. # than additional overhead in pure AltiVec approach.
  29. #
  30. # May 2016
  31. #
  32. # Add XTS subroutine, 9x on little- and 12x improvement on big-endian
  33. # systems were measured.
  34. #
  35. ######################################################################
  36. # Current large-block performance in cycles per byte processed with
  37. # 128-bit key (less is better).
  38. #
  39. # CBC en-/decrypt CTR XTS
  40. # POWER8[le] 3.96/0.72 0.74 1.1
  41. # POWER8[be] 3.75/0.65 0.66 1.0
  42. # POWER9[le] 4.02/0.86 0.84 1.05
  43. # POWER9[be] 3.99/0.78 0.79 0.97
  44. # $output is the last argument if it looks like a file (it has an extension)
  45. # $flavour is the first argument if it doesn't look like a file
  46. $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
  47. $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
  48. if ($flavour =~ /64/) {
  49. $SIZE_T =8;
  50. $LRSAVE =2*$SIZE_T;
  51. $STU ="stdu";
  52. $POP ="ld";
  53. $PUSH ="std";
  54. $UCMP ="cmpld";
  55. $SHL ="sldi";
  56. } elsif ($flavour =~ /32/) {
  57. $SIZE_T =4;
  58. $LRSAVE =$SIZE_T;
  59. $STU ="stwu";
  60. $POP ="lwz";
  61. $PUSH ="stw";
  62. $UCMP ="cmplw";
  63. $SHL ="slwi";
  64. } else { die "nonsense $flavour"; }
  65. $LITTLE_ENDIAN = ($flavour=~/le$/) ? $SIZE_T : 0;
  66. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  67. ( $xlate="${dir}ppc-xlate.pl" and -f $xlate ) or
  68. ( $xlate="${dir}../../perlasm/ppc-xlate.pl" and -f $xlate) or
  69. die "can't locate ppc-xlate.pl";
  70. open STDOUT,"| $^X $xlate $flavour \"$output\""
  71. or die "can't call $xlate: $!";
  72. $FRAME=8*$SIZE_T;
  73. $prefix="aes_p8";
  74. $sp="r1";
  75. $vrsave="r12";
  76. #########################################################################
  77. {{{ # Key setup procedures #
  78. my ($inp,$bits,$out,$ptr,$cnt,$rounds)=map("r$_",(3..8));
  79. my ($zero,$in0,$in1,$key,$rcon,$mask,$tmp)=map("v$_",(0..6));
  80. my ($stage,$outperm,$outmask,$outhead,$outtail)=map("v$_",(7..11));
  81. $code.=<<___;
  82. .machine "any"
  83. .text
  84. .align 7
  85. rcon:
  86. .long 0x01000000, 0x01000000, 0x01000000, 0x01000000 ?rev
  87. .long 0x1b000000, 0x1b000000, 0x1b000000, 0x1b000000 ?rev
  88. .long 0x0d0e0f0c, 0x0d0e0f0c, 0x0d0e0f0c, 0x0d0e0f0c ?rev
  89. .long 0,0,0,0 ?asis
  90. Lconsts:
  91. mflr r0
  92. bcl 20,31,\$+4
  93. mflr $ptr #vvvvv "distance between . and rcon
  94. addi $ptr,$ptr,-0x48
  95. mtlr r0
  96. blr
  97. .long 0
  98. .byte 0,12,0x14,0,0,0,0,0
  99. .asciz "AES for PowerISA 2.07, CRYPTOGAMS by <appro\@openssl.org>"
  100. .globl .${prefix}_set_encrypt_key
  101. .align 5
  102. .${prefix}_set_encrypt_key:
  103. Lset_encrypt_key:
  104. mflr r11
  105. $PUSH r11,$LRSAVE($sp)
  106. li $ptr,-1
  107. ${UCMP}i $inp,0
  108. beq- Lenc_key_abort # if ($inp==0) return -1;
  109. ${UCMP}i $out,0
  110. beq- Lenc_key_abort # if ($out==0) return -1;
  111. li $ptr,-2
  112. cmpwi $bits,128
  113. blt- Lenc_key_abort
  114. cmpwi $bits,256
  115. bgt- Lenc_key_abort
  116. andi. r0,$bits,0x3f
  117. bne- Lenc_key_abort
  118. lis r0,0xfff0
  119. mfspr $vrsave,256
  120. mtspr 256,r0
  121. bl Lconsts
  122. mtlr r11
  123. neg r9,$inp
  124. lvx $in0,0,$inp
  125. addi $inp,$inp,15 # 15 is not typo
  126. lvsr $key,0,r9 # borrow $key
  127. li r8,0x20
  128. cmpwi $bits,192
  129. lvx $in1,0,$inp
  130. le?vspltisb $mask,0x0f # borrow $mask
  131. lvx $rcon,0,$ptr
  132. le?vxor $key,$key,$mask # adjust for byte swap
  133. lvx $mask,r8,$ptr
  134. addi $ptr,$ptr,0x10
  135. vperm $in0,$in0,$in1,$key # align [and byte swap in LE]
  136. li $cnt,8
  137. vxor $zero,$zero,$zero
  138. mtctr $cnt
  139. ?lvsr $outperm,0,$out
  140. vspltisb $outmask,-1
  141. lvx $outhead,0,$out
  142. ?vperm $outmask,$zero,$outmask,$outperm
  143. blt Loop128
  144. addi $inp,$inp,8
  145. beq L192
  146. addi $inp,$inp,8
  147. b L256
  148. .align 4
  149. Loop128:
  150. vperm $key,$in0,$in0,$mask # rotate-n-splat
  151. vsldoi $tmp,$zero,$in0,12 # >>32
  152. vperm $outtail,$in0,$in0,$outperm # rotate
  153. vsel $stage,$outhead,$outtail,$outmask
  154. vmr $outhead,$outtail
  155. vcipherlast $key,$key,$rcon
  156. stvx $stage,0,$out
  157. addi $out,$out,16
  158. vxor $in0,$in0,$tmp
  159. vsldoi $tmp,$zero,$tmp,12 # >>32
  160. vxor $in0,$in0,$tmp
  161. vsldoi $tmp,$zero,$tmp,12 # >>32
  162. vxor $in0,$in0,$tmp
  163. vadduwm $rcon,$rcon,$rcon
  164. vxor $in0,$in0,$key
  165. bdnz Loop128
  166. lvx $rcon,0,$ptr # last two round keys
  167. vperm $key,$in0,$in0,$mask # rotate-n-splat
  168. vsldoi $tmp,$zero,$in0,12 # >>32
  169. vperm $outtail,$in0,$in0,$outperm # rotate
  170. vsel $stage,$outhead,$outtail,$outmask
  171. vmr $outhead,$outtail
  172. vcipherlast $key,$key,$rcon
  173. stvx $stage,0,$out
  174. addi $out,$out,16
  175. vxor $in0,$in0,$tmp
  176. vsldoi $tmp,$zero,$tmp,12 # >>32
  177. vxor $in0,$in0,$tmp
  178. vsldoi $tmp,$zero,$tmp,12 # >>32
  179. vxor $in0,$in0,$tmp
  180. vadduwm $rcon,$rcon,$rcon
  181. vxor $in0,$in0,$key
  182. vperm $key,$in0,$in0,$mask # rotate-n-splat
  183. vsldoi $tmp,$zero,$in0,12 # >>32
  184. vperm $outtail,$in0,$in0,$outperm # rotate
  185. vsel $stage,$outhead,$outtail,$outmask
  186. vmr $outhead,$outtail
  187. vcipherlast $key,$key,$rcon
  188. stvx $stage,0,$out
  189. addi $out,$out,16
  190. vxor $in0,$in0,$tmp
  191. vsldoi $tmp,$zero,$tmp,12 # >>32
  192. vxor $in0,$in0,$tmp
  193. vsldoi $tmp,$zero,$tmp,12 # >>32
  194. vxor $in0,$in0,$tmp
  195. vxor $in0,$in0,$key
  196. vperm $outtail,$in0,$in0,$outperm # rotate
  197. vsel $stage,$outhead,$outtail,$outmask
  198. vmr $outhead,$outtail
  199. stvx $stage,0,$out
  200. addi $inp,$out,15 # 15 is not typo
  201. addi $out,$out,0x50
  202. li $rounds,10
  203. b Ldone
  204. .align 4
  205. L192:
  206. lvx $tmp,0,$inp
  207. li $cnt,4
  208. vperm $outtail,$in0,$in0,$outperm # rotate
  209. vsel $stage,$outhead,$outtail,$outmask
  210. vmr $outhead,$outtail
  211. stvx $stage,0,$out
  212. addi $out,$out,16
  213. vperm $in1,$in1,$tmp,$key # align [and byte swap in LE]
  214. vspltisb $key,8 # borrow $key
  215. mtctr $cnt
  216. vsububm $mask,$mask,$key # adjust the mask
  217. Loop192:
  218. vperm $key,$in1,$in1,$mask # roate-n-splat
  219. vsldoi $tmp,$zero,$in0,12 # >>32
  220. vcipherlast $key,$key,$rcon
  221. vxor $in0,$in0,$tmp
  222. vsldoi $tmp,$zero,$tmp,12 # >>32
  223. vxor $in0,$in0,$tmp
  224. vsldoi $tmp,$zero,$tmp,12 # >>32
  225. vxor $in0,$in0,$tmp
  226. vsldoi $stage,$zero,$in1,8
  227. vspltw $tmp,$in0,3
  228. vxor $tmp,$tmp,$in1
  229. vsldoi $in1,$zero,$in1,12 # >>32
  230. vadduwm $rcon,$rcon,$rcon
  231. vxor $in1,$in1,$tmp
  232. vxor $in0,$in0,$key
  233. vxor $in1,$in1,$key
  234. vsldoi $stage,$stage,$in0,8
  235. vperm $key,$in1,$in1,$mask # rotate-n-splat
  236. vsldoi $tmp,$zero,$in0,12 # >>32
  237. vperm $outtail,$stage,$stage,$outperm # rotate
  238. vsel $stage,$outhead,$outtail,$outmask
  239. vmr $outhead,$outtail
  240. vcipherlast $key,$key,$rcon
  241. stvx $stage,0,$out
  242. addi $out,$out,16
  243. vsldoi $stage,$in0,$in1,8
  244. vxor $in0,$in0,$tmp
  245. vsldoi $tmp,$zero,$tmp,12 # >>32
  246. vperm $outtail,$stage,$stage,$outperm # rotate
  247. vsel $stage,$outhead,$outtail,$outmask
  248. vmr $outhead,$outtail
  249. vxor $in0,$in0,$tmp
  250. vsldoi $tmp,$zero,$tmp,12 # >>32
  251. vxor $in0,$in0,$tmp
  252. stvx $stage,0,$out
  253. addi $out,$out,16
  254. vspltw $tmp,$in0,3
  255. vxor $tmp,$tmp,$in1
  256. vsldoi $in1,$zero,$in1,12 # >>32
  257. vadduwm $rcon,$rcon,$rcon
  258. vxor $in1,$in1,$tmp
  259. vxor $in0,$in0,$key
  260. vxor $in1,$in1,$key
  261. vperm $outtail,$in0,$in0,$outperm # rotate
  262. vsel $stage,$outhead,$outtail,$outmask
  263. vmr $outhead,$outtail
  264. stvx $stage,0,$out
  265. addi $inp,$out,15 # 15 is not typo
  266. addi $out,$out,16
  267. bdnz Loop192
  268. li $rounds,12
  269. addi $out,$out,0x20
  270. b Ldone
  271. .align 4
  272. L256:
  273. lvx $tmp,0,$inp
  274. li $cnt,7
  275. li $rounds,14
  276. vperm $outtail,$in0,$in0,$outperm # rotate
  277. vsel $stage,$outhead,$outtail,$outmask
  278. vmr $outhead,$outtail
  279. stvx $stage,0,$out
  280. addi $out,$out,16
  281. vperm $in1,$in1,$tmp,$key # align [and byte swap in LE]
  282. mtctr $cnt
  283. Loop256:
  284. vperm $key,$in1,$in1,$mask # rotate-n-splat
  285. vsldoi $tmp,$zero,$in0,12 # >>32
  286. vperm $outtail,$in1,$in1,$outperm # rotate
  287. vsel $stage,$outhead,$outtail,$outmask
  288. vmr $outhead,$outtail
  289. vcipherlast $key,$key,$rcon
  290. stvx $stage,0,$out
  291. addi $out,$out,16
  292. vxor $in0,$in0,$tmp
  293. vsldoi $tmp,$zero,$tmp,12 # >>32
  294. vxor $in0,$in0,$tmp
  295. vsldoi $tmp,$zero,$tmp,12 # >>32
  296. vxor $in0,$in0,$tmp
  297. vadduwm $rcon,$rcon,$rcon
  298. vxor $in0,$in0,$key
  299. vperm $outtail,$in0,$in0,$outperm # rotate
  300. vsel $stage,$outhead,$outtail,$outmask
  301. vmr $outhead,$outtail
  302. stvx $stage,0,$out
  303. addi $inp,$out,15 # 15 is not typo
  304. addi $out,$out,16
  305. bdz Ldone
  306. vspltw $key,$in0,3 # just splat
  307. vsldoi $tmp,$zero,$in1,12 # >>32
  308. vsbox $key,$key
  309. vxor $in1,$in1,$tmp
  310. vsldoi $tmp,$zero,$tmp,12 # >>32
  311. vxor $in1,$in1,$tmp
  312. vsldoi $tmp,$zero,$tmp,12 # >>32
  313. vxor $in1,$in1,$tmp
  314. vxor $in1,$in1,$key
  315. b Loop256
  316. .align 4
  317. Ldone:
  318. lvx $in1,0,$inp # redundant in aligned case
  319. vsel $in1,$outhead,$in1,$outmask
  320. stvx $in1,0,$inp
  321. li $ptr,0
  322. mtspr 256,$vrsave
  323. stw $rounds,0($out)
  324. Lenc_key_abort:
  325. mr r3,$ptr
  326. blr
  327. .long 0
  328. .byte 0,12,0x14,1,0,0,3,0
  329. .long 0
  330. .size .${prefix}_set_encrypt_key,.-.${prefix}_set_encrypt_key
  331. .globl .${prefix}_set_decrypt_key
  332. .align 5
  333. .${prefix}_set_decrypt_key:
  334. $STU $sp,-$FRAME($sp)
  335. mflr r10
  336. $PUSH r10,$FRAME+$LRSAVE($sp)
  337. bl Lset_encrypt_key
  338. mtlr r10
  339. cmpwi r3,0
  340. bne- Ldec_key_abort
  341. slwi $cnt,$rounds,4
  342. subi $inp,$out,240 # first round key
  343. srwi $rounds,$rounds,1
  344. add $out,$inp,$cnt # last round key
  345. mtctr $rounds
  346. Ldeckey:
  347. lwz r0, 0($inp)
  348. lwz r6, 4($inp)
  349. lwz r7, 8($inp)
  350. lwz r8, 12($inp)
  351. addi $inp,$inp,16
  352. lwz r9, 0($out)
  353. lwz r10,4($out)
  354. lwz r11,8($out)
  355. lwz r12,12($out)
  356. stw r0, 0($out)
  357. stw r6, 4($out)
  358. stw r7, 8($out)
  359. stw r8, 12($out)
  360. subi $out,$out,16
  361. stw r9, -16($inp)
  362. stw r10,-12($inp)
  363. stw r11,-8($inp)
  364. stw r12,-4($inp)
  365. bdnz Ldeckey
  366. xor r3,r3,r3 # return value
  367. Ldec_key_abort:
  368. addi $sp,$sp,$FRAME
  369. blr
  370. .long 0
  371. .byte 0,12,4,1,0x80,0,3,0
  372. .long 0
  373. .size .${prefix}_set_decrypt_key,.-.${prefix}_set_decrypt_key
  374. ___
  375. }}}
  376. #########################################################################
  377. {{{ # Single block en- and decrypt procedures #
  378. sub gen_block () {
  379. my $dir = shift;
  380. my $n = $dir eq "de" ? "n" : "";
  381. my ($inp,$out,$key,$rounds,$idx)=map("r$_",(3..7));
  382. $code.=<<___;
  383. .globl .${prefix}_${dir}crypt
  384. .align 5
  385. .${prefix}_${dir}crypt:
  386. lwz $rounds,240($key)
  387. lis r0,0xfc00
  388. mfspr $vrsave,256
  389. li $idx,15 # 15 is not typo
  390. mtspr 256,r0
  391. lvx v0,0,$inp
  392. neg r11,$out
  393. lvx v1,$idx,$inp
  394. lvsl v2,0,$inp # inpperm
  395. le?vspltisb v4,0x0f
  396. ?lvsl v3,0,r11 # outperm
  397. le?vxor v2,v2,v4
  398. li $idx,16
  399. vperm v0,v0,v1,v2 # align [and byte swap in LE]
  400. lvx v1,0,$key
  401. ?lvsl v5,0,$key # keyperm
  402. srwi $rounds,$rounds,1
  403. lvx v2,$idx,$key
  404. addi $idx,$idx,16
  405. subi $rounds,$rounds,1
  406. ?vperm v1,v1,v2,v5 # align round key
  407. vxor v0,v0,v1
  408. lvx v1,$idx,$key
  409. addi $idx,$idx,16
  410. mtctr $rounds
  411. Loop_${dir}c:
  412. ?vperm v2,v2,v1,v5
  413. v${n}cipher v0,v0,v2
  414. lvx v2,$idx,$key
  415. addi $idx,$idx,16
  416. ?vperm v1,v1,v2,v5
  417. v${n}cipher v0,v0,v1
  418. lvx v1,$idx,$key
  419. addi $idx,$idx,16
  420. bdnz Loop_${dir}c
  421. ?vperm v2,v2,v1,v5
  422. v${n}cipher v0,v0,v2
  423. lvx v2,$idx,$key
  424. ?vperm v1,v1,v2,v5
  425. v${n}cipherlast v0,v0,v1
  426. vspltisb v2,-1
  427. vxor v1,v1,v1
  428. li $idx,15 # 15 is not typo
  429. ?vperm v2,v1,v2,v3 # outmask
  430. le?vxor v3,v3,v4
  431. lvx v1,0,$out # outhead
  432. vperm v0,v0,v0,v3 # rotate [and byte swap in LE]
  433. vsel v1,v1,v0,v2
  434. lvx v4,$idx,$out
  435. stvx v1,0,$out
  436. vsel v0,v0,v4,v2
  437. stvx v0,$idx,$out
  438. mtspr 256,$vrsave
  439. blr
  440. .long 0
  441. .byte 0,12,0x14,0,0,0,3,0
  442. .long 0
  443. .size .${prefix}_${dir}crypt,.-.${prefix}_${dir}crypt
  444. ___
  445. }
  446. &gen_block("en");
  447. &gen_block("de");
  448. }}}
  449. #########################################################################
  450. {{{ # CBC en- and decrypt procedures #
  451. my ($inp,$out,$len,$key,$ivp,$enc,$rounds,$idx)=map("r$_",(3..10));
  452. my ($rndkey0,$rndkey1,$inout,$tmp)= map("v$_",(0..3));
  453. my ($ivec,$inptail,$inpperm,$outhead,$outperm,$outmask,$keyperm)=
  454. map("v$_",(4..10));
  455. $code.=<<___;
  456. .globl .${prefix}_cbc_encrypt
  457. .align 5
  458. .${prefix}_cbc_encrypt:
  459. ${UCMP}i $len,16
  460. bltlr-
  461. cmpwi $enc,0 # test direction
  462. lis r0,0xffe0
  463. mfspr $vrsave,256
  464. mtspr 256,r0
  465. li $idx,15
  466. vxor $rndkey0,$rndkey0,$rndkey0
  467. le?vspltisb $tmp,0x0f
  468. lvx $ivec,0,$ivp # load [unaligned] iv
  469. lvsl $inpperm,0,$ivp
  470. lvx $inptail,$idx,$ivp
  471. le?vxor $inpperm,$inpperm,$tmp
  472. vperm $ivec,$ivec,$inptail,$inpperm
  473. neg r11,$inp
  474. ?lvsl $keyperm,0,$key # prepare for unaligned key
  475. lwz $rounds,240($key)
  476. lvsr $inpperm,0,r11 # prepare for unaligned load
  477. lvx $inptail,0,$inp
  478. addi $inp,$inp,15 # 15 is not typo
  479. le?vxor $inpperm,$inpperm,$tmp
  480. ?lvsr $outperm,0,$out # prepare for unaligned store
  481. vspltisb $outmask,-1
  482. lvx $outhead,0,$out
  483. ?vperm $outmask,$rndkey0,$outmask,$outperm
  484. le?vxor $outperm,$outperm,$tmp
  485. srwi $rounds,$rounds,1
  486. li $idx,16
  487. subi $rounds,$rounds,1
  488. beq Lcbc_dec
  489. Lcbc_enc:
  490. vmr $inout,$inptail
  491. lvx $inptail,0,$inp
  492. addi $inp,$inp,16
  493. mtctr $rounds
  494. subi $len,$len,16 # len-=16
  495. lvx $rndkey0,0,$key
  496. vperm $inout,$inout,$inptail,$inpperm
  497. lvx $rndkey1,$idx,$key
  498. addi $idx,$idx,16
  499. ?vperm $rndkey0,$rndkey0,$rndkey1,$keyperm
  500. vxor $inout,$inout,$rndkey0
  501. lvx $rndkey0,$idx,$key
  502. addi $idx,$idx,16
  503. vxor $inout,$inout,$ivec
  504. Loop_cbc_enc:
  505. ?vperm $rndkey1,$rndkey1,$rndkey0,$keyperm
  506. vcipher $inout,$inout,$rndkey1
  507. lvx $rndkey1,$idx,$key
  508. addi $idx,$idx,16
  509. ?vperm $rndkey0,$rndkey0,$rndkey1,$keyperm
  510. vcipher $inout,$inout,$rndkey0
  511. lvx $rndkey0,$idx,$key
  512. addi $idx,$idx,16
  513. bdnz Loop_cbc_enc
  514. ?vperm $rndkey1,$rndkey1,$rndkey0,$keyperm
  515. vcipher $inout,$inout,$rndkey1
  516. lvx $rndkey1,$idx,$key
  517. li $idx,16
  518. ?vperm $rndkey0,$rndkey0,$rndkey1,$keyperm
  519. vcipherlast $ivec,$inout,$rndkey0
  520. ${UCMP}i $len,16
  521. vperm $tmp,$ivec,$ivec,$outperm
  522. vsel $inout,$outhead,$tmp,$outmask
  523. vmr $outhead,$tmp
  524. stvx $inout,0,$out
  525. addi $out,$out,16
  526. bge Lcbc_enc
  527. b Lcbc_done
  528. .align 4
  529. Lcbc_dec:
  530. ${UCMP}i $len,128
  531. bge _aesp8_cbc_decrypt8x
  532. vmr $tmp,$inptail
  533. lvx $inptail,0,$inp
  534. addi $inp,$inp,16
  535. mtctr $rounds
  536. subi $len,$len,16 # len-=16
  537. lvx $rndkey0,0,$key
  538. vperm $tmp,$tmp,$inptail,$inpperm
  539. lvx $rndkey1,$idx,$key
  540. addi $idx,$idx,16
  541. ?vperm $rndkey0,$rndkey0,$rndkey1,$keyperm
  542. vxor $inout,$tmp,$rndkey0
  543. lvx $rndkey0,$idx,$key
  544. addi $idx,$idx,16
  545. Loop_cbc_dec:
  546. ?vperm $rndkey1,$rndkey1,$rndkey0,$keyperm
  547. vncipher $inout,$inout,$rndkey1
  548. lvx $rndkey1,$idx,$key
  549. addi $idx,$idx,16
  550. ?vperm $rndkey0,$rndkey0,$rndkey1,$keyperm
  551. vncipher $inout,$inout,$rndkey0
  552. lvx $rndkey0,$idx,$key
  553. addi $idx,$idx,16
  554. bdnz Loop_cbc_dec
  555. ?vperm $rndkey1,$rndkey1,$rndkey0,$keyperm
  556. vncipher $inout,$inout,$rndkey1
  557. lvx $rndkey1,$idx,$key
  558. li $idx,16
  559. ?vperm $rndkey0,$rndkey0,$rndkey1,$keyperm
  560. vncipherlast $inout,$inout,$rndkey0
  561. ${UCMP}i $len,16
  562. vxor $inout,$inout,$ivec
  563. vmr $ivec,$tmp
  564. vperm $tmp,$inout,$inout,$outperm
  565. vsel $inout,$outhead,$tmp,$outmask
  566. vmr $outhead,$tmp
  567. stvx $inout,0,$out
  568. addi $out,$out,16
  569. bge Lcbc_dec
  570. Lcbc_done:
  571. addi $out,$out,-1
  572. lvx $inout,0,$out # redundant in aligned case
  573. vsel $inout,$outhead,$inout,$outmask
  574. stvx $inout,0,$out
  575. neg $enc,$ivp # write [unaligned] iv
  576. li $idx,15 # 15 is not typo
  577. vxor $rndkey0,$rndkey0,$rndkey0
  578. vspltisb $outmask,-1
  579. le?vspltisb $tmp,0x0f
  580. ?lvsl $outperm,0,$enc
  581. ?vperm $outmask,$rndkey0,$outmask,$outperm
  582. le?vxor $outperm,$outperm,$tmp
  583. lvx $outhead,0,$ivp
  584. vperm $ivec,$ivec,$ivec,$outperm
  585. vsel $inout,$outhead,$ivec,$outmask
  586. lvx $inptail,$idx,$ivp
  587. stvx $inout,0,$ivp
  588. vsel $inout,$ivec,$inptail,$outmask
  589. stvx $inout,$idx,$ivp
  590. mtspr 256,$vrsave
  591. blr
  592. .long 0
  593. .byte 0,12,0x14,0,0,0,6,0
  594. .long 0
  595. ___
  596. #########################################################################
  597. {{ # Optimized CBC decrypt procedure #
  598. my $key_="r11";
  599. my ($x00,$x10,$x20,$x30,$x40,$x50,$x60,$x70)=map("r$_",(0,8,26..31));
  600. $x00=0 if ($flavour =~ /osx/);
  601. my ($in0, $in1, $in2, $in3, $in4, $in5, $in6, $in7 )=map("v$_",(0..3,10..13));
  602. my ($out0,$out1,$out2,$out3,$out4,$out5,$out6,$out7)=map("v$_",(14..21));
  603. my $rndkey0="v23"; # v24-v25 rotating buffer for first found keys
  604. # v26-v31 last 6 round keys
  605. my ($tmp,$keyperm)=($in3,$in4); # aliases with "caller", redundant assignment
  606. $code.=<<___;
  607. .align 5
  608. _aesp8_cbc_decrypt8x:
  609. $STU $sp,-`($FRAME+21*16+6*$SIZE_T)`($sp)
  610. li r10,`$FRAME+8*16+15`
  611. li r11,`$FRAME+8*16+31`
  612. stvx v20,r10,$sp # ABI says so
  613. addi r10,r10,32
  614. stvx v21,r11,$sp
  615. addi r11,r11,32
  616. stvx v22,r10,$sp
  617. addi r10,r10,32
  618. stvx v23,r11,$sp
  619. addi r11,r11,32
  620. stvx v24,r10,$sp
  621. addi r10,r10,32
  622. stvx v25,r11,$sp
  623. addi r11,r11,32
  624. stvx v26,r10,$sp
  625. addi r10,r10,32
  626. stvx v27,r11,$sp
  627. addi r11,r11,32
  628. stvx v28,r10,$sp
  629. addi r10,r10,32
  630. stvx v29,r11,$sp
  631. addi r11,r11,32
  632. stvx v30,r10,$sp
  633. stvx v31,r11,$sp
  634. li r0,-1
  635. stw $vrsave,`$FRAME+21*16-4`($sp) # save vrsave
  636. li $x10,0x10
  637. $PUSH r26,`$FRAME+21*16+0*$SIZE_T`($sp)
  638. li $x20,0x20
  639. $PUSH r27,`$FRAME+21*16+1*$SIZE_T`($sp)
  640. li $x30,0x30
  641. $PUSH r28,`$FRAME+21*16+2*$SIZE_T`($sp)
  642. li $x40,0x40
  643. $PUSH r29,`$FRAME+21*16+3*$SIZE_T`($sp)
  644. li $x50,0x50
  645. $PUSH r30,`$FRAME+21*16+4*$SIZE_T`($sp)
  646. li $x60,0x60
  647. $PUSH r31,`$FRAME+21*16+5*$SIZE_T`($sp)
  648. li $x70,0x70
  649. mtspr 256,r0
  650. subi $rounds,$rounds,3 # -4 in total
  651. subi $len,$len,128 # bias
  652. lvx $rndkey0,$x00,$key # load key schedule
  653. lvx v30,$x10,$key
  654. addi $key,$key,0x20
  655. lvx v31,$x00,$key
  656. ?vperm $rndkey0,$rndkey0,v30,$keyperm
  657. addi $key_,$sp,$FRAME+15
  658. mtctr $rounds
  659. Load_cbc_dec_key:
  660. ?vperm v24,v30,v31,$keyperm
  661. lvx v30,$x10,$key
  662. addi $key,$key,0x20
  663. stvx v24,$x00,$key_ # off-load round[1]
  664. ?vperm v25,v31,v30,$keyperm
  665. lvx v31,$x00,$key
  666. stvx v25,$x10,$key_ # off-load round[2]
  667. addi $key_,$key_,0x20
  668. bdnz Load_cbc_dec_key
  669. lvx v26,$x10,$key
  670. ?vperm v24,v30,v31,$keyperm
  671. lvx v27,$x20,$key
  672. stvx v24,$x00,$key_ # off-load round[3]
  673. ?vperm v25,v31,v26,$keyperm
  674. lvx v28,$x30,$key
  675. stvx v25,$x10,$key_ # off-load round[4]
  676. addi $key_,$sp,$FRAME+15 # rewind $key_
  677. ?vperm v26,v26,v27,$keyperm
  678. lvx v29,$x40,$key
  679. ?vperm v27,v27,v28,$keyperm
  680. lvx v30,$x50,$key
  681. ?vperm v28,v28,v29,$keyperm
  682. lvx v31,$x60,$key
  683. ?vperm v29,v29,v30,$keyperm
  684. lvx $out0,$x70,$key # borrow $out0
  685. ?vperm v30,v30,v31,$keyperm
  686. lvx v24,$x00,$key_ # pre-load round[1]
  687. ?vperm v31,v31,$out0,$keyperm
  688. lvx v25,$x10,$key_ # pre-load round[2]
  689. #lvx $inptail,0,$inp # "caller" already did this
  690. #addi $inp,$inp,15 # 15 is not typo
  691. subi $inp,$inp,15 # undo "caller"
  692. le?li $idx,8
  693. lvx_u $in0,$x00,$inp # load first 8 "words"
  694. le?lvsl $inpperm,0,$idx
  695. le?vspltisb $tmp,0x0f
  696. lvx_u $in1,$x10,$inp
  697. le?vxor $inpperm,$inpperm,$tmp # transform for lvx_u/stvx_u
  698. lvx_u $in2,$x20,$inp
  699. le?vperm $in0,$in0,$in0,$inpperm
  700. lvx_u $in3,$x30,$inp
  701. le?vperm $in1,$in1,$in1,$inpperm
  702. lvx_u $in4,$x40,$inp
  703. le?vperm $in2,$in2,$in2,$inpperm
  704. vxor $out0,$in0,$rndkey0
  705. lvx_u $in5,$x50,$inp
  706. le?vperm $in3,$in3,$in3,$inpperm
  707. vxor $out1,$in1,$rndkey0
  708. lvx_u $in6,$x60,$inp
  709. le?vperm $in4,$in4,$in4,$inpperm
  710. vxor $out2,$in2,$rndkey0
  711. lvx_u $in7,$x70,$inp
  712. addi $inp,$inp,0x80
  713. le?vperm $in5,$in5,$in5,$inpperm
  714. vxor $out3,$in3,$rndkey0
  715. le?vperm $in6,$in6,$in6,$inpperm
  716. vxor $out4,$in4,$rndkey0
  717. le?vperm $in7,$in7,$in7,$inpperm
  718. vxor $out5,$in5,$rndkey0
  719. vxor $out6,$in6,$rndkey0
  720. vxor $out7,$in7,$rndkey0
  721. mtctr $rounds
  722. b Loop_cbc_dec8x
  723. .align 5
  724. Loop_cbc_dec8x:
  725. vncipher $out0,$out0,v24
  726. vncipher $out1,$out1,v24
  727. vncipher $out2,$out2,v24
  728. vncipher $out3,$out3,v24
  729. vncipher $out4,$out4,v24
  730. vncipher $out5,$out5,v24
  731. vncipher $out6,$out6,v24
  732. vncipher $out7,$out7,v24
  733. lvx v24,$x20,$key_ # round[3]
  734. addi $key_,$key_,0x20
  735. vncipher $out0,$out0,v25
  736. vncipher $out1,$out1,v25
  737. vncipher $out2,$out2,v25
  738. vncipher $out3,$out3,v25
  739. vncipher $out4,$out4,v25
  740. vncipher $out5,$out5,v25
  741. vncipher $out6,$out6,v25
  742. vncipher $out7,$out7,v25
  743. lvx v25,$x10,$key_ # round[4]
  744. bdnz Loop_cbc_dec8x
  745. subic $len,$len,128 # $len-=128
  746. vncipher $out0,$out0,v24
  747. vncipher $out1,$out1,v24
  748. vncipher $out2,$out2,v24
  749. vncipher $out3,$out3,v24
  750. vncipher $out4,$out4,v24
  751. vncipher $out5,$out5,v24
  752. vncipher $out6,$out6,v24
  753. vncipher $out7,$out7,v24
  754. subfe. r0,r0,r0 # borrow?-1:0
  755. vncipher $out0,$out0,v25
  756. vncipher $out1,$out1,v25
  757. vncipher $out2,$out2,v25
  758. vncipher $out3,$out3,v25
  759. vncipher $out4,$out4,v25
  760. vncipher $out5,$out5,v25
  761. vncipher $out6,$out6,v25
  762. vncipher $out7,$out7,v25
  763. and r0,r0,$len
  764. vncipher $out0,$out0,v26
  765. vncipher $out1,$out1,v26
  766. vncipher $out2,$out2,v26
  767. vncipher $out3,$out3,v26
  768. vncipher $out4,$out4,v26
  769. vncipher $out5,$out5,v26
  770. vncipher $out6,$out6,v26
  771. vncipher $out7,$out7,v26
  772. add $inp,$inp,r0 # $inp is adjusted in such
  773. # way that at exit from the
  774. # loop inX-in7 are loaded
  775. # with last "words"
  776. vncipher $out0,$out0,v27
  777. vncipher $out1,$out1,v27
  778. vncipher $out2,$out2,v27
  779. vncipher $out3,$out3,v27
  780. vncipher $out4,$out4,v27
  781. vncipher $out5,$out5,v27
  782. vncipher $out6,$out6,v27
  783. vncipher $out7,$out7,v27
  784. addi $key_,$sp,$FRAME+15 # rewind $key_
  785. vncipher $out0,$out0,v28
  786. vncipher $out1,$out1,v28
  787. vncipher $out2,$out2,v28
  788. vncipher $out3,$out3,v28
  789. vncipher $out4,$out4,v28
  790. vncipher $out5,$out5,v28
  791. vncipher $out6,$out6,v28
  792. vncipher $out7,$out7,v28
  793. lvx v24,$x00,$key_ # re-pre-load round[1]
  794. vncipher $out0,$out0,v29
  795. vncipher $out1,$out1,v29
  796. vncipher $out2,$out2,v29
  797. vncipher $out3,$out3,v29
  798. vncipher $out4,$out4,v29
  799. vncipher $out5,$out5,v29
  800. vncipher $out6,$out6,v29
  801. vncipher $out7,$out7,v29
  802. lvx v25,$x10,$key_ # re-pre-load round[2]
  803. vncipher $out0,$out0,v30
  804. vxor $ivec,$ivec,v31 # xor with last round key
  805. vncipher $out1,$out1,v30
  806. vxor $in0,$in0,v31
  807. vncipher $out2,$out2,v30
  808. vxor $in1,$in1,v31
  809. vncipher $out3,$out3,v30
  810. vxor $in2,$in2,v31
  811. vncipher $out4,$out4,v30
  812. vxor $in3,$in3,v31
  813. vncipher $out5,$out5,v30
  814. vxor $in4,$in4,v31
  815. vncipher $out6,$out6,v30
  816. vxor $in5,$in5,v31
  817. vncipher $out7,$out7,v30
  818. vxor $in6,$in6,v31
  819. vncipherlast $out0,$out0,$ivec
  820. vncipherlast $out1,$out1,$in0
  821. lvx_u $in0,$x00,$inp # load next input block
  822. vncipherlast $out2,$out2,$in1
  823. lvx_u $in1,$x10,$inp
  824. vncipherlast $out3,$out3,$in2
  825. le?vperm $in0,$in0,$in0,$inpperm
  826. lvx_u $in2,$x20,$inp
  827. vncipherlast $out4,$out4,$in3
  828. le?vperm $in1,$in1,$in1,$inpperm
  829. lvx_u $in3,$x30,$inp
  830. vncipherlast $out5,$out5,$in4
  831. le?vperm $in2,$in2,$in2,$inpperm
  832. lvx_u $in4,$x40,$inp
  833. vncipherlast $out6,$out6,$in5
  834. le?vperm $in3,$in3,$in3,$inpperm
  835. lvx_u $in5,$x50,$inp
  836. vncipherlast $out7,$out7,$in6
  837. le?vperm $in4,$in4,$in4,$inpperm
  838. lvx_u $in6,$x60,$inp
  839. vmr $ivec,$in7
  840. le?vperm $in5,$in5,$in5,$inpperm
  841. lvx_u $in7,$x70,$inp
  842. addi $inp,$inp,0x80
  843. le?vperm $out0,$out0,$out0,$inpperm
  844. le?vperm $out1,$out1,$out1,$inpperm
  845. stvx_u $out0,$x00,$out
  846. le?vperm $in6,$in6,$in6,$inpperm
  847. vxor $out0,$in0,$rndkey0
  848. le?vperm $out2,$out2,$out2,$inpperm
  849. stvx_u $out1,$x10,$out
  850. le?vperm $in7,$in7,$in7,$inpperm
  851. vxor $out1,$in1,$rndkey0
  852. le?vperm $out3,$out3,$out3,$inpperm
  853. stvx_u $out2,$x20,$out
  854. vxor $out2,$in2,$rndkey0
  855. le?vperm $out4,$out4,$out4,$inpperm
  856. stvx_u $out3,$x30,$out
  857. vxor $out3,$in3,$rndkey0
  858. le?vperm $out5,$out5,$out5,$inpperm
  859. stvx_u $out4,$x40,$out
  860. vxor $out4,$in4,$rndkey0
  861. le?vperm $out6,$out6,$out6,$inpperm
  862. stvx_u $out5,$x50,$out
  863. vxor $out5,$in5,$rndkey0
  864. le?vperm $out7,$out7,$out7,$inpperm
  865. stvx_u $out6,$x60,$out
  866. vxor $out6,$in6,$rndkey0
  867. stvx_u $out7,$x70,$out
  868. addi $out,$out,0x80
  869. vxor $out7,$in7,$rndkey0
  870. mtctr $rounds
  871. beq Loop_cbc_dec8x # did $len-=128 borrow?
  872. addic. $len,$len,128
  873. beq Lcbc_dec8x_done
  874. nop
  875. nop
  876. Loop_cbc_dec8x_tail: # up to 7 "words" tail...
  877. vncipher $out1,$out1,v24
  878. vncipher $out2,$out2,v24
  879. vncipher $out3,$out3,v24
  880. vncipher $out4,$out4,v24
  881. vncipher $out5,$out5,v24
  882. vncipher $out6,$out6,v24
  883. vncipher $out7,$out7,v24
  884. lvx v24,$x20,$key_ # round[3]
  885. addi $key_,$key_,0x20
  886. vncipher $out1,$out1,v25
  887. vncipher $out2,$out2,v25
  888. vncipher $out3,$out3,v25
  889. vncipher $out4,$out4,v25
  890. vncipher $out5,$out5,v25
  891. vncipher $out6,$out6,v25
  892. vncipher $out7,$out7,v25
  893. lvx v25,$x10,$key_ # round[4]
  894. bdnz Loop_cbc_dec8x_tail
  895. vncipher $out1,$out1,v24
  896. vncipher $out2,$out2,v24
  897. vncipher $out3,$out3,v24
  898. vncipher $out4,$out4,v24
  899. vncipher $out5,$out5,v24
  900. vncipher $out6,$out6,v24
  901. vncipher $out7,$out7,v24
  902. vncipher $out1,$out1,v25
  903. vncipher $out2,$out2,v25
  904. vncipher $out3,$out3,v25
  905. vncipher $out4,$out4,v25
  906. vncipher $out5,$out5,v25
  907. vncipher $out6,$out6,v25
  908. vncipher $out7,$out7,v25
  909. vncipher $out1,$out1,v26
  910. vncipher $out2,$out2,v26
  911. vncipher $out3,$out3,v26
  912. vncipher $out4,$out4,v26
  913. vncipher $out5,$out5,v26
  914. vncipher $out6,$out6,v26
  915. vncipher $out7,$out7,v26
  916. vncipher $out1,$out1,v27
  917. vncipher $out2,$out2,v27
  918. vncipher $out3,$out3,v27
  919. vncipher $out4,$out4,v27
  920. vncipher $out5,$out5,v27
  921. vncipher $out6,$out6,v27
  922. vncipher $out7,$out7,v27
  923. vncipher $out1,$out1,v28
  924. vncipher $out2,$out2,v28
  925. vncipher $out3,$out3,v28
  926. vncipher $out4,$out4,v28
  927. vncipher $out5,$out5,v28
  928. vncipher $out6,$out6,v28
  929. vncipher $out7,$out7,v28
  930. vncipher $out1,$out1,v29
  931. vncipher $out2,$out2,v29
  932. vncipher $out3,$out3,v29
  933. vncipher $out4,$out4,v29
  934. vncipher $out5,$out5,v29
  935. vncipher $out6,$out6,v29
  936. vncipher $out7,$out7,v29
  937. vncipher $out1,$out1,v30
  938. vxor $ivec,$ivec,v31 # last round key
  939. vncipher $out2,$out2,v30
  940. vxor $in1,$in1,v31
  941. vncipher $out3,$out3,v30
  942. vxor $in2,$in2,v31
  943. vncipher $out4,$out4,v30
  944. vxor $in3,$in3,v31
  945. vncipher $out5,$out5,v30
  946. vxor $in4,$in4,v31
  947. vncipher $out6,$out6,v30
  948. vxor $in5,$in5,v31
  949. vncipher $out7,$out7,v30
  950. vxor $in6,$in6,v31
  951. cmplwi $len,32 # switch($len)
  952. blt Lcbc_dec8x_one
  953. nop
  954. beq Lcbc_dec8x_two
  955. cmplwi $len,64
  956. blt Lcbc_dec8x_three
  957. nop
  958. beq Lcbc_dec8x_four
  959. cmplwi $len,96
  960. blt Lcbc_dec8x_five
  961. nop
  962. beq Lcbc_dec8x_six
  963. Lcbc_dec8x_seven:
  964. vncipherlast $out1,$out1,$ivec
  965. vncipherlast $out2,$out2,$in1
  966. vncipherlast $out3,$out3,$in2
  967. vncipherlast $out4,$out4,$in3
  968. vncipherlast $out5,$out5,$in4
  969. vncipherlast $out6,$out6,$in5
  970. vncipherlast $out7,$out7,$in6
  971. vmr $ivec,$in7
  972. le?vperm $out1,$out1,$out1,$inpperm
  973. le?vperm $out2,$out2,$out2,$inpperm
  974. stvx_u $out1,$x00,$out
  975. le?vperm $out3,$out3,$out3,$inpperm
  976. stvx_u $out2,$x10,$out
  977. le?vperm $out4,$out4,$out4,$inpperm
  978. stvx_u $out3,$x20,$out
  979. le?vperm $out5,$out5,$out5,$inpperm
  980. stvx_u $out4,$x30,$out
  981. le?vperm $out6,$out6,$out6,$inpperm
  982. stvx_u $out5,$x40,$out
  983. le?vperm $out7,$out7,$out7,$inpperm
  984. stvx_u $out6,$x50,$out
  985. stvx_u $out7,$x60,$out
  986. addi $out,$out,0x70
  987. b Lcbc_dec8x_done
  988. .align 5
  989. Lcbc_dec8x_six:
  990. vncipherlast $out2,$out2,$ivec
  991. vncipherlast $out3,$out3,$in2
  992. vncipherlast $out4,$out4,$in3
  993. vncipherlast $out5,$out5,$in4
  994. vncipherlast $out6,$out6,$in5
  995. vncipherlast $out7,$out7,$in6
  996. vmr $ivec,$in7
  997. le?vperm $out2,$out2,$out2,$inpperm
  998. le?vperm $out3,$out3,$out3,$inpperm
  999. stvx_u $out2,$x00,$out
  1000. le?vperm $out4,$out4,$out4,$inpperm
  1001. stvx_u $out3,$x10,$out
  1002. le?vperm $out5,$out5,$out5,$inpperm
  1003. stvx_u $out4,$x20,$out
  1004. le?vperm $out6,$out6,$out6,$inpperm
  1005. stvx_u $out5,$x30,$out
  1006. le?vperm $out7,$out7,$out7,$inpperm
  1007. stvx_u $out6,$x40,$out
  1008. stvx_u $out7,$x50,$out
  1009. addi $out,$out,0x60
  1010. b Lcbc_dec8x_done
  1011. .align 5
  1012. Lcbc_dec8x_five:
  1013. vncipherlast $out3,$out3,$ivec
  1014. vncipherlast $out4,$out4,$in3
  1015. vncipherlast $out5,$out5,$in4
  1016. vncipherlast $out6,$out6,$in5
  1017. vncipherlast $out7,$out7,$in6
  1018. vmr $ivec,$in7
  1019. le?vperm $out3,$out3,$out3,$inpperm
  1020. le?vperm $out4,$out4,$out4,$inpperm
  1021. stvx_u $out3,$x00,$out
  1022. le?vperm $out5,$out5,$out5,$inpperm
  1023. stvx_u $out4,$x10,$out
  1024. le?vperm $out6,$out6,$out6,$inpperm
  1025. stvx_u $out5,$x20,$out
  1026. le?vperm $out7,$out7,$out7,$inpperm
  1027. stvx_u $out6,$x30,$out
  1028. stvx_u $out7,$x40,$out
  1029. addi $out,$out,0x50
  1030. b Lcbc_dec8x_done
  1031. .align 5
  1032. Lcbc_dec8x_four:
  1033. vncipherlast $out4,$out4,$ivec
  1034. vncipherlast $out5,$out5,$in4
  1035. vncipherlast $out6,$out6,$in5
  1036. vncipherlast $out7,$out7,$in6
  1037. vmr $ivec,$in7
  1038. le?vperm $out4,$out4,$out4,$inpperm
  1039. le?vperm $out5,$out5,$out5,$inpperm
  1040. stvx_u $out4,$x00,$out
  1041. le?vperm $out6,$out6,$out6,$inpperm
  1042. stvx_u $out5,$x10,$out
  1043. le?vperm $out7,$out7,$out7,$inpperm
  1044. stvx_u $out6,$x20,$out
  1045. stvx_u $out7,$x30,$out
  1046. addi $out,$out,0x40
  1047. b Lcbc_dec8x_done
  1048. .align 5
  1049. Lcbc_dec8x_three:
  1050. vncipherlast $out5,$out5,$ivec
  1051. vncipherlast $out6,$out6,$in5
  1052. vncipherlast $out7,$out7,$in6
  1053. vmr $ivec,$in7
  1054. le?vperm $out5,$out5,$out5,$inpperm
  1055. le?vperm $out6,$out6,$out6,$inpperm
  1056. stvx_u $out5,$x00,$out
  1057. le?vperm $out7,$out7,$out7,$inpperm
  1058. stvx_u $out6,$x10,$out
  1059. stvx_u $out7,$x20,$out
  1060. addi $out,$out,0x30
  1061. b Lcbc_dec8x_done
  1062. .align 5
  1063. Lcbc_dec8x_two:
  1064. vncipherlast $out6,$out6,$ivec
  1065. vncipherlast $out7,$out7,$in6
  1066. vmr $ivec,$in7
  1067. le?vperm $out6,$out6,$out6,$inpperm
  1068. le?vperm $out7,$out7,$out7,$inpperm
  1069. stvx_u $out6,$x00,$out
  1070. stvx_u $out7,$x10,$out
  1071. addi $out,$out,0x20
  1072. b Lcbc_dec8x_done
  1073. .align 5
  1074. Lcbc_dec8x_one:
  1075. vncipherlast $out7,$out7,$ivec
  1076. vmr $ivec,$in7
  1077. le?vperm $out7,$out7,$out7,$inpperm
  1078. stvx_u $out7,0,$out
  1079. addi $out,$out,0x10
  1080. Lcbc_dec8x_done:
  1081. le?vperm $ivec,$ivec,$ivec,$inpperm
  1082. stvx_u $ivec,0,$ivp # write [unaligned] iv
  1083. li r10,`$FRAME+15`
  1084. li r11,`$FRAME+31`
  1085. stvx $inpperm,r10,$sp # wipe copies of round keys
  1086. addi r10,r10,32
  1087. stvx $inpperm,r11,$sp
  1088. addi r11,r11,32
  1089. stvx $inpperm,r10,$sp
  1090. addi r10,r10,32
  1091. stvx $inpperm,r11,$sp
  1092. addi r11,r11,32
  1093. stvx $inpperm,r10,$sp
  1094. addi r10,r10,32
  1095. stvx $inpperm,r11,$sp
  1096. addi r11,r11,32
  1097. stvx $inpperm,r10,$sp
  1098. addi r10,r10,32
  1099. stvx $inpperm,r11,$sp
  1100. addi r11,r11,32
  1101. mtspr 256,$vrsave
  1102. lvx v20,r10,$sp # ABI says so
  1103. addi r10,r10,32
  1104. lvx v21,r11,$sp
  1105. addi r11,r11,32
  1106. lvx v22,r10,$sp
  1107. addi r10,r10,32
  1108. lvx v23,r11,$sp
  1109. addi r11,r11,32
  1110. lvx v24,r10,$sp
  1111. addi r10,r10,32
  1112. lvx v25,r11,$sp
  1113. addi r11,r11,32
  1114. lvx v26,r10,$sp
  1115. addi r10,r10,32
  1116. lvx v27,r11,$sp
  1117. addi r11,r11,32
  1118. lvx v28,r10,$sp
  1119. addi r10,r10,32
  1120. lvx v29,r11,$sp
  1121. addi r11,r11,32
  1122. lvx v30,r10,$sp
  1123. lvx v31,r11,$sp
  1124. $POP r26,`$FRAME+21*16+0*$SIZE_T`($sp)
  1125. $POP r27,`$FRAME+21*16+1*$SIZE_T`($sp)
  1126. $POP r28,`$FRAME+21*16+2*$SIZE_T`($sp)
  1127. $POP r29,`$FRAME+21*16+3*$SIZE_T`($sp)
  1128. $POP r30,`$FRAME+21*16+4*$SIZE_T`($sp)
  1129. $POP r31,`$FRAME+21*16+5*$SIZE_T`($sp)
  1130. addi $sp,$sp,`$FRAME+21*16+6*$SIZE_T`
  1131. blr
  1132. .long 0
  1133. .byte 0,12,0x04,0,0x80,6,6,0
  1134. .long 0
  1135. .size .${prefix}_cbc_encrypt,.-.${prefix}_cbc_encrypt
  1136. ___
  1137. }} }}}
  1138. #########################################################################
  1139. {{{ # CTR procedure[s] #
  1140. my ($inp,$out,$len,$key,$ivp,$x10,$rounds,$idx)=map("r$_",(3..10));
  1141. my ($rndkey0,$rndkey1,$inout,$tmp)= map("v$_",(0..3));
  1142. my ($ivec,$inptail,$inpperm,$outhead,$outperm,$outmask,$keyperm,$one)=
  1143. map("v$_",(4..11));
  1144. my $dat=$tmp;
  1145. $code.=<<___;
  1146. .globl .${prefix}_ctr32_encrypt_blocks
  1147. .align 5
  1148. .${prefix}_ctr32_encrypt_blocks:
  1149. ${UCMP}i $len,1
  1150. bltlr-
  1151. lis r0,0xfff0
  1152. mfspr $vrsave,256
  1153. mtspr 256,r0
  1154. li $idx,15
  1155. vxor $rndkey0,$rndkey0,$rndkey0
  1156. le?vspltisb $tmp,0x0f
  1157. lvx $ivec,0,$ivp # load [unaligned] iv
  1158. lvsl $inpperm,0,$ivp
  1159. lvx $inptail,$idx,$ivp
  1160. vspltisb $one,1
  1161. le?vxor $inpperm,$inpperm,$tmp
  1162. vperm $ivec,$ivec,$inptail,$inpperm
  1163. vsldoi $one,$rndkey0,$one,1
  1164. neg r11,$inp
  1165. ?lvsl $keyperm,0,$key # prepare for unaligned key
  1166. lwz $rounds,240($key)
  1167. lvsr $inpperm,0,r11 # prepare for unaligned load
  1168. lvx $inptail,0,$inp
  1169. addi $inp,$inp,15 # 15 is not typo
  1170. le?vxor $inpperm,$inpperm,$tmp
  1171. srwi $rounds,$rounds,1
  1172. li $idx,16
  1173. subi $rounds,$rounds,1
  1174. ${UCMP}i $len,8
  1175. bge _aesp8_ctr32_encrypt8x
  1176. ?lvsr $outperm,0,$out # prepare for unaligned store
  1177. vspltisb $outmask,-1
  1178. lvx $outhead,0,$out
  1179. ?vperm $outmask,$rndkey0,$outmask,$outperm
  1180. le?vxor $outperm,$outperm,$tmp
  1181. lvx $rndkey0,0,$key
  1182. mtctr $rounds
  1183. lvx $rndkey1,$idx,$key
  1184. addi $idx,$idx,16
  1185. ?vperm $rndkey0,$rndkey0,$rndkey1,$keyperm
  1186. vxor $inout,$ivec,$rndkey0
  1187. lvx $rndkey0,$idx,$key
  1188. addi $idx,$idx,16
  1189. b Loop_ctr32_enc
  1190. .align 5
  1191. Loop_ctr32_enc:
  1192. ?vperm $rndkey1,$rndkey1,$rndkey0,$keyperm
  1193. vcipher $inout,$inout,$rndkey1
  1194. lvx $rndkey1,$idx,$key
  1195. addi $idx,$idx,16
  1196. ?vperm $rndkey0,$rndkey0,$rndkey1,$keyperm
  1197. vcipher $inout,$inout,$rndkey0
  1198. lvx $rndkey0,$idx,$key
  1199. addi $idx,$idx,16
  1200. bdnz Loop_ctr32_enc
  1201. vadduwm $ivec,$ivec,$one
  1202. vmr $dat,$inptail
  1203. lvx $inptail,0,$inp
  1204. addi $inp,$inp,16
  1205. subic. $len,$len,1 # blocks--
  1206. ?vperm $rndkey1,$rndkey1,$rndkey0,$keyperm
  1207. vcipher $inout,$inout,$rndkey1
  1208. lvx $rndkey1,$idx,$key
  1209. vperm $dat,$dat,$inptail,$inpperm
  1210. li $idx,16
  1211. ?vperm $rndkey1,$rndkey0,$rndkey1,$keyperm
  1212. lvx $rndkey0,0,$key
  1213. vxor $dat,$dat,$rndkey1 # last round key
  1214. vcipherlast $inout,$inout,$dat
  1215. lvx $rndkey1,$idx,$key
  1216. addi $idx,$idx,16
  1217. vperm $inout,$inout,$inout,$outperm
  1218. vsel $dat,$outhead,$inout,$outmask
  1219. mtctr $rounds
  1220. ?vperm $rndkey0,$rndkey0,$rndkey1,$keyperm
  1221. vmr $outhead,$inout
  1222. vxor $inout,$ivec,$rndkey0
  1223. lvx $rndkey0,$idx,$key
  1224. addi $idx,$idx,16
  1225. stvx $dat,0,$out
  1226. addi $out,$out,16
  1227. bne Loop_ctr32_enc
  1228. addi $out,$out,-1
  1229. lvx $inout,0,$out # redundant in aligned case
  1230. vsel $inout,$outhead,$inout,$outmask
  1231. stvx $inout,0,$out
  1232. mtspr 256,$vrsave
  1233. blr
  1234. .long 0
  1235. .byte 0,12,0x14,0,0,0,6,0
  1236. .long 0
  1237. ___
  1238. #########################################################################
  1239. {{ # Optimized CTR procedure #
  1240. my $key_="r11";
  1241. my ($x00,$x10,$x20,$x30,$x40,$x50,$x60,$x70)=map("r$_",(0,8,26..31));
  1242. $x00=0 if ($flavour =~ /osx/);
  1243. my ($in0, $in1, $in2, $in3, $in4, $in5, $in6, $in7 )=map("v$_",(0..3,10,12..14));
  1244. my ($out0,$out1,$out2,$out3,$out4,$out5,$out6,$out7)=map("v$_",(15..22));
  1245. my $rndkey0="v23"; # v24-v25 rotating buffer for first found keys
  1246. # v26-v31 last 6 round keys
  1247. my ($tmp,$keyperm)=($in3,$in4); # aliases with "caller", redundant assignment
  1248. my ($two,$three,$four)=($outhead,$outperm,$outmask);
  1249. $code.=<<___;
  1250. .align 5
  1251. _aesp8_ctr32_encrypt8x:
  1252. $STU $sp,-`($FRAME+21*16+6*$SIZE_T)`($sp)
  1253. li r10,`$FRAME+8*16+15`
  1254. li r11,`$FRAME+8*16+31`
  1255. stvx v20,r10,$sp # ABI says so
  1256. addi r10,r10,32
  1257. stvx v21,r11,$sp
  1258. addi r11,r11,32
  1259. stvx v22,r10,$sp
  1260. addi r10,r10,32
  1261. stvx v23,r11,$sp
  1262. addi r11,r11,32
  1263. stvx v24,r10,$sp
  1264. addi r10,r10,32
  1265. stvx v25,r11,$sp
  1266. addi r11,r11,32
  1267. stvx v26,r10,$sp
  1268. addi r10,r10,32
  1269. stvx v27,r11,$sp
  1270. addi r11,r11,32
  1271. stvx v28,r10,$sp
  1272. addi r10,r10,32
  1273. stvx v29,r11,$sp
  1274. addi r11,r11,32
  1275. stvx v30,r10,$sp
  1276. stvx v31,r11,$sp
  1277. li r0,-1
  1278. stw $vrsave,`$FRAME+21*16-4`($sp) # save vrsave
  1279. li $x10,0x10
  1280. $PUSH r26,`$FRAME+21*16+0*$SIZE_T`($sp)
  1281. li $x20,0x20
  1282. $PUSH r27,`$FRAME+21*16+1*$SIZE_T`($sp)
  1283. li $x30,0x30
  1284. $PUSH r28,`$FRAME+21*16+2*$SIZE_T`($sp)
  1285. li $x40,0x40
  1286. $PUSH r29,`$FRAME+21*16+3*$SIZE_T`($sp)
  1287. li $x50,0x50
  1288. $PUSH r30,`$FRAME+21*16+4*$SIZE_T`($sp)
  1289. li $x60,0x60
  1290. $PUSH r31,`$FRAME+21*16+5*$SIZE_T`($sp)
  1291. li $x70,0x70
  1292. mtspr 256,r0
  1293. subi $rounds,$rounds,3 # -4 in total
  1294. lvx $rndkey0,$x00,$key # load key schedule
  1295. lvx v30,$x10,$key
  1296. addi $key,$key,0x20
  1297. lvx v31,$x00,$key
  1298. ?vperm $rndkey0,$rndkey0,v30,$keyperm
  1299. addi $key_,$sp,$FRAME+15
  1300. mtctr $rounds
  1301. Load_ctr32_enc_key:
  1302. ?vperm v24,v30,v31,$keyperm
  1303. lvx v30,$x10,$key
  1304. addi $key,$key,0x20
  1305. stvx v24,$x00,$key_ # off-load round[1]
  1306. ?vperm v25,v31,v30,$keyperm
  1307. lvx v31,$x00,$key
  1308. stvx v25,$x10,$key_ # off-load round[2]
  1309. addi $key_,$key_,0x20
  1310. bdnz Load_ctr32_enc_key
  1311. lvx v26,$x10,$key
  1312. ?vperm v24,v30,v31,$keyperm
  1313. lvx v27,$x20,$key
  1314. stvx v24,$x00,$key_ # off-load round[3]
  1315. ?vperm v25,v31,v26,$keyperm
  1316. lvx v28,$x30,$key
  1317. stvx v25,$x10,$key_ # off-load round[4]
  1318. addi $key_,$sp,$FRAME+15 # rewind $key_
  1319. ?vperm v26,v26,v27,$keyperm
  1320. lvx v29,$x40,$key
  1321. ?vperm v27,v27,v28,$keyperm
  1322. lvx v30,$x50,$key
  1323. ?vperm v28,v28,v29,$keyperm
  1324. lvx v31,$x60,$key
  1325. ?vperm v29,v29,v30,$keyperm
  1326. lvx $out0,$x70,$key # borrow $out0
  1327. ?vperm v30,v30,v31,$keyperm
  1328. lvx v24,$x00,$key_ # pre-load round[1]
  1329. ?vperm v31,v31,$out0,$keyperm
  1330. lvx v25,$x10,$key_ # pre-load round[2]
  1331. vadduwm $two,$one,$one
  1332. subi $inp,$inp,15 # undo "caller"
  1333. $SHL $len,$len,4
  1334. vadduwm $out1,$ivec,$one # counter values ...
  1335. vadduwm $out2,$ivec,$two
  1336. vxor $out0,$ivec,$rndkey0 # ... xored with rndkey[0]
  1337. le?li $idx,8
  1338. vadduwm $out3,$out1,$two
  1339. vxor $out1,$out1,$rndkey0
  1340. le?lvsl $inpperm,0,$idx
  1341. vadduwm $out4,$out2,$two
  1342. vxor $out2,$out2,$rndkey0
  1343. le?vspltisb $tmp,0x0f
  1344. vadduwm $out5,$out3,$two
  1345. vxor $out3,$out3,$rndkey0
  1346. le?vxor $inpperm,$inpperm,$tmp # transform for lvx_u/stvx_u
  1347. vadduwm $out6,$out4,$two
  1348. vxor $out4,$out4,$rndkey0
  1349. vadduwm $out7,$out5,$two
  1350. vxor $out5,$out5,$rndkey0
  1351. vadduwm $ivec,$out6,$two # next counter value
  1352. vxor $out6,$out6,$rndkey0
  1353. vxor $out7,$out7,$rndkey0
  1354. mtctr $rounds
  1355. b Loop_ctr32_enc8x
  1356. .align 5
  1357. Loop_ctr32_enc8x:
  1358. vcipher $out0,$out0,v24
  1359. vcipher $out1,$out1,v24
  1360. vcipher $out2,$out2,v24
  1361. vcipher $out3,$out3,v24
  1362. vcipher $out4,$out4,v24
  1363. vcipher $out5,$out5,v24
  1364. vcipher $out6,$out6,v24
  1365. vcipher $out7,$out7,v24
  1366. Loop_ctr32_enc8x_middle:
  1367. lvx v24,$x20,$key_ # round[3]
  1368. addi $key_,$key_,0x20
  1369. vcipher $out0,$out0,v25
  1370. vcipher $out1,$out1,v25
  1371. vcipher $out2,$out2,v25
  1372. vcipher $out3,$out3,v25
  1373. vcipher $out4,$out4,v25
  1374. vcipher $out5,$out5,v25
  1375. vcipher $out6,$out6,v25
  1376. vcipher $out7,$out7,v25
  1377. lvx v25,$x10,$key_ # round[4]
  1378. bdnz Loop_ctr32_enc8x
  1379. subic r11,$len,256 # $len-256, borrow $key_
  1380. vcipher $out0,$out0,v24
  1381. vcipher $out1,$out1,v24
  1382. vcipher $out2,$out2,v24
  1383. vcipher $out3,$out3,v24
  1384. vcipher $out4,$out4,v24
  1385. vcipher $out5,$out5,v24
  1386. vcipher $out6,$out6,v24
  1387. vcipher $out7,$out7,v24
  1388. subfe r0,r0,r0 # borrow?-1:0
  1389. vcipher $out0,$out0,v25
  1390. vcipher $out1,$out1,v25
  1391. vcipher $out2,$out2,v25
  1392. vcipher $out3,$out3,v25
  1393. vcipher $out4,$out4,v25
  1394. vcipher $out5,$out5,v25
  1395. vcipher $out6,$out6,v25
  1396. vcipher $out7,$out7,v25
  1397. and r0,r0,r11
  1398. addi $key_,$sp,$FRAME+15 # rewind $key_
  1399. vcipher $out0,$out0,v26
  1400. vcipher $out1,$out1,v26
  1401. vcipher $out2,$out2,v26
  1402. vcipher $out3,$out3,v26
  1403. vcipher $out4,$out4,v26
  1404. vcipher $out5,$out5,v26
  1405. vcipher $out6,$out6,v26
  1406. vcipher $out7,$out7,v26
  1407. lvx v24,$x00,$key_ # re-pre-load round[1]
  1408. subic $len,$len,129 # $len-=129
  1409. vcipher $out0,$out0,v27
  1410. addi $len,$len,1 # $len-=128 really
  1411. vcipher $out1,$out1,v27
  1412. vcipher $out2,$out2,v27
  1413. vcipher $out3,$out3,v27
  1414. vcipher $out4,$out4,v27
  1415. vcipher $out5,$out5,v27
  1416. vcipher $out6,$out6,v27
  1417. vcipher $out7,$out7,v27
  1418. lvx v25,$x10,$key_ # re-pre-load round[2]
  1419. vcipher $out0,$out0,v28
  1420. lvx_u $in0,$x00,$inp # load input
  1421. vcipher $out1,$out1,v28
  1422. lvx_u $in1,$x10,$inp
  1423. vcipher $out2,$out2,v28
  1424. lvx_u $in2,$x20,$inp
  1425. vcipher $out3,$out3,v28
  1426. lvx_u $in3,$x30,$inp
  1427. vcipher $out4,$out4,v28
  1428. lvx_u $in4,$x40,$inp
  1429. vcipher $out5,$out5,v28
  1430. lvx_u $in5,$x50,$inp
  1431. vcipher $out6,$out6,v28
  1432. lvx_u $in6,$x60,$inp
  1433. vcipher $out7,$out7,v28
  1434. lvx_u $in7,$x70,$inp
  1435. addi $inp,$inp,0x80
  1436. vcipher $out0,$out0,v29
  1437. le?vperm $in0,$in0,$in0,$inpperm
  1438. vcipher $out1,$out1,v29
  1439. le?vperm $in1,$in1,$in1,$inpperm
  1440. vcipher $out2,$out2,v29
  1441. le?vperm $in2,$in2,$in2,$inpperm
  1442. vcipher $out3,$out3,v29
  1443. le?vperm $in3,$in3,$in3,$inpperm
  1444. vcipher $out4,$out4,v29
  1445. le?vperm $in4,$in4,$in4,$inpperm
  1446. vcipher $out5,$out5,v29
  1447. le?vperm $in5,$in5,$in5,$inpperm
  1448. vcipher $out6,$out6,v29
  1449. le?vperm $in6,$in6,$in6,$inpperm
  1450. vcipher $out7,$out7,v29
  1451. le?vperm $in7,$in7,$in7,$inpperm
  1452. add $inp,$inp,r0 # $inp is adjusted in such
  1453. # way that at exit from the
  1454. # loop inX-in7 are loaded
  1455. # with last "words"
  1456. subfe. r0,r0,r0 # borrow?-1:0
  1457. vcipher $out0,$out0,v30
  1458. vxor $in0,$in0,v31 # xor with last round key
  1459. vcipher $out1,$out1,v30
  1460. vxor $in1,$in1,v31
  1461. vcipher $out2,$out2,v30
  1462. vxor $in2,$in2,v31
  1463. vcipher $out3,$out3,v30
  1464. vxor $in3,$in3,v31
  1465. vcipher $out4,$out4,v30
  1466. vxor $in4,$in4,v31
  1467. vcipher $out5,$out5,v30
  1468. vxor $in5,$in5,v31
  1469. vcipher $out6,$out6,v30
  1470. vxor $in6,$in6,v31
  1471. vcipher $out7,$out7,v30
  1472. vxor $in7,$in7,v31
  1473. bne Lctr32_enc8x_break # did $len-129 borrow?
  1474. vcipherlast $in0,$out0,$in0
  1475. vcipherlast $in1,$out1,$in1
  1476. vadduwm $out1,$ivec,$one # counter values ...
  1477. vcipherlast $in2,$out2,$in2
  1478. vadduwm $out2,$ivec,$two
  1479. vxor $out0,$ivec,$rndkey0 # ... xored with rndkey[0]
  1480. vcipherlast $in3,$out3,$in3
  1481. vadduwm $out3,$out1,$two
  1482. vxor $out1,$out1,$rndkey0
  1483. vcipherlast $in4,$out4,$in4
  1484. vadduwm $out4,$out2,$two
  1485. vxor $out2,$out2,$rndkey0
  1486. vcipherlast $in5,$out5,$in5
  1487. vadduwm $out5,$out3,$two
  1488. vxor $out3,$out3,$rndkey0
  1489. vcipherlast $in6,$out6,$in6
  1490. vadduwm $out6,$out4,$two
  1491. vxor $out4,$out4,$rndkey0
  1492. vcipherlast $in7,$out7,$in7
  1493. vadduwm $out7,$out5,$two
  1494. vxor $out5,$out5,$rndkey0
  1495. le?vperm $in0,$in0,$in0,$inpperm
  1496. vadduwm $ivec,$out6,$two # next counter value
  1497. vxor $out6,$out6,$rndkey0
  1498. le?vperm $in1,$in1,$in1,$inpperm
  1499. vxor $out7,$out7,$rndkey0
  1500. mtctr $rounds
  1501. vcipher $out0,$out0,v24
  1502. stvx_u $in0,$x00,$out
  1503. le?vperm $in2,$in2,$in2,$inpperm
  1504. vcipher $out1,$out1,v24
  1505. stvx_u $in1,$x10,$out
  1506. le?vperm $in3,$in3,$in3,$inpperm
  1507. vcipher $out2,$out2,v24
  1508. stvx_u $in2,$x20,$out
  1509. le?vperm $in4,$in4,$in4,$inpperm
  1510. vcipher $out3,$out3,v24
  1511. stvx_u $in3,$x30,$out
  1512. le?vperm $in5,$in5,$in5,$inpperm
  1513. vcipher $out4,$out4,v24
  1514. stvx_u $in4,$x40,$out
  1515. le?vperm $in6,$in6,$in6,$inpperm
  1516. vcipher $out5,$out5,v24
  1517. stvx_u $in5,$x50,$out
  1518. le?vperm $in7,$in7,$in7,$inpperm
  1519. vcipher $out6,$out6,v24
  1520. stvx_u $in6,$x60,$out
  1521. vcipher $out7,$out7,v24
  1522. stvx_u $in7,$x70,$out
  1523. addi $out,$out,0x80
  1524. b Loop_ctr32_enc8x_middle
  1525. .align 5
  1526. Lctr32_enc8x_break:
  1527. cmpwi $len,-0x60
  1528. blt Lctr32_enc8x_one
  1529. nop
  1530. beq Lctr32_enc8x_two
  1531. cmpwi $len,-0x40
  1532. blt Lctr32_enc8x_three
  1533. nop
  1534. beq Lctr32_enc8x_four
  1535. cmpwi $len,-0x20
  1536. blt Lctr32_enc8x_five
  1537. nop
  1538. beq Lctr32_enc8x_six
  1539. cmpwi $len,0x00
  1540. blt Lctr32_enc8x_seven
  1541. Lctr32_enc8x_eight:
  1542. vcipherlast $out0,$out0,$in0
  1543. vcipherlast $out1,$out1,$in1
  1544. vcipherlast $out2,$out2,$in2
  1545. vcipherlast $out3,$out3,$in3
  1546. vcipherlast $out4,$out4,$in4
  1547. vcipherlast $out5,$out5,$in5
  1548. vcipherlast $out6,$out6,$in6
  1549. vcipherlast $out7,$out7,$in7
  1550. le?vperm $out0,$out0,$out0,$inpperm
  1551. le?vperm $out1,$out1,$out1,$inpperm
  1552. stvx_u $out0,$x00,$out
  1553. le?vperm $out2,$out2,$out2,$inpperm
  1554. stvx_u $out1,$x10,$out
  1555. le?vperm $out3,$out3,$out3,$inpperm
  1556. stvx_u $out2,$x20,$out
  1557. le?vperm $out4,$out4,$out4,$inpperm
  1558. stvx_u $out3,$x30,$out
  1559. le?vperm $out5,$out5,$out5,$inpperm
  1560. stvx_u $out4,$x40,$out
  1561. le?vperm $out6,$out6,$out6,$inpperm
  1562. stvx_u $out5,$x50,$out
  1563. le?vperm $out7,$out7,$out7,$inpperm
  1564. stvx_u $out6,$x60,$out
  1565. stvx_u $out7,$x70,$out
  1566. addi $out,$out,0x80
  1567. b Lctr32_enc8x_done
  1568. .align 5
  1569. Lctr32_enc8x_seven:
  1570. vcipherlast $out0,$out0,$in1
  1571. vcipherlast $out1,$out1,$in2
  1572. vcipherlast $out2,$out2,$in3
  1573. vcipherlast $out3,$out3,$in4
  1574. vcipherlast $out4,$out4,$in5
  1575. vcipherlast $out5,$out5,$in6
  1576. vcipherlast $out6,$out6,$in7
  1577. le?vperm $out0,$out0,$out0,$inpperm
  1578. le?vperm $out1,$out1,$out1,$inpperm
  1579. stvx_u $out0,$x00,$out
  1580. le?vperm $out2,$out2,$out2,$inpperm
  1581. stvx_u $out1,$x10,$out
  1582. le?vperm $out3,$out3,$out3,$inpperm
  1583. stvx_u $out2,$x20,$out
  1584. le?vperm $out4,$out4,$out4,$inpperm
  1585. stvx_u $out3,$x30,$out
  1586. le?vperm $out5,$out5,$out5,$inpperm
  1587. stvx_u $out4,$x40,$out
  1588. le?vperm $out6,$out6,$out6,$inpperm
  1589. stvx_u $out5,$x50,$out
  1590. stvx_u $out6,$x60,$out
  1591. addi $out,$out,0x70
  1592. b Lctr32_enc8x_done
  1593. .align 5
  1594. Lctr32_enc8x_six:
  1595. vcipherlast $out0,$out0,$in2
  1596. vcipherlast $out1,$out1,$in3
  1597. vcipherlast $out2,$out2,$in4
  1598. vcipherlast $out3,$out3,$in5
  1599. vcipherlast $out4,$out4,$in6
  1600. vcipherlast $out5,$out5,$in7
  1601. le?vperm $out0,$out0,$out0,$inpperm
  1602. le?vperm $out1,$out1,$out1,$inpperm
  1603. stvx_u $out0,$x00,$out
  1604. le?vperm $out2,$out2,$out2,$inpperm
  1605. stvx_u $out1,$x10,$out
  1606. le?vperm $out3,$out3,$out3,$inpperm
  1607. stvx_u $out2,$x20,$out
  1608. le?vperm $out4,$out4,$out4,$inpperm
  1609. stvx_u $out3,$x30,$out
  1610. le?vperm $out5,$out5,$out5,$inpperm
  1611. stvx_u $out4,$x40,$out
  1612. stvx_u $out5,$x50,$out
  1613. addi $out,$out,0x60
  1614. b Lctr32_enc8x_done
  1615. .align 5
  1616. Lctr32_enc8x_five:
  1617. vcipherlast $out0,$out0,$in3
  1618. vcipherlast $out1,$out1,$in4
  1619. vcipherlast $out2,$out2,$in5
  1620. vcipherlast $out3,$out3,$in6
  1621. vcipherlast $out4,$out4,$in7
  1622. le?vperm $out0,$out0,$out0,$inpperm
  1623. le?vperm $out1,$out1,$out1,$inpperm
  1624. stvx_u $out0,$x00,$out
  1625. le?vperm $out2,$out2,$out2,$inpperm
  1626. stvx_u $out1,$x10,$out
  1627. le?vperm $out3,$out3,$out3,$inpperm
  1628. stvx_u $out2,$x20,$out
  1629. le?vperm $out4,$out4,$out4,$inpperm
  1630. stvx_u $out3,$x30,$out
  1631. stvx_u $out4,$x40,$out
  1632. addi $out,$out,0x50
  1633. b Lctr32_enc8x_done
  1634. .align 5
  1635. Lctr32_enc8x_four:
  1636. vcipherlast $out0,$out0,$in4
  1637. vcipherlast $out1,$out1,$in5
  1638. vcipherlast $out2,$out2,$in6
  1639. vcipherlast $out3,$out3,$in7
  1640. le?vperm $out0,$out0,$out0,$inpperm
  1641. le?vperm $out1,$out1,$out1,$inpperm
  1642. stvx_u $out0,$x00,$out
  1643. le?vperm $out2,$out2,$out2,$inpperm
  1644. stvx_u $out1,$x10,$out
  1645. le?vperm $out3,$out3,$out3,$inpperm
  1646. stvx_u $out2,$x20,$out
  1647. stvx_u $out3,$x30,$out
  1648. addi $out,$out,0x40
  1649. b Lctr32_enc8x_done
  1650. .align 5
  1651. Lctr32_enc8x_three:
  1652. vcipherlast $out0,$out0,$in5
  1653. vcipherlast $out1,$out1,$in6
  1654. vcipherlast $out2,$out2,$in7
  1655. le?vperm $out0,$out0,$out0,$inpperm
  1656. le?vperm $out1,$out1,$out1,$inpperm
  1657. stvx_u $out0,$x00,$out
  1658. le?vperm $out2,$out2,$out2,$inpperm
  1659. stvx_u $out1,$x10,$out
  1660. stvx_u $out2,$x20,$out
  1661. addi $out,$out,0x30
  1662. b Lctr32_enc8x_done
  1663. .align 5
  1664. Lctr32_enc8x_two:
  1665. vcipherlast $out0,$out0,$in6
  1666. vcipherlast $out1,$out1,$in7
  1667. le?vperm $out0,$out0,$out0,$inpperm
  1668. le?vperm $out1,$out1,$out1,$inpperm
  1669. stvx_u $out0,$x00,$out
  1670. stvx_u $out1,$x10,$out
  1671. addi $out,$out,0x20
  1672. b Lctr32_enc8x_done
  1673. .align 5
  1674. Lctr32_enc8x_one:
  1675. vcipherlast $out0,$out0,$in7
  1676. le?vperm $out0,$out0,$out0,$inpperm
  1677. stvx_u $out0,0,$out
  1678. addi $out,$out,0x10
  1679. Lctr32_enc8x_done:
  1680. li r10,`$FRAME+15`
  1681. li r11,`$FRAME+31`
  1682. stvx $inpperm,r10,$sp # wipe copies of round keys
  1683. addi r10,r10,32
  1684. stvx $inpperm,r11,$sp
  1685. addi r11,r11,32
  1686. stvx $inpperm,r10,$sp
  1687. addi r10,r10,32
  1688. stvx $inpperm,r11,$sp
  1689. addi r11,r11,32
  1690. stvx $inpperm,r10,$sp
  1691. addi r10,r10,32
  1692. stvx $inpperm,r11,$sp
  1693. addi r11,r11,32
  1694. stvx $inpperm,r10,$sp
  1695. addi r10,r10,32
  1696. stvx $inpperm,r11,$sp
  1697. addi r11,r11,32
  1698. mtspr 256,$vrsave
  1699. lvx v20,r10,$sp # ABI says so
  1700. addi r10,r10,32
  1701. lvx v21,r11,$sp
  1702. addi r11,r11,32
  1703. lvx v22,r10,$sp
  1704. addi r10,r10,32
  1705. lvx v23,r11,$sp
  1706. addi r11,r11,32
  1707. lvx v24,r10,$sp
  1708. addi r10,r10,32
  1709. lvx v25,r11,$sp
  1710. addi r11,r11,32
  1711. lvx v26,r10,$sp
  1712. addi r10,r10,32
  1713. lvx v27,r11,$sp
  1714. addi r11,r11,32
  1715. lvx v28,r10,$sp
  1716. addi r10,r10,32
  1717. lvx v29,r11,$sp
  1718. addi r11,r11,32
  1719. lvx v30,r10,$sp
  1720. lvx v31,r11,$sp
  1721. $POP r26,`$FRAME+21*16+0*$SIZE_T`($sp)
  1722. $POP r27,`$FRAME+21*16+1*$SIZE_T`($sp)
  1723. $POP r28,`$FRAME+21*16+2*$SIZE_T`($sp)
  1724. $POP r29,`$FRAME+21*16+3*$SIZE_T`($sp)
  1725. $POP r30,`$FRAME+21*16+4*$SIZE_T`($sp)
  1726. $POP r31,`$FRAME+21*16+5*$SIZE_T`($sp)
  1727. addi $sp,$sp,`$FRAME+21*16+6*$SIZE_T`
  1728. blr
  1729. .long 0
  1730. .byte 0,12,0x04,0,0x80,6,6,0
  1731. .long 0
  1732. .size .${prefix}_ctr32_encrypt_blocks,.-.${prefix}_ctr32_encrypt_blocks
  1733. ___
  1734. }} }}}
  1735. #########################################################################
  1736. {{{ # XTS procedures #
  1737. # int aes_p8_xts_[en|de]crypt(const char *inp, char *out, size_t len, #
  1738. # const AES_KEY *key1, const AES_KEY *key2, #
  1739. # [const] unsigned char iv[16]); #
  1740. # If $key2 is NULL, then a "tweak chaining" mode is engaged, in which #
  1741. # input tweak value is assumed to be encrypted already, and last tweak #
  1742. # value, one suitable for consecutive call on same chunk of data, is #
  1743. # written back to original buffer. In addition, in "tweak chaining" #
  1744. # mode only complete input blocks are processed. #
  1745. my ($inp,$out,$len,$key1,$key2,$ivp,$rounds,$idx) = map("r$_",(3..10));
  1746. my ($rndkey0,$rndkey1,$inout) = map("v$_",(0..2));
  1747. my ($output,$inptail,$inpperm,$leperm,$keyperm) = map("v$_",(3..7));
  1748. my ($tweak,$seven,$eighty7,$tmp,$tweak1) = map("v$_",(8..12));
  1749. my $taillen = $key2;
  1750. ($inp,$idx) = ($idx,$inp); # reassign
  1751. $code.=<<___;
  1752. .globl .${prefix}_xts_encrypt
  1753. .align 5
  1754. .${prefix}_xts_encrypt:
  1755. mr $inp,r3 # reassign
  1756. li r3,-1
  1757. ${UCMP}i $len,16
  1758. bltlr-
  1759. lis r0,0xfff0
  1760. mfspr r12,256 # save vrsave
  1761. li r11,0
  1762. mtspr 256,r0
  1763. vspltisb $seven,0x07 # 0x070707..07
  1764. le?lvsl $leperm,r11,r11
  1765. le?vspltisb $tmp,0x0f
  1766. le?vxor $leperm,$leperm,$seven
  1767. li $idx,15
  1768. lvx $tweak,0,$ivp # load [unaligned] iv
  1769. lvsl $inpperm,0,$ivp
  1770. lvx $inptail,$idx,$ivp
  1771. le?vxor $inpperm,$inpperm,$tmp
  1772. vperm $tweak,$tweak,$inptail,$inpperm
  1773. neg r11,$inp
  1774. lvsr $inpperm,0,r11 # prepare for unaligned load
  1775. lvx $inout,0,$inp
  1776. addi $inp,$inp,15 # 15 is not typo
  1777. le?vxor $inpperm,$inpperm,$tmp
  1778. ${UCMP}i $key2,0 # key2==NULL?
  1779. beq Lxts_enc_no_key2
  1780. ?lvsl $keyperm,0,$key2 # prepare for unaligned key
  1781. lwz $rounds,240($key2)
  1782. srwi $rounds,$rounds,1
  1783. subi $rounds,$rounds,1
  1784. li $idx,16
  1785. lvx $rndkey0,0,$key2
  1786. lvx $rndkey1,$idx,$key2
  1787. addi $idx,$idx,16
  1788. ?vperm $rndkey0,$rndkey0,$rndkey1,$keyperm
  1789. vxor $tweak,$tweak,$rndkey0
  1790. lvx $rndkey0,$idx,$key2
  1791. addi $idx,$idx,16
  1792. mtctr $rounds
  1793. Ltweak_xts_enc:
  1794. ?vperm $rndkey1,$rndkey1,$rndkey0,$keyperm
  1795. vcipher $tweak,$tweak,$rndkey1
  1796. lvx $rndkey1,$idx,$key2
  1797. addi $idx,$idx,16
  1798. ?vperm $rndkey0,$rndkey0,$rndkey1,$keyperm
  1799. vcipher $tweak,$tweak,$rndkey0
  1800. lvx $rndkey0,$idx,$key2
  1801. addi $idx,$idx,16
  1802. bdnz Ltweak_xts_enc
  1803. ?vperm $rndkey1,$rndkey1,$rndkey0,$keyperm
  1804. vcipher $tweak,$tweak,$rndkey1
  1805. lvx $rndkey1,$idx,$key2
  1806. ?vperm $rndkey0,$rndkey0,$rndkey1,$keyperm
  1807. vcipherlast $tweak,$tweak,$rndkey0
  1808. li $ivp,0 # don't chain the tweak
  1809. b Lxts_enc
  1810. Lxts_enc_no_key2:
  1811. li $idx,-16
  1812. and $len,$len,$idx # in "tweak chaining"
  1813. # mode only complete
  1814. # blocks are processed
  1815. Lxts_enc:
  1816. lvx $inptail,0,$inp
  1817. addi $inp,$inp,16
  1818. ?lvsl $keyperm,0,$key1 # prepare for unaligned key
  1819. lwz $rounds,240($key1)
  1820. srwi $rounds,$rounds,1
  1821. subi $rounds,$rounds,1
  1822. li $idx,16
  1823. vslb $eighty7,$seven,$seven # 0x808080..80
  1824. vor $eighty7,$eighty7,$seven # 0x878787..87
  1825. vspltisb $tmp,1 # 0x010101..01
  1826. vsldoi $eighty7,$eighty7,$tmp,15 # 0x870101..01
  1827. ${UCMP}i $len,96
  1828. bge _aesp8_xts_encrypt6x
  1829. andi. $taillen,$len,15
  1830. subic r0,$len,32
  1831. subi $taillen,$taillen,16
  1832. subfe r0,r0,r0
  1833. and r0,r0,$taillen
  1834. add $inp,$inp,r0
  1835. lvx $rndkey0,0,$key1
  1836. lvx $rndkey1,$idx,$key1
  1837. addi $idx,$idx,16
  1838. vperm $inout,$inout,$inptail,$inpperm
  1839. ?vperm $rndkey0,$rndkey0,$rndkey1,$keyperm
  1840. vxor $inout,$inout,$tweak
  1841. vxor $inout,$inout,$rndkey0
  1842. lvx $rndkey0,$idx,$key1
  1843. addi $idx,$idx,16
  1844. mtctr $rounds
  1845. b Loop_xts_enc
  1846. .align 5
  1847. Loop_xts_enc:
  1848. ?vperm $rndkey1,$rndkey1,$rndkey0,$keyperm
  1849. vcipher $inout,$inout,$rndkey1
  1850. lvx $rndkey1,$idx,$key1
  1851. addi $idx,$idx,16
  1852. ?vperm $rndkey0,$rndkey0,$rndkey1,$keyperm
  1853. vcipher $inout,$inout,$rndkey0
  1854. lvx $rndkey0,$idx,$key1
  1855. addi $idx,$idx,16
  1856. bdnz Loop_xts_enc
  1857. ?vperm $rndkey1,$rndkey1,$rndkey0,$keyperm
  1858. vcipher $inout,$inout,$rndkey1
  1859. lvx $rndkey1,$idx,$key1
  1860. li $idx,16
  1861. ?vperm $rndkey0,$rndkey0,$rndkey1,$keyperm
  1862. vxor $rndkey0,$rndkey0,$tweak
  1863. vcipherlast $output,$inout,$rndkey0
  1864. le?vperm $tmp,$output,$output,$leperm
  1865. be?nop
  1866. le?stvx_u $tmp,0,$out
  1867. be?stvx_u $output,0,$out
  1868. addi $out,$out,16
  1869. subic. $len,$len,16
  1870. beq Lxts_enc_done
  1871. vmr $inout,$inptail
  1872. lvx $inptail,0,$inp
  1873. addi $inp,$inp,16
  1874. lvx $rndkey0,0,$key1
  1875. lvx $rndkey1,$idx,$key1
  1876. addi $idx,$idx,16
  1877. subic r0,$len,32
  1878. subfe r0,r0,r0
  1879. and r0,r0,$taillen
  1880. add $inp,$inp,r0
  1881. vsrab $tmp,$tweak,$seven # next tweak value
  1882. vaddubm $tweak,$tweak,$tweak
  1883. vsldoi $tmp,$tmp,$tmp,15
  1884. vand $tmp,$tmp,$eighty7
  1885. vxor $tweak,$tweak,$tmp
  1886. vperm $inout,$inout,$inptail,$inpperm
  1887. ?vperm $rndkey0,$rndkey0,$rndkey1,$keyperm
  1888. vxor $inout,$inout,$tweak
  1889. vxor $output,$output,$rndkey0 # just in case $len<16
  1890. vxor $inout,$inout,$rndkey0
  1891. lvx $rndkey0,$idx,$key1
  1892. addi $idx,$idx,16
  1893. mtctr $rounds
  1894. ${UCMP}i $len,16
  1895. bge Loop_xts_enc
  1896. vxor $output,$output,$tweak
  1897. lvsr $inpperm,0,$len # $inpperm is no longer needed
  1898. vxor $inptail,$inptail,$inptail # $inptail is no longer needed
  1899. vspltisb $tmp,-1
  1900. vperm $inptail,$inptail,$tmp,$inpperm
  1901. vsel $inout,$inout,$output,$inptail
  1902. subi r11,$out,17
  1903. subi $out,$out,16
  1904. mtctr $len
  1905. li $len,16
  1906. Loop_xts_enc_steal:
  1907. lbzu r0,1(r11)
  1908. stb r0,16(r11)
  1909. bdnz Loop_xts_enc_steal
  1910. mtctr $rounds
  1911. b Loop_xts_enc # one more time...
  1912. Lxts_enc_done:
  1913. ${UCMP}i $ivp,0
  1914. beq Lxts_enc_ret
  1915. vsrab $tmp,$tweak,$seven # next tweak value
  1916. vaddubm $tweak,$tweak,$tweak
  1917. vsldoi $tmp,$tmp,$tmp,15
  1918. vand $tmp,$tmp,$eighty7
  1919. vxor $tweak,$tweak,$tmp
  1920. le?vperm $tweak,$tweak,$tweak,$leperm
  1921. stvx_u $tweak,0,$ivp
  1922. Lxts_enc_ret:
  1923. mtspr 256,r12 # restore vrsave
  1924. li r3,0
  1925. blr
  1926. .long 0
  1927. .byte 0,12,0x04,0,0x80,6,6,0
  1928. .long 0
  1929. .size .${prefix}_xts_encrypt,.-.${prefix}_xts_encrypt
  1930. .globl .${prefix}_xts_decrypt
  1931. .align 5
  1932. .${prefix}_xts_decrypt:
  1933. mr $inp,r3 # reassign
  1934. li r3,-1
  1935. ${UCMP}i $len,16
  1936. bltlr-
  1937. lis r0,0xfff8
  1938. mfspr r12,256 # save vrsave
  1939. li r11,0
  1940. mtspr 256,r0
  1941. andi. r0,$len,15
  1942. neg r0,r0
  1943. andi. r0,r0,16
  1944. sub $len,$len,r0
  1945. vspltisb $seven,0x07 # 0x070707..07
  1946. le?lvsl $leperm,r11,r11
  1947. le?vspltisb $tmp,0x0f
  1948. le?vxor $leperm,$leperm,$seven
  1949. li $idx,15
  1950. lvx $tweak,0,$ivp # load [unaligned] iv
  1951. lvsl $inpperm,0,$ivp
  1952. lvx $inptail,$idx,$ivp
  1953. le?vxor $inpperm,$inpperm,$tmp
  1954. vperm $tweak,$tweak,$inptail,$inpperm
  1955. neg r11,$inp
  1956. lvsr $inpperm,0,r11 # prepare for unaligned load
  1957. lvx $inout,0,$inp
  1958. addi $inp,$inp,15 # 15 is not typo
  1959. le?vxor $inpperm,$inpperm,$tmp
  1960. ${UCMP}i $key2,0 # key2==NULL?
  1961. beq Lxts_dec_no_key2
  1962. ?lvsl $keyperm,0,$key2 # prepare for unaligned key
  1963. lwz $rounds,240($key2)
  1964. srwi $rounds,$rounds,1
  1965. subi $rounds,$rounds,1
  1966. li $idx,16
  1967. lvx $rndkey0,0,$key2
  1968. lvx $rndkey1,$idx,$key2
  1969. addi $idx,$idx,16
  1970. ?vperm $rndkey0,$rndkey0,$rndkey1,$keyperm
  1971. vxor $tweak,$tweak,$rndkey0
  1972. lvx $rndkey0,$idx,$key2
  1973. addi $idx,$idx,16
  1974. mtctr $rounds
  1975. Ltweak_xts_dec:
  1976. ?vperm $rndkey1,$rndkey1,$rndkey0,$keyperm
  1977. vcipher $tweak,$tweak,$rndkey1
  1978. lvx $rndkey1,$idx,$key2
  1979. addi $idx,$idx,16
  1980. ?vperm $rndkey0,$rndkey0,$rndkey1,$keyperm
  1981. vcipher $tweak,$tweak,$rndkey0
  1982. lvx $rndkey0,$idx,$key2
  1983. addi $idx,$idx,16
  1984. bdnz Ltweak_xts_dec
  1985. ?vperm $rndkey1,$rndkey1,$rndkey0,$keyperm
  1986. vcipher $tweak,$tweak,$rndkey1
  1987. lvx $rndkey1,$idx,$key2
  1988. ?vperm $rndkey0,$rndkey0,$rndkey1,$keyperm
  1989. vcipherlast $tweak,$tweak,$rndkey0
  1990. li $ivp,0 # don't chain the tweak
  1991. b Lxts_dec
  1992. Lxts_dec_no_key2:
  1993. neg $idx,$len
  1994. andi. $idx,$idx,15
  1995. add $len,$len,$idx # in "tweak chaining"
  1996. # mode only complete
  1997. # blocks are processed
  1998. Lxts_dec:
  1999. lvx $inptail,0,$inp
  2000. addi $inp,$inp,16
  2001. ?lvsl $keyperm,0,$key1 # prepare for unaligned key
  2002. lwz $rounds,240($key1)
  2003. srwi $rounds,$rounds,1
  2004. subi $rounds,$rounds,1
  2005. li $idx,16
  2006. vslb $eighty7,$seven,$seven # 0x808080..80
  2007. vor $eighty7,$eighty7,$seven # 0x878787..87
  2008. vspltisb $tmp,1 # 0x010101..01
  2009. vsldoi $eighty7,$eighty7,$tmp,15 # 0x870101..01
  2010. ${UCMP}i $len,96
  2011. bge _aesp8_xts_decrypt6x
  2012. lvx $rndkey0,0,$key1
  2013. lvx $rndkey1,$idx,$key1
  2014. addi $idx,$idx,16
  2015. vperm $inout,$inout,$inptail,$inpperm
  2016. ?vperm $rndkey0,$rndkey0,$rndkey1,$keyperm
  2017. vxor $inout,$inout,$tweak
  2018. vxor $inout,$inout,$rndkey0
  2019. lvx $rndkey0,$idx,$key1
  2020. addi $idx,$idx,16
  2021. mtctr $rounds
  2022. ${UCMP}i $len,16
  2023. blt Ltail_xts_dec
  2024. be?b Loop_xts_dec
  2025. .align 5
  2026. Loop_xts_dec:
  2027. ?vperm $rndkey1,$rndkey1,$rndkey0,$keyperm
  2028. vncipher $inout,$inout,$rndkey1
  2029. lvx $rndkey1,$idx,$key1
  2030. addi $idx,$idx,16
  2031. ?vperm $rndkey0,$rndkey0,$rndkey1,$keyperm
  2032. vncipher $inout,$inout,$rndkey0
  2033. lvx $rndkey0,$idx,$key1
  2034. addi $idx,$idx,16
  2035. bdnz Loop_xts_dec
  2036. ?vperm $rndkey1,$rndkey1,$rndkey0,$keyperm
  2037. vncipher $inout,$inout,$rndkey1
  2038. lvx $rndkey1,$idx,$key1
  2039. li $idx,16
  2040. ?vperm $rndkey0,$rndkey0,$rndkey1,$keyperm
  2041. vxor $rndkey0,$rndkey0,$tweak
  2042. vncipherlast $output,$inout,$rndkey0
  2043. le?vperm $tmp,$output,$output,$leperm
  2044. be?nop
  2045. le?stvx_u $tmp,0,$out
  2046. be?stvx_u $output,0,$out
  2047. addi $out,$out,16
  2048. subic. $len,$len,16
  2049. beq Lxts_dec_done
  2050. vmr $inout,$inptail
  2051. lvx $inptail,0,$inp
  2052. addi $inp,$inp,16
  2053. lvx $rndkey0,0,$key1
  2054. lvx $rndkey1,$idx,$key1
  2055. addi $idx,$idx,16
  2056. vsrab $tmp,$tweak,$seven # next tweak value
  2057. vaddubm $tweak,$tweak,$tweak
  2058. vsldoi $tmp,$tmp,$tmp,15
  2059. vand $tmp,$tmp,$eighty7
  2060. vxor $tweak,$tweak,$tmp
  2061. vperm $inout,$inout,$inptail,$inpperm
  2062. ?vperm $rndkey0,$rndkey0,$rndkey1,$keyperm
  2063. vxor $inout,$inout,$tweak
  2064. vxor $inout,$inout,$rndkey0
  2065. lvx $rndkey0,$idx,$key1
  2066. addi $idx,$idx,16
  2067. mtctr $rounds
  2068. ${UCMP}i $len,16
  2069. bge Loop_xts_dec
  2070. Ltail_xts_dec:
  2071. vsrab $tmp,$tweak,$seven # next tweak value
  2072. vaddubm $tweak1,$tweak,$tweak
  2073. vsldoi $tmp,$tmp,$tmp,15
  2074. vand $tmp,$tmp,$eighty7
  2075. vxor $tweak1,$tweak1,$tmp
  2076. subi $inp,$inp,16
  2077. add $inp,$inp,$len
  2078. vxor $inout,$inout,$tweak # :-(
  2079. vxor $inout,$inout,$tweak1 # :-)
  2080. Loop_xts_dec_short:
  2081. ?vperm $rndkey1,$rndkey1,$rndkey0,$keyperm
  2082. vncipher $inout,$inout,$rndkey1
  2083. lvx $rndkey1,$idx,$key1
  2084. addi $idx,$idx,16
  2085. ?vperm $rndkey0,$rndkey0,$rndkey1,$keyperm
  2086. vncipher $inout,$inout,$rndkey0
  2087. lvx $rndkey0,$idx,$key1
  2088. addi $idx,$idx,16
  2089. bdnz Loop_xts_dec_short
  2090. ?vperm $rndkey1,$rndkey1,$rndkey0,$keyperm
  2091. vncipher $inout,$inout,$rndkey1
  2092. lvx $rndkey1,$idx,$key1
  2093. li $idx,16
  2094. ?vperm $rndkey0,$rndkey0,$rndkey1,$keyperm
  2095. vxor $rndkey0,$rndkey0,$tweak1
  2096. vncipherlast $output,$inout,$rndkey0
  2097. le?vperm $tmp,$output,$output,$leperm
  2098. be?nop
  2099. le?stvx_u $tmp,0,$out
  2100. be?stvx_u $output,0,$out
  2101. vmr $inout,$inptail
  2102. lvx $inptail,0,$inp
  2103. #addi $inp,$inp,16
  2104. lvx $rndkey0,0,$key1
  2105. lvx $rndkey1,$idx,$key1
  2106. addi $idx,$idx,16
  2107. vperm $inout,$inout,$inptail,$inpperm
  2108. ?vperm $rndkey0,$rndkey0,$rndkey1,$keyperm
  2109. lvsr $inpperm,0,$len # $inpperm is no longer needed
  2110. vxor $inptail,$inptail,$inptail # $inptail is no longer needed
  2111. vspltisb $tmp,-1
  2112. vperm $inptail,$inptail,$tmp,$inpperm
  2113. vsel $inout,$inout,$output,$inptail
  2114. vxor $rndkey0,$rndkey0,$tweak
  2115. vxor $inout,$inout,$rndkey0
  2116. lvx $rndkey0,$idx,$key1
  2117. addi $idx,$idx,16
  2118. subi r11,$out,1
  2119. mtctr $len
  2120. li $len,16
  2121. Loop_xts_dec_steal:
  2122. lbzu r0,1(r11)
  2123. stb r0,16(r11)
  2124. bdnz Loop_xts_dec_steal
  2125. mtctr $rounds
  2126. b Loop_xts_dec # one more time...
  2127. Lxts_dec_done:
  2128. ${UCMP}i $ivp,0
  2129. beq Lxts_dec_ret
  2130. vsrab $tmp,$tweak,$seven # next tweak value
  2131. vaddubm $tweak,$tweak,$tweak
  2132. vsldoi $tmp,$tmp,$tmp,15
  2133. vand $tmp,$tmp,$eighty7
  2134. vxor $tweak,$tweak,$tmp
  2135. le?vperm $tweak,$tweak,$tweak,$leperm
  2136. stvx_u $tweak,0,$ivp
  2137. Lxts_dec_ret:
  2138. mtspr 256,r12 # restore vrsave
  2139. li r3,0
  2140. blr
  2141. .long 0
  2142. .byte 0,12,0x04,0,0x80,6,6,0
  2143. .long 0
  2144. .size .${prefix}_xts_decrypt,.-.${prefix}_xts_decrypt
  2145. ___
  2146. #########################################################################
  2147. {{ # Optimized XTS procedures #
  2148. my $key_=$key2;
  2149. my ($x00,$x10,$x20,$x30,$x40,$x50,$x60,$x70)=map("r$_",(0,3,26..31));
  2150. $x00=0 if ($flavour =~ /osx/);
  2151. my ($in0, $in1, $in2, $in3, $in4, $in5 )=map("v$_",(0..5));
  2152. my ($out0, $out1, $out2, $out3, $out4, $out5)=map("v$_",(7,12..16));
  2153. my ($twk0, $twk1, $twk2, $twk3, $twk4, $twk5)=map("v$_",(17..22));
  2154. my $rndkey0="v23"; # v24-v25 rotating buffer for first found keys
  2155. # v26-v31 last 6 round keys
  2156. my ($keyperm)=($out0); # aliases with "caller", redundant assignment
  2157. my $taillen=$x70;
  2158. $code.=<<___;
  2159. .align 5
  2160. _aesp8_xts_encrypt6x:
  2161. $STU $sp,-`($FRAME+21*16+6*$SIZE_T)`($sp)
  2162. mflr r11
  2163. li r7,`$FRAME+8*16+15`
  2164. li r3,`$FRAME+8*16+31`
  2165. $PUSH r11,`$FRAME+21*16+6*$SIZE_T+$LRSAVE`($sp)
  2166. stvx v20,r7,$sp # ABI says so
  2167. addi r7,r7,32
  2168. stvx v21,r3,$sp
  2169. addi r3,r3,32
  2170. stvx v22,r7,$sp
  2171. addi r7,r7,32
  2172. stvx v23,r3,$sp
  2173. addi r3,r3,32
  2174. stvx v24,r7,$sp
  2175. addi r7,r7,32
  2176. stvx v25,r3,$sp
  2177. addi r3,r3,32
  2178. stvx v26,r7,$sp
  2179. addi r7,r7,32
  2180. stvx v27,r3,$sp
  2181. addi r3,r3,32
  2182. stvx v28,r7,$sp
  2183. addi r7,r7,32
  2184. stvx v29,r3,$sp
  2185. addi r3,r3,32
  2186. stvx v30,r7,$sp
  2187. stvx v31,r3,$sp
  2188. li r0,-1
  2189. stw $vrsave,`$FRAME+21*16-4`($sp) # save vrsave
  2190. li $x10,0x10
  2191. $PUSH r26,`$FRAME+21*16+0*$SIZE_T`($sp)
  2192. li $x20,0x20
  2193. $PUSH r27,`$FRAME+21*16+1*$SIZE_T`($sp)
  2194. li $x30,0x30
  2195. $PUSH r28,`$FRAME+21*16+2*$SIZE_T`($sp)
  2196. li $x40,0x40
  2197. $PUSH r29,`$FRAME+21*16+3*$SIZE_T`($sp)
  2198. li $x50,0x50
  2199. $PUSH r30,`$FRAME+21*16+4*$SIZE_T`($sp)
  2200. li $x60,0x60
  2201. $PUSH r31,`$FRAME+21*16+5*$SIZE_T`($sp)
  2202. li $x70,0x70
  2203. mtspr 256,r0
  2204. subi $rounds,$rounds,3 # -4 in total
  2205. lvx $rndkey0,$x00,$key1 # load key schedule
  2206. lvx v30,$x10,$key1
  2207. addi $key1,$key1,0x20
  2208. lvx v31,$x00,$key1
  2209. ?vperm $rndkey0,$rndkey0,v30,$keyperm
  2210. addi $key_,$sp,$FRAME+15
  2211. mtctr $rounds
  2212. Load_xts_enc_key:
  2213. ?vperm v24,v30,v31,$keyperm
  2214. lvx v30,$x10,$key1
  2215. addi $key1,$key1,0x20
  2216. stvx v24,$x00,$key_ # off-load round[1]
  2217. ?vperm v25,v31,v30,$keyperm
  2218. lvx v31,$x00,$key1
  2219. stvx v25,$x10,$key_ # off-load round[2]
  2220. addi $key_,$key_,0x20
  2221. bdnz Load_xts_enc_key
  2222. lvx v26,$x10,$key1
  2223. ?vperm v24,v30,v31,$keyperm
  2224. lvx v27,$x20,$key1
  2225. stvx v24,$x00,$key_ # off-load round[3]
  2226. ?vperm v25,v31,v26,$keyperm
  2227. lvx v28,$x30,$key1
  2228. stvx v25,$x10,$key_ # off-load round[4]
  2229. addi $key_,$sp,$FRAME+15 # rewind $key_
  2230. ?vperm v26,v26,v27,$keyperm
  2231. lvx v29,$x40,$key1
  2232. ?vperm v27,v27,v28,$keyperm
  2233. lvx v30,$x50,$key1
  2234. ?vperm v28,v28,v29,$keyperm
  2235. lvx v31,$x60,$key1
  2236. ?vperm v29,v29,v30,$keyperm
  2237. lvx $twk5,$x70,$key1 # borrow $twk5
  2238. ?vperm v30,v30,v31,$keyperm
  2239. lvx v24,$x00,$key_ # pre-load round[1]
  2240. ?vperm v31,v31,$twk5,$keyperm
  2241. lvx v25,$x10,$key_ # pre-load round[2]
  2242. vperm $in0,$inout,$inptail,$inpperm
  2243. subi $inp,$inp,31 # undo "caller"
  2244. vxor $twk0,$tweak,$rndkey0
  2245. vsrab $tmp,$tweak,$seven # next tweak value
  2246. vaddubm $tweak,$tweak,$tweak
  2247. vsldoi $tmp,$tmp,$tmp,15
  2248. vand $tmp,$tmp,$eighty7
  2249. vxor $out0,$in0,$twk0
  2250. vxor $tweak,$tweak,$tmp
  2251. lvx_u $in1,$x10,$inp
  2252. vxor $twk1,$tweak,$rndkey0
  2253. vsrab $tmp,$tweak,$seven # next tweak value
  2254. vaddubm $tweak,$tweak,$tweak
  2255. vsldoi $tmp,$tmp,$tmp,15
  2256. le?vperm $in1,$in1,$in1,$leperm
  2257. vand $tmp,$tmp,$eighty7
  2258. vxor $out1,$in1,$twk1
  2259. vxor $tweak,$tweak,$tmp
  2260. lvx_u $in2,$x20,$inp
  2261. andi. $taillen,$len,15
  2262. vxor $twk2,$tweak,$rndkey0
  2263. vsrab $tmp,$tweak,$seven # next tweak value
  2264. vaddubm $tweak,$tweak,$tweak
  2265. vsldoi $tmp,$tmp,$tmp,15
  2266. le?vperm $in2,$in2,$in2,$leperm
  2267. vand $tmp,$tmp,$eighty7
  2268. vxor $out2,$in2,$twk2
  2269. vxor $tweak,$tweak,$tmp
  2270. lvx_u $in3,$x30,$inp
  2271. sub $len,$len,$taillen
  2272. vxor $twk3,$tweak,$rndkey0
  2273. vsrab $tmp,$tweak,$seven # next tweak value
  2274. vaddubm $tweak,$tweak,$tweak
  2275. vsldoi $tmp,$tmp,$tmp,15
  2276. le?vperm $in3,$in3,$in3,$leperm
  2277. vand $tmp,$tmp,$eighty7
  2278. vxor $out3,$in3,$twk3
  2279. vxor $tweak,$tweak,$tmp
  2280. lvx_u $in4,$x40,$inp
  2281. subi $len,$len,0x60
  2282. vxor $twk4,$tweak,$rndkey0
  2283. vsrab $tmp,$tweak,$seven # next tweak value
  2284. vaddubm $tweak,$tweak,$tweak
  2285. vsldoi $tmp,$tmp,$tmp,15
  2286. le?vperm $in4,$in4,$in4,$leperm
  2287. vand $tmp,$tmp,$eighty7
  2288. vxor $out4,$in4,$twk4
  2289. vxor $tweak,$tweak,$tmp
  2290. lvx_u $in5,$x50,$inp
  2291. addi $inp,$inp,0x60
  2292. vxor $twk5,$tweak,$rndkey0
  2293. vsrab $tmp,$tweak,$seven # next tweak value
  2294. vaddubm $tweak,$tweak,$tweak
  2295. vsldoi $tmp,$tmp,$tmp,15
  2296. le?vperm $in5,$in5,$in5,$leperm
  2297. vand $tmp,$tmp,$eighty7
  2298. vxor $out5,$in5,$twk5
  2299. vxor $tweak,$tweak,$tmp
  2300. vxor v31,v31,$rndkey0
  2301. mtctr $rounds
  2302. b Loop_xts_enc6x
  2303. .align 5
  2304. Loop_xts_enc6x:
  2305. vcipher $out0,$out0,v24
  2306. vcipher $out1,$out1,v24
  2307. vcipher $out2,$out2,v24
  2308. vcipher $out3,$out3,v24
  2309. vcipher $out4,$out4,v24
  2310. vcipher $out5,$out5,v24
  2311. lvx v24,$x20,$key_ # round[3]
  2312. addi $key_,$key_,0x20
  2313. vcipher $out0,$out0,v25
  2314. vcipher $out1,$out1,v25
  2315. vcipher $out2,$out2,v25
  2316. vcipher $out3,$out3,v25
  2317. vcipher $out4,$out4,v25
  2318. vcipher $out5,$out5,v25
  2319. lvx v25,$x10,$key_ # round[4]
  2320. bdnz Loop_xts_enc6x
  2321. subic $len,$len,96 # $len-=96
  2322. vxor $in0,$twk0,v31 # xor with last round key
  2323. vcipher $out0,$out0,v24
  2324. vcipher $out1,$out1,v24
  2325. vsrab $tmp,$tweak,$seven # next tweak value
  2326. vxor $twk0,$tweak,$rndkey0
  2327. vaddubm $tweak,$tweak,$tweak
  2328. vcipher $out2,$out2,v24
  2329. vcipher $out3,$out3,v24
  2330. vsldoi $tmp,$tmp,$tmp,15
  2331. vcipher $out4,$out4,v24
  2332. vcipher $out5,$out5,v24
  2333. subfe. r0,r0,r0 # borrow?-1:0
  2334. vand $tmp,$tmp,$eighty7
  2335. vcipher $out0,$out0,v25
  2336. vcipher $out1,$out1,v25
  2337. vxor $tweak,$tweak,$tmp
  2338. vcipher $out2,$out2,v25
  2339. vcipher $out3,$out3,v25
  2340. vxor $in1,$twk1,v31
  2341. vsrab $tmp,$tweak,$seven # next tweak value
  2342. vxor $twk1,$tweak,$rndkey0
  2343. vcipher $out4,$out4,v25
  2344. vcipher $out5,$out5,v25
  2345. and r0,r0,$len
  2346. vaddubm $tweak,$tweak,$tweak
  2347. vsldoi $tmp,$tmp,$tmp,15
  2348. vcipher $out0,$out0,v26
  2349. vcipher $out1,$out1,v26
  2350. vand $tmp,$tmp,$eighty7
  2351. vcipher $out2,$out2,v26
  2352. vcipher $out3,$out3,v26
  2353. vxor $tweak,$tweak,$tmp
  2354. vcipher $out4,$out4,v26
  2355. vcipher $out5,$out5,v26
  2356. add $inp,$inp,r0 # $inp is adjusted in such
  2357. # way that at exit from the
  2358. # loop inX-in5 are loaded
  2359. # with last "words"
  2360. vxor $in2,$twk2,v31
  2361. vsrab $tmp,$tweak,$seven # next tweak value
  2362. vxor $twk2,$tweak,$rndkey0
  2363. vaddubm $tweak,$tweak,$tweak
  2364. vcipher $out0,$out0,v27
  2365. vcipher $out1,$out1,v27
  2366. vsldoi $tmp,$tmp,$tmp,15
  2367. vcipher $out2,$out2,v27
  2368. vcipher $out3,$out3,v27
  2369. vand $tmp,$tmp,$eighty7
  2370. vcipher $out4,$out4,v27
  2371. vcipher $out5,$out5,v27
  2372. addi $key_,$sp,$FRAME+15 # rewind $key_
  2373. vxor $tweak,$tweak,$tmp
  2374. vcipher $out0,$out0,v28
  2375. vcipher $out1,$out1,v28
  2376. vxor $in3,$twk3,v31
  2377. vsrab $tmp,$tweak,$seven # next tweak value
  2378. vxor $twk3,$tweak,$rndkey0
  2379. vcipher $out2,$out2,v28
  2380. vcipher $out3,$out3,v28
  2381. vaddubm $tweak,$tweak,$tweak
  2382. vsldoi $tmp,$tmp,$tmp,15
  2383. vcipher $out4,$out4,v28
  2384. vcipher $out5,$out5,v28
  2385. lvx v24,$x00,$key_ # re-pre-load round[1]
  2386. vand $tmp,$tmp,$eighty7
  2387. vcipher $out0,$out0,v29
  2388. vcipher $out1,$out1,v29
  2389. vxor $tweak,$tweak,$tmp
  2390. vcipher $out2,$out2,v29
  2391. vcipher $out3,$out3,v29
  2392. vxor $in4,$twk4,v31
  2393. vsrab $tmp,$tweak,$seven # next tweak value
  2394. vxor $twk4,$tweak,$rndkey0
  2395. vcipher $out4,$out4,v29
  2396. vcipher $out5,$out5,v29
  2397. lvx v25,$x10,$key_ # re-pre-load round[2]
  2398. vaddubm $tweak,$tweak,$tweak
  2399. vsldoi $tmp,$tmp,$tmp,15
  2400. vcipher $out0,$out0,v30
  2401. vcipher $out1,$out1,v30
  2402. vand $tmp,$tmp,$eighty7
  2403. vcipher $out2,$out2,v30
  2404. vcipher $out3,$out3,v30
  2405. vxor $tweak,$tweak,$tmp
  2406. vcipher $out4,$out4,v30
  2407. vcipher $out5,$out5,v30
  2408. vxor $in5,$twk5,v31
  2409. vsrab $tmp,$tweak,$seven # next tweak value
  2410. vxor $twk5,$tweak,$rndkey0
  2411. vcipherlast $out0,$out0,$in0
  2412. lvx_u $in0,$x00,$inp # load next input block
  2413. vaddubm $tweak,$tweak,$tweak
  2414. vsldoi $tmp,$tmp,$tmp,15
  2415. vcipherlast $out1,$out1,$in1
  2416. lvx_u $in1,$x10,$inp
  2417. vcipherlast $out2,$out2,$in2
  2418. le?vperm $in0,$in0,$in0,$leperm
  2419. lvx_u $in2,$x20,$inp
  2420. vand $tmp,$tmp,$eighty7
  2421. vcipherlast $out3,$out3,$in3
  2422. le?vperm $in1,$in1,$in1,$leperm
  2423. lvx_u $in3,$x30,$inp
  2424. vcipherlast $out4,$out4,$in4
  2425. le?vperm $in2,$in2,$in2,$leperm
  2426. lvx_u $in4,$x40,$inp
  2427. vxor $tweak,$tweak,$tmp
  2428. vcipherlast $tmp,$out5,$in5 # last block might be needed
  2429. # in stealing mode
  2430. le?vperm $in3,$in3,$in3,$leperm
  2431. lvx_u $in5,$x50,$inp
  2432. addi $inp,$inp,0x60
  2433. le?vperm $in4,$in4,$in4,$leperm
  2434. le?vperm $in5,$in5,$in5,$leperm
  2435. le?vperm $out0,$out0,$out0,$leperm
  2436. le?vperm $out1,$out1,$out1,$leperm
  2437. stvx_u $out0,$x00,$out # store output
  2438. vxor $out0,$in0,$twk0
  2439. le?vperm $out2,$out2,$out2,$leperm
  2440. stvx_u $out1,$x10,$out
  2441. vxor $out1,$in1,$twk1
  2442. le?vperm $out3,$out3,$out3,$leperm
  2443. stvx_u $out2,$x20,$out
  2444. vxor $out2,$in2,$twk2
  2445. le?vperm $out4,$out4,$out4,$leperm
  2446. stvx_u $out3,$x30,$out
  2447. vxor $out3,$in3,$twk3
  2448. le?vperm $out5,$tmp,$tmp,$leperm
  2449. stvx_u $out4,$x40,$out
  2450. vxor $out4,$in4,$twk4
  2451. le?stvx_u $out5,$x50,$out
  2452. be?stvx_u $tmp, $x50,$out
  2453. vxor $out5,$in5,$twk5
  2454. addi $out,$out,0x60
  2455. mtctr $rounds
  2456. beq Loop_xts_enc6x # did $len-=96 borrow?
  2457. addic. $len,$len,0x60
  2458. beq Lxts_enc6x_zero
  2459. cmpwi $len,0x20
  2460. blt Lxts_enc6x_one
  2461. nop
  2462. beq Lxts_enc6x_two
  2463. cmpwi $len,0x40
  2464. blt Lxts_enc6x_three
  2465. nop
  2466. beq Lxts_enc6x_four
  2467. Lxts_enc6x_five:
  2468. vxor $out0,$in1,$twk0
  2469. vxor $out1,$in2,$twk1
  2470. vxor $out2,$in3,$twk2
  2471. vxor $out3,$in4,$twk3
  2472. vxor $out4,$in5,$twk4
  2473. bl _aesp8_xts_enc5x
  2474. le?vperm $out0,$out0,$out0,$leperm
  2475. vmr $twk0,$twk5 # unused tweak
  2476. le?vperm $out1,$out1,$out1,$leperm
  2477. stvx_u $out0,$x00,$out # store output
  2478. le?vperm $out2,$out2,$out2,$leperm
  2479. stvx_u $out1,$x10,$out
  2480. le?vperm $out3,$out3,$out3,$leperm
  2481. stvx_u $out2,$x20,$out
  2482. vxor $tmp,$out4,$twk5 # last block prep for stealing
  2483. le?vperm $out4,$out4,$out4,$leperm
  2484. stvx_u $out3,$x30,$out
  2485. stvx_u $out4,$x40,$out
  2486. addi $out,$out,0x50
  2487. bne Lxts_enc6x_steal
  2488. b Lxts_enc6x_done
  2489. .align 4
  2490. Lxts_enc6x_four:
  2491. vxor $out0,$in2,$twk0
  2492. vxor $out1,$in3,$twk1
  2493. vxor $out2,$in4,$twk2
  2494. vxor $out3,$in5,$twk3
  2495. vxor $out4,$out4,$out4
  2496. bl _aesp8_xts_enc5x
  2497. le?vperm $out0,$out0,$out0,$leperm
  2498. vmr $twk0,$twk4 # unused tweak
  2499. le?vperm $out1,$out1,$out1,$leperm
  2500. stvx_u $out0,$x00,$out # store output
  2501. le?vperm $out2,$out2,$out2,$leperm
  2502. stvx_u $out1,$x10,$out
  2503. vxor $tmp,$out3,$twk4 # last block prep for stealing
  2504. le?vperm $out3,$out3,$out3,$leperm
  2505. stvx_u $out2,$x20,$out
  2506. stvx_u $out3,$x30,$out
  2507. addi $out,$out,0x40
  2508. bne Lxts_enc6x_steal
  2509. b Lxts_enc6x_done
  2510. .align 4
  2511. Lxts_enc6x_three:
  2512. vxor $out0,$in3,$twk0
  2513. vxor $out1,$in4,$twk1
  2514. vxor $out2,$in5,$twk2
  2515. vxor $out3,$out3,$out3
  2516. vxor $out4,$out4,$out4
  2517. bl _aesp8_xts_enc5x
  2518. le?vperm $out0,$out0,$out0,$leperm
  2519. vmr $twk0,$twk3 # unused tweak
  2520. le?vperm $out1,$out1,$out1,$leperm
  2521. stvx_u $out0,$x00,$out # store output
  2522. vxor $tmp,$out2,$twk3 # last block prep for stealing
  2523. le?vperm $out2,$out2,$out2,$leperm
  2524. stvx_u $out1,$x10,$out
  2525. stvx_u $out2,$x20,$out
  2526. addi $out,$out,0x30
  2527. bne Lxts_enc6x_steal
  2528. b Lxts_enc6x_done
  2529. .align 4
  2530. Lxts_enc6x_two:
  2531. vxor $out0,$in4,$twk0
  2532. vxor $out1,$in5,$twk1
  2533. vxor $out2,$out2,$out2
  2534. vxor $out3,$out3,$out3
  2535. vxor $out4,$out4,$out4
  2536. bl _aesp8_xts_enc5x
  2537. le?vperm $out0,$out0,$out0,$leperm
  2538. vmr $twk0,$twk2 # unused tweak
  2539. vxor $tmp,$out1,$twk2 # last block prep for stealing
  2540. le?vperm $out1,$out1,$out1,$leperm
  2541. stvx_u $out0,$x00,$out # store output
  2542. stvx_u $out1,$x10,$out
  2543. addi $out,$out,0x20
  2544. bne Lxts_enc6x_steal
  2545. b Lxts_enc6x_done
  2546. .align 4
  2547. Lxts_enc6x_one:
  2548. vxor $out0,$in5,$twk0
  2549. nop
  2550. Loop_xts_enc1x:
  2551. vcipher $out0,$out0,v24
  2552. lvx v24,$x20,$key_ # round[3]
  2553. addi $key_,$key_,0x20
  2554. vcipher $out0,$out0,v25
  2555. lvx v25,$x10,$key_ # round[4]
  2556. bdnz Loop_xts_enc1x
  2557. add $inp,$inp,$taillen
  2558. cmpwi $taillen,0
  2559. vcipher $out0,$out0,v24
  2560. subi $inp,$inp,16
  2561. vcipher $out0,$out0,v25
  2562. lvsr $inpperm,0,$taillen
  2563. vcipher $out0,$out0,v26
  2564. lvx_u $in0,0,$inp
  2565. vcipher $out0,$out0,v27
  2566. addi $key_,$sp,$FRAME+15 # rewind $key_
  2567. vcipher $out0,$out0,v28
  2568. lvx v24,$x00,$key_ # re-pre-load round[1]
  2569. vcipher $out0,$out0,v29
  2570. lvx v25,$x10,$key_ # re-pre-load round[2]
  2571. vxor $twk0,$twk0,v31
  2572. le?vperm $in0,$in0,$in0,$leperm
  2573. vcipher $out0,$out0,v30
  2574. vperm $in0,$in0,$in0,$inpperm
  2575. vcipherlast $out0,$out0,$twk0
  2576. vmr $twk0,$twk1 # unused tweak
  2577. vxor $tmp,$out0,$twk1 # last block prep for stealing
  2578. le?vperm $out0,$out0,$out0,$leperm
  2579. stvx_u $out0,$x00,$out # store output
  2580. addi $out,$out,0x10
  2581. bne Lxts_enc6x_steal
  2582. b Lxts_enc6x_done
  2583. .align 4
  2584. Lxts_enc6x_zero:
  2585. cmpwi $taillen,0
  2586. beq Lxts_enc6x_done
  2587. add $inp,$inp,$taillen
  2588. subi $inp,$inp,16
  2589. lvx_u $in0,0,$inp
  2590. lvsr $inpperm,0,$taillen # $in5 is no more
  2591. le?vperm $in0,$in0,$in0,$leperm
  2592. vperm $in0,$in0,$in0,$inpperm
  2593. vxor $tmp,$tmp,$twk0
  2594. Lxts_enc6x_steal:
  2595. vxor $in0,$in0,$twk0
  2596. vxor $out0,$out0,$out0
  2597. vspltisb $out1,-1
  2598. vperm $out0,$out0,$out1,$inpperm
  2599. vsel $out0,$in0,$tmp,$out0 # $tmp is last block, remember?
  2600. subi r30,$out,17
  2601. subi $out,$out,16
  2602. mtctr $taillen
  2603. Loop_xts_enc6x_steal:
  2604. lbzu r0,1(r30)
  2605. stb r0,16(r30)
  2606. bdnz Loop_xts_enc6x_steal
  2607. li $taillen,0
  2608. mtctr $rounds
  2609. b Loop_xts_enc1x # one more time...
  2610. .align 4
  2611. Lxts_enc6x_done:
  2612. ${UCMP}i $ivp,0
  2613. beq Lxts_enc6x_ret
  2614. vxor $tweak,$twk0,$rndkey0
  2615. le?vperm $tweak,$tweak,$tweak,$leperm
  2616. stvx_u $tweak,0,$ivp
  2617. Lxts_enc6x_ret:
  2618. mtlr r11
  2619. li r10,`$FRAME+15`
  2620. li r11,`$FRAME+31`
  2621. stvx $seven,r10,$sp # wipe copies of round keys
  2622. addi r10,r10,32
  2623. stvx $seven,r11,$sp
  2624. addi r11,r11,32
  2625. stvx $seven,r10,$sp
  2626. addi r10,r10,32
  2627. stvx $seven,r11,$sp
  2628. addi r11,r11,32
  2629. stvx $seven,r10,$sp
  2630. addi r10,r10,32
  2631. stvx $seven,r11,$sp
  2632. addi r11,r11,32
  2633. stvx $seven,r10,$sp
  2634. addi r10,r10,32
  2635. stvx $seven,r11,$sp
  2636. addi r11,r11,32
  2637. mtspr 256,$vrsave
  2638. lvx v20,r10,$sp # ABI says so
  2639. addi r10,r10,32
  2640. lvx v21,r11,$sp
  2641. addi r11,r11,32
  2642. lvx v22,r10,$sp
  2643. addi r10,r10,32
  2644. lvx v23,r11,$sp
  2645. addi r11,r11,32
  2646. lvx v24,r10,$sp
  2647. addi r10,r10,32
  2648. lvx v25,r11,$sp
  2649. addi r11,r11,32
  2650. lvx v26,r10,$sp
  2651. addi r10,r10,32
  2652. lvx v27,r11,$sp
  2653. addi r11,r11,32
  2654. lvx v28,r10,$sp
  2655. addi r10,r10,32
  2656. lvx v29,r11,$sp
  2657. addi r11,r11,32
  2658. lvx v30,r10,$sp
  2659. lvx v31,r11,$sp
  2660. $POP r26,`$FRAME+21*16+0*$SIZE_T`($sp)
  2661. $POP r27,`$FRAME+21*16+1*$SIZE_T`($sp)
  2662. $POP r28,`$FRAME+21*16+2*$SIZE_T`($sp)
  2663. $POP r29,`$FRAME+21*16+3*$SIZE_T`($sp)
  2664. $POP r30,`$FRAME+21*16+4*$SIZE_T`($sp)
  2665. $POP r31,`$FRAME+21*16+5*$SIZE_T`($sp)
  2666. addi $sp,$sp,`$FRAME+21*16+6*$SIZE_T`
  2667. blr
  2668. .long 0
  2669. .byte 0,12,0x04,1,0x80,6,6,0
  2670. .long 0
  2671. .align 5
  2672. _aesp8_xts_enc5x:
  2673. vcipher $out0,$out0,v24
  2674. vcipher $out1,$out1,v24
  2675. vcipher $out2,$out2,v24
  2676. vcipher $out3,$out3,v24
  2677. vcipher $out4,$out4,v24
  2678. lvx v24,$x20,$key_ # round[3]
  2679. addi $key_,$key_,0x20
  2680. vcipher $out0,$out0,v25
  2681. vcipher $out1,$out1,v25
  2682. vcipher $out2,$out2,v25
  2683. vcipher $out3,$out3,v25
  2684. vcipher $out4,$out4,v25
  2685. lvx v25,$x10,$key_ # round[4]
  2686. bdnz _aesp8_xts_enc5x
  2687. add $inp,$inp,$taillen
  2688. cmpwi $taillen,0
  2689. vcipher $out0,$out0,v24
  2690. vcipher $out1,$out1,v24
  2691. vcipher $out2,$out2,v24
  2692. vcipher $out3,$out3,v24
  2693. vcipher $out4,$out4,v24
  2694. subi $inp,$inp,16
  2695. vcipher $out0,$out0,v25
  2696. vcipher $out1,$out1,v25
  2697. vcipher $out2,$out2,v25
  2698. vcipher $out3,$out3,v25
  2699. vcipher $out4,$out4,v25
  2700. vxor $twk0,$twk0,v31
  2701. vcipher $out0,$out0,v26
  2702. lvsr $inpperm,0,$taillen # $in5 is no more
  2703. vcipher $out1,$out1,v26
  2704. vcipher $out2,$out2,v26
  2705. vcipher $out3,$out3,v26
  2706. vcipher $out4,$out4,v26
  2707. vxor $in1,$twk1,v31
  2708. vcipher $out0,$out0,v27
  2709. lvx_u $in0,0,$inp
  2710. vcipher $out1,$out1,v27
  2711. vcipher $out2,$out2,v27
  2712. vcipher $out3,$out3,v27
  2713. vcipher $out4,$out4,v27
  2714. vxor $in2,$twk2,v31
  2715. addi $key_,$sp,$FRAME+15 # rewind $key_
  2716. vcipher $out0,$out0,v28
  2717. vcipher $out1,$out1,v28
  2718. vcipher $out2,$out2,v28
  2719. vcipher $out3,$out3,v28
  2720. vcipher $out4,$out4,v28
  2721. lvx v24,$x00,$key_ # re-pre-load round[1]
  2722. vxor $in3,$twk3,v31
  2723. vcipher $out0,$out0,v29
  2724. le?vperm $in0,$in0,$in0,$leperm
  2725. vcipher $out1,$out1,v29
  2726. vcipher $out2,$out2,v29
  2727. vcipher $out3,$out3,v29
  2728. vcipher $out4,$out4,v29
  2729. lvx v25,$x10,$key_ # re-pre-load round[2]
  2730. vxor $in4,$twk4,v31
  2731. vcipher $out0,$out0,v30
  2732. vperm $in0,$in0,$in0,$inpperm
  2733. vcipher $out1,$out1,v30
  2734. vcipher $out2,$out2,v30
  2735. vcipher $out3,$out3,v30
  2736. vcipher $out4,$out4,v30
  2737. vcipherlast $out0,$out0,$twk0
  2738. vcipherlast $out1,$out1,$in1
  2739. vcipherlast $out2,$out2,$in2
  2740. vcipherlast $out3,$out3,$in3
  2741. vcipherlast $out4,$out4,$in4
  2742. blr
  2743. .long 0
  2744. .byte 0,12,0x14,0,0,0,0,0
  2745. .align 5
  2746. _aesp8_xts_decrypt6x:
  2747. $STU $sp,-`($FRAME+21*16+6*$SIZE_T)`($sp)
  2748. mflr r11
  2749. li r7,`$FRAME+8*16+15`
  2750. li r3,`$FRAME+8*16+31`
  2751. $PUSH r11,`$FRAME+21*16+6*$SIZE_T+$LRSAVE`($sp)
  2752. stvx v20,r7,$sp # ABI says so
  2753. addi r7,r7,32
  2754. stvx v21,r3,$sp
  2755. addi r3,r3,32
  2756. stvx v22,r7,$sp
  2757. addi r7,r7,32
  2758. stvx v23,r3,$sp
  2759. addi r3,r3,32
  2760. stvx v24,r7,$sp
  2761. addi r7,r7,32
  2762. stvx v25,r3,$sp
  2763. addi r3,r3,32
  2764. stvx v26,r7,$sp
  2765. addi r7,r7,32
  2766. stvx v27,r3,$sp
  2767. addi r3,r3,32
  2768. stvx v28,r7,$sp
  2769. addi r7,r7,32
  2770. stvx v29,r3,$sp
  2771. addi r3,r3,32
  2772. stvx v30,r7,$sp
  2773. stvx v31,r3,$sp
  2774. li r0,-1
  2775. stw $vrsave,`$FRAME+21*16-4`($sp) # save vrsave
  2776. li $x10,0x10
  2777. $PUSH r26,`$FRAME+21*16+0*$SIZE_T`($sp)
  2778. li $x20,0x20
  2779. $PUSH r27,`$FRAME+21*16+1*$SIZE_T`($sp)
  2780. li $x30,0x30
  2781. $PUSH r28,`$FRAME+21*16+2*$SIZE_T`($sp)
  2782. li $x40,0x40
  2783. $PUSH r29,`$FRAME+21*16+3*$SIZE_T`($sp)
  2784. li $x50,0x50
  2785. $PUSH r30,`$FRAME+21*16+4*$SIZE_T`($sp)
  2786. li $x60,0x60
  2787. $PUSH r31,`$FRAME+21*16+5*$SIZE_T`($sp)
  2788. li $x70,0x70
  2789. mtspr 256,r0
  2790. subi $rounds,$rounds,3 # -4 in total
  2791. lvx $rndkey0,$x00,$key1 # load key schedule
  2792. lvx v30,$x10,$key1
  2793. addi $key1,$key1,0x20
  2794. lvx v31,$x00,$key1
  2795. ?vperm $rndkey0,$rndkey0,v30,$keyperm
  2796. addi $key_,$sp,$FRAME+15
  2797. mtctr $rounds
  2798. Load_xts_dec_key:
  2799. ?vperm v24,v30,v31,$keyperm
  2800. lvx v30,$x10,$key1
  2801. addi $key1,$key1,0x20
  2802. stvx v24,$x00,$key_ # off-load round[1]
  2803. ?vperm v25,v31,v30,$keyperm
  2804. lvx v31,$x00,$key1
  2805. stvx v25,$x10,$key_ # off-load round[2]
  2806. addi $key_,$key_,0x20
  2807. bdnz Load_xts_dec_key
  2808. lvx v26,$x10,$key1
  2809. ?vperm v24,v30,v31,$keyperm
  2810. lvx v27,$x20,$key1
  2811. stvx v24,$x00,$key_ # off-load round[3]
  2812. ?vperm v25,v31,v26,$keyperm
  2813. lvx v28,$x30,$key1
  2814. stvx v25,$x10,$key_ # off-load round[4]
  2815. addi $key_,$sp,$FRAME+15 # rewind $key_
  2816. ?vperm v26,v26,v27,$keyperm
  2817. lvx v29,$x40,$key1
  2818. ?vperm v27,v27,v28,$keyperm
  2819. lvx v30,$x50,$key1
  2820. ?vperm v28,v28,v29,$keyperm
  2821. lvx v31,$x60,$key1
  2822. ?vperm v29,v29,v30,$keyperm
  2823. lvx $twk5,$x70,$key1 # borrow $twk5
  2824. ?vperm v30,v30,v31,$keyperm
  2825. lvx v24,$x00,$key_ # pre-load round[1]
  2826. ?vperm v31,v31,$twk5,$keyperm
  2827. lvx v25,$x10,$key_ # pre-load round[2]
  2828. vperm $in0,$inout,$inptail,$inpperm
  2829. subi $inp,$inp,31 # undo "caller"
  2830. vxor $twk0,$tweak,$rndkey0
  2831. vsrab $tmp,$tweak,$seven # next tweak value
  2832. vaddubm $tweak,$tweak,$tweak
  2833. vsldoi $tmp,$tmp,$tmp,15
  2834. vand $tmp,$tmp,$eighty7
  2835. vxor $out0,$in0,$twk0
  2836. vxor $tweak,$tweak,$tmp
  2837. lvx_u $in1,$x10,$inp
  2838. vxor $twk1,$tweak,$rndkey0
  2839. vsrab $tmp,$tweak,$seven # next tweak value
  2840. vaddubm $tweak,$tweak,$tweak
  2841. vsldoi $tmp,$tmp,$tmp,15
  2842. le?vperm $in1,$in1,$in1,$leperm
  2843. vand $tmp,$tmp,$eighty7
  2844. vxor $out1,$in1,$twk1
  2845. vxor $tweak,$tweak,$tmp
  2846. lvx_u $in2,$x20,$inp
  2847. andi. $taillen,$len,15
  2848. vxor $twk2,$tweak,$rndkey0
  2849. vsrab $tmp,$tweak,$seven # next tweak value
  2850. vaddubm $tweak,$tweak,$tweak
  2851. vsldoi $tmp,$tmp,$tmp,15
  2852. le?vperm $in2,$in2,$in2,$leperm
  2853. vand $tmp,$tmp,$eighty7
  2854. vxor $out2,$in2,$twk2
  2855. vxor $tweak,$tweak,$tmp
  2856. lvx_u $in3,$x30,$inp
  2857. sub $len,$len,$taillen
  2858. vxor $twk3,$tweak,$rndkey0
  2859. vsrab $tmp,$tweak,$seven # next tweak value
  2860. vaddubm $tweak,$tweak,$tweak
  2861. vsldoi $tmp,$tmp,$tmp,15
  2862. le?vperm $in3,$in3,$in3,$leperm
  2863. vand $tmp,$tmp,$eighty7
  2864. vxor $out3,$in3,$twk3
  2865. vxor $tweak,$tweak,$tmp
  2866. lvx_u $in4,$x40,$inp
  2867. subi $len,$len,0x60
  2868. vxor $twk4,$tweak,$rndkey0
  2869. vsrab $tmp,$tweak,$seven # next tweak value
  2870. vaddubm $tweak,$tweak,$tweak
  2871. vsldoi $tmp,$tmp,$tmp,15
  2872. le?vperm $in4,$in4,$in4,$leperm
  2873. vand $tmp,$tmp,$eighty7
  2874. vxor $out4,$in4,$twk4
  2875. vxor $tweak,$tweak,$tmp
  2876. lvx_u $in5,$x50,$inp
  2877. addi $inp,$inp,0x60
  2878. vxor $twk5,$tweak,$rndkey0
  2879. vsrab $tmp,$tweak,$seven # next tweak value
  2880. vaddubm $tweak,$tweak,$tweak
  2881. vsldoi $tmp,$tmp,$tmp,15
  2882. le?vperm $in5,$in5,$in5,$leperm
  2883. vand $tmp,$tmp,$eighty7
  2884. vxor $out5,$in5,$twk5
  2885. vxor $tweak,$tweak,$tmp
  2886. vxor v31,v31,$rndkey0
  2887. mtctr $rounds
  2888. b Loop_xts_dec6x
  2889. .align 5
  2890. Loop_xts_dec6x:
  2891. vncipher $out0,$out0,v24
  2892. vncipher $out1,$out1,v24
  2893. vncipher $out2,$out2,v24
  2894. vncipher $out3,$out3,v24
  2895. vncipher $out4,$out4,v24
  2896. vncipher $out5,$out5,v24
  2897. lvx v24,$x20,$key_ # round[3]
  2898. addi $key_,$key_,0x20
  2899. vncipher $out0,$out0,v25
  2900. vncipher $out1,$out1,v25
  2901. vncipher $out2,$out2,v25
  2902. vncipher $out3,$out3,v25
  2903. vncipher $out4,$out4,v25
  2904. vncipher $out5,$out5,v25
  2905. lvx v25,$x10,$key_ # round[4]
  2906. bdnz Loop_xts_dec6x
  2907. subic $len,$len,96 # $len-=96
  2908. vxor $in0,$twk0,v31 # xor with last round key
  2909. vncipher $out0,$out0,v24
  2910. vncipher $out1,$out1,v24
  2911. vsrab $tmp,$tweak,$seven # next tweak value
  2912. vxor $twk0,$tweak,$rndkey0
  2913. vaddubm $tweak,$tweak,$tweak
  2914. vncipher $out2,$out2,v24
  2915. vncipher $out3,$out3,v24
  2916. vsldoi $tmp,$tmp,$tmp,15
  2917. vncipher $out4,$out4,v24
  2918. vncipher $out5,$out5,v24
  2919. subfe. r0,r0,r0 # borrow?-1:0
  2920. vand $tmp,$tmp,$eighty7
  2921. vncipher $out0,$out0,v25
  2922. vncipher $out1,$out1,v25
  2923. vxor $tweak,$tweak,$tmp
  2924. vncipher $out2,$out2,v25
  2925. vncipher $out3,$out3,v25
  2926. vxor $in1,$twk1,v31
  2927. vsrab $tmp,$tweak,$seven # next tweak value
  2928. vxor $twk1,$tweak,$rndkey0
  2929. vncipher $out4,$out4,v25
  2930. vncipher $out5,$out5,v25
  2931. and r0,r0,$len
  2932. vaddubm $tweak,$tweak,$tweak
  2933. vsldoi $tmp,$tmp,$tmp,15
  2934. vncipher $out0,$out0,v26
  2935. vncipher $out1,$out1,v26
  2936. vand $tmp,$tmp,$eighty7
  2937. vncipher $out2,$out2,v26
  2938. vncipher $out3,$out3,v26
  2939. vxor $tweak,$tweak,$tmp
  2940. vncipher $out4,$out4,v26
  2941. vncipher $out5,$out5,v26
  2942. add $inp,$inp,r0 # $inp is adjusted in such
  2943. # way that at exit from the
  2944. # loop inX-in5 are loaded
  2945. # with last "words"
  2946. vxor $in2,$twk2,v31
  2947. vsrab $tmp,$tweak,$seven # next tweak value
  2948. vxor $twk2,$tweak,$rndkey0
  2949. vaddubm $tweak,$tweak,$tweak
  2950. vncipher $out0,$out0,v27
  2951. vncipher $out1,$out1,v27
  2952. vsldoi $tmp,$tmp,$tmp,15
  2953. vncipher $out2,$out2,v27
  2954. vncipher $out3,$out3,v27
  2955. vand $tmp,$tmp,$eighty7
  2956. vncipher $out4,$out4,v27
  2957. vncipher $out5,$out5,v27
  2958. addi $key_,$sp,$FRAME+15 # rewind $key_
  2959. vxor $tweak,$tweak,$tmp
  2960. vncipher $out0,$out0,v28
  2961. vncipher $out1,$out1,v28
  2962. vxor $in3,$twk3,v31
  2963. vsrab $tmp,$tweak,$seven # next tweak value
  2964. vxor $twk3,$tweak,$rndkey0
  2965. vncipher $out2,$out2,v28
  2966. vncipher $out3,$out3,v28
  2967. vaddubm $tweak,$tweak,$tweak
  2968. vsldoi $tmp,$tmp,$tmp,15
  2969. vncipher $out4,$out4,v28
  2970. vncipher $out5,$out5,v28
  2971. lvx v24,$x00,$key_ # re-pre-load round[1]
  2972. vand $tmp,$tmp,$eighty7
  2973. vncipher $out0,$out0,v29
  2974. vncipher $out1,$out1,v29
  2975. vxor $tweak,$tweak,$tmp
  2976. vncipher $out2,$out2,v29
  2977. vncipher $out3,$out3,v29
  2978. vxor $in4,$twk4,v31
  2979. vsrab $tmp,$tweak,$seven # next tweak value
  2980. vxor $twk4,$tweak,$rndkey0
  2981. vncipher $out4,$out4,v29
  2982. vncipher $out5,$out5,v29
  2983. lvx v25,$x10,$key_ # re-pre-load round[2]
  2984. vaddubm $tweak,$tweak,$tweak
  2985. vsldoi $tmp,$tmp,$tmp,15
  2986. vncipher $out0,$out0,v30
  2987. vncipher $out1,$out1,v30
  2988. vand $tmp,$tmp,$eighty7
  2989. vncipher $out2,$out2,v30
  2990. vncipher $out3,$out3,v30
  2991. vxor $tweak,$tweak,$tmp
  2992. vncipher $out4,$out4,v30
  2993. vncipher $out5,$out5,v30
  2994. vxor $in5,$twk5,v31
  2995. vsrab $tmp,$tweak,$seven # next tweak value
  2996. vxor $twk5,$tweak,$rndkey0
  2997. vncipherlast $out0,$out0,$in0
  2998. lvx_u $in0,$x00,$inp # load next input block
  2999. vaddubm $tweak,$tweak,$tweak
  3000. vsldoi $tmp,$tmp,$tmp,15
  3001. vncipherlast $out1,$out1,$in1
  3002. lvx_u $in1,$x10,$inp
  3003. vncipherlast $out2,$out2,$in2
  3004. le?vperm $in0,$in0,$in0,$leperm
  3005. lvx_u $in2,$x20,$inp
  3006. vand $tmp,$tmp,$eighty7
  3007. vncipherlast $out3,$out3,$in3
  3008. le?vperm $in1,$in1,$in1,$leperm
  3009. lvx_u $in3,$x30,$inp
  3010. vncipherlast $out4,$out4,$in4
  3011. le?vperm $in2,$in2,$in2,$leperm
  3012. lvx_u $in4,$x40,$inp
  3013. vxor $tweak,$tweak,$tmp
  3014. vncipherlast $out5,$out5,$in5
  3015. le?vperm $in3,$in3,$in3,$leperm
  3016. lvx_u $in5,$x50,$inp
  3017. addi $inp,$inp,0x60
  3018. le?vperm $in4,$in4,$in4,$leperm
  3019. le?vperm $in5,$in5,$in5,$leperm
  3020. le?vperm $out0,$out0,$out0,$leperm
  3021. le?vperm $out1,$out1,$out1,$leperm
  3022. stvx_u $out0,$x00,$out # store output
  3023. vxor $out0,$in0,$twk0
  3024. le?vperm $out2,$out2,$out2,$leperm
  3025. stvx_u $out1,$x10,$out
  3026. vxor $out1,$in1,$twk1
  3027. le?vperm $out3,$out3,$out3,$leperm
  3028. stvx_u $out2,$x20,$out
  3029. vxor $out2,$in2,$twk2
  3030. le?vperm $out4,$out4,$out4,$leperm
  3031. stvx_u $out3,$x30,$out
  3032. vxor $out3,$in3,$twk3
  3033. le?vperm $out5,$out5,$out5,$leperm
  3034. stvx_u $out4,$x40,$out
  3035. vxor $out4,$in4,$twk4
  3036. stvx_u $out5,$x50,$out
  3037. vxor $out5,$in5,$twk5
  3038. addi $out,$out,0x60
  3039. mtctr $rounds
  3040. beq Loop_xts_dec6x # did $len-=96 borrow?
  3041. addic. $len,$len,0x60
  3042. beq Lxts_dec6x_zero
  3043. cmpwi $len,0x20
  3044. blt Lxts_dec6x_one
  3045. nop
  3046. beq Lxts_dec6x_two
  3047. cmpwi $len,0x40
  3048. blt Lxts_dec6x_three
  3049. nop
  3050. beq Lxts_dec6x_four
  3051. Lxts_dec6x_five:
  3052. vxor $out0,$in1,$twk0
  3053. vxor $out1,$in2,$twk1
  3054. vxor $out2,$in3,$twk2
  3055. vxor $out3,$in4,$twk3
  3056. vxor $out4,$in5,$twk4
  3057. bl _aesp8_xts_dec5x
  3058. le?vperm $out0,$out0,$out0,$leperm
  3059. vmr $twk0,$twk5 # unused tweak
  3060. vxor $twk1,$tweak,$rndkey0
  3061. le?vperm $out1,$out1,$out1,$leperm
  3062. stvx_u $out0,$x00,$out # store output
  3063. vxor $out0,$in0,$twk1
  3064. le?vperm $out2,$out2,$out2,$leperm
  3065. stvx_u $out1,$x10,$out
  3066. le?vperm $out3,$out3,$out3,$leperm
  3067. stvx_u $out2,$x20,$out
  3068. le?vperm $out4,$out4,$out4,$leperm
  3069. stvx_u $out3,$x30,$out
  3070. stvx_u $out4,$x40,$out
  3071. addi $out,$out,0x50
  3072. bne Lxts_dec6x_steal
  3073. b Lxts_dec6x_done
  3074. .align 4
  3075. Lxts_dec6x_four:
  3076. vxor $out0,$in2,$twk0
  3077. vxor $out1,$in3,$twk1
  3078. vxor $out2,$in4,$twk2
  3079. vxor $out3,$in5,$twk3
  3080. vxor $out4,$out4,$out4
  3081. bl _aesp8_xts_dec5x
  3082. le?vperm $out0,$out0,$out0,$leperm
  3083. vmr $twk0,$twk4 # unused tweak
  3084. vmr $twk1,$twk5
  3085. le?vperm $out1,$out1,$out1,$leperm
  3086. stvx_u $out0,$x00,$out # store output
  3087. vxor $out0,$in0,$twk5
  3088. le?vperm $out2,$out2,$out2,$leperm
  3089. stvx_u $out1,$x10,$out
  3090. le?vperm $out3,$out3,$out3,$leperm
  3091. stvx_u $out2,$x20,$out
  3092. stvx_u $out3,$x30,$out
  3093. addi $out,$out,0x40
  3094. bne Lxts_dec6x_steal
  3095. b Lxts_dec6x_done
  3096. .align 4
  3097. Lxts_dec6x_three:
  3098. vxor $out0,$in3,$twk0
  3099. vxor $out1,$in4,$twk1
  3100. vxor $out2,$in5,$twk2
  3101. vxor $out3,$out3,$out3
  3102. vxor $out4,$out4,$out4
  3103. bl _aesp8_xts_dec5x
  3104. le?vperm $out0,$out0,$out0,$leperm
  3105. vmr $twk0,$twk3 # unused tweak
  3106. vmr $twk1,$twk4
  3107. le?vperm $out1,$out1,$out1,$leperm
  3108. stvx_u $out0,$x00,$out # store output
  3109. vxor $out0,$in0,$twk4
  3110. le?vperm $out2,$out2,$out2,$leperm
  3111. stvx_u $out1,$x10,$out
  3112. stvx_u $out2,$x20,$out
  3113. addi $out,$out,0x30
  3114. bne Lxts_dec6x_steal
  3115. b Lxts_dec6x_done
  3116. .align 4
  3117. Lxts_dec6x_two:
  3118. vxor $out0,$in4,$twk0
  3119. vxor $out1,$in5,$twk1
  3120. vxor $out2,$out2,$out2
  3121. vxor $out3,$out3,$out3
  3122. vxor $out4,$out4,$out4
  3123. bl _aesp8_xts_dec5x
  3124. le?vperm $out0,$out0,$out0,$leperm
  3125. vmr $twk0,$twk2 # unused tweak
  3126. vmr $twk1,$twk3
  3127. le?vperm $out1,$out1,$out1,$leperm
  3128. stvx_u $out0,$x00,$out # store output
  3129. vxor $out0,$in0,$twk3
  3130. stvx_u $out1,$x10,$out
  3131. addi $out,$out,0x20
  3132. bne Lxts_dec6x_steal
  3133. b Lxts_dec6x_done
  3134. .align 4
  3135. Lxts_dec6x_one:
  3136. vxor $out0,$in5,$twk0
  3137. nop
  3138. Loop_xts_dec1x:
  3139. vncipher $out0,$out0,v24
  3140. lvx v24,$x20,$key_ # round[3]
  3141. addi $key_,$key_,0x20
  3142. vncipher $out0,$out0,v25
  3143. lvx v25,$x10,$key_ # round[4]
  3144. bdnz Loop_xts_dec1x
  3145. subi r0,$taillen,1
  3146. vncipher $out0,$out0,v24
  3147. andi. r0,r0,16
  3148. cmpwi $taillen,0
  3149. vncipher $out0,$out0,v25
  3150. sub $inp,$inp,r0
  3151. vncipher $out0,$out0,v26
  3152. lvx_u $in0,0,$inp
  3153. vncipher $out0,$out0,v27
  3154. addi $key_,$sp,$FRAME+15 # rewind $key_
  3155. vncipher $out0,$out0,v28
  3156. lvx v24,$x00,$key_ # re-pre-load round[1]
  3157. vncipher $out0,$out0,v29
  3158. lvx v25,$x10,$key_ # re-pre-load round[2]
  3159. vxor $twk0,$twk0,v31
  3160. le?vperm $in0,$in0,$in0,$leperm
  3161. vncipher $out0,$out0,v30
  3162. mtctr $rounds
  3163. vncipherlast $out0,$out0,$twk0
  3164. vmr $twk0,$twk1 # unused tweak
  3165. vmr $twk1,$twk2
  3166. le?vperm $out0,$out0,$out0,$leperm
  3167. stvx_u $out0,$x00,$out # store output
  3168. addi $out,$out,0x10
  3169. vxor $out0,$in0,$twk2
  3170. bne Lxts_dec6x_steal
  3171. b Lxts_dec6x_done
  3172. .align 4
  3173. Lxts_dec6x_zero:
  3174. cmpwi $taillen,0
  3175. beq Lxts_dec6x_done
  3176. lvx_u $in0,0,$inp
  3177. le?vperm $in0,$in0,$in0,$leperm
  3178. vxor $out0,$in0,$twk1
  3179. Lxts_dec6x_steal:
  3180. vncipher $out0,$out0,v24
  3181. lvx v24,$x20,$key_ # round[3]
  3182. addi $key_,$key_,0x20
  3183. vncipher $out0,$out0,v25
  3184. lvx v25,$x10,$key_ # round[4]
  3185. bdnz Lxts_dec6x_steal
  3186. add $inp,$inp,$taillen
  3187. vncipher $out0,$out0,v24
  3188. cmpwi $taillen,0
  3189. vncipher $out0,$out0,v25
  3190. lvx_u $in0,0,$inp
  3191. vncipher $out0,$out0,v26
  3192. lvsr $inpperm,0,$taillen # $in5 is no more
  3193. vncipher $out0,$out0,v27
  3194. addi $key_,$sp,$FRAME+15 # rewind $key_
  3195. vncipher $out0,$out0,v28
  3196. lvx v24,$x00,$key_ # re-pre-load round[1]
  3197. vncipher $out0,$out0,v29
  3198. lvx v25,$x10,$key_ # re-pre-load round[2]
  3199. vxor $twk1,$twk1,v31
  3200. le?vperm $in0,$in0,$in0,$leperm
  3201. vncipher $out0,$out0,v30
  3202. vperm $in0,$in0,$in0,$inpperm
  3203. vncipherlast $tmp,$out0,$twk1
  3204. le?vperm $out0,$tmp,$tmp,$leperm
  3205. le?stvx_u $out0,0,$out
  3206. be?stvx_u $tmp,0,$out
  3207. vxor $out0,$out0,$out0
  3208. vspltisb $out1,-1
  3209. vperm $out0,$out0,$out1,$inpperm
  3210. vsel $out0,$in0,$tmp,$out0
  3211. vxor $out0,$out0,$twk0
  3212. subi r30,$out,1
  3213. mtctr $taillen
  3214. Loop_xts_dec6x_steal:
  3215. lbzu r0,1(r30)
  3216. stb r0,16(r30)
  3217. bdnz Loop_xts_dec6x_steal
  3218. li $taillen,0
  3219. mtctr $rounds
  3220. b Loop_xts_dec1x # one more time...
  3221. .align 4
  3222. Lxts_dec6x_done:
  3223. ${UCMP}i $ivp,0
  3224. beq Lxts_dec6x_ret
  3225. vxor $tweak,$twk0,$rndkey0
  3226. le?vperm $tweak,$tweak,$tweak,$leperm
  3227. stvx_u $tweak,0,$ivp
  3228. Lxts_dec6x_ret:
  3229. mtlr r11
  3230. li r10,`$FRAME+15`
  3231. li r11,`$FRAME+31`
  3232. stvx $seven,r10,$sp # wipe copies of round keys
  3233. addi r10,r10,32
  3234. stvx $seven,r11,$sp
  3235. addi r11,r11,32
  3236. stvx $seven,r10,$sp
  3237. addi r10,r10,32
  3238. stvx $seven,r11,$sp
  3239. addi r11,r11,32
  3240. stvx $seven,r10,$sp
  3241. addi r10,r10,32
  3242. stvx $seven,r11,$sp
  3243. addi r11,r11,32
  3244. stvx $seven,r10,$sp
  3245. addi r10,r10,32
  3246. stvx $seven,r11,$sp
  3247. addi r11,r11,32
  3248. mtspr 256,$vrsave
  3249. lvx v20,r10,$sp # ABI says so
  3250. addi r10,r10,32
  3251. lvx v21,r11,$sp
  3252. addi r11,r11,32
  3253. lvx v22,r10,$sp
  3254. addi r10,r10,32
  3255. lvx v23,r11,$sp
  3256. addi r11,r11,32
  3257. lvx v24,r10,$sp
  3258. addi r10,r10,32
  3259. lvx v25,r11,$sp
  3260. addi r11,r11,32
  3261. lvx v26,r10,$sp
  3262. addi r10,r10,32
  3263. lvx v27,r11,$sp
  3264. addi r11,r11,32
  3265. lvx v28,r10,$sp
  3266. addi r10,r10,32
  3267. lvx v29,r11,$sp
  3268. addi r11,r11,32
  3269. lvx v30,r10,$sp
  3270. lvx v31,r11,$sp
  3271. $POP r26,`$FRAME+21*16+0*$SIZE_T`($sp)
  3272. $POP r27,`$FRAME+21*16+1*$SIZE_T`($sp)
  3273. $POP r28,`$FRAME+21*16+2*$SIZE_T`($sp)
  3274. $POP r29,`$FRAME+21*16+3*$SIZE_T`($sp)
  3275. $POP r30,`$FRAME+21*16+4*$SIZE_T`($sp)
  3276. $POP r31,`$FRAME+21*16+5*$SIZE_T`($sp)
  3277. addi $sp,$sp,`$FRAME+21*16+6*$SIZE_T`
  3278. blr
  3279. .long 0
  3280. .byte 0,12,0x04,1,0x80,6,6,0
  3281. .long 0
  3282. .align 5
  3283. _aesp8_xts_dec5x:
  3284. vncipher $out0,$out0,v24
  3285. vncipher $out1,$out1,v24
  3286. vncipher $out2,$out2,v24
  3287. vncipher $out3,$out3,v24
  3288. vncipher $out4,$out4,v24
  3289. lvx v24,$x20,$key_ # round[3]
  3290. addi $key_,$key_,0x20
  3291. vncipher $out0,$out0,v25
  3292. vncipher $out1,$out1,v25
  3293. vncipher $out2,$out2,v25
  3294. vncipher $out3,$out3,v25
  3295. vncipher $out4,$out4,v25
  3296. lvx v25,$x10,$key_ # round[4]
  3297. bdnz _aesp8_xts_dec5x
  3298. subi r0,$taillen,1
  3299. vncipher $out0,$out0,v24
  3300. vncipher $out1,$out1,v24
  3301. vncipher $out2,$out2,v24
  3302. vncipher $out3,$out3,v24
  3303. vncipher $out4,$out4,v24
  3304. andi. r0,r0,16
  3305. cmpwi $taillen,0
  3306. vncipher $out0,$out0,v25
  3307. vncipher $out1,$out1,v25
  3308. vncipher $out2,$out2,v25
  3309. vncipher $out3,$out3,v25
  3310. vncipher $out4,$out4,v25
  3311. vxor $twk0,$twk0,v31
  3312. sub $inp,$inp,r0
  3313. vncipher $out0,$out0,v26
  3314. vncipher $out1,$out1,v26
  3315. vncipher $out2,$out2,v26
  3316. vncipher $out3,$out3,v26
  3317. vncipher $out4,$out4,v26
  3318. vxor $in1,$twk1,v31
  3319. vncipher $out0,$out0,v27
  3320. lvx_u $in0,0,$inp
  3321. vncipher $out1,$out1,v27
  3322. vncipher $out2,$out2,v27
  3323. vncipher $out3,$out3,v27
  3324. vncipher $out4,$out4,v27
  3325. vxor $in2,$twk2,v31
  3326. addi $key_,$sp,$FRAME+15 # rewind $key_
  3327. vncipher $out0,$out0,v28
  3328. vncipher $out1,$out1,v28
  3329. vncipher $out2,$out2,v28
  3330. vncipher $out3,$out3,v28
  3331. vncipher $out4,$out4,v28
  3332. lvx v24,$x00,$key_ # re-pre-load round[1]
  3333. vxor $in3,$twk3,v31
  3334. vncipher $out0,$out0,v29
  3335. le?vperm $in0,$in0,$in0,$leperm
  3336. vncipher $out1,$out1,v29
  3337. vncipher $out2,$out2,v29
  3338. vncipher $out3,$out3,v29
  3339. vncipher $out4,$out4,v29
  3340. lvx v25,$x10,$key_ # re-pre-load round[2]
  3341. vxor $in4,$twk4,v31
  3342. vncipher $out0,$out0,v30
  3343. vncipher $out1,$out1,v30
  3344. vncipher $out2,$out2,v30
  3345. vncipher $out3,$out3,v30
  3346. vncipher $out4,$out4,v30
  3347. vncipherlast $out0,$out0,$twk0
  3348. vncipherlast $out1,$out1,$in1
  3349. vncipherlast $out2,$out2,$in2
  3350. vncipherlast $out3,$out3,$in3
  3351. vncipherlast $out4,$out4,$in4
  3352. mtctr $rounds
  3353. blr
  3354. .long 0
  3355. .byte 0,12,0x14,0,0,0,0,0
  3356. ___
  3357. }} }}}
  3358. my $consts=1;
  3359. foreach(split("\n",$code)) {
  3360. s/\`([^\`]*)\`/eval($1)/geo;
  3361. # constants table endian-specific conversion
  3362. if ($consts && m/\.(long|byte)\s+(.+)\s+(\?[a-z]*)$/o) {
  3363. my $conv=$3;
  3364. my @bytes=();
  3365. # convert to endian-agnostic format
  3366. if ($1 eq "long") {
  3367. foreach (split(/,\s*/,$2)) {
  3368. my $l = /^0/?oct:int;
  3369. push @bytes,($l>>24)&0xff,($l>>16)&0xff,($l>>8)&0xff,$l&0xff;
  3370. }
  3371. } else {
  3372. @bytes = map(/^0/?oct:int,split(/,\s*/,$2));
  3373. }
  3374. # little-endian conversion
  3375. if ($flavour =~ /le$/o) {
  3376. SWITCH: for($conv) {
  3377. /\?inv/ && do { @bytes=map($_^0xf,@bytes); last; };
  3378. /\?rev/ && do { @bytes=reverse(@bytes); last; };
  3379. }
  3380. }
  3381. #emit
  3382. print ".byte\t",join(',',map (sprintf("0x%02x",$_),@bytes)),"\n";
  3383. next;
  3384. }
  3385. $consts=0 if (m/Lconsts:/o); # end of table
  3386. # instructions prefixed with '?' are endian-specific and need
  3387. # to be adjusted accordingly...
  3388. if ($flavour =~ /le$/o) { # little-endian
  3389. s/le\?//o or
  3390. s/be\?/#be#/o or
  3391. s/\?lvsr/lvsl/o or
  3392. s/\?lvsl/lvsr/o or
  3393. s/\?(vperm\s+v[0-9]+,\s*)(v[0-9]+,\s*)(v[0-9]+,\s*)(v[0-9]+)/$1$3$2$4/o or
  3394. s/\?(vsldoi\s+v[0-9]+,\s*)(v[0-9]+,)\s*(v[0-9]+,\s*)([0-9]+)/$1$3$2 16-$4/o or
  3395. s/\?(vspltw\s+v[0-9]+,\s*)(v[0-9]+,)\s*([0-9])/$1$2 3-$3/o;
  3396. } else { # big-endian
  3397. s/le\?/#le#/o or
  3398. s/be\?//o or
  3399. s/\?([a-z]+)/$1/o;
  3400. }
  3401. print $_,"\n";
  3402. }
  3403. close STDOUT or die "error closing STDOUT: $!";