]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
lib/crypto: chacha: Zeroize permuted_state before it leaves scope
authorEric Biggers <ebiggers@kernel.org>
Thu, 26 Mar 2026 03:29:20 +0000 (20:29 -0700)
committerEric Biggers <ebiggers@kernel.org>
Fri, 27 Mar 2026 20:35:35 +0000 (13:35 -0700)
Since the ChaCha permutation is invertible, the local variable
'permuted_state' is sufficient to compute the original 'state', and thus
the key, even after the permutation has been done.

While the kernel is quite inconsistent about zeroizing secrets on the
stack (and some prominent userspace crypto libraries don't bother at all
since it's not guaranteed to work anyway), the kernel does try to do it
as a best practice, especially in cases involving the RNG.

Thus, explicitly zeroize 'permuted_state' before it goes out of scope.

Fixes: c08d0e647305 ("crypto: chacha20 - Add a generic ChaCha20 stream cipher implementation")
Cc: stable@vger.kernel.org
Acked-by: Ard Biesheuvel <ardb@kernel.org>
Link: https://lore.kernel.org/r/20260326032920.39408-1-ebiggers@kernel.org
Signed-off-by: Eric Biggers <ebiggers@kernel.org>
lib/crypto/chacha-block-generic.c

index 77f68de71066a9fc952ba96e8ed9304ef0887d1f..4a6d627580cb629491ddcf59eaf001f6161c904d 100644 (file)
@@ -87,6 +87,8 @@ void chacha_block_generic(struct chacha_state *state,
                                   &out[i * sizeof(u32)]);
 
        state->x[12]++;
+
+       chacha_zeroize_state(&permuted_state);
 }
 EXPORT_SYMBOL(chacha_block_generic);
 
@@ -110,5 +112,7 @@ void hchacha_block_generic(const struct chacha_state *state,
 
        memcpy(&out[0], &permuted_state.x[0], 16);
        memcpy(&out[4], &permuted_state.x[12], 16);
+
+       chacha_zeroize_state(&permuted_state);
 }
 EXPORT_SYMBOL(hchacha_block_generic);