From patchwork Mon Sep 11 01:10:39 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Longpeng(Mike)" X-Patchwork-Id: 9946447 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 A32F6604D5 for ; Mon, 11 Sep 2017 01:15:35 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 97BA3289AB for ; Mon, 11 Sep 2017 01:15:35 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8BB35289ED; Mon, 11 Sep 2017 01:15:35 +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 0B421289AB for ; Mon, 11 Sep 2017 01:15:34 +0000 (UTC) Received: from localhost ([::1]:54834 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1drDK2-0006oP-6I for patchwork-qemu-devel@patchwork.kernel.org; Sun, 10 Sep 2017 21:15:34 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35256) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1drDG6-0004HM-30 for qemu-devel@nongnu.org; Sun, 10 Sep 2017 21:11:31 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1drDG3-00046m-BO for qemu-devel@nongnu.org; Sun, 10 Sep 2017 21:11:30 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:2258) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1drDG2-00044Z-D3 for qemu-devel@nongnu.org; Sun, 10 Sep 2017 21:11:27 -0400 Received: from 172.30.72.59 (EHLO DGGEMS403-HUB.china.huawei.com) ([172.30.72.59]) by dggrg05-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id DHB06019; Mon, 11 Sep 2017 09:11:03 +0800 (CST) Received: from localhost (10.177.246.209) by DGGEMS403-HUB.china.huawei.com (10.3.19.203) with Microsoft SMTP Server id 14.3.301.0; Mon, 11 Sep 2017 09:10:54 +0800 From: "Longpeng(Mike)" To: , Date: Mon, 11 Sep 2017 09:10:39 +0800 Message-ID: <1505092240-10864-8-git-send-email-longpeng2@huawei.com> X-Mailer: git-send-email 1.8.4.msysgit.0 In-Reply-To: <1505092240-10864-1-git-send-email-longpeng2@huawei.com> References: <1505092240-10864-1-git-send-email-longpeng2@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.246.209] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A0B0205.59B5E2A8.002E, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: cad29c4c6fcdff6afc1ffa45d899c509 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 45.249.212.191 Subject: [Qemu-devel] [RFC 7/8] cryptodev-builtin: add stateless cipher 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, mst@redhat.com, jasowang@redhat.com, john.griffin@intel.com, Varun.Sethi@freescale.com, denglingli@chinamobile.com, arei.gonglei@hotmail.com, agraf@suse.de, arei.gonglei@huawei.com, "Longpeng\(Mike\)" , vincent.jardin@6wind.com, Ola.Liljedahl@arm.com, luonengjun@huawei.com, xin.zeng@intel.com, liang.j.ma@intel.com, stefanha@redhat.com, Jani.Kokkonen@huawei.com, pasic@linux.vnet.ibm.com, brian.a.keating@intel.com, wangxinxin.wang@huawei.com, cohuck@redhat.com, mike.caraman@nxp.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Adds stateless cipher support for builtin-backend Signed-off-by: Gonglei [simplify the code & correct the return value] Signed-off-by: Longpeng(Mike) --- backends/cryptodev-builtin.c | 86 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/backends/cryptodev-builtin.c b/backends/cryptodev-builtin.c index ab3d88d..1152ea2 100755 --- a/backends/cryptodev-builtin.c +++ b/backends/cryptodev-builtin.c @@ -335,6 +335,13 @@ static int cryptodev_builtin_sym_operation( return -VIRTIO_CRYPTO_NOTSUPP; } + if (op_info->op_code != VIRTIO_CRYPTO_CIPHER_ENCRYPT && + op_info->op_code != VIRTIO_CRYPTO_CIPHER_DECRYPT) { + error_setg(errp, + "Unsupported op code: %u", op_info->op_code); + return -VIRTIO_CRYPTO_NOTSUPP; + } + sess = builtin->sessions[op_info->session_id]; if (op_info->iv_len > 0) { @@ -389,6 +396,84 @@ static void cryptodev_builtin_cleanup( cryptodev_backend_set_ready(backend, false); } +static int +cryptodev_builtin_sym_stateless_operation( + CryptoDevBackend *backend, + CryptoDevBackendSymStatelessInfo *op_info, + uint32_t queue_index, Error **errp) +{ + CryptoDevBackendSymSessionInfo *sess_info; + CryptoDevBackendSymOpInfo *sym_op_info; + int algo, mode; + int ret; + QCryptoCipher *cipher = NULL; + + sess_info = &op_info->session_info; + sym_op_info = &op_info->op_info; + + if (sess_info->op_type != VIRTIO_CRYPTO_SYM_OP_CIPHER) { + error_setg(errp, "Unsupported op_type: %u", sess_info->op_type); + return -VIRTIO_CRYPTO_NOTSUPP; + } + + switch (sym_op_info->op_code) { + case VIRTIO_CRYPTO_CIPHER_ENCRYPT: + case VIRTIO_CRYPTO_CIPHER_DECRYPT: + ret = cryptodev_builtin_get_cipher_alg_mode(sess_info, + &algo, &mode, errp); + if (ret < 0) { + return -VIRTIO_CRYPTO_NOTSUPP; + } + + cipher = qcrypto_cipher_new(algo, mode, + sess_info->cipher_key, + sess_info->key_len, + errp); + if (!cipher) { + return -VIRTIO_CRYPTO_ERR; + } + + if (sym_op_info->iv_len > 0) { + ret = qcrypto_cipher_setiv(cipher, sym_op_info->iv, + sym_op_info->iv_len, errp); + if (ret < 0) { + ret = -VIRTIO_CRYPTO_ERR; + goto out; + } + } + + if (sess_info->direction == VIRTIO_CRYPTO_OP_ENCRYPT) { + ret = qcrypto_cipher_encrypt(cipher, sym_op_info->src, + sym_op_info->dst, + sym_op_info->src_len, errp); + if (ret < 0) { + ret = -VIRTIO_CRYPTO_ERR; + goto out; + } + } else { + ret = qcrypto_cipher_decrypt(cipher, sym_op_info->src, + sym_op_info->dst, + sym_op_info->src_len, errp); + if (ret < 0) { + ret = -VIRTIO_CRYPTO_ERR; + goto out; + } + } + break; + + default: + error_setg(errp, "Unsupported op_code: %" PRIu32 "", + sym_op_info->op_code); + return -VIRTIO_CRYPTO_NOTSUPP; + } + + ret = VIRTIO_CRYPTO_OK; + +out: + qcrypto_cipher_free(cipher); + return ret; +} + static void cryptodev_builtin_class_init(ObjectClass *oc, void *data) { @@ -399,6 +484,7 @@ cryptodev_builtin_class_init(ObjectClass *oc, void *data) bc->create_session = cryptodev_builtin_sym_create_session; bc->close_session = cryptodev_builtin_sym_close_session; bc->do_sym_op = cryptodev_builtin_sym_operation; + bc->do_sym_stateless_op = cryptodev_builtin_sym_stateless_operation; } static const TypeInfo cryptodev_builtin_info = {