caamhash.c 57 KB

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