cipher.c 131 KB

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