ccp-ops.c 58 KB

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