2 * QEMU Cryptodev backend for QEMU cipher APIs
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/>.
24 #include "qemu/osdep.h"
25 #include "sysemu/cryptodev.h"
26 #include "qapi/error.h"
27 #include "standard-headers/linux/virtio_crypto.h"
28 #include "crypto/cipher.h"
29 #include "qom/object.h"
33 * @TYPE_CRYPTODEV_BACKEND_BUILTIN:
34 * name of backend that uses QEMU cipher API
36 #define TYPE_CRYPTODEV_BACKEND_BUILTIN "cryptodev-backend-builtin"
38 OBJECT_DECLARE_SIMPLE_TYPE(CryptoDevBackendBuiltin
, CRYPTODEV_BACKEND_BUILTIN
)
41 typedef struct CryptoDevBackendBuiltinSession
{
42 QCryptoCipher
*cipher
;
43 uint8_t direction
; /* encryption or decryption */
44 uint8_t type
; /* cipher? hash? aead? */
45 QTAILQ_ENTRY(CryptoDevBackendBuiltinSession
) next
;
46 } CryptoDevBackendBuiltinSession
;
48 /* Max number of symmetric sessions */
49 #define MAX_NUM_SESSIONS 256
51 #define CRYPTODEV_BUITLIN_MAX_AUTH_KEY_LEN 512
52 #define CRYPTODEV_BUITLIN_MAX_CIPHER_KEY_LEN 64
54 struct CryptoDevBackendBuiltin
{
55 CryptoDevBackend parent_obj
;
57 CryptoDevBackendBuiltinSession
*sessions
[MAX_NUM_SESSIONS
];
60 static void cryptodev_builtin_init(
61 CryptoDevBackend
*backend
, Error
**errp
)
63 /* Only support one queue */
64 int queues
= backend
->conf
.peers
.queues
;
65 CryptoDevBackendClient
*cc
;
69 "Only support one queue in cryptdov-builtin backend");
73 cc
= cryptodev_backend_new_client(
74 "cryptodev-builtin", NULL
);
75 cc
->info_str
= g_strdup_printf("cryptodev-builtin0");
77 cc
->type
= CRYPTODEV_BACKEND_TYPE_BUILTIN
;
78 backend
->conf
.peers
.ccs
[0] = cc
;
80 backend
->conf
.crypto_services
=
81 1u << VIRTIO_CRYPTO_SERVICE_CIPHER
|
82 1u << VIRTIO_CRYPTO_SERVICE_HASH
|
83 1u << VIRTIO_CRYPTO_SERVICE_MAC
;
84 backend
->conf
.cipher_algo_l
= 1u << VIRTIO_CRYPTO_CIPHER_AES_CBC
;
85 backend
->conf
.hash_algo
= 1u << VIRTIO_CRYPTO_HASH_SHA1
;
87 * Set the Maximum length of crypto request.
88 * Why this value? Just avoid to overflow when
89 * memory allocation for each crypto request.
91 backend
->conf
.max_size
= LONG_MAX
- sizeof(CryptoDevBackendSymOpInfo
);
92 backend
->conf
.max_cipher_key_len
= CRYPTODEV_BUITLIN_MAX_CIPHER_KEY_LEN
;
93 backend
->conf
.max_auth_key_len
= CRYPTODEV_BUITLIN_MAX_AUTH_KEY_LEN
;
95 cryptodev_backend_set_ready(backend
, true);
99 cryptodev_builtin_get_unused_session_index(
100 CryptoDevBackendBuiltin
*builtin
)
104 for (i
= 0; i
< MAX_NUM_SESSIONS
; i
++) {
105 if (builtin
->sessions
[i
] == NULL
) {
113 #define AES_KEYSIZE_128 16
114 #define AES_KEYSIZE_192 24
115 #define AES_KEYSIZE_256 32
116 #define AES_KEYSIZE_128_XTS AES_KEYSIZE_256
117 #define AES_KEYSIZE_256_XTS 64
120 cryptodev_builtin_get_aes_algo(uint32_t key_len
, int mode
, Error
**errp
)
124 if (key_len
== AES_KEYSIZE_128
) {
125 algo
= QCRYPTO_CIPHER_ALG_AES_128
;
126 } else if (key_len
== AES_KEYSIZE_192
) {
127 algo
= QCRYPTO_CIPHER_ALG_AES_192
;
128 } else if (key_len
== AES_KEYSIZE_256
) { /* equals AES_KEYSIZE_128_XTS */
129 if (mode
== QCRYPTO_CIPHER_MODE_XTS
) {
130 algo
= QCRYPTO_CIPHER_ALG_AES_128
;
132 algo
= QCRYPTO_CIPHER_ALG_AES_256
;
134 } else if (key_len
== AES_KEYSIZE_256_XTS
) {
135 if (mode
== QCRYPTO_CIPHER_MODE_XTS
) {
136 algo
= QCRYPTO_CIPHER_ALG_AES_256
;
147 error_setg(errp
, "Unsupported key length :%u", key_len
);
151 static int cryptodev_builtin_create_cipher_session(
152 CryptoDevBackendBuiltin
*builtin
,
153 CryptoDevBackendSymSessionInfo
*sess_info
,
158 QCryptoCipher
*cipher
;
160 CryptoDevBackendBuiltinSession
*sess
;
162 if (sess_info
->op_type
!= VIRTIO_CRYPTO_SYM_OP_CIPHER
) {
163 error_setg(errp
, "Unsupported optype :%u", sess_info
->op_type
);
167 index
= cryptodev_builtin_get_unused_session_index(builtin
);
169 error_setg(errp
, "Total number of sessions created exceeds %u",
174 switch (sess_info
->cipher_alg
) {
175 case VIRTIO_CRYPTO_CIPHER_AES_ECB
:
176 mode
= QCRYPTO_CIPHER_MODE_ECB
;
177 algo
= cryptodev_builtin_get_aes_algo(sess_info
->key_len
,
183 case VIRTIO_CRYPTO_CIPHER_AES_CBC
:
184 mode
= QCRYPTO_CIPHER_MODE_CBC
;
185 algo
= cryptodev_builtin_get_aes_algo(sess_info
->key_len
,
191 case VIRTIO_CRYPTO_CIPHER_AES_CTR
:
192 mode
= QCRYPTO_CIPHER_MODE_CTR
;
193 algo
= cryptodev_builtin_get_aes_algo(sess_info
->key_len
,
199 case VIRTIO_CRYPTO_CIPHER_AES_XTS
:
200 mode
= QCRYPTO_CIPHER_MODE_XTS
;
201 algo
= cryptodev_builtin_get_aes_algo(sess_info
->key_len
,
207 case VIRTIO_CRYPTO_CIPHER_3DES_ECB
:
208 mode
= QCRYPTO_CIPHER_MODE_ECB
;
209 algo
= QCRYPTO_CIPHER_ALG_3DES
;
211 case VIRTIO_CRYPTO_CIPHER_3DES_CBC
:
212 mode
= QCRYPTO_CIPHER_MODE_CBC
;
213 algo
= QCRYPTO_CIPHER_ALG_3DES
;
215 case VIRTIO_CRYPTO_CIPHER_3DES_CTR
:
216 mode
= QCRYPTO_CIPHER_MODE_CTR
;
217 algo
= QCRYPTO_CIPHER_ALG_3DES
;
220 error_setg(errp
, "Unsupported cipher alg :%u",
221 sess_info
->cipher_alg
);
225 cipher
= qcrypto_cipher_new(algo
, mode
,
226 sess_info
->cipher_key
,
233 sess
= g_new0(CryptoDevBackendBuiltinSession
, 1);
234 sess
->cipher
= cipher
;
235 sess
->direction
= sess_info
->direction
;
236 sess
->type
= sess_info
->op_type
;
238 builtin
->sessions
[index
] = sess
;
243 static int64_t cryptodev_builtin_sym_create_session(
244 CryptoDevBackend
*backend
,
245 CryptoDevBackendSymSessionInfo
*sess_info
,
246 uint32_t queue_index
, Error
**errp
)
248 CryptoDevBackendBuiltin
*builtin
=
249 CRYPTODEV_BACKEND_BUILTIN(backend
);
250 int64_t session_id
= -1;
253 switch (sess_info
->op_code
) {
254 case VIRTIO_CRYPTO_CIPHER_CREATE_SESSION
:
255 ret
= cryptodev_builtin_create_cipher_session(
256 builtin
, sess_info
, errp
);
263 case VIRTIO_CRYPTO_HASH_CREATE_SESSION
:
264 case VIRTIO_CRYPTO_MAC_CREATE_SESSION
:
266 error_setg(errp
, "Unsupported opcode :%" PRIu32
"",
274 static int cryptodev_builtin_sym_close_session(
275 CryptoDevBackend
*backend
,
277 uint32_t queue_index
, Error
**errp
)
279 CryptoDevBackendBuiltin
*builtin
=
280 CRYPTODEV_BACKEND_BUILTIN(backend
);
282 assert(session_id
< MAX_NUM_SESSIONS
&& builtin
->sessions
[session_id
]);
284 qcrypto_cipher_free(builtin
->sessions
[session_id
]->cipher
);
285 g_free(builtin
->sessions
[session_id
]);
286 builtin
->sessions
[session_id
] = NULL
;
290 static int cryptodev_builtin_sym_operation(
291 CryptoDevBackend
*backend
,
292 CryptoDevBackendSymOpInfo
*op_info
,
293 uint32_t queue_index
, Error
**errp
)
295 CryptoDevBackendBuiltin
*builtin
=
296 CRYPTODEV_BACKEND_BUILTIN(backend
);
297 CryptoDevBackendBuiltinSession
*sess
;
300 if (op_info
->session_id
>= MAX_NUM_SESSIONS
||
301 builtin
->sessions
[op_info
->session_id
] == NULL
) {
302 error_setg(errp
, "Cannot find a valid session id: %" PRIu64
"",
303 op_info
->session_id
);
304 return -VIRTIO_CRYPTO_INVSESS
;
307 if (op_info
->op_type
== VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING
) {
309 "Algorithm chain is unsupported for cryptdoev-builtin");
310 return -VIRTIO_CRYPTO_NOTSUPP
;
313 sess
= builtin
->sessions
[op_info
->session_id
];
315 if (op_info
->iv_len
> 0) {
316 ret
= qcrypto_cipher_setiv(sess
->cipher
, op_info
->iv
,
317 op_info
->iv_len
, errp
);
319 return -VIRTIO_CRYPTO_ERR
;
323 if (sess
->direction
== VIRTIO_CRYPTO_OP_ENCRYPT
) {
324 ret
= qcrypto_cipher_encrypt(sess
->cipher
, op_info
->src
,
325 op_info
->dst
, op_info
->src_len
, errp
);
327 return -VIRTIO_CRYPTO_ERR
;
330 ret
= qcrypto_cipher_decrypt(sess
->cipher
, op_info
->src
,
331 op_info
->dst
, op_info
->src_len
, errp
);
333 return -VIRTIO_CRYPTO_ERR
;
336 return VIRTIO_CRYPTO_OK
;
339 static void cryptodev_builtin_cleanup(
340 CryptoDevBackend
*backend
,
343 CryptoDevBackendBuiltin
*builtin
=
344 CRYPTODEV_BACKEND_BUILTIN(backend
);
346 int queues
= backend
->conf
.peers
.queues
;
347 CryptoDevBackendClient
*cc
;
349 for (i
= 0; i
< MAX_NUM_SESSIONS
; i
++) {
350 if (builtin
->sessions
[i
] != NULL
) {
351 cryptodev_builtin_sym_close_session(backend
, i
, 0, &error_abort
);
355 for (i
= 0; i
< queues
; i
++) {
356 cc
= backend
->conf
.peers
.ccs
[i
];
358 cryptodev_backend_free_client(cc
);
359 backend
->conf
.peers
.ccs
[i
] = NULL
;
363 cryptodev_backend_set_ready(backend
, false);
367 cryptodev_builtin_class_init(ObjectClass
*oc
, void *data
)
369 CryptoDevBackendClass
*bc
= CRYPTODEV_BACKEND_CLASS(oc
);
371 bc
->init
= cryptodev_builtin_init
;
372 bc
->cleanup
= cryptodev_builtin_cleanup
;
373 bc
->create_session
= cryptodev_builtin_sym_create_session
;
374 bc
->close_session
= cryptodev_builtin_sym_close_session
;
375 bc
->do_sym_op
= cryptodev_builtin_sym_operation
;
378 static const TypeInfo cryptodev_builtin_info
= {
379 .name
= TYPE_CRYPTODEV_BACKEND_BUILTIN
,
380 .parent
= TYPE_CRYPTODEV_BACKEND
,
381 .class_init
= cryptodev_builtin_class_init
,
382 .instance_size
= sizeof(CryptoDevBackendBuiltin
),
386 cryptodev_builtin_register_types(void)
388 type_register_static(&cryptodev_builtin_info
);
391 type_init(cryptodev_builtin_register_types
);