From nobody Sat Feb 7 07:24:32 2026 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.zohomail.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 1500456706709286.08897556919555; Wed, 19 Jul 2017 02:31:46 -0700 (PDT) Received: from localhost ([::1]:60753 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dXlKZ-00053r-Uo for importer@patchew.org; Wed, 19 Jul 2017 05:31:43 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38656) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dXl63-0000pl-NO for qemu-devel@nongnu.org; Wed, 19 Jul 2017 05:16:45 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dXl62-0001gI-Cn for qemu-devel@nongnu.org; Wed, 19 Jul 2017 05:16:43 -0400 Received: from mx1.redhat.com ([209.132.183.28]:48732) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dXl62-0001fO-3H for qemu-devel@nongnu.org; Wed, 19 Jul 2017 05:16:42 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1D6433B722; Wed, 19 Jul 2017 09:16:41 +0000 (UTC) Received: from t460.redhat.com (ovpn-117-51.ams2.redhat.com [10.36.117.51]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9A7C37FCE0; Wed, 19 Jul 2017 09:16:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 1D6433B722 Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=berrange@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 1D6433B722 From: "Daniel P. Berrange" To: qemu-devel@nongnu.org Date: Wed, 19 Jul 2017 10:15:56 +0100 Message-Id: <20170719091600.8555-15-berrange@redhat.com> In-Reply-To: <20170719091600.8555-1-berrange@redhat.com> References: <20170719091600.8555-1-berrange@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Wed, 19 Jul 2017 09:16:41 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL v2 14/18] crypto: hash: add afalg-backend hash 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: Peter Maydell , "Longpeng\(Mike\)" 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 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: "Longpeng(Mike)" Adds afalg-backend hash support: introduces some private APIs firstly, and then intergrates them into qcrypto_hash_afalg_driver. Signed-off-by: Longpeng(Mike) Signed-off-by: Daniel P. Berrange --- crypto/Makefile.objs | 1 + crypto/afalgpriv.h | 1 + crypto/hash-afalg.c | 136 +++++++++++++++++++++++++++++++++++++++++++++++= ++++ crypto/hash.c | 17 +++++++ crypto/hashpriv.h | 8 +++ 5 files changed, 163 insertions(+) create mode 100644 crypto/hash-afalg.c diff --git a/crypto/Makefile.objs b/crypto/Makefile.objs index d2e8fa866b..2b99e08062 100644 --- a/crypto/Makefile.objs +++ b/crypto/Makefile.objs @@ -12,6 +12,7 @@ crypto-obj-y +=3D desrfb.o crypto-obj-y +=3D cipher.o crypto-obj-$(CONFIG_AF_ALG) +=3D afalg.o crypto-obj-$(CONFIG_AF_ALG) +=3D cipher-afalg.o +crypto-obj-$(CONFIG_AF_ALG) +=3D hash-afalg.o crypto-obj-y +=3D tlscreds.o crypto-obj-y +=3D tlscredsanon.o crypto-obj-y +=3D tlscredsx509.o diff --git a/crypto/afalgpriv.h b/crypto/afalgpriv.h index d0941d4ef6..f6550b5c51 100644 --- a/crypto/afalgpriv.h +++ b/crypto/afalgpriv.h @@ -24,6 +24,7 @@ #endif =20 #define AFALG_TYPE_CIPHER "skcipher" +#define AFALG_TYPE_HASH "hash" =20 #define ALG_OPTYPE_LEN 4 #define ALG_MSGIV_LEN(len) (sizeof(struct af_alg_iv) + (len)) diff --git a/crypto/hash-afalg.c b/crypto/hash-afalg.c new file mode 100644 index 0000000000..70ab414cdf --- /dev/null +++ b/crypto/hash-afalg.c @@ -0,0 +1,136 @@ +/* + * QEMU Crypto af_alg-backend hash support + * + * Copyright (c) 2017 HUAWEI TECHNOLOGIES CO., LTD. + * + * Authors: + * Longpeng(Mike) + * + * This work is licensed under the terms of the GNU GPL, version 2 or + * (at your option) any later version. See the COPYING file in the + * top-level directory. + */ +#include "qemu/osdep.h" +#include "qemu/iov.h" +#include "qemu/sockets.h" +#include "qemu-common.h" +#include "qapi/error.h" +#include "crypto/hash.h" +#include "hashpriv.h" + +static char * +qcrypto_afalg_hash_format_name(QCryptoHashAlgorithm alg, + Error **errp) +{ + char *name; + const char *alg_name; + + switch (alg) { + case QCRYPTO_HASH_ALG_MD5: + alg_name =3D "md5"; + break; + case QCRYPTO_HASH_ALG_SHA1: + alg_name =3D "sha1"; + break; + case QCRYPTO_HASH_ALG_SHA224: + alg_name =3D "sha224"; + break; + case QCRYPTO_HASH_ALG_SHA256: + alg_name =3D "sha256"; + break; + case QCRYPTO_HASH_ALG_SHA384: + alg_name =3D "sha384"; + break; + case QCRYPTO_HASH_ALG_SHA512: + alg_name =3D "sha512"; + break; + case QCRYPTO_HASH_ALG_RIPEMD160: + alg_name =3D "rmd160"; + break; + + default: + error_setg(errp, "Unsupported hash algorithm %d", alg); + return NULL; + } + + name =3D g_strdup_printf("%s", alg_name); + + return name; +} + +static QCryptoAFAlg * +qcrypto_afalg_hash_ctx_new(QCryptoHashAlgorithm alg, Error **errp) +{ + QCryptoAFAlg *afalg; + char *name; + + name =3D qcrypto_afalg_hash_format_name(alg, errp); + if (!name) { + return NULL; + } + + afalg =3D qcrypto_afalg_comm_alloc(AFALG_TYPE_HASH, name, errp); + if (!afalg) { + g_free(name); + return NULL; + } + + g_free(name); + + return afalg; +} + +static int +qcrypto_afalg_hash_bytesv(QCryptoHashAlgorithm alg, + const struct iovec *iov, + size_t niov, uint8_t **result, + size_t *resultlen, + Error **errp) +{ + QCryptoAFAlg *afalg; + struct iovec outv; + int ret =3D 0; + const int expect_len =3D qcrypto_hash_digest_len(alg); + + if (*resultlen =3D=3D 0) { + *resultlen =3D expect_len; + *result =3D g_new0(uint8_t, *resultlen); + } else if (*resultlen !=3D expect_len) { + error_setg(errp, + "Result buffer size %zu is not match hash %d", + *resultlen, expect_len); + return -1; + } + + afalg =3D qcrypto_afalg_hash_ctx_new(alg, errp); + if (!afalg) { + return -1; + } + + /* send data to kernel's crypto core */ + ret =3D iov_send_recv(afalg->opfd, iov, niov, + 0, iov_size(iov, niov), true); + if (ret < 0) { + error_setg_errno(errp, errno, "Send data to afalg-core failed"); + goto out; + } + + /* hash && get result */ + outv.iov_base =3D *result; + outv.iov_len =3D *resultlen; + ret =3D iov_send_recv(afalg->opfd, &outv, 1, + 0, iov_size(&outv, 1), false); + if (ret < 0) { + error_setg_errno(errp, errno, "Recv result from afalg-core failed"= ); + } else { + ret =3D 0; + } + +out: + qcrypto_afalg_comm_free(afalg); + return ret; +} + +QCryptoHashDriver qcrypto_hash_afalg_driver =3D { + .hash_bytesv =3D qcrypto_afalg_hash_bytesv, +}; diff --git a/crypto/hash.c b/crypto/hash.c index c43fd874fa..ac59c63d5f 100644 --- a/crypto/hash.c +++ b/crypto/hash.c @@ -46,6 +46,23 @@ int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg, size_t *resultlen, Error **errp) { +#ifdef CONFIG_AF_ALG + int ret; + + ret =3D qcrypto_hash_afalg_driver.hash_bytesv(alg, iov, niov, + result, resultlen, + errp); + if (ret =3D=3D 0) { + return ret; + } + + /* + * TODO: + * Maybe we should treat some afalg errors as fatal + */ + error_free(*errp); +#endif + return qcrypto_hash_lib_driver.hash_bytesv(alg, iov, niov, result, resultlen, errp); diff --git a/crypto/hashpriv.h b/crypto/hashpriv.h index 5e505e0492..cee26ccb47 100644 --- a/crypto/hashpriv.h +++ b/crypto/hashpriv.h @@ -28,4 +28,12 @@ struct QCryptoHashDriver { =20 extern QCryptoHashDriver qcrypto_hash_lib_driver; =20 +#ifdef CONFIG_AF_ALG + +#include "afalgpriv.h" + +extern QCryptoHashDriver qcrypto_hash_afalg_driver; + +#endif + #endif --=20 2.13.0