ccp-ops.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425
  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 (dma_mapping_error(wa->dev, 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 if (xts->key_len == AES_KEYSIZE_256)
  892. aestype = CCP_AES_TYPE_256;
  893. else
  894. return -EINVAL;
  895. if (!xts->final && (xts->src_len & (AES_BLOCK_SIZE - 1)))
  896. return -EINVAL;
  897. if (xts->iv_len != AES_BLOCK_SIZE)
  898. return -EINVAL;
  899. if (!xts->key || !xts->iv || !xts->src || !xts->dst)
  900. return -EINVAL;
  901. BUILD_BUG_ON(CCP_XTS_AES_KEY_SB_COUNT != 1);
  902. BUILD_BUG_ON(CCP_XTS_AES_CTX_SB_COUNT != 1);
  903. ret = -EIO;
  904. memset(&op, 0, sizeof(op));
  905. op.cmd_q = cmd_q;
  906. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  907. op.sb_key = cmd_q->sb_key;
  908. op.sb_ctx = cmd_q->sb_ctx;
  909. op.init = 1;
  910. op.u.xts.type = aestype;
  911. op.u.xts.action = xts->action;
  912. op.u.xts.unit_size = xts->unit_size;
  913. /* A version 3 device only supports 128-bit keys, which fits into a
  914. * single SB entry. A version 5 device uses a 512-bit vector, so two
  915. * SB entries.
  916. */
  917. if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0))
  918. sb_count = CCP_XTS_AES_KEY_SB_COUNT;
  919. else
  920. sb_count = CCP5_XTS_AES_KEY_SB_COUNT;
  921. ret = ccp_init_dm_workarea(&key, cmd_q,
  922. sb_count * CCP_SB_BYTES,
  923. DMA_TO_DEVICE);
  924. if (ret)
  925. return ret;
  926. if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0)) {
  927. /* All supported key sizes must be in little endian format.
  928. * Use the 256-bit byte swap passthru option to convert from
  929. * big endian to little endian.
  930. */
  931. dm_offset = CCP_SB_BYTES - AES_KEYSIZE_128;
  932. ccp_set_dm_area(&key, dm_offset, xts->key, 0, xts->key_len);
  933. ccp_set_dm_area(&key, 0, xts->key, xts->key_len, xts->key_len);
  934. } else {
  935. /* Version 5 CCPs use a 512-bit space for the key: each portion
  936. * occupies 256 bits, or one entire slot, and is zero-padded.
  937. */
  938. unsigned int pad;
  939. dm_offset = CCP_SB_BYTES;
  940. pad = dm_offset - xts->key_len;
  941. ccp_set_dm_area(&key, pad, xts->key, 0, xts->key_len);
  942. ccp_set_dm_area(&key, dm_offset + pad, xts->key, xts->key_len,
  943. xts->key_len);
  944. }
  945. ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
  946. CCP_PASSTHRU_BYTESWAP_256BIT);
  947. if (ret) {
  948. cmd->engine_error = cmd_q->cmd_error;
  949. goto e_key;
  950. }
  951. /* The AES context fits in a single (32-byte) SB entry and
  952. * for XTS is already in little endian format so no byte swapping
  953. * is needed.
  954. */
  955. ret = ccp_init_dm_workarea(&ctx, cmd_q,
  956. CCP_XTS_AES_CTX_SB_COUNT * CCP_SB_BYTES,
  957. DMA_BIDIRECTIONAL);
  958. if (ret)
  959. goto e_key;
  960. ccp_set_dm_area(&ctx, 0, xts->iv, 0, xts->iv_len);
  961. ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  962. CCP_PASSTHRU_BYTESWAP_NOOP);
  963. if (ret) {
  964. cmd->engine_error = cmd_q->cmd_error;
  965. goto e_ctx;
  966. }
  967. /* Prepare the input and output data workareas. For in-place
  968. * operations we need to set the dma direction to BIDIRECTIONAL
  969. * and copy the src workarea to the dst workarea.
  970. */
  971. if (sg_virt(xts->src) == sg_virt(xts->dst))
  972. in_place = true;
  973. ret = ccp_init_data(&src, cmd_q, xts->src, xts->src_len,
  974. unit_size,
  975. in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  976. if (ret)
  977. goto e_ctx;
  978. if (in_place) {
  979. dst = src;
  980. } else {
  981. ret = ccp_init_data(&dst, cmd_q, xts->dst, xts->src_len,
  982. unit_size, DMA_FROM_DEVICE);
  983. if (ret)
  984. goto e_src;
  985. }
  986. /* Send data to the CCP AES engine */
  987. while (src.sg_wa.bytes_left) {
  988. ccp_prepare_data(&src, &dst, &op, unit_size, true);
  989. if (!src.sg_wa.bytes_left)
  990. op.eom = 1;
  991. ret = cmd_q->ccp->vdata->perform->xts_aes(&op);
  992. if (ret) {
  993. cmd->engine_error = cmd_q->cmd_error;
  994. goto e_dst;
  995. }
  996. ccp_process_data(&src, &dst, &op);
  997. }
  998. /* Retrieve the AES context - convert from LE to BE using
  999. * 32-byte (256-bit) byteswapping
  1000. */
  1001. ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  1002. CCP_PASSTHRU_BYTESWAP_256BIT);
  1003. if (ret) {
  1004. cmd->engine_error = cmd_q->cmd_error;
  1005. goto e_dst;
  1006. }
  1007. /* ...but we only need AES_BLOCK_SIZE bytes */
  1008. dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
  1009. ccp_get_dm_area(&ctx, dm_offset, xts->iv, 0, xts->iv_len);
  1010. e_dst:
  1011. if (!in_place)
  1012. ccp_free_data(&dst, cmd_q);
  1013. e_src:
  1014. ccp_free_data(&src, cmd_q);
  1015. e_ctx:
  1016. ccp_dm_free(&ctx);
  1017. e_key:
  1018. ccp_dm_free(&key);
  1019. return ret;
  1020. }
  1021. static int ccp_run_des3_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1022. {
  1023. struct ccp_des3_engine *des3 = &cmd->u.des3;
  1024. struct ccp_dm_workarea key, ctx;
  1025. struct ccp_data src, dst;
  1026. struct ccp_op op;
  1027. unsigned int dm_offset;
  1028. unsigned int len_singlekey;
  1029. bool in_place = false;
  1030. int ret;
  1031. /* Error checks */
  1032. if (!cmd_q->ccp->vdata->perform->des3)
  1033. return -EINVAL;
  1034. if (des3->key_len != DES3_EDE_KEY_SIZE)
  1035. return -EINVAL;
  1036. if (((des3->mode == CCP_DES3_MODE_ECB) ||
  1037. (des3->mode == CCP_DES3_MODE_CBC)) &&
  1038. (des3->src_len & (DES3_EDE_BLOCK_SIZE - 1)))
  1039. return -EINVAL;
  1040. if (!des3->key || !des3->src || !des3->dst)
  1041. return -EINVAL;
  1042. if (des3->mode != CCP_DES3_MODE_ECB) {
  1043. if (des3->iv_len != DES3_EDE_BLOCK_SIZE)
  1044. return -EINVAL;
  1045. if (!des3->iv)
  1046. return -EINVAL;
  1047. }
  1048. ret = -EIO;
  1049. /* Zero out all the fields of the command desc */
  1050. memset(&op, 0, sizeof(op));
  1051. /* Set up the Function field */
  1052. op.cmd_q = cmd_q;
  1053. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  1054. op.sb_key = cmd_q->sb_key;
  1055. op.init = (des3->mode == CCP_DES3_MODE_ECB) ? 0 : 1;
  1056. op.u.des3.type = des3->type;
  1057. op.u.des3.mode = des3->mode;
  1058. op.u.des3.action = des3->action;
  1059. /*
  1060. * All supported key sizes fit in a single (32-byte) KSB entry and
  1061. * (like AES) must be in little endian format. Use the 256-bit byte
  1062. * swap passthru option to convert from big endian to little endian.
  1063. */
  1064. ret = ccp_init_dm_workarea(&key, cmd_q,
  1065. CCP_DES3_KEY_SB_COUNT * CCP_SB_BYTES,
  1066. DMA_TO_DEVICE);
  1067. if (ret)
  1068. return ret;
  1069. /*
  1070. * The contents of the key triplet are in the reverse order of what
  1071. * is required by the engine. Copy the 3 pieces individually to put
  1072. * them where they belong.
  1073. */
  1074. dm_offset = CCP_SB_BYTES - des3->key_len; /* Basic offset */
  1075. len_singlekey = des3->key_len / 3;
  1076. ccp_set_dm_area(&key, dm_offset + 2 * len_singlekey,
  1077. des3->key, 0, len_singlekey);
  1078. ccp_set_dm_area(&key, dm_offset + len_singlekey,
  1079. des3->key, len_singlekey, len_singlekey);
  1080. ccp_set_dm_area(&key, dm_offset,
  1081. des3->key, 2 * len_singlekey, len_singlekey);
  1082. /* Copy the key to the SB */
  1083. ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
  1084. CCP_PASSTHRU_BYTESWAP_256BIT);
  1085. if (ret) {
  1086. cmd->engine_error = cmd_q->cmd_error;
  1087. goto e_key;
  1088. }
  1089. /*
  1090. * The DES3 context fits in a single (32-byte) KSB entry and
  1091. * must be in little endian format. Use the 256-bit byte swap
  1092. * passthru option to convert from big endian to little endian.
  1093. */
  1094. if (des3->mode != CCP_DES3_MODE_ECB) {
  1095. u32 load_mode;
  1096. op.sb_ctx = cmd_q->sb_ctx;
  1097. ret = ccp_init_dm_workarea(&ctx, cmd_q,
  1098. CCP_DES3_CTX_SB_COUNT * CCP_SB_BYTES,
  1099. DMA_BIDIRECTIONAL);
  1100. if (ret)
  1101. goto e_key;
  1102. /* Load the context into the LSB */
  1103. dm_offset = CCP_SB_BYTES - des3->iv_len;
  1104. ccp_set_dm_area(&ctx, dm_offset, des3->iv, 0, des3->iv_len);
  1105. if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0))
  1106. load_mode = CCP_PASSTHRU_BYTESWAP_NOOP;
  1107. else
  1108. load_mode = CCP_PASSTHRU_BYTESWAP_256BIT;
  1109. ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  1110. load_mode);
  1111. if (ret) {
  1112. cmd->engine_error = cmd_q->cmd_error;
  1113. goto e_ctx;
  1114. }
  1115. }
  1116. /*
  1117. * Prepare the input and output data workareas. For in-place
  1118. * operations we need to set the dma direction to BIDIRECTIONAL
  1119. * and copy the src workarea to the dst workarea.
  1120. */
  1121. if (sg_virt(des3->src) == sg_virt(des3->dst))
  1122. in_place = true;
  1123. ret = ccp_init_data(&src, cmd_q, des3->src, des3->src_len,
  1124. DES3_EDE_BLOCK_SIZE,
  1125. in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  1126. if (ret)
  1127. goto e_ctx;
  1128. if (in_place)
  1129. dst = src;
  1130. else {
  1131. ret = ccp_init_data(&dst, cmd_q, des3->dst, des3->src_len,
  1132. DES3_EDE_BLOCK_SIZE, DMA_FROM_DEVICE);
  1133. if (ret)
  1134. goto e_src;
  1135. }
  1136. /* Send data to the CCP DES3 engine */
  1137. while (src.sg_wa.bytes_left) {
  1138. ccp_prepare_data(&src, &dst, &op, DES3_EDE_BLOCK_SIZE, true);
  1139. if (!src.sg_wa.bytes_left) {
  1140. op.eom = 1;
  1141. /* Since we don't retrieve the context in ECB mode
  1142. * we have to wait for the operation to complete
  1143. * on the last piece of data
  1144. */
  1145. op.soc = 0;
  1146. }
  1147. ret = cmd_q->ccp->vdata->perform->des3(&op);
  1148. if (ret) {
  1149. cmd->engine_error = cmd_q->cmd_error;
  1150. goto e_dst;
  1151. }
  1152. ccp_process_data(&src, &dst, &op);
  1153. }
  1154. if (des3->mode != CCP_DES3_MODE_ECB) {
  1155. /* Retrieve the context and make BE */
  1156. ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  1157. CCP_PASSTHRU_BYTESWAP_256BIT);
  1158. if (ret) {
  1159. cmd->engine_error = cmd_q->cmd_error;
  1160. goto e_dst;
  1161. }
  1162. /* ...but we only need the last DES3_EDE_BLOCK_SIZE bytes */
  1163. if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0))
  1164. dm_offset = CCP_SB_BYTES - des3->iv_len;
  1165. else
  1166. dm_offset = 0;
  1167. ccp_get_dm_area(&ctx, dm_offset, des3->iv, 0,
  1168. DES3_EDE_BLOCK_SIZE);
  1169. }
  1170. e_dst:
  1171. if (!in_place)
  1172. ccp_free_data(&dst, cmd_q);
  1173. e_src:
  1174. ccp_free_data(&src, cmd_q);
  1175. e_ctx:
  1176. if (des3->mode != CCP_DES3_MODE_ECB)
  1177. ccp_dm_free(&ctx);
  1178. e_key:
  1179. ccp_dm_free(&key);
  1180. return ret;
  1181. }
  1182. static int ccp_run_sha_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1183. {
  1184. struct ccp_sha_engine *sha = &cmd->u.sha;
  1185. struct ccp_dm_workarea ctx;
  1186. struct ccp_data src;
  1187. struct ccp_op op;
  1188. unsigned int ioffset, ooffset;
  1189. unsigned int digest_size;
  1190. int sb_count;
  1191. const void *init;
  1192. u64 block_size;
  1193. int ctx_size;
  1194. int ret;
  1195. switch (sha->type) {
  1196. case CCP_SHA_TYPE_1:
  1197. if (sha->ctx_len < SHA1_DIGEST_SIZE)
  1198. return -EINVAL;
  1199. block_size = SHA1_BLOCK_SIZE;
  1200. break;
  1201. case CCP_SHA_TYPE_224:
  1202. if (sha->ctx_len < SHA224_DIGEST_SIZE)
  1203. return -EINVAL;
  1204. block_size = SHA224_BLOCK_SIZE;
  1205. break;
  1206. case CCP_SHA_TYPE_256:
  1207. if (sha->ctx_len < SHA256_DIGEST_SIZE)
  1208. return -EINVAL;
  1209. block_size = SHA256_BLOCK_SIZE;
  1210. break;
  1211. case CCP_SHA_TYPE_384:
  1212. if (cmd_q->ccp->vdata->version < CCP_VERSION(4, 0)
  1213. || sha->ctx_len < SHA384_DIGEST_SIZE)
  1214. return -EINVAL;
  1215. block_size = SHA384_BLOCK_SIZE;
  1216. break;
  1217. case CCP_SHA_TYPE_512:
  1218. if (cmd_q->ccp->vdata->version < CCP_VERSION(4, 0)
  1219. || sha->ctx_len < SHA512_DIGEST_SIZE)
  1220. return -EINVAL;
  1221. block_size = SHA512_BLOCK_SIZE;
  1222. break;
  1223. default:
  1224. return -EINVAL;
  1225. }
  1226. if (!sha->ctx)
  1227. return -EINVAL;
  1228. if (!sha->final && (sha->src_len & (block_size - 1)))
  1229. return -EINVAL;
  1230. /* The version 3 device can't handle zero-length input */
  1231. if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0)) {
  1232. if (!sha->src_len) {
  1233. unsigned int digest_len;
  1234. const u8 *sha_zero;
  1235. /* Not final, just return */
  1236. if (!sha->final)
  1237. return 0;
  1238. /* CCP can't do a zero length sha operation so the
  1239. * caller must buffer the data.
  1240. */
  1241. if (sha->msg_bits)
  1242. return -EINVAL;
  1243. /* The CCP cannot perform zero-length sha operations
  1244. * so the caller is required to buffer data for the
  1245. * final operation. However, a sha operation for a
  1246. * message with a total length of zero is valid so
  1247. * known values are required to supply the result.
  1248. */
  1249. switch (sha->type) {
  1250. case CCP_SHA_TYPE_1:
  1251. sha_zero = sha1_zero_message_hash;
  1252. digest_len = SHA1_DIGEST_SIZE;
  1253. break;
  1254. case CCP_SHA_TYPE_224:
  1255. sha_zero = sha224_zero_message_hash;
  1256. digest_len = SHA224_DIGEST_SIZE;
  1257. break;
  1258. case CCP_SHA_TYPE_256:
  1259. sha_zero = sha256_zero_message_hash;
  1260. digest_len = SHA256_DIGEST_SIZE;
  1261. break;
  1262. default:
  1263. return -EINVAL;
  1264. }
  1265. scatterwalk_map_and_copy((void *)sha_zero, sha->ctx, 0,
  1266. digest_len, 1);
  1267. return 0;
  1268. }
  1269. }
  1270. /* Set variables used throughout */
  1271. switch (sha->type) {
  1272. case CCP_SHA_TYPE_1:
  1273. digest_size = SHA1_DIGEST_SIZE;
  1274. init = (void *) ccp_sha1_init;
  1275. ctx_size = SHA1_DIGEST_SIZE;
  1276. sb_count = 1;
  1277. if (cmd_q->ccp->vdata->version != CCP_VERSION(3, 0))
  1278. ooffset = ioffset = CCP_SB_BYTES - SHA1_DIGEST_SIZE;
  1279. else
  1280. ooffset = ioffset = 0;
  1281. break;
  1282. case CCP_SHA_TYPE_224:
  1283. digest_size = SHA224_DIGEST_SIZE;
  1284. init = (void *) ccp_sha224_init;
  1285. ctx_size = SHA256_DIGEST_SIZE;
  1286. sb_count = 1;
  1287. ioffset = 0;
  1288. if (cmd_q->ccp->vdata->version != CCP_VERSION(3, 0))
  1289. ooffset = CCP_SB_BYTES - SHA224_DIGEST_SIZE;
  1290. else
  1291. ooffset = 0;
  1292. break;
  1293. case CCP_SHA_TYPE_256:
  1294. digest_size = SHA256_DIGEST_SIZE;
  1295. init = (void *) ccp_sha256_init;
  1296. ctx_size = SHA256_DIGEST_SIZE;
  1297. sb_count = 1;
  1298. ooffset = ioffset = 0;
  1299. break;
  1300. case CCP_SHA_TYPE_384:
  1301. digest_size = SHA384_DIGEST_SIZE;
  1302. init = (void *) ccp_sha384_init;
  1303. ctx_size = SHA512_DIGEST_SIZE;
  1304. sb_count = 2;
  1305. ioffset = 0;
  1306. ooffset = 2 * CCP_SB_BYTES - SHA384_DIGEST_SIZE;
  1307. break;
  1308. case CCP_SHA_TYPE_512:
  1309. digest_size = SHA512_DIGEST_SIZE;
  1310. init = (void *) ccp_sha512_init;
  1311. ctx_size = SHA512_DIGEST_SIZE;
  1312. sb_count = 2;
  1313. ooffset = ioffset = 0;
  1314. break;
  1315. default:
  1316. ret = -EINVAL;
  1317. goto e_data;
  1318. }
  1319. /* For zero-length plaintext the src pointer is ignored;
  1320. * otherwise both parts must be valid
  1321. */
  1322. if (sha->src_len && !sha->src)
  1323. return -EINVAL;
  1324. memset(&op, 0, sizeof(op));
  1325. op.cmd_q = cmd_q;
  1326. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  1327. op.sb_ctx = cmd_q->sb_ctx; /* Pre-allocated */
  1328. op.u.sha.type = sha->type;
  1329. op.u.sha.msg_bits = sha->msg_bits;
  1330. /* For SHA1/224/256 the context fits in a single (32-byte) SB entry;
  1331. * SHA384/512 require 2 adjacent SB slots, with the right half in the
  1332. * first slot, and the left half in the second. Each portion must then
  1333. * be in little endian format: use the 256-bit byte swap option.
  1334. */
  1335. ret = ccp_init_dm_workarea(&ctx, cmd_q, sb_count * CCP_SB_BYTES,
  1336. DMA_BIDIRECTIONAL);
  1337. if (ret)
  1338. return ret;
  1339. if (sha->first) {
  1340. switch (sha->type) {
  1341. case CCP_SHA_TYPE_1:
  1342. case CCP_SHA_TYPE_224:
  1343. case CCP_SHA_TYPE_256:
  1344. memcpy(ctx.address + ioffset, init, ctx_size);
  1345. break;
  1346. case CCP_SHA_TYPE_384:
  1347. case CCP_SHA_TYPE_512:
  1348. memcpy(ctx.address + ctx_size / 2, init,
  1349. ctx_size / 2);
  1350. memcpy(ctx.address, init + ctx_size / 2,
  1351. ctx_size / 2);
  1352. break;
  1353. default:
  1354. ret = -EINVAL;
  1355. goto e_ctx;
  1356. }
  1357. } else {
  1358. /* Restore the context */
  1359. ccp_set_dm_area(&ctx, 0, sha->ctx, 0,
  1360. sb_count * CCP_SB_BYTES);
  1361. }
  1362. ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  1363. CCP_PASSTHRU_BYTESWAP_256BIT);
  1364. if (ret) {
  1365. cmd->engine_error = cmd_q->cmd_error;
  1366. goto e_ctx;
  1367. }
  1368. if (sha->src) {
  1369. /* Send data to the CCP SHA engine; block_size is set above */
  1370. ret = ccp_init_data(&src, cmd_q, sha->src, sha->src_len,
  1371. block_size, DMA_TO_DEVICE);
  1372. if (ret)
  1373. goto e_ctx;
  1374. while (src.sg_wa.bytes_left) {
  1375. ccp_prepare_data(&src, NULL, &op, block_size, false);
  1376. if (sha->final && !src.sg_wa.bytes_left)
  1377. op.eom = 1;
  1378. ret = cmd_q->ccp->vdata->perform->sha(&op);
  1379. if (ret) {
  1380. cmd->engine_error = cmd_q->cmd_error;
  1381. goto e_data;
  1382. }
  1383. ccp_process_data(&src, NULL, &op);
  1384. }
  1385. } else {
  1386. op.eom = 1;
  1387. ret = cmd_q->ccp->vdata->perform->sha(&op);
  1388. if (ret) {
  1389. cmd->engine_error = cmd_q->cmd_error;
  1390. goto e_data;
  1391. }
  1392. }
  1393. /* Retrieve the SHA context - convert from LE to BE using
  1394. * 32-byte (256-bit) byteswapping to BE
  1395. */
  1396. ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  1397. CCP_PASSTHRU_BYTESWAP_256BIT);
  1398. if (ret) {
  1399. cmd->engine_error = cmd_q->cmd_error;
  1400. goto e_data;
  1401. }
  1402. if (sha->final) {
  1403. /* Finishing up, so get the digest */
  1404. switch (sha->type) {
  1405. case CCP_SHA_TYPE_1:
  1406. case CCP_SHA_TYPE_224:
  1407. case CCP_SHA_TYPE_256:
  1408. ccp_get_dm_area(&ctx, ooffset,
  1409. sha->ctx, 0,
  1410. digest_size);
  1411. break;
  1412. case CCP_SHA_TYPE_384:
  1413. case CCP_SHA_TYPE_512:
  1414. ccp_get_dm_area(&ctx, 0,
  1415. sha->ctx, LSB_ITEM_SIZE - ooffset,
  1416. LSB_ITEM_SIZE);
  1417. ccp_get_dm_area(&ctx, LSB_ITEM_SIZE + ooffset,
  1418. sha->ctx, 0,
  1419. LSB_ITEM_SIZE - ooffset);
  1420. break;
  1421. default:
  1422. ret = -EINVAL;
  1423. goto e_ctx;
  1424. }
  1425. } else {
  1426. /* Stash the context */
  1427. ccp_get_dm_area(&ctx, 0, sha->ctx, 0,
  1428. sb_count * CCP_SB_BYTES);
  1429. }
  1430. if (sha->final && sha->opad) {
  1431. /* HMAC operation, recursively perform final SHA */
  1432. struct ccp_cmd hmac_cmd;
  1433. struct scatterlist sg;
  1434. u8 *hmac_buf;
  1435. if (sha->opad_len != block_size) {
  1436. ret = -EINVAL;
  1437. goto e_data;
  1438. }
  1439. hmac_buf = kmalloc(block_size + digest_size, GFP_KERNEL);
  1440. if (!hmac_buf) {
  1441. ret = -ENOMEM;
  1442. goto e_data;
  1443. }
  1444. sg_init_one(&sg, hmac_buf, block_size + digest_size);
  1445. scatterwalk_map_and_copy(hmac_buf, sha->opad, 0, block_size, 0);
  1446. switch (sha->type) {
  1447. case CCP_SHA_TYPE_1:
  1448. case CCP_SHA_TYPE_224:
  1449. case CCP_SHA_TYPE_256:
  1450. memcpy(hmac_buf + block_size,
  1451. ctx.address + ooffset,
  1452. digest_size);
  1453. break;
  1454. case CCP_SHA_TYPE_384:
  1455. case CCP_SHA_TYPE_512:
  1456. memcpy(hmac_buf + block_size,
  1457. ctx.address + LSB_ITEM_SIZE + ooffset,
  1458. LSB_ITEM_SIZE);
  1459. memcpy(hmac_buf + block_size +
  1460. (LSB_ITEM_SIZE - ooffset),
  1461. ctx.address,
  1462. LSB_ITEM_SIZE);
  1463. break;
  1464. default:
  1465. ret = -EINVAL;
  1466. goto e_ctx;
  1467. }
  1468. memset(&hmac_cmd, 0, sizeof(hmac_cmd));
  1469. hmac_cmd.engine = CCP_ENGINE_SHA;
  1470. hmac_cmd.u.sha.type = sha->type;
  1471. hmac_cmd.u.sha.ctx = sha->ctx;
  1472. hmac_cmd.u.sha.ctx_len = sha->ctx_len;
  1473. hmac_cmd.u.sha.src = &sg;
  1474. hmac_cmd.u.sha.src_len = block_size + digest_size;
  1475. hmac_cmd.u.sha.opad = NULL;
  1476. hmac_cmd.u.sha.opad_len = 0;
  1477. hmac_cmd.u.sha.first = 1;
  1478. hmac_cmd.u.sha.final = 1;
  1479. hmac_cmd.u.sha.msg_bits = (block_size + digest_size) << 3;
  1480. ret = ccp_run_sha_cmd(cmd_q, &hmac_cmd);
  1481. if (ret)
  1482. cmd->engine_error = hmac_cmd.engine_error;
  1483. kfree(hmac_buf);
  1484. }
  1485. e_data:
  1486. if (sha->src)
  1487. ccp_free_data(&src, cmd_q);
  1488. e_ctx:
  1489. ccp_dm_free(&ctx);
  1490. return ret;
  1491. }
  1492. static int ccp_run_rsa_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1493. {
  1494. struct ccp_rsa_engine *rsa = &cmd->u.rsa;
  1495. struct ccp_dm_workarea exp, src, dst;
  1496. struct ccp_op op;
  1497. unsigned int sb_count, i_len, o_len;
  1498. int ret;
  1499. /* Check against the maximum allowable size, in bits */
  1500. if (rsa->key_size > cmd_q->ccp->vdata->rsamax)
  1501. return -EINVAL;
  1502. if (!rsa->exp || !rsa->mod || !rsa->src || !rsa->dst)
  1503. return -EINVAL;
  1504. memset(&op, 0, sizeof(op));
  1505. op.cmd_q = cmd_q;
  1506. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  1507. /* The RSA modulus must precede the message being acted upon, so
  1508. * it must be copied to a DMA area where the message and the
  1509. * modulus can be concatenated. Therefore the input buffer
  1510. * length required is twice the output buffer length (which
  1511. * must be a multiple of 256-bits). Compute o_len, i_len in bytes.
  1512. * Buffer sizes must be a multiple of 32 bytes; rounding up may be
  1513. * required.
  1514. */
  1515. o_len = 32 * ((rsa->key_size + 255) / 256);
  1516. i_len = o_len * 2;
  1517. sb_count = 0;
  1518. if (cmd_q->ccp->vdata->version < CCP_VERSION(5, 0)) {
  1519. /* sb_count is the number of storage block slots required
  1520. * for the modulus.
  1521. */
  1522. sb_count = o_len / CCP_SB_BYTES;
  1523. op.sb_key = cmd_q->ccp->vdata->perform->sballoc(cmd_q,
  1524. sb_count);
  1525. if (!op.sb_key)
  1526. return -EIO;
  1527. } else {
  1528. /* A version 5 device allows a modulus size that will not fit
  1529. * in the LSB, so the command will transfer it from memory.
  1530. * Set the sb key to the default, even though it's not used.
  1531. */
  1532. op.sb_key = cmd_q->sb_key;
  1533. }
  1534. /* The RSA exponent must be in little endian format. Reverse its
  1535. * byte order.
  1536. */
  1537. ret = ccp_init_dm_workarea(&exp, cmd_q, o_len, DMA_TO_DEVICE);
  1538. if (ret)
  1539. goto e_sb;
  1540. ret = ccp_reverse_set_dm_area(&exp, 0, rsa->exp, 0, rsa->exp_len);
  1541. if (ret)
  1542. goto e_exp;
  1543. if (cmd_q->ccp->vdata->version < CCP_VERSION(5, 0)) {
  1544. /* Copy the exponent to the local storage block, using
  1545. * as many 32-byte blocks as were allocated above. It's
  1546. * already little endian, so no further change is required.
  1547. */
  1548. ret = ccp_copy_to_sb(cmd_q, &exp, op.jobid, op.sb_key,
  1549. CCP_PASSTHRU_BYTESWAP_NOOP);
  1550. if (ret) {
  1551. cmd->engine_error = cmd_q->cmd_error;
  1552. goto e_exp;
  1553. }
  1554. } else {
  1555. /* The exponent can be retrieved from memory via DMA. */
  1556. op.exp.u.dma.address = exp.dma.address;
  1557. op.exp.u.dma.offset = 0;
  1558. }
  1559. /* Concatenate the modulus and the message. Both the modulus and
  1560. * the operands must be in little endian format. Since the input
  1561. * is in big endian format it must be converted.
  1562. */
  1563. ret = ccp_init_dm_workarea(&src, cmd_q, i_len, DMA_TO_DEVICE);
  1564. if (ret)
  1565. goto e_exp;
  1566. ret = ccp_reverse_set_dm_area(&src, 0, rsa->mod, 0, rsa->mod_len);
  1567. if (ret)
  1568. goto e_src;
  1569. ret = ccp_reverse_set_dm_area(&src, o_len, rsa->src, 0, rsa->src_len);
  1570. if (ret)
  1571. goto e_src;
  1572. /* Prepare the output area for the operation */
  1573. ret = ccp_init_dm_workarea(&dst, cmd_q, o_len, DMA_FROM_DEVICE);
  1574. if (ret)
  1575. goto e_src;
  1576. op.soc = 1;
  1577. op.src.u.dma.address = src.dma.address;
  1578. op.src.u.dma.offset = 0;
  1579. op.src.u.dma.length = i_len;
  1580. op.dst.u.dma.address = dst.dma.address;
  1581. op.dst.u.dma.offset = 0;
  1582. op.dst.u.dma.length = o_len;
  1583. op.u.rsa.mod_size = rsa->key_size;
  1584. op.u.rsa.input_len = i_len;
  1585. ret = cmd_q->ccp->vdata->perform->rsa(&op);
  1586. if (ret) {
  1587. cmd->engine_error = cmd_q->cmd_error;
  1588. goto e_dst;
  1589. }
  1590. ccp_reverse_get_dm_area(&dst, 0, rsa->dst, 0, rsa->mod_len);
  1591. e_dst:
  1592. ccp_dm_free(&dst);
  1593. e_src:
  1594. ccp_dm_free(&src);
  1595. e_exp:
  1596. ccp_dm_free(&exp);
  1597. e_sb:
  1598. if (sb_count)
  1599. cmd_q->ccp->vdata->perform->sbfree(cmd_q, op.sb_key, sb_count);
  1600. return ret;
  1601. }
  1602. static int ccp_run_passthru_cmd(struct ccp_cmd_queue *cmd_q,
  1603. struct ccp_cmd *cmd)
  1604. {
  1605. struct ccp_passthru_engine *pt = &cmd->u.passthru;
  1606. struct ccp_dm_workarea mask;
  1607. struct ccp_data src, dst;
  1608. struct ccp_op op;
  1609. bool in_place = false;
  1610. unsigned int i;
  1611. int ret = 0;
  1612. if (!pt->final && (pt->src_len & (CCP_PASSTHRU_BLOCKSIZE - 1)))
  1613. return -EINVAL;
  1614. if (!pt->src || !pt->dst)
  1615. return -EINVAL;
  1616. if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
  1617. if (pt->mask_len != CCP_PASSTHRU_MASKSIZE)
  1618. return -EINVAL;
  1619. if (!pt->mask)
  1620. return -EINVAL;
  1621. }
  1622. BUILD_BUG_ON(CCP_PASSTHRU_SB_COUNT != 1);
  1623. memset(&op, 0, sizeof(op));
  1624. op.cmd_q = cmd_q;
  1625. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  1626. if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
  1627. /* Load the mask */
  1628. op.sb_key = cmd_q->sb_key;
  1629. ret = ccp_init_dm_workarea(&mask, cmd_q,
  1630. CCP_PASSTHRU_SB_COUNT *
  1631. CCP_SB_BYTES,
  1632. DMA_TO_DEVICE);
  1633. if (ret)
  1634. return ret;
  1635. ccp_set_dm_area(&mask, 0, pt->mask, 0, pt->mask_len);
  1636. ret = ccp_copy_to_sb(cmd_q, &mask, op.jobid, op.sb_key,
  1637. CCP_PASSTHRU_BYTESWAP_NOOP);
  1638. if (ret) {
  1639. cmd->engine_error = cmd_q->cmd_error;
  1640. goto e_mask;
  1641. }
  1642. }
  1643. /* Prepare the input and output data workareas. For in-place
  1644. * operations we need to set the dma direction to BIDIRECTIONAL
  1645. * and copy the src workarea to the dst workarea.
  1646. */
  1647. if (sg_virt(pt->src) == sg_virt(pt->dst))
  1648. in_place = true;
  1649. ret = ccp_init_data(&src, cmd_q, pt->src, pt->src_len,
  1650. CCP_PASSTHRU_MASKSIZE,
  1651. in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  1652. if (ret)
  1653. goto e_mask;
  1654. if (in_place) {
  1655. dst = src;
  1656. } else {
  1657. ret = ccp_init_data(&dst, cmd_q, pt->dst, pt->src_len,
  1658. CCP_PASSTHRU_MASKSIZE, DMA_FROM_DEVICE);
  1659. if (ret)
  1660. goto e_src;
  1661. }
  1662. /* Send data to the CCP Passthru engine
  1663. * Because the CCP engine works on a single source and destination
  1664. * dma address at a time, each entry in the source scatterlist
  1665. * (after the dma_map_sg call) must be less than or equal to the
  1666. * (remaining) length in the destination scatterlist entry and the
  1667. * length must be a multiple of CCP_PASSTHRU_BLOCKSIZE
  1668. */
  1669. dst.sg_wa.sg_used = 0;
  1670. for (i = 1; i <= src.sg_wa.dma_count; i++) {
  1671. if (!dst.sg_wa.sg ||
  1672. (dst.sg_wa.sg->length < src.sg_wa.sg->length)) {
  1673. ret = -EINVAL;
  1674. goto e_dst;
  1675. }
  1676. if (i == src.sg_wa.dma_count) {
  1677. op.eom = 1;
  1678. op.soc = 1;
  1679. }
  1680. op.src.type = CCP_MEMTYPE_SYSTEM;
  1681. op.src.u.dma.address = sg_dma_address(src.sg_wa.sg);
  1682. op.src.u.dma.offset = 0;
  1683. op.src.u.dma.length = sg_dma_len(src.sg_wa.sg);
  1684. op.dst.type = CCP_MEMTYPE_SYSTEM;
  1685. op.dst.u.dma.address = sg_dma_address(dst.sg_wa.sg);
  1686. op.dst.u.dma.offset = dst.sg_wa.sg_used;
  1687. op.dst.u.dma.length = op.src.u.dma.length;
  1688. ret = cmd_q->ccp->vdata->perform->passthru(&op);
  1689. if (ret) {
  1690. cmd->engine_error = cmd_q->cmd_error;
  1691. goto e_dst;
  1692. }
  1693. dst.sg_wa.sg_used += src.sg_wa.sg->length;
  1694. if (dst.sg_wa.sg_used == dst.sg_wa.sg->length) {
  1695. dst.sg_wa.sg = sg_next(dst.sg_wa.sg);
  1696. dst.sg_wa.sg_used = 0;
  1697. }
  1698. src.sg_wa.sg = sg_next(src.sg_wa.sg);
  1699. }
  1700. e_dst:
  1701. if (!in_place)
  1702. ccp_free_data(&dst, cmd_q);
  1703. e_src:
  1704. ccp_free_data(&src, cmd_q);
  1705. e_mask:
  1706. if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP)
  1707. ccp_dm_free(&mask);
  1708. return ret;
  1709. }
  1710. static int ccp_run_passthru_nomap_cmd(struct ccp_cmd_queue *cmd_q,
  1711. struct ccp_cmd *cmd)
  1712. {
  1713. struct ccp_passthru_nomap_engine *pt = &cmd->u.passthru_nomap;
  1714. struct ccp_dm_workarea mask;
  1715. struct ccp_op op;
  1716. int ret;
  1717. if (!pt->final && (pt->src_len & (CCP_PASSTHRU_BLOCKSIZE - 1)))
  1718. return -EINVAL;
  1719. if (!pt->src_dma || !pt->dst_dma)
  1720. return -EINVAL;
  1721. if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
  1722. if (pt->mask_len != CCP_PASSTHRU_MASKSIZE)
  1723. return -EINVAL;
  1724. if (!pt->mask)
  1725. return -EINVAL;
  1726. }
  1727. BUILD_BUG_ON(CCP_PASSTHRU_SB_COUNT != 1);
  1728. memset(&op, 0, sizeof(op));
  1729. op.cmd_q = cmd_q;
  1730. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  1731. if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
  1732. /* Load the mask */
  1733. op.sb_key = cmd_q->sb_key;
  1734. mask.length = pt->mask_len;
  1735. mask.dma.address = pt->mask;
  1736. mask.dma.length = pt->mask_len;
  1737. ret = ccp_copy_to_sb(cmd_q, &mask, op.jobid, op.sb_key,
  1738. CCP_PASSTHRU_BYTESWAP_NOOP);
  1739. if (ret) {
  1740. cmd->engine_error = cmd_q->cmd_error;
  1741. return ret;
  1742. }
  1743. }
  1744. /* Send data to the CCP Passthru engine */
  1745. op.eom = 1;
  1746. op.soc = 1;
  1747. op.src.type = CCP_MEMTYPE_SYSTEM;
  1748. op.src.u.dma.address = pt->src_dma;
  1749. op.src.u.dma.offset = 0;
  1750. op.src.u.dma.length = pt->src_len;
  1751. op.dst.type = CCP_MEMTYPE_SYSTEM;
  1752. op.dst.u.dma.address = pt->dst_dma;
  1753. op.dst.u.dma.offset = 0;
  1754. op.dst.u.dma.length = pt->src_len;
  1755. ret = cmd_q->ccp->vdata->perform->passthru(&op);
  1756. if (ret)
  1757. cmd->engine_error = cmd_q->cmd_error;
  1758. return ret;
  1759. }
  1760. static int ccp_run_ecc_mm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1761. {
  1762. struct ccp_ecc_engine *ecc = &cmd->u.ecc;
  1763. struct ccp_dm_workarea src, dst;
  1764. struct ccp_op op;
  1765. int ret;
  1766. u8 *save;
  1767. if (!ecc->u.mm.operand_1 ||
  1768. (ecc->u.mm.operand_1_len > CCP_ECC_MODULUS_BYTES))
  1769. return -EINVAL;
  1770. if (ecc->function != CCP_ECC_FUNCTION_MINV_384BIT)
  1771. if (!ecc->u.mm.operand_2 ||
  1772. (ecc->u.mm.operand_2_len > CCP_ECC_MODULUS_BYTES))
  1773. return -EINVAL;
  1774. if (!ecc->u.mm.result ||
  1775. (ecc->u.mm.result_len < CCP_ECC_MODULUS_BYTES))
  1776. return -EINVAL;
  1777. memset(&op, 0, sizeof(op));
  1778. op.cmd_q = cmd_q;
  1779. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  1780. /* Concatenate the modulus and the operands. Both the modulus and
  1781. * the operands must be in little endian format. Since the input
  1782. * is in big endian format it must be converted and placed in a
  1783. * fixed length buffer.
  1784. */
  1785. ret = ccp_init_dm_workarea(&src, cmd_q, CCP_ECC_SRC_BUF_SIZE,
  1786. DMA_TO_DEVICE);
  1787. if (ret)
  1788. return ret;
  1789. /* Save the workarea address since it is updated in order to perform
  1790. * the concatenation
  1791. */
  1792. save = src.address;
  1793. /* Copy the ECC modulus */
  1794. ret = ccp_reverse_set_dm_area(&src, 0, ecc->mod, 0, ecc->mod_len);
  1795. if (ret)
  1796. goto e_src;
  1797. src.address += CCP_ECC_OPERAND_SIZE;
  1798. /* Copy the first operand */
  1799. ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.mm.operand_1, 0,
  1800. ecc->u.mm.operand_1_len);
  1801. if (ret)
  1802. goto e_src;
  1803. src.address += CCP_ECC_OPERAND_SIZE;
  1804. if (ecc->function != CCP_ECC_FUNCTION_MINV_384BIT) {
  1805. /* Copy the second operand */
  1806. ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.mm.operand_2, 0,
  1807. ecc->u.mm.operand_2_len);
  1808. if (ret)
  1809. goto e_src;
  1810. src.address += CCP_ECC_OPERAND_SIZE;
  1811. }
  1812. /* Restore the workarea address */
  1813. src.address = save;
  1814. /* Prepare the output area for the operation */
  1815. ret = ccp_init_dm_workarea(&dst, cmd_q, CCP_ECC_DST_BUF_SIZE,
  1816. DMA_FROM_DEVICE);
  1817. if (ret)
  1818. goto e_src;
  1819. op.soc = 1;
  1820. op.src.u.dma.address = src.dma.address;
  1821. op.src.u.dma.offset = 0;
  1822. op.src.u.dma.length = src.length;
  1823. op.dst.u.dma.address = dst.dma.address;
  1824. op.dst.u.dma.offset = 0;
  1825. op.dst.u.dma.length = dst.length;
  1826. op.u.ecc.function = cmd->u.ecc.function;
  1827. ret = cmd_q->ccp->vdata->perform->ecc(&op);
  1828. if (ret) {
  1829. cmd->engine_error = cmd_q->cmd_error;
  1830. goto e_dst;
  1831. }
  1832. ecc->ecc_result = le16_to_cpup(
  1833. (const __le16 *)(dst.address + CCP_ECC_RESULT_OFFSET));
  1834. if (!(ecc->ecc_result & CCP_ECC_RESULT_SUCCESS)) {
  1835. ret = -EIO;
  1836. goto e_dst;
  1837. }
  1838. /* Save the ECC result */
  1839. ccp_reverse_get_dm_area(&dst, 0, ecc->u.mm.result, 0,
  1840. CCP_ECC_MODULUS_BYTES);
  1841. e_dst:
  1842. ccp_dm_free(&dst);
  1843. e_src:
  1844. ccp_dm_free(&src);
  1845. return ret;
  1846. }
  1847. static int ccp_run_ecc_pm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1848. {
  1849. struct ccp_ecc_engine *ecc = &cmd->u.ecc;
  1850. struct ccp_dm_workarea src, dst;
  1851. struct ccp_op op;
  1852. int ret;
  1853. u8 *save;
  1854. if (!ecc->u.pm.point_1.x ||
  1855. (ecc->u.pm.point_1.x_len > CCP_ECC_MODULUS_BYTES) ||
  1856. !ecc->u.pm.point_1.y ||
  1857. (ecc->u.pm.point_1.y_len > CCP_ECC_MODULUS_BYTES))
  1858. return -EINVAL;
  1859. if (ecc->function == CCP_ECC_FUNCTION_PADD_384BIT) {
  1860. if (!ecc->u.pm.point_2.x ||
  1861. (ecc->u.pm.point_2.x_len > CCP_ECC_MODULUS_BYTES) ||
  1862. !ecc->u.pm.point_2.y ||
  1863. (ecc->u.pm.point_2.y_len > CCP_ECC_MODULUS_BYTES))
  1864. return -EINVAL;
  1865. } else {
  1866. if (!ecc->u.pm.domain_a ||
  1867. (ecc->u.pm.domain_a_len > CCP_ECC_MODULUS_BYTES))
  1868. return -EINVAL;
  1869. if (ecc->function == CCP_ECC_FUNCTION_PMUL_384BIT)
  1870. if (!ecc->u.pm.scalar ||
  1871. (ecc->u.pm.scalar_len > CCP_ECC_MODULUS_BYTES))
  1872. return -EINVAL;
  1873. }
  1874. if (!ecc->u.pm.result.x ||
  1875. (ecc->u.pm.result.x_len < CCP_ECC_MODULUS_BYTES) ||
  1876. !ecc->u.pm.result.y ||
  1877. (ecc->u.pm.result.y_len < CCP_ECC_MODULUS_BYTES))
  1878. return -EINVAL;
  1879. memset(&op, 0, sizeof(op));
  1880. op.cmd_q = cmd_q;
  1881. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  1882. /* Concatenate the modulus and the operands. Both the modulus and
  1883. * the operands must be in little endian format. Since the input
  1884. * is in big endian format it must be converted and placed in a
  1885. * fixed length buffer.
  1886. */
  1887. ret = ccp_init_dm_workarea(&src, cmd_q, CCP_ECC_SRC_BUF_SIZE,
  1888. DMA_TO_DEVICE);
  1889. if (ret)
  1890. return ret;
  1891. /* Save the workarea address since it is updated in order to perform
  1892. * the concatenation
  1893. */
  1894. save = src.address;
  1895. /* Copy the ECC modulus */
  1896. ret = ccp_reverse_set_dm_area(&src, 0, ecc->mod, 0, ecc->mod_len);
  1897. if (ret)
  1898. goto e_src;
  1899. src.address += CCP_ECC_OPERAND_SIZE;
  1900. /* Copy the first point X and Y coordinate */
  1901. ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.point_1.x, 0,
  1902. ecc->u.pm.point_1.x_len);
  1903. if (ret)
  1904. goto e_src;
  1905. src.address += CCP_ECC_OPERAND_SIZE;
  1906. ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.point_1.y, 0,
  1907. ecc->u.pm.point_1.y_len);
  1908. if (ret)
  1909. goto e_src;
  1910. src.address += CCP_ECC_OPERAND_SIZE;
  1911. /* Set the first point Z coordinate to 1 */
  1912. *src.address = 0x01;
  1913. src.address += CCP_ECC_OPERAND_SIZE;
  1914. if (ecc->function == CCP_ECC_FUNCTION_PADD_384BIT) {
  1915. /* Copy the second point X and Y coordinate */
  1916. ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.point_2.x, 0,
  1917. ecc->u.pm.point_2.x_len);
  1918. if (ret)
  1919. goto e_src;
  1920. src.address += CCP_ECC_OPERAND_SIZE;
  1921. ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.point_2.y, 0,
  1922. ecc->u.pm.point_2.y_len);
  1923. if (ret)
  1924. goto e_src;
  1925. src.address += CCP_ECC_OPERAND_SIZE;
  1926. /* Set the second point Z coordinate to 1 */
  1927. *src.address = 0x01;
  1928. src.address += CCP_ECC_OPERAND_SIZE;
  1929. } else {
  1930. /* Copy the Domain "a" parameter */
  1931. ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.domain_a, 0,
  1932. ecc->u.pm.domain_a_len);
  1933. if (ret)
  1934. goto e_src;
  1935. src.address += CCP_ECC_OPERAND_SIZE;
  1936. if (ecc->function == CCP_ECC_FUNCTION_PMUL_384BIT) {
  1937. /* Copy the scalar value */
  1938. ret = ccp_reverse_set_dm_area(&src, 0,
  1939. ecc->u.pm.scalar, 0,
  1940. ecc->u.pm.scalar_len);
  1941. if (ret)
  1942. goto e_src;
  1943. src.address += CCP_ECC_OPERAND_SIZE;
  1944. }
  1945. }
  1946. /* Restore the workarea address */
  1947. src.address = save;
  1948. /* Prepare the output area for the operation */
  1949. ret = ccp_init_dm_workarea(&dst, cmd_q, CCP_ECC_DST_BUF_SIZE,
  1950. DMA_FROM_DEVICE);
  1951. if (ret)
  1952. goto e_src;
  1953. op.soc = 1;
  1954. op.src.u.dma.address = src.dma.address;
  1955. op.src.u.dma.offset = 0;
  1956. op.src.u.dma.length = src.length;
  1957. op.dst.u.dma.address = dst.dma.address;
  1958. op.dst.u.dma.offset = 0;
  1959. op.dst.u.dma.length = dst.length;
  1960. op.u.ecc.function = cmd->u.ecc.function;
  1961. ret = cmd_q->ccp->vdata->perform->ecc(&op);
  1962. if (ret) {
  1963. cmd->engine_error = cmd_q->cmd_error;
  1964. goto e_dst;
  1965. }
  1966. ecc->ecc_result = le16_to_cpup(
  1967. (const __le16 *)(dst.address + CCP_ECC_RESULT_OFFSET));
  1968. if (!(ecc->ecc_result & CCP_ECC_RESULT_SUCCESS)) {
  1969. ret = -EIO;
  1970. goto e_dst;
  1971. }
  1972. /* Save the workarea address since it is updated as we walk through
  1973. * to copy the point math result
  1974. */
  1975. save = dst.address;
  1976. /* Save the ECC result X and Y coordinates */
  1977. ccp_reverse_get_dm_area(&dst, 0, ecc->u.pm.result.x, 0,
  1978. CCP_ECC_MODULUS_BYTES);
  1979. dst.address += CCP_ECC_OUTPUT_SIZE;
  1980. ccp_reverse_get_dm_area(&dst, 0, ecc->u.pm.result.y, 0,
  1981. CCP_ECC_MODULUS_BYTES);
  1982. dst.address += CCP_ECC_OUTPUT_SIZE;
  1983. /* Restore the workarea address */
  1984. dst.address = save;
  1985. e_dst:
  1986. ccp_dm_free(&dst);
  1987. e_src:
  1988. ccp_dm_free(&src);
  1989. return ret;
  1990. }
  1991. static int ccp_run_ecc_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1992. {
  1993. struct ccp_ecc_engine *ecc = &cmd->u.ecc;
  1994. ecc->ecc_result = 0;
  1995. if (!ecc->mod ||
  1996. (ecc->mod_len > CCP_ECC_MODULUS_BYTES))
  1997. return -EINVAL;
  1998. switch (ecc->function) {
  1999. case CCP_ECC_FUNCTION_MMUL_384BIT:
  2000. case CCP_ECC_FUNCTION_MADD_384BIT:
  2001. case CCP_ECC_FUNCTION_MINV_384BIT:
  2002. return ccp_run_ecc_mm_cmd(cmd_q, cmd);
  2003. case CCP_ECC_FUNCTION_PADD_384BIT:
  2004. case CCP_ECC_FUNCTION_PMUL_384BIT:
  2005. case CCP_ECC_FUNCTION_PDBL_384BIT:
  2006. return ccp_run_ecc_pm_cmd(cmd_q, cmd);
  2007. default:
  2008. return -EINVAL;
  2009. }
  2010. }
  2011. int ccp_run_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  2012. {
  2013. int ret;
  2014. cmd->engine_error = 0;
  2015. cmd_q->cmd_error = 0;
  2016. cmd_q->int_rcvd = 0;
  2017. cmd_q->free_slots = cmd_q->ccp->vdata->perform->get_free_slots(cmd_q);
  2018. switch (cmd->engine) {
  2019. case CCP_ENGINE_AES:
  2020. ret = ccp_run_aes_cmd(cmd_q, cmd);
  2021. break;
  2022. case CCP_ENGINE_XTS_AES_128:
  2023. ret = ccp_run_xts_aes_cmd(cmd_q, cmd);
  2024. break;
  2025. case CCP_ENGINE_DES3:
  2026. ret = ccp_run_des3_cmd(cmd_q, cmd);
  2027. break;
  2028. case CCP_ENGINE_SHA:
  2029. ret = ccp_run_sha_cmd(cmd_q, cmd);
  2030. break;
  2031. case CCP_ENGINE_RSA:
  2032. ret = ccp_run_rsa_cmd(cmd_q, cmd);
  2033. break;
  2034. case CCP_ENGINE_PASSTHRU:
  2035. if (cmd->flags & CCP_CMD_PASSTHRU_NO_DMA_MAP)
  2036. ret = ccp_run_passthru_nomap_cmd(cmd_q, cmd);
  2037. else
  2038. ret = ccp_run_passthru_cmd(cmd_q, cmd);
  2039. break;
  2040. case CCP_ENGINE_ECC:
  2041. ret = ccp_run_ecc_cmd(cmd_q, cmd);
  2042. break;
  2043. default:
  2044. ret = -EINVAL;
  2045. }
  2046. return ret;
  2047. }