caamalg_qi.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Freescale FSL CAAM support for crypto API over QI backend.
  4. * Based on caamalg.c
  5. *
  6. * Copyright 2013-2016 Freescale Semiconductor, Inc.
  7. * Copyright 2016-2019 NXP
  8. */
  9. #include "compat.h"
  10. #include "ctrl.h"
  11. #include "regs.h"
  12. #include "intern.h"
  13. #include "desc_constr.h"
  14. #include "error.h"
  15. #include "sg_sw_qm.h"
  16. #include "key_gen.h"
  17. #include "qi.h"
  18. #include "jr.h"
  19. #include "caamalg_desc.h"
  20. #include <crypto/xts.h>
  21. #include <asm/unaligned.h>
  22. /*
  23. * crypto alg
  24. */
  25. #define CAAM_CRA_PRIORITY 2000
  26. /* max key is sum of AES_MAX_KEY_SIZE, max split key size */
  27. #define CAAM_MAX_KEY_SIZE (AES_MAX_KEY_SIZE + \
  28. SHA512_DIGEST_SIZE * 2)
  29. #define DESC_MAX_USED_BYTES (DESC_QI_AEAD_GIVENC_LEN + \
  30. CAAM_MAX_KEY_SIZE)
  31. #define DESC_MAX_USED_LEN (DESC_MAX_USED_BYTES / CAAM_CMD_SZ)
  32. struct caam_alg_entry {
  33. int class1_alg_type;
  34. int class2_alg_type;
  35. bool rfc3686;
  36. bool geniv;
  37. bool nodkp;
  38. };
  39. struct caam_aead_alg {
  40. struct aead_alg aead;
  41. struct caam_alg_entry caam;
  42. bool registered;
  43. };
  44. struct caam_skcipher_alg {
  45. struct skcipher_alg skcipher;
  46. struct caam_alg_entry caam;
  47. bool registered;
  48. };
  49. /*
  50. * per-session context
  51. */
  52. struct caam_ctx {
  53. struct device *jrdev;
  54. u32 sh_desc_enc[DESC_MAX_USED_LEN];
  55. u32 sh_desc_dec[DESC_MAX_USED_LEN];
  56. u8 key[CAAM_MAX_KEY_SIZE];
  57. dma_addr_t key_dma;
  58. enum dma_data_direction dir;
  59. struct alginfo adata;
  60. struct alginfo cdata;
  61. unsigned int authsize;
  62. struct device *qidev;
  63. spinlock_t lock; /* Protects multiple init of driver context */
  64. struct caam_drv_ctx *drv_ctx[NUM_OP];
  65. bool xts_key_fallback;
  66. struct crypto_skcipher *fallback;
  67. };
  68. struct caam_skcipher_req_ctx {
  69. struct skcipher_request fallback_req;
  70. };
  71. static int aead_set_sh_desc(struct crypto_aead *aead)
  72. {
  73. struct caam_aead_alg *alg = container_of(crypto_aead_alg(aead),
  74. typeof(*alg), aead);
  75. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  76. unsigned int ivsize = crypto_aead_ivsize(aead);
  77. u32 ctx1_iv_off = 0;
  78. u32 *nonce = NULL;
  79. unsigned int data_len[2];
  80. u32 inl_mask;
  81. const bool ctr_mode = ((ctx->cdata.algtype & OP_ALG_AAI_MASK) ==
  82. OP_ALG_AAI_CTR_MOD128);
  83. const bool is_rfc3686 = alg->caam.rfc3686;
  84. struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctx->jrdev->parent);
  85. if (!ctx->cdata.keylen || !ctx->authsize)
  86. return 0;
  87. /*
  88. * AES-CTR needs to load IV in CONTEXT1 reg
  89. * at an offset of 128bits (16bytes)
  90. * CONTEXT1[255:128] = IV
  91. */
  92. if (ctr_mode)
  93. ctx1_iv_off = 16;
  94. /*
  95. * RFC3686 specific:
  96. * CONTEXT1[255:128] = {NONCE, IV, COUNTER}
  97. */
  98. if (is_rfc3686) {
  99. ctx1_iv_off = 16 + CTR_RFC3686_NONCE_SIZE;
  100. nonce = (u32 *)((void *)ctx->key + ctx->adata.keylen_pad +
  101. ctx->cdata.keylen - CTR_RFC3686_NONCE_SIZE);
  102. }
  103. /*
  104. * In case |user key| > |derived key|, using DKP<imm,imm> would result
  105. * in invalid opcodes (last bytes of user key) in the resulting
  106. * descriptor. Use DKP<ptr,imm> instead => both virtual and dma key
  107. * addresses are needed.
  108. */
  109. ctx->adata.key_virt = ctx->key;
  110. ctx->adata.key_dma = ctx->key_dma;
  111. ctx->cdata.key_virt = ctx->key + ctx->adata.keylen_pad;
  112. ctx->cdata.key_dma = ctx->key_dma + ctx->adata.keylen_pad;
  113. data_len[0] = ctx->adata.keylen_pad;
  114. data_len[1] = ctx->cdata.keylen;
  115. if (alg->caam.geniv)
  116. goto skip_enc;
  117. /* aead_encrypt shared descriptor */
  118. if (desc_inline_query(DESC_QI_AEAD_ENC_LEN +
  119. (is_rfc3686 ? DESC_AEAD_CTR_RFC3686_LEN : 0),
  120. DESC_JOB_IO_LEN, data_len, &inl_mask,
  121. ARRAY_SIZE(data_len)) < 0)
  122. return -EINVAL;
  123. ctx->adata.key_inline = !!(inl_mask & 1);
  124. ctx->cdata.key_inline = !!(inl_mask & 2);
  125. cnstr_shdsc_aead_encap(ctx->sh_desc_enc, &ctx->cdata, &ctx->adata,
  126. ivsize, ctx->authsize, is_rfc3686, nonce,
  127. ctx1_iv_off, true, ctrlpriv->era);
  128. skip_enc:
  129. /* aead_decrypt shared descriptor */
  130. if (desc_inline_query(DESC_QI_AEAD_DEC_LEN +
  131. (is_rfc3686 ? DESC_AEAD_CTR_RFC3686_LEN : 0),
  132. DESC_JOB_IO_LEN, data_len, &inl_mask,
  133. ARRAY_SIZE(data_len)) < 0)
  134. return -EINVAL;
  135. ctx->adata.key_inline = !!(inl_mask & 1);
  136. ctx->cdata.key_inline = !!(inl_mask & 2);
  137. cnstr_shdsc_aead_decap(ctx->sh_desc_dec, &ctx->cdata, &ctx->adata,
  138. ivsize, ctx->authsize, alg->caam.geniv,
  139. is_rfc3686, nonce, ctx1_iv_off, true,
  140. ctrlpriv->era);
  141. if (!alg->caam.geniv)
  142. goto skip_givenc;
  143. /* aead_givencrypt shared descriptor */
  144. if (desc_inline_query(DESC_QI_AEAD_GIVENC_LEN +
  145. (is_rfc3686 ? DESC_AEAD_CTR_RFC3686_LEN : 0),
  146. DESC_JOB_IO_LEN, data_len, &inl_mask,
  147. ARRAY_SIZE(data_len)) < 0)
  148. return -EINVAL;
  149. ctx->adata.key_inline = !!(inl_mask & 1);
  150. ctx->cdata.key_inline = !!(inl_mask & 2);
  151. cnstr_shdsc_aead_givencap(ctx->sh_desc_enc, &ctx->cdata, &ctx->adata,
  152. ivsize, ctx->authsize, is_rfc3686, nonce,
  153. ctx1_iv_off, true, ctrlpriv->era);
  154. skip_givenc:
  155. return 0;
  156. }
  157. static int aead_setauthsize(struct crypto_aead *authenc, unsigned int authsize)
  158. {
  159. struct caam_ctx *ctx = crypto_aead_ctx(authenc);
  160. ctx->authsize = authsize;
  161. aead_set_sh_desc(authenc);
  162. return 0;
  163. }
  164. static int aead_setkey(struct crypto_aead *aead, const u8 *key,
  165. unsigned int keylen)
  166. {
  167. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  168. struct device *jrdev = ctx->jrdev;
  169. struct caam_drv_private *ctrlpriv = dev_get_drvdata(jrdev->parent);
  170. struct crypto_authenc_keys keys;
  171. int ret = 0;
  172. if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
  173. goto badkey;
  174. dev_dbg(jrdev, "keylen %d enckeylen %d authkeylen %d\n",
  175. keys.authkeylen + keys.enckeylen, keys.enckeylen,
  176. keys.authkeylen);
  177. print_hex_dump_debug("key in @" __stringify(__LINE__)": ",
  178. DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
  179. /*
  180. * If DKP is supported, use it in the shared descriptor to generate
  181. * the split key.
  182. */
  183. if (ctrlpriv->era >= 6) {
  184. ctx->adata.keylen = keys.authkeylen;
  185. ctx->adata.keylen_pad = split_key_len(ctx->adata.algtype &
  186. OP_ALG_ALGSEL_MASK);
  187. if (ctx->adata.keylen_pad + keys.enckeylen > CAAM_MAX_KEY_SIZE)
  188. goto badkey;
  189. memcpy(ctx->key, keys.authkey, keys.authkeylen);
  190. memcpy(ctx->key + ctx->adata.keylen_pad, keys.enckey,
  191. keys.enckeylen);
  192. dma_sync_single_for_device(jrdev->parent, ctx->key_dma,
  193. ctx->adata.keylen_pad +
  194. keys.enckeylen, ctx->dir);
  195. goto skip_split_key;
  196. }
  197. ret = gen_split_key(jrdev, ctx->key, &ctx->adata, keys.authkey,
  198. keys.authkeylen, CAAM_MAX_KEY_SIZE -
  199. keys.enckeylen);
  200. if (ret)
  201. goto badkey;
  202. /* postpend encryption key to auth split key */
  203. memcpy(ctx->key + ctx->adata.keylen_pad, keys.enckey, keys.enckeylen);
  204. dma_sync_single_for_device(jrdev->parent, ctx->key_dma,
  205. ctx->adata.keylen_pad + keys.enckeylen,
  206. ctx->dir);
  207. print_hex_dump_debug("ctx.key@" __stringify(__LINE__)": ",
  208. DUMP_PREFIX_ADDRESS, 16, 4, ctx->key,
  209. ctx->adata.keylen_pad + keys.enckeylen, 1);
  210. skip_split_key:
  211. ctx->cdata.keylen = keys.enckeylen;
  212. ret = aead_set_sh_desc(aead);
  213. if (ret)
  214. goto badkey;
  215. /* Now update the driver contexts with the new shared descriptor */
  216. if (ctx->drv_ctx[ENCRYPT]) {
  217. ret = caam_drv_ctx_update(ctx->drv_ctx[ENCRYPT],
  218. ctx->sh_desc_enc);
  219. if (ret) {
  220. dev_err(jrdev, "driver enc context update failed\n");
  221. goto badkey;
  222. }
  223. }
  224. if (ctx->drv_ctx[DECRYPT]) {
  225. ret = caam_drv_ctx_update(ctx->drv_ctx[DECRYPT],
  226. ctx->sh_desc_dec);
  227. if (ret) {
  228. dev_err(jrdev, "driver dec context update failed\n");
  229. goto badkey;
  230. }
  231. }
  232. memzero_explicit(&keys, sizeof(keys));
  233. return ret;
  234. badkey:
  235. memzero_explicit(&keys, sizeof(keys));
  236. return -EINVAL;
  237. }
  238. static int des3_aead_setkey(struct crypto_aead *aead, const u8 *key,
  239. unsigned int keylen)
  240. {
  241. struct crypto_authenc_keys keys;
  242. int err;
  243. err = crypto_authenc_extractkeys(&keys, key, keylen);
  244. if (unlikely(err))
  245. return err;
  246. err = verify_aead_des3_key(aead, keys.enckey, keys.enckeylen) ?:
  247. aead_setkey(aead, key, keylen);
  248. memzero_explicit(&keys, sizeof(keys));
  249. return err;
  250. }
  251. static int gcm_set_sh_desc(struct crypto_aead *aead)
  252. {
  253. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  254. unsigned int ivsize = crypto_aead_ivsize(aead);
  255. int rem_bytes = CAAM_DESC_BYTES_MAX - DESC_JOB_IO_LEN -
  256. ctx->cdata.keylen;
  257. if (!ctx->cdata.keylen || !ctx->authsize)
  258. return 0;
  259. /*
  260. * Job Descriptor and Shared Descriptor
  261. * must fit into the 64-word Descriptor h/w Buffer
  262. */
  263. if (rem_bytes >= DESC_QI_GCM_ENC_LEN) {
  264. ctx->cdata.key_inline = true;
  265. ctx->cdata.key_virt = ctx->key;
  266. } else {
  267. ctx->cdata.key_inline = false;
  268. ctx->cdata.key_dma = ctx->key_dma;
  269. }
  270. cnstr_shdsc_gcm_encap(ctx->sh_desc_enc, &ctx->cdata, ivsize,
  271. ctx->authsize, true);
  272. /*
  273. * Job Descriptor and Shared Descriptor
  274. * must fit into the 64-word Descriptor h/w Buffer
  275. */
  276. if (rem_bytes >= DESC_QI_GCM_DEC_LEN) {
  277. ctx->cdata.key_inline = true;
  278. ctx->cdata.key_virt = ctx->key;
  279. } else {
  280. ctx->cdata.key_inline = false;
  281. ctx->cdata.key_dma = ctx->key_dma;
  282. }
  283. cnstr_shdsc_gcm_decap(ctx->sh_desc_dec, &ctx->cdata, ivsize,
  284. ctx->authsize, true);
  285. return 0;
  286. }
  287. static int gcm_setauthsize(struct crypto_aead *authenc, unsigned int authsize)
  288. {
  289. struct caam_ctx *ctx = crypto_aead_ctx(authenc);
  290. int err;
  291. err = crypto_gcm_check_authsize(authsize);
  292. if (err)
  293. return err;
  294. ctx->authsize = authsize;
  295. gcm_set_sh_desc(authenc);
  296. return 0;
  297. }
  298. static int gcm_setkey(struct crypto_aead *aead,
  299. const u8 *key, unsigned int keylen)
  300. {
  301. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  302. struct device *jrdev = ctx->jrdev;
  303. int ret;
  304. ret = aes_check_keylen(keylen);
  305. if (ret)
  306. return ret;
  307. print_hex_dump_debug("key in @" __stringify(__LINE__)": ",
  308. DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
  309. memcpy(ctx->key, key, keylen);
  310. dma_sync_single_for_device(jrdev->parent, ctx->key_dma, keylen,
  311. ctx->dir);
  312. ctx->cdata.keylen = keylen;
  313. ret = gcm_set_sh_desc(aead);
  314. if (ret)
  315. return ret;
  316. /* Now update the driver contexts with the new shared descriptor */
  317. if (ctx->drv_ctx[ENCRYPT]) {
  318. ret = caam_drv_ctx_update(ctx->drv_ctx[ENCRYPT],
  319. ctx->sh_desc_enc);
  320. if (ret) {
  321. dev_err(jrdev, "driver enc context update failed\n");
  322. return ret;
  323. }
  324. }
  325. if (ctx->drv_ctx[DECRYPT]) {
  326. ret = caam_drv_ctx_update(ctx->drv_ctx[DECRYPT],
  327. ctx->sh_desc_dec);
  328. if (ret) {
  329. dev_err(jrdev, "driver dec context update failed\n");
  330. return ret;
  331. }
  332. }
  333. return 0;
  334. }
  335. static int rfc4106_set_sh_desc(struct crypto_aead *aead)
  336. {
  337. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  338. unsigned int ivsize = crypto_aead_ivsize(aead);
  339. int rem_bytes = CAAM_DESC_BYTES_MAX - DESC_JOB_IO_LEN -
  340. ctx->cdata.keylen;
  341. if (!ctx->cdata.keylen || !ctx->authsize)
  342. return 0;
  343. ctx->cdata.key_virt = ctx->key;
  344. /*
  345. * Job Descriptor and Shared Descriptor
  346. * must fit into the 64-word Descriptor h/w Buffer
  347. */
  348. if (rem_bytes >= DESC_QI_RFC4106_ENC_LEN) {
  349. ctx->cdata.key_inline = true;
  350. } else {
  351. ctx->cdata.key_inline = false;
  352. ctx->cdata.key_dma = ctx->key_dma;
  353. }
  354. cnstr_shdsc_rfc4106_encap(ctx->sh_desc_enc, &ctx->cdata, ivsize,
  355. ctx->authsize, true);
  356. /*
  357. * Job Descriptor and Shared Descriptor
  358. * must fit into the 64-word Descriptor h/w Buffer
  359. */
  360. if (rem_bytes >= DESC_QI_RFC4106_DEC_LEN) {
  361. ctx->cdata.key_inline = true;
  362. } else {
  363. ctx->cdata.key_inline = false;
  364. ctx->cdata.key_dma = ctx->key_dma;
  365. }
  366. cnstr_shdsc_rfc4106_decap(ctx->sh_desc_dec, &ctx->cdata, ivsize,
  367. ctx->authsize, true);
  368. return 0;
  369. }
  370. static int rfc4106_setauthsize(struct crypto_aead *authenc,
  371. unsigned int authsize)
  372. {
  373. struct caam_ctx *ctx = crypto_aead_ctx(authenc);
  374. int err;
  375. err = crypto_rfc4106_check_authsize(authsize);
  376. if (err)
  377. return err;
  378. ctx->authsize = authsize;
  379. rfc4106_set_sh_desc(authenc);
  380. return 0;
  381. }
  382. static int rfc4106_setkey(struct crypto_aead *aead,
  383. const u8 *key, unsigned int keylen)
  384. {
  385. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  386. struct device *jrdev = ctx->jrdev;
  387. int ret;
  388. ret = aes_check_keylen(keylen - 4);
  389. if (ret)
  390. return ret;
  391. print_hex_dump_debug("key in @" __stringify(__LINE__)": ",
  392. DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
  393. memcpy(ctx->key, key, keylen);
  394. /*
  395. * The last four bytes of the key material are used as the salt value
  396. * in the nonce. Update the AES key length.
  397. */
  398. ctx->cdata.keylen = keylen - 4;
  399. dma_sync_single_for_device(jrdev->parent, ctx->key_dma,
  400. ctx->cdata.keylen, ctx->dir);
  401. ret = rfc4106_set_sh_desc(aead);
  402. if (ret)
  403. return ret;
  404. /* Now update the driver contexts with the new shared descriptor */
  405. if (ctx->drv_ctx[ENCRYPT]) {
  406. ret = caam_drv_ctx_update(ctx->drv_ctx[ENCRYPT],
  407. ctx->sh_desc_enc);
  408. if (ret) {
  409. dev_err(jrdev, "driver enc context update failed\n");
  410. return ret;
  411. }
  412. }
  413. if (ctx->drv_ctx[DECRYPT]) {
  414. ret = caam_drv_ctx_update(ctx->drv_ctx[DECRYPT],
  415. ctx->sh_desc_dec);
  416. if (ret) {
  417. dev_err(jrdev, "driver dec context update failed\n");
  418. return ret;
  419. }
  420. }
  421. return 0;
  422. }
  423. static int rfc4543_set_sh_desc(struct crypto_aead *aead)
  424. {
  425. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  426. unsigned int ivsize = crypto_aead_ivsize(aead);
  427. int rem_bytes = CAAM_DESC_BYTES_MAX - DESC_JOB_IO_LEN -
  428. ctx->cdata.keylen;
  429. if (!ctx->cdata.keylen || !ctx->authsize)
  430. return 0;
  431. ctx->cdata.key_virt = ctx->key;
  432. /*
  433. * Job Descriptor and Shared Descriptor
  434. * must fit into the 64-word Descriptor h/w Buffer
  435. */
  436. if (rem_bytes >= DESC_QI_RFC4543_ENC_LEN) {
  437. ctx->cdata.key_inline = true;
  438. } else {
  439. ctx->cdata.key_inline = false;
  440. ctx->cdata.key_dma = ctx->key_dma;
  441. }
  442. cnstr_shdsc_rfc4543_encap(ctx->sh_desc_enc, &ctx->cdata, ivsize,
  443. ctx->authsize, true);
  444. /*
  445. * Job Descriptor and Shared Descriptor
  446. * must fit into the 64-word Descriptor h/w Buffer
  447. */
  448. if (rem_bytes >= DESC_QI_RFC4543_DEC_LEN) {
  449. ctx->cdata.key_inline = true;
  450. } else {
  451. ctx->cdata.key_inline = false;
  452. ctx->cdata.key_dma = ctx->key_dma;
  453. }
  454. cnstr_shdsc_rfc4543_decap(ctx->sh_desc_dec, &ctx->cdata, ivsize,
  455. ctx->authsize, true);
  456. return 0;
  457. }
  458. static int rfc4543_setauthsize(struct crypto_aead *authenc,
  459. unsigned int authsize)
  460. {
  461. struct caam_ctx *ctx = crypto_aead_ctx(authenc);
  462. if (authsize != 16)
  463. return -EINVAL;
  464. ctx->authsize = authsize;
  465. rfc4543_set_sh_desc(authenc);
  466. return 0;
  467. }
  468. static int rfc4543_setkey(struct crypto_aead *aead,
  469. const u8 *key, unsigned int keylen)
  470. {
  471. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  472. struct device *jrdev = ctx->jrdev;
  473. int ret;
  474. ret = aes_check_keylen(keylen - 4);
  475. if (ret)
  476. return ret;
  477. print_hex_dump_debug("key in @" __stringify(__LINE__)": ",
  478. DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
  479. memcpy(ctx->key, key, keylen);
  480. /*
  481. * The last four bytes of the key material are used as the salt value
  482. * in the nonce. Update the AES key length.
  483. */
  484. ctx->cdata.keylen = keylen - 4;
  485. dma_sync_single_for_device(jrdev->parent, ctx->key_dma,
  486. ctx->cdata.keylen, ctx->dir);
  487. ret = rfc4543_set_sh_desc(aead);
  488. if (ret)
  489. return ret;
  490. /* Now update the driver contexts with the new shared descriptor */
  491. if (ctx->drv_ctx[ENCRYPT]) {
  492. ret = caam_drv_ctx_update(ctx->drv_ctx[ENCRYPT],
  493. ctx->sh_desc_enc);
  494. if (ret) {
  495. dev_err(jrdev, "driver enc context update failed\n");
  496. return ret;
  497. }
  498. }
  499. if (ctx->drv_ctx[DECRYPT]) {
  500. ret = caam_drv_ctx_update(ctx->drv_ctx[DECRYPT],
  501. ctx->sh_desc_dec);
  502. if (ret) {
  503. dev_err(jrdev, "driver dec context update failed\n");
  504. return ret;
  505. }
  506. }
  507. return 0;
  508. }
  509. static int skcipher_setkey(struct crypto_skcipher *skcipher, const u8 *key,
  510. unsigned int keylen, const u32 ctx1_iv_off)
  511. {
  512. struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
  513. struct caam_skcipher_alg *alg =
  514. container_of(crypto_skcipher_alg(skcipher), typeof(*alg),
  515. skcipher);
  516. struct device *jrdev = ctx->jrdev;
  517. unsigned int ivsize = crypto_skcipher_ivsize(skcipher);
  518. const bool is_rfc3686 = alg->caam.rfc3686;
  519. int ret = 0;
  520. print_hex_dump_debug("key in @" __stringify(__LINE__)": ",
  521. DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
  522. ctx->cdata.keylen = keylen;
  523. ctx->cdata.key_virt = key;
  524. ctx->cdata.key_inline = true;
  525. /* skcipher encrypt, decrypt shared descriptors */
  526. cnstr_shdsc_skcipher_encap(ctx->sh_desc_enc, &ctx->cdata, ivsize,
  527. is_rfc3686, ctx1_iv_off);
  528. cnstr_shdsc_skcipher_decap(ctx->sh_desc_dec, &ctx->cdata, ivsize,
  529. is_rfc3686, ctx1_iv_off);
  530. /* Now update the driver contexts with the new shared descriptor */
  531. if (ctx->drv_ctx[ENCRYPT]) {
  532. ret = caam_drv_ctx_update(ctx->drv_ctx[ENCRYPT],
  533. ctx->sh_desc_enc);
  534. if (ret) {
  535. dev_err(jrdev, "driver enc context update failed\n");
  536. return -EINVAL;
  537. }
  538. }
  539. if (ctx->drv_ctx[DECRYPT]) {
  540. ret = caam_drv_ctx_update(ctx->drv_ctx[DECRYPT],
  541. ctx->sh_desc_dec);
  542. if (ret) {
  543. dev_err(jrdev, "driver dec context update failed\n");
  544. return -EINVAL;
  545. }
  546. }
  547. return ret;
  548. }
  549. static int aes_skcipher_setkey(struct crypto_skcipher *skcipher,
  550. const u8 *key, unsigned int keylen)
  551. {
  552. int err;
  553. err = aes_check_keylen(keylen);
  554. if (err)
  555. return err;
  556. return skcipher_setkey(skcipher, key, keylen, 0);
  557. }
  558. static int rfc3686_skcipher_setkey(struct crypto_skcipher *skcipher,
  559. const u8 *key, unsigned int keylen)
  560. {
  561. u32 ctx1_iv_off;
  562. int err;
  563. /*
  564. * RFC3686 specific:
  565. * | CONTEXT1[255:128] = {NONCE, IV, COUNTER}
  566. * | *key = {KEY, NONCE}
  567. */
  568. ctx1_iv_off = 16 + CTR_RFC3686_NONCE_SIZE;
  569. keylen -= CTR_RFC3686_NONCE_SIZE;
  570. err = aes_check_keylen(keylen);
  571. if (err)
  572. return err;
  573. return skcipher_setkey(skcipher, key, keylen, ctx1_iv_off);
  574. }
  575. static int ctr_skcipher_setkey(struct crypto_skcipher *skcipher,
  576. const u8 *key, unsigned int keylen)
  577. {
  578. u32 ctx1_iv_off;
  579. int err;
  580. /*
  581. * AES-CTR needs to load IV in CONTEXT1 reg
  582. * at an offset of 128bits (16bytes)
  583. * CONTEXT1[255:128] = IV
  584. */
  585. ctx1_iv_off = 16;
  586. err = aes_check_keylen(keylen);
  587. if (err)
  588. return err;
  589. return skcipher_setkey(skcipher, key, keylen, ctx1_iv_off);
  590. }
  591. static int des3_skcipher_setkey(struct crypto_skcipher *skcipher,
  592. const u8 *key, unsigned int keylen)
  593. {
  594. return verify_skcipher_des3_key(skcipher, key) ?:
  595. skcipher_setkey(skcipher, key, keylen, 0);
  596. }
  597. static int des_skcipher_setkey(struct crypto_skcipher *skcipher,
  598. const u8 *key, unsigned int keylen)
  599. {
  600. return verify_skcipher_des_key(skcipher, key) ?:
  601. skcipher_setkey(skcipher, key, keylen, 0);
  602. }
  603. static int xts_skcipher_setkey(struct crypto_skcipher *skcipher, const u8 *key,
  604. unsigned int keylen)
  605. {
  606. struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
  607. struct device *jrdev = ctx->jrdev;
  608. struct caam_drv_private *ctrlpriv = dev_get_drvdata(jrdev->parent);
  609. int ret = 0;
  610. int err;
  611. err = xts_verify_key(skcipher, key, keylen);
  612. if (err) {
  613. dev_dbg(jrdev, "key size mismatch\n");
  614. return err;
  615. }
  616. if (keylen != 2 * AES_KEYSIZE_128 && keylen != 2 * AES_KEYSIZE_256)
  617. ctx->xts_key_fallback = true;
  618. if (ctrlpriv->era <= 8 || ctx->xts_key_fallback) {
  619. err = crypto_skcipher_setkey(ctx->fallback, key, keylen);
  620. if (err)
  621. return err;
  622. }
  623. ctx->cdata.keylen = keylen;
  624. ctx->cdata.key_virt = key;
  625. ctx->cdata.key_inline = true;
  626. /* xts skcipher encrypt, decrypt shared descriptors */
  627. cnstr_shdsc_xts_skcipher_encap(ctx->sh_desc_enc, &ctx->cdata);
  628. cnstr_shdsc_xts_skcipher_decap(ctx->sh_desc_dec, &ctx->cdata);
  629. /* Now update the driver contexts with the new shared descriptor */
  630. if (ctx->drv_ctx[ENCRYPT]) {
  631. ret = caam_drv_ctx_update(ctx->drv_ctx[ENCRYPT],
  632. ctx->sh_desc_enc);
  633. if (ret) {
  634. dev_err(jrdev, "driver enc context update failed\n");
  635. return -EINVAL;
  636. }
  637. }
  638. if (ctx->drv_ctx[DECRYPT]) {
  639. ret = caam_drv_ctx_update(ctx->drv_ctx[DECRYPT],
  640. ctx->sh_desc_dec);
  641. if (ret) {
  642. dev_err(jrdev, "driver dec context update failed\n");
  643. return -EINVAL;
  644. }
  645. }
  646. return ret;
  647. }
  648. /*
  649. * aead_edesc - s/w-extended aead descriptor
  650. * @src_nents: number of segments in input scatterlist
  651. * @dst_nents: number of segments in output scatterlist
  652. * @iv_dma: dma address of iv for checking continuity and link table
  653. * @qm_sg_bytes: length of dma mapped h/w link table
  654. * @qm_sg_dma: bus physical mapped address of h/w link table
  655. * @assoclen: associated data length, in CAAM endianness
  656. * @assoclen_dma: bus physical mapped address of req->assoclen
  657. * @drv_req: driver-specific request structure
  658. * @sgt: the h/w link table, followed by IV
  659. */
  660. struct aead_edesc {
  661. int src_nents;
  662. int dst_nents;
  663. dma_addr_t iv_dma;
  664. int qm_sg_bytes;
  665. dma_addr_t qm_sg_dma;
  666. unsigned int assoclen;
  667. dma_addr_t assoclen_dma;
  668. struct caam_drv_req drv_req;
  669. struct qm_sg_entry sgt[];
  670. };
  671. /*
  672. * skcipher_edesc - s/w-extended skcipher descriptor
  673. * @src_nents: number of segments in input scatterlist
  674. * @dst_nents: number of segments in output scatterlist
  675. * @iv_dma: dma address of iv for checking continuity and link table
  676. * @qm_sg_bytes: length of dma mapped h/w link table
  677. * @qm_sg_dma: bus physical mapped address of h/w link table
  678. * @drv_req: driver-specific request structure
  679. * @sgt: the h/w link table, followed by IV
  680. */
  681. struct skcipher_edesc {
  682. int src_nents;
  683. int dst_nents;
  684. dma_addr_t iv_dma;
  685. int qm_sg_bytes;
  686. dma_addr_t qm_sg_dma;
  687. struct caam_drv_req drv_req;
  688. struct qm_sg_entry sgt[];
  689. };
  690. static struct caam_drv_ctx *get_drv_ctx(struct caam_ctx *ctx,
  691. enum optype type)
  692. {
  693. /*
  694. * This function is called on the fast path with values of 'type'
  695. * known at compile time. Invalid arguments are not expected and
  696. * thus no checks are made.
  697. */
  698. struct caam_drv_ctx *drv_ctx = ctx->drv_ctx[type];
  699. u32 *desc;
  700. if (unlikely(!drv_ctx)) {
  701. spin_lock(&ctx->lock);
  702. /* Read again to check if some other core init drv_ctx */
  703. drv_ctx = ctx->drv_ctx[type];
  704. if (!drv_ctx) {
  705. int cpu;
  706. if (type == ENCRYPT)
  707. desc = ctx->sh_desc_enc;
  708. else /* (type == DECRYPT) */
  709. desc = ctx->sh_desc_dec;
  710. cpu = smp_processor_id();
  711. drv_ctx = caam_drv_ctx_init(ctx->qidev, &cpu, desc);
  712. if (!IS_ERR(drv_ctx))
  713. drv_ctx->op_type = type;
  714. ctx->drv_ctx[type] = drv_ctx;
  715. }
  716. spin_unlock(&ctx->lock);
  717. }
  718. return drv_ctx;
  719. }
  720. static void caam_unmap(struct device *dev, struct scatterlist *src,
  721. struct scatterlist *dst, int src_nents,
  722. int dst_nents, dma_addr_t iv_dma, int ivsize,
  723. enum dma_data_direction iv_dir, dma_addr_t qm_sg_dma,
  724. int qm_sg_bytes)
  725. {
  726. if (dst != src) {
  727. if (src_nents)
  728. dma_unmap_sg(dev, src, src_nents, DMA_TO_DEVICE);
  729. if (dst_nents)
  730. dma_unmap_sg(dev, dst, dst_nents, DMA_FROM_DEVICE);
  731. } else {
  732. dma_unmap_sg(dev, src, src_nents, DMA_BIDIRECTIONAL);
  733. }
  734. if (iv_dma)
  735. dma_unmap_single(dev, iv_dma, ivsize, iv_dir);
  736. if (qm_sg_bytes)
  737. dma_unmap_single(dev, qm_sg_dma, qm_sg_bytes, DMA_TO_DEVICE);
  738. }
  739. static void aead_unmap(struct device *dev,
  740. struct aead_edesc *edesc,
  741. struct aead_request *req)
  742. {
  743. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  744. int ivsize = crypto_aead_ivsize(aead);
  745. caam_unmap(dev, req->src, req->dst, edesc->src_nents, edesc->dst_nents,
  746. edesc->iv_dma, ivsize, DMA_TO_DEVICE, edesc->qm_sg_dma,
  747. edesc->qm_sg_bytes);
  748. dma_unmap_single(dev, edesc->assoclen_dma, 4, DMA_TO_DEVICE);
  749. }
  750. static void skcipher_unmap(struct device *dev, struct skcipher_edesc *edesc,
  751. struct skcipher_request *req)
  752. {
  753. struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
  754. int ivsize = crypto_skcipher_ivsize(skcipher);
  755. caam_unmap(dev, req->src, req->dst, edesc->src_nents, edesc->dst_nents,
  756. edesc->iv_dma, ivsize, DMA_BIDIRECTIONAL, edesc->qm_sg_dma,
  757. edesc->qm_sg_bytes);
  758. }
  759. static void aead_done(struct caam_drv_req *drv_req, u32 status)
  760. {
  761. struct device *qidev;
  762. struct aead_edesc *edesc;
  763. struct aead_request *aead_req = drv_req->app_ctx;
  764. struct crypto_aead *aead = crypto_aead_reqtfm(aead_req);
  765. struct caam_ctx *caam_ctx = crypto_aead_ctx(aead);
  766. int ecode = 0;
  767. qidev = caam_ctx->qidev;
  768. if (unlikely(status))
  769. ecode = caam_jr_strstatus(qidev, status);
  770. edesc = container_of(drv_req, typeof(*edesc), drv_req);
  771. aead_unmap(qidev, edesc, aead_req);
  772. aead_request_complete(aead_req, ecode);
  773. qi_cache_free(edesc);
  774. }
  775. /*
  776. * allocate and map the aead extended descriptor
  777. */
  778. static struct aead_edesc *aead_edesc_alloc(struct aead_request *req,
  779. bool encrypt)
  780. {
  781. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  782. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  783. struct caam_aead_alg *alg = container_of(crypto_aead_alg(aead),
  784. typeof(*alg), aead);
  785. struct device *qidev = ctx->qidev;
  786. gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
  787. GFP_KERNEL : GFP_ATOMIC;
  788. int src_nents, mapped_src_nents, dst_nents = 0, mapped_dst_nents = 0;
  789. int src_len, dst_len = 0;
  790. struct aead_edesc *edesc;
  791. dma_addr_t qm_sg_dma, iv_dma = 0;
  792. int ivsize = 0;
  793. unsigned int authsize = ctx->authsize;
  794. int qm_sg_index = 0, qm_sg_ents = 0, qm_sg_bytes;
  795. int in_len, out_len;
  796. struct qm_sg_entry *sg_table, *fd_sgt;
  797. struct caam_drv_ctx *drv_ctx;
  798. drv_ctx = get_drv_ctx(ctx, encrypt ? ENCRYPT : DECRYPT);
  799. if (IS_ERR(drv_ctx))
  800. return (struct aead_edesc *)drv_ctx;
  801. /* allocate space for base edesc and hw desc commands, link tables */
  802. edesc = qi_cache_alloc(GFP_DMA | flags);
  803. if (unlikely(!edesc)) {
  804. dev_err(qidev, "could not allocate extended descriptor\n");
  805. return ERR_PTR(-ENOMEM);
  806. }
  807. if (likely(req->src == req->dst)) {
  808. src_len = req->assoclen + req->cryptlen +
  809. (encrypt ? authsize : 0);
  810. src_nents = sg_nents_for_len(req->src, src_len);
  811. if (unlikely(src_nents < 0)) {
  812. dev_err(qidev, "Insufficient bytes (%d) in src S/G\n",
  813. src_len);
  814. qi_cache_free(edesc);
  815. return ERR_PTR(src_nents);
  816. }
  817. mapped_src_nents = dma_map_sg(qidev, req->src, src_nents,
  818. DMA_BIDIRECTIONAL);
  819. if (unlikely(!mapped_src_nents)) {
  820. dev_err(qidev, "unable to map source\n");
  821. qi_cache_free(edesc);
  822. return ERR_PTR(-ENOMEM);
  823. }
  824. } else {
  825. src_len = req->assoclen + req->cryptlen;
  826. dst_len = src_len + (encrypt ? authsize : (-authsize));
  827. src_nents = sg_nents_for_len(req->src, src_len);
  828. if (unlikely(src_nents < 0)) {
  829. dev_err(qidev, "Insufficient bytes (%d) in src S/G\n",
  830. src_len);
  831. qi_cache_free(edesc);
  832. return ERR_PTR(src_nents);
  833. }
  834. dst_nents = sg_nents_for_len(req->dst, dst_len);
  835. if (unlikely(dst_nents < 0)) {
  836. dev_err(qidev, "Insufficient bytes (%d) in dst S/G\n",
  837. dst_len);
  838. qi_cache_free(edesc);
  839. return ERR_PTR(dst_nents);
  840. }
  841. if (src_nents) {
  842. mapped_src_nents = dma_map_sg(qidev, req->src,
  843. src_nents, DMA_TO_DEVICE);
  844. if (unlikely(!mapped_src_nents)) {
  845. dev_err(qidev, "unable to map source\n");
  846. qi_cache_free(edesc);
  847. return ERR_PTR(-ENOMEM);
  848. }
  849. } else {
  850. mapped_src_nents = 0;
  851. }
  852. if (dst_nents) {
  853. mapped_dst_nents = dma_map_sg(qidev, req->dst,
  854. dst_nents,
  855. DMA_FROM_DEVICE);
  856. if (unlikely(!mapped_dst_nents)) {
  857. dev_err(qidev, "unable to map destination\n");
  858. dma_unmap_sg(qidev, req->src, src_nents,
  859. DMA_TO_DEVICE);
  860. qi_cache_free(edesc);
  861. return ERR_PTR(-ENOMEM);
  862. }
  863. } else {
  864. mapped_dst_nents = 0;
  865. }
  866. }
  867. if ((alg->caam.rfc3686 && encrypt) || !alg->caam.geniv)
  868. ivsize = crypto_aead_ivsize(aead);
  869. /*
  870. * Create S/G table: req->assoclen, [IV,] req->src [, req->dst].
  871. * Input is not contiguous.
  872. * HW reads 4 S/G entries at a time; make sure the reads don't go beyond
  873. * the end of the table by allocating more S/G entries. Logic:
  874. * if (src != dst && output S/G)
  875. * pad output S/G, if needed
  876. * else if (src == dst && S/G)
  877. * overlapping S/Gs; pad one of them
  878. * else if (input S/G) ...
  879. * pad input S/G, if needed
  880. */
  881. qm_sg_ents = 1 + !!ivsize + mapped_src_nents;
  882. if (mapped_dst_nents > 1)
  883. qm_sg_ents += pad_sg_nents(mapped_dst_nents);
  884. else if ((req->src == req->dst) && (mapped_src_nents > 1))
  885. qm_sg_ents = max(pad_sg_nents(qm_sg_ents),
  886. 1 + !!ivsize + pad_sg_nents(mapped_src_nents));
  887. else
  888. qm_sg_ents = pad_sg_nents(qm_sg_ents);
  889. sg_table = &edesc->sgt[0];
  890. qm_sg_bytes = qm_sg_ents * sizeof(*sg_table);
  891. if (unlikely(offsetof(struct aead_edesc, sgt) + qm_sg_bytes + ivsize >
  892. CAAM_QI_MEMCACHE_SIZE)) {
  893. dev_err(qidev, "No space for %d S/G entries and/or %dB IV\n",
  894. qm_sg_ents, ivsize);
  895. caam_unmap(qidev, req->src, req->dst, src_nents, dst_nents, 0,
  896. 0, DMA_NONE, 0, 0);
  897. qi_cache_free(edesc);
  898. return ERR_PTR(-ENOMEM);
  899. }
  900. if (ivsize) {
  901. u8 *iv = (u8 *)(sg_table + qm_sg_ents);
  902. /* Make sure IV is located in a DMAable area */
  903. memcpy(iv, req->iv, ivsize);
  904. iv_dma = dma_map_single(qidev, iv, ivsize, DMA_TO_DEVICE);
  905. if (dma_mapping_error(qidev, iv_dma)) {
  906. dev_err(qidev, "unable to map IV\n");
  907. caam_unmap(qidev, req->src, req->dst, src_nents,
  908. dst_nents, 0, 0, DMA_NONE, 0, 0);
  909. qi_cache_free(edesc);
  910. return ERR_PTR(-ENOMEM);
  911. }
  912. }
  913. edesc->src_nents = src_nents;
  914. edesc->dst_nents = dst_nents;
  915. edesc->iv_dma = iv_dma;
  916. edesc->drv_req.app_ctx = req;
  917. edesc->drv_req.cbk = aead_done;
  918. edesc->drv_req.drv_ctx = drv_ctx;
  919. edesc->assoclen = cpu_to_caam32(req->assoclen);
  920. edesc->assoclen_dma = dma_map_single(qidev, &edesc->assoclen, 4,
  921. DMA_TO_DEVICE);
  922. if (dma_mapping_error(qidev, edesc->assoclen_dma)) {
  923. dev_err(qidev, "unable to map assoclen\n");
  924. caam_unmap(qidev, req->src, req->dst, src_nents, dst_nents,
  925. iv_dma, ivsize, DMA_TO_DEVICE, 0, 0);
  926. qi_cache_free(edesc);
  927. return ERR_PTR(-ENOMEM);
  928. }
  929. dma_to_qm_sg_one(sg_table, edesc->assoclen_dma, 4, 0);
  930. qm_sg_index++;
  931. if (ivsize) {
  932. dma_to_qm_sg_one(sg_table + qm_sg_index, iv_dma, ivsize, 0);
  933. qm_sg_index++;
  934. }
  935. sg_to_qm_sg_last(req->src, src_len, sg_table + qm_sg_index, 0);
  936. qm_sg_index += mapped_src_nents;
  937. if (mapped_dst_nents > 1)
  938. sg_to_qm_sg_last(req->dst, dst_len, sg_table + qm_sg_index, 0);
  939. qm_sg_dma = dma_map_single(qidev, sg_table, qm_sg_bytes, DMA_TO_DEVICE);
  940. if (dma_mapping_error(qidev, qm_sg_dma)) {
  941. dev_err(qidev, "unable to map S/G table\n");
  942. dma_unmap_single(qidev, edesc->assoclen_dma, 4, DMA_TO_DEVICE);
  943. caam_unmap(qidev, req->src, req->dst, src_nents, dst_nents,
  944. iv_dma, ivsize, DMA_TO_DEVICE, 0, 0);
  945. qi_cache_free(edesc);
  946. return ERR_PTR(-ENOMEM);
  947. }
  948. edesc->qm_sg_dma = qm_sg_dma;
  949. edesc->qm_sg_bytes = qm_sg_bytes;
  950. out_len = req->assoclen + req->cryptlen +
  951. (encrypt ? ctx->authsize : (-ctx->authsize));
  952. in_len = 4 + ivsize + req->assoclen + req->cryptlen;
  953. fd_sgt = &edesc->drv_req.fd_sgt[0];
  954. dma_to_qm_sg_one_last_ext(&fd_sgt[1], qm_sg_dma, in_len, 0);
  955. if (req->dst == req->src) {
  956. if (mapped_src_nents == 1)
  957. dma_to_qm_sg_one(&fd_sgt[0], sg_dma_address(req->src),
  958. out_len, 0);
  959. else
  960. dma_to_qm_sg_one_ext(&fd_sgt[0], qm_sg_dma +
  961. (1 + !!ivsize) * sizeof(*sg_table),
  962. out_len, 0);
  963. } else if (mapped_dst_nents <= 1) {
  964. dma_to_qm_sg_one(&fd_sgt[0], sg_dma_address(req->dst), out_len,
  965. 0);
  966. } else {
  967. dma_to_qm_sg_one_ext(&fd_sgt[0], qm_sg_dma + sizeof(*sg_table) *
  968. qm_sg_index, out_len, 0);
  969. }
  970. return edesc;
  971. }
  972. static inline int aead_crypt(struct aead_request *req, bool encrypt)
  973. {
  974. struct aead_edesc *edesc;
  975. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  976. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  977. int ret;
  978. if (unlikely(caam_congested))
  979. return -EAGAIN;
  980. /* allocate extended descriptor */
  981. edesc = aead_edesc_alloc(req, encrypt);
  982. if (IS_ERR(edesc))
  983. return PTR_ERR(edesc);
  984. /* Create and submit job descriptor */
  985. ret = caam_qi_enqueue(ctx->qidev, &edesc->drv_req);
  986. if (!ret) {
  987. ret = -EINPROGRESS;
  988. } else {
  989. aead_unmap(ctx->qidev, edesc, req);
  990. qi_cache_free(edesc);
  991. }
  992. return ret;
  993. }
  994. static int aead_encrypt(struct aead_request *req)
  995. {
  996. return aead_crypt(req, true);
  997. }
  998. static int aead_decrypt(struct aead_request *req)
  999. {
  1000. return aead_crypt(req, false);
  1001. }
  1002. static int ipsec_gcm_encrypt(struct aead_request *req)
  1003. {
  1004. return crypto_ipsec_check_assoclen(req->assoclen) ? : aead_crypt(req,
  1005. true);
  1006. }
  1007. static int ipsec_gcm_decrypt(struct aead_request *req)
  1008. {
  1009. return crypto_ipsec_check_assoclen(req->assoclen) ? : aead_crypt(req,
  1010. false);
  1011. }
  1012. static void skcipher_done(struct caam_drv_req *drv_req, u32 status)
  1013. {
  1014. struct skcipher_edesc *edesc;
  1015. struct skcipher_request *req = drv_req->app_ctx;
  1016. struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
  1017. struct caam_ctx *caam_ctx = crypto_skcipher_ctx(skcipher);
  1018. struct device *qidev = caam_ctx->qidev;
  1019. int ivsize = crypto_skcipher_ivsize(skcipher);
  1020. int ecode = 0;
  1021. dev_dbg(qidev, "%s %d: status 0x%x\n", __func__, __LINE__, status);
  1022. edesc = container_of(drv_req, typeof(*edesc), drv_req);
  1023. if (status)
  1024. ecode = caam_jr_strstatus(qidev, status);
  1025. print_hex_dump_debug("dstiv @" __stringify(__LINE__)": ",
  1026. DUMP_PREFIX_ADDRESS, 16, 4, req->iv,
  1027. edesc->src_nents > 1 ? 100 : ivsize, 1);
  1028. caam_dump_sg("dst @" __stringify(__LINE__)": ",
  1029. DUMP_PREFIX_ADDRESS, 16, 4, req->dst,
  1030. edesc->dst_nents > 1 ? 100 : req->cryptlen, 1);
  1031. skcipher_unmap(qidev, edesc, req);
  1032. /*
  1033. * The crypto API expects us to set the IV (req->iv) to the last
  1034. * ciphertext block (CBC mode) or last counter (CTR mode).
  1035. * This is used e.g. by the CTS mode.
  1036. */
  1037. if (!ecode)
  1038. memcpy(req->iv, (u8 *)&edesc->sgt[0] + edesc->qm_sg_bytes,
  1039. ivsize);
  1040. qi_cache_free(edesc);
  1041. skcipher_request_complete(req, ecode);
  1042. }
  1043. static struct skcipher_edesc *skcipher_edesc_alloc(struct skcipher_request *req,
  1044. bool encrypt)
  1045. {
  1046. struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
  1047. struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
  1048. struct device *qidev = ctx->qidev;
  1049. gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
  1050. GFP_KERNEL : GFP_ATOMIC;
  1051. int src_nents, mapped_src_nents, dst_nents = 0, mapped_dst_nents = 0;
  1052. struct skcipher_edesc *edesc;
  1053. dma_addr_t iv_dma;
  1054. u8 *iv;
  1055. int ivsize = crypto_skcipher_ivsize(skcipher);
  1056. int dst_sg_idx, qm_sg_ents, qm_sg_bytes;
  1057. struct qm_sg_entry *sg_table, *fd_sgt;
  1058. struct caam_drv_ctx *drv_ctx;
  1059. drv_ctx = get_drv_ctx(ctx, encrypt ? ENCRYPT : DECRYPT);
  1060. if (IS_ERR(drv_ctx))
  1061. return (struct skcipher_edesc *)drv_ctx;
  1062. src_nents = sg_nents_for_len(req->src, req->cryptlen);
  1063. if (unlikely(src_nents < 0)) {
  1064. dev_err(qidev, "Insufficient bytes (%d) in src S/G\n",
  1065. req->cryptlen);
  1066. return ERR_PTR(src_nents);
  1067. }
  1068. if (unlikely(req->src != req->dst)) {
  1069. dst_nents = sg_nents_for_len(req->dst, req->cryptlen);
  1070. if (unlikely(dst_nents < 0)) {
  1071. dev_err(qidev, "Insufficient bytes (%d) in dst S/G\n",
  1072. req->cryptlen);
  1073. return ERR_PTR(dst_nents);
  1074. }
  1075. mapped_src_nents = dma_map_sg(qidev, req->src, src_nents,
  1076. DMA_TO_DEVICE);
  1077. if (unlikely(!mapped_src_nents)) {
  1078. dev_err(qidev, "unable to map source\n");
  1079. return ERR_PTR(-ENOMEM);
  1080. }
  1081. mapped_dst_nents = dma_map_sg(qidev, req->dst, dst_nents,
  1082. DMA_FROM_DEVICE);
  1083. if (unlikely(!mapped_dst_nents)) {
  1084. dev_err(qidev, "unable to map destination\n");
  1085. dma_unmap_sg(qidev, req->src, src_nents, DMA_TO_DEVICE);
  1086. return ERR_PTR(-ENOMEM);
  1087. }
  1088. } else {
  1089. mapped_src_nents = dma_map_sg(qidev, req->src, src_nents,
  1090. DMA_BIDIRECTIONAL);
  1091. if (unlikely(!mapped_src_nents)) {
  1092. dev_err(qidev, "unable to map source\n");
  1093. return ERR_PTR(-ENOMEM);
  1094. }
  1095. }
  1096. qm_sg_ents = 1 + mapped_src_nents;
  1097. dst_sg_idx = qm_sg_ents;
  1098. /*
  1099. * Input, output HW S/G tables: [IV, src][dst, IV]
  1100. * IV entries point to the same buffer
  1101. * If src == dst, S/G entries are reused (S/G tables overlap)
  1102. *
  1103. * HW reads 4 S/G entries at a time; make sure the reads don't go beyond
  1104. * the end of the table by allocating more S/G entries.
  1105. */
  1106. if (req->src != req->dst)
  1107. qm_sg_ents += pad_sg_nents(mapped_dst_nents + 1);
  1108. else
  1109. qm_sg_ents = 1 + pad_sg_nents(qm_sg_ents);
  1110. qm_sg_bytes = qm_sg_ents * sizeof(struct qm_sg_entry);
  1111. if (unlikely(offsetof(struct skcipher_edesc, sgt) + qm_sg_bytes +
  1112. ivsize > CAAM_QI_MEMCACHE_SIZE)) {
  1113. dev_err(qidev, "No space for %d S/G entries and/or %dB IV\n",
  1114. qm_sg_ents, ivsize);
  1115. caam_unmap(qidev, req->src, req->dst, src_nents, dst_nents, 0,
  1116. 0, DMA_NONE, 0, 0);
  1117. return ERR_PTR(-ENOMEM);
  1118. }
  1119. /* allocate space for base edesc, link tables and IV */
  1120. edesc = qi_cache_alloc(GFP_DMA | flags);
  1121. if (unlikely(!edesc)) {
  1122. dev_err(qidev, "could not allocate extended descriptor\n");
  1123. caam_unmap(qidev, req->src, req->dst, src_nents, dst_nents, 0,
  1124. 0, DMA_NONE, 0, 0);
  1125. return ERR_PTR(-ENOMEM);
  1126. }
  1127. /* Make sure IV is located in a DMAable area */
  1128. sg_table = &edesc->sgt[0];
  1129. iv = (u8 *)(sg_table + qm_sg_ents);
  1130. memcpy(iv, req->iv, ivsize);
  1131. iv_dma = dma_map_single(qidev, iv, ivsize, DMA_BIDIRECTIONAL);
  1132. if (dma_mapping_error(qidev, iv_dma)) {
  1133. dev_err(qidev, "unable to map IV\n");
  1134. caam_unmap(qidev, req->src, req->dst, src_nents, dst_nents, 0,
  1135. 0, DMA_NONE, 0, 0);
  1136. qi_cache_free(edesc);
  1137. return ERR_PTR(-ENOMEM);
  1138. }
  1139. edesc->src_nents = src_nents;
  1140. edesc->dst_nents = dst_nents;
  1141. edesc->iv_dma = iv_dma;
  1142. edesc->qm_sg_bytes = qm_sg_bytes;
  1143. edesc->drv_req.app_ctx = req;
  1144. edesc->drv_req.cbk = skcipher_done;
  1145. edesc->drv_req.drv_ctx = drv_ctx;
  1146. dma_to_qm_sg_one(sg_table, iv_dma, ivsize, 0);
  1147. sg_to_qm_sg(req->src, req->cryptlen, sg_table + 1, 0);
  1148. if (req->src != req->dst)
  1149. sg_to_qm_sg(req->dst, req->cryptlen, sg_table + dst_sg_idx, 0);
  1150. dma_to_qm_sg_one(sg_table + dst_sg_idx + mapped_dst_nents, iv_dma,
  1151. ivsize, 0);
  1152. edesc->qm_sg_dma = dma_map_single(qidev, sg_table, edesc->qm_sg_bytes,
  1153. DMA_TO_DEVICE);
  1154. if (dma_mapping_error(qidev, edesc->qm_sg_dma)) {
  1155. dev_err(qidev, "unable to map S/G table\n");
  1156. caam_unmap(qidev, req->src, req->dst, src_nents, dst_nents,
  1157. iv_dma, ivsize, DMA_BIDIRECTIONAL, 0, 0);
  1158. qi_cache_free(edesc);
  1159. return ERR_PTR(-ENOMEM);
  1160. }
  1161. fd_sgt = &edesc->drv_req.fd_sgt[0];
  1162. dma_to_qm_sg_one_last_ext(&fd_sgt[1], edesc->qm_sg_dma,
  1163. ivsize + req->cryptlen, 0);
  1164. if (req->src == req->dst)
  1165. dma_to_qm_sg_one_ext(&fd_sgt[0], edesc->qm_sg_dma +
  1166. sizeof(*sg_table), req->cryptlen + ivsize,
  1167. 0);
  1168. else
  1169. dma_to_qm_sg_one_ext(&fd_sgt[0], edesc->qm_sg_dma + dst_sg_idx *
  1170. sizeof(*sg_table), req->cryptlen + ivsize,
  1171. 0);
  1172. return edesc;
  1173. }
  1174. static inline bool xts_skcipher_ivsize(struct skcipher_request *req)
  1175. {
  1176. struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
  1177. unsigned int ivsize = crypto_skcipher_ivsize(skcipher);
  1178. return !!get_unaligned((u64 *)(req->iv + (ivsize / 2)));
  1179. }
  1180. static inline int skcipher_crypt(struct skcipher_request *req, bool encrypt)
  1181. {
  1182. struct skcipher_edesc *edesc;
  1183. struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
  1184. struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
  1185. struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctx->jrdev->parent);
  1186. int ret;
  1187. /*
  1188. * XTS is expected to return an error even for input length = 0
  1189. * Note that the case input length < block size will be caught during
  1190. * HW offloading and return an error.
  1191. */
  1192. if (!req->cryptlen && !ctx->fallback)
  1193. return 0;
  1194. if (ctx->fallback && ((ctrlpriv->era <= 8 && xts_skcipher_ivsize(req)) ||
  1195. ctx->xts_key_fallback)) {
  1196. struct caam_skcipher_req_ctx *rctx = skcipher_request_ctx(req);
  1197. skcipher_request_set_tfm(&rctx->fallback_req, ctx->fallback);
  1198. skcipher_request_set_callback(&rctx->fallback_req,
  1199. req->base.flags,
  1200. req->base.complete,
  1201. req->base.data);
  1202. skcipher_request_set_crypt(&rctx->fallback_req, req->src,
  1203. req->dst, req->cryptlen, req->iv);
  1204. return encrypt ? crypto_skcipher_encrypt(&rctx->fallback_req) :
  1205. crypto_skcipher_decrypt(&rctx->fallback_req);
  1206. }
  1207. if (unlikely(caam_congested))
  1208. return -EAGAIN;
  1209. /* allocate extended descriptor */
  1210. edesc = skcipher_edesc_alloc(req, encrypt);
  1211. if (IS_ERR(edesc))
  1212. return PTR_ERR(edesc);
  1213. ret = caam_qi_enqueue(ctx->qidev, &edesc->drv_req);
  1214. if (!ret) {
  1215. ret = -EINPROGRESS;
  1216. } else {
  1217. skcipher_unmap(ctx->qidev, edesc, req);
  1218. qi_cache_free(edesc);
  1219. }
  1220. return ret;
  1221. }
  1222. static int skcipher_encrypt(struct skcipher_request *req)
  1223. {
  1224. return skcipher_crypt(req, true);
  1225. }
  1226. static int skcipher_decrypt(struct skcipher_request *req)
  1227. {
  1228. return skcipher_crypt(req, false);
  1229. }
  1230. static struct caam_skcipher_alg driver_algs[] = {
  1231. {
  1232. .skcipher = {
  1233. .base = {
  1234. .cra_name = "cbc(aes)",
  1235. .cra_driver_name = "cbc-aes-caam-qi",
  1236. .cra_blocksize = AES_BLOCK_SIZE,
  1237. },
  1238. .setkey = aes_skcipher_setkey,
  1239. .encrypt = skcipher_encrypt,
  1240. .decrypt = skcipher_decrypt,
  1241. .min_keysize = AES_MIN_KEY_SIZE,
  1242. .max_keysize = AES_MAX_KEY_SIZE,
  1243. .ivsize = AES_BLOCK_SIZE,
  1244. },
  1245. .caam.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1246. },
  1247. {
  1248. .skcipher = {
  1249. .base = {
  1250. .cra_name = "cbc(des3_ede)",
  1251. .cra_driver_name = "cbc-3des-caam-qi",
  1252. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1253. },
  1254. .setkey = des3_skcipher_setkey,
  1255. .encrypt = skcipher_encrypt,
  1256. .decrypt = skcipher_decrypt,
  1257. .min_keysize = DES3_EDE_KEY_SIZE,
  1258. .max_keysize = DES3_EDE_KEY_SIZE,
  1259. .ivsize = DES3_EDE_BLOCK_SIZE,
  1260. },
  1261. .caam.class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1262. },
  1263. {
  1264. .skcipher = {
  1265. .base = {
  1266. .cra_name = "cbc(des)",
  1267. .cra_driver_name = "cbc-des-caam-qi",
  1268. .cra_blocksize = DES_BLOCK_SIZE,
  1269. },
  1270. .setkey = des_skcipher_setkey,
  1271. .encrypt = skcipher_encrypt,
  1272. .decrypt = skcipher_decrypt,
  1273. .min_keysize = DES_KEY_SIZE,
  1274. .max_keysize = DES_KEY_SIZE,
  1275. .ivsize = DES_BLOCK_SIZE,
  1276. },
  1277. .caam.class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  1278. },
  1279. {
  1280. .skcipher = {
  1281. .base = {
  1282. .cra_name = "ctr(aes)",
  1283. .cra_driver_name = "ctr-aes-caam-qi",
  1284. .cra_blocksize = 1,
  1285. },
  1286. .setkey = ctr_skcipher_setkey,
  1287. .encrypt = skcipher_encrypt,
  1288. .decrypt = skcipher_decrypt,
  1289. .min_keysize = AES_MIN_KEY_SIZE,
  1290. .max_keysize = AES_MAX_KEY_SIZE,
  1291. .ivsize = AES_BLOCK_SIZE,
  1292. .chunksize = AES_BLOCK_SIZE,
  1293. },
  1294. .caam.class1_alg_type = OP_ALG_ALGSEL_AES |
  1295. OP_ALG_AAI_CTR_MOD128,
  1296. },
  1297. {
  1298. .skcipher = {
  1299. .base = {
  1300. .cra_name = "rfc3686(ctr(aes))",
  1301. .cra_driver_name = "rfc3686-ctr-aes-caam-qi",
  1302. .cra_blocksize = 1,
  1303. },
  1304. .setkey = rfc3686_skcipher_setkey,
  1305. .encrypt = skcipher_encrypt,
  1306. .decrypt = skcipher_decrypt,
  1307. .min_keysize = AES_MIN_KEY_SIZE +
  1308. CTR_RFC3686_NONCE_SIZE,
  1309. .max_keysize = AES_MAX_KEY_SIZE +
  1310. CTR_RFC3686_NONCE_SIZE,
  1311. .ivsize = CTR_RFC3686_IV_SIZE,
  1312. .chunksize = AES_BLOCK_SIZE,
  1313. },
  1314. .caam = {
  1315. .class1_alg_type = OP_ALG_ALGSEL_AES |
  1316. OP_ALG_AAI_CTR_MOD128,
  1317. .rfc3686 = true,
  1318. },
  1319. },
  1320. {
  1321. .skcipher = {
  1322. .base = {
  1323. .cra_name = "xts(aes)",
  1324. .cra_driver_name = "xts-aes-caam-qi",
  1325. .cra_flags = CRYPTO_ALG_NEED_FALLBACK,
  1326. .cra_blocksize = AES_BLOCK_SIZE,
  1327. },
  1328. .setkey = xts_skcipher_setkey,
  1329. .encrypt = skcipher_encrypt,
  1330. .decrypt = skcipher_decrypt,
  1331. .min_keysize = 2 * AES_MIN_KEY_SIZE,
  1332. .max_keysize = 2 * AES_MAX_KEY_SIZE,
  1333. .ivsize = AES_BLOCK_SIZE,
  1334. },
  1335. .caam.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_XTS,
  1336. },
  1337. };
  1338. static struct caam_aead_alg driver_aeads[] = {
  1339. {
  1340. .aead = {
  1341. .base = {
  1342. .cra_name = "rfc4106(gcm(aes))",
  1343. .cra_driver_name = "rfc4106-gcm-aes-caam-qi",
  1344. .cra_blocksize = 1,
  1345. },
  1346. .setkey = rfc4106_setkey,
  1347. .setauthsize = rfc4106_setauthsize,
  1348. .encrypt = ipsec_gcm_encrypt,
  1349. .decrypt = ipsec_gcm_decrypt,
  1350. .ivsize = 8,
  1351. .maxauthsize = AES_BLOCK_SIZE,
  1352. },
  1353. .caam = {
  1354. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_GCM,
  1355. .nodkp = true,
  1356. },
  1357. },
  1358. {
  1359. .aead = {
  1360. .base = {
  1361. .cra_name = "rfc4543(gcm(aes))",
  1362. .cra_driver_name = "rfc4543-gcm-aes-caam-qi",
  1363. .cra_blocksize = 1,
  1364. },
  1365. .setkey = rfc4543_setkey,
  1366. .setauthsize = rfc4543_setauthsize,
  1367. .encrypt = ipsec_gcm_encrypt,
  1368. .decrypt = ipsec_gcm_decrypt,
  1369. .ivsize = 8,
  1370. .maxauthsize = AES_BLOCK_SIZE,
  1371. },
  1372. .caam = {
  1373. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_GCM,
  1374. .nodkp = true,
  1375. },
  1376. },
  1377. /* Galois Counter Mode */
  1378. {
  1379. .aead = {
  1380. .base = {
  1381. .cra_name = "gcm(aes)",
  1382. .cra_driver_name = "gcm-aes-caam-qi",
  1383. .cra_blocksize = 1,
  1384. },
  1385. .setkey = gcm_setkey,
  1386. .setauthsize = gcm_setauthsize,
  1387. .encrypt = aead_encrypt,
  1388. .decrypt = aead_decrypt,
  1389. .ivsize = 12,
  1390. .maxauthsize = AES_BLOCK_SIZE,
  1391. },
  1392. .caam = {
  1393. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_GCM,
  1394. .nodkp = true,
  1395. }
  1396. },
  1397. /* single-pass ipsec_esp descriptor */
  1398. {
  1399. .aead = {
  1400. .base = {
  1401. .cra_name = "authenc(hmac(md5),cbc(aes))",
  1402. .cra_driver_name = "authenc-hmac-md5-"
  1403. "cbc-aes-caam-qi",
  1404. .cra_blocksize = AES_BLOCK_SIZE,
  1405. },
  1406. .setkey = aead_setkey,
  1407. .setauthsize = aead_setauthsize,
  1408. .encrypt = aead_encrypt,
  1409. .decrypt = aead_decrypt,
  1410. .ivsize = AES_BLOCK_SIZE,
  1411. .maxauthsize = MD5_DIGEST_SIZE,
  1412. },
  1413. .caam = {
  1414. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1415. .class2_alg_type = OP_ALG_ALGSEL_MD5 |
  1416. OP_ALG_AAI_HMAC_PRECOMP,
  1417. }
  1418. },
  1419. {
  1420. .aead = {
  1421. .base = {
  1422. .cra_name = "echainiv(authenc(hmac(md5),"
  1423. "cbc(aes)))",
  1424. .cra_driver_name = "echainiv-authenc-hmac-md5-"
  1425. "cbc-aes-caam-qi",
  1426. .cra_blocksize = AES_BLOCK_SIZE,
  1427. },
  1428. .setkey = aead_setkey,
  1429. .setauthsize = aead_setauthsize,
  1430. .encrypt = aead_encrypt,
  1431. .decrypt = aead_decrypt,
  1432. .ivsize = AES_BLOCK_SIZE,
  1433. .maxauthsize = MD5_DIGEST_SIZE,
  1434. },
  1435. .caam = {
  1436. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1437. .class2_alg_type = OP_ALG_ALGSEL_MD5 |
  1438. OP_ALG_AAI_HMAC_PRECOMP,
  1439. .geniv = true,
  1440. }
  1441. },
  1442. {
  1443. .aead = {
  1444. .base = {
  1445. .cra_name = "authenc(hmac(sha1),cbc(aes))",
  1446. .cra_driver_name = "authenc-hmac-sha1-"
  1447. "cbc-aes-caam-qi",
  1448. .cra_blocksize = AES_BLOCK_SIZE,
  1449. },
  1450. .setkey = aead_setkey,
  1451. .setauthsize = aead_setauthsize,
  1452. .encrypt = aead_encrypt,
  1453. .decrypt = aead_decrypt,
  1454. .ivsize = AES_BLOCK_SIZE,
  1455. .maxauthsize = SHA1_DIGEST_SIZE,
  1456. },
  1457. .caam = {
  1458. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1459. .class2_alg_type = OP_ALG_ALGSEL_SHA1 |
  1460. OP_ALG_AAI_HMAC_PRECOMP,
  1461. }
  1462. },
  1463. {
  1464. .aead = {
  1465. .base = {
  1466. .cra_name = "echainiv(authenc(hmac(sha1),"
  1467. "cbc(aes)))",
  1468. .cra_driver_name = "echainiv-authenc-"
  1469. "hmac-sha1-cbc-aes-caam-qi",
  1470. .cra_blocksize = AES_BLOCK_SIZE,
  1471. },
  1472. .setkey = aead_setkey,
  1473. .setauthsize = aead_setauthsize,
  1474. .encrypt = aead_encrypt,
  1475. .decrypt = aead_decrypt,
  1476. .ivsize = AES_BLOCK_SIZE,
  1477. .maxauthsize = SHA1_DIGEST_SIZE,
  1478. },
  1479. .caam = {
  1480. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1481. .class2_alg_type = OP_ALG_ALGSEL_SHA1 |
  1482. OP_ALG_AAI_HMAC_PRECOMP,
  1483. .geniv = true,
  1484. },
  1485. },
  1486. {
  1487. .aead = {
  1488. .base = {
  1489. .cra_name = "authenc(hmac(sha224),cbc(aes))",
  1490. .cra_driver_name = "authenc-hmac-sha224-"
  1491. "cbc-aes-caam-qi",
  1492. .cra_blocksize = AES_BLOCK_SIZE,
  1493. },
  1494. .setkey = aead_setkey,
  1495. .setauthsize = aead_setauthsize,
  1496. .encrypt = aead_encrypt,
  1497. .decrypt = aead_decrypt,
  1498. .ivsize = AES_BLOCK_SIZE,
  1499. .maxauthsize = SHA224_DIGEST_SIZE,
  1500. },
  1501. .caam = {
  1502. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1503. .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
  1504. OP_ALG_AAI_HMAC_PRECOMP,
  1505. }
  1506. },
  1507. {
  1508. .aead = {
  1509. .base = {
  1510. .cra_name = "echainiv(authenc(hmac(sha224),"
  1511. "cbc(aes)))",
  1512. .cra_driver_name = "echainiv-authenc-"
  1513. "hmac-sha224-cbc-aes-caam-qi",
  1514. .cra_blocksize = AES_BLOCK_SIZE,
  1515. },
  1516. .setkey = aead_setkey,
  1517. .setauthsize = aead_setauthsize,
  1518. .encrypt = aead_encrypt,
  1519. .decrypt = aead_decrypt,
  1520. .ivsize = AES_BLOCK_SIZE,
  1521. .maxauthsize = SHA224_DIGEST_SIZE,
  1522. },
  1523. .caam = {
  1524. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1525. .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
  1526. OP_ALG_AAI_HMAC_PRECOMP,
  1527. .geniv = true,
  1528. }
  1529. },
  1530. {
  1531. .aead = {
  1532. .base = {
  1533. .cra_name = "authenc(hmac(sha256),cbc(aes))",
  1534. .cra_driver_name = "authenc-hmac-sha256-"
  1535. "cbc-aes-caam-qi",
  1536. .cra_blocksize = AES_BLOCK_SIZE,
  1537. },
  1538. .setkey = aead_setkey,
  1539. .setauthsize = aead_setauthsize,
  1540. .encrypt = aead_encrypt,
  1541. .decrypt = aead_decrypt,
  1542. .ivsize = AES_BLOCK_SIZE,
  1543. .maxauthsize = SHA256_DIGEST_SIZE,
  1544. },
  1545. .caam = {
  1546. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1547. .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
  1548. OP_ALG_AAI_HMAC_PRECOMP,
  1549. }
  1550. },
  1551. {
  1552. .aead = {
  1553. .base = {
  1554. .cra_name = "echainiv(authenc(hmac(sha256),"
  1555. "cbc(aes)))",
  1556. .cra_driver_name = "echainiv-authenc-"
  1557. "hmac-sha256-cbc-aes-"
  1558. "caam-qi",
  1559. .cra_blocksize = AES_BLOCK_SIZE,
  1560. },
  1561. .setkey = aead_setkey,
  1562. .setauthsize = aead_setauthsize,
  1563. .encrypt = aead_encrypt,
  1564. .decrypt = aead_decrypt,
  1565. .ivsize = AES_BLOCK_SIZE,
  1566. .maxauthsize = SHA256_DIGEST_SIZE,
  1567. },
  1568. .caam = {
  1569. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1570. .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
  1571. OP_ALG_AAI_HMAC_PRECOMP,
  1572. .geniv = true,
  1573. }
  1574. },
  1575. {
  1576. .aead = {
  1577. .base = {
  1578. .cra_name = "authenc(hmac(sha384),cbc(aes))",
  1579. .cra_driver_name = "authenc-hmac-sha384-"
  1580. "cbc-aes-caam-qi",
  1581. .cra_blocksize = AES_BLOCK_SIZE,
  1582. },
  1583. .setkey = aead_setkey,
  1584. .setauthsize = aead_setauthsize,
  1585. .encrypt = aead_encrypt,
  1586. .decrypt = aead_decrypt,
  1587. .ivsize = AES_BLOCK_SIZE,
  1588. .maxauthsize = SHA384_DIGEST_SIZE,
  1589. },
  1590. .caam = {
  1591. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1592. .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
  1593. OP_ALG_AAI_HMAC_PRECOMP,
  1594. }
  1595. },
  1596. {
  1597. .aead = {
  1598. .base = {
  1599. .cra_name = "echainiv(authenc(hmac(sha384),"
  1600. "cbc(aes)))",
  1601. .cra_driver_name = "echainiv-authenc-"
  1602. "hmac-sha384-cbc-aes-"
  1603. "caam-qi",
  1604. .cra_blocksize = AES_BLOCK_SIZE,
  1605. },
  1606. .setkey = aead_setkey,
  1607. .setauthsize = aead_setauthsize,
  1608. .encrypt = aead_encrypt,
  1609. .decrypt = aead_decrypt,
  1610. .ivsize = AES_BLOCK_SIZE,
  1611. .maxauthsize = SHA384_DIGEST_SIZE,
  1612. },
  1613. .caam = {
  1614. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1615. .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
  1616. OP_ALG_AAI_HMAC_PRECOMP,
  1617. .geniv = true,
  1618. }
  1619. },
  1620. {
  1621. .aead = {
  1622. .base = {
  1623. .cra_name = "authenc(hmac(sha512),cbc(aes))",
  1624. .cra_driver_name = "authenc-hmac-sha512-"
  1625. "cbc-aes-caam-qi",
  1626. .cra_blocksize = AES_BLOCK_SIZE,
  1627. },
  1628. .setkey = aead_setkey,
  1629. .setauthsize = aead_setauthsize,
  1630. .encrypt = aead_encrypt,
  1631. .decrypt = aead_decrypt,
  1632. .ivsize = AES_BLOCK_SIZE,
  1633. .maxauthsize = SHA512_DIGEST_SIZE,
  1634. },
  1635. .caam = {
  1636. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1637. .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
  1638. OP_ALG_AAI_HMAC_PRECOMP,
  1639. }
  1640. },
  1641. {
  1642. .aead = {
  1643. .base = {
  1644. .cra_name = "echainiv(authenc(hmac(sha512),"
  1645. "cbc(aes)))",
  1646. .cra_driver_name = "echainiv-authenc-"
  1647. "hmac-sha512-cbc-aes-"
  1648. "caam-qi",
  1649. .cra_blocksize = AES_BLOCK_SIZE,
  1650. },
  1651. .setkey = aead_setkey,
  1652. .setauthsize = aead_setauthsize,
  1653. .encrypt = aead_encrypt,
  1654. .decrypt = aead_decrypt,
  1655. .ivsize = AES_BLOCK_SIZE,
  1656. .maxauthsize = SHA512_DIGEST_SIZE,
  1657. },
  1658. .caam = {
  1659. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1660. .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
  1661. OP_ALG_AAI_HMAC_PRECOMP,
  1662. .geniv = true,
  1663. }
  1664. },
  1665. {
  1666. .aead = {
  1667. .base = {
  1668. .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
  1669. .cra_driver_name = "authenc-hmac-md5-"
  1670. "cbc-des3_ede-caam-qi",
  1671. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1672. },
  1673. .setkey = des3_aead_setkey,
  1674. .setauthsize = aead_setauthsize,
  1675. .encrypt = aead_encrypt,
  1676. .decrypt = aead_decrypt,
  1677. .ivsize = DES3_EDE_BLOCK_SIZE,
  1678. .maxauthsize = MD5_DIGEST_SIZE,
  1679. },
  1680. .caam = {
  1681. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1682. .class2_alg_type = OP_ALG_ALGSEL_MD5 |
  1683. OP_ALG_AAI_HMAC_PRECOMP,
  1684. }
  1685. },
  1686. {
  1687. .aead = {
  1688. .base = {
  1689. .cra_name = "echainiv(authenc(hmac(md5),"
  1690. "cbc(des3_ede)))",
  1691. .cra_driver_name = "echainiv-authenc-hmac-md5-"
  1692. "cbc-des3_ede-caam-qi",
  1693. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1694. },
  1695. .setkey = des3_aead_setkey,
  1696. .setauthsize = aead_setauthsize,
  1697. .encrypt = aead_encrypt,
  1698. .decrypt = aead_decrypt,
  1699. .ivsize = DES3_EDE_BLOCK_SIZE,
  1700. .maxauthsize = MD5_DIGEST_SIZE,
  1701. },
  1702. .caam = {
  1703. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1704. .class2_alg_type = OP_ALG_ALGSEL_MD5 |
  1705. OP_ALG_AAI_HMAC_PRECOMP,
  1706. .geniv = true,
  1707. }
  1708. },
  1709. {
  1710. .aead = {
  1711. .base = {
  1712. .cra_name = "authenc(hmac(sha1),"
  1713. "cbc(des3_ede))",
  1714. .cra_driver_name = "authenc-hmac-sha1-"
  1715. "cbc-des3_ede-caam-qi",
  1716. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1717. },
  1718. .setkey = des3_aead_setkey,
  1719. .setauthsize = aead_setauthsize,
  1720. .encrypt = aead_encrypt,
  1721. .decrypt = aead_decrypt,
  1722. .ivsize = DES3_EDE_BLOCK_SIZE,
  1723. .maxauthsize = SHA1_DIGEST_SIZE,
  1724. },
  1725. .caam = {
  1726. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1727. .class2_alg_type = OP_ALG_ALGSEL_SHA1 |
  1728. OP_ALG_AAI_HMAC_PRECOMP,
  1729. },
  1730. },
  1731. {
  1732. .aead = {
  1733. .base = {
  1734. .cra_name = "echainiv(authenc(hmac(sha1),"
  1735. "cbc(des3_ede)))",
  1736. .cra_driver_name = "echainiv-authenc-"
  1737. "hmac-sha1-"
  1738. "cbc-des3_ede-caam-qi",
  1739. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1740. },
  1741. .setkey = des3_aead_setkey,
  1742. .setauthsize = aead_setauthsize,
  1743. .encrypt = aead_encrypt,
  1744. .decrypt = aead_decrypt,
  1745. .ivsize = DES3_EDE_BLOCK_SIZE,
  1746. .maxauthsize = SHA1_DIGEST_SIZE,
  1747. },
  1748. .caam = {
  1749. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1750. .class2_alg_type = OP_ALG_ALGSEL_SHA1 |
  1751. OP_ALG_AAI_HMAC_PRECOMP,
  1752. .geniv = true,
  1753. }
  1754. },
  1755. {
  1756. .aead = {
  1757. .base = {
  1758. .cra_name = "authenc(hmac(sha224),"
  1759. "cbc(des3_ede))",
  1760. .cra_driver_name = "authenc-hmac-sha224-"
  1761. "cbc-des3_ede-caam-qi",
  1762. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1763. },
  1764. .setkey = des3_aead_setkey,
  1765. .setauthsize = aead_setauthsize,
  1766. .encrypt = aead_encrypt,
  1767. .decrypt = aead_decrypt,
  1768. .ivsize = DES3_EDE_BLOCK_SIZE,
  1769. .maxauthsize = SHA224_DIGEST_SIZE,
  1770. },
  1771. .caam = {
  1772. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1773. .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
  1774. OP_ALG_AAI_HMAC_PRECOMP,
  1775. },
  1776. },
  1777. {
  1778. .aead = {
  1779. .base = {
  1780. .cra_name = "echainiv(authenc(hmac(sha224),"
  1781. "cbc(des3_ede)))",
  1782. .cra_driver_name = "echainiv-authenc-"
  1783. "hmac-sha224-"
  1784. "cbc-des3_ede-caam-qi",
  1785. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1786. },
  1787. .setkey = des3_aead_setkey,
  1788. .setauthsize = aead_setauthsize,
  1789. .encrypt = aead_encrypt,
  1790. .decrypt = aead_decrypt,
  1791. .ivsize = DES3_EDE_BLOCK_SIZE,
  1792. .maxauthsize = SHA224_DIGEST_SIZE,
  1793. },
  1794. .caam = {
  1795. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1796. .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
  1797. OP_ALG_AAI_HMAC_PRECOMP,
  1798. .geniv = true,
  1799. }
  1800. },
  1801. {
  1802. .aead = {
  1803. .base = {
  1804. .cra_name = "authenc(hmac(sha256),"
  1805. "cbc(des3_ede))",
  1806. .cra_driver_name = "authenc-hmac-sha256-"
  1807. "cbc-des3_ede-caam-qi",
  1808. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1809. },
  1810. .setkey = des3_aead_setkey,
  1811. .setauthsize = aead_setauthsize,
  1812. .encrypt = aead_encrypt,
  1813. .decrypt = aead_decrypt,
  1814. .ivsize = DES3_EDE_BLOCK_SIZE,
  1815. .maxauthsize = SHA256_DIGEST_SIZE,
  1816. },
  1817. .caam = {
  1818. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1819. .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
  1820. OP_ALG_AAI_HMAC_PRECOMP,
  1821. },
  1822. },
  1823. {
  1824. .aead = {
  1825. .base = {
  1826. .cra_name = "echainiv(authenc(hmac(sha256),"
  1827. "cbc(des3_ede)))",
  1828. .cra_driver_name = "echainiv-authenc-"
  1829. "hmac-sha256-"
  1830. "cbc-des3_ede-caam-qi",
  1831. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1832. },
  1833. .setkey = des3_aead_setkey,
  1834. .setauthsize = aead_setauthsize,
  1835. .encrypt = aead_encrypt,
  1836. .decrypt = aead_decrypt,
  1837. .ivsize = DES3_EDE_BLOCK_SIZE,
  1838. .maxauthsize = SHA256_DIGEST_SIZE,
  1839. },
  1840. .caam = {
  1841. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1842. .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
  1843. OP_ALG_AAI_HMAC_PRECOMP,
  1844. .geniv = true,
  1845. }
  1846. },
  1847. {
  1848. .aead = {
  1849. .base = {
  1850. .cra_name = "authenc(hmac(sha384),"
  1851. "cbc(des3_ede))",
  1852. .cra_driver_name = "authenc-hmac-sha384-"
  1853. "cbc-des3_ede-caam-qi",
  1854. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1855. },
  1856. .setkey = des3_aead_setkey,
  1857. .setauthsize = aead_setauthsize,
  1858. .encrypt = aead_encrypt,
  1859. .decrypt = aead_decrypt,
  1860. .ivsize = DES3_EDE_BLOCK_SIZE,
  1861. .maxauthsize = SHA384_DIGEST_SIZE,
  1862. },
  1863. .caam = {
  1864. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1865. .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
  1866. OP_ALG_AAI_HMAC_PRECOMP,
  1867. },
  1868. },
  1869. {
  1870. .aead = {
  1871. .base = {
  1872. .cra_name = "echainiv(authenc(hmac(sha384),"
  1873. "cbc(des3_ede)))",
  1874. .cra_driver_name = "echainiv-authenc-"
  1875. "hmac-sha384-"
  1876. "cbc-des3_ede-caam-qi",
  1877. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1878. },
  1879. .setkey = des3_aead_setkey,
  1880. .setauthsize = aead_setauthsize,
  1881. .encrypt = aead_encrypt,
  1882. .decrypt = aead_decrypt,
  1883. .ivsize = DES3_EDE_BLOCK_SIZE,
  1884. .maxauthsize = SHA384_DIGEST_SIZE,
  1885. },
  1886. .caam = {
  1887. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1888. .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
  1889. OP_ALG_AAI_HMAC_PRECOMP,
  1890. .geniv = true,
  1891. }
  1892. },
  1893. {
  1894. .aead = {
  1895. .base = {
  1896. .cra_name = "authenc(hmac(sha512),"
  1897. "cbc(des3_ede))",
  1898. .cra_driver_name = "authenc-hmac-sha512-"
  1899. "cbc-des3_ede-caam-qi",
  1900. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1901. },
  1902. .setkey = des3_aead_setkey,
  1903. .setauthsize = aead_setauthsize,
  1904. .encrypt = aead_encrypt,
  1905. .decrypt = aead_decrypt,
  1906. .ivsize = DES3_EDE_BLOCK_SIZE,
  1907. .maxauthsize = SHA512_DIGEST_SIZE,
  1908. },
  1909. .caam = {
  1910. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1911. .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
  1912. OP_ALG_AAI_HMAC_PRECOMP,
  1913. },
  1914. },
  1915. {
  1916. .aead = {
  1917. .base = {
  1918. .cra_name = "echainiv(authenc(hmac(sha512),"
  1919. "cbc(des3_ede)))",
  1920. .cra_driver_name = "echainiv-authenc-"
  1921. "hmac-sha512-"
  1922. "cbc-des3_ede-caam-qi",
  1923. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1924. },
  1925. .setkey = des3_aead_setkey,
  1926. .setauthsize = aead_setauthsize,
  1927. .encrypt = aead_encrypt,
  1928. .decrypt = aead_decrypt,
  1929. .ivsize = DES3_EDE_BLOCK_SIZE,
  1930. .maxauthsize = SHA512_DIGEST_SIZE,
  1931. },
  1932. .caam = {
  1933. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1934. .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
  1935. OP_ALG_AAI_HMAC_PRECOMP,
  1936. .geniv = true,
  1937. }
  1938. },
  1939. {
  1940. .aead = {
  1941. .base = {
  1942. .cra_name = "authenc(hmac(md5),cbc(des))",
  1943. .cra_driver_name = "authenc-hmac-md5-"
  1944. "cbc-des-caam-qi",
  1945. .cra_blocksize = DES_BLOCK_SIZE,
  1946. },
  1947. .setkey = aead_setkey,
  1948. .setauthsize = aead_setauthsize,
  1949. .encrypt = aead_encrypt,
  1950. .decrypt = aead_decrypt,
  1951. .ivsize = DES_BLOCK_SIZE,
  1952. .maxauthsize = MD5_DIGEST_SIZE,
  1953. },
  1954. .caam = {
  1955. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  1956. .class2_alg_type = OP_ALG_ALGSEL_MD5 |
  1957. OP_ALG_AAI_HMAC_PRECOMP,
  1958. },
  1959. },
  1960. {
  1961. .aead = {
  1962. .base = {
  1963. .cra_name = "echainiv(authenc(hmac(md5),"
  1964. "cbc(des)))",
  1965. .cra_driver_name = "echainiv-authenc-hmac-md5-"
  1966. "cbc-des-caam-qi",
  1967. .cra_blocksize = DES_BLOCK_SIZE,
  1968. },
  1969. .setkey = aead_setkey,
  1970. .setauthsize = aead_setauthsize,
  1971. .encrypt = aead_encrypt,
  1972. .decrypt = aead_decrypt,
  1973. .ivsize = DES_BLOCK_SIZE,
  1974. .maxauthsize = MD5_DIGEST_SIZE,
  1975. },
  1976. .caam = {
  1977. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  1978. .class2_alg_type = OP_ALG_ALGSEL_MD5 |
  1979. OP_ALG_AAI_HMAC_PRECOMP,
  1980. .geniv = true,
  1981. }
  1982. },
  1983. {
  1984. .aead = {
  1985. .base = {
  1986. .cra_name = "authenc(hmac(sha1),cbc(des))",
  1987. .cra_driver_name = "authenc-hmac-sha1-"
  1988. "cbc-des-caam-qi",
  1989. .cra_blocksize = DES_BLOCK_SIZE,
  1990. },
  1991. .setkey = aead_setkey,
  1992. .setauthsize = aead_setauthsize,
  1993. .encrypt = aead_encrypt,
  1994. .decrypt = aead_decrypt,
  1995. .ivsize = DES_BLOCK_SIZE,
  1996. .maxauthsize = SHA1_DIGEST_SIZE,
  1997. },
  1998. .caam = {
  1999. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  2000. .class2_alg_type = OP_ALG_ALGSEL_SHA1 |
  2001. OP_ALG_AAI_HMAC_PRECOMP,
  2002. },
  2003. },
  2004. {
  2005. .aead = {
  2006. .base = {
  2007. .cra_name = "echainiv(authenc(hmac(sha1),"
  2008. "cbc(des)))",
  2009. .cra_driver_name = "echainiv-authenc-"
  2010. "hmac-sha1-cbc-des-caam-qi",
  2011. .cra_blocksize = DES_BLOCK_SIZE,
  2012. },
  2013. .setkey = aead_setkey,
  2014. .setauthsize = aead_setauthsize,
  2015. .encrypt = aead_encrypt,
  2016. .decrypt = aead_decrypt,
  2017. .ivsize = DES_BLOCK_SIZE,
  2018. .maxauthsize = SHA1_DIGEST_SIZE,
  2019. },
  2020. .caam = {
  2021. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  2022. .class2_alg_type = OP_ALG_ALGSEL_SHA1 |
  2023. OP_ALG_AAI_HMAC_PRECOMP,
  2024. .geniv = true,
  2025. }
  2026. },
  2027. {
  2028. .aead = {
  2029. .base = {
  2030. .cra_name = "authenc(hmac(sha224),cbc(des))",
  2031. .cra_driver_name = "authenc-hmac-sha224-"
  2032. "cbc-des-caam-qi",
  2033. .cra_blocksize = DES_BLOCK_SIZE,
  2034. },
  2035. .setkey = aead_setkey,
  2036. .setauthsize = aead_setauthsize,
  2037. .encrypt = aead_encrypt,
  2038. .decrypt = aead_decrypt,
  2039. .ivsize = DES_BLOCK_SIZE,
  2040. .maxauthsize = SHA224_DIGEST_SIZE,
  2041. },
  2042. .caam = {
  2043. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  2044. .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
  2045. OP_ALG_AAI_HMAC_PRECOMP,
  2046. },
  2047. },
  2048. {
  2049. .aead = {
  2050. .base = {
  2051. .cra_name = "echainiv(authenc(hmac(sha224),"
  2052. "cbc(des)))",
  2053. .cra_driver_name = "echainiv-authenc-"
  2054. "hmac-sha224-cbc-des-"
  2055. "caam-qi",
  2056. .cra_blocksize = DES_BLOCK_SIZE,
  2057. },
  2058. .setkey = aead_setkey,
  2059. .setauthsize = aead_setauthsize,
  2060. .encrypt = aead_encrypt,
  2061. .decrypt = aead_decrypt,
  2062. .ivsize = DES_BLOCK_SIZE,
  2063. .maxauthsize = SHA224_DIGEST_SIZE,
  2064. },
  2065. .caam = {
  2066. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  2067. .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
  2068. OP_ALG_AAI_HMAC_PRECOMP,
  2069. .geniv = true,
  2070. }
  2071. },
  2072. {
  2073. .aead = {
  2074. .base = {
  2075. .cra_name = "authenc(hmac(sha256),cbc(des))",
  2076. .cra_driver_name = "authenc-hmac-sha256-"
  2077. "cbc-des-caam-qi",
  2078. .cra_blocksize = DES_BLOCK_SIZE,
  2079. },
  2080. .setkey = aead_setkey,
  2081. .setauthsize = aead_setauthsize,
  2082. .encrypt = aead_encrypt,
  2083. .decrypt = aead_decrypt,
  2084. .ivsize = DES_BLOCK_SIZE,
  2085. .maxauthsize = SHA256_DIGEST_SIZE,
  2086. },
  2087. .caam = {
  2088. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  2089. .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
  2090. OP_ALG_AAI_HMAC_PRECOMP,
  2091. },
  2092. },
  2093. {
  2094. .aead = {
  2095. .base = {
  2096. .cra_name = "echainiv(authenc(hmac(sha256),"
  2097. "cbc(des)))",
  2098. .cra_driver_name = "echainiv-authenc-"
  2099. "hmac-sha256-cbc-des-"
  2100. "caam-qi",
  2101. .cra_blocksize = DES_BLOCK_SIZE,
  2102. },
  2103. .setkey = aead_setkey,
  2104. .setauthsize = aead_setauthsize,
  2105. .encrypt = aead_encrypt,
  2106. .decrypt = aead_decrypt,
  2107. .ivsize = DES_BLOCK_SIZE,
  2108. .maxauthsize = SHA256_DIGEST_SIZE,
  2109. },
  2110. .caam = {
  2111. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  2112. .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
  2113. OP_ALG_AAI_HMAC_PRECOMP,
  2114. .geniv = true,
  2115. },
  2116. },
  2117. {
  2118. .aead = {
  2119. .base = {
  2120. .cra_name = "authenc(hmac(sha384),cbc(des))",
  2121. .cra_driver_name = "authenc-hmac-sha384-"
  2122. "cbc-des-caam-qi",
  2123. .cra_blocksize = DES_BLOCK_SIZE,
  2124. },
  2125. .setkey = aead_setkey,
  2126. .setauthsize = aead_setauthsize,
  2127. .encrypt = aead_encrypt,
  2128. .decrypt = aead_decrypt,
  2129. .ivsize = DES_BLOCK_SIZE,
  2130. .maxauthsize = SHA384_DIGEST_SIZE,
  2131. },
  2132. .caam = {
  2133. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  2134. .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
  2135. OP_ALG_AAI_HMAC_PRECOMP,
  2136. },
  2137. },
  2138. {
  2139. .aead = {
  2140. .base = {
  2141. .cra_name = "echainiv(authenc(hmac(sha384),"
  2142. "cbc(des)))",
  2143. .cra_driver_name = "echainiv-authenc-"
  2144. "hmac-sha384-cbc-des-"
  2145. "caam-qi",
  2146. .cra_blocksize = DES_BLOCK_SIZE,
  2147. },
  2148. .setkey = aead_setkey,
  2149. .setauthsize = aead_setauthsize,
  2150. .encrypt = aead_encrypt,
  2151. .decrypt = aead_decrypt,
  2152. .ivsize = DES_BLOCK_SIZE,
  2153. .maxauthsize = SHA384_DIGEST_SIZE,
  2154. },
  2155. .caam = {
  2156. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  2157. .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
  2158. OP_ALG_AAI_HMAC_PRECOMP,
  2159. .geniv = true,
  2160. }
  2161. },
  2162. {
  2163. .aead = {
  2164. .base = {
  2165. .cra_name = "authenc(hmac(sha512),cbc(des))",
  2166. .cra_driver_name = "authenc-hmac-sha512-"
  2167. "cbc-des-caam-qi",
  2168. .cra_blocksize = DES_BLOCK_SIZE,
  2169. },
  2170. .setkey = aead_setkey,
  2171. .setauthsize = aead_setauthsize,
  2172. .encrypt = aead_encrypt,
  2173. .decrypt = aead_decrypt,
  2174. .ivsize = DES_BLOCK_SIZE,
  2175. .maxauthsize = SHA512_DIGEST_SIZE,
  2176. },
  2177. .caam = {
  2178. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  2179. .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
  2180. OP_ALG_AAI_HMAC_PRECOMP,
  2181. }
  2182. },
  2183. {
  2184. .aead = {
  2185. .base = {
  2186. .cra_name = "echainiv(authenc(hmac(sha512),"
  2187. "cbc(des)))",
  2188. .cra_driver_name = "echainiv-authenc-"
  2189. "hmac-sha512-cbc-des-"
  2190. "caam-qi",
  2191. .cra_blocksize = DES_BLOCK_SIZE,
  2192. },
  2193. .setkey = aead_setkey,
  2194. .setauthsize = aead_setauthsize,
  2195. .encrypt = aead_encrypt,
  2196. .decrypt = aead_decrypt,
  2197. .ivsize = DES_BLOCK_SIZE,
  2198. .maxauthsize = SHA512_DIGEST_SIZE,
  2199. },
  2200. .caam = {
  2201. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  2202. .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
  2203. OP_ALG_AAI_HMAC_PRECOMP,
  2204. .geniv = true,
  2205. }
  2206. },
  2207. };
  2208. static int caam_init_common(struct caam_ctx *ctx, struct caam_alg_entry *caam,
  2209. bool uses_dkp)
  2210. {
  2211. struct caam_drv_private *priv;
  2212. struct device *dev;
  2213. /*
  2214. * distribute tfms across job rings to ensure in-order
  2215. * crypto request processing per tfm
  2216. */
  2217. ctx->jrdev = caam_jr_alloc();
  2218. if (IS_ERR(ctx->jrdev)) {
  2219. pr_err("Job Ring Device allocation for transform failed\n");
  2220. return PTR_ERR(ctx->jrdev);
  2221. }
  2222. dev = ctx->jrdev->parent;
  2223. priv = dev_get_drvdata(dev);
  2224. if (priv->era >= 6 && uses_dkp)
  2225. ctx->dir = DMA_BIDIRECTIONAL;
  2226. else
  2227. ctx->dir = DMA_TO_DEVICE;
  2228. ctx->key_dma = dma_map_single(dev, ctx->key, sizeof(ctx->key),
  2229. ctx->dir);
  2230. if (dma_mapping_error(dev, ctx->key_dma)) {
  2231. dev_err(dev, "unable to map key\n");
  2232. caam_jr_free(ctx->jrdev);
  2233. return -ENOMEM;
  2234. }
  2235. /* copy descriptor header template value */
  2236. ctx->cdata.algtype = OP_TYPE_CLASS1_ALG | caam->class1_alg_type;
  2237. ctx->adata.algtype = OP_TYPE_CLASS2_ALG | caam->class2_alg_type;
  2238. ctx->qidev = dev;
  2239. spin_lock_init(&ctx->lock);
  2240. ctx->drv_ctx[ENCRYPT] = NULL;
  2241. ctx->drv_ctx[DECRYPT] = NULL;
  2242. return 0;
  2243. }
  2244. static int caam_cra_init(struct crypto_skcipher *tfm)
  2245. {
  2246. struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
  2247. struct caam_skcipher_alg *caam_alg =
  2248. container_of(alg, typeof(*caam_alg), skcipher);
  2249. struct caam_ctx *ctx = crypto_skcipher_ctx(tfm);
  2250. u32 alg_aai = caam_alg->caam.class1_alg_type & OP_ALG_AAI_MASK;
  2251. int ret = 0;
  2252. if (alg_aai == OP_ALG_AAI_XTS) {
  2253. const char *tfm_name = crypto_tfm_alg_name(&tfm->base);
  2254. struct crypto_skcipher *fallback;
  2255. fallback = crypto_alloc_skcipher(tfm_name, 0,
  2256. CRYPTO_ALG_NEED_FALLBACK);
  2257. if (IS_ERR(fallback)) {
  2258. pr_err("Failed to allocate %s fallback: %ld\n",
  2259. tfm_name, PTR_ERR(fallback));
  2260. return PTR_ERR(fallback);
  2261. }
  2262. ctx->fallback = fallback;
  2263. crypto_skcipher_set_reqsize(tfm, sizeof(struct caam_skcipher_req_ctx) +
  2264. crypto_skcipher_reqsize(fallback));
  2265. }
  2266. ret = caam_init_common(ctx, &caam_alg->caam, false);
  2267. if (ret && ctx->fallback)
  2268. crypto_free_skcipher(ctx->fallback);
  2269. return ret;
  2270. }
  2271. static int caam_aead_init(struct crypto_aead *tfm)
  2272. {
  2273. struct aead_alg *alg = crypto_aead_alg(tfm);
  2274. struct caam_aead_alg *caam_alg = container_of(alg, typeof(*caam_alg),
  2275. aead);
  2276. struct caam_ctx *ctx = crypto_aead_ctx(tfm);
  2277. return caam_init_common(ctx, &caam_alg->caam, !caam_alg->caam.nodkp);
  2278. }
  2279. static void caam_exit_common(struct caam_ctx *ctx)
  2280. {
  2281. caam_drv_ctx_rel(ctx->drv_ctx[ENCRYPT]);
  2282. caam_drv_ctx_rel(ctx->drv_ctx[DECRYPT]);
  2283. dma_unmap_single(ctx->jrdev->parent, ctx->key_dma, sizeof(ctx->key),
  2284. ctx->dir);
  2285. caam_jr_free(ctx->jrdev);
  2286. }
  2287. static void caam_cra_exit(struct crypto_skcipher *tfm)
  2288. {
  2289. struct caam_ctx *ctx = crypto_skcipher_ctx(tfm);
  2290. if (ctx->fallback)
  2291. crypto_free_skcipher(ctx->fallback);
  2292. caam_exit_common(ctx);
  2293. }
  2294. static void caam_aead_exit(struct crypto_aead *tfm)
  2295. {
  2296. caam_exit_common(crypto_aead_ctx(tfm));
  2297. }
  2298. void caam_qi_algapi_exit(void)
  2299. {
  2300. int i;
  2301. for (i = 0; i < ARRAY_SIZE(driver_aeads); i++) {
  2302. struct caam_aead_alg *t_alg = driver_aeads + i;
  2303. if (t_alg->registered)
  2304. crypto_unregister_aead(&t_alg->aead);
  2305. }
  2306. for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
  2307. struct caam_skcipher_alg *t_alg = driver_algs + i;
  2308. if (t_alg->registered)
  2309. crypto_unregister_skcipher(&t_alg->skcipher);
  2310. }
  2311. }
  2312. static void caam_skcipher_alg_init(struct caam_skcipher_alg *t_alg)
  2313. {
  2314. struct skcipher_alg *alg = &t_alg->skcipher;
  2315. alg->base.cra_module = THIS_MODULE;
  2316. alg->base.cra_priority = CAAM_CRA_PRIORITY;
  2317. alg->base.cra_ctxsize = sizeof(struct caam_ctx);
  2318. alg->base.cra_flags |= (CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY |
  2319. CRYPTO_ALG_KERN_DRIVER_ONLY);
  2320. alg->init = caam_cra_init;
  2321. alg->exit = caam_cra_exit;
  2322. }
  2323. static void caam_aead_alg_init(struct caam_aead_alg *t_alg)
  2324. {
  2325. struct aead_alg *alg = &t_alg->aead;
  2326. alg->base.cra_module = THIS_MODULE;
  2327. alg->base.cra_priority = CAAM_CRA_PRIORITY;
  2328. alg->base.cra_ctxsize = sizeof(struct caam_ctx);
  2329. alg->base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY |
  2330. CRYPTO_ALG_KERN_DRIVER_ONLY;
  2331. alg->init = caam_aead_init;
  2332. alg->exit = caam_aead_exit;
  2333. }
  2334. int caam_qi_algapi_init(struct device *ctrldev)
  2335. {
  2336. struct caam_drv_private *priv = dev_get_drvdata(ctrldev);
  2337. int i = 0, err = 0;
  2338. u32 aes_vid, aes_inst, des_inst, md_vid, md_inst;
  2339. unsigned int md_limit = SHA512_DIGEST_SIZE;
  2340. bool registered = false;
  2341. /* Make sure this runs only on (DPAA 1.x) QI */
  2342. if (!priv->qi_present || caam_dpaa2)
  2343. return 0;
  2344. /*
  2345. * Register crypto algorithms the device supports.
  2346. * First, detect presence and attributes of DES, AES, and MD blocks.
  2347. */
  2348. if (priv->era < 10) {
  2349. u32 cha_vid, cha_inst;
  2350. cha_vid = rd_reg32(&priv->ctrl->perfmon.cha_id_ls);
  2351. aes_vid = cha_vid & CHA_ID_LS_AES_MASK;
  2352. md_vid = (cha_vid & CHA_ID_LS_MD_MASK) >> CHA_ID_LS_MD_SHIFT;
  2353. cha_inst = rd_reg32(&priv->ctrl->perfmon.cha_num_ls);
  2354. des_inst = (cha_inst & CHA_ID_LS_DES_MASK) >>
  2355. CHA_ID_LS_DES_SHIFT;
  2356. aes_inst = cha_inst & CHA_ID_LS_AES_MASK;
  2357. md_inst = (cha_inst & CHA_ID_LS_MD_MASK) >> CHA_ID_LS_MD_SHIFT;
  2358. } else {
  2359. u32 aesa, mdha;
  2360. aesa = rd_reg32(&priv->ctrl->vreg.aesa);
  2361. mdha = rd_reg32(&priv->ctrl->vreg.mdha);
  2362. aes_vid = (aesa & CHA_VER_VID_MASK) >> CHA_VER_VID_SHIFT;
  2363. md_vid = (mdha & CHA_VER_VID_MASK) >> CHA_VER_VID_SHIFT;
  2364. des_inst = rd_reg32(&priv->ctrl->vreg.desa) & CHA_VER_NUM_MASK;
  2365. aes_inst = aesa & CHA_VER_NUM_MASK;
  2366. md_inst = mdha & CHA_VER_NUM_MASK;
  2367. }
  2368. /* If MD is present, limit digest size based on LP256 */
  2369. if (md_inst && md_vid == CHA_VER_VID_MD_LP256)
  2370. md_limit = SHA256_DIGEST_SIZE;
  2371. for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
  2372. struct caam_skcipher_alg *t_alg = driver_algs + i;
  2373. u32 alg_sel = t_alg->caam.class1_alg_type & OP_ALG_ALGSEL_MASK;
  2374. /* Skip DES algorithms if not supported by device */
  2375. if (!des_inst &&
  2376. ((alg_sel == OP_ALG_ALGSEL_3DES) ||
  2377. (alg_sel == OP_ALG_ALGSEL_DES)))
  2378. continue;
  2379. /* Skip AES algorithms if not supported by device */
  2380. if (!aes_inst && (alg_sel == OP_ALG_ALGSEL_AES))
  2381. continue;
  2382. caam_skcipher_alg_init(t_alg);
  2383. err = crypto_register_skcipher(&t_alg->skcipher);
  2384. if (err) {
  2385. dev_warn(ctrldev, "%s alg registration failed\n",
  2386. t_alg->skcipher.base.cra_driver_name);
  2387. continue;
  2388. }
  2389. t_alg->registered = true;
  2390. registered = true;
  2391. }
  2392. for (i = 0; i < ARRAY_SIZE(driver_aeads); i++) {
  2393. struct caam_aead_alg *t_alg = driver_aeads + i;
  2394. u32 c1_alg_sel = t_alg->caam.class1_alg_type &
  2395. OP_ALG_ALGSEL_MASK;
  2396. u32 c2_alg_sel = t_alg->caam.class2_alg_type &
  2397. OP_ALG_ALGSEL_MASK;
  2398. u32 alg_aai = t_alg->caam.class1_alg_type & OP_ALG_AAI_MASK;
  2399. /* Skip DES algorithms if not supported by device */
  2400. if (!des_inst &&
  2401. ((c1_alg_sel == OP_ALG_ALGSEL_3DES) ||
  2402. (c1_alg_sel == OP_ALG_ALGSEL_DES)))
  2403. continue;
  2404. /* Skip AES algorithms if not supported by device */
  2405. if (!aes_inst && (c1_alg_sel == OP_ALG_ALGSEL_AES))
  2406. continue;
  2407. /*
  2408. * Check support for AES algorithms not available
  2409. * on LP devices.
  2410. */
  2411. if (aes_vid == CHA_VER_VID_AES_LP && alg_aai == OP_ALG_AAI_GCM)
  2412. continue;
  2413. /*
  2414. * Skip algorithms requiring message digests
  2415. * if MD or MD size is not supported by device.
  2416. */
  2417. if (c2_alg_sel &&
  2418. (!md_inst || (t_alg->aead.maxauthsize > md_limit)))
  2419. continue;
  2420. caam_aead_alg_init(t_alg);
  2421. err = crypto_register_aead(&t_alg->aead);
  2422. if (err) {
  2423. pr_warn("%s alg registration failed\n",
  2424. t_alg->aead.base.cra_driver_name);
  2425. continue;
  2426. }
  2427. t_alg->registered = true;
  2428. registered = true;
  2429. }
  2430. if (registered)
  2431. dev_info(ctrldev, "algorithms registered in /proc/crypto\n");
  2432. return err;
  2433. }