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 <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
This commit is contained in:
Eric Biggers 2025-05-05 11:18:24 -07:00 committed by Herbert Xu
parent 607c92141c
commit bdc2a55687
12 changed files with 49 additions and 44 deletions

View File

@ -23,9 +23,9 @@ asmlinkage void chacha_4block_xor_neon(const struct chacha_state *state,
u8 *dst, const u8 *src, u8 *dst, const u8 *src,
int nrounds, unsigned int nbytes); int nrounds, unsigned int nbytes);
asmlinkage void hchacha_block_arm(const struct chacha_state *state, asmlinkage void hchacha_block_arm(const struct chacha_state *state,
u32 *out, int nrounds); u32 out[HCHACHA_OUT_WORDS], int nrounds);
asmlinkage void hchacha_block_neon(const struct chacha_state *state, asmlinkage void hchacha_block_neon(const struct chacha_state *state,
u32 *out, int nrounds); u32 out[HCHACHA_OUT_WORDS], int nrounds);
asmlinkage void chacha_doarm(u8 *dst, const u8 *src, unsigned int bytes, asmlinkage void chacha_doarm(u8 *dst, const u8 *src, unsigned int bytes,
const struct chacha_state *state, int nrounds); const struct chacha_state *state, int nrounds);
@ -64,14 +64,14 @@ static void chacha_doneon(struct chacha_state *state, u8 *dst, const u8 *src,
} }
} }
void hchacha_block_arch(const struct chacha_state *state, u32 *stream, void hchacha_block_arch(const struct chacha_state *state,
int nrounds) u32 out[HCHACHA_OUT_WORDS], int nrounds)
{ {
if (!IS_ENABLED(CONFIG_KERNEL_MODE_NEON) || !neon_usable()) { if (!IS_ENABLED(CONFIG_KERNEL_MODE_NEON) || !neon_usable()) {
hchacha_block_arm(state, stream, nrounds); hchacha_block_arm(state, out, nrounds);
} else { } else {
kernel_neon_begin(); kernel_neon_begin();
hchacha_block_neon(state, stream, nrounds); hchacha_block_neon(state, out, nrounds);
kernel_neon_end(); kernel_neon_end();
} }
} }

View File

@ -408,7 +408,7 @@ ENDPROC(chacha_doarm)
/* /*
* void hchacha_block_arm(const struct chacha_state *state, * void hchacha_block_arm(const struct chacha_state *state,
* u32 out[8], int nrounds); * u32 out[HCHACHA_OUT_WORDS], int nrounds);
*/ */
ENTRY(hchacha_block_arm) ENTRY(hchacha_block_arm)
push {r1,r4-r11,lr} push {r1,r4-r11,lr}

View File

@ -34,7 +34,7 @@ asmlinkage void chacha_4block_xor_neon(const struct chacha_state *state,
u8 *dst, const u8 *src, u8 *dst, const u8 *src,
int nrounds, int bytes); int nrounds, int bytes);
asmlinkage void hchacha_block_neon(const struct chacha_state *state, asmlinkage void hchacha_block_neon(const struct chacha_state *state,
u32 *out, int nrounds); u32 out[HCHACHA_OUT_WORDS], int nrounds);
static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_neon); static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_neon);
@ -61,14 +61,14 @@ static void chacha_doneon(struct chacha_state *state, u8 *dst, const u8 *src,
} }
} }
void hchacha_block_arch(const struct chacha_state *state, u32 *stream, void hchacha_block_arch(const struct chacha_state *state,
int nrounds) u32 out[HCHACHA_OUT_WORDS], int nrounds)
{ {
if (!static_branch_likely(&have_neon) || !crypto_simd_usable()) { if (!static_branch_likely(&have_neon) || !crypto_simd_usable()) {
hchacha_block_generic(state, stream, nrounds); hchacha_block_generic(state, out, nrounds);
} else { } else {
kernel_neon_begin(); kernel_neon_begin();
hchacha_block_neon(state, stream, nrounds); hchacha_block_neon(state, out, nrounds);
kernel_neon_end(); kernel_neon_end();
} }
} }

View File

@ -15,7 +15,7 @@ asmlinkage void chacha_crypt_arch(struct chacha_state *state,
EXPORT_SYMBOL(chacha_crypt_arch); EXPORT_SYMBOL(chacha_crypt_arch);
asmlinkage void hchacha_block_arch(const struct chacha_state *state, asmlinkage void hchacha_block_arch(const struct chacha_state *state,
u32 *stream, int nrounds); u32 out[HCHACHA_OUT_WORDS], int nrounds);
EXPORT_SYMBOL(hchacha_block_arch); EXPORT_SYMBOL(hchacha_block_arch);
bool chacha_is_arch_optimized(void) bool chacha_is_arch_optimized(void)

View File

@ -49,9 +49,9 @@ static void chacha_p10_do_8x(struct chacha_state *state, u8 *dst, const u8 *src,
} }
void hchacha_block_arch(const struct chacha_state *state, void hchacha_block_arch(const struct chacha_state *state,
u32 *stream, int nrounds) u32 out[HCHACHA_OUT_WORDS], int nrounds)
{ {
hchacha_block_generic(state, stream, nrounds); hchacha_block_generic(state, out, nrounds);
} }
EXPORT_SYMBOL(hchacha_block_arch); EXPORT_SYMBOL(hchacha_block_arch);

View File

@ -18,7 +18,8 @@ static __ro_after_init DEFINE_STATIC_KEY_FALSE(use_zvkb);
asmlinkage void chacha_zvkb(struct chacha_state *state, const u8 *in, u8 *out, asmlinkage void chacha_zvkb(struct chacha_state *state, const u8 *in, u8 *out,
size_t nblocks, int nrounds); size_t nblocks, int nrounds);
void hchacha_block_arch(const struct chacha_state *state, u32 *out, int nrounds) void hchacha_block_arch(const struct chacha_state *state,
u32 out[HCHACHA_OUT_WORDS], int nrounds)
{ {
hchacha_block_generic(state, out, nrounds); hchacha_block_generic(state, out, nrounds);
} }

View File

@ -17,10 +17,10 @@
#include "chacha-s390.h" #include "chacha-s390.h"
void hchacha_block_arch(const struct chacha_state *state, void hchacha_block_arch(const struct chacha_state *state,
u32 *stream, int nrounds) u32 out[HCHACHA_OUT_WORDS], int nrounds)
{ {
/* TODO: implement hchacha_block_arch() in assembly */ /* TODO: implement hchacha_block_arch() in assembly */
hchacha_block_generic(state, stream, nrounds); hchacha_block_generic(state, out, nrounds);
} }
EXPORT_SYMBOL(hchacha_block_arch); EXPORT_SYMBOL(hchacha_block_arch);

View File

@ -19,7 +19,7 @@ asmlinkage void chacha_4block_xor_ssse3(const struct chacha_state *state,
u8 *dst, const u8 *src, u8 *dst, const u8 *src,
unsigned int len, int nrounds); unsigned int len, int nrounds);
asmlinkage void hchacha_block_ssse3(const struct chacha_state *state, asmlinkage void hchacha_block_ssse3(const struct chacha_state *state,
u32 *out, int nrounds); u32 out[HCHACHA_OUT_WORDS], int nrounds);
asmlinkage void chacha_2block_xor_avx2(const struct chacha_state *state, asmlinkage void chacha_2block_xor_avx2(const struct chacha_state *state,
u8 *dst, const u8 *src, u8 *dst, const u8 *src,
@ -127,13 +127,13 @@ static void chacha_dosimd(struct chacha_state *state, u8 *dst, const u8 *src,
} }
void hchacha_block_arch(const struct chacha_state *state, void hchacha_block_arch(const struct chacha_state *state,
u32 *stream, int nrounds) u32 out[HCHACHA_OUT_WORDS], int nrounds)
{ {
if (!static_branch_likely(&chacha_use_simd)) { if (!static_branch_likely(&chacha_use_simd)) {
hchacha_block_generic(state, stream, nrounds); hchacha_block_generic(state, out, nrounds);
} else { } else {
kernel_fpu_begin(); kernel_fpu_begin();
hchacha_block_ssse3(state, stream, nrounds); hchacha_block_ssse3(state, out, nrounds);
kernel_fpu_end(); kernel_fpu_end();
} }
} }

View File

@ -46,8 +46,8 @@ static int chacha12_setkey(struct crypto_skcipher *tfm,
} }
static int chacha_stream_xor(struct skcipher_request *req, static int chacha_stream_xor(struct skcipher_request *req,
const struct chacha_ctx *ctx, const u8 *iv, const struct chacha_ctx *ctx,
bool arch) const u8 iv[CHACHA_IV_SIZE], bool arch)
{ {
struct skcipher_walk walk; struct skcipher_walk walk;
struct chacha_state state; struct chacha_state state;

View File

@ -26,7 +26,9 @@
#define CHACHA_BLOCK_SIZE 64 #define CHACHA_BLOCK_SIZE 64
#define CHACHAPOLY_IV_SIZE 12 #define CHACHAPOLY_IV_SIZE 12
#define CHACHA_STATE_WORDS (CHACHA_BLOCK_SIZE / sizeof(u32)) #define CHACHA_KEY_WORDS 8
#define CHACHA_STATE_WORDS 16
#define HCHACHA_OUT_WORDS 8
/* 192-bit nonce, then 64-bit stream position */ /* 192-bit nonce, then 64-bit stream position */
#define XCHACHA_IV_SIZE 32 #define XCHACHA_IV_SIZE 32
@ -35,19 +37,21 @@ struct chacha_state {
u32 x[CHACHA_STATE_WORDS]; u32 x[CHACHA_STATE_WORDS];
}; };
void chacha_block_generic(struct chacha_state *state, u8 *stream, int nrounds); void chacha_block_generic(struct chacha_state *state,
static inline void chacha20_block(struct chacha_state *state, u8 *stream) u8 out[CHACHA_BLOCK_SIZE], int nrounds);
static inline void chacha20_block(struct chacha_state *state,
u8 out[CHACHA_BLOCK_SIZE])
{ {
chacha_block_generic(state, stream, 20); chacha_block_generic(state, out, 20);
} }
void hchacha_block_arch(const struct chacha_state *state, u32 *out, void hchacha_block_arch(const struct chacha_state *state,
int nrounds); u32 out[HCHACHA_OUT_WORDS], int nrounds);
void hchacha_block_generic(const struct chacha_state *state, u32 *out, void hchacha_block_generic(const struct chacha_state *state,
int nrounds); u32 out[HCHACHA_OUT_WORDS], int nrounds);
static inline void hchacha_block(const struct chacha_state *state, u32 *out, static inline void hchacha_block(const struct chacha_state *state,
int nrounds) u32 out[HCHACHA_OUT_WORDS], int nrounds)
{ {
if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_CHACHA)) if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_CHACHA))
hchacha_block_arch(state, out, nrounds); hchacha_block_arch(state, out, nrounds);
@ -71,7 +75,8 @@ static inline void chacha_init_consts(struct chacha_state *state)
} }
static inline void chacha_init(struct chacha_state *state, static inline void chacha_init(struct chacha_state *state,
const u32 *key, const u8 *iv) const u32 key[CHACHA_KEY_WORDS],
const u8 iv[CHACHA_IV_SIZE])
{ {
chacha_init_consts(state); chacha_init_consts(state);
state->x[4] = key[0]; state->x[4] = key[0];

View File

@ -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 * chacha_block_generic - generate one keystream block and increment block counter
* @state: input state matrix * @state: input state matrix
* @stream: output keystream block (64 bytes) * @out: output keystream block
* @nrounds: number of rounds (20 or 12; 20 is recommended) * @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. * 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 * The caller has already converted the endianness of the input. This function
* also handles incrementing the block counter in the input matrix. * 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; struct chacha_state permuted_state = *state;
int i; 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++) for (i = 0; i < ARRAY_SIZE(state->x); i++)
put_unaligned_le32(permuted_state.x[i] + state->x[i], put_unaligned_le32(permuted_state.x[i] + state->x[i],
&stream[i * sizeof(u32)]); &out[i * sizeof(u32)]);
state->x[12]++; state->x[12]++;
} }
@ -92,7 +93,7 @@ EXPORT_SYMBOL(chacha_block_generic);
/** /**
* hchacha_block_generic - abbreviated ChaCha core, for XChaCha * hchacha_block_generic - abbreviated ChaCha core, for XChaCha
* @state: input state matrix * @state: input state matrix
* @stream: output (8 32-bit words) * @out: the output words
* @nrounds: number of rounds (20 or 12; 20 is recommended) * @nrounds: number of rounds (20 or 12; 20 is recommended)
* *
* HChaCha is the ChaCha equivalent of HSalsa and is an intermediate step * 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. * of the state. It should not be used for streaming directly.
*/ */
void hchacha_block_generic(const struct chacha_state *state, 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; struct chacha_state permuted_state = *state;
chacha_permute(&permuted_state, nrounds); chacha_permute(&permuted_state, nrounds);
memcpy(&stream[0], &permuted_state.x[0], 16); memcpy(&out[0], &permuted_state.x[0], 16);
memcpy(&stream[4], &permuted_state.x[12], 16); memcpy(&out[4], &permuted_state.x[12], 16);
} }
EXPORT_SYMBOL(hchacha_block_generic); EXPORT_SYMBOL(hchacha_block_generic);

View File

@ -18,8 +18,6 @@
#include <linux/mm.h> #include <linux/mm.h>
#include <linux/module.h> #include <linux/module.h>
#define CHACHA_KEY_WORDS (CHACHA_KEY_SIZE / sizeof(u32))
static void chacha_load_key(u32 *k, const u8 *in) static void chacha_load_key(u32 *k, const u8 *in)
{ {
k[0] = get_unaligned_le32(in); k[0] = get_unaligned_le32(in);