virtio_crypto_algs.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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. struct virtio_crypto_algo {
  45. uint32_t algonum;
  46. uint32_t service;
  47. unsigned int active_devs;
  48. struct crypto_alg algo;
  49. };
  50. /*
  51. * The algs_lock protects the below global virtio_crypto_active_devs
  52. * and crypto algorithms registion.
  53. */
  54. static DEFINE_MUTEX(algs_lock);
  55. static void virtio_crypto_ablkcipher_finalize_req(
  56. struct virtio_crypto_sym_request *vc_sym_req,
  57. struct ablkcipher_request *req,
  58. int err);
  59. static void virtio_crypto_dataq_sym_callback
  60. (struct virtio_crypto_request *vc_req, int len)
  61. {
  62. struct virtio_crypto_sym_request *vc_sym_req =
  63. container_of(vc_req, struct virtio_crypto_sym_request, base);
  64. struct ablkcipher_request *ablk_req;
  65. int error;
  66. /* Finish the encrypt or decrypt process */
  67. if (vc_sym_req->type == VIRTIO_CRYPTO_SYM_OP_CIPHER) {
  68. switch (vc_req->status) {
  69. case VIRTIO_CRYPTO_OK:
  70. error = 0;
  71. break;
  72. case VIRTIO_CRYPTO_INVSESS:
  73. case VIRTIO_CRYPTO_ERR:
  74. error = -EINVAL;
  75. break;
  76. case VIRTIO_CRYPTO_BADMSG:
  77. error = -EBADMSG;
  78. break;
  79. default:
  80. error = -EIO;
  81. break;
  82. }
  83. ablk_req = vc_sym_req->ablkcipher_req;
  84. virtio_crypto_ablkcipher_finalize_req(vc_sym_req,
  85. ablk_req, error);
  86. }
  87. }
  88. static u64 virtio_crypto_alg_sg_nents_length(struct scatterlist *sg)
  89. {
  90. u64 total = 0;
  91. for (total = 0; sg; sg = sg_next(sg))
  92. total += sg->length;
  93. return total;
  94. }
  95. static int
  96. virtio_crypto_alg_validate_key(int key_len, uint32_t *alg)
  97. {
  98. switch (key_len) {
  99. case AES_KEYSIZE_128:
  100. case AES_KEYSIZE_192:
  101. case AES_KEYSIZE_256:
  102. *alg = VIRTIO_CRYPTO_CIPHER_AES_CBC;
  103. break;
  104. default:
  105. return -EINVAL;
  106. }
  107. return 0;
  108. }
  109. static int virtio_crypto_alg_ablkcipher_init_session(
  110. struct virtio_crypto_ablkcipher_ctx *ctx,
  111. uint32_t alg, const uint8_t *key,
  112. unsigned int keylen,
  113. int encrypt)
  114. {
  115. struct scatterlist outhdr, key_sg, inhdr, *sgs[3];
  116. unsigned int tmp;
  117. struct virtio_crypto *vcrypto = ctx->vcrypto;
  118. int op = encrypt ? VIRTIO_CRYPTO_OP_ENCRYPT : VIRTIO_CRYPTO_OP_DECRYPT;
  119. int err;
  120. unsigned int num_out = 0, num_in = 0;
  121. /*
  122. * Avoid to do DMA from the stack, switch to using
  123. * dynamically-allocated for the key
  124. */
  125. uint8_t *cipher_key = kmalloc(keylen, GFP_ATOMIC);
  126. if (!cipher_key)
  127. return -ENOMEM;
  128. memcpy(cipher_key, key, keylen);
  129. spin_lock(&vcrypto->ctrl_lock);
  130. /* Pad ctrl header */
  131. vcrypto->ctrl.header.opcode =
  132. cpu_to_le32(VIRTIO_CRYPTO_CIPHER_CREATE_SESSION);
  133. vcrypto->ctrl.header.algo = cpu_to_le32(alg);
  134. /* Set the default dataqueue id to 0 */
  135. vcrypto->ctrl.header.queue_id = 0;
  136. vcrypto->input.status = cpu_to_le32(VIRTIO_CRYPTO_ERR);
  137. /* Pad cipher's parameters */
  138. vcrypto->ctrl.u.sym_create_session.op_type =
  139. cpu_to_le32(VIRTIO_CRYPTO_SYM_OP_CIPHER);
  140. vcrypto->ctrl.u.sym_create_session.u.cipher.para.algo =
  141. vcrypto->ctrl.header.algo;
  142. vcrypto->ctrl.u.sym_create_session.u.cipher.para.keylen =
  143. cpu_to_le32(keylen);
  144. vcrypto->ctrl.u.sym_create_session.u.cipher.para.op =
  145. cpu_to_le32(op);
  146. sg_init_one(&outhdr, &vcrypto->ctrl, sizeof(vcrypto->ctrl));
  147. sgs[num_out++] = &outhdr;
  148. /* Set key */
  149. sg_init_one(&key_sg, cipher_key, keylen);
  150. sgs[num_out++] = &key_sg;
  151. /* Return status and session id back */
  152. sg_init_one(&inhdr, &vcrypto->input, sizeof(vcrypto->input));
  153. sgs[num_out + num_in++] = &inhdr;
  154. err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out,
  155. num_in, vcrypto, GFP_ATOMIC);
  156. if (err < 0) {
  157. spin_unlock(&vcrypto->ctrl_lock);
  158. kzfree(cipher_key);
  159. return err;
  160. }
  161. virtqueue_kick(vcrypto->ctrl_vq);
  162. /*
  163. * Trapping into the hypervisor, so the request should be
  164. * handled immediately.
  165. */
  166. while (!virtqueue_get_buf(vcrypto->ctrl_vq, &tmp) &&
  167. !virtqueue_is_broken(vcrypto->ctrl_vq))
  168. cpu_relax();
  169. if (le32_to_cpu(vcrypto->input.status) != VIRTIO_CRYPTO_OK) {
  170. spin_unlock(&vcrypto->ctrl_lock);
  171. pr_err("virtio_crypto: Create session failed status: %u\n",
  172. le32_to_cpu(vcrypto->input.status));
  173. kzfree(cipher_key);
  174. return -EINVAL;
  175. }
  176. if (encrypt)
  177. ctx->enc_sess_info.session_id =
  178. le64_to_cpu(vcrypto->input.session_id);
  179. else
  180. ctx->dec_sess_info.session_id =
  181. le64_to_cpu(vcrypto->input.session_id);
  182. spin_unlock(&vcrypto->ctrl_lock);
  183. kzfree(cipher_key);
  184. return 0;
  185. }
  186. static int virtio_crypto_alg_ablkcipher_close_session(
  187. struct virtio_crypto_ablkcipher_ctx *ctx,
  188. int encrypt)
  189. {
  190. struct scatterlist outhdr, status_sg, *sgs[2];
  191. unsigned int tmp;
  192. struct virtio_crypto_destroy_session_req *destroy_session;
  193. struct virtio_crypto *vcrypto = ctx->vcrypto;
  194. int err;
  195. unsigned int num_out = 0, num_in = 0;
  196. spin_lock(&vcrypto->ctrl_lock);
  197. vcrypto->ctrl_status.status = VIRTIO_CRYPTO_ERR;
  198. /* Pad ctrl header */
  199. vcrypto->ctrl.header.opcode =
  200. cpu_to_le32(VIRTIO_CRYPTO_CIPHER_DESTROY_SESSION);
  201. /* Set the default virtqueue id to 0 */
  202. vcrypto->ctrl.header.queue_id = 0;
  203. destroy_session = &vcrypto->ctrl.u.destroy_session;
  204. if (encrypt)
  205. destroy_session->session_id =
  206. cpu_to_le64(ctx->enc_sess_info.session_id);
  207. else
  208. destroy_session->session_id =
  209. cpu_to_le64(ctx->dec_sess_info.session_id);
  210. sg_init_one(&outhdr, &vcrypto->ctrl, sizeof(vcrypto->ctrl));
  211. sgs[num_out++] = &outhdr;
  212. /* Return status and session id back */
  213. sg_init_one(&status_sg, &vcrypto->ctrl_status.status,
  214. sizeof(vcrypto->ctrl_status.status));
  215. sgs[num_out + num_in++] = &status_sg;
  216. err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out,
  217. num_in, vcrypto, GFP_ATOMIC);
  218. if (err < 0) {
  219. spin_unlock(&vcrypto->ctrl_lock);
  220. return err;
  221. }
  222. virtqueue_kick(vcrypto->ctrl_vq);
  223. while (!virtqueue_get_buf(vcrypto->ctrl_vq, &tmp) &&
  224. !virtqueue_is_broken(vcrypto->ctrl_vq))
  225. cpu_relax();
  226. if (vcrypto->ctrl_status.status != VIRTIO_CRYPTO_OK) {
  227. spin_unlock(&vcrypto->ctrl_lock);
  228. pr_err("virtio_crypto: Close session failed status: %u, session_id: 0x%llx\n",
  229. vcrypto->ctrl_status.status,
  230. destroy_session->session_id);
  231. return -EINVAL;
  232. }
  233. spin_unlock(&vcrypto->ctrl_lock);
  234. return 0;
  235. }
  236. static int virtio_crypto_alg_ablkcipher_init_sessions(
  237. struct virtio_crypto_ablkcipher_ctx *ctx,
  238. const uint8_t *key, unsigned int keylen)
  239. {
  240. uint32_t alg;
  241. int ret;
  242. struct virtio_crypto *vcrypto = ctx->vcrypto;
  243. if (keylen > vcrypto->max_cipher_key_len) {
  244. pr_err("virtio_crypto: the key is too long\n");
  245. goto bad_key;
  246. }
  247. if (virtio_crypto_alg_validate_key(keylen, &alg))
  248. goto bad_key;
  249. /* Create encryption session */
  250. ret = virtio_crypto_alg_ablkcipher_init_session(ctx,
  251. alg, key, keylen, 1);
  252. if (ret)
  253. return ret;
  254. /* Create decryption session */
  255. ret = virtio_crypto_alg_ablkcipher_init_session(ctx,
  256. alg, key, keylen, 0);
  257. if (ret) {
  258. virtio_crypto_alg_ablkcipher_close_session(ctx, 1);
  259. return ret;
  260. }
  261. return 0;
  262. bad_key:
  263. crypto_tfm_set_flags(ctx->tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  264. return -EINVAL;
  265. }
  266. /* Note: kernel crypto API realization */
  267. static int virtio_crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
  268. const uint8_t *key,
  269. unsigned int keylen)
  270. {
  271. struct virtio_crypto_ablkcipher_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  272. uint32_t alg;
  273. int ret;
  274. ret = virtio_crypto_alg_validate_key(keylen, &alg);
  275. if (ret)
  276. return ret;
  277. if (!ctx->vcrypto) {
  278. /* New key */
  279. int node = virtio_crypto_get_current_node();
  280. struct virtio_crypto *vcrypto =
  281. virtcrypto_get_dev_node(node,
  282. VIRTIO_CRYPTO_SERVICE_CIPHER, alg);
  283. if (!vcrypto) {
  284. pr_err("virtio_crypto: Could not find a virtio device in the system or unsupported algo\n");
  285. return -ENODEV;
  286. }
  287. ctx->vcrypto = vcrypto;
  288. } else {
  289. /* Rekeying, we should close the created sessions previously */
  290. virtio_crypto_alg_ablkcipher_close_session(ctx, 1);
  291. virtio_crypto_alg_ablkcipher_close_session(ctx, 0);
  292. }
  293. ret = virtio_crypto_alg_ablkcipher_init_sessions(ctx, key, keylen);
  294. if (ret) {
  295. virtcrypto_dev_put(ctx->vcrypto);
  296. ctx->vcrypto = NULL;
  297. return ret;
  298. }
  299. return 0;
  300. }
  301. static int
  302. __virtio_crypto_ablkcipher_do_req(struct virtio_crypto_sym_request *vc_sym_req,
  303. struct ablkcipher_request *req,
  304. struct data_queue *data_vq)
  305. {
  306. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  307. struct virtio_crypto_ablkcipher_ctx *ctx = vc_sym_req->ablkcipher_ctx;
  308. struct virtio_crypto_request *vc_req = &vc_sym_req->base;
  309. unsigned int ivsize = crypto_ablkcipher_ivsize(tfm);
  310. struct virtio_crypto *vcrypto = ctx->vcrypto;
  311. struct virtio_crypto_op_data_req *req_data;
  312. int src_nents, dst_nents;
  313. int err;
  314. unsigned long flags;
  315. struct scatterlist outhdr, iv_sg, status_sg, **sgs;
  316. u64 dst_len;
  317. unsigned int num_out = 0, num_in = 0;
  318. int sg_total;
  319. uint8_t *iv;
  320. struct scatterlist *sg;
  321. src_nents = sg_nents_for_len(req->src, req->nbytes);
  322. if (src_nents < 0) {
  323. pr_err("Invalid number of src SG.\n");
  324. return src_nents;
  325. }
  326. dst_nents = sg_nents(req->dst);
  327. pr_debug("virtio_crypto: Number of sgs (src_nents: %d, dst_nents: %d)\n",
  328. src_nents, dst_nents);
  329. /* Why 3? outhdr + iv + inhdr */
  330. sg_total = src_nents + dst_nents + 3;
  331. sgs = kcalloc_node(sg_total, sizeof(*sgs), GFP_KERNEL,
  332. dev_to_node(&vcrypto->vdev->dev));
  333. if (!sgs)
  334. return -ENOMEM;
  335. req_data = kzalloc_node(sizeof(*req_data), GFP_KERNEL,
  336. dev_to_node(&vcrypto->vdev->dev));
  337. if (!req_data) {
  338. kfree(sgs);
  339. return -ENOMEM;
  340. }
  341. vc_req->req_data = req_data;
  342. vc_sym_req->type = VIRTIO_CRYPTO_SYM_OP_CIPHER;
  343. /* Head of operation */
  344. if (vc_sym_req->encrypt) {
  345. req_data->header.session_id =
  346. cpu_to_le64(ctx->enc_sess_info.session_id);
  347. req_data->header.opcode =
  348. cpu_to_le32(VIRTIO_CRYPTO_CIPHER_ENCRYPT);
  349. } else {
  350. req_data->header.session_id =
  351. cpu_to_le64(ctx->dec_sess_info.session_id);
  352. req_data->header.opcode =
  353. cpu_to_le32(VIRTIO_CRYPTO_CIPHER_DECRYPT);
  354. }
  355. req_data->u.sym_req.op_type = cpu_to_le32(VIRTIO_CRYPTO_SYM_OP_CIPHER);
  356. req_data->u.sym_req.u.cipher.para.iv_len = cpu_to_le32(ivsize);
  357. req_data->u.sym_req.u.cipher.para.src_data_len =
  358. cpu_to_le32(req->nbytes);
  359. dst_len = virtio_crypto_alg_sg_nents_length(req->dst);
  360. if (unlikely(dst_len > U32_MAX)) {
  361. pr_err("virtio_crypto: The dst_len is beyond U32_MAX\n");
  362. err = -EINVAL;
  363. goto free;
  364. }
  365. dst_len = min_t(unsigned int, req->nbytes, dst_len);
  366. pr_debug("virtio_crypto: src_len: %u, dst_len: %llu\n",
  367. req->nbytes, dst_len);
  368. if (unlikely(req->nbytes + dst_len + ivsize +
  369. sizeof(vc_req->status) > vcrypto->max_size)) {
  370. pr_err("virtio_crypto: The length is too big\n");
  371. err = -EINVAL;
  372. goto free;
  373. }
  374. req_data->u.sym_req.u.cipher.para.dst_data_len =
  375. cpu_to_le32((uint32_t)dst_len);
  376. /* Outhdr */
  377. sg_init_one(&outhdr, req_data, sizeof(*req_data));
  378. sgs[num_out++] = &outhdr;
  379. /* IV */
  380. /*
  381. * Avoid to do DMA from the stack, switch to using
  382. * dynamically-allocated for the IV
  383. */
  384. iv = kzalloc_node(ivsize, GFP_ATOMIC,
  385. dev_to_node(&vcrypto->vdev->dev));
  386. if (!iv) {
  387. err = -ENOMEM;
  388. goto free;
  389. }
  390. memcpy(iv, req->info, ivsize);
  391. if (!vc_sym_req->encrypt)
  392. scatterwalk_map_and_copy(req->info, req->src,
  393. req->nbytes - AES_BLOCK_SIZE,
  394. AES_BLOCK_SIZE, 0);
  395. sg_init_one(&iv_sg, iv, ivsize);
  396. sgs[num_out++] = &iv_sg;
  397. vc_sym_req->iv = iv;
  398. /* Source data */
  399. for (sg = req->src; src_nents; sg = sg_next(sg), src_nents--)
  400. sgs[num_out++] = sg;
  401. /* Destination data */
  402. for (sg = req->dst; sg; sg = sg_next(sg))
  403. sgs[num_out + num_in++] = sg;
  404. /* Status */
  405. sg_init_one(&status_sg, &vc_req->status, sizeof(vc_req->status));
  406. sgs[num_out + num_in++] = &status_sg;
  407. vc_req->sgs = sgs;
  408. spin_lock_irqsave(&data_vq->lock, flags);
  409. err = virtqueue_add_sgs(data_vq->vq, sgs, num_out,
  410. num_in, vc_req, GFP_ATOMIC);
  411. virtqueue_kick(data_vq->vq);
  412. spin_unlock_irqrestore(&data_vq->lock, flags);
  413. if (unlikely(err < 0))
  414. goto free_iv;
  415. return 0;
  416. free_iv:
  417. kzfree(iv);
  418. free:
  419. kzfree(req_data);
  420. kfree(sgs);
  421. return err;
  422. }
  423. static int virtio_crypto_ablkcipher_encrypt(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. if (!req->nbytes)
  434. return 0;
  435. if (req->nbytes % AES_BLOCK_SIZE)
  436. return -EINVAL;
  437. vc_req->dataq = data_vq;
  438. vc_req->alg_cb = virtio_crypto_dataq_sym_callback;
  439. vc_sym_req->ablkcipher_ctx = ctx;
  440. vc_sym_req->ablkcipher_req = req;
  441. vc_sym_req->encrypt = true;
  442. return crypto_transfer_ablkcipher_request_to_engine(data_vq->engine, req);
  443. }
  444. static int virtio_crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
  445. {
  446. struct crypto_ablkcipher *atfm = crypto_ablkcipher_reqtfm(req);
  447. struct virtio_crypto_ablkcipher_ctx *ctx = crypto_ablkcipher_ctx(atfm);
  448. struct virtio_crypto_sym_request *vc_sym_req =
  449. ablkcipher_request_ctx(req);
  450. struct virtio_crypto_request *vc_req = &vc_sym_req->base;
  451. struct virtio_crypto *vcrypto = ctx->vcrypto;
  452. /* Use the first data virtqueue as default */
  453. struct data_queue *data_vq = &vcrypto->data_vq[0];
  454. if (!req->nbytes)
  455. return 0;
  456. if (req->nbytes % AES_BLOCK_SIZE)
  457. return -EINVAL;
  458. vc_req->dataq = data_vq;
  459. vc_req->alg_cb = virtio_crypto_dataq_sym_callback;
  460. vc_sym_req->ablkcipher_ctx = ctx;
  461. vc_sym_req->ablkcipher_req = req;
  462. vc_sym_req->encrypt = false;
  463. return crypto_transfer_ablkcipher_request_to_engine(data_vq->engine, req);
  464. }
  465. static int virtio_crypto_ablkcipher_init(struct crypto_tfm *tfm)
  466. {
  467. struct virtio_crypto_ablkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
  468. tfm->crt_ablkcipher.reqsize = sizeof(struct virtio_crypto_sym_request);
  469. ctx->tfm = tfm;
  470. ctx->enginectx.op.do_one_request = virtio_crypto_ablkcipher_crypt_req;
  471. ctx->enginectx.op.prepare_request = NULL;
  472. ctx->enginectx.op.unprepare_request = NULL;
  473. return 0;
  474. }
  475. static void virtio_crypto_ablkcipher_exit(struct crypto_tfm *tfm)
  476. {
  477. struct virtio_crypto_ablkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
  478. if (!ctx->vcrypto)
  479. return;
  480. virtio_crypto_alg_ablkcipher_close_session(ctx, 1);
  481. virtio_crypto_alg_ablkcipher_close_session(ctx, 0);
  482. virtcrypto_dev_put(ctx->vcrypto);
  483. ctx->vcrypto = NULL;
  484. }
  485. int virtio_crypto_ablkcipher_crypt_req(
  486. struct crypto_engine *engine, void *vreq)
  487. {
  488. struct ablkcipher_request *req = container_of(vreq, struct ablkcipher_request, base);
  489. struct virtio_crypto_sym_request *vc_sym_req =
  490. ablkcipher_request_ctx(req);
  491. struct virtio_crypto_request *vc_req = &vc_sym_req->base;
  492. struct data_queue *data_vq = vc_req->dataq;
  493. int ret;
  494. ret = __virtio_crypto_ablkcipher_do_req(vc_sym_req, req, data_vq);
  495. if (ret < 0)
  496. return ret;
  497. virtqueue_kick(data_vq->vq);
  498. return 0;
  499. }
  500. static void virtio_crypto_ablkcipher_finalize_req(
  501. struct virtio_crypto_sym_request *vc_sym_req,
  502. struct ablkcipher_request *req,
  503. int err)
  504. {
  505. if (vc_sym_req->encrypt)
  506. scatterwalk_map_and_copy(req->info, req->dst,
  507. req->nbytes - AES_BLOCK_SIZE,
  508. AES_BLOCK_SIZE, 0);
  509. kzfree(vc_sym_req->iv);
  510. virtcrypto_clear_request(&vc_sym_req->base);
  511. crypto_finalize_ablkcipher_request(vc_sym_req->base.dataq->engine,
  512. req, err);
  513. }
  514. static struct virtio_crypto_algo virtio_crypto_algs[] = { {
  515. .algonum = VIRTIO_CRYPTO_CIPHER_AES_CBC,
  516. .service = VIRTIO_CRYPTO_SERVICE_CIPHER,
  517. .algo = {
  518. .cra_name = "cbc(aes)",
  519. .cra_driver_name = "virtio_crypto_aes_cbc",
  520. .cra_priority = 150,
  521. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  522. .cra_blocksize = AES_BLOCK_SIZE,
  523. .cra_ctxsize = sizeof(struct virtio_crypto_ablkcipher_ctx),
  524. .cra_alignmask = 0,
  525. .cra_module = THIS_MODULE,
  526. .cra_type = &crypto_ablkcipher_type,
  527. .cra_init = virtio_crypto_ablkcipher_init,
  528. .cra_exit = virtio_crypto_ablkcipher_exit,
  529. .cra_u = {
  530. .ablkcipher = {
  531. .setkey = virtio_crypto_ablkcipher_setkey,
  532. .decrypt = virtio_crypto_ablkcipher_decrypt,
  533. .encrypt = virtio_crypto_ablkcipher_encrypt,
  534. .min_keysize = AES_MIN_KEY_SIZE,
  535. .max_keysize = AES_MAX_KEY_SIZE,
  536. .ivsize = AES_BLOCK_SIZE,
  537. },
  538. },
  539. },
  540. } };
  541. int virtio_crypto_algs_register(struct virtio_crypto *vcrypto)
  542. {
  543. int ret = 0;
  544. int i = 0;
  545. mutex_lock(&algs_lock);
  546. for (i = 0; i < ARRAY_SIZE(virtio_crypto_algs); i++) {
  547. uint32_t service = virtio_crypto_algs[i].service;
  548. uint32_t algonum = virtio_crypto_algs[i].algonum;
  549. if (!virtcrypto_algo_is_supported(vcrypto, service, algonum))
  550. continue;
  551. if (virtio_crypto_algs[i].active_devs == 0) {
  552. ret = crypto_register_alg(&virtio_crypto_algs[i].algo);
  553. if (ret)
  554. goto unlock;
  555. }
  556. virtio_crypto_algs[i].active_devs++;
  557. dev_info(&vcrypto->vdev->dev, "Registered algo %s\n",
  558. virtio_crypto_algs[i].algo.cra_name);
  559. }
  560. unlock:
  561. mutex_unlock(&algs_lock);
  562. return ret;
  563. }
  564. void virtio_crypto_algs_unregister(struct virtio_crypto *vcrypto)
  565. {
  566. int i = 0;
  567. mutex_lock(&algs_lock);
  568. for (i = 0; i < ARRAY_SIZE(virtio_crypto_algs); i++) {
  569. uint32_t service = virtio_crypto_algs[i].service;
  570. uint32_t algonum = virtio_crypto_algs[i].algonum;
  571. if (virtio_crypto_algs[i].active_devs == 0 ||
  572. !virtcrypto_algo_is_supported(vcrypto, service, algonum))
  573. continue;
  574. if (virtio_crypto_algs[i].active_devs == 1)
  575. crypto_unregister_alg(&virtio_crypto_algs[i].algo);
  576. virtio_crypto_algs[i].active_devs--;
  577. }
  578. mutex_unlock(&algs_lock);
  579. }