caamalg.c 129 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312
  1. /*
  2. * caam - Freescale FSL CAAM support for crypto API
  3. *
  4. * Copyright 2008-2011 Freescale Semiconductor, Inc.
  5. *
  6. * Based on talitos crypto API driver.
  7. *
  8. * relationship of job descriptors to shared descriptors (SteveC Dec 10 2008):
  9. *
  10. * --------------- ---------------
  11. * | JobDesc #1 |-------------------->| ShareDesc |
  12. * | *(packet 1) | | (PDB) |
  13. * --------------- |------------->| (hashKey) |
  14. * . | | (cipherKey) |
  15. * . | |-------->| (operation) |
  16. * --------------- | | ---------------
  17. * | JobDesc #2 |------| |
  18. * | *(packet 2) | |
  19. * --------------- |
  20. * . |
  21. * . |
  22. * --------------- |
  23. * | JobDesc #3 |------------
  24. * | *(packet 3) |
  25. * ---------------
  26. *
  27. * The SharedDesc never changes for a connection unless rekeyed, but
  28. * each packet will likely be in a different place. So all we need
  29. * to know to process the packet is where the input is, where the
  30. * output goes, and what context we want to process with. Context is
  31. * in the SharedDesc, packet references in the JobDesc.
  32. *
  33. * So, a job desc looks like:
  34. *
  35. * ---------------------
  36. * | Header |
  37. * | ShareDesc Pointer |
  38. * | SEQ_OUT_PTR |
  39. * | (output buffer) |
  40. * | (output length) |
  41. * | SEQ_IN_PTR |
  42. * | (input buffer) |
  43. * | (input length) |
  44. * ---------------------
  45. */
  46. #include "compat.h"
  47. #include "regs.h"
  48. #include "intern.h"
  49. #include "desc_constr.h"
  50. #include "jr.h"
  51. #include "error.h"
  52. #include "sg_sw_sec4.h"
  53. #include "key_gen.h"
  54. /*
  55. * crypto alg
  56. */
  57. #define CAAM_CRA_PRIORITY 3000
  58. /* max key is sum of AES_MAX_KEY_SIZE, max split key size */
  59. #define CAAM_MAX_KEY_SIZE (AES_MAX_KEY_SIZE + \
  60. CTR_RFC3686_NONCE_SIZE + \
  61. SHA512_DIGEST_SIZE * 2)
  62. /* max IV is max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */
  63. #define CAAM_MAX_IV_LENGTH 16
  64. /* length of descriptors text */
  65. #define DESC_AEAD_BASE (4 * CAAM_CMD_SZ)
  66. #define DESC_AEAD_ENC_LEN (DESC_AEAD_BASE + 15 * CAAM_CMD_SZ)
  67. #define DESC_AEAD_DEC_LEN (DESC_AEAD_BASE + 18 * CAAM_CMD_SZ)
  68. #define DESC_AEAD_GIVENC_LEN (DESC_AEAD_ENC_LEN + 7 * CAAM_CMD_SZ)
  69. /* Note: Nonce is counted in enckeylen */
  70. #define DESC_AEAD_CTR_RFC3686_LEN (6 * CAAM_CMD_SZ)
  71. #define DESC_AEAD_NULL_BASE (3 * CAAM_CMD_SZ)
  72. #define DESC_AEAD_NULL_ENC_LEN (DESC_AEAD_NULL_BASE + 14 * CAAM_CMD_SZ)
  73. #define DESC_AEAD_NULL_DEC_LEN (DESC_AEAD_NULL_BASE + 17 * CAAM_CMD_SZ)
  74. #define DESC_GCM_BASE (3 * CAAM_CMD_SZ)
  75. #define DESC_GCM_ENC_LEN (DESC_GCM_BASE + 23 * CAAM_CMD_SZ)
  76. #define DESC_GCM_DEC_LEN (DESC_GCM_BASE + 19 * CAAM_CMD_SZ)
  77. #define DESC_RFC4106_BASE (3 * CAAM_CMD_SZ)
  78. #define DESC_RFC4106_ENC_LEN (DESC_RFC4106_BASE + 15 * CAAM_CMD_SZ)
  79. #define DESC_RFC4106_DEC_LEN (DESC_RFC4106_BASE + 14 * CAAM_CMD_SZ)
  80. #define DESC_RFC4106_GIVENC_LEN (DESC_RFC4106_BASE + 21 * CAAM_CMD_SZ)
  81. #define DESC_RFC4543_BASE (3 * CAAM_CMD_SZ)
  82. #define DESC_RFC4543_ENC_LEN (DESC_RFC4543_BASE + 25 * CAAM_CMD_SZ)
  83. #define DESC_RFC4543_DEC_LEN (DESC_RFC4543_BASE + 27 * CAAM_CMD_SZ)
  84. #define DESC_RFC4543_GIVENC_LEN (DESC_RFC4543_BASE + 30 * CAAM_CMD_SZ)
  85. #define DESC_ABLKCIPHER_BASE (3 * CAAM_CMD_SZ)
  86. #define DESC_ABLKCIPHER_ENC_LEN (DESC_ABLKCIPHER_BASE + \
  87. 20 * CAAM_CMD_SZ)
  88. #define DESC_ABLKCIPHER_DEC_LEN (DESC_ABLKCIPHER_BASE + \
  89. 15 * CAAM_CMD_SZ)
  90. #define DESC_MAX_USED_BYTES (DESC_RFC4543_GIVENC_LEN + \
  91. CAAM_MAX_KEY_SIZE)
  92. #define DESC_MAX_USED_LEN (DESC_MAX_USED_BYTES / CAAM_CMD_SZ)
  93. #ifdef DEBUG
  94. /* for print_hex_dumps with line references */
  95. #define debug(format, arg...) printk(format, arg)
  96. #else
  97. #define debug(format, arg...)
  98. #endif
  99. static struct list_head alg_list;
  100. /* Set DK bit in class 1 operation if shared */
  101. static inline void append_dec_op1(u32 *desc, u32 type)
  102. {
  103. u32 *jump_cmd, *uncond_jump_cmd;
  104. /* DK bit is valid only for AES */
  105. if ((type & OP_ALG_ALGSEL_MASK) != OP_ALG_ALGSEL_AES) {
  106. append_operation(desc, type | OP_ALG_AS_INITFINAL |
  107. OP_ALG_DECRYPT);
  108. return;
  109. }
  110. jump_cmd = append_jump(desc, JUMP_TEST_ALL | JUMP_COND_SHRD);
  111. append_operation(desc, type | OP_ALG_AS_INITFINAL |
  112. OP_ALG_DECRYPT);
  113. uncond_jump_cmd = append_jump(desc, JUMP_TEST_ALL);
  114. set_jump_tgt_here(desc, jump_cmd);
  115. append_operation(desc, type | OP_ALG_AS_INITFINAL |
  116. OP_ALG_DECRYPT | OP_ALG_AAI_DK);
  117. set_jump_tgt_here(desc, uncond_jump_cmd);
  118. }
  119. /*
  120. * For aead functions, read payload and write payload,
  121. * both of which are specified in req->src and req->dst
  122. */
  123. static inline void aead_append_src_dst(u32 *desc, u32 msg_type)
  124. {
  125. append_seq_fifo_store(desc, 0, FIFOST_TYPE_MESSAGE_DATA | KEY_VLF);
  126. append_seq_fifo_load(desc, 0, FIFOLD_CLASS_BOTH |
  127. KEY_VLF | msg_type | FIFOLD_TYPE_LASTBOTH);
  128. }
  129. /*
  130. * For aead encrypt and decrypt, read iv for both classes
  131. */
  132. static inline void aead_append_ld_iv(u32 *desc, int ivsize, int ivoffset)
  133. {
  134. append_seq_load(desc, ivsize, LDST_CLASS_1_CCB |
  135. LDST_SRCDST_BYTE_CONTEXT |
  136. (ivoffset << LDST_OFFSET_SHIFT));
  137. append_move(desc, MOVE_SRC_CLASS1CTX | MOVE_DEST_CLASS2INFIFO |
  138. (ivoffset << MOVE_OFFSET_SHIFT) | ivsize);
  139. }
  140. /*
  141. * For ablkcipher encrypt and decrypt, read from req->src and
  142. * write to req->dst
  143. */
  144. static inline void ablkcipher_append_src_dst(u32 *desc)
  145. {
  146. append_math_add(desc, VARSEQOUTLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
  147. append_math_add(desc, VARSEQINLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
  148. append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 |
  149. KEY_VLF | FIFOLD_TYPE_MSG | FIFOLD_TYPE_LAST1);
  150. append_seq_fifo_store(desc, 0, FIFOST_TYPE_MESSAGE_DATA | KEY_VLF);
  151. }
  152. /*
  153. * If all data, including src (with assoc and iv) or dst (with iv only) are
  154. * contiguous
  155. */
  156. #define GIV_SRC_CONTIG 1
  157. #define GIV_DST_CONTIG (1 << 1)
  158. /*
  159. * per-session context
  160. */
  161. struct caam_ctx {
  162. struct device *jrdev;
  163. u32 sh_desc_enc[DESC_MAX_USED_LEN];
  164. u32 sh_desc_dec[DESC_MAX_USED_LEN];
  165. u32 sh_desc_givenc[DESC_MAX_USED_LEN];
  166. dma_addr_t sh_desc_enc_dma;
  167. dma_addr_t sh_desc_dec_dma;
  168. dma_addr_t sh_desc_givenc_dma;
  169. u32 class1_alg_type;
  170. u32 class2_alg_type;
  171. u32 alg_op;
  172. u8 key[CAAM_MAX_KEY_SIZE];
  173. dma_addr_t key_dma;
  174. unsigned int enckeylen;
  175. unsigned int split_key_len;
  176. unsigned int split_key_pad_len;
  177. unsigned int authsize;
  178. };
  179. static void append_key_aead(u32 *desc, struct caam_ctx *ctx,
  180. int keys_fit_inline, bool is_rfc3686)
  181. {
  182. u32 *nonce;
  183. unsigned int enckeylen = ctx->enckeylen;
  184. /*
  185. * RFC3686 specific:
  186. * | ctx->key = {AUTH_KEY, ENC_KEY, NONCE}
  187. * | enckeylen = encryption key size + nonce size
  188. */
  189. if (is_rfc3686)
  190. enckeylen -= CTR_RFC3686_NONCE_SIZE;
  191. if (keys_fit_inline) {
  192. append_key_as_imm(desc, ctx->key, ctx->split_key_pad_len,
  193. ctx->split_key_len, CLASS_2 |
  194. KEY_DEST_MDHA_SPLIT | KEY_ENC);
  195. append_key_as_imm(desc, (void *)ctx->key +
  196. ctx->split_key_pad_len, enckeylen,
  197. enckeylen, CLASS_1 | KEY_DEST_CLASS_REG);
  198. } else {
  199. append_key(desc, ctx->key_dma, ctx->split_key_len, CLASS_2 |
  200. KEY_DEST_MDHA_SPLIT | KEY_ENC);
  201. append_key(desc, ctx->key_dma + ctx->split_key_pad_len,
  202. enckeylen, CLASS_1 | KEY_DEST_CLASS_REG);
  203. }
  204. /* Load Counter into CONTEXT1 reg */
  205. if (is_rfc3686) {
  206. nonce = (u32 *)((void *)ctx->key + ctx->split_key_pad_len +
  207. enckeylen);
  208. append_load_imm_u32(desc, *nonce, LDST_CLASS_IND_CCB |
  209. LDST_SRCDST_BYTE_OUTFIFO | LDST_IMM);
  210. append_move(desc,
  211. MOVE_SRC_OUTFIFO |
  212. MOVE_DEST_CLASS1CTX |
  213. (16 << MOVE_OFFSET_SHIFT) |
  214. (CTR_RFC3686_NONCE_SIZE << MOVE_LEN_SHIFT));
  215. }
  216. }
  217. static void init_sh_desc_key_aead(u32 *desc, struct caam_ctx *ctx,
  218. int keys_fit_inline, bool is_rfc3686)
  219. {
  220. u32 *key_jump_cmd;
  221. /* Note: Context registers are saved. */
  222. init_sh_desc(desc, HDR_SHARE_SERIAL | HDR_SAVECTX);
  223. /* Skip if already shared */
  224. key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
  225. JUMP_COND_SHRD);
  226. append_key_aead(desc, ctx, keys_fit_inline, is_rfc3686);
  227. set_jump_tgt_here(desc, key_jump_cmd);
  228. }
  229. static int aead_null_set_sh_desc(struct crypto_aead *aead)
  230. {
  231. struct aead_tfm *tfm = &aead->base.crt_aead;
  232. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  233. struct device *jrdev = ctx->jrdev;
  234. bool keys_fit_inline = false;
  235. u32 *key_jump_cmd, *jump_cmd, *read_move_cmd, *write_move_cmd;
  236. u32 *desc;
  237. /*
  238. * Job Descriptor and Shared Descriptors
  239. * must all fit into the 64-word Descriptor h/w Buffer
  240. */
  241. if (DESC_AEAD_NULL_ENC_LEN + DESC_JOB_IO_LEN +
  242. ctx->split_key_pad_len <= CAAM_DESC_BYTES_MAX)
  243. keys_fit_inline = true;
  244. /* aead_encrypt shared descriptor */
  245. desc = ctx->sh_desc_enc;
  246. init_sh_desc(desc, HDR_SHARE_SERIAL);
  247. /* Skip if already shared */
  248. key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
  249. JUMP_COND_SHRD);
  250. if (keys_fit_inline)
  251. append_key_as_imm(desc, ctx->key, ctx->split_key_pad_len,
  252. ctx->split_key_len, CLASS_2 |
  253. KEY_DEST_MDHA_SPLIT | KEY_ENC);
  254. else
  255. append_key(desc, ctx->key_dma, ctx->split_key_len, CLASS_2 |
  256. KEY_DEST_MDHA_SPLIT | KEY_ENC);
  257. set_jump_tgt_here(desc, key_jump_cmd);
  258. /* cryptlen = seqoutlen - authsize */
  259. append_math_sub_imm_u32(desc, REG3, SEQOUTLEN, IMM, ctx->authsize);
  260. /*
  261. * NULL encryption; IV is zero
  262. * assoclen = (assoclen + cryptlen) - cryptlen
  263. */
  264. append_math_sub(desc, VARSEQINLEN, SEQINLEN, REG3, CAAM_CMD_SZ);
  265. /* read assoc before reading payload */
  266. append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_MSG |
  267. KEY_VLF);
  268. /* Prepare to read and write cryptlen bytes */
  269. append_math_add(desc, VARSEQINLEN, ZERO, REG3, CAAM_CMD_SZ);
  270. append_math_add(desc, VARSEQOUTLEN, ZERO, REG3, CAAM_CMD_SZ);
  271. /*
  272. * MOVE_LEN opcode is not available in all SEC HW revisions,
  273. * thus need to do some magic, i.e. self-patch the descriptor
  274. * buffer.
  275. */
  276. read_move_cmd = append_move(desc, MOVE_SRC_DESCBUF |
  277. MOVE_DEST_MATH3 |
  278. (0x6 << MOVE_LEN_SHIFT));
  279. write_move_cmd = append_move(desc, MOVE_SRC_MATH3 |
  280. MOVE_DEST_DESCBUF |
  281. MOVE_WAITCOMP |
  282. (0x8 << MOVE_LEN_SHIFT));
  283. /* Class 2 operation */
  284. append_operation(desc, ctx->class2_alg_type |
  285. OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
  286. /* Read and write cryptlen bytes */
  287. aead_append_src_dst(desc, FIFOLD_TYPE_MSG | FIFOLD_TYPE_FLUSH1);
  288. set_move_tgt_here(desc, read_move_cmd);
  289. set_move_tgt_here(desc, write_move_cmd);
  290. append_cmd(desc, CMD_LOAD | DISABLE_AUTO_INFO_FIFO);
  291. append_move(desc, MOVE_SRC_INFIFO_CL | MOVE_DEST_OUTFIFO |
  292. MOVE_AUX_LS);
  293. /* Write ICV */
  294. append_seq_store(desc, ctx->authsize, LDST_CLASS_2_CCB |
  295. LDST_SRCDST_BYTE_CONTEXT);
  296. ctx->sh_desc_enc_dma = dma_map_single(jrdev, desc,
  297. desc_bytes(desc),
  298. DMA_TO_DEVICE);
  299. if (dma_mapping_error(jrdev, ctx->sh_desc_enc_dma)) {
  300. dev_err(jrdev, "unable to map shared descriptor\n");
  301. return -ENOMEM;
  302. }
  303. #ifdef DEBUG
  304. print_hex_dump(KERN_ERR,
  305. "aead null enc shdesc@"__stringify(__LINE__)": ",
  306. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  307. desc_bytes(desc), 1);
  308. #endif
  309. /*
  310. * Job Descriptor and Shared Descriptors
  311. * must all fit into the 64-word Descriptor h/w Buffer
  312. */
  313. keys_fit_inline = false;
  314. if (DESC_AEAD_NULL_DEC_LEN + DESC_JOB_IO_LEN +
  315. ctx->split_key_pad_len <= CAAM_DESC_BYTES_MAX)
  316. keys_fit_inline = true;
  317. desc = ctx->sh_desc_dec;
  318. /* aead_decrypt shared descriptor */
  319. init_sh_desc(desc, HDR_SHARE_SERIAL);
  320. /* Skip if already shared */
  321. key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
  322. JUMP_COND_SHRD);
  323. if (keys_fit_inline)
  324. append_key_as_imm(desc, ctx->key, ctx->split_key_pad_len,
  325. ctx->split_key_len, CLASS_2 |
  326. KEY_DEST_MDHA_SPLIT | KEY_ENC);
  327. else
  328. append_key(desc, ctx->key_dma, ctx->split_key_len, CLASS_2 |
  329. KEY_DEST_MDHA_SPLIT | KEY_ENC);
  330. set_jump_tgt_here(desc, key_jump_cmd);
  331. /* Class 2 operation */
  332. append_operation(desc, ctx->class2_alg_type |
  333. OP_ALG_AS_INITFINAL | OP_ALG_DECRYPT | OP_ALG_ICV_ON);
  334. /* assoclen + cryptlen = seqinlen - ivsize - authsize */
  335. append_math_sub_imm_u32(desc, REG3, SEQINLEN, IMM,
  336. ctx->authsize + tfm->ivsize);
  337. /* assoclen = (assoclen + cryptlen) - cryptlen */
  338. append_math_sub(desc, REG2, SEQOUTLEN, REG0, CAAM_CMD_SZ);
  339. append_math_sub(desc, VARSEQINLEN, REG3, REG2, CAAM_CMD_SZ);
  340. /* read assoc before reading payload */
  341. append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_MSG |
  342. KEY_VLF);
  343. /* Prepare to read and write cryptlen bytes */
  344. append_math_add(desc, VARSEQINLEN, ZERO, REG2, CAAM_CMD_SZ);
  345. append_math_add(desc, VARSEQOUTLEN, ZERO, REG2, CAAM_CMD_SZ);
  346. /*
  347. * MOVE_LEN opcode is not available in all SEC HW revisions,
  348. * thus need to do some magic, i.e. self-patch the descriptor
  349. * buffer.
  350. */
  351. read_move_cmd = append_move(desc, MOVE_SRC_DESCBUF |
  352. MOVE_DEST_MATH2 |
  353. (0x6 << MOVE_LEN_SHIFT));
  354. write_move_cmd = append_move(desc, MOVE_SRC_MATH2 |
  355. MOVE_DEST_DESCBUF |
  356. MOVE_WAITCOMP |
  357. (0x8 << MOVE_LEN_SHIFT));
  358. /* Read and write cryptlen bytes */
  359. aead_append_src_dst(desc, FIFOLD_TYPE_MSG | FIFOLD_TYPE_FLUSH1);
  360. /*
  361. * Insert a NOP here, since we need at least 4 instructions between
  362. * code patching the descriptor buffer and the location being patched.
  363. */
  364. jump_cmd = append_jump(desc, JUMP_TEST_ALL);
  365. set_jump_tgt_here(desc, jump_cmd);
  366. set_move_tgt_here(desc, read_move_cmd);
  367. set_move_tgt_here(desc, write_move_cmd);
  368. append_cmd(desc, CMD_LOAD | DISABLE_AUTO_INFO_FIFO);
  369. append_move(desc, MOVE_SRC_INFIFO_CL | MOVE_DEST_OUTFIFO |
  370. MOVE_AUX_LS);
  371. append_cmd(desc, CMD_LOAD | ENABLE_AUTO_INFO_FIFO);
  372. /* Load ICV */
  373. append_seq_fifo_load(desc, ctx->authsize, FIFOLD_CLASS_CLASS2 |
  374. FIFOLD_TYPE_LAST2 | FIFOLD_TYPE_ICV);
  375. ctx->sh_desc_dec_dma = dma_map_single(jrdev, desc,
  376. desc_bytes(desc),
  377. DMA_TO_DEVICE);
  378. if (dma_mapping_error(jrdev, ctx->sh_desc_dec_dma)) {
  379. dev_err(jrdev, "unable to map shared descriptor\n");
  380. return -ENOMEM;
  381. }
  382. #ifdef DEBUG
  383. print_hex_dump(KERN_ERR,
  384. "aead null dec shdesc@"__stringify(__LINE__)": ",
  385. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  386. desc_bytes(desc), 1);
  387. #endif
  388. return 0;
  389. }
  390. static int aead_set_sh_desc(struct crypto_aead *aead)
  391. {
  392. struct aead_tfm *tfm = &aead->base.crt_aead;
  393. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  394. struct crypto_tfm *ctfm = crypto_aead_tfm(aead);
  395. const char *alg_name = crypto_tfm_alg_name(ctfm);
  396. struct device *jrdev = ctx->jrdev;
  397. bool keys_fit_inline;
  398. u32 geniv, moveiv;
  399. u32 ctx1_iv_off = 0;
  400. u32 *desc;
  401. const bool ctr_mode = ((ctx->class1_alg_type & OP_ALG_AAI_MASK) ==
  402. OP_ALG_AAI_CTR_MOD128);
  403. const bool is_rfc3686 = (ctr_mode &&
  404. (strstr(alg_name, "rfc3686") != NULL));
  405. if (!ctx->authsize)
  406. return 0;
  407. /* NULL encryption / decryption */
  408. if (!ctx->enckeylen)
  409. return aead_null_set_sh_desc(aead);
  410. /*
  411. * AES-CTR needs to load IV in CONTEXT1 reg
  412. * at an offset of 128bits (16bytes)
  413. * CONTEXT1[255:128] = IV
  414. */
  415. if (ctr_mode)
  416. ctx1_iv_off = 16;
  417. /*
  418. * RFC3686 specific:
  419. * CONTEXT1[255:128] = {NONCE, IV, COUNTER}
  420. */
  421. if (is_rfc3686)
  422. ctx1_iv_off = 16 + CTR_RFC3686_NONCE_SIZE;
  423. /*
  424. * Job Descriptor and Shared Descriptors
  425. * must all fit into the 64-word Descriptor h/w Buffer
  426. */
  427. keys_fit_inline = false;
  428. if (DESC_AEAD_ENC_LEN + DESC_JOB_IO_LEN +
  429. ctx->split_key_pad_len + ctx->enckeylen +
  430. (is_rfc3686 ? DESC_AEAD_CTR_RFC3686_LEN : 0) <=
  431. CAAM_DESC_BYTES_MAX)
  432. keys_fit_inline = true;
  433. /* aead_encrypt shared descriptor */
  434. desc = ctx->sh_desc_enc;
  435. /* Note: Context registers are saved. */
  436. init_sh_desc_key_aead(desc, ctx, keys_fit_inline, is_rfc3686);
  437. /* Class 2 operation */
  438. append_operation(desc, ctx->class2_alg_type |
  439. OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
  440. /* cryptlen = seqoutlen - authsize */
  441. append_math_sub_imm_u32(desc, REG3, SEQOUTLEN, IMM, ctx->authsize);
  442. /* assoclen + cryptlen = seqinlen - ivsize */
  443. append_math_sub_imm_u32(desc, REG2, SEQINLEN, IMM, tfm->ivsize);
  444. /* assoclen = (assoclen + cryptlen) - cryptlen */
  445. append_math_sub(desc, VARSEQINLEN, REG2, REG3, CAAM_CMD_SZ);
  446. /* read assoc before reading payload */
  447. append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_MSG |
  448. KEY_VLF);
  449. aead_append_ld_iv(desc, tfm->ivsize, ctx1_iv_off);
  450. /* Load Counter into CONTEXT1 reg */
  451. if (is_rfc3686)
  452. append_load_imm_u32(desc, be32_to_cpu(1), LDST_IMM |
  453. LDST_CLASS_1_CCB |
  454. LDST_SRCDST_BYTE_CONTEXT |
  455. ((ctx1_iv_off + CTR_RFC3686_IV_SIZE) <<
  456. LDST_OFFSET_SHIFT));
  457. /* Class 1 operation */
  458. append_operation(desc, ctx->class1_alg_type |
  459. OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
  460. /* Read and write cryptlen bytes */
  461. append_math_add(desc, VARSEQINLEN, ZERO, REG3, CAAM_CMD_SZ);
  462. append_math_add(desc, VARSEQOUTLEN, ZERO, REG3, CAAM_CMD_SZ);
  463. aead_append_src_dst(desc, FIFOLD_TYPE_MSG1OUT2);
  464. /* Write ICV */
  465. append_seq_store(desc, ctx->authsize, LDST_CLASS_2_CCB |
  466. LDST_SRCDST_BYTE_CONTEXT);
  467. ctx->sh_desc_enc_dma = dma_map_single(jrdev, desc,
  468. desc_bytes(desc),
  469. DMA_TO_DEVICE);
  470. if (dma_mapping_error(jrdev, ctx->sh_desc_enc_dma)) {
  471. dev_err(jrdev, "unable to map shared descriptor\n");
  472. return -ENOMEM;
  473. }
  474. #ifdef DEBUG
  475. print_hex_dump(KERN_ERR, "aead enc shdesc@"__stringify(__LINE__)": ",
  476. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  477. desc_bytes(desc), 1);
  478. #endif
  479. /*
  480. * Job Descriptor and Shared Descriptors
  481. * must all fit into the 64-word Descriptor h/w Buffer
  482. */
  483. keys_fit_inline = false;
  484. if (DESC_AEAD_DEC_LEN + DESC_JOB_IO_LEN +
  485. ctx->split_key_pad_len + ctx->enckeylen +
  486. (is_rfc3686 ? DESC_AEAD_CTR_RFC3686_LEN : 0) <=
  487. CAAM_DESC_BYTES_MAX)
  488. keys_fit_inline = true;
  489. /* aead_decrypt shared descriptor */
  490. desc = ctx->sh_desc_dec;
  491. /* Note: Context registers are saved. */
  492. init_sh_desc_key_aead(desc, ctx, keys_fit_inline, is_rfc3686);
  493. /* Class 2 operation */
  494. append_operation(desc, ctx->class2_alg_type |
  495. OP_ALG_AS_INITFINAL | OP_ALG_DECRYPT | OP_ALG_ICV_ON);
  496. /* assoclen + cryptlen = seqinlen - ivsize - authsize */
  497. append_math_sub_imm_u32(desc, REG3, SEQINLEN, IMM,
  498. ctx->authsize + tfm->ivsize);
  499. /* assoclen = (assoclen + cryptlen) - cryptlen */
  500. append_math_sub(desc, REG2, SEQOUTLEN, REG0, CAAM_CMD_SZ);
  501. append_math_sub(desc, VARSEQINLEN, REG3, REG2, CAAM_CMD_SZ);
  502. /* read assoc before reading payload */
  503. append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_MSG |
  504. KEY_VLF);
  505. aead_append_ld_iv(desc, tfm->ivsize, ctx1_iv_off);
  506. /* Load Counter into CONTEXT1 reg */
  507. if (is_rfc3686)
  508. append_load_imm_u32(desc, be32_to_cpu(1), LDST_IMM |
  509. LDST_CLASS_1_CCB |
  510. LDST_SRCDST_BYTE_CONTEXT |
  511. ((ctx1_iv_off + CTR_RFC3686_IV_SIZE) <<
  512. LDST_OFFSET_SHIFT));
  513. /* Choose operation */
  514. if (ctr_mode)
  515. append_operation(desc, ctx->class1_alg_type |
  516. OP_ALG_AS_INITFINAL | OP_ALG_DECRYPT);
  517. else
  518. append_dec_op1(desc, ctx->class1_alg_type);
  519. /* Read and write cryptlen bytes */
  520. append_math_add(desc, VARSEQINLEN, ZERO, REG2, CAAM_CMD_SZ);
  521. append_math_add(desc, VARSEQOUTLEN, ZERO, REG2, CAAM_CMD_SZ);
  522. aead_append_src_dst(desc, FIFOLD_TYPE_MSG);
  523. /* Load ICV */
  524. append_seq_fifo_load(desc, ctx->authsize, FIFOLD_CLASS_CLASS2 |
  525. FIFOLD_TYPE_LAST2 | FIFOLD_TYPE_ICV);
  526. ctx->sh_desc_dec_dma = dma_map_single(jrdev, desc,
  527. desc_bytes(desc),
  528. DMA_TO_DEVICE);
  529. if (dma_mapping_error(jrdev, ctx->sh_desc_dec_dma)) {
  530. dev_err(jrdev, "unable to map shared descriptor\n");
  531. return -ENOMEM;
  532. }
  533. #ifdef DEBUG
  534. print_hex_dump(KERN_ERR, "aead dec shdesc@"__stringify(__LINE__)": ",
  535. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  536. desc_bytes(desc), 1);
  537. #endif
  538. /*
  539. * Job Descriptor and Shared Descriptors
  540. * must all fit into the 64-word Descriptor h/w Buffer
  541. */
  542. keys_fit_inline = false;
  543. if (DESC_AEAD_GIVENC_LEN + DESC_JOB_IO_LEN +
  544. ctx->split_key_pad_len + ctx->enckeylen +
  545. (is_rfc3686 ? DESC_AEAD_CTR_RFC3686_LEN : 0) <=
  546. CAAM_DESC_BYTES_MAX)
  547. keys_fit_inline = true;
  548. /* aead_givencrypt shared descriptor */
  549. desc = ctx->sh_desc_givenc;
  550. /* Note: Context registers are saved. */
  551. init_sh_desc_key_aead(desc, ctx, keys_fit_inline, is_rfc3686);
  552. /* Generate IV */
  553. geniv = NFIFOENTRY_STYPE_PAD | NFIFOENTRY_DEST_DECO |
  554. NFIFOENTRY_DTYPE_MSG | NFIFOENTRY_LC1 |
  555. NFIFOENTRY_PTYPE_RND | (tfm->ivsize << NFIFOENTRY_DLEN_SHIFT);
  556. append_load_imm_u32(desc, geniv, LDST_CLASS_IND_CCB |
  557. LDST_SRCDST_WORD_INFO_FIFO | LDST_IMM);
  558. append_cmd(desc, CMD_LOAD | DISABLE_AUTO_INFO_FIFO);
  559. append_move(desc, MOVE_WAITCOMP |
  560. MOVE_SRC_INFIFO | MOVE_DEST_CLASS1CTX |
  561. (ctx1_iv_off << MOVE_OFFSET_SHIFT) |
  562. (tfm->ivsize << MOVE_LEN_SHIFT));
  563. append_cmd(desc, CMD_LOAD | ENABLE_AUTO_INFO_FIFO);
  564. /* Copy IV to class 1 context */
  565. append_move(desc, MOVE_SRC_CLASS1CTX | MOVE_DEST_OUTFIFO |
  566. (ctx1_iv_off << MOVE_OFFSET_SHIFT) |
  567. (tfm->ivsize << MOVE_LEN_SHIFT));
  568. /* Return to encryption */
  569. append_operation(desc, ctx->class2_alg_type |
  570. OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
  571. /* ivsize + cryptlen = seqoutlen - authsize */
  572. append_math_sub_imm_u32(desc, REG3, SEQOUTLEN, IMM, ctx->authsize);
  573. /* assoclen = seqinlen - (ivsize + cryptlen) */
  574. append_math_sub(desc, VARSEQINLEN, SEQINLEN, REG3, CAAM_CMD_SZ);
  575. /* read assoc before reading payload */
  576. append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_MSG |
  577. KEY_VLF);
  578. /* Copy iv from outfifo to class 2 fifo */
  579. moveiv = NFIFOENTRY_STYPE_OFIFO | NFIFOENTRY_DEST_CLASS2 |
  580. NFIFOENTRY_DTYPE_MSG | (tfm->ivsize << NFIFOENTRY_DLEN_SHIFT);
  581. append_load_imm_u32(desc, moveiv, LDST_CLASS_IND_CCB |
  582. LDST_SRCDST_WORD_INFO_FIFO | LDST_IMM);
  583. append_load_imm_u32(desc, tfm->ivsize, LDST_CLASS_2_CCB |
  584. LDST_SRCDST_WORD_DATASZ_REG | LDST_IMM);
  585. /* Load Counter into CONTEXT1 reg */
  586. if (is_rfc3686)
  587. append_load_imm_u32(desc, be32_to_cpu(1), LDST_IMM |
  588. LDST_CLASS_1_CCB |
  589. LDST_SRCDST_BYTE_CONTEXT |
  590. ((ctx1_iv_off + CTR_RFC3686_IV_SIZE) <<
  591. LDST_OFFSET_SHIFT));
  592. /* Class 1 operation */
  593. append_operation(desc, ctx->class1_alg_type |
  594. OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
  595. /* Will write ivsize + cryptlen */
  596. append_math_add(desc, VARSEQOUTLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
  597. /* Not need to reload iv */
  598. append_seq_fifo_load(desc, tfm->ivsize,
  599. FIFOLD_CLASS_SKIP);
  600. /* Will read cryptlen */
  601. append_math_add(desc, VARSEQINLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
  602. aead_append_src_dst(desc, FIFOLD_TYPE_MSG1OUT2);
  603. /* Write ICV */
  604. append_seq_store(desc, ctx->authsize, LDST_CLASS_2_CCB |
  605. LDST_SRCDST_BYTE_CONTEXT);
  606. ctx->sh_desc_givenc_dma = dma_map_single(jrdev, desc,
  607. desc_bytes(desc),
  608. DMA_TO_DEVICE);
  609. if (dma_mapping_error(jrdev, ctx->sh_desc_givenc_dma)) {
  610. dev_err(jrdev, "unable to map shared descriptor\n");
  611. return -ENOMEM;
  612. }
  613. #ifdef DEBUG
  614. print_hex_dump(KERN_ERR, "aead givenc shdesc@"__stringify(__LINE__)": ",
  615. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  616. desc_bytes(desc), 1);
  617. #endif
  618. return 0;
  619. }
  620. static int aead_setauthsize(struct crypto_aead *authenc,
  621. unsigned int authsize)
  622. {
  623. struct caam_ctx *ctx = crypto_aead_ctx(authenc);
  624. ctx->authsize = authsize;
  625. aead_set_sh_desc(authenc);
  626. return 0;
  627. }
  628. static int gcm_set_sh_desc(struct crypto_aead *aead)
  629. {
  630. struct aead_tfm *tfm = &aead->base.crt_aead;
  631. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  632. struct device *jrdev = ctx->jrdev;
  633. bool keys_fit_inline = false;
  634. u32 *key_jump_cmd, *zero_payload_jump_cmd,
  635. *zero_assoc_jump_cmd1, *zero_assoc_jump_cmd2;
  636. u32 *desc;
  637. if (!ctx->enckeylen || !ctx->authsize)
  638. return 0;
  639. /*
  640. * AES GCM encrypt shared descriptor
  641. * Job Descriptor and Shared Descriptor
  642. * must fit into the 64-word Descriptor h/w Buffer
  643. */
  644. if (DESC_GCM_ENC_LEN + DESC_JOB_IO_LEN +
  645. ctx->enckeylen <= CAAM_DESC_BYTES_MAX)
  646. keys_fit_inline = true;
  647. desc = ctx->sh_desc_enc;
  648. init_sh_desc(desc, HDR_SHARE_SERIAL);
  649. /* skip key loading if they are loaded due to sharing */
  650. key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
  651. JUMP_COND_SHRD | JUMP_COND_SELF);
  652. if (keys_fit_inline)
  653. append_key_as_imm(desc, (void *)ctx->key, ctx->enckeylen,
  654. ctx->enckeylen, CLASS_1 | KEY_DEST_CLASS_REG);
  655. else
  656. append_key(desc, ctx->key_dma, ctx->enckeylen,
  657. CLASS_1 | KEY_DEST_CLASS_REG);
  658. set_jump_tgt_here(desc, key_jump_cmd);
  659. /* class 1 operation */
  660. append_operation(desc, ctx->class1_alg_type |
  661. OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
  662. /* cryptlen = seqoutlen - authsize */
  663. append_math_sub_imm_u32(desc, REG3, SEQOUTLEN, IMM, ctx->authsize);
  664. /* assoclen + cryptlen = seqinlen - ivsize */
  665. append_math_sub_imm_u32(desc, REG2, SEQINLEN, IMM, tfm->ivsize);
  666. /* assoclen = (assoclen + cryptlen) - cryptlen */
  667. append_math_sub(desc, REG1, REG2, REG3, CAAM_CMD_SZ);
  668. /* if cryptlen is ZERO jump to zero-payload commands */
  669. append_math_add(desc, VARSEQOUTLEN, ZERO, REG3, CAAM_CMD_SZ);
  670. zero_payload_jump_cmd = append_jump(desc, JUMP_TEST_ALL |
  671. JUMP_COND_MATH_Z);
  672. /* read IV */
  673. append_seq_fifo_load(desc, tfm->ivsize, FIFOLD_CLASS_CLASS1 |
  674. FIFOLD_TYPE_IV | FIFOLD_TYPE_FLUSH1);
  675. /* if assoclen is ZERO, skip reading the assoc data */
  676. append_math_add(desc, VARSEQINLEN, ZERO, REG1, CAAM_CMD_SZ);
  677. zero_assoc_jump_cmd1 = append_jump(desc, JUMP_TEST_ALL |
  678. JUMP_COND_MATH_Z);
  679. /* read assoc data */
  680. append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 | FIFOLDST_VLF |
  681. FIFOLD_TYPE_AAD | FIFOLD_TYPE_FLUSH1);
  682. set_jump_tgt_here(desc, zero_assoc_jump_cmd1);
  683. append_math_add(desc, VARSEQINLEN, ZERO, REG3, CAAM_CMD_SZ);
  684. /* write encrypted data */
  685. append_seq_fifo_store(desc, 0, FIFOST_TYPE_MESSAGE_DATA | FIFOLDST_VLF);
  686. /* read payload data */
  687. append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 | FIFOLDST_VLF |
  688. FIFOLD_TYPE_MSG | FIFOLD_TYPE_LAST1);
  689. /* jump the zero-payload commands */
  690. append_jump(desc, JUMP_TEST_ALL | 7);
  691. /* zero-payload commands */
  692. set_jump_tgt_here(desc, zero_payload_jump_cmd);
  693. /* if assoclen is ZERO, jump to IV reading - is the only input data */
  694. append_math_add(desc, VARSEQINLEN, ZERO, REG1, CAAM_CMD_SZ);
  695. zero_assoc_jump_cmd2 = append_jump(desc, JUMP_TEST_ALL |
  696. JUMP_COND_MATH_Z);
  697. /* read IV */
  698. append_seq_fifo_load(desc, tfm->ivsize, FIFOLD_CLASS_CLASS1 |
  699. FIFOLD_TYPE_IV | FIFOLD_TYPE_FLUSH1);
  700. /* read assoc data */
  701. append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 | FIFOLDST_VLF |
  702. FIFOLD_TYPE_AAD | FIFOLD_TYPE_LAST1);
  703. /* jump to ICV writing */
  704. append_jump(desc, JUMP_TEST_ALL | 2);
  705. /* read IV - is the only input data */
  706. set_jump_tgt_here(desc, zero_assoc_jump_cmd2);
  707. append_seq_fifo_load(desc, tfm->ivsize, FIFOLD_CLASS_CLASS1 |
  708. FIFOLD_TYPE_IV | FIFOLD_TYPE_FLUSH1 |
  709. FIFOLD_TYPE_LAST1);
  710. /* write ICV */
  711. append_seq_store(desc, ctx->authsize, LDST_CLASS_1_CCB |
  712. LDST_SRCDST_BYTE_CONTEXT);
  713. ctx->sh_desc_enc_dma = dma_map_single(jrdev, desc,
  714. desc_bytes(desc),
  715. DMA_TO_DEVICE);
  716. if (dma_mapping_error(jrdev, ctx->sh_desc_enc_dma)) {
  717. dev_err(jrdev, "unable to map shared descriptor\n");
  718. return -ENOMEM;
  719. }
  720. #ifdef DEBUG
  721. print_hex_dump(KERN_ERR, "gcm enc shdesc@"__stringify(__LINE__)": ",
  722. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  723. desc_bytes(desc), 1);
  724. #endif
  725. /*
  726. * Job Descriptor and Shared Descriptors
  727. * must all fit into the 64-word Descriptor h/w Buffer
  728. */
  729. keys_fit_inline = false;
  730. if (DESC_GCM_DEC_LEN + DESC_JOB_IO_LEN +
  731. ctx->enckeylen <= CAAM_DESC_BYTES_MAX)
  732. keys_fit_inline = true;
  733. desc = ctx->sh_desc_dec;
  734. init_sh_desc(desc, HDR_SHARE_SERIAL);
  735. /* skip key loading if they are loaded due to sharing */
  736. key_jump_cmd = append_jump(desc, JUMP_JSL |
  737. JUMP_TEST_ALL | JUMP_COND_SHRD |
  738. JUMP_COND_SELF);
  739. if (keys_fit_inline)
  740. append_key_as_imm(desc, (void *)ctx->key, ctx->enckeylen,
  741. ctx->enckeylen, CLASS_1 | KEY_DEST_CLASS_REG);
  742. else
  743. append_key(desc, ctx->key_dma, ctx->enckeylen,
  744. CLASS_1 | KEY_DEST_CLASS_REG);
  745. set_jump_tgt_here(desc, key_jump_cmd);
  746. /* class 1 operation */
  747. append_operation(desc, ctx->class1_alg_type |
  748. OP_ALG_AS_INITFINAL | OP_ALG_DECRYPT | OP_ALG_ICV_ON);
  749. /* assoclen + cryptlen = seqinlen - ivsize - icvsize */
  750. append_math_sub_imm_u32(desc, REG3, SEQINLEN, IMM,
  751. ctx->authsize + tfm->ivsize);
  752. /* assoclen = (assoclen + cryptlen) - cryptlen */
  753. append_math_sub(desc, REG2, SEQOUTLEN, REG0, CAAM_CMD_SZ);
  754. append_math_sub(desc, REG1, REG3, REG2, CAAM_CMD_SZ);
  755. /* read IV */
  756. append_seq_fifo_load(desc, tfm->ivsize, FIFOLD_CLASS_CLASS1 |
  757. FIFOLD_TYPE_IV | FIFOLD_TYPE_FLUSH1);
  758. /* jump to zero-payload command if cryptlen is zero */
  759. append_math_add(desc, VARSEQOUTLEN, ZERO, REG2, CAAM_CMD_SZ);
  760. zero_payload_jump_cmd = append_jump(desc, JUMP_TEST_ALL |
  761. JUMP_COND_MATH_Z);
  762. append_math_add(desc, VARSEQINLEN, ZERO, REG1, CAAM_CMD_SZ);
  763. /* if asoclen is ZERO, skip reading assoc data */
  764. zero_assoc_jump_cmd1 = append_jump(desc, JUMP_TEST_ALL |
  765. JUMP_COND_MATH_Z);
  766. /* read assoc data */
  767. append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 | FIFOLDST_VLF |
  768. FIFOLD_TYPE_AAD | FIFOLD_TYPE_FLUSH1);
  769. set_jump_tgt_here(desc, zero_assoc_jump_cmd1);
  770. append_math_add(desc, VARSEQINLEN, ZERO, REG2, CAAM_CMD_SZ);
  771. /* store encrypted data */
  772. append_seq_fifo_store(desc, 0, FIFOST_TYPE_MESSAGE_DATA | FIFOLDST_VLF);
  773. /* read payload data */
  774. append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 | FIFOLDST_VLF |
  775. FIFOLD_TYPE_MSG | FIFOLD_TYPE_FLUSH1);
  776. /* jump the zero-payload commands */
  777. append_jump(desc, JUMP_TEST_ALL | 4);
  778. /* zero-payload command */
  779. set_jump_tgt_here(desc, zero_payload_jump_cmd);
  780. /* if assoclen is ZERO, jump to ICV reading */
  781. append_math_add(desc, VARSEQINLEN, ZERO, REG1, CAAM_CMD_SZ);
  782. zero_assoc_jump_cmd2 = append_jump(desc, JUMP_TEST_ALL |
  783. JUMP_COND_MATH_Z);
  784. /* read assoc data */
  785. append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 | FIFOLDST_VLF |
  786. FIFOLD_TYPE_AAD | FIFOLD_TYPE_FLUSH1);
  787. set_jump_tgt_here(desc, zero_assoc_jump_cmd2);
  788. /* read ICV */
  789. append_seq_fifo_load(desc, ctx->authsize, FIFOLD_CLASS_CLASS1 |
  790. FIFOLD_TYPE_ICV | FIFOLD_TYPE_LAST1);
  791. ctx->sh_desc_dec_dma = dma_map_single(jrdev, desc,
  792. desc_bytes(desc),
  793. DMA_TO_DEVICE);
  794. if (dma_mapping_error(jrdev, ctx->sh_desc_dec_dma)) {
  795. dev_err(jrdev, "unable to map shared descriptor\n");
  796. return -ENOMEM;
  797. }
  798. #ifdef DEBUG
  799. print_hex_dump(KERN_ERR, "gcm dec shdesc@"__stringify(__LINE__)": ",
  800. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  801. desc_bytes(desc), 1);
  802. #endif
  803. return 0;
  804. }
  805. static int gcm_setauthsize(struct crypto_aead *authenc, unsigned int authsize)
  806. {
  807. struct caam_ctx *ctx = crypto_aead_ctx(authenc);
  808. ctx->authsize = authsize;
  809. gcm_set_sh_desc(authenc);
  810. return 0;
  811. }
  812. static int rfc4106_set_sh_desc(struct crypto_aead *aead)
  813. {
  814. struct aead_tfm *tfm = &aead->base.crt_aead;
  815. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  816. struct device *jrdev = ctx->jrdev;
  817. bool keys_fit_inline = false;
  818. u32 *key_jump_cmd, *move_cmd, *write_iv_cmd;
  819. u32 *desc;
  820. u32 geniv;
  821. if (!ctx->enckeylen || !ctx->authsize)
  822. return 0;
  823. /*
  824. * RFC4106 encrypt shared descriptor
  825. * Job Descriptor and Shared Descriptor
  826. * must fit into the 64-word Descriptor h/w Buffer
  827. */
  828. if (DESC_RFC4106_ENC_LEN + DESC_JOB_IO_LEN +
  829. ctx->enckeylen <= CAAM_DESC_BYTES_MAX)
  830. keys_fit_inline = true;
  831. desc = ctx->sh_desc_enc;
  832. init_sh_desc(desc, HDR_SHARE_SERIAL);
  833. /* Skip key loading if it is loaded due to sharing */
  834. key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
  835. JUMP_COND_SHRD);
  836. if (keys_fit_inline)
  837. append_key_as_imm(desc, (void *)ctx->key, ctx->enckeylen,
  838. ctx->enckeylen, CLASS_1 | KEY_DEST_CLASS_REG);
  839. else
  840. append_key(desc, ctx->key_dma, ctx->enckeylen,
  841. CLASS_1 | KEY_DEST_CLASS_REG);
  842. set_jump_tgt_here(desc, key_jump_cmd);
  843. /* Class 1 operation */
  844. append_operation(desc, ctx->class1_alg_type |
  845. OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
  846. /* cryptlen = seqoutlen - authsize */
  847. append_math_sub_imm_u32(desc, REG3, SEQOUTLEN, IMM, ctx->authsize);
  848. append_math_add(desc, VARSEQOUTLEN, ZERO, REG3, CAAM_CMD_SZ);
  849. /* assoclen + cryptlen = seqinlen - ivsize */
  850. append_math_sub_imm_u32(desc, REG2, SEQINLEN, IMM, tfm->ivsize);
  851. /* assoclen = (assoclen + cryptlen) - cryptlen */
  852. append_math_sub(desc, VARSEQINLEN, REG2, REG3, CAAM_CMD_SZ);
  853. /* Read Salt */
  854. append_fifo_load_as_imm(desc, (void *)(ctx->key + ctx->enckeylen),
  855. 4, FIFOLD_CLASS_CLASS1 | FIFOLD_TYPE_IV);
  856. /* Read AES-GCM-ESP IV */
  857. append_seq_fifo_load(desc, tfm->ivsize, FIFOLD_CLASS_CLASS1 |
  858. FIFOLD_TYPE_IV | FIFOLD_TYPE_FLUSH1);
  859. /* Read assoc data */
  860. append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 | FIFOLDST_VLF |
  861. FIFOLD_TYPE_AAD | FIFOLD_TYPE_FLUSH1);
  862. /* Will read cryptlen bytes */
  863. append_math_add(desc, VARSEQINLEN, ZERO, REG3, CAAM_CMD_SZ);
  864. /* Write encrypted data */
  865. append_seq_fifo_store(desc, 0, FIFOST_TYPE_MESSAGE_DATA | FIFOLDST_VLF);
  866. /* Read payload data */
  867. append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 | FIFOLDST_VLF |
  868. FIFOLD_TYPE_MSG | FIFOLD_TYPE_LAST1);
  869. /* Write ICV */
  870. append_seq_store(desc, ctx->authsize, LDST_CLASS_1_CCB |
  871. LDST_SRCDST_BYTE_CONTEXT);
  872. ctx->sh_desc_enc_dma = dma_map_single(jrdev, desc,
  873. desc_bytes(desc),
  874. DMA_TO_DEVICE);
  875. if (dma_mapping_error(jrdev, ctx->sh_desc_enc_dma)) {
  876. dev_err(jrdev, "unable to map shared descriptor\n");
  877. return -ENOMEM;
  878. }
  879. #ifdef DEBUG
  880. print_hex_dump(KERN_ERR, "rfc4106 enc shdesc@"__stringify(__LINE__)": ",
  881. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  882. desc_bytes(desc), 1);
  883. #endif
  884. /*
  885. * Job Descriptor and Shared Descriptors
  886. * must all fit into the 64-word Descriptor h/w Buffer
  887. */
  888. keys_fit_inline = false;
  889. if (DESC_RFC4106_DEC_LEN + DESC_JOB_IO_LEN +
  890. ctx->enckeylen <= CAAM_DESC_BYTES_MAX)
  891. keys_fit_inline = true;
  892. desc = ctx->sh_desc_dec;
  893. init_sh_desc(desc, HDR_SHARE_SERIAL);
  894. /* Skip key loading if it is loaded due to sharing */
  895. key_jump_cmd = append_jump(desc, JUMP_JSL |
  896. JUMP_TEST_ALL | JUMP_COND_SHRD);
  897. if (keys_fit_inline)
  898. append_key_as_imm(desc, (void *)ctx->key, ctx->enckeylen,
  899. ctx->enckeylen, CLASS_1 | KEY_DEST_CLASS_REG);
  900. else
  901. append_key(desc, ctx->key_dma, ctx->enckeylen,
  902. CLASS_1 | KEY_DEST_CLASS_REG);
  903. set_jump_tgt_here(desc, key_jump_cmd);
  904. /* Class 1 operation */
  905. append_operation(desc, ctx->class1_alg_type |
  906. OP_ALG_AS_INITFINAL | OP_ALG_DECRYPT | OP_ALG_ICV_ON);
  907. /* assoclen + cryptlen = seqinlen - ivsize - icvsize */
  908. append_math_sub_imm_u32(desc, REG3, SEQINLEN, IMM,
  909. ctx->authsize + tfm->ivsize);
  910. /* assoclen = (assoclen + cryptlen) - cryptlen */
  911. append_math_sub(desc, REG2, SEQOUTLEN, REG0, CAAM_CMD_SZ);
  912. append_math_sub(desc, VARSEQINLEN, REG3, REG2, CAAM_CMD_SZ);
  913. /* Will write cryptlen bytes */
  914. append_math_sub(desc, VARSEQOUTLEN, SEQOUTLEN, REG0, CAAM_CMD_SZ);
  915. /* Read Salt */
  916. append_fifo_load_as_imm(desc, (void *)(ctx->key + ctx->enckeylen),
  917. 4, FIFOLD_CLASS_CLASS1 | FIFOLD_TYPE_IV);
  918. /* Read AES-GCM-ESP IV */
  919. append_seq_fifo_load(desc, tfm->ivsize, FIFOLD_CLASS_CLASS1 |
  920. FIFOLD_TYPE_IV | FIFOLD_TYPE_FLUSH1);
  921. /* Read assoc data */
  922. append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 | FIFOLDST_VLF |
  923. FIFOLD_TYPE_AAD | FIFOLD_TYPE_FLUSH1);
  924. /* Will read cryptlen bytes */
  925. append_math_add(desc, VARSEQINLEN, ZERO, REG2, CAAM_CMD_SZ);
  926. /* Store payload data */
  927. append_seq_fifo_store(desc, 0, FIFOST_TYPE_MESSAGE_DATA | FIFOLDST_VLF);
  928. /* Read encrypted data */
  929. append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 | FIFOLDST_VLF |
  930. FIFOLD_TYPE_MSG | FIFOLD_TYPE_FLUSH1);
  931. /* Read ICV */
  932. append_seq_fifo_load(desc, ctx->authsize, FIFOLD_CLASS_CLASS1 |
  933. FIFOLD_TYPE_ICV | FIFOLD_TYPE_LAST1);
  934. ctx->sh_desc_dec_dma = dma_map_single(jrdev, desc,
  935. desc_bytes(desc),
  936. DMA_TO_DEVICE);
  937. if (dma_mapping_error(jrdev, ctx->sh_desc_dec_dma)) {
  938. dev_err(jrdev, "unable to map shared descriptor\n");
  939. return -ENOMEM;
  940. }
  941. #ifdef DEBUG
  942. print_hex_dump(KERN_ERR, "rfc4106 dec shdesc@"__stringify(__LINE__)": ",
  943. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  944. desc_bytes(desc), 1);
  945. #endif
  946. /*
  947. * Job Descriptor and Shared Descriptors
  948. * must all fit into the 64-word Descriptor h/w Buffer
  949. */
  950. keys_fit_inline = false;
  951. if (DESC_RFC4106_GIVENC_LEN + DESC_JOB_IO_LEN +
  952. ctx->split_key_pad_len + ctx->enckeylen <=
  953. CAAM_DESC_BYTES_MAX)
  954. keys_fit_inline = true;
  955. /* rfc4106_givencrypt shared descriptor */
  956. desc = ctx->sh_desc_givenc;
  957. init_sh_desc(desc, HDR_SHARE_SERIAL);
  958. /* Skip key loading if it is loaded due to sharing */
  959. key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
  960. JUMP_COND_SHRD);
  961. if (keys_fit_inline)
  962. append_key_as_imm(desc, (void *)ctx->key, ctx->enckeylen,
  963. ctx->enckeylen, CLASS_1 | KEY_DEST_CLASS_REG);
  964. else
  965. append_key(desc, ctx->key_dma, ctx->enckeylen,
  966. CLASS_1 | KEY_DEST_CLASS_REG);
  967. set_jump_tgt_here(desc, key_jump_cmd);
  968. /* Generate IV */
  969. geniv = NFIFOENTRY_STYPE_PAD | NFIFOENTRY_DEST_DECO |
  970. NFIFOENTRY_DTYPE_MSG | NFIFOENTRY_LC1 |
  971. NFIFOENTRY_PTYPE_RND | (tfm->ivsize << NFIFOENTRY_DLEN_SHIFT);
  972. append_load_imm_u32(desc, geniv, LDST_CLASS_IND_CCB |
  973. LDST_SRCDST_WORD_INFO_FIFO | LDST_IMM);
  974. append_cmd(desc, CMD_LOAD | DISABLE_AUTO_INFO_FIFO);
  975. move_cmd = append_move(desc, MOVE_SRC_INFIFO | MOVE_DEST_DESCBUF |
  976. (tfm->ivsize << MOVE_LEN_SHIFT));
  977. append_cmd(desc, CMD_LOAD | ENABLE_AUTO_INFO_FIFO);
  978. /* Copy generated IV to OFIFO */
  979. write_iv_cmd = append_move(desc, MOVE_SRC_DESCBUF | MOVE_DEST_OUTFIFO |
  980. (tfm->ivsize << MOVE_LEN_SHIFT));
  981. /* Class 1 operation */
  982. append_operation(desc, ctx->class1_alg_type |
  983. OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
  984. /* ivsize + cryptlen = seqoutlen - authsize */
  985. append_math_sub_imm_u32(desc, REG3, SEQOUTLEN, IMM, ctx->authsize);
  986. /* assoclen = seqinlen - (ivsize + cryptlen) */
  987. append_math_sub(desc, VARSEQINLEN, SEQINLEN, REG3, CAAM_CMD_SZ);
  988. /* Will write ivsize + cryptlen */
  989. append_math_add(desc, VARSEQOUTLEN, REG3, REG0, CAAM_CMD_SZ);
  990. /* Read Salt and generated IV */
  991. append_cmd(desc, CMD_FIFO_LOAD | FIFOLD_CLASS_CLASS1 | FIFOLD_TYPE_IV |
  992. FIFOLD_TYPE_FLUSH1 | IMMEDIATE | 12);
  993. /* Append Salt */
  994. append_data(desc, (void *)(ctx->key + ctx->enckeylen), 4);
  995. set_move_tgt_here(desc, move_cmd);
  996. set_move_tgt_here(desc, write_iv_cmd);
  997. /* Blank commands. Will be overwritten by generated IV. */
  998. append_cmd(desc, 0x00000000);
  999. append_cmd(desc, 0x00000000);
  1000. /* End of blank commands */
  1001. /* No need to reload iv */
  1002. append_seq_fifo_load(desc, tfm->ivsize, FIFOLD_CLASS_SKIP);
  1003. /* Read assoc data */
  1004. append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 | FIFOLDST_VLF |
  1005. FIFOLD_TYPE_AAD | FIFOLD_TYPE_FLUSH1);
  1006. /* Will read cryptlen */
  1007. append_math_add(desc, VARSEQINLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
  1008. /* Store generated IV and encrypted data */
  1009. append_seq_fifo_store(desc, 0, FIFOST_TYPE_MESSAGE_DATA | FIFOLDST_VLF);
  1010. /* Read payload data */
  1011. append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 | FIFOLDST_VLF |
  1012. FIFOLD_TYPE_MSG | FIFOLD_TYPE_LAST1);
  1013. /* Write ICV */
  1014. append_seq_store(desc, ctx->authsize, LDST_CLASS_1_CCB |
  1015. LDST_SRCDST_BYTE_CONTEXT);
  1016. ctx->sh_desc_givenc_dma = dma_map_single(jrdev, desc,
  1017. desc_bytes(desc),
  1018. DMA_TO_DEVICE);
  1019. if (dma_mapping_error(jrdev, ctx->sh_desc_givenc_dma)) {
  1020. dev_err(jrdev, "unable to map shared descriptor\n");
  1021. return -ENOMEM;
  1022. }
  1023. #ifdef DEBUG
  1024. print_hex_dump(KERN_ERR,
  1025. "rfc4106 givenc shdesc@"__stringify(__LINE__)": ",
  1026. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  1027. desc_bytes(desc), 1);
  1028. #endif
  1029. return 0;
  1030. }
  1031. static int rfc4106_setauthsize(struct crypto_aead *authenc,
  1032. unsigned int authsize)
  1033. {
  1034. struct caam_ctx *ctx = crypto_aead_ctx(authenc);
  1035. ctx->authsize = authsize;
  1036. rfc4106_set_sh_desc(authenc);
  1037. return 0;
  1038. }
  1039. static int rfc4543_set_sh_desc(struct crypto_aead *aead)
  1040. {
  1041. struct aead_tfm *tfm = &aead->base.crt_aead;
  1042. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  1043. struct device *jrdev = ctx->jrdev;
  1044. bool keys_fit_inline = false;
  1045. u32 *key_jump_cmd, *write_iv_cmd, *write_aad_cmd;
  1046. u32 *read_move_cmd, *write_move_cmd;
  1047. u32 *desc;
  1048. u32 geniv;
  1049. if (!ctx->enckeylen || !ctx->authsize)
  1050. return 0;
  1051. /*
  1052. * RFC4543 encrypt shared descriptor
  1053. * Job Descriptor and Shared Descriptor
  1054. * must fit into the 64-word Descriptor h/w Buffer
  1055. */
  1056. if (DESC_RFC4543_ENC_LEN + DESC_JOB_IO_LEN +
  1057. ctx->enckeylen <= CAAM_DESC_BYTES_MAX)
  1058. keys_fit_inline = true;
  1059. desc = ctx->sh_desc_enc;
  1060. init_sh_desc(desc, HDR_SHARE_SERIAL);
  1061. /* Skip key loading if it is loaded due to sharing */
  1062. key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
  1063. JUMP_COND_SHRD);
  1064. if (keys_fit_inline)
  1065. append_key_as_imm(desc, (void *)ctx->key, ctx->enckeylen,
  1066. ctx->enckeylen, CLASS_1 | KEY_DEST_CLASS_REG);
  1067. else
  1068. append_key(desc, ctx->key_dma, ctx->enckeylen,
  1069. CLASS_1 | KEY_DEST_CLASS_REG);
  1070. set_jump_tgt_here(desc, key_jump_cmd);
  1071. /* Class 1 operation */
  1072. append_operation(desc, ctx->class1_alg_type |
  1073. OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
  1074. /* Load AES-GMAC ESP IV into Math1 register */
  1075. append_cmd(desc, CMD_SEQ_LOAD | LDST_SRCDST_WORD_DECO_MATH1 |
  1076. LDST_CLASS_DECO | tfm->ivsize);
  1077. /* Wait the DMA transaction to finish */
  1078. append_jump(desc, JUMP_TEST_ALL | JUMP_COND_CALM |
  1079. (1 << JUMP_OFFSET_SHIFT));
  1080. /* Overwrite blank immediate AES-GMAC ESP IV data */
  1081. write_iv_cmd = append_move(desc, MOVE_SRC_MATH1 | MOVE_DEST_DESCBUF |
  1082. (tfm->ivsize << MOVE_LEN_SHIFT));
  1083. /* Overwrite blank immediate AAD data */
  1084. write_aad_cmd = append_move(desc, MOVE_SRC_MATH1 | MOVE_DEST_DESCBUF |
  1085. (tfm->ivsize << MOVE_LEN_SHIFT));
  1086. /* cryptlen = seqoutlen - authsize */
  1087. append_math_sub_imm_u32(desc, REG3, SEQOUTLEN, IMM, ctx->authsize);
  1088. /* assoclen = (seqinlen - ivsize) - cryptlen */
  1089. append_math_sub(desc, VARSEQINLEN, SEQINLEN, REG3, CAAM_CMD_SZ);
  1090. /* Read Salt and AES-GMAC ESP IV */
  1091. append_cmd(desc, CMD_FIFO_LOAD | FIFOLD_CLASS_CLASS1 | IMMEDIATE |
  1092. FIFOLD_TYPE_IV | FIFOLD_TYPE_FLUSH1 | (4 + tfm->ivsize));
  1093. /* Append Salt */
  1094. append_data(desc, (void *)(ctx->key + ctx->enckeylen), 4);
  1095. set_move_tgt_here(desc, write_iv_cmd);
  1096. /* Blank commands. Will be overwritten by AES-GMAC ESP IV. */
  1097. append_cmd(desc, 0x00000000);
  1098. append_cmd(desc, 0x00000000);
  1099. /* End of blank commands */
  1100. /* Read assoc data */
  1101. append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 | FIFOLDST_VLF |
  1102. FIFOLD_TYPE_AAD);
  1103. /* Will read cryptlen bytes */
  1104. append_math_add(desc, VARSEQINLEN, ZERO, REG3, CAAM_CMD_SZ);
  1105. /* Will write cryptlen bytes */
  1106. append_math_add(desc, VARSEQOUTLEN, ZERO, REG3, CAAM_CMD_SZ);
  1107. /*
  1108. * MOVE_LEN opcode is not available in all SEC HW revisions,
  1109. * thus need to do some magic, i.e. self-patch the descriptor
  1110. * buffer.
  1111. */
  1112. read_move_cmd = append_move(desc, MOVE_SRC_DESCBUF | MOVE_DEST_MATH3 |
  1113. (0x6 << MOVE_LEN_SHIFT));
  1114. write_move_cmd = append_move(desc, MOVE_SRC_MATH3 | MOVE_DEST_DESCBUF |
  1115. (0x8 << MOVE_LEN_SHIFT));
  1116. /* Authenticate AES-GMAC ESP IV */
  1117. append_cmd(desc, CMD_FIFO_LOAD | FIFOLD_CLASS_CLASS1 | IMMEDIATE |
  1118. FIFOLD_TYPE_AAD | tfm->ivsize);
  1119. set_move_tgt_here(desc, write_aad_cmd);
  1120. /* Blank commands. Will be overwritten by AES-GMAC ESP IV. */
  1121. append_cmd(desc, 0x00000000);
  1122. append_cmd(desc, 0x00000000);
  1123. /* End of blank commands */
  1124. /* Read and write cryptlen bytes */
  1125. aead_append_src_dst(desc, FIFOLD_TYPE_AAD);
  1126. set_move_tgt_here(desc, read_move_cmd);
  1127. set_move_tgt_here(desc, write_move_cmd);
  1128. append_cmd(desc, CMD_LOAD | DISABLE_AUTO_INFO_FIFO);
  1129. /* Move payload data to OFIFO */
  1130. append_move(desc, MOVE_SRC_INFIFO_CL | MOVE_DEST_OUTFIFO);
  1131. /* Write ICV */
  1132. append_seq_store(desc, ctx->authsize, LDST_CLASS_1_CCB |
  1133. LDST_SRCDST_BYTE_CONTEXT);
  1134. ctx->sh_desc_enc_dma = dma_map_single(jrdev, desc,
  1135. desc_bytes(desc),
  1136. DMA_TO_DEVICE);
  1137. if (dma_mapping_error(jrdev, ctx->sh_desc_enc_dma)) {
  1138. dev_err(jrdev, "unable to map shared descriptor\n");
  1139. return -ENOMEM;
  1140. }
  1141. #ifdef DEBUG
  1142. print_hex_dump(KERN_ERR, "rfc4543 enc shdesc@"__stringify(__LINE__)": ",
  1143. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  1144. desc_bytes(desc), 1);
  1145. #endif
  1146. /*
  1147. * Job Descriptor and Shared Descriptors
  1148. * must all fit into the 64-word Descriptor h/w Buffer
  1149. */
  1150. keys_fit_inline = false;
  1151. if (DESC_RFC4543_DEC_LEN + DESC_JOB_IO_LEN +
  1152. ctx->enckeylen <= CAAM_DESC_BYTES_MAX)
  1153. keys_fit_inline = true;
  1154. desc = ctx->sh_desc_dec;
  1155. init_sh_desc(desc, HDR_SHARE_SERIAL);
  1156. /* Skip key loading if it is loaded due to sharing */
  1157. key_jump_cmd = append_jump(desc, JUMP_JSL |
  1158. JUMP_TEST_ALL | JUMP_COND_SHRD);
  1159. if (keys_fit_inline)
  1160. append_key_as_imm(desc, (void *)ctx->key, ctx->enckeylen,
  1161. ctx->enckeylen, CLASS_1 | KEY_DEST_CLASS_REG);
  1162. else
  1163. append_key(desc, ctx->key_dma, ctx->enckeylen,
  1164. CLASS_1 | KEY_DEST_CLASS_REG);
  1165. set_jump_tgt_here(desc, key_jump_cmd);
  1166. /* Class 1 operation */
  1167. append_operation(desc, ctx->class1_alg_type |
  1168. OP_ALG_AS_INITFINAL | OP_ALG_DECRYPT | OP_ALG_ICV_ON);
  1169. /* Load AES-GMAC ESP IV into Math1 register */
  1170. append_cmd(desc, CMD_SEQ_LOAD | LDST_SRCDST_WORD_DECO_MATH1 |
  1171. LDST_CLASS_DECO | tfm->ivsize);
  1172. /* Wait the DMA transaction to finish */
  1173. append_jump(desc, JUMP_TEST_ALL | JUMP_COND_CALM |
  1174. (1 << JUMP_OFFSET_SHIFT));
  1175. /* assoclen + cryptlen = (seqinlen - ivsize) - icvsize */
  1176. append_math_sub_imm_u32(desc, REG3, SEQINLEN, IMM, ctx->authsize);
  1177. /* Overwrite blank immediate AES-GMAC ESP IV data */
  1178. write_iv_cmd = append_move(desc, MOVE_SRC_MATH1 | MOVE_DEST_DESCBUF |
  1179. (tfm->ivsize << MOVE_LEN_SHIFT));
  1180. /* Overwrite blank immediate AAD data */
  1181. write_aad_cmd = append_move(desc, MOVE_SRC_MATH1 | MOVE_DEST_DESCBUF |
  1182. (tfm->ivsize << MOVE_LEN_SHIFT));
  1183. /* assoclen = (assoclen + cryptlen) - cryptlen */
  1184. append_math_sub(desc, REG2, SEQOUTLEN, REG0, CAAM_CMD_SZ);
  1185. append_math_sub(desc, VARSEQINLEN, REG3, REG2, CAAM_CMD_SZ);
  1186. /*
  1187. * MOVE_LEN opcode is not available in all SEC HW revisions,
  1188. * thus need to do some magic, i.e. self-patch the descriptor
  1189. * buffer.
  1190. */
  1191. read_move_cmd = append_move(desc, MOVE_SRC_DESCBUF | MOVE_DEST_MATH3 |
  1192. (0x6 << MOVE_LEN_SHIFT));
  1193. write_move_cmd = append_move(desc, MOVE_SRC_MATH3 | MOVE_DEST_DESCBUF |
  1194. (0x8 << MOVE_LEN_SHIFT));
  1195. /* Read Salt and AES-GMAC ESP IV */
  1196. append_cmd(desc, CMD_FIFO_LOAD | FIFOLD_CLASS_CLASS1 | IMMEDIATE |
  1197. FIFOLD_TYPE_IV | FIFOLD_TYPE_FLUSH1 | (4 + tfm->ivsize));
  1198. /* Append Salt */
  1199. append_data(desc, (void *)(ctx->key + ctx->enckeylen), 4);
  1200. set_move_tgt_here(desc, write_iv_cmd);
  1201. /* Blank commands. Will be overwritten by AES-GMAC ESP IV. */
  1202. append_cmd(desc, 0x00000000);
  1203. append_cmd(desc, 0x00000000);
  1204. /* End of blank commands */
  1205. /* Read assoc data */
  1206. append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 | FIFOLDST_VLF |
  1207. FIFOLD_TYPE_AAD);
  1208. /* Will read cryptlen bytes */
  1209. append_math_add(desc, VARSEQINLEN, ZERO, REG2, CAAM_CMD_SZ);
  1210. /* Will write cryptlen bytes */
  1211. append_math_add(desc, VARSEQOUTLEN, ZERO, REG2, CAAM_CMD_SZ);
  1212. /* Authenticate AES-GMAC ESP IV */
  1213. append_cmd(desc, CMD_FIFO_LOAD | FIFOLD_CLASS_CLASS1 | IMMEDIATE |
  1214. FIFOLD_TYPE_AAD | tfm->ivsize);
  1215. set_move_tgt_here(desc, write_aad_cmd);
  1216. /* Blank commands. Will be overwritten by AES-GMAC ESP IV. */
  1217. append_cmd(desc, 0x00000000);
  1218. append_cmd(desc, 0x00000000);
  1219. /* End of blank commands */
  1220. /* Store payload data */
  1221. append_seq_fifo_store(desc, 0, FIFOST_TYPE_MESSAGE_DATA | FIFOLDST_VLF);
  1222. /* In-snoop cryptlen data */
  1223. append_seq_fifo_load(desc, 0, FIFOLD_CLASS_BOTH | FIFOLDST_VLF |
  1224. FIFOLD_TYPE_AAD | FIFOLD_TYPE_LAST2FLUSH1);
  1225. set_move_tgt_here(desc, read_move_cmd);
  1226. set_move_tgt_here(desc, write_move_cmd);
  1227. append_cmd(desc, CMD_LOAD | DISABLE_AUTO_INFO_FIFO);
  1228. /* Move payload data to OFIFO */
  1229. append_move(desc, MOVE_SRC_INFIFO_CL | MOVE_DEST_OUTFIFO);
  1230. append_cmd(desc, CMD_LOAD | ENABLE_AUTO_INFO_FIFO);
  1231. /* Read ICV */
  1232. append_seq_fifo_load(desc, ctx->authsize, FIFOLD_CLASS_CLASS1 |
  1233. FIFOLD_TYPE_ICV | FIFOLD_TYPE_LAST1);
  1234. ctx->sh_desc_dec_dma = dma_map_single(jrdev, desc,
  1235. desc_bytes(desc),
  1236. DMA_TO_DEVICE);
  1237. if (dma_mapping_error(jrdev, ctx->sh_desc_dec_dma)) {
  1238. dev_err(jrdev, "unable to map shared descriptor\n");
  1239. return -ENOMEM;
  1240. }
  1241. #ifdef DEBUG
  1242. print_hex_dump(KERN_ERR, "rfc4543 dec shdesc@"__stringify(__LINE__)": ",
  1243. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  1244. desc_bytes(desc), 1);
  1245. #endif
  1246. /*
  1247. * Job Descriptor and Shared Descriptors
  1248. * must all fit into the 64-word Descriptor h/w Buffer
  1249. */
  1250. keys_fit_inline = false;
  1251. if (DESC_RFC4543_GIVENC_LEN + DESC_JOB_IO_LEN +
  1252. ctx->enckeylen <= CAAM_DESC_BYTES_MAX)
  1253. keys_fit_inline = true;
  1254. /* rfc4543_givencrypt shared descriptor */
  1255. desc = ctx->sh_desc_givenc;
  1256. init_sh_desc(desc, HDR_SHARE_SERIAL);
  1257. /* Skip key loading if it is loaded due to sharing */
  1258. key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
  1259. JUMP_COND_SHRD);
  1260. if (keys_fit_inline)
  1261. append_key_as_imm(desc, (void *)ctx->key, ctx->enckeylen,
  1262. ctx->enckeylen, CLASS_1 | KEY_DEST_CLASS_REG);
  1263. else
  1264. append_key(desc, ctx->key_dma, ctx->enckeylen,
  1265. CLASS_1 | KEY_DEST_CLASS_REG);
  1266. set_jump_tgt_here(desc, key_jump_cmd);
  1267. /* Generate IV */
  1268. geniv = NFIFOENTRY_STYPE_PAD | NFIFOENTRY_DEST_DECO |
  1269. NFIFOENTRY_DTYPE_MSG | NFIFOENTRY_LC1 |
  1270. NFIFOENTRY_PTYPE_RND | (tfm->ivsize << NFIFOENTRY_DLEN_SHIFT);
  1271. append_load_imm_u32(desc, geniv, LDST_CLASS_IND_CCB |
  1272. LDST_SRCDST_WORD_INFO_FIFO | LDST_IMM);
  1273. append_cmd(desc, CMD_LOAD | DISABLE_AUTO_INFO_FIFO);
  1274. /* Move generated IV to Math1 register */
  1275. append_move(desc, MOVE_SRC_INFIFO | MOVE_DEST_MATH1 |
  1276. (tfm->ivsize << MOVE_LEN_SHIFT));
  1277. append_cmd(desc, CMD_LOAD | ENABLE_AUTO_INFO_FIFO);
  1278. /* Overwrite blank immediate AES-GMAC IV data */
  1279. write_iv_cmd = append_move(desc, MOVE_SRC_MATH1 | MOVE_DEST_DESCBUF |
  1280. (tfm->ivsize << MOVE_LEN_SHIFT));
  1281. /* Overwrite blank immediate AAD data */
  1282. write_aad_cmd = append_move(desc, MOVE_SRC_MATH1 | MOVE_DEST_DESCBUF |
  1283. (tfm->ivsize << MOVE_LEN_SHIFT));
  1284. /* Copy generated IV to OFIFO */
  1285. append_move(desc, MOVE_SRC_MATH1 | MOVE_DEST_OUTFIFO |
  1286. (tfm->ivsize << MOVE_LEN_SHIFT));
  1287. /* Class 1 operation */
  1288. append_operation(desc, ctx->class1_alg_type |
  1289. OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
  1290. /* ivsize + cryptlen = seqoutlen - authsize */
  1291. append_math_sub_imm_u32(desc, REG3, SEQOUTLEN, IMM, ctx->authsize);
  1292. /* assoclen = seqinlen - (ivsize + cryptlen) */
  1293. append_math_sub(desc, VARSEQINLEN, SEQINLEN, REG3, CAAM_CMD_SZ);
  1294. /* Will write ivsize + cryptlen */
  1295. append_math_add(desc, VARSEQOUTLEN, REG3, REG0, CAAM_CMD_SZ);
  1296. /*
  1297. * MOVE_LEN opcode is not available in all SEC HW revisions,
  1298. * thus need to do some magic, i.e. self-patch the descriptor
  1299. * buffer.
  1300. */
  1301. read_move_cmd = append_move(desc, MOVE_SRC_DESCBUF | MOVE_DEST_MATH3 |
  1302. (0x6 << MOVE_LEN_SHIFT));
  1303. write_move_cmd = append_move(desc, MOVE_SRC_MATH3 | MOVE_DEST_DESCBUF |
  1304. (0x8 << MOVE_LEN_SHIFT));
  1305. /* Read Salt and AES-GMAC generated IV */
  1306. append_cmd(desc, CMD_FIFO_LOAD | FIFOLD_CLASS_CLASS1 | IMMEDIATE |
  1307. FIFOLD_TYPE_IV | FIFOLD_TYPE_FLUSH1 | (4 + tfm->ivsize));
  1308. /* Append Salt */
  1309. append_data(desc, (void *)(ctx->key + ctx->enckeylen), 4);
  1310. set_move_tgt_here(desc, write_iv_cmd);
  1311. /* Blank commands. Will be overwritten by AES-GMAC generated IV. */
  1312. append_cmd(desc, 0x00000000);
  1313. append_cmd(desc, 0x00000000);
  1314. /* End of blank commands */
  1315. /* No need to reload iv */
  1316. append_seq_fifo_load(desc, tfm->ivsize, FIFOLD_CLASS_SKIP);
  1317. /* Read assoc data */
  1318. append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 | FIFOLDST_VLF |
  1319. FIFOLD_TYPE_AAD);
  1320. /* Will read cryptlen */
  1321. append_math_add(desc, VARSEQINLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
  1322. /* Authenticate AES-GMAC IV */
  1323. append_cmd(desc, CMD_FIFO_LOAD | FIFOLD_CLASS_CLASS1 | IMMEDIATE |
  1324. FIFOLD_TYPE_AAD | tfm->ivsize);
  1325. set_move_tgt_here(desc, write_aad_cmd);
  1326. /* Blank commands. Will be overwritten by AES-GMAC IV. */
  1327. append_cmd(desc, 0x00000000);
  1328. append_cmd(desc, 0x00000000);
  1329. /* End of blank commands */
  1330. /* Read and write cryptlen bytes */
  1331. aead_append_src_dst(desc, FIFOLD_TYPE_AAD);
  1332. set_move_tgt_here(desc, read_move_cmd);
  1333. set_move_tgt_here(desc, write_move_cmd);
  1334. append_cmd(desc, CMD_LOAD | DISABLE_AUTO_INFO_FIFO);
  1335. /* Move payload data to OFIFO */
  1336. append_move(desc, MOVE_SRC_INFIFO_CL | MOVE_DEST_OUTFIFO);
  1337. /* Write ICV */
  1338. append_seq_store(desc, ctx->authsize, LDST_CLASS_1_CCB |
  1339. LDST_SRCDST_BYTE_CONTEXT);
  1340. ctx->sh_desc_givenc_dma = dma_map_single(jrdev, desc,
  1341. desc_bytes(desc),
  1342. DMA_TO_DEVICE);
  1343. if (dma_mapping_error(jrdev, ctx->sh_desc_givenc_dma)) {
  1344. dev_err(jrdev, "unable to map shared descriptor\n");
  1345. return -ENOMEM;
  1346. }
  1347. #ifdef DEBUG
  1348. print_hex_dump(KERN_ERR,
  1349. "rfc4543 givenc shdesc@"__stringify(__LINE__)": ",
  1350. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  1351. desc_bytes(desc), 1);
  1352. #endif
  1353. return 0;
  1354. }
  1355. static int rfc4543_setauthsize(struct crypto_aead *authenc,
  1356. unsigned int authsize)
  1357. {
  1358. struct caam_ctx *ctx = crypto_aead_ctx(authenc);
  1359. ctx->authsize = authsize;
  1360. rfc4543_set_sh_desc(authenc);
  1361. return 0;
  1362. }
  1363. static u32 gen_split_aead_key(struct caam_ctx *ctx, const u8 *key_in,
  1364. u32 authkeylen)
  1365. {
  1366. return gen_split_key(ctx->jrdev, ctx->key, ctx->split_key_len,
  1367. ctx->split_key_pad_len, key_in, authkeylen,
  1368. ctx->alg_op);
  1369. }
  1370. static int aead_setkey(struct crypto_aead *aead,
  1371. const u8 *key, unsigned int keylen)
  1372. {
  1373. /* Sizes for MDHA pads (*not* keys): MD5, SHA1, 224, 256, 384, 512 */
  1374. static const u8 mdpadlen[] = { 16, 20, 32, 32, 64, 64 };
  1375. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  1376. struct device *jrdev = ctx->jrdev;
  1377. struct crypto_authenc_keys keys;
  1378. int ret = 0;
  1379. if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
  1380. goto badkey;
  1381. /* Pick class 2 key length from algorithm submask */
  1382. ctx->split_key_len = mdpadlen[(ctx->alg_op & OP_ALG_ALGSEL_SUBMASK) >>
  1383. OP_ALG_ALGSEL_SHIFT] * 2;
  1384. ctx->split_key_pad_len = ALIGN(ctx->split_key_len, 16);
  1385. if (ctx->split_key_pad_len + keys.enckeylen > CAAM_MAX_KEY_SIZE)
  1386. goto badkey;
  1387. #ifdef DEBUG
  1388. printk(KERN_ERR "keylen %d enckeylen %d authkeylen %d\n",
  1389. keys.authkeylen + keys.enckeylen, keys.enckeylen,
  1390. keys.authkeylen);
  1391. printk(KERN_ERR "split_key_len %d split_key_pad_len %d\n",
  1392. ctx->split_key_len, ctx->split_key_pad_len);
  1393. print_hex_dump(KERN_ERR, "key in @"__stringify(__LINE__)": ",
  1394. DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
  1395. #endif
  1396. ret = gen_split_aead_key(ctx, keys.authkey, keys.authkeylen);
  1397. if (ret) {
  1398. goto badkey;
  1399. }
  1400. /* postpend encryption key to auth split key */
  1401. memcpy(ctx->key + ctx->split_key_pad_len, keys.enckey, keys.enckeylen);
  1402. ctx->key_dma = dma_map_single(jrdev, ctx->key, ctx->split_key_pad_len +
  1403. keys.enckeylen, DMA_TO_DEVICE);
  1404. if (dma_mapping_error(jrdev, ctx->key_dma)) {
  1405. dev_err(jrdev, "unable to map key i/o memory\n");
  1406. return -ENOMEM;
  1407. }
  1408. #ifdef DEBUG
  1409. print_hex_dump(KERN_ERR, "ctx.key@"__stringify(__LINE__)": ",
  1410. DUMP_PREFIX_ADDRESS, 16, 4, ctx->key,
  1411. ctx->split_key_pad_len + keys.enckeylen, 1);
  1412. #endif
  1413. ctx->enckeylen = keys.enckeylen;
  1414. ret = aead_set_sh_desc(aead);
  1415. if (ret) {
  1416. dma_unmap_single(jrdev, ctx->key_dma, ctx->split_key_pad_len +
  1417. keys.enckeylen, DMA_TO_DEVICE);
  1418. }
  1419. return ret;
  1420. badkey:
  1421. crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
  1422. return -EINVAL;
  1423. }
  1424. static int gcm_setkey(struct crypto_aead *aead,
  1425. const u8 *key, unsigned int keylen)
  1426. {
  1427. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  1428. struct device *jrdev = ctx->jrdev;
  1429. int ret = 0;
  1430. #ifdef DEBUG
  1431. print_hex_dump(KERN_ERR, "key in @"__stringify(__LINE__)": ",
  1432. DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
  1433. #endif
  1434. memcpy(ctx->key, key, keylen);
  1435. ctx->key_dma = dma_map_single(jrdev, ctx->key, keylen,
  1436. DMA_TO_DEVICE);
  1437. if (dma_mapping_error(jrdev, ctx->key_dma)) {
  1438. dev_err(jrdev, "unable to map key i/o memory\n");
  1439. return -ENOMEM;
  1440. }
  1441. ctx->enckeylen = keylen;
  1442. ret = gcm_set_sh_desc(aead);
  1443. if (ret) {
  1444. dma_unmap_single(jrdev, ctx->key_dma, ctx->enckeylen,
  1445. DMA_TO_DEVICE);
  1446. }
  1447. return ret;
  1448. }
  1449. static int rfc4106_setkey(struct crypto_aead *aead,
  1450. const u8 *key, unsigned int keylen)
  1451. {
  1452. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  1453. struct device *jrdev = ctx->jrdev;
  1454. int ret = 0;
  1455. if (keylen < 4)
  1456. return -EINVAL;
  1457. #ifdef DEBUG
  1458. print_hex_dump(KERN_ERR, "key in @"__stringify(__LINE__)": ",
  1459. DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
  1460. #endif
  1461. memcpy(ctx->key, key, keylen);
  1462. /*
  1463. * The last four bytes of the key material are used as the salt value
  1464. * in the nonce. Update the AES key length.
  1465. */
  1466. ctx->enckeylen = keylen - 4;
  1467. ctx->key_dma = dma_map_single(jrdev, ctx->key, ctx->enckeylen,
  1468. DMA_TO_DEVICE);
  1469. if (dma_mapping_error(jrdev, ctx->key_dma)) {
  1470. dev_err(jrdev, "unable to map key i/o memory\n");
  1471. return -ENOMEM;
  1472. }
  1473. ret = rfc4106_set_sh_desc(aead);
  1474. if (ret) {
  1475. dma_unmap_single(jrdev, ctx->key_dma, ctx->enckeylen,
  1476. DMA_TO_DEVICE);
  1477. }
  1478. return ret;
  1479. }
  1480. static int rfc4543_setkey(struct crypto_aead *aead,
  1481. const u8 *key, unsigned int keylen)
  1482. {
  1483. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  1484. struct device *jrdev = ctx->jrdev;
  1485. int ret = 0;
  1486. if (keylen < 4)
  1487. return -EINVAL;
  1488. #ifdef DEBUG
  1489. print_hex_dump(KERN_ERR, "key in @"__stringify(__LINE__)": ",
  1490. DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
  1491. #endif
  1492. memcpy(ctx->key, key, keylen);
  1493. /*
  1494. * The last four bytes of the key material are used as the salt value
  1495. * in the nonce. Update the AES key length.
  1496. */
  1497. ctx->enckeylen = keylen - 4;
  1498. ctx->key_dma = dma_map_single(jrdev, ctx->key, ctx->enckeylen,
  1499. DMA_TO_DEVICE);
  1500. if (dma_mapping_error(jrdev, ctx->key_dma)) {
  1501. dev_err(jrdev, "unable to map key i/o memory\n");
  1502. return -ENOMEM;
  1503. }
  1504. ret = rfc4543_set_sh_desc(aead);
  1505. if (ret) {
  1506. dma_unmap_single(jrdev, ctx->key_dma, ctx->enckeylen,
  1507. DMA_TO_DEVICE);
  1508. }
  1509. return ret;
  1510. }
  1511. static int ablkcipher_setkey(struct crypto_ablkcipher *ablkcipher,
  1512. const u8 *key, unsigned int keylen)
  1513. {
  1514. struct caam_ctx *ctx = crypto_ablkcipher_ctx(ablkcipher);
  1515. struct ablkcipher_tfm *crt = &ablkcipher->base.crt_ablkcipher;
  1516. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(ablkcipher);
  1517. const char *alg_name = crypto_tfm_alg_name(tfm);
  1518. struct device *jrdev = ctx->jrdev;
  1519. int ret = 0;
  1520. u32 *key_jump_cmd;
  1521. u32 *desc;
  1522. u32 *nonce;
  1523. u32 geniv;
  1524. u32 ctx1_iv_off = 0;
  1525. const bool ctr_mode = ((ctx->class1_alg_type & OP_ALG_AAI_MASK) ==
  1526. OP_ALG_AAI_CTR_MOD128);
  1527. const bool is_rfc3686 = (ctr_mode &&
  1528. (strstr(alg_name, "rfc3686") != NULL));
  1529. #ifdef DEBUG
  1530. print_hex_dump(KERN_ERR, "key in @"__stringify(__LINE__)": ",
  1531. DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
  1532. #endif
  1533. /*
  1534. * AES-CTR needs to load IV in CONTEXT1 reg
  1535. * at an offset of 128bits (16bytes)
  1536. * CONTEXT1[255:128] = IV
  1537. */
  1538. if (ctr_mode)
  1539. ctx1_iv_off = 16;
  1540. /*
  1541. * RFC3686 specific:
  1542. * | CONTEXT1[255:128] = {NONCE, IV, COUNTER}
  1543. * | *key = {KEY, NONCE}
  1544. */
  1545. if (is_rfc3686) {
  1546. ctx1_iv_off = 16 + CTR_RFC3686_NONCE_SIZE;
  1547. keylen -= CTR_RFC3686_NONCE_SIZE;
  1548. }
  1549. memcpy(ctx->key, key, keylen);
  1550. ctx->key_dma = dma_map_single(jrdev, ctx->key, keylen,
  1551. DMA_TO_DEVICE);
  1552. if (dma_mapping_error(jrdev, ctx->key_dma)) {
  1553. dev_err(jrdev, "unable to map key i/o memory\n");
  1554. return -ENOMEM;
  1555. }
  1556. ctx->enckeylen = keylen;
  1557. /* ablkcipher_encrypt shared descriptor */
  1558. desc = ctx->sh_desc_enc;
  1559. init_sh_desc(desc, HDR_SHARE_SERIAL | HDR_SAVECTX);
  1560. /* Skip if already shared */
  1561. key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
  1562. JUMP_COND_SHRD);
  1563. /* Load class1 key only */
  1564. append_key_as_imm(desc, (void *)ctx->key, ctx->enckeylen,
  1565. ctx->enckeylen, CLASS_1 |
  1566. KEY_DEST_CLASS_REG);
  1567. /* Load nonce into CONTEXT1 reg */
  1568. if (is_rfc3686) {
  1569. nonce = (u32 *)(key + keylen);
  1570. append_load_imm_u32(desc, *nonce, LDST_CLASS_IND_CCB |
  1571. LDST_SRCDST_BYTE_OUTFIFO | LDST_IMM);
  1572. append_move(desc, MOVE_WAITCOMP |
  1573. MOVE_SRC_OUTFIFO |
  1574. MOVE_DEST_CLASS1CTX |
  1575. (16 << MOVE_OFFSET_SHIFT) |
  1576. (CTR_RFC3686_NONCE_SIZE << MOVE_LEN_SHIFT));
  1577. }
  1578. set_jump_tgt_here(desc, key_jump_cmd);
  1579. /* Load iv */
  1580. append_seq_load(desc, crt->ivsize, LDST_SRCDST_BYTE_CONTEXT |
  1581. LDST_CLASS_1_CCB | (ctx1_iv_off << LDST_OFFSET_SHIFT));
  1582. /* Load counter into CONTEXT1 reg */
  1583. if (is_rfc3686)
  1584. append_load_imm_u32(desc, be32_to_cpu(1), LDST_IMM |
  1585. LDST_CLASS_1_CCB |
  1586. LDST_SRCDST_BYTE_CONTEXT |
  1587. ((ctx1_iv_off + CTR_RFC3686_IV_SIZE) <<
  1588. LDST_OFFSET_SHIFT));
  1589. /* Load operation */
  1590. append_operation(desc, ctx->class1_alg_type |
  1591. OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
  1592. /* Perform operation */
  1593. ablkcipher_append_src_dst(desc);
  1594. ctx->sh_desc_enc_dma = dma_map_single(jrdev, desc,
  1595. desc_bytes(desc),
  1596. DMA_TO_DEVICE);
  1597. if (dma_mapping_error(jrdev, ctx->sh_desc_enc_dma)) {
  1598. dev_err(jrdev, "unable to map shared descriptor\n");
  1599. return -ENOMEM;
  1600. }
  1601. #ifdef DEBUG
  1602. print_hex_dump(KERN_ERR,
  1603. "ablkcipher enc shdesc@"__stringify(__LINE__)": ",
  1604. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  1605. desc_bytes(desc), 1);
  1606. #endif
  1607. /* ablkcipher_decrypt shared descriptor */
  1608. desc = ctx->sh_desc_dec;
  1609. init_sh_desc(desc, HDR_SHARE_SERIAL | HDR_SAVECTX);
  1610. /* Skip if already shared */
  1611. key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
  1612. JUMP_COND_SHRD);
  1613. /* Load class1 key only */
  1614. append_key_as_imm(desc, (void *)ctx->key, ctx->enckeylen,
  1615. ctx->enckeylen, CLASS_1 |
  1616. KEY_DEST_CLASS_REG);
  1617. /* Load nonce into CONTEXT1 reg */
  1618. if (is_rfc3686) {
  1619. nonce = (u32 *)(key + keylen);
  1620. append_load_imm_u32(desc, *nonce, LDST_CLASS_IND_CCB |
  1621. LDST_SRCDST_BYTE_OUTFIFO | LDST_IMM);
  1622. append_move(desc, MOVE_WAITCOMP |
  1623. MOVE_SRC_OUTFIFO |
  1624. MOVE_DEST_CLASS1CTX |
  1625. (16 << MOVE_OFFSET_SHIFT) |
  1626. (CTR_RFC3686_NONCE_SIZE << MOVE_LEN_SHIFT));
  1627. }
  1628. set_jump_tgt_here(desc, key_jump_cmd);
  1629. /* load IV */
  1630. append_seq_load(desc, crt->ivsize, LDST_SRCDST_BYTE_CONTEXT |
  1631. LDST_CLASS_1_CCB | (ctx1_iv_off << LDST_OFFSET_SHIFT));
  1632. /* Load counter into CONTEXT1 reg */
  1633. if (is_rfc3686)
  1634. append_load_imm_u32(desc, be32_to_cpu(1), LDST_IMM |
  1635. LDST_CLASS_1_CCB |
  1636. LDST_SRCDST_BYTE_CONTEXT |
  1637. ((ctx1_iv_off + CTR_RFC3686_IV_SIZE) <<
  1638. LDST_OFFSET_SHIFT));
  1639. /* Choose operation */
  1640. if (ctr_mode)
  1641. append_operation(desc, ctx->class1_alg_type |
  1642. OP_ALG_AS_INITFINAL | OP_ALG_DECRYPT);
  1643. else
  1644. append_dec_op1(desc, ctx->class1_alg_type);
  1645. /* Perform operation */
  1646. ablkcipher_append_src_dst(desc);
  1647. ctx->sh_desc_dec_dma = dma_map_single(jrdev, desc,
  1648. desc_bytes(desc),
  1649. DMA_TO_DEVICE);
  1650. if (dma_mapping_error(jrdev, ctx->sh_desc_dec_dma)) {
  1651. dev_err(jrdev, "unable to map shared descriptor\n");
  1652. return -ENOMEM;
  1653. }
  1654. #ifdef DEBUG
  1655. print_hex_dump(KERN_ERR,
  1656. "ablkcipher dec shdesc@"__stringify(__LINE__)": ",
  1657. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  1658. desc_bytes(desc), 1);
  1659. #endif
  1660. /* ablkcipher_givencrypt shared descriptor */
  1661. desc = ctx->sh_desc_givenc;
  1662. init_sh_desc(desc, HDR_SHARE_SERIAL | HDR_SAVECTX);
  1663. /* Skip if already shared */
  1664. key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
  1665. JUMP_COND_SHRD);
  1666. /* Load class1 key only */
  1667. append_key_as_imm(desc, (void *)ctx->key, ctx->enckeylen,
  1668. ctx->enckeylen, CLASS_1 |
  1669. KEY_DEST_CLASS_REG);
  1670. /* Load Nonce into CONTEXT1 reg */
  1671. if (is_rfc3686) {
  1672. nonce = (u32 *)(key + keylen);
  1673. append_load_imm_u32(desc, *nonce, LDST_CLASS_IND_CCB |
  1674. LDST_SRCDST_BYTE_OUTFIFO | LDST_IMM);
  1675. append_move(desc, MOVE_WAITCOMP |
  1676. MOVE_SRC_OUTFIFO |
  1677. MOVE_DEST_CLASS1CTX |
  1678. (16 << MOVE_OFFSET_SHIFT) |
  1679. (CTR_RFC3686_NONCE_SIZE << MOVE_LEN_SHIFT));
  1680. }
  1681. set_jump_tgt_here(desc, key_jump_cmd);
  1682. /* Generate IV */
  1683. geniv = NFIFOENTRY_STYPE_PAD | NFIFOENTRY_DEST_DECO |
  1684. NFIFOENTRY_DTYPE_MSG | NFIFOENTRY_LC1 |
  1685. NFIFOENTRY_PTYPE_RND | (crt->ivsize << NFIFOENTRY_DLEN_SHIFT);
  1686. append_load_imm_u32(desc, geniv, LDST_CLASS_IND_CCB |
  1687. LDST_SRCDST_WORD_INFO_FIFO | LDST_IMM);
  1688. append_cmd(desc, CMD_LOAD | DISABLE_AUTO_INFO_FIFO);
  1689. append_move(desc, MOVE_WAITCOMP |
  1690. MOVE_SRC_INFIFO |
  1691. MOVE_DEST_CLASS1CTX |
  1692. (crt->ivsize << MOVE_LEN_SHIFT) |
  1693. (ctx1_iv_off << MOVE_OFFSET_SHIFT));
  1694. append_cmd(desc, CMD_LOAD | ENABLE_AUTO_INFO_FIFO);
  1695. /* Copy generated IV to memory */
  1696. append_seq_store(desc, crt->ivsize,
  1697. LDST_SRCDST_BYTE_CONTEXT | LDST_CLASS_1_CCB |
  1698. (ctx1_iv_off << LDST_OFFSET_SHIFT));
  1699. /* Load Counter into CONTEXT1 reg */
  1700. if (is_rfc3686)
  1701. append_load_imm_u32(desc, (u32)1, LDST_IMM |
  1702. LDST_CLASS_1_CCB |
  1703. LDST_SRCDST_BYTE_CONTEXT |
  1704. ((ctx1_iv_off + CTR_RFC3686_IV_SIZE) <<
  1705. LDST_OFFSET_SHIFT));
  1706. if (ctx1_iv_off)
  1707. append_jump(desc, JUMP_JSL | JUMP_TEST_ALL | JUMP_COND_NCP |
  1708. (1 << JUMP_OFFSET_SHIFT));
  1709. /* Load operation */
  1710. append_operation(desc, ctx->class1_alg_type |
  1711. OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
  1712. /* Perform operation */
  1713. ablkcipher_append_src_dst(desc);
  1714. ctx->sh_desc_givenc_dma = dma_map_single(jrdev, desc,
  1715. desc_bytes(desc),
  1716. DMA_TO_DEVICE);
  1717. if (dma_mapping_error(jrdev, ctx->sh_desc_givenc_dma)) {
  1718. dev_err(jrdev, "unable to map shared descriptor\n");
  1719. return -ENOMEM;
  1720. }
  1721. #ifdef DEBUG
  1722. print_hex_dump(KERN_ERR,
  1723. "ablkcipher givenc shdesc@" __stringify(__LINE__) ": ",
  1724. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  1725. desc_bytes(desc), 1);
  1726. #endif
  1727. return ret;
  1728. }
  1729. /*
  1730. * aead_edesc - s/w-extended aead descriptor
  1731. * @assoc_nents: number of segments in associated data (SPI+Seq) scatterlist
  1732. * @assoc_chained: if source is chained
  1733. * @src_nents: number of segments in input scatterlist
  1734. * @src_chained: if source is chained
  1735. * @dst_nents: number of segments in output scatterlist
  1736. * @dst_chained: if destination is chained
  1737. * @iv_dma: dma address of iv for checking continuity and link table
  1738. * @desc: h/w descriptor (variable length; must not exceed MAX_CAAM_DESCSIZE)
  1739. * @sec4_sg_bytes: length of dma mapped sec4_sg space
  1740. * @sec4_sg_dma: bus physical mapped address of h/w link table
  1741. * @hw_desc: the h/w job descriptor followed by any referenced link tables
  1742. */
  1743. struct aead_edesc {
  1744. int assoc_nents;
  1745. bool assoc_chained;
  1746. int src_nents;
  1747. bool src_chained;
  1748. int dst_nents;
  1749. bool dst_chained;
  1750. dma_addr_t iv_dma;
  1751. int sec4_sg_bytes;
  1752. dma_addr_t sec4_sg_dma;
  1753. struct sec4_sg_entry *sec4_sg;
  1754. u32 hw_desc[0];
  1755. };
  1756. /*
  1757. * ablkcipher_edesc - s/w-extended ablkcipher descriptor
  1758. * @src_nents: number of segments in input scatterlist
  1759. * @src_chained: if source is chained
  1760. * @dst_nents: number of segments in output scatterlist
  1761. * @dst_chained: if destination is chained
  1762. * @iv_dma: dma address of iv for checking continuity and link table
  1763. * @desc: h/w descriptor (variable length; must not exceed MAX_CAAM_DESCSIZE)
  1764. * @sec4_sg_bytes: length of dma mapped sec4_sg space
  1765. * @sec4_sg_dma: bus physical mapped address of h/w link table
  1766. * @hw_desc: the h/w job descriptor followed by any referenced link tables
  1767. */
  1768. struct ablkcipher_edesc {
  1769. int src_nents;
  1770. bool src_chained;
  1771. int dst_nents;
  1772. bool dst_chained;
  1773. dma_addr_t iv_dma;
  1774. int sec4_sg_bytes;
  1775. dma_addr_t sec4_sg_dma;
  1776. struct sec4_sg_entry *sec4_sg;
  1777. u32 hw_desc[0];
  1778. };
  1779. static void caam_unmap(struct device *dev, struct scatterlist *src,
  1780. struct scatterlist *dst, int src_nents,
  1781. bool src_chained, int dst_nents, bool dst_chained,
  1782. dma_addr_t iv_dma, int ivsize, dma_addr_t sec4_sg_dma,
  1783. int sec4_sg_bytes)
  1784. {
  1785. if (dst != src) {
  1786. dma_unmap_sg_chained(dev, src, src_nents ? : 1, DMA_TO_DEVICE,
  1787. src_chained);
  1788. dma_unmap_sg_chained(dev, dst, dst_nents ? : 1, DMA_FROM_DEVICE,
  1789. dst_chained);
  1790. } else {
  1791. dma_unmap_sg_chained(dev, src, src_nents ? : 1,
  1792. DMA_BIDIRECTIONAL, src_chained);
  1793. }
  1794. if (iv_dma)
  1795. dma_unmap_single(dev, iv_dma, ivsize, DMA_TO_DEVICE);
  1796. if (sec4_sg_bytes)
  1797. dma_unmap_single(dev, sec4_sg_dma, sec4_sg_bytes,
  1798. DMA_TO_DEVICE);
  1799. }
  1800. static void aead_unmap(struct device *dev,
  1801. struct aead_edesc *edesc,
  1802. struct aead_request *req)
  1803. {
  1804. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  1805. int ivsize = crypto_aead_ivsize(aead);
  1806. dma_unmap_sg_chained(dev, req->assoc, edesc->assoc_nents,
  1807. DMA_TO_DEVICE, edesc->assoc_chained);
  1808. caam_unmap(dev, req->src, req->dst,
  1809. edesc->src_nents, edesc->src_chained, edesc->dst_nents,
  1810. edesc->dst_chained, edesc->iv_dma, ivsize,
  1811. edesc->sec4_sg_dma, edesc->sec4_sg_bytes);
  1812. }
  1813. static void ablkcipher_unmap(struct device *dev,
  1814. struct ablkcipher_edesc *edesc,
  1815. struct ablkcipher_request *req)
  1816. {
  1817. struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
  1818. int ivsize = crypto_ablkcipher_ivsize(ablkcipher);
  1819. caam_unmap(dev, req->src, req->dst,
  1820. edesc->src_nents, edesc->src_chained, edesc->dst_nents,
  1821. edesc->dst_chained, edesc->iv_dma, ivsize,
  1822. edesc->sec4_sg_dma, edesc->sec4_sg_bytes);
  1823. }
  1824. static void aead_encrypt_done(struct device *jrdev, u32 *desc, u32 err,
  1825. void *context)
  1826. {
  1827. struct aead_request *req = context;
  1828. struct aead_edesc *edesc;
  1829. #ifdef DEBUG
  1830. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  1831. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  1832. int ivsize = crypto_aead_ivsize(aead);
  1833. dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
  1834. #endif
  1835. edesc = (struct aead_edesc *)((char *)desc -
  1836. offsetof(struct aead_edesc, hw_desc));
  1837. if (err)
  1838. caam_jr_strstatus(jrdev, err);
  1839. aead_unmap(jrdev, edesc, req);
  1840. #ifdef DEBUG
  1841. print_hex_dump(KERN_ERR, "assoc @"__stringify(__LINE__)": ",
  1842. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->assoc),
  1843. req->assoclen , 1);
  1844. print_hex_dump(KERN_ERR, "dstiv @"__stringify(__LINE__)": ",
  1845. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src) - ivsize,
  1846. edesc->src_nents ? 100 : ivsize, 1);
  1847. print_hex_dump(KERN_ERR, "dst @"__stringify(__LINE__)": ",
  1848. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
  1849. edesc->src_nents ? 100 : req->cryptlen +
  1850. ctx->authsize + 4, 1);
  1851. #endif
  1852. kfree(edesc);
  1853. aead_request_complete(req, err);
  1854. }
  1855. static void aead_decrypt_done(struct device *jrdev, u32 *desc, u32 err,
  1856. void *context)
  1857. {
  1858. struct aead_request *req = context;
  1859. struct aead_edesc *edesc;
  1860. #ifdef DEBUG
  1861. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  1862. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  1863. int ivsize = crypto_aead_ivsize(aead);
  1864. dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
  1865. #endif
  1866. edesc = (struct aead_edesc *)((char *)desc -
  1867. offsetof(struct aead_edesc, hw_desc));
  1868. #ifdef DEBUG
  1869. print_hex_dump(KERN_ERR, "dstiv @"__stringify(__LINE__)": ",
  1870. DUMP_PREFIX_ADDRESS, 16, 4, req->iv,
  1871. ivsize, 1);
  1872. print_hex_dump(KERN_ERR, "dst @"__stringify(__LINE__)": ",
  1873. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->dst),
  1874. req->cryptlen - ctx->authsize, 1);
  1875. #endif
  1876. if (err)
  1877. caam_jr_strstatus(jrdev, err);
  1878. aead_unmap(jrdev, edesc, req);
  1879. /*
  1880. * verify hw auth check passed else return -EBADMSG
  1881. */
  1882. if ((err & JRSTA_CCBERR_ERRID_MASK) == JRSTA_CCBERR_ERRID_ICVCHK)
  1883. err = -EBADMSG;
  1884. #ifdef DEBUG
  1885. print_hex_dump(KERN_ERR, "iphdrout@"__stringify(__LINE__)": ",
  1886. DUMP_PREFIX_ADDRESS, 16, 4,
  1887. ((char *)sg_virt(req->assoc) - sizeof(struct iphdr)),
  1888. sizeof(struct iphdr) + req->assoclen +
  1889. ((req->cryptlen > 1500) ? 1500 : req->cryptlen) +
  1890. ctx->authsize + 36, 1);
  1891. if (!err && edesc->sec4_sg_bytes) {
  1892. struct scatterlist *sg = sg_last(req->src, edesc->src_nents);
  1893. print_hex_dump(KERN_ERR, "sglastout@"__stringify(__LINE__)": ",
  1894. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(sg),
  1895. sg->length + ctx->authsize + 16, 1);
  1896. }
  1897. #endif
  1898. kfree(edesc);
  1899. aead_request_complete(req, err);
  1900. }
  1901. static void ablkcipher_encrypt_done(struct device *jrdev, u32 *desc, u32 err,
  1902. void *context)
  1903. {
  1904. struct ablkcipher_request *req = context;
  1905. struct ablkcipher_edesc *edesc;
  1906. #ifdef DEBUG
  1907. struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
  1908. int ivsize = crypto_ablkcipher_ivsize(ablkcipher);
  1909. dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
  1910. #endif
  1911. edesc = (struct ablkcipher_edesc *)((char *)desc -
  1912. offsetof(struct ablkcipher_edesc, hw_desc));
  1913. if (err)
  1914. caam_jr_strstatus(jrdev, err);
  1915. #ifdef DEBUG
  1916. print_hex_dump(KERN_ERR, "dstiv @"__stringify(__LINE__)": ",
  1917. DUMP_PREFIX_ADDRESS, 16, 4, req->info,
  1918. edesc->src_nents > 1 ? 100 : ivsize, 1);
  1919. print_hex_dump(KERN_ERR, "dst @"__stringify(__LINE__)": ",
  1920. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
  1921. edesc->dst_nents > 1 ? 100 : req->nbytes, 1);
  1922. #endif
  1923. ablkcipher_unmap(jrdev, edesc, req);
  1924. kfree(edesc);
  1925. ablkcipher_request_complete(req, err);
  1926. }
  1927. static void ablkcipher_decrypt_done(struct device *jrdev, u32 *desc, u32 err,
  1928. void *context)
  1929. {
  1930. struct ablkcipher_request *req = context;
  1931. struct ablkcipher_edesc *edesc;
  1932. #ifdef DEBUG
  1933. struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
  1934. int ivsize = crypto_ablkcipher_ivsize(ablkcipher);
  1935. dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
  1936. #endif
  1937. edesc = (struct ablkcipher_edesc *)((char *)desc -
  1938. offsetof(struct ablkcipher_edesc, hw_desc));
  1939. if (err)
  1940. caam_jr_strstatus(jrdev, err);
  1941. #ifdef DEBUG
  1942. print_hex_dump(KERN_ERR, "dstiv @"__stringify(__LINE__)": ",
  1943. DUMP_PREFIX_ADDRESS, 16, 4, req->info,
  1944. ivsize, 1);
  1945. print_hex_dump(KERN_ERR, "dst @"__stringify(__LINE__)": ",
  1946. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
  1947. edesc->dst_nents > 1 ? 100 : req->nbytes, 1);
  1948. #endif
  1949. ablkcipher_unmap(jrdev, edesc, req);
  1950. kfree(edesc);
  1951. ablkcipher_request_complete(req, err);
  1952. }
  1953. /*
  1954. * Fill in aead job descriptor
  1955. */
  1956. static void init_aead_job(u32 *sh_desc, dma_addr_t ptr,
  1957. struct aead_edesc *edesc,
  1958. struct aead_request *req,
  1959. bool all_contig, bool encrypt)
  1960. {
  1961. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  1962. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  1963. int ivsize = crypto_aead_ivsize(aead);
  1964. int authsize = ctx->authsize;
  1965. u32 *desc = edesc->hw_desc;
  1966. u32 out_options = 0, in_options;
  1967. dma_addr_t dst_dma, src_dma;
  1968. int len, sec4_sg_index = 0;
  1969. bool is_gcm = false;
  1970. #ifdef DEBUG
  1971. debug("assoclen %d cryptlen %d authsize %d\n",
  1972. req->assoclen, req->cryptlen, authsize);
  1973. print_hex_dump(KERN_ERR, "assoc @"__stringify(__LINE__)": ",
  1974. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->assoc),
  1975. req->assoclen , 1);
  1976. print_hex_dump(KERN_ERR, "presciv@"__stringify(__LINE__)": ",
  1977. DUMP_PREFIX_ADDRESS, 16, 4, req->iv,
  1978. edesc->src_nents ? 100 : ivsize, 1);
  1979. print_hex_dump(KERN_ERR, "src @"__stringify(__LINE__)": ",
  1980. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
  1981. edesc->src_nents ? 100 : req->cryptlen, 1);
  1982. print_hex_dump(KERN_ERR, "shrdesc@"__stringify(__LINE__)": ",
  1983. DUMP_PREFIX_ADDRESS, 16, 4, sh_desc,
  1984. desc_bytes(sh_desc), 1);
  1985. #endif
  1986. if (((ctx->class1_alg_type & OP_ALG_ALGSEL_MASK) ==
  1987. OP_ALG_ALGSEL_AES) &&
  1988. ((ctx->class1_alg_type & OP_ALG_AAI_MASK) == OP_ALG_AAI_GCM))
  1989. is_gcm = true;
  1990. len = desc_len(sh_desc);
  1991. init_job_desc_shared(desc, ptr, len, HDR_SHARE_DEFER | HDR_REVERSE);
  1992. if (all_contig) {
  1993. if (is_gcm)
  1994. src_dma = edesc->iv_dma;
  1995. else
  1996. src_dma = sg_dma_address(req->assoc);
  1997. in_options = 0;
  1998. } else {
  1999. src_dma = edesc->sec4_sg_dma;
  2000. sec4_sg_index += (edesc->assoc_nents ? : 1) + 1 +
  2001. (edesc->src_nents ? : 1);
  2002. in_options = LDST_SGF;
  2003. }
  2004. append_seq_in_ptr(desc, src_dma, req->assoclen + ivsize + req->cryptlen,
  2005. in_options);
  2006. if (likely(req->src == req->dst)) {
  2007. if (all_contig) {
  2008. dst_dma = sg_dma_address(req->src);
  2009. } else {
  2010. dst_dma = src_dma + sizeof(struct sec4_sg_entry) *
  2011. ((edesc->assoc_nents ? : 1) + 1);
  2012. out_options = LDST_SGF;
  2013. }
  2014. } else {
  2015. if (!edesc->dst_nents) {
  2016. dst_dma = sg_dma_address(req->dst);
  2017. } else {
  2018. dst_dma = edesc->sec4_sg_dma +
  2019. sec4_sg_index *
  2020. sizeof(struct sec4_sg_entry);
  2021. out_options = LDST_SGF;
  2022. }
  2023. }
  2024. if (encrypt)
  2025. append_seq_out_ptr(desc, dst_dma, req->cryptlen + authsize,
  2026. out_options);
  2027. else
  2028. append_seq_out_ptr(desc, dst_dma, req->cryptlen - authsize,
  2029. out_options);
  2030. }
  2031. /*
  2032. * Fill in aead givencrypt job descriptor
  2033. */
  2034. static void init_aead_giv_job(u32 *sh_desc, dma_addr_t ptr,
  2035. struct aead_edesc *edesc,
  2036. struct aead_request *req,
  2037. int contig)
  2038. {
  2039. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  2040. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  2041. int ivsize = crypto_aead_ivsize(aead);
  2042. int authsize = ctx->authsize;
  2043. u32 *desc = edesc->hw_desc;
  2044. u32 out_options = 0, in_options;
  2045. dma_addr_t dst_dma, src_dma;
  2046. int len, sec4_sg_index = 0;
  2047. bool is_gcm = false;
  2048. #ifdef DEBUG
  2049. debug("assoclen %d cryptlen %d authsize %d\n",
  2050. req->assoclen, req->cryptlen, authsize);
  2051. print_hex_dump(KERN_ERR, "assoc @"__stringify(__LINE__)": ",
  2052. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->assoc),
  2053. req->assoclen , 1);
  2054. print_hex_dump(KERN_ERR, "presciv@"__stringify(__LINE__)": ",
  2055. DUMP_PREFIX_ADDRESS, 16, 4, req->iv, ivsize, 1);
  2056. print_hex_dump(KERN_ERR, "src @"__stringify(__LINE__)": ",
  2057. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
  2058. edesc->src_nents > 1 ? 100 : req->cryptlen, 1);
  2059. print_hex_dump(KERN_ERR, "shrdesc@"__stringify(__LINE__)": ",
  2060. DUMP_PREFIX_ADDRESS, 16, 4, sh_desc,
  2061. desc_bytes(sh_desc), 1);
  2062. #endif
  2063. if (((ctx->class1_alg_type & OP_ALG_ALGSEL_MASK) ==
  2064. OP_ALG_ALGSEL_AES) &&
  2065. ((ctx->class1_alg_type & OP_ALG_AAI_MASK) == OP_ALG_AAI_GCM))
  2066. is_gcm = true;
  2067. len = desc_len(sh_desc);
  2068. init_job_desc_shared(desc, ptr, len, HDR_SHARE_DEFER | HDR_REVERSE);
  2069. if (contig & GIV_SRC_CONTIG) {
  2070. if (is_gcm)
  2071. src_dma = edesc->iv_dma;
  2072. else
  2073. src_dma = sg_dma_address(req->assoc);
  2074. in_options = 0;
  2075. } else {
  2076. src_dma = edesc->sec4_sg_dma;
  2077. sec4_sg_index += edesc->assoc_nents + 1 + edesc->src_nents;
  2078. in_options = LDST_SGF;
  2079. }
  2080. append_seq_in_ptr(desc, src_dma, req->assoclen + ivsize + req->cryptlen,
  2081. in_options);
  2082. if (contig & GIV_DST_CONTIG) {
  2083. dst_dma = edesc->iv_dma;
  2084. } else {
  2085. if (likely(req->src == req->dst)) {
  2086. dst_dma = src_dma + sizeof(struct sec4_sg_entry) *
  2087. (edesc->assoc_nents +
  2088. (is_gcm ? 1 + edesc->src_nents : 0));
  2089. out_options = LDST_SGF;
  2090. } else {
  2091. dst_dma = edesc->sec4_sg_dma +
  2092. sec4_sg_index *
  2093. sizeof(struct sec4_sg_entry);
  2094. out_options = LDST_SGF;
  2095. }
  2096. }
  2097. append_seq_out_ptr(desc, dst_dma, ivsize + req->cryptlen + authsize,
  2098. out_options);
  2099. }
  2100. /*
  2101. * Fill in ablkcipher job descriptor
  2102. */
  2103. static void init_ablkcipher_job(u32 *sh_desc, dma_addr_t ptr,
  2104. struct ablkcipher_edesc *edesc,
  2105. struct ablkcipher_request *req,
  2106. bool iv_contig)
  2107. {
  2108. struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
  2109. int ivsize = crypto_ablkcipher_ivsize(ablkcipher);
  2110. u32 *desc = edesc->hw_desc;
  2111. u32 out_options = 0, in_options;
  2112. dma_addr_t dst_dma, src_dma;
  2113. int len, sec4_sg_index = 0;
  2114. #ifdef DEBUG
  2115. print_hex_dump(KERN_ERR, "presciv@"__stringify(__LINE__)": ",
  2116. DUMP_PREFIX_ADDRESS, 16, 4, req->info,
  2117. ivsize, 1);
  2118. print_hex_dump(KERN_ERR, "src @"__stringify(__LINE__)": ",
  2119. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
  2120. edesc->src_nents ? 100 : req->nbytes, 1);
  2121. #endif
  2122. len = desc_len(sh_desc);
  2123. init_job_desc_shared(desc, ptr, len, HDR_SHARE_DEFER | HDR_REVERSE);
  2124. if (iv_contig) {
  2125. src_dma = edesc->iv_dma;
  2126. in_options = 0;
  2127. } else {
  2128. src_dma = edesc->sec4_sg_dma;
  2129. sec4_sg_index += (iv_contig ? 0 : 1) + edesc->src_nents;
  2130. in_options = LDST_SGF;
  2131. }
  2132. append_seq_in_ptr(desc, src_dma, req->nbytes + ivsize, in_options);
  2133. if (likely(req->src == req->dst)) {
  2134. if (!edesc->src_nents && iv_contig) {
  2135. dst_dma = sg_dma_address(req->src);
  2136. } else {
  2137. dst_dma = edesc->sec4_sg_dma +
  2138. sizeof(struct sec4_sg_entry);
  2139. out_options = LDST_SGF;
  2140. }
  2141. } else {
  2142. if (!edesc->dst_nents) {
  2143. dst_dma = sg_dma_address(req->dst);
  2144. } else {
  2145. dst_dma = edesc->sec4_sg_dma +
  2146. sec4_sg_index * sizeof(struct sec4_sg_entry);
  2147. out_options = LDST_SGF;
  2148. }
  2149. }
  2150. append_seq_out_ptr(desc, dst_dma, req->nbytes, out_options);
  2151. }
  2152. /*
  2153. * Fill in ablkcipher givencrypt job descriptor
  2154. */
  2155. static void init_ablkcipher_giv_job(u32 *sh_desc, dma_addr_t ptr,
  2156. struct ablkcipher_edesc *edesc,
  2157. struct ablkcipher_request *req,
  2158. bool iv_contig)
  2159. {
  2160. struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
  2161. int ivsize = crypto_ablkcipher_ivsize(ablkcipher);
  2162. u32 *desc = edesc->hw_desc;
  2163. u32 out_options, in_options;
  2164. dma_addr_t dst_dma, src_dma;
  2165. int len, sec4_sg_index = 0;
  2166. #ifdef DEBUG
  2167. print_hex_dump(KERN_ERR, "presciv@" __stringify(__LINE__) ": ",
  2168. DUMP_PREFIX_ADDRESS, 16, 4, req->info,
  2169. ivsize, 1);
  2170. print_hex_dump(KERN_ERR, "src @" __stringify(__LINE__) ": ",
  2171. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
  2172. edesc->src_nents ? 100 : req->nbytes, 1);
  2173. #endif
  2174. len = desc_len(sh_desc);
  2175. init_job_desc_shared(desc, ptr, len, HDR_SHARE_DEFER | HDR_REVERSE);
  2176. if (!edesc->src_nents) {
  2177. src_dma = sg_dma_address(req->src);
  2178. in_options = 0;
  2179. } else {
  2180. src_dma = edesc->sec4_sg_dma;
  2181. sec4_sg_index += edesc->src_nents;
  2182. in_options = LDST_SGF;
  2183. }
  2184. append_seq_in_ptr(desc, src_dma, req->nbytes, in_options);
  2185. if (iv_contig) {
  2186. dst_dma = edesc->iv_dma;
  2187. out_options = 0;
  2188. } else {
  2189. dst_dma = edesc->sec4_sg_dma +
  2190. sec4_sg_index * sizeof(struct sec4_sg_entry);
  2191. out_options = LDST_SGF;
  2192. }
  2193. append_seq_out_ptr(desc, dst_dma, req->nbytes + ivsize, out_options);
  2194. }
  2195. /*
  2196. * allocate and map the aead extended descriptor
  2197. */
  2198. static struct aead_edesc *aead_edesc_alloc(struct aead_request *req,
  2199. int desc_bytes, bool *all_contig_ptr,
  2200. bool encrypt)
  2201. {
  2202. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  2203. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  2204. struct device *jrdev = ctx->jrdev;
  2205. gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
  2206. CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
  2207. int assoc_nents, src_nents, dst_nents = 0;
  2208. struct aead_edesc *edesc;
  2209. dma_addr_t iv_dma = 0;
  2210. int sgc;
  2211. bool all_contig = true;
  2212. bool assoc_chained = false, src_chained = false, dst_chained = false;
  2213. int ivsize = crypto_aead_ivsize(aead);
  2214. int sec4_sg_index, sec4_sg_len = 0, sec4_sg_bytes;
  2215. unsigned int authsize = ctx->authsize;
  2216. bool is_gcm = false;
  2217. assoc_nents = sg_count(req->assoc, req->assoclen, &assoc_chained);
  2218. if (unlikely(req->dst != req->src)) {
  2219. src_nents = sg_count(req->src, req->cryptlen, &src_chained);
  2220. dst_nents = sg_count(req->dst,
  2221. req->cryptlen +
  2222. (encrypt ? authsize : (-authsize)),
  2223. &dst_chained);
  2224. } else {
  2225. src_nents = sg_count(req->src,
  2226. req->cryptlen +
  2227. (encrypt ? authsize : 0),
  2228. &src_chained);
  2229. }
  2230. sgc = dma_map_sg_chained(jrdev, req->assoc, assoc_nents ? : 1,
  2231. DMA_TO_DEVICE, assoc_chained);
  2232. if (likely(req->src == req->dst)) {
  2233. sgc = dma_map_sg_chained(jrdev, req->src, src_nents ? : 1,
  2234. DMA_BIDIRECTIONAL, src_chained);
  2235. } else {
  2236. sgc = dma_map_sg_chained(jrdev, req->src, src_nents ? : 1,
  2237. DMA_TO_DEVICE, src_chained);
  2238. sgc = dma_map_sg_chained(jrdev, req->dst, dst_nents ? : 1,
  2239. DMA_FROM_DEVICE, dst_chained);
  2240. }
  2241. iv_dma = dma_map_single(jrdev, req->iv, ivsize, DMA_TO_DEVICE);
  2242. if (dma_mapping_error(jrdev, iv_dma)) {
  2243. dev_err(jrdev, "unable to map IV\n");
  2244. return ERR_PTR(-ENOMEM);
  2245. }
  2246. if (((ctx->class1_alg_type & OP_ALG_ALGSEL_MASK) ==
  2247. OP_ALG_ALGSEL_AES) &&
  2248. ((ctx->class1_alg_type & OP_ALG_AAI_MASK) == OP_ALG_AAI_GCM))
  2249. is_gcm = true;
  2250. /*
  2251. * Check if data are contiguous.
  2252. * GCM expected input sequence: IV, AAD, text
  2253. * All other - expected input sequence: AAD, IV, text
  2254. */
  2255. if (is_gcm)
  2256. all_contig = (!assoc_nents &&
  2257. iv_dma + ivsize == sg_dma_address(req->assoc) &&
  2258. !src_nents && sg_dma_address(req->assoc) +
  2259. req->assoclen == sg_dma_address(req->src));
  2260. else
  2261. all_contig = (!assoc_nents && sg_dma_address(req->assoc) +
  2262. req->assoclen == iv_dma && !src_nents &&
  2263. iv_dma + ivsize == sg_dma_address(req->src));
  2264. if (!all_contig) {
  2265. assoc_nents = assoc_nents ? : 1;
  2266. src_nents = src_nents ? : 1;
  2267. sec4_sg_len = assoc_nents + 1 + src_nents;
  2268. }
  2269. sec4_sg_len += dst_nents;
  2270. sec4_sg_bytes = sec4_sg_len * sizeof(struct sec4_sg_entry);
  2271. /* allocate space for base edesc and hw desc commands, link tables */
  2272. edesc = kmalloc(sizeof(struct aead_edesc) + desc_bytes +
  2273. sec4_sg_bytes, GFP_DMA | flags);
  2274. if (!edesc) {
  2275. dev_err(jrdev, "could not allocate extended descriptor\n");
  2276. return ERR_PTR(-ENOMEM);
  2277. }
  2278. edesc->assoc_nents = assoc_nents;
  2279. edesc->assoc_chained = assoc_chained;
  2280. edesc->src_nents = src_nents;
  2281. edesc->src_chained = src_chained;
  2282. edesc->dst_nents = dst_nents;
  2283. edesc->dst_chained = dst_chained;
  2284. edesc->iv_dma = iv_dma;
  2285. edesc->sec4_sg_bytes = sec4_sg_bytes;
  2286. edesc->sec4_sg = (void *)edesc + sizeof(struct aead_edesc) +
  2287. desc_bytes;
  2288. *all_contig_ptr = all_contig;
  2289. sec4_sg_index = 0;
  2290. if (!all_contig) {
  2291. if (!is_gcm) {
  2292. sg_to_sec4_sg(req->assoc,
  2293. (assoc_nents ? : 1),
  2294. edesc->sec4_sg +
  2295. sec4_sg_index, 0);
  2296. sec4_sg_index += assoc_nents ? : 1;
  2297. }
  2298. dma_to_sec4_sg_one(edesc->sec4_sg + sec4_sg_index,
  2299. iv_dma, ivsize, 0);
  2300. sec4_sg_index += 1;
  2301. if (is_gcm) {
  2302. sg_to_sec4_sg(req->assoc,
  2303. (assoc_nents ? : 1),
  2304. edesc->sec4_sg +
  2305. sec4_sg_index, 0);
  2306. sec4_sg_index += assoc_nents ? : 1;
  2307. }
  2308. sg_to_sec4_sg_last(req->src,
  2309. (src_nents ? : 1),
  2310. edesc->sec4_sg +
  2311. sec4_sg_index, 0);
  2312. sec4_sg_index += src_nents ? : 1;
  2313. }
  2314. if (dst_nents) {
  2315. sg_to_sec4_sg_last(req->dst, dst_nents,
  2316. edesc->sec4_sg + sec4_sg_index, 0);
  2317. }
  2318. edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
  2319. sec4_sg_bytes, DMA_TO_DEVICE);
  2320. if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
  2321. dev_err(jrdev, "unable to map S/G table\n");
  2322. return ERR_PTR(-ENOMEM);
  2323. }
  2324. return edesc;
  2325. }
  2326. static int aead_encrypt(struct aead_request *req)
  2327. {
  2328. struct aead_edesc *edesc;
  2329. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  2330. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  2331. struct device *jrdev = ctx->jrdev;
  2332. bool all_contig;
  2333. u32 *desc;
  2334. int ret = 0;
  2335. /* allocate extended descriptor */
  2336. edesc = aead_edesc_alloc(req, DESC_JOB_IO_LEN *
  2337. CAAM_CMD_SZ, &all_contig, true);
  2338. if (IS_ERR(edesc))
  2339. return PTR_ERR(edesc);
  2340. /* Create and submit job descriptor */
  2341. init_aead_job(ctx->sh_desc_enc, ctx->sh_desc_enc_dma, edesc, req,
  2342. all_contig, true);
  2343. #ifdef DEBUG
  2344. print_hex_dump(KERN_ERR, "aead jobdesc@"__stringify(__LINE__)": ",
  2345. DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
  2346. desc_bytes(edesc->hw_desc), 1);
  2347. #endif
  2348. desc = edesc->hw_desc;
  2349. ret = caam_jr_enqueue(jrdev, desc, aead_encrypt_done, req);
  2350. if (!ret) {
  2351. ret = -EINPROGRESS;
  2352. } else {
  2353. aead_unmap(jrdev, edesc, req);
  2354. kfree(edesc);
  2355. }
  2356. return ret;
  2357. }
  2358. static int aead_decrypt(struct aead_request *req)
  2359. {
  2360. struct aead_edesc *edesc;
  2361. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  2362. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  2363. struct device *jrdev = ctx->jrdev;
  2364. bool all_contig;
  2365. u32 *desc;
  2366. int ret = 0;
  2367. /* allocate extended descriptor */
  2368. edesc = aead_edesc_alloc(req, DESC_JOB_IO_LEN *
  2369. CAAM_CMD_SZ, &all_contig, false);
  2370. if (IS_ERR(edesc))
  2371. return PTR_ERR(edesc);
  2372. #ifdef DEBUG
  2373. print_hex_dump(KERN_ERR, "dec src@"__stringify(__LINE__)": ",
  2374. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
  2375. req->cryptlen, 1);
  2376. #endif
  2377. /* Create and submit job descriptor*/
  2378. init_aead_job(ctx->sh_desc_dec,
  2379. ctx->sh_desc_dec_dma, edesc, req, all_contig, false);
  2380. #ifdef DEBUG
  2381. print_hex_dump(KERN_ERR, "aead jobdesc@"__stringify(__LINE__)": ",
  2382. DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
  2383. desc_bytes(edesc->hw_desc), 1);
  2384. #endif
  2385. desc = edesc->hw_desc;
  2386. ret = caam_jr_enqueue(jrdev, desc, aead_decrypt_done, req);
  2387. if (!ret) {
  2388. ret = -EINPROGRESS;
  2389. } else {
  2390. aead_unmap(jrdev, edesc, req);
  2391. kfree(edesc);
  2392. }
  2393. return ret;
  2394. }
  2395. /*
  2396. * allocate and map the aead extended descriptor for aead givencrypt
  2397. */
  2398. static struct aead_edesc *aead_giv_edesc_alloc(struct aead_givcrypt_request
  2399. *greq, int desc_bytes,
  2400. u32 *contig_ptr)
  2401. {
  2402. struct aead_request *req = &greq->areq;
  2403. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  2404. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  2405. struct device *jrdev = ctx->jrdev;
  2406. gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
  2407. CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
  2408. int assoc_nents, src_nents, dst_nents = 0;
  2409. struct aead_edesc *edesc;
  2410. dma_addr_t iv_dma = 0;
  2411. int sgc;
  2412. u32 contig = GIV_SRC_CONTIG | GIV_DST_CONTIG;
  2413. int ivsize = crypto_aead_ivsize(aead);
  2414. bool assoc_chained = false, src_chained = false, dst_chained = false;
  2415. int sec4_sg_index, sec4_sg_len = 0, sec4_sg_bytes;
  2416. bool is_gcm = false;
  2417. assoc_nents = sg_count(req->assoc, req->assoclen, &assoc_chained);
  2418. src_nents = sg_count(req->src, req->cryptlen, &src_chained);
  2419. if (unlikely(req->dst != req->src))
  2420. dst_nents = sg_count(req->dst, req->cryptlen + ctx->authsize,
  2421. &dst_chained);
  2422. sgc = dma_map_sg_chained(jrdev, req->assoc, assoc_nents ? : 1,
  2423. DMA_TO_DEVICE, assoc_chained);
  2424. if (likely(req->src == req->dst)) {
  2425. sgc = dma_map_sg_chained(jrdev, req->src, src_nents ? : 1,
  2426. DMA_BIDIRECTIONAL, src_chained);
  2427. } else {
  2428. sgc = dma_map_sg_chained(jrdev, req->src, src_nents ? : 1,
  2429. DMA_TO_DEVICE, src_chained);
  2430. sgc = dma_map_sg_chained(jrdev, req->dst, dst_nents ? : 1,
  2431. DMA_FROM_DEVICE, dst_chained);
  2432. }
  2433. iv_dma = dma_map_single(jrdev, greq->giv, ivsize, DMA_TO_DEVICE);
  2434. if (dma_mapping_error(jrdev, iv_dma)) {
  2435. dev_err(jrdev, "unable to map IV\n");
  2436. return ERR_PTR(-ENOMEM);
  2437. }
  2438. if (((ctx->class1_alg_type & OP_ALG_ALGSEL_MASK) ==
  2439. OP_ALG_ALGSEL_AES) &&
  2440. ((ctx->class1_alg_type & OP_ALG_AAI_MASK) == OP_ALG_AAI_GCM))
  2441. is_gcm = true;
  2442. /*
  2443. * Check if data are contiguous.
  2444. * GCM expected input sequence: IV, AAD, text
  2445. * All other - expected input sequence: AAD, IV, text
  2446. */
  2447. if (is_gcm) {
  2448. if (assoc_nents || iv_dma + ivsize !=
  2449. sg_dma_address(req->assoc) || src_nents ||
  2450. sg_dma_address(req->assoc) + req->assoclen !=
  2451. sg_dma_address(req->src))
  2452. contig &= ~GIV_SRC_CONTIG;
  2453. } else {
  2454. if (assoc_nents ||
  2455. sg_dma_address(req->assoc) + req->assoclen != iv_dma ||
  2456. src_nents || iv_dma + ivsize != sg_dma_address(req->src))
  2457. contig &= ~GIV_SRC_CONTIG;
  2458. }
  2459. if (dst_nents || iv_dma + ivsize != sg_dma_address(req->dst))
  2460. contig &= ~GIV_DST_CONTIG;
  2461. if (!(contig & GIV_SRC_CONTIG)) {
  2462. assoc_nents = assoc_nents ? : 1;
  2463. src_nents = src_nents ? : 1;
  2464. sec4_sg_len += assoc_nents + 1 + src_nents;
  2465. if (req->src == req->dst &&
  2466. (src_nents || iv_dma + ivsize != sg_dma_address(req->src)))
  2467. contig &= ~GIV_DST_CONTIG;
  2468. }
  2469. /*
  2470. * Add new sg entries for GCM output sequence.
  2471. * Expected output sequence: IV, encrypted text.
  2472. */
  2473. if (is_gcm && req->src == req->dst && !(contig & GIV_DST_CONTIG))
  2474. sec4_sg_len += 1 + src_nents;
  2475. if (unlikely(req->src != req->dst)) {
  2476. dst_nents = dst_nents ? : 1;
  2477. sec4_sg_len += 1 + dst_nents;
  2478. }
  2479. sec4_sg_bytes = sec4_sg_len * sizeof(struct sec4_sg_entry);
  2480. /* allocate space for base edesc and hw desc commands, link tables */
  2481. edesc = kmalloc(sizeof(struct aead_edesc) + desc_bytes +
  2482. sec4_sg_bytes, GFP_DMA | flags);
  2483. if (!edesc) {
  2484. dev_err(jrdev, "could not allocate extended descriptor\n");
  2485. return ERR_PTR(-ENOMEM);
  2486. }
  2487. edesc->assoc_nents = assoc_nents;
  2488. edesc->assoc_chained = assoc_chained;
  2489. edesc->src_nents = src_nents;
  2490. edesc->src_chained = src_chained;
  2491. edesc->dst_nents = dst_nents;
  2492. edesc->dst_chained = dst_chained;
  2493. edesc->iv_dma = iv_dma;
  2494. edesc->sec4_sg_bytes = sec4_sg_bytes;
  2495. edesc->sec4_sg = (void *)edesc + sizeof(struct aead_edesc) +
  2496. desc_bytes;
  2497. *contig_ptr = contig;
  2498. sec4_sg_index = 0;
  2499. if (!(contig & GIV_SRC_CONTIG)) {
  2500. if (!is_gcm) {
  2501. sg_to_sec4_sg(req->assoc, assoc_nents,
  2502. edesc->sec4_sg + sec4_sg_index, 0);
  2503. sec4_sg_index += assoc_nents;
  2504. }
  2505. dma_to_sec4_sg_one(edesc->sec4_sg + sec4_sg_index,
  2506. iv_dma, ivsize, 0);
  2507. sec4_sg_index += 1;
  2508. if (is_gcm) {
  2509. sg_to_sec4_sg(req->assoc, assoc_nents,
  2510. edesc->sec4_sg + sec4_sg_index, 0);
  2511. sec4_sg_index += assoc_nents;
  2512. }
  2513. sg_to_sec4_sg_last(req->src, src_nents,
  2514. edesc->sec4_sg +
  2515. sec4_sg_index, 0);
  2516. sec4_sg_index += src_nents;
  2517. }
  2518. if (is_gcm && req->src == req->dst && !(contig & GIV_DST_CONTIG)) {
  2519. dma_to_sec4_sg_one(edesc->sec4_sg + sec4_sg_index,
  2520. iv_dma, ivsize, 0);
  2521. sec4_sg_index += 1;
  2522. sg_to_sec4_sg_last(req->src, src_nents,
  2523. edesc->sec4_sg + sec4_sg_index, 0);
  2524. }
  2525. if (unlikely(req->src != req->dst && !(contig & GIV_DST_CONTIG))) {
  2526. dma_to_sec4_sg_one(edesc->sec4_sg + sec4_sg_index,
  2527. iv_dma, ivsize, 0);
  2528. sec4_sg_index += 1;
  2529. sg_to_sec4_sg_last(req->dst, dst_nents,
  2530. edesc->sec4_sg + sec4_sg_index, 0);
  2531. }
  2532. edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
  2533. sec4_sg_bytes, DMA_TO_DEVICE);
  2534. if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
  2535. dev_err(jrdev, "unable to map S/G table\n");
  2536. return ERR_PTR(-ENOMEM);
  2537. }
  2538. return edesc;
  2539. }
  2540. static int aead_givencrypt(struct aead_givcrypt_request *areq)
  2541. {
  2542. struct aead_request *req = &areq->areq;
  2543. struct aead_edesc *edesc;
  2544. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  2545. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  2546. struct device *jrdev = ctx->jrdev;
  2547. u32 contig;
  2548. u32 *desc;
  2549. int ret = 0;
  2550. /* allocate extended descriptor */
  2551. edesc = aead_giv_edesc_alloc(areq, DESC_JOB_IO_LEN *
  2552. CAAM_CMD_SZ, &contig);
  2553. if (IS_ERR(edesc))
  2554. return PTR_ERR(edesc);
  2555. #ifdef DEBUG
  2556. print_hex_dump(KERN_ERR, "giv src@"__stringify(__LINE__)": ",
  2557. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
  2558. req->cryptlen, 1);
  2559. #endif
  2560. /* Create and submit job descriptor*/
  2561. init_aead_giv_job(ctx->sh_desc_givenc,
  2562. ctx->sh_desc_givenc_dma, edesc, req, contig);
  2563. #ifdef DEBUG
  2564. print_hex_dump(KERN_ERR, "aead jobdesc@"__stringify(__LINE__)": ",
  2565. DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
  2566. desc_bytes(edesc->hw_desc), 1);
  2567. #endif
  2568. desc = edesc->hw_desc;
  2569. ret = caam_jr_enqueue(jrdev, desc, aead_encrypt_done, req);
  2570. if (!ret) {
  2571. ret = -EINPROGRESS;
  2572. } else {
  2573. aead_unmap(jrdev, edesc, req);
  2574. kfree(edesc);
  2575. }
  2576. return ret;
  2577. }
  2578. static int aead_null_givencrypt(struct aead_givcrypt_request *areq)
  2579. {
  2580. return aead_encrypt(&areq->areq);
  2581. }
  2582. /*
  2583. * allocate and map the ablkcipher extended descriptor for ablkcipher
  2584. */
  2585. static struct ablkcipher_edesc *ablkcipher_edesc_alloc(struct ablkcipher_request
  2586. *req, int desc_bytes,
  2587. bool *iv_contig_out)
  2588. {
  2589. struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
  2590. struct caam_ctx *ctx = crypto_ablkcipher_ctx(ablkcipher);
  2591. struct device *jrdev = ctx->jrdev;
  2592. gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
  2593. CRYPTO_TFM_REQ_MAY_SLEEP)) ?
  2594. GFP_KERNEL : GFP_ATOMIC;
  2595. int src_nents, dst_nents = 0, sec4_sg_bytes;
  2596. struct ablkcipher_edesc *edesc;
  2597. dma_addr_t iv_dma = 0;
  2598. bool iv_contig = false;
  2599. int sgc;
  2600. int ivsize = crypto_ablkcipher_ivsize(ablkcipher);
  2601. bool src_chained = false, dst_chained = false;
  2602. int sec4_sg_index;
  2603. src_nents = sg_count(req->src, req->nbytes, &src_chained);
  2604. if (req->dst != req->src)
  2605. dst_nents = sg_count(req->dst, req->nbytes, &dst_chained);
  2606. if (likely(req->src == req->dst)) {
  2607. sgc = dma_map_sg_chained(jrdev, req->src, src_nents ? : 1,
  2608. DMA_BIDIRECTIONAL, src_chained);
  2609. } else {
  2610. sgc = dma_map_sg_chained(jrdev, req->src, src_nents ? : 1,
  2611. DMA_TO_DEVICE, src_chained);
  2612. sgc = dma_map_sg_chained(jrdev, req->dst, dst_nents ? : 1,
  2613. DMA_FROM_DEVICE, dst_chained);
  2614. }
  2615. iv_dma = dma_map_single(jrdev, req->info, ivsize, DMA_TO_DEVICE);
  2616. if (dma_mapping_error(jrdev, iv_dma)) {
  2617. dev_err(jrdev, "unable to map IV\n");
  2618. return ERR_PTR(-ENOMEM);
  2619. }
  2620. /*
  2621. * Check if iv can be contiguous with source and destination.
  2622. * If so, include it. If not, create scatterlist.
  2623. */
  2624. if (!src_nents && iv_dma + ivsize == sg_dma_address(req->src))
  2625. iv_contig = true;
  2626. else
  2627. src_nents = src_nents ? : 1;
  2628. sec4_sg_bytes = ((iv_contig ? 0 : 1) + src_nents + dst_nents) *
  2629. sizeof(struct sec4_sg_entry);
  2630. /* allocate space for base edesc and hw desc commands, link tables */
  2631. edesc = kmalloc(sizeof(struct ablkcipher_edesc) + desc_bytes +
  2632. sec4_sg_bytes, GFP_DMA | flags);
  2633. if (!edesc) {
  2634. dev_err(jrdev, "could not allocate extended descriptor\n");
  2635. return ERR_PTR(-ENOMEM);
  2636. }
  2637. edesc->src_nents = src_nents;
  2638. edesc->src_chained = src_chained;
  2639. edesc->dst_nents = dst_nents;
  2640. edesc->dst_chained = dst_chained;
  2641. edesc->sec4_sg_bytes = sec4_sg_bytes;
  2642. edesc->sec4_sg = (void *)edesc + sizeof(struct ablkcipher_edesc) +
  2643. desc_bytes;
  2644. sec4_sg_index = 0;
  2645. if (!iv_contig) {
  2646. dma_to_sec4_sg_one(edesc->sec4_sg, iv_dma, ivsize, 0);
  2647. sg_to_sec4_sg_last(req->src, src_nents,
  2648. edesc->sec4_sg + 1, 0);
  2649. sec4_sg_index += 1 + src_nents;
  2650. }
  2651. if (dst_nents) {
  2652. sg_to_sec4_sg_last(req->dst, dst_nents,
  2653. edesc->sec4_sg + sec4_sg_index, 0);
  2654. }
  2655. edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
  2656. sec4_sg_bytes, DMA_TO_DEVICE);
  2657. if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
  2658. dev_err(jrdev, "unable to map S/G table\n");
  2659. return ERR_PTR(-ENOMEM);
  2660. }
  2661. edesc->iv_dma = iv_dma;
  2662. #ifdef DEBUG
  2663. print_hex_dump(KERN_ERR, "ablkcipher sec4_sg@"__stringify(__LINE__)": ",
  2664. DUMP_PREFIX_ADDRESS, 16, 4, edesc->sec4_sg,
  2665. sec4_sg_bytes, 1);
  2666. #endif
  2667. *iv_contig_out = iv_contig;
  2668. return edesc;
  2669. }
  2670. static int ablkcipher_encrypt(struct ablkcipher_request *req)
  2671. {
  2672. struct ablkcipher_edesc *edesc;
  2673. struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
  2674. struct caam_ctx *ctx = crypto_ablkcipher_ctx(ablkcipher);
  2675. struct device *jrdev = ctx->jrdev;
  2676. bool iv_contig;
  2677. u32 *desc;
  2678. int ret = 0;
  2679. /* allocate extended descriptor */
  2680. edesc = ablkcipher_edesc_alloc(req, DESC_JOB_IO_LEN *
  2681. CAAM_CMD_SZ, &iv_contig);
  2682. if (IS_ERR(edesc))
  2683. return PTR_ERR(edesc);
  2684. /* Create and submit job descriptor*/
  2685. init_ablkcipher_job(ctx->sh_desc_enc,
  2686. ctx->sh_desc_enc_dma, edesc, req, iv_contig);
  2687. #ifdef DEBUG
  2688. print_hex_dump(KERN_ERR, "ablkcipher jobdesc@"__stringify(__LINE__)": ",
  2689. DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
  2690. desc_bytes(edesc->hw_desc), 1);
  2691. #endif
  2692. desc = edesc->hw_desc;
  2693. ret = caam_jr_enqueue(jrdev, desc, ablkcipher_encrypt_done, req);
  2694. if (!ret) {
  2695. ret = -EINPROGRESS;
  2696. } else {
  2697. ablkcipher_unmap(jrdev, edesc, req);
  2698. kfree(edesc);
  2699. }
  2700. return ret;
  2701. }
  2702. static int ablkcipher_decrypt(struct ablkcipher_request *req)
  2703. {
  2704. struct ablkcipher_edesc *edesc;
  2705. struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
  2706. struct caam_ctx *ctx = crypto_ablkcipher_ctx(ablkcipher);
  2707. struct device *jrdev = ctx->jrdev;
  2708. bool iv_contig;
  2709. u32 *desc;
  2710. int ret = 0;
  2711. /* allocate extended descriptor */
  2712. edesc = ablkcipher_edesc_alloc(req, DESC_JOB_IO_LEN *
  2713. CAAM_CMD_SZ, &iv_contig);
  2714. if (IS_ERR(edesc))
  2715. return PTR_ERR(edesc);
  2716. /* Create and submit job descriptor*/
  2717. init_ablkcipher_job(ctx->sh_desc_dec,
  2718. ctx->sh_desc_dec_dma, edesc, req, iv_contig);
  2719. desc = edesc->hw_desc;
  2720. #ifdef DEBUG
  2721. print_hex_dump(KERN_ERR, "ablkcipher jobdesc@"__stringify(__LINE__)": ",
  2722. DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
  2723. desc_bytes(edesc->hw_desc), 1);
  2724. #endif
  2725. ret = caam_jr_enqueue(jrdev, desc, ablkcipher_decrypt_done, req);
  2726. if (!ret) {
  2727. ret = -EINPROGRESS;
  2728. } else {
  2729. ablkcipher_unmap(jrdev, edesc, req);
  2730. kfree(edesc);
  2731. }
  2732. return ret;
  2733. }
  2734. /*
  2735. * allocate and map the ablkcipher extended descriptor
  2736. * for ablkcipher givencrypt
  2737. */
  2738. static struct ablkcipher_edesc *ablkcipher_giv_edesc_alloc(
  2739. struct skcipher_givcrypt_request *greq,
  2740. int desc_bytes,
  2741. bool *iv_contig_out)
  2742. {
  2743. struct ablkcipher_request *req = &greq->creq;
  2744. struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
  2745. struct caam_ctx *ctx = crypto_ablkcipher_ctx(ablkcipher);
  2746. struct device *jrdev = ctx->jrdev;
  2747. gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
  2748. CRYPTO_TFM_REQ_MAY_SLEEP)) ?
  2749. GFP_KERNEL : GFP_ATOMIC;
  2750. int src_nents, dst_nents = 0, sec4_sg_bytes;
  2751. struct ablkcipher_edesc *edesc;
  2752. dma_addr_t iv_dma = 0;
  2753. bool iv_contig = false;
  2754. int sgc;
  2755. int ivsize = crypto_ablkcipher_ivsize(ablkcipher);
  2756. bool src_chained = false, dst_chained = false;
  2757. int sec4_sg_index;
  2758. src_nents = sg_count(req->src, req->nbytes, &src_chained);
  2759. if (unlikely(req->dst != req->src))
  2760. dst_nents = sg_count(req->dst, req->nbytes, &dst_chained);
  2761. if (likely(req->src == req->dst)) {
  2762. sgc = dma_map_sg_chained(jrdev, req->src, src_nents ? : 1,
  2763. DMA_BIDIRECTIONAL, src_chained);
  2764. } else {
  2765. sgc = dma_map_sg_chained(jrdev, req->src, src_nents ? : 1,
  2766. DMA_TO_DEVICE, src_chained);
  2767. sgc = dma_map_sg_chained(jrdev, req->dst, dst_nents ? : 1,
  2768. DMA_FROM_DEVICE, dst_chained);
  2769. }
  2770. /*
  2771. * Check if iv can be contiguous with source and destination.
  2772. * If so, include it. If not, create scatterlist.
  2773. */
  2774. iv_dma = dma_map_single(jrdev, greq->giv, ivsize, DMA_TO_DEVICE);
  2775. if (dma_mapping_error(jrdev, iv_dma)) {
  2776. dev_err(jrdev, "unable to map IV\n");
  2777. return ERR_PTR(-ENOMEM);
  2778. }
  2779. if (!dst_nents && iv_dma + ivsize == sg_dma_address(req->dst))
  2780. iv_contig = true;
  2781. else
  2782. dst_nents = dst_nents ? : 1;
  2783. sec4_sg_bytes = ((iv_contig ? 0 : 1) + src_nents + dst_nents) *
  2784. sizeof(struct sec4_sg_entry);
  2785. /* allocate space for base edesc and hw desc commands, link tables */
  2786. edesc = kmalloc(sizeof(*edesc) + desc_bytes +
  2787. sec4_sg_bytes, GFP_DMA | flags);
  2788. if (!edesc) {
  2789. dev_err(jrdev, "could not allocate extended descriptor\n");
  2790. return ERR_PTR(-ENOMEM);
  2791. }
  2792. edesc->src_nents = src_nents;
  2793. edesc->src_chained = src_chained;
  2794. edesc->dst_nents = dst_nents;
  2795. edesc->dst_chained = dst_chained;
  2796. edesc->sec4_sg_bytes = sec4_sg_bytes;
  2797. edesc->sec4_sg = (void *)edesc + sizeof(struct ablkcipher_edesc) +
  2798. desc_bytes;
  2799. sec4_sg_index = 0;
  2800. if (src_nents) {
  2801. sg_to_sec4_sg_last(req->src, src_nents, edesc->sec4_sg, 0);
  2802. sec4_sg_index += src_nents;
  2803. }
  2804. if (!iv_contig) {
  2805. dma_to_sec4_sg_one(edesc->sec4_sg + sec4_sg_index,
  2806. iv_dma, ivsize, 0);
  2807. sec4_sg_index += 1;
  2808. sg_to_sec4_sg_last(req->dst, dst_nents,
  2809. edesc->sec4_sg + sec4_sg_index, 0);
  2810. }
  2811. edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
  2812. sec4_sg_bytes, DMA_TO_DEVICE);
  2813. if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
  2814. dev_err(jrdev, "unable to map S/G table\n");
  2815. return ERR_PTR(-ENOMEM);
  2816. }
  2817. edesc->iv_dma = iv_dma;
  2818. #ifdef DEBUG
  2819. print_hex_dump(KERN_ERR,
  2820. "ablkcipher sec4_sg@" __stringify(__LINE__) ": ",
  2821. DUMP_PREFIX_ADDRESS, 16, 4, edesc->sec4_sg,
  2822. sec4_sg_bytes, 1);
  2823. #endif
  2824. *iv_contig_out = iv_contig;
  2825. return edesc;
  2826. }
  2827. static int ablkcipher_givencrypt(struct skcipher_givcrypt_request *creq)
  2828. {
  2829. struct ablkcipher_request *req = &creq->creq;
  2830. struct ablkcipher_edesc *edesc;
  2831. struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
  2832. struct caam_ctx *ctx = crypto_ablkcipher_ctx(ablkcipher);
  2833. struct device *jrdev = ctx->jrdev;
  2834. bool iv_contig;
  2835. u32 *desc;
  2836. int ret = 0;
  2837. /* allocate extended descriptor */
  2838. edesc = ablkcipher_giv_edesc_alloc(creq, DESC_JOB_IO_LEN *
  2839. CAAM_CMD_SZ, &iv_contig);
  2840. if (IS_ERR(edesc))
  2841. return PTR_ERR(edesc);
  2842. /* Create and submit job descriptor*/
  2843. init_ablkcipher_giv_job(ctx->sh_desc_givenc, ctx->sh_desc_givenc_dma,
  2844. edesc, req, iv_contig);
  2845. #ifdef DEBUG
  2846. print_hex_dump(KERN_ERR,
  2847. "ablkcipher jobdesc@" __stringify(__LINE__) ": ",
  2848. DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
  2849. desc_bytes(edesc->hw_desc), 1);
  2850. #endif
  2851. desc = edesc->hw_desc;
  2852. ret = caam_jr_enqueue(jrdev, desc, ablkcipher_encrypt_done, req);
  2853. if (!ret) {
  2854. ret = -EINPROGRESS;
  2855. } else {
  2856. ablkcipher_unmap(jrdev, edesc, req);
  2857. kfree(edesc);
  2858. }
  2859. return ret;
  2860. }
  2861. #define template_aead template_u.aead
  2862. #define template_ablkcipher template_u.ablkcipher
  2863. struct caam_alg_template {
  2864. char name[CRYPTO_MAX_ALG_NAME];
  2865. char driver_name[CRYPTO_MAX_ALG_NAME];
  2866. unsigned int blocksize;
  2867. u32 type;
  2868. union {
  2869. struct ablkcipher_alg ablkcipher;
  2870. struct aead_alg aead;
  2871. struct blkcipher_alg blkcipher;
  2872. struct cipher_alg cipher;
  2873. struct compress_alg compress;
  2874. struct rng_alg rng;
  2875. } template_u;
  2876. u32 class1_alg_type;
  2877. u32 class2_alg_type;
  2878. u32 alg_op;
  2879. };
  2880. static struct caam_alg_template driver_algs[] = {
  2881. /* single-pass ipsec_esp descriptor */
  2882. {
  2883. .name = "authenc(hmac(md5),ecb(cipher_null))",
  2884. .driver_name = "authenc-hmac-md5-ecb-cipher_null-caam",
  2885. .blocksize = NULL_BLOCK_SIZE,
  2886. .type = CRYPTO_ALG_TYPE_AEAD,
  2887. .template_aead = {
  2888. .setkey = aead_setkey,
  2889. .setauthsize = aead_setauthsize,
  2890. .encrypt = aead_encrypt,
  2891. .decrypt = aead_decrypt,
  2892. .givencrypt = aead_null_givencrypt,
  2893. .geniv = "<built-in>",
  2894. .ivsize = NULL_IV_SIZE,
  2895. .maxauthsize = MD5_DIGEST_SIZE,
  2896. },
  2897. .class1_alg_type = 0,
  2898. .class2_alg_type = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC_PRECOMP,
  2899. .alg_op = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC,
  2900. },
  2901. {
  2902. .name = "authenc(hmac(sha1),ecb(cipher_null))",
  2903. .driver_name = "authenc-hmac-sha1-ecb-cipher_null-caam",
  2904. .blocksize = NULL_BLOCK_SIZE,
  2905. .type = CRYPTO_ALG_TYPE_AEAD,
  2906. .template_aead = {
  2907. .setkey = aead_setkey,
  2908. .setauthsize = aead_setauthsize,
  2909. .encrypt = aead_encrypt,
  2910. .decrypt = aead_decrypt,
  2911. .givencrypt = aead_null_givencrypt,
  2912. .geniv = "<built-in>",
  2913. .ivsize = NULL_IV_SIZE,
  2914. .maxauthsize = SHA1_DIGEST_SIZE,
  2915. },
  2916. .class1_alg_type = 0,
  2917. .class2_alg_type = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC_PRECOMP,
  2918. .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC,
  2919. },
  2920. {
  2921. .name = "authenc(hmac(sha224),ecb(cipher_null))",
  2922. .driver_name = "authenc-hmac-sha224-ecb-cipher_null-caam",
  2923. .blocksize = NULL_BLOCK_SIZE,
  2924. .type = CRYPTO_ALG_TYPE_AEAD,
  2925. .template_aead = {
  2926. .setkey = aead_setkey,
  2927. .setauthsize = aead_setauthsize,
  2928. .encrypt = aead_encrypt,
  2929. .decrypt = aead_decrypt,
  2930. .givencrypt = aead_null_givencrypt,
  2931. .geniv = "<built-in>",
  2932. .ivsize = NULL_IV_SIZE,
  2933. .maxauthsize = SHA224_DIGEST_SIZE,
  2934. },
  2935. .class1_alg_type = 0,
  2936. .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
  2937. OP_ALG_AAI_HMAC_PRECOMP,
  2938. .alg_op = OP_ALG_ALGSEL_SHA224 | OP_ALG_AAI_HMAC,
  2939. },
  2940. {
  2941. .name = "authenc(hmac(sha256),ecb(cipher_null))",
  2942. .driver_name = "authenc-hmac-sha256-ecb-cipher_null-caam",
  2943. .blocksize = NULL_BLOCK_SIZE,
  2944. .type = CRYPTO_ALG_TYPE_AEAD,
  2945. .template_aead = {
  2946. .setkey = aead_setkey,
  2947. .setauthsize = aead_setauthsize,
  2948. .encrypt = aead_encrypt,
  2949. .decrypt = aead_decrypt,
  2950. .givencrypt = aead_null_givencrypt,
  2951. .geniv = "<built-in>",
  2952. .ivsize = NULL_IV_SIZE,
  2953. .maxauthsize = SHA256_DIGEST_SIZE,
  2954. },
  2955. .class1_alg_type = 0,
  2956. .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
  2957. OP_ALG_AAI_HMAC_PRECOMP,
  2958. .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC,
  2959. },
  2960. {
  2961. .name = "authenc(hmac(sha384),ecb(cipher_null))",
  2962. .driver_name = "authenc-hmac-sha384-ecb-cipher_null-caam",
  2963. .blocksize = NULL_BLOCK_SIZE,
  2964. .type = CRYPTO_ALG_TYPE_AEAD,
  2965. .template_aead = {
  2966. .setkey = aead_setkey,
  2967. .setauthsize = aead_setauthsize,
  2968. .encrypt = aead_encrypt,
  2969. .decrypt = aead_decrypt,
  2970. .givencrypt = aead_null_givencrypt,
  2971. .geniv = "<built-in>",
  2972. .ivsize = NULL_IV_SIZE,
  2973. .maxauthsize = SHA384_DIGEST_SIZE,
  2974. },
  2975. .class1_alg_type = 0,
  2976. .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
  2977. OP_ALG_AAI_HMAC_PRECOMP,
  2978. .alg_op = OP_ALG_ALGSEL_SHA384 | OP_ALG_AAI_HMAC,
  2979. },
  2980. {
  2981. .name = "authenc(hmac(sha512),ecb(cipher_null))",
  2982. .driver_name = "authenc-hmac-sha512-ecb-cipher_null-caam",
  2983. .blocksize = NULL_BLOCK_SIZE,
  2984. .type = CRYPTO_ALG_TYPE_AEAD,
  2985. .template_aead = {
  2986. .setkey = aead_setkey,
  2987. .setauthsize = aead_setauthsize,
  2988. .encrypt = aead_encrypt,
  2989. .decrypt = aead_decrypt,
  2990. .givencrypt = aead_null_givencrypt,
  2991. .geniv = "<built-in>",
  2992. .ivsize = NULL_IV_SIZE,
  2993. .maxauthsize = SHA512_DIGEST_SIZE,
  2994. },
  2995. .class1_alg_type = 0,
  2996. .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
  2997. OP_ALG_AAI_HMAC_PRECOMP,
  2998. .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC,
  2999. },
  3000. {
  3001. .name = "authenc(hmac(md5),cbc(aes))",
  3002. .driver_name = "authenc-hmac-md5-cbc-aes-caam",
  3003. .blocksize = AES_BLOCK_SIZE,
  3004. .type = CRYPTO_ALG_TYPE_AEAD,
  3005. .template_aead = {
  3006. .setkey = aead_setkey,
  3007. .setauthsize = aead_setauthsize,
  3008. .encrypt = aead_encrypt,
  3009. .decrypt = aead_decrypt,
  3010. .givencrypt = aead_givencrypt,
  3011. .geniv = "<built-in>",
  3012. .ivsize = AES_BLOCK_SIZE,
  3013. .maxauthsize = MD5_DIGEST_SIZE,
  3014. },
  3015. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  3016. .class2_alg_type = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC_PRECOMP,
  3017. .alg_op = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC,
  3018. },
  3019. {
  3020. .name = "authenc(hmac(sha1),cbc(aes))",
  3021. .driver_name = "authenc-hmac-sha1-cbc-aes-caam",
  3022. .blocksize = AES_BLOCK_SIZE,
  3023. .type = CRYPTO_ALG_TYPE_AEAD,
  3024. .template_aead = {
  3025. .setkey = aead_setkey,
  3026. .setauthsize = aead_setauthsize,
  3027. .encrypt = aead_encrypt,
  3028. .decrypt = aead_decrypt,
  3029. .givencrypt = aead_givencrypt,
  3030. .geniv = "<built-in>",
  3031. .ivsize = AES_BLOCK_SIZE,
  3032. .maxauthsize = SHA1_DIGEST_SIZE,
  3033. },
  3034. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  3035. .class2_alg_type = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC_PRECOMP,
  3036. .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC,
  3037. },
  3038. {
  3039. .name = "authenc(hmac(sha224),cbc(aes))",
  3040. .driver_name = "authenc-hmac-sha224-cbc-aes-caam",
  3041. .blocksize = AES_BLOCK_SIZE,
  3042. .type = CRYPTO_ALG_TYPE_AEAD,
  3043. .template_aead = {
  3044. .setkey = aead_setkey,
  3045. .setauthsize = aead_setauthsize,
  3046. .encrypt = aead_encrypt,
  3047. .decrypt = aead_decrypt,
  3048. .givencrypt = aead_givencrypt,
  3049. .geniv = "<built-in>",
  3050. .ivsize = AES_BLOCK_SIZE,
  3051. .maxauthsize = SHA224_DIGEST_SIZE,
  3052. },
  3053. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  3054. .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
  3055. OP_ALG_AAI_HMAC_PRECOMP,
  3056. .alg_op = OP_ALG_ALGSEL_SHA224 | OP_ALG_AAI_HMAC,
  3057. },
  3058. {
  3059. .name = "authenc(hmac(sha256),cbc(aes))",
  3060. .driver_name = "authenc-hmac-sha256-cbc-aes-caam",
  3061. .blocksize = AES_BLOCK_SIZE,
  3062. .type = CRYPTO_ALG_TYPE_AEAD,
  3063. .template_aead = {
  3064. .setkey = aead_setkey,
  3065. .setauthsize = aead_setauthsize,
  3066. .encrypt = aead_encrypt,
  3067. .decrypt = aead_decrypt,
  3068. .givencrypt = aead_givencrypt,
  3069. .geniv = "<built-in>",
  3070. .ivsize = AES_BLOCK_SIZE,
  3071. .maxauthsize = SHA256_DIGEST_SIZE,
  3072. },
  3073. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  3074. .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
  3075. OP_ALG_AAI_HMAC_PRECOMP,
  3076. .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC,
  3077. },
  3078. {
  3079. .name = "authenc(hmac(sha384),cbc(aes))",
  3080. .driver_name = "authenc-hmac-sha384-cbc-aes-caam",
  3081. .blocksize = AES_BLOCK_SIZE,
  3082. .type = CRYPTO_ALG_TYPE_AEAD,
  3083. .template_aead = {
  3084. .setkey = aead_setkey,
  3085. .setauthsize = aead_setauthsize,
  3086. .encrypt = aead_encrypt,
  3087. .decrypt = aead_decrypt,
  3088. .givencrypt = aead_givencrypt,
  3089. .geniv = "<built-in>",
  3090. .ivsize = AES_BLOCK_SIZE,
  3091. .maxauthsize = SHA384_DIGEST_SIZE,
  3092. },
  3093. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  3094. .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
  3095. OP_ALG_AAI_HMAC_PRECOMP,
  3096. .alg_op = OP_ALG_ALGSEL_SHA384 | OP_ALG_AAI_HMAC,
  3097. },
  3098. {
  3099. .name = "authenc(hmac(sha512),cbc(aes))",
  3100. .driver_name = "authenc-hmac-sha512-cbc-aes-caam",
  3101. .blocksize = AES_BLOCK_SIZE,
  3102. .type = CRYPTO_ALG_TYPE_AEAD,
  3103. .template_aead = {
  3104. .setkey = aead_setkey,
  3105. .setauthsize = aead_setauthsize,
  3106. .encrypt = aead_encrypt,
  3107. .decrypt = aead_decrypt,
  3108. .givencrypt = aead_givencrypt,
  3109. .geniv = "<built-in>",
  3110. .ivsize = AES_BLOCK_SIZE,
  3111. .maxauthsize = SHA512_DIGEST_SIZE,
  3112. },
  3113. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  3114. .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
  3115. OP_ALG_AAI_HMAC_PRECOMP,
  3116. .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC,
  3117. },
  3118. {
  3119. .name = "authenc(hmac(md5),cbc(des3_ede))",
  3120. .driver_name = "authenc-hmac-md5-cbc-des3_ede-caam",
  3121. .blocksize = DES3_EDE_BLOCK_SIZE,
  3122. .type = CRYPTO_ALG_TYPE_AEAD,
  3123. .template_aead = {
  3124. .setkey = aead_setkey,
  3125. .setauthsize = aead_setauthsize,
  3126. .encrypt = aead_encrypt,
  3127. .decrypt = aead_decrypt,
  3128. .givencrypt = aead_givencrypt,
  3129. .geniv = "<built-in>",
  3130. .ivsize = DES3_EDE_BLOCK_SIZE,
  3131. .maxauthsize = MD5_DIGEST_SIZE,
  3132. },
  3133. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  3134. .class2_alg_type = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC_PRECOMP,
  3135. .alg_op = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC,
  3136. },
  3137. {
  3138. .name = "authenc(hmac(sha1),cbc(des3_ede))",
  3139. .driver_name = "authenc-hmac-sha1-cbc-des3_ede-caam",
  3140. .blocksize = DES3_EDE_BLOCK_SIZE,
  3141. .type = CRYPTO_ALG_TYPE_AEAD,
  3142. .template_aead = {
  3143. .setkey = aead_setkey,
  3144. .setauthsize = aead_setauthsize,
  3145. .encrypt = aead_encrypt,
  3146. .decrypt = aead_decrypt,
  3147. .givencrypt = aead_givencrypt,
  3148. .geniv = "<built-in>",
  3149. .ivsize = DES3_EDE_BLOCK_SIZE,
  3150. .maxauthsize = SHA1_DIGEST_SIZE,
  3151. },
  3152. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  3153. .class2_alg_type = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC_PRECOMP,
  3154. .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC,
  3155. },
  3156. {
  3157. .name = "authenc(hmac(sha224),cbc(des3_ede))",
  3158. .driver_name = "authenc-hmac-sha224-cbc-des3_ede-caam",
  3159. .blocksize = DES3_EDE_BLOCK_SIZE,
  3160. .type = CRYPTO_ALG_TYPE_AEAD,
  3161. .template_aead = {
  3162. .setkey = aead_setkey,
  3163. .setauthsize = aead_setauthsize,
  3164. .encrypt = aead_encrypt,
  3165. .decrypt = aead_decrypt,
  3166. .givencrypt = aead_givencrypt,
  3167. .geniv = "<built-in>",
  3168. .ivsize = DES3_EDE_BLOCK_SIZE,
  3169. .maxauthsize = SHA224_DIGEST_SIZE,
  3170. },
  3171. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  3172. .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
  3173. OP_ALG_AAI_HMAC_PRECOMP,
  3174. .alg_op = OP_ALG_ALGSEL_SHA224 | OP_ALG_AAI_HMAC,
  3175. },
  3176. {
  3177. .name = "authenc(hmac(sha256),cbc(des3_ede))",
  3178. .driver_name = "authenc-hmac-sha256-cbc-des3_ede-caam",
  3179. .blocksize = DES3_EDE_BLOCK_SIZE,
  3180. .type = CRYPTO_ALG_TYPE_AEAD,
  3181. .template_aead = {
  3182. .setkey = aead_setkey,
  3183. .setauthsize = aead_setauthsize,
  3184. .encrypt = aead_encrypt,
  3185. .decrypt = aead_decrypt,
  3186. .givencrypt = aead_givencrypt,
  3187. .geniv = "<built-in>",
  3188. .ivsize = DES3_EDE_BLOCK_SIZE,
  3189. .maxauthsize = SHA256_DIGEST_SIZE,
  3190. },
  3191. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  3192. .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
  3193. OP_ALG_AAI_HMAC_PRECOMP,
  3194. .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC,
  3195. },
  3196. {
  3197. .name = "authenc(hmac(sha384),cbc(des3_ede))",
  3198. .driver_name = "authenc-hmac-sha384-cbc-des3_ede-caam",
  3199. .blocksize = DES3_EDE_BLOCK_SIZE,
  3200. .type = CRYPTO_ALG_TYPE_AEAD,
  3201. .template_aead = {
  3202. .setkey = aead_setkey,
  3203. .setauthsize = aead_setauthsize,
  3204. .encrypt = aead_encrypt,
  3205. .decrypt = aead_decrypt,
  3206. .givencrypt = aead_givencrypt,
  3207. .geniv = "<built-in>",
  3208. .ivsize = DES3_EDE_BLOCK_SIZE,
  3209. .maxauthsize = SHA384_DIGEST_SIZE,
  3210. },
  3211. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  3212. .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
  3213. OP_ALG_AAI_HMAC_PRECOMP,
  3214. .alg_op = OP_ALG_ALGSEL_SHA384 | OP_ALG_AAI_HMAC,
  3215. },
  3216. {
  3217. .name = "authenc(hmac(sha512),cbc(des3_ede))",
  3218. .driver_name = "authenc-hmac-sha512-cbc-des3_ede-caam",
  3219. .blocksize = DES3_EDE_BLOCK_SIZE,
  3220. .type = CRYPTO_ALG_TYPE_AEAD,
  3221. .template_aead = {
  3222. .setkey = aead_setkey,
  3223. .setauthsize = aead_setauthsize,
  3224. .encrypt = aead_encrypt,
  3225. .decrypt = aead_decrypt,
  3226. .givencrypt = aead_givencrypt,
  3227. .geniv = "<built-in>",
  3228. .ivsize = DES3_EDE_BLOCK_SIZE,
  3229. .maxauthsize = SHA512_DIGEST_SIZE,
  3230. },
  3231. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  3232. .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
  3233. OP_ALG_AAI_HMAC_PRECOMP,
  3234. .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC,
  3235. },
  3236. {
  3237. .name = "authenc(hmac(md5),cbc(des))",
  3238. .driver_name = "authenc-hmac-md5-cbc-des-caam",
  3239. .blocksize = DES_BLOCK_SIZE,
  3240. .type = CRYPTO_ALG_TYPE_AEAD,
  3241. .template_aead = {
  3242. .setkey = aead_setkey,
  3243. .setauthsize = aead_setauthsize,
  3244. .encrypt = aead_encrypt,
  3245. .decrypt = aead_decrypt,
  3246. .givencrypt = aead_givencrypt,
  3247. .geniv = "<built-in>",
  3248. .ivsize = DES_BLOCK_SIZE,
  3249. .maxauthsize = MD5_DIGEST_SIZE,
  3250. },
  3251. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  3252. .class2_alg_type = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC_PRECOMP,
  3253. .alg_op = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC,
  3254. },
  3255. {
  3256. .name = "authenc(hmac(sha1),cbc(des))",
  3257. .driver_name = "authenc-hmac-sha1-cbc-des-caam",
  3258. .blocksize = DES_BLOCK_SIZE,
  3259. .type = CRYPTO_ALG_TYPE_AEAD,
  3260. .template_aead = {
  3261. .setkey = aead_setkey,
  3262. .setauthsize = aead_setauthsize,
  3263. .encrypt = aead_encrypt,
  3264. .decrypt = aead_decrypt,
  3265. .givencrypt = aead_givencrypt,
  3266. .geniv = "<built-in>",
  3267. .ivsize = DES_BLOCK_SIZE,
  3268. .maxauthsize = SHA1_DIGEST_SIZE,
  3269. },
  3270. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  3271. .class2_alg_type = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC_PRECOMP,
  3272. .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC,
  3273. },
  3274. {
  3275. .name = "authenc(hmac(sha224),cbc(des))",
  3276. .driver_name = "authenc-hmac-sha224-cbc-des-caam",
  3277. .blocksize = DES_BLOCK_SIZE,
  3278. .type = CRYPTO_ALG_TYPE_AEAD,
  3279. .template_aead = {
  3280. .setkey = aead_setkey,
  3281. .setauthsize = aead_setauthsize,
  3282. .encrypt = aead_encrypt,
  3283. .decrypt = aead_decrypt,
  3284. .givencrypt = aead_givencrypt,
  3285. .geniv = "<built-in>",
  3286. .ivsize = DES_BLOCK_SIZE,
  3287. .maxauthsize = SHA224_DIGEST_SIZE,
  3288. },
  3289. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  3290. .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
  3291. OP_ALG_AAI_HMAC_PRECOMP,
  3292. .alg_op = OP_ALG_ALGSEL_SHA224 | OP_ALG_AAI_HMAC,
  3293. },
  3294. {
  3295. .name = "authenc(hmac(sha256),cbc(des))",
  3296. .driver_name = "authenc-hmac-sha256-cbc-des-caam",
  3297. .blocksize = DES_BLOCK_SIZE,
  3298. .type = CRYPTO_ALG_TYPE_AEAD,
  3299. .template_aead = {
  3300. .setkey = aead_setkey,
  3301. .setauthsize = aead_setauthsize,
  3302. .encrypt = aead_encrypt,
  3303. .decrypt = aead_decrypt,
  3304. .givencrypt = aead_givencrypt,
  3305. .geniv = "<built-in>",
  3306. .ivsize = DES_BLOCK_SIZE,
  3307. .maxauthsize = SHA256_DIGEST_SIZE,
  3308. },
  3309. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  3310. .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
  3311. OP_ALG_AAI_HMAC_PRECOMP,
  3312. .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC,
  3313. },
  3314. {
  3315. .name = "authenc(hmac(sha384),cbc(des))",
  3316. .driver_name = "authenc-hmac-sha384-cbc-des-caam",
  3317. .blocksize = DES_BLOCK_SIZE,
  3318. .type = CRYPTO_ALG_TYPE_AEAD,
  3319. .template_aead = {
  3320. .setkey = aead_setkey,
  3321. .setauthsize = aead_setauthsize,
  3322. .encrypt = aead_encrypt,
  3323. .decrypt = aead_decrypt,
  3324. .givencrypt = aead_givencrypt,
  3325. .geniv = "<built-in>",
  3326. .ivsize = DES_BLOCK_SIZE,
  3327. .maxauthsize = SHA384_DIGEST_SIZE,
  3328. },
  3329. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  3330. .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
  3331. OP_ALG_AAI_HMAC_PRECOMP,
  3332. .alg_op = OP_ALG_ALGSEL_SHA384 | OP_ALG_AAI_HMAC,
  3333. },
  3334. {
  3335. .name = "authenc(hmac(sha512),cbc(des))",
  3336. .driver_name = "authenc-hmac-sha512-cbc-des-caam",
  3337. .blocksize = DES_BLOCK_SIZE,
  3338. .type = CRYPTO_ALG_TYPE_AEAD,
  3339. .template_aead = {
  3340. .setkey = aead_setkey,
  3341. .setauthsize = aead_setauthsize,
  3342. .encrypt = aead_encrypt,
  3343. .decrypt = aead_decrypt,
  3344. .givencrypt = aead_givencrypt,
  3345. .geniv = "<built-in>",
  3346. .ivsize = DES_BLOCK_SIZE,
  3347. .maxauthsize = SHA512_DIGEST_SIZE,
  3348. },
  3349. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  3350. .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
  3351. OP_ALG_AAI_HMAC_PRECOMP,
  3352. .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC,
  3353. },
  3354. {
  3355. .name = "authenc(hmac(md5),rfc3686(ctr(aes)))",
  3356. .driver_name = "authenc-hmac-md5-rfc3686-ctr-aes-caam",
  3357. .blocksize = 1,
  3358. .type = CRYPTO_ALG_TYPE_AEAD,
  3359. .template_aead = {
  3360. .setkey = aead_setkey,
  3361. .setauthsize = aead_setauthsize,
  3362. .encrypt = aead_encrypt,
  3363. .decrypt = aead_decrypt,
  3364. .givencrypt = aead_givencrypt,
  3365. .geniv = "<built-in>",
  3366. .ivsize = CTR_RFC3686_IV_SIZE,
  3367. .maxauthsize = MD5_DIGEST_SIZE,
  3368. },
  3369. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CTR_MOD128,
  3370. .class2_alg_type = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC_PRECOMP,
  3371. .alg_op = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC,
  3372. },
  3373. {
  3374. .name = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
  3375. .driver_name = "authenc-hmac-sha1-rfc3686-ctr-aes-caam",
  3376. .blocksize = 1,
  3377. .type = CRYPTO_ALG_TYPE_AEAD,
  3378. .template_aead = {
  3379. .setkey = aead_setkey,
  3380. .setauthsize = aead_setauthsize,
  3381. .encrypt = aead_encrypt,
  3382. .decrypt = aead_decrypt,
  3383. .givencrypt = aead_givencrypt,
  3384. .geniv = "<built-in>",
  3385. .ivsize = CTR_RFC3686_IV_SIZE,
  3386. .maxauthsize = SHA1_DIGEST_SIZE,
  3387. },
  3388. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CTR_MOD128,
  3389. .class2_alg_type = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC_PRECOMP,
  3390. .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC,
  3391. },
  3392. {
  3393. .name = "authenc(hmac(sha224),rfc3686(ctr(aes)))",
  3394. .driver_name = "authenc-hmac-sha224-rfc3686-ctr-aes-caam",
  3395. .blocksize = 1,
  3396. .type = CRYPTO_ALG_TYPE_AEAD,
  3397. .template_aead = {
  3398. .setkey = aead_setkey,
  3399. .setauthsize = aead_setauthsize,
  3400. .encrypt = aead_encrypt,
  3401. .decrypt = aead_decrypt,
  3402. .givencrypt = aead_givencrypt,
  3403. .geniv = "<built-in>",
  3404. .ivsize = CTR_RFC3686_IV_SIZE,
  3405. .maxauthsize = SHA224_DIGEST_SIZE,
  3406. },
  3407. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CTR_MOD128,
  3408. .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
  3409. OP_ALG_AAI_HMAC_PRECOMP,
  3410. .alg_op = OP_ALG_ALGSEL_SHA224 | OP_ALG_AAI_HMAC,
  3411. },
  3412. {
  3413. .name = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
  3414. .driver_name = "authenc-hmac-sha256-rfc3686-ctr-aes-caam",
  3415. .blocksize = 1,
  3416. .type = CRYPTO_ALG_TYPE_AEAD,
  3417. .template_aead = {
  3418. .setkey = aead_setkey,
  3419. .setauthsize = aead_setauthsize,
  3420. .encrypt = aead_encrypt,
  3421. .decrypt = aead_decrypt,
  3422. .givencrypt = aead_givencrypt,
  3423. .geniv = "<built-in>",
  3424. .ivsize = CTR_RFC3686_IV_SIZE,
  3425. .maxauthsize = SHA256_DIGEST_SIZE,
  3426. },
  3427. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CTR_MOD128,
  3428. .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
  3429. OP_ALG_AAI_HMAC_PRECOMP,
  3430. .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC,
  3431. },
  3432. {
  3433. .name = "authenc(hmac(sha384),rfc3686(ctr(aes)))",
  3434. .driver_name = "authenc-hmac-sha384-rfc3686-ctr-aes-caam",
  3435. .blocksize = 1,
  3436. .type = CRYPTO_ALG_TYPE_AEAD,
  3437. .template_aead = {
  3438. .setkey = aead_setkey,
  3439. .setauthsize = aead_setauthsize,
  3440. .encrypt = aead_encrypt,
  3441. .decrypt = aead_decrypt,
  3442. .givencrypt = aead_givencrypt,
  3443. .geniv = "<built-in>",
  3444. .ivsize = CTR_RFC3686_IV_SIZE,
  3445. .maxauthsize = SHA384_DIGEST_SIZE,
  3446. },
  3447. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CTR_MOD128,
  3448. .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
  3449. OP_ALG_AAI_HMAC_PRECOMP,
  3450. .alg_op = OP_ALG_ALGSEL_SHA384 | OP_ALG_AAI_HMAC,
  3451. },
  3452. {
  3453. .name = "authenc(hmac(sha512),rfc3686(ctr(aes)))",
  3454. .driver_name = "authenc-hmac-sha512-rfc3686-ctr-aes-caam",
  3455. .blocksize = 1,
  3456. .type = CRYPTO_ALG_TYPE_AEAD,
  3457. .template_aead = {
  3458. .setkey = aead_setkey,
  3459. .setauthsize = aead_setauthsize,
  3460. .encrypt = aead_encrypt,
  3461. .decrypt = aead_decrypt,
  3462. .givencrypt = aead_givencrypt,
  3463. .geniv = "<built-in>",
  3464. .ivsize = CTR_RFC3686_IV_SIZE,
  3465. .maxauthsize = SHA512_DIGEST_SIZE,
  3466. },
  3467. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CTR_MOD128,
  3468. .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
  3469. OP_ALG_AAI_HMAC_PRECOMP,
  3470. .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC,
  3471. },
  3472. {
  3473. .name = "rfc4106(gcm(aes))",
  3474. .driver_name = "rfc4106-gcm-aes-caam",
  3475. .blocksize = 1,
  3476. .type = CRYPTO_ALG_TYPE_AEAD,
  3477. .template_aead = {
  3478. .setkey = rfc4106_setkey,
  3479. .setauthsize = rfc4106_setauthsize,
  3480. .encrypt = aead_encrypt,
  3481. .decrypt = aead_decrypt,
  3482. .givencrypt = aead_givencrypt,
  3483. .geniv = "<built-in>",
  3484. .ivsize = 8,
  3485. .maxauthsize = AES_BLOCK_SIZE,
  3486. },
  3487. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_GCM,
  3488. },
  3489. {
  3490. .name = "rfc4543(gcm(aes))",
  3491. .driver_name = "rfc4543-gcm-aes-caam",
  3492. .blocksize = 1,
  3493. .type = CRYPTO_ALG_TYPE_AEAD,
  3494. .template_aead = {
  3495. .setkey = rfc4543_setkey,
  3496. .setauthsize = rfc4543_setauthsize,
  3497. .encrypt = aead_encrypt,
  3498. .decrypt = aead_decrypt,
  3499. .givencrypt = aead_givencrypt,
  3500. .geniv = "<built-in>",
  3501. .ivsize = 8,
  3502. .maxauthsize = AES_BLOCK_SIZE,
  3503. },
  3504. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_GCM,
  3505. },
  3506. /* Galois Counter Mode */
  3507. {
  3508. .name = "gcm(aes)",
  3509. .driver_name = "gcm-aes-caam",
  3510. .blocksize = 1,
  3511. .type = CRYPTO_ALG_TYPE_AEAD,
  3512. .template_aead = {
  3513. .setkey = gcm_setkey,
  3514. .setauthsize = gcm_setauthsize,
  3515. .encrypt = aead_encrypt,
  3516. .decrypt = aead_decrypt,
  3517. .givencrypt = NULL,
  3518. .geniv = "<built-in>",
  3519. .ivsize = 12,
  3520. .maxauthsize = AES_BLOCK_SIZE,
  3521. },
  3522. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_GCM,
  3523. },
  3524. /* ablkcipher descriptor */
  3525. {
  3526. .name = "cbc(aes)",
  3527. .driver_name = "cbc-aes-caam",
  3528. .blocksize = AES_BLOCK_SIZE,
  3529. .type = CRYPTO_ALG_TYPE_GIVCIPHER,
  3530. .template_ablkcipher = {
  3531. .setkey = ablkcipher_setkey,
  3532. .encrypt = ablkcipher_encrypt,
  3533. .decrypt = ablkcipher_decrypt,
  3534. .givencrypt = ablkcipher_givencrypt,
  3535. .geniv = "<built-in>",
  3536. .min_keysize = AES_MIN_KEY_SIZE,
  3537. .max_keysize = AES_MAX_KEY_SIZE,
  3538. .ivsize = AES_BLOCK_SIZE,
  3539. },
  3540. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  3541. },
  3542. {
  3543. .name = "cbc(des3_ede)",
  3544. .driver_name = "cbc-3des-caam",
  3545. .blocksize = DES3_EDE_BLOCK_SIZE,
  3546. .type = CRYPTO_ALG_TYPE_GIVCIPHER,
  3547. .template_ablkcipher = {
  3548. .setkey = ablkcipher_setkey,
  3549. .encrypt = ablkcipher_encrypt,
  3550. .decrypt = ablkcipher_decrypt,
  3551. .givencrypt = ablkcipher_givencrypt,
  3552. .geniv = "<built-in>",
  3553. .min_keysize = DES3_EDE_KEY_SIZE,
  3554. .max_keysize = DES3_EDE_KEY_SIZE,
  3555. .ivsize = DES3_EDE_BLOCK_SIZE,
  3556. },
  3557. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  3558. },
  3559. {
  3560. .name = "cbc(des)",
  3561. .driver_name = "cbc-des-caam",
  3562. .blocksize = DES_BLOCK_SIZE,
  3563. .type = CRYPTO_ALG_TYPE_GIVCIPHER,
  3564. .template_ablkcipher = {
  3565. .setkey = ablkcipher_setkey,
  3566. .encrypt = ablkcipher_encrypt,
  3567. .decrypt = ablkcipher_decrypt,
  3568. .givencrypt = ablkcipher_givencrypt,
  3569. .geniv = "<built-in>",
  3570. .min_keysize = DES_KEY_SIZE,
  3571. .max_keysize = DES_KEY_SIZE,
  3572. .ivsize = DES_BLOCK_SIZE,
  3573. },
  3574. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  3575. },
  3576. {
  3577. .name = "ctr(aes)",
  3578. .driver_name = "ctr-aes-caam",
  3579. .blocksize = 1,
  3580. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  3581. .template_ablkcipher = {
  3582. .setkey = ablkcipher_setkey,
  3583. .encrypt = ablkcipher_encrypt,
  3584. .decrypt = ablkcipher_decrypt,
  3585. .geniv = "chainiv",
  3586. .min_keysize = AES_MIN_KEY_SIZE,
  3587. .max_keysize = AES_MAX_KEY_SIZE,
  3588. .ivsize = AES_BLOCK_SIZE,
  3589. },
  3590. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CTR_MOD128,
  3591. },
  3592. {
  3593. .name = "rfc3686(ctr(aes))",
  3594. .driver_name = "rfc3686-ctr-aes-caam",
  3595. .blocksize = 1,
  3596. .type = CRYPTO_ALG_TYPE_GIVCIPHER,
  3597. .template_ablkcipher = {
  3598. .setkey = ablkcipher_setkey,
  3599. .encrypt = ablkcipher_encrypt,
  3600. .decrypt = ablkcipher_decrypt,
  3601. .givencrypt = ablkcipher_givencrypt,
  3602. .geniv = "<built-in>",
  3603. .min_keysize = AES_MIN_KEY_SIZE +
  3604. CTR_RFC3686_NONCE_SIZE,
  3605. .max_keysize = AES_MAX_KEY_SIZE +
  3606. CTR_RFC3686_NONCE_SIZE,
  3607. .ivsize = CTR_RFC3686_IV_SIZE,
  3608. },
  3609. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CTR_MOD128,
  3610. }
  3611. };
  3612. struct caam_crypto_alg {
  3613. struct list_head entry;
  3614. int class1_alg_type;
  3615. int class2_alg_type;
  3616. int alg_op;
  3617. struct crypto_alg crypto_alg;
  3618. };
  3619. static int caam_cra_init(struct crypto_tfm *tfm)
  3620. {
  3621. struct crypto_alg *alg = tfm->__crt_alg;
  3622. struct caam_crypto_alg *caam_alg =
  3623. container_of(alg, struct caam_crypto_alg, crypto_alg);
  3624. struct caam_ctx *ctx = crypto_tfm_ctx(tfm);
  3625. ctx->jrdev = caam_jr_alloc();
  3626. if (IS_ERR(ctx->jrdev)) {
  3627. pr_err("Job Ring Device allocation for transform failed\n");
  3628. return PTR_ERR(ctx->jrdev);
  3629. }
  3630. /* copy descriptor header template value */
  3631. ctx->class1_alg_type = OP_TYPE_CLASS1_ALG | caam_alg->class1_alg_type;
  3632. ctx->class2_alg_type = OP_TYPE_CLASS2_ALG | caam_alg->class2_alg_type;
  3633. ctx->alg_op = OP_TYPE_CLASS2_ALG | caam_alg->alg_op;
  3634. return 0;
  3635. }
  3636. static void caam_cra_exit(struct crypto_tfm *tfm)
  3637. {
  3638. struct caam_ctx *ctx = crypto_tfm_ctx(tfm);
  3639. if (ctx->sh_desc_enc_dma &&
  3640. !dma_mapping_error(ctx->jrdev, ctx->sh_desc_enc_dma))
  3641. dma_unmap_single(ctx->jrdev, ctx->sh_desc_enc_dma,
  3642. desc_bytes(ctx->sh_desc_enc), DMA_TO_DEVICE);
  3643. if (ctx->sh_desc_dec_dma &&
  3644. !dma_mapping_error(ctx->jrdev, ctx->sh_desc_dec_dma))
  3645. dma_unmap_single(ctx->jrdev, ctx->sh_desc_dec_dma,
  3646. desc_bytes(ctx->sh_desc_dec), DMA_TO_DEVICE);
  3647. if (ctx->sh_desc_givenc_dma &&
  3648. !dma_mapping_error(ctx->jrdev, ctx->sh_desc_givenc_dma))
  3649. dma_unmap_single(ctx->jrdev, ctx->sh_desc_givenc_dma,
  3650. desc_bytes(ctx->sh_desc_givenc),
  3651. DMA_TO_DEVICE);
  3652. if (ctx->key_dma &&
  3653. !dma_mapping_error(ctx->jrdev, ctx->key_dma))
  3654. dma_unmap_single(ctx->jrdev, ctx->key_dma,
  3655. ctx->enckeylen + ctx->split_key_pad_len,
  3656. DMA_TO_DEVICE);
  3657. caam_jr_free(ctx->jrdev);
  3658. }
  3659. static void __exit caam_algapi_exit(void)
  3660. {
  3661. struct caam_crypto_alg *t_alg, *n;
  3662. if (!alg_list.next)
  3663. return;
  3664. list_for_each_entry_safe(t_alg, n, &alg_list, entry) {
  3665. crypto_unregister_alg(&t_alg->crypto_alg);
  3666. list_del(&t_alg->entry);
  3667. kfree(t_alg);
  3668. }
  3669. }
  3670. static struct caam_crypto_alg *caam_alg_alloc(struct caam_alg_template
  3671. *template)
  3672. {
  3673. struct caam_crypto_alg *t_alg;
  3674. struct crypto_alg *alg;
  3675. t_alg = kzalloc(sizeof(struct caam_crypto_alg), GFP_KERNEL);
  3676. if (!t_alg) {
  3677. pr_err("failed to allocate t_alg\n");
  3678. return ERR_PTR(-ENOMEM);
  3679. }
  3680. alg = &t_alg->crypto_alg;
  3681. snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", template->name);
  3682. snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  3683. template->driver_name);
  3684. alg->cra_module = THIS_MODULE;
  3685. alg->cra_init = caam_cra_init;
  3686. alg->cra_exit = caam_cra_exit;
  3687. alg->cra_priority = CAAM_CRA_PRIORITY;
  3688. alg->cra_blocksize = template->blocksize;
  3689. alg->cra_alignmask = 0;
  3690. alg->cra_ctxsize = sizeof(struct caam_ctx);
  3691. alg->cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY |
  3692. template->type;
  3693. switch (template->type) {
  3694. case CRYPTO_ALG_TYPE_GIVCIPHER:
  3695. alg->cra_type = &crypto_givcipher_type;
  3696. alg->cra_ablkcipher = template->template_ablkcipher;
  3697. break;
  3698. case CRYPTO_ALG_TYPE_ABLKCIPHER:
  3699. alg->cra_type = &crypto_ablkcipher_type;
  3700. alg->cra_ablkcipher = template->template_ablkcipher;
  3701. break;
  3702. case CRYPTO_ALG_TYPE_AEAD:
  3703. alg->cra_type = &crypto_aead_type;
  3704. alg->cra_aead = template->template_aead;
  3705. break;
  3706. }
  3707. t_alg->class1_alg_type = template->class1_alg_type;
  3708. t_alg->class2_alg_type = template->class2_alg_type;
  3709. t_alg->alg_op = template->alg_op;
  3710. return t_alg;
  3711. }
  3712. static int __init caam_algapi_init(void)
  3713. {
  3714. struct device_node *dev_node;
  3715. struct platform_device *pdev;
  3716. struct device *ctrldev;
  3717. void *priv;
  3718. int i = 0, err = 0;
  3719. dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec-v4.0");
  3720. if (!dev_node) {
  3721. dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec4.0");
  3722. if (!dev_node)
  3723. return -ENODEV;
  3724. }
  3725. pdev = of_find_device_by_node(dev_node);
  3726. if (!pdev) {
  3727. of_node_put(dev_node);
  3728. return -ENODEV;
  3729. }
  3730. ctrldev = &pdev->dev;
  3731. priv = dev_get_drvdata(ctrldev);
  3732. of_node_put(dev_node);
  3733. /*
  3734. * If priv is NULL, it's probably because the caam driver wasn't
  3735. * properly initialized (e.g. RNG4 init failed). Thus, bail out here.
  3736. */
  3737. if (!priv)
  3738. return -ENODEV;
  3739. INIT_LIST_HEAD(&alg_list);
  3740. /* register crypto algorithms the device supports */
  3741. for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
  3742. /* TODO: check if h/w supports alg */
  3743. struct caam_crypto_alg *t_alg;
  3744. t_alg = caam_alg_alloc(&driver_algs[i]);
  3745. if (IS_ERR(t_alg)) {
  3746. err = PTR_ERR(t_alg);
  3747. pr_warn("%s alg allocation failed\n",
  3748. driver_algs[i].driver_name);
  3749. continue;
  3750. }
  3751. err = crypto_register_alg(&t_alg->crypto_alg);
  3752. if (err) {
  3753. pr_warn("%s alg registration failed\n",
  3754. t_alg->crypto_alg.cra_driver_name);
  3755. kfree(t_alg);
  3756. } else
  3757. list_add_tail(&t_alg->entry, &alg_list);
  3758. }
  3759. if (!list_empty(&alg_list))
  3760. pr_info("caam algorithms registered in /proc/crypto\n");
  3761. return err;
  3762. }
  3763. module_init(caam_algapi_init);
  3764. module_exit(caam_algapi_exit);
  3765. MODULE_LICENSE("GPL");
  3766. MODULE_DESCRIPTION("FSL CAAM support for crypto API");
  3767. MODULE_AUTHOR("Freescale Semiconductor - NMG/STC");