From: "Daniel P. Berrange" <berrange@redhat.com>
To: qemu-devel@nongnu.org
Cc: Peter Maydell <peter.maydell@linaro.org>,
"Longpeng(Mike)" <longpeng2@huawei.com>,
"Daniel P . Berrange" <berrange@redhat.com>
Subject: [Qemu-devel] [PULL v1 04/18] crypto: cipher: introduce qcrypto_cipher_ctx_new for builtin-backend
Date: Tue, 18 Jul 2017 11:25:05 +0100 [thread overview]
Message-ID: <20170718102519.15392-5-berrange@redhat.com> (raw)
In-Reply-To: <20170718102519.15392-1-berrange@redhat.com>
From: "Longpeng(Mike)" <longpeng2@huawei.com>
Extracts qcrypto_cipher_ctx_new() from qcrypto_cipher_new() for
builtin-backend impls.
Reviewed-by: Gonglei <arei.gonglei@huawei.com>
Signed-off-by: Longpeng(Mike) <longpeng2@huawei.com>
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
---
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 b4bc2b9ca6..4ecd15eb9e 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;
+}
--
2.13.0
next prev parent reply other threads:[~2017-07-18 10:25 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-07-18 10:25 [Qemu-devel] [PULL v1 00/18] Merge crypto 201/07/18 Daniel P. Berrange
2017-07-18 10:25 ` [Qemu-devel] [PULL v1 01/18] crypto: cipher: introduce context free function Daniel P. Berrange
2017-07-18 10:25 ` [Qemu-devel] [PULL v1 02/18] crypto: cipher: introduce qcrypto_cipher_ctx_new for gcrypt-backend Daniel P. Berrange
2017-07-18 10:25 ` [Qemu-devel] [PULL v1 03/18] crypto: cipher: introduce qcrypto_cipher_ctx_new for nettle-backend Daniel P. Berrange
2017-07-18 10:25 ` Daniel P. Berrange [this message]
2017-07-18 10:25 ` [Qemu-devel] [PULL v1 05/18] crypto: cipher: add cipher driver framework Daniel P. Berrange
2017-07-18 10:25 ` [Qemu-devel] [PULL v1 06/18] crypto: hash: add hash " Daniel P. Berrange
2017-07-18 10:25 ` [Qemu-devel] [PULL v1 07/18] crypto: hmac: move crypto/hmac.h into include/crypto/ Daniel P. Berrange
2017-07-18 10:25 ` [Qemu-devel] [PULL v1 08/18] crypto: hmac: introduce qcrypto_hmac_ctx_new for gcrypt-backend Daniel P. Berrange
2017-07-18 10:25 ` [Qemu-devel] [PULL v1 09/18] crypto: hmac: introduce qcrypto_hmac_ctx_new for nettle-backend Daniel P. Berrange
2017-07-18 10:25 ` [Qemu-devel] [PULL v1 10/18] crypto: hmac: introduce qcrypto_hmac_ctx_new for glib-backend Daniel P. Berrange
2017-07-18 10:25 ` [Qemu-devel] [PULL v1 11/18] crypto: hmac: add hmac driver framework Daniel P. Berrange
2017-07-18 10:25 ` [Qemu-devel] [PULL v1 12/18] crypto: introduce some common functions for af_alg backend Daniel P. Berrange
2017-07-18 10:25 ` [Qemu-devel] [PULL v1 13/18] crypto: cipher: add afalg-backend cipher support Daniel P. Berrange
2017-07-18 10:25 ` [Qemu-devel] [PULL v1 14/18] crypto: hash: add afalg-backend hash support Daniel P. Berrange
2017-07-18 10:25 ` [Qemu-devel] [PULL v1 15/18] crypto: hmac: add af_alg-backend hmac support Daniel P. Berrange
2017-07-18 10:25 ` [Qemu-devel] [PULL v1 16/18] tests: crypto: add cipher speed benchmark support Daniel P. Berrange
2017-07-18 10:25 ` [Qemu-devel] [PULL v1 17/18] tests: crypto: add hash " Daniel P. Berrange
2017-07-18 10:25 ` [Qemu-devel] [PULL v1 18/18] tests: crypto: add hmac " Daniel P. Berrange
2017-07-19 8:11 ` [Qemu-devel] [PULL v1 00/18] Merge crypto 201/07/18 Peter Maydell
2017-07-19 8:28 ` Daniel P. Berrange
2017-07-19 8:42 ` Daniel P. Berrange
2017-07-19 9:16 ` Daniel P. Berrange
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170718102519.15392-5-berrange@redhat.com \
--to=berrange@redhat.com \
--cc=longpeng2@huawei.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.