chcr_algo.c 107 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724
  1. /*
  2. * This file is part of the Chelsio T6 Crypto driver for Linux.
  3. *
  4. * Copyright (c) 2003-2016 Chelsio Communications, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. *
  34. * Written and Maintained by:
  35. * Manoj Malviya (manojmalviya@chelsio.com)
  36. * Atul Gupta (atul.gupta@chelsio.com)
  37. * Jitendra Lulla (jlulla@chelsio.com)
  38. * Yeshaswi M R Gowda (yeshaswi@chelsio.com)
  39. * Harsh Jain (harsh@chelsio.com)
  40. */
  41. #define pr_fmt(fmt) "chcr:" fmt
  42. #include <linux/kernel.h>
  43. #include <linux/module.h>
  44. #include <linux/crypto.h>
  45. #include <linux/cryptohash.h>
  46. #include <linux/skbuff.h>
  47. #include <linux/rtnetlink.h>
  48. #include <linux/highmem.h>
  49. #include <linux/scatterlist.h>
  50. #include <crypto/aes.h>
  51. #include <crypto/algapi.h>
  52. #include <crypto/hash.h>
  53. #include <crypto/sha.h>
  54. #include <crypto/authenc.h>
  55. #include <crypto/ctr.h>
  56. #include <crypto/gf128mul.h>
  57. #include <crypto/internal/aead.h>
  58. #include <crypto/null.h>
  59. #include <crypto/internal/skcipher.h>
  60. #include <crypto/aead.h>
  61. #include <crypto/scatterwalk.h>
  62. #include <crypto/internal/hash.h>
  63. #include "t4fw_api.h"
  64. #include "t4_msg.h"
  65. #include "chcr_core.h"
  66. #include "chcr_algo.h"
  67. #include "chcr_crypto.h"
  68. static inline struct chcr_aead_ctx *AEAD_CTX(struct chcr_context *ctx)
  69. {
  70. return ctx->crypto_ctx->aeadctx;
  71. }
  72. static inline struct ablk_ctx *ABLK_CTX(struct chcr_context *ctx)
  73. {
  74. return ctx->crypto_ctx->ablkctx;
  75. }
  76. static inline struct hmac_ctx *HMAC_CTX(struct chcr_context *ctx)
  77. {
  78. return ctx->crypto_ctx->hmacctx;
  79. }
  80. static inline struct chcr_gcm_ctx *GCM_CTX(struct chcr_aead_ctx *gctx)
  81. {
  82. return gctx->ctx->gcm;
  83. }
  84. static inline struct chcr_authenc_ctx *AUTHENC_CTX(struct chcr_aead_ctx *gctx)
  85. {
  86. return gctx->ctx->authenc;
  87. }
  88. static inline struct uld_ctx *ULD_CTX(struct chcr_context *ctx)
  89. {
  90. return ctx->dev->u_ctx;
  91. }
  92. static inline int is_ofld_imm(const struct sk_buff *skb)
  93. {
  94. return (skb->len <= CRYPTO_MAX_IMM_TX_PKT_LEN);
  95. }
  96. /*
  97. * sgl_len - calculates the size of an SGL of the given capacity
  98. * @n: the number of SGL entries
  99. * Calculates the number of flits needed for a scatter/gather list that
  100. * can hold the given number of entries.
  101. */
  102. static inline unsigned int sgl_len(unsigned int n)
  103. {
  104. n--;
  105. return (3 * n) / 2 + (n & 1) + 2;
  106. }
  107. static void chcr_verify_tag(struct aead_request *req, u8 *input, int *err)
  108. {
  109. u8 temp[SHA512_DIGEST_SIZE];
  110. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  111. int authsize = crypto_aead_authsize(tfm);
  112. struct cpl_fw6_pld *fw6_pld;
  113. int cmp = 0;
  114. fw6_pld = (struct cpl_fw6_pld *)input;
  115. if ((get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106) ||
  116. (get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_GCM)) {
  117. cmp = crypto_memneq(&fw6_pld->data[2], (fw6_pld + 1), authsize);
  118. } else {
  119. sg_pcopy_to_buffer(req->src, sg_nents(req->src), temp,
  120. authsize, req->assoclen +
  121. req->cryptlen - authsize);
  122. cmp = crypto_memneq(temp, (fw6_pld + 1), authsize);
  123. }
  124. if (cmp)
  125. *err = -EBADMSG;
  126. else
  127. *err = 0;
  128. }
  129. /*
  130. * chcr_handle_resp - Unmap the DMA buffers associated with the request
  131. * @req: crypto request
  132. */
  133. int chcr_handle_resp(struct crypto_async_request *req, unsigned char *input,
  134. int err)
  135. {
  136. struct crypto_tfm *tfm = req->tfm;
  137. struct chcr_context *ctx = crypto_tfm_ctx(tfm);
  138. struct uld_ctx *u_ctx = ULD_CTX(ctx);
  139. struct chcr_req_ctx ctx_req;
  140. unsigned int digestsize, updated_digestsize;
  141. struct adapter *adap = padap(ctx->dev);
  142. switch (tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
  143. case CRYPTO_ALG_TYPE_AEAD:
  144. ctx_req.req.aead_req = aead_request_cast(req);
  145. ctx_req.ctx.reqctx = aead_request_ctx(ctx_req.req.aead_req);
  146. dma_unmap_sg(&u_ctx->lldi.pdev->dev, ctx_req.ctx.reqctx->dst,
  147. ctx_req.ctx.reqctx->dst_nents, DMA_FROM_DEVICE);
  148. if (ctx_req.ctx.reqctx->skb) {
  149. kfree_skb(ctx_req.ctx.reqctx->skb);
  150. ctx_req.ctx.reqctx->skb = NULL;
  151. }
  152. free_new_sg(ctx_req.ctx.reqctx->newdstsg);
  153. ctx_req.ctx.reqctx->newdstsg = NULL;
  154. if (ctx_req.ctx.reqctx->verify == VERIFY_SW) {
  155. chcr_verify_tag(ctx_req.req.aead_req, input,
  156. &err);
  157. ctx_req.ctx.reqctx->verify = VERIFY_HW;
  158. }
  159. ctx_req.req.aead_req->base.complete(req, err);
  160. break;
  161. case CRYPTO_ALG_TYPE_ABLKCIPHER:
  162. err = chcr_handle_cipher_resp(ablkcipher_request_cast(req),
  163. input, err);
  164. break;
  165. case CRYPTO_ALG_TYPE_AHASH:
  166. ctx_req.req.ahash_req = ahash_request_cast(req);
  167. ctx_req.ctx.ahash_ctx =
  168. ahash_request_ctx(ctx_req.req.ahash_req);
  169. digestsize =
  170. crypto_ahash_digestsize(crypto_ahash_reqtfm(
  171. ctx_req.req.ahash_req));
  172. updated_digestsize = digestsize;
  173. if (digestsize == SHA224_DIGEST_SIZE)
  174. updated_digestsize = SHA256_DIGEST_SIZE;
  175. else if (digestsize == SHA384_DIGEST_SIZE)
  176. updated_digestsize = SHA512_DIGEST_SIZE;
  177. if (ctx_req.ctx.ahash_ctx->skb) {
  178. kfree_skb(ctx_req.ctx.ahash_ctx->skb);
  179. ctx_req.ctx.ahash_ctx->skb = NULL;
  180. }
  181. if (ctx_req.ctx.ahash_ctx->result == 1) {
  182. ctx_req.ctx.ahash_ctx->result = 0;
  183. memcpy(ctx_req.req.ahash_req->result, input +
  184. sizeof(struct cpl_fw6_pld),
  185. digestsize);
  186. } else {
  187. memcpy(ctx_req.ctx.ahash_ctx->partial_hash, input +
  188. sizeof(struct cpl_fw6_pld),
  189. updated_digestsize);
  190. }
  191. ctx_req.req.ahash_req->base.complete(req, err);
  192. break;
  193. }
  194. atomic_inc(&adap->chcr_stats.complete);
  195. return err;
  196. }
  197. /*
  198. * calc_tx_flits_ofld - calculate # of flits for an offload packet
  199. * @skb: the packet
  200. * Returns the number of flits needed for the given offload packet.
  201. * These packets are already fully constructed and no additional headers
  202. * will be added.
  203. */
  204. static inline unsigned int calc_tx_flits_ofld(const struct sk_buff *skb)
  205. {
  206. unsigned int flits, cnt;
  207. if (is_ofld_imm(skb))
  208. return DIV_ROUND_UP(skb->len, 8);
  209. flits = skb_transport_offset(skb) / 8; /* headers */
  210. cnt = skb_shinfo(skb)->nr_frags;
  211. if (skb_tail_pointer(skb) != skb_transport_header(skb))
  212. cnt++;
  213. return flits + sgl_len(cnt);
  214. }
  215. static inline void get_aes_decrypt_key(unsigned char *dec_key,
  216. const unsigned char *key,
  217. unsigned int keylength)
  218. {
  219. u32 temp;
  220. u32 w_ring[MAX_NK];
  221. int i, j, k;
  222. u8 nr, nk;
  223. switch (keylength) {
  224. case AES_KEYLENGTH_128BIT:
  225. nk = KEYLENGTH_4BYTES;
  226. nr = NUMBER_OF_ROUNDS_10;
  227. break;
  228. case AES_KEYLENGTH_192BIT:
  229. nk = KEYLENGTH_6BYTES;
  230. nr = NUMBER_OF_ROUNDS_12;
  231. break;
  232. case AES_KEYLENGTH_256BIT:
  233. nk = KEYLENGTH_8BYTES;
  234. nr = NUMBER_OF_ROUNDS_14;
  235. break;
  236. default:
  237. return;
  238. }
  239. for (i = 0; i < nk; i++)
  240. w_ring[i] = be32_to_cpu(*(u32 *)&key[4 * i]);
  241. i = 0;
  242. temp = w_ring[nk - 1];
  243. while (i + nk < (nr + 1) * 4) {
  244. if (!(i % nk)) {
  245. /* RotWord(temp) */
  246. temp = (temp << 8) | (temp >> 24);
  247. temp = aes_ks_subword(temp);
  248. temp ^= round_constant[i / nk];
  249. } else if (nk == 8 && (i % 4 == 0)) {
  250. temp = aes_ks_subword(temp);
  251. }
  252. w_ring[i % nk] ^= temp;
  253. temp = w_ring[i % nk];
  254. i++;
  255. }
  256. i--;
  257. for (k = 0, j = i % nk; k < nk; k++) {
  258. *((u32 *)dec_key + k) = htonl(w_ring[j]);
  259. j--;
  260. if (j < 0)
  261. j += nk;
  262. }
  263. }
  264. static struct crypto_shash *chcr_alloc_shash(unsigned int ds)
  265. {
  266. struct crypto_shash *base_hash = ERR_PTR(-EINVAL);
  267. switch (ds) {
  268. case SHA1_DIGEST_SIZE:
  269. base_hash = crypto_alloc_shash("sha1", 0, 0);
  270. break;
  271. case SHA224_DIGEST_SIZE:
  272. base_hash = crypto_alloc_shash("sha224", 0, 0);
  273. break;
  274. case SHA256_DIGEST_SIZE:
  275. base_hash = crypto_alloc_shash("sha256", 0, 0);
  276. break;
  277. case SHA384_DIGEST_SIZE:
  278. base_hash = crypto_alloc_shash("sha384", 0, 0);
  279. break;
  280. case SHA512_DIGEST_SIZE:
  281. base_hash = crypto_alloc_shash("sha512", 0, 0);
  282. break;
  283. }
  284. return base_hash;
  285. }
  286. static int chcr_compute_partial_hash(struct shash_desc *desc,
  287. char *iopad, char *result_hash,
  288. int digest_size)
  289. {
  290. struct sha1_state sha1_st;
  291. struct sha256_state sha256_st;
  292. struct sha512_state sha512_st;
  293. int error;
  294. if (digest_size == SHA1_DIGEST_SIZE) {
  295. error = crypto_shash_init(desc) ?:
  296. crypto_shash_update(desc, iopad, SHA1_BLOCK_SIZE) ?:
  297. crypto_shash_export(desc, (void *)&sha1_st);
  298. memcpy(result_hash, sha1_st.state, SHA1_DIGEST_SIZE);
  299. } else if (digest_size == SHA224_DIGEST_SIZE) {
  300. error = crypto_shash_init(desc) ?:
  301. crypto_shash_update(desc, iopad, SHA256_BLOCK_SIZE) ?:
  302. crypto_shash_export(desc, (void *)&sha256_st);
  303. memcpy(result_hash, sha256_st.state, SHA256_DIGEST_SIZE);
  304. } else if (digest_size == SHA256_DIGEST_SIZE) {
  305. error = crypto_shash_init(desc) ?:
  306. crypto_shash_update(desc, iopad, SHA256_BLOCK_SIZE) ?:
  307. crypto_shash_export(desc, (void *)&sha256_st);
  308. memcpy(result_hash, sha256_st.state, SHA256_DIGEST_SIZE);
  309. } else if (digest_size == SHA384_DIGEST_SIZE) {
  310. error = crypto_shash_init(desc) ?:
  311. crypto_shash_update(desc, iopad, SHA512_BLOCK_SIZE) ?:
  312. crypto_shash_export(desc, (void *)&sha512_st);
  313. memcpy(result_hash, sha512_st.state, SHA512_DIGEST_SIZE);
  314. } else if (digest_size == SHA512_DIGEST_SIZE) {
  315. error = crypto_shash_init(desc) ?:
  316. crypto_shash_update(desc, iopad, SHA512_BLOCK_SIZE) ?:
  317. crypto_shash_export(desc, (void *)&sha512_st);
  318. memcpy(result_hash, sha512_st.state, SHA512_DIGEST_SIZE);
  319. } else {
  320. error = -EINVAL;
  321. pr_err("Unknown digest size %d\n", digest_size);
  322. }
  323. return error;
  324. }
  325. static void chcr_change_order(char *buf, int ds)
  326. {
  327. int i;
  328. if (ds == SHA512_DIGEST_SIZE) {
  329. for (i = 0; i < (ds / sizeof(u64)); i++)
  330. *((__be64 *)buf + i) =
  331. cpu_to_be64(*((u64 *)buf + i));
  332. } else {
  333. for (i = 0; i < (ds / sizeof(u32)); i++)
  334. *((__be32 *)buf + i) =
  335. cpu_to_be32(*((u32 *)buf + i));
  336. }
  337. }
  338. static inline int is_hmac(struct crypto_tfm *tfm)
  339. {
  340. struct crypto_alg *alg = tfm->__crt_alg;
  341. struct chcr_alg_template *chcr_crypto_alg =
  342. container_of(__crypto_ahash_alg(alg), struct chcr_alg_template,
  343. alg.hash);
  344. if (chcr_crypto_alg->type == CRYPTO_ALG_TYPE_HMAC)
  345. return 1;
  346. return 0;
  347. }
  348. static void write_phys_cpl(struct cpl_rx_phys_dsgl *phys_cpl,
  349. struct scatterlist *sg,
  350. struct phys_sge_parm *sg_param)
  351. {
  352. struct phys_sge_pairs *to;
  353. unsigned int len = 0, left_size = sg_param->obsize;
  354. unsigned int nents = sg_param->nents, i, j = 0;
  355. phys_cpl->op_to_tid = htonl(CPL_RX_PHYS_DSGL_OPCODE_V(CPL_RX_PHYS_DSGL)
  356. | CPL_RX_PHYS_DSGL_ISRDMA_V(0));
  357. phys_cpl->pcirlxorder_to_noofsgentr =
  358. htonl(CPL_RX_PHYS_DSGL_PCIRLXORDER_V(0) |
  359. CPL_RX_PHYS_DSGL_PCINOSNOOP_V(0) |
  360. CPL_RX_PHYS_DSGL_PCITPHNTENB_V(0) |
  361. CPL_RX_PHYS_DSGL_PCITPHNT_V(0) |
  362. CPL_RX_PHYS_DSGL_DCAID_V(0) |
  363. CPL_RX_PHYS_DSGL_NOOFSGENTR_V(nents));
  364. phys_cpl->rss_hdr_int.opcode = CPL_RX_PHYS_ADDR;
  365. phys_cpl->rss_hdr_int.qid = htons(sg_param->qid);
  366. phys_cpl->rss_hdr_int.hash_val = 0;
  367. to = (struct phys_sge_pairs *)((unsigned char *)phys_cpl +
  368. sizeof(struct cpl_rx_phys_dsgl));
  369. for (i = 0; nents && left_size; to++) {
  370. for (j = 0; j < 8 && nents && left_size; j++, nents--) {
  371. len = min(left_size, sg_dma_len(sg));
  372. to->len[j] = htons(len);
  373. to->addr[j] = cpu_to_be64(sg_dma_address(sg));
  374. left_size -= len;
  375. sg = sg_next(sg);
  376. }
  377. }
  378. }
  379. static inline int map_writesg_phys_cpl(struct device *dev,
  380. struct cpl_rx_phys_dsgl *phys_cpl,
  381. struct scatterlist *sg,
  382. struct phys_sge_parm *sg_param)
  383. {
  384. if (!sg || !sg_param->nents)
  385. return -EINVAL;
  386. sg_param->nents = dma_map_sg(dev, sg, sg_param->nents, DMA_FROM_DEVICE);
  387. if (sg_param->nents == 0) {
  388. pr_err("CHCR : DMA mapping failed\n");
  389. return -EINVAL;
  390. }
  391. write_phys_cpl(phys_cpl, sg, sg_param);
  392. return 0;
  393. }
  394. static inline int get_aead_subtype(struct crypto_aead *aead)
  395. {
  396. struct aead_alg *alg = crypto_aead_alg(aead);
  397. struct chcr_alg_template *chcr_crypto_alg =
  398. container_of(alg, struct chcr_alg_template, alg.aead);
  399. return chcr_crypto_alg->type & CRYPTO_ALG_SUB_TYPE_MASK;
  400. }
  401. static inline int get_cryptoalg_subtype(struct crypto_tfm *tfm)
  402. {
  403. struct crypto_alg *alg = tfm->__crt_alg;
  404. struct chcr_alg_template *chcr_crypto_alg =
  405. container_of(alg, struct chcr_alg_template, alg.crypto);
  406. return chcr_crypto_alg->type & CRYPTO_ALG_SUB_TYPE_MASK;
  407. }
  408. static inline void write_buffer_to_skb(struct sk_buff *skb,
  409. unsigned int *frags,
  410. char *bfr,
  411. u8 bfr_len)
  412. {
  413. skb->len += bfr_len;
  414. skb->data_len += bfr_len;
  415. skb->truesize += bfr_len;
  416. get_page(virt_to_page(bfr));
  417. skb_fill_page_desc(skb, *frags, virt_to_page(bfr),
  418. offset_in_page(bfr), bfr_len);
  419. (*frags)++;
  420. }
  421. static inline void
  422. write_sg_to_skb(struct sk_buff *skb, unsigned int *frags,
  423. struct scatterlist *sg, unsigned int count)
  424. {
  425. struct page *spage;
  426. unsigned int page_len;
  427. skb->len += count;
  428. skb->data_len += count;
  429. skb->truesize += count;
  430. while (count > 0) {
  431. if (!sg || (!(sg->length)))
  432. break;
  433. spage = sg_page(sg);
  434. get_page(spage);
  435. page_len = min(sg->length, count);
  436. skb_fill_page_desc(skb, *frags, spage, sg->offset, page_len);
  437. (*frags)++;
  438. count -= page_len;
  439. sg = sg_next(sg);
  440. }
  441. }
  442. static int cxgb4_is_crypto_q_full(struct net_device *dev, unsigned int idx)
  443. {
  444. struct adapter *adap = netdev2adap(dev);
  445. struct sge_uld_txq_info *txq_info =
  446. adap->sge.uld_txq_info[CXGB4_TX_CRYPTO];
  447. struct sge_uld_txq *txq;
  448. int ret = 0;
  449. local_bh_disable();
  450. txq = &txq_info->uldtxq[idx];
  451. spin_lock(&txq->sendq.lock);
  452. if (txq->full)
  453. ret = -1;
  454. spin_unlock(&txq->sendq.lock);
  455. local_bh_enable();
  456. return ret;
  457. }
  458. static int generate_copy_rrkey(struct ablk_ctx *ablkctx,
  459. struct _key_ctx *key_ctx)
  460. {
  461. if (ablkctx->ciph_mode == CHCR_SCMD_CIPHER_MODE_AES_CBC) {
  462. memcpy(key_ctx->key, ablkctx->rrkey, ablkctx->enckey_len);
  463. } else {
  464. memcpy(key_ctx->key,
  465. ablkctx->key + (ablkctx->enckey_len >> 1),
  466. ablkctx->enckey_len >> 1);
  467. memcpy(key_ctx->key + (ablkctx->enckey_len >> 1),
  468. ablkctx->rrkey, ablkctx->enckey_len >> 1);
  469. }
  470. return 0;
  471. }
  472. static int chcr_sg_ent_in_wr(struct scatterlist *src,
  473. struct scatterlist *dst,
  474. unsigned int minsg,
  475. unsigned int space,
  476. short int *sent,
  477. short int *dent)
  478. {
  479. int srclen = 0, dstlen = 0;
  480. int srcsg = minsg, dstsg = 0;
  481. *sent = 0;
  482. *dent = 0;
  483. while (src && dst && ((srcsg + 1) <= MAX_SKB_FRAGS) &&
  484. space > (sgl_ent_len[srcsg + 1] + dsgl_ent_len[dstsg])) {
  485. srclen += src->length;
  486. srcsg++;
  487. while (dst && ((dstsg + 1) <= MAX_DSGL_ENT) &&
  488. space > (sgl_ent_len[srcsg] + dsgl_ent_len[dstsg + 1])) {
  489. if (srclen <= dstlen)
  490. break;
  491. dstlen += dst->length;
  492. dst = sg_next(dst);
  493. dstsg++;
  494. }
  495. src = sg_next(src);
  496. }
  497. *sent = srcsg - minsg;
  498. *dent = dstsg;
  499. return min(srclen, dstlen);
  500. }
  501. static int chcr_cipher_fallback(struct crypto_skcipher *cipher,
  502. u32 flags,
  503. struct scatterlist *src,
  504. struct scatterlist *dst,
  505. unsigned int nbytes,
  506. u8 *iv,
  507. unsigned short op_type)
  508. {
  509. int err;
  510. SKCIPHER_REQUEST_ON_STACK(subreq, cipher);
  511. skcipher_request_set_tfm(subreq, cipher);
  512. skcipher_request_set_callback(subreq, flags, NULL, NULL);
  513. skcipher_request_set_crypt(subreq, src, dst,
  514. nbytes, iv);
  515. err = op_type ? crypto_skcipher_decrypt(subreq) :
  516. crypto_skcipher_encrypt(subreq);
  517. skcipher_request_zero(subreq);
  518. return err;
  519. }
  520. static inline void create_wreq(struct chcr_context *ctx,
  521. struct chcr_wr *chcr_req,
  522. void *req, struct sk_buff *skb,
  523. int kctx_len, int hash_sz,
  524. int is_iv,
  525. unsigned int sc_len,
  526. unsigned int lcb)
  527. {
  528. struct uld_ctx *u_ctx = ULD_CTX(ctx);
  529. int iv_loc = IV_DSGL;
  530. int qid = u_ctx->lldi.rxq_ids[ctx->rx_qidx];
  531. unsigned int immdatalen = 0, nr_frags = 0;
  532. if (is_ofld_imm(skb)) {
  533. immdatalen = skb->data_len;
  534. iv_loc = IV_IMMEDIATE;
  535. } else {
  536. nr_frags = skb_shinfo(skb)->nr_frags;
  537. }
  538. chcr_req->wreq.op_to_cctx_size = FILL_WR_OP_CCTX_SIZE(immdatalen,
  539. ((sizeof(chcr_req->key_ctx) + kctx_len) >> 4));
  540. chcr_req->wreq.pld_size_hash_size =
  541. htonl(FW_CRYPTO_LOOKASIDE_WR_PLD_SIZE_V(sgl_lengths[nr_frags]) |
  542. FW_CRYPTO_LOOKASIDE_WR_HASH_SIZE_V(hash_sz));
  543. chcr_req->wreq.len16_pkd =
  544. htonl(FW_CRYPTO_LOOKASIDE_WR_LEN16_V(DIV_ROUND_UP(
  545. (calc_tx_flits_ofld(skb) * 8), 16)));
  546. chcr_req->wreq.cookie = cpu_to_be64((uintptr_t)req);
  547. chcr_req->wreq.rx_chid_to_rx_q_id =
  548. FILL_WR_RX_Q_ID(ctx->dev->rx_channel_id, qid,
  549. is_iv ? iv_loc : IV_NOP, !!lcb,
  550. ctx->tx_qidx);
  551. chcr_req->ulptx.cmd_dest = FILL_ULPTX_CMD_DEST(ctx->dev->tx_channel_id,
  552. qid);
  553. chcr_req->ulptx.len = htonl((DIV_ROUND_UP((calc_tx_flits_ofld(skb) * 8),
  554. 16) - ((sizeof(chcr_req->wreq)) >> 4)));
  555. chcr_req->sc_imm.cmd_more = FILL_CMD_MORE(immdatalen);
  556. chcr_req->sc_imm.len = cpu_to_be32(sizeof(struct cpl_tx_sec_pdu) +
  557. sizeof(chcr_req->key_ctx) +
  558. kctx_len + sc_len + immdatalen);
  559. }
  560. /**
  561. * create_cipher_wr - form the WR for cipher operations
  562. * @req: cipher req.
  563. * @ctx: crypto driver context of the request.
  564. * @qid: ingress qid where response of this WR should be received.
  565. * @op_type: encryption or decryption
  566. */
  567. static struct sk_buff *create_cipher_wr(struct cipher_wr_param *wrparam)
  568. {
  569. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(wrparam->req);
  570. struct chcr_context *ctx = crypto_ablkcipher_ctx(tfm);
  571. struct uld_ctx *u_ctx = ULD_CTX(ctx);
  572. struct ablk_ctx *ablkctx = ABLK_CTX(ctx);
  573. struct sk_buff *skb = NULL;
  574. struct chcr_wr *chcr_req;
  575. struct cpl_rx_phys_dsgl *phys_cpl;
  576. struct chcr_blkcipher_req_ctx *reqctx =
  577. ablkcipher_request_ctx(wrparam->req);
  578. struct phys_sge_parm sg_param;
  579. unsigned int frags = 0, transhdr_len, phys_dsgl;
  580. int error;
  581. unsigned int ivsize = AES_BLOCK_SIZE, kctx_len;
  582. gfp_t flags = wrparam->req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
  583. GFP_KERNEL : GFP_ATOMIC;
  584. struct adapter *adap = padap(ctx->dev);
  585. phys_dsgl = get_space_for_phys_dsgl(reqctx->dst_nents);
  586. kctx_len = (DIV_ROUND_UP(ablkctx->enckey_len, 16) * 16);
  587. transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, phys_dsgl);
  588. skb = alloc_skb((transhdr_len + sizeof(struct sge_opaque_hdr)), flags);
  589. if (!skb) {
  590. error = -ENOMEM;
  591. goto err;
  592. }
  593. skb_reserve(skb, sizeof(struct sge_opaque_hdr));
  594. chcr_req = __skb_put_zero(skb, transhdr_len);
  595. chcr_req->sec_cpl.op_ivinsrtofst =
  596. FILL_SEC_CPL_OP_IVINSR(ctx->dev->rx_channel_id, 2, 1);
  597. chcr_req->sec_cpl.pldlen = htonl(ivsize + wrparam->bytes);
  598. chcr_req->sec_cpl.aadstart_cipherstop_hi =
  599. FILL_SEC_CPL_CIPHERSTOP_HI(0, 0, ivsize + 1, 0);
  600. chcr_req->sec_cpl.cipherstop_lo_authinsert =
  601. FILL_SEC_CPL_AUTHINSERT(0, 0, 0, 0);
  602. chcr_req->sec_cpl.seqno_numivs = FILL_SEC_CPL_SCMD0_SEQNO(reqctx->op, 0,
  603. ablkctx->ciph_mode,
  604. 0, 0, ivsize >> 1);
  605. chcr_req->sec_cpl.ivgen_hdrlen = FILL_SEC_CPL_IVGEN_HDRLEN(0, 0, 0,
  606. 0, 1, phys_dsgl);
  607. chcr_req->key_ctx.ctx_hdr = ablkctx->key_ctx_hdr;
  608. if ((reqctx->op == CHCR_DECRYPT_OP) &&
  609. (!(get_cryptoalg_subtype(crypto_ablkcipher_tfm(tfm)) ==
  610. CRYPTO_ALG_SUB_TYPE_CTR)) &&
  611. (!(get_cryptoalg_subtype(crypto_ablkcipher_tfm(tfm)) ==
  612. CRYPTO_ALG_SUB_TYPE_CTR_RFC3686))) {
  613. generate_copy_rrkey(ablkctx, &chcr_req->key_ctx);
  614. } else {
  615. if ((ablkctx->ciph_mode == CHCR_SCMD_CIPHER_MODE_AES_CBC) ||
  616. (ablkctx->ciph_mode == CHCR_SCMD_CIPHER_MODE_AES_CTR)) {
  617. memcpy(chcr_req->key_ctx.key, ablkctx->key,
  618. ablkctx->enckey_len);
  619. } else {
  620. memcpy(chcr_req->key_ctx.key, ablkctx->key +
  621. (ablkctx->enckey_len >> 1),
  622. ablkctx->enckey_len >> 1);
  623. memcpy(chcr_req->key_ctx.key +
  624. (ablkctx->enckey_len >> 1),
  625. ablkctx->key,
  626. ablkctx->enckey_len >> 1);
  627. }
  628. }
  629. phys_cpl = (struct cpl_rx_phys_dsgl *)((u8 *)(chcr_req + 1) + kctx_len);
  630. sg_param.nents = reqctx->dst_nents;
  631. sg_param.obsize = wrparam->bytes;
  632. sg_param.qid = wrparam->qid;
  633. error = map_writesg_phys_cpl(&u_ctx->lldi.pdev->dev, phys_cpl,
  634. reqctx->dst, &sg_param);
  635. if (error)
  636. goto map_fail1;
  637. skb_set_transport_header(skb, transhdr_len);
  638. write_buffer_to_skb(skb, &frags, reqctx->iv, ivsize);
  639. write_sg_to_skb(skb, &frags, wrparam->srcsg, wrparam->bytes);
  640. atomic_inc(&adap->chcr_stats.cipher_rqst);
  641. create_wreq(ctx, chcr_req, &(wrparam->req->base), skb, kctx_len, 0, 1,
  642. sizeof(struct cpl_rx_phys_dsgl) + phys_dsgl,
  643. ablkctx->ciph_mode == CHCR_SCMD_CIPHER_MODE_AES_CBC);
  644. reqctx->skb = skb;
  645. skb_get(skb);
  646. return skb;
  647. map_fail1:
  648. kfree_skb(skb);
  649. err:
  650. return ERR_PTR(error);
  651. }
  652. static inline int chcr_keyctx_ck_size(unsigned int keylen)
  653. {
  654. int ck_size = 0;
  655. if (keylen == AES_KEYSIZE_128)
  656. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
  657. else if (keylen == AES_KEYSIZE_192)
  658. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
  659. else if (keylen == AES_KEYSIZE_256)
  660. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
  661. else
  662. ck_size = 0;
  663. return ck_size;
  664. }
  665. static int chcr_cipher_fallback_setkey(struct crypto_ablkcipher *cipher,
  666. const u8 *key,
  667. unsigned int keylen)
  668. {
  669. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
  670. struct chcr_context *ctx = crypto_ablkcipher_ctx(cipher);
  671. struct ablk_ctx *ablkctx = ABLK_CTX(ctx);
  672. int err = 0;
  673. crypto_skcipher_clear_flags(ablkctx->sw_cipher, CRYPTO_TFM_REQ_MASK);
  674. crypto_skcipher_set_flags(ablkctx->sw_cipher, cipher->base.crt_flags &
  675. CRYPTO_TFM_REQ_MASK);
  676. err = crypto_skcipher_setkey(ablkctx->sw_cipher, key, keylen);
  677. tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
  678. tfm->crt_flags |=
  679. crypto_skcipher_get_flags(ablkctx->sw_cipher) &
  680. CRYPTO_TFM_RES_MASK;
  681. return err;
  682. }
  683. static int chcr_aes_cbc_setkey(struct crypto_ablkcipher *cipher,
  684. const u8 *key,
  685. unsigned int keylen)
  686. {
  687. struct chcr_context *ctx = crypto_ablkcipher_ctx(cipher);
  688. struct ablk_ctx *ablkctx = ABLK_CTX(ctx);
  689. unsigned int ck_size, context_size;
  690. u16 alignment = 0;
  691. int err;
  692. err = chcr_cipher_fallback_setkey(cipher, key, keylen);
  693. if (err)
  694. goto badkey_err;
  695. ck_size = chcr_keyctx_ck_size(keylen);
  696. alignment = ck_size == CHCR_KEYCTX_CIPHER_KEY_SIZE_192 ? 8 : 0;
  697. memcpy(ablkctx->key, key, keylen);
  698. ablkctx->enckey_len = keylen;
  699. get_aes_decrypt_key(ablkctx->rrkey, ablkctx->key, keylen << 3);
  700. context_size = (KEY_CONTEXT_HDR_SALT_AND_PAD +
  701. keylen + alignment) >> 4;
  702. ablkctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, CHCR_KEYCTX_NO_KEY,
  703. 0, 0, context_size);
  704. ablkctx->ciph_mode = CHCR_SCMD_CIPHER_MODE_AES_CBC;
  705. return 0;
  706. badkey_err:
  707. crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  708. ablkctx->enckey_len = 0;
  709. return err;
  710. }
  711. static int chcr_aes_ctr_setkey(struct crypto_ablkcipher *cipher,
  712. const u8 *key,
  713. unsigned int keylen)
  714. {
  715. struct chcr_context *ctx = crypto_ablkcipher_ctx(cipher);
  716. struct ablk_ctx *ablkctx = ABLK_CTX(ctx);
  717. unsigned int ck_size, context_size;
  718. u16 alignment = 0;
  719. int err;
  720. err = chcr_cipher_fallback_setkey(cipher, key, keylen);
  721. if (err)
  722. goto badkey_err;
  723. ck_size = chcr_keyctx_ck_size(keylen);
  724. alignment = (ck_size == CHCR_KEYCTX_CIPHER_KEY_SIZE_192) ? 8 : 0;
  725. memcpy(ablkctx->key, key, keylen);
  726. ablkctx->enckey_len = keylen;
  727. context_size = (KEY_CONTEXT_HDR_SALT_AND_PAD +
  728. keylen + alignment) >> 4;
  729. ablkctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, CHCR_KEYCTX_NO_KEY,
  730. 0, 0, context_size);
  731. ablkctx->ciph_mode = CHCR_SCMD_CIPHER_MODE_AES_CTR;
  732. return 0;
  733. badkey_err:
  734. crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  735. ablkctx->enckey_len = 0;
  736. return err;
  737. }
  738. static int chcr_aes_rfc3686_setkey(struct crypto_ablkcipher *cipher,
  739. const u8 *key,
  740. unsigned int keylen)
  741. {
  742. struct chcr_context *ctx = crypto_ablkcipher_ctx(cipher);
  743. struct ablk_ctx *ablkctx = ABLK_CTX(ctx);
  744. unsigned int ck_size, context_size;
  745. u16 alignment = 0;
  746. int err;
  747. if (keylen < CTR_RFC3686_NONCE_SIZE)
  748. return -EINVAL;
  749. memcpy(ablkctx->nonce, key + (keylen - CTR_RFC3686_NONCE_SIZE),
  750. CTR_RFC3686_NONCE_SIZE);
  751. keylen -= CTR_RFC3686_NONCE_SIZE;
  752. err = chcr_cipher_fallback_setkey(cipher, key, keylen);
  753. if (err)
  754. goto badkey_err;
  755. ck_size = chcr_keyctx_ck_size(keylen);
  756. alignment = (ck_size == CHCR_KEYCTX_CIPHER_KEY_SIZE_192) ? 8 : 0;
  757. memcpy(ablkctx->key, key, keylen);
  758. ablkctx->enckey_len = keylen;
  759. context_size = (KEY_CONTEXT_HDR_SALT_AND_PAD +
  760. keylen + alignment) >> 4;
  761. ablkctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, CHCR_KEYCTX_NO_KEY,
  762. 0, 0, context_size);
  763. ablkctx->ciph_mode = CHCR_SCMD_CIPHER_MODE_AES_CTR;
  764. return 0;
  765. badkey_err:
  766. crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  767. ablkctx->enckey_len = 0;
  768. return err;
  769. }
  770. static void ctr_add_iv(u8 *dstiv, u8 *srciv, u32 add)
  771. {
  772. unsigned int size = AES_BLOCK_SIZE;
  773. __be32 *b = (__be32 *)(dstiv + size);
  774. u32 c, prev;
  775. memcpy(dstiv, srciv, AES_BLOCK_SIZE);
  776. for (; size >= 4; size -= 4) {
  777. prev = be32_to_cpu(*--b);
  778. c = prev + add;
  779. *b = cpu_to_be32(c);
  780. if (prev < c)
  781. break;
  782. add = 1;
  783. }
  784. }
  785. static unsigned int adjust_ctr_overflow(u8 *iv, u32 bytes)
  786. {
  787. __be32 *b = (__be32 *)(iv + AES_BLOCK_SIZE);
  788. u64 c;
  789. u32 temp = be32_to_cpu(*--b);
  790. temp = ~temp;
  791. c = (u64)temp + 1; // No of block can processed withou overflow
  792. if ((bytes / AES_BLOCK_SIZE) > c)
  793. bytes = c * AES_BLOCK_SIZE;
  794. return bytes;
  795. }
  796. static int chcr_update_tweak(struct ablkcipher_request *req, u8 *iv)
  797. {
  798. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  799. struct chcr_context *ctx = crypto_ablkcipher_ctx(tfm);
  800. struct ablk_ctx *ablkctx = ABLK_CTX(ctx);
  801. struct chcr_blkcipher_req_ctx *reqctx = ablkcipher_request_ctx(req);
  802. struct crypto_cipher *cipher;
  803. int ret, i;
  804. u8 *key;
  805. unsigned int keylen;
  806. cipher = ablkctx->aes_generic;
  807. memcpy(iv, req->info, AES_BLOCK_SIZE);
  808. keylen = ablkctx->enckey_len / 2;
  809. key = ablkctx->key + keylen;
  810. ret = crypto_cipher_setkey(cipher, key, keylen);
  811. if (ret)
  812. goto out;
  813. crypto_cipher_encrypt_one(cipher, iv, iv);
  814. for (i = 0; i < (reqctx->processed / AES_BLOCK_SIZE); i++)
  815. gf128mul_x_ble((le128 *)iv, (le128 *)iv);
  816. crypto_cipher_decrypt_one(cipher, iv, iv);
  817. out:
  818. return ret;
  819. }
  820. static int chcr_update_cipher_iv(struct ablkcipher_request *req,
  821. struct cpl_fw6_pld *fw6_pld, u8 *iv)
  822. {
  823. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  824. struct chcr_blkcipher_req_ctx *reqctx = ablkcipher_request_ctx(req);
  825. int subtype = get_cryptoalg_subtype(crypto_ablkcipher_tfm(tfm));
  826. int ret = 0;
  827. if (subtype == CRYPTO_ALG_SUB_TYPE_CTR)
  828. ctr_add_iv(iv, req->info, (reqctx->processed /
  829. AES_BLOCK_SIZE));
  830. else if (subtype == CRYPTO_ALG_SUB_TYPE_CTR_RFC3686)
  831. *(__be32 *)(reqctx->iv + CTR_RFC3686_NONCE_SIZE +
  832. CTR_RFC3686_IV_SIZE) = cpu_to_be32((reqctx->processed /
  833. AES_BLOCK_SIZE) + 1);
  834. else if (subtype == CRYPTO_ALG_SUB_TYPE_XTS)
  835. ret = chcr_update_tweak(req, iv);
  836. else if (subtype == CRYPTO_ALG_SUB_TYPE_CBC) {
  837. if (reqctx->op)
  838. sg_pcopy_to_buffer(req->src, sg_nents(req->src), iv,
  839. 16,
  840. reqctx->processed - AES_BLOCK_SIZE);
  841. else
  842. memcpy(iv, &fw6_pld->data[2], AES_BLOCK_SIZE);
  843. }
  844. return ret;
  845. }
  846. /* We need separate function for final iv because in rfc3686 Initial counter
  847. * starts from 1 and buffer size of iv is 8 byte only which remains constant
  848. * for subsequent update requests
  849. */
  850. static int chcr_final_cipher_iv(struct ablkcipher_request *req,
  851. struct cpl_fw6_pld *fw6_pld, u8 *iv)
  852. {
  853. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  854. struct chcr_blkcipher_req_ctx *reqctx = ablkcipher_request_ctx(req);
  855. int subtype = get_cryptoalg_subtype(crypto_ablkcipher_tfm(tfm));
  856. int ret = 0;
  857. if (subtype == CRYPTO_ALG_SUB_TYPE_CTR)
  858. ctr_add_iv(iv, req->info, (reqctx->processed /
  859. AES_BLOCK_SIZE));
  860. else if (subtype == CRYPTO_ALG_SUB_TYPE_XTS)
  861. ret = chcr_update_tweak(req, iv);
  862. else if (subtype == CRYPTO_ALG_SUB_TYPE_CBC) {
  863. if (reqctx->op)
  864. sg_pcopy_to_buffer(req->src, sg_nents(req->src), iv,
  865. 16,
  866. reqctx->processed - AES_BLOCK_SIZE);
  867. else
  868. memcpy(iv, &fw6_pld->data[2], AES_BLOCK_SIZE);
  869. }
  870. return ret;
  871. }
  872. static int chcr_handle_cipher_resp(struct ablkcipher_request *req,
  873. unsigned char *input, int err)
  874. {
  875. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  876. struct chcr_context *ctx = crypto_ablkcipher_ctx(tfm);
  877. struct uld_ctx *u_ctx = ULD_CTX(ctx);
  878. struct ablk_ctx *ablkctx = ABLK_CTX(ctx);
  879. struct sk_buff *skb;
  880. struct cpl_fw6_pld *fw6_pld = (struct cpl_fw6_pld *)input;
  881. struct chcr_blkcipher_req_ctx *reqctx = ablkcipher_request_ctx(req);
  882. struct cipher_wr_param wrparam;
  883. int bytes;
  884. dma_unmap_sg(&u_ctx->lldi.pdev->dev, reqctx->dst, reqctx->dst_nents,
  885. DMA_FROM_DEVICE);
  886. if (reqctx->skb) {
  887. kfree_skb(reqctx->skb);
  888. reqctx->skb = NULL;
  889. }
  890. if (err)
  891. goto complete;
  892. if (req->nbytes == reqctx->processed) {
  893. err = chcr_final_cipher_iv(req, fw6_pld, req->info);
  894. goto complete;
  895. }
  896. if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
  897. ctx->tx_qidx))) {
  898. if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
  899. err = -EBUSY;
  900. goto complete;
  901. }
  902. }
  903. wrparam.srcsg = scatterwalk_ffwd(reqctx->srcffwd, req->src,
  904. reqctx->processed);
  905. reqctx->dst = scatterwalk_ffwd(reqctx->dstffwd, reqctx->dstsg,
  906. reqctx->processed);
  907. if (!wrparam.srcsg || !reqctx->dst) {
  908. pr_err("Input sg list length less that nbytes\n");
  909. err = -EINVAL;
  910. goto complete;
  911. }
  912. bytes = chcr_sg_ent_in_wr(wrparam.srcsg, reqctx->dst, 1,
  913. SPACE_LEFT(ablkctx->enckey_len),
  914. &wrparam.snent, &reqctx->dst_nents);
  915. if ((bytes + reqctx->processed) >= req->nbytes)
  916. bytes = req->nbytes - reqctx->processed;
  917. else
  918. bytes = ROUND_16(bytes);
  919. err = chcr_update_cipher_iv(req, fw6_pld, reqctx->iv);
  920. if (err)
  921. goto complete;
  922. if (unlikely(bytes == 0)) {
  923. err = chcr_cipher_fallback(ablkctx->sw_cipher,
  924. req->base.flags,
  925. wrparam.srcsg,
  926. reqctx->dst,
  927. req->nbytes - reqctx->processed,
  928. reqctx->iv,
  929. reqctx->op);
  930. goto complete;
  931. }
  932. if (get_cryptoalg_subtype(crypto_ablkcipher_tfm(tfm)) ==
  933. CRYPTO_ALG_SUB_TYPE_CTR)
  934. bytes = adjust_ctr_overflow(reqctx->iv, bytes);
  935. reqctx->processed += bytes;
  936. wrparam.qid = u_ctx->lldi.rxq_ids[ctx->rx_qidx];
  937. wrparam.req = req;
  938. wrparam.bytes = bytes;
  939. skb = create_cipher_wr(&wrparam);
  940. if (IS_ERR(skb)) {
  941. pr_err("chcr : %s : Failed to form WR. No memory\n", __func__);
  942. err = PTR_ERR(skb);
  943. goto complete;
  944. }
  945. skb->dev = u_ctx->lldi.ports[0];
  946. set_wr_txq(skb, CPL_PRIORITY_DATA, ctx->tx_qidx);
  947. chcr_send_wr(skb);
  948. return 0;
  949. complete:
  950. free_new_sg(reqctx->newdstsg);
  951. reqctx->newdstsg = NULL;
  952. req->base.complete(&req->base, err);
  953. return err;
  954. }
  955. static int process_cipher(struct ablkcipher_request *req,
  956. unsigned short qid,
  957. struct sk_buff **skb,
  958. unsigned short op_type)
  959. {
  960. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  961. unsigned int ivsize = crypto_ablkcipher_ivsize(tfm);
  962. struct chcr_blkcipher_req_ctx *reqctx = ablkcipher_request_ctx(req);
  963. struct chcr_context *ctx = crypto_ablkcipher_ctx(tfm);
  964. struct ablk_ctx *ablkctx = ABLK_CTX(ctx);
  965. struct cipher_wr_param wrparam;
  966. int bytes, nents, err = -EINVAL;
  967. reqctx->newdstsg = NULL;
  968. reqctx->processed = 0;
  969. if (!req->info)
  970. goto error;
  971. if ((ablkctx->enckey_len == 0) || (ivsize > AES_BLOCK_SIZE) ||
  972. (req->nbytes == 0) ||
  973. (req->nbytes % crypto_ablkcipher_blocksize(tfm))) {
  974. pr_err("AES: Invalid value of Key Len %d nbytes %d IV Len %d\n",
  975. ablkctx->enckey_len, req->nbytes, ivsize);
  976. goto error;
  977. }
  978. wrparam.srcsg = req->src;
  979. if (is_newsg(req->dst, &nents)) {
  980. reqctx->newdstsg = alloc_new_sg(req->dst, nents);
  981. if (IS_ERR(reqctx->newdstsg))
  982. return PTR_ERR(reqctx->newdstsg);
  983. reqctx->dstsg = reqctx->newdstsg;
  984. } else {
  985. reqctx->dstsg = req->dst;
  986. }
  987. bytes = chcr_sg_ent_in_wr(wrparam.srcsg, reqctx->dstsg, MIN_CIPHER_SG,
  988. SPACE_LEFT(ablkctx->enckey_len),
  989. &wrparam.snent,
  990. &reqctx->dst_nents);
  991. if ((bytes + reqctx->processed) >= req->nbytes)
  992. bytes = req->nbytes - reqctx->processed;
  993. else
  994. bytes = ROUND_16(bytes);
  995. if (unlikely(bytes > req->nbytes))
  996. bytes = req->nbytes;
  997. if (get_cryptoalg_subtype(crypto_ablkcipher_tfm(tfm)) ==
  998. CRYPTO_ALG_SUB_TYPE_CTR) {
  999. bytes = adjust_ctr_overflow(req->info, bytes);
  1000. }
  1001. if (get_cryptoalg_subtype(crypto_ablkcipher_tfm(tfm)) ==
  1002. CRYPTO_ALG_SUB_TYPE_CTR_RFC3686) {
  1003. memcpy(reqctx->iv, ablkctx->nonce, CTR_RFC3686_NONCE_SIZE);
  1004. memcpy(reqctx->iv + CTR_RFC3686_NONCE_SIZE, req->info,
  1005. CTR_RFC3686_IV_SIZE);
  1006. /* initialize counter portion of counter block */
  1007. *(__be32 *)(reqctx->iv + CTR_RFC3686_NONCE_SIZE +
  1008. CTR_RFC3686_IV_SIZE) = cpu_to_be32(1);
  1009. } else {
  1010. memcpy(reqctx->iv, req->info, ivsize);
  1011. }
  1012. if (unlikely(bytes == 0)) {
  1013. err = chcr_cipher_fallback(ablkctx->sw_cipher,
  1014. req->base.flags,
  1015. req->src,
  1016. req->dst,
  1017. req->nbytes,
  1018. req->info,
  1019. op_type);
  1020. goto error;
  1021. }
  1022. reqctx->processed = bytes;
  1023. reqctx->dst = reqctx->dstsg;
  1024. reqctx->op = op_type;
  1025. wrparam.qid = qid;
  1026. wrparam.req = req;
  1027. wrparam.bytes = bytes;
  1028. *skb = create_cipher_wr(&wrparam);
  1029. if (IS_ERR(*skb)) {
  1030. err = PTR_ERR(*skb);
  1031. goto error;
  1032. }
  1033. return 0;
  1034. error:
  1035. free_new_sg(reqctx->newdstsg);
  1036. reqctx->newdstsg = NULL;
  1037. return err;
  1038. }
  1039. static int chcr_aes_encrypt(struct ablkcipher_request *req)
  1040. {
  1041. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  1042. struct chcr_context *ctx = crypto_ablkcipher_ctx(tfm);
  1043. struct sk_buff *skb = NULL;
  1044. int err;
  1045. struct uld_ctx *u_ctx = ULD_CTX(ctx);
  1046. if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
  1047. ctx->tx_qidx))) {
  1048. if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
  1049. return -EBUSY;
  1050. }
  1051. err = process_cipher(req, u_ctx->lldi.rxq_ids[ctx->rx_qidx], &skb,
  1052. CHCR_ENCRYPT_OP);
  1053. if (err || !skb)
  1054. return err;
  1055. skb->dev = u_ctx->lldi.ports[0];
  1056. set_wr_txq(skb, CPL_PRIORITY_DATA, ctx->tx_qidx);
  1057. chcr_send_wr(skb);
  1058. return -EINPROGRESS;
  1059. }
  1060. static int chcr_aes_decrypt(struct ablkcipher_request *req)
  1061. {
  1062. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  1063. struct chcr_context *ctx = crypto_ablkcipher_ctx(tfm);
  1064. struct uld_ctx *u_ctx = ULD_CTX(ctx);
  1065. struct sk_buff *skb = NULL;
  1066. int err;
  1067. if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
  1068. ctx->tx_qidx))) {
  1069. if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
  1070. return -EBUSY;
  1071. }
  1072. err = process_cipher(req, u_ctx->lldi.rxq_ids[ctx->rx_qidx], &skb,
  1073. CHCR_DECRYPT_OP);
  1074. if (err || !skb)
  1075. return err;
  1076. skb->dev = u_ctx->lldi.ports[0];
  1077. set_wr_txq(skb, CPL_PRIORITY_DATA, ctx->tx_qidx);
  1078. chcr_send_wr(skb);
  1079. return -EINPROGRESS;
  1080. }
  1081. static int chcr_device_init(struct chcr_context *ctx)
  1082. {
  1083. struct uld_ctx *u_ctx = NULL;
  1084. struct adapter *adap;
  1085. unsigned int id;
  1086. int txq_perchan, txq_idx, ntxq;
  1087. int err = 0, rxq_perchan, rxq_idx;
  1088. id = smp_processor_id();
  1089. if (!ctx->dev) {
  1090. u_ctx = assign_chcr_device();
  1091. if (!u_ctx) {
  1092. pr_err("chcr device assignment fails\n");
  1093. goto out;
  1094. }
  1095. ctx->dev = u_ctx->dev;
  1096. adap = padap(ctx->dev);
  1097. ntxq = min_not_zero((unsigned int)u_ctx->lldi.nrxq,
  1098. adap->vres.ncrypto_fc);
  1099. rxq_perchan = u_ctx->lldi.nrxq / u_ctx->lldi.nchan;
  1100. txq_perchan = ntxq / u_ctx->lldi.nchan;
  1101. rxq_idx = ctx->dev->tx_channel_id * rxq_perchan;
  1102. rxq_idx += id % rxq_perchan;
  1103. txq_idx = ctx->dev->tx_channel_id * txq_perchan;
  1104. txq_idx += id % txq_perchan;
  1105. spin_lock(&ctx->dev->lock_chcr_dev);
  1106. ctx->rx_qidx = rxq_idx;
  1107. ctx->tx_qidx = txq_idx;
  1108. ctx->dev->tx_channel_id = !ctx->dev->tx_channel_id;
  1109. ctx->dev->rx_channel_id = 0;
  1110. spin_unlock(&ctx->dev->lock_chcr_dev);
  1111. }
  1112. out:
  1113. return err;
  1114. }
  1115. static int chcr_cra_init(struct crypto_tfm *tfm)
  1116. {
  1117. struct crypto_alg *alg = tfm->__crt_alg;
  1118. struct chcr_context *ctx = crypto_tfm_ctx(tfm);
  1119. struct ablk_ctx *ablkctx = ABLK_CTX(ctx);
  1120. ablkctx->sw_cipher = crypto_alloc_skcipher(alg->cra_name, 0,
  1121. CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK);
  1122. if (IS_ERR(ablkctx->sw_cipher)) {
  1123. pr_err("failed to allocate fallback for %s\n", alg->cra_name);
  1124. return PTR_ERR(ablkctx->sw_cipher);
  1125. }
  1126. if (get_cryptoalg_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_XTS) {
  1127. /* To update tweak*/
  1128. ablkctx->aes_generic = crypto_alloc_cipher("aes-generic", 0, 0);
  1129. if (IS_ERR(ablkctx->aes_generic)) {
  1130. pr_err("failed to allocate aes cipher for tweak\n");
  1131. return PTR_ERR(ablkctx->aes_generic);
  1132. }
  1133. } else
  1134. ablkctx->aes_generic = NULL;
  1135. tfm->crt_ablkcipher.reqsize = sizeof(struct chcr_blkcipher_req_ctx);
  1136. return chcr_device_init(crypto_tfm_ctx(tfm));
  1137. }
  1138. static int chcr_rfc3686_init(struct crypto_tfm *tfm)
  1139. {
  1140. struct crypto_alg *alg = tfm->__crt_alg;
  1141. struct chcr_context *ctx = crypto_tfm_ctx(tfm);
  1142. struct ablk_ctx *ablkctx = ABLK_CTX(ctx);
  1143. /*RFC3686 initialises IV counter value to 1, rfc3686(ctr(aes))
  1144. * cannot be used as fallback in chcr_handle_cipher_response
  1145. */
  1146. ablkctx->sw_cipher = crypto_alloc_skcipher("ctr(aes)", 0,
  1147. CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK);
  1148. if (IS_ERR(ablkctx->sw_cipher)) {
  1149. pr_err("failed to allocate fallback for %s\n", alg->cra_name);
  1150. return PTR_ERR(ablkctx->sw_cipher);
  1151. }
  1152. tfm->crt_ablkcipher.reqsize = sizeof(struct chcr_blkcipher_req_ctx);
  1153. return chcr_device_init(crypto_tfm_ctx(tfm));
  1154. }
  1155. static void chcr_cra_exit(struct crypto_tfm *tfm)
  1156. {
  1157. struct chcr_context *ctx = crypto_tfm_ctx(tfm);
  1158. struct ablk_ctx *ablkctx = ABLK_CTX(ctx);
  1159. crypto_free_skcipher(ablkctx->sw_cipher);
  1160. if (ablkctx->aes_generic)
  1161. crypto_free_cipher(ablkctx->aes_generic);
  1162. }
  1163. static int get_alg_config(struct algo_param *params,
  1164. unsigned int auth_size)
  1165. {
  1166. switch (auth_size) {
  1167. case SHA1_DIGEST_SIZE:
  1168. params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_160;
  1169. params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA1;
  1170. params->result_size = SHA1_DIGEST_SIZE;
  1171. break;
  1172. case SHA224_DIGEST_SIZE:
  1173. params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_256;
  1174. params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA224;
  1175. params->result_size = SHA256_DIGEST_SIZE;
  1176. break;
  1177. case SHA256_DIGEST_SIZE:
  1178. params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_256;
  1179. params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA256;
  1180. params->result_size = SHA256_DIGEST_SIZE;
  1181. break;
  1182. case SHA384_DIGEST_SIZE:
  1183. params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_512;
  1184. params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA512_384;
  1185. params->result_size = SHA512_DIGEST_SIZE;
  1186. break;
  1187. case SHA512_DIGEST_SIZE:
  1188. params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_512;
  1189. params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA512_512;
  1190. params->result_size = SHA512_DIGEST_SIZE;
  1191. break;
  1192. default:
  1193. pr_err("chcr : ERROR, unsupported digest size\n");
  1194. return -EINVAL;
  1195. }
  1196. return 0;
  1197. }
  1198. static inline void chcr_free_shash(struct crypto_shash *base_hash)
  1199. {
  1200. crypto_free_shash(base_hash);
  1201. }
  1202. /**
  1203. * create_hash_wr - Create hash work request
  1204. * @req - Cipher req base
  1205. */
  1206. static struct sk_buff *create_hash_wr(struct ahash_request *req,
  1207. struct hash_wr_param *param)
  1208. {
  1209. struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
  1210. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1211. struct chcr_context *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
  1212. struct hmac_ctx *hmacctx = HMAC_CTX(ctx);
  1213. struct sk_buff *skb = NULL;
  1214. struct chcr_wr *chcr_req;
  1215. unsigned int frags = 0, transhdr_len, iopad_alignment = 0;
  1216. unsigned int digestsize = crypto_ahash_digestsize(tfm);
  1217. unsigned int kctx_len = 0;
  1218. u8 hash_size_in_response = 0;
  1219. gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
  1220. GFP_ATOMIC;
  1221. struct adapter *adap = padap(ctx->dev);
  1222. iopad_alignment = KEYCTX_ALIGN_PAD(digestsize);
  1223. kctx_len = param->alg_prm.result_size + iopad_alignment;
  1224. if (param->opad_needed)
  1225. kctx_len += param->alg_prm.result_size + iopad_alignment;
  1226. if (req_ctx->result)
  1227. hash_size_in_response = digestsize;
  1228. else
  1229. hash_size_in_response = param->alg_prm.result_size;
  1230. transhdr_len = HASH_TRANSHDR_SIZE(kctx_len);
  1231. skb = alloc_skb((transhdr_len + sizeof(struct sge_opaque_hdr)), flags);
  1232. if (!skb)
  1233. return skb;
  1234. skb_reserve(skb, sizeof(struct sge_opaque_hdr));
  1235. chcr_req = __skb_put_zero(skb, transhdr_len);
  1236. chcr_req->sec_cpl.op_ivinsrtofst =
  1237. FILL_SEC_CPL_OP_IVINSR(ctx->dev->rx_channel_id, 2, 0);
  1238. chcr_req->sec_cpl.pldlen = htonl(param->bfr_len + param->sg_len);
  1239. chcr_req->sec_cpl.aadstart_cipherstop_hi =
  1240. FILL_SEC_CPL_CIPHERSTOP_HI(0, 0, 0, 0);
  1241. chcr_req->sec_cpl.cipherstop_lo_authinsert =
  1242. FILL_SEC_CPL_AUTHINSERT(0, 1, 0, 0);
  1243. chcr_req->sec_cpl.seqno_numivs =
  1244. FILL_SEC_CPL_SCMD0_SEQNO(0, 0, 0, param->alg_prm.auth_mode,
  1245. param->opad_needed, 0);
  1246. chcr_req->sec_cpl.ivgen_hdrlen =
  1247. FILL_SEC_CPL_IVGEN_HDRLEN(param->last, param->more, 0, 1, 0, 0);
  1248. memcpy(chcr_req->key_ctx.key, req_ctx->partial_hash,
  1249. param->alg_prm.result_size);
  1250. if (param->opad_needed)
  1251. memcpy(chcr_req->key_ctx.key +
  1252. ((param->alg_prm.result_size <= 32) ? 32 :
  1253. CHCR_HASH_MAX_DIGEST_SIZE),
  1254. hmacctx->opad, param->alg_prm.result_size);
  1255. chcr_req->key_ctx.ctx_hdr = FILL_KEY_CTX_HDR(CHCR_KEYCTX_NO_KEY,
  1256. param->alg_prm.mk_size, 0,
  1257. param->opad_needed,
  1258. ((kctx_len +
  1259. sizeof(chcr_req->key_ctx)) >> 4));
  1260. chcr_req->sec_cpl.scmd1 = cpu_to_be64((u64)param->scmd1);
  1261. skb_set_transport_header(skb, transhdr_len);
  1262. if (param->bfr_len != 0)
  1263. write_buffer_to_skb(skb, &frags, req_ctx->reqbfr,
  1264. param->bfr_len);
  1265. if (param->sg_len != 0)
  1266. write_sg_to_skb(skb, &frags, req->src, param->sg_len);
  1267. atomic_inc(&adap->chcr_stats.digest_rqst);
  1268. create_wreq(ctx, chcr_req, &req->base, skb, kctx_len,
  1269. hash_size_in_response, 0, DUMMY_BYTES, 0);
  1270. req_ctx->skb = skb;
  1271. skb_get(skb);
  1272. return skb;
  1273. }
  1274. static int chcr_ahash_update(struct ahash_request *req)
  1275. {
  1276. struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
  1277. struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
  1278. struct chcr_context *ctx = crypto_tfm_ctx(crypto_ahash_tfm(rtfm));
  1279. struct uld_ctx *u_ctx = NULL;
  1280. struct sk_buff *skb;
  1281. u8 remainder = 0, bs;
  1282. unsigned int nbytes = req->nbytes;
  1283. struct hash_wr_param params;
  1284. bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
  1285. u_ctx = ULD_CTX(ctx);
  1286. if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
  1287. ctx->tx_qidx))) {
  1288. if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
  1289. return -EBUSY;
  1290. }
  1291. if (nbytes + req_ctx->reqlen >= bs) {
  1292. remainder = (nbytes + req_ctx->reqlen) % bs;
  1293. nbytes = nbytes + req_ctx->reqlen - remainder;
  1294. } else {
  1295. sg_pcopy_to_buffer(req->src, sg_nents(req->src), req_ctx->reqbfr
  1296. + req_ctx->reqlen, nbytes, 0);
  1297. req_ctx->reqlen += nbytes;
  1298. return 0;
  1299. }
  1300. params.opad_needed = 0;
  1301. params.more = 1;
  1302. params.last = 0;
  1303. params.sg_len = nbytes - req_ctx->reqlen;
  1304. params.bfr_len = req_ctx->reqlen;
  1305. params.scmd1 = 0;
  1306. get_alg_config(&params.alg_prm, crypto_ahash_digestsize(rtfm));
  1307. req_ctx->result = 0;
  1308. req_ctx->data_len += params.sg_len + params.bfr_len;
  1309. skb = create_hash_wr(req, &params);
  1310. if (!skb)
  1311. return -ENOMEM;
  1312. if (remainder) {
  1313. u8 *temp;
  1314. /* Swap buffers */
  1315. temp = req_ctx->reqbfr;
  1316. req_ctx->reqbfr = req_ctx->skbfr;
  1317. req_ctx->skbfr = temp;
  1318. sg_pcopy_to_buffer(req->src, sg_nents(req->src),
  1319. req_ctx->reqbfr, remainder, req->nbytes -
  1320. remainder);
  1321. }
  1322. req_ctx->reqlen = remainder;
  1323. skb->dev = u_ctx->lldi.ports[0];
  1324. set_wr_txq(skb, CPL_PRIORITY_DATA, ctx->tx_qidx);
  1325. chcr_send_wr(skb);
  1326. return -EINPROGRESS;
  1327. }
  1328. static void create_last_hash_block(char *bfr_ptr, unsigned int bs, u64 scmd1)
  1329. {
  1330. memset(bfr_ptr, 0, bs);
  1331. *bfr_ptr = 0x80;
  1332. if (bs == 64)
  1333. *(__be64 *)(bfr_ptr + 56) = cpu_to_be64(scmd1 << 3);
  1334. else
  1335. *(__be64 *)(bfr_ptr + 120) = cpu_to_be64(scmd1 << 3);
  1336. }
  1337. static int chcr_ahash_final(struct ahash_request *req)
  1338. {
  1339. struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
  1340. struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
  1341. struct chcr_context *ctx = crypto_tfm_ctx(crypto_ahash_tfm(rtfm));
  1342. struct hash_wr_param params;
  1343. struct sk_buff *skb;
  1344. struct uld_ctx *u_ctx = NULL;
  1345. u8 bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
  1346. u_ctx = ULD_CTX(ctx);
  1347. if (is_hmac(crypto_ahash_tfm(rtfm)))
  1348. params.opad_needed = 1;
  1349. else
  1350. params.opad_needed = 0;
  1351. params.sg_len = 0;
  1352. get_alg_config(&params.alg_prm, crypto_ahash_digestsize(rtfm));
  1353. req_ctx->result = 1;
  1354. params.bfr_len = req_ctx->reqlen;
  1355. req_ctx->data_len += params.bfr_len + params.sg_len;
  1356. if (req_ctx->reqlen == 0) {
  1357. create_last_hash_block(req_ctx->reqbfr, bs, req_ctx->data_len);
  1358. params.last = 0;
  1359. params.more = 1;
  1360. params.scmd1 = 0;
  1361. params.bfr_len = bs;
  1362. } else {
  1363. params.scmd1 = req_ctx->data_len;
  1364. params.last = 1;
  1365. params.more = 0;
  1366. }
  1367. skb = create_hash_wr(req, &params);
  1368. if (!skb)
  1369. return -ENOMEM;
  1370. skb->dev = u_ctx->lldi.ports[0];
  1371. set_wr_txq(skb, CPL_PRIORITY_DATA, ctx->tx_qidx);
  1372. chcr_send_wr(skb);
  1373. return -EINPROGRESS;
  1374. }
  1375. static int chcr_ahash_finup(struct ahash_request *req)
  1376. {
  1377. struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
  1378. struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
  1379. struct chcr_context *ctx = crypto_tfm_ctx(crypto_ahash_tfm(rtfm));
  1380. struct uld_ctx *u_ctx = NULL;
  1381. struct sk_buff *skb;
  1382. struct hash_wr_param params;
  1383. u8 bs;
  1384. bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
  1385. u_ctx = ULD_CTX(ctx);
  1386. if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
  1387. ctx->tx_qidx))) {
  1388. if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
  1389. return -EBUSY;
  1390. }
  1391. if (is_hmac(crypto_ahash_tfm(rtfm)))
  1392. params.opad_needed = 1;
  1393. else
  1394. params.opad_needed = 0;
  1395. params.sg_len = req->nbytes;
  1396. params.bfr_len = req_ctx->reqlen;
  1397. get_alg_config(&params.alg_prm, crypto_ahash_digestsize(rtfm));
  1398. req_ctx->data_len += params.bfr_len + params.sg_len;
  1399. req_ctx->result = 1;
  1400. if ((req_ctx->reqlen + req->nbytes) == 0) {
  1401. create_last_hash_block(req_ctx->reqbfr, bs, req_ctx->data_len);
  1402. params.last = 0;
  1403. params.more = 1;
  1404. params.scmd1 = 0;
  1405. params.bfr_len = bs;
  1406. } else {
  1407. params.scmd1 = req_ctx->data_len;
  1408. params.last = 1;
  1409. params.more = 0;
  1410. }
  1411. skb = create_hash_wr(req, &params);
  1412. if (!skb)
  1413. return -ENOMEM;
  1414. skb->dev = u_ctx->lldi.ports[0];
  1415. set_wr_txq(skb, CPL_PRIORITY_DATA, ctx->tx_qidx);
  1416. chcr_send_wr(skb);
  1417. return -EINPROGRESS;
  1418. }
  1419. static int chcr_ahash_digest(struct ahash_request *req)
  1420. {
  1421. struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
  1422. struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
  1423. struct chcr_context *ctx = crypto_tfm_ctx(crypto_ahash_tfm(rtfm));
  1424. struct uld_ctx *u_ctx = NULL;
  1425. struct sk_buff *skb;
  1426. struct hash_wr_param params;
  1427. u8 bs;
  1428. rtfm->init(req);
  1429. bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
  1430. u_ctx = ULD_CTX(ctx);
  1431. if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
  1432. ctx->tx_qidx))) {
  1433. if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
  1434. return -EBUSY;
  1435. }
  1436. if (is_hmac(crypto_ahash_tfm(rtfm)))
  1437. params.opad_needed = 1;
  1438. else
  1439. params.opad_needed = 0;
  1440. params.last = 0;
  1441. params.more = 0;
  1442. params.sg_len = req->nbytes;
  1443. params.bfr_len = 0;
  1444. params.scmd1 = 0;
  1445. get_alg_config(&params.alg_prm, crypto_ahash_digestsize(rtfm));
  1446. req_ctx->result = 1;
  1447. req_ctx->data_len += params.bfr_len + params.sg_len;
  1448. if (req->nbytes == 0) {
  1449. create_last_hash_block(req_ctx->reqbfr, bs, 0);
  1450. params.more = 1;
  1451. params.bfr_len = bs;
  1452. }
  1453. skb = create_hash_wr(req, &params);
  1454. if (!skb)
  1455. return -ENOMEM;
  1456. skb->dev = u_ctx->lldi.ports[0];
  1457. set_wr_txq(skb, CPL_PRIORITY_DATA, ctx->tx_qidx);
  1458. chcr_send_wr(skb);
  1459. return -EINPROGRESS;
  1460. }
  1461. static int chcr_ahash_export(struct ahash_request *areq, void *out)
  1462. {
  1463. struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  1464. struct chcr_ahash_req_ctx *state = out;
  1465. state->reqlen = req_ctx->reqlen;
  1466. state->data_len = req_ctx->data_len;
  1467. memcpy(state->bfr1, req_ctx->reqbfr, req_ctx->reqlen);
  1468. memcpy(state->partial_hash, req_ctx->partial_hash,
  1469. CHCR_HASH_MAX_DIGEST_SIZE);
  1470. return 0;
  1471. }
  1472. static int chcr_ahash_import(struct ahash_request *areq, const void *in)
  1473. {
  1474. struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  1475. struct chcr_ahash_req_ctx *state = (struct chcr_ahash_req_ctx *)in;
  1476. req_ctx->reqlen = state->reqlen;
  1477. req_ctx->data_len = state->data_len;
  1478. req_ctx->reqbfr = req_ctx->bfr1;
  1479. req_ctx->skbfr = req_ctx->bfr2;
  1480. memcpy(req_ctx->bfr1, state->bfr1, CHCR_HASH_MAX_BLOCK_SIZE_128);
  1481. memcpy(req_ctx->partial_hash, state->partial_hash,
  1482. CHCR_HASH_MAX_DIGEST_SIZE);
  1483. return 0;
  1484. }
  1485. static int chcr_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
  1486. unsigned int keylen)
  1487. {
  1488. struct chcr_context *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
  1489. struct hmac_ctx *hmacctx = HMAC_CTX(ctx);
  1490. unsigned int digestsize = crypto_ahash_digestsize(tfm);
  1491. unsigned int bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
  1492. unsigned int i, err = 0, updated_digestsize;
  1493. SHASH_DESC_ON_STACK(shash, hmacctx->base_hash);
  1494. /* use the key to calculate the ipad and opad. ipad will sent with the
  1495. * first request's data. opad will be sent with the final hash result
  1496. * ipad in hmacctx->ipad and opad in hmacctx->opad location
  1497. */
  1498. shash->tfm = hmacctx->base_hash;
  1499. shash->flags = crypto_shash_get_flags(hmacctx->base_hash);
  1500. if (keylen > bs) {
  1501. err = crypto_shash_digest(shash, key, keylen,
  1502. hmacctx->ipad);
  1503. if (err)
  1504. goto out;
  1505. keylen = digestsize;
  1506. } else {
  1507. memcpy(hmacctx->ipad, key, keylen);
  1508. }
  1509. memset(hmacctx->ipad + keylen, 0, bs - keylen);
  1510. memcpy(hmacctx->opad, hmacctx->ipad, bs);
  1511. for (i = 0; i < bs / sizeof(int); i++) {
  1512. *((unsigned int *)(&hmacctx->ipad) + i) ^= IPAD_DATA;
  1513. *((unsigned int *)(&hmacctx->opad) + i) ^= OPAD_DATA;
  1514. }
  1515. updated_digestsize = digestsize;
  1516. if (digestsize == SHA224_DIGEST_SIZE)
  1517. updated_digestsize = SHA256_DIGEST_SIZE;
  1518. else if (digestsize == SHA384_DIGEST_SIZE)
  1519. updated_digestsize = SHA512_DIGEST_SIZE;
  1520. err = chcr_compute_partial_hash(shash, hmacctx->ipad,
  1521. hmacctx->ipad, digestsize);
  1522. if (err)
  1523. goto out;
  1524. chcr_change_order(hmacctx->ipad, updated_digestsize);
  1525. err = chcr_compute_partial_hash(shash, hmacctx->opad,
  1526. hmacctx->opad, digestsize);
  1527. if (err)
  1528. goto out;
  1529. chcr_change_order(hmacctx->opad, updated_digestsize);
  1530. out:
  1531. return err;
  1532. }
  1533. static int chcr_aes_xts_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
  1534. unsigned int key_len)
  1535. {
  1536. struct chcr_context *ctx = crypto_ablkcipher_ctx(cipher);
  1537. struct ablk_ctx *ablkctx = ABLK_CTX(ctx);
  1538. unsigned short context_size = 0;
  1539. int err;
  1540. err = chcr_cipher_fallback_setkey(cipher, key, key_len);
  1541. if (err)
  1542. goto badkey_err;
  1543. memcpy(ablkctx->key, key, key_len);
  1544. ablkctx->enckey_len = key_len;
  1545. get_aes_decrypt_key(ablkctx->rrkey, ablkctx->key, key_len << 2);
  1546. context_size = (KEY_CONTEXT_HDR_SALT_AND_PAD + key_len) >> 4;
  1547. ablkctx->key_ctx_hdr =
  1548. FILL_KEY_CTX_HDR((key_len == AES_KEYSIZE_256) ?
  1549. CHCR_KEYCTX_CIPHER_KEY_SIZE_128 :
  1550. CHCR_KEYCTX_CIPHER_KEY_SIZE_256,
  1551. CHCR_KEYCTX_NO_KEY, 1,
  1552. 0, context_size);
  1553. ablkctx->ciph_mode = CHCR_SCMD_CIPHER_MODE_AES_XTS;
  1554. return 0;
  1555. badkey_err:
  1556. crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  1557. ablkctx->enckey_len = 0;
  1558. return err;
  1559. }
  1560. static int chcr_sha_init(struct ahash_request *areq)
  1561. {
  1562. struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  1563. struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
  1564. int digestsize = crypto_ahash_digestsize(tfm);
  1565. req_ctx->data_len = 0;
  1566. req_ctx->reqlen = 0;
  1567. req_ctx->reqbfr = req_ctx->bfr1;
  1568. req_ctx->skbfr = req_ctx->bfr2;
  1569. req_ctx->skb = NULL;
  1570. req_ctx->result = 0;
  1571. copy_hash_init_values(req_ctx->partial_hash, digestsize);
  1572. return 0;
  1573. }
  1574. static int chcr_sha_cra_init(struct crypto_tfm *tfm)
  1575. {
  1576. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  1577. sizeof(struct chcr_ahash_req_ctx));
  1578. return chcr_device_init(crypto_tfm_ctx(tfm));
  1579. }
  1580. static int chcr_hmac_init(struct ahash_request *areq)
  1581. {
  1582. struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  1583. struct crypto_ahash *rtfm = crypto_ahash_reqtfm(areq);
  1584. struct chcr_context *ctx = crypto_tfm_ctx(crypto_ahash_tfm(rtfm));
  1585. struct hmac_ctx *hmacctx = HMAC_CTX(ctx);
  1586. unsigned int digestsize = crypto_ahash_digestsize(rtfm);
  1587. unsigned int bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
  1588. chcr_sha_init(areq);
  1589. req_ctx->data_len = bs;
  1590. if (is_hmac(crypto_ahash_tfm(rtfm))) {
  1591. if (digestsize == SHA224_DIGEST_SIZE)
  1592. memcpy(req_ctx->partial_hash, hmacctx->ipad,
  1593. SHA256_DIGEST_SIZE);
  1594. else if (digestsize == SHA384_DIGEST_SIZE)
  1595. memcpy(req_ctx->partial_hash, hmacctx->ipad,
  1596. SHA512_DIGEST_SIZE);
  1597. else
  1598. memcpy(req_ctx->partial_hash, hmacctx->ipad,
  1599. digestsize);
  1600. }
  1601. return 0;
  1602. }
  1603. static int chcr_hmac_cra_init(struct crypto_tfm *tfm)
  1604. {
  1605. struct chcr_context *ctx = crypto_tfm_ctx(tfm);
  1606. struct hmac_ctx *hmacctx = HMAC_CTX(ctx);
  1607. unsigned int digestsize =
  1608. crypto_ahash_digestsize(__crypto_ahash_cast(tfm));
  1609. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  1610. sizeof(struct chcr_ahash_req_ctx));
  1611. hmacctx->base_hash = chcr_alloc_shash(digestsize);
  1612. if (IS_ERR(hmacctx->base_hash))
  1613. return PTR_ERR(hmacctx->base_hash);
  1614. return chcr_device_init(crypto_tfm_ctx(tfm));
  1615. }
  1616. static void chcr_hmac_cra_exit(struct crypto_tfm *tfm)
  1617. {
  1618. struct chcr_context *ctx = crypto_tfm_ctx(tfm);
  1619. struct hmac_ctx *hmacctx = HMAC_CTX(ctx);
  1620. if (hmacctx->base_hash) {
  1621. chcr_free_shash(hmacctx->base_hash);
  1622. hmacctx->base_hash = NULL;
  1623. }
  1624. }
  1625. static int is_newsg(struct scatterlist *sgl, unsigned int *newents)
  1626. {
  1627. int nents = 0;
  1628. int ret = 0;
  1629. while (sgl) {
  1630. if (sgl->length > CHCR_SG_SIZE)
  1631. ret = 1;
  1632. nents += DIV_ROUND_UP(sgl->length, CHCR_SG_SIZE);
  1633. sgl = sg_next(sgl);
  1634. }
  1635. *newents = nents;
  1636. return ret;
  1637. }
  1638. static inline void free_new_sg(struct scatterlist *sgl)
  1639. {
  1640. kfree(sgl);
  1641. }
  1642. static struct scatterlist *alloc_new_sg(struct scatterlist *sgl,
  1643. unsigned int nents)
  1644. {
  1645. struct scatterlist *newsg, *sg;
  1646. int i, len, processed = 0;
  1647. struct page *spage;
  1648. int offset;
  1649. newsg = kmalloc_array(nents, sizeof(struct scatterlist), GFP_KERNEL);
  1650. if (!newsg)
  1651. return ERR_PTR(-ENOMEM);
  1652. sg = newsg;
  1653. sg_init_table(sg, nents);
  1654. offset = sgl->offset;
  1655. spage = sg_page(sgl);
  1656. for (i = 0; i < nents; i++) {
  1657. len = min_t(u32, sgl->length - processed, CHCR_SG_SIZE);
  1658. sg_set_page(sg, spage, len, offset);
  1659. processed += len;
  1660. offset += len;
  1661. if (offset >= PAGE_SIZE) {
  1662. offset = offset % PAGE_SIZE;
  1663. spage++;
  1664. }
  1665. if (processed == sgl->length) {
  1666. processed = 0;
  1667. sgl = sg_next(sgl);
  1668. if (!sgl)
  1669. break;
  1670. spage = sg_page(sgl);
  1671. offset = sgl->offset;
  1672. }
  1673. sg = sg_next(sg);
  1674. }
  1675. return newsg;
  1676. }
  1677. static int chcr_copy_assoc(struct aead_request *req,
  1678. struct chcr_aead_ctx *ctx)
  1679. {
  1680. SKCIPHER_REQUEST_ON_STACK(skreq, ctx->null);
  1681. skcipher_request_set_tfm(skreq, ctx->null);
  1682. skcipher_request_set_callback(skreq, aead_request_flags(req),
  1683. NULL, NULL);
  1684. skcipher_request_set_crypt(skreq, req->src, req->dst, req->assoclen,
  1685. NULL);
  1686. return crypto_skcipher_encrypt(skreq);
  1687. }
  1688. static int chcr_aead_need_fallback(struct aead_request *req, int src_nent,
  1689. int aadmax, int wrlen,
  1690. unsigned short op_type)
  1691. {
  1692. unsigned int authsize = crypto_aead_authsize(crypto_aead_reqtfm(req));
  1693. if (((req->cryptlen - (op_type ? authsize : 0)) == 0) ||
  1694. (req->assoclen > aadmax) ||
  1695. (src_nent > MAX_SKB_FRAGS) ||
  1696. (wrlen > MAX_WR_SIZE))
  1697. return 1;
  1698. return 0;
  1699. }
  1700. static int chcr_aead_fallback(struct aead_request *req, unsigned short op_type)
  1701. {
  1702. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1703. struct chcr_context *ctx = crypto_aead_ctx(tfm);
  1704. struct chcr_aead_ctx *aeadctx = AEAD_CTX(ctx);
  1705. struct aead_request *subreq = aead_request_ctx(req);
  1706. aead_request_set_tfm(subreq, aeadctx->sw_cipher);
  1707. aead_request_set_callback(subreq, req->base.flags,
  1708. req->base.complete, req->base.data);
  1709. aead_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
  1710. req->iv);
  1711. aead_request_set_ad(subreq, req->assoclen);
  1712. return op_type ? crypto_aead_decrypt(subreq) :
  1713. crypto_aead_encrypt(subreq);
  1714. }
  1715. static struct sk_buff *create_authenc_wr(struct aead_request *req,
  1716. unsigned short qid,
  1717. int size,
  1718. unsigned short op_type)
  1719. {
  1720. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1721. struct chcr_context *ctx = crypto_aead_ctx(tfm);
  1722. struct uld_ctx *u_ctx = ULD_CTX(ctx);
  1723. struct chcr_aead_ctx *aeadctx = AEAD_CTX(ctx);
  1724. struct chcr_authenc_ctx *actx = AUTHENC_CTX(aeadctx);
  1725. struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
  1726. struct sk_buff *skb = NULL;
  1727. struct chcr_wr *chcr_req;
  1728. struct cpl_rx_phys_dsgl *phys_cpl;
  1729. struct phys_sge_parm sg_param;
  1730. struct scatterlist *src;
  1731. unsigned int frags = 0, transhdr_len;
  1732. unsigned int ivsize = crypto_aead_ivsize(tfm), dst_size = 0;
  1733. unsigned int kctx_len = 0, nents;
  1734. unsigned short stop_offset = 0;
  1735. unsigned int assoclen = req->assoclen;
  1736. unsigned int authsize = crypto_aead_authsize(tfm);
  1737. int error = -EINVAL, src_nent;
  1738. int null = 0;
  1739. gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
  1740. GFP_ATOMIC;
  1741. struct adapter *adap = padap(ctx->dev);
  1742. reqctx->newdstsg = NULL;
  1743. dst_size = req->assoclen + req->cryptlen + (op_type ? -authsize :
  1744. authsize);
  1745. if (aeadctx->enckey_len == 0 || (req->cryptlen <= 0))
  1746. goto err;
  1747. if (op_type && req->cryptlen < crypto_aead_authsize(tfm))
  1748. goto err;
  1749. src_nent = sg_nents_for_len(req->src, req->assoclen + req->cryptlen);
  1750. if (src_nent < 0)
  1751. goto err;
  1752. src = scatterwalk_ffwd(reqctx->srcffwd, req->src, req->assoclen);
  1753. if (req->src != req->dst) {
  1754. error = chcr_copy_assoc(req, aeadctx);
  1755. if (error)
  1756. return ERR_PTR(error);
  1757. }
  1758. if (dst_size && is_newsg(req->dst, &nents)) {
  1759. reqctx->newdstsg = alloc_new_sg(req->dst, nents);
  1760. if (IS_ERR(reqctx->newdstsg))
  1761. return ERR_CAST(reqctx->newdstsg);
  1762. reqctx->dst = scatterwalk_ffwd(reqctx->dstffwd,
  1763. reqctx->newdstsg, req->assoclen);
  1764. } else {
  1765. if (req->src == req->dst)
  1766. reqctx->dst = src;
  1767. else
  1768. reqctx->dst = scatterwalk_ffwd(reqctx->dstffwd,
  1769. req->dst, req->assoclen);
  1770. }
  1771. if (get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_NULL) {
  1772. null = 1;
  1773. assoclen = 0;
  1774. }
  1775. reqctx->dst_nents = sg_nents_for_len(reqctx->dst, req->cryptlen +
  1776. (op_type ? -authsize : authsize));
  1777. if (reqctx->dst_nents < 0) {
  1778. pr_err("AUTHENC:Invalid Destination sg entries\n");
  1779. error = -EINVAL;
  1780. goto err;
  1781. }
  1782. dst_size = get_space_for_phys_dsgl(reqctx->dst_nents);
  1783. kctx_len = (ntohl(KEY_CONTEXT_CTX_LEN_V(aeadctx->key_ctx_hdr)) << 4)
  1784. - sizeof(chcr_req->key_ctx);
  1785. transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, dst_size);
  1786. if (chcr_aead_need_fallback(req, src_nent + MIN_AUTH_SG,
  1787. T6_MAX_AAD_SIZE,
  1788. transhdr_len + (sgl_len(src_nent + MIN_AUTH_SG) * 8),
  1789. op_type)) {
  1790. atomic_inc(&adap->chcr_stats.fallback);
  1791. free_new_sg(reqctx->newdstsg);
  1792. reqctx->newdstsg = NULL;
  1793. return ERR_PTR(chcr_aead_fallback(req, op_type));
  1794. }
  1795. skb = alloc_skb((transhdr_len + sizeof(struct sge_opaque_hdr)), flags);
  1796. if (!skb) {
  1797. error = -ENOMEM;
  1798. goto err;
  1799. }
  1800. /* LLD is going to write the sge hdr. */
  1801. skb_reserve(skb, sizeof(struct sge_opaque_hdr));
  1802. /* Write WR */
  1803. chcr_req = __skb_put_zero(skb, transhdr_len);
  1804. stop_offset = (op_type == CHCR_ENCRYPT_OP) ? 0 : authsize;
  1805. /*
  1806. * Input order is AAD,IV and Payload. where IV should be included as
  1807. * the part of authdata. All other fields should be filled according
  1808. * to the hardware spec
  1809. */
  1810. chcr_req->sec_cpl.op_ivinsrtofst =
  1811. FILL_SEC_CPL_OP_IVINSR(ctx->dev->rx_channel_id, 2,
  1812. (ivsize ? (assoclen + 1) : 0));
  1813. chcr_req->sec_cpl.pldlen = htonl(assoclen + ivsize + req->cryptlen);
  1814. chcr_req->sec_cpl.aadstart_cipherstop_hi = FILL_SEC_CPL_CIPHERSTOP_HI(
  1815. assoclen ? 1 : 0, assoclen,
  1816. assoclen + ivsize + 1,
  1817. (stop_offset & 0x1F0) >> 4);
  1818. chcr_req->sec_cpl.cipherstop_lo_authinsert = FILL_SEC_CPL_AUTHINSERT(
  1819. stop_offset & 0xF,
  1820. null ? 0 : assoclen + ivsize + 1,
  1821. stop_offset, stop_offset);
  1822. chcr_req->sec_cpl.seqno_numivs = FILL_SEC_CPL_SCMD0_SEQNO(op_type,
  1823. (op_type == CHCR_ENCRYPT_OP) ? 1 : 0,
  1824. CHCR_SCMD_CIPHER_MODE_AES_CBC,
  1825. actx->auth_mode, aeadctx->hmac_ctrl,
  1826. ivsize >> 1);
  1827. chcr_req->sec_cpl.ivgen_hdrlen = FILL_SEC_CPL_IVGEN_HDRLEN(0, 0, 1,
  1828. 0, 1, dst_size);
  1829. chcr_req->key_ctx.ctx_hdr = aeadctx->key_ctx_hdr;
  1830. if (op_type == CHCR_ENCRYPT_OP)
  1831. memcpy(chcr_req->key_ctx.key, aeadctx->key,
  1832. aeadctx->enckey_len);
  1833. else
  1834. memcpy(chcr_req->key_ctx.key, actx->dec_rrkey,
  1835. aeadctx->enckey_len);
  1836. memcpy(chcr_req->key_ctx.key + (DIV_ROUND_UP(aeadctx->enckey_len, 16) <<
  1837. 4), actx->h_iopad, kctx_len -
  1838. (DIV_ROUND_UP(aeadctx->enckey_len, 16) << 4));
  1839. phys_cpl = (struct cpl_rx_phys_dsgl *)((u8 *)(chcr_req + 1) + kctx_len);
  1840. sg_param.nents = reqctx->dst_nents;
  1841. sg_param.obsize = req->cryptlen + (op_type ? -authsize : authsize);
  1842. sg_param.qid = qid;
  1843. error = map_writesg_phys_cpl(&u_ctx->lldi.pdev->dev, phys_cpl,
  1844. reqctx->dst, &sg_param);
  1845. if (error)
  1846. goto dstmap_fail;
  1847. skb_set_transport_header(skb, transhdr_len);
  1848. if (assoclen) {
  1849. /* AAD buffer in */
  1850. write_sg_to_skb(skb, &frags, req->src, assoclen);
  1851. }
  1852. write_buffer_to_skb(skb, &frags, req->iv, ivsize);
  1853. write_sg_to_skb(skb, &frags, src, req->cryptlen);
  1854. atomic_inc(&adap->chcr_stats.cipher_rqst);
  1855. create_wreq(ctx, chcr_req, &req->base, skb, kctx_len, size, 1,
  1856. sizeof(struct cpl_rx_phys_dsgl) + dst_size, 0);
  1857. reqctx->skb = skb;
  1858. skb_get(skb);
  1859. return skb;
  1860. dstmap_fail:
  1861. /* ivmap_fail: */
  1862. kfree_skb(skb);
  1863. err:
  1864. free_new_sg(reqctx->newdstsg);
  1865. reqctx->newdstsg = NULL;
  1866. return ERR_PTR(error);
  1867. }
  1868. static int set_msg_len(u8 *block, unsigned int msglen, int csize)
  1869. {
  1870. __be32 data;
  1871. memset(block, 0, csize);
  1872. block += csize;
  1873. if (csize >= 4)
  1874. csize = 4;
  1875. else if (msglen > (unsigned int)(1 << (8 * csize)))
  1876. return -EOVERFLOW;
  1877. data = cpu_to_be32(msglen);
  1878. memcpy(block - csize, (u8 *)&data + 4 - csize, csize);
  1879. return 0;
  1880. }
  1881. static void generate_b0(struct aead_request *req,
  1882. struct chcr_aead_ctx *aeadctx,
  1883. unsigned short op_type)
  1884. {
  1885. unsigned int l, lp, m;
  1886. int rc;
  1887. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  1888. struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
  1889. u8 *b0 = reqctx->scratch_pad;
  1890. m = crypto_aead_authsize(aead);
  1891. memcpy(b0, reqctx->iv, 16);
  1892. lp = b0[0];
  1893. l = lp + 1;
  1894. /* set m, bits 3-5 */
  1895. *b0 |= (8 * ((m - 2) / 2));
  1896. /* set adata, bit 6, if associated data is used */
  1897. if (req->assoclen)
  1898. *b0 |= 64;
  1899. rc = set_msg_len(b0 + 16 - l,
  1900. (op_type == CHCR_DECRYPT_OP) ?
  1901. req->cryptlen - m : req->cryptlen, l);
  1902. }
  1903. static inline int crypto_ccm_check_iv(const u8 *iv)
  1904. {
  1905. /* 2 <= L <= 8, so 1 <= L' <= 7. */
  1906. if (iv[0] < 1 || iv[0] > 7)
  1907. return -EINVAL;
  1908. return 0;
  1909. }
  1910. static int ccm_format_packet(struct aead_request *req,
  1911. struct chcr_aead_ctx *aeadctx,
  1912. unsigned int sub_type,
  1913. unsigned short op_type)
  1914. {
  1915. struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
  1916. int rc = 0;
  1917. if (sub_type == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309) {
  1918. reqctx->iv[0] = 3;
  1919. memcpy(reqctx->iv + 1, &aeadctx->salt[0], 3);
  1920. memcpy(reqctx->iv + 4, req->iv, 8);
  1921. memset(reqctx->iv + 12, 0, 4);
  1922. *((unsigned short *)(reqctx->scratch_pad + 16)) =
  1923. htons(req->assoclen - 8);
  1924. } else {
  1925. memcpy(reqctx->iv, req->iv, 16);
  1926. *((unsigned short *)(reqctx->scratch_pad + 16)) =
  1927. htons(req->assoclen);
  1928. }
  1929. generate_b0(req, aeadctx, op_type);
  1930. /* zero the ctr value */
  1931. memset(reqctx->iv + 15 - reqctx->iv[0], 0, reqctx->iv[0] + 1);
  1932. return rc;
  1933. }
  1934. static void fill_sec_cpl_for_aead(struct cpl_tx_sec_pdu *sec_cpl,
  1935. unsigned int dst_size,
  1936. struct aead_request *req,
  1937. unsigned short op_type,
  1938. struct chcr_context *chcrctx)
  1939. {
  1940. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1941. struct chcr_aead_ctx *aeadctx = AEAD_CTX(crypto_aead_ctx(tfm));
  1942. unsigned int ivsize = AES_BLOCK_SIZE;
  1943. unsigned int cipher_mode = CHCR_SCMD_CIPHER_MODE_AES_CCM;
  1944. unsigned int mac_mode = CHCR_SCMD_AUTH_MODE_CBCMAC;
  1945. unsigned int c_id = chcrctx->dev->rx_channel_id;
  1946. unsigned int ccm_xtra;
  1947. unsigned char tag_offset = 0, auth_offset = 0;
  1948. unsigned int assoclen;
  1949. if (get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309)
  1950. assoclen = req->assoclen - 8;
  1951. else
  1952. assoclen = req->assoclen;
  1953. ccm_xtra = CCM_B0_SIZE +
  1954. ((assoclen) ? CCM_AAD_FIELD_SIZE : 0);
  1955. auth_offset = req->cryptlen ?
  1956. (assoclen + ivsize + 1 + ccm_xtra) : 0;
  1957. if (op_type == CHCR_DECRYPT_OP) {
  1958. if (crypto_aead_authsize(tfm) != req->cryptlen)
  1959. tag_offset = crypto_aead_authsize(tfm);
  1960. else
  1961. auth_offset = 0;
  1962. }
  1963. sec_cpl->op_ivinsrtofst = FILL_SEC_CPL_OP_IVINSR(c_id,
  1964. 2, (ivsize ? (assoclen + 1) : 0) +
  1965. ccm_xtra);
  1966. sec_cpl->pldlen =
  1967. htonl(assoclen + ivsize + req->cryptlen + ccm_xtra);
  1968. /* For CCM there wil be b0 always. So AAD start will be 1 always */
  1969. sec_cpl->aadstart_cipherstop_hi = FILL_SEC_CPL_CIPHERSTOP_HI(
  1970. 1, assoclen + ccm_xtra, assoclen
  1971. + ivsize + 1 + ccm_xtra, 0);
  1972. sec_cpl->cipherstop_lo_authinsert = FILL_SEC_CPL_AUTHINSERT(0,
  1973. auth_offset, tag_offset,
  1974. (op_type == CHCR_ENCRYPT_OP) ? 0 :
  1975. crypto_aead_authsize(tfm));
  1976. sec_cpl->seqno_numivs = FILL_SEC_CPL_SCMD0_SEQNO(op_type,
  1977. (op_type == CHCR_ENCRYPT_OP) ? 0 : 1,
  1978. cipher_mode, mac_mode,
  1979. aeadctx->hmac_ctrl, ivsize >> 1);
  1980. sec_cpl->ivgen_hdrlen = FILL_SEC_CPL_IVGEN_HDRLEN(0, 0, 1, 0,
  1981. 1, dst_size);
  1982. }
  1983. int aead_ccm_validate_input(unsigned short op_type,
  1984. struct aead_request *req,
  1985. struct chcr_aead_ctx *aeadctx,
  1986. unsigned int sub_type)
  1987. {
  1988. if (sub_type != CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309) {
  1989. if (crypto_ccm_check_iv(req->iv)) {
  1990. pr_err("CCM: IV check fails\n");
  1991. return -EINVAL;
  1992. }
  1993. } else {
  1994. if (req->assoclen != 16 && req->assoclen != 20) {
  1995. pr_err("RFC4309: Invalid AAD length %d\n",
  1996. req->assoclen);
  1997. return -EINVAL;
  1998. }
  1999. }
  2000. if (aeadctx->enckey_len == 0) {
  2001. pr_err("CCM: Encryption key not set\n");
  2002. return -EINVAL;
  2003. }
  2004. return 0;
  2005. }
  2006. unsigned int fill_aead_req_fields(struct sk_buff *skb,
  2007. struct aead_request *req,
  2008. struct scatterlist *src,
  2009. unsigned int ivsize,
  2010. struct chcr_aead_ctx *aeadctx)
  2011. {
  2012. unsigned int frags = 0;
  2013. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  2014. struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
  2015. /* b0 and aad length(if available) */
  2016. write_buffer_to_skb(skb, &frags, reqctx->scratch_pad, CCM_B0_SIZE +
  2017. (req->assoclen ? CCM_AAD_FIELD_SIZE : 0));
  2018. if (req->assoclen) {
  2019. if (get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309)
  2020. write_sg_to_skb(skb, &frags, req->src,
  2021. req->assoclen - 8);
  2022. else
  2023. write_sg_to_skb(skb, &frags, req->src, req->assoclen);
  2024. }
  2025. write_buffer_to_skb(skb, &frags, reqctx->iv, ivsize);
  2026. if (req->cryptlen)
  2027. write_sg_to_skb(skb, &frags, src, req->cryptlen);
  2028. return frags;
  2029. }
  2030. static struct sk_buff *create_aead_ccm_wr(struct aead_request *req,
  2031. unsigned short qid,
  2032. int size,
  2033. unsigned short op_type)
  2034. {
  2035. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  2036. struct chcr_context *ctx = crypto_aead_ctx(tfm);
  2037. struct uld_ctx *u_ctx = ULD_CTX(ctx);
  2038. struct chcr_aead_ctx *aeadctx = AEAD_CTX(ctx);
  2039. struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
  2040. struct sk_buff *skb = NULL;
  2041. struct chcr_wr *chcr_req;
  2042. struct cpl_rx_phys_dsgl *phys_cpl;
  2043. struct phys_sge_parm sg_param;
  2044. struct scatterlist *src;
  2045. unsigned int frags = 0, transhdr_len, ivsize = AES_BLOCK_SIZE;
  2046. unsigned int dst_size = 0, kctx_len, nents;
  2047. unsigned int sub_type;
  2048. unsigned int authsize = crypto_aead_authsize(tfm);
  2049. int error = -EINVAL, src_nent;
  2050. gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
  2051. GFP_ATOMIC;
  2052. struct adapter *adap = padap(ctx->dev);
  2053. dst_size = req->assoclen + req->cryptlen + (op_type ? -authsize :
  2054. authsize);
  2055. reqctx->newdstsg = NULL;
  2056. if (op_type && req->cryptlen < crypto_aead_authsize(tfm))
  2057. goto err;
  2058. src_nent = sg_nents_for_len(req->src, req->assoclen + req->cryptlen);
  2059. if (src_nent < 0)
  2060. goto err;
  2061. sub_type = get_aead_subtype(tfm);
  2062. src = scatterwalk_ffwd(reqctx->srcffwd, req->src, req->assoclen);
  2063. if (req->src != req->dst) {
  2064. error = chcr_copy_assoc(req, aeadctx);
  2065. if (error) {
  2066. pr_err("AAD copy to destination buffer fails\n");
  2067. return ERR_PTR(error);
  2068. }
  2069. }
  2070. if (dst_size && is_newsg(req->dst, &nents)) {
  2071. reqctx->newdstsg = alloc_new_sg(req->dst, nents);
  2072. if (IS_ERR(reqctx->newdstsg))
  2073. return ERR_CAST(reqctx->newdstsg);
  2074. reqctx->dst = scatterwalk_ffwd(reqctx->dstffwd,
  2075. reqctx->newdstsg, req->assoclen);
  2076. } else {
  2077. if (req->src == req->dst)
  2078. reqctx->dst = src;
  2079. else
  2080. reqctx->dst = scatterwalk_ffwd(reqctx->dstffwd,
  2081. req->dst, req->assoclen);
  2082. }
  2083. reqctx->dst_nents = sg_nents_for_len(reqctx->dst, req->cryptlen +
  2084. (op_type ? -authsize : authsize));
  2085. if (reqctx->dst_nents < 0) {
  2086. pr_err("CCM:Invalid Destination sg entries\n");
  2087. error = -EINVAL;
  2088. goto err;
  2089. }
  2090. error = aead_ccm_validate_input(op_type, req, aeadctx, sub_type);
  2091. if (error)
  2092. goto err;
  2093. dst_size = get_space_for_phys_dsgl(reqctx->dst_nents);
  2094. kctx_len = ((DIV_ROUND_UP(aeadctx->enckey_len, 16)) << 4) * 2;
  2095. transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, dst_size);
  2096. if (chcr_aead_need_fallback(req, src_nent + MIN_CCM_SG,
  2097. T6_MAX_AAD_SIZE - 18,
  2098. transhdr_len + (sgl_len(src_nent + MIN_CCM_SG) * 8),
  2099. op_type)) {
  2100. atomic_inc(&adap->chcr_stats.fallback);
  2101. free_new_sg(reqctx->newdstsg);
  2102. reqctx->newdstsg = NULL;
  2103. return ERR_PTR(chcr_aead_fallback(req, op_type));
  2104. }
  2105. skb = alloc_skb((transhdr_len + sizeof(struct sge_opaque_hdr)), flags);
  2106. if (!skb) {
  2107. error = -ENOMEM;
  2108. goto err;
  2109. }
  2110. skb_reserve(skb, sizeof(struct sge_opaque_hdr));
  2111. chcr_req = __skb_put_zero(skb, transhdr_len);
  2112. fill_sec_cpl_for_aead(&chcr_req->sec_cpl, dst_size, req, op_type, ctx);
  2113. chcr_req->key_ctx.ctx_hdr = aeadctx->key_ctx_hdr;
  2114. memcpy(chcr_req->key_ctx.key, aeadctx->key, aeadctx->enckey_len);
  2115. memcpy(chcr_req->key_ctx.key + (DIV_ROUND_UP(aeadctx->enckey_len, 16) *
  2116. 16), aeadctx->key, aeadctx->enckey_len);
  2117. phys_cpl = (struct cpl_rx_phys_dsgl *)((u8 *)(chcr_req + 1) + kctx_len);
  2118. error = ccm_format_packet(req, aeadctx, sub_type, op_type);
  2119. if (error)
  2120. goto dstmap_fail;
  2121. sg_param.nents = reqctx->dst_nents;
  2122. sg_param.obsize = req->cryptlen + (op_type ? -authsize : authsize);
  2123. sg_param.qid = qid;
  2124. error = map_writesg_phys_cpl(&u_ctx->lldi.pdev->dev, phys_cpl,
  2125. reqctx->dst, &sg_param);
  2126. if (error)
  2127. goto dstmap_fail;
  2128. skb_set_transport_header(skb, transhdr_len);
  2129. frags = fill_aead_req_fields(skb, req, src, ivsize, aeadctx);
  2130. atomic_inc(&adap->chcr_stats.aead_rqst);
  2131. create_wreq(ctx, chcr_req, &req->base, skb, kctx_len, 0, 1,
  2132. sizeof(struct cpl_rx_phys_dsgl) + dst_size, 0);
  2133. reqctx->skb = skb;
  2134. skb_get(skb);
  2135. return skb;
  2136. dstmap_fail:
  2137. kfree_skb(skb);
  2138. err:
  2139. free_new_sg(reqctx->newdstsg);
  2140. reqctx->newdstsg = NULL;
  2141. return ERR_PTR(error);
  2142. }
  2143. static struct sk_buff *create_gcm_wr(struct aead_request *req,
  2144. unsigned short qid,
  2145. int size,
  2146. unsigned short op_type)
  2147. {
  2148. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  2149. struct chcr_context *ctx = crypto_aead_ctx(tfm);
  2150. struct uld_ctx *u_ctx = ULD_CTX(ctx);
  2151. struct chcr_aead_ctx *aeadctx = AEAD_CTX(ctx);
  2152. struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
  2153. struct sk_buff *skb = NULL;
  2154. struct chcr_wr *chcr_req;
  2155. struct cpl_rx_phys_dsgl *phys_cpl;
  2156. struct phys_sge_parm sg_param;
  2157. struct scatterlist *src;
  2158. unsigned int frags = 0, transhdr_len;
  2159. unsigned int ivsize = AES_BLOCK_SIZE;
  2160. unsigned int dst_size = 0, kctx_len, nents, assoclen = req->assoclen;
  2161. unsigned char tag_offset = 0;
  2162. unsigned int authsize = crypto_aead_authsize(tfm);
  2163. int error = -EINVAL, src_nent;
  2164. gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
  2165. GFP_ATOMIC;
  2166. struct adapter *adap = padap(ctx->dev);
  2167. reqctx->newdstsg = NULL;
  2168. dst_size = assoclen + req->cryptlen + (op_type ? -authsize :
  2169. authsize);
  2170. /* validate key size */
  2171. if (aeadctx->enckey_len == 0)
  2172. goto err;
  2173. if (op_type && req->cryptlen < crypto_aead_authsize(tfm))
  2174. goto err;
  2175. src_nent = sg_nents_for_len(req->src, assoclen + req->cryptlen);
  2176. if (src_nent < 0)
  2177. goto err;
  2178. src = scatterwalk_ffwd(reqctx->srcffwd, req->src, assoclen);
  2179. if (req->src != req->dst) {
  2180. error = chcr_copy_assoc(req, aeadctx);
  2181. if (error)
  2182. return ERR_PTR(error);
  2183. }
  2184. if (dst_size && is_newsg(req->dst, &nents)) {
  2185. reqctx->newdstsg = alloc_new_sg(req->dst, nents);
  2186. if (IS_ERR(reqctx->newdstsg))
  2187. return ERR_CAST(reqctx->newdstsg);
  2188. reqctx->dst = scatterwalk_ffwd(reqctx->dstffwd,
  2189. reqctx->newdstsg, assoclen);
  2190. } else {
  2191. if (req->src == req->dst)
  2192. reqctx->dst = src;
  2193. else
  2194. reqctx->dst = scatterwalk_ffwd(reqctx->dstffwd,
  2195. req->dst, assoclen);
  2196. }
  2197. reqctx->dst_nents = sg_nents_for_len(reqctx->dst, req->cryptlen +
  2198. (op_type ? -authsize : authsize));
  2199. if (reqctx->dst_nents < 0) {
  2200. pr_err("GCM:Invalid Destination sg entries\n");
  2201. error = -EINVAL;
  2202. goto err;
  2203. }
  2204. dst_size = get_space_for_phys_dsgl(reqctx->dst_nents);
  2205. kctx_len = ((DIV_ROUND_UP(aeadctx->enckey_len, 16)) << 4) +
  2206. AEAD_H_SIZE;
  2207. transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, dst_size);
  2208. if (chcr_aead_need_fallback(req, src_nent + MIN_GCM_SG,
  2209. T6_MAX_AAD_SIZE,
  2210. transhdr_len + (sgl_len(src_nent + MIN_GCM_SG) * 8),
  2211. op_type)) {
  2212. atomic_inc(&adap->chcr_stats.fallback);
  2213. free_new_sg(reqctx->newdstsg);
  2214. reqctx->newdstsg = NULL;
  2215. return ERR_PTR(chcr_aead_fallback(req, op_type));
  2216. }
  2217. skb = alloc_skb((transhdr_len + sizeof(struct sge_opaque_hdr)), flags);
  2218. if (!skb) {
  2219. error = -ENOMEM;
  2220. goto err;
  2221. }
  2222. /* NIC driver is going to write the sge hdr. */
  2223. skb_reserve(skb, sizeof(struct sge_opaque_hdr));
  2224. chcr_req = __skb_put_zero(skb, transhdr_len);
  2225. if (get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106)
  2226. assoclen = req->assoclen - 8;
  2227. tag_offset = (op_type == CHCR_ENCRYPT_OP) ? 0 : authsize;
  2228. chcr_req->sec_cpl.op_ivinsrtofst = FILL_SEC_CPL_OP_IVINSR(
  2229. ctx->dev->rx_channel_id, 2, (ivsize ?
  2230. (assoclen + 1) : 0));
  2231. chcr_req->sec_cpl.pldlen =
  2232. htonl(assoclen + ivsize + req->cryptlen);
  2233. chcr_req->sec_cpl.aadstart_cipherstop_hi = FILL_SEC_CPL_CIPHERSTOP_HI(
  2234. assoclen ? 1 : 0, assoclen,
  2235. assoclen + ivsize + 1, 0);
  2236. chcr_req->sec_cpl.cipherstop_lo_authinsert =
  2237. FILL_SEC_CPL_AUTHINSERT(0, assoclen + ivsize + 1,
  2238. tag_offset, tag_offset);
  2239. chcr_req->sec_cpl.seqno_numivs =
  2240. FILL_SEC_CPL_SCMD0_SEQNO(op_type, (op_type ==
  2241. CHCR_ENCRYPT_OP) ? 1 : 0,
  2242. CHCR_SCMD_CIPHER_MODE_AES_GCM,
  2243. CHCR_SCMD_AUTH_MODE_GHASH,
  2244. aeadctx->hmac_ctrl, ivsize >> 1);
  2245. chcr_req->sec_cpl.ivgen_hdrlen = FILL_SEC_CPL_IVGEN_HDRLEN(0, 0, 1,
  2246. 0, 1, dst_size);
  2247. chcr_req->key_ctx.ctx_hdr = aeadctx->key_ctx_hdr;
  2248. memcpy(chcr_req->key_ctx.key, aeadctx->key, aeadctx->enckey_len);
  2249. memcpy(chcr_req->key_ctx.key + (DIV_ROUND_UP(aeadctx->enckey_len, 16) *
  2250. 16), GCM_CTX(aeadctx)->ghash_h, AEAD_H_SIZE);
  2251. /* prepare a 16 byte iv */
  2252. /* S A L T | IV | 0x00000001 */
  2253. if (get_aead_subtype(tfm) ==
  2254. CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106) {
  2255. memcpy(reqctx->iv, aeadctx->salt, 4);
  2256. memcpy(reqctx->iv + 4, req->iv, 8);
  2257. } else {
  2258. memcpy(reqctx->iv, req->iv, 12);
  2259. }
  2260. *((unsigned int *)(reqctx->iv + 12)) = htonl(0x01);
  2261. phys_cpl = (struct cpl_rx_phys_dsgl *)((u8 *)(chcr_req + 1) + kctx_len);
  2262. sg_param.nents = reqctx->dst_nents;
  2263. sg_param.obsize = req->cryptlen + (op_type ? -authsize : authsize);
  2264. sg_param.qid = qid;
  2265. error = map_writesg_phys_cpl(&u_ctx->lldi.pdev->dev, phys_cpl,
  2266. reqctx->dst, &sg_param);
  2267. if (error)
  2268. goto dstmap_fail;
  2269. skb_set_transport_header(skb, transhdr_len);
  2270. write_sg_to_skb(skb, &frags, req->src, assoclen);
  2271. write_buffer_to_skb(skb, &frags, reqctx->iv, ivsize);
  2272. write_sg_to_skb(skb, &frags, src, req->cryptlen);
  2273. atomic_inc(&adap->chcr_stats.aead_rqst);
  2274. create_wreq(ctx, chcr_req, &req->base, skb, kctx_len, size, 1,
  2275. sizeof(struct cpl_rx_phys_dsgl) + dst_size,
  2276. reqctx->verify);
  2277. reqctx->skb = skb;
  2278. skb_get(skb);
  2279. return skb;
  2280. dstmap_fail:
  2281. /* ivmap_fail: */
  2282. kfree_skb(skb);
  2283. err:
  2284. free_new_sg(reqctx->newdstsg);
  2285. reqctx->newdstsg = NULL;
  2286. return ERR_PTR(error);
  2287. }
  2288. static int chcr_aead_cra_init(struct crypto_aead *tfm)
  2289. {
  2290. struct chcr_context *ctx = crypto_aead_ctx(tfm);
  2291. struct chcr_aead_ctx *aeadctx = AEAD_CTX(ctx);
  2292. struct aead_alg *alg = crypto_aead_alg(tfm);
  2293. aeadctx->sw_cipher = crypto_alloc_aead(alg->base.cra_name, 0,
  2294. CRYPTO_ALG_NEED_FALLBACK |
  2295. CRYPTO_ALG_ASYNC);
  2296. if (IS_ERR(aeadctx->sw_cipher))
  2297. return PTR_ERR(aeadctx->sw_cipher);
  2298. crypto_aead_set_reqsize(tfm, max(sizeof(struct chcr_aead_reqctx),
  2299. sizeof(struct aead_request) +
  2300. crypto_aead_reqsize(aeadctx->sw_cipher)));
  2301. aeadctx->null = crypto_get_default_null_skcipher();
  2302. if (IS_ERR(aeadctx->null))
  2303. return PTR_ERR(aeadctx->null);
  2304. return chcr_device_init(ctx);
  2305. }
  2306. static void chcr_aead_cra_exit(struct crypto_aead *tfm)
  2307. {
  2308. struct chcr_context *ctx = crypto_aead_ctx(tfm);
  2309. struct chcr_aead_ctx *aeadctx = AEAD_CTX(ctx);
  2310. crypto_put_default_null_skcipher();
  2311. crypto_free_aead(aeadctx->sw_cipher);
  2312. }
  2313. static int chcr_authenc_null_setauthsize(struct crypto_aead *tfm,
  2314. unsigned int authsize)
  2315. {
  2316. struct chcr_aead_ctx *aeadctx = AEAD_CTX(crypto_aead_ctx(tfm));
  2317. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NOP;
  2318. aeadctx->mayverify = VERIFY_HW;
  2319. return crypto_aead_setauthsize(aeadctx->sw_cipher, authsize);
  2320. }
  2321. static int chcr_authenc_setauthsize(struct crypto_aead *tfm,
  2322. unsigned int authsize)
  2323. {
  2324. struct chcr_aead_ctx *aeadctx = AEAD_CTX(crypto_aead_ctx(tfm));
  2325. u32 maxauth = crypto_aead_maxauthsize(tfm);
  2326. /*SHA1 authsize in ipsec is 12 instead of 10 i.e maxauthsize / 2 is not
  2327. * true for sha1. authsize == 12 condition should be before
  2328. * authsize == (maxauth >> 1)
  2329. */
  2330. if (authsize == ICV_4) {
  2331. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL1;
  2332. aeadctx->mayverify = VERIFY_HW;
  2333. } else if (authsize == ICV_6) {
  2334. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL2;
  2335. aeadctx->mayverify = VERIFY_HW;
  2336. } else if (authsize == ICV_10) {
  2337. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_TRUNC_RFC4366;
  2338. aeadctx->mayverify = VERIFY_HW;
  2339. } else if (authsize == ICV_12) {
  2340. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_IPSEC_96BIT;
  2341. aeadctx->mayverify = VERIFY_HW;
  2342. } else if (authsize == ICV_14) {
  2343. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL3;
  2344. aeadctx->mayverify = VERIFY_HW;
  2345. } else if (authsize == (maxauth >> 1)) {
  2346. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_DIV2;
  2347. aeadctx->mayverify = VERIFY_HW;
  2348. } else if (authsize == maxauth) {
  2349. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
  2350. aeadctx->mayverify = VERIFY_HW;
  2351. } else {
  2352. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
  2353. aeadctx->mayverify = VERIFY_SW;
  2354. }
  2355. return crypto_aead_setauthsize(aeadctx->sw_cipher, authsize);
  2356. }
  2357. static int chcr_gcm_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
  2358. {
  2359. struct chcr_aead_ctx *aeadctx = AEAD_CTX(crypto_aead_ctx(tfm));
  2360. switch (authsize) {
  2361. case ICV_4:
  2362. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL1;
  2363. aeadctx->mayverify = VERIFY_HW;
  2364. break;
  2365. case ICV_8:
  2366. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_DIV2;
  2367. aeadctx->mayverify = VERIFY_HW;
  2368. break;
  2369. case ICV_12:
  2370. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_IPSEC_96BIT;
  2371. aeadctx->mayverify = VERIFY_HW;
  2372. break;
  2373. case ICV_14:
  2374. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL3;
  2375. aeadctx->mayverify = VERIFY_HW;
  2376. break;
  2377. case ICV_16:
  2378. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
  2379. aeadctx->mayverify = VERIFY_HW;
  2380. break;
  2381. case ICV_13:
  2382. case ICV_15:
  2383. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
  2384. aeadctx->mayverify = VERIFY_SW;
  2385. break;
  2386. default:
  2387. crypto_tfm_set_flags((struct crypto_tfm *) tfm,
  2388. CRYPTO_TFM_RES_BAD_KEY_LEN);
  2389. return -EINVAL;
  2390. }
  2391. return crypto_aead_setauthsize(aeadctx->sw_cipher, authsize);
  2392. }
  2393. static int chcr_4106_4309_setauthsize(struct crypto_aead *tfm,
  2394. unsigned int authsize)
  2395. {
  2396. struct chcr_aead_ctx *aeadctx = AEAD_CTX(crypto_aead_ctx(tfm));
  2397. switch (authsize) {
  2398. case ICV_8:
  2399. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_DIV2;
  2400. aeadctx->mayverify = VERIFY_HW;
  2401. break;
  2402. case ICV_12:
  2403. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_IPSEC_96BIT;
  2404. aeadctx->mayverify = VERIFY_HW;
  2405. break;
  2406. case ICV_16:
  2407. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
  2408. aeadctx->mayverify = VERIFY_HW;
  2409. break;
  2410. default:
  2411. crypto_tfm_set_flags((struct crypto_tfm *)tfm,
  2412. CRYPTO_TFM_RES_BAD_KEY_LEN);
  2413. return -EINVAL;
  2414. }
  2415. return crypto_aead_setauthsize(aeadctx->sw_cipher, authsize);
  2416. }
  2417. static int chcr_ccm_setauthsize(struct crypto_aead *tfm,
  2418. unsigned int authsize)
  2419. {
  2420. struct chcr_aead_ctx *aeadctx = AEAD_CTX(crypto_aead_ctx(tfm));
  2421. switch (authsize) {
  2422. case ICV_4:
  2423. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL1;
  2424. aeadctx->mayverify = VERIFY_HW;
  2425. break;
  2426. case ICV_6:
  2427. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL2;
  2428. aeadctx->mayverify = VERIFY_HW;
  2429. break;
  2430. case ICV_8:
  2431. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_DIV2;
  2432. aeadctx->mayverify = VERIFY_HW;
  2433. break;
  2434. case ICV_10:
  2435. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_TRUNC_RFC4366;
  2436. aeadctx->mayverify = VERIFY_HW;
  2437. break;
  2438. case ICV_12:
  2439. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_IPSEC_96BIT;
  2440. aeadctx->mayverify = VERIFY_HW;
  2441. break;
  2442. case ICV_14:
  2443. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL3;
  2444. aeadctx->mayverify = VERIFY_HW;
  2445. break;
  2446. case ICV_16:
  2447. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
  2448. aeadctx->mayverify = VERIFY_HW;
  2449. break;
  2450. default:
  2451. crypto_tfm_set_flags((struct crypto_tfm *)tfm,
  2452. CRYPTO_TFM_RES_BAD_KEY_LEN);
  2453. return -EINVAL;
  2454. }
  2455. return crypto_aead_setauthsize(aeadctx->sw_cipher, authsize);
  2456. }
  2457. static int chcr_ccm_common_setkey(struct crypto_aead *aead,
  2458. const u8 *key,
  2459. unsigned int keylen)
  2460. {
  2461. struct chcr_context *ctx = crypto_aead_ctx(aead);
  2462. struct chcr_aead_ctx *aeadctx = AEAD_CTX(ctx);
  2463. unsigned char ck_size, mk_size;
  2464. int key_ctx_size = 0;
  2465. key_ctx_size = sizeof(struct _key_ctx) +
  2466. ((DIV_ROUND_UP(keylen, 16)) << 4) * 2;
  2467. if (keylen == AES_KEYSIZE_128) {
  2468. mk_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
  2469. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
  2470. } else if (keylen == AES_KEYSIZE_192) {
  2471. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
  2472. mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_192;
  2473. } else if (keylen == AES_KEYSIZE_256) {
  2474. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
  2475. mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_256;
  2476. } else {
  2477. crypto_tfm_set_flags((struct crypto_tfm *)aead,
  2478. CRYPTO_TFM_RES_BAD_KEY_LEN);
  2479. aeadctx->enckey_len = 0;
  2480. return -EINVAL;
  2481. }
  2482. aeadctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, mk_size, 0, 0,
  2483. key_ctx_size >> 4);
  2484. memcpy(aeadctx->key, key, keylen);
  2485. aeadctx->enckey_len = keylen;
  2486. return 0;
  2487. }
  2488. static int chcr_aead_ccm_setkey(struct crypto_aead *aead,
  2489. const u8 *key,
  2490. unsigned int keylen)
  2491. {
  2492. struct chcr_context *ctx = crypto_aead_ctx(aead);
  2493. struct chcr_aead_ctx *aeadctx = AEAD_CTX(ctx);
  2494. int error;
  2495. crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK);
  2496. crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(aead) &
  2497. CRYPTO_TFM_REQ_MASK);
  2498. error = crypto_aead_setkey(aeadctx->sw_cipher, key, keylen);
  2499. crypto_aead_clear_flags(aead, CRYPTO_TFM_RES_MASK);
  2500. crypto_aead_set_flags(aead, crypto_aead_get_flags(aeadctx->sw_cipher) &
  2501. CRYPTO_TFM_RES_MASK);
  2502. if (error)
  2503. return error;
  2504. return chcr_ccm_common_setkey(aead, key, keylen);
  2505. }
  2506. static int chcr_aead_rfc4309_setkey(struct crypto_aead *aead, const u8 *key,
  2507. unsigned int keylen)
  2508. {
  2509. struct chcr_context *ctx = crypto_aead_ctx(aead);
  2510. struct chcr_aead_ctx *aeadctx = AEAD_CTX(ctx);
  2511. int error;
  2512. if (keylen < 3) {
  2513. crypto_tfm_set_flags((struct crypto_tfm *)aead,
  2514. CRYPTO_TFM_RES_BAD_KEY_LEN);
  2515. aeadctx->enckey_len = 0;
  2516. return -EINVAL;
  2517. }
  2518. crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK);
  2519. crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(aead) &
  2520. CRYPTO_TFM_REQ_MASK);
  2521. error = crypto_aead_setkey(aeadctx->sw_cipher, key, keylen);
  2522. crypto_aead_clear_flags(aead, CRYPTO_TFM_RES_MASK);
  2523. crypto_aead_set_flags(aead, crypto_aead_get_flags(aeadctx->sw_cipher) &
  2524. CRYPTO_TFM_RES_MASK);
  2525. if (error)
  2526. return error;
  2527. keylen -= 3;
  2528. memcpy(aeadctx->salt, key + keylen, 3);
  2529. return chcr_ccm_common_setkey(aead, key, keylen);
  2530. }
  2531. static int chcr_gcm_setkey(struct crypto_aead *aead, const u8 *key,
  2532. unsigned int keylen)
  2533. {
  2534. struct chcr_context *ctx = crypto_aead_ctx(aead);
  2535. struct chcr_aead_ctx *aeadctx = AEAD_CTX(ctx);
  2536. struct chcr_gcm_ctx *gctx = GCM_CTX(aeadctx);
  2537. struct crypto_cipher *cipher;
  2538. unsigned int ck_size;
  2539. int ret = 0, key_ctx_size = 0;
  2540. aeadctx->enckey_len = 0;
  2541. crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK);
  2542. crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(aead)
  2543. & CRYPTO_TFM_REQ_MASK);
  2544. ret = crypto_aead_setkey(aeadctx->sw_cipher, key, keylen);
  2545. crypto_aead_clear_flags(aead, CRYPTO_TFM_RES_MASK);
  2546. crypto_aead_set_flags(aead, crypto_aead_get_flags(aeadctx->sw_cipher) &
  2547. CRYPTO_TFM_RES_MASK);
  2548. if (ret)
  2549. goto out;
  2550. if (get_aead_subtype(aead) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106 &&
  2551. keylen > 3) {
  2552. keylen -= 4; /* nonce/salt is present in the last 4 bytes */
  2553. memcpy(aeadctx->salt, key + keylen, 4);
  2554. }
  2555. if (keylen == AES_KEYSIZE_128) {
  2556. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
  2557. } else if (keylen == AES_KEYSIZE_192) {
  2558. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
  2559. } else if (keylen == AES_KEYSIZE_256) {
  2560. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
  2561. } else {
  2562. crypto_tfm_set_flags((struct crypto_tfm *)aead,
  2563. CRYPTO_TFM_RES_BAD_KEY_LEN);
  2564. pr_err("GCM: Invalid key length %d\n", keylen);
  2565. ret = -EINVAL;
  2566. goto out;
  2567. }
  2568. memcpy(aeadctx->key, key, keylen);
  2569. aeadctx->enckey_len = keylen;
  2570. key_ctx_size = sizeof(struct _key_ctx) +
  2571. ((DIV_ROUND_UP(keylen, 16)) << 4) +
  2572. AEAD_H_SIZE;
  2573. aeadctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size,
  2574. CHCR_KEYCTX_MAC_KEY_SIZE_128,
  2575. 0, 0,
  2576. key_ctx_size >> 4);
  2577. /* Calculate the H = CIPH(K, 0 repeated 16 times).
  2578. * It will go in key context
  2579. */
  2580. cipher = crypto_alloc_cipher("aes-generic", 0, 0);
  2581. if (IS_ERR(cipher)) {
  2582. aeadctx->enckey_len = 0;
  2583. ret = -ENOMEM;
  2584. goto out;
  2585. }
  2586. ret = crypto_cipher_setkey(cipher, key, keylen);
  2587. if (ret) {
  2588. aeadctx->enckey_len = 0;
  2589. goto out1;
  2590. }
  2591. memset(gctx->ghash_h, 0, AEAD_H_SIZE);
  2592. crypto_cipher_encrypt_one(cipher, gctx->ghash_h, gctx->ghash_h);
  2593. out1:
  2594. crypto_free_cipher(cipher);
  2595. out:
  2596. return ret;
  2597. }
  2598. static int chcr_authenc_setkey(struct crypto_aead *authenc, const u8 *key,
  2599. unsigned int keylen)
  2600. {
  2601. struct chcr_context *ctx = crypto_aead_ctx(authenc);
  2602. struct chcr_aead_ctx *aeadctx = AEAD_CTX(ctx);
  2603. struct chcr_authenc_ctx *actx = AUTHENC_CTX(aeadctx);
  2604. /* it contains auth and cipher key both*/
  2605. struct crypto_authenc_keys keys;
  2606. unsigned int bs;
  2607. unsigned int max_authsize = crypto_aead_alg(authenc)->maxauthsize;
  2608. int err = 0, i, key_ctx_len = 0;
  2609. unsigned char ck_size = 0;
  2610. unsigned char pad[CHCR_HASH_MAX_BLOCK_SIZE_128] = { 0 };
  2611. struct crypto_shash *base_hash = ERR_PTR(-EINVAL);
  2612. struct algo_param param;
  2613. int align;
  2614. u8 *o_ptr = NULL;
  2615. crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK);
  2616. crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(authenc)
  2617. & CRYPTO_TFM_REQ_MASK);
  2618. err = crypto_aead_setkey(aeadctx->sw_cipher, key, keylen);
  2619. crypto_aead_clear_flags(authenc, CRYPTO_TFM_RES_MASK);
  2620. crypto_aead_set_flags(authenc, crypto_aead_get_flags(aeadctx->sw_cipher)
  2621. & CRYPTO_TFM_RES_MASK);
  2622. if (err)
  2623. goto out;
  2624. if (crypto_authenc_extractkeys(&keys, key, keylen) != 0) {
  2625. crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
  2626. goto out;
  2627. }
  2628. if (get_alg_config(&param, max_authsize)) {
  2629. pr_err("chcr : Unsupported digest size\n");
  2630. goto out;
  2631. }
  2632. if (keys.enckeylen == AES_KEYSIZE_128) {
  2633. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
  2634. } else if (keys.enckeylen == AES_KEYSIZE_192) {
  2635. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
  2636. } else if (keys.enckeylen == AES_KEYSIZE_256) {
  2637. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
  2638. } else {
  2639. pr_err("chcr : Unsupported cipher key\n");
  2640. goto out;
  2641. }
  2642. /* Copy only encryption key. We use authkey to generate h(ipad) and
  2643. * h(opad) so authkey is not needed again. authkeylen size have the
  2644. * size of the hash digest size.
  2645. */
  2646. memcpy(aeadctx->key, keys.enckey, keys.enckeylen);
  2647. aeadctx->enckey_len = keys.enckeylen;
  2648. get_aes_decrypt_key(actx->dec_rrkey, aeadctx->key,
  2649. aeadctx->enckey_len << 3);
  2650. base_hash = chcr_alloc_shash(max_authsize);
  2651. if (IS_ERR(base_hash)) {
  2652. pr_err("chcr : Base driver cannot be loaded\n");
  2653. aeadctx->enckey_len = 0;
  2654. return -EINVAL;
  2655. }
  2656. {
  2657. SHASH_DESC_ON_STACK(shash, base_hash);
  2658. shash->tfm = base_hash;
  2659. shash->flags = crypto_shash_get_flags(base_hash);
  2660. bs = crypto_shash_blocksize(base_hash);
  2661. align = KEYCTX_ALIGN_PAD(max_authsize);
  2662. o_ptr = actx->h_iopad + param.result_size + align;
  2663. if (keys.authkeylen > bs) {
  2664. err = crypto_shash_digest(shash, keys.authkey,
  2665. keys.authkeylen,
  2666. o_ptr);
  2667. if (err) {
  2668. pr_err("chcr : Base driver cannot be loaded\n");
  2669. goto out;
  2670. }
  2671. keys.authkeylen = max_authsize;
  2672. } else
  2673. memcpy(o_ptr, keys.authkey, keys.authkeylen);
  2674. /* Compute the ipad-digest*/
  2675. memset(pad + keys.authkeylen, 0, bs - keys.authkeylen);
  2676. memcpy(pad, o_ptr, keys.authkeylen);
  2677. for (i = 0; i < bs >> 2; i++)
  2678. *((unsigned int *)pad + i) ^= IPAD_DATA;
  2679. if (chcr_compute_partial_hash(shash, pad, actx->h_iopad,
  2680. max_authsize))
  2681. goto out;
  2682. /* Compute the opad-digest */
  2683. memset(pad + keys.authkeylen, 0, bs - keys.authkeylen);
  2684. memcpy(pad, o_ptr, keys.authkeylen);
  2685. for (i = 0; i < bs >> 2; i++)
  2686. *((unsigned int *)pad + i) ^= OPAD_DATA;
  2687. if (chcr_compute_partial_hash(shash, pad, o_ptr, max_authsize))
  2688. goto out;
  2689. /* convert the ipad and opad digest to network order */
  2690. chcr_change_order(actx->h_iopad, param.result_size);
  2691. chcr_change_order(o_ptr, param.result_size);
  2692. key_ctx_len = sizeof(struct _key_ctx) +
  2693. ((DIV_ROUND_UP(keys.enckeylen, 16)) << 4) +
  2694. (param.result_size + align) * 2;
  2695. aeadctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, param.mk_size,
  2696. 0, 1, key_ctx_len >> 4);
  2697. actx->auth_mode = param.auth_mode;
  2698. chcr_free_shash(base_hash);
  2699. return 0;
  2700. }
  2701. out:
  2702. aeadctx->enckey_len = 0;
  2703. if (!IS_ERR(base_hash))
  2704. chcr_free_shash(base_hash);
  2705. return -EINVAL;
  2706. }
  2707. static int chcr_aead_digest_null_setkey(struct crypto_aead *authenc,
  2708. const u8 *key, unsigned int keylen)
  2709. {
  2710. struct chcr_context *ctx = crypto_aead_ctx(authenc);
  2711. struct chcr_aead_ctx *aeadctx = AEAD_CTX(ctx);
  2712. struct chcr_authenc_ctx *actx = AUTHENC_CTX(aeadctx);
  2713. struct crypto_authenc_keys keys;
  2714. int err;
  2715. /* it contains auth and cipher key both*/
  2716. int key_ctx_len = 0;
  2717. unsigned char ck_size = 0;
  2718. crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK);
  2719. crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(authenc)
  2720. & CRYPTO_TFM_REQ_MASK);
  2721. err = crypto_aead_setkey(aeadctx->sw_cipher, key, keylen);
  2722. crypto_aead_clear_flags(authenc, CRYPTO_TFM_RES_MASK);
  2723. crypto_aead_set_flags(authenc, crypto_aead_get_flags(aeadctx->sw_cipher)
  2724. & CRYPTO_TFM_RES_MASK);
  2725. if (err)
  2726. goto out;
  2727. if (crypto_authenc_extractkeys(&keys, key, keylen) != 0) {
  2728. crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
  2729. goto out;
  2730. }
  2731. if (keys.enckeylen == AES_KEYSIZE_128) {
  2732. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
  2733. } else if (keys.enckeylen == AES_KEYSIZE_192) {
  2734. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
  2735. } else if (keys.enckeylen == AES_KEYSIZE_256) {
  2736. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
  2737. } else {
  2738. pr_err("chcr : Unsupported cipher key\n");
  2739. goto out;
  2740. }
  2741. memcpy(aeadctx->key, keys.enckey, keys.enckeylen);
  2742. aeadctx->enckey_len = keys.enckeylen;
  2743. get_aes_decrypt_key(actx->dec_rrkey, aeadctx->key,
  2744. aeadctx->enckey_len << 3);
  2745. key_ctx_len = sizeof(struct _key_ctx)
  2746. + ((DIV_ROUND_UP(keys.enckeylen, 16)) << 4);
  2747. aeadctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, CHCR_KEYCTX_NO_KEY, 0,
  2748. 0, key_ctx_len >> 4);
  2749. actx->auth_mode = CHCR_SCMD_AUTH_MODE_NOP;
  2750. return 0;
  2751. out:
  2752. aeadctx->enckey_len = 0;
  2753. return -EINVAL;
  2754. }
  2755. static int chcr_aead_encrypt(struct aead_request *req)
  2756. {
  2757. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  2758. struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
  2759. reqctx->verify = VERIFY_HW;
  2760. switch (get_aead_subtype(tfm)) {
  2761. case CRYPTO_ALG_SUB_TYPE_AEAD_AUTHENC:
  2762. case CRYPTO_ALG_SUB_TYPE_AEAD_NULL:
  2763. return chcr_aead_op(req, CHCR_ENCRYPT_OP, 0,
  2764. create_authenc_wr);
  2765. case CRYPTO_ALG_SUB_TYPE_AEAD_CCM:
  2766. case CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309:
  2767. return chcr_aead_op(req, CHCR_ENCRYPT_OP, 0,
  2768. create_aead_ccm_wr);
  2769. default:
  2770. return chcr_aead_op(req, CHCR_ENCRYPT_OP, 0,
  2771. create_gcm_wr);
  2772. }
  2773. }
  2774. static int chcr_aead_decrypt(struct aead_request *req)
  2775. {
  2776. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  2777. struct chcr_aead_ctx *aeadctx = AEAD_CTX(crypto_aead_ctx(tfm));
  2778. struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
  2779. int size;
  2780. if (aeadctx->mayverify == VERIFY_SW) {
  2781. size = crypto_aead_maxauthsize(tfm);
  2782. reqctx->verify = VERIFY_SW;
  2783. } else {
  2784. size = 0;
  2785. reqctx->verify = VERIFY_HW;
  2786. }
  2787. switch (get_aead_subtype(tfm)) {
  2788. case CRYPTO_ALG_SUB_TYPE_AEAD_AUTHENC:
  2789. case CRYPTO_ALG_SUB_TYPE_AEAD_NULL:
  2790. return chcr_aead_op(req, CHCR_DECRYPT_OP, size,
  2791. create_authenc_wr);
  2792. case CRYPTO_ALG_SUB_TYPE_AEAD_CCM:
  2793. case CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309:
  2794. return chcr_aead_op(req, CHCR_DECRYPT_OP, size,
  2795. create_aead_ccm_wr);
  2796. default:
  2797. return chcr_aead_op(req, CHCR_DECRYPT_OP, size,
  2798. create_gcm_wr);
  2799. }
  2800. }
  2801. static int chcr_aead_op(struct aead_request *req,
  2802. unsigned short op_type,
  2803. int size,
  2804. create_wr_t create_wr_fn)
  2805. {
  2806. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  2807. struct chcr_context *ctx = crypto_aead_ctx(tfm);
  2808. struct uld_ctx *u_ctx;
  2809. struct sk_buff *skb;
  2810. if (!ctx->dev) {
  2811. pr_err("chcr : %s : No crypto device.\n", __func__);
  2812. return -ENXIO;
  2813. }
  2814. u_ctx = ULD_CTX(ctx);
  2815. if (cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
  2816. ctx->tx_qidx)) {
  2817. if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
  2818. return -EBUSY;
  2819. }
  2820. /* Form a WR from req */
  2821. skb = create_wr_fn(req, u_ctx->lldi.rxq_ids[ctx->rx_qidx], size,
  2822. op_type);
  2823. if (IS_ERR(skb) || !skb)
  2824. return PTR_ERR(skb);
  2825. skb->dev = u_ctx->lldi.ports[0];
  2826. set_wr_txq(skb, CPL_PRIORITY_DATA, ctx->tx_qidx);
  2827. chcr_send_wr(skb);
  2828. return -EINPROGRESS;
  2829. }
  2830. static struct chcr_alg_template driver_algs[] = {
  2831. /* AES-CBC */
  2832. {
  2833. .type = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_SUB_TYPE_CBC,
  2834. .is_registered = 0,
  2835. .alg.crypto = {
  2836. .cra_name = "cbc(aes)",
  2837. .cra_driver_name = "cbc-aes-chcr",
  2838. .cra_blocksize = AES_BLOCK_SIZE,
  2839. .cra_init = chcr_cra_init,
  2840. .cra_exit = chcr_cra_exit,
  2841. .cra_u.ablkcipher = {
  2842. .min_keysize = AES_MIN_KEY_SIZE,
  2843. .max_keysize = AES_MAX_KEY_SIZE,
  2844. .ivsize = AES_BLOCK_SIZE,
  2845. .setkey = chcr_aes_cbc_setkey,
  2846. .encrypt = chcr_aes_encrypt,
  2847. .decrypt = chcr_aes_decrypt,
  2848. }
  2849. }
  2850. },
  2851. {
  2852. .type = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_SUB_TYPE_XTS,
  2853. .is_registered = 0,
  2854. .alg.crypto = {
  2855. .cra_name = "xts(aes)",
  2856. .cra_driver_name = "xts-aes-chcr",
  2857. .cra_blocksize = AES_BLOCK_SIZE,
  2858. .cra_init = chcr_cra_init,
  2859. .cra_exit = NULL,
  2860. .cra_u .ablkcipher = {
  2861. .min_keysize = 2 * AES_MIN_KEY_SIZE,
  2862. .max_keysize = 2 * AES_MAX_KEY_SIZE,
  2863. .ivsize = AES_BLOCK_SIZE,
  2864. .setkey = chcr_aes_xts_setkey,
  2865. .encrypt = chcr_aes_encrypt,
  2866. .decrypt = chcr_aes_decrypt,
  2867. }
  2868. }
  2869. },
  2870. {
  2871. .type = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_SUB_TYPE_CTR,
  2872. .is_registered = 0,
  2873. .alg.crypto = {
  2874. .cra_name = "ctr(aes)",
  2875. .cra_driver_name = "ctr-aes-chcr",
  2876. .cra_blocksize = 1,
  2877. .cra_init = chcr_cra_init,
  2878. .cra_exit = chcr_cra_exit,
  2879. .cra_u.ablkcipher = {
  2880. .min_keysize = AES_MIN_KEY_SIZE,
  2881. .max_keysize = AES_MAX_KEY_SIZE,
  2882. .ivsize = AES_BLOCK_SIZE,
  2883. .setkey = chcr_aes_ctr_setkey,
  2884. .encrypt = chcr_aes_encrypt,
  2885. .decrypt = chcr_aes_decrypt,
  2886. }
  2887. }
  2888. },
  2889. {
  2890. .type = CRYPTO_ALG_TYPE_ABLKCIPHER |
  2891. CRYPTO_ALG_SUB_TYPE_CTR_RFC3686,
  2892. .is_registered = 0,
  2893. .alg.crypto = {
  2894. .cra_name = "rfc3686(ctr(aes))",
  2895. .cra_driver_name = "rfc3686-ctr-aes-chcr",
  2896. .cra_blocksize = 1,
  2897. .cra_init = chcr_rfc3686_init,
  2898. .cra_exit = chcr_cra_exit,
  2899. .cra_u.ablkcipher = {
  2900. .min_keysize = AES_MIN_KEY_SIZE +
  2901. CTR_RFC3686_NONCE_SIZE,
  2902. .max_keysize = AES_MAX_KEY_SIZE +
  2903. CTR_RFC3686_NONCE_SIZE,
  2904. .ivsize = CTR_RFC3686_IV_SIZE,
  2905. .setkey = chcr_aes_rfc3686_setkey,
  2906. .encrypt = chcr_aes_encrypt,
  2907. .decrypt = chcr_aes_decrypt,
  2908. .geniv = "seqiv",
  2909. }
  2910. }
  2911. },
  2912. /* SHA */
  2913. {
  2914. .type = CRYPTO_ALG_TYPE_AHASH,
  2915. .is_registered = 0,
  2916. .alg.hash = {
  2917. .halg.digestsize = SHA1_DIGEST_SIZE,
  2918. .halg.base = {
  2919. .cra_name = "sha1",
  2920. .cra_driver_name = "sha1-chcr",
  2921. .cra_blocksize = SHA1_BLOCK_SIZE,
  2922. }
  2923. }
  2924. },
  2925. {
  2926. .type = CRYPTO_ALG_TYPE_AHASH,
  2927. .is_registered = 0,
  2928. .alg.hash = {
  2929. .halg.digestsize = SHA256_DIGEST_SIZE,
  2930. .halg.base = {
  2931. .cra_name = "sha256",
  2932. .cra_driver_name = "sha256-chcr",
  2933. .cra_blocksize = SHA256_BLOCK_SIZE,
  2934. }
  2935. }
  2936. },
  2937. {
  2938. .type = CRYPTO_ALG_TYPE_AHASH,
  2939. .is_registered = 0,
  2940. .alg.hash = {
  2941. .halg.digestsize = SHA224_DIGEST_SIZE,
  2942. .halg.base = {
  2943. .cra_name = "sha224",
  2944. .cra_driver_name = "sha224-chcr",
  2945. .cra_blocksize = SHA224_BLOCK_SIZE,
  2946. }
  2947. }
  2948. },
  2949. {
  2950. .type = CRYPTO_ALG_TYPE_AHASH,
  2951. .is_registered = 0,
  2952. .alg.hash = {
  2953. .halg.digestsize = SHA384_DIGEST_SIZE,
  2954. .halg.base = {
  2955. .cra_name = "sha384",
  2956. .cra_driver_name = "sha384-chcr",
  2957. .cra_blocksize = SHA384_BLOCK_SIZE,
  2958. }
  2959. }
  2960. },
  2961. {
  2962. .type = CRYPTO_ALG_TYPE_AHASH,
  2963. .is_registered = 0,
  2964. .alg.hash = {
  2965. .halg.digestsize = SHA512_DIGEST_SIZE,
  2966. .halg.base = {
  2967. .cra_name = "sha512",
  2968. .cra_driver_name = "sha512-chcr",
  2969. .cra_blocksize = SHA512_BLOCK_SIZE,
  2970. }
  2971. }
  2972. },
  2973. /* HMAC */
  2974. {
  2975. .type = CRYPTO_ALG_TYPE_HMAC,
  2976. .is_registered = 0,
  2977. .alg.hash = {
  2978. .halg.digestsize = SHA1_DIGEST_SIZE,
  2979. .halg.base = {
  2980. .cra_name = "hmac(sha1)",
  2981. .cra_driver_name = "hmac-sha1-chcr",
  2982. .cra_blocksize = SHA1_BLOCK_SIZE,
  2983. }
  2984. }
  2985. },
  2986. {
  2987. .type = CRYPTO_ALG_TYPE_HMAC,
  2988. .is_registered = 0,
  2989. .alg.hash = {
  2990. .halg.digestsize = SHA224_DIGEST_SIZE,
  2991. .halg.base = {
  2992. .cra_name = "hmac(sha224)",
  2993. .cra_driver_name = "hmac-sha224-chcr",
  2994. .cra_blocksize = SHA224_BLOCK_SIZE,
  2995. }
  2996. }
  2997. },
  2998. {
  2999. .type = CRYPTO_ALG_TYPE_HMAC,
  3000. .is_registered = 0,
  3001. .alg.hash = {
  3002. .halg.digestsize = SHA256_DIGEST_SIZE,
  3003. .halg.base = {
  3004. .cra_name = "hmac(sha256)",
  3005. .cra_driver_name = "hmac-sha256-chcr",
  3006. .cra_blocksize = SHA256_BLOCK_SIZE,
  3007. }
  3008. }
  3009. },
  3010. {
  3011. .type = CRYPTO_ALG_TYPE_HMAC,
  3012. .is_registered = 0,
  3013. .alg.hash = {
  3014. .halg.digestsize = SHA384_DIGEST_SIZE,
  3015. .halg.base = {
  3016. .cra_name = "hmac(sha384)",
  3017. .cra_driver_name = "hmac-sha384-chcr",
  3018. .cra_blocksize = SHA384_BLOCK_SIZE,
  3019. }
  3020. }
  3021. },
  3022. {
  3023. .type = CRYPTO_ALG_TYPE_HMAC,
  3024. .is_registered = 0,
  3025. .alg.hash = {
  3026. .halg.digestsize = SHA512_DIGEST_SIZE,
  3027. .halg.base = {
  3028. .cra_name = "hmac(sha512)",
  3029. .cra_driver_name = "hmac-sha512-chcr",
  3030. .cra_blocksize = SHA512_BLOCK_SIZE,
  3031. }
  3032. }
  3033. },
  3034. /* Add AEAD Algorithms */
  3035. {
  3036. .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_AEAD_GCM,
  3037. .is_registered = 0,
  3038. .alg.aead = {
  3039. .base = {
  3040. .cra_name = "gcm(aes)",
  3041. .cra_driver_name = "gcm-aes-chcr",
  3042. .cra_blocksize = 1,
  3043. .cra_priority = CHCR_AEAD_PRIORITY,
  3044. .cra_ctxsize = sizeof(struct chcr_context) +
  3045. sizeof(struct chcr_aead_ctx) +
  3046. sizeof(struct chcr_gcm_ctx),
  3047. },
  3048. .ivsize = 12,
  3049. .maxauthsize = GHASH_DIGEST_SIZE,
  3050. .setkey = chcr_gcm_setkey,
  3051. .setauthsize = chcr_gcm_setauthsize,
  3052. }
  3053. },
  3054. {
  3055. .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106,
  3056. .is_registered = 0,
  3057. .alg.aead = {
  3058. .base = {
  3059. .cra_name = "rfc4106(gcm(aes))",
  3060. .cra_driver_name = "rfc4106-gcm-aes-chcr",
  3061. .cra_blocksize = 1,
  3062. .cra_priority = CHCR_AEAD_PRIORITY + 1,
  3063. .cra_ctxsize = sizeof(struct chcr_context) +
  3064. sizeof(struct chcr_aead_ctx) +
  3065. sizeof(struct chcr_gcm_ctx),
  3066. },
  3067. .ivsize = 8,
  3068. .maxauthsize = GHASH_DIGEST_SIZE,
  3069. .setkey = chcr_gcm_setkey,
  3070. .setauthsize = chcr_4106_4309_setauthsize,
  3071. }
  3072. },
  3073. {
  3074. .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_AEAD_CCM,
  3075. .is_registered = 0,
  3076. .alg.aead = {
  3077. .base = {
  3078. .cra_name = "ccm(aes)",
  3079. .cra_driver_name = "ccm-aes-chcr",
  3080. .cra_blocksize = 1,
  3081. .cra_priority = CHCR_AEAD_PRIORITY,
  3082. .cra_ctxsize = sizeof(struct chcr_context) +
  3083. sizeof(struct chcr_aead_ctx),
  3084. },
  3085. .ivsize = AES_BLOCK_SIZE,
  3086. .maxauthsize = GHASH_DIGEST_SIZE,
  3087. .setkey = chcr_aead_ccm_setkey,
  3088. .setauthsize = chcr_ccm_setauthsize,
  3089. }
  3090. },
  3091. {
  3092. .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309,
  3093. .is_registered = 0,
  3094. .alg.aead = {
  3095. .base = {
  3096. .cra_name = "rfc4309(ccm(aes))",
  3097. .cra_driver_name = "rfc4309-ccm-aes-chcr",
  3098. .cra_blocksize = 1,
  3099. .cra_priority = CHCR_AEAD_PRIORITY + 1,
  3100. .cra_ctxsize = sizeof(struct chcr_context) +
  3101. sizeof(struct chcr_aead_ctx),
  3102. },
  3103. .ivsize = 8,
  3104. .maxauthsize = GHASH_DIGEST_SIZE,
  3105. .setkey = chcr_aead_rfc4309_setkey,
  3106. .setauthsize = chcr_4106_4309_setauthsize,
  3107. }
  3108. },
  3109. {
  3110. .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_AEAD_AUTHENC,
  3111. .is_registered = 0,
  3112. .alg.aead = {
  3113. .base = {
  3114. .cra_name = "authenc(hmac(sha1),cbc(aes))",
  3115. .cra_driver_name =
  3116. "authenc-hmac-sha1-cbc-aes-chcr",
  3117. .cra_blocksize = AES_BLOCK_SIZE,
  3118. .cra_priority = CHCR_AEAD_PRIORITY,
  3119. .cra_ctxsize = sizeof(struct chcr_context) +
  3120. sizeof(struct chcr_aead_ctx) +
  3121. sizeof(struct chcr_authenc_ctx),
  3122. },
  3123. .ivsize = AES_BLOCK_SIZE,
  3124. .maxauthsize = SHA1_DIGEST_SIZE,
  3125. .setkey = chcr_authenc_setkey,
  3126. .setauthsize = chcr_authenc_setauthsize,
  3127. }
  3128. },
  3129. {
  3130. .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_AEAD_AUTHENC,
  3131. .is_registered = 0,
  3132. .alg.aead = {
  3133. .base = {
  3134. .cra_name = "authenc(hmac(sha256),cbc(aes))",
  3135. .cra_driver_name =
  3136. "authenc-hmac-sha256-cbc-aes-chcr",
  3137. .cra_blocksize = AES_BLOCK_SIZE,
  3138. .cra_priority = CHCR_AEAD_PRIORITY,
  3139. .cra_ctxsize = sizeof(struct chcr_context) +
  3140. sizeof(struct chcr_aead_ctx) +
  3141. sizeof(struct chcr_authenc_ctx),
  3142. },
  3143. .ivsize = AES_BLOCK_SIZE,
  3144. .maxauthsize = SHA256_DIGEST_SIZE,
  3145. .setkey = chcr_authenc_setkey,
  3146. .setauthsize = chcr_authenc_setauthsize,
  3147. }
  3148. },
  3149. {
  3150. .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_AEAD_AUTHENC,
  3151. .is_registered = 0,
  3152. .alg.aead = {
  3153. .base = {
  3154. .cra_name = "authenc(hmac(sha224),cbc(aes))",
  3155. .cra_driver_name =
  3156. "authenc-hmac-sha224-cbc-aes-chcr",
  3157. .cra_blocksize = AES_BLOCK_SIZE,
  3158. .cra_priority = CHCR_AEAD_PRIORITY,
  3159. .cra_ctxsize = sizeof(struct chcr_context) +
  3160. sizeof(struct chcr_aead_ctx) +
  3161. sizeof(struct chcr_authenc_ctx),
  3162. },
  3163. .ivsize = AES_BLOCK_SIZE,
  3164. .maxauthsize = SHA224_DIGEST_SIZE,
  3165. .setkey = chcr_authenc_setkey,
  3166. .setauthsize = chcr_authenc_setauthsize,
  3167. }
  3168. },
  3169. {
  3170. .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_AEAD_AUTHENC,
  3171. .is_registered = 0,
  3172. .alg.aead = {
  3173. .base = {
  3174. .cra_name = "authenc(hmac(sha384),cbc(aes))",
  3175. .cra_driver_name =
  3176. "authenc-hmac-sha384-cbc-aes-chcr",
  3177. .cra_blocksize = AES_BLOCK_SIZE,
  3178. .cra_priority = CHCR_AEAD_PRIORITY,
  3179. .cra_ctxsize = sizeof(struct chcr_context) +
  3180. sizeof(struct chcr_aead_ctx) +
  3181. sizeof(struct chcr_authenc_ctx),
  3182. },
  3183. .ivsize = AES_BLOCK_SIZE,
  3184. .maxauthsize = SHA384_DIGEST_SIZE,
  3185. .setkey = chcr_authenc_setkey,
  3186. .setauthsize = chcr_authenc_setauthsize,
  3187. }
  3188. },
  3189. {
  3190. .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_AEAD_AUTHENC,
  3191. .is_registered = 0,
  3192. .alg.aead = {
  3193. .base = {
  3194. .cra_name = "authenc(hmac(sha512),cbc(aes))",
  3195. .cra_driver_name =
  3196. "authenc-hmac-sha512-cbc-aes-chcr",
  3197. .cra_blocksize = AES_BLOCK_SIZE,
  3198. .cra_priority = CHCR_AEAD_PRIORITY,
  3199. .cra_ctxsize = sizeof(struct chcr_context) +
  3200. sizeof(struct chcr_aead_ctx) +
  3201. sizeof(struct chcr_authenc_ctx),
  3202. },
  3203. .ivsize = AES_BLOCK_SIZE,
  3204. .maxauthsize = SHA512_DIGEST_SIZE,
  3205. .setkey = chcr_authenc_setkey,
  3206. .setauthsize = chcr_authenc_setauthsize,
  3207. }
  3208. },
  3209. {
  3210. .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_AEAD_NULL,
  3211. .is_registered = 0,
  3212. .alg.aead = {
  3213. .base = {
  3214. .cra_name = "authenc(digest_null,cbc(aes))",
  3215. .cra_driver_name =
  3216. "authenc-digest_null-cbc-aes-chcr",
  3217. .cra_blocksize = AES_BLOCK_SIZE,
  3218. .cra_priority = CHCR_AEAD_PRIORITY,
  3219. .cra_ctxsize = sizeof(struct chcr_context) +
  3220. sizeof(struct chcr_aead_ctx) +
  3221. sizeof(struct chcr_authenc_ctx),
  3222. },
  3223. .ivsize = AES_BLOCK_SIZE,
  3224. .maxauthsize = 0,
  3225. .setkey = chcr_aead_digest_null_setkey,
  3226. .setauthsize = chcr_authenc_null_setauthsize,
  3227. }
  3228. },
  3229. };
  3230. /*
  3231. * chcr_unregister_alg - Deregister crypto algorithms with
  3232. * kernel framework.
  3233. */
  3234. static int chcr_unregister_alg(void)
  3235. {
  3236. int i;
  3237. for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
  3238. switch (driver_algs[i].type & CRYPTO_ALG_TYPE_MASK) {
  3239. case CRYPTO_ALG_TYPE_ABLKCIPHER:
  3240. if (driver_algs[i].is_registered)
  3241. crypto_unregister_alg(
  3242. &driver_algs[i].alg.crypto);
  3243. break;
  3244. case CRYPTO_ALG_TYPE_AEAD:
  3245. if (driver_algs[i].is_registered)
  3246. crypto_unregister_aead(
  3247. &driver_algs[i].alg.aead);
  3248. break;
  3249. case CRYPTO_ALG_TYPE_AHASH:
  3250. if (driver_algs[i].is_registered)
  3251. crypto_unregister_ahash(
  3252. &driver_algs[i].alg.hash);
  3253. break;
  3254. }
  3255. driver_algs[i].is_registered = 0;
  3256. }
  3257. return 0;
  3258. }
  3259. #define SZ_AHASH_CTX sizeof(struct chcr_context)
  3260. #define SZ_AHASH_H_CTX (sizeof(struct chcr_context) + sizeof(struct hmac_ctx))
  3261. #define SZ_AHASH_REQ_CTX sizeof(struct chcr_ahash_req_ctx)
  3262. #define AHASH_CRA_FLAGS (CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC)
  3263. /*
  3264. * chcr_register_alg - Register crypto algorithms with kernel framework.
  3265. */
  3266. static int chcr_register_alg(void)
  3267. {
  3268. struct crypto_alg ai;
  3269. struct ahash_alg *a_hash;
  3270. int err = 0, i;
  3271. char *name = NULL;
  3272. for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
  3273. if (driver_algs[i].is_registered)
  3274. continue;
  3275. switch (driver_algs[i].type & CRYPTO_ALG_TYPE_MASK) {
  3276. case CRYPTO_ALG_TYPE_ABLKCIPHER:
  3277. driver_algs[i].alg.crypto.cra_priority =
  3278. CHCR_CRA_PRIORITY;
  3279. driver_algs[i].alg.crypto.cra_module = THIS_MODULE;
  3280. driver_algs[i].alg.crypto.cra_flags =
  3281. CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC |
  3282. CRYPTO_ALG_NEED_FALLBACK;
  3283. driver_algs[i].alg.crypto.cra_ctxsize =
  3284. sizeof(struct chcr_context) +
  3285. sizeof(struct ablk_ctx);
  3286. driver_algs[i].alg.crypto.cra_alignmask = 0;
  3287. driver_algs[i].alg.crypto.cra_type =
  3288. &crypto_ablkcipher_type;
  3289. err = crypto_register_alg(&driver_algs[i].alg.crypto);
  3290. name = driver_algs[i].alg.crypto.cra_driver_name;
  3291. break;
  3292. case CRYPTO_ALG_TYPE_AEAD:
  3293. driver_algs[i].alg.aead.base.cra_flags =
  3294. CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC |
  3295. CRYPTO_ALG_NEED_FALLBACK;
  3296. driver_algs[i].alg.aead.encrypt = chcr_aead_encrypt;
  3297. driver_algs[i].alg.aead.decrypt = chcr_aead_decrypt;
  3298. driver_algs[i].alg.aead.init = chcr_aead_cra_init;
  3299. driver_algs[i].alg.aead.exit = chcr_aead_cra_exit;
  3300. driver_algs[i].alg.aead.base.cra_module = THIS_MODULE;
  3301. err = crypto_register_aead(&driver_algs[i].alg.aead);
  3302. name = driver_algs[i].alg.aead.base.cra_driver_name;
  3303. break;
  3304. case CRYPTO_ALG_TYPE_AHASH:
  3305. a_hash = &driver_algs[i].alg.hash;
  3306. a_hash->update = chcr_ahash_update;
  3307. a_hash->final = chcr_ahash_final;
  3308. a_hash->finup = chcr_ahash_finup;
  3309. a_hash->digest = chcr_ahash_digest;
  3310. a_hash->export = chcr_ahash_export;
  3311. a_hash->import = chcr_ahash_import;
  3312. a_hash->halg.statesize = SZ_AHASH_REQ_CTX;
  3313. a_hash->halg.base.cra_priority = CHCR_CRA_PRIORITY;
  3314. a_hash->halg.base.cra_module = THIS_MODULE;
  3315. a_hash->halg.base.cra_flags = AHASH_CRA_FLAGS;
  3316. a_hash->halg.base.cra_alignmask = 0;
  3317. a_hash->halg.base.cra_exit = NULL;
  3318. a_hash->halg.base.cra_type = &crypto_ahash_type;
  3319. if (driver_algs[i].type == CRYPTO_ALG_TYPE_HMAC) {
  3320. a_hash->halg.base.cra_init = chcr_hmac_cra_init;
  3321. a_hash->halg.base.cra_exit = chcr_hmac_cra_exit;
  3322. a_hash->init = chcr_hmac_init;
  3323. a_hash->setkey = chcr_ahash_setkey;
  3324. a_hash->halg.base.cra_ctxsize = SZ_AHASH_H_CTX;
  3325. } else {
  3326. a_hash->init = chcr_sha_init;
  3327. a_hash->halg.base.cra_ctxsize = SZ_AHASH_CTX;
  3328. a_hash->halg.base.cra_init = chcr_sha_cra_init;
  3329. }
  3330. err = crypto_register_ahash(&driver_algs[i].alg.hash);
  3331. ai = driver_algs[i].alg.hash.halg.base;
  3332. name = ai.cra_driver_name;
  3333. break;
  3334. }
  3335. if (err) {
  3336. pr_err("chcr : %s : Algorithm registration failed\n",
  3337. name);
  3338. goto register_err;
  3339. } else {
  3340. driver_algs[i].is_registered = 1;
  3341. }
  3342. }
  3343. return 0;
  3344. register_err:
  3345. chcr_unregister_alg();
  3346. return err;
  3347. }
  3348. /*
  3349. * start_crypto - Register the crypto algorithms.
  3350. * This should called once when the first device comesup. After this
  3351. * kernel will start calling driver APIs for crypto operations.
  3352. */
  3353. int start_crypto(void)
  3354. {
  3355. return chcr_register_alg();
  3356. }
  3357. /*
  3358. * stop_crypto - Deregister all the crypto algorithms with kernel.
  3359. * This should be called once when the last device goes down. After this
  3360. * kernel will not call the driver API for crypto operations.
  3361. */
  3362. int stop_crypto(void)
  3363. {
  3364. chcr_unregister_alg();
  3365. return 0;
  3366. }