2 * QEMU Crypto Device Implementation
4 * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
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/>.
26 #include "qemu/queue.h"
27 #include "qom/object.h"
28 #include "qapi/qapi-types-cryptodev.h"
33 * The CryptoDevBackend object is an interface
34 * for different cryptodev backends, which provides crypto
39 #define TYPE_CRYPTODEV_BACKEND "cryptodev-backend"
41 OBJECT_DECLARE_TYPE(CryptoDevBackend
, CryptoDevBackendClass
,
45 #define MAX_CRYPTO_QUEUE_NUM 64
47 typedef struct CryptoDevBackendConf CryptoDevBackendConf
;
48 typedef struct CryptoDevBackendPeers CryptoDevBackendPeers
;
49 typedef struct CryptoDevBackendClient
50 CryptoDevBackendClient
;
52 enum CryptoDevBackendAlgType
{
53 CRYPTODEV_BACKEND_ALG_SYM
,
54 CRYPTODEV_BACKEND_ALG_ASYM
,
55 CRYPTODEV_BACKEND_ALG__MAX
,
59 * CryptoDevBackendSymSessionInfo:
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
76 typedef struct CryptoDevBackendSymSessionInfo
{
77 /* corresponding with virtio crypto spec */
81 uint32_t hash_result_len
;
82 uint32_t auth_key_len
;
87 uint8_t alg_chain_order
;
90 } CryptoDevBackendSymSessionInfo
;
93 * CryptoDevBackendAsymSessionInfo:
95 typedef struct CryptoDevBackendRsaPara
{
96 uint32_t padding_algo
;
98 } CryptoDevBackendRsaPara
;
100 typedef struct CryptoDevBackendAsymSessionInfo
{
101 /* corresponding with virtio crypto spec */
107 CryptoDevBackendRsaPara rsa
;
109 } CryptoDevBackendAsymSessionInfo
;
111 typedef struct CryptoDevBackendSessionInfo
{
114 CryptoDevBackendSymSessionInfo sym_sess_info
;
115 CryptoDevBackendAsymSessionInfo asym_sess_info
;
118 } CryptoDevBackendSessionInfo
;
121 * CryptoDevBackendSymOpInfo:
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
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
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
147 typedef struct CryptoDevBackendSymOpInfo
{
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
;
162 uint8_t *digest_result
;
164 } CryptoDevBackendSymOpInfo
;
168 * CryptoDevBackendAsymOpInfo:
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
176 typedef struct CryptoDevBackendAsymOpInfo
{
181 } CryptoDevBackendAsymOpInfo
;
183 typedef struct CryptoDevBackendOpInfo
{
184 enum CryptoDevBackendAlgType algtype
;
188 CryptoDevBackendSymOpInfo
*sym_op_info
;
189 CryptoDevBackendAsymOpInfo
*asym_op_info
;
191 } CryptoDevBackendOpInfo
;
193 typedef void (*CryptoDevCompletionFunc
) (void *opaque
, int ret
);
194 struct CryptoDevBackendClass
{
195 ObjectClass parent_class
;
197 void (*init
)(CryptoDevBackend
*backend
, Error
**errp
);
198 void (*cleanup
)(CryptoDevBackend
*backend
, Error
**errp
);
200 int (*create_session
)(CryptoDevBackend
*backend
,
201 CryptoDevBackendSessionInfo
*sess_info
,
202 uint32_t queue_index
,
203 CryptoDevCompletionFunc cb
,
206 int (*close_session
)(CryptoDevBackend
*backend
,
208 uint32_t queue_index
,
209 CryptoDevCompletionFunc cb
,
212 int (*do_op
)(CryptoDevBackend
*backend
,
213 CryptoDevBackendOpInfo
*op_info
,
214 uint32_t queue_index
,
215 CryptoDevCompletionFunc cb
,
219 struct CryptoDevBackendClient
{
220 QCryptodevBackendType type
;
222 unsigned int queue_index
;
224 QTAILQ_ENTRY(CryptoDevBackendClient
) next
;
227 struct CryptoDevBackendPeers
{
228 CryptoDevBackendClient
*ccs
[MAX_CRYPTO_QUEUE_NUM
];
232 struct CryptoDevBackendConf
{
233 CryptoDevBackendPeers peers
;
235 /* Supported service mask */
236 uint32_t crypto_services
;
238 /* Detailed algorithms mask */
239 uint32_t cipher_algo_l
;
240 uint32_t cipher_algo_h
;
245 uint32_t akcipher_algo
;
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 */
254 struct CryptoDevBackend
{
258 /* Tag the cryptodev backend is used by virtio-crypto or not */
260 CryptoDevBackendConf conf
;
264 * cryptodev_backend_new_client:
266 * Creates a new cryptodev backend client object.
268 * The returned object must be released with
269 * cryptodev_backend_free_client() when no
272 * Returns: a new cryptodev backend client object
274 CryptoDevBackendClient
*cryptodev_backend_new_client(void);
277 * cryptodev_backend_free_client:
278 * @cc: the cryptodev backend client object
280 * Release the memory associated with @cc that
281 * was previously allocated by cryptodev_backend_new_client()
283 void cryptodev_backend_free_client(
284 CryptoDevBackendClient
*cc
);
287 * cryptodev_backend_cleanup:
288 * @backend: the cryptodev backend object
289 * @errp: pointer to a NULL-initialized error object
291 * Clean the resouce associated with @backend that realizaed
292 * by the specific backend's init() callback
294 void cryptodev_backend_cleanup(
295 CryptoDevBackend
*backend
,
299 * cryptodev_backend_create_session:
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
304 * @cb: callback when session create is compeleted
305 * @opaque: parameter passed to callback
307 * Create a session for symmetric/asymmetric algorithms
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.
312 int cryptodev_backend_create_session(
313 CryptoDevBackend
*backend
,
314 CryptoDevBackendSessionInfo
*sess_info
,
315 uint32_t queue_index
,
316 CryptoDevCompletionFunc cb
,
320 * cryptodev_backend_close_session:
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
325 * @cb: callback when session create is compeleted
326 * @opaque: parameter passed to callback
328 * Close a session for which was previously
329 * created by cryptodev_backend_create_session()
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.
334 int cryptodev_backend_close_session(
335 CryptoDevBackend
*backend
,
337 uint32_t queue_index
,
338 CryptoDevCompletionFunc cb
,
342 * cryptodev_backend_crypto_operation:
343 * @backend: the cryptodev backend object
344 * @opaque1: pointer to a VirtIOCryptoReq object
345 * @queue_index: queue index of cryptodev backend client
346 * @errp: pointer to a NULL-initialized error object
347 * @cb: callbacks when operation is completed
348 * @opaque2: parameter passed to cb
350 * Do crypto operation, such as encryption and
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.
356 int cryptodev_backend_crypto_operation(
357 CryptoDevBackend
*backend
,
359 uint32_t queue_index
,
360 CryptoDevCompletionFunc cb
,
364 * cryptodev_backend_set_used:
365 * @backend: the cryptodev backend object
366 * @used: ture or false
368 * Set the cryptodev backend is used by virtio-crypto or not
370 void cryptodev_backend_set_used(CryptoDevBackend
*backend
, bool used
);
373 * cryptodev_backend_is_used:
374 * @backend: the cryptodev backend object
376 * Return the status that the cryptodev backend is used
377 * by virtio-crypto or not
379 * Returns: true on used, or false on not used
381 bool cryptodev_backend_is_used(CryptoDevBackend
*backend
);
384 * cryptodev_backend_set_ready:
385 * @backend: the cryptodev backend object
386 * @ready: ture or false
388 * Set the cryptodev backend is ready or not, which is called
389 * by the children of the cryptodev banckend interface.
391 void cryptodev_backend_set_ready(CryptoDevBackend
*backend
, bool ready
);
394 * cryptodev_backend_is_ready:
395 * @backend: the cryptodev backend object
397 * Return the status that the cryptodev backend is ready or not
399 * Returns: true on ready, or false on not ready
401 bool cryptodev_backend_is_ready(CryptoDevBackend
*backend
);
403 #endif /* CRYPTODEV_H */