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 enum CryptoDevBackendAlgType { 539e4f86a8SGonglei CRYPTODEV_BACKEND_ALG_SYM, 540e660a6fSzhenwei pi CRYPTODEV_BACKEND_ALG_ASYM, 559e4f86a8SGonglei CRYPTODEV_BACKEND_ALG__MAX, 569e4f86a8SGonglei }; 579e4f86a8SGonglei 589e4f86a8SGonglei /** 599e4f86a8SGonglei * CryptoDevBackendSymSessionInfo: 609e4f86a8SGonglei * 619e4f86a8SGonglei * @cipher_alg: algorithm type of CIPHER 629e4f86a8SGonglei * @key_len: byte length of cipher key 639e4f86a8SGonglei * @hash_alg: algorithm type of HASH/MAC 649e4f86a8SGonglei * @hash_result_len: byte length of HASH operation result 659e4f86a8SGonglei * @auth_key_len: byte length of authenticated key 669e4f86a8SGonglei * @add_len: byte length of additional authenticated data 679e4f86a8SGonglei * @op_type: operation type (refer to virtio_crypto.h) 689e4f86a8SGonglei * @direction: encryption or direction for CIPHER 699e4f86a8SGonglei * @hash_mode: HASH mode for HASH operation (refer to virtio_crypto.h) 709e4f86a8SGonglei * @alg_chain_order: order of algorithm chaining (CIPHER then HASH, 719e4f86a8SGonglei * or HASH then CIPHER) 729e4f86a8SGonglei * @cipher_key: point to a key of CIPHER 739e4f86a8SGonglei * @auth_key: point to an authenticated key of MAC 749e4f86a8SGonglei * 759e4f86a8SGonglei */ 769e4f86a8SGonglei typedef struct CryptoDevBackendSymSessionInfo { 779e4f86a8SGonglei /* corresponding with virtio crypto spec */ 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 /** 930e660a6fSzhenwei pi * CryptoDevBackendAsymSessionInfo: 940e660a6fSzhenwei pi */ 950e660a6fSzhenwei pi typedef struct CryptoDevBackendRsaPara { 960e660a6fSzhenwei pi uint32_t padding_algo; 970e660a6fSzhenwei pi uint32_t hash_algo; 980e660a6fSzhenwei pi } CryptoDevBackendRsaPara; 990e660a6fSzhenwei pi 1000e660a6fSzhenwei pi typedef struct CryptoDevBackendAsymSessionInfo { 1010e660a6fSzhenwei pi /* corresponding with virtio crypto spec */ 1020e660a6fSzhenwei pi uint32_t algo; 1030e660a6fSzhenwei pi uint32_t keytype; 1040e660a6fSzhenwei pi uint32_t keylen; 1050e660a6fSzhenwei pi uint8_t *key; 1060e660a6fSzhenwei pi union { 1070e660a6fSzhenwei pi CryptoDevBackendRsaPara rsa; 1080e660a6fSzhenwei pi } u; 1090e660a6fSzhenwei pi } CryptoDevBackendAsymSessionInfo; 1100e660a6fSzhenwei pi 1110e660a6fSzhenwei pi typedef struct CryptoDevBackendSessionInfo { 1120e660a6fSzhenwei pi uint32_t op_code; 1130e660a6fSzhenwei pi union { 1140e660a6fSzhenwei pi CryptoDevBackendSymSessionInfo sym_sess_info; 1150e660a6fSzhenwei pi CryptoDevBackendAsymSessionInfo asym_sess_info; 1160e660a6fSzhenwei pi } u; 1172fda101dSLei He uint64_t session_id; 1180e660a6fSzhenwei pi } CryptoDevBackendSessionInfo; 1190e660a6fSzhenwei pi 1200e660a6fSzhenwei pi /** 1219e4f86a8SGonglei * CryptoDevBackendSymOpInfo: 1229e4f86a8SGonglei * 1239e4f86a8SGonglei * @aad_len: byte length of additional authenticated data 1249e4f86a8SGonglei * @iv_len: byte length of initialization vector or counter 1259e4f86a8SGonglei * @src_len: byte length of source data 1269e4f86a8SGonglei * @dst_len: byte length of destination data 1279e4f86a8SGonglei * @digest_result_len: byte length of hash digest result 1289e4f86a8SGonglei * @hash_start_src_offset: Starting point for hash processing, specified 1299e4f86a8SGonglei * as number of bytes from start of packet in source data, only used for 1309e4f86a8SGonglei * algorithm chain 1319e4f86a8SGonglei * @cipher_start_src_offset: Starting point for cipher processing, specified 1329e4f86a8SGonglei * as number of bytes from start of packet in source data, only used for 1339e4f86a8SGonglei * algorithm chain 1349e4f86a8SGonglei * @len_to_hash: byte length of source data on which the hash 1359e4f86a8SGonglei * operation will be computed, only used for algorithm chain 1369e4f86a8SGonglei * @len_to_cipher: byte length of source data on which the cipher 1379e4f86a8SGonglei * operation will be computed, only used for algorithm chain 1389e4f86a8SGonglei * @op_type: operation type (refer to virtio_crypto.h) 1399e4f86a8SGonglei * @iv: point to the initialization vector or counter 1409e4f86a8SGonglei * @src: point to the source data 1419e4f86a8SGonglei * @dst: point to the destination data 1429e4f86a8SGonglei * @aad_data: point to the additional authenticated data 1439e4f86a8SGonglei * @digest_result: point to the digest result data 1449e4f86a8SGonglei * @data[0]: point to the extensional memory by one memory allocation 1459e4f86a8SGonglei * 1469e4f86a8SGonglei */ 1479e4f86a8SGonglei typedef struct CryptoDevBackendSymOpInfo { 1489e4f86a8SGonglei uint32_t aad_len; 1499e4f86a8SGonglei uint32_t iv_len; 1509e4f86a8SGonglei uint32_t src_len; 1519e4f86a8SGonglei uint32_t dst_len; 1529e4f86a8SGonglei uint32_t digest_result_len; 1539e4f86a8SGonglei uint32_t hash_start_src_offset; 1549e4f86a8SGonglei uint32_t cipher_start_src_offset; 1559e4f86a8SGonglei uint32_t len_to_hash; 1569e4f86a8SGonglei uint32_t len_to_cipher; 1579e4f86a8SGonglei uint8_t op_type; 1589e4f86a8SGonglei uint8_t *iv; 1599e4f86a8SGonglei uint8_t *src; 1609e4f86a8SGonglei uint8_t *dst; 1619e4f86a8SGonglei uint8_t *aad_data; 1629e4f86a8SGonglei uint8_t *digest_result; 163f7795e40SPhilippe Mathieu-Daudé uint8_t data[]; 1649e4f86a8SGonglei } CryptoDevBackendSymOpInfo; 165d0ee7a13SGonglei 1660e660a6fSzhenwei pi 1670e660a6fSzhenwei pi /** 1680e660a6fSzhenwei pi * CryptoDevBackendAsymOpInfo: 1690e660a6fSzhenwei pi * 1700e660a6fSzhenwei pi * @src_len: byte length of source data 1710e660a6fSzhenwei pi * @dst_len: byte length of destination data 1720e660a6fSzhenwei pi * @src: point to the source data 1730e660a6fSzhenwei pi * @dst: point to the destination data 1740e660a6fSzhenwei pi * 1750e660a6fSzhenwei pi */ 1760e660a6fSzhenwei pi typedef struct CryptoDevBackendAsymOpInfo { 1770e660a6fSzhenwei pi uint32_t src_len; 1780e660a6fSzhenwei pi uint32_t dst_len; 1790e660a6fSzhenwei pi uint8_t *src; 1800e660a6fSzhenwei pi uint8_t *dst; 1810e660a6fSzhenwei pi } CryptoDevBackendAsymOpInfo; 1820e660a6fSzhenwei pi 1830e660a6fSzhenwei pi typedef struct CryptoDevBackendOpInfo { 1840e660a6fSzhenwei pi enum CryptoDevBackendAlgType algtype; 1850e660a6fSzhenwei pi uint32_t op_code; 1860e660a6fSzhenwei pi uint64_t session_id; 1870e660a6fSzhenwei pi union { 1880e660a6fSzhenwei pi CryptoDevBackendSymOpInfo *sym_op_info; 1890e660a6fSzhenwei pi CryptoDevBackendAsymOpInfo *asym_op_info; 1900e660a6fSzhenwei pi } u; 1910e660a6fSzhenwei pi } CryptoDevBackendOpInfo; 1920e660a6fSzhenwei pi 1932fda101dSLei He typedef void (*CryptoDevCompletionFunc) (void *opaque, int ret); 194db1015e9SEduardo Habkost struct CryptoDevBackendClass { 195d0ee7a13SGonglei ObjectClass parent_class; 196d0ee7a13SGonglei 197d0ee7a13SGonglei void (*init)(CryptoDevBackend *backend, Error **errp); 198d0ee7a13SGonglei void (*cleanup)(CryptoDevBackend *backend, Error **errp); 1999e4f86a8SGonglei 2002fda101dSLei He int (*create_session)(CryptoDevBackend *backend, 2010e660a6fSzhenwei pi CryptoDevBackendSessionInfo *sess_info, 2022fda101dSLei He uint32_t queue_index, 2032fda101dSLei He CryptoDevCompletionFunc cb, 2042fda101dSLei He void *opaque); 2052fda101dSLei He 2069e4f86a8SGonglei int (*close_session)(CryptoDevBackend *backend, 2079e4f86a8SGonglei uint64_t session_id, 2082fda101dSLei He uint32_t queue_index, 2092fda101dSLei He CryptoDevCompletionFunc cb, 2102fda101dSLei He void *opaque); 2112fda101dSLei He 2120e660a6fSzhenwei pi int (*do_op)(CryptoDevBackend *backend, 2130e660a6fSzhenwei pi CryptoDevBackendOpInfo *op_info, 2142fda101dSLei He uint32_t queue_index, 2152fda101dSLei He CryptoDevCompletionFunc cb, 2162fda101dSLei He void *opaque); 217db1015e9SEduardo Habkost }; 218d0ee7a13SGonglei 219d0ee7a13SGonglei struct CryptoDevBackendClient { 22014c9fd16Szhenwei pi QCryptodevBackendType type; 221d0ee7a13SGonglei char *info_str; 222d0ee7a13SGonglei unsigned int queue_index; 2235da73dabSGonglei int vring_enable; 224d0ee7a13SGonglei QTAILQ_ENTRY(CryptoDevBackendClient) next; 225d0ee7a13SGonglei }; 226d0ee7a13SGonglei 227d0ee7a13SGonglei struct CryptoDevBackendPeers { 228d0ee7a13SGonglei CryptoDevBackendClient *ccs[MAX_CRYPTO_QUEUE_NUM]; 229d0ee7a13SGonglei uint32_t queues; 230d0ee7a13SGonglei }; 231d0ee7a13SGonglei 232d0ee7a13SGonglei struct CryptoDevBackendConf { 233d0ee7a13SGonglei CryptoDevBackendPeers peers; 234d0ee7a13SGonglei 235d0ee7a13SGonglei /* Supported service mask */ 236d0ee7a13SGonglei uint32_t crypto_services; 237d0ee7a13SGonglei 238d0ee7a13SGonglei /* Detailed algorithms mask */ 239d0ee7a13SGonglei uint32_t cipher_algo_l; 240d0ee7a13SGonglei uint32_t cipher_algo_h; 241d0ee7a13SGonglei uint32_t hash_algo; 242d0ee7a13SGonglei uint32_t mac_algo_l; 243d0ee7a13SGonglei uint32_t mac_algo_h; 244d0ee7a13SGonglei uint32_t aead_algo; 2450e660a6fSzhenwei pi uint32_t akcipher_algo; 246d0ee7a13SGonglei /* Maximum length of cipher key */ 247d0ee7a13SGonglei uint32_t max_cipher_key_len; 248d0ee7a13SGonglei /* Maximum length of authenticated key */ 249d0ee7a13SGonglei uint32_t max_auth_key_len; 250d0ee7a13SGonglei /* Maximum size of each crypto request's content */ 251d0ee7a13SGonglei uint64_t max_size; 252d0ee7a13SGonglei }; 253d0ee7a13SGonglei 254d0ee7a13SGonglei struct CryptoDevBackend { 255d0ee7a13SGonglei Object parent_obj; 256d0ee7a13SGonglei 257d0ee7a13SGonglei bool ready; 25846fd1705SGonglei /* Tag the cryptodev backend is used by virtio-crypto or not */ 25946fd1705SGonglei bool is_used; 260d0ee7a13SGonglei CryptoDevBackendConf conf; 261d0ee7a13SGonglei }; 262d0ee7a13SGonglei 263d0ee7a13SGonglei /** 264d0ee7a13SGonglei * cryptodev_backend_new_client: 265d0ee7a13SGonglei * 266*3f478371Szhenwei pi * Creates a new cryptodev backend client object. 267d0ee7a13SGonglei * 268d0ee7a13SGonglei * The returned object must be released with 269d0ee7a13SGonglei * cryptodev_backend_free_client() when no 270d0ee7a13SGonglei * longer required 271d0ee7a13SGonglei * 272d0ee7a13SGonglei * Returns: a new cryptodev backend client object 273d0ee7a13SGonglei */ 274*3f478371Szhenwei pi CryptoDevBackendClient *cryptodev_backend_new_client(void); 275*3f478371Szhenwei pi 276d0ee7a13SGonglei /** 277d0ee7a13SGonglei * cryptodev_backend_free_client: 278d0ee7a13SGonglei * @cc: the cryptodev backend client object 279d0ee7a13SGonglei * 280d0ee7a13SGonglei * Release the memory associated with @cc that 281d0ee7a13SGonglei * was previously allocated by cryptodev_backend_new_client() 282d0ee7a13SGonglei */ 283d0ee7a13SGonglei void cryptodev_backend_free_client( 284d0ee7a13SGonglei CryptoDevBackendClient *cc); 285d0ee7a13SGonglei 286d0ee7a13SGonglei /** 287d0ee7a13SGonglei * cryptodev_backend_cleanup: 288d0ee7a13SGonglei * @backend: the cryptodev backend object 289d0ee7a13SGonglei * @errp: pointer to a NULL-initialized error object 290d0ee7a13SGonglei * 291d0ee7a13SGonglei * Clean the resouce associated with @backend that realizaed 292d0ee7a13SGonglei * by the specific backend's init() callback 293d0ee7a13SGonglei */ 294d0ee7a13SGonglei void cryptodev_backend_cleanup( 295d0ee7a13SGonglei CryptoDevBackend *backend, 296d0ee7a13SGonglei Error **errp); 297d0ee7a13SGonglei 2989e4f86a8SGonglei /** 2990e660a6fSzhenwei pi * cryptodev_backend_create_session: 3009e4f86a8SGonglei * @backend: the cryptodev backend object 3019e4f86a8SGonglei * @sess_info: parameters needed by session creating 3029e4f86a8SGonglei * @queue_index: queue index of cryptodev backend client 3039e4f86a8SGonglei * @errp: pointer to a NULL-initialized error object 3042fda101dSLei He * @cb: callback when session create is compeleted 3052fda101dSLei He * @opaque: parameter passed to callback 3069e4f86a8SGonglei * 3072fda101dSLei He * Create a session for symmetric/asymmetric algorithms 3089e4f86a8SGonglei * 3092fda101dSLei He * Returns: 0 for success and cb will be called when creation is completed, 3102fda101dSLei He * negative value for error, and cb will not be called. 3119e4f86a8SGonglei */ 3122fda101dSLei He int cryptodev_backend_create_session( 3139e4f86a8SGonglei CryptoDevBackend *backend, 3140e660a6fSzhenwei pi CryptoDevBackendSessionInfo *sess_info, 3152fda101dSLei He uint32_t queue_index, 3162fda101dSLei He CryptoDevCompletionFunc cb, 3172fda101dSLei He void *opaque); 3189e4f86a8SGonglei 3199e4f86a8SGonglei /** 3200e660a6fSzhenwei pi * cryptodev_backend_close_session: 3219e4f86a8SGonglei * @backend: the cryptodev backend object 3229e4f86a8SGonglei * @session_id: the session id 3239e4f86a8SGonglei * @queue_index: queue index of cryptodev backend client 3249e4f86a8SGonglei * @errp: pointer to a NULL-initialized error object 3252fda101dSLei He * @cb: callback when session create is compeleted 3262fda101dSLei He * @opaque: parameter passed to callback 3279e4f86a8SGonglei * 3280e660a6fSzhenwei pi * Close a session for which was previously 3290e660a6fSzhenwei pi * created by cryptodev_backend_create_session() 3309e4f86a8SGonglei * 3312fda101dSLei He * Returns: 0 for success and cb will be called when creation is completed, 3322fda101dSLei He * negative value for error, and cb will not be called. 3339e4f86a8SGonglei */ 3340e660a6fSzhenwei pi int cryptodev_backend_close_session( 3359e4f86a8SGonglei CryptoDevBackend *backend, 3369e4f86a8SGonglei uint64_t session_id, 3372fda101dSLei He uint32_t queue_index, 3382fda101dSLei He CryptoDevCompletionFunc cb, 3392fda101dSLei He void *opaque); 3409e4f86a8SGonglei 3419e4f86a8SGonglei /** 342d6634ac0SGonglei * cryptodev_backend_crypto_operation: 3439e4f86a8SGonglei * @backend: the cryptodev backend object 3442fda101dSLei He * @opaque1: pointer to a VirtIOCryptoReq object 3459e4f86a8SGonglei * @queue_index: queue index of cryptodev backend client 3469e4f86a8SGonglei * @errp: pointer to a NULL-initialized error object 3472fda101dSLei He * @cb: callbacks when operation is completed 3482fda101dSLei He * @opaque2: parameter passed to cb 3499e4f86a8SGonglei * 350d6634ac0SGonglei * Do crypto operation, such as encryption and 3519e4f86a8SGonglei * decryption 3529e4f86a8SGonglei * 3532fda101dSLei He * Returns: 0 for success and cb will be called when creation is completed, 3542fda101dSLei He * negative value for error, and cb will not be called. 3559e4f86a8SGonglei */ 356d6634ac0SGonglei int cryptodev_backend_crypto_operation( 3579e4f86a8SGonglei CryptoDevBackend *backend, 3582fda101dSLei He void *opaque1, 3592fda101dSLei He uint32_t queue_index, 3602fda101dSLei He CryptoDevCompletionFunc cb, 3612fda101dSLei He void *opaque2); 3629e4f86a8SGonglei 36346fd1705SGonglei /** 36446fd1705SGonglei * cryptodev_backend_set_used: 36546fd1705SGonglei * @backend: the cryptodev backend object 36646fd1705SGonglei * @used: ture or false 36746fd1705SGonglei * 36846fd1705SGonglei * Set the cryptodev backend is used by virtio-crypto or not 36946fd1705SGonglei */ 37046fd1705SGonglei void cryptodev_backend_set_used(CryptoDevBackend *backend, bool used); 37146fd1705SGonglei 37246fd1705SGonglei /** 37346fd1705SGonglei * cryptodev_backend_is_used: 37446fd1705SGonglei * @backend: the cryptodev backend object 37546fd1705SGonglei * 37646fd1705SGonglei * Return the status that the cryptodev backend is used 37746fd1705SGonglei * by virtio-crypto or not 37846fd1705SGonglei * 37946fd1705SGonglei * Returns: true on used, or false on not used 38046fd1705SGonglei */ 38146fd1705SGonglei bool cryptodev_backend_is_used(CryptoDevBackend *backend); 38246fd1705SGonglei 3836138dbdaSGonglei /** 3846138dbdaSGonglei * cryptodev_backend_set_ready: 3856138dbdaSGonglei * @backend: the cryptodev backend object 3866138dbdaSGonglei * @ready: ture or false 3876138dbdaSGonglei * 3886138dbdaSGonglei * Set the cryptodev backend is ready or not, which is called 3896138dbdaSGonglei * by the children of the cryptodev banckend interface. 3906138dbdaSGonglei */ 3916138dbdaSGonglei void cryptodev_backend_set_ready(CryptoDevBackend *backend, bool ready); 3926138dbdaSGonglei 3936138dbdaSGonglei /** 3946138dbdaSGonglei * cryptodev_backend_is_ready: 3956138dbdaSGonglei * @backend: the cryptodev backend object 3966138dbdaSGonglei * 3976138dbdaSGonglei * Return the status that the cryptodev backend is ready or not 3986138dbdaSGonglei * 3996138dbdaSGonglei * Returns: true on ready, or false on not ready 4006138dbdaSGonglei */ 4016138dbdaSGonglei bool cryptodev_backend_is_ready(CryptoDevBackend *backend); 40246fd1705SGonglei 403d0ee7a13SGonglei #endif /* CRYPTODEV_H */ 404