ccp-ops.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901
  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 <linux/ccp.h>
  19. #include "ccp-dev.h"
  20. /* SHA initial context values */
  21. static const __be32 ccp_sha1_init[SHA1_DIGEST_SIZE / sizeof(__be32)] = {
  22. cpu_to_be32(SHA1_H0), cpu_to_be32(SHA1_H1),
  23. cpu_to_be32(SHA1_H2), cpu_to_be32(SHA1_H3),
  24. cpu_to_be32(SHA1_H4),
  25. };
  26. static const __be32 ccp_sha224_init[SHA256_DIGEST_SIZE / sizeof(__be32)] = {
  27. cpu_to_be32(SHA224_H0), cpu_to_be32(SHA224_H1),
  28. cpu_to_be32(SHA224_H2), cpu_to_be32(SHA224_H3),
  29. cpu_to_be32(SHA224_H4), cpu_to_be32(SHA224_H5),
  30. cpu_to_be32(SHA224_H6), cpu_to_be32(SHA224_H7),
  31. };
  32. static const __be32 ccp_sha256_init[SHA256_DIGEST_SIZE / sizeof(__be32)] = {
  33. cpu_to_be32(SHA256_H0), cpu_to_be32(SHA256_H1),
  34. cpu_to_be32(SHA256_H2), cpu_to_be32(SHA256_H3),
  35. cpu_to_be32(SHA256_H4), cpu_to_be32(SHA256_H5),
  36. cpu_to_be32(SHA256_H6), cpu_to_be32(SHA256_H7),
  37. };
  38. #define CCP_NEW_JOBID(ccp) ((ccp->vdata->version == CCP_VERSION(3, 0)) ? \
  39. ccp_gen_jobid(ccp) : 0)
  40. static u32 ccp_gen_jobid(struct ccp_device *ccp)
  41. {
  42. return atomic_inc_return(&ccp->current_id) & CCP_JOBID_MASK;
  43. }
  44. static void ccp_sg_free(struct ccp_sg_workarea *wa)
  45. {
  46. if (wa->dma_count)
  47. dma_unmap_sg(wa->dma_dev, wa->dma_sg, wa->nents, wa->dma_dir);
  48. wa->dma_count = 0;
  49. }
  50. static int ccp_init_sg_workarea(struct ccp_sg_workarea *wa, struct device *dev,
  51. struct scatterlist *sg, u64 len,
  52. enum dma_data_direction dma_dir)
  53. {
  54. memset(wa, 0, sizeof(*wa));
  55. wa->sg = sg;
  56. if (!sg)
  57. return 0;
  58. wa->nents = sg_nents_for_len(sg, len);
  59. if (wa->nents < 0)
  60. return wa->nents;
  61. wa->bytes_left = len;
  62. wa->sg_used = 0;
  63. if (len == 0)
  64. return 0;
  65. if (dma_dir == DMA_NONE)
  66. return 0;
  67. wa->dma_sg = sg;
  68. wa->dma_dev = dev;
  69. wa->dma_dir = dma_dir;
  70. wa->dma_count = dma_map_sg(dev, sg, wa->nents, dma_dir);
  71. if (!wa->dma_count)
  72. return -ENOMEM;
  73. return 0;
  74. }
  75. static void ccp_update_sg_workarea(struct ccp_sg_workarea *wa, unsigned int len)
  76. {
  77. unsigned int nbytes = min_t(u64, len, wa->bytes_left);
  78. if (!wa->sg)
  79. return;
  80. wa->sg_used += nbytes;
  81. wa->bytes_left -= nbytes;
  82. if (wa->sg_used == wa->sg->length) {
  83. wa->sg = sg_next(wa->sg);
  84. wa->sg_used = 0;
  85. }
  86. }
  87. static void ccp_dm_free(struct ccp_dm_workarea *wa)
  88. {
  89. if (wa->length <= CCP_DMAPOOL_MAX_SIZE) {
  90. if (wa->address)
  91. dma_pool_free(wa->dma_pool, wa->address,
  92. wa->dma.address);
  93. } else {
  94. if (wa->dma.address)
  95. dma_unmap_single(wa->dev, wa->dma.address, wa->length,
  96. wa->dma.dir);
  97. kfree(wa->address);
  98. }
  99. wa->address = NULL;
  100. wa->dma.address = 0;
  101. }
  102. static int ccp_init_dm_workarea(struct ccp_dm_workarea *wa,
  103. struct ccp_cmd_queue *cmd_q,
  104. unsigned int len,
  105. enum dma_data_direction dir)
  106. {
  107. memset(wa, 0, sizeof(*wa));
  108. if (!len)
  109. return 0;
  110. wa->dev = cmd_q->ccp->dev;
  111. wa->length = len;
  112. if (len <= CCP_DMAPOOL_MAX_SIZE) {
  113. wa->dma_pool = cmd_q->dma_pool;
  114. wa->address = dma_pool_alloc(wa->dma_pool, GFP_KERNEL,
  115. &wa->dma.address);
  116. if (!wa->address)
  117. return -ENOMEM;
  118. wa->dma.length = CCP_DMAPOOL_MAX_SIZE;
  119. memset(wa->address, 0, CCP_DMAPOOL_MAX_SIZE);
  120. } else {
  121. wa->address = kzalloc(len, GFP_KERNEL);
  122. if (!wa->address)
  123. return -ENOMEM;
  124. wa->dma.address = dma_map_single(wa->dev, wa->address, len,
  125. dir);
  126. if (!wa->dma.address)
  127. return -ENOMEM;
  128. wa->dma.length = len;
  129. }
  130. wa->dma.dir = dir;
  131. return 0;
  132. }
  133. static void ccp_set_dm_area(struct ccp_dm_workarea *wa, unsigned int wa_offset,
  134. struct scatterlist *sg, unsigned int sg_offset,
  135. unsigned int len)
  136. {
  137. WARN_ON(!wa->address);
  138. scatterwalk_map_and_copy(wa->address + wa_offset, sg, sg_offset, len,
  139. 0);
  140. }
  141. static void ccp_get_dm_area(struct ccp_dm_workarea *wa, unsigned int wa_offset,
  142. struct scatterlist *sg, unsigned int sg_offset,
  143. unsigned int len)
  144. {
  145. WARN_ON(!wa->address);
  146. scatterwalk_map_and_copy(wa->address + wa_offset, sg, sg_offset, len,
  147. 1);
  148. }
  149. static int ccp_reverse_set_dm_area(struct ccp_dm_workarea *wa,
  150. struct scatterlist *sg,
  151. unsigned int len, unsigned int se_len,
  152. bool sign_extend)
  153. {
  154. unsigned int nbytes, sg_offset, dm_offset, sb_len, i;
  155. u8 buffer[CCP_REVERSE_BUF_SIZE];
  156. if (WARN_ON(se_len > sizeof(buffer)))
  157. return -EINVAL;
  158. sg_offset = len;
  159. dm_offset = 0;
  160. nbytes = len;
  161. while (nbytes) {
  162. sb_len = min_t(unsigned int, nbytes, se_len);
  163. sg_offset -= sb_len;
  164. scatterwalk_map_and_copy(buffer, sg, sg_offset, sb_len, 0);
  165. for (i = 0; i < sb_len; i++)
  166. wa->address[dm_offset + i] = buffer[sb_len - i - 1];
  167. dm_offset += sb_len;
  168. nbytes -= sb_len;
  169. if ((sb_len != se_len) && sign_extend) {
  170. /* Must sign-extend to nearest sign-extend length */
  171. if (wa->address[dm_offset - 1] & 0x80)
  172. memset(wa->address + dm_offset, 0xff,
  173. se_len - sb_len);
  174. }
  175. }
  176. return 0;
  177. }
  178. static void ccp_reverse_get_dm_area(struct ccp_dm_workarea *wa,
  179. struct scatterlist *sg,
  180. unsigned int len)
  181. {
  182. unsigned int nbytes, sg_offset, dm_offset, sb_len, i;
  183. u8 buffer[CCP_REVERSE_BUF_SIZE];
  184. sg_offset = 0;
  185. dm_offset = len;
  186. nbytes = len;
  187. while (nbytes) {
  188. sb_len = min_t(unsigned int, nbytes, sizeof(buffer));
  189. dm_offset -= sb_len;
  190. for (i = 0; i < sb_len; i++)
  191. buffer[sb_len - i - 1] = wa->address[dm_offset + i];
  192. scatterwalk_map_and_copy(buffer, sg, sg_offset, sb_len, 1);
  193. sg_offset += sb_len;
  194. nbytes -= sb_len;
  195. }
  196. }
  197. static void ccp_free_data(struct ccp_data *data, struct ccp_cmd_queue *cmd_q)
  198. {
  199. ccp_dm_free(&data->dm_wa);
  200. ccp_sg_free(&data->sg_wa);
  201. }
  202. static int ccp_init_data(struct ccp_data *data, struct ccp_cmd_queue *cmd_q,
  203. struct scatterlist *sg, u64 sg_len,
  204. unsigned int dm_len,
  205. enum dma_data_direction dir)
  206. {
  207. int ret;
  208. memset(data, 0, sizeof(*data));
  209. ret = ccp_init_sg_workarea(&data->sg_wa, cmd_q->ccp->dev, sg, sg_len,
  210. dir);
  211. if (ret)
  212. goto e_err;
  213. ret = ccp_init_dm_workarea(&data->dm_wa, cmd_q, dm_len, dir);
  214. if (ret)
  215. goto e_err;
  216. return 0;
  217. e_err:
  218. ccp_free_data(data, cmd_q);
  219. return ret;
  220. }
  221. static unsigned int ccp_queue_buf(struct ccp_data *data, unsigned int from)
  222. {
  223. struct ccp_sg_workarea *sg_wa = &data->sg_wa;
  224. struct ccp_dm_workarea *dm_wa = &data->dm_wa;
  225. unsigned int buf_count, nbytes;
  226. /* Clear the buffer if setting it */
  227. if (!from)
  228. memset(dm_wa->address, 0, dm_wa->length);
  229. if (!sg_wa->sg)
  230. return 0;
  231. /* Perform the copy operation
  232. * nbytes will always be <= UINT_MAX because dm_wa->length is
  233. * an unsigned int
  234. */
  235. nbytes = min_t(u64, sg_wa->bytes_left, dm_wa->length);
  236. scatterwalk_map_and_copy(dm_wa->address, sg_wa->sg, sg_wa->sg_used,
  237. nbytes, from);
  238. /* Update the structures and generate the count */
  239. buf_count = 0;
  240. while (sg_wa->bytes_left && (buf_count < dm_wa->length)) {
  241. nbytes = min(sg_wa->sg->length - sg_wa->sg_used,
  242. dm_wa->length - buf_count);
  243. nbytes = min_t(u64, sg_wa->bytes_left, nbytes);
  244. buf_count += nbytes;
  245. ccp_update_sg_workarea(sg_wa, nbytes);
  246. }
  247. return buf_count;
  248. }
  249. static unsigned int ccp_fill_queue_buf(struct ccp_data *data)
  250. {
  251. return ccp_queue_buf(data, 0);
  252. }
  253. static unsigned int ccp_empty_queue_buf(struct ccp_data *data)
  254. {
  255. return ccp_queue_buf(data, 1);
  256. }
  257. static void ccp_prepare_data(struct ccp_data *src, struct ccp_data *dst,
  258. struct ccp_op *op, unsigned int block_size,
  259. bool blocksize_op)
  260. {
  261. unsigned int sg_src_len, sg_dst_len, op_len;
  262. /* The CCP can only DMA from/to one address each per operation. This
  263. * requires that we find the smallest DMA area between the source
  264. * and destination. The resulting len values will always be <= UINT_MAX
  265. * because the dma length is an unsigned int.
  266. */
  267. sg_src_len = sg_dma_len(src->sg_wa.sg) - src->sg_wa.sg_used;
  268. sg_src_len = min_t(u64, src->sg_wa.bytes_left, sg_src_len);
  269. if (dst) {
  270. sg_dst_len = sg_dma_len(dst->sg_wa.sg) - dst->sg_wa.sg_used;
  271. sg_dst_len = min_t(u64, src->sg_wa.bytes_left, sg_dst_len);
  272. op_len = min(sg_src_len, sg_dst_len);
  273. } else {
  274. op_len = sg_src_len;
  275. }
  276. /* The data operation length will be at least block_size in length
  277. * or the smaller of available sg room remaining for the source or
  278. * the destination
  279. */
  280. op_len = max(op_len, block_size);
  281. /* Unless we have to buffer data, there's no reason to wait */
  282. op->soc = 0;
  283. if (sg_src_len < block_size) {
  284. /* Not enough data in the sg element, so it
  285. * needs to be buffered into a blocksize chunk
  286. */
  287. int cp_len = ccp_fill_queue_buf(src);
  288. op->soc = 1;
  289. op->src.u.dma.address = src->dm_wa.dma.address;
  290. op->src.u.dma.offset = 0;
  291. op->src.u.dma.length = (blocksize_op) ? block_size : cp_len;
  292. } else {
  293. /* Enough data in the sg element, but we need to
  294. * adjust for any previously copied data
  295. */
  296. op->src.u.dma.address = sg_dma_address(src->sg_wa.sg);
  297. op->src.u.dma.offset = src->sg_wa.sg_used;
  298. op->src.u.dma.length = op_len & ~(block_size - 1);
  299. ccp_update_sg_workarea(&src->sg_wa, op->src.u.dma.length);
  300. }
  301. if (dst) {
  302. if (sg_dst_len < block_size) {
  303. /* Not enough room in the sg element or we're on the
  304. * last piece of data (when using padding), so the
  305. * output needs to be buffered into a blocksize chunk
  306. */
  307. op->soc = 1;
  308. op->dst.u.dma.address = dst->dm_wa.dma.address;
  309. op->dst.u.dma.offset = 0;
  310. op->dst.u.dma.length = op->src.u.dma.length;
  311. } else {
  312. /* Enough room in the sg element, but we need to
  313. * adjust for any previously used area
  314. */
  315. op->dst.u.dma.address = sg_dma_address(dst->sg_wa.sg);
  316. op->dst.u.dma.offset = dst->sg_wa.sg_used;
  317. op->dst.u.dma.length = op->src.u.dma.length;
  318. }
  319. }
  320. }
  321. static void ccp_process_data(struct ccp_data *src, struct ccp_data *dst,
  322. struct ccp_op *op)
  323. {
  324. op->init = 0;
  325. if (dst) {
  326. if (op->dst.u.dma.address == dst->dm_wa.dma.address)
  327. ccp_empty_queue_buf(dst);
  328. else
  329. ccp_update_sg_workarea(&dst->sg_wa,
  330. op->dst.u.dma.length);
  331. }
  332. }
  333. static int ccp_copy_to_from_sb(struct ccp_cmd_queue *cmd_q,
  334. struct ccp_dm_workarea *wa, u32 jobid, u32 sb,
  335. u32 byte_swap, bool from)
  336. {
  337. struct ccp_op op;
  338. memset(&op, 0, sizeof(op));
  339. op.cmd_q = cmd_q;
  340. op.jobid = jobid;
  341. op.eom = 1;
  342. if (from) {
  343. op.soc = 1;
  344. op.src.type = CCP_MEMTYPE_SB;
  345. op.src.u.sb = sb;
  346. op.dst.type = CCP_MEMTYPE_SYSTEM;
  347. op.dst.u.dma.address = wa->dma.address;
  348. op.dst.u.dma.length = wa->length;
  349. } else {
  350. op.src.type = CCP_MEMTYPE_SYSTEM;
  351. op.src.u.dma.address = wa->dma.address;
  352. op.src.u.dma.length = wa->length;
  353. op.dst.type = CCP_MEMTYPE_SB;
  354. op.dst.u.sb = sb;
  355. }
  356. op.u.passthru.byte_swap = byte_swap;
  357. return cmd_q->ccp->vdata->perform->passthru(&op);
  358. }
  359. static int ccp_copy_to_sb(struct ccp_cmd_queue *cmd_q,
  360. struct ccp_dm_workarea *wa, u32 jobid, u32 sb,
  361. u32 byte_swap)
  362. {
  363. return ccp_copy_to_from_sb(cmd_q, wa, jobid, sb, byte_swap, false);
  364. }
  365. static int ccp_copy_from_sb(struct ccp_cmd_queue *cmd_q,
  366. struct ccp_dm_workarea *wa, u32 jobid, u32 sb,
  367. u32 byte_swap)
  368. {
  369. return ccp_copy_to_from_sb(cmd_q, wa, jobid, sb, byte_swap, true);
  370. }
  371. static int ccp_run_aes_cmac_cmd(struct ccp_cmd_queue *cmd_q,
  372. struct ccp_cmd *cmd)
  373. {
  374. struct ccp_aes_engine *aes = &cmd->u.aes;
  375. struct ccp_dm_workarea key, ctx;
  376. struct ccp_data src;
  377. struct ccp_op op;
  378. unsigned int dm_offset;
  379. int ret;
  380. if (!((aes->key_len == AES_KEYSIZE_128) ||
  381. (aes->key_len == AES_KEYSIZE_192) ||
  382. (aes->key_len == AES_KEYSIZE_256)))
  383. return -EINVAL;
  384. if (aes->src_len & (AES_BLOCK_SIZE - 1))
  385. return -EINVAL;
  386. if (aes->iv_len != AES_BLOCK_SIZE)
  387. return -EINVAL;
  388. if (!aes->key || !aes->iv || !aes->src)
  389. return -EINVAL;
  390. if (aes->cmac_final) {
  391. if (aes->cmac_key_len != AES_BLOCK_SIZE)
  392. return -EINVAL;
  393. if (!aes->cmac_key)
  394. return -EINVAL;
  395. }
  396. BUILD_BUG_ON(CCP_AES_KEY_SB_COUNT != 1);
  397. BUILD_BUG_ON(CCP_AES_CTX_SB_COUNT != 1);
  398. ret = -EIO;
  399. memset(&op, 0, sizeof(op));
  400. op.cmd_q = cmd_q;
  401. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  402. op.sb_key = cmd_q->sb_key;
  403. op.sb_ctx = cmd_q->sb_ctx;
  404. op.init = 1;
  405. op.u.aes.type = aes->type;
  406. op.u.aes.mode = aes->mode;
  407. op.u.aes.action = aes->action;
  408. /* All supported key sizes fit in a single (32-byte) SB entry
  409. * and must be in little endian format. Use the 256-bit byte
  410. * swap passthru option to convert from big endian to little
  411. * endian.
  412. */
  413. ret = ccp_init_dm_workarea(&key, cmd_q,
  414. CCP_AES_KEY_SB_COUNT * CCP_SB_BYTES,
  415. DMA_TO_DEVICE);
  416. if (ret)
  417. return ret;
  418. dm_offset = CCP_SB_BYTES - aes->key_len;
  419. ccp_set_dm_area(&key, dm_offset, aes->key, 0, aes->key_len);
  420. ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
  421. CCP_PASSTHRU_BYTESWAP_256BIT);
  422. if (ret) {
  423. cmd->engine_error = cmd_q->cmd_error;
  424. goto e_key;
  425. }
  426. /* The AES context fits in a single (32-byte) SB entry and
  427. * must be in little endian format. Use the 256-bit byte swap
  428. * passthru option to convert from big endian to little endian.
  429. */
  430. ret = ccp_init_dm_workarea(&ctx, cmd_q,
  431. CCP_AES_CTX_SB_COUNT * CCP_SB_BYTES,
  432. DMA_BIDIRECTIONAL);
  433. if (ret)
  434. goto e_key;
  435. dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
  436. ccp_set_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
  437. ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  438. CCP_PASSTHRU_BYTESWAP_256BIT);
  439. if (ret) {
  440. cmd->engine_error = cmd_q->cmd_error;
  441. goto e_ctx;
  442. }
  443. /* Send data to the CCP AES engine */
  444. ret = ccp_init_data(&src, cmd_q, aes->src, aes->src_len,
  445. AES_BLOCK_SIZE, DMA_TO_DEVICE);
  446. if (ret)
  447. goto e_ctx;
  448. while (src.sg_wa.bytes_left) {
  449. ccp_prepare_data(&src, NULL, &op, AES_BLOCK_SIZE, true);
  450. if (aes->cmac_final && !src.sg_wa.bytes_left) {
  451. op.eom = 1;
  452. /* Push the K1/K2 key to the CCP now */
  453. ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid,
  454. op.sb_ctx,
  455. CCP_PASSTHRU_BYTESWAP_256BIT);
  456. if (ret) {
  457. cmd->engine_error = cmd_q->cmd_error;
  458. goto e_src;
  459. }
  460. ccp_set_dm_area(&ctx, 0, aes->cmac_key, 0,
  461. aes->cmac_key_len);
  462. ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  463. CCP_PASSTHRU_BYTESWAP_256BIT);
  464. if (ret) {
  465. cmd->engine_error = cmd_q->cmd_error;
  466. goto e_src;
  467. }
  468. }
  469. ret = cmd_q->ccp->vdata->perform->aes(&op);
  470. if (ret) {
  471. cmd->engine_error = cmd_q->cmd_error;
  472. goto e_src;
  473. }
  474. ccp_process_data(&src, NULL, &op);
  475. }
  476. /* Retrieve the AES context - convert from LE to BE using
  477. * 32-byte (256-bit) byteswapping
  478. */
  479. ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  480. CCP_PASSTHRU_BYTESWAP_256BIT);
  481. if (ret) {
  482. cmd->engine_error = cmd_q->cmd_error;
  483. goto e_src;
  484. }
  485. /* ...but we only need AES_BLOCK_SIZE bytes */
  486. dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
  487. ccp_get_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
  488. e_src:
  489. ccp_free_data(&src, cmd_q);
  490. e_ctx:
  491. ccp_dm_free(&ctx);
  492. e_key:
  493. ccp_dm_free(&key);
  494. return ret;
  495. }
  496. static int ccp_run_aes_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  497. {
  498. struct ccp_aes_engine *aes = &cmd->u.aes;
  499. struct ccp_dm_workarea key, ctx;
  500. struct ccp_data src, dst;
  501. struct ccp_op op;
  502. unsigned int dm_offset;
  503. bool in_place = false;
  504. int ret;
  505. if (aes->mode == CCP_AES_MODE_CMAC)
  506. return ccp_run_aes_cmac_cmd(cmd_q, cmd);
  507. if (!((aes->key_len == AES_KEYSIZE_128) ||
  508. (aes->key_len == AES_KEYSIZE_192) ||
  509. (aes->key_len == AES_KEYSIZE_256)))
  510. return -EINVAL;
  511. if (((aes->mode == CCP_AES_MODE_ECB) ||
  512. (aes->mode == CCP_AES_MODE_CBC) ||
  513. (aes->mode == CCP_AES_MODE_CFB)) &&
  514. (aes->src_len & (AES_BLOCK_SIZE - 1)))
  515. return -EINVAL;
  516. if (!aes->key || !aes->src || !aes->dst)
  517. return -EINVAL;
  518. if (aes->mode != CCP_AES_MODE_ECB) {
  519. if (aes->iv_len != AES_BLOCK_SIZE)
  520. return -EINVAL;
  521. if (!aes->iv)
  522. return -EINVAL;
  523. }
  524. BUILD_BUG_ON(CCP_AES_KEY_SB_COUNT != 1);
  525. BUILD_BUG_ON(CCP_AES_CTX_SB_COUNT != 1);
  526. ret = -EIO;
  527. memset(&op, 0, sizeof(op));
  528. op.cmd_q = cmd_q;
  529. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  530. op.sb_key = cmd_q->sb_key;
  531. op.sb_ctx = cmd_q->sb_ctx;
  532. op.init = (aes->mode == CCP_AES_MODE_ECB) ? 0 : 1;
  533. op.u.aes.type = aes->type;
  534. op.u.aes.mode = aes->mode;
  535. op.u.aes.action = aes->action;
  536. /* All supported key sizes fit in a single (32-byte) SB entry
  537. * and must be in little endian format. Use the 256-bit byte
  538. * swap passthru option to convert from big endian to little
  539. * endian.
  540. */
  541. ret = ccp_init_dm_workarea(&key, cmd_q,
  542. CCP_AES_KEY_SB_COUNT * CCP_SB_BYTES,
  543. DMA_TO_DEVICE);
  544. if (ret)
  545. return ret;
  546. dm_offset = CCP_SB_BYTES - aes->key_len;
  547. ccp_set_dm_area(&key, dm_offset, aes->key, 0, aes->key_len);
  548. ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
  549. CCP_PASSTHRU_BYTESWAP_256BIT);
  550. if (ret) {
  551. cmd->engine_error = cmd_q->cmd_error;
  552. goto e_key;
  553. }
  554. /* The AES context fits in a single (32-byte) SB entry and
  555. * must be in little endian format. Use the 256-bit byte swap
  556. * passthru option to convert from big endian to little endian.
  557. */
  558. ret = ccp_init_dm_workarea(&ctx, cmd_q,
  559. CCP_AES_CTX_SB_COUNT * CCP_SB_BYTES,
  560. DMA_BIDIRECTIONAL);
  561. if (ret)
  562. goto e_key;
  563. if (aes->mode != CCP_AES_MODE_ECB) {
  564. /* Load the AES context - convert to LE */
  565. dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
  566. ccp_set_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
  567. ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  568. CCP_PASSTHRU_BYTESWAP_256BIT);
  569. if (ret) {
  570. cmd->engine_error = cmd_q->cmd_error;
  571. goto e_ctx;
  572. }
  573. }
  574. /* Prepare the input and output data workareas. For in-place
  575. * operations we need to set the dma direction to BIDIRECTIONAL
  576. * and copy the src workarea to the dst workarea.
  577. */
  578. if (sg_virt(aes->src) == sg_virt(aes->dst))
  579. in_place = true;
  580. ret = ccp_init_data(&src, cmd_q, aes->src, aes->src_len,
  581. AES_BLOCK_SIZE,
  582. in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  583. if (ret)
  584. goto e_ctx;
  585. if (in_place) {
  586. dst = src;
  587. } else {
  588. ret = ccp_init_data(&dst, cmd_q, aes->dst, aes->src_len,
  589. AES_BLOCK_SIZE, DMA_FROM_DEVICE);
  590. if (ret)
  591. goto e_src;
  592. }
  593. /* Send data to the CCP AES engine */
  594. while (src.sg_wa.bytes_left) {
  595. ccp_prepare_data(&src, &dst, &op, AES_BLOCK_SIZE, true);
  596. if (!src.sg_wa.bytes_left) {
  597. op.eom = 1;
  598. /* Since we don't retrieve the AES context in ECB
  599. * mode we have to wait for the operation to complete
  600. * on the last piece of data
  601. */
  602. if (aes->mode == CCP_AES_MODE_ECB)
  603. op.soc = 1;
  604. }
  605. ret = cmd_q->ccp->vdata->perform->aes(&op);
  606. if (ret) {
  607. cmd->engine_error = cmd_q->cmd_error;
  608. goto e_dst;
  609. }
  610. ccp_process_data(&src, &dst, &op);
  611. }
  612. if (aes->mode != CCP_AES_MODE_ECB) {
  613. /* Retrieve the AES context - convert from LE to BE using
  614. * 32-byte (256-bit) byteswapping
  615. */
  616. ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  617. CCP_PASSTHRU_BYTESWAP_256BIT);
  618. if (ret) {
  619. cmd->engine_error = cmd_q->cmd_error;
  620. goto e_dst;
  621. }
  622. /* ...but we only need AES_BLOCK_SIZE bytes */
  623. dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
  624. ccp_get_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
  625. }
  626. e_dst:
  627. if (!in_place)
  628. ccp_free_data(&dst, cmd_q);
  629. e_src:
  630. ccp_free_data(&src, cmd_q);
  631. e_ctx:
  632. ccp_dm_free(&ctx);
  633. e_key:
  634. ccp_dm_free(&key);
  635. return ret;
  636. }
  637. static int ccp_run_xts_aes_cmd(struct ccp_cmd_queue *cmd_q,
  638. struct ccp_cmd *cmd)
  639. {
  640. struct ccp_xts_aes_engine *xts = &cmd->u.xts;
  641. struct ccp_dm_workarea key, ctx;
  642. struct ccp_data src, dst;
  643. struct ccp_op op;
  644. unsigned int unit_size, dm_offset;
  645. bool in_place = false;
  646. unsigned int sb_count;
  647. enum ccp_aes_type aestype;
  648. int ret;
  649. switch (xts->unit_size) {
  650. case CCP_XTS_AES_UNIT_SIZE_16:
  651. unit_size = 16;
  652. break;
  653. case CCP_XTS_AES_UNIT_SIZE_512:
  654. unit_size = 512;
  655. break;
  656. case CCP_XTS_AES_UNIT_SIZE_1024:
  657. unit_size = 1024;
  658. break;
  659. case CCP_XTS_AES_UNIT_SIZE_2048:
  660. unit_size = 2048;
  661. break;
  662. case CCP_XTS_AES_UNIT_SIZE_4096:
  663. unit_size = 4096;
  664. break;
  665. default:
  666. return -EINVAL;
  667. }
  668. if (xts->key_len == AES_KEYSIZE_128)
  669. aestype = CCP_AES_TYPE_128;
  670. else
  671. return -EINVAL;
  672. if (!xts->final && (xts->src_len & (AES_BLOCK_SIZE - 1)))
  673. return -EINVAL;
  674. if (xts->iv_len != AES_BLOCK_SIZE)
  675. return -EINVAL;
  676. if (!xts->key || !xts->iv || !xts->src || !xts->dst)
  677. return -EINVAL;
  678. BUILD_BUG_ON(CCP_XTS_AES_KEY_SB_COUNT != 1);
  679. BUILD_BUG_ON(CCP_XTS_AES_CTX_SB_COUNT != 1);
  680. ret = -EIO;
  681. memset(&op, 0, sizeof(op));
  682. op.cmd_q = cmd_q;
  683. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  684. op.sb_key = cmd_q->sb_key;
  685. op.sb_ctx = cmd_q->sb_ctx;
  686. op.init = 1;
  687. op.u.xts.type = aestype;
  688. op.u.xts.action = xts->action;
  689. op.u.xts.unit_size = xts->unit_size;
  690. /* A version 3 device only supports 128-bit keys, which fits into a
  691. * single SB entry. A version 5 device uses a 512-bit vector, so two
  692. * SB entries.
  693. */
  694. if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0))
  695. sb_count = CCP_XTS_AES_KEY_SB_COUNT;
  696. else
  697. sb_count = CCP5_XTS_AES_KEY_SB_COUNT;
  698. ret = ccp_init_dm_workarea(&key, cmd_q,
  699. sb_count * CCP_SB_BYTES,
  700. DMA_TO_DEVICE);
  701. if (ret)
  702. return ret;
  703. if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0)) {
  704. /* All supported key sizes must be in little endian format.
  705. * Use the 256-bit byte swap passthru option to convert from
  706. * big endian to little endian.
  707. */
  708. dm_offset = CCP_SB_BYTES - AES_KEYSIZE_128;
  709. ccp_set_dm_area(&key, dm_offset, xts->key, 0, xts->key_len);
  710. ccp_set_dm_area(&key, 0, xts->key, xts->key_len, xts->key_len);
  711. } else {
  712. /* Version 5 CCPs use a 512-bit space for the key: each portion
  713. * occupies 256 bits, or one entire slot, and is zero-padded.
  714. */
  715. unsigned int pad;
  716. dm_offset = CCP_SB_BYTES;
  717. pad = dm_offset - xts->key_len;
  718. ccp_set_dm_area(&key, pad, xts->key, 0, xts->key_len);
  719. ccp_set_dm_area(&key, dm_offset + pad, xts->key, xts->key_len,
  720. xts->key_len);
  721. }
  722. ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
  723. CCP_PASSTHRU_BYTESWAP_256BIT);
  724. if (ret) {
  725. cmd->engine_error = cmd_q->cmd_error;
  726. goto e_key;
  727. }
  728. /* The AES context fits in a single (32-byte) SB entry and
  729. * for XTS is already in little endian format so no byte swapping
  730. * is needed.
  731. */
  732. ret = ccp_init_dm_workarea(&ctx, cmd_q,
  733. CCP_XTS_AES_CTX_SB_COUNT * CCP_SB_BYTES,
  734. DMA_BIDIRECTIONAL);
  735. if (ret)
  736. goto e_key;
  737. ccp_set_dm_area(&ctx, 0, xts->iv, 0, xts->iv_len);
  738. ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  739. CCP_PASSTHRU_BYTESWAP_NOOP);
  740. if (ret) {
  741. cmd->engine_error = cmd_q->cmd_error;
  742. goto e_ctx;
  743. }
  744. /* Prepare the input and output data workareas. For in-place
  745. * operations we need to set the dma direction to BIDIRECTIONAL
  746. * and copy the src workarea to the dst workarea.
  747. */
  748. if (sg_virt(xts->src) == sg_virt(xts->dst))
  749. in_place = true;
  750. ret = ccp_init_data(&src, cmd_q, xts->src, xts->src_len,
  751. unit_size,
  752. in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  753. if (ret)
  754. goto e_ctx;
  755. if (in_place) {
  756. dst = src;
  757. } else {
  758. ret = ccp_init_data(&dst, cmd_q, xts->dst, xts->src_len,
  759. unit_size, DMA_FROM_DEVICE);
  760. if (ret)
  761. goto e_src;
  762. }
  763. /* Send data to the CCP AES engine */
  764. while (src.sg_wa.bytes_left) {
  765. ccp_prepare_data(&src, &dst, &op, unit_size, true);
  766. if (!src.sg_wa.bytes_left)
  767. op.eom = 1;
  768. ret = cmd_q->ccp->vdata->perform->xts_aes(&op);
  769. if (ret) {
  770. cmd->engine_error = cmd_q->cmd_error;
  771. goto e_dst;
  772. }
  773. ccp_process_data(&src, &dst, &op);
  774. }
  775. /* Retrieve the AES context - convert from LE to BE using
  776. * 32-byte (256-bit) byteswapping
  777. */
  778. ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  779. CCP_PASSTHRU_BYTESWAP_256BIT);
  780. if (ret) {
  781. cmd->engine_error = cmd_q->cmd_error;
  782. goto e_dst;
  783. }
  784. /* ...but we only need AES_BLOCK_SIZE bytes */
  785. dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
  786. ccp_get_dm_area(&ctx, dm_offset, xts->iv, 0, xts->iv_len);
  787. e_dst:
  788. if (!in_place)
  789. ccp_free_data(&dst, cmd_q);
  790. e_src:
  791. ccp_free_data(&src, cmd_q);
  792. e_ctx:
  793. ccp_dm_free(&ctx);
  794. e_key:
  795. ccp_dm_free(&key);
  796. return ret;
  797. }
  798. static int ccp_run_sha_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  799. {
  800. struct ccp_sha_engine *sha = &cmd->u.sha;
  801. struct ccp_dm_workarea ctx;
  802. struct ccp_data src;
  803. struct ccp_op op;
  804. unsigned int ioffset, ooffset;
  805. unsigned int digest_size;
  806. int sb_count;
  807. const void *init;
  808. u64 block_size;
  809. int ctx_size;
  810. int ret;
  811. switch (sha->type) {
  812. case CCP_SHA_TYPE_1:
  813. if (sha->ctx_len < SHA1_DIGEST_SIZE)
  814. return -EINVAL;
  815. block_size = SHA1_BLOCK_SIZE;
  816. break;
  817. case CCP_SHA_TYPE_224:
  818. if (sha->ctx_len < SHA224_DIGEST_SIZE)
  819. return -EINVAL;
  820. block_size = SHA224_BLOCK_SIZE;
  821. break;
  822. case CCP_SHA_TYPE_256:
  823. if (sha->ctx_len < SHA256_DIGEST_SIZE)
  824. return -EINVAL;
  825. block_size = SHA256_BLOCK_SIZE;
  826. break;
  827. default:
  828. return -EINVAL;
  829. }
  830. if (!sha->ctx)
  831. return -EINVAL;
  832. if (!sha->final && (sha->src_len & (block_size - 1)))
  833. return -EINVAL;
  834. /* The version 3 device can't handle zero-length input */
  835. if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0)) {
  836. if (!sha->src_len) {
  837. unsigned int digest_len;
  838. const u8 *sha_zero;
  839. /* Not final, just return */
  840. if (!sha->final)
  841. return 0;
  842. /* CCP can't do a zero length sha operation so the
  843. * caller must buffer the data.
  844. */
  845. if (sha->msg_bits)
  846. return -EINVAL;
  847. /* The CCP cannot perform zero-length sha operations
  848. * so the caller is required to buffer data for the
  849. * final operation. However, a sha operation for a
  850. * message with a total length of zero is valid so
  851. * known values are required to supply the result.
  852. */
  853. switch (sha->type) {
  854. case CCP_SHA_TYPE_1:
  855. sha_zero = sha1_zero_message_hash;
  856. digest_len = SHA1_DIGEST_SIZE;
  857. break;
  858. case CCP_SHA_TYPE_224:
  859. sha_zero = sha224_zero_message_hash;
  860. digest_len = SHA224_DIGEST_SIZE;
  861. break;
  862. case CCP_SHA_TYPE_256:
  863. sha_zero = sha256_zero_message_hash;
  864. digest_len = SHA256_DIGEST_SIZE;
  865. break;
  866. default:
  867. return -EINVAL;
  868. }
  869. scatterwalk_map_and_copy((void *)sha_zero, sha->ctx, 0,
  870. digest_len, 1);
  871. return 0;
  872. }
  873. }
  874. /* Set variables used throughout */
  875. switch (sha->type) {
  876. case CCP_SHA_TYPE_1:
  877. digest_size = SHA1_DIGEST_SIZE;
  878. init = (void *) ccp_sha1_init;
  879. ctx_size = SHA1_DIGEST_SIZE;
  880. sb_count = 1;
  881. if (cmd_q->ccp->vdata->version != CCP_VERSION(3, 0))
  882. ooffset = ioffset = CCP_SB_BYTES - SHA1_DIGEST_SIZE;
  883. else
  884. ooffset = ioffset = 0;
  885. break;
  886. case CCP_SHA_TYPE_224:
  887. digest_size = SHA224_DIGEST_SIZE;
  888. init = (void *) ccp_sha224_init;
  889. ctx_size = SHA256_DIGEST_SIZE;
  890. sb_count = 1;
  891. ioffset = 0;
  892. if (cmd_q->ccp->vdata->version != CCP_VERSION(3, 0))
  893. ooffset = CCP_SB_BYTES - SHA224_DIGEST_SIZE;
  894. else
  895. ooffset = 0;
  896. break;
  897. case CCP_SHA_TYPE_256:
  898. digest_size = SHA256_DIGEST_SIZE;
  899. init = (void *) ccp_sha256_init;
  900. ctx_size = SHA256_DIGEST_SIZE;
  901. sb_count = 1;
  902. ooffset = ioffset = 0;
  903. break;
  904. default:
  905. ret = -EINVAL;
  906. goto e_data;
  907. }
  908. /* For zero-length plaintext the src pointer is ignored;
  909. * otherwise both parts must be valid
  910. */
  911. if (sha->src_len && !sha->src)
  912. return -EINVAL;
  913. memset(&op, 0, sizeof(op));
  914. op.cmd_q = cmd_q;
  915. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  916. op.sb_ctx = cmd_q->sb_ctx; /* Pre-allocated */
  917. op.u.sha.type = sha->type;
  918. op.u.sha.msg_bits = sha->msg_bits;
  919. ret = ccp_init_dm_workarea(&ctx, cmd_q, sb_count * CCP_SB_BYTES,
  920. DMA_BIDIRECTIONAL);
  921. if (ret)
  922. return ret;
  923. if (sha->first) {
  924. switch (sha->type) {
  925. case CCP_SHA_TYPE_1:
  926. case CCP_SHA_TYPE_224:
  927. case CCP_SHA_TYPE_256:
  928. memcpy(ctx.address + ioffset, init, ctx_size);
  929. break;
  930. default:
  931. ret = -EINVAL;
  932. goto e_ctx;
  933. }
  934. } else {
  935. /* Restore the context */
  936. ccp_set_dm_area(&ctx, 0, sha->ctx, 0,
  937. sb_count * CCP_SB_BYTES);
  938. }
  939. ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  940. CCP_PASSTHRU_BYTESWAP_256BIT);
  941. if (ret) {
  942. cmd->engine_error = cmd_q->cmd_error;
  943. goto e_ctx;
  944. }
  945. if (sha->src) {
  946. /* Send data to the CCP SHA engine; block_size is set above */
  947. ret = ccp_init_data(&src, cmd_q, sha->src, sha->src_len,
  948. block_size, DMA_TO_DEVICE);
  949. if (ret)
  950. goto e_ctx;
  951. while (src.sg_wa.bytes_left) {
  952. ccp_prepare_data(&src, NULL, &op, block_size, false);
  953. if (sha->final && !src.sg_wa.bytes_left)
  954. op.eom = 1;
  955. ret = cmd_q->ccp->vdata->perform->sha(&op);
  956. if (ret) {
  957. cmd->engine_error = cmd_q->cmd_error;
  958. goto e_data;
  959. }
  960. ccp_process_data(&src, NULL, &op);
  961. }
  962. } else {
  963. op.eom = 1;
  964. ret = cmd_q->ccp->vdata->perform->sha(&op);
  965. if (ret) {
  966. cmd->engine_error = cmd_q->cmd_error;
  967. goto e_data;
  968. }
  969. }
  970. /* Retrieve the SHA context - convert from LE to BE using
  971. * 32-byte (256-bit) byteswapping to BE
  972. */
  973. ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  974. CCP_PASSTHRU_BYTESWAP_256BIT);
  975. if (ret) {
  976. cmd->engine_error = cmd_q->cmd_error;
  977. goto e_data;
  978. }
  979. if (sha->final) {
  980. /* Finishing up, so get the digest */
  981. switch (sha->type) {
  982. case CCP_SHA_TYPE_1:
  983. case CCP_SHA_TYPE_224:
  984. case CCP_SHA_TYPE_256:
  985. ccp_get_dm_area(&ctx, ooffset,
  986. sha->ctx, 0,
  987. digest_size);
  988. break;
  989. default:
  990. ret = -EINVAL;
  991. goto e_ctx;
  992. }
  993. } else {
  994. /* Stash the context */
  995. ccp_get_dm_area(&ctx, 0, sha->ctx, 0,
  996. sb_count * CCP_SB_BYTES);
  997. }
  998. if (sha->final && sha->opad) {
  999. /* HMAC operation, recursively perform final SHA */
  1000. struct ccp_cmd hmac_cmd;
  1001. struct scatterlist sg;
  1002. u8 *hmac_buf;
  1003. if (sha->opad_len != block_size) {
  1004. ret = -EINVAL;
  1005. goto e_data;
  1006. }
  1007. hmac_buf = kmalloc(block_size + digest_size, GFP_KERNEL);
  1008. if (!hmac_buf) {
  1009. ret = -ENOMEM;
  1010. goto e_data;
  1011. }
  1012. sg_init_one(&sg, hmac_buf, block_size + digest_size);
  1013. scatterwalk_map_and_copy(hmac_buf, sha->opad, 0, block_size, 0);
  1014. switch (sha->type) {
  1015. case CCP_SHA_TYPE_1:
  1016. case CCP_SHA_TYPE_224:
  1017. case CCP_SHA_TYPE_256:
  1018. memcpy(hmac_buf + block_size,
  1019. ctx.address + ooffset,
  1020. digest_size);
  1021. break;
  1022. default:
  1023. ret = -EINVAL;
  1024. goto e_ctx;
  1025. }
  1026. memset(&hmac_cmd, 0, sizeof(hmac_cmd));
  1027. hmac_cmd.engine = CCP_ENGINE_SHA;
  1028. hmac_cmd.u.sha.type = sha->type;
  1029. hmac_cmd.u.sha.ctx = sha->ctx;
  1030. hmac_cmd.u.sha.ctx_len = sha->ctx_len;
  1031. hmac_cmd.u.sha.src = &sg;
  1032. hmac_cmd.u.sha.src_len = block_size + digest_size;
  1033. hmac_cmd.u.sha.opad = NULL;
  1034. hmac_cmd.u.sha.opad_len = 0;
  1035. hmac_cmd.u.sha.first = 1;
  1036. hmac_cmd.u.sha.final = 1;
  1037. hmac_cmd.u.sha.msg_bits = (block_size + digest_size) << 3;
  1038. ret = ccp_run_sha_cmd(cmd_q, &hmac_cmd);
  1039. if (ret)
  1040. cmd->engine_error = hmac_cmd.engine_error;
  1041. kfree(hmac_buf);
  1042. }
  1043. e_data:
  1044. if (sha->src)
  1045. ccp_free_data(&src, cmd_q);
  1046. e_ctx:
  1047. ccp_dm_free(&ctx);
  1048. return ret;
  1049. }
  1050. static int ccp_run_rsa_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1051. {
  1052. struct ccp_rsa_engine *rsa = &cmd->u.rsa;
  1053. struct ccp_dm_workarea exp, src;
  1054. struct ccp_data dst;
  1055. struct ccp_op op;
  1056. unsigned int sb_count, i_len, o_len;
  1057. int ret;
  1058. if (rsa->key_size > CCP_RSA_MAX_WIDTH)
  1059. return -EINVAL;
  1060. if (!rsa->exp || !rsa->mod || !rsa->src || !rsa->dst)
  1061. return -EINVAL;
  1062. /* The RSA modulus must precede the message being acted upon, so
  1063. * it must be copied to a DMA area where the message and the
  1064. * modulus can be concatenated. Therefore the input buffer
  1065. * length required is twice the output buffer length (which
  1066. * must be a multiple of 256-bits).
  1067. */
  1068. o_len = ((rsa->key_size + 255) / 256) * 32;
  1069. i_len = o_len * 2;
  1070. sb_count = o_len / CCP_SB_BYTES;
  1071. memset(&op, 0, sizeof(op));
  1072. op.cmd_q = cmd_q;
  1073. op.jobid = ccp_gen_jobid(cmd_q->ccp);
  1074. op.sb_key = cmd_q->ccp->vdata->perform->sballoc(cmd_q, sb_count);
  1075. if (!op.sb_key)
  1076. return -EIO;
  1077. /* The RSA exponent may span multiple (32-byte) SB entries and must
  1078. * be in little endian format. Reverse copy each 32-byte chunk
  1079. * of the exponent (En chunk to E0 chunk, E(n-1) chunk to E1 chunk)
  1080. * and each byte within that chunk and do not perform any byte swap
  1081. * operations on the passthru operation.
  1082. */
  1083. ret = ccp_init_dm_workarea(&exp, cmd_q, o_len, DMA_TO_DEVICE);
  1084. if (ret)
  1085. goto e_sb;
  1086. ret = ccp_reverse_set_dm_area(&exp, rsa->exp, rsa->exp_len,
  1087. CCP_SB_BYTES, false);
  1088. if (ret)
  1089. goto e_exp;
  1090. ret = ccp_copy_to_sb(cmd_q, &exp, op.jobid, op.sb_key,
  1091. CCP_PASSTHRU_BYTESWAP_NOOP);
  1092. if (ret) {
  1093. cmd->engine_error = cmd_q->cmd_error;
  1094. goto e_exp;
  1095. }
  1096. /* Concatenate the modulus and the message. Both the modulus and
  1097. * the operands must be in little endian format. Since the input
  1098. * is in big endian format it must be converted.
  1099. */
  1100. ret = ccp_init_dm_workarea(&src, cmd_q, i_len, DMA_TO_DEVICE);
  1101. if (ret)
  1102. goto e_exp;
  1103. ret = ccp_reverse_set_dm_area(&src, rsa->mod, rsa->mod_len,
  1104. CCP_SB_BYTES, false);
  1105. if (ret)
  1106. goto e_src;
  1107. src.address += o_len; /* Adjust the address for the copy operation */
  1108. ret = ccp_reverse_set_dm_area(&src, rsa->src, rsa->src_len,
  1109. CCP_SB_BYTES, false);
  1110. if (ret)
  1111. goto e_src;
  1112. src.address -= o_len; /* Reset the address to original value */
  1113. /* Prepare the output area for the operation */
  1114. ret = ccp_init_data(&dst, cmd_q, rsa->dst, rsa->mod_len,
  1115. o_len, DMA_FROM_DEVICE);
  1116. if (ret)
  1117. goto e_src;
  1118. op.soc = 1;
  1119. op.src.u.dma.address = src.dma.address;
  1120. op.src.u.dma.offset = 0;
  1121. op.src.u.dma.length = i_len;
  1122. op.dst.u.dma.address = dst.dm_wa.dma.address;
  1123. op.dst.u.dma.offset = 0;
  1124. op.dst.u.dma.length = o_len;
  1125. op.u.rsa.mod_size = rsa->key_size;
  1126. op.u.rsa.input_len = i_len;
  1127. ret = cmd_q->ccp->vdata->perform->rsa(&op);
  1128. if (ret) {
  1129. cmd->engine_error = cmd_q->cmd_error;
  1130. goto e_dst;
  1131. }
  1132. ccp_reverse_get_dm_area(&dst.dm_wa, rsa->dst, rsa->mod_len);
  1133. e_dst:
  1134. ccp_free_data(&dst, cmd_q);
  1135. e_src:
  1136. ccp_dm_free(&src);
  1137. e_exp:
  1138. ccp_dm_free(&exp);
  1139. e_sb:
  1140. cmd_q->ccp->vdata->perform->sbfree(cmd_q, op.sb_key, sb_count);
  1141. return ret;
  1142. }
  1143. static int ccp_run_passthru_cmd(struct ccp_cmd_queue *cmd_q,
  1144. struct ccp_cmd *cmd)
  1145. {
  1146. struct ccp_passthru_engine *pt = &cmd->u.passthru;
  1147. struct ccp_dm_workarea mask;
  1148. struct ccp_data src, dst;
  1149. struct ccp_op op;
  1150. bool in_place = false;
  1151. unsigned int i;
  1152. int ret = 0;
  1153. if (!pt->final && (pt->src_len & (CCP_PASSTHRU_BLOCKSIZE - 1)))
  1154. return -EINVAL;
  1155. if (!pt->src || !pt->dst)
  1156. return -EINVAL;
  1157. if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
  1158. if (pt->mask_len != CCP_PASSTHRU_MASKSIZE)
  1159. return -EINVAL;
  1160. if (!pt->mask)
  1161. return -EINVAL;
  1162. }
  1163. BUILD_BUG_ON(CCP_PASSTHRU_SB_COUNT != 1);
  1164. memset(&op, 0, sizeof(op));
  1165. op.cmd_q = cmd_q;
  1166. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  1167. if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
  1168. /* Load the mask */
  1169. op.sb_key = cmd_q->sb_key;
  1170. ret = ccp_init_dm_workarea(&mask, cmd_q,
  1171. CCP_PASSTHRU_SB_COUNT *
  1172. CCP_SB_BYTES,
  1173. DMA_TO_DEVICE);
  1174. if (ret)
  1175. return ret;
  1176. ccp_set_dm_area(&mask, 0, pt->mask, 0, pt->mask_len);
  1177. ret = ccp_copy_to_sb(cmd_q, &mask, op.jobid, op.sb_key,
  1178. CCP_PASSTHRU_BYTESWAP_NOOP);
  1179. if (ret) {
  1180. cmd->engine_error = cmd_q->cmd_error;
  1181. goto e_mask;
  1182. }
  1183. }
  1184. /* Prepare the input and output data workareas. For in-place
  1185. * operations we need to set the dma direction to BIDIRECTIONAL
  1186. * and copy the src workarea to the dst workarea.
  1187. */
  1188. if (sg_virt(pt->src) == sg_virt(pt->dst))
  1189. in_place = true;
  1190. ret = ccp_init_data(&src, cmd_q, pt->src, pt->src_len,
  1191. CCP_PASSTHRU_MASKSIZE,
  1192. in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  1193. if (ret)
  1194. goto e_mask;
  1195. if (in_place) {
  1196. dst = src;
  1197. } else {
  1198. ret = ccp_init_data(&dst, cmd_q, pt->dst, pt->src_len,
  1199. CCP_PASSTHRU_MASKSIZE, DMA_FROM_DEVICE);
  1200. if (ret)
  1201. goto e_src;
  1202. }
  1203. /* Send data to the CCP Passthru engine
  1204. * Because the CCP engine works on a single source and destination
  1205. * dma address at a time, each entry in the source scatterlist
  1206. * (after the dma_map_sg call) must be less than or equal to the
  1207. * (remaining) length in the destination scatterlist entry and the
  1208. * length must be a multiple of CCP_PASSTHRU_BLOCKSIZE
  1209. */
  1210. dst.sg_wa.sg_used = 0;
  1211. for (i = 1; i <= src.sg_wa.dma_count; i++) {
  1212. if (!dst.sg_wa.sg ||
  1213. (dst.sg_wa.sg->length < src.sg_wa.sg->length)) {
  1214. ret = -EINVAL;
  1215. goto e_dst;
  1216. }
  1217. if (i == src.sg_wa.dma_count) {
  1218. op.eom = 1;
  1219. op.soc = 1;
  1220. }
  1221. op.src.type = CCP_MEMTYPE_SYSTEM;
  1222. op.src.u.dma.address = sg_dma_address(src.sg_wa.sg);
  1223. op.src.u.dma.offset = 0;
  1224. op.src.u.dma.length = sg_dma_len(src.sg_wa.sg);
  1225. op.dst.type = CCP_MEMTYPE_SYSTEM;
  1226. op.dst.u.dma.address = sg_dma_address(dst.sg_wa.sg);
  1227. op.dst.u.dma.offset = dst.sg_wa.sg_used;
  1228. op.dst.u.dma.length = op.src.u.dma.length;
  1229. ret = cmd_q->ccp->vdata->perform->passthru(&op);
  1230. if (ret) {
  1231. cmd->engine_error = cmd_q->cmd_error;
  1232. goto e_dst;
  1233. }
  1234. dst.sg_wa.sg_used += src.sg_wa.sg->length;
  1235. if (dst.sg_wa.sg_used == dst.sg_wa.sg->length) {
  1236. dst.sg_wa.sg = sg_next(dst.sg_wa.sg);
  1237. dst.sg_wa.sg_used = 0;
  1238. }
  1239. src.sg_wa.sg = sg_next(src.sg_wa.sg);
  1240. }
  1241. e_dst:
  1242. if (!in_place)
  1243. ccp_free_data(&dst, cmd_q);
  1244. e_src:
  1245. ccp_free_data(&src, cmd_q);
  1246. e_mask:
  1247. if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP)
  1248. ccp_dm_free(&mask);
  1249. return ret;
  1250. }
  1251. static int ccp_run_passthru_nomap_cmd(struct ccp_cmd_queue *cmd_q,
  1252. struct ccp_cmd *cmd)
  1253. {
  1254. struct ccp_passthru_nomap_engine *pt = &cmd->u.passthru_nomap;
  1255. struct ccp_dm_workarea mask;
  1256. struct ccp_op op;
  1257. int ret;
  1258. if (!pt->final && (pt->src_len & (CCP_PASSTHRU_BLOCKSIZE - 1)))
  1259. return -EINVAL;
  1260. if (!pt->src_dma || !pt->dst_dma)
  1261. return -EINVAL;
  1262. if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
  1263. if (pt->mask_len != CCP_PASSTHRU_MASKSIZE)
  1264. return -EINVAL;
  1265. if (!pt->mask)
  1266. return -EINVAL;
  1267. }
  1268. BUILD_BUG_ON(CCP_PASSTHRU_SB_COUNT != 1);
  1269. memset(&op, 0, sizeof(op));
  1270. op.cmd_q = cmd_q;
  1271. op.jobid = ccp_gen_jobid(cmd_q->ccp);
  1272. if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
  1273. /* Load the mask */
  1274. op.sb_key = cmd_q->sb_key;
  1275. mask.length = pt->mask_len;
  1276. mask.dma.address = pt->mask;
  1277. mask.dma.length = pt->mask_len;
  1278. ret = ccp_copy_to_sb(cmd_q, &mask, op.jobid, op.sb_key,
  1279. CCP_PASSTHRU_BYTESWAP_NOOP);
  1280. if (ret) {
  1281. cmd->engine_error = cmd_q->cmd_error;
  1282. return ret;
  1283. }
  1284. }
  1285. /* Send data to the CCP Passthru engine */
  1286. op.eom = 1;
  1287. op.soc = 1;
  1288. op.src.type = CCP_MEMTYPE_SYSTEM;
  1289. op.src.u.dma.address = pt->src_dma;
  1290. op.src.u.dma.offset = 0;
  1291. op.src.u.dma.length = pt->src_len;
  1292. op.dst.type = CCP_MEMTYPE_SYSTEM;
  1293. op.dst.u.dma.address = pt->dst_dma;
  1294. op.dst.u.dma.offset = 0;
  1295. op.dst.u.dma.length = pt->src_len;
  1296. ret = cmd_q->ccp->vdata->perform->passthru(&op);
  1297. if (ret)
  1298. cmd->engine_error = cmd_q->cmd_error;
  1299. return ret;
  1300. }
  1301. static int ccp_run_ecc_mm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1302. {
  1303. struct ccp_ecc_engine *ecc = &cmd->u.ecc;
  1304. struct ccp_dm_workarea src, dst;
  1305. struct ccp_op op;
  1306. int ret;
  1307. u8 *save;
  1308. if (!ecc->u.mm.operand_1 ||
  1309. (ecc->u.mm.operand_1_len > CCP_ECC_MODULUS_BYTES))
  1310. return -EINVAL;
  1311. if (ecc->function != CCP_ECC_FUNCTION_MINV_384BIT)
  1312. if (!ecc->u.mm.operand_2 ||
  1313. (ecc->u.mm.operand_2_len > CCP_ECC_MODULUS_BYTES))
  1314. return -EINVAL;
  1315. if (!ecc->u.mm.result ||
  1316. (ecc->u.mm.result_len < CCP_ECC_MODULUS_BYTES))
  1317. return -EINVAL;
  1318. memset(&op, 0, sizeof(op));
  1319. op.cmd_q = cmd_q;
  1320. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  1321. /* Concatenate the modulus and the operands. Both the modulus and
  1322. * the operands must be in little endian format. Since the input
  1323. * is in big endian format it must be converted and placed in a
  1324. * fixed length buffer.
  1325. */
  1326. ret = ccp_init_dm_workarea(&src, cmd_q, CCP_ECC_SRC_BUF_SIZE,
  1327. DMA_TO_DEVICE);
  1328. if (ret)
  1329. return ret;
  1330. /* Save the workarea address since it is updated in order to perform
  1331. * the concatenation
  1332. */
  1333. save = src.address;
  1334. /* Copy the ECC modulus */
  1335. ret = ccp_reverse_set_dm_area(&src, ecc->mod, ecc->mod_len,
  1336. CCP_ECC_OPERAND_SIZE, false);
  1337. if (ret)
  1338. goto e_src;
  1339. src.address += CCP_ECC_OPERAND_SIZE;
  1340. /* Copy the first operand */
  1341. ret = ccp_reverse_set_dm_area(&src, ecc->u.mm.operand_1,
  1342. ecc->u.mm.operand_1_len,
  1343. CCP_ECC_OPERAND_SIZE, false);
  1344. if (ret)
  1345. goto e_src;
  1346. src.address += CCP_ECC_OPERAND_SIZE;
  1347. if (ecc->function != CCP_ECC_FUNCTION_MINV_384BIT) {
  1348. /* Copy the second operand */
  1349. ret = ccp_reverse_set_dm_area(&src, ecc->u.mm.operand_2,
  1350. ecc->u.mm.operand_2_len,
  1351. CCP_ECC_OPERAND_SIZE, false);
  1352. if (ret)
  1353. goto e_src;
  1354. src.address += CCP_ECC_OPERAND_SIZE;
  1355. }
  1356. /* Restore the workarea address */
  1357. src.address = save;
  1358. /* Prepare the output area for the operation */
  1359. ret = ccp_init_dm_workarea(&dst, cmd_q, CCP_ECC_DST_BUF_SIZE,
  1360. DMA_FROM_DEVICE);
  1361. if (ret)
  1362. goto e_src;
  1363. op.soc = 1;
  1364. op.src.u.dma.address = src.dma.address;
  1365. op.src.u.dma.offset = 0;
  1366. op.src.u.dma.length = src.length;
  1367. op.dst.u.dma.address = dst.dma.address;
  1368. op.dst.u.dma.offset = 0;
  1369. op.dst.u.dma.length = dst.length;
  1370. op.u.ecc.function = cmd->u.ecc.function;
  1371. ret = cmd_q->ccp->vdata->perform->ecc(&op);
  1372. if (ret) {
  1373. cmd->engine_error = cmd_q->cmd_error;
  1374. goto e_dst;
  1375. }
  1376. ecc->ecc_result = le16_to_cpup(
  1377. (const __le16 *)(dst.address + CCP_ECC_RESULT_OFFSET));
  1378. if (!(ecc->ecc_result & CCP_ECC_RESULT_SUCCESS)) {
  1379. ret = -EIO;
  1380. goto e_dst;
  1381. }
  1382. /* Save the ECC result */
  1383. ccp_reverse_get_dm_area(&dst, ecc->u.mm.result, CCP_ECC_MODULUS_BYTES);
  1384. e_dst:
  1385. ccp_dm_free(&dst);
  1386. e_src:
  1387. ccp_dm_free(&src);
  1388. return ret;
  1389. }
  1390. static int ccp_run_ecc_pm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1391. {
  1392. struct ccp_ecc_engine *ecc = &cmd->u.ecc;
  1393. struct ccp_dm_workarea src, dst;
  1394. struct ccp_op op;
  1395. int ret;
  1396. u8 *save;
  1397. if (!ecc->u.pm.point_1.x ||
  1398. (ecc->u.pm.point_1.x_len > CCP_ECC_MODULUS_BYTES) ||
  1399. !ecc->u.pm.point_1.y ||
  1400. (ecc->u.pm.point_1.y_len > CCP_ECC_MODULUS_BYTES))
  1401. return -EINVAL;
  1402. if (ecc->function == CCP_ECC_FUNCTION_PADD_384BIT) {
  1403. if (!ecc->u.pm.point_2.x ||
  1404. (ecc->u.pm.point_2.x_len > CCP_ECC_MODULUS_BYTES) ||
  1405. !ecc->u.pm.point_2.y ||
  1406. (ecc->u.pm.point_2.y_len > CCP_ECC_MODULUS_BYTES))
  1407. return -EINVAL;
  1408. } else {
  1409. if (!ecc->u.pm.domain_a ||
  1410. (ecc->u.pm.domain_a_len > CCP_ECC_MODULUS_BYTES))
  1411. return -EINVAL;
  1412. if (ecc->function == CCP_ECC_FUNCTION_PMUL_384BIT)
  1413. if (!ecc->u.pm.scalar ||
  1414. (ecc->u.pm.scalar_len > CCP_ECC_MODULUS_BYTES))
  1415. return -EINVAL;
  1416. }
  1417. if (!ecc->u.pm.result.x ||
  1418. (ecc->u.pm.result.x_len < CCP_ECC_MODULUS_BYTES) ||
  1419. !ecc->u.pm.result.y ||
  1420. (ecc->u.pm.result.y_len < CCP_ECC_MODULUS_BYTES))
  1421. return -EINVAL;
  1422. memset(&op, 0, sizeof(op));
  1423. op.cmd_q = cmd_q;
  1424. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  1425. /* Concatenate the modulus and the operands. Both the modulus and
  1426. * the operands must be in little endian format. Since the input
  1427. * is in big endian format it must be converted and placed in a
  1428. * fixed length buffer.
  1429. */
  1430. ret = ccp_init_dm_workarea(&src, cmd_q, CCP_ECC_SRC_BUF_SIZE,
  1431. DMA_TO_DEVICE);
  1432. if (ret)
  1433. return ret;
  1434. /* Save the workarea address since it is updated in order to perform
  1435. * the concatenation
  1436. */
  1437. save = src.address;
  1438. /* Copy the ECC modulus */
  1439. ret = ccp_reverse_set_dm_area(&src, ecc->mod, ecc->mod_len,
  1440. CCP_ECC_OPERAND_SIZE, false);
  1441. if (ret)
  1442. goto e_src;
  1443. src.address += CCP_ECC_OPERAND_SIZE;
  1444. /* Copy the first point X and Y coordinate */
  1445. ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.point_1.x,
  1446. ecc->u.pm.point_1.x_len,
  1447. CCP_ECC_OPERAND_SIZE, false);
  1448. if (ret)
  1449. goto e_src;
  1450. src.address += CCP_ECC_OPERAND_SIZE;
  1451. ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.point_1.y,
  1452. ecc->u.pm.point_1.y_len,
  1453. CCP_ECC_OPERAND_SIZE, false);
  1454. if (ret)
  1455. goto e_src;
  1456. src.address += CCP_ECC_OPERAND_SIZE;
  1457. /* Set the first point Z coordinate to 1 */
  1458. *src.address = 0x01;
  1459. src.address += CCP_ECC_OPERAND_SIZE;
  1460. if (ecc->function == CCP_ECC_FUNCTION_PADD_384BIT) {
  1461. /* Copy the second point X and Y coordinate */
  1462. ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.point_2.x,
  1463. ecc->u.pm.point_2.x_len,
  1464. CCP_ECC_OPERAND_SIZE, false);
  1465. if (ret)
  1466. goto e_src;
  1467. src.address += CCP_ECC_OPERAND_SIZE;
  1468. ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.point_2.y,
  1469. ecc->u.pm.point_2.y_len,
  1470. CCP_ECC_OPERAND_SIZE, false);
  1471. if (ret)
  1472. goto e_src;
  1473. src.address += CCP_ECC_OPERAND_SIZE;
  1474. /* Set the second point Z coordinate to 1 */
  1475. *src.address = 0x01;
  1476. src.address += CCP_ECC_OPERAND_SIZE;
  1477. } else {
  1478. /* Copy the Domain "a" parameter */
  1479. ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.domain_a,
  1480. ecc->u.pm.domain_a_len,
  1481. CCP_ECC_OPERAND_SIZE, false);
  1482. if (ret)
  1483. goto e_src;
  1484. src.address += CCP_ECC_OPERAND_SIZE;
  1485. if (ecc->function == CCP_ECC_FUNCTION_PMUL_384BIT) {
  1486. /* Copy the scalar value */
  1487. ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.scalar,
  1488. ecc->u.pm.scalar_len,
  1489. CCP_ECC_OPERAND_SIZE,
  1490. false);
  1491. if (ret)
  1492. goto e_src;
  1493. src.address += CCP_ECC_OPERAND_SIZE;
  1494. }
  1495. }
  1496. /* Restore the workarea address */
  1497. src.address = save;
  1498. /* Prepare the output area for the operation */
  1499. ret = ccp_init_dm_workarea(&dst, cmd_q, CCP_ECC_DST_BUF_SIZE,
  1500. DMA_FROM_DEVICE);
  1501. if (ret)
  1502. goto e_src;
  1503. op.soc = 1;
  1504. op.src.u.dma.address = src.dma.address;
  1505. op.src.u.dma.offset = 0;
  1506. op.src.u.dma.length = src.length;
  1507. op.dst.u.dma.address = dst.dma.address;
  1508. op.dst.u.dma.offset = 0;
  1509. op.dst.u.dma.length = dst.length;
  1510. op.u.ecc.function = cmd->u.ecc.function;
  1511. ret = cmd_q->ccp->vdata->perform->ecc(&op);
  1512. if (ret) {
  1513. cmd->engine_error = cmd_q->cmd_error;
  1514. goto e_dst;
  1515. }
  1516. ecc->ecc_result = le16_to_cpup(
  1517. (const __le16 *)(dst.address + CCP_ECC_RESULT_OFFSET));
  1518. if (!(ecc->ecc_result & CCP_ECC_RESULT_SUCCESS)) {
  1519. ret = -EIO;
  1520. goto e_dst;
  1521. }
  1522. /* Save the workarea address since it is updated as we walk through
  1523. * to copy the point math result
  1524. */
  1525. save = dst.address;
  1526. /* Save the ECC result X and Y coordinates */
  1527. ccp_reverse_get_dm_area(&dst, ecc->u.pm.result.x,
  1528. CCP_ECC_MODULUS_BYTES);
  1529. dst.address += CCP_ECC_OUTPUT_SIZE;
  1530. ccp_reverse_get_dm_area(&dst, ecc->u.pm.result.y,
  1531. CCP_ECC_MODULUS_BYTES);
  1532. dst.address += CCP_ECC_OUTPUT_SIZE;
  1533. /* Restore the workarea address */
  1534. dst.address = save;
  1535. e_dst:
  1536. ccp_dm_free(&dst);
  1537. e_src:
  1538. ccp_dm_free(&src);
  1539. return ret;
  1540. }
  1541. static int ccp_run_ecc_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1542. {
  1543. struct ccp_ecc_engine *ecc = &cmd->u.ecc;
  1544. ecc->ecc_result = 0;
  1545. if (!ecc->mod ||
  1546. (ecc->mod_len > CCP_ECC_MODULUS_BYTES))
  1547. return -EINVAL;
  1548. switch (ecc->function) {
  1549. case CCP_ECC_FUNCTION_MMUL_384BIT:
  1550. case CCP_ECC_FUNCTION_MADD_384BIT:
  1551. case CCP_ECC_FUNCTION_MINV_384BIT:
  1552. return ccp_run_ecc_mm_cmd(cmd_q, cmd);
  1553. case CCP_ECC_FUNCTION_PADD_384BIT:
  1554. case CCP_ECC_FUNCTION_PMUL_384BIT:
  1555. case CCP_ECC_FUNCTION_PDBL_384BIT:
  1556. return ccp_run_ecc_pm_cmd(cmd_q, cmd);
  1557. default:
  1558. return -EINVAL;
  1559. }
  1560. }
  1561. int ccp_run_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1562. {
  1563. int ret;
  1564. cmd->engine_error = 0;
  1565. cmd_q->cmd_error = 0;
  1566. cmd_q->int_rcvd = 0;
  1567. cmd_q->free_slots = cmd_q->ccp->vdata->perform->get_free_slots(cmd_q);
  1568. switch (cmd->engine) {
  1569. case CCP_ENGINE_AES:
  1570. ret = ccp_run_aes_cmd(cmd_q, cmd);
  1571. break;
  1572. case CCP_ENGINE_XTS_AES_128:
  1573. ret = ccp_run_xts_aes_cmd(cmd_q, cmd);
  1574. break;
  1575. case CCP_ENGINE_SHA:
  1576. ret = ccp_run_sha_cmd(cmd_q, cmd);
  1577. break;
  1578. case CCP_ENGINE_RSA:
  1579. ret = ccp_run_rsa_cmd(cmd_q, cmd);
  1580. break;
  1581. case CCP_ENGINE_PASSTHRU:
  1582. if (cmd->flags & CCP_CMD_PASSTHRU_NO_DMA_MAP)
  1583. ret = ccp_run_passthru_nomap_cmd(cmd_q, cmd);
  1584. else
  1585. ret = ccp_run_passthru_cmd(cmd_q, cmd);
  1586. break;
  1587. case CCP_ENGINE_ECC:
  1588. ret = ccp_run_ecc_cmd(cmd_q, cmd);
  1589. break;
  1590. default:
  1591. ret = -EINVAL;
  1592. }
  1593. return ret;
  1594. }