From patchwork Mon Nov 6 06:57:01 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Longpeng (Mike, Cloud Infrastructure Service Product Dept.)" X-Patchwork-Id: 834489 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=2001:4830:134:3::11; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3yVk9L4JMSz9s7p for ; Mon, 6 Nov 2017 18:05:42 +1100 (AEDT) Received: from localhost ([::1]:46746 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eBbTY-00042f-LB for incoming@patchwork.ozlabs.org; Mon, 06 Nov 2017 02:05:40 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49883) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eBbLl-0006Pl-UV for qemu-devel@nongnu.org; Mon, 06 Nov 2017 01:57:38 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eBbLh-0000C2-9e for qemu-devel@nongnu.org; Mon, 06 Nov 2017 01:57:38 -0500 Received: from szxga05-in.huawei.com ([45.249.212.191]:2278) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1eBbLg-00005R-NP for qemu-devel@nongnu.org; Mon, 06 Nov 2017 01:57:33 -0500 Received: from 172.30.72.58 (EHLO DGGEMS403-HUB.china.huawei.com) ([172.30.72.58]) by dggrg05-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id DKL32654; Mon, 06 Nov 2017 14:57:30 +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.361.1; Mon, 6 Nov 2017 14:57:10 +0800 From: "Longpeng(Mike)" To: , Date: Mon, 6 Nov 2017 14:57:01 +0800 Message-ID: <1509951422-20060-10-git-send-email-longpeng2@huawei.com> X-Mailer: git-send-email 1.8.4.msysgit.0 In-Reply-To: <1509951422-20060-1-git-send-email-longpeng2@huawei.com> References: <1509951422-20060-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.0A020206.5A0007DA.004C, 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: bd7140c685ac2a2fc51860b79a8d464f 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 09/10] 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: wangxinxin.wang@huawei.com, "Longpeng\(Mike\)" , weidong.huang@huawei.com, qemu-devel@nongnu.org, jianjay.zhou@huawei.com Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" 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 ba6d15c..e0737e0 100644 --- 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 = {