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 120dda001bSChetan Pant * version 2.1 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" 2814c9fd16Szhenwei pi #include "qapi/qapi-types-cryptodev.h" 29d0ee7a13SGonglei 30d0ee7a13SGonglei /** 31d0ee7a13SGonglei * CryptoDevBackend: 32d0ee7a13SGonglei * 33d0ee7a13SGonglei * The CryptoDevBackend object is an interface 34d0ee7a13SGonglei * for different cryptodev backends, which provides crypto 35d0ee7a13SGonglei * operation wrapper. 36d0ee7a13SGonglei * 37d0ee7a13SGonglei */ 38d0ee7a13SGonglei 39d0ee7a13SGonglei #define TYPE_CRYPTODEV_BACKEND "cryptodev-backend" 40d0ee7a13SGonglei 41c821774aSEduardo Habkost OBJECT_DECLARE_TYPE(CryptoDevBackend, CryptoDevBackendClass, 4230b5707cSEduardo Habkost CRYPTODEV_BACKEND) 43d0ee7a13SGonglei 44d0ee7a13SGonglei 45d0ee7a13SGonglei #define MAX_CRYPTO_QUEUE_NUM 64 46d0ee7a13SGonglei 47d0ee7a13SGonglei typedef struct CryptoDevBackendConf CryptoDevBackendConf; 48d0ee7a13SGonglei typedef struct CryptoDevBackendPeers CryptoDevBackendPeers; 49d0ee7a13SGonglei typedef struct CryptoDevBackendClient 50d0ee7a13SGonglei CryptoDevBackendClient; 51d0ee7a13SGonglei 529e4f86a8SGonglei /** 539e4f86a8SGonglei * CryptoDevBackendSymSessionInfo: 549e4f86a8SGonglei * 559e4f86a8SGonglei * @cipher_alg: algorithm type of CIPHER 569e4f86a8SGonglei * @key_len: byte length of cipher key 579e4f86a8SGonglei * @hash_alg: algorithm type of HASH/MAC 589e4f86a8SGonglei * @hash_result_len: byte length of HASH operation result 599e4f86a8SGonglei * @auth_key_len: byte length of authenticated key 609e4f86a8SGonglei * @add_len: byte length of additional authenticated data 619e4f86a8SGonglei * @op_type: operation type (refer to virtio_crypto.h) 629e4f86a8SGonglei * @direction: encryption or direction for CIPHER 639e4f86a8SGonglei * @hash_mode: HASH mode for HASH operation (refer to virtio_crypto.h) 649e4f86a8SGonglei * @alg_chain_order: order of algorithm chaining (CIPHER then HASH, 659e4f86a8SGonglei * or HASH then CIPHER) 669e4f86a8SGonglei * @cipher_key: point to a key of CIPHER 679e4f86a8SGonglei * @auth_key: point to an authenticated key of MAC 689e4f86a8SGonglei * 699e4f86a8SGonglei */ 709e4f86a8SGonglei typedef struct CryptoDevBackendSymSessionInfo { 719e4f86a8SGonglei /* corresponding with virtio crypto spec */ 729e4f86a8SGonglei uint32_t cipher_alg; 739e4f86a8SGonglei uint32_t key_len; 749e4f86a8SGonglei uint32_t hash_alg; 759e4f86a8SGonglei uint32_t hash_result_len; 769e4f86a8SGonglei uint32_t auth_key_len; 779e4f86a8SGonglei uint32_t add_len; 789e4f86a8SGonglei uint8_t op_type; 799e4f86a8SGonglei uint8_t direction; 809e4f86a8SGonglei uint8_t hash_mode; 819e4f86a8SGonglei uint8_t alg_chain_order; 829e4f86a8SGonglei uint8_t *cipher_key; 839e4f86a8SGonglei uint8_t *auth_key; 849e4f86a8SGonglei } CryptoDevBackendSymSessionInfo; 859e4f86a8SGonglei 869e4f86a8SGonglei /** 870e660a6fSzhenwei pi * CryptoDevBackendAsymSessionInfo: 880e660a6fSzhenwei pi */ 890e660a6fSzhenwei pi typedef struct CryptoDevBackendRsaPara { 900e660a6fSzhenwei pi uint32_t padding_algo; 910e660a6fSzhenwei pi uint32_t hash_algo; 920e660a6fSzhenwei pi } CryptoDevBackendRsaPara; 930e660a6fSzhenwei pi 940e660a6fSzhenwei pi typedef struct CryptoDevBackendAsymSessionInfo { 950e660a6fSzhenwei pi /* corresponding with virtio crypto spec */ 960e660a6fSzhenwei pi uint32_t algo; 970e660a6fSzhenwei pi uint32_t keytype; 980e660a6fSzhenwei pi uint32_t keylen; 990e660a6fSzhenwei pi uint8_t *key; 1000e660a6fSzhenwei pi union { 1010e660a6fSzhenwei pi CryptoDevBackendRsaPara rsa; 1020e660a6fSzhenwei pi } u; 1030e660a6fSzhenwei pi } CryptoDevBackendAsymSessionInfo; 1040e660a6fSzhenwei pi 1050e660a6fSzhenwei pi typedef struct CryptoDevBackendSessionInfo { 1060e660a6fSzhenwei pi uint32_t op_code; 1070e660a6fSzhenwei pi union { 1080e660a6fSzhenwei pi CryptoDevBackendSymSessionInfo sym_sess_info; 1090e660a6fSzhenwei pi CryptoDevBackendAsymSessionInfo asym_sess_info; 1100e660a6fSzhenwei pi } u; 1112fda101dSLei He uint64_t session_id; 1120e660a6fSzhenwei pi } CryptoDevBackendSessionInfo; 1130e660a6fSzhenwei pi 1140e660a6fSzhenwei pi /** 1159e4f86a8SGonglei * CryptoDevBackendSymOpInfo: 1169e4f86a8SGonglei * 1179e4f86a8SGonglei * @aad_len: byte length of additional authenticated data 1189e4f86a8SGonglei * @iv_len: byte length of initialization vector or counter 1199e4f86a8SGonglei * @src_len: byte length of source data 1209e4f86a8SGonglei * @dst_len: byte length of destination data 1219e4f86a8SGonglei * @digest_result_len: byte length of hash digest result 1229e4f86a8SGonglei * @hash_start_src_offset: Starting point for hash processing, specified 1239e4f86a8SGonglei * as number of bytes from start of packet in source data, only used for 1249e4f86a8SGonglei * algorithm chain 1259e4f86a8SGonglei * @cipher_start_src_offset: Starting point for cipher processing, specified 1269e4f86a8SGonglei * as number of bytes from start of packet in source data, only used for 1279e4f86a8SGonglei * algorithm chain 1289e4f86a8SGonglei * @len_to_hash: byte length of source data on which the hash 1299e4f86a8SGonglei * operation will be computed, only used for algorithm chain 1309e4f86a8SGonglei * @len_to_cipher: byte length of source data on which the cipher 1319e4f86a8SGonglei * operation will be computed, only used for algorithm chain 1329e4f86a8SGonglei * @op_type: operation type (refer to virtio_crypto.h) 1339e4f86a8SGonglei * @iv: point to the initialization vector or counter 1349e4f86a8SGonglei * @src: point to the source data 1359e4f86a8SGonglei * @dst: point to the destination data 1369e4f86a8SGonglei * @aad_data: point to the additional authenticated data 1379e4f86a8SGonglei * @digest_result: point to the digest result data 1389e4f86a8SGonglei * @data[0]: point to the extensional memory by one memory allocation 1399e4f86a8SGonglei * 1409e4f86a8SGonglei */ 1419e4f86a8SGonglei typedef struct CryptoDevBackendSymOpInfo { 1429e4f86a8SGonglei uint32_t aad_len; 1439e4f86a8SGonglei uint32_t iv_len; 1449e4f86a8SGonglei uint32_t src_len; 1459e4f86a8SGonglei uint32_t dst_len; 1469e4f86a8SGonglei uint32_t digest_result_len; 1479e4f86a8SGonglei uint32_t hash_start_src_offset; 1489e4f86a8SGonglei uint32_t cipher_start_src_offset; 1499e4f86a8SGonglei uint32_t len_to_hash; 1509e4f86a8SGonglei uint32_t len_to_cipher; 1519e4f86a8SGonglei uint8_t op_type; 1529e4f86a8SGonglei uint8_t *iv; 1539e4f86a8SGonglei uint8_t *src; 1549e4f86a8SGonglei uint8_t *dst; 1559e4f86a8SGonglei uint8_t *aad_data; 1569e4f86a8SGonglei uint8_t *digest_result; 157f7795e40SPhilippe Mathieu-Daudé uint8_t data[]; 1589e4f86a8SGonglei } CryptoDevBackendSymOpInfo; 159d0ee7a13SGonglei 1600e660a6fSzhenwei pi 1610e660a6fSzhenwei pi /** 1620e660a6fSzhenwei pi * CryptoDevBackendAsymOpInfo: 1630e660a6fSzhenwei pi * 1640e660a6fSzhenwei pi * @src_len: byte length of source data 1650e660a6fSzhenwei pi * @dst_len: byte length of destination data 1660e660a6fSzhenwei pi * @src: point to the source data 1670e660a6fSzhenwei pi * @dst: point to the destination data 1680e660a6fSzhenwei pi * 1690e660a6fSzhenwei pi */ 1700e660a6fSzhenwei pi typedef struct CryptoDevBackendAsymOpInfo { 1710e660a6fSzhenwei pi uint32_t src_len; 1720e660a6fSzhenwei pi uint32_t dst_len; 1730e660a6fSzhenwei pi uint8_t *src; 1740e660a6fSzhenwei pi uint8_t *dst; 1750e660a6fSzhenwei pi } CryptoDevBackendAsymOpInfo; 1760e660a6fSzhenwei pi 1770e660a6fSzhenwei pi typedef struct CryptoDevBackendOpInfo { 178*999c789fSzhenwei pi QCryptodevBackendAlgType algtype; 1790e660a6fSzhenwei pi uint32_t op_code; 1800e660a6fSzhenwei pi uint64_t session_id; 1810e660a6fSzhenwei pi union { 1820e660a6fSzhenwei pi CryptoDevBackendSymOpInfo *sym_op_info; 1830e660a6fSzhenwei pi CryptoDevBackendAsymOpInfo *asym_op_info; 1840e660a6fSzhenwei pi } u; 1850e660a6fSzhenwei pi } CryptoDevBackendOpInfo; 1860e660a6fSzhenwei pi 1872fda101dSLei He typedef void (*CryptoDevCompletionFunc) (void *opaque, int ret); 188db1015e9SEduardo Habkost struct CryptoDevBackendClass { 189d0ee7a13SGonglei ObjectClass parent_class; 190d0ee7a13SGonglei 191d0ee7a13SGonglei void (*init)(CryptoDevBackend *backend, Error **errp); 192d0ee7a13SGonglei void (*cleanup)(CryptoDevBackend *backend, Error **errp); 1939e4f86a8SGonglei 1942fda101dSLei He int (*create_session)(CryptoDevBackend *backend, 1950e660a6fSzhenwei pi CryptoDevBackendSessionInfo *sess_info, 1962fda101dSLei He uint32_t queue_index, 1972fda101dSLei He CryptoDevCompletionFunc cb, 1982fda101dSLei He void *opaque); 1992fda101dSLei He 2009e4f86a8SGonglei int (*close_session)(CryptoDevBackend *backend, 2019e4f86a8SGonglei uint64_t session_id, 2022fda101dSLei He uint32_t queue_index, 2032fda101dSLei He CryptoDevCompletionFunc cb, 2042fda101dSLei He void *opaque); 2052fda101dSLei He 2060e660a6fSzhenwei pi int (*do_op)(CryptoDevBackend *backend, 2070e660a6fSzhenwei pi CryptoDevBackendOpInfo *op_info, 2082fda101dSLei He uint32_t queue_index, 2092fda101dSLei He CryptoDevCompletionFunc cb, 2102fda101dSLei He void *opaque); 211db1015e9SEduardo Habkost }; 212d0ee7a13SGonglei 213d0ee7a13SGonglei struct CryptoDevBackendClient { 21414c9fd16Szhenwei pi QCryptodevBackendType type; 215d0ee7a13SGonglei char *info_str; 216d0ee7a13SGonglei unsigned int queue_index; 2175da73dabSGonglei int vring_enable; 218d0ee7a13SGonglei QTAILQ_ENTRY(CryptoDevBackendClient) next; 219d0ee7a13SGonglei }; 220d0ee7a13SGonglei 221d0ee7a13SGonglei struct CryptoDevBackendPeers { 222d0ee7a13SGonglei CryptoDevBackendClient *ccs[MAX_CRYPTO_QUEUE_NUM]; 223d0ee7a13SGonglei uint32_t queues; 224d0ee7a13SGonglei }; 225d0ee7a13SGonglei 226d0ee7a13SGonglei struct CryptoDevBackendConf { 227d0ee7a13SGonglei CryptoDevBackendPeers peers; 228d0ee7a13SGonglei 229d0ee7a13SGonglei /* Supported service mask */ 230d0ee7a13SGonglei uint32_t crypto_services; 231d0ee7a13SGonglei 232d0ee7a13SGonglei /* Detailed algorithms mask */ 233d0ee7a13SGonglei uint32_t cipher_algo_l; 234d0ee7a13SGonglei uint32_t cipher_algo_h; 235d0ee7a13SGonglei uint32_t hash_algo; 236d0ee7a13SGonglei uint32_t mac_algo_l; 237d0ee7a13SGonglei uint32_t mac_algo_h; 238d0ee7a13SGonglei uint32_t aead_algo; 2390e660a6fSzhenwei pi uint32_t akcipher_algo; 240d0ee7a13SGonglei /* Maximum length of cipher key */ 241d0ee7a13SGonglei uint32_t max_cipher_key_len; 242d0ee7a13SGonglei /* Maximum length of authenticated key */ 243d0ee7a13SGonglei uint32_t max_auth_key_len; 244d0ee7a13SGonglei /* Maximum size of each crypto request's content */ 245d0ee7a13SGonglei uint64_t max_size; 246d0ee7a13SGonglei }; 247d0ee7a13SGonglei 248d0ee7a13SGonglei struct CryptoDevBackend { 249d0ee7a13SGonglei Object parent_obj; 250d0ee7a13SGonglei 251d0ee7a13SGonglei bool ready; 25246fd1705SGonglei /* Tag the cryptodev backend is used by virtio-crypto or not */ 25346fd1705SGonglei bool is_used; 254d0ee7a13SGonglei CryptoDevBackendConf conf; 255d0ee7a13SGonglei }; 256d0ee7a13SGonglei 257d0ee7a13SGonglei /** 258d0ee7a13SGonglei * cryptodev_backend_new_client: 259d0ee7a13SGonglei * 2603f478371Szhenwei pi * Creates a new cryptodev backend client object. 261d0ee7a13SGonglei * 262d0ee7a13SGonglei * The returned object must be released with 263d0ee7a13SGonglei * cryptodev_backend_free_client() when no 264d0ee7a13SGonglei * longer required 265d0ee7a13SGonglei * 266d0ee7a13SGonglei * Returns: a new cryptodev backend client object 267d0ee7a13SGonglei */ 2683f478371Szhenwei pi CryptoDevBackendClient *cryptodev_backend_new_client(void); 2693f478371Szhenwei pi 270d0ee7a13SGonglei /** 271d0ee7a13SGonglei * cryptodev_backend_free_client: 272d0ee7a13SGonglei * @cc: the cryptodev backend client object 273d0ee7a13SGonglei * 274d0ee7a13SGonglei * Release the memory associated with @cc that 275d0ee7a13SGonglei * was previously allocated by cryptodev_backend_new_client() 276d0ee7a13SGonglei */ 277d0ee7a13SGonglei void cryptodev_backend_free_client( 278d0ee7a13SGonglei CryptoDevBackendClient *cc); 279d0ee7a13SGonglei 280d0ee7a13SGonglei /** 281d0ee7a13SGonglei * cryptodev_backend_cleanup: 282d0ee7a13SGonglei * @backend: the cryptodev backend object 283d0ee7a13SGonglei * @errp: pointer to a NULL-initialized error object 284d0ee7a13SGonglei * 285d0ee7a13SGonglei * Clean the resouce associated with @backend that realizaed 286d0ee7a13SGonglei * by the specific backend's init() callback 287d0ee7a13SGonglei */ 288d0ee7a13SGonglei void cryptodev_backend_cleanup( 289d0ee7a13SGonglei CryptoDevBackend *backend, 290d0ee7a13SGonglei Error **errp); 291d0ee7a13SGonglei 2929e4f86a8SGonglei /** 2930e660a6fSzhenwei pi * cryptodev_backend_create_session: 2949e4f86a8SGonglei * @backend: the cryptodev backend object 2959e4f86a8SGonglei * @sess_info: parameters needed by session creating 2969e4f86a8SGonglei * @queue_index: queue index of cryptodev backend client 2979e4f86a8SGonglei * @errp: pointer to a NULL-initialized error object 2982fda101dSLei He * @cb: callback when session create is compeleted 2992fda101dSLei He * @opaque: parameter passed to callback 3009e4f86a8SGonglei * 3012fda101dSLei He * Create a session for symmetric/asymmetric algorithms 3029e4f86a8SGonglei * 3032fda101dSLei He * Returns: 0 for success and cb will be called when creation is completed, 3042fda101dSLei He * negative value for error, and cb will not be called. 3059e4f86a8SGonglei */ 3062fda101dSLei He int cryptodev_backend_create_session( 3079e4f86a8SGonglei CryptoDevBackend *backend, 3080e660a6fSzhenwei pi CryptoDevBackendSessionInfo *sess_info, 3092fda101dSLei He uint32_t queue_index, 3102fda101dSLei He CryptoDevCompletionFunc cb, 3112fda101dSLei He void *opaque); 3129e4f86a8SGonglei 3139e4f86a8SGonglei /** 3140e660a6fSzhenwei pi * cryptodev_backend_close_session: 3159e4f86a8SGonglei * @backend: the cryptodev backend object 3169e4f86a8SGonglei * @session_id: the session id 3179e4f86a8SGonglei * @queue_index: queue index of cryptodev backend client 3189e4f86a8SGonglei * @errp: pointer to a NULL-initialized error object 3192fda101dSLei He * @cb: callback when session create is compeleted 3202fda101dSLei He * @opaque: parameter passed to callback 3219e4f86a8SGonglei * 3220e660a6fSzhenwei pi * Close a session for which was previously 3230e660a6fSzhenwei pi * created by cryptodev_backend_create_session() 3249e4f86a8SGonglei * 3252fda101dSLei He * Returns: 0 for success and cb will be called when creation is completed, 3262fda101dSLei He * negative value for error, and cb will not be called. 3279e4f86a8SGonglei */ 3280e660a6fSzhenwei pi int cryptodev_backend_close_session( 3299e4f86a8SGonglei CryptoDevBackend *backend, 3309e4f86a8SGonglei uint64_t session_id, 3312fda101dSLei He uint32_t queue_index, 3322fda101dSLei He CryptoDevCompletionFunc cb, 3332fda101dSLei He void *opaque); 3349e4f86a8SGonglei 3359e4f86a8SGonglei /** 336d6634ac0SGonglei * cryptodev_backend_crypto_operation: 3379e4f86a8SGonglei * @backend: the cryptodev backend object 3382fda101dSLei He * @opaque1: pointer to a VirtIOCryptoReq object 3399e4f86a8SGonglei * @queue_index: queue index of cryptodev backend client 3409e4f86a8SGonglei * @errp: pointer to a NULL-initialized error object 3412fda101dSLei He * @cb: callbacks when operation is completed 3422fda101dSLei He * @opaque2: parameter passed to cb 3439e4f86a8SGonglei * 344d6634ac0SGonglei * Do crypto operation, such as encryption and 3459e4f86a8SGonglei * decryption 3469e4f86a8SGonglei * 3472fda101dSLei He * Returns: 0 for success and cb will be called when creation is completed, 3482fda101dSLei He * negative value for error, and cb will not be called. 3499e4f86a8SGonglei */ 350d6634ac0SGonglei int cryptodev_backend_crypto_operation( 3519e4f86a8SGonglei CryptoDevBackend *backend, 3522fda101dSLei He void *opaque1, 3532fda101dSLei He uint32_t queue_index, 3542fda101dSLei He CryptoDevCompletionFunc cb, 3552fda101dSLei He void *opaque2); 3569e4f86a8SGonglei 35746fd1705SGonglei /** 35846fd1705SGonglei * cryptodev_backend_set_used: 35946fd1705SGonglei * @backend: the cryptodev backend object 36046fd1705SGonglei * @used: ture or false 36146fd1705SGonglei * 36246fd1705SGonglei * Set the cryptodev backend is used by virtio-crypto or not 36346fd1705SGonglei */ 36446fd1705SGonglei void cryptodev_backend_set_used(CryptoDevBackend *backend, bool used); 36546fd1705SGonglei 36646fd1705SGonglei /** 36746fd1705SGonglei * cryptodev_backend_is_used: 36846fd1705SGonglei * @backend: the cryptodev backend object 36946fd1705SGonglei * 37046fd1705SGonglei * Return the status that the cryptodev backend is used 37146fd1705SGonglei * by virtio-crypto or not 37246fd1705SGonglei * 37346fd1705SGonglei * Returns: true on used, or false on not used 37446fd1705SGonglei */ 37546fd1705SGonglei bool cryptodev_backend_is_used(CryptoDevBackend *backend); 37646fd1705SGonglei 3776138dbdaSGonglei /** 3786138dbdaSGonglei * cryptodev_backend_set_ready: 3796138dbdaSGonglei * @backend: the cryptodev backend object 3806138dbdaSGonglei * @ready: ture or false 3816138dbdaSGonglei * 3826138dbdaSGonglei * Set the cryptodev backend is ready or not, which is called 3836138dbdaSGonglei * by the children of the cryptodev banckend interface. 3846138dbdaSGonglei */ 3856138dbdaSGonglei void cryptodev_backend_set_ready(CryptoDevBackend *backend, bool ready); 3866138dbdaSGonglei 3876138dbdaSGonglei /** 3886138dbdaSGonglei * cryptodev_backend_is_ready: 3896138dbdaSGonglei * @backend: the cryptodev backend object 3906138dbdaSGonglei * 3916138dbdaSGonglei * Return the status that the cryptodev backend is ready or not 3926138dbdaSGonglei * 3936138dbdaSGonglei * Returns: true on ready, or false on not ready 3946138dbdaSGonglei */ 3956138dbdaSGonglei bool cryptodev_backend_is_ready(CryptoDevBackend *backend); 39646fd1705SGonglei 397d0ee7a13SGonglei #endif /* CRYPTODEV_H */ 398