Merge tag 'mips-20221108' of https://github.com/philmd/qemu into staging
[qemu/ar7.git] / include / sysemu / cryptodev.h
blobcf9b3f07fe84610e0f41ef426e6578ad40130755
1 /*
2 * QEMU Crypto Device Implementation
4 * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
6 * Authors:
7 * Gonglei <arei.gonglei@huawei.com>
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.
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.
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/>.
23 #ifndef CRYPTODEV_H
24 #define CRYPTODEV_H
26 #include "qemu/queue.h"
27 #include "qom/object.h"
29 /**
30 * CryptoDevBackend:
32 * The CryptoDevBackend object is an interface
33 * for different cryptodev backends, which provides crypto
34 * operation wrapper.
38 #define TYPE_CRYPTODEV_BACKEND "cryptodev-backend"
40 OBJECT_DECLARE_TYPE(CryptoDevBackend, CryptoDevBackendClass,
41 CRYPTODEV_BACKEND)
44 #define MAX_CRYPTO_QUEUE_NUM 64
46 typedef struct CryptoDevBackendConf CryptoDevBackendConf;
47 typedef struct CryptoDevBackendPeers CryptoDevBackendPeers;
48 typedef struct CryptoDevBackendClient
49 CryptoDevBackendClient;
51 enum CryptoDevBackendAlgType {
52 CRYPTODEV_BACKEND_ALG_SYM,
53 CRYPTODEV_BACKEND_ALG_ASYM,
54 CRYPTODEV_BACKEND_ALG__MAX,
57 /**
58 * CryptoDevBackendSymSessionInfo:
60 * @cipher_alg: algorithm type of CIPHER
61 * @key_len: byte length of cipher key
62 * @hash_alg: algorithm type of HASH/MAC
63 * @hash_result_len: byte length of HASH operation result
64 * @auth_key_len: byte length of authenticated key
65 * @add_len: byte length of additional authenticated data
66 * @op_type: operation type (refer to virtio_crypto.h)
67 * @direction: encryption or direction for CIPHER
68 * @hash_mode: HASH mode for HASH operation (refer to virtio_crypto.h)
69 * @alg_chain_order: order of algorithm chaining (CIPHER then HASH,
70 * or HASH then CIPHER)
71 * @cipher_key: point to a key of CIPHER
72 * @auth_key: point to an authenticated key of MAC
75 typedef struct CryptoDevBackendSymSessionInfo {
76 /* corresponding with virtio crypto spec */
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;
91 /**
92 * CryptoDevBackendAsymSessionInfo:
94 typedef struct CryptoDevBackendRsaPara {
95 uint32_t padding_algo;
96 uint32_t hash_algo;
97 } CryptoDevBackendRsaPara;
99 typedef struct CryptoDevBackendAsymSessionInfo {
100 /* corresponding with virtio crypto spec */
101 uint32_t algo;
102 uint32_t keytype;
103 uint32_t keylen;
104 uint8_t *key;
105 union {
106 CryptoDevBackendRsaPara rsa;
107 } u;
108 } CryptoDevBackendAsymSessionInfo;
110 typedef struct CryptoDevBackendSessionInfo {
111 uint32_t op_code;
112 union {
113 CryptoDevBackendSymSessionInfo sym_sess_info;
114 CryptoDevBackendAsymSessionInfo asym_sess_info;
115 } u;
116 uint64_t session_id;
117 } CryptoDevBackendSessionInfo;
120 * CryptoDevBackendSymOpInfo:
122 * @aad_len: byte length of additional authenticated data
123 * @iv_len: byte length of initialization vector or counter
124 * @src_len: byte length of source data
125 * @dst_len: byte length of destination data
126 * @digest_result_len: byte length of hash digest result
127 * @hash_start_src_offset: Starting point for hash processing, specified
128 * as number of bytes from start of packet in source data, only used for
129 * algorithm chain
130 * @cipher_start_src_offset: Starting point for cipher processing, specified
131 * as number of bytes from start of packet in source data, only used for
132 * algorithm chain
133 * @len_to_hash: byte length of source data on which the hash
134 * operation will be computed, only used for algorithm chain
135 * @len_to_cipher: byte length of source data on which the cipher
136 * operation will be computed, only used for algorithm chain
137 * @op_type: operation type (refer to virtio_crypto.h)
138 * @iv: point to the initialization vector or counter
139 * @src: point to the source data
140 * @dst: point to the destination data
141 * @aad_data: point to the additional authenticated data
142 * @digest_result: point to the digest result data
143 * @data[0]: point to the extensional memory by one memory allocation
146 typedef struct CryptoDevBackendSymOpInfo {
147 uint32_t aad_len;
148 uint32_t iv_len;
149 uint32_t src_len;
150 uint32_t dst_len;
151 uint32_t digest_result_len;
152 uint32_t hash_start_src_offset;
153 uint32_t cipher_start_src_offset;
154 uint32_t len_to_hash;
155 uint32_t len_to_cipher;
156 uint8_t op_type;
157 uint8_t *iv;
158 uint8_t *src;
159 uint8_t *dst;
160 uint8_t *aad_data;
161 uint8_t *digest_result;
162 uint8_t data[];
163 } CryptoDevBackendSymOpInfo;
167 * CryptoDevBackendAsymOpInfo:
169 * @src_len: byte length of source data
170 * @dst_len: byte length of destination data
171 * @src: point to the source data
172 * @dst: point to the destination data
175 typedef struct CryptoDevBackendAsymOpInfo {
176 uint32_t src_len;
177 uint32_t dst_len;
178 uint8_t *src;
179 uint8_t *dst;
180 } CryptoDevBackendAsymOpInfo;
182 typedef struct CryptoDevBackendOpInfo {
183 enum CryptoDevBackendAlgType algtype;
184 uint32_t op_code;
185 uint64_t session_id;
186 union {
187 CryptoDevBackendSymOpInfo *sym_op_info;
188 CryptoDevBackendAsymOpInfo *asym_op_info;
189 } u;
190 } CryptoDevBackendOpInfo;
192 typedef void (*CryptoDevCompletionFunc) (void *opaque, int ret);
193 struct CryptoDevBackendClass {
194 ObjectClass parent_class;
196 void (*init)(CryptoDevBackend *backend, Error **errp);
197 void (*cleanup)(CryptoDevBackend *backend, Error **errp);
199 int (*create_session)(CryptoDevBackend *backend,
200 CryptoDevBackendSessionInfo *sess_info,
201 uint32_t queue_index,
202 CryptoDevCompletionFunc cb,
203 void *opaque);
205 int (*close_session)(CryptoDevBackend *backend,
206 uint64_t session_id,
207 uint32_t queue_index,
208 CryptoDevCompletionFunc cb,
209 void *opaque);
211 int (*do_op)(CryptoDevBackend *backend,
212 CryptoDevBackendOpInfo *op_info,
213 uint32_t queue_index,
214 CryptoDevCompletionFunc cb,
215 void *opaque);
218 typedef enum CryptoDevBackendOptionsType {
219 CRYPTODEV_BACKEND_TYPE_NONE = 0,
220 CRYPTODEV_BACKEND_TYPE_BUILTIN = 1,
221 CRYPTODEV_BACKEND_TYPE_VHOST_USER = 2,
222 CRYPTODEV_BACKEND_TYPE_LKCF = 3,
223 CRYPTODEV_BACKEND_TYPE__MAX,
224 } CryptoDevBackendOptionsType;
226 struct CryptoDevBackendClient {
227 CryptoDevBackendOptionsType type;
228 char *model;
229 char *name;
230 char *info_str;
231 unsigned int queue_index;
232 int vring_enable;
233 QTAILQ_ENTRY(CryptoDevBackendClient) next;
236 struct CryptoDevBackendPeers {
237 CryptoDevBackendClient *ccs[MAX_CRYPTO_QUEUE_NUM];
238 uint32_t queues;
241 struct CryptoDevBackendConf {
242 CryptoDevBackendPeers peers;
244 /* Supported service mask */
245 uint32_t crypto_services;
247 /* Detailed algorithms mask */
248 uint32_t cipher_algo_l;
249 uint32_t cipher_algo_h;
250 uint32_t hash_algo;
251 uint32_t mac_algo_l;
252 uint32_t mac_algo_h;
253 uint32_t aead_algo;
254 uint32_t akcipher_algo;
255 /* Maximum length of cipher key */
256 uint32_t max_cipher_key_len;
257 /* Maximum length of authenticated key */
258 uint32_t max_auth_key_len;
259 /* Maximum size of each crypto request's content */
260 uint64_t max_size;
263 struct CryptoDevBackend {
264 Object parent_obj;
266 bool ready;
267 /* Tag the cryptodev backend is used by virtio-crypto or not */
268 bool is_used;
269 CryptoDevBackendConf conf;
273 * cryptodev_backend_new_client:
274 * @model: the cryptodev backend model
275 * @name: the cryptodev backend name, can be NULL
277 * Creates a new cryptodev backend client object
278 * with the @name in the model @model.
280 * The returned object must be released with
281 * cryptodev_backend_free_client() when no
282 * longer required
284 * Returns: a new cryptodev backend client object
286 CryptoDevBackendClient *
287 cryptodev_backend_new_client(const char *model,
288 const char *name);
290 * cryptodev_backend_free_client:
291 * @cc: the cryptodev backend client object
293 * Release the memory associated with @cc that
294 * was previously allocated by cryptodev_backend_new_client()
296 void cryptodev_backend_free_client(
297 CryptoDevBackendClient *cc);
300 * cryptodev_backend_cleanup:
301 * @backend: the cryptodev backend object
302 * @errp: pointer to a NULL-initialized error object
304 * Clean the resouce associated with @backend that realizaed
305 * by the specific backend's init() callback
307 void cryptodev_backend_cleanup(
308 CryptoDevBackend *backend,
309 Error **errp);
312 * cryptodev_backend_create_session:
313 * @backend: the cryptodev backend object
314 * @sess_info: parameters needed by session creating
315 * @queue_index: queue index of cryptodev backend client
316 * @errp: pointer to a NULL-initialized error object
317 * @cb: callback when session create is compeleted
318 * @opaque: parameter passed to callback
320 * Create a session for symmetric/asymmetric algorithms
322 * Returns: 0 for success and cb will be called when creation is completed,
323 * negative value for error, and cb will not be called.
325 int cryptodev_backend_create_session(
326 CryptoDevBackend *backend,
327 CryptoDevBackendSessionInfo *sess_info,
328 uint32_t queue_index,
329 CryptoDevCompletionFunc cb,
330 void *opaque);
333 * cryptodev_backend_close_session:
334 * @backend: the cryptodev backend object
335 * @session_id: the session id
336 * @queue_index: queue index of cryptodev backend client
337 * @errp: pointer to a NULL-initialized error object
338 * @cb: callback when session create is compeleted
339 * @opaque: parameter passed to callback
341 * Close a session for which was previously
342 * created by cryptodev_backend_create_session()
344 * Returns: 0 for success and cb will be called when creation is completed,
345 * negative value for error, and cb will not be called.
347 int cryptodev_backend_close_session(
348 CryptoDevBackend *backend,
349 uint64_t session_id,
350 uint32_t queue_index,
351 CryptoDevCompletionFunc cb,
352 void *opaque);
355 * cryptodev_backend_crypto_operation:
356 * @backend: the cryptodev backend object
357 * @opaque1: pointer to a VirtIOCryptoReq object
358 * @queue_index: queue index of cryptodev backend client
359 * @errp: pointer to a NULL-initialized error object
360 * @cb: callbacks when operation is completed
361 * @opaque2: parameter passed to cb
363 * Do crypto operation, such as encryption and
364 * decryption
366 * Returns: 0 for success and cb will be called when creation is completed,
367 * negative value for error, and cb will not be called.
369 int cryptodev_backend_crypto_operation(
370 CryptoDevBackend *backend,
371 void *opaque1,
372 uint32_t queue_index,
373 CryptoDevCompletionFunc cb,
374 void *opaque2);
377 * cryptodev_backend_set_used:
378 * @backend: the cryptodev backend object
379 * @used: ture or false
381 * Set the cryptodev backend is used by virtio-crypto or not
383 void cryptodev_backend_set_used(CryptoDevBackend *backend, bool used);
386 * cryptodev_backend_is_used:
387 * @backend: the cryptodev backend object
389 * Return the status that the cryptodev backend is used
390 * by virtio-crypto or not
392 * Returns: true on used, or false on not used
394 bool cryptodev_backend_is_used(CryptoDevBackend *backend);
397 * cryptodev_backend_set_ready:
398 * @backend: the cryptodev backend object
399 * @ready: ture or false
401 * Set the cryptodev backend is ready or not, which is called
402 * by the children of the cryptodev banckend interface.
404 void cryptodev_backend_set_ready(CryptoDevBackend *backend, bool ready);
407 * cryptodev_backend_is_ready:
408 * @backend: the cryptodev backend object
410 * Return the status that the cryptodev backend is ready or not
412 * Returns: true on ready, or false on not ready
414 bool cryptodev_backend_is_ready(CryptoDevBackend *backend);
416 #endif /* CRYPTODEV_H */