cipher.c 135 KB

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