ccp-ops.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910
  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. switch (aes->mode) {
  575. case CCP_AES_MODE_CFB: /* CFB128 only */
  576. case CCP_AES_MODE_CTR:
  577. op.u.aes.size = AES_BLOCK_SIZE * BITS_PER_BYTE - 1;
  578. break;
  579. default:
  580. op.u.aes.size = 0;
  581. }
  582. /* Prepare the input and output data workareas. For in-place
  583. * operations we need to set the dma direction to BIDIRECTIONAL
  584. * and copy the src workarea to the dst workarea.
  585. */
  586. if (sg_virt(aes->src) == sg_virt(aes->dst))
  587. in_place = true;
  588. ret = ccp_init_data(&src, cmd_q, aes->src, aes->src_len,
  589. AES_BLOCK_SIZE,
  590. in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  591. if (ret)
  592. goto e_ctx;
  593. if (in_place) {
  594. dst = src;
  595. } else {
  596. ret = ccp_init_data(&dst, cmd_q, aes->dst, aes->src_len,
  597. AES_BLOCK_SIZE, DMA_FROM_DEVICE);
  598. if (ret)
  599. goto e_src;
  600. }
  601. /* Send data to the CCP AES engine */
  602. while (src.sg_wa.bytes_left) {
  603. ccp_prepare_data(&src, &dst, &op, AES_BLOCK_SIZE, true);
  604. if (!src.sg_wa.bytes_left) {
  605. op.eom = 1;
  606. /* Since we don't retrieve the AES context in ECB
  607. * mode we have to wait for the operation to complete
  608. * on the last piece of data
  609. */
  610. if (aes->mode == CCP_AES_MODE_ECB)
  611. op.soc = 1;
  612. }
  613. ret = cmd_q->ccp->vdata->perform->aes(&op);
  614. if (ret) {
  615. cmd->engine_error = cmd_q->cmd_error;
  616. goto e_dst;
  617. }
  618. ccp_process_data(&src, &dst, &op);
  619. }
  620. if (aes->mode != CCP_AES_MODE_ECB) {
  621. /* Retrieve the AES context - convert from LE to BE using
  622. * 32-byte (256-bit) byteswapping
  623. */
  624. ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  625. CCP_PASSTHRU_BYTESWAP_256BIT);
  626. if (ret) {
  627. cmd->engine_error = cmd_q->cmd_error;
  628. goto e_dst;
  629. }
  630. /* ...but we only need AES_BLOCK_SIZE bytes */
  631. dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
  632. ccp_get_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
  633. }
  634. e_dst:
  635. if (!in_place)
  636. ccp_free_data(&dst, cmd_q);
  637. e_src:
  638. ccp_free_data(&src, cmd_q);
  639. e_ctx:
  640. ccp_dm_free(&ctx);
  641. e_key:
  642. ccp_dm_free(&key);
  643. return ret;
  644. }
  645. static int ccp_run_xts_aes_cmd(struct ccp_cmd_queue *cmd_q,
  646. struct ccp_cmd *cmd)
  647. {
  648. struct ccp_xts_aes_engine *xts = &cmd->u.xts;
  649. struct ccp_dm_workarea key, ctx;
  650. struct ccp_data src, dst;
  651. struct ccp_op op;
  652. unsigned int unit_size, dm_offset;
  653. bool in_place = false;
  654. unsigned int sb_count;
  655. enum ccp_aes_type aestype;
  656. int ret;
  657. switch (xts->unit_size) {
  658. case CCP_XTS_AES_UNIT_SIZE_16:
  659. unit_size = 16;
  660. break;
  661. case CCP_XTS_AES_UNIT_SIZE_512:
  662. unit_size = 512;
  663. break;
  664. case CCP_XTS_AES_UNIT_SIZE_1024:
  665. unit_size = 1024;
  666. break;
  667. case CCP_XTS_AES_UNIT_SIZE_2048:
  668. unit_size = 2048;
  669. break;
  670. case CCP_XTS_AES_UNIT_SIZE_4096:
  671. unit_size = 4096;
  672. break;
  673. default:
  674. return -EINVAL;
  675. }
  676. if (xts->key_len == AES_KEYSIZE_128)
  677. aestype = CCP_AES_TYPE_128;
  678. else
  679. return -EINVAL;
  680. if (!xts->final && (xts->src_len & (AES_BLOCK_SIZE - 1)))
  681. return -EINVAL;
  682. if (xts->iv_len != AES_BLOCK_SIZE)
  683. return -EINVAL;
  684. if (!xts->key || !xts->iv || !xts->src || !xts->dst)
  685. return -EINVAL;
  686. BUILD_BUG_ON(CCP_XTS_AES_KEY_SB_COUNT != 1);
  687. BUILD_BUG_ON(CCP_XTS_AES_CTX_SB_COUNT != 1);
  688. ret = -EIO;
  689. memset(&op, 0, sizeof(op));
  690. op.cmd_q = cmd_q;
  691. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  692. op.sb_key = cmd_q->sb_key;
  693. op.sb_ctx = cmd_q->sb_ctx;
  694. op.init = 1;
  695. op.u.xts.type = aestype;
  696. op.u.xts.action = xts->action;
  697. op.u.xts.unit_size = xts->unit_size;
  698. /* A version 3 device only supports 128-bit keys, which fits into a
  699. * single SB entry. A version 5 device uses a 512-bit vector, so two
  700. * SB entries.
  701. */
  702. if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0))
  703. sb_count = CCP_XTS_AES_KEY_SB_COUNT;
  704. else
  705. sb_count = CCP5_XTS_AES_KEY_SB_COUNT;
  706. ret = ccp_init_dm_workarea(&key, cmd_q,
  707. sb_count * CCP_SB_BYTES,
  708. DMA_TO_DEVICE);
  709. if (ret)
  710. return ret;
  711. if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0)) {
  712. /* All supported key sizes must be in little endian format.
  713. * Use the 256-bit byte swap passthru option to convert from
  714. * big endian to little endian.
  715. */
  716. dm_offset = CCP_SB_BYTES - AES_KEYSIZE_128;
  717. ccp_set_dm_area(&key, dm_offset, xts->key, 0, xts->key_len);
  718. ccp_set_dm_area(&key, 0, xts->key, xts->key_len, xts->key_len);
  719. } else {
  720. /* Version 5 CCPs use a 512-bit space for the key: each portion
  721. * occupies 256 bits, or one entire slot, and is zero-padded.
  722. */
  723. unsigned int pad;
  724. dm_offset = CCP_SB_BYTES;
  725. pad = dm_offset - xts->key_len;
  726. ccp_set_dm_area(&key, pad, xts->key, 0, xts->key_len);
  727. ccp_set_dm_area(&key, dm_offset + pad, xts->key, xts->key_len,
  728. xts->key_len);
  729. }
  730. ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
  731. CCP_PASSTHRU_BYTESWAP_256BIT);
  732. if (ret) {
  733. cmd->engine_error = cmd_q->cmd_error;
  734. goto e_key;
  735. }
  736. /* The AES context fits in a single (32-byte) SB entry and
  737. * for XTS is already in little endian format so no byte swapping
  738. * is needed.
  739. */
  740. ret = ccp_init_dm_workarea(&ctx, cmd_q,
  741. CCP_XTS_AES_CTX_SB_COUNT * CCP_SB_BYTES,
  742. DMA_BIDIRECTIONAL);
  743. if (ret)
  744. goto e_key;
  745. ccp_set_dm_area(&ctx, 0, xts->iv, 0, xts->iv_len);
  746. ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  747. CCP_PASSTHRU_BYTESWAP_NOOP);
  748. if (ret) {
  749. cmd->engine_error = cmd_q->cmd_error;
  750. goto e_ctx;
  751. }
  752. /* Prepare the input and output data workareas. For in-place
  753. * operations we need to set the dma direction to BIDIRECTIONAL
  754. * and copy the src workarea to the dst workarea.
  755. */
  756. if (sg_virt(xts->src) == sg_virt(xts->dst))
  757. in_place = true;
  758. ret = ccp_init_data(&src, cmd_q, xts->src, xts->src_len,
  759. unit_size,
  760. in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  761. if (ret)
  762. goto e_ctx;
  763. if (in_place) {
  764. dst = src;
  765. } else {
  766. ret = ccp_init_data(&dst, cmd_q, xts->dst, xts->src_len,
  767. unit_size, DMA_FROM_DEVICE);
  768. if (ret)
  769. goto e_src;
  770. }
  771. /* Send data to the CCP AES engine */
  772. while (src.sg_wa.bytes_left) {
  773. ccp_prepare_data(&src, &dst, &op, unit_size, true);
  774. if (!src.sg_wa.bytes_left)
  775. op.eom = 1;
  776. ret = cmd_q->ccp->vdata->perform->xts_aes(&op);
  777. if (ret) {
  778. cmd->engine_error = cmd_q->cmd_error;
  779. goto e_dst;
  780. }
  781. ccp_process_data(&src, &dst, &op);
  782. }
  783. /* Retrieve the AES context - convert from LE to BE using
  784. * 32-byte (256-bit) byteswapping
  785. */
  786. ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  787. CCP_PASSTHRU_BYTESWAP_256BIT);
  788. if (ret) {
  789. cmd->engine_error = cmd_q->cmd_error;
  790. goto e_dst;
  791. }
  792. /* ...but we only need AES_BLOCK_SIZE bytes */
  793. dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
  794. ccp_get_dm_area(&ctx, dm_offset, xts->iv, 0, xts->iv_len);
  795. e_dst:
  796. if (!in_place)
  797. ccp_free_data(&dst, cmd_q);
  798. e_src:
  799. ccp_free_data(&src, cmd_q);
  800. e_ctx:
  801. ccp_dm_free(&ctx);
  802. e_key:
  803. ccp_dm_free(&key);
  804. return ret;
  805. }
  806. static int ccp_run_sha_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  807. {
  808. struct ccp_sha_engine *sha = &cmd->u.sha;
  809. struct ccp_dm_workarea ctx;
  810. struct ccp_data src;
  811. struct ccp_op op;
  812. unsigned int ioffset, ooffset;
  813. unsigned int digest_size;
  814. int sb_count;
  815. const void *init;
  816. u64 block_size;
  817. int ctx_size;
  818. int ret;
  819. switch (sha->type) {
  820. case CCP_SHA_TYPE_1:
  821. if (sha->ctx_len < SHA1_DIGEST_SIZE)
  822. return -EINVAL;
  823. block_size = SHA1_BLOCK_SIZE;
  824. break;
  825. case CCP_SHA_TYPE_224:
  826. if (sha->ctx_len < SHA224_DIGEST_SIZE)
  827. return -EINVAL;
  828. block_size = SHA224_BLOCK_SIZE;
  829. break;
  830. case CCP_SHA_TYPE_256:
  831. if (sha->ctx_len < SHA256_DIGEST_SIZE)
  832. return -EINVAL;
  833. block_size = SHA256_BLOCK_SIZE;
  834. break;
  835. default:
  836. return -EINVAL;
  837. }
  838. if (!sha->ctx)
  839. return -EINVAL;
  840. if (!sha->final && (sha->src_len & (block_size - 1)))
  841. return -EINVAL;
  842. /* The version 3 device can't handle zero-length input */
  843. if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0)) {
  844. if (!sha->src_len) {
  845. unsigned int digest_len;
  846. const u8 *sha_zero;
  847. /* Not final, just return */
  848. if (!sha->final)
  849. return 0;
  850. /* CCP can't do a zero length sha operation so the
  851. * caller must buffer the data.
  852. */
  853. if (sha->msg_bits)
  854. return -EINVAL;
  855. /* The CCP cannot perform zero-length sha operations
  856. * so the caller is required to buffer data for the
  857. * final operation. However, a sha operation for a
  858. * message with a total length of zero is valid so
  859. * known values are required to supply the result.
  860. */
  861. switch (sha->type) {
  862. case CCP_SHA_TYPE_1:
  863. sha_zero = sha1_zero_message_hash;
  864. digest_len = SHA1_DIGEST_SIZE;
  865. break;
  866. case CCP_SHA_TYPE_224:
  867. sha_zero = sha224_zero_message_hash;
  868. digest_len = SHA224_DIGEST_SIZE;
  869. break;
  870. case CCP_SHA_TYPE_256:
  871. sha_zero = sha256_zero_message_hash;
  872. digest_len = SHA256_DIGEST_SIZE;
  873. break;
  874. default:
  875. return -EINVAL;
  876. }
  877. scatterwalk_map_and_copy((void *)sha_zero, sha->ctx, 0,
  878. digest_len, 1);
  879. return 0;
  880. }
  881. }
  882. /* Set variables used throughout */
  883. switch (sha->type) {
  884. case CCP_SHA_TYPE_1:
  885. digest_size = SHA1_DIGEST_SIZE;
  886. init = (void *) ccp_sha1_init;
  887. ctx_size = SHA1_DIGEST_SIZE;
  888. sb_count = 1;
  889. if (cmd_q->ccp->vdata->version != CCP_VERSION(3, 0))
  890. ooffset = ioffset = CCP_SB_BYTES - SHA1_DIGEST_SIZE;
  891. else
  892. ooffset = ioffset = 0;
  893. break;
  894. case CCP_SHA_TYPE_224:
  895. digest_size = SHA224_DIGEST_SIZE;
  896. init = (void *) ccp_sha224_init;
  897. ctx_size = SHA256_DIGEST_SIZE;
  898. sb_count = 1;
  899. ioffset = 0;
  900. if (cmd_q->ccp->vdata->version != CCP_VERSION(3, 0))
  901. ooffset = CCP_SB_BYTES - SHA224_DIGEST_SIZE;
  902. else
  903. ooffset = 0;
  904. break;
  905. case CCP_SHA_TYPE_256:
  906. digest_size = SHA256_DIGEST_SIZE;
  907. init = (void *) ccp_sha256_init;
  908. ctx_size = SHA256_DIGEST_SIZE;
  909. sb_count = 1;
  910. ooffset = ioffset = 0;
  911. break;
  912. default:
  913. ret = -EINVAL;
  914. goto e_data;
  915. }
  916. /* For zero-length plaintext the src pointer is ignored;
  917. * otherwise both parts must be valid
  918. */
  919. if (sha->src_len && !sha->src)
  920. return -EINVAL;
  921. memset(&op, 0, sizeof(op));
  922. op.cmd_q = cmd_q;
  923. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  924. op.sb_ctx = cmd_q->sb_ctx; /* Pre-allocated */
  925. op.u.sha.type = sha->type;
  926. op.u.sha.msg_bits = sha->msg_bits;
  927. ret = ccp_init_dm_workarea(&ctx, cmd_q, sb_count * CCP_SB_BYTES,
  928. DMA_BIDIRECTIONAL);
  929. if (ret)
  930. return ret;
  931. if (sha->first) {
  932. switch (sha->type) {
  933. case CCP_SHA_TYPE_1:
  934. case CCP_SHA_TYPE_224:
  935. case CCP_SHA_TYPE_256:
  936. memcpy(ctx.address + ioffset, init, ctx_size);
  937. break;
  938. default:
  939. ret = -EINVAL;
  940. goto e_ctx;
  941. }
  942. } else {
  943. /* Restore the context */
  944. ccp_set_dm_area(&ctx, 0, sha->ctx, 0,
  945. sb_count * CCP_SB_BYTES);
  946. }
  947. ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  948. CCP_PASSTHRU_BYTESWAP_256BIT);
  949. if (ret) {
  950. cmd->engine_error = cmd_q->cmd_error;
  951. goto e_ctx;
  952. }
  953. if (sha->src) {
  954. /* Send data to the CCP SHA engine; block_size is set above */
  955. ret = ccp_init_data(&src, cmd_q, sha->src, sha->src_len,
  956. block_size, DMA_TO_DEVICE);
  957. if (ret)
  958. goto e_ctx;
  959. while (src.sg_wa.bytes_left) {
  960. ccp_prepare_data(&src, NULL, &op, block_size, false);
  961. if (sha->final && !src.sg_wa.bytes_left)
  962. op.eom = 1;
  963. ret = cmd_q->ccp->vdata->perform->sha(&op);
  964. if (ret) {
  965. cmd->engine_error = cmd_q->cmd_error;
  966. goto e_data;
  967. }
  968. ccp_process_data(&src, NULL, &op);
  969. }
  970. } else {
  971. op.eom = 1;
  972. ret = cmd_q->ccp->vdata->perform->sha(&op);
  973. if (ret) {
  974. cmd->engine_error = cmd_q->cmd_error;
  975. goto e_data;
  976. }
  977. }
  978. /* Retrieve the SHA context - convert from LE to BE using
  979. * 32-byte (256-bit) byteswapping to BE
  980. */
  981. ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
  982. CCP_PASSTHRU_BYTESWAP_256BIT);
  983. if (ret) {
  984. cmd->engine_error = cmd_q->cmd_error;
  985. goto e_data;
  986. }
  987. if (sha->final) {
  988. /* Finishing up, so get the digest */
  989. switch (sha->type) {
  990. case CCP_SHA_TYPE_1:
  991. case CCP_SHA_TYPE_224:
  992. case CCP_SHA_TYPE_256:
  993. ccp_get_dm_area(&ctx, ooffset,
  994. sha->ctx, 0,
  995. digest_size);
  996. break;
  997. default:
  998. ret = -EINVAL;
  999. goto e_ctx;
  1000. }
  1001. } else {
  1002. /* Stash the context */
  1003. ccp_get_dm_area(&ctx, 0, sha->ctx, 0,
  1004. sb_count * CCP_SB_BYTES);
  1005. }
  1006. if (sha->final && sha->opad) {
  1007. /* HMAC operation, recursively perform final SHA */
  1008. struct ccp_cmd hmac_cmd;
  1009. struct scatterlist sg;
  1010. u8 *hmac_buf;
  1011. if (sha->opad_len != block_size) {
  1012. ret = -EINVAL;
  1013. goto e_data;
  1014. }
  1015. hmac_buf = kmalloc(block_size + digest_size, GFP_KERNEL);
  1016. if (!hmac_buf) {
  1017. ret = -ENOMEM;
  1018. goto e_data;
  1019. }
  1020. sg_init_one(&sg, hmac_buf, block_size + digest_size);
  1021. scatterwalk_map_and_copy(hmac_buf, sha->opad, 0, block_size, 0);
  1022. switch (sha->type) {
  1023. case CCP_SHA_TYPE_1:
  1024. case CCP_SHA_TYPE_224:
  1025. case CCP_SHA_TYPE_256:
  1026. memcpy(hmac_buf + block_size,
  1027. ctx.address + ooffset,
  1028. digest_size);
  1029. break;
  1030. default:
  1031. ret = -EINVAL;
  1032. goto e_ctx;
  1033. }
  1034. memset(&hmac_cmd, 0, sizeof(hmac_cmd));
  1035. hmac_cmd.engine = CCP_ENGINE_SHA;
  1036. hmac_cmd.u.sha.type = sha->type;
  1037. hmac_cmd.u.sha.ctx = sha->ctx;
  1038. hmac_cmd.u.sha.ctx_len = sha->ctx_len;
  1039. hmac_cmd.u.sha.src = &sg;
  1040. hmac_cmd.u.sha.src_len = block_size + digest_size;
  1041. hmac_cmd.u.sha.opad = NULL;
  1042. hmac_cmd.u.sha.opad_len = 0;
  1043. hmac_cmd.u.sha.first = 1;
  1044. hmac_cmd.u.sha.final = 1;
  1045. hmac_cmd.u.sha.msg_bits = (block_size + digest_size) << 3;
  1046. ret = ccp_run_sha_cmd(cmd_q, &hmac_cmd);
  1047. if (ret)
  1048. cmd->engine_error = hmac_cmd.engine_error;
  1049. kfree(hmac_buf);
  1050. }
  1051. e_data:
  1052. if (sha->src)
  1053. ccp_free_data(&src, cmd_q);
  1054. e_ctx:
  1055. ccp_dm_free(&ctx);
  1056. return ret;
  1057. }
  1058. static int ccp_run_rsa_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1059. {
  1060. struct ccp_rsa_engine *rsa = &cmd->u.rsa;
  1061. struct ccp_dm_workarea exp, src;
  1062. struct ccp_data dst;
  1063. struct ccp_op op;
  1064. unsigned int sb_count, i_len, o_len;
  1065. int ret;
  1066. if (rsa->key_size > CCP_RSA_MAX_WIDTH)
  1067. return -EINVAL;
  1068. if (!rsa->exp || !rsa->mod || !rsa->src || !rsa->dst)
  1069. return -EINVAL;
  1070. /* The RSA modulus must precede the message being acted upon, so
  1071. * it must be copied to a DMA area where the message and the
  1072. * modulus can be concatenated. Therefore the input buffer
  1073. * length required is twice the output buffer length (which
  1074. * must be a multiple of 256-bits).
  1075. */
  1076. o_len = ((rsa->key_size + 255) / 256) * 32;
  1077. i_len = o_len * 2;
  1078. sb_count = o_len / CCP_SB_BYTES;
  1079. memset(&op, 0, sizeof(op));
  1080. op.cmd_q = cmd_q;
  1081. op.jobid = ccp_gen_jobid(cmd_q->ccp);
  1082. op.sb_key = cmd_q->ccp->vdata->perform->sballoc(cmd_q, sb_count);
  1083. if (!op.sb_key)
  1084. return -EIO;
  1085. /* The RSA exponent may span multiple (32-byte) SB entries and must
  1086. * be in little endian format. Reverse copy each 32-byte chunk
  1087. * of the exponent (En chunk to E0 chunk, E(n-1) chunk to E1 chunk)
  1088. * and each byte within that chunk and do not perform any byte swap
  1089. * operations on the passthru operation.
  1090. */
  1091. ret = ccp_init_dm_workarea(&exp, cmd_q, o_len, DMA_TO_DEVICE);
  1092. if (ret)
  1093. goto e_sb;
  1094. ret = ccp_reverse_set_dm_area(&exp, rsa->exp, rsa->exp_len,
  1095. CCP_SB_BYTES, false);
  1096. if (ret)
  1097. goto e_exp;
  1098. ret = ccp_copy_to_sb(cmd_q, &exp, op.jobid, op.sb_key,
  1099. CCP_PASSTHRU_BYTESWAP_NOOP);
  1100. if (ret) {
  1101. cmd->engine_error = cmd_q->cmd_error;
  1102. goto e_exp;
  1103. }
  1104. /* Concatenate the modulus and the message. Both the modulus and
  1105. * the operands must be in little endian format. Since the input
  1106. * is in big endian format it must be converted.
  1107. */
  1108. ret = ccp_init_dm_workarea(&src, cmd_q, i_len, DMA_TO_DEVICE);
  1109. if (ret)
  1110. goto e_exp;
  1111. ret = ccp_reverse_set_dm_area(&src, rsa->mod, rsa->mod_len,
  1112. CCP_SB_BYTES, false);
  1113. if (ret)
  1114. goto e_src;
  1115. src.address += o_len; /* Adjust the address for the copy operation */
  1116. ret = ccp_reverse_set_dm_area(&src, rsa->src, rsa->src_len,
  1117. CCP_SB_BYTES, false);
  1118. if (ret)
  1119. goto e_src;
  1120. src.address -= o_len; /* Reset the address to original value */
  1121. /* Prepare the output area for the operation */
  1122. ret = ccp_init_data(&dst, cmd_q, rsa->dst, rsa->mod_len,
  1123. o_len, DMA_FROM_DEVICE);
  1124. if (ret)
  1125. goto e_src;
  1126. op.soc = 1;
  1127. op.src.u.dma.address = src.dma.address;
  1128. op.src.u.dma.offset = 0;
  1129. op.src.u.dma.length = i_len;
  1130. op.dst.u.dma.address = dst.dm_wa.dma.address;
  1131. op.dst.u.dma.offset = 0;
  1132. op.dst.u.dma.length = o_len;
  1133. op.u.rsa.mod_size = rsa->key_size;
  1134. op.u.rsa.input_len = i_len;
  1135. ret = cmd_q->ccp->vdata->perform->rsa(&op);
  1136. if (ret) {
  1137. cmd->engine_error = cmd_q->cmd_error;
  1138. goto e_dst;
  1139. }
  1140. ccp_reverse_get_dm_area(&dst.dm_wa, rsa->dst, rsa->mod_len);
  1141. e_dst:
  1142. ccp_free_data(&dst, cmd_q);
  1143. e_src:
  1144. ccp_dm_free(&src);
  1145. e_exp:
  1146. ccp_dm_free(&exp);
  1147. e_sb:
  1148. cmd_q->ccp->vdata->perform->sbfree(cmd_q, op.sb_key, sb_count);
  1149. return ret;
  1150. }
  1151. static int ccp_run_passthru_cmd(struct ccp_cmd_queue *cmd_q,
  1152. struct ccp_cmd *cmd)
  1153. {
  1154. struct ccp_passthru_engine *pt = &cmd->u.passthru;
  1155. struct ccp_dm_workarea mask;
  1156. struct ccp_data src, dst;
  1157. struct ccp_op op;
  1158. bool in_place = false;
  1159. unsigned int i;
  1160. int ret = 0;
  1161. if (!pt->final && (pt->src_len & (CCP_PASSTHRU_BLOCKSIZE - 1)))
  1162. return -EINVAL;
  1163. if (!pt->src || !pt->dst)
  1164. return -EINVAL;
  1165. if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
  1166. if (pt->mask_len != CCP_PASSTHRU_MASKSIZE)
  1167. return -EINVAL;
  1168. if (!pt->mask)
  1169. return -EINVAL;
  1170. }
  1171. BUILD_BUG_ON(CCP_PASSTHRU_SB_COUNT != 1);
  1172. memset(&op, 0, sizeof(op));
  1173. op.cmd_q = cmd_q;
  1174. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  1175. if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
  1176. /* Load the mask */
  1177. op.sb_key = cmd_q->sb_key;
  1178. ret = ccp_init_dm_workarea(&mask, cmd_q,
  1179. CCP_PASSTHRU_SB_COUNT *
  1180. CCP_SB_BYTES,
  1181. DMA_TO_DEVICE);
  1182. if (ret)
  1183. return ret;
  1184. ccp_set_dm_area(&mask, 0, pt->mask, 0, pt->mask_len);
  1185. ret = ccp_copy_to_sb(cmd_q, &mask, op.jobid, op.sb_key,
  1186. CCP_PASSTHRU_BYTESWAP_NOOP);
  1187. if (ret) {
  1188. cmd->engine_error = cmd_q->cmd_error;
  1189. goto e_mask;
  1190. }
  1191. }
  1192. /* Prepare the input and output data workareas. For in-place
  1193. * operations we need to set the dma direction to BIDIRECTIONAL
  1194. * and copy the src workarea to the dst workarea.
  1195. */
  1196. if (sg_virt(pt->src) == sg_virt(pt->dst))
  1197. in_place = true;
  1198. ret = ccp_init_data(&src, cmd_q, pt->src, pt->src_len,
  1199. CCP_PASSTHRU_MASKSIZE,
  1200. in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  1201. if (ret)
  1202. goto e_mask;
  1203. if (in_place) {
  1204. dst = src;
  1205. } else {
  1206. ret = ccp_init_data(&dst, cmd_q, pt->dst, pt->src_len,
  1207. CCP_PASSTHRU_MASKSIZE, DMA_FROM_DEVICE);
  1208. if (ret)
  1209. goto e_src;
  1210. }
  1211. /* Send data to the CCP Passthru engine
  1212. * Because the CCP engine works on a single source and destination
  1213. * dma address at a time, each entry in the source scatterlist
  1214. * (after the dma_map_sg call) must be less than or equal to the
  1215. * (remaining) length in the destination scatterlist entry and the
  1216. * length must be a multiple of CCP_PASSTHRU_BLOCKSIZE
  1217. */
  1218. dst.sg_wa.sg_used = 0;
  1219. for (i = 1; i <= src.sg_wa.dma_count; i++) {
  1220. if (!dst.sg_wa.sg ||
  1221. (dst.sg_wa.sg->length < src.sg_wa.sg->length)) {
  1222. ret = -EINVAL;
  1223. goto e_dst;
  1224. }
  1225. if (i == src.sg_wa.dma_count) {
  1226. op.eom = 1;
  1227. op.soc = 1;
  1228. }
  1229. op.src.type = CCP_MEMTYPE_SYSTEM;
  1230. op.src.u.dma.address = sg_dma_address(src.sg_wa.sg);
  1231. op.src.u.dma.offset = 0;
  1232. op.src.u.dma.length = sg_dma_len(src.sg_wa.sg);
  1233. op.dst.type = CCP_MEMTYPE_SYSTEM;
  1234. op.dst.u.dma.address = sg_dma_address(dst.sg_wa.sg);
  1235. op.dst.u.dma.offset = dst.sg_wa.sg_used;
  1236. op.dst.u.dma.length = op.src.u.dma.length;
  1237. ret = cmd_q->ccp->vdata->perform->passthru(&op);
  1238. if (ret) {
  1239. cmd->engine_error = cmd_q->cmd_error;
  1240. goto e_dst;
  1241. }
  1242. dst.sg_wa.sg_used += src.sg_wa.sg->length;
  1243. if (dst.sg_wa.sg_used == dst.sg_wa.sg->length) {
  1244. dst.sg_wa.sg = sg_next(dst.sg_wa.sg);
  1245. dst.sg_wa.sg_used = 0;
  1246. }
  1247. src.sg_wa.sg = sg_next(src.sg_wa.sg);
  1248. }
  1249. e_dst:
  1250. if (!in_place)
  1251. ccp_free_data(&dst, cmd_q);
  1252. e_src:
  1253. ccp_free_data(&src, cmd_q);
  1254. e_mask:
  1255. if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP)
  1256. ccp_dm_free(&mask);
  1257. return ret;
  1258. }
  1259. static int ccp_run_passthru_nomap_cmd(struct ccp_cmd_queue *cmd_q,
  1260. struct ccp_cmd *cmd)
  1261. {
  1262. struct ccp_passthru_nomap_engine *pt = &cmd->u.passthru_nomap;
  1263. struct ccp_dm_workarea mask;
  1264. struct ccp_op op;
  1265. int ret;
  1266. if (!pt->final && (pt->src_len & (CCP_PASSTHRU_BLOCKSIZE - 1)))
  1267. return -EINVAL;
  1268. if (!pt->src_dma || !pt->dst_dma)
  1269. return -EINVAL;
  1270. if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
  1271. if (pt->mask_len != CCP_PASSTHRU_MASKSIZE)
  1272. return -EINVAL;
  1273. if (!pt->mask)
  1274. return -EINVAL;
  1275. }
  1276. BUILD_BUG_ON(CCP_PASSTHRU_SB_COUNT != 1);
  1277. memset(&op, 0, sizeof(op));
  1278. op.cmd_q = cmd_q;
  1279. op.jobid = ccp_gen_jobid(cmd_q->ccp);
  1280. if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
  1281. /* Load the mask */
  1282. op.sb_key = cmd_q->sb_key;
  1283. mask.length = pt->mask_len;
  1284. mask.dma.address = pt->mask;
  1285. mask.dma.length = pt->mask_len;
  1286. ret = ccp_copy_to_sb(cmd_q, &mask, op.jobid, op.sb_key,
  1287. CCP_PASSTHRU_BYTESWAP_NOOP);
  1288. if (ret) {
  1289. cmd->engine_error = cmd_q->cmd_error;
  1290. return ret;
  1291. }
  1292. }
  1293. /* Send data to the CCP Passthru engine */
  1294. op.eom = 1;
  1295. op.soc = 1;
  1296. op.src.type = CCP_MEMTYPE_SYSTEM;
  1297. op.src.u.dma.address = pt->src_dma;
  1298. op.src.u.dma.offset = 0;
  1299. op.src.u.dma.length = pt->src_len;
  1300. op.dst.type = CCP_MEMTYPE_SYSTEM;
  1301. op.dst.u.dma.address = pt->dst_dma;
  1302. op.dst.u.dma.offset = 0;
  1303. op.dst.u.dma.length = pt->src_len;
  1304. ret = cmd_q->ccp->vdata->perform->passthru(&op);
  1305. if (ret)
  1306. cmd->engine_error = cmd_q->cmd_error;
  1307. return ret;
  1308. }
  1309. static int ccp_run_ecc_mm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1310. {
  1311. struct ccp_ecc_engine *ecc = &cmd->u.ecc;
  1312. struct ccp_dm_workarea src, dst;
  1313. struct ccp_op op;
  1314. int ret;
  1315. u8 *save;
  1316. if (!ecc->u.mm.operand_1 ||
  1317. (ecc->u.mm.operand_1_len > CCP_ECC_MODULUS_BYTES))
  1318. return -EINVAL;
  1319. if (ecc->function != CCP_ECC_FUNCTION_MINV_384BIT)
  1320. if (!ecc->u.mm.operand_2 ||
  1321. (ecc->u.mm.operand_2_len > CCP_ECC_MODULUS_BYTES))
  1322. return -EINVAL;
  1323. if (!ecc->u.mm.result ||
  1324. (ecc->u.mm.result_len < CCP_ECC_MODULUS_BYTES))
  1325. return -EINVAL;
  1326. memset(&op, 0, sizeof(op));
  1327. op.cmd_q = cmd_q;
  1328. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  1329. /* Concatenate the modulus and the operands. Both the modulus and
  1330. * the operands must be in little endian format. Since the input
  1331. * is in big endian format it must be converted and placed in a
  1332. * fixed length buffer.
  1333. */
  1334. ret = ccp_init_dm_workarea(&src, cmd_q, CCP_ECC_SRC_BUF_SIZE,
  1335. DMA_TO_DEVICE);
  1336. if (ret)
  1337. return ret;
  1338. /* Save the workarea address since it is updated in order to perform
  1339. * the concatenation
  1340. */
  1341. save = src.address;
  1342. /* Copy the ECC modulus */
  1343. ret = ccp_reverse_set_dm_area(&src, ecc->mod, ecc->mod_len,
  1344. CCP_ECC_OPERAND_SIZE, false);
  1345. if (ret)
  1346. goto e_src;
  1347. src.address += CCP_ECC_OPERAND_SIZE;
  1348. /* Copy the first operand */
  1349. ret = ccp_reverse_set_dm_area(&src, ecc->u.mm.operand_1,
  1350. ecc->u.mm.operand_1_len,
  1351. CCP_ECC_OPERAND_SIZE, false);
  1352. if (ret)
  1353. goto e_src;
  1354. src.address += CCP_ECC_OPERAND_SIZE;
  1355. if (ecc->function != CCP_ECC_FUNCTION_MINV_384BIT) {
  1356. /* Copy the second operand */
  1357. ret = ccp_reverse_set_dm_area(&src, ecc->u.mm.operand_2,
  1358. ecc->u.mm.operand_2_len,
  1359. CCP_ECC_OPERAND_SIZE, false);
  1360. if (ret)
  1361. goto e_src;
  1362. src.address += CCP_ECC_OPERAND_SIZE;
  1363. }
  1364. /* Restore the workarea address */
  1365. src.address = save;
  1366. /* Prepare the output area for the operation */
  1367. ret = ccp_init_dm_workarea(&dst, cmd_q, CCP_ECC_DST_BUF_SIZE,
  1368. DMA_FROM_DEVICE);
  1369. if (ret)
  1370. goto e_src;
  1371. op.soc = 1;
  1372. op.src.u.dma.address = src.dma.address;
  1373. op.src.u.dma.offset = 0;
  1374. op.src.u.dma.length = src.length;
  1375. op.dst.u.dma.address = dst.dma.address;
  1376. op.dst.u.dma.offset = 0;
  1377. op.dst.u.dma.length = dst.length;
  1378. op.u.ecc.function = cmd->u.ecc.function;
  1379. ret = cmd_q->ccp->vdata->perform->ecc(&op);
  1380. if (ret) {
  1381. cmd->engine_error = cmd_q->cmd_error;
  1382. goto e_dst;
  1383. }
  1384. ecc->ecc_result = le16_to_cpup(
  1385. (const __le16 *)(dst.address + CCP_ECC_RESULT_OFFSET));
  1386. if (!(ecc->ecc_result & CCP_ECC_RESULT_SUCCESS)) {
  1387. ret = -EIO;
  1388. goto e_dst;
  1389. }
  1390. /* Save the ECC result */
  1391. ccp_reverse_get_dm_area(&dst, ecc->u.mm.result, CCP_ECC_MODULUS_BYTES);
  1392. e_dst:
  1393. ccp_dm_free(&dst);
  1394. e_src:
  1395. ccp_dm_free(&src);
  1396. return ret;
  1397. }
  1398. static int ccp_run_ecc_pm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1399. {
  1400. struct ccp_ecc_engine *ecc = &cmd->u.ecc;
  1401. struct ccp_dm_workarea src, dst;
  1402. struct ccp_op op;
  1403. int ret;
  1404. u8 *save;
  1405. if (!ecc->u.pm.point_1.x ||
  1406. (ecc->u.pm.point_1.x_len > CCP_ECC_MODULUS_BYTES) ||
  1407. !ecc->u.pm.point_1.y ||
  1408. (ecc->u.pm.point_1.y_len > CCP_ECC_MODULUS_BYTES))
  1409. return -EINVAL;
  1410. if (ecc->function == CCP_ECC_FUNCTION_PADD_384BIT) {
  1411. if (!ecc->u.pm.point_2.x ||
  1412. (ecc->u.pm.point_2.x_len > CCP_ECC_MODULUS_BYTES) ||
  1413. !ecc->u.pm.point_2.y ||
  1414. (ecc->u.pm.point_2.y_len > CCP_ECC_MODULUS_BYTES))
  1415. return -EINVAL;
  1416. } else {
  1417. if (!ecc->u.pm.domain_a ||
  1418. (ecc->u.pm.domain_a_len > CCP_ECC_MODULUS_BYTES))
  1419. return -EINVAL;
  1420. if (ecc->function == CCP_ECC_FUNCTION_PMUL_384BIT)
  1421. if (!ecc->u.pm.scalar ||
  1422. (ecc->u.pm.scalar_len > CCP_ECC_MODULUS_BYTES))
  1423. return -EINVAL;
  1424. }
  1425. if (!ecc->u.pm.result.x ||
  1426. (ecc->u.pm.result.x_len < CCP_ECC_MODULUS_BYTES) ||
  1427. !ecc->u.pm.result.y ||
  1428. (ecc->u.pm.result.y_len < CCP_ECC_MODULUS_BYTES))
  1429. return -EINVAL;
  1430. memset(&op, 0, sizeof(op));
  1431. op.cmd_q = cmd_q;
  1432. op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
  1433. /* Concatenate the modulus and the operands. Both the modulus and
  1434. * the operands must be in little endian format. Since the input
  1435. * is in big endian format it must be converted and placed in a
  1436. * fixed length buffer.
  1437. */
  1438. ret = ccp_init_dm_workarea(&src, cmd_q, CCP_ECC_SRC_BUF_SIZE,
  1439. DMA_TO_DEVICE);
  1440. if (ret)
  1441. return ret;
  1442. /* Save the workarea address since it is updated in order to perform
  1443. * the concatenation
  1444. */
  1445. save = src.address;
  1446. /* Copy the ECC modulus */
  1447. ret = ccp_reverse_set_dm_area(&src, ecc->mod, ecc->mod_len,
  1448. CCP_ECC_OPERAND_SIZE, false);
  1449. if (ret)
  1450. goto e_src;
  1451. src.address += CCP_ECC_OPERAND_SIZE;
  1452. /* Copy the first point X and Y coordinate */
  1453. ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.point_1.x,
  1454. ecc->u.pm.point_1.x_len,
  1455. CCP_ECC_OPERAND_SIZE, false);
  1456. if (ret)
  1457. goto e_src;
  1458. src.address += CCP_ECC_OPERAND_SIZE;
  1459. ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.point_1.y,
  1460. ecc->u.pm.point_1.y_len,
  1461. CCP_ECC_OPERAND_SIZE, false);
  1462. if (ret)
  1463. goto e_src;
  1464. src.address += CCP_ECC_OPERAND_SIZE;
  1465. /* Set the first point Z coordinate to 1 */
  1466. *src.address = 0x01;
  1467. src.address += CCP_ECC_OPERAND_SIZE;
  1468. if (ecc->function == CCP_ECC_FUNCTION_PADD_384BIT) {
  1469. /* Copy the second point X and Y coordinate */
  1470. ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.point_2.x,
  1471. ecc->u.pm.point_2.x_len,
  1472. CCP_ECC_OPERAND_SIZE, false);
  1473. if (ret)
  1474. goto e_src;
  1475. src.address += CCP_ECC_OPERAND_SIZE;
  1476. ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.point_2.y,
  1477. ecc->u.pm.point_2.y_len,
  1478. CCP_ECC_OPERAND_SIZE, false);
  1479. if (ret)
  1480. goto e_src;
  1481. src.address += CCP_ECC_OPERAND_SIZE;
  1482. /* Set the second point Z coordinate to 1 */
  1483. *src.address = 0x01;
  1484. src.address += CCP_ECC_OPERAND_SIZE;
  1485. } else {
  1486. /* Copy the Domain "a" parameter */
  1487. ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.domain_a,
  1488. ecc->u.pm.domain_a_len,
  1489. CCP_ECC_OPERAND_SIZE, false);
  1490. if (ret)
  1491. goto e_src;
  1492. src.address += CCP_ECC_OPERAND_SIZE;
  1493. if (ecc->function == CCP_ECC_FUNCTION_PMUL_384BIT) {
  1494. /* Copy the scalar value */
  1495. ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.scalar,
  1496. ecc->u.pm.scalar_len,
  1497. CCP_ECC_OPERAND_SIZE,
  1498. false);
  1499. if (ret)
  1500. goto e_src;
  1501. src.address += CCP_ECC_OPERAND_SIZE;
  1502. }
  1503. }
  1504. /* Restore the workarea address */
  1505. src.address = save;
  1506. /* Prepare the output area for the operation */
  1507. ret = ccp_init_dm_workarea(&dst, cmd_q, CCP_ECC_DST_BUF_SIZE,
  1508. DMA_FROM_DEVICE);
  1509. if (ret)
  1510. goto e_src;
  1511. op.soc = 1;
  1512. op.src.u.dma.address = src.dma.address;
  1513. op.src.u.dma.offset = 0;
  1514. op.src.u.dma.length = src.length;
  1515. op.dst.u.dma.address = dst.dma.address;
  1516. op.dst.u.dma.offset = 0;
  1517. op.dst.u.dma.length = dst.length;
  1518. op.u.ecc.function = cmd->u.ecc.function;
  1519. ret = cmd_q->ccp->vdata->perform->ecc(&op);
  1520. if (ret) {
  1521. cmd->engine_error = cmd_q->cmd_error;
  1522. goto e_dst;
  1523. }
  1524. ecc->ecc_result = le16_to_cpup(
  1525. (const __le16 *)(dst.address + CCP_ECC_RESULT_OFFSET));
  1526. if (!(ecc->ecc_result & CCP_ECC_RESULT_SUCCESS)) {
  1527. ret = -EIO;
  1528. goto e_dst;
  1529. }
  1530. /* Save the workarea address since it is updated as we walk through
  1531. * to copy the point math result
  1532. */
  1533. save = dst.address;
  1534. /* Save the ECC result X and Y coordinates */
  1535. ccp_reverse_get_dm_area(&dst, ecc->u.pm.result.x,
  1536. CCP_ECC_MODULUS_BYTES);
  1537. dst.address += CCP_ECC_OUTPUT_SIZE;
  1538. ccp_reverse_get_dm_area(&dst, ecc->u.pm.result.y,
  1539. CCP_ECC_MODULUS_BYTES);
  1540. dst.address += CCP_ECC_OUTPUT_SIZE;
  1541. /* Restore the workarea address */
  1542. dst.address = save;
  1543. e_dst:
  1544. ccp_dm_free(&dst);
  1545. e_src:
  1546. ccp_dm_free(&src);
  1547. return ret;
  1548. }
  1549. static int ccp_run_ecc_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1550. {
  1551. struct ccp_ecc_engine *ecc = &cmd->u.ecc;
  1552. ecc->ecc_result = 0;
  1553. if (!ecc->mod ||
  1554. (ecc->mod_len > CCP_ECC_MODULUS_BYTES))
  1555. return -EINVAL;
  1556. switch (ecc->function) {
  1557. case CCP_ECC_FUNCTION_MMUL_384BIT:
  1558. case CCP_ECC_FUNCTION_MADD_384BIT:
  1559. case CCP_ECC_FUNCTION_MINV_384BIT:
  1560. return ccp_run_ecc_mm_cmd(cmd_q, cmd);
  1561. case CCP_ECC_FUNCTION_PADD_384BIT:
  1562. case CCP_ECC_FUNCTION_PMUL_384BIT:
  1563. case CCP_ECC_FUNCTION_PDBL_384BIT:
  1564. return ccp_run_ecc_pm_cmd(cmd_q, cmd);
  1565. default:
  1566. return -EINVAL;
  1567. }
  1568. }
  1569. int ccp_run_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1570. {
  1571. int ret;
  1572. cmd->engine_error = 0;
  1573. cmd_q->cmd_error = 0;
  1574. cmd_q->int_rcvd = 0;
  1575. cmd_q->free_slots = cmd_q->ccp->vdata->perform->get_free_slots(cmd_q);
  1576. switch (cmd->engine) {
  1577. case CCP_ENGINE_AES:
  1578. ret = ccp_run_aes_cmd(cmd_q, cmd);
  1579. break;
  1580. case CCP_ENGINE_XTS_AES_128:
  1581. ret = ccp_run_xts_aes_cmd(cmd_q, cmd);
  1582. break;
  1583. case CCP_ENGINE_SHA:
  1584. ret = ccp_run_sha_cmd(cmd_q, cmd);
  1585. break;
  1586. case CCP_ENGINE_RSA:
  1587. ret = ccp_run_rsa_cmd(cmd_q, cmd);
  1588. break;
  1589. case CCP_ENGINE_PASSTHRU:
  1590. if (cmd->flags & CCP_CMD_PASSTHRU_NO_DMA_MAP)
  1591. ret = ccp_run_passthru_nomap_cmd(cmd_q, cmd);
  1592. else
  1593. ret = ccp_run_passthru_cmd(cmd_q, cmd);
  1594. break;
  1595. case CCP_ENGINE_ECC:
  1596. ret = ccp_run_ecc_cmd(cmd_q, cmd);
  1597. break;
  1598. default:
  1599. ret = -EINVAL;
  1600. }
  1601. return ret;
  1602. }