From patchwork Fri Jul 14 18:04:01 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: long mike X-Patchwork-Id: 9841509 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 59D23602D8 for ; Fri, 14 Jul 2017 18:10:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 410EE28752 for ; Fri, 14 Jul 2017 18:10:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 36109287A4; Fri, 14 Jul 2017 18:10:59 +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 B85A428752 for ; Fri, 14 Jul 2017 18:10:58 +0000 (UTC) Received: from localhost ([::1]:39295 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW53J-0001Jr-DD for patchwork-qemu-devel@patchwork.kernel.org; Fri, 14 Jul 2017 14:10:57 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55260) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW4x2-0005JG-Sc for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dW4wz-0003BS-OX for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:28 -0400 Received: from mail-pf0-x243.google.com ([2607:f8b0:400e:c00::243]:34424) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dW4wz-0003B9-JE for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:25 -0400 Received: by mail-pf0-x243.google.com with SMTP id c24so11760875pfe.1 for ; Fri, 14 Jul 2017 11:04:25 -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=s3L4JGja2yN3oDDn8U81boXH4d6mOeOXWrepxyG3NDtCNArcQx7J8ms/3CXYLznNJz slPAwY7rUTcYEph7/Gg8UsBNz0lf+4fZKrJmJouCzE4zSRBi+0Adks/GJk97WnWZ6eGj ZdcSM5QONkPkgkm4e1XuFhx+wFVKSxhYoQz1En+lVNzdyJqvIvDSYSk4TDxS6obGinXn q6VMJnPayPbbzSykj3ibMVr+UN68l65F+FcsNS0n0wZA6fSa42o3LppYnHbQlqocqbEv BTsg2gzuk7PVnXLIEzkAfEomwAE5ZvzD2ied2AtrTXdnlbIWl60zwD4X2hZaQLyUaZIZ oNBg== 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=eDWfylj+glHRsnWDjfNAYIe6bmXEdF1tRXYsDi5Y9NW3fr8xJYcyAWbmIhQq42nMf/ ahFv0NaigeSp1JgoapXKwTvm21awn3F7xYHFpv5RlQB81FbxPhQBWdFj32DtIGUFs6y9 UwN6kM1c5n8Wbv52ELX54fb7J57aB8/2/d8cKO5t6/+p5aA0va0dGA6vGOLuuCLl7Ysc iL8xvrxTr/AwZF9ng6DBV3aZ5wWjRo7oKjYZ1PuOOvTIa+K2bne7Yn0lblED36PJlZ3j +5XbCklYHc9VE/D37/9Us1MCoNl7gfjwhPAJ9EdtYYGRA5zBJ/kbG1URrFxUBqFgzWw8 A5Ug== X-Gm-Message-State: AIVw1118JB9RFSDcoxa85FVUjKRjvpxCI92OHpQUNU0a/hJKPMNL19L6 mgtHMSGZ8kAdWA== X-Received: by 10.98.107.201 with SMTP id g192mr6510081pfc.65.1500055464786; Fri, 14 Jul 2017 11:04:24 -0700 (PDT) Received: from localhost (45.78.25.254.16clouds.com. [45.78.25.254]) by smtp.gmail.com with ESMTPSA id d184sm19589542pfd.20.2017.07.14.11.04.24 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Fri, 14 Jul 2017 11:04:24 -0700 (PDT) From: longpeng.mike@gmail.com To: berrange@redhat.com Date: Fri, 14 Jul 2017 14:04:01 -0400 Message-Id: <1500055451-14041-9-git-send-email-longpeng.mike@gmail.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> References: <1500055451-14041-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 v6 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; +}