ccp-ops.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422
  1. /*
  2. * AMD Cryptographic Coprocessor (CCP) driver
  3. *
  4. * Copyright (C) 2013,2017 Advanced Micro Devices, Inc.
  5. *
  6. * Author: Tom Lendacky <thomas.lendacky@amd.com>
  7. * Author: Gary R Hook <gary.hook@amd.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/pci.h>
  16. #include <linux/interrupt.h>
  17. #include <crypto/scatterwalk.h>
  18. #include <crypto/des.h>
  19. #include <linux/ccp.h>
  20. #include "ccp-dev.h"
  21. /* SHA initial context values */
  22. static const __be32 ccp_sha1_init[SHA1_DIGEST_SIZE / sizeof(__be32)] = {
  23. cpu_to_be32(SHA1_H0), cpu_to_be32(SHA1_H1),
  24. cpu_to_be32(SHA1_H2), cpu_to_be32(SHA1_H3),
  25. cpu_to_be32(SHA1_H4),
  26. };
  27. static const __be32 ccp_sha224_init[SHA256_DIGEST_SIZE / sizeof(__be32)] = {
  28. cpu_to_be32(SHA224_H0), cpu_to_be32(SHA224_H1),
  29. cpu_to_be32(SHA224_H2), cpu_to_be32(SHA224_H3),
  30. cpu_to_be32(SHA224_H4), cpu_to_be32(SHA224_H5),
  31. cpu_to_be32(SHA224_H6), cpu_to_be32(SHA224_H7),
  32. };
  33. static const __be32 ccp_sha256_init[SHA256_DIGEST_SIZE / sizeof(__be32)] = {
  34. cpu_to_be32(SHA256_H0), cpu_to_be32(SHA256_H1),
  35. cpu_to_be32(SHA256_H2), cpu_to_be32(SHA256_H3),
  36. cpu_to_be32(SHA256_H4), cpu_to_be32(SHA256_H5),
  37. cpu_to_be32(SHA256_H6), cpu_to_be32(SHA256_H7),
  38. };
  39. static const __be64 ccp_sha384_init[SHA512_DIGEST_SIZE / sizeof(__be64)] = {
  40. cpu_to_be64(SHA384_H0), cpu_to_be64(SHA384_H1),
  41. cpu_to_be64(SHA384_H2), cpu_to_be64(SHA384_H3),
  42. cpu_to_be64(SHA384_H4), cpu_to_be64(SHA384_H5),
  43. cpu_to_be64(SHA384_H6), cpu_to_be64(SHA384_H7),
  44. };
  45. static const __be64 ccp_sha512_init[SHA512_DIGEST_SIZE / sizeof(__be64)] = {
  46. cpu_to_be64(SHA512_H0), cpu_to_be64(SHA512_H1),
  47. cpu_to_be64(SHA512_H2), cpu_to_be64(SHA512_H3),
  48. cpu_to_be64(SHA512_H4), cpu_to_be64(SHA512_H5),
  49. cpu_to_be64(SHA512_H6), cpu_to_be64(SHA512_H7),
  50. };
  51. #define CCP_NEW_JOBID(ccp) ((ccp->vdata->version == CCP_VERSION(3, 0)) ? \
  52. ccp_gen_jobid(ccp) : 0)
  53. static u32 ccp_gen_jobid(struct ccp_device *ccp)
  54. {
  55. return atomic_inc_return(&ccp->current_id) & CCP_JOBID_MASK;
  56. }
  57. static void ccp_sg_free(struct ccp_sg_workarea *wa)
  58. {
  59. if (wa->dma_count)
  60. dma_unmap_sg(wa->dma_dev, wa->dma_sg, wa->nents, wa->dma_dir);
  61. wa->dma_count = 0;
  62. }
  63. static int ccp_init_sg_workarea(struct ccp_sg_workarea *wa, struct device *dev,
  64. struct scatterlist *sg, u64 len,
  65. enum dma_data_direction dma_dir)
  66. {
  67. memset(wa, 0, sizeof(*wa));
  68. wa->sg = sg;
  69. if (!sg)
  70. return 0;
  71. wa->nents = sg_nents_for_len(sg, len);
  72. if (wa->nents < 0)
  73. return wa->nents;
  74. wa->bytes_left = len;
  75. wa->sg_used = 0;
  76. if (len == 0)
  77. return 0;
  78. if (dma_dir == DMA_NONE)
  79. return 0;
  80. wa->dma_sg = sg;
  81. wa->dma_dev = dev;
  82. wa->dma_dir = dma_dir;
  83. wa->dma_count = dma_map_sg(dev, sg, wa->nents, dma_dir);
  84. if (!wa->dma_count)
  85. return -ENOMEM;
  86. return 0;
  87. }
  88. static void ccp_update_sg_workarea(struct ccp_sg_workarea *wa, unsigned int len)
  89. {
  90. unsigned int nbytes = min_t(u64, len, wa->bytes_left);
  91. if (!wa->sg)
  92. return;
  93. wa->sg_used += nbytes;
  94. wa->bytes_left -= nbytes;
  95. if (wa->sg_used == wa->sg->length) {
  96. wa->sg = sg_next(wa->sg);
  97. wa->sg_used = 0;
  98. }
  99. }
  100. static void ccp_dm_free(struct ccp_dm_workarea *wa)
  101. {
  102. if (wa->length <= CCP_DMAPOOL_MAX_SIZE) {
  103. if (wa->address)
  104. dma_pool_free(wa->dma_pool, wa->address,
  105. wa->dma.address);
  106. } else {
  107. if (wa->dma.address)
  108. dma_unmap_single(wa->dev, wa->dma.address, wa->length,
  109. wa->dma.dir);
  110. kfree(wa->address);
  111. }
  112. wa->address = NULL;
  113. wa->dma.address = 0;
  114. }
  115. static int ccp_init_dm_workarea(struct ccp_dm_workarea *wa,
  116. struct ccp_cmd_queue *cmd_q,
  117. unsigned int len,
  118. enum dma_data_direction dir)
  119. {
  120. memset(wa, 0, sizeof(*wa));
  121. if (!len)
  122. return 0;
  123. wa->dev = cmd_q->ccp->dev;
  124. wa->length = len;
  125. if (len <= CCP_DMAPOOL_MAX_SIZE) {
  126. wa->dma_pool = cmd_q->dma_pool;
  127. wa->address = dma_pool_alloc(wa->dma_pool, GFP_KERNEL,
  128. &wa->dma.address);
  129. if (!wa->address)
  130. return -ENOMEM;
  131. wa->dma.length = CCP_DMAPOOL_MAX_SIZE;
  132. memset(wa->address, 0, CCP_DMAPOOL_MAX_SIZE);
  133. } else {
  134. wa->address = kzalloc(len, GFP_KERNEL);
  135. if (!wa->address)
  136. return -ENOMEM;
  137. wa->dma.address = dma_map_single(wa->dev, wa->address, len,
  138. dir);
  139. if (!wa->dma.address)
  140. return -ENOMEM;
  141. wa->dma.length = len;
  142. }
  143. wa->dma.dir = dir;
  144. return 0;
  145. }
  146. static void ccp_set_dm_area(struct ccp_dm_workarea *wa, unsigned int wa_offset,
  147. struct scatterlist *sg, unsigned int sg_offset,
  148. unsigned int len)
  149. {
  150. WARN_ON(!wa->address);
  151. scatterwalk_map_and_copy(wa->address + wa_offset, sg, sg_offset, len,
  152. 0);
  153. }
  154. static void ccp_get_dm_area(struct ccp_dm_workarea *wa, unsigned int wa_offset,
  155. struct scatterlist *sg, unsigned int sg_offset,
  156. unsigned int len)
  157. {
  158. WARN_ON(!wa->address);
  159. scatterwalk_map_and_copy(wa->address + wa_offset, sg, sg_offset, len,
  160. 1);
  161. }
  162. static int ccp_reverse_set_dm_area(struct ccp_dm_workarea *wa,
  163. unsigned int wa_offset,
  164. struct scatterlist *sg,
  165. unsigned int sg_offset,
  166. unsigned int len)
  167. {
  168. u8 *p, *q;
  169. ccp_set_dm_area(wa, wa_offset, sg, sg_offset, len);
  170. p = wa->address + wa_offset;
  171. q = p + len - 1;
  172. while (p < q) {
  173. *p = *p ^ *q;
  174. *q = *p ^ *q;
  175. *p = *p ^ *q;
  176. p++;
  177. q--;
  178. }
  179. return 0;
  180. }
  181. static void ccp_reverse_get_dm_area(struct ccp_dm_workarea *wa,
  182. unsigned int wa_offset,
  183. struct scatterlist *sg,
  184. unsigned int sg_offset,
  185. unsigned int len)
  186. {
  187. u8 *p, *q;
  188. p = wa->address + wa_offset;
  189. q = p + len - 1;
  190. while (p < q) {
  191. *p = *p ^ *q;
  192. *q = *p ^ *q;
  193. *p = *p ^ *q;
  194. p++;
  195. q--;
  196. }
  197. ccp_get_dm_area(wa, wa_offset, sg, sg_offset, len);
  198. }
  199. static void ccp_free_data(struct ccp_data *data, struct ccp_cmd_queue *cmd_q)
  200. {
  201. ccp_dm_free(&data->dm_wa);
  202. ccp_sg_free(&data->sg_wa);
  203. }
  204. static int ccp_init_data(struct ccp_data *data, struct ccp_cmd_queue *cmd_q,
  205. struct scatterlist *sg, u64 sg_len,
  206. unsigned int dm_len,
  207. enum dma_data_direction dir)
  208. {
  209. int ret;
  210. memset(data, 0, sizeof(*data));
  211. ret = ccp_init_sg_workarea(&data->sg_wa, cmd_q->ccp->dev, sg, sg_len,
  212. dir);
  213. if (ret)
  214. goto e_err;
  215. ret = ccp_init_dm_workarea(&data->dm_wa, cmd_q, dm_len, dir);
  216. if (ret)
  217. goto e_err;
  218. return 0;
  219. e_err:
  220. ccp_free_data(data, cmd_q);
  221. return ret;
  222. }
  223. static unsigned int ccp_queue_buf(struct ccp_data *data, unsigned int from)
  224. {
  225. struct ccp_sg_workarea *sg_wa = &data->sg_wa;
  226. struct ccp_dm_workarea *dm_wa = &data->dm_wa;
  227. unsigned int buf_count, nbytes;
  228. /* Clear the buffer if setting it */
  229. if (!from)
  230. memset(dm_wa->address, 0, dm_wa->length);
  231. if (!sg_wa->sg)
  232. return 0;
  233. /* Perform the copy operation
  234. * nbytes will always be <= UINT_MAX because dm_wa->length is
  235. * an unsigned int
  236. */
  237. nbytes = min_t(u64, sg_wa->bytes_left, dm_wa->length);
  238. scatterwalk_map_and_copy(dm_wa->address, sg_wa->sg, sg_wa->sg_used,
  239. nbytes, from);
  240. /* Update the structures and generate the count */
  241. buf_count = 0;
  242. while (sg_wa->bytes_left && (buf_count < dm_wa->length)) {
  243. nbytes = min(sg_wa->sg->length - sg_wa->sg_used,
  244. dm_wa->length - buf_count);
  245. nbytes = min_t(u64, sg_wa->bytes_left, nbytes);
  246. buf_count += nbytes;
  247. ccp_update_sg_workarea(sg_wa, nbytes);
  248. }
  249. return buf_count;
  250. }
  251. static unsigned int ccp_fill_queue_buf(struct ccp_data *data)
  252. {
  253. return ccp_queue_buf(data, 0);
  254. }
  255. static unsigned int ccp_empty_queue_buf(struct ccp_data *data)
  256. {
  257. return ccp_queue_buf(data, 1);
  258. }
  259. static void ccp_prepare_data(struct ccp_data *src, struct ccp_data *dst,
  260. struct ccp_op *op, unsigned int block_size,
  261. bool blocksize_op)
  262. {
  263. unsigned int sg_src_len, sg_dst_len, op_len;
  264. /* The CCP can only DMA from/to one address each per operation. This
  265. * requires that we find the smallest DMA area between the source
  266. * and destination. The resulting len values will always be <= UINT_MAX
  267. * because the dma length is an unsigned int.
  268. */
  269. sg_src_len = sg_dma_len(src->sg_wa.sg) - src->sg_wa.sg_used;
  270. sg_src_len = min_t(u64, src->sg_wa.bytes_left, sg_src_len);
  271. if (dst) {
  272. sg_dst_len = sg_dma_len(dst->sg_wa.sg) - dst->sg_wa.sg_used;
  273. sg_dst_len = min_t(u64, src->sg_wa.bytes_left, sg_dst_len);
  274. op_len = min(sg_src_len, sg_dst_len);
  275. } else {
  276. op_len = sg_src_len;
  277. }
  278. /* The data operation length will be at least block_size in length
  279. * or the smaller of available sg room remaining for the source or
  280. * the destination
  281. */
  282. op_len = max(op_len, block_size);
  283. /* Unless we have to buffer data, there's no reason to wait */
  284. op->soc = 0;
  285. if (sg_src_len < block_size) {
  286. /* Not enough data in the sg element, so it
  287. * needs to be buffered into a blocksize chunk
  288. */
  289. int cp_len = ccp_fill_queue_buf(src);
  290. op->soc = 1;
  291. op->src.u.dma.address = src->dm_wa.dma.address;
  292. op->src.u.dma.offset = 0;
  293. op->src.u.dma.length = (blocksize_op) ? block_size : cp_len;
  294. } else {
  295. /* Enough data in the sg element, but we need to
  296. * adjust for any previously copied data
  297. */
  298. op->src.u.dma.address = sg_dma_address(src->sg_wa.sg);
  299. op->src.u.dma.offset = src->sg_wa.sg_used;
  300. op->src.u.dma.length = op_len & ~(block_size - 1);
  301. ccp_update_sg_workarea(&src->sg_wa, op->src.u.dma.length);
  302. }
  303. if (dst) {
  304. if (sg_dst_len < block_size) {
  305. /* Not enough room in the sg element or we're on the
  306. * last piece of data (when using padding), so the
  307. * output needs to be buffered into a blocksize chunk
  308. */
  309. op->soc = 1;
  310. op->dst.u.dma.address = dst->dm_wa.dma.address;
  311. op->dst.u.dma.offset = 0;
  312. op->dst.u.dma.length = op->src.u.dma.length;
  313. } else {
  314. /* Enough room in the sg element, but we need to
  315. * adjust for any previously used area
  316. */
  317. op->dst.u.dma.address = sg_dma_address(dst->sg_wa.sg);
  318. op->dst.u.dma.offset = dst->sg_wa.sg_used;
  319. op->dst.u.dma.length = op->src.u.dma.length;
  320. }
  321. }
  322. }
  323. static void ccp_process_data(struct ccp_data *src, struct ccp_data *dst,
  324. struct ccp_op *op)
  325. {
  326. op->init = 0;
  327. if (dst) {
  328. if (op->dst.u.dma.address == dst->dm_wa.dma.address)
  329. ccp_empty_queue_buf(dst);
  330. else
  331. ccp_update_sg_workarea(&dst->sg_wa,
  332. op->dst.u.dma.length);
  333. }
  334. }
  335. static int ccp_copy_to_from_sb(struct ccp_cmd_queue *cmd_q,
  336. struct ccp_dm_workarea *wa, u32 jobid, u32 sb,
  337. u32 byte_swap, bool from)
  338. {
  339. struct ccp_op op;
  340. memset(&op, 0, sizeof(op));
  341. op.cmd_q = cmd_q;
  342. op.jobid = jobid;
  343. op.eom = 1;
  344. if (from) {
  345. op.soc = 1;
  346. op.src.type = CCP_MEMTYPE_SB;
  347. op.src.u.sb = sb;
  348. op.dst.type = CCP_MEMTYPE_SYSTEM;
  349. op.dst.u.dma.address = wa->dma.address;
  350. op.dst.u.dma.length = wa->length;
  351. } else {
  352. op.src.type = CCP_MEMTYPE_SYSTEM;
  353. op.src.u.dma.address = wa->dma.address;
  354. op.src.u.dma.length = wa->length;
  355. op.dst.type = CCP_MEMTYPE_SB;
  356. op.dst.u.sb = sb;
  357. }
  358. op.u.passthru.byte_swap = byte_swap;
  359. return cmd_q->ccp->vdata->perform->passthru(&op);
  360. }
  361. static int ccp_copy_to_sb(struct ccp_cmd_queue *cmd_q,
  362. struct ccp_dm_workarea *wa, u32 jobid, u32 sb,
  363. u32 byte_swap)
  364. {
  365. return ccp_copy_to_from_sb(cmd_q, wa, jobid, sb, byte_swap, false);
  366. }
  367. static int ccp_copy_from_sb(struct ccp_cmd_queue *cmd_q,
  368. struct ccp_dm_workarea *wa, u32 jobid, u32 sb,
  369. u32 byte_swap)
  370. {
  371. return ccp_copy_to_from_sb(cmd_q, wa, jobid, sb, byte_swap, true);
  372. }
  373. static int ccp_run_aes_cmac_cmd(struct ccp_cmd_queue *cmd_q,
  374. struct ccp_cmd *cmd)
  375. {
  376. struct ccp_aes_engine *aes = &cmd->u.aes;
  377. struct ccp_dm_workarea key, ctx;
  378. struct ccp_data src;
  379. struct ccp_op op;
  380. unsigned int dm_offset;
  381. int ret;
  382. if (!((aes->key_len == AES_KEYSIZE_128) ||
  383. (aes->key_len == AES_KEYSIZE_192) ||
  384. (aes->key_len == AES_KEYSIZE_256)))
  385. return -EINVAL;
  386. if (aes->src_len & (AES_BLOCK_SIZE - 1))
  387. return -EINVAL;
  388. if (aes->iv_len != AES_BLOCK_SIZE)
  389. return -EINVAL;
  390. if (!aes->key || !aes->iv || !aes->src)
  391. return -EINVAL;
  392. if (aes->cmac_final) {
  393. if (aes->cmac_key_len != AES_BLOCK_SIZE)
  394. return -EINVAL;
  395. if (!aes->cmac_key)
  396. return -EINVAL;
  397. }
  398. BUILD_BUG_ON(CCP_AES_KEY_SB_COUNT != 1);
  399. BUILD_BUG_ON(CCP_AES_CTX_SB_COUNT != 1);
  400. ret = -EIO;
  401. memset(&op, 0, sizeof(op));
  402. op.cmd_q = cmd_q;
  403. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  404. op.sb_key = cmd_q->sb_key;
  405. op.sb_ctx = cmd_q->sb_ctx;
  406. op.init = 1;
  407. op.u.aes.type = aes->type;
  408. op.u.aes.mode = aes->mode;
  409. op.u.aes.action = aes->action;
  410. /* All supported key sizes fit in a single (32-byte) SB entry
  411. * and must be in little endian format. Use the 256-bit byte
  412. * swap passthru option to convert from big endian to little
  413. * endian.
  414. */
  415. ret = ccp_init_dm_workarea(&key, cmd_q,
  416. CCP_AES_KEY_SB_COUNT * CCP_SB_BYTES,
  417. DMA_TO_DEVICE);
  418. if (ret)
  419. return ret;
  420. dm_offset = CCP_SB_BYTES - aes->key_len;
  421. ccp_set_dm_area(&key, dm_offset, aes->key, 0, aes->key_len);
  422. ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
  423. CCP_PASSTHRU_BYTESWAP_256BIT);
  424. if (ret) {
  425. cmd->engine_error = cmd_q->cmd_error;
  426. goto e_key;
  427. }
  428. /* The AES context fits in a single (32-byte) SB entry and
  429. * must be in little endian format. Use the 256-bit byte swap
  430. * passthru option to convert from big endian to little endian.
  431. */
  432. ret = ccp_init_dm_workarea(&ctx, cmd_q,
  433. CCP_AES_CTX_SB_COUNT * CCP_SB_BYTES,
  434. DMA_BIDIRECTIONAL);
  435. if (ret)
  436. goto e_key;
  437. dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
  438. ccp_set_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
  439. ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  440. CCP_PASSTHRU_BYTESWAP_256BIT);
  441. if (ret) {
  442. cmd->engine_error = cmd_q->cmd_error;
  443. goto e_ctx;
  444. }
  445. /* Send data to the CCP AES engine */
  446. ret = ccp_init_data(&src, cmd_q, aes->src, aes->src_len,
  447. AES_BLOCK_SIZE, DMA_TO_DEVICE);
  448. if (ret)
  449. goto e_ctx;
  450. while (src.sg_wa.bytes_left) {
  451. ccp_prepare_data(&src, NULL, &op, AES_BLOCK_SIZE, true);
  452. if (aes->cmac_final && !src.sg_wa.bytes_left) {
  453. op.eom = 1;
  454. /* Push the K1/K2 key to the CCP now */
  455. ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid,
  456. op.sb_ctx,
  457. CCP_PASSTHRU_BYTESWAP_256BIT);
  458. if (ret) {
  459. cmd->engine_error = cmd_q->cmd_error;
  460. goto e_src;
  461. }
  462. ccp_set_dm_area(&ctx, 0, aes->cmac_key, 0,
  463. aes->cmac_key_len);
  464. ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  465. CCP_PASSTHRU_BYTESWAP_256BIT);
  466. if (ret) {
  467. cmd->engine_error = cmd_q->cmd_error;
  468. goto e_src;
  469. }
  470. }
  471. ret = cmd_q->ccp->vdata->perform->aes(&op);
  472. if (ret) {
  473. cmd->engine_error = cmd_q->cmd_error;
  474. goto e_src;
  475. }
  476. ccp_process_data(&src, NULL, &op);
  477. }
  478. /* Retrieve the AES context - convert from LE to BE using
  479. * 32-byte (256-bit) byteswapping
  480. */
  481. ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  482. CCP_PASSTHRU_BYTESWAP_256BIT);
  483. if (ret) {
  484. cmd->engine_error = cmd_q->cmd_error;
  485. goto e_src;
  486. }
  487. /* ...but we only need AES_BLOCK_SIZE bytes */
  488. dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
  489. ccp_get_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
  490. e_src:
  491. ccp_free_data(&src, cmd_q);
  492. e_ctx:
  493. ccp_dm_free(&ctx);
  494. e_key:
  495. ccp_dm_free(&key);
  496. return ret;
  497. }
  498. static int ccp_run_aes_gcm_cmd(struct ccp_cmd_queue *cmd_q,
  499. struct ccp_cmd *cmd)
  500. {
  501. struct ccp_aes_engine *aes = &cmd->u.aes;
  502. struct ccp_dm_workarea key, ctx, final_wa, tag;
  503. struct ccp_data src, dst;
  504. struct ccp_data aad;
  505. struct ccp_op op;
  506. unsigned long long *final;
  507. unsigned int dm_offset;
  508. unsigned int ilen;
  509. bool in_place = true; /* Default value */
  510. int ret;
  511. struct scatterlist *p_inp, sg_inp[2];
  512. struct scatterlist *p_tag, sg_tag[2];
  513. struct scatterlist *p_outp, sg_outp[2];
  514. struct scatterlist *p_aad;
  515. if (!aes->iv)
  516. return -EINVAL;
  517. if (!((aes->key_len == AES_KEYSIZE_128) ||
  518. (aes->key_len == AES_KEYSIZE_192) ||
  519. (aes->key_len == AES_KEYSIZE_256)))
  520. return -EINVAL;
  521. if (!aes->key) /* Gotta have a key SGL */
  522. return -EINVAL;
  523. /* First, decompose the source buffer into AAD & PT,
  524. * and the destination buffer into AAD, CT & tag, or
  525. * the input into CT & tag.
  526. * It is expected that the input and output SGs will
  527. * be valid, even if the AAD and input lengths are 0.
  528. */
  529. p_aad = aes->src;
  530. p_inp = scatterwalk_ffwd(sg_inp, aes->src, aes->aad_len);
  531. p_outp = scatterwalk_ffwd(sg_outp, aes->dst, aes->aad_len);
  532. if (aes->action == CCP_AES_ACTION_ENCRYPT) {
  533. ilen = aes->src_len;
  534. p_tag = scatterwalk_ffwd(sg_tag, p_outp, ilen);
  535. } else {
  536. /* Input length for decryption includes tag */
  537. ilen = aes->src_len - AES_BLOCK_SIZE;
  538. p_tag = scatterwalk_ffwd(sg_tag, p_inp, ilen);
  539. }
  540. memset(&op, 0, sizeof(op));
  541. op.cmd_q = cmd_q;
  542. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  543. op.sb_key = cmd_q->sb_key; /* Pre-allocated */
  544. op.sb_ctx = cmd_q->sb_ctx; /* Pre-allocated */
  545. op.init = 1;
  546. op.u.aes.type = aes->type;
  547. /* Copy the key to the LSB */
  548. ret = ccp_init_dm_workarea(&key, cmd_q,
  549. CCP_AES_CTX_SB_COUNT * CCP_SB_BYTES,
  550. DMA_TO_DEVICE);
  551. if (ret)
  552. return ret;
  553. dm_offset = CCP_SB_BYTES - aes->key_len;
  554. ccp_set_dm_area(&key, dm_offset, aes->key, 0, aes->key_len);
  555. ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
  556. CCP_PASSTHRU_BYTESWAP_256BIT);
  557. if (ret) {
  558. cmd->engine_error = cmd_q->cmd_error;
  559. goto e_key;
  560. }
  561. /* Copy the context (IV) to the LSB.
  562. * There is an assumption here that the IV is 96 bits in length, plus
  563. * a nonce of 32 bits. If no IV is present, use a zeroed buffer.
  564. */
  565. ret = ccp_init_dm_workarea(&ctx, cmd_q,
  566. CCP_AES_CTX_SB_COUNT * CCP_SB_BYTES,
  567. DMA_BIDIRECTIONAL);
  568. if (ret)
  569. goto e_key;
  570. dm_offset = CCP_AES_CTX_SB_COUNT * CCP_SB_BYTES - aes->iv_len;
  571. ccp_set_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
  572. ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  573. CCP_PASSTHRU_BYTESWAP_256BIT);
  574. if (ret) {
  575. cmd->engine_error = cmd_q->cmd_error;
  576. goto e_ctx;
  577. }
  578. op.init = 1;
  579. if (aes->aad_len > 0) {
  580. /* Step 1: Run a GHASH over the Additional Authenticated Data */
  581. ret = ccp_init_data(&aad, cmd_q, p_aad, aes->aad_len,
  582. AES_BLOCK_SIZE,
  583. DMA_TO_DEVICE);
  584. if (ret)
  585. goto e_ctx;
  586. op.u.aes.mode = CCP_AES_MODE_GHASH;
  587. op.u.aes.action = CCP_AES_GHASHAAD;
  588. while (aad.sg_wa.bytes_left) {
  589. ccp_prepare_data(&aad, NULL, &op, AES_BLOCK_SIZE, true);
  590. ret = cmd_q->ccp->vdata->perform->aes(&op);
  591. if (ret) {
  592. cmd->engine_error = cmd_q->cmd_error;
  593. goto e_aad;
  594. }
  595. ccp_process_data(&aad, NULL, &op);
  596. op.init = 0;
  597. }
  598. }
  599. op.u.aes.mode = CCP_AES_MODE_GCTR;
  600. op.u.aes.action = aes->action;
  601. if (ilen > 0) {
  602. /* Step 2: Run a GCTR over the plaintext */
  603. in_place = (sg_virt(p_inp) == sg_virt(p_outp)) ? true : false;
  604. ret = ccp_init_data(&src, cmd_q, p_inp, ilen,
  605. AES_BLOCK_SIZE,
  606. in_place ? DMA_BIDIRECTIONAL
  607. : DMA_TO_DEVICE);
  608. if (ret)
  609. goto e_ctx;
  610. if (in_place) {
  611. dst = src;
  612. } else {
  613. ret = ccp_init_data(&dst, cmd_q, p_outp, ilen,
  614. AES_BLOCK_SIZE, DMA_FROM_DEVICE);
  615. if (ret)
  616. goto e_src;
  617. }
  618. op.soc = 0;
  619. op.eom = 0;
  620. op.init = 1;
  621. while (src.sg_wa.bytes_left) {
  622. ccp_prepare_data(&src, &dst, &op, AES_BLOCK_SIZE, true);
  623. if (!src.sg_wa.bytes_left) {
  624. unsigned int nbytes = aes->src_len
  625. % AES_BLOCK_SIZE;
  626. if (nbytes) {
  627. op.eom = 1;
  628. op.u.aes.size = (nbytes * 8) - 1;
  629. }
  630. }
  631. ret = cmd_q->ccp->vdata->perform->aes(&op);
  632. if (ret) {
  633. cmd->engine_error = cmd_q->cmd_error;
  634. goto e_dst;
  635. }
  636. ccp_process_data(&src, &dst, &op);
  637. op.init = 0;
  638. }
  639. }
  640. /* Step 3: Update the IV portion of the context with the original IV */
  641. ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  642. CCP_PASSTHRU_BYTESWAP_256BIT);
  643. if (ret) {
  644. cmd->engine_error = cmd_q->cmd_error;
  645. goto e_dst;
  646. }
  647. ccp_set_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
  648. ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  649. CCP_PASSTHRU_BYTESWAP_256BIT);
  650. if (ret) {
  651. cmd->engine_error = cmd_q->cmd_error;
  652. goto e_dst;
  653. }
  654. /* Step 4: Concatenate the lengths of the AAD and source, and
  655. * hash that 16 byte buffer.
  656. */
  657. ret = ccp_init_dm_workarea(&final_wa, cmd_q, AES_BLOCK_SIZE,
  658. DMA_BIDIRECTIONAL);
  659. if (ret)
  660. goto e_dst;
  661. final = (unsigned long long *) final_wa.address;
  662. final[0] = cpu_to_be64(aes->aad_len * 8);
  663. final[1] = cpu_to_be64(ilen * 8);
  664. op.u.aes.mode = CCP_AES_MODE_GHASH;
  665. op.u.aes.action = CCP_AES_GHASHFINAL;
  666. op.src.type = CCP_MEMTYPE_SYSTEM;
  667. op.src.u.dma.address = final_wa.dma.address;
  668. op.src.u.dma.length = AES_BLOCK_SIZE;
  669. op.dst.type = CCP_MEMTYPE_SYSTEM;
  670. op.dst.u.dma.address = final_wa.dma.address;
  671. op.dst.u.dma.length = AES_BLOCK_SIZE;
  672. op.eom = 1;
  673. op.u.aes.size = 0;
  674. ret = cmd_q->ccp->vdata->perform->aes(&op);
  675. if (ret)
  676. goto e_dst;
  677. if (aes->action == CCP_AES_ACTION_ENCRYPT) {
  678. /* Put the ciphered tag after the ciphertext. */
  679. ccp_get_dm_area(&final_wa, 0, p_tag, 0, AES_BLOCK_SIZE);
  680. } else {
  681. /* Does this ciphered tag match the input? */
  682. ret = ccp_init_dm_workarea(&tag, cmd_q, AES_BLOCK_SIZE,
  683. DMA_BIDIRECTIONAL);
  684. if (ret)
  685. goto e_tag;
  686. ccp_set_dm_area(&tag, 0, p_tag, 0, AES_BLOCK_SIZE);
  687. ret = memcmp(tag.address, final_wa.address, AES_BLOCK_SIZE);
  688. ccp_dm_free(&tag);
  689. }
  690. e_tag:
  691. ccp_dm_free(&final_wa);
  692. e_dst:
  693. if (aes->src_len && !in_place)
  694. ccp_free_data(&dst, cmd_q);
  695. e_src:
  696. if (aes->src_len)
  697. ccp_free_data(&src, cmd_q);
  698. e_aad:
  699. if (aes->aad_len)
  700. ccp_free_data(&aad, cmd_q);
  701. e_ctx:
  702. ccp_dm_free(&ctx);
  703. e_key:
  704. ccp_dm_free(&key);
  705. return ret;
  706. }
  707. static int ccp_run_aes_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  708. {
  709. struct ccp_aes_engine *aes = &cmd->u.aes;
  710. struct ccp_dm_workarea key, ctx;
  711. struct ccp_data src, dst;
  712. struct ccp_op op;
  713. unsigned int dm_offset;
  714. bool in_place = false;
  715. int ret;
  716. if (aes->mode == CCP_AES_MODE_CMAC)
  717. return ccp_run_aes_cmac_cmd(cmd_q, cmd);
  718. if (aes->mode == CCP_AES_MODE_GCM)
  719. return ccp_run_aes_gcm_cmd(cmd_q, cmd);
  720. if (!((aes->key_len == AES_KEYSIZE_128) ||
  721. (aes->key_len == AES_KEYSIZE_192) ||
  722. (aes->key_len == AES_KEYSIZE_256)))
  723. return -EINVAL;
  724. if (((aes->mode == CCP_AES_MODE_ECB) ||
  725. (aes->mode == CCP_AES_MODE_CBC) ||
  726. (aes->mode == CCP_AES_MODE_CFB)) &&
  727. (aes->src_len & (AES_BLOCK_SIZE - 1)))
  728. return -EINVAL;
  729. if (!aes->key || !aes->src || !aes->dst)
  730. return -EINVAL;
  731. if (aes->mode != CCP_AES_MODE_ECB) {
  732. if (aes->iv_len != AES_BLOCK_SIZE)
  733. return -EINVAL;
  734. if (!aes->iv)
  735. return -EINVAL;
  736. }
  737. BUILD_BUG_ON(CCP_AES_KEY_SB_COUNT != 1);
  738. BUILD_BUG_ON(CCP_AES_CTX_SB_COUNT != 1);
  739. ret = -EIO;
  740. memset(&op, 0, sizeof(op));
  741. op.cmd_q = cmd_q;
  742. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  743. op.sb_key = cmd_q->sb_key;
  744. op.sb_ctx = cmd_q->sb_ctx;
  745. op.init = (aes->mode == CCP_AES_MODE_ECB) ? 0 : 1;
  746. op.u.aes.type = aes->type;
  747. op.u.aes.mode = aes->mode;
  748. op.u.aes.action = aes->action;
  749. /* All supported key sizes fit in a single (32-byte) SB entry
  750. * and must be in little endian format. Use the 256-bit byte
  751. * swap passthru option to convert from big endian to little
  752. * endian.
  753. */
  754. ret = ccp_init_dm_workarea(&key, cmd_q,
  755. CCP_AES_KEY_SB_COUNT * CCP_SB_BYTES,
  756. DMA_TO_DEVICE);
  757. if (ret)
  758. return ret;
  759. dm_offset = CCP_SB_BYTES - aes->key_len;
  760. ccp_set_dm_area(&key, dm_offset, aes->key, 0, aes->key_len);
  761. ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
  762. CCP_PASSTHRU_BYTESWAP_256BIT);
  763. if (ret) {
  764. cmd->engine_error = cmd_q->cmd_error;
  765. goto e_key;
  766. }
  767. /* The AES context fits in a single (32-byte) SB entry and
  768. * must be in little endian format. Use the 256-bit byte swap
  769. * passthru option to convert from big endian to little endian.
  770. */
  771. ret = ccp_init_dm_workarea(&ctx, cmd_q,
  772. CCP_AES_CTX_SB_COUNT * CCP_SB_BYTES,
  773. DMA_BIDIRECTIONAL);
  774. if (ret)
  775. goto e_key;
  776. if (aes->mode != CCP_AES_MODE_ECB) {
  777. /* Load the AES context - convert to LE */
  778. dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
  779. ccp_set_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
  780. ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  781. CCP_PASSTHRU_BYTESWAP_256BIT);
  782. if (ret) {
  783. cmd->engine_error = cmd_q->cmd_error;
  784. goto e_ctx;
  785. }
  786. }
  787. switch (aes->mode) {
  788. case CCP_AES_MODE_CFB: /* CFB128 only */
  789. case CCP_AES_MODE_CTR:
  790. op.u.aes.size = AES_BLOCK_SIZE * BITS_PER_BYTE - 1;
  791. break;
  792. default:
  793. op.u.aes.size = 0;
  794. }
  795. /* Prepare the input and output data workareas. For in-place
  796. * operations we need to set the dma direction to BIDIRECTIONAL
  797. * and copy the src workarea to the dst workarea.
  798. */
  799. if (sg_virt(aes->src) == sg_virt(aes->dst))
  800. in_place = true;
  801. ret = ccp_init_data(&src, cmd_q, aes->src, aes->src_len,
  802. AES_BLOCK_SIZE,
  803. in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  804. if (ret)
  805. goto e_ctx;
  806. if (in_place) {
  807. dst = src;
  808. } else {
  809. ret = ccp_init_data(&dst, cmd_q, aes->dst, aes->src_len,
  810. AES_BLOCK_SIZE, DMA_FROM_DEVICE);
  811. if (ret)
  812. goto e_src;
  813. }
  814. /* Send data to the CCP AES engine */
  815. while (src.sg_wa.bytes_left) {
  816. ccp_prepare_data(&src, &dst, &op, AES_BLOCK_SIZE, true);
  817. if (!src.sg_wa.bytes_left) {
  818. op.eom = 1;
  819. /* Since we don't retrieve the AES context in ECB
  820. * mode we have to wait for the operation to complete
  821. * on the last piece of data
  822. */
  823. if (aes->mode == CCP_AES_MODE_ECB)
  824. op.soc = 1;
  825. }
  826. ret = cmd_q->ccp->vdata->perform->aes(&op);
  827. if (ret) {
  828. cmd->engine_error = cmd_q->cmd_error;
  829. goto e_dst;
  830. }
  831. ccp_process_data(&src, &dst, &op);
  832. }
  833. if (aes->mode != CCP_AES_MODE_ECB) {
  834. /* Retrieve the AES context - convert from LE to BE using
  835. * 32-byte (256-bit) byteswapping
  836. */
  837. ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  838. CCP_PASSTHRU_BYTESWAP_256BIT);
  839. if (ret) {
  840. cmd->engine_error = cmd_q->cmd_error;
  841. goto e_dst;
  842. }
  843. /* ...but we only need AES_BLOCK_SIZE bytes */
  844. dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
  845. ccp_get_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
  846. }
  847. e_dst:
  848. if (!in_place)
  849. ccp_free_data(&dst, cmd_q);
  850. e_src:
  851. ccp_free_data(&src, cmd_q);
  852. e_ctx:
  853. ccp_dm_free(&ctx);
  854. e_key:
  855. ccp_dm_free(&key);
  856. return ret;
  857. }
  858. static int ccp_run_xts_aes_cmd(struct ccp_cmd_queue *cmd_q,
  859. struct ccp_cmd *cmd)
  860. {
  861. struct ccp_xts_aes_engine *xts = &cmd->u.xts;
  862. struct ccp_dm_workarea key, ctx;
  863. struct ccp_data src, dst;
  864. struct ccp_op op;
  865. unsigned int unit_size, dm_offset;
  866. bool in_place = false;
  867. unsigned int sb_count;
  868. enum ccp_aes_type aestype;
  869. int ret;
  870. switch (xts->unit_size) {
  871. case CCP_XTS_AES_UNIT_SIZE_16:
  872. unit_size = 16;
  873. break;
  874. case CCP_XTS_AES_UNIT_SIZE_512:
  875. unit_size = 512;
  876. break;
  877. case CCP_XTS_AES_UNIT_SIZE_1024:
  878. unit_size = 1024;
  879. break;
  880. case CCP_XTS_AES_UNIT_SIZE_2048:
  881. unit_size = 2048;
  882. break;
  883. case CCP_XTS_AES_UNIT_SIZE_4096:
  884. unit_size = 4096;
  885. break;
  886. default:
  887. return -EINVAL;
  888. }
  889. if (xts->key_len == AES_KEYSIZE_128)
  890. aestype = CCP_AES_TYPE_128;
  891. else
  892. return -EINVAL;
  893. if (!xts->final && (xts->src_len & (AES_BLOCK_SIZE - 1)))
  894. return -EINVAL;
  895. if (xts->iv_len != AES_BLOCK_SIZE)
  896. return -EINVAL;
  897. if (!xts->key || !xts->iv || !xts->src || !xts->dst)
  898. return -EINVAL;
  899. BUILD_BUG_ON(CCP_XTS_AES_KEY_SB_COUNT != 1);
  900. BUILD_BUG_ON(CCP_XTS_AES_CTX_SB_COUNT != 1);
  901. ret = -EIO;
  902. memset(&op, 0, sizeof(op));
  903. op.cmd_q = cmd_q;
  904. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  905. op.sb_key = cmd_q->sb_key;
  906. op.sb_ctx = cmd_q->sb_ctx;
  907. op.init = 1;
  908. op.u.xts.type = aestype;
  909. op.u.xts.action = xts->action;
  910. op.u.xts.unit_size = xts->unit_size;
  911. /* A version 3 device only supports 128-bit keys, which fits into a
  912. * single SB entry. A version 5 device uses a 512-bit vector, so two
  913. * SB entries.
  914. */
  915. if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0))
  916. sb_count = CCP_XTS_AES_KEY_SB_COUNT;
  917. else
  918. sb_count = CCP5_XTS_AES_KEY_SB_COUNT;
  919. ret = ccp_init_dm_workarea(&key, cmd_q,
  920. sb_count * CCP_SB_BYTES,
  921. DMA_TO_DEVICE);
  922. if (ret)
  923. return ret;
  924. if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0)) {
  925. /* All supported key sizes must be in little endian format.
  926. * Use the 256-bit byte swap passthru option to convert from
  927. * big endian to little endian.
  928. */
  929. dm_offset = CCP_SB_BYTES - AES_KEYSIZE_128;
  930. ccp_set_dm_area(&key, dm_offset, xts->key, 0, xts->key_len);
  931. ccp_set_dm_area(&key, 0, xts->key, xts->key_len, xts->key_len);
  932. } else {
  933. /* Version 5 CCPs use a 512-bit space for the key: each portion
  934. * occupies 256 bits, or one entire slot, and is zero-padded.
  935. */
  936. unsigned int pad;
  937. dm_offset = CCP_SB_BYTES;
  938. pad = dm_offset - xts->key_len;
  939. ccp_set_dm_area(&key, pad, xts->key, 0, xts->key_len);
  940. ccp_set_dm_area(&key, dm_offset + pad, xts->key, xts->key_len,
  941. xts->key_len);
  942. }
  943. ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
  944. CCP_PASSTHRU_BYTESWAP_256BIT);
  945. if (ret) {
  946. cmd->engine_error = cmd_q->cmd_error;
  947. goto e_key;
  948. }
  949. /* The AES context fits in a single (32-byte) SB entry and
  950. * for XTS is already in little endian format so no byte swapping
  951. * is needed.
  952. */
  953. ret = ccp_init_dm_workarea(&ctx, cmd_q,
  954. CCP_XTS_AES_CTX_SB_COUNT * CCP_SB_BYTES,
  955. DMA_BIDIRECTIONAL);
  956. if (ret)
  957. goto e_key;
  958. ccp_set_dm_area(&ctx, 0, xts->iv, 0, xts->iv_len);
  959. ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  960. CCP_PASSTHRU_BYTESWAP_NOOP);
  961. if (ret) {
  962. cmd->engine_error = cmd_q->cmd_error;
  963. goto e_ctx;
  964. }
  965. /* Prepare the input and output data workareas. For in-place
  966. * operations we need to set the dma direction to BIDIRECTIONAL
  967. * and copy the src workarea to the dst workarea.
  968. */
  969. if (sg_virt(xts->src) == sg_virt(xts->dst))
  970. in_place = true;
  971. ret = ccp_init_data(&src, cmd_q, xts->src, xts->src_len,
  972. unit_size,
  973. in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  974. if (ret)
  975. goto e_ctx;
  976. if (in_place) {
  977. dst = src;
  978. } else {
  979. ret = ccp_init_data(&dst, cmd_q, xts->dst, xts->src_len,
  980. unit_size, DMA_FROM_DEVICE);
  981. if (ret)
  982. goto e_src;
  983. }
  984. /* Send data to the CCP AES engine */
  985. while (src.sg_wa.bytes_left) {
  986. ccp_prepare_data(&src, &dst, &op, unit_size, true);
  987. if (!src.sg_wa.bytes_left)
  988. op.eom = 1;
  989. ret = cmd_q->ccp->vdata->perform->xts_aes(&op);
  990. if (ret) {
  991. cmd->engine_error = cmd_q->cmd_error;
  992. goto e_dst;
  993. }
  994. ccp_process_data(&src, &dst, &op);
  995. }
  996. /* Retrieve the AES context - convert from LE to BE using
  997. * 32-byte (256-bit) byteswapping
  998. */
  999. ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  1000. CCP_PASSTHRU_BYTESWAP_256BIT);
  1001. if (ret) {
  1002. cmd->engine_error = cmd_q->cmd_error;
  1003. goto e_dst;
  1004. }
  1005. /* ...but we only need AES_BLOCK_SIZE bytes */
  1006. dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
  1007. ccp_get_dm_area(&ctx, dm_offset, xts->iv, 0, xts->iv_len);
  1008. e_dst:
  1009. if (!in_place)
  1010. ccp_free_data(&dst, cmd_q);
  1011. e_src:
  1012. ccp_free_data(&src, cmd_q);
  1013. e_ctx:
  1014. ccp_dm_free(&ctx);
  1015. e_key:
  1016. ccp_dm_free(&key);
  1017. return ret;
  1018. }
  1019. static int ccp_run_des3_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1020. {
  1021. struct ccp_des3_engine *des3 = &cmd->u.des3;
  1022. struct ccp_dm_workarea key, ctx;
  1023. struct ccp_data src, dst;
  1024. struct ccp_op op;
  1025. unsigned int dm_offset;
  1026. unsigned int len_singlekey;
  1027. bool in_place = false;
  1028. int ret;
  1029. /* Error checks */
  1030. if (!cmd_q->ccp->vdata->perform->des3)
  1031. return -EINVAL;
  1032. if (des3->key_len != DES3_EDE_KEY_SIZE)
  1033. return -EINVAL;
  1034. if (((des3->mode == CCP_DES3_MODE_ECB) ||
  1035. (des3->mode == CCP_DES3_MODE_CBC)) &&
  1036. (des3->src_len & (DES3_EDE_BLOCK_SIZE - 1)))
  1037. return -EINVAL;
  1038. if (!des3->key || !des3->src || !des3->dst)
  1039. return -EINVAL;
  1040. if (des3->mode != CCP_DES3_MODE_ECB) {
  1041. if (des3->iv_len != DES3_EDE_BLOCK_SIZE)
  1042. return -EINVAL;
  1043. if (!des3->iv)
  1044. return -EINVAL;
  1045. }
  1046. ret = -EIO;
  1047. /* Zero out all the fields of the command desc */
  1048. memset(&op, 0, sizeof(op));
  1049. /* Set up the Function field */
  1050. op.cmd_q = cmd_q;
  1051. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  1052. op.sb_key = cmd_q->sb_key;
  1053. op.init = (des3->mode == CCP_DES3_MODE_ECB) ? 0 : 1;
  1054. op.u.des3.type = des3->type;
  1055. op.u.des3.mode = des3->mode;
  1056. op.u.des3.action = des3->action;
  1057. /*
  1058. * All supported key sizes fit in a single (32-byte) KSB entry and
  1059. * (like AES) must be in little endian format. Use the 256-bit byte
  1060. * swap passthru option to convert from big endian to little endian.
  1061. */
  1062. ret = ccp_init_dm_workarea(&key, cmd_q,
  1063. CCP_DES3_KEY_SB_COUNT * CCP_SB_BYTES,
  1064. DMA_TO_DEVICE);
  1065. if (ret)
  1066. return ret;
  1067. /*
  1068. * The contents of the key triplet are in the reverse order of what
  1069. * is required by the engine. Copy the 3 pieces individually to put
  1070. * them where they belong.
  1071. */
  1072. dm_offset = CCP_SB_BYTES - des3->key_len; /* Basic offset */
  1073. len_singlekey = des3->key_len / 3;
  1074. ccp_set_dm_area(&key, dm_offset + 2 * len_singlekey,
  1075. des3->key, 0, len_singlekey);
  1076. ccp_set_dm_area(&key, dm_offset + len_singlekey,
  1077. des3->key, len_singlekey, len_singlekey);
  1078. ccp_set_dm_area(&key, dm_offset,
  1079. des3->key, 2 * len_singlekey, len_singlekey);
  1080. /* Copy the key to the SB */
  1081. ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
  1082. CCP_PASSTHRU_BYTESWAP_256BIT);
  1083. if (ret) {
  1084. cmd->engine_error = cmd_q->cmd_error;
  1085. goto e_key;
  1086. }
  1087. /*
  1088. * The DES3 context fits in a single (32-byte) KSB entry and
  1089. * must be in little endian format. Use the 256-bit byte swap
  1090. * passthru option to convert from big endian to little endian.
  1091. */
  1092. if (des3->mode != CCP_DES3_MODE_ECB) {
  1093. u32 load_mode;
  1094. op.sb_ctx = cmd_q->sb_ctx;
  1095. ret = ccp_init_dm_workarea(&ctx, cmd_q,
  1096. CCP_DES3_CTX_SB_COUNT * CCP_SB_BYTES,
  1097. DMA_BIDIRECTIONAL);
  1098. if (ret)
  1099. goto e_key;
  1100. /* Load the context into the LSB */
  1101. dm_offset = CCP_SB_BYTES - des3->iv_len;
  1102. ccp_set_dm_area(&ctx, dm_offset, des3->iv, 0, des3->iv_len);
  1103. if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0))
  1104. load_mode = CCP_PASSTHRU_BYTESWAP_NOOP;
  1105. else
  1106. load_mode = CCP_PASSTHRU_BYTESWAP_256BIT;
  1107. ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  1108. load_mode);
  1109. if (ret) {
  1110. cmd->engine_error = cmd_q->cmd_error;
  1111. goto e_ctx;
  1112. }
  1113. }
  1114. /*
  1115. * Prepare the input and output data workareas. For in-place
  1116. * operations we need to set the dma direction to BIDIRECTIONAL
  1117. * and copy the src workarea to the dst workarea.
  1118. */
  1119. if (sg_virt(des3->src) == sg_virt(des3->dst))
  1120. in_place = true;
  1121. ret = ccp_init_data(&src, cmd_q, des3->src, des3->src_len,
  1122. DES3_EDE_BLOCK_SIZE,
  1123. in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  1124. if (ret)
  1125. goto e_ctx;
  1126. if (in_place)
  1127. dst = src;
  1128. else {
  1129. ret = ccp_init_data(&dst, cmd_q, des3->dst, des3->src_len,
  1130. DES3_EDE_BLOCK_SIZE, DMA_FROM_DEVICE);
  1131. if (ret)
  1132. goto e_src;
  1133. }
  1134. /* Send data to the CCP DES3 engine */
  1135. while (src.sg_wa.bytes_left) {
  1136. ccp_prepare_data(&src, &dst, &op, DES3_EDE_BLOCK_SIZE, true);
  1137. if (!src.sg_wa.bytes_left) {
  1138. op.eom = 1;
  1139. /* Since we don't retrieve the context in ECB mode
  1140. * we have to wait for the operation to complete
  1141. * on the last piece of data
  1142. */
  1143. op.soc = 0;
  1144. }
  1145. ret = cmd_q->ccp->vdata->perform->des3(&op);
  1146. if (ret) {
  1147. cmd->engine_error = cmd_q->cmd_error;
  1148. goto e_dst;
  1149. }
  1150. ccp_process_data(&src, &dst, &op);
  1151. }
  1152. if (des3->mode != CCP_DES3_MODE_ECB) {
  1153. /* Retrieve the context and make BE */
  1154. ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  1155. CCP_PASSTHRU_BYTESWAP_256BIT);
  1156. if (ret) {
  1157. cmd->engine_error = cmd_q->cmd_error;
  1158. goto e_dst;
  1159. }
  1160. /* ...but we only need the last DES3_EDE_BLOCK_SIZE bytes */
  1161. if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0))
  1162. dm_offset = CCP_SB_BYTES - des3->iv_len;
  1163. else
  1164. dm_offset = 0;
  1165. ccp_get_dm_area(&ctx, dm_offset, des3->iv, 0,
  1166. DES3_EDE_BLOCK_SIZE);
  1167. }
  1168. e_dst:
  1169. if (!in_place)
  1170. ccp_free_data(&dst, cmd_q);
  1171. e_src:
  1172. ccp_free_data(&src, cmd_q);
  1173. e_ctx:
  1174. if (des3->mode != CCP_DES3_MODE_ECB)
  1175. ccp_dm_free(&ctx);
  1176. e_key:
  1177. ccp_dm_free(&key);
  1178. return ret;
  1179. }
  1180. static int ccp_run_sha_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1181. {
  1182. struct ccp_sha_engine *sha = &cmd->u.sha;
  1183. struct ccp_dm_workarea ctx;
  1184. struct ccp_data src;
  1185. struct ccp_op op;
  1186. unsigned int ioffset, ooffset;
  1187. unsigned int digest_size;
  1188. int sb_count;
  1189. const void *init;
  1190. u64 block_size;
  1191. int ctx_size;
  1192. int ret;
  1193. switch (sha->type) {
  1194. case CCP_SHA_TYPE_1:
  1195. if (sha->ctx_len < SHA1_DIGEST_SIZE)
  1196. return -EINVAL;
  1197. block_size = SHA1_BLOCK_SIZE;
  1198. break;
  1199. case CCP_SHA_TYPE_224:
  1200. if (sha->ctx_len < SHA224_DIGEST_SIZE)
  1201. return -EINVAL;
  1202. block_size = SHA224_BLOCK_SIZE;
  1203. break;
  1204. case CCP_SHA_TYPE_256:
  1205. if (sha->ctx_len < SHA256_DIGEST_SIZE)
  1206. return -EINVAL;
  1207. block_size = SHA256_BLOCK_SIZE;
  1208. break;
  1209. case CCP_SHA_TYPE_384:
  1210. if (cmd_q->ccp->vdata->version < CCP_VERSION(4, 0)
  1211. || sha->ctx_len < SHA384_DIGEST_SIZE)
  1212. return -EINVAL;
  1213. block_size = SHA384_BLOCK_SIZE;
  1214. break;
  1215. case CCP_SHA_TYPE_512:
  1216. if (cmd_q->ccp->vdata->version < CCP_VERSION(4, 0)
  1217. || sha->ctx_len < SHA512_DIGEST_SIZE)
  1218. return -EINVAL;
  1219. block_size = SHA512_BLOCK_SIZE;
  1220. break;
  1221. default:
  1222. return -EINVAL;
  1223. }
  1224. if (!sha->ctx)
  1225. return -EINVAL;
  1226. if (!sha->final && (sha->src_len & (block_size - 1)))
  1227. return -EINVAL;
  1228. /* The version 3 device can't handle zero-length input */
  1229. if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0)) {
  1230. if (!sha->src_len) {
  1231. unsigned int digest_len;
  1232. const u8 *sha_zero;
  1233. /* Not final, just return */
  1234. if (!sha->final)
  1235. return 0;
  1236. /* CCP can't do a zero length sha operation so the
  1237. * caller must buffer the data.
  1238. */
  1239. if (sha->msg_bits)
  1240. return -EINVAL;
  1241. /* The CCP cannot perform zero-length sha operations
  1242. * so the caller is required to buffer data for the
  1243. * final operation. However, a sha operation for a
  1244. * message with a total length of zero is valid so
  1245. * known values are required to supply the result.
  1246. */
  1247. switch (sha->type) {
  1248. case CCP_SHA_TYPE_1:
  1249. sha_zero = sha1_zero_message_hash;
  1250. digest_len = SHA1_DIGEST_SIZE;
  1251. break;
  1252. case CCP_SHA_TYPE_224:
  1253. sha_zero = sha224_zero_message_hash;
  1254. digest_len = SHA224_DIGEST_SIZE;
  1255. break;
  1256. case CCP_SHA_TYPE_256:
  1257. sha_zero = sha256_zero_message_hash;
  1258. digest_len = SHA256_DIGEST_SIZE;
  1259. break;
  1260. default:
  1261. return -EINVAL;
  1262. }
  1263. scatterwalk_map_and_copy((void *)sha_zero, sha->ctx, 0,
  1264. digest_len, 1);
  1265. return 0;
  1266. }
  1267. }
  1268. /* Set variables used throughout */
  1269. switch (sha->type) {
  1270. case CCP_SHA_TYPE_1:
  1271. digest_size = SHA1_DIGEST_SIZE;
  1272. init = (void *) ccp_sha1_init;
  1273. ctx_size = SHA1_DIGEST_SIZE;
  1274. sb_count = 1;
  1275. if (cmd_q->ccp->vdata->version != CCP_VERSION(3, 0))
  1276. ooffset = ioffset = CCP_SB_BYTES - SHA1_DIGEST_SIZE;
  1277. else
  1278. ooffset = ioffset = 0;
  1279. break;
  1280. case CCP_SHA_TYPE_224:
  1281. digest_size = SHA224_DIGEST_SIZE;
  1282. init = (void *) ccp_sha224_init;
  1283. ctx_size = SHA256_DIGEST_SIZE;
  1284. sb_count = 1;
  1285. ioffset = 0;
  1286. if (cmd_q->ccp->vdata->version != CCP_VERSION(3, 0))
  1287. ooffset = CCP_SB_BYTES - SHA224_DIGEST_SIZE;
  1288. else
  1289. ooffset = 0;
  1290. break;
  1291. case CCP_SHA_TYPE_256:
  1292. digest_size = SHA256_DIGEST_SIZE;
  1293. init = (void *) ccp_sha256_init;
  1294. ctx_size = SHA256_DIGEST_SIZE;
  1295. sb_count = 1;
  1296. ooffset = ioffset = 0;
  1297. break;
  1298. case CCP_SHA_TYPE_384:
  1299. digest_size = SHA384_DIGEST_SIZE;
  1300. init = (void *) ccp_sha384_init;
  1301. ctx_size = SHA512_DIGEST_SIZE;
  1302. sb_count = 2;
  1303. ioffset = 0;
  1304. ooffset = 2 * CCP_SB_BYTES - SHA384_DIGEST_SIZE;
  1305. break;
  1306. case CCP_SHA_TYPE_512:
  1307. digest_size = SHA512_DIGEST_SIZE;
  1308. init = (void *) ccp_sha512_init;
  1309. ctx_size = SHA512_DIGEST_SIZE;
  1310. sb_count = 2;
  1311. ooffset = ioffset = 0;
  1312. break;
  1313. default:
  1314. ret = -EINVAL;
  1315. goto e_data;
  1316. }
  1317. /* For zero-length plaintext the src pointer is ignored;
  1318. * otherwise both parts must be valid
  1319. */
  1320. if (sha->src_len && !sha->src)
  1321. return -EINVAL;
  1322. memset(&op, 0, sizeof(op));
  1323. op.cmd_q = cmd_q;
  1324. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  1325. op.sb_ctx = cmd_q->sb_ctx; /* Pre-allocated */
  1326. op.u.sha.type = sha->type;
  1327. op.u.sha.msg_bits = sha->msg_bits;
  1328. /* For SHA1/224/256 the context fits in a single (32-byte) SB entry;
  1329. * SHA384/512 require 2 adjacent SB slots, with the right half in the
  1330. * first slot, and the left half in the second. Each portion must then
  1331. * be in little endian format: use the 256-bit byte swap option.
  1332. */
  1333. ret = ccp_init_dm_workarea(&ctx, cmd_q, sb_count * CCP_SB_BYTES,
  1334. DMA_BIDIRECTIONAL);
  1335. if (ret)
  1336. return ret;
  1337. if (sha->first) {
  1338. switch (sha->type) {
  1339. case CCP_SHA_TYPE_1:
  1340. case CCP_SHA_TYPE_224:
  1341. case CCP_SHA_TYPE_256:
  1342. memcpy(ctx.address + ioffset, init, ctx_size);
  1343. break;
  1344. case CCP_SHA_TYPE_384:
  1345. case CCP_SHA_TYPE_512:
  1346. memcpy(ctx.address + ctx_size / 2, init,
  1347. ctx_size / 2);
  1348. memcpy(ctx.address, init + ctx_size / 2,
  1349. ctx_size / 2);
  1350. break;
  1351. default:
  1352. ret = -EINVAL;
  1353. goto e_ctx;
  1354. }
  1355. } else {
  1356. /* Restore the context */
  1357. ccp_set_dm_area(&ctx, 0, sha->ctx, 0,
  1358. sb_count * CCP_SB_BYTES);
  1359. }
  1360. ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  1361. CCP_PASSTHRU_BYTESWAP_256BIT);
  1362. if (ret) {
  1363. cmd->engine_error = cmd_q->cmd_error;
  1364. goto e_ctx;
  1365. }
  1366. if (sha->src) {
  1367. /* Send data to the CCP SHA engine; block_size is set above */
  1368. ret = ccp_init_data(&src, cmd_q, sha->src, sha->src_len,
  1369. block_size, DMA_TO_DEVICE);
  1370. if (ret)
  1371. goto e_ctx;
  1372. while (src.sg_wa.bytes_left) {
  1373. ccp_prepare_data(&src, NULL, &op, block_size, false);
  1374. if (sha->final && !src.sg_wa.bytes_left)
  1375. op.eom = 1;
  1376. ret = cmd_q->ccp->vdata->perform->sha(&op);
  1377. if (ret) {
  1378. cmd->engine_error = cmd_q->cmd_error;
  1379. goto e_data;
  1380. }
  1381. ccp_process_data(&src, NULL, &op);
  1382. }
  1383. } else {
  1384. op.eom = 1;
  1385. ret = cmd_q->ccp->vdata->perform->sha(&op);
  1386. if (ret) {
  1387. cmd->engine_error = cmd_q->cmd_error;
  1388. goto e_data;
  1389. }
  1390. }
  1391. /* Retrieve the SHA context - convert from LE to BE using
  1392. * 32-byte (256-bit) byteswapping to BE
  1393. */
  1394. ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  1395. CCP_PASSTHRU_BYTESWAP_256BIT);
  1396. if (ret) {
  1397. cmd->engine_error = cmd_q->cmd_error;
  1398. goto e_data;
  1399. }
  1400. if (sha->final) {
  1401. /* Finishing up, so get the digest */
  1402. switch (sha->type) {
  1403. case CCP_SHA_TYPE_1:
  1404. case CCP_SHA_TYPE_224:
  1405. case CCP_SHA_TYPE_256:
  1406. ccp_get_dm_area(&ctx, ooffset,
  1407. sha->ctx, 0,
  1408. digest_size);
  1409. break;
  1410. case CCP_SHA_TYPE_384:
  1411. case CCP_SHA_TYPE_512:
  1412. ccp_get_dm_area(&ctx, 0,
  1413. sha->ctx, LSB_ITEM_SIZE - ooffset,
  1414. LSB_ITEM_SIZE);
  1415. ccp_get_dm_area(&ctx, LSB_ITEM_SIZE + ooffset,
  1416. sha->ctx, 0,
  1417. LSB_ITEM_SIZE - ooffset);
  1418. break;
  1419. default:
  1420. ret = -EINVAL;
  1421. goto e_ctx;
  1422. }
  1423. } else {
  1424. /* Stash the context */
  1425. ccp_get_dm_area(&ctx, 0, sha->ctx, 0,
  1426. sb_count * CCP_SB_BYTES);
  1427. }
  1428. if (sha->final && sha->opad) {
  1429. /* HMAC operation, recursively perform final SHA */
  1430. struct ccp_cmd hmac_cmd;
  1431. struct scatterlist sg;
  1432. u8 *hmac_buf;
  1433. if (sha->opad_len != block_size) {
  1434. ret = -EINVAL;
  1435. goto e_data;
  1436. }
  1437. hmac_buf = kmalloc(block_size + digest_size, GFP_KERNEL);
  1438. if (!hmac_buf) {
  1439. ret = -ENOMEM;
  1440. goto e_data;
  1441. }
  1442. sg_init_one(&sg, hmac_buf, block_size + digest_size);
  1443. scatterwalk_map_and_copy(hmac_buf, sha->opad, 0, block_size, 0);
  1444. switch (sha->type) {
  1445. case CCP_SHA_TYPE_1:
  1446. case CCP_SHA_TYPE_224:
  1447. case CCP_SHA_TYPE_256:
  1448. memcpy(hmac_buf + block_size,
  1449. ctx.address + ooffset,
  1450. digest_size);
  1451. break;
  1452. case CCP_SHA_TYPE_384:
  1453. case CCP_SHA_TYPE_512:
  1454. memcpy(hmac_buf + block_size,
  1455. ctx.address + LSB_ITEM_SIZE + ooffset,
  1456. LSB_ITEM_SIZE);
  1457. memcpy(hmac_buf + block_size +
  1458. (LSB_ITEM_SIZE - ooffset),
  1459. ctx.address,
  1460. LSB_ITEM_SIZE);
  1461. break;
  1462. default:
  1463. ret = -EINVAL;
  1464. goto e_ctx;
  1465. }
  1466. memset(&hmac_cmd, 0, sizeof(hmac_cmd));
  1467. hmac_cmd.engine = CCP_ENGINE_SHA;
  1468. hmac_cmd.u.sha.type = sha->type;
  1469. hmac_cmd.u.sha.ctx = sha->ctx;
  1470. hmac_cmd.u.sha.ctx_len = sha->ctx_len;
  1471. hmac_cmd.u.sha.src = &sg;
  1472. hmac_cmd.u.sha.src_len = block_size + digest_size;
  1473. hmac_cmd.u.sha.opad = NULL;
  1474. hmac_cmd.u.sha.opad_len = 0;
  1475. hmac_cmd.u.sha.first = 1;
  1476. hmac_cmd.u.sha.final = 1;
  1477. hmac_cmd.u.sha.msg_bits = (block_size + digest_size) << 3;
  1478. ret = ccp_run_sha_cmd(cmd_q, &hmac_cmd);
  1479. if (ret)
  1480. cmd->engine_error = hmac_cmd.engine_error;
  1481. kfree(hmac_buf);
  1482. }
  1483. e_data:
  1484. if (sha->src)
  1485. ccp_free_data(&src, cmd_q);
  1486. e_ctx:
  1487. ccp_dm_free(&ctx);
  1488. return ret;
  1489. }
  1490. static int ccp_run_rsa_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1491. {
  1492. struct ccp_rsa_engine *rsa = &cmd->u.rsa;
  1493. struct ccp_dm_workarea exp, src, dst;
  1494. struct ccp_op op;
  1495. unsigned int sb_count, i_len, o_len;
  1496. int ret;
  1497. /* Check against the maximum allowable size, in bits */
  1498. if (rsa->key_size > cmd_q->ccp->vdata->rsamax)
  1499. return -EINVAL;
  1500. if (!rsa->exp || !rsa->mod || !rsa->src || !rsa->dst)
  1501. return -EINVAL;
  1502. memset(&op, 0, sizeof(op));
  1503. op.cmd_q = cmd_q;
  1504. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  1505. /* The RSA modulus must precede the message being acted upon, so
  1506. * it must be copied to a DMA area where the message and the
  1507. * modulus can be concatenated. Therefore the input buffer
  1508. * length required is twice the output buffer length (which
  1509. * must be a multiple of 256-bits). Compute o_len, i_len in bytes.
  1510. * Buffer sizes must be a multiple of 32 bytes; rounding up may be
  1511. * required.
  1512. */
  1513. o_len = 32 * ((rsa->key_size + 255) / 256);
  1514. i_len = o_len * 2;
  1515. if (cmd_q->ccp->vdata->version < CCP_VERSION(5, 0)) {
  1516. /* sb_count is the number of storage block slots required
  1517. * for the modulus.
  1518. */
  1519. sb_count = o_len / CCP_SB_BYTES;
  1520. op.sb_key = cmd_q->ccp->vdata->perform->sballoc(cmd_q,
  1521. sb_count);
  1522. if (!op.sb_key)
  1523. return -EIO;
  1524. } else {
  1525. /* A version 5 device allows a modulus size that will not fit
  1526. * in the LSB, so the command will transfer it from memory.
  1527. * Set the sb key to the default, even though it's not used.
  1528. */
  1529. op.sb_key = cmd_q->sb_key;
  1530. }
  1531. /* The RSA exponent must be in little endian format. Reverse its
  1532. * byte order.
  1533. */
  1534. ret = ccp_init_dm_workarea(&exp, cmd_q, o_len, DMA_TO_DEVICE);
  1535. if (ret)
  1536. goto e_sb;
  1537. ret = ccp_reverse_set_dm_area(&exp, 0, rsa->exp, 0, rsa->exp_len);
  1538. if (ret)
  1539. goto e_exp;
  1540. if (cmd_q->ccp->vdata->version < CCP_VERSION(5, 0)) {
  1541. /* Copy the exponent to the local storage block, using
  1542. * as many 32-byte blocks as were allocated above. It's
  1543. * already little endian, so no further change is required.
  1544. */
  1545. ret = ccp_copy_to_sb(cmd_q, &exp, op.jobid, op.sb_key,
  1546. CCP_PASSTHRU_BYTESWAP_NOOP);
  1547. if (ret) {
  1548. cmd->engine_error = cmd_q->cmd_error;
  1549. goto e_exp;
  1550. }
  1551. } else {
  1552. /* The exponent can be retrieved from memory via DMA. */
  1553. op.exp.u.dma.address = exp.dma.address;
  1554. op.exp.u.dma.offset = 0;
  1555. }
  1556. /* Concatenate the modulus and the message. Both the modulus and
  1557. * the operands must be in little endian format. Since the input
  1558. * is in big endian format it must be converted.
  1559. */
  1560. ret = ccp_init_dm_workarea(&src, cmd_q, i_len, DMA_TO_DEVICE);
  1561. if (ret)
  1562. goto e_exp;
  1563. ret = ccp_reverse_set_dm_area(&src, 0, rsa->mod, 0, rsa->mod_len);
  1564. if (ret)
  1565. goto e_src;
  1566. ret = ccp_reverse_set_dm_area(&src, o_len, rsa->src, 0, rsa->src_len);
  1567. if (ret)
  1568. goto e_src;
  1569. /* Prepare the output area for the operation */
  1570. ret = ccp_init_dm_workarea(&dst, cmd_q, o_len, DMA_FROM_DEVICE);
  1571. if (ret)
  1572. goto e_src;
  1573. op.soc = 1;
  1574. op.src.u.dma.address = src.dma.address;
  1575. op.src.u.dma.offset = 0;
  1576. op.src.u.dma.length = i_len;
  1577. op.dst.u.dma.address = dst.dma.address;
  1578. op.dst.u.dma.offset = 0;
  1579. op.dst.u.dma.length = o_len;
  1580. op.u.rsa.mod_size = rsa->key_size;
  1581. op.u.rsa.input_len = i_len;
  1582. ret = cmd_q->ccp->vdata->perform->rsa(&op);
  1583. if (ret) {
  1584. cmd->engine_error = cmd_q->cmd_error;
  1585. goto e_dst;
  1586. }
  1587. ccp_reverse_get_dm_area(&dst, 0, rsa->dst, 0, rsa->mod_len);
  1588. e_dst:
  1589. ccp_dm_free(&dst);
  1590. e_src:
  1591. ccp_dm_free(&src);
  1592. e_exp:
  1593. ccp_dm_free(&exp);
  1594. e_sb:
  1595. if (cmd_q->ccp->vdata->version < CCP_VERSION(5, 0))
  1596. cmd_q->ccp->vdata->perform->sbfree(cmd_q, op.sb_key, sb_count);
  1597. return ret;
  1598. }
  1599. static int ccp_run_passthru_cmd(struct ccp_cmd_queue *cmd_q,
  1600. struct ccp_cmd *cmd)
  1601. {
  1602. struct ccp_passthru_engine *pt = &cmd->u.passthru;
  1603. struct ccp_dm_workarea mask;
  1604. struct ccp_data src, dst;
  1605. struct ccp_op op;
  1606. bool in_place = false;
  1607. unsigned int i;
  1608. int ret = 0;
  1609. if (!pt->final && (pt->src_len & (CCP_PASSTHRU_BLOCKSIZE - 1)))
  1610. return -EINVAL;
  1611. if (!pt->src || !pt->dst)
  1612. return -EINVAL;
  1613. if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
  1614. if (pt->mask_len != CCP_PASSTHRU_MASKSIZE)
  1615. return -EINVAL;
  1616. if (!pt->mask)
  1617. return -EINVAL;
  1618. }
  1619. BUILD_BUG_ON(CCP_PASSTHRU_SB_COUNT != 1);
  1620. memset(&op, 0, sizeof(op));
  1621. op.cmd_q = cmd_q;
  1622. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  1623. if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
  1624. /* Load the mask */
  1625. op.sb_key = cmd_q->sb_key;
  1626. ret = ccp_init_dm_workarea(&mask, cmd_q,
  1627. CCP_PASSTHRU_SB_COUNT *
  1628. CCP_SB_BYTES,
  1629. DMA_TO_DEVICE);
  1630. if (ret)
  1631. return ret;
  1632. ccp_set_dm_area(&mask, 0, pt->mask, 0, pt->mask_len);
  1633. ret = ccp_copy_to_sb(cmd_q, &mask, op.jobid, op.sb_key,
  1634. CCP_PASSTHRU_BYTESWAP_NOOP);
  1635. if (ret) {
  1636. cmd->engine_error = cmd_q->cmd_error;
  1637. goto e_mask;
  1638. }
  1639. }
  1640. /* Prepare the input and output data workareas. For in-place
  1641. * operations we need to set the dma direction to BIDIRECTIONAL
  1642. * and copy the src workarea to the dst workarea.
  1643. */
  1644. if (sg_virt(pt->src) == sg_virt(pt->dst))
  1645. in_place = true;
  1646. ret = ccp_init_data(&src, cmd_q, pt->src, pt->src_len,
  1647. CCP_PASSTHRU_MASKSIZE,
  1648. in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  1649. if (ret)
  1650. goto e_mask;
  1651. if (in_place) {
  1652. dst = src;
  1653. } else {
  1654. ret = ccp_init_data(&dst, cmd_q, pt->dst, pt->src_len,
  1655. CCP_PASSTHRU_MASKSIZE, DMA_FROM_DEVICE);
  1656. if (ret)
  1657. goto e_src;
  1658. }
  1659. /* Send data to the CCP Passthru engine
  1660. * Because the CCP engine works on a single source and destination
  1661. * dma address at a time, each entry in the source scatterlist
  1662. * (after the dma_map_sg call) must be less than or equal to the
  1663. * (remaining) length in the destination scatterlist entry and the
  1664. * length must be a multiple of CCP_PASSTHRU_BLOCKSIZE
  1665. */
  1666. dst.sg_wa.sg_used = 0;
  1667. for (i = 1; i <= src.sg_wa.dma_count; i++) {
  1668. if (!dst.sg_wa.sg ||
  1669. (dst.sg_wa.sg->length < src.sg_wa.sg->length)) {
  1670. ret = -EINVAL;
  1671. goto e_dst;
  1672. }
  1673. if (i == src.sg_wa.dma_count) {
  1674. op.eom = 1;
  1675. op.soc = 1;
  1676. }
  1677. op.src.type = CCP_MEMTYPE_SYSTEM;
  1678. op.src.u.dma.address = sg_dma_address(src.sg_wa.sg);
  1679. op.src.u.dma.offset = 0;
  1680. op.src.u.dma.length = sg_dma_len(src.sg_wa.sg);
  1681. op.dst.type = CCP_MEMTYPE_SYSTEM;
  1682. op.dst.u.dma.address = sg_dma_address(dst.sg_wa.sg);
  1683. op.dst.u.dma.offset = dst.sg_wa.sg_used;
  1684. op.dst.u.dma.length = op.src.u.dma.length;
  1685. ret = cmd_q->ccp->vdata->perform->passthru(&op);
  1686. if (ret) {
  1687. cmd->engine_error = cmd_q->cmd_error;
  1688. goto e_dst;
  1689. }
  1690. dst.sg_wa.sg_used += src.sg_wa.sg->length;
  1691. if (dst.sg_wa.sg_used == dst.sg_wa.sg->length) {
  1692. dst.sg_wa.sg = sg_next(dst.sg_wa.sg);
  1693. dst.sg_wa.sg_used = 0;
  1694. }
  1695. src.sg_wa.sg = sg_next(src.sg_wa.sg);
  1696. }
  1697. e_dst:
  1698. if (!in_place)
  1699. ccp_free_data(&dst, cmd_q);
  1700. e_src:
  1701. ccp_free_data(&src, cmd_q);
  1702. e_mask:
  1703. if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP)
  1704. ccp_dm_free(&mask);
  1705. return ret;
  1706. }
  1707. static int ccp_run_passthru_nomap_cmd(struct ccp_cmd_queue *cmd_q,
  1708. struct ccp_cmd *cmd)
  1709. {
  1710. struct ccp_passthru_nomap_engine *pt = &cmd->u.passthru_nomap;
  1711. struct ccp_dm_workarea mask;
  1712. struct ccp_op op;
  1713. int ret;
  1714. if (!pt->final && (pt->src_len & (CCP_PASSTHRU_BLOCKSIZE - 1)))
  1715. return -EINVAL;
  1716. if (!pt->src_dma || !pt->dst_dma)
  1717. return -EINVAL;
  1718. if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
  1719. if (pt->mask_len != CCP_PASSTHRU_MASKSIZE)
  1720. return -EINVAL;
  1721. if (!pt->mask)
  1722. return -EINVAL;
  1723. }
  1724. BUILD_BUG_ON(CCP_PASSTHRU_SB_COUNT != 1);
  1725. memset(&op, 0, sizeof(op));
  1726. op.cmd_q = cmd_q;
  1727. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  1728. if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
  1729. /* Load the mask */
  1730. op.sb_key = cmd_q->sb_key;
  1731. mask.length = pt->mask_len;
  1732. mask.dma.address = pt->mask;
  1733. mask.dma.length = pt->mask_len;
  1734. ret = ccp_copy_to_sb(cmd_q, &mask, op.jobid, op.sb_key,
  1735. CCP_PASSTHRU_BYTESWAP_NOOP);
  1736. if (ret) {
  1737. cmd->engine_error = cmd_q->cmd_error;
  1738. return ret;
  1739. }
  1740. }
  1741. /* Send data to the CCP Passthru engine */
  1742. op.eom = 1;
  1743. op.soc = 1;
  1744. op.src.type = CCP_MEMTYPE_SYSTEM;
  1745. op.src.u.dma.address = pt->src_dma;
  1746. op.src.u.dma.offset = 0;
  1747. op.src.u.dma.length = pt->src_len;
  1748. op.dst.type = CCP_MEMTYPE_SYSTEM;
  1749. op.dst.u.dma.address = pt->dst_dma;
  1750. op.dst.u.dma.offset = 0;
  1751. op.dst.u.dma.length = pt->src_len;
  1752. ret = cmd_q->ccp->vdata->perform->passthru(&op);
  1753. if (ret)
  1754. cmd->engine_error = cmd_q->cmd_error;
  1755. return ret;
  1756. }
  1757. static int ccp_run_ecc_mm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1758. {
  1759. struct ccp_ecc_engine *ecc = &cmd->u.ecc;
  1760. struct ccp_dm_workarea src, dst;
  1761. struct ccp_op op;
  1762. int ret;
  1763. u8 *save;
  1764. if (!ecc->u.mm.operand_1 ||
  1765. (ecc->u.mm.operand_1_len > CCP_ECC_MODULUS_BYTES))
  1766. return -EINVAL;
  1767. if (ecc->function != CCP_ECC_FUNCTION_MINV_384BIT)
  1768. if (!ecc->u.mm.operand_2 ||
  1769. (ecc->u.mm.operand_2_len > CCP_ECC_MODULUS_BYTES))
  1770. return -EINVAL;
  1771. if (!ecc->u.mm.result ||
  1772. (ecc->u.mm.result_len < CCP_ECC_MODULUS_BYTES))
  1773. return -EINVAL;
  1774. memset(&op, 0, sizeof(op));
  1775. op.cmd_q = cmd_q;
  1776. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  1777. /* Concatenate the modulus and the operands. Both the modulus and
  1778. * the operands must be in little endian format. Since the input
  1779. * is in big endian format it must be converted and placed in a
  1780. * fixed length buffer.
  1781. */
  1782. ret = ccp_init_dm_workarea(&src, cmd_q, CCP_ECC_SRC_BUF_SIZE,
  1783. DMA_TO_DEVICE);
  1784. if (ret)
  1785. return ret;
  1786. /* Save the workarea address since it is updated in order to perform
  1787. * the concatenation
  1788. */
  1789. save = src.address;
  1790. /* Copy the ECC modulus */
  1791. ret = ccp_reverse_set_dm_area(&src, 0, ecc->mod, 0, ecc->mod_len);
  1792. if (ret)
  1793. goto e_src;
  1794. src.address += CCP_ECC_OPERAND_SIZE;
  1795. /* Copy the first operand */
  1796. ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.mm.operand_1, 0,
  1797. ecc->u.mm.operand_1_len);
  1798. if (ret)
  1799. goto e_src;
  1800. src.address += CCP_ECC_OPERAND_SIZE;
  1801. if (ecc->function != CCP_ECC_FUNCTION_MINV_384BIT) {
  1802. /* Copy the second operand */
  1803. ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.mm.operand_2, 0,
  1804. ecc->u.mm.operand_2_len);
  1805. if (ret)
  1806. goto e_src;
  1807. src.address += CCP_ECC_OPERAND_SIZE;
  1808. }
  1809. /* Restore the workarea address */
  1810. src.address = save;
  1811. /* Prepare the output area for the operation */
  1812. ret = ccp_init_dm_workarea(&dst, cmd_q, CCP_ECC_DST_BUF_SIZE,
  1813. DMA_FROM_DEVICE);
  1814. if (ret)
  1815. goto e_src;
  1816. op.soc = 1;
  1817. op.src.u.dma.address = src.dma.address;
  1818. op.src.u.dma.offset = 0;
  1819. op.src.u.dma.length = src.length;
  1820. op.dst.u.dma.address = dst.dma.address;
  1821. op.dst.u.dma.offset = 0;
  1822. op.dst.u.dma.length = dst.length;
  1823. op.u.ecc.function = cmd->u.ecc.function;
  1824. ret = cmd_q->ccp->vdata->perform->ecc(&op);
  1825. if (ret) {
  1826. cmd->engine_error = cmd_q->cmd_error;
  1827. goto e_dst;
  1828. }
  1829. ecc->ecc_result = le16_to_cpup(
  1830. (const __le16 *)(dst.address + CCP_ECC_RESULT_OFFSET));
  1831. if (!(ecc->ecc_result & CCP_ECC_RESULT_SUCCESS)) {
  1832. ret = -EIO;
  1833. goto e_dst;
  1834. }
  1835. /* Save the ECC result */
  1836. ccp_reverse_get_dm_area(&dst, 0, ecc->u.mm.result, 0,
  1837. CCP_ECC_MODULUS_BYTES);
  1838. e_dst:
  1839. ccp_dm_free(&dst);
  1840. e_src:
  1841. ccp_dm_free(&src);
  1842. return ret;
  1843. }
  1844. static int ccp_run_ecc_pm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1845. {
  1846. struct ccp_ecc_engine *ecc = &cmd->u.ecc;
  1847. struct ccp_dm_workarea src, dst;
  1848. struct ccp_op op;
  1849. int ret;
  1850. u8 *save;
  1851. if (!ecc->u.pm.point_1.x ||
  1852. (ecc->u.pm.point_1.x_len > CCP_ECC_MODULUS_BYTES) ||
  1853. !ecc->u.pm.point_1.y ||
  1854. (ecc->u.pm.point_1.y_len > CCP_ECC_MODULUS_BYTES))
  1855. return -EINVAL;
  1856. if (ecc->function == CCP_ECC_FUNCTION_PADD_384BIT) {
  1857. if (!ecc->u.pm.point_2.x ||
  1858. (ecc->u.pm.point_2.x_len > CCP_ECC_MODULUS_BYTES) ||
  1859. !ecc->u.pm.point_2.y ||
  1860. (ecc->u.pm.point_2.y_len > CCP_ECC_MODULUS_BYTES))
  1861. return -EINVAL;
  1862. } else {
  1863. if (!ecc->u.pm.domain_a ||
  1864. (ecc->u.pm.domain_a_len > CCP_ECC_MODULUS_BYTES))
  1865. return -EINVAL;
  1866. if (ecc->function == CCP_ECC_FUNCTION_PMUL_384BIT)
  1867. if (!ecc->u.pm.scalar ||
  1868. (ecc->u.pm.scalar_len > CCP_ECC_MODULUS_BYTES))
  1869. return -EINVAL;
  1870. }
  1871. if (!ecc->u.pm.result.x ||
  1872. (ecc->u.pm.result.x_len < CCP_ECC_MODULUS_BYTES) ||
  1873. !ecc->u.pm.result.y ||
  1874. (ecc->u.pm.result.y_len < CCP_ECC_MODULUS_BYTES))
  1875. return -EINVAL;
  1876. memset(&op, 0, sizeof(op));
  1877. op.cmd_q = cmd_q;
  1878. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  1879. /* Concatenate the modulus and the operands. Both the modulus and
  1880. * the operands must be in little endian format. Since the input
  1881. * is in big endian format it must be converted and placed in a
  1882. * fixed length buffer.
  1883. */
  1884. ret = ccp_init_dm_workarea(&src, cmd_q, CCP_ECC_SRC_BUF_SIZE,
  1885. DMA_TO_DEVICE);
  1886. if (ret)
  1887. return ret;
  1888. /* Save the workarea address since it is updated in order to perform
  1889. * the concatenation
  1890. */
  1891. save = src.address;
  1892. /* Copy the ECC modulus */
  1893. ret = ccp_reverse_set_dm_area(&src, 0, ecc->mod, 0, ecc->mod_len);
  1894. if (ret)
  1895. goto e_src;
  1896. src.address += CCP_ECC_OPERAND_SIZE;
  1897. /* Copy the first point X and Y coordinate */
  1898. ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.point_1.x, 0,
  1899. ecc->u.pm.point_1.x_len);
  1900. if (ret)
  1901. goto e_src;
  1902. src.address += CCP_ECC_OPERAND_SIZE;
  1903. ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.point_1.y, 0,
  1904. ecc->u.pm.point_1.y_len);
  1905. if (ret)
  1906. goto e_src;
  1907. src.address += CCP_ECC_OPERAND_SIZE;
  1908. /* Set the first point Z coordinate to 1 */
  1909. *src.address = 0x01;
  1910. src.address += CCP_ECC_OPERAND_SIZE;
  1911. if (ecc->function == CCP_ECC_FUNCTION_PADD_384BIT) {
  1912. /* Copy the second point X and Y coordinate */
  1913. ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.point_2.x, 0,
  1914. ecc->u.pm.point_2.x_len);
  1915. if (ret)
  1916. goto e_src;
  1917. src.address += CCP_ECC_OPERAND_SIZE;
  1918. ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.point_2.y, 0,
  1919. ecc->u.pm.point_2.y_len);
  1920. if (ret)
  1921. goto e_src;
  1922. src.address += CCP_ECC_OPERAND_SIZE;
  1923. /* Set the second point Z coordinate to 1 */
  1924. *src.address = 0x01;
  1925. src.address += CCP_ECC_OPERAND_SIZE;
  1926. } else {
  1927. /* Copy the Domain "a" parameter */
  1928. ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.domain_a, 0,
  1929. ecc->u.pm.domain_a_len);
  1930. if (ret)
  1931. goto e_src;
  1932. src.address += CCP_ECC_OPERAND_SIZE;
  1933. if (ecc->function == CCP_ECC_FUNCTION_PMUL_384BIT) {
  1934. /* Copy the scalar value */
  1935. ret = ccp_reverse_set_dm_area(&src, 0,
  1936. ecc->u.pm.scalar, 0,
  1937. ecc->u.pm.scalar_len);
  1938. if (ret)
  1939. goto e_src;
  1940. src.address += CCP_ECC_OPERAND_SIZE;
  1941. }
  1942. }
  1943. /* Restore the workarea address */
  1944. src.address = save;
  1945. /* Prepare the output area for the operation */
  1946. ret = ccp_init_dm_workarea(&dst, cmd_q, CCP_ECC_DST_BUF_SIZE,
  1947. DMA_FROM_DEVICE);
  1948. if (ret)
  1949. goto e_src;
  1950. op.soc = 1;
  1951. op.src.u.dma.address = src.dma.address;
  1952. op.src.u.dma.offset = 0;
  1953. op.src.u.dma.length = src.length;
  1954. op.dst.u.dma.address = dst.dma.address;
  1955. op.dst.u.dma.offset = 0;
  1956. op.dst.u.dma.length = dst.length;
  1957. op.u.ecc.function = cmd->u.ecc.function;
  1958. ret = cmd_q->ccp->vdata->perform->ecc(&op);
  1959. if (ret) {
  1960. cmd->engine_error = cmd_q->cmd_error;
  1961. goto e_dst;
  1962. }
  1963. ecc->ecc_result = le16_to_cpup(
  1964. (const __le16 *)(dst.address + CCP_ECC_RESULT_OFFSET));
  1965. if (!(ecc->ecc_result & CCP_ECC_RESULT_SUCCESS)) {
  1966. ret = -EIO;
  1967. goto e_dst;
  1968. }
  1969. /* Save the workarea address since it is updated as we walk through
  1970. * to copy the point math result
  1971. */
  1972. save = dst.address;
  1973. /* Save the ECC result X and Y coordinates */
  1974. ccp_reverse_get_dm_area(&dst, 0, ecc->u.pm.result.x, 0,
  1975. CCP_ECC_MODULUS_BYTES);
  1976. dst.address += CCP_ECC_OUTPUT_SIZE;
  1977. ccp_reverse_get_dm_area(&dst, 0, ecc->u.pm.result.y, 0,
  1978. CCP_ECC_MODULUS_BYTES);
  1979. dst.address += CCP_ECC_OUTPUT_SIZE;
  1980. /* Restore the workarea address */
  1981. dst.address = save;
  1982. e_dst:
  1983. ccp_dm_free(&dst);
  1984. e_src:
  1985. ccp_dm_free(&src);
  1986. return ret;
  1987. }
  1988. static int ccp_run_ecc_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1989. {
  1990. struct ccp_ecc_engine *ecc = &cmd->u.ecc;
  1991. ecc->ecc_result = 0;
  1992. if (!ecc->mod ||
  1993. (ecc->mod_len > CCP_ECC_MODULUS_BYTES))
  1994. return -EINVAL;
  1995. switch (ecc->function) {
  1996. case CCP_ECC_FUNCTION_MMUL_384BIT:
  1997. case CCP_ECC_FUNCTION_MADD_384BIT:
  1998. case CCP_ECC_FUNCTION_MINV_384BIT:
  1999. return ccp_run_ecc_mm_cmd(cmd_q, cmd);
  2000. case CCP_ECC_FUNCTION_PADD_384BIT:
  2001. case CCP_ECC_FUNCTION_PMUL_384BIT:
  2002. case CCP_ECC_FUNCTION_PDBL_384BIT:
  2003. return ccp_run_ecc_pm_cmd(cmd_q, cmd);
  2004. default:
  2005. return -EINVAL;
  2006. }
  2007. }
  2008. int ccp_run_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  2009. {
  2010. int ret;
  2011. cmd->engine_error = 0;
  2012. cmd_q->cmd_error = 0;
  2013. cmd_q->int_rcvd = 0;
  2014. cmd_q->free_slots = cmd_q->ccp->vdata->perform->get_free_slots(cmd_q);
  2015. switch (cmd->engine) {
  2016. case CCP_ENGINE_AES:
  2017. ret = ccp_run_aes_cmd(cmd_q, cmd);
  2018. break;
  2019. case CCP_ENGINE_XTS_AES_128:
  2020. ret = ccp_run_xts_aes_cmd(cmd_q, cmd);
  2021. break;
  2022. case CCP_ENGINE_DES3:
  2023. ret = ccp_run_des3_cmd(cmd_q, cmd);
  2024. break;
  2025. case CCP_ENGINE_SHA:
  2026. ret = ccp_run_sha_cmd(cmd_q, cmd);
  2027. break;
  2028. case CCP_ENGINE_RSA:
  2029. ret = ccp_run_rsa_cmd(cmd_q, cmd);
  2030. break;
  2031. case CCP_ENGINE_PASSTHRU:
  2032. if (cmd->flags & CCP_CMD_PASSTHRU_NO_DMA_MAP)
  2033. ret = ccp_run_passthru_nomap_cmd(cmd_q, cmd);
  2034. else
  2035. ret = ccp_run_passthru_cmd(cmd_q, cmd);
  2036. break;
  2037. case CCP_ENGINE_ECC:
  2038. ret = ccp_run_ecc_cmd(cmd_q, cmd);
  2039. break;
  2040. default:
  2041. ret = -EINVAL;
  2042. }
  2043. return ret;
  2044. }