cipher.c 135 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938
  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 bcm_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 __maybe_unused 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. int err = 0;
  1466. rctx = mssg->ctx;
  1467. if (unlikely(!rctx)) {
  1468. /* This is fatal */
  1469. pr_err("%s(): no request context", __func__);
  1470. err = -EFAULT;
  1471. goto cb_finish;
  1472. }
  1473. /* process the SPU status */
  1474. err = spu->spu_status_process(rctx->msg_buf.rx_stat);
  1475. if (err != 0) {
  1476. if (err == SPU_INVALID_ICV)
  1477. atomic_inc(&iproc_priv.bad_icv);
  1478. err = -EBADMSG;
  1479. goto cb_finish;
  1480. }
  1481. /* Process the SPU response message */
  1482. switch (rctx->ctx->alg->type) {
  1483. case CRYPTO_ALG_TYPE_ABLKCIPHER:
  1484. handle_ablkcipher_resp(rctx);
  1485. break;
  1486. case CRYPTO_ALG_TYPE_AHASH:
  1487. handle_ahash_resp(rctx);
  1488. break;
  1489. case CRYPTO_ALG_TYPE_AEAD:
  1490. handle_aead_resp(rctx);
  1491. break;
  1492. default:
  1493. err = -EINVAL;
  1494. goto cb_finish;
  1495. }
  1496. /*
  1497. * If this response does not complete the request, then send the next
  1498. * request chunk.
  1499. */
  1500. if (rctx->total_sent < rctx->total_todo) {
  1501. /* Deallocate anything specific to previous chunk */
  1502. spu_chunk_cleanup(rctx);
  1503. switch (rctx->ctx->alg->type) {
  1504. case CRYPTO_ALG_TYPE_ABLKCIPHER:
  1505. err = handle_ablkcipher_req(rctx);
  1506. break;
  1507. case CRYPTO_ALG_TYPE_AHASH:
  1508. err = handle_ahash_req(rctx);
  1509. if (err == -EAGAIN)
  1510. /*
  1511. * we saved data in hash carry, but tell crypto
  1512. * API we successfully completed request.
  1513. */
  1514. err = 0;
  1515. break;
  1516. case CRYPTO_ALG_TYPE_AEAD:
  1517. err = handle_aead_req(rctx);
  1518. break;
  1519. default:
  1520. err = -EINVAL;
  1521. }
  1522. if (err == -EINPROGRESS)
  1523. /* Successfully submitted request for next chunk */
  1524. return;
  1525. }
  1526. cb_finish:
  1527. finish_req(rctx, err);
  1528. }
  1529. /* ==================== Kernel Cryptographic API ==================== */
  1530. /**
  1531. * ablkcipher_enqueue() - Handle ablkcipher encrypt or decrypt request.
  1532. * @req: Crypto API request
  1533. * @encrypt: true if encrypting; false if decrypting
  1534. *
  1535. * Return: -EINPROGRESS if request accepted and result will be returned
  1536. * asynchronously
  1537. * < 0 if an error
  1538. */
  1539. static int ablkcipher_enqueue(struct ablkcipher_request *req, bool encrypt)
  1540. {
  1541. struct iproc_reqctx_s *rctx = ablkcipher_request_ctx(req);
  1542. struct iproc_ctx_s *ctx =
  1543. crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req));
  1544. int err;
  1545. flow_log("%s() enc:%u\n", __func__, encrypt);
  1546. rctx->gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
  1547. CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
  1548. rctx->parent = &req->base;
  1549. rctx->is_encrypt = encrypt;
  1550. rctx->bd_suppress = false;
  1551. rctx->total_todo = req->nbytes;
  1552. rctx->src_sent = 0;
  1553. rctx->total_sent = 0;
  1554. rctx->total_received = 0;
  1555. rctx->ctx = ctx;
  1556. /* Initialize current position in src and dst scatterlists */
  1557. rctx->src_sg = req->src;
  1558. rctx->src_nents = 0;
  1559. rctx->src_skip = 0;
  1560. rctx->dst_sg = req->dst;
  1561. rctx->dst_nents = 0;
  1562. rctx->dst_skip = 0;
  1563. if (ctx->cipher.mode == CIPHER_MODE_CBC ||
  1564. ctx->cipher.mode == CIPHER_MODE_CTR ||
  1565. ctx->cipher.mode == CIPHER_MODE_OFB ||
  1566. ctx->cipher.mode == CIPHER_MODE_XTS ||
  1567. ctx->cipher.mode == CIPHER_MODE_GCM ||
  1568. ctx->cipher.mode == CIPHER_MODE_CCM) {
  1569. rctx->iv_ctr_len =
  1570. crypto_ablkcipher_ivsize(crypto_ablkcipher_reqtfm(req));
  1571. memcpy(rctx->msg_buf.iv_ctr, req->info, rctx->iv_ctr_len);
  1572. } else {
  1573. rctx->iv_ctr_len = 0;
  1574. }
  1575. /* Choose a SPU to process this request */
  1576. rctx->chan_idx = select_channel();
  1577. err = handle_ablkcipher_req(rctx);
  1578. if (err != -EINPROGRESS)
  1579. /* synchronous result */
  1580. spu_chunk_cleanup(rctx);
  1581. return err;
  1582. }
  1583. static int des_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
  1584. unsigned int keylen)
  1585. {
  1586. struct iproc_ctx_s *ctx = crypto_ablkcipher_ctx(cipher);
  1587. u32 tmp[DES_EXPKEY_WORDS];
  1588. if (keylen == DES_KEY_SIZE) {
  1589. if (des_ekey(tmp, key) == 0) {
  1590. if (crypto_ablkcipher_get_flags(cipher) &
  1591. CRYPTO_TFM_REQ_WEAK_KEY) {
  1592. u32 flags = CRYPTO_TFM_RES_WEAK_KEY;
  1593. crypto_ablkcipher_set_flags(cipher, flags);
  1594. return -EINVAL;
  1595. }
  1596. }
  1597. ctx->cipher_type = CIPHER_TYPE_DES;
  1598. } else {
  1599. crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  1600. return -EINVAL;
  1601. }
  1602. return 0;
  1603. }
  1604. static int threedes_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
  1605. unsigned int keylen)
  1606. {
  1607. struct iproc_ctx_s *ctx = crypto_ablkcipher_ctx(cipher);
  1608. if (keylen == (DES_KEY_SIZE * 3)) {
  1609. const u32 *K = (const u32 *)key;
  1610. u32 flags = CRYPTO_TFM_RES_BAD_KEY_SCHED;
  1611. if (!((K[0] ^ K[2]) | (K[1] ^ K[3])) ||
  1612. !((K[2] ^ K[4]) | (K[3] ^ K[5]))) {
  1613. crypto_ablkcipher_set_flags(cipher, flags);
  1614. return -EINVAL;
  1615. }
  1616. ctx->cipher_type = CIPHER_TYPE_3DES;
  1617. } else {
  1618. crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  1619. return -EINVAL;
  1620. }
  1621. return 0;
  1622. }
  1623. static int aes_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
  1624. unsigned int keylen)
  1625. {
  1626. struct iproc_ctx_s *ctx = crypto_ablkcipher_ctx(cipher);
  1627. if (ctx->cipher.mode == CIPHER_MODE_XTS)
  1628. /* XTS includes two keys of equal length */
  1629. keylen = keylen / 2;
  1630. switch (keylen) {
  1631. case AES_KEYSIZE_128:
  1632. ctx->cipher_type = CIPHER_TYPE_AES128;
  1633. break;
  1634. case AES_KEYSIZE_192:
  1635. ctx->cipher_type = CIPHER_TYPE_AES192;
  1636. break;
  1637. case AES_KEYSIZE_256:
  1638. ctx->cipher_type = CIPHER_TYPE_AES256;
  1639. break;
  1640. default:
  1641. crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  1642. return -EINVAL;
  1643. }
  1644. WARN_ON((ctx->max_payload != SPU_MAX_PAYLOAD_INF) &&
  1645. ((ctx->max_payload % AES_BLOCK_SIZE) != 0));
  1646. return 0;
  1647. }
  1648. static int rc4_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
  1649. unsigned int keylen)
  1650. {
  1651. struct iproc_ctx_s *ctx = crypto_ablkcipher_ctx(cipher);
  1652. int i;
  1653. ctx->enckeylen = ARC4_MAX_KEY_SIZE + ARC4_STATE_SIZE;
  1654. ctx->enckey[0] = 0x00; /* 0x00 */
  1655. ctx->enckey[1] = 0x00; /* i */
  1656. ctx->enckey[2] = 0x00; /* 0x00 */
  1657. ctx->enckey[3] = 0x00; /* j */
  1658. for (i = 0; i < ARC4_MAX_KEY_SIZE; i++)
  1659. ctx->enckey[i + ARC4_STATE_SIZE] = key[i % keylen];
  1660. ctx->cipher_type = CIPHER_TYPE_INIT;
  1661. return 0;
  1662. }
  1663. static int ablkcipher_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
  1664. unsigned int keylen)
  1665. {
  1666. struct spu_hw *spu = &iproc_priv.spu;
  1667. struct iproc_ctx_s *ctx = crypto_ablkcipher_ctx(cipher);
  1668. struct spu_cipher_parms cipher_parms;
  1669. u32 alloc_len = 0;
  1670. int err;
  1671. flow_log("ablkcipher_setkey() keylen: %d\n", keylen);
  1672. flow_dump(" key: ", key, keylen);
  1673. switch (ctx->cipher.alg) {
  1674. case CIPHER_ALG_DES:
  1675. err = des_setkey(cipher, key, keylen);
  1676. break;
  1677. case CIPHER_ALG_3DES:
  1678. err = threedes_setkey(cipher, key, keylen);
  1679. break;
  1680. case CIPHER_ALG_AES:
  1681. err = aes_setkey(cipher, key, keylen);
  1682. break;
  1683. case CIPHER_ALG_RC4:
  1684. err = rc4_setkey(cipher, key, keylen);
  1685. break;
  1686. default:
  1687. pr_err("%s() Error: unknown cipher alg\n", __func__);
  1688. err = -EINVAL;
  1689. }
  1690. if (err)
  1691. return err;
  1692. /* RC4 already populated ctx->enkey */
  1693. if (ctx->cipher.alg != CIPHER_ALG_RC4) {
  1694. memcpy(ctx->enckey, key, keylen);
  1695. ctx->enckeylen = keylen;
  1696. }
  1697. /* SPU needs XTS keys in the reverse order the crypto API presents */
  1698. if ((ctx->cipher.alg == CIPHER_ALG_AES) &&
  1699. (ctx->cipher.mode == CIPHER_MODE_XTS)) {
  1700. unsigned int xts_keylen = keylen / 2;
  1701. memcpy(ctx->enckey, key + xts_keylen, xts_keylen);
  1702. memcpy(ctx->enckey + xts_keylen, key, xts_keylen);
  1703. }
  1704. if (spu->spu_type == SPU_TYPE_SPUM)
  1705. alloc_len = BCM_HDR_LEN + SPU_HEADER_ALLOC_LEN;
  1706. else if (spu->spu_type == SPU_TYPE_SPU2)
  1707. alloc_len = BCM_HDR_LEN + SPU2_HEADER_ALLOC_LEN;
  1708. memset(ctx->bcm_spu_req_hdr, 0, alloc_len);
  1709. cipher_parms.iv_buf = NULL;
  1710. cipher_parms.iv_len = crypto_ablkcipher_ivsize(cipher);
  1711. flow_log("%s: iv_len %u\n", __func__, cipher_parms.iv_len);
  1712. cipher_parms.alg = ctx->cipher.alg;
  1713. cipher_parms.mode = ctx->cipher.mode;
  1714. cipher_parms.type = ctx->cipher_type;
  1715. cipher_parms.key_buf = ctx->enckey;
  1716. cipher_parms.key_len = ctx->enckeylen;
  1717. /* Prepend SPU request message with BCM header */
  1718. memcpy(ctx->bcm_spu_req_hdr, BCMHEADER, BCM_HDR_LEN);
  1719. ctx->spu_req_hdr_len =
  1720. spu->spu_cipher_req_init(ctx->bcm_spu_req_hdr + BCM_HDR_LEN,
  1721. &cipher_parms);
  1722. ctx->spu_resp_hdr_len = spu->spu_response_hdr_len(ctx->authkeylen,
  1723. ctx->enckeylen,
  1724. false);
  1725. atomic_inc(&iproc_priv.setkey_cnt[SPU_OP_CIPHER]);
  1726. return 0;
  1727. }
  1728. static int ablkcipher_encrypt(struct ablkcipher_request *req)
  1729. {
  1730. flow_log("ablkcipher_encrypt() nbytes:%u\n", req->nbytes);
  1731. return ablkcipher_enqueue(req, true);
  1732. }
  1733. static int ablkcipher_decrypt(struct ablkcipher_request *req)
  1734. {
  1735. flow_log("ablkcipher_decrypt() nbytes:%u\n", req->nbytes);
  1736. return ablkcipher_enqueue(req, false);
  1737. }
  1738. static int ahash_enqueue(struct ahash_request *req)
  1739. {
  1740. struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
  1741. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1742. struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
  1743. int err = 0;
  1744. const char *alg_name;
  1745. flow_log("ahash_enqueue() nbytes:%u\n", req->nbytes);
  1746. rctx->gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
  1747. CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
  1748. rctx->parent = &req->base;
  1749. rctx->ctx = ctx;
  1750. rctx->bd_suppress = true;
  1751. memset(&rctx->mb_mssg, 0, sizeof(struct brcm_message));
  1752. /* Initialize position in src scatterlist */
  1753. rctx->src_sg = req->src;
  1754. rctx->src_skip = 0;
  1755. rctx->src_nents = 0;
  1756. rctx->dst_sg = NULL;
  1757. rctx->dst_skip = 0;
  1758. rctx->dst_nents = 0;
  1759. /* SPU2 hardware does not compute hash of zero length data */
  1760. if ((rctx->is_final == 1) && (rctx->total_todo == 0) &&
  1761. (iproc_priv.spu.spu_type == SPU_TYPE_SPU2)) {
  1762. alg_name = crypto_tfm_alg_name(crypto_ahash_tfm(tfm));
  1763. flow_log("Doing %sfinal %s zero-len hash request in software\n",
  1764. rctx->is_final ? "" : "non-", alg_name);
  1765. err = do_shash((unsigned char *)alg_name, req->result,
  1766. NULL, 0, NULL, 0, ctx->authkey,
  1767. ctx->authkeylen);
  1768. if (err < 0)
  1769. flow_log("Hash request failed with error %d\n", err);
  1770. return err;
  1771. }
  1772. /* Choose a SPU to process this request */
  1773. rctx->chan_idx = select_channel();
  1774. err = handle_ahash_req(rctx);
  1775. if (err != -EINPROGRESS)
  1776. /* synchronous result */
  1777. spu_chunk_cleanup(rctx);
  1778. if (err == -EAGAIN)
  1779. /*
  1780. * we saved data in hash carry, but tell crypto API
  1781. * we successfully completed request.
  1782. */
  1783. err = 0;
  1784. return err;
  1785. }
  1786. static int __ahash_init(struct ahash_request *req)
  1787. {
  1788. struct spu_hw *spu = &iproc_priv.spu;
  1789. struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
  1790. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1791. struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
  1792. flow_log("%s()\n", __func__);
  1793. /* Initialize the context */
  1794. rctx->hash_carry_len = 0;
  1795. rctx->is_final = 0;
  1796. rctx->total_todo = 0;
  1797. rctx->src_sent = 0;
  1798. rctx->total_sent = 0;
  1799. rctx->total_received = 0;
  1800. ctx->digestsize = crypto_ahash_digestsize(tfm);
  1801. /* If we add a hash whose digest is larger, catch it here. */
  1802. WARN_ON(ctx->digestsize > MAX_DIGEST_SIZE);
  1803. rctx->is_sw_hmac = false;
  1804. ctx->spu_resp_hdr_len = spu->spu_response_hdr_len(ctx->authkeylen, 0,
  1805. true);
  1806. return 0;
  1807. }
  1808. /**
  1809. * spu_no_incr_hash() - Determine whether incremental hashing is supported.
  1810. * @ctx: Crypto session context
  1811. *
  1812. * SPU-2 does not support incremental hashing (we'll have to revisit and
  1813. * condition based on chip revision or device tree entry if future versions do
  1814. * support incremental hash)
  1815. *
  1816. * SPU-M also doesn't support incremental hashing of AES-XCBC
  1817. *
  1818. * Return: true if incremental hashing is not supported
  1819. * false otherwise
  1820. */
  1821. bool spu_no_incr_hash(struct iproc_ctx_s *ctx)
  1822. {
  1823. struct spu_hw *spu = &iproc_priv.spu;
  1824. if (spu->spu_type == SPU_TYPE_SPU2)
  1825. return true;
  1826. if ((ctx->auth.alg == HASH_ALG_AES) &&
  1827. (ctx->auth.mode == HASH_MODE_XCBC))
  1828. return true;
  1829. /* Otherwise, incremental hashing is supported */
  1830. return false;
  1831. }
  1832. static int ahash_init(struct ahash_request *req)
  1833. {
  1834. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1835. struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
  1836. const char *alg_name;
  1837. struct crypto_shash *hash;
  1838. int ret;
  1839. gfp_t gfp;
  1840. if (spu_no_incr_hash(ctx)) {
  1841. /*
  1842. * If we get an incremental hashing request and it's not
  1843. * supported by the hardware, we need to handle it in software
  1844. * by calling synchronous hash functions.
  1845. */
  1846. alg_name = crypto_tfm_alg_name(crypto_ahash_tfm(tfm));
  1847. hash = crypto_alloc_shash(alg_name, 0, 0);
  1848. if (IS_ERR(hash)) {
  1849. ret = PTR_ERR(hash);
  1850. goto err;
  1851. }
  1852. gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
  1853. CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
  1854. ctx->shash = kmalloc(sizeof(*ctx->shash) +
  1855. crypto_shash_descsize(hash), gfp);
  1856. if (!ctx->shash) {
  1857. ret = -ENOMEM;
  1858. goto err_hash;
  1859. }
  1860. ctx->shash->tfm = hash;
  1861. ctx->shash->flags = 0;
  1862. /* Set the key using data we already have from setkey */
  1863. if (ctx->authkeylen > 0) {
  1864. ret = crypto_shash_setkey(hash, ctx->authkey,
  1865. ctx->authkeylen);
  1866. if (ret)
  1867. goto err_shash;
  1868. }
  1869. /* Initialize hash w/ this key and other params */
  1870. ret = crypto_shash_init(ctx->shash);
  1871. if (ret)
  1872. goto err_shash;
  1873. } else {
  1874. /* Otherwise call the internal function which uses SPU hw */
  1875. ret = __ahash_init(req);
  1876. }
  1877. return ret;
  1878. err_shash:
  1879. kfree(ctx->shash);
  1880. err_hash:
  1881. crypto_free_shash(hash);
  1882. err:
  1883. return ret;
  1884. }
  1885. static int __ahash_update(struct ahash_request *req)
  1886. {
  1887. struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
  1888. flow_log("ahash_update() nbytes:%u\n", req->nbytes);
  1889. if (!req->nbytes)
  1890. return 0;
  1891. rctx->total_todo += req->nbytes;
  1892. rctx->src_sent = 0;
  1893. return ahash_enqueue(req);
  1894. }
  1895. static int ahash_update(struct ahash_request *req)
  1896. {
  1897. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1898. struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
  1899. u8 *tmpbuf;
  1900. int ret;
  1901. int nents;
  1902. gfp_t gfp;
  1903. if (spu_no_incr_hash(ctx)) {
  1904. /*
  1905. * If we get an incremental hashing request and it's not
  1906. * supported by the hardware, we need to handle it in software
  1907. * by calling synchronous hash functions.
  1908. */
  1909. if (req->src)
  1910. nents = sg_nents(req->src);
  1911. else
  1912. return -EINVAL;
  1913. /* Copy data from req scatterlist to tmp buffer */
  1914. gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
  1915. CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
  1916. tmpbuf = kmalloc(req->nbytes, gfp);
  1917. if (!tmpbuf)
  1918. return -ENOMEM;
  1919. if (sg_copy_to_buffer(req->src, nents, tmpbuf, req->nbytes) !=
  1920. req->nbytes) {
  1921. kfree(tmpbuf);
  1922. return -EINVAL;
  1923. }
  1924. /* Call synchronous update */
  1925. ret = crypto_shash_update(ctx->shash, tmpbuf, req->nbytes);
  1926. kfree(tmpbuf);
  1927. } else {
  1928. /* Otherwise call the internal function which uses SPU hw */
  1929. ret = __ahash_update(req);
  1930. }
  1931. return ret;
  1932. }
  1933. static int __ahash_final(struct ahash_request *req)
  1934. {
  1935. struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
  1936. flow_log("ahash_final() nbytes:%u\n", req->nbytes);
  1937. rctx->is_final = 1;
  1938. return ahash_enqueue(req);
  1939. }
  1940. static int ahash_final(struct ahash_request *req)
  1941. {
  1942. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1943. struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
  1944. int ret;
  1945. if (spu_no_incr_hash(ctx)) {
  1946. /*
  1947. * If we get an incremental hashing request and it's not
  1948. * supported by the hardware, we need to handle it in software
  1949. * by calling synchronous hash functions.
  1950. */
  1951. ret = crypto_shash_final(ctx->shash, req->result);
  1952. /* Done with hash, can deallocate it now */
  1953. crypto_free_shash(ctx->shash->tfm);
  1954. kfree(ctx->shash);
  1955. } else {
  1956. /* Otherwise call the internal function which uses SPU hw */
  1957. ret = __ahash_final(req);
  1958. }
  1959. return ret;
  1960. }
  1961. static int __ahash_finup(struct ahash_request *req)
  1962. {
  1963. struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
  1964. flow_log("ahash_finup() nbytes:%u\n", req->nbytes);
  1965. rctx->total_todo += req->nbytes;
  1966. rctx->src_sent = 0;
  1967. rctx->is_final = 1;
  1968. return ahash_enqueue(req);
  1969. }
  1970. static int ahash_finup(struct ahash_request *req)
  1971. {
  1972. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1973. struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
  1974. u8 *tmpbuf;
  1975. int ret;
  1976. int nents;
  1977. gfp_t gfp;
  1978. if (spu_no_incr_hash(ctx)) {
  1979. /*
  1980. * If we get an incremental hashing request and it's not
  1981. * supported by the hardware, we need to handle it in software
  1982. * by calling synchronous hash functions.
  1983. */
  1984. if (req->src) {
  1985. nents = sg_nents(req->src);
  1986. } else {
  1987. ret = -EINVAL;
  1988. goto ahash_finup_exit;
  1989. }
  1990. /* Copy data from req scatterlist to tmp buffer */
  1991. gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
  1992. CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
  1993. tmpbuf = kmalloc(req->nbytes, gfp);
  1994. if (!tmpbuf) {
  1995. ret = -ENOMEM;
  1996. goto ahash_finup_exit;
  1997. }
  1998. if (sg_copy_to_buffer(req->src, nents, tmpbuf, req->nbytes) !=
  1999. req->nbytes) {
  2000. ret = -EINVAL;
  2001. goto ahash_finup_free;
  2002. }
  2003. /* Call synchronous update */
  2004. ret = crypto_shash_finup(ctx->shash, tmpbuf, req->nbytes,
  2005. req->result);
  2006. } else {
  2007. /* Otherwise call the internal function which uses SPU hw */
  2008. return __ahash_finup(req);
  2009. }
  2010. ahash_finup_free:
  2011. kfree(tmpbuf);
  2012. ahash_finup_exit:
  2013. /* Done with hash, can deallocate it now */
  2014. crypto_free_shash(ctx->shash->tfm);
  2015. kfree(ctx->shash);
  2016. return ret;
  2017. }
  2018. static int ahash_digest(struct ahash_request *req)
  2019. {
  2020. int err = 0;
  2021. flow_log("ahash_digest() nbytes:%u\n", req->nbytes);
  2022. /* whole thing at once */
  2023. err = __ahash_init(req);
  2024. if (!err)
  2025. err = __ahash_finup(req);
  2026. return err;
  2027. }
  2028. static int ahash_setkey(struct crypto_ahash *ahash, const u8 *key,
  2029. unsigned int keylen)
  2030. {
  2031. struct iproc_ctx_s *ctx = crypto_ahash_ctx(ahash);
  2032. flow_log("%s() ahash:%p key:%p keylen:%u\n",
  2033. __func__, ahash, key, keylen);
  2034. flow_dump(" key: ", key, keylen);
  2035. if (ctx->auth.alg == HASH_ALG_AES) {
  2036. switch (keylen) {
  2037. case AES_KEYSIZE_128:
  2038. ctx->cipher_type = CIPHER_TYPE_AES128;
  2039. break;
  2040. case AES_KEYSIZE_192:
  2041. ctx->cipher_type = CIPHER_TYPE_AES192;
  2042. break;
  2043. case AES_KEYSIZE_256:
  2044. ctx->cipher_type = CIPHER_TYPE_AES256;
  2045. break;
  2046. default:
  2047. pr_err("%s() Error: Invalid key length\n", __func__);
  2048. return -EINVAL;
  2049. }
  2050. } else {
  2051. pr_err("%s() Error: unknown hash alg\n", __func__);
  2052. return -EINVAL;
  2053. }
  2054. memcpy(ctx->authkey, key, keylen);
  2055. ctx->authkeylen = keylen;
  2056. return 0;
  2057. }
  2058. static int ahash_export(struct ahash_request *req, void *out)
  2059. {
  2060. const struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
  2061. struct spu_hash_export_s *spu_exp = (struct spu_hash_export_s *)out;
  2062. spu_exp->total_todo = rctx->total_todo;
  2063. spu_exp->total_sent = rctx->total_sent;
  2064. spu_exp->is_sw_hmac = rctx->is_sw_hmac;
  2065. memcpy(spu_exp->hash_carry, rctx->hash_carry, sizeof(rctx->hash_carry));
  2066. spu_exp->hash_carry_len = rctx->hash_carry_len;
  2067. memcpy(spu_exp->incr_hash, rctx->incr_hash, sizeof(rctx->incr_hash));
  2068. return 0;
  2069. }
  2070. static int ahash_import(struct ahash_request *req, const void *in)
  2071. {
  2072. struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
  2073. struct spu_hash_export_s *spu_exp = (struct spu_hash_export_s *)in;
  2074. rctx->total_todo = spu_exp->total_todo;
  2075. rctx->total_sent = spu_exp->total_sent;
  2076. rctx->is_sw_hmac = spu_exp->is_sw_hmac;
  2077. memcpy(rctx->hash_carry, spu_exp->hash_carry, sizeof(rctx->hash_carry));
  2078. rctx->hash_carry_len = spu_exp->hash_carry_len;
  2079. memcpy(rctx->incr_hash, spu_exp->incr_hash, sizeof(rctx->incr_hash));
  2080. return 0;
  2081. }
  2082. static int ahash_hmac_setkey(struct crypto_ahash *ahash, const u8 *key,
  2083. unsigned int keylen)
  2084. {
  2085. struct iproc_ctx_s *ctx = crypto_ahash_ctx(ahash);
  2086. unsigned int blocksize =
  2087. crypto_tfm_alg_blocksize(crypto_ahash_tfm(ahash));
  2088. unsigned int digestsize = crypto_ahash_digestsize(ahash);
  2089. unsigned int index;
  2090. int rc;
  2091. flow_log("%s() ahash:%p key:%p keylen:%u blksz:%u digestsz:%u\n",
  2092. __func__, ahash, key, keylen, blocksize, digestsize);
  2093. flow_dump(" key: ", key, keylen);
  2094. if (keylen > blocksize) {
  2095. switch (ctx->auth.alg) {
  2096. case HASH_ALG_MD5:
  2097. rc = do_shash("md5", ctx->authkey, key, keylen, NULL,
  2098. 0, NULL, 0);
  2099. break;
  2100. case HASH_ALG_SHA1:
  2101. rc = do_shash("sha1", ctx->authkey, key, keylen, NULL,
  2102. 0, NULL, 0);
  2103. break;
  2104. case HASH_ALG_SHA224:
  2105. rc = do_shash("sha224", ctx->authkey, key, keylen, NULL,
  2106. 0, NULL, 0);
  2107. break;
  2108. case HASH_ALG_SHA256:
  2109. rc = do_shash("sha256", ctx->authkey, key, keylen, NULL,
  2110. 0, NULL, 0);
  2111. break;
  2112. case HASH_ALG_SHA384:
  2113. rc = do_shash("sha384", ctx->authkey, key, keylen, NULL,
  2114. 0, NULL, 0);
  2115. break;
  2116. case HASH_ALG_SHA512:
  2117. rc = do_shash("sha512", ctx->authkey, key, keylen, NULL,
  2118. 0, NULL, 0);
  2119. break;
  2120. case HASH_ALG_SHA3_224:
  2121. rc = do_shash("sha3-224", ctx->authkey, key, keylen,
  2122. NULL, 0, NULL, 0);
  2123. break;
  2124. case HASH_ALG_SHA3_256:
  2125. rc = do_shash("sha3-256", ctx->authkey, key, keylen,
  2126. NULL, 0, NULL, 0);
  2127. break;
  2128. case HASH_ALG_SHA3_384:
  2129. rc = do_shash("sha3-384", ctx->authkey, key, keylen,
  2130. NULL, 0, NULL, 0);
  2131. break;
  2132. case HASH_ALG_SHA3_512:
  2133. rc = do_shash("sha3-512", ctx->authkey, key, keylen,
  2134. NULL, 0, NULL, 0);
  2135. break;
  2136. default:
  2137. pr_err("%s() Error: unknown hash alg\n", __func__);
  2138. return -EINVAL;
  2139. }
  2140. if (rc < 0) {
  2141. pr_err("%s() Error %d computing shash for %s\n",
  2142. __func__, rc, hash_alg_name[ctx->auth.alg]);
  2143. return rc;
  2144. }
  2145. ctx->authkeylen = digestsize;
  2146. flow_log(" keylen > digestsize... hashed\n");
  2147. flow_dump(" newkey: ", ctx->authkey, ctx->authkeylen);
  2148. } else {
  2149. memcpy(ctx->authkey, key, keylen);
  2150. ctx->authkeylen = keylen;
  2151. }
  2152. /*
  2153. * Full HMAC operation in SPUM is not verified,
  2154. * So keeping the generation of IPAD, OPAD and
  2155. * outer hashing in software.
  2156. */
  2157. if (iproc_priv.spu.spu_type == SPU_TYPE_SPUM) {
  2158. memcpy(ctx->ipad, ctx->authkey, ctx->authkeylen);
  2159. memset(ctx->ipad + ctx->authkeylen, 0,
  2160. blocksize - ctx->authkeylen);
  2161. ctx->authkeylen = 0;
  2162. memcpy(ctx->opad, ctx->ipad, blocksize);
  2163. for (index = 0; index < blocksize; index++) {
  2164. ctx->ipad[index] ^= HMAC_IPAD_VALUE;
  2165. ctx->opad[index] ^= HMAC_OPAD_VALUE;
  2166. }
  2167. flow_dump(" ipad: ", ctx->ipad, blocksize);
  2168. flow_dump(" opad: ", ctx->opad, blocksize);
  2169. }
  2170. ctx->digestsize = digestsize;
  2171. atomic_inc(&iproc_priv.setkey_cnt[SPU_OP_HMAC]);
  2172. return 0;
  2173. }
  2174. static int ahash_hmac_init(struct ahash_request *req)
  2175. {
  2176. struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
  2177. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  2178. struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
  2179. unsigned int blocksize =
  2180. crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
  2181. flow_log("ahash_hmac_init()\n");
  2182. /* init the context as a hash */
  2183. ahash_init(req);
  2184. if (!spu_no_incr_hash(ctx)) {
  2185. /* SPU-M can do incr hashing but needs sw for outer HMAC */
  2186. rctx->is_sw_hmac = true;
  2187. ctx->auth.mode = HASH_MODE_HASH;
  2188. /* start with a prepended ipad */
  2189. memcpy(rctx->hash_carry, ctx->ipad, blocksize);
  2190. rctx->hash_carry_len = blocksize;
  2191. rctx->total_todo += blocksize;
  2192. }
  2193. return 0;
  2194. }
  2195. static int ahash_hmac_update(struct ahash_request *req)
  2196. {
  2197. flow_log("ahash_hmac_update() nbytes:%u\n", req->nbytes);
  2198. if (!req->nbytes)
  2199. return 0;
  2200. return ahash_update(req);
  2201. }
  2202. static int ahash_hmac_final(struct ahash_request *req)
  2203. {
  2204. flow_log("ahash_hmac_final() nbytes:%u\n", req->nbytes);
  2205. return ahash_final(req);
  2206. }
  2207. static int ahash_hmac_finup(struct ahash_request *req)
  2208. {
  2209. flow_log("ahash_hmac_finupl() nbytes:%u\n", req->nbytes);
  2210. return ahash_finup(req);
  2211. }
  2212. static int ahash_hmac_digest(struct ahash_request *req)
  2213. {
  2214. struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
  2215. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  2216. struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
  2217. unsigned int blocksize =
  2218. crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
  2219. flow_log("ahash_hmac_digest() nbytes:%u\n", req->nbytes);
  2220. /* Perform initialization and then call finup */
  2221. __ahash_init(req);
  2222. if (iproc_priv.spu.spu_type == SPU_TYPE_SPU2) {
  2223. /*
  2224. * SPU2 supports full HMAC implementation in the
  2225. * hardware, need not to generate IPAD, OPAD and
  2226. * outer hash in software.
  2227. * Only for hash key len > hash block size, SPU2
  2228. * expects to perform hashing on the key, shorten
  2229. * it to digest size and feed it as hash key.
  2230. */
  2231. rctx->is_sw_hmac = false;
  2232. ctx->auth.mode = HASH_MODE_HMAC;
  2233. } else {
  2234. rctx->is_sw_hmac = true;
  2235. ctx->auth.mode = HASH_MODE_HASH;
  2236. /* start with a prepended ipad */
  2237. memcpy(rctx->hash_carry, ctx->ipad, blocksize);
  2238. rctx->hash_carry_len = blocksize;
  2239. rctx->total_todo += blocksize;
  2240. }
  2241. return __ahash_finup(req);
  2242. }
  2243. /* aead helpers */
  2244. static int aead_need_fallback(struct aead_request *req)
  2245. {
  2246. struct iproc_reqctx_s *rctx = aead_request_ctx(req);
  2247. struct spu_hw *spu = &iproc_priv.spu;
  2248. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  2249. struct iproc_ctx_s *ctx = crypto_aead_ctx(aead);
  2250. u32 payload_len;
  2251. /*
  2252. * SPU hardware cannot handle the AES-GCM/CCM case where plaintext
  2253. * and AAD are both 0 bytes long. So use fallback in this case.
  2254. */
  2255. if (((ctx->cipher.mode == CIPHER_MODE_GCM) ||
  2256. (ctx->cipher.mode == CIPHER_MODE_CCM)) &&
  2257. (req->assoclen == 0)) {
  2258. if ((rctx->is_encrypt && (req->cryptlen == 0)) ||
  2259. (!rctx->is_encrypt && (req->cryptlen == ctx->digestsize))) {
  2260. flow_log("AES GCM/CCM needs fallback for 0 len req\n");
  2261. return 1;
  2262. }
  2263. }
  2264. /* SPU-M hardware only supports CCM digest size of 8, 12, or 16 bytes */
  2265. if ((ctx->cipher.mode == CIPHER_MODE_CCM) &&
  2266. (spu->spu_type == SPU_TYPE_SPUM) &&
  2267. (ctx->digestsize != 8) && (ctx->digestsize != 12) &&
  2268. (ctx->digestsize != 16)) {
  2269. flow_log("%s() AES CCM needs fallback for digest size %d\n",
  2270. __func__, ctx->digestsize);
  2271. return 1;
  2272. }
  2273. /*
  2274. * SPU-M on NSP has an issue where AES-CCM hash is not correct
  2275. * when AAD size is 0
  2276. */
  2277. if ((ctx->cipher.mode == CIPHER_MODE_CCM) &&
  2278. (spu->spu_subtype == SPU_SUBTYPE_SPUM_NSP) &&
  2279. (req->assoclen == 0)) {
  2280. flow_log("%s() AES_CCM needs fallback for 0 len AAD on NSP\n",
  2281. __func__);
  2282. return 1;
  2283. }
  2284. payload_len = req->cryptlen;
  2285. if (spu->spu_type == SPU_TYPE_SPUM)
  2286. payload_len += req->assoclen;
  2287. flow_log("%s() payload len: %u\n", __func__, payload_len);
  2288. if (ctx->max_payload == SPU_MAX_PAYLOAD_INF)
  2289. return 0;
  2290. else
  2291. return payload_len > ctx->max_payload;
  2292. }
  2293. static void aead_complete(struct crypto_async_request *areq, int err)
  2294. {
  2295. struct aead_request *req =
  2296. container_of(areq, struct aead_request, base);
  2297. struct iproc_reqctx_s *rctx = aead_request_ctx(req);
  2298. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  2299. flow_log("%s() err:%d\n", __func__, err);
  2300. areq->tfm = crypto_aead_tfm(aead);
  2301. areq->complete = rctx->old_complete;
  2302. areq->data = rctx->old_data;
  2303. areq->complete(areq, err);
  2304. }
  2305. static int aead_do_fallback(struct aead_request *req, bool is_encrypt)
  2306. {
  2307. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  2308. struct crypto_tfm *tfm = crypto_aead_tfm(aead);
  2309. struct iproc_reqctx_s *rctx = aead_request_ctx(req);
  2310. struct iproc_ctx_s *ctx = crypto_tfm_ctx(tfm);
  2311. int err;
  2312. u32 req_flags;
  2313. flow_log("%s() enc:%u\n", __func__, is_encrypt);
  2314. if (ctx->fallback_cipher) {
  2315. /* Store the cipher tfm and then use the fallback tfm */
  2316. rctx->old_tfm = tfm;
  2317. aead_request_set_tfm(req, ctx->fallback_cipher);
  2318. /*
  2319. * Save the callback and chain ourselves in, so we can restore
  2320. * the tfm
  2321. */
  2322. rctx->old_complete = req->base.complete;
  2323. rctx->old_data = req->base.data;
  2324. req_flags = aead_request_flags(req);
  2325. aead_request_set_callback(req, req_flags, aead_complete, req);
  2326. err = is_encrypt ? crypto_aead_encrypt(req) :
  2327. crypto_aead_decrypt(req);
  2328. if (err == 0) {
  2329. /*
  2330. * fallback was synchronous (did not return
  2331. * -EINPROGRESS). So restore request state here.
  2332. */
  2333. aead_request_set_callback(req, req_flags,
  2334. rctx->old_complete, req);
  2335. req->base.data = rctx->old_data;
  2336. aead_request_set_tfm(req, aead);
  2337. flow_log("%s() fallback completed successfully\n\n",
  2338. __func__);
  2339. }
  2340. } else {
  2341. err = -EINVAL;
  2342. }
  2343. return err;
  2344. }
  2345. static int aead_enqueue(struct aead_request *req, bool is_encrypt)
  2346. {
  2347. struct iproc_reqctx_s *rctx = aead_request_ctx(req);
  2348. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  2349. struct iproc_ctx_s *ctx = crypto_aead_ctx(aead);
  2350. int err;
  2351. flow_log("%s() enc:%u\n", __func__, is_encrypt);
  2352. if (req->assoclen > MAX_ASSOC_SIZE) {
  2353. pr_err
  2354. ("%s() Error: associated data too long. (%u > %u bytes)\n",
  2355. __func__, req->assoclen, MAX_ASSOC_SIZE);
  2356. return -EINVAL;
  2357. }
  2358. rctx->gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
  2359. CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
  2360. rctx->parent = &req->base;
  2361. rctx->is_encrypt = is_encrypt;
  2362. rctx->bd_suppress = false;
  2363. rctx->total_todo = req->cryptlen;
  2364. rctx->src_sent = 0;
  2365. rctx->total_sent = 0;
  2366. rctx->total_received = 0;
  2367. rctx->is_sw_hmac = false;
  2368. rctx->ctx = ctx;
  2369. memset(&rctx->mb_mssg, 0, sizeof(struct brcm_message));
  2370. /* assoc data is at start of src sg */
  2371. rctx->assoc = req->src;
  2372. /*
  2373. * Init current position in src scatterlist to be after assoc data.
  2374. * src_skip set to buffer offset where data begins. (Assoc data could
  2375. * end in the middle of a buffer.)
  2376. */
  2377. if (spu_sg_at_offset(req->src, req->assoclen, &rctx->src_sg,
  2378. &rctx->src_skip) < 0) {
  2379. pr_err("%s() Error: Unable to find start of src data\n",
  2380. __func__);
  2381. return -EINVAL;
  2382. }
  2383. rctx->src_nents = 0;
  2384. rctx->dst_nents = 0;
  2385. if (req->dst == req->src) {
  2386. rctx->dst_sg = rctx->src_sg;
  2387. rctx->dst_skip = rctx->src_skip;
  2388. } else {
  2389. /*
  2390. * Expect req->dst to have room for assoc data followed by
  2391. * output data and ICV, if encrypt. So initialize dst_sg
  2392. * to point beyond assoc len offset.
  2393. */
  2394. if (spu_sg_at_offset(req->dst, req->assoclen, &rctx->dst_sg,
  2395. &rctx->dst_skip) < 0) {
  2396. pr_err("%s() Error: Unable to find start of dst data\n",
  2397. __func__);
  2398. return -EINVAL;
  2399. }
  2400. }
  2401. if (ctx->cipher.mode == CIPHER_MODE_CBC ||
  2402. ctx->cipher.mode == CIPHER_MODE_CTR ||
  2403. ctx->cipher.mode == CIPHER_MODE_OFB ||
  2404. ctx->cipher.mode == CIPHER_MODE_XTS ||
  2405. ctx->cipher.mode == CIPHER_MODE_GCM) {
  2406. rctx->iv_ctr_len =
  2407. ctx->salt_len +
  2408. crypto_aead_ivsize(crypto_aead_reqtfm(req));
  2409. } else if (ctx->cipher.mode == CIPHER_MODE_CCM) {
  2410. rctx->iv_ctr_len = CCM_AES_IV_SIZE;
  2411. } else {
  2412. rctx->iv_ctr_len = 0;
  2413. }
  2414. rctx->hash_carry_len = 0;
  2415. flow_log(" src sg: %p\n", req->src);
  2416. flow_log(" rctx->src_sg: %p, src_skip %u\n",
  2417. rctx->src_sg, rctx->src_skip);
  2418. flow_log(" assoc: %p, assoclen %u\n", rctx->assoc, req->assoclen);
  2419. flow_log(" dst sg: %p\n", req->dst);
  2420. flow_log(" rctx->dst_sg: %p, dst_skip %u\n",
  2421. rctx->dst_sg, rctx->dst_skip);
  2422. flow_log(" iv_ctr_len:%u\n", rctx->iv_ctr_len);
  2423. flow_dump(" iv: ", req->iv, rctx->iv_ctr_len);
  2424. flow_log(" authkeylen:%u\n", ctx->authkeylen);
  2425. flow_log(" is_esp: %s\n", ctx->is_esp ? "yes" : "no");
  2426. if (ctx->max_payload == SPU_MAX_PAYLOAD_INF)
  2427. flow_log(" max_payload infinite");
  2428. else
  2429. flow_log(" max_payload: %u\n", ctx->max_payload);
  2430. if (unlikely(aead_need_fallback(req)))
  2431. return aead_do_fallback(req, is_encrypt);
  2432. /*
  2433. * Do memory allocations for request after fallback check, because if we
  2434. * do fallback, we won't call finish_req() to dealloc.
  2435. */
  2436. if (rctx->iv_ctr_len) {
  2437. if (ctx->salt_len)
  2438. memcpy(rctx->msg_buf.iv_ctr + ctx->salt_offset,
  2439. ctx->salt, ctx->salt_len);
  2440. memcpy(rctx->msg_buf.iv_ctr + ctx->salt_offset + ctx->salt_len,
  2441. req->iv,
  2442. rctx->iv_ctr_len - ctx->salt_len - ctx->salt_offset);
  2443. }
  2444. rctx->chan_idx = select_channel();
  2445. err = handle_aead_req(rctx);
  2446. if (err != -EINPROGRESS)
  2447. /* synchronous result */
  2448. spu_chunk_cleanup(rctx);
  2449. return err;
  2450. }
  2451. static int aead_authenc_setkey(struct crypto_aead *cipher,
  2452. const u8 *key, unsigned int keylen)
  2453. {
  2454. struct spu_hw *spu = &iproc_priv.spu;
  2455. struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
  2456. struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
  2457. struct crypto_authenc_keys keys;
  2458. int ret;
  2459. flow_log("%s() aead:%p key:%p keylen:%u\n", __func__, cipher, key,
  2460. keylen);
  2461. flow_dump(" key: ", key, keylen);
  2462. ret = crypto_authenc_extractkeys(&keys, key, keylen);
  2463. if (ret)
  2464. goto badkey;
  2465. if (keys.enckeylen > MAX_KEY_SIZE ||
  2466. keys.authkeylen > MAX_KEY_SIZE)
  2467. goto badkey;
  2468. ctx->enckeylen = keys.enckeylen;
  2469. ctx->authkeylen = keys.authkeylen;
  2470. memcpy(ctx->enckey, keys.enckey, keys.enckeylen);
  2471. /* May end up padding auth key. So make sure it's zeroed. */
  2472. memset(ctx->authkey, 0, sizeof(ctx->authkey));
  2473. memcpy(ctx->authkey, keys.authkey, keys.authkeylen);
  2474. switch (ctx->alg->cipher_info.alg) {
  2475. case CIPHER_ALG_DES:
  2476. if (ctx->enckeylen == DES_KEY_SIZE) {
  2477. u32 tmp[DES_EXPKEY_WORDS];
  2478. u32 flags = CRYPTO_TFM_RES_WEAK_KEY;
  2479. if (des_ekey(tmp, keys.enckey) == 0) {
  2480. if (crypto_aead_get_flags(cipher) &
  2481. CRYPTO_TFM_REQ_WEAK_KEY) {
  2482. crypto_aead_set_flags(cipher, flags);
  2483. return -EINVAL;
  2484. }
  2485. }
  2486. ctx->cipher_type = CIPHER_TYPE_DES;
  2487. } else {
  2488. goto badkey;
  2489. }
  2490. break;
  2491. case CIPHER_ALG_3DES:
  2492. if (ctx->enckeylen == (DES_KEY_SIZE * 3)) {
  2493. const u32 *K = (const u32 *)keys.enckey;
  2494. u32 flags = CRYPTO_TFM_RES_BAD_KEY_SCHED;
  2495. if (!((K[0] ^ K[2]) | (K[1] ^ K[3])) ||
  2496. !((K[2] ^ K[4]) | (K[3] ^ K[5]))) {
  2497. crypto_aead_set_flags(cipher, flags);
  2498. return -EINVAL;
  2499. }
  2500. ctx->cipher_type = CIPHER_TYPE_3DES;
  2501. } else {
  2502. crypto_aead_set_flags(cipher,
  2503. CRYPTO_TFM_RES_BAD_KEY_LEN);
  2504. return -EINVAL;
  2505. }
  2506. break;
  2507. case CIPHER_ALG_AES:
  2508. switch (ctx->enckeylen) {
  2509. case AES_KEYSIZE_128:
  2510. ctx->cipher_type = CIPHER_TYPE_AES128;
  2511. break;
  2512. case AES_KEYSIZE_192:
  2513. ctx->cipher_type = CIPHER_TYPE_AES192;
  2514. break;
  2515. case AES_KEYSIZE_256:
  2516. ctx->cipher_type = CIPHER_TYPE_AES256;
  2517. break;
  2518. default:
  2519. goto badkey;
  2520. }
  2521. break;
  2522. case CIPHER_ALG_RC4:
  2523. ctx->cipher_type = CIPHER_TYPE_INIT;
  2524. break;
  2525. default:
  2526. pr_err("%s() Error: Unknown cipher alg\n", __func__);
  2527. return -EINVAL;
  2528. }
  2529. flow_log(" enckeylen:%u authkeylen:%u\n", ctx->enckeylen,
  2530. ctx->authkeylen);
  2531. flow_dump(" enc: ", ctx->enckey, ctx->enckeylen);
  2532. flow_dump(" auth: ", ctx->authkey, ctx->authkeylen);
  2533. /* setkey the fallback just in case we needto use it */
  2534. if (ctx->fallback_cipher) {
  2535. flow_log(" running fallback setkey()\n");
  2536. ctx->fallback_cipher->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
  2537. ctx->fallback_cipher->base.crt_flags |=
  2538. tfm->crt_flags & CRYPTO_TFM_REQ_MASK;
  2539. ret = crypto_aead_setkey(ctx->fallback_cipher, key, keylen);
  2540. if (ret) {
  2541. flow_log(" fallback setkey() returned:%d\n", ret);
  2542. tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
  2543. tfm->crt_flags |=
  2544. (ctx->fallback_cipher->base.crt_flags &
  2545. CRYPTO_TFM_RES_MASK);
  2546. }
  2547. }
  2548. ctx->spu_resp_hdr_len = spu->spu_response_hdr_len(ctx->authkeylen,
  2549. ctx->enckeylen,
  2550. false);
  2551. atomic_inc(&iproc_priv.setkey_cnt[SPU_OP_AEAD]);
  2552. return ret;
  2553. badkey:
  2554. ctx->enckeylen = 0;
  2555. ctx->authkeylen = 0;
  2556. ctx->digestsize = 0;
  2557. crypto_aead_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  2558. return -EINVAL;
  2559. }
  2560. static int aead_gcm_ccm_setkey(struct crypto_aead *cipher,
  2561. const u8 *key, unsigned int keylen)
  2562. {
  2563. struct spu_hw *spu = &iproc_priv.spu;
  2564. struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
  2565. struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
  2566. int ret = 0;
  2567. flow_log("%s() keylen:%u\n", __func__, keylen);
  2568. flow_dump(" key: ", key, keylen);
  2569. if (!ctx->is_esp)
  2570. ctx->digestsize = keylen;
  2571. ctx->enckeylen = keylen;
  2572. ctx->authkeylen = 0;
  2573. switch (ctx->enckeylen) {
  2574. case AES_KEYSIZE_128:
  2575. ctx->cipher_type = CIPHER_TYPE_AES128;
  2576. break;
  2577. case AES_KEYSIZE_192:
  2578. ctx->cipher_type = CIPHER_TYPE_AES192;
  2579. break;
  2580. case AES_KEYSIZE_256:
  2581. ctx->cipher_type = CIPHER_TYPE_AES256;
  2582. break;
  2583. default:
  2584. goto badkey;
  2585. }
  2586. memcpy(ctx->enckey, key, ctx->enckeylen);
  2587. flow_log(" enckeylen:%u authkeylen:%u\n", ctx->enckeylen,
  2588. ctx->authkeylen);
  2589. flow_dump(" enc: ", ctx->enckey, ctx->enckeylen);
  2590. flow_dump(" auth: ", ctx->authkey, ctx->authkeylen);
  2591. /* setkey the fallback just in case we need to use it */
  2592. if (ctx->fallback_cipher) {
  2593. flow_log(" running fallback setkey()\n");
  2594. ctx->fallback_cipher->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
  2595. ctx->fallback_cipher->base.crt_flags |=
  2596. tfm->crt_flags & CRYPTO_TFM_REQ_MASK;
  2597. ret = crypto_aead_setkey(ctx->fallback_cipher, key,
  2598. keylen + ctx->salt_len);
  2599. if (ret) {
  2600. flow_log(" fallback setkey() returned:%d\n", ret);
  2601. tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
  2602. tfm->crt_flags |=
  2603. (ctx->fallback_cipher->base.crt_flags &
  2604. CRYPTO_TFM_RES_MASK);
  2605. }
  2606. }
  2607. ctx->spu_resp_hdr_len = spu->spu_response_hdr_len(ctx->authkeylen,
  2608. ctx->enckeylen,
  2609. false);
  2610. atomic_inc(&iproc_priv.setkey_cnt[SPU_OP_AEAD]);
  2611. flow_log(" enckeylen:%u authkeylen:%u\n", ctx->enckeylen,
  2612. ctx->authkeylen);
  2613. return ret;
  2614. badkey:
  2615. ctx->enckeylen = 0;
  2616. ctx->authkeylen = 0;
  2617. ctx->digestsize = 0;
  2618. crypto_aead_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  2619. return -EINVAL;
  2620. }
  2621. /**
  2622. * aead_gcm_esp_setkey() - setkey() operation for ESP variant of GCM AES.
  2623. * @cipher: AEAD structure
  2624. * @key: Key followed by 4 bytes of salt
  2625. * @keylen: Length of key plus salt, in bytes
  2626. *
  2627. * Extracts salt from key and stores it to be prepended to IV on each request.
  2628. * Digest is always 16 bytes
  2629. *
  2630. * Return: Value from generic gcm setkey.
  2631. */
  2632. static int aead_gcm_esp_setkey(struct crypto_aead *cipher,
  2633. const u8 *key, unsigned int keylen)
  2634. {
  2635. struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
  2636. flow_log("%s\n", __func__);
  2637. if (keylen < GCM_ESP_SALT_SIZE)
  2638. return -EINVAL;
  2639. ctx->salt_len = GCM_ESP_SALT_SIZE;
  2640. ctx->salt_offset = GCM_ESP_SALT_OFFSET;
  2641. memcpy(ctx->salt, key + keylen - GCM_ESP_SALT_SIZE, GCM_ESP_SALT_SIZE);
  2642. keylen -= GCM_ESP_SALT_SIZE;
  2643. ctx->digestsize = GCM_ESP_DIGESTSIZE;
  2644. ctx->is_esp = true;
  2645. flow_dump("salt: ", ctx->salt, GCM_ESP_SALT_SIZE);
  2646. return aead_gcm_ccm_setkey(cipher, key, keylen);
  2647. }
  2648. /**
  2649. * rfc4543_gcm_esp_setkey() - setkey operation for RFC4543 variant of GCM/GMAC.
  2650. * cipher: AEAD structure
  2651. * key: Key followed by 4 bytes of salt
  2652. * keylen: Length of key plus salt, in bytes
  2653. *
  2654. * Extracts salt from key and stores it to be prepended to IV on each request.
  2655. * Digest is always 16 bytes
  2656. *
  2657. * Return: Value from generic gcm setkey.
  2658. */
  2659. static int rfc4543_gcm_esp_setkey(struct crypto_aead *cipher,
  2660. const u8 *key, unsigned int keylen)
  2661. {
  2662. struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
  2663. flow_log("%s\n", __func__);
  2664. if (keylen < GCM_ESP_SALT_SIZE)
  2665. return -EINVAL;
  2666. ctx->salt_len = GCM_ESP_SALT_SIZE;
  2667. ctx->salt_offset = GCM_ESP_SALT_OFFSET;
  2668. memcpy(ctx->salt, key + keylen - GCM_ESP_SALT_SIZE, GCM_ESP_SALT_SIZE);
  2669. keylen -= GCM_ESP_SALT_SIZE;
  2670. ctx->digestsize = GCM_ESP_DIGESTSIZE;
  2671. ctx->is_esp = true;
  2672. ctx->is_rfc4543 = true;
  2673. flow_dump("salt: ", ctx->salt, GCM_ESP_SALT_SIZE);
  2674. return aead_gcm_ccm_setkey(cipher, key, keylen);
  2675. }
  2676. /**
  2677. * aead_ccm_esp_setkey() - setkey() operation for ESP variant of CCM AES.
  2678. * @cipher: AEAD structure
  2679. * @key: Key followed by 4 bytes of salt
  2680. * @keylen: Length of key plus salt, in bytes
  2681. *
  2682. * Extracts salt from key and stores it to be prepended to IV on each request.
  2683. * Digest is always 16 bytes
  2684. *
  2685. * Return: Value from generic ccm setkey.
  2686. */
  2687. static int aead_ccm_esp_setkey(struct crypto_aead *cipher,
  2688. const u8 *key, unsigned int keylen)
  2689. {
  2690. struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
  2691. flow_log("%s\n", __func__);
  2692. if (keylen < CCM_ESP_SALT_SIZE)
  2693. return -EINVAL;
  2694. ctx->salt_len = CCM_ESP_SALT_SIZE;
  2695. ctx->salt_offset = CCM_ESP_SALT_OFFSET;
  2696. memcpy(ctx->salt, key + keylen - CCM_ESP_SALT_SIZE, CCM_ESP_SALT_SIZE);
  2697. keylen -= CCM_ESP_SALT_SIZE;
  2698. ctx->is_esp = true;
  2699. flow_dump("salt: ", ctx->salt, CCM_ESP_SALT_SIZE);
  2700. return aead_gcm_ccm_setkey(cipher, key, keylen);
  2701. }
  2702. static int aead_setauthsize(struct crypto_aead *cipher, unsigned int authsize)
  2703. {
  2704. struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
  2705. int ret = 0;
  2706. flow_log("%s() authkeylen:%u authsize:%u\n",
  2707. __func__, ctx->authkeylen, authsize);
  2708. ctx->digestsize = authsize;
  2709. /* setkey the fallback just in case we needto use it */
  2710. if (ctx->fallback_cipher) {
  2711. flow_log(" running fallback setauth()\n");
  2712. ret = crypto_aead_setauthsize(ctx->fallback_cipher, authsize);
  2713. if (ret)
  2714. flow_log(" fallback setauth() returned:%d\n", ret);
  2715. }
  2716. return ret;
  2717. }
  2718. static int aead_encrypt(struct aead_request *req)
  2719. {
  2720. flow_log("%s() cryptlen:%u %08x\n", __func__, req->cryptlen,
  2721. req->cryptlen);
  2722. dump_sg(req->src, 0, req->cryptlen + req->assoclen);
  2723. flow_log(" assoc_len:%u\n", req->assoclen);
  2724. return aead_enqueue(req, true);
  2725. }
  2726. static int aead_decrypt(struct aead_request *req)
  2727. {
  2728. flow_log("%s() cryptlen:%u\n", __func__, req->cryptlen);
  2729. dump_sg(req->src, 0, req->cryptlen + req->assoclen);
  2730. flow_log(" assoc_len:%u\n", req->assoclen);
  2731. return aead_enqueue(req, false);
  2732. }
  2733. /* ==================== Supported Cipher Algorithms ==================== */
  2734. static struct iproc_alg_s driver_algs[] = {
  2735. {
  2736. .type = CRYPTO_ALG_TYPE_AEAD,
  2737. .alg.aead = {
  2738. .base = {
  2739. .cra_name = "gcm(aes)",
  2740. .cra_driver_name = "gcm-aes-iproc",
  2741. .cra_blocksize = AES_BLOCK_SIZE,
  2742. .cra_flags = CRYPTO_ALG_NEED_FALLBACK
  2743. },
  2744. .setkey = aead_gcm_ccm_setkey,
  2745. .ivsize = GCM_AES_IV_SIZE,
  2746. .maxauthsize = AES_BLOCK_SIZE,
  2747. },
  2748. .cipher_info = {
  2749. .alg = CIPHER_ALG_AES,
  2750. .mode = CIPHER_MODE_GCM,
  2751. },
  2752. .auth_info = {
  2753. .alg = HASH_ALG_AES,
  2754. .mode = HASH_MODE_GCM,
  2755. },
  2756. .auth_first = 0,
  2757. },
  2758. {
  2759. .type = CRYPTO_ALG_TYPE_AEAD,
  2760. .alg.aead = {
  2761. .base = {
  2762. .cra_name = "ccm(aes)",
  2763. .cra_driver_name = "ccm-aes-iproc",
  2764. .cra_blocksize = AES_BLOCK_SIZE,
  2765. .cra_flags = CRYPTO_ALG_NEED_FALLBACK
  2766. },
  2767. .setkey = aead_gcm_ccm_setkey,
  2768. .ivsize = CCM_AES_IV_SIZE,
  2769. .maxauthsize = AES_BLOCK_SIZE,
  2770. },
  2771. .cipher_info = {
  2772. .alg = CIPHER_ALG_AES,
  2773. .mode = CIPHER_MODE_CCM,
  2774. },
  2775. .auth_info = {
  2776. .alg = HASH_ALG_AES,
  2777. .mode = HASH_MODE_CCM,
  2778. },
  2779. .auth_first = 0,
  2780. },
  2781. {
  2782. .type = CRYPTO_ALG_TYPE_AEAD,
  2783. .alg.aead = {
  2784. .base = {
  2785. .cra_name = "rfc4106(gcm(aes))",
  2786. .cra_driver_name = "gcm-aes-esp-iproc",
  2787. .cra_blocksize = AES_BLOCK_SIZE,
  2788. .cra_flags = CRYPTO_ALG_NEED_FALLBACK
  2789. },
  2790. .setkey = aead_gcm_esp_setkey,
  2791. .ivsize = GCM_RFC4106_IV_SIZE,
  2792. .maxauthsize = AES_BLOCK_SIZE,
  2793. },
  2794. .cipher_info = {
  2795. .alg = CIPHER_ALG_AES,
  2796. .mode = CIPHER_MODE_GCM,
  2797. },
  2798. .auth_info = {
  2799. .alg = HASH_ALG_AES,
  2800. .mode = HASH_MODE_GCM,
  2801. },
  2802. .auth_first = 0,
  2803. },
  2804. {
  2805. .type = CRYPTO_ALG_TYPE_AEAD,
  2806. .alg.aead = {
  2807. .base = {
  2808. .cra_name = "rfc4309(ccm(aes))",
  2809. .cra_driver_name = "ccm-aes-esp-iproc",
  2810. .cra_blocksize = AES_BLOCK_SIZE,
  2811. .cra_flags = CRYPTO_ALG_NEED_FALLBACK
  2812. },
  2813. .setkey = aead_ccm_esp_setkey,
  2814. .ivsize = CCM_AES_IV_SIZE,
  2815. .maxauthsize = AES_BLOCK_SIZE,
  2816. },
  2817. .cipher_info = {
  2818. .alg = CIPHER_ALG_AES,
  2819. .mode = CIPHER_MODE_CCM,
  2820. },
  2821. .auth_info = {
  2822. .alg = HASH_ALG_AES,
  2823. .mode = HASH_MODE_CCM,
  2824. },
  2825. .auth_first = 0,
  2826. },
  2827. {
  2828. .type = CRYPTO_ALG_TYPE_AEAD,
  2829. .alg.aead = {
  2830. .base = {
  2831. .cra_name = "rfc4543(gcm(aes))",
  2832. .cra_driver_name = "gmac-aes-esp-iproc",
  2833. .cra_blocksize = AES_BLOCK_SIZE,
  2834. .cra_flags = CRYPTO_ALG_NEED_FALLBACK
  2835. },
  2836. .setkey = rfc4543_gcm_esp_setkey,
  2837. .ivsize = GCM_RFC4106_IV_SIZE,
  2838. .maxauthsize = AES_BLOCK_SIZE,
  2839. },
  2840. .cipher_info = {
  2841. .alg = CIPHER_ALG_AES,
  2842. .mode = CIPHER_MODE_GCM,
  2843. },
  2844. .auth_info = {
  2845. .alg = HASH_ALG_AES,
  2846. .mode = HASH_MODE_GCM,
  2847. },
  2848. .auth_first = 0,
  2849. },
  2850. {
  2851. .type = CRYPTO_ALG_TYPE_AEAD,
  2852. .alg.aead = {
  2853. .base = {
  2854. .cra_name = "authenc(hmac(md5),cbc(aes))",
  2855. .cra_driver_name = "authenc-hmac-md5-cbc-aes-iproc",
  2856. .cra_blocksize = AES_BLOCK_SIZE,
  2857. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
  2858. },
  2859. .setkey = aead_authenc_setkey,
  2860. .ivsize = AES_BLOCK_SIZE,
  2861. .maxauthsize = MD5_DIGEST_SIZE,
  2862. },
  2863. .cipher_info = {
  2864. .alg = CIPHER_ALG_AES,
  2865. .mode = CIPHER_MODE_CBC,
  2866. },
  2867. .auth_info = {
  2868. .alg = HASH_ALG_MD5,
  2869. .mode = HASH_MODE_HMAC,
  2870. },
  2871. .auth_first = 0,
  2872. },
  2873. {
  2874. .type = CRYPTO_ALG_TYPE_AEAD,
  2875. .alg.aead = {
  2876. .base = {
  2877. .cra_name = "authenc(hmac(sha1),cbc(aes))",
  2878. .cra_driver_name = "authenc-hmac-sha1-cbc-aes-iproc",
  2879. .cra_blocksize = AES_BLOCK_SIZE,
  2880. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
  2881. },
  2882. .setkey = aead_authenc_setkey,
  2883. .ivsize = AES_BLOCK_SIZE,
  2884. .maxauthsize = SHA1_DIGEST_SIZE,
  2885. },
  2886. .cipher_info = {
  2887. .alg = CIPHER_ALG_AES,
  2888. .mode = CIPHER_MODE_CBC,
  2889. },
  2890. .auth_info = {
  2891. .alg = HASH_ALG_SHA1,
  2892. .mode = HASH_MODE_HMAC,
  2893. },
  2894. .auth_first = 0,
  2895. },
  2896. {
  2897. .type = CRYPTO_ALG_TYPE_AEAD,
  2898. .alg.aead = {
  2899. .base = {
  2900. .cra_name = "authenc(hmac(sha256),cbc(aes))",
  2901. .cra_driver_name = "authenc-hmac-sha256-cbc-aes-iproc",
  2902. .cra_blocksize = AES_BLOCK_SIZE,
  2903. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
  2904. },
  2905. .setkey = aead_authenc_setkey,
  2906. .ivsize = AES_BLOCK_SIZE,
  2907. .maxauthsize = SHA256_DIGEST_SIZE,
  2908. },
  2909. .cipher_info = {
  2910. .alg = CIPHER_ALG_AES,
  2911. .mode = CIPHER_MODE_CBC,
  2912. },
  2913. .auth_info = {
  2914. .alg = HASH_ALG_SHA256,
  2915. .mode = HASH_MODE_HMAC,
  2916. },
  2917. .auth_first = 0,
  2918. },
  2919. {
  2920. .type = CRYPTO_ALG_TYPE_AEAD,
  2921. .alg.aead = {
  2922. .base = {
  2923. .cra_name = "authenc(hmac(md5),cbc(des))",
  2924. .cra_driver_name = "authenc-hmac-md5-cbc-des-iproc",
  2925. .cra_blocksize = DES_BLOCK_SIZE,
  2926. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
  2927. },
  2928. .setkey = aead_authenc_setkey,
  2929. .ivsize = DES_BLOCK_SIZE,
  2930. .maxauthsize = MD5_DIGEST_SIZE,
  2931. },
  2932. .cipher_info = {
  2933. .alg = CIPHER_ALG_DES,
  2934. .mode = CIPHER_MODE_CBC,
  2935. },
  2936. .auth_info = {
  2937. .alg = HASH_ALG_MD5,
  2938. .mode = HASH_MODE_HMAC,
  2939. },
  2940. .auth_first = 0,
  2941. },
  2942. {
  2943. .type = CRYPTO_ALG_TYPE_AEAD,
  2944. .alg.aead = {
  2945. .base = {
  2946. .cra_name = "authenc(hmac(sha1),cbc(des))",
  2947. .cra_driver_name = "authenc-hmac-sha1-cbc-des-iproc",
  2948. .cra_blocksize = DES_BLOCK_SIZE,
  2949. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
  2950. },
  2951. .setkey = aead_authenc_setkey,
  2952. .ivsize = DES_BLOCK_SIZE,
  2953. .maxauthsize = SHA1_DIGEST_SIZE,
  2954. },
  2955. .cipher_info = {
  2956. .alg = CIPHER_ALG_DES,
  2957. .mode = CIPHER_MODE_CBC,
  2958. },
  2959. .auth_info = {
  2960. .alg = HASH_ALG_SHA1,
  2961. .mode = HASH_MODE_HMAC,
  2962. },
  2963. .auth_first = 0,
  2964. },
  2965. {
  2966. .type = CRYPTO_ALG_TYPE_AEAD,
  2967. .alg.aead = {
  2968. .base = {
  2969. .cra_name = "authenc(hmac(sha224),cbc(des))",
  2970. .cra_driver_name = "authenc-hmac-sha224-cbc-des-iproc",
  2971. .cra_blocksize = DES_BLOCK_SIZE,
  2972. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
  2973. },
  2974. .setkey = aead_authenc_setkey,
  2975. .ivsize = DES_BLOCK_SIZE,
  2976. .maxauthsize = SHA224_DIGEST_SIZE,
  2977. },
  2978. .cipher_info = {
  2979. .alg = CIPHER_ALG_DES,
  2980. .mode = CIPHER_MODE_CBC,
  2981. },
  2982. .auth_info = {
  2983. .alg = HASH_ALG_SHA224,
  2984. .mode = HASH_MODE_HMAC,
  2985. },
  2986. .auth_first = 0,
  2987. },
  2988. {
  2989. .type = CRYPTO_ALG_TYPE_AEAD,
  2990. .alg.aead = {
  2991. .base = {
  2992. .cra_name = "authenc(hmac(sha256),cbc(des))",
  2993. .cra_driver_name = "authenc-hmac-sha256-cbc-des-iproc",
  2994. .cra_blocksize = DES_BLOCK_SIZE,
  2995. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
  2996. },
  2997. .setkey = aead_authenc_setkey,
  2998. .ivsize = DES_BLOCK_SIZE,
  2999. .maxauthsize = SHA256_DIGEST_SIZE,
  3000. },
  3001. .cipher_info = {
  3002. .alg = CIPHER_ALG_DES,
  3003. .mode = CIPHER_MODE_CBC,
  3004. },
  3005. .auth_info = {
  3006. .alg = HASH_ALG_SHA256,
  3007. .mode = HASH_MODE_HMAC,
  3008. },
  3009. .auth_first = 0,
  3010. },
  3011. {
  3012. .type = CRYPTO_ALG_TYPE_AEAD,
  3013. .alg.aead = {
  3014. .base = {
  3015. .cra_name = "authenc(hmac(sha384),cbc(des))",
  3016. .cra_driver_name = "authenc-hmac-sha384-cbc-des-iproc",
  3017. .cra_blocksize = DES_BLOCK_SIZE,
  3018. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
  3019. },
  3020. .setkey = aead_authenc_setkey,
  3021. .ivsize = DES_BLOCK_SIZE,
  3022. .maxauthsize = SHA384_DIGEST_SIZE,
  3023. },
  3024. .cipher_info = {
  3025. .alg = CIPHER_ALG_DES,
  3026. .mode = CIPHER_MODE_CBC,
  3027. },
  3028. .auth_info = {
  3029. .alg = HASH_ALG_SHA384,
  3030. .mode = HASH_MODE_HMAC,
  3031. },
  3032. .auth_first = 0,
  3033. },
  3034. {
  3035. .type = CRYPTO_ALG_TYPE_AEAD,
  3036. .alg.aead = {
  3037. .base = {
  3038. .cra_name = "authenc(hmac(sha512),cbc(des))",
  3039. .cra_driver_name = "authenc-hmac-sha512-cbc-des-iproc",
  3040. .cra_blocksize = DES_BLOCK_SIZE,
  3041. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
  3042. },
  3043. .setkey = aead_authenc_setkey,
  3044. .ivsize = DES_BLOCK_SIZE,
  3045. .maxauthsize = SHA512_DIGEST_SIZE,
  3046. },
  3047. .cipher_info = {
  3048. .alg = CIPHER_ALG_DES,
  3049. .mode = CIPHER_MODE_CBC,
  3050. },
  3051. .auth_info = {
  3052. .alg = HASH_ALG_SHA512,
  3053. .mode = HASH_MODE_HMAC,
  3054. },
  3055. .auth_first = 0,
  3056. },
  3057. {
  3058. .type = CRYPTO_ALG_TYPE_AEAD,
  3059. .alg.aead = {
  3060. .base = {
  3061. .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
  3062. .cra_driver_name = "authenc-hmac-md5-cbc-des3-iproc",
  3063. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  3064. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
  3065. },
  3066. .setkey = aead_authenc_setkey,
  3067. .ivsize = DES3_EDE_BLOCK_SIZE,
  3068. .maxauthsize = MD5_DIGEST_SIZE,
  3069. },
  3070. .cipher_info = {
  3071. .alg = CIPHER_ALG_3DES,
  3072. .mode = CIPHER_MODE_CBC,
  3073. },
  3074. .auth_info = {
  3075. .alg = HASH_ALG_MD5,
  3076. .mode = HASH_MODE_HMAC,
  3077. },
  3078. .auth_first = 0,
  3079. },
  3080. {
  3081. .type = CRYPTO_ALG_TYPE_AEAD,
  3082. .alg.aead = {
  3083. .base = {
  3084. .cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
  3085. .cra_driver_name = "authenc-hmac-sha1-cbc-des3-iproc",
  3086. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  3087. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
  3088. },
  3089. .setkey = aead_authenc_setkey,
  3090. .ivsize = DES3_EDE_BLOCK_SIZE,
  3091. .maxauthsize = SHA1_DIGEST_SIZE,
  3092. },
  3093. .cipher_info = {
  3094. .alg = CIPHER_ALG_3DES,
  3095. .mode = CIPHER_MODE_CBC,
  3096. },
  3097. .auth_info = {
  3098. .alg = HASH_ALG_SHA1,
  3099. .mode = HASH_MODE_HMAC,
  3100. },
  3101. .auth_first = 0,
  3102. },
  3103. {
  3104. .type = CRYPTO_ALG_TYPE_AEAD,
  3105. .alg.aead = {
  3106. .base = {
  3107. .cra_name = "authenc(hmac(sha224),cbc(des3_ede))",
  3108. .cra_driver_name = "authenc-hmac-sha224-cbc-des3-iproc",
  3109. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  3110. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
  3111. },
  3112. .setkey = aead_authenc_setkey,
  3113. .ivsize = DES3_EDE_BLOCK_SIZE,
  3114. .maxauthsize = SHA224_DIGEST_SIZE,
  3115. },
  3116. .cipher_info = {
  3117. .alg = CIPHER_ALG_3DES,
  3118. .mode = CIPHER_MODE_CBC,
  3119. },
  3120. .auth_info = {
  3121. .alg = HASH_ALG_SHA224,
  3122. .mode = HASH_MODE_HMAC,
  3123. },
  3124. .auth_first = 0,
  3125. },
  3126. {
  3127. .type = CRYPTO_ALG_TYPE_AEAD,
  3128. .alg.aead = {
  3129. .base = {
  3130. .cra_name = "authenc(hmac(sha256),cbc(des3_ede))",
  3131. .cra_driver_name = "authenc-hmac-sha256-cbc-des3-iproc",
  3132. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  3133. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
  3134. },
  3135. .setkey = aead_authenc_setkey,
  3136. .ivsize = DES3_EDE_BLOCK_SIZE,
  3137. .maxauthsize = SHA256_DIGEST_SIZE,
  3138. },
  3139. .cipher_info = {
  3140. .alg = CIPHER_ALG_3DES,
  3141. .mode = CIPHER_MODE_CBC,
  3142. },
  3143. .auth_info = {
  3144. .alg = HASH_ALG_SHA256,
  3145. .mode = HASH_MODE_HMAC,
  3146. },
  3147. .auth_first = 0,
  3148. },
  3149. {
  3150. .type = CRYPTO_ALG_TYPE_AEAD,
  3151. .alg.aead = {
  3152. .base = {
  3153. .cra_name = "authenc(hmac(sha384),cbc(des3_ede))",
  3154. .cra_driver_name = "authenc-hmac-sha384-cbc-des3-iproc",
  3155. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  3156. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
  3157. },
  3158. .setkey = aead_authenc_setkey,
  3159. .ivsize = DES3_EDE_BLOCK_SIZE,
  3160. .maxauthsize = SHA384_DIGEST_SIZE,
  3161. },
  3162. .cipher_info = {
  3163. .alg = CIPHER_ALG_3DES,
  3164. .mode = CIPHER_MODE_CBC,
  3165. },
  3166. .auth_info = {
  3167. .alg = HASH_ALG_SHA384,
  3168. .mode = HASH_MODE_HMAC,
  3169. },
  3170. .auth_first = 0,
  3171. },
  3172. {
  3173. .type = CRYPTO_ALG_TYPE_AEAD,
  3174. .alg.aead = {
  3175. .base = {
  3176. .cra_name = "authenc(hmac(sha512),cbc(des3_ede))",
  3177. .cra_driver_name = "authenc-hmac-sha512-cbc-des3-iproc",
  3178. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  3179. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
  3180. },
  3181. .setkey = aead_authenc_setkey,
  3182. .ivsize = DES3_EDE_BLOCK_SIZE,
  3183. .maxauthsize = SHA512_DIGEST_SIZE,
  3184. },
  3185. .cipher_info = {
  3186. .alg = CIPHER_ALG_3DES,
  3187. .mode = CIPHER_MODE_CBC,
  3188. },
  3189. .auth_info = {
  3190. .alg = HASH_ALG_SHA512,
  3191. .mode = HASH_MODE_HMAC,
  3192. },
  3193. .auth_first = 0,
  3194. },
  3195. /* ABLKCIPHER algorithms. */
  3196. {
  3197. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  3198. .alg.crypto = {
  3199. .cra_name = "ecb(arc4)",
  3200. .cra_driver_name = "ecb-arc4-iproc",
  3201. .cra_blocksize = ARC4_BLOCK_SIZE,
  3202. .cra_ablkcipher = {
  3203. .min_keysize = ARC4_MIN_KEY_SIZE,
  3204. .max_keysize = ARC4_MAX_KEY_SIZE,
  3205. .ivsize = 0,
  3206. }
  3207. },
  3208. .cipher_info = {
  3209. .alg = CIPHER_ALG_RC4,
  3210. .mode = CIPHER_MODE_NONE,
  3211. },
  3212. .auth_info = {
  3213. .alg = HASH_ALG_NONE,
  3214. .mode = HASH_MODE_NONE,
  3215. },
  3216. },
  3217. {
  3218. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  3219. .alg.crypto = {
  3220. .cra_name = "ofb(des)",
  3221. .cra_driver_name = "ofb-des-iproc",
  3222. .cra_blocksize = DES_BLOCK_SIZE,
  3223. .cra_ablkcipher = {
  3224. .min_keysize = DES_KEY_SIZE,
  3225. .max_keysize = DES_KEY_SIZE,
  3226. .ivsize = DES_BLOCK_SIZE,
  3227. }
  3228. },
  3229. .cipher_info = {
  3230. .alg = CIPHER_ALG_DES,
  3231. .mode = CIPHER_MODE_OFB,
  3232. },
  3233. .auth_info = {
  3234. .alg = HASH_ALG_NONE,
  3235. .mode = HASH_MODE_NONE,
  3236. },
  3237. },
  3238. {
  3239. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  3240. .alg.crypto = {
  3241. .cra_name = "cbc(des)",
  3242. .cra_driver_name = "cbc-des-iproc",
  3243. .cra_blocksize = DES_BLOCK_SIZE,
  3244. .cra_ablkcipher = {
  3245. .min_keysize = DES_KEY_SIZE,
  3246. .max_keysize = DES_KEY_SIZE,
  3247. .ivsize = DES_BLOCK_SIZE,
  3248. }
  3249. },
  3250. .cipher_info = {
  3251. .alg = CIPHER_ALG_DES,
  3252. .mode = CIPHER_MODE_CBC,
  3253. },
  3254. .auth_info = {
  3255. .alg = HASH_ALG_NONE,
  3256. .mode = HASH_MODE_NONE,
  3257. },
  3258. },
  3259. {
  3260. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  3261. .alg.crypto = {
  3262. .cra_name = "ecb(des)",
  3263. .cra_driver_name = "ecb-des-iproc",
  3264. .cra_blocksize = DES_BLOCK_SIZE,
  3265. .cra_ablkcipher = {
  3266. .min_keysize = DES_KEY_SIZE,
  3267. .max_keysize = DES_KEY_SIZE,
  3268. .ivsize = 0,
  3269. }
  3270. },
  3271. .cipher_info = {
  3272. .alg = CIPHER_ALG_DES,
  3273. .mode = CIPHER_MODE_ECB,
  3274. },
  3275. .auth_info = {
  3276. .alg = HASH_ALG_NONE,
  3277. .mode = HASH_MODE_NONE,
  3278. },
  3279. },
  3280. {
  3281. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  3282. .alg.crypto = {
  3283. .cra_name = "ofb(des3_ede)",
  3284. .cra_driver_name = "ofb-des3-iproc",
  3285. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  3286. .cra_ablkcipher = {
  3287. .min_keysize = DES3_EDE_KEY_SIZE,
  3288. .max_keysize = DES3_EDE_KEY_SIZE,
  3289. .ivsize = DES3_EDE_BLOCK_SIZE,
  3290. }
  3291. },
  3292. .cipher_info = {
  3293. .alg = CIPHER_ALG_3DES,
  3294. .mode = CIPHER_MODE_OFB,
  3295. },
  3296. .auth_info = {
  3297. .alg = HASH_ALG_NONE,
  3298. .mode = HASH_MODE_NONE,
  3299. },
  3300. },
  3301. {
  3302. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  3303. .alg.crypto = {
  3304. .cra_name = "cbc(des3_ede)",
  3305. .cra_driver_name = "cbc-des3-iproc",
  3306. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  3307. .cra_ablkcipher = {
  3308. .min_keysize = DES3_EDE_KEY_SIZE,
  3309. .max_keysize = DES3_EDE_KEY_SIZE,
  3310. .ivsize = DES3_EDE_BLOCK_SIZE,
  3311. }
  3312. },
  3313. .cipher_info = {
  3314. .alg = CIPHER_ALG_3DES,
  3315. .mode = CIPHER_MODE_CBC,
  3316. },
  3317. .auth_info = {
  3318. .alg = HASH_ALG_NONE,
  3319. .mode = HASH_MODE_NONE,
  3320. },
  3321. },
  3322. {
  3323. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  3324. .alg.crypto = {
  3325. .cra_name = "ecb(des3_ede)",
  3326. .cra_driver_name = "ecb-des3-iproc",
  3327. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  3328. .cra_ablkcipher = {
  3329. .min_keysize = DES3_EDE_KEY_SIZE,
  3330. .max_keysize = DES3_EDE_KEY_SIZE,
  3331. .ivsize = 0,
  3332. }
  3333. },
  3334. .cipher_info = {
  3335. .alg = CIPHER_ALG_3DES,
  3336. .mode = CIPHER_MODE_ECB,
  3337. },
  3338. .auth_info = {
  3339. .alg = HASH_ALG_NONE,
  3340. .mode = HASH_MODE_NONE,
  3341. },
  3342. },
  3343. {
  3344. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  3345. .alg.crypto = {
  3346. .cra_name = "ofb(aes)",
  3347. .cra_driver_name = "ofb-aes-iproc",
  3348. .cra_blocksize = AES_BLOCK_SIZE,
  3349. .cra_ablkcipher = {
  3350. .min_keysize = AES_MIN_KEY_SIZE,
  3351. .max_keysize = AES_MAX_KEY_SIZE,
  3352. .ivsize = AES_BLOCK_SIZE,
  3353. }
  3354. },
  3355. .cipher_info = {
  3356. .alg = CIPHER_ALG_AES,
  3357. .mode = CIPHER_MODE_OFB,
  3358. },
  3359. .auth_info = {
  3360. .alg = HASH_ALG_NONE,
  3361. .mode = HASH_MODE_NONE,
  3362. },
  3363. },
  3364. {
  3365. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  3366. .alg.crypto = {
  3367. .cra_name = "cbc(aes)",
  3368. .cra_driver_name = "cbc-aes-iproc",
  3369. .cra_blocksize = AES_BLOCK_SIZE,
  3370. .cra_ablkcipher = {
  3371. .min_keysize = AES_MIN_KEY_SIZE,
  3372. .max_keysize = AES_MAX_KEY_SIZE,
  3373. .ivsize = AES_BLOCK_SIZE,
  3374. }
  3375. },
  3376. .cipher_info = {
  3377. .alg = CIPHER_ALG_AES,
  3378. .mode = CIPHER_MODE_CBC,
  3379. },
  3380. .auth_info = {
  3381. .alg = HASH_ALG_NONE,
  3382. .mode = HASH_MODE_NONE,
  3383. },
  3384. },
  3385. {
  3386. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  3387. .alg.crypto = {
  3388. .cra_name = "ecb(aes)",
  3389. .cra_driver_name = "ecb-aes-iproc",
  3390. .cra_blocksize = AES_BLOCK_SIZE,
  3391. .cra_ablkcipher = {
  3392. .min_keysize = AES_MIN_KEY_SIZE,
  3393. .max_keysize = AES_MAX_KEY_SIZE,
  3394. .ivsize = 0,
  3395. }
  3396. },
  3397. .cipher_info = {
  3398. .alg = CIPHER_ALG_AES,
  3399. .mode = CIPHER_MODE_ECB,
  3400. },
  3401. .auth_info = {
  3402. .alg = HASH_ALG_NONE,
  3403. .mode = HASH_MODE_NONE,
  3404. },
  3405. },
  3406. {
  3407. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  3408. .alg.crypto = {
  3409. .cra_name = "ctr(aes)",
  3410. .cra_driver_name = "ctr-aes-iproc",
  3411. .cra_blocksize = AES_BLOCK_SIZE,
  3412. .cra_ablkcipher = {
  3413. /* .geniv = "chainiv", */
  3414. .min_keysize = AES_MIN_KEY_SIZE,
  3415. .max_keysize = AES_MAX_KEY_SIZE,
  3416. .ivsize = AES_BLOCK_SIZE,
  3417. }
  3418. },
  3419. .cipher_info = {
  3420. .alg = CIPHER_ALG_AES,
  3421. .mode = CIPHER_MODE_CTR,
  3422. },
  3423. .auth_info = {
  3424. .alg = HASH_ALG_NONE,
  3425. .mode = HASH_MODE_NONE,
  3426. },
  3427. },
  3428. {
  3429. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  3430. .alg.crypto = {
  3431. .cra_name = "xts(aes)",
  3432. .cra_driver_name = "xts-aes-iproc",
  3433. .cra_blocksize = AES_BLOCK_SIZE,
  3434. .cra_ablkcipher = {
  3435. .min_keysize = 2 * AES_MIN_KEY_SIZE,
  3436. .max_keysize = 2 * AES_MAX_KEY_SIZE,
  3437. .ivsize = AES_BLOCK_SIZE,
  3438. }
  3439. },
  3440. .cipher_info = {
  3441. .alg = CIPHER_ALG_AES,
  3442. .mode = CIPHER_MODE_XTS,
  3443. },
  3444. .auth_info = {
  3445. .alg = HASH_ALG_NONE,
  3446. .mode = HASH_MODE_NONE,
  3447. },
  3448. },
  3449. /* AHASH algorithms. */
  3450. {
  3451. .type = CRYPTO_ALG_TYPE_AHASH,
  3452. .alg.hash = {
  3453. .halg.digestsize = MD5_DIGEST_SIZE,
  3454. .halg.base = {
  3455. .cra_name = "md5",
  3456. .cra_driver_name = "md5-iproc",
  3457. .cra_blocksize = MD5_BLOCK_WORDS * 4,
  3458. .cra_flags = CRYPTO_ALG_ASYNC,
  3459. }
  3460. },
  3461. .cipher_info = {
  3462. .alg = CIPHER_ALG_NONE,
  3463. .mode = CIPHER_MODE_NONE,
  3464. },
  3465. .auth_info = {
  3466. .alg = HASH_ALG_MD5,
  3467. .mode = HASH_MODE_HASH,
  3468. },
  3469. },
  3470. {
  3471. .type = CRYPTO_ALG_TYPE_AHASH,
  3472. .alg.hash = {
  3473. .halg.digestsize = MD5_DIGEST_SIZE,
  3474. .halg.base = {
  3475. .cra_name = "hmac(md5)",
  3476. .cra_driver_name = "hmac-md5-iproc",
  3477. .cra_blocksize = MD5_BLOCK_WORDS * 4,
  3478. }
  3479. },
  3480. .cipher_info = {
  3481. .alg = CIPHER_ALG_NONE,
  3482. .mode = CIPHER_MODE_NONE,
  3483. },
  3484. .auth_info = {
  3485. .alg = HASH_ALG_MD5,
  3486. .mode = HASH_MODE_HMAC,
  3487. },
  3488. },
  3489. {.type = CRYPTO_ALG_TYPE_AHASH,
  3490. .alg.hash = {
  3491. .halg.digestsize = SHA1_DIGEST_SIZE,
  3492. .halg.base = {
  3493. .cra_name = "sha1",
  3494. .cra_driver_name = "sha1-iproc",
  3495. .cra_blocksize = SHA1_BLOCK_SIZE,
  3496. }
  3497. },
  3498. .cipher_info = {
  3499. .alg = CIPHER_ALG_NONE,
  3500. .mode = CIPHER_MODE_NONE,
  3501. },
  3502. .auth_info = {
  3503. .alg = HASH_ALG_SHA1,
  3504. .mode = HASH_MODE_HASH,
  3505. },
  3506. },
  3507. {.type = CRYPTO_ALG_TYPE_AHASH,
  3508. .alg.hash = {
  3509. .halg.digestsize = SHA1_DIGEST_SIZE,
  3510. .halg.base = {
  3511. .cra_name = "hmac(sha1)",
  3512. .cra_driver_name = "hmac-sha1-iproc",
  3513. .cra_blocksize = SHA1_BLOCK_SIZE,
  3514. }
  3515. },
  3516. .cipher_info = {
  3517. .alg = CIPHER_ALG_NONE,
  3518. .mode = CIPHER_MODE_NONE,
  3519. },
  3520. .auth_info = {
  3521. .alg = HASH_ALG_SHA1,
  3522. .mode = HASH_MODE_HMAC,
  3523. },
  3524. },
  3525. {.type = CRYPTO_ALG_TYPE_AHASH,
  3526. .alg.hash = {
  3527. .halg.digestsize = SHA224_DIGEST_SIZE,
  3528. .halg.base = {
  3529. .cra_name = "sha224",
  3530. .cra_driver_name = "sha224-iproc",
  3531. .cra_blocksize = SHA224_BLOCK_SIZE,
  3532. }
  3533. },
  3534. .cipher_info = {
  3535. .alg = CIPHER_ALG_NONE,
  3536. .mode = CIPHER_MODE_NONE,
  3537. },
  3538. .auth_info = {
  3539. .alg = HASH_ALG_SHA224,
  3540. .mode = HASH_MODE_HASH,
  3541. },
  3542. },
  3543. {.type = CRYPTO_ALG_TYPE_AHASH,
  3544. .alg.hash = {
  3545. .halg.digestsize = SHA224_DIGEST_SIZE,
  3546. .halg.base = {
  3547. .cra_name = "hmac(sha224)",
  3548. .cra_driver_name = "hmac-sha224-iproc",
  3549. .cra_blocksize = SHA224_BLOCK_SIZE,
  3550. }
  3551. },
  3552. .cipher_info = {
  3553. .alg = CIPHER_ALG_NONE,
  3554. .mode = CIPHER_MODE_NONE,
  3555. },
  3556. .auth_info = {
  3557. .alg = HASH_ALG_SHA224,
  3558. .mode = HASH_MODE_HMAC,
  3559. },
  3560. },
  3561. {.type = CRYPTO_ALG_TYPE_AHASH,
  3562. .alg.hash = {
  3563. .halg.digestsize = SHA256_DIGEST_SIZE,
  3564. .halg.base = {
  3565. .cra_name = "sha256",
  3566. .cra_driver_name = "sha256-iproc",
  3567. .cra_blocksize = SHA256_BLOCK_SIZE,
  3568. }
  3569. },
  3570. .cipher_info = {
  3571. .alg = CIPHER_ALG_NONE,
  3572. .mode = CIPHER_MODE_NONE,
  3573. },
  3574. .auth_info = {
  3575. .alg = HASH_ALG_SHA256,
  3576. .mode = HASH_MODE_HASH,
  3577. },
  3578. },
  3579. {.type = CRYPTO_ALG_TYPE_AHASH,
  3580. .alg.hash = {
  3581. .halg.digestsize = SHA256_DIGEST_SIZE,
  3582. .halg.base = {
  3583. .cra_name = "hmac(sha256)",
  3584. .cra_driver_name = "hmac-sha256-iproc",
  3585. .cra_blocksize = SHA256_BLOCK_SIZE,
  3586. }
  3587. },
  3588. .cipher_info = {
  3589. .alg = CIPHER_ALG_NONE,
  3590. .mode = CIPHER_MODE_NONE,
  3591. },
  3592. .auth_info = {
  3593. .alg = HASH_ALG_SHA256,
  3594. .mode = HASH_MODE_HMAC,
  3595. },
  3596. },
  3597. {
  3598. .type = CRYPTO_ALG_TYPE_AHASH,
  3599. .alg.hash = {
  3600. .halg.digestsize = SHA384_DIGEST_SIZE,
  3601. .halg.base = {
  3602. .cra_name = "sha384",
  3603. .cra_driver_name = "sha384-iproc",
  3604. .cra_blocksize = SHA384_BLOCK_SIZE,
  3605. }
  3606. },
  3607. .cipher_info = {
  3608. .alg = CIPHER_ALG_NONE,
  3609. .mode = CIPHER_MODE_NONE,
  3610. },
  3611. .auth_info = {
  3612. .alg = HASH_ALG_SHA384,
  3613. .mode = HASH_MODE_HASH,
  3614. },
  3615. },
  3616. {
  3617. .type = CRYPTO_ALG_TYPE_AHASH,
  3618. .alg.hash = {
  3619. .halg.digestsize = SHA384_DIGEST_SIZE,
  3620. .halg.base = {
  3621. .cra_name = "hmac(sha384)",
  3622. .cra_driver_name = "hmac-sha384-iproc",
  3623. .cra_blocksize = SHA384_BLOCK_SIZE,
  3624. }
  3625. },
  3626. .cipher_info = {
  3627. .alg = CIPHER_ALG_NONE,
  3628. .mode = CIPHER_MODE_NONE,
  3629. },
  3630. .auth_info = {
  3631. .alg = HASH_ALG_SHA384,
  3632. .mode = HASH_MODE_HMAC,
  3633. },
  3634. },
  3635. {
  3636. .type = CRYPTO_ALG_TYPE_AHASH,
  3637. .alg.hash = {
  3638. .halg.digestsize = SHA512_DIGEST_SIZE,
  3639. .halg.base = {
  3640. .cra_name = "sha512",
  3641. .cra_driver_name = "sha512-iproc",
  3642. .cra_blocksize = SHA512_BLOCK_SIZE,
  3643. }
  3644. },
  3645. .cipher_info = {
  3646. .alg = CIPHER_ALG_NONE,
  3647. .mode = CIPHER_MODE_NONE,
  3648. },
  3649. .auth_info = {
  3650. .alg = HASH_ALG_SHA512,
  3651. .mode = HASH_MODE_HASH,
  3652. },
  3653. },
  3654. {
  3655. .type = CRYPTO_ALG_TYPE_AHASH,
  3656. .alg.hash = {
  3657. .halg.digestsize = SHA512_DIGEST_SIZE,
  3658. .halg.base = {
  3659. .cra_name = "hmac(sha512)",
  3660. .cra_driver_name = "hmac-sha512-iproc",
  3661. .cra_blocksize = SHA512_BLOCK_SIZE,
  3662. }
  3663. },
  3664. .cipher_info = {
  3665. .alg = CIPHER_ALG_NONE,
  3666. .mode = CIPHER_MODE_NONE,
  3667. },
  3668. .auth_info = {
  3669. .alg = HASH_ALG_SHA512,
  3670. .mode = HASH_MODE_HMAC,
  3671. },
  3672. },
  3673. {
  3674. .type = CRYPTO_ALG_TYPE_AHASH,
  3675. .alg.hash = {
  3676. .halg.digestsize = SHA3_224_DIGEST_SIZE,
  3677. .halg.base = {
  3678. .cra_name = "sha3-224",
  3679. .cra_driver_name = "sha3-224-iproc",
  3680. .cra_blocksize = SHA3_224_BLOCK_SIZE,
  3681. }
  3682. },
  3683. .cipher_info = {
  3684. .alg = CIPHER_ALG_NONE,
  3685. .mode = CIPHER_MODE_NONE,
  3686. },
  3687. .auth_info = {
  3688. .alg = HASH_ALG_SHA3_224,
  3689. .mode = HASH_MODE_HASH,
  3690. },
  3691. },
  3692. {
  3693. .type = CRYPTO_ALG_TYPE_AHASH,
  3694. .alg.hash = {
  3695. .halg.digestsize = SHA3_224_DIGEST_SIZE,
  3696. .halg.base = {
  3697. .cra_name = "hmac(sha3-224)",
  3698. .cra_driver_name = "hmac-sha3-224-iproc",
  3699. .cra_blocksize = SHA3_224_BLOCK_SIZE,
  3700. }
  3701. },
  3702. .cipher_info = {
  3703. .alg = CIPHER_ALG_NONE,
  3704. .mode = CIPHER_MODE_NONE,
  3705. },
  3706. .auth_info = {
  3707. .alg = HASH_ALG_SHA3_224,
  3708. .mode = HASH_MODE_HMAC
  3709. },
  3710. },
  3711. {
  3712. .type = CRYPTO_ALG_TYPE_AHASH,
  3713. .alg.hash = {
  3714. .halg.digestsize = SHA3_256_DIGEST_SIZE,
  3715. .halg.base = {
  3716. .cra_name = "sha3-256",
  3717. .cra_driver_name = "sha3-256-iproc",
  3718. .cra_blocksize = SHA3_256_BLOCK_SIZE,
  3719. }
  3720. },
  3721. .cipher_info = {
  3722. .alg = CIPHER_ALG_NONE,
  3723. .mode = CIPHER_MODE_NONE,
  3724. },
  3725. .auth_info = {
  3726. .alg = HASH_ALG_SHA3_256,
  3727. .mode = HASH_MODE_HASH,
  3728. },
  3729. },
  3730. {
  3731. .type = CRYPTO_ALG_TYPE_AHASH,
  3732. .alg.hash = {
  3733. .halg.digestsize = SHA3_256_DIGEST_SIZE,
  3734. .halg.base = {
  3735. .cra_name = "hmac(sha3-256)",
  3736. .cra_driver_name = "hmac-sha3-256-iproc",
  3737. .cra_blocksize = SHA3_256_BLOCK_SIZE,
  3738. }
  3739. },
  3740. .cipher_info = {
  3741. .alg = CIPHER_ALG_NONE,
  3742. .mode = CIPHER_MODE_NONE,
  3743. },
  3744. .auth_info = {
  3745. .alg = HASH_ALG_SHA3_256,
  3746. .mode = HASH_MODE_HMAC,
  3747. },
  3748. },
  3749. {
  3750. .type = CRYPTO_ALG_TYPE_AHASH,
  3751. .alg.hash = {
  3752. .halg.digestsize = SHA3_384_DIGEST_SIZE,
  3753. .halg.base = {
  3754. .cra_name = "sha3-384",
  3755. .cra_driver_name = "sha3-384-iproc",
  3756. .cra_blocksize = SHA3_224_BLOCK_SIZE,
  3757. }
  3758. },
  3759. .cipher_info = {
  3760. .alg = CIPHER_ALG_NONE,
  3761. .mode = CIPHER_MODE_NONE,
  3762. },
  3763. .auth_info = {
  3764. .alg = HASH_ALG_SHA3_384,
  3765. .mode = HASH_MODE_HASH,
  3766. },
  3767. },
  3768. {
  3769. .type = CRYPTO_ALG_TYPE_AHASH,
  3770. .alg.hash = {
  3771. .halg.digestsize = SHA3_384_DIGEST_SIZE,
  3772. .halg.base = {
  3773. .cra_name = "hmac(sha3-384)",
  3774. .cra_driver_name = "hmac-sha3-384-iproc",
  3775. .cra_blocksize = SHA3_384_BLOCK_SIZE,
  3776. }
  3777. },
  3778. .cipher_info = {
  3779. .alg = CIPHER_ALG_NONE,
  3780. .mode = CIPHER_MODE_NONE,
  3781. },
  3782. .auth_info = {
  3783. .alg = HASH_ALG_SHA3_384,
  3784. .mode = HASH_MODE_HMAC,
  3785. },
  3786. },
  3787. {
  3788. .type = CRYPTO_ALG_TYPE_AHASH,
  3789. .alg.hash = {
  3790. .halg.digestsize = SHA3_512_DIGEST_SIZE,
  3791. .halg.base = {
  3792. .cra_name = "sha3-512",
  3793. .cra_driver_name = "sha3-512-iproc",
  3794. .cra_blocksize = SHA3_512_BLOCK_SIZE,
  3795. }
  3796. },
  3797. .cipher_info = {
  3798. .alg = CIPHER_ALG_NONE,
  3799. .mode = CIPHER_MODE_NONE,
  3800. },
  3801. .auth_info = {
  3802. .alg = HASH_ALG_SHA3_512,
  3803. .mode = HASH_MODE_HASH,
  3804. },
  3805. },
  3806. {
  3807. .type = CRYPTO_ALG_TYPE_AHASH,
  3808. .alg.hash = {
  3809. .halg.digestsize = SHA3_512_DIGEST_SIZE,
  3810. .halg.base = {
  3811. .cra_name = "hmac(sha3-512)",
  3812. .cra_driver_name = "hmac-sha3-512-iproc",
  3813. .cra_blocksize = SHA3_512_BLOCK_SIZE,
  3814. }
  3815. },
  3816. .cipher_info = {
  3817. .alg = CIPHER_ALG_NONE,
  3818. .mode = CIPHER_MODE_NONE,
  3819. },
  3820. .auth_info = {
  3821. .alg = HASH_ALG_SHA3_512,
  3822. .mode = HASH_MODE_HMAC,
  3823. },
  3824. },
  3825. {
  3826. .type = CRYPTO_ALG_TYPE_AHASH,
  3827. .alg.hash = {
  3828. .halg.digestsize = AES_BLOCK_SIZE,
  3829. .halg.base = {
  3830. .cra_name = "xcbc(aes)",
  3831. .cra_driver_name = "xcbc-aes-iproc",
  3832. .cra_blocksize = AES_BLOCK_SIZE,
  3833. }
  3834. },
  3835. .cipher_info = {
  3836. .alg = CIPHER_ALG_NONE,
  3837. .mode = CIPHER_MODE_NONE,
  3838. },
  3839. .auth_info = {
  3840. .alg = HASH_ALG_AES,
  3841. .mode = HASH_MODE_XCBC,
  3842. },
  3843. },
  3844. {
  3845. .type = CRYPTO_ALG_TYPE_AHASH,
  3846. .alg.hash = {
  3847. .halg.digestsize = AES_BLOCK_SIZE,
  3848. .halg.base = {
  3849. .cra_name = "cmac(aes)",
  3850. .cra_driver_name = "cmac-aes-iproc",
  3851. .cra_blocksize = AES_BLOCK_SIZE,
  3852. }
  3853. },
  3854. .cipher_info = {
  3855. .alg = CIPHER_ALG_NONE,
  3856. .mode = CIPHER_MODE_NONE,
  3857. },
  3858. .auth_info = {
  3859. .alg = HASH_ALG_AES,
  3860. .mode = HASH_MODE_CMAC,
  3861. },
  3862. },
  3863. };
  3864. static int generic_cra_init(struct crypto_tfm *tfm,
  3865. struct iproc_alg_s *cipher_alg)
  3866. {
  3867. struct spu_hw *spu = &iproc_priv.spu;
  3868. struct iproc_ctx_s *ctx = crypto_tfm_ctx(tfm);
  3869. unsigned int blocksize = crypto_tfm_alg_blocksize(tfm);
  3870. flow_log("%s()\n", __func__);
  3871. ctx->alg = cipher_alg;
  3872. ctx->cipher = cipher_alg->cipher_info;
  3873. ctx->auth = cipher_alg->auth_info;
  3874. ctx->auth_first = cipher_alg->auth_first;
  3875. ctx->max_payload = spu->spu_ctx_max_payload(ctx->cipher.alg,
  3876. ctx->cipher.mode,
  3877. blocksize);
  3878. ctx->fallback_cipher = NULL;
  3879. ctx->enckeylen = 0;
  3880. ctx->authkeylen = 0;
  3881. atomic_inc(&iproc_priv.stream_count);
  3882. atomic_inc(&iproc_priv.session_count);
  3883. return 0;
  3884. }
  3885. static int ablkcipher_cra_init(struct crypto_tfm *tfm)
  3886. {
  3887. struct crypto_alg *alg = tfm->__crt_alg;
  3888. struct iproc_alg_s *cipher_alg;
  3889. flow_log("%s()\n", __func__);
  3890. tfm->crt_ablkcipher.reqsize = sizeof(struct iproc_reqctx_s);
  3891. cipher_alg = container_of(alg, struct iproc_alg_s, alg.crypto);
  3892. return generic_cra_init(tfm, cipher_alg);
  3893. }
  3894. static int ahash_cra_init(struct crypto_tfm *tfm)
  3895. {
  3896. int err;
  3897. struct crypto_alg *alg = tfm->__crt_alg;
  3898. struct iproc_alg_s *cipher_alg;
  3899. cipher_alg = container_of(__crypto_ahash_alg(alg), struct iproc_alg_s,
  3900. alg.hash);
  3901. err = generic_cra_init(tfm, cipher_alg);
  3902. flow_log("%s()\n", __func__);
  3903. /*
  3904. * export state size has to be < 512 bytes. So don't include msg bufs
  3905. * in state size.
  3906. */
  3907. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  3908. sizeof(struct iproc_reqctx_s));
  3909. return err;
  3910. }
  3911. static int aead_cra_init(struct crypto_aead *aead)
  3912. {
  3913. struct crypto_tfm *tfm = crypto_aead_tfm(aead);
  3914. struct iproc_ctx_s *ctx = crypto_tfm_ctx(tfm);
  3915. struct crypto_alg *alg = tfm->__crt_alg;
  3916. struct aead_alg *aalg = container_of(alg, struct aead_alg, base);
  3917. struct iproc_alg_s *cipher_alg = container_of(aalg, struct iproc_alg_s,
  3918. alg.aead);
  3919. int err = generic_cra_init(tfm, cipher_alg);
  3920. flow_log("%s()\n", __func__);
  3921. crypto_aead_set_reqsize(aead, sizeof(struct iproc_reqctx_s));
  3922. ctx->is_esp = false;
  3923. ctx->salt_len = 0;
  3924. ctx->salt_offset = 0;
  3925. /* random first IV */
  3926. get_random_bytes(ctx->iv, MAX_IV_SIZE);
  3927. flow_dump(" iv: ", ctx->iv, MAX_IV_SIZE);
  3928. if (!err) {
  3929. if (alg->cra_flags & CRYPTO_ALG_NEED_FALLBACK) {
  3930. flow_log("%s() creating fallback cipher\n", __func__);
  3931. ctx->fallback_cipher =
  3932. crypto_alloc_aead(alg->cra_name, 0,
  3933. CRYPTO_ALG_ASYNC |
  3934. CRYPTO_ALG_NEED_FALLBACK);
  3935. if (IS_ERR(ctx->fallback_cipher)) {
  3936. pr_err("%s() Error: failed to allocate fallback for %s\n",
  3937. __func__, alg->cra_name);
  3938. return PTR_ERR(ctx->fallback_cipher);
  3939. }
  3940. }
  3941. }
  3942. return err;
  3943. }
  3944. static void generic_cra_exit(struct crypto_tfm *tfm)
  3945. {
  3946. atomic_dec(&iproc_priv.session_count);
  3947. }
  3948. static void aead_cra_exit(struct crypto_aead *aead)
  3949. {
  3950. struct crypto_tfm *tfm = crypto_aead_tfm(aead);
  3951. struct iproc_ctx_s *ctx = crypto_tfm_ctx(tfm);
  3952. generic_cra_exit(tfm);
  3953. if (ctx->fallback_cipher) {
  3954. crypto_free_aead(ctx->fallback_cipher);
  3955. ctx->fallback_cipher = NULL;
  3956. }
  3957. }
  3958. /**
  3959. * spu_functions_register() - Specify hardware-specific SPU functions based on
  3960. * SPU type read from device tree.
  3961. * @dev: device structure
  3962. * @spu_type: SPU hardware generation
  3963. * @spu_subtype: SPU hardware version
  3964. */
  3965. static void spu_functions_register(struct device *dev,
  3966. enum spu_spu_type spu_type,
  3967. enum spu_spu_subtype spu_subtype)
  3968. {
  3969. struct spu_hw *spu = &iproc_priv.spu;
  3970. if (spu_type == SPU_TYPE_SPUM) {
  3971. dev_dbg(dev, "Registering SPUM functions");
  3972. spu->spu_dump_msg_hdr = spum_dump_msg_hdr;
  3973. spu->spu_payload_length = spum_payload_length;
  3974. spu->spu_response_hdr_len = spum_response_hdr_len;
  3975. spu->spu_hash_pad_len = spum_hash_pad_len;
  3976. spu->spu_gcm_ccm_pad_len = spum_gcm_ccm_pad_len;
  3977. spu->spu_assoc_resp_len = spum_assoc_resp_len;
  3978. spu->spu_aead_ivlen = spum_aead_ivlen;
  3979. spu->spu_hash_type = spum_hash_type;
  3980. spu->spu_digest_size = spum_digest_size;
  3981. spu->spu_create_request = spum_create_request;
  3982. spu->spu_cipher_req_init = spum_cipher_req_init;
  3983. spu->spu_cipher_req_finish = spum_cipher_req_finish;
  3984. spu->spu_request_pad = spum_request_pad;
  3985. spu->spu_tx_status_len = spum_tx_status_len;
  3986. spu->spu_rx_status_len = spum_rx_status_len;
  3987. spu->spu_status_process = spum_status_process;
  3988. spu->spu_xts_tweak_in_payload = spum_xts_tweak_in_payload;
  3989. spu->spu_ccm_update_iv = spum_ccm_update_iv;
  3990. spu->spu_wordalign_padlen = spum_wordalign_padlen;
  3991. if (spu_subtype == SPU_SUBTYPE_SPUM_NS2)
  3992. spu->spu_ctx_max_payload = spum_ns2_ctx_max_payload;
  3993. else
  3994. spu->spu_ctx_max_payload = spum_nsp_ctx_max_payload;
  3995. } else {
  3996. dev_dbg(dev, "Registering SPU2 functions");
  3997. spu->spu_dump_msg_hdr = spu2_dump_msg_hdr;
  3998. spu->spu_ctx_max_payload = spu2_ctx_max_payload;
  3999. spu->spu_payload_length = spu2_payload_length;
  4000. spu->spu_response_hdr_len = spu2_response_hdr_len;
  4001. spu->spu_hash_pad_len = spu2_hash_pad_len;
  4002. spu->spu_gcm_ccm_pad_len = spu2_gcm_ccm_pad_len;
  4003. spu->spu_assoc_resp_len = spu2_assoc_resp_len;
  4004. spu->spu_aead_ivlen = spu2_aead_ivlen;
  4005. spu->spu_hash_type = spu2_hash_type;
  4006. spu->spu_digest_size = spu2_digest_size;
  4007. spu->spu_create_request = spu2_create_request;
  4008. spu->spu_cipher_req_init = spu2_cipher_req_init;
  4009. spu->spu_cipher_req_finish = spu2_cipher_req_finish;
  4010. spu->spu_request_pad = spu2_request_pad;
  4011. spu->spu_tx_status_len = spu2_tx_status_len;
  4012. spu->spu_rx_status_len = spu2_rx_status_len;
  4013. spu->spu_status_process = spu2_status_process;
  4014. spu->spu_xts_tweak_in_payload = spu2_xts_tweak_in_payload;
  4015. spu->spu_ccm_update_iv = spu2_ccm_update_iv;
  4016. spu->spu_wordalign_padlen = spu2_wordalign_padlen;
  4017. }
  4018. }
  4019. /**
  4020. * spu_mb_init() - Initialize mailbox client. Request ownership of a mailbox
  4021. * channel for the SPU being probed.
  4022. * @dev: SPU driver device structure
  4023. *
  4024. * Return: 0 if successful
  4025. * < 0 otherwise
  4026. */
  4027. static int spu_mb_init(struct device *dev)
  4028. {
  4029. struct mbox_client *mcl = &iproc_priv.mcl;
  4030. int err, i;
  4031. iproc_priv.mbox = devm_kcalloc(dev, iproc_priv.spu.num_chan,
  4032. sizeof(struct mbox_chan *), GFP_KERNEL);
  4033. if (!iproc_priv.mbox)
  4034. return -ENOMEM;
  4035. mcl->dev = dev;
  4036. mcl->tx_block = false;
  4037. mcl->tx_tout = 0;
  4038. mcl->knows_txdone = true;
  4039. mcl->rx_callback = spu_rx_callback;
  4040. mcl->tx_done = NULL;
  4041. for (i = 0; i < iproc_priv.spu.num_chan; i++) {
  4042. iproc_priv.mbox[i] = mbox_request_channel(mcl, i);
  4043. if (IS_ERR(iproc_priv.mbox[i])) {
  4044. err = (int)PTR_ERR(iproc_priv.mbox[i]);
  4045. dev_err(dev,
  4046. "Mbox channel %d request failed with err %d",
  4047. i, err);
  4048. iproc_priv.mbox[i] = NULL;
  4049. goto free_channels;
  4050. }
  4051. }
  4052. return 0;
  4053. free_channels:
  4054. for (i = 0; i < iproc_priv.spu.num_chan; i++) {
  4055. if (iproc_priv.mbox[i])
  4056. mbox_free_channel(iproc_priv.mbox[i]);
  4057. }
  4058. return err;
  4059. }
  4060. static void spu_mb_release(struct platform_device *pdev)
  4061. {
  4062. int i;
  4063. for (i = 0; i < iproc_priv.spu.num_chan; i++)
  4064. mbox_free_channel(iproc_priv.mbox[i]);
  4065. }
  4066. static void spu_counters_init(void)
  4067. {
  4068. int i;
  4069. int j;
  4070. atomic_set(&iproc_priv.session_count, 0);
  4071. atomic_set(&iproc_priv.stream_count, 0);
  4072. atomic_set(&iproc_priv.next_chan, (int)iproc_priv.spu.num_chan);
  4073. atomic64_set(&iproc_priv.bytes_in, 0);
  4074. atomic64_set(&iproc_priv.bytes_out, 0);
  4075. for (i = 0; i < SPU_OP_NUM; i++) {
  4076. atomic_set(&iproc_priv.op_counts[i], 0);
  4077. atomic_set(&iproc_priv.setkey_cnt[i], 0);
  4078. }
  4079. for (i = 0; i < CIPHER_ALG_LAST; i++)
  4080. for (j = 0; j < CIPHER_MODE_LAST; j++)
  4081. atomic_set(&iproc_priv.cipher_cnt[i][j], 0);
  4082. for (i = 0; i < HASH_ALG_LAST; i++) {
  4083. atomic_set(&iproc_priv.hash_cnt[i], 0);
  4084. atomic_set(&iproc_priv.hmac_cnt[i], 0);
  4085. }
  4086. for (i = 0; i < AEAD_TYPE_LAST; i++)
  4087. atomic_set(&iproc_priv.aead_cnt[i], 0);
  4088. atomic_set(&iproc_priv.mb_no_spc, 0);
  4089. atomic_set(&iproc_priv.mb_send_fail, 0);
  4090. atomic_set(&iproc_priv.bad_icv, 0);
  4091. }
  4092. static int spu_register_ablkcipher(struct iproc_alg_s *driver_alg)
  4093. {
  4094. struct spu_hw *spu = &iproc_priv.spu;
  4095. struct crypto_alg *crypto = &driver_alg->alg.crypto;
  4096. int err;
  4097. /* SPU2 does not support RC4 */
  4098. if ((driver_alg->cipher_info.alg == CIPHER_ALG_RC4) &&
  4099. (spu->spu_type == SPU_TYPE_SPU2))
  4100. return 0;
  4101. crypto->cra_module = THIS_MODULE;
  4102. crypto->cra_priority = cipher_pri;
  4103. crypto->cra_alignmask = 0;
  4104. crypto->cra_ctxsize = sizeof(struct iproc_ctx_s);
  4105. INIT_LIST_HEAD(&crypto->cra_list);
  4106. crypto->cra_init = ablkcipher_cra_init;
  4107. crypto->cra_exit = generic_cra_exit;
  4108. crypto->cra_type = &crypto_ablkcipher_type;
  4109. crypto->cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC |
  4110. CRYPTO_ALG_KERN_DRIVER_ONLY;
  4111. crypto->cra_ablkcipher.setkey = ablkcipher_setkey;
  4112. crypto->cra_ablkcipher.encrypt = ablkcipher_encrypt;
  4113. crypto->cra_ablkcipher.decrypt = ablkcipher_decrypt;
  4114. err = crypto_register_alg(crypto);
  4115. /* Mark alg as having been registered, if successful */
  4116. if (err == 0)
  4117. driver_alg->registered = true;
  4118. pr_debug(" registered ablkcipher %s\n", crypto->cra_driver_name);
  4119. return err;
  4120. }
  4121. static int spu_register_ahash(struct iproc_alg_s *driver_alg)
  4122. {
  4123. struct spu_hw *spu = &iproc_priv.spu;
  4124. struct ahash_alg *hash = &driver_alg->alg.hash;
  4125. int err;
  4126. /* AES-XCBC is the only AES hash type currently supported on SPU-M */
  4127. if ((driver_alg->auth_info.alg == HASH_ALG_AES) &&
  4128. (driver_alg->auth_info.mode != HASH_MODE_XCBC) &&
  4129. (spu->spu_type == SPU_TYPE_SPUM))
  4130. return 0;
  4131. /* SHA3 algorithm variants are not registered for SPU-M or SPU2. */
  4132. if ((driver_alg->auth_info.alg >= HASH_ALG_SHA3_224) &&
  4133. (spu->spu_subtype != SPU_SUBTYPE_SPU2_V2))
  4134. return 0;
  4135. hash->halg.base.cra_module = THIS_MODULE;
  4136. hash->halg.base.cra_priority = hash_pri;
  4137. hash->halg.base.cra_alignmask = 0;
  4138. hash->halg.base.cra_ctxsize = sizeof(struct iproc_ctx_s);
  4139. hash->halg.base.cra_init = ahash_cra_init;
  4140. hash->halg.base.cra_exit = generic_cra_exit;
  4141. hash->halg.base.cra_flags = CRYPTO_ALG_ASYNC;
  4142. hash->halg.statesize = sizeof(struct spu_hash_export_s);
  4143. if (driver_alg->auth_info.mode != HASH_MODE_HMAC) {
  4144. hash->init = ahash_init;
  4145. hash->update = ahash_update;
  4146. hash->final = ahash_final;
  4147. hash->finup = ahash_finup;
  4148. hash->digest = ahash_digest;
  4149. if ((driver_alg->auth_info.alg == HASH_ALG_AES) &&
  4150. ((driver_alg->auth_info.mode == HASH_MODE_XCBC) ||
  4151. (driver_alg->auth_info.mode == HASH_MODE_CMAC))) {
  4152. hash->setkey = ahash_setkey;
  4153. }
  4154. } else {
  4155. hash->setkey = ahash_hmac_setkey;
  4156. hash->init = ahash_hmac_init;
  4157. hash->update = ahash_hmac_update;
  4158. hash->final = ahash_hmac_final;
  4159. hash->finup = ahash_hmac_finup;
  4160. hash->digest = ahash_hmac_digest;
  4161. }
  4162. hash->export = ahash_export;
  4163. hash->import = ahash_import;
  4164. err = crypto_register_ahash(hash);
  4165. /* Mark alg as having been registered, if successful */
  4166. if (err == 0)
  4167. driver_alg->registered = true;
  4168. pr_debug(" registered ahash %s\n",
  4169. hash->halg.base.cra_driver_name);
  4170. return err;
  4171. }
  4172. static int spu_register_aead(struct iproc_alg_s *driver_alg)
  4173. {
  4174. struct aead_alg *aead = &driver_alg->alg.aead;
  4175. int err;
  4176. aead->base.cra_module = THIS_MODULE;
  4177. aead->base.cra_priority = aead_pri;
  4178. aead->base.cra_alignmask = 0;
  4179. aead->base.cra_ctxsize = sizeof(struct iproc_ctx_s);
  4180. INIT_LIST_HEAD(&aead->base.cra_list);
  4181. aead->base.cra_flags |= CRYPTO_ALG_ASYNC;
  4182. /* setkey set in alg initialization */
  4183. aead->setauthsize = aead_setauthsize;
  4184. aead->encrypt = aead_encrypt;
  4185. aead->decrypt = aead_decrypt;
  4186. aead->init = aead_cra_init;
  4187. aead->exit = aead_cra_exit;
  4188. err = crypto_register_aead(aead);
  4189. /* Mark alg as having been registered, if successful */
  4190. if (err == 0)
  4191. driver_alg->registered = true;
  4192. pr_debug(" registered aead %s\n", aead->base.cra_driver_name);
  4193. return err;
  4194. }
  4195. /* register crypto algorithms the device supports */
  4196. static int spu_algs_register(struct device *dev)
  4197. {
  4198. int i, j;
  4199. int err;
  4200. for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
  4201. switch (driver_algs[i].type) {
  4202. case CRYPTO_ALG_TYPE_ABLKCIPHER:
  4203. err = spu_register_ablkcipher(&driver_algs[i]);
  4204. break;
  4205. case CRYPTO_ALG_TYPE_AHASH:
  4206. err = spu_register_ahash(&driver_algs[i]);
  4207. break;
  4208. case CRYPTO_ALG_TYPE_AEAD:
  4209. err = spu_register_aead(&driver_algs[i]);
  4210. break;
  4211. default:
  4212. dev_err(dev,
  4213. "iproc-crypto: unknown alg type: %d",
  4214. driver_algs[i].type);
  4215. err = -EINVAL;
  4216. }
  4217. if (err) {
  4218. dev_err(dev, "alg registration failed with error %d\n",
  4219. err);
  4220. goto err_algs;
  4221. }
  4222. }
  4223. return 0;
  4224. err_algs:
  4225. for (j = 0; j < i; j++) {
  4226. /* Skip any algorithm not registered */
  4227. if (!driver_algs[j].registered)
  4228. continue;
  4229. switch (driver_algs[j].type) {
  4230. case CRYPTO_ALG_TYPE_ABLKCIPHER:
  4231. crypto_unregister_alg(&driver_algs[j].alg.crypto);
  4232. driver_algs[j].registered = false;
  4233. break;
  4234. case CRYPTO_ALG_TYPE_AHASH:
  4235. crypto_unregister_ahash(&driver_algs[j].alg.hash);
  4236. driver_algs[j].registered = false;
  4237. break;
  4238. case CRYPTO_ALG_TYPE_AEAD:
  4239. crypto_unregister_aead(&driver_algs[j].alg.aead);
  4240. driver_algs[j].registered = false;
  4241. break;
  4242. }
  4243. }
  4244. return err;
  4245. }
  4246. /* ==================== Kernel Platform API ==================== */
  4247. static struct spu_type_subtype spum_ns2_types = {
  4248. SPU_TYPE_SPUM, SPU_SUBTYPE_SPUM_NS2
  4249. };
  4250. static struct spu_type_subtype spum_nsp_types = {
  4251. SPU_TYPE_SPUM, SPU_SUBTYPE_SPUM_NSP
  4252. };
  4253. static struct spu_type_subtype spu2_types = {
  4254. SPU_TYPE_SPU2, SPU_SUBTYPE_SPU2_V1
  4255. };
  4256. static struct spu_type_subtype spu2_v2_types = {
  4257. SPU_TYPE_SPU2, SPU_SUBTYPE_SPU2_V2
  4258. };
  4259. static const struct of_device_id bcm_spu_dt_ids[] = {
  4260. {
  4261. .compatible = "brcm,spum-crypto",
  4262. .data = &spum_ns2_types,
  4263. },
  4264. {
  4265. .compatible = "brcm,spum-nsp-crypto",
  4266. .data = &spum_nsp_types,
  4267. },
  4268. {
  4269. .compatible = "brcm,spu2-crypto",
  4270. .data = &spu2_types,
  4271. },
  4272. {
  4273. .compatible = "brcm,spu2-v2-crypto",
  4274. .data = &spu2_v2_types,
  4275. },
  4276. { /* sentinel */ }
  4277. };
  4278. MODULE_DEVICE_TABLE(of, bcm_spu_dt_ids);
  4279. static int spu_dt_read(struct platform_device *pdev)
  4280. {
  4281. struct device *dev = &pdev->dev;
  4282. struct spu_hw *spu = &iproc_priv.spu;
  4283. struct resource *spu_ctrl_regs;
  4284. const struct spu_type_subtype *matched_spu_type;
  4285. struct device_node *dn = pdev->dev.of_node;
  4286. int err, i;
  4287. /* Count number of mailbox channels */
  4288. spu->num_chan = of_count_phandle_with_args(dn, "mboxes", "#mbox-cells");
  4289. matched_spu_type = of_device_get_match_data(dev);
  4290. if (!matched_spu_type) {
  4291. dev_err(&pdev->dev, "Failed to match device\n");
  4292. return -ENODEV;
  4293. }
  4294. spu->spu_type = matched_spu_type->type;
  4295. spu->spu_subtype = matched_spu_type->subtype;
  4296. i = 0;
  4297. for (i = 0; (i < MAX_SPUS) && ((spu_ctrl_regs =
  4298. platform_get_resource(pdev, IORESOURCE_MEM, i)) != NULL); i++) {
  4299. spu->reg_vbase[i] = devm_ioremap_resource(dev, spu_ctrl_regs);
  4300. if (IS_ERR(spu->reg_vbase[i])) {
  4301. err = PTR_ERR(spu->reg_vbase[i]);
  4302. dev_err(&pdev->dev, "Failed to map registers: %d\n",
  4303. err);
  4304. spu->reg_vbase[i] = NULL;
  4305. return err;
  4306. }
  4307. }
  4308. spu->num_spu = i;
  4309. dev_dbg(dev, "Device has %d SPUs", spu->num_spu);
  4310. return 0;
  4311. }
  4312. int bcm_spu_probe(struct platform_device *pdev)
  4313. {
  4314. struct device *dev = &pdev->dev;
  4315. struct spu_hw *spu = &iproc_priv.spu;
  4316. int err = 0;
  4317. iproc_priv.pdev = pdev;
  4318. platform_set_drvdata(iproc_priv.pdev,
  4319. &iproc_priv);
  4320. err = spu_dt_read(pdev);
  4321. if (err < 0)
  4322. goto failure;
  4323. err = spu_mb_init(&pdev->dev);
  4324. if (err < 0)
  4325. goto failure;
  4326. if (spu->spu_type == SPU_TYPE_SPUM)
  4327. iproc_priv.bcm_hdr_len = 8;
  4328. else if (spu->spu_type == SPU_TYPE_SPU2)
  4329. iproc_priv.bcm_hdr_len = 0;
  4330. spu_functions_register(&pdev->dev, spu->spu_type, spu->spu_subtype);
  4331. spu_counters_init();
  4332. spu_setup_debugfs();
  4333. err = spu_algs_register(dev);
  4334. if (err < 0)
  4335. goto fail_reg;
  4336. return 0;
  4337. fail_reg:
  4338. spu_free_debugfs();
  4339. failure:
  4340. spu_mb_release(pdev);
  4341. dev_err(dev, "%s failed with error %d.\n", __func__, err);
  4342. return err;
  4343. }
  4344. int bcm_spu_remove(struct platform_device *pdev)
  4345. {
  4346. int i;
  4347. struct device *dev = &pdev->dev;
  4348. char *cdn;
  4349. for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
  4350. /*
  4351. * Not all algorithms were registered, depending on whether
  4352. * hardware is SPU or SPU2. So here we make sure to skip
  4353. * those algorithms that were not previously registered.
  4354. */
  4355. if (!driver_algs[i].registered)
  4356. continue;
  4357. switch (driver_algs[i].type) {
  4358. case CRYPTO_ALG_TYPE_ABLKCIPHER:
  4359. crypto_unregister_alg(&driver_algs[i].alg.crypto);
  4360. dev_dbg(dev, " unregistered cipher %s\n",
  4361. driver_algs[i].alg.crypto.cra_driver_name);
  4362. driver_algs[i].registered = false;
  4363. break;
  4364. case CRYPTO_ALG_TYPE_AHASH:
  4365. crypto_unregister_ahash(&driver_algs[i].alg.hash);
  4366. cdn = driver_algs[i].alg.hash.halg.base.cra_driver_name;
  4367. dev_dbg(dev, " unregistered hash %s\n", cdn);
  4368. driver_algs[i].registered = false;
  4369. break;
  4370. case CRYPTO_ALG_TYPE_AEAD:
  4371. crypto_unregister_aead(&driver_algs[i].alg.aead);
  4372. dev_dbg(dev, " unregistered aead %s\n",
  4373. driver_algs[i].alg.aead.base.cra_driver_name);
  4374. driver_algs[i].registered = false;
  4375. break;
  4376. }
  4377. }
  4378. spu_free_debugfs();
  4379. spu_mb_release(pdev);
  4380. return 0;
  4381. }
  4382. /* ===== Kernel Module API ===== */
  4383. static struct platform_driver bcm_spu_pdriver = {
  4384. .driver = {
  4385. .name = "brcm-spu-crypto",
  4386. .of_match_table = of_match_ptr(bcm_spu_dt_ids),
  4387. },
  4388. .probe = bcm_spu_probe,
  4389. .remove = bcm_spu_remove,
  4390. };
  4391. module_platform_driver(bcm_spu_pdriver);
  4392. MODULE_AUTHOR("Rob Rice <rob.rice@broadcom.com>");
  4393. MODULE_DESCRIPTION("Broadcom symmetric crypto offload driver");
  4394. MODULE_LICENSE("GPL v2");