From patchwork Fri Jul 14 18:03:57 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: long mike X-Patchwork-Id: 9841505 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 B26A5602D8 for ; Fri, 14 Jul 2017 18:08:27 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A2B9F2871A for ; Fri, 14 Jul 2017 18:08:27 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 97A5428789; Fri, 14 Jul 2017 18:08:27 +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 1D7482871A for ; Fri, 14 Jul 2017 18:08:27 +0000 (UTC) Received: from localhost ([::1]:39287 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW50s-0007SA-8B for patchwork-qemu-devel@patchwork.kernel.org; Fri, 14 Jul 2017 14:08:26 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55153) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW4ww-0005CQ-61 for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:23 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dW4wu-00036g-Qq for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:22 -0400 Received: from mail-pg0-x242.google.com ([2607:f8b0:400e:c05::242]:33679) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dW4wu-00035w-K2 for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:20 -0400 Received: by mail-pg0-x242.google.com with SMTP id u62so11375447pgb.0 for ; Fri, 14 Jul 2017 11:04:20 -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=QIwHBIRVpH/bYQ2OrSuG10knEVAtsTcH8QVVbXVoqtk31BDuAm9DkSboV7oVP8UAdj AbDIGY2UjYYZMCD8UXXp3AcYnwZSTba6cSkSDmfm4OEqfDswdq0b/axLGgRZtxglyJma MNWOK/ti9jDRkS1wP2x8Cm+NQGruRjIXSnHzM0w+3eEuI20LJJmnq0XXyJNzdtNRmLO0 2k9hhUwbYHU4DF58xjbzq7JlHvXBYaHmKpTvW7EYCncYMzFJggG1BpW9HVoo51eFAG3o 9DPmXBEaoAbhGFaZVevhQKT29mLhE4w/m3QnYzpowHTRmwvBU8tFsHvK9cs+leDJilxu xHFg== 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=jF1ghTEbR1jEPSNFrbF02GKkDbsAkCrZQwj46zpFcM+ZBQQPJP33nRRCUR1k0MNOAn HVsqAy2qZUu1S3Ovvzjo2PfMk5Z80nPIA/F6HQo+pgQDxq9KJol7KV34SLL1lhQoPWcg foOY1TmU70nu5sbBSqsevAAE22KuFQdghEA/tBoqcDDXWOd7liE0qfHHdwKCvrDsGKXT C/ELbQz9B3xeM+52IaZ2/S7Vk8KDOyRMycGSW2qhscQVDXkYQ37oQFagvJXQOSJo/UuP sUnBBbfcbizxPXoi+rcpfhwjTr3pGYb1CcO3Acu7lhrca/n7VaOv6eMVh0LPkqufnF3i uP7Q== X-Gm-Message-State: AIVw110/J8P2WJGbsOuik4Ml9ZEhzxvowmYPl/z5ZfDkdhoPRw9WoecG YGM6N3wRTvEILQ== X-Received: by 10.98.57.4 with SMTP id g4mr6490202pfa.155.1500055459760; Fri, 14 Jul 2017 11:04:19 -0700 (PDT) Received: from localhost (45.78.25.254.16clouds.com. [45.78.25.254]) by smtp.gmail.com with ESMTPSA id a2sm18430870pfe.101.2017.07.14.11.04.19 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Fri, 14 Jul 2017 11:04:19 -0700 (PDT) From: longpeng.mike@gmail.com To: berrange@redhat.com Date: Fri, 14 Jul 2017 14:03:57 -0400 Message-Id: <1500055451-14041-5-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:c05::242 Subject: [Qemu-devel] [PATCH v6 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; +}