virtio_crypto_algs.c 16 KB

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