chcr_algo.c 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996
  1. /*
  2. * This file is part of the Chelsio T6 Crypto driver for Linux.
  3. *
  4. * Copyright (c) 2003-2016 Chelsio Communications, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. *
  34. * Written and Maintained by:
  35. * Manoj Malviya (manojmalviya@chelsio.com)
  36. * Atul Gupta (atul.gupta@chelsio.com)
  37. * Jitendra Lulla (jlulla@chelsio.com)
  38. * Yeshaswi M R Gowda (yeshaswi@chelsio.com)
  39. * Harsh Jain (harsh@chelsio.com)
  40. */
  41. #define pr_fmt(fmt) "chcr:" fmt
  42. #include <linux/kernel.h>
  43. #include <linux/module.h>
  44. #include <linux/crypto.h>
  45. #include <linux/cryptohash.h>
  46. #include <linux/skbuff.h>
  47. #include <linux/rtnetlink.h>
  48. #include <linux/highmem.h>
  49. #include <linux/scatterlist.h>
  50. #include <crypto/aes.h>
  51. #include <crypto/algapi.h>
  52. #include <crypto/hash.h>
  53. #include <crypto/sha.h>
  54. #include <crypto/authenc.h>
  55. #include <crypto/internal/aead.h>
  56. #include <crypto/null.h>
  57. #include <crypto/internal/skcipher.h>
  58. #include <crypto/aead.h>
  59. #include <crypto/scatterwalk.h>
  60. #include <crypto/internal/hash.h>
  61. #include "t4fw_api.h"
  62. #include "t4_msg.h"
  63. #include "chcr_core.h"
  64. #include "chcr_algo.h"
  65. #include "chcr_crypto.h"
  66. static inline struct chcr_aead_ctx *AEAD_CTX(struct chcr_context *ctx)
  67. {
  68. return ctx->crypto_ctx->aeadctx;
  69. }
  70. static inline struct ablk_ctx *ABLK_CTX(struct chcr_context *ctx)
  71. {
  72. return ctx->crypto_ctx->ablkctx;
  73. }
  74. static inline struct hmac_ctx *HMAC_CTX(struct chcr_context *ctx)
  75. {
  76. return ctx->crypto_ctx->hmacctx;
  77. }
  78. static inline struct chcr_gcm_ctx *GCM_CTX(struct chcr_aead_ctx *gctx)
  79. {
  80. return gctx->ctx->gcm;
  81. }
  82. static inline struct chcr_authenc_ctx *AUTHENC_CTX(struct chcr_aead_ctx *gctx)
  83. {
  84. return gctx->ctx->authenc;
  85. }
  86. static inline struct uld_ctx *ULD_CTX(struct chcr_context *ctx)
  87. {
  88. return ctx->dev->u_ctx;
  89. }
  90. static inline int is_ofld_imm(const struct sk_buff *skb)
  91. {
  92. return (skb->len <= CRYPTO_MAX_IMM_TX_PKT_LEN);
  93. }
  94. /*
  95. * sgl_len - calculates the size of an SGL of the given capacity
  96. * @n: the number of SGL entries
  97. * Calculates the number of flits needed for a scatter/gather list that
  98. * can hold the given number of entries.
  99. */
  100. static inline unsigned int sgl_len(unsigned int n)
  101. {
  102. n--;
  103. return (3 * n) / 2 + (n & 1) + 2;
  104. }
  105. static void chcr_verify_tag(struct aead_request *req, u8 *input, int *err)
  106. {
  107. u8 temp[SHA512_DIGEST_SIZE];
  108. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  109. int authsize = crypto_aead_authsize(tfm);
  110. struct cpl_fw6_pld *fw6_pld;
  111. int cmp = 0;
  112. fw6_pld = (struct cpl_fw6_pld *)input;
  113. if ((get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106) ||
  114. (get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_GCM)) {
  115. cmp = memcmp(&fw6_pld->data[2], (fw6_pld + 1), authsize);
  116. } else {
  117. sg_pcopy_to_buffer(req->src, sg_nents(req->src), temp,
  118. authsize, req->assoclen +
  119. req->cryptlen - authsize);
  120. cmp = memcmp(temp, (fw6_pld + 1), authsize);
  121. }
  122. if (cmp)
  123. *err = -EBADMSG;
  124. else
  125. *err = 0;
  126. }
  127. /*
  128. * chcr_handle_resp - Unmap the DMA buffers associated with the request
  129. * @req: crypto request
  130. */
  131. int chcr_handle_resp(struct crypto_async_request *req, unsigned char *input,
  132. int err)
  133. {
  134. struct crypto_tfm *tfm = req->tfm;
  135. struct chcr_context *ctx = crypto_tfm_ctx(tfm);
  136. struct uld_ctx *u_ctx = ULD_CTX(ctx);
  137. struct chcr_req_ctx ctx_req;
  138. struct cpl_fw6_pld *fw6_pld;
  139. unsigned int digestsize, updated_digestsize;
  140. switch (tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
  141. case CRYPTO_ALG_TYPE_AEAD:
  142. ctx_req.req.aead_req = (struct aead_request *)req;
  143. ctx_req.ctx.reqctx = aead_request_ctx(ctx_req.req.aead_req);
  144. dma_unmap_sg(&u_ctx->lldi.pdev->dev, ctx_req.ctx.reqctx->dst,
  145. ctx_req.ctx.reqctx->dst_nents, DMA_FROM_DEVICE);
  146. if (ctx_req.ctx.reqctx->skb) {
  147. kfree_skb(ctx_req.ctx.reqctx->skb);
  148. ctx_req.ctx.reqctx->skb = NULL;
  149. }
  150. if (ctx_req.ctx.reqctx->verify == VERIFY_SW) {
  151. chcr_verify_tag(ctx_req.req.aead_req, input,
  152. &err);
  153. ctx_req.ctx.reqctx->verify = VERIFY_HW;
  154. }
  155. break;
  156. case CRYPTO_ALG_TYPE_ABLKCIPHER:
  157. ctx_req.req.ablk_req = (struct ablkcipher_request *)req;
  158. ctx_req.ctx.ablk_ctx =
  159. ablkcipher_request_ctx(ctx_req.req.ablk_req);
  160. if (!err) {
  161. fw6_pld = (struct cpl_fw6_pld *)input;
  162. memcpy(ctx_req.req.ablk_req->info, &fw6_pld->data[2],
  163. AES_BLOCK_SIZE);
  164. }
  165. dma_unmap_sg(&u_ctx->lldi.pdev->dev, ctx_req.req.ablk_req->dst,
  166. ctx_req.ctx.ablk_ctx->dst_nents, DMA_FROM_DEVICE);
  167. if (ctx_req.ctx.ablk_ctx->skb) {
  168. kfree_skb(ctx_req.ctx.ablk_ctx->skb);
  169. ctx_req.ctx.ablk_ctx->skb = NULL;
  170. }
  171. break;
  172. case CRYPTO_ALG_TYPE_AHASH:
  173. ctx_req.req.ahash_req = (struct ahash_request *)req;
  174. ctx_req.ctx.ahash_ctx =
  175. ahash_request_ctx(ctx_req.req.ahash_req);
  176. digestsize =
  177. crypto_ahash_digestsize(crypto_ahash_reqtfm(
  178. ctx_req.req.ahash_req));
  179. updated_digestsize = digestsize;
  180. if (digestsize == SHA224_DIGEST_SIZE)
  181. updated_digestsize = SHA256_DIGEST_SIZE;
  182. else if (digestsize == SHA384_DIGEST_SIZE)
  183. updated_digestsize = SHA512_DIGEST_SIZE;
  184. if (ctx_req.ctx.ahash_ctx->skb) {
  185. kfree_skb(ctx_req.ctx.ahash_ctx->skb);
  186. ctx_req.ctx.ahash_ctx->skb = NULL;
  187. }
  188. if (ctx_req.ctx.ahash_ctx->result == 1) {
  189. ctx_req.ctx.ahash_ctx->result = 0;
  190. memcpy(ctx_req.req.ahash_req->result, input +
  191. sizeof(struct cpl_fw6_pld),
  192. digestsize);
  193. } else {
  194. memcpy(ctx_req.ctx.ahash_ctx->partial_hash, input +
  195. sizeof(struct cpl_fw6_pld),
  196. updated_digestsize);
  197. }
  198. break;
  199. }
  200. return err;
  201. }
  202. /*
  203. * calc_tx_flits_ofld - calculate # of flits for an offload packet
  204. * @skb: the packet
  205. * Returns the number of flits needed for the given offload packet.
  206. * These packets are already fully constructed and no additional headers
  207. * will be added.
  208. */
  209. static inline unsigned int calc_tx_flits_ofld(const struct sk_buff *skb)
  210. {
  211. unsigned int flits, cnt;
  212. if (is_ofld_imm(skb))
  213. return DIV_ROUND_UP(skb->len, 8);
  214. flits = skb_transport_offset(skb) / 8; /* headers */
  215. cnt = skb_shinfo(skb)->nr_frags;
  216. if (skb_tail_pointer(skb) != skb_transport_header(skb))
  217. cnt++;
  218. return flits + sgl_len(cnt);
  219. }
  220. static inline void get_aes_decrypt_key(unsigned char *dec_key,
  221. const unsigned char *key,
  222. unsigned int keylength)
  223. {
  224. u32 temp;
  225. u32 w_ring[MAX_NK];
  226. int i, j, k;
  227. u8 nr, nk;
  228. switch (keylength) {
  229. case AES_KEYLENGTH_128BIT:
  230. nk = KEYLENGTH_4BYTES;
  231. nr = NUMBER_OF_ROUNDS_10;
  232. break;
  233. case AES_KEYLENGTH_192BIT:
  234. nk = KEYLENGTH_6BYTES;
  235. nr = NUMBER_OF_ROUNDS_12;
  236. break;
  237. case AES_KEYLENGTH_256BIT:
  238. nk = KEYLENGTH_8BYTES;
  239. nr = NUMBER_OF_ROUNDS_14;
  240. break;
  241. default:
  242. return;
  243. }
  244. for (i = 0; i < nk; i++)
  245. w_ring[i] = be32_to_cpu(*(u32 *)&key[4 * i]);
  246. i = 0;
  247. temp = w_ring[nk - 1];
  248. while (i + nk < (nr + 1) * 4) {
  249. if (!(i % nk)) {
  250. /* RotWord(temp) */
  251. temp = (temp << 8) | (temp >> 24);
  252. temp = aes_ks_subword(temp);
  253. temp ^= round_constant[i / nk];
  254. } else if (nk == 8 && (i % 4 == 0)) {
  255. temp = aes_ks_subword(temp);
  256. }
  257. w_ring[i % nk] ^= temp;
  258. temp = w_ring[i % nk];
  259. i++;
  260. }
  261. i--;
  262. for (k = 0, j = i % nk; k < nk; k++) {
  263. *((u32 *)dec_key + k) = htonl(w_ring[j]);
  264. j--;
  265. if (j < 0)
  266. j += nk;
  267. }
  268. }
  269. static struct crypto_shash *chcr_alloc_shash(unsigned int ds)
  270. {
  271. struct crypto_shash *base_hash = 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->rx_channel_id, qid,
  493. is_iv ? iv_loc : IV_NOP, ctx->tx_channel_id);
  494. chcr_req->ulptx.cmd_dest = FILL_ULPTX_CMD_DEST(ctx->dev->tx_channel_id,
  495. qid);
  496. chcr_req->ulptx.len = htonl((DIV_ROUND_UP((calc_tx_flits_ofld(skb) * 8),
  497. 16) - ((sizeof(chcr_req->wreq)) >> 4)));
  498. chcr_req->sc_imm.cmd_more = FILL_CMD_MORE(immdatalen);
  499. chcr_req->sc_imm.len = cpu_to_be32(sizeof(struct cpl_tx_sec_pdu) +
  500. sizeof(chcr_req->key_ctx) +
  501. kctx_len + sc_len + immdatalen);
  502. }
  503. /**
  504. * create_cipher_wr - form the WR for cipher operations
  505. * @req: cipher req.
  506. * @ctx: crypto driver context of the request.
  507. * @qid: ingress qid where response of this WR should be received.
  508. * @op_type: encryption or decryption
  509. */
  510. static struct sk_buff
  511. *create_cipher_wr(struct ablkcipher_request *req,
  512. unsigned short qid,
  513. unsigned short op_type)
  514. {
  515. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  516. struct chcr_context *ctx = crypto_ablkcipher_ctx(tfm);
  517. struct uld_ctx *u_ctx = ULD_CTX(ctx);
  518. struct ablk_ctx *ablkctx = ABLK_CTX(ctx);
  519. struct sk_buff *skb = NULL;
  520. struct chcr_wr *chcr_req;
  521. struct cpl_rx_phys_dsgl *phys_cpl;
  522. struct chcr_blkcipher_req_ctx *reqctx = ablkcipher_request_ctx(req);
  523. struct phys_sge_parm sg_param;
  524. unsigned int frags = 0, transhdr_len, phys_dsgl;
  525. unsigned int ivsize = crypto_ablkcipher_ivsize(tfm), kctx_len;
  526. gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
  527. GFP_ATOMIC;
  528. if (!req->info)
  529. return ERR_PTR(-EINVAL);
  530. reqctx->dst_nents = sg_nents_for_len(req->dst, req->nbytes);
  531. if (reqctx->dst_nents <= 0) {
  532. pr_err("AES:Invalid Destination sg lists\n");
  533. return ERR_PTR(-EINVAL);
  534. }
  535. if ((ablkctx->enckey_len == 0) || (ivsize > AES_BLOCK_SIZE) ||
  536. (req->nbytes <= 0) || (req->nbytes % AES_BLOCK_SIZE)) {
  537. pr_err("AES: Invalid value of Key Len %d nbytes %d IV Len %d\n",
  538. ablkctx->enckey_len, req->nbytes, ivsize);
  539. return ERR_PTR(-EINVAL);
  540. }
  541. phys_dsgl = get_space_for_phys_dsgl(reqctx->dst_nents);
  542. kctx_len = (DIV_ROUND_UP(ablkctx->enckey_len, 16) * 16);
  543. transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, phys_dsgl);
  544. skb = alloc_skb((transhdr_len + sizeof(struct sge_opaque_hdr)), flags);
  545. if (!skb)
  546. return ERR_PTR(-ENOMEM);
  547. skb_reserve(skb, sizeof(struct sge_opaque_hdr));
  548. chcr_req = (struct chcr_wr *)__skb_put(skb, transhdr_len);
  549. memset(chcr_req, 0, transhdr_len);
  550. chcr_req->sec_cpl.op_ivinsrtofst =
  551. FILL_SEC_CPL_OP_IVINSR(ctx->dev->rx_channel_id, 2, 1);
  552. chcr_req->sec_cpl.pldlen = htonl(ivsize + req->nbytes);
  553. chcr_req->sec_cpl.aadstart_cipherstop_hi =
  554. FILL_SEC_CPL_CIPHERSTOP_HI(0, 0, ivsize + 1, 0);
  555. chcr_req->sec_cpl.cipherstop_lo_authinsert =
  556. FILL_SEC_CPL_AUTHINSERT(0, 0, 0, 0);
  557. chcr_req->sec_cpl.seqno_numivs = FILL_SEC_CPL_SCMD0_SEQNO(op_type, 0,
  558. ablkctx->ciph_mode,
  559. 0, 0, ivsize >> 1);
  560. chcr_req->sec_cpl.ivgen_hdrlen = FILL_SEC_CPL_IVGEN_HDRLEN(0, 0, 0,
  561. 0, 1, phys_dsgl);
  562. chcr_req->key_ctx.ctx_hdr = ablkctx->key_ctx_hdr;
  563. if (op_type == CHCR_DECRYPT_OP) {
  564. generate_copy_rrkey(ablkctx, &chcr_req->key_ctx);
  565. } else {
  566. if (ablkctx->ciph_mode == CHCR_SCMD_CIPHER_MODE_AES_CBC) {
  567. memcpy(chcr_req->key_ctx.key, ablkctx->key,
  568. ablkctx->enckey_len);
  569. } else {
  570. memcpy(chcr_req->key_ctx.key, ablkctx->key +
  571. (ablkctx->enckey_len >> 1),
  572. ablkctx->enckey_len >> 1);
  573. memcpy(chcr_req->key_ctx.key +
  574. (ablkctx->enckey_len >> 1),
  575. ablkctx->key,
  576. ablkctx->enckey_len >> 1);
  577. }
  578. }
  579. phys_cpl = (struct cpl_rx_phys_dsgl *)((u8 *)(chcr_req + 1) + kctx_len);
  580. sg_param.nents = reqctx->dst_nents;
  581. sg_param.obsize = req->nbytes;
  582. sg_param.qid = qid;
  583. sg_param.align = 1;
  584. if (map_writesg_phys_cpl(&u_ctx->lldi.pdev->dev, phys_cpl, req->dst,
  585. &sg_param))
  586. goto map_fail1;
  587. skb_set_transport_header(skb, transhdr_len);
  588. memcpy(reqctx->iv, req->info, ivsize);
  589. write_buffer_to_skb(skb, &frags, reqctx->iv, ivsize);
  590. write_sg_to_skb(skb, &frags, req->src, req->nbytes);
  591. create_wreq(ctx, chcr_req, req, skb, kctx_len, 0, 1,
  592. sizeof(struct cpl_rx_phys_dsgl) + phys_dsgl);
  593. reqctx->skb = skb;
  594. skb_get(skb);
  595. return skb;
  596. map_fail1:
  597. kfree_skb(skb);
  598. return ERR_PTR(-ENOMEM);
  599. }
  600. static int chcr_aes_cbc_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  601. unsigned int keylen)
  602. {
  603. struct chcr_context *ctx = crypto_ablkcipher_ctx(tfm);
  604. struct ablk_ctx *ablkctx = ABLK_CTX(ctx);
  605. unsigned int ck_size, context_size;
  606. u16 alignment = 0;
  607. if (keylen == AES_KEYSIZE_128) {
  608. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
  609. } else if (keylen == AES_KEYSIZE_192) {
  610. alignment = 8;
  611. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
  612. } else if (keylen == AES_KEYSIZE_256) {
  613. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
  614. } else {
  615. goto badkey_err;
  616. }
  617. memcpy(ablkctx->key, key, keylen);
  618. ablkctx->enckey_len = keylen;
  619. get_aes_decrypt_key(ablkctx->rrkey, ablkctx->key, keylen << 3);
  620. context_size = (KEY_CONTEXT_HDR_SALT_AND_PAD +
  621. keylen + alignment) >> 4;
  622. ablkctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, CHCR_KEYCTX_NO_KEY,
  623. 0, 0, context_size);
  624. ablkctx->ciph_mode = CHCR_SCMD_CIPHER_MODE_AES_CBC;
  625. return 0;
  626. badkey_err:
  627. crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  628. ablkctx->enckey_len = 0;
  629. return -EINVAL;
  630. }
  631. static int cxgb4_is_crypto_q_full(struct net_device *dev, unsigned int idx)
  632. {
  633. struct adapter *adap = netdev2adap(dev);
  634. struct sge_uld_txq_info *txq_info =
  635. adap->sge.uld_txq_info[CXGB4_TX_CRYPTO];
  636. struct sge_uld_txq *txq;
  637. int ret = 0;
  638. local_bh_disable();
  639. txq = &txq_info->uldtxq[idx];
  640. spin_lock(&txq->sendq.lock);
  641. if (txq->full)
  642. ret = -1;
  643. spin_unlock(&txq->sendq.lock);
  644. local_bh_enable();
  645. return ret;
  646. }
  647. static int chcr_aes_encrypt(struct ablkcipher_request *req)
  648. {
  649. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  650. struct chcr_context *ctx = crypto_ablkcipher_ctx(tfm);
  651. struct uld_ctx *u_ctx = ULD_CTX(ctx);
  652. struct sk_buff *skb;
  653. if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
  654. ctx->tx_channel_id))) {
  655. if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
  656. return -EBUSY;
  657. }
  658. skb = create_cipher_wr(req, u_ctx->lldi.rxq_ids[ctx->tx_channel_id],
  659. CHCR_ENCRYPT_OP);
  660. if (IS_ERR(skb)) {
  661. pr_err("chcr : %s : Failed to form WR. No memory\n", __func__);
  662. return PTR_ERR(skb);
  663. }
  664. skb->dev = u_ctx->lldi.ports[0];
  665. set_wr_txq(skb, CPL_PRIORITY_DATA, ctx->tx_channel_id);
  666. chcr_send_wr(skb);
  667. return -EINPROGRESS;
  668. }
  669. static int chcr_aes_decrypt(struct ablkcipher_request *req)
  670. {
  671. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  672. struct chcr_context *ctx = crypto_ablkcipher_ctx(tfm);
  673. struct uld_ctx *u_ctx = ULD_CTX(ctx);
  674. struct sk_buff *skb;
  675. if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
  676. ctx->tx_channel_id))) {
  677. if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
  678. return -EBUSY;
  679. }
  680. skb = create_cipher_wr(req, u_ctx->lldi.rxq_ids[0],
  681. CHCR_DECRYPT_OP);
  682. if (IS_ERR(skb)) {
  683. pr_err("chcr : %s : Failed to form WR. No memory\n", __func__);
  684. return PTR_ERR(skb);
  685. }
  686. skb->dev = u_ctx->lldi.ports[0];
  687. set_wr_txq(skb, CPL_PRIORITY_DATA, ctx->tx_channel_id);
  688. chcr_send_wr(skb);
  689. return -EINPROGRESS;
  690. }
  691. static int chcr_device_init(struct chcr_context *ctx)
  692. {
  693. struct uld_ctx *u_ctx;
  694. unsigned int id;
  695. int err = 0, rxq_perchan, rxq_idx;
  696. id = smp_processor_id();
  697. if (!ctx->dev) {
  698. err = assign_chcr_device(&ctx->dev);
  699. if (err) {
  700. pr_err("chcr device assignment fails\n");
  701. goto out;
  702. }
  703. u_ctx = ULD_CTX(ctx);
  704. rxq_perchan = u_ctx->lldi.nrxq / u_ctx->lldi.nchan;
  705. rxq_idx = ctx->dev->tx_channel_id * rxq_perchan;
  706. rxq_idx += id % rxq_perchan;
  707. spin_lock(&ctx->dev->lock_chcr_dev);
  708. ctx->tx_channel_id = rxq_idx;
  709. ctx->dev->tx_channel_id = !ctx->dev->tx_channel_id;
  710. ctx->dev->rx_channel_id = 0;
  711. spin_unlock(&ctx->dev->lock_chcr_dev);
  712. }
  713. out:
  714. return err;
  715. }
  716. static int chcr_cra_init(struct crypto_tfm *tfm)
  717. {
  718. tfm->crt_ablkcipher.reqsize = sizeof(struct chcr_blkcipher_req_ctx);
  719. return chcr_device_init(crypto_tfm_ctx(tfm));
  720. }
  721. static int get_alg_config(struct algo_param *params,
  722. unsigned int auth_size)
  723. {
  724. switch (auth_size) {
  725. case SHA1_DIGEST_SIZE:
  726. params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_160;
  727. params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA1;
  728. params->result_size = SHA1_DIGEST_SIZE;
  729. break;
  730. case SHA224_DIGEST_SIZE:
  731. params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_256;
  732. params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA224;
  733. params->result_size = SHA256_DIGEST_SIZE;
  734. break;
  735. case SHA256_DIGEST_SIZE:
  736. params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_256;
  737. params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA256;
  738. params->result_size = SHA256_DIGEST_SIZE;
  739. break;
  740. case SHA384_DIGEST_SIZE:
  741. params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_512;
  742. params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA512_384;
  743. params->result_size = SHA512_DIGEST_SIZE;
  744. break;
  745. case SHA512_DIGEST_SIZE:
  746. params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_512;
  747. params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA512_512;
  748. params->result_size = SHA512_DIGEST_SIZE;
  749. break;
  750. default:
  751. pr_err("chcr : ERROR, unsupported digest size\n");
  752. return -EINVAL;
  753. }
  754. return 0;
  755. }
  756. static inline void chcr_free_shash(struct crypto_shash *base_hash)
  757. {
  758. crypto_free_shash(base_hash);
  759. }
  760. /**
  761. * create_hash_wr - Create hash work request
  762. * @req - Cipher req base
  763. */
  764. static struct sk_buff *create_hash_wr(struct ahash_request *req,
  765. struct hash_wr_param *param)
  766. {
  767. struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
  768. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  769. struct chcr_context *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
  770. struct hmac_ctx *hmacctx = HMAC_CTX(ctx);
  771. struct sk_buff *skb = NULL;
  772. struct chcr_wr *chcr_req;
  773. unsigned int frags = 0, transhdr_len, iopad_alignment = 0;
  774. unsigned int digestsize = crypto_ahash_digestsize(tfm);
  775. unsigned int kctx_len = 0;
  776. u8 hash_size_in_response = 0;
  777. gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
  778. GFP_ATOMIC;
  779. iopad_alignment = KEYCTX_ALIGN_PAD(digestsize);
  780. kctx_len = param->alg_prm.result_size + iopad_alignment;
  781. if (param->opad_needed)
  782. kctx_len += param->alg_prm.result_size + iopad_alignment;
  783. if (req_ctx->result)
  784. hash_size_in_response = digestsize;
  785. else
  786. hash_size_in_response = param->alg_prm.result_size;
  787. transhdr_len = HASH_TRANSHDR_SIZE(kctx_len);
  788. skb = alloc_skb((transhdr_len + sizeof(struct sge_opaque_hdr)), flags);
  789. if (!skb)
  790. return skb;
  791. skb_reserve(skb, sizeof(struct sge_opaque_hdr));
  792. chcr_req = (struct chcr_wr *)__skb_put(skb, transhdr_len);
  793. memset(chcr_req, 0, transhdr_len);
  794. chcr_req->sec_cpl.op_ivinsrtofst =
  795. FILL_SEC_CPL_OP_IVINSR(ctx->dev->rx_channel_id, 2, 0);
  796. chcr_req->sec_cpl.pldlen = htonl(param->bfr_len + param->sg_len);
  797. chcr_req->sec_cpl.aadstart_cipherstop_hi =
  798. FILL_SEC_CPL_CIPHERSTOP_HI(0, 0, 0, 0);
  799. chcr_req->sec_cpl.cipherstop_lo_authinsert =
  800. FILL_SEC_CPL_AUTHINSERT(0, 1, 0, 0);
  801. chcr_req->sec_cpl.seqno_numivs =
  802. FILL_SEC_CPL_SCMD0_SEQNO(0, 0, 0, param->alg_prm.auth_mode,
  803. param->opad_needed, 0);
  804. chcr_req->sec_cpl.ivgen_hdrlen =
  805. FILL_SEC_CPL_IVGEN_HDRLEN(param->last, param->more, 0, 1, 0, 0);
  806. memcpy(chcr_req->key_ctx.key, req_ctx->partial_hash,
  807. param->alg_prm.result_size);
  808. if (param->opad_needed)
  809. memcpy(chcr_req->key_ctx.key +
  810. ((param->alg_prm.result_size <= 32) ? 32 :
  811. CHCR_HASH_MAX_DIGEST_SIZE),
  812. hmacctx->opad, param->alg_prm.result_size);
  813. chcr_req->key_ctx.ctx_hdr = FILL_KEY_CTX_HDR(CHCR_KEYCTX_NO_KEY,
  814. param->alg_prm.mk_size, 0,
  815. param->opad_needed,
  816. ((kctx_len +
  817. sizeof(chcr_req->key_ctx)) >> 4));
  818. chcr_req->sec_cpl.scmd1 = cpu_to_be64((u64)param->scmd1);
  819. skb_set_transport_header(skb, transhdr_len);
  820. if (param->bfr_len != 0)
  821. write_buffer_to_skb(skb, &frags, req_ctx->reqbfr,
  822. param->bfr_len);
  823. if (param->sg_len != 0)
  824. write_sg_to_skb(skb, &frags, req->src, param->sg_len);
  825. create_wreq(ctx, chcr_req, req, skb, kctx_len, hash_size_in_response, 0,
  826. DUMMY_BYTES);
  827. req_ctx->skb = skb;
  828. skb_get(skb);
  829. return skb;
  830. }
  831. static int chcr_ahash_update(struct ahash_request *req)
  832. {
  833. struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
  834. struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
  835. struct chcr_context *ctx = crypto_tfm_ctx(crypto_ahash_tfm(rtfm));
  836. struct uld_ctx *u_ctx = NULL;
  837. struct sk_buff *skb;
  838. u8 remainder = 0, bs;
  839. unsigned int nbytes = req->nbytes;
  840. struct hash_wr_param params;
  841. bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
  842. u_ctx = ULD_CTX(ctx);
  843. if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
  844. ctx->tx_channel_id))) {
  845. if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
  846. return -EBUSY;
  847. }
  848. if (nbytes + req_ctx->reqlen >= bs) {
  849. remainder = (nbytes + req_ctx->reqlen) % bs;
  850. nbytes = nbytes + req_ctx->reqlen - remainder;
  851. } else {
  852. sg_pcopy_to_buffer(req->src, sg_nents(req->src), req_ctx->reqbfr
  853. + req_ctx->reqlen, nbytes, 0);
  854. req_ctx->reqlen += nbytes;
  855. return 0;
  856. }
  857. params.opad_needed = 0;
  858. params.more = 1;
  859. params.last = 0;
  860. params.sg_len = nbytes - req_ctx->reqlen;
  861. params.bfr_len = req_ctx->reqlen;
  862. params.scmd1 = 0;
  863. get_alg_config(&params.alg_prm, crypto_ahash_digestsize(rtfm));
  864. req_ctx->result = 0;
  865. req_ctx->data_len += params.sg_len + params.bfr_len;
  866. skb = create_hash_wr(req, &params);
  867. if (!skb)
  868. return -ENOMEM;
  869. if (remainder) {
  870. u8 *temp;
  871. /* Swap buffers */
  872. temp = req_ctx->reqbfr;
  873. req_ctx->reqbfr = req_ctx->skbfr;
  874. req_ctx->skbfr = temp;
  875. sg_pcopy_to_buffer(req->src, sg_nents(req->src),
  876. req_ctx->reqbfr, remainder, req->nbytes -
  877. remainder);
  878. }
  879. req_ctx->reqlen = remainder;
  880. skb->dev = u_ctx->lldi.ports[0];
  881. set_wr_txq(skb, CPL_PRIORITY_DATA, ctx->tx_channel_id);
  882. chcr_send_wr(skb);
  883. return -EINPROGRESS;
  884. }
  885. static void create_last_hash_block(char *bfr_ptr, unsigned int bs, u64 scmd1)
  886. {
  887. memset(bfr_ptr, 0, bs);
  888. *bfr_ptr = 0x80;
  889. if (bs == 64)
  890. *(__be64 *)(bfr_ptr + 56) = cpu_to_be64(scmd1 << 3);
  891. else
  892. *(__be64 *)(bfr_ptr + 120) = cpu_to_be64(scmd1 << 3);
  893. }
  894. static int chcr_ahash_final(struct ahash_request *req)
  895. {
  896. struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
  897. struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
  898. struct chcr_context *ctx = crypto_tfm_ctx(crypto_ahash_tfm(rtfm));
  899. struct hash_wr_param params;
  900. struct sk_buff *skb;
  901. struct uld_ctx *u_ctx = NULL;
  902. u8 bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
  903. u_ctx = ULD_CTX(ctx);
  904. if (is_hmac(crypto_ahash_tfm(rtfm)))
  905. params.opad_needed = 1;
  906. else
  907. params.opad_needed = 0;
  908. params.sg_len = 0;
  909. get_alg_config(&params.alg_prm, crypto_ahash_digestsize(rtfm));
  910. req_ctx->result = 1;
  911. params.bfr_len = req_ctx->reqlen;
  912. req_ctx->data_len += params.bfr_len + params.sg_len;
  913. if (req_ctx->reqlen == 0) {
  914. create_last_hash_block(req_ctx->reqbfr, bs, req_ctx->data_len);
  915. params.last = 0;
  916. params.more = 1;
  917. params.scmd1 = 0;
  918. params.bfr_len = bs;
  919. } else {
  920. params.scmd1 = req_ctx->data_len;
  921. params.last = 1;
  922. params.more = 0;
  923. }
  924. skb = create_hash_wr(req, &params);
  925. if (!skb)
  926. return -ENOMEM;
  927. skb->dev = u_ctx->lldi.ports[0];
  928. set_wr_txq(skb, CPL_PRIORITY_DATA, ctx->tx_channel_id);
  929. chcr_send_wr(skb);
  930. return -EINPROGRESS;
  931. }
  932. static int chcr_ahash_finup(struct ahash_request *req)
  933. {
  934. struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
  935. struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
  936. struct chcr_context *ctx = crypto_tfm_ctx(crypto_ahash_tfm(rtfm));
  937. struct uld_ctx *u_ctx = NULL;
  938. struct sk_buff *skb;
  939. struct hash_wr_param params;
  940. u8 bs;
  941. bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
  942. u_ctx = ULD_CTX(ctx);
  943. if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
  944. ctx->tx_channel_id))) {
  945. if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
  946. return -EBUSY;
  947. }
  948. if (is_hmac(crypto_ahash_tfm(rtfm)))
  949. params.opad_needed = 1;
  950. else
  951. params.opad_needed = 0;
  952. params.sg_len = req->nbytes;
  953. params.bfr_len = req_ctx->reqlen;
  954. get_alg_config(&params.alg_prm, crypto_ahash_digestsize(rtfm));
  955. req_ctx->data_len += params.bfr_len + params.sg_len;
  956. req_ctx->result = 1;
  957. if ((req_ctx->reqlen + req->nbytes) == 0) {
  958. create_last_hash_block(req_ctx->reqbfr, bs, req_ctx->data_len);
  959. params.last = 0;
  960. params.more = 1;
  961. params.scmd1 = 0;
  962. params.bfr_len = bs;
  963. } else {
  964. params.scmd1 = req_ctx->data_len;
  965. params.last = 1;
  966. params.more = 0;
  967. }
  968. skb = create_hash_wr(req, &params);
  969. if (!skb)
  970. return -ENOMEM;
  971. skb->dev = u_ctx->lldi.ports[0];
  972. set_wr_txq(skb, CPL_PRIORITY_DATA, ctx->tx_channel_id);
  973. chcr_send_wr(skb);
  974. return -EINPROGRESS;
  975. }
  976. static int chcr_ahash_digest(struct ahash_request *req)
  977. {
  978. struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
  979. struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
  980. struct chcr_context *ctx = crypto_tfm_ctx(crypto_ahash_tfm(rtfm));
  981. struct uld_ctx *u_ctx = NULL;
  982. struct sk_buff *skb;
  983. struct hash_wr_param params;
  984. u8 bs;
  985. rtfm->init(req);
  986. bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
  987. u_ctx = ULD_CTX(ctx);
  988. if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
  989. ctx->tx_channel_id))) {
  990. if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
  991. return -EBUSY;
  992. }
  993. if (is_hmac(crypto_ahash_tfm(rtfm)))
  994. params.opad_needed = 1;
  995. else
  996. params.opad_needed = 0;
  997. params.last = 0;
  998. params.more = 0;
  999. params.sg_len = req->nbytes;
  1000. params.bfr_len = 0;
  1001. params.scmd1 = 0;
  1002. get_alg_config(&params.alg_prm, crypto_ahash_digestsize(rtfm));
  1003. req_ctx->result = 1;
  1004. req_ctx->data_len += params.bfr_len + params.sg_len;
  1005. if (req->nbytes == 0) {
  1006. create_last_hash_block(req_ctx->reqbfr, bs, 0);
  1007. params.more = 1;
  1008. params.bfr_len = bs;
  1009. }
  1010. skb = create_hash_wr(req, &params);
  1011. if (!skb)
  1012. return -ENOMEM;
  1013. skb->dev = u_ctx->lldi.ports[0];
  1014. set_wr_txq(skb, CPL_PRIORITY_DATA, ctx->tx_channel_id);
  1015. chcr_send_wr(skb);
  1016. return -EINPROGRESS;
  1017. }
  1018. static int chcr_ahash_export(struct ahash_request *areq, void *out)
  1019. {
  1020. struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  1021. struct chcr_ahash_req_ctx *state = out;
  1022. state->reqlen = req_ctx->reqlen;
  1023. state->data_len = req_ctx->data_len;
  1024. memcpy(state->bfr1, req_ctx->reqbfr, req_ctx->reqlen);
  1025. memcpy(state->partial_hash, req_ctx->partial_hash,
  1026. CHCR_HASH_MAX_DIGEST_SIZE);
  1027. return 0;
  1028. }
  1029. static int chcr_ahash_import(struct ahash_request *areq, const void *in)
  1030. {
  1031. struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  1032. struct chcr_ahash_req_ctx *state = (struct chcr_ahash_req_ctx *)in;
  1033. req_ctx->reqlen = state->reqlen;
  1034. req_ctx->data_len = state->data_len;
  1035. req_ctx->reqbfr = req_ctx->bfr1;
  1036. req_ctx->skbfr = req_ctx->bfr2;
  1037. memcpy(req_ctx->bfr1, state->bfr1, CHCR_HASH_MAX_BLOCK_SIZE_128);
  1038. memcpy(req_ctx->partial_hash, state->partial_hash,
  1039. CHCR_HASH_MAX_DIGEST_SIZE);
  1040. return 0;
  1041. }
  1042. static int chcr_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
  1043. unsigned int keylen)
  1044. {
  1045. struct chcr_context *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
  1046. struct hmac_ctx *hmacctx = HMAC_CTX(ctx);
  1047. unsigned int digestsize = crypto_ahash_digestsize(tfm);
  1048. unsigned int bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
  1049. unsigned int i, err = 0, updated_digestsize;
  1050. SHASH_DESC_ON_STACK(shash, hmacctx->base_hash);
  1051. /* use the key to calculate the ipad and opad. ipad will sent with the
  1052. * first request's data. opad will be sent with the final hash result
  1053. * ipad in hmacctx->ipad and opad in hmacctx->opad location
  1054. */
  1055. shash->tfm = hmacctx->base_hash;
  1056. shash->flags = crypto_shash_get_flags(hmacctx->base_hash);
  1057. if (keylen > bs) {
  1058. err = crypto_shash_digest(shash, key, keylen,
  1059. hmacctx->ipad);
  1060. if (err)
  1061. goto out;
  1062. keylen = digestsize;
  1063. } else {
  1064. memcpy(hmacctx->ipad, key, keylen);
  1065. }
  1066. memset(hmacctx->ipad + keylen, 0, bs - keylen);
  1067. memcpy(hmacctx->opad, hmacctx->ipad, bs);
  1068. for (i = 0; i < bs / sizeof(int); i++) {
  1069. *((unsigned int *)(&hmacctx->ipad) + i) ^= IPAD_DATA;
  1070. *((unsigned int *)(&hmacctx->opad) + i) ^= OPAD_DATA;
  1071. }
  1072. updated_digestsize = digestsize;
  1073. if (digestsize == SHA224_DIGEST_SIZE)
  1074. updated_digestsize = SHA256_DIGEST_SIZE;
  1075. else if (digestsize == SHA384_DIGEST_SIZE)
  1076. updated_digestsize = SHA512_DIGEST_SIZE;
  1077. err = chcr_compute_partial_hash(shash, hmacctx->ipad,
  1078. hmacctx->ipad, digestsize);
  1079. if (err)
  1080. goto out;
  1081. chcr_change_order(hmacctx->ipad, updated_digestsize);
  1082. err = chcr_compute_partial_hash(shash, hmacctx->opad,
  1083. hmacctx->opad, digestsize);
  1084. if (err)
  1085. goto out;
  1086. chcr_change_order(hmacctx->opad, updated_digestsize);
  1087. out:
  1088. return err;
  1089. }
  1090. static int chcr_aes_xts_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  1091. unsigned int key_len)
  1092. {
  1093. struct chcr_context *ctx = crypto_ablkcipher_ctx(tfm);
  1094. struct ablk_ctx *ablkctx = ABLK_CTX(ctx);
  1095. unsigned short context_size = 0;
  1096. if ((key_len != (AES_KEYSIZE_128 << 1)) &&
  1097. (key_len != (AES_KEYSIZE_256 << 1))) {
  1098. crypto_tfm_set_flags((struct crypto_tfm *)tfm,
  1099. CRYPTO_TFM_RES_BAD_KEY_LEN);
  1100. ablkctx->enckey_len = 0;
  1101. return -EINVAL;
  1102. }
  1103. memcpy(ablkctx->key, key, key_len);
  1104. ablkctx->enckey_len = key_len;
  1105. get_aes_decrypt_key(ablkctx->rrkey, ablkctx->key, key_len << 2);
  1106. context_size = (KEY_CONTEXT_HDR_SALT_AND_PAD + key_len) >> 4;
  1107. ablkctx->key_ctx_hdr =
  1108. FILL_KEY_CTX_HDR((key_len == AES_KEYSIZE_256) ?
  1109. CHCR_KEYCTX_CIPHER_KEY_SIZE_128 :
  1110. CHCR_KEYCTX_CIPHER_KEY_SIZE_256,
  1111. CHCR_KEYCTX_NO_KEY, 1,
  1112. 0, context_size);
  1113. ablkctx->ciph_mode = CHCR_SCMD_CIPHER_MODE_AES_XTS;
  1114. return 0;
  1115. }
  1116. static int chcr_sha_init(struct ahash_request *areq)
  1117. {
  1118. struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  1119. struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
  1120. int digestsize = crypto_ahash_digestsize(tfm);
  1121. req_ctx->data_len = 0;
  1122. req_ctx->reqlen = 0;
  1123. req_ctx->reqbfr = req_ctx->bfr1;
  1124. req_ctx->skbfr = req_ctx->bfr2;
  1125. req_ctx->skb = NULL;
  1126. req_ctx->result = 0;
  1127. copy_hash_init_values(req_ctx->partial_hash, digestsize);
  1128. return 0;
  1129. }
  1130. static int chcr_sha_cra_init(struct crypto_tfm *tfm)
  1131. {
  1132. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  1133. sizeof(struct chcr_ahash_req_ctx));
  1134. return chcr_device_init(crypto_tfm_ctx(tfm));
  1135. }
  1136. static int chcr_hmac_init(struct ahash_request *areq)
  1137. {
  1138. struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  1139. struct crypto_ahash *rtfm = crypto_ahash_reqtfm(areq);
  1140. struct chcr_context *ctx = crypto_tfm_ctx(crypto_ahash_tfm(rtfm));
  1141. struct hmac_ctx *hmacctx = HMAC_CTX(ctx);
  1142. unsigned int digestsize = crypto_ahash_digestsize(rtfm);
  1143. unsigned int bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
  1144. chcr_sha_init(areq);
  1145. req_ctx->data_len = bs;
  1146. if (is_hmac(crypto_ahash_tfm(rtfm))) {
  1147. if (digestsize == SHA224_DIGEST_SIZE)
  1148. memcpy(req_ctx->partial_hash, hmacctx->ipad,
  1149. SHA256_DIGEST_SIZE);
  1150. else if (digestsize == SHA384_DIGEST_SIZE)
  1151. memcpy(req_ctx->partial_hash, hmacctx->ipad,
  1152. SHA512_DIGEST_SIZE);
  1153. else
  1154. memcpy(req_ctx->partial_hash, hmacctx->ipad,
  1155. digestsize);
  1156. }
  1157. return 0;
  1158. }
  1159. static int chcr_hmac_cra_init(struct crypto_tfm *tfm)
  1160. {
  1161. struct chcr_context *ctx = crypto_tfm_ctx(tfm);
  1162. struct hmac_ctx *hmacctx = HMAC_CTX(ctx);
  1163. unsigned int digestsize =
  1164. crypto_ahash_digestsize(__crypto_ahash_cast(tfm));
  1165. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  1166. sizeof(struct chcr_ahash_req_ctx));
  1167. hmacctx->base_hash = chcr_alloc_shash(digestsize);
  1168. if (IS_ERR(hmacctx->base_hash))
  1169. return PTR_ERR(hmacctx->base_hash);
  1170. return chcr_device_init(crypto_tfm_ctx(tfm));
  1171. }
  1172. static void chcr_hmac_cra_exit(struct crypto_tfm *tfm)
  1173. {
  1174. struct chcr_context *ctx = crypto_tfm_ctx(tfm);
  1175. struct hmac_ctx *hmacctx = HMAC_CTX(ctx);
  1176. if (hmacctx->base_hash) {
  1177. chcr_free_shash(hmacctx->base_hash);
  1178. hmacctx->base_hash = NULL;
  1179. }
  1180. }
  1181. static int chcr_copy_assoc(struct aead_request *req,
  1182. struct chcr_aead_ctx *ctx)
  1183. {
  1184. SKCIPHER_REQUEST_ON_STACK(skreq, ctx->null);
  1185. skcipher_request_set_tfm(skreq, ctx->null);
  1186. skcipher_request_set_callback(skreq, aead_request_flags(req),
  1187. NULL, NULL);
  1188. skcipher_request_set_crypt(skreq, req->src, req->dst, req->assoclen,
  1189. NULL);
  1190. return crypto_skcipher_encrypt(skreq);
  1191. }
  1192. static unsigned char get_hmac(unsigned int authsize)
  1193. {
  1194. switch (authsize) {
  1195. case ICV_8:
  1196. return CHCR_SCMD_HMAC_CTRL_PL1;
  1197. case ICV_10:
  1198. return CHCR_SCMD_HMAC_CTRL_TRUNC_RFC4366;
  1199. case ICV_12:
  1200. return CHCR_SCMD_HMAC_CTRL_IPSEC_96BIT;
  1201. }
  1202. return CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
  1203. }
  1204. static struct sk_buff *create_authenc_wr(struct aead_request *req,
  1205. unsigned short qid,
  1206. int size,
  1207. unsigned short op_type)
  1208. {
  1209. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1210. struct chcr_context *ctx = crypto_aead_ctx(tfm);
  1211. struct uld_ctx *u_ctx = ULD_CTX(ctx);
  1212. struct chcr_aead_ctx *aeadctx = AEAD_CTX(ctx);
  1213. struct chcr_authenc_ctx *actx = AUTHENC_CTX(aeadctx);
  1214. struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
  1215. struct sk_buff *skb = NULL;
  1216. struct chcr_wr *chcr_req;
  1217. struct cpl_rx_phys_dsgl *phys_cpl;
  1218. struct phys_sge_parm sg_param;
  1219. struct scatterlist *src;
  1220. unsigned int frags = 0, transhdr_len;
  1221. unsigned int ivsize = crypto_aead_ivsize(tfm), dst_size = 0;
  1222. unsigned int kctx_len = 0;
  1223. unsigned short stop_offset = 0;
  1224. unsigned int assoclen = req->assoclen;
  1225. unsigned int authsize = crypto_aead_authsize(tfm);
  1226. int err = 0;
  1227. int null = 0;
  1228. gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
  1229. GFP_ATOMIC;
  1230. if (aeadctx->enckey_len == 0 || (req->cryptlen == 0))
  1231. goto err;
  1232. if (op_type && req->cryptlen < crypto_aead_authsize(tfm))
  1233. goto err;
  1234. if (sg_nents_for_len(req->src, req->assoclen + req->cryptlen) < 0)
  1235. goto err;
  1236. src = scatterwalk_ffwd(reqctx->srcffwd, req->src, req->assoclen);
  1237. reqctx->dst = src;
  1238. if (req->src != req->dst) {
  1239. err = chcr_copy_assoc(req, aeadctx);
  1240. if (err)
  1241. return ERR_PTR(err);
  1242. reqctx->dst = scatterwalk_ffwd(reqctx->dstffwd, req->dst,
  1243. req->assoclen);
  1244. }
  1245. if (get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_NULL) {
  1246. null = 1;
  1247. assoclen = 0;
  1248. }
  1249. reqctx->dst_nents = sg_nents_for_len(reqctx->dst, req->cryptlen +
  1250. (op_type ? -authsize : authsize));
  1251. if (reqctx->dst_nents <= 0) {
  1252. pr_err("AUTHENC:Invalid Destination sg entries\n");
  1253. goto err;
  1254. }
  1255. dst_size = get_space_for_phys_dsgl(reqctx->dst_nents);
  1256. kctx_len = (ntohl(KEY_CONTEXT_CTX_LEN_V(aeadctx->key_ctx_hdr)) << 4)
  1257. - sizeof(chcr_req->key_ctx);
  1258. transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, dst_size);
  1259. skb = alloc_skb((transhdr_len + sizeof(struct sge_opaque_hdr)), flags);
  1260. if (!skb)
  1261. goto err;
  1262. /* LLD is going to write the sge hdr. */
  1263. skb_reserve(skb, sizeof(struct sge_opaque_hdr));
  1264. /* Write WR */
  1265. chcr_req = (struct chcr_wr *) __skb_put(skb, transhdr_len);
  1266. memset(chcr_req, 0, transhdr_len);
  1267. stop_offset = (op_type == CHCR_ENCRYPT_OP) ? 0 : authsize;
  1268. /*
  1269. * Input order is AAD,IV and Payload. where IV should be included as
  1270. * the part of authdata. All other fields should be filled according
  1271. * to the hardware spec
  1272. */
  1273. chcr_req->sec_cpl.op_ivinsrtofst =
  1274. FILL_SEC_CPL_OP_IVINSR(ctx->dev->rx_channel_id, 2,
  1275. (ivsize ? (assoclen + 1) : 0));
  1276. chcr_req->sec_cpl.pldlen = htonl(assoclen + ivsize + req->cryptlen);
  1277. chcr_req->sec_cpl.aadstart_cipherstop_hi = FILL_SEC_CPL_CIPHERSTOP_HI(
  1278. assoclen ? 1 : 0, assoclen,
  1279. assoclen + ivsize + 1,
  1280. (stop_offset & 0x1F0) >> 4);
  1281. chcr_req->sec_cpl.cipherstop_lo_authinsert = FILL_SEC_CPL_AUTHINSERT(
  1282. stop_offset & 0xF,
  1283. null ? 0 : assoclen + ivsize + 1,
  1284. stop_offset, stop_offset);
  1285. chcr_req->sec_cpl.seqno_numivs = FILL_SEC_CPL_SCMD0_SEQNO(op_type,
  1286. (op_type == CHCR_ENCRYPT_OP) ? 1 : 0,
  1287. CHCR_SCMD_CIPHER_MODE_AES_CBC,
  1288. actx->auth_mode, aeadctx->hmac_ctrl,
  1289. ivsize >> 1);
  1290. chcr_req->sec_cpl.ivgen_hdrlen = FILL_SEC_CPL_IVGEN_HDRLEN(0, 0, 1,
  1291. 0, 1, dst_size);
  1292. chcr_req->key_ctx.ctx_hdr = aeadctx->key_ctx_hdr;
  1293. if (op_type == CHCR_ENCRYPT_OP)
  1294. memcpy(chcr_req->key_ctx.key, aeadctx->key,
  1295. aeadctx->enckey_len);
  1296. else
  1297. memcpy(chcr_req->key_ctx.key, actx->dec_rrkey,
  1298. aeadctx->enckey_len);
  1299. memcpy(chcr_req->key_ctx.key + (DIV_ROUND_UP(aeadctx->enckey_len, 16) <<
  1300. 4), actx->h_iopad, kctx_len -
  1301. (DIV_ROUND_UP(aeadctx->enckey_len, 16) << 4));
  1302. phys_cpl = (struct cpl_rx_phys_dsgl *)((u8 *)(chcr_req + 1) + kctx_len);
  1303. sg_param.nents = reqctx->dst_nents;
  1304. sg_param.obsize = req->cryptlen + (op_type ? -authsize : authsize);
  1305. sg_param.qid = qid;
  1306. sg_param.align = 0;
  1307. if (map_writesg_phys_cpl(&u_ctx->lldi.pdev->dev, phys_cpl, reqctx->dst,
  1308. &sg_param))
  1309. goto dstmap_fail;
  1310. skb_set_transport_header(skb, transhdr_len);
  1311. if (assoclen) {
  1312. /* AAD buffer in */
  1313. write_sg_to_skb(skb, &frags, req->src, assoclen);
  1314. }
  1315. write_buffer_to_skb(skb, &frags, req->iv, ivsize);
  1316. write_sg_to_skb(skb, &frags, src, req->cryptlen);
  1317. create_wreq(ctx, chcr_req, req, skb, kctx_len, size, 1,
  1318. sizeof(struct cpl_rx_phys_dsgl) + dst_size);
  1319. reqctx->skb = skb;
  1320. skb_get(skb);
  1321. return skb;
  1322. dstmap_fail:
  1323. /* ivmap_fail: */
  1324. kfree_skb(skb);
  1325. err:
  1326. return ERR_PTR(-EINVAL);
  1327. }
  1328. static void aes_gcm_empty_pld_pad(struct scatterlist *sg,
  1329. unsigned short offset)
  1330. {
  1331. struct page *spage;
  1332. unsigned char *addr;
  1333. spage = sg_page(sg);
  1334. get_page(spage); /* so that it is not freed by NIC */
  1335. #ifdef KMAP_ATOMIC_ARGS
  1336. addr = kmap_atomic(spage, KM_SOFTIRQ0);
  1337. #else
  1338. addr = kmap_atomic(spage);
  1339. #endif
  1340. memset(addr + sg->offset, 0, offset + 1);
  1341. kunmap_atomic(addr);
  1342. }
  1343. static int set_msg_len(u8 *block, unsigned int msglen, int csize)
  1344. {
  1345. __be32 data;
  1346. memset(block, 0, csize);
  1347. block += csize;
  1348. if (csize >= 4)
  1349. csize = 4;
  1350. else if (msglen > (unsigned int)(1 << (8 * csize)))
  1351. return -EOVERFLOW;
  1352. data = cpu_to_be32(msglen);
  1353. memcpy(block - csize, (u8 *)&data + 4 - csize, csize);
  1354. return 0;
  1355. }
  1356. static void generate_b0(struct aead_request *req,
  1357. struct chcr_aead_ctx *aeadctx,
  1358. unsigned short op_type)
  1359. {
  1360. unsigned int l, lp, m;
  1361. int rc;
  1362. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  1363. struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
  1364. u8 *b0 = reqctx->scratch_pad;
  1365. m = crypto_aead_authsize(aead);
  1366. memcpy(b0, reqctx->iv, 16);
  1367. lp = b0[0];
  1368. l = lp + 1;
  1369. /* set m, bits 3-5 */
  1370. *b0 |= (8 * ((m - 2) / 2));
  1371. /* set adata, bit 6, if associated data is used */
  1372. if (req->assoclen)
  1373. *b0 |= 64;
  1374. rc = set_msg_len(b0 + 16 - l,
  1375. (op_type == CHCR_DECRYPT_OP) ?
  1376. req->cryptlen - m : req->cryptlen, l);
  1377. }
  1378. static inline int crypto_ccm_check_iv(const u8 *iv)
  1379. {
  1380. /* 2 <= L <= 8, so 1 <= L' <= 7. */
  1381. if (iv[0] < 1 || iv[0] > 7)
  1382. return -EINVAL;
  1383. return 0;
  1384. }
  1385. static int ccm_format_packet(struct aead_request *req,
  1386. struct chcr_aead_ctx *aeadctx,
  1387. unsigned int sub_type,
  1388. unsigned short op_type)
  1389. {
  1390. struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
  1391. int rc = 0;
  1392. if (req->assoclen > T5_MAX_AAD_SIZE) {
  1393. pr_err("CCM: Unsupported AAD data. It should be < %d\n",
  1394. T5_MAX_AAD_SIZE);
  1395. return -EINVAL;
  1396. }
  1397. if (sub_type == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309) {
  1398. reqctx->iv[0] = 3;
  1399. memcpy(reqctx->iv + 1, &aeadctx->salt[0], 3);
  1400. memcpy(reqctx->iv + 4, req->iv, 8);
  1401. memset(reqctx->iv + 12, 0, 4);
  1402. *((unsigned short *)(reqctx->scratch_pad + 16)) =
  1403. htons(req->assoclen - 8);
  1404. } else {
  1405. memcpy(reqctx->iv, req->iv, 16);
  1406. *((unsigned short *)(reqctx->scratch_pad + 16)) =
  1407. htons(req->assoclen);
  1408. }
  1409. generate_b0(req, aeadctx, op_type);
  1410. /* zero the ctr value */
  1411. memset(reqctx->iv + 15 - reqctx->iv[0], 0, reqctx->iv[0] + 1);
  1412. return rc;
  1413. }
  1414. static void fill_sec_cpl_for_aead(struct cpl_tx_sec_pdu *sec_cpl,
  1415. unsigned int dst_size,
  1416. struct aead_request *req,
  1417. unsigned short op_type,
  1418. struct chcr_context *chcrctx)
  1419. {
  1420. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1421. unsigned int ivsize = AES_BLOCK_SIZE;
  1422. unsigned int cipher_mode = CHCR_SCMD_CIPHER_MODE_AES_CCM;
  1423. unsigned int mac_mode = CHCR_SCMD_AUTH_MODE_CBCMAC;
  1424. unsigned int c_id = chcrctx->dev->rx_channel_id;
  1425. unsigned int ccm_xtra;
  1426. unsigned char tag_offset = 0, auth_offset = 0;
  1427. unsigned char hmac_ctrl = get_hmac(crypto_aead_authsize(tfm));
  1428. unsigned int assoclen;
  1429. if (get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309)
  1430. assoclen = req->assoclen - 8;
  1431. else
  1432. assoclen = req->assoclen;
  1433. ccm_xtra = CCM_B0_SIZE +
  1434. ((assoclen) ? CCM_AAD_FIELD_SIZE : 0);
  1435. auth_offset = req->cryptlen ?
  1436. (assoclen + ivsize + 1 + ccm_xtra) : 0;
  1437. if (op_type == CHCR_DECRYPT_OP) {
  1438. if (crypto_aead_authsize(tfm) != req->cryptlen)
  1439. tag_offset = crypto_aead_authsize(tfm);
  1440. else
  1441. auth_offset = 0;
  1442. }
  1443. sec_cpl->op_ivinsrtofst = FILL_SEC_CPL_OP_IVINSR(c_id,
  1444. 2, (ivsize ? (assoclen + 1) : 0) +
  1445. ccm_xtra);
  1446. sec_cpl->pldlen =
  1447. htonl(assoclen + ivsize + req->cryptlen + ccm_xtra);
  1448. /* For CCM there wil be b0 always. So AAD start will be 1 always */
  1449. sec_cpl->aadstart_cipherstop_hi = FILL_SEC_CPL_CIPHERSTOP_HI(
  1450. 1, assoclen + ccm_xtra, assoclen
  1451. + ivsize + 1 + ccm_xtra, 0);
  1452. sec_cpl->cipherstop_lo_authinsert = FILL_SEC_CPL_AUTHINSERT(0,
  1453. auth_offset, tag_offset,
  1454. (op_type == CHCR_ENCRYPT_OP) ? 0 :
  1455. crypto_aead_authsize(tfm));
  1456. sec_cpl->seqno_numivs = FILL_SEC_CPL_SCMD0_SEQNO(op_type,
  1457. (op_type == CHCR_ENCRYPT_OP) ? 0 : 1,
  1458. cipher_mode, mac_mode, hmac_ctrl,
  1459. ivsize >> 1);
  1460. sec_cpl->ivgen_hdrlen = FILL_SEC_CPL_IVGEN_HDRLEN(0, 0, 1, 0,
  1461. 1, dst_size);
  1462. }
  1463. int aead_ccm_validate_input(unsigned short op_type,
  1464. struct aead_request *req,
  1465. struct chcr_aead_ctx *aeadctx,
  1466. unsigned int sub_type)
  1467. {
  1468. if (sub_type != CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309) {
  1469. if (crypto_ccm_check_iv(req->iv)) {
  1470. pr_err("CCM: IV check fails\n");
  1471. return -EINVAL;
  1472. }
  1473. } else {
  1474. if (req->assoclen != 16 && req->assoclen != 20) {
  1475. pr_err("RFC4309: Invalid AAD length %d\n",
  1476. req->assoclen);
  1477. return -EINVAL;
  1478. }
  1479. }
  1480. if (aeadctx->enckey_len == 0) {
  1481. pr_err("CCM: Encryption key not set\n");
  1482. return -EINVAL;
  1483. }
  1484. return 0;
  1485. }
  1486. unsigned int fill_aead_req_fields(struct sk_buff *skb,
  1487. struct aead_request *req,
  1488. struct scatterlist *src,
  1489. unsigned int ivsize,
  1490. struct chcr_aead_ctx *aeadctx)
  1491. {
  1492. unsigned int frags = 0;
  1493. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1494. struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
  1495. /* b0 and aad length(if available) */
  1496. write_buffer_to_skb(skb, &frags, reqctx->scratch_pad, CCM_B0_SIZE +
  1497. (req->assoclen ? CCM_AAD_FIELD_SIZE : 0));
  1498. if (req->assoclen) {
  1499. if (get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309)
  1500. write_sg_to_skb(skb, &frags, req->src,
  1501. req->assoclen - 8);
  1502. else
  1503. write_sg_to_skb(skb, &frags, req->src, req->assoclen);
  1504. }
  1505. write_buffer_to_skb(skb, &frags, reqctx->iv, ivsize);
  1506. if (req->cryptlen)
  1507. write_sg_to_skb(skb, &frags, src, req->cryptlen);
  1508. return frags;
  1509. }
  1510. static struct sk_buff *create_aead_ccm_wr(struct aead_request *req,
  1511. unsigned short qid,
  1512. int size,
  1513. unsigned short op_type)
  1514. {
  1515. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1516. struct chcr_context *ctx = crypto_aead_ctx(tfm);
  1517. struct uld_ctx *u_ctx = ULD_CTX(ctx);
  1518. struct chcr_aead_ctx *aeadctx = AEAD_CTX(ctx);
  1519. struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
  1520. struct sk_buff *skb = NULL;
  1521. struct chcr_wr *chcr_req;
  1522. struct cpl_rx_phys_dsgl *phys_cpl;
  1523. struct phys_sge_parm sg_param;
  1524. struct scatterlist *src;
  1525. unsigned int frags = 0, transhdr_len, ivsize = AES_BLOCK_SIZE;
  1526. unsigned int dst_size = 0, kctx_len;
  1527. unsigned int sub_type;
  1528. unsigned int authsize = crypto_aead_authsize(tfm);
  1529. int err = 0;
  1530. gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
  1531. GFP_ATOMIC;
  1532. if (op_type && req->cryptlen < crypto_aead_authsize(tfm))
  1533. goto err;
  1534. if (sg_nents_for_len(req->src, req->assoclen + req->cryptlen) < 0)
  1535. goto err;
  1536. sub_type = get_aead_subtype(tfm);
  1537. src = scatterwalk_ffwd(reqctx->srcffwd, req->src, req->assoclen);
  1538. reqctx->dst = src;
  1539. if (req->src != req->dst) {
  1540. err = chcr_copy_assoc(req, aeadctx);
  1541. if (err) {
  1542. pr_err("AAD copy to destination buffer fails\n");
  1543. return ERR_PTR(err);
  1544. }
  1545. reqctx->dst = scatterwalk_ffwd(reqctx->dstffwd, req->dst,
  1546. req->assoclen);
  1547. }
  1548. reqctx->dst_nents = sg_nents_for_len(reqctx->dst, req->cryptlen +
  1549. (op_type ? -authsize : authsize));
  1550. if (reqctx->dst_nents <= 0) {
  1551. pr_err("CCM:Invalid Destination sg entries\n");
  1552. goto err;
  1553. }
  1554. if (aead_ccm_validate_input(op_type, req, aeadctx, sub_type))
  1555. goto err;
  1556. dst_size = get_space_for_phys_dsgl(reqctx->dst_nents);
  1557. kctx_len = ((DIV_ROUND_UP(aeadctx->enckey_len, 16)) << 4) * 2;
  1558. transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, dst_size);
  1559. skb = alloc_skb((transhdr_len + sizeof(struct sge_opaque_hdr)), flags);
  1560. if (!skb)
  1561. goto err;
  1562. skb_reserve(skb, sizeof(struct sge_opaque_hdr));
  1563. chcr_req = (struct chcr_wr *) __skb_put(skb, transhdr_len);
  1564. memset(chcr_req, 0, transhdr_len);
  1565. fill_sec_cpl_for_aead(&chcr_req->sec_cpl, dst_size, req, op_type, ctx);
  1566. chcr_req->key_ctx.ctx_hdr = aeadctx->key_ctx_hdr;
  1567. memcpy(chcr_req->key_ctx.key, aeadctx->key, aeadctx->enckey_len);
  1568. memcpy(chcr_req->key_ctx.key + (DIV_ROUND_UP(aeadctx->enckey_len, 16) *
  1569. 16), aeadctx->key, aeadctx->enckey_len);
  1570. phys_cpl = (struct cpl_rx_phys_dsgl *)((u8 *)(chcr_req + 1) + kctx_len);
  1571. if (ccm_format_packet(req, aeadctx, sub_type, op_type))
  1572. goto dstmap_fail;
  1573. sg_param.nents = reqctx->dst_nents;
  1574. sg_param.obsize = req->cryptlen + (op_type ? -authsize : authsize);
  1575. sg_param.qid = qid;
  1576. sg_param.align = 0;
  1577. if (map_writesg_phys_cpl(&u_ctx->lldi.pdev->dev, phys_cpl, reqctx->dst,
  1578. &sg_param))
  1579. goto dstmap_fail;
  1580. skb_set_transport_header(skb, transhdr_len);
  1581. frags = fill_aead_req_fields(skb, req, src, ivsize, aeadctx);
  1582. create_wreq(ctx, chcr_req, req, skb, kctx_len, 0, 1,
  1583. sizeof(struct cpl_rx_phys_dsgl) + dst_size);
  1584. reqctx->skb = skb;
  1585. skb_get(skb);
  1586. return skb;
  1587. dstmap_fail:
  1588. kfree_skb(skb);
  1589. skb = NULL;
  1590. err:
  1591. return ERR_PTR(-EINVAL);
  1592. }
  1593. static struct sk_buff *create_gcm_wr(struct aead_request *req,
  1594. unsigned short qid,
  1595. int size,
  1596. unsigned short op_type)
  1597. {
  1598. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1599. struct chcr_context *ctx = crypto_aead_ctx(tfm);
  1600. struct uld_ctx *u_ctx = ULD_CTX(ctx);
  1601. struct chcr_aead_ctx *aeadctx = AEAD_CTX(ctx);
  1602. struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
  1603. struct sk_buff *skb = NULL;
  1604. struct chcr_wr *chcr_req;
  1605. struct cpl_rx_phys_dsgl *phys_cpl;
  1606. struct phys_sge_parm sg_param;
  1607. struct scatterlist *src;
  1608. unsigned int frags = 0, transhdr_len;
  1609. unsigned int ivsize = AES_BLOCK_SIZE;
  1610. unsigned int dst_size = 0, kctx_len;
  1611. unsigned char tag_offset = 0;
  1612. unsigned int crypt_len = 0;
  1613. unsigned int authsize = crypto_aead_authsize(tfm);
  1614. unsigned char hmac_ctrl = get_hmac(authsize);
  1615. int err = 0;
  1616. gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
  1617. GFP_ATOMIC;
  1618. /* validate key size */
  1619. if (aeadctx->enckey_len == 0)
  1620. goto err;
  1621. if (op_type && req->cryptlen < crypto_aead_authsize(tfm))
  1622. goto err;
  1623. if (sg_nents_for_len(req->src, req->assoclen + req->cryptlen) < 0)
  1624. goto err;
  1625. src = scatterwalk_ffwd(reqctx->srcffwd, req->src, req->assoclen);
  1626. reqctx->dst = src;
  1627. if (req->src != req->dst) {
  1628. err = chcr_copy_assoc(req, aeadctx);
  1629. if (err)
  1630. return ERR_PTR(err);
  1631. reqctx->dst = scatterwalk_ffwd(reqctx->dstffwd, req->dst,
  1632. req->assoclen);
  1633. }
  1634. if (!req->cryptlen)
  1635. /* null-payload is not supported in the hardware.
  1636. * software is sending block size
  1637. */
  1638. crypt_len = AES_BLOCK_SIZE;
  1639. else
  1640. crypt_len = req->cryptlen;
  1641. reqctx->dst_nents = sg_nents_for_len(reqctx->dst, req->cryptlen +
  1642. (op_type ? -authsize : authsize));
  1643. if (reqctx->dst_nents <= 0) {
  1644. pr_err("GCM:Invalid Destination sg entries\n");
  1645. goto err;
  1646. }
  1647. dst_size = get_space_for_phys_dsgl(reqctx->dst_nents);
  1648. kctx_len = ((DIV_ROUND_UP(aeadctx->enckey_len, 16)) << 4) +
  1649. AEAD_H_SIZE;
  1650. transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, dst_size);
  1651. skb = alloc_skb((transhdr_len + sizeof(struct sge_opaque_hdr)), flags);
  1652. if (!skb)
  1653. goto err;
  1654. /* NIC driver is going to write the sge hdr. */
  1655. skb_reserve(skb, sizeof(struct sge_opaque_hdr));
  1656. chcr_req = (struct chcr_wr *)__skb_put(skb, transhdr_len);
  1657. memset(chcr_req, 0, transhdr_len);
  1658. if (get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106)
  1659. req->assoclen -= 8;
  1660. tag_offset = (op_type == CHCR_ENCRYPT_OP) ? 0 : authsize;
  1661. chcr_req->sec_cpl.op_ivinsrtofst = FILL_SEC_CPL_OP_IVINSR(
  1662. ctx->dev->rx_channel_id, 2, (ivsize ?
  1663. (req->assoclen + 1) : 0));
  1664. chcr_req->sec_cpl.pldlen = htonl(req->assoclen + ivsize + crypt_len);
  1665. chcr_req->sec_cpl.aadstart_cipherstop_hi = FILL_SEC_CPL_CIPHERSTOP_HI(
  1666. req->assoclen ? 1 : 0, req->assoclen,
  1667. req->assoclen + ivsize + 1, 0);
  1668. if (req->cryptlen) {
  1669. chcr_req->sec_cpl.cipherstop_lo_authinsert =
  1670. FILL_SEC_CPL_AUTHINSERT(0, req->assoclen + ivsize + 1,
  1671. tag_offset, tag_offset);
  1672. chcr_req->sec_cpl.seqno_numivs =
  1673. FILL_SEC_CPL_SCMD0_SEQNO(op_type, (op_type ==
  1674. CHCR_ENCRYPT_OP) ? 1 : 0,
  1675. CHCR_SCMD_CIPHER_MODE_AES_GCM,
  1676. CHCR_SCMD_AUTH_MODE_GHASH, hmac_ctrl,
  1677. ivsize >> 1);
  1678. } else {
  1679. chcr_req->sec_cpl.cipherstop_lo_authinsert =
  1680. FILL_SEC_CPL_AUTHINSERT(0, 0, 0, 0);
  1681. chcr_req->sec_cpl.seqno_numivs =
  1682. FILL_SEC_CPL_SCMD0_SEQNO(op_type,
  1683. (op_type == CHCR_ENCRYPT_OP) ?
  1684. 1 : 0, CHCR_SCMD_CIPHER_MODE_AES_CBC,
  1685. 0, 0, ivsize >> 1);
  1686. }
  1687. chcr_req->sec_cpl.ivgen_hdrlen = FILL_SEC_CPL_IVGEN_HDRLEN(0, 0, 1,
  1688. 0, 1, dst_size);
  1689. chcr_req->key_ctx.ctx_hdr = aeadctx->key_ctx_hdr;
  1690. memcpy(chcr_req->key_ctx.key, aeadctx->key, aeadctx->enckey_len);
  1691. memcpy(chcr_req->key_ctx.key + (DIV_ROUND_UP(aeadctx->enckey_len, 16) *
  1692. 16), GCM_CTX(aeadctx)->ghash_h, AEAD_H_SIZE);
  1693. /* prepare a 16 byte iv */
  1694. /* S A L T | IV | 0x00000001 */
  1695. if (get_aead_subtype(tfm) ==
  1696. CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106) {
  1697. memcpy(reqctx->iv, aeadctx->salt, 4);
  1698. memcpy(reqctx->iv + 4, req->iv, 8);
  1699. } else {
  1700. memcpy(reqctx->iv, req->iv, 12);
  1701. }
  1702. *((unsigned int *)(reqctx->iv + 12)) = htonl(0x01);
  1703. phys_cpl = (struct cpl_rx_phys_dsgl *)((u8 *)(chcr_req + 1) + kctx_len);
  1704. sg_param.nents = reqctx->dst_nents;
  1705. sg_param.obsize = req->cryptlen + (op_type ? -authsize : authsize);
  1706. sg_param.qid = qid;
  1707. sg_param.align = 0;
  1708. if (map_writesg_phys_cpl(&u_ctx->lldi.pdev->dev, phys_cpl, reqctx->dst,
  1709. &sg_param))
  1710. goto dstmap_fail;
  1711. skb_set_transport_header(skb, transhdr_len);
  1712. write_sg_to_skb(skb, &frags, req->src, req->assoclen);
  1713. write_buffer_to_skb(skb, &frags, reqctx->iv, ivsize);
  1714. if (req->cryptlen) {
  1715. write_sg_to_skb(skb, &frags, src, req->cryptlen);
  1716. } else {
  1717. aes_gcm_empty_pld_pad(req->dst, authsize - 1);
  1718. write_sg_to_skb(skb, &frags, reqctx->dst, crypt_len);
  1719. }
  1720. create_wreq(ctx, chcr_req, req, skb, kctx_len, size, 1,
  1721. sizeof(struct cpl_rx_phys_dsgl) + dst_size);
  1722. reqctx->skb = skb;
  1723. skb_get(skb);
  1724. return skb;
  1725. dstmap_fail:
  1726. /* ivmap_fail: */
  1727. kfree_skb(skb);
  1728. skb = NULL;
  1729. err:
  1730. return skb;
  1731. }
  1732. static int chcr_aead_cra_init(struct crypto_aead *tfm)
  1733. {
  1734. struct chcr_context *ctx = crypto_aead_ctx(tfm);
  1735. struct chcr_aead_ctx *aeadctx = AEAD_CTX(ctx);
  1736. crypto_aead_set_reqsize(tfm, sizeof(struct chcr_aead_reqctx));
  1737. aeadctx->null = crypto_get_default_null_skcipher();
  1738. if (IS_ERR(aeadctx->null))
  1739. return PTR_ERR(aeadctx->null);
  1740. return chcr_device_init(ctx);
  1741. }
  1742. static void chcr_aead_cra_exit(struct crypto_aead *tfm)
  1743. {
  1744. crypto_put_default_null_skcipher();
  1745. }
  1746. static int chcr_authenc_null_setauthsize(struct crypto_aead *tfm,
  1747. unsigned int authsize)
  1748. {
  1749. struct chcr_aead_ctx *aeadctx = AEAD_CTX(crypto_aead_ctx(tfm));
  1750. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NOP;
  1751. aeadctx->mayverify = VERIFY_HW;
  1752. return 0;
  1753. }
  1754. static int chcr_authenc_setauthsize(struct crypto_aead *tfm,
  1755. unsigned int authsize)
  1756. {
  1757. struct chcr_aead_ctx *aeadctx = AEAD_CTX(crypto_aead_ctx(tfm));
  1758. u32 maxauth = crypto_aead_maxauthsize(tfm);
  1759. /*SHA1 authsize in ipsec is 12 instead of 10 i.e maxauthsize / 2 is not
  1760. * true for sha1. authsize == 12 condition should be before
  1761. * authsize == (maxauth >> 1)
  1762. */
  1763. if (authsize == ICV_4) {
  1764. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL1;
  1765. aeadctx->mayverify = VERIFY_HW;
  1766. } else if (authsize == ICV_6) {
  1767. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL2;
  1768. aeadctx->mayverify = VERIFY_HW;
  1769. } else if (authsize == ICV_10) {
  1770. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_TRUNC_RFC4366;
  1771. aeadctx->mayverify = VERIFY_HW;
  1772. } else if (authsize == ICV_12) {
  1773. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_IPSEC_96BIT;
  1774. aeadctx->mayverify = VERIFY_HW;
  1775. } else if (authsize == ICV_14) {
  1776. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL3;
  1777. aeadctx->mayverify = VERIFY_HW;
  1778. } else if (authsize == (maxauth >> 1)) {
  1779. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_DIV2;
  1780. aeadctx->mayverify = VERIFY_HW;
  1781. } else if (authsize == maxauth) {
  1782. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
  1783. aeadctx->mayverify = VERIFY_HW;
  1784. } else {
  1785. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
  1786. aeadctx->mayverify = VERIFY_SW;
  1787. }
  1788. return 0;
  1789. }
  1790. static int chcr_gcm_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
  1791. {
  1792. struct chcr_aead_ctx *aeadctx = AEAD_CTX(crypto_aead_ctx(tfm));
  1793. switch (authsize) {
  1794. case ICV_4:
  1795. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL1;
  1796. aeadctx->mayverify = VERIFY_HW;
  1797. break;
  1798. case ICV_8:
  1799. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_DIV2;
  1800. aeadctx->mayverify = VERIFY_HW;
  1801. break;
  1802. case ICV_12:
  1803. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_IPSEC_96BIT;
  1804. aeadctx->mayverify = VERIFY_HW;
  1805. break;
  1806. case ICV_14:
  1807. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL3;
  1808. aeadctx->mayverify = VERIFY_HW;
  1809. break;
  1810. case ICV_16:
  1811. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
  1812. aeadctx->mayverify = VERIFY_HW;
  1813. break;
  1814. case ICV_13:
  1815. case ICV_15:
  1816. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
  1817. aeadctx->mayverify = VERIFY_SW;
  1818. break;
  1819. default:
  1820. crypto_tfm_set_flags((struct crypto_tfm *) tfm,
  1821. CRYPTO_TFM_RES_BAD_KEY_LEN);
  1822. return -EINVAL;
  1823. }
  1824. return 0;
  1825. }
  1826. static int chcr_4106_4309_setauthsize(struct crypto_aead *tfm,
  1827. unsigned int authsize)
  1828. {
  1829. struct chcr_aead_ctx *aeadctx = AEAD_CTX(crypto_aead_ctx(tfm));
  1830. switch (authsize) {
  1831. case ICV_8:
  1832. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_DIV2;
  1833. aeadctx->mayverify = VERIFY_HW;
  1834. break;
  1835. case ICV_12:
  1836. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_IPSEC_96BIT;
  1837. aeadctx->mayverify = VERIFY_HW;
  1838. break;
  1839. case ICV_16:
  1840. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
  1841. aeadctx->mayverify = VERIFY_HW;
  1842. break;
  1843. default:
  1844. crypto_tfm_set_flags((struct crypto_tfm *)tfm,
  1845. CRYPTO_TFM_RES_BAD_KEY_LEN);
  1846. return -EINVAL;
  1847. }
  1848. return 0;
  1849. }
  1850. static int chcr_ccm_setauthsize(struct crypto_aead *tfm,
  1851. unsigned int authsize)
  1852. {
  1853. struct chcr_aead_ctx *aeadctx = AEAD_CTX(crypto_aead_ctx(tfm));
  1854. switch (authsize) {
  1855. case ICV_4:
  1856. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL1;
  1857. aeadctx->mayverify = VERIFY_HW;
  1858. break;
  1859. case ICV_6:
  1860. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL2;
  1861. aeadctx->mayverify = VERIFY_HW;
  1862. break;
  1863. case ICV_8:
  1864. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_DIV2;
  1865. aeadctx->mayverify = VERIFY_HW;
  1866. break;
  1867. case ICV_10:
  1868. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_TRUNC_RFC4366;
  1869. aeadctx->mayverify = VERIFY_HW;
  1870. break;
  1871. case ICV_12:
  1872. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_IPSEC_96BIT;
  1873. aeadctx->mayverify = VERIFY_HW;
  1874. break;
  1875. case ICV_14:
  1876. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL3;
  1877. aeadctx->mayverify = VERIFY_HW;
  1878. break;
  1879. case ICV_16:
  1880. aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
  1881. aeadctx->mayverify = VERIFY_HW;
  1882. break;
  1883. default:
  1884. crypto_tfm_set_flags((struct crypto_tfm *)tfm,
  1885. CRYPTO_TFM_RES_BAD_KEY_LEN);
  1886. return -EINVAL;
  1887. }
  1888. return 0;
  1889. }
  1890. static int chcr_aead_ccm_setkey(struct crypto_aead *aead,
  1891. const u8 *key,
  1892. unsigned int keylen)
  1893. {
  1894. struct chcr_context *ctx = crypto_aead_ctx(aead);
  1895. struct chcr_aead_ctx *aeadctx = AEAD_CTX(ctx);
  1896. unsigned char ck_size, mk_size;
  1897. int key_ctx_size = 0;
  1898. memcpy(aeadctx->key, key, keylen);
  1899. aeadctx->enckey_len = keylen;
  1900. key_ctx_size = sizeof(struct _key_ctx) +
  1901. ((DIV_ROUND_UP(keylen, 16)) << 4) * 2;
  1902. if (keylen == AES_KEYSIZE_128) {
  1903. mk_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
  1904. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
  1905. } else if (keylen == AES_KEYSIZE_192) {
  1906. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
  1907. mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_192;
  1908. } else if (keylen == AES_KEYSIZE_256) {
  1909. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
  1910. mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_256;
  1911. } else {
  1912. crypto_tfm_set_flags((struct crypto_tfm *)aead,
  1913. CRYPTO_TFM_RES_BAD_KEY_LEN);
  1914. aeadctx->enckey_len = 0;
  1915. return -EINVAL;
  1916. }
  1917. aeadctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, mk_size, 0, 0,
  1918. key_ctx_size >> 4);
  1919. return 0;
  1920. }
  1921. static int chcr_aead_rfc4309_setkey(struct crypto_aead *aead, const u8 *key,
  1922. unsigned int keylen)
  1923. {
  1924. struct chcr_context *ctx = crypto_aead_ctx(aead);
  1925. struct chcr_aead_ctx *aeadctx = AEAD_CTX(ctx);
  1926. if (keylen < 3) {
  1927. crypto_tfm_set_flags((struct crypto_tfm *)aead,
  1928. CRYPTO_TFM_RES_BAD_KEY_LEN);
  1929. aeadctx->enckey_len = 0;
  1930. return -EINVAL;
  1931. }
  1932. keylen -= 3;
  1933. memcpy(aeadctx->salt, key + keylen, 3);
  1934. return chcr_aead_ccm_setkey(aead, key, keylen);
  1935. }
  1936. static int chcr_gcm_setkey(struct crypto_aead *aead, const u8 *key,
  1937. unsigned int keylen)
  1938. {
  1939. struct chcr_context *ctx = crypto_aead_ctx(aead);
  1940. struct chcr_aead_ctx *aeadctx = AEAD_CTX(ctx);
  1941. struct chcr_gcm_ctx *gctx = GCM_CTX(aeadctx);
  1942. struct crypto_cipher *cipher;
  1943. unsigned int ck_size;
  1944. int ret = 0, key_ctx_size = 0;
  1945. if (get_aead_subtype(aead) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106 &&
  1946. keylen > 3) {
  1947. keylen -= 4; /* nonce/salt is present in the last 4 bytes */
  1948. memcpy(aeadctx->salt, key + keylen, 4);
  1949. }
  1950. if (keylen == AES_KEYSIZE_128) {
  1951. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
  1952. } else if (keylen == AES_KEYSIZE_192) {
  1953. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
  1954. } else if (keylen == AES_KEYSIZE_256) {
  1955. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
  1956. } else {
  1957. crypto_tfm_set_flags((struct crypto_tfm *)aead,
  1958. CRYPTO_TFM_RES_BAD_KEY_LEN);
  1959. aeadctx->enckey_len = 0;
  1960. pr_err("GCM: Invalid key length %d", keylen);
  1961. ret = -EINVAL;
  1962. goto out;
  1963. }
  1964. memcpy(aeadctx->key, key, keylen);
  1965. aeadctx->enckey_len = keylen;
  1966. key_ctx_size = sizeof(struct _key_ctx) +
  1967. ((DIV_ROUND_UP(keylen, 16)) << 4) +
  1968. AEAD_H_SIZE;
  1969. aeadctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size,
  1970. CHCR_KEYCTX_MAC_KEY_SIZE_128,
  1971. 0, 0,
  1972. key_ctx_size >> 4);
  1973. /* Calculate the H = CIPH(K, 0 repeated 16 times).
  1974. * It will go in key context
  1975. */
  1976. cipher = crypto_alloc_cipher("aes-generic", 0, 0);
  1977. if (IS_ERR(cipher)) {
  1978. aeadctx->enckey_len = 0;
  1979. ret = -ENOMEM;
  1980. goto out;
  1981. }
  1982. ret = crypto_cipher_setkey(cipher, key, keylen);
  1983. if (ret) {
  1984. aeadctx->enckey_len = 0;
  1985. goto out1;
  1986. }
  1987. memset(gctx->ghash_h, 0, AEAD_H_SIZE);
  1988. crypto_cipher_encrypt_one(cipher, gctx->ghash_h, gctx->ghash_h);
  1989. out1:
  1990. crypto_free_cipher(cipher);
  1991. out:
  1992. return ret;
  1993. }
  1994. static int chcr_authenc_setkey(struct crypto_aead *authenc, const u8 *key,
  1995. unsigned int keylen)
  1996. {
  1997. struct chcr_context *ctx = crypto_aead_ctx(authenc);
  1998. struct chcr_aead_ctx *aeadctx = AEAD_CTX(ctx);
  1999. struct chcr_authenc_ctx *actx = AUTHENC_CTX(aeadctx);
  2000. /* it contains auth and cipher key both*/
  2001. struct crypto_authenc_keys keys;
  2002. unsigned int bs;
  2003. unsigned int max_authsize = crypto_aead_alg(authenc)->maxauthsize;
  2004. int err = 0, i, key_ctx_len = 0;
  2005. unsigned char ck_size = 0;
  2006. unsigned char pad[CHCR_HASH_MAX_BLOCK_SIZE_128] = { 0 };
  2007. struct crypto_shash *base_hash = NULL;
  2008. struct algo_param param;
  2009. int align;
  2010. u8 *o_ptr = NULL;
  2011. if (crypto_authenc_extractkeys(&keys, key, keylen) != 0) {
  2012. crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
  2013. goto out;
  2014. }
  2015. if (get_alg_config(&param, max_authsize)) {
  2016. pr_err("chcr : Unsupported digest size\n");
  2017. goto out;
  2018. }
  2019. if (keys.enckeylen == AES_KEYSIZE_128) {
  2020. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
  2021. } else if (keys.enckeylen == AES_KEYSIZE_192) {
  2022. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
  2023. } else if (keys.enckeylen == AES_KEYSIZE_256) {
  2024. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
  2025. } else {
  2026. pr_err("chcr : Unsupported cipher key\n");
  2027. goto out;
  2028. }
  2029. /* Copy only encryption key. We use authkey to generate h(ipad) and
  2030. * h(opad) so authkey is not needed again. authkeylen size have the
  2031. * size of the hash digest size.
  2032. */
  2033. memcpy(aeadctx->key, keys.enckey, keys.enckeylen);
  2034. aeadctx->enckey_len = keys.enckeylen;
  2035. get_aes_decrypt_key(actx->dec_rrkey, aeadctx->key,
  2036. aeadctx->enckey_len << 3);
  2037. base_hash = chcr_alloc_shash(max_authsize);
  2038. if (IS_ERR(base_hash)) {
  2039. pr_err("chcr : Base driver cannot be loaded\n");
  2040. goto out;
  2041. }
  2042. {
  2043. SHASH_DESC_ON_STACK(shash, base_hash);
  2044. shash->tfm = base_hash;
  2045. shash->flags = crypto_shash_get_flags(base_hash);
  2046. bs = crypto_shash_blocksize(base_hash);
  2047. align = KEYCTX_ALIGN_PAD(max_authsize);
  2048. o_ptr = actx->h_iopad + param.result_size + align;
  2049. if (keys.authkeylen > bs) {
  2050. err = crypto_shash_digest(shash, keys.authkey,
  2051. keys.authkeylen,
  2052. o_ptr);
  2053. if (err) {
  2054. pr_err("chcr : Base driver cannot be loaded\n");
  2055. goto out;
  2056. }
  2057. keys.authkeylen = max_authsize;
  2058. } else
  2059. memcpy(o_ptr, keys.authkey, keys.authkeylen);
  2060. /* Compute the ipad-digest*/
  2061. memset(pad + keys.authkeylen, 0, bs - keys.authkeylen);
  2062. memcpy(pad, o_ptr, keys.authkeylen);
  2063. for (i = 0; i < bs >> 2; i++)
  2064. *((unsigned int *)pad + i) ^= IPAD_DATA;
  2065. if (chcr_compute_partial_hash(shash, pad, actx->h_iopad,
  2066. max_authsize))
  2067. goto out;
  2068. /* Compute the opad-digest */
  2069. memset(pad + keys.authkeylen, 0, bs - keys.authkeylen);
  2070. memcpy(pad, o_ptr, keys.authkeylen);
  2071. for (i = 0; i < bs >> 2; i++)
  2072. *((unsigned int *)pad + i) ^= OPAD_DATA;
  2073. if (chcr_compute_partial_hash(shash, pad, o_ptr, max_authsize))
  2074. goto out;
  2075. /* convert the ipad and opad digest to network order */
  2076. chcr_change_order(actx->h_iopad, param.result_size);
  2077. chcr_change_order(o_ptr, param.result_size);
  2078. key_ctx_len = sizeof(struct _key_ctx) +
  2079. ((DIV_ROUND_UP(keys.enckeylen, 16)) << 4) +
  2080. (param.result_size + align) * 2;
  2081. aeadctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, param.mk_size,
  2082. 0, 1, key_ctx_len >> 4);
  2083. actx->auth_mode = param.auth_mode;
  2084. chcr_free_shash(base_hash);
  2085. return 0;
  2086. }
  2087. out:
  2088. aeadctx->enckey_len = 0;
  2089. if (base_hash)
  2090. chcr_free_shash(base_hash);
  2091. return -EINVAL;
  2092. }
  2093. static int chcr_aead_digest_null_setkey(struct crypto_aead *authenc,
  2094. const u8 *key, unsigned int keylen)
  2095. {
  2096. struct chcr_context *ctx = crypto_aead_ctx(authenc);
  2097. struct chcr_aead_ctx *aeadctx = AEAD_CTX(ctx);
  2098. struct chcr_authenc_ctx *actx = AUTHENC_CTX(aeadctx);
  2099. struct crypto_authenc_keys keys;
  2100. /* it contains auth and cipher key both*/
  2101. int key_ctx_len = 0;
  2102. unsigned char ck_size = 0;
  2103. if (crypto_authenc_extractkeys(&keys, key, keylen) != 0) {
  2104. crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
  2105. goto out;
  2106. }
  2107. if (keys.enckeylen == AES_KEYSIZE_128) {
  2108. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
  2109. } else if (keys.enckeylen == AES_KEYSIZE_192) {
  2110. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
  2111. } else if (keys.enckeylen == AES_KEYSIZE_256) {
  2112. ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
  2113. } else {
  2114. pr_err("chcr : Unsupported cipher key\n");
  2115. goto out;
  2116. }
  2117. memcpy(aeadctx->key, keys.enckey, keys.enckeylen);
  2118. aeadctx->enckey_len = keys.enckeylen;
  2119. get_aes_decrypt_key(actx->dec_rrkey, aeadctx->key,
  2120. aeadctx->enckey_len << 3);
  2121. key_ctx_len = sizeof(struct _key_ctx)
  2122. + ((DIV_ROUND_UP(keys.enckeylen, 16)) << 4);
  2123. aeadctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, CHCR_KEYCTX_NO_KEY, 0,
  2124. 0, key_ctx_len >> 4);
  2125. actx->auth_mode = CHCR_SCMD_AUTH_MODE_NOP;
  2126. return 0;
  2127. out:
  2128. aeadctx->enckey_len = 0;
  2129. return -EINVAL;
  2130. }
  2131. static int chcr_aead_encrypt(struct aead_request *req)
  2132. {
  2133. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  2134. struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
  2135. reqctx->verify = VERIFY_HW;
  2136. switch (get_aead_subtype(tfm)) {
  2137. case CRYPTO_ALG_SUB_TYPE_AEAD_AUTHENC:
  2138. case CRYPTO_ALG_SUB_TYPE_AEAD_NULL:
  2139. return chcr_aead_op(req, CHCR_ENCRYPT_OP, 0,
  2140. create_authenc_wr);
  2141. case CRYPTO_ALG_SUB_TYPE_AEAD_CCM:
  2142. case CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309:
  2143. return chcr_aead_op(req, CHCR_ENCRYPT_OP, 0,
  2144. create_aead_ccm_wr);
  2145. default:
  2146. return chcr_aead_op(req, CHCR_ENCRYPT_OP, 0,
  2147. create_gcm_wr);
  2148. }
  2149. }
  2150. static int chcr_aead_decrypt(struct aead_request *req)
  2151. {
  2152. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  2153. struct chcr_aead_ctx *aeadctx = AEAD_CTX(crypto_aead_ctx(tfm));
  2154. struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
  2155. int size;
  2156. if (aeadctx->mayverify == VERIFY_SW) {
  2157. size = crypto_aead_maxauthsize(tfm);
  2158. reqctx->verify = VERIFY_SW;
  2159. } else {
  2160. size = 0;
  2161. reqctx->verify = VERIFY_HW;
  2162. }
  2163. switch (get_aead_subtype(tfm)) {
  2164. case CRYPTO_ALG_SUB_TYPE_AEAD_AUTHENC:
  2165. case CRYPTO_ALG_SUB_TYPE_AEAD_NULL:
  2166. return chcr_aead_op(req, CHCR_DECRYPT_OP, size,
  2167. create_authenc_wr);
  2168. case CRYPTO_ALG_SUB_TYPE_AEAD_CCM:
  2169. case CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309:
  2170. return chcr_aead_op(req, CHCR_DECRYPT_OP, size,
  2171. create_aead_ccm_wr);
  2172. default:
  2173. return chcr_aead_op(req, CHCR_DECRYPT_OP, size,
  2174. create_gcm_wr);
  2175. }
  2176. }
  2177. static int chcr_aead_op(struct aead_request *req,
  2178. unsigned short op_type,
  2179. int size,
  2180. create_wr_t create_wr_fn)
  2181. {
  2182. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  2183. struct chcr_context *ctx = crypto_aead_ctx(tfm);
  2184. struct uld_ctx *u_ctx;
  2185. struct sk_buff *skb;
  2186. if (!ctx->dev) {
  2187. pr_err("chcr : %s : No crypto device.\n", __func__);
  2188. return -ENXIO;
  2189. }
  2190. u_ctx = ULD_CTX(ctx);
  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_ABLKCIPHER |
  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_ABLKCIPHER |
  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. }