caamhash.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * caam - Freescale FSL CAAM support for ahash functions of crypto API
  4. *
  5. * Copyright 2011 Freescale Semiconductor, Inc.
  6. * Copyright 2018-2019, 2023 NXP
  7. *
  8. * Based on caamalg.c crypto API driver.
  9. *
  10. * relationship of digest job descriptor or first job descriptor after init to
  11. * shared descriptors:
  12. *
  13. * --------------- ---------------
  14. * | JobDesc #1 |-------------------->| ShareDesc |
  15. * | *(packet 1) | | (hashKey) |
  16. * --------------- | (operation) |
  17. * ---------------
  18. *
  19. * relationship of subsequent job descriptors to shared descriptors:
  20. *
  21. * --------------- ---------------
  22. * | JobDesc #2 |-------------------->| ShareDesc |
  23. * | *(packet 2) | |------------->| (hashKey) |
  24. * --------------- | |-------->| (operation) |
  25. * . | | | (load ctx2) |
  26. * . | | ---------------
  27. * --------------- | |
  28. * | JobDesc #3 |------| |
  29. * | *(packet 3) | |
  30. * --------------- |
  31. * . |
  32. * . |
  33. * --------------- |
  34. * | JobDesc #4 |------------
  35. * | *(packet 4) |
  36. * ---------------
  37. *
  38. * The SharedDesc never changes for a connection unless rekeyed, but
  39. * each packet will likely be in a different place. So all we need
  40. * to know to process the packet is where the input is, where the
  41. * output goes, and what context we want to process with. Context is
  42. * in the SharedDesc, packet references in the JobDesc.
  43. *
  44. * So, a job desc looks like:
  45. *
  46. * ---------------------
  47. * | Header |
  48. * | ShareDesc Pointer |
  49. * | SEQ_OUT_PTR |
  50. * | (output buffer) |
  51. * | (output length) |
  52. * | SEQ_IN_PTR |
  53. * | (input buffer) |
  54. * | (input length) |
  55. * ---------------------
  56. */
  57. #include "compat.h"
  58. #include "regs.h"
  59. #include "intern.h"
  60. #include "desc_constr.h"
  61. #include "jr.h"
  62. #include "error.h"
  63. #include "sg_sw_sec4.h"
  64. #include "key_gen.h"
  65. #include "caamhash_desc.h"
  66. #include <crypto/internal/engine.h>
  67. #include <crypto/internal/hash.h>
  68. #include <linux/dma-mapping.h>
  69. #include <linux/err.h>
  70. #include <linux/kernel.h>
  71. #include <linux/slab.h>
  72. #include <linux/string.h>
  73. #define CAAM_CRA_PRIORITY 3000
  74. /* max hash key is max split key size */
  75. #define CAAM_MAX_HASH_KEY_SIZE (SHA512_DIGEST_SIZE * 2)
  76. #define CAAM_MAX_HASH_BLOCK_SIZE SHA512_BLOCK_SIZE
  77. #define CAAM_MAX_HASH_DIGEST_SIZE SHA512_DIGEST_SIZE
  78. #define DESC_HASH_MAX_USED_BYTES (DESC_AHASH_FINAL_LEN + \
  79. CAAM_MAX_HASH_KEY_SIZE)
  80. #define DESC_HASH_MAX_USED_LEN (DESC_HASH_MAX_USED_BYTES / CAAM_CMD_SZ)
  81. /* caam context sizes for hashes: running digest + 8 */
  82. #define HASH_MSG_LEN 8
  83. #define MAX_CTX_LEN (HASH_MSG_LEN + SHA512_DIGEST_SIZE)
  84. static struct list_head hash_list;
  85. /* ahash per-session context */
  86. struct caam_hash_ctx {
  87. u32 sh_desc_update[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
  88. u32 sh_desc_update_first[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
  89. u32 sh_desc_fin[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
  90. u32 sh_desc_digest[DESC_HASH_MAX_USED_LEN] ____cacheline_aligned;
  91. u8 key[CAAM_MAX_HASH_KEY_SIZE] ____cacheline_aligned;
  92. dma_addr_t sh_desc_update_dma ____cacheline_aligned;
  93. dma_addr_t sh_desc_update_first_dma;
  94. dma_addr_t sh_desc_fin_dma;
  95. dma_addr_t sh_desc_digest_dma;
  96. enum dma_data_direction dir;
  97. enum dma_data_direction key_dir;
  98. struct device *jrdev;
  99. int ctx_len;
  100. struct alginfo adata;
  101. };
  102. /* ahash state */
  103. struct caam_hash_state {
  104. dma_addr_t buf_dma;
  105. dma_addr_t ctx_dma;
  106. int ctx_dma_len;
  107. u8 buf[CAAM_MAX_HASH_BLOCK_SIZE] ____cacheline_aligned;
  108. int buflen;
  109. int next_buflen;
  110. u8 caam_ctx[MAX_CTX_LEN] ____cacheline_aligned;
  111. int (*update)(struct ahash_request *req) ____cacheline_aligned;
  112. int (*final)(struct ahash_request *req);
  113. int (*finup)(struct ahash_request *req);
  114. struct ahash_edesc *edesc;
  115. void (*ahash_op_done)(struct device *jrdev, u32 *desc, u32 err,
  116. void *context);
  117. };
  118. struct caam_export_state {
  119. u8 buf[CAAM_MAX_HASH_BLOCK_SIZE];
  120. u8 caam_ctx[MAX_CTX_LEN];
  121. int buflen;
  122. int (*update)(struct ahash_request *req);
  123. int (*final)(struct ahash_request *req);
  124. int (*finup)(struct ahash_request *req);
  125. };
  126. static inline bool is_cmac_aes(u32 algtype)
  127. {
  128. return (algtype & (OP_ALG_ALGSEL_MASK | OP_ALG_AAI_MASK)) ==
  129. (OP_ALG_ALGSEL_AES | OP_ALG_AAI_CMAC);
  130. }
  131. /* Common job descriptor seq in/out ptr routines */
  132. /* Map state->caam_ctx, and append seq_out_ptr command that points to it */
  133. static inline int map_seq_out_ptr_ctx(u32 *desc, struct device *jrdev,
  134. struct caam_hash_state *state,
  135. int ctx_len)
  136. {
  137. state->ctx_dma_len = ctx_len;
  138. state->ctx_dma = dma_map_single(jrdev, state->caam_ctx,
  139. ctx_len, DMA_FROM_DEVICE);
  140. if (dma_mapping_error(jrdev, state->ctx_dma)) {
  141. dev_err(jrdev, "unable to map ctx\n");
  142. state->ctx_dma = 0;
  143. return -ENOMEM;
  144. }
  145. append_seq_out_ptr(desc, state->ctx_dma, ctx_len, 0);
  146. return 0;
  147. }
  148. /* Map current buffer in state (if length > 0) and put it in link table */
  149. static inline int buf_map_to_sec4_sg(struct device *jrdev,
  150. struct sec4_sg_entry *sec4_sg,
  151. struct caam_hash_state *state)
  152. {
  153. int buflen = state->buflen;
  154. if (!buflen)
  155. return 0;
  156. state->buf_dma = dma_map_single(jrdev, state->buf, buflen,
  157. DMA_TO_DEVICE);
  158. if (dma_mapping_error(jrdev, state->buf_dma)) {
  159. dev_err(jrdev, "unable to map buf\n");
  160. state->buf_dma = 0;
  161. return -ENOMEM;
  162. }
  163. dma_to_sec4_sg_one(sec4_sg, state->buf_dma, buflen, 0);
  164. return 0;
  165. }
  166. /* Map state->caam_ctx, and add it to link table */
  167. static inline int ctx_map_to_sec4_sg(struct device *jrdev,
  168. struct caam_hash_state *state, int ctx_len,
  169. struct sec4_sg_entry *sec4_sg, u32 flag)
  170. {
  171. state->ctx_dma_len = ctx_len;
  172. state->ctx_dma = dma_map_single(jrdev, state->caam_ctx, ctx_len, flag);
  173. if (dma_mapping_error(jrdev, state->ctx_dma)) {
  174. dev_err(jrdev, "unable to map ctx\n");
  175. state->ctx_dma = 0;
  176. return -ENOMEM;
  177. }
  178. dma_to_sec4_sg_one(sec4_sg, state->ctx_dma, ctx_len, 0);
  179. return 0;
  180. }
  181. static int ahash_set_sh_desc(struct crypto_ahash *ahash)
  182. {
  183. struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash);
  184. int digestsize = crypto_ahash_digestsize(ahash);
  185. struct device *jrdev = ctx->jrdev;
  186. struct caam_drv_private *ctrlpriv = dev_get_drvdata(jrdev->parent);
  187. u32 *desc;
  188. ctx->adata.key_virt = ctx->key;
  189. /* ahash_update shared descriptor */
  190. desc = ctx->sh_desc_update;
  191. cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_UPDATE, ctx->ctx_len,
  192. ctx->ctx_len, true, ctrlpriv->era);
  193. dma_sync_single_for_device(jrdev, ctx->sh_desc_update_dma,
  194. desc_bytes(desc), ctx->dir);
  195. print_hex_dump_debug("ahash update shdesc@"__stringify(__LINE__)": ",
  196. DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
  197. 1);
  198. /* ahash_update_first shared descriptor */
  199. desc = ctx->sh_desc_update_first;
  200. cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_INIT, ctx->ctx_len,
  201. ctx->ctx_len, false, ctrlpriv->era);
  202. dma_sync_single_for_device(jrdev, ctx->sh_desc_update_first_dma,
  203. desc_bytes(desc), ctx->dir);
  204. print_hex_dump_debug("ahash update first shdesc@"__stringify(__LINE__)
  205. ": ", DUMP_PREFIX_ADDRESS, 16, 4, desc,
  206. desc_bytes(desc), 1);
  207. /* ahash_final shared descriptor */
  208. desc = ctx->sh_desc_fin;
  209. cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_FINALIZE, digestsize,
  210. ctx->ctx_len, true, ctrlpriv->era);
  211. dma_sync_single_for_device(jrdev, ctx->sh_desc_fin_dma,
  212. desc_bytes(desc), ctx->dir);
  213. print_hex_dump_debug("ahash final shdesc@"__stringify(__LINE__)": ",
  214. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  215. desc_bytes(desc), 1);
  216. /* ahash_digest shared descriptor */
  217. desc = ctx->sh_desc_digest;
  218. cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_INITFINAL, digestsize,
  219. ctx->ctx_len, false, ctrlpriv->era);
  220. dma_sync_single_for_device(jrdev, ctx->sh_desc_digest_dma,
  221. desc_bytes(desc), ctx->dir);
  222. print_hex_dump_debug("ahash digest shdesc@"__stringify(__LINE__)": ",
  223. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  224. desc_bytes(desc), 1);
  225. return 0;
  226. }
  227. static int axcbc_set_sh_desc(struct crypto_ahash *ahash)
  228. {
  229. struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash);
  230. int digestsize = crypto_ahash_digestsize(ahash);
  231. struct device *jrdev = ctx->jrdev;
  232. u32 *desc;
  233. /* shared descriptor for ahash_update */
  234. desc = ctx->sh_desc_update;
  235. cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_UPDATE,
  236. ctx->ctx_len, ctx->ctx_len);
  237. dma_sync_single_for_device(jrdev, ctx->sh_desc_update_dma,
  238. desc_bytes(desc), ctx->dir);
  239. print_hex_dump_debug("axcbc update shdesc@" __stringify(__LINE__)" : ",
  240. DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
  241. 1);
  242. /* shared descriptor for ahash_{final,finup} */
  243. desc = ctx->sh_desc_fin;
  244. cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_FINALIZE,
  245. digestsize, ctx->ctx_len);
  246. dma_sync_single_for_device(jrdev, ctx->sh_desc_fin_dma,
  247. desc_bytes(desc), ctx->dir);
  248. print_hex_dump_debug("axcbc finup shdesc@" __stringify(__LINE__)" : ",
  249. DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
  250. 1);
  251. /* key is immediate data for INIT and INITFINAL states */
  252. ctx->adata.key_virt = ctx->key;
  253. /* shared descriptor for first invocation of ahash_update */
  254. desc = ctx->sh_desc_update_first;
  255. cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_INIT, ctx->ctx_len,
  256. ctx->ctx_len);
  257. dma_sync_single_for_device(jrdev, ctx->sh_desc_update_first_dma,
  258. desc_bytes(desc), ctx->dir);
  259. print_hex_dump_debug("axcbc update first shdesc@" __stringify(__LINE__)
  260. " : ", DUMP_PREFIX_ADDRESS, 16, 4, desc,
  261. desc_bytes(desc), 1);
  262. /* shared descriptor for ahash_digest */
  263. desc = ctx->sh_desc_digest;
  264. cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_INITFINAL,
  265. digestsize, ctx->ctx_len);
  266. dma_sync_single_for_device(jrdev, ctx->sh_desc_digest_dma,
  267. desc_bytes(desc), ctx->dir);
  268. print_hex_dump_debug("axcbc digest shdesc@" __stringify(__LINE__)" : ",
  269. DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
  270. 1);
  271. return 0;
  272. }
  273. static int acmac_set_sh_desc(struct crypto_ahash *ahash)
  274. {
  275. struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash);
  276. int digestsize = crypto_ahash_digestsize(ahash);
  277. struct device *jrdev = ctx->jrdev;
  278. u32 *desc;
  279. /* shared descriptor for ahash_update */
  280. desc = ctx->sh_desc_update;
  281. cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_UPDATE,
  282. ctx->ctx_len, ctx->ctx_len);
  283. dma_sync_single_for_device(jrdev, ctx->sh_desc_update_dma,
  284. desc_bytes(desc), ctx->dir);
  285. print_hex_dump_debug("acmac update shdesc@" __stringify(__LINE__)" : ",
  286. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  287. desc_bytes(desc), 1);
  288. /* shared descriptor for ahash_{final,finup} */
  289. desc = ctx->sh_desc_fin;
  290. cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_FINALIZE,
  291. digestsize, ctx->ctx_len);
  292. dma_sync_single_for_device(jrdev, ctx->sh_desc_fin_dma,
  293. desc_bytes(desc), ctx->dir);
  294. print_hex_dump_debug("acmac finup shdesc@" __stringify(__LINE__)" : ",
  295. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  296. desc_bytes(desc), 1);
  297. /* shared descriptor for first invocation of ahash_update */
  298. desc = ctx->sh_desc_update_first;
  299. cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_INIT, ctx->ctx_len,
  300. ctx->ctx_len);
  301. dma_sync_single_for_device(jrdev, ctx->sh_desc_update_first_dma,
  302. desc_bytes(desc), ctx->dir);
  303. print_hex_dump_debug("acmac update first shdesc@" __stringify(__LINE__)
  304. " : ", DUMP_PREFIX_ADDRESS, 16, 4, desc,
  305. desc_bytes(desc), 1);
  306. /* shared descriptor for ahash_digest */
  307. desc = ctx->sh_desc_digest;
  308. cnstr_shdsc_sk_hash(desc, &ctx->adata, OP_ALG_AS_INITFINAL,
  309. digestsize, ctx->ctx_len);
  310. dma_sync_single_for_device(jrdev, ctx->sh_desc_digest_dma,
  311. desc_bytes(desc), ctx->dir);
  312. print_hex_dump_debug("acmac digest shdesc@" __stringify(__LINE__)" : ",
  313. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  314. desc_bytes(desc), 1);
  315. return 0;
  316. }
  317. /* Digest hash size if it is too large */
  318. static int hash_digest_key(struct caam_hash_ctx *ctx, u32 *keylen, u8 *key,
  319. u32 digestsize)
  320. {
  321. struct device *jrdev = ctx->jrdev;
  322. u32 *desc;
  323. struct split_key_result result;
  324. dma_addr_t key_dma;
  325. int ret;
  326. desc = kmalloc(CAAM_CMD_SZ * 8 + CAAM_PTR_SZ * 2, GFP_KERNEL);
  327. if (!desc)
  328. return -ENOMEM;
  329. init_job_desc(desc, 0);
  330. key_dma = dma_map_single(jrdev, key, *keylen, DMA_BIDIRECTIONAL);
  331. if (dma_mapping_error(jrdev, key_dma)) {
  332. dev_err(jrdev, "unable to map key memory\n");
  333. kfree(desc);
  334. return -ENOMEM;
  335. }
  336. /* Job descriptor to perform unkeyed hash on key_in */
  337. append_operation(desc, ctx->adata.algtype | OP_ALG_ENCRYPT |
  338. OP_ALG_AS_INITFINAL);
  339. append_seq_in_ptr(desc, key_dma, *keylen, 0);
  340. append_seq_fifo_load(desc, *keylen, FIFOLD_CLASS_CLASS2 |
  341. FIFOLD_TYPE_LAST2 | FIFOLD_TYPE_MSG);
  342. append_seq_out_ptr(desc, key_dma, digestsize, 0);
  343. append_seq_store(desc, digestsize, LDST_CLASS_2_CCB |
  344. LDST_SRCDST_BYTE_CONTEXT);
  345. print_hex_dump_debug("key_in@"__stringify(__LINE__)": ",
  346. DUMP_PREFIX_ADDRESS, 16, 4, key, *keylen, 1);
  347. print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
  348. DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
  349. 1);
  350. result.err = 0;
  351. init_completion(&result.completion);
  352. ret = caam_jr_enqueue(jrdev, desc, split_key_done, &result);
  353. if (ret == -EINPROGRESS) {
  354. /* in progress */
  355. wait_for_completion(&result.completion);
  356. ret = result.err;
  357. print_hex_dump_debug("digested key@"__stringify(__LINE__)": ",
  358. DUMP_PREFIX_ADDRESS, 16, 4, key,
  359. digestsize, 1);
  360. }
  361. dma_unmap_single(jrdev, key_dma, *keylen, DMA_BIDIRECTIONAL);
  362. *keylen = digestsize;
  363. kfree(desc);
  364. return ret;
  365. }
  366. static int ahash_setkey(struct crypto_ahash *ahash,
  367. const u8 *key, unsigned int keylen)
  368. {
  369. struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash);
  370. struct device *jrdev = ctx->jrdev;
  371. int blocksize = crypto_tfm_alg_blocksize(&ahash->base);
  372. int digestsize = crypto_ahash_digestsize(ahash);
  373. struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctx->jrdev->parent);
  374. int ret;
  375. u8 *hashed_key = NULL;
  376. dev_dbg(jrdev, "keylen %d\n", keylen);
  377. if (keylen > blocksize) {
  378. unsigned int aligned_len =
  379. ALIGN(keylen, dma_get_cache_alignment());
  380. if (aligned_len < keylen)
  381. return -EOVERFLOW;
  382. hashed_key = kmalloc(aligned_len, GFP_KERNEL);
  383. if (!hashed_key)
  384. return -ENOMEM;
  385. memcpy(hashed_key, key, keylen);
  386. ret = hash_digest_key(ctx, &keylen, hashed_key, digestsize);
  387. if (ret)
  388. goto bad_free_key;
  389. key = hashed_key;
  390. }
  391. /*
  392. * If DKP is supported, use it in the shared descriptor to generate
  393. * the split key.
  394. */
  395. if (ctrlpriv->era >= 6) {
  396. ctx->adata.key_inline = true;
  397. ctx->adata.keylen = keylen;
  398. ctx->adata.keylen_pad = split_key_len(ctx->adata.algtype &
  399. OP_ALG_ALGSEL_MASK);
  400. if (ctx->adata.keylen_pad > CAAM_MAX_HASH_KEY_SIZE)
  401. goto bad_free_key;
  402. memcpy(ctx->key, key, keylen);
  403. /*
  404. * In case |user key| > |derived key|, using DKP<imm,imm>
  405. * would result in invalid opcodes (last bytes of user key) in
  406. * the resulting descriptor. Use DKP<ptr,imm> instead => both
  407. * virtual and dma key addresses are needed.
  408. */
  409. if (keylen > ctx->adata.keylen_pad)
  410. dma_sync_single_for_device(ctx->jrdev,
  411. ctx->adata.key_dma,
  412. ctx->adata.keylen_pad,
  413. DMA_TO_DEVICE);
  414. } else {
  415. ret = gen_split_key(ctx->jrdev, ctx->key, &ctx->adata, key,
  416. keylen, CAAM_MAX_HASH_KEY_SIZE);
  417. if (ret)
  418. goto bad_free_key;
  419. }
  420. kfree(hashed_key);
  421. return ahash_set_sh_desc(ahash);
  422. bad_free_key:
  423. kfree(hashed_key);
  424. return -EINVAL;
  425. }
  426. static int axcbc_setkey(struct crypto_ahash *ahash, const u8 *key,
  427. unsigned int keylen)
  428. {
  429. struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash);
  430. struct device *jrdev = ctx->jrdev;
  431. if (keylen != AES_KEYSIZE_128)
  432. return -EINVAL;
  433. memcpy(ctx->key, key, keylen);
  434. dma_sync_single_for_device(jrdev, ctx->adata.key_dma, keylen,
  435. DMA_TO_DEVICE);
  436. ctx->adata.keylen = keylen;
  437. print_hex_dump_debug("axcbc ctx.key@" __stringify(__LINE__)" : ",
  438. DUMP_PREFIX_ADDRESS, 16, 4, ctx->key, keylen, 1);
  439. return axcbc_set_sh_desc(ahash);
  440. }
  441. static int acmac_setkey(struct crypto_ahash *ahash, const u8 *key,
  442. unsigned int keylen)
  443. {
  444. struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash);
  445. int err;
  446. err = aes_check_keylen(keylen);
  447. if (err)
  448. return err;
  449. /* key is immediate data for all cmac shared descriptors */
  450. ctx->adata.key_virt = key;
  451. ctx->adata.keylen = keylen;
  452. print_hex_dump_debug("acmac ctx.key@" __stringify(__LINE__)" : ",
  453. DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
  454. return acmac_set_sh_desc(ahash);
  455. }
  456. /*
  457. * ahash_edesc - s/w-extended ahash descriptor
  458. * @sec4_sg_dma: physical mapped address of h/w link table
  459. * @src_nents: number of segments in input scatterlist
  460. * @sec4_sg_bytes: length of dma mapped sec4_sg space
  461. * @bklog: stored to determine if the request needs backlog
  462. * @hw_desc: the h/w job descriptor followed by any referenced link tables
  463. * @sec4_sg: h/w link table
  464. */
  465. struct ahash_edesc {
  466. dma_addr_t sec4_sg_dma;
  467. int src_nents;
  468. int sec4_sg_bytes;
  469. bool bklog;
  470. u32 hw_desc[DESC_JOB_IO_LEN_MAX / sizeof(u32)] ____cacheline_aligned;
  471. struct sec4_sg_entry sec4_sg[];
  472. };
  473. static inline void ahash_unmap(struct device *dev,
  474. struct ahash_edesc *edesc,
  475. struct ahash_request *req, int dst_len)
  476. {
  477. struct caam_hash_state *state = ahash_request_ctx_dma(req);
  478. if (edesc->src_nents)
  479. dma_unmap_sg(dev, req->src, edesc->src_nents, DMA_TO_DEVICE);
  480. if (edesc->sec4_sg_bytes)
  481. dma_unmap_single(dev, edesc->sec4_sg_dma,
  482. edesc->sec4_sg_bytes, DMA_TO_DEVICE);
  483. if (state->buf_dma) {
  484. dma_unmap_single(dev, state->buf_dma, state->buflen,
  485. DMA_TO_DEVICE);
  486. state->buf_dma = 0;
  487. }
  488. }
  489. static inline void ahash_unmap_ctx(struct device *dev,
  490. struct ahash_edesc *edesc,
  491. struct ahash_request *req, int dst_len, u32 flag)
  492. {
  493. struct caam_hash_state *state = ahash_request_ctx_dma(req);
  494. if (state->ctx_dma) {
  495. dma_unmap_single(dev, state->ctx_dma, state->ctx_dma_len, flag);
  496. state->ctx_dma = 0;
  497. }
  498. ahash_unmap(dev, edesc, req, dst_len);
  499. }
  500. static inline void ahash_done_cpy(struct device *jrdev, u32 *desc, u32 err,
  501. void *context, enum dma_data_direction dir)
  502. {
  503. struct ahash_request *req = context;
  504. struct caam_drv_private_jr *jrp = dev_get_drvdata(jrdev);
  505. struct ahash_edesc *edesc;
  506. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  507. int digestsize = crypto_ahash_digestsize(ahash);
  508. struct caam_hash_state *state = ahash_request_ctx_dma(req);
  509. struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash);
  510. int ecode = 0;
  511. bool has_bklog;
  512. dev_dbg(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
  513. edesc = state->edesc;
  514. has_bklog = edesc->bklog;
  515. if (err)
  516. ecode = caam_jr_strstatus(jrdev, err);
  517. ahash_unmap_ctx(jrdev, edesc, req, digestsize, dir);
  518. memcpy(req->result, state->caam_ctx, digestsize);
  519. kfree(edesc);
  520. print_hex_dump_debug("ctx@"__stringify(__LINE__)": ",
  521. DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
  522. ctx->ctx_len, 1);
  523. /*
  524. * If no backlog flag, the completion of the request is done
  525. * by CAAM, not crypto engine.
  526. */
  527. if (!has_bklog)
  528. ahash_request_complete(req, ecode);
  529. else
  530. crypto_finalize_hash_request(jrp->engine, req, ecode);
  531. }
  532. static void ahash_done(struct device *jrdev, u32 *desc, u32 err,
  533. void *context)
  534. {
  535. ahash_done_cpy(jrdev, desc, err, context, DMA_FROM_DEVICE);
  536. }
  537. static void ahash_done_ctx_src(struct device *jrdev, u32 *desc, u32 err,
  538. void *context)
  539. {
  540. ahash_done_cpy(jrdev, desc, err, context, DMA_BIDIRECTIONAL);
  541. }
  542. static inline void ahash_done_switch(struct device *jrdev, u32 *desc, u32 err,
  543. void *context, enum dma_data_direction dir)
  544. {
  545. struct ahash_request *req = context;
  546. struct caam_drv_private_jr *jrp = dev_get_drvdata(jrdev);
  547. struct ahash_edesc *edesc;
  548. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  549. struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash);
  550. struct caam_hash_state *state = ahash_request_ctx_dma(req);
  551. int digestsize = crypto_ahash_digestsize(ahash);
  552. int ecode = 0;
  553. bool has_bklog;
  554. dev_dbg(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
  555. edesc = state->edesc;
  556. has_bklog = edesc->bklog;
  557. if (err)
  558. ecode = caam_jr_strstatus(jrdev, err);
  559. ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, dir);
  560. kfree(edesc);
  561. scatterwalk_map_and_copy(state->buf, req->src,
  562. req->nbytes - state->next_buflen,
  563. state->next_buflen, 0);
  564. state->buflen = state->next_buflen;
  565. print_hex_dump_debug("buf@" __stringify(__LINE__)": ",
  566. DUMP_PREFIX_ADDRESS, 16, 4, state->buf,
  567. state->buflen, 1);
  568. print_hex_dump_debug("ctx@"__stringify(__LINE__)": ",
  569. DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
  570. ctx->ctx_len, 1);
  571. if (req->result)
  572. print_hex_dump_debug("result@"__stringify(__LINE__)": ",
  573. DUMP_PREFIX_ADDRESS, 16, 4, req->result,
  574. digestsize, 1);
  575. /*
  576. * If no backlog flag, the completion of the request is done
  577. * by CAAM, not crypto engine.
  578. */
  579. if (!has_bklog)
  580. ahash_request_complete(req, ecode);
  581. else
  582. crypto_finalize_hash_request(jrp->engine, req, ecode);
  583. }
  584. static void ahash_done_bi(struct device *jrdev, u32 *desc, u32 err,
  585. void *context)
  586. {
  587. ahash_done_switch(jrdev, desc, err, context, DMA_BIDIRECTIONAL);
  588. }
  589. static void ahash_done_ctx_dst(struct device *jrdev, u32 *desc, u32 err,
  590. void *context)
  591. {
  592. ahash_done_switch(jrdev, desc, err, context, DMA_FROM_DEVICE);
  593. }
  594. /*
  595. * Allocate an enhanced descriptor, which contains the hardware descriptor
  596. * and space for hardware scatter table containing sg_num entries.
  597. */
  598. static struct ahash_edesc *ahash_edesc_alloc(struct ahash_request *req,
  599. int sg_num, u32 *sh_desc,
  600. dma_addr_t sh_desc_dma)
  601. {
  602. struct caam_hash_state *state = ahash_request_ctx_dma(req);
  603. gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
  604. GFP_KERNEL : GFP_ATOMIC;
  605. struct ahash_edesc *edesc;
  606. sg_num = pad_sg_nents(sg_num);
  607. edesc = kzalloc_flex(*edesc, sec4_sg, sg_num, flags);
  608. if (!edesc)
  609. return NULL;
  610. state->edesc = edesc;
  611. init_job_desc_shared(edesc->hw_desc, sh_desc_dma, desc_len(sh_desc),
  612. HDR_SHARE_DEFER | HDR_REVERSE);
  613. return edesc;
  614. }
  615. static int ahash_edesc_add_src(struct caam_hash_ctx *ctx,
  616. struct ahash_edesc *edesc,
  617. struct ahash_request *req, int nents,
  618. unsigned int first_sg,
  619. unsigned int first_bytes, size_t to_hash)
  620. {
  621. dma_addr_t src_dma;
  622. u32 options;
  623. if (nents > 1 || first_sg) {
  624. struct sec4_sg_entry *sg = edesc->sec4_sg;
  625. unsigned int sgsize = sizeof(*sg) *
  626. pad_sg_nents(first_sg + nents);
  627. sg_to_sec4_sg_last(req->src, to_hash, sg + first_sg, 0);
  628. src_dma = dma_map_single(ctx->jrdev, sg, sgsize, DMA_TO_DEVICE);
  629. if (dma_mapping_error(ctx->jrdev, src_dma)) {
  630. dev_err(ctx->jrdev, "unable to map S/G table\n");
  631. return -ENOMEM;
  632. }
  633. edesc->sec4_sg_bytes = sgsize;
  634. edesc->sec4_sg_dma = src_dma;
  635. options = LDST_SGF;
  636. } else {
  637. src_dma = sg_dma_address(req->src);
  638. options = 0;
  639. }
  640. append_seq_in_ptr(edesc->hw_desc, src_dma, first_bytes + to_hash,
  641. options);
  642. return 0;
  643. }
  644. static int ahash_do_one_req(struct crypto_engine *engine, void *areq)
  645. {
  646. struct ahash_request *req = ahash_request_cast(areq);
  647. struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(crypto_ahash_reqtfm(req));
  648. struct caam_hash_state *state = ahash_request_ctx_dma(req);
  649. struct device *jrdev = ctx->jrdev;
  650. u32 *desc = state->edesc->hw_desc;
  651. int ret;
  652. state->edesc->bklog = true;
  653. ret = caam_jr_enqueue(jrdev, desc, state->ahash_op_done, req);
  654. if (ret == -ENOSPC && engine->retry_support)
  655. return ret;
  656. if (ret != -EINPROGRESS) {
  657. ahash_unmap(jrdev, state->edesc, req, 0);
  658. kfree(state->edesc);
  659. } else {
  660. ret = 0;
  661. }
  662. return ret;
  663. }
  664. static int ahash_enqueue_req(struct device *jrdev,
  665. void (*cbk)(struct device *jrdev, u32 *desc,
  666. u32 err, void *context),
  667. struct ahash_request *req,
  668. int dst_len, enum dma_data_direction dir)
  669. {
  670. struct caam_drv_private_jr *jrpriv = dev_get_drvdata(jrdev);
  671. struct caam_hash_state *state = ahash_request_ctx_dma(req);
  672. struct ahash_edesc *edesc = state->edesc;
  673. u32 *desc = edesc->hw_desc;
  674. int ret;
  675. state->ahash_op_done = cbk;
  676. /*
  677. * Only the backlog request are sent to crypto-engine since the others
  678. * can be handled by CAAM, if free, especially since JR has up to 1024
  679. * entries (more than the 10 entries from crypto-engine).
  680. */
  681. if (req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)
  682. ret = crypto_transfer_hash_request_to_engine(jrpriv->engine,
  683. req);
  684. else
  685. ret = caam_jr_enqueue(jrdev, desc, cbk, req);
  686. if ((ret != -EINPROGRESS) && (ret != -EBUSY)) {
  687. ahash_unmap_ctx(jrdev, edesc, req, dst_len, dir);
  688. kfree(edesc);
  689. }
  690. return ret;
  691. }
  692. /* submit update job descriptor */
  693. static int ahash_update_ctx(struct ahash_request *req)
  694. {
  695. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  696. struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash);
  697. struct caam_hash_state *state = ahash_request_ctx_dma(req);
  698. struct device *jrdev = ctx->jrdev;
  699. u8 *buf = state->buf;
  700. int *buflen = &state->buflen;
  701. int *next_buflen = &state->next_buflen;
  702. int blocksize = crypto_ahash_blocksize(ahash);
  703. int in_len = *buflen + req->nbytes, to_hash;
  704. u32 *desc;
  705. int src_nents, mapped_nents, sec4_sg_bytes, sec4_sg_src_index;
  706. struct ahash_edesc *edesc;
  707. int ret = 0;
  708. *next_buflen = in_len & (blocksize - 1);
  709. to_hash = in_len - *next_buflen;
  710. /*
  711. * For XCBC and CMAC, if to_hash is multiple of block size,
  712. * keep last block in internal buffer
  713. */
  714. if ((is_xcbc_aes(ctx->adata.algtype) ||
  715. is_cmac_aes(ctx->adata.algtype)) && to_hash >= blocksize &&
  716. (*next_buflen == 0)) {
  717. *next_buflen = blocksize;
  718. to_hash -= blocksize;
  719. }
  720. if (to_hash) {
  721. int pad_nents;
  722. int src_len = req->nbytes - *next_buflen;
  723. src_nents = sg_nents_for_len(req->src, src_len);
  724. if (src_nents < 0) {
  725. dev_err(jrdev, "Invalid number of src SG.\n");
  726. return src_nents;
  727. }
  728. if (src_nents) {
  729. mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
  730. DMA_TO_DEVICE);
  731. if (!mapped_nents) {
  732. dev_err(jrdev, "unable to DMA map source\n");
  733. return -ENOMEM;
  734. }
  735. } else {
  736. mapped_nents = 0;
  737. }
  738. sec4_sg_src_index = 1 + (*buflen ? 1 : 0);
  739. pad_nents = pad_sg_nents(sec4_sg_src_index + mapped_nents);
  740. sec4_sg_bytes = pad_nents * sizeof(struct sec4_sg_entry);
  741. /*
  742. * allocate space for base edesc and hw desc commands,
  743. * link tables
  744. */
  745. edesc = ahash_edesc_alloc(req, pad_nents, ctx->sh_desc_update,
  746. ctx->sh_desc_update_dma);
  747. if (!edesc) {
  748. dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
  749. return -ENOMEM;
  750. }
  751. edesc->src_nents = src_nents;
  752. edesc->sec4_sg_bytes = sec4_sg_bytes;
  753. ret = ctx_map_to_sec4_sg(jrdev, state, ctx->ctx_len,
  754. edesc->sec4_sg, DMA_BIDIRECTIONAL);
  755. if (ret)
  756. goto unmap_ctx;
  757. ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1, state);
  758. if (ret)
  759. goto unmap_ctx;
  760. if (mapped_nents)
  761. sg_to_sec4_sg_last(req->src, src_len,
  762. edesc->sec4_sg + sec4_sg_src_index,
  763. 0);
  764. else
  765. sg_to_sec4_set_last(edesc->sec4_sg + sec4_sg_src_index -
  766. 1);
  767. desc = edesc->hw_desc;
  768. edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
  769. sec4_sg_bytes,
  770. DMA_TO_DEVICE);
  771. if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
  772. dev_err(jrdev, "unable to map S/G table\n");
  773. ret = -ENOMEM;
  774. goto unmap_ctx;
  775. }
  776. append_seq_in_ptr(desc, edesc->sec4_sg_dma, ctx->ctx_len +
  777. to_hash, LDST_SGF);
  778. append_seq_out_ptr(desc, state->ctx_dma, ctx->ctx_len, 0);
  779. print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
  780. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  781. desc_bytes(desc), 1);
  782. ret = ahash_enqueue_req(jrdev, ahash_done_bi, req,
  783. ctx->ctx_len, DMA_BIDIRECTIONAL);
  784. } else if (*next_buflen) {
  785. scatterwalk_map_and_copy(buf + *buflen, req->src, 0,
  786. req->nbytes, 0);
  787. *buflen = *next_buflen;
  788. print_hex_dump_debug("buf@" __stringify(__LINE__)": ",
  789. DUMP_PREFIX_ADDRESS, 16, 4, buf,
  790. *buflen, 1);
  791. }
  792. return ret;
  793. unmap_ctx:
  794. ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_BIDIRECTIONAL);
  795. kfree(edesc);
  796. return ret;
  797. }
  798. static int ahash_final_ctx(struct ahash_request *req)
  799. {
  800. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  801. struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash);
  802. struct caam_hash_state *state = ahash_request_ctx_dma(req);
  803. struct device *jrdev = ctx->jrdev;
  804. int buflen = state->buflen;
  805. u32 *desc;
  806. int sec4_sg_bytes;
  807. int digestsize = crypto_ahash_digestsize(ahash);
  808. struct ahash_edesc *edesc;
  809. int ret;
  810. sec4_sg_bytes = pad_sg_nents(1 + (buflen ? 1 : 0)) *
  811. sizeof(struct sec4_sg_entry);
  812. /* allocate space for base edesc and hw desc commands, link tables */
  813. edesc = ahash_edesc_alloc(req, 4, ctx->sh_desc_fin,
  814. ctx->sh_desc_fin_dma);
  815. if (!edesc)
  816. return -ENOMEM;
  817. desc = edesc->hw_desc;
  818. edesc->sec4_sg_bytes = sec4_sg_bytes;
  819. ret = ctx_map_to_sec4_sg(jrdev, state, ctx->ctx_len,
  820. edesc->sec4_sg, DMA_BIDIRECTIONAL);
  821. if (ret)
  822. goto unmap_ctx;
  823. ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1, state);
  824. if (ret)
  825. goto unmap_ctx;
  826. sg_to_sec4_set_last(edesc->sec4_sg + (buflen ? 1 : 0));
  827. edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
  828. sec4_sg_bytes, DMA_TO_DEVICE);
  829. if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
  830. dev_err(jrdev, "unable to map S/G table\n");
  831. ret = -ENOMEM;
  832. goto unmap_ctx;
  833. }
  834. append_seq_in_ptr(desc, edesc->sec4_sg_dma, ctx->ctx_len + buflen,
  835. LDST_SGF);
  836. append_seq_out_ptr(desc, state->ctx_dma, digestsize, 0);
  837. print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
  838. DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
  839. 1);
  840. return ahash_enqueue_req(jrdev, ahash_done_ctx_src, req,
  841. digestsize, DMA_BIDIRECTIONAL);
  842. unmap_ctx:
  843. ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_BIDIRECTIONAL);
  844. kfree(edesc);
  845. return ret;
  846. }
  847. static int ahash_finup_ctx(struct ahash_request *req)
  848. {
  849. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  850. struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash);
  851. struct caam_hash_state *state = ahash_request_ctx_dma(req);
  852. struct device *jrdev = ctx->jrdev;
  853. int buflen = state->buflen;
  854. u32 *desc;
  855. int sec4_sg_src_index;
  856. int src_nents, mapped_nents;
  857. int digestsize = crypto_ahash_digestsize(ahash);
  858. struct ahash_edesc *edesc;
  859. int ret;
  860. src_nents = sg_nents_for_len(req->src, req->nbytes);
  861. if (src_nents < 0) {
  862. dev_err(jrdev, "Invalid number of src SG.\n");
  863. return src_nents;
  864. }
  865. if (src_nents) {
  866. mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
  867. DMA_TO_DEVICE);
  868. if (!mapped_nents) {
  869. dev_err(jrdev, "unable to DMA map source\n");
  870. return -ENOMEM;
  871. }
  872. } else {
  873. mapped_nents = 0;
  874. }
  875. sec4_sg_src_index = 1 + (buflen ? 1 : 0);
  876. /* allocate space for base edesc and hw desc commands, link tables */
  877. edesc = ahash_edesc_alloc(req, sec4_sg_src_index + mapped_nents,
  878. ctx->sh_desc_fin, ctx->sh_desc_fin_dma);
  879. if (!edesc) {
  880. dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
  881. return -ENOMEM;
  882. }
  883. desc = edesc->hw_desc;
  884. edesc->src_nents = src_nents;
  885. ret = ctx_map_to_sec4_sg(jrdev, state, ctx->ctx_len,
  886. edesc->sec4_sg, DMA_BIDIRECTIONAL);
  887. if (ret)
  888. goto unmap_ctx;
  889. ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1, state);
  890. if (ret)
  891. goto unmap_ctx;
  892. ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents,
  893. sec4_sg_src_index, ctx->ctx_len + buflen,
  894. req->nbytes);
  895. if (ret)
  896. goto unmap_ctx;
  897. append_seq_out_ptr(desc, state->ctx_dma, digestsize, 0);
  898. print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
  899. DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
  900. 1);
  901. return ahash_enqueue_req(jrdev, ahash_done_ctx_src, req,
  902. digestsize, DMA_BIDIRECTIONAL);
  903. unmap_ctx:
  904. ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_BIDIRECTIONAL);
  905. kfree(edesc);
  906. return ret;
  907. }
  908. static int ahash_digest(struct ahash_request *req)
  909. {
  910. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  911. struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash);
  912. struct caam_hash_state *state = ahash_request_ctx_dma(req);
  913. struct device *jrdev = ctx->jrdev;
  914. u32 *desc;
  915. int digestsize = crypto_ahash_digestsize(ahash);
  916. int src_nents, mapped_nents;
  917. struct ahash_edesc *edesc;
  918. int ret;
  919. state->buf_dma = 0;
  920. src_nents = sg_nents_for_len(req->src, req->nbytes);
  921. if (src_nents < 0) {
  922. dev_err(jrdev, "Invalid number of src SG.\n");
  923. return src_nents;
  924. }
  925. if (src_nents) {
  926. mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
  927. DMA_TO_DEVICE);
  928. if (!mapped_nents) {
  929. dev_err(jrdev, "unable to map source for DMA\n");
  930. return -ENOMEM;
  931. }
  932. } else {
  933. mapped_nents = 0;
  934. }
  935. /* allocate space for base edesc and hw desc commands, link tables */
  936. edesc = ahash_edesc_alloc(req, mapped_nents > 1 ? mapped_nents : 0,
  937. ctx->sh_desc_digest, ctx->sh_desc_digest_dma);
  938. if (!edesc) {
  939. dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
  940. return -ENOMEM;
  941. }
  942. edesc->src_nents = src_nents;
  943. ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents, 0, 0,
  944. req->nbytes);
  945. if (ret) {
  946. ahash_unmap(jrdev, edesc, req, digestsize);
  947. kfree(edesc);
  948. return ret;
  949. }
  950. desc = edesc->hw_desc;
  951. ret = map_seq_out_ptr_ctx(desc, jrdev, state, digestsize);
  952. if (ret) {
  953. ahash_unmap(jrdev, edesc, req, digestsize);
  954. kfree(edesc);
  955. return -ENOMEM;
  956. }
  957. print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
  958. DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
  959. 1);
  960. return ahash_enqueue_req(jrdev, ahash_done, req, digestsize,
  961. DMA_FROM_DEVICE);
  962. }
  963. /* submit ahash final if it the first job descriptor */
  964. static int ahash_final_no_ctx(struct ahash_request *req)
  965. {
  966. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  967. struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash);
  968. struct caam_hash_state *state = ahash_request_ctx_dma(req);
  969. struct device *jrdev = ctx->jrdev;
  970. u8 *buf = state->buf;
  971. int buflen = state->buflen;
  972. u32 *desc;
  973. int digestsize = crypto_ahash_digestsize(ahash);
  974. struct ahash_edesc *edesc;
  975. int ret;
  976. /* allocate space for base edesc and hw desc commands, link tables */
  977. edesc = ahash_edesc_alloc(req, 0, ctx->sh_desc_digest,
  978. ctx->sh_desc_digest_dma);
  979. if (!edesc)
  980. return -ENOMEM;
  981. desc = edesc->hw_desc;
  982. if (buflen) {
  983. state->buf_dma = dma_map_single(jrdev, buf, buflen,
  984. DMA_TO_DEVICE);
  985. if (dma_mapping_error(jrdev, state->buf_dma)) {
  986. dev_err(jrdev, "unable to map src\n");
  987. goto unmap;
  988. }
  989. append_seq_in_ptr(desc, state->buf_dma, buflen, 0);
  990. }
  991. ret = map_seq_out_ptr_ctx(desc, jrdev, state, digestsize);
  992. if (ret)
  993. goto unmap;
  994. print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
  995. DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
  996. 1);
  997. return ahash_enqueue_req(jrdev, ahash_done, req,
  998. digestsize, DMA_FROM_DEVICE);
  999. unmap:
  1000. ahash_unmap(jrdev, edesc, req, digestsize);
  1001. kfree(edesc);
  1002. return -ENOMEM;
  1003. }
  1004. /* submit ahash update if it the first job descriptor after update */
  1005. static int ahash_update_no_ctx(struct ahash_request *req)
  1006. {
  1007. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  1008. struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash);
  1009. struct caam_hash_state *state = ahash_request_ctx_dma(req);
  1010. struct device *jrdev = ctx->jrdev;
  1011. u8 *buf = state->buf;
  1012. int *buflen = &state->buflen;
  1013. int *next_buflen = &state->next_buflen;
  1014. int blocksize = crypto_ahash_blocksize(ahash);
  1015. int in_len = *buflen + req->nbytes, to_hash;
  1016. int sec4_sg_bytes, src_nents, mapped_nents;
  1017. struct ahash_edesc *edesc;
  1018. u32 *desc;
  1019. int ret = 0;
  1020. *next_buflen = in_len & (blocksize - 1);
  1021. to_hash = in_len - *next_buflen;
  1022. /*
  1023. * For XCBC and CMAC, if to_hash is multiple of block size,
  1024. * keep last block in internal buffer
  1025. */
  1026. if ((is_xcbc_aes(ctx->adata.algtype) ||
  1027. is_cmac_aes(ctx->adata.algtype)) && to_hash >= blocksize &&
  1028. (*next_buflen == 0)) {
  1029. *next_buflen = blocksize;
  1030. to_hash -= blocksize;
  1031. }
  1032. if (to_hash) {
  1033. int pad_nents;
  1034. int src_len = req->nbytes - *next_buflen;
  1035. src_nents = sg_nents_for_len(req->src, src_len);
  1036. if (src_nents < 0) {
  1037. dev_err(jrdev, "Invalid number of src SG.\n");
  1038. return src_nents;
  1039. }
  1040. if (src_nents) {
  1041. mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
  1042. DMA_TO_DEVICE);
  1043. if (!mapped_nents) {
  1044. dev_err(jrdev, "unable to DMA map source\n");
  1045. return -ENOMEM;
  1046. }
  1047. } else {
  1048. mapped_nents = 0;
  1049. }
  1050. pad_nents = pad_sg_nents(1 + mapped_nents);
  1051. sec4_sg_bytes = pad_nents * sizeof(struct sec4_sg_entry);
  1052. /*
  1053. * allocate space for base edesc and hw desc commands,
  1054. * link tables
  1055. */
  1056. edesc = ahash_edesc_alloc(req, pad_nents,
  1057. ctx->sh_desc_update_first,
  1058. ctx->sh_desc_update_first_dma);
  1059. if (!edesc) {
  1060. dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
  1061. return -ENOMEM;
  1062. }
  1063. edesc->src_nents = src_nents;
  1064. edesc->sec4_sg_bytes = sec4_sg_bytes;
  1065. ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg, state);
  1066. if (ret)
  1067. goto unmap_ctx;
  1068. sg_to_sec4_sg_last(req->src, src_len, edesc->sec4_sg + 1, 0);
  1069. desc = edesc->hw_desc;
  1070. edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
  1071. sec4_sg_bytes,
  1072. DMA_TO_DEVICE);
  1073. if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
  1074. dev_err(jrdev, "unable to map S/G table\n");
  1075. ret = -ENOMEM;
  1076. goto unmap_ctx;
  1077. }
  1078. append_seq_in_ptr(desc, edesc->sec4_sg_dma, to_hash, LDST_SGF);
  1079. ret = map_seq_out_ptr_ctx(desc, jrdev, state, ctx->ctx_len);
  1080. if (ret)
  1081. goto unmap_ctx;
  1082. print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
  1083. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  1084. desc_bytes(desc), 1);
  1085. ret = ahash_enqueue_req(jrdev, ahash_done_ctx_dst, req,
  1086. ctx->ctx_len, DMA_TO_DEVICE);
  1087. if ((ret != -EINPROGRESS) && (ret != -EBUSY))
  1088. return ret;
  1089. state->update = ahash_update_ctx;
  1090. state->finup = ahash_finup_ctx;
  1091. state->final = ahash_final_ctx;
  1092. } else if (*next_buflen) {
  1093. scatterwalk_map_and_copy(buf + *buflen, req->src, 0,
  1094. req->nbytes, 0);
  1095. *buflen = *next_buflen;
  1096. print_hex_dump_debug("buf@" __stringify(__LINE__)": ",
  1097. DUMP_PREFIX_ADDRESS, 16, 4, buf,
  1098. *buflen, 1);
  1099. }
  1100. return ret;
  1101. unmap_ctx:
  1102. ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_TO_DEVICE);
  1103. kfree(edesc);
  1104. return ret;
  1105. }
  1106. /* submit ahash finup if it the first job descriptor after update */
  1107. static int ahash_finup_no_ctx(struct ahash_request *req)
  1108. {
  1109. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  1110. struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash);
  1111. struct caam_hash_state *state = ahash_request_ctx_dma(req);
  1112. struct device *jrdev = ctx->jrdev;
  1113. int buflen = state->buflen;
  1114. u32 *desc;
  1115. int sec4_sg_bytes, sec4_sg_src_index, src_nents, mapped_nents;
  1116. int digestsize = crypto_ahash_digestsize(ahash);
  1117. struct ahash_edesc *edesc;
  1118. int ret;
  1119. src_nents = sg_nents_for_len(req->src, req->nbytes);
  1120. if (src_nents < 0) {
  1121. dev_err(jrdev, "Invalid number of src SG.\n");
  1122. return src_nents;
  1123. }
  1124. if (src_nents) {
  1125. mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
  1126. DMA_TO_DEVICE);
  1127. if (!mapped_nents) {
  1128. dev_err(jrdev, "unable to DMA map source\n");
  1129. return -ENOMEM;
  1130. }
  1131. } else {
  1132. mapped_nents = 0;
  1133. }
  1134. sec4_sg_src_index = 2;
  1135. sec4_sg_bytes = (sec4_sg_src_index + mapped_nents) *
  1136. sizeof(struct sec4_sg_entry);
  1137. /* allocate space for base edesc and hw desc commands, link tables */
  1138. edesc = ahash_edesc_alloc(req, sec4_sg_src_index + mapped_nents,
  1139. ctx->sh_desc_digest, ctx->sh_desc_digest_dma);
  1140. if (!edesc) {
  1141. dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
  1142. return -ENOMEM;
  1143. }
  1144. desc = edesc->hw_desc;
  1145. edesc->src_nents = src_nents;
  1146. edesc->sec4_sg_bytes = sec4_sg_bytes;
  1147. ret = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg, state);
  1148. if (ret)
  1149. goto unmap;
  1150. ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents, 1, buflen,
  1151. req->nbytes);
  1152. if (ret) {
  1153. dev_err(jrdev, "unable to map S/G table\n");
  1154. goto unmap;
  1155. }
  1156. ret = map_seq_out_ptr_ctx(desc, jrdev, state, digestsize);
  1157. if (ret)
  1158. goto unmap;
  1159. print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
  1160. DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
  1161. 1);
  1162. return ahash_enqueue_req(jrdev, ahash_done, req,
  1163. digestsize, DMA_FROM_DEVICE);
  1164. unmap:
  1165. ahash_unmap(jrdev, edesc, req, digestsize);
  1166. kfree(edesc);
  1167. return -ENOMEM;
  1168. }
  1169. /* submit first update job descriptor after init */
  1170. static int ahash_update_first(struct ahash_request *req)
  1171. {
  1172. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  1173. struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash);
  1174. struct caam_hash_state *state = ahash_request_ctx_dma(req);
  1175. struct device *jrdev = ctx->jrdev;
  1176. u8 *buf = state->buf;
  1177. int *buflen = &state->buflen;
  1178. int *next_buflen = &state->next_buflen;
  1179. int to_hash;
  1180. int blocksize = crypto_ahash_blocksize(ahash);
  1181. u32 *desc;
  1182. int src_nents, mapped_nents;
  1183. struct ahash_edesc *edesc;
  1184. int ret = 0;
  1185. *next_buflen = req->nbytes & (blocksize - 1);
  1186. to_hash = req->nbytes - *next_buflen;
  1187. /*
  1188. * For XCBC and CMAC, if to_hash is multiple of block size,
  1189. * keep last block in internal buffer
  1190. */
  1191. if ((is_xcbc_aes(ctx->adata.algtype) ||
  1192. is_cmac_aes(ctx->adata.algtype)) && to_hash >= blocksize &&
  1193. (*next_buflen == 0)) {
  1194. *next_buflen = blocksize;
  1195. to_hash -= blocksize;
  1196. }
  1197. if (to_hash) {
  1198. src_nents = sg_nents_for_len(req->src,
  1199. req->nbytes - *next_buflen);
  1200. if (src_nents < 0) {
  1201. dev_err(jrdev, "Invalid number of src SG.\n");
  1202. return src_nents;
  1203. }
  1204. if (src_nents) {
  1205. mapped_nents = dma_map_sg(jrdev, req->src, src_nents,
  1206. DMA_TO_DEVICE);
  1207. if (!mapped_nents) {
  1208. dev_err(jrdev, "unable to map source for DMA\n");
  1209. return -ENOMEM;
  1210. }
  1211. } else {
  1212. mapped_nents = 0;
  1213. }
  1214. /*
  1215. * allocate space for base edesc and hw desc commands,
  1216. * link tables
  1217. */
  1218. edesc = ahash_edesc_alloc(req, mapped_nents > 1 ?
  1219. mapped_nents : 0,
  1220. ctx->sh_desc_update_first,
  1221. ctx->sh_desc_update_first_dma);
  1222. if (!edesc) {
  1223. dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
  1224. return -ENOMEM;
  1225. }
  1226. edesc->src_nents = src_nents;
  1227. ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents, 0, 0,
  1228. to_hash);
  1229. if (ret)
  1230. goto unmap_ctx;
  1231. desc = edesc->hw_desc;
  1232. ret = map_seq_out_ptr_ctx(desc, jrdev, state, ctx->ctx_len);
  1233. if (ret)
  1234. goto unmap_ctx;
  1235. print_hex_dump_debug("jobdesc@"__stringify(__LINE__)": ",
  1236. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  1237. desc_bytes(desc), 1);
  1238. ret = ahash_enqueue_req(jrdev, ahash_done_ctx_dst, req,
  1239. ctx->ctx_len, DMA_TO_DEVICE);
  1240. if ((ret != -EINPROGRESS) && (ret != -EBUSY))
  1241. return ret;
  1242. state->update = ahash_update_ctx;
  1243. state->finup = ahash_finup_ctx;
  1244. state->final = ahash_final_ctx;
  1245. } else if (*next_buflen) {
  1246. state->update = ahash_update_no_ctx;
  1247. state->finup = ahash_finup_no_ctx;
  1248. state->final = ahash_final_no_ctx;
  1249. scatterwalk_map_and_copy(buf, req->src, 0,
  1250. req->nbytes, 0);
  1251. *buflen = *next_buflen;
  1252. print_hex_dump_debug("buf@" __stringify(__LINE__)": ",
  1253. DUMP_PREFIX_ADDRESS, 16, 4, buf,
  1254. *buflen, 1);
  1255. }
  1256. return ret;
  1257. unmap_ctx:
  1258. ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_TO_DEVICE);
  1259. kfree(edesc);
  1260. return ret;
  1261. }
  1262. static int ahash_finup_first(struct ahash_request *req)
  1263. {
  1264. return ahash_digest(req);
  1265. }
  1266. static int ahash_init(struct ahash_request *req)
  1267. {
  1268. struct caam_hash_state *state = ahash_request_ctx_dma(req);
  1269. state->update = ahash_update_first;
  1270. state->finup = ahash_finup_first;
  1271. state->final = ahash_final_no_ctx;
  1272. state->ctx_dma = 0;
  1273. state->ctx_dma_len = 0;
  1274. state->buf_dma = 0;
  1275. state->buflen = 0;
  1276. state->next_buflen = 0;
  1277. return 0;
  1278. }
  1279. static int ahash_update(struct ahash_request *req)
  1280. {
  1281. struct caam_hash_state *state = ahash_request_ctx_dma(req);
  1282. return state->update(req);
  1283. }
  1284. static int ahash_finup(struct ahash_request *req)
  1285. {
  1286. struct caam_hash_state *state = ahash_request_ctx_dma(req);
  1287. return state->finup(req);
  1288. }
  1289. static int ahash_final(struct ahash_request *req)
  1290. {
  1291. struct caam_hash_state *state = ahash_request_ctx_dma(req);
  1292. return state->final(req);
  1293. }
  1294. static int ahash_export(struct ahash_request *req, void *out)
  1295. {
  1296. struct caam_hash_state *state = ahash_request_ctx_dma(req);
  1297. struct caam_export_state *export = out;
  1298. u8 *buf = state->buf;
  1299. int len = state->buflen;
  1300. memcpy(export->buf, buf, len);
  1301. memcpy(export->caam_ctx, state->caam_ctx, sizeof(export->caam_ctx));
  1302. export->buflen = len;
  1303. export->update = state->update;
  1304. export->final = state->final;
  1305. export->finup = state->finup;
  1306. return 0;
  1307. }
  1308. static int ahash_import(struct ahash_request *req, const void *in)
  1309. {
  1310. struct caam_hash_state *state = ahash_request_ctx_dma(req);
  1311. const struct caam_export_state *export = in;
  1312. memset(state, 0, sizeof(*state));
  1313. memcpy(state->buf, export->buf, export->buflen);
  1314. memcpy(state->caam_ctx, export->caam_ctx, sizeof(state->caam_ctx));
  1315. state->buflen = export->buflen;
  1316. state->update = export->update;
  1317. state->final = export->final;
  1318. state->finup = export->finup;
  1319. return 0;
  1320. }
  1321. struct caam_hash_template {
  1322. char name[CRYPTO_MAX_ALG_NAME];
  1323. char driver_name[CRYPTO_MAX_ALG_NAME];
  1324. char hmac_name[CRYPTO_MAX_ALG_NAME];
  1325. char hmac_driver_name[CRYPTO_MAX_ALG_NAME];
  1326. unsigned int blocksize;
  1327. struct ahash_alg template_ahash;
  1328. u32 alg_type;
  1329. };
  1330. /* ahash descriptors */
  1331. static struct caam_hash_template driver_hash[] = {
  1332. {
  1333. .name = "sha1",
  1334. .driver_name = "sha1-caam",
  1335. .hmac_name = "hmac(sha1)",
  1336. .hmac_driver_name = "hmac-sha1-caam",
  1337. .blocksize = SHA1_BLOCK_SIZE,
  1338. .template_ahash = {
  1339. .init = ahash_init,
  1340. .update = ahash_update,
  1341. .final = ahash_final,
  1342. .finup = ahash_finup,
  1343. .digest = ahash_digest,
  1344. .export = ahash_export,
  1345. .import = ahash_import,
  1346. .setkey = ahash_setkey,
  1347. .halg = {
  1348. .digestsize = SHA1_DIGEST_SIZE,
  1349. .statesize = sizeof(struct caam_export_state),
  1350. },
  1351. },
  1352. .alg_type = OP_ALG_ALGSEL_SHA1,
  1353. }, {
  1354. .name = "sha224",
  1355. .driver_name = "sha224-caam",
  1356. .hmac_name = "hmac(sha224)",
  1357. .hmac_driver_name = "hmac-sha224-caam",
  1358. .blocksize = SHA224_BLOCK_SIZE,
  1359. .template_ahash = {
  1360. .init = ahash_init,
  1361. .update = ahash_update,
  1362. .final = ahash_final,
  1363. .finup = ahash_finup,
  1364. .digest = ahash_digest,
  1365. .export = ahash_export,
  1366. .import = ahash_import,
  1367. .setkey = ahash_setkey,
  1368. .halg = {
  1369. .digestsize = SHA224_DIGEST_SIZE,
  1370. .statesize = sizeof(struct caam_export_state),
  1371. },
  1372. },
  1373. .alg_type = OP_ALG_ALGSEL_SHA224,
  1374. }, {
  1375. .name = "sha256",
  1376. .driver_name = "sha256-caam",
  1377. .hmac_name = "hmac(sha256)",
  1378. .hmac_driver_name = "hmac-sha256-caam",
  1379. .blocksize = SHA256_BLOCK_SIZE,
  1380. .template_ahash = {
  1381. .init = ahash_init,
  1382. .update = ahash_update,
  1383. .final = ahash_final,
  1384. .finup = ahash_finup,
  1385. .digest = ahash_digest,
  1386. .export = ahash_export,
  1387. .import = ahash_import,
  1388. .setkey = ahash_setkey,
  1389. .halg = {
  1390. .digestsize = SHA256_DIGEST_SIZE,
  1391. .statesize = sizeof(struct caam_export_state),
  1392. },
  1393. },
  1394. .alg_type = OP_ALG_ALGSEL_SHA256,
  1395. }, {
  1396. .name = "sha384",
  1397. .driver_name = "sha384-caam",
  1398. .hmac_name = "hmac(sha384)",
  1399. .hmac_driver_name = "hmac-sha384-caam",
  1400. .blocksize = SHA384_BLOCK_SIZE,
  1401. .template_ahash = {
  1402. .init = ahash_init,
  1403. .update = ahash_update,
  1404. .final = ahash_final,
  1405. .finup = ahash_finup,
  1406. .digest = ahash_digest,
  1407. .export = ahash_export,
  1408. .import = ahash_import,
  1409. .setkey = ahash_setkey,
  1410. .halg = {
  1411. .digestsize = SHA384_DIGEST_SIZE,
  1412. .statesize = sizeof(struct caam_export_state),
  1413. },
  1414. },
  1415. .alg_type = OP_ALG_ALGSEL_SHA384,
  1416. }, {
  1417. .name = "sha512",
  1418. .driver_name = "sha512-caam",
  1419. .hmac_name = "hmac(sha512)",
  1420. .hmac_driver_name = "hmac-sha512-caam",
  1421. .blocksize = SHA512_BLOCK_SIZE,
  1422. .template_ahash = {
  1423. .init = ahash_init,
  1424. .update = ahash_update,
  1425. .final = ahash_final,
  1426. .finup = ahash_finup,
  1427. .digest = ahash_digest,
  1428. .export = ahash_export,
  1429. .import = ahash_import,
  1430. .setkey = ahash_setkey,
  1431. .halg = {
  1432. .digestsize = SHA512_DIGEST_SIZE,
  1433. .statesize = sizeof(struct caam_export_state),
  1434. },
  1435. },
  1436. .alg_type = OP_ALG_ALGSEL_SHA512,
  1437. }, {
  1438. .name = "md5",
  1439. .driver_name = "md5-caam",
  1440. .hmac_name = "hmac(md5)",
  1441. .hmac_driver_name = "hmac-md5-caam",
  1442. .blocksize = MD5_BLOCK_WORDS * 4,
  1443. .template_ahash = {
  1444. .init = ahash_init,
  1445. .update = ahash_update,
  1446. .final = ahash_final,
  1447. .finup = ahash_finup,
  1448. .digest = ahash_digest,
  1449. .export = ahash_export,
  1450. .import = ahash_import,
  1451. .setkey = ahash_setkey,
  1452. .halg = {
  1453. .digestsize = MD5_DIGEST_SIZE,
  1454. .statesize = sizeof(struct caam_export_state),
  1455. },
  1456. },
  1457. .alg_type = OP_ALG_ALGSEL_MD5,
  1458. }, {
  1459. .hmac_name = "xcbc(aes)",
  1460. .hmac_driver_name = "xcbc-aes-caam",
  1461. .blocksize = AES_BLOCK_SIZE,
  1462. .template_ahash = {
  1463. .init = ahash_init,
  1464. .update = ahash_update,
  1465. .final = ahash_final,
  1466. .finup = ahash_finup,
  1467. .digest = ahash_digest,
  1468. .export = ahash_export,
  1469. .import = ahash_import,
  1470. .setkey = axcbc_setkey,
  1471. .halg = {
  1472. .digestsize = AES_BLOCK_SIZE,
  1473. .statesize = sizeof(struct caam_export_state),
  1474. },
  1475. },
  1476. .alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_XCBC_MAC,
  1477. }, {
  1478. .hmac_name = "cmac(aes)",
  1479. .hmac_driver_name = "cmac-aes-caam",
  1480. .blocksize = AES_BLOCK_SIZE,
  1481. .template_ahash = {
  1482. .init = ahash_init,
  1483. .update = ahash_update,
  1484. .final = ahash_final,
  1485. .finup = ahash_finup,
  1486. .digest = ahash_digest,
  1487. .export = ahash_export,
  1488. .import = ahash_import,
  1489. .setkey = acmac_setkey,
  1490. .halg = {
  1491. .digestsize = AES_BLOCK_SIZE,
  1492. .statesize = sizeof(struct caam_export_state),
  1493. },
  1494. },
  1495. .alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CMAC,
  1496. },
  1497. };
  1498. struct caam_hash_alg {
  1499. struct list_head entry;
  1500. int alg_type;
  1501. bool is_hmac;
  1502. struct ahash_engine_alg ahash_alg;
  1503. };
  1504. static int caam_hash_cra_init(struct crypto_tfm *tfm)
  1505. {
  1506. struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
  1507. struct crypto_alg *base = tfm->__crt_alg;
  1508. struct hash_alg_common *halg =
  1509. container_of(base, struct hash_alg_common, base);
  1510. struct ahash_alg *alg =
  1511. container_of(halg, struct ahash_alg, halg);
  1512. struct caam_hash_alg *caam_hash =
  1513. container_of(alg, struct caam_hash_alg, ahash_alg.base);
  1514. struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash);
  1515. /* Sizes for MDHA running digests: MD5, SHA1, 224, 256, 384, 512 */
  1516. static const u8 runninglen[] = { HASH_MSG_LEN + MD5_DIGEST_SIZE,
  1517. HASH_MSG_LEN + SHA1_DIGEST_SIZE,
  1518. HASH_MSG_LEN + 32,
  1519. HASH_MSG_LEN + SHA256_DIGEST_SIZE,
  1520. HASH_MSG_LEN + 64,
  1521. HASH_MSG_LEN + SHA512_DIGEST_SIZE };
  1522. const size_t sh_desc_update_offset = offsetof(struct caam_hash_ctx,
  1523. sh_desc_update);
  1524. dma_addr_t dma_addr;
  1525. struct caam_drv_private *priv;
  1526. /*
  1527. * Get a Job ring from Job Ring driver to ensure in-order
  1528. * crypto request processing per tfm
  1529. */
  1530. ctx->jrdev = caam_jr_alloc();
  1531. if (IS_ERR(ctx->jrdev)) {
  1532. pr_err("Job Ring Device allocation for transform failed\n");
  1533. return PTR_ERR(ctx->jrdev);
  1534. }
  1535. priv = dev_get_drvdata(ctx->jrdev->parent);
  1536. if (is_xcbc_aes(caam_hash->alg_type)) {
  1537. ctx->dir = DMA_TO_DEVICE;
  1538. ctx->key_dir = DMA_BIDIRECTIONAL;
  1539. ctx->adata.algtype = OP_TYPE_CLASS1_ALG | caam_hash->alg_type;
  1540. ctx->ctx_len = 48;
  1541. } else if (is_cmac_aes(caam_hash->alg_type)) {
  1542. ctx->dir = DMA_TO_DEVICE;
  1543. ctx->key_dir = DMA_NONE;
  1544. ctx->adata.algtype = OP_TYPE_CLASS1_ALG | caam_hash->alg_type;
  1545. ctx->ctx_len = 32;
  1546. } else {
  1547. if (priv->era >= 6) {
  1548. ctx->dir = DMA_BIDIRECTIONAL;
  1549. ctx->key_dir = caam_hash->is_hmac ? DMA_TO_DEVICE : DMA_NONE;
  1550. } else {
  1551. ctx->dir = DMA_TO_DEVICE;
  1552. ctx->key_dir = DMA_NONE;
  1553. }
  1554. ctx->adata.algtype = OP_TYPE_CLASS2_ALG | caam_hash->alg_type;
  1555. ctx->ctx_len = runninglen[(ctx->adata.algtype &
  1556. OP_ALG_ALGSEL_SUBMASK) >>
  1557. OP_ALG_ALGSEL_SHIFT];
  1558. }
  1559. if (ctx->key_dir != DMA_NONE) {
  1560. ctx->adata.key_dma = dma_map_single_attrs(ctx->jrdev, ctx->key,
  1561. ARRAY_SIZE(ctx->key),
  1562. ctx->key_dir,
  1563. DMA_ATTR_SKIP_CPU_SYNC);
  1564. if (dma_mapping_error(ctx->jrdev, ctx->adata.key_dma)) {
  1565. dev_err(ctx->jrdev, "unable to map key\n");
  1566. caam_jr_free(ctx->jrdev);
  1567. return -ENOMEM;
  1568. }
  1569. }
  1570. dma_addr = dma_map_single_attrs(ctx->jrdev, ctx->sh_desc_update,
  1571. offsetof(struct caam_hash_ctx, key) -
  1572. sh_desc_update_offset,
  1573. ctx->dir, DMA_ATTR_SKIP_CPU_SYNC);
  1574. if (dma_mapping_error(ctx->jrdev, dma_addr)) {
  1575. dev_err(ctx->jrdev, "unable to map shared descriptors\n");
  1576. if (ctx->key_dir != DMA_NONE)
  1577. dma_unmap_single_attrs(ctx->jrdev, ctx->adata.key_dma,
  1578. ARRAY_SIZE(ctx->key),
  1579. ctx->key_dir,
  1580. DMA_ATTR_SKIP_CPU_SYNC);
  1581. caam_jr_free(ctx->jrdev);
  1582. return -ENOMEM;
  1583. }
  1584. ctx->sh_desc_update_dma = dma_addr;
  1585. ctx->sh_desc_update_first_dma = dma_addr +
  1586. offsetof(struct caam_hash_ctx,
  1587. sh_desc_update_first) -
  1588. sh_desc_update_offset;
  1589. ctx->sh_desc_fin_dma = dma_addr + offsetof(struct caam_hash_ctx,
  1590. sh_desc_fin) -
  1591. sh_desc_update_offset;
  1592. ctx->sh_desc_digest_dma = dma_addr + offsetof(struct caam_hash_ctx,
  1593. sh_desc_digest) -
  1594. sh_desc_update_offset;
  1595. crypto_ahash_set_reqsize_dma(ahash, sizeof(struct caam_hash_state));
  1596. /*
  1597. * For keyed hash algorithms shared descriptors
  1598. * will be created later in setkey() callback
  1599. */
  1600. return caam_hash->is_hmac ? 0 : ahash_set_sh_desc(ahash);
  1601. }
  1602. static void caam_hash_cra_exit(struct crypto_tfm *tfm)
  1603. {
  1604. struct caam_hash_ctx *ctx = crypto_tfm_ctx_dma(tfm);
  1605. dma_unmap_single_attrs(ctx->jrdev, ctx->sh_desc_update_dma,
  1606. offsetof(struct caam_hash_ctx, key) -
  1607. offsetof(struct caam_hash_ctx, sh_desc_update),
  1608. ctx->dir, DMA_ATTR_SKIP_CPU_SYNC);
  1609. if (ctx->key_dir != DMA_NONE)
  1610. dma_unmap_single_attrs(ctx->jrdev, ctx->adata.key_dma,
  1611. ARRAY_SIZE(ctx->key), ctx->key_dir,
  1612. DMA_ATTR_SKIP_CPU_SYNC);
  1613. caam_jr_free(ctx->jrdev);
  1614. }
  1615. void caam_algapi_hash_exit(void)
  1616. {
  1617. struct caam_hash_alg *t_alg, *n;
  1618. if (!hash_list.next)
  1619. return;
  1620. list_for_each_entry_safe(t_alg, n, &hash_list, entry) {
  1621. crypto_engine_unregister_ahash(&t_alg->ahash_alg);
  1622. list_del(&t_alg->entry);
  1623. kfree(t_alg);
  1624. }
  1625. }
  1626. static struct caam_hash_alg *
  1627. caam_hash_alloc(struct caam_hash_template *template,
  1628. bool keyed)
  1629. {
  1630. struct caam_hash_alg *t_alg;
  1631. struct ahash_alg *halg;
  1632. struct crypto_alg *alg;
  1633. t_alg = kzalloc_obj(*t_alg);
  1634. if (!t_alg)
  1635. return ERR_PTR(-ENOMEM);
  1636. t_alg->ahash_alg.base = template->template_ahash;
  1637. halg = &t_alg->ahash_alg.base;
  1638. alg = &halg->halg.base;
  1639. if (keyed) {
  1640. snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s",
  1641. template->hmac_name);
  1642. snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  1643. template->hmac_driver_name);
  1644. t_alg->is_hmac = true;
  1645. } else {
  1646. snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s",
  1647. template->name);
  1648. snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  1649. template->driver_name);
  1650. halg->setkey = NULL;
  1651. t_alg->is_hmac = false;
  1652. }
  1653. alg->cra_module = THIS_MODULE;
  1654. alg->cra_init = caam_hash_cra_init;
  1655. alg->cra_exit = caam_hash_cra_exit;
  1656. alg->cra_ctxsize = sizeof(struct caam_hash_ctx) + crypto_dma_padding();
  1657. alg->cra_priority = CAAM_CRA_PRIORITY;
  1658. alg->cra_blocksize = template->blocksize;
  1659. alg->cra_alignmask = 0;
  1660. alg->cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY;
  1661. t_alg->alg_type = template->alg_type;
  1662. t_alg->ahash_alg.op.do_one_request = ahash_do_one_req;
  1663. return t_alg;
  1664. }
  1665. int caam_algapi_hash_init(struct device *ctrldev)
  1666. {
  1667. int i = 0, err = 0;
  1668. struct caam_drv_private *priv = dev_get_drvdata(ctrldev);
  1669. unsigned int md_limit = SHA512_DIGEST_SIZE;
  1670. u32 md_inst, md_vid;
  1671. /*
  1672. * Register crypto algorithms the device supports. First, identify
  1673. * presence and attributes of MD block.
  1674. */
  1675. if (priv->era < 10) {
  1676. struct caam_perfmon __iomem *perfmon = &priv->jr[0]->perfmon;
  1677. md_vid = (rd_reg32(&perfmon->cha_id_ls) &
  1678. CHA_ID_LS_MD_MASK) >> CHA_ID_LS_MD_SHIFT;
  1679. md_inst = (rd_reg32(&perfmon->cha_num_ls) &
  1680. CHA_ID_LS_MD_MASK) >> CHA_ID_LS_MD_SHIFT;
  1681. } else {
  1682. u32 mdha = rd_reg32(&priv->jr[0]->vreg.mdha);
  1683. md_vid = (mdha & CHA_VER_VID_MASK) >> CHA_VER_VID_SHIFT;
  1684. md_inst = mdha & CHA_VER_NUM_MASK;
  1685. }
  1686. /*
  1687. * Skip registration of any hashing algorithms if MD block
  1688. * is not present.
  1689. */
  1690. if (!md_inst)
  1691. return 0;
  1692. /* Limit digest size based on LP256 */
  1693. if (md_vid == CHA_VER_VID_MD_LP256)
  1694. md_limit = SHA256_DIGEST_SIZE;
  1695. INIT_LIST_HEAD(&hash_list);
  1696. /* register crypto algorithms the device supports */
  1697. for (i = 0; i < ARRAY_SIZE(driver_hash); i++) {
  1698. struct caam_hash_alg *t_alg;
  1699. struct caam_hash_template *alg = driver_hash + i;
  1700. /* If MD size is not supported by device, skip registration */
  1701. if (is_mdha(alg->alg_type) &&
  1702. alg->template_ahash.halg.digestsize > md_limit)
  1703. continue;
  1704. /* register hmac version */
  1705. t_alg = caam_hash_alloc(alg, true);
  1706. if (IS_ERR(t_alg)) {
  1707. err = PTR_ERR(t_alg);
  1708. pr_warn("%s alg allocation failed\n",
  1709. alg->hmac_driver_name);
  1710. continue;
  1711. }
  1712. err = crypto_engine_register_ahash(&t_alg->ahash_alg);
  1713. if (err) {
  1714. pr_warn("%s alg registration failed: %d\n",
  1715. t_alg->ahash_alg.base.halg.base.cra_driver_name,
  1716. err);
  1717. kfree(t_alg);
  1718. } else
  1719. list_add_tail(&t_alg->entry, &hash_list);
  1720. if ((alg->alg_type & OP_ALG_ALGSEL_MASK) == OP_ALG_ALGSEL_AES)
  1721. continue;
  1722. /* register unkeyed version */
  1723. t_alg = caam_hash_alloc(alg, false);
  1724. if (IS_ERR(t_alg)) {
  1725. err = PTR_ERR(t_alg);
  1726. pr_warn("%s alg allocation failed\n", alg->driver_name);
  1727. continue;
  1728. }
  1729. err = crypto_engine_register_ahash(&t_alg->ahash_alg);
  1730. if (err) {
  1731. pr_warn("%s alg registration failed: %d\n",
  1732. t_alg->ahash_alg.base.halg.base.cra_driver_name,
  1733. err);
  1734. kfree(t_alg);
  1735. } else
  1736. list_add_tail(&t_alg->entry, &hash_list);
  1737. }
  1738. return err;
  1739. }