From patchwork Fri Jul 14 18:03:58 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: long mike X-Patchwork-Id: 788697 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3x8LMS2Fpsz9t2H for ; Sat, 15 Jul 2017 04:10:27 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="iSQ15JKc"; dkim-atps=neutral Received: from localhost ([::1]:39293 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW52m-0001IJ-JT for incoming@patchwork.ozlabs.org; Fri, 14 Jul 2017 14:10:24 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55182) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW4wy-0005Eb-30 for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:26 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dW4ww-00038H-8k for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:24 -0400 Received: from mail-pf0-x242.google.com ([2607:f8b0:400e:c00::242]:35505) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dW4wv-00037n-WA for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:22 -0400 Received: by mail-pf0-x242.google.com with SMTP id q85so11727861pfq.2 for ; Fri, 14 Jul 2017 11:04:21 -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=uxq0ODblstWlOT1yn/6nMk03/1icIp/MOwJpg+iSaT4=; b=iSQ15JKc4UuKhkxjBidSMNEArbwKpPDuMXm9XbdbhsuvV131nHwd/hPa/+e3u6RZ75 d2HMiMlsbjoafB88LQyDbAs6WIncQTwxvNOM61LNlPhMJKnGsGHDo7QAlpm4DEPuSict aiRvPm7uVkXs/zpxeijKbiaieH9hv4MmFhQxxvNv9FltPB71zKieHBaTShywy3d1Wt7k CQ7O8bvrQPL8jFiW/gAx9pkGJH4PNpbb6IXsSmWBLNcNXaSm9fe08yBy7bd6on5ZmS0W SV+1RjtOrt429u2Ms75ul7HRXUo/ZZPyQ6s/1N0WAgJqxeWNSiwll40xkMY0cuF3CbYT nKrw== 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=uxq0ODblstWlOT1yn/6nMk03/1icIp/MOwJpg+iSaT4=; b=N/QDwXZcTFTmBipFOR4951MKE9/eTaVCZlIb6x6q4e/poyiy2ABAlfxkXNpgGKVpmw ogMuM/RjnM28ucA1jx4d+g2IigkFUnIsKMfO96SqSV5CB3c5Tln41xwywamjbRXTxYDL gS3aEeECfCeE3bN2X9ew1YeTB4MYvhFGtR6sTY3Ib/ACyLEJoAAVA7W+BCOT1uqTQK/n wwaO0I6FBL4HBNuaPLpHEjpZBRvR6V7cZ/HcWAlZDDPO8LLCh2ndhne/EGL28qX30sdO TkFeLUlVyruVlEsEUNl/HOYqYXE1RiJJhEglyHPbodARi5mf9qzRDdToTqpGiH416XVe cnPg== X-Gm-Message-State: AIVw110uwLgfA9byfmDxGNMK1eqYKlLFB3txBhHO83HRgnGfvC5UThLZ XrCoZf8gvKL9+Q== X-Received: by 10.84.217.139 with SMTP id p11mr17928875pli.214.1500055460982; Fri, 14 Jul 2017 11:04:20 -0700 (PDT) Received: from localhost (45.78.25.254.16clouds.com. [45.78.25.254]) by smtp.gmail.com with ESMTPSA id w66sm20303385pfi.63.2017.07.14.11.04.20 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Fri, 14 Jul 2017 11:04:20 -0700 (PDT) From: longpeng.mike@gmail.com To: berrange@redhat.com Date: Fri, 14 Jul 2017 14:03:58 -0400 Message-Id: <1500055451-14041-6-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::242 Subject: [Qemu-devel] [PATCH v6 05/18] 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\)" , arei.gonglei@huawei.com, weidong.huang@huawei.com, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" From: "Longpeng(Mike)" 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. Reviewed-by: Daniel P. Berrange Reviewed-by: Gonglei Signed-off-by: Longpeng(Mike) --- crypto/cipher-builtin.c | 64 +++++++++++++++++-------------------------- crypto/cipher-gcrypt.c | 72 +++++++++++++++++++++---------------------------- crypto/cipher-nettle.c | 71 ++++++++++++++++++++---------------------------- crypto/cipher.c | 65 ++++++++++++++++++++++++++++++++++++++++++++ crypto/cipherpriv.h | 40 +++++++++++++++++++++++++++ include/crypto/cipher.h | 1 + 6 files changed, 190 insertions(+), 123 deletions(-) create mode 100644 crypto/cipherpriv.h diff --git a/crypto/cipher-builtin.c b/crypto/cipher-builtin.c index 4ecd15e..16a36d4 100644 --- a/crypto/cipher-builtin.c +++ b/crypto/cipher-builtin.c @@ -22,6 +22,7 @@ #include "crypto/aes.h" #include "crypto/desrfb.h" #include "crypto/xts.h" +#include "cipherpriv.h" typedef struct QCryptoCipherBuiltinAESContext QCryptoCipherBuiltinAESContext; struct QCryptoCipherBuiltinAESContext { @@ -466,25 +467,22 @@ static QCryptoCipherBuiltin *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg, return ctxt; } -void qcrypto_cipher_free(QCryptoCipher *cipher) +static void +qcrypto_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 +qcrypto_builtin_cipher_encrypt(QCryptoCipher *cipher, + const void *in, + void *out, + size_t len, + Error **errp) { QCryptoCipherBuiltin *ctxt = cipher->opaque; @@ -498,11 +496,12 @@ int qcrypto_cipher_encrypt(QCryptoCipher *cipher, } -int qcrypto_cipher_decrypt(QCryptoCipher *cipher, - const void *in, - void *out, - size_t len, - Error **errp) +static int +qcrypto_builtin_cipher_decrypt(QCryptoCipher *cipher, + const void *in, + void *out, + size_t len, + Error **errp) { QCryptoCipherBuiltin *ctxt = cipher->opaque; @@ -516,9 +515,10 @@ int qcrypto_cipher_decrypt(QCryptoCipher *cipher, } -int qcrypto_cipher_setiv(QCryptoCipher *cipher, - const uint8_t *iv, size_t niv, - Error **errp) +static int +qcrypto_builtin_cipher_setiv(QCryptoCipher *cipher, + const uint8_t *iv, size_t niv, + Error **errp) { QCryptoCipherBuiltin *ctxt = cipher->opaque; @@ -526,23 +526,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) { - 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 = qcrypto_builtin_cipher_encrypt, + .cipher_decrypt = qcrypto_builtin_cipher_decrypt, + .cipher_setiv = qcrypto_builtin_cipher_setiv, + .cipher_free = qcrypto_builtin_cipher_ctx_free, +}; diff --git a/crypto/cipher-gcrypt.c b/crypto/cipher-gcrypt.c index 4939386..0489147 100644 --- a/crypto/cipher-gcrypt.c +++ b/crypto/cipher-gcrypt.c @@ -20,6 +20,7 @@ #include "qemu/osdep.h" #include "crypto/xts.h" +#include "cipherpriv.h" #include @@ -64,8 +65,9 @@ struct QCryptoCipherGcrypt { uint8_t *iv; }; -static void gcrypt_cipher_free_ctx(QCryptoCipherGcrypt *ctx, - QCryptoCipherMode mode) +static void +qcrypto_gcrypt_cipher_free_ctx(QCryptoCipherGcrypt *ctx, + QCryptoCipherMode mode) { if (!ctx) { return; @@ -239,18 +241,15 @@ static QCryptoCipherGcrypt *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg, return ctx; error: - gcrypt_cipher_free_ctx(ctx, mode); + qcrypto_gcrypt_cipher_free_ctx(ctx, mode); return NULL; } -void qcrypto_cipher_free(QCryptoCipher *cipher) +static void +qcrypto_gcrypt_cipher_ctx_free(QCryptoCipher *cipher) { - if (!cipher) { - return; - } - gcrypt_cipher_free_ctx(cipher->opaque, cipher->mode); - g_free(cipher); + qcrypto_gcrypt_cipher_free_ctx(cipher->opaque, cipher->mode); } @@ -274,11 +273,12 @@ 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 +qcrypto_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 +309,12 @@ int qcrypto_cipher_encrypt(QCryptoCipher *cipher, } -int qcrypto_cipher_decrypt(QCryptoCipher *cipher, - const void *in, - void *out, - size_t len, - Error **errp) +static int +qcrypto_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 +344,10 @@ 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 +qcrypto_gcrypt_cipher_setiv(QCryptoCipher *cipher, + const uint8_t *iv, size_t niv, + Error **errp) { QCryptoCipherGcrypt *ctx = cipher->opaque; gcry_error_t err; @@ -381,23 +383,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) { - 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 = qcrypto_gcrypt_cipher_encrypt, + .cipher_decrypt = qcrypto_gcrypt_cipher_decrypt, + .cipher_setiv = qcrypto_gcrypt_cipher_setiv, + .cipher_free = qcrypto_gcrypt_cipher_ctx_free, +}; diff --git a/crypto/cipher-nettle.c b/crypto/cipher-nettle.c index e6d6e6c..c51f119 100644 --- a/crypto/cipher-nettle.c +++ b/crypto/cipher-nettle.c @@ -20,6 +20,7 @@ #include "qemu/osdep.h" #include "crypto/xts.h" +#include "cipherpriv.h" #include #include @@ -249,7 +250,8 @@ bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg, } -static void nettle_cipher_free_ctx(QCryptoCipherNettle *ctx) +static void +qcrypto_nettle_cipher_free_ctx(QCryptoCipherNettle *ctx) { if (!ctx) { return; @@ -434,30 +436,27 @@ static QCryptoCipherNettle *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg, return ctx; error: - nettle_cipher_free_ctx(ctx); + qcrypto_nettle_cipher_free_ctx(ctx); return NULL; } -void qcrypto_cipher_free(QCryptoCipher *cipher) +static void +qcrypto_nettle_cipher_ctx_free(QCryptoCipher *cipher) { QCryptoCipherNettle *ctx; - if (!cipher) { - return; - } - ctx = cipher->opaque; - nettle_cipher_free_ctx(ctx); - g_free(cipher); + qcrypto_nettle_cipher_free_ctx(ctx); } -int qcrypto_cipher_encrypt(QCryptoCipher *cipher, - const void *in, - void *out, - size_t len, - Error **errp) +static int +qcrypto_nettle_cipher_encrypt(QCryptoCipher *cipher, + const void *in, + void *out, + size_t len, + Error **errp) { QCryptoCipherNettle *ctx = cipher->opaque; @@ -499,11 +498,12 @@ int qcrypto_cipher_encrypt(QCryptoCipher *cipher, } -int qcrypto_cipher_decrypt(QCryptoCipher *cipher, - const void *in, - void *out, - size_t len, - Error **errp) +static int +qcrypto_nettle_cipher_decrypt(QCryptoCipher *cipher, + const void *in, + void *out, + size_t len, + Error **errp) { QCryptoCipherNettle *ctx = cipher->opaque; @@ -543,9 +543,10 @@ 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 +qcrypto_nettle_cipher_setiv(QCryptoCipher *cipher, + const uint8_t *iv, size_t niv, + Error **errp) { QCryptoCipherNettle *ctx = cipher->opaque; if (niv != ctx->blocksize) { @@ -558,23 +559,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 = qcrypto_nettle_cipher_encrypt, + .cipher_decrypt = qcrypto_nettle_cipher_decrypt, + .cipher_setiv = qcrypto_nettle_cipher_setiv, + .cipher_free = qcrypto_nettle_cipher_ctx_free, +}; diff --git a/crypto/cipher.c b/crypto/cipher.c index 5a96489..0a3d2e5 100644 --- a/crypto/cipher.c +++ b/crypto/cipher.c @@ -21,6 +21,7 @@ #include "qemu/osdep.h" #include "qapi/error.h" #include "crypto/cipher.h" +#include "cipherpriv.h" static size_t alg_key_len[QCRYPTO_CIPHER_ALG__MAX] = { @@ -155,3 +156,67 @@ 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) { + return NULL; + } + + cipher = g_new0(QCryptoCipher, 1); + cipher->alg = alg; + cipher->mode = mode; + cipher->opaque = ctx; + cipher->driver = (void *)&qcrypto_cipher_lib_driver; + + return cipher; +} + + +int qcrypto_cipher_encrypt(QCryptoCipher *cipher, + const void *in, + void *out, + size_t len, + Error **errp) +{ + QCryptoCipherDriver *drv = cipher->driver; + return drv->cipher_encrypt(cipher, in, out, len, errp); +} + + +int qcrypto_cipher_decrypt(QCryptoCipher *cipher, + const void *in, + void *out, + size_t len, + Error **errp) +{ + QCryptoCipherDriver *drv = cipher->driver; + return drv->cipher_decrypt(cipher, in, out, len, errp); +} + + +int qcrypto_cipher_setiv(QCryptoCipher *cipher, + const uint8_t *iv, size_t niv, + Error **errp) +{ + QCryptoCipherDriver *drv = cipher->driver; + return drv->cipher_setiv(cipher, iv, niv, errp); +} + + +void qcrypto_cipher_free(QCryptoCipher *cipher) +{ + QCryptoCipherDriver *drv; + if (cipher) { + drv = cipher->driver; + drv->cipher_free(cipher); + g_free(cipher); + } +} diff --git a/crypto/cipherpriv.h b/crypto/cipherpriv.h new file mode 100644 index 0000000..4af5e85 --- /dev/null +++ b/crypto/cipherpriv.h @@ -0,0 +1,40 @@ +/* + * QEMU Crypto cipher driver supports + * + * Copyright (c) 2017 HUAWEI TECHNOLOGIES CO., LTD. + * + * Authors: + * Longpeng(Mike) + * + * This work is licensed under the terms of the GNU GPL, version 2 or + * (at your option) any later version. See the COPYING file in the + * top-level directory. + * + */ + +#ifndef QCRYPTO_CIPHERPRIV_H +#define QCRYPTO_CIPHERPRIV_H + +typedef struct QCryptoCipherDriver QCryptoCipherDriver; + +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); +}; + +#endif diff --git a/include/crypto/cipher.h b/include/crypto/cipher.h index bec9f41..984fb82 100644 --- a/include/crypto/cipher.h +++ b/include/crypto/cipher.h @@ -80,6 +80,7 @@ struct QCryptoCipher { QCryptoCipherAlgorithm alg; QCryptoCipherMode mode; void *opaque; + void *driver; }; /**