From patchwork Mon Apr 17 01:33:09 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Longpeng (Mike, Cloud Infrastructure Service Product Dept.)" X-Patchwork-Id: 751217 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3w5rRN0B5bz9s2s for ; Mon, 17 Apr 2017 11:34:55 +1000 (AEST) Received: from localhost ([::1]:34215 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1czvZ5-0003sk-9I for incoming@patchwork.ozlabs.org; Sun, 16 Apr 2017 21:34:51 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57628) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1czvYK-0003rE-MY for qemu-devel@nongnu.org; Sun, 16 Apr 2017 21:34:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1czvYH-0005Vm-SN for qemu-devel@nongnu.org; Sun, 16 Apr 2017 21:34:04 -0400 Received: from szxga01-in.huawei.com ([45.249.212.187]:3476 helo=dggrg01-dlp.huawei.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1czvYH-0005SN-9j for qemu-devel@nongnu.org; Sun, 16 Apr 2017 21:34:01 -0400 Received: from 172.30.72.54 (EHLO DGGEML402-HUB.china.huawei.com) ([172.30.72.54]) by dggrg01-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id AMW01799; Mon, 17 Apr 2017 09:33:52 +0800 (CST) Received: from localhost (10.177.246.209) by DGGEML402-HUB.china.huawei.com (10.3.17.38) with Microsoft SMTP Server id 14.3.301.0; Mon, 17 Apr 2017 09:33:42 +0800 From: "Longpeng(Mike)" To: Date: Mon, 17 Apr 2017 09:33:09 +0800 Message-ID: <1492392806-53720-2-git-send-email-longpeng2@huawei.com> X-Mailer: git-send-email 1.8.4.msysgit.0 In-Reply-To: <1492392806-53720-1-git-send-email-longpeng2@huawei.com> References: <1492392806-53720-1-git-send-email-longpeng2@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.246.209] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A090202.58F41B80.007A, 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: 2c1e2d7ac7a153743129eb66b52aa1df 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] [PATCH v2 for-2.10 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: weidong.huang@huawei.com, mst@redhat.com, qemu-devel@nongnu.org, armbru@redhat.com, arei.gonglei@huawei.com, "Longpeng\(Mike\)" Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" 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. Signed-off-by: Longpeng(Mike) Reviewed-by: Gonglei --- 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); }