xref: /openbmc/qemu/include/sysemu/cryptodev.h (revision d6634ac09abf20d890fd094773f125ee0ff0b1cb)
1d0ee7a13SGonglei /*
2d0ee7a13SGonglei  * QEMU Crypto Device Implementation
3d0ee7a13SGonglei  *
4d0ee7a13SGonglei  * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
5d0ee7a13SGonglei  *
6d0ee7a13SGonglei  * Authors:
7d0ee7a13SGonglei  *    Gonglei <arei.gonglei@huawei.com>
8d0ee7a13SGonglei  *
9d0ee7a13SGonglei  * This library is free software; you can redistribute it and/or
10d0ee7a13SGonglei  * modify it under the terms of the GNU Lesser General Public
11d0ee7a13SGonglei  * License as published by the Free Software Foundation; either
12d0ee7a13SGonglei  * version 2 of the License, or (at your option) any later version.
13d0ee7a13SGonglei  *
14d0ee7a13SGonglei  * This library is distributed in the hope that it will be useful,
15d0ee7a13SGonglei  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16d0ee7a13SGonglei  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17d0ee7a13SGonglei  * Lesser General Public License for more details.
18d0ee7a13SGonglei  *
19d0ee7a13SGonglei  * You should have received a copy of the GNU Lesser General Public
20d0ee7a13SGonglei  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21d0ee7a13SGonglei  *
22d0ee7a13SGonglei  */
23d0ee7a13SGonglei #ifndef CRYPTODEV_H
24d0ee7a13SGonglei #define CRYPTODEV_H
25d0ee7a13SGonglei 
26d0ee7a13SGonglei #include "qom/object.h"
27d0ee7a13SGonglei #include "qemu-common.h"
28d0ee7a13SGonglei 
29d0ee7a13SGonglei /**
30d0ee7a13SGonglei  * CryptoDevBackend:
31d0ee7a13SGonglei  *
32d0ee7a13SGonglei  * The CryptoDevBackend object is an interface
33d0ee7a13SGonglei  * for different cryptodev backends, which provides crypto
34d0ee7a13SGonglei  * operation wrapper.
35d0ee7a13SGonglei  *
36d0ee7a13SGonglei  */
37d0ee7a13SGonglei 
38d0ee7a13SGonglei #define TYPE_CRYPTODEV_BACKEND "cryptodev-backend"
39d0ee7a13SGonglei 
40d0ee7a13SGonglei #define CRYPTODEV_BACKEND(obj) \
41d0ee7a13SGonglei     OBJECT_CHECK(CryptoDevBackend, \
42d0ee7a13SGonglei                  (obj), TYPE_CRYPTODEV_BACKEND)
43d0ee7a13SGonglei #define CRYPTODEV_BACKEND_GET_CLASS(obj) \
44d0ee7a13SGonglei     OBJECT_GET_CLASS(CryptoDevBackendClass, \
45d0ee7a13SGonglei                  (obj), TYPE_CRYPTODEV_BACKEND)
46d0ee7a13SGonglei #define CRYPTODEV_BACKEND_CLASS(klass) \
47d0ee7a13SGonglei     OBJECT_CLASS_CHECK(CryptoDevBackendClass, \
48d0ee7a13SGonglei                 (klass), TYPE_CRYPTODEV_BACKEND)
49d0ee7a13SGonglei 
50d0ee7a13SGonglei 
51d0ee7a13SGonglei #define MAX_CRYPTO_QUEUE_NUM  64
52d0ee7a13SGonglei 
53d0ee7a13SGonglei typedef struct CryptoDevBackendConf CryptoDevBackendConf;
54d0ee7a13SGonglei typedef struct CryptoDevBackendPeers CryptoDevBackendPeers;
55d0ee7a13SGonglei typedef struct CryptoDevBackendClient
56d0ee7a13SGonglei                      CryptoDevBackendClient;
57d0ee7a13SGonglei typedef struct CryptoDevBackend CryptoDevBackend;
58d0ee7a13SGonglei 
599e4f86a8SGonglei enum CryptoDevBackendAlgType {
609e4f86a8SGonglei     CRYPTODEV_BACKEND_ALG_SYM,
619e4f86a8SGonglei     CRYPTODEV_BACKEND_ALG__MAX,
629e4f86a8SGonglei };
639e4f86a8SGonglei 
649e4f86a8SGonglei /**
659e4f86a8SGonglei  * CryptoDevBackendSymSessionInfo:
669e4f86a8SGonglei  *
679e4f86a8SGonglei  * @op_code: operation code (refer to virtio_crypto.h)
689e4f86a8SGonglei  * @cipher_alg: algorithm type of CIPHER
699e4f86a8SGonglei  * @key_len: byte length of cipher key
709e4f86a8SGonglei  * @hash_alg: algorithm type of HASH/MAC
719e4f86a8SGonglei  * @hash_result_len: byte length of HASH operation result
729e4f86a8SGonglei  * @auth_key_len: byte length of authenticated key
739e4f86a8SGonglei  * @add_len: byte length of additional authenticated data
749e4f86a8SGonglei  * @op_type: operation type (refer to virtio_crypto.h)
759e4f86a8SGonglei  * @direction: encryption or direction for CIPHER
769e4f86a8SGonglei  * @hash_mode: HASH mode for HASH operation (refer to virtio_crypto.h)
779e4f86a8SGonglei  * @alg_chain_order: order of algorithm chaining (CIPHER then HASH,
789e4f86a8SGonglei  *                   or HASH then CIPHER)
799e4f86a8SGonglei  * @cipher_key: point to a key of CIPHER
809e4f86a8SGonglei  * @auth_key: point to an authenticated key of MAC
819e4f86a8SGonglei  *
829e4f86a8SGonglei  */
839e4f86a8SGonglei typedef struct CryptoDevBackendSymSessionInfo {
849e4f86a8SGonglei     /* corresponding with virtio crypto spec */
859e4f86a8SGonglei     uint32_t op_code;
869e4f86a8SGonglei     uint32_t cipher_alg;
879e4f86a8SGonglei     uint32_t key_len;
889e4f86a8SGonglei     uint32_t hash_alg;
899e4f86a8SGonglei     uint32_t hash_result_len;
909e4f86a8SGonglei     uint32_t auth_key_len;
919e4f86a8SGonglei     uint32_t add_len;
929e4f86a8SGonglei     uint8_t op_type;
939e4f86a8SGonglei     uint8_t direction;
949e4f86a8SGonglei     uint8_t hash_mode;
959e4f86a8SGonglei     uint8_t alg_chain_order;
969e4f86a8SGonglei     uint8_t *cipher_key;
979e4f86a8SGonglei     uint8_t *auth_key;
989e4f86a8SGonglei } CryptoDevBackendSymSessionInfo;
999e4f86a8SGonglei 
1009e4f86a8SGonglei /**
1019e4f86a8SGonglei  * CryptoDevBackendSymOpInfo:
1029e4f86a8SGonglei  *
1039e4f86a8SGonglei  * @session_id: session index which was previously
1049e4f86a8SGonglei  *              created by cryptodev_backend_sym_create_session()
1059e4f86a8SGonglei  * @aad_len: byte length of additional authenticated data
1069e4f86a8SGonglei  * @iv_len: byte length of initialization vector or counter
1079e4f86a8SGonglei  * @src_len: byte length of source data
1089e4f86a8SGonglei  * @dst_len: byte length of destination data
1099e4f86a8SGonglei  * @digest_result_len: byte length of hash digest result
1109e4f86a8SGonglei  * @hash_start_src_offset: Starting point for hash processing, specified
1119e4f86a8SGonglei  *  as number of bytes from start of packet in source data, only used for
1129e4f86a8SGonglei  *  algorithm chain
1139e4f86a8SGonglei  * @cipher_start_src_offset: Starting point for cipher processing, specified
1149e4f86a8SGonglei  *  as number of bytes from start of packet in source data, only used for
1159e4f86a8SGonglei  *  algorithm chain
1169e4f86a8SGonglei  * @len_to_hash: byte length of source data on which the hash
1179e4f86a8SGonglei  *  operation will be computed, only used for algorithm chain
1189e4f86a8SGonglei  * @len_to_cipher: byte length of source data on which the cipher
1199e4f86a8SGonglei  *  operation will be computed, only used for algorithm chain
1209e4f86a8SGonglei  * @op_type: operation type (refer to virtio_crypto.h)
1219e4f86a8SGonglei  * @iv: point to the initialization vector or counter
1229e4f86a8SGonglei  * @src: point to the source data
1239e4f86a8SGonglei  * @dst: point to the destination data
1249e4f86a8SGonglei  * @aad_data: point to the additional authenticated data
1259e4f86a8SGonglei  * @digest_result: point to the digest result data
1269e4f86a8SGonglei  * @data[0]: point to the extensional memory by one memory allocation
1279e4f86a8SGonglei  *
1289e4f86a8SGonglei  */
1299e4f86a8SGonglei typedef struct CryptoDevBackendSymOpInfo {
1309e4f86a8SGonglei     uint64_t session_id;
1319e4f86a8SGonglei     uint32_t aad_len;
1329e4f86a8SGonglei     uint32_t iv_len;
1339e4f86a8SGonglei     uint32_t src_len;
1349e4f86a8SGonglei     uint32_t dst_len;
1359e4f86a8SGonglei     uint32_t digest_result_len;
1369e4f86a8SGonglei     uint32_t hash_start_src_offset;
1379e4f86a8SGonglei     uint32_t cipher_start_src_offset;
1389e4f86a8SGonglei     uint32_t len_to_hash;
1399e4f86a8SGonglei     uint32_t len_to_cipher;
1409e4f86a8SGonglei     uint8_t op_type;
1419e4f86a8SGonglei     uint8_t *iv;
1429e4f86a8SGonglei     uint8_t *src;
1439e4f86a8SGonglei     uint8_t *dst;
1449e4f86a8SGonglei     uint8_t *aad_data;
1459e4f86a8SGonglei     uint8_t *digest_result;
1469e4f86a8SGonglei     uint8_t data[0];
1479e4f86a8SGonglei } CryptoDevBackendSymOpInfo;
148d0ee7a13SGonglei 
149d0ee7a13SGonglei typedef struct CryptoDevBackendClass {
150d0ee7a13SGonglei     ObjectClass parent_class;
151d0ee7a13SGonglei 
152d0ee7a13SGonglei     void (*init)(CryptoDevBackend *backend, Error **errp);
153d0ee7a13SGonglei     void (*cleanup)(CryptoDevBackend *backend, Error **errp);
1549e4f86a8SGonglei 
1559e4f86a8SGonglei     int64_t (*create_session)(CryptoDevBackend *backend,
1569e4f86a8SGonglei                        CryptoDevBackendSymSessionInfo *sess_info,
1579e4f86a8SGonglei                        uint32_t queue_index, Error **errp);
1589e4f86a8SGonglei     int (*close_session)(CryptoDevBackend *backend,
1599e4f86a8SGonglei                            uint64_t session_id,
1609e4f86a8SGonglei                            uint32_t queue_index, Error **errp);
1619e4f86a8SGonglei     int (*do_sym_op)(CryptoDevBackend *backend,
1629e4f86a8SGonglei                      CryptoDevBackendSymOpInfo *op_info,
1639e4f86a8SGonglei                      uint32_t queue_index, Error **errp);
164d0ee7a13SGonglei } CryptoDevBackendClass;
165d0ee7a13SGonglei 
166d0ee7a13SGonglei 
167d0ee7a13SGonglei struct CryptoDevBackendClient {
168d0ee7a13SGonglei     char *model;
169d0ee7a13SGonglei     char *name;
170d0ee7a13SGonglei     char *info_str;
171d0ee7a13SGonglei     unsigned int queue_index;
172d0ee7a13SGonglei     QTAILQ_ENTRY(CryptoDevBackendClient) next;
173d0ee7a13SGonglei };
174d0ee7a13SGonglei 
175d0ee7a13SGonglei struct CryptoDevBackendPeers {
176d0ee7a13SGonglei     CryptoDevBackendClient *ccs[MAX_CRYPTO_QUEUE_NUM];
177d0ee7a13SGonglei     uint32_t queues;
178d0ee7a13SGonglei };
179d0ee7a13SGonglei 
180d0ee7a13SGonglei struct CryptoDevBackendConf {
181d0ee7a13SGonglei     CryptoDevBackendPeers peers;
182d0ee7a13SGonglei 
183d0ee7a13SGonglei     /* Supported service mask */
184d0ee7a13SGonglei     uint32_t crypto_services;
185d0ee7a13SGonglei 
186d0ee7a13SGonglei     /* Detailed algorithms mask */
187d0ee7a13SGonglei     uint32_t cipher_algo_l;
188d0ee7a13SGonglei     uint32_t cipher_algo_h;
189d0ee7a13SGonglei     uint32_t hash_algo;
190d0ee7a13SGonglei     uint32_t mac_algo_l;
191d0ee7a13SGonglei     uint32_t mac_algo_h;
192d0ee7a13SGonglei     uint32_t aead_algo;
193d0ee7a13SGonglei     /* Maximum length of cipher key */
194d0ee7a13SGonglei     uint32_t max_cipher_key_len;
195d0ee7a13SGonglei     /* Maximum length of authenticated key */
196d0ee7a13SGonglei     uint32_t max_auth_key_len;
197d0ee7a13SGonglei     /* Maximum size of each crypto request's content */
198d0ee7a13SGonglei     uint64_t max_size;
199d0ee7a13SGonglei };
200d0ee7a13SGonglei 
201d0ee7a13SGonglei struct CryptoDevBackend {
202d0ee7a13SGonglei     Object parent_obj;
203d0ee7a13SGonglei 
204d0ee7a13SGonglei     bool ready;
205d0ee7a13SGonglei     CryptoDevBackendConf conf;
206d0ee7a13SGonglei };
207d0ee7a13SGonglei 
208d0ee7a13SGonglei /**
209d0ee7a13SGonglei  * cryptodev_backend_new_client:
210d0ee7a13SGonglei  * @model: the cryptodev backend model
211d0ee7a13SGonglei  * @name: the cryptodev backend name, can be NULL
212d0ee7a13SGonglei  *
213d0ee7a13SGonglei  * Creates a new cryptodev backend client object
214d0ee7a13SGonglei  * with the @name in the model @model.
215d0ee7a13SGonglei  *
216d0ee7a13SGonglei  * The returned object must be released with
217d0ee7a13SGonglei  * cryptodev_backend_free_client() when no
218d0ee7a13SGonglei  * longer required
219d0ee7a13SGonglei  *
220d0ee7a13SGonglei  * Returns: a new cryptodev backend client object
221d0ee7a13SGonglei  */
222d0ee7a13SGonglei CryptoDevBackendClient *
223d0ee7a13SGonglei cryptodev_backend_new_client(const char *model,
224d0ee7a13SGonglei                                     const char *name);
225d0ee7a13SGonglei /**
226d0ee7a13SGonglei  * cryptodev_backend_free_client:
227d0ee7a13SGonglei  * @cc: the cryptodev backend client object
228d0ee7a13SGonglei  *
229d0ee7a13SGonglei  * Release the memory associated with @cc that
230d0ee7a13SGonglei  * was previously allocated by cryptodev_backend_new_client()
231d0ee7a13SGonglei  */
232d0ee7a13SGonglei void cryptodev_backend_free_client(
233d0ee7a13SGonglei                   CryptoDevBackendClient *cc);
234d0ee7a13SGonglei 
235d0ee7a13SGonglei /**
236d0ee7a13SGonglei  * cryptodev_backend_cleanup:
237d0ee7a13SGonglei  * @backend: the cryptodev backend object
238d0ee7a13SGonglei  * @errp: pointer to a NULL-initialized error object
239d0ee7a13SGonglei  *
240d0ee7a13SGonglei  * Clean the resouce associated with @backend that realizaed
241d0ee7a13SGonglei  * by the specific backend's init() callback
242d0ee7a13SGonglei  */
243d0ee7a13SGonglei void cryptodev_backend_cleanup(
244d0ee7a13SGonglei            CryptoDevBackend *backend,
245d0ee7a13SGonglei            Error **errp);
246d0ee7a13SGonglei 
2479e4f86a8SGonglei /**
2489e4f86a8SGonglei  * cryptodev_backend_sym_create_session:
2499e4f86a8SGonglei  * @backend: the cryptodev backend object
2509e4f86a8SGonglei  * @sess_info: parameters needed by session creating
2519e4f86a8SGonglei  * @queue_index: queue index of cryptodev backend client
2529e4f86a8SGonglei  * @errp: pointer to a NULL-initialized error object
2539e4f86a8SGonglei  *
2549e4f86a8SGonglei  * Create a session for symmetric algorithms
2559e4f86a8SGonglei  *
2569e4f86a8SGonglei  * Returns: session id on success, or -1 on error
2579e4f86a8SGonglei  */
2589e4f86a8SGonglei int64_t cryptodev_backend_sym_create_session(
2599e4f86a8SGonglei            CryptoDevBackend *backend,
2609e4f86a8SGonglei            CryptoDevBackendSymSessionInfo *sess_info,
2619e4f86a8SGonglei            uint32_t queue_index, Error **errp);
2629e4f86a8SGonglei 
2639e4f86a8SGonglei /**
2649e4f86a8SGonglei  * cryptodev_backend_sym_close_session:
2659e4f86a8SGonglei  * @backend: the cryptodev backend object
2669e4f86a8SGonglei  * @session_id: the session id
2679e4f86a8SGonglei  * @queue_index: queue index of cryptodev backend client
2689e4f86a8SGonglei  * @errp: pointer to a NULL-initialized error object
2699e4f86a8SGonglei  *
2709e4f86a8SGonglei  * Close a session for symmetric algorithms which was previously
2719e4f86a8SGonglei  * created by cryptodev_backend_sym_create_session()
2729e4f86a8SGonglei  *
2739e4f86a8SGonglei  * Returns: 0 on success, or Negative on error
2749e4f86a8SGonglei  */
2759e4f86a8SGonglei int cryptodev_backend_sym_close_session(
2769e4f86a8SGonglei            CryptoDevBackend *backend,
2779e4f86a8SGonglei            uint64_t session_id,
2789e4f86a8SGonglei            uint32_t queue_index, Error **errp);
2799e4f86a8SGonglei 
2809e4f86a8SGonglei /**
281*d6634ac0SGonglei  * cryptodev_backend_crypto_operation:
2829e4f86a8SGonglei  * @backend: the cryptodev backend object
283*d6634ac0SGonglei  * @opaque: pointer to a VirtIOCryptoReq object
2849e4f86a8SGonglei  * @queue_index: queue index of cryptodev backend client
2859e4f86a8SGonglei  * @errp: pointer to a NULL-initialized error object
2869e4f86a8SGonglei  *
287*d6634ac0SGonglei  * Do crypto operation, such as encryption and
2889e4f86a8SGonglei  * decryption
2899e4f86a8SGonglei  *
290*d6634ac0SGonglei  * Returns: VIRTIO_CRYPTO_OK on success,
291*d6634ac0SGonglei  *         or -VIRTIO_CRYPTO_* on error
2929e4f86a8SGonglei  */
293*d6634ac0SGonglei int cryptodev_backend_crypto_operation(
2949e4f86a8SGonglei                  CryptoDevBackend *backend,
295*d6634ac0SGonglei                  void *opaque,
2969e4f86a8SGonglei                  uint32_t queue_index, Error **errp);
2979e4f86a8SGonglei 
298d0ee7a13SGonglei #endif /* CRYPTODEV_H */
299