PageRenderTime 69ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/include/sysemu/cryptodev.h

https://github.com/mstsirkin/qemu
C Header | 348 lines | 135 code | 31 blank | 182 comment | 0 complexity | 2bd2c9bb67584bd6581027e30f4927ef MD5 | raw file
  1. /*
  2. * QEMU Crypto Device Implementation
  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. #ifndef CRYPTODEV_H
  24. #define CRYPTODEV_H
  25. #include "qom/object.h"
  26. #include "qemu-common.h"
  27. /**
  28. * CryptoDevBackend:
  29. *
  30. * The CryptoDevBackend object is an interface
  31. * for different cryptodev backends, which provides crypto
  32. * operation wrapper.
  33. *
  34. */
  35. #define TYPE_CRYPTODEV_BACKEND "cryptodev-backend"
  36. #define CRYPTODEV_BACKEND(obj) \
  37. OBJECT_CHECK(CryptoDevBackend, \
  38. (obj), TYPE_CRYPTODEV_BACKEND)
  39. #define CRYPTODEV_BACKEND_GET_CLASS(obj) \
  40. OBJECT_GET_CLASS(CryptoDevBackendClass, \
  41. (obj), TYPE_CRYPTODEV_BACKEND)
  42. #define CRYPTODEV_BACKEND_CLASS(klass) \
  43. OBJECT_CLASS_CHECK(CryptoDevBackendClass, \
  44. (klass), TYPE_CRYPTODEV_BACKEND)
  45. #define MAX_CRYPTO_QUEUE_NUM 64
  46. typedef struct CryptoDevBackendConf CryptoDevBackendConf;
  47. typedef struct CryptoDevBackendPeers CryptoDevBackendPeers;
  48. typedef struct CryptoDevBackendClient
  49. CryptoDevBackendClient;
  50. typedef struct CryptoDevBackend CryptoDevBackend;
  51. enum CryptoDevBackendAlgType {
  52. CRYPTODEV_BACKEND_ALG_SYM,
  53. CRYPTODEV_BACKEND_ALG__MAX,
  54. };
  55. /**
  56. * CryptoDevBackendSymSessionInfo:
  57. *
  58. * @op_code: operation code (refer to virtio_crypto.h)
  59. * @cipher_alg: algorithm type of CIPHER
  60. * @key_len: byte length of cipher key
  61. * @hash_alg: algorithm type of HASH/MAC
  62. * @hash_result_len: byte length of HASH operation result
  63. * @auth_key_len: byte length of authenticated key
  64. * @add_len: byte length of additional authenticated data
  65. * @op_type: operation type (refer to virtio_crypto.h)
  66. * @direction: encryption or direction for CIPHER
  67. * @hash_mode: HASH mode for HASH operation (refer to virtio_crypto.h)
  68. * @alg_chain_order: order of algorithm chaining (CIPHER then HASH,
  69. * or HASH then CIPHER)
  70. * @cipher_key: point to a key of CIPHER
  71. * @auth_key: point to an authenticated key of MAC
  72. *
  73. */
  74. typedef struct CryptoDevBackendSymSessionInfo {
  75. /* corresponding with virtio crypto spec */
  76. uint32_t op_code;
  77. uint32_t cipher_alg;
  78. uint32_t key_len;
  79. uint32_t hash_alg;
  80. uint32_t hash_result_len;
  81. uint32_t auth_key_len;
  82. uint32_t add_len;
  83. uint8_t op_type;
  84. uint8_t direction;
  85. uint8_t hash_mode;
  86. uint8_t alg_chain_order;
  87. uint8_t *cipher_key;
  88. uint8_t *auth_key;
  89. } CryptoDevBackendSymSessionInfo;
  90. /**
  91. * CryptoDevBackendSymOpInfo:
  92. *
  93. * @session_id: session index which was previously
  94. * created by cryptodev_backend_sym_create_session()
  95. * @aad_len: byte length of additional authenticated data
  96. * @iv_len: byte length of initialization vector or counter
  97. * @src_len: byte length of source data
  98. * @dst_len: byte length of destination data
  99. * @digest_result_len: byte length of hash digest result
  100. * @hash_start_src_offset: Starting point for hash processing, specified
  101. * as number of bytes from start of packet in source data, only used for
  102. * algorithm chain
  103. * @cipher_start_src_offset: Starting point for cipher processing, specified
  104. * as number of bytes from start of packet in source data, only used for
  105. * algorithm chain
  106. * @len_to_hash: byte length of source data on which the hash
  107. * operation will be computed, only used for algorithm chain
  108. * @len_to_cipher: byte length of source data on which the cipher
  109. * operation will be computed, only used for algorithm chain
  110. * @op_type: operation type (refer to virtio_crypto.h)
  111. * @iv: point to the initialization vector or counter
  112. * @src: point to the source data
  113. * @dst: point to the destination data
  114. * @aad_data: point to the additional authenticated data
  115. * @digest_result: point to the digest result data
  116. * @data[0]: point to the extensional memory by one memory allocation
  117. *
  118. */
  119. typedef struct CryptoDevBackendSymOpInfo {
  120. uint64_t session_id;
  121. uint32_t aad_len;
  122. uint32_t iv_len;
  123. uint32_t src_len;
  124. uint32_t dst_len;
  125. uint32_t digest_result_len;
  126. uint32_t hash_start_src_offset;
  127. uint32_t cipher_start_src_offset;
  128. uint32_t len_to_hash;
  129. uint32_t len_to_cipher;
  130. uint8_t op_type;
  131. uint8_t *iv;
  132. uint8_t *src;
  133. uint8_t *dst;
  134. uint8_t *aad_data;
  135. uint8_t *digest_result;
  136. uint8_t data[0];
  137. } CryptoDevBackendSymOpInfo;
  138. typedef struct CryptoDevBackendClass {
  139. ObjectClass parent_class;
  140. void (*init)(CryptoDevBackend *backend, Error **errp);
  141. void (*cleanup)(CryptoDevBackend *backend, Error **errp);
  142. int64_t (*create_session)(CryptoDevBackend *backend,
  143. CryptoDevBackendSymSessionInfo *sess_info,
  144. uint32_t queue_index, Error **errp);
  145. int (*close_session)(CryptoDevBackend *backend,
  146. uint64_t session_id,
  147. uint32_t queue_index, Error **errp);
  148. int (*do_sym_op)(CryptoDevBackend *backend,
  149. CryptoDevBackendSymOpInfo *op_info,
  150. uint32_t queue_index, Error **errp);
  151. } CryptoDevBackendClass;
  152. typedef enum CryptoDevBackendOptionsType {
  153. CRYPTODEV_BACKEND_TYPE_NONE = 0,
  154. CRYPTODEV_BACKEND_TYPE_BUILTIN = 1,
  155. CRYPTODEV_BACKEND_TYPE_VHOST_USER = 2,
  156. CRYPTODEV_BACKEND_TYPE__MAX,
  157. } CryptoDevBackendOptionsType;
  158. struct CryptoDevBackendClient {
  159. CryptoDevBackendOptionsType type;
  160. char *model;
  161. char *name;
  162. char *info_str;
  163. unsigned int queue_index;
  164. int vring_enable;
  165. QTAILQ_ENTRY(CryptoDevBackendClient) next;
  166. };
  167. struct CryptoDevBackendPeers {
  168. CryptoDevBackendClient *ccs[MAX_CRYPTO_QUEUE_NUM];
  169. uint32_t queues;
  170. };
  171. struct CryptoDevBackendConf {
  172. CryptoDevBackendPeers peers;
  173. /* Supported service mask */
  174. uint32_t crypto_services;
  175. /* Detailed algorithms mask */
  176. uint32_t cipher_algo_l;
  177. uint32_t cipher_algo_h;
  178. uint32_t hash_algo;
  179. uint32_t mac_algo_l;
  180. uint32_t mac_algo_h;
  181. uint32_t aead_algo;
  182. /* Maximum length of cipher key */
  183. uint32_t max_cipher_key_len;
  184. /* Maximum length of authenticated key */
  185. uint32_t max_auth_key_len;
  186. /* Maximum size of each crypto request's content */
  187. uint64_t max_size;
  188. };
  189. struct CryptoDevBackend {
  190. Object parent_obj;
  191. bool ready;
  192. /* Tag the cryptodev backend is used by virtio-crypto or not */
  193. bool is_used;
  194. CryptoDevBackendConf conf;
  195. };
  196. /**
  197. * cryptodev_backend_new_client:
  198. * @model: the cryptodev backend model
  199. * @name: the cryptodev backend name, can be NULL
  200. *
  201. * Creates a new cryptodev backend client object
  202. * with the @name in the model @model.
  203. *
  204. * The returned object must be released with
  205. * cryptodev_backend_free_client() when no
  206. * longer required
  207. *
  208. * Returns: a new cryptodev backend client object
  209. */
  210. CryptoDevBackendClient *
  211. cryptodev_backend_new_client(const char *model,
  212. const char *name);
  213. /**
  214. * cryptodev_backend_free_client:
  215. * @cc: the cryptodev backend client object
  216. *
  217. * Release the memory associated with @cc that
  218. * was previously allocated by cryptodev_backend_new_client()
  219. */
  220. void cryptodev_backend_free_client(
  221. CryptoDevBackendClient *cc);
  222. /**
  223. * cryptodev_backend_cleanup:
  224. * @backend: the cryptodev backend object
  225. * @errp: pointer to a NULL-initialized error object
  226. *
  227. * Clean the resouce associated with @backend that realizaed
  228. * by the specific backend's init() callback
  229. */
  230. void cryptodev_backend_cleanup(
  231. CryptoDevBackend *backend,
  232. Error **errp);
  233. /**
  234. * cryptodev_backend_sym_create_session:
  235. * @backend: the cryptodev backend object
  236. * @sess_info: parameters needed by session creating
  237. * @queue_index: queue index of cryptodev backend client
  238. * @errp: pointer to a NULL-initialized error object
  239. *
  240. * Create a session for symmetric algorithms
  241. *
  242. * Returns: session id on success, or -1 on error
  243. */
  244. int64_t cryptodev_backend_sym_create_session(
  245. CryptoDevBackend *backend,
  246. CryptoDevBackendSymSessionInfo *sess_info,
  247. uint32_t queue_index, Error **errp);
  248. /**
  249. * cryptodev_backend_sym_close_session:
  250. * @backend: the cryptodev backend object
  251. * @session_id: the session id
  252. * @queue_index: queue index of cryptodev backend client
  253. * @errp: pointer to a NULL-initialized error object
  254. *
  255. * Close a session for symmetric algorithms which was previously
  256. * created by cryptodev_backend_sym_create_session()
  257. *
  258. * Returns: 0 on success, or Negative on error
  259. */
  260. int cryptodev_backend_sym_close_session(
  261. CryptoDevBackend *backend,
  262. uint64_t session_id,
  263. uint32_t queue_index, Error **errp);
  264. /**
  265. * cryptodev_backend_crypto_operation:
  266. * @backend: the cryptodev backend object
  267. * @opaque: pointer to a VirtIOCryptoReq object
  268. * @queue_index: queue index of cryptodev backend client
  269. * @errp: pointer to a NULL-initialized error object
  270. *
  271. * Do crypto operation, such as encryption and
  272. * decryption
  273. *
  274. * Returns: VIRTIO_CRYPTO_OK on success,
  275. * or -VIRTIO_CRYPTO_* on error
  276. */
  277. int cryptodev_backend_crypto_operation(
  278. CryptoDevBackend *backend,
  279. void *opaque,
  280. uint32_t queue_index, Error **errp);
  281. /**
  282. * cryptodev_backend_set_used:
  283. * @backend: the cryptodev backend object
  284. * @used: ture or false
  285. *
  286. * Set the cryptodev backend is used by virtio-crypto or not
  287. */
  288. void cryptodev_backend_set_used(CryptoDevBackend *backend, bool used);
  289. /**
  290. * cryptodev_backend_is_used:
  291. * @backend: the cryptodev backend object
  292. *
  293. * Return the status that the cryptodev backend is used
  294. * by virtio-crypto or not
  295. *
  296. * Returns: true on used, or false on not used
  297. */
  298. bool cryptodev_backend_is_used(CryptoDevBackend *backend);
  299. /**
  300. * cryptodev_backend_set_ready:
  301. * @backend: the cryptodev backend object
  302. * @ready: ture or false
  303. *
  304. * Set the cryptodev backend is ready or not, which is called
  305. * by the children of the cryptodev banckend interface.
  306. */
  307. void cryptodev_backend_set_ready(CryptoDevBackend *backend, bool ready);
  308. /**
  309. * cryptodev_backend_is_ready:
  310. * @backend: the cryptodev backend object
  311. *
  312. * Return the status that the cryptodev backend is ready or not
  313. *
  314. * Returns: true on ready, or false on not ready
  315. */
  316. bool cryptodev_backend_is_ready(CryptoDevBackend *backend);
  317. #endif /* CRYPTODEV_H */