virtio_crypto_skcipher_algs.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Algorithms supported by virtio crypto device
  3. *
  4. * Authors: Gonglei <arei.gonglei@huawei.com>
  5. *
  6. * Copyright 2016 HUAWEI TECHNOLOGIES CO., LTD.
  7. */
  8. #include <crypto/engine.h>
  9. #include <crypto/internal/skcipher.h>
  10. #include <crypto/scatterwalk.h>
  11. #include <linux/err.h>
  12. #include <linux/scatterlist.h>
  13. #include <uapi/linux/virtio_crypto.h>
  14. #include "virtio_crypto_common.h"
  15. struct virtio_crypto_skcipher_ctx {
  16. struct virtio_crypto *vcrypto;
  17. struct virtio_crypto_sym_session_info enc_sess_info;
  18. struct virtio_crypto_sym_session_info dec_sess_info;
  19. };
  20. struct virtio_crypto_sym_request {
  21. struct virtio_crypto_request base;
  22. /* Cipher or aead */
  23. uint32_t type;
  24. uint8_t *iv;
  25. /* Encryption? */
  26. bool encrypt;
  27. };
  28. struct virtio_crypto_algo {
  29. uint32_t algonum;
  30. uint32_t service;
  31. unsigned int active_devs;
  32. struct skcipher_engine_alg algo;
  33. };
  34. /*
  35. * The algs_lock protects the below global virtio_crypto_active_devs
  36. * and crypto algorithms registion.
  37. */
  38. static DEFINE_MUTEX(algs_lock);
  39. static void virtio_crypto_skcipher_finalize_req(
  40. struct virtio_crypto_sym_request *vc_sym_req,
  41. struct skcipher_request *req,
  42. int err);
  43. static void virtio_crypto_dataq_sym_callback
  44. (struct virtio_crypto_request *vc_req, int len)
  45. {
  46. struct virtio_crypto_sym_request *vc_sym_req =
  47. container_of(vc_req, struct virtio_crypto_sym_request, base);
  48. struct skcipher_request *ablk_req =
  49. container_of((void *)vc_sym_req, struct skcipher_request,
  50. __ctx);
  51. int error;
  52. /* Finish the encrypt or decrypt process */
  53. if (vc_sym_req->type == VIRTIO_CRYPTO_SYM_OP_CIPHER) {
  54. switch (vc_req->status) {
  55. case VIRTIO_CRYPTO_OK:
  56. error = 0;
  57. break;
  58. case VIRTIO_CRYPTO_INVSESS:
  59. case VIRTIO_CRYPTO_ERR:
  60. error = -EINVAL;
  61. break;
  62. case VIRTIO_CRYPTO_BADMSG:
  63. error = -EBADMSG;
  64. break;
  65. default:
  66. error = -EIO;
  67. break;
  68. }
  69. virtio_crypto_skcipher_finalize_req(vc_sym_req,
  70. ablk_req, error);
  71. }
  72. }
  73. static u64 virtio_crypto_alg_sg_nents_length(struct scatterlist *sg)
  74. {
  75. u64 total = 0;
  76. for (total = 0; sg; sg = sg_next(sg))
  77. total += sg->length;
  78. return total;
  79. }
  80. static int
  81. virtio_crypto_alg_validate_key(int key_len, uint32_t *alg)
  82. {
  83. switch (key_len) {
  84. case AES_KEYSIZE_128:
  85. case AES_KEYSIZE_192:
  86. case AES_KEYSIZE_256:
  87. *alg = VIRTIO_CRYPTO_CIPHER_AES_CBC;
  88. break;
  89. default:
  90. return -EINVAL;
  91. }
  92. return 0;
  93. }
  94. static int virtio_crypto_alg_skcipher_init_session(
  95. struct virtio_crypto_skcipher_ctx *ctx,
  96. uint32_t alg, const uint8_t *key,
  97. unsigned int keylen,
  98. int encrypt)
  99. {
  100. struct scatterlist outhdr, key_sg, inhdr, *sgs[3];
  101. struct virtio_crypto *vcrypto = ctx->vcrypto;
  102. int op = encrypt ? VIRTIO_CRYPTO_OP_ENCRYPT : VIRTIO_CRYPTO_OP_DECRYPT;
  103. int err;
  104. unsigned int num_out = 0, num_in = 0;
  105. struct virtio_crypto_op_ctrl_req *ctrl;
  106. struct virtio_crypto_session_input *input;
  107. struct virtio_crypto_sym_create_session_req *sym_create_session;
  108. struct virtio_crypto_ctrl_request *vc_ctrl_req;
  109. /*
  110. * Avoid to do DMA from the stack, switch to using
  111. * dynamically-allocated for the key
  112. */
  113. uint8_t *cipher_key = kmemdup(key, keylen, GFP_ATOMIC);
  114. if (!cipher_key)
  115. return -ENOMEM;
  116. vc_ctrl_req = kzalloc_obj(*vc_ctrl_req);
  117. if (!vc_ctrl_req) {
  118. err = -ENOMEM;
  119. goto out;
  120. }
  121. /* Pad ctrl header */
  122. ctrl = &vc_ctrl_req->ctrl;
  123. ctrl->header.opcode = cpu_to_le32(VIRTIO_CRYPTO_CIPHER_CREATE_SESSION);
  124. ctrl->header.algo = cpu_to_le32(alg);
  125. /* Set the default dataqueue id to 0 */
  126. ctrl->header.queue_id = 0;
  127. input = &vc_ctrl_req->input;
  128. input->status = cpu_to_le32(VIRTIO_CRYPTO_ERR);
  129. /* Pad cipher's parameters */
  130. sym_create_session = &ctrl->u.sym_create_session;
  131. sym_create_session->op_type = cpu_to_le32(VIRTIO_CRYPTO_SYM_OP_CIPHER);
  132. sym_create_session->u.cipher.para.algo = ctrl->header.algo;
  133. sym_create_session->u.cipher.para.keylen = cpu_to_le32(keylen);
  134. sym_create_session->u.cipher.para.op = cpu_to_le32(op);
  135. sg_init_one(&outhdr, ctrl, sizeof(*ctrl));
  136. sgs[num_out++] = &outhdr;
  137. /* Set key */
  138. sg_init_one(&key_sg, cipher_key, keylen);
  139. sgs[num_out++] = &key_sg;
  140. /* Return status and session id back */
  141. sg_init_one(&inhdr, input, sizeof(*input));
  142. sgs[num_out + num_in++] = &inhdr;
  143. err = virtio_crypto_ctrl_vq_request(vcrypto, sgs, num_out, num_in, vc_ctrl_req);
  144. if (err < 0)
  145. goto out;
  146. if (le32_to_cpu(input->status) != VIRTIO_CRYPTO_OK) {
  147. pr_err("virtio_crypto: Create session failed status: %u\n",
  148. le32_to_cpu(input->status));
  149. err = -EINVAL;
  150. goto out;
  151. }
  152. if (encrypt)
  153. ctx->enc_sess_info.session_id = le64_to_cpu(input->session_id);
  154. else
  155. ctx->dec_sess_info.session_id = le64_to_cpu(input->session_id);
  156. err = 0;
  157. out:
  158. kfree(vc_ctrl_req);
  159. kfree_sensitive(cipher_key);
  160. return err;
  161. }
  162. static int virtio_crypto_alg_skcipher_close_session(
  163. struct virtio_crypto_skcipher_ctx *ctx,
  164. int encrypt)
  165. {
  166. struct scatterlist outhdr, status_sg, *sgs[2];
  167. struct virtio_crypto_destroy_session_req *destroy_session;
  168. struct virtio_crypto *vcrypto = ctx->vcrypto;
  169. int err;
  170. unsigned int num_out = 0, num_in = 0;
  171. struct virtio_crypto_op_ctrl_req *ctrl;
  172. struct virtio_crypto_inhdr *ctrl_status;
  173. struct virtio_crypto_ctrl_request *vc_ctrl_req;
  174. vc_ctrl_req = kzalloc_obj(*vc_ctrl_req);
  175. if (!vc_ctrl_req)
  176. return -ENOMEM;
  177. ctrl_status = &vc_ctrl_req->ctrl_status;
  178. ctrl_status->status = VIRTIO_CRYPTO_ERR;
  179. /* Pad ctrl header */
  180. ctrl = &vc_ctrl_req->ctrl;
  181. ctrl->header.opcode = cpu_to_le32(VIRTIO_CRYPTO_CIPHER_DESTROY_SESSION);
  182. /* Set the default virtqueue id to 0 */
  183. ctrl->header.queue_id = 0;
  184. destroy_session = &ctrl->u.destroy_session;
  185. if (encrypt)
  186. destroy_session->session_id = cpu_to_le64(ctx->enc_sess_info.session_id);
  187. else
  188. destroy_session->session_id = cpu_to_le64(ctx->dec_sess_info.session_id);
  189. sg_init_one(&outhdr, ctrl, sizeof(*ctrl));
  190. sgs[num_out++] = &outhdr;
  191. /* Return status and session id back */
  192. sg_init_one(&status_sg, &ctrl_status->status, sizeof(ctrl_status->status));
  193. sgs[num_out + num_in++] = &status_sg;
  194. err = virtio_crypto_ctrl_vq_request(vcrypto, sgs, num_out, num_in, vc_ctrl_req);
  195. if (err < 0)
  196. goto out;
  197. if (ctrl_status->status != VIRTIO_CRYPTO_OK) {
  198. pr_err("virtio_crypto: Close session failed status: %u, session_id: 0x%llx\n",
  199. ctrl_status->status, destroy_session->session_id);
  200. err = -EINVAL;
  201. goto out;
  202. }
  203. err = 0;
  204. out:
  205. kfree(vc_ctrl_req);
  206. return err;
  207. }
  208. static int virtio_crypto_alg_skcipher_init_sessions(
  209. struct virtio_crypto_skcipher_ctx *ctx,
  210. const uint8_t *key, unsigned int keylen)
  211. {
  212. uint32_t alg;
  213. int ret;
  214. struct virtio_crypto *vcrypto = ctx->vcrypto;
  215. if (keylen > vcrypto->max_cipher_key_len) {
  216. pr_err("virtio_crypto: the key is too long\n");
  217. return -EINVAL;
  218. }
  219. if (virtio_crypto_alg_validate_key(keylen, &alg))
  220. return -EINVAL;
  221. /* Create encryption session */
  222. ret = virtio_crypto_alg_skcipher_init_session(ctx,
  223. alg, key, keylen, 1);
  224. if (ret)
  225. return ret;
  226. /* Create decryption session */
  227. ret = virtio_crypto_alg_skcipher_init_session(ctx,
  228. alg, key, keylen, 0);
  229. if (ret) {
  230. virtio_crypto_alg_skcipher_close_session(ctx, 1);
  231. return ret;
  232. }
  233. return 0;
  234. }
  235. /* Note: kernel crypto API realization */
  236. static int virtio_crypto_skcipher_setkey(struct crypto_skcipher *tfm,
  237. const uint8_t *key,
  238. unsigned int keylen)
  239. {
  240. struct virtio_crypto_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  241. uint32_t alg;
  242. int ret;
  243. ret = virtio_crypto_alg_validate_key(keylen, &alg);
  244. if (ret)
  245. return ret;
  246. if (!ctx->vcrypto) {
  247. /* New key */
  248. int node = virtio_crypto_get_current_node();
  249. struct virtio_crypto *vcrypto =
  250. virtcrypto_get_dev_node(node,
  251. VIRTIO_CRYPTO_SERVICE_CIPHER, alg);
  252. if (!vcrypto) {
  253. pr_err("virtio_crypto: Could not find a virtio device in the system or unsupported algo\n");
  254. return -ENODEV;
  255. }
  256. ctx->vcrypto = vcrypto;
  257. } else {
  258. /* Rekeying, we should close the created sessions previously */
  259. virtio_crypto_alg_skcipher_close_session(ctx, 1);
  260. virtio_crypto_alg_skcipher_close_session(ctx, 0);
  261. }
  262. ret = virtio_crypto_alg_skcipher_init_sessions(ctx, key, keylen);
  263. if (ret) {
  264. virtcrypto_dev_put(ctx->vcrypto);
  265. ctx->vcrypto = NULL;
  266. return ret;
  267. }
  268. return 0;
  269. }
  270. static int
  271. __virtio_crypto_skcipher_do_req(struct virtio_crypto_sym_request *vc_sym_req,
  272. struct skcipher_request *req,
  273. struct data_queue *data_vq)
  274. {
  275. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  276. struct virtio_crypto_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  277. struct virtio_crypto_request *vc_req = &vc_sym_req->base;
  278. unsigned int ivsize = crypto_skcipher_ivsize(tfm);
  279. struct virtio_crypto *vcrypto = ctx->vcrypto;
  280. struct virtio_crypto_op_data_req *req_data;
  281. int src_nents, dst_nents;
  282. int err;
  283. unsigned long flags;
  284. struct scatterlist outhdr, iv_sg, status_sg, **sgs;
  285. u64 dst_len;
  286. unsigned int num_out = 0, num_in = 0;
  287. int sg_total;
  288. uint8_t *iv;
  289. struct scatterlist *sg;
  290. src_nents = sg_nents_for_len(req->src, req->cryptlen);
  291. if (src_nents < 0) {
  292. pr_err("Invalid number of src SG.\n");
  293. return src_nents;
  294. }
  295. dst_nents = sg_nents(req->dst);
  296. pr_debug("virtio_crypto: Number of sgs (src_nents: %d, dst_nents: %d)\n",
  297. src_nents, dst_nents);
  298. /* Why 3? outhdr + iv + inhdr */
  299. sg_total = src_nents + dst_nents + 3;
  300. sgs = kcalloc_node(sg_total, sizeof(*sgs), GFP_KERNEL,
  301. dev_to_node(&vcrypto->vdev->dev));
  302. if (!sgs)
  303. return -ENOMEM;
  304. req_data = kzalloc_node(sizeof(*req_data), GFP_KERNEL,
  305. dev_to_node(&vcrypto->vdev->dev));
  306. if (!req_data) {
  307. kfree(sgs);
  308. return -ENOMEM;
  309. }
  310. vc_req->req_data = req_data;
  311. vc_sym_req->type = VIRTIO_CRYPTO_SYM_OP_CIPHER;
  312. /* Head of operation */
  313. if (vc_sym_req->encrypt) {
  314. req_data->header.session_id =
  315. cpu_to_le64(ctx->enc_sess_info.session_id);
  316. req_data->header.opcode =
  317. cpu_to_le32(VIRTIO_CRYPTO_CIPHER_ENCRYPT);
  318. } else {
  319. req_data->header.session_id =
  320. cpu_to_le64(ctx->dec_sess_info.session_id);
  321. req_data->header.opcode =
  322. cpu_to_le32(VIRTIO_CRYPTO_CIPHER_DECRYPT);
  323. }
  324. req_data->u.sym_req.op_type = cpu_to_le32(VIRTIO_CRYPTO_SYM_OP_CIPHER);
  325. req_data->u.sym_req.u.cipher.para.iv_len = cpu_to_le32(ivsize);
  326. req_data->u.sym_req.u.cipher.para.src_data_len =
  327. cpu_to_le32(req->cryptlen);
  328. dst_len = virtio_crypto_alg_sg_nents_length(req->dst);
  329. if (unlikely(dst_len > U32_MAX)) {
  330. pr_err("virtio_crypto: The dst_len is beyond U32_MAX\n");
  331. err = -EINVAL;
  332. goto free;
  333. }
  334. dst_len = min_t(unsigned int, req->cryptlen, dst_len);
  335. pr_debug("virtio_crypto: src_len: %u, dst_len: %llu\n",
  336. req->cryptlen, dst_len);
  337. if (unlikely(req->cryptlen + dst_len + ivsize +
  338. sizeof(vc_req->status) > vcrypto->max_size)) {
  339. pr_err("virtio_crypto: The length is too big\n");
  340. err = -EINVAL;
  341. goto free;
  342. }
  343. req_data->u.sym_req.u.cipher.para.dst_data_len =
  344. cpu_to_le32((uint32_t)dst_len);
  345. /* Outhdr */
  346. sg_init_one(&outhdr, req_data, sizeof(*req_data));
  347. sgs[num_out++] = &outhdr;
  348. /* IV */
  349. /*
  350. * Avoid to do DMA from the stack, switch to using
  351. * dynamically-allocated for the IV
  352. */
  353. iv = kzalloc_node(ivsize, GFP_ATOMIC,
  354. dev_to_node(&vcrypto->vdev->dev));
  355. if (!iv) {
  356. err = -ENOMEM;
  357. goto free;
  358. }
  359. memcpy(iv, req->iv, ivsize);
  360. if (!vc_sym_req->encrypt)
  361. scatterwalk_map_and_copy(req->iv, req->src,
  362. req->cryptlen - AES_BLOCK_SIZE,
  363. AES_BLOCK_SIZE, 0);
  364. sg_init_one(&iv_sg, iv, ivsize);
  365. sgs[num_out++] = &iv_sg;
  366. vc_sym_req->iv = iv;
  367. /* Source data */
  368. for (sg = req->src; src_nents; sg = sg_next(sg), src_nents--)
  369. sgs[num_out++] = sg;
  370. /* Destination data */
  371. for (sg = req->dst; sg; sg = sg_next(sg))
  372. sgs[num_out + num_in++] = sg;
  373. /* Status */
  374. sg_init_one(&status_sg, &vc_req->status, sizeof(vc_req->status));
  375. sgs[num_out + num_in++] = &status_sg;
  376. vc_req->sgs = sgs;
  377. spin_lock_irqsave(&data_vq->lock, flags);
  378. err = virtqueue_add_sgs(data_vq->vq, sgs, num_out,
  379. num_in, vc_req, GFP_ATOMIC);
  380. virtqueue_kick(data_vq->vq);
  381. spin_unlock_irqrestore(&data_vq->lock, flags);
  382. if (unlikely(err < 0))
  383. goto free_iv;
  384. return 0;
  385. free_iv:
  386. kfree_sensitive(iv);
  387. free:
  388. kfree_sensitive(req_data);
  389. kfree(sgs);
  390. return err;
  391. }
  392. static int virtio_crypto_skcipher_encrypt(struct skcipher_request *req)
  393. {
  394. struct crypto_skcipher *atfm = crypto_skcipher_reqtfm(req);
  395. struct virtio_crypto_skcipher_ctx *ctx = crypto_skcipher_ctx(atfm);
  396. struct virtio_crypto_sym_request *vc_sym_req =
  397. skcipher_request_ctx(req);
  398. struct virtio_crypto_request *vc_req = &vc_sym_req->base;
  399. struct virtio_crypto *vcrypto = ctx->vcrypto;
  400. /* Use the first data virtqueue as default */
  401. struct data_queue *data_vq = &vcrypto->data_vq[0];
  402. if (!req->cryptlen)
  403. return 0;
  404. if (req->cryptlen % AES_BLOCK_SIZE)
  405. return -EINVAL;
  406. vc_req->dataq = data_vq;
  407. vc_req->alg_cb = virtio_crypto_dataq_sym_callback;
  408. vc_sym_req->encrypt = true;
  409. return crypto_transfer_skcipher_request_to_engine(data_vq->engine, req);
  410. }
  411. static int virtio_crypto_skcipher_decrypt(struct skcipher_request *req)
  412. {
  413. struct crypto_skcipher *atfm = crypto_skcipher_reqtfm(req);
  414. struct virtio_crypto_skcipher_ctx *ctx = crypto_skcipher_ctx(atfm);
  415. struct virtio_crypto_sym_request *vc_sym_req =
  416. skcipher_request_ctx(req);
  417. struct virtio_crypto_request *vc_req = &vc_sym_req->base;
  418. struct virtio_crypto *vcrypto = ctx->vcrypto;
  419. /* Use the first data virtqueue as default */
  420. struct data_queue *data_vq = &vcrypto->data_vq[0];
  421. if (!req->cryptlen)
  422. return 0;
  423. if (req->cryptlen % AES_BLOCK_SIZE)
  424. return -EINVAL;
  425. vc_req->dataq = data_vq;
  426. vc_req->alg_cb = virtio_crypto_dataq_sym_callback;
  427. vc_sym_req->encrypt = false;
  428. return crypto_transfer_skcipher_request_to_engine(data_vq->engine, req);
  429. }
  430. static int virtio_crypto_skcipher_init(struct crypto_skcipher *tfm)
  431. {
  432. crypto_skcipher_set_reqsize(tfm, sizeof(struct virtio_crypto_sym_request));
  433. return 0;
  434. }
  435. static void virtio_crypto_skcipher_exit(struct crypto_skcipher *tfm)
  436. {
  437. struct virtio_crypto_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  438. if (!ctx->vcrypto)
  439. return;
  440. virtio_crypto_alg_skcipher_close_session(ctx, 1);
  441. virtio_crypto_alg_skcipher_close_session(ctx, 0);
  442. virtcrypto_dev_put(ctx->vcrypto);
  443. ctx->vcrypto = NULL;
  444. }
  445. int virtio_crypto_skcipher_crypt_req(
  446. struct crypto_engine *engine, void *vreq)
  447. {
  448. struct skcipher_request *req = container_of(vreq, struct skcipher_request, base);
  449. struct virtio_crypto_sym_request *vc_sym_req =
  450. skcipher_request_ctx(req);
  451. struct virtio_crypto_request *vc_req = &vc_sym_req->base;
  452. struct data_queue *data_vq = vc_req->dataq;
  453. int ret;
  454. ret = __virtio_crypto_skcipher_do_req(vc_sym_req, req, data_vq);
  455. if (ret < 0)
  456. return ret;
  457. return 0;
  458. }
  459. static void virtio_crypto_skcipher_finalize_req(
  460. struct virtio_crypto_sym_request *vc_sym_req,
  461. struct skcipher_request *req,
  462. int err)
  463. {
  464. if (vc_sym_req->encrypt)
  465. scatterwalk_map_and_copy(req->iv, req->dst,
  466. req->cryptlen - AES_BLOCK_SIZE,
  467. AES_BLOCK_SIZE, 0);
  468. kfree_sensitive(vc_sym_req->iv);
  469. virtcrypto_clear_request(&vc_sym_req->base);
  470. crypto_finalize_skcipher_request(vc_sym_req->base.dataq->engine,
  471. req, err);
  472. }
  473. static struct virtio_crypto_algo virtio_crypto_algs[] = { {
  474. .algonum = VIRTIO_CRYPTO_CIPHER_AES_CBC,
  475. .service = VIRTIO_CRYPTO_SERVICE_CIPHER,
  476. .algo.base = {
  477. .base.cra_name = "cbc(aes)",
  478. .base.cra_driver_name = "virtio_crypto_aes_cbc",
  479. .base.cra_priority = 150,
  480. .base.cra_flags = CRYPTO_ALG_ASYNC |
  481. CRYPTO_ALG_ALLOCATES_MEMORY,
  482. .base.cra_blocksize = AES_BLOCK_SIZE,
  483. .base.cra_ctxsize = sizeof(struct virtio_crypto_skcipher_ctx),
  484. .base.cra_module = THIS_MODULE,
  485. .init = virtio_crypto_skcipher_init,
  486. .exit = virtio_crypto_skcipher_exit,
  487. .setkey = virtio_crypto_skcipher_setkey,
  488. .decrypt = virtio_crypto_skcipher_decrypt,
  489. .encrypt = virtio_crypto_skcipher_encrypt,
  490. .min_keysize = AES_MIN_KEY_SIZE,
  491. .max_keysize = AES_MAX_KEY_SIZE,
  492. .ivsize = AES_BLOCK_SIZE,
  493. },
  494. .algo.op = {
  495. .do_one_request = virtio_crypto_skcipher_crypt_req,
  496. },
  497. } };
  498. int virtio_crypto_skcipher_algs_register(struct virtio_crypto *vcrypto)
  499. {
  500. int ret = 0;
  501. int i = 0;
  502. mutex_lock(&algs_lock);
  503. for (i = 0; i < ARRAY_SIZE(virtio_crypto_algs); i++) {
  504. uint32_t service = virtio_crypto_algs[i].service;
  505. uint32_t algonum = virtio_crypto_algs[i].algonum;
  506. if (!virtcrypto_algo_is_supported(vcrypto, service, algonum))
  507. continue;
  508. if (virtio_crypto_algs[i].active_devs == 0) {
  509. ret = crypto_engine_register_skcipher(&virtio_crypto_algs[i].algo);
  510. if (ret)
  511. goto unlock;
  512. }
  513. virtio_crypto_algs[i].active_devs++;
  514. dev_info(&vcrypto->vdev->dev, "Registered algo %s\n",
  515. virtio_crypto_algs[i].algo.base.base.cra_name);
  516. }
  517. unlock:
  518. mutex_unlock(&algs_lock);
  519. return ret;
  520. }
  521. void virtio_crypto_skcipher_algs_unregister(struct virtio_crypto *vcrypto)
  522. {
  523. int i = 0;
  524. mutex_lock(&algs_lock);
  525. for (i = 0; i < ARRAY_SIZE(virtio_crypto_algs); i++) {
  526. uint32_t service = virtio_crypto_algs[i].service;
  527. uint32_t algonum = virtio_crypto_algs[i].algonum;
  528. if (virtio_crypto_algs[i].active_devs == 0 ||
  529. !virtcrypto_algo_is_supported(vcrypto, service, algonum))
  530. continue;
  531. if (virtio_crypto_algs[i].active_devs == 1)
  532. crypto_engine_unregister_skcipher(&virtio_crypto_algs[i].algo);
  533. virtio_crypto_algs[i].active_devs--;
  534. }
  535. mutex_unlock(&algs_lock);
  536. }