qat_asym_algs.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  1. /*
  2. This file is provided under a dual BSD/GPLv2 license. When using or
  3. redistributing this file, you may do so under either license.
  4. GPL LICENSE SUMMARY
  5. Copyright(c) 2014 Intel Corporation.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of version 2 of the GNU General Public License as
  8. published by the Free Software Foundation.
  9. This program is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. Contact Information:
  14. qat-linux@intel.com
  15. BSD LICENSE
  16. Copyright(c) 2014 Intel Corporation.
  17. Redistribution and use in source and binary forms, with or without
  18. modification, are permitted provided that the following conditions
  19. are met:
  20. * Redistributions of source code must retain the above copyright
  21. notice, this list of conditions and the following disclaimer.
  22. * Redistributions in binary form must reproduce the above copyright
  23. notice, this list of conditions and the following disclaimer in
  24. the documentation and/or other materials provided with the
  25. distribution.
  26. * Neither the name of Intel Corporation nor the names of its
  27. contributors may be used to endorse or promote products derived
  28. from this software without specific prior written permission.
  29. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  32. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  33. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  34. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  35. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  36. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  37. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  38. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  39. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. */
  41. #include <linux/module.h>
  42. #include <crypto/internal/rsa.h>
  43. #include <crypto/internal/akcipher.h>
  44. #include <crypto/akcipher.h>
  45. #include <crypto/kpp.h>
  46. #include <crypto/internal/kpp.h>
  47. #include <crypto/dh.h>
  48. #include <linux/dma-mapping.h>
  49. #include <linux/fips.h>
  50. #include <crypto/scatterwalk.h>
  51. #include "icp_qat_fw_pke.h"
  52. #include "adf_accel_devices.h"
  53. #include "adf_transport.h"
  54. #include "adf_common_drv.h"
  55. #include "qat_crypto.h"
  56. static DEFINE_MUTEX(algs_lock);
  57. static unsigned int active_devs;
  58. struct qat_rsa_input_params {
  59. union {
  60. struct {
  61. dma_addr_t m;
  62. dma_addr_t e;
  63. dma_addr_t n;
  64. } enc;
  65. struct {
  66. dma_addr_t c;
  67. dma_addr_t d;
  68. dma_addr_t n;
  69. } dec;
  70. struct {
  71. dma_addr_t c;
  72. dma_addr_t p;
  73. dma_addr_t q;
  74. dma_addr_t dp;
  75. dma_addr_t dq;
  76. dma_addr_t qinv;
  77. } dec_crt;
  78. u64 in_tab[8];
  79. };
  80. } __packed __aligned(64);
  81. struct qat_rsa_output_params {
  82. union {
  83. struct {
  84. dma_addr_t c;
  85. } enc;
  86. struct {
  87. dma_addr_t m;
  88. } dec;
  89. u64 out_tab[8];
  90. };
  91. } __packed __aligned(64);
  92. struct qat_rsa_ctx {
  93. char *n;
  94. char *e;
  95. char *d;
  96. char *p;
  97. char *q;
  98. char *dp;
  99. char *dq;
  100. char *qinv;
  101. dma_addr_t dma_n;
  102. dma_addr_t dma_e;
  103. dma_addr_t dma_d;
  104. dma_addr_t dma_p;
  105. dma_addr_t dma_q;
  106. dma_addr_t dma_dp;
  107. dma_addr_t dma_dq;
  108. dma_addr_t dma_qinv;
  109. unsigned int key_sz;
  110. bool crt_mode;
  111. struct qat_crypto_instance *inst;
  112. } __packed __aligned(64);
  113. struct qat_dh_input_params {
  114. union {
  115. struct {
  116. dma_addr_t b;
  117. dma_addr_t xa;
  118. dma_addr_t p;
  119. } in;
  120. struct {
  121. dma_addr_t xa;
  122. dma_addr_t p;
  123. } in_g2;
  124. u64 in_tab[8];
  125. };
  126. } __packed __aligned(64);
  127. struct qat_dh_output_params {
  128. union {
  129. dma_addr_t r;
  130. u64 out_tab[8];
  131. };
  132. } __packed __aligned(64);
  133. struct qat_dh_ctx {
  134. char *g;
  135. char *xa;
  136. char *p;
  137. dma_addr_t dma_g;
  138. dma_addr_t dma_xa;
  139. dma_addr_t dma_p;
  140. unsigned int p_size;
  141. bool g2;
  142. struct qat_crypto_instance *inst;
  143. } __packed __aligned(64);
  144. struct qat_asym_request {
  145. union {
  146. struct qat_rsa_input_params rsa;
  147. struct qat_dh_input_params dh;
  148. } in;
  149. union {
  150. struct qat_rsa_output_params rsa;
  151. struct qat_dh_output_params dh;
  152. } out;
  153. dma_addr_t phy_in;
  154. dma_addr_t phy_out;
  155. char *src_align;
  156. char *dst_align;
  157. struct icp_qat_fw_pke_request req;
  158. union {
  159. struct qat_rsa_ctx *rsa;
  160. struct qat_dh_ctx *dh;
  161. } ctx;
  162. union {
  163. struct akcipher_request *rsa;
  164. struct kpp_request *dh;
  165. } areq;
  166. int err;
  167. void (*cb)(struct icp_qat_fw_pke_resp *resp);
  168. } __aligned(64);
  169. static void qat_dh_cb(struct icp_qat_fw_pke_resp *resp)
  170. {
  171. struct qat_asym_request *req = (void *)(__force long)resp->opaque;
  172. struct kpp_request *areq = req->areq.dh;
  173. struct device *dev = &GET_DEV(req->ctx.dh->inst->accel_dev);
  174. int err = ICP_QAT_FW_PKE_RESP_PKE_STAT_GET(
  175. resp->pke_resp_hdr.comn_resp_flags);
  176. err = (err == ICP_QAT_FW_COMN_STATUS_FLAG_OK) ? 0 : -EINVAL;
  177. if (areq->src) {
  178. if (req->src_align)
  179. dma_free_coherent(dev, req->ctx.dh->p_size,
  180. req->src_align, req->in.dh.in.b);
  181. else
  182. dma_unmap_single(dev, req->in.dh.in.b,
  183. req->ctx.dh->p_size, DMA_TO_DEVICE);
  184. }
  185. areq->dst_len = req->ctx.dh->p_size;
  186. if (req->dst_align) {
  187. scatterwalk_map_and_copy(req->dst_align, areq->dst, 0,
  188. areq->dst_len, 1);
  189. dma_free_coherent(dev, req->ctx.dh->p_size, req->dst_align,
  190. req->out.dh.r);
  191. } else {
  192. dma_unmap_single(dev, req->out.dh.r, req->ctx.dh->p_size,
  193. DMA_FROM_DEVICE);
  194. }
  195. dma_unmap_single(dev, req->phy_in, sizeof(struct qat_dh_input_params),
  196. DMA_TO_DEVICE);
  197. dma_unmap_single(dev, req->phy_out,
  198. sizeof(struct qat_dh_output_params),
  199. DMA_TO_DEVICE);
  200. kpp_request_complete(areq, err);
  201. }
  202. #define PKE_DH_1536 0x390c1a49
  203. #define PKE_DH_G2_1536 0x2e0b1a3e
  204. #define PKE_DH_2048 0x4d0c1a60
  205. #define PKE_DH_G2_2048 0x3e0b1a55
  206. #define PKE_DH_3072 0x510c1a77
  207. #define PKE_DH_G2_3072 0x3a0b1a6c
  208. #define PKE_DH_4096 0x690c1a8e
  209. #define PKE_DH_G2_4096 0x4a0b1a83
  210. static unsigned long qat_dh_fn_id(unsigned int len, bool g2)
  211. {
  212. unsigned int bitslen = len << 3;
  213. switch (bitslen) {
  214. case 1536:
  215. return g2 ? PKE_DH_G2_1536 : PKE_DH_1536;
  216. case 2048:
  217. return g2 ? PKE_DH_G2_2048 : PKE_DH_2048;
  218. case 3072:
  219. return g2 ? PKE_DH_G2_3072 : PKE_DH_3072;
  220. case 4096:
  221. return g2 ? PKE_DH_G2_4096 : PKE_DH_4096;
  222. default:
  223. return 0;
  224. };
  225. }
  226. static inline struct qat_dh_ctx *qat_dh_get_params(struct crypto_kpp *tfm)
  227. {
  228. return kpp_tfm_ctx(tfm);
  229. }
  230. static int qat_dh_compute_value(struct kpp_request *req)
  231. {
  232. struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
  233. struct qat_dh_ctx *ctx = kpp_tfm_ctx(tfm);
  234. struct qat_crypto_instance *inst = ctx->inst;
  235. struct device *dev = &GET_DEV(inst->accel_dev);
  236. struct qat_asym_request *qat_req =
  237. PTR_ALIGN(kpp_request_ctx(req), 64);
  238. struct icp_qat_fw_pke_request *msg = &qat_req->req;
  239. int ret, ctr = 0;
  240. int n_input_params = 0;
  241. if (unlikely(!ctx->xa))
  242. return -EINVAL;
  243. if (req->dst_len < ctx->p_size) {
  244. req->dst_len = ctx->p_size;
  245. return -EOVERFLOW;
  246. }
  247. memset(msg, '\0', sizeof(*msg));
  248. ICP_QAT_FW_PKE_HDR_VALID_FLAG_SET(msg->pke_hdr,
  249. ICP_QAT_FW_COMN_REQ_FLAG_SET);
  250. msg->pke_hdr.cd_pars.func_id = qat_dh_fn_id(ctx->p_size,
  251. !req->src && ctx->g2);
  252. if (unlikely(!msg->pke_hdr.cd_pars.func_id))
  253. return -EINVAL;
  254. qat_req->cb = qat_dh_cb;
  255. qat_req->ctx.dh = ctx;
  256. qat_req->areq.dh = req;
  257. msg->pke_hdr.service_type = ICP_QAT_FW_COMN_REQ_CPM_FW_PKE;
  258. msg->pke_hdr.comn_req_flags =
  259. ICP_QAT_FW_COMN_FLAGS_BUILD(QAT_COMN_PTR_TYPE_FLAT,
  260. QAT_COMN_CD_FLD_TYPE_64BIT_ADR);
  261. /*
  262. * If no source is provided use g as base
  263. */
  264. if (req->src) {
  265. qat_req->in.dh.in.xa = ctx->dma_xa;
  266. qat_req->in.dh.in.p = ctx->dma_p;
  267. n_input_params = 3;
  268. } else {
  269. if (ctx->g2) {
  270. qat_req->in.dh.in_g2.xa = ctx->dma_xa;
  271. qat_req->in.dh.in_g2.p = ctx->dma_p;
  272. n_input_params = 2;
  273. } else {
  274. qat_req->in.dh.in.b = ctx->dma_g;
  275. qat_req->in.dh.in.xa = ctx->dma_xa;
  276. qat_req->in.dh.in.p = ctx->dma_p;
  277. n_input_params = 3;
  278. }
  279. }
  280. ret = -ENOMEM;
  281. if (req->src) {
  282. /*
  283. * src can be of any size in valid range, but HW expects it to
  284. * be the same as modulo p so in case it is different we need
  285. * to allocate a new buf and copy src data.
  286. * In other case we just need to map the user provided buffer.
  287. * Also need to make sure that it is in contiguous buffer.
  288. */
  289. if (sg_is_last(req->src) && req->src_len == ctx->p_size) {
  290. qat_req->src_align = NULL;
  291. qat_req->in.dh.in.b = dma_map_single(dev,
  292. sg_virt(req->src),
  293. req->src_len,
  294. DMA_TO_DEVICE);
  295. if (unlikely(dma_mapping_error(dev,
  296. qat_req->in.dh.in.b)))
  297. return ret;
  298. } else {
  299. int shift = ctx->p_size - req->src_len;
  300. qat_req->src_align = dma_zalloc_coherent(dev,
  301. ctx->p_size,
  302. &qat_req->in.dh.in.b,
  303. GFP_KERNEL);
  304. if (unlikely(!qat_req->src_align))
  305. return ret;
  306. scatterwalk_map_and_copy(qat_req->src_align + shift,
  307. req->src, 0, req->src_len, 0);
  308. }
  309. }
  310. /*
  311. * dst can be of any size in valid range, but HW expects it to be the
  312. * same as modulo m so in case it is different we need to allocate a
  313. * new buf and copy src data.
  314. * In other case we just need to map the user provided buffer.
  315. * Also need to make sure that it is in contiguous buffer.
  316. */
  317. if (sg_is_last(req->dst) && req->dst_len == ctx->p_size) {
  318. qat_req->dst_align = NULL;
  319. qat_req->out.dh.r = dma_map_single(dev, sg_virt(req->dst),
  320. req->dst_len,
  321. DMA_FROM_DEVICE);
  322. if (unlikely(dma_mapping_error(dev, qat_req->out.dh.r)))
  323. goto unmap_src;
  324. } else {
  325. qat_req->dst_align = dma_zalloc_coherent(dev, ctx->p_size,
  326. &qat_req->out.dh.r,
  327. GFP_KERNEL);
  328. if (unlikely(!qat_req->dst_align))
  329. goto unmap_src;
  330. }
  331. qat_req->in.dh.in_tab[n_input_params] = 0;
  332. qat_req->out.dh.out_tab[1] = 0;
  333. /* Mapping in.in.b or in.in_g2.xa is the same */
  334. qat_req->phy_in = dma_map_single(dev, &qat_req->in.dh.in.b,
  335. sizeof(struct qat_dh_input_params),
  336. DMA_TO_DEVICE);
  337. if (unlikely(dma_mapping_error(dev, qat_req->phy_in)))
  338. goto unmap_dst;
  339. qat_req->phy_out = dma_map_single(dev, &qat_req->out.dh.r,
  340. sizeof(struct qat_dh_output_params),
  341. DMA_TO_DEVICE);
  342. if (unlikely(dma_mapping_error(dev, qat_req->phy_out)))
  343. goto unmap_in_params;
  344. msg->pke_mid.src_data_addr = qat_req->phy_in;
  345. msg->pke_mid.dest_data_addr = qat_req->phy_out;
  346. msg->pke_mid.opaque = (uint64_t)(__force long)qat_req;
  347. msg->input_param_count = n_input_params;
  348. msg->output_param_count = 1;
  349. do {
  350. ret = adf_send_message(ctx->inst->pke_tx, (uint32_t *)msg);
  351. } while (ret == -EBUSY && ctr++ < 100);
  352. if (!ret)
  353. return -EINPROGRESS;
  354. if (!dma_mapping_error(dev, qat_req->phy_out))
  355. dma_unmap_single(dev, qat_req->phy_out,
  356. sizeof(struct qat_dh_output_params),
  357. DMA_TO_DEVICE);
  358. unmap_in_params:
  359. if (!dma_mapping_error(dev, qat_req->phy_in))
  360. dma_unmap_single(dev, qat_req->phy_in,
  361. sizeof(struct qat_dh_input_params),
  362. DMA_TO_DEVICE);
  363. unmap_dst:
  364. if (qat_req->dst_align)
  365. dma_free_coherent(dev, ctx->p_size, qat_req->dst_align,
  366. qat_req->out.dh.r);
  367. else
  368. if (!dma_mapping_error(dev, qat_req->out.dh.r))
  369. dma_unmap_single(dev, qat_req->out.dh.r, ctx->p_size,
  370. DMA_FROM_DEVICE);
  371. unmap_src:
  372. if (req->src) {
  373. if (qat_req->src_align)
  374. dma_free_coherent(dev, ctx->p_size, qat_req->src_align,
  375. qat_req->in.dh.in.b);
  376. else
  377. if (!dma_mapping_error(dev, qat_req->in.dh.in.b))
  378. dma_unmap_single(dev, qat_req->in.dh.in.b,
  379. ctx->p_size,
  380. DMA_TO_DEVICE);
  381. }
  382. return ret;
  383. }
  384. static int qat_dh_check_params_length(unsigned int p_len)
  385. {
  386. switch (p_len) {
  387. case 1536:
  388. case 2048:
  389. case 3072:
  390. case 4096:
  391. return 0;
  392. }
  393. return -EINVAL;
  394. }
  395. static int qat_dh_set_params(struct qat_dh_ctx *ctx, struct dh *params)
  396. {
  397. struct qat_crypto_instance *inst = ctx->inst;
  398. struct device *dev = &GET_DEV(inst->accel_dev);
  399. if (qat_dh_check_params_length(params->p_size << 3))
  400. return -EINVAL;
  401. ctx->p_size = params->p_size;
  402. ctx->p = dma_zalloc_coherent(dev, ctx->p_size, &ctx->dma_p, GFP_KERNEL);
  403. if (!ctx->p)
  404. return -ENOMEM;
  405. memcpy(ctx->p, params->p, ctx->p_size);
  406. /* If g equals 2 don't copy it */
  407. if (params->g_size == 1 && *(char *)params->g == 0x02) {
  408. ctx->g2 = true;
  409. return 0;
  410. }
  411. ctx->g = dma_zalloc_coherent(dev, ctx->p_size, &ctx->dma_g, GFP_KERNEL);
  412. if (!ctx->g)
  413. return -ENOMEM;
  414. memcpy(ctx->g + (ctx->p_size - params->g_size), params->g,
  415. params->g_size);
  416. return 0;
  417. }
  418. static void qat_dh_clear_ctx(struct device *dev, struct qat_dh_ctx *ctx)
  419. {
  420. if (ctx->g) {
  421. dma_free_coherent(dev, ctx->p_size, ctx->g, ctx->dma_g);
  422. ctx->g = NULL;
  423. }
  424. if (ctx->xa) {
  425. dma_free_coherent(dev, ctx->p_size, ctx->xa, ctx->dma_xa);
  426. ctx->xa = NULL;
  427. }
  428. if (ctx->p) {
  429. dma_free_coherent(dev, ctx->p_size, ctx->p, ctx->dma_p);
  430. ctx->p = NULL;
  431. }
  432. ctx->p_size = 0;
  433. ctx->g2 = false;
  434. }
  435. static int qat_dh_set_secret(struct crypto_kpp *tfm, const void *buf,
  436. unsigned int len)
  437. {
  438. struct qat_dh_ctx *ctx = kpp_tfm_ctx(tfm);
  439. struct device *dev = &GET_DEV(ctx->inst->accel_dev);
  440. struct dh params;
  441. int ret;
  442. if (crypto_dh_decode_key(buf, len, &params) < 0)
  443. return -EINVAL;
  444. /* Free old secret if any */
  445. qat_dh_clear_ctx(dev, ctx);
  446. ret = qat_dh_set_params(ctx, &params);
  447. if (ret < 0)
  448. goto err_clear_ctx;
  449. ctx->xa = dma_zalloc_coherent(dev, ctx->p_size, &ctx->dma_xa,
  450. GFP_KERNEL);
  451. if (!ctx->xa) {
  452. ret = -ENOMEM;
  453. goto err_clear_ctx;
  454. }
  455. memcpy(ctx->xa + (ctx->p_size - params.key_size), params.key,
  456. params.key_size);
  457. return 0;
  458. err_clear_ctx:
  459. qat_dh_clear_ctx(dev, ctx);
  460. return ret;
  461. }
  462. static unsigned int qat_dh_max_size(struct crypto_kpp *tfm)
  463. {
  464. struct qat_dh_ctx *ctx = kpp_tfm_ctx(tfm);
  465. return ctx->p_size;
  466. }
  467. static int qat_dh_init_tfm(struct crypto_kpp *tfm)
  468. {
  469. struct qat_dh_ctx *ctx = kpp_tfm_ctx(tfm);
  470. struct qat_crypto_instance *inst =
  471. qat_crypto_get_instance_node(get_current_node());
  472. if (!inst)
  473. return -EINVAL;
  474. ctx->p_size = 0;
  475. ctx->g2 = false;
  476. ctx->inst = inst;
  477. return 0;
  478. }
  479. static void qat_dh_exit_tfm(struct crypto_kpp *tfm)
  480. {
  481. struct qat_dh_ctx *ctx = kpp_tfm_ctx(tfm);
  482. struct device *dev = &GET_DEV(ctx->inst->accel_dev);
  483. qat_dh_clear_ctx(dev, ctx);
  484. qat_crypto_put_instance(ctx->inst);
  485. }
  486. static void qat_rsa_cb(struct icp_qat_fw_pke_resp *resp)
  487. {
  488. struct qat_asym_request *req = (void *)(__force long)resp->opaque;
  489. struct akcipher_request *areq = req->areq.rsa;
  490. struct device *dev = &GET_DEV(req->ctx.rsa->inst->accel_dev);
  491. int err = ICP_QAT_FW_PKE_RESP_PKE_STAT_GET(
  492. resp->pke_resp_hdr.comn_resp_flags);
  493. err = (err == ICP_QAT_FW_COMN_STATUS_FLAG_OK) ? 0 : -EINVAL;
  494. if (req->src_align)
  495. dma_free_coherent(dev, req->ctx.rsa->key_sz, req->src_align,
  496. req->in.rsa.enc.m);
  497. else
  498. dma_unmap_single(dev, req->in.rsa.enc.m, req->ctx.rsa->key_sz,
  499. DMA_TO_DEVICE);
  500. areq->dst_len = req->ctx.rsa->key_sz;
  501. if (req->dst_align) {
  502. scatterwalk_map_and_copy(req->dst_align, areq->dst, 0,
  503. areq->dst_len, 1);
  504. dma_free_coherent(dev, req->ctx.rsa->key_sz, req->dst_align,
  505. req->out.rsa.enc.c);
  506. } else {
  507. dma_unmap_single(dev, req->out.rsa.enc.c, req->ctx.rsa->key_sz,
  508. DMA_FROM_DEVICE);
  509. }
  510. dma_unmap_single(dev, req->phy_in, sizeof(struct qat_rsa_input_params),
  511. DMA_TO_DEVICE);
  512. dma_unmap_single(dev, req->phy_out,
  513. sizeof(struct qat_rsa_output_params),
  514. DMA_TO_DEVICE);
  515. akcipher_request_complete(areq, err);
  516. }
  517. void qat_alg_asym_callback(void *_resp)
  518. {
  519. struct icp_qat_fw_pke_resp *resp = _resp;
  520. struct qat_asym_request *areq = (void *)(__force long)resp->opaque;
  521. areq->cb(resp);
  522. }
  523. #define PKE_RSA_EP_512 0x1c161b21
  524. #define PKE_RSA_EP_1024 0x35111bf7
  525. #define PKE_RSA_EP_1536 0x4d111cdc
  526. #define PKE_RSA_EP_2048 0x6e111dba
  527. #define PKE_RSA_EP_3072 0x7d111ea3
  528. #define PKE_RSA_EP_4096 0xa5101f7e
  529. static unsigned long qat_rsa_enc_fn_id(unsigned int len)
  530. {
  531. unsigned int bitslen = len << 3;
  532. switch (bitslen) {
  533. case 512:
  534. return PKE_RSA_EP_512;
  535. case 1024:
  536. return PKE_RSA_EP_1024;
  537. case 1536:
  538. return PKE_RSA_EP_1536;
  539. case 2048:
  540. return PKE_RSA_EP_2048;
  541. case 3072:
  542. return PKE_RSA_EP_3072;
  543. case 4096:
  544. return PKE_RSA_EP_4096;
  545. default:
  546. return 0;
  547. };
  548. }
  549. #define PKE_RSA_DP1_512 0x1c161b3c
  550. #define PKE_RSA_DP1_1024 0x35111c12
  551. #define PKE_RSA_DP1_1536 0x4d111cf7
  552. #define PKE_RSA_DP1_2048 0x6e111dda
  553. #define PKE_RSA_DP1_3072 0x7d111ebe
  554. #define PKE_RSA_DP1_4096 0xa5101f98
  555. static unsigned long qat_rsa_dec_fn_id(unsigned int len)
  556. {
  557. unsigned int bitslen = len << 3;
  558. switch (bitslen) {
  559. case 512:
  560. return PKE_RSA_DP1_512;
  561. case 1024:
  562. return PKE_RSA_DP1_1024;
  563. case 1536:
  564. return PKE_RSA_DP1_1536;
  565. case 2048:
  566. return PKE_RSA_DP1_2048;
  567. case 3072:
  568. return PKE_RSA_DP1_3072;
  569. case 4096:
  570. return PKE_RSA_DP1_4096;
  571. default:
  572. return 0;
  573. };
  574. }
  575. #define PKE_RSA_DP2_512 0x1c131b57
  576. #define PKE_RSA_DP2_1024 0x26131c2d
  577. #define PKE_RSA_DP2_1536 0x45111d12
  578. #define PKE_RSA_DP2_2048 0x59121dfa
  579. #define PKE_RSA_DP2_3072 0x81121ed9
  580. #define PKE_RSA_DP2_4096 0xb1111fb2
  581. static unsigned long qat_rsa_dec_fn_id_crt(unsigned int len)
  582. {
  583. unsigned int bitslen = len << 3;
  584. switch (bitslen) {
  585. case 512:
  586. return PKE_RSA_DP2_512;
  587. case 1024:
  588. return PKE_RSA_DP2_1024;
  589. case 1536:
  590. return PKE_RSA_DP2_1536;
  591. case 2048:
  592. return PKE_RSA_DP2_2048;
  593. case 3072:
  594. return PKE_RSA_DP2_3072;
  595. case 4096:
  596. return PKE_RSA_DP2_4096;
  597. default:
  598. return 0;
  599. };
  600. }
  601. static int qat_rsa_enc(struct akcipher_request *req)
  602. {
  603. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  604. struct qat_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
  605. struct qat_crypto_instance *inst = ctx->inst;
  606. struct device *dev = &GET_DEV(inst->accel_dev);
  607. struct qat_asym_request *qat_req =
  608. PTR_ALIGN(akcipher_request_ctx(req), 64);
  609. struct icp_qat_fw_pke_request *msg = &qat_req->req;
  610. int ret, ctr = 0;
  611. if (unlikely(!ctx->n || !ctx->e))
  612. return -EINVAL;
  613. if (req->dst_len < ctx->key_sz) {
  614. req->dst_len = ctx->key_sz;
  615. return -EOVERFLOW;
  616. }
  617. memset(msg, '\0', sizeof(*msg));
  618. ICP_QAT_FW_PKE_HDR_VALID_FLAG_SET(msg->pke_hdr,
  619. ICP_QAT_FW_COMN_REQ_FLAG_SET);
  620. msg->pke_hdr.cd_pars.func_id = qat_rsa_enc_fn_id(ctx->key_sz);
  621. if (unlikely(!msg->pke_hdr.cd_pars.func_id))
  622. return -EINVAL;
  623. qat_req->cb = qat_rsa_cb;
  624. qat_req->ctx.rsa = ctx;
  625. qat_req->areq.rsa = req;
  626. msg->pke_hdr.service_type = ICP_QAT_FW_COMN_REQ_CPM_FW_PKE;
  627. msg->pke_hdr.comn_req_flags =
  628. ICP_QAT_FW_COMN_FLAGS_BUILD(QAT_COMN_PTR_TYPE_FLAT,
  629. QAT_COMN_CD_FLD_TYPE_64BIT_ADR);
  630. qat_req->in.rsa.enc.e = ctx->dma_e;
  631. qat_req->in.rsa.enc.n = ctx->dma_n;
  632. ret = -ENOMEM;
  633. /*
  634. * src can be of any size in valid range, but HW expects it to be the
  635. * same as modulo n so in case it is different we need to allocate a
  636. * new buf and copy src data.
  637. * In other case we just need to map the user provided buffer.
  638. * Also need to make sure that it is in contiguous buffer.
  639. */
  640. if (sg_is_last(req->src) && req->src_len == ctx->key_sz) {
  641. qat_req->src_align = NULL;
  642. qat_req->in.rsa.enc.m = dma_map_single(dev, sg_virt(req->src),
  643. req->src_len, DMA_TO_DEVICE);
  644. if (unlikely(dma_mapping_error(dev, qat_req->in.rsa.enc.m)))
  645. return ret;
  646. } else {
  647. int shift = ctx->key_sz - req->src_len;
  648. qat_req->src_align = dma_zalloc_coherent(dev, ctx->key_sz,
  649. &qat_req->in.rsa.enc.m,
  650. GFP_KERNEL);
  651. if (unlikely(!qat_req->src_align))
  652. return ret;
  653. scatterwalk_map_and_copy(qat_req->src_align + shift, req->src,
  654. 0, req->src_len, 0);
  655. }
  656. if (sg_is_last(req->dst) && req->dst_len == ctx->key_sz) {
  657. qat_req->dst_align = NULL;
  658. qat_req->out.rsa.enc.c = dma_map_single(dev, sg_virt(req->dst),
  659. req->dst_len,
  660. DMA_FROM_DEVICE);
  661. if (unlikely(dma_mapping_error(dev, qat_req->out.rsa.enc.c)))
  662. goto unmap_src;
  663. } else {
  664. qat_req->dst_align = dma_zalloc_coherent(dev, ctx->key_sz,
  665. &qat_req->out.rsa.enc.c,
  666. GFP_KERNEL);
  667. if (unlikely(!qat_req->dst_align))
  668. goto unmap_src;
  669. }
  670. qat_req->in.rsa.in_tab[3] = 0;
  671. qat_req->out.rsa.out_tab[1] = 0;
  672. qat_req->phy_in = dma_map_single(dev, &qat_req->in.rsa.enc.m,
  673. sizeof(struct qat_rsa_input_params),
  674. DMA_TO_DEVICE);
  675. if (unlikely(dma_mapping_error(dev, qat_req->phy_in)))
  676. goto unmap_dst;
  677. qat_req->phy_out = dma_map_single(dev, &qat_req->out.rsa.enc.c,
  678. sizeof(struct qat_rsa_output_params),
  679. DMA_TO_DEVICE);
  680. if (unlikely(dma_mapping_error(dev, qat_req->phy_out)))
  681. goto unmap_in_params;
  682. msg->pke_mid.src_data_addr = qat_req->phy_in;
  683. msg->pke_mid.dest_data_addr = qat_req->phy_out;
  684. msg->pke_mid.opaque = (uint64_t)(__force long)qat_req;
  685. msg->input_param_count = 3;
  686. msg->output_param_count = 1;
  687. do {
  688. ret = adf_send_message(ctx->inst->pke_tx, (uint32_t *)msg);
  689. } while (ret == -EBUSY && ctr++ < 100);
  690. if (!ret)
  691. return -EINPROGRESS;
  692. if (!dma_mapping_error(dev, qat_req->phy_out))
  693. dma_unmap_single(dev, qat_req->phy_out,
  694. sizeof(struct qat_rsa_output_params),
  695. DMA_TO_DEVICE);
  696. unmap_in_params:
  697. if (!dma_mapping_error(dev, qat_req->phy_in))
  698. dma_unmap_single(dev, qat_req->phy_in,
  699. sizeof(struct qat_rsa_input_params),
  700. DMA_TO_DEVICE);
  701. unmap_dst:
  702. if (qat_req->dst_align)
  703. dma_free_coherent(dev, ctx->key_sz, qat_req->dst_align,
  704. qat_req->out.rsa.enc.c);
  705. else
  706. if (!dma_mapping_error(dev, qat_req->out.rsa.enc.c))
  707. dma_unmap_single(dev, qat_req->out.rsa.enc.c,
  708. ctx->key_sz, DMA_FROM_DEVICE);
  709. unmap_src:
  710. if (qat_req->src_align)
  711. dma_free_coherent(dev, ctx->key_sz, qat_req->src_align,
  712. qat_req->in.rsa.enc.m);
  713. else
  714. if (!dma_mapping_error(dev, qat_req->in.rsa.enc.m))
  715. dma_unmap_single(dev, qat_req->in.rsa.enc.m,
  716. ctx->key_sz, DMA_TO_DEVICE);
  717. return ret;
  718. }
  719. static int qat_rsa_dec(struct akcipher_request *req)
  720. {
  721. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  722. struct qat_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
  723. struct qat_crypto_instance *inst = ctx->inst;
  724. struct device *dev = &GET_DEV(inst->accel_dev);
  725. struct qat_asym_request *qat_req =
  726. PTR_ALIGN(akcipher_request_ctx(req), 64);
  727. struct icp_qat_fw_pke_request *msg = &qat_req->req;
  728. int ret, ctr = 0;
  729. if (unlikely(!ctx->n || !ctx->d))
  730. return -EINVAL;
  731. if (req->dst_len < ctx->key_sz) {
  732. req->dst_len = ctx->key_sz;
  733. return -EOVERFLOW;
  734. }
  735. memset(msg, '\0', sizeof(*msg));
  736. ICP_QAT_FW_PKE_HDR_VALID_FLAG_SET(msg->pke_hdr,
  737. ICP_QAT_FW_COMN_REQ_FLAG_SET);
  738. msg->pke_hdr.cd_pars.func_id = ctx->crt_mode ?
  739. qat_rsa_dec_fn_id_crt(ctx->key_sz) :
  740. qat_rsa_dec_fn_id(ctx->key_sz);
  741. if (unlikely(!msg->pke_hdr.cd_pars.func_id))
  742. return -EINVAL;
  743. qat_req->cb = qat_rsa_cb;
  744. qat_req->ctx.rsa = ctx;
  745. qat_req->areq.rsa = req;
  746. msg->pke_hdr.service_type = ICP_QAT_FW_COMN_REQ_CPM_FW_PKE;
  747. msg->pke_hdr.comn_req_flags =
  748. ICP_QAT_FW_COMN_FLAGS_BUILD(QAT_COMN_PTR_TYPE_FLAT,
  749. QAT_COMN_CD_FLD_TYPE_64BIT_ADR);
  750. if (ctx->crt_mode) {
  751. qat_req->in.rsa.dec_crt.p = ctx->dma_p;
  752. qat_req->in.rsa.dec_crt.q = ctx->dma_q;
  753. qat_req->in.rsa.dec_crt.dp = ctx->dma_dp;
  754. qat_req->in.rsa.dec_crt.dq = ctx->dma_dq;
  755. qat_req->in.rsa.dec_crt.qinv = ctx->dma_qinv;
  756. } else {
  757. qat_req->in.rsa.dec.d = ctx->dma_d;
  758. qat_req->in.rsa.dec.n = ctx->dma_n;
  759. }
  760. ret = -ENOMEM;
  761. /*
  762. * src can be of any size in valid range, but HW expects it to be the
  763. * same as modulo n so in case it is different we need to allocate a
  764. * new buf and copy src data.
  765. * In other case we just need to map the user provided buffer.
  766. * Also need to make sure that it is in contiguous buffer.
  767. */
  768. if (sg_is_last(req->src) && req->src_len == ctx->key_sz) {
  769. qat_req->src_align = NULL;
  770. qat_req->in.rsa.dec.c = dma_map_single(dev, sg_virt(req->src),
  771. req->dst_len, DMA_TO_DEVICE);
  772. if (unlikely(dma_mapping_error(dev, qat_req->in.rsa.dec.c)))
  773. return ret;
  774. } else {
  775. int shift = ctx->key_sz - req->src_len;
  776. qat_req->src_align = dma_zalloc_coherent(dev, ctx->key_sz,
  777. &qat_req->in.rsa.dec.c,
  778. GFP_KERNEL);
  779. if (unlikely(!qat_req->src_align))
  780. return ret;
  781. scatterwalk_map_and_copy(qat_req->src_align + shift, req->src,
  782. 0, req->src_len, 0);
  783. }
  784. if (sg_is_last(req->dst) && req->dst_len == ctx->key_sz) {
  785. qat_req->dst_align = NULL;
  786. qat_req->out.rsa.dec.m = dma_map_single(dev, sg_virt(req->dst),
  787. req->dst_len,
  788. DMA_FROM_DEVICE);
  789. if (unlikely(dma_mapping_error(dev, qat_req->out.rsa.dec.m)))
  790. goto unmap_src;
  791. } else {
  792. qat_req->dst_align = dma_zalloc_coherent(dev, ctx->key_sz,
  793. &qat_req->out.rsa.dec.m,
  794. GFP_KERNEL);
  795. if (unlikely(!qat_req->dst_align))
  796. goto unmap_src;
  797. }
  798. if (ctx->crt_mode)
  799. qat_req->in.rsa.in_tab[6] = 0;
  800. else
  801. qat_req->in.rsa.in_tab[3] = 0;
  802. qat_req->out.rsa.out_tab[1] = 0;
  803. qat_req->phy_in = dma_map_single(dev, &qat_req->in.rsa.dec.c,
  804. sizeof(struct qat_rsa_input_params),
  805. DMA_TO_DEVICE);
  806. if (unlikely(dma_mapping_error(dev, qat_req->phy_in)))
  807. goto unmap_dst;
  808. qat_req->phy_out = dma_map_single(dev, &qat_req->out.rsa.dec.m,
  809. sizeof(struct qat_rsa_output_params),
  810. DMA_TO_DEVICE);
  811. if (unlikely(dma_mapping_error(dev, qat_req->phy_out)))
  812. goto unmap_in_params;
  813. msg->pke_mid.src_data_addr = qat_req->phy_in;
  814. msg->pke_mid.dest_data_addr = qat_req->phy_out;
  815. msg->pke_mid.opaque = (uint64_t)(__force long)qat_req;
  816. if (ctx->crt_mode)
  817. msg->input_param_count = 6;
  818. else
  819. msg->input_param_count = 3;
  820. msg->output_param_count = 1;
  821. do {
  822. ret = adf_send_message(ctx->inst->pke_tx, (uint32_t *)msg);
  823. } while (ret == -EBUSY && ctr++ < 100);
  824. if (!ret)
  825. return -EINPROGRESS;
  826. if (!dma_mapping_error(dev, qat_req->phy_out))
  827. dma_unmap_single(dev, qat_req->phy_out,
  828. sizeof(struct qat_rsa_output_params),
  829. DMA_TO_DEVICE);
  830. unmap_in_params:
  831. if (!dma_mapping_error(dev, qat_req->phy_in))
  832. dma_unmap_single(dev, qat_req->phy_in,
  833. sizeof(struct qat_rsa_input_params),
  834. DMA_TO_DEVICE);
  835. unmap_dst:
  836. if (qat_req->dst_align)
  837. dma_free_coherent(dev, ctx->key_sz, qat_req->dst_align,
  838. qat_req->out.rsa.dec.m);
  839. else
  840. if (!dma_mapping_error(dev, qat_req->out.rsa.dec.m))
  841. dma_unmap_single(dev, qat_req->out.rsa.dec.m,
  842. ctx->key_sz, DMA_FROM_DEVICE);
  843. unmap_src:
  844. if (qat_req->src_align)
  845. dma_free_coherent(dev, ctx->key_sz, qat_req->src_align,
  846. qat_req->in.rsa.dec.c);
  847. else
  848. if (!dma_mapping_error(dev, qat_req->in.rsa.dec.c))
  849. dma_unmap_single(dev, qat_req->in.rsa.dec.c,
  850. ctx->key_sz, DMA_TO_DEVICE);
  851. return ret;
  852. }
  853. static int qat_rsa_set_n(struct qat_rsa_ctx *ctx, const char *value,
  854. size_t vlen)
  855. {
  856. struct qat_crypto_instance *inst = ctx->inst;
  857. struct device *dev = &GET_DEV(inst->accel_dev);
  858. const char *ptr = value;
  859. int ret;
  860. while (!*ptr && vlen) {
  861. ptr++;
  862. vlen--;
  863. }
  864. ctx->key_sz = vlen;
  865. ret = -EINVAL;
  866. /* invalid key size provided */
  867. if (!qat_rsa_enc_fn_id(ctx->key_sz))
  868. goto err;
  869. ret = -ENOMEM;
  870. ctx->n = dma_zalloc_coherent(dev, ctx->key_sz, &ctx->dma_n, GFP_KERNEL);
  871. if (!ctx->n)
  872. goto err;
  873. memcpy(ctx->n, ptr, ctx->key_sz);
  874. return 0;
  875. err:
  876. ctx->key_sz = 0;
  877. ctx->n = NULL;
  878. return ret;
  879. }
  880. static int qat_rsa_set_e(struct qat_rsa_ctx *ctx, const char *value,
  881. size_t vlen)
  882. {
  883. struct qat_crypto_instance *inst = ctx->inst;
  884. struct device *dev = &GET_DEV(inst->accel_dev);
  885. const char *ptr = value;
  886. while (!*ptr && vlen) {
  887. ptr++;
  888. vlen--;
  889. }
  890. if (!ctx->key_sz || !vlen || vlen > ctx->key_sz) {
  891. ctx->e = NULL;
  892. return -EINVAL;
  893. }
  894. ctx->e = dma_zalloc_coherent(dev, ctx->key_sz, &ctx->dma_e, GFP_KERNEL);
  895. if (!ctx->e)
  896. return -ENOMEM;
  897. memcpy(ctx->e + (ctx->key_sz - vlen), ptr, vlen);
  898. return 0;
  899. }
  900. static int qat_rsa_set_d(struct qat_rsa_ctx *ctx, const char *value,
  901. size_t vlen)
  902. {
  903. struct qat_crypto_instance *inst = ctx->inst;
  904. struct device *dev = &GET_DEV(inst->accel_dev);
  905. const char *ptr = value;
  906. int ret;
  907. while (!*ptr && vlen) {
  908. ptr++;
  909. vlen--;
  910. }
  911. ret = -EINVAL;
  912. if (!ctx->key_sz || !vlen || vlen > ctx->key_sz)
  913. goto err;
  914. ret = -ENOMEM;
  915. ctx->d = dma_zalloc_coherent(dev, ctx->key_sz, &ctx->dma_d, GFP_KERNEL);
  916. if (!ctx->d)
  917. goto err;
  918. memcpy(ctx->d + (ctx->key_sz - vlen), ptr, vlen);
  919. return 0;
  920. err:
  921. ctx->d = NULL;
  922. return ret;
  923. }
  924. static void qat_rsa_drop_leading_zeros(const char **ptr, unsigned int *len)
  925. {
  926. while (!**ptr && *len) {
  927. (*ptr)++;
  928. (*len)--;
  929. }
  930. }
  931. static void qat_rsa_setkey_crt(struct qat_rsa_ctx *ctx, struct rsa_key *rsa_key)
  932. {
  933. struct qat_crypto_instance *inst = ctx->inst;
  934. struct device *dev = &GET_DEV(inst->accel_dev);
  935. const char *ptr;
  936. unsigned int len;
  937. unsigned int half_key_sz = ctx->key_sz / 2;
  938. /* p */
  939. ptr = rsa_key->p;
  940. len = rsa_key->p_sz;
  941. qat_rsa_drop_leading_zeros(&ptr, &len);
  942. if (!len)
  943. goto err;
  944. ctx->p = dma_zalloc_coherent(dev, half_key_sz, &ctx->dma_p, GFP_KERNEL);
  945. if (!ctx->p)
  946. goto err;
  947. memcpy(ctx->p + (half_key_sz - len), ptr, len);
  948. /* q */
  949. ptr = rsa_key->q;
  950. len = rsa_key->q_sz;
  951. qat_rsa_drop_leading_zeros(&ptr, &len);
  952. if (!len)
  953. goto free_p;
  954. ctx->q = dma_zalloc_coherent(dev, half_key_sz, &ctx->dma_q, GFP_KERNEL);
  955. if (!ctx->q)
  956. goto free_p;
  957. memcpy(ctx->q + (half_key_sz - len), ptr, len);
  958. /* dp */
  959. ptr = rsa_key->dp;
  960. len = rsa_key->dp_sz;
  961. qat_rsa_drop_leading_zeros(&ptr, &len);
  962. if (!len)
  963. goto free_q;
  964. ctx->dp = dma_zalloc_coherent(dev, half_key_sz, &ctx->dma_dp,
  965. GFP_KERNEL);
  966. if (!ctx->dp)
  967. goto free_q;
  968. memcpy(ctx->dp + (half_key_sz - len), ptr, len);
  969. /* dq */
  970. ptr = rsa_key->dq;
  971. len = rsa_key->dq_sz;
  972. qat_rsa_drop_leading_zeros(&ptr, &len);
  973. if (!len)
  974. goto free_dp;
  975. ctx->dq = dma_zalloc_coherent(dev, half_key_sz, &ctx->dma_dq,
  976. GFP_KERNEL);
  977. if (!ctx->dq)
  978. goto free_dp;
  979. memcpy(ctx->dq + (half_key_sz - len), ptr, len);
  980. /* qinv */
  981. ptr = rsa_key->qinv;
  982. len = rsa_key->qinv_sz;
  983. qat_rsa_drop_leading_zeros(&ptr, &len);
  984. if (!len)
  985. goto free_dq;
  986. ctx->qinv = dma_zalloc_coherent(dev, half_key_sz, &ctx->dma_qinv,
  987. GFP_KERNEL);
  988. if (!ctx->qinv)
  989. goto free_dq;
  990. memcpy(ctx->qinv + (half_key_sz - len), ptr, len);
  991. ctx->crt_mode = true;
  992. return;
  993. free_dq:
  994. memset(ctx->dq, '\0', half_key_sz);
  995. dma_free_coherent(dev, half_key_sz, ctx->dq, ctx->dma_dq);
  996. ctx->dq = NULL;
  997. free_dp:
  998. memset(ctx->dp, '\0', half_key_sz);
  999. dma_free_coherent(dev, half_key_sz, ctx->dp, ctx->dma_dp);
  1000. ctx->dp = NULL;
  1001. free_q:
  1002. memset(ctx->q, '\0', half_key_sz);
  1003. dma_free_coherent(dev, half_key_sz, ctx->q, ctx->dma_q);
  1004. ctx->q = NULL;
  1005. free_p:
  1006. memset(ctx->p, '\0', half_key_sz);
  1007. dma_free_coherent(dev, half_key_sz, ctx->p, ctx->dma_p);
  1008. ctx->p = NULL;
  1009. err:
  1010. ctx->crt_mode = false;
  1011. }
  1012. static void qat_rsa_clear_ctx(struct device *dev, struct qat_rsa_ctx *ctx)
  1013. {
  1014. unsigned int half_key_sz = ctx->key_sz / 2;
  1015. /* Free the old key if any */
  1016. if (ctx->n)
  1017. dma_free_coherent(dev, ctx->key_sz, ctx->n, ctx->dma_n);
  1018. if (ctx->e)
  1019. dma_free_coherent(dev, ctx->key_sz, ctx->e, ctx->dma_e);
  1020. if (ctx->d) {
  1021. memset(ctx->d, '\0', ctx->key_sz);
  1022. dma_free_coherent(dev, ctx->key_sz, ctx->d, ctx->dma_d);
  1023. }
  1024. if (ctx->p) {
  1025. memset(ctx->p, '\0', half_key_sz);
  1026. dma_free_coherent(dev, half_key_sz, ctx->p, ctx->dma_p);
  1027. }
  1028. if (ctx->q) {
  1029. memset(ctx->q, '\0', half_key_sz);
  1030. dma_free_coherent(dev, half_key_sz, ctx->q, ctx->dma_q);
  1031. }
  1032. if (ctx->dp) {
  1033. memset(ctx->dp, '\0', half_key_sz);
  1034. dma_free_coherent(dev, half_key_sz, ctx->dp, ctx->dma_dp);
  1035. }
  1036. if (ctx->dq) {
  1037. memset(ctx->dq, '\0', half_key_sz);
  1038. dma_free_coherent(dev, half_key_sz, ctx->dq, ctx->dma_dq);
  1039. }
  1040. if (ctx->qinv) {
  1041. memset(ctx->qinv, '\0', half_key_sz);
  1042. dma_free_coherent(dev, half_key_sz, ctx->qinv, ctx->dma_qinv);
  1043. }
  1044. ctx->n = NULL;
  1045. ctx->e = NULL;
  1046. ctx->d = NULL;
  1047. ctx->p = NULL;
  1048. ctx->q = NULL;
  1049. ctx->dp = NULL;
  1050. ctx->dq = NULL;
  1051. ctx->qinv = NULL;
  1052. ctx->crt_mode = false;
  1053. ctx->key_sz = 0;
  1054. }
  1055. static int qat_rsa_setkey(struct crypto_akcipher *tfm, const void *key,
  1056. unsigned int keylen, bool private)
  1057. {
  1058. struct qat_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
  1059. struct device *dev = &GET_DEV(ctx->inst->accel_dev);
  1060. struct rsa_key rsa_key;
  1061. int ret;
  1062. qat_rsa_clear_ctx(dev, ctx);
  1063. if (private)
  1064. ret = rsa_parse_priv_key(&rsa_key, key, keylen);
  1065. else
  1066. ret = rsa_parse_pub_key(&rsa_key, key, keylen);
  1067. if (ret < 0)
  1068. goto free;
  1069. ret = qat_rsa_set_n(ctx, rsa_key.n, rsa_key.n_sz);
  1070. if (ret < 0)
  1071. goto free;
  1072. ret = qat_rsa_set_e(ctx, rsa_key.e, rsa_key.e_sz);
  1073. if (ret < 0)
  1074. goto free;
  1075. if (private) {
  1076. ret = qat_rsa_set_d(ctx, rsa_key.d, rsa_key.d_sz);
  1077. if (ret < 0)
  1078. goto free;
  1079. qat_rsa_setkey_crt(ctx, &rsa_key);
  1080. }
  1081. if (!ctx->n || !ctx->e) {
  1082. /* invalid key provided */
  1083. ret = -EINVAL;
  1084. goto free;
  1085. }
  1086. if (private && !ctx->d) {
  1087. /* invalid private key provided */
  1088. ret = -EINVAL;
  1089. goto free;
  1090. }
  1091. return 0;
  1092. free:
  1093. qat_rsa_clear_ctx(dev, ctx);
  1094. return ret;
  1095. }
  1096. static int qat_rsa_setpubkey(struct crypto_akcipher *tfm, const void *key,
  1097. unsigned int keylen)
  1098. {
  1099. return qat_rsa_setkey(tfm, key, keylen, false);
  1100. }
  1101. static int qat_rsa_setprivkey(struct crypto_akcipher *tfm, const void *key,
  1102. unsigned int keylen)
  1103. {
  1104. return qat_rsa_setkey(tfm, key, keylen, true);
  1105. }
  1106. static unsigned int qat_rsa_max_size(struct crypto_akcipher *tfm)
  1107. {
  1108. struct qat_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
  1109. return ctx->key_sz;
  1110. }
  1111. static int qat_rsa_init_tfm(struct crypto_akcipher *tfm)
  1112. {
  1113. struct qat_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
  1114. struct qat_crypto_instance *inst =
  1115. qat_crypto_get_instance_node(get_current_node());
  1116. if (!inst)
  1117. return -EINVAL;
  1118. ctx->key_sz = 0;
  1119. ctx->inst = inst;
  1120. return 0;
  1121. }
  1122. static void qat_rsa_exit_tfm(struct crypto_akcipher *tfm)
  1123. {
  1124. struct qat_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
  1125. struct device *dev = &GET_DEV(ctx->inst->accel_dev);
  1126. if (ctx->n)
  1127. dma_free_coherent(dev, ctx->key_sz, ctx->n, ctx->dma_n);
  1128. if (ctx->e)
  1129. dma_free_coherent(dev, ctx->key_sz, ctx->e, ctx->dma_e);
  1130. if (ctx->d) {
  1131. memset(ctx->d, '\0', ctx->key_sz);
  1132. dma_free_coherent(dev, ctx->key_sz, ctx->d, ctx->dma_d);
  1133. }
  1134. qat_crypto_put_instance(ctx->inst);
  1135. ctx->n = NULL;
  1136. ctx->e = NULL;
  1137. ctx->d = NULL;
  1138. }
  1139. static struct akcipher_alg rsa = {
  1140. .encrypt = qat_rsa_enc,
  1141. .decrypt = qat_rsa_dec,
  1142. .sign = qat_rsa_dec,
  1143. .verify = qat_rsa_enc,
  1144. .set_pub_key = qat_rsa_setpubkey,
  1145. .set_priv_key = qat_rsa_setprivkey,
  1146. .max_size = qat_rsa_max_size,
  1147. .init = qat_rsa_init_tfm,
  1148. .exit = qat_rsa_exit_tfm,
  1149. .reqsize = sizeof(struct qat_asym_request) + 64,
  1150. .base = {
  1151. .cra_name = "rsa",
  1152. .cra_driver_name = "qat-rsa",
  1153. .cra_priority = 1000,
  1154. .cra_module = THIS_MODULE,
  1155. .cra_ctxsize = sizeof(struct qat_rsa_ctx),
  1156. },
  1157. };
  1158. static struct kpp_alg dh = {
  1159. .set_secret = qat_dh_set_secret,
  1160. .generate_public_key = qat_dh_compute_value,
  1161. .compute_shared_secret = qat_dh_compute_value,
  1162. .max_size = qat_dh_max_size,
  1163. .init = qat_dh_init_tfm,
  1164. .exit = qat_dh_exit_tfm,
  1165. .reqsize = sizeof(struct qat_asym_request) + 64,
  1166. .base = {
  1167. .cra_name = "dh",
  1168. .cra_driver_name = "qat-dh",
  1169. .cra_priority = 1000,
  1170. .cra_module = THIS_MODULE,
  1171. .cra_ctxsize = sizeof(struct qat_dh_ctx),
  1172. },
  1173. };
  1174. int qat_asym_algs_register(void)
  1175. {
  1176. int ret = 0;
  1177. mutex_lock(&algs_lock);
  1178. if (++active_devs == 1) {
  1179. rsa.base.cra_flags = 0;
  1180. ret = crypto_register_akcipher(&rsa);
  1181. if (ret)
  1182. goto unlock;
  1183. ret = crypto_register_kpp(&dh);
  1184. }
  1185. unlock:
  1186. mutex_unlock(&algs_lock);
  1187. return ret;
  1188. }
  1189. void qat_asym_algs_unregister(void)
  1190. {
  1191. mutex_lock(&algs_lock);
  1192. if (--active_devs == 0) {
  1193. crypto_unregister_akcipher(&rsa);
  1194. crypto_unregister_kpp(&dh);
  1195. }
  1196. mutex_unlock(&algs_lock);
  1197. }