From patchwork Fri Jul 14 11:38:23 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: long mike X-Patchwork-Id: 788368 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 3x89hQ29DGz9s76 for ; Fri, 14 Jul 2017 21:39:34 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="CkRSyi1m"; dkim-atps=neutral Received: from localhost ([::1]:37166 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dVywV-0000x5-SE for incoming@patchwork.ozlabs.org; Fri, 14 Jul 2017 07:39:32 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35510) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dVyvr-0000uN-So for qemu-devel@nongnu.org; Fri, 14 Jul 2017 07:38:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dVyvn-0007i6-Th for qemu-devel@nongnu.org; Fri, 14 Jul 2017 07:38:51 -0400 Received: from mail-pf0-x243.google.com ([2607:f8b0:400e:c00::243]:34946) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dVyvn-0007gb-OR for qemu-devel@nongnu.org; Fri, 14 Jul 2017 07:38:47 -0400 Received: by mail-pf0-x243.google.com with SMTP id q85so10728147pfq.2 for ; Fri, 14 Jul 2017 04:38:46 -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=CkRSyi1mtONWJxbsjZJ45RwaEYVaaxnpKAFDYKh0F/UVDJthAardbmYQxJU8CPMkuc SFpz4q2R3DScxuv0TOwNh4FMmkxbjVeO1978TpXRHdpsQMRZJhAiS7j9pn4iVZgMfGeC KeDcql4L0R5p5Ipgz8514Rqu68wLkRfTJpJ5W+yI1Fe5RibFw4bahCDiPmebc1E2PAQW EWzioy2DpgJ1kFnn4+SCdKSNCGV5T2/9WkRCeS1E3Ux0AGPV+UVvAxBNzlgpXiM8nZou kAmw78kQ0zt1cOjiuFQJYA8la4sNbYZG5zFCSbe6xTCwUGZMqjIB283kcc6BpyppMjzm 0nRg== 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=jQ0Mv0/ds8kgwUi5DP0Y4Wo/El3gIaYBFtmdGRIL1nBdz11tL896iVQ94rsSgSBS8X faU9+bIdrRNFJwpIrUDG/LstjR7HCgtA9th9p5aZ9ymhkAEZJTUADIfFbdoFeIr+buNS YN28KOp0jwyKZKqCtfPywFgeILiiIxzl63yPzj5aTtv+TfDxyEOxx6d2QvmU07qtTQNX qJUZq+vnDVgglZ1yvjj8CCGcqQCLa/oU/NhCvFbGCqSC6oqUiglbqBMdqBjQpfKSIzfX BGJHwmRLv2Ox1z89MChyh3KY0e9v30ligLfdtr+2nPBRAANPjygZ9oho6wqtGr8nr2YL XWQg== X-Gm-Message-State: AIVw112vh/gnhTS2Aos/3SG3C5CAOq9Tv+jJLdj+nKHkNeTRaWdfv4jx JN7o/+7LkBwC/2qHHX0= X-Received: by 10.99.115.18 with SMTP id o18mr14203123pgc.103.1500032325310; Fri, 14 Jul 2017 04:38:45 -0700 (PDT) Received: from localhost (45.78.25.254.16clouds.com. [45.78.25.254]) by smtp.gmail.com with ESMTPSA id d18sm23905167pgn.27.2017.07.14.04.38.44 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Fri, 14 Jul 2017 04:38:44 -0700 (PDT) From: longpeng.mike@gmail.com To: berrange@redhat.com Date: Fri, 14 Jul 2017 07:38:23 -0400 Message-Id: <1500032321-13951-2-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 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" 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); }