cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

random.h (4319B)


      1/* SPDX-License-Identifier: GPL-2.0 */
      2
      3#ifndef _LINUX_RANDOM_H
      4#define _LINUX_RANDOM_H
      5
      6#include <linux/bug.h>
      7#include <linux/kernel.h>
      8#include <linux/list.h>
      9#include <linux/once.h>
     10
     11#include <uapi/linux/random.h>
     12
     13struct notifier_block;
     14
     15void add_device_randomness(const void *buf, size_t len);
     16void __init add_bootloader_randomness(const void *buf, size_t len);
     17void add_input_randomness(unsigned int type, unsigned int code,
     18			  unsigned int value) __latent_entropy;
     19void add_interrupt_randomness(int irq) __latent_entropy;
     20void add_hwgenerator_randomness(const void *buf, size_t len, size_t entropy);
     21
     22#if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__)
     23static inline void add_latent_entropy(void)
     24{
     25	add_device_randomness((const void *)&latent_entropy, sizeof(latent_entropy));
     26}
     27#else
     28static inline void add_latent_entropy(void) { }
     29#endif
     30
     31#if IS_ENABLED(CONFIG_VMGENID)
     32void add_vmfork_randomness(const void *unique_vm_id, size_t len);
     33int register_random_vmfork_notifier(struct notifier_block *nb);
     34int unregister_random_vmfork_notifier(struct notifier_block *nb);
     35#else
     36static inline int register_random_vmfork_notifier(struct notifier_block *nb) { return 0; }
     37static inline int unregister_random_vmfork_notifier(struct notifier_block *nb) { return 0; }
     38#endif
     39
     40void get_random_bytes(void *buf, size_t len);
     41u32 get_random_u32(void);
     42u64 get_random_u64(void);
     43static inline unsigned int get_random_int(void)
     44{
     45	return get_random_u32();
     46}
     47static inline unsigned long get_random_long(void)
     48{
     49#if BITS_PER_LONG == 64
     50	return get_random_u64();
     51#else
     52	return get_random_u32();
     53#endif
     54}
     55
     56/*
     57 * On 64-bit architectures, protect against non-terminated C string overflows
     58 * by zeroing out the first byte of the canary; this leaves 56 bits of entropy.
     59 */
     60#ifdef CONFIG_64BIT
     61# ifdef __LITTLE_ENDIAN
     62#  define CANARY_MASK 0xffffffffffffff00UL
     63# else /* big endian, 64 bits: */
     64#  define CANARY_MASK 0x00ffffffffffffffUL
     65# endif
     66#else /* 32 bits: */
     67# define CANARY_MASK 0xffffffffUL
     68#endif
     69
     70static inline unsigned long get_random_canary(void)
     71{
     72	return get_random_long() & CANARY_MASK;
     73}
     74
     75int __init random_init(const char *command_line);
     76bool rng_is_initialized(void);
     77int wait_for_random_bytes(void);
     78
     79/* Calls wait_for_random_bytes() and then calls get_random_bytes(buf, nbytes).
     80 * Returns the result of the call to wait_for_random_bytes. */
     81static inline int get_random_bytes_wait(void *buf, size_t nbytes)
     82{
     83	int ret = wait_for_random_bytes();
     84	get_random_bytes(buf, nbytes);
     85	return ret;
     86}
     87
     88#define declare_get_random_var_wait(name, ret_type) \
     89	static inline int get_random_ ## name ## _wait(ret_type *out) { \
     90		int ret = wait_for_random_bytes(); \
     91		if (unlikely(ret)) \
     92			return ret; \
     93		*out = get_random_ ## name(); \
     94		return 0; \
     95	}
     96declare_get_random_var_wait(u32, u32)
     97declare_get_random_var_wait(u64, u32)
     98declare_get_random_var_wait(int, unsigned int)
     99declare_get_random_var_wait(long, unsigned long)
    100#undef declare_get_random_var
    101
    102/*
    103 * This is designed to be standalone for just prandom
    104 * users, but for now we include it from <linux/random.h>
    105 * for legacy reasons.
    106 */
    107#include <linux/prandom.h>
    108
    109#ifdef CONFIG_ARCH_RANDOM
    110# include <asm/archrandom.h>
    111#else
    112static inline bool __must_check arch_get_random_long(unsigned long *v) { return false; }
    113static inline bool __must_check arch_get_random_int(unsigned int *v) { return false; }
    114static inline bool __must_check arch_get_random_seed_long(unsigned long *v) { return false; }
    115static inline bool __must_check arch_get_random_seed_int(unsigned int *v) { return false; }
    116#endif
    117
    118/*
    119 * Called from the boot CPU during startup; not valid to call once
    120 * secondary CPUs are up and preemption is possible.
    121 */
    122#ifndef arch_get_random_seed_long_early
    123static inline bool __init arch_get_random_seed_long_early(unsigned long *v)
    124{
    125	WARN_ON(system_state != SYSTEM_BOOTING);
    126	return arch_get_random_seed_long(v);
    127}
    128#endif
    129
    130#ifndef arch_get_random_long_early
    131static inline bool __init arch_get_random_long_early(unsigned long *v)
    132{
    133	WARN_ON(system_state != SYSTEM_BOOTING);
    134	return arch_get_random_long(v);
    135}
    136#endif
    137
    138#ifdef CONFIG_SMP
    139int random_prepare_cpu(unsigned int cpu);
    140int random_online_cpu(unsigned int cpu);
    141#endif
    142
    143#ifndef MODULE
    144extern const struct file_operations random_fops, urandom_fops;
    145#endif
    146
    147#endif /* _LINUX_RANDOM_H */