From patchwork Thu Oct 13 07:12:06 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Gonglei (Arei)" X-Patchwork-Id: 9374469 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id AA6B460839 for ; Thu, 13 Oct 2016 07:32:47 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9F56F298E2 for ; Thu, 13 Oct 2016 07:32:47 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 92888298E4; Thu, 13 Oct 2016 07:32:47 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 9E0AB298E2 for ; Thu, 13 Oct 2016 07:32:46 +0000 (UTC) Received: from localhost ([::1]:38262 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1buaVR-0001Aw-Ph for patchwork-qemu-devel@patchwork.kernel.org; Thu, 13 Oct 2016 03:32:45 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37866) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1buaUz-000182-TK for qemu-devel@nongnu.org; Thu, 13 Oct 2016 03:32:19 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1buaUx-0000EF-CJ for qemu-devel@nongnu.org; Thu, 13 Oct 2016 03:32:16 -0400 Received: from szxga01-in.huawei.com ([58.251.152.64]:48204) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1buaUZ-0008EK-DJ for qemu-devel@nongnu.org; Thu, 13 Oct 2016 03:32:15 -0400 Received: from 172.24.1.136 (EHLO szxeml433-hub.china.huawei.com) ([172.24.1.136]) by szxrg01-dlp.huawei.com (MOS 4.3.7-GA FastPath queued) with ESMTP id DSP98006; Thu, 13 Oct 2016 15:12:29 +0800 (CST) Received: from localhost (10.177.18.62) by szxeml433-hub.china.huawei.com (10.82.67.210) with Microsoft SMTP Server id 14.3.235.1; Thu, 13 Oct 2016 15:12:23 +0800 From: Gonglei To: , Date: Thu, 13 Oct 2016 15:12:06 +0800 Message-ID: <1476342726-104488-13-git-send-email-arei.gonglei@huawei.com> X-Mailer: git-send-email 2.8.2.windows.1 In-Reply-To: <1476342726-104488-1-git-send-email-arei.gonglei@huawei.com> References: <1476342726-104488-1-git-send-email-arei.gonglei@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.18.62] X-CFilter-Loop: Reflected X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 58.251.152.64 Subject: [Qemu-devel] [PATCH v7 12/12] virtio-crypto: perfect algorithms chainning support X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: weidong.huang@huawei.com, claudio.fontana@huawei.com, mst@redhat.com, xin.zeng@intel.com, hanweidong@huawei.com, luonengjun@huawei.com, agraf@suse.de, nmorey@kalray.eu, mike.caraman@nxp.com, Gonglei , stefanha@redhat.com, jianjay.zhou@huawei.com, pbonzini@redhat.com, peter.huangpeng@huawei.com, vincent.jardin@6wind.com, wu.wubin@huawei.com, arei.gonglei@hotmail.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP For one source buffer, we can do cipher and hash operations partly in algorithms chainning mode. We updated the corresponding part in virtio crypto specification firstly. The cryptodev-builtin backend doesn't support algorithm chainning, so let's add a check for it. Signed-off-by: Gonglei --- backends/cryptodev-builtin.c | 6 +++ hw/virtio/virtio-crypto.c | 62 ++++++++++++++++---------- include/standard-headers/linux/virtio_crypto.h | 11 ++++- include/sysemu/cryptodev.h | 13 +++++- 4 files changed, 66 insertions(+), 26 deletions(-) diff --git a/backends/cryptodev-builtin.c b/backends/cryptodev-builtin.c index dc0a364..9ad79ad 100644 --- a/backends/cryptodev-builtin.c +++ b/backends/cryptodev-builtin.c @@ -265,6 +265,12 @@ static int cryptodev_builtin_sym_operation( return -VIRTIO_CRYPTO_INVSESS; } + if (op_info->op_type == VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING) { + error_setg(errp, + "Algorithm chain is unsupported for cryptdoev-builtin"); + return -VIRTIO_CRYPTO_NOTSUPP; + } + sess = builtin->sessions[op_info->session_id]; ret = qcrypto_cipher_setiv(sess->cipher, op_info->iv, diff --git a/hw/virtio/virtio-crypto.c b/hw/virtio/virtio-crypto.c index fc78a86..2455ebb 100644 --- a/hw/virtio/virtio-crypto.c +++ b/hw/virtio/virtio-crypto.c @@ -390,21 +390,42 @@ virtio_crypto_get_request(VirtIOCrypto *s, VirtQueue *vq) static CryptoDevBackendSymOpInfo * virtio_crypto_sym_op_helper(VirtIODevice *vdev, - struct virtio_crypto_cipher_para *para, - uint32_t aad_len, - struct iovec *iov, unsigned int out_num, - uint32_t hash_result_len, - uint32_t hash_start_src_offset) + struct virtio_crypto_cipher_para *cipher_para, + struct virtio_crypto_alg_chain_data_para *alg_chain_para, + struct iovec *iov, unsigned int out_num) { CryptoDevBackendSymOpInfo *op_info; - uint32_t src_len, dst_len; - uint32_t iv_len; + uint32_t src_len = 0, dst_len = 0; + uint32_t iv_len = 0; + uint32_t aad_len = 0, hash_result_len = 0; + uint32_t hash_start_src_offset = 0, len_to_hash = 0; + uint32_t cipher_start_src_offset = 0, len_to_cipher = 0; + size_t max_len, curr_size = 0; size_t s; - iv_len = virtio_ldl_p(vdev, ¶->iv_len); - src_len = virtio_ldl_p(vdev, ¶->src_data_len); - dst_len = virtio_ldl_p(vdev, ¶->dst_data_len); + /* Plain cipher */ + if (cipher_para) { + iv_len = virtio_ldl_p(vdev, &cipher_para->iv_len); + src_len = virtio_ldl_p(vdev, &cipher_para->src_data_len); + dst_len = virtio_ldl_p(vdev, &cipher_para->dst_data_len); + } else if (alg_chain_para) { /* Algorithm chain */ + iv_len = virtio_ldl_p(vdev, &alg_chain_para->iv_len); + src_len = virtio_ldl_p(vdev, &alg_chain_para->src_data_len); + dst_len = virtio_ldl_p(vdev, &alg_chain_para->dst_data_len); + + aad_len = virtio_ldl_p(vdev, &alg_chain_para->aad_len); + hash_result_len = virtio_ldl_p(vdev, + &alg_chain_para->hash_result_len); + hash_start_src_offset = virtio_ldl_p(vdev, + &alg_chain_para->hash_start_src_offset); + cipher_start_src_offset = virtio_ldl_p(vdev, + &alg_chain_para->cipher_start_src_offset); + len_to_cipher = virtio_ldl_p(vdev, &alg_chain_para->len_to_cipher); + len_to_hash = virtio_ldl_p(vdev, &alg_chain_para->len_to_hash); + } else { + return NULL; + } max_len = iv_len + aad_len + src_len + dst_len + hash_result_len; op_info = g_malloc0(sizeof(CryptoDevBackendSymOpInfo) + max_len); @@ -414,6 +435,9 @@ virtio_crypto_sym_op_helper(VirtIODevice *vdev, op_info->aad_len = aad_len; op_info->digest_result_len = hash_result_len; op_info->hash_start_src_offset = hash_start_src_offset; + op_info->len_to_hash = len_to_hash; + op_info->cipher_start_src_offset = cipher_start_src_offset; + op_info->len_to_cipher = len_to_cipher; /* Handle the initilization vector */ if (op_info->iv_len > 0) { DPRINTF("iv_len=%" PRIu32 "\n", op_info->iv_len); @@ -491,25 +515,15 @@ virtio_crypto_handle_sym_req(VirtIOCrypto *vcrypto, if (op_type == VIRTIO_CRYPTO_SYM_OP_CIPHER) { op_info = virtio_crypto_sym_op_helper(vdev, &req->u.cipher.para, - 0, iov, out_num, 0, 0); + NULL, iov, out_num); if (!op_info) { return -EFAULT; } op_info->op_type = op_type; } else if (op_type == VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING) { - uint32_t aad_len, hash_result_len; - uint32_t hash_start_src_offset; - - aad_len = virtio_ldl_p(vdev, &req->u.chain.para.aad_len); - hash_result_len = virtio_ldl_p(vdev, - &req->u.chain.para.hash_result_len); - hash_start_src_offset = virtio_ldl_p(vdev, - &req->u.chain.para.hash_start_src_offset); - /* cipher part */ - op_info = virtio_crypto_sym_op_helper(vdev, &req->u.chain.para.cipher, - aad_len, iov, out_num, - hash_result_len, - hash_start_src_offset); + op_info = virtio_crypto_sym_op_helper(vdev, NULL, + &req->u.chain.para, + iov, out_num); if (!op_info) { return -EFAULT; } diff --git a/include/standard-headers/linux/virtio_crypto.h b/include/standard-headers/linux/virtio_crypto.h index f2a059e..9ae02fb 100644 --- a/include/standard-headers/linux/virtio_crypto.h +++ b/include/standard-headers/linux/virtio_crypto.h @@ -326,7 +326,15 @@ struct virtio_crypto_mac_data_req { }; struct virtio_crypto_alg_chain_data_para { - struct virtio_crypto_cipher_para cipher; + __virtio32 iv_len; + /* Length of source data */ + __virtio32 src_data_len; + /* Length of destination data */ + __virtio32 dst_data_len; + /* Starting point for cipher processing in source data */ + __virtio32 cipher_start_src_offset; + /* Length of the source data that the cipher will be computed on */ + __virtio32 len_to_cipher; /* Starting point for hash processing in source data */ __virtio32 hash_start_src_offset; /* Length of the source data that the hash will be computed on */ @@ -335,6 +343,7 @@ struct virtio_crypto_alg_chain_data_para { __virtio32 aad_len; /* Length of the hash result */ __virtio32 hash_result_len; + __virtio32 reserved; }; struct virtio_crypto_alg_chain_data_req { diff --git a/include/sysemu/cryptodev.h b/include/sysemu/cryptodev.h index f55b79e..f9a4b36 100644 --- a/include/sysemu/cryptodev.h +++ b/include/sysemu/cryptodev.h @@ -108,7 +108,15 @@ typedef struct CryptoDevBackendSymSessionInfo { * @dst_len: byte length of destination data * @digest_result_len: byte length of hash digest result * @hash_start_src_offset: Starting point for hash processing, specified - * as number of bytes from start of packet in source data + * as number of bytes from start of packet in source data, only used for + * algorithm chain + * @cipher_start_src_offset: Starting point for cipher processing, specified + * as number of bytes from start of packet in source data, only used for + * algorithm chain + * @len_to_hash: byte length of source data on which the hash + * operation will be computed, only used for algorithm chain + * @len_to_cipher: byte length of source data on which the cipher + * operation will be computed, only used for algorithm chain * @op_type: operation type (refer to virtio_crypto.h) * @iv: point to the initialization vector or counter * @src: point to the source data @@ -126,6 +134,9 @@ typedef struct CryptoDevBackendSymOpInfo { uint32_t dst_len; uint32_t digest_result_len; uint32_t hash_start_src_offset; + uint32_t cipher_start_src_offset; + uint32_t len_to_hash; + uint32_t len_to_cipher; uint8_t op_type; uint8_t *iv; uint8_t *src;