/* SPDX-License-Identifier: GPL-2.0 */1/*2* Copyright (C) 2022-2024 Jason A. Donenfeld <[email protected]>. All Rights Reserved.3*/45#ifndef _VDSO_GETRANDOM_H6#define _VDSO_GETRANDOM_H78#include <linux/types.h>910#define CHACHA_KEY_SIZE 3211#define CHACHA_BLOCK_SIZE 641213/**14* struct vgetrandom_state - State used by vDSO getrandom().15*16* @batch: One and a half ChaCha20 blocks of buffered RNG output.17*18* @key: Key to be used for generating next batch.19*20* @batch_key: Union of the prior two members, which is exactly two full21* ChaCha20 blocks in size, so that @batch and @key can be filled22* together.23*24* @generation: Snapshot of @rng_info->generation in the vDSO data page at25* the time @key was generated.26*27* @pos: Offset into @batch of the next available random byte.28*29* @in_use: Reentrancy guard for reusing a state within the same thread30* due to signal handlers.31*/32struct vgetrandom_state {33union {34struct {35u8 batch[CHACHA_BLOCK_SIZE * 3 / 2];36u32 key[CHACHA_KEY_SIZE / sizeof(u32)];37};38u8 batch_key[CHACHA_BLOCK_SIZE * 2];39};40u64 generation;41u8 pos;42bool in_use;43};4445/**46* __arch_chacha20_blocks_nostack - Generate ChaCha20 stream without using the stack.47* @dst_bytes: Destination buffer to hold @nblocks * 64 bytes of output.48* @key: 32-byte input key.49* @counter: 8-byte counter, read on input and updated on return.50* @nblocks: Number of blocks to generate.51*52* Generates a given positive number of blocks of ChaCha20 output with nonce=0, and does not write53* to any stack or memory outside of the parameters passed to it, in order to mitigate stack data54* leaking into forked child processes.55*/56extern void __arch_chacha20_blocks_nostack(u8 *dst_bytes, const u32 *key, u32 *counter, size_t nblocks);5758/**59* __vdso_getrandom - Architecture-specific vDSO implementation of getrandom() syscall.60* @buffer: Passed to __cvdso_getrandom().61* @len: Passed to __cvdso_getrandom().62* @flags: Passed to __cvdso_getrandom().63* @opaque_state: Passed to __cvdso_getrandom().64* @opaque_len: Passed to __cvdso_getrandom();65*66* This function is implemented by making a single call to to __cvdso_getrandom(), whose67* documentation may be consulted for more information.68*69* Returns: The return value of __cvdso_getrandom().70*/71extern ssize_t __vdso_getrandom(void *buffer, size_t len, unsigned int flags, void *opaque_state, size_t opaque_len);7273#endif /* _VDSO_GETRANDOM_H */747576