ccp-ops.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876
  1. /*
  2. * AMD Cryptographic Coprocessor (CCP) driver
  3. *
  4. * Copyright (C) 2013,2016 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 <linux/ccp.h>
  19. #include "ccp-dev.h"
  20. /* SHA initial context values */
  21. static const __be32 ccp_sha1_init[SHA1_DIGEST_SIZE / sizeof(__be32)] = {
  22. cpu_to_be32(SHA1_H0), cpu_to_be32(SHA1_H1),
  23. cpu_to_be32(SHA1_H2), cpu_to_be32(SHA1_H3),
  24. cpu_to_be32(SHA1_H4),
  25. };
  26. static const __be32 ccp_sha224_init[SHA256_DIGEST_SIZE / sizeof(__be32)] = {
  27. cpu_to_be32(SHA224_H0), cpu_to_be32(SHA224_H1),
  28. cpu_to_be32(SHA224_H2), cpu_to_be32(SHA224_H3),
  29. cpu_to_be32(SHA224_H4), cpu_to_be32(SHA224_H5),
  30. cpu_to_be32(SHA224_H6), cpu_to_be32(SHA224_H7),
  31. };
  32. static const __be32 ccp_sha256_init[SHA256_DIGEST_SIZE / sizeof(__be32)] = {
  33. cpu_to_be32(SHA256_H0), cpu_to_be32(SHA256_H1),
  34. cpu_to_be32(SHA256_H2), cpu_to_be32(SHA256_H3),
  35. cpu_to_be32(SHA256_H4), cpu_to_be32(SHA256_H5),
  36. cpu_to_be32(SHA256_H6), cpu_to_be32(SHA256_H7),
  37. };
  38. #define CCP_NEW_JOBID(ccp) ((ccp->vdata->version == CCP_VERSION(3, 0)) ? \
  39. ccp_gen_jobid(ccp) : 0)
  40. static u32 ccp_gen_jobid(struct ccp_device *ccp)
  41. {
  42. return atomic_inc_return(&ccp->current_id) & CCP_JOBID_MASK;
  43. }
  44. static void ccp_sg_free(struct ccp_sg_workarea *wa)
  45. {
  46. if (wa->dma_count)
  47. dma_unmap_sg(wa->dma_dev, wa->dma_sg, wa->nents, wa->dma_dir);
  48. wa->dma_count = 0;
  49. }
  50. static int ccp_init_sg_workarea(struct ccp_sg_workarea *wa, struct device *dev,
  51. struct scatterlist *sg, u64 len,
  52. enum dma_data_direction dma_dir)
  53. {
  54. memset(wa, 0, sizeof(*wa));
  55. wa->sg = sg;
  56. if (!sg)
  57. return 0;
  58. wa->nents = sg_nents_for_len(sg, len);
  59. if (wa->nents < 0)
  60. return wa->nents;
  61. wa->bytes_left = len;
  62. wa->sg_used = 0;
  63. if (len == 0)
  64. return 0;
  65. if (dma_dir == DMA_NONE)
  66. return 0;
  67. wa->dma_sg = sg;
  68. wa->dma_dev = dev;
  69. wa->dma_dir = dma_dir;
  70. wa->dma_count = dma_map_sg(dev, sg, wa->nents, dma_dir);
  71. if (!wa->dma_count)
  72. return -ENOMEM;
  73. return 0;
  74. }
  75. static void ccp_update_sg_workarea(struct ccp_sg_workarea *wa, unsigned int len)
  76. {
  77. unsigned int nbytes = min_t(u64, len, wa->bytes_left);
  78. if (!wa->sg)
  79. return;
  80. wa->sg_used += nbytes;
  81. wa->bytes_left -= nbytes;
  82. if (wa->sg_used == wa->sg->length) {
  83. wa->sg = sg_next(wa->sg);
  84. wa->sg_used = 0;
  85. }
  86. }
  87. static void ccp_dm_free(struct ccp_dm_workarea *wa)
  88. {
  89. if (wa->length <= CCP_DMAPOOL_MAX_SIZE) {
  90. if (wa->address)
  91. dma_pool_free(wa->dma_pool, wa->address,
  92. wa->dma.address);
  93. } else {
  94. if (wa->dma.address)
  95. dma_unmap_single(wa->dev, wa->dma.address, wa->length,
  96. wa->dma.dir);
  97. kfree(wa->address);
  98. }
  99. wa->address = NULL;
  100. wa->dma.address = 0;
  101. }
  102. static int ccp_init_dm_workarea(struct ccp_dm_workarea *wa,
  103. struct ccp_cmd_queue *cmd_q,
  104. unsigned int len,
  105. enum dma_data_direction dir)
  106. {
  107. memset(wa, 0, sizeof(*wa));
  108. if (!len)
  109. return 0;
  110. wa->dev = cmd_q->ccp->dev;
  111. wa->length = len;
  112. if (len <= CCP_DMAPOOL_MAX_SIZE) {
  113. wa->dma_pool = cmd_q->dma_pool;
  114. wa->address = dma_pool_alloc(wa->dma_pool, GFP_KERNEL,
  115. &wa->dma.address);
  116. if (!wa->address)
  117. return -ENOMEM;
  118. wa->dma.length = CCP_DMAPOOL_MAX_SIZE;
  119. memset(wa->address, 0, CCP_DMAPOOL_MAX_SIZE);
  120. } else {
  121. wa->address = kzalloc(len, GFP_KERNEL);
  122. if (!wa->address)
  123. return -ENOMEM;
  124. wa->dma.address = dma_map_single(wa->dev, wa->address, len,
  125. dir);
  126. if (!wa->dma.address)
  127. return -ENOMEM;
  128. wa->dma.length = len;
  129. }
  130. wa->dma.dir = dir;
  131. return 0;
  132. }
  133. static void ccp_set_dm_area(struct ccp_dm_workarea *wa, unsigned int wa_offset,
  134. struct scatterlist *sg, unsigned int sg_offset,
  135. unsigned int len)
  136. {
  137. WARN_ON(!wa->address);
  138. scatterwalk_map_and_copy(wa->address + wa_offset, sg, sg_offset, len,
  139. 0);
  140. }
  141. static void ccp_get_dm_area(struct ccp_dm_workarea *wa, unsigned int wa_offset,
  142. struct scatterlist *sg, unsigned int sg_offset,
  143. unsigned int len)
  144. {
  145. WARN_ON(!wa->address);
  146. scatterwalk_map_and_copy(wa->address + wa_offset, sg, sg_offset, len,
  147. 1);
  148. }
  149. static int ccp_reverse_set_dm_area(struct ccp_dm_workarea *wa,
  150. struct scatterlist *sg,
  151. unsigned int len, unsigned int se_len,
  152. bool sign_extend)
  153. {
  154. unsigned int nbytes, sg_offset, dm_offset, sb_len, i;
  155. u8 buffer[CCP_REVERSE_BUF_SIZE];
  156. if (WARN_ON(se_len > sizeof(buffer)))
  157. return -EINVAL;
  158. sg_offset = len;
  159. dm_offset = 0;
  160. nbytes = len;
  161. while (nbytes) {
  162. sb_len = min_t(unsigned int, nbytes, se_len);
  163. sg_offset -= sb_len;
  164. scatterwalk_map_and_copy(buffer, sg, sg_offset, sb_len, 0);
  165. for (i = 0; i < sb_len; i++)
  166. wa->address[dm_offset + i] = buffer[sb_len - i - 1];
  167. dm_offset += sb_len;
  168. nbytes -= sb_len;
  169. if ((sb_len != se_len) && sign_extend) {
  170. /* Must sign-extend to nearest sign-extend length */
  171. if (wa->address[dm_offset - 1] & 0x80)
  172. memset(wa->address + dm_offset, 0xff,
  173. se_len - sb_len);
  174. }
  175. }
  176. return 0;
  177. }
  178. static void ccp_reverse_get_dm_area(struct ccp_dm_workarea *wa,
  179. struct scatterlist *sg,
  180. unsigned int len)
  181. {
  182. unsigned int nbytes, sg_offset, dm_offset, sb_len, i;
  183. u8 buffer[CCP_REVERSE_BUF_SIZE];
  184. sg_offset = 0;
  185. dm_offset = len;
  186. nbytes = len;
  187. while (nbytes) {
  188. sb_len = min_t(unsigned int, nbytes, sizeof(buffer));
  189. dm_offset -= sb_len;
  190. for (i = 0; i < sb_len; i++)
  191. buffer[sb_len - i - 1] = wa->address[dm_offset + i];
  192. scatterwalk_map_and_copy(buffer, sg, sg_offset, sb_len, 1);
  193. sg_offset += sb_len;
  194. nbytes -= sb_len;
  195. }
  196. }
  197. static void ccp_free_data(struct ccp_data *data, struct ccp_cmd_queue *cmd_q)
  198. {
  199. ccp_dm_free(&data->dm_wa);
  200. ccp_sg_free(&data->sg_wa);
  201. }
  202. static int ccp_init_data(struct ccp_data *data, struct ccp_cmd_queue *cmd_q,
  203. struct scatterlist *sg, u64 sg_len,
  204. unsigned int dm_len,
  205. enum dma_data_direction dir)
  206. {
  207. int ret;
  208. memset(data, 0, sizeof(*data));
  209. ret = ccp_init_sg_workarea(&data->sg_wa, cmd_q->ccp->dev, sg, sg_len,
  210. dir);
  211. if (ret)
  212. goto e_err;
  213. ret = ccp_init_dm_workarea(&data->dm_wa, cmd_q, dm_len, dir);
  214. if (ret)
  215. goto e_err;
  216. return 0;
  217. e_err:
  218. ccp_free_data(data, cmd_q);
  219. return ret;
  220. }
  221. static unsigned int ccp_queue_buf(struct ccp_data *data, unsigned int from)
  222. {
  223. struct ccp_sg_workarea *sg_wa = &data->sg_wa;
  224. struct ccp_dm_workarea *dm_wa = &data->dm_wa;
  225. unsigned int buf_count, nbytes;
  226. /* Clear the buffer if setting it */
  227. if (!from)
  228. memset(dm_wa->address, 0, dm_wa->length);
  229. if (!sg_wa->sg)
  230. return 0;
  231. /* Perform the copy operation
  232. * nbytes will always be <= UINT_MAX because dm_wa->length is
  233. * an unsigned int
  234. */
  235. nbytes = min_t(u64, sg_wa->bytes_left, dm_wa->length);
  236. scatterwalk_map_and_copy(dm_wa->address, sg_wa->sg, sg_wa->sg_used,
  237. nbytes, from);
  238. /* Update the structures and generate the count */
  239. buf_count = 0;
  240. while (sg_wa->bytes_left && (buf_count < dm_wa->length)) {
  241. nbytes = min(sg_wa->sg->length - sg_wa->sg_used,
  242. dm_wa->length - buf_count);
  243. nbytes = min_t(u64, sg_wa->bytes_left, nbytes);
  244. buf_count += nbytes;
  245. ccp_update_sg_workarea(sg_wa, nbytes);
  246. }
  247. return buf_count;
  248. }
  249. static unsigned int ccp_fill_queue_buf(struct ccp_data *data)
  250. {
  251. return ccp_queue_buf(data, 0);
  252. }
  253. static unsigned int ccp_empty_queue_buf(struct ccp_data *data)
  254. {
  255. return ccp_queue_buf(data, 1);
  256. }
  257. static void ccp_prepare_data(struct ccp_data *src, struct ccp_data *dst,
  258. struct ccp_op *op, unsigned int block_size,
  259. bool blocksize_op)
  260. {
  261. unsigned int sg_src_len, sg_dst_len, op_len;
  262. /* The CCP can only DMA from/to one address each per operation. This
  263. * requires that we find the smallest DMA area between the source
  264. * and destination. The resulting len values will always be <= UINT_MAX
  265. * because the dma length is an unsigned int.
  266. */
  267. sg_src_len = sg_dma_len(src->sg_wa.sg) - src->sg_wa.sg_used;
  268. sg_src_len = min_t(u64, src->sg_wa.bytes_left, sg_src_len);
  269. if (dst) {
  270. sg_dst_len = sg_dma_len(dst->sg_wa.sg) - dst->sg_wa.sg_used;
  271. sg_dst_len = min_t(u64, src->sg_wa.bytes_left, sg_dst_len);
  272. op_len = min(sg_src_len, sg_dst_len);
  273. } else {
  274. op_len = sg_src_len;
  275. }
  276. /* The data operation length will be at least block_size in length
  277. * or the smaller of available sg room remaining for the source or
  278. * the destination
  279. */
  280. op_len = max(op_len, block_size);
  281. /* Unless we have to buffer data, there's no reason to wait */
  282. op->soc = 0;
  283. if (sg_src_len < block_size) {
  284. /* Not enough data in the sg element, so it
  285. * needs to be buffered into a blocksize chunk
  286. */
  287. int cp_len = ccp_fill_queue_buf(src);
  288. op->soc = 1;
  289. op->src.u.dma.address = src->dm_wa.dma.address;
  290. op->src.u.dma.offset = 0;
  291. op->src.u.dma.length = (blocksize_op) ? block_size : cp_len;
  292. } else {
  293. /* Enough data in the sg element, but we need to
  294. * adjust for any previously copied data
  295. */
  296. op->src.u.dma.address = sg_dma_address(src->sg_wa.sg);
  297. op->src.u.dma.offset = src->sg_wa.sg_used;
  298. op->src.u.dma.length = op_len & ~(block_size - 1);
  299. ccp_update_sg_workarea(&src->sg_wa, op->src.u.dma.length);
  300. }
  301. if (dst) {
  302. if (sg_dst_len < block_size) {
  303. /* Not enough room in the sg element or we're on the
  304. * last piece of data (when using padding), so the
  305. * output needs to be buffered into a blocksize chunk
  306. */
  307. op->soc = 1;
  308. op->dst.u.dma.address = dst->dm_wa.dma.address;
  309. op->dst.u.dma.offset = 0;
  310. op->dst.u.dma.length = op->src.u.dma.length;
  311. } else {
  312. /* Enough room in the sg element, but we need to
  313. * adjust for any previously used area
  314. */
  315. op->dst.u.dma.address = sg_dma_address(dst->sg_wa.sg);
  316. op->dst.u.dma.offset = dst->sg_wa.sg_used;
  317. op->dst.u.dma.length = op->src.u.dma.length;
  318. }
  319. }
  320. }
  321. static void ccp_process_data(struct ccp_data *src, struct ccp_data *dst,
  322. struct ccp_op *op)
  323. {
  324. op->init = 0;
  325. if (dst) {
  326. if (op->dst.u.dma.address == dst->dm_wa.dma.address)
  327. ccp_empty_queue_buf(dst);
  328. else
  329. ccp_update_sg_workarea(&dst->sg_wa,
  330. op->dst.u.dma.length);
  331. }
  332. }
  333. static int ccp_copy_to_from_sb(struct ccp_cmd_queue *cmd_q,
  334. struct ccp_dm_workarea *wa, u32 jobid, u32 sb,
  335. u32 byte_swap, bool from)
  336. {
  337. struct ccp_op op;
  338. memset(&op, 0, sizeof(op));
  339. op.cmd_q = cmd_q;
  340. op.jobid = jobid;
  341. op.eom = 1;
  342. if (from) {
  343. op.soc = 1;
  344. op.src.type = CCP_MEMTYPE_SB;
  345. op.src.u.sb = sb;
  346. op.dst.type = CCP_MEMTYPE_SYSTEM;
  347. op.dst.u.dma.address = wa->dma.address;
  348. op.dst.u.dma.length = wa->length;
  349. } else {
  350. op.src.type = CCP_MEMTYPE_SYSTEM;
  351. op.src.u.dma.address = wa->dma.address;
  352. op.src.u.dma.length = wa->length;
  353. op.dst.type = CCP_MEMTYPE_SB;
  354. op.dst.u.sb = sb;
  355. }
  356. op.u.passthru.byte_swap = byte_swap;
  357. return cmd_q->ccp->vdata->perform->passthru(&op);
  358. }
  359. static int ccp_copy_to_sb(struct ccp_cmd_queue *cmd_q,
  360. struct ccp_dm_workarea *wa, u32 jobid, u32 sb,
  361. u32 byte_swap)
  362. {
  363. return ccp_copy_to_from_sb(cmd_q, wa, jobid, sb, byte_swap, false);
  364. }
  365. static int ccp_copy_from_sb(struct ccp_cmd_queue *cmd_q,
  366. struct ccp_dm_workarea *wa, u32 jobid, u32 sb,
  367. u32 byte_swap)
  368. {
  369. return ccp_copy_to_from_sb(cmd_q, wa, jobid, sb, byte_swap, true);
  370. }
  371. static int ccp_run_aes_cmac_cmd(struct ccp_cmd_queue *cmd_q,
  372. struct ccp_cmd *cmd)
  373. {
  374. struct ccp_aes_engine *aes = &cmd->u.aes;
  375. struct ccp_dm_workarea key, ctx;
  376. struct ccp_data src;
  377. struct ccp_op op;
  378. unsigned int dm_offset;
  379. int ret;
  380. if (!((aes->key_len == AES_KEYSIZE_128) ||
  381. (aes->key_len == AES_KEYSIZE_192) ||
  382. (aes->key_len == AES_KEYSIZE_256)))
  383. return -EINVAL;
  384. if (aes->src_len & (AES_BLOCK_SIZE - 1))
  385. return -EINVAL;
  386. if (aes->iv_len != AES_BLOCK_SIZE)
  387. return -EINVAL;
  388. if (!aes->key || !aes->iv || !aes->src)
  389. return -EINVAL;
  390. if (aes->cmac_final) {
  391. if (aes->cmac_key_len != AES_BLOCK_SIZE)
  392. return -EINVAL;
  393. if (!aes->cmac_key)
  394. return -EINVAL;
  395. }
  396. BUILD_BUG_ON(CCP_AES_KEY_SB_COUNT != 1);
  397. BUILD_BUG_ON(CCP_AES_CTX_SB_COUNT != 1);
  398. ret = -EIO;
  399. memset(&op, 0, sizeof(op));
  400. op.cmd_q = cmd_q;
  401. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  402. op.sb_key = cmd_q->sb_key;
  403. op.sb_ctx = cmd_q->sb_ctx;
  404. op.init = 1;
  405. op.u.aes.type = aes->type;
  406. op.u.aes.mode = aes->mode;
  407. op.u.aes.action = aes->action;
  408. /* All supported key sizes fit in a single (32-byte) SB entry
  409. * and must be in little endian format. Use the 256-bit byte
  410. * swap passthru option to convert from big endian to little
  411. * endian.
  412. */
  413. ret = ccp_init_dm_workarea(&key, cmd_q,
  414. CCP_AES_KEY_SB_COUNT * CCP_SB_BYTES,
  415. DMA_TO_DEVICE);
  416. if (ret)
  417. return ret;
  418. dm_offset = CCP_SB_BYTES - aes->key_len;
  419. ccp_set_dm_area(&key, dm_offset, aes->key, 0, aes->key_len);
  420. ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
  421. CCP_PASSTHRU_BYTESWAP_256BIT);
  422. if (ret) {
  423. cmd->engine_error = cmd_q->cmd_error;
  424. goto e_key;
  425. }
  426. /* The AES context fits in a single (32-byte) SB entry and
  427. * must be in little endian format. Use the 256-bit byte swap
  428. * passthru option to convert from big endian to little endian.
  429. */
  430. ret = ccp_init_dm_workarea(&ctx, cmd_q,
  431. CCP_AES_CTX_SB_COUNT * CCP_SB_BYTES,
  432. DMA_BIDIRECTIONAL);
  433. if (ret)
  434. goto e_key;
  435. dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
  436. ccp_set_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
  437. ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  438. CCP_PASSTHRU_BYTESWAP_256BIT);
  439. if (ret) {
  440. cmd->engine_error = cmd_q->cmd_error;
  441. goto e_ctx;
  442. }
  443. /* Send data to the CCP AES engine */
  444. ret = ccp_init_data(&src, cmd_q, aes->src, aes->src_len,
  445. AES_BLOCK_SIZE, DMA_TO_DEVICE);
  446. if (ret)
  447. goto e_ctx;
  448. while (src.sg_wa.bytes_left) {
  449. ccp_prepare_data(&src, NULL, &op, AES_BLOCK_SIZE, true);
  450. if (aes->cmac_final && !src.sg_wa.bytes_left) {
  451. op.eom = 1;
  452. /* Push the K1/K2 key to the CCP now */
  453. ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid,
  454. op.sb_ctx,
  455. CCP_PASSTHRU_BYTESWAP_256BIT);
  456. if (ret) {
  457. cmd->engine_error = cmd_q->cmd_error;
  458. goto e_src;
  459. }
  460. ccp_set_dm_area(&ctx, 0, aes->cmac_key, 0,
  461. aes->cmac_key_len);
  462. ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  463. CCP_PASSTHRU_BYTESWAP_256BIT);
  464. if (ret) {
  465. cmd->engine_error = cmd_q->cmd_error;
  466. goto e_src;
  467. }
  468. }
  469. ret = cmd_q->ccp->vdata->perform->aes(&op);
  470. if (ret) {
  471. cmd->engine_error = cmd_q->cmd_error;
  472. goto e_src;
  473. }
  474. ccp_process_data(&src, NULL, &op);
  475. }
  476. /* Retrieve the AES context - convert from LE to BE using
  477. * 32-byte (256-bit) byteswapping
  478. */
  479. ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  480. CCP_PASSTHRU_BYTESWAP_256BIT);
  481. if (ret) {
  482. cmd->engine_error = cmd_q->cmd_error;
  483. goto e_src;
  484. }
  485. /* ...but we only need AES_BLOCK_SIZE bytes */
  486. dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
  487. ccp_get_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
  488. e_src:
  489. ccp_free_data(&src, cmd_q);
  490. e_ctx:
  491. ccp_dm_free(&ctx);
  492. e_key:
  493. ccp_dm_free(&key);
  494. return ret;
  495. }
  496. static int ccp_run_aes_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  497. {
  498. struct ccp_aes_engine *aes = &cmd->u.aes;
  499. struct ccp_dm_workarea key, ctx;
  500. struct ccp_data src, dst;
  501. struct ccp_op op;
  502. unsigned int dm_offset;
  503. bool in_place = false;
  504. int ret;
  505. if (aes->mode == CCP_AES_MODE_CMAC)
  506. return ccp_run_aes_cmac_cmd(cmd_q, cmd);
  507. if (!((aes->key_len == AES_KEYSIZE_128) ||
  508. (aes->key_len == AES_KEYSIZE_192) ||
  509. (aes->key_len == AES_KEYSIZE_256)))
  510. return -EINVAL;
  511. if (((aes->mode == CCP_AES_MODE_ECB) ||
  512. (aes->mode == CCP_AES_MODE_CBC) ||
  513. (aes->mode == CCP_AES_MODE_CFB)) &&
  514. (aes->src_len & (AES_BLOCK_SIZE - 1)))
  515. return -EINVAL;
  516. if (!aes->key || !aes->src || !aes->dst)
  517. return -EINVAL;
  518. if (aes->mode != CCP_AES_MODE_ECB) {
  519. if (aes->iv_len != AES_BLOCK_SIZE)
  520. return -EINVAL;
  521. if (!aes->iv)
  522. return -EINVAL;
  523. }
  524. BUILD_BUG_ON(CCP_AES_KEY_SB_COUNT != 1);
  525. BUILD_BUG_ON(CCP_AES_CTX_SB_COUNT != 1);
  526. ret = -EIO;
  527. memset(&op, 0, sizeof(op));
  528. op.cmd_q = cmd_q;
  529. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  530. op.sb_key = cmd_q->sb_key;
  531. op.sb_ctx = cmd_q->sb_ctx;
  532. op.init = (aes->mode == CCP_AES_MODE_ECB) ? 0 : 1;
  533. op.u.aes.type = aes->type;
  534. op.u.aes.mode = aes->mode;
  535. op.u.aes.action = aes->action;
  536. /* All supported key sizes fit in a single (32-byte) SB entry
  537. * and must be in little endian format. Use the 256-bit byte
  538. * swap passthru option to convert from big endian to little
  539. * endian.
  540. */
  541. ret = ccp_init_dm_workarea(&key, cmd_q,
  542. CCP_AES_KEY_SB_COUNT * CCP_SB_BYTES,
  543. DMA_TO_DEVICE);
  544. if (ret)
  545. return ret;
  546. dm_offset = CCP_SB_BYTES - aes->key_len;
  547. ccp_set_dm_area(&key, dm_offset, aes->key, 0, aes->key_len);
  548. ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
  549. CCP_PASSTHRU_BYTESWAP_256BIT);
  550. if (ret) {
  551. cmd->engine_error = cmd_q->cmd_error;
  552. goto e_key;
  553. }
  554. /* The AES context fits in a single (32-byte) SB entry and
  555. * must be in little endian format. Use the 256-bit byte swap
  556. * passthru option to convert from big endian to little endian.
  557. */
  558. ret = ccp_init_dm_workarea(&ctx, cmd_q,
  559. CCP_AES_CTX_SB_COUNT * CCP_SB_BYTES,
  560. DMA_BIDIRECTIONAL);
  561. if (ret)
  562. goto e_key;
  563. if (aes->mode != CCP_AES_MODE_ECB) {
  564. /* Load the AES context - convert to LE */
  565. dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
  566. ccp_set_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
  567. ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  568. CCP_PASSTHRU_BYTESWAP_256BIT);
  569. if (ret) {
  570. cmd->engine_error = cmd_q->cmd_error;
  571. goto e_ctx;
  572. }
  573. }
  574. /* Prepare the input and output data workareas. For in-place
  575. * operations we need to set the dma direction to BIDIRECTIONAL
  576. * and copy the src workarea to the dst workarea.
  577. */
  578. if (sg_virt(aes->src) == sg_virt(aes->dst))
  579. in_place = true;
  580. ret = ccp_init_data(&src, cmd_q, aes->src, aes->src_len,
  581. AES_BLOCK_SIZE,
  582. in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  583. if (ret)
  584. goto e_ctx;
  585. if (in_place) {
  586. dst = src;
  587. } else {
  588. ret = ccp_init_data(&dst, cmd_q, aes->dst, aes->src_len,
  589. AES_BLOCK_SIZE, DMA_FROM_DEVICE);
  590. if (ret)
  591. goto e_src;
  592. }
  593. /* Send data to the CCP AES engine */
  594. while (src.sg_wa.bytes_left) {
  595. ccp_prepare_data(&src, &dst, &op, AES_BLOCK_SIZE, true);
  596. if (!src.sg_wa.bytes_left) {
  597. op.eom = 1;
  598. /* Since we don't retrieve the AES context in ECB
  599. * mode we have to wait for the operation to complete
  600. * on the last piece of data
  601. */
  602. if (aes->mode == CCP_AES_MODE_ECB)
  603. op.soc = 1;
  604. }
  605. ret = cmd_q->ccp->vdata->perform->aes(&op);
  606. if (ret) {
  607. cmd->engine_error = cmd_q->cmd_error;
  608. goto e_dst;
  609. }
  610. ccp_process_data(&src, &dst, &op);
  611. }
  612. if (aes->mode != CCP_AES_MODE_ECB) {
  613. /* Retrieve the AES context - convert from LE to BE using
  614. * 32-byte (256-bit) byteswapping
  615. */
  616. ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  617. CCP_PASSTHRU_BYTESWAP_256BIT);
  618. if (ret) {
  619. cmd->engine_error = cmd_q->cmd_error;
  620. goto e_dst;
  621. }
  622. /* ...but we only need AES_BLOCK_SIZE bytes */
  623. dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
  624. ccp_get_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
  625. }
  626. e_dst:
  627. if (!in_place)
  628. ccp_free_data(&dst, cmd_q);
  629. e_src:
  630. ccp_free_data(&src, cmd_q);
  631. e_ctx:
  632. ccp_dm_free(&ctx);
  633. e_key:
  634. ccp_dm_free(&key);
  635. return ret;
  636. }
  637. static int ccp_run_xts_aes_cmd(struct ccp_cmd_queue *cmd_q,
  638. struct ccp_cmd *cmd)
  639. {
  640. struct ccp_xts_aes_engine *xts = &cmd->u.xts;
  641. struct ccp_dm_workarea key, ctx;
  642. struct ccp_data src, dst;
  643. struct ccp_op op;
  644. unsigned int unit_size, dm_offset;
  645. bool in_place = false;
  646. int ret;
  647. switch (xts->unit_size) {
  648. case CCP_XTS_AES_UNIT_SIZE_16:
  649. unit_size = 16;
  650. break;
  651. case CCP_XTS_AES_UNIT_SIZE_512:
  652. unit_size = 512;
  653. break;
  654. case CCP_XTS_AES_UNIT_SIZE_1024:
  655. unit_size = 1024;
  656. break;
  657. case CCP_XTS_AES_UNIT_SIZE_2048:
  658. unit_size = 2048;
  659. break;
  660. case CCP_XTS_AES_UNIT_SIZE_4096:
  661. unit_size = 4096;
  662. break;
  663. default:
  664. return -EINVAL;
  665. }
  666. if (xts->key_len != AES_KEYSIZE_128)
  667. return -EINVAL;
  668. if (!xts->final && (xts->src_len & (AES_BLOCK_SIZE - 1)))
  669. return -EINVAL;
  670. if (xts->iv_len != AES_BLOCK_SIZE)
  671. return -EINVAL;
  672. if (!xts->key || !xts->iv || !xts->src || !xts->dst)
  673. return -EINVAL;
  674. BUILD_BUG_ON(CCP_XTS_AES_KEY_SB_COUNT != 1);
  675. BUILD_BUG_ON(CCP_XTS_AES_CTX_SB_COUNT != 1);
  676. ret = -EIO;
  677. memset(&op, 0, sizeof(op));
  678. op.cmd_q = cmd_q;
  679. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  680. op.sb_key = cmd_q->sb_key;
  681. op.sb_ctx = cmd_q->sb_ctx;
  682. op.init = 1;
  683. op.u.xts.action = xts->action;
  684. op.u.xts.unit_size = xts->unit_size;
  685. /* All supported key sizes fit in a single (32-byte) SB entry
  686. * and must be in little endian format. Use the 256-bit byte
  687. * swap passthru option to convert from big endian to little
  688. * endian.
  689. */
  690. ret = ccp_init_dm_workarea(&key, cmd_q,
  691. CCP_XTS_AES_KEY_SB_COUNT * CCP_SB_BYTES,
  692. DMA_TO_DEVICE);
  693. if (ret)
  694. return ret;
  695. dm_offset = CCP_SB_BYTES - AES_KEYSIZE_128;
  696. ccp_set_dm_area(&key, dm_offset, xts->key, 0, xts->key_len);
  697. ccp_set_dm_area(&key, 0, xts->key, dm_offset, xts->key_len);
  698. ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
  699. CCP_PASSTHRU_BYTESWAP_256BIT);
  700. if (ret) {
  701. cmd->engine_error = cmd_q->cmd_error;
  702. goto e_key;
  703. }
  704. /* The AES context fits in a single (32-byte) SB entry and
  705. * for XTS is already in little endian format so no byte swapping
  706. * is needed.
  707. */
  708. ret = ccp_init_dm_workarea(&ctx, cmd_q,
  709. CCP_XTS_AES_CTX_SB_COUNT * CCP_SB_BYTES,
  710. DMA_BIDIRECTIONAL);
  711. if (ret)
  712. goto e_key;
  713. ccp_set_dm_area(&ctx, 0, xts->iv, 0, xts->iv_len);
  714. ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  715. CCP_PASSTHRU_BYTESWAP_NOOP);
  716. if (ret) {
  717. cmd->engine_error = cmd_q->cmd_error;
  718. goto e_ctx;
  719. }
  720. /* Prepare the input and output data workareas. For in-place
  721. * operations we need to set the dma direction to BIDIRECTIONAL
  722. * and copy the src workarea to the dst workarea.
  723. */
  724. if (sg_virt(xts->src) == sg_virt(xts->dst))
  725. in_place = true;
  726. ret = ccp_init_data(&src, cmd_q, xts->src, xts->src_len,
  727. unit_size,
  728. in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  729. if (ret)
  730. goto e_ctx;
  731. if (in_place) {
  732. dst = src;
  733. } else {
  734. ret = ccp_init_data(&dst, cmd_q, xts->dst, xts->src_len,
  735. unit_size, DMA_FROM_DEVICE);
  736. if (ret)
  737. goto e_src;
  738. }
  739. /* Send data to the CCP AES engine */
  740. while (src.sg_wa.bytes_left) {
  741. ccp_prepare_data(&src, &dst, &op, unit_size, true);
  742. if (!src.sg_wa.bytes_left)
  743. op.eom = 1;
  744. ret = cmd_q->ccp->vdata->perform->xts_aes(&op);
  745. if (ret) {
  746. cmd->engine_error = cmd_q->cmd_error;
  747. goto e_dst;
  748. }
  749. ccp_process_data(&src, &dst, &op);
  750. }
  751. /* Retrieve the AES context - convert from LE to BE using
  752. * 32-byte (256-bit) byteswapping
  753. */
  754. ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  755. CCP_PASSTHRU_BYTESWAP_256BIT);
  756. if (ret) {
  757. cmd->engine_error = cmd_q->cmd_error;
  758. goto e_dst;
  759. }
  760. /* ...but we only need AES_BLOCK_SIZE bytes */
  761. dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
  762. ccp_get_dm_area(&ctx, dm_offset, xts->iv, 0, xts->iv_len);
  763. e_dst:
  764. if (!in_place)
  765. ccp_free_data(&dst, cmd_q);
  766. e_src:
  767. ccp_free_data(&src, cmd_q);
  768. e_ctx:
  769. ccp_dm_free(&ctx);
  770. e_key:
  771. ccp_dm_free(&key);
  772. return ret;
  773. }
  774. static int ccp_run_sha_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  775. {
  776. struct ccp_sha_engine *sha = &cmd->u.sha;
  777. struct ccp_dm_workarea ctx;
  778. struct ccp_data src;
  779. struct ccp_op op;
  780. unsigned int ioffset, ooffset;
  781. unsigned int digest_size;
  782. int sb_count;
  783. const void *init;
  784. u64 block_size;
  785. int ctx_size;
  786. int ret;
  787. switch (sha->type) {
  788. case CCP_SHA_TYPE_1:
  789. if (sha->ctx_len < SHA1_DIGEST_SIZE)
  790. return -EINVAL;
  791. block_size = SHA1_BLOCK_SIZE;
  792. break;
  793. case CCP_SHA_TYPE_224:
  794. if (sha->ctx_len < SHA224_DIGEST_SIZE)
  795. return -EINVAL;
  796. block_size = SHA224_BLOCK_SIZE;
  797. break;
  798. case CCP_SHA_TYPE_256:
  799. if (sha->ctx_len < SHA256_DIGEST_SIZE)
  800. return -EINVAL;
  801. block_size = SHA256_BLOCK_SIZE;
  802. break;
  803. default:
  804. return -EINVAL;
  805. }
  806. if (!sha->ctx)
  807. return -EINVAL;
  808. if (!sha->final && (sha->src_len & (block_size - 1)))
  809. return -EINVAL;
  810. /* The version 3 device can't handle zero-length input */
  811. if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0)) {
  812. if (!sha->src_len) {
  813. unsigned int digest_len;
  814. const u8 *sha_zero;
  815. /* Not final, just return */
  816. if (!sha->final)
  817. return 0;
  818. /* CCP can't do a zero length sha operation so the
  819. * caller must buffer the data.
  820. */
  821. if (sha->msg_bits)
  822. return -EINVAL;
  823. /* The CCP cannot perform zero-length sha operations
  824. * so the caller is required to buffer data for the
  825. * final operation. However, a sha operation for a
  826. * message with a total length of zero is valid so
  827. * known values are required to supply the result.
  828. */
  829. switch (sha->type) {
  830. case CCP_SHA_TYPE_1:
  831. sha_zero = sha1_zero_message_hash;
  832. digest_len = SHA1_DIGEST_SIZE;
  833. break;
  834. case CCP_SHA_TYPE_224:
  835. sha_zero = sha224_zero_message_hash;
  836. digest_len = SHA224_DIGEST_SIZE;
  837. break;
  838. case CCP_SHA_TYPE_256:
  839. sha_zero = sha256_zero_message_hash;
  840. digest_len = SHA256_DIGEST_SIZE;
  841. break;
  842. default:
  843. return -EINVAL;
  844. }
  845. scatterwalk_map_and_copy((void *)sha_zero, sha->ctx, 0,
  846. digest_len, 1);
  847. return 0;
  848. }
  849. }
  850. /* Set variables used throughout */
  851. switch (sha->type) {
  852. case CCP_SHA_TYPE_1:
  853. digest_size = SHA1_DIGEST_SIZE;
  854. init = (void *) ccp_sha1_init;
  855. ctx_size = SHA1_DIGEST_SIZE;
  856. sb_count = 1;
  857. if (cmd_q->ccp->vdata->version != CCP_VERSION(3, 0))
  858. ooffset = ioffset = CCP_SB_BYTES - SHA1_DIGEST_SIZE;
  859. else
  860. ooffset = ioffset = 0;
  861. break;
  862. case CCP_SHA_TYPE_224:
  863. digest_size = SHA224_DIGEST_SIZE;
  864. init = (void *) ccp_sha224_init;
  865. ctx_size = SHA256_DIGEST_SIZE;
  866. sb_count = 1;
  867. ioffset = 0;
  868. if (cmd_q->ccp->vdata->version != CCP_VERSION(3, 0))
  869. ooffset = CCP_SB_BYTES - SHA224_DIGEST_SIZE;
  870. else
  871. ooffset = 0;
  872. break;
  873. case CCP_SHA_TYPE_256:
  874. digest_size = SHA256_DIGEST_SIZE;
  875. init = (void *) ccp_sha256_init;
  876. ctx_size = SHA256_DIGEST_SIZE;
  877. sb_count = 1;
  878. ooffset = ioffset = 0;
  879. break;
  880. default:
  881. ret = -EINVAL;
  882. goto e_data;
  883. }
  884. /* For zero-length plaintext the src pointer is ignored;
  885. * otherwise both parts must be valid
  886. */
  887. if (sha->src_len && !sha->src)
  888. return -EINVAL;
  889. memset(&op, 0, sizeof(op));
  890. op.cmd_q = cmd_q;
  891. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  892. op.sb_ctx = cmd_q->sb_ctx; /* Pre-allocated */
  893. op.u.sha.type = sha->type;
  894. op.u.sha.msg_bits = sha->msg_bits;
  895. ret = ccp_init_dm_workarea(&ctx, cmd_q, sb_count * CCP_SB_BYTES,
  896. DMA_BIDIRECTIONAL);
  897. if (ret)
  898. return ret;
  899. if (sha->first) {
  900. switch (sha->type) {
  901. case CCP_SHA_TYPE_1:
  902. case CCP_SHA_TYPE_224:
  903. case CCP_SHA_TYPE_256:
  904. memcpy(ctx.address + ioffset, init, ctx_size);
  905. break;
  906. default:
  907. ret = -EINVAL;
  908. goto e_ctx;
  909. }
  910. } else {
  911. /* Restore the context */
  912. ccp_set_dm_area(&ctx, 0, sha->ctx, 0,
  913. sb_count * CCP_SB_BYTES);
  914. }
  915. ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  916. CCP_PASSTHRU_BYTESWAP_256BIT);
  917. if (ret) {
  918. cmd->engine_error = cmd_q->cmd_error;
  919. goto e_ctx;
  920. }
  921. if (sha->src) {
  922. /* Send data to the CCP SHA engine; block_size is set above */
  923. ret = ccp_init_data(&src, cmd_q, sha->src, sha->src_len,
  924. block_size, DMA_TO_DEVICE);
  925. if (ret)
  926. goto e_ctx;
  927. while (src.sg_wa.bytes_left) {
  928. ccp_prepare_data(&src, NULL, &op, block_size, false);
  929. if (sha->final && !src.sg_wa.bytes_left)
  930. op.eom = 1;
  931. ret = cmd_q->ccp->vdata->perform->sha(&op);
  932. if (ret) {
  933. cmd->engine_error = cmd_q->cmd_error;
  934. goto e_data;
  935. }
  936. ccp_process_data(&src, NULL, &op);
  937. }
  938. } else {
  939. op.eom = 1;
  940. ret = cmd_q->ccp->vdata->perform->sha(&op);
  941. if (ret) {
  942. cmd->engine_error = cmd_q->cmd_error;
  943. goto e_data;
  944. }
  945. }
  946. /* Retrieve the SHA context - convert from LE to BE using
  947. * 32-byte (256-bit) byteswapping to BE
  948. */
  949. ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  950. CCP_PASSTHRU_BYTESWAP_256BIT);
  951. if (ret) {
  952. cmd->engine_error = cmd_q->cmd_error;
  953. goto e_data;
  954. }
  955. if (sha->final) {
  956. /* Finishing up, so get the digest */
  957. switch (sha->type) {
  958. case CCP_SHA_TYPE_1:
  959. case CCP_SHA_TYPE_224:
  960. case CCP_SHA_TYPE_256:
  961. ccp_get_dm_area(&ctx, ooffset,
  962. sha->ctx, 0,
  963. digest_size);
  964. break;
  965. default:
  966. ret = -EINVAL;
  967. goto e_ctx;
  968. }
  969. } else {
  970. /* Stash the context */
  971. ccp_get_dm_area(&ctx, 0, sha->ctx, 0,
  972. sb_count * CCP_SB_BYTES);
  973. }
  974. if (sha->final && sha->opad) {
  975. /* HMAC operation, recursively perform final SHA */
  976. struct ccp_cmd hmac_cmd;
  977. struct scatterlist sg;
  978. u8 *hmac_buf;
  979. if (sha->opad_len != block_size) {
  980. ret = -EINVAL;
  981. goto e_data;
  982. }
  983. hmac_buf = kmalloc(block_size + digest_size, GFP_KERNEL);
  984. if (!hmac_buf) {
  985. ret = -ENOMEM;
  986. goto e_data;
  987. }
  988. sg_init_one(&sg, hmac_buf, block_size + digest_size);
  989. scatterwalk_map_and_copy(hmac_buf, sha->opad, 0, block_size, 0);
  990. switch (sha->type) {
  991. case CCP_SHA_TYPE_1:
  992. case CCP_SHA_TYPE_224:
  993. case CCP_SHA_TYPE_256:
  994. memcpy(hmac_buf + block_size,
  995. ctx.address + ooffset,
  996. digest_size);
  997. break;
  998. default:
  999. ret = -EINVAL;
  1000. goto e_ctx;
  1001. }
  1002. memset(&hmac_cmd, 0, sizeof(hmac_cmd));
  1003. hmac_cmd.engine = CCP_ENGINE_SHA;
  1004. hmac_cmd.u.sha.type = sha->type;
  1005. hmac_cmd.u.sha.ctx = sha->ctx;
  1006. hmac_cmd.u.sha.ctx_len = sha->ctx_len;
  1007. hmac_cmd.u.sha.src = &sg;
  1008. hmac_cmd.u.sha.src_len = block_size + digest_size;
  1009. hmac_cmd.u.sha.opad = NULL;
  1010. hmac_cmd.u.sha.opad_len = 0;
  1011. hmac_cmd.u.sha.first = 1;
  1012. hmac_cmd.u.sha.final = 1;
  1013. hmac_cmd.u.sha.msg_bits = (block_size + digest_size) << 3;
  1014. ret = ccp_run_sha_cmd(cmd_q, &hmac_cmd);
  1015. if (ret)
  1016. cmd->engine_error = hmac_cmd.engine_error;
  1017. kfree(hmac_buf);
  1018. }
  1019. e_data:
  1020. if (sha->src)
  1021. ccp_free_data(&src, cmd_q);
  1022. e_ctx:
  1023. ccp_dm_free(&ctx);
  1024. return ret;
  1025. }
  1026. static int ccp_run_rsa_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1027. {
  1028. struct ccp_rsa_engine *rsa = &cmd->u.rsa;
  1029. struct ccp_dm_workarea exp, src;
  1030. struct ccp_data dst;
  1031. struct ccp_op op;
  1032. unsigned int sb_count, i_len, o_len;
  1033. int ret;
  1034. if (rsa->key_size > CCP_RSA_MAX_WIDTH)
  1035. return -EINVAL;
  1036. if (!rsa->exp || !rsa->mod || !rsa->src || !rsa->dst)
  1037. return -EINVAL;
  1038. /* The RSA modulus must precede the message being acted upon, so
  1039. * it must be copied to a DMA area where the message and the
  1040. * modulus can be concatenated. Therefore the input buffer
  1041. * length required is twice the output buffer length (which
  1042. * must be a multiple of 256-bits).
  1043. */
  1044. o_len = ((rsa->key_size + 255) / 256) * 32;
  1045. i_len = o_len * 2;
  1046. sb_count = o_len / CCP_SB_BYTES;
  1047. memset(&op, 0, sizeof(op));
  1048. op.cmd_q = cmd_q;
  1049. op.jobid = ccp_gen_jobid(cmd_q->ccp);
  1050. op.sb_key = cmd_q->ccp->vdata->perform->sballoc(cmd_q, sb_count);
  1051. if (!op.sb_key)
  1052. return -EIO;
  1053. /* The RSA exponent may span multiple (32-byte) SB entries and must
  1054. * be in little endian format. Reverse copy each 32-byte chunk
  1055. * of the exponent (En chunk to E0 chunk, E(n-1) chunk to E1 chunk)
  1056. * and each byte within that chunk and do not perform any byte swap
  1057. * operations on the passthru operation.
  1058. */
  1059. ret = ccp_init_dm_workarea(&exp, cmd_q, o_len, DMA_TO_DEVICE);
  1060. if (ret)
  1061. goto e_sb;
  1062. ret = ccp_reverse_set_dm_area(&exp, rsa->exp, rsa->exp_len,
  1063. CCP_SB_BYTES, false);
  1064. if (ret)
  1065. goto e_exp;
  1066. ret = ccp_copy_to_sb(cmd_q, &exp, op.jobid, op.sb_key,
  1067. CCP_PASSTHRU_BYTESWAP_NOOP);
  1068. if (ret) {
  1069. cmd->engine_error = cmd_q->cmd_error;
  1070. goto e_exp;
  1071. }
  1072. /* Concatenate the modulus and the message. Both the modulus and
  1073. * the operands must be in little endian format. Since the input
  1074. * is in big endian format it must be converted.
  1075. */
  1076. ret = ccp_init_dm_workarea(&src, cmd_q, i_len, DMA_TO_DEVICE);
  1077. if (ret)
  1078. goto e_exp;
  1079. ret = ccp_reverse_set_dm_area(&src, rsa->mod, rsa->mod_len,
  1080. CCP_SB_BYTES, false);
  1081. if (ret)
  1082. goto e_src;
  1083. src.address += o_len; /* Adjust the address for the copy operation */
  1084. ret = ccp_reverse_set_dm_area(&src, rsa->src, rsa->src_len,
  1085. CCP_SB_BYTES, false);
  1086. if (ret)
  1087. goto e_src;
  1088. src.address -= o_len; /* Reset the address to original value */
  1089. /* Prepare the output area for the operation */
  1090. ret = ccp_init_data(&dst, cmd_q, rsa->dst, rsa->mod_len,
  1091. o_len, DMA_FROM_DEVICE);
  1092. if (ret)
  1093. goto e_src;
  1094. op.soc = 1;
  1095. op.src.u.dma.address = src.dma.address;
  1096. op.src.u.dma.offset = 0;
  1097. op.src.u.dma.length = i_len;
  1098. op.dst.u.dma.address = dst.dm_wa.dma.address;
  1099. op.dst.u.dma.offset = 0;
  1100. op.dst.u.dma.length = o_len;
  1101. op.u.rsa.mod_size = rsa->key_size;
  1102. op.u.rsa.input_len = i_len;
  1103. ret = cmd_q->ccp->vdata->perform->rsa(&op);
  1104. if (ret) {
  1105. cmd->engine_error = cmd_q->cmd_error;
  1106. goto e_dst;
  1107. }
  1108. ccp_reverse_get_dm_area(&dst.dm_wa, rsa->dst, rsa->mod_len);
  1109. e_dst:
  1110. ccp_free_data(&dst, cmd_q);
  1111. e_src:
  1112. ccp_dm_free(&src);
  1113. e_exp:
  1114. ccp_dm_free(&exp);
  1115. e_sb:
  1116. cmd_q->ccp->vdata->perform->sbfree(cmd_q, op.sb_key, sb_count);
  1117. return ret;
  1118. }
  1119. static int ccp_run_passthru_cmd(struct ccp_cmd_queue *cmd_q,
  1120. struct ccp_cmd *cmd)
  1121. {
  1122. struct ccp_passthru_engine *pt = &cmd->u.passthru;
  1123. struct ccp_dm_workarea mask;
  1124. struct ccp_data src, dst;
  1125. struct ccp_op op;
  1126. bool in_place = false;
  1127. unsigned int i;
  1128. int ret = 0;
  1129. if (!pt->final && (pt->src_len & (CCP_PASSTHRU_BLOCKSIZE - 1)))
  1130. return -EINVAL;
  1131. if (!pt->src || !pt->dst)
  1132. return -EINVAL;
  1133. if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
  1134. if (pt->mask_len != CCP_PASSTHRU_MASKSIZE)
  1135. return -EINVAL;
  1136. if (!pt->mask)
  1137. return -EINVAL;
  1138. }
  1139. BUILD_BUG_ON(CCP_PASSTHRU_SB_COUNT != 1);
  1140. memset(&op, 0, sizeof(op));
  1141. op.cmd_q = cmd_q;
  1142. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  1143. if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
  1144. /* Load the mask */
  1145. op.sb_key = cmd_q->sb_key;
  1146. ret = ccp_init_dm_workarea(&mask, cmd_q,
  1147. CCP_PASSTHRU_SB_COUNT *
  1148. CCP_SB_BYTES,
  1149. DMA_TO_DEVICE);
  1150. if (ret)
  1151. return ret;
  1152. ccp_set_dm_area(&mask, 0, pt->mask, 0, pt->mask_len);
  1153. ret = ccp_copy_to_sb(cmd_q, &mask, op.jobid, op.sb_key,
  1154. CCP_PASSTHRU_BYTESWAP_NOOP);
  1155. if (ret) {
  1156. cmd->engine_error = cmd_q->cmd_error;
  1157. goto e_mask;
  1158. }
  1159. }
  1160. /* Prepare the input and output data workareas. For in-place
  1161. * operations we need to set the dma direction to BIDIRECTIONAL
  1162. * and copy the src workarea to the dst workarea.
  1163. */
  1164. if (sg_virt(pt->src) == sg_virt(pt->dst))
  1165. in_place = true;
  1166. ret = ccp_init_data(&src, cmd_q, pt->src, pt->src_len,
  1167. CCP_PASSTHRU_MASKSIZE,
  1168. in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  1169. if (ret)
  1170. goto e_mask;
  1171. if (in_place) {
  1172. dst = src;
  1173. } else {
  1174. ret = ccp_init_data(&dst, cmd_q, pt->dst, pt->src_len,
  1175. CCP_PASSTHRU_MASKSIZE, DMA_FROM_DEVICE);
  1176. if (ret)
  1177. goto e_src;
  1178. }
  1179. /* Send data to the CCP Passthru engine
  1180. * Because the CCP engine works on a single source and destination
  1181. * dma address at a time, each entry in the source scatterlist
  1182. * (after the dma_map_sg call) must be less than or equal to the
  1183. * (remaining) length in the destination scatterlist entry and the
  1184. * length must be a multiple of CCP_PASSTHRU_BLOCKSIZE
  1185. */
  1186. dst.sg_wa.sg_used = 0;
  1187. for (i = 1; i <= src.sg_wa.dma_count; i++) {
  1188. if (!dst.sg_wa.sg ||
  1189. (dst.sg_wa.sg->length < src.sg_wa.sg->length)) {
  1190. ret = -EINVAL;
  1191. goto e_dst;
  1192. }
  1193. if (i == src.sg_wa.dma_count) {
  1194. op.eom = 1;
  1195. op.soc = 1;
  1196. }
  1197. op.src.type = CCP_MEMTYPE_SYSTEM;
  1198. op.src.u.dma.address = sg_dma_address(src.sg_wa.sg);
  1199. op.src.u.dma.offset = 0;
  1200. op.src.u.dma.length = sg_dma_len(src.sg_wa.sg);
  1201. op.dst.type = CCP_MEMTYPE_SYSTEM;
  1202. op.dst.u.dma.address = sg_dma_address(dst.sg_wa.sg);
  1203. op.dst.u.dma.offset = dst.sg_wa.sg_used;
  1204. op.dst.u.dma.length = op.src.u.dma.length;
  1205. ret = cmd_q->ccp->vdata->perform->passthru(&op);
  1206. if (ret) {
  1207. cmd->engine_error = cmd_q->cmd_error;
  1208. goto e_dst;
  1209. }
  1210. dst.sg_wa.sg_used += src.sg_wa.sg->length;
  1211. if (dst.sg_wa.sg_used == dst.sg_wa.sg->length) {
  1212. dst.sg_wa.sg = sg_next(dst.sg_wa.sg);
  1213. dst.sg_wa.sg_used = 0;
  1214. }
  1215. src.sg_wa.sg = sg_next(src.sg_wa.sg);
  1216. }
  1217. e_dst:
  1218. if (!in_place)
  1219. ccp_free_data(&dst, cmd_q);
  1220. e_src:
  1221. ccp_free_data(&src, cmd_q);
  1222. e_mask:
  1223. if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP)
  1224. ccp_dm_free(&mask);
  1225. return ret;
  1226. }
  1227. static int ccp_run_passthru_nomap_cmd(struct ccp_cmd_queue *cmd_q,
  1228. struct ccp_cmd *cmd)
  1229. {
  1230. struct ccp_passthru_nomap_engine *pt = &cmd->u.passthru_nomap;
  1231. struct ccp_dm_workarea mask;
  1232. struct ccp_op op;
  1233. int ret;
  1234. if (!pt->final && (pt->src_len & (CCP_PASSTHRU_BLOCKSIZE - 1)))
  1235. return -EINVAL;
  1236. if (!pt->src_dma || !pt->dst_dma)
  1237. return -EINVAL;
  1238. if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
  1239. if (pt->mask_len != CCP_PASSTHRU_MASKSIZE)
  1240. return -EINVAL;
  1241. if (!pt->mask)
  1242. return -EINVAL;
  1243. }
  1244. BUILD_BUG_ON(CCP_PASSTHRU_SB_COUNT != 1);
  1245. memset(&op, 0, sizeof(op));
  1246. op.cmd_q = cmd_q;
  1247. op.jobid = ccp_gen_jobid(cmd_q->ccp);
  1248. if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
  1249. /* Load the mask */
  1250. op.sb_key = cmd_q->sb_key;
  1251. mask.length = pt->mask_len;
  1252. mask.dma.address = pt->mask;
  1253. mask.dma.length = pt->mask_len;
  1254. ret = ccp_copy_to_sb(cmd_q, &mask, op.jobid, op.sb_key,
  1255. CCP_PASSTHRU_BYTESWAP_NOOP);
  1256. if (ret) {
  1257. cmd->engine_error = cmd_q->cmd_error;
  1258. return ret;
  1259. }
  1260. }
  1261. /* Send data to the CCP Passthru engine */
  1262. op.eom = 1;
  1263. op.soc = 1;
  1264. op.src.type = CCP_MEMTYPE_SYSTEM;
  1265. op.src.u.dma.address = pt->src_dma;
  1266. op.src.u.dma.offset = 0;
  1267. op.src.u.dma.length = pt->src_len;
  1268. op.dst.type = CCP_MEMTYPE_SYSTEM;
  1269. op.dst.u.dma.address = pt->dst_dma;
  1270. op.dst.u.dma.offset = 0;
  1271. op.dst.u.dma.length = pt->src_len;
  1272. ret = cmd_q->ccp->vdata->perform->passthru(&op);
  1273. if (ret)
  1274. cmd->engine_error = cmd_q->cmd_error;
  1275. return ret;
  1276. }
  1277. static int ccp_run_ecc_mm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1278. {
  1279. struct ccp_ecc_engine *ecc = &cmd->u.ecc;
  1280. struct ccp_dm_workarea src, dst;
  1281. struct ccp_op op;
  1282. int ret;
  1283. u8 *save;
  1284. if (!ecc->u.mm.operand_1 ||
  1285. (ecc->u.mm.operand_1_len > CCP_ECC_MODULUS_BYTES))
  1286. return -EINVAL;
  1287. if (ecc->function != CCP_ECC_FUNCTION_MINV_384BIT)
  1288. if (!ecc->u.mm.operand_2 ||
  1289. (ecc->u.mm.operand_2_len > CCP_ECC_MODULUS_BYTES))
  1290. return -EINVAL;
  1291. if (!ecc->u.mm.result ||
  1292. (ecc->u.mm.result_len < CCP_ECC_MODULUS_BYTES))
  1293. return -EINVAL;
  1294. memset(&op, 0, sizeof(op));
  1295. op.cmd_q = cmd_q;
  1296. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  1297. /* Concatenate the modulus and the operands. Both the modulus and
  1298. * the operands must be in little endian format. Since the input
  1299. * is in big endian format it must be converted and placed in a
  1300. * fixed length buffer.
  1301. */
  1302. ret = ccp_init_dm_workarea(&src, cmd_q, CCP_ECC_SRC_BUF_SIZE,
  1303. DMA_TO_DEVICE);
  1304. if (ret)
  1305. return ret;
  1306. /* Save the workarea address since it is updated in order to perform
  1307. * the concatenation
  1308. */
  1309. save = src.address;
  1310. /* Copy the ECC modulus */
  1311. ret = ccp_reverse_set_dm_area(&src, ecc->mod, ecc->mod_len,
  1312. CCP_ECC_OPERAND_SIZE, false);
  1313. if (ret)
  1314. goto e_src;
  1315. src.address += CCP_ECC_OPERAND_SIZE;
  1316. /* Copy the first operand */
  1317. ret = ccp_reverse_set_dm_area(&src, ecc->u.mm.operand_1,
  1318. ecc->u.mm.operand_1_len,
  1319. CCP_ECC_OPERAND_SIZE, false);
  1320. if (ret)
  1321. goto e_src;
  1322. src.address += CCP_ECC_OPERAND_SIZE;
  1323. if (ecc->function != CCP_ECC_FUNCTION_MINV_384BIT) {
  1324. /* Copy the second operand */
  1325. ret = ccp_reverse_set_dm_area(&src, ecc->u.mm.operand_2,
  1326. ecc->u.mm.operand_2_len,
  1327. CCP_ECC_OPERAND_SIZE, false);
  1328. if (ret)
  1329. goto e_src;
  1330. src.address += CCP_ECC_OPERAND_SIZE;
  1331. }
  1332. /* Restore the workarea address */
  1333. src.address = save;
  1334. /* Prepare the output area for the operation */
  1335. ret = ccp_init_dm_workarea(&dst, cmd_q, CCP_ECC_DST_BUF_SIZE,
  1336. DMA_FROM_DEVICE);
  1337. if (ret)
  1338. goto e_src;
  1339. op.soc = 1;
  1340. op.src.u.dma.address = src.dma.address;
  1341. op.src.u.dma.offset = 0;
  1342. op.src.u.dma.length = src.length;
  1343. op.dst.u.dma.address = dst.dma.address;
  1344. op.dst.u.dma.offset = 0;
  1345. op.dst.u.dma.length = dst.length;
  1346. op.u.ecc.function = cmd->u.ecc.function;
  1347. ret = cmd_q->ccp->vdata->perform->ecc(&op);
  1348. if (ret) {
  1349. cmd->engine_error = cmd_q->cmd_error;
  1350. goto e_dst;
  1351. }
  1352. ecc->ecc_result = le16_to_cpup(
  1353. (const __le16 *)(dst.address + CCP_ECC_RESULT_OFFSET));
  1354. if (!(ecc->ecc_result & CCP_ECC_RESULT_SUCCESS)) {
  1355. ret = -EIO;
  1356. goto e_dst;
  1357. }
  1358. /* Save the ECC result */
  1359. ccp_reverse_get_dm_area(&dst, ecc->u.mm.result, CCP_ECC_MODULUS_BYTES);
  1360. e_dst:
  1361. ccp_dm_free(&dst);
  1362. e_src:
  1363. ccp_dm_free(&src);
  1364. return ret;
  1365. }
  1366. static int ccp_run_ecc_pm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1367. {
  1368. struct ccp_ecc_engine *ecc = &cmd->u.ecc;
  1369. struct ccp_dm_workarea src, dst;
  1370. struct ccp_op op;
  1371. int ret;
  1372. u8 *save;
  1373. if (!ecc->u.pm.point_1.x ||
  1374. (ecc->u.pm.point_1.x_len > CCP_ECC_MODULUS_BYTES) ||
  1375. !ecc->u.pm.point_1.y ||
  1376. (ecc->u.pm.point_1.y_len > CCP_ECC_MODULUS_BYTES))
  1377. return -EINVAL;
  1378. if (ecc->function == CCP_ECC_FUNCTION_PADD_384BIT) {
  1379. if (!ecc->u.pm.point_2.x ||
  1380. (ecc->u.pm.point_2.x_len > CCP_ECC_MODULUS_BYTES) ||
  1381. !ecc->u.pm.point_2.y ||
  1382. (ecc->u.pm.point_2.y_len > CCP_ECC_MODULUS_BYTES))
  1383. return -EINVAL;
  1384. } else {
  1385. if (!ecc->u.pm.domain_a ||
  1386. (ecc->u.pm.domain_a_len > CCP_ECC_MODULUS_BYTES))
  1387. return -EINVAL;
  1388. if (ecc->function == CCP_ECC_FUNCTION_PMUL_384BIT)
  1389. if (!ecc->u.pm.scalar ||
  1390. (ecc->u.pm.scalar_len > CCP_ECC_MODULUS_BYTES))
  1391. return -EINVAL;
  1392. }
  1393. if (!ecc->u.pm.result.x ||
  1394. (ecc->u.pm.result.x_len < CCP_ECC_MODULUS_BYTES) ||
  1395. !ecc->u.pm.result.y ||
  1396. (ecc->u.pm.result.y_len < CCP_ECC_MODULUS_BYTES))
  1397. return -EINVAL;
  1398. memset(&op, 0, sizeof(op));
  1399. op.cmd_q = cmd_q;
  1400. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  1401. /* Concatenate the modulus and the operands. Both the modulus and
  1402. * the operands must be in little endian format. Since the input
  1403. * is in big endian format it must be converted and placed in a
  1404. * fixed length buffer.
  1405. */
  1406. ret = ccp_init_dm_workarea(&src, cmd_q, CCP_ECC_SRC_BUF_SIZE,
  1407. DMA_TO_DEVICE);
  1408. if (ret)
  1409. return ret;
  1410. /* Save the workarea address since it is updated in order to perform
  1411. * the concatenation
  1412. */
  1413. save = src.address;
  1414. /* Copy the ECC modulus */
  1415. ret = ccp_reverse_set_dm_area(&src, ecc->mod, ecc->mod_len,
  1416. CCP_ECC_OPERAND_SIZE, false);
  1417. if (ret)
  1418. goto e_src;
  1419. src.address += CCP_ECC_OPERAND_SIZE;
  1420. /* Copy the first point X and Y coordinate */
  1421. ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.point_1.x,
  1422. ecc->u.pm.point_1.x_len,
  1423. CCP_ECC_OPERAND_SIZE, false);
  1424. if (ret)
  1425. goto e_src;
  1426. src.address += CCP_ECC_OPERAND_SIZE;
  1427. ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.point_1.y,
  1428. ecc->u.pm.point_1.y_len,
  1429. CCP_ECC_OPERAND_SIZE, false);
  1430. if (ret)
  1431. goto e_src;
  1432. src.address += CCP_ECC_OPERAND_SIZE;
  1433. /* Set the first point Z coordinate to 1 */
  1434. *src.address = 0x01;
  1435. src.address += CCP_ECC_OPERAND_SIZE;
  1436. if (ecc->function == CCP_ECC_FUNCTION_PADD_384BIT) {
  1437. /* Copy the second point X and Y coordinate */
  1438. ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.point_2.x,
  1439. ecc->u.pm.point_2.x_len,
  1440. CCP_ECC_OPERAND_SIZE, false);
  1441. if (ret)
  1442. goto e_src;
  1443. src.address += CCP_ECC_OPERAND_SIZE;
  1444. ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.point_2.y,
  1445. ecc->u.pm.point_2.y_len,
  1446. CCP_ECC_OPERAND_SIZE, false);
  1447. if (ret)
  1448. goto e_src;
  1449. src.address += CCP_ECC_OPERAND_SIZE;
  1450. /* Set the second point Z coordinate to 1 */
  1451. *src.address = 0x01;
  1452. src.address += CCP_ECC_OPERAND_SIZE;
  1453. } else {
  1454. /* Copy the Domain "a" parameter */
  1455. ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.domain_a,
  1456. ecc->u.pm.domain_a_len,
  1457. CCP_ECC_OPERAND_SIZE, false);
  1458. if (ret)
  1459. goto e_src;
  1460. src.address += CCP_ECC_OPERAND_SIZE;
  1461. if (ecc->function == CCP_ECC_FUNCTION_PMUL_384BIT) {
  1462. /* Copy the scalar value */
  1463. ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.scalar,
  1464. ecc->u.pm.scalar_len,
  1465. CCP_ECC_OPERAND_SIZE,
  1466. false);
  1467. if (ret)
  1468. goto e_src;
  1469. src.address += CCP_ECC_OPERAND_SIZE;
  1470. }
  1471. }
  1472. /* Restore the workarea address */
  1473. src.address = save;
  1474. /* Prepare the output area for the operation */
  1475. ret = ccp_init_dm_workarea(&dst, cmd_q, CCP_ECC_DST_BUF_SIZE,
  1476. DMA_FROM_DEVICE);
  1477. if (ret)
  1478. goto e_src;
  1479. op.soc = 1;
  1480. op.src.u.dma.address = src.dma.address;
  1481. op.src.u.dma.offset = 0;
  1482. op.src.u.dma.length = src.length;
  1483. op.dst.u.dma.address = dst.dma.address;
  1484. op.dst.u.dma.offset = 0;
  1485. op.dst.u.dma.length = dst.length;
  1486. op.u.ecc.function = cmd->u.ecc.function;
  1487. ret = cmd_q->ccp->vdata->perform->ecc(&op);
  1488. if (ret) {
  1489. cmd->engine_error = cmd_q->cmd_error;
  1490. goto e_dst;
  1491. }
  1492. ecc->ecc_result = le16_to_cpup(
  1493. (const __le16 *)(dst.address + CCP_ECC_RESULT_OFFSET));
  1494. if (!(ecc->ecc_result & CCP_ECC_RESULT_SUCCESS)) {
  1495. ret = -EIO;
  1496. goto e_dst;
  1497. }
  1498. /* Save the workarea address since it is updated as we walk through
  1499. * to copy the point math result
  1500. */
  1501. save = dst.address;
  1502. /* Save the ECC result X and Y coordinates */
  1503. ccp_reverse_get_dm_area(&dst, ecc->u.pm.result.x,
  1504. CCP_ECC_MODULUS_BYTES);
  1505. dst.address += CCP_ECC_OUTPUT_SIZE;
  1506. ccp_reverse_get_dm_area(&dst, ecc->u.pm.result.y,
  1507. CCP_ECC_MODULUS_BYTES);
  1508. dst.address += CCP_ECC_OUTPUT_SIZE;
  1509. /* Restore the workarea address */
  1510. dst.address = save;
  1511. e_dst:
  1512. ccp_dm_free(&dst);
  1513. e_src:
  1514. ccp_dm_free(&src);
  1515. return ret;
  1516. }
  1517. static int ccp_run_ecc_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1518. {
  1519. struct ccp_ecc_engine *ecc = &cmd->u.ecc;
  1520. ecc->ecc_result = 0;
  1521. if (!ecc->mod ||
  1522. (ecc->mod_len > CCP_ECC_MODULUS_BYTES))
  1523. return -EINVAL;
  1524. switch (ecc->function) {
  1525. case CCP_ECC_FUNCTION_MMUL_384BIT:
  1526. case CCP_ECC_FUNCTION_MADD_384BIT:
  1527. case CCP_ECC_FUNCTION_MINV_384BIT:
  1528. return ccp_run_ecc_mm_cmd(cmd_q, cmd);
  1529. case CCP_ECC_FUNCTION_PADD_384BIT:
  1530. case CCP_ECC_FUNCTION_PMUL_384BIT:
  1531. case CCP_ECC_FUNCTION_PDBL_384BIT:
  1532. return ccp_run_ecc_pm_cmd(cmd_q, cmd);
  1533. default:
  1534. return -EINVAL;
  1535. }
  1536. }
  1537. int ccp_run_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1538. {
  1539. int ret;
  1540. cmd->engine_error = 0;
  1541. cmd_q->cmd_error = 0;
  1542. cmd_q->int_rcvd = 0;
  1543. cmd_q->free_slots = cmd_q->ccp->vdata->perform->get_free_slots(cmd_q);
  1544. switch (cmd->engine) {
  1545. case CCP_ENGINE_AES:
  1546. ret = ccp_run_aes_cmd(cmd_q, cmd);
  1547. break;
  1548. case CCP_ENGINE_XTS_AES_128:
  1549. ret = ccp_run_xts_aes_cmd(cmd_q, cmd);
  1550. break;
  1551. case CCP_ENGINE_SHA:
  1552. ret = ccp_run_sha_cmd(cmd_q, cmd);
  1553. break;
  1554. case CCP_ENGINE_RSA:
  1555. ret = ccp_run_rsa_cmd(cmd_q, cmd);
  1556. break;
  1557. case CCP_ENGINE_PASSTHRU:
  1558. if (cmd->flags & CCP_CMD_PASSTHRU_NO_DMA_MAP)
  1559. ret = ccp_run_passthru_nomap_cmd(cmd_q, cmd);
  1560. else
  1561. ret = ccp_run_passthru_cmd(cmd_q, cmd);
  1562. break;
  1563. case CCP_ENGINE_ECC:
  1564. ret = ccp_run_ecc_cmd(cmd_q, cmd);
  1565. break;
  1566. default:
  1567. ret = -EINVAL;
  1568. }
  1569. return ret;
  1570. }