2
0

cryptodev-builtin.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 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 "sysemu/cryptodev.h"
  25. #include "qapi/error.h"
  26. #include "standard-headers/linux/virtio_crypto.h"
  27. #include "crypto/cipher.h"
  28. /**
  29. * @TYPE_CRYPTODEV_BACKEND_BUILTIN:
  30. * name of backend that uses QEMU cipher API
  31. */
  32. #define TYPE_CRYPTODEV_BACKEND_BUILTIN "cryptodev-backend-builtin"
  33. #define CRYPTODEV_BACKEND_BUILTIN(obj) \
  34. OBJECT_CHECK(CryptoDevBackendBuiltin, \
  35. (obj), TYPE_CRYPTODEV_BACKEND_BUILTIN)
  36. typedef struct CryptoDevBackendBuiltin
  37. CryptoDevBackendBuiltin;
  38. typedef struct CryptoDevBackendBuiltinSession {
  39. QCryptoCipher *cipher;
  40. uint8_t direction; /* encryption or decryption */
  41. uint8_t type; /* cipher? hash? aead? */
  42. QTAILQ_ENTRY(CryptoDevBackendBuiltinSession) next;
  43. } CryptoDevBackendBuiltinSession;
  44. /* Max number of symmetric 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(
  53. CryptoDevBackend *backend, Error **errp)
  54. {
  55. /* Only support one queue */
  56. int queues = backend->conf.peers.queues;
  57. CryptoDevBackendClient *cc;
  58. if (queues != 1) {
  59. error_setg(errp,
  60. "Only support one queue in cryptdov-builtin backend");
  61. return;
  62. }
  63. cc = cryptodev_backend_new_client(
  64. "cryptodev-builtin", NULL);
  65. cc->info_str = g_strdup_printf("cryptodev-builtin0");
  66. cc->queue_index = 0;
  67. cc->type = CRYPTODEV_BACKEND_TYPE_BUILTIN;
  68. backend->conf.peers.ccs[0] = cc;
  69. backend->conf.crypto_services =
  70. 1u << VIRTIO_CRYPTO_SERVICE_CIPHER |
  71. 1u << VIRTIO_CRYPTO_SERVICE_HASH |
  72. 1u << VIRTIO_CRYPTO_SERVICE_MAC;
  73. backend->conf.cipher_algo_l = 1u << VIRTIO_CRYPTO_CIPHER_AES_CBC;
  74. backend->conf.hash_algo = 1u << VIRTIO_CRYPTO_HASH_SHA1;
  75. /*
  76. * Set the Maximum length of crypto request.
  77. * Why this value? Just avoid to overflow when
  78. * memory allocation for each crypto request.
  79. */
  80. backend->conf.max_size = LONG_MAX - sizeof(CryptoDevBackendSymOpInfo);
  81. backend->conf.max_cipher_key_len = CRYPTODEV_BUITLIN_MAX_CIPHER_KEY_LEN;
  82. backend->conf.max_auth_key_len = CRYPTODEV_BUITLIN_MAX_AUTH_KEY_LEN;
  83. cryptodev_backend_set_ready(backend, true);
  84. }
  85. static int
  86. cryptodev_builtin_get_unused_session_index(
  87. CryptoDevBackendBuiltin *builtin)
  88. {
  89. size_t i;
  90. for (i = 0; i < MAX_NUM_SESSIONS; i++) {
  91. if (builtin->sessions[i] == NULL) {
  92. return i;
  93. }
  94. }
  95. return -1;
  96. }
  97. #define AES_KEYSIZE_128 16
  98. #define AES_KEYSIZE_192 24
  99. #define AES_KEYSIZE_256 32
  100. #define AES_KEYSIZE_128_XTS AES_KEYSIZE_256
  101. #define AES_KEYSIZE_256_XTS 64
  102. static int
  103. cryptodev_builtin_get_aes_algo(uint32_t key_len, int mode, Error **errp)
  104. {
  105. int algo;
  106. if (key_len == AES_KEYSIZE_128) {
  107. algo = QCRYPTO_CIPHER_ALG_AES_128;
  108. } else if (key_len == AES_KEYSIZE_192) {
  109. algo = QCRYPTO_CIPHER_ALG_AES_192;
  110. } else if (key_len == AES_KEYSIZE_256) { /* equals AES_KEYSIZE_128_XTS */
  111. if (mode == QCRYPTO_CIPHER_MODE_XTS) {
  112. algo = QCRYPTO_CIPHER_ALG_AES_128;
  113. } else {
  114. algo = QCRYPTO_CIPHER_ALG_AES_256;
  115. }
  116. } else if (key_len == AES_KEYSIZE_256_XTS) {
  117. if (mode == QCRYPTO_CIPHER_MODE_XTS) {
  118. algo = QCRYPTO_CIPHER_ALG_AES_256;
  119. } else {
  120. goto err;
  121. }
  122. } else {
  123. goto err;
  124. }
  125. return algo;
  126. err:
  127. error_setg(errp, "Unsupported key length :%u", key_len);
  128. return -1;
  129. }
  130. static int cryptodev_builtin_create_cipher_session(
  131. CryptoDevBackendBuiltin *builtin,
  132. CryptoDevBackendSymSessionInfo *sess_info,
  133. Error **errp)
  134. {
  135. int algo;
  136. int mode;
  137. QCryptoCipher *cipher;
  138. int index;
  139. CryptoDevBackendBuiltinSession *sess;
  140. if (sess_info->op_type != VIRTIO_CRYPTO_SYM_OP_CIPHER) {
  141. error_setg(errp, "Unsupported optype :%u", sess_info->op_type);
  142. return -1;
  143. }
  144. index = cryptodev_builtin_get_unused_session_index(builtin);
  145. if (index < 0) {
  146. error_setg(errp, "Total number of sessions created exceeds %u",
  147. MAX_NUM_SESSIONS);
  148. return -1;
  149. }
  150. switch (sess_info->cipher_alg) {
  151. case VIRTIO_CRYPTO_CIPHER_AES_ECB:
  152. mode = QCRYPTO_CIPHER_MODE_ECB;
  153. algo = cryptodev_builtin_get_aes_algo(sess_info->key_len,
  154. mode, errp);
  155. if (algo < 0) {
  156. return -1;
  157. }
  158. break;
  159. case VIRTIO_CRYPTO_CIPHER_AES_CBC:
  160. mode = QCRYPTO_CIPHER_MODE_CBC;
  161. algo = cryptodev_builtin_get_aes_algo(sess_info->key_len,
  162. mode, errp);
  163. if (algo < 0) {
  164. return -1;
  165. }
  166. break;
  167. case VIRTIO_CRYPTO_CIPHER_AES_CTR:
  168. mode = QCRYPTO_CIPHER_MODE_CTR;
  169. algo = cryptodev_builtin_get_aes_algo(sess_info->key_len,
  170. mode, errp);
  171. if (algo < 0) {
  172. return -1;
  173. }
  174. break;
  175. case VIRTIO_CRYPTO_CIPHER_AES_XTS:
  176. mode = QCRYPTO_CIPHER_MODE_XTS;
  177. algo = cryptodev_builtin_get_aes_algo(sess_info->key_len,
  178. mode, errp);
  179. if (algo < 0) {
  180. return -1;
  181. }
  182. break;
  183. case VIRTIO_CRYPTO_CIPHER_3DES_ECB:
  184. mode = QCRYPTO_CIPHER_MODE_ECB;
  185. algo = QCRYPTO_CIPHER_ALG_3DES;
  186. break;
  187. case VIRTIO_CRYPTO_CIPHER_3DES_CBC:
  188. mode = QCRYPTO_CIPHER_MODE_CBC;
  189. algo = QCRYPTO_CIPHER_ALG_3DES;
  190. break;
  191. case VIRTIO_CRYPTO_CIPHER_3DES_CTR:
  192. mode = QCRYPTO_CIPHER_MODE_CTR;
  193. algo = QCRYPTO_CIPHER_ALG_3DES;
  194. break;
  195. default:
  196. error_setg(errp, "Unsupported cipher alg :%u",
  197. sess_info->cipher_alg);
  198. return -1;
  199. }
  200. cipher = qcrypto_cipher_new(algo, mode,
  201. sess_info->cipher_key,
  202. sess_info->key_len,
  203. errp);
  204. if (!cipher) {
  205. return -1;
  206. }
  207. sess = g_new0(CryptoDevBackendBuiltinSession, 1);
  208. sess->cipher = cipher;
  209. sess->direction = sess_info->direction;
  210. sess->type = sess_info->op_type;
  211. builtin->sessions[index] = sess;
  212. return index;
  213. }
  214. static int64_t cryptodev_builtin_sym_create_session(
  215. CryptoDevBackend *backend,
  216. CryptoDevBackendSymSessionInfo *sess_info,
  217. uint32_t queue_index, Error **errp)
  218. {
  219. CryptoDevBackendBuiltin *builtin =
  220. CRYPTODEV_BACKEND_BUILTIN(backend);
  221. int64_t session_id = -1;
  222. int ret;
  223. switch (sess_info->op_code) {
  224. case VIRTIO_CRYPTO_CIPHER_CREATE_SESSION:
  225. ret = cryptodev_builtin_create_cipher_session(
  226. builtin, sess_info, errp);
  227. if (ret < 0) {
  228. return ret;
  229. } else {
  230. session_id = ret;
  231. }
  232. break;
  233. case VIRTIO_CRYPTO_HASH_CREATE_SESSION:
  234. case VIRTIO_CRYPTO_MAC_CREATE_SESSION:
  235. default:
  236. error_setg(errp, "Unsupported opcode :%" PRIu32 "",
  237. sess_info->op_code);
  238. return -1;
  239. }
  240. return session_id;
  241. }
  242. static int cryptodev_builtin_sym_close_session(
  243. CryptoDevBackend *backend,
  244. uint64_t session_id,
  245. uint32_t queue_index, Error **errp)
  246. {
  247. CryptoDevBackendBuiltin *builtin =
  248. CRYPTODEV_BACKEND_BUILTIN(backend);
  249. assert(session_id < MAX_NUM_SESSIONS && builtin->sessions[session_id]);
  250. qcrypto_cipher_free(builtin->sessions[session_id]->cipher);
  251. g_free(builtin->sessions[session_id]);
  252. builtin->sessions[session_id] = NULL;
  253. return 0;
  254. }
  255. static int cryptodev_builtin_sym_operation(
  256. CryptoDevBackend *backend,
  257. CryptoDevBackendSymOpInfo *op_info,
  258. uint32_t queue_index, Error **errp)
  259. {
  260. CryptoDevBackendBuiltin *builtin =
  261. CRYPTODEV_BACKEND_BUILTIN(backend);
  262. CryptoDevBackendBuiltinSession *sess;
  263. int ret;
  264. if (op_info->session_id >= MAX_NUM_SESSIONS ||
  265. builtin->sessions[op_info->session_id] == NULL) {
  266. error_setg(errp, "Cannot find a valid session id: %" PRIu64 "",
  267. op_info->session_id);
  268. return -VIRTIO_CRYPTO_INVSESS;
  269. }
  270. if (op_info->op_type == VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING) {
  271. error_setg(errp,
  272. "Algorithm chain is unsupported for cryptdoev-builtin");
  273. return -VIRTIO_CRYPTO_NOTSUPP;
  274. }
  275. sess = builtin->sessions[op_info->session_id];
  276. if (op_info->iv_len > 0) {
  277. ret = qcrypto_cipher_setiv(sess->cipher, op_info->iv,
  278. op_info->iv_len, errp);
  279. if (ret < 0) {
  280. return -VIRTIO_CRYPTO_ERR;
  281. }
  282. }
  283. if (sess->direction == VIRTIO_CRYPTO_OP_ENCRYPT) {
  284. ret = qcrypto_cipher_encrypt(sess->cipher, op_info->src,
  285. op_info->dst, op_info->src_len, errp);
  286. if (ret < 0) {
  287. return -VIRTIO_CRYPTO_ERR;
  288. }
  289. } else {
  290. ret = qcrypto_cipher_decrypt(sess->cipher, op_info->src,
  291. op_info->dst, op_info->src_len, errp);
  292. if (ret < 0) {
  293. return -VIRTIO_CRYPTO_ERR;
  294. }
  295. }
  296. return VIRTIO_CRYPTO_OK;
  297. }
  298. static void cryptodev_builtin_cleanup(
  299. CryptoDevBackend *backend,
  300. Error **errp)
  301. {
  302. CryptoDevBackendBuiltin *builtin =
  303. CRYPTODEV_BACKEND_BUILTIN(backend);
  304. size_t i;
  305. int queues = backend->conf.peers.queues;
  306. CryptoDevBackendClient *cc;
  307. for (i = 0; i < MAX_NUM_SESSIONS; i++) {
  308. if (builtin->sessions[i] != NULL) {
  309. cryptodev_builtin_sym_close_session(backend, i, 0, &error_abort);
  310. }
  311. }
  312. for (i = 0; i < queues; i++) {
  313. cc = backend->conf.peers.ccs[i];
  314. if (cc) {
  315. cryptodev_backend_free_client(cc);
  316. backend->conf.peers.ccs[i] = NULL;
  317. }
  318. }
  319. cryptodev_backend_set_ready(backend, false);
  320. }
  321. static void
  322. cryptodev_builtin_class_init(ObjectClass *oc, void *data)
  323. {
  324. CryptoDevBackendClass *bc = CRYPTODEV_BACKEND_CLASS(oc);
  325. bc->init = cryptodev_builtin_init;
  326. bc->cleanup = cryptodev_builtin_cleanup;
  327. bc->create_session = cryptodev_builtin_sym_create_session;
  328. bc->close_session = cryptodev_builtin_sym_close_session;
  329. bc->do_sym_op = cryptodev_builtin_sym_operation;
  330. }
  331. static const TypeInfo cryptodev_builtin_info = {
  332. .name = TYPE_CRYPTODEV_BACKEND_BUILTIN,
  333. .parent = TYPE_CRYPTODEV_BACKEND,
  334. .class_init = cryptodev_builtin_class_init,
  335. .instance_size = sizeof(CryptoDevBackendBuiltin),
  336. };
  337. static void
  338. cryptodev_builtin_register_types(void)
  339. {
  340. type_register_static(&cryptodev_builtin_info);
  341. }
  342. type_init(cryptodev_builtin_register_types);