2019-01-16 02:51:17

by Xiongfeng Wang

[permalink] [raw]
Subject: [PATCH 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.

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 | 81 +++++++++++++++--------------------------------
crypto/chacha20poly1305.c | 38 +++++++++-------------
crypto/ctr.c | 46 ++++++++++-----------------
crypto/gcm.c | 76 +++++++++++++++-----------------------------
include/crypto/algapi.h | 2 ++
6 files changed, 113 insertions(+), 157 deletions(-)

--
1.7.12.4


2019-01-17 02:51:15

by Xiongfeng Wang

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



On 2019/1/17 1:49, Eric Biggers wrote:
> On Wed, Jan 16, 2019 at 10:50:29AM +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.
>>
>> 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 | 81 +++++++++++++++--------------------------------
>> crypto/chacha20poly1305.c | 38 +++++++++-------------
>> crypto/ctr.c | 46 ++++++++++-----------------
>> crypto/gcm.c | 76 +++++++++++++++-----------------------------
>> include/crypto/algapi.h | 2 ++
>> 6 files changed, 113 insertions(+), 157 deletions(-)
>>
>> --
>> 1.7.12.4
>>
>
> Hi Xiongfeng, I think this a useful cleanup but can you please rebase this onto
> cryptodev? There is a conflict in crypto/ctr.c.

Hi Eric, thanks for your reply. I will change it in the next version.

Thanks,
Xiongfeng
>
> - Eric
>
> .
>

2019-01-16 02:51:32

by Xiongfeng Wang

[permalink] [raw]
Subject: [PATCH 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 | 76 +++++++++++++++++++++---------------------------------------
1 file changed, 26 insertions(+), 50 deletions(-)

diff --git a/crypto/gcm.c b/crypto/gcm.c
index e438492..c8d5fe3 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,27 @@ 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_tmpl[] = {
+ {
+ .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 +1246,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_tmpl,
+ ARRAY_SIZE(crypto_gcm_tmpl));
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_tmpl,
+ ARRAY_SIZE(crypto_gcm_tmpl));
}

module_init(crypto_gcm_module_init);
--
1.7.12.4

2019-01-16 02:51:32

by Xiongfeng Wang

[permalink] [raw]
Subject: [PATCH 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 | 81 +++++++++++++++++++-----------------------------------------
1 file changed, 26 insertions(+), 55 deletions(-)

diff --git a/crypto/ccm.c b/crypto/ccm.c
index b242fd0..8949aa2 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,40 @@ 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_tmpl[] = {
+ {
+ .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_tmpl,
+ ARRAY_SIZE(crypto_ccm_tmpl));
}

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_tmpl,
+ ARRAY_SIZE(crypto_ccm_tmpl));
}

module_init(crypto_ccm_module_init);
--
1.7.12.4

2019-01-16 02:51:17

by Xiongfeng Wang

[permalink] [raw]
Subject: [PATCH 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 8b65ada..59a6599 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 *tmpl, int num)
+{
+ int i, err;
+
+ for (i = 0; i < num; i++) {
+ err = crypto_register_template(&tmpl[i]);
+ if (err)
+ goto out;
+ }
+ return 0;
+
+out:
+ for (i -= 1; i >= 0; i--)
+ crypto_unregister_template(&tmpl[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 *tmpl, int num)
+{
+ int i;
+
+ for (i = num - 1; i >= 0; i--)
+ crypto_unregister_template(&tmpl[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 4a5ad10..e879a20 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 *tmpl, int num);
void crypto_unregister_template(struct crypto_template *tmpl);
+void crypto_unregister_templates(struct crypto_template *tmpl, int num);
struct crypto_template *crypto_lookup_template(const char *name);

int crypto_register_instance(struct crypto_template *tmpl,
--
1.7.12.4

2019-01-16 17:57:03

by Eric Biggers

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

On Wed, Jan 16, 2019 at 10:50:31AM +0800, Xiongfeng Wang wrote:
> Use crypto template array registering API to simplify the code.
>
> Signed-off-by: Xiongfeng Wang <[email protected]>
> ---
> crypto/ccm.c | 81 +++++++++++++++++++-----------------------------------------
> 1 file changed, 26 insertions(+), 55 deletions(-)
>
> diff --git a/crypto/ccm.c b/crypto/ccm.c
> index b242fd0..8949aa2 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,40 @@ 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_tmpl[] = {

Add an 's': 'crypto_ccm_tmpls'. Likewise everywhere else in this patch series
where you're defining an array of crypto_templates.

> + {
> + .name = "cbcmac",
> + .create = cbcmac_create,
> + .free = shash_free_instance,
> + .module = THIS_MODULE,
> + },
> + {

In a list of struct definitions, the { should be on the same line as the
previous '},'. Likewise everywhere else in this patch series where you're
defining an array of crypto_templates.

> + .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_tmpl,
> + ARRAY_SIZE(crypto_ccm_tmpl));
> }

Please fix the indentation here, so the continuation line is aligned.

return crypto_register_templates(crypto_ccm_tmpl,
ARRAY_SIZE(crypto_ccm_tmpl));

Likewise everywhere else in the patch series where you're registering or
unregistering templates.

>
> 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_tmpl,
> + ARRAY_SIZE(crypto_ccm_tmpl));
> }

Same here.

crypto_unregister_templates(crypto_ccm_tmpl,
ARRAY_SIZE(crypto_ccm_tmpl));

>
> module_init(crypto_ccm_module_init);
> --
> 1.7.12.4
>

Thanks,

- Eric

2019-01-16 17:49:53

by Eric Biggers

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

On Wed, Jan 16, 2019 at 10:50:29AM +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.
>
> 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 | 81 +++++++++++++++--------------------------------
> crypto/chacha20poly1305.c | 38 +++++++++-------------
> crypto/ctr.c | 46 ++++++++++-----------------
> crypto/gcm.c | 76 +++++++++++++++-----------------------------
> include/crypto/algapi.h | 2 ++
> 6 files changed, 113 insertions(+), 157 deletions(-)
>
> --
> 1.7.12.4
>

Hi Xiongfeng, I think this a useful cleanup but can you please rebase this onto
cryptodev? There is a conflict in crypto/ctr.c.

- Eric

2019-01-16 17:52:39

by Eric Biggers

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

A few nits:

On Wed, Jan 16, 2019 at 10:50:30AM +0800, Xiongfeng Wang wrote:
> 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 8b65ada..59a6599 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 *tmpl, int num)

Can you adjust the parameter names to make it consistent with the other bulk
registration functions?

int crypto_register_templates(struct crypto_template *tmpls, int count)

Note the 's' on 'tmpls'.

> +{
> + int i, err;
> +
> + for (i = 0; i < num; i++) {
> + err = crypto_register_template(&tmpl[i]);
> + if (err)
> + goto out;
> + }
> + return 0;
> +
> +out:
> + for (i -= 1; i >= 0; i--)

Other places write this as: 'for (--i; i >= 0; --i)'

> + crypto_unregister_template(&tmpl[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 *tmpl, int num)
> +{

Same here.

> + int i;
> +
> + for (i = num - 1; i >= 0; i--)
> + crypto_unregister_template(&tmpl[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 4a5ad10..e879a20 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 *tmpl, int num);
> void crypto_unregister_template(struct crypto_template *tmpl);
> +void crypto_unregister_templates(struct crypto_template *tmpl, int num);

And here.

- Eric

2019-01-16 02:51:32

by Xiongfeng Wang

[permalink] [raw]
Subject: [PATCH 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 | 46 +++++++++++++++++-----------------------------
1 file changed, 17 insertions(+), 29 deletions(-)

diff --git a/crypto/ctr.c b/crypto/ctr.c
index 30f3946..ef51099 100644
--- a/crypto/ctr.c
+++ b/crypto/ctr.c
@@ -248,13 +248,6 @@ static void crypto_ctr_free(struct crypto_instance *inst)
kfree(inst);
}

-static struct crypto_template crypto_ctr_tmpl = {
- .name = "ctr",
- .alloc = crypto_ctr_alloc,
- .free = crypto_ctr_free,
- .module = THIS_MODULE,
-};
-
static int crypto_rfc3686_setkey(struct crypto_skcipher *parent,
const u8 *key, unsigned int keylen)
{
@@ -444,36 +437,31 @@ 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_tmpl[] = {
+ {
+ .name = "ctr",
+ .alloc = crypto_ctr_alloc,
+ .free = crypto_ctr_free,
+ .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_tmpl,
+ ARRAY_SIZE(crypto_ctr_tmpl));
}

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_tmpl,
+ ARRAY_SIZE(crypto_ctr_tmpl));
}

module_init(crypto_ctr_module_init);
--
1.7.12.4

2019-01-16 02:51:32

by Xiongfeng Wang

[permalink] [raw]
Subject: [PATCH 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 | 38 +++++++++++++++-----------------------
1 file changed, 15 insertions(+), 23 deletions(-)

diff --git a/crypto/chacha20poly1305.c b/crypto/chacha20poly1305.c
index fef1144..f5134d3 100644
--- a/crypto/chacha20poly1305.c
+++ b/crypto/chacha20poly1305.c
@@ -701,37 +701,29 @@ 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_tmpl[] = {
+ {
+ .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_tmpl,
+ ARRAY_SIZE(rfc7539_tmpl));
}

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

module_init(chacha20poly1305_module_init);
--
1.7.12.4