xref: /linux/drivers/char/random.c (revision 748bc4dd9e663f23448d8ad7e58c011a67ea1eca)
1a07fdae3SJason A. Donenfeld // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
21da177e4SLinus Torvalds /*
39f9eff85SJason A. Donenfeld  * Copyright (C) 2017-2022 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
49e95ce27SMatt Mackall  * Copyright Matt Mackall <mpm@selenic.com>, 2003, 2004, 2005
55f75d9f3SJason A. Donenfeld  * Copyright Theodore Ts'o, 1994, 1995, 1996, 1997, 1998, 1999. All rights reserved.
61da177e4SLinus Torvalds  *
75f75d9f3SJason A. Donenfeld  * This driver produces cryptographically secure pseudorandom data. It is divided
85f75d9f3SJason A. Donenfeld  * into roughly six sections, each with a section header:
91da177e4SLinus Torvalds  *
105f75d9f3SJason A. Donenfeld  *   - Initialization and readiness waiting.
115f75d9f3SJason A. Donenfeld  *   - Fast key erasure RNG, the "crng".
125f75d9f3SJason A. Donenfeld  *   - Entropy accumulation and extraction routines.
135f75d9f3SJason A. Donenfeld  *   - Entropy collection routines.
145f75d9f3SJason A. Donenfeld  *   - Userspace reader/writer interfaces.
155f75d9f3SJason A. Donenfeld  *   - Sysctl interface.
161da177e4SLinus Torvalds  *
175f75d9f3SJason A. Donenfeld  * The high level overview is that there is one input pool, into which
18e85c0fc1SJason A. Donenfeld  * various pieces of data are hashed. Prior to initialization, some of that
19e85c0fc1SJason A. Donenfeld  * data is then "credited" as having a certain number of bits of entropy.
20e85c0fc1SJason A. Donenfeld  * When enough bits of entropy are available, the hash is finalized and
21e85c0fc1SJason A. Donenfeld  * handed as a key to a stream cipher that expands it indefinitely for
22e85c0fc1SJason A. Donenfeld  * various consumers. This key is periodically refreshed as the various
23e85c0fc1SJason A. Donenfeld  * entropy collectors, described below, add data to the input pool.
241da177e4SLinus Torvalds  */
251da177e4SLinus Torvalds 
2612cd53afSYangtao Li #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2712cd53afSYangtao Li 
281da177e4SLinus Torvalds #include <linux/utsname.h>
291da177e4SLinus Torvalds #include <linux/module.h>
301da177e4SLinus Torvalds #include <linux/kernel.h>
311da177e4SLinus Torvalds #include <linux/major.h>
321da177e4SLinus Torvalds #include <linux/string.h>
331da177e4SLinus Torvalds #include <linux/fcntl.h>
341da177e4SLinus Torvalds #include <linux/slab.h>
351da177e4SLinus Torvalds #include <linux/random.h>
361da177e4SLinus Torvalds #include <linux/poll.h>
371da177e4SLinus Torvalds #include <linux/init.h>
381da177e4SLinus Torvalds #include <linux/fs.h>
39322cbb50SChristoph Hellwig #include <linux/blkdev.h>
401da177e4SLinus Torvalds #include <linux/interrupt.h>
4127ac792cSAndrea Righi #include <linux/mm.h>
42dd0f0cf5SMichael Ellerman #include <linux/nodemask.h>
431da177e4SLinus Torvalds #include <linux/spinlock.h>
44c84dbf61STorsten Duwe #include <linux/kthread.h>
451da177e4SLinus Torvalds #include <linux/percpu.h>
46775f4b29STheodore Ts'o #include <linux/ptrace.h>
476265e169STheodore Ts'o #include <linux/workqueue.h>
48d178a1ebSYinghai Lu #include <linux/irq.h>
494e00b339STheodore Ts'o #include <linux/ratelimit.h>
50c6e9d6f3STheodore Ts'o #include <linux/syscalls.h>
51c6e9d6f3STheodore Ts'o #include <linux/completion.h>
528da4b8c4SAndy Shevchenko #include <linux/uuid.h>
5387e7d5abSJason A. Donenfeld #include <linux/uaccess.h>
54b7b67d13SJason A. Donenfeld #include <linux/suspend.h>
55e73aaae2SJason A. Donenfeld #include <linux/siphash.h>
561ca1b917SEric Biggers #include <crypto/chacha.h>
579f9eff85SJason A. Donenfeld #include <crypto/blake2s.h>
581da177e4SLinus Torvalds #include <asm/processor.h>
591da177e4SLinus Torvalds #include <asm/irq.h>
60775f4b29STheodore Ts'o #include <asm/irq_regs.h>
611da177e4SLinus Torvalds #include <asm/io.h>
621da177e4SLinus Torvalds 
635f1bb112SJason A. Donenfeld /*********************************************************************
645f1bb112SJason A. Donenfeld  *
655f1bb112SJason A. Donenfeld  * Initialization and readiness waiting.
665f1bb112SJason A. Donenfeld  *
675f1bb112SJason A. Donenfeld  * Much of the RNG infrastructure is devoted to various dependencies
685f1bb112SJason A. Donenfeld  * being able to wait until the RNG has collected enough entropy and
695f1bb112SJason A. Donenfeld  * is ready for safe consumption.
705f1bb112SJason A. Donenfeld  *
715f1bb112SJason A. Donenfeld  *********************************************************************/
725f1bb112SJason A. Donenfeld 
735f1bb112SJason A. Donenfeld /*
745f1bb112SJason A. Donenfeld  * crng_init is protected by base_crng->lock, and only increases
75e3d2c5e7SJason A. Donenfeld  * its value (from empty->early->ready).
765f1bb112SJason A. Donenfeld  */
77e3d2c5e7SJason A. Donenfeld static enum {
78e3d2c5e7SJason A. Donenfeld 	CRNG_EMPTY = 0, /* Little to no entropy collected */
79e3d2c5e7SJason A. Donenfeld 	CRNG_EARLY = 1, /* At least POOL_EARLY_BITS collected */
80e3d2c5e7SJason A. Donenfeld 	CRNG_READY = 2  /* Fully initialized with POOL_READY_BITS collected */
81f5bda35fSJason A. Donenfeld } crng_init __read_mostly = CRNG_EMPTY;
82f5bda35fSJason A. Donenfeld static DEFINE_STATIC_KEY_FALSE(crng_is_ready);
83f5bda35fSJason A. Donenfeld #define crng_ready() (static_branch_likely(&crng_is_ready) || crng_init >= CRNG_READY)
84e3d2c5e7SJason A. Donenfeld /* Various types of waiters for crng_init->CRNG_READY transition. */
855f1bb112SJason A. Donenfeld static DECLARE_WAIT_QUEUE_HEAD(crng_init_wait);
865f1bb112SJason A. Donenfeld static struct fasync_struct *fasync;
875f1bb112SJason A. Donenfeld 
885f1bb112SJason A. Donenfeld /* Control how we warn userspace. */
890313bc27SLinus Torvalds static struct ratelimit_state urandom_warning =
90c01d4d0aSJason A. Donenfeld 	RATELIMIT_STATE_INIT_FLAGS("urandom_warning", HZ, 3, RATELIMIT_MSG_ON_RELEASE);
91cc1e127bSJason A. Donenfeld static int ratelimit_disable __read_mostly =
92cc1e127bSJason A. Donenfeld 	IS_ENABLED(CONFIG_WARN_ALL_UNSEEDED_RANDOM);
935f1bb112SJason A. Donenfeld module_param_named(ratelimit_disable, ratelimit_disable, int, 0644);
945f1bb112SJason A. Donenfeld MODULE_PARM_DESC(ratelimit_disable, "Disable random ratelimit suppression");
955f1bb112SJason A. Donenfeld 
965f1bb112SJason A. Donenfeld /*
975f1bb112SJason A. Donenfeld  * Returns whether or not the input pool has been seeded and thus guaranteed
980313bc27SLinus Torvalds  * to supply cryptographically secure random numbers. This applies to: the
990313bc27SLinus Torvalds  * /dev/urandom device, the get_random_bytes function, and the get_random_{u32,
1000313bc27SLinus Torvalds  * ,u64,int,long} family of functions.
1015f1bb112SJason A. Donenfeld  *
1025f1bb112SJason A. Donenfeld  * Returns: true if the input pool has been seeded.
1035f1bb112SJason A. Donenfeld  *          false if the input pool has not been seeded.
1045f1bb112SJason A. Donenfeld  */
1055f1bb112SJason A. Donenfeld bool rng_is_initialized(void)
1065f1bb112SJason A. Donenfeld {
1075f1bb112SJason A. Donenfeld 	return crng_ready();
1085f1bb112SJason A. Donenfeld }
1095f1bb112SJason A. Donenfeld EXPORT_SYMBOL(rng_is_initialized);
1105f1bb112SJason A. Donenfeld 
111560181c2SJason A. Donenfeld static void __cold crng_set_ready(struct work_struct *work)
112f5bda35fSJason A. Donenfeld {
113f5bda35fSJason A. Donenfeld 	static_branch_enable(&crng_is_ready);
114f5bda35fSJason A. Donenfeld }
115f5bda35fSJason A. Donenfeld 
1165f1bb112SJason A. Donenfeld /* Used by wait_for_random_bytes(), and considered an entropy collector, below. */
1175f1bb112SJason A. Donenfeld static void try_to_generate_entropy(void);
1185f1bb112SJason A. Donenfeld 
1195f1bb112SJason A. Donenfeld /*
1205f1bb112SJason A. Donenfeld  * Wait for the input pool to be seeded and thus guaranteed to supply
1210313bc27SLinus Torvalds  * cryptographically secure random numbers. This applies to: the /dev/urandom
1220313bc27SLinus Torvalds  * device, the get_random_bytes function, and the get_random_{u32,u64,int,long}
1230313bc27SLinus Torvalds  * family of functions. Using any of these functions without first calling
1240313bc27SLinus Torvalds  * this function forfeits the guarantee of security.
1255f1bb112SJason A. Donenfeld  *
1265f1bb112SJason A. Donenfeld  * Returns: 0 if the input pool has been seeded.
1275f1bb112SJason A. Donenfeld  *          -ERESTARTSYS if the function was interrupted by a signal.
1285f1bb112SJason A. Donenfeld  */
1295f1bb112SJason A. Donenfeld int wait_for_random_bytes(void)
1305f1bb112SJason A. Donenfeld {
131a96cfe2dSJason A. Donenfeld 	while (!crng_ready()) {
1325f1bb112SJason A. Donenfeld 		int ret;
1333e504d20SJason A. Donenfeld 
1343e504d20SJason A. Donenfeld 		try_to_generate_entropy();
1355f1bb112SJason A. Donenfeld 		ret = wait_event_interruptible_timeout(crng_init_wait, crng_ready(), HZ);
1365f1bb112SJason A. Donenfeld 		if (ret)
1375f1bb112SJason A. Donenfeld 			return ret > 0 ? 0 : ret;
138a96cfe2dSJason A. Donenfeld 	}
1395f1bb112SJason A. Donenfeld 	return 0;
1405f1bb112SJason A. Donenfeld }
1415f1bb112SJason A. Donenfeld EXPORT_SYMBOL(wait_for_random_bytes);
1425f1bb112SJason A. Donenfeld 
143cc1e127bSJason A. Donenfeld #define warn_unseeded_randomness() \
144560181c2SJason A. Donenfeld 	if (IS_ENABLED(CONFIG_WARN_ALL_UNSEEDED_RANDOM) && !crng_ready()) \
145560181c2SJason A. Donenfeld 		printk_deferred(KERN_NOTICE "random: %s called from %pS with crng_init=%d\n", \
146560181c2SJason A. Donenfeld 				__func__, (void *)_RET_IP_, crng_init)
1475f1bb112SJason A. Donenfeld 
1485f1bb112SJason A. Donenfeld 
1493655adc7SJason A. Donenfeld /*********************************************************************
1503655adc7SJason A. Donenfeld  *
1513655adc7SJason A. Donenfeld  * Fast key erasure RNG, the "crng".
1523655adc7SJason A. Donenfeld  *
1533655adc7SJason A. Donenfeld  * These functions expand entropy from the entropy extractor into
1543655adc7SJason A. Donenfeld  * long streams for external consumption using the "fast key erasure"
1553655adc7SJason A. Donenfeld  * RNG described at <https://blog.cr.yp.to/20170723-random.html>.
1563655adc7SJason A. Donenfeld  *
1573655adc7SJason A. Donenfeld  * There are a few exported interfaces for use by other drivers:
1583655adc7SJason A. Donenfeld  *
159a1940263SJason A. Donenfeld  *	void get_random_bytes(void *buf, size_t len)
1603655adc7SJason A. Donenfeld  *	u32 get_random_u32()
1613655adc7SJason A. Donenfeld  *	u64 get_random_u64()
1623655adc7SJason A. Donenfeld  *	unsigned int get_random_int()
1633655adc7SJason A. Donenfeld  *	unsigned long get_random_long()
1643655adc7SJason A. Donenfeld  *
1653655adc7SJason A. Donenfeld  * These interfaces will return the requested number of random bytes
1660313bc27SLinus Torvalds  * into the given buffer or as a return value. This is equivalent to
167dd7aa36eSJason A. Donenfeld  * a read from /dev/urandom. The u32, u64, int, and long family of
168dd7aa36eSJason A. Donenfeld  * functions may be higher performance for one-off random integers,
169dd7aa36eSJason A. Donenfeld  * because they do a bit of buffering and do not invoke reseeding
170dd7aa36eSJason A. Donenfeld  * until the buffer is emptied.
1713655adc7SJason A. Donenfeld  *
1723655adc7SJason A. Donenfeld  *********************************************************************/
1733655adc7SJason A. Donenfeld 
174e85c0fc1SJason A. Donenfeld enum {
175e85c0fc1SJason A. Donenfeld 	CRNG_RESEED_START_INTERVAL = HZ,
176e85c0fc1SJason A. Donenfeld 	CRNG_RESEED_INTERVAL = 60 * HZ
177e85c0fc1SJason A. Donenfeld };
1783655adc7SJason A. Donenfeld 
1793655adc7SJason A. Donenfeld static struct {
1803655adc7SJason A. Donenfeld 	u8 key[CHACHA_KEY_SIZE] __aligned(__alignof__(long));
1813655adc7SJason A. Donenfeld 	unsigned long birth;
1823655adc7SJason A. Donenfeld 	unsigned long generation;
1833655adc7SJason A. Donenfeld 	spinlock_t lock;
1843655adc7SJason A. Donenfeld } base_crng = {
1853655adc7SJason A. Donenfeld 	.lock = __SPIN_LOCK_UNLOCKED(base_crng.lock)
1863655adc7SJason A. Donenfeld };
1873655adc7SJason A. Donenfeld 
1883655adc7SJason A. Donenfeld struct crng {
1893655adc7SJason A. Donenfeld 	u8 key[CHACHA_KEY_SIZE];
1903655adc7SJason A. Donenfeld 	unsigned long generation;
1913655adc7SJason A. Donenfeld 	local_lock_t lock;
1923655adc7SJason A. Donenfeld };
1933655adc7SJason A. Donenfeld 
1943655adc7SJason A. Donenfeld static DEFINE_PER_CPU(struct crng, crngs) = {
1953655adc7SJason A. Donenfeld 	.generation = ULONG_MAX,
1963655adc7SJason A. Donenfeld 	.lock = INIT_LOCAL_LOCK(crngs.lock),
1973655adc7SJason A. Donenfeld };
1983655adc7SJason A. Donenfeld 
199e85c0fc1SJason A. Donenfeld /* Used by crng_reseed() and crng_make_state() to extract a new seed from the input pool. */
200a1940263SJason A. Donenfeld static void extract_entropy(void *buf, size_t len);
2013655adc7SJason A. Donenfeld 
202e85c0fc1SJason A. Donenfeld /* This extracts a new crng key from the input pool. */
203e85c0fc1SJason A. Donenfeld static void crng_reseed(void)
2043655adc7SJason A. Donenfeld {
2053655adc7SJason A. Donenfeld 	unsigned long flags;
2063655adc7SJason A. Donenfeld 	unsigned long next_gen;
2073655adc7SJason A. Donenfeld 	u8 key[CHACHA_KEY_SIZE];
2083655adc7SJason A. Donenfeld 
209e85c0fc1SJason A. Donenfeld 	extract_entropy(key, sizeof(key));
2103655adc7SJason A. Donenfeld 
2113655adc7SJason A. Donenfeld 	/*
2123655adc7SJason A. Donenfeld 	 * We copy the new key into the base_crng, overwriting the old one,
2133655adc7SJason A. Donenfeld 	 * and update the generation counter. We avoid hitting ULONG_MAX,
2143655adc7SJason A. Donenfeld 	 * because the per-cpu crngs are initialized to ULONG_MAX, so this
2153655adc7SJason A. Donenfeld 	 * forces new CPUs that come online to always initialize.
2163655adc7SJason A. Donenfeld 	 */
2173655adc7SJason A. Donenfeld 	spin_lock_irqsave(&base_crng.lock, flags);
2183655adc7SJason A. Donenfeld 	memcpy(base_crng.key, key, sizeof(base_crng.key));
2193655adc7SJason A. Donenfeld 	next_gen = base_crng.generation + 1;
2203655adc7SJason A. Donenfeld 	if (next_gen == ULONG_MAX)
2213655adc7SJason A. Donenfeld 		++next_gen;
2223655adc7SJason A. Donenfeld 	WRITE_ONCE(base_crng.generation, next_gen);
2233655adc7SJason A. Donenfeld 	WRITE_ONCE(base_crng.birth, jiffies);
224f5bda35fSJason A. Donenfeld 	if (!static_branch_likely(&crng_is_ready))
225e3d2c5e7SJason A. Donenfeld 		crng_init = CRNG_READY;
2263655adc7SJason A. Donenfeld 	spin_unlock_irqrestore(&base_crng.lock, flags);
2273655adc7SJason A. Donenfeld 	memzero_explicit(key, sizeof(key));
2283655adc7SJason A. Donenfeld }
2293655adc7SJason A. Donenfeld 
2303655adc7SJason A. Donenfeld /*
2313655adc7SJason A. Donenfeld  * This generates a ChaCha block using the provided key, and then
2327f637be4SJason A. Donenfeld  * immediately overwrites that key with half the block. It returns
2333655adc7SJason A. Donenfeld  * the resultant ChaCha state to the user, along with the second
2343655adc7SJason A. Donenfeld  * half of the block containing 32 bytes of random data that may
2353655adc7SJason A. Donenfeld  * be used; random_data_len may not be greater than 32.
2368717627dSJason A. Donenfeld  *
2378717627dSJason A. Donenfeld  * The returned ChaCha state contains within it a copy of the old
2388717627dSJason A. Donenfeld  * key value, at index 4, so the state should always be zeroed out
2398717627dSJason A. Donenfeld  * immediately after using in order to maintain forward secrecy.
2408717627dSJason A. Donenfeld  * If the state cannot be erased in a timely manner, then it is
2418717627dSJason A. Donenfeld  * safer to set the random_data parameter to &chacha_state[4] so
2428717627dSJason A. Donenfeld  * that this function overwrites it before returning.
2433655adc7SJason A. Donenfeld  */
2443655adc7SJason A. Donenfeld static void crng_fast_key_erasure(u8 key[CHACHA_KEY_SIZE],
2453655adc7SJason A. Donenfeld 				  u32 chacha_state[CHACHA_STATE_WORDS],
2463655adc7SJason A. Donenfeld 				  u8 *random_data, size_t random_data_len)
2473655adc7SJason A. Donenfeld {
2483655adc7SJason A. Donenfeld 	u8 first_block[CHACHA_BLOCK_SIZE];
2493655adc7SJason A. Donenfeld 
2503655adc7SJason A. Donenfeld 	BUG_ON(random_data_len > 32);
2513655adc7SJason A. Donenfeld 
2523655adc7SJason A. Donenfeld 	chacha_init_consts(chacha_state);
2533655adc7SJason A. Donenfeld 	memcpy(&chacha_state[4], key, CHACHA_KEY_SIZE);
2543655adc7SJason A. Donenfeld 	memset(&chacha_state[12], 0, sizeof(u32) * 4);
2553655adc7SJason A. Donenfeld 	chacha20_block(chacha_state, first_block);
2563655adc7SJason A. Donenfeld 
2573655adc7SJason A. Donenfeld 	memcpy(key, first_block, CHACHA_KEY_SIZE);
2588717627dSJason A. Donenfeld 	memcpy(random_data, first_block + CHACHA_KEY_SIZE, random_data_len);
2593655adc7SJason A. Donenfeld 	memzero_explicit(first_block, sizeof(first_block));
2603655adc7SJason A. Donenfeld }
2613655adc7SJason A. Donenfeld 
2623655adc7SJason A. Donenfeld /*
263745558f9SDominik Brodowski  * Return the interval until the next reseeding, which is normally
264745558f9SDominik Brodowski  * CRNG_RESEED_INTERVAL, but during early boot, it is at an interval
265e85c0fc1SJason A. Donenfeld  * proportional to the uptime.
2667a7ff644SJason A. Donenfeld  */
267745558f9SDominik Brodowski static unsigned int crng_reseed_interval(void)
2687a7ff644SJason A. Donenfeld {
2697a7ff644SJason A. Donenfeld 	static bool early_boot = true;
2707a7ff644SJason A. Donenfeld 
2717a7ff644SJason A. Donenfeld 	if (unlikely(READ_ONCE(early_boot))) {
2727a7ff644SJason A. Donenfeld 		time64_t uptime = ktime_get_seconds();
2737a7ff644SJason A. Donenfeld 		if (uptime >= CRNG_RESEED_INTERVAL / HZ * 2)
2747a7ff644SJason A. Donenfeld 			WRITE_ONCE(early_boot, false);
2757a7ff644SJason A. Donenfeld 		else
276745558f9SDominik Brodowski 			return max_t(unsigned int, CRNG_RESEED_START_INTERVAL,
2777a7ff644SJason A. Donenfeld 				     (unsigned int)uptime / 2 * HZ);
2787a7ff644SJason A. Donenfeld 	}
279745558f9SDominik Brodowski 	return CRNG_RESEED_INTERVAL;
2807a7ff644SJason A. Donenfeld }
2817a7ff644SJason A. Donenfeld 
2827a7ff644SJason A. Donenfeld /*
2833655adc7SJason A. Donenfeld  * This function returns a ChaCha state that you may use for generating
2843655adc7SJason A. Donenfeld  * random data. It also returns up to 32 bytes on its own of random data
2853655adc7SJason A. Donenfeld  * that may be used; random_data_len may not be greater than 32.
2863655adc7SJason A. Donenfeld  */
2873655adc7SJason A. Donenfeld static void crng_make_state(u32 chacha_state[CHACHA_STATE_WORDS],
2883655adc7SJason A. Donenfeld 			    u8 *random_data, size_t random_data_len)
2893655adc7SJason A. Donenfeld {
2903655adc7SJason A. Donenfeld 	unsigned long flags;
2913655adc7SJason A. Donenfeld 	struct crng *crng;
2923655adc7SJason A. Donenfeld 
2933655adc7SJason A. Donenfeld 	BUG_ON(random_data_len > 32);
2943655adc7SJason A. Donenfeld 
2953655adc7SJason A. Donenfeld 	/*
2963655adc7SJason A. Donenfeld 	 * For the fast path, we check whether we're ready, unlocked first, and
2973655adc7SJason A. Donenfeld 	 * then re-check once locked later. In the case where we're really not
2985c3b747eSJason A. Donenfeld 	 * ready, we do fast key erasure with the base_crng directly, extracting
299e3d2c5e7SJason A. Donenfeld 	 * when crng_init is CRNG_EMPTY.
3003655adc7SJason A. Donenfeld 	 */
301a96cfe2dSJason A. Donenfeld 	if (!crng_ready()) {
3023655adc7SJason A. Donenfeld 		bool ready;
3033655adc7SJason A. Donenfeld 
3043655adc7SJason A. Donenfeld 		spin_lock_irqsave(&base_crng.lock, flags);
3053655adc7SJason A. Donenfeld 		ready = crng_ready();
3065c3b747eSJason A. Donenfeld 		if (!ready) {
307e3d2c5e7SJason A. Donenfeld 			if (crng_init == CRNG_EMPTY)
3085c3b747eSJason A. Donenfeld 				extract_entropy(base_crng.key, sizeof(base_crng.key));
3093655adc7SJason A. Donenfeld 			crng_fast_key_erasure(base_crng.key, chacha_state,
3103655adc7SJason A. Donenfeld 					      random_data, random_data_len);
3115c3b747eSJason A. Donenfeld 		}
3123655adc7SJason A. Donenfeld 		spin_unlock_irqrestore(&base_crng.lock, flags);
3133655adc7SJason A. Donenfeld 		if (!ready)
3143655adc7SJason A. Donenfeld 			return;
3153655adc7SJason A. Donenfeld 	}
3163655adc7SJason A. Donenfeld 
3173655adc7SJason A. Donenfeld 	/*
318e85c0fc1SJason A. Donenfeld 	 * If the base_crng is old enough, we reseed, which in turn bumps the
319e85c0fc1SJason A. Donenfeld 	 * generation counter that we check below.
3203655adc7SJason A. Donenfeld 	 */
321745558f9SDominik Brodowski 	if (unlikely(time_is_before_jiffies(READ_ONCE(base_crng.birth) + crng_reseed_interval())))
322e85c0fc1SJason A. Donenfeld 		crng_reseed();
3233655adc7SJason A. Donenfeld 
3243655adc7SJason A. Donenfeld 	local_lock_irqsave(&crngs.lock, flags);
3253655adc7SJason A. Donenfeld 	crng = raw_cpu_ptr(&crngs);
3263655adc7SJason A. Donenfeld 
3273655adc7SJason A. Donenfeld 	/*
3283655adc7SJason A. Donenfeld 	 * If our per-cpu crng is older than the base_crng, then it means
3293655adc7SJason A. Donenfeld 	 * somebody reseeded the base_crng. In that case, we do fast key
3303655adc7SJason A. Donenfeld 	 * erasure on the base_crng, and use its output as the new key
3313655adc7SJason A. Donenfeld 	 * for our per-cpu crng. This brings us up to date with base_crng.
3323655adc7SJason A. Donenfeld 	 */
3333655adc7SJason A. Donenfeld 	if (unlikely(crng->generation != READ_ONCE(base_crng.generation))) {
3343655adc7SJason A. Donenfeld 		spin_lock(&base_crng.lock);
3353655adc7SJason A. Donenfeld 		crng_fast_key_erasure(base_crng.key, chacha_state,
3363655adc7SJason A. Donenfeld 				      crng->key, sizeof(crng->key));
3373655adc7SJason A. Donenfeld 		crng->generation = base_crng.generation;
3383655adc7SJason A. Donenfeld 		spin_unlock(&base_crng.lock);
3393655adc7SJason A. Donenfeld 	}
3403655adc7SJason A. Donenfeld 
3413655adc7SJason A. Donenfeld 	/*
3423655adc7SJason A. Donenfeld 	 * Finally, when we've made it this far, our per-cpu crng has an up
3433655adc7SJason A. Donenfeld 	 * to date key, and we can do fast key erasure with it to produce
3443655adc7SJason A. Donenfeld 	 * some random data and a ChaCha state for the caller. All other
3453655adc7SJason A. Donenfeld 	 * branches of this function are "unlikely", so most of the time we
3463655adc7SJason A. Donenfeld 	 * should wind up here immediately.
3473655adc7SJason A. Donenfeld 	 */
3483655adc7SJason A. Donenfeld 	crng_fast_key_erasure(crng->key, chacha_state, random_data, random_data_len);
3493655adc7SJason A. Donenfeld 	local_unlock_irqrestore(&crngs.lock, flags);
3503655adc7SJason A. Donenfeld }
3513655adc7SJason A. Donenfeld 
352a1940263SJason A. Donenfeld static void _get_random_bytes(void *buf, size_t len)
3533655adc7SJason A. Donenfeld {
3543655adc7SJason A. Donenfeld 	u32 chacha_state[CHACHA_STATE_WORDS];
3553655adc7SJason A. Donenfeld 	u8 tmp[CHACHA_BLOCK_SIZE];
356a1940263SJason A. Donenfeld 	size_t first_block_len;
3573655adc7SJason A. Donenfeld 
358a1940263SJason A. Donenfeld 	if (!len)
3593655adc7SJason A. Donenfeld 		return;
3603655adc7SJason A. Donenfeld 
361a1940263SJason A. Donenfeld 	first_block_len = min_t(size_t, 32, len);
362a1940263SJason A. Donenfeld 	crng_make_state(chacha_state, buf, first_block_len);
363a1940263SJason A. Donenfeld 	len -= first_block_len;
364a1940263SJason A. Donenfeld 	buf += first_block_len;
3653655adc7SJason A. Donenfeld 
366a1940263SJason A. Donenfeld 	while (len) {
367a1940263SJason A. Donenfeld 		if (len < CHACHA_BLOCK_SIZE) {
3683655adc7SJason A. Donenfeld 			chacha20_block(chacha_state, tmp);
369a1940263SJason A. Donenfeld 			memcpy(buf, tmp, len);
3703655adc7SJason A. Donenfeld 			memzero_explicit(tmp, sizeof(tmp));
3713655adc7SJason A. Donenfeld 			break;
3723655adc7SJason A. Donenfeld 		}
3733655adc7SJason A. Donenfeld 
3743655adc7SJason A. Donenfeld 		chacha20_block(chacha_state, buf);
3753655adc7SJason A. Donenfeld 		if (unlikely(chacha_state[12] == 0))
3763655adc7SJason A. Donenfeld 			++chacha_state[13];
377a1940263SJason A. Donenfeld 		len -= CHACHA_BLOCK_SIZE;
3783655adc7SJason A. Donenfeld 		buf += CHACHA_BLOCK_SIZE;
3793655adc7SJason A. Donenfeld 	}
3803655adc7SJason A. Donenfeld 
3813655adc7SJason A. Donenfeld 	memzero_explicit(chacha_state, sizeof(chacha_state));
3823655adc7SJason A. Donenfeld }
3833655adc7SJason A. Donenfeld 
3843655adc7SJason A. Donenfeld /*
3853655adc7SJason A. Donenfeld  * This function is the exported kernel interface.  It returns some
3863655adc7SJason A. Donenfeld  * number of good random numbers, suitable for key generation, seeding
387248561adSJason A. Donenfeld  * TCP sequence numbers, etc. In order to ensure that the randomness
388248561adSJason A. Donenfeld  * by this function is okay, the function wait_for_random_bytes()
389248561adSJason A. Donenfeld  * should be called and return 0 at least once at any point prior.
3903655adc7SJason A. Donenfeld  */
391a1940263SJason A. Donenfeld void get_random_bytes(void *buf, size_t len)
3923655adc7SJason A. Donenfeld {
393cc1e127bSJason A. Donenfeld 	warn_unseeded_randomness();
394a1940263SJason A. Donenfeld 	_get_random_bytes(buf, len);
3953655adc7SJason A. Donenfeld }
3963655adc7SJason A. Donenfeld EXPORT_SYMBOL(get_random_bytes);
3973655adc7SJason A. Donenfeld 
3981b388e77SJens Axboe static ssize_t get_random_bytes_user(struct iov_iter *iter)
3993655adc7SJason A. Donenfeld {
4003655adc7SJason A. Donenfeld 	u32 chacha_state[CHACHA_STATE_WORDS];
4011b388e77SJens Axboe 	u8 block[CHACHA_BLOCK_SIZE];
4021b388e77SJens Axboe 	size_t ret = 0, copied;
4033655adc7SJason A. Donenfeld 
4041b388e77SJens Axboe 	if (unlikely(!iov_iter_count(iter)))
4053655adc7SJason A. Donenfeld 		return 0;
4063655adc7SJason A. Donenfeld 
407aba120ccSJason A. Donenfeld 	/*
408aba120ccSJason A. Donenfeld 	 * Immediately overwrite the ChaCha key at index 4 with random
40963b8ea5eSJason A. Donenfeld 	 * bytes, in case userspace causes copy_to_iter() below to sleep
410aba120ccSJason A. Donenfeld 	 * forever, so that we still retain forward secrecy in that case.
411aba120ccSJason A. Donenfeld 	 */
412aba120ccSJason A. Donenfeld 	crng_make_state(chacha_state, (u8 *)&chacha_state[4], CHACHA_KEY_SIZE);
413aba120ccSJason A. Donenfeld 	/*
414aba120ccSJason A. Donenfeld 	 * However, if we're doing a read of len <= 32, we don't need to
415aba120ccSJason A. Donenfeld 	 * use chacha_state after, so we can simply return those bytes to
416aba120ccSJason A. Donenfeld 	 * the user directly.
417aba120ccSJason A. Donenfeld 	 */
4181b388e77SJens Axboe 	if (iov_iter_count(iter) <= CHACHA_KEY_SIZE) {
4191b388e77SJens Axboe 		ret = copy_to_iter(&chacha_state[4], CHACHA_KEY_SIZE, iter);
420aba120ccSJason A. Donenfeld 		goto out_zero_chacha;
421aba120ccSJason A. Donenfeld 	}
4223655adc7SJason A. Donenfeld 
4235209aed5SJason A. Donenfeld 	for (;;) {
4241b388e77SJens Axboe 		chacha20_block(chacha_state, block);
4253655adc7SJason A. Donenfeld 		if (unlikely(chacha_state[12] == 0))
4263655adc7SJason A. Donenfeld 			++chacha_state[13];
4273655adc7SJason A. Donenfeld 
4281b388e77SJens Axboe 		copied = copy_to_iter(block, sizeof(block), iter);
4291b388e77SJens Axboe 		ret += copied;
4301b388e77SJens Axboe 		if (!iov_iter_count(iter) || copied != sizeof(block))
4315209aed5SJason A. Donenfeld 			break;
432e3c1c4fdSJason A. Donenfeld 
4331b388e77SJens Axboe 		BUILD_BUG_ON(PAGE_SIZE % sizeof(block) != 0);
4345209aed5SJason A. Donenfeld 		if (ret % PAGE_SIZE == 0) {
435e3c1c4fdSJason A. Donenfeld 			if (signal_pending(current))
436e3c1c4fdSJason A. Donenfeld 				break;
437e3c1c4fdSJason A. Donenfeld 			cond_resched();
438e3c1c4fdSJason A. Donenfeld 		}
4395209aed5SJason A. Donenfeld 	}
4403655adc7SJason A. Donenfeld 
4411b388e77SJens Axboe 	memzero_explicit(block, sizeof(block));
442aba120ccSJason A. Donenfeld out_zero_chacha:
443aba120ccSJason A. Donenfeld 	memzero_explicit(chacha_state, sizeof(chacha_state));
4445209aed5SJason A. Donenfeld 	return ret ? ret : -EFAULT;
4453655adc7SJason A. Donenfeld }
4463655adc7SJason A. Donenfeld 
4473655adc7SJason A. Donenfeld /*
4483655adc7SJason A. Donenfeld  * Batched entropy returns random integers. The quality of the random
4493655adc7SJason A. Donenfeld  * number is good as /dev/urandom. In order to ensure that the randomness
4503655adc7SJason A. Donenfeld  * provided by this function is okay, the function wait_for_random_bytes()
4513655adc7SJason A. Donenfeld  * should be called and return 0 at least once at any point prior.
4523655adc7SJason A. Donenfeld  */
4533655adc7SJason A. Donenfeld 
4543092adceSJason A. Donenfeld #define DEFINE_BATCHED_ENTROPY(type)						\
4553092adceSJason A. Donenfeld struct batch_ ##type {								\
4563092adceSJason A. Donenfeld 	/*									\
4573092adceSJason A. Donenfeld 	 * We make this 1.5x a ChaCha block, so that we get the			\
4583092adceSJason A. Donenfeld 	 * remaining 32 bytes from fast key erasure, plus one full		\
4593092adceSJason A. Donenfeld 	 * block from the detached ChaCha state. We can increase		\
4603092adceSJason A. Donenfeld 	 * the size of this later if needed so long as we keep the		\
4613092adceSJason A. Donenfeld 	 * formula of (integer_blocks + 0.5) * CHACHA_BLOCK_SIZE.		\
4623092adceSJason A. Donenfeld 	 */									\
4633092adceSJason A. Donenfeld 	type entropy[CHACHA_BLOCK_SIZE * 3 / (2 * sizeof(type))];		\
4643092adceSJason A. Donenfeld 	local_lock_t lock;							\
4653092adceSJason A. Donenfeld 	unsigned long generation;						\
4663092adceSJason A. Donenfeld 	unsigned int position;							\
4673092adceSJason A. Donenfeld };										\
4683092adceSJason A. Donenfeld 										\
4693092adceSJason A. Donenfeld static DEFINE_PER_CPU(struct batch_ ##type, batched_entropy_ ##type) = {	\
4703092adceSJason A. Donenfeld 	.lock = INIT_LOCAL_LOCK(batched_entropy_ ##type.lock),			\
4713092adceSJason A. Donenfeld 	.position = UINT_MAX							\
4723092adceSJason A. Donenfeld };										\
4733092adceSJason A. Donenfeld 										\
4743092adceSJason A. Donenfeld type get_random_ ##type(void)							\
4753092adceSJason A. Donenfeld {										\
4763092adceSJason A. Donenfeld 	type ret;								\
4773092adceSJason A. Donenfeld 	unsigned long flags;							\
4783092adceSJason A. Donenfeld 	struct batch_ ##type *batch;						\
4793092adceSJason A. Donenfeld 	unsigned long next_gen;							\
4803092adceSJason A. Donenfeld 										\
4813092adceSJason A. Donenfeld 	warn_unseeded_randomness();						\
4823092adceSJason A. Donenfeld 										\
4833092adceSJason A. Donenfeld 	if  (!crng_ready()) {							\
4843092adceSJason A. Donenfeld 		_get_random_bytes(&ret, sizeof(ret));				\
4853092adceSJason A. Donenfeld 		return ret;							\
4863092adceSJason A. Donenfeld 	}									\
4873092adceSJason A. Donenfeld 										\
4883092adceSJason A. Donenfeld 	local_lock_irqsave(&batched_entropy_ ##type.lock, flags);		\
4893092adceSJason A. Donenfeld 	batch = raw_cpu_ptr(&batched_entropy_##type);				\
4903092adceSJason A. Donenfeld 										\
4913092adceSJason A. Donenfeld 	next_gen = READ_ONCE(base_crng.generation);				\
4923092adceSJason A. Donenfeld 	if (batch->position >= ARRAY_SIZE(batch->entropy) ||			\
4933092adceSJason A. Donenfeld 	    next_gen != batch->generation) {					\
4943092adceSJason A. Donenfeld 		_get_random_bytes(batch->entropy, sizeof(batch->entropy));	\
4953092adceSJason A. Donenfeld 		batch->position = 0;						\
4963092adceSJason A. Donenfeld 		batch->generation = next_gen;					\
4973092adceSJason A. Donenfeld 	}									\
4983092adceSJason A. Donenfeld 										\
4993092adceSJason A. Donenfeld 	ret = batch->entropy[batch->position];					\
5003092adceSJason A. Donenfeld 	batch->entropy[batch->position] = 0;					\
5013092adceSJason A. Donenfeld 	++batch->position;							\
5023092adceSJason A. Donenfeld 	local_unlock_irqrestore(&batched_entropy_ ##type.lock, flags);		\
5033092adceSJason A. Donenfeld 	return ret;								\
5043092adceSJason A. Donenfeld }										\
5053092adceSJason A. Donenfeld EXPORT_SYMBOL(get_random_ ##type);
5063655adc7SJason A. Donenfeld 
5073092adceSJason A. Donenfeld DEFINE_BATCHED_ENTROPY(u64)
5083092adceSJason A. Donenfeld DEFINE_BATCHED_ENTROPY(u32)
5093655adc7SJason A. Donenfeld 
5103191dd5aSJason A. Donenfeld #ifdef CONFIG_SMP
5113191dd5aSJason A. Donenfeld /*
5123191dd5aSJason A. Donenfeld  * This function is called when the CPU is coming up, with entry
5133191dd5aSJason A. Donenfeld  * CPUHP_RANDOM_PREPARE, which comes before CPUHP_WORKQUEUE_PREP.
5143191dd5aSJason A. Donenfeld  */
515560181c2SJason A. Donenfeld int __cold random_prepare_cpu(unsigned int cpu)
5163191dd5aSJason A. Donenfeld {
5173191dd5aSJason A. Donenfeld 	/*
5183191dd5aSJason A. Donenfeld 	 * When the cpu comes back online, immediately invalidate both
5193191dd5aSJason A. Donenfeld 	 * the per-cpu crng and all batches, so that we serve fresh
5203191dd5aSJason A. Donenfeld 	 * randomness.
5213191dd5aSJason A. Donenfeld 	 */
5223191dd5aSJason A. Donenfeld 	per_cpu_ptr(&crngs, cpu)->generation = ULONG_MAX;
5233191dd5aSJason A. Donenfeld 	per_cpu_ptr(&batched_entropy_u32, cpu)->position = UINT_MAX;
5243191dd5aSJason A. Donenfeld 	per_cpu_ptr(&batched_entropy_u64, cpu)->position = UINT_MAX;
5253191dd5aSJason A. Donenfeld 	return 0;
5263191dd5aSJason A. Donenfeld }
5273191dd5aSJason A. Donenfeld #endif
5283191dd5aSJason A. Donenfeld 
529a5ed7cb1SJason A. Donenfeld 
530a5ed7cb1SJason A. Donenfeld /**********************************************************************
531a5ed7cb1SJason A. Donenfeld  *
532a5ed7cb1SJason A. Donenfeld  * Entropy accumulation and extraction routines.
533a5ed7cb1SJason A. Donenfeld  *
534a5ed7cb1SJason A. Donenfeld  * Callers may add entropy via:
535a5ed7cb1SJason A. Donenfeld  *
536a1940263SJason A. Donenfeld  *     static void mix_pool_bytes(const void *buf, size_t len)
537a5ed7cb1SJason A. Donenfeld  *
538a5ed7cb1SJason A. Donenfeld  * After which, if added entropy should be credited:
539a5ed7cb1SJason A. Donenfeld  *
540a1940263SJason A. Donenfeld  *     static void credit_init_bits(size_t bits)
541a5ed7cb1SJason A. Donenfeld  *
542e85c0fc1SJason A. Donenfeld  * Finally, extract entropy via:
543a5ed7cb1SJason A. Donenfeld  *
544a1940263SJason A. Donenfeld  *     static void extract_entropy(void *buf, size_t len)
545a5ed7cb1SJason A. Donenfeld  *
546a5ed7cb1SJason A. Donenfeld  **********************************************************************/
547a5ed7cb1SJason A. Donenfeld 
548c5704490SJason A. Donenfeld enum {
5496e8ec255SJason A. Donenfeld 	POOL_BITS = BLAKE2S_HASH_SIZE * 8,
550e3d2c5e7SJason A. Donenfeld 	POOL_READY_BITS = POOL_BITS, /* When crng_init->CRNG_READY */
551e3d2c5e7SJason A. Donenfeld 	POOL_EARLY_BITS = POOL_READY_BITS / 2 /* When crng_init->CRNG_EARLY */
5521da177e4SLinus Torvalds };
5531da177e4SLinus Torvalds 
55490ed1e67SJason A. Donenfeld static struct {
5556e8ec255SJason A. Donenfeld 	struct blake2s_state hash;
55643358209SMatt Mackall 	spinlock_t lock;
557e85c0fc1SJason A. Donenfeld 	unsigned int init_bits;
55890ed1e67SJason A. Donenfeld } input_pool = {
5596e8ec255SJason A. Donenfeld 	.hash.h = { BLAKE2S_IV0 ^ (0x01010000 | BLAKE2S_HASH_SIZE),
5606e8ec255SJason A. Donenfeld 		    BLAKE2S_IV1, BLAKE2S_IV2, BLAKE2S_IV3, BLAKE2S_IV4,
5616e8ec255SJason A. Donenfeld 		    BLAKE2S_IV5, BLAKE2S_IV6, BLAKE2S_IV7 },
5626e8ec255SJason A. Donenfeld 	.hash.outlen = BLAKE2S_HASH_SIZE,
563eece09ecSThomas Gleixner 	.lock = __SPIN_LOCK_UNLOCKED(input_pool.lock),
5641da177e4SLinus Torvalds };
5651da177e4SLinus Torvalds 
566a1940263SJason A. Donenfeld static void _mix_pool_bytes(const void *buf, size_t len)
567a5ed7cb1SJason A. Donenfeld {
568a1940263SJason A. Donenfeld 	blake2s_update(&input_pool.hash, buf, len);
569a5ed7cb1SJason A. Donenfeld }
57090ed1e67SJason A. Donenfeld 
5711da177e4SLinus Torvalds /*
572e85c0fc1SJason A. Donenfeld  * This function adds bytes into the input pool. It does not
573e85c0fc1SJason A. Donenfeld  * update the initialization bit counter; the caller should call
574e85c0fc1SJason A. Donenfeld  * credit_init_bits if this is appropriate.
5751da177e4SLinus Torvalds  */
576a1940263SJason A. Donenfeld static void mix_pool_bytes(const void *buf, size_t len)
5771da177e4SLinus Torvalds {
578902c098aSTheodore Ts'o 	unsigned long flags;
579902c098aSTheodore Ts'o 
58090ed1e67SJason A. Donenfeld 	spin_lock_irqsave(&input_pool.lock, flags);
581a1940263SJason A. Donenfeld 	_mix_pool_bytes(buf, len);
58290ed1e67SJason A. Donenfeld 	spin_unlock_irqrestore(&input_pool.lock, flags);
5831da177e4SLinus Torvalds }
5841da177e4SLinus Torvalds 
585a5ed7cb1SJason A. Donenfeld /*
586a5ed7cb1SJason A. Donenfeld  * This is an HKDF-like construction for using the hashed collected entropy
587a5ed7cb1SJason A. Donenfeld  * as a PRF key, that's then expanded block-by-block.
588a5ed7cb1SJason A. Donenfeld  */
589a1940263SJason A. Donenfeld static void extract_entropy(void *buf, size_t len)
590a5ed7cb1SJason A. Donenfeld {
591a5ed7cb1SJason A. Donenfeld 	unsigned long flags;
592a5ed7cb1SJason A. Donenfeld 	u8 seed[BLAKE2S_HASH_SIZE], next_key[BLAKE2S_HASH_SIZE];
593a5ed7cb1SJason A. Donenfeld 	struct {
594a5ed7cb1SJason A. Donenfeld 		unsigned long rdseed[32 / sizeof(long)];
595a5ed7cb1SJason A. Donenfeld 		size_t counter;
596a5ed7cb1SJason A. Donenfeld 	} block;
597d349ab99SJason A. Donenfeld 	size_t i, longs;
598a5ed7cb1SJason A. Donenfeld 
599d349ab99SJason A. Donenfeld 	for (i = 0; i < ARRAY_SIZE(block.rdseed);) {
600d349ab99SJason A. Donenfeld 		longs = arch_get_random_seed_longs(&block.rdseed[i], ARRAY_SIZE(block.rdseed) - i);
601d349ab99SJason A. Donenfeld 		if (longs) {
602d349ab99SJason A. Donenfeld 			i += longs;
603d349ab99SJason A. Donenfeld 			continue;
604d349ab99SJason A. Donenfeld 		}
605d349ab99SJason A. Donenfeld 		longs = arch_get_random_longs(&block.rdseed[i], ARRAY_SIZE(block.rdseed) - i);
606d349ab99SJason A. Donenfeld 		if (longs) {
607d349ab99SJason A. Donenfeld 			i += longs;
608d349ab99SJason A. Donenfeld 			continue;
609d349ab99SJason A. Donenfeld 		}
610d349ab99SJason A. Donenfeld 		block.rdseed[i++] = random_get_entropy();
611a5ed7cb1SJason A. Donenfeld 	}
612a5ed7cb1SJason A. Donenfeld 
613a5ed7cb1SJason A. Donenfeld 	spin_lock_irqsave(&input_pool.lock, flags);
614a5ed7cb1SJason A. Donenfeld 
615a5ed7cb1SJason A. Donenfeld 	/* seed = HASHPRF(last_key, entropy_input) */
616a5ed7cb1SJason A. Donenfeld 	blake2s_final(&input_pool.hash, seed);
617a5ed7cb1SJason A. Donenfeld 
618a5ed7cb1SJason A. Donenfeld 	/* next_key = HASHPRF(seed, RDSEED || 0) */
619a5ed7cb1SJason A. Donenfeld 	block.counter = 0;
620a5ed7cb1SJason A. Donenfeld 	blake2s(next_key, (u8 *)&block, seed, sizeof(next_key), sizeof(block), sizeof(seed));
621a5ed7cb1SJason A. Donenfeld 	blake2s_init_key(&input_pool.hash, BLAKE2S_HASH_SIZE, next_key, sizeof(next_key));
622a5ed7cb1SJason A. Donenfeld 
623a5ed7cb1SJason A. Donenfeld 	spin_unlock_irqrestore(&input_pool.lock, flags);
624a5ed7cb1SJason A. Donenfeld 	memzero_explicit(next_key, sizeof(next_key));
625a5ed7cb1SJason A. Donenfeld 
626a1940263SJason A. Donenfeld 	while (len) {
627a1940263SJason A. Donenfeld 		i = min_t(size_t, len, BLAKE2S_HASH_SIZE);
628a5ed7cb1SJason A. Donenfeld 		/* output = HASHPRF(seed, RDSEED || ++counter) */
629a5ed7cb1SJason A. Donenfeld 		++block.counter;
630a5ed7cb1SJason A. Donenfeld 		blake2s(buf, (u8 *)&block, seed, i, sizeof(block), sizeof(seed));
631a1940263SJason A. Donenfeld 		len -= i;
632a5ed7cb1SJason A. Donenfeld 		buf += i;
633a5ed7cb1SJason A. Donenfeld 	}
634a5ed7cb1SJason A. Donenfeld 
635a5ed7cb1SJason A. Donenfeld 	memzero_explicit(seed, sizeof(seed));
636a5ed7cb1SJason A. Donenfeld 	memzero_explicit(&block, sizeof(block));
637a5ed7cb1SJason A. Donenfeld }
638a5ed7cb1SJason A. Donenfeld 
639560181c2SJason A. Donenfeld #define credit_init_bits(bits) if (!crng_ready()) _credit_init_bits(bits)
640560181c2SJason A. Donenfeld 
641560181c2SJason A. Donenfeld static void __cold _credit_init_bits(size_t bits)
642a5ed7cb1SJason A. Donenfeld {
643f5bda35fSJason A. Donenfeld 	static struct execute_work set_ready;
644fed7ef06SJason A. Donenfeld 	unsigned int new, orig, add;
6455c3b747eSJason A. Donenfeld 	unsigned long flags;
6465c3b747eSJason A. Donenfeld 
647560181c2SJason A. Donenfeld 	if (!bits)
6485c3b747eSJason A. Donenfeld 		return;
6495c3b747eSJason A. Donenfeld 
650a1940263SJason A. Donenfeld 	add = min_t(size_t, bits, POOL_BITS);
6515c3b747eSJason A. Donenfeld 
652e85c0fc1SJason A. Donenfeld 	orig = READ_ONCE(input_pool.init_bits);
653b7a68f67SUros Bizjak 	do {
654fed7ef06SJason A. Donenfeld 		new = min_t(unsigned int, POOL_BITS, orig + add);
655b7a68f67SUros Bizjak 	} while (!try_cmpxchg(&input_pool.init_bits, &orig, new));
6565c3b747eSJason A. Donenfeld 
65768c9c8b1SJason A. Donenfeld 	if (orig < POOL_READY_BITS && new >= POOL_READY_BITS) {
65868c9c8b1SJason A. Donenfeld 		crng_reseed(); /* Sets crng_init to CRNG_READY under base_crng.lock. */
65960e5b288SJason A. Donenfeld 		if (static_key_initialized)
660f5bda35fSJason A. Donenfeld 			execute_in_process_context(crng_set_ready, &set_ready);
66168c9c8b1SJason A. Donenfeld 		wake_up_interruptible(&crng_init_wait);
66268c9c8b1SJason A. Donenfeld 		kill_fasync(&fasync, SIGIO, POLL_IN);
66368c9c8b1SJason A. Donenfeld 		pr_notice("crng init done\n");
664cc1e127bSJason A. Donenfeld 		if (urandom_warning.missed)
66568c9c8b1SJason A. Donenfeld 			pr_notice("%d urandom warning(s) missed due to ratelimiting\n",
66668c9c8b1SJason A. Donenfeld 				  urandom_warning.missed);
66768c9c8b1SJason A. Donenfeld 	} else if (orig < POOL_EARLY_BITS && new >= POOL_EARLY_BITS) {
6685c3b747eSJason A. Donenfeld 		spin_lock_irqsave(&base_crng.lock, flags);
66968c9c8b1SJason A. Donenfeld 		/* Check if crng_init is CRNG_EMPTY, to avoid race with crng_reseed(). */
670e3d2c5e7SJason A. Donenfeld 		if (crng_init == CRNG_EMPTY) {
6715c3b747eSJason A. Donenfeld 			extract_entropy(base_crng.key, sizeof(base_crng.key));
672e3d2c5e7SJason A. Donenfeld 			crng_init = CRNG_EARLY;
6735c3b747eSJason A. Donenfeld 		}
6745c3b747eSJason A. Donenfeld 		spin_unlock_irqrestore(&base_crng.lock, flags);
6755c3b747eSJason A. Donenfeld 	}
6765c3b747eSJason A. Donenfeld }
6775c3b747eSJason A. Donenfeld 
67892c653cfSJason A. Donenfeld 
67992c653cfSJason A. Donenfeld /**********************************************************************
68092c653cfSJason A. Donenfeld  *
68192c653cfSJason A. Donenfeld  * Entropy collection routines.
68292c653cfSJason A. Donenfeld  *
68392c653cfSJason A. Donenfeld  * The following exported functions are used for pushing entropy into
68492c653cfSJason A. Donenfeld  * the above entropy accumulation routines:
68592c653cfSJason A. Donenfeld  *
686a1940263SJason A. Donenfeld  *	void add_device_randomness(const void *buf, size_t len);
687a1940263SJason A. Donenfeld  *	void add_hwgenerator_randomness(const void *buf, size_t len, size_t entropy);
688a1940263SJason A. Donenfeld  *	void add_bootloader_randomness(const void *buf, size_t len);
689a1940263SJason A. Donenfeld  *	void add_vmfork_randomness(const void *unique_vm_id, size_t len);
69092c653cfSJason A. Donenfeld  *	void add_interrupt_randomness(int irq);
691a1940263SJason A. Donenfeld  *	void add_input_randomness(unsigned int type, unsigned int code, unsigned int value);
692a4b5c26bSJason A. Donenfeld  *	void add_disk_randomness(struct gendisk *disk);
69392c653cfSJason A. Donenfeld  *
69492c653cfSJason A. Donenfeld  * add_device_randomness() adds data to the input pool that
69592c653cfSJason A. Donenfeld  * is likely to differ between two devices (or possibly even per boot).
69692c653cfSJason A. Donenfeld  * This would be things like MAC addresses or serial numbers, or the
69792c653cfSJason A. Donenfeld  * read-out of the RTC. This does *not* credit any actual entropy to
69892c653cfSJason A. Donenfeld  * the pool, but it initializes the pool to different values for devices
69992c653cfSJason A. Donenfeld  * that might otherwise be identical and have very little entropy
70092c653cfSJason A. Donenfeld  * available to them (particularly common in the embedded world).
70192c653cfSJason A. Donenfeld  *
70292c653cfSJason A. Donenfeld  * add_hwgenerator_randomness() is for true hardware RNGs, and will credit
70392c653cfSJason A. Donenfeld  * entropy as specified by the caller. If the entropy pool is full it will
70492c653cfSJason A. Donenfeld  * block until more entropy is needed.
70592c653cfSJason A. Donenfeld  *
7065c3b747eSJason A. Donenfeld  * add_bootloader_randomness() is called by bootloader drivers, such as EFI
7075c3b747eSJason A. Donenfeld  * and device tree, and credits its input depending on whether or not the
7085c3b747eSJason A. Donenfeld  * configuration option CONFIG_RANDOM_TRUST_BOOTLOADER is set.
70992c653cfSJason A. Donenfeld  *
710ae099e8eSJason A. Donenfeld  * add_vmfork_randomness() adds a unique (but not necessarily secret) ID
711ae099e8eSJason A. Donenfeld  * representing the current instance of a VM to the pool, without crediting,
712ae099e8eSJason A. Donenfeld  * and then force-reseeds the crng so that it takes effect immediately.
713ae099e8eSJason A. Donenfeld  *
71492c653cfSJason A. Donenfeld  * add_interrupt_randomness() uses the interrupt timing as random
71592c653cfSJason A. Donenfeld  * inputs to the entropy pool. Using the cycle counters and the irq source
71692c653cfSJason A. Donenfeld  * as inputs, it feeds the input pool roughly once a second or after 64
71792c653cfSJason A. Donenfeld  * interrupts, crediting 1 bit of entropy for whichever comes first.
71892c653cfSJason A. Donenfeld  *
719a4b5c26bSJason A. Donenfeld  * add_input_randomness() uses the input layer interrupt timing, as well
720a4b5c26bSJason A. Donenfeld  * as the event type information from the hardware.
721a4b5c26bSJason A. Donenfeld  *
722a4b5c26bSJason A. Donenfeld  * add_disk_randomness() uses what amounts to the seek time of block
723a4b5c26bSJason A. Donenfeld  * layer request events, on a per-disk_devt basis, as input to the
724a4b5c26bSJason A. Donenfeld  * entropy pool. Note that high-speed solid state drives with very low
725a4b5c26bSJason A. Donenfeld  * seek times do not make for good sources of entropy, as their seek
726a4b5c26bSJason A. Donenfeld  * times are usually fairly consistent.
727a4b5c26bSJason A. Donenfeld  *
728a4b5c26bSJason A. Donenfeld  * The last two routines try to estimate how many bits of entropy
729a4b5c26bSJason A. Donenfeld  * to credit. They do this by keeping track of the first and second
730a4b5c26bSJason A. Donenfeld  * order deltas of the event timings.
731a4b5c26bSJason A. Donenfeld  *
73292c653cfSJason A. Donenfeld  **********************************************************************/
73392c653cfSJason A. Donenfeld 
73439e0f991SJason A. Donenfeld static bool trust_cpu __initdata = IS_ENABLED(CONFIG_RANDOM_TRUST_CPU);
73539e0f991SJason A. Donenfeld static bool trust_bootloader __initdata = IS_ENABLED(CONFIG_RANDOM_TRUST_BOOTLOADER);
73692c653cfSJason A. Donenfeld static int __init parse_trust_cpu(char *arg)
73792c653cfSJason A. Donenfeld {
73892c653cfSJason A. Donenfeld 	return kstrtobool(arg, &trust_cpu);
73992c653cfSJason A. Donenfeld }
740d97c68d1SJason A. Donenfeld static int __init parse_trust_bootloader(char *arg)
741d97c68d1SJason A. Donenfeld {
742d97c68d1SJason A. Donenfeld 	return kstrtobool(arg, &trust_bootloader);
743d97c68d1SJason A. Donenfeld }
74492c653cfSJason A. Donenfeld early_param("random.trust_cpu", parse_trust_cpu);
745d97c68d1SJason A. Donenfeld early_param("random.trust_bootloader", parse_trust_bootloader);
746775f4b29STheodore Ts'o 
747b7b67d13SJason A. Donenfeld static int random_pm_notification(struct notifier_block *nb, unsigned long action, void *data)
748b7b67d13SJason A. Donenfeld {
749b7b67d13SJason A. Donenfeld 	unsigned long flags, entropy = random_get_entropy();
750b7b67d13SJason A. Donenfeld 
751b7b67d13SJason A. Donenfeld 	/*
752b7b67d13SJason A. Donenfeld 	 * Encode a representation of how long the system has been suspended,
753b7b67d13SJason A. Donenfeld 	 * in a way that is distinct from prior system suspends.
754b7b67d13SJason A. Donenfeld 	 */
755b7b67d13SJason A. Donenfeld 	ktime_t stamps[] = { ktime_get(), ktime_get_boottime(), ktime_get_real() };
756b7b67d13SJason A. Donenfeld 
757b7b67d13SJason A. Donenfeld 	spin_lock_irqsave(&input_pool.lock, flags);
758b7b67d13SJason A. Donenfeld 	_mix_pool_bytes(&action, sizeof(action));
759b7b67d13SJason A. Donenfeld 	_mix_pool_bytes(stamps, sizeof(stamps));
760b7b67d13SJason A. Donenfeld 	_mix_pool_bytes(&entropy, sizeof(entropy));
761b7b67d13SJason A. Donenfeld 	spin_unlock_irqrestore(&input_pool.lock, flags);
762b7b67d13SJason A. Donenfeld 
763b7b67d13SJason A. Donenfeld 	if (crng_ready() && (action == PM_RESTORE_PREPARE ||
764261e224dSKalesh Singh 	    (action == PM_POST_SUSPEND && !IS_ENABLED(CONFIG_PM_AUTOSLEEP) &&
765261e224dSKalesh Singh 	     !IS_ENABLED(CONFIG_PM_USERSPACE_AUTOSLEEP)))) {
766e85c0fc1SJason A. Donenfeld 		crng_reseed();
767b7b67d13SJason A. Donenfeld 		pr_notice("crng reseeded on system resumption\n");
768b7b67d13SJason A. Donenfeld 	}
769b7b67d13SJason A. Donenfeld 	return 0;
770b7b67d13SJason A. Donenfeld }
771b7b67d13SJason A. Donenfeld 
772b7b67d13SJason A. Donenfeld static struct notifier_block pm_notifier = { .notifier_call = random_pm_notification };
773b7b67d13SJason A. Donenfeld 
774775f4b29STheodore Ts'o /*
77592c653cfSJason A. Donenfeld  * The first collection of entropy occurs at system boot while interrupts
7762f14062bSJason A. Donenfeld  * are still turned off. Here we push in latent entropy, RDSEED, a timestamp,
7772f14062bSJason A. Donenfeld  * utsname(), and the command line. Depending on the above configuration knob,
7782f14062bSJason A. Donenfeld  * RDSEED may be considered sufficient for initialization. Note that much
7792f14062bSJason A. Donenfeld  * earlier setup may already have pushed entropy into the input pool by the
7802f14062bSJason A. Donenfeld  * time we get here.
781775f4b29STheodore Ts'o  */
7822f14062bSJason A. Donenfeld int __init random_init(const char *command_line)
783775f4b29STheodore Ts'o {
78492c653cfSJason A. Donenfeld 	ktime_t now = ktime_get_real();
785d349ab99SJason A. Donenfeld 	size_t i, longs, arch_bits;
786d349ab99SJason A. Donenfeld 	unsigned long entropy[BLAKE2S_BLOCK_SIZE / sizeof(long)];
787775f4b29STheodore Ts'o 
7881754abb3SJason A. Donenfeld #if defined(LATENT_ENTROPY_PLUGIN)
7891754abb3SJason A. Donenfeld 	static const u8 compiletime_seed[BLAKE2S_BLOCK_SIZE] __initconst __latent_entropy;
7901754abb3SJason A. Donenfeld 	_mix_pool_bytes(compiletime_seed, sizeof(compiletime_seed));
7911754abb3SJason A. Donenfeld #endif
7921754abb3SJason A. Donenfeld 
793d349ab99SJason A. Donenfeld 	for (i = 0, arch_bits = sizeof(entropy) * 8; i < ARRAY_SIZE(entropy);) {
794d349ab99SJason A. Donenfeld 		longs = arch_get_random_seed_longs(entropy, ARRAY_SIZE(entropy) - i);
795d349ab99SJason A. Donenfeld 		if (longs) {
796d349ab99SJason A. Donenfeld 			_mix_pool_bytes(entropy, sizeof(*entropy) * longs);
797d349ab99SJason A. Donenfeld 			i += longs;
798d349ab99SJason A. Donenfeld 			continue;
79992c653cfSJason A. Donenfeld 		}
800d349ab99SJason A. Donenfeld 		longs = arch_get_random_longs(entropy, ARRAY_SIZE(entropy) - i);
801d349ab99SJason A. Donenfeld 		if (longs) {
802d349ab99SJason A. Donenfeld 			_mix_pool_bytes(entropy, sizeof(*entropy) * longs);
803d349ab99SJason A. Donenfeld 			i += longs;
804d349ab99SJason A. Donenfeld 			continue;
805d349ab99SJason A. Donenfeld 		}
806d349ab99SJason A. Donenfeld 		entropy[0] = random_get_entropy();
807d349ab99SJason A. Donenfeld 		_mix_pool_bytes(entropy, sizeof(*entropy));
808d349ab99SJason A. Donenfeld 		arch_bits -= sizeof(*entropy) * 8;
809d349ab99SJason A. Donenfeld 		++i;
81092c653cfSJason A. Donenfeld 	}
811afba0b80SJason A. Donenfeld 	_mix_pool_bytes(&now, sizeof(now));
812afba0b80SJason A. Donenfeld 	_mix_pool_bytes(utsname(), sizeof(*(utsname())));
8132f14062bSJason A. Donenfeld 	_mix_pool_bytes(command_line, strlen(command_line));
8142f14062bSJason A. Donenfeld 	add_latent_entropy();
815655b2264STheodore Ts'o 
81660e5b288SJason A. Donenfeld 	/*
81760e5b288SJason A. Donenfeld 	 * If we were initialized by the bootloader before jump labels are
81860e5b288SJason A. Donenfeld 	 * initialized, then we should enable the static branch here, where
81960e5b288SJason A. Donenfeld 	 * it's guaranteed that jump labels have been initialized.
82060e5b288SJason A. Donenfeld 	 */
82160e5b288SJason A. Donenfeld 	if (!static_branch_likely(&crng_is_ready) && crng_init >= CRNG_READY)
82260e5b288SJason A. Donenfeld 		crng_set_ready(NULL);
82360e5b288SJason A. Donenfeld 
824e85c0fc1SJason A. Donenfeld 	if (crng_ready())
825e85c0fc1SJason A. Donenfeld 		crng_reseed();
82612e45a2aSJason A. Donenfeld 	else if (trust_cpu)
82777fc95f8SJason A. Donenfeld 		_credit_init_bits(arch_bits);
828775f4b29STheodore Ts'o 
829b7b67d13SJason A. Donenfeld 	WARN_ON(register_pm_notifier(&pm_notifier));
830b7b67d13SJason A. Donenfeld 
8314b758edaSJason A. Donenfeld 	WARN(!random_get_entropy(), "Missing cycle counter and fallback timer; RNG "
8324b758edaSJason A. Donenfeld 				    "entropy collection will consequently suffer.");
83392c653cfSJason A. Donenfeld 	return 0;
83492c653cfSJason A. Donenfeld }
8351da177e4SLinus Torvalds 
836a2080a67SLinus Torvalds /*
837e192be9dSTheodore Ts'o  * Add device- or boot-specific data to the input pool to help
838e192be9dSTheodore Ts'o  * initialize it.
839a2080a67SLinus Torvalds  *
840e192be9dSTheodore Ts'o  * None of this adds any entropy; it is meant to avoid the problem of
841e192be9dSTheodore Ts'o  * the entropy pool having similar initial state across largely
842e192be9dSTheodore Ts'o  * identical devices.
843a2080a67SLinus Torvalds  */
844a1940263SJason A. Donenfeld void add_device_randomness(const void *buf, size_t len)
845a2080a67SLinus Torvalds {
8464b758edaSJason A. Donenfeld 	unsigned long entropy = random_get_entropy();
8474b758edaSJason A. Donenfeld 	unsigned long flags;
848a2080a67SLinus Torvalds 
8493ef4cb2dSTheodore Ts'o 	spin_lock_irqsave(&input_pool.lock, flags);
8504b758edaSJason A. Donenfeld 	_mix_pool_bytes(&entropy, sizeof(entropy));
851a1940263SJason A. Donenfeld 	_mix_pool_bytes(buf, len);
8523ef4cb2dSTheodore Ts'o 	spin_unlock_irqrestore(&input_pool.lock, flags);
853a2080a67SLinus Torvalds }
854a2080a67SLinus Torvalds EXPORT_SYMBOL(add_device_randomness);
855a2080a67SLinus Torvalds 
85692c653cfSJason A. Donenfeld /*
85792c653cfSJason A. Donenfeld  * Interface for in-kernel drivers of true hardware RNGs.
85892c653cfSJason A. Donenfeld  * Those devices may produce endless random bits and will be throttled
85992c653cfSJason A. Donenfeld  * when our pool is full.
86092c653cfSJason A. Donenfeld  */
861a1940263SJason A. Donenfeld void add_hwgenerator_randomness(const void *buf, size_t len, size_t entropy)
86292c653cfSJason A. Donenfeld {
863a1940263SJason A. Donenfeld 	mix_pool_bytes(buf, len);
864e85c0fc1SJason A. Donenfeld 	credit_init_bits(entropy);
865e85c0fc1SJason A. Donenfeld 
866e85c0fc1SJason A. Donenfeld 	/*
867745558f9SDominik Brodowski 	 * Throttle writing to once every reseed interval, unless we're not yet
868d775335eSJason A. Donenfeld 	 * initialized or no entropy is credited.
869e85c0fc1SJason A. Donenfeld 	 */
870d775335eSJason A. Donenfeld 	if (!kthread_should_stop() && (crng_ready() || !entropy))
871745558f9SDominik Brodowski 		schedule_timeout_interruptible(crng_reseed_interval());
87292c653cfSJason A. Donenfeld }
87392c653cfSJason A. Donenfeld EXPORT_SYMBOL_GPL(add_hwgenerator_randomness);
87492c653cfSJason A. Donenfeld 
87592c653cfSJason A. Donenfeld /*
8765c3b747eSJason A. Donenfeld  * Handle random seed passed by bootloader, and credit it if
8775c3b747eSJason A. Donenfeld  * CONFIG_RANDOM_TRUST_BOOTLOADER is set.
87892c653cfSJason A. Donenfeld  */
87939e0f991SJason A. Donenfeld void __init add_bootloader_randomness(const void *buf, size_t len)
88092c653cfSJason A. Donenfeld {
881a1940263SJason A. Donenfeld 	mix_pool_bytes(buf, len);
882d97c68d1SJason A. Donenfeld 	if (trust_bootloader)
883a1940263SJason A. Donenfeld 		credit_init_bits(len * 8);
88492c653cfSJason A. Donenfeld }
88592c653cfSJason A. Donenfeld 
886a4107d34SJason A. Donenfeld #if IS_ENABLED(CONFIG_VMGENID)
887f3c2682bSJason A. Donenfeld static BLOCKING_NOTIFIER_HEAD(vmfork_chain);
888f3c2682bSJason A. Donenfeld 
889ae099e8eSJason A. Donenfeld /*
890ae099e8eSJason A. Donenfeld  * Handle a new unique VM ID, which is unique, not secret, so we
891ae099e8eSJason A. Donenfeld  * don't credit it, but we do immediately force a reseed after so
892ae099e8eSJason A. Donenfeld  * that it's used by the crng posthaste.
893ae099e8eSJason A. Donenfeld  */
894560181c2SJason A. Donenfeld void __cold add_vmfork_randomness(const void *unique_vm_id, size_t len)
895ae099e8eSJason A. Donenfeld {
896a1940263SJason A. Donenfeld 	add_device_randomness(unique_vm_id, len);
897ae099e8eSJason A. Donenfeld 	if (crng_ready()) {
898e85c0fc1SJason A. Donenfeld 		crng_reseed();
899ae099e8eSJason A. Donenfeld 		pr_notice("crng reseeded due to virtual machine fork\n");
900ae099e8eSJason A. Donenfeld 	}
901f3c2682bSJason A. Donenfeld 	blocking_notifier_call_chain(&vmfork_chain, 0, NULL);
902ae099e8eSJason A. Donenfeld }
903a4107d34SJason A. Donenfeld #if IS_MODULE(CONFIG_VMGENID)
904ae099e8eSJason A. Donenfeld EXPORT_SYMBOL_GPL(add_vmfork_randomness);
905a4107d34SJason A. Donenfeld #endif
906f3c2682bSJason A. Donenfeld 
907560181c2SJason A. Donenfeld int __cold register_random_vmfork_notifier(struct notifier_block *nb)
908f3c2682bSJason A. Donenfeld {
909f3c2682bSJason A. Donenfeld 	return blocking_notifier_chain_register(&vmfork_chain, nb);
910f3c2682bSJason A. Donenfeld }
911f3c2682bSJason A. Donenfeld EXPORT_SYMBOL_GPL(register_random_vmfork_notifier);
912f3c2682bSJason A. Donenfeld 
913560181c2SJason A. Donenfeld int __cold unregister_random_vmfork_notifier(struct notifier_block *nb)
914f3c2682bSJason A. Donenfeld {
915f3c2682bSJason A. Donenfeld 	return blocking_notifier_chain_unregister(&vmfork_chain, nb);
916f3c2682bSJason A. Donenfeld }
917f3c2682bSJason A. Donenfeld EXPORT_SYMBOL_GPL(unregister_random_vmfork_notifier);
918a4107d34SJason A. Donenfeld #endif
919ae099e8eSJason A. Donenfeld 
92092c653cfSJason A. Donenfeld struct fast_pool {
921f5eab0e2SJason A. Donenfeld 	unsigned long pool[4];
92292c653cfSJason A. Donenfeld 	unsigned long last;
9233191dd5aSJason A. Donenfeld 	unsigned int count;
924*748bc4ddSJason A. Donenfeld 	struct timer_list mix;
92592c653cfSJason A. Donenfeld };
92692c653cfSJason A. Donenfeld 
927*748bc4ddSJason A. Donenfeld static void mix_interrupt_randomness(struct timer_list *work);
928*748bc4ddSJason A. Donenfeld 
929f5eab0e2SJason A. Donenfeld static DEFINE_PER_CPU(struct fast_pool, irq_randomness) = {
930f5eab0e2SJason A. Donenfeld #ifdef CONFIG_64BIT
931e73aaae2SJason A. Donenfeld #define FASTMIX_PERM SIPHASH_PERMUTATION
932*748bc4ddSJason A. Donenfeld 	.pool = { SIPHASH_CONST_0, SIPHASH_CONST_1, SIPHASH_CONST_2, SIPHASH_CONST_3 },
933f5eab0e2SJason A. Donenfeld #else
934e73aaae2SJason A. Donenfeld #define FASTMIX_PERM HSIPHASH_PERMUTATION
935*748bc4ddSJason A. Donenfeld 	.pool = { HSIPHASH_CONST_0, HSIPHASH_CONST_1, HSIPHASH_CONST_2, HSIPHASH_CONST_3 },
936f5eab0e2SJason A. Donenfeld #endif
937*748bc4ddSJason A. Donenfeld 	.mix = __TIMER_INITIALIZER(mix_interrupt_randomness, 0)
938f5eab0e2SJason A. Donenfeld };
939f5eab0e2SJason A. Donenfeld 
94092c653cfSJason A. Donenfeld /*
941f5eab0e2SJason A. Donenfeld  * This is [Half]SipHash-1-x, starting from an empty key. Because
942f5eab0e2SJason A. Donenfeld  * the key is fixed, it assumes that its inputs are non-malicious,
943f5eab0e2SJason A. Donenfeld  * and therefore this has no security on its own. s represents the
9444b758edaSJason A. Donenfeld  * four-word SipHash state, while v represents a two-word input.
94592c653cfSJason A. Donenfeld  */
946791332b3SJason A. Donenfeld static void fast_mix(unsigned long s[4], unsigned long v1, unsigned long v2)
94792c653cfSJason A. Donenfeld {
948791332b3SJason A. Donenfeld 	s[3] ^= v1;
949e73aaae2SJason A. Donenfeld 	FASTMIX_PERM(s[0], s[1], s[2], s[3]);
950791332b3SJason A. Donenfeld 	s[0] ^= v1;
951791332b3SJason A. Donenfeld 	s[3] ^= v2;
952e73aaae2SJason A. Donenfeld 	FASTMIX_PERM(s[0], s[1], s[2], s[3]);
953791332b3SJason A. Donenfeld 	s[0] ^= v2;
954f5eab0e2SJason A. Donenfeld }
955775f4b29STheodore Ts'o 
9563191dd5aSJason A. Donenfeld #ifdef CONFIG_SMP
9573191dd5aSJason A. Donenfeld /*
9583191dd5aSJason A. Donenfeld  * This function is called when the CPU has just come online, with
9593191dd5aSJason A. Donenfeld  * entry CPUHP_AP_RANDOM_ONLINE, just after CPUHP_AP_WORKQUEUE_ONLINE.
9603191dd5aSJason A. Donenfeld  */
961560181c2SJason A. Donenfeld int __cold random_online_cpu(unsigned int cpu)
9623191dd5aSJason A. Donenfeld {
9633191dd5aSJason A. Donenfeld 	/*
9643191dd5aSJason A. Donenfeld 	 * During CPU shutdown and before CPU onlining, add_interrupt_
9653191dd5aSJason A. Donenfeld 	 * randomness() may schedule mix_interrupt_randomness(), and
9663191dd5aSJason A. Donenfeld 	 * set the MIX_INFLIGHT flag. However, because the worker can
9673191dd5aSJason A. Donenfeld 	 * be scheduled on a different CPU during this period, that
9683191dd5aSJason A. Donenfeld 	 * flag will never be cleared. For that reason, we zero out
9693191dd5aSJason A. Donenfeld 	 * the flag here, which runs just after workqueues are onlined
9703191dd5aSJason A. Donenfeld 	 * for the CPU again. This also has the effect of setting the
9713191dd5aSJason A. Donenfeld 	 * irq randomness count to zero so that new accumulated irqs
9723191dd5aSJason A. Donenfeld 	 * are fresh.
9733191dd5aSJason A. Donenfeld 	 */
9743191dd5aSJason A. Donenfeld 	per_cpu_ptr(&irq_randomness, cpu)->count = 0;
9753191dd5aSJason A. Donenfeld 	return 0;
9763191dd5aSJason A. Donenfeld }
9773191dd5aSJason A. Donenfeld #endif
9783191dd5aSJason A. Donenfeld 
979*748bc4ddSJason A. Donenfeld static void mix_interrupt_randomness(struct timer_list *work)
98058340f8eSJason A. Donenfeld {
98158340f8eSJason A. Donenfeld 	struct fast_pool *fast_pool = container_of(work, struct fast_pool, mix);
982f5eab0e2SJason A. Donenfeld 	/*
9834b758edaSJason A. Donenfeld 	 * The size of the copied stack pool is explicitly 2 longs so that we
9844b758edaSJason A. Donenfeld 	 * only ever ingest half of the siphash output each time, retaining
9854b758edaSJason A. Donenfeld 	 * the other half as the next "key" that carries over. The entropy is
9864b758edaSJason A. Donenfeld 	 * supposed to be sufficiently dispersed between bits so on average
9874b758edaSJason A. Donenfeld 	 * we don't wind up "losing" some.
988f5eab0e2SJason A. Donenfeld 	 */
9894b758edaSJason A. Donenfeld 	unsigned long pool[2];
990e3e33fc2SJason A. Donenfeld 	unsigned int count;
99158340f8eSJason A. Donenfeld 
99258340f8eSJason A. Donenfeld 	/* Check to see if we're running on the wrong CPU due to hotplug. */
99358340f8eSJason A. Donenfeld 	local_irq_disable();
99458340f8eSJason A. Donenfeld 	if (fast_pool != this_cpu_ptr(&irq_randomness)) {
99558340f8eSJason A. Donenfeld 		local_irq_enable();
99658340f8eSJason A. Donenfeld 		return;
99758340f8eSJason A. Donenfeld 	}
99858340f8eSJason A. Donenfeld 
99958340f8eSJason A. Donenfeld 	/*
100058340f8eSJason A. Donenfeld 	 * Copy the pool to the stack so that the mixer always has a
100158340f8eSJason A. Donenfeld 	 * consistent view, before we reenable irqs again.
100258340f8eSJason A. Donenfeld 	 */
1003f5eab0e2SJason A. Donenfeld 	memcpy(pool, fast_pool->pool, sizeof(pool));
1004e3e33fc2SJason A. Donenfeld 	count = fast_pool->count;
10053191dd5aSJason A. Donenfeld 	fast_pool->count = 0;
100658340f8eSJason A. Donenfeld 	fast_pool->last = jiffies;
100758340f8eSJason A. Donenfeld 	local_irq_enable();
100858340f8eSJason A. Donenfeld 
100958340f8eSJason A. Donenfeld 	mix_pool_bytes(pool, sizeof(pool));
1010e78a802aSJason A. Donenfeld 	credit_init_bits(clamp_t(unsigned int, (count & U16_MAX) / 64, 1, sizeof(pool) * 8));
1011c2a7de4fSJason A. Donenfeld 
101258340f8eSJason A. Donenfeld 	memzero_explicit(pool, sizeof(pool));
101358340f8eSJason A. Donenfeld }
101458340f8eSJason A. Donenfeld 
1015703f7066SSebastian Andrzej Siewior void add_interrupt_randomness(int irq)
10161da177e4SLinus Torvalds {
101758340f8eSJason A. Donenfeld 	enum { MIX_INFLIGHT = 1U << 31 };
10184b758edaSJason A. Donenfeld 	unsigned long entropy = random_get_entropy();
10191b2a1a7eSChristoph Lameter 	struct fast_pool *fast_pool = this_cpu_ptr(&irq_randomness);
1020775f4b29STheodore Ts'o 	struct pt_regs *regs = get_irq_regs();
102158340f8eSJason A. Donenfeld 	unsigned int new_count;
10223060d6feSYinghai Lu 
1023791332b3SJason A. Donenfeld 	fast_mix(fast_pool->pool, entropy,
1024791332b3SJason A. Donenfeld 		 (regs ? instruction_pointer(regs) : _RET_IP_) ^ swab(irq));
10253191dd5aSJason A. Donenfeld 	new_count = ++fast_pool->count;
1026775f4b29STheodore Ts'o 
102758340f8eSJason A. Donenfeld 	if (new_count & MIX_INFLIGHT)
10281da177e4SLinus Torvalds 		return;
1029840f9507STheodore Ts'o 
1030534d2eafSJason A. Donenfeld 	if (new_count < 1024 && !time_is_before_jiffies(fast_pool->last + HZ))
10311da177e4SLinus Torvalds 		return;
10321da177e4SLinus Torvalds 
10333191dd5aSJason A. Donenfeld 	fast_pool->count |= MIX_INFLIGHT;
1034*748bc4ddSJason A. Donenfeld 	if (!timer_pending(&fast_pool->mix)) {
1035*748bc4ddSJason A. Donenfeld 		fast_pool->mix.expires = jiffies;
1036*748bc4ddSJason A. Donenfeld 		add_timer_on(&fast_pool->mix, raw_smp_processor_id());
1037*748bc4ddSJason A. Donenfeld 	}
10381da177e4SLinus Torvalds }
10394b44f2d1SStephan Mueller EXPORT_SYMBOL_GPL(add_interrupt_randomness);
10401da177e4SLinus Torvalds 
1041a4b5c26bSJason A. Donenfeld /* There is one of these per entropy source */
1042a4b5c26bSJason A. Donenfeld struct timer_rand_state {
1043a4b5c26bSJason A. Donenfeld 	unsigned long last_time;
1044a4b5c26bSJason A. Donenfeld 	long last_delta, last_delta2;
1045a4b5c26bSJason A. Donenfeld };
1046a4b5c26bSJason A. Donenfeld 
1047a4b5c26bSJason A. Donenfeld /*
1048a4b5c26bSJason A. Donenfeld  * This function adds entropy to the entropy "pool" by using timing
1049a4b5c26bSJason A. Donenfeld  * delays. It uses the timer_rand_state structure to make an estimate
1050e3e33fc2SJason A. Donenfeld  * of how many bits of entropy this call has added to the pool. The
1051e3e33fc2SJason A. Donenfeld  * value "num" is also added to the pool; it should somehow describe
1052e3e33fc2SJason A. Donenfeld  * the type of event that just happened.
1053a4b5c26bSJason A. Donenfeld  */
1054a4b5c26bSJason A. Donenfeld static void add_timer_randomness(struct timer_rand_state *state, unsigned int num)
1055a4b5c26bSJason A. Donenfeld {
1056a4b5c26bSJason A. Donenfeld 	unsigned long entropy = random_get_entropy(), now = jiffies, flags;
1057a4b5c26bSJason A. Donenfeld 	long delta, delta2, delta3;
1058e3e33fc2SJason A. Donenfeld 	unsigned int bits;
1059a4b5c26bSJason A. Donenfeld 
1060e3e33fc2SJason A. Donenfeld 	/*
1061e3e33fc2SJason A. Donenfeld 	 * If we're in a hard IRQ, add_interrupt_randomness() will be called
1062e3e33fc2SJason A. Donenfeld 	 * sometime after, so mix into the fast pool.
1063e3e33fc2SJason A. Donenfeld 	 */
1064e3e33fc2SJason A. Donenfeld 	if (in_hardirq()) {
1065791332b3SJason A. Donenfeld 		fast_mix(this_cpu_ptr(&irq_randomness)->pool, entropy, num);
1066e3e33fc2SJason A. Donenfeld 	} else {
1067a4b5c26bSJason A. Donenfeld 		spin_lock_irqsave(&input_pool.lock, flags);
1068a4b5c26bSJason A. Donenfeld 		_mix_pool_bytes(&entropy, sizeof(entropy));
1069a4b5c26bSJason A. Donenfeld 		_mix_pool_bytes(&num, sizeof(num));
1070a4b5c26bSJason A. Donenfeld 		spin_unlock_irqrestore(&input_pool.lock, flags);
1071e3e33fc2SJason A. Donenfeld 	}
1072a4b5c26bSJason A. Donenfeld 
1073a4b5c26bSJason A. Donenfeld 	if (crng_ready())
1074a4b5c26bSJason A. Donenfeld 		return;
1075a4b5c26bSJason A. Donenfeld 
1076a4b5c26bSJason A. Donenfeld 	/*
1077a4b5c26bSJason A. Donenfeld 	 * Calculate number of bits of randomness we probably added.
1078a4b5c26bSJason A. Donenfeld 	 * We take into account the first, second and third-order deltas
1079a4b5c26bSJason A. Donenfeld 	 * in order to make our estimate.
1080a4b5c26bSJason A. Donenfeld 	 */
1081a4b5c26bSJason A. Donenfeld 	delta = now - READ_ONCE(state->last_time);
1082a4b5c26bSJason A. Donenfeld 	WRITE_ONCE(state->last_time, now);
1083a4b5c26bSJason A. Donenfeld 
1084a4b5c26bSJason A. Donenfeld 	delta2 = delta - READ_ONCE(state->last_delta);
1085a4b5c26bSJason A. Donenfeld 	WRITE_ONCE(state->last_delta, delta);
1086a4b5c26bSJason A. Donenfeld 
1087a4b5c26bSJason A. Donenfeld 	delta3 = delta2 - READ_ONCE(state->last_delta2);
1088a4b5c26bSJason A. Donenfeld 	WRITE_ONCE(state->last_delta2, delta2);
1089a4b5c26bSJason A. Donenfeld 
1090a4b5c26bSJason A. Donenfeld 	if (delta < 0)
1091a4b5c26bSJason A. Donenfeld 		delta = -delta;
1092a4b5c26bSJason A. Donenfeld 	if (delta2 < 0)
1093a4b5c26bSJason A. Donenfeld 		delta2 = -delta2;
1094a4b5c26bSJason A. Donenfeld 	if (delta3 < 0)
1095a4b5c26bSJason A. Donenfeld 		delta3 = -delta3;
1096a4b5c26bSJason A. Donenfeld 	if (delta > delta2)
1097a4b5c26bSJason A. Donenfeld 		delta = delta2;
1098a4b5c26bSJason A. Donenfeld 	if (delta > delta3)
1099a4b5c26bSJason A. Donenfeld 		delta = delta3;
1100a4b5c26bSJason A. Donenfeld 
1101a4b5c26bSJason A. Donenfeld 	/*
1102e3e33fc2SJason A. Donenfeld 	 * delta is now minimum absolute delta. Round down by 1 bit
1103e3e33fc2SJason A. Donenfeld 	 * on general principles, and limit entropy estimate to 11 bits.
1104a4b5c26bSJason A. Donenfeld 	 */
1105e3e33fc2SJason A. Donenfeld 	bits = min(fls(delta >> 1), 11);
1106e3e33fc2SJason A. Donenfeld 
1107e3e33fc2SJason A. Donenfeld 	/*
1108e3e33fc2SJason A. Donenfeld 	 * As mentioned above, if we're in a hard IRQ, add_interrupt_randomness()
1109e3e33fc2SJason A. Donenfeld 	 * will run after this, which uses a different crediting scheme of 1 bit
1110e3e33fc2SJason A. Donenfeld 	 * per every 64 interrupts. In order to let that function do accounting
1111e3e33fc2SJason A. Donenfeld 	 * close to the one in this function, we credit a full 64/64 bit per bit,
1112e3e33fc2SJason A. Donenfeld 	 * and then subtract one to account for the extra one added.
1113e3e33fc2SJason A. Donenfeld 	 */
1114e3e33fc2SJason A. Donenfeld 	if (in_hardirq())
1115e3e33fc2SJason A. Donenfeld 		this_cpu_ptr(&irq_randomness)->count += max(1u, bits * 64) - 1;
1116e3e33fc2SJason A. Donenfeld 	else
1117560181c2SJason A. Donenfeld 		_credit_init_bits(bits);
1118a4b5c26bSJason A. Donenfeld }
1119a4b5c26bSJason A. Donenfeld 
1120a1940263SJason A. Donenfeld void add_input_randomness(unsigned int type, unsigned int code, unsigned int value)
1121a4b5c26bSJason A. Donenfeld {
1122a4b5c26bSJason A. Donenfeld 	static unsigned char last_value;
1123a4b5c26bSJason A. Donenfeld 	static struct timer_rand_state input_timer_state = { INITIAL_JIFFIES };
1124a4b5c26bSJason A. Donenfeld 
1125a4b5c26bSJason A. Donenfeld 	/* Ignore autorepeat and the like. */
1126a4b5c26bSJason A. Donenfeld 	if (value == last_value)
1127a4b5c26bSJason A. Donenfeld 		return;
1128a4b5c26bSJason A. Donenfeld 
1129a4b5c26bSJason A. Donenfeld 	last_value = value;
1130a4b5c26bSJason A. Donenfeld 	add_timer_randomness(&input_timer_state,
1131a4b5c26bSJason A. Donenfeld 			     (type << 4) ^ code ^ (code >> 4) ^ value);
1132a4b5c26bSJason A. Donenfeld }
1133a4b5c26bSJason A. Donenfeld EXPORT_SYMBOL_GPL(add_input_randomness);
1134a4b5c26bSJason A. Donenfeld 
1135a4b5c26bSJason A. Donenfeld #ifdef CONFIG_BLOCK
1136a4b5c26bSJason A. Donenfeld void add_disk_randomness(struct gendisk *disk)
1137a4b5c26bSJason A. Donenfeld {
1138a4b5c26bSJason A. Donenfeld 	if (!disk || !disk->random)
1139a4b5c26bSJason A. Donenfeld 		return;
1140a4b5c26bSJason A. Donenfeld 	/* First major is 1, so we get >= 0x200 here. */
1141a4b5c26bSJason A. Donenfeld 	add_timer_randomness(disk->random, 0x100 + disk_devt(disk));
1142a4b5c26bSJason A. Donenfeld }
1143a4b5c26bSJason A. Donenfeld EXPORT_SYMBOL_GPL(add_disk_randomness);
1144a4b5c26bSJason A. Donenfeld 
1145560181c2SJason A. Donenfeld void __cold rand_initialize_disk(struct gendisk *disk)
1146a4b5c26bSJason A. Donenfeld {
1147a4b5c26bSJason A. Donenfeld 	struct timer_rand_state *state;
1148a4b5c26bSJason A. Donenfeld 
1149a4b5c26bSJason A. Donenfeld 	/*
1150a4b5c26bSJason A. Donenfeld 	 * If kzalloc returns null, we just won't use that entropy
1151a4b5c26bSJason A. Donenfeld 	 * source.
1152a4b5c26bSJason A. Donenfeld 	 */
1153a4b5c26bSJason A. Donenfeld 	state = kzalloc(sizeof(struct timer_rand_state), GFP_KERNEL);
1154a4b5c26bSJason A. Donenfeld 	if (state) {
1155a4b5c26bSJason A. Donenfeld 		state->last_time = INITIAL_JIFFIES;
1156a4b5c26bSJason A. Donenfeld 		disk->random = state;
1157a4b5c26bSJason A. Donenfeld 	}
1158a4b5c26bSJason A. Donenfeld }
1159a4b5c26bSJason A. Donenfeld #endif
1160a4b5c26bSJason A. Donenfeld 
116178c768e6SJason A. Donenfeld struct entropy_timer_state {
116278c768e6SJason A. Donenfeld 	unsigned long entropy;
116378c768e6SJason A. Donenfeld 	struct timer_list timer;
116478c768e6SJason A. Donenfeld 	unsigned int samples, samples_per_bit;
116578c768e6SJason A. Donenfeld };
116678c768e6SJason A. Donenfeld 
11671da177e4SLinus Torvalds /*
116850ee7529SLinus Torvalds  * Each time the timer fires, we expect that we got an unpredictable
116950ee7529SLinus Torvalds  * jump in the cycle counter. Even if the timer is running on another
117050ee7529SLinus Torvalds  * CPU, the timer activity will be touching the stack of the CPU that is
117150ee7529SLinus Torvalds  * generating entropy..
117250ee7529SLinus Torvalds  *
117350ee7529SLinus Torvalds  * Note that we don't re-arm the timer in the timer itself - we are
117450ee7529SLinus Torvalds  * happy to be scheduled away, since that just makes the load more
117550ee7529SLinus Torvalds  * complex, but we do not want the timer to keep ticking unless the
117650ee7529SLinus Torvalds  * entropy loop is running.
117750ee7529SLinus Torvalds  *
117850ee7529SLinus Torvalds  * So the re-arming always happens in the entropy loop itself.
117950ee7529SLinus Torvalds  */
1180560181c2SJason A. Donenfeld static void __cold entropy_timer(struct timer_list *timer)
118150ee7529SLinus Torvalds {
118278c768e6SJason A. Donenfeld 	struct entropy_timer_state *state = container_of(timer, struct entropy_timer_state, timer);
118378c768e6SJason A. Donenfeld 
118478c768e6SJason A. Donenfeld 	if (++state->samples == state->samples_per_bit) {
1185e85c0fc1SJason A. Donenfeld 		credit_init_bits(1);
118678c768e6SJason A. Donenfeld 		state->samples = 0;
118778c768e6SJason A. Donenfeld 	}
118850ee7529SLinus Torvalds }
118950ee7529SLinus Torvalds 
119050ee7529SLinus Torvalds /*
119150ee7529SLinus Torvalds  * If we have an actual cycle counter, see if we can
119250ee7529SLinus Torvalds  * generate enough entropy with timing noise
119350ee7529SLinus Torvalds  */
1194560181c2SJason A. Donenfeld static void __cold try_to_generate_entropy(void)
119550ee7529SLinus Torvalds {
1196829d680eSJason A. Donenfeld 	enum { NUM_TRIAL_SAMPLES = 8192, MAX_SAMPLES_PER_BIT = HZ / 30 };
119778c768e6SJason A. Donenfeld 	struct entropy_timer_state stack;
119878c768e6SJason A. Donenfeld 	unsigned int i, num_different = 0;
119978c768e6SJason A. Donenfeld 	unsigned long last = random_get_entropy();
120050ee7529SLinus Torvalds 
120178c768e6SJason A. Donenfeld 	for (i = 0; i < NUM_TRIAL_SAMPLES - 1; ++i) {
12024b758edaSJason A. Donenfeld 		stack.entropy = random_get_entropy();
120378c768e6SJason A. Donenfeld 		if (stack.entropy != last)
120478c768e6SJason A. Donenfeld 			++num_different;
120578c768e6SJason A. Donenfeld 		last = stack.entropy;
120678c768e6SJason A. Donenfeld 	}
120778c768e6SJason A. Donenfeld 	stack.samples_per_bit = DIV_ROUND_UP(NUM_TRIAL_SAMPLES, num_different + 1);
120878c768e6SJason A. Donenfeld 	if (stack.samples_per_bit > MAX_SAMPLES_PER_BIT)
120950ee7529SLinus Torvalds 		return;
121050ee7529SLinus Torvalds 
121178c768e6SJason A. Donenfeld 	stack.samples = 0;
121250ee7529SLinus Torvalds 	timer_setup_on_stack(&stack.timer, entropy_timer, 0);
12133e504d20SJason A. Donenfeld 	while (!crng_ready() && !signal_pending(current)) {
121450ee7529SLinus Torvalds 		if (!timer_pending(&stack.timer))
121550ee7529SLinus Torvalds 			mod_timer(&stack.timer, jiffies + 1);
12164b758edaSJason A. Donenfeld 		mix_pool_bytes(&stack.entropy, sizeof(stack.entropy));
121750ee7529SLinus Torvalds 		schedule();
12184b758edaSJason A. Donenfeld 		stack.entropy = random_get_entropy();
121950ee7529SLinus Torvalds 	}
122050ee7529SLinus Torvalds 
122150ee7529SLinus Torvalds 	del_timer_sync(&stack.timer);
122250ee7529SLinus Torvalds 	destroy_timer_on_stack(&stack.timer);
12234b758edaSJason A. Donenfeld 	mix_pool_bytes(&stack.entropy, sizeof(stack.entropy));
122450ee7529SLinus Torvalds }
122550ee7529SLinus Torvalds 
1226a6adf8e7SJason A. Donenfeld 
1227a6adf8e7SJason A. Donenfeld /**********************************************************************
1228a6adf8e7SJason A. Donenfeld  *
1229a6adf8e7SJason A. Donenfeld  * Userspace reader/writer interfaces.
1230a6adf8e7SJason A. Donenfeld  *
1231a6adf8e7SJason A. Donenfeld  * getrandom(2) is the primary modern interface into the RNG and should
1232a6adf8e7SJason A. Donenfeld  * be used in preference to anything else.
1233a6adf8e7SJason A. Donenfeld  *
12340313bc27SLinus Torvalds  * Reading from /dev/random has the same functionality as calling
12350313bc27SLinus Torvalds  * getrandom(2) with flags=0. In earlier versions, however, it had
12360313bc27SLinus Torvalds  * vastly different semantics and should therefore be avoided, to
12370313bc27SLinus Torvalds  * prevent backwards compatibility issues.
12380313bc27SLinus Torvalds  *
12390313bc27SLinus Torvalds  * Reading from /dev/urandom has the same functionality as calling
12400313bc27SLinus Torvalds  * getrandom(2) with flags=GRND_INSECURE. Because it does not block
12410313bc27SLinus Torvalds  * waiting for the RNG to be ready, it should not be used.
1242a6adf8e7SJason A. Donenfeld  *
1243a6adf8e7SJason A. Donenfeld  * Writing to either /dev/random or /dev/urandom adds entropy to
1244a6adf8e7SJason A. Donenfeld  * the input pool but does not credit it.
1245a6adf8e7SJason A. Donenfeld  *
12460313bc27SLinus Torvalds  * Polling on /dev/random indicates when the RNG is initialized, on
12470313bc27SLinus Torvalds  * the read side, and when it wants new entropy, on the write side.
1248a6adf8e7SJason A. Donenfeld  *
1249a6adf8e7SJason A. Donenfeld  * Both /dev/random and /dev/urandom have the same set of ioctls for
1250a6adf8e7SJason A. Donenfeld  * adding entropy, getting the entropy count, zeroing the count, and
1251a6adf8e7SJason A. Donenfeld  * reseeding the crng.
1252a6adf8e7SJason A. Donenfeld  *
1253a6adf8e7SJason A. Donenfeld  **********************************************************************/
1254a6adf8e7SJason A. Donenfeld 
1255a1940263SJason A. Donenfeld SYSCALL_DEFINE3(getrandom, char __user *, ubuf, size_t, len, unsigned int, flags)
12561da177e4SLinus Torvalds {
12571b388e77SJens Axboe 	struct iov_iter iter;
12581b388e77SJens Axboe 	struct iovec iov;
12591b388e77SJens Axboe 	int ret;
12601b388e77SJens Axboe 
1261a6adf8e7SJason A. Donenfeld 	if (flags & ~(GRND_NONBLOCK | GRND_RANDOM | GRND_INSECURE))
1262a6adf8e7SJason A. Donenfeld 		return -EINVAL;
1263301f0595STheodore Ts'o 
1264a6adf8e7SJason A. Donenfeld 	/*
1265a6adf8e7SJason A. Donenfeld 	 * Requesting insecure and blocking randomness at the same time makes
1266a6adf8e7SJason A. Donenfeld 	 * no sense.
1267a6adf8e7SJason A. Donenfeld 	 */
1268a6adf8e7SJason A. Donenfeld 	if ((flags & (GRND_INSECURE | GRND_RANDOM)) == (GRND_INSECURE | GRND_RANDOM))
1269a6adf8e7SJason A. Donenfeld 		return -EINVAL;
1270c6f1deb1SAndy Lutomirski 
1271f5bda35fSJason A. Donenfeld 	if (!crng_ready() && !(flags & GRND_INSECURE)) {
1272a6adf8e7SJason A. Donenfeld 		if (flags & GRND_NONBLOCK)
1273a6adf8e7SJason A. Donenfeld 			return -EAGAIN;
127430c08efeSAndy Lutomirski 		ret = wait_for_random_bytes();
1275a6adf8e7SJason A. Donenfeld 		if (unlikely(ret))
127630c08efeSAndy Lutomirski 			return ret;
1277a6adf8e7SJason A. Donenfeld 	}
12781b388e77SJens Axboe 
12791b388e77SJens Axboe 	ret = import_single_range(READ, ubuf, len, &iov, &iter);
12801b388e77SJens Axboe 	if (unlikely(ret))
12811b388e77SJens Axboe 		return ret;
12821b388e77SJens Axboe 	return get_random_bytes_user(&iter);
128330c08efeSAndy Lutomirski }
128430c08efeSAndy Lutomirski 
1285248045b8SJason A. Donenfeld static __poll_t random_poll(struct file *file, poll_table *wait)
128689b310a2SChristoph Hellwig {
128730c08efeSAndy Lutomirski 	poll_wait(file, &crng_init_wait, wait);
1288e85c0fc1SJason A. Donenfeld 	return crng_ready() ? EPOLLIN | EPOLLRDNORM : EPOLLOUT | EPOLLWRNORM;
12891da177e4SLinus Torvalds }
12901da177e4SLinus Torvalds 
12911ce6c8d6SJason A. Donenfeld static ssize_t write_pool_user(struct iov_iter *iter)
12927f397dcdSMatt Mackall {
129304ec96b7SJason A. Donenfeld 	u8 block[BLAKE2S_BLOCK_SIZE];
129422b0a222SJens Axboe 	ssize_t ret = 0;
129522b0a222SJens Axboe 	size_t copied;
12967f397dcdSMatt Mackall 
129722b0a222SJens Axboe 	if (unlikely(!iov_iter_count(iter)))
129822b0a222SJens Axboe 		return 0;
129922b0a222SJens Axboe 
130022b0a222SJens Axboe 	for (;;) {
130122b0a222SJens Axboe 		copied = copy_from_iter(block, sizeof(block), iter);
130222b0a222SJens Axboe 		ret += copied;
130322b0a222SJens Axboe 		mix_pool_bytes(block, copied);
130422b0a222SJens Axboe 		if (!iov_iter_count(iter) || copied != sizeof(block))
130522b0a222SJens Axboe 			break;
13061ce6c8d6SJason A. Donenfeld 
13071ce6c8d6SJason A. Donenfeld 		BUILD_BUG_ON(PAGE_SIZE % sizeof(block) != 0);
13081ce6c8d6SJason A. Donenfeld 		if (ret % PAGE_SIZE == 0) {
13091ce6c8d6SJason A. Donenfeld 			if (signal_pending(current))
13101ce6c8d6SJason A. Donenfeld 				break;
131191f3f1e3SMatt Mackall 			cond_resched();
13127f397dcdSMatt Mackall 		}
13131ce6c8d6SJason A. Donenfeld 	}
13147f397dcdSMatt Mackall 
13157b5164fbSJason A. Donenfeld 	memzero_explicit(block, sizeof(block));
131622b0a222SJens Axboe 	return ret ? ret : -EFAULT;
13177f397dcdSMatt Mackall }
13187f397dcdSMatt Mackall 
131922b0a222SJens Axboe static ssize_t random_write_iter(struct kiocb *kiocb, struct iov_iter *iter)
13201da177e4SLinus Torvalds {
13211ce6c8d6SJason A. Donenfeld 	return write_pool_user(iter);
13221da177e4SLinus Torvalds }
13231da177e4SLinus Torvalds 
13241b388e77SJens Axboe static ssize_t urandom_read_iter(struct kiocb *kiocb, struct iov_iter *iter)
13250313bc27SLinus Torvalds {
13260313bc27SLinus Torvalds 	static int maxwarn = 10;
13270313bc27SLinus Torvalds 
132848bff105SJason A. Donenfeld 	/*
132948bff105SJason A. Donenfeld 	 * Opportunistically attempt to initialize the RNG on platforms that
133048bff105SJason A. Donenfeld 	 * have fast cycle counters, but don't (for now) require it to succeed.
133148bff105SJason A. Donenfeld 	 */
133248bff105SJason A. Donenfeld 	if (!crng_ready())
133348bff105SJason A. Donenfeld 		try_to_generate_entropy();
133448bff105SJason A. Donenfeld 
1335cc1e127bSJason A. Donenfeld 	if (!crng_ready()) {
1336cc1e127bSJason A. Donenfeld 		if (!ratelimit_disable && maxwarn <= 0)
1337cc1e127bSJason A. Donenfeld 			++urandom_warning.missed;
1338cc1e127bSJason A. Donenfeld 		else if (ratelimit_disable || __ratelimit(&urandom_warning)) {
1339cc1e127bSJason A. Donenfeld 			--maxwarn;
13401b388e77SJens Axboe 			pr_notice("%s: uninitialized urandom read (%zu bytes read)\n",
13411b388e77SJens Axboe 				  current->comm, iov_iter_count(iter));
13420313bc27SLinus Torvalds 		}
1343cc1e127bSJason A. Donenfeld 	}
13440313bc27SLinus Torvalds 
13451b388e77SJens Axboe 	return get_random_bytes_user(iter);
13460313bc27SLinus Torvalds }
13470313bc27SLinus Torvalds 
13481b388e77SJens Axboe static ssize_t random_read_iter(struct kiocb *kiocb, struct iov_iter *iter)
1349a6adf8e7SJason A. Donenfeld {
1350a6adf8e7SJason A. Donenfeld 	int ret;
1351a6adf8e7SJason A. Donenfeld 
1352cd4f24aeSJason A. Donenfeld 	if (!crng_ready() &&
1353cd4f24aeSJason A. Donenfeld 	    ((kiocb->ki_flags & (IOCB_NOWAIT | IOCB_NOIO)) ||
1354cd4f24aeSJason A. Donenfeld 	     (kiocb->ki_filp->f_flags & O_NONBLOCK)))
1355cd4f24aeSJason A. Donenfeld 		return -EAGAIN;
1356cd4f24aeSJason A. Donenfeld 
1357a6adf8e7SJason A. Donenfeld 	ret = wait_for_random_bytes();
1358a6adf8e7SJason A. Donenfeld 	if (ret != 0)
1359a6adf8e7SJason A. Donenfeld 		return ret;
13601b388e77SJens Axboe 	return get_random_bytes_user(iter);
1361a6adf8e7SJason A. Donenfeld }
1362a6adf8e7SJason A. Donenfeld 
136343ae4860SMatt Mackall static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
13641da177e4SLinus Torvalds {
13651da177e4SLinus Torvalds 	int __user *p = (int __user *)arg;
136622b0a222SJens Axboe 	int ent_count;
13671da177e4SLinus Torvalds 
13681da177e4SLinus Torvalds 	switch (cmd) {
13691da177e4SLinus Torvalds 	case RNDGETENTCNT:
1370a6adf8e7SJason A. Donenfeld 		/* Inherently racy, no point locking. */
1371e85c0fc1SJason A. Donenfeld 		if (put_user(input_pool.init_bits, p))
13721da177e4SLinus Torvalds 			return -EFAULT;
13731da177e4SLinus Torvalds 		return 0;
13741da177e4SLinus Torvalds 	case RNDADDTOENTCNT:
13751da177e4SLinus Torvalds 		if (!capable(CAP_SYS_ADMIN))
13761da177e4SLinus Torvalds 			return -EPERM;
13771da177e4SLinus Torvalds 		if (get_user(ent_count, p))
13781da177e4SLinus Torvalds 			return -EFAULT;
1379a49c010eSJason A. Donenfeld 		if (ent_count < 0)
1380a49c010eSJason A. Donenfeld 			return -EINVAL;
1381e85c0fc1SJason A. Donenfeld 		credit_init_bits(ent_count);
1382a49c010eSJason A. Donenfeld 		return 0;
138322b0a222SJens Axboe 	case RNDADDENTROPY: {
138422b0a222SJens Axboe 		struct iov_iter iter;
138522b0a222SJens Axboe 		struct iovec iov;
138622b0a222SJens Axboe 		ssize_t ret;
138722b0a222SJens Axboe 		int len;
138822b0a222SJens Axboe 
13891da177e4SLinus Torvalds 		if (!capable(CAP_SYS_ADMIN))
13901da177e4SLinus Torvalds 			return -EPERM;
13911da177e4SLinus Torvalds 		if (get_user(ent_count, p++))
13921da177e4SLinus Torvalds 			return -EFAULT;
13931da177e4SLinus Torvalds 		if (ent_count < 0)
13941da177e4SLinus Torvalds 			return -EINVAL;
139522b0a222SJens Axboe 		if (get_user(len, p++))
13961da177e4SLinus Torvalds 			return -EFAULT;
139722b0a222SJens Axboe 		ret = import_single_range(WRITE, p, len, &iov, &iter);
139822b0a222SJens Axboe 		if (unlikely(ret))
139922b0a222SJens Axboe 			return ret;
14001ce6c8d6SJason A. Donenfeld 		ret = write_pool_user(&iter);
140122b0a222SJens Axboe 		if (unlikely(ret < 0))
140222b0a222SJens Axboe 			return ret;
140322b0a222SJens Axboe 		/* Since we're crediting, enforce that it was all written into the pool. */
140422b0a222SJens Axboe 		if (unlikely(ret != len))
140522b0a222SJens Axboe 			return -EFAULT;
1406e85c0fc1SJason A. Donenfeld 		credit_init_bits(ent_count);
1407a49c010eSJason A. Donenfeld 		return 0;
140822b0a222SJens Axboe 	}
14091da177e4SLinus Torvalds 	case RNDZAPENTCNT:
14101da177e4SLinus Torvalds 	case RNDCLEARPOOL:
1411e85c0fc1SJason A. Donenfeld 		/* No longer has any effect. */
14121da177e4SLinus Torvalds 		if (!capable(CAP_SYS_ADMIN))
14131da177e4SLinus Torvalds 			return -EPERM;
14141da177e4SLinus Torvalds 		return 0;
1415d848e5f8STheodore Ts'o 	case RNDRESEEDCRNG:
1416d848e5f8STheodore Ts'o 		if (!capable(CAP_SYS_ADMIN))
1417d848e5f8STheodore Ts'o 			return -EPERM;
1418a96cfe2dSJason A. Donenfeld 		if (!crng_ready())
1419d848e5f8STheodore Ts'o 			return -ENODATA;
1420e85c0fc1SJason A. Donenfeld 		crng_reseed();
1421d848e5f8STheodore Ts'o 		return 0;
14221da177e4SLinus Torvalds 	default:
14231da177e4SLinus Torvalds 		return -EINVAL;
14241da177e4SLinus Torvalds 	}
14251da177e4SLinus Torvalds }
14261da177e4SLinus Torvalds 
14279a6f70bbSJeff Dike static int random_fasync(int fd, struct file *filp, int on)
14289a6f70bbSJeff Dike {
14299a6f70bbSJeff Dike 	return fasync_helper(fd, filp, on, &fasync);
14309a6f70bbSJeff Dike }
14319a6f70bbSJeff Dike 
14322b8693c0SArjan van de Ven const struct file_operations random_fops = {
14331b388e77SJens Axboe 	.read_iter = random_read_iter,
143422b0a222SJens Axboe 	.write_iter = random_write_iter,
1435a11e1d43SLinus Torvalds 	.poll = random_poll,
143643ae4860SMatt Mackall 	.unlocked_ioctl = random_ioctl,
1437507e4e2bSArnd Bergmann 	.compat_ioctl = compat_ptr_ioctl,
14389a6f70bbSJeff Dike 	.fasync = random_fasync,
14396038f373SArnd Bergmann 	.llseek = noop_llseek,
144079025e72SJens Axboe 	.splice_read = generic_file_splice_read,
144179025e72SJens Axboe 	.splice_write = iter_file_splice_write,
14421da177e4SLinus Torvalds };
14431da177e4SLinus Torvalds 
14440313bc27SLinus Torvalds const struct file_operations urandom_fops = {
14451b388e77SJens Axboe 	.read_iter = urandom_read_iter,
144622b0a222SJens Axboe 	.write_iter = random_write_iter,
14470313bc27SLinus Torvalds 	.unlocked_ioctl = random_ioctl,
14480313bc27SLinus Torvalds 	.compat_ioctl = compat_ptr_ioctl,
14490313bc27SLinus Torvalds 	.fasync = random_fasync,
14500313bc27SLinus Torvalds 	.llseek = noop_llseek,
145179025e72SJens Axboe 	.splice_read = generic_file_splice_read,
145279025e72SJens Axboe 	.splice_write = iter_file_splice_write,
14530313bc27SLinus Torvalds };
14540313bc27SLinus Torvalds 
14550deff3c4SJason A. Donenfeld 
14561da177e4SLinus Torvalds /********************************************************************
14571da177e4SLinus Torvalds  *
14580deff3c4SJason A. Donenfeld  * Sysctl interface.
14590deff3c4SJason A. Donenfeld  *
14600deff3c4SJason A. Donenfeld  * These are partly unused legacy knobs with dummy values to not break
14610deff3c4SJason A. Donenfeld  * userspace and partly still useful things. They are usually accessible
14620deff3c4SJason A. Donenfeld  * in /proc/sys/kernel/random/ and are as follows:
14630deff3c4SJason A. Donenfeld  *
14640deff3c4SJason A. Donenfeld  * - boot_id - a UUID representing the current boot.
14650deff3c4SJason A. Donenfeld  *
14660deff3c4SJason A. Donenfeld  * - uuid - a random UUID, different each time the file is read.
14670deff3c4SJason A. Donenfeld  *
14680deff3c4SJason A. Donenfeld  * - poolsize - the number of bits of entropy that the input pool can
14690deff3c4SJason A. Donenfeld  *   hold, tied to the POOL_BITS constant.
14700deff3c4SJason A. Donenfeld  *
14710deff3c4SJason A. Donenfeld  * - entropy_avail - the number of bits of entropy currently in the
14720deff3c4SJason A. Donenfeld  *   input pool. Always <= poolsize.
14730deff3c4SJason A. Donenfeld  *
14740deff3c4SJason A. Donenfeld  * - write_wakeup_threshold - the amount of entropy in the input pool
14750deff3c4SJason A. Donenfeld  *   below which write polls to /dev/random will unblock, requesting
1476e3d2c5e7SJason A. Donenfeld  *   more entropy, tied to the POOL_READY_BITS constant. It is writable
14770deff3c4SJason A. Donenfeld  *   to avoid breaking old userspaces, but writing to it does not
14780deff3c4SJason A. Donenfeld  *   change any behavior of the RNG.
14790deff3c4SJason A. Donenfeld  *
1480d0efdf35SJason A. Donenfeld  * - urandom_min_reseed_secs - fixed to the value CRNG_RESEED_INTERVAL.
14810deff3c4SJason A. Donenfeld  *   It is writable to avoid breaking old userspaces, but writing
14820deff3c4SJason A. Donenfeld  *   to it does not change any behavior of the RNG.
14831da177e4SLinus Torvalds  *
14841da177e4SLinus Torvalds  ********************************************************************/
14851da177e4SLinus Torvalds 
14861da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
14871da177e4SLinus Torvalds 
14881da177e4SLinus Torvalds #include <linux/sysctl.h>
14891da177e4SLinus Torvalds 
1490d0efdf35SJason A. Donenfeld static int sysctl_random_min_urandom_seed = CRNG_RESEED_INTERVAL / HZ;
1491e3d2c5e7SJason A. Donenfeld static int sysctl_random_write_wakeup_bits = POOL_READY_BITS;
1492489c7fc4SJason A. Donenfeld static int sysctl_poolsize = POOL_BITS;
149364276a99SJason A. Donenfeld static u8 sysctl_bootid[UUID_SIZE];
14941da177e4SLinus Torvalds 
14951da177e4SLinus Torvalds /*
1496f22052b2SGreg Price  * This function is used to return both the bootid UUID, and random
14971da177e4SLinus Torvalds  * UUID. The difference is in whether table->data is NULL; if it is,
14981da177e4SLinus Torvalds  * then a new UUID is generated and returned to the user.
14991da177e4SLinus Torvalds  */
1500a1940263SJason A. Donenfeld static int proc_do_uuid(struct ctl_table *table, int write, void *buf,
1501248045b8SJason A. Donenfeld 			size_t *lenp, loff_t *ppos)
15021da177e4SLinus Torvalds {
150364276a99SJason A. Donenfeld 	u8 tmp_uuid[UUID_SIZE], *uuid;
150464276a99SJason A. Donenfeld 	char uuid_string[UUID_STRING_LEN + 1];
150564276a99SJason A. Donenfeld 	struct ctl_table fake_table = {
150664276a99SJason A. Donenfeld 		.data = uuid_string,
150764276a99SJason A. Donenfeld 		.maxlen = UUID_STRING_LEN
150864276a99SJason A. Donenfeld 	};
150964276a99SJason A. Donenfeld 
151064276a99SJason A. Donenfeld 	if (write)
151164276a99SJason A. Donenfeld 		return -EPERM;
15121da177e4SLinus Torvalds 
15131da177e4SLinus Torvalds 	uuid = table->data;
15141da177e4SLinus Torvalds 	if (!uuid) {
15151da177e4SLinus Torvalds 		uuid = tmp_uuid;
15161da177e4SLinus Torvalds 		generate_random_uuid(uuid);
151744e4360fSMathieu Desnoyers 	} else {
151844e4360fSMathieu Desnoyers 		static DEFINE_SPINLOCK(bootid_spinlock);
151944e4360fSMathieu Desnoyers 
152044e4360fSMathieu Desnoyers 		spin_lock(&bootid_spinlock);
152144e4360fSMathieu Desnoyers 		if (!uuid[8])
152244e4360fSMathieu Desnoyers 			generate_random_uuid(uuid);
152344e4360fSMathieu Desnoyers 		spin_unlock(&bootid_spinlock);
152444e4360fSMathieu Desnoyers 	}
15251da177e4SLinus Torvalds 
152664276a99SJason A. Donenfeld 	snprintf(uuid_string, sizeof(uuid_string), "%pU", uuid);
1527a1940263SJason A. Donenfeld 	return proc_dostring(&fake_table, 0, buf, lenp, ppos);
15281da177e4SLinus Torvalds }
15291da177e4SLinus Torvalds 
153077553cf8SJason A. Donenfeld /* The same as proc_dointvec, but writes don't change anything. */
1531a1940263SJason A. Donenfeld static int proc_do_rointvec(struct ctl_table *table, int write, void *buf,
153277553cf8SJason A. Donenfeld 			    size_t *lenp, loff_t *ppos)
153377553cf8SJason A. Donenfeld {
1534a1940263SJason A. Donenfeld 	return write ? 0 : proc_dointvec(table, 0, buf, lenp, ppos);
153577553cf8SJason A. Donenfeld }
153677553cf8SJason A. Donenfeld 
15375475e8f0SXiaoming Ni static struct ctl_table random_table[] = {
15381da177e4SLinus Torvalds 	{
15391da177e4SLinus Torvalds 		.procname	= "poolsize",
15401da177e4SLinus Torvalds 		.data		= &sysctl_poolsize,
15411da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
15421da177e4SLinus Torvalds 		.mode		= 0444,
15436d456111SEric W. Biederman 		.proc_handler	= proc_dointvec,
15441da177e4SLinus Torvalds 	},
15451da177e4SLinus Torvalds 	{
15461da177e4SLinus Torvalds 		.procname	= "entropy_avail",
1547e85c0fc1SJason A. Donenfeld 		.data		= &input_pool.init_bits,
15481da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
15491da177e4SLinus Torvalds 		.mode		= 0444,
1550c5704490SJason A. Donenfeld 		.proc_handler	= proc_dointvec,
15511da177e4SLinus Torvalds 	},
15521da177e4SLinus Torvalds 	{
15531da177e4SLinus Torvalds 		.procname	= "write_wakeup_threshold",
15540deff3c4SJason A. Donenfeld 		.data		= &sysctl_random_write_wakeup_bits,
15551da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
15561da177e4SLinus Torvalds 		.mode		= 0644,
155777553cf8SJason A. Donenfeld 		.proc_handler	= proc_do_rointvec,
15581da177e4SLinus Torvalds 	},
15591da177e4SLinus Torvalds 	{
1560f5c2742cSTheodore Ts'o 		.procname	= "urandom_min_reseed_secs",
15610deff3c4SJason A. Donenfeld 		.data		= &sysctl_random_min_urandom_seed,
1562f5c2742cSTheodore Ts'o 		.maxlen		= sizeof(int),
1563f5c2742cSTheodore Ts'o 		.mode		= 0644,
156477553cf8SJason A. Donenfeld 		.proc_handler	= proc_do_rointvec,
1565f5c2742cSTheodore Ts'o 	},
1566f5c2742cSTheodore Ts'o 	{
15671da177e4SLinus Torvalds 		.procname	= "boot_id",
15681da177e4SLinus Torvalds 		.data		= &sysctl_bootid,
15691da177e4SLinus Torvalds 		.mode		= 0444,
15706d456111SEric W. Biederman 		.proc_handler	= proc_do_uuid,
15711da177e4SLinus Torvalds 	},
15721da177e4SLinus Torvalds 	{
15731da177e4SLinus Torvalds 		.procname	= "uuid",
15741da177e4SLinus Torvalds 		.mode		= 0444,
15756d456111SEric W. Biederman 		.proc_handler	= proc_do_uuid,
15761da177e4SLinus Torvalds 	},
1577894d2491SEric W. Biederman 	{ }
15781da177e4SLinus Torvalds };
15795475e8f0SXiaoming Ni 
15805475e8f0SXiaoming Ni /*
15812f14062bSJason A. Donenfeld  * random_init() is called before sysctl_init(),
15822f14062bSJason A. Donenfeld  * so we cannot call register_sysctl_init() in random_init()
15835475e8f0SXiaoming Ni  */
15845475e8f0SXiaoming Ni static int __init random_sysctls_init(void)
15855475e8f0SXiaoming Ni {
15865475e8f0SXiaoming Ni 	register_sysctl_init("kernel/random", random_table);
15875475e8f0SXiaoming Ni 	return 0;
15885475e8f0SXiaoming Ni }
15895475e8f0SXiaoming Ni device_initcall(random_sysctls_init);
15900deff3c4SJason A. Donenfeld #endif
1591