Skip to content

Commit

Permalink
crypto: chacha20poly1305 - Skip encryption/decryption for 0-len
Browse files Browse the repository at this point in the history
If the length of the plaintext is zero, there's no need to waste cycles
on encryption and decryption. Using the chacha20poly1305 construction
for zero-length plaintexts is a common way of using a shared encryption
key for AAD authentication.

Signed-off-by: Jason A. Donenfeld <[email protected]>
Signed-off-by: Herbert Xu <[email protected]>
  • Loading branch information
zx2c4 authored and herbertx committed Dec 9, 2015
1 parent 3d5b1ec commit 161151d
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions crypto/chacha20poly1305.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ static int chacha_decrypt(struct aead_request *req)
struct scatterlist *src, *dst;
int err;

if (rctx->cryptlen == 0)
goto skip;

chacha_iv(creq->iv, req, 1);

sg_init_table(rctx->src, 2);
Expand All @@ -150,6 +153,7 @@ static int chacha_decrypt(struct aead_request *req)
if (err)
return err;

skip:
return poly_verify_tag(req);
}

Expand Down Expand Up @@ -415,6 +419,9 @@ static int chacha_encrypt(struct aead_request *req)
struct scatterlist *src, *dst;
int err;

if (req->cryptlen == 0)
goto skip;

chacha_iv(creq->iv, req, 1);

sg_init_table(rctx->src, 2);
Expand All @@ -435,6 +442,7 @@ static int chacha_encrypt(struct aead_request *req)
if (err)
return err;

skip:
return poly_genkey(req);
}

Expand Down

0 comments on commit 161151d

Please sign in to comment.