]> git.proxmox.com Git - mirror_qemu.git/blob - include/sysemu/cryptodev.h
cryptodev: Introduce cryptodev alg type in QAPI
[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 struct CryptoDevBackendOpInfo {
178 QCryptodevBackendAlgType algtype;
179 uint32_t op_code;
180 uint64_t session_id;
181 union {
182 CryptoDevBackendSymOpInfo *sym_op_info;
183 CryptoDevBackendAsymOpInfo *asym_op_info;
184 } u;
185 } CryptoDevBackendOpInfo;
186
187 typedef void (*CryptoDevCompletionFunc) (void *opaque, int ret);
188 struct CryptoDevBackendClass {
189 ObjectClass parent_class;
190
191 void (*init)(CryptoDevBackend *backend, Error **errp);
192 void (*cleanup)(CryptoDevBackend *backend, Error **errp);
193
194 int (*create_session)(CryptoDevBackend *backend,
195 CryptoDevBackendSessionInfo *sess_info,
196 uint32_t queue_index,
197 CryptoDevCompletionFunc cb,
198 void *opaque);
199
200 int (*close_session)(CryptoDevBackend *backend,
201 uint64_t session_id,
202 uint32_t queue_index,
203 CryptoDevCompletionFunc cb,
204 void *opaque);
205
206 int (*do_op)(CryptoDevBackend *backend,
207 CryptoDevBackendOpInfo *op_info,
208 uint32_t queue_index,
209 CryptoDevCompletionFunc cb,
210 void *opaque);
211 };
212
213 struct CryptoDevBackendClient {
214 QCryptodevBackendType type;
215 char *info_str;
216 unsigned int queue_index;
217 int vring_enable;
218 QTAILQ_ENTRY(CryptoDevBackendClient) next;
219 };
220
221 struct CryptoDevBackendPeers {
222 CryptoDevBackendClient *ccs[MAX_CRYPTO_QUEUE_NUM];
223 uint32_t queues;
224 };
225
226 struct CryptoDevBackendConf {
227 CryptoDevBackendPeers peers;
228
229 /* Supported service mask */
230 uint32_t crypto_services;
231
232 /* Detailed algorithms mask */
233 uint32_t cipher_algo_l;
234 uint32_t cipher_algo_h;
235 uint32_t hash_algo;
236 uint32_t mac_algo_l;
237 uint32_t mac_algo_h;
238 uint32_t aead_algo;
239 uint32_t akcipher_algo;
240 /* Maximum length of cipher key */
241 uint32_t max_cipher_key_len;
242 /* Maximum length of authenticated key */
243 uint32_t max_auth_key_len;
244 /* Maximum size of each crypto request's content */
245 uint64_t max_size;
246 };
247
248 struct CryptoDevBackend {
249 Object parent_obj;
250
251 bool ready;
252 /* Tag the cryptodev backend is used by virtio-crypto or not */
253 bool is_used;
254 CryptoDevBackendConf conf;
255 };
256
257 /**
258 * cryptodev_backend_new_client:
259 *
260 * Creates a new cryptodev backend client object.
261 *
262 * The returned object must be released with
263 * cryptodev_backend_free_client() when no
264 * longer required
265 *
266 * Returns: a new cryptodev backend client object
267 */
268 CryptoDevBackendClient *cryptodev_backend_new_client(void);
269
270 /**
271 * cryptodev_backend_free_client:
272 * @cc: the cryptodev backend client object
273 *
274 * Release the memory associated with @cc that
275 * was previously allocated by cryptodev_backend_new_client()
276 */
277 void cryptodev_backend_free_client(
278 CryptoDevBackendClient *cc);
279
280 /**
281 * cryptodev_backend_cleanup:
282 * @backend: the cryptodev backend object
283 * @errp: pointer to a NULL-initialized error object
284 *
285 * Clean the resouce associated with @backend that realizaed
286 * by the specific backend's init() callback
287 */
288 void cryptodev_backend_cleanup(
289 CryptoDevBackend *backend,
290 Error **errp);
291
292 /**
293 * cryptodev_backend_create_session:
294 * @backend: the cryptodev backend object
295 * @sess_info: parameters needed by session creating
296 * @queue_index: queue index of cryptodev backend client
297 * @errp: pointer to a NULL-initialized error object
298 * @cb: callback when session create is compeleted
299 * @opaque: parameter passed to callback
300 *
301 * Create a session for symmetric/asymmetric algorithms
302 *
303 * Returns: 0 for success and cb will be called when creation is completed,
304 * negative value for error, and cb will not be called.
305 */
306 int cryptodev_backend_create_session(
307 CryptoDevBackend *backend,
308 CryptoDevBackendSessionInfo *sess_info,
309 uint32_t queue_index,
310 CryptoDevCompletionFunc cb,
311 void *opaque);
312
313 /**
314 * cryptodev_backend_close_session:
315 * @backend: the cryptodev backend object
316 * @session_id: the session id
317 * @queue_index: queue index of cryptodev backend client
318 * @errp: pointer to a NULL-initialized error object
319 * @cb: callback when session create is compeleted
320 * @opaque: parameter passed to callback
321 *
322 * Close a session for which was previously
323 * created by cryptodev_backend_create_session()
324 *
325 * Returns: 0 for success and cb will be called when creation is completed,
326 * negative value for error, and cb will not be called.
327 */
328 int cryptodev_backend_close_session(
329 CryptoDevBackend *backend,
330 uint64_t session_id,
331 uint32_t queue_index,
332 CryptoDevCompletionFunc cb,
333 void *opaque);
334
335 /**
336 * cryptodev_backend_crypto_operation:
337 * @backend: the cryptodev backend object
338 * @opaque1: pointer to a VirtIOCryptoReq object
339 * @queue_index: queue index of cryptodev backend client
340 * @errp: pointer to a NULL-initialized error object
341 * @cb: callbacks when operation is completed
342 * @opaque2: parameter passed to cb
343 *
344 * Do crypto operation, such as encryption and
345 * decryption
346 *
347 * Returns: 0 for success and cb will be called when creation is completed,
348 * negative value for error, and cb will not be called.
349 */
350 int cryptodev_backend_crypto_operation(
351 CryptoDevBackend *backend,
352 void *opaque1,
353 uint32_t queue_index,
354 CryptoDevCompletionFunc cb,
355 void *opaque2);
356
357 /**
358 * cryptodev_backend_set_used:
359 * @backend: the cryptodev backend object
360 * @used: ture or false
361 *
362 * Set the cryptodev backend is used by virtio-crypto or not
363 */
364 void cryptodev_backend_set_used(CryptoDevBackend *backend, bool used);
365
366 /**
367 * cryptodev_backend_is_used:
368 * @backend: the cryptodev backend object
369 *
370 * Return the status that the cryptodev backend is used
371 * by virtio-crypto or not
372 *
373 * Returns: true on used, or false on not used
374 */
375 bool cryptodev_backend_is_used(CryptoDevBackend *backend);
376
377 /**
378 * cryptodev_backend_set_ready:
379 * @backend: the cryptodev backend object
380 * @ready: ture or false
381 *
382 * Set the cryptodev backend is ready or not, which is called
383 * by the children of the cryptodev banckend interface.
384 */
385 void cryptodev_backend_set_ready(CryptoDevBackend *backend, bool ready);
386
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
397 #endif /* CRYPTODEV_H */