kernel: 5.4: import wireguard backport
[openwrt/openwrt.git] / target / linux / generic / backport-5.4 / 080-wireguard-0053-crypto-arm64-chacha-correctly-walk-through-blocks.patch
1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: "Jason A. Donenfeld" <Jason@zx2c4.com>
3 Date: Wed, 18 Mar 2020 20:27:32 -0600
4 Subject: [PATCH] crypto: arm64/chacha - correctly walk through blocks
5
6 commit c8cfcb78c65877313cda7bcbace624d3dbd1f3b3 upstream.
7
8 Prior, passing in chunks of 2, 3, or 4, followed by any additional
9 chunks would result in the chacha state counter getting out of sync,
10 resulting in incorrect encryption/decryption, which is a pretty nasty
11 crypto vuln: "why do images look weird on webpages?" WireGuard users
12 never experienced this prior, because we have always, out of tree, used
13 a different crypto library, until the recent Frankenzinc addition. This
14 commit fixes the issue by advancing the pointers and state counter by
15 the actual size processed. It also fixes up a bug in the (optional,
16 costly) stride test that prevented it from running on arm64.
17
18 Fixes: b3aad5bad26a ("crypto: arm64/chacha - expose arm64 ChaCha routine as library function")
19 Reported-and-tested-by: Emil Renner Berthing <kernel@esmil.dk>
20 Cc: Ard Biesheuvel <ardb@kernel.org>
21 Cc: stable@vger.kernel.org # v5.5+
22 Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
23 Reviewed-by: Eric Biggers <ebiggers@google.com>
24 Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
25 Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
26 ---
27 arch/arm64/crypto/chacha-neon-glue.c | 8 ++++----
28 lib/crypto/chacha20poly1305-selftest.c | 11 ++++++++---
29 2 files changed, 12 insertions(+), 7 deletions(-)
30
31 --- a/arch/arm64/crypto/chacha-neon-glue.c
32 +++ b/arch/arm64/crypto/chacha-neon-glue.c
33 @@ -55,10 +55,10 @@ static void chacha_doneon(u32 *state, u8
34 break;
35 }
36 chacha_4block_xor_neon(state, dst, src, nrounds, l);
37 - bytes -= CHACHA_BLOCK_SIZE * 5;
38 - src += CHACHA_BLOCK_SIZE * 5;
39 - dst += CHACHA_BLOCK_SIZE * 5;
40 - state[12] += 5;
41 + bytes -= l;
42 + src += l;
43 + dst += l;
44 + state[12] += DIV_ROUND_UP(l, CHACHA_BLOCK_SIZE);
45 }
46 }
47
48 --- a/lib/crypto/chacha20poly1305-selftest.c
49 +++ b/lib/crypto/chacha20poly1305-selftest.c
50 @@ -9028,10 +9028,15 @@ bool __init chacha20poly1305_selftest(vo
51 && total_len <= 1 << 10; ++total_len) {
52 for (i = 0; i <= total_len; ++i) {
53 for (j = i; j <= total_len; ++j) {
54 + k = 0;
55 sg_init_table(sg_src, 3);
56 - sg_set_buf(&sg_src[0], input, i);
57 - sg_set_buf(&sg_src[1], input + i, j - i);
58 - sg_set_buf(&sg_src[2], input + j, total_len - j);
59 + if (i)
60 + sg_set_buf(&sg_src[k++], input, i);
61 + if (j - i)
62 + sg_set_buf(&sg_src[k++], input + i, j - i);
63 + if (total_len - j)
64 + sg_set_buf(&sg_src[k++], input + j, total_len - j);
65 + sg_init_marker(sg_src, k);
66 memset(computed_output, 0, total_len);
67 memset(input, 0, total_len);
68