cipher.c 129 KB

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