Skip to content

Commit c288178

Browse files
ebiggersherbertx
authored andcommitted
crypto: shash - allow essiv and hmac to use OPTIONAL_KEY algorithms
The essiv and hmac templates refuse to use any hash algorithm that has a ->setkey() function, which includes not just algorithms that always need a key, but also algorithms that optionally take a key. Previously the only optionally-keyed hash algorithms in the crypto API were non-cryptographic algorithms like crc32, so this didn't really matter. But that's changed with BLAKE2 support being added. BLAKE2 should work with essiv and hmac, just like any other cryptographic hash. Fix this by allowing the use of both algorithms without a ->setkey() function and algorithms that have the OPTIONAL_KEY flag set. Signed-off-by: Eric Biggers <ebiggers@google.com> Acked-by: Ard Biesheuvel <ardb@kernel.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 89873b4 commit c288178

4 files changed

Lines changed: 10 additions & 5 deletions

File tree

‎crypto/essiv.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ static bool essiv_supported_algorithms(const char *essiv_cipher_name,
442442
if (ivsize != alg->cra_blocksize)
443443
goto out;
444444

445-
if (crypto_shash_alg_has_setkey(hash_alg))
445+
if (crypto_shash_alg_needs_key(hash_alg))
446446
goto out;
447447

448448
ret = true;

‎crypto/hmac.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,9 @@ static int hmac_create(struct crypto_template *tmpl, struct rtattr **tb)
185185
return PTR_ERR(salg);
186186
alg = &salg->base;
187187

188-
/* The underlying hash algorithm must be unkeyed */
188+
/* The underlying hash algorithm must not require a key */
189189
err = -EINVAL;
190-
if (crypto_shash_alg_has_setkey(salg))
190+
if (crypto_shash_alg_needs_key(salg))
191191
goto out_put_alg;
192192

193193
ds = salg->digestsize;

‎crypto/shash.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ static int shash_setkey_unaligned(struct crypto_shash *tfm, const u8 *key,
5050

5151
static void shash_set_needkey(struct crypto_shash *tfm, struct shash_alg *alg)
5252
{
53-
if (crypto_shash_alg_has_setkey(alg) &&
54-
!(alg->base.cra_flags & CRYPTO_ALG_OPTIONAL_KEY))
53+
if (crypto_shash_alg_needs_key(alg))
5554
crypto_shash_set_flags(tfm, CRYPTO_TFM_NEED_KEY);
5655
}
5756

‎include/crypto/internal/hash.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ static inline bool crypto_shash_alg_has_setkey(struct shash_alg *alg)
8585
return alg->setkey != shash_no_setkey;
8686
}
8787

88+
static inline bool crypto_shash_alg_needs_key(struct shash_alg *alg)
89+
{
90+
return crypto_shash_alg_has_setkey(alg) &&
91+
!(alg->base.cra_flags & CRYPTO_ALG_OPTIONAL_KEY);
92+
}
93+
8894
bool crypto_hash_alg_has_setkey(struct hash_alg_common *halg);
8995

9096
int crypto_init_ahash_spawn(struct crypto_ahash_spawn *spawn,

0 commit comments

Comments
 (0)