From patchwork Fri Jul 14 11:38:30 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: long mike X-Patchwork-Id: 9840703 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 36F5E60212 for ; Fri, 14 Jul 2017 11:55:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 29DA928752 for ; Fri, 14 Jul 2017 11:55:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1DB6A2876A; Fri, 14 Jul 2017 11:55:19 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 9FC9F28752 for ; Fri, 14 Jul 2017 11:55:18 +0000 (UTC) Received: from localhost ([::1]:37243 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dVzBl-00006Q-T9 for patchwork-qemu-devel@patchwork.kernel.org; Fri, 14 Jul 2017 07:55:17 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35587) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dVyvv-0000xF-G4 for qemu-devel@nongnu.org; Fri, 14 Jul 2017 07:38:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dVyvu-0007n9-OU for qemu-devel@nongnu.org; Fri, 14 Jul 2017 07:38:55 -0400 Received: from mail-pf0-x243.google.com ([2607:f8b0:400e:c00::243]:36077) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dVyvu-0007mm-JW for qemu-devel@nongnu.org; Fri, 14 Jul 2017 07:38:54 -0400 Received: by mail-pf0-x243.google.com with SMTP id z6so10753935pfk.3 for ; Fri, 14 Jul 2017 04:38:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=xeeBA9Xf4XM1R9yH0ixx/jOKrcEJCfPziyHxLwOeoiQ=; b=rfWFqFP2p+gtO4icCrew8ytp44SPZcGdIQC581bdYyRA3kqUbOo2OmOC29lsqa8BF/ PmGphv4vt9jnFUnnWTlFKD5HxNQhBs/qP7iMgEYenoDGbkqkx0icA3srCkqj/or6MQ+M +hqGiedguQbVQeCLSMzuVRK/ExSrx8yAiCNM8eSNwkNNWirNvCIAHnXg9SihN5oXJQzj 5+zQtV6Jb5Ev0n9iswBtjMVfPqgq4vPkhaIrWfgXEAlNBjKNawMXxO8nM/VEoHpgH9TV /1Fr0geC43AkSTFiOpxpMzyR532BV0EZt8Pt43pd4r6OCWXflpo3JzXsnQn6JfaItwzs 4SBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=xeeBA9Xf4XM1R9yH0ixx/jOKrcEJCfPziyHxLwOeoiQ=; b=tGXdzqfoG1JY/4ka5g8Snrv6F3V6TN1uJTMqOCYf7kgqWsfACObToA+B7Fd1o8lIJM MqZkmLDy51I1JJAxyqxjz/voOpMPG8WuUAZ5ru7PJyNC8oq8o4tD+acLnTzB0nerZxsY zzRHwW7VDnU3wzy0S/sq9gGOhxYLR02cksSBd8Mom3iGC5uI5bUlFqim+welfr68gm6R bGB+XFvGWwEwYPDyCVlTMAYyxF0umtZOl0SdlysJmWt99vOL8Qm2t6mXDQB6YdROeZJ+ GBOrIPPDdoVoq9hk+k3nhzq4zar7hNubXqSQhiQb3nlYVPhTQtiWjve3pauNMgxW4+24 /B1w== X-Gm-Message-State: AIVw111+fcD6GDdQYzV1hbaYPYdezpf5FXXcoycTA2HUT/PckSN7U6ob HIQprYOoSbPhMg== X-Received: by 10.84.143.131 with SMTP id 3mr15121740plz.17.1500032333849; Fri, 14 Jul 2017 04:38:53 -0700 (PDT) Received: from localhost (45.78.25.254.16clouds.com. [45.78.25.254]) by smtp.gmail.com with ESMTPSA id d3sm19176057pfj.47.2017.07.14.04.38.53 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Fri, 14 Jul 2017 04:38:53 -0700 (PDT) From: longpeng.mike@gmail.com To: berrange@redhat.com Date: Fri, 14 Jul 2017 07:38:30 -0400 Message-Id: <1500032321-13951-9-git-send-email-longpeng.mike@gmail.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1500032321-13951-1-git-send-email-longpeng.mike@gmail.com> References: <1500032321-13951-1-git-send-email-longpeng.mike@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::243 Subject: [Qemu-devel] [PATCH v5 08/18] crypto: hmac: introduce qcrypto_hmac_ctx_new for gcrypt-backend 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: "Longpeng\(Mike\)" , arei.gonglei@huawei.com, weidong.huang@huawei.com, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: "Longpeng(Mike)" 1) Fix a handle-leak problem in qcrypto_hmac_new(), didn't free ctx->handle if gcry_mac_setkey fails. 2) Extracts qcrypto_hmac_ctx_new() from qcrypto_hmac_new() for gcrypt-backend impls. Reviewed-by: Daniel P. Berrange Reviewed-by: Gonglei Signed-off-by: Longpeng(Mike) --- crypto/hmac-gcrypt.c | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/crypto/hmac-gcrypt.c b/crypto/hmac-gcrypt.c index 21189e6..372ad7f 100644 --- a/crypto/hmac-gcrypt.c +++ b/crypto/hmac-gcrypt.c @@ -42,11 +42,11 @@ bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg) return false; } -QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg, - const uint8_t *key, size_t nkey, - Error **errp) +static QCryptoHmacGcrypt * +qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg, + const uint8_t *key, size_t nkey, + Error **errp) { - QCryptoHmac *hmac; QCryptoHmacGcrypt *ctx; gcry_error_t err; @@ -56,9 +56,6 @@ QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg, return NULL; } - hmac = g_new0(QCryptoHmac, 1); - hmac->alg = alg; - ctx = g_new0(QCryptoHmacGcrypt, 1); err = gcry_mac_open(&ctx->handle, qcrypto_hmac_alg_map[alg], @@ -73,15 +70,14 @@ QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg, if (err != 0) { error_setg(errp, "Cannot set key: %s", gcry_strerror(err)); + gcry_mac_close(ctx->handle); goto error; } - hmac->opaque = ctx; - return hmac; + return ctx; error: g_free(ctx); - g_free(hmac); return NULL; } @@ -150,3 +146,22 @@ int qcrypto_hmac_bytesv(QCryptoHmac *hmac, return 0; } + +QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg, + const uint8_t *key, size_t nkey, + Error **errp) +{ + QCryptoHmac *hmac; + QCryptoHmacGcrypt *ctx; + + ctx = qcrypto_hmac_ctx_new(alg, key, nkey, errp); + if (!ctx) { + return NULL; + } + + hmac = g_new0(QCryptoHmac, 1); + hmac->alg = alg; + hmac->opaque = ctx; + + return hmac; +}