cipher.c 136 KB

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