From bdc2a55687f123bd32aaefb81e11c7450a431eaf Mon Sep 17 00:00:00 2001 From: Eric Biggers Date: Mon, 5 May 2025 11:18:24 -0700 Subject: crypto: lib/chacha - add array bounds to function prototypes Add explicit array bounds to the function prototypes for the parameters that didn't already get handled by the conversion to use chacha_state: - chacha_block_*(): Change 'u8 *out' or 'u8 *stream' to u8 out[CHACHA_BLOCK_SIZE]. - hchacha_block_*(): Change 'u32 *out' or 'u32 *stream' to u32 out[HCHACHA_OUT_WORDS]. - chacha_init(): Change 'const u32 *key' to 'const u32 key[CHACHA_KEY_WORDS]'. Change 'const u8 *iv' to 'const u8 iv[CHACHA_IV_SIZE]'. No functional changes. This just makes it clear when fixed-size arrays are expected. Signed-off-by: Eric Biggers Signed-off-by: Herbert Xu --- lib/crypto/chacha.c | 15 ++++++++------- lib/crypto/chacha20poly1305.c | 2 -- 2 files changed, 8 insertions(+), 9 deletions(-) (limited to 'lib') diff --git a/lib/crypto/chacha.c b/lib/crypto/chacha.c index ae50e441f9fb..ced87dd31a97 100644 --- a/lib/crypto/chacha.c +++ b/lib/crypto/chacha.c @@ -67,14 +67,15 @@ static void chacha_permute(struct chacha_state *state, int nrounds) /** * chacha_block_generic - generate one keystream block and increment block counter * @state: input state matrix - * @stream: output keystream block (64 bytes) + * @out: output keystream block * @nrounds: number of rounds (20 or 12; 20 is recommended) * * This is the ChaCha core, a function from 64-byte strings to 64-byte strings. * The caller has already converted the endianness of the input. This function * also handles incrementing the block counter in the input matrix. */ -void chacha_block_generic(struct chacha_state *state, u8 *stream, int nrounds) +void chacha_block_generic(struct chacha_state *state, + u8 out[CHACHA_BLOCK_SIZE], int nrounds) { struct chacha_state permuted_state = *state; int i; @@ -83,7 +84,7 @@ void chacha_block_generic(struct chacha_state *state, u8 *stream, int nrounds) for (i = 0; i < ARRAY_SIZE(state->x); i++) put_unaligned_le32(permuted_state.x[i] + state->x[i], - &stream[i * sizeof(u32)]); + &out[i * sizeof(u32)]); state->x[12]++; } @@ -92,7 +93,7 @@ EXPORT_SYMBOL(chacha_block_generic); /** * hchacha_block_generic - abbreviated ChaCha core, for XChaCha * @state: input state matrix - * @stream: output (8 32-bit words) + * @out: the output words * @nrounds: number of rounds (20 or 12; 20 is recommended) * * HChaCha is the ChaCha equivalent of HSalsa and is an intermediate step @@ -101,13 +102,13 @@ EXPORT_SYMBOL(chacha_block_generic); * of the state. It should not be used for streaming directly. */ void hchacha_block_generic(const struct chacha_state *state, - u32 *stream, int nrounds) + u32 out[HCHACHA_OUT_WORDS], int nrounds) { struct chacha_state permuted_state = *state; chacha_permute(&permuted_state, nrounds); - memcpy(&stream[0], &permuted_state.x[0], 16); - memcpy(&stream[4], &permuted_state.x[12], 16); + memcpy(&out[0], &permuted_state.x[0], 16); + memcpy(&out[4], &permuted_state.x[12], 16); } EXPORT_SYMBOL(hchacha_block_generic); diff --git a/lib/crypto/chacha20poly1305.c b/lib/crypto/chacha20poly1305.c index 2e7bbc1a67ea..fbd3690e2531 100644 --- a/lib/crypto/chacha20poly1305.c +++ b/lib/crypto/chacha20poly1305.c @@ -18,8 +18,6 @@ #include #include -#define CHACHA_KEY_WORDS (CHACHA_KEY_SIZE / sizeof(u32)) - static void chacha_load_key(u32 *k, const u8 *in) { k[0] = get_unaligned_le32(in); -- cgit v1.2.3-59-g8ed1b