From patchwork Fri Jul 14 18:03:55 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: long mike X-Patchwork-Id: 9841499 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 97E50602D8 for ; Fri, 14 Jul 2017 18:05:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 87E08287A3 for ; Fri, 14 Jul 2017 18:05:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7CF43287A8; Fri, 14 Jul 2017 18:05:49 +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 6175C287A4 for ; Fri, 14 Jul 2017 18:05:48 +0000 (UTC) Received: from localhost ([::1]:39270 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW4yJ-0005CL-JD for patchwork-qemu-devel@patchwork.kernel.org; Fri, 14 Jul 2017 14:05:47 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55089) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW4wt-0005C0-MB for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dW4ws-00034O-HN for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:19 -0400 Received: from mail-pf0-x244.google.com ([2607:f8b0:400e:c00::244]:34410) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dW4ws-00032r-A6 for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:18 -0400 Received: by mail-pf0-x244.google.com with SMTP id c24so11760505pfe.1 for ; Fri, 14 Jul 2017 11:04:18 -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=zA6R4j+s3RaGE6vDHeuhYZ1YZgI7F9bAM2qBExxEYrk=; b=kCnedk6SZ1phWuSKgQlmBh+v0AzA5TaepehVlkVEFnDmZGpeeP+k5G50KT+LjqSiez v9/JKDBhmtIwB7akO/Fz540YGCB3TKz5vIF6IOQ90JmlvbnBPdPFyofOPfWxFpTz6wD3 /xfAIJTat1SlfOKF5W54vgnEVB9v34GeWb30eqIICpqkMoOPojnKcIrJkFS7n+GkBUii DWD/3g6YjJwAQGnmMJZWaItzkBmXZj2xC3FxcH6BOmKbrxx6BFIxMlSDXCrF1hmivSJL RAf1q2jHDmQP03+wVazoQ03ctWUX7ujwVeCezthc0qV27t4Zt10ll98mNWemIhvI5IrO TOTw== 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=zA6R4j+s3RaGE6vDHeuhYZ1YZgI7F9bAM2qBExxEYrk=; b=hjdqVGSGpfTVl7kngxCLbC4KV/CCIMiyTDj1ndrgSdyzddcxATDCBlSWMtFJLCI+uz jllNB7vF4Kvk3f8ZC09bHa8nErB+tp01CfHcZ4pCRN5IMFgzWMV2ui9Oo4byzUk4iG4u eTiW9Yts1vuYlW5doje/bycF1q70/lnoeE6z/LDKBTLL+ej9VzwEFkAKXN+FG+zPk0Ee rkovljyT5gN7UvHyJRL8ZaA48OZkpbLBKLb74FoDm0BNmRP0Onkw4CE9pSzNPsmUYvre k0LxsQAwD+NTCE3DTOTWY0KpKRvmoHmpwZQOkINeg/bfCizA+rchXo3FTP9p0+D/Ux4D id8g== X-Gm-Message-State: AIVw1111gaXPyW2cCiOoBt/8Jif4cMp8fQg4dQd6HKhvp/xduiZp4OZY QlJ/r16TrU9vSfHBFAc= X-Received: by 10.101.90.69 with SMTP id z5mr15894289pgs.277.1500055457432; Fri, 14 Jul 2017 11:04:17 -0700 (PDT) Received: from localhost (45.78.25.254.16clouds.com. [45.78.25.254]) by smtp.gmail.com with ESMTPSA id q3sm20903903pfk.8.2017.07.14.11.04.16 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Fri, 14 Jul 2017 11:04:16 -0700 (PDT) From: longpeng.mike@gmail.com To: berrange@redhat.com Date: Fri, 14 Jul 2017 14:03:55 -0400 Message-Id: <1500055451-14041-3-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::244 Subject: [Qemu-devel] [PATCH v6 02/18] crypto: cipher: introduce qcrypto_cipher_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)" Extracts qcrypto_cipher_ctx_new() from qcrypto_cipher_new() for gcrypt-backend impls. Reviewed-by: Daniel P. Berrange Reviewed-by: Gonglei Signed-off-by: Longpeng(Mike) --- crypto/cipher-gcrypt.c | 50 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/crypto/cipher-gcrypt.c b/crypto/cipher-gcrypt.c index 0ecffa2..4939386 100644 --- a/crypto/cipher-gcrypt.c +++ b/crypto/cipher-gcrypt.c @@ -80,12 +80,12 @@ static void gcrypt_cipher_free_ctx(QCryptoCipherGcrypt *ctx, } -QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, - QCryptoCipherMode mode, - const uint8_t *key, size_t nkey, - Error **errp) +static QCryptoCipherGcrypt *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg, + QCryptoCipherMode mode, + const uint8_t *key, + size_t nkey, + Error **errp) { - QCryptoCipher *cipher; QCryptoCipherGcrypt *ctx; gcry_error_t err; int gcryalg, gcrymode; @@ -162,10 +162,6 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, return NULL; } - cipher = g_new0(QCryptoCipher, 1); - cipher->alg = alg; - cipher->mode = mode; - ctx = g_new0(QCryptoCipherGcrypt, 1); err = gcry_cipher_open(&ctx->handle, gcryalg, gcrymode, 0); @@ -174,7 +170,7 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, gcry_strerror(err)); goto error; } - if (cipher->mode == QCRYPTO_CIPHER_MODE_XTS) { + if (mode == QCRYPTO_CIPHER_MODE_XTS) { err = gcry_cipher_open(&ctx->tweakhandle, gcryalg, gcrymode, 0); if (err != 0) { error_setg(errp, "Cannot initialize cipher: %s", @@ -183,7 +179,7 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, } } - if (cipher->alg == QCRYPTO_CIPHER_ALG_DES_RFB) { + if (alg == QCRYPTO_CIPHER_ALG_DES_RFB) { /* We're using standard DES cipher from gcrypt, so we need * to munge the key so that the results are the same as the * bizarre RFB variant of DES :-) @@ -193,7 +189,7 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, g_free(rfbkey); ctx->blocksize = 8; } else { - if (cipher->mode == QCRYPTO_CIPHER_MODE_XTS) { + if (mode == QCRYPTO_CIPHER_MODE_XTS) { nkey /= 2; err = gcry_cipher_setkey(ctx->handle, key, nkey); if (err != 0) { @@ -210,7 +206,7 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, gcry_strerror(err)); goto error; } - switch (cipher->alg) { + switch (alg) { case QCRYPTO_CIPHER_ALG_AES_128: case QCRYPTO_CIPHER_ALG_AES_192: case QCRYPTO_CIPHER_ALG_AES_256: @@ -230,7 +226,7 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, } } - if (cipher->mode == QCRYPTO_CIPHER_MODE_XTS) { + if (mode == QCRYPTO_CIPHER_MODE_XTS) { if (ctx->blocksize != XTS_BLOCK_SIZE) { error_setg(errp, "Cipher block size %zu must equal XTS block size %d", @@ -240,12 +236,10 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, ctx->iv = g_new0(uint8_t, ctx->blocksize); } - cipher->opaque = ctx; - return cipher; + return ctx; error: gcrypt_cipher_free_ctx(ctx, mode); - g_free(cipher); return NULL; } @@ -385,3 +379,25 @@ int qcrypto_cipher_setiv(QCryptoCipher *cipher, return 0; } + + +QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, + QCryptoCipherMode mode, + const uint8_t *key, size_t nkey, + Error **errp) +{ + QCryptoCipher *cipher; + QCryptoCipherGcrypt *ctx; + + ctx = qcrypto_cipher_ctx_new(alg, mode, key, nkey, errp); + if (!ctx) { + return NULL; + } + + cipher = g_new0(QCryptoCipher, 1); + cipher->alg = alg; + cipher->mode = mode; + cipher->opaque = ctx; + + return cipher; +}