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
;
53 * CryptoDevBackendSymSessionInfo:
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
70 typedef struct CryptoDevBackendSymSessionInfo
{
71 /* corresponding with virtio crypto spec */
75 uint32_t hash_result_len
;
76 uint32_t auth_key_len
;
81 uint8_t alg_chain_order
;
84 } CryptoDevBackendSymSessionInfo
;
87 * CryptoDevBackendAsymSessionInfo:
89 typedef struct CryptoDevBackendRsaPara
{
90 uint32_t padding_algo
;
92 } CryptoDevBackendRsaPara
;
94 typedef struct CryptoDevBackendAsymSessionInfo
{
95 /* corresponding with virtio crypto spec */
101 CryptoDevBackendRsaPara rsa
;
103 } CryptoDevBackendAsymSessionInfo
;
105 typedef struct CryptoDevBackendSessionInfo
{
108 CryptoDevBackendSymSessionInfo sym_sess_info
;
109 CryptoDevBackendAsymSessionInfo asym_sess_info
;
112 } CryptoDevBackendSessionInfo
;
115 * CryptoDevBackendSymOpInfo:
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
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
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
141 typedef struct CryptoDevBackendSymOpInfo
{
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
;
156 uint8_t *digest_result
;
158 } CryptoDevBackendSymOpInfo
;
162 * CryptoDevBackendAsymOpInfo:
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
170 typedef struct CryptoDevBackendAsymOpInfo
{
175 } CryptoDevBackendAsymOpInfo
;
177 typedef void (*CryptoDevCompletionFunc
) (void *opaque
, int ret
);
179 typedef struct CryptoDevBackendOpInfo
{
180 QCryptodevBackendAlgType algtype
;
182 uint32_t queue_index
;
183 CryptoDevCompletionFunc cb
;
184 void *opaque
; /* argument for cb */
187 CryptoDevBackendSymOpInfo
*sym_op_info
;
188 CryptoDevBackendAsymOpInfo
*asym_op_info
;
190 } CryptoDevBackendOpInfo
;
192 struct CryptoDevBackendClass
{
193 ObjectClass parent_class
;
195 void (*init
)(CryptoDevBackend
*backend
, Error
**errp
);
196 void (*cleanup
)(CryptoDevBackend
*backend
, Error
**errp
);
198 int (*create_session
)(CryptoDevBackend
*backend
,
199 CryptoDevBackendSessionInfo
*sess_info
,
200 uint32_t queue_index
,
201 CryptoDevCompletionFunc cb
,
204 int (*close_session
)(CryptoDevBackend
*backend
,
206 uint32_t queue_index
,
207 CryptoDevCompletionFunc cb
,
210 int (*do_op
)(CryptoDevBackend
*backend
,
211 CryptoDevBackendOpInfo
*op_info
);
214 struct CryptoDevBackendClient
{
215 QCryptodevBackendType type
;
217 unsigned int queue_index
;
219 QTAILQ_ENTRY(CryptoDevBackendClient
) next
;
222 struct CryptoDevBackendPeers
{
223 CryptoDevBackendClient
*ccs
[MAX_CRYPTO_QUEUE_NUM
];
227 struct CryptoDevBackendConf
{
228 CryptoDevBackendPeers peers
;
230 /* Supported service mask */
231 uint32_t crypto_services
;
233 /* Detailed algorithms mask */
234 uint32_t cipher_algo_l
;
235 uint32_t cipher_algo_h
;
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 */
249 struct CryptoDevBackend
{
253 /* Tag the cryptodev backend is used by virtio-crypto or not */
255 CryptoDevBackendConf conf
;
259 * cryptodev_backend_new_client:
261 * Creates a new cryptodev backend client object.
263 * The returned object must be released with
264 * cryptodev_backend_free_client() when no
267 * Returns: a new cryptodev backend client object
269 CryptoDevBackendClient
*cryptodev_backend_new_client(void);
272 * cryptodev_backend_free_client:
273 * @cc: the cryptodev backend client object
275 * Release the memory associated with @cc that
276 * was previously allocated by cryptodev_backend_new_client()
278 void cryptodev_backend_free_client(
279 CryptoDevBackendClient
*cc
);
282 * cryptodev_backend_cleanup:
283 * @backend: the cryptodev backend object
284 * @errp: pointer to a NULL-initialized error object
286 * Clean the resouce associated with @backend that realizaed
287 * by the specific backend's init() callback
289 void cryptodev_backend_cleanup(
290 CryptoDevBackend
*backend
,
294 * cryptodev_backend_create_session:
295 * @backend: the cryptodev backend object
296 * @sess_info: parameters needed by session creating
297 * @queue_index: queue index of cryptodev backend client
298 * @errp: pointer to a NULL-initialized error object
299 * @cb: callback when session create is compeleted
300 * @opaque: parameter passed to callback
302 * Create a session for symmetric/asymmetric algorithms
304 * Returns: 0 for success and cb will be called when creation is completed,
305 * negative value for error, and cb will not be called.
307 int cryptodev_backend_create_session(
308 CryptoDevBackend
*backend
,
309 CryptoDevBackendSessionInfo
*sess_info
,
310 uint32_t queue_index
,
311 CryptoDevCompletionFunc cb
,
315 * cryptodev_backend_close_session:
316 * @backend: the cryptodev backend object
317 * @session_id: the session id
318 * @queue_index: queue index of cryptodev backend client
319 * @errp: pointer to a NULL-initialized error object
320 * @cb: callback when session create is compeleted
321 * @opaque: parameter passed to callback
323 * Close a session for which was previously
324 * created by cryptodev_backend_create_session()
326 * Returns: 0 for success and cb will be called when creation is completed,
327 * negative value for error, and cb will not be called.
329 int cryptodev_backend_close_session(
330 CryptoDevBackend
*backend
,
332 uint32_t queue_index
,
333 CryptoDevCompletionFunc cb
,
337 * cryptodev_backend_crypto_operation:
338 * @backend: the cryptodev backend object
339 * @op_info: pointer to a CryptoDevBackendOpInfo object
341 * Do crypto operation, such as encryption, decryption, signature and
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_crypto_operation(
348 CryptoDevBackend
*backend
,
349 CryptoDevBackendOpInfo
*op_info
);
352 * cryptodev_backend_set_used:
353 * @backend: the cryptodev backend object
354 * @used: ture or false
356 * Set the cryptodev backend is used by virtio-crypto or not
358 void cryptodev_backend_set_used(CryptoDevBackend
*backend
, bool used
);
361 * cryptodev_backend_is_used:
362 * @backend: the cryptodev backend object
364 * Return the status that the cryptodev backend is used
365 * by virtio-crypto or not
367 * Returns: true on used, or false on not used
369 bool cryptodev_backend_is_used(CryptoDevBackend
*backend
);
372 * cryptodev_backend_set_ready:
373 * @backend: the cryptodev backend object
374 * @ready: ture or false
376 * Set the cryptodev backend is ready or not, which is called
377 * by the children of the cryptodev banckend interface.
379 void cryptodev_backend_set_ready(CryptoDevBackend
*backend
, bool ready
);
382 * cryptodev_backend_is_ready:
383 * @backend: the cryptodev backend object
385 * Return the status that the cryptodev backend is ready or not
387 * Returns: true on ready, or false on not ready
389 bool cryptodev_backend_is_ready(CryptoDevBackend
*backend
);
391 #endif /* CRYPTODEV_H */