From patchwork Mon Apr 10 08:59:46 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Longpeng(Mike)" X-Patchwork-Id: 9672119 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 962ED60244 for ; Mon, 10 Apr 2017 09:03:15 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8B1DD283E1 for ; Mon, 10 Apr 2017 09:03:15 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7FC83283F9; Mon, 10 Apr 2017 09:03:15 +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 AD7ED283E1 for ; Mon, 10 Apr 2017 09:03:14 +0000 (UTC) Received: from localhost ([::1]:33077 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cxVE9-00010N-QH for patchwork-qemu-devel@patchwork.kernel.org; Mon, 10 Apr 2017 05:03:13 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53918) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cxVB9-0007jX-Km for qemu-devel@nongnu.org; Mon, 10 Apr 2017 05:00:09 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cxVB4-0001gv-EJ for qemu-devel@nongnu.org; Mon, 10 Apr 2017 05:00:07 -0400 Received: from szxga03-in.huawei.com ([45.249.212.189]:3354 helo=dggrg03-dlp.huawei.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1cxVB3-0001fM-2v for qemu-devel@nongnu.org; Mon, 10 Apr 2017 05:00:02 -0400 Received: from 172.30.72.57 (EHLO DGGEML404-HUB.china.huawei.com) ([172.30.72.57]) by dggrg03-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id ALL31361; Mon, 10 Apr 2017 16:59:57 +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:48 +0800 From: "Longpeng(Mike)" To: Date: Mon, 10 Apr 2017 16:59:46 +0800 Message-ID: <1491814786-60036-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.58EB498D.00D9, 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: 77e4d2c80e4aa1a8a3c8a3966c3b6c12 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 05/19] crypto: cipher: add cipher driver framework 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 1) makes the public APIs in cipher-nettle/gcrypt/builtin static, and rename them with "nettle/gcrypt/builtin" prefix. 2) introduces cipher framework, including QCryptoCipherDriver and new public APIs. Signed-off-by: Longpeng(Mike) --- crypto/cipher-builtin.c | 59 +++++++++++++++++-------------------------------- crypto/cipher-gcrypt.c | 58 +++++++++++++++++------------------------------- crypto/cipher-nettle.c | 59 +++++++++++++++++-------------------------------- crypto/cipher.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ include/crypto/cipher.h | 22 ++++++++++++++++++ 5 files changed, 141 insertions(+), 116 deletions(-) diff --git a/crypto/cipher-builtin.c b/crypto/cipher-builtin.c index 8cf47d1..a35f461 100644 --- a/crypto/cipher-builtin.c +++ b/crypto/cipher-builtin.c @@ -466,25 +466,20 @@ static QCryptoCipherBuiltin *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg, return ctxt; } -void qcrypto_cipher_free(QCryptoCipher *cipher) +static void builtin_cipher_ctx_free(QCryptoCipher *cipher) { QCryptoCipherBuiltin *ctxt; - if (!cipher) { - return; - } - ctxt = cipher->opaque; ctxt->free(cipher); - g_free(cipher); } -int qcrypto_cipher_encrypt(QCryptoCipher *cipher, - const void *in, - void *out, - size_t len, - Error **errp) +static int builtin_cipher_encrypt(QCryptoCipher *cipher, + const void *in, + void *out, + size_t len, + Error **errp) { QCryptoCipherBuiltin *ctxt = cipher->opaque; @@ -498,11 +493,11 @@ int qcrypto_cipher_encrypt(QCryptoCipher *cipher, } -int qcrypto_cipher_decrypt(QCryptoCipher *cipher, - const void *in, - void *out, - size_t len, - Error **errp) +static int builtin_cipher_decrypt(QCryptoCipher *cipher, + const void *in, + void *out, + size_t len, + Error **errp) { QCryptoCipherBuiltin *ctxt = cipher->opaque; @@ -516,9 +511,9 @@ int qcrypto_cipher_decrypt(QCryptoCipher *cipher, } -int qcrypto_cipher_setiv(QCryptoCipher *cipher, - const uint8_t *iv, size_t niv, - Error **errp) +static int builtin_cipher_setiv(QCryptoCipher *cipher, + const uint8_t *iv, size_t niv, + Error **errp) { QCryptoCipherBuiltin *ctxt = cipher->opaque; @@ -526,23 +521,9 @@ int qcrypto_cipher_setiv(QCryptoCipher *cipher, } -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; -} +static struct QCryptoCipherDriver qcrypto_cipher_lib_driver = { + .cipher_encrypt = builtin_cipher_encrypt, + .cipher_decrypt = builtin_cipher_decrypt, + .cipher_setiv = builtin_cipher_setiv, + .cipher_free = builtin_cipher_ctx_free, +}; diff --git a/crypto/cipher-gcrypt.c b/crypto/cipher-gcrypt.c index 871730b..36a0626 100644 --- a/crypto/cipher-gcrypt.c +++ b/crypto/cipher-gcrypt.c @@ -244,13 +244,9 @@ static QCryptoCipherGcrypt *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg, } -void qcrypto_cipher_free(QCryptoCipher *cipher) +static void gcrypt_cipher_ctx_free(QCryptoCipher *cipher) { - if (!cipher) { - return; - } gcrypt_cipher_free_ctx(cipher->opaque, cipher->mode); - g_free(cipher); } @@ -274,11 +270,11 @@ static void qcrypto_gcrypt_xts_decrypt(const void *ctx, g_assert(err == 0); } -int qcrypto_cipher_encrypt(QCryptoCipher *cipher, - const void *in, - void *out, - size_t len, - Error **errp) +static int gcrypt_cipher_encrypt(QCryptoCipher *cipher, + const void *in, + void *out, + size_t len, + Error **errp) { QCryptoCipherGcrypt *ctx = cipher->opaque; gcry_error_t err; @@ -309,11 +305,11 @@ int qcrypto_cipher_encrypt(QCryptoCipher *cipher, } -int qcrypto_cipher_decrypt(QCryptoCipher *cipher, - const void *in, - void *out, - size_t len, - Error **errp) +static int gcrypt_cipher_decrypt(QCryptoCipher *cipher, + const void *in, + void *out, + size_t len, + Error **errp) { QCryptoCipherGcrypt *ctx = cipher->opaque; gcry_error_t err; @@ -343,9 +339,9 @@ int qcrypto_cipher_decrypt(QCryptoCipher *cipher, return 0; } -int qcrypto_cipher_setiv(QCryptoCipher *cipher, - const uint8_t *iv, size_t niv, - Error **errp) +static int gcrypt_cipher_setiv(QCryptoCipher *cipher, + const uint8_t *iv, size_t niv, + Error **errp) { QCryptoCipherGcrypt *ctx = cipher->opaque; gcry_error_t err; @@ -381,23 +377,9 @@ int qcrypto_cipher_setiv(QCryptoCipher *cipher, } -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 == NULL) { - return NULL; - } - - cipher = g_new0(QCryptoCipher, 1); - cipher->alg = alg; - cipher->mode = mode; - cipher->opaque = ctx; - - return cipher; -} +static struct QCryptoCipherDriver qcrypto_cipher_lib_driver = { + .cipher_encrypt = gcrypt_cipher_encrypt, + .cipher_decrypt = gcrypt_cipher_decrypt, + .cipher_setiv = gcrypt_cipher_setiv, + .cipher_free = gcrypt_cipher_ctx_free, +}; diff --git a/crypto/cipher-nettle.c b/crypto/cipher-nettle.c index e6d6e6c..6373f3f 100644 --- a/crypto/cipher-nettle.c +++ b/crypto/cipher-nettle.c @@ -439,25 +439,20 @@ static QCryptoCipherNettle *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg, } -void qcrypto_cipher_free(QCryptoCipher *cipher) +static void nettle_cipher_ctx_free(QCryptoCipher *cipher) { QCryptoCipherNettle *ctx; - if (!cipher) { - return; - } - ctx = cipher->opaque; nettle_cipher_free_ctx(ctx); - g_free(cipher); } -int qcrypto_cipher_encrypt(QCryptoCipher *cipher, - const void *in, - void *out, - size_t len, - Error **errp) +static int nettle_cipher_encrypt(QCryptoCipher *cipher, + const void *in, + void *out, + size_t len, + Error **errp) { QCryptoCipherNettle *ctx = cipher->opaque; @@ -499,11 +494,11 @@ int qcrypto_cipher_encrypt(QCryptoCipher *cipher, } -int qcrypto_cipher_decrypt(QCryptoCipher *cipher, - const void *in, - void *out, - size_t len, - Error **errp) +static int nettle_cipher_decrypt(QCryptoCipher *cipher, + const void *in, + void *out, + size_t len, + Error **errp) { QCryptoCipherNettle *ctx = cipher->opaque; @@ -543,9 +538,9 @@ int qcrypto_cipher_decrypt(QCryptoCipher *cipher, return 0; } -int qcrypto_cipher_setiv(QCryptoCipher *cipher, - const uint8_t *iv, size_t niv, - Error **errp) +static int nettle_cipher_setiv(QCryptoCipher *cipher, + const uint8_t *iv, size_t niv, + Error **errp) { QCryptoCipherNettle *ctx = cipher->opaque; if (niv != ctx->blocksize) { @@ -558,23 +553,9 @@ int qcrypto_cipher_setiv(QCryptoCipher *cipher, } -QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, - QCryptoCipherMode mode, - const uint8_t *key, size_t nkey, - Error **errp) -{ - QCryptoCipher *cipher; - QCryptoCipherNettle *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; -} +static struct QCryptoCipherDriver qcrypto_cipher_lib_driver = { + .cipher_encrypt = nettle_cipher_encrypt, + .cipher_decrypt = nettle_cipher_decrypt, + .cipher_setiv = nettle_cipher_setiv, + .cipher_free = nettle_cipher_ctx_free, +}; diff --git a/crypto/cipher.c b/crypto/cipher.c index 5a96489..fa31f2f 100644 --- a/crypto/cipher.c +++ b/crypto/cipher.c @@ -155,3 +155,62 @@ qcrypto_cipher_munge_des_rfb_key(const uint8_t *key, #else #include "crypto/cipher-builtin.c" #endif + +QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, + QCryptoCipherMode mode, + const uint8_t *key, size_t nkey, + Error **errp) +{ + QCryptoCipher *cipher; + void *ctx; + + ctx = qcrypto_cipher_ctx_new(alg, mode, key, nkey, errp); + if (ctx == NULL) { + return NULL; + } + + cipher = g_new0(QCryptoCipher, 1); + cipher->alg = alg; + cipher->mode = mode; + cipher->opaque = ctx; + cipher->driver = &qcrypto_cipher_lib_driver; + + return cipher; +} + + +int qcrypto_cipher_encrypt(QCryptoCipher *cipher, + const void *in, + void *out, + size_t len, + Error **errp) +{ + return cipher->driver->cipher_encrypt(cipher, in, out, len, errp); +} + + +int qcrypto_cipher_decrypt(QCryptoCipher *cipher, + const void *in, + void *out, + size_t len, + Error **errp) +{ + return cipher->driver->cipher_decrypt(cipher, in, out, len, errp); +} + + +int qcrypto_cipher_setiv(QCryptoCipher *cipher, + const uint8_t *iv, size_t niv, + Error **errp) +{ + return cipher->driver->cipher_setiv(cipher, iv, niv, errp); +} + + +void qcrypto_cipher_free(QCryptoCipher *cipher) +{ + if (cipher) { + cipher->driver->cipher_free(cipher); + g_free(cipher); + } +} diff --git a/include/crypto/cipher.h b/include/crypto/cipher.h index bec9f41..32b6065 100644 --- a/include/crypto/cipher.h +++ b/include/crypto/cipher.h @@ -23,6 +23,7 @@ #include "qapi-types.h" +typedef struct QCryptoCipherDriver QCryptoCipherDriver; typedef struct QCryptoCipher QCryptoCipher; /* See also "QCryptoCipherAlgorithm" and "QCryptoCipherMode" @@ -76,7 +77,28 @@ typedef struct QCryptoCipher QCryptoCipher; * */ +struct QCryptoCipherDriver { + int (*cipher_encrypt)(QCryptoCipher *cipher, + const void *in, + void *out, + size_t len, + Error **errp); + + int (*cipher_decrypt)(QCryptoCipher *cipher, + const void *in, + void *out, + size_t len, + Error **errp); + + int (*cipher_setiv)(QCryptoCipher *cipher, + const uint8_t *iv, size_t niv, + Error **errp); + + void (*cipher_free)(QCryptoCipher *cipher); +}; + struct QCryptoCipher { + QCryptoCipherDriver *driver; QCryptoCipherAlgorithm alg; QCryptoCipherMode mode; void *opaque;