chcr_algo.c 85 KB

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