2019-01-17 11:34:49

by Xiongfeng Wang

[permalink] [raw]
Subject: [PATCH V2 0/5] Crypto Cleanup

The patchset introduce a helper to (un)register a array of crypto templates.
The following patches use this helper to simplify the code. This is also
a preparation for a coming patchset, which will register several crypto
templates.

Changelog:
v1 -> v2:
rebased to cryptodev, fix some format issue

Xiongfeng Wang (5):
crypto: api - add a helper to (un)register a array of templates
crypto: ccm - use template array registering API to simplify the code
crypto: gcm - use template array registering API to simplify the code
crypto: ctr - use template array registering API to simplify the code
crypto: chacha20poly1305 - use template array registering API to
simplify the code

crypto/algapi.c | 27 ++++++++++++++++
crypto/ccm.c | 78 ++++++++++++++---------------------------------
crypto/chacha20poly1305.c | 37 +++++++++-------------
crypto/ctr.c | 43 +++++++++-----------------
crypto/gcm.c | 73 ++++++++++++++------------------------------
include/crypto/algapi.h | 2 ++
6 files changed, 104 insertions(+), 156 deletions(-)

--
1.7.12.4



2019-01-17 09:18:41

by Xiongfeng Wang

[permalink] [raw]
Subject: [PATCH V2 3/5] crypto: gcm - use template array registering API to simplify the code

Use crypto template array registering API to simplify the code.

Signed-off-by: Xiongfeng Wang <[email protected]>
---
crypto/gcm.c | 73 +++++++++++++++++++-----------------------------------------
1 file changed, 23 insertions(+), 50 deletions(-)

diff --git a/crypto/gcm.c b/crypto/gcm.c
index e438492..31eb694 100644
--- a/crypto/gcm.c
+++ b/crypto/gcm.c
@@ -727,12 +727,6 @@ static int crypto_gcm_create(struct crypto_template *tmpl, struct rtattr **tb)
ctr_name, "ghash");
}

-static struct crypto_template crypto_gcm_tmpl = {
- .name = "gcm",
- .create = crypto_gcm_create,
- .module = THIS_MODULE,
-};
-
static int crypto_gcm_base_create(struct crypto_template *tmpl,
struct rtattr **tb)
{
@@ -756,12 +750,6 @@ static int crypto_gcm_base_create(struct crypto_template *tmpl,
ctr_name, ghash_name);
}

-static struct crypto_template crypto_gcm_base_tmpl = {
- .name = "gcm_base",
- .create = crypto_gcm_base_create,
- .module = THIS_MODULE,
-};
-
static int crypto_rfc4106_setkey(struct crypto_aead *parent, const u8 *key,
unsigned int keylen)
{
@@ -989,12 +977,6 @@ static int crypto_rfc4106_create(struct crypto_template *tmpl,
goto out;
}

-static struct crypto_template crypto_rfc4106_tmpl = {
- .name = "rfc4106",
- .create = crypto_rfc4106_create,
- .module = THIS_MODULE,
-};
-
static int crypto_rfc4543_setkey(struct crypto_aead *parent, const u8 *key,
unsigned int keylen)
{
@@ -1231,10 +1213,24 @@ static int crypto_rfc4543_create(struct crypto_template *tmpl,
goto out;
}

-static struct crypto_template crypto_rfc4543_tmpl = {
- .name = "rfc4543",
- .create = crypto_rfc4543_create,
- .module = THIS_MODULE,
+static struct crypto_template crypto_gcm_tmpls[] = {
+ {
+ .name = "gcm_base",
+ .create = crypto_gcm_base_create,
+ .module = THIS_MODULE,
+ }, {
+ .name = "gcm",
+ .create = crypto_gcm_create,
+ .module = THIS_MODULE,
+ }, {
+ .name = "rfc4106",
+ .create = crypto_rfc4106_create,
+ .module = THIS_MODULE,
+ }, {
+ .name = "rfc4543",
+ .create = crypto_rfc4543_create,
+ .module = THIS_MODULE,
+ },
};

static int __init crypto_gcm_module_init(void)
@@ -1247,42 +1243,19 @@ static int __init crypto_gcm_module_init(void)

sg_init_one(&gcm_zeroes->sg, gcm_zeroes->buf, sizeof(gcm_zeroes->buf));

- err = crypto_register_template(&crypto_gcm_base_tmpl);
- if (err)
- goto out;
-
- err = crypto_register_template(&crypto_gcm_tmpl);
+ err = crypto_register_templates(crypto_gcm_tmpls,
+ ARRAY_SIZE(crypto_gcm_tmpls));
if (err)
- goto out_undo_base;
+ kfree(gcm_zeroes);

- err = crypto_register_template(&crypto_rfc4106_tmpl);
- if (err)
- goto out_undo_gcm;
-
- err = crypto_register_template(&crypto_rfc4543_tmpl);
- if (err)
- goto out_undo_rfc4106;
-
- return 0;
-
-out_undo_rfc4106:
- crypto_unregister_template(&crypto_rfc4106_tmpl);
-out_undo_gcm:
- crypto_unregister_template(&crypto_gcm_tmpl);
-out_undo_base:
- crypto_unregister_template(&crypto_gcm_base_tmpl);
-out:
- kfree(gcm_zeroes);
return err;
}

static void __exit crypto_gcm_module_exit(void)
{
kfree(gcm_zeroes);
- crypto_unregister_template(&crypto_rfc4543_tmpl);
- crypto_unregister_template(&crypto_rfc4106_tmpl);
- crypto_unregister_template(&crypto_gcm_tmpl);
- crypto_unregister_template(&crypto_gcm_base_tmpl);
+ crypto_unregister_templates(crypto_gcm_tmpls,
+ ARRAY_SIZE(crypto_gcm_tmpls));
}

module_init(crypto_gcm_module_init);
--
1.7.12.4


2019-01-17 09:19:34

by Xiongfeng Wang

[permalink] [raw]
Subject: [PATCH V2 5/5] crypto: chacha20poly1305 - use template array registering API to simplify the code

Use crypto template array registering API to simplify the code.

Signed-off-by: Xiongfeng Wang <[email protected]>
---
crypto/chacha20poly1305.c | 37 ++++++++++++++-----------------------
1 file changed, 14 insertions(+), 23 deletions(-)

diff --git a/crypto/chacha20poly1305.c b/crypto/chacha20poly1305.c
index fef1144..ed2e12e 100644
--- a/crypto/chacha20poly1305.c
+++ b/crypto/chacha20poly1305.c
@@ -701,37 +701,28 @@ static int rfc7539esp_create(struct crypto_template *tmpl, struct rtattr **tb)
return chachapoly_create(tmpl, tb, "rfc7539esp", 8);
}

-static struct crypto_template rfc7539_tmpl = {
- .name = "rfc7539",
- .create = rfc7539_create,
- .module = THIS_MODULE,
-};
-
-static struct crypto_template rfc7539esp_tmpl = {
- .name = "rfc7539esp",
- .create = rfc7539esp_create,
- .module = THIS_MODULE,
+static struct crypto_template rfc7539_tmpls[] = {
+ {
+ .name = "rfc7539",
+ .create = rfc7539_create,
+ .module = THIS_MODULE,
+ }, {
+ .name = "rfc7539esp",
+ .create = rfc7539esp_create,
+ .module = THIS_MODULE,
+ },
};

static int __init chacha20poly1305_module_init(void)
{
- int err;
-
- err = crypto_register_template(&rfc7539_tmpl);
- if (err)
- return err;
-
- err = crypto_register_template(&rfc7539esp_tmpl);
- if (err)
- crypto_unregister_template(&rfc7539_tmpl);
-
- return err;
+ return crypto_register_templates(rfc7539_tmpls,
+ ARRAY_SIZE(rfc7539_tmpls));
}

static void __exit chacha20poly1305_module_exit(void)
{
- crypto_unregister_template(&rfc7539esp_tmpl);
- crypto_unregister_template(&rfc7539_tmpl);
+ crypto_unregister_templates(rfc7539_tmpls,
+ ARRAY_SIZE(rfc7539_tmpls));
}

module_init(chacha20poly1305_module_init);
--
1.7.12.4


2019-01-17 09:19:47

by Xiongfeng Wang

[permalink] [raw]
Subject: [PATCH V2 4/5] crypto: ctr - use template array registering API to simplify the code

Use crypto template array registering API to simplify the code.

Signed-off-by: Xiongfeng Wang <[email protected]>
---
crypto/ctr.c | 43 +++++++++++++++----------------------------
1 file changed, 15 insertions(+), 28 deletions(-)

diff --git a/crypto/ctr.c b/crypto/ctr.c
index 4c743a9..d9f9d65 100644
--- a/crypto/ctr.c
+++ b/crypto/ctr.c
@@ -171,12 +171,6 @@ static int crypto_ctr_create(struct crypto_template *tmpl, struct rtattr **tb)
return err;
}

-static struct crypto_template crypto_ctr_tmpl = {
- .name = "ctr",
- .create = crypto_ctr_create,
- .module = THIS_MODULE,
-};
-
static int crypto_rfc3686_setkey(struct crypto_skcipher *parent,
const u8 *key, unsigned int keylen)
{
@@ -366,36 +360,29 @@ static int crypto_rfc3686_create(struct crypto_template *tmpl,
goto out;
}

-static struct crypto_template crypto_rfc3686_tmpl = {
- .name = "rfc3686",
- .create = crypto_rfc3686_create,
- .module = THIS_MODULE,
+static struct crypto_template crypto_ctr_tmpls[] = {
+ {
+ .name = "ctr",
+ .create = crypto_ctr_create,
+ .module = THIS_MODULE,
+ }, {
+ .name = "rfc3686",
+ .create = crypto_rfc3686_create,
+ .module = THIS_MODULE,
+ },
};

+
static int __init crypto_ctr_module_init(void)
{
- int err;
-
- err = crypto_register_template(&crypto_ctr_tmpl);
- if (err)
- goto out;
-
- err = crypto_register_template(&crypto_rfc3686_tmpl);
- if (err)
- goto out_drop_ctr;
-
-out:
- return err;
-
-out_drop_ctr:
- crypto_unregister_template(&crypto_ctr_tmpl);
- goto out;
+ return crypto_register_templates(crypto_ctr_tmpls,
+ ARRAY_SIZE(crypto_ctr_tmpls));
}

static void __exit crypto_ctr_module_exit(void)
{
- crypto_unregister_template(&crypto_rfc3686_tmpl);
- crypto_unregister_template(&crypto_ctr_tmpl);
+ crypto_unregister_templates(crypto_ctr_tmpls,
+ ARRAY_SIZE(crypto_ctr_tmpls));
}

module_init(crypto_ctr_module_init);
--
1.7.12.4


2019-01-17 09:20:03

by Xiongfeng Wang

[permalink] [raw]
Subject: [PATCH V2 1/5] crypto: api - add a helper to (un)register a array of templates

This patch add a helper to (un)register a array of templates. The
following patches will use this helper to simplify the code.

Signed-off-by: Xiongfeng Wang <[email protected]>
---
crypto/algapi.c | 27 +++++++++++++++++++++++++++
include/crypto/algapi.h | 2 ++
2 files changed, 29 insertions(+)

diff --git a/crypto/algapi.c b/crypto/algapi.c
index 713baab..4c9c86b 100644
--- a/crypto/algapi.c
+++ b/crypto/algapi.c
@@ -494,6 +494,24 @@ int crypto_register_template(struct crypto_template *tmpl)
}
EXPORT_SYMBOL_GPL(crypto_register_template);

+int crypto_register_templates(struct crypto_template *tmpls, int count)
+{
+ int i, err;
+
+ for (i = 0; i < count; i++) {
+ err = crypto_register_template(&tmpls[i]);
+ if (err)
+ goto out;
+ }
+ return 0;
+
+out:
+ for (--i; i >= 0; --i)
+ crypto_unregister_template(&tmpls[i]);
+ return err;
+}
+EXPORT_SYMBOL_GPL(crypto_register_templates);
+
void crypto_unregister_template(struct crypto_template *tmpl)
{
struct crypto_instance *inst;
@@ -523,6 +541,15 @@ void crypto_unregister_template(struct crypto_template *tmpl)
}
EXPORT_SYMBOL_GPL(crypto_unregister_template);

+void crypto_unregister_templates(struct crypto_template *tmpls, int count)
+{
+ int i;
+
+ for (i = count - 1; i >= 0; --i)
+ crypto_unregister_template(&tmpls[i]);
+}
+EXPORT_SYMBOL_GPL(crypto_unregister_templates);
+
static struct crypto_template *__crypto_lookup_template(const char *name)
{
struct crypto_template *q, *tmpl = NULL;
diff --git a/include/crypto/algapi.h b/include/crypto/algapi.h
index 093869f..4be38cd 100644
--- a/include/crypto/algapi.h
+++ b/include/crypto/algapi.h
@@ -143,7 +143,9 @@ struct ablkcipher_walk {
void crypto_mod_put(struct crypto_alg *alg);

int crypto_register_template(struct crypto_template *tmpl);
+int crypto_register_templates(struct crypto_template *tmpls, int count);
void crypto_unregister_template(struct crypto_template *tmpl);
+void crypto_unregister_templates(struct crypto_template *tmpls, int count);
struct crypto_template *crypto_lookup_template(const char *name);

int crypto_register_instance(struct crypto_template *tmpl,
--
1.7.12.4


2019-01-17 13:22:29

by Xiongfeng Wang

[permalink] [raw]
Subject: [PATCH V2 2/5] crypto: ccm - use template array registering API to simplify the code

Use crypto template array registering API to simplify the code.

Signed-off-by: Xiongfeng Wang <[email protected]>
---
crypto/ccm.c | 78 ++++++++++++++++++------------------------------------------
1 file changed, 23 insertions(+), 55 deletions(-)

diff --git a/crypto/ccm.c b/crypto/ccm.c
index b242fd0..50df8f0 100644
--- a/crypto/ccm.c
+++ b/crypto/ccm.c
@@ -589,12 +589,6 @@ static int crypto_ccm_create(struct crypto_template *tmpl, struct rtattr **tb)
mac_name);
}

-static struct crypto_template crypto_ccm_tmpl = {
- .name = "ccm",
- .create = crypto_ccm_create,
- .module = THIS_MODULE,
-};
-
static int crypto_ccm_base_create(struct crypto_template *tmpl,
struct rtattr **tb)
{
@@ -618,12 +612,6 @@ static int crypto_ccm_base_create(struct crypto_template *tmpl,
cipher_name);
}

-static struct crypto_template crypto_ccm_base_tmpl = {
- .name = "ccm_base",
- .create = crypto_ccm_base_create,
- .module = THIS_MODULE,
-};
-
static int crypto_rfc4309_setkey(struct crypto_aead *parent, const u8 *key,
unsigned int keylen)
{
@@ -854,12 +842,6 @@ static int crypto_rfc4309_create(struct crypto_template *tmpl,
goto out;
}

-static struct crypto_template crypto_rfc4309_tmpl = {
- .name = "rfc4309",
- .create = crypto_rfc4309_create,
- .module = THIS_MODULE,
-};
-
static int crypto_cbcmac_digest_setkey(struct crypto_shash *parent,
const u8 *inkey, unsigned int keylen)
{
@@ -999,51 +981,37 @@ static int cbcmac_create(struct crypto_template *tmpl, struct rtattr **tb)
return err;
}

-static struct crypto_template crypto_cbcmac_tmpl = {
- .name = "cbcmac",
- .create = cbcmac_create,
- .free = shash_free_instance,
- .module = THIS_MODULE,
+static struct crypto_template crypto_ccm_tmpls[] = {
+ {
+ .name = "cbcmac",
+ .create = cbcmac_create,
+ .free = shash_free_instance,
+ .module = THIS_MODULE,
+ }, {
+ .name = "ccm_base",
+ .create = crypto_ccm_base_create,
+ .module = THIS_MODULE,
+ }, {
+ .name = "ccm",
+ .create = crypto_ccm_create,
+ .module = THIS_MODULE,
+ }, {
+ .name = "rfc4309",
+ .create = crypto_rfc4309_create,
+ .module = THIS_MODULE,
+ },
};

static int __init crypto_ccm_module_init(void)
{
- int err;
-
- err = crypto_register_template(&crypto_cbcmac_tmpl);
- if (err)
- goto out;
-
- err = crypto_register_template(&crypto_ccm_base_tmpl);
- if (err)
- goto out_undo_cbcmac;
-
- err = crypto_register_template(&crypto_ccm_tmpl);
- if (err)
- goto out_undo_base;
-
- err = crypto_register_template(&crypto_rfc4309_tmpl);
- if (err)
- goto out_undo_ccm;
-
-out:
- return err;
-
-out_undo_ccm:
- crypto_unregister_template(&crypto_ccm_tmpl);
-out_undo_base:
- crypto_unregister_template(&crypto_ccm_base_tmpl);
-out_undo_cbcmac:
- crypto_register_template(&crypto_cbcmac_tmpl);
- goto out;
+ return crypto_register_templates(crypto_ccm_tmpls,
+ ARRAY_SIZE(crypto_ccm_tmpls));
}

static void __exit crypto_ccm_module_exit(void)
{
- crypto_unregister_template(&crypto_rfc4309_tmpl);
- crypto_unregister_template(&crypto_ccm_tmpl);
- crypto_unregister_template(&crypto_ccm_base_tmpl);
- crypto_unregister_template(&crypto_cbcmac_tmpl);
+ crypto_unregister_templates(crypto_ccm_tmpls,
+ ARRAY_SIZE(crypto_ccm_tmpls));
}

module_init(crypto_ccm_module_init);
--
1.7.12.4


2019-01-18 02:58:55

by Eric Biggers

[permalink] [raw]
Subject: Re: [PATCH V2 0/5] Crypto Cleanup

On Thu, Jan 17, 2019 at 02:47:58PM +0800, Xiongfeng Wang wrote:
> The patchset introduce a helper to (un)register a array of crypto templates.
> The following patches use this helper to simplify the code. This is also
> a preparation for a coming patchset, which will register several crypto
> templates.
>
> Changelog:
> v1 -> v2:
> rebased to cryptodev, fix some format issue
>
> Xiongfeng Wang (5):
> crypto: api - add a helper to (un)register a array of templates
> crypto: ccm - use template array registering API to simplify the code
> crypto: gcm - use template array registering API to simplify the code
> crypto: ctr - use template array registering API to simplify the code
> crypto: chacha20poly1305 - use template array registering API to
> simplify the code
>
> crypto/algapi.c | 27 ++++++++++++++++
> crypto/ccm.c | 78 ++++++++++++++---------------------------------
> crypto/chacha20poly1305.c | 37 +++++++++-------------
> crypto/ctr.c | 43 +++++++++-----------------
> crypto/gcm.c | 73 ++++++++++++++------------------------------
> include/crypto/algapi.h | 2 ++
> 6 files changed, 104 insertions(+), 156 deletions(-)
>
> --
> 1.7.12.4
>

For all 5 patches:

Reviewed-by: Eric Biggers <[email protected]>

However, you need to fix your email address to be the same in the Author and
Signed-off-by. Right now it's:

Author: Xiongfeng Wang <[email protected]>
Signed-off-by: Xiongfeng Wang <[email protected]>

(checkpatch.pl also warns about this.)

Thanks!

- Eric

2019-01-18 02:59:40

by Eric Biggers

[permalink] [raw]
Subject: Re: [PATCH V2 4/5] crypto: ctr - use template array registering API to simplify the code

On Thu, Jan 17, 2019 at 02:48:02PM +0800, Xiongfeng Wang wrote:
> Use crypto template array registering API to simplify the code.
>
> Signed-off-by: Xiongfeng Wang <[email protected]>
> ---
> crypto/ctr.c | 43 +++++++++++++++----------------------------
> 1 file changed, 15 insertions(+), 28 deletions(-)
>
> diff --git a/crypto/ctr.c b/crypto/ctr.c
> index 4c743a9..d9f9d65 100644
> --- a/crypto/ctr.c
> +++ b/crypto/ctr.c
> @@ -171,12 +171,6 @@ static int crypto_ctr_create(struct crypto_template *tmpl, struct rtattr **tb)
> return err;
> }
>
> -static struct crypto_template crypto_ctr_tmpl = {
> - .name = "ctr",
> - .create = crypto_ctr_create,
> - .module = THIS_MODULE,
> -};
> -
> static int crypto_rfc3686_setkey(struct crypto_skcipher *parent,
> const u8 *key, unsigned int keylen)
> {
> @@ -366,36 +360,29 @@ static int crypto_rfc3686_create(struct crypto_template *tmpl,
> goto out;
> }
>
> -static struct crypto_template crypto_rfc3686_tmpl = {
> - .name = "rfc3686",
> - .create = crypto_rfc3686_create,
> - .module = THIS_MODULE,
> +static struct crypto_template crypto_ctr_tmpls[] = {
> + {
> + .name = "ctr",
> + .create = crypto_ctr_create,
> + .module = THIS_MODULE,
> + }, {
> + .name = "rfc3686",
> + .create = crypto_rfc3686_create,
> + .module = THIS_MODULE,
> + },
> };
>
> +

If you resend please also remove the extra blank line here.

- Eric