qat_asym_algs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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 <linux/dma-mapping.h>
  46. #include <linux/fips.h>
  47. #include <crypto/scatterwalk.h>
  48. #include "qat_rsapubkey-asn1.h"
  49. #include "qat_rsaprivkey-asn1.h"
  50. #include "icp_qat_fw_pke.h"
  51. #include "adf_accel_devices.h"
  52. #include "adf_transport.h"
  53. #include "adf_common_drv.h"
  54. #include "qat_crypto.h"
  55. static DEFINE_MUTEX(algs_lock);
  56. static unsigned int active_devs;
  57. struct qat_rsa_input_params {
  58. union {
  59. struct {
  60. dma_addr_t m;
  61. dma_addr_t e;
  62. dma_addr_t n;
  63. } enc;
  64. struct {
  65. dma_addr_t c;
  66. dma_addr_t d;
  67. dma_addr_t n;
  68. } dec;
  69. u64 in_tab[8];
  70. };
  71. } __packed __aligned(64);
  72. struct qat_rsa_output_params {
  73. union {
  74. struct {
  75. dma_addr_t c;
  76. } enc;
  77. struct {
  78. dma_addr_t m;
  79. } dec;
  80. u64 out_tab[8];
  81. };
  82. } __packed __aligned(64);
  83. struct qat_rsa_ctx {
  84. char *n;
  85. char *e;
  86. char *d;
  87. dma_addr_t dma_n;
  88. dma_addr_t dma_e;
  89. dma_addr_t dma_d;
  90. unsigned int key_sz;
  91. struct qat_crypto_instance *inst;
  92. } __packed __aligned(64);
  93. struct qat_rsa_request {
  94. struct qat_rsa_input_params in;
  95. struct qat_rsa_output_params out;
  96. dma_addr_t phy_in;
  97. dma_addr_t phy_out;
  98. char *src_align;
  99. char *dst_align;
  100. struct icp_qat_fw_pke_request req;
  101. struct qat_rsa_ctx *ctx;
  102. int err;
  103. } __aligned(64);
  104. static void qat_rsa_cb(struct icp_qat_fw_pke_resp *resp)
  105. {
  106. struct akcipher_request *areq = (void *)(__force long)resp->opaque;
  107. struct qat_rsa_request *req = PTR_ALIGN(akcipher_request_ctx(areq), 64);
  108. struct device *dev = &GET_DEV(req->ctx->inst->accel_dev);
  109. int err = ICP_QAT_FW_PKE_RESP_PKE_STAT_GET(
  110. resp->pke_resp_hdr.comn_resp_flags);
  111. err = (err == ICP_QAT_FW_COMN_STATUS_FLAG_OK) ? 0 : -EINVAL;
  112. if (req->src_align)
  113. dma_free_coherent(dev, req->ctx->key_sz, req->src_align,
  114. req->in.enc.m);
  115. else
  116. dma_unmap_single(dev, req->in.enc.m, req->ctx->key_sz,
  117. DMA_TO_DEVICE);
  118. areq->dst_len = req->ctx->key_sz;
  119. if (req->dst_align) {
  120. char *ptr = req->dst_align;
  121. while (!(*ptr) && areq->dst_len) {
  122. areq->dst_len--;
  123. ptr++;
  124. }
  125. if (areq->dst_len != req->ctx->key_sz)
  126. memmove(req->dst_align, ptr, areq->dst_len);
  127. scatterwalk_map_and_copy(req->dst_align, areq->dst, 0,
  128. areq->dst_len, 1);
  129. dma_free_coherent(dev, req->ctx->key_sz, req->dst_align,
  130. req->out.enc.c);
  131. } else {
  132. char *ptr = sg_virt(areq->dst);
  133. while (!(*ptr) && areq->dst_len) {
  134. areq->dst_len--;
  135. ptr++;
  136. }
  137. if (sg_virt(areq->dst) != ptr && areq->dst_len)
  138. memmove(sg_virt(areq->dst), ptr, areq->dst_len);
  139. dma_unmap_single(dev, req->out.enc.c, req->ctx->key_sz,
  140. DMA_FROM_DEVICE);
  141. }
  142. dma_unmap_single(dev, req->phy_in, sizeof(struct qat_rsa_input_params),
  143. DMA_TO_DEVICE);
  144. dma_unmap_single(dev, req->phy_out,
  145. sizeof(struct qat_rsa_output_params),
  146. DMA_TO_DEVICE);
  147. akcipher_request_complete(areq, err);
  148. }
  149. void qat_alg_asym_callback(void *_resp)
  150. {
  151. struct icp_qat_fw_pke_resp *resp = _resp;
  152. qat_rsa_cb(resp);
  153. }
  154. #define PKE_RSA_EP_512 0x1c161b21
  155. #define PKE_RSA_EP_1024 0x35111bf7
  156. #define PKE_RSA_EP_1536 0x4d111cdc
  157. #define PKE_RSA_EP_2048 0x6e111dba
  158. #define PKE_RSA_EP_3072 0x7d111ea3
  159. #define PKE_RSA_EP_4096 0xa5101f7e
  160. static unsigned long qat_rsa_enc_fn_id(unsigned int len)
  161. {
  162. unsigned int bitslen = len << 3;
  163. switch (bitslen) {
  164. case 512:
  165. return PKE_RSA_EP_512;
  166. case 1024:
  167. return PKE_RSA_EP_1024;
  168. case 1536:
  169. return PKE_RSA_EP_1536;
  170. case 2048:
  171. return PKE_RSA_EP_2048;
  172. case 3072:
  173. return PKE_RSA_EP_3072;
  174. case 4096:
  175. return PKE_RSA_EP_4096;
  176. default:
  177. return 0;
  178. };
  179. }
  180. #define PKE_RSA_DP1_512 0x1c161b3c
  181. #define PKE_RSA_DP1_1024 0x35111c12
  182. #define PKE_RSA_DP1_1536 0x4d111cf7
  183. #define PKE_RSA_DP1_2048 0x6e111dda
  184. #define PKE_RSA_DP1_3072 0x7d111ebe
  185. #define PKE_RSA_DP1_4096 0xa5101f98
  186. static unsigned long qat_rsa_dec_fn_id(unsigned int len)
  187. {
  188. unsigned int bitslen = len << 3;
  189. switch (bitslen) {
  190. case 512:
  191. return PKE_RSA_DP1_512;
  192. case 1024:
  193. return PKE_RSA_DP1_1024;
  194. case 1536:
  195. return PKE_RSA_DP1_1536;
  196. case 2048:
  197. return PKE_RSA_DP1_2048;
  198. case 3072:
  199. return PKE_RSA_DP1_3072;
  200. case 4096:
  201. return PKE_RSA_DP1_4096;
  202. default:
  203. return 0;
  204. };
  205. }
  206. static int qat_rsa_enc(struct akcipher_request *req)
  207. {
  208. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  209. struct qat_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
  210. struct qat_crypto_instance *inst = ctx->inst;
  211. struct device *dev = &GET_DEV(inst->accel_dev);
  212. struct qat_rsa_request *qat_req =
  213. PTR_ALIGN(akcipher_request_ctx(req), 64);
  214. struct icp_qat_fw_pke_request *msg = &qat_req->req;
  215. int ret, ctr = 0;
  216. if (unlikely(!ctx->n || !ctx->e))
  217. return -EINVAL;
  218. if (req->dst_len < ctx->key_sz) {
  219. req->dst_len = ctx->key_sz;
  220. return -EOVERFLOW;
  221. }
  222. memset(msg, '\0', sizeof(*msg));
  223. ICP_QAT_FW_PKE_HDR_VALID_FLAG_SET(msg->pke_hdr,
  224. ICP_QAT_FW_COMN_REQ_FLAG_SET);
  225. msg->pke_hdr.cd_pars.func_id = qat_rsa_enc_fn_id(ctx->key_sz);
  226. if (unlikely(!msg->pke_hdr.cd_pars.func_id))
  227. return -EINVAL;
  228. qat_req->ctx = ctx;
  229. msg->pke_hdr.service_type = ICP_QAT_FW_COMN_REQ_CPM_FW_PKE;
  230. msg->pke_hdr.comn_req_flags =
  231. ICP_QAT_FW_COMN_FLAGS_BUILD(QAT_COMN_PTR_TYPE_FLAT,
  232. QAT_COMN_CD_FLD_TYPE_64BIT_ADR);
  233. qat_req->in.enc.e = ctx->dma_e;
  234. qat_req->in.enc.n = ctx->dma_n;
  235. ret = -ENOMEM;
  236. /*
  237. * src can be of any size in valid range, but HW expects it to be the
  238. * same as modulo n so in case it is different we need to allocate a
  239. * new buf and copy src data.
  240. * In other case we just need to map the user provided buffer.
  241. * Also need to make sure that it is in contiguous buffer.
  242. */
  243. if (sg_is_last(req->src) && req->src_len == ctx->key_sz) {
  244. qat_req->src_align = NULL;
  245. qat_req->in.enc.m = dma_map_single(dev, sg_virt(req->src),
  246. req->src_len, DMA_TO_DEVICE);
  247. if (unlikely(dma_mapping_error(dev, qat_req->in.enc.m)))
  248. return ret;
  249. } else {
  250. int shift = ctx->key_sz - req->src_len;
  251. qat_req->src_align = dma_zalloc_coherent(dev, ctx->key_sz,
  252. &qat_req->in.enc.m,
  253. GFP_KERNEL);
  254. if (unlikely(!qat_req->src_align))
  255. return ret;
  256. scatterwalk_map_and_copy(qat_req->src_align + shift, req->src,
  257. 0, req->src_len, 0);
  258. }
  259. if (sg_is_last(req->dst) && req->dst_len == ctx->key_sz) {
  260. qat_req->dst_align = NULL;
  261. qat_req->out.enc.c = dma_map_single(dev, sg_virt(req->dst),
  262. req->dst_len,
  263. DMA_FROM_DEVICE);
  264. if (unlikely(dma_mapping_error(dev, qat_req->out.enc.c)))
  265. goto unmap_src;
  266. } else {
  267. qat_req->dst_align = dma_zalloc_coherent(dev, ctx->key_sz,
  268. &qat_req->out.enc.c,
  269. GFP_KERNEL);
  270. if (unlikely(!qat_req->dst_align))
  271. goto unmap_src;
  272. }
  273. qat_req->in.in_tab[3] = 0;
  274. qat_req->out.out_tab[1] = 0;
  275. qat_req->phy_in = dma_map_single(dev, &qat_req->in.enc.m,
  276. sizeof(struct qat_rsa_input_params),
  277. DMA_TO_DEVICE);
  278. if (unlikely(dma_mapping_error(dev, qat_req->phy_in)))
  279. goto unmap_dst;
  280. qat_req->phy_out = dma_map_single(dev, &qat_req->out.enc.c,
  281. sizeof(struct qat_rsa_output_params),
  282. DMA_TO_DEVICE);
  283. if (unlikely(dma_mapping_error(dev, qat_req->phy_out)))
  284. goto unmap_in_params;
  285. msg->pke_mid.src_data_addr = qat_req->phy_in;
  286. msg->pke_mid.dest_data_addr = qat_req->phy_out;
  287. msg->pke_mid.opaque = (uint64_t)(__force long)req;
  288. msg->input_param_count = 3;
  289. msg->output_param_count = 1;
  290. do {
  291. ret = adf_send_message(ctx->inst->pke_tx, (uint32_t *)msg);
  292. } while (ret == -EBUSY && ctr++ < 100);
  293. if (!ret)
  294. return -EINPROGRESS;
  295. unmap_src:
  296. if (qat_req->src_align)
  297. dma_free_coherent(dev, ctx->key_sz, qat_req->src_align,
  298. qat_req->in.enc.m);
  299. else
  300. if (!dma_mapping_error(dev, qat_req->in.enc.m))
  301. dma_unmap_single(dev, qat_req->in.enc.m, ctx->key_sz,
  302. DMA_TO_DEVICE);
  303. unmap_dst:
  304. if (qat_req->dst_align)
  305. dma_free_coherent(dev, ctx->key_sz, qat_req->dst_align,
  306. qat_req->out.enc.c);
  307. else
  308. if (!dma_mapping_error(dev, qat_req->out.enc.c))
  309. dma_unmap_single(dev, qat_req->out.enc.c, ctx->key_sz,
  310. DMA_FROM_DEVICE);
  311. unmap_in_params:
  312. if (!dma_mapping_error(dev, qat_req->phy_in))
  313. dma_unmap_single(dev, qat_req->phy_in,
  314. sizeof(struct qat_rsa_input_params),
  315. DMA_TO_DEVICE);
  316. if (!dma_mapping_error(dev, qat_req->phy_out))
  317. dma_unmap_single(dev, qat_req->phy_out,
  318. sizeof(struct qat_rsa_output_params),
  319. DMA_TO_DEVICE);
  320. return ret;
  321. }
  322. static int qat_rsa_dec(struct akcipher_request *req)
  323. {
  324. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  325. struct qat_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
  326. struct qat_crypto_instance *inst = ctx->inst;
  327. struct device *dev = &GET_DEV(inst->accel_dev);
  328. struct qat_rsa_request *qat_req =
  329. PTR_ALIGN(akcipher_request_ctx(req), 64);
  330. struct icp_qat_fw_pke_request *msg = &qat_req->req;
  331. int ret, ctr = 0;
  332. if (unlikely(!ctx->n || !ctx->d))
  333. return -EINVAL;
  334. if (req->dst_len < ctx->key_sz) {
  335. req->dst_len = ctx->key_sz;
  336. return -EOVERFLOW;
  337. }
  338. memset(msg, '\0', sizeof(*msg));
  339. ICP_QAT_FW_PKE_HDR_VALID_FLAG_SET(msg->pke_hdr,
  340. ICP_QAT_FW_COMN_REQ_FLAG_SET);
  341. msg->pke_hdr.cd_pars.func_id = qat_rsa_dec_fn_id(ctx->key_sz);
  342. if (unlikely(!msg->pke_hdr.cd_pars.func_id))
  343. return -EINVAL;
  344. qat_req->ctx = ctx;
  345. msg->pke_hdr.service_type = ICP_QAT_FW_COMN_REQ_CPM_FW_PKE;
  346. msg->pke_hdr.comn_req_flags =
  347. ICP_QAT_FW_COMN_FLAGS_BUILD(QAT_COMN_PTR_TYPE_FLAT,
  348. QAT_COMN_CD_FLD_TYPE_64BIT_ADR);
  349. qat_req->in.dec.d = ctx->dma_d;
  350. qat_req->in.dec.n = ctx->dma_n;
  351. ret = -ENOMEM;
  352. /*
  353. * src can be of any size in valid range, but HW expects it to be the
  354. * same as modulo n so in case it is different we need to allocate a
  355. * new buf and copy src data.
  356. * In other case we just need to map the user provided buffer.
  357. * Also need to make sure that it is in contiguous buffer.
  358. */
  359. if (sg_is_last(req->src) && req->src_len == ctx->key_sz) {
  360. qat_req->src_align = NULL;
  361. qat_req->in.dec.c = dma_map_single(dev, sg_virt(req->src),
  362. req->dst_len, DMA_TO_DEVICE);
  363. if (unlikely(dma_mapping_error(dev, qat_req->in.dec.c)))
  364. return ret;
  365. } else {
  366. int shift = ctx->key_sz - req->src_len;
  367. qat_req->src_align = dma_zalloc_coherent(dev, ctx->key_sz,
  368. &qat_req->in.dec.c,
  369. GFP_KERNEL);
  370. if (unlikely(!qat_req->src_align))
  371. return ret;
  372. scatterwalk_map_and_copy(qat_req->src_align + shift, req->src,
  373. 0, req->src_len, 0);
  374. }
  375. if (sg_is_last(req->dst) && req->dst_len == ctx->key_sz) {
  376. qat_req->dst_align = NULL;
  377. qat_req->out.dec.m = dma_map_single(dev, sg_virt(req->dst),
  378. req->dst_len,
  379. DMA_FROM_DEVICE);
  380. if (unlikely(dma_mapping_error(dev, qat_req->out.dec.m)))
  381. goto unmap_src;
  382. } else {
  383. qat_req->dst_align = dma_zalloc_coherent(dev, ctx->key_sz,
  384. &qat_req->out.dec.m,
  385. GFP_KERNEL);
  386. if (unlikely(!qat_req->dst_align))
  387. goto unmap_src;
  388. }
  389. qat_req->in.in_tab[3] = 0;
  390. qat_req->out.out_tab[1] = 0;
  391. qat_req->phy_in = dma_map_single(dev, &qat_req->in.dec.c,
  392. sizeof(struct qat_rsa_input_params),
  393. DMA_TO_DEVICE);
  394. if (unlikely(dma_mapping_error(dev, qat_req->phy_in)))
  395. goto unmap_dst;
  396. qat_req->phy_out = dma_map_single(dev, &qat_req->out.dec.m,
  397. sizeof(struct qat_rsa_output_params),
  398. DMA_TO_DEVICE);
  399. if (unlikely(dma_mapping_error(dev, qat_req->phy_out)))
  400. goto unmap_in_params;
  401. msg->pke_mid.src_data_addr = qat_req->phy_in;
  402. msg->pke_mid.dest_data_addr = qat_req->phy_out;
  403. msg->pke_mid.opaque = (uint64_t)(__force long)req;
  404. msg->input_param_count = 3;
  405. msg->output_param_count = 1;
  406. do {
  407. ret = adf_send_message(ctx->inst->pke_tx, (uint32_t *)msg);
  408. } while (ret == -EBUSY && ctr++ < 100);
  409. if (!ret)
  410. return -EINPROGRESS;
  411. unmap_src:
  412. if (qat_req->src_align)
  413. dma_free_coherent(dev, ctx->key_sz, qat_req->src_align,
  414. qat_req->in.dec.c);
  415. else
  416. if (!dma_mapping_error(dev, qat_req->in.dec.c))
  417. dma_unmap_single(dev, qat_req->in.dec.c, ctx->key_sz,
  418. DMA_TO_DEVICE);
  419. unmap_dst:
  420. if (qat_req->dst_align)
  421. dma_free_coherent(dev, ctx->key_sz, qat_req->dst_align,
  422. qat_req->out.dec.m);
  423. else
  424. if (!dma_mapping_error(dev, qat_req->out.dec.m))
  425. dma_unmap_single(dev, qat_req->out.dec.m, ctx->key_sz,
  426. DMA_FROM_DEVICE);
  427. unmap_in_params:
  428. if (!dma_mapping_error(dev, qat_req->phy_in))
  429. dma_unmap_single(dev, qat_req->phy_in,
  430. sizeof(struct qat_rsa_input_params),
  431. DMA_TO_DEVICE);
  432. if (!dma_mapping_error(dev, qat_req->phy_out))
  433. dma_unmap_single(dev, qat_req->phy_out,
  434. sizeof(struct qat_rsa_output_params),
  435. DMA_TO_DEVICE);
  436. return ret;
  437. }
  438. int qat_rsa_get_n(void *context, size_t hdrlen, unsigned char tag,
  439. const void *value, size_t vlen)
  440. {
  441. struct qat_rsa_ctx *ctx = context;
  442. struct qat_crypto_instance *inst = ctx->inst;
  443. struct device *dev = &GET_DEV(inst->accel_dev);
  444. const char *ptr = value;
  445. int ret;
  446. while (!*ptr && vlen) {
  447. ptr++;
  448. vlen--;
  449. }
  450. ctx->key_sz = vlen;
  451. ret = -EINVAL;
  452. /* In FIPS mode only allow key size 2K & 3K */
  453. if (fips_enabled && (ctx->key_sz != 256 && ctx->key_sz != 384)) {
  454. pr_err("QAT: RSA: key size not allowed in FIPS mode\n");
  455. goto err;
  456. }
  457. /* invalid key size provided */
  458. if (!qat_rsa_enc_fn_id(ctx->key_sz))
  459. goto err;
  460. ret = -ENOMEM;
  461. ctx->n = dma_zalloc_coherent(dev, ctx->key_sz, &ctx->dma_n, GFP_KERNEL);
  462. if (!ctx->n)
  463. goto err;
  464. memcpy(ctx->n, ptr, ctx->key_sz);
  465. return 0;
  466. err:
  467. ctx->key_sz = 0;
  468. ctx->n = NULL;
  469. return ret;
  470. }
  471. int qat_rsa_get_e(void *context, size_t hdrlen, unsigned char tag,
  472. const void *value, size_t vlen)
  473. {
  474. struct qat_rsa_ctx *ctx = context;
  475. struct qat_crypto_instance *inst = ctx->inst;
  476. struct device *dev = &GET_DEV(inst->accel_dev);
  477. const char *ptr = value;
  478. while (!*ptr && vlen) {
  479. ptr++;
  480. vlen--;
  481. }
  482. if (!ctx->key_sz || !vlen || vlen > ctx->key_sz) {
  483. ctx->e = NULL;
  484. return -EINVAL;
  485. }
  486. ctx->e = dma_zalloc_coherent(dev, ctx->key_sz, &ctx->dma_e, GFP_KERNEL);
  487. if (!ctx->e) {
  488. ctx->e = NULL;
  489. return -ENOMEM;
  490. }
  491. memcpy(ctx->e + (ctx->key_sz - vlen), ptr, vlen);
  492. return 0;
  493. }
  494. int qat_rsa_get_d(void *context, size_t hdrlen, unsigned char tag,
  495. const void *value, size_t vlen)
  496. {
  497. struct qat_rsa_ctx *ctx = context;
  498. struct qat_crypto_instance *inst = ctx->inst;
  499. struct device *dev = &GET_DEV(inst->accel_dev);
  500. const char *ptr = value;
  501. int ret;
  502. while (!*ptr && vlen) {
  503. ptr++;
  504. vlen--;
  505. }
  506. ret = -EINVAL;
  507. if (!ctx->key_sz || !vlen || vlen > ctx->key_sz)
  508. goto err;
  509. /* In FIPS mode only allow key size 2K & 3K */
  510. if (fips_enabled && (vlen != 256 && vlen != 384)) {
  511. pr_err("QAT: RSA: key size not allowed in FIPS mode\n");
  512. goto err;
  513. }
  514. ret = -ENOMEM;
  515. ctx->d = dma_zalloc_coherent(dev, ctx->key_sz, &ctx->dma_d, GFP_KERNEL);
  516. if (!ctx->n)
  517. goto err;
  518. memcpy(ctx->d + (ctx->key_sz - vlen), ptr, vlen);
  519. return 0;
  520. err:
  521. ctx->d = NULL;
  522. return ret;
  523. }
  524. static int qat_rsa_setkey(struct crypto_akcipher *tfm, const void *key,
  525. unsigned int keylen, bool private)
  526. {
  527. struct qat_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
  528. struct device *dev = &GET_DEV(ctx->inst->accel_dev);
  529. int ret;
  530. /* Free the old key if any */
  531. if (ctx->n)
  532. dma_free_coherent(dev, ctx->key_sz, ctx->n, ctx->dma_n);
  533. if (ctx->e)
  534. dma_free_coherent(dev, ctx->key_sz, ctx->e, ctx->dma_e);
  535. if (ctx->d) {
  536. memset(ctx->d, '\0', ctx->key_sz);
  537. dma_free_coherent(dev, ctx->key_sz, ctx->d, ctx->dma_d);
  538. }
  539. ctx->n = NULL;
  540. ctx->e = NULL;
  541. ctx->d = NULL;
  542. if (private)
  543. ret = asn1_ber_decoder(&qat_rsaprivkey_decoder, ctx, key,
  544. keylen);
  545. else
  546. ret = asn1_ber_decoder(&qat_rsapubkey_decoder, ctx, key,
  547. keylen);
  548. if (ret < 0)
  549. goto free;
  550. if (!ctx->n || !ctx->e) {
  551. /* invalid key provided */
  552. ret = -EINVAL;
  553. goto free;
  554. }
  555. if (private && !ctx->d) {
  556. /* invalid private key provided */
  557. ret = -EINVAL;
  558. goto free;
  559. }
  560. return 0;
  561. free:
  562. if (ctx->d) {
  563. memset(ctx->d, '\0', ctx->key_sz);
  564. dma_free_coherent(dev, ctx->key_sz, ctx->d, ctx->dma_d);
  565. ctx->d = NULL;
  566. }
  567. if (ctx->e) {
  568. dma_free_coherent(dev, ctx->key_sz, ctx->e, ctx->dma_e);
  569. ctx->e = NULL;
  570. }
  571. if (ctx->n) {
  572. dma_free_coherent(dev, ctx->key_sz, ctx->n, ctx->dma_n);
  573. ctx->n = NULL;
  574. ctx->key_sz = 0;
  575. }
  576. return ret;
  577. }
  578. static int qat_rsa_setpubkey(struct crypto_akcipher *tfm, const void *key,
  579. unsigned int keylen)
  580. {
  581. return qat_rsa_setkey(tfm, key, keylen, false);
  582. }
  583. static int qat_rsa_setprivkey(struct crypto_akcipher *tfm, const void *key,
  584. unsigned int keylen)
  585. {
  586. return qat_rsa_setkey(tfm, key, keylen, true);
  587. }
  588. static int qat_rsa_max_size(struct crypto_akcipher *tfm)
  589. {
  590. struct qat_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
  591. return (ctx->n) ? ctx->key_sz : -EINVAL;
  592. }
  593. static int qat_rsa_init_tfm(struct crypto_akcipher *tfm)
  594. {
  595. struct qat_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
  596. struct qat_crypto_instance *inst =
  597. qat_crypto_get_instance_node(get_current_node());
  598. if (!inst)
  599. return -EINVAL;
  600. ctx->key_sz = 0;
  601. ctx->inst = inst;
  602. return 0;
  603. }
  604. static void qat_rsa_exit_tfm(struct crypto_akcipher *tfm)
  605. {
  606. struct qat_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
  607. struct device *dev = &GET_DEV(ctx->inst->accel_dev);
  608. if (ctx->n)
  609. dma_free_coherent(dev, ctx->key_sz, ctx->n, ctx->dma_n);
  610. if (ctx->e)
  611. dma_free_coherent(dev, ctx->key_sz, ctx->e, ctx->dma_e);
  612. if (ctx->d) {
  613. memset(ctx->d, '\0', ctx->key_sz);
  614. dma_free_coherent(dev, ctx->key_sz, ctx->d, ctx->dma_d);
  615. }
  616. qat_crypto_put_instance(ctx->inst);
  617. ctx->n = NULL;
  618. ctx->d = NULL;
  619. ctx->d = NULL;
  620. }
  621. static struct akcipher_alg rsa = {
  622. .encrypt = qat_rsa_enc,
  623. .decrypt = qat_rsa_dec,
  624. .sign = qat_rsa_dec,
  625. .verify = qat_rsa_enc,
  626. .set_pub_key = qat_rsa_setpubkey,
  627. .set_priv_key = qat_rsa_setprivkey,
  628. .max_size = qat_rsa_max_size,
  629. .init = qat_rsa_init_tfm,
  630. .exit = qat_rsa_exit_tfm,
  631. .reqsize = sizeof(struct qat_rsa_request) + 64,
  632. .base = {
  633. .cra_name = "rsa",
  634. .cra_driver_name = "qat-rsa",
  635. .cra_priority = 1000,
  636. .cra_module = THIS_MODULE,
  637. .cra_ctxsize = sizeof(struct qat_rsa_ctx),
  638. },
  639. };
  640. int qat_asym_algs_register(void)
  641. {
  642. int ret = 0;
  643. mutex_lock(&algs_lock);
  644. if (++active_devs == 1) {
  645. rsa.base.cra_flags = 0;
  646. ret = crypto_register_akcipher(&rsa);
  647. }
  648. mutex_unlock(&algs_lock);
  649. return ret;
  650. }
  651. void qat_asym_algs_unregister(void)
  652. {
  653. mutex_lock(&algs_lock);
  654. if (--active_devs == 0)
  655. crypto_unregister_akcipher(&rsa);
  656. mutex_unlock(&algs_lock);
  657. }