ccp-ops.c 61 KB

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