cipher.c 129 KB

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