From: Eric Biggers <ebigg...@google.com>

The shash_alg structs for the four variants of BLAKE2b are identical
except for the algorithm name, driver name, and digest size.  So, avoid
code duplication by using a macro to define these structs.

Signed-off-by: Eric Biggers <ebigg...@google.com>
---
 crypto/blake2b_generic.c | 82 ++++++++++++----------------------------
 1 file changed, 25 insertions(+), 57 deletions(-)

diff --git a/crypto/blake2b_generic.c b/crypto/blake2b_generic.c
index 83942f511075e..0e38e3e48297c 100644
--- a/crypto/blake2b_generic.c
+++ b/crypto/blake2b_generic.c
@@ -236,64 +236,32 @@ static int blake2b_final(struct shash_desc *desc, u8 *out)
        return 0;
 }
 
-static struct shash_alg blake2b_algs[] = {
-       {
-               .base.cra_name          = "blake2b-160",
-               .base.cra_driver_name   = "blake2b-160-generic",
-               .base.cra_priority      = 100,
-               .base.cra_flags         = CRYPTO_ALG_OPTIONAL_KEY,
-               .base.cra_blocksize     = BLAKE2B_BLOCK_SIZE,
-               .base.cra_ctxsize       = sizeof(struct blake2b_tfm_ctx),
-               .base.cra_module        = THIS_MODULE,
-               .digestsize             = BLAKE2B_160_HASH_SIZE,
-               .setkey                 = blake2b_setkey,
-               .init                   = blake2b_init,
-               .update                 = blake2b_update,
-               .final                  = blake2b_final,
-               .descsize               = sizeof(struct blake2b_state),
-       }, {
-               .base.cra_name          = "blake2b-256",
-               .base.cra_driver_name   = "blake2b-256-generic",
-               .base.cra_priority      = 100,
-               .base.cra_flags         = CRYPTO_ALG_OPTIONAL_KEY,
-               .base.cra_blocksize     = BLAKE2B_BLOCK_SIZE,
-               .base.cra_ctxsize       = sizeof(struct blake2b_tfm_ctx),
-               .base.cra_module        = THIS_MODULE,
-               .digestsize             = BLAKE2B_256_HASH_SIZE,
-               .setkey                 = blake2b_setkey,
-               .init                   = blake2b_init,
-               .update                 = blake2b_update,
-               .final                  = blake2b_final,
-               .descsize               = sizeof(struct blake2b_state),
-       }, {
-               .base.cra_name          = "blake2b-384",
-               .base.cra_driver_name   = "blake2b-384-generic",
-               .base.cra_priority      = 100,
-               .base.cra_flags         = CRYPTO_ALG_OPTIONAL_KEY,
-               .base.cra_blocksize     = BLAKE2B_BLOCK_SIZE,
-               .base.cra_ctxsize       = sizeof(struct blake2b_tfm_ctx),
-               .base.cra_module        = THIS_MODULE,
-               .digestsize             = BLAKE2B_384_HASH_SIZE,
-               .setkey                 = blake2b_setkey,
-               .init                   = blake2b_init,
-               .update                 = blake2b_update,
-               .final                  = blake2b_final,
-               .descsize               = sizeof(struct blake2b_state),
-       }, {
-               .base.cra_name          = "blake2b-512",
-               .base.cra_driver_name   = "blake2b-512-generic",
-               .base.cra_priority      = 100,
-               .base.cra_flags         = CRYPTO_ALG_OPTIONAL_KEY,
-               .base.cra_blocksize     = BLAKE2B_BLOCK_SIZE,
-               .base.cra_ctxsize       = sizeof(struct blake2b_tfm_ctx),
-               .base.cra_module        = THIS_MODULE,
-               .digestsize             = BLAKE2B_512_HASH_SIZE,
-               .setkey                 = blake2b_setkey,
-               .init                   = blake2b_init,
-               .update                 = blake2b_update,
-               .final                  = blake2b_final,
-               .descsize               = sizeof(struct blake2b_state),
+#define BLAKE2B_ALG(name, driver_name, digest_size)                    \
+       {                                                               \
+               .base.cra_name          = name,                         \
+               .base.cra_driver_name   = driver_name,                  \
+               .base.cra_priority      = 100,                          \
+               .base.cra_flags         = CRYPTO_ALG_OPTIONAL_KEY,      \
+               .base.cra_blocksize     = BLAKE2B_BLOCK_SIZE,           \
+               .base.cra_ctxsize       = sizeof(struct blake2b_tfm_ctx), \
+               .base.cra_module        = THIS_MODULE,                  \
+               .digestsize             = digest_size,                  \
+               .setkey                 = blake2b_setkey,               \
+               .init                   = blake2b_init,                 \
+               .update                 = blake2b_update,               \
+               .final                  = blake2b_final,                \
+               .descsize               = sizeof(struct blake2b_state), \
        }
+
+static struct shash_alg blake2b_algs[] = {
+       BLAKE2B_ALG("blake2b-160", "blake2b-160-generic",
+                   BLAKE2B_160_HASH_SIZE),
+       BLAKE2B_ALG("blake2b-256", "blake2b-256-generic",
+                   BLAKE2B_256_HASH_SIZE),
+       BLAKE2B_ALG("blake2b-384", "blake2b-384-generic",
+                   BLAKE2B_384_HASH_SIZE),
+       BLAKE2B_ALG("blake2b-512", "blake2b-512-generic",
+                   BLAKE2B_512_HASH_SIZE),
 };
 
 static int __init blake2b_mod_init(void)
-- 
2.29.2

Reply via email to