2
0

cryptodev-lkcf.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. /*
  2. * QEMU Cryptodev backend for QEMU cipher APIs
  3. *
  4. * Copyright (c) 2022 Bytedance.Inc
  5. *
  6. * Authors:
  7. * lei he <helei.sig11@bytedance.com>
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. #include "qemu/osdep.h"
  24. #include "crypto/cipher.h"
  25. #include "crypto/akcipher.h"
  26. #include "qapi/error.h"
  27. #include "qemu/main-loop.h"
  28. #include "qemu/thread.h"
  29. #include "qemu/error-report.h"
  30. #include "qemu/queue.h"
  31. #include "qom/object.h"
  32. #include "sysemu/cryptodev.h"
  33. #include "standard-headers/linux/virtio_crypto.h"
  34. #include <keyutils.h>
  35. #include <sys/eventfd.h>
  36. /**
  37. * @TYPE_CRYPTODEV_BACKEND_LKCF:
  38. * name of backend that uses linux kernel crypto framework
  39. */
  40. #define TYPE_CRYPTODEV_BACKEND_LKCF "cryptodev-backend-lkcf"
  41. OBJECT_DECLARE_SIMPLE_TYPE(CryptoDevBackendLKCF, CRYPTODEV_BACKEND_LKCF)
  42. #define INVALID_KEY_ID -1
  43. #define MAX_SESSIONS 256
  44. #define NR_WORKER_THREAD 64
  45. #define KCTL_KEY_TYPE_PKEY "asymmetric"
  46. /**
  47. * Here the key is uploaded to the thread-keyring of worker thread, at least
  48. * util linux-6.0:
  49. * 1. process keyring seems to behave unexpectedly if main-thread does not
  50. * create the keyring before creating any other thread.
  51. * 2. at present, the guest kernel never perform multiple operations on a
  52. * session.
  53. * 3. it can reduce the load of the main-loop because the key passed by the
  54. * guest kernel has been already checked.
  55. */
  56. #define KCTL_KEY_RING KEY_SPEC_THREAD_KEYRING
  57. typedef struct CryptoDevBackendLKCFSession {
  58. uint8_t *key;
  59. size_t keylen;
  60. QCryptoAkCipherKeyType keytype;
  61. QCryptoAkCipherOptions akcipher_opts;
  62. } CryptoDevBackendLKCFSession;
  63. typedef struct CryptoDevBackendLKCF CryptoDevBackendLKCF;
  64. typedef struct CryptoDevLKCFTask CryptoDevLKCFTask;
  65. struct CryptoDevLKCFTask {
  66. CryptoDevBackendLKCFSession *sess;
  67. CryptoDevBackendOpInfo *op_info;
  68. CryptoDevCompletionFunc cb;
  69. void *opaque;
  70. int status;
  71. CryptoDevBackendLKCF *lkcf;
  72. QSIMPLEQ_ENTRY(CryptoDevLKCFTask) queue;
  73. };
  74. typedef struct CryptoDevBackendLKCF {
  75. CryptoDevBackend parent_obj;
  76. CryptoDevBackendLKCFSession *sess[MAX_SESSIONS];
  77. QSIMPLEQ_HEAD(, CryptoDevLKCFTask) requests;
  78. QSIMPLEQ_HEAD(, CryptoDevLKCFTask) responses;
  79. QemuMutex mutex;
  80. QemuCond cond;
  81. QemuMutex rsp_mutex;
  82. /**
  83. * There is no async interface for asymmetric keys like AF_ALG sockets,
  84. * we don't seem to have better way than create a lots of thread.
  85. */
  86. QemuThread worker_threads[NR_WORKER_THREAD];
  87. bool running;
  88. int eventfd;
  89. } CryptoDevBackendLKCF;
  90. static void *cryptodev_lkcf_worker(void *arg);
  91. static int cryptodev_lkcf_close_session(CryptoDevBackend *backend,
  92. uint64_t session_id,
  93. uint32_t queue_index,
  94. CryptoDevCompletionFunc cb,
  95. void *opaque);
  96. static void cryptodev_lkcf_handle_response(void *opaque)
  97. {
  98. CryptoDevBackendLKCF *lkcf = (CryptoDevBackendLKCF *)opaque;
  99. QSIMPLEQ_HEAD(, CryptoDevLKCFTask) responses;
  100. CryptoDevLKCFTask *task, *next;
  101. eventfd_t nevent;
  102. QSIMPLEQ_INIT(&responses);
  103. eventfd_read(lkcf->eventfd, &nevent);
  104. qemu_mutex_lock(&lkcf->rsp_mutex);
  105. QSIMPLEQ_PREPEND(&responses, &lkcf->responses);
  106. qemu_mutex_unlock(&lkcf->rsp_mutex);
  107. QSIMPLEQ_FOREACH_SAFE(task, &responses, queue, next) {
  108. if (task->cb) {
  109. task->cb(task->opaque, task->status);
  110. }
  111. g_free(task);
  112. }
  113. }
  114. static int cryptodev_lkcf_set_op_desc(QCryptoAkCipherOptions *opts,
  115. char *key_desc,
  116. size_t desc_len,
  117. Error **errp)
  118. {
  119. QCryptoAkCipherOptionsRSA *rsa_opt;
  120. if (opts->alg != QCRYPTO_AKCIPHER_ALG_RSA) {
  121. error_setg(errp, "Unsupported alg: %u", opts->alg);
  122. return -1;
  123. }
  124. rsa_opt = &opts->u.rsa;
  125. if (rsa_opt->padding_alg == QCRYPTO_RSA_PADDING_ALG_PKCS1) {
  126. snprintf(key_desc, desc_len, "enc=%s hash=%s",
  127. QCryptoRSAPaddingAlgorithm_str(rsa_opt->padding_alg),
  128. QCryptoHashAlgorithm_str(rsa_opt->hash_alg));
  129. } else {
  130. snprintf(key_desc, desc_len, "enc=%s",
  131. QCryptoRSAPaddingAlgorithm_str(rsa_opt->padding_alg));
  132. }
  133. return 0;
  134. }
  135. static int cryptodev_lkcf_set_rsa_opt(int virtio_padding_alg,
  136. int virtio_hash_alg,
  137. QCryptoAkCipherOptionsRSA *opt,
  138. Error **errp)
  139. {
  140. if (virtio_padding_alg == VIRTIO_CRYPTO_RSA_PKCS1_PADDING) {
  141. opt->padding_alg = QCRYPTO_RSA_PADDING_ALG_PKCS1;
  142. switch (virtio_hash_alg) {
  143. case VIRTIO_CRYPTO_RSA_MD5:
  144. opt->hash_alg = QCRYPTO_HASH_ALG_MD5;
  145. break;
  146. case VIRTIO_CRYPTO_RSA_SHA1:
  147. opt->hash_alg = QCRYPTO_HASH_ALG_SHA1;
  148. break;
  149. case VIRTIO_CRYPTO_RSA_SHA256:
  150. opt->hash_alg = QCRYPTO_HASH_ALG_SHA256;
  151. break;
  152. case VIRTIO_CRYPTO_RSA_SHA512:
  153. opt->hash_alg = QCRYPTO_HASH_ALG_SHA512;
  154. break;
  155. default:
  156. error_setg(errp, "Unsupported rsa hash algo: %d", virtio_hash_alg);
  157. return -1;
  158. }
  159. return 0;
  160. }
  161. if (virtio_padding_alg == VIRTIO_CRYPTO_RSA_RAW_PADDING) {
  162. opt->padding_alg = QCRYPTO_RSA_PADDING_ALG_RAW;
  163. return 0;
  164. }
  165. error_setg(errp, "Unsupported rsa padding algo: %u", virtio_padding_alg);
  166. return -1;
  167. }
  168. static int cryptodev_lkcf_get_unused_session_index(CryptoDevBackendLKCF *lkcf)
  169. {
  170. size_t i;
  171. for (i = 0; i < MAX_SESSIONS; i++) {
  172. if (lkcf->sess[i] == NULL) {
  173. return i;
  174. }
  175. }
  176. return -1;
  177. }
  178. static void cryptodev_lkcf_init(CryptoDevBackend *backend, Error **errp)
  179. {
  180. /* Only support one queue */
  181. int queues = backend->conf.peers.queues, i;
  182. CryptoDevBackendClient *cc;
  183. CryptoDevBackendLKCF *lkcf =
  184. CRYPTODEV_BACKEND_LKCF(backend);
  185. if (queues != 1) {
  186. error_setg(errp,
  187. "Only support one queue in cryptodev-builtin backend");
  188. return;
  189. }
  190. lkcf->eventfd = eventfd(0, 0);
  191. if (lkcf->eventfd < 0) {
  192. error_setg(errp, "Failed to create eventfd: %d", errno);
  193. return;
  194. }
  195. cc = cryptodev_backend_new_client();
  196. cc->info_str = g_strdup_printf("cryptodev-lkcf0");
  197. cc->queue_index = 0;
  198. cc->type = QCRYPTODEV_BACKEND_TYPE_LKCF;
  199. backend->conf.peers.ccs[0] = cc;
  200. backend->conf.crypto_services =
  201. 1u << QCRYPTODEV_BACKEND_SERVICE_AKCIPHER;
  202. backend->conf.akcipher_algo = 1u << VIRTIO_CRYPTO_AKCIPHER_RSA;
  203. lkcf->running = true;
  204. QSIMPLEQ_INIT(&lkcf->requests);
  205. QSIMPLEQ_INIT(&lkcf->responses);
  206. qemu_mutex_init(&lkcf->mutex);
  207. qemu_mutex_init(&lkcf->rsp_mutex);
  208. qemu_cond_init(&lkcf->cond);
  209. for (i = 0; i < NR_WORKER_THREAD; i++) {
  210. qemu_thread_create(&lkcf->worker_threads[i], "lkcf-worker",
  211. cryptodev_lkcf_worker, lkcf, 0);
  212. }
  213. qemu_set_fd_handler(
  214. lkcf->eventfd, cryptodev_lkcf_handle_response, NULL, lkcf);
  215. cryptodev_backend_set_ready(backend, true);
  216. }
  217. static void cryptodev_lkcf_cleanup(CryptoDevBackend *backend, Error **errp)
  218. {
  219. CryptoDevBackendLKCF *lkcf = CRYPTODEV_BACKEND_LKCF(backend);
  220. size_t i;
  221. int queues = backend->conf.peers.queues;
  222. CryptoDevBackendClient *cc;
  223. CryptoDevLKCFTask *task, *next;
  224. qemu_mutex_lock(&lkcf->mutex);
  225. lkcf->running = false;
  226. qemu_mutex_unlock(&lkcf->mutex);
  227. qemu_cond_broadcast(&lkcf->cond);
  228. close(lkcf->eventfd);
  229. for (i = 0; i < NR_WORKER_THREAD; i++) {
  230. qemu_thread_join(&lkcf->worker_threads[i]);
  231. }
  232. QSIMPLEQ_FOREACH_SAFE(task, &lkcf->requests, queue, next) {
  233. if (task->cb) {
  234. task->cb(task->opaque, task->status);
  235. }
  236. g_free(task);
  237. }
  238. QSIMPLEQ_FOREACH_SAFE(task, &lkcf->responses, queue, next) {
  239. if (task->cb) {
  240. task->cb(task->opaque, task->status);
  241. }
  242. g_free(task);
  243. }
  244. qemu_mutex_destroy(&lkcf->mutex);
  245. qemu_cond_destroy(&lkcf->cond);
  246. qemu_mutex_destroy(&lkcf->rsp_mutex);
  247. for (i = 0; i < MAX_SESSIONS; i++) {
  248. if (lkcf->sess[i] != NULL) {
  249. cryptodev_lkcf_close_session(backend, i, 0, NULL, NULL);
  250. }
  251. }
  252. for (i = 0; i < queues; i++) {
  253. cc = backend->conf.peers.ccs[i];
  254. if (cc) {
  255. cryptodev_backend_free_client(cc);
  256. backend->conf.peers.ccs[i] = NULL;
  257. }
  258. }
  259. cryptodev_backend_set_ready(backend, false);
  260. }
  261. static void cryptodev_lkcf_execute_task(CryptoDevLKCFTask *task)
  262. {
  263. CryptoDevBackendLKCFSession *session = task->sess;
  264. CryptoDevBackendAsymOpInfo *asym_op_info;
  265. bool kick = false;
  266. int ret, status, op_code = task->op_info->op_code;
  267. size_t p8info_len;
  268. g_autofree uint8_t *p8info = NULL;
  269. Error *local_error = NULL;
  270. key_serial_t key_id = INVALID_KEY_ID;
  271. char op_desc[64];
  272. g_autoptr(QCryptoAkCipher) akcipher = NULL;
  273. /**
  274. * We only offload private key session:
  275. * 1. currently, the Linux kernel can only accept public key wrapped
  276. * with X.509 certificates, but unfortunately the cost of making a
  277. * ceritificate with public key is too expensive.
  278. * 2. generally, public key related compution is fast, just compute it with
  279. * thread-pool.
  280. */
  281. if (session->keytype == QCRYPTO_AKCIPHER_KEY_TYPE_PRIVATE) {
  282. if (qcrypto_akcipher_export_p8info(&session->akcipher_opts,
  283. session->key, session->keylen,
  284. &p8info, &p8info_len,
  285. &local_error) != 0 ||
  286. cryptodev_lkcf_set_op_desc(&session->akcipher_opts, op_desc,
  287. sizeof(op_desc), &local_error) != 0) {
  288. error_report_err(local_error);
  289. } else {
  290. key_id = add_key(KCTL_KEY_TYPE_PKEY, "lkcf-backend-priv-key",
  291. p8info, p8info_len, KCTL_KEY_RING);
  292. }
  293. }
  294. if (key_id < 0) {
  295. if (!qcrypto_akcipher_supports(&session->akcipher_opts)) {
  296. status = -VIRTIO_CRYPTO_NOTSUPP;
  297. goto out;
  298. }
  299. akcipher = qcrypto_akcipher_new(&session->akcipher_opts,
  300. session->keytype,
  301. session->key, session->keylen,
  302. &local_error);
  303. if (!akcipher) {
  304. status = -VIRTIO_CRYPTO_ERR;
  305. goto out;
  306. }
  307. }
  308. asym_op_info = task->op_info->u.asym_op_info;
  309. switch (op_code) {
  310. case VIRTIO_CRYPTO_AKCIPHER_ENCRYPT:
  311. if (key_id >= 0) {
  312. ret = keyctl_pkey_encrypt(key_id, op_desc,
  313. asym_op_info->src, asym_op_info->src_len,
  314. asym_op_info->dst, asym_op_info->dst_len);
  315. } else {
  316. ret = qcrypto_akcipher_encrypt(akcipher,
  317. asym_op_info->src, asym_op_info->src_len,
  318. asym_op_info->dst, asym_op_info->dst_len, &local_error);
  319. }
  320. break;
  321. case VIRTIO_CRYPTO_AKCIPHER_DECRYPT:
  322. if (key_id >= 0) {
  323. ret = keyctl_pkey_decrypt(key_id, op_desc,
  324. asym_op_info->src, asym_op_info->src_len,
  325. asym_op_info->dst, asym_op_info->dst_len);
  326. } else {
  327. ret = qcrypto_akcipher_decrypt(akcipher,
  328. asym_op_info->src, asym_op_info->src_len,
  329. asym_op_info->dst, asym_op_info->dst_len, &local_error);
  330. }
  331. break;
  332. case VIRTIO_CRYPTO_AKCIPHER_SIGN:
  333. if (key_id >= 0) {
  334. ret = keyctl_pkey_sign(key_id, op_desc,
  335. asym_op_info->src, asym_op_info->src_len,
  336. asym_op_info->dst, asym_op_info->dst_len);
  337. } else {
  338. ret = qcrypto_akcipher_sign(akcipher,
  339. asym_op_info->src, asym_op_info->src_len,
  340. asym_op_info->dst, asym_op_info->dst_len, &local_error);
  341. }
  342. break;
  343. case VIRTIO_CRYPTO_AKCIPHER_VERIFY:
  344. if (key_id >= 0) {
  345. ret = keyctl_pkey_verify(key_id, op_desc,
  346. asym_op_info->src, asym_op_info->src_len,
  347. asym_op_info->dst, asym_op_info->dst_len);
  348. } else {
  349. ret = qcrypto_akcipher_verify(akcipher,
  350. asym_op_info->src, asym_op_info->src_len,
  351. asym_op_info->dst, asym_op_info->dst_len, &local_error);
  352. }
  353. break;
  354. default:
  355. error_setg(&local_error, "Unknown opcode: %u", op_code);
  356. status = -VIRTIO_CRYPTO_ERR;
  357. goto out;
  358. }
  359. if (ret < 0) {
  360. if (!local_error) {
  361. if (errno != EKEYREJECTED) {
  362. error_report("Failed do operation with keyctl: %d", errno);
  363. }
  364. } else {
  365. error_report_err(local_error);
  366. }
  367. status = op_code == VIRTIO_CRYPTO_AKCIPHER_VERIFY ?
  368. -VIRTIO_CRYPTO_KEY_REJECTED : -VIRTIO_CRYPTO_ERR;
  369. } else {
  370. status = VIRTIO_CRYPTO_OK;
  371. asym_op_info->dst_len = ret;
  372. }
  373. out:
  374. if (key_id >= 0) {
  375. keyctl_unlink(key_id, KCTL_KEY_RING);
  376. }
  377. task->status = status;
  378. qemu_mutex_lock(&task->lkcf->rsp_mutex);
  379. if (QSIMPLEQ_EMPTY(&task->lkcf->responses)) {
  380. kick = true;
  381. }
  382. QSIMPLEQ_INSERT_TAIL(&task->lkcf->responses, task, queue);
  383. qemu_mutex_unlock(&task->lkcf->rsp_mutex);
  384. if (kick) {
  385. eventfd_write(task->lkcf->eventfd, 1);
  386. }
  387. }
  388. static void *cryptodev_lkcf_worker(void *arg)
  389. {
  390. CryptoDevBackendLKCF *backend = (CryptoDevBackendLKCF *)arg;
  391. CryptoDevLKCFTask *task;
  392. for (;;) {
  393. task = NULL;
  394. qemu_mutex_lock(&backend->mutex);
  395. while (backend->running && QSIMPLEQ_EMPTY(&backend->requests)) {
  396. qemu_cond_wait(&backend->cond, &backend->mutex);
  397. }
  398. if (backend->running) {
  399. task = QSIMPLEQ_FIRST(&backend->requests);
  400. QSIMPLEQ_REMOVE_HEAD(&backend->requests, queue);
  401. }
  402. qemu_mutex_unlock(&backend->mutex);
  403. /* stopped */
  404. if (!task) {
  405. break;
  406. }
  407. cryptodev_lkcf_execute_task(task);
  408. }
  409. return NULL;
  410. }
  411. static int cryptodev_lkcf_operation(
  412. CryptoDevBackend *backend,
  413. CryptoDevBackendOpInfo *op_info)
  414. {
  415. CryptoDevBackendLKCF *lkcf =
  416. CRYPTODEV_BACKEND_LKCF(backend);
  417. CryptoDevBackendLKCFSession *sess;
  418. QCryptodevBackendAlgType algtype = op_info->algtype;
  419. CryptoDevLKCFTask *task;
  420. if (op_info->session_id >= MAX_SESSIONS ||
  421. lkcf->sess[op_info->session_id] == NULL) {
  422. error_report("Cannot find a valid session id: %" PRIu64 "",
  423. op_info->session_id);
  424. return -VIRTIO_CRYPTO_INVSESS;
  425. }
  426. sess = lkcf->sess[op_info->session_id];
  427. if (algtype != QCRYPTODEV_BACKEND_ALG_ASYM) {
  428. error_report("algtype not supported: %u", algtype);
  429. return -VIRTIO_CRYPTO_NOTSUPP;
  430. }
  431. task = g_new0(CryptoDevLKCFTask, 1);
  432. task->op_info = op_info;
  433. task->cb = op_info->cb;
  434. task->opaque = op_info->opaque;
  435. task->sess = sess;
  436. task->lkcf = lkcf;
  437. task->status = -VIRTIO_CRYPTO_ERR;
  438. qemu_mutex_lock(&lkcf->mutex);
  439. QSIMPLEQ_INSERT_TAIL(&lkcf->requests, task, queue);
  440. qemu_mutex_unlock(&lkcf->mutex);
  441. qemu_cond_signal(&lkcf->cond);
  442. return VIRTIO_CRYPTO_OK;
  443. }
  444. static int cryptodev_lkcf_create_asym_session(
  445. CryptoDevBackendLKCF *lkcf,
  446. CryptoDevBackendAsymSessionInfo *sess_info,
  447. uint64_t *session_id)
  448. {
  449. Error *local_error = NULL;
  450. int index;
  451. g_autofree CryptoDevBackendLKCFSession *sess =
  452. g_new0(CryptoDevBackendLKCFSession, 1);
  453. switch (sess_info->algo) {
  454. case VIRTIO_CRYPTO_AKCIPHER_RSA:
  455. sess->akcipher_opts.alg = QCRYPTO_AKCIPHER_ALG_RSA;
  456. if (cryptodev_lkcf_set_rsa_opt(
  457. sess_info->u.rsa.padding_algo, sess_info->u.rsa.hash_algo,
  458. &sess->akcipher_opts.u.rsa, &local_error) != 0) {
  459. error_report_err(local_error);
  460. return -VIRTIO_CRYPTO_ERR;
  461. }
  462. break;
  463. default:
  464. error_report("Unsupported asym alg %u", sess_info->algo);
  465. return -VIRTIO_CRYPTO_NOTSUPP;
  466. }
  467. switch (sess_info->keytype) {
  468. case VIRTIO_CRYPTO_AKCIPHER_KEY_TYPE_PUBLIC:
  469. sess->keytype = QCRYPTO_AKCIPHER_KEY_TYPE_PUBLIC;
  470. break;
  471. case VIRTIO_CRYPTO_AKCIPHER_KEY_TYPE_PRIVATE:
  472. sess->keytype = QCRYPTO_AKCIPHER_KEY_TYPE_PRIVATE;
  473. break;
  474. default:
  475. error_report("Unknown akcipher keytype: %u", sess_info->keytype);
  476. return -VIRTIO_CRYPTO_ERR;
  477. }
  478. index = cryptodev_lkcf_get_unused_session_index(lkcf);
  479. if (index < 0) {
  480. error_report("Total number of sessions created exceeds %u",
  481. MAX_SESSIONS);
  482. return -VIRTIO_CRYPTO_ERR;
  483. }
  484. sess->keylen = sess_info->keylen;
  485. sess->key = g_malloc(sess_info->keylen);
  486. memcpy(sess->key, sess_info->key, sess_info->keylen);
  487. lkcf->sess[index] = g_steal_pointer(&sess);
  488. *session_id = index;
  489. return VIRTIO_CRYPTO_OK;
  490. }
  491. static int cryptodev_lkcf_create_session(
  492. CryptoDevBackend *backend,
  493. CryptoDevBackendSessionInfo *sess_info,
  494. uint32_t queue_index,
  495. CryptoDevCompletionFunc cb,
  496. void *opaque)
  497. {
  498. CryptoDevBackendAsymSessionInfo *asym_sess_info;
  499. CryptoDevBackendLKCF *lkcf =
  500. CRYPTODEV_BACKEND_LKCF(backend);
  501. int ret;
  502. switch (sess_info->op_code) {
  503. case VIRTIO_CRYPTO_AKCIPHER_CREATE_SESSION:
  504. asym_sess_info = &sess_info->u.asym_sess_info;
  505. ret = cryptodev_lkcf_create_asym_session(
  506. lkcf, asym_sess_info, &sess_info->session_id);
  507. break;
  508. default:
  509. ret = -VIRTIO_CRYPTO_NOTSUPP;
  510. error_report("Unsupported opcode: %" PRIu32 "",
  511. sess_info->op_code);
  512. break;
  513. }
  514. if (cb) {
  515. cb(opaque, ret);
  516. }
  517. return 0;
  518. }
  519. static int cryptodev_lkcf_close_session(CryptoDevBackend *backend,
  520. uint64_t session_id,
  521. uint32_t queue_index,
  522. CryptoDevCompletionFunc cb,
  523. void *opaque)
  524. {
  525. CryptoDevBackendLKCF *lkcf = CRYPTODEV_BACKEND_LKCF(backend);
  526. CryptoDevBackendLKCFSession *session;
  527. assert(session_id < MAX_SESSIONS && lkcf->sess[session_id]);
  528. session = lkcf->sess[session_id];
  529. lkcf->sess[session_id] = NULL;
  530. g_free(session->key);
  531. g_free(session);
  532. if (cb) {
  533. cb(opaque, VIRTIO_CRYPTO_OK);
  534. }
  535. return 0;
  536. }
  537. static void cryptodev_lkcf_class_init(ObjectClass *oc, void *data)
  538. {
  539. CryptoDevBackendClass *bc = CRYPTODEV_BACKEND_CLASS(oc);
  540. bc->init = cryptodev_lkcf_init;
  541. bc->cleanup = cryptodev_lkcf_cleanup;
  542. bc->create_session = cryptodev_lkcf_create_session;
  543. bc->close_session = cryptodev_lkcf_close_session;
  544. bc->do_op = cryptodev_lkcf_operation;
  545. }
  546. static const TypeInfo cryptodev_builtin_info = {
  547. .name = TYPE_CRYPTODEV_BACKEND_LKCF,
  548. .parent = TYPE_CRYPTODEV_BACKEND,
  549. .class_init = cryptodev_lkcf_class_init,
  550. .instance_size = sizeof(CryptoDevBackendLKCF),
  551. };
  552. static void cryptodev_lkcf_register_types(void)
  553. {
  554. type_register_static(&cryptodev_builtin_info);
  555. }
  556. type_init(cryptodev_lkcf_register_types);