1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#ifndef CRYPTODEV_H
24#define CRYPTODEV_H
25
26#include "qemu/queue.h"
27#include "qemu/throttle.h"
28#include "qom/object.h"
29#include "qapi/qapi-types-cryptodev.h"
30
31
32
33
34
35
36
37
38
39
40#define TYPE_CRYPTODEV_BACKEND "cryptodev-backend"
41
42OBJECT_DECLARE_TYPE(CryptoDevBackend, CryptoDevBackendClass,
43 CRYPTODEV_BACKEND)
44
45
46#define MAX_CRYPTO_QUEUE_NUM 64
47
48typedef struct CryptoDevBackendConf CryptoDevBackendConf;
49typedef struct CryptoDevBackendPeers CryptoDevBackendPeers;
50typedef struct CryptoDevBackendClient
51 CryptoDevBackendClient;
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71typedef struct CryptoDevBackendSymSessionInfo {
72
73 uint32_t cipher_alg;
74 uint32_t key_len;
75 uint32_t hash_alg;
76 uint32_t hash_result_len;
77 uint32_t auth_key_len;
78 uint32_t add_len;
79 uint8_t op_type;
80 uint8_t direction;
81 uint8_t hash_mode;
82 uint8_t alg_chain_order;
83 uint8_t *cipher_key;
84 uint8_t *auth_key;
85} CryptoDevBackendSymSessionInfo;
86
87
88
89
90typedef struct CryptoDevBackendRsaPara {
91 uint32_t padding_algo;
92 uint32_t hash_algo;
93} CryptoDevBackendRsaPara;
94
95typedef struct CryptoDevBackendAsymSessionInfo {
96
97 uint32_t algo;
98 uint32_t keytype;
99 uint32_t keylen;
100 uint8_t *key;
101 union {
102 CryptoDevBackendRsaPara rsa;
103 } u;
104} CryptoDevBackendAsymSessionInfo;
105
106typedef struct CryptoDevBackendSessionInfo {
107 uint32_t op_code;
108 union {
109 CryptoDevBackendSymSessionInfo sym_sess_info;
110 CryptoDevBackendAsymSessionInfo asym_sess_info;
111 } u;
112 uint64_t session_id;
113} CryptoDevBackendSessionInfo;
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142typedef struct CryptoDevBackendSymOpInfo {
143 uint32_t aad_len;
144 uint32_t iv_len;
145 uint32_t src_len;
146 uint32_t dst_len;
147 uint32_t digest_result_len;
148 uint32_t hash_start_src_offset;
149 uint32_t cipher_start_src_offset;
150 uint32_t len_to_hash;
151 uint32_t len_to_cipher;
152 uint8_t op_type;
153 uint8_t *iv;
154 uint8_t *src;
155 uint8_t *dst;
156 uint8_t *aad_data;
157 uint8_t *digest_result;
158 uint8_t data[];
159} CryptoDevBackendSymOpInfo;
160
161
162
163
164
165
166
167
168
169
170
171typedef struct CryptoDevBackendAsymOpInfo {
172 uint32_t src_len;
173 uint32_t dst_len;
174 uint8_t *src;
175 uint8_t *dst;
176} CryptoDevBackendAsymOpInfo;
177
178typedef void (*CryptoDevCompletionFunc) (void *opaque, int ret);
179
180typedef struct CryptoDevBackendOpInfo {
181 QCryptodevBackendAlgType algtype;
182 uint32_t op_code;
183 uint32_t queue_index;
184 CryptoDevCompletionFunc cb;
185 void *opaque;
186 uint64_t session_id;
187 union {
188 CryptoDevBackendSymOpInfo *sym_op_info;
189 CryptoDevBackendAsymOpInfo *asym_op_info;
190 } u;
191 QTAILQ_ENTRY(CryptoDevBackendOpInfo) next;
192} CryptoDevBackendOpInfo;
193
194struct CryptoDevBackendClass {
195 ObjectClass parent_class;
196
197 void (*init)(CryptoDevBackend *backend, Error **errp);
198 void (*cleanup)(CryptoDevBackend *backend, Error **errp);
199
200 int (*create_session)(CryptoDevBackend *backend,
201 CryptoDevBackendSessionInfo *sess_info,
202 uint32_t queue_index,
203 CryptoDevCompletionFunc cb,
204 void *opaque);
205
206 int (*close_session)(CryptoDevBackend *backend,
207 uint64_t session_id,
208 uint32_t queue_index,
209 CryptoDevCompletionFunc cb,
210 void *opaque);
211
212 int (*do_op)(CryptoDevBackend *backend,
213 CryptoDevBackendOpInfo *op_info);
214};
215
216struct CryptoDevBackendClient {
217 QCryptodevBackendType type;
218 char *info_str;
219 unsigned int queue_index;
220 int vring_enable;
221 QTAILQ_ENTRY(CryptoDevBackendClient) next;
222};
223
224struct CryptoDevBackendPeers {
225 CryptoDevBackendClient *ccs[MAX_CRYPTO_QUEUE_NUM];
226 uint32_t queues;
227};
228
229struct CryptoDevBackendConf {
230 CryptoDevBackendPeers peers;
231
232
233 uint32_t crypto_services;
234
235
236 uint32_t cipher_algo_l;
237 uint32_t cipher_algo_h;
238 uint32_t hash_algo;
239 uint32_t mac_algo_l;
240 uint32_t mac_algo_h;
241 uint32_t aead_algo;
242 uint32_t akcipher_algo;
243
244 uint32_t max_cipher_key_len;
245
246 uint32_t max_auth_key_len;
247
248 uint64_t max_size;
249};
250
251typedef struct CryptodevBackendSymStat {
252 int64_t encrypt_ops;
253 int64_t decrypt_ops;
254 int64_t encrypt_bytes;
255 int64_t decrypt_bytes;
256} CryptodevBackendSymStat;
257
258typedef struct CryptodevBackendAsymStat {
259 int64_t encrypt_ops;
260 int64_t decrypt_ops;
261 int64_t sign_ops;
262 int64_t verify_ops;
263 int64_t encrypt_bytes;
264 int64_t decrypt_bytes;
265 int64_t sign_bytes;
266 int64_t verify_bytes;
267} CryptodevBackendAsymStat;
268
269struct CryptoDevBackend {
270 Object parent_obj;
271
272 bool ready;
273
274 bool is_used;
275 CryptoDevBackendConf conf;
276 CryptodevBackendSymStat *sym_stat;
277 CryptodevBackendAsymStat *asym_stat;
278
279 ThrottleState ts;
280 ThrottleTimers tt;
281 ThrottleConfig tc;
282 QTAILQ_HEAD(, CryptoDevBackendOpInfo) opinfos;
283};
284
285#define CryptodevSymStatInc(be, op, bytes) do { \
286 be->sym_stat->op##_bytes += (bytes); \
287 be->sym_stat->op##_ops += 1; \
288} while (0)
289
290#define CryptodevSymStatIncEncrypt(be, bytes) \
291 CryptodevSymStatInc(be, encrypt, bytes)
292
293#define CryptodevSymStatIncDecrypt(be, bytes) \
294 CryptodevSymStatInc(be, decrypt, bytes)
295
296#define CryptodevAsymStatInc(be, op, bytes) do { \
297 be->asym_stat->op##_bytes += (bytes); \
298 be->asym_stat->op##_ops += 1; \
299} while (0)
300
301#define CryptodevAsymStatIncEncrypt(be, bytes) \
302 CryptodevAsymStatInc(be, encrypt, bytes)
303
304#define CryptodevAsymStatIncDecrypt(be, bytes) \
305 CryptodevAsymStatInc(be, decrypt, bytes)
306
307#define CryptodevAsymStatIncSign(be, bytes) \
308 CryptodevAsymStatInc(be, sign, bytes)
309
310#define CryptodevAsymStatIncVerify(be, bytes) \
311 CryptodevAsymStatInc(be, verify, bytes)
312
313
314
315
316
317
318
319
320
321
322
323
324
325CryptoDevBackendClient *cryptodev_backend_new_client(void);
326
327
328
329
330
331
332
333
334void cryptodev_backend_free_client(
335 CryptoDevBackendClient *cc);
336
337
338
339
340
341
342
343
344
345void cryptodev_backend_cleanup(
346 CryptoDevBackend *backend,
347 Error **errp);
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363int cryptodev_backend_create_session(
364 CryptoDevBackend *backend,
365 CryptoDevBackendSessionInfo *sess_info,
366 uint32_t queue_index,
367 CryptoDevCompletionFunc cb,
368 void *opaque);
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385int cryptodev_backend_close_session(
386 CryptoDevBackend *backend,
387 uint64_t session_id,
388 uint32_t queue_index,
389 CryptoDevCompletionFunc cb,
390 void *opaque);
391
392
393
394
395
396
397
398
399
400
401
402
403int cryptodev_backend_crypto_operation(
404 CryptoDevBackend *backend,
405 CryptoDevBackendOpInfo *op_info);
406
407
408
409
410
411
412
413
414void cryptodev_backend_set_used(CryptoDevBackend *backend, bool used);
415
416
417
418
419
420
421
422
423
424
425bool cryptodev_backend_is_used(CryptoDevBackend *backend);
426
427
428
429
430
431
432
433
434
435void cryptodev_backend_set_ready(CryptoDevBackend *backend, bool ready);
436
437
438
439
440
441
442
443
444
445bool cryptodev_backend_is_ready(CryptoDevBackend *backend);
446
447#endif
448