Skip to content

Commit a208fa8

Browse files
ebiggersherbertx
authored andcommitted
crypto: hash - annotate algorithms taking optional key
We need to consistently enforce that keyed hashes cannot be used without setting the key. To do this we need a reliable way to determine whether a given hash algorithm is keyed or not. AF_ALG currently does this by checking for the presence of a ->setkey() method. However, this is actually slightly broken because the CRC-32 algorithms implement ->setkey() but can also be used without a key. (The CRC-32 "key" is not actually a cryptographic key but rather represents the initial state. If not overridden, then a default initial state is used.) Prepare to fix this by introducing a flag CRYPTO_ALG_OPTIONAL_KEY which indicates that the algorithm has a ->setkey() method, but it is not required to be called. Then set it on all the CRC-32 algorithms. The same also applies to the Adler-32 implementation in Lustre. Also, the cryptd and mcryptd templates have to pass through the flag from their underlying algorithm. Cc: stable@vger.kernel.org Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent a16e772 commit a208fa8

15 files changed

Lines changed: 30 additions & 9 deletions

File tree

‎arch/arm/crypto/crc32-ce-glue.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ static struct shash_alg crc32_pmull_algs[] = { {
188188
.base.cra_name = "crc32",
189189
.base.cra_driver_name = "crc32-arm-ce",
190190
.base.cra_priority = 200,
191+
.base.cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
191192
.base.cra_blocksize = 1,
192193
.base.cra_module = THIS_MODULE,
193194
}, {
@@ -203,6 +204,7 @@ static struct shash_alg crc32_pmull_algs[] = { {
203204
.base.cra_name = "crc32c",
204205
.base.cra_driver_name = "crc32c-arm-ce",
205206
.base.cra_priority = 200,
207+
.base.cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
206208
.base.cra_blocksize = 1,
207209
.base.cra_module = THIS_MODULE,
208210
} };

‎arch/arm64/crypto/crc32-ce-glue.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ static struct shash_alg crc32_pmull_algs[] = { {
185185
.base.cra_name = "crc32",
186186
.base.cra_driver_name = "crc32-arm64-ce",
187187
.base.cra_priority = 200,
188+
.base.cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
188189
.base.cra_blocksize = 1,
189190
.base.cra_module = THIS_MODULE,
190191
}, {
@@ -200,6 +201,7 @@ static struct shash_alg crc32_pmull_algs[] = { {
200201
.base.cra_name = "crc32c",
201202
.base.cra_driver_name = "crc32c-arm64-ce",
202203
.base.cra_priority = 200,
204+
.base.cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
203205
.base.cra_blocksize = 1,
204206
.base.cra_module = THIS_MODULE,
205207
} };

‎arch/powerpc/crypto/crc32c-vpmsum_glue.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ static struct shash_alg alg = {
141141
.cra_name = "crc32c",
142142
.cra_driver_name = "crc32c-vpmsum",
143143
.cra_priority = 200,
144+
.cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
144145
.cra_blocksize = CHKSUM_BLOCK_SIZE,
145146
.cra_ctxsize = sizeof(u32),
146147
.cra_module = THIS_MODULE,

‎arch/s390/crypto/crc32-vx.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ static struct shash_alg crc32_vx_algs[] = {
239239
.cra_name = "crc32",
240240
.cra_driver_name = "crc32-vx",
241241
.cra_priority = 200,
242+
.cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
242243
.cra_blocksize = CRC32_BLOCK_SIZE,
243244
.cra_ctxsize = sizeof(struct crc_ctx),
244245
.cra_module = THIS_MODULE,
@@ -259,6 +260,7 @@ static struct shash_alg crc32_vx_algs[] = {
259260
.cra_name = "crc32be",
260261
.cra_driver_name = "crc32be-vx",
261262
.cra_priority = 200,
263+
.cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
262264
.cra_blocksize = CRC32_BLOCK_SIZE,
263265
.cra_ctxsize = sizeof(struct crc_ctx),
264266
.cra_module = THIS_MODULE,
@@ -279,6 +281,7 @@ static struct shash_alg crc32_vx_algs[] = {
279281
.cra_name = "crc32c",
280282
.cra_driver_name = "crc32c-vx",
281283
.cra_priority = 200,
284+
.cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
282285
.cra_blocksize = CRC32_BLOCK_SIZE,
283286
.cra_ctxsize = sizeof(struct crc_ctx),
284287
.cra_module = THIS_MODULE,

‎arch/sparc/crypto/crc32c_glue.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ static struct shash_alg alg = {
133133
.cra_name = "crc32c",
134134
.cra_driver_name = "crc32c-sparc64",
135135
.cra_priority = SPARC_CR_OPCODE_PRIORITY,
136+
.cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
136137
.cra_blocksize = CHKSUM_BLOCK_SIZE,
137138
.cra_ctxsize = sizeof(u32),
138139
.cra_alignmask = 7,

‎arch/x86/crypto/crc32-pclmul_glue.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ static struct shash_alg alg = {
162162
.cra_name = "crc32",
163163
.cra_driver_name = "crc32-pclmul",
164164
.cra_priority = 200,
165+
.cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
165166
.cra_blocksize = CHKSUM_BLOCK_SIZE,
166167
.cra_ctxsize = sizeof(u32),
167168
.cra_module = THIS_MODULE,

‎arch/x86/crypto/crc32c-intel_glue.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ static struct shash_alg alg = {
226226
.cra_name = "crc32c",
227227
.cra_driver_name = "crc32c-intel",
228228
.cra_priority = 200,
229+
.cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
229230
.cra_blocksize = CHKSUM_BLOCK_SIZE,
230231
.cra_ctxsize = sizeof(u32),
231232
.cra_module = THIS_MODULE,

‎crypto/crc32_generic.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ static struct shash_alg alg = {
133133
.cra_name = "crc32",
134134
.cra_driver_name = "crc32-generic",
135135
.cra_priority = 100,
136+
.cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
136137
.cra_blocksize = CHKSUM_BLOCK_SIZE,
137138
.cra_ctxsize = sizeof(u32),
138139
.cra_module = THIS_MODULE,

‎crypto/crc32c_generic.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ static struct shash_alg alg = {
146146
.cra_name = "crc32c",
147147
.cra_driver_name = "crc32c-generic",
148148
.cra_priority = 100,
149+
.cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
149150
.cra_blocksize = CHKSUM_BLOCK_SIZE,
150151
.cra_alignmask = 3,
151152
.cra_ctxsize = sizeof(struct chksum_ctx),

‎crypto/cryptd.c‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -896,10 +896,9 @@ static int cryptd_create_hash(struct crypto_template *tmpl, struct rtattr **tb,
896896
if (err)
897897
goto out_free_inst;
898898

899-
type = CRYPTO_ALG_ASYNC;
900-
if (alg->cra_flags & CRYPTO_ALG_INTERNAL)
901-
type |= CRYPTO_ALG_INTERNAL;
902-
inst->alg.halg.base.cra_flags = type;
899+
inst->alg.halg.base.cra_flags = CRYPTO_ALG_ASYNC |
900+
(alg->cra_flags & (CRYPTO_ALG_INTERNAL |
901+
CRYPTO_ALG_OPTIONAL_KEY));
903902

904903
inst->alg.halg.digestsize = salg->digestsize;
905904
inst->alg.halg.statesize = salg->statesize;

0 commit comments

Comments
 (0)