chcr_algo.c 124 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401
  1. /*
  2. * This file is part of the Chelsio T6 Crypto driver for Linux.
  3. *
  4. * Copyright (c) 2003-2016 Chelsio Communications, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. *
  34. * Written and Maintained by:
  35. * Manoj Malviya (manojmalviya@chelsio.com)
  36. * Atul Gupta (atul.gupta@chelsio.com)
  37. * Jitendra Lulla (jlulla@chelsio.com)
  38. * Yeshaswi M R Gowda (yeshaswi@chelsio.com)
  39. * Harsh Jain (harsh@chelsio.com)
  40. */
  41. #define pr_fmt(fmt) "chcr:" fmt
  42. #include <linux/kernel.h>
  43. #include <linux/module.h>
  44. #include <linux/crypto.h>
  45. #include <linux/skbuff.h>
  46. #include <linux/rtnetlink.h>
  47. #include <linux/highmem.h>
  48. #include <linux/scatterlist.h>
  49. #include <crypto/aes.h>
  50. #include <crypto/algapi.h>
  51. #include <crypto/gcm.h>
  52. #include <crypto/sha1.h>
  53. #include <crypto/sha2.h>
  54. #include <crypto/authenc.h>
  55. #include <crypto/ctr.h>
  56. #include <crypto/gf128mul.h>
  57. #include <crypto/internal/aead.h>
  58. #include <crypto/null.h>
  59. #include <crypto/internal/skcipher.h>
  60. #include <crypto/aead.h>
  61. #include <crypto/scatterwalk.h>
  62. #include <crypto/internal/hash.h>
  63. #include "t4fw_api.h"
  64. #include "t4_msg.h"
  65. #include "chcr_core.h"
  66. #include "chcr_algo.h"
  67. #include "chcr_crypto.h"
  68. #define IV AES_BLOCK_SIZE
  69. static unsigned int sgl_ent_len[] = {
  70. 0, 0, 16, 24, 40, 48, 64, 72, 88,
  71. 96, 112, 120, 136, 144, 160, 168, 184,
  72. 192, 208, 216, 232, 240, 256, 264, 280,
  73. 288, 304, 312, 328, 336, 352, 360, 376
  74. };
  75. static unsigned int dsgl_ent_len[] = {
  76. 0, 32, 32, 48, 48, 64, 64, 80, 80,
  77. 112, 112, 128, 128, 144, 144, 160, 160,
  78. 192, 192, 208, 208, 224, 224, 240, 240,
  79. 272, 272, 288, 288, 304, 304, 320, 320
  80. };
  81. static u32 round_constant[11] = {
  82. 0x01000000, 0x02000000, 0x04000000, 0x08000000,
  83. 0x10000000, 0x20000000, 0x40000000, 0x80000000,
  84. 0x1B000000, 0x36000000, 0x6C000000
  85. };
  86. static int chcr_handle_cipher_resp(struct skcipher_request *req,
  87. unsigned char *input, int err);
  88. static inline struct chcr_aead_ctx *AEAD_CTX(struct chcr_context *ctx)
  89. {
  90. return &ctx->crypto_ctx->aeadctx;
  91. }
  92. static inline struct ablk_ctx *ABLK_CTX(struct chcr_context *ctx)
  93. {
  94. return &ctx->crypto_ctx->ablkctx;
  95. }
  96. static inline struct hmac_ctx *HMAC_CTX(struct chcr_context *ctx)
  97. {
  98. return &ctx->crypto_ctx->hmacctx;
  99. }
  100. static inline struct chcr_gcm_ctx *GCM_CTX(struct chcr_aead_ctx *gctx)
  101. {
  102. return gctx->ctx->gcm;
  103. }
  104. static inline struct chcr_authenc_ctx *AUTHENC_CTX(struct chcr_aead_ctx *gctx)
  105. {
  106. return gctx->ctx->authenc;
  107. }
  108. static inline struct uld_ctx *ULD_CTX(struct chcr_context *ctx)
  109. {
  110. return container_of(ctx->dev, struct uld_ctx, dev);
  111. }
  112. static inline void chcr_init_hctx_per_wr(struct chcr_ahash_req_ctx *reqctx)
  113. {
  114. memset(&reqctx->hctx_wr, 0, sizeof(struct chcr_hctx_per_wr));
  115. }
  116. static int sg_nents_xlen(struct scatterlist *sg, unsigned int reqlen,
  117. unsigned int entlen,
  118. unsigned int skip)
  119. {
  120. int nents = 0;
  121. unsigned int less;
  122. unsigned int skip_len = 0;
  123. while (sg && skip) {
  124. if (sg_dma_len(sg) <= skip) {
  125. skip -= sg_dma_len(sg);
  126. skip_len = 0;
  127. sg = sg_next(sg);
  128. } else {
  129. skip_len = skip;
  130. skip = 0;
  131. }
  132. }
  133. while (sg && reqlen) {
  134. less = min(reqlen, sg_dma_len(sg) - skip_len);
  135. nents += DIV_ROUND_UP(less, entlen);
  136. reqlen -= less;
  137. skip_len = 0;
  138. sg = sg_next(sg);
  139. }
  140. return nents;
  141. }
  142. static inline int get_aead_subtype(struct crypto_aead *aead)
  143. {
  144. struct aead_alg *alg = crypto_aead_alg(aead);
  145. struct chcr_alg_template *chcr_crypto_alg =
  146. container_of(alg, struct chcr_alg_template, alg.aead);
  147. return chcr_crypto_alg->type & CRYPTO_ALG_SUB_TYPE_MASK;
  148. }
  149. void chcr_verify_tag(struct aead_request *req, u8 *input, int *err)
  150. {
  151. u8 temp[SHA512_DIGEST_SIZE];
  152. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  153. int authsize = crypto_aead_authsize(tfm);
  154. struct cpl_fw6_pld *fw6_pld;
  155. int cmp = 0;
  156. fw6_pld = (struct cpl_fw6_pld *)input;
  157. if ((get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106) ||
  158. (get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_GCM)) {
  159. cmp = crypto_memneq(&fw6_pld->data[2], (fw6_pld + 1), authsize);
  160. } else {
  161. sg_pcopy_to_buffer(req->src, sg_nents(req->src), temp,
  162. authsize, req->assoclen +
  163. req->cryptlen - authsize);
  164. cmp = crypto_memneq(temp, (fw6_pld + 1), authsize);
  165. }
  166. if (cmp)
  167. *err = -EBADMSG;
  168. else
  169. *err = 0;
  170. }
  171. static int chcr_inc_wrcount(struct chcr_dev *dev)
  172. {
  173. if (dev->state == CHCR_DETACH)
  174. return 1;
  175. atomic_inc(&dev->inflight);
  176. return 0;
  177. }
  178. static inline void chcr_dec_wrcount(struct chcr_dev *dev)
  179. {
  180. atomic_dec(&dev->inflight);
  181. }
  182. static inline int chcr_handle_aead_resp(struct aead_request *req,
  183. unsigned char *input,
  184. int err)
  185. {
  186. struct chcr_aead_reqctx *reqctx = aead_request_ctx_dma(req);
  187. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  188. struct chcr_dev *dev = a_ctx(tfm)->dev;
  189. chcr_aead_common_exit(req);
  190. if (reqctx->verify == VERIFY_SW) {
  191. chcr_verify_tag(req, input, &err);
  192. reqctx->verify = VERIFY_HW;
  193. }
  194. chcr_dec_wrcount(dev);
  195. aead_request_complete(req, err);
  196. return err;
  197. }
  198. static void get_aes_decrypt_key(unsigned char *dec_key,
  199. const unsigned char *key,
  200. unsigned int keylength)
  201. {
  202. u32 temp;
  203. u32 w_ring[MAX_NK];
  204. int i, j, k;
  205. u8 nr, nk;
  206. switch (keylength) {
  207. case AES_KEYLENGTH_128BIT:
  208. nk = KEYLENGTH_4BYTES;
  209. nr = NUMBER_OF_ROUNDS_10;
  210. break;
  211. case AES_KEYLENGTH_192BIT:
  212. nk = KEYLENGTH_6BYTES;
  213. nr = NUMBER_OF_ROUNDS_12;
  214. break;
  215. case AES_KEYLENGTH_256BIT:
  216. nk = KEYLENGTH_8BYTES;
  217. nr = NUMBER_OF_ROUNDS_14;
  218. break;
  219. default:
  220. return;
  221. }
  222. for (i = 0; i < nk; i++)
  223. w_ring[i] = get_unaligned_be32(&key[i * 4]);
  224. i = 0;
  225. temp = w_ring[nk - 1];
  226. while (i + nk < (nr + 1) * 4) {
  227. if (!(i % nk)) {
  228. /* RotWord(temp) */
  229. temp = (temp << 8) | (temp >> 24);
  230. temp = aes_ks_subword(temp);
  231. temp ^= round_constant[i / nk];
  232. } else if (nk == 8 && (i % 4 == 0)) {
  233. temp = aes_ks_subword(temp);
  234. }
  235. w_ring[i % nk] ^= temp;
  236. temp = w_ring[i % nk];
  237. i++;
  238. }
  239. i--;
  240. for (k = 0, j = i % nk; k < nk; k++) {
  241. put_unaligned_be32(w_ring[j], &dec_key[k * 4]);
  242. j--;
  243. if (j < 0)
  244. j += nk;
  245. }
  246. }
  247. static int chcr_prepare_hmac_key(const u8 *raw_key, unsigned int raw_key_len,
  248. int digestsize, void *istate, void *ostate)
  249. {
  250. __be32 *istate32 = istate, *ostate32 = ostate;
  251. __be64 *istate64 = istate, *ostate64 = ostate;
  252. union {
  253. struct hmac_sha1_key sha1;
  254. struct hmac_sha224_key sha224;
  255. struct hmac_sha256_key sha256;
  256. struct hmac_sha384_key sha384;
  257. struct hmac_sha512_key sha512;
  258. } k;
  259. switch (digestsize) {
  260. case SHA1_DIGEST_SIZE:
  261. hmac_sha1_preparekey(&k.sha1, raw_key, raw_key_len);
  262. for (int i = 0; i < ARRAY_SIZE(k.sha1.istate.h); i++) {
  263. istate32[i] = cpu_to_be32(k.sha1.istate.h[i]);
  264. ostate32[i] = cpu_to_be32(k.sha1.ostate.h[i]);
  265. }
  266. break;
  267. case SHA224_DIGEST_SIZE:
  268. hmac_sha224_preparekey(&k.sha224, raw_key, raw_key_len);
  269. for (int i = 0; i < ARRAY_SIZE(k.sha224.key.istate.h); i++) {
  270. istate32[i] = cpu_to_be32(k.sha224.key.istate.h[i]);
  271. ostate32[i] = cpu_to_be32(k.sha224.key.ostate.h[i]);
  272. }
  273. break;
  274. case SHA256_DIGEST_SIZE:
  275. hmac_sha256_preparekey(&k.sha256, raw_key, raw_key_len);
  276. for (int i = 0; i < ARRAY_SIZE(k.sha256.key.istate.h); i++) {
  277. istate32[i] = cpu_to_be32(k.sha256.key.istate.h[i]);
  278. ostate32[i] = cpu_to_be32(k.sha256.key.ostate.h[i]);
  279. }
  280. break;
  281. case SHA384_DIGEST_SIZE:
  282. hmac_sha384_preparekey(&k.sha384, raw_key, raw_key_len);
  283. for (int i = 0; i < ARRAY_SIZE(k.sha384.key.istate.h); i++) {
  284. istate64[i] = cpu_to_be64(k.sha384.key.istate.h[i]);
  285. ostate64[i] = cpu_to_be64(k.sha384.key.ostate.h[i]);
  286. }
  287. break;
  288. case SHA512_DIGEST_SIZE:
  289. hmac_sha512_preparekey(&k.sha512, raw_key, raw_key_len);
  290. for (int i = 0; i < ARRAY_SIZE(k.sha512.key.istate.h); i++) {
  291. istate64[i] = cpu_to_be64(k.sha512.key.istate.h[i]);
  292. ostate64[i] = cpu_to_be64(k.sha512.key.ostate.h[i]);
  293. }
  294. break;
  295. default:
  296. return -EINVAL;
  297. }
  298. memzero_explicit(&k, sizeof(k));
  299. return 0;
  300. }
  301. static inline int is_hmac(struct crypto_tfm *tfm)
  302. {
  303. struct crypto_alg *alg = tfm->__crt_alg;
  304. struct chcr_alg_template *chcr_crypto_alg =
  305. container_of(__crypto_ahash_alg(alg), struct chcr_alg_template,
  306. alg.hash);
  307. if (chcr_crypto_alg->type == CRYPTO_ALG_TYPE_HMAC)
  308. return 1;
  309. return 0;
  310. }
  311. static inline void dsgl_walk_init(struct dsgl_walk *walk,
  312. struct cpl_rx_phys_dsgl *dsgl)
  313. {
  314. walk->dsgl = dsgl;
  315. walk->nents = 0;
  316. walk->to = (struct phys_sge_pairs *)(dsgl + 1);
  317. }
  318. static inline void dsgl_walk_end(struct dsgl_walk *walk, unsigned short qid,
  319. int pci_chan_id)
  320. {
  321. struct cpl_rx_phys_dsgl *phys_cpl;
  322. phys_cpl = walk->dsgl;
  323. phys_cpl->op_to_tid = htonl(CPL_RX_PHYS_DSGL_OPCODE_V(CPL_RX_PHYS_DSGL)
  324. | CPL_RX_PHYS_DSGL_ISRDMA_V(0));
  325. phys_cpl->pcirlxorder_to_noofsgentr =
  326. htonl(CPL_RX_PHYS_DSGL_PCIRLXORDER_V(0) |
  327. CPL_RX_PHYS_DSGL_PCINOSNOOP_V(0) |
  328. CPL_RX_PHYS_DSGL_PCITPHNTENB_V(0) |
  329. CPL_RX_PHYS_DSGL_PCITPHNT_V(0) |
  330. CPL_RX_PHYS_DSGL_DCAID_V(0) |
  331. CPL_RX_PHYS_DSGL_NOOFSGENTR_V(walk->nents));
  332. phys_cpl->rss_hdr_int.opcode = CPL_RX_PHYS_ADDR;
  333. phys_cpl->rss_hdr_int.qid = htons(qid);
  334. phys_cpl->rss_hdr_int.hash_val = 0;
  335. phys_cpl->rss_hdr_int.channel = pci_chan_id;
  336. }
  337. static inline void dsgl_walk_add_page(struct dsgl_walk *walk,
  338. size_t size,
  339. dma_addr_t addr)
  340. {
  341. int j;
  342. if (!size)
  343. return;
  344. j = walk->nents;
  345. walk->to->len[j % 8] = htons(size);
  346. walk->to->addr[j % 8] = cpu_to_be64(addr);
  347. j++;
  348. if ((j % 8) == 0)
  349. walk->to++;
  350. walk->nents = j;
  351. }
  352. static void dsgl_walk_add_sg(struct dsgl_walk *walk,
  353. struct scatterlist *sg,
  354. unsigned int slen,
  355. unsigned int skip)
  356. {
  357. int skip_len = 0;
  358. unsigned int left_size = slen, len = 0;
  359. unsigned int j = walk->nents;
  360. int offset, ent_len;
  361. if (!slen)
  362. return;
  363. while (sg && skip) {
  364. if (sg_dma_len(sg) <= skip) {
  365. skip -= sg_dma_len(sg);
  366. skip_len = 0;
  367. sg = sg_next(sg);
  368. } else {
  369. skip_len = skip;
  370. skip = 0;
  371. }
  372. }
  373. while (left_size && sg) {
  374. len = min_t(u32, left_size, sg_dma_len(sg) - skip_len);
  375. offset = 0;
  376. while (len) {
  377. ent_len = min_t(u32, len, CHCR_DST_SG_SIZE);
  378. walk->to->len[j % 8] = htons(ent_len);
  379. walk->to->addr[j % 8] = cpu_to_be64(sg_dma_address(sg) +
  380. offset + skip_len);
  381. offset += ent_len;
  382. len -= ent_len;
  383. j++;
  384. if ((j % 8) == 0)
  385. walk->to++;
  386. }
  387. walk->last_sg = sg;
  388. walk->last_sg_len = min_t(u32, left_size, sg_dma_len(sg) -
  389. skip_len) + skip_len;
  390. left_size -= min_t(u32, left_size, sg_dma_len(sg) - skip_len);
  391. skip_len = 0;
  392. sg = sg_next(sg);
  393. }
  394. walk->nents = j;
  395. }
  396. static inline void ulptx_walk_init(struct ulptx_walk *walk,
  397. struct ulptx_sgl *ulp)
  398. {
  399. walk->sgl = ulp;
  400. walk->nents = 0;
  401. walk->pair_idx = 0;
  402. walk->pair = ulp->sge;
  403. walk->last_sg = NULL;
  404. walk->last_sg_len = 0;
  405. }
  406. static inline void ulptx_walk_end(struct ulptx_walk *walk)
  407. {
  408. walk->sgl->cmd_nsge = htonl(ULPTX_CMD_V(ULP_TX_SC_DSGL) |
  409. ULPTX_NSGE_V(walk->nents));
  410. }
  411. static inline void ulptx_walk_add_page(struct ulptx_walk *walk,
  412. size_t size,
  413. dma_addr_t addr)
  414. {
  415. if (!size)
  416. return;
  417. if (walk->nents == 0) {
  418. walk->sgl->len0 = cpu_to_be32(size);
  419. walk->sgl->addr0 = cpu_to_be64(addr);
  420. } else {
  421. walk->pair->addr[walk->pair_idx] = cpu_to_be64(addr);
  422. walk->pair->len[walk->pair_idx] = cpu_to_be32(size);
  423. walk->pair_idx = !walk->pair_idx;
  424. if (!walk->pair_idx)
  425. walk->pair++;
  426. }
  427. walk->nents++;
  428. }
  429. static void ulptx_walk_add_sg(struct ulptx_walk *walk,
  430. struct scatterlist *sg,
  431. unsigned int len,
  432. unsigned int skip)
  433. {
  434. int small;
  435. int skip_len = 0;
  436. unsigned int sgmin;
  437. if (!len)
  438. return;
  439. while (sg && skip) {
  440. if (sg_dma_len(sg) <= skip) {
  441. skip -= sg_dma_len(sg);
  442. skip_len = 0;
  443. sg = sg_next(sg);
  444. } else {
  445. skip_len = skip;
  446. skip = 0;
  447. }
  448. }
  449. WARN(!sg, "SG should not be null here\n");
  450. if (sg && (walk->nents == 0)) {
  451. small = min_t(unsigned int, sg_dma_len(sg) - skip_len, len);
  452. sgmin = min_t(unsigned int, small, CHCR_SRC_SG_SIZE);
  453. walk->sgl->len0 = cpu_to_be32(sgmin);
  454. walk->sgl->addr0 = cpu_to_be64(sg_dma_address(sg) + skip_len);
  455. walk->nents++;
  456. len -= sgmin;
  457. walk->last_sg = sg;
  458. walk->last_sg_len = sgmin + skip_len;
  459. skip_len += sgmin;
  460. if (sg_dma_len(sg) == skip_len) {
  461. sg = sg_next(sg);
  462. skip_len = 0;
  463. }
  464. }
  465. while (sg && len) {
  466. small = min(sg_dma_len(sg) - skip_len, len);
  467. sgmin = min_t(unsigned int, small, CHCR_SRC_SG_SIZE);
  468. walk->pair->len[walk->pair_idx] = cpu_to_be32(sgmin);
  469. walk->pair->addr[walk->pair_idx] =
  470. cpu_to_be64(sg_dma_address(sg) + skip_len);
  471. walk->pair_idx = !walk->pair_idx;
  472. walk->nents++;
  473. if (!walk->pair_idx)
  474. walk->pair++;
  475. len -= sgmin;
  476. skip_len += sgmin;
  477. walk->last_sg = sg;
  478. walk->last_sg_len = skip_len;
  479. if (sg_dma_len(sg) == skip_len) {
  480. sg = sg_next(sg);
  481. skip_len = 0;
  482. }
  483. }
  484. }
  485. static inline int get_cryptoalg_subtype(struct crypto_skcipher *tfm)
  486. {
  487. struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
  488. struct chcr_alg_template *chcr_crypto_alg =
  489. container_of(alg, struct chcr_alg_template, alg.skcipher);
  490. return chcr_crypto_alg->type & CRYPTO_ALG_SUB_TYPE_MASK;
  491. }
  492. static int cxgb4_is_crypto_q_full(struct net_device *dev, unsigned int idx)
  493. {
  494. struct adapter *adap = netdev2adap(dev);
  495. struct sge_uld_txq_info *txq_info =
  496. adap->sge.uld_txq_info[CXGB4_TX_CRYPTO];
  497. struct sge_uld_txq *txq;
  498. int ret = 0;
  499. local_bh_disable();
  500. txq = &txq_info->uldtxq[idx];
  501. spin_lock(&txq->sendq.lock);
  502. if (txq->full)
  503. ret = -1;
  504. spin_unlock(&txq->sendq.lock);
  505. local_bh_enable();
  506. return ret;
  507. }
  508. static int generate_copy_rrkey(struct ablk_ctx *ablkctx,
  509. struct _key_ctx *key_ctx)
  510. {
  511. if (ablkctx->ciph_mode == CHCR_SCMD_CIPHER_MODE_AES_CBC) {
  512. memcpy(key_ctx->key, ablkctx->rrkey, ablkctx->enckey_len);
  513. } else {
  514. memcpy(key_ctx->key,
  515. ablkctx->key + (ablkctx->enckey_len >> 1),
  516. ablkctx->enckey_len >> 1);
  517. memcpy(key_ctx->key + (ablkctx->enckey_len >> 1),
  518. ablkctx->rrkey, ablkctx->enckey_len >> 1);
  519. }
  520. return 0;
  521. }
  522. static int chcr_hash_ent_in_wr(struct scatterlist *src,
  523. unsigned int minsg,
  524. unsigned int space,
  525. unsigned int srcskip)
  526. {
  527. int srclen = 0;
  528. int srcsg = minsg;
  529. int soffset = 0, sless;
  530. if (sg_dma_len(src) == srcskip) {
  531. src = sg_next(src);
  532. srcskip = 0;
  533. }
  534. while (src && space > (sgl_ent_len[srcsg + 1])) {
  535. sless = min_t(unsigned int, sg_dma_len(src) - soffset - srcskip,
  536. CHCR_SRC_SG_SIZE);
  537. srclen += sless;
  538. soffset += sless;
  539. srcsg++;
  540. if (sg_dma_len(src) == (soffset + srcskip)) {
  541. src = sg_next(src);
  542. soffset = 0;
  543. srcskip = 0;
  544. }
  545. }
  546. return srclen;
  547. }
  548. static int chcr_sg_ent_in_wr(struct scatterlist *src,
  549. struct scatterlist *dst,
  550. unsigned int minsg,
  551. unsigned int space,
  552. unsigned int srcskip,
  553. unsigned int dstskip)
  554. {
  555. int srclen = 0, dstlen = 0;
  556. int srcsg = minsg, dstsg = minsg;
  557. int offset = 0, soffset = 0, less, sless = 0;
  558. if (sg_dma_len(src) == srcskip) {
  559. src = sg_next(src);
  560. srcskip = 0;
  561. }
  562. if (sg_dma_len(dst) == dstskip) {
  563. dst = sg_next(dst);
  564. dstskip = 0;
  565. }
  566. while (src && dst &&
  567. space > (sgl_ent_len[srcsg + 1] + dsgl_ent_len[dstsg])) {
  568. sless = min_t(unsigned int, sg_dma_len(src) - srcskip - soffset,
  569. CHCR_SRC_SG_SIZE);
  570. srclen += sless;
  571. srcsg++;
  572. offset = 0;
  573. while (dst && ((dstsg + 1) <= MAX_DSGL_ENT) &&
  574. space > (sgl_ent_len[srcsg] + dsgl_ent_len[dstsg + 1])) {
  575. if (srclen <= dstlen)
  576. break;
  577. less = min_t(unsigned int, sg_dma_len(dst) - offset -
  578. dstskip, CHCR_DST_SG_SIZE);
  579. dstlen += less;
  580. offset += less;
  581. if ((offset + dstskip) == sg_dma_len(dst)) {
  582. dst = sg_next(dst);
  583. offset = 0;
  584. }
  585. dstsg++;
  586. dstskip = 0;
  587. }
  588. soffset += sless;
  589. if ((soffset + srcskip) == sg_dma_len(src)) {
  590. src = sg_next(src);
  591. srcskip = 0;
  592. soffset = 0;
  593. }
  594. }
  595. return min(srclen, dstlen);
  596. }
  597. static int chcr_cipher_fallback(struct crypto_skcipher *cipher,
  598. struct skcipher_request *req,
  599. u8 *iv,
  600. unsigned short op_type)
  601. {
  602. struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req);
  603. int err;
  604. skcipher_request_set_tfm(&reqctx->fallback_req, cipher);
  605. skcipher_request_set_callback(&reqctx->fallback_req, req->base.flags,
  606. req->base.complete, req->base.data);
  607. skcipher_request_set_crypt(&reqctx->fallback_req, req->src, req->dst,
  608. req->cryptlen, iv);
  609. err = op_type ? crypto_skcipher_decrypt(&reqctx->fallback_req) :
  610. crypto_skcipher_encrypt(&reqctx->fallback_req);
  611. return err;
  612. }
  613. static inline int get_qidxs(struct crypto_async_request *req,
  614. unsigned int *txqidx, unsigned int *rxqidx)
  615. {
  616. struct crypto_tfm *tfm = req->tfm;
  617. int ret = 0;
  618. switch (tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
  619. case CRYPTO_ALG_TYPE_AEAD:
  620. {
  621. struct aead_request *aead_req =
  622. container_of(req, struct aead_request, base);
  623. struct chcr_aead_reqctx *reqctx = aead_request_ctx_dma(aead_req);
  624. *txqidx = reqctx->txqidx;
  625. *rxqidx = reqctx->rxqidx;
  626. break;
  627. }
  628. case CRYPTO_ALG_TYPE_SKCIPHER:
  629. {
  630. struct skcipher_request *sk_req =
  631. container_of(req, struct skcipher_request, base);
  632. struct chcr_skcipher_req_ctx *reqctx =
  633. skcipher_request_ctx(sk_req);
  634. *txqidx = reqctx->txqidx;
  635. *rxqidx = reqctx->rxqidx;
  636. break;
  637. }
  638. case CRYPTO_ALG_TYPE_AHASH:
  639. {
  640. struct ahash_request *ahash_req =
  641. container_of(req, struct ahash_request, base);
  642. struct chcr_ahash_req_ctx *reqctx =
  643. ahash_request_ctx(ahash_req);
  644. *txqidx = reqctx->txqidx;
  645. *rxqidx = reqctx->rxqidx;
  646. break;
  647. }
  648. default:
  649. ret = -EINVAL;
  650. /* should never get here */
  651. BUG();
  652. break;
  653. }
  654. return ret;
  655. }
  656. static inline void create_wreq(struct chcr_context *ctx,
  657. struct chcr_wr *chcr_req,
  658. struct crypto_async_request *req,
  659. unsigned int imm,
  660. int hash_sz,
  661. unsigned int len16,
  662. unsigned int sc_len,
  663. unsigned int lcb)
  664. {
  665. struct uld_ctx *u_ctx = ULD_CTX(ctx);
  666. unsigned int tx_channel_id, rx_channel_id;
  667. unsigned int txqidx = 0, rxqidx = 0;
  668. unsigned int qid, fid, portno;
  669. get_qidxs(req, &txqidx, &rxqidx);
  670. qid = u_ctx->lldi.rxq_ids[rxqidx];
  671. fid = u_ctx->lldi.rxq_ids[0];
  672. portno = rxqidx / ctx->rxq_perchan;
  673. tx_channel_id = txqidx / ctx->txq_perchan;
  674. rx_channel_id = cxgb4_port_e2cchan(u_ctx->lldi.ports[portno]);
  675. chcr_req->wreq.op_to_cctx_size = FILL_WR_OP_CCTX_SIZE;
  676. chcr_req->wreq.pld_size_hash_size =
  677. htonl(FW_CRYPTO_LOOKASIDE_WR_HASH_SIZE_V(hash_sz));
  678. chcr_req->wreq.len16_pkd =
  679. htonl(FW_CRYPTO_LOOKASIDE_WR_LEN16_V(DIV_ROUND_UP(len16, 16)));
  680. chcr_req->wreq.cookie = cpu_to_be64((uintptr_t)req);
  681. chcr_req->wreq.rx_chid_to_rx_q_id = FILL_WR_RX_Q_ID(rx_channel_id, qid,
  682. !!lcb, txqidx);
  683. chcr_req->ulptx.cmd_dest = FILL_ULPTX_CMD_DEST(tx_channel_id, fid);
  684. chcr_req->ulptx.len = htonl((DIV_ROUND_UP(len16, 16) -
  685. ((sizeof(chcr_req->wreq)) >> 4)));
  686. chcr_req->sc_imm.cmd_more = FILL_CMD_MORE(!imm);
  687. chcr_req->sc_imm.len = cpu_to_be32(sizeof(struct cpl_tx_sec_pdu) +
  688. sizeof(chcr_req->key_ctx) + sc_len);
  689. }
  690. /**
  691. * create_cipher_wr - form the WR for cipher operations
  692. * @wrparam: Container for create_cipher_wr()'s parameters
  693. */
  694. static struct sk_buff *create_cipher_wr(struct cipher_wr_param *wrparam)
  695. {
  696. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(wrparam->req);
  697. struct chcr_context *ctx = c_ctx(tfm);
  698. struct uld_ctx *u_ctx = ULD_CTX(ctx);
  699. struct ablk_ctx *ablkctx = ABLK_CTX(ctx);
  700. struct sk_buff *skb = NULL;
  701. struct chcr_wr *chcr_req;
  702. struct cpl_rx_phys_dsgl *phys_cpl;
  703. struct ulptx_sgl *ulptx;
  704. struct chcr_skcipher_req_ctx *reqctx =
  705. skcipher_request_ctx(wrparam->req);
  706. unsigned int temp = 0, transhdr_len, dst_size;
  707. int error;
  708. int nents;
  709. unsigned int kctx_len;
  710. gfp_t flags = wrparam->req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
  711. GFP_KERNEL : GFP_ATOMIC;
  712. struct adapter *adap = padap(ctx->dev);
  713. unsigned int rx_channel_id = reqctx->rxqidx / ctx->rxq_perchan;
  714. rx_channel_id = cxgb4_port_e2cchan(u_ctx->lldi.ports[rx_channel_id]);
  715. nents = sg_nents_xlen(reqctx->dstsg, wrparam->bytes, CHCR_DST_SG_SIZE,
  716. reqctx->dst_ofst);
  717. dst_size = get_space_for_phys_dsgl(nents);
  718. kctx_len = roundup(ablkctx->enckey_len, 16);
  719. transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, dst_size);
  720. nents = sg_nents_xlen(reqctx->srcsg, wrparam->bytes,
  721. CHCR_SRC_SG_SIZE, reqctx->src_ofst);
  722. temp = reqctx->imm ? roundup(wrparam->bytes, 16) :
  723. (sgl_len(nents) * 8);
  724. transhdr_len += temp;
  725. transhdr_len = roundup(transhdr_len, 16);
  726. skb = alloc_skb(SGE_MAX_WR_LEN, flags);
  727. if (!skb) {
  728. error = -ENOMEM;
  729. goto err;
  730. }
  731. chcr_req = __skb_put_zero(skb, transhdr_len);
  732. chcr_req->sec_cpl.op_ivinsrtofst =
  733. FILL_SEC_CPL_OP_IVINSR(rx_channel_id, 2, 1);
  734. chcr_req->sec_cpl.pldlen = htonl(IV + wrparam->bytes);
  735. chcr_req->sec_cpl.aadstart_cipherstop_hi =
  736. FILL_SEC_CPL_CIPHERSTOP_HI(0, 0, IV + 1, 0);
  737. chcr_req->sec_cpl.cipherstop_lo_authinsert =
  738. FILL_SEC_CPL_AUTHINSERT(0, 0, 0, 0);
  739. chcr_req->sec_cpl.seqno_numivs = FILL_SEC_CPL_SCMD0_SEQNO(reqctx->op, 0,
  740. ablkctx->ciph_mode,
  741. 0, 0, IV >> 1);
  742. chcr_req->sec_cpl.ivgen_hdrlen = FILL_SEC_CPL_IVGEN_HDRLEN(0, 0, 0,
  743. 0, 1, dst_size);
  744. chcr_req->key_ctx.ctx_hdr = ablkctx->key_ctx_hdr;
  745. if ((reqctx->op == CHCR_DECRYPT_OP) &&
  746. (!(get_cryptoalg_subtype(tfm) ==
  747. CRYPTO_ALG_SUB_TYPE_CTR)) &&
  748. (!(get_cryptoalg_subtype(tfm) ==
  749. CRYPTO_ALG_SUB_TYPE_CTR_RFC3686))) {
  750. generate_copy_rrkey(ablkctx, &chcr_req->key_ctx);
  751. } else {
  752. if ((ablkctx->ciph_mode == CHCR_SCMD_CIPHER_MODE_AES_CBC) ||
  753. (ablkctx->ciph_mode == CHCR_SCMD_CIPHER_MODE_AES_CTR)) {
  754. memcpy(chcr_req->key_ctx.key, ablkctx->key,
  755. ablkctx->enckey_len);
  756. } else {
  757. memcpy(chcr_req->key_ctx.key, ablkctx->key +
  758. (ablkctx->enckey_len >> 1),
  759. ablkctx->enckey_len >> 1);
  760. memcpy(chcr_req->key_ctx.key +
  761. (ablkctx->enckey_len >> 1),
  762. ablkctx->key,
  763. ablkctx->enckey_len >> 1);
  764. }
  765. }
  766. phys_cpl = (struct cpl_rx_phys_dsgl *)((u8 *)(chcr_req + 1) + kctx_len);
  767. ulptx = (struct ulptx_sgl *)((u8 *)(phys_cpl + 1) + dst_size);
  768. chcr_add_cipher_src_ent(wrparam->req, ulptx, wrparam);
  769. chcr_add_cipher_dst_ent(wrparam->req, phys_cpl, wrparam, wrparam->qid);
  770. atomic_inc(&adap->chcr_stats.cipher_rqst);
  771. temp = sizeof(struct cpl_rx_phys_dsgl) + dst_size + kctx_len + IV
  772. + (reqctx->imm ? (wrparam->bytes) : 0);
  773. create_wreq(c_ctx(tfm), chcr_req, &(wrparam->req->base), reqctx->imm, 0,
  774. transhdr_len, temp,
  775. ablkctx->ciph_mode == CHCR_SCMD_CIPHER_MODE_AES_CBC);
  776. reqctx->skb = skb;
  777. if (reqctx->op && (ablkctx->ciph_mode ==
  778. CHCR_SCMD_CIPHER_MODE_AES_CBC))
  779. sg_pcopy_to_buffer(wrparam->req->src,
  780. sg_nents(wrparam->req->src), wrparam->req->iv, 16,
  781. reqctx->processed + wrparam->bytes - AES_BLOCK_SIZE);
  782. return skb;
  783. err:
  784. return ERR_PTR(error);
  785. }
  786. static inline int chcr_keyctx_ck_size(unsigned int keylen)
  787. {
  788. int ck_size = 0;
  789. if (keylen == AES_KEYSIZE_128)
  790. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
  791. else if (keylen == AES_KEYSIZE_192)
  792. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
  793. else if (keylen == AES_KEYSIZE_256)
  794. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
  795. else
  796. ck_size = 0;
  797. return ck_size;
  798. }
  799. static int chcr_cipher_fallback_setkey(struct crypto_skcipher *cipher,
  800. const u8 *key,
  801. unsigned int keylen)
  802. {
  803. struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(cipher));
  804. crypto_skcipher_clear_flags(ablkctx->sw_cipher,
  805. CRYPTO_TFM_REQ_MASK);
  806. crypto_skcipher_set_flags(ablkctx->sw_cipher,
  807. cipher->base.crt_flags & CRYPTO_TFM_REQ_MASK);
  808. return crypto_skcipher_setkey(ablkctx->sw_cipher, key, keylen);
  809. }
  810. static int chcr_aes_cbc_setkey(struct crypto_skcipher *cipher,
  811. const u8 *key,
  812. unsigned int keylen)
  813. {
  814. struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(cipher));
  815. unsigned int ck_size, context_size;
  816. u16 alignment = 0;
  817. int err;
  818. err = chcr_cipher_fallback_setkey(cipher, key, keylen);
  819. if (err)
  820. goto badkey_err;
  821. ck_size = chcr_keyctx_ck_size(keylen);
  822. alignment = ck_size == CHCR_KEYCTX_CIPHER_KEY_SIZE_192 ? 8 : 0;
  823. memcpy(ablkctx->key, key, keylen);
  824. ablkctx->enckey_len = keylen;
  825. get_aes_decrypt_key(ablkctx->rrkey, ablkctx->key, keylen << 3);
  826. context_size = (KEY_CONTEXT_HDR_SALT_AND_PAD +
  827. keylen + alignment) >> 4;
  828. ablkctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, CHCR_KEYCTX_NO_KEY,
  829. 0, 0, context_size);
  830. ablkctx->ciph_mode = CHCR_SCMD_CIPHER_MODE_AES_CBC;
  831. return 0;
  832. badkey_err:
  833. ablkctx->enckey_len = 0;
  834. return err;
  835. }
  836. static int chcr_aes_ctr_setkey(struct crypto_skcipher *cipher,
  837. const u8 *key,
  838. unsigned int keylen)
  839. {
  840. struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(cipher));
  841. unsigned int ck_size, context_size;
  842. u16 alignment = 0;
  843. int err;
  844. err = chcr_cipher_fallback_setkey(cipher, key, keylen);
  845. if (err)
  846. goto badkey_err;
  847. ck_size = chcr_keyctx_ck_size(keylen);
  848. alignment = (ck_size == CHCR_KEYCTX_CIPHER_KEY_SIZE_192) ? 8 : 0;
  849. memcpy(ablkctx->key, key, keylen);
  850. ablkctx->enckey_len = keylen;
  851. context_size = (KEY_CONTEXT_HDR_SALT_AND_PAD +
  852. keylen + alignment) >> 4;
  853. ablkctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, CHCR_KEYCTX_NO_KEY,
  854. 0, 0, context_size);
  855. ablkctx->ciph_mode = CHCR_SCMD_CIPHER_MODE_AES_CTR;
  856. return 0;
  857. badkey_err:
  858. ablkctx->enckey_len = 0;
  859. return err;
  860. }
  861. static int chcr_aes_rfc3686_setkey(struct crypto_skcipher *cipher,
  862. const u8 *key,
  863. unsigned int keylen)
  864. {
  865. struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(cipher));
  866. unsigned int ck_size, context_size;
  867. u16 alignment = 0;
  868. int err;
  869. if (keylen < CTR_RFC3686_NONCE_SIZE)
  870. return -EINVAL;
  871. memcpy(ablkctx->nonce, key + (keylen - CTR_RFC3686_NONCE_SIZE),
  872. CTR_RFC3686_NONCE_SIZE);
  873. keylen -= CTR_RFC3686_NONCE_SIZE;
  874. err = chcr_cipher_fallback_setkey(cipher, key, keylen);
  875. if (err)
  876. goto badkey_err;
  877. ck_size = chcr_keyctx_ck_size(keylen);
  878. alignment = (ck_size == CHCR_KEYCTX_CIPHER_KEY_SIZE_192) ? 8 : 0;
  879. memcpy(ablkctx->key, key, keylen);
  880. ablkctx->enckey_len = keylen;
  881. context_size = (KEY_CONTEXT_HDR_SALT_AND_PAD +
  882. keylen + alignment) >> 4;
  883. ablkctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, CHCR_KEYCTX_NO_KEY,
  884. 0, 0, context_size);
  885. ablkctx->ciph_mode = CHCR_SCMD_CIPHER_MODE_AES_CTR;
  886. return 0;
  887. badkey_err:
  888. ablkctx->enckey_len = 0;
  889. return err;
  890. }
  891. static void ctr_add_iv(u8 *dstiv, u8 *srciv, u32 add)
  892. {
  893. unsigned int size = AES_BLOCK_SIZE;
  894. __be32 *b = (__be32 *)(dstiv + size);
  895. u32 c, prev;
  896. memcpy(dstiv, srciv, AES_BLOCK_SIZE);
  897. for (; size >= 4; size -= 4) {
  898. prev = be32_to_cpu(*--b);
  899. c = prev + add;
  900. *b = cpu_to_be32(c);
  901. if (prev < c)
  902. break;
  903. add = 1;
  904. }
  905. }
  906. static unsigned int adjust_ctr_overflow(u8 *iv, u32 bytes)
  907. {
  908. __be32 *b = (__be32 *)(iv + AES_BLOCK_SIZE);
  909. u64 c;
  910. u32 temp = be32_to_cpu(*--b);
  911. temp = ~temp;
  912. c = (u64)temp + 1; // No of block can processed without overflow
  913. if ((bytes / AES_BLOCK_SIZE) >= c)
  914. bytes = c * AES_BLOCK_SIZE;
  915. return bytes;
  916. }
  917. static int chcr_update_tweak(struct skcipher_request *req, u8 *iv,
  918. u32 isfinal)
  919. {
  920. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  921. struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(tfm));
  922. struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req);
  923. struct aes_key aes;
  924. int ret, i;
  925. u8 *key;
  926. unsigned int keylen;
  927. int round = reqctx->last_req_len / AES_BLOCK_SIZE;
  928. int round8 = round / 8;
  929. memcpy(iv, reqctx->iv, AES_BLOCK_SIZE);
  930. keylen = ablkctx->enckey_len / 2;
  931. key = ablkctx->key + keylen;
  932. /* For a 192 bit key remove the padded zeroes which was
  933. * added in chcr_xts_setkey
  934. */
  935. if (KEY_CONTEXT_CK_SIZE_G(ntohl(ablkctx->key_ctx_hdr))
  936. == CHCR_KEYCTX_CIPHER_KEY_SIZE_192)
  937. ret = aes_preparekey(&aes, key, keylen - 8);
  938. else
  939. ret = aes_preparekey(&aes, key, keylen);
  940. if (ret)
  941. return ret;
  942. aes_encrypt(&aes, iv, iv);
  943. for (i = 0; i < round8; i++)
  944. gf128mul_x8_ble((le128 *)iv, (le128 *)iv);
  945. for (i = 0; i < (round % 8); i++)
  946. gf128mul_x_ble((le128 *)iv, (le128 *)iv);
  947. if (!isfinal)
  948. aes_decrypt(&aes, iv, iv);
  949. memzero_explicit(&aes, sizeof(aes));
  950. return 0;
  951. }
  952. static int chcr_update_cipher_iv(struct skcipher_request *req,
  953. struct cpl_fw6_pld *fw6_pld, u8 *iv)
  954. {
  955. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  956. struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req);
  957. int subtype = get_cryptoalg_subtype(tfm);
  958. int ret = 0;
  959. if (subtype == CRYPTO_ALG_SUB_TYPE_CTR)
  960. ctr_add_iv(iv, req->iv, (reqctx->processed /
  961. AES_BLOCK_SIZE));
  962. else if (subtype == CRYPTO_ALG_SUB_TYPE_CTR_RFC3686)
  963. *(__be32 *)(reqctx->iv + CTR_RFC3686_NONCE_SIZE +
  964. CTR_RFC3686_IV_SIZE) = cpu_to_be32((reqctx->processed /
  965. AES_BLOCK_SIZE) + 1);
  966. else if (subtype == CRYPTO_ALG_SUB_TYPE_XTS)
  967. ret = chcr_update_tweak(req, iv, 0);
  968. else if (subtype == CRYPTO_ALG_SUB_TYPE_CBC) {
  969. if (reqctx->op)
  970. /*Updated before sending last WR*/
  971. memcpy(iv, req->iv, AES_BLOCK_SIZE);
  972. else
  973. memcpy(iv, &fw6_pld->data[2], AES_BLOCK_SIZE);
  974. }
  975. return ret;
  976. }
  977. /* We need separate function for final iv because in rfc3686 Initial counter
  978. * starts from 1 and buffer size of iv is 8 byte only which remains constant
  979. * for subsequent update requests
  980. */
  981. static int chcr_final_cipher_iv(struct skcipher_request *req,
  982. struct cpl_fw6_pld *fw6_pld, u8 *iv)
  983. {
  984. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  985. struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req);
  986. int subtype = get_cryptoalg_subtype(tfm);
  987. int ret = 0;
  988. if (subtype == CRYPTO_ALG_SUB_TYPE_CTR)
  989. ctr_add_iv(iv, req->iv, DIV_ROUND_UP(reqctx->processed,
  990. AES_BLOCK_SIZE));
  991. else if (subtype == CRYPTO_ALG_SUB_TYPE_XTS) {
  992. if (!reqctx->partial_req)
  993. memcpy(iv, reqctx->iv, AES_BLOCK_SIZE);
  994. else
  995. ret = chcr_update_tweak(req, iv, 1);
  996. }
  997. else if (subtype == CRYPTO_ALG_SUB_TYPE_CBC) {
  998. /*Already updated for Decrypt*/
  999. if (!reqctx->op)
  1000. memcpy(iv, &fw6_pld->data[2], AES_BLOCK_SIZE);
  1001. }
  1002. return ret;
  1003. }
  1004. static int chcr_handle_cipher_resp(struct skcipher_request *req,
  1005. unsigned char *input, int err)
  1006. {
  1007. struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req);
  1008. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  1009. struct cpl_fw6_pld *fw6_pld = (struct cpl_fw6_pld *)input;
  1010. struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(tfm));
  1011. struct uld_ctx *u_ctx = ULD_CTX(c_ctx(tfm));
  1012. struct chcr_dev *dev = c_ctx(tfm)->dev;
  1013. struct chcr_context *ctx = c_ctx(tfm);
  1014. struct adapter *adap = padap(ctx->dev);
  1015. struct cipher_wr_param wrparam;
  1016. struct sk_buff *skb;
  1017. int bytes;
  1018. if (err)
  1019. goto unmap;
  1020. if (req->cryptlen == reqctx->processed) {
  1021. chcr_cipher_dma_unmap(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev,
  1022. req);
  1023. err = chcr_final_cipher_iv(req, fw6_pld, req->iv);
  1024. goto complete;
  1025. }
  1026. if (!reqctx->imm) {
  1027. bytes = chcr_sg_ent_in_wr(reqctx->srcsg, reqctx->dstsg, 0,
  1028. CIP_SPACE_LEFT(ablkctx->enckey_len),
  1029. reqctx->src_ofst, reqctx->dst_ofst);
  1030. if ((bytes + reqctx->processed) >= req->cryptlen)
  1031. bytes = req->cryptlen - reqctx->processed;
  1032. else
  1033. bytes = rounddown(bytes, 16);
  1034. } else {
  1035. /*CTR mode counter overflow*/
  1036. bytes = req->cryptlen - reqctx->processed;
  1037. }
  1038. err = chcr_update_cipher_iv(req, fw6_pld, reqctx->iv);
  1039. if (err)
  1040. goto unmap;
  1041. if (unlikely(bytes == 0)) {
  1042. chcr_cipher_dma_unmap(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev,
  1043. req);
  1044. memcpy(req->iv, reqctx->init_iv, IV);
  1045. atomic_inc(&adap->chcr_stats.fallback);
  1046. err = chcr_cipher_fallback(ablkctx->sw_cipher, req, req->iv,
  1047. reqctx->op);
  1048. goto complete;
  1049. }
  1050. if (get_cryptoalg_subtype(tfm) ==
  1051. CRYPTO_ALG_SUB_TYPE_CTR)
  1052. bytes = adjust_ctr_overflow(reqctx->iv, bytes);
  1053. wrparam.qid = u_ctx->lldi.rxq_ids[reqctx->rxqidx];
  1054. wrparam.req = req;
  1055. wrparam.bytes = bytes;
  1056. skb = create_cipher_wr(&wrparam);
  1057. if (IS_ERR(skb)) {
  1058. pr_err("%s : Failed to form WR. No memory\n", __func__);
  1059. err = PTR_ERR(skb);
  1060. goto unmap;
  1061. }
  1062. skb->dev = u_ctx->lldi.ports[0];
  1063. set_wr_txq(skb, CPL_PRIORITY_DATA, reqctx->txqidx);
  1064. chcr_send_wr(skb);
  1065. reqctx->last_req_len = bytes;
  1066. reqctx->processed += bytes;
  1067. if (get_cryptoalg_subtype(tfm) ==
  1068. CRYPTO_ALG_SUB_TYPE_CBC && req->base.flags ==
  1069. CRYPTO_TFM_REQ_MAY_SLEEP ) {
  1070. complete(&ctx->cbc_aes_aio_done);
  1071. }
  1072. return 0;
  1073. unmap:
  1074. chcr_cipher_dma_unmap(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev, req);
  1075. complete:
  1076. if (get_cryptoalg_subtype(tfm) ==
  1077. CRYPTO_ALG_SUB_TYPE_CBC && req->base.flags ==
  1078. CRYPTO_TFM_REQ_MAY_SLEEP ) {
  1079. complete(&ctx->cbc_aes_aio_done);
  1080. }
  1081. chcr_dec_wrcount(dev);
  1082. skcipher_request_complete(req, err);
  1083. return err;
  1084. }
  1085. static int process_cipher(struct skcipher_request *req,
  1086. unsigned short qid,
  1087. struct sk_buff **skb,
  1088. unsigned short op_type)
  1089. {
  1090. struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req);
  1091. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  1092. unsigned int ivsize = crypto_skcipher_ivsize(tfm);
  1093. struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(tfm));
  1094. struct adapter *adap = padap(c_ctx(tfm)->dev);
  1095. struct cipher_wr_param wrparam;
  1096. int bytes, err = -EINVAL;
  1097. int subtype;
  1098. reqctx->processed = 0;
  1099. reqctx->partial_req = 0;
  1100. if (!req->iv)
  1101. goto error;
  1102. subtype = get_cryptoalg_subtype(tfm);
  1103. if ((ablkctx->enckey_len == 0) || (ivsize > AES_BLOCK_SIZE) ||
  1104. (req->cryptlen == 0) ||
  1105. (req->cryptlen % crypto_skcipher_blocksize(tfm))) {
  1106. if (req->cryptlen == 0 && subtype != CRYPTO_ALG_SUB_TYPE_XTS)
  1107. goto fallback;
  1108. else if (req->cryptlen % crypto_skcipher_blocksize(tfm) &&
  1109. subtype == CRYPTO_ALG_SUB_TYPE_XTS)
  1110. goto fallback;
  1111. pr_err("AES: Invalid value of Key Len %d nbytes %d IV Len %d\n",
  1112. ablkctx->enckey_len, req->cryptlen, ivsize);
  1113. goto error;
  1114. }
  1115. err = chcr_cipher_dma_map(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev, req);
  1116. if (err)
  1117. goto error;
  1118. if (req->cryptlen < (SGE_MAX_WR_LEN - (sizeof(struct chcr_wr) +
  1119. AES_MIN_KEY_SIZE +
  1120. sizeof(struct cpl_rx_phys_dsgl) +
  1121. /*Min dsgl size*/
  1122. 32))) {
  1123. /* Can be sent as Imm*/
  1124. unsigned int dnents = 0, transhdr_len, phys_dsgl, kctx_len;
  1125. dnents = sg_nents_xlen(req->dst, req->cryptlen,
  1126. CHCR_DST_SG_SIZE, 0);
  1127. phys_dsgl = get_space_for_phys_dsgl(dnents);
  1128. kctx_len = roundup(ablkctx->enckey_len, 16);
  1129. transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, phys_dsgl);
  1130. reqctx->imm = (transhdr_len + IV + req->cryptlen) <=
  1131. SGE_MAX_WR_LEN;
  1132. bytes = IV + req->cryptlen;
  1133. } else {
  1134. reqctx->imm = 0;
  1135. }
  1136. if (!reqctx->imm) {
  1137. bytes = chcr_sg_ent_in_wr(req->src, req->dst, 0,
  1138. CIP_SPACE_LEFT(ablkctx->enckey_len),
  1139. 0, 0);
  1140. if ((bytes + reqctx->processed) >= req->cryptlen)
  1141. bytes = req->cryptlen - reqctx->processed;
  1142. else
  1143. bytes = rounddown(bytes, 16);
  1144. } else {
  1145. bytes = req->cryptlen;
  1146. }
  1147. if (subtype == CRYPTO_ALG_SUB_TYPE_CTR) {
  1148. bytes = adjust_ctr_overflow(req->iv, bytes);
  1149. }
  1150. if (subtype == CRYPTO_ALG_SUB_TYPE_CTR_RFC3686) {
  1151. memcpy(reqctx->iv, ablkctx->nonce, CTR_RFC3686_NONCE_SIZE);
  1152. memcpy(reqctx->iv + CTR_RFC3686_NONCE_SIZE, req->iv,
  1153. CTR_RFC3686_IV_SIZE);
  1154. /* initialize counter portion of counter block */
  1155. *(__be32 *)(reqctx->iv + CTR_RFC3686_NONCE_SIZE +
  1156. CTR_RFC3686_IV_SIZE) = cpu_to_be32(1);
  1157. memcpy(reqctx->init_iv, reqctx->iv, IV);
  1158. } else {
  1159. memcpy(reqctx->iv, req->iv, IV);
  1160. memcpy(reqctx->init_iv, req->iv, IV);
  1161. }
  1162. if (unlikely(bytes == 0)) {
  1163. chcr_cipher_dma_unmap(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev,
  1164. req);
  1165. fallback: atomic_inc(&adap->chcr_stats.fallback);
  1166. err = chcr_cipher_fallback(ablkctx->sw_cipher, req,
  1167. subtype ==
  1168. CRYPTO_ALG_SUB_TYPE_CTR_RFC3686 ?
  1169. reqctx->iv : req->iv,
  1170. op_type);
  1171. goto error;
  1172. }
  1173. reqctx->op = op_type;
  1174. reqctx->srcsg = req->src;
  1175. reqctx->dstsg = req->dst;
  1176. reqctx->src_ofst = 0;
  1177. reqctx->dst_ofst = 0;
  1178. wrparam.qid = qid;
  1179. wrparam.req = req;
  1180. wrparam.bytes = bytes;
  1181. *skb = create_cipher_wr(&wrparam);
  1182. if (IS_ERR(*skb)) {
  1183. err = PTR_ERR(*skb);
  1184. goto unmap;
  1185. }
  1186. reqctx->processed = bytes;
  1187. reqctx->last_req_len = bytes;
  1188. reqctx->partial_req = !!(req->cryptlen - reqctx->processed);
  1189. return 0;
  1190. unmap:
  1191. chcr_cipher_dma_unmap(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev, req);
  1192. error:
  1193. return err;
  1194. }
  1195. static int chcr_aes_encrypt(struct skcipher_request *req)
  1196. {
  1197. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  1198. struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req);
  1199. struct chcr_dev *dev = c_ctx(tfm)->dev;
  1200. struct sk_buff *skb = NULL;
  1201. int err;
  1202. struct uld_ctx *u_ctx = ULD_CTX(c_ctx(tfm));
  1203. struct chcr_context *ctx = c_ctx(tfm);
  1204. unsigned int cpu;
  1205. cpu = get_cpu();
  1206. reqctx->txqidx = cpu % ctx->ntxq;
  1207. reqctx->rxqidx = cpu % ctx->nrxq;
  1208. put_cpu();
  1209. err = chcr_inc_wrcount(dev);
  1210. if (err)
  1211. return -ENXIO;
  1212. if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
  1213. reqctx->txqidx) &&
  1214. (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)))) {
  1215. err = -ENOSPC;
  1216. goto error;
  1217. }
  1218. err = process_cipher(req, u_ctx->lldi.rxq_ids[reqctx->rxqidx],
  1219. &skb, CHCR_ENCRYPT_OP);
  1220. if (err || !skb)
  1221. return err;
  1222. skb->dev = u_ctx->lldi.ports[0];
  1223. set_wr_txq(skb, CPL_PRIORITY_DATA, reqctx->txqidx);
  1224. chcr_send_wr(skb);
  1225. if (get_cryptoalg_subtype(tfm) ==
  1226. CRYPTO_ALG_SUB_TYPE_CBC && req->base.flags ==
  1227. CRYPTO_TFM_REQ_MAY_SLEEP ) {
  1228. reqctx->partial_req = 1;
  1229. wait_for_completion(&ctx->cbc_aes_aio_done);
  1230. }
  1231. return -EINPROGRESS;
  1232. error:
  1233. chcr_dec_wrcount(dev);
  1234. return err;
  1235. }
  1236. static int chcr_aes_decrypt(struct skcipher_request *req)
  1237. {
  1238. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  1239. struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req);
  1240. struct uld_ctx *u_ctx = ULD_CTX(c_ctx(tfm));
  1241. struct chcr_dev *dev = c_ctx(tfm)->dev;
  1242. struct sk_buff *skb = NULL;
  1243. int err;
  1244. struct chcr_context *ctx = c_ctx(tfm);
  1245. unsigned int cpu;
  1246. cpu = get_cpu();
  1247. reqctx->txqidx = cpu % ctx->ntxq;
  1248. reqctx->rxqidx = cpu % ctx->nrxq;
  1249. put_cpu();
  1250. err = chcr_inc_wrcount(dev);
  1251. if (err)
  1252. return -ENXIO;
  1253. if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
  1254. reqctx->txqidx) &&
  1255. (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))))
  1256. return -ENOSPC;
  1257. err = process_cipher(req, u_ctx->lldi.rxq_ids[reqctx->rxqidx],
  1258. &skb, CHCR_DECRYPT_OP);
  1259. if (err || !skb)
  1260. return err;
  1261. skb->dev = u_ctx->lldi.ports[0];
  1262. set_wr_txq(skb, CPL_PRIORITY_DATA, reqctx->txqidx);
  1263. chcr_send_wr(skb);
  1264. return -EINPROGRESS;
  1265. }
  1266. static int chcr_device_init(struct chcr_context *ctx)
  1267. {
  1268. struct uld_ctx *u_ctx = NULL;
  1269. int txq_perchan, ntxq;
  1270. int err = 0, rxq_perchan;
  1271. if (!ctx->dev) {
  1272. u_ctx = assign_chcr_device();
  1273. if (!u_ctx) {
  1274. err = -ENXIO;
  1275. pr_err("chcr device assignment fails\n");
  1276. goto out;
  1277. }
  1278. ctx->dev = &u_ctx->dev;
  1279. ntxq = u_ctx->lldi.ntxq;
  1280. rxq_perchan = u_ctx->lldi.nrxq / u_ctx->lldi.nchan;
  1281. txq_perchan = ntxq / u_ctx->lldi.nchan;
  1282. ctx->ntxq = ntxq;
  1283. ctx->nrxq = u_ctx->lldi.nrxq;
  1284. ctx->rxq_perchan = rxq_perchan;
  1285. ctx->txq_perchan = txq_perchan;
  1286. }
  1287. out:
  1288. return err;
  1289. }
  1290. static int chcr_init_tfm(struct crypto_skcipher *tfm)
  1291. {
  1292. struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
  1293. struct chcr_context *ctx = crypto_skcipher_ctx(tfm);
  1294. struct ablk_ctx *ablkctx = ABLK_CTX(ctx);
  1295. ablkctx->sw_cipher = crypto_alloc_skcipher(alg->base.cra_name, 0,
  1296. CRYPTO_ALG_NEED_FALLBACK);
  1297. if (IS_ERR(ablkctx->sw_cipher)) {
  1298. pr_err("failed to allocate fallback for %s\n", alg->base.cra_name);
  1299. return PTR_ERR(ablkctx->sw_cipher);
  1300. }
  1301. init_completion(&ctx->cbc_aes_aio_done);
  1302. crypto_skcipher_set_reqsize(tfm, sizeof(struct chcr_skcipher_req_ctx) +
  1303. crypto_skcipher_reqsize(ablkctx->sw_cipher));
  1304. return chcr_device_init(ctx);
  1305. }
  1306. static int chcr_rfc3686_init(struct crypto_skcipher *tfm)
  1307. {
  1308. struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
  1309. struct chcr_context *ctx = crypto_skcipher_ctx(tfm);
  1310. struct ablk_ctx *ablkctx = ABLK_CTX(ctx);
  1311. /*RFC3686 initialises IV counter value to 1, rfc3686(ctr(aes))
  1312. * cannot be used as fallback in chcr_handle_cipher_response
  1313. */
  1314. ablkctx->sw_cipher = crypto_alloc_skcipher("ctr(aes)", 0,
  1315. CRYPTO_ALG_NEED_FALLBACK);
  1316. if (IS_ERR(ablkctx->sw_cipher)) {
  1317. pr_err("failed to allocate fallback for %s\n", alg->base.cra_name);
  1318. return PTR_ERR(ablkctx->sw_cipher);
  1319. }
  1320. crypto_skcipher_set_reqsize(tfm, sizeof(struct chcr_skcipher_req_ctx) +
  1321. crypto_skcipher_reqsize(ablkctx->sw_cipher));
  1322. return chcr_device_init(ctx);
  1323. }
  1324. static void chcr_exit_tfm(struct crypto_skcipher *tfm)
  1325. {
  1326. struct chcr_context *ctx = crypto_skcipher_ctx(tfm);
  1327. struct ablk_ctx *ablkctx = ABLK_CTX(ctx);
  1328. crypto_free_skcipher(ablkctx->sw_cipher);
  1329. }
  1330. static int get_alg_config(struct algo_param *params,
  1331. unsigned int auth_size)
  1332. {
  1333. switch (auth_size) {
  1334. case SHA1_DIGEST_SIZE:
  1335. params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_160;
  1336. params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA1;
  1337. params->result_size = SHA1_DIGEST_SIZE;
  1338. break;
  1339. case SHA224_DIGEST_SIZE:
  1340. params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_256;
  1341. params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA224;
  1342. params->result_size = SHA256_DIGEST_SIZE;
  1343. break;
  1344. case SHA256_DIGEST_SIZE:
  1345. params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_256;
  1346. params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA256;
  1347. params->result_size = SHA256_DIGEST_SIZE;
  1348. break;
  1349. case SHA384_DIGEST_SIZE:
  1350. params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_512;
  1351. params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA512_384;
  1352. params->result_size = SHA512_DIGEST_SIZE;
  1353. break;
  1354. case SHA512_DIGEST_SIZE:
  1355. params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_512;
  1356. params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA512_512;
  1357. params->result_size = SHA512_DIGEST_SIZE;
  1358. break;
  1359. default:
  1360. pr_err("ERROR, unsupported digest size\n");
  1361. return -EINVAL;
  1362. }
  1363. return 0;
  1364. }
  1365. /**
  1366. * create_hash_wr - Create hash work request
  1367. * @req: Cipher req base
  1368. * @param: Container for create_hash_wr()'s parameters
  1369. */
  1370. static struct sk_buff *create_hash_wr(struct ahash_request *req,
  1371. struct hash_wr_param *param)
  1372. {
  1373. struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
  1374. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1375. struct chcr_context *ctx = h_ctx(tfm);
  1376. struct hmac_ctx *hmacctx = HMAC_CTX(ctx);
  1377. struct sk_buff *skb = NULL;
  1378. struct uld_ctx *u_ctx = ULD_CTX(ctx);
  1379. struct chcr_wr *chcr_req;
  1380. struct ulptx_sgl *ulptx;
  1381. unsigned int nents = 0, transhdr_len;
  1382. unsigned int temp = 0;
  1383. gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
  1384. GFP_ATOMIC;
  1385. struct adapter *adap = padap(h_ctx(tfm)->dev);
  1386. int error = 0;
  1387. unsigned int rx_channel_id = req_ctx->rxqidx / ctx->rxq_perchan;
  1388. rx_channel_id = cxgb4_port_e2cchan(u_ctx->lldi.ports[rx_channel_id]);
  1389. transhdr_len = HASH_TRANSHDR_SIZE(param->kctx_len);
  1390. req_ctx->hctx_wr.imm = (transhdr_len + param->bfr_len +
  1391. param->sg_len) <= SGE_MAX_WR_LEN;
  1392. nents = sg_nents_xlen(req_ctx->hctx_wr.srcsg, param->sg_len,
  1393. CHCR_SRC_SG_SIZE, req_ctx->hctx_wr.src_ofst);
  1394. nents += param->bfr_len ? 1 : 0;
  1395. transhdr_len += req_ctx->hctx_wr.imm ? roundup(param->bfr_len +
  1396. param->sg_len, 16) : (sgl_len(nents) * 8);
  1397. transhdr_len = roundup(transhdr_len, 16);
  1398. skb = alloc_skb(transhdr_len, flags);
  1399. if (!skb)
  1400. return ERR_PTR(-ENOMEM);
  1401. chcr_req = __skb_put_zero(skb, transhdr_len);
  1402. chcr_req->sec_cpl.op_ivinsrtofst =
  1403. FILL_SEC_CPL_OP_IVINSR(rx_channel_id, 2, 0);
  1404. chcr_req->sec_cpl.pldlen = htonl(param->bfr_len + param->sg_len);
  1405. chcr_req->sec_cpl.aadstart_cipherstop_hi =
  1406. FILL_SEC_CPL_CIPHERSTOP_HI(0, 0, 0, 0);
  1407. chcr_req->sec_cpl.cipherstop_lo_authinsert =
  1408. FILL_SEC_CPL_AUTHINSERT(0, 1, 0, 0);
  1409. chcr_req->sec_cpl.seqno_numivs =
  1410. FILL_SEC_CPL_SCMD0_SEQNO(0, 0, 0, param->alg_prm.auth_mode,
  1411. param->opad_needed, 0);
  1412. chcr_req->sec_cpl.ivgen_hdrlen =
  1413. FILL_SEC_CPL_IVGEN_HDRLEN(param->last, param->more, 0, 1, 0, 0);
  1414. memcpy(chcr_req->key_ctx.key, req_ctx->partial_hash,
  1415. param->alg_prm.result_size);
  1416. if (param->opad_needed)
  1417. memcpy(chcr_req->key_ctx.key +
  1418. ((param->alg_prm.result_size <= 32) ? 32 :
  1419. CHCR_HASH_MAX_DIGEST_SIZE),
  1420. hmacctx->opad, param->alg_prm.result_size);
  1421. chcr_req->key_ctx.ctx_hdr = FILL_KEY_CTX_HDR(CHCR_KEYCTX_NO_KEY,
  1422. param->alg_prm.mk_size, 0,
  1423. param->opad_needed,
  1424. ((param->kctx_len +
  1425. sizeof(chcr_req->key_ctx)) >> 4));
  1426. chcr_req->sec_cpl.scmd1 = cpu_to_be64((u64)param->scmd1);
  1427. ulptx = (struct ulptx_sgl *)((u8 *)(chcr_req + 1) + param->kctx_len +
  1428. DUMMY_BYTES);
  1429. if (param->bfr_len != 0) {
  1430. req_ctx->hctx_wr.dma_addr =
  1431. dma_map_single(&u_ctx->lldi.pdev->dev, req_ctx->reqbfr,
  1432. param->bfr_len, DMA_TO_DEVICE);
  1433. if (dma_mapping_error(&u_ctx->lldi.pdev->dev,
  1434. req_ctx->hctx_wr. dma_addr)) {
  1435. error = -ENOMEM;
  1436. goto err;
  1437. }
  1438. req_ctx->hctx_wr.dma_len = param->bfr_len;
  1439. } else {
  1440. req_ctx->hctx_wr.dma_addr = 0;
  1441. }
  1442. chcr_add_hash_src_ent(req, ulptx, param);
  1443. /* Request upto max wr size */
  1444. temp = param->kctx_len + DUMMY_BYTES + (req_ctx->hctx_wr.imm ?
  1445. (param->sg_len + param->bfr_len) : 0);
  1446. atomic_inc(&adap->chcr_stats.digest_rqst);
  1447. create_wreq(h_ctx(tfm), chcr_req, &req->base, req_ctx->hctx_wr.imm,
  1448. param->hash_size, transhdr_len,
  1449. temp, 0);
  1450. req_ctx->hctx_wr.skb = skb;
  1451. return skb;
  1452. err:
  1453. kfree_skb(skb);
  1454. return ERR_PTR(error);
  1455. }
  1456. static int chcr_ahash_update(struct ahash_request *req)
  1457. {
  1458. struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
  1459. struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
  1460. struct uld_ctx *u_ctx = ULD_CTX(h_ctx(rtfm));
  1461. struct chcr_context *ctx = h_ctx(rtfm);
  1462. struct chcr_dev *dev = h_ctx(rtfm)->dev;
  1463. struct sk_buff *skb;
  1464. u8 remainder = 0, bs;
  1465. unsigned int nbytes = req->nbytes;
  1466. struct hash_wr_param params;
  1467. int error;
  1468. unsigned int cpu;
  1469. cpu = get_cpu();
  1470. req_ctx->txqidx = cpu % ctx->ntxq;
  1471. req_ctx->rxqidx = cpu % ctx->nrxq;
  1472. put_cpu();
  1473. bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
  1474. if (nbytes + req_ctx->reqlen >= bs) {
  1475. remainder = (nbytes + req_ctx->reqlen) % bs;
  1476. nbytes = nbytes + req_ctx->reqlen - remainder;
  1477. } else {
  1478. sg_pcopy_to_buffer(req->src, sg_nents(req->src), req_ctx->reqbfr
  1479. + req_ctx->reqlen, nbytes, 0);
  1480. req_ctx->reqlen += nbytes;
  1481. return 0;
  1482. }
  1483. error = chcr_inc_wrcount(dev);
  1484. if (error)
  1485. return -ENXIO;
  1486. /* Detach state for CHCR means lldi or padap is freed. Increasing
  1487. * inflight count for dev guarantees that lldi and padap is valid
  1488. */
  1489. if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
  1490. req_ctx->txqidx) &&
  1491. (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)))) {
  1492. error = -ENOSPC;
  1493. goto err;
  1494. }
  1495. chcr_init_hctx_per_wr(req_ctx);
  1496. error = chcr_hash_dma_map(&u_ctx->lldi.pdev->dev, req);
  1497. if (error) {
  1498. error = -ENOMEM;
  1499. goto err;
  1500. }
  1501. get_alg_config(&params.alg_prm, crypto_ahash_digestsize(rtfm));
  1502. params.kctx_len = roundup(params.alg_prm.result_size, 16);
  1503. params.sg_len = chcr_hash_ent_in_wr(req->src, !!req_ctx->reqlen,
  1504. HASH_SPACE_LEFT(params.kctx_len), 0);
  1505. if (params.sg_len > req->nbytes)
  1506. params.sg_len = req->nbytes;
  1507. params.sg_len = rounddown(params.sg_len + req_ctx->reqlen, bs) -
  1508. req_ctx->reqlen;
  1509. params.opad_needed = 0;
  1510. params.more = 1;
  1511. params.last = 0;
  1512. params.bfr_len = req_ctx->reqlen;
  1513. params.scmd1 = 0;
  1514. req_ctx->hctx_wr.srcsg = req->src;
  1515. params.hash_size = params.alg_prm.result_size;
  1516. req_ctx->data_len += params.sg_len + params.bfr_len;
  1517. skb = create_hash_wr(req, &params);
  1518. if (IS_ERR(skb)) {
  1519. error = PTR_ERR(skb);
  1520. goto unmap;
  1521. }
  1522. req_ctx->hctx_wr.processed += params.sg_len;
  1523. if (remainder) {
  1524. /* Swap buffers */
  1525. swap(req_ctx->reqbfr, req_ctx->skbfr);
  1526. sg_pcopy_to_buffer(req->src, sg_nents(req->src),
  1527. req_ctx->reqbfr, remainder, req->nbytes -
  1528. remainder);
  1529. }
  1530. req_ctx->reqlen = remainder;
  1531. skb->dev = u_ctx->lldi.ports[0];
  1532. set_wr_txq(skb, CPL_PRIORITY_DATA, req_ctx->txqidx);
  1533. chcr_send_wr(skb);
  1534. return -EINPROGRESS;
  1535. unmap:
  1536. chcr_hash_dma_unmap(&u_ctx->lldi.pdev->dev, req);
  1537. err:
  1538. chcr_dec_wrcount(dev);
  1539. return error;
  1540. }
  1541. static void create_last_hash_block(char *bfr_ptr, unsigned int bs, u64 scmd1)
  1542. {
  1543. memset(bfr_ptr, 0, bs);
  1544. *bfr_ptr = 0x80;
  1545. if (bs == 64)
  1546. *(__be64 *)(bfr_ptr + 56) = cpu_to_be64(scmd1 << 3);
  1547. else
  1548. *(__be64 *)(bfr_ptr + 120) = cpu_to_be64(scmd1 << 3);
  1549. }
  1550. static int chcr_ahash_final(struct ahash_request *req)
  1551. {
  1552. struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
  1553. struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
  1554. struct chcr_dev *dev = h_ctx(rtfm)->dev;
  1555. struct hash_wr_param params;
  1556. struct sk_buff *skb;
  1557. struct uld_ctx *u_ctx = ULD_CTX(h_ctx(rtfm));
  1558. struct chcr_context *ctx = h_ctx(rtfm);
  1559. u8 bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
  1560. int error;
  1561. unsigned int cpu;
  1562. cpu = get_cpu();
  1563. req_ctx->txqidx = cpu % ctx->ntxq;
  1564. req_ctx->rxqidx = cpu % ctx->nrxq;
  1565. put_cpu();
  1566. error = chcr_inc_wrcount(dev);
  1567. if (error)
  1568. return -ENXIO;
  1569. chcr_init_hctx_per_wr(req_ctx);
  1570. if (is_hmac(crypto_ahash_tfm(rtfm)))
  1571. params.opad_needed = 1;
  1572. else
  1573. params.opad_needed = 0;
  1574. params.sg_len = 0;
  1575. req_ctx->hctx_wr.isfinal = 1;
  1576. get_alg_config(&params.alg_prm, crypto_ahash_digestsize(rtfm));
  1577. params.kctx_len = roundup(params.alg_prm.result_size, 16);
  1578. if (is_hmac(crypto_ahash_tfm(rtfm))) {
  1579. params.opad_needed = 1;
  1580. params.kctx_len *= 2;
  1581. } else {
  1582. params.opad_needed = 0;
  1583. }
  1584. req_ctx->hctx_wr.result = 1;
  1585. params.bfr_len = req_ctx->reqlen;
  1586. req_ctx->data_len += params.bfr_len + params.sg_len;
  1587. req_ctx->hctx_wr.srcsg = req->src;
  1588. if (req_ctx->reqlen == 0) {
  1589. create_last_hash_block(req_ctx->reqbfr, bs, req_ctx->data_len);
  1590. params.last = 0;
  1591. params.more = 1;
  1592. params.scmd1 = 0;
  1593. params.bfr_len = bs;
  1594. } else {
  1595. params.scmd1 = req_ctx->data_len;
  1596. params.last = 1;
  1597. params.more = 0;
  1598. }
  1599. params.hash_size = crypto_ahash_digestsize(rtfm);
  1600. skb = create_hash_wr(req, &params);
  1601. if (IS_ERR(skb)) {
  1602. error = PTR_ERR(skb);
  1603. goto err;
  1604. }
  1605. req_ctx->reqlen = 0;
  1606. skb->dev = u_ctx->lldi.ports[0];
  1607. set_wr_txq(skb, CPL_PRIORITY_DATA, req_ctx->txqidx);
  1608. chcr_send_wr(skb);
  1609. return -EINPROGRESS;
  1610. err:
  1611. chcr_dec_wrcount(dev);
  1612. return error;
  1613. }
  1614. static int chcr_ahash_finup(struct ahash_request *req)
  1615. {
  1616. struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
  1617. struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
  1618. struct chcr_dev *dev = h_ctx(rtfm)->dev;
  1619. struct uld_ctx *u_ctx = ULD_CTX(h_ctx(rtfm));
  1620. struct chcr_context *ctx = h_ctx(rtfm);
  1621. struct sk_buff *skb;
  1622. struct hash_wr_param params;
  1623. u8 bs;
  1624. int error;
  1625. unsigned int cpu;
  1626. cpu = get_cpu();
  1627. req_ctx->txqidx = cpu % ctx->ntxq;
  1628. req_ctx->rxqidx = cpu % ctx->nrxq;
  1629. put_cpu();
  1630. bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
  1631. error = chcr_inc_wrcount(dev);
  1632. if (error)
  1633. return -ENXIO;
  1634. if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
  1635. req_ctx->txqidx) &&
  1636. (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)))) {
  1637. error = -ENOSPC;
  1638. goto err;
  1639. }
  1640. chcr_init_hctx_per_wr(req_ctx);
  1641. error = chcr_hash_dma_map(&u_ctx->lldi.pdev->dev, req);
  1642. if (error) {
  1643. error = -ENOMEM;
  1644. goto err;
  1645. }
  1646. get_alg_config(&params.alg_prm, crypto_ahash_digestsize(rtfm));
  1647. params.kctx_len = roundup(params.alg_prm.result_size, 16);
  1648. if (is_hmac(crypto_ahash_tfm(rtfm))) {
  1649. params.kctx_len *= 2;
  1650. params.opad_needed = 1;
  1651. } else {
  1652. params.opad_needed = 0;
  1653. }
  1654. params.sg_len = chcr_hash_ent_in_wr(req->src, !!req_ctx->reqlen,
  1655. HASH_SPACE_LEFT(params.kctx_len), 0);
  1656. if (params.sg_len < req->nbytes) {
  1657. if (is_hmac(crypto_ahash_tfm(rtfm))) {
  1658. params.kctx_len /= 2;
  1659. params.opad_needed = 0;
  1660. }
  1661. params.last = 0;
  1662. params.more = 1;
  1663. params.sg_len = rounddown(params.sg_len + req_ctx->reqlen, bs)
  1664. - req_ctx->reqlen;
  1665. params.hash_size = params.alg_prm.result_size;
  1666. params.scmd1 = 0;
  1667. } else {
  1668. params.last = 1;
  1669. params.more = 0;
  1670. params.sg_len = req->nbytes;
  1671. params.hash_size = crypto_ahash_digestsize(rtfm);
  1672. params.scmd1 = req_ctx->data_len + req_ctx->reqlen +
  1673. params.sg_len;
  1674. }
  1675. params.bfr_len = req_ctx->reqlen;
  1676. req_ctx->data_len += params.bfr_len + params.sg_len;
  1677. req_ctx->hctx_wr.result = 1;
  1678. req_ctx->hctx_wr.srcsg = req->src;
  1679. if ((req_ctx->reqlen + req->nbytes) == 0) {
  1680. create_last_hash_block(req_ctx->reqbfr, bs, req_ctx->data_len);
  1681. params.last = 0;
  1682. params.more = 1;
  1683. params.scmd1 = 0;
  1684. params.bfr_len = bs;
  1685. }
  1686. skb = create_hash_wr(req, &params);
  1687. if (IS_ERR(skb)) {
  1688. error = PTR_ERR(skb);
  1689. goto unmap;
  1690. }
  1691. req_ctx->reqlen = 0;
  1692. req_ctx->hctx_wr.processed += params.sg_len;
  1693. skb->dev = u_ctx->lldi.ports[0];
  1694. set_wr_txq(skb, CPL_PRIORITY_DATA, req_ctx->txqidx);
  1695. chcr_send_wr(skb);
  1696. return -EINPROGRESS;
  1697. unmap:
  1698. chcr_hash_dma_unmap(&u_ctx->lldi.pdev->dev, req);
  1699. err:
  1700. chcr_dec_wrcount(dev);
  1701. return error;
  1702. }
  1703. static int chcr_hmac_init(struct ahash_request *areq);
  1704. static int chcr_sha_init(struct ahash_request *areq);
  1705. static int chcr_ahash_digest(struct ahash_request *req)
  1706. {
  1707. struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
  1708. struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
  1709. struct chcr_dev *dev = h_ctx(rtfm)->dev;
  1710. struct uld_ctx *u_ctx = ULD_CTX(h_ctx(rtfm));
  1711. struct chcr_context *ctx = h_ctx(rtfm);
  1712. struct sk_buff *skb;
  1713. struct hash_wr_param params;
  1714. u8 bs;
  1715. int error;
  1716. unsigned int cpu;
  1717. cpu = get_cpu();
  1718. req_ctx->txqidx = cpu % ctx->ntxq;
  1719. req_ctx->rxqidx = cpu % ctx->nrxq;
  1720. put_cpu();
  1721. if (is_hmac(crypto_ahash_tfm(rtfm)))
  1722. chcr_hmac_init(req);
  1723. else
  1724. chcr_sha_init(req);
  1725. bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
  1726. error = chcr_inc_wrcount(dev);
  1727. if (error)
  1728. return -ENXIO;
  1729. if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
  1730. req_ctx->txqidx) &&
  1731. (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)))) {
  1732. error = -ENOSPC;
  1733. goto err;
  1734. }
  1735. chcr_init_hctx_per_wr(req_ctx);
  1736. error = chcr_hash_dma_map(&u_ctx->lldi.pdev->dev, req);
  1737. if (error) {
  1738. error = -ENOMEM;
  1739. goto err;
  1740. }
  1741. get_alg_config(&params.alg_prm, crypto_ahash_digestsize(rtfm));
  1742. params.kctx_len = roundup(params.alg_prm.result_size, 16);
  1743. if (is_hmac(crypto_ahash_tfm(rtfm))) {
  1744. params.kctx_len *= 2;
  1745. params.opad_needed = 1;
  1746. } else {
  1747. params.opad_needed = 0;
  1748. }
  1749. params.sg_len = chcr_hash_ent_in_wr(req->src, !!req_ctx->reqlen,
  1750. HASH_SPACE_LEFT(params.kctx_len), 0);
  1751. if (params.sg_len < req->nbytes) {
  1752. if (is_hmac(crypto_ahash_tfm(rtfm))) {
  1753. params.kctx_len /= 2;
  1754. params.opad_needed = 0;
  1755. }
  1756. params.last = 0;
  1757. params.more = 1;
  1758. params.scmd1 = 0;
  1759. params.sg_len = rounddown(params.sg_len, bs);
  1760. params.hash_size = params.alg_prm.result_size;
  1761. } else {
  1762. params.sg_len = req->nbytes;
  1763. params.hash_size = crypto_ahash_digestsize(rtfm);
  1764. params.last = 1;
  1765. params.more = 0;
  1766. params.scmd1 = req->nbytes + req_ctx->data_len;
  1767. }
  1768. params.bfr_len = 0;
  1769. req_ctx->hctx_wr.result = 1;
  1770. req_ctx->hctx_wr.srcsg = req->src;
  1771. req_ctx->data_len += params.bfr_len + params.sg_len;
  1772. if (req->nbytes == 0) {
  1773. create_last_hash_block(req_ctx->reqbfr, bs, req_ctx->data_len);
  1774. params.more = 1;
  1775. params.bfr_len = bs;
  1776. }
  1777. skb = create_hash_wr(req, &params);
  1778. if (IS_ERR(skb)) {
  1779. error = PTR_ERR(skb);
  1780. goto unmap;
  1781. }
  1782. req_ctx->hctx_wr.processed += params.sg_len;
  1783. skb->dev = u_ctx->lldi.ports[0];
  1784. set_wr_txq(skb, CPL_PRIORITY_DATA, req_ctx->txqidx);
  1785. chcr_send_wr(skb);
  1786. return -EINPROGRESS;
  1787. unmap:
  1788. chcr_hash_dma_unmap(&u_ctx->lldi.pdev->dev, req);
  1789. err:
  1790. chcr_dec_wrcount(dev);
  1791. return error;
  1792. }
  1793. static int chcr_ahash_continue(struct ahash_request *req)
  1794. {
  1795. struct chcr_ahash_req_ctx *reqctx = ahash_request_ctx(req);
  1796. struct chcr_hctx_per_wr *hctx_wr = &reqctx->hctx_wr;
  1797. struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
  1798. struct chcr_context *ctx = h_ctx(rtfm);
  1799. struct uld_ctx *u_ctx = ULD_CTX(ctx);
  1800. struct sk_buff *skb;
  1801. struct hash_wr_param params;
  1802. u8 bs;
  1803. int error;
  1804. unsigned int cpu;
  1805. cpu = get_cpu();
  1806. reqctx->txqidx = cpu % ctx->ntxq;
  1807. reqctx->rxqidx = cpu % ctx->nrxq;
  1808. put_cpu();
  1809. bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
  1810. get_alg_config(&params.alg_prm, crypto_ahash_digestsize(rtfm));
  1811. params.kctx_len = roundup(params.alg_prm.result_size, 16);
  1812. if (is_hmac(crypto_ahash_tfm(rtfm))) {
  1813. params.kctx_len *= 2;
  1814. params.opad_needed = 1;
  1815. } else {
  1816. params.opad_needed = 0;
  1817. }
  1818. params.sg_len = chcr_hash_ent_in_wr(hctx_wr->srcsg, 0,
  1819. HASH_SPACE_LEFT(params.kctx_len),
  1820. hctx_wr->src_ofst);
  1821. if ((params.sg_len + hctx_wr->processed) > req->nbytes)
  1822. params.sg_len = req->nbytes - hctx_wr->processed;
  1823. if (!hctx_wr->result ||
  1824. ((params.sg_len + hctx_wr->processed) < req->nbytes)) {
  1825. if (is_hmac(crypto_ahash_tfm(rtfm))) {
  1826. params.kctx_len /= 2;
  1827. params.opad_needed = 0;
  1828. }
  1829. params.last = 0;
  1830. params.more = 1;
  1831. params.sg_len = rounddown(params.sg_len, bs);
  1832. params.hash_size = params.alg_prm.result_size;
  1833. params.scmd1 = 0;
  1834. } else {
  1835. params.last = 1;
  1836. params.more = 0;
  1837. params.hash_size = crypto_ahash_digestsize(rtfm);
  1838. params.scmd1 = reqctx->data_len + params.sg_len;
  1839. }
  1840. params.bfr_len = 0;
  1841. reqctx->data_len += params.sg_len;
  1842. skb = create_hash_wr(req, &params);
  1843. if (IS_ERR(skb)) {
  1844. error = PTR_ERR(skb);
  1845. goto err;
  1846. }
  1847. hctx_wr->processed += params.sg_len;
  1848. skb->dev = u_ctx->lldi.ports[0];
  1849. set_wr_txq(skb, CPL_PRIORITY_DATA, reqctx->txqidx);
  1850. chcr_send_wr(skb);
  1851. return 0;
  1852. err:
  1853. return error;
  1854. }
  1855. static inline void chcr_handle_ahash_resp(struct ahash_request *req,
  1856. unsigned char *input,
  1857. int err)
  1858. {
  1859. struct chcr_ahash_req_ctx *reqctx = ahash_request_ctx(req);
  1860. struct chcr_hctx_per_wr *hctx_wr = &reqctx->hctx_wr;
  1861. int digestsize, updated_digestsize;
  1862. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1863. struct uld_ctx *u_ctx = ULD_CTX(h_ctx(tfm));
  1864. struct chcr_dev *dev = h_ctx(tfm)->dev;
  1865. if (input == NULL)
  1866. goto out;
  1867. digestsize = crypto_ahash_digestsize(crypto_ahash_reqtfm(req));
  1868. updated_digestsize = digestsize;
  1869. if (digestsize == SHA224_DIGEST_SIZE)
  1870. updated_digestsize = SHA256_DIGEST_SIZE;
  1871. else if (digestsize == SHA384_DIGEST_SIZE)
  1872. updated_digestsize = SHA512_DIGEST_SIZE;
  1873. if (hctx_wr->dma_addr) {
  1874. dma_unmap_single(&u_ctx->lldi.pdev->dev, hctx_wr->dma_addr,
  1875. hctx_wr->dma_len, DMA_TO_DEVICE);
  1876. hctx_wr->dma_addr = 0;
  1877. }
  1878. if (hctx_wr->isfinal || ((hctx_wr->processed + reqctx->reqlen) ==
  1879. req->nbytes)) {
  1880. if (hctx_wr->result == 1) {
  1881. hctx_wr->result = 0;
  1882. memcpy(req->result, input + sizeof(struct cpl_fw6_pld),
  1883. digestsize);
  1884. } else {
  1885. memcpy(reqctx->partial_hash,
  1886. input + sizeof(struct cpl_fw6_pld),
  1887. updated_digestsize);
  1888. }
  1889. goto unmap;
  1890. }
  1891. memcpy(reqctx->partial_hash, input + sizeof(struct cpl_fw6_pld),
  1892. updated_digestsize);
  1893. err = chcr_ahash_continue(req);
  1894. if (err)
  1895. goto unmap;
  1896. return;
  1897. unmap:
  1898. if (hctx_wr->is_sg_map)
  1899. chcr_hash_dma_unmap(&u_ctx->lldi.pdev->dev, req);
  1900. out:
  1901. chcr_dec_wrcount(dev);
  1902. ahash_request_complete(req, err);
  1903. }
  1904. /*
  1905. * chcr_handle_resp - Unmap the DMA buffers associated with the request
  1906. * @req: crypto request
  1907. */
  1908. int chcr_handle_resp(struct crypto_async_request *req, unsigned char *input,
  1909. int err)
  1910. {
  1911. struct crypto_tfm *tfm = req->tfm;
  1912. struct chcr_context *ctx = crypto_tfm_ctx(tfm);
  1913. struct adapter *adap = padap(ctx->dev);
  1914. switch (tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
  1915. case CRYPTO_ALG_TYPE_AEAD:
  1916. err = chcr_handle_aead_resp(aead_request_cast(req), input, err);
  1917. break;
  1918. case CRYPTO_ALG_TYPE_SKCIPHER:
  1919. chcr_handle_cipher_resp(skcipher_request_cast(req),
  1920. input, err);
  1921. break;
  1922. case CRYPTO_ALG_TYPE_AHASH:
  1923. chcr_handle_ahash_resp(ahash_request_cast(req), input, err);
  1924. }
  1925. atomic_inc(&adap->chcr_stats.complete);
  1926. return err;
  1927. }
  1928. static int chcr_ahash_export(struct ahash_request *areq, void *out)
  1929. {
  1930. struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  1931. struct chcr_ahash_req_ctx *state = out;
  1932. state->reqlen = req_ctx->reqlen;
  1933. state->data_len = req_ctx->data_len;
  1934. memcpy(state->bfr1, req_ctx->reqbfr, req_ctx->reqlen);
  1935. memcpy(state->partial_hash, req_ctx->partial_hash,
  1936. CHCR_HASH_MAX_DIGEST_SIZE);
  1937. chcr_init_hctx_per_wr(state);
  1938. return 0;
  1939. }
  1940. static int chcr_ahash_import(struct ahash_request *areq, const void *in)
  1941. {
  1942. struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  1943. struct chcr_ahash_req_ctx *state = (struct chcr_ahash_req_ctx *)in;
  1944. req_ctx->reqlen = state->reqlen;
  1945. req_ctx->data_len = state->data_len;
  1946. req_ctx->reqbfr = req_ctx->bfr1;
  1947. req_ctx->skbfr = req_ctx->bfr2;
  1948. memcpy(req_ctx->bfr1, state->bfr1, CHCR_HASH_MAX_BLOCK_SIZE_128);
  1949. memcpy(req_ctx->partial_hash, state->partial_hash,
  1950. CHCR_HASH_MAX_DIGEST_SIZE);
  1951. chcr_init_hctx_per_wr(req_ctx);
  1952. return 0;
  1953. }
  1954. static int chcr_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
  1955. unsigned int keylen)
  1956. {
  1957. struct hmac_ctx *hmacctx = HMAC_CTX(h_ctx(tfm));
  1958. /* use the key to calculate the ipad and opad. ipad will sent with the
  1959. * first request's data. opad will be sent with the final hash result
  1960. * ipad in hmacctx->ipad and opad in hmacctx->opad location
  1961. */
  1962. return chcr_prepare_hmac_key(key, keylen, crypto_ahash_digestsize(tfm),
  1963. hmacctx->ipad, hmacctx->opad);
  1964. }
  1965. static int chcr_aes_xts_setkey(struct crypto_skcipher *cipher, const u8 *key,
  1966. unsigned int key_len)
  1967. {
  1968. struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(cipher));
  1969. unsigned short context_size = 0;
  1970. int err;
  1971. err = chcr_cipher_fallback_setkey(cipher, key, key_len);
  1972. if (err)
  1973. goto badkey_err;
  1974. memcpy(ablkctx->key, key, key_len);
  1975. ablkctx->enckey_len = key_len;
  1976. get_aes_decrypt_key(ablkctx->rrkey, ablkctx->key, key_len << 2);
  1977. context_size = (KEY_CONTEXT_HDR_SALT_AND_PAD + key_len) >> 4;
  1978. /* Both keys for xts must be aligned to 16 byte boundary
  1979. * by padding with zeros. So for 24 byte keys padding 8 zeroes.
  1980. */
  1981. if (key_len == 48) {
  1982. context_size = (KEY_CONTEXT_HDR_SALT_AND_PAD + key_len
  1983. + 16) >> 4;
  1984. memmove(ablkctx->key + 32, ablkctx->key + 24, 24);
  1985. memset(ablkctx->key + 24, 0, 8);
  1986. memset(ablkctx->key + 56, 0, 8);
  1987. ablkctx->enckey_len = 64;
  1988. ablkctx->key_ctx_hdr =
  1989. FILL_KEY_CTX_HDR(CHCR_KEYCTX_CIPHER_KEY_SIZE_192,
  1990. CHCR_KEYCTX_NO_KEY, 1,
  1991. 0, context_size);
  1992. } else {
  1993. ablkctx->key_ctx_hdr =
  1994. FILL_KEY_CTX_HDR((key_len == AES_KEYSIZE_256) ?
  1995. CHCR_KEYCTX_CIPHER_KEY_SIZE_128 :
  1996. CHCR_KEYCTX_CIPHER_KEY_SIZE_256,
  1997. CHCR_KEYCTX_NO_KEY, 1,
  1998. 0, context_size);
  1999. }
  2000. ablkctx->ciph_mode = CHCR_SCMD_CIPHER_MODE_AES_XTS;
  2001. return 0;
  2002. badkey_err:
  2003. ablkctx->enckey_len = 0;
  2004. return err;
  2005. }
  2006. static int chcr_sha_init(struct ahash_request *areq)
  2007. {
  2008. struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  2009. struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
  2010. int digestsize = crypto_ahash_digestsize(tfm);
  2011. req_ctx->data_len = 0;
  2012. req_ctx->reqlen = 0;
  2013. req_ctx->reqbfr = req_ctx->bfr1;
  2014. req_ctx->skbfr = req_ctx->bfr2;
  2015. copy_hash_init_values(req_ctx->partial_hash, digestsize);
  2016. return 0;
  2017. }
  2018. static int chcr_sha_cra_init(struct crypto_tfm *tfm)
  2019. {
  2020. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  2021. sizeof(struct chcr_ahash_req_ctx));
  2022. return chcr_device_init(crypto_tfm_ctx(tfm));
  2023. }
  2024. static int chcr_hmac_init(struct ahash_request *areq)
  2025. {
  2026. struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  2027. struct crypto_ahash *rtfm = crypto_ahash_reqtfm(areq);
  2028. struct hmac_ctx *hmacctx = HMAC_CTX(h_ctx(rtfm));
  2029. unsigned int digestsize = crypto_ahash_digestsize(rtfm);
  2030. unsigned int bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
  2031. chcr_sha_init(areq);
  2032. req_ctx->data_len = bs;
  2033. if (is_hmac(crypto_ahash_tfm(rtfm))) {
  2034. if (digestsize == SHA224_DIGEST_SIZE)
  2035. memcpy(req_ctx->partial_hash, hmacctx->ipad,
  2036. SHA256_DIGEST_SIZE);
  2037. else if (digestsize == SHA384_DIGEST_SIZE)
  2038. memcpy(req_ctx->partial_hash, hmacctx->ipad,
  2039. SHA512_DIGEST_SIZE);
  2040. else
  2041. memcpy(req_ctx->partial_hash, hmacctx->ipad,
  2042. digestsize);
  2043. }
  2044. return 0;
  2045. }
  2046. static int chcr_hmac_cra_init(struct crypto_tfm *tfm)
  2047. {
  2048. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  2049. sizeof(struct chcr_ahash_req_ctx));
  2050. return chcr_device_init(crypto_tfm_ctx(tfm));
  2051. }
  2052. inline void chcr_aead_common_exit(struct aead_request *req)
  2053. {
  2054. struct chcr_aead_reqctx *reqctx = aead_request_ctx_dma(req);
  2055. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  2056. struct uld_ctx *u_ctx = ULD_CTX(a_ctx(tfm));
  2057. chcr_aead_dma_unmap(&u_ctx->lldi.pdev->dev, req, reqctx->op);
  2058. }
  2059. static int chcr_aead_common_init(struct aead_request *req)
  2060. {
  2061. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  2062. struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
  2063. struct chcr_aead_reqctx *reqctx = aead_request_ctx_dma(req);
  2064. unsigned int authsize = crypto_aead_authsize(tfm);
  2065. int error = -EINVAL;
  2066. /* validate key size */
  2067. if (aeadctx->enckey_len == 0)
  2068. goto err;
  2069. if (reqctx->op && req->cryptlen < authsize)
  2070. goto err;
  2071. if (reqctx->b0_len)
  2072. reqctx->scratch_pad = reqctx->iv + IV;
  2073. else
  2074. reqctx->scratch_pad = NULL;
  2075. error = chcr_aead_dma_map(&ULD_CTX(a_ctx(tfm))->lldi.pdev->dev, req,
  2076. reqctx->op);
  2077. if (error) {
  2078. error = -ENOMEM;
  2079. goto err;
  2080. }
  2081. return 0;
  2082. err:
  2083. return error;
  2084. }
  2085. static int chcr_aead_need_fallback(struct aead_request *req, int dst_nents,
  2086. int aadmax, int wrlen,
  2087. unsigned short op_type)
  2088. {
  2089. unsigned int authsize = crypto_aead_authsize(crypto_aead_reqtfm(req));
  2090. if (((req->cryptlen - (op_type ? authsize : 0)) == 0) ||
  2091. dst_nents > MAX_DSGL_ENT ||
  2092. (req->assoclen > aadmax) ||
  2093. (wrlen > SGE_MAX_WR_LEN))
  2094. return 1;
  2095. return 0;
  2096. }
  2097. static int chcr_aead_fallback(struct aead_request *req, unsigned short op_type)
  2098. {
  2099. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  2100. struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
  2101. struct aead_request *subreq = aead_request_ctx_dma(req);
  2102. aead_request_set_tfm(subreq, aeadctx->sw_cipher);
  2103. aead_request_set_callback(subreq, req->base.flags,
  2104. req->base.complete, req->base.data);
  2105. aead_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
  2106. req->iv);
  2107. aead_request_set_ad(subreq, req->assoclen);
  2108. return op_type ? crypto_aead_decrypt(subreq) :
  2109. crypto_aead_encrypt(subreq);
  2110. }
  2111. static struct sk_buff *create_authenc_wr(struct aead_request *req,
  2112. unsigned short qid,
  2113. int size)
  2114. {
  2115. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  2116. struct chcr_context *ctx = a_ctx(tfm);
  2117. struct uld_ctx *u_ctx = ULD_CTX(ctx);
  2118. struct chcr_aead_ctx *aeadctx = AEAD_CTX(ctx);
  2119. struct chcr_authenc_ctx *actx = AUTHENC_CTX(aeadctx);
  2120. struct chcr_aead_reqctx *reqctx = aead_request_ctx_dma(req);
  2121. struct sk_buff *skb = NULL;
  2122. struct chcr_wr *chcr_req;
  2123. struct cpl_rx_phys_dsgl *phys_cpl;
  2124. struct ulptx_sgl *ulptx;
  2125. unsigned int transhdr_len;
  2126. unsigned int dst_size = 0, temp, subtype = get_aead_subtype(tfm);
  2127. unsigned int kctx_len = 0, dnents, snents;
  2128. unsigned int authsize = crypto_aead_authsize(tfm);
  2129. int error = -EINVAL;
  2130. u8 *ivptr;
  2131. int null = 0;
  2132. gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
  2133. GFP_ATOMIC;
  2134. struct adapter *adap = padap(ctx->dev);
  2135. unsigned int rx_channel_id = reqctx->rxqidx / ctx->rxq_perchan;
  2136. rx_channel_id = cxgb4_port_e2cchan(u_ctx->lldi.ports[rx_channel_id]);
  2137. if (req->cryptlen == 0)
  2138. return NULL;
  2139. reqctx->b0_len = 0;
  2140. error = chcr_aead_common_init(req);
  2141. if (error)
  2142. return ERR_PTR(error);
  2143. if (subtype == CRYPTO_ALG_SUB_TYPE_CBC_NULL ||
  2144. subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL) {
  2145. null = 1;
  2146. }
  2147. dnents = sg_nents_xlen(req->dst, req->assoclen + req->cryptlen +
  2148. (reqctx->op ? -authsize : authsize), CHCR_DST_SG_SIZE, 0);
  2149. dnents += MIN_AUTH_SG; // For IV
  2150. snents = sg_nents_xlen(req->src, req->assoclen + req->cryptlen,
  2151. CHCR_SRC_SG_SIZE, 0);
  2152. dst_size = get_space_for_phys_dsgl(dnents);
  2153. kctx_len = (KEY_CONTEXT_CTX_LEN_G(ntohl(aeadctx->key_ctx_hdr)) << 4)
  2154. - sizeof(chcr_req->key_ctx);
  2155. transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, dst_size);
  2156. reqctx->imm = (transhdr_len + req->assoclen + req->cryptlen) <
  2157. SGE_MAX_WR_LEN;
  2158. temp = reqctx->imm ? roundup(req->assoclen + req->cryptlen, 16)
  2159. : (sgl_len(snents) * 8);
  2160. transhdr_len += temp;
  2161. transhdr_len = roundup(transhdr_len, 16);
  2162. if (chcr_aead_need_fallback(req, dnents, T6_MAX_AAD_SIZE,
  2163. transhdr_len, reqctx->op)) {
  2164. atomic_inc(&adap->chcr_stats.fallback);
  2165. chcr_aead_common_exit(req);
  2166. return ERR_PTR(chcr_aead_fallback(req, reqctx->op));
  2167. }
  2168. skb = alloc_skb(transhdr_len, flags);
  2169. if (!skb) {
  2170. error = -ENOMEM;
  2171. goto err;
  2172. }
  2173. chcr_req = __skb_put_zero(skb, transhdr_len);
  2174. temp = (reqctx->op == CHCR_ENCRYPT_OP) ? 0 : authsize;
  2175. /*
  2176. * Input order is AAD,IV and Payload. where IV should be included as
  2177. * the part of authdata. All other fields should be filled according
  2178. * to the hardware spec
  2179. */
  2180. chcr_req->sec_cpl.op_ivinsrtofst =
  2181. FILL_SEC_CPL_OP_IVINSR(rx_channel_id, 2, 1);
  2182. chcr_req->sec_cpl.pldlen = htonl(req->assoclen + IV + req->cryptlen);
  2183. chcr_req->sec_cpl.aadstart_cipherstop_hi = FILL_SEC_CPL_CIPHERSTOP_HI(
  2184. null ? 0 : 1 + IV,
  2185. null ? 0 : IV + req->assoclen,
  2186. req->assoclen + IV + 1,
  2187. (temp & 0x1F0) >> 4);
  2188. chcr_req->sec_cpl.cipherstop_lo_authinsert = FILL_SEC_CPL_AUTHINSERT(
  2189. temp & 0xF,
  2190. null ? 0 : req->assoclen + IV + 1,
  2191. temp, temp);
  2192. if (subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL ||
  2193. subtype == CRYPTO_ALG_SUB_TYPE_CTR_SHA)
  2194. temp = CHCR_SCMD_CIPHER_MODE_AES_CTR;
  2195. else
  2196. temp = CHCR_SCMD_CIPHER_MODE_AES_CBC;
  2197. chcr_req->sec_cpl.seqno_numivs = FILL_SEC_CPL_SCMD0_SEQNO(reqctx->op,
  2198. (reqctx->op == CHCR_ENCRYPT_OP) ? 1 : 0,
  2199. temp,
  2200. actx->auth_mode, aeadctx->hmac_ctrl,
  2201. IV >> 1);
  2202. chcr_req->sec_cpl.ivgen_hdrlen = FILL_SEC_CPL_IVGEN_HDRLEN(0, 0, 1,
  2203. 0, 0, dst_size);
  2204. chcr_req->key_ctx.ctx_hdr = aeadctx->key_ctx_hdr;
  2205. if (reqctx->op == CHCR_ENCRYPT_OP ||
  2206. subtype == CRYPTO_ALG_SUB_TYPE_CTR_SHA ||
  2207. subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL)
  2208. memcpy(chcr_req->key_ctx.key, aeadctx->key,
  2209. aeadctx->enckey_len);
  2210. else
  2211. memcpy(chcr_req->key_ctx.key, actx->dec_rrkey,
  2212. aeadctx->enckey_len);
  2213. memcpy(chcr_req->key_ctx.key + roundup(aeadctx->enckey_len, 16),
  2214. actx->h_iopad, kctx_len - roundup(aeadctx->enckey_len, 16));
  2215. phys_cpl = (struct cpl_rx_phys_dsgl *)((u8 *)(chcr_req + 1) + kctx_len);
  2216. ivptr = (u8 *)(phys_cpl + 1) + dst_size;
  2217. ulptx = (struct ulptx_sgl *)(ivptr + IV);
  2218. if (subtype == CRYPTO_ALG_SUB_TYPE_CTR_SHA ||
  2219. subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL) {
  2220. memcpy(ivptr, aeadctx->nonce, CTR_RFC3686_NONCE_SIZE);
  2221. memcpy(ivptr + CTR_RFC3686_NONCE_SIZE, req->iv,
  2222. CTR_RFC3686_IV_SIZE);
  2223. *(__be32 *)(ivptr + CTR_RFC3686_NONCE_SIZE +
  2224. CTR_RFC3686_IV_SIZE) = cpu_to_be32(1);
  2225. } else {
  2226. memcpy(ivptr, req->iv, IV);
  2227. }
  2228. chcr_add_aead_dst_ent(req, phys_cpl, qid);
  2229. chcr_add_aead_src_ent(req, ulptx);
  2230. atomic_inc(&adap->chcr_stats.cipher_rqst);
  2231. temp = sizeof(struct cpl_rx_phys_dsgl) + dst_size + IV +
  2232. kctx_len + (reqctx->imm ? (req->assoclen + req->cryptlen) : 0);
  2233. create_wreq(a_ctx(tfm), chcr_req, &req->base, reqctx->imm, size,
  2234. transhdr_len, temp, 0);
  2235. reqctx->skb = skb;
  2236. return skb;
  2237. err:
  2238. chcr_aead_common_exit(req);
  2239. return ERR_PTR(error);
  2240. }
  2241. int chcr_aead_dma_map(struct device *dev,
  2242. struct aead_request *req,
  2243. unsigned short op_type)
  2244. {
  2245. int error;
  2246. struct chcr_aead_reqctx *reqctx = aead_request_ctx_dma(req);
  2247. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  2248. unsigned int authsize = crypto_aead_authsize(tfm);
  2249. int src_len, dst_len;
  2250. /* calculate and handle src and dst sg length separately
  2251. * for inplace and out-of place operations
  2252. */
  2253. if (req->src == req->dst) {
  2254. src_len = req->assoclen + req->cryptlen + (op_type ?
  2255. 0 : authsize);
  2256. dst_len = src_len;
  2257. } else {
  2258. src_len = req->assoclen + req->cryptlen;
  2259. dst_len = req->assoclen + req->cryptlen + (op_type ?
  2260. -authsize : authsize);
  2261. }
  2262. if (!req->cryptlen || !src_len || !dst_len)
  2263. return 0;
  2264. reqctx->iv_dma = dma_map_single(dev, reqctx->iv, (IV + reqctx->b0_len),
  2265. DMA_BIDIRECTIONAL);
  2266. if (dma_mapping_error(dev, reqctx->iv_dma))
  2267. return -ENOMEM;
  2268. if (reqctx->b0_len)
  2269. reqctx->b0_dma = reqctx->iv_dma + IV;
  2270. else
  2271. reqctx->b0_dma = 0;
  2272. if (req->src == req->dst) {
  2273. error = dma_map_sg(dev, req->src,
  2274. sg_nents_for_len(req->src, src_len),
  2275. DMA_BIDIRECTIONAL);
  2276. if (!error)
  2277. goto err;
  2278. } else {
  2279. error = dma_map_sg(dev, req->src,
  2280. sg_nents_for_len(req->src, src_len),
  2281. DMA_TO_DEVICE);
  2282. if (!error)
  2283. goto err;
  2284. error = dma_map_sg(dev, req->dst,
  2285. sg_nents_for_len(req->dst, dst_len),
  2286. DMA_FROM_DEVICE);
  2287. if (!error) {
  2288. dma_unmap_sg(dev, req->src,
  2289. sg_nents_for_len(req->src, src_len),
  2290. DMA_TO_DEVICE);
  2291. goto err;
  2292. }
  2293. }
  2294. return 0;
  2295. err:
  2296. dma_unmap_single(dev, reqctx->iv_dma, IV, DMA_BIDIRECTIONAL);
  2297. return -ENOMEM;
  2298. }
  2299. void chcr_aead_dma_unmap(struct device *dev,
  2300. struct aead_request *req,
  2301. unsigned short op_type)
  2302. {
  2303. struct chcr_aead_reqctx *reqctx = aead_request_ctx_dma(req);
  2304. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  2305. unsigned int authsize = crypto_aead_authsize(tfm);
  2306. int src_len, dst_len;
  2307. /* calculate and handle src and dst sg length separately
  2308. * for inplace and out-of place operations
  2309. */
  2310. if (req->src == req->dst) {
  2311. src_len = req->assoclen + req->cryptlen + (op_type ?
  2312. 0 : authsize);
  2313. dst_len = src_len;
  2314. } else {
  2315. src_len = req->assoclen + req->cryptlen;
  2316. dst_len = req->assoclen + req->cryptlen + (op_type ?
  2317. -authsize : authsize);
  2318. }
  2319. if (!req->cryptlen || !src_len || !dst_len)
  2320. return;
  2321. dma_unmap_single(dev, reqctx->iv_dma, (IV + reqctx->b0_len),
  2322. DMA_BIDIRECTIONAL);
  2323. if (req->src == req->dst) {
  2324. dma_unmap_sg(dev, req->src,
  2325. sg_nents_for_len(req->src, src_len),
  2326. DMA_BIDIRECTIONAL);
  2327. } else {
  2328. dma_unmap_sg(dev, req->src,
  2329. sg_nents_for_len(req->src, src_len),
  2330. DMA_TO_DEVICE);
  2331. dma_unmap_sg(dev, req->dst,
  2332. sg_nents_for_len(req->dst, dst_len),
  2333. DMA_FROM_DEVICE);
  2334. }
  2335. }
  2336. void chcr_add_aead_src_ent(struct aead_request *req,
  2337. struct ulptx_sgl *ulptx)
  2338. {
  2339. struct ulptx_walk ulp_walk;
  2340. struct chcr_aead_reqctx *reqctx = aead_request_ctx_dma(req);
  2341. if (reqctx->imm) {
  2342. u8 *buf = (u8 *)ulptx;
  2343. if (reqctx->b0_len) {
  2344. memcpy(buf, reqctx->scratch_pad, reqctx->b0_len);
  2345. buf += reqctx->b0_len;
  2346. }
  2347. sg_pcopy_to_buffer(req->src, sg_nents(req->src),
  2348. buf, req->cryptlen + req->assoclen, 0);
  2349. } else {
  2350. ulptx_walk_init(&ulp_walk, ulptx);
  2351. if (reqctx->b0_len)
  2352. ulptx_walk_add_page(&ulp_walk, reqctx->b0_len,
  2353. reqctx->b0_dma);
  2354. ulptx_walk_add_sg(&ulp_walk, req->src, req->cryptlen +
  2355. req->assoclen, 0);
  2356. ulptx_walk_end(&ulp_walk);
  2357. }
  2358. }
  2359. void chcr_add_aead_dst_ent(struct aead_request *req,
  2360. struct cpl_rx_phys_dsgl *phys_cpl,
  2361. unsigned short qid)
  2362. {
  2363. struct chcr_aead_reqctx *reqctx = aead_request_ctx_dma(req);
  2364. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  2365. struct dsgl_walk dsgl_walk;
  2366. unsigned int authsize = crypto_aead_authsize(tfm);
  2367. struct chcr_context *ctx = a_ctx(tfm);
  2368. struct uld_ctx *u_ctx = ULD_CTX(ctx);
  2369. u32 temp;
  2370. unsigned int rx_channel_id = reqctx->rxqidx / ctx->rxq_perchan;
  2371. rx_channel_id = cxgb4_port_e2cchan(u_ctx->lldi.ports[rx_channel_id]);
  2372. dsgl_walk_init(&dsgl_walk, phys_cpl);
  2373. dsgl_walk_add_page(&dsgl_walk, IV + reqctx->b0_len, reqctx->iv_dma);
  2374. temp = req->assoclen + req->cryptlen +
  2375. (reqctx->op ? -authsize : authsize);
  2376. dsgl_walk_add_sg(&dsgl_walk, req->dst, temp, 0);
  2377. dsgl_walk_end(&dsgl_walk, qid, rx_channel_id);
  2378. }
  2379. void chcr_add_cipher_src_ent(struct skcipher_request *req,
  2380. void *ulptx,
  2381. struct cipher_wr_param *wrparam)
  2382. {
  2383. struct ulptx_walk ulp_walk;
  2384. struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req);
  2385. u8 *buf = ulptx;
  2386. memcpy(buf, reqctx->iv, IV);
  2387. buf += IV;
  2388. if (reqctx->imm) {
  2389. sg_pcopy_to_buffer(req->src, sg_nents(req->src),
  2390. buf, wrparam->bytes, reqctx->processed);
  2391. } else {
  2392. ulptx_walk_init(&ulp_walk, (struct ulptx_sgl *)buf);
  2393. ulptx_walk_add_sg(&ulp_walk, reqctx->srcsg, wrparam->bytes,
  2394. reqctx->src_ofst);
  2395. reqctx->srcsg = ulp_walk.last_sg;
  2396. reqctx->src_ofst = ulp_walk.last_sg_len;
  2397. ulptx_walk_end(&ulp_walk);
  2398. }
  2399. }
  2400. void chcr_add_cipher_dst_ent(struct skcipher_request *req,
  2401. struct cpl_rx_phys_dsgl *phys_cpl,
  2402. struct cipher_wr_param *wrparam,
  2403. unsigned short qid)
  2404. {
  2405. struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req);
  2406. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(wrparam->req);
  2407. struct chcr_context *ctx = c_ctx(tfm);
  2408. struct uld_ctx *u_ctx = ULD_CTX(ctx);
  2409. struct dsgl_walk dsgl_walk;
  2410. unsigned int rx_channel_id = reqctx->rxqidx / ctx->rxq_perchan;
  2411. rx_channel_id = cxgb4_port_e2cchan(u_ctx->lldi.ports[rx_channel_id]);
  2412. dsgl_walk_init(&dsgl_walk, phys_cpl);
  2413. dsgl_walk_add_sg(&dsgl_walk, reqctx->dstsg, wrparam->bytes,
  2414. reqctx->dst_ofst);
  2415. reqctx->dstsg = dsgl_walk.last_sg;
  2416. reqctx->dst_ofst = dsgl_walk.last_sg_len;
  2417. dsgl_walk_end(&dsgl_walk, qid, rx_channel_id);
  2418. }
  2419. void chcr_add_hash_src_ent(struct ahash_request *req,
  2420. struct ulptx_sgl *ulptx,
  2421. struct hash_wr_param *param)
  2422. {
  2423. struct ulptx_walk ulp_walk;
  2424. struct chcr_ahash_req_ctx *reqctx = ahash_request_ctx(req);
  2425. if (reqctx->hctx_wr.imm) {
  2426. u8 *buf = (u8 *)ulptx;
  2427. if (param->bfr_len) {
  2428. memcpy(buf, reqctx->reqbfr, param->bfr_len);
  2429. buf += param->bfr_len;
  2430. }
  2431. sg_pcopy_to_buffer(reqctx->hctx_wr.srcsg,
  2432. sg_nents(reqctx->hctx_wr.srcsg), buf,
  2433. param->sg_len, 0);
  2434. } else {
  2435. ulptx_walk_init(&ulp_walk, ulptx);
  2436. if (param->bfr_len)
  2437. ulptx_walk_add_page(&ulp_walk, param->bfr_len,
  2438. reqctx->hctx_wr.dma_addr);
  2439. ulptx_walk_add_sg(&ulp_walk, reqctx->hctx_wr.srcsg,
  2440. param->sg_len, reqctx->hctx_wr.src_ofst);
  2441. reqctx->hctx_wr.srcsg = ulp_walk.last_sg;
  2442. reqctx->hctx_wr.src_ofst = ulp_walk.last_sg_len;
  2443. ulptx_walk_end(&ulp_walk);
  2444. }
  2445. }
  2446. int chcr_hash_dma_map(struct device *dev,
  2447. struct ahash_request *req)
  2448. {
  2449. struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
  2450. int error = 0;
  2451. if (!req->nbytes)
  2452. return 0;
  2453. error = dma_map_sg(dev, req->src, sg_nents(req->src),
  2454. DMA_TO_DEVICE);
  2455. if (!error)
  2456. return -ENOMEM;
  2457. req_ctx->hctx_wr.is_sg_map = 1;
  2458. return 0;
  2459. }
  2460. void chcr_hash_dma_unmap(struct device *dev,
  2461. struct ahash_request *req)
  2462. {
  2463. struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
  2464. if (!req->nbytes)
  2465. return;
  2466. dma_unmap_sg(dev, req->src, sg_nents(req->src),
  2467. DMA_TO_DEVICE);
  2468. req_ctx->hctx_wr.is_sg_map = 0;
  2469. }
  2470. int chcr_cipher_dma_map(struct device *dev,
  2471. struct skcipher_request *req)
  2472. {
  2473. int error;
  2474. if (req->src == req->dst) {
  2475. error = dma_map_sg(dev, req->src, sg_nents(req->src),
  2476. DMA_BIDIRECTIONAL);
  2477. if (!error)
  2478. goto err;
  2479. } else {
  2480. error = dma_map_sg(dev, req->src, sg_nents(req->src),
  2481. DMA_TO_DEVICE);
  2482. if (!error)
  2483. goto err;
  2484. error = dma_map_sg(dev, req->dst, sg_nents(req->dst),
  2485. DMA_FROM_DEVICE);
  2486. if (!error) {
  2487. dma_unmap_sg(dev, req->src, sg_nents(req->src),
  2488. DMA_TO_DEVICE);
  2489. goto err;
  2490. }
  2491. }
  2492. return 0;
  2493. err:
  2494. return -ENOMEM;
  2495. }
  2496. void chcr_cipher_dma_unmap(struct device *dev,
  2497. struct skcipher_request *req)
  2498. {
  2499. if (req->src == req->dst) {
  2500. dma_unmap_sg(dev, req->src, sg_nents(req->src),
  2501. DMA_BIDIRECTIONAL);
  2502. } else {
  2503. dma_unmap_sg(dev, req->src, sg_nents(req->src),
  2504. DMA_TO_DEVICE);
  2505. dma_unmap_sg(dev, req->dst, sg_nents(req->dst),
  2506. DMA_FROM_DEVICE);
  2507. }
  2508. }
  2509. static int set_msg_len(u8 *block, unsigned int msglen, int csize)
  2510. {
  2511. __be32 data;
  2512. memset(block, 0, csize);
  2513. block += csize;
  2514. if (csize >= 4)
  2515. csize = 4;
  2516. else if (msglen > (unsigned int)(1 << (8 * csize)))
  2517. return -EOVERFLOW;
  2518. data = cpu_to_be32(msglen);
  2519. memcpy(block - csize, (u8 *)&data + 4 - csize, csize);
  2520. return 0;
  2521. }
  2522. static int generate_b0(struct aead_request *req, u8 *ivptr,
  2523. unsigned short op_type)
  2524. {
  2525. unsigned int l, lp, m;
  2526. int rc;
  2527. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  2528. struct chcr_aead_reqctx *reqctx = aead_request_ctx_dma(req);
  2529. u8 *b0 = reqctx->scratch_pad;
  2530. m = crypto_aead_authsize(aead);
  2531. memcpy(b0, ivptr, 16);
  2532. lp = b0[0];
  2533. l = lp + 1;
  2534. /* set m, bits 3-5 */
  2535. *b0 |= (8 * ((m - 2) / 2));
  2536. /* set adata, bit 6, if associated data is used */
  2537. if (req->assoclen)
  2538. *b0 |= 64;
  2539. rc = set_msg_len(b0 + 16 - l,
  2540. (op_type == CHCR_DECRYPT_OP) ?
  2541. req->cryptlen - m : req->cryptlen, l);
  2542. return rc;
  2543. }
  2544. static inline int crypto_ccm_check_iv(const u8 *iv)
  2545. {
  2546. /* 2 <= L <= 8, so 1 <= L' <= 7. */
  2547. if (iv[0] < 1 || iv[0] > 7)
  2548. return -EINVAL;
  2549. return 0;
  2550. }
  2551. static int ccm_format_packet(struct aead_request *req,
  2552. u8 *ivptr,
  2553. unsigned int sub_type,
  2554. unsigned short op_type,
  2555. unsigned int assoclen)
  2556. {
  2557. struct chcr_aead_reqctx *reqctx = aead_request_ctx_dma(req);
  2558. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  2559. struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
  2560. int rc = 0;
  2561. if (sub_type == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309) {
  2562. ivptr[0] = 3;
  2563. memcpy(ivptr + 1, &aeadctx->salt[0], 3);
  2564. memcpy(ivptr + 4, req->iv, 8);
  2565. memset(ivptr + 12, 0, 4);
  2566. } else {
  2567. memcpy(ivptr, req->iv, 16);
  2568. }
  2569. if (assoclen)
  2570. put_unaligned_be16(assoclen, &reqctx->scratch_pad[16]);
  2571. rc = generate_b0(req, ivptr, op_type);
  2572. /* zero the ctr value */
  2573. memset(ivptr + 15 - ivptr[0], 0, ivptr[0] + 1);
  2574. return rc;
  2575. }
  2576. static void fill_sec_cpl_for_aead(struct cpl_tx_sec_pdu *sec_cpl,
  2577. unsigned int dst_size,
  2578. struct aead_request *req,
  2579. unsigned short op_type)
  2580. {
  2581. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  2582. struct chcr_context *ctx = a_ctx(tfm);
  2583. struct uld_ctx *u_ctx = ULD_CTX(ctx);
  2584. struct chcr_aead_ctx *aeadctx = AEAD_CTX(ctx);
  2585. struct chcr_aead_reqctx *reqctx = aead_request_ctx_dma(req);
  2586. unsigned int cipher_mode = CHCR_SCMD_CIPHER_MODE_AES_CCM;
  2587. unsigned int mac_mode = CHCR_SCMD_AUTH_MODE_CBCMAC;
  2588. unsigned int rx_channel_id = reqctx->rxqidx / ctx->rxq_perchan;
  2589. unsigned int ccm_xtra;
  2590. unsigned int tag_offset = 0, auth_offset = 0;
  2591. unsigned int assoclen;
  2592. rx_channel_id = cxgb4_port_e2cchan(u_ctx->lldi.ports[rx_channel_id]);
  2593. if (get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309)
  2594. assoclen = req->assoclen - 8;
  2595. else
  2596. assoclen = req->assoclen;
  2597. ccm_xtra = CCM_B0_SIZE +
  2598. ((assoclen) ? CCM_AAD_FIELD_SIZE : 0);
  2599. auth_offset = req->cryptlen ?
  2600. (req->assoclen + IV + 1 + ccm_xtra) : 0;
  2601. if (op_type == CHCR_DECRYPT_OP) {
  2602. if (crypto_aead_authsize(tfm) != req->cryptlen)
  2603. tag_offset = crypto_aead_authsize(tfm);
  2604. else
  2605. auth_offset = 0;
  2606. }
  2607. sec_cpl->op_ivinsrtofst = FILL_SEC_CPL_OP_IVINSR(rx_channel_id, 2, 1);
  2608. sec_cpl->pldlen =
  2609. htonl(req->assoclen + IV + req->cryptlen + ccm_xtra);
  2610. /* For CCM there wil be b0 always. So AAD start will be 1 always */
  2611. sec_cpl->aadstart_cipherstop_hi = FILL_SEC_CPL_CIPHERSTOP_HI(
  2612. 1 + IV, IV + assoclen + ccm_xtra,
  2613. req->assoclen + IV + 1 + ccm_xtra, 0);
  2614. sec_cpl->cipherstop_lo_authinsert = FILL_SEC_CPL_AUTHINSERT(0,
  2615. auth_offset, tag_offset,
  2616. (op_type == CHCR_ENCRYPT_OP) ? 0 :
  2617. crypto_aead_authsize(tfm));
  2618. sec_cpl->seqno_numivs = FILL_SEC_CPL_SCMD0_SEQNO(op_type,
  2619. (op_type == CHCR_ENCRYPT_OP) ? 0 : 1,
  2620. cipher_mode, mac_mode,
  2621. aeadctx->hmac_ctrl, IV >> 1);
  2622. sec_cpl->ivgen_hdrlen = FILL_SEC_CPL_IVGEN_HDRLEN(0, 0, 1, 0,
  2623. 0, dst_size);
  2624. }
  2625. static int aead_ccm_validate_input(unsigned short op_type,
  2626. struct aead_request *req,
  2627. struct chcr_aead_ctx *aeadctx,
  2628. unsigned int sub_type)
  2629. {
  2630. if (sub_type != CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309) {
  2631. if (crypto_ccm_check_iv(req->iv)) {
  2632. pr_err("CCM: IV check fails\n");
  2633. return -EINVAL;
  2634. }
  2635. } else {
  2636. if (req->assoclen != 16 && req->assoclen != 20) {
  2637. pr_err("RFC4309: Invalid AAD length %d\n",
  2638. req->assoclen);
  2639. return -EINVAL;
  2640. }
  2641. }
  2642. return 0;
  2643. }
  2644. static struct sk_buff *create_aead_ccm_wr(struct aead_request *req,
  2645. unsigned short qid,
  2646. int size)
  2647. {
  2648. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  2649. struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
  2650. struct chcr_aead_reqctx *reqctx = aead_request_ctx_dma(req);
  2651. struct sk_buff *skb = NULL;
  2652. struct chcr_wr *chcr_req;
  2653. struct cpl_rx_phys_dsgl *phys_cpl;
  2654. struct ulptx_sgl *ulptx;
  2655. unsigned int transhdr_len;
  2656. unsigned int dst_size = 0, kctx_len, dnents, temp, snents;
  2657. unsigned int sub_type, assoclen = req->assoclen;
  2658. unsigned int authsize = crypto_aead_authsize(tfm);
  2659. int error = -EINVAL;
  2660. u8 *ivptr;
  2661. gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
  2662. GFP_ATOMIC;
  2663. struct adapter *adap = padap(a_ctx(tfm)->dev);
  2664. sub_type = get_aead_subtype(tfm);
  2665. if (sub_type == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309)
  2666. assoclen -= 8;
  2667. reqctx->b0_len = CCM_B0_SIZE + (assoclen ? CCM_AAD_FIELD_SIZE : 0);
  2668. error = chcr_aead_common_init(req);
  2669. if (error)
  2670. return ERR_PTR(error);
  2671. error = aead_ccm_validate_input(reqctx->op, req, aeadctx, sub_type);
  2672. if (error)
  2673. goto err;
  2674. dnents = sg_nents_xlen(req->dst, req->assoclen + req->cryptlen
  2675. + (reqctx->op ? -authsize : authsize),
  2676. CHCR_DST_SG_SIZE, 0);
  2677. dnents += MIN_CCM_SG; // For IV and B0
  2678. dst_size = get_space_for_phys_dsgl(dnents);
  2679. snents = sg_nents_xlen(req->src, req->assoclen + req->cryptlen,
  2680. CHCR_SRC_SG_SIZE, 0);
  2681. snents += MIN_CCM_SG; //For B0
  2682. kctx_len = roundup(aeadctx->enckey_len, 16) * 2;
  2683. transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, dst_size);
  2684. reqctx->imm = (transhdr_len + req->assoclen + req->cryptlen +
  2685. reqctx->b0_len) <= SGE_MAX_WR_LEN;
  2686. temp = reqctx->imm ? roundup(req->assoclen + req->cryptlen +
  2687. reqctx->b0_len, 16) :
  2688. (sgl_len(snents) * 8);
  2689. transhdr_len += temp;
  2690. transhdr_len = roundup(transhdr_len, 16);
  2691. if (chcr_aead_need_fallback(req, dnents, T6_MAX_AAD_SIZE -
  2692. reqctx->b0_len, transhdr_len, reqctx->op)) {
  2693. atomic_inc(&adap->chcr_stats.fallback);
  2694. chcr_aead_common_exit(req);
  2695. return ERR_PTR(chcr_aead_fallback(req, reqctx->op));
  2696. }
  2697. skb = alloc_skb(transhdr_len, flags);
  2698. if (!skb) {
  2699. error = -ENOMEM;
  2700. goto err;
  2701. }
  2702. chcr_req = __skb_put_zero(skb, transhdr_len);
  2703. fill_sec_cpl_for_aead(&chcr_req->sec_cpl, dst_size, req, reqctx->op);
  2704. chcr_req->key_ctx.ctx_hdr = aeadctx->key_ctx_hdr;
  2705. memcpy(chcr_req->key_ctx.key, aeadctx->key, aeadctx->enckey_len);
  2706. memcpy(chcr_req->key_ctx.key + roundup(aeadctx->enckey_len, 16),
  2707. aeadctx->key, aeadctx->enckey_len);
  2708. phys_cpl = (struct cpl_rx_phys_dsgl *)((u8 *)(chcr_req + 1) + kctx_len);
  2709. ivptr = (u8 *)(phys_cpl + 1) + dst_size;
  2710. ulptx = (struct ulptx_sgl *)(ivptr + IV);
  2711. error = ccm_format_packet(req, ivptr, sub_type, reqctx->op, assoclen);
  2712. if (error)
  2713. goto dstmap_fail;
  2714. chcr_add_aead_dst_ent(req, phys_cpl, qid);
  2715. chcr_add_aead_src_ent(req, ulptx);
  2716. atomic_inc(&adap->chcr_stats.aead_rqst);
  2717. temp = sizeof(struct cpl_rx_phys_dsgl) + dst_size + IV +
  2718. kctx_len + (reqctx->imm ? (req->assoclen + req->cryptlen +
  2719. reqctx->b0_len) : 0);
  2720. create_wreq(a_ctx(tfm), chcr_req, &req->base, reqctx->imm, 0,
  2721. transhdr_len, temp, 0);
  2722. reqctx->skb = skb;
  2723. return skb;
  2724. dstmap_fail:
  2725. kfree_skb(skb);
  2726. err:
  2727. chcr_aead_common_exit(req);
  2728. return ERR_PTR(error);
  2729. }
  2730. static struct sk_buff *create_gcm_wr(struct aead_request *req,
  2731. unsigned short qid,
  2732. int size)
  2733. {
  2734. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  2735. struct chcr_context *ctx = a_ctx(tfm);
  2736. struct uld_ctx *u_ctx = ULD_CTX(ctx);
  2737. struct chcr_aead_ctx *aeadctx = AEAD_CTX(ctx);
  2738. struct chcr_aead_reqctx *reqctx = aead_request_ctx_dma(req);
  2739. struct sk_buff *skb = NULL;
  2740. struct chcr_wr *chcr_req;
  2741. struct cpl_rx_phys_dsgl *phys_cpl;
  2742. struct ulptx_sgl *ulptx;
  2743. unsigned int transhdr_len, dnents = 0, snents;
  2744. unsigned int dst_size = 0, temp = 0, kctx_len, assoclen = req->assoclen;
  2745. unsigned int authsize = crypto_aead_authsize(tfm);
  2746. int error = -EINVAL;
  2747. u8 *ivptr;
  2748. gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
  2749. GFP_ATOMIC;
  2750. struct adapter *adap = padap(ctx->dev);
  2751. unsigned int rx_channel_id = reqctx->rxqidx / ctx->rxq_perchan;
  2752. rx_channel_id = cxgb4_port_e2cchan(u_ctx->lldi.ports[rx_channel_id]);
  2753. if (get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106)
  2754. assoclen = req->assoclen - 8;
  2755. reqctx->b0_len = 0;
  2756. error = chcr_aead_common_init(req);
  2757. if (error)
  2758. return ERR_PTR(error);
  2759. dnents = sg_nents_xlen(req->dst, req->assoclen + req->cryptlen +
  2760. (reqctx->op ? -authsize : authsize),
  2761. CHCR_DST_SG_SIZE, 0);
  2762. snents = sg_nents_xlen(req->src, req->assoclen + req->cryptlen,
  2763. CHCR_SRC_SG_SIZE, 0);
  2764. dnents += MIN_GCM_SG; // For IV
  2765. dst_size = get_space_for_phys_dsgl(dnents);
  2766. kctx_len = roundup(aeadctx->enckey_len, 16) + AEAD_H_SIZE;
  2767. transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, dst_size);
  2768. reqctx->imm = (transhdr_len + req->assoclen + req->cryptlen) <=
  2769. SGE_MAX_WR_LEN;
  2770. temp = reqctx->imm ? roundup(req->assoclen + req->cryptlen, 16) :
  2771. (sgl_len(snents) * 8);
  2772. transhdr_len += temp;
  2773. transhdr_len = roundup(transhdr_len, 16);
  2774. if (chcr_aead_need_fallback(req, dnents, T6_MAX_AAD_SIZE,
  2775. transhdr_len, reqctx->op)) {
  2776. atomic_inc(&adap->chcr_stats.fallback);
  2777. chcr_aead_common_exit(req);
  2778. return ERR_PTR(chcr_aead_fallback(req, reqctx->op));
  2779. }
  2780. skb = alloc_skb(transhdr_len, flags);
  2781. if (!skb) {
  2782. error = -ENOMEM;
  2783. goto err;
  2784. }
  2785. chcr_req = __skb_put_zero(skb, transhdr_len);
  2786. //Offset of tag from end
  2787. temp = (reqctx->op == CHCR_ENCRYPT_OP) ? 0 : authsize;
  2788. chcr_req->sec_cpl.op_ivinsrtofst = FILL_SEC_CPL_OP_IVINSR(
  2789. rx_channel_id, 2, 1);
  2790. chcr_req->sec_cpl.pldlen =
  2791. htonl(req->assoclen + IV + req->cryptlen);
  2792. chcr_req->sec_cpl.aadstart_cipherstop_hi = FILL_SEC_CPL_CIPHERSTOP_HI(
  2793. assoclen ? 1 + IV : 0,
  2794. assoclen ? IV + assoclen : 0,
  2795. req->assoclen + IV + 1, 0);
  2796. chcr_req->sec_cpl.cipherstop_lo_authinsert =
  2797. FILL_SEC_CPL_AUTHINSERT(0, req->assoclen + IV + 1,
  2798. temp, temp);
  2799. chcr_req->sec_cpl.seqno_numivs =
  2800. FILL_SEC_CPL_SCMD0_SEQNO(reqctx->op, (reqctx->op ==
  2801. CHCR_ENCRYPT_OP) ? 1 : 0,
  2802. CHCR_SCMD_CIPHER_MODE_AES_GCM,
  2803. CHCR_SCMD_AUTH_MODE_GHASH,
  2804. aeadctx->hmac_ctrl, IV >> 1);
  2805. chcr_req->sec_cpl.ivgen_hdrlen = FILL_SEC_CPL_IVGEN_HDRLEN(0, 0, 1,
  2806. 0, 0, dst_size);
  2807. chcr_req->key_ctx.ctx_hdr = aeadctx->key_ctx_hdr;
  2808. memcpy(chcr_req->key_ctx.key, aeadctx->key, aeadctx->enckey_len);
  2809. memcpy(chcr_req->key_ctx.key + roundup(aeadctx->enckey_len, 16),
  2810. GCM_CTX(aeadctx)->ghash_h, AEAD_H_SIZE);
  2811. phys_cpl = (struct cpl_rx_phys_dsgl *)((u8 *)(chcr_req + 1) + kctx_len);
  2812. ivptr = (u8 *)(phys_cpl + 1) + dst_size;
  2813. /* prepare a 16 byte iv */
  2814. /* S A L T | IV | 0x00000001 */
  2815. if (get_aead_subtype(tfm) ==
  2816. CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106) {
  2817. memcpy(ivptr, aeadctx->salt, 4);
  2818. memcpy(ivptr + 4, req->iv, GCM_RFC4106_IV_SIZE);
  2819. } else {
  2820. memcpy(ivptr, req->iv, GCM_AES_IV_SIZE);
  2821. }
  2822. put_unaligned_be32(0x01, &ivptr[12]);
  2823. ulptx = (struct ulptx_sgl *)(ivptr + 16);
  2824. chcr_add_aead_dst_ent(req, phys_cpl, qid);
  2825. chcr_add_aead_src_ent(req, ulptx);
  2826. atomic_inc(&adap->chcr_stats.aead_rqst);
  2827. temp = sizeof(struct cpl_rx_phys_dsgl) + dst_size + IV +
  2828. kctx_len + (reqctx->imm ? (req->assoclen + req->cryptlen) : 0);
  2829. create_wreq(a_ctx(tfm), chcr_req, &req->base, reqctx->imm, size,
  2830. transhdr_len, temp, reqctx->verify);
  2831. reqctx->skb = skb;
  2832. return skb;
  2833. err:
  2834. chcr_aead_common_exit(req);
  2835. return ERR_PTR(error);
  2836. }
  2837. static int chcr_aead_cra_init(struct crypto_aead *tfm)
  2838. {
  2839. struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
  2840. struct aead_alg *alg = crypto_aead_alg(tfm);
  2841. aeadctx->sw_cipher = crypto_alloc_aead(alg->base.cra_name, 0,
  2842. CRYPTO_ALG_NEED_FALLBACK |
  2843. CRYPTO_ALG_ASYNC);
  2844. if (IS_ERR(aeadctx->sw_cipher))
  2845. return PTR_ERR(aeadctx->sw_cipher);
  2846. crypto_aead_set_reqsize_dma(
  2847. tfm, max(sizeof(struct chcr_aead_reqctx),
  2848. sizeof(struct aead_request) +
  2849. crypto_aead_reqsize(aeadctx->sw_cipher)));
  2850. return chcr_device_init(a_ctx(tfm));
  2851. }
  2852. static void chcr_aead_cra_exit(struct crypto_aead *tfm)
  2853. {
  2854. struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
  2855. crypto_free_aead(aeadctx->sw_cipher);
  2856. }
  2857. static int chcr_authenc_null_setauthsize(struct crypto_aead *tfm,
  2858. unsigned int authsize)
  2859. {
  2860. struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
  2861. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NOP;
  2862. aeadctx->mayverify = VERIFY_HW;
  2863. return crypto_aead_setauthsize(aeadctx->sw_cipher, authsize);
  2864. }
  2865. static int chcr_authenc_setauthsize(struct crypto_aead *tfm,
  2866. unsigned int authsize)
  2867. {
  2868. struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
  2869. u32 maxauth = crypto_aead_maxauthsize(tfm);
  2870. /*SHA1 authsize in ipsec is 12 instead of 10 i.e maxauthsize / 2 is not
  2871. * true for sha1. authsize == 12 condition should be before
  2872. * authsize == (maxauth >> 1)
  2873. */
  2874. if (authsize == ICV_4) {
  2875. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL1;
  2876. aeadctx->mayverify = VERIFY_HW;
  2877. } else if (authsize == ICV_6) {
  2878. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL2;
  2879. aeadctx->mayverify = VERIFY_HW;
  2880. } else if (authsize == ICV_10) {
  2881. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_TRUNC_RFC4366;
  2882. aeadctx->mayverify = VERIFY_HW;
  2883. } else if (authsize == ICV_12) {
  2884. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_IPSEC_96BIT;
  2885. aeadctx->mayverify = VERIFY_HW;
  2886. } else if (authsize == ICV_14) {
  2887. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL3;
  2888. aeadctx->mayverify = VERIFY_HW;
  2889. } else if (authsize == (maxauth >> 1)) {
  2890. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_DIV2;
  2891. aeadctx->mayverify = VERIFY_HW;
  2892. } else if (authsize == maxauth) {
  2893. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
  2894. aeadctx->mayverify = VERIFY_HW;
  2895. } else {
  2896. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
  2897. aeadctx->mayverify = VERIFY_SW;
  2898. }
  2899. return crypto_aead_setauthsize(aeadctx->sw_cipher, authsize);
  2900. }
  2901. static int chcr_gcm_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
  2902. {
  2903. struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
  2904. switch (authsize) {
  2905. case ICV_4:
  2906. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL1;
  2907. aeadctx->mayverify = VERIFY_HW;
  2908. break;
  2909. case ICV_8:
  2910. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_DIV2;
  2911. aeadctx->mayverify = VERIFY_HW;
  2912. break;
  2913. case ICV_12:
  2914. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_IPSEC_96BIT;
  2915. aeadctx->mayverify = VERIFY_HW;
  2916. break;
  2917. case ICV_14:
  2918. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL3;
  2919. aeadctx->mayverify = VERIFY_HW;
  2920. break;
  2921. case ICV_16:
  2922. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
  2923. aeadctx->mayverify = VERIFY_HW;
  2924. break;
  2925. case ICV_13:
  2926. case ICV_15:
  2927. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
  2928. aeadctx->mayverify = VERIFY_SW;
  2929. break;
  2930. default:
  2931. return -EINVAL;
  2932. }
  2933. return crypto_aead_setauthsize(aeadctx->sw_cipher, authsize);
  2934. }
  2935. static int chcr_4106_4309_setauthsize(struct crypto_aead *tfm,
  2936. unsigned int authsize)
  2937. {
  2938. struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
  2939. switch (authsize) {
  2940. case ICV_8:
  2941. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_DIV2;
  2942. aeadctx->mayverify = VERIFY_HW;
  2943. break;
  2944. case ICV_12:
  2945. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_IPSEC_96BIT;
  2946. aeadctx->mayverify = VERIFY_HW;
  2947. break;
  2948. case ICV_16:
  2949. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
  2950. aeadctx->mayverify = VERIFY_HW;
  2951. break;
  2952. default:
  2953. return -EINVAL;
  2954. }
  2955. return crypto_aead_setauthsize(aeadctx->sw_cipher, authsize);
  2956. }
  2957. static int chcr_ccm_setauthsize(struct crypto_aead *tfm,
  2958. unsigned int authsize)
  2959. {
  2960. struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
  2961. switch (authsize) {
  2962. case ICV_4:
  2963. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL1;
  2964. aeadctx->mayverify = VERIFY_HW;
  2965. break;
  2966. case ICV_6:
  2967. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL2;
  2968. aeadctx->mayverify = VERIFY_HW;
  2969. break;
  2970. case ICV_8:
  2971. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_DIV2;
  2972. aeadctx->mayverify = VERIFY_HW;
  2973. break;
  2974. case ICV_10:
  2975. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_TRUNC_RFC4366;
  2976. aeadctx->mayverify = VERIFY_HW;
  2977. break;
  2978. case ICV_12:
  2979. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_IPSEC_96BIT;
  2980. aeadctx->mayverify = VERIFY_HW;
  2981. break;
  2982. case ICV_14:
  2983. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL3;
  2984. aeadctx->mayverify = VERIFY_HW;
  2985. break;
  2986. case ICV_16:
  2987. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
  2988. aeadctx->mayverify = VERIFY_HW;
  2989. break;
  2990. default:
  2991. return -EINVAL;
  2992. }
  2993. return crypto_aead_setauthsize(aeadctx->sw_cipher, authsize);
  2994. }
  2995. static int chcr_ccm_common_setkey(struct crypto_aead *aead,
  2996. const u8 *key,
  2997. unsigned int keylen)
  2998. {
  2999. struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(aead));
  3000. unsigned char ck_size, mk_size;
  3001. int key_ctx_size = 0;
  3002. key_ctx_size = sizeof(struct _key_ctx) + roundup(keylen, 16) * 2;
  3003. if (keylen == AES_KEYSIZE_128) {
  3004. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
  3005. mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_128;
  3006. } else if (keylen == AES_KEYSIZE_192) {
  3007. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
  3008. mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_192;
  3009. } else if (keylen == AES_KEYSIZE_256) {
  3010. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
  3011. mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_256;
  3012. } else {
  3013. aeadctx->enckey_len = 0;
  3014. return -EINVAL;
  3015. }
  3016. aeadctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, mk_size, 0, 0,
  3017. key_ctx_size >> 4);
  3018. memcpy(aeadctx->key, key, keylen);
  3019. aeadctx->enckey_len = keylen;
  3020. return 0;
  3021. }
  3022. static int chcr_aead_ccm_setkey(struct crypto_aead *aead,
  3023. const u8 *key,
  3024. unsigned int keylen)
  3025. {
  3026. struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(aead));
  3027. int error;
  3028. crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK);
  3029. crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(aead) &
  3030. CRYPTO_TFM_REQ_MASK);
  3031. error = crypto_aead_setkey(aeadctx->sw_cipher, key, keylen);
  3032. if (error)
  3033. return error;
  3034. return chcr_ccm_common_setkey(aead, key, keylen);
  3035. }
  3036. static int chcr_aead_rfc4309_setkey(struct crypto_aead *aead, const u8 *key,
  3037. unsigned int keylen)
  3038. {
  3039. struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(aead));
  3040. int error;
  3041. if (keylen < 3) {
  3042. aeadctx->enckey_len = 0;
  3043. return -EINVAL;
  3044. }
  3045. crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK);
  3046. crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(aead) &
  3047. CRYPTO_TFM_REQ_MASK);
  3048. error = crypto_aead_setkey(aeadctx->sw_cipher, key, keylen);
  3049. if (error)
  3050. return error;
  3051. keylen -= 3;
  3052. memcpy(aeadctx->salt, key + keylen, 3);
  3053. return chcr_ccm_common_setkey(aead, key, keylen);
  3054. }
  3055. static int chcr_gcm_setkey(struct crypto_aead *aead, const u8 *key,
  3056. unsigned int keylen)
  3057. {
  3058. struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(aead));
  3059. struct chcr_gcm_ctx *gctx = GCM_CTX(aeadctx);
  3060. unsigned int ck_size;
  3061. int ret = 0, key_ctx_size = 0;
  3062. struct aes_enckey aes;
  3063. aeadctx->enckey_len = 0;
  3064. crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK);
  3065. crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(aead)
  3066. & CRYPTO_TFM_REQ_MASK);
  3067. ret = crypto_aead_setkey(aeadctx->sw_cipher, key, keylen);
  3068. if (ret)
  3069. goto out;
  3070. if (get_aead_subtype(aead) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106 &&
  3071. keylen > 3) {
  3072. keylen -= 4; /* nonce/salt is present in the last 4 bytes */
  3073. memcpy(aeadctx->salt, key + keylen, 4);
  3074. }
  3075. if (keylen == AES_KEYSIZE_128) {
  3076. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
  3077. } else if (keylen == AES_KEYSIZE_192) {
  3078. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
  3079. } else if (keylen == AES_KEYSIZE_256) {
  3080. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
  3081. } else {
  3082. pr_err("GCM: Invalid key length %d\n", keylen);
  3083. ret = -EINVAL;
  3084. goto out;
  3085. }
  3086. memcpy(aeadctx->key, key, keylen);
  3087. aeadctx->enckey_len = keylen;
  3088. key_ctx_size = sizeof(struct _key_ctx) + roundup(keylen, 16) +
  3089. AEAD_H_SIZE;
  3090. aeadctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size,
  3091. CHCR_KEYCTX_MAC_KEY_SIZE_128,
  3092. 0, 0,
  3093. key_ctx_size >> 4);
  3094. /* Calculate the H = CIPH(K, 0 repeated 16 times).
  3095. * It will go in key context
  3096. */
  3097. ret = aes_prepareenckey(&aes, key, keylen);
  3098. if (ret) {
  3099. aeadctx->enckey_len = 0;
  3100. goto out;
  3101. }
  3102. memset(gctx->ghash_h, 0, AEAD_H_SIZE);
  3103. aes_encrypt(&aes, gctx->ghash_h, gctx->ghash_h);
  3104. memzero_explicit(&aes, sizeof(aes));
  3105. out:
  3106. return ret;
  3107. }
  3108. static int chcr_authenc_setkey(struct crypto_aead *authenc, const u8 *key,
  3109. unsigned int keylen)
  3110. {
  3111. struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(authenc));
  3112. struct chcr_authenc_ctx *actx = AUTHENC_CTX(aeadctx);
  3113. /* it contains auth and cipher key both*/
  3114. struct crypto_authenc_keys keys;
  3115. unsigned int subtype;
  3116. unsigned int max_authsize = crypto_aead_alg(authenc)->maxauthsize;
  3117. int err = 0, key_ctx_len = 0;
  3118. unsigned char ck_size = 0;
  3119. struct algo_param param;
  3120. int align;
  3121. crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK);
  3122. crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(authenc)
  3123. & CRYPTO_TFM_REQ_MASK);
  3124. err = crypto_aead_setkey(aeadctx->sw_cipher, key, keylen);
  3125. if (err)
  3126. goto out;
  3127. if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
  3128. goto out;
  3129. if (get_alg_config(&param, max_authsize)) {
  3130. pr_err("Unsupported digest size\n");
  3131. goto out;
  3132. }
  3133. subtype = get_aead_subtype(authenc);
  3134. if (subtype == CRYPTO_ALG_SUB_TYPE_CTR_SHA ||
  3135. subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL) {
  3136. if (keys.enckeylen < CTR_RFC3686_NONCE_SIZE)
  3137. goto out;
  3138. memcpy(aeadctx->nonce, keys.enckey + (keys.enckeylen
  3139. - CTR_RFC3686_NONCE_SIZE), CTR_RFC3686_NONCE_SIZE);
  3140. keys.enckeylen -= CTR_RFC3686_NONCE_SIZE;
  3141. }
  3142. if (keys.enckeylen == AES_KEYSIZE_128) {
  3143. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
  3144. } else if (keys.enckeylen == AES_KEYSIZE_192) {
  3145. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
  3146. } else if (keys.enckeylen == AES_KEYSIZE_256) {
  3147. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
  3148. } else {
  3149. pr_err("Unsupported cipher key\n");
  3150. goto out;
  3151. }
  3152. /* Copy only encryption key. We use authkey to generate h(ipad) and
  3153. * h(opad) so authkey is not needed again. authkeylen size have the
  3154. * size of the hash digest size.
  3155. */
  3156. memcpy(aeadctx->key, keys.enckey, keys.enckeylen);
  3157. aeadctx->enckey_len = keys.enckeylen;
  3158. if (subtype == CRYPTO_ALG_SUB_TYPE_CBC_SHA ||
  3159. subtype == CRYPTO_ALG_SUB_TYPE_CBC_NULL) {
  3160. get_aes_decrypt_key(actx->dec_rrkey, aeadctx->key,
  3161. aeadctx->enckey_len << 3);
  3162. }
  3163. align = KEYCTX_ALIGN_PAD(max_authsize);
  3164. err = chcr_prepare_hmac_key(keys.authkey, keys.authkeylen, max_authsize,
  3165. actx->h_iopad,
  3166. actx->h_iopad + param.result_size + align);
  3167. if (err)
  3168. goto out;
  3169. key_ctx_len = sizeof(struct _key_ctx) + roundup(keys.enckeylen, 16) +
  3170. (param.result_size + align) * 2;
  3171. aeadctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, param.mk_size, 0, 1,
  3172. key_ctx_len >> 4);
  3173. actx->auth_mode = param.auth_mode;
  3174. memzero_explicit(&keys, sizeof(keys));
  3175. return 0;
  3176. out:
  3177. aeadctx->enckey_len = 0;
  3178. memzero_explicit(&keys, sizeof(keys));
  3179. return -EINVAL;
  3180. }
  3181. static int chcr_aead_digest_null_setkey(struct crypto_aead *authenc,
  3182. const u8 *key, unsigned int keylen)
  3183. {
  3184. struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(authenc));
  3185. struct chcr_authenc_ctx *actx = AUTHENC_CTX(aeadctx);
  3186. struct crypto_authenc_keys keys;
  3187. int err;
  3188. /* it contains auth and cipher key both*/
  3189. unsigned int subtype;
  3190. int key_ctx_len = 0;
  3191. unsigned char ck_size = 0;
  3192. crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK);
  3193. crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(authenc)
  3194. & CRYPTO_TFM_REQ_MASK);
  3195. err = crypto_aead_setkey(aeadctx->sw_cipher, key, keylen);
  3196. if (err)
  3197. goto out;
  3198. if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
  3199. goto out;
  3200. subtype = get_aead_subtype(authenc);
  3201. if (subtype == CRYPTO_ALG_SUB_TYPE_CTR_SHA ||
  3202. subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL) {
  3203. if (keys.enckeylen < CTR_RFC3686_NONCE_SIZE)
  3204. goto out;
  3205. memcpy(aeadctx->nonce, keys.enckey + (keys.enckeylen
  3206. - CTR_RFC3686_NONCE_SIZE), CTR_RFC3686_NONCE_SIZE);
  3207. keys.enckeylen -= CTR_RFC3686_NONCE_SIZE;
  3208. }
  3209. if (keys.enckeylen == AES_KEYSIZE_128) {
  3210. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
  3211. } else if (keys.enckeylen == AES_KEYSIZE_192) {
  3212. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
  3213. } else if (keys.enckeylen == AES_KEYSIZE_256) {
  3214. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
  3215. } else {
  3216. pr_err("Unsupported cipher key %d\n", keys.enckeylen);
  3217. goto out;
  3218. }
  3219. memcpy(aeadctx->key, keys.enckey, keys.enckeylen);
  3220. aeadctx->enckey_len = keys.enckeylen;
  3221. if (subtype == CRYPTO_ALG_SUB_TYPE_CBC_SHA ||
  3222. subtype == CRYPTO_ALG_SUB_TYPE_CBC_NULL) {
  3223. get_aes_decrypt_key(actx->dec_rrkey, aeadctx->key,
  3224. aeadctx->enckey_len << 3);
  3225. }
  3226. key_ctx_len = sizeof(struct _key_ctx) + roundup(keys.enckeylen, 16);
  3227. aeadctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, CHCR_KEYCTX_NO_KEY, 0,
  3228. 0, key_ctx_len >> 4);
  3229. actx->auth_mode = CHCR_SCMD_AUTH_MODE_NOP;
  3230. memzero_explicit(&keys, sizeof(keys));
  3231. return 0;
  3232. out:
  3233. aeadctx->enckey_len = 0;
  3234. memzero_explicit(&keys, sizeof(keys));
  3235. return -EINVAL;
  3236. }
  3237. static int chcr_aead_op(struct aead_request *req,
  3238. int size,
  3239. create_wr_t create_wr_fn)
  3240. {
  3241. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  3242. struct chcr_aead_reqctx *reqctx = aead_request_ctx_dma(req);
  3243. struct chcr_context *ctx = a_ctx(tfm);
  3244. struct uld_ctx *u_ctx = ULD_CTX(ctx);
  3245. struct sk_buff *skb;
  3246. struct chcr_dev *cdev;
  3247. cdev = a_ctx(tfm)->dev;
  3248. if (!cdev) {
  3249. pr_err("%s : No crypto device.\n", __func__);
  3250. return -ENXIO;
  3251. }
  3252. if (chcr_inc_wrcount(cdev)) {
  3253. /* Detach state for CHCR means lldi or padap is freed.
  3254. * We cannot increment fallback here.
  3255. */
  3256. return chcr_aead_fallback(req, reqctx->op);
  3257. }
  3258. if (cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
  3259. reqctx->txqidx) &&
  3260. (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))) {
  3261. chcr_dec_wrcount(cdev);
  3262. return -ENOSPC;
  3263. }
  3264. if (get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106 &&
  3265. crypto_ipsec_check_assoclen(req->assoclen) != 0) {
  3266. pr_err("RFC4106: Invalid value of assoclen %d\n",
  3267. req->assoclen);
  3268. return -EINVAL;
  3269. }
  3270. /* Form a WR from req */
  3271. skb = create_wr_fn(req, u_ctx->lldi.rxq_ids[reqctx->rxqidx], size);
  3272. if (IS_ERR_OR_NULL(skb)) {
  3273. chcr_dec_wrcount(cdev);
  3274. return PTR_ERR_OR_ZERO(skb);
  3275. }
  3276. skb->dev = u_ctx->lldi.ports[0];
  3277. set_wr_txq(skb, CPL_PRIORITY_DATA, reqctx->txqidx);
  3278. chcr_send_wr(skb);
  3279. return -EINPROGRESS;
  3280. }
  3281. static int chcr_aead_encrypt(struct aead_request *req)
  3282. {
  3283. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  3284. struct chcr_aead_reqctx *reqctx = aead_request_ctx_dma(req);
  3285. struct chcr_context *ctx = a_ctx(tfm);
  3286. unsigned int cpu;
  3287. cpu = get_cpu();
  3288. reqctx->txqidx = cpu % ctx->ntxq;
  3289. reqctx->rxqidx = cpu % ctx->nrxq;
  3290. put_cpu();
  3291. reqctx->verify = VERIFY_HW;
  3292. reqctx->op = CHCR_ENCRYPT_OP;
  3293. switch (get_aead_subtype(tfm)) {
  3294. case CRYPTO_ALG_SUB_TYPE_CTR_SHA:
  3295. case CRYPTO_ALG_SUB_TYPE_CBC_SHA:
  3296. case CRYPTO_ALG_SUB_TYPE_CBC_NULL:
  3297. case CRYPTO_ALG_SUB_TYPE_CTR_NULL:
  3298. return chcr_aead_op(req, 0, create_authenc_wr);
  3299. case CRYPTO_ALG_SUB_TYPE_AEAD_CCM:
  3300. case CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309:
  3301. return chcr_aead_op(req, 0, create_aead_ccm_wr);
  3302. default:
  3303. return chcr_aead_op(req, 0, create_gcm_wr);
  3304. }
  3305. }
  3306. static int chcr_aead_decrypt(struct aead_request *req)
  3307. {
  3308. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  3309. struct chcr_context *ctx = a_ctx(tfm);
  3310. struct chcr_aead_ctx *aeadctx = AEAD_CTX(ctx);
  3311. struct chcr_aead_reqctx *reqctx = aead_request_ctx_dma(req);
  3312. int size;
  3313. unsigned int cpu;
  3314. cpu = get_cpu();
  3315. reqctx->txqidx = cpu % ctx->ntxq;
  3316. reqctx->rxqidx = cpu % ctx->nrxq;
  3317. put_cpu();
  3318. if (aeadctx->mayverify == VERIFY_SW) {
  3319. size = crypto_aead_maxauthsize(tfm);
  3320. reqctx->verify = VERIFY_SW;
  3321. } else {
  3322. size = 0;
  3323. reqctx->verify = VERIFY_HW;
  3324. }
  3325. reqctx->op = CHCR_DECRYPT_OP;
  3326. switch (get_aead_subtype(tfm)) {
  3327. case CRYPTO_ALG_SUB_TYPE_CBC_SHA:
  3328. case CRYPTO_ALG_SUB_TYPE_CTR_SHA:
  3329. case CRYPTO_ALG_SUB_TYPE_CBC_NULL:
  3330. case CRYPTO_ALG_SUB_TYPE_CTR_NULL:
  3331. return chcr_aead_op(req, size, create_authenc_wr);
  3332. case CRYPTO_ALG_SUB_TYPE_AEAD_CCM:
  3333. case CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309:
  3334. return chcr_aead_op(req, size, create_aead_ccm_wr);
  3335. default:
  3336. return chcr_aead_op(req, size, create_gcm_wr);
  3337. }
  3338. }
  3339. static struct chcr_alg_template driver_algs[] = {
  3340. /* AES-CBC */
  3341. {
  3342. .type = CRYPTO_ALG_TYPE_SKCIPHER | CRYPTO_ALG_SUB_TYPE_CBC,
  3343. .is_registered = 0,
  3344. .alg.skcipher = {
  3345. .base.cra_name = "cbc(aes)",
  3346. .base.cra_driver_name = "cbc-aes-chcr",
  3347. .base.cra_blocksize = AES_BLOCK_SIZE,
  3348. .init = chcr_init_tfm,
  3349. .exit = chcr_exit_tfm,
  3350. .min_keysize = AES_MIN_KEY_SIZE,
  3351. .max_keysize = AES_MAX_KEY_SIZE,
  3352. .ivsize = AES_BLOCK_SIZE,
  3353. .setkey = chcr_aes_cbc_setkey,
  3354. .encrypt = chcr_aes_encrypt,
  3355. .decrypt = chcr_aes_decrypt,
  3356. }
  3357. },
  3358. {
  3359. .type = CRYPTO_ALG_TYPE_SKCIPHER | CRYPTO_ALG_SUB_TYPE_XTS,
  3360. .is_registered = 0,
  3361. .alg.skcipher = {
  3362. .base.cra_name = "xts(aes)",
  3363. .base.cra_driver_name = "xts-aes-chcr",
  3364. .base.cra_blocksize = AES_BLOCK_SIZE,
  3365. .init = chcr_init_tfm,
  3366. .exit = chcr_exit_tfm,
  3367. .min_keysize = 2 * AES_MIN_KEY_SIZE,
  3368. .max_keysize = 2 * AES_MAX_KEY_SIZE,
  3369. .ivsize = AES_BLOCK_SIZE,
  3370. .setkey = chcr_aes_xts_setkey,
  3371. .encrypt = chcr_aes_encrypt,
  3372. .decrypt = chcr_aes_decrypt,
  3373. }
  3374. },
  3375. {
  3376. .type = CRYPTO_ALG_TYPE_SKCIPHER | CRYPTO_ALG_SUB_TYPE_CTR,
  3377. .is_registered = 0,
  3378. .alg.skcipher = {
  3379. .base.cra_name = "ctr(aes)",
  3380. .base.cra_driver_name = "ctr-aes-chcr",
  3381. .base.cra_blocksize = 1,
  3382. .init = chcr_init_tfm,
  3383. .exit = chcr_exit_tfm,
  3384. .min_keysize = AES_MIN_KEY_SIZE,
  3385. .max_keysize = AES_MAX_KEY_SIZE,
  3386. .ivsize = AES_BLOCK_SIZE,
  3387. .setkey = chcr_aes_ctr_setkey,
  3388. .encrypt = chcr_aes_encrypt,
  3389. .decrypt = chcr_aes_decrypt,
  3390. }
  3391. },
  3392. {
  3393. .type = CRYPTO_ALG_TYPE_SKCIPHER |
  3394. CRYPTO_ALG_SUB_TYPE_CTR_RFC3686,
  3395. .is_registered = 0,
  3396. .alg.skcipher = {
  3397. .base.cra_name = "rfc3686(ctr(aes))",
  3398. .base.cra_driver_name = "rfc3686-ctr-aes-chcr",
  3399. .base.cra_blocksize = 1,
  3400. .init = chcr_rfc3686_init,
  3401. .exit = chcr_exit_tfm,
  3402. .min_keysize = AES_MIN_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
  3403. .max_keysize = AES_MAX_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
  3404. .ivsize = CTR_RFC3686_IV_SIZE,
  3405. .setkey = chcr_aes_rfc3686_setkey,
  3406. .encrypt = chcr_aes_encrypt,
  3407. .decrypt = chcr_aes_decrypt,
  3408. }
  3409. },
  3410. /* SHA */
  3411. {
  3412. .type = CRYPTO_ALG_TYPE_AHASH,
  3413. .is_registered = 0,
  3414. .alg.hash = {
  3415. .halg.digestsize = SHA1_DIGEST_SIZE,
  3416. .halg.base = {
  3417. .cra_name = "sha1",
  3418. .cra_driver_name = "sha1-chcr",
  3419. .cra_blocksize = SHA1_BLOCK_SIZE,
  3420. }
  3421. }
  3422. },
  3423. {
  3424. .type = CRYPTO_ALG_TYPE_AHASH,
  3425. .is_registered = 0,
  3426. .alg.hash = {
  3427. .halg.digestsize = SHA256_DIGEST_SIZE,
  3428. .halg.base = {
  3429. .cra_name = "sha256",
  3430. .cra_driver_name = "sha256-chcr",
  3431. .cra_blocksize = SHA256_BLOCK_SIZE,
  3432. }
  3433. }
  3434. },
  3435. {
  3436. .type = CRYPTO_ALG_TYPE_AHASH,
  3437. .is_registered = 0,
  3438. .alg.hash = {
  3439. .halg.digestsize = SHA224_DIGEST_SIZE,
  3440. .halg.base = {
  3441. .cra_name = "sha224",
  3442. .cra_driver_name = "sha224-chcr",
  3443. .cra_blocksize = SHA224_BLOCK_SIZE,
  3444. }
  3445. }
  3446. },
  3447. {
  3448. .type = CRYPTO_ALG_TYPE_AHASH,
  3449. .is_registered = 0,
  3450. .alg.hash = {
  3451. .halg.digestsize = SHA384_DIGEST_SIZE,
  3452. .halg.base = {
  3453. .cra_name = "sha384",
  3454. .cra_driver_name = "sha384-chcr",
  3455. .cra_blocksize = SHA384_BLOCK_SIZE,
  3456. }
  3457. }
  3458. },
  3459. {
  3460. .type = CRYPTO_ALG_TYPE_AHASH,
  3461. .is_registered = 0,
  3462. .alg.hash = {
  3463. .halg.digestsize = SHA512_DIGEST_SIZE,
  3464. .halg.base = {
  3465. .cra_name = "sha512",
  3466. .cra_driver_name = "sha512-chcr",
  3467. .cra_blocksize = SHA512_BLOCK_SIZE,
  3468. }
  3469. }
  3470. },
  3471. /* HMAC */
  3472. {
  3473. .type = CRYPTO_ALG_TYPE_HMAC,
  3474. .is_registered = 0,
  3475. .alg.hash = {
  3476. .halg.digestsize = SHA1_DIGEST_SIZE,
  3477. .halg.base = {
  3478. .cra_name = "hmac(sha1)",
  3479. .cra_driver_name = "hmac-sha1-chcr",
  3480. .cra_blocksize = SHA1_BLOCK_SIZE,
  3481. }
  3482. }
  3483. },
  3484. {
  3485. .type = CRYPTO_ALG_TYPE_HMAC,
  3486. .is_registered = 0,
  3487. .alg.hash = {
  3488. .halg.digestsize = SHA224_DIGEST_SIZE,
  3489. .halg.base = {
  3490. .cra_name = "hmac(sha224)",
  3491. .cra_driver_name = "hmac-sha224-chcr",
  3492. .cra_blocksize = SHA224_BLOCK_SIZE,
  3493. }
  3494. }
  3495. },
  3496. {
  3497. .type = CRYPTO_ALG_TYPE_HMAC,
  3498. .is_registered = 0,
  3499. .alg.hash = {
  3500. .halg.digestsize = SHA256_DIGEST_SIZE,
  3501. .halg.base = {
  3502. .cra_name = "hmac(sha256)",
  3503. .cra_driver_name = "hmac-sha256-chcr",
  3504. .cra_blocksize = SHA256_BLOCK_SIZE,
  3505. }
  3506. }
  3507. },
  3508. {
  3509. .type = CRYPTO_ALG_TYPE_HMAC,
  3510. .is_registered = 0,
  3511. .alg.hash = {
  3512. .halg.digestsize = SHA384_DIGEST_SIZE,
  3513. .halg.base = {
  3514. .cra_name = "hmac(sha384)",
  3515. .cra_driver_name = "hmac-sha384-chcr",
  3516. .cra_blocksize = SHA384_BLOCK_SIZE,
  3517. }
  3518. }
  3519. },
  3520. {
  3521. .type = CRYPTO_ALG_TYPE_HMAC,
  3522. .is_registered = 0,
  3523. .alg.hash = {
  3524. .halg.digestsize = SHA512_DIGEST_SIZE,
  3525. .halg.base = {
  3526. .cra_name = "hmac(sha512)",
  3527. .cra_driver_name = "hmac-sha512-chcr",
  3528. .cra_blocksize = SHA512_BLOCK_SIZE,
  3529. }
  3530. }
  3531. },
  3532. /* Add AEAD Algorithms */
  3533. {
  3534. .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_AEAD_GCM,
  3535. .is_registered = 0,
  3536. .alg.aead = {
  3537. .base = {
  3538. .cra_name = "gcm(aes)",
  3539. .cra_driver_name = "gcm-aes-chcr",
  3540. .cra_blocksize = 1,
  3541. .cra_priority = CHCR_AEAD_PRIORITY,
  3542. .cra_ctxsize = sizeof(struct chcr_context) +
  3543. sizeof(struct chcr_aead_ctx) +
  3544. sizeof(struct chcr_gcm_ctx),
  3545. },
  3546. .ivsize = GCM_AES_IV_SIZE,
  3547. .maxauthsize = GHASH_DIGEST_SIZE,
  3548. .setkey = chcr_gcm_setkey,
  3549. .setauthsize = chcr_gcm_setauthsize,
  3550. }
  3551. },
  3552. {
  3553. .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106,
  3554. .is_registered = 0,
  3555. .alg.aead = {
  3556. .base = {
  3557. .cra_name = "rfc4106(gcm(aes))",
  3558. .cra_driver_name = "rfc4106-gcm-aes-chcr",
  3559. .cra_blocksize = 1,
  3560. .cra_priority = CHCR_AEAD_PRIORITY + 1,
  3561. .cra_ctxsize = sizeof(struct chcr_context) +
  3562. sizeof(struct chcr_aead_ctx) +
  3563. sizeof(struct chcr_gcm_ctx),
  3564. },
  3565. .ivsize = GCM_RFC4106_IV_SIZE,
  3566. .maxauthsize = GHASH_DIGEST_SIZE,
  3567. .setkey = chcr_gcm_setkey,
  3568. .setauthsize = chcr_4106_4309_setauthsize,
  3569. }
  3570. },
  3571. {
  3572. .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_AEAD_CCM,
  3573. .is_registered = 0,
  3574. .alg.aead = {
  3575. .base = {
  3576. .cra_name = "ccm(aes)",
  3577. .cra_driver_name = "ccm-aes-chcr",
  3578. .cra_blocksize = 1,
  3579. .cra_priority = CHCR_AEAD_PRIORITY,
  3580. .cra_ctxsize = sizeof(struct chcr_context) +
  3581. sizeof(struct chcr_aead_ctx),
  3582. },
  3583. .ivsize = AES_BLOCK_SIZE,
  3584. .maxauthsize = GHASH_DIGEST_SIZE,
  3585. .setkey = chcr_aead_ccm_setkey,
  3586. .setauthsize = chcr_ccm_setauthsize,
  3587. }
  3588. },
  3589. {
  3590. .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309,
  3591. .is_registered = 0,
  3592. .alg.aead = {
  3593. .base = {
  3594. .cra_name = "rfc4309(ccm(aes))",
  3595. .cra_driver_name = "rfc4309-ccm-aes-chcr",
  3596. .cra_blocksize = 1,
  3597. .cra_priority = CHCR_AEAD_PRIORITY + 1,
  3598. .cra_ctxsize = sizeof(struct chcr_context) +
  3599. sizeof(struct chcr_aead_ctx),
  3600. },
  3601. .ivsize = 8,
  3602. .maxauthsize = GHASH_DIGEST_SIZE,
  3603. .setkey = chcr_aead_rfc4309_setkey,
  3604. .setauthsize = chcr_4106_4309_setauthsize,
  3605. }
  3606. },
  3607. {
  3608. .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_SHA,
  3609. .is_registered = 0,
  3610. .alg.aead = {
  3611. .base = {
  3612. .cra_name = "authenc(hmac(sha1),cbc(aes))",
  3613. .cra_driver_name =
  3614. "authenc-hmac-sha1-cbc-aes-chcr",
  3615. .cra_blocksize = AES_BLOCK_SIZE,
  3616. .cra_priority = CHCR_AEAD_PRIORITY,
  3617. .cra_ctxsize = sizeof(struct chcr_context) +
  3618. sizeof(struct chcr_aead_ctx) +
  3619. sizeof(struct chcr_authenc_ctx),
  3620. },
  3621. .ivsize = AES_BLOCK_SIZE,
  3622. .maxauthsize = SHA1_DIGEST_SIZE,
  3623. .setkey = chcr_authenc_setkey,
  3624. .setauthsize = chcr_authenc_setauthsize,
  3625. }
  3626. },
  3627. {
  3628. .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_SHA,
  3629. .is_registered = 0,
  3630. .alg.aead = {
  3631. .base = {
  3632. .cra_name = "authenc(hmac(sha256),cbc(aes))",
  3633. .cra_driver_name =
  3634. "authenc-hmac-sha256-cbc-aes-chcr",
  3635. .cra_blocksize = AES_BLOCK_SIZE,
  3636. .cra_priority = CHCR_AEAD_PRIORITY,
  3637. .cra_ctxsize = sizeof(struct chcr_context) +
  3638. sizeof(struct chcr_aead_ctx) +
  3639. sizeof(struct chcr_authenc_ctx),
  3640. },
  3641. .ivsize = AES_BLOCK_SIZE,
  3642. .maxauthsize = SHA256_DIGEST_SIZE,
  3643. .setkey = chcr_authenc_setkey,
  3644. .setauthsize = chcr_authenc_setauthsize,
  3645. }
  3646. },
  3647. {
  3648. .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_SHA,
  3649. .is_registered = 0,
  3650. .alg.aead = {
  3651. .base = {
  3652. .cra_name = "authenc(hmac(sha224),cbc(aes))",
  3653. .cra_driver_name =
  3654. "authenc-hmac-sha224-cbc-aes-chcr",
  3655. .cra_blocksize = AES_BLOCK_SIZE,
  3656. .cra_priority = CHCR_AEAD_PRIORITY,
  3657. .cra_ctxsize = sizeof(struct chcr_context) +
  3658. sizeof(struct chcr_aead_ctx) +
  3659. sizeof(struct chcr_authenc_ctx),
  3660. },
  3661. .ivsize = AES_BLOCK_SIZE,
  3662. .maxauthsize = SHA224_DIGEST_SIZE,
  3663. .setkey = chcr_authenc_setkey,
  3664. .setauthsize = chcr_authenc_setauthsize,
  3665. }
  3666. },
  3667. {
  3668. .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_SHA,
  3669. .is_registered = 0,
  3670. .alg.aead = {
  3671. .base = {
  3672. .cra_name = "authenc(hmac(sha384),cbc(aes))",
  3673. .cra_driver_name =
  3674. "authenc-hmac-sha384-cbc-aes-chcr",
  3675. .cra_blocksize = AES_BLOCK_SIZE,
  3676. .cra_priority = CHCR_AEAD_PRIORITY,
  3677. .cra_ctxsize = sizeof(struct chcr_context) +
  3678. sizeof(struct chcr_aead_ctx) +
  3679. sizeof(struct chcr_authenc_ctx),
  3680. },
  3681. .ivsize = AES_BLOCK_SIZE,
  3682. .maxauthsize = SHA384_DIGEST_SIZE,
  3683. .setkey = chcr_authenc_setkey,
  3684. .setauthsize = chcr_authenc_setauthsize,
  3685. }
  3686. },
  3687. {
  3688. .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_SHA,
  3689. .is_registered = 0,
  3690. .alg.aead = {
  3691. .base = {
  3692. .cra_name = "authenc(hmac(sha512),cbc(aes))",
  3693. .cra_driver_name =
  3694. "authenc-hmac-sha512-cbc-aes-chcr",
  3695. .cra_blocksize = AES_BLOCK_SIZE,
  3696. .cra_priority = CHCR_AEAD_PRIORITY,
  3697. .cra_ctxsize = sizeof(struct chcr_context) +
  3698. sizeof(struct chcr_aead_ctx) +
  3699. sizeof(struct chcr_authenc_ctx),
  3700. },
  3701. .ivsize = AES_BLOCK_SIZE,
  3702. .maxauthsize = SHA512_DIGEST_SIZE,
  3703. .setkey = chcr_authenc_setkey,
  3704. .setauthsize = chcr_authenc_setauthsize,
  3705. }
  3706. },
  3707. {
  3708. .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_NULL,
  3709. .is_registered = 0,
  3710. .alg.aead = {
  3711. .base = {
  3712. .cra_name = "authenc(digest_null,cbc(aes))",
  3713. .cra_driver_name =
  3714. "authenc-digest_null-cbc-aes-chcr",
  3715. .cra_blocksize = AES_BLOCK_SIZE,
  3716. .cra_priority = CHCR_AEAD_PRIORITY,
  3717. .cra_ctxsize = sizeof(struct chcr_context) +
  3718. sizeof(struct chcr_aead_ctx) +
  3719. sizeof(struct chcr_authenc_ctx),
  3720. },
  3721. .ivsize = AES_BLOCK_SIZE,
  3722. .maxauthsize = 0,
  3723. .setkey = chcr_aead_digest_null_setkey,
  3724. .setauthsize = chcr_authenc_null_setauthsize,
  3725. }
  3726. },
  3727. {
  3728. .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_SHA,
  3729. .is_registered = 0,
  3730. .alg.aead = {
  3731. .base = {
  3732. .cra_name = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
  3733. .cra_driver_name =
  3734. "authenc-hmac-sha1-rfc3686-ctr-aes-chcr",
  3735. .cra_blocksize = 1,
  3736. .cra_priority = CHCR_AEAD_PRIORITY,
  3737. .cra_ctxsize = sizeof(struct chcr_context) +
  3738. sizeof(struct chcr_aead_ctx) +
  3739. sizeof(struct chcr_authenc_ctx),
  3740. },
  3741. .ivsize = CTR_RFC3686_IV_SIZE,
  3742. .maxauthsize = SHA1_DIGEST_SIZE,
  3743. .setkey = chcr_authenc_setkey,
  3744. .setauthsize = chcr_authenc_setauthsize,
  3745. }
  3746. },
  3747. {
  3748. .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_SHA,
  3749. .is_registered = 0,
  3750. .alg.aead = {
  3751. .base = {
  3752. .cra_name = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
  3753. .cra_driver_name =
  3754. "authenc-hmac-sha256-rfc3686-ctr-aes-chcr",
  3755. .cra_blocksize = 1,
  3756. .cra_priority = CHCR_AEAD_PRIORITY,
  3757. .cra_ctxsize = sizeof(struct chcr_context) +
  3758. sizeof(struct chcr_aead_ctx) +
  3759. sizeof(struct chcr_authenc_ctx),
  3760. },
  3761. .ivsize = CTR_RFC3686_IV_SIZE,
  3762. .maxauthsize = SHA256_DIGEST_SIZE,
  3763. .setkey = chcr_authenc_setkey,
  3764. .setauthsize = chcr_authenc_setauthsize,
  3765. }
  3766. },
  3767. {
  3768. .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_SHA,
  3769. .is_registered = 0,
  3770. .alg.aead = {
  3771. .base = {
  3772. .cra_name = "authenc(hmac(sha224),rfc3686(ctr(aes)))",
  3773. .cra_driver_name =
  3774. "authenc-hmac-sha224-rfc3686-ctr-aes-chcr",
  3775. .cra_blocksize = 1,
  3776. .cra_priority = CHCR_AEAD_PRIORITY,
  3777. .cra_ctxsize = sizeof(struct chcr_context) +
  3778. sizeof(struct chcr_aead_ctx) +
  3779. sizeof(struct chcr_authenc_ctx),
  3780. },
  3781. .ivsize = CTR_RFC3686_IV_SIZE,
  3782. .maxauthsize = SHA224_DIGEST_SIZE,
  3783. .setkey = chcr_authenc_setkey,
  3784. .setauthsize = chcr_authenc_setauthsize,
  3785. }
  3786. },
  3787. {
  3788. .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_SHA,
  3789. .is_registered = 0,
  3790. .alg.aead = {
  3791. .base = {
  3792. .cra_name = "authenc(hmac(sha384),rfc3686(ctr(aes)))",
  3793. .cra_driver_name =
  3794. "authenc-hmac-sha384-rfc3686-ctr-aes-chcr",
  3795. .cra_blocksize = 1,
  3796. .cra_priority = CHCR_AEAD_PRIORITY,
  3797. .cra_ctxsize = sizeof(struct chcr_context) +
  3798. sizeof(struct chcr_aead_ctx) +
  3799. sizeof(struct chcr_authenc_ctx),
  3800. },
  3801. .ivsize = CTR_RFC3686_IV_SIZE,
  3802. .maxauthsize = SHA384_DIGEST_SIZE,
  3803. .setkey = chcr_authenc_setkey,
  3804. .setauthsize = chcr_authenc_setauthsize,
  3805. }
  3806. },
  3807. {
  3808. .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_SHA,
  3809. .is_registered = 0,
  3810. .alg.aead = {
  3811. .base = {
  3812. .cra_name = "authenc(hmac(sha512),rfc3686(ctr(aes)))",
  3813. .cra_driver_name =
  3814. "authenc-hmac-sha512-rfc3686-ctr-aes-chcr",
  3815. .cra_blocksize = 1,
  3816. .cra_priority = CHCR_AEAD_PRIORITY,
  3817. .cra_ctxsize = sizeof(struct chcr_context) +
  3818. sizeof(struct chcr_aead_ctx) +
  3819. sizeof(struct chcr_authenc_ctx),
  3820. },
  3821. .ivsize = CTR_RFC3686_IV_SIZE,
  3822. .maxauthsize = SHA512_DIGEST_SIZE,
  3823. .setkey = chcr_authenc_setkey,
  3824. .setauthsize = chcr_authenc_setauthsize,
  3825. }
  3826. },
  3827. {
  3828. .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_NULL,
  3829. .is_registered = 0,
  3830. .alg.aead = {
  3831. .base = {
  3832. .cra_name = "authenc(digest_null,rfc3686(ctr(aes)))",
  3833. .cra_driver_name =
  3834. "authenc-digest_null-rfc3686-ctr-aes-chcr",
  3835. .cra_blocksize = 1,
  3836. .cra_priority = CHCR_AEAD_PRIORITY,
  3837. .cra_ctxsize = sizeof(struct chcr_context) +
  3838. sizeof(struct chcr_aead_ctx) +
  3839. sizeof(struct chcr_authenc_ctx),
  3840. },
  3841. .ivsize = CTR_RFC3686_IV_SIZE,
  3842. .maxauthsize = 0,
  3843. .setkey = chcr_aead_digest_null_setkey,
  3844. .setauthsize = chcr_authenc_null_setauthsize,
  3845. }
  3846. },
  3847. };
  3848. /*
  3849. * chcr_unregister_alg - Deregister crypto algorithms with
  3850. * kernel framework.
  3851. */
  3852. static int chcr_unregister_alg(void)
  3853. {
  3854. int i;
  3855. for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
  3856. switch (driver_algs[i].type & CRYPTO_ALG_TYPE_MASK) {
  3857. case CRYPTO_ALG_TYPE_SKCIPHER:
  3858. if (driver_algs[i].is_registered && refcount_read(
  3859. &driver_algs[i].alg.skcipher.base.cra_refcnt)
  3860. == 1) {
  3861. crypto_unregister_skcipher(
  3862. &driver_algs[i].alg.skcipher);
  3863. driver_algs[i].is_registered = 0;
  3864. }
  3865. break;
  3866. case CRYPTO_ALG_TYPE_AEAD:
  3867. if (driver_algs[i].is_registered && refcount_read(
  3868. &driver_algs[i].alg.aead.base.cra_refcnt) == 1) {
  3869. crypto_unregister_aead(
  3870. &driver_algs[i].alg.aead);
  3871. driver_algs[i].is_registered = 0;
  3872. }
  3873. break;
  3874. case CRYPTO_ALG_TYPE_AHASH:
  3875. if (driver_algs[i].is_registered && refcount_read(
  3876. &driver_algs[i].alg.hash.halg.base.cra_refcnt)
  3877. == 1) {
  3878. crypto_unregister_ahash(
  3879. &driver_algs[i].alg.hash);
  3880. driver_algs[i].is_registered = 0;
  3881. }
  3882. break;
  3883. }
  3884. }
  3885. return 0;
  3886. }
  3887. #define SZ_AHASH_CTX sizeof(struct chcr_context)
  3888. #define SZ_AHASH_H_CTX (sizeof(struct chcr_context) + sizeof(struct hmac_ctx))
  3889. #define SZ_AHASH_REQ_CTX sizeof(struct chcr_ahash_req_ctx)
  3890. /*
  3891. * chcr_register_alg - Register crypto algorithms with kernel framework.
  3892. */
  3893. static int chcr_register_alg(void)
  3894. {
  3895. struct crypto_alg ai;
  3896. struct ahash_alg *a_hash;
  3897. int err = 0, i;
  3898. char *name = NULL;
  3899. for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
  3900. if (driver_algs[i].is_registered)
  3901. continue;
  3902. switch (driver_algs[i].type & CRYPTO_ALG_TYPE_MASK) {
  3903. case CRYPTO_ALG_TYPE_SKCIPHER:
  3904. driver_algs[i].alg.skcipher.base.cra_priority =
  3905. CHCR_CRA_PRIORITY;
  3906. driver_algs[i].alg.skcipher.base.cra_module = THIS_MODULE;
  3907. driver_algs[i].alg.skcipher.base.cra_flags =
  3908. CRYPTO_ALG_TYPE_SKCIPHER | CRYPTO_ALG_ASYNC |
  3909. CRYPTO_ALG_ALLOCATES_MEMORY |
  3910. CRYPTO_ALG_NEED_FALLBACK;
  3911. driver_algs[i].alg.skcipher.base.cra_ctxsize =
  3912. sizeof(struct chcr_context) +
  3913. sizeof(struct ablk_ctx);
  3914. driver_algs[i].alg.skcipher.base.cra_alignmask = 0;
  3915. err = crypto_register_skcipher(&driver_algs[i].alg.skcipher);
  3916. name = driver_algs[i].alg.skcipher.base.cra_driver_name;
  3917. break;
  3918. case CRYPTO_ALG_TYPE_AEAD:
  3919. driver_algs[i].alg.aead.base.cra_flags =
  3920. CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK |
  3921. CRYPTO_ALG_ALLOCATES_MEMORY;
  3922. driver_algs[i].alg.aead.encrypt = chcr_aead_encrypt;
  3923. driver_algs[i].alg.aead.decrypt = chcr_aead_decrypt;
  3924. driver_algs[i].alg.aead.init = chcr_aead_cra_init;
  3925. driver_algs[i].alg.aead.exit = chcr_aead_cra_exit;
  3926. driver_algs[i].alg.aead.base.cra_module = THIS_MODULE;
  3927. err = crypto_register_aead(&driver_algs[i].alg.aead);
  3928. name = driver_algs[i].alg.aead.base.cra_driver_name;
  3929. break;
  3930. case CRYPTO_ALG_TYPE_AHASH:
  3931. a_hash = &driver_algs[i].alg.hash;
  3932. a_hash->update = chcr_ahash_update;
  3933. a_hash->final = chcr_ahash_final;
  3934. a_hash->finup = chcr_ahash_finup;
  3935. a_hash->digest = chcr_ahash_digest;
  3936. a_hash->export = chcr_ahash_export;
  3937. a_hash->import = chcr_ahash_import;
  3938. a_hash->halg.statesize = SZ_AHASH_REQ_CTX;
  3939. a_hash->halg.base.cra_priority = CHCR_CRA_PRIORITY;
  3940. a_hash->halg.base.cra_module = THIS_MODULE;
  3941. a_hash->halg.base.cra_flags =
  3942. CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY;
  3943. a_hash->halg.base.cra_alignmask = 0;
  3944. a_hash->halg.base.cra_exit = NULL;
  3945. if (driver_algs[i].type == CRYPTO_ALG_TYPE_HMAC) {
  3946. a_hash->halg.base.cra_init = chcr_hmac_cra_init;
  3947. a_hash->init = chcr_hmac_init;
  3948. a_hash->setkey = chcr_ahash_setkey;
  3949. a_hash->halg.base.cra_ctxsize = SZ_AHASH_H_CTX;
  3950. } else {
  3951. a_hash->init = chcr_sha_init;
  3952. a_hash->halg.base.cra_ctxsize = SZ_AHASH_CTX;
  3953. a_hash->halg.base.cra_init = chcr_sha_cra_init;
  3954. }
  3955. err = crypto_register_ahash(&driver_algs[i].alg.hash);
  3956. ai = driver_algs[i].alg.hash.halg.base;
  3957. name = ai.cra_driver_name;
  3958. break;
  3959. }
  3960. if (err) {
  3961. pr_err("%s : Algorithm registration failed\n", name);
  3962. goto register_err;
  3963. } else {
  3964. driver_algs[i].is_registered = 1;
  3965. }
  3966. }
  3967. return 0;
  3968. register_err:
  3969. chcr_unregister_alg();
  3970. return err;
  3971. }
  3972. /*
  3973. * start_crypto - Register the crypto algorithms.
  3974. * This should called once when the first device comesup. After this
  3975. * kernel will start calling driver APIs for crypto operations.
  3976. */
  3977. int start_crypto(void)
  3978. {
  3979. return chcr_register_alg();
  3980. }
  3981. /*
  3982. * stop_crypto - Deregister all the crypto algorithms with kernel.
  3983. * This should be called once when the last device goes down. After this
  3984. * kernel will not call the driver API for crypto operations.
  3985. */
  3986. int stop_crypto(void)
  3987. {
  3988. chcr_unregister_alg();
  3989. return 0;
  3990. }