Skip to content

Commit

Permalink
crypto: testmgr - use consistent IV copies for AEADs that need it
Browse files Browse the repository at this point in the history
rfc4543 was missing from the list of algorithms that may treat the end
of the AAD buffer specially.

Also, with rfc4106, rfc4309, rfc4543, and rfc7539esp, the end of the AAD
buffer is actually supposed to contain a second copy of the IV, and
we've concluded that if the IV copies don't match the behavior is
implementation-defined.  So, the fuzz tests can't easily test that case.

So, make the fuzz tests only use inputs where the two IV copies match.

Reported-by: Geert Uytterhoeven <[email protected]>
Fixes: 40153b1 ("crypto: testmgr - fuzz AEADs against their generic implementation")
Cc: Stephan Mueller <[email protected]>
Originally-from: Gilad Ben-Yossef <[email protected]>
Signed-off-by: Eric Biggers <[email protected]>
Signed-off-by: Herbert Xu <[email protected]>
  • Loading branch information
ebiggers authored and herbertx committed Mar 12, 2020
1 parent d069b20 commit 6f3a06d
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions crypto/testmgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,11 @@ struct aead_test_suite {
unsigned int einval_allowed : 1;

/*
* Set if the algorithm intentionally ignores the last 8 bytes of the
* AAD buffer during decryption.
* Set if this algorithm requires that the IV be located at the end of
* the AAD buffer, in addition to being given in the normal way. The
* behavior when the two IV copies differ is implementation-defined.
*/
unsigned int esp_aad : 1;
unsigned int aad_iv : 1;
};

struct cipher_test_suite {
Expand Down Expand Up @@ -2167,9 +2168,10 @@ struct aead_extra_tests_ctx {
* here means the full ciphertext including the authentication tag. The
* authentication tag (and hence also the ciphertext) is assumed to be nonempty.
*/
static void mutate_aead_message(struct aead_testvec *vec, bool esp_aad)
static void mutate_aead_message(struct aead_testvec *vec, bool aad_iv,
unsigned int ivsize)
{
const unsigned int aad_tail_size = esp_aad ? 8 : 0;
const unsigned int aad_tail_size = aad_iv ? ivsize : 0;
const unsigned int authsize = vec->clen - vec->plen;

if (prandom_u32() % 2 == 0 && vec->alen > aad_tail_size) {
Expand Down Expand Up @@ -2207,6 +2209,9 @@ static void generate_aead_message(struct aead_request *req,

/* Generate the AAD. */
generate_random_bytes((u8 *)vec->assoc, vec->alen);
if (suite->aad_iv && vec->alen >= ivsize)
/* Avoid implementation-defined behavior. */
memcpy((u8 *)vec->assoc + vec->alen - ivsize, vec->iv, ivsize);

if (inauthentic && prandom_u32() % 2 == 0) {
/* Generate a random ciphertext. */
Expand Down Expand Up @@ -2242,7 +2247,7 @@ static void generate_aead_message(struct aead_request *req,
* Mutate the authentic (ciphertext, AAD) pair to get an
* inauthentic one.
*/
mutate_aead_message(vec, suite->esp_aad);
mutate_aead_message(vec, suite->aad_iv, ivsize);
}
vec->novrfy = 1;
if (suite->einval_allowed)
Expand Down Expand Up @@ -5202,7 +5207,7 @@ static const struct alg_test_desc alg_test_descs[] = {
.aead = {
____VECS(aes_gcm_rfc4106_tv_template),
.einval_allowed = 1,
.esp_aad = 1,
.aad_iv = 1,
}
}
}, {
Expand All @@ -5214,7 +5219,7 @@ static const struct alg_test_desc alg_test_descs[] = {
.aead = {
____VECS(aes_ccm_rfc4309_tv_template),
.einval_allowed = 1,
.esp_aad = 1,
.aad_iv = 1,
}
}
}, {
Expand All @@ -5225,6 +5230,7 @@ static const struct alg_test_desc alg_test_descs[] = {
.aead = {
____VECS(aes_gcm_rfc4543_tv_template),
.einval_allowed = 1,
.aad_iv = 1,
}
}
}, {
Expand All @@ -5240,7 +5246,7 @@ static const struct alg_test_desc alg_test_descs[] = {
.aead = {
____VECS(rfc7539esp_tv_template),
.einval_allowed = 1,
.esp_aad = 1,
.aad_iv = 1,
}
}
}, {
Expand Down

0 comments on commit 6f3a06d

Please sign in to comment.