cryptodev-builtin.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. /*
  2. * QEMU Cryptodev backend for QEMU cipher APIs
  3. *
  4. * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
  5. *
  6. * Authors:
  7. * Gonglei <arei.gonglei@huawei.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 "system/cryptodev.h"
  25. #include "qemu/error-report.h"
  26. #include "qapi/error.h"
  27. #include "standard-headers/linux/virtio_crypto.h"
  28. #include "crypto/cipher.h"
  29. #include "crypto/akcipher.h"
  30. #include "qom/object.h"
  31. /**
  32. * @TYPE_CRYPTODEV_BACKEND_BUILTIN:
  33. * name of backend that uses QEMU cipher API
  34. */
  35. #define TYPE_CRYPTODEV_BACKEND_BUILTIN "cryptodev-backend-builtin"
  36. OBJECT_DECLARE_SIMPLE_TYPE(CryptoDevBackendBuiltin, CRYPTODEV_BACKEND_BUILTIN)
  37. typedef struct CryptoDevBackendBuiltinSession {
  38. QCryptoCipher *cipher;
  39. uint8_t direction; /* encryption or decryption */
  40. uint8_t type; /* cipher? hash? aead? */
  41. QCryptoAkCipher *akcipher;
  42. QTAILQ_ENTRY(CryptoDevBackendBuiltinSession) next;
  43. } CryptoDevBackendBuiltinSession;
  44. /* Max number of symmetric/asymmetric sessions */
  45. #define MAX_NUM_SESSIONS 256
  46. #define CRYPTODEV_BUITLIN_MAX_AUTH_KEY_LEN 512
  47. #define CRYPTODEV_BUITLIN_MAX_CIPHER_KEY_LEN 64
  48. struct CryptoDevBackendBuiltin {
  49. CryptoDevBackend parent_obj;
  50. CryptoDevBackendBuiltinSession *sessions[MAX_NUM_SESSIONS];
  51. };
  52. static void cryptodev_builtin_init_akcipher(CryptoDevBackend *backend)
  53. {
  54. QCryptoAkCipherOptions opts;
  55. opts.alg = QCRYPTO_AK_CIPHER_ALGO_RSA;
  56. opts.u.rsa.padding_alg = QCRYPTO_RSA_PADDING_ALGO_RAW;
  57. if (qcrypto_akcipher_supports(&opts)) {
  58. backend->conf.crypto_services |=
  59. (1u << QCRYPTODEV_BACKEND_SERVICE_TYPE_AKCIPHER);
  60. backend->conf.akcipher_algo = 1u << VIRTIO_CRYPTO_AKCIPHER_RSA;
  61. }
  62. }
  63. static void cryptodev_builtin_init(
  64. CryptoDevBackend *backend, Error **errp)
  65. {
  66. /* Only support one queue */
  67. int queues = backend->conf.peers.queues;
  68. CryptoDevBackendClient *cc;
  69. if (queues != 1) {
  70. error_setg(errp,
  71. "Only support one queue in cryptdov-builtin backend");
  72. return;
  73. }
  74. cc = cryptodev_backend_new_client();
  75. cc->info_str = g_strdup_printf("cryptodev-builtin0");
  76. cc->queue_index = 0;
  77. cc->type = QCRYPTODEV_BACKEND_TYPE_BUILTIN;
  78. backend->conf.peers.ccs[0] = cc;
  79. backend->conf.crypto_services =
  80. 1u << QCRYPTODEV_BACKEND_SERVICE_TYPE_CIPHER |
  81. 1u << QCRYPTODEV_BACKEND_SERVICE_TYPE_HASH |
  82. 1u << QCRYPTODEV_BACKEND_SERVICE_TYPE_MAC;
  83. backend->conf.cipher_algo_l = 1u << VIRTIO_CRYPTO_CIPHER_AES_CBC;
  84. backend->conf.hash_algo = 1u << VIRTIO_CRYPTO_HASH_SHA1;
  85. /*
  86. * Set the Maximum length of crypto request.
  87. * Why this value? Just avoid to overflow when
  88. * memory allocation for each crypto request.
  89. */
  90. backend->conf.max_size = LONG_MAX - sizeof(CryptoDevBackendOpInfo);
  91. backend->conf.max_cipher_key_len = CRYPTODEV_BUITLIN_MAX_CIPHER_KEY_LEN;
  92. backend->conf.max_auth_key_len = CRYPTODEV_BUITLIN_MAX_AUTH_KEY_LEN;
  93. cryptodev_builtin_init_akcipher(backend);
  94. cryptodev_backend_set_ready(backend, true);
  95. }
  96. static int
  97. cryptodev_builtin_get_unused_session_index(
  98. CryptoDevBackendBuiltin *builtin)
  99. {
  100. size_t i;
  101. for (i = 0; i < MAX_NUM_SESSIONS; i++) {
  102. if (builtin->sessions[i] == NULL) {
  103. return i;
  104. }
  105. }
  106. return -1;
  107. }
  108. #define AES_KEYSIZE_128 16
  109. #define AES_KEYSIZE_192 24
  110. #define AES_KEYSIZE_256 32
  111. #define AES_KEYSIZE_128_XTS AES_KEYSIZE_256
  112. #define AES_KEYSIZE_256_XTS 64
  113. static int
  114. cryptodev_builtin_get_aes_algo(uint32_t key_len, int mode, Error **errp)
  115. {
  116. int algo;
  117. if (key_len == AES_KEYSIZE_128) {
  118. algo = QCRYPTO_CIPHER_ALGO_AES_128;
  119. } else if (key_len == AES_KEYSIZE_192) {
  120. algo = QCRYPTO_CIPHER_ALGO_AES_192;
  121. } else if (key_len == AES_KEYSIZE_256) { /* equals AES_KEYSIZE_128_XTS */
  122. if (mode == QCRYPTO_CIPHER_MODE_XTS) {
  123. algo = QCRYPTO_CIPHER_ALGO_AES_128;
  124. } else {
  125. algo = QCRYPTO_CIPHER_ALGO_AES_256;
  126. }
  127. } else if (key_len == AES_KEYSIZE_256_XTS) {
  128. if (mode == QCRYPTO_CIPHER_MODE_XTS) {
  129. algo = QCRYPTO_CIPHER_ALGO_AES_256;
  130. } else {
  131. goto err;
  132. }
  133. } else {
  134. goto err;
  135. }
  136. return algo;
  137. err:
  138. error_setg(errp, "Unsupported key length :%u", key_len);
  139. return -1;
  140. }
  141. static int cryptodev_builtin_get_rsa_hash_algo(
  142. int virtio_rsa_hash, Error **errp)
  143. {
  144. switch (virtio_rsa_hash) {
  145. case VIRTIO_CRYPTO_RSA_MD5:
  146. return QCRYPTO_HASH_ALGO_MD5;
  147. case VIRTIO_CRYPTO_RSA_SHA1:
  148. return QCRYPTO_HASH_ALGO_SHA1;
  149. case VIRTIO_CRYPTO_RSA_SHA256:
  150. return QCRYPTO_HASH_ALGO_SHA256;
  151. case VIRTIO_CRYPTO_RSA_SHA512:
  152. return QCRYPTO_HASH_ALGO_SHA512;
  153. default:
  154. error_setg(errp, "Unsupported rsa hash algo: %d", virtio_rsa_hash);
  155. return -1;
  156. }
  157. }
  158. static int cryptodev_builtin_set_rsa_options(
  159. int virtio_padding_algo,
  160. int virtio_hash_algo,
  161. QCryptoAkCipherOptionsRSA *opt,
  162. Error **errp)
  163. {
  164. if (virtio_padding_algo == VIRTIO_CRYPTO_RSA_PKCS1_PADDING) {
  165. int hash_alg;
  166. hash_alg = cryptodev_builtin_get_rsa_hash_algo(virtio_hash_algo, errp);
  167. if (hash_alg < 0) {
  168. return -1;
  169. }
  170. opt->hash_alg = hash_alg;
  171. opt->padding_alg = QCRYPTO_RSA_PADDING_ALGO_PKCS1;
  172. return 0;
  173. }
  174. if (virtio_padding_algo == VIRTIO_CRYPTO_RSA_RAW_PADDING) {
  175. opt->padding_alg = QCRYPTO_RSA_PADDING_ALGO_RAW;
  176. return 0;
  177. }
  178. error_setg(errp, "Unsupported rsa padding algo: %d", virtio_padding_algo);
  179. return -1;
  180. }
  181. static int cryptodev_builtin_create_cipher_session(
  182. CryptoDevBackendBuiltin *builtin,
  183. CryptoDevBackendSymSessionInfo *sess_info,
  184. Error **errp)
  185. {
  186. int algo;
  187. int mode;
  188. QCryptoCipher *cipher;
  189. int index;
  190. CryptoDevBackendBuiltinSession *sess;
  191. if (sess_info->op_type != VIRTIO_CRYPTO_SYM_OP_CIPHER) {
  192. error_setg(errp, "Unsupported optype :%u", sess_info->op_type);
  193. return -1;
  194. }
  195. index = cryptodev_builtin_get_unused_session_index(builtin);
  196. if (index < 0) {
  197. error_setg(errp, "Total number of sessions created exceeds %u",
  198. MAX_NUM_SESSIONS);
  199. return -1;
  200. }
  201. switch (sess_info->cipher_alg) {
  202. case VIRTIO_CRYPTO_CIPHER_AES_ECB:
  203. mode = QCRYPTO_CIPHER_MODE_ECB;
  204. algo = cryptodev_builtin_get_aes_algo(sess_info->key_len,
  205. mode, errp);
  206. if (algo < 0) {
  207. return -1;
  208. }
  209. break;
  210. case VIRTIO_CRYPTO_CIPHER_AES_CBC:
  211. mode = QCRYPTO_CIPHER_MODE_CBC;
  212. algo = cryptodev_builtin_get_aes_algo(sess_info->key_len,
  213. mode, errp);
  214. if (algo < 0) {
  215. return -1;
  216. }
  217. break;
  218. case VIRTIO_CRYPTO_CIPHER_AES_CTR:
  219. mode = QCRYPTO_CIPHER_MODE_CTR;
  220. algo = cryptodev_builtin_get_aes_algo(sess_info->key_len,
  221. mode, errp);
  222. if (algo < 0) {
  223. return -1;
  224. }
  225. break;
  226. case VIRTIO_CRYPTO_CIPHER_AES_XTS:
  227. mode = QCRYPTO_CIPHER_MODE_XTS;
  228. algo = cryptodev_builtin_get_aes_algo(sess_info->key_len,
  229. mode, errp);
  230. if (algo < 0) {
  231. return -1;
  232. }
  233. break;
  234. case VIRTIO_CRYPTO_CIPHER_3DES_ECB:
  235. mode = QCRYPTO_CIPHER_MODE_ECB;
  236. algo = QCRYPTO_CIPHER_ALGO_3DES;
  237. break;
  238. case VIRTIO_CRYPTO_CIPHER_3DES_CBC:
  239. mode = QCRYPTO_CIPHER_MODE_CBC;
  240. algo = QCRYPTO_CIPHER_ALGO_3DES;
  241. break;
  242. case VIRTIO_CRYPTO_CIPHER_3DES_CTR:
  243. mode = QCRYPTO_CIPHER_MODE_CTR;
  244. algo = QCRYPTO_CIPHER_ALGO_3DES;
  245. break;
  246. default:
  247. error_setg(errp, "Unsupported cipher alg :%u",
  248. sess_info->cipher_alg);
  249. return -1;
  250. }
  251. cipher = qcrypto_cipher_new(algo, mode,
  252. sess_info->cipher_key,
  253. sess_info->key_len,
  254. errp);
  255. if (!cipher) {
  256. return -1;
  257. }
  258. sess = g_new0(CryptoDevBackendBuiltinSession, 1);
  259. sess->cipher = cipher;
  260. sess->direction = sess_info->direction;
  261. sess->type = sess_info->op_type;
  262. builtin->sessions[index] = sess;
  263. return index;
  264. }
  265. static int cryptodev_builtin_create_akcipher_session(
  266. CryptoDevBackendBuiltin *builtin,
  267. CryptoDevBackendAsymSessionInfo *sess_info,
  268. Error **errp)
  269. {
  270. CryptoDevBackendBuiltinSession *sess;
  271. QCryptoAkCipher *akcipher;
  272. int index;
  273. QCryptoAkCipherKeyType type;
  274. QCryptoAkCipherOptions opts;
  275. switch (sess_info->algo) {
  276. case VIRTIO_CRYPTO_AKCIPHER_RSA:
  277. opts.alg = QCRYPTO_AK_CIPHER_ALGO_RSA;
  278. if (cryptodev_builtin_set_rsa_options(sess_info->u.rsa.padding_algo,
  279. sess_info->u.rsa.hash_algo, &opts.u.rsa, errp) != 0) {
  280. return -1;
  281. }
  282. break;
  283. /* TODO support DSA&ECDSA until qemu crypto framework support these */
  284. default:
  285. error_setg(errp, "Unsupported akcipher alg %u", sess_info->algo);
  286. return -1;
  287. }
  288. switch (sess_info->keytype) {
  289. case VIRTIO_CRYPTO_AKCIPHER_KEY_TYPE_PUBLIC:
  290. type = QCRYPTO_AK_CIPHER_KEY_TYPE_PUBLIC;
  291. break;
  292. case VIRTIO_CRYPTO_AKCIPHER_KEY_TYPE_PRIVATE:
  293. type = QCRYPTO_AK_CIPHER_KEY_TYPE_PRIVATE;
  294. break;
  295. default:
  296. error_setg(errp, "Unsupported akcipher keytype %u", sess_info->keytype);
  297. return -1;
  298. }
  299. index = cryptodev_builtin_get_unused_session_index(builtin);
  300. if (index < 0) {
  301. error_setg(errp, "Total number of sessions created exceeds %u",
  302. MAX_NUM_SESSIONS);
  303. return -1;
  304. }
  305. akcipher = qcrypto_akcipher_new(&opts, type, sess_info->key,
  306. sess_info->keylen, errp);
  307. if (!akcipher) {
  308. return -1;
  309. }
  310. sess = g_new0(CryptoDevBackendBuiltinSession, 1);
  311. sess->akcipher = akcipher;
  312. builtin->sessions[index] = sess;
  313. return index;
  314. }
  315. static int cryptodev_builtin_create_session(
  316. CryptoDevBackend *backend,
  317. CryptoDevBackendSessionInfo *sess_info,
  318. uint32_t queue_index,
  319. CryptoDevCompletionFunc cb,
  320. void *opaque)
  321. {
  322. CryptoDevBackendBuiltin *builtin =
  323. CRYPTODEV_BACKEND_BUILTIN(backend);
  324. CryptoDevBackendSymSessionInfo *sym_sess_info;
  325. CryptoDevBackendAsymSessionInfo *asym_sess_info;
  326. int ret, status;
  327. Error *local_error = NULL;
  328. switch (sess_info->op_code) {
  329. case VIRTIO_CRYPTO_CIPHER_CREATE_SESSION:
  330. sym_sess_info = &sess_info->u.sym_sess_info;
  331. ret = cryptodev_builtin_create_cipher_session(
  332. builtin, sym_sess_info, &local_error);
  333. break;
  334. case VIRTIO_CRYPTO_AKCIPHER_CREATE_SESSION:
  335. asym_sess_info = &sess_info->u.asym_sess_info;
  336. ret = cryptodev_builtin_create_akcipher_session(
  337. builtin, asym_sess_info, &local_error);
  338. break;
  339. case VIRTIO_CRYPTO_HASH_CREATE_SESSION:
  340. case VIRTIO_CRYPTO_MAC_CREATE_SESSION:
  341. default:
  342. error_report("Unsupported opcode :%" PRIu32 "",
  343. sess_info->op_code);
  344. return -VIRTIO_CRYPTO_NOTSUPP;
  345. }
  346. if (local_error) {
  347. error_report_err(local_error);
  348. }
  349. if (ret < 0) {
  350. status = -VIRTIO_CRYPTO_ERR;
  351. } else {
  352. sess_info->session_id = ret;
  353. status = VIRTIO_CRYPTO_OK;
  354. }
  355. if (cb) {
  356. cb(opaque, status);
  357. }
  358. return 0;
  359. }
  360. static int cryptodev_builtin_close_session(
  361. CryptoDevBackend *backend,
  362. uint64_t session_id,
  363. uint32_t queue_index,
  364. CryptoDevCompletionFunc cb,
  365. void *opaque)
  366. {
  367. CryptoDevBackendBuiltin *builtin =
  368. CRYPTODEV_BACKEND_BUILTIN(backend);
  369. CryptoDevBackendBuiltinSession *session;
  370. if (session_id >= MAX_NUM_SESSIONS || !builtin->sessions[session_id]) {
  371. return -VIRTIO_CRYPTO_INVSESS;
  372. }
  373. session = builtin->sessions[session_id];
  374. if (session->cipher) {
  375. qcrypto_cipher_free(session->cipher);
  376. } else if (session->akcipher) {
  377. qcrypto_akcipher_free(session->akcipher);
  378. }
  379. g_free(session);
  380. builtin->sessions[session_id] = NULL;
  381. if (cb) {
  382. cb(opaque, VIRTIO_CRYPTO_OK);
  383. }
  384. return 0;
  385. }
  386. static int cryptodev_builtin_sym_operation(
  387. CryptoDevBackendBuiltinSession *sess,
  388. CryptoDevBackendSymOpInfo *op_info, Error **errp)
  389. {
  390. int ret;
  391. if (op_info->op_type == VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING) {
  392. error_setg(errp,
  393. "Algorithm chain is unsupported for cryptdoev-builtin");
  394. return -VIRTIO_CRYPTO_NOTSUPP;
  395. }
  396. if (op_info->iv_len > 0) {
  397. ret = qcrypto_cipher_setiv(sess->cipher, op_info->iv,
  398. op_info->iv_len, errp);
  399. if (ret < 0) {
  400. return -VIRTIO_CRYPTO_ERR;
  401. }
  402. }
  403. if (sess->direction == VIRTIO_CRYPTO_OP_ENCRYPT) {
  404. ret = qcrypto_cipher_encrypt(sess->cipher, op_info->src,
  405. op_info->dst, op_info->src_len, errp);
  406. if (ret < 0) {
  407. return -VIRTIO_CRYPTO_ERR;
  408. }
  409. } else {
  410. ret = qcrypto_cipher_decrypt(sess->cipher, op_info->src,
  411. op_info->dst, op_info->src_len, errp);
  412. if (ret < 0) {
  413. return -VIRTIO_CRYPTO_ERR;
  414. }
  415. }
  416. return VIRTIO_CRYPTO_OK;
  417. }
  418. static int cryptodev_builtin_asym_operation(
  419. CryptoDevBackendBuiltinSession *sess, uint32_t op_code,
  420. CryptoDevBackendAsymOpInfo *op_info, Error **errp)
  421. {
  422. int ret;
  423. switch (op_code) {
  424. case VIRTIO_CRYPTO_AKCIPHER_ENCRYPT:
  425. ret = qcrypto_akcipher_encrypt(sess->akcipher,
  426. op_info->src, op_info->src_len,
  427. op_info->dst, op_info->dst_len, errp);
  428. break;
  429. case VIRTIO_CRYPTO_AKCIPHER_DECRYPT:
  430. ret = qcrypto_akcipher_decrypt(sess->akcipher,
  431. op_info->src, op_info->src_len,
  432. op_info->dst, op_info->dst_len, errp);
  433. break;
  434. case VIRTIO_CRYPTO_AKCIPHER_SIGN:
  435. ret = qcrypto_akcipher_sign(sess->akcipher,
  436. op_info->src, op_info->src_len,
  437. op_info->dst, op_info->dst_len, errp);
  438. break;
  439. case VIRTIO_CRYPTO_AKCIPHER_VERIFY:
  440. ret = qcrypto_akcipher_verify(sess->akcipher,
  441. op_info->src, op_info->src_len,
  442. op_info->dst, op_info->dst_len, errp);
  443. break;
  444. default:
  445. return -VIRTIO_CRYPTO_ERR;
  446. }
  447. if (ret < 0) {
  448. if (op_code == VIRTIO_CRYPTO_AKCIPHER_VERIFY) {
  449. return -VIRTIO_CRYPTO_KEY_REJECTED;
  450. }
  451. return -VIRTIO_CRYPTO_ERR;
  452. }
  453. /* Buffer is too short, typically the driver should handle this case */
  454. if (unlikely(ret > op_info->dst_len)) {
  455. if (errp && !*errp) {
  456. error_setg(errp, "dst buffer too short");
  457. }
  458. return -VIRTIO_CRYPTO_ERR;
  459. }
  460. op_info->dst_len = ret;
  461. return VIRTIO_CRYPTO_OK;
  462. }
  463. static int cryptodev_builtin_operation(
  464. CryptoDevBackend *backend,
  465. CryptoDevBackendOpInfo *op_info)
  466. {
  467. CryptoDevBackendBuiltin *builtin =
  468. CRYPTODEV_BACKEND_BUILTIN(backend);
  469. CryptoDevBackendBuiltinSession *sess;
  470. CryptoDevBackendSymOpInfo *sym_op_info;
  471. CryptoDevBackendAsymOpInfo *asym_op_info;
  472. QCryptodevBackendAlgoType algtype = op_info->algtype;
  473. int status = -VIRTIO_CRYPTO_ERR;
  474. Error *local_error = NULL;
  475. if (op_info->session_id >= MAX_NUM_SESSIONS ||
  476. builtin->sessions[op_info->session_id] == NULL) {
  477. error_report("Cannot find a valid session id: %" PRIu64 "",
  478. op_info->session_id);
  479. return -VIRTIO_CRYPTO_INVSESS;
  480. }
  481. sess = builtin->sessions[op_info->session_id];
  482. if (algtype == QCRYPTODEV_BACKEND_ALGO_TYPE_SYM) {
  483. sym_op_info = op_info->u.sym_op_info;
  484. status = cryptodev_builtin_sym_operation(sess, sym_op_info,
  485. &local_error);
  486. } else if (algtype == QCRYPTODEV_BACKEND_ALGO_TYPE_ASYM) {
  487. asym_op_info = op_info->u.asym_op_info;
  488. status = cryptodev_builtin_asym_operation(sess, op_info->op_code,
  489. asym_op_info, &local_error);
  490. }
  491. if (local_error) {
  492. error_report_err(local_error);
  493. }
  494. if (op_info->cb) {
  495. op_info->cb(op_info->opaque, status);
  496. }
  497. return 0;
  498. }
  499. static void cryptodev_builtin_cleanup(
  500. CryptoDevBackend *backend,
  501. Error **errp)
  502. {
  503. CryptoDevBackendBuiltin *builtin =
  504. CRYPTODEV_BACKEND_BUILTIN(backend);
  505. size_t i;
  506. int queues = backend->conf.peers.queues;
  507. CryptoDevBackendClient *cc;
  508. for (i = 0; i < MAX_NUM_SESSIONS; i++) {
  509. if (builtin->sessions[i] != NULL) {
  510. cryptodev_builtin_close_session(backend, i, 0, NULL, NULL);
  511. }
  512. }
  513. for (i = 0; i < queues; i++) {
  514. cc = backend->conf.peers.ccs[i];
  515. if (cc) {
  516. cryptodev_backend_free_client(cc);
  517. backend->conf.peers.ccs[i] = NULL;
  518. }
  519. }
  520. cryptodev_backend_set_ready(backend, false);
  521. }
  522. static void
  523. cryptodev_builtin_class_init(ObjectClass *oc, void *data)
  524. {
  525. CryptoDevBackendClass *bc = CRYPTODEV_BACKEND_CLASS(oc);
  526. bc->init = cryptodev_builtin_init;
  527. bc->cleanup = cryptodev_builtin_cleanup;
  528. bc->create_session = cryptodev_builtin_create_session;
  529. bc->close_session = cryptodev_builtin_close_session;
  530. bc->do_op = cryptodev_builtin_operation;
  531. }
  532. static const TypeInfo cryptodev_builtin_info = {
  533. .name = TYPE_CRYPTODEV_BACKEND_BUILTIN,
  534. .parent = TYPE_CRYPTODEV_BACKEND,
  535. .class_init = cryptodev_builtin_class_init,
  536. .instance_size = sizeof(CryptoDevBackendBuiltin),
  537. };
  538. static void
  539. cryptodev_builtin_register_types(void)
  540. {
  541. type_register_static(&cryptodev_builtin_info);
  542. }
  543. type_init(cryptodev_builtin_register_types);