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