chcr_algo.c 127 KB

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