From patchwork Fri Jul 14 18:03:54 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: long mike X-Patchwork-Id: 9841495 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 007F1602D8 for ; Fri, 14 Jul 2017 18:05:48 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DB58D287A3 for ; Fri, 14 Jul 2017 18:05:48 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CE722287AB; Fri, 14 Jul 2017 18:05:48 +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 4453A287A3 for ; Fri, 14 Jul 2017 18:05:47 +0000 (UTC) Received: from localhost ([::1]:39271 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW4yI-0005Cl-Po for patchwork-qemu-devel@patchwork.kernel.org; Fri, 14 Jul 2017 14:05:46 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55088) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW4wt-0005Bz-M1 for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dW4wr-00032J-4D for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:19 -0400 Received: from mail-pf0-x243.google.com ([2607:f8b0:400e:c00::243]:34408) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dW4wq-00031U-Vg for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:17 -0400 Received: by mail-pf0-x243.google.com with SMTP id c24so11760437pfe.1 for ; Fri, 14 Jul 2017 11:04:16 -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=kLjfTKXYACheyjfd72zcKFg4tvUGQaic7q3VlkEPP2E=; b=cutBx93X1+zSElQTYnK1emvjBA4nucz9gHUVIrXxqCna5onPjCk68muYIj/tsHhocl jWhy+RP7zrxPkP26G9kadSL43RbKHO0THL+G5W/6MSny+SIV1FCZ6wy8AAvG9+Fj4eQ9 ZoThF/Kb32pA8sYZP0OIu9vQ/yOxtlRKoxMm8czIEMpL8VpXIs7/5lXEkNPSUW+uk0qF QghvHvp3CFrcnKnVg2KQY5CXoPvzyAjomlXdxt2tbVAsg4PiBq5FXFkdFcasFJa+JTTv hIVH24TdPtaqeAQOuFo+/fB04DO0fP7HuNsoO38GCsiEU5Eb9rZOCsiXcgjMZwBJ66vG +1tw== 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=kLjfTKXYACheyjfd72zcKFg4tvUGQaic7q3VlkEPP2E=; b=Y1DNScK04PhbCjZ5V0GvIY+xWnZYjBsxwxSfqz2szXw3MAM+L7qdBlZHslG3H+uPtU KJpm7Q4bHbWaD8vVgk1/ikYHB08pZo9f7moaoLZK6NGKsCNu/gvnjB/4X19eqPjRDIG8 IuwzDV27s1xDEwbVlkiTi3xrVD2eS4P+77htCmhu1w/hYcnusLkSAMJ133ymiY3qpplb YyOlWrqpTVVbSXZMYyZuhraE8C4Cj1Xzgfl9EFv9gaBaQabtzfaGHHq44wc6YLxO6GQR EgNMFPhHKp+dvLrFml/9+U8rjGIHOn/oDqW0Mp43tM3lswkfUNbUbZLCx0h04F4Ov6zB j+gg== X-Gm-Message-State: AIVw113ua8p88M9jkkvXgPcv85klUT2Z8Z/bAm5Peq1B/ivBfd5DC0JZ ZokOEOOZZczhfw== X-Received: by 10.99.126.69 with SMTP id o5mr16649742pgn.2.1500055456125; Fri, 14 Jul 2017 11:04:16 -0700 (PDT) Received: from localhost (45.78.25.254.16clouds.com. [45.78.25.254]) by smtp.gmail.com with ESMTPSA id d185sm15034536pgc.39.2017.07.14.11.04.15 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Fri, 14 Jul 2017 11:04:15 -0700 (PDT) From: longpeng.mike@gmail.com To: berrange@redhat.com Date: Fri, 14 Jul 2017 14:03:54 -0400 Message-Id: <1500055451-14041-2-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 01/18] crypto: cipher: introduce context free 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: "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)" Refactors the qcrypto_cipher_free(), splits it into two parts. One is gcrypt/nettle__cipher_free_ctx() to free the special context. This makes code more clear, what's more, it would be used by the later patch. Reviewed-by: Daniel P. Berrange Reviewed-by: Gonglei Signed-off-by: Longpeng(Mike) --- crypto/cipher-gcrypt.c | 31 ++++++++++++++++++------------- crypto/cipher-nettle.c | 18 ++++++++++++++---- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/crypto/cipher-gcrypt.c b/crypto/cipher-gcrypt.c index 6487eca..0ecffa2 100644 --- a/crypto/cipher-gcrypt.c +++ b/crypto/cipher-gcrypt.c @@ -64,6 +64,22 @@ struct QCryptoCipherGcrypt { uint8_t *iv; }; +static void gcrypt_cipher_free_ctx(QCryptoCipherGcrypt *ctx, + QCryptoCipherMode mode) +{ + if (!ctx) { + return; + } + + gcry_cipher_close(ctx->handle); + if (mode == QCRYPTO_CIPHER_MODE_XTS) { + gcry_cipher_close(ctx->tweakhandle); + } + g_free(ctx->iv); + g_free(ctx); +} + + QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, QCryptoCipherMode mode, const uint8_t *key, size_t nkey, @@ -228,11 +244,7 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, return cipher; error: - gcry_cipher_close(ctx->handle); - if (cipher->mode == QCRYPTO_CIPHER_MODE_XTS) { - gcry_cipher_close(ctx->tweakhandle); - } - g_free(ctx); + gcrypt_cipher_free_ctx(ctx, mode); g_free(cipher); return NULL; } @@ -240,17 +252,10 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, void qcrypto_cipher_free(QCryptoCipher *cipher) { - QCryptoCipherGcrypt *ctx; if (!cipher) { return; } - ctx = cipher->opaque; - gcry_cipher_close(ctx->handle); - if (cipher->mode == QCRYPTO_CIPHER_MODE_XTS) { - gcry_cipher_close(ctx->tweakhandle); - } - g_free(ctx->iv); - g_free(ctx); + gcrypt_cipher_free_ctx(cipher->opaque, cipher->mode); g_free(cipher); } diff --git a/crypto/cipher-nettle.c b/crypto/cipher-nettle.c index dfc9030..e04e3a1 100644 --- a/crypto/cipher-nettle.c +++ b/crypto/cipher-nettle.c @@ -249,6 +249,19 @@ bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg, } +static void nettle_cipher_free_ctx(QCryptoCipherNettle *ctx) +{ + if (!ctx) { + return; + } + + g_free(ctx->iv); + g_free(ctx->ctx); + g_free(ctx->ctx_tweak); + g_free(ctx); +} + + QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, QCryptoCipherMode mode, const uint8_t *key, size_t nkey, @@ -440,10 +453,7 @@ void qcrypto_cipher_free(QCryptoCipher *cipher) } ctx = cipher->opaque; - g_free(ctx->iv); - g_free(ctx->ctx); - g_free(ctx->ctx_tweak); - g_free(ctx); + nettle_cipher_free_ctx(ctx); g_free(cipher); }