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 26dc5e9ac7SMarkus Armbruster #include "qemu/queue.h" 27d0ee7a13SGonglei #include "qom/object.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 40*c821774aSEduardo Habkost OBJECT_DECLARE_TYPE(CryptoDevBackend, CryptoDevBackendClass, 41*c821774aSEduardo Habkost cryptodev_backend, CRYPTODEV_BACKEND) 42d0ee7a13SGonglei 43d0ee7a13SGonglei 44d0ee7a13SGonglei #define MAX_CRYPTO_QUEUE_NUM 64 45d0ee7a13SGonglei 46d0ee7a13SGonglei typedef struct CryptoDevBackendConf CryptoDevBackendConf; 47d0ee7a13SGonglei typedef struct CryptoDevBackendPeers CryptoDevBackendPeers; 48d0ee7a13SGonglei typedef struct CryptoDevBackendClient 49d0ee7a13SGonglei CryptoDevBackendClient; 50d0ee7a13SGonglei 519e4f86a8SGonglei enum CryptoDevBackendAlgType { 529e4f86a8SGonglei CRYPTODEV_BACKEND_ALG_SYM, 539e4f86a8SGonglei CRYPTODEV_BACKEND_ALG__MAX, 549e4f86a8SGonglei }; 559e4f86a8SGonglei 569e4f86a8SGonglei /** 579e4f86a8SGonglei * CryptoDevBackendSymSessionInfo: 589e4f86a8SGonglei * 599e4f86a8SGonglei * @op_code: operation code (refer to virtio_crypto.h) 609e4f86a8SGonglei * @cipher_alg: algorithm type of CIPHER 619e4f86a8SGonglei * @key_len: byte length of cipher key 629e4f86a8SGonglei * @hash_alg: algorithm type of HASH/MAC 639e4f86a8SGonglei * @hash_result_len: byte length of HASH operation result 649e4f86a8SGonglei * @auth_key_len: byte length of authenticated key 659e4f86a8SGonglei * @add_len: byte length of additional authenticated data 669e4f86a8SGonglei * @op_type: operation type (refer to virtio_crypto.h) 679e4f86a8SGonglei * @direction: encryption or direction for CIPHER 689e4f86a8SGonglei * @hash_mode: HASH mode for HASH operation (refer to virtio_crypto.h) 699e4f86a8SGonglei * @alg_chain_order: order of algorithm chaining (CIPHER then HASH, 709e4f86a8SGonglei * or HASH then CIPHER) 719e4f86a8SGonglei * @cipher_key: point to a key of CIPHER 729e4f86a8SGonglei * @auth_key: point to an authenticated key of MAC 739e4f86a8SGonglei * 749e4f86a8SGonglei */ 759e4f86a8SGonglei typedef struct CryptoDevBackendSymSessionInfo { 769e4f86a8SGonglei /* corresponding with virtio crypto spec */ 779e4f86a8SGonglei uint32_t op_code; 789e4f86a8SGonglei uint32_t cipher_alg; 799e4f86a8SGonglei uint32_t key_len; 809e4f86a8SGonglei uint32_t hash_alg; 819e4f86a8SGonglei uint32_t hash_result_len; 829e4f86a8SGonglei uint32_t auth_key_len; 839e4f86a8SGonglei uint32_t add_len; 849e4f86a8SGonglei uint8_t op_type; 859e4f86a8SGonglei uint8_t direction; 869e4f86a8SGonglei uint8_t hash_mode; 879e4f86a8SGonglei uint8_t alg_chain_order; 889e4f86a8SGonglei uint8_t *cipher_key; 899e4f86a8SGonglei uint8_t *auth_key; 909e4f86a8SGonglei } CryptoDevBackendSymSessionInfo; 919e4f86a8SGonglei 929e4f86a8SGonglei /** 939e4f86a8SGonglei * CryptoDevBackendSymOpInfo: 949e4f86a8SGonglei * 959e4f86a8SGonglei * @session_id: session index which was previously 969e4f86a8SGonglei * created by cryptodev_backend_sym_create_session() 979e4f86a8SGonglei * @aad_len: byte length of additional authenticated data 989e4f86a8SGonglei * @iv_len: byte length of initialization vector or counter 999e4f86a8SGonglei * @src_len: byte length of source data 1009e4f86a8SGonglei * @dst_len: byte length of destination data 1019e4f86a8SGonglei * @digest_result_len: byte length of hash digest result 1029e4f86a8SGonglei * @hash_start_src_offset: Starting point for hash processing, specified 1039e4f86a8SGonglei * as number of bytes from start of packet in source data, only used for 1049e4f86a8SGonglei * algorithm chain 1059e4f86a8SGonglei * @cipher_start_src_offset: Starting point for cipher processing, specified 1069e4f86a8SGonglei * as number of bytes from start of packet in source data, only used for 1079e4f86a8SGonglei * algorithm chain 1089e4f86a8SGonglei * @len_to_hash: byte length of source data on which the hash 1099e4f86a8SGonglei * operation will be computed, only used for algorithm chain 1109e4f86a8SGonglei * @len_to_cipher: byte length of source data on which the cipher 1119e4f86a8SGonglei * operation will be computed, only used for algorithm chain 1129e4f86a8SGonglei * @op_type: operation type (refer to virtio_crypto.h) 1139e4f86a8SGonglei * @iv: point to the initialization vector or counter 1149e4f86a8SGonglei * @src: point to the source data 1159e4f86a8SGonglei * @dst: point to the destination data 1169e4f86a8SGonglei * @aad_data: point to the additional authenticated data 1179e4f86a8SGonglei * @digest_result: point to the digest result data 1189e4f86a8SGonglei * @data[0]: point to the extensional memory by one memory allocation 1199e4f86a8SGonglei * 1209e4f86a8SGonglei */ 1219e4f86a8SGonglei typedef struct CryptoDevBackendSymOpInfo { 1229e4f86a8SGonglei uint64_t session_id; 1239e4f86a8SGonglei uint32_t aad_len; 1249e4f86a8SGonglei uint32_t iv_len; 1259e4f86a8SGonglei uint32_t src_len; 1269e4f86a8SGonglei uint32_t dst_len; 1279e4f86a8SGonglei uint32_t digest_result_len; 1289e4f86a8SGonglei uint32_t hash_start_src_offset; 1299e4f86a8SGonglei uint32_t cipher_start_src_offset; 1309e4f86a8SGonglei uint32_t len_to_hash; 1319e4f86a8SGonglei uint32_t len_to_cipher; 1329e4f86a8SGonglei uint8_t op_type; 1339e4f86a8SGonglei uint8_t *iv; 1349e4f86a8SGonglei uint8_t *src; 1359e4f86a8SGonglei uint8_t *dst; 1369e4f86a8SGonglei uint8_t *aad_data; 1379e4f86a8SGonglei uint8_t *digest_result; 138f7795e40SPhilippe Mathieu-Daudé uint8_t data[]; 1399e4f86a8SGonglei } CryptoDevBackendSymOpInfo; 140d0ee7a13SGonglei 141db1015e9SEduardo Habkost struct CryptoDevBackendClass { 142d0ee7a13SGonglei ObjectClass parent_class; 143d0ee7a13SGonglei 144d0ee7a13SGonglei void (*init)(CryptoDevBackend *backend, Error **errp); 145d0ee7a13SGonglei void (*cleanup)(CryptoDevBackend *backend, Error **errp); 1469e4f86a8SGonglei 1479e4f86a8SGonglei int64_t (*create_session)(CryptoDevBackend *backend, 1489e4f86a8SGonglei CryptoDevBackendSymSessionInfo *sess_info, 1499e4f86a8SGonglei uint32_t queue_index, Error **errp); 1509e4f86a8SGonglei int (*close_session)(CryptoDevBackend *backend, 1519e4f86a8SGonglei uint64_t session_id, 1529e4f86a8SGonglei uint32_t queue_index, Error **errp); 1539e4f86a8SGonglei int (*do_sym_op)(CryptoDevBackend *backend, 1549e4f86a8SGonglei CryptoDevBackendSymOpInfo *op_info, 1559e4f86a8SGonglei uint32_t queue_index, Error **errp); 156db1015e9SEduardo Habkost }; 157d0ee7a13SGonglei 1585da73dabSGonglei typedef enum CryptoDevBackendOptionsType { 1595da73dabSGonglei CRYPTODEV_BACKEND_TYPE_NONE = 0, 1605da73dabSGonglei CRYPTODEV_BACKEND_TYPE_BUILTIN = 1, 1615da73dabSGonglei CRYPTODEV_BACKEND_TYPE_VHOST_USER = 2, 1625da73dabSGonglei CRYPTODEV_BACKEND_TYPE__MAX, 1635da73dabSGonglei } CryptoDevBackendOptionsType; 164d0ee7a13SGonglei 165d0ee7a13SGonglei struct CryptoDevBackendClient { 1665da73dabSGonglei CryptoDevBackendOptionsType type; 167d0ee7a13SGonglei char *model; 168d0ee7a13SGonglei char *name; 169d0ee7a13SGonglei char *info_str; 170d0ee7a13SGonglei unsigned int queue_index; 1715da73dabSGonglei int vring_enable; 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; 20546fd1705SGonglei /* Tag the cryptodev backend is used by virtio-crypto or not */ 20646fd1705SGonglei bool is_used; 207d0ee7a13SGonglei CryptoDevBackendConf conf; 208d0ee7a13SGonglei }; 209d0ee7a13SGonglei 210d0ee7a13SGonglei /** 211d0ee7a13SGonglei * cryptodev_backend_new_client: 212d0ee7a13SGonglei * @model: the cryptodev backend model 213d0ee7a13SGonglei * @name: the cryptodev backend name, can be NULL 214d0ee7a13SGonglei * 215d0ee7a13SGonglei * Creates a new cryptodev backend client object 216d0ee7a13SGonglei * with the @name in the model @model. 217d0ee7a13SGonglei * 218d0ee7a13SGonglei * The returned object must be released with 219d0ee7a13SGonglei * cryptodev_backend_free_client() when no 220d0ee7a13SGonglei * longer required 221d0ee7a13SGonglei * 222d0ee7a13SGonglei * Returns: a new cryptodev backend client object 223d0ee7a13SGonglei */ 224d0ee7a13SGonglei CryptoDevBackendClient * 225d0ee7a13SGonglei cryptodev_backend_new_client(const char *model, 226d0ee7a13SGonglei const char *name); 227d0ee7a13SGonglei /** 228d0ee7a13SGonglei * cryptodev_backend_free_client: 229d0ee7a13SGonglei * @cc: the cryptodev backend client object 230d0ee7a13SGonglei * 231d0ee7a13SGonglei * Release the memory associated with @cc that 232d0ee7a13SGonglei * was previously allocated by cryptodev_backend_new_client() 233d0ee7a13SGonglei */ 234d0ee7a13SGonglei void cryptodev_backend_free_client( 235d0ee7a13SGonglei CryptoDevBackendClient *cc); 236d0ee7a13SGonglei 237d0ee7a13SGonglei /** 238d0ee7a13SGonglei * cryptodev_backend_cleanup: 239d0ee7a13SGonglei * @backend: the cryptodev backend object 240d0ee7a13SGonglei * @errp: pointer to a NULL-initialized error object 241d0ee7a13SGonglei * 242d0ee7a13SGonglei * Clean the resouce associated with @backend that realizaed 243d0ee7a13SGonglei * by the specific backend's init() callback 244d0ee7a13SGonglei */ 245d0ee7a13SGonglei void cryptodev_backend_cleanup( 246d0ee7a13SGonglei CryptoDevBackend *backend, 247d0ee7a13SGonglei Error **errp); 248d0ee7a13SGonglei 2499e4f86a8SGonglei /** 2509e4f86a8SGonglei * cryptodev_backend_sym_create_session: 2519e4f86a8SGonglei * @backend: the cryptodev backend object 2529e4f86a8SGonglei * @sess_info: parameters needed by session creating 2539e4f86a8SGonglei * @queue_index: queue index of cryptodev backend client 2549e4f86a8SGonglei * @errp: pointer to a NULL-initialized error object 2559e4f86a8SGonglei * 2569e4f86a8SGonglei * Create a session for symmetric algorithms 2579e4f86a8SGonglei * 2589e4f86a8SGonglei * Returns: session id on success, or -1 on error 2599e4f86a8SGonglei */ 2609e4f86a8SGonglei int64_t cryptodev_backend_sym_create_session( 2619e4f86a8SGonglei CryptoDevBackend *backend, 2629e4f86a8SGonglei CryptoDevBackendSymSessionInfo *sess_info, 2639e4f86a8SGonglei uint32_t queue_index, Error **errp); 2649e4f86a8SGonglei 2659e4f86a8SGonglei /** 2669e4f86a8SGonglei * cryptodev_backend_sym_close_session: 2679e4f86a8SGonglei * @backend: the cryptodev backend object 2689e4f86a8SGonglei * @session_id: the session id 2699e4f86a8SGonglei * @queue_index: queue index of cryptodev backend client 2709e4f86a8SGonglei * @errp: pointer to a NULL-initialized error object 2719e4f86a8SGonglei * 2729e4f86a8SGonglei * Close a session for symmetric algorithms which was previously 2739e4f86a8SGonglei * created by cryptodev_backend_sym_create_session() 2749e4f86a8SGonglei * 2759e4f86a8SGonglei * Returns: 0 on success, or Negative on error 2769e4f86a8SGonglei */ 2779e4f86a8SGonglei int cryptodev_backend_sym_close_session( 2789e4f86a8SGonglei CryptoDevBackend *backend, 2799e4f86a8SGonglei uint64_t session_id, 2809e4f86a8SGonglei uint32_t queue_index, Error **errp); 2819e4f86a8SGonglei 2829e4f86a8SGonglei /** 283d6634ac0SGonglei * cryptodev_backend_crypto_operation: 2849e4f86a8SGonglei * @backend: the cryptodev backend object 285d6634ac0SGonglei * @opaque: pointer to a VirtIOCryptoReq object 2869e4f86a8SGonglei * @queue_index: queue index of cryptodev backend client 2879e4f86a8SGonglei * @errp: pointer to a NULL-initialized error object 2889e4f86a8SGonglei * 289d6634ac0SGonglei * Do crypto operation, such as encryption and 2909e4f86a8SGonglei * decryption 2919e4f86a8SGonglei * 292d6634ac0SGonglei * Returns: VIRTIO_CRYPTO_OK on success, 293d6634ac0SGonglei * or -VIRTIO_CRYPTO_* on error 2949e4f86a8SGonglei */ 295d6634ac0SGonglei int cryptodev_backend_crypto_operation( 2969e4f86a8SGonglei CryptoDevBackend *backend, 297d6634ac0SGonglei void *opaque, 2989e4f86a8SGonglei uint32_t queue_index, Error **errp); 2999e4f86a8SGonglei 30046fd1705SGonglei /** 30146fd1705SGonglei * cryptodev_backend_set_used: 30246fd1705SGonglei * @backend: the cryptodev backend object 30346fd1705SGonglei * @used: ture or false 30446fd1705SGonglei * 30546fd1705SGonglei * Set the cryptodev backend is used by virtio-crypto or not 30646fd1705SGonglei */ 30746fd1705SGonglei void cryptodev_backend_set_used(CryptoDevBackend *backend, bool used); 30846fd1705SGonglei 30946fd1705SGonglei /** 31046fd1705SGonglei * cryptodev_backend_is_used: 31146fd1705SGonglei * @backend: the cryptodev backend object 31246fd1705SGonglei * 31346fd1705SGonglei * Return the status that the cryptodev backend is used 31446fd1705SGonglei * by virtio-crypto or not 31546fd1705SGonglei * 31646fd1705SGonglei * Returns: true on used, or false on not used 31746fd1705SGonglei */ 31846fd1705SGonglei bool cryptodev_backend_is_used(CryptoDevBackend *backend); 31946fd1705SGonglei 3206138dbdaSGonglei /** 3216138dbdaSGonglei * cryptodev_backend_set_ready: 3226138dbdaSGonglei * @backend: the cryptodev backend object 3236138dbdaSGonglei * @ready: ture or false 3246138dbdaSGonglei * 3256138dbdaSGonglei * Set the cryptodev backend is ready or not, which is called 3266138dbdaSGonglei * by the children of the cryptodev banckend interface. 3276138dbdaSGonglei */ 3286138dbdaSGonglei void cryptodev_backend_set_ready(CryptoDevBackend *backend, bool ready); 3296138dbdaSGonglei 3306138dbdaSGonglei /** 3316138dbdaSGonglei * cryptodev_backend_is_ready: 3326138dbdaSGonglei * @backend: the cryptodev backend object 3336138dbdaSGonglei * 3346138dbdaSGonglei * Return the status that the cryptodev backend is ready or not 3356138dbdaSGonglei * 3366138dbdaSGonglei * Returns: true on ready, or false on not ready 3376138dbdaSGonglei */ 3386138dbdaSGonglei bool cryptodev_backend_is_ready(CryptoDevBackend *backend); 33946fd1705SGonglei 340d0ee7a13SGonglei #endif /* CRYPTODEV_H */ 341