diff mbox series

[1/2] crypto/chacha20: fix handling of chunked input

Message ID 20170814132815.24524-1-ard.biesheuvel@linaro.org
State Accepted
Commit 4de437265eaac18f880d827f8e105b10de9b87a3
Headers show
Series [1/2] crypto/chacha20: fix handling of chunked input | expand

Commit Message

Ard Biesheuvel Aug. 14, 2017, 1:28 p.m. UTC
Commit 9ae433bc79f9 ("crypto: chacha20 - convert generic and x86 versions
to skcipher") ported the existing chacha20 code to use the new skcipher
API, and introduced a bug along the way. Unfortunately, the tcrypt tests
did not catch the error, and it was only found recently by Tobias.

Stefan kindly diagnosed the error, and proposed a fix which is similar
to the one below, with the exception that 'walk.stride' is used rather
than the hardcoded block size. This does not actually matter in this
case, but it's a better example of how to use the skcipher walk API.

Fixes: 9ae433bc79f9 ("crypto: chacha20 - convert generic and x86 ...")
Cc: <stable@vger.kernel.org> # v4.11+
Cc: Steffen Klassert <steffen.klassert@secunet.com>
Reported-by: Tobias Brunner <tobias@strongswan.org>
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>

---
 crypto/chacha20_generic.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

-- 
2.11.0

Comments

Herbert Xu Aug. 22, 2017, 7:58 a.m. UTC | #1
On Mon, Aug 14, 2017 at 02:28:14PM +0100, Ard Biesheuvel wrote:
> Commit 9ae433bc79f9 ("crypto: chacha20 - convert generic and x86 versions

> to skcipher") ported the existing chacha20 code to use the new skcipher

> API, and introduced a bug along the way. Unfortunately, the tcrypt tests

> did not catch the error, and it was only found recently by Tobias.

> 

> Stefan kindly diagnosed the error, and proposed a fix which is similar

> to the one below, with the exception that 'walk.stride' is used rather

> than the hardcoded block size. This does not actually matter in this

> case, but it's a better example of how to use the skcipher walk API.

> 

> Fixes: 9ae433bc79f9 ("crypto: chacha20 - convert generic and x86 ...")

> Cc: <stable@vger.kernel.org> # v4.11+

> Cc: Steffen Klassert <steffen.klassert@secunet.com>

> Reported-by: Tobias Brunner <tobias@strongswan.org>

> Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>


Patch applied.  Thanks.
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
diff mbox series

Patch

diff --git a/crypto/chacha20_generic.c b/crypto/chacha20_generic.c
index 8b3c04d625c3..4a45fa4890c0 100644
--- a/crypto/chacha20_generic.c
+++ b/crypto/chacha20_generic.c
@@ -91,9 +91,14 @@  int crypto_chacha20_crypt(struct skcipher_request *req)
 	crypto_chacha20_init(state, ctx, walk.iv);
 
 	while (walk.nbytes > 0) {
+		unsigned int nbytes = walk.nbytes;
+
+		if (nbytes < walk.total)
+			nbytes = round_down(nbytes, walk.stride);
+
 		chacha20_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr,
-				 walk.nbytes);
-		err = skcipher_walk_done(&walk, 0);
+				 nbytes);
+		err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
 	}
 
 	return err;