virtio_crypto_akcipher_algs.c 16 KB

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