chcr_algo.c 89 KB

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