cipher.c 135 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943
  1. /*
  2. * Copyright 2016 Broadcom
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License, version 2, as
  6. * published by the Free Software Foundation (the "GPL").
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License version 2 (GPLv2) for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * version 2 (GPLv2) along with this source code.
  15. */
  16. #include <linux/err.h>
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/errno.h>
  20. #include <linux/kernel.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/scatterlist.h>
  24. #include <linux/crypto.h>
  25. #include <linux/kthread.h>
  26. #include <linux/rtnetlink.h>
  27. #include <linux/sched.h>
  28. #include <linux/of_address.h>
  29. #include <linux/of_device.h>
  30. #include <linux/io.h>
  31. #include <linux/bitops.h>
  32. #include <crypto/algapi.h>
  33. #include <crypto/aead.h>
  34. #include <crypto/internal/aead.h>
  35. #include <crypto/aes.h>
  36. #include <crypto/des.h>
  37. #include <crypto/hmac.h>
  38. #include <crypto/sha.h>
  39. #include <crypto/md5.h>
  40. #include <crypto/authenc.h>
  41. #include <crypto/skcipher.h>
  42. #include <crypto/hash.h>
  43. #include <crypto/sha3.h>
  44. #include "util.h"
  45. #include "cipher.h"
  46. #include "spu.h"
  47. #include "spum.h"
  48. #include "spu2.h"
  49. /* ================= Device Structure ================== */
  50. struct device_private iproc_priv;
  51. /* ==================== Parameters ===================== */
  52. int flow_debug_logging;
  53. module_param(flow_debug_logging, int, 0644);
  54. MODULE_PARM_DESC(flow_debug_logging, "Enable Flow Debug Logging");
  55. int packet_debug_logging;
  56. module_param(packet_debug_logging, int, 0644);
  57. MODULE_PARM_DESC(packet_debug_logging, "Enable Packet Debug Logging");
  58. int debug_logging_sleep;
  59. module_param(debug_logging_sleep, int, 0644);
  60. MODULE_PARM_DESC(debug_logging_sleep, "Packet Debug Logging Sleep");
  61. /*
  62. * The value of these module parameters is used to set the priority for each
  63. * algo type when this driver registers algos with the kernel crypto API.
  64. * To use a priority other than the default, set the priority in the insmod or
  65. * modprobe. Changing the module priority after init time has no effect.
  66. *
  67. * The default priorities are chosen to be lower (less preferred) than ARMv8 CE
  68. * algos, but more preferred than generic software algos.
  69. */
  70. static int cipher_pri = 150;
  71. module_param(cipher_pri, int, 0644);
  72. MODULE_PARM_DESC(cipher_pri, "Priority for cipher algos");
  73. static int hash_pri = 100;
  74. module_param(hash_pri, int, 0644);
  75. MODULE_PARM_DESC(hash_pri, "Priority for hash algos");
  76. static int aead_pri = 150;
  77. module_param(aead_pri, int, 0644);
  78. MODULE_PARM_DESC(aead_pri, "Priority for AEAD algos");
  79. /* A type 3 BCM header, expected to precede the SPU header for SPU-M.
  80. * Bits 3 and 4 in the first byte encode the channel number (the dma ringset).
  81. * 0x60 - ring 0
  82. * 0x68 - ring 1
  83. * 0x70 - ring 2
  84. * 0x78 - ring 3
  85. */
  86. char BCMHEADER[] = { 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28 };
  87. /*
  88. * Some SPU hw does not use BCM header on SPU messages. So BCM_HDR_LEN
  89. * is set dynamically after reading SPU type from device tree.
  90. */
  91. #define BCM_HDR_LEN iproc_priv.bcm_hdr_len
  92. /* min and max time to sleep before retrying when mbox queue is full. usec */
  93. #define MBOX_SLEEP_MIN 800
  94. #define MBOX_SLEEP_MAX 1000
  95. /**
  96. * select_channel() - Select a SPU channel to handle a crypto request. Selects
  97. * channel in round robin order.
  98. *
  99. * Return: channel index
  100. */
  101. static u8 select_channel(void)
  102. {
  103. u8 chan_idx = atomic_inc_return(&iproc_priv.next_chan);
  104. return chan_idx % iproc_priv.spu.num_chan;
  105. }
  106. /**
  107. * spu_ablkcipher_rx_sg_create() - Build up the scatterlist of buffers used to
  108. * receive a SPU response message for an ablkcipher request. Includes buffers to
  109. * catch SPU message headers and the response data.
  110. * @mssg: mailbox message containing the receive sg
  111. * @rctx: crypto request context
  112. * @rx_frag_num: number of scatterlist elements required to hold the
  113. * SPU response message
  114. * @chunksize: Number of bytes of response data expected
  115. * @stat_pad_len: Number of bytes required to pad the STAT field to
  116. * a 4-byte boundary
  117. *
  118. * The scatterlist that gets allocated here is freed in spu_chunk_cleanup()
  119. * when the request completes, whether the request is handled successfully or
  120. * there is an error.
  121. *
  122. * Returns:
  123. * 0 if successful
  124. * < 0 if an error
  125. */
  126. static int
  127. spu_ablkcipher_rx_sg_create(struct brcm_message *mssg,
  128. struct iproc_reqctx_s *rctx,
  129. u8 rx_frag_num,
  130. unsigned int chunksize, u32 stat_pad_len)
  131. {
  132. struct spu_hw *spu = &iproc_priv.spu;
  133. struct scatterlist *sg; /* used to build sgs in mbox message */
  134. struct iproc_ctx_s *ctx = rctx->ctx;
  135. u32 datalen; /* Number of bytes of response data expected */
  136. mssg->spu.dst = kcalloc(rx_frag_num, sizeof(struct scatterlist),
  137. rctx->gfp);
  138. if (!mssg->spu.dst)
  139. return -ENOMEM;
  140. sg = mssg->spu.dst;
  141. sg_init_table(sg, rx_frag_num);
  142. /* Space for SPU message header */
  143. sg_set_buf(sg++, rctx->msg_buf.spu_resp_hdr, ctx->spu_resp_hdr_len);
  144. /* If XTS tweak in payload, add buffer to receive encrypted tweak */
  145. if ((ctx->cipher.mode == CIPHER_MODE_XTS) &&
  146. spu->spu_xts_tweak_in_payload())
  147. sg_set_buf(sg++, rctx->msg_buf.c.supdt_tweak,
  148. SPU_XTS_TWEAK_SIZE);
  149. /* Copy in each dst sg entry from request, up to chunksize */
  150. datalen = spu_msg_sg_add(&sg, &rctx->dst_sg, &rctx->dst_skip,
  151. rctx->dst_nents, chunksize);
  152. if (datalen < chunksize) {
  153. pr_err("%s(): failed to copy dst sg to mbox msg. chunksize %u, datalen %u",
  154. __func__, chunksize, datalen);
  155. return -EFAULT;
  156. }
  157. if (ctx->cipher.alg == CIPHER_ALG_RC4)
  158. /* Add buffer to catch 260-byte SUPDT field for RC4 */
  159. sg_set_buf(sg++, rctx->msg_buf.c.supdt_tweak, SPU_SUPDT_LEN);
  160. if (stat_pad_len)
  161. sg_set_buf(sg++, rctx->msg_buf.rx_stat_pad, stat_pad_len);
  162. memset(rctx->msg_buf.rx_stat, 0, SPU_RX_STATUS_LEN);
  163. sg_set_buf(sg, rctx->msg_buf.rx_stat, spu->spu_rx_status_len());
  164. return 0;
  165. }
  166. /**
  167. * spu_ablkcipher_tx_sg_create() - Build up the scatterlist of buffers used to
  168. * send a SPU request message for an ablkcipher request. Includes SPU message
  169. * headers and the request data.
  170. * @mssg: mailbox message containing the transmit sg
  171. * @rctx: crypto request context
  172. * @tx_frag_num: number of scatterlist elements required to construct the
  173. * SPU request message
  174. * @chunksize: Number of bytes of request data
  175. * @pad_len: Number of pad bytes
  176. *
  177. * The scatterlist that gets allocated here is freed in spu_chunk_cleanup()
  178. * when the request completes, whether the request is handled successfully or
  179. * there is an error.
  180. *
  181. * Returns:
  182. * 0 if successful
  183. * < 0 if an error
  184. */
  185. static int
  186. spu_ablkcipher_tx_sg_create(struct brcm_message *mssg,
  187. struct iproc_reqctx_s *rctx,
  188. u8 tx_frag_num, unsigned int chunksize, u32 pad_len)
  189. {
  190. struct spu_hw *spu = &iproc_priv.spu;
  191. struct scatterlist *sg; /* used to build sgs in mbox message */
  192. struct iproc_ctx_s *ctx = rctx->ctx;
  193. u32 datalen; /* Number of bytes of response data expected */
  194. u32 stat_len;
  195. mssg->spu.src = kcalloc(tx_frag_num, sizeof(struct scatterlist),
  196. rctx->gfp);
  197. if (unlikely(!mssg->spu.src))
  198. return -ENOMEM;
  199. sg = mssg->spu.src;
  200. sg_init_table(sg, tx_frag_num);
  201. sg_set_buf(sg++, rctx->msg_buf.bcm_spu_req_hdr,
  202. BCM_HDR_LEN + ctx->spu_req_hdr_len);
  203. /* if XTS tweak in payload, copy from IV (where crypto API puts it) */
  204. if ((ctx->cipher.mode == CIPHER_MODE_XTS) &&
  205. spu->spu_xts_tweak_in_payload())
  206. sg_set_buf(sg++, rctx->msg_buf.iv_ctr, SPU_XTS_TWEAK_SIZE);
  207. /* Copy in each src sg entry from request, up to chunksize */
  208. datalen = spu_msg_sg_add(&sg, &rctx->src_sg, &rctx->src_skip,
  209. rctx->src_nents, chunksize);
  210. if (unlikely(datalen < chunksize)) {
  211. pr_err("%s(): failed to copy src sg to mbox msg",
  212. __func__);
  213. return -EFAULT;
  214. }
  215. if (pad_len)
  216. sg_set_buf(sg++, rctx->msg_buf.spu_req_pad, pad_len);
  217. stat_len = spu->spu_tx_status_len();
  218. if (stat_len) {
  219. memset(rctx->msg_buf.tx_stat, 0, stat_len);
  220. sg_set_buf(sg, rctx->msg_buf.tx_stat, stat_len);
  221. }
  222. return 0;
  223. }
  224. static int mailbox_send_message(struct brcm_message *mssg, u32 flags,
  225. u8 chan_idx)
  226. {
  227. int err;
  228. int retry_cnt = 0;
  229. struct device *dev = &(iproc_priv.pdev->dev);
  230. err = mbox_send_message(iproc_priv.mbox[chan_idx], mssg);
  231. if (flags & CRYPTO_TFM_REQ_MAY_SLEEP) {
  232. while ((err == -ENOBUFS) && (retry_cnt < SPU_MB_RETRY_MAX)) {
  233. /*
  234. * Mailbox queue is full. Since MAY_SLEEP is set, assume
  235. * not in atomic context and we can wait and try again.
  236. */
  237. retry_cnt++;
  238. usleep_range(MBOX_SLEEP_MIN, MBOX_SLEEP_MAX);
  239. err = mbox_send_message(iproc_priv.mbox[chan_idx],
  240. mssg);
  241. atomic_inc(&iproc_priv.mb_no_spc);
  242. }
  243. }
  244. if (err < 0) {
  245. atomic_inc(&iproc_priv.mb_send_fail);
  246. return err;
  247. }
  248. /* Check error returned by mailbox controller */
  249. err = mssg->error;
  250. if (unlikely(err < 0)) {
  251. dev_err(dev, "message error %d", err);
  252. /* Signal txdone for mailbox channel */
  253. }
  254. /* Signal txdone for mailbox channel */
  255. mbox_client_txdone(iproc_priv.mbox[chan_idx], err);
  256. return err;
  257. }
  258. /**
  259. * handle_ablkcipher_req() - Submit as much of a block cipher request as fits in
  260. * a single SPU request message, starting at the current position in the request
  261. * data.
  262. * @rctx: Crypto request context
  263. *
  264. * This may be called on the crypto API thread, or, when a request is so large
  265. * it must be broken into multiple SPU messages, on the thread used to invoke
  266. * the response callback. When requests are broken into multiple SPU
  267. * messages, we assume subsequent messages depend on previous results, and
  268. * thus always wait for previous results before submitting the next message.
  269. * Because requests are submitted in lock step like this, there is no need
  270. * to synchronize access to request data structures.
  271. *
  272. * Return: -EINPROGRESS: request has been accepted and result will be returned
  273. * asynchronously
  274. * Any other value indicates an error
  275. */
  276. static int handle_ablkcipher_req(struct iproc_reqctx_s *rctx)
  277. {
  278. struct spu_hw *spu = &iproc_priv.spu;
  279. struct crypto_async_request *areq = rctx->parent;
  280. struct ablkcipher_request *req =
  281. container_of(areq, struct ablkcipher_request, base);
  282. struct iproc_ctx_s *ctx = rctx->ctx;
  283. struct spu_cipher_parms cipher_parms;
  284. int err = 0;
  285. unsigned int chunksize = 0; /* Num bytes of request to submit */
  286. int remaining = 0; /* Bytes of request still to process */
  287. int chunk_start; /* Beginning of data for current SPU msg */
  288. /* IV or ctr value to use in this SPU msg */
  289. u8 local_iv_ctr[MAX_IV_SIZE];
  290. u32 stat_pad_len; /* num bytes to align status field */
  291. u32 pad_len; /* total length of all padding */
  292. bool update_key = false;
  293. struct brcm_message *mssg; /* mailbox message */
  294. /* number of entries in src and dst sg in mailbox message. */
  295. u8 rx_frag_num = 2; /* response header and STATUS */
  296. u8 tx_frag_num = 1; /* request header */
  297. flow_log("%s\n", __func__);
  298. cipher_parms.alg = ctx->cipher.alg;
  299. cipher_parms.mode = ctx->cipher.mode;
  300. cipher_parms.type = ctx->cipher_type;
  301. cipher_parms.key_len = ctx->enckeylen;
  302. cipher_parms.key_buf = ctx->enckey;
  303. cipher_parms.iv_buf = local_iv_ctr;
  304. cipher_parms.iv_len = rctx->iv_ctr_len;
  305. mssg = &rctx->mb_mssg;
  306. chunk_start = rctx->src_sent;
  307. remaining = rctx->total_todo - chunk_start;
  308. /* determine the chunk we are breaking off and update the indexes */
  309. if ((ctx->max_payload != SPU_MAX_PAYLOAD_INF) &&
  310. (remaining > ctx->max_payload))
  311. chunksize = ctx->max_payload;
  312. else
  313. chunksize = remaining;
  314. rctx->src_sent += chunksize;
  315. rctx->total_sent = rctx->src_sent;
  316. /* Count number of sg entries to be included in this request */
  317. rctx->src_nents = spu_sg_count(rctx->src_sg, rctx->src_skip, chunksize);
  318. rctx->dst_nents = spu_sg_count(rctx->dst_sg, rctx->dst_skip, chunksize);
  319. if ((ctx->cipher.mode == CIPHER_MODE_CBC) &&
  320. rctx->is_encrypt && chunk_start)
  321. /*
  322. * Encrypting non-first first chunk. Copy last block of
  323. * previous result to IV for this chunk.
  324. */
  325. sg_copy_part_to_buf(req->dst, rctx->msg_buf.iv_ctr,
  326. rctx->iv_ctr_len,
  327. chunk_start - rctx->iv_ctr_len);
  328. if (rctx->iv_ctr_len) {
  329. /* get our local copy of the iv */
  330. __builtin_memcpy(local_iv_ctr, rctx->msg_buf.iv_ctr,
  331. rctx->iv_ctr_len);
  332. /* generate the next IV if possible */
  333. if ((ctx->cipher.mode == CIPHER_MODE_CBC) &&
  334. !rctx->is_encrypt) {
  335. /*
  336. * CBC Decrypt: next IV is the last ciphertext block in
  337. * this chunk
  338. */
  339. sg_copy_part_to_buf(req->src, rctx->msg_buf.iv_ctr,
  340. rctx->iv_ctr_len,
  341. rctx->src_sent - rctx->iv_ctr_len);
  342. } else if (ctx->cipher.mode == CIPHER_MODE_CTR) {
  343. /*
  344. * The SPU hardware increments the counter once for
  345. * each AES block of 16 bytes. So update the counter
  346. * for the next chunk, if there is one. Note that for
  347. * this chunk, the counter has already been copied to
  348. * local_iv_ctr. We can assume a block size of 16,
  349. * because we only support CTR mode for AES, not for
  350. * any other cipher alg.
  351. */
  352. add_to_ctr(rctx->msg_buf.iv_ctr, chunksize >> 4);
  353. }
  354. }
  355. if (ctx->cipher.alg == CIPHER_ALG_RC4) {
  356. rx_frag_num++;
  357. if (chunk_start) {
  358. /*
  359. * for non-first RC4 chunks, use SUPDT from previous
  360. * response as key for this chunk.
  361. */
  362. cipher_parms.key_buf = rctx->msg_buf.c.supdt_tweak;
  363. update_key = true;
  364. cipher_parms.type = CIPHER_TYPE_UPDT;
  365. } else if (!rctx->is_encrypt) {
  366. /*
  367. * First RC4 chunk. For decrypt, key in pre-built msg
  368. * header may have been changed if encrypt required
  369. * multiple chunks. So revert the key to the
  370. * ctx->enckey value.
  371. */
  372. update_key = true;
  373. cipher_parms.type = CIPHER_TYPE_INIT;
  374. }
  375. }
  376. if (ctx->max_payload == SPU_MAX_PAYLOAD_INF)
  377. flow_log("max_payload infinite\n");
  378. else
  379. flow_log("max_payload %u\n", ctx->max_payload);
  380. flow_log("sent:%u start:%u remains:%u size:%u\n",
  381. rctx->src_sent, chunk_start, remaining, chunksize);
  382. /* Copy SPU header template created at setkey time */
  383. memcpy(rctx->msg_buf.bcm_spu_req_hdr, ctx->bcm_spu_req_hdr,
  384. sizeof(rctx->msg_buf.bcm_spu_req_hdr));
  385. /*
  386. * Pass SUPDT field as key. Key field in finish() call is only used
  387. * when update_key has been set above for RC4. Will be ignored in
  388. * all other cases.
  389. */
  390. spu->spu_cipher_req_finish(rctx->msg_buf.bcm_spu_req_hdr + BCM_HDR_LEN,
  391. ctx->spu_req_hdr_len, !(rctx->is_encrypt),
  392. &cipher_parms, update_key, chunksize);
  393. atomic64_add(chunksize, &iproc_priv.bytes_out);
  394. stat_pad_len = spu->spu_wordalign_padlen(chunksize);
  395. if (stat_pad_len)
  396. rx_frag_num++;
  397. pad_len = stat_pad_len;
  398. if (pad_len) {
  399. tx_frag_num++;
  400. spu->spu_request_pad(rctx->msg_buf.spu_req_pad, 0,
  401. 0, ctx->auth.alg, ctx->auth.mode,
  402. rctx->total_sent, stat_pad_len);
  403. }
  404. spu->spu_dump_msg_hdr(rctx->msg_buf.bcm_spu_req_hdr + BCM_HDR_LEN,
  405. ctx->spu_req_hdr_len);
  406. packet_log("payload:\n");
  407. dump_sg(rctx->src_sg, rctx->src_skip, chunksize);
  408. packet_dump(" pad: ", rctx->msg_buf.spu_req_pad, pad_len);
  409. /*
  410. * Build mailbox message containing SPU request msg and rx buffers
  411. * to catch response message
  412. */
  413. memset(mssg, 0, sizeof(*mssg));
  414. mssg->type = BRCM_MESSAGE_SPU;
  415. mssg->ctx = rctx; /* Will be returned in response */
  416. /* Create rx scatterlist to catch result */
  417. rx_frag_num += rctx->dst_nents;
  418. if ((ctx->cipher.mode == CIPHER_MODE_XTS) &&
  419. spu->spu_xts_tweak_in_payload())
  420. rx_frag_num++; /* extra sg to insert tweak */
  421. err = spu_ablkcipher_rx_sg_create(mssg, rctx, rx_frag_num, chunksize,
  422. stat_pad_len);
  423. if (err)
  424. return err;
  425. /* Create tx scatterlist containing SPU request message */
  426. tx_frag_num += rctx->src_nents;
  427. if (spu->spu_tx_status_len())
  428. tx_frag_num++;
  429. if ((ctx->cipher.mode == CIPHER_MODE_XTS) &&
  430. spu->spu_xts_tweak_in_payload())
  431. tx_frag_num++; /* extra sg to insert tweak */
  432. err = spu_ablkcipher_tx_sg_create(mssg, rctx, tx_frag_num, chunksize,
  433. pad_len);
  434. if (err)
  435. return err;
  436. err = mailbox_send_message(mssg, req->base.flags, rctx->chan_idx);
  437. if (unlikely(err < 0))
  438. return err;
  439. return -EINPROGRESS;
  440. }
  441. /**
  442. * handle_ablkcipher_resp() - Process a block cipher SPU response. Updates the
  443. * total received count for the request and updates global stats.
  444. * @rctx: Crypto request context
  445. */
  446. static void handle_ablkcipher_resp(struct iproc_reqctx_s *rctx)
  447. {
  448. struct spu_hw *spu = &iproc_priv.spu;
  449. #ifdef DEBUG
  450. struct crypto_async_request *areq = rctx->parent;
  451. struct ablkcipher_request *req = ablkcipher_request_cast(areq);
  452. #endif
  453. struct iproc_ctx_s *ctx = rctx->ctx;
  454. u32 payload_len;
  455. /* See how much data was returned */
  456. payload_len = spu->spu_payload_length(rctx->msg_buf.spu_resp_hdr);
  457. /*
  458. * In XTS mode, the first SPU_XTS_TWEAK_SIZE bytes may be the
  459. * encrypted tweak ("i") value; we don't count those.
  460. */
  461. if ((ctx->cipher.mode == CIPHER_MODE_XTS) &&
  462. spu->spu_xts_tweak_in_payload() &&
  463. (payload_len >= SPU_XTS_TWEAK_SIZE))
  464. payload_len -= SPU_XTS_TWEAK_SIZE;
  465. atomic64_add(payload_len, &iproc_priv.bytes_in);
  466. flow_log("%s() offset: %u, bd_len: %u BD:\n",
  467. __func__, rctx->total_received, payload_len);
  468. dump_sg(req->dst, rctx->total_received, payload_len);
  469. if (ctx->cipher.alg == CIPHER_ALG_RC4)
  470. packet_dump(" supdt ", rctx->msg_buf.c.supdt_tweak,
  471. SPU_SUPDT_LEN);
  472. rctx->total_received += payload_len;
  473. if (rctx->total_received == rctx->total_todo) {
  474. atomic_inc(&iproc_priv.op_counts[SPU_OP_CIPHER]);
  475. atomic_inc(
  476. &iproc_priv.cipher_cnt[ctx->cipher.alg][ctx->cipher.mode]);
  477. }
  478. }
  479. /**
  480. * spu_ahash_rx_sg_create() - Build up the scatterlist of buffers used to
  481. * receive a SPU response message for an ahash request.
  482. * @mssg: mailbox message containing the receive sg
  483. * @rctx: crypto request context
  484. * @rx_frag_num: number of scatterlist elements required to hold the
  485. * SPU response message
  486. * @digestsize: length of hash digest, in bytes
  487. * @stat_pad_len: Number of bytes required to pad the STAT field to
  488. * a 4-byte boundary
  489. *
  490. * The scatterlist that gets allocated here is freed in spu_chunk_cleanup()
  491. * when the request completes, whether the request is handled successfully or
  492. * there is an error.
  493. *
  494. * Return:
  495. * 0 if successful
  496. * < 0 if an error
  497. */
  498. static int
  499. spu_ahash_rx_sg_create(struct brcm_message *mssg,
  500. struct iproc_reqctx_s *rctx,
  501. u8 rx_frag_num, unsigned int digestsize,
  502. u32 stat_pad_len)
  503. {
  504. struct spu_hw *spu = &iproc_priv.spu;
  505. struct scatterlist *sg; /* used to build sgs in mbox message */
  506. struct iproc_ctx_s *ctx = rctx->ctx;
  507. mssg->spu.dst = kcalloc(rx_frag_num, sizeof(struct scatterlist),
  508. rctx->gfp);
  509. if (!mssg->spu.dst)
  510. return -ENOMEM;
  511. sg = mssg->spu.dst;
  512. sg_init_table(sg, rx_frag_num);
  513. /* Space for SPU message header */
  514. sg_set_buf(sg++, rctx->msg_buf.spu_resp_hdr, ctx->spu_resp_hdr_len);
  515. /* Space for digest */
  516. sg_set_buf(sg++, rctx->msg_buf.digest, digestsize);
  517. if (stat_pad_len)
  518. sg_set_buf(sg++, rctx->msg_buf.rx_stat_pad, stat_pad_len);
  519. memset(rctx->msg_buf.rx_stat, 0, SPU_RX_STATUS_LEN);
  520. sg_set_buf(sg, rctx->msg_buf.rx_stat, spu->spu_rx_status_len());
  521. return 0;
  522. }
  523. /**
  524. * spu_ahash_tx_sg_create() - Build up the scatterlist of buffers used to send
  525. * a SPU request message for an ahash request. Includes SPU message headers and
  526. * the request data.
  527. * @mssg: mailbox message containing the transmit sg
  528. * @rctx: crypto request context
  529. * @tx_frag_num: number of scatterlist elements required to construct the
  530. * SPU request message
  531. * @spu_hdr_len: length in bytes of SPU message header
  532. * @hash_carry_len: Number of bytes of data carried over from previous req
  533. * @new_data_len: Number of bytes of new request data
  534. * @pad_len: Number of pad bytes
  535. *
  536. * The scatterlist that gets allocated here is freed in spu_chunk_cleanup()
  537. * when the request completes, whether the request is handled successfully or
  538. * there is an error.
  539. *
  540. * Return:
  541. * 0 if successful
  542. * < 0 if an error
  543. */
  544. static int
  545. spu_ahash_tx_sg_create(struct brcm_message *mssg,
  546. struct iproc_reqctx_s *rctx,
  547. u8 tx_frag_num,
  548. u32 spu_hdr_len,
  549. unsigned int hash_carry_len,
  550. unsigned int new_data_len, u32 pad_len)
  551. {
  552. struct spu_hw *spu = &iproc_priv.spu;
  553. struct scatterlist *sg; /* used to build sgs in mbox message */
  554. u32 datalen; /* Number of bytes of response data expected */
  555. u32 stat_len;
  556. mssg->spu.src = kcalloc(tx_frag_num, sizeof(struct scatterlist),
  557. rctx->gfp);
  558. if (!mssg->spu.src)
  559. return -ENOMEM;
  560. sg = mssg->spu.src;
  561. sg_init_table(sg, tx_frag_num);
  562. sg_set_buf(sg++, rctx->msg_buf.bcm_spu_req_hdr,
  563. BCM_HDR_LEN + spu_hdr_len);
  564. if (hash_carry_len)
  565. sg_set_buf(sg++, rctx->hash_carry, hash_carry_len);
  566. if (new_data_len) {
  567. /* Copy in each src sg entry from request, up to chunksize */
  568. datalen = spu_msg_sg_add(&sg, &rctx->src_sg, &rctx->src_skip,
  569. rctx->src_nents, new_data_len);
  570. if (datalen < new_data_len) {
  571. pr_err("%s(): failed to copy src sg to mbox msg",
  572. __func__);
  573. return -EFAULT;
  574. }
  575. }
  576. if (pad_len)
  577. sg_set_buf(sg++, rctx->msg_buf.spu_req_pad, pad_len);
  578. stat_len = spu->spu_tx_status_len();
  579. if (stat_len) {
  580. memset(rctx->msg_buf.tx_stat, 0, stat_len);
  581. sg_set_buf(sg, rctx->msg_buf.tx_stat, stat_len);
  582. }
  583. return 0;
  584. }
  585. /**
  586. * handle_ahash_req() - Process an asynchronous hash request from the crypto
  587. * API.
  588. * @rctx: Crypto request context
  589. *
  590. * Builds a SPU request message embedded in a mailbox message and submits the
  591. * mailbox message on a selected mailbox channel. The SPU request message is
  592. * constructed as a scatterlist, including entries from the crypto API's
  593. * src scatterlist to avoid copying the data to be hashed. This function is
  594. * called either on the thread from the crypto API, or, in the case that the
  595. * crypto API request is too large to fit in a single SPU request message,
  596. * on the thread that invokes the receive callback with a response message.
  597. * Because some operations require the response from one chunk before the next
  598. * chunk can be submitted, we always wait for the response for the previous
  599. * chunk before submitting the next chunk. Because requests are submitted in
  600. * lock step like this, there is no need to synchronize access to request data
  601. * structures.
  602. *
  603. * Return:
  604. * -EINPROGRESS: request has been submitted to SPU and response will be
  605. * returned asynchronously
  606. * -EAGAIN: non-final request included a small amount of data, which for
  607. * efficiency we did not submit to the SPU, but instead stored
  608. * to be submitted to the SPU with the next part of the request
  609. * other: an error code
  610. */
  611. static int handle_ahash_req(struct iproc_reqctx_s *rctx)
  612. {
  613. struct spu_hw *spu = &iproc_priv.spu;
  614. struct crypto_async_request *areq = rctx->parent;
  615. struct ahash_request *req = ahash_request_cast(areq);
  616. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  617. struct crypto_tfm *tfm = crypto_ahash_tfm(ahash);
  618. unsigned int blocksize = crypto_tfm_alg_blocksize(tfm);
  619. struct iproc_ctx_s *ctx = rctx->ctx;
  620. /* number of bytes still to be hashed in this req */
  621. unsigned int nbytes_to_hash = 0;
  622. int err = 0;
  623. unsigned int chunksize = 0; /* length of hash carry + new data */
  624. /*
  625. * length of new data, not from hash carry, to be submitted in
  626. * this hw request
  627. */
  628. unsigned int new_data_len;
  629. unsigned int chunk_start = 0;
  630. u32 db_size; /* Length of data field, incl gcm and hash padding */
  631. int pad_len = 0; /* total pad len, including gcm, hash, stat padding */
  632. u32 data_pad_len = 0; /* length of GCM/CCM padding */
  633. u32 stat_pad_len = 0; /* length of padding to align STATUS word */
  634. struct brcm_message *mssg; /* mailbox message */
  635. struct spu_request_opts req_opts;
  636. struct spu_cipher_parms cipher_parms;
  637. struct spu_hash_parms hash_parms;
  638. struct spu_aead_parms aead_parms;
  639. unsigned int local_nbuf;
  640. u32 spu_hdr_len;
  641. unsigned int digestsize;
  642. u16 rem = 0;
  643. /*
  644. * number of entries in src and dst sg. Always includes SPU msg header.
  645. * rx always includes a buffer to catch digest and STATUS.
  646. */
  647. u8 rx_frag_num = 3;
  648. u8 tx_frag_num = 1;
  649. flow_log("total_todo %u, total_sent %u\n",
  650. rctx->total_todo, rctx->total_sent);
  651. memset(&req_opts, 0, sizeof(req_opts));
  652. memset(&cipher_parms, 0, sizeof(cipher_parms));
  653. memset(&hash_parms, 0, sizeof(hash_parms));
  654. memset(&aead_parms, 0, sizeof(aead_parms));
  655. req_opts.bd_suppress = true;
  656. hash_parms.alg = ctx->auth.alg;
  657. hash_parms.mode = ctx->auth.mode;
  658. hash_parms.type = HASH_TYPE_NONE;
  659. hash_parms.key_buf = (u8 *)ctx->authkey;
  660. hash_parms.key_len = ctx->authkeylen;
  661. /*
  662. * For hash algorithms below assignment looks bit odd but
  663. * it's needed for AES-XCBC and AES-CMAC hash algorithms
  664. * to differentiate between 128, 192, 256 bit key values.
  665. * Based on the key values, hash algorithm is selected.
  666. * For example for 128 bit key, hash algorithm is AES-128.
  667. */
  668. cipher_parms.type = ctx->cipher_type;
  669. mssg = &rctx->mb_mssg;
  670. chunk_start = rctx->src_sent;
  671. /*
  672. * Compute the amount remaining to hash. This may include data
  673. * carried over from previous requests.
  674. */
  675. nbytes_to_hash = rctx->total_todo - rctx->total_sent;
  676. chunksize = nbytes_to_hash;
  677. if ((ctx->max_payload != SPU_MAX_PAYLOAD_INF) &&
  678. (chunksize > ctx->max_payload))
  679. chunksize = ctx->max_payload;
  680. /*
  681. * If this is not a final request and the request data is not a multiple
  682. * of a full block, then simply park the extra data and prefix it to the
  683. * data for the next request.
  684. */
  685. if (!rctx->is_final) {
  686. u8 *dest = rctx->hash_carry + rctx->hash_carry_len;
  687. u16 new_len; /* len of data to add to hash carry */
  688. rem = chunksize % blocksize; /* remainder */
  689. if (rem) {
  690. /* chunksize not a multiple of blocksize */
  691. chunksize -= rem;
  692. if (chunksize == 0) {
  693. /* Don't have a full block to submit to hw */
  694. new_len = rem - rctx->hash_carry_len;
  695. sg_copy_part_to_buf(req->src, dest, new_len,
  696. rctx->src_sent);
  697. rctx->hash_carry_len = rem;
  698. flow_log("Exiting with hash carry len: %u\n",
  699. rctx->hash_carry_len);
  700. packet_dump(" buf: ",
  701. rctx->hash_carry,
  702. rctx->hash_carry_len);
  703. return -EAGAIN;
  704. }
  705. }
  706. }
  707. /* if we have hash carry, then prefix it to the data in this request */
  708. local_nbuf = rctx->hash_carry_len;
  709. rctx->hash_carry_len = 0;
  710. if (local_nbuf)
  711. tx_frag_num++;
  712. new_data_len = chunksize - local_nbuf;
  713. /* Count number of sg entries to be used in this request */
  714. rctx->src_nents = spu_sg_count(rctx->src_sg, rctx->src_skip,
  715. new_data_len);
  716. /* AES hashing keeps key size in type field, so need to copy it here */
  717. if (hash_parms.alg == HASH_ALG_AES)
  718. hash_parms.type = (enum hash_type)cipher_parms.type;
  719. else
  720. hash_parms.type = spu->spu_hash_type(rctx->total_sent);
  721. digestsize = spu->spu_digest_size(ctx->digestsize, ctx->auth.alg,
  722. hash_parms.type);
  723. hash_parms.digestsize = digestsize;
  724. /* update the indexes */
  725. rctx->total_sent += chunksize;
  726. /* if you sent a prebuf then that wasn't from this req->src */
  727. rctx->src_sent += new_data_len;
  728. if ((rctx->total_sent == rctx->total_todo) && rctx->is_final)
  729. hash_parms.pad_len = spu->spu_hash_pad_len(hash_parms.alg,
  730. hash_parms.mode,
  731. chunksize,
  732. blocksize);
  733. /*
  734. * If a non-first chunk, then include the digest returned from the
  735. * previous chunk so that hw can add to it (except for AES types).
  736. */
  737. if ((hash_parms.type == HASH_TYPE_UPDT) &&
  738. (hash_parms.alg != HASH_ALG_AES)) {
  739. hash_parms.key_buf = rctx->incr_hash;
  740. hash_parms.key_len = digestsize;
  741. }
  742. atomic64_add(chunksize, &iproc_priv.bytes_out);
  743. flow_log("%s() final: %u nbuf: %u ",
  744. __func__, rctx->is_final, local_nbuf);
  745. if (ctx->max_payload == SPU_MAX_PAYLOAD_INF)
  746. flow_log("max_payload infinite\n");
  747. else
  748. flow_log("max_payload %u\n", ctx->max_payload);
  749. flow_log("chunk_start: %u chunk_size: %u\n", chunk_start, chunksize);
  750. /* Prepend SPU header with type 3 BCM header */
  751. memcpy(rctx->msg_buf.bcm_spu_req_hdr, BCMHEADER, BCM_HDR_LEN);
  752. hash_parms.prebuf_len = local_nbuf;
  753. spu_hdr_len = spu->spu_create_request(rctx->msg_buf.bcm_spu_req_hdr +
  754. BCM_HDR_LEN,
  755. &req_opts, &cipher_parms,
  756. &hash_parms, &aead_parms,
  757. new_data_len);
  758. if (spu_hdr_len == 0) {
  759. pr_err("Failed to create SPU request header\n");
  760. return -EFAULT;
  761. }
  762. /*
  763. * Determine total length of padding required. Put all padding in one
  764. * buffer.
  765. */
  766. data_pad_len = spu->spu_gcm_ccm_pad_len(ctx->cipher.mode, chunksize);
  767. db_size = spu_real_db_size(0, 0, local_nbuf, new_data_len,
  768. 0, 0, hash_parms.pad_len);
  769. if (spu->spu_tx_status_len())
  770. stat_pad_len = spu->spu_wordalign_padlen(db_size);
  771. if (stat_pad_len)
  772. rx_frag_num++;
  773. pad_len = hash_parms.pad_len + data_pad_len + stat_pad_len;
  774. if (pad_len) {
  775. tx_frag_num++;
  776. spu->spu_request_pad(rctx->msg_buf.spu_req_pad, data_pad_len,
  777. hash_parms.pad_len, ctx->auth.alg,
  778. ctx->auth.mode, rctx->total_sent,
  779. stat_pad_len);
  780. }
  781. spu->spu_dump_msg_hdr(rctx->msg_buf.bcm_spu_req_hdr + BCM_HDR_LEN,
  782. spu_hdr_len);
  783. packet_dump(" prebuf: ", rctx->hash_carry, local_nbuf);
  784. flow_log("Data:\n");
  785. dump_sg(rctx->src_sg, rctx->src_skip, new_data_len);
  786. packet_dump(" pad: ", rctx->msg_buf.spu_req_pad, pad_len);
  787. /*
  788. * Build mailbox message containing SPU request msg and rx buffers
  789. * to catch response message
  790. */
  791. memset(mssg, 0, sizeof(*mssg));
  792. mssg->type = BRCM_MESSAGE_SPU;
  793. mssg->ctx = rctx; /* Will be returned in response */
  794. /* Create rx scatterlist to catch result */
  795. err = spu_ahash_rx_sg_create(mssg, rctx, rx_frag_num, digestsize,
  796. stat_pad_len);
  797. if (err)
  798. return err;
  799. /* Create tx scatterlist containing SPU request message */
  800. tx_frag_num += rctx->src_nents;
  801. if (spu->spu_tx_status_len())
  802. tx_frag_num++;
  803. err = spu_ahash_tx_sg_create(mssg, rctx, tx_frag_num, spu_hdr_len,
  804. local_nbuf, new_data_len, pad_len);
  805. if (err)
  806. return err;
  807. err = mailbox_send_message(mssg, req->base.flags, rctx->chan_idx);
  808. if (unlikely(err < 0))
  809. return err;
  810. return -EINPROGRESS;
  811. }
  812. /**
  813. * spu_hmac_outer_hash() - Request synchonous software compute of the outer hash
  814. * for an HMAC request.
  815. * @req: The HMAC request from the crypto API
  816. * @ctx: The session context
  817. *
  818. * Return: 0 if synchronous hash operation successful
  819. * -EINVAL if the hash algo is unrecognized
  820. * any other value indicates an error
  821. */
  822. static int spu_hmac_outer_hash(struct ahash_request *req,
  823. struct iproc_ctx_s *ctx)
  824. {
  825. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  826. unsigned int blocksize =
  827. crypto_tfm_alg_blocksize(crypto_ahash_tfm(ahash));
  828. int rc;
  829. switch (ctx->auth.alg) {
  830. case HASH_ALG_MD5:
  831. rc = do_shash("md5", req->result, ctx->opad, blocksize,
  832. req->result, ctx->digestsize, NULL, 0);
  833. break;
  834. case HASH_ALG_SHA1:
  835. rc = do_shash("sha1", req->result, ctx->opad, blocksize,
  836. req->result, ctx->digestsize, NULL, 0);
  837. break;
  838. case HASH_ALG_SHA224:
  839. rc = do_shash("sha224", req->result, ctx->opad, blocksize,
  840. req->result, ctx->digestsize, NULL, 0);
  841. break;
  842. case HASH_ALG_SHA256:
  843. rc = do_shash("sha256", req->result, ctx->opad, blocksize,
  844. req->result, ctx->digestsize, NULL, 0);
  845. break;
  846. case HASH_ALG_SHA384:
  847. rc = do_shash("sha384", req->result, ctx->opad, blocksize,
  848. req->result, ctx->digestsize, NULL, 0);
  849. break;
  850. case HASH_ALG_SHA512:
  851. rc = do_shash("sha512", req->result, ctx->opad, blocksize,
  852. req->result, ctx->digestsize, NULL, 0);
  853. break;
  854. default:
  855. pr_err("%s() Error : unknown hmac type\n", __func__);
  856. rc = -EINVAL;
  857. }
  858. return rc;
  859. }
  860. /**
  861. * ahash_req_done() - Process a hash result from the SPU hardware.
  862. * @rctx: Crypto request context
  863. *
  864. * Return: 0 if successful
  865. * < 0 if an error
  866. */
  867. static int ahash_req_done(struct iproc_reqctx_s *rctx)
  868. {
  869. struct spu_hw *spu = &iproc_priv.spu;
  870. struct crypto_async_request *areq = rctx->parent;
  871. struct ahash_request *req = ahash_request_cast(areq);
  872. struct iproc_ctx_s *ctx = rctx->ctx;
  873. int err;
  874. memcpy(req->result, rctx->msg_buf.digest, ctx->digestsize);
  875. if (spu->spu_type == SPU_TYPE_SPUM) {
  876. /* byte swap the output from the UPDT function to network byte
  877. * order
  878. */
  879. if (ctx->auth.alg == HASH_ALG_MD5) {
  880. __swab32s((u32 *)req->result);
  881. __swab32s(((u32 *)req->result) + 1);
  882. __swab32s(((u32 *)req->result) + 2);
  883. __swab32s(((u32 *)req->result) + 3);
  884. __swab32s(((u32 *)req->result) + 4);
  885. }
  886. }
  887. flow_dump(" digest ", req->result, ctx->digestsize);
  888. /* if this an HMAC then do the outer hash */
  889. if (rctx->is_sw_hmac) {
  890. err = spu_hmac_outer_hash(req, ctx);
  891. if (err < 0)
  892. return err;
  893. flow_dump(" hmac: ", req->result, ctx->digestsize);
  894. }
  895. if (rctx->is_sw_hmac || ctx->auth.mode == HASH_MODE_HMAC) {
  896. atomic_inc(&iproc_priv.op_counts[SPU_OP_HMAC]);
  897. atomic_inc(&iproc_priv.hmac_cnt[ctx->auth.alg]);
  898. } else {
  899. atomic_inc(&iproc_priv.op_counts[SPU_OP_HASH]);
  900. atomic_inc(&iproc_priv.hash_cnt[ctx->auth.alg]);
  901. }
  902. return 0;
  903. }
  904. /**
  905. * handle_ahash_resp() - Process a SPU response message for a hash request.
  906. * Checks if the entire crypto API request has been processed, and if so,
  907. * invokes post processing on the result.
  908. * @rctx: Crypto request context
  909. */
  910. static void handle_ahash_resp(struct iproc_reqctx_s *rctx)
  911. {
  912. struct iproc_ctx_s *ctx = rctx->ctx;
  913. #ifdef DEBUG
  914. struct crypto_async_request *areq = rctx->parent;
  915. struct ahash_request *req = ahash_request_cast(areq);
  916. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  917. unsigned int blocksize =
  918. crypto_tfm_alg_blocksize(crypto_ahash_tfm(ahash));
  919. #endif
  920. /*
  921. * Save hash to use as input to next op if incremental. Might be copying
  922. * too much, but that's easier than figuring out actual digest size here
  923. */
  924. memcpy(rctx->incr_hash, rctx->msg_buf.digest, MAX_DIGEST_SIZE);
  925. flow_log("%s() blocksize:%u digestsize:%u\n",
  926. __func__, blocksize, ctx->digestsize);
  927. atomic64_add(ctx->digestsize, &iproc_priv.bytes_in);
  928. if (rctx->is_final && (rctx->total_sent == rctx->total_todo))
  929. ahash_req_done(rctx);
  930. }
  931. /**
  932. * spu_aead_rx_sg_create() - Build up the scatterlist of buffers used to receive
  933. * a SPU response message for an AEAD request. Includes buffers to catch SPU
  934. * message headers and the response data.
  935. * @mssg: mailbox message containing the receive sg
  936. * @rctx: crypto request context
  937. * @rx_frag_num: number of scatterlist elements required to hold the
  938. * SPU response message
  939. * @assoc_len: Length of associated data included in the crypto request
  940. * @ret_iv_len: Length of IV returned in response
  941. * @resp_len: Number of bytes of response data expected to be written to
  942. * dst buffer from crypto API
  943. * @digestsize: Length of hash digest, in bytes
  944. * @stat_pad_len: Number of bytes required to pad the STAT field to
  945. * a 4-byte boundary
  946. *
  947. * The scatterlist that gets allocated here is freed in spu_chunk_cleanup()
  948. * when the request completes, whether the request is handled successfully or
  949. * there is an error.
  950. *
  951. * Returns:
  952. * 0 if successful
  953. * < 0 if an error
  954. */
  955. static int spu_aead_rx_sg_create(struct brcm_message *mssg,
  956. struct aead_request *req,
  957. struct iproc_reqctx_s *rctx,
  958. u8 rx_frag_num,
  959. unsigned int assoc_len,
  960. u32 ret_iv_len, unsigned int resp_len,
  961. unsigned int digestsize, u32 stat_pad_len)
  962. {
  963. struct spu_hw *spu = &iproc_priv.spu;
  964. struct scatterlist *sg; /* used to build sgs in mbox message */
  965. struct iproc_ctx_s *ctx = rctx->ctx;
  966. u32 datalen; /* Number of bytes of response data expected */
  967. u32 assoc_buf_len;
  968. u8 data_padlen = 0;
  969. if (ctx->is_rfc4543) {
  970. /* RFC4543: only pad after data, not after AAD */
  971. data_padlen = spu->spu_gcm_ccm_pad_len(ctx->cipher.mode,
  972. assoc_len + resp_len);
  973. assoc_buf_len = assoc_len;
  974. } else {
  975. data_padlen = spu->spu_gcm_ccm_pad_len(ctx->cipher.mode,
  976. resp_len);
  977. assoc_buf_len = spu->spu_assoc_resp_len(ctx->cipher.mode,
  978. assoc_len, ret_iv_len,
  979. rctx->is_encrypt);
  980. }
  981. if (ctx->cipher.mode == CIPHER_MODE_CCM)
  982. /* ICV (after data) must be in the next 32-bit word for CCM */
  983. data_padlen += spu->spu_wordalign_padlen(assoc_buf_len +
  984. resp_len +
  985. data_padlen);
  986. if (data_padlen)
  987. /* have to catch gcm pad in separate buffer */
  988. rx_frag_num++;
  989. mssg->spu.dst = kcalloc(rx_frag_num, sizeof(struct scatterlist),
  990. rctx->gfp);
  991. if (!mssg->spu.dst)
  992. return -ENOMEM;
  993. sg = mssg->spu.dst;
  994. sg_init_table(sg, rx_frag_num);
  995. /* Space for SPU message header */
  996. sg_set_buf(sg++, rctx->msg_buf.spu_resp_hdr, ctx->spu_resp_hdr_len);
  997. if (assoc_buf_len) {
  998. /*
  999. * Don't write directly to req->dst, because SPU may pad the
  1000. * assoc data in the response
  1001. */
  1002. memset(rctx->msg_buf.a.resp_aad, 0, assoc_buf_len);
  1003. sg_set_buf(sg++, rctx->msg_buf.a.resp_aad, assoc_buf_len);
  1004. }
  1005. if (resp_len) {
  1006. /*
  1007. * Copy in each dst sg entry from request, up to chunksize.
  1008. * dst sg catches just the data. digest caught in separate buf.
  1009. */
  1010. datalen = spu_msg_sg_add(&sg, &rctx->dst_sg, &rctx->dst_skip,
  1011. rctx->dst_nents, resp_len);
  1012. if (datalen < (resp_len)) {
  1013. pr_err("%s(): failed to copy dst sg to mbox msg. expected len %u, datalen %u",
  1014. __func__, resp_len, datalen);
  1015. return -EFAULT;
  1016. }
  1017. }
  1018. /* If GCM/CCM data is padded, catch padding in separate buffer */
  1019. if (data_padlen) {
  1020. memset(rctx->msg_buf.a.gcmpad, 0, data_padlen);
  1021. sg_set_buf(sg++, rctx->msg_buf.a.gcmpad, data_padlen);
  1022. }
  1023. /* Always catch ICV in separate buffer */
  1024. sg_set_buf(sg++, rctx->msg_buf.digest, digestsize);
  1025. flow_log("stat_pad_len %u\n", stat_pad_len);
  1026. if (stat_pad_len) {
  1027. memset(rctx->msg_buf.rx_stat_pad, 0, stat_pad_len);
  1028. sg_set_buf(sg++, rctx->msg_buf.rx_stat_pad, stat_pad_len);
  1029. }
  1030. memset(rctx->msg_buf.rx_stat, 0, SPU_RX_STATUS_LEN);
  1031. sg_set_buf(sg, rctx->msg_buf.rx_stat, spu->spu_rx_status_len());
  1032. return 0;
  1033. }
  1034. /**
  1035. * spu_aead_tx_sg_create() - Build up the scatterlist of buffers used to send a
  1036. * SPU request message for an AEAD request. Includes SPU message headers and the
  1037. * request data.
  1038. * @mssg: mailbox message containing the transmit sg
  1039. * @rctx: crypto request context
  1040. * @tx_frag_num: number of scatterlist elements required to construct the
  1041. * SPU request message
  1042. * @spu_hdr_len: length of SPU message header in bytes
  1043. * @assoc: crypto API associated data scatterlist
  1044. * @assoc_len: length of associated data
  1045. * @assoc_nents: number of scatterlist entries containing assoc data
  1046. * @aead_iv_len: length of AEAD IV, if included
  1047. * @chunksize: Number of bytes of request data
  1048. * @aad_pad_len: Number of bytes of padding at end of AAD. For GCM/CCM.
  1049. * @pad_len: Number of pad bytes
  1050. * @incl_icv: If true, write separate ICV buffer after data and
  1051. * any padding
  1052. *
  1053. * The scatterlist that gets allocated here is freed in spu_chunk_cleanup()
  1054. * when the request completes, whether the request is handled successfully or
  1055. * there is an error.
  1056. *
  1057. * Return:
  1058. * 0 if successful
  1059. * < 0 if an error
  1060. */
  1061. static int spu_aead_tx_sg_create(struct brcm_message *mssg,
  1062. struct iproc_reqctx_s *rctx,
  1063. u8 tx_frag_num,
  1064. u32 spu_hdr_len,
  1065. struct scatterlist *assoc,
  1066. unsigned int assoc_len,
  1067. int assoc_nents,
  1068. unsigned int aead_iv_len,
  1069. unsigned int chunksize,
  1070. u32 aad_pad_len, u32 pad_len, bool incl_icv)
  1071. {
  1072. struct spu_hw *spu = &iproc_priv.spu;
  1073. struct scatterlist *sg; /* used to build sgs in mbox message */
  1074. struct scatterlist *assoc_sg = assoc;
  1075. struct iproc_ctx_s *ctx = rctx->ctx;
  1076. u32 datalen; /* Number of bytes of data to write */
  1077. u32 written; /* Number of bytes of data written */
  1078. u32 assoc_offset = 0;
  1079. u32 stat_len;
  1080. mssg->spu.src = kcalloc(tx_frag_num, sizeof(struct scatterlist),
  1081. rctx->gfp);
  1082. if (!mssg->spu.src)
  1083. return -ENOMEM;
  1084. sg = mssg->spu.src;
  1085. sg_init_table(sg, tx_frag_num);
  1086. sg_set_buf(sg++, rctx->msg_buf.bcm_spu_req_hdr,
  1087. BCM_HDR_LEN + spu_hdr_len);
  1088. if (assoc_len) {
  1089. /* Copy in each associated data sg entry from request */
  1090. written = spu_msg_sg_add(&sg, &assoc_sg, &assoc_offset,
  1091. assoc_nents, assoc_len);
  1092. if (written < assoc_len) {
  1093. pr_err("%s(): failed to copy assoc sg to mbox msg",
  1094. __func__);
  1095. return -EFAULT;
  1096. }
  1097. }
  1098. if (aead_iv_len)
  1099. sg_set_buf(sg++, rctx->msg_buf.iv_ctr, aead_iv_len);
  1100. if (aad_pad_len) {
  1101. memset(rctx->msg_buf.a.req_aad_pad, 0, aad_pad_len);
  1102. sg_set_buf(sg++, rctx->msg_buf.a.req_aad_pad, aad_pad_len);
  1103. }
  1104. datalen = chunksize;
  1105. if ((chunksize > ctx->digestsize) && incl_icv)
  1106. datalen -= ctx->digestsize;
  1107. if (datalen) {
  1108. /* For aead, a single msg should consume the entire src sg */
  1109. written = spu_msg_sg_add(&sg, &rctx->src_sg, &rctx->src_skip,
  1110. rctx->src_nents, datalen);
  1111. if (written < datalen) {
  1112. pr_err("%s(): failed to copy src sg to mbox msg",
  1113. __func__);
  1114. return -EFAULT;
  1115. }
  1116. }
  1117. if (pad_len) {
  1118. memset(rctx->msg_buf.spu_req_pad, 0, pad_len);
  1119. sg_set_buf(sg++, rctx->msg_buf.spu_req_pad, pad_len);
  1120. }
  1121. if (incl_icv)
  1122. sg_set_buf(sg++, rctx->msg_buf.digest, ctx->digestsize);
  1123. stat_len = spu->spu_tx_status_len();
  1124. if (stat_len) {
  1125. memset(rctx->msg_buf.tx_stat, 0, stat_len);
  1126. sg_set_buf(sg, rctx->msg_buf.tx_stat, stat_len);
  1127. }
  1128. return 0;
  1129. }
  1130. /**
  1131. * handle_aead_req() - Submit a SPU request message for the next chunk of the
  1132. * current AEAD request.
  1133. * @rctx: Crypto request context
  1134. *
  1135. * Unlike other operation types, we assume the length of the request fits in
  1136. * a single SPU request message. aead_enqueue() makes sure this is true.
  1137. * Comments for other op types regarding threads applies here as well.
  1138. *
  1139. * Unlike incremental hash ops, where the spu returns the entire hash for
  1140. * truncated algs like sha-224, the SPU returns just the truncated hash in
  1141. * response to aead requests. So digestsize is always ctx->digestsize here.
  1142. *
  1143. * Return: -EINPROGRESS: crypto request has been accepted and result will be
  1144. * returned asynchronously
  1145. * Any other value indicates an error
  1146. */
  1147. static int handle_aead_req(struct iproc_reqctx_s *rctx)
  1148. {
  1149. struct spu_hw *spu = &iproc_priv.spu;
  1150. struct crypto_async_request *areq = rctx->parent;
  1151. struct aead_request *req = container_of(areq,
  1152. struct aead_request, base);
  1153. struct iproc_ctx_s *ctx = rctx->ctx;
  1154. int err;
  1155. unsigned int chunksize;
  1156. unsigned int resp_len;
  1157. u32 spu_hdr_len;
  1158. u32 db_size;
  1159. u32 stat_pad_len;
  1160. u32 pad_len;
  1161. struct brcm_message *mssg; /* mailbox message */
  1162. struct spu_request_opts req_opts;
  1163. struct spu_cipher_parms cipher_parms;
  1164. struct spu_hash_parms hash_parms;
  1165. struct spu_aead_parms aead_parms;
  1166. int assoc_nents = 0;
  1167. bool incl_icv = false;
  1168. unsigned int digestsize = ctx->digestsize;
  1169. /* number of entries in src and dst sg. Always includes SPU msg header.
  1170. */
  1171. u8 rx_frag_num = 2; /* and STATUS */
  1172. u8 tx_frag_num = 1;
  1173. /* doing the whole thing at once */
  1174. chunksize = rctx->total_todo;
  1175. flow_log("%s: chunksize %u\n", __func__, chunksize);
  1176. memset(&req_opts, 0, sizeof(req_opts));
  1177. memset(&hash_parms, 0, sizeof(hash_parms));
  1178. memset(&aead_parms, 0, sizeof(aead_parms));
  1179. req_opts.is_inbound = !(rctx->is_encrypt);
  1180. req_opts.auth_first = ctx->auth_first;
  1181. req_opts.is_aead = true;
  1182. req_opts.is_esp = ctx->is_esp;
  1183. cipher_parms.alg = ctx->cipher.alg;
  1184. cipher_parms.mode = ctx->cipher.mode;
  1185. cipher_parms.type = ctx->cipher_type;
  1186. cipher_parms.key_buf = ctx->enckey;
  1187. cipher_parms.key_len = ctx->enckeylen;
  1188. cipher_parms.iv_buf = rctx->msg_buf.iv_ctr;
  1189. cipher_parms.iv_len = rctx->iv_ctr_len;
  1190. hash_parms.alg = ctx->auth.alg;
  1191. hash_parms.mode = ctx->auth.mode;
  1192. hash_parms.type = HASH_TYPE_NONE;
  1193. hash_parms.key_buf = (u8 *)ctx->authkey;
  1194. hash_parms.key_len = ctx->authkeylen;
  1195. hash_parms.digestsize = digestsize;
  1196. if ((ctx->auth.alg == HASH_ALG_SHA224) &&
  1197. (ctx->authkeylen < SHA224_DIGEST_SIZE))
  1198. hash_parms.key_len = SHA224_DIGEST_SIZE;
  1199. aead_parms.assoc_size = req->assoclen;
  1200. if (ctx->is_esp && !ctx->is_rfc4543) {
  1201. /*
  1202. * 8-byte IV is included assoc data in request. SPU2
  1203. * expects AAD to include just SPI and seqno. So
  1204. * subtract off the IV len.
  1205. */
  1206. aead_parms.assoc_size -= GCM_RFC4106_IV_SIZE;
  1207. if (rctx->is_encrypt) {
  1208. aead_parms.return_iv = true;
  1209. aead_parms.ret_iv_len = GCM_RFC4106_IV_SIZE;
  1210. aead_parms.ret_iv_off = GCM_ESP_SALT_SIZE;
  1211. }
  1212. } else {
  1213. aead_parms.ret_iv_len = 0;
  1214. }
  1215. /*
  1216. * Count number of sg entries from the crypto API request that are to
  1217. * be included in this mailbox message. For dst sg, don't count space
  1218. * for digest. Digest gets caught in a separate buffer and copied back
  1219. * to dst sg when processing response.
  1220. */
  1221. rctx->src_nents = spu_sg_count(rctx->src_sg, rctx->src_skip, chunksize);
  1222. rctx->dst_nents = spu_sg_count(rctx->dst_sg, rctx->dst_skip, chunksize);
  1223. if (aead_parms.assoc_size)
  1224. assoc_nents = spu_sg_count(rctx->assoc, 0,
  1225. aead_parms.assoc_size);
  1226. mssg = &rctx->mb_mssg;
  1227. rctx->total_sent = chunksize;
  1228. rctx->src_sent = chunksize;
  1229. if (spu->spu_assoc_resp_len(ctx->cipher.mode,
  1230. aead_parms.assoc_size,
  1231. aead_parms.ret_iv_len,
  1232. rctx->is_encrypt))
  1233. rx_frag_num++;
  1234. aead_parms.iv_len = spu->spu_aead_ivlen(ctx->cipher.mode,
  1235. rctx->iv_ctr_len);
  1236. if (ctx->auth.alg == HASH_ALG_AES)
  1237. hash_parms.type = (enum hash_type)ctx->cipher_type;
  1238. /* General case AAD padding (CCM and RFC4543 special cases below) */
  1239. aead_parms.aad_pad_len = spu->spu_gcm_ccm_pad_len(ctx->cipher.mode,
  1240. aead_parms.assoc_size);
  1241. /* General case data padding (CCM decrypt special case below) */
  1242. aead_parms.data_pad_len = spu->spu_gcm_ccm_pad_len(ctx->cipher.mode,
  1243. chunksize);
  1244. if (ctx->cipher.mode == CIPHER_MODE_CCM) {
  1245. /*
  1246. * for CCM, AAD len + 2 (rather than AAD len) needs to be
  1247. * 128-bit aligned
  1248. */
  1249. aead_parms.aad_pad_len = spu->spu_gcm_ccm_pad_len(
  1250. ctx->cipher.mode,
  1251. aead_parms.assoc_size + 2);
  1252. /*
  1253. * And when decrypting CCM, need to pad without including
  1254. * size of ICV which is tacked on to end of chunk
  1255. */
  1256. if (!rctx->is_encrypt)
  1257. aead_parms.data_pad_len =
  1258. spu->spu_gcm_ccm_pad_len(ctx->cipher.mode,
  1259. chunksize - digestsize);
  1260. /* CCM also requires software to rewrite portions of IV: */
  1261. spu->spu_ccm_update_iv(digestsize, &cipher_parms, req->assoclen,
  1262. chunksize, rctx->is_encrypt,
  1263. ctx->is_esp);
  1264. }
  1265. if (ctx->is_rfc4543) {
  1266. /*
  1267. * RFC4543: data is included in AAD, so don't pad after AAD
  1268. * and pad data based on both AAD + data size
  1269. */
  1270. aead_parms.aad_pad_len = 0;
  1271. if (!rctx->is_encrypt)
  1272. aead_parms.data_pad_len = spu->spu_gcm_ccm_pad_len(
  1273. ctx->cipher.mode,
  1274. aead_parms.assoc_size + chunksize -
  1275. digestsize);
  1276. else
  1277. aead_parms.data_pad_len = spu->spu_gcm_ccm_pad_len(
  1278. ctx->cipher.mode,
  1279. aead_parms.assoc_size + chunksize);
  1280. req_opts.is_rfc4543 = true;
  1281. }
  1282. if (spu_req_incl_icv(ctx->cipher.mode, rctx->is_encrypt)) {
  1283. incl_icv = true;
  1284. tx_frag_num++;
  1285. /* Copy ICV from end of src scatterlist to digest buf */
  1286. sg_copy_part_to_buf(req->src, rctx->msg_buf.digest, digestsize,
  1287. req->assoclen + rctx->total_sent -
  1288. digestsize);
  1289. }
  1290. atomic64_add(chunksize, &iproc_priv.bytes_out);
  1291. flow_log("%s()-sent chunksize:%u\n", __func__, chunksize);
  1292. /* Prepend SPU header with type 3 BCM header */
  1293. memcpy(rctx->msg_buf.bcm_spu_req_hdr, BCMHEADER, BCM_HDR_LEN);
  1294. spu_hdr_len = spu->spu_create_request(rctx->msg_buf.bcm_spu_req_hdr +
  1295. BCM_HDR_LEN, &req_opts,
  1296. &cipher_parms, &hash_parms,
  1297. &aead_parms, chunksize);
  1298. /* Determine total length of padding. Put all padding in one buffer. */
  1299. db_size = spu_real_db_size(aead_parms.assoc_size, aead_parms.iv_len, 0,
  1300. chunksize, aead_parms.aad_pad_len,
  1301. aead_parms.data_pad_len, 0);
  1302. stat_pad_len = spu->spu_wordalign_padlen(db_size);
  1303. if (stat_pad_len)
  1304. rx_frag_num++;
  1305. pad_len = aead_parms.data_pad_len + stat_pad_len;
  1306. if (pad_len) {
  1307. tx_frag_num++;
  1308. spu->spu_request_pad(rctx->msg_buf.spu_req_pad,
  1309. aead_parms.data_pad_len, 0,
  1310. ctx->auth.alg, ctx->auth.mode,
  1311. rctx->total_sent, stat_pad_len);
  1312. }
  1313. spu->spu_dump_msg_hdr(rctx->msg_buf.bcm_spu_req_hdr + BCM_HDR_LEN,
  1314. spu_hdr_len);
  1315. dump_sg(rctx->assoc, 0, aead_parms.assoc_size);
  1316. packet_dump(" aead iv: ", rctx->msg_buf.iv_ctr, aead_parms.iv_len);
  1317. packet_log("BD:\n");
  1318. dump_sg(rctx->src_sg, rctx->src_skip, chunksize);
  1319. packet_dump(" pad: ", rctx->msg_buf.spu_req_pad, pad_len);
  1320. /*
  1321. * Build mailbox message containing SPU request msg and rx buffers
  1322. * to catch response message
  1323. */
  1324. memset(mssg, 0, sizeof(*mssg));
  1325. mssg->type = BRCM_MESSAGE_SPU;
  1326. mssg->ctx = rctx; /* Will be returned in response */
  1327. /* Create rx scatterlist to catch result */
  1328. rx_frag_num += rctx->dst_nents;
  1329. resp_len = chunksize;
  1330. /*
  1331. * Always catch ICV in separate buffer. Have to for GCM/CCM because of
  1332. * padding. Have to for SHA-224 and other truncated SHAs because SPU
  1333. * sends entire digest back.
  1334. */
  1335. rx_frag_num++;
  1336. if (((ctx->cipher.mode == CIPHER_MODE_GCM) ||
  1337. (ctx->cipher.mode == CIPHER_MODE_CCM)) && !rctx->is_encrypt) {
  1338. /*
  1339. * Input is ciphertxt plus ICV, but ICV not incl
  1340. * in output.
  1341. */
  1342. resp_len -= ctx->digestsize;
  1343. if (resp_len == 0)
  1344. /* no rx frags to catch output data */
  1345. rx_frag_num -= rctx->dst_nents;
  1346. }
  1347. err = spu_aead_rx_sg_create(mssg, req, rctx, rx_frag_num,
  1348. aead_parms.assoc_size,
  1349. aead_parms.ret_iv_len, resp_len, digestsize,
  1350. stat_pad_len);
  1351. if (err)
  1352. return err;
  1353. /* Create tx scatterlist containing SPU request message */
  1354. tx_frag_num += rctx->src_nents;
  1355. tx_frag_num += assoc_nents;
  1356. if (aead_parms.aad_pad_len)
  1357. tx_frag_num++;
  1358. if (aead_parms.iv_len)
  1359. tx_frag_num++;
  1360. if (spu->spu_tx_status_len())
  1361. tx_frag_num++;
  1362. err = spu_aead_tx_sg_create(mssg, rctx, tx_frag_num, spu_hdr_len,
  1363. rctx->assoc, aead_parms.assoc_size,
  1364. assoc_nents, aead_parms.iv_len, chunksize,
  1365. aead_parms.aad_pad_len, pad_len, incl_icv);
  1366. if (err)
  1367. return err;
  1368. err = mailbox_send_message(mssg, req->base.flags, rctx->chan_idx);
  1369. if (unlikely(err < 0))
  1370. return err;
  1371. return -EINPROGRESS;
  1372. }
  1373. /**
  1374. * handle_aead_resp() - Process a SPU response message for an AEAD request.
  1375. * @rctx: Crypto request context
  1376. */
  1377. static void handle_aead_resp(struct iproc_reqctx_s *rctx)
  1378. {
  1379. struct spu_hw *spu = &iproc_priv.spu;
  1380. struct crypto_async_request *areq = rctx->parent;
  1381. struct aead_request *req = container_of(areq,
  1382. struct aead_request, base);
  1383. struct iproc_ctx_s *ctx = rctx->ctx;
  1384. u32 payload_len;
  1385. unsigned int icv_offset;
  1386. u32 result_len;
  1387. /* See how much data was returned */
  1388. payload_len = spu->spu_payload_length(rctx->msg_buf.spu_resp_hdr);
  1389. flow_log("payload_len %u\n", payload_len);
  1390. /* only count payload */
  1391. atomic64_add(payload_len, &iproc_priv.bytes_in);
  1392. if (req->assoclen)
  1393. packet_dump(" assoc_data ", rctx->msg_buf.a.resp_aad,
  1394. req->assoclen);
  1395. /*
  1396. * Copy the ICV back to the destination
  1397. * buffer. In decrypt case, SPU gives us back the digest, but crypto
  1398. * API doesn't expect ICV in dst buffer.
  1399. */
  1400. result_len = req->cryptlen;
  1401. if (rctx->is_encrypt) {
  1402. icv_offset = req->assoclen + rctx->total_sent;
  1403. packet_dump(" ICV: ", rctx->msg_buf.digest, ctx->digestsize);
  1404. flow_log("copying ICV to dst sg at offset %u\n", icv_offset);
  1405. sg_copy_part_from_buf(req->dst, rctx->msg_buf.digest,
  1406. ctx->digestsize, icv_offset);
  1407. result_len += ctx->digestsize;
  1408. }
  1409. packet_log("response data: ");
  1410. dump_sg(req->dst, req->assoclen, result_len);
  1411. atomic_inc(&iproc_priv.op_counts[SPU_OP_AEAD]);
  1412. if (ctx->cipher.alg == CIPHER_ALG_AES) {
  1413. if (ctx->cipher.mode == CIPHER_MODE_CCM)
  1414. atomic_inc(&iproc_priv.aead_cnt[AES_CCM]);
  1415. else if (ctx->cipher.mode == CIPHER_MODE_GCM)
  1416. atomic_inc(&iproc_priv.aead_cnt[AES_GCM]);
  1417. else
  1418. atomic_inc(&iproc_priv.aead_cnt[AUTHENC]);
  1419. } else {
  1420. atomic_inc(&iproc_priv.aead_cnt[AUTHENC]);
  1421. }
  1422. }
  1423. /**
  1424. * spu_chunk_cleanup() - Do cleanup after processing one chunk of a request
  1425. * @rctx: request context
  1426. *
  1427. * Mailbox scatterlists are allocated for each chunk. So free them after
  1428. * processing each chunk.
  1429. */
  1430. static void spu_chunk_cleanup(struct iproc_reqctx_s *rctx)
  1431. {
  1432. /* mailbox message used to tx request */
  1433. struct brcm_message *mssg = &rctx->mb_mssg;
  1434. kfree(mssg->spu.src);
  1435. kfree(mssg->spu.dst);
  1436. memset(mssg, 0, sizeof(struct brcm_message));
  1437. }
  1438. /**
  1439. * finish_req() - Used to invoke the complete callback from the requester when
  1440. * a request has been handled asynchronously.
  1441. * @rctx: Request context
  1442. * @err: Indicates whether the request was successful or not
  1443. *
  1444. * Ensures that cleanup has been done for request
  1445. */
  1446. static void finish_req(struct iproc_reqctx_s *rctx, int err)
  1447. {
  1448. struct crypto_async_request *areq = rctx->parent;
  1449. flow_log("%s() err:%d\n\n", __func__, err);
  1450. /* No harm done if already called */
  1451. spu_chunk_cleanup(rctx);
  1452. if (areq)
  1453. areq->complete(areq, err);
  1454. }
  1455. /**
  1456. * spu_rx_callback() - Callback from mailbox framework with a SPU response.
  1457. * @cl: mailbox client structure for SPU driver
  1458. * @msg: mailbox message containing SPU response
  1459. */
  1460. static void spu_rx_callback(struct mbox_client *cl, void *msg)
  1461. {
  1462. struct spu_hw *spu = &iproc_priv.spu;
  1463. struct brcm_message *mssg = msg;
  1464. struct iproc_reqctx_s *rctx;
  1465. struct iproc_ctx_s *ctx;
  1466. struct crypto_async_request *areq;
  1467. int err = 0;
  1468. rctx = mssg->ctx;
  1469. if (unlikely(!rctx)) {
  1470. /* This is fatal */
  1471. pr_err("%s(): no request context", __func__);
  1472. err = -EFAULT;
  1473. goto cb_finish;
  1474. }
  1475. areq = rctx->parent;
  1476. ctx = rctx->ctx;
  1477. /* process the SPU status */
  1478. err = spu->spu_status_process(rctx->msg_buf.rx_stat);
  1479. if (err != 0) {
  1480. if (err == SPU_INVALID_ICV)
  1481. atomic_inc(&iproc_priv.bad_icv);
  1482. err = -EBADMSG;
  1483. goto cb_finish;
  1484. }
  1485. /* Process the SPU response message */
  1486. switch (rctx->ctx->alg->type) {
  1487. case CRYPTO_ALG_TYPE_ABLKCIPHER:
  1488. handle_ablkcipher_resp(rctx);
  1489. break;
  1490. case CRYPTO_ALG_TYPE_AHASH:
  1491. handle_ahash_resp(rctx);
  1492. break;
  1493. case CRYPTO_ALG_TYPE_AEAD:
  1494. handle_aead_resp(rctx);
  1495. break;
  1496. default:
  1497. err = -EINVAL;
  1498. goto cb_finish;
  1499. }
  1500. /*
  1501. * If this response does not complete the request, then send the next
  1502. * request chunk.
  1503. */
  1504. if (rctx->total_sent < rctx->total_todo) {
  1505. /* Deallocate anything specific to previous chunk */
  1506. spu_chunk_cleanup(rctx);
  1507. switch (rctx->ctx->alg->type) {
  1508. case CRYPTO_ALG_TYPE_ABLKCIPHER:
  1509. err = handle_ablkcipher_req(rctx);
  1510. break;
  1511. case CRYPTO_ALG_TYPE_AHASH:
  1512. err = handle_ahash_req(rctx);
  1513. if (err == -EAGAIN)
  1514. /*
  1515. * we saved data in hash carry, but tell crypto
  1516. * API we successfully completed request.
  1517. */
  1518. err = 0;
  1519. break;
  1520. case CRYPTO_ALG_TYPE_AEAD:
  1521. err = handle_aead_req(rctx);
  1522. break;
  1523. default:
  1524. err = -EINVAL;
  1525. }
  1526. if (err == -EINPROGRESS)
  1527. /* Successfully submitted request for next chunk */
  1528. return;
  1529. }
  1530. cb_finish:
  1531. finish_req(rctx, err);
  1532. }
  1533. /* ==================== Kernel Cryptographic API ==================== */
  1534. /**
  1535. * ablkcipher_enqueue() - Handle ablkcipher encrypt or decrypt request.
  1536. * @req: Crypto API request
  1537. * @encrypt: true if encrypting; false if decrypting
  1538. *
  1539. * Return: -EINPROGRESS if request accepted and result will be returned
  1540. * asynchronously
  1541. * < 0 if an error
  1542. */
  1543. static int ablkcipher_enqueue(struct ablkcipher_request *req, bool encrypt)
  1544. {
  1545. struct iproc_reqctx_s *rctx = ablkcipher_request_ctx(req);
  1546. struct iproc_ctx_s *ctx =
  1547. crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req));
  1548. int err;
  1549. flow_log("%s() enc:%u\n", __func__, encrypt);
  1550. rctx->gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
  1551. CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
  1552. rctx->parent = &req->base;
  1553. rctx->is_encrypt = encrypt;
  1554. rctx->bd_suppress = false;
  1555. rctx->total_todo = req->nbytes;
  1556. rctx->src_sent = 0;
  1557. rctx->total_sent = 0;
  1558. rctx->total_received = 0;
  1559. rctx->ctx = ctx;
  1560. /* Initialize current position in src and dst scatterlists */
  1561. rctx->src_sg = req->src;
  1562. rctx->src_nents = 0;
  1563. rctx->src_skip = 0;
  1564. rctx->dst_sg = req->dst;
  1565. rctx->dst_nents = 0;
  1566. rctx->dst_skip = 0;
  1567. if (ctx->cipher.mode == CIPHER_MODE_CBC ||
  1568. ctx->cipher.mode == CIPHER_MODE_CTR ||
  1569. ctx->cipher.mode == CIPHER_MODE_OFB ||
  1570. ctx->cipher.mode == CIPHER_MODE_XTS ||
  1571. ctx->cipher.mode == CIPHER_MODE_GCM ||
  1572. ctx->cipher.mode == CIPHER_MODE_CCM) {
  1573. rctx->iv_ctr_len =
  1574. crypto_ablkcipher_ivsize(crypto_ablkcipher_reqtfm(req));
  1575. memcpy(rctx->msg_buf.iv_ctr, req->info, rctx->iv_ctr_len);
  1576. } else {
  1577. rctx->iv_ctr_len = 0;
  1578. }
  1579. /* Choose a SPU to process this request */
  1580. rctx->chan_idx = select_channel();
  1581. err = handle_ablkcipher_req(rctx);
  1582. if (err != -EINPROGRESS)
  1583. /* synchronous result */
  1584. spu_chunk_cleanup(rctx);
  1585. return err;
  1586. }
  1587. static int des_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
  1588. unsigned int keylen)
  1589. {
  1590. struct iproc_ctx_s *ctx = crypto_ablkcipher_ctx(cipher);
  1591. u32 tmp[DES_EXPKEY_WORDS];
  1592. if (keylen == DES_KEY_SIZE) {
  1593. if (des_ekey(tmp, key) == 0) {
  1594. if (crypto_ablkcipher_get_flags(cipher) &
  1595. CRYPTO_TFM_REQ_WEAK_KEY) {
  1596. u32 flags = CRYPTO_TFM_RES_WEAK_KEY;
  1597. crypto_ablkcipher_set_flags(cipher, flags);
  1598. return -EINVAL;
  1599. }
  1600. }
  1601. ctx->cipher_type = CIPHER_TYPE_DES;
  1602. } else {
  1603. crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  1604. return -EINVAL;
  1605. }
  1606. return 0;
  1607. }
  1608. static int threedes_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
  1609. unsigned int keylen)
  1610. {
  1611. struct iproc_ctx_s *ctx = crypto_ablkcipher_ctx(cipher);
  1612. if (keylen == (DES_KEY_SIZE * 3)) {
  1613. const u32 *K = (const u32 *)key;
  1614. u32 flags = CRYPTO_TFM_RES_BAD_KEY_SCHED;
  1615. if (!((K[0] ^ K[2]) | (K[1] ^ K[3])) ||
  1616. !((K[2] ^ K[4]) | (K[3] ^ K[5]))) {
  1617. crypto_ablkcipher_set_flags(cipher, flags);
  1618. return -EINVAL;
  1619. }
  1620. ctx->cipher_type = CIPHER_TYPE_3DES;
  1621. } else {
  1622. crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  1623. return -EINVAL;
  1624. }
  1625. return 0;
  1626. }
  1627. static int aes_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
  1628. unsigned int keylen)
  1629. {
  1630. struct iproc_ctx_s *ctx = crypto_ablkcipher_ctx(cipher);
  1631. if (ctx->cipher.mode == CIPHER_MODE_XTS)
  1632. /* XTS includes two keys of equal length */
  1633. keylen = keylen / 2;
  1634. switch (keylen) {
  1635. case AES_KEYSIZE_128:
  1636. ctx->cipher_type = CIPHER_TYPE_AES128;
  1637. break;
  1638. case AES_KEYSIZE_192:
  1639. ctx->cipher_type = CIPHER_TYPE_AES192;
  1640. break;
  1641. case AES_KEYSIZE_256:
  1642. ctx->cipher_type = CIPHER_TYPE_AES256;
  1643. break;
  1644. default:
  1645. crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  1646. return -EINVAL;
  1647. }
  1648. WARN_ON((ctx->max_payload != SPU_MAX_PAYLOAD_INF) &&
  1649. ((ctx->max_payload % AES_BLOCK_SIZE) != 0));
  1650. return 0;
  1651. }
  1652. static int rc4_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
  1653. unsigned int keylen)
  1654. {
  1655. struct iproc_ctx_s *ctx = crypto_ablkcipher_ctx(cipher);
  1656. int i;
  1657. ctx->enckeylen = ARC4_MAX_KEY_SIZE + ARC4_STATE_SIZE;
  1658. ctx->enckey[0] = 0x00; /* 0x00 */
  1659. ctx->enckey[1] = 0x00; /* i */
  1660. ctx->enckey[2] = 0x00; /* 0x00 */
  1661. ctx->enckey[3] = 0x00; /* j */
  1662. for (i = 0; i < ARC4_MAX_KEY_SIZE; i++)
  1663. ctx->enckey[i + ARC4_STATE_SIZE] = key[i % keylen];
  1664. ctx->cipher_type = CIPHER_TYPE_INIT;
  1665. return 0;
  1666. }
  1667. static int ablkcipher_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
  1668. unsigned int keylen)
  1669. {
  1670. struct spu_hw *spu = &iproc_priv.spu;
  1671. struct iproc_ctx_s *ctx = crypto_ablkcipher_ctx(cipher);
  1672. struct spu_cipher_parms cipher_parms;
  1673. u32 alloc_len = 0;
  1674. int err;
  1675. flow_log("ablkcipher_setkey() keylen: %d\n", keylen);
  1676. flow_dump(" key: ", key, keylen);
  1677. switch (ctx->cipher.alg) {
  1678. case CIPHER_ALG_DES:
  1679. err = des_setkey(cipher, key, keylen);
  1680. break;
  1681. case CIPHER_ALG_3DES:
  1682. err = threedes_setkey(cipher, key, keylen);
  1683. break;
  1684. case CIPHER_ALG_AES:
  1685. err = aes_setkey(cipher, key, keylen);
  1686. break;
  1687. case CIPHER_ALG_RC4:
  1688. err = rc4_setkey(cipher, key, keylen);
  1689. break;
  1690. default:
  1691. pr_err("%s() Error: unknown cipher alg\n", __func__);
  1692. err = -EINVAL;
  1693. }
  1694. if (err)
  1695. return err;
  1696. /* RC4 already populated ctx->enkey */
  1697. if (ctx->cipher.alg != CIPHER_ALG_RC4) {
  1698. memcpy(ctx->enckey, key, keylen);
  1699. ctx->enckeylen = keylen;
  1700. }
  1701. /* SPU needs XTS keys in the reverse order the crypto API presents */
  1702. if ((ctx->cipher.alg == CIPHER_ALG_AES) &&
  1703. (ctx->cipher.mode == CIPHER_MODE_XTS)) {
  1704. unsigned int xts_keylen = keylen / 2;
  1705. memcpy(ctx->enckey, key + xts_keylen, xts_keylen);
  1706. memcpy(ctx->enckey + xts_keylen, key, xts_keylen);
  1707. }
  1708. if (spu->spu_type == SPU_TYPE_SPUM)
  1709. alloc_len = BCM_HDR_LEN + SPU_HEADER_ALLOC_LEN;
  1710. else if (spu->spu_type == SPU_TYPE_SPU2)
  1711. alloc_len = BCM_HDR_LEN + SPU2_HEADER_ALLOC_LEN;
  1712. memset(ctx->bcm_spu_req_hdr, 0, alloc_len);
  1713. cipher_parms.iv_buf = NULL;
  1714. cipher_parms.iv_len = crypto_ablkcipher_ivsize(cipher);
  1715. flow_log("%s: iv_len %u\n", __func__, cipher_parms.iv_len);
  1716. cipher_parms.alg = ctx->cipher.alg;
  1717. cipher_parms.mode = ctx->cipher.mode;
  1718. cipher_parms.type = ctx->cipher_type;
  1719. cipher_parms.key_buf = ctx->enckey;
  1720. cipher_parms.key_len = ctx->enckeylen;
  1721. /* Prepend SPU request message with BCM header */
  1722. memcpy(ctx->bcm_spu_req_hdr, BCMHEADER, BCM_HDR_LEN);
  1723. ctx->spu_req_hdr_len =
  1724. spu->spu_cipher_req_init(ctx->bcm_spu_req_hdr + BCM_HDR_LEN,
  1725. &cipher_parms);
  1726. ctx->spu_resp_hdr_len = spu->spu_response_hdr_len(ctx->authkeylen,
  1727. ctx->enckeylen,
  1728. false);
  1729. atomic_inc(&iproc_priv.setkey_cnt[SPU_OP_CIPHER]);
  1730. return 0;
  1731. }
  1732. static int ablkcipher_encrypt(struct ablkcipher_request *req)
  1733. {
  1734. flow_log("ablkcipher_encrypt() nbytes:%u\n", req->nbytes);
  1735. return ablkcipher_enqueue(req, true);
  1736. }
  1737. static int ablkcipher_decrypt(struct ablkcipher_request *req)
  1738. {
  1739. flow_log("ablkcipher_decrypt() nbytes:%u\n", req->nbytes);
  1740. return ablkcipher_enqueue(req, false);
  1741. }
  1742. static int ahash_enqueue(struct ahash_request *req)
  1743. {
  1744. struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
  1745. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1746. struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
  1747. int err = 0;
  1748. const char *alg_name;
  1749. flow_log("ahash_enqueue() nbytes:%u\n", req->nbytes);
  1750. rctx->gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
  1751. CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
  1752. rctx->parent = &req->base;
  1753. rctx->ctx = ctx;
  1754. rctx->bd_suppress = true;
  1755. memset(&rctx->mb_mssg, 0, sizeof(struct brcm_message));
  1756. /* Initialize position in src scatterlist */
  1757. rctx->src_sg = req->src;
  1758. rctx->src_skip = 0;
  1759. rctx->src_nents = 0;
  1760. rctx->dst_sg = NULL;
  1761. rctx->dst_skip = 0;
  1762. rctx->dst_nents = 0;
  1763. /* SPU2 hardware does not compute hash of zero length data */
  1764. if ((rctx->is_final == 1) && (rctx->total_todo == 0) &&
  1765. (iproc_priv.spu.spu_type == SPU_TYPE_SPU2)) {
  1766. alg_name = crypto_tfm_alg_name(crypto_ahash_tfm(tfm));
  1767. flow_log("Doing %sfinal %s zero-len hash request in software\n",
  1768. rctx->is_final ? "" : "non-", alg_name);
  1769. err = do_shash((unsigned char *)alg_name, req->result,
  1770. NULL, 0, NULL, 0, ctx->authkey,
  1771. ctx->authkeylen);
  1772. if (err < 0)
  1773. flow_log("Hash request failed with error %d\n", err);
  1774. return err;
  1775. }
  1776. /* Choose a SPU to process this request */
  1777. rctx->chan_idx = select_channel();
  1778. err = handle_ahash_req(rctx);
  1779. if (err != -EINPROGRESS)
  1780. /* synchronous result */
  1781. spu_chunk_cleanup(rctx);
  1782. if (err == -EAGAIN)
  1783. /*
  1784. * we saved data in hash carry, but tell crypto API
  1785. * we successfully completed request.
  1786. */
  1787. err = 0;
  1788. return err;
  1789. }
  1790. static int __ahash_init(struct ahash_request *req)
  1791. {
  1792. struct spu_hw *spu = &iproc_priv.spu;
  1793. struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
  1794. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1795. struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
  1796. flow_log("%s()\n", __func__);
  1797. /* Initialize the context */
  1798. rctx->hash_carry_len = 0;
  1799. rctx->is_final = 0;
  1800. rctx->total_todo = 0;
  1801. rctx->src_sent = 0;
  1802. rctx->total_sent = 0;
  1803. rctx->total_received = 0;
  1804. ctx->digestsize = crypto_ahash_digestsize(tfm);
  1805. /* If we add a hash whose digest is larger, catch it here. */
  1806. WARN_ON(ctx->digestsize > MAX_DIGEST_SIZE);
  1807. rctx->is_sw_hmac = false;
  1808. ctx->spu_resp_hdr_len = spu->spu_response_hdr_len(ctx->authkeylen, 0,
  1809. true);
  1810. return 0;
  1811. }
  1812. /**
  1813. * spu_no_incr_hash() - Determine whether incremental hashing is supported.
  1814. * @ctx: Crypto session context
  1815. *
  1816. * SPU-2 does not support incremental hashing (we'll have to revisit and
  1817. * condition based on chip revision or device tree entry if future versions do
  1818. * support incremental hash)
  1819. *
  1820. * SPU-M also doesn't support incremental hashing of AES-XCBC
  1821. *
  1822. * Return: true if incremental hashing is not supported
  1823. * false otherwise
  1824. */
  1825. bool spu_no_incr_hash(struct iproc_ctx_s *ctx)
  1826. {
  1827. struct spu_hw *spu = &iproc_priv.spu;
  1828. if (spu->spu_type == SPU_TYPE_SPU2)
  1829. return true;
  1830. if ((ctx->auth.alg == HASH_ALG_AES) &&
  1831. (ctx->auth.mode == HASH_MODE_XCBC))
  1832. return true;
  1833. /* Otherwise, incremental hashing is supported */
  1834. return false;
  1835. }
  1836. static int ahash_init(struct ahash_request *req)
  1837. {
  1838. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1839. struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
  1840. const char *alg_name;
  1841. struct crypto_shash *hash;
  1842. int ret;
  1843. gfp_t gfp;
  1844. if (spu_no_incr_hash(ctx)) {
  1845. /*
  1846. * If we get an incremental hashing request and it's not
  1847. * supported by the hardware, we need to handle it in software
  1848. * by calling synchronous hash functions.
  1849. */
  1850. alg_name = crypto_tfm_alg_name(crypto_ahash_tfm(tfm));
  1851. hash = crypto_alloc_shash(alg_name, 0, 0);
  1852. if (IS_ERR(hash)) {
  1853. ret = PTR_ERR(hash);
  1854. goto err;
  1855. }
  1856. gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
  1857. CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
  1858. ctx->shash = kmalloc(sizeof(*ctx->shash) +
  1859. crypto_shash_descsize(hash), gfp);
  1860. if (!ctx->shash) {
  1861. ret = -ENOMEM;
  1862. goto err_hash;
  1863. }
  1864. ctx->shash->tfm = hash;
  1865. ctx->shash->flags = 0;
  1866. /* Set the key using data we already have from setkey */
  1867. if (ctx->authkeylen > 0) {
  1868. ret = crypto_shash_setkey(hash, ctx->authkey,
  1869. ctx->authkeylen);
  1870. if (ret)
  1871. goto err_shash;
  1872. }
  1873. /* Initialize hash w/ this key and other params */
  1874. ret = crypto_shash_init(ctx->shash);
  1875. if (ret)
  1876. goto err_shash;
  1877. } else {
  1878. /* Otherwise call the internal function which uses SPU hw */
  1879. ret = __ahash_init(req);
  1880. }
  1881. return ret;
  1882. err_shash:
  1883. kfree(ctx->shash);
  1884. err_hash:
  1885. crypto_free_shash(hash);
  1886. err:
  1887. return ret;
  1888. }
  1889. static int __ahash_update(struct ahash_request *req)
  1890. {
  1891. struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
  1892. flow_log("ahash_update() nbytes:%u\n", req->nbytes);
  1893. if (!req->nbytes)
  1894. return 0;
  1895. rctx->total_todo += req->nbytes;
  1896. rctx->src_sent = 0;
  1897. return ahash_enqueue(req);
  1898. }
  1899. static int ahash_update(struct ahash_request *req)
  1900. {
  1901. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1902. struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
  1903. u8 *tmpbuf;
  1904. int ret;
  1905. int nents;
  1906. gfp_t gfp;
  1907. if (spu_no_incr_hash(ctx)) {
  1908. /*
  1909. * If we get an incremental hashing request and it's not
  1910. * supported by the hardware, we need to handle it in software
  1911. * by calling synchronous hash functions.
  1912. */
  1913. if (req->src)
  1914. nents = sg_nents(req->src);
  1915. else
  1916. return -EINVAL;
  1917. /* Copy data from req scatterlist to tmp buffer */
  1918. gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
  1919. CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
  1920. tmpbuf = kmalloc(req->nbytes, gfp);
  1921. if (!tmpbuf)
  1922. return -ENOMEM;
  1923. if (sg_copy_to_buffer(req->src, nents, tmpbuf, req->nbytes) !=
  1924. req->nbytes) {
  1925. kfree(tmpbuf);
  1926. return -EINVAL;
  1927. }
  1928. /* Call synchronous update */
  1929. ret = crypto_shash_update(ctx->shash, tmpbuf, req->nbytes);
  1930. kfree(tmpbuf);
  1931. } else {
  1932. /* Otherwise call the internal function which uses SPU hw */
  1933. ret = __ahash_update(req);
  1934. }
  1935. return ret;
  1936. }
  1937. static int __ahash_final(struct ahash_request *req)
  1938. {
  1939. struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
  1940. flow_log("ahash_final() nbytes:%u\n", req->nbytes);
  1941. rctx->is_final = 1;
  1942. return ahash_enqueue(req);
  1943. }
  1944. static int ahash_final(struct ahash_request *req)
  1945. {
  1946. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1947. struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
  1948. int ret;
  1949. if (spu_no_incr_hash(ctx)) {
  1950. /*
  1951. * If we get an incremental hashing request and it's not
  1952. * supported by the hardware, we need to handle it in software
  1953. * by calling synchronous hash functions.
  1954. */
  1955. ret = crypto_shash_final(ctx->shash, req->result);
  1956. /* Done with hash, can deallocate it now */
  1957. crypto_free_shash(ctx->shash->tfm);
  1958. kfree(ctx->shash);
  1959. } else {
  1960. /* Otherwise call the internal function which uses SPU hw */
  1961. ret = __ahash_final(req);
  1962. }
  1963. return ret;
  1964. }
  1965. static int __ahash_finup(struct ahash_request *req)
  1966. {
  1967. struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
  1968. flow_log("ahash_finup() nbytes:%u\n", req->nbytes);
  1969. rctx->total_todo += req->nbytes;
  1970. rctx->src_sent = 0;
  1971. rctx->is_final = 1;
  1972. return ahash_enqueue(req);
  1973. }
  1974. static int ahash_finup(struct ahash_request *req)
  1975. {
  1976. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1977. struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
  1978. u8 *tmpbuf;
  1979. int ret;
  1980. int nents;
  1981. gfp_t gfp;
  1982. if (spu_no_incr_hash(ctx)) {
  1983. /*
  1984. * If we get an incremental hashing request and it's not
  1985. * supported by the hardware, we need to handle it in software
  1986. * by calling synchronous hash functions.
  1987. */
  1988. if (req->src) {
  1989. nents = sg_nents(req->src);
  1990. } else {
  1991. ret = -EINVAL;
  1992. goto ahash_finup_exit;
  1993. }
  1994. /* Copy data from req scatterlist to tmp buffer */
  1995. gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
  1996. CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
  1997. tmpbuf = kmalloc(req->nbytes, gfp);
  1998. if (!tmpbuf) {
  1999. ret = -ENOMEM;
  2000. goto ahash_finup_exit;
  2001. }
  2002. if (sg_copy_to_buffer(req->src, nents, tmpbuf, req->nbytes) !=
  2003. req->nbytes) {
  2004. ret = -EINVAL;
  2005. goto ahash_finup_free;
  2006. }
  2007. /* Call synchronous update */
  2008. ret = crypto_shash_finup(ctx->shash, tmpbuf, req->nbytes,
  2009. req->result);
  2010. } else {
  2011. /* Otherwise call the internal function which uses SPU hw */
  2012. return __ahash_finup(req);
  2013. }
  2014. ahash_finup_free:
  2015. kfree(tmpbuf);
  2016. ahash_finup_exit:
  2017. /* Done with hash, can deallocate it now */
  2018. crypto_free_shash(ctx->shash->tfm);
  2019. kfree(ctx->shash);
  2020. return ret;
  2021. }
  2022. static int ahash_digest(struct ahash_request *req)
  2023. {
  2024. int err = 0;
  2025. flow_log("ahash_digest() nbytes:%u\n", req->nbytes);
  2026. /* whole thing at once */
  2027. err = __ahash_init(req);
  2028. if (!err)
  2029. err = __ahash_finup(req);
  2030. return err;
  2031. }
  2032. static int ahash_setkey(struct crypto_ahash *ahash, const u8 *key,
  2033. unsigned int keylen)
  2034. {
  2035. struct iproc_ctx_s *ctx = crypto_ahash_ctx(ahash);
  2036. flow_log("%s() ahash:%p key:%p keylen:%u\n",
  2037. __func__, ahash, key, keylen);
  2038. flow_dump(" key: ", key, keylen);
  2039. if (ctx->auth.alg == HASH_ALG_AES) {
  2040. switch (keylen) {
  2041. case AES_KEYSIZE_128:
  2042. ctx->cipher_type = CIPHER_TYPE_AES128;
  2043. break;
  2044. case AES_KEYSIZE_192:
  2045. ctx->cipher_type = CIPHER_TYPE_AES192;
  2046. break;
  2047. case AES_KEYSIZE_256:
  2048. ctx->cipher_type = CIPHER_TYPE_AES256;
  2049. break;
  2050. default:
  2051. pr_err("%s() Error: Invalid key length\n", __func__);
  2052. return -EINVAL;
  2053. }
  2054. } else {
  2055. pr_err("%s() Error: unknown hash alg\n", __func__);
  2056. return -EINVAL;
  2057. }
  2058. memcpy(ctx->authkey, key, keylen);
  2059. ctx->authkeylen = keylen;
  2060. return 0;
  2061. }
  2062. static int ahash_export(struct ahash_request *req, void *out)
  2063. {
  2064. const struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
  2065. struct spu_hash_export_s *spu_exp = (struct spu_hash_export_s *)out;
  2066. spu_exp->total_todo = rctx->total_todo;
  2067. spu_exp->total_sent = rctx->total_sent;
  2068. spu_exp->is_sw_hmac = rctx->is_sw_hmac;
  2069. memcpy(spu_exp->hash_carry, rctx->hash_carry, sizeof(rctx->hash_carry));
  2070. spu_exp->hash_carry_len = rctx->hash_carry_len;
  2071. memcpy(spu_exp->incr_hash, rctx->incr_hash, sizeof(rctx->incr_hash));
  2072. return 0;
  2073. }
  2074. static int ahash_import(struct ahash_request *req, const void *in)
  2075. {
  2076. struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
  2077. struct spu_hash_export_s *spu_exp = (struct spu_hash_export_s *)in;
  2078. rctx->total_todo = spu_exp->total_todo;
  2079. rctx->total_sent = spu_exp->total_sent;
  2080. rctx->is_sw_hmac = spu_exp->is_sw_hmac;
  2081. memcpy(rctx->hash_carry, spu_exp->hash_carry, sizeof(rctx->hash_carry));
  2082. rctx->hash_carry_len = spu_exp->hash_carry_len;
  2083. memcpy(rctx->incr_hash, spu_exp->incr_hash, sizeof(rctx->incr_hash));
  2084. return 0;
  2085. }
  2086. static int ahash_hmac_setkey(struct crypto_ahash *ahash, const u8 *key,
  2087. unsigned int keylen)
  2088. {
  2089. struct iproc_ctx_s *ctx = crypto_ahash_ctx(ahash);
  2090. unsigned int blocksize =
  2091. crypto_tfm_alg_blocksize(crypto_ahash_tfm(ahash));
  2092. unsigned int digestsize = crypto_ahash_digestsize(ahash);
  2093. unsigned int index;
  2094. int rc;
  2095. flow_log("%s() ahash:%p key:%p keylen:%u blksz:%u digestsz:%u\n",
  2096. __func__, ahash, key, keylen, blocksize, digestsize);
  2097. flow_dump(" key: ", key, keylen);
  2098. if (keylen > blocksize) {
  2099. switch (ctx->auth.alg) {
  2100. case HASH_ALG_MD5:
  2101. rc = do_shash("md5", ctx->authkey, key, keylen, NULL,
  2102. 0, NULL, 0);
  2103. break;
  2104. case HASH_ALG_SHA1:
  2105. rc = do_shash("sha1", ctx->authkey, key, keylen, NULL,
  2106. 0, NULL, 0);
  2107. break;
  2108. case HASH_ALG_SHA224:
  2109. rc = do_shash("sha224", ctx->authkey, key, keylen, NULL,
  2110. 0, NULL, 0);
  2111. break;
  2112. case HASH_ALG_SHA256:
  2113. rc = do_shash("sha256", ctx->authkey, key, keylen, NULL,
  2114. 0, NULL, 0);
  2115. break;
  2116. case HASH_ALG_SHA384:
  2117. rc = do_shash("sha384", ctx->authkey, key, keylen, NULL,
  2118. 0, NULL, 0);
  2119. break;
  2120. case HASH_ALG_SHA512:
  2121. rc = do_shash("sha512", ctx->authkey, key, keylen, NULL,
  2122. 0, NULL, 0);
  2123. break;
  2124. case HASH_ALG_SHA3_224:
  2125. rc = do_shash("sha3-224", ctx->authkey, key, keylen,
  2126. NULL, 0, NULL, 0);
  2127. break;
  2128. case HASH_ALG_SHA3_256:
  2129. rc = do_shash("sha3-256", ctx->authkey, key, keylen,
  2130. NULL, 0, NULL, 0);
  2131. break;
  2132. case HASH_ALG_SHA3_384:
  2133. rc = do_shash("sha3-384", ctx->authkey, key, keylen,
  2134. NULL, 0, NULL, 0);
  2135. break;
  2136. case HASH_ALG_SHA3_512:
  2137. rc = do_shash("sha3-512", ctx->authkey, key, keylen,
  2138. NULL, 0, NULL, 0);
  2139. break;
  2140. default:
  2141. pr_err("%s() Error: unknown hash alg\n", __func__);
  2142. return -EINVAL;
  2143. }
  2144. if (rc < 0) {
  2145. pr_err("%s() Error %d computing shash for %s\n",
  2146. __func__, rc, hash_alg_name[ctx->auth.alg]);
  2147. return rc;
  2148. }
  2149. ctx->authkeylen = digestsize;
  2150. flow_log(" keylen > digestsize... hashed\n");
  2151. flow_dump(" newkey: ", ctx->authkey, ctx->authkeylen);
  2152. } else {
  2153. memcpy(ctx->authkey, key, keylen);
  2154. ctx->authkeylen = keylen;
  2155. }
  2156. /*
  2157. * Full HMAC operation in SPUM is not verified,
  2158. * So keeping the generation of IPAD, OPAD and
  2159. * outer hashing in software.
  2160. */
  2161. if (iproc_priv.spu.spu_type == SPU_TYPE_SPUM) {
  2162. memcpy(ctx->ipad, ctx->authkey, ctx->authkeylen);
  2163. memset(ctx->ipad + ctx->authkeylen, 0,
  2164. blocksize - ctx->authkeylen);
  2165. ctx->authkeylen = 0;
  2166. memcpy(ctx->opad, ctx->ipad, blocksize);
  2167. for (index = 0; index < blocksize; index++) {
  2168. ctx->ipad[index] ^= HMAC_IPAD_VALUE;
  2169. ctx->opad[index] ^= HMAC_OPAD_VALUE;
  2170. }
  2171. flow_dump(" ipad: ", ctx->ipad, blocksize);
  2172. flow_dump(" opad: ", ctx->opad, blocksize);
  2173. }
  2174. ctx->digestsize = digestsize;
  2175. atomic_inc(&iproc_priv.setkey_cnt[SPU_OP_HMAC]);
  2176. return 0;
  2177. }
  2178. static int ahash_hmac_init(struct ahash_request *req)
  2179. {
  2180. struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
  2181. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  2182. struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
  2183. unsigned int blocksize =
  2184. crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
  2185. flow_log("ahash_hmac_init()\n");
  2186. /* init the context as a hash */
  2187. ahash_init(req);
  2188. if (!spu_no_incr_hash(ctx)) {
  2189. /* SPU-M can do incr hashing but needs sw for outer HMAC */
  2190. rctx->is_sw_hmac = true;
  2191. ctx->auth.mode = HASH_MODE_HASH;
  2192. /* start with a prepended ipad */
  2193. memcpy(rctx->hash_carry, ctx->ipad, blocksize);
  2194. rctx->hash_carry_len = blocksize;
  2195. rctx->total_todo += blocksize;
  2196. }
  2197. return 0;
  2198. }
  2199. static int ahash_hmac_update(struct ahash_request *req)
  2200. {
  2201. flow_log("ahash_hmac_update() nbytes:%u\n", req->nbytes);
  2202. if (!req->nbytes)
  2203. return 0;
  2204. return ahash_update(req);
  2205. }
  2206. static int ahash_hmac_final(struct ahash_request *req)
  2207. {
  2208. flow_log("ahash_hmac_final() nbytes:%u\n", req->nbytes);
  2209. return ahash_final(req);
  2210. }
  2211. static int ahash_hmac_finup(struct ahash_request *req)
  2212. {
  2213. flow_log("ahash_hmac_finupl() nbytes:%u\n", req->nbytes);
  2214. return ahash_finup(req);
  2215. }
  2216. static int ahash_hmac_digest(struct ahash_request *req)
  2217. {
  2218. struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
  2219. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  2220. struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
  2221. unsigned int blocksize =
  2222. crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
  2223. flow_log("ahash_hmac_digest() nbytes:%u\n", req->nbytes);
  2224. /* Perform initialization and then call finup */
  2225. __ahash_init(req);
  2226. if (iproc_priv.spu.spu_type == SPU_TYPE_SPU2) {
  2227. /*
  2228. * SPU2 supports full HMAC implementation in the
  2229. * hardware, need not to generate IPAD, OPAD and
  2230. * outer hash in software.
  2231. * Only for hash key len > hash block size, SPU2
  2232. * expects to perform hashing on the key, shorten
  2233. * it to digest size and feed it as hash key.
  2234. */
  2235. rctx->is_sw_hmac = false;
  2236. ctx->auth.mode = HASH_MODE_HMAC;
  2237. } else {
  2238. rctx->is_sw_hmac = true;
  2239. ctx->auth.mode = HASH_MODE_HASH;
  2240. /* start with a prepended ipad */
  2241. memcpy(rctx->hash_carry, ctx->ipad, blocksize);
  2242. rctx->hash_carry_len = blocksize;
  2243. rctx->total_todo += blocksize;
  2244. }
  2245. return __ahash_finup(req);
  2246. }
  2247. /* aead helpers */
  2248. static int aead_need_fallback(struct aead_request *req)
  2249. {
  2250. struct iproc_reqctx_s *rctx = aead_request_ctx(req);
  2251. struct spu_hw *spu = &iproc_priv.spu;
  2252. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  2253. struct iproc_ctx_s *ctx = crypto_aead_ctx(aead);
  2254. u32 payload_len;
  2255. /*
  2256. * SPU hardware cannot handle the AES-GCM/CCM case where plaintext
  2257. * and AAD are both 0 bytes long. So use fallback in this case.
  2258. */
  2259. if (((ctx->cipher.mode == CIPHER_MODE_GCM) ||
  2260. (ctx->cipher.mode == CIPHER_MODE_CCM)) &&
  2261. (req->assoclen == 0)) {
  2262. if ((rctx->is_encrypt && (req->cryptlen == 0)) ||
  2263. (!rctx->is_encrypt && (req->cryptlen == ctx->digestsize))) {
  2264. flow_log("AES GCM/CCM needs fallback for 0 len req\n");
  2265. return 1;
  2266. }
  2267. }
  2268. /* SPU-M hardware only supports CCM digest size of 8, 12, or 16 bytes */
  2269. if ((ctx->cipher.mode == CIPHER_MODE_CCM) &&
  2270. (spu->spu_type == SPU_TYPE_SPUM) &&
  2271. (ctx->digestsize != 8) && (ctx->digestsize != 12) &&
  2272. (ctx->digestsize != 16)) {
  2273. flow_log("%s() AES CCM needs fallback for digest size %d\n",
  2274. __func__, ctx->digestsize);
  2275. return 1;
  2276. }
  2277. /*
  2278. * SPU-M on NSP has an issue where AES-CCM hash is not correct
  2279. * when AAD size is 0
  2280. */
  2281. if ((ctx->cipher.mode == CIPHER_MODE_CCM) &&
  2282. (spu->spu_subtype == SPU_SUBTYPE_SPUM_NSP) &&
  2283. (req->assoclen == 0)) {
  2284. flow_log("%s() AES_CCM needs fallback for 0 len AAD on NSP\n",
  2285. __func__);
  2286. return 1;
  2287. }
  2288. payload_len = req->cryptlen;
  2289. if (spu->spu_type == SPU_TYPE_SPUM)
  2290. payload_len += req->assoclen;
  2291. flow_log("%s() payload len: %u\n", __func__, payload_len);
  2292. if (ctx->max_payload == SPU_MAX_PAYLOAD_INF)
  2293. return 0;
  2294. else
  2295. return payload_len > ctx->max_payload;
  2296. }
  2297. static void aead_complete(struct crypto_async_request *areq, int err)
  2298. {
  2299. struct aead_request *req =
  2300. container_of(areq, struct aead_request, base);
  2301. struct iproc_reqctx_s *rctx = aead_request_ctx(req);
  2302. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  2303. flow_log("%s() err:%d\n", __func__, err);
  2304. areq->tfm = crypto_aead_tfm(aead);
  2305. areq->complete = rctx->old_complete;
  2306. areq->data = rctx->old_data;
  2307. areq->complete(areq, err);
  2308. }
  2309. static int aead_do_fallback(struct aead_request *req, bool is_encrypt)
  2310. {
  2311. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  2312. struct crypto_tfm *tfm = crypto_aead_tfm(aead);
  2313. struct iproc_reqctx_s *rctx = aead_request_ctx(req);
  2314. struct iproc_ctx_s *ctx = crypto_tfm_ctx(tfm);
  2315. int err;
  2316. u32 req_flags;
  2317. flow_log("%s() enc:%u\n", __func__, is_encrypt);
  2318. if (ctx->fallback_cipher) {
  2319. /* Store the cipher tfm and then use the fallback tfm */
  2320. rctx->old_tfm = tfm;
  2321. aead_request_set_tfm(req, ctx->fallback_cipher);
  2322. /*
  2323. * Save the callback and chain ourselves in, so we can restore
  2324. * the tfm
  2325. */
  2326. rctx->old_complete = req->base.complete;
  2327. rctx->old_data = req->base.data;
  2328. req_flags = aead_request_flags(req);
  2329. aead_request_set_callback(req, req_flags, aead_complete, req);
  2330. err = is_encrypt ? crypto_aead_encrypt(req) :
  2331. crypto_aead_decrypt(req);
  2332. if (err == 0) {
  2333. /*
  2334. * fallback was synchronous (did not return
  2335. * -EINPROGRESS). So restore request state here.
  2336. */
  2337. aead_request_set_callback(req, req_flags,
  2338. rctx->old_complete, req);
  2339. req->base.data = rctx->old_data;
  2340. aead_request_set_tfm(req, aead);
  2341. flow_log("%s() fallback completed successfully\n\n",
  2342. __func__);
  2343. }
  2344. } else {
  2345. err = -EINVAL;
  2346. }
  2347. return err;
  2348. }
  2349. static int aead_enqueue(struct aead_request *req, bool is_encrypt)
  2350. {
  2351. struct iproc_reqctx_s *rctx = aead_request_ctx(req);
  2352. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  2353. struct iproc_ctx_s *ctx = crypto_aead_ctx(aead);
  2354. int err;
  2355. flow_log("%s() enc:%u\n", __func__, is_encrypt);
  2356. if (req->assoclen > MAX_ASSOC_SIZE) {
  2357. pr_err
  2358. ("%s() Error: associated data too long. (%u > %u bytes)\n",
  2359. __func__, req->assoclen, MAX_ASSOC_SIZE);
  2360. return -EINVAL;
  2361. }
  2362. rctx->gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
  2363. CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
  2364. rctx->parent = &req->base;
  2365. rctx->is_encrypt = is_encrypt;
  2366. rctx->bd_suppress = false;
  2367. rctx->total_todo = req->cryptlen;
  2368. rctx->src_sent = 0;
  2369. rctx->total_sent = 0;
  2370. rctx->total_received = 0;
  2371. rctx->is_sw_hmac = false;
  2372. rctx->ctx = ctx;
  2373. memset(&rctx->mb_mssg, 0, sizeof(struct brcm_message));
  2374. /* assoc data is at start of src sg */
  2375. rctx->assoc = req->src;
  2376. /*
  2377. * Init current position in src scatterlist to be after assoc data.
  2378. * src_skip set to buffer offset where data begins. (Assoc data could
  2379. * end in the middle of a buffer.)
  2380. */
  2381. if (spu_sg_at_offset(req->src, req->assoclen, &rctx->src_sg,
  2382. &rctx->src_skip) < 0) {
  2383. pr_err("%s() Error: Unable to find start of src data\n",
  2384. __func__);
  2385. return -EINVAL;
  2386. }
  2387. rctx->src_nents = 0;
  2388. rctx->dst_nents = 0;
  2389. if (req->dst == req->src) {
  2390. rctx->dst_sg = rctx->src_sg;
  2391. rctx->dst_skip = rctx->src_skip;
  2392. } else {
  2393. /*
  2394. * Expect req->dst to have room for assoc data followed by
  2395. * output data and ICV, if encrypt. So initialize dst_sg
  2396. * to point beyond assoc len offset.
  2397. */
  2398. if (spu_sg_at_offset(req->dst, req->assoclen, &rctx->dst_sg,
  2399. &rctx->dst_skip) < 0) {
  2400. pr_err("%s() Error: Unable to find start of dst data\n",
  2401. __func__);
  2402. return -EINVAL;
  2403. }
  2404. }
  2405. if (ctx->cipher.mode == CIPHER_MODE_CBC ||
  2406. ctx->cipher.mode == CIPHER_MODE_CTR ||
  2407. ctx->cipher.mode == CIPHER_MODE_OFB ||
  2408. ctx->cipher.mode == CIPHER_MODE_XTS ||
  2409. ctx->cipher.mode == CIPHER_MODE_GCM) {
  2410. rctx->iv_ctr_len =
  2411. ctx->salt_len +
  2412. crypto_aead_ivsize(crypto_aead_reqtfm(req));
  2413. } else if (ctx->cipher.mode == CIPHER_MODE_CCM) {
  2414. rctx->iv_ctr_len = CCM_AES_IV_SIZE;
  2415. } else {
  2416. rctx->iv_ctr_len = 0;
  2417. }
  2418. rctx->hash_carry_len = 0;
  2419. flow_log(" src sg: %p\n", req->src);
  2420. flow_log(" rctx->src_sg: %p, src_skip %u\n",
  2421. rctx->src_sg, rctx->src_skip);
  2422. flow_log(" assoc: %p, assoclen %u\n", rctx->assoc, req->assoclen);
  2423. flow_log(" dst sg: %p\n", req->dst);
  2424. flow_log(" rctx->dst_sg: %p, dst_skip %u\n",
  2425. rctx->dst_sg, rctx->dst_skip);
  2426. flow_log(" iv_ctr_len:%u\n", rctx->iv_ctr_len);
  2427. flow_dump(" iv: ", req->iv, rctx->iv_ctr_len);
  2428. flow_log(" authkeylen:%u\n", ctx->authkeylen);
  2429. flow_log(" is_esp: %s\n", ctx->is_esp ? "yes" : "no");
  2430. if (ctx->max_payload == SPU_MAX_PAYLOAD_INF)
  2431. flow_log(" max_payload infinite");
  2432. else
  2433. flow_log(" max_payload: %u\n", ctx->max_payload);
  2434. if (unlikely(aead_need_fallback(req)))
  2435. return aead_do_fallback(req, is_encrypt);
  2436. /*
  2437. * Do memory allocations for request after fallback check, because if we
  2438. * do fallback, we won't call finish_req() to dealloc.
  2439. */
  2440. if (rctx->iv_ctr_len) {
  2441. if (ctx->salt_len)
  2442. memcpy(rctx->msg_buf.iv_ctr + ctx->salt_offset,
  2443. ctx->salt, ctx->salt_len);
  2444. memcpy(rctx->msg_buf.iv_ctr + ctx->salt_offset + ctx->salt_len,
  2445. req->iv,
  2446. rctx->iv_ctr_len - ctx->salt_len - ctx->salt_offset);
  2447. }
  2448. rctx->chan_idx = select_channel();
  2449. err = handle_aead_req(rctx);
  2450. if (err != -EINPROGRESS)
  2451. /* synchronous result */
  2452. spu_chunk_cleanup(rctx);
  2453. return err;
  2454. }
  2455. static int aead_authenc_setkey(struct crypto_aead *cipher,
  2456. const u8 *key, unsigned int keylen)
  2457. {
  2458. struct spu_hw *spu = &iproc_priv.spu;
  2459. struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
  2460. struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
  2461. struct rtattr *rta = (void *)key;
  2462. struct crypto_authenc_key_param *param;
  2463. const u8 *origkey = key;
  2464. const unsigned int origkeylen = keylen;
  2465. int ret = 0;
  2466. flow_log("%s() aead:%p key:%p keylen:%u\n", __func__, cipher, key,
  2467. keylen);
  2468. flow_dump(" key: ", key, keylen);
  2469. if (!RTA_OK(rta, keylen))
  2470. goto badkey;
  2471. if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
  2472. goto badkey;
  2473. if (RTA_PAYLOAD(rta) < sizeof(*param))
  2474. goto badkey;
  2475. param = RTA_DATA(rta);
  2476. ctx->enckeylen = be32_to_cpu(param->enckeylen);
  2477. key += RTA_ALIGN(rta->rta_len);
  2478. keylen -= RTA_ALIGN(rta->rta_len);
  2479. if (keylen < ctx->enckeylen)
  2480. goto badkey;
  2481. if (ctx->enckeylen > MAX_KEY_SIZE)
  2482. goto badkey;
  2483. ctx->authkeylen = keylen - ctx->enckeylen;
  2484. if (ctx->authkeylen > MAX_KEY_SIZE)
  2485. goto badkey;
  2486. memcpy(ctx->enckey, key + ctx->authkeylen, ctx->enckeylen);
  2487. /* May end up padding auth key. So make sure it's zeroed. */
  2488. memset(ctx->authkey, 0, sizeof(ctx->authkey));
  2489. memcpy(ctx->authkey, key, ctx->authkeylen);
  2490. switch (ctx->alg->cipher_info.alg) {
  2491. case CIPHER_ALG_DES:
  2492. if (ctx->enckeylen == DES_KEY_SIZE) {
  2493. u32 tmp[DES_EXPKEY_WORDS];
  2494. u32 flags = CRYPTO_TFM_RES_WEAK_KEY;
  2495. if (des_ekey(tmp, key) == 0) {
  2496. if (crypto_aead_get_flags(cipher) &
  2497. CRYPTO_TFM_REQ_WEAK_KEY) {
  2498. crypto_aead_set_flags(cipher, flags);
  2499. return -EINVAL;
  2500. }
  2501. }
  2502. ctx->cipher_type = CIPHER_TYPE_DES;
  2503. } else {
  2504. goto badkey;
  2505. }
  2506. break;
  2507. case CIPHER_ALG_3DES:
  2508. if (ctx->enckeylen == (DES_KEY_SIZE * 3)) {
  2509. const u32 *K = (const u32 *)key;
  2510. u32 flags = CRYPTO_TFM_RES_BAD_KEY_SCHED;
  2511. if (!((K[0] ^ K[2]) | (K[1] ^ K[3])) ||
  2512. !((K[2] ^ K[4]) | (K[3] ^ K[5]))) {
  2513. crypto_aead_set_flags(cipher, flags);
  2514. return -EINVAL;
  2515. }
  2516. ctx->cipher_type = CIPHER_TYPE_3DES;
  2517. } else {
  2518. crypto_aead_set_flags(cipher,
  2519. CRYPTO_TFM_RES_BAD_KEY_LEN);
  2520. return -EINVAL;
  2521. }
  2522. break;
  2523. case CIPHER_ALG_AES:
  2524. switch (ctx->enckeylen) {
  2525. case AES_KEYSIZE_128:
  2526. ctx->cipher_type = CIPHER_TYPE_AES128;
  2527. break;
  2528. case AES_KEYSIZE_192:
  2529. ctx->cipher_type = CIPHER_TYPE_AES192;
  2530. break;
  2531. case AES_KEYSIZE_256:
  2532. ctx->cipher_type = CIPHER_TYPE_AES256;
  2533. break;
  2534. default:
  2535. goto badkey;
  2536. }
  2537. break;
  2538. case CIPHER_ALG_RC4:
  2539. ctx->cipher_type = CIPHER_TYPE_INIT;
  2540. break;
  2541. default:
  2542. pr_err("%s() Error: Unknown cipher alg\n", __func__);
  2543. return -EINVAL;
  2544. }
  2545. flow_log(" enckeylen:%u authkeylen:%u\n", ctx->enckeylen,
  2546. ctx->authkeylen);
  2547. flow_dump(" enc: ", ctx->enckey, ctx->enckeylen);
  2548. flow_dump(" auth: ", ctx->authkey, ctx->authkeylen);
  2549. /* setkey the fallback just in case we needto use it */
  2550. if (ctx->fallback_cipher) {
  2551. flow_log(" running fallback setkey()\n");
  2552. ctx->fallback_cipher->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
  2553. ctx->fallback_cipher->base.crt_flags |=
  2554. tfm->crt_flags & CRYPTO_TFM_REQ_MASK;
  2555. ret =
  2556. crypto_aead_setkey(ctx->fallback_cipher, origkey,
  2557. origkeylen);
  2558. if (ret) {
  2559. flow_log(" fallback setkey() returned:%d\n", ret);
  2560. tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
  2561. tfm->crt_flags |=
  2562. (ctx->fallback_cipher->base.crt_flags &
  2563. CRYPTO_TFM_RES_MASK);
  2564. }
  2565. }
  2566. ctx->spu_resp_hdr_len = spu->spu_response_hdr_len(ctx->authkeylen,
  2567. ctx->enckeylen,
  2568. false);
  2569. atomic_inc(&iproc_priv.setkey_cnt[SPU_OP_AEAD]);
  2570. return ret;
  2571. badkey:
  2572. ctx->enckeylen = 0;
  2573. ctx->authkeylen = 0;
  2574. ctx->digestsize = 0;
  2575. crypto_aead_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  2576. return -EINVAL;
  2577. }
  2578. static int aead_gcm_ccm_setkey(struct crypto_aead *cipher,
  2579. const u8 *key, unsigned int keylen)
  2580. {
  2581. struct spu_hw *spu = &iproc_priv.spu;
  2582. struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
  2583. struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
  2584. int ret = 0;
  2585. flow_log("%s() keylen:%u\n", __func__, keylen);
  2586. flow_dump(" key: ", key, keylen);
  2587. if (!ctx->is_esp)
  2588. ctx->digestsize = keylen;
  2589. ctx->enckeylen = keylen;
  2590. ctx->authkeylen = 0;
  2591. memcpy(ctx->enckey, key, ctx->enckeylen);
  2592. switch (ctx->enckeylen) {
  2593. case AES_KEYSIZE_128:
  2594. ctx->cipher_type = CIPHER_TYPE_AES128;
  2595. break;
  2596. case AES_KEYSIZE_192:
  2597. ctx->cipher_type = CIPHER_TYPE_AES192;
  2598. break;
  2599. case AES_KEYSIZE_256:
  2600. ctx->cipher_type = CIPHER_TYPE_AES256;
  2601. break;
  2602. default:
  2603. goto badkey;
  2604. }
  2605. flow_log(" enckeylen:%u authkeylen:%u\n", ctx->enckeylen,
  2606. ctx->authkeylen);
  2607. flow_dump(" enc: ", ctx->enckey, ctx->enckeylen);
  2608. flow_dump(" auth: ", ctx->authkey, ctx->authkeylen);
  2609. /* setkey the fallback just in case we need to use it */
  2610. if (ctx->fallback_cipher) {
  2611. flow_log(" running fallback setkey()\n");
  2612. ctx->fallback_cipher->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
  2613. ctx->fallback_cipher->base.crt_flags |=
  2614. tfm->crt_flags & CRYPTO_TFM_REQ_MASK;
  2615. ret = crypto_aead_setkey(ctx->fallback_cipher, key,
  2616. keylen + ctx->salt_len);
  2617. if (ret) {
  2618. flow_log(" fallback setkey() returned:%d\n", ret);
  2619. tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
  2620. tfm->crt_flags |=
  2621. (ctx->fallback_cipher->base.crt_flags &
  2622. CRYPTO_TFM_RES_MASK);
  2623. }
  2624. }
  2625. ctx->spu_resp_hdr_len = spu->spu_response_hdr_len(ctx->authkeylen,
  2626. ctx->enckeylen,
  2627. false);
  2628. atomic_inc(&iproc_priv.setkey_cnt[SPU_OP_AEAD]);
  2629. flow_log(" enckeylen:%u authkeylen:%u\n", ctx->enckeylen,
  2630. ctx->authkeylen);
  2631. return ret;
  2632. badkey:
  2633. ctx->enckeylen = 0;
  2634. ctx->authkeylen = 0;
  2635. ctx->digestsize = 0;
  2636. crypto_aead_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  2637. return -EINVAL;
  2638. }
  2639. /**
  2640. * aead_gcm_esp_setkey() - setkey() operation for ESP variant of GCM AES.
  2641. * @cipher: AEAD structure
  2642. * @key: Key followed by 4 bytes of salt
  2643. * @keylen: Length of key plus salt, in bytes
  2644. *
  2645. * Extracts salt from key and stores it to be prepended to IV on each request.
  2646. * Digest is always 16 bytes
  2647. *
  2648. * Return: Value from generic gcm setkey.
  2649. */
  2650. static int aead_gcm_esp_setkey(struct crypto_aead *cipher,
  2651. const u8 *key, unsigned int keylen)
  2652. {
  2653. struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
  2654. flow_log("%s\n", __func__);
  2655. ctx->salt_len = GCM_ESP_SALT_SIZE;
  2656. ctx->salt_offset = GCM_ESP_SALT_OFFSET;
  2657. memcpy(ctx->salt, key + keylen - GCM_ESP_SALT_SIZE, GCM_ESP_SALT_SIZE);
  2658. keylen -= GCM_ESP_SALT_SIZE;
  2659. ctx->digestsize = GCM_ESP_DIGESTSIZE;
  2660. ctx->is_esp = true;
  2661. flow_dump("salt: ", ctx->salt, GCM_ESP_SALT_SIZE);
  2662. return aead_gcm_ccm_setkey(cipher, key, keylen);
  2663. }
  2664. /**
  2665. * rfc4543_gcm_esp_setkey() - setkey operation for RFC4543 variant of GCM/GMAC.
  2666. * cipher: AEAD structure
  2667. * key: Key followed by 4 bytes of salt
  2668. * keylen: Length of key plus salt, in bytes
  2669. *
  2670. * Extracts salt from key and stores it to be prepended to IV on each request.
  2671. * Digest is always 16 bytes
  2672. *
  2673. * Return: Value from generic gcm setkey.
  2674. */
  2675. static int rfc4543_gcm_esp_setkey(struct crypto_aead *cipher,
  2676. const u8 *key, unsigned int keylen)
  2677. {
  2678. struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
  2679. flow_log("%s\n", __func__);
  2680. ctx->salt_len = GCM_ESP_SALT_SIZE;
  2681. ctx->salt_offset = GCM_ESP_SALT_OFFSET;
  2682. memcpy(ctx->salt, key + keylen - GCM_ESP_SALT_SIZE, GCM_ESP_SALT_SIZE);
  2683. keylen -= GCM_ESP_SALT_SIZE;
  2684. ctx->digestsize = GCM_ESP_DIGESTSIZE;
  2685. ctx->is_esp = true;
  2686. ctx->is_rfc4543 = true;
  2687. flow_dump("salt: ", ctx->salt, GCM_ESP_SALT_SIZE);
  2688. return aead_gcm_ccm_setkey(cipher, key, keylen);
  2689. }
  2690. /**
  2691. * aead_ccm_esp_setkey() - setkey() operation for ESP variant of CCM AES.
  2692. * @cipher: AEAD structure
  2693. * @key: Key followed by 4 bytes of salt
  2694. * @keylen: Length of key plus salt, in bytes
  2695. *
  2696. * Extracts salt from key and stores it to be prepended to IV on each request.
  2697. * Digest is always 16 bytes
  2698. *
  2699. * Return: Value from generic ccm setkey.
  2700. */
  2701. static int aead_ccm_esp_setkey(struct crypto_aead *cipher,
  2702. const u8 *key, unsigned int keylen)
  2703. {
  2704. struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
  2705. flow_log("%s\n", __func__);
  2706. ctx->salt_len = CCM_ESP_SALT_SIZE;
  2707. ctx->salt_offset = CCM_ESP_SALT_OFFSET;
  2708. memcpy(ctx->salt, key + keylen - CCM_ESP_SALT_SIZE, CCM_ESP_SALT_SIZE);
  2709. keylen -= CCM_ESP_SALT_SIZE;
  2710. ctx->is_esp = true;
  2711. flow_dump("salt: ", ctx->salt, CCM_ESP_SALT_SIZE);
  2712. return aead_gcm_ccm_setkey(cipher, key, keylen);
  2713. }
  2714. static int aead_setauthsize(struct crypto_aead *cipher, unsigned int authsize)
  2715. {
  2716. struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
  2717. int ret = 0;
  2718. flow_log("%s() authkeylen:%u authsize:%u\n",
  2719. __func__, ctx->authkeylen, authsize);
  2720. ctx->digestsize = authsize;
  2721. /* setkey the fallback just in case we needto use it */
  2722. if (ctx->fallback_cipher) {
  2723. flow_log(" running fallback setauth()\n");
  2724. ret = crypto_aead_setauthsize(ctx->fallback_cipher, authsize);
  2725. if (ret)
  2726. flow_log(" fallback setauth() returned:%d\n", ret);
  2727. }
  2728. return ret;
  2729. }
  2730. static int aead_encrypt(struct aead_request *req)
  2731. {
  2732. flow_log("%s() cryptlen:%u %08x\n", __func__, req->cryptlen,
  2733. req->cryptlen);
  2734. dump_sg(req->src, 0, req->cryptlen + req->assoclen);
  2735. flow_log(" assoc_len:%u\n", req->assoclen);
  2736. return aead_enqueue(req, true);
  2737. }
  2738. static int aead_decrypt(struct aead_request *req)
  2739. {
  2740. flow_log("%s() cryptlen:%u\n", __func__, req->cryptlen);
  2741. dump_sg(req->src, 0, req->cryptlen + req->assoclen);
  2742. flow_log(" assoc_len:%u\n", req->assoclen);
  2743. return aead_enqueue(req, false);
  2744. }
  2745. /* ==================== Supported Cipher Algorithms ==================== */
  2746. static struct iproc_alg_s driver_algs[] = {
  2747. {
  2748. .type = CRYPTO_ALG_TYPE_AEAD,
  2749. .alg.aead = {
  2750. .base = {
  2751. .cra_name = "gcm(aes)",
  2752. .cra_driver_name = "gcm-aes-iproc",
  2753. .cra_blocksize = AES_BLOCK_SIZE,
  2754. .cra_flags = CRYPTO_ALG_NEED_FALLBACK
  2755. },
  2756. .setkey = aead_gcm_ccm_setkey,
  2757. .ivsize = GCM_AES_IV_SIZE,
  2758. .maxauthsize = AES_BLOCK_SIZE,
  2759. },
  2760. .cipher_info = {
  2761. .alg = CIPHER_ALG_AES,
  2762. .mode = CIPHER_MODE_GCM,
  2763. },
  2764. .auth_info = {
  2765. .alg = HASH_ALG_AES,
  2766. .mode = HASH_MODE_GCM,
  2767. },
  2768. .auth_first = 0,
  2769. },
  2770. {
  2771. .type = CRYPTO_ALG_TYPE_AEAD,
  2772. .alg.aead = {
  2773. .base = {
  2774. .cra_name = "ccm(aes)",
  2775. .cra_driver_name = "ccm-aes-iproc",
  2776. .cra_blocksize = AES_BLOCK_SIZE,
  2777. .cra_flags = CRYPTO_ALG_NEED_FALLBACK
  2778. },
  2779. .setkey = aead_gcm_ccm_setkey,
  2780. .ivsize = CCM_AES_IV_SIZE,
  2781. .maxauthsize = AES_BLOCK_SIZE,
  2782. },
  2783. .cipher_info = {
  2784. .alg = CIPHER_ALG_AES,
  2785. .mode = CIPHER_MODE_CCM,
  2786. },
  2787. .auth_info = {
  2788. .alg = HASH_ALG_AES,
  2789. .mode = HASH_MODE_CCM,
  2790. },
  2791. .auth_first = 0,
  2792. },
  2793. {
  2794. .type = CRYPTO_ALG_TYPE_AEAD,
  2795. .alg.aead = {
  2796. .base = {
  2797. .cra_name = "rfc4106(gcm(aes))",
  2798. .cra_driver_name = "gcm-aes-esp-iproc",
  2799. .cra_blocksize = AES_BLOCK_SIZE,
  2800. .cra_flags = CRYPTO_ALG_NEED_FALLBACK
  2801. },
  2802. .setkey = aead_gcm_esp_setkey,
  2803. .ivsize = GCM_RFC4106_IV_SIZE,
  2804. .maxauthsize = AES_BLOCK_SIZE,
  2805. },
  2806. .cipher_info = {
  2807. .alg = CIPHER_ALG_AES,
  2808. .mode = CIPHER_MODE_GCM,
  2809. },
  2810. .auth_info = {
  2811. .alg = HASH_ALG_AES,
  2812. .mode = HASH_MODE_GCM,
  2813. },
  2814. .auth_first = 0,
  2815. },
  2816. {
  2817. .type = CRYPTO_ALG_TYPE_AEAD,
  2818. .alg.aead = {
  2819. .base = {
  2820. .cra_name = "rfc4309(ccm(aes))",
  2821. .cra_driver_name = "ccm-aes-esp-iproc",
  2822. .cra_blocksize = AES_BLOCK_SIZE,
  2823. .cra_flags = CRYPTO_ALG_NEED_FALLBACK
  2824. },
  2825. .setkey = aead_ccm_esp_setkey,
  2826. .ivsize = CCM_AES_IV_SIZE,
  2827. .maxauthsize = AES_BLOCK_SIZE,
  2828. },
  2829. .cipher_info = {
  2830. .alg = CIPHER_ALG_AES,
  2831. .mode = CIPHER_MODE_CCM,
  2832. },
  2833. .auth_info = {
  2834. .alg = HASH_ALG_AES,
  2835. .mode = HASH_MODE_CCM,
  2836. },
  2837. .auth_first = 0,
  2838. },
  2839. {
  2840. .type = CRYPTO_ALG_TYPE_AEAD,
  2841. .alg.aead = {
  2842. .base = {
  2843. .cra_name = "rfc4543(gcm(aes))",
  2844. .cra_driver_name = "gmac-aes-esp-iproc",
  2845. .cra_blocksize = AES_BLOCK_SIZE,
  2846. .cra_flags = CRYPTO_ALG_NEED_FALLBACK
  2847. },
  2848. .setkey = rfc4543_gcm_esp_setkey,
  2849. .ivsize = GCM_RFC4106_IV_SIZE,
  2850. .maxauthsize = AES_BLOCK_SIZE,
  2851. },
  2852. .cipher_info = {
  2853. .alg = CIPHER_ALG_AES,
  2854. .mode = CIPHER_MODE_GCM,
  2855. },
  2856. .auth_info = {
  2857. .alg = HASH_ALG_AES,
  2858. .mode = HASH_MODE_GCM,
  2859. },
  2860. .auth_first = 0,
  2861. },
  2862. {
  2863. .type = CRYPTO_ALG_TYPE_AEAD,
  2864. .alg.aead = {
  2865. .base = {
  2866. .cra_name = "authenc(hmac(md5),cbc(aes))",
  2867. .cra_driver_name = "authenc-hmac-md5-cbc-aes-iproc",
  2868. .cra_blocksize = AES_BLOCK_SIZE,
  2869. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
  2870. },
  2871. .setkey = aead_authenc_setkey,
  2872. .ivsize = AES_BLOCK_SIZE,
  2873. .maxauthsize = MD5_DIGEST_SIZE,
  2874. },
  2875. .cipher_info = {
  2876. .alg = CIPHER_ALG_AES,
  2877. .mode = CIPHER_MODE_CBC,
  2878. },
  2879. .auth_info = {
  2880. .alg = HASH_ALG_MD5,
  2881. .mode = HASH_MODE_HMAC,
  2882. },
  2883. .auth_first = 0,
  2884. },
  2885. {
  2886. .type = CRYPTO_ALG_TYPE_AEAD,
  2887. .alg.aead = {
  2888. .base = {
  2889. .cra_name = "authenc(hmac(sha1),cbc(aes))",
  2890. .cra_driver_name = "authenc-hmac-sha1-cbc-aes-iproc",
  2891. .cra_blocksize = AES_BLOCK_SIZE,
  2892. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
  2893. },
  2894. .setkey = aead_authenc_setkey,
  2895. .ivsize = AES_BLOCK_SIZE,
  2896. .maxauthsize = SHA1_DIGEST_SIZE,
  2897. },
  2898. .cipher_info = {
  2899. .alg = CIPHER_ALG_AES,
  2900. .mode = CIPHER_MODE_CBC,
  2901. },
  2902. .auth_info = {
  2903. .alg = HASH_ALG_SHA1,
  2904. .mode = HASH_MODE_HMAC,
  2905. },
  2906. .auth_first = 0,
  2907. },
  2908. {
  2909. .type = CRYPTO_ALG_TYPE_AEAD,
  2910. .alg.aead = {
  2911. .base = {
  2912. .cra_name = "authenc(hmac(sha256),cbc(aes))",
  2913. .cra_driver_name = "authenc-hmac-sha256-cbc-aes-iproc",
  2914. .cra_blocksize = AES_BLOCK_SIZE,
  2915. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
  2916. },
  2917. .setkey = aead_authenc_setkey,
  2918. .ivsize = AES_BLOCK_SIZE,
  2919. .maxauthsize = SHA256_DIGEST_SIZE,
  2920. },
  2921. .cipher_info = {
  2922. .alg = CIPHER_ALG_AES,
  2923. .mode = CIPHER_MODE_CBC,
  2924. },
  2925. .auth_info = {
  2926. .alg = HASH_ALG_SHA256,
  2927. .mode = HASH_MODE_HMAC,
  2928. },
  2929. .auth_first = 0,
  2930. },
  2931. {
  2932. .type = CRYPTO_ALG_TYPE_AEAD,
  2933. .alg.aead = {
  2934. .base = {
  2935. .cra_name = "authenc(hmac(md5),cbc(des))",
  2936. .cra_driver_name = "authenc-hmac-md5-cbc-des-iproc",
  2937. .cra_blocksize = DES_BLOCK_SIZE,
  2938. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
  2939. },
  2940. .setkey = aead_authenc_setkey,
  2941. .ivsize = DES_BLOCK_SIZE,
  2942. .maxauthsize = MD5_DIGEST_SIZE,
  2943. },
  2944. .cipher_info = {
  2945. .alg = CIPHER_ALG_DES,
  2946. .mode = CIPHER_MODE_CBC,
  2947. },
  2948. .auth_info = {
  2949. .alg = HASH_ALG_MD5,
  2950. .mode = HASH_MODE_HMAC,
  2951. },
  2952. .auth_first = 0,
  2953. },
  2954. {
  2955. .type = CRYPTO_ALG_TYPE_AEAD,
  2956. .alg.aead = {
  2957. .base = {
  2958. .cra_name = "authenc(hmac(sha1),cbc(des))",
  2959. .cra_driver_name = "authenc-hmac-sha1-cbc-des-iproc",
  2960. .cra_blocksize = DES_BLOCK_SIZE,
  2961. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
  2962. },
  2963. .setkey = aead_authenc_setkey,
  2964. .ivsize = DES_BLOCK_SIZE,
  2965. .maxauthsize = SHA1_DIGEST_SIZE,
  2966. },
  2967. .cipher_info = {
  2968. .alg = CIPHER_ALG_DES,
  2969. .mode = CIPHER_MODE_CBC,
  2970. },
  2971. .auth_info = {
  2972. .alg = HASH_ALG_SHA1,
  2973. .mode = HASH_MODE_HMAC,
  2974. },
  2975. .auth_first = 0,
  2976. },
  2977. {
  2978. .type = CRYPTO_ALG_TYPE_AEAD,
  2979. .alg.aead = {
  2980. .base = {
  2981. .cra_name = "authenc(hmac(sha224),cbc(des))",
  2982. .cra_driver_name = "authenc-hmac-sha224-cbc-des-iproc",
  2983. .cra_blocksize = DES_BLOCK_SIZE,
  2984. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
  2985. },
  2986. .setkey = aead_authenc_setkey,
  2987. .ivsize = DES_BLOCK_SIZE,
  2988. .maxauthsize = SHA224_DIGEST_SIZE,
  2989. },
  2990. .cipher_info = {
  2991. .alg = CIPHER_ALG_DES,
  2992. .mode = CIPHER_MODE_CBC,
  2993. },
  2994. .auth_info = {
  2995. .alg = HASH_ALG_SHA224,
  2996. .mode = HASH_MODE_HMAC,
  2997. },
  2998. .auth_first = 0,
  2999. },
  3000. {
  3001. .type = CRYPTO_ALG_TYPE_AEAD,
  3002. .alg.aead = {
  3003. .base = {
  3004. .cra_name = "authenc(hmac(sha256),cbc(des))",
  3005. .cra_driver_name = "authenc-hmac-sha256-cbc-des-iproc",
  3006. .cra_blocksize = DES_BLOCK_SIZE,
  3007. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
  3008. },
  3009. .setkey = aead_authenc_setkey,
  3010. .ivsize = DES_BLOCK_SIZE,
  3011. .maxauthsize = SHA256_DIGEST_SIZE,
  3012. },
  3013. .cipher_info = {
  3014. .alg = CIPHER_ALG_DES,
  3015. .mode = CIPHER_MODE_CBC,
  3016. },
  3017. .auth_info = {
  3018. .alg = HASH_ALG_SHA256,
  3019. .mode = HASH_MODE_HMAC,
  3020. },
  3021. .auth_first = 0,
  3022. },
  3023. {
  3024. .type = CRYPTO_ALG_TYPE_AEAD,
  3025. .alg.aead = {
  3026. .base = {
  3027. .cra_name = "authenc(hmac(sha384),cbc(des))",
  3028. .cra_driver_name = "authenc-hmac-sha384-cbc-des-iproc",
  3029. .cra_blocksize = DES_BLOCK_SIZE,
  3030. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
  3031. },
  3032. .setkey = aead_authenc_setkey,
  3033. .ivsize = DES_BLOCK_SIZE,
  3034. .maxauthsize = SHA384_DIGEST_SIZE,
  3035. },
  3036. .cipher_info = {
  3037. .alg = CIPHER_ALG_DES,
  3038. .mode = CIPHER_MODE_CBC,
  3039. },
  3040. .auth_info = {
  3041. .alg = HASH_ALG_SHA384,
  3042. .mode = HASH_MODE_HMAC,
  3043. },
  3044. .auth_first = 0,
  3045. },
  3046. {
  3047. .type = CRYPTO_ALG_TYPE_AEAD,
  3048. .alg.aead = {
  3049. .base = {
  3050. .cra_name = "authenc(hmac(sha512),cbc(des))",
  3051. .cra_driver_name = "authenc-hmac-sha512-cbc-des-iproc",
  3052. .cra_blocksize = DES_BLOCK_SIZE,
  3053. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
  3054. },
  3055. .setkey = aead_authenc_setkey,
  3056. .ivsize = DES_BLOCK_SIZE,
  3057. .maxauthsize = SHA512_DIGEST_SIZE,
  3058. },
  3059. .cipher_info = {
  3060. .alg = CIPHER_ALG_DES,
  3061. .mode = CIPHER_MODE_CBC,
  3062. },
  3063. .auth_info = {
  3064. .alg = HASH_ALG_SHA512,
  3065. .mode = HASH_MODE_HMAC,
  3066. },
  3067. .auth_first = 0,
  3068. },
  3069. {
  3070. .type = CRYPTO_ALG_TYPE_AEAD,
  3071. .alg.aead = {
  3072. .base = {
  3073. .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
  3074. .cra_driver_name = "authenc-hmac-md5-cbc-des3-iproc",
  3075. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  3076. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
  3077. },
  3078. .setkey = aead_authenc_setkey,
  3079. .ivsize = DES3_EDE_BLOCK_SIZE,
  3080. .maxauthsize = MD5_DIGEST_SIZE,
  3081. },
  3082. .cipher_info = {
  3083. .alg = CIPHER_ALG_3DES,
  3084. .mode = CIPHER_MODE_CBC,
  3085. },
  3086. .auth_info = {
  3087. .alg = HASH_ALG_MD5,
  3088. .mode = HASH_MODE_HMAC,
  3089. },
  3090. .auth_first = 0,
  3091. },
  3092. {
  3093. .type = CRYPTO_ALG_TYPE_AEAD,
  3094. .alg.aead = {
  3095. .base = {
  3096. .cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
  3097. .cra_driver_name = "authenc-hmac-sha1-cbc-des3-iproc",
  3098. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  3099. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
  3100. },
  3101. .setkey = aead_authenc_setkey,
  3102. .ivsize = DES3_EDE_BLOCK_SIZE,
  3103. .maxauthsize = SHA1_DIGEST_SIZE,
  3104. },
  3105. .cipher_info = {
  3106. .alg = CIPHER_ALG_3DES,
  3107. .mode = CIPHER_MODE_CBC,
  3108. },
  3109. .auth_info = {
  3110. .alg = HASH_ALG_SHA1,
  3111. .mode = HASH_MODE_HMAC,
  3112. },
  3113. .auth_first = 0,
  3114. },
  3115. {
  3116. .type = CRYPTO_ALG_TYPE_AEAD,
  3117. .alg.aead = {
  3118. .base = {
  3119. .cra_name = "authenc(hmac(sha224),cbc(des3_ede))",
  3120. .cra_driver_name = "authenc-hmac-sha224-cbc-des3-iproc",
  3121. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  3122. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
  3123. },
  3124. .setkey = aead_authenc_setkey,
  3125. .ivsize = DES3_EDE_BLOCK_SIZE,
  3126. .maxauthsize = SHA224_DIGEST_SIZE,
  3127. },
  3128. .cipher_info = {
  3129. .alg = CIPHER_ALG_3DES,
  3130. .mode = CIPHER_MODE_CBC,
  3131. },
  3132. .auth_info = {
  3133. .alg = HASH_ALG_SHA224,
  3134. .mode = HASH_MODE_HMAC,
  3135. },
  3136. .auth_first = 0,
  3137. },
  3138. {
  3139. .type = CRYPTO_ALG_TYPE_AEAD,
  3140. .alg.aead = {
  3141. .base = {
  3142. .cra_name = "authenc(hmac(sha256),cbc(des3_ede))",
  3143. .cra_driver_name = "authenc-hmac-sha256-cbc-des3-iproc",
  3144. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  3145. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
  3146. },
  3147. .setkey = aead_authenc_setkey,
  3148. .ivsize = DES3_EDE_BLOCK_SIZE,
  3149. .maxauthsize = SHA256_DIGEST_SIZE,
  3150. },
  3151. .cipher_info = {
  3152. .alg = CIPHER_ALG_3DES,
  3153. .mode = CIPHER_MODE_CBC,
  3154. },
  3155. .auth_info = {
  3156. .alg = HASH_ALG_SHA256,
  3157. .mode = HASH_MODE_HMAC,
  3158. },
  3159. .auth_first = 0,
  3160. },
  3161. {
  3162. .type = CRYPTO_ALG_TYPE_AEAD,
  3163. .alg.aead = {
  3164. .base = {
  3165. .cra_name = "authenc(hmac(sha384),cbc(des3_ede))",
  3166. .cra_driver_name = "authenc-hmac-sha384-cbc-des3-iproc",
  3167. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  3168. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
  3169. },
  3170. .setkey = aead_authenc_setkey,
  3171. .ivsize = DES3_EDE_BLOCK_SIZE,
  3172. .maxauthsize = SHA384_DIGEST_SIZE,
  3173. },
  3174. .cipher_info = {
  3175. .alg = CIPHER_ALG_3DES,
  3176. .mode = CIPHER_MODE_CBC,
  3177. },
  3178. .auth_info = {
  3179. .alg = HASH_ALG_SHA384,
  3180. .mode = HASH_MODE_HMAC,
  3181. },
  3182. .auth_first = 0,
  3183. },
  3184. {
  3185. .type = CRYPTO_ALG_TYPE_AEAD,
  3186. .alg.aead = {
  3187. .base = {
  3188. .cra_name = "authenc(hmac(sha512),cbc(des3_ede))",
  3189. .cra_driver_name = "authenc-hmac-sha512-cbc-des3-iproc",
  3190. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  3191. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
  3192. },
  3193. .setkey = aead_authenc_setkey,
  3194. .ivsize = DES3_EDE_BLOCK_SIZE,
  3195. .maxauthsize = SHA512_DIGEST_SIZE,
  3196. },
  3197. .cipher_info = {
  3198. .alg = CIPHER_ALG_3DES,
  3199. .mode = CIPHER_MODE_CBC,
  3200. },
  3201. .auth_info = {
  3202. .alg = HASH_ALG_SHA512,
  3203. .mode = HASH_MODE_HMAC,
  3204. },
  3205. .auth_first = 0,
  3206. },
  3207. /* ABLKCIPHER algorithms. */
  3208. {
  3209. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  3210. .alg.crypto = {
  3211. .cra_name = "ecb(arc4)",
  3212. .cra_driver_name = "ecb-arc4-iproc",
  3213. .cra_blocksize = ARC4_BLOCK_SIZE,
  3214. .cra_ablkcipher = {
  3215. .min_keysize = ARC4_MIN_KEY_SIZE,
  3216. .max_keysize = ARC4_MAX_KEY_SIZE,
  3217. .ivsize = 0,
  3218. }
  3219. },
  3220. .cipher_info = {
  3221. .alg = CIPHER_ALG_RC4,
  3222. .mode = CIPHER_MODE_NONE,
  3223. },
  3224. .auth_info = {
  3225. .alg = HASH_ALG_NONE,
  3226. .mode = HASH_MODE_NONE,
  3227. },
  3228. },
  3229. {
  3230. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  3231. .alg.crypto = {
  3232. .cra_name = "ofb(des)",
  3233. .cra_driver_name = "ofb-des-iproc",
  3234. .cra_blocksize = DES_BLOCK_SIZE,
  3235. .cra_ablkcipher = {
  3236. .min_keysize = DES_KEY_SIZE,
  3237. .max_keysize = DES_KEY_SIZE,
  3238. .ivsize = DES_BLOCK_SIZE,
  3239. }
  3240. },
  3241. .cipher_info = {
  3242. .alg = CIPHER_ALG_DES,
  3243. .mode = CIPHER_MODE_OFB,
  3244. },
  3245. .auth_info = {
  3246. .alg = HASH_ALG_NONE,
  3247. .mode = HASH_MODE_NONE,
  3248. },
  3249. },
  3250. {
  3251. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  3252. .alg.crypto = {
  3253. .cra_name = "cbc(des)",
  3254. .cra_driver_name = "cbc-des-iproc",
  3255. .cra_blocksize = DES_BLOCK_SIZE,
  3256. .cra_ablkcipher = {
  3257. .min_keysize = DES_KEY_SIZE,
  3258. .max_keysize = DES_KEY_SIZE,
  3259. .ivsize = DES_BLOCK_SIZE,
  3260. }
  3261. },
  3262. .cipher_info = {
  3263. .alg = CIPHER_ALG_DES,
  3264. .mode = CIPHER_MODE_CBC,
  3265. },
  3266. .auth_info = {
  3267. .alg = HASH_ALG_NONE,
  3268. .mode = HASH_MODE_NONE,
  3269. },
  3270. },
  3271. {
  3272. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  3273. .alg.crypto = {
  3274. .cra_name = "ecb(des)",
  3275. .cra_driver_name = "ecb-des-iproc",
  3276. .cra_blocksize = DES_BLOCK_SIZE,
  3277. .cra_ablkcipher = {
  3278. .min_keysize = DES_KEY_SIZE,
  3279. .max_keysize = DES_KEY_SIZE,
  3280. .ivsize = 0,
  3281. }
  3282. },
  3283. .cipher_info = {
  3284. .alg = CIPHER_ALG_DES,
  3285. .mode = CIPHER_MODE_ECB,
  3286. },
  3287. .auth_info = {
  3288. .alg = HASH_ALG_NONE,
  3289. .mode = HASH_MODE_NONE,
  3290. },
  3291. },
  3292. {
  3293. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  3294. .alg.crypto = {
  3295. .cra_name = "ofb(des3_ede)",
  3296. .cra_driver_name = "ofb-des3-iproc",
  3297. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  3298. .cra_ablkcipher = {
  3299. .min_keysize = DES3_EDE_KEY_SIZE,
  3300. .max_keysize = DES3_EDE_KEY_SIZE,
  3301. .ivsize = DES3_EDE_BLOCK_SIZE,
  3302. }
  3303. },
  3304. .cipher_info = {
  3305. .alg = CIPHER_ALG_3DES,
  3306. .mode = CIPHER_MODE_OFB,
  3307. },
  3308. .auth_info = {
  3309. .alg = HASH_ALG_NONE,
  3310. .mode = HASH_MODE_NONE,
  3311. },
  3312. },
  3313. {
  3314. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  3315. .alg.crypto = {
  3316. .cra_name = "cbc(des3_ede)",
  3317. .cra_driver_name = "cbc-des3-iproc",
  3318. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  3319. .cra_ablkcipher = {
  3320. .min_keysize = DES3_EDE_KEY_SIZE,
  3321. .max_keysize = DES3_EDE_KEY_SIZE,
  3322. .ivsize = DES3_EDE_BLOCK_SIZE,
  3323. }
  3324. },
  3325. .cipher_info = {
  3326. .alg = CIPHER_ALG_3DES,
  3327. .mode = CIPHER_MODE_CBC,
  3328. },
  3329. .auth_info = {
  3330. .alg = HASH_ALG_NONE,
  3331. .mode = HASH_MODE_NONE,
  3332. },
  3333. },
  3334. {
  3335. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  3336. .alg.crypto = {
  3337. .cra_name = "ecb(des3_ede)",
  3338. .cra_driver_name = "ecb-des3-iproc",
  3339. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  3340. .cra_ablkcipher = {
  3341. .min_keysize = DES3_EDE_KEY_SIZE,
  3342. .max_keysize = DES3_EDE_KEY_SIZE,
  3343. .ivsize = 0,
  3344. }
  3345. },
  3346. .cipher_info = {
  3347. .alg = CIPHER_ALG_3DES,
  3348. .mode = CIPHER_MODE_ECB,
  3349. },
  3350. .auth_info = {
  3351. .alg = HASH_ALG_NONE,
  3352. .mode = HASH_MODE_NONE,
  3353. },
  3354. },
  3355. {
  3356. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  3357. .alg.crypto = {
  3358. .cra_name = "ofb(aes)",
  3359. .cra_driver_name = "ofb-aes-iproc",
  3360. .cra_blocksize = AES_BLOCK_SIZE,
  3361. .cra_ablkcipher = {
  3362. .min_keysize = AES_MIN_KEY_SIZE,
  3363. .max_keysize = AES_MAX_KEY_SIZE,
  3364. .ivsize = AES_BLOCK_SIZE,
  3365. }
  3366. },
  3367. .cipher_info = {
  3368. .alg = CIPHER_ALG_AES,
  3369. .mode = CIPHER_MODE_OFB,
  3370. },
  3371. .auth_info = {
  3372. .alg = HASH_ALG_NONE,
  3373. .mode = HASH_MODE_NONE,
  3374. },
  3375. },
  3376. {
  3377. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  3378. .alg.crypto = {
  3379. .cra_name = "cbc(aes)",
  3380. .cra_driver_name = "cbc-aes-iproc",
  3381. .cra_blocksize = AES_BLOCK_SIZE,
  3382. .cra_ablkcipher = {
  3383. .min_keysize = AES_MIN_KEY_SIZE,
  3384. .max_keysize = AES_MAX_KEY_SIZE,
  3385. .ivsize = AES_BLOCK_SIZE,
  3386. }
  3387. },
  3388. .cipher_info = {
  3389. .alg = CIPHER_ALG_AES,
  3390. .mode = CIPHER_MODE_CBC,
  3391. },
  3392. .auth_info = {
  3393. .alg = HASH_ALG_NONE,
  3394. .mode = HASH_MODE_NONE,
  3395. },
  3396. },
  3397. {
  3398. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  3399. .alg.crypto = {
  3400. .cra_name = "ecb(aes)",
  3401. .cra_driver_name = "ecb-aes-iproc",
  3402. .cra_blocksize = AES_BLOCK_SIZE,
  3403. .cra_ablkcipher = {
  3404. .min_keysize = AES_MIN_KEY_SIZE,
  3405. .max_keysize = AES_MAX_KEY_SIZE,
  3406. .ivsize = 0,
  3407. }
  3408. },
  3409. .cipher_info = {
  3410. .alg = CIPHER_ALG_AES,
  3411. .mode = CIPHER_MODE_ECB,
  3412. },
  3413. .auth_info = {
  3414. .alg = HASH_ALG_NONE,
  3415. .mode = HASH_MODE_NONE,
  3416. },
  3417. },
  3418. {
  3419. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  3420. .alg.crypto = {
  3421. .cra_name = "ctr(aes)",
  3422. .cra_driver_name = "ctr-aes-iproc",
  3423. .cra_blocksize = AES_BLOCK_SIZE,
  3424. .cra_ablkcipher = {
  3425. /* .geniv = "chainiv", */
  3426. .min_keysize = AES_MIN_KEY_SIZE,
  3427. .max_keysize = AES_MAX_KEY_SIZE,
  3428. .ivsize = AES_BLOCK_SIZE,
  3429. }
  3430. },
  3431. .cipher_info = {
  3432. .alg = CIPHER_ALG_AES,
  3433. .mode = CIPHER_MODE_CTR,
  3434. },
  3435. .auth_info = {
  3436. .alg = HASH_ALG_NONE,
  3437. .mode = HASH_MODE_NONE,
  3438. },
  3439. },
  3440. {
  3441. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  3442. .alg.crypto = {
  3443. .cra_name = "xts(aes)",
  3444. .cra_driver_name = "xts-aes-iproc",
  3445. .cra_blocksize = AES_BLOCK_SIZE,
  3446. .cra_ablkcipher = {
  3447. .min_keysize = 2 * AES_MIN_KEY_SIZE,
  3448. .max_keysize = 2 * AES_MAX_KEY_SIZE,
  3449. .ivsize = AES_BLOCK_SIZE,
  3450. }
  3451. },
  3452. .cipher_info = {
  3453. .alg = CIPHER_ALG_AES,
  3454. .mode = CIPHER_MODE_XTS,
  3455. },
  3456. .auth_info = {
  3457. .alg = HASH_ALG_NONE,
  3458. .mode = HASH_MODE_NONE,
  3459. },
  3460. },
  3461. /* AHASH algorithms. */
  3462. {
  3463. .type = CRYPTO_ALG_TYPE_AHASH,
  3464. .alg.hash = {
  3465. .halg.digestsize = MD5_DIGEST_SIZE,
  3466. .halg.base = {
  3467. .cra_name = "md5",
  3468. .cra_driver_name = "md5-iproc",
  3469. .cra_blocksize = MD5_BLOCK_WORDS * 4,
  3470. .cra_flags = CRYPTO_ALG_ASYNC,
  3471. }
  3472. },
  3473. .cipher_info = {
  3474. .alg = CIPHER_ALG_NONE,
  3475. .mode = CIPHER_MODE_NONE,
  3476. },
  3477. .auth_info = {
  3478. .alg = HASH_ALG_MD5,
  3479. .mode = HASH_MODE_HASH,
  3480. },
  3481. },
  3482. {
  3483. .type = CRYPTO_ALG_TYPE_AHASH,
  3484. .alg.hash = {
  3485. .halg.digestsize = MD5_DIGEST_SIZE,
  3486. .halg.base = {
  3487. .cra_name = "hmac(md5)",
  3488. .cra_driver_name = "hmac-md5-iproc",
  3489. .cra_blocksize = MD5_BLOCK_WORDS * 4,
  3490. }
  3491. },
  3492. .cipher_info = {
  3493. .alg = CIPHER_ALG_NONE,
  3494. .mode = CIPHER_MODE_NONE,
  3495. },
  3496. .auth_info = {
  3497. .alg = HASH_ALG_MD5,
  3498. .mode = HASH_MODE_HMAC,
  3499. },
  3500. },
  3501. {.type = CRYPTO_ALG_TYPE_AHASH,
  3502. .alg.hash = {
  3503. .halg.digestsize = SHA1_DIGEST_SIZE,
  3504. .halg.base = {
  3505. .cra_name = "sha1",
  3506. .cra_driver_name = "sha1-iproc",
  3507. .cra_blocksize = SHA1_BLOCK_SIZE,
  3508. }
  3509. },
  3510. .cipher_info = {
  3511. .alg = CIPHER_ALG_NONE,
  3512. .mode = CIPHER_MODE_NONE,
  3513. },
  3514. .auth_info = {
  3515. .alg = HASH_ALG_SHA1,
  3516. .mode = HASH_MODE_HASH,
  3517. },
  3518. },
  3519. {.type = CRYPTO_ALG_TYPE_AHASH,
  3520. .alg.hash = {
  3521. .halg.digestsize = SHA1_DIGEST_SIZE,
  3522. .halg.base = {
  3523. .cra_name = "hmac(sha1)",
  3524. .cra_driver_name = "hmac-sha1-iproc",
  3525. .cra_blocksize = SHA1_BLOCK_SIZE,
  3526. }
  3527. },
  3528. .cipher_info = {
  3529. .alg = CIPHER_ALG_NONE,
  3530. .mode = CIPHER_MODE_NONE,
  3531. },
  3532. .auth_info = {
  3533. .alg = HASH_ALG_SHA1,
  3534. .mode = HASH_MODE_HMAC,
  3535. },
  3536. },
  3537. {.type = CRYPTO_ALG_TYPE_AHASH,
  3538. .alg.hash = {
  3539. .halg.digestsize = SHA224_DIGEST_SIZE,
  3540. .halg.base = {
  3541. .cra_name = "sha224",
  3542. .cra_driver_name = "sha224-iproc",
  3543. .cra_blocksize = SHA224_BLOCK_SIZE,
  3544. }
  3545. },
  3546. .cipher_info = {
  3547. .alg = CIPHER_ALG_NONE,
  3548. .mode = CIPHER_MODE_NONE,
  3549. },
  3550. .auth_info = {
  3551. .alg = HASH_ALG_SHA224,
  3552. .mode = HASH_MODE_HASH,
  3553. },
  3554. },
  3555. {.type = CRYPTO_ALG_TYPE_AHASH,
  3556. .alg.hash = {
  3557. .halg.digestsize = SHA224_DIGEST_SIZE,
  3558. .halg.base = {
  3559. .cra_name = "hmac(sha224)",
  3560. .cra_driver_name = "hmac-sha224-iproc",
  3561. .cra_blocksize = SHA224_BLOCK_SIZE,
  3562. }
  3563. },
  3564. .cipher_info = {
  3565. .alg = CIPHER_ALG_NONE,
  3566. .mode = CIPHER_MODE_NONE,
  3567. },
  3568. .auth_info = {
  3569. .alg = HASH_ALG_SHA224,
  3570. .mode = HASH_MODE_HMAC,
  3571. },
  3572. },
  3573. {.type = CRYPTO_ALG_TYPE_AHASH,
  3574. .alg.hash = {
  3575. .halg.digestsize = SHA256_DIGEST_SIZE,
  3576. .halg.base = {
  3577. .cra_name = "sha256",
  3578. .cra_driver_name = "sha256-iproc",
  3579. .cra_blocksize = SHA256_BLOCK_SIZE,
  3580. }
  3581. },
  3582. .cipher_info = {
  3583. .alg = CIPHER_ALG_NONE,
  3584. .mode = CIPHER_MODE_NONE,
  3585. },
  3586. .auth_info = {
  3587. .alg = HASH_ALG_SHA256,
  3588. .mode = HASH_MODE_HASH,
  3589. },
  3590. },
  3591. {.type = CRYPTO_ALG_TYPE_AHASH,
  3592. .alg.hash = {
  3593. .halg.digestsize = SHA256_DIGEST_SIZE,
  3594. .halg.base = {
  3595. .cra_name = "hmac(sha256)",
  3596. .cra_driver_name = "hmac-sha256-iproc",
  3597. .cra_blocksize = SHA256_BLOCK_SIZE,
  3598. }
  3599. },
  3600. .cipher_info = {
  3601. .alg = CIPHER_ALG_NONE,
  3602. .mode = CIPHER_MODE_NONE,
  3603. },
  3604. .auth_info = {
  3605. .alg = HASH_ALG_SHA256,
  3606. .mode = HASH_MODE_HMAC,
  3607. },
  3608. },
  3609. {
  3610. .type = CRYPTO_ALG_TYPE_AHASH,
  3611. .alg.hash = {
  3612. .halg.digestsize = SHA384_DIGEST_SIZE,
  3613. .halg.base = {
  3614. .cra_name = "sha384",
  3615. .cra_driver_name = "sha384-iproc",
  3616. .cra_blocksize = SHA384_BLOCK_SIZE,
  3617. }
  3618. },
  3619. .cipher_info = {
  3620. .alg = CIPHER_ALG_NONE,
  3621. .mode = CIPHER_MODE_NONE,
  3622. },
  3623. .auth_info = {
  3624. .alg = HASH_ALG_SHA384,
  3625. .mode = HASH_MODE_HASH,
  3626. },
  3627. },
  3628. {
  3629. .type = CRYPTO_ALG_TYPE_AHASH,
  3630. .alg.hash = {
  3631. .halg.digestsize = SHA384_DIGEST_SIZE,
  3632. .halg.base = {
  3633. .cra_name = "hmac(sha384)",
  3634. .cra_driver_name = "hmac-sha384-iproc",
  3635. .cra_blocksize = SHA384_BLOCK_SIZE,
  3636. }
  3637. },
  3638. .cipher_info = {
  3639. .alg = CIPHER_ALG_NONE,
  3640. .mode = CIPHER_MODE_NONE,
  3641. },
  3642. .auth_info = {
  3643. .alg = HASH_ALG_SHA384,
  3644. .mode = HASH_MODE_HMAC,
  3645. },
  3646. },
  3647. {
  3648. .type = CRYPTO_ALG_TYPE_AHASH,
  3649. .alg.hash = {
  3650. .halg.digestsize = SHA512_DIGEST_SIZE,
  3651. .halg.base = {
  3652. .cra_name = "sha512",
  3653. .cra_driver_name = "sha512-iproc",
  3654. .cra_blocksize = SHA512_BLOCK_SIZE,
  3655. }
  3656. },
  3657. .cipher_info = {
  3658. .alg = CIPHER_ALG_NONE,
  3659. .mode = CIPHER_MODE_NONE,
  3660. },
  3661. .auth_info = {
  3662. .alg = HASH_ALG_SHA512,
  3663. .mode = HASH_MODE_HASH,
  3664. },
  3665. },
  3666. {
  3667. .type = CRYPTO_ALG_TYPE_AHASH,
  3668. .alg.hash = {
  3669. .halg.digestsize = SHA512_DIGEST_SIZE,
  3670. .halg.base = {
  3671. .cra_name = "hmac(sha512)",
  3672. .cra_driver_name = "hmac-sha512-iproc",
  3673. .cra_blocksize = SHA512_BLOCK_SIZE,
  3674. }
  3675. },
  3676. .cipher_info = {
  3677. .alg = CIPHER_ALG_NONE,
  3678. .mode = CIPHER_MODE_NONE,
  3679. },
  3680. .auth_info = {
  3681. .alg = HASH_ALG_SHA512,
  3682. .mode = HASH_MODE_HMAC,
  3683. },
  3684. },
  3685. {
  3686. .type = CRYPTO_ALG_TYPE_AHASH,
  3687. .alg.hash = {
  3688. .halg.digestsize = SHA3_224_DIGEST_SIZE,
  3689. .halg.base = {
  3690. .cra_name = "sha3-224",
  3691. .cra_driver_name = "sha3-224-iproc",
  3692. .cra_blocksize = SHA3_224_BLOCK_SIZE,
  3693. }
  3694. },
  3695. .cipher_info = {
  3696. .alg = CIPHER_ALG_NONE,
  3697. .mode = CIPHER_MODE_NONE,
  3698. },
  3699. .auth_info = {
  3700. .alg = HASH_ALG_SHA3_224,
  3701. .mode = HASH_MODE_HASH,
  3702. },
  3703. },
  3704. {
  3705. .type = CRYPTO_ALG_TYPE_AHASH,
  3706. .alg.hash = {
  3707. .halg.digestsize = SHA3_224_DIGEST_SIZE,
  3708. .halg.base = {
  3709. .cra_name = "hmac(sha3-224)",
  3710. .cra_driver_name = "hmac-sha3-224-iproc",
  3711. .cra_blocksize = SHA3_224_BLOCK_SIZE,
  3712. }
  3713. },
  3714. .cipher_info = {
  3715. .alg = CIPHER_ALG_NONE,
  3716. .mode = CIPHER_MODE_NONE,
  3717. },
  3718. .auth_info = {
  3719. .alg = HASH_ALG_SHA3_224,
  3720. .mode = HASH_MODE_HMAC
  3721. },
  3722. },
  3723. {
  3724. .type = CRYPTO_ALG_TYPE_AHASH,
  3725. .alg.hash = {
  3726. .halg.digestsize = SHA3_256_DIGEST_SIZE,
  3727. .halg.base = {
  3728. .cra_name = "sha3-256",
  3729. .cra_driver_name = "sha3-256-iproc",
  3730. .cra_blocksize = SHA3_256_BLOCK_SIZE,
  3731. }
  3732. },
  3733. .cipher_info = {
  3734. .alg = CIPHER_ALG_NONE,
  3735. .mode = CIPHER_MODE_NONE,
  3736. },
  3737. .auth_info = {
  3738. .alg = HASH_ALG_SHA3_256,
  3739. .mode = HASH_MODE_HASH,
  3740. },
  3741. },
  3742. {
  3743. .type = CRYPTO_ALG_TYPE_AHASH,
  3744. .alg.hash = {
  3745. .halg.digestsize = SHA3_256_DIGEST_SIZE,
  3746. .halg.base = {
  3747. .cra_name = "hmac(sha3-256)",
  3748. .cra_driver_name = "hmac-sha3-256-iproc",
  3749. .cra_blocksize = SHA3_256_BLOCK_SIZE,
  3750. }
  3751. },
  3752. .cipher_info = {
  3753. .alg = CIPHER_ALG_NONE,
  3754. .mode = CIPHER_MODE_NONE,
  3755. },
  3756. .auth_info = {
  3757. .alg = HASH_ALG_SHA3_256,
  3758. .mode = HASH_MODE_HMAC,
  3759. },
  3760. },
  3761. {
  3762. .type = CRYPTO_ALG_TYPE_AHASH,
  3763. .alg.hash = {
  3764. .halg.digestsize = SHA3_384_DIGEST_SIZE,
  3765. .halg.base = {
  3766. .cra_name = "sha3-384",
  3767. .cra_driver_name = "sha3-384-iproc",
  3768. .cra_blocksize = SHA3_224_BLOCK_SIZE,
  3769. }
  3770. },
  3771. .cipher_info = {
  3772. .alg = CIPHER_ALG_NONE,
  3773. .mode = CIPHER_MODE_NONE,
  3774. },
  3775. .auth_info = {
  3776. .alg = HASH_ALG_SHA3_384,
  3777. .mode = HASH_MODE_HASH,
  3778. },
  3779. },
  3780. {
  3781. .type = CRYPTO_ALG_TYPE_AHASH,
  3782. .alg.hash = {
  3783. .halg.digestsize = SHA3_384_DIGEST_SIZE,
  3784. .halg.base = {
  3785. .cra_name = "hmac(sha3-384)",
  3786. .cra_driver_name = "hmac-sha3-384-iproc",
  3787. .cra_blocksize = SHA3_384_BLOCK_SIZE,
  3788. }
  3789. },
  3790. .cipher_info = {
  3791. .alg = CIPHER_ALG_NONE,
  3792. .mode = CIPHER_MODE_NONE,
  3793. },
  3794. .auth_info = {
  3795. .alg = HASH_ALG_SHA3_384,
  3796. .mode = HASH_MODE_HMAC,
  3797. },
  3798. },
  3799. {
  3800. .type = CRYPTO_ALG_TYPE_AHASH,
  3801. .alg.hash = {
  3802. .halg.digestsize = SHA3_512_DIGEST_SIZE,
  3803. .halg.base = {
  3804. .cra_name = "sha3-512",
  3805. .cra_driver_name = "sha3-512-iproc",
  3806. .cra_blocksize = SHA3_512_BLOCK_SIZE,
  3807. }
  3808. },
  3809. .cipher_info = {
  3810. .alg = CIPHER_ALG_NONE,
  3811. .mode = CIPHER_MODE_NONE,
  3812. },
  3813. .auth_info = {
  3814. .alg = HASH_ALG_SHA3_512,
  3815. .mode = HASH_MODE_HASH,
  3816. },
  3817. },
  3818. {
  3819. .type = CRYPTO_ALG_TYPE_AHASH,
  3820. .alg.hash = {
  3821. .halg.digestsize = SHA3_512_DIGEST_SIZE,
  3822. .halg.base = {
  3823. .cra_name = "hmac(sha3-512)",
  3824. .cra_driver_name = "hmac-sha3-512-iproc",
  3825. .cra_blocksize = SHA3_512_BLOCK_SIZE,
  3826. }
  3827. },
  3828. .cipher_info = {
  3829. .alg = CIPHER_ALG_NONE,
  3830. .mode = CIPHER_MODE_NONE,
  3831. },
  3832. .auth_info = {
  3833. .alg = HASH_ALG_SHA3_512,
  3834. .mode = HASH_MODE_HMAC,
  3835. },
  3836. },
  3837. {
  3838. .type = CRYPTO_ALG_TYPE_AHASH,
  3839. .alg.hash = {
  3840. .halg.digestsize = AES_BLOCK_SIZE,
  3841. .halg.base = {
  3842. .cra_name = "xcbc(aes)",
  3843. .cra_driver_name = "xcbc-aes-iproc",
  3844. .cra_blocksize = AES_BLOCK_SIZE,
  3845. }
  3846. },
  3847. .cipher_info = {
  3848. .alg = CIPHER_ALG_NONE,
  3849. .mode = CIPHER_MODE_NONE,
  3850. },
  3851. .auth_info = {
  3852. .alg = HASH_ALG_AES,
  3853. .mode = HASH_MODE_XCBC,
  3854. },
  3855. },
  3856. {
  3857. .type = CRYPTO_ALG_TYPE_AHASH,
  3858. .alg.hash = {
  3859. .halg.digestsize = AES_BLOCK_SIZE,
  3860. .halg.base = {
  3861. .cra_name = "cmac(aes)",
  3862. .cra_driver_name = "cmac-aes-iproc",
  3863. .cra_blocksize = AES_BLOCK_SIZE,
  3864. }
  3865. },
  3866. .cipher_info = {
  3867. .alg = CIPHER_ALG_NONE,
  3868. .mode = CIPHER_MODE_NONE,
  3869. },
  3870. .auth_info = {
  3871. .alg = HASH_ALG_AES,
  3872. .mode = HASH_MODE_CMAC,
  3873. },
  3874. },
  3875. };
  3876. static int generic_cra_init(struct crypto_tfm *tfm,
  3877. struct iproc_alg_s *cipher_alg)
  3878. {
  3879. struct spu_hw *spu = &iproc_priv.spu;
  3880. struct iproc_ctx_s *ctx = crypto_tfm_ctx(tfm);
  3881. unsigned int blocksize = crypto_tfm_alg_blocksize(tfm);
  3882. flow_log("%s()\n", __func__);
  3883. ctx->alg = cipher_alg;
  3884. ctx->cipher = cipher_alg->cipher_info;
  3885. ctx->auth = cipher_alg->auth_info;
  3886. ctx->auth_first = cipher_alg->auth_first;
  3887. ctx->max_payload = spu->spu_ctx_max_payload(ctx->cipher.alg,
  3888. ctx->cipher.mode,
  3889. blocksize);
  3890. ctx->fallback_cipher = NULL;
  3891. ctx->enckeylen = 0;
  3892. ctx->authkeylen = 0;
  3893. atomic_inc(&iproc_priv.stream_count);
  3894. atomic_inc(&iproc_priv.session_count);
  3895. return 0;
  3896. }
  3897. static int ablkcipher_cra_init(struct crypto_tfm *tfm)
  3898. {
  3899. struct crypto_alg *alg = tfm->__crt_alg;
  3900. struct iproc_alg_s *cipher_alg;
  3901. flow_log("%s()\n", __func__);
  3902. tfm->crt_ablkcipher.reqsize = sizeof(struct iproc_reqctx_s);
  3903. cipher_alg = container_of(alg, struct iproc_alg_s, alg.crypto);
  3904. return generic_cra_init(tfm, cipher_alg);
  3905. }
  3906. static int ahash_cra_init(struct crypto_tfm *tfm)
  3907. {
  3908. int err;
  3909. struct crypto_alg *alg = tfm->__crt_alg;
  3910. struct iproc_alg_s *cipher_alg;
  3911. cipher_alg = container_of(__crypto_ahash_alg(alg), struct iproc_alg_s,
  3912. alg.hash);
  3913. err = generic_cra_init(tfm, cipher_alg);
  3914. flow_log("%s()\n", __func__);
  3915. /*
  3916. * export state size has to be < 512 bytes. So don't include msg bufs
  3917. * in state size.
  3918. */
  3919. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  3920. sizeof(struct iproc_reqctx_s));
  3921. return err;
  3922. }
  3923. static int aead_cra_init(struct crypto_aead *aead)
  3924. {
  3925. struct crypto_tfm *tfm = crypto_aead_tfm(aead);
  3926. struct iproc_ctx_s *ctx = crypto_tfm_ctx(tfm);
  3927. struct crypto_alg *alg = tfm->__crt_alg;
  3928. struct aead_alg *aalg = container_of(alg, struct aead_alg, base);
  3929. struct iproc_alg_s *cipher_alg = container_of(aalg, struct iproc_alg_s,
  3930. alg.aead);
  3931. int err = generic_cra_init(tfm, cipher_alg);
  3932. flow_log("%s()\n", __func__);
  3933. crypto_aead_set_reqsize(aead, sizeof(struct iproc_reqctx_s));
  3934. ctx->is_esp = false;
  3935. ctx->salt_len = 0;
  3936. ctx->salt_offset = 0;
  3937. /* random first IV */
  3938. get_random_bytes(ctx->iv, MAX_IV_SIZE);
  3939. flow_dump(" iv: ", ctx->iv, MAX_IV_SIZE);
  3940. if (!err) {
  3941. if (alg->cra_flags & CRYPTO_ALG_NEED_FALLBACK) {
  3942. flow_log("%s() creating fallback cipher\n", __func__);
  3943. ctx->fallback_cipher =
  3944. crypto_alloc_aead(alg->cra_name, 0,
  3945. CRYPTO_ALG_ASYNC |
  3946. CRYPTO_ALG_NEED_FALLBACK);
  3947. if (IS_ERR(ctx->fallback_cipher)) {
  3948. pr_err("%s() Error: failed to allocate fallback for %s\n",
  3949. __func__, alg->cra_name);
  3950. return PTR_ERR(ctx->fallback_cipher);
  3951. }
  3952. }
  3953. }
  3954. return err;
  3955. }
  3956. static void generic_cra_exit(struct crypto_tfm *tfm)
  3957. {
  3958. atomic_dec(&iproc_priv.session_count);
  3959. }
  3960. static void aead_cra_exit(struct crypto_aead *aead)
  3961. {
  3962. struct crypto_tfm *tfm = crypto_aead_tfm(aead);
  3963. struct iproc_ctx_s *ctx = crypto_tfm_ctx(tfm);
  3964. generic_cra_exit(tfm);
  3965. if (ctx->fallback_cipher) {
  3966. crypto_free_aead(ctx->fallback_cipher);
  3967. ctx->fallback_cipher = NULL;
  3968. }
  3969. }
  3970. /**
  3971. * spu_functions_register() - Specify hardware-specific SPU functions based on
  3972. * SPU type read from device tree.
  3973. * @dev: device structure
  3974. * @spu_type: SPU hardware generation
  3975. * @spu_subtype: SPU hardware version
  3976. */
  3977. static void spu_functions_register(struct device *dev,
  3978. enum spu_spu_type spu_type,
  3979. enum spu_spu_subtype spu_subtype)
  3980. {
  3981. struct spu_hw *spu = &iproc_priv.spu;
  3982. if (spu_type == SPU_TYPE_SPUM) {
  3983. dev_dbg(dev, "Registering SPUM functions");
  3984. spu->spu_dump_msg_hdr = spum_dump_msg_hdr;
  3985. spu->spu_payload_length = spum_payload_length;
  3986. spu->spu_response_hdr_len = spum_response_hdr_len;
  3987. spu->spu_hash_pad_len = spum_hash_pad_len;
  3988. spu->spu_gcm_ccm_pad_len = spum_gcm_ccm_pad_len;
  3989. spu->spu_assoc_resp_len = spum_assoc_resp_len;
  3990. spu->spu_aead_ivlen = spum_aead_ivlen;
  3991. spu->spu_hash_type = spum_hash_type;
  3992. spu->spu_digest_size = spum_digest_size;
  3993. spu->spu_create_request = spum_create_request;
  3994. spu->spu_cipher_req_init = spum_cipher_req_init;
  3995. spu->spu_cipher_req_finish = spum_cipher_req_finish;
  3996. spu->spu_request_pad = spum_request_pad;
  3997. spu->spu_tx_status_len = spum_tx_status_len;
  3998. spu->spu_rx_status_len = spum_rx_status_len;
  3999. spu->spu_status_process = spum_status_process;
  4000. spu->spu_xts_tweak_in_payload = spum_xts_tweak_in_payload;
  4001. spu->spu_ccm_update_iv = spum_ccm_update_iv;
  4002. spu->spu_wordalign_padlen = spum_wordalign_padlen;
  4003. if (spu_subtype == SPU_SUBTYPE_SPUM_NS2)
  4004. spu->spu_ctx_max_payload = spum_ns2_ctx_max_payload;
  4005. else
  4006. spu->spu_ctx_max_payload = spum_nsp_ctx_max_payload;
  4007. } else {
  4008. dev_dbg(dev, "Registering SPU2 functions");
  4009. spu->spu_dump_msg_hdr = spu2_dump_msg_hdr;
  4010. spu->spu_ctx_max_payload = spu2_ctx_max_payload;
  4011. spu->spu_payload_length = spu2_payload_length;
  4012. spu->spu_response_hdr_len = spu2_response_hdr_len;
  4013. spu->spu_hash_pad_len = spu2_hash_pad_len;
  4014. spu->spu_gcm_ccm_pad_len = spu2_gcm_ccm_pad_len;
  4015. spu->spu_assoc_resp_len = spu2_assoc_resp_len;
  4016. spu->spu_aead_ivlen = spu2_aead_ivlen;
  4017. spu->spu_hash_type = spu2_hash_type;
  4018. spu->spu_digest_size = spu2_digest_size;
  4019. spu->spu_create_request = spu2_create_request;
  4020. spu->spu_cipher_req_init = spu2_cipher_req_init;
  4021. spu->spu_cipher_req_finish = spu2_cipher_req_finish;
  4022. spu->spu_request_pad = spu2_request_pad;
  4023. spu->spu_tx_status_len = spu2_tx_status_len;
  4024. spu->spu_rx_status_len = spu2_rx_status_len;
  4025. spu->spu_status_process = spu2_status_process;
  4026. spu->spu_xts_tweak_in_payload = spu2_xts_tweak_in_payload;
  4027. spu->spu_ccm_update_iv = spu2_ccm_update_iv;
  4028. spu->spu_wordalign_padlen = spu2_wordalign_padlen;
  4029. }
  4030. }
  4031. /**
  4032. * spu_mb_init() - Initialize mailbox client. Request ownership of a mailbox
  4033. * channel for the SPU being probed.
  4034. * @dev: SPU driver device structure
  4035. *
  4036. * Return: 0 if successful
  4037. * < 0 otherwise
  4038. */
  4039. static int spu_mb_init(struct device *dev)
  4040. {
  4041. struct mbox_client *mcl = &iproc_priv.mcl;
  4042. int err, i;
  4043. iproc_priv.mbox = devm_kcalloc(dev, iproc_priv.spu.num_chan,
  4044. sizeof(struct mbox_chan *), GFP_KERNEL);
  4045. if (!iproc_priv.mbox)
  4046. return -ENOMEM;
  4047. mcl->dev = dev;
  4048. mcl->tx_block = false;
  4049. mcl->tx_tout = 0;
  4050. mcl->knows_txdone = true;
  4051. mcl->rx_callback = spu_rx_callback;
  4052. mcl->tx_done = NULL;
  4053. for (i = 0; i < iproc_priv.spu.num_chan; i++) {
  4054. iproc_priv.mbox[i] = mbox_request_channel(mcl, i);
  4055. if (IS_ERR(iproc_priv.mbox[i])) {
  4056. err = (int)PTR_ERR(iproc_priv.mbox[i]);
  4057. dev_err(dev,
  4058. "Mbox channel %d request failed with err %d",
  4059. i, err);
  4060. iproc_priv.mbox[i] = NULL;
  4061. goto free_channels;
  4062. }
  4063. }
  4064. return 0;
  4065. free_channels:
  4066. for (i = 0; i < iproc_priv.spu.num_chan; i++) {
  4067. if (iproc_priv.mbox[i])
  4068. mbox_free_channel(iproc_priv.mbox[i]);
  4069. }
  4070. return err;
  4071. }
  4072. static void spu_mb_release(struct platform_device *pdev)
  4073. {
  4074. int i;
  4075. for (i = 0; i < iproc_priv.spu.num_chan; i++)
  4076. mbox_free_channel(iproc_priv.mbox[i]);
  4077. }
  4078. static void spu_counters_init(void)
  4079. {
  4080. int i;
  4081. int j;
  4082. atomic_set(&iproc_priv.session_count, 0);
  4083. atomic_set(&iproc_priv.stream_count, 0);
  4084. atomic_set(&iproc_priv.next_chan, (int)iproc_priv.spu.num_chan);
  4085. atomic64_set(&iproc_priv.bytes_in, 0);
  4086. atomic64_set(&iproc_priv.bytes_out, 0);
  4087. for (i = 0; i < SPU_OP_NUM; i++) {
  4088. atomic_set(&iproc_priv.op_counts[i], 0);
  4089. atomic_set(&iproc_priv.setkey_cnt[i], 0);
  4090. }
  4091. for (i = 0; i < CIPHER_ALG_LAST; i++)
  4092. for (j = 0; j < CIPHER_MODE_LAST; j++)
  4093. atomic_set(&iproc_priv.cipher_cnt[i][j], 0);
  4094. for (i = 0; i < HASH_ALG_LAST; i++) {
  4095. atomic_set(&iproc_priv.hash_cnt[i], 0);
  4096. atomic_set(&iproc_priv.hmac_cnt[i], 0);
  4097. }
  4098. for (i = 0; i < AEAD_TYPE_LAST; i++)
  4099. atomic_set(&iproc_priv.aead_cnt[i], 0);
  4100. atomic_set(&iproc_priv.mb_no_spc, 0);
  4101. atomic_set(&iproc_priv.mb_send_fail, 0);
  4102. atomic_set(&iproc_priv.bad_icv, 0);
  4103. }
  4104. static int spu_register_ablkcipher(struct iproc_alg_s *driver_alg)
  4105. {
  4106. struct spu_hw *spu = &iproc_priv.spu;
  4107. struct crypto_alg *crypto = &driver_alg->alg.crypto;
  4108. int err;
  4109. /* SPU2 does not support RC4 */
  4110. if ((driver_alg->cipher_info.alg == CIPHER_ALG_RC4) &&
  4111. (spu->spu_type == SPU_TYPE_SPU2))
  4112. return 0;
  4113. crypto->cra_module = THIS_MODULE;
  4114. crypto->cra_priority = cipher_pri;
  4115. crypto->cra_alignmask = 0;
  4116. crypto->cra_ctxsize = sizeof(struct iproc_ctx_s);
  4117. INIT_LIST_HEAD(&crypto->cra_list);
  4118. crypto->cra_init = ablkcipher_cra_init;
  4119. crypto->cra_exit = generic_cra_exit;
  4120. crypto->cra_type = &crypto_ablkcipher_type;
  4121. crypto->cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC |
  4122. CRYPTO_ALG_KERN_DRIVER_ONLY;
  4123. crypto->cra_ablkcipher.setkey = ablkcipher_setkey;
  4124. crypto->cra_ablkcipher.encrypt = ablkcipher_encrypt;
  4125. crypto->cra_ablkcipher.decrypt = ablkcipher_decrypt;
  4126. err = crypto_register_alg(crypto);
  4127. /* Mark alg as having been registered, if successful */
  4128. if (err == 0)
  4129. driver_alg->registered = true;
  4130. pr_debug(" registered ablkcipher %s\n", crypto->cra_driver_name);
  4131. return err;
  4132. }
  4133. static int spu_register_ahash(struct iproc_alg_s *driver_alg)
  4134. {
  4135. struct spu_hw *spu = &iproc_priv.spu;
  4136. struct ahash_alg *hash = &driver_alg->alg.hash;
  4137. int err;
  4138. /* AES-XCBC is the only AES hash type currently supported on SPU-M */
  4139. if ((driver_alg->auth_info.alg == HASH_ALG_AES) &&
  4140. (driver_alg->auth_info.mode != HASH_MODE_XCBC) &&
  4141. (spu->spu_type == SPU_TYPE_SPUM))
  4142. return 0;
  4143. /* SHA3 algorithm variants are not registered for SPU-M or SPU2. */
  4144. if ((driver_alg->auth_info.alg >= HASH_ALG_SHA3_224) &&
  4145. (spu->spu_subtype != SPU_SUBTYPE_SPU2_V2))
  4146. return 0;
  4147. hash->halg.base.cra_module = THIS_MODULE;
  4148. hash->halg.base.cra_priority = hash_pri;
  4149. hash->halg.base.cra_alignmask = 0;
  4150. hash->halg.base.cra_ctxsize = sizeof(struct iproc_ctx_s);
  4151. hash->halg.base.cra_init = ahash_cra_init;
  4152. hash->halg.base.cra_exit = generic_cra_exit;
  4153. hash->halg.base.cra_flags = CRYPTO_ALG_ASYNC;
  4154. hash->halg.statesize = sizeof(struct spu_hash_export_s);
  4155. if (driver_alg->auth_info.mode != HASH_MODE_HMAC) {
  4156. hash->setkey = ahash_setkey;
  4157. hash->init = ahash_init;
  4158. hash->update = ahash_update;
  4159. hash->final = ahash_final;
  4160. hash->finup = ahash_finup;
  4161. hash->digest = ahash_digest;
  4162. } else {
  4163. hash->setkey = ahash_hmac_setkey;
  4164. hash->init = ahash_hmac_init;
  4165. hash->update = ahash_hmac_update;
  4166. hash->final = ahash_hmac_final;
  4167. hash->finup = ahash_hmac_finup;
  4168. hash->digest = ahash_hmac_digest;
  4169. }
  4170. hash->export = ahash_export;
  4171. hash->import = ahash_import;
  4172. err = crypto_register_ahash(hash);
  4173. /* Mark alg as having been registered, if successful */
  4174. if (err == 0)
  4175. driver_alg->registered = true;
  4176. pr_debug(" registered ahash %s\n",
  4177. hash->halg.base.cra_driver_name);
  4178. return err;
  4179. }
  4180. static int spu_register_aead(struct iproc_alg_s *driver_alg)
  4181. {
  4182. struct aead_alg *aead = &driver_alg->alg.aead;
  4183. int err;
  4184. aead->base.cra_module = THIS_MODULE;
  4185. aead->base.cra_priority = aead_pri;
  4186. aead->base.cra_alignmask = 0;
  4187. aead->base.cra_ctxsize = sizeof(struct iproc_ctx_s);
  4188. INIT_LIST_HEAD(&aead->base.cra_list);
  4189. aead->base.cra_flags |= CRYPTO_ALG_ASYNC;
  4190. /* setkey set in alg initialization */
  4191. aead->setauthsize = aead_setauthsize;
  4192. aead->encrypt = aead_encrypt;
  4193. aead->decrypt = aead_decrypt;
  4194. aead->init = aead_cra_init;
  4195. aead->exit = aead_cra_exit;
  4196. err = crypto_register_aead(aead);
  4197. /* Mark alg as having been registered, if successful */
  4198. if (err == 0)
  4199. driver_alg->registered = true;
  4200. pr_debug(" registered aead %s\n", aead->base.cra_driver_name);
  4201. return err;
  4202. }
  4203. /* register crypto algorithms the device supports */
  4204. static int spu_algs_register(struct device *dev)
  4205. {
  4206. int i, j;
  4207. int err;
  4208. for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
  4209. switch (driver_algs[i].type) {
  4210. case CRYPTO_ALG_TYPE_ABLKCIPHER:
  4211. err = spu_register_ablkcipher(&driver_algs[i]);
  4212. break;
  4213. case CRYPTO_ALG_TYPE_AHASH:
  4214. err = spu_register_ahash(&driver_algs[i]);
  4215. break;
  4216. case CRYPTO_ALG_TYPE_AEAD:
  4217. err = spu_register_aead(&driver_algs[i]);
  4218. break;
  4219. default:
  4220. dev_err(dev,
  4221. "iproc-crypto: unknown alg type: %d",
  4222. driver_algs[i].type);
  4223. err = -EINVAL;
  4224. }
  4225. if (err) {
  4226. dev_err(dev, "alg registration failed with error %d\n",
  4227. err);
  4228. goto err_algs;
  4229. }
  4230. }
  4231. return 0;
  4232. err_algs:
  4233. for (j = 0; j < i; j++) {
  4234. /* Skip any algorithm not registered */
  4235. if (!driver_algs[j].registered)
  4236. continue;
  4237. switch (driver_algs[j].type) {
  4238. case CRYPTO_ALG_TYPE_ABLKCIPHER:
  4239. crypto_unregister_alg(&driver_algs[j].alg.crypto);
  4240. driver_algs[j].registered = false;
  4241. break;
  4242. case CRYPTO_ALG_TYPE_AHASH:
  4243. crypto_unregister_ahash(&driver_algs[j].alg.hash);
  4244. driver_algs[j].registered = false;
  4245. break;
  4246. case CRYPTO_ALG_TYPE_AEAD:
  4247. crypto_unregister_aead(&driver_algs[j].alg.aead);
  4248. driver_algs[j].registered = false;
  4249. break;
  4250. }
  4251. }
  4252. return err;
  4253. }
  4254. /* ==================== Kernel Platform API ==================== */
  4255. static struct spu_type_subtype spum_ns2_types = {
  4256. SPU_TYPE_SPUM, SPU_SUBTYPE_SPUM_NS2
  4257. };
  4258. static struct spu_type_subtype spum_nsp_types = {
  4259. SPU_TYPE_SPUM, SPU_SUBTYPE_SPUM_NSP
  4260. };
  4261. static struct spu_type_subtype spu2_types = {
  4262. SPU_TYPE_SPU2, SPU_SUBTYPE_SPU2_V1
  4263. };
  4264. static struct spu_type_subtype spu2_v2_types = {
  4265. SPU_TYPE_SPU2, SPU_SUBTYPE_SPU2_V2
  4266. };
  4267. static const struct of_device_id bcm_spu_dt_ids[] = {
  4268. {
  4269. .compatible = "brcm,spum-crypto",
  4270. .data = &spum_ns2_types,
  4271. },
  4272. {
  4273. .compatible = "brcm,spum-nsp-crypto",
  4274. .data = &spum_nsp_types,
  4275. },
  4276. {
  4277. .compatible = "brcm,spu2-crypto",
  4278. .data = &spu2_types,
  4279. },
  4280. {
  4281. .compatible = "brcm,spu2-v2-crypto",
  4282. .data = &spu2_v2_types,
  4283. },
  4284. { /* sentinel */ }
  4285. };
  4286. MODULE_DEVICE_TABLE(of, bcm_spu_dt_ids);
  4287. static int spu_dt_read(struct platform_device *pdev)
  4288. {
  4289. struct device *dev = &pdev->dev;
  4290. struct spu_hw *spu = &iproc_priv.spu;
  4291. struct resource *spu_ctrl_regs;
  4292. const struct spu_type_subtype *matched_spu_type;
  4293. struct device_node *dn = pdev->dev.of_node;
  4294. int err, i;
  4295. /* Count number of mailbox channels */
  4296. spu->num_chan = of_count_phandle_with_args(dn, "mboxes", "#mbox-cells");
  4297. matched_spu_type = of_device_get_match_data(dev);
  4298. if (!matched_spu_type) {
  4299. dev_err(&pdev->dev, "Failed to match device\n");
  4300. return -ENODEV;
  4301. }
  4302. spu->spu_type = matched_spu_type->type;
  4303. spu->spu_subtype = matched_spu_type->subtype;
  4304. i = 0;
  4305. for (i = 0; (i < MAX_SPUS) && ((spu_ctrl_regs =
  4306. platform_get_resource(pdev, IORESOURCE_MEM, i)) != NULL); i++) {
  4307. spu->reg_vbase[i] = devm_ioremap_resource(dev, spu_ctrl_regs);
  4308. if (IS_ERR(spu->reg_vbase[i])) {
  4309. err = PTR_ERR(spu->reg_vbase[i]);
  4310. dev_err(&pdev->dev, "Failed to map registers: %d\n",
  4311. err);
  4312. spu->reg_vbase[i] = NULL;
  4313. return err;
  4314. }
  4315. }
  4316. spu->num_spu = i;
  4317. dev_dbg(dev, "Device has %d SPUs", spu->num_spu);
  4318. return 0;
  4319. }
  4320. int bcm_spu_probe(struct platform_device *pdev)
  4321. {
  4322. struct device *dev = &pdev->dev;
  4323. struct spu_hw *spu = &iproc_priv.spu;
  4324. int err = 0;
  4325. iproc_priv.pdev = pdev;
  4326. platform_set_drvdata(iproc_priv.pdev,
  4327. &iproc_priv);
  4328. err = spu_dt_read(pdev);
  4329. if (err < 0)
  4330. goto failure;
  4331. err = spu_mb_init(&pdev->dev);
  4332. if (err < 0)
  4333. goto failure;
  4334. if (spu->spu_type == SPU_TYPE_SPUM)
  4335. iproc_priv.bcm_hdr_len = 8;
  4336. else if (spu->spu_type == SPU_TYPE_SPU2)
  4337. iproc_priv.bcm_hdr_len = 0;
  4338. spu_functions_register(&pdev->dev, spu->spu_type, spu->spu_subtype);
  4339. spu_counters_init();
  4340. spu_setup_debugfs();
  4341. err = spu_algs_register(dev);
  4342. if (err < 0)
  4343. goto fail_reg;
  4344. return 0;
  4345. fail_reg:
  4346. spu_free_debugfs();
  4347. failure:
  4348. spu_mb_release(pdev);
  4349. dev_err(dev, "%s failed with error %d.\n", __func__, err);
  4350. return err;
  4351. }
  4352. int bcm_spu_remove(struct platform_device *pdev)
  4353. {
  4354. int i;
  4355. struct device *dev = &pdev->dev;
  4356. char *cdn;
  4357. for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
  4358. /*
  4359. * Not all algorithms were registered, depending on whether
  4360. * hardware is SPU or SPU2. So here we make sure to skip
  4361. * those algorithms that were not previously registered.
  4362. */
  4363. if (!driver_algs[i].registered)
  4364. continue;
  4365. switch (driver_algs[i].type) {
  4366. case CRYPTO_ALG_TYPE_ABLKCIPHER:
  4367. crypto_unregister_alg(&driver_algs[i].alg.crypto);
  4368. dev_dbg(dev, " unregistered cipher %s\n",
  4369. driver_algs[i].alg.crypto.cra_driver_name);
  4370. driver_algs[i].registered = false;
  4371. break;
  4372. case CRYPTO_ALG_TYPE_AHASH:
  4373. crypto_unregister_ahash(&driver_algs[i].alg.hash);
  4374. cdn = driver_algs[i].alg.hash.halg.base.cra_driver_name;
  4375. dev_dbg(dev, " unregistered hash %s\n", cdn);
  4376. driver_algs[i].registered = false;
  4377. break;
  4378. case CRYPTO_ALG_TYPE_AEAD:
  4379. crypto_unregister_aead(&driver_algs[i].alg.aead);
  4380. dev_dbg(dev, " unregistered aead %s\n",
  4381. driver_algs[i].alg.aead.base.cra_driver_name);
  4382. driver_algs[i].registered = false;
  4383. break;
  4384. }
  4385. }
  4386. spu_free_debugfs();
  4387. spu_mb_release(pdev);
  4388. return 0;
  4389. }
  4390. /* ===== Kernel Module API ===== */
  4391. static struct platform_driver bcm_spu_pdriver = {
  4392. .driver = {
  4393. .name = "brcm-spu-crypto",
  4394. .of_match_table = of_match_ptr(bcm_spu_dt_ids),
  4395. },
  4396. .probe = bcm_spu_probe,
  4397. .remove = bcm_spu_remove,
  4398. };
  4399. module_platform_driver(bcm_spu_pdriver);
  4400. MODULE_AUTHOR("Rob Rice <rob.rice@broadcom.com>");
  4401. MODULE_DESCRIPTION("Broadcom symmetric crypto offload driver");
  4402. MODULE_LICENSE("GPL v2");