virtio_crypto_akcipher_algs.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Asymmetric algorithms supported by virtio crypto device
  3. *
  4. * Authors: zhenwei pi <pizhenwei@bytedance.com>
  5. * lei he <helei.sig11@bytedance.com>
  6. *
  7. * Copyright 2022 Bytedance CO., LTD.
  8. */
  9. #include <crypto/engine.h>
  10. #include <crypto/internal/akcipher.h>
  11. #include <crypto/internal/rsa.h>
  12. #include <crypto/scatterwalk.h>
  13. #include <linux/err.h>
  14. #include <linux/kernel.h>
  15. #include <linux/mpi.h>
  16. #include <linux/scatterlist.h>
  17. #include <linux/slab.h>
  18. #include <linux/string.h>
  19. #include <uapi/linux/virtio_crypto.h>
  20. #include "virtio_crypto_common.h"
  21. struct virtio_crypto_rsa_ctx {
  22. MPI n;
  23. };
  24. struct virtio_crypto_akcipher_ctx {
  25. struct virtio_crypto *vcrypto;
  26. struct crypto_akcipher *tfm;
  27. bool session_valid;
  28. __u64 session_id;
  29. union {
  30. struct virtio_crypto_rsa_ctx rsa_ctx;
  31. };
  32. };
  33. struct virtio_crypto_akcipher_request {
  34. struct virtio_crypto_request base;
  35. struct virtio_crypto_akcipher_ctx *akcipher_ctx;
  36. struct akcipher_request *akcipher_req;
  37. void *src_buf;
  38. void *dst_buf;
  39. uint32_t opcode;
  40. };
  41. struct virtio_crypto_akcipher_algo {
  42. uint32_t algonum;
  43. uint32_t service;
  44. unsigned int active_devs;
  45. struct akcipher_engine_alg algo;
  46. };
  47. static DEFINE_MUTEX(algs_lock);
  48. static void virtio_crypto_akcipher_finalize_req(
  49. struct virtio_crypto_akcipher_request *vc_akcipher_req,
  50. struct akcipher_request *req, int err)
  51. {
  52. kfree(vc_akcipher_req->src_buf);
  53. kfree(vc_akcipher_req->dst_buf);
  54. vc_akcipher_req->src_buf = NULL;
  55. vc_akcipher_req->dst_buf = NULL;
  56. virtcrypto_clear_request(&vc_akcipher_req->base);
  57. crypto_finalize_akcipher_request(vc_akcipher_req->base.dataq->engine, req, err);
  58. }
  59. static void virtio_crypto_dataq_akcipher_callback(struct virtio_crypto_request *vc_req, int len)
  60. {
  61. struct virtio_crypto_akcipher_request *vc_akcipher_req =
  62. container_of(vc_req, struct virtio_crypto_akcipher_request, base);
  63. struct akcipher_request *akcipher_req;
  64. int error;
  65. switch (vc_req->status) {
  66. case VIRTIO_CRYPTO_OK:
  67. error = 0;
  68. break;
  69. case VIRTIO_CRYPTO_INVSESS:
  70. case VIRTIO_CRYPTO_ERR:
  71. error = -EINVAL;
  72. break;
  73. case VIRTIO_CRYPTO_BADMSG:
  74. error = -EBADMSG;
  75. break;
  76. case VIRTIO_CRYPTO_KEY_REJECTED:
  77. error = -EKEYREJECTED;
  78. break;
  79. default:
  80. error = -EIO;
  81. break;
  82. }
  83. akcipher_req = vc_akcipher_req->akcipher_req;
  84. if (vc_akcipher_req->opcode != VIRTIO_CRYPTO_AKCIPHER_VERIFY) {
  85. /* actuall length maybe less than dst buffer */
  86. akcipher_req->dst_len = len - sizeof(vc_req->status);
  87. sg_copy_from_buffer(akcipher_req->dst, sg_nents(akcipher_req->dst),
  88. vc_akcipher_req->dst_buf, akcipher_req->dst_len);
  89. }
  90. virtio_crypto_akcipher_finalize_req(vc_akcipher_req, akcipher_req, error);
  91. }
  92. static int virtio_crypto_alg_akcipher_init_session(struct virtio_crypto_akcipher_ctx *ctx,
  93. struct virtio_crypto_ctrl_header *header,
  94. struct virtio_crypto_akcipher_session_para *para,
  95. const uint8_t *key, unsigned int keylen)
  96. {
  97. struct scatterlist outhdr_sg, key_sg, inhdr_sg, *sgs[3];
  98. struct virtio_crypto *vcrypto = ctx->vcrypto;
  99. uint8_t *pkey;
  100. int err;
  101. unsigned int num_out = 0, num_in = 0;
  102. struct virtio_crypto_op_ctrl_req *ctrl;
  103. struct virtio_crypto_session_input *input;
  104. struct virtio_crypto_ctrl_request *vc_ctrl_req;
  105. pkey = kmemdup(key, keylen, GFP_KERNEL);
  106. if (!pkey)
  107. return -ENOMEM;
  108. vc_ctrl_req = kzalloc(sizeof(*vc_ctrl_req), GFP_KERNEL);
  109. if (!vc_ctrl_req) {
  110. err = -ENOMEM;
  111. goto out;
  112. }
  113. ctrl = &vc_ctrl_req->ctrl;
  114. memcpy(&ctrl->header, header, sizeof(ctrl->header));
  115. memcpy(&ctrl->u.akcipher_create_session.para, para, sizeof(*para));
  116. input = &vc_ctrl_req->input;
  117. input->status = cpu_to_le32(VIRTIO_CRYPTO_ERR);
  118. sg_init_one(&outhdr_sg, ctrl, sizeof(*ctrl));
  119. sgs[num_out++] = &outhdr_sg;
  120. sg_init_one(&key_sg, pkey, keylen);
  121. sgs[num_out++] = &key_sg;
  122. sg_init_one(&inhdr_sg, input, sizeof(*input));
  123. sgs[num_out + num_in++] = &inhdr_sg;
  124. err = virtio_crypto_ctrl_vq_request(vcrypto, sgs, num_out, num_in, vc_ctrl_req);
  125. if (err < 0)
  126. goto out;
  127. if (le32_to_cpu(input->status) != VIRTIO_CRYPTO_OK) {
  128. pr_err("virtio_crypto: Create session failed status: %u\n",
  129. le32_to_cpu(input->status));
  130. err = -EINVAL;
  131. goto out;
  132. }
  133. ctx->session_id = le64_to_cpu(input->session_id);
  134. ctx->session_valid = true;
  135. err = 0;
  136. out:
  137. kfree(vc_ctrl_req);
  138. kfree_sensitive(pkey);
  139. return err;
  140. }
  141. static int virtio_crypto_alg_akcipher_close_session(struct virtio_crypto_akcipher_ctx *ctx)
  142. {
  143. struct scatterlist outhdr_sg, inhdr_sg, *sgs[2];
  144. struct virtio_crypto_destroy_session_req *destroy_session;
  145. struct virtio_crypto *vcrypto = ctx->vcrypto;
  146. unsigned int num_out = 0, num_in = 0;
  147. int err;
  148. struct virtio_crypto_op_ctrl_req *ctrl;
  149. struct virtio_crypto_inhdr *ctrl_status;
  150. struct virtio_crypto_ctrl_request *vc_ctrl_req;
  151. if (!ctx->session_valid)
  152. return 0;
  153. vc_ctrl_req = kzalloc(sizeof(*vc_ctrl_req), GFP_KERNEL);
  154. if (!vc_ctrl_req)
  155. return -ENOMEM;
  156. ctrl_status = &vc_ctrl_req->ctrl_status;
  157. ctrl_status->status = VIRTIO_CRYPTO_ERR;
  158. ctrl = &vc_ctrl_req->ctrl;
  159. ctrl->header.opcode = cpu_to_le32(VIRTIO_CRYPTO_AKCIPHER_DESTROY_SESSION);
  160. ctrl->header.queue_id = 0;
  161. destroy_session = &ctrl->u.destroy_session;
  162. destroy_session->session_id = cpu_to_le64(ctx->session_id);
  163. sg_init_one(&outhdr_sg, ctrl, sizeof(*ctrl));
  164. sgs[num_out++] = &outhdr_sg;
  165. sg_init_one(&inhdr_sg, &ctrl_status->status, sizeof(ctrl_status->status));
  166. sgs[num_out + num_in++] = &inhdr_sg;
  167. err = virtio_crypto_ctrl_vq_request(vcrypto, sgs, num_out, num_in, vc_ctrl_req);
  168. if (err < 0)
  169. goto out;
  170. if (ctrl_status->status != VIRTIO_CRYPTO_OK) {
  171. pr_err("virtio_crypto: Close session failed status: %u, session_id: 0x%llx\n",
  172. ctrl_status->status, destroy_session->session_id);
  173. err = -EINVAL;
  174. goto out;
  175. }
  176. err = 0;
  177. ctx->session_valid = false;
  178. out:
  179. kfree(vc_ctrl_req);
  180. return err;
  181. }
  182. static int __virtio_crypto_akcipher_do_req(struct virtio_crypto_akcipher_request *vc_akcipher_req,
  183. struct akcipher_request *req, struct data_queue *data_vq)
  184. {
  185. struct virtio_crypto_akcipher_ctx *ctx = vc_akcipher_req->akcipher_ctx;
  186. struct virtio_crypto_request *vc_req = &vc_akcipher_req->base;
  187. struct virtio_crypto *vcrypto = ctx->vcrypto;
  188. struct virtio_crypto_op_data_req *req_data = vc_req->req_data;
  189. struct scatterlist *sgs[4], outhdr_sg, inhdr_sg, srcdata_sg, dstdata_sg;
  190. void *src_buf, *dst_buf = NULL;
  191. unsigned int num_out = 0, num_in = 0;
  192. int node = dev_to_node(&vcrypto->vdev->dev);
  193. unsigned long flags;
  194. int ret;
  195. bool verify = vc_akcipher_req->opcode == VIRTIO_CRYPTO_AKCIPHER_VERIFY;
  196. unsigned int src_len = verify ? req->src_len + req->dst_len : req->src_len;
  197. /* out header */
  198. sg_init_one(&outhdr_sg, req_data, sizeof(*req_data));
  199. sgs[num_out++] = &outhdr_sg;
  200. /* src data */
  201. src_buf = kcalloc_node(src_len, 1, GFP_KERNEL, node);
  202. if (!src_buf)
  203. return -ENOMEM;
  204. if (verify) {
  205. /* for verify operation, both src and dst data work as OUT direction */
  206. sg_copy_to_buffer(req->src, sg_nents(req->src), src_buf, src_len);
  207. sg_init_one(&srcdata_sg, src_buf, src_len);
  208. sgs[num_out++] = &srcdata_sg;
  209. } else {
  210. sg_copy_to_buffer(req->src, sg_nents(req->src), src_buf, src_len);
  211. sg_init_one(&srcdata_sg, src_buf, src_len);
  212. sgs[num_out++] = &srcdata_sg;
  213. /* dst data */
  214. dst_buf = kcalloc_node(req->dst_len, 1, GFP_KERNEL, node);
  215. if (!dst_buf)
  216. goto free_src;
  217. sg_init_one(&dstdata_sg, dst_buf, req->dst_len);
  218. sgs[num_out + num_in++] = &dstdata_sg;
  219. }
  220. vc_akcipher_req->src_buf = src_buf;
  221. vc_akcipher_req->dst_buf = dst_buf;
  222. /* in header */
  223. sg_init_one(&inhdr_sg, &vc_req->status, sizeof(vc_req->status));
  224. sgs[num_out + num_in++] = &inhdr_sg;
  225. spin_lock_irqsave(&data_vq->lock, flags);
  226. ret = virtqueue_add_sgs(data_vq->vq, sgs, num_out, num_in, vc_req, GFP_ATOMIC);
  227. virtqueue_kick(data_vq->vq);
  228. spin_unlock_irqrestore(&data_vq->lock, flags);
  229. if (ret)
  230. goto err;
  231. return 0;
  232. err:
  233. kfree(dst_buf);
  234. free_src:
  235. kfree(src_buf);
  236. return -ENOMEM;
  237. }
  238. static int virtio_crypto_rsa_do_req(struct crypto_engine *engine, void *vreq)
  239. {
  240. struct akcipher_request *req = container_of(vreq, struct akcipher_request, base);
  241. struct virtio_crypto_akcipher_request *vc_akcipher_req = akcipher_request_ctx(req);
  242. struct virtio_crypto_request *vc_req = &vc_akcipher_req->base;
  243. struct virtio_crypto_akcipher_ctx *ctx = vc_akcipher_req->akcipher_ctx;
  244. struct virtio_crypto *vcrypto = ctx->vcrypto;
  245. struct data_queue *data_vq = vc_req->dataq;
  246. struct virtio_crypto_op_header *header;
  247. struct virtio_crypto_akcipher_data_req *akcipher_req;
  248. int ret;
  249. vc_req->sgs = NULL;
  250. vc_req->req_data = kzalloc_node(sizeof(*vc_req->req_data),
  251. GFP_KERNEL, dev_to_node(&vcrypto->vdev->dev));
  252. if (!vc_req->req_data)
  253. return -ENOMEM;
  254. /* build request header */
  255. header = &vc_req->req_data->header;
  256. header->opcode = cpu_to_le32(vc_akcipher_req->opcode);
  257. header->algo = cpu_to_le32(VIRTIO_CRYPTO_AKCIPHER_RSA);
  258. header->session_id = cpu_to_le64(ctx->session_id);
  259. /* build request akcipher data */
  260. akcipher_req = &vc_req->req_data->u.akcipher_req;
  261. akcipher_req->para.src_data_len = cpu_to_le32(req->src_len);
  262. akcipher_req->para.dst_data_len = cpu_to_le32(req->dst_len);
  263. ret = __virtio_crypto_akcipher_do_req(vc_akcipher_req, req, data_vq);
  264. if (ret < 0) {
  265. kfree_sensitive(vc_req->req_data);
  266. vc_req->req_data = NULL;
  267. return ret;
  268. }
  269. return 0;
  270. }
  271. static int virtio_crypto_rsa_req(struct akcipher_request *req, uint32_t opcode)
  272. {
  273. struct crypto_akcipher *atfm = crypto_akcipher_reqtfm(req);
  274. struct virtio_crypto_akcipher_ctx *ctx = akcipher_tfm_ctx(atfm);
  275. struct virtio_crypto_akcipher_request *vc_akcipher_req = akcipher_request_ctx(req);
  276. struct virtio_crypto_request *vc_req = &vc_akcipher_req->base;
  277. struct virtio_crypto *vcrypto = ctx->vcrypto;
  278. /* Use the first data virtqueue as default */
  279. struct data_queue *data_vq = &vcrypto->data_vq[0];
  280. vc_req->dataq = data_vq;
  281. vc_req->alg_cb = virtio_crypto_dataq_akcipher_callback;
  282. vc_akcipher_req->akcipher_ctx = ctx;
  283. vc_akcipher_req->akcipher_req = req;
  284. vc_akcipher_req->opcode = opcode;
  285. return crypto_transfer_akcipher_request_to_engine(data_vq->engine, req);
  286. }
  287. static int virtio_crypto_rsa_encrypt(struct akcipher_request *req)
  288. {
  289. return virtio_crypto_rsa_req(req, VIRTIO_CRYPTO_AKCIPHER_ENCRYPT);
  290. }
  291. static int virtio_crypto_rsa_decrypt(struct akcipher_request *req)
  292. {
  293. return virtio_crypto_rsa_req(req, VIRTIO_CRYPTO_AKCIPHER_DECRYPT);
  294. }
  295. static int virtio_crypto_rsa_sign(struct akcipher_request *req)
  296. {
  297. return virtio_crypto_rsa_req(req, VIRTIO_CRYPTO_AKCIPHER_SIGN);
  298. }
  299. static int virtio_crypto_rsa_verify(struct akcipher_request *req)
  300. {
  301. return virtio_crypto_rsa_req(req, VIRTIO_CRYPTO_AKCIPHER_VERIFY);
  302. }
  303. static int virtio_crypto_rsa_set_key(struct crypto_akcipher *tfm,
  304. const void *key,
  305. unsigned int keylen,
  306. bool private,
  307. int padding_algo,
  308. int hash_algo)
  309. {
  310. struct virtio_crypto_akcipher_ctx *ctx = akcipher_tfm_ctx(tfm);
  311. struct virtio_crypto_rsa_ctx *rsa_ctx = &ctx->rsa_ctx;
  312. struct virtio_crypto *vcrypto;
  313. struct virtio_crypto_ctrl_header header;
  314. struct virtio_crypto_akcipher_session_para para;
  315. struct rsa_key rsa_key = {0};
  316. int node = virtio_crypto_get_current_node();
  317. uint32_t keytype;
  318. int ret;
  319. /* mpi_free will test n, just free it. */
  320. mpi_free(rsa_ctx->n);
  321. rsa_ctx->n = NULL;
  322. if (private) {
  323. keytype = VIRTIO_CRYPTO_AKCIPHER_KEY_TYPE_PRIVATE;
  324. ret = rsa_parse_priv_key(&rsa_key, key, keylen);
  325. } else {
  326. keytype = VIRTIO_CRYPTO_AKCIPHER_KEY_TYPE_PUBLIC;
  327. ret = rsa_parse_pub_key(&rsa_key, key, keylen);
  328. }
  329. if (ret)
  330. return ret;
  331. rsa_ctx->n = mpi_read_raw_data(rsa_key.n, rsa_key.n_sz);
  332. if (!rsa_ctx->n)
  333. return -ENOMEM;
  334. if (!ctx->vcrypto) {
  335. vcrypto = virtcrypto_get_dev_node(node, VIRTIO_CRYPTO_SERVICE_AKCIPHER,
  336. VIRTIO_CRYPTO_AKCIPHER_RSA);
  337. if (!vcrypto) {
  338. pr_err("virtio_crypto: Could not find a virtio device in the system or unsupported algo\n");
  339. return -ENODEV;
  340. }
  341. ctx->vcrypto = vcrypto;
  342. } else {
  343. virtio_crypto_alg_akcipher_close_session(ctx);
  344. }
  345. /* set ctrl header */
  346. header.opcode = cpu_to_le32(VIRTIO_CRYPTO_AKCIPHER_CREATE_SESSION);
  347. header.algo = cpu_to_le32(VIRTIO_CRYPTO_AKCIPHER_RSA);
  348. header.queue_id = 0;
  349. /* set RSA para */
  350. para.algo = cpu_to_le32(VIRTIO_CRYPTO_AKCIPHER_RSA);
  351. para.keytype = cpu_to_le32(keytype);
  352. para.keylen = cpu_to_le32(keylen);
  353. para.u.rsa.padding_algo = cpu_to_le32(padding_algo);
  354. para.u.rsa.hash_algo = cpu_to_le32(hash_algo);
  355. return virtio_crypto_alg_akcipher_init_session(ctx, &header, &para, key, keylen);
  356. }
  357. static int virtio_crypto_rsa_raw_set_priv_key(struct crypto_akcipher *tfm,
  358. const void *key,
  359. unsigned int keylen)
  360. {
  361. return virtio_crypto_rsa_set_key(tfm, key, keylen, 1,
  362. VIRTIO_CRYPTO_RSA_RAW_PADDING,
  363. VIRTIO_CRYPTO_RSA_NO_HASH);
  364. }
  365. static int virtio_crypto_p1pad_rsa_sha1_set_priv_key(struct crypto_akcipher *tfm,
  366. const void *key,
  367. unsigned int keylen)
  368. {
  369. return virtio_crypto_rsa_set_key(tfm, key, keylen, 1,
  370. VIRTIO_CRYPTO_RSA_PKCS1_PADDING,
  371. VIRTIO_CRYPTO_RSA_SHA1);
  372. }
  373. static int virtio_crypto_rsa_raw_set_pub_key(struct crypto_akcipher *tfm,
  374. const void *key,
  375. unsigned int keylen)
  376. {
  377. return virtio_crypto_rsa_set_key(tfm, key, keylen, 0,
  378. VIRTIO_CRYPTO_RSA_RAW_PADDING,
  379. VIRTIO_CRYPTO_RSA_NO_HASH);
  380. }
  381. static int virtio_crypto_p1pad_rsa_sha1_set_pub_key(struct crypto_akcipher *tfm,
  382. const void *key,
  383. unsigned int keylen)
  384. {
  385. return virtio_crypto_rsa_set_key(tfm, key, keylen, 0,
  386. VIRTIO_CRYPTO_RSA_PKCS1_PADDING,
  387. VIRTIO_CRYPTO_RSA_SHA1);
  388. }
  389. static unsigned int virtio_crypto_rsa_max_size(struct crypto_akcipher *tfm)
  390. {
  391. struct virtio_crypto_akcipher_ctx *ctx = akcipher_tfm_ctx(tfm);
  392. struct virtio_crypto_rsa_ctx *rsa_ctx = &ctx->rsa_ctx;
  393. return mpi_get_size(rsa_ctx->n);
  394. }
  395. static int virtio_crypto_rsa_init_tfm(struct crypto_akcipher *tfm)
  396. {
  397. struct virtio_crypto_akcipher_ctx *ctx = akcipher_tfm_ctx(tfm);
  398. ctx->tfm = tfm;
  399. akcipher_set_reqsize(tfm,
  400. sizeof(struct virtio_crypto_akcipher_request));
  401. return 0;
  402. }
  403. static void virtio_crypto_rsa_exit_tfm(struct crypto_akcipher *tfm)
  404. {
  405. struct virtio_crypto_akcipher_ctx *ctx = akcipher_tfm_ctx(tfm);
  406. struct virtio_crypto_rsa_ctx *rsa_ctx = &ctx->rsa_ctx;
  407. virtio_crypto_alg_akcipher_close_session(ctx);
  408. virtcrypto_dev_put(ctx->vcrypto);
  409. mpi_free(rsa_ctx->n);
  410. rsa_ctx->n = NULL;
  411. }
  412. static struct virtio_crypto_akcipher_algo virtio_crypto_akcipher_algs[] = {
  413. {
  414. .algonum = VIRTIO_CRYPTO_AKCIPHER_RSA,
  415. .service = VIRTIO_CRYPTO_SERVICE_AKCIPHER,
  416. .algo.base = {
  417. .encrypt = virtio_crypto_rsa_encrypt,
  418. .decrypt = virtio_crypto_rsa_decrypt,
  419. .set_pub_key = virtio_crypto_rsa_raw_set_pub_key,
  420. .set_priv_key = virtio_crypto_rsa_raw_set_priv_key,
  421. .max_size = virtio_crypto_rsa_max_size,
  422. .init = virtio_crypto_rsa_init_tfm,
  423. .exit = virtio_crypto_rsa_exit_tfm,
  424. .base = {
  425. .cra_name = "rsa",
  426. .cra_driver_name = "virtio-crypto-rsa",
  427. .cra_priority = 150,
  428. .cra_module = THIS_MODULE,
  429. .cra_ctxsize = sizeof(struct virtio_crypto_akcipher_ctx),
  430. },
  431. },
  432. .algo.op = {
  433. .do_one_request = virtio_crypto_rsa_do_req,
  434. },
  435. },
  436. {
  437. .algonum = VIRTIO_CRYPTO_AKCIPHER_RSA,
  438. .service = VIRTIO_CRYPTO_SERVICE_AKCIPHER,
  439. .algo.base = {
  440. .encrypt = virtio_crypto_rsa_encrypt,
  441. .decrypt = virtio_crypto_rsa_decrypt,
  442. .sign = virtio_crypto_rsa_sign,
  443. .verify = virtio_crypto_rsa_verify,
  444. .set_pub_key = virtio_crypto_p1pad_rsa_sha1_set_pub_key,
  445. .set_priv_key = virtio_crypto_p1pad_rsa_sha1_set_priv_key,
  446. .max_size = virtio_crypto_rsa_max_size,
  447. .init = virtio_crypto_rsa_init_tfm,
  448. .exit = virtio_crypto_rsa_exit_tfm,
  449. .base = {
  450. .cra_name = "pkcs1pad(rsa,sha1)",
  451. .cra_driver_name = "virtio-pkcs1-rsa-with-sha1",
  452. .cra_priority = 150,
  453. .cra_module = THIS_MODULE,
  454. .cra_ctxsize = sizeof(struct virtio_crypto_akcipher_ctx),
  455. },
  456. },
  457. .algo.op = {
  458. .do_one_request = virtio_crypto_rsa_do_req,
  459. },
  460. },
  461. };
  462. int virtio_crypto_akcipher_algs_register(struct virtio_crypto *vcrypto)
  463. {
  464. int ret = 0;
  465. int i = 0;
  466. mutex_lock(&algs_lock);
  467. for (i = 0; i < ARRAY_SIZE(virtio_crypto_akcipher_algs); i++) {
  468. uint32_t service = virtio_crypto_akcipher_algs[i].service;
  469. uint32_t algonum = virtio_crypto_akcipher_algs[i].algonum;
  470. if (!virtcrypto_algo_is_supported(vcrypto, service, algonum))
  471. continue;
  472. if (virtio_crypto_akcipher_algs[i].active_devs == 0) {
  473. ret = crypto_engine_register_akcipher(&virtio_crypto_akcipher_algs[i].algo);
  474. if (ret)
  475. goto unlock;
  476. }
  477. virtio_crypto_akcipher_algs[i].active_devs++;
  478. dev_info(&vcrypto->vdev->dev, "Registered akcipher algo %s\n",
  479. virtio_crypto_akcipher_algs[i].algo.base.base.cra_name);
  480. }
  481. unlock:
  482. mutex_unlock(&algs_lock);
  483. return ret;
  484. }
  485. void virtio_crypto_akcipher_algs_unregister(struct virtio_crypto *vcrypto)
  486. {
  487. int i = 0;
  488. mutex_lock(&algs_lock);
  489. for (i = 0; i < ARRAY_SIZE(virtio_crypto_akcipher_algs); i++) {
  490. uint32_t service = virtio_crypto_akcipher_algs[i].service;
  491. uint32_t algonum = virtio_crypto_akcipher_algs[i].algonum;
  492. if (virtio_crypto_akcipher_algs[i].active_devs == 0 ||
  493. !virtcrypto_algo_is_supported(vcrypto, service, algonum))
  494. continue;
  495. if (virtio_crypto_akcipher_algs[i].active_devs == 1)
  496. crypto_engine_unregister_akcipher(&virtio_crypto_akcipher_algs[i].algo);
  497. virtio_crypto_akcipher_algs[i].active_devs--;
  498. }
  499. mutex_unlock(&algs_lock);
  500. }