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 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 "qom/object.h"
27 #include "qemu-common.h"
32 * The CryptoDevBackend object is an interface
33 * for different cryptodev backends, which provides crypto
38 #define TYPE_CRYPTODEV_BACKEND "cryptodev-backend"
40 #define CRYPTODEV_BACKEND(obj) \
41 OBJECT_CHECK(CryptoDevBackend, \
42 (obj), TYPE_CRYPTODEV_BACKEND)
43 #define CRYPTODEV_BACKEND_GET_CLASS(obj) \
44 OBJECT_GET_CLASS(CryptoDevBackendClass, \
45 (obj), TYPE_CRYPTODEV_BACKEND)
46 #define CRYPTODEV_BACKEND_CLASS(klass) \
47 OBJECT_CLASS_CHECK(CryptoDevBackendClass, \
48 (klass), TYPE_CRYPTODEV_BACKEND)
51 #define MAX_CRYPTO_QUEUE_NUM 64
53 typedef struct CryptoDevBackendConf CryptoDevBackendConf
;
54 typedef struct CryptoDevBackendPeers CryptoDevBackendPeers
;
55 typedef struct CryptoDevBackendClient
56 CryptoDevBackendClient
;
57 typedef struct CryptoDevBackend CryptoDevBackend
;
59 enum CryptoDevBackendAlgType
{
60 CRYPTODEV_BACKEND_ALG_SYM
,
61 CRYPTODEV_BACKEND_ALG__MAX
,
65 * CryptoDevBackendSymSessionInfo:
67 * @op_code: operation code (refer to virtio_crypto.h)
68 * @cipher_alg: algorithm type of CIPHER
69 * @key_len: byte length of cipher key
70 * @hash_alg: algorithm type of HASH/MAC
71 * @hash_result_len: byte length of HASH operation result
72 * @auth_key_len: byte length of authenticated key
73 * @add_len: byte length of additional authenticated data
74 * @op_type: operation type (refer to virtio_crypto.h)
75 * @direction: encryption or direction for CIPHER
76 * @hash_mode: HASH mode for HASH operation (refer to virtio_crypto.h)
77 * @alg_chain_order: order of algorithm chaining (CIPHER then HASH,
78 * or HASH then CIPHER)
79 * @cipher_key: point to a key of CIPHER
80 * @auth_key: point to an authenticated key of MAC
83 typedef struct CryptoDevBackendSymSessionInfo
{
84 /* corresponding with virtio crypto spec */
89 uint32_t hash_result_len
;
90 uint32_t auth_key_len
;
95 uint8_t alg_chain_order
;
98 } CryptoDevBackendSymSessionInfo
;
101 * CryptoDevBackendSymOpInfo:
103 * @session_id: session index which was previously
104 * created by cryptodev_backend_sym_create_session()
105 * @aad_len: byte length of additional authenticated data
106 * @iv_len: byte length of initialization vector or counter
107 * @src_len: byte length of source data
108 * @dst_len: byte length of destination data
109 * @digest_result_len: byte length of hash digest result
110 * @hash_start_src_offset: Starting point for hash processing, specified
111 * as number of bytes from start of packet in source data, only used for
113 * @cipher_start_src_offset: Starting point for cipher processing, specified
114 * as number of bytes from start of packet in source data, only used for
116 * @len_to_hash: byte length of source data on which the hash
117 * operation will be computed, only used for algorithm chain
118 * @len_to_cipher: byte length of source data on which the cipher
119 * operation will be computed, only used for algorithm chain
120 * @op_type: operation type (refer to virtio_crypto.h)
121 * @iv: point to the initialization vector or counter
122 * @src: point to the source data
123 * @dst: point to the destination data
124 * @aad_data: point to the additional authenticated data
125 * @digest_result: point to the digest result data
126 * @data[0]: point to the extensional memory by one memory allocation
129 typedef struct CryptoDevBackendSymOpInfo
{
135 uint32_t digest_result_len
;
136 uint32_t hash_start_src_offset
;
137 uint32_t cipher_start_src_offset
;
138 uint32_t len_to_hash
;
139 uint32_t len_to_cipher
;
145 uint8_t *digest_result
;
147 } CryptoDevBackendSymOpInfo
;
149 typedef struct CryptoDevBackendClass
{
150 ObjectClass parent_class
;
152 void (*init
)(CryptoDevBackend
*backend
, Error
**errp
);
153 void (*cleanup
)(CryptoDevBackend
*backend
, Error
**errp
);
155 int64_t (*create_session
)(CryptoDevBackend
*backend
,
156 CryptoDevBackendSymSessionInfo
*sess_info
,
157 uint32_t queue_index
, Error
**errp
);
158 int (*close_session
)(CryptoDevBackend
*backend
,
160 uint32_t queue_index
, Error
**errp
);
161 int (*do_sym_op
)(CryptoDevBackend
*backend
,
162 CryptoDevBackendSymOpInfo
*op_info
,
163 uint32_t queue_index
, Error
**errp
);
164 } CryptoDevBackendClass
;
167 struct CryptoDevBackendClient
{
171 unsigned int queue_index
;
172 QTAILQ_ENTRY(CryptoDevBackendClient
) next
;
175 struct CryptoDevBackendPeers
{
176 CryptoDevBackendClient
*ccs
[MAX_CRYPTO_QUEUE_NUM
];
180 struct CryptoDevBackendConf
{
181 CryptoDevBackendPeers peers
;
183 /* Supported service mask */
184 uint32_t crypto_services
;
186 /* Detailed algorithms mask */
187 uint32_t cipher_algo_l
;
188 uint32_t cipher_algo_h
;
193 /* Maximum length of cipher key */
194 uint32_t max_cipher_key_len
;
195 /* Maximum length of authenticated key */
196 uint32_t max_auth_key_len
;
197 /* Maximum size of each crypto request's content */
201 struct CryptoDevBackend
{
205 CryptoDevBackendConf conf
;
209 * cryptodev_backend_new_client:
210 * @model: the cryptodev backend model
211 * @name: the cryptodev backend name, can be NULL
213 * Creates a new cryptodev backend client object
214 * with the @name in the model @model.
216 * The returned object must be released with
217 * cryptodev_backend_free_client() when no
220 * Returns: a new cryptodev backend client object
222 CryptoDevBackendClient
*
223 cryptodev_backend_new_client(const char *model
,
226 * cryptodev_backend_free_client:
227 * @cc: the cryptodev backend client object
229 * Release the memory associated with @cc that
230 * was previously allocated by cryptodev_backend_new_client()
232 void cryptodev_backend_free_client(
233 CryptoDevBackendClient
*cc
);
236 * cryptodev_backend_cleanup:
237 * @backend: the cryptodev backend object
238 * @errp: pointer to a NULL-initialized error object
240 * Clean the resouce associated with @backend that realizaed
241 * by the specific backend's init() callback
243 void cryptodev_backend_cleanup(
244 CryptoDevBackend
*backend
,
248 * cryptodev_backend_sym_create_session:
249 * @backend: the cryptodev backend object
250 * @sess_info: parameters needed by session creating
251 * @queue_index: queue index of cryptodev backend client
252 * @errp: pointer to a NULL-initialized error object
254 * Create a session for symmetric algorithms
256 * Returns: session id on success, or -1 on error
258 int64_t cryptodev_backend_sym_create_session(
259 CryptoDevBackend
*backend
,
260 CryptoDevBackendSymSessionInfo
*sess_info
,
261 uint32_t queue_index
, Error
**errp
);
264 * cryptodev_backend_sym_close_session:
265 * @backend: the cryptodev backend object
266 * @session_id: the session id
267 * @queue_index: queue index of cryptodev backend client
268 * @errp: pointer to a NULL-initialized error object
270 * Close a session for symmetric algorithms which was previously
271 * created by cryptodev_backend_sym_create_session()
273 * Returns: 0 on success, or Negative on error
275 int cryptodev_backend_sym_close_session(
276 CryptoDevBackend
*backend
,
278 uint32_t queue_index
, Error
**errp
);
281 * cryptodev_backend_crypto_operation:
282 * @backend: the cryptodev backend object
283 * @opaque: pointer to a VirtIOCryptoReq object
284 * @queue_index: queue index of cryptodev backend client
285 * @errp: pointer to a NULL-initialized error object
287 * Do crypto operation, such as encryption and
290 * Returns: VIRTIO_CRYPTO_OK on success,
291 * or -VIRTIO_CRYPTO_* on error
293 int cryptodev_backend_crypto_operation(
294 CryptoDevBackend
*backend
,
296 uint32_t queue_index
, Error
**errp
);
298 #endif /* CRYPTODEV_H */