mirror of
https://github.com/torvalds/linux.git
synced 2025-12-07 20:06:24 +00:00
Several parameters of the chacha20poly1305 functions require arrays of
an exact length. Use the new at_least keyword to instruct gcc and
clang to statically check that the caller is passing an object of at
least that length.
Here it is in action, with this faulty patch to wireguard's cookie.h:
struct cookie_checker {
u8 secret[NOISE_HASH_LEN];
- u8 cookie_encryption_key[NOISE_SYMMETRIC_KEY_LEN];
+ u8 cookie_encryption_key[NOISE_SYMMETRIC_KEY_LEN - 1];
u8 message_mac1_key[NOISE_SYMMETRIC_KEY_LEN];
If I try compiling this code, I get this helpful warning:
CC drivers/net/wireguard/cookie.o
drivers/net/wireguard/cookie.c: In function ‘wg_cookie_message_create’:
drivers/net/wireguard/cookie.c:193:9: warning: ‘xchacha20poly1305_encrypt’ reading 32 bytes from a region of size 31 [-Wstringop-overread]
193 | xchacha20poly1305_encrypt(dst->encrypted_cookie, cookie, COOKIE_LEN,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
194 | macs->mac1, COOKIE_LEN, dst->nonce,
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
195 | checker->cookie_encryption_key);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/net/wireguard/cookie.c:193:9: note: referencing argument 7 of type ‘const u8 *’ {aka ‘const unsigned char *’}
In file included from drivers/net/wireguard/messages.h:10,
from drivers/net/wireguard/cookie.h:9,
from drivers/net/wireguard/cookie.c:6:
include/crypto/chacha20poly1305.h:28:6: note: in a call to function ‘xchacha20poly1305_encrypt’
28 | void xchacha20poly1305_encrypt(u8 *dst, const u8 *src, const size_t src_len,
Acked-by: Ard Biesheuvel <ardb@kernel.org>
Signed-off-by: "Jason A. Donenfeld" <Jason@zx2c4.com>
Link: https://lore.kernel.org/r/20251123054819.2371989-4-Jason@zx2c4.com
Signed-off-by: Eric Biggers <ebiggers@kernel.org>
52 lines
1.7 KiB
C
52 lines
1.7 KiB
C
/* SPDX-License-Identifier: GPL-2.0 OR MIT */
|
|
/*
|
|
* Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
|
*/
|
|
|
|
#ifndef __CHACHA20POLY1305_H
|
|
#define __CHACHA20POLY1305_H
|
|
|
|
#include <linux/types.h>
|
|
#include <linux/scatterlist.h>
|
|
|
|
enum chacha20poly1305_lengths {
|
|
XCHACHA20POLY1305_NONCE_SIZE = 24,
|
|
CHACHA20POLY1305_KEY_SIZE = 32,
|
|
CHACHA20POLY1305_AUTHTAG_SIZE = 16
|
|
};
|
|
|
|
void chacha20poly1305_encrypt(u8 *dst, const u8 *src, const size_t src_len,
|
|
const u8 *ad, const size_t ad_len,
|
|
const u64 nonce,
|
|
const u8 key[at_least CHACHA20POLY1305_KEY_SIZE]);
|
|
|
|
bool __must_check
|
|
chacha20poly1305_decrypt(u8 *dst, const u8 *src, const size_t src_len,
|
|
const u8 *ad, const size_t ad_len, const u64 nonce,
|
|
const u8 key[at_least CHACHA20POLY1305_KEY_SIZE]);
|
|
|
|
void xchacha20poly1305_encrypt(u8 *dst, const u8 *src, const size_t src_len,
|
|
const u8 *ad, const size_t ad_len,
|
|
const u8 nonce[at_least XCHACHA20POLY1305_NONCE_SIZE],
|
|
const u8 key[at_least CHACHA20POLY1305_KEY_SIZE]);
|
|
|
|
bool __must_check xchacha20poly1305_decrypt(
|
|
u8 *dst, const u8 *src, const size_t src_len,
|
|
const u8 *ad, const size_t ad_len,
|
|
const u8 nonce[at_least XCHACHA20POLY1305_NONCE_SIZE],
|
|
const u8 key[at_least CHACHA20POLY1305_KEY_SIZE]);
|
|
|
|
bool chacha20poly1305_encrypt_sg_inplace(struct scatterlist *src, size_t src_len,
|
|
const u8 *ad, const size_t ad_len,
|
|
const u64 nonce,
|
|
const u8 key[at_least CHACHA20POLY1305_KEY_SIZE]);
|
|
|
|
bool chacha20poly1305_decrypt_sg_inplace(struct scatterlist *src, size_t src_len,
|
|
const u8 *ad, const size_t ad_len,
|
|
const u64 nonce,
|
|
const u8 key[at_least CHACHA20POLY1305_KEY_SIZE]);
|
|
|
|
bool chacha20poly1305_selftest(void);
|
|
|
|
#endif /* __CHACHA20POLY1305_H */
|