ccp-ops.c 61 KB

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