virtio_crypto_skcipher_algs.c 17 KB

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