From patchwork Mon Apr 10 08:59:40 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Longpeng(Mike)" X-Patchwork-Id: 9672121 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 1F3B960244 for ; Mon, 10 Apr 2017 09:03:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 14AC728066 for ; Mon, 10 Apr 2017 09:03:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 09907283E2; Mon, 10 Apr 2017 09:03:16 +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.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI 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 7BAB028066 for ; Mon, 10 Apr 2017 09:03:15 +0000 (UTC) Received: from localhost ([::1]:33076 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cxVEA-000108-IB for patchwork-qemu-devel@patchwork.kernel.org; Mon, 10 Apr 2017 05:03:14 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53894) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cxVB5-0007gO-G5 for qemu-devel@nongnu.org; Mon, 10 Apr 2017 05:00:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cxVB0-0001fT-RC for qemu-devel@nongnu.org; Mon, 10 Apr 2017 05:00:03 -0400 Received: from szxga03-in.huawei.com ([45.249.212.189]:3353 helo=dggrg03-dlp.huawei.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1cxVAz-0001eJ-Dv for qemu-devel@nongnu.org; Mon, 10 Apr 2017 04:59:58 -0400 Received: from 172.30.72.54 (EHLO DGGEML404-HUB.china.huawei.com) ([172.30.72.54]) by dggrg03-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id ALL31349; Mon, 10 Apr 2017 16:59:52 +0800 (CST) Received: from localhost (10.177.246.209) by DGGEML404-HUB.china.huawei.com (10.3.17.39) with Microsoft SMTP Server id 14.3.301.0; Mon, 10 Apr 2017 16:59:41 +0800 From: "Longpeng(Mike)" To: Date: Mon, 10 Apr 2017 16:59:40 +0800 Message-ID: <1491814780-63308-1-git-send-email-longpeng2@huawei.com> X-Mailer: git-send-email 1.8.4.msysgit.0 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.0A090204.58EB4988.009C, 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: eefbe114d3a6440c80ff36c9f6c9dac4 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 45.249.212.189 Subject: [Qemu-devel] [PATCH for-2.10 04/19] 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\)" , xuquan8@huawei.com, arei.gonglei@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 Extracts qcrypto_cipher_ctx_new() from qcrypto_cipher_new() for glib-backend impls. 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..8cf47d1 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 == NULL) { + return NULL; + } + + cipher = g_new0(QCryptoCipher, 1); + cipher->alg = alg; + cipher->mode = mode; + cipher->opaque = ctxt; + + return cipher; +}