chcr_algo.c 107 KB

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