Skip to content

Commit dc26c17

Browse files
ebiggersherbertx
authored andcommitted
crypto: aead - prevent using AEADs without setting key
Similar to what was done for the hash API, update the AEAD API to track whether each transform has been keyed, and reject encryption/decryption if a key is needed but one hasn't been set. This isn't quite as important as the equivalent fix for the hash API because AEADs always require a key, so are unlikely to be used without one. Still, tracking the key will prevent accidental unkeyed use. algif_aead also had to track the key anyway, so the new flag replaces that and slightly simplifies the algif_aead implementation. Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent f8d33fa commit dc26c17

3 files changed

Lines changed: 23 additions & 11 deletions

File tree

‎crypto/aead.c‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,18 @@ int crypto_aead_setkey(struct crypto_aead *tfm,
5454
const u8 *key, unsigned int keylen)
5555
{
5656
unsigned long alignmask = crypto_aead_alignmask(tfm);
57+
int err;
5758

5859
if ((unsigned long)key & alignmask)
59-
return setkey_unaligned(tfm, key, keylen);
60+
err = setkey_unaligned(tfm, key, keylen);
61+
else
62+
err = crypto_aead_alg(tfm)->setkey(tfm, key, keylen);
63+
64+
if (err)
65+
return err;
6066

61-
return crypto_aead_alg(tfm)->setkey(tfm, key, keylen);
67+
crypto_aead_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
68+
return 0;
6269
}
6370
EXPORT_SYMBOL_GPL(crypto_aead_setkey);
6471

@@ -93,6 +100,8 @@ static int crypto_aead_init_tfm(struct crypto_tfm *tfm)
93100
struct crypto_aead *aead = __crypto_aead_cast(tfm);
94101
struct aead_alg *alg = crypto_aead_alg(aead);
95102

103+
crypto_aead_set_flags(aead, CRYPTO_TFM_NEED_KEY);
104+
96105
aead->authsize = alg->maxauthsize;
97106

98107
if (alg->exit)

‎crypto/algif_aead.c‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242

4343
struct aead_tfm {
4444
struct crypto_aead *aead;
45-
bool has_key;
4645
struct crypto_skcipher *null_tfm;
4746
};
4847

@@ -398,7 +397,7 @@ static int aead_check_key(struct socket *sock)
398397

399398
err = -ENOKEY;
400399
lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
401-
if (!tfm->has_key)
400+
if (crypto_aead_get_flags(tfm->aead) & CRYPTO_TFM_NEED_KEY)
402401
goto unlock;
403402

404403
if (!pask->refcnt++)
@@ -523,12 +522,8 @@ static int aead_setauthsize(void *private, unsigned int authsize)
523522
static int aead_setkey(void *private, const u8 *key, unsigned int keylen)
524523
{
525524
struct aead_tfm *tfm = private;
526-
int err;
527-
528-
err = crypto_aead_setkey(tfm->aead, key, keylen);
529-
tfm->has_key = !err;
530525

531-
return err;
526+
return crypto_aead_setkey(tfm->aead, key, keylen);
532527
}
533528

534529
static void aead_sock_destruct(struct sock *sk)
@@ -589,7 +584,7 @@ static int aead_accept_parent(void *private, struct sock *sk)
589584
{
590585
struct aead_tfm *tfm = private;
591586

592-
if (!tfm->has_key)
587+
if (crypto_aead_get_flags(tfm->aead) & CRYPTO_TFM_NEED_KEY)
593588
return -ENOKEY;
594589

595590
return aead_accept_parent_nokey(private, sk);

‎include/crypto/aead.h‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,12 @@ static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
327327
*/
328328
static inline int crypto_aead_encrypt(struct aead_request *req)
329329
{
330-
return crypto_aead_alg(crypto_aead_reqtfm(req))->encrypt(req);
330+
struct crypto_aead *aead = crypto_aead_reqtfm(req);
331+
332+
if (crypto_aead_get_flags(aead) & CRYPTO_TFM_NEED_KEY)
333+
return -ENOKEY;
334+
335+
return crypto_aead_alg(aead)->encrypt(req);
331336
}
332337

333338
/**
@@ -356,6 +361,9 @@ static inline int crypto_aead_decrypt(struct aead_request *req)
356361
{
357362
struct crypto_aead *aead = crypto_aead_reqtfm(req);
358363

364+
if (crypto_aead_get_flags(aead) & CRYPTO_TFM_NEED_KEY)
365+
return -ENOKEY;
366+
359367
if (req->cryptlen < crypto_aead_authsize(aead))
360368
return -EINVAL;
361369

0 commit comments

Comments
 (0)