1 From f699594d436960160f6d5ba84ed4a222f20d11cd Mon Sep 17 00:00:00 2001
2 From: Eric Biggers <ebiggers@google.com>
3 Date: Thu, 18 Apr 2019 14:43:02 -0700
4 Subject: crypto: gcm - fix incompatibility between "gcm" and "gcm_base"
6 From: Eric Biggers <ebiggers@google.com>
8 commit f699594d436960160f6d5ba84ed4a222f20d11cd upstream.
10 GCM instances can be created by either the "gcm" template, which only
11 allows choosing the block cipher, e.g. "gcm(aes)"; or by "gcm_base",
12 which allows choosing the ctr and ghash implementations, e.g.
13 "gcm_base(ctr(aes-generic),ghash-generic)".
15 However, a "gcm_base" instance prevents a "gcm" instance from being
16 registered using the same implementations. Nor will the instance be
17 found by lookups of "gcm". This can be used as a denial of service.
18 Moreover, "gcm_base" instances are never tested by the crypto
19 self-tests, even if there are compatible "gcm" tests.
21 The root cause of these problems is that instances of the two templates
22 use different cra_names. Therefore, fix these problems by making
23 "gcm_base" instances set the same cra_name as "gcm" instances, e.g.
24 "gcm(aes)" instead of "gcm_base(ctr(aes-generic),ghash-generic)".
26 This requires extracting the block cipher name from the name of the ctr
27 algorithm. It also requires starting to verify that the algorithms are
28 really ctr and ghash, not something else entirely. But it would be
29 bizarre if anyone were actually using non-gcm-compatible algorithms with
30 gcm_base, so this shouldn't break anyone in practice.
32 Fixes: d00aa19b507b ("[CRYPTO] gcm: Allow block cipher parameter")
33 Cc: stable@vger.kernel.org
34 Signed-off-by: Eric Biggers <ebiggers@google.com>
35 Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
36 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
40 crypto/gcm.c | 34 +++++++++++-----------------------
41 1 file changed, 11 insertions(+), 23 deletions(-)
45 @@ -616,7 +616,6 @@ static void crypto_gcm_free(struct aead_
47 static int crypto_gcm_create_common(struct crypto_template *tmpl,
49 - const char *full_name,
51 const char *ghash_name)
53 @@ -657,7 +656,8 @@ static int crypto_gcm_create_common(stru
57 - if (ghash->digestsize != 16)
58 + if (strcmp(ghash->base.cra_name, "ghash") != 0 ||
59 + ghash->digestsize != 16)
62 crypto_set_skcipher_spawn(&ctx->ctr, aead_crypto_instance(inst));
63 @@ -669,24 +669,24 @@ static int crypto_gcm_create_common(stru
65 ctr = crypto_skcipher_spawn_alg(&ctx->ctr);
67 - /* We only support 16-byte blocks. */
68 + /* The skcipher algorithm must be CTR mode, using 16-byte blocks. */
70 - if (ctr->cra_ablkcipher.ivsize != 16)
71 + if (strncmp(ctr->cra_name, "ctr(", 4) != 0 ||
72 + ctr->cra_ablkcipher.ivsize != 16 ||
73 + ctr->cra_blocksize != 1)
76 - /* Not a stream cipher? */
77 - if (ctr->cra_blocksize != 1)
78 + err = -ENAMETOOLONG;
79 + if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
80 + "gcm(%s", ctr->cra_name + 4) >= CRYPTO_MAX_ALG_NAME)
83 - err = -ENAMETOOLONG;
84 if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
85 "gcm_base(%s,%s)", ctr->cra_driver_name,
86 ghash_alg->cra_driver_name) >=
90 - memcpy(inst->alg.base.cra_name, full_name, CRYPTO_MAX_ALG_NAME);
92 inst->alg.base.cra_flags = (ghash->base.cra_flags | ctr->cra_flags) &
94 inst->alg.base.cra_priority = (ghash->base.cra_priority +
95 @@ -727,7 +727,6 @@ static int crypto_gcm_create(struct cryp
97 const char *cipher_name;
98 char ctr_name[CRYPTO_MAX_ALG_NAME];
99 - char full_name[CRYPTO_MAX_ALG_NAME];
101 cipher_name = crypto_attr_alg_name(tb[1]);
102 if (IS_ERR(cipher_name))
103 @@ -737,12 +736,7 @@ static int crypto_gcm_create(struct cryp
105 return -ENAMETOOLONG;
107 - if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "gcm(%s)", cipher_name) >=
108 - CRYPTO_MAX_ALG_NAME)
109 - return -ENAMETOOLONG;
111 - return crypto_gcm_create_common(tmpl, tb, full_name,
112 - ctr_name, "ghash");
113 + return crypto_gcm_create_common(tmpl, tb, ctr_name, "ghash");
116 static struct crypto_template crypto_gcm_tmpl = {
117 @@ -756,7 +750,6 @@ static int crypto_gcm_base_create(struct
119 const char *ctr_name;
120 const char *ghash_name;
121 - char full_name[CRYPTO_MAX_ALG_NAME];
123 ctr_name = crypto_attr_alg_name(tb[1]);
124 if (IS_ERR(ctr_name))
125 @@ -766,12 +759,7 @@ static int crypto_gcm_base_create(struct
126 if (IS_ERR(ghash_name))
127 return PTR_ERR(ghash_name);
129 - if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "gcm_base(%s,%s)",
130 - ctr_name, ghash_name) >= CRYPTO_MAX_ALG_NAME)
131 - return -ENAMETOOLONG;
133 - return crypto_gcm_create_common(tmpl, tb, full_name,
134 - ctr_name, ghash_name);
135 + return crypto_gcm_create_common(tmpl, tb, ctr_name, ghash_name);
138 static struct crypto_template crypto_gcm_base_tmpl = {