cryptodev.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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.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. #ifndef CRYPTODEV_H
  24. #define CRYPTODEV_H
  25. #include "qemu/queue.h"
  26. #include "qemu/throttle.h"
  27. #include "qom/object.h"
  28. #include "qapi/qapi-types-cryptodev.h"
  29. /**
  30. * CryptoDevBackend:
  31. *
  32. * The CryptoDevBackend object is an interface
  33. * for different cryptodev backends, which provides crypto
  34. * operation wrapper.
  35. *
  36. */
  37. #define TYPE_CRYPTODEV_BACKEND "cryptodev-backend"
  38. OBJECT_DECLARE_TYPE(CryptoDevBackend, CryptoDevBackendClass,
  39. CRYPTODEV_BACKEND)
  40. #define MAX_CRYPTO_QUEUE_NUM 64
  41. typedef struct CryptoDevBackendConf CryptoDevBackendConf;
  42. typedef struct CryptoDevBackendPeers CryptoDevBackendPeers;
  43. typedef struct CryptoDevBackendClient
  44. CryptoDevBackendClient;
  45. /**
  46. * CryptoDevBackendSymSessionInfo:
  47. *
  48. * @cipher_alg: algorithm type of CIPHER
  49. * @key_len: byte length of cipher key
  50. * @hash_alg: algorithm type of HASH/MAC
  51. * @hash_result_len: byte length of HASH operation result
  52. * @auth_key_len: byte length of authenticated key
  53. * @add_len: byte length of additional authenticated data
  54. * @op_type: operation type (refer to virtio_crypto.h)
  55. * @direction: encryption or direction for CIPHER
  56. * @hash_mode: HASH mode for HASH operation (refer to virtio_crypto.h)
  57. * @alg_chain_order: order of algorithm chaining (CIPHER then HASH,
  58. * or HASH then CIPHER)
  59. * @cipher_key: point to a key of CIPHER
  60. * @auth_key: point to an authenticated key of MAC
  61. *
  62. */
  63. typedef struct CryptoDevBackendSymSessionInfo {
  64. /* corresponding with virtio crypto spec */
  65. uint32_t cipher_alg;
  66. uint32_t key_len;
  67. uint32_t hash_alg;
  68. uint32_t hash_result_len;
  69. uint32_t auth_key_len;
  70. uint32_t add_len;
  71. uint8_t op_type;
  72. uint8_t direction;
  73. uint8_t hash_mode;
  74. uint8_t alg_chain_order;
  75. uint8_t *cipher_key;
  76. uint8_t *auth_key;
  77. } CryptoDevBackendSymSessionInfo;
  78. /**
  79. * CryptoDevBackendAsymSessionInfo:
  80. */
  81. typedef struct CryptoDevBackendRsaPara {
  82. uint32_t padding_algo;
  83. uint32_t hash_algo;
  84. } CryptoDevBackendRsaPara;
  85. typedef struct CryptoDevBackendAsymSessionInfo {
  86. /* corresponding with virtio crypto spec */
  87. uint32_t algo;
  88. uint32_t keytype;
  89. uint32_t keylen;
  90. uint8_t *key;
  91. union {
  92. CryptoDevBackendRsaPara rsa;
  93. } u;
  94. } CryptoDevBackendAsymSessionInfo;
  95. typedef struct CryptoDevBackendSessionInfo {
  96. uint32_t op_code;
  97. union {
  98. CryptoDevBackendSymSessionInfo sym_sess_info;
  99. CryptoDevBackendAsymSessionInfo asym_sess_info;
  100. } u;
  101. uint64_t session_id;
  102. } CryptoDevBackendSessionInfo;
  103. /**
  104. * CryptoDevBackendSymOpInfo:
  105. *
  106. * @aad_len: byte length of additional authenticated data
  107. * @iv_len: byte length of initialization vector or counter
  108. * @src_len: byte length of source data
  109. * @dst_len: byte length of destination data
  110. * @digest_result_len: byte length of hash digest result
  111. * @hash_start_src_offset: Starting point for hash processing, specified
  112. * as number of bytes from start of packet in source data, only used for
  113. * algorithm chain
  114. * @cipher_start_src_offset: Starting point for cipher processing, specified
  115. * as number of bytes from start of packet in source data, only used for
  116. * algorithm chain
  117. * @len_to_hash: byte length of source data on which the hash
  118. * operation will be computed, only used for algorithm chain
  119. * @len_to_cipher: byte length of source data on which the cipher
  120. * operation will be computed, only used for algorithm chain
  121. * @op_type: operation type (refer to virtio_crypto.h)
  122. * @iv: point to the initialization vector or counter
  123. * @src: point to the source data
  124. * @dst: point to the destination data
  125. * @aad_data: point to the additional authenticated data
  126. * @digest_result: point to the digest result data
  127. * @data[0]: point to the extensional memory by one memory allocation
  128. *
  129. */
  130. typedef struct CryptoDevBackendSymOpInfo {
  131. uint32_t aad_len;
  132. uint32_t iv_len;
  133. uint32_t src_len;
  134. uint32_t dst_len;
  135. uint32_t digest_result_len;
  136. uint32_t hash_start_src_offset;
  137. uint32_t cipher_start_src_offset;
  138. uint32_t len_to_hash;
  139. uint32_t len_to_cipher;
  140. uint8_t op_type;
  141. uint8_t *iv;
  142. uint8_t *src;
  143. uint8_t *dst;
  144. uint8_t *aad_data;
  145. uint8_t *digest_result;
  146. uint8_t data[];
  147. } CryptoDevBackendSymOpInfo;
  148. /**
  149. * CryptoDevBackendAsymOpInfo:
  150. *
  151. * @src_len: byte length of source data
  152. * @dst_len: byte length of destination data
  153. * @src: point to the source data
  154. * @dst: point to the destination data
  155. *
  156. */
  157. typedef struct CryptoDevBackendAsymOpInfo {
  158. uint32_t src_len;
  159. uint32_t dst_len;
  160. uint8_t *src;
  161. uint8_t *dst;
  162. } CryptoDevBackendAsymOpInfo;
  163. typedef void (*CryptoDevCompletionFunc) (void *opaque, int ret);
  164. typedef struct CryptoDevBackendOpInfo {
  165. QCryptodevBackendAlgoType algtype;
  166. uint32_t op_code;
  167. uint32_t queue_index;
  168. CryptoDevCompletionFunc cb;
  169. void *opaque; /* argument for cb */
  170. uint64_t session_id;
  171. union {
  172. CryptoDevBackendSymOpInfo *sym_op_info;
  173. CryptoDevBackendAsymOpInfo *asym_op_info;
  174. } u;
  175. QTAILQ_ENTRY(CryptoDevBackendOpInfo) next;
  176. } CryptoDevBackendOpInfo;
  177. struct CryptoDevBackendClass {
  178. ObjectClass parent_class;
  179. void (*init)(CryptoDevBackend *backend, Error **errp);
  180. void (*cleanup)(CryptoDevBackend *backend, Error **errp);
  181. int (*create_session)(CryptoDevBackend *backend,
  182. CryptoDevBackendSessionInfo *sess_info,
  183. uint32_t queue_index,
  184. CryptoDevCompletionFunc cb,
  185. void *opaque);
  186. int (*close_session)(CryptoDevBackend *backend,
  187. uint64_t session_id,
  188. uint32_t queue_index,
  189. CryptoDevCompletionFunc cb,
  190. void *opaque);
  191. int (*do_op)(CryptoDevBackend *backend,
  192. CryptoDevBackendOpInfo *op_info);
  193. };
  194. struct CryptoDevBackendClient {
  195. QCryptodevBackendType type;
  196. char *info_str;
  197. unsigned int queue_index;
  198. int vring_enable;
  199. QTAILQ_ENTRY(CryptoDevBackendClient) next;
  200. };
  201. struct CryptoDevBackendPeers {
  202. CryptoDevBackendClient *ccs[MAX_CRYPTO_QUEUE_NUM];
  203. uint32_t queues;
  204. };
  205. struct CryptoDevBackendConf {
  206. CryptoDevBackendPeers peers;
  207. /* Supported service mask */
  208. uint32_t crypto_services;
  209. /* Detailed algorithms mask */
  210. uint32_t cipher_algo_l;
  211. uint32_t cipher_algo_h;
  212. uint32_t hash_algo;
  213. uint32_t mac_algo_l;
  214. uint32_t mac_algo_h;
  215. uint32_t aead_algo;
  216. uint32_t akcipher_algo;
  217. /* Maximum length of cipher key */
  218. uint32_t max_cipher_key_len;
  219. /* Maximum length of authenticated key */
  220. uint32_t max_auth_key_len;
  221. /* Maximum size of each crypto request's content */
  222. uint64_t max_size;
  223. };
  224. typedef struct CryptodevBackendSymStat {
  225. int64_t encrypt_ops;
  226. int64_t decrypt_ops;
  227. int64_t encrypt_bytes;
  228. int64_t decrypt_bytes;
  229. } CryptodevBackendSymStat;
  230. typedef struct CryptodevBackendAsymStat {
  231. int64_t encrypt_ops;
  232. int64_t decrypt_ops;
  233. int64_t sign_ops;
  234. int64_t verify_ops;
  235. int64_t encrypt_bytes;
  236. int64_t decrypt_bytes;
  237. int64_t sign_bytes;
  238. int64_t verify_bytes;
  239. } CryptodevBackendAsymStat;
  240. struct CryptoDevBackend {
  241. Object parent_obj;
  242. bool ready;
  243. /* Tag the cryptodev backend is used by virtio-crypto or not */
  244. bool is_used;
  245. CryptoDevBackendConf conf;
  246. CryptodevBackendSymStat *sym_stat;
  247. CryptodevBackendAsymStat *asym_stat;
  248. ThrottleState ts;
  249. ThrottleTimers tt;
  250. ThrottleConfig tc;
  251. QTAILQ_HEAD(, CryptoDevBackendOpInfo) opinfos;
  252. };
  253. #define CryptodevSymStatInc(be, op, bytes) do { \
  254. be->sym_stat->op##_bytes += (bytes); \
  255. be->sym_stat->op##_ops += 1; \
  256. } while (/*CONSTCOND*/0)
  257. #define CryptodevSymStatIncEncrypt(be, bytes) \
  258. CryptodevSymStatInc(be, encrypt, bytes)
  259. #define CryptodevSymStatIncDecrypt(be, bytes) \
  260. CryptodevSymStatInc(be, decrypt, bytes)
  261. #define CryptodevAsymStatInc(be, op, bytes) do { \
  262. be->asym_stat->op##_bytes += (bytes); \
  263. be->asym_stat->op##_ops += 1; \
  264. } while (/*CONSTCOND*/0)
  265. #define CryptodevAsymStatIncEncrypt(be, bytes) \
  266. CryptodevAsymStatInc(be, encrypt, bytes)
  267. #define CryptodevAsymStatIncDecrypt(be, bytes) \
  268. CryptodevAsymStatInc(be, decrypt, bytes)
  269. #define CryptodevAsymStatIncSign(be, bytes) \
  270. CryptodevAsymStatInc(be, sign, bytes)
  271. #define CryptodevAsymStatIncVerify(be, bytes) \
  272. CryptodevAsymStatInc(be, verify, bytes)
  273. /**
  274. * cryptodev_backend_new_client:
  275. *
  276. * Creates a new cryptodev backend client object.
  277. *
  278. * The returned object must be released with
  279. * cryptodev_backend_free_client() when no
  280. * longer required
  281. *
  282. * Returns: a new cryptodev backend client object
  283. */
  284. CryptoDevBackendClient *cryptodev_backend_new_client(void);
  285. /**
  286. * cryptodev_backend_free_client:
  287. * @cc: the cryptodev backend client object
  288. *
  289. * Release the memory associated with @cc that
  290. * was previously allocated by cryptodev_backend_new_client()
  291. */
  292. void cryptodev_backend_free_client(
  293. CryptoDevBackendClient *cc);
  294. /**
  295. * cryptodev_backend_cleanup:
  296. * @backend: the cryptodev backend object
  297. * @errp: pointer to a NULL-initialized error object
  298. *
  299. * Clean the resource associated with @backend that realizaed
  300. * by the specific backend's init() callback
  301. */
  302. void cryptodev_backend_cleanup(
  303. CryptoDevBackend *backend,
  304. Error **errp);
  305. /**
  306. * cryptodev_backend_create_session:
  307. * @backend: the cryptodev backend object
  308. * @sess_info: parameters needed by session creating
  309. * @queue_index: queue index of cryptodev backend client
  310. * @errp: pointer to a NULL-initialized error object
  311. * @cb: callback when session create is compeleted
  312. * @opaque: parameter passed to callback
  313. *
  314. * Create a session for symmetric/asymmetric algorithms
  315. *
  316. * Returns: 0 for success and cb will be called when creation is completed,
  317. * negative value for error, and cb will not be called.
  318. */
  319. int cryptodev_backend_create_session(
  320. CryptoDevBackend *backend,
  321. CryptoDevBackendSessionInfo *sess_info,
  322. uint32_t queue_index,
  323. CryptoDevCompletionFunc cb,
  324. void *opaque);
  325. /**
  326. * cryptodev_backend_close_session:
  327. * @backend: the cryptodev backend object
  328. * @session_id: the session id
  329. * @queue_index: queue index of cryptodev backend client
  330. * @errp: pointer to a NULL-initialized error object
  331. * @cb: callback when session create is compeleted
  332. * @opaque: parameter passed to callback
  333. *
  334. * Close a session for which was previously
  335. * created by cryptodev_backend_create_session()
  336. *
  337. * Returns: 0 for success and cb will be called when creation is completed,
  338. * negative value for error, and cb will not be called.
  339. */
  340. int cryptodev_backend_close_session(
  341. CryptoDevBackend *backend,
  342. uint64_t session_id,
  343. uint32_t queue_index,
  344. CryptoDevCompletionFunc cb,
  345. void *opaque);
  346. /**
  347. * cryptodev_backend_crypto_operation:
  348. * @backend: the cryptodev backend object
  349. * @op_info: pointer to a CryptoDevBackendOpInfo object
  350. *
  351. * Do crypto operation, such as encryption, decryption, signature and
  352. * verification
  353. *
  354. * Returns: 0 for success and cb will be called when creation is completed,
  355. * negative value for error, and cb will not be called.
  356. */
  357. int cryptodev_backend_crypto_operation(
  358. CryptoDevBackend *backend,
  359. CryptoDevBackendOpInfo *op_info);
  360. /**
  361. * cryptodev_backend_set_used:
  362. * @backend: the cryptodev backend object
  363. * @used: true or false
  364. *
  365. * Set the cryptodev backend is used by virtio-crypto or not
  366. */
  367. void cryptodev_backend_set_used(CryptoDevBackend *backend, bool used);
  368. /**
  369. * cryptodev_backend_is_used:
  370. * @backend: the cryptodev backend object
  371. *
  372. * Return the status that the cryptodev backend is used
  373. * by virtio-crypto or not
  374. *
  375. * Returns: true on used, or false on not used
  376. */
  377. bool cryptodev_backend_is_used(CryptoDevBackend *backend);
  378. /**
  379. * cryptodev_backend_set_ready:
  380. * @backend: the cryptodev backend object
  381. * @ready: true or false
  382. *
  383. * Set the cryptodev backend is ready or not, which is called
  384. * by the children of the cryptodev banckend interface.
  385. */
  386. void cryptodev_backend_set_ready(CryptoDevBackend *backend, bool ready);
  387. /**
  388. * cryptodev_backend_is_ready:
  389. * @backend: the cryptodev backend object
  390. *
  391. * Return the status that the cryptodev backend is ready or not
  392. *
  393. * Returns: true on ready, or false on not ready
  394. */
  395. bool cryptodev_backend_is_ready(CryptoDevBackend *backend);
  396. #endif /* CRYPTODEV_H */