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 177*2cb06927Szhenwei pi typedef void (*CryptoDevCompletionFunc) (void *opaque, int ret); 178*2cb06927Szhenwei pi 1790e660a6fSzhenwei pi typedef struct CryptoDevBackendOpInfo { 180999c789fSzhenwei pi QCryptodevBackendAlgType algtype; 1810e660a6fSzhenwei pi uint32_t op_code; 182*2cb06927Szhenwei pi uint32_t queue_index; 183*2cb06927Szhenwei pi CryptoDevCompletionFunc cb; 184*2cb06927Szhenwei pi void *opaque; /* argument for cb */ 1850e660a6fSzhenwei pi uint64_t session_id; 1860e660a6fSzhenwei pi union { 1870e660a6fSzhenwei pi CryptoDevBackendSymOpInfo *sym_op_info; 1880e660a6fSzhenwei pi CryptoDevBackendAsymOpInfo *asym_op_info; 1890e660a6fSzhenwei pi } u; 1900e660a6fSzhenwei pi } CryptoDevBackendOpInfo; 1910e660a6fSzhenwei pi 192db1015e9SEduardo Habkost struct CryptoDevBackendClass { 193d0ee7a13SGonglei ObjectClass parent_class; 194d0ee7a13SGonglei 195d0ee7a13SGonglei void (*init)(CryptoDevBackend *backend, Error **errp); 196d0ee7a13SGonglei void (*cleanup)(CryptoDevBackend *backend, Error **errp); 1979e4f86a8SGonglei 1982fda101dSLei He int (*create_session)(CryptoDevBackend *backend, 1990e660a6fSzhenwei pi CryptoDevBackendSessionInfo *sess_info, 2002fda101dSLei He uint32_t queue_index, 2012fda101dSLei He CryptoDevCompletionFunc cb, 2022fda101dSLei He void *opaque); 2032fda101dSLei He 2049e4f86a8SGonglei int (*close_session)(CryptoDevBackend *backend, 2059e4f86a8SGonglei uint64_t session_id, 2062fda101dSLei He uint32_t queue_index, 2072fda101dSLei He CryptoDevCompletionFunc cb, 2082fda101dSLei He void *opaque); 2092fda101dSLei He 2100e660a6fSzhenwei pi int (*do_op)(CryptoDevBackend *backend, 211*2cb06927Szhenwei pi CryptoDevBackendOpInfo *op_info); 212db1015e9SEduardo Habkost }; 213d0ee7a13SGonglei 214d0ee7a13SGonglei struct CryptoDevBackendClient { 21514c9fd16Szhenwei pi QCryptodevBackendType type; 216d0ee7a13SGonglei char *info_str; 217d0ee7a13SGonglei unsigned int queue_index; 2185da73dabSGonglei int vring_enable; 219d0ee7a13SGonglei QTAILQ_ENTRY(CryptoDevBackendClient) next; 220d0ee7a13SGonglei }; 221d0ee7a13SGonglei 222d0ee7a13SGonglei struct CryptoDevBackendPeers { 223d0ee7a13SGonglei CryptoDevBackendClient *ccs[MAX_CRYPTO_QUEUE_NUM]; 224d0ee7a13SGonglei uint32_t queues; 225d0ee7a13SGonglei }; 226d0ee7a13SGonglei 227d0ee7a13SGonglei struct CryptoDevBackendConf { 228d0ee7a13SGonglei CryptoDevBackendPeers peers; 229d0ee7a13SGonglei 230d0ee7a13SGonglei /* Supported service mask */ 231d0ee7a13SGonglei uint32_t crypto_services; 232d0ee7a13SGonglei 233d0ee7a13SGonglei /* Detailed algorithms mask */ 234d0ee7a13SGonglei uint32_t cipher_algo_l; 235d0ee7a13SGonglei uint32_t cipher_algo_h; 236d0ee7a13SGonglei uint32_t hash_algo; 237d0ee7a13SGonglei uint32_t mac_algo_l; 238d0ee7a13SGonglei uint32_t mac_algo_h; 239d0ee7a13SGonglei uint32_t aead_algo; 2400e660a6fSzhenwei pi uint32_t akcipher_algo; 241d0ee7a13SGonglei /* Maximum length of cipher key */ 242d0ee7a13SGonglei uint32_t max_cipher_key_len; 243d0ee7a13SGonglei /* Maximum length of authenticated key */ 244d0ee7a13SGonglei uint32_t max_auth_key_len; 245d0ee7a13SGonglei /* Maximum size of each crypto request's content */ 246d0ee7a13SGonglei uint64_t max_size; 247d0ee7a13SGonglei }; 248d0ee7a13SGonglei 249d0ee7a13SGonglei struct CryptoDevBackend { 250d0ee7a13SGonglei Object parent_obj; 251d0ee7a13SGonglei 252d0ee7a13SGonglei bool ready; 25346fd1705SGonglei /* Tag the cryptodev backend is used by virtio-crypto or not */ 25446fd1705SGonglei bool is_used; 255d0ee7a13SGonglei CryptoDevBackendConf conf; 256d0ee7a13SGonglei }; 257d0ee7a13SGonglei 258d0ee7a13SGonglei /** 259d0ee7a13SGonglei * cryptodev_backend_new_client: 260d0ee7a13SGonglei * 2613f478371Szhenwei pi * Creates a new cryptodev backend client object. 262d0ee7a13SGonglei * 263d0ee7a13SGonglei * The returned object must be released with 264d0ee7a13SGonglei * cryptodev_backend_free_client() when no 265d0ee7a13SGonglei * longer required 266d0ee7a13SGonglei * 267d0ee7a13SGonglei * Returns: a new cryptodev backend client object 268d0ee7a13SGonglei */ 2693f478371Szhenwei pi CryptoDevBackendClient *cryptodev_backend_new_client(void); 2703f478371Szhenwei pi 271d0ee7a13SGonglei /** 272d0ee7a13SGonglei * cryptodev_backend_free_client: 273d0ee7a13SGonglei * @cc: the cryptodev backend client object 274d0ee7a13SGonglei * 275d0ee7a13SGonglei * Release the memory associated with @cc that 276d0ee7a13SGonglei * was previously allocated by cryptodev_backend_new_client() 277d0ee7a13SGonglei */ 278d0ee7a13SGonglei void cryptodev_backend_free_client( 279d0ee7a13SGonglei CryptoDevBackendClient *cc); 280d0ee7a13SGonglei 281d0ee7a13SGonglei /** 282d0ee7a13SGonglei * cryptodev_backend_cleanup: 283d0ee7a13SGonglei * @backend: the cryptodev backend object 284d0ee7a13SGonglei * @errp: pointer to a NULL-initialized error object 285d0ee7a13SGonglei * 286d0ee7a13SGonglei * Clean the resouce associated with @backend that realizaed 287d0ee7a13SGonglei * by the specific backend's init() callback 288d0ee7a13SGonglei */ 289d0ee7a13SGonglei void cryptodev_backend_cleanup( 290d0ee7a13SGonglei CryptoDevBackend *backend, 291d0ee7a13SGonglei Error **errp); 292d0ee7a13SGonglei 2939e4f86a8SGonglei /** 2940e660a6fSzhenwei pi * cryptodev_backend_create_session: 2959e4f86a8SGonglei * @backend: the cryptodev backend object 2969e4f86a8SGonglei * @sess_info: parameters needed by session creating 2979e4f86a8SGonglei * @queue_index: queue index of cryptodev backend client 2989e4f86a8SGonglei * @errp: pointer to a NULL-initialized error object 2992fda101dSLei He * @cb: callback when session create is compeleted 3002fda101dSLei He * @opaque: parameter passed to callback 3019e4f86a8SGonglei * 3022fda101dSLei He * Create a session for symmetric/asymmetric algorithms 3039e4f86a8SGonglei * 3042fda101dSLei He * Returns: 0 for success and cb will be called when creation is completed, 3052fda101dSLei He * negative value for error, and cb will not be called. 3069e4f86a8SGonglei */ 3072fda101dSLei He int cryptodev_backend_create_session( 3089e4f86a8SGonglei CryptoDevBackend *backend, 3090e660a6fSzhenwei pi CryptoDevBackendSessionInfo *sess_info, 3102fda101dSLei He uint32_t queue_index, 3112fda101dSLei He CryptoDevCompletionFunc cb, 3122fda101dSLei He void *opaque); 3139e4f86a8SGonglei 3149e4f86a8SGonglei /** 3150e660a6fSzhenwei pi * cryptodev_backend_close_session: 3169e4f86a8SGonglei * @backend: the cryptodev backend object 3179e4f86a8SGonglei * @session_id: the session id 3189e4f86a8SGonglei * @queue_index: queue index of cryptodev backend client 3199e4f86a8SGonglei * @errp: pointer to a NULL-initialized error object 3202fda101dSLei He * @cb: callback when session create is compeleted 3212fda101dSLei He * @opaque: parameter passed to callback 3229e4f86a8SGonglei * 3230e660a6fSzhenwei pi * Close a session for which was previously 3240e660a6fSzhenwei pi * created by cryptodev_backend_create_session() 3259e4f86a8SGonglei * 3262fda101dSLei He * Returns: 0 for success and cb will be called when creation is completed, 3272fda101dSLei He * negative value for error, and cb will not be called. 3289e4f86a8SGonglei */ 3290e660a6fSzhenwei pi int cryptodev_backend_close_session( 3309e4f86a8SGonglei CryptoDevBackend *backend, 3319e4f86a8SGonglei uint64_t session_id, 3322fda101dSLei He uint32_t queue_index, 3332fda101dSLei He CryptoDevCompletionFunc cb, 3342fda101dSLei He void *opaque); 3359e4f86a8SGonglei 3369e4f86a8SGonglei /** 337d6634ac0SGonglei * cryptodev_backend_crypto_operation: 3389e4f86a8SGonglei * @backend: the cryptodev backend object 339*2cb06927Szhenwei pi * @op_info: pointer to a CryptoDevBackendOpInfo object 3409e4f86a8SGonglei * 341*2cb06927Szhenwei pi * Do crypto operation, such as encryption, decryption, signature and 342*2cb06927Szhenwei pi * verification 3439e4f86a8SGonglei * 3442fda101dSLei He * Returns: 0 for success and cb will be called when creation is completed, 3452fda101dSLei He * negative value for error, and cb will not be called. 3469e4f86a8SGonglei */ 347d6634ac0SGonglei int cryptodev_backend_crypto_operation( 3489e4f86a8SGonglei CryptoDevBackend *backend, 349*2cb06927Szhenwei pi CryptoDevBackendOpInfo *op_info); 3509e4f86a8SGonglei 35146fd1705SGonglei /** 35246fd1705SGonglei * cryptodev_backend_set_used: 35346fd1705SGonglei * @backend: the cryptodev backend object 35446fd1705SGonglei * @used: ture or false 35546fd1705SGonglei * 35646fd1705SGonglei * Set the cryptodev backend is used by virtio-crypto or not 35746fd1705SGonglei */ 35846fd1705SGonglei void cryptodev_backend_set_used(CryptoDevBackend *backend, bool used); 35946fd1705SGonglei 36046fd1705SGonglei /** 36146fd1705SGonglei * cryptodev_backend_is_used: 36246fd1705SGonglei * @backend: the cryptodev backend object 36346fd1705SGonglei * 36446fd1705SGonglei * Return the status that the cryptodev backend is used 36546fd1705SGonglei * by virtio-crypto or not 36646fd1705SGonglei * 36746fd1705SGonglei * Returns: true on used, or false on not used 36846fd1705SGonglei */ 36946fd1705SGonglei bool cryptodev_backend_is_used(CryptoDevBackend *backend); 37046fd1705SGonglei 3716138dbdaSGonglei /** 3726138dbdaSGonglei * cryptodev_backend_set_ready: 3736138dbdaSGonglei * @backend: the cryptodev backend object 3746138dbdaSGonglei * @ready: ture or false 3756138dbdaSGonglei * 3766138dbdaSGonglei * Set the cryptodev backend is ready or not, which is called 3776138dbdaSGonglei * by the children of the cryptodev banckend interface. 3786138dbdaSGonglei */ 3796138dbdaSGonglei void cryptodev_backend_set_ready(CryptoDevBackend *backend, bool ready); 3806138dbdaSGonglei 3816138dbdaSGonglei /** 3826138dbdaSGonglei * cryptodev_backend_is_ready: 3836138dbdaSGonglei * @backend: the cryptodev backend object 3846138dbdaSGonglei * 3856138dbdaSGonglei * Return the status that the cryptodev backend is ready or not 3866138dbdaSGonglei * 3876138dbdaSGonglei * Returns: true on ready, or false on not ready 3886138dbdaSGonglei */ 3896138dbdaSGonglei bool cryptodev_backend_is_ready(CryptoDevBackend *backend); 39046fd1705SGonglei 391d0ee7a13SGonglei #endif /* CRYPTODEV_H */ 392