cryptodev-builtin.c 12 KB

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