From nobody Thu May 2 11:43:47 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1494244066301627.8705279169654; Mon, 8 May 2017 04:47:46 -0700 (PDT) Received: from localhost ([::1]:58944 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d7h8j-0000ui-3h for importer@patchew.org; Mon, 08 May 2017 07:47:45 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50746) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d7h0X-0002ze-QU for qemu-devel@nongnu.org; Mon, 08 May 2017 07:39:18 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d7h0W-0004XX-Mp for qemu-devel@nongnu.org; Mon, 08 May 2017 07:39:17 -0400 Received: from szxga01-in.huawei.com ([45.249.212.187]:3982) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1d7h0V-0004WY-RJ for qemu-devel@nongnu.org; Mon, 08 May 2017 07:39:16 -0400 Received: from 172.30.72.53 (EHLO DGGEML402-HUB.china.huawei.com) ([172.30.72.53]) by dggrg01-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id AOD07410; Mon, 08 May 2017 19:38:49 +0800 (CST) Received: from localhost (10.177.18.62) by DGGEML402-HUB.china.huawei.com (10.3.17.38) with Microsoft SMTP Server id 14.3.301.0; Mon, 8 May 2017 19:38:36 +0800 From: Gonglei To: Date: Mon, 8 May 2017 19:38:16 +0800 Message-ID: <1494243504-127980-2-git-send-email-arei.gonglei@huawei.com> X-Mailer: git-send-email 2.8.2.windows.1 In-Reply-To: <1494243504-127980-1-git-send-email-arei.gonglei@huawei.com> References: <1494243504-127980-1-git-send-email-arei.gonglei@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.18.62] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A090203.591058CB.0032, 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: ea76f428d027de081d2ad571cd0e5ef0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 45.249.212.187 Subject: [Qemu-devel] [RFC v1 1/9] cryptodev: introduce stateless sym operation stuff 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: pasic@linux.vnet.ibm.com, weidong.huang@huawei.com, mst@redhat.com, xin.zeng@intel.com, luonengjun@huawei.com, linqiangmin@huawei.com, Gonglei , stefanha@redhat.com, cornelia.huck@de.ibm.com, wu.wubin@huawei.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Because a stateless crypto request include all information about one crypto operation, we should introduce a wrapper for it, and add a new crypto operation callback to do statless crypto operation. Signed-off-by: Gonglei --- backends/cryptodev-builtin.c | 10 ++++++++++ backends/cryptodev.c | 21 +++++++++++++++++++++ include/hw/virtio/virtio-crypto.h | 1 + include/sysemu/cryptodev.h | 19 +++++++++++++++++++ 4 files changed, 51 insertions(+) diff --git a/backends/cryptodev-builtin.c b/backends/cryptodev-builtin.c index 657c0ba..1fa2cab 100644 --- a/backends/cryptodev-builtin.c +++ b/backends/cryptodev-builtin.c @@ -372,6 +372,15 @@ static void cryptodev_builtin_cleanup( cryptodev_backend_set_ready(backend, false); } =20 +static int +cryptodev_builtin_sym_stateless_operation( + CryptoDevBackend *backend, + CryptoDevBackendSymStatelessInfo *op_info, + uint32_t queue_index, Error **errp) +{ + return -VIRTIO_CRYPTO_ERR; +} + static void cryptodev_builtin_class_init(ObjectClass *oc, void *data) { @@ -382,6 +391,7 @@ cryptodev_builtin_class_init(ObjectClass *oc, void *dat= a) bc->create_session =3D cryptodev_builtin_sym_create_session; bc->close_session =3D cryptodev_builtin_sym_close_session; bc->do_sym_op =3D cryptodev_builtin_sym_operation; + bc->do_sym_stateless_op =3D cryptodev_builtin_sym_stateless_operation; } =20 static const TypeInfo cryptodev_builtin_info =3D { diff --git a/backends/cryptodev.c b/backends/cryptodev.c index 832f056..c6d7c5f 100644 --- a/backends/cryptodev.c +++ b/backends/cryptodev.c @@ -120,6 +120,21 @@ static int cryptodev_backend_sym_operation( return -VIRTIO_CRYPTO_ERR; } =20 +static int cryptodev_backend_sym_stateless_operation( + CryptoDevBackend *backend, + CryptoDevBackendSymStatelessInfo *op_info, + uint32_t queue_index, Error **errp) +{ + CryptoDevBackendClass *bc =3D + CRYPTODEV_BACKEND_GET_CLASS(backend); + + if (bc->do_sym_stateless_op) { + return bc->do_sym_stateless_op(backend, op_info, queue_index, errp= ); + } + + return -VIRTIO_CRYPTO_ERR; +} + int cryptodev_backend_crypto_operation( CryptoDevBackend *backend, void *opaque, @@ -133,6 +148,12 @@ int cryptodev_backend_crypto_operation( =20 return cryptodev_backend_sym_operation(backend, op_info, queue_index, errp); + } else if (req->flags =3D=3D CRYPTODEV_BACKEND_ALG_SYM_STATELESS) { + CryptoDevBackendSymStatelessInfo *op_info; + op_info =3D req->u.sym_stateless_info; + + return cryptodev_backend_sym_stateless_operation(backend, + op_info, queue_index, errp); } else { error_setg(errp, "Unsupported cryptodev alg type: %" PRIu32 "", req->flags); diff --git a/include/hw/virtio/virtio-crypto.h b/include/hw/virtio/virtio-c= rypto.h index a00a0bf..465ad20 100644 --- a/include/hw/virtio/virtio-crypto.h +++ b/include/hw/virtio/virtio-crypto.h @@ -73,6 +73,7 @@ typedef struct VirtIOCryptoReq { struct VirtIOCrypto *vcrypto; union { CryptoDevBackendSymOpInfo *sym_op_info; + CryptoDevBackendSymStatelessInfo *sym_stateless_info; } u; } VirtIOCryptoReq; =20 diff --git a/include/sysemu/cryptodev.h b/include/sysemu/cryptodev.h index a9d0d1e..be507f7 100644 --- a/include/sysemu/cryptodev.h +++ b/include/sysemu/cryptodev.h @@ -58,6 +58,7 @@ typedef struct CryptoDevBackend CryptoDevBackend; =20 enum CryptoDevBackendAlgType { CRYPTODEV_BACKEND_ALG_SYM, + CRYPTODEV_BACKEND_ALG_SYM_STATELESS, CRYPTODEV_BACKEND_ALG__MAX, }; =20 @@ -146,6 +147,21 @@ typedef struct CryptoDevBackendSymOpInfo { uint8_t data[0]; } CryptoDevBackendSymOpInfo; =20 +/** + * CryptoDevBackendSymStatelessInfo: + * + * @session_info: session information, see above + * CryptoDevBackendSymSessionInfo + * @op_info: crypto operation information, see above + * CryptoDevBackendSymOpInfo, @session_id is ignored + * + */ +typedef struct CryptoDevBackendSymStatelessInfo { + CryptoDevBackendSymSessionInfo session_info; + CryptoDevBackendSymOpInfo op_info; +} CryptoDevBackendSymStatelessInfo; + + typedef struct CryptoDevBackendClass { ObjectClass parent_class; =20 @@ -161,6 +177,9 @@ typedef struct CryptoDevBackendClass { int (*do_sym_op)(CryptoDevBackend *backend, CryptoDevBackendSymOpInfo *op_info, uint32_t queue_index, Error **errp); + int (*do_sym_stateless_op)(CryptoDevBackend *backend, + CryptoDevBackendSymStatelessInfo *op_info, + uint32_t queue_index, Error **errp); } CryptoDevBackendClass; =20 =20 --=20 1.8.3.1 From nobody Thu May 2 11:43:47 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1494243781961946.795921657666; Mon, 8 May 2017 04:43:01 -0700 (PDT) Received: from localhost ([::1]:58928 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d7h46-0005wr-Tm for importer@patchew.org; Mon, 08 May 2017 07:42:58 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50777) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d7h0e-00035h-6E for qemu-devel@nongnu.org; Mon, 08 May 2017 07:39:25 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d7h0Z-0004YG-HC for qemu-devel@nongnu.org; Mon, 08 May 2017 07:39:24 -0400 Received: from szxga01-in.huawei.com ([45.249.212.187]:3983) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1d7h0Y-0004XT-LT for qemu-devel@nongnu.org; Mon, 08 May 2017 07:39:19 -0400 Received: from 172.30.72.53 (EHLO DGGEML402-HUB.china.huawei.com) ([172.30.72.53]) by dggrg01-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id AOD07417; Mon, 08 May 2017 19:38:50 +0800 (CST) Received: from localhost (10.177.18.62) by DGGEML402-HUB.china.huawei.com (10.3.17.38) with Microsoft SMTP Server id 14.3.301.0; Mon, 8 May 2017 19:38:36 +0800 From: Gonglei To: Date: Mon, 8 May 2017 19:38:17 +0800 Message-ID: <1494243504-127980-3-git-send-email-arei.gonglei@huawei.com> X-Mailer: git-send-email 2.8.2.windows.1 In-Reply-To: <1494243504-127980-1-git-send-email-arei.gonglei@huawei.com> References: <1494243504-127980-1-git-send-email-arei.gonglei@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.18.62] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A090205.591058CB.00A6, 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: 51f76c2f82d45befed7b5208969020dc X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 45.249.212.187 Subject: [Qemu-devel] [RFC v1 2/9] cryptodev: extract one util function 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: pasic@linux.vnet.ibm.com, weidong.huang@huawei.com, mst@redhat.com, xin.zeng@intel.com, luonengjun@huawei.com, linqiangmin@huawei.com, Gonglei , stefanha@redhat.com, cornelia.huck@de.ibm.com, wu.wubin@huawei.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" So that the new function can be used by both seesion creation and the following stateless crypto operation. Signed-off-by: Gonglei --- backends/cryptodev-builtin.c | 100 +++++++++++++++++++++++++--------------= ---- 1 file changed, 58 insertions(+), 42 deletions(-) diff --git a/backends/cryptodev-builtin.c b/backends/cryptodev-builtin.c index 1fa2cab..abc2655 100644 --- a/backends/cryptodev-builtin.c +++ b/backends/cryptodev-builtin.c @@ -151,73 +151,56 @@ err: return -1; } =20 -static int cryptodev_builtin_create_cipher_session( - CryptoDevBackendBuiltin *builtin, +static int +cryptodev_builtin_get_cipher_alg_mode( CryptoDevBackendSymSessionInfo *sess_info, + int *algo, int *mode, Error **errp) { - int algo; - int mode; - QCryptoCipher *cipher; - int index; - CryptoDevBackendBuiltinSession *sess; - - if (sess_info->op_type !=3D VIRTIO_CRYPTO_SYM_OP_CIPHER) { - error_setg(errp, "Unsupported optype :%u", sess_info->op_type); - return -1; - } - - index =3D cryptodev_builtin_get_unused_session_index(builtin); - if (index < 0) { - error_setg(errp, "Total number of sessions created exceeds %u", - MAX_NUM_SESSIONS); - return -1; - } - switch (sess_info->cipher_alg) { case VIRTIO_CRYPTO_CIPHER_AES_ECB: - mode =3D QCRYPTO_CIPHER_MODE_ECB; - algo =3D cryptodev_builtin_get_aes_algo(sess_info->key_len, - mode, errp); - if (algo < 0) { + *mode =3D QCRYPTO_CIPHER_MODE_ECB; + *algo =3D cryptodev_builtin_get_aes_algo(sess_info->key_len, + *mode, errp); + if (*algo < 0) { return -1; } break; case VIRTIO_CRYPTO_CIPHER_AES_CBC: - mode =3D QCRYPTO_CIPHER_MODE_CBC; - algo =3D cryptodev_builtin_get_aes_algo(sess_info->key_len, - mode, errp); - if (algo < 0) { + *mode =3D QCRYPTO_CIPHER_MODE_CBC; + *algo =3D cryptodev_builtin_get_aes_algo(sess_info->key_len, + *mode, errp); + if (*algo < 0) { return -1; } break; case VIRTIO_CRYPTO_CIPHER_AES_CTR: - mode =3D QCRYPTO_CIPHER_MODE_CTR; - algo =3D cryptodev_builtin_get_aes_algo(sess_info->key_len, - mode, errp); - if (algo < 0) { + *mode =3D QCRYPTO_CIPHER_MODE_CTR; + *algo =3D cryptodev_builtin_get_aes_algo(sess_info->key_len, + *mode, errp); + if (*algo < 0) { return -1; } break; case VIRTIO_CRYPTO_CIPHER_AES_XTS: - mode =3D QCRYPTO_CIPHER_MODE_XTS; - algo =3D cryptodev_builtin_get_aes_algo(sess_info->key_len, - mode, errp); - if (algo < 0) { + *mode =3D QCRYPTO_CIPHER_MODE_XTS; + *algo =3D cryptodev_builtin_get_aes_algo(sess_info->key_len, + *mode, errp); + if (*algo < 0) { return -1; } break; case VIRTIO_CRYPTO_CIPHER_3DES_ECB: - mode =3D QCRYPTO_CIPHER_MODE_ECB; - algo =3D QCRYPTO_CIPHER_ALG_3DES; + *mode =3D QCRYPTO_CIPHER_MODE_ECB; + *algo =3D QCRYPTO_CIPHER_ALG_3DES; break; case VIRTIO_CRYPTO_CIPHER_3DES_CBC: - mode =3D QCRYPTO_CIPHER_MODE_CBC; - algo =3D QCRYPTO_CIPHER_ALG_3DES; + *mode =3D QCRYPTO_CIPHER_MODE_CBC; + *algo =3D QCRYPTO_CIPHER_ALG_3DES; break; case VIRTIO_CRYPTO_CIPHER_3DES_CTR: - mode =3D QCRYPTO_CIPHER_MODE_CTR; - algo =3D QCRYPTO_CIPHER_ALG_3DES; + *mode =3D QCRYPTO_CIPHER_MODE_CTR; + *algo =3D QCRYPTO_CIPHER_ALG_3DES; break; default: error_setg(errp, "Unsupported cipher alg :%u", @@ -225,6 +208,39 @@ static int cryptodev_builtin_create_cipher_session( return -1; } =20 + return 0; +} + +static int cryptodev_builtin_create_cipher_session( + CryptoDevBackendBuiltin *builtin, + CryptoDevBackendSymSessionInfo *sess_info, + Error **errp) +{ + int algo; + int mode; + QCryptoCipher *cipher; + int index; + CryptoDevBackendBuiltinSession *sess; + int ret; + + if (sess_info->op_type !=3D VIRTIO_CRYPTO_SYM_OP_CIPHER) { + error_setg(errp, "Unsupported optype :%u", sess_info->op_type); + return -1; + } + + index =3D cryptodev_builtin_get_unused_session_index(builtin); + if (index < 0) { + error_setg(errp, "Total number of sessions created exceeds %u", + MAX_NUM_SESSIONS); + return -1; + } + + ret =3D cryptodev_builtin_get_cipher_alg_mode(sess_info, + &algo, &mode, errp); + if (ret < 0) { + return -1; + } + cipher =3D qcrypto_cipher_new(algo, mode, sess_info->cipher_key, sess_info->key_len, --=20 1.8.3.1 From nobody Thu May 2 11:43:47 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1494243919657901.3893829103462; Mon, 8 May 2017 04:45:19 -0700 (PDT) Received: from localhost ([::1]:58939 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d7h6L-0007kQ-QM for importer@patchew.org; Mon, 08 May 2017 07:45:17 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50716) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d7h0U-0002xO-Ql for qemu-devel@nongnu.org; Mon, 08 May 2017 07:39:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d7h0T-0004Wq-Ri for qemu-devel@nongnu.org; Mon, 08 May 2017 07:39:14 -0400 Received: from szxga01-in.huawei.com ([45.249.212.187]:3981) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1d7h0T-0004Vj-7I for qemu-devel@nongnu.org; Mon, 08 May 2017 07:39:13 -0400 Received: from 172.30.72.53 (EHLO DGGEML402-HUB.china.huawei.com) ([172.30.72.53]) by dggrg01-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id AOD07420; Mon, 08 May 2017 19:38:51 +0800 (CST) Received: from localhost (10.177.18.62) by DGGEML402-HUB.china.huawei.com (10.3.17.38) with Microsoft SMTP Server id 14.3.301.0; Mon, 8 May 2017 19:38:36 +0800 From: Gonglei To: Date: Mon, 8 May 2017 19:38:18 +0800 Message-ID: <1494243504-127980-4-git-send-email-arei.gonglei@huawei.com> X-Mailer: git-send-email 2.8.2.windows.1 In-Reply-To: <1494243504-127980-1-git-send-email-arei.gonglei@huawei.com> References: <1494243504-127980-1-git-send-email-arei.gonglei@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.18.62] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A090201.591058CC.005A, 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: 3d281c51ba62c7d6225fad76b5baff1b X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 45.249.212.187 Subject: [Qemu-devel] [RFC v1 3/9] cryptodev: add missing op_code for symmertric crypto 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: pasic@linux.vnet.ibm.com, weidong.huang@huawei.com, mst@redhat.com, xin.zeng@intel.com, luonengjun@huawei.com, linqiangmin@huawei.com, Gonglei , stefanha@redhat.com, cornelia.huck@de.ibm.com, wu.wubin@huawei.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" We need the op_code to identify which of crypto operations for stateless crypto operation. Let's add it. Signed-off-by: Gonglei --- backends/cryptodev-builtin.c | 7 +++++++ include/sysemu/cryptodev.h | 3 +++ 2 files changed, 10 insertions(+) diff --git a/backends/cryptodev-builtin.c b/backends/cryptodev-builtin.c index abc2655..1e72985 100644 --- a/backends/cryptodev-builtin.c +++ b/backends/cryptodev-builtin.c @@ -334,6 +334,13 @@ static int cryptodev_builtin_sym_operation( return -VIRTIO_CRYPTO_NOTSUPP; } =20 + if (op_info->op_code !=3D VIRTIO_CRYPTO_CIPHER_ENCRYPT && + op_info->op_code !=3D VIRTIO_CRYPTO_CIPHER_DECRYPT) { + error_setg(errp, + "Unsupported op code: %u", op_info->op_code); + return -VIRTIO_CRYPTO_NOTSUPP; + } + sess =3D builtin->sessions[op_info->session_id]; =20 if (op_info->iv_len > 0) { diff --git a/include/sysemu/cryptodev.h b/include/sysemu/cryptodev.h index be507f7..d75f2e7 100644 --- a/include/sysemu/cryptodev.h +++ b/include/sysemu/cryptodev.h @@ -103,6 +103,7 @@ typedef struct CryptoDevBackendSymSessionInfo { * * @session_id: session index which was previously * created by cryptodev_backend_sym_create_session() + * @op_code: operation code (refer to virtio_crypto.h) * @aad_len: byte length of additional authenticated data * @iv_len: byte length of initialization vector or counter * @src_len: byte length of source data @@ -129,6 +130,8 @@ typedef struct CryptoDevBackendSymSessionInfo { */ typedef struct CryptoDevBackendSymOpInfo { uint64_t session_id; + /* corresponding with virtio crypto spec */ + uint32_t op_code; uint32_t aad_len; uint32_t iv_len; uint32_t src_len; --=20 1.8.3.1 From nobody Thu May 2 11:43:47 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1494243781800718.6228625744172; Mon, 8 May 2017 04:43:01 -0700 (PDT) Received: from localhost ([::1]:58929 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d7h48-0005yP-Et for importer@patchew.org; Mon, 08 May 2017 07:43:00 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50815) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d7h10-0003LP-7s for qemu-devel@nongnu.org; Mon, 08 May 2017 07:39:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d7h0v-0004by-Kg for qemu-devel@nongnu.org; Mon, 08 May 2017 07:39:46 -0400 Received: from szxga01-in.huawei.com ([45.249.212.187]:3984) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1d7h0v-0004bB-0Q for qemu-devel@nongnu.org; Mon, 08 May 2017 07:39:41 -0400 Received: from 172.30.72.53 (EHLO DGGEML402-HUB.china.huawei.com) ([172.30.72.53]) by dggrg01-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id AOD07416; Mon, 08 May 2017 19:38:49 +0800 (CST) Received: from localhost (10.177.18.62) by DGGEML402-HUB.china.huawei.com (10.3.17.38) with Microsoft SMTP Server id 14.3.301.0; Mon, 8 May 2017 19:38:36 +0800 From: Gonglei To: Date: Mon, 8 May 2017 19:38:19 +0800 Message-ID: <1494243504-127980-5-git-send-email-arei.gonglei@huawei.com> X-Mailer: git-send-email 2.8.2.windows.1 In-Reply-To: <1494243504-127980-1-git-send-email-arei.gonglei@huawei.com> References: <1494243504-127980-1-git-send-email-arei.gonglei@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.18.62] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020204.591058CB.007E, 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: 05c9dca09eb5a9c373fff763f313e6fe X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 45.249.212.187 Subject: [Qemu-devel] [RFC v1 4/9] cryptodev-builtin: realize stateless operation function 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: pasic@linux.vnet.ibm.com, weidong.huang@huawei.com, mst@redhat.com, xin.zeng@intel.com, luonengjun@huawei.com, linqiangmin@huawei.com, Gonglei , stefanha@redhat.com, cornelia.huck@de.ibm.com, wu.wubin@huawei.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" At this point, the stateless crypto operation for cryptodev-builtin backend works. Signed-off-by: Gonglei --- backends/cryptodev-builtin.c | 71 ++++++++++++++++++++++++++++++++++++++++= +++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/backends/cryptodev-builtin.c b/backends/cryptodev-builtin.c index 1e72985..7829999 100644 --- a/backends/cryptodev-builtin.c +++ b/backends/cryptodev-builtin.c @@ -401,7 +401,76 @@ cryptodev_builtin_sym_stateless_operation( CryptoDevBackendSymStatelessInfo *op_info, uint32_t queue_index, Error **errp) { - return -VIRTIO_CRYPTO_ERR; + CryptoDevBackendSymSessionInfo *sess_info; + CryptoDevBackendSymOpInfo *sym_op_info; + int algo, mode; + int ret; + QCryptoCipher *cipher =3D NULL; + + sess_info =3D &op_info->session_info; + sym_op_info =3D &op_info->op_info; + + if (sess_info->op_type !=3D VIRTIO_CRYPTO_SYM_OP_CIPHER) { + error_setg(errp, "Unsupported op_type: %u", sess_info->op_type); + return -VIRTIO_CRYPTO_ERR; + } + + switch (sym_op_info->op_code) { + case VIRTIO_CRYPTO_CIPHER_ENCRYPT: + case VIRTIO_CRYPTO_CIPHER_DECRYPT: + ret =3D cryptodev_builtin_get_cipher_alg_mode(sess_info, + &algo, &mode, errp); + if (ret < 0) { + return -VIRTIO_CRYPTO_ERR; + } + + cipher =3D 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 =3D qcrypto_cipher_setiv(cipher, sym_op_info->iv, + sym_op_info->iv_len, errp); + if (ret < 0) { + ret =3D -VIRTIO_CRYPTO_ERR; + goto out; + } + } + + if (sess_info->direction =3D=3D VIRTIO_CRYPTO_OP_ENCRYPT) { + ret =3D qcrypto_cipher_encrypt(cipher, sym_op_info->src, + sym_op_info->dst, + sym_op_info->src_len, errp); + if (ret < 0) { + ret =3D -VIRTIO_CRYPTO_ERR; + goto out; + } + } else { + ret =3D qcrypto_cipher_decrypt(cipher, sym_op_info->src, + sym_op_info->dst, + sym_op_info->src_len, errp); + if (ret < 0) { + ret =3D -VIRTIO_CRYPTO_ERR; + goto out; + } + } + break; + + default: + error_setg(errp, "Unsupported op_code: %" PRIu32 "", + sym_op_info->op_code); + return -VIRTIO_CRYPTO_ERR; + } + + ret =3D VIRTIO_CRYPTO_OK; + +out: + qcrypto_cipher_free(cipher); + return ret; } =20 static void --=20 1.8.3.1 From nobody Thu May 2 11:43:47 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1494243657405247.69479906730487; Mon, 8 May 2017 04:40:57 -0700 (PDT) Received: from localhost ([::1]:58921 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d7h28-00044p-3Z for importer@patchew.org; Mon, 08 May 2017 07:40:56 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50691) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d7h0T-0002xE-Nq for qemu-devel@nongnu.org; Mon, 08 May 2017 07:39:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d7h0P-0004V6-MM for qemu-devel@nongnu.org; Mon, 08 May 2017 07:39:13 -0400 Received: from szxga01-in.huawei.com ([45.249.212.187]:3978) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1d7h0O-0004U6-Q1 for qemu-devel@nongnu.org; Mon, 08 May 2017 07:39:09 -0400 Received: from 172.30.72.53 (EHLO DGGEML402-HUB.china.huawei.com) ([172.30.72.53]) by dggrg01-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id AOD07419; Mon, 08 May 2017 19:38:50 +0800 (CST) Received: from localhost (10.177.18.62) by DGGEML402-HUB.china.huawei.com (10.3.17.38) with Microsoft SMTP Server id 14.3.301.0; Mon, 8 May 2017 19:38:37 +0800 From: Gonglei To: Date: Mon, 8 May 2017 19:38:20 +0800 Message-ID: <1494243504-127980-6-git-send-email-arei.gonglei@huawei.com> X-Mailer: git-send-email 2.8.2.windows.1 In-Reply-To: <1494243504-127980-1-git-send-email-arei.gonglei@huawei.com> References: <1494243504-127980-1-git-send-email-arei.gonglei@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.18.62] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A090203.591058CB.0092, 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: fa8d519629b7cdd8f7661d8c4e4306b3 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 45.249.212.187 Subject: [Qemu-devel] [RFC v1 5/9] virtio-crypto: update header file 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: pasic@linux.vnet.ibm.com, weidong.huang@huawei.com, mst@redhat.com, xin.zeng@intel.com, luonengjun@huawei.com, linqiangmin@huawei.com, Gonglei , stefanha@redhat.com, cornelia.huck@de.ibm.com, wu.wubin@huawei.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Update the header based on the newset virtio crypto spec, so that the virtio crypto can support both session and stateless based crypto services and keep compatibility with the pre-existing code by introducing five feature bits. Signed-off-by: Gonglei --- include/standard-headers/linux/virtio_crypto.h | 210 +++++++++++++++++++++= +++- 1 file changed, 208 insertions(+), 2 deletions(-) diff --git a/include/standard-headers/linux/virtio_crypto.h b/include/stand= ard-headers/linux/virtio_crypto.h index 5ff0b4e..c447829 100644 --- a/include/standard-headers/linux/virtio_crypto.h +++ b/include/standard-headers/linux/virtio_crypto.h @@ -33,11 +33,20 @@ #include "standard-headers/linux/virtio_config.h" =20 =20 +/* The crypto service for virtio crypto */ #define VIRTIO_CRYPTO_SERVICE_CIPHER 0 #define VIRTIO_CRYPTO_SERVICE_HASH 1 #define VIRTIO_CRYPTO_SERVICE_MAC 2 #define VIRTIO_CRYPTO_SERVICE_AEAD 3 =20 +/* The feature bitmap for virtio crypto */ +#define VIRTIO_CRYPTO_F_MUX_MODE 0 /* Multiplexing mode is available */ +#define VIRTIO_CRYPTO_F_CIPHER_STATELESS_MODE 1 +#define VIRTIO_CRYPTO_F_HASH_STATELESS_MODE 2 +#define VIRTIO_CRYPTO_F_MAC_STATELESS_MODE 3 +#define VIRTIO_CRYPTO_F_AEAD_STATELESS_MODE 4 + + #define VIRTIO_CRYPTO_OPCODE(service, op) (((service) << 8) | (op)) =20 struct virtio_crypto_ctrl_header { @@ -166,8 +175,8 @@ struct virtio_crypto_aead_session_para { uint32_t algo; /* length of key */ uint32_t key_len; - /* hash result length */ - uint32_t hash_result_len; + /* Authentication tag length */ + uint32_t tag_len; /* length of the additional authenticated data (AAD) in bytes */ uint32_t aad_len; /* encrypt or decrypt, See above VIRTIO_CRYPTO_OP_* */ @@ -271,6 +280,8 @@ struct virtio_crypto_op_header { uint32_t algo; /* session_id should be service-specific algorithms */ uint64_t session_id; +#define VIRTIO_CRYPTO_FLAG_SESSION_MODE 1 +#define VIRTIO_CRYPTO_FLAG_STATELESS_MODE 2 /* control flag to control the request */ uint32_t flag; uint32_t padding; @@ -403,6 +414,201 @@ struct virtio_crypto_op_data_req { } u; }; =20 +struct virtio_crypto_cipher_para_stateless { + struct { + /* See VIRTIO_CRYPTO_CIPHER* above */ + uint32_t algo; + /* length of key */ + uint32_t keylen; + + /* See VIRTIO_CRYPTO_OP_* above */ + uint32_t op; + } sess_para; + + /* + * Byte Length of valid IV/Counter + */ + uint32_t iv_len; + /* length of source data */ + uint32_t src_data_len; + /* length of dst data */ + uint32_t dst_data_len; +}; + +struct virtio_crypto_alg_chain_data_para_stateless { + struct { + /* See VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_* above */ + uint32_t alg_chain_order; + /* length of the additional authenticated data in bytes */ + uint32_t aad_len; + + struct { + /* See VIRTIO_CRYPTO_CIPHER* above */ + uint32_t algo; + /* length of key */ + uint32_t keylen; + /* See VIRTIO_CRYPTO_OP_* above */ + uint32_t op; + } cipher; + + struct { + /* See VIRTIO_CRYPTO_HASH_* or _MAC_* above */ + uint32_t algo; + /* length of authenticated key */ + uint32_t auth_key_len; + /* See VIRTIO_CRYPTO_SYM_HASH_MODE_* above */ + uint32_t hash_mode; + } hash; + } sess_para; + + uint32_t iv_len; + /* Length of source data */ + uint32_t src_data_len; + /* Length of destination data */ + uint32_t dst_data_len; + /* Starting point for cipher processing in source data */ + uint32_t cipher_start_src_offset; + /* Length of the source data that the cipher will be computed on */ + uint32_t len_to_cipher; + /* Starting point for hash processing in source data */ + uint32_t hash_start_src_offset; + /* Length of the source data that the hash will be computed on */ + uint32_t len_to_hash; + /* Length of the additional auth data */ + uint32_t aad_len; + /* Length of the hash result */ + uint32_t hash_result_len; + uint32_t reserved; +}; + +struct virtio_crypto_hash_para_stateless { + struct { + /* See VIRTIO_CRYPTO_HASH_* above */ + uint32_t algo; + } sess_para; + + /* length of source data */ + uint32_t src_data_len; + /* hash result length */ + uint32_t hash_result_len; + uint32_t reserved; +}; + +struct virtio_crypto_mac_para_stateless { + struct { + /* See VIRTIO_CRYPTO_MAC_* above */ + uint32_t algo; + /* length of authenticated key */ + uint32_t auth_key_len; + } sess_para; + + /* length of source data */ + uint32_t src_data_len; + /* hash result length */ + uint32_t hash_result_len; +}; + +struct virtio_crypto_aead_para_stateless { + struct { + /* See VIRTIO_CRYPTO_AEAD_* above */ + uint32_t algo; + /* length of key */ + uint32_t key_len; + /* encrypt or decrypt, See above VIRTIO_CRYPTO_OP_* */ + uint32_t op; + } sess_para; + + /* + * Byte Length of valid IV data pointed to by the below iv_addr + * parameter. + */ + uint32_t iv_len; + /* Authentication tag length */ + uint32_t tag_len; + /* length of the additional authenticated data (AAD) in bytes */ + uint32_t aad_len; + /* length of source data */ + uint32_t src_data_len; + /* length of dst data, it should be at least src_data_len + tag_len */ + uint32_t dst_data_len; +}; + +struct virtio_crypto_cipher_data_req_stateless { + /* Device-readable part */ + struct virtio_crypto_cipher_para_stateless para; + uint8_t padding[48]; +}; + +struct virtio_crypto_hash_data_req_stateless { + /* Device-readable part */ + struct virtio_crypto_hash_para_stateless para; + uint8_t padding[64]; +}; + +struct virtio_crypto_mac_data_req_stateless { + /* Device-readable part */ + struct virtio_crypto_mac_para_stateless para; + uint8_t padding[64]; +}; + +struct virtio_crypto_alg_chain_data_req_stateless { + /* Device-readable part */ + struct virtio_crypto_alg_chain_data_para_stateless para; +}; + +struct virtio_crypto_sym_data_req_stateless { + union { + struct virtio_crypto_cipher_data_req_stateless cipher; + struct virtio_crypto_alg_chain_data_req_stateless chain; + uint8_t padding[72]; + } u; + + /* See above VIRTIO_CRYPTO_SYM_OP_* */ + uint32_t op_type; + uint32_t padding; +}; + +struct virtio_crypto_aead_data_req_stateless { + /* Device-readable part */ + struct virtio_crypto_aead_para_stateless para; + uint8_t padding[48]; +}; + +/* The request of the data virtqueue's packet */ +struct virtio_crypto_op_data_req_mux { + /* The size is 24 byte */ + struct virtio_crypto_op_header header; + + union { + struct { + struct virtio_crypto_sym_data_req data; + uint8_t padding[56]; + } sym_req; + struct { + struct virtio_crypto_hash_data_req data; + uint8_t padding[56]; + } hash_req; + struct { + struct virtio_crypto_mac_data_req data; + uint8_t padding[56]; + } mac_req; + struct { + struct virtio_crypto_aead_data_req data; + uint8_t padding[56]; + } aead_req; + + struct virtio_crypto_sym_data_req_stateless sym_stateless_req; + struct virtio_crypto_hash_data_req_stateless hash_stateless_req; + struct virtio_crypto_mac_data_req_stateless mac_stateless_req; + struct virtio_crypto_aead_data_req_stateless aead_stateless_req; + /* + * Making the request's total size is equal to 128 byte, and + * reserving 24 byte for future extension. + */ + uint8_t padding[104]; + } u; +}; + #define VIRTIO_CRYPTO_OK 0 #define VIRTIO_CRYPTO_ERR 1 #define VIRTIO_CRYPTO_BADMSG 2 --=20 1.8.3.1 From nobody Thu May 2 11:43:47 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1494243777890545.4340434701581; Mon, 8 May 2017 04:42:57 -0700 (PDT) Received: from localhost ([::1]:58927 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d7h44-0005n7-KN for importer@patchew.org; Mon, 08 May 2017 07:42:56 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50686) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d7h0T-0002xA-Fh for qemu-devel@nongnu.org; Mon, 08 May 2017 07:39:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d7h0O-0004Uj-Pr for qemu-devel@nongnu.org; Mon, 08 May 2017 07:39:13 -0400 Received: from szxga01-in.huawei.com ([45.249.212.187]:3975) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1d7h0N-0004Sy-W0 for qemu-devel@nongnu.org; Mon, 08 May 2017 07:39:08 -0400 Received: from 172.30.72.53 (EHLO DGGEML402-HUB.china.huawei.com) ([172.30.72.53]) by dggrg01-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id AOD07411; Mon, 08 May 2017 19:38:49 +0800 (CST) Received: from localhost (10.177.18.62) by DGGEML402-HUB.china.huawei.com (10.3.17.38) with Microsoft SMTP Server id 14.3.301.0; Mon, 8 May 2017 19:38:37 +0800 From: Gonglei To: Date: Mon, 8 May 2017 19:38:21 +0800 Message-ID: <1494243504-127980-7-git-send-email-arei.gonglei@huawei.com> X-Mailer: git-send-email 2.8.2.windows.1 In-Reply-To: <1494243504-127980-1-git-send-email-arei.gonglei@huawei.com> References: <1494243504-127980-1-git-send-email-arei.gonglei@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.18.62] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A090201.591058CB.0049, 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: 1fdab46b78938e3e3d3354b9c4ae4013 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 45.249.212.187 Subject: [Qemu-devel] [RFC v1 6/9] virtio-crypto: rework virtio_crypto_handle_request 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: pasic@linux.vnet.ibm.com, weidong.huang@huawei.com, mst@redhat.com, xin.zeng@intel.com, luonengjun@huawei.com, linqiangmin@huawei.com, Gonglei , stefanha@redhat.com, cornelia.huck@de.ibm.com, wu.wubin@huawei.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" According to the new spec, we should use different requst structure to store the data request based on whether VIRTIO_CRYPTO_F_MUX_MODE feature bit is negotiated or not. In this patch, we havn't supported stateless mode yet. The device reportes an error if both VIRTIO_CRYPTO_F_MUX_MODE and VIRTIO_CRYPTO_F_CIPHER_STATELESS_MODE are negotiated, meanwhile the header.flag doesn't set to VIRTIO_CRYPTO_FLAG_SESSION_MODE. Let's handle this scenario in the following patches. Signed-off-by: Gonglei --- hw/virtio/virtio-crypto.c | 83 ++++++++++++++++++++++++++++++++++++++++---= ---- 1 file changed, 71 insertions(+), 12 deletions(-) diff --git a/hw/virtio/virtio-crypto.c b/hw/virtio/virtio-crypto.c index 0353eb6..c4b8a2c 100644 --- a/hw/virtio/virtio-crypto.c +++ b/hw/virtio/virtio-crypto.c @@ -577,6 +577,7 @@ virtio_crypto_handle_request(VirtIOCryptoReq *request) VirtQueueElement *elem =3D &request->elem; int queue_index =3D virtio_crypto_vq2q(virtio_get_queue_index(request-= >vq)); struct virtio_crypto_op_data_req req; + struct virtio_crypto_op_data_req_mux req_mux; int ret; struct iovec *in_iov; struct iovec *out_iov; @@ -587,6 +588,9 @@ virtio_crypto_handle_request(VirtIOCryptoReq *request) uint64_t session_id; CryptoDevBackendSymOpInfo *sym_op_info =3D NULL; Error *local_err =3D NULL; + bool mux_mode_is_negotiated; + struct virtio_crypto_op_header *header; + bool is_stateless_req =3D false; =20 if (elem->out_num < 1 || elem->in_num < 1) { virtio_error(vdev, "virtio-crypto dataq missing headers"); @@ -597,12 +601,28 @@ virtio_crypto_handle_request(VirtIOCryptoReq *request) out_iov =3D elem->out_sg; in_num =3D elem->in_num; in_iov =3D elem->in_sg; - if (unlikely(iov_to_buf(out_iov, out_num, 0, &req, sizeof(req)) - !=3D sizeof(req))) { - virtio_error(vdev, "virtio-crypto request outhdr too short"); - return -1; + + mux_mode_is_negotiated =3D + virtio_vdev_has_feature(vdev, VIRTIO_CRYPTO_F_MUX_MODE); + if (!mux_mode_is_negotiated) { + if (unlikely(iov_to_buf(out_iov, out_num, 0, &req, sizeof(req)) + !=3D sizeof(req))) { + virtio_error(vdev, "virtio-crypto request outhdr too short"); + return -1; + } + iov_discard_front(&out_iov, &out_num, sizeof(req)); + + header =3D &req.header; + } else { + if (unlikely(iov_to_buf(out_iov, out_num, 0, &req_mux, + sizeof(req_mux)) !=3D sizeof(req_mux))) { + virtio_error(vdev, "virtio-crypto request outhdr too short"); + return -1; + } + iov_discard_front(&out_iov, &out_num, sizeof(req_mux)); + + header =3D &req_mux.header; } - iov_discard_front(&out_iov, &out_num, sizeof(req)); =20 if (in_iov[in_num - 1].iov_len < sizeof(struct virtio_crypto_inhdr)) { @@ -623,16 +643,51 @@ virtio_crypto_handle_request(VirtIOCryptoReq *request) request->in_num =3D in_num; request->in_iov =3D in_iov; =20 - opcode =3D ldl_le_p(&req.header.opcode); - session_id =3D ldq_le_p(&req.header.session_id); + opcode =3D ldl_le_p(&header->opcode); =20 switch (opcode) { case VIRTIO_CRYPTO_CIPHER_ENCRYPT: case VIRTIO_CRYPTO_CIPHER_DECRYPT: - ret =3D virtio_crypto_handle_sym_req(vcrypto, + if (!mux_mode_is_negotiated) { + ret =3D virtio_crypto_handle_sym_req(vcrypto, &req.u.sym_req, &sym_op_info, out_iov, out_num); + } else { + if (!virtio_vdev_has_feature(vdev, + VIRTIO_CRYPTO_F_CIPHER_STATELESS_MODE)) { + /* + * If the VIRTIO_CRYPTO_F_CIPHER_STATELESS_MODE is not + * negotiated, the driver MUST use the session mode + */ + ret =3D virtio_crypto_handle_sym_req(vcrypto, + &req_mux.u.sym_req.data, + &sym_op_info, + out_iov, out_num); + } else { + /* + * If the VIRTIO_CRYPTO_F_CIPHER_STATELESS_MODE is + * negotiated, the device MUST parse header.flag + * in order to decide which mode the driver uses. + */ + if (header->flag =3D=3D VIRTIO_CRYPTO_FLAG_SESSION_MODE) { + ret =3D virtio_crypto_handle_sym_req(vcrypto, + &req_mux.u.sym_req.data, + &sym_op_info, + out_iov, out_num); + } else { + is_stateless_req =3D true; + /* + * Handle stateless mode, that is + * header->flag =3D=3D VIRTIO_CRYPTO_FLAG_STATELESS_MO= DE + */ + virtio_error(vdev, + "virtio-crypto do not support stateless mode"); + return -1; + } + } + } + /* Serious errors, need to reset virtio crypto device */ if (ret =3D=3D -EFAULT) { return -1; @@ -640,11 +695,15 @@ virtio_crypto_handle_request(VirtIOCryptoReq *request) virtio_crypto_req_complete(request, VIRTIO_CRYPTO_NOTSUPP); virtio_crypto_free_request(request); } else { - sym_op_info->session_id =3D session_id; + if (!is_stateless_req) { + sym_op_info->op_code =3D opcode; + session_id =3D ldq_le_p(&header->session_id); + sym_op_info->session_id =3D session_id; + /* Set request's parameter */ + request->flags =3D CRYPTODEV_BACKEND_ALG_SYM; + request->u.sym_op_info =3D sym_op_info; + } =20 - /* Set request's parameter */ - request->flags =3D CRYPTODEV_BACKEND_ALG_SYM; - request->u.sym_op_info =3D sym_op_info; ret =3D cryptodev_backend_crypto_operation(vcrypto->cryptodev, request, queue_index, &local_err); if (ret < 0) { --=20 1.8.3.1 From nobody Thu May 2 11:43:47 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1494243660717925.582752418859; Mon, 8 May 2017 04:41:00 -0700 (PDT) Received: from localhost ([::1]:58922 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d7h2B-00046q-Bx for importer@patchew.org; Mon, 08 May 2017 07:40:59 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50692) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d7h0T-0002xF-Nv for qemu-devel@nongnu.org; Mon, 08 May 2017 07:39:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d7h0Q-0004VU-OY for qemu-devel@nongnu.org; Mon, 08 May 2017 07:39:13 -0400 Received: from szxga01-in.huawei.com ([45.249.212.187]:3979) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1d7h0P-0004UK-K1 for qemu-devel@nongnu.org; Mon, 08 May 2017 07:39:10 -0400 Received: from 172.30.72.53 (EHLO DGGEML402-HUB.china.huawei.com) ([172.30.72.53]) by dggrg01-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id AOD07418; Mon, 08 May 2017 19:38:50 +0800 (CST) Received: from localhost (10.177.18.62) by DGGEML402-HUB.china.huawei.com (10.3.17.38) with Microsoft SMTP Server id 14.3.301.0; Mon, 8 May 2017 19:38:38 +0800 From: Gonglei To: Date: Mon, 8 May 2017 19:38:22 +0800 Message-ID: <1494243504-127980-8-git-send-email-arei.gonglei@huawei.com> X-Mailer: git-send-email 2.8.2.windows.1 In-Reply-To: <1494243504-127980-1-git-send-email-arei.gonglei@huawei.com> References: <1494243504-127980-1-git-send-email-arei.gonglei@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.18.62] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A090206.591058CC.0068, 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: 54340a09c82bf25f2baadec14ed7ad31 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 45.249.212.187 Subject: [Qemu-devel] [RFC v1 7/9] virtio-crypto: add stateless crypto request handler 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: pasic@linux.vnet.ibm.com, weidong.huang@huawei.com, mst@redhat.com, xin.zeng@intel.com, luonengjun@huawei.com, linqiangmin@huawei.com, Gonglei , stefanha@redhat.com, cornelia.huck@de.ibm.com, wu.wubin@huawei.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" We can support stateless crypto request now. The stateless cipher request componet is: header + key + iv + src_data + dst_data and The algorithm chainning stateless request component is: header + key + auth_key + iv + aad + src_data + dst_data + hash_result Signed-off-by: Gonglei --- backends/cryptodev-builtin.c | 3 +- hw/virtio/virtio-crypto.c | 248 +++++++++++++++++++++++++++++++++++++++= +++- 2 files changed, 246 insertions(+), 5 deletions(-) diff --git a/backends/cryptodev-builtin.c b/backends/cryptodev-builtin.c index 7829999..40dc568 100644 --- a/backends/cryptodev-builtin.c +++ b/backends/cryptodev-builtin.c @@ -91,7 +91,8 @@ static void cryptodev_builtin_init( * Why this value? Just avoid to overflow when * memory allocation for each crypto request. */ - backend->conf.max_size =3D LONG_MAX - sizeof(CryptoDevBackendSymOpInfo= ); + backend->conf.max_size =3D LONG_MAX - + sizeof(CryptoDevBackendSymStatelessInfo); backend->conf.max_cipher_key_len =3D CRYPTODEV_BUITLIN_MAX_CIPHER_KEY_= LEN; backend->conf.max_auth_key_len =3D CRYPTODEV_BUITLIN_MAX_AUTH_KEY_LEN; =20 diff --git a/hw/virtio/virtio-crypto.c b/hw/virtio/virtio-crypto.c index c4b8a2c..5422f25 100644 --- a/hw/virtio/virtio-crypto.c +++ b/hw/virtio/virtio-crypto.c @@ -335,9 +335,10 @@ static void virtio_crypto_init_request(VirtIOCrypto *v= crypto, VirtQueue *vq, =20 static void virtio_crypto_free_request(VirtIOCryptoReq *req) { + size_t max_len; + if (req) { if (req->flags =3D=3D CRYPTODEV_BACKEND_ALG_SYM) { - size_t max_len; CryptoDevBackendSymOpInfo *op_info =3D req->u.sym_op_info; =20 max_len =3D op_info->iv_len + @@ -349,6 +350,21 @@ static void virtio_crypto_free_request(VirtIOCryptoReq= *req) /* Zeroize and free request data structure */ memset(op_info, 0, sizeof(*op_info) + max_len); g_free(op_info); + } else if (req->flags =3D=3D CRYPTODEV_BACKEND_ALG_SYM_STATELESS) { + CryptoDevBackendSymStatelessInfo *sym_stateless_info; + + sym_stateless_info =3D req->u.sym_stateless_info; + max_len =3D sym_stateless_info->session_info.key_len + + sym_stateless_info->session_info.auth_key_len + + sym_stateless_info->op_info.iv_len + + sym_stateless_info->op_info.src_len + + sym_stateless_info->op_info.dst_len + + sym_stateless_info->op_info.aad_len + + sym_stateless_info->op_info.digest_result_len; + /* Zeroize and free request data structure */ + memset(sym_stateless_info, 0, + sizeof(*sym_stateless_info) + max_len); + g_free(sym_stateless_info); } g_free(req); } @@ -396,6 +412,9 @@ static void virtio_crypto_req_complete(VirtIOCryptoReq = *req, uint8_t status) if (req->flags =3D=3D CRYPTODEV_BACKEND_ALG_SYM) { virtio_crypto_sym_input_data_helper(vdev, req, status, req->u.sym_op_info); + } else if (req->flags =3D=3D CRYPTODEV_BACKEND_ALG_SYM_STATELESS) { + virtio_crypto_sym_input_data_helper(vdev, req, status, + &req->u.sym_stateless_info->op_info); } stb_p(&req->in->status, status); virtqueue_push(req->vq, &req->elem, req->in_len); @@ -570,6 +589,221 @@ virtio_crypto_handle_sym_req(VirtIOCrypto *vcrypto, } =20 static int +virtio_crypto_handle_sym_stateless_req(VirtIOCrypto *vcrypto, + struct virtio_crypto_sym_data_req_stateless *req, + CryptoDevBackendSymStatelessInfo **stateless_info, + struct iovec *iov, unsigned int out_num) +{ + VirtIODevice *vdev =3D VIRTIO_DEVICE(vcrypto); + CryptoDevBackendSymStatelessInfo *sym_stateless_info; + + uint32_t op_type; + uint32_t src_len =3D 0, dst_len =3D 0; + uint32_t iv_len =3D 0; + uint32_t aad_len =3D 0, hash_result_len =3D 0; + uint32_t hash_start_src_offset =3D 0, len_to_hash =3D 0; + uint32_t cipher_start_src_offset =3D 0, len_to_cipher =3D 0; + uint32_t key_len =3D 0, auth_key_len =3D 0; + + uint64_t max_len, curr_size =3D 0; + size_t s; + + op_type =3D ldl_le_p(&req->op_type); + + if (op_type =3D=3D VIRTIO_CRYPTO_SYM_OP_CIPHER) { + key_len =3D ldl_le_p(&req->u.cipher.para.sess_para.keylen); + iv_len =3D ldl_le_p(&req->u.cipher.para.iv_len); + src_len =3D ldl_le_p(&req->u.cipher.para.src_data_len); + dst_len =3D ldl_le_p(&req->u.cipher.para.dst_data_len); + } else if (op_type =3D=3D VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING) { + key_len =3D ldl_le_p(&req->u.chain.para.sess_para.cipher.keylen); + auth_key_len =3D + ldl_le_p(&req->u.chain.para.sess_para.hash.auth_key_len); + iv_len =3D ldl_le_p(&req->u.chain.para.iv_len); + src_len =3D ldl_le_p(&req->u.chain.para.src_data_len); + dst_len =3D ldl_le_p(&req->u.chain.para.dst_data_len); + + aad_len =3D ldl_le_p(&req->u.chain.para.aad_len); + hash_result_len =3D ldl_le_p(&req->u.chain.para.hash_result_len); + hash_start_src_offset =3D ldl_le_p( + &req->u.chain.para.hash_start_src_offset); + cipher_start_src_offset =3D ldl_le_p( + &req->u.chain.para.cipher_start_src_offset); + len_to_cipher =3D ldl_le_p(&req->u.chain.para.len_to_cipher); + len_to_hash =3D ldl_le_p(&req->u.chain.para.len_to_hash); + } else { + /* VIRTIO_CRYPTO_SYM_OP_NONE */ + error_report("virtio-crypto unsupported cipher type"); + return -VIRTIO_CRYPTO_NOTSUPP; + } + + if (key_len > vcrypto->conf.max_cipher_key_len) { + virtio_error(vdev, + "virtio-crypto length of cipher key is too big: %u", key_len); + return -EFAULT; + } + + if (auth_key_len > vcrypto->conf.max_auth_key_len) { + virtio_error(vdev, + "virtio-crypto length of auth key is too big: %u", auth_key_len); + return -EFAULT; + } + + max_len =3D (uint64_t)key_len + auth_key_len + iv_len + aad_len + + src_len + dst_len + hash_result_len; + if (unlikely(max_len > vcrypto->conf.max_size)) { + virtio_error(vdev, "virtio-crypto too big length"); + return -EFAULT; + } + + sym_stateless_info =3D + g_malloc0(sizeof(CryptoDevBackendSymStatelessInfo) + max_len); + sym_stateless_info->session_info.key_len =3D key_len; + sym_stateless_info->session_info.auth_key_len =3D auth_key_len; + sym_stateless_info->op_info.iv_len =3D iv_len; + sym_stateless_info->op_info.src_len =3D src_len; + sym_stateless_info->op_info.dst_len =3D dst_len; + sym_stateless_info->op_info.aad_len =3D aad_len; + sym_stateless_info->op_info.digest_result_len =3D hash_result_len; + sym_stateless_info->op_info.hash_start_src_offset =3D + hash_start_src_offset; + sym_stateless_info->op_info.len_to_hash =3D len_to_hash; + sym_stateless_info->op_info.cipher_start_src_offset =3D + cipher_start_src_offset; + sym_stateless_info->op_info.len_to_cipher =3D len_to_cipher; + + sym_stateless_info->session_info.op_type =3D + sym_stateless_info->op_info.op_type =3D op_type; + if (op_type =3D=3D VIRTIO_CRYPTO_SYM_OP_CIPHER) { + sym_stateless_info->session_info.cipher_alg =3D + ldl_le_p(&req->u.cipher.para.sess_para.algo); + sym_stateless_info->session_info.direction =3D + ldl_le_p(&req->u.cipher.para.sess_para.op); + } else { /* It must be algorithm chain here */ + sym_stateless_info->session_info.cipher_alg =3D + ldl_le_p(&req->u.chain.para.sess_para.cipher.algo); + sym_stateless_info->session_info.direction =3D + ldl_le_p(&req->u.chain.para.sess_para.cipher.op); + sym_stateless_info->session_info.hash_alg =3D + ldl_le_p(&req->u.chain.para.sess_para.hash.algo); + sym_stateless_info->session_info.hash_mode =3D + ldl_le_p(&req->u.chain.para.sess_para.hash.hash_mode); + sym_stateless_info->session_info.alg_chain_order =3D + ldl_le_p(&req->u.chain.para.sess_para.alg_chain_order); + } + + DPRINTF("cipher_alg=3D%" PRIu32 ", info->direction=3D%" PRIu32 "\n", + sym_stateless_info->session_info.cipher_alg, + sym_stateless_info->session_info.direction); + /* Begin to parse the buffer */ + + /* + * Cipher request components: + * header + key + iv + src_data + dst_data + * + * Alg_chainning request components: + * header + key + auth_key + iv + aad + src_data + dst_data + hash_r= esult + */ + if (key_len > 0) { + DPRINTF("key_len=3D%" PRIu32 "\n", key_len); + sym_stateless_info->session_info.cipher_key =3D + sym_stateless_info->op_info.data + curr_size; + + s =3D iov_to_buf(iov, out_num, 0, + sym_stateless_info->session_info.cipher_key, key_len); + if (unlikely(s !=3D key_len)) { + virtio_error(vdev, "virtio-crypto cipher key incorrect"); + goto err; + } + iov_discard_front(&iov, &out_num, key_len); + curr_size +=3D key_len; + } + if (auth_key_len > 0) { + DPRINTF("auth_key_len=3D%" PRIu32 "\n", auth_key_len); + sym_stateless_info->session_info.auth_key =3D + sym_stateless_info->op_info.data + curr_size; + + s =3D iov_to_buf(iov, out_num, 0, + sym_stateless_info->session_info.auth_key, auth_key_len); + if (unlikely(s !=3D auth_key_len)) { + virtio_error(vdev, "virtio-crypto auth key incorrect"); + goto err; + } + iov_discard_front(&iov, &out_num, auth_key_len); + curr_size +=3D auth_key_len; + } + if (iv_len > 0) { + DPRINTF("iv_len=3D%" PRIu32 "\n", iv_len); + sym_stateless_info->op_info.iv =3D + sym_stateless_info->op_info.data + curr_size; + + s =3D iov_to_buf(iov, out_num, 0, + sym_stateless_info->op_info.iv, iv_len); + if (unlikely(s !=3D iv_len)) { + virtio_error(vdev, "virtio-crypto iv incorrect"); + goto err; + } + iov_discard_front(&iov, &out_num, iv_len); + curr_size +=3D iv_len; + } + + /* Handle additional authentication data if exists */ + if (aad_len > 0) { + DPRINTF("aad_len=3D%" PRIu32 "\n", aad_len); + sym_stateless_info->op_info.aad_data =3D + sym_stateless_info->op_info.data + curr_size; + + s =3D iov_to_buf(iov, out_num, 0, + sym_stateless_info->op_info.aad_data, aad_len); + if (unlikely(s !=3D aad_len)) { + virtio_error(vdev, "virtio-crypto additional auth data incorre= ct"); + goto err; + } + iov_discard_front(&iov, &out_num, aad_len); + + curr_size +=3D aad_len; + } + /* Handle the source data */ + if (src_len > 0) { + DPRINTF("src_len=3D%" PRIu32 "\n", src_len); + sym_stateless_info->op_info.src =3D + sym_stateless_info->op_info.data + curr_size; + + s =3D iov_to_buf(iov, out_num, 0, + sym_stateless_info->op_info.src, src_len); + if (unlikely(s !=3D src_len)) { + virtio_error(vdev, "virtio-crypto source data incorrect"); + goto err; + } + iov_discard_front(&iov, &out_num, src_len); + + curr_size +=3D src_len; + } + + /* Handle the destination data */ + sym_stateless_info->op_info.dst =3D + sym_stateless_info->op_info.data + curr_size; + curr_size +=3D dst_len; + + DPRINTF("dst_len=3D%" PRIu32 "\n", dst_len); + + /* Handle the hash digest result */ + if (hash_result_len > 0) { + DPRINTF("hash_result_len=3D%" PRIu32 "\n", hash_result_len); + sym_stateless_info->op_info.digest_result =3D + sym_stateless_info->op_info.data + curr_size; + } + + *stateless_info =3D sym_stateless_info; + + return 0; + +err: + g_free(sym_stateless_info); + return -EFAULT; +} + +static int virtio_crypto_handle_request(VirtIOCryptoReq *request) { VirtIOCrypto *vcrypto =3D request->vcrypto; @@ -591,6 +825,7 @@ virtio_crypto_handle_request(VirtIOCryptoReq *request) bool mux_mode_is_negotiated; struct virtio_crypto_op_header *header; bool is_stateless_req =3D false; + CryptoDevBackendSymStatelessInfo *stateless_info =3D NULL; =20 if (elem->out_num < 1 || elem->in_num < 1) { virtio_error(vdev, "virtio-crypto dataq missing headers"); @@ -681,9 +916,10 @@ virtio_crypto_handle_request(VirtIOCryptoReq *request) * Handle stateless mode, that is * header->flag =3D=3D VIRTIO_CRYPTO_FLAG_STATELESS_MO= DE */ - virtio_error(vdev, - "virtio-crypto do not support stateless mode"); - return -1; + ret =3D virtio_crypto_handle_sym_stateless_req(vcrypto, + &req_mux.u.sym_stateless_req, + &stateless_info, + out_iov, out_num); } } } @@ -702,6 +938,10 @@ virtio_crypto_handle_request(VirtIOCryptoReq *request) /* Set request's parameter */ request->flags =3D CRYPTODEV_BACKEND_ALG_SYM; request->u.sym_op_info =3D sym_op_info; + } else { + stateless_info->op_info.op_code =3D opcode; + request->flags =3D CRYPTODEV_BACKEND_ALG_SYM_STATELESS; + request->u.sym_stateless_info =3D stateless_info; } =20 ret =3D cryptodev_backend_crypto_operation(vcrypto->cryptodev, --=20 1.8.3.1 From nobody Thu May 2 11:43:47 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1494243656010102.69500049243516; Mon, 8 May 2017 04:40:56 -0700 (PDT) Received: from localhost ([::1]:58920 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d7h26-00043X-I4 for importer@patchew.org; Mon, 08 May 2017 07:40:54 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50684) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d7h0T-0002x9-Dz for qemu-devel@nongnu.org; Mon, 08 May 2017 07:39:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d7h0O-0004Ub-JS for qemu-devel@nongnu.org; Mon, 08 May 2017 07:39:13 -0400 Received: from szxga01-in.huawei.com ([45.249.212.187]:3976) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1d7h0N-0004TF-Vy for qemu-devel@nongnu.org; Mon, 08 May 2017 07:39:08 -0400 Received: from 172.30.72.53 (EHLO DGGEML402-HUB.china.huawei.com) ([172.30.72.53]) by dggrg01-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id AOD07405; Mon, 08 May 2017 19:38:48 +0800 (CST) Received: from localhost (10.177.18.62) by DGGEML402-HUB.china.huawei.com (10.3.17.38) with Microsoft SMTP Server id 14.3.301.0; Mon, 8 May 2017 19:38:39 +0800 From: Gonglei To: Date: Mon, 8 May 2017 19:38:23 +0800 Message-ID: <1494243504-127980-9-git-send-email-arei.gonglei@huawei.com> X-Mailer: git-send-email 2.8.2.windows.1 In-Reply-To: <1494243504-127980-1-git-send-email-arei.gonglei@huawei.com> References: <1494243504-127980-1-git-send-email-arei.gonglei@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.18.62] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020206.591058CB.0054, 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: 2ac349108ea4ea9e00bb06b20f4f275a X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 45.249.212.187 Subject: [Qemu-devel] [RFC v1 8/9] virtio-crypto: add host feature bits 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: pasic@linux.vnet.ibm.com, weidong.huang@huawei.com, mst@redhat.com, xin.zeng@intel.com, luonengjun@huawei.com, linqiangmin@huawei.com, Gonglei , stefanha@redhat.com, cornelia.huck@de.ibm.com, wu.wubin@huawei.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" We enable all feature bits acquiescently. Signed-off-by: Gonglei --- hw/virtio/virtio-crypto.c | 15 +++++++++++++++ include/hw/virtio/virtio-crypto.h | 1 + 2 files changed, 16 insertions(+) diff --git a/hw/virtio/virtio-crypto.c b/hw/virtio/virtio-crypto.c index 5422f25..3dc0ff2 100644 --- a/hw/virtio/virtio-crypto.c +++ b/hw/virtio/virtio-crypto.c @@ -1034,6 +1034,11 @@ static uint64_t virtio_crypto_get_features(VirtIODev= ice *vdev, uint64_t features, Error **errp) { + VirtIOCrypto *vcrypto =3D VIRTIO_CRYPTO(vdev); + + /* Firstly sync all virtio-crypto possible supported features */ + features |=3D vcrypto->host_features; + return features; } =20 @@ -1144,6 +1149,16 @@ static const VMStateDescription vmstate_virtio_crypt= o =3D { }; =20 static Property virtio_crypto_properties[] =3D { + DEFINE_PROP_BIT("mux_mode", VirtIOCrypto, host_features, + VIRTIO_CRYPTO_F_MUX_MODE, true), + DEFINE_PROP_BIT("cipher_stateless_mode", VirtIOCrypto, host_features, + VIRTIO_CRYPTO_F_CIPHER_STATELESS_MODE, true), + DEFINE_PROP_BIT("hash_stateless_mode", VirtIOCrypto, host_features, + VIRTIO_CRYPTO_F_HASH_STATELESS_MODE, true), + DEFINE_PROP_BIT("mac_stateless_mode", VirtIOCrypto, host_features, + VIRTIO_CRYPTO_F_MAC_STATELESS_MODE, true), + DEFINE_PROP_BIT("aead_stateless_mode", VirtIOCrypto, host_features, + VIRTIO_CRYPTO_F_AEAD_STATELESS_MODE, true), DEFINE_PROP_END_OF_LIST(), }; =20 diff --git a/include/hw/virtio/virtio-crypto.h b/include/hw/virtio/virtio-c= rypto.h index 465ad20..30ea51d 100644 --- a/include/hw/virtio/virtio-crypto.h +++ b/include/hw/virtio/virtio-crypto.h @@ -97,6 +97,7 @@ typedef struct VirtIOCrypto { int multiqueue; uint32_t curr_queues; size_t config_size; + uint32_t host_features; } VirtIOCrypto; =20 #endif /* _QEMU_VIRTIO_CRYPTO_H */ --=20 1.8.3.1 From nobody Thu May 2 11:43:47 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1494243979293285.44956592500387; Mon, 8 May 2017 04:46:19 -0700 (PDT) Received: from localhost ([::1]:58941 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d7h7J-0008JT-Nw for importer@patchew.org; Mon, 08 May 2017 07:46:17 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50689) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d7h0T-0002xD-Nm for qemu-devel@nongnu.org; Mon, 08 May 2017 07:39:16 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d7h0P-0004Ut-8N for qemu-devel@nongnu.org; Mon, 08 May 2017 07:39:13 -0400 Received: from szxga01-in.huawei.com ([45.249.212.187]:3977) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1d7h0N-0004TQ-Vp for qemu-devel@nongnu.org; Mon, 08 May 2017 07:39:09 -0400 Received: from 172.30.72.53 (EHLO DGGEML402-HUB.china.huawei.com) ([172.30.72.53]) by dggrg01-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id AOD07432; Mon, 08 May 2017 19:38:55 +0800 (CST) Received: from localhost (10.177.18.62) by DGGEML402-HUB.china.huawei.com (10.3.17.38) with Microsoft SMTP Server id 14.3.301.0; Mon, 8 May 2017 19:38:40 +0800 From: Gonglei To: Date: Mon, 8 May 2017 19:38:24 +0800 Message-ID: <1494243504-127980-10-git-send-email-arei.gonglei@huawei.com> X-Mailer: git-send-email 2.8.2.windows.1 In-Reply-To: <1494243504-127980-1-git-send-email-arei.gonglei@huawei.com> References: <1494243504-127980-1-git-send-email-arei.gonglei@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.18.62] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A090203.591058D0.000C, 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: 390f1dfff7868995e13c884d1bf0aa1f X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 45.249.212.187 Subject: [Qemu-devel] [RFC v1 9/9] qtest: emulate virtio crypto as a legacy device for experiment 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: pasic@linux.vnet.ibm.com, weidong.huang@huawei.com, mst@redhat.com, xin.zeng@intel.com, luonengjun@huawei.com, linqiangmin@huawei.com, Gonglei , stefanha@redhat.com, cornelia.huck@de.ibm.com, wu.wubin@huawei.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Because the current qtest framework do not support virtio-1 or latter devices. For experimental purpose, let's emulate the virtio crypto device as a legacy virtio device by default. Using 0x1014 as virtio crypto pci device ID because virtio crypto ID is 20 (0x14). Signed-off-by: Gonglei virtio-crypto-test: add qtest case for virtio-crypto We can simply test the functions of virtio crypto device, including session creation, session closing, cipher encryption and decryption. Quick usage: # make tests/virtio-crypto-test && ./tests/virtio-crypto-test CC tests/virtio-crypto-test.o LINK tests/virtio-crypto-test /virtio/crypto/cbc(aes-128-session-mode): OK /virtio/crypto/cbc(aes-128-stateless-mode): OK Signed-off-by: Gonglei --- docs/specs/pci-ids.txt | 2 + hw/virtio/virtio-crypto-pci.c | 4 +- include/hw/pci/pci.h | 2 + tests/Makefile.include | 3 + tests/virtio-crypto-test.c | 596 ++++++++++++++++++++++++++++++++++++++= ++++ 5 files changed, 606 insertions(+), 1 deletion(-) create mode 100644 tests/virtio-crypto-test.c diff --git a/docs/specs/pci-ids.txt b/docs/specs/pci-ids.txt index 95adee0..fe1581d 100644 --- a/docs/specs/pci-ids.txt +++ b/docs/specs/pci-ids.txt @@ -22,6 +22,7 @@ maintained as part of the virtio specification. 1af4:1004 SCSI host bus adapter device (legacy) 1af4:1005 entropy generator device (legacy) 1af4:1009 9p filesystem device (legacy) +1af4:1014 crypto device (legacy) =20 1af4:1041 network device (modern) 1af4:1042 block device (modern) @@ -32,6 +33,7 @@ maintained as part of the virtio specification. 1af4:1049 9p filesystem device (modern) 1af4:1050 virtio gpu device (modern) 1af4:1052 virtio input device (modern) +1af4:1054 crypto device (modern) =20 1af4:10f0 Available for experimental usage without registration. Must get to official ID when the code leaves the test lab (i.e. when seeking diff --git a/hw/virtio/virtio-crypto-pci.c b/hw/virtio/virtio-crypto-pci.c index 422aca3..06b5917 100644 --- a/hw/virtio/virtio-crypto-pci.c +++ b/hw/virtio/virtio-crypto-pci.c @@ -37,7 +37,6 @@ static void virtio_crypto_pci_realize(VirtIOPCIProxy *vpc= i_dev, Error **errp) } =20 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); - virtio_pci_force_virtio_1(vpci_dev); object_property_set_bool(OBJECT(vdev), true, "realized", errp); object_property_set_link(OBJECT(vcrypto), OBJECT(vcrypto->vdev.conf.cryptodev), "cryptodev", @@ -53,6 +52,9 @@ static void virtio_crypto_pci_class_init(ObjectClass *kla= ss, void *data) k->realize =3D virtio_crypto_pci_realize; set_bit(DEVICE_CATEGORY_MISC, dc->categories); dc->props =3D virtio_crypto_pci_properties; + pcidev_k->vendor_id =3D PCI_VENDOR_ID_REDHAT_QUMRANET; + pcidev_k->device_id =3D PCI_DEVICE_ID_VIRTIO_CRYPTO; + pcidev_k->revision =3D 0; pcidev_k->class_id =3D PCI_CLASS_OTHERS; } =20 diff --git a/include/hw/pci/pci.h b/include/hw/pci/pci.h index a37a2d5..9f06db6 100644 --- a/include/hw/pci/pci.h +++ b/include/hw/pci/pci.h @@ -83,6 +83,8 @@ #define PCI_DEVICE_ID_VIRTIO_RNG 0x1005 #define PCI_DEVICE_ID_VIRTIO_9P 0x1009 #define PCI_DEVICE_ID_VIRTIO_VSOCK 0x1012 +#define PCI_DEVICE_ID_VIRTIO_CRYPTO 0x1014 + =20 #define PCI_VENDOR_ID_REDHAT 0x1b36 #define PCI_DEVICE_ID_REDHAT_BRIDGE 0x0001 diff --git a/tests/Makefile.include b/tests/Makefile.include index 31931c0..e3af4b9 100644 --- a/tests/Makefile.include +++ b/tests/Makefile.include @@ -168,6 +168,8 @@ check-qtest-virtio-y +=3D tests/virtio-serial-test$(EXE= SUF) gcov-files-virtio-y +=3D i386-softmmu/hw/char/virtio-serial-bus.c check-qtest-virtio-y +=3D $(check-qtest-virtioserial-y) gcov-files-virtio-y +=3D $(gcov-files-virtioserial-y) +check-qtest-virtio-y +=3D tests/virtio-crypto-test$(EXESUF) +gcov-files-virtio-y +=3D i386-softmmu/hw/virtio/virtio-crypto.c =20 check-qtest-pci-y +=3D tests/e1000-test$(EXESUF) gcov-files-pci-y +=3D hw/net/e1000.c @@ -717,6 +719,7 @@ tests/virtio-scsi-test$(EXESUF): tests/virtio-scsi-test= .o $(libqos-virtio-obj-y) tests/virtio-9p-test$(EXESUF): tests/virtio-9p-test.o $(libqos-virtio-obj-= y) tests/virtio-serial-test$(EXESUF): tests/virtio-serial-test.o tests/virtio-console-test$(EXESUF): tests/virtio-console-test.o +tests/virtio-crypto-test$(EXESUF): tests/virtio-crypto-test.o $(libqos-vir= tio-obj-y) tests/tpci200-test$(EXESUF): tests/tpci200-test.o tests/display-vga-test$(EXESUF): tests/display-vga-test.o tests/ipoctal232-test$(EXESUF): tests/ipoctal232-test.o diff --git a/tests/virtio-crypto-test.c b/tests/virtio-crypto-test.c new file mode 100644 index 0000000..630eb9d --- /dev/null +++ b/tests/virtio-crypto-test.c @@ -0,0 +1,596 @@ +/* + * QTest testcase for VirtIO Crypto Device + * + * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD. + * + * Authors: + * Gonglei + * + * This work is licensed under the terms of the GNU GPL, version 2 or late= r. + * See the COPYING file in the top-level directory. + */ + +#include + +#include "qemu/osdep.h" +#include "libqtest.h" +#include "libqos/virtio.h" +#include "libqos/virtio-pci.h" +#include "libqos/virtio-mmio.h" +#include "libqos/pci-pc.h" +#include "libqos/malloc.h" +#include "libqos/malloc-pc.h" +#include "libqos/malloc-generic.h" +#include "qemu/bswap.h" +#include "standard-headers/linux/virtio_ids.h" +#include "standard-headers/linux/virtio_config.h" +#include "standard-headers/linux/virtio_ring.h" +#include "standard-headers/linux/virtio_crypto.h" +#include "standard-headers/linux/virtio_pci.h" + +#define QVIRTIO_CRYPTO_TIMEOUT_US (30 * 1000 * 1000) + +#define PCI_SLOT_HP 0x06 +#define PCI_SLOT 0x04 +#define PCI_FN 0x00 + +/* + * VirtIOCryptoCipherTestData: structure to describe a cipher test + * @key: A pointer to a key used by the test + * @key_len: The length of @key + * @iv: A pointer to the IV/Counter used by the test + * @iv_len: The length of @iv + * @input: A pointer to data used as input + * @ilen The length of data in @input + * @output: A pointer to what the test need to produce + * @olen: The length of data in @output + * @algo: The type of algorithm, refer to VIRTIO_CRYPTO_CIPHER_AES_* + */ +typedef struct VirtIOCryptoCipherTestData { + const char *path; + unsigned short algo; + const char *key; + const char *iv; + const char *input; + const char *output; + unsigned char key_len; + unsigned char iv_len; + unsigned short ilen; + unsigned short olen; + bool is_statelss_mode; +} VirtIOCryptoCipherTestData; + + +static VirtIOCryptoCipherTestData cipher_test_data[] =3D { + { /* From RFC 3602 */ + .path =3D "/virtio/crypto/cbc(aes-128-session-mode)", + .algo =3D VIRTIO_CRYPTO_CIPHER_AES_CBC, + .key =3D "\x06\xa9\x21\x40\x36\xb8\xa1\x5b" + "\x51\x2e\x03\xd5\x34\x12\x00\x06", + .key_len =3D 16, + .iv =3D "\x3d\xaf\xba\x42\x9d\x9e\xb4\x30" + "\xb4\x22\xda\x80\x2c\x9f\xac\x41", + .iv_len =3D 16, + .input =3D "Single block msg", + .ilen =3D 16, + .output =3D "\xe3\x53\x77\x9c\x10\x79\xae\xb8" + "\x27\x08\x94\x2d\xbe\x77\x18\x1a", + .olen =3D 16, + .is_statelss_mode =3D false, + }, + { /* From RFC 3602 */ + .path =3D "/virtio/crypto/cbc(aes-128-stateless-mode)", + .algo =3D VIRTIO_CRYPTO_CIPHER_AES_CBC, + .key =3D "\x06\xa9\x21\x40\x36\xb8\xa1\x5b" + "\x51\x2e\x03\xd5\x34\x12\x00\x06", + .key_len =3D 16, + .iv =3D "\x3d\xaf\xba\x42\x9d\x9e\xb4\x30" + "\xb4\x22\xda\x80\x2c\x9f\xac\x41", + .iv_len =3D 16, + .input =3D "Single block msg", + .ilen =3D 16, + .output =3D "\xe3\x53\x77\x9c\x10\x79\xae\xb8" + "\x27\x08\x94\x2d\xbe\x77\x18\x1a", + .olen =3D 16, + .is_statelss_mode =3D true, + }, +}; + +static QPCIBus *virtio_crypto_test_start(void) +{ + char *cmdline; + + cmdline =3D g_strdup_printf( + "-object cryptodev-backend-builtin,id=3Dcryptodev0 " + "-device virtio-crypto-pci,id=3Dcrypto0," + "cryptodev=3Dcryptodev0"); + + qtest_start(cmdline); + g_free(cmdline); + + return qpci_init_pc(NULL); +} + +static void test_end(void) +{ + qtest_end(); +} + +static QVirtioPCIDevice *virtio_crypto_pci_init(QPCIBus *bus, int slot) +{ + QVirtioPCIDevice *dev; + + dev =3D qvirtio_pci_device_find(bus, VIRTIO_ID_CRYPTO); + g_assert(dev !=3D NULL); + g_assert_cmphex(dev->vdev.device_type, =3D=3D, VIRTIO_ID_CRYPTO); + + qvirtio_pci_device_enable(dev); + qvirtio_reset(&dev->vdev); + qvirtio_set_acknowledge(&dev->vdev); + qvirtio_set_driver(&dev->vdev); + + return dev; +} + +static uint64_t +virtio_crypto_ctrl_request(QGuestAllocator *alloc, + struct virtio_crypto_op_ctrl_req *req) +{ + uint64_t addr; + + addr =3D guest_alloc(alloc, sizeof(*req)); + + memwrite(addr, req, sizeof(*req)); + + return addr; +} + +static uint64_t +virtio_crypto_data_request(QGuestAllocator *alloc, + struct virtio_crypto_op_data_req *req) +{ + uint64_t addr; + + addr =3D guest_alloc(alloc, sizeof(*req)); + + memwrite(addr, req, sizeof(*req)); + + return addr; +} + +static void +virtio_crypto_driver_init(QVirtioDevice *dev) +{ + /* Read configure space to get supported crypto services */ + + qvirtio_set_driver_ok(dev); +} + +static uint64_t +virtio_crypto_create_session(QVirtioDevice *dev, + QGuestAllocator *alloc, QVirtQueue *vq, + VirtIOCryptoCipherTestData *data, + int encrypt) +{ + uint32_t free_head; + struct virtio_crypto_op_ctrl_req ctrl; + struct virtio_crypto_session_input input; + uint32_t key_len =3D data->key_len; + uint64_t req_addr; + uint64_t key_addr, input_addr; /* cipher key guest physical address */ + uint64_t session_id; + QVRingIndirectDesc *indirect; + + /* Create an encryption session */ + ctrl.header.opcode =3D VIRTIO_CRYPTO_CIPHER_CREATE_SESSION; + ctrl.header.algo =3D data->algo; + /* Set the default dataqueue id to 0 */ + ctrl.header.queue_id =3D 0; + + /* Pad cipher's parameters */ + ctrl.u.sym_create_session.op_type =3D VIRTIO_CRYPTO_SYM_OP_CIPHER; + ctrl.u.sym_create_session.u.cipher.para.algo =3D ctrl.header.algo; + ctrl.u.sym_create_session.u.cipher.para.keylen =3D key_len; + if (encrypt) { + ctrl.u.sym_create_session.u.cipher.para.op =3D VIRTIO_CRYPTO_OP_EN= CRYPT; + } else { + ctrl.u.sym_create_session.u.cipher.para.op =3D VIRTIO_CRYPTO_OP_DE= CRYPT; + } + + req_addr =3D virtio_crypto_ctrl_request(alloc, &ctrl); + + /* Pad cipher's output data */ + key_addr =3D guest_alloc(alloc, key_len); + memwrite(key_addr, data->key, key_len); + + input.status =3D VIRTIO_CRYPTO_ERR; + input_addr =3D guest_alloc(alloc, sizeof(input)); + memwrite(input_addr, &input, sizeof(input)); + + indirect =3D qvring_indirect_desc_setup(dev, alloc, 3); + qvring_indirect_desc_add(indirect, req_addr, sizeof(ctrl), false); + qvring_indirect_desc_add(indirect, key_addr, key_len, false); + qvring_indirect_desc_add(indirect, input_addr, sizeof(input), true); + free_head =3D qvirtqueue_add_indirect(vq, indirect); + + qvirtqueue_kick(dev, vq, free_head); + + qvirtio_wait_queue_isr(dev, vq, QVIRTIO_CRYPTO_TIMEOUT_US); + + /* calculate the offset of input data */ + + memread(input_addr, &input, sizeof(input)); + + /* Verify the result */ + g_assert_cmpint(input.status, =3D=3D, VIRTIO_CRYPTO_OK); + + session_id =3D input.session_id; + + g_free(indirect); + guest_free(alloc, input_addr); + guest_free(alloc, key_addr); + guest_free(alloc, req_addr); + + return session_id; +} + +static void +virtio_crypto_close_session(QVirtioDevice *dev, + QGuestAllocator *alloc, QVirtQueue *vq, + uint64_t session_id) +{ + uint32_t free_head; + struct virtio_crypto_op_ctrl_req ctrl; + uint64_t req_addr, status_addr; + uint8_t status; + QVRingIndirectDesc *indirect; + + /* Create an encryption session */ + ctrl.header.opcode =3D VIRTIO_CRYPTO_CIPHER_DESTROY_SESSION; + /* Set the default dataqueue id to 0 */ + ctrl.header.queue_id =3D 0; + + ctrl.u.destroy_session.session_id =3D session_id; + + req_addr =3D virtio_crypto_ctrl_request(alloc, &ctrl); + + status_addr =3D guest_alloc(alloc, sizeof(status)); + writel(status_addr, VIRTIO_CRYPTO_ERR); + + indirect =3D qvring_indirect_desc_setup(dev, alloc, 2); + qvring_indirect_desc_add(indirect, req_addr, sizeof(ctrl), false); + qvring_indirect_desc_add(indirect, status_addr, sizeof(status), true); + free_head =3D qvirtqueue_add_indirect(vq, indirect); + + qvirtqueue_kick(dev, vq, free_head); + + qvirtio_wait_queue_isr(dev, vq, QVIRTIO_CRYPTO_TIMEOUT_US); + + /* Verify the result */ + status =3D readl(status_addr); + g_assert_cmpint(status, =3D=3D, VIRTIO_CRYPTO_OK); + + g_free(indirect); + guest_free(alloc, req_addr); + guest_free(alloc, status_addr); +} + + +static void +virtio_crypto_test_cipher_session_mode(QVirtioDevice *dev, + QGuestAllocator *alloc, QVirtQueue *ctrlq, + QVirtQueue *vq, VirtIOCryptoCipherTestData *data, + int encrypt) +{ + uint32_t free_head; + struct virtio_crypto_op_data_req req; + uint64_t req_addr, status_addr; + uint64_t iv_addr =3D 0, src_addr, dst_addr; + uint64_t session_id; + char *output; + uint32_t src_len, dst_len; + uint8_t status; + QVRingIndirectDesc *indirect; + uint8_t entry_num; + + /* Create a session */ + session_id =3D virtio_crypto_create_session(dev, alloc, + ctrlq, data, encrypt); + + /* Head of operation */ + req.header.session_id =3D session_id; + if (encrypt) { + req.header.opcode =3D VIRTIO_CRYPTO_CIPHER_ENCRYPT; + } else { + req.header.opcode =3D VIRTIO_CRYPTO_CIPHER_DECRYPT; + } + + req.u.sym_req.op_type =3D VIRTIO_CRYPTO_SYM_OP_CIPHER; + req.u.sym_req.u.cipher.para.iv_len =3D data->iv_len; + req.u.sym_req.u.cipher.para.src_data_len =3D data->ilen; + req.u.sym_req.u.cipher.para.dst_data_len =3D data->olen; + + req_addr =3D virtio_crypto_data_request(alloc, &req); + + /* IV */ + if (data->iv_len > 0) { + iv_addr =3D guest_alloc(alloc, data->iv_len); + memwrite(iv_addr, data->iv, data->iv_len); + + /* header + iv + src + dst + status */ + entry_num =3D 5; + } else { + /* header + src + dst + status */ + entry_num =3D 4; + } + + if (encrypt) { + src_len =3D data->ilen; + dst_len =3D data->olen; + /* Source data is the input data which is a single buffer */ + src_addr =3D guest_alloc(alloc, src_len); + memwrite(src_addr, data->input, src_len); + } else { + src_len =3D data->olen; + dst_len =3D data->ilen; + /* Source data is the output data which is a single buffer */ + src_addr =3D guest_alloc(alloc, src_len); + memwrite(src_addr, data->output, src_len); + } + + dst_addr =3D guest_alloc(alloc, dst_len); + + status_addr =3D guest_alloc(alloc, sizeof(status)); + writel(status_addr, VIRTIO_CRYPTO_ERR); + + /* Allocate descripto table entries */ + indirect =3D qvring_indirect_desc_setup(dev, alloc, entry_num); + qvring_indirect_desc_add(indirect, req_addr, sizeof(req), false); + if (data->iv_len > 0) { + qvring_indirect_desc_add(indirect, iv_addr, data->iv_len, false); + } + qvring_indirect_desc_add(indirect, src_addr, src_len, false); + qvring_indirect_desc_add(indirect, dst_addr, dst_len, true); + qvring_indirect_desc_add(indirect, status_addr, sizeof(status), true); + free_head =3D qvirtqueue_add_indirect(vq, indirect); + + qvirtqueue_kick(dev, vq, free_head); + + qvirtio_wait_queue_isr(dev, vq, QVIRTIO_CRYPTO_TIMEOUT_US); + + /* Verify the result */ + status =3D readl(status_addr); + g_assert_cmpint(status, =3D=3D, VIRTIO_CRYPTO_OK); + + output =3D g_malloc0(dst_len); + memread(dst_addr, output, dst_len); + if (encrypt) { + g_assert_cmpstr(output, =3D=3D, data->output); + } else { + g_assert_cmpstr(output, =3D=3D, data->input); + } + g_free(output); + + g_free(indirect); + + if (data->iv_len > 0) { + guest_free(alloc, iv_addr); + } + guest_free(alloc, src_addr); + guest_free(alloc, dst_addr); + guest_free(alloc, req_addr); + guest_free(alloc, status_addr); + + /* Close the session */ + virtio_crypto_close_session(dev, alloc, ctrlq, session_id); +} + +static void +virtio_crypto_test_cipher_stateless_mode(QVirtioDevice *dev, + QGuestAllocator *alloc, + QVirtQueue *vq, VirtIOCryptoCipherTestData *data, + int encrypt) +{ + uint32_t free_head; + struct virtio_crypto_op_data_req_mux req; + uint64_t req_addr, status_addr; + uint64_t iv_addr =3D 0, src_addr, dst_addr, key_addr; + char *output; + uint32_t src_len, dst_len; + uint8_t status; + QVRingIndirectDesc *indirect; + uint8_t entry_num; + + /* Head of operation */ + req.header.flag =3D VIRTIO_CRYPTO_FLAG_STATELESS_MODE; + if (encrypt) { + req.header.opcode =3D VIRTIO_CRYPTO_CIPHER_ENCRYPT; + req.u.sym_stateless_req.u.cipher.para.sess_para.op =3D + VIRTIO_CRYPTO_OP_ENCRYPT; + } else { + req.header.opcode =3D VIRTIO_CRYPTO_CIPHER_DECRYPT; + req.u.sym_stateless_req.u.cipher.para.sess_para.op =3D + VIRTIO_CRYPTO_OP_DECRYPT; + } + + req.u.sym_stateless_req.op_type =3D VIRTIO_CRYPTO_SYM_OP_CIPHER; + req.u.sym_stateless_req.u.cipher.para.sess_para.algo =3D data->algo; + req.u.sym_stateless_req.u.cipher.para.sess_para.keylen =3D data->key_l= en; + req.u.sym_stateless_req.u.cipher.para.iv_len =3D data->iv_len; + req.u.sym_stateless_req.u.cipher.para.src_data_len =3D data->ilen; + req.u.sym_stateless_req.u.cipher.para.dst_data_len =3D data->olen; + + req_addr =3D guest_alloc(alloc, sizeof(req)); + memwrite(req_addr, &req, sizeof(req)); + + g_assert(data->key_len > 0); + key_addr =3D guest_alloc(alloc, data->key_len); + memwrite(key_addr, data->key, data->key_len); + + /* IV */ + if (data->iv_len > 0) { + iv_addr =3D guest_alloc(alloc, data->iv_len); + memwrite(iv_addr, data->iv, data->iv_len); + + /* header + key + iv + src + dst + status */ + entry_num =3D 6; + } else { + /* header + key + src + dst + status */ + entry_num =3D 5; + } + + if (encrypt) { + src_len =3D data->ilen; + dst_len =3D data->olen; + /* Source data is the input data which is a single buffer */ + src_addr =3D guest_alloc(alloc, src_len); + memwrite(src_addr, data->input, src_len); + } else { + src_len =3D data->olen; + dst_len =3D data->ilen; + /* Source data is the output data which is a single buffer */ + src_addr =3D guest_alloc(alloc, src_len); + memwrite(src_addr, data->output, src_len); + } + + dst_addr =3D guest_alloc(alloc, dst_len); + + status_addr =3D guest_alloc(alloc, sizeof(status)); + writel(status_addr, VIRTIO_CRYPTO_ERR); + + /* Allocate desc table entries */ + indirect =3D qvring_indirect_desc_setup(dev, alloc, entry_num); + qvring_indirect_desc_add(indirect, req_addr, sizeof(req), false); + qvring_indirect_desc_add(indirect, key_addr, data->key_len, false); + if (data->iv_len > 0) { + qvring_indirect_desc_add(indirect, iv_addr, data->iv_len, false); + } + qvring_indirect_desc_add(indirect, src_addr, src_len, false); + qvring_indirect_desc_add(indirect, dst_addr, dst_len, true); + qvring_indirect_desc_add(indirect, status_addr, sizeof(status), true); + free_head =3D qvirtqueue_add_indirect(vq, indirect); + + qvirtqueue_kick(dev, vq, free_head); + + qvirtio_wait_queue_isr(dev, vq, QVIRTIO_CRYPTO_TIMEOUT_US); + + /* Verify the result */ + status =3D readl(status_addr); + g_assert_cmpint(status, =3D=3D, VIRTIO_CRYPTO_OK); + + output =3D g_malloc0(dst_len); + memread(dst_addr, output, dst_len); + if (encrypt) { + g_assert_cmpstr(output, =3D=3D, data->output); + } else { + g_assert_cmpstr(output, =3D=3D, data->input); + } + g_free(output); + + g_free(indirect); + guest_free(alloc, key_addr); + if (data->iv_len > 0) { + guest_free(alloc, iv_addr); + } + guest_free(alloc, src_addr); + guest_free(alloc, dst_addr); + guest_free(alloc, req_addr); + guest_free(alloc, status_addr); +} + +static void +virtio_crypto_test_cipher(QVirtioDevice *dev, + QGuestAllocator *alloc, QVirtQueue *ctrlq, + QVirtQueue *dataq, VirtIOCryptoCipherTestData *data, + int encrypt) +{ + if (!data->is_statelss_mode) { + virtio_crypto_test_cipher_session_mode(dev, alloc, + ctrlq, dataq, data, encrypt); + } else { + virtio_crypto_test_cipher_stateless_mode(dev, alloc, + dataq, data, encrypt); + } +} + +static void virtio_crypto_pci_basic(void *opaque) +{ + VirtIOCryptoCipherTestData *test_data =3D opaque; + QVirtioPCIDevice *dev; + QPCIBus *bus; + QGuestAllocator *alloc; + QVirtQueuePCI *dataq, *controlq; + uint32_t features; + + bus =3D virtio_crypto_test_start(); + dev =3D virtio_crypto_pci_init(bus, PCI_SLOT); + + alloc =3D pc_alloc_init(); + + features =3D qvirtio_get_features(&dev->vdev); + g_assert_cmphex(features & (1u << VIRTIO_RING_F_INDIRECT_DESC), !=3D, = 0); + + if (!test_data->is_statelss_mode) { + features =3D features & ~(QVIRTIO_F_BAD_FEATURE | + (1u << VIRTIO_RING_F_EVENT_IDX | + 1u << VIRTIO_CRYPTO_F_MUX_MODE | + 1u << VIRTIO_CRYPTO_F_CIPHER_STATELESS_MOD= E)); + } else { + features =3D features & ~(QVIRTIO_F_BAD_FEATURE | + (1u << VIRTIO_RING_F_EVENT_IDX)); + } + qvirtio_set_features(&dev->vdev, features); + + dataq =3D (QVirtQueuePCI *)qvirtqueue_setup(&dev->vdev, + alloc, 0); + controlq =3D (QVirtQueuePCI *)qvirtqueue_setup(&dev->vdev, + alloc, 1); + + virtio_crypto_driver_init(&dev->vdev); + + /* Step 1: Encryption */ + virtio_crypto_test_cipher(&dev->vdev, alloc, + &controlq->vq, &dataq->vq, + test_data, 1); + /* Step 2: Decryption */ + virtio_crypto_test_cipher(&dev->vdev, alloc, + &controlq->vq, &dataq->vq, + test_data, 0); + + /* End test */ + guest_free(alloc, dataq->vq.desc); + guest_free(alloc, controlq->vq.desc); + pc_alloc_uninit(alloc); + qvirtio_pci_device_disable(dev); + g_free(dev); + qpci_free_pc(bus); + test_end(); +} + +int main(int argc, char **argv) +{ + const char *qemu; + const char *arch; + int i, ret; + + qemu =3D getenv("QTEST_QEMU_BINARY"); + if (qemu =3D=3D NULL) { + ret =3D setenv("QTEST_QEMU_BINARY", + "x86_64-softmmu/qemu-system-x86_64", 0); + g_assert(ret =3D=3D 0); + } + + arch =3D qtest_get_arch(); + + g_test_init(&argc, &argv, NULL); + + if (strcmp(arch, "i386") =3D=3D 0 || strcmp(arch, "x86_64") =3D=3D 0) { + for (i =3D 0; i < G_N_ELEMENTS(cipher_test_data); i++) { + g_test_add_data_func(cipher_test_data[i].path, + (void *)&cipher_test_data[i], + (GTestDataFunc)virtio_crypto_pci_basic); + } + } + + return g_test_run(); +} --=20 1.8.3.1