From patchwork Fri Jul 14 11:38:26 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: long mike X-Patchwork-Id: 9840671 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 69BA2602D8 for ; Fri, 14 Jul 2017 11:44:28 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5BF6E28755 for ; Fri, 14 Jul 2017 11:44:28 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 50D652876E; Fri, 14 Jul 2017 11:44:28 +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 C851928755 for ; Fri, 14 Jul 2017 11:44:27 +0000 (UTC) Received: from localhost ([::1]:37191 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dVz1H-0005l5-0P for patchwork-qemu-devel@patchwork.kernel.org; Fri, 14 Jul 2017 07:44:27 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35512) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dVyvr-0000uP-Th 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 1dVyvq-0007jV-4j for qemu-devel@nongnu.org; Fri, 14 Jul 2017 07:38:51 -0400 Received: from mail-pf0-x241.google.com ([2607:f8b0:400e:c00::241]:36068) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dVyvp-0007j3-Sm for qemu-devel@nongnu.org; Fri, 14 Jul 2017 07:38:50 -0400 Received: by mail-pf0-x241.google.com with SMTP id z6so10753719pfk.3 for ; Fri, 14 Jul 2017 04:38:49 -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=S1OqGz7HmjPUuyiqmTp94rpmbZ3LXpn0vdrkZUmN40I=; b=Sby8bvUxvxaB2csxbDfge3mLyWOoum78yj135Jk6Q6sRsC7OwF6YfyrKdyGrOkIyN8 9tExqqjBZees2waUYxBchI17z3/aZ+OXsvkGcmDcEwfxkvHgI4lIkdKMbHaxDsUuGDb4 Kzg0NEVhjKd6g8A6ca+ONw/MDlyTWaU59VmG3mq4yVCHD+xF/k1D6Mcgsm3OdrXM3klO tqoX7Fu/3G3pbv9nsavCsLDmoO6OGhj1/lkOFAkGxpWWvl4z6Cd7pwSUq2HmAPI9Eh/K 9H946ohDGdvxPCQ+nPSnKQPlnuVzbvw9ncby3RbjHjQyV5QFxjBQlJbtK2Qi4S/zjPSZ Q6yQ== 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=S1OqGz7HmjPUuyiqmTp94rpmbZ3LXpn0vdrkZUmN40I=; b=eXJFlq5I7Uk+FtqHPL452NxNJuQZ7/8A3bOA3tRxKJtwVHtWF6r7jA8XfL7JGGH+WX Hyu67GsU+xhQ5cy5uboKYuOUk/YlXyogF7876W4cwO/NnBygM/ZJzZDcIbtHyrf7iqzt TcP1OLFOfvOj5qwKCGlzqncaK8gsaWy+uXKPpCekneWnxPuRyG1tzxZLuig4RTz+mSCV LtiszJhDrhxgX83WdyX8lLTxN3fyrf785DvhFz544Mq/UQpzGnU29shqAAW0b8iBCayT ns3/l+25FTIulYHWBQpbxUvWVpzJlui1qww2Ff9rulBiYHugf3pceGBrrbpQCcOEmB6H ygow== X-Gm-Message-State: AIVw111IWQCHhSHRAq6GleMfKJd/ohgem09syQYrRbTrnRTWPPGmNuoY 0iyMWWccBrgnSg== X-Received: by 10.84.132.74 with SMTP id 68mr15341147ple.179.1500032328917; Fri, 14 Jul 2017 04:38:48 -0700 (PDT) Received: from localhost (45.78.25.254.16clouds.com. [45.78.25.254]) by smtp.gmail.com with ESMTPSA id v70sm18357776pfi.110.2017.07.14.04.38.48 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Fri, 14 Jul 2017 04:38:48 -0700 (PDT) From: longpeng.mike@gmail.com To: berrange@redhat.com Date: Fri, 14 Jul 2017 07:38:26 -0400 Message-Id: <1500032321-13951-5-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::241 Subject: [Qemu-devel] [PATCH v5 04/18] crypto: cipher: introduce qcrypto_cipher_ctx_new for builtin-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 builtin-backend impls. Reviewed-by: Daniel P. Berrange Reviewed-by: Gonglei Signed-off-by: Longpeng(Mike) --- crypto/cipher-builtin.c | 101 ++++++++++++++++++++++++++---------------------- 1 file changed, 55 insertions(+), 46 deletions(-) diff --git a/crypto/cipher-builtin.c b/crypto/cipher-builtin.c index b4bc2b9..4ecd15e 100644 --- a/crypto/cipher-builtin.c +++ b/crypto/cipher-builtin.c @@ -235,23 +235,24 @@ static int qcrypto_cipher_setiv_aes(QCryptoCipher *cipher, -static int qcrypto_cipher_init_aes(QCryptoCipher *cipher, - const uint8_t *key, size_t nkey, - Error **errp) +static QCryptoCipherBuiltin * +qcrypto_cipher_init_aes(QCryptoCipherMode mode, + const uint8_t *key, size_t nkey, + Error **errp) { QCryptoCipherBuiltin *ctxt; - if (cipher->mode != QCRYPTO_CIPHER_MODE_CBC && - cipher->mode != QCRYPTO_CIPHER_MODE_ECB && - cipher->mode != QCRYPTO_CIPHER_MODE_XTS) { + if (mode != QCRYPTO_CIPHER_MODE_CBC && + mode != QCRYPTO_CIPHER_MODE_ECB && + mode != QCRYPTO_CIPHER_MODE_XTS) { error_setg(errp, "Unsupported cipher mode %s", - QCryptoCipherMode_lookup[cipher->mode]); - return -1; + QCryptoCipherMode_lookup[mode]); + return NULL; } ctxt = g_new0(QCryptoCipherBuiltin, 1); - if (cipher->mode == QCRYPTO_CIPHER_MODE_XTS) { + if (mode == QCRYPTO_CIPHER_MODE_XTS) { if (AES_set_encrypt_key(key, nkey * 4, &ctxt->state.aes.key.enc) != 0) { error_setg(errp, "Failed to set encryption key"); goto error; @@ -291,13 +292,11 @@ static int qcrypto_cipher_init_aes(QCryptoCipher *cipher, ctxt->encrypt = qcrypto_cipher_encrypt_aes; ctxt->decrypt = qcrypto_cipher_decrypt_aes; - cipher->opaque = ctxt; - - return 0; + return ctxt; error: g_free(ctxt); - return -1; + return NULL; } @@ -370,16 +369,17 @@ static int qcrypto_cipher_setiv_des_rfb(QCryptoCipher *cipher, } -static int qcrypto_cipher_init_des_rfb(QCryptoCipher *cipher, - const uint8_t *key, size_t nkey, - Error **errp) +static QCryptoCipherBuiltin * +qcrypto_cipher_init_des_rfb(QCryptoCipherMode mode, + const uint8_t *key, size_t nkey, + Error **errp) { QCryptoCipherBuiltin *ctxt; - if (cipher->mode != QCRYPTO_CIPHER_MODE_ECB) { + if (mode != QCRYPTO_CIPHER_MODE_ECB) { error_setg(errp, "Unsupported cipher mode %s", - QCryptoCipherMode_lookup[cipher->mode]); - return -1; + QCryptoCipherMode_lookup[mode]); + return NULL; } ctxt = g_new0(QCryptoCipherBuiltin, 1); @@ -394,9 +394,7 @@ static int qcrypto_cipher_init_des_rfb(QCryptoCipher *cipher, ctxt->encrypt = qcrypto_cipher_encrypt_des_rfb; ctxt->decrypt = qcrypto_cipher_decrypt_des_rfb; - cipher->opaque = ctxt; - - return 0; + return ctxt; } @@ -426,12 +424,13 @@ bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg, } -QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, - QCryptoCipherMode mode, - const uint8_t *key, size_t nkey, - Error **errp) +static QCryptoCipherBuiltin *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg, + QCryptoCipherMode mode, + const uint8_t *key, + size_t nkey, + Error **errp) { - QCryptoCipher *cipher; + QCryptoCipherBuiltin *ctxt; switch (mode) { case QCRYPTO_CIPHER_MODE_ECB: @@ -444,39 +443,27 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, return NULL; } - cipher = g_new0(QCryptoCipher, 1); - cipher->alg = alg; - cipher->mode = mode; - if (!qcrypto_cipher_validate_key_length(alg, mode, nkey, errp)) { - goto error; + return NULL; } - switch (cipher->alg) { + switch (alg) { case QCRYPTO_CIPHER_ALG_DES_RFB: - if (qcrypto_cipher_init_des_rfb(cipher, key, nkey, errp) < 0) { - goto error; - } + ctxt = qcrypto_cipher_init_des_rfb(mode, key, nkey, errp); break; case QCRYPTO_CIPHER_ALG_AES_128: case QCRYPTO_CIPHER_ALG_AES_192: case QCRYPTO_CIPHER_ALG_AES_256: - if (qcrypto_cipher_init_aes(cipher, key, nkey, errp) < 0) { - goto error; - } + ctxt = qcrypto_cipher_init_aes(mode, key, nkey, errp); break; default: error_setg(errp, "Unsupported cipher algorithm %s", - QCryptoCipherAlgorithm_lookup[cipher->alg]); - goto error; + QCryptoCipherAlgorithm_lookup[alg]); + return NULL; } - return cipher; - - error: - g_free(cipher); - return NULL; + return ctxt; } void qcrypto_cipher_free(QCryptoCipher *cipher) @@ -537,3 +524,25 @@ int qcrypto_cipher_setiv(QCryptoCipher *cipher, return ctxt->setiv(cipher, iv, niv, errp); } + + +QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, + QCryptoCipherMode mode, + const uint8_t *key, size_t nkey, + Error **errp) +{ + QCryptoCipher *cipher; + QCryptoCipherBuiltin *ctxt; + + ctxt = qcrypto_cipher_ctx_new(alg, mode, key, nkey, errp); + if (!ctxt) { + return NULL; + } + + cipher = g_new0(QCryptoCipher, 1); + cipher->alg = alg; + cipher->mode = mode; + cipher->opaque = ctxt; + + return cipher; +}