virtio_crypto_algs.c 17 KB

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