ccp-ops.c 61 KB

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