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