11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * random.c -- A strong random number generator 31da177e4SLinus Torvalds * 49e95ce27SMatt Mackall * Copyright Matt Mackall <mpm@selenic.com>, 2003, 2004, 2005 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Copyright Theodore Ts'o, 1994, 1995, 1996, 1997, 1998, 1999. All 71da177e4SLinus Torvalds * rights reserved. 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * Redistribution and use in source and binary forms, with or without 101da177e4SLinus Torvalds * modification, are permitted provided that the following conditions 111da177e4SLinus Torvalds * are met: 121da177e4SLinus Torvalds * 1. Redistributions of source code must retain the above copyright 131da177e4SLinus Torvalds * notice, and the entire permission notice in its entirety, 141da177e4SLinus Torvalds * including the disclaimer of warranties. 151da177e4SLinus Torvalds * 2. Redistributions in binary form must reproduce the above copyright 161da177e4SLinus Torvalds * notice, this list of conditions and the following disclaimer in the 171da177e4SLinus Torvalds * documentation and/or other materials provided with the distribution. 181da177e4SLinus Torvalds * 3. The name of the author may not be used to endorse or promote 191da177e4SLinus Torvalds * products derived from this software without specific prior 201da177e4SLinus Torvalds * written permission. 211da177e4SLinus Torvalds * 221da177e4SLinus Torvalds * ALTERNATIVELY, this product may be distributed under the terms of 231da177e4SLinus Torvalds * the GNU General Public License, in which case the provisions of the GPL are 241da177e4SLinus Torvalds * required INSTEAD OF the above restrictions. (This clause is 251da177e4SLinus Torvalds * necessary due to a potential bad interaction between the GPL and 261da177e4SLinus Torvalds * the restrictions contained in a BSD-style copyright.) 271da177e4SLinus Torvalds * 281da177e4SLinus Torvalds * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 291da177e4SLinus Torvalds * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 301da177e4SLinus Torvalds * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF 311da177e4SLinus Torvalds * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE 321da177e4SLinus Torvalds * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 331da177e4SLinus Torvalds * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 341da177e4SLinus Torvalds * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 351da177e4SLinus Torvalds * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 361da177e4SLinus Torvalds * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 371da177e4SLinus Torvalds * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 381da177e4SLinus Torvalds * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH 391da177e4SLinus Torvalds * DAMAGE. 401da177e4SLinus Torvalds */ 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds /* 431da177e4SLinus Torvalds * (now, with legal B.S. out of the way.....) 441da177e4SLinus Torvalds * 451da177e4SLinus Torvalds * This routine gathers environmental noise from device drivers, etc., 461da177e4SLinus Torvalds * and returns good random numbers, suitable for cryptographic use. 471da177e4SLinus Torvalds * Besides the obvious cryptographic uses, these numbers are also good 481da177e4SLinus Torvalds * for seeding TCP sequence numbers, and other places where it is 491da177e4SLinus Torvalds * desirable to have numbers which are not only random, but hard to 501da177e4SLinus Torvalds * predict by an attacker. 511da177e4SLinus Torvalds * 521da177e4SLinus Torvalds * Theory of operation 531da177e4SLinus Torvalds * =================== 541da177e4SLinus Torvalds * 551da177e4SLinus Torvalds * Computers are very predictable devices. Hence it is extremely hard 561da177e4SLinus Torvalds * to produce truly random numbers on a computer --- as opposed to 571da177e4SLinus Torvalds * pseudo-random numbers, which can easily generated by using a 581da177e4SLinus Torvalds * algorithm. Unfortunately, it is very easy for attackers to guess 591da177e4SLinus Torvalds * the sequence of pseudo-random number generators, and for some 601da177e4SLinus Torvalds * applications this is not acceptable. So instead, we must try to 611da177e4SLinus Torvalds * gather "environmental noise" from the computer's environment, which 621da177e4SLinus Torvalds * must be hard for outside attackers to observe, and use that to 631da177e4SLinus Torvalds * generate random numbers. In a Unix environment, this is best done 641da177e4SLinus Torvalds * from inside the kernel. 651da177e4SLinus Torvalds * 661da177e4SLinus Torvalds * Sources of randomness from the environment include inter-keyboard 671da177e4SLinus Torvalds * timings, inter-interrupt timings from some interrupts, and other 681da177e4SLinus Torvalds * events which are both (a) non-deterministic and (b) hard for an 691da177e4SLinus Torvalds * outside observer to measure. Randomness from these sources are 701da177e4SLinus Torvalds * added to an "entropy pool", which is mixed using a CRC-like function. 711da177e4SLinus Torvalds * This is not cryptographically strong, but it is adequate assuming 721da177e4SLinus Torvalds * the randomness is not chosen maliciously, and it is fast enough that 731da177e4SLinus Torvalds * the overhead of doing it on every interrupt is very reasonable. 741da177e4SLinus Torvalds * As random bytes are mixed into the entropy pool, the routines keep 751da177e4SLinus Torvalds * an *estimate* of how many bits of randomness have been stored into 761da177e4SLinus Torvalds * the random number generator's internal state. 771da177e4SLinus Torvalds * 781da177e4SLinus Torvalds * When random bytes are desired, they are obtained by taking the SHA 791da177e4SLinus Torvalds * hash of the contents of the "entropy pool". The SHA hash avoids 801da177e4SLinus Torvalds * exposing the internal state of the entropy pool. It is believed to 811da177e4SLinus Torvalds * be computationally infeasible to derive any useful information 821da177e4SLinus Torvalds * about the input of SHA from its output. Even if it is possible to 831da177e4SLinus Torvalds * analyze SHA in some clever way, as long as the amount of data 841da177e4SLinus Torvalds * returned from the generator is less than the inherent entropy in 851da177e4SLinus Torvalds * the pool, the output data is totally unpredictable. For this 861da177e4SLinus Torvalds * reason, the routine decreases its internal estimate of how many 871da177e4SLinus Torvalds * bits of "true randomness" are contained in the entropy pool as it 881da177e4SLinus Torvalds * outputs random numbers. 891da177e4SLinus Torvalds * 901da177e4SLinus Torvalds * If this estimate goes to zero, the routine can still generate 911da177e4SLinus Torvalds * random numbers; however, an attacker may (at least in theory) be 921da177e4SLinus Torvalds * able to infer the future output of the generator from prior 931da177e4SLinus Torvalds * outputs. This requires successful cryptanalysis of SHA, which is 941da177e4SLinus Torvalds * not believed to be feasible, but there is a remote possibility. 951da177e4SLinus Torvalds * Nonetheless, these numbers should be useful for the vast majority 961da177e4SLinus Torvalds * of purposes. 971da177e4SLinus Torvalds * 981da177e4SLinus Torvalds * Exported interfaces ---- output 991da177e4SLinus Torvalds * =============================== 1001da177e4SLinus Torvalds * 1011da177e4SLinus Torvalds * There are three exported interfaces; the first is one designed to 1021da177e4SLinus Torvalds * be used from within the kernel: 1031da177e4SLinus Torvalds * 1041da177e4SLinus Torvalds * void get_random_bytes(void *buf, int nbytes); 1051da177e4SLinus Torvalds * 1061da177e4SLinus Torvalds * This interface will return the requested number of random bytes, 1071da177e4SLinus Torvalds * and place it in the requested buffer. 1081da177e4SLinus Torvalds * 1091da177e4SLinus Torvalds * The two other interfaces are two character devices /dev/random and 1101da177e4SLinus Torvalds * /dev/urandom. /dev/random is suitable for use when very high 1111da177e4SLinus Torvalds * quality randomness is desired (for example, for key generation or 1121da177e4SLinus Torvalds * one-time pads), as it will only return a maximum of the number of 1131da177e4SLinus Torvalds * bits of randomness (as estimated by the random number generator) 1141da177e4SLinus Torvalds * contained in the entropy pool. 1151da177e4SLinus Torvalds * 1161da177e4SLinus Torvalds * The /dev/urandom device does not have this limit, and will return 1171da177e4SLinus Torvalds * as many bytes as are requested. As more and more random bytes are 1181da177e4SLinus Torvalds * requested without giving time for the entropy pool to recharge, 1191da177e4SLinus Torvalds * this will result in random numbers that are merely cryptographically 1201da177e4SLinus Torvalds * strong. For many applications, however, this is acceptable. 1211da177e4SLinus Torvalds * 1221da177e4SLinus Torvalds * Exported interfaces ---- input 1231da177e4SLinus Torvalds * ============================== 1241da177e4SLinus Torvalds * 1251da177e4SLinus Torvalds * The current exported interfaces for gathering environmental noise 1261da177e4SLinus Torvalds * from the devices are: 1271da177e4SLinus Torvalds * 128a2080a67SLinus Torvalds * void add_device_randomness(const void *buf, unsigned int size); 1291da177e4SLinus Torvalds * void add_input_randomness(unsigned int type, unsigned int code, 1301da177e4SLinus Torvalds * unsigned int value); 131775f4b29STheodore Ts'o * void add_interrupt_randomness(int irq, int irq_flags); 132442a4fffSJarod Wilson * void add_disk_randomness(struct gendisk *disk); 1331da177e4SLinus Torvalds * 134a2080a67SLinus Torvalds * add_device_randomness() is for adding data to the random pool that 135a2080a67SLinus Torvalds * is likely to differ between two devices (or possibly even per boot). 136a2080a67SLinus Torvalds * This would be things like MAC addresses or serial numbers, or the 137a2080a67SLinus Torvalds * read-out of the RTC. This does *not* add any actual entropy to the 138a2080a67SLinus Torvalds * pool, but it initializes the pool to different values for devices 139a2080a67SLinus Torvalds * that might otherwise be identical and have very little entropy 140a2080a67SLinus Torvalds * available to them (particularly common in the embedded world). 141a2080a67SLinus Torvalds * 1421da177e4SLinus Torvalds * add_input_randomness() uses the input layer interrupt timing, as well as 1431da177e4SLinus Torvalds * the event type information from the hardware. 1441da177e4SLinus Torvalds * 145775f4b29STheodore Ts'o * add_interrupt_randomness() uses the interrupt timing as random 146775f4b29STheodore Ts'o * inputs to the entropy pool. Using the cycle counters and the irq source 147775f4b29STheodore Ts'o * as inputs, it feeds the randomness roughly once a second. 148442a4fffSJarod Wilson * 149442a4fffSJarod Wilson * add_disk_randomness() uses what amounts to the seek time of block 150442a4fffSJarod Wilson * layer request events, on a per-disk_devt basis, as input to the 151442a4fffSJarod Wilson * entropy pool. Note that high-speed solid state drives with very low 152442a4fffSJarod Wilson * seek times do not make for good sources of entropy, as their seek 153442a4fffSJarod Wilson * times are usually fairly consistent. 1541da177e4SLinus Torvalds * 1551da177e4SLinus Torvalds * All of these routines try to estimate how many bits of randomness a 1561da177e4SLinus Torvalds * particular randomness source. They do this by keeping track of the 1571da177e4SLinus Torvalds * first and second order deltas of the event timings. 1581da177e4SLinus Torvalds * 1591da177e4SLinus Torvalds * Ensuring unpredictability at system startup 1601da177e4SLinus Torvalds * ============================================ 1611da177e4SLinus Torvalds * 1621da177e4SLinus Torvalds * When any operating system starts up, it will go through a sequence 1631da177e4SLinus Torvalds * of actions that are fairly predictable by an adversary, especially 1641da177e4SLinus Torvalds * if the start-up does not involve interaction with a human operator. 1651da177e4SLinus Torvalds * This reduces the actual number of bits of unpredictability in the 1661da177e4SLinus Torvalds * entropy pool below the value in entropy_count. In order to 1671da177e4SLinus Torvalds * counteract this effect, it helps to carry information in the 1681da177e4SLinus Torvalds * entropy pool across shut-downs and start-ups. To do this, put the 1691da177e4SLinus Torvalds * following lines an appropriate script which is run during the boot 1701da177e4SLinus Torvalds * sequence: 1711da177e4SLinus Torvalds * 1721da177e4SLinus Torvalds * echo "Initializing random number generator..." 1731da177e4SLinus Torvalds * random_seed=/var/run/random-seed 1741da177e4SLinus Torvalds * # Carry a random seed from start-up to start-up 1751da177e4SLinus Torvalds * # Load and then save the whole entropy pool 1761da177e4SLinus Torvalds * if [ -f $random_seed ]; then 1771da177e4SLinus Torvalds * cat $random_seed >/dev/urandom 1781da177e4SLinus Torvalds * else 1791da177e4SLinus Torvalds * touch $random_seed 1801da177e4SLinus Torvalds * fi 1811da177e4SLinus Torvalds * chmod 600 $random_seed 1821da177e4SLinus Torvalds * dd if=/dev/urandom of=$random_seed count=1 bs=512 1831da177e4SLinus Torvalds * 1841da177e4SLinus Torvalds * and the following lines in an appropriate script which is run as 1851da177e4SLinus Torvalds * the system is shutdown: 1861da177e4SLinus Torvalds * 1871da177e4SLinus Torvalds * # Carry a random seed from shut-down to start-up 1881da177e4SLinus Torvalds * # Save the whole entropy pool 1891da177e4SLinus Torvalds * echo "Saving random seed..." 1901da177e4SLinus Torvalds * random_seed=/var/run/random-seed 1911da177e4SLinus Torvalds * touch $random_seed 1921da177e4SLinus Torvalds * chmod 600 $random_seed 1931da177e4SLinus Torvalds * dd if=/dev/urandom of=$random_seed count=1 bs=512 1941da177e4SLinus Torvalds * 1951da177e4SLinus Torvalds * For example, on most modern systems using the System V init 1961da177e4SLinus Torvalds * scripts, such code fragments would be found in 1971da177e4SLinus Torvalds * /etc/rc.d/init.d/random. On older Linux systems, the correct script 1981da177e4SLinus Torvalds * location might be in /etc/rcb.d/rc.local or /etc/rc.d/rc.0. 1991da177e4SLinus Torvalds * 2001da177e4SLinus Torvalds * Effectively, these commands cause the contents of the entropy pool 2011da177e4SLinus Torvalds * to be saved at shut-down time and reloaded into the entropy pool at 2021da177e4SLinus Torvalds * start-up. (The 'dd' in the addition to the bootup script is to 2031da177e4SLinus Torvalds * make sure that /etc/random-seed is different for every start-up, 2041da177e4SLinus Torvalds * even if the system crashes without executing rc.0.) Even with 2051da177e4SLinus Torvalds * complete knowledge of the start-up activities, predicting the state 2061da177e4SLinus Torvalds * of the entropy pool requires knowledge of the previous history of 2071da177e4SLinus Torvalds * the system. 2081da177e4SLinus Torvalds * 2091da177e4SLinus Torvalds * Configuring the /dev/random driver under Linux 2101da177e4SLinus Torvalds * ============================================== 2111da177e4SLinus Torvalds * 2121da177e4SLinus Torvalds * The /dev/random driver under Linux uses minor numbers 8 and 9 of 2131da177e4SLinus Torvalds * the /dev/mem major number (#1). So if your system does not have 2141da177e4SLinus Torvalds * /dev/random and /dev/urandom created already, they can be created 2151da177e4SLinus Torvalds * by using the commands: 2161da177e4SLinus Torvalds * 2171da177e4SLinus Torvalds * mknod /dev/random c 1 8 2181da177e4SLinus Torvalds * mknod /dev/urandom c 1 9 2191da177e4SLinus Torvalds * 2201da177e4SLinus Torvalds * Acknowledgements: 2211da177e4SLinus Torvalds * ================= 2221da177e4SLinus Torvalds * 2231da177e4SLinus Torvalds * Ideas for constructing this random number generator were derived 2241da177e4SLinus Torvalds * from Pretty Good Privacy's random number generator, and from private 2251da177e4SLinus Torvalds * discussions with Phil Karn. Colin Plumb provided a faster random 2261da177e4SLinus Torvalds * number generator, which speed up the mixing function of the entropy 2271da177e4SLinus Torvalds * pool, taken from PGPfone. Dale Worley has also contributed many 2281da177e4SLinus Torvalds * useful ideas and suggestions to improve this driver. 2291da177e4SLinus Torvalds * 2301da177e4SLinus Torvalds * Any flaws in the design are solely my responsibility, and should 2311da177e4SLinus Torvalds * not be attributed to the Phil, Colin, or any of authors of PGP. 2321da177e4SLinus Torvalds * 2331da177e4SLinus Torvalds * Further background information on this topic may be obtained from 2341da177e4SLinus Torvalds * RFC 1750, "Randomness Recommendations for Security", by Donald 2351da177e4SLinus Torvalds * Eastlake, Steve Crocker, and Jeff Schiller. 2361da177e4SLinus Torvalds */ 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds #include <linux/utsname.h> 2391da177e4SLinus Torvalds #include <linux/module.h> 2401da177e4SLinus Torvalds #include <linux/kernel.h> 2411da177e4SLinus Torvalds #include <linux/major.h> 2421da177e4SLinus Torvalds #include <linux/string.h> 2431da177e4SLinus Torvalds #include <linux/fcntl.h> 2441da177e4SLinus Torvalds #include <linux/slab.h> 2451da177e4SLinus Torvalds #include <linux/random.h> 2461da177e4SLinus Torvalds #include <linux/poll.h> 2471da177e4SLinus Torvalds #include <linux/init.h> 2481da177e4SLinus Torvalds #include <linux/fs.h> 2491da177e4SLinus Torvalds #include <linux/genhd.h> 2501da177e4SLinus Torvalds #include <linux/interrupt.h> 25127ac792cSAndrea Righi #include <linux/mm.h> 252dd0f0cf5SMichael Ellerman #include <linux/nodemask.h> 2531da177e4SLinus Torvalds #include <linux/spinlock.h> 254c84dbf61STorsten Duwe #include <linux/kthread.h> 2551da177e4SLinus Torvalds #include <linux/percpu.h> 2561da177e4SLinus Torvalds #include <linux/cryptohash.h> 2575b739ef8SNeil Horman #include <linux/fips.h> 258775f4b29STheodore Ts'o #include <linux/ptrace.h> 259e6d4947bSTheodore Ts'o #include <linux/kmemcheck.h> 2606265e169STheodore Ts'o #include <linux/workqueue.h> 261d178a1ebSYinghai Lu #include <linux/irq.h> 262c6e9d6f3STheodore Ts'o #include <linux/syscalls.h> 263c6e9d6f3STheodore Ts'o #include <linux/completion.h> 2648da4b8c4SAndy Shevchenko #include <linux/uuid.h> 265e192be9dSTheodore Ts'o #include <crypto/chacha20.h> 266d178a1ebSYinghai Lu 2671da177e4SLinus Torvalds #include <asm/processor.h> 2687c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 2691da177e4SLinus Torvalds #include <asm/irq.h> 270775f4b29STheodore Ts'o #include <asm/irq_regs.h> 2711da177e4SLinus Torvalds #include <asm/io.h> 2721da177e4SLinus Torvalds 27300ce1db1STheodore Ts'o #define CREATE_TRACE_POINTS 27400ce1db1STheodore Ts'o #include <trace/events/random.h> 27500ce1db1STheodore Ts'o 27643759d4fSTheodore Ts'o /* #define ADD_INTERRUPT_BENCH */ 27743759d4fSTheodore Ts'o 2781da177e4SLinus Torvalds /* 2791da177e4SLinus Torvalds * Configuration information 2801da177e4SLinus Torvalds */ 28130e37ec5SH. Peter Anvin #define INPUT_POOL_SHIFT 12 28230e37ec5SH. Peter Anvin #define INPUT_POOL_WORDS (1 << (INPUT_POOL_SHIFT-5)) 28330e37ec5SH. Peter Anvin #define OUTPUT_POOL_SHIFT 10 28430e37ec5SH. Peter Anvin #define OUTPUT_POOL_WORDS (1 << (OUTPUT_POOL_SHIFT-5)) 2851da177e4SLinus Torvalds #define SEC_XFER_SIZE 512 286e954bc91SMatt Mackall #define EXTRACT_SIZE 10 2871da177e4SLinus Torvalds 288392a546dSTheodore Ts'o #define DEBUG_RANDOM_BOOT 0 289392a546dSTheodore Ts'o 290d2e7c96aSH. Peter Anvin #define LONGS(x) (((x) + sizeof(unsigned long) - 1)/sizeof(unsigned long)) 291d2e7c96aSH. Peter Anvin 2921da177e4SLinus Torvalds /* 29395b709b6STheodore Ts'o * To allow fractional bits to be tracked, the entropy_count field is 29495b709b6STheodore Ts'o * denominated in units of 1/8th bits. 29530e37ec5SH. Peter Anvin * 29630e37ec5SH. Peter Anvin * 2*(ENTROPY_SHIFT + log2(poolbits)) must <= 31, or the multiply in 29730e37ec5SH. Peter Anvin * credit_entropy_bits() needs to be 64 bits wide. 298a283b5c4SH. Peter Anvin */ 299a283b5c4SH. Peter Anvin #define ENTROPY_SHIFT 3 300a283b5c4SH. Peter Anvin #define ENTROPY_BITS(r) ((r)->entropy_count >> ENTROPY_SHIFT) 301a283b5c4SH. Peter Anvin 302a283b5c4SH. Peter Anvin /* 3031da177e4SLinus Torvalds * The minimum number of bits of entropy before we wake up a read on 3041da177e4SLinus Torvalds * /dev/random. Should be enough to do a significant reseed. 3051da177e4SLinus Torvalds */ 3062132a96fSGreg Price static int random_read_wakeup_bits = 64; 3071da177e4SLinus Torvalds 3081da177e4SLinus Torvalds /* 3091da177e4SLinus Torvalds * If the entropy count falls under this number of bits, then we 3101da177e4SLinus Torvalds * should wake up processes which are selecting or polling on write 3111da177e4SLinus Torvalds * access to /dev/random. 3121da177e4SLinus Torvalds */ 3132132a96fSGreg Price static int random_write_wakeup_bits = 28 * OUTPUT_POOL_WORDS; 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds /* 3166e9fa2c8STheodore Ts'o * Originally, we used a primitive polynomial of degree .poolwords 3176e9fa2c8STheodore Ts'o * over GF(2). The taps for various sizes are defined below. They 3186e9fa2c8STheodore Ts'o * were chosen to be evenly spaced except for the last tap, which is 1 3196e9fa2c8STheodore Ts'o * to get the twisting happening as fast as possible. 3201da177e4SLinus Torvalds * 3216e9fa2c8STheodore Ts'o * For the purposes of better mixing, we use the CRC-32 polynomial as 3226e9fa2c8STheodore Ts'o * well to make a (modified) twisted Generalized Feedback Shift 3236e9fa2c8STheodore Ts'o * Register. (See M. Matsumoto & Y. Kurita, 1992. Twisted GFSR 3246e9fa2c8STheodore Ts'o * generators. ACM Transactions on Modeling and Computer Simulation 3256e9fa2c8STheodore Ts'o * 2(3):179-194. Also see M. Matsumoto & Y. Kurita, 1994. Twisted 326dfd38750SGreg Price * GFSR generators II. ACM Transactions on Modeling and Computer 3276e9fa2c8STheodore Ts'o * Simulation 4:254-266) 3281da177e4SLinus Torvalds * 3291da177e4SLinus Torvalds * Thanks to Colin Plumb for suggesting this. 3301da177e4SLinus Torvalds * 3316e9fa2c8STheodore Ts'o * The mixing operation is much less sensitive than the output hash, 3326e9fa2c8STheodore Ts'o * where we use SHA-1. All that we want of mixing operation is that 3336e9fa2c8STheodore Ts'o * it be a good non-cryptographic hash; i.e. it not produce collisions 3346e9fa2c8STheodore Ts'o * when fed "random" data of the sort we expect to see. As long as 3356e9fa2c8STheodore Ts'o * the pool state differs for different inputs, we have preserved the 3366e9fa2c8STheodore Ts'o * input entropy and done a good job. The fact that an intelligent 3376e9fa2c8STheodore Ts'o * attacker can construct inputs that will produce controlled 3386e9fa2c8STheodore Ts'o * alterations to the pool's state is not important because we don't 3396e9fa2c8STheodore Ts'o * consider such inputs to contribute any randomness. The only 3406e9fa2c8STheodore Ts'o * property we need with respect to them is that the attacker can't 3416e9fa2c8STheodore Ts'o * increase his/her knowledge of the pool's state. Since all 3426e9fa2c8STheodore Ts'o * additions are reversible (knowing the final state and the input, 3436e9fa2c8STheodore Ts'o * you can reconstruct the initial state), if an attacker has any 3446e9fa2c8STheodore Ts'o * uncertainty about the initial state, he/she can only shuffle that 3456e9fa2c8STheodore Ts'o * uncertainty about, but never cause any collisions (which would 3461da177e4SLinus Torvalds * decrease the uncertainty). 3471da177e4SLinus Torvalds * 3486e9fa2c8STheodore Ts'o * Our mixing functions were analyzed by Lacharme, Roeck, Strubel, and 3496e9fa2c8STheodore Ts'o * Videau in their paper, "The Linux Pseudorandom Number Generator 3506e9fa2c8STheodore Ts'o * Revisited" (see: http://eprint.iacr.org/2012/251.pdf). In their 3516e9fa2c8STheodore Ts'o * paper, they point out that we are not using a true Twisted GFSR, 3526e9fa2c8STheodore Ts'o * since Matsumoto & Kurita used a trinomial feedback polynomial (that 3536e9fa2c8STheodore Ts'o * is, with only three taps, instead of the six that we are using). 3546e9fa2c8STheodore Ts'o * As a result, the resulting polynomial is neither primitive nor 3556e9fa2c8STheodore Ts'o * irreducible, and hence does not have a maximal period over 3566e9fa2c8STheodore Ts'o * GF(2**32). They suggest a slight change to the generator 3576e9fa2c8STheodore Ts'o * polynomial which improves the resulting TGFSR polynomial to be 3586e9fa2c8STheodore Ts'o * irreducible, which we have made here. 3591da177e4SLinus Torvalds */ 3601da177e4SLinus Torvalds static struct poolinfo { 361a283b5c4SH. Peter Anvin int poolbitshift, poolwords, poolbytes, poolbits, poolfracbits; 362a283b5c4SH. Peter Anvin #define S(x) ilog2(x)+5, (x), (x)*4, (x)*32, (x) << (ENTROPY_SHIFT+5) 3631da177e4SLinus Torvalds int tap1, tap2, tap3, tap4, tap5; 3641da177e4SLinus Torvalds } poolinfo_table[] = { 3656e9fa2c8STheodore Ts'o /* was: x^128 + x^103 + x^76 + x^51 +x^25 + x + 1 */ 3666e9fa2c8STheodore Ts'o /* x^128 + x^104 + x^76 + x^51 +x^25 + x + 1 */ 3676e9fa2c8STheodore Ts'o { S(128), 104, 76, 51, 25, 1 }, 3686e9fa2c8STheodore Ts'o /* was: x^32 + x^26 + x^20 + x^14 + x^7 + x + 1 */ 3696e9fa2c8STheodore Ts'o /* x^32 + x^26 + x^19 + x^14 + x^7 + x + 1 */ 3706e9fa2c8STheodore Ts'o { S(32), 26, 19, 14, 7, 1 }, 3711da177e4SLinus Torvalds #if 0 3721da177e4SLinus Torvalds /* x^2048 + x^1638 + x^1231 + x^819 + x^411 + x + 1 -- 115 */ 3739ed17b70SH. Peter Anvin { S(2048), 1638, 1231, 819, 411, 1 }, 3741da177e4SLinus Torvalds 3751da177e4SLinus Torvalds /* x^1024 + x^817 + x^615 + x^412 + x^204 + x + 1 -- 290 */ 3769ed17b70SH. Peter Anvin { S(1024), 817, 615, 412, 204, 1 }, 3771da177e4SLinus Torvalds 3781da177e4SLinus Torvalds /* x^1024 + x^819 + x^616 + x^410 + x^207 + x^2 + 1 -- 115 */ 3799ed17b70SH. Peter Anvin { S(1024), 819, 616, 410, 207, 2 }, 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds /* x^512 + x^411 + x^308 + x^208 + x^104 + x + 1 -- 225 */ 3829ed17b70SH. Peter Anvin { S(512), 411, 308, 208, 104, 1 }, 3831da177e4SLinus Torvalds 3841da177e4SLinus Torvalds /* x^512 + x^409 + x^307 + x^206 + x^102 + x^2 + 1 -- 95 */ 3859ed17b70SH. Peter Anvin { S(512), 409, 307, 206, 102, 2 }, 3861da177e4SLinus Torvalds /* x^512 + x^409 + x^309 + x^205 + x^103 + x^2 + 1 -- 95 */ 3879ed17b70SH. Peter Anvin { S(512), 409, 309, 205, 103, 2 }, 3881da177e4SLinus Torvalds 3891da177e4SLinus Torvalds /* x^256 + x^205 + x^155 + x^101 + x^52 + x + 1 -- 125 */ 3909ed17b70SH. Peter Anvin { S(256), 205, 155, 101, 52, 1 }, 3911da177e4SLinus Torvalds 3921da177e4SLinus Torvalds /* x^128 + x^103 + x^78 + x^51 + x^27 + x^2 + 1 -- 70 */ 3939ed17b70SH. Peter Anvin { S(128), 103, 78, 51, 27, 2 }, 3941da177e4SLinus Torvalds 3951da177e4SLinus Torvalds /* x^64 + x^52 + x^39 + x^26 + x^14 + x + 1 -- 15 */ 3969ed17b70SH. Peter Anvin { S(64), 52, 39, 26, 14, 1 }, 3971da177e4SLinus Torvalds #endif 3981da177e4SLinus Torvalds }; 3991da177e4SLinus Torvalds 4001da177e4SLinus Torvalds /* 4011da177e4SLinus Torvalds * Static global variables 4021da177e4SLinus Torvalds */ 4031da177e4SLinus Torvalds static DECLARE_WAIT_QUEUE_HEAD(random_read_wait); 4041da177e4SLinus Torvalds static DECLARE_WAIT_QUEUE_HEAD(random_write_wait); 4059a6f70bbSJeff Dike static struct fasync_struct *fasync; 4061da177e4SLinus Torvalds 407205a525cSHerbert Xu static DEFINE_SPINLOCK(random_ready_list_lock); 408205a525cSHerbert Xu static LIST_HEAD(random_ready_list); 409205a525cSHerbert Xu 410e192be9dSTheodore Ts'o struct crng_state { 411e192be9dSTheodore Ts'o __u32 state[16]; 412e192be9dSTheodore Ts'o unsigned long init_time; 413e192be9dSTheodore Ts'o spinlock_t lock; 414e192be9dSTheodore Ts'o }; 415e192be9dSTheodore Ts'o 416e192be9dSTheodore Ts'o struct crng_state primary_crng = { 417e192be9dSTheodore Ts'o .lock = __SPIN_LOCK_UNLOCKED(primary_crng.lock), 418e192be9dSTheodore Ts'o }; 419e192be9dSTheodore Ts'o 420e192be9dSTheodore Ts'o /* 421e192be9dSTheodore Ts'o * crng_init = 0 --> Uninitialized 422e192be9dSTheodore Ts'o * 1 --> Initialized 423e192be9dSTheodore Ts'o * 2 --> Initialized from input_pool 424e192be9dSTheodore Ts'o * 425e192be9dSTheodore Ts'o * crng_init is protected by primary_crng->lock, and only increases 426e192be9dSTheodore Ts'o * its value (from 0->1->2). 427e192be9dSTheodore Ts'o */ 428e192be9dSTheodore Ts'o static int crng_init = 0; 429e192be9dSTheodore Ts'o #define crng_ready() (likely(crng_init > 0)) 430e192be9dSTheodore Ts'o static int crng_init_cnt = 0; 431e192be9dSTheodore Ts'o #define CRNG_INIT_CNT_THRESH (2*CHACHA20_KEY_SIZE) 4321e7f583aSTheodore Ts'o static void _extract_crng(struct crng_state *crng, 4331e7f583aSTheodore Ts'o __u8 out[CHACHA20_BLOCK_SIZE]); 434c92e040dSTheodore Ts'o static void _crng_backtrack_protect(struct crng_state *crng, 435c92e040dSTheodore Ts'o __u8 tmp[CHACHA20_BLOCK_SIZE], int used); 436e192be9dSTheodore Ts'o static void process_random_ready_list(void); 437e192be9dSTheodore Ts'o 4381da177e4SLinus Torvalds /********************************************************************** 4391da177e4SLinus Torvalds * 4401da177e4SLinus Torvalds * OS independent entropy store. Here are the functions which handle 4411da177e4SLinus Torvalds * storing entropy in an entropy pool. 4421da177e4SLinus Torvalds * 4431da177e4SLinus Torvalds **********************************************************************/ 4441da177e4SLinus Torvalds 4451da177e4SLinus Torvalds struct entropy_store; 4461da177e4SLinus Torvalds struct entropy_store { 44743358209SMatt Mackall /* read-only data: */ 44830e37ec5SH. Peter Anvin const struct poolinfo *poolinfo; 4491da177e4SLinus Torvalds __u32 *pool; 4501da177e4SLinus Torvalds const char *name; 4511da177e4SLinus Torvalds struct entropy_store *pull; 4526265e169STheodore Ts'o struct work_struct push_work; 4531da177e4SLinus Torvalds 4541da177e4SLinus Torvalds /* read-write data: */ 455f5c2742cSTheodore Ts'o unsigned long last_pulled; 45643358209SMatt Mackall spinlock_t lock; 457c59974aeSTheodore Ts'o unsigned short add_ptr; 458c59974aeSTheodore Ts'o unsigned short input_rotate; 459cda796a3SMatt Mackall int entropy_count; 460775f4b29STheodore Ts'o int entropy_total; 461775f4b29STheodore Ts'o unsigned int initialized:1; 462c59974aeSTheodore Ts'o unsigned int last_data_init:1; 463e954bc91SMatt Mackall __u8 last_data[EXTRACT_SIZE]; 4641da177e4SLinus Torvalds }; 4651da177e4SLinus Torvalds 466e192be9dSTheodore Ts'o static ssize_t extract_entropy(struct entropy_store *r, void *buf, 467e192be9dSTheodore Ts'o size_t nbytes, int min, int rsvd); 468e192be9dSTheodore Ts'o static ssize_t _extract_entropy(struct entropy_store *r, void *buf, 469e192be9dSTheodore Ts'o size_t nbytes, int fips); 470e192be9dSTheodore Ts'o 471e192be9dSTheodore Ts'o static void crng_reseed(struct crng_state *crng, struct entropy_store *r); 4726265e169STheodore Ts'o static void push_to_pool(struct work_struct *work); 4730766f788SEmese Revfy static __u32 input_pool_data[INPUT_POOL_WORDS] __latent_entropy; 4740766f788SEmese Revfy static __u32 blocking_pool_data[OUTPUT_POOL_WORDS] __latent_entropy; 4751da177e4SLinus Torvalds 4761da177e4SLinus Torvalds static struct entropy_store input_pool = { 4771da177e4SLinus Torvalds .poolinfo = &poolinfo_table[0], 4781da177e4SLinus Torvalds .name = "input", 479eece09ecSThomas Gleixner .lock = __SPIN_LOCK_UNLOCKED(input_pool.lock), 4801da177e4SLinus Torvalds .pool = input_pool_data 4811da177e4SLinus Torvalds }; 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds static struct entropy_store blocking_pool = { 4841da177e4SLinus Torvalds .poolinfo = &poolinfo_table[1], 4851da177e4SLinus Torvalds .name = "blocking", 4861da177e4SLinus Torvalds .pull = &input_pool, 487eece09ecSThomas Gleixner .lock = __SPIN_LOCK_UNLOCKED(blocking_pool.lock), 4886265e169STheodore Ts'o .pool = blocking_pool_data, 4896265e169STheodore Ts'o .push_work = __WORK_INITIALIZER(blocking_pool.push_work, 4906265e169STheodore Ts'o push_to_pool), 4911da177e4SLinus Torvalds }; 4921da177e4SLinus Torvalds 493775f4b29STheodore Ts'o static __u32 const twist_table[8] = { 494775f4b29STheodore Ts'o 0x00000000, 0x3b6e20c8, 0x76dc4190, 0x4db26158, 495775f4b29STheodore Ts'o 0xedb88320, 0xd6d6a3e8, 0x9b64c2b0, 0xa00ae278 }; 496775f4b29STheodore Ts'o 4971da177e4SLinus Torvalds /* 498e68e5b66SMatt Mackall * This function adds bytes into the entropy "pool". It does not 4991da177e4SLinus Torvalds * update the entropy estimate. The caller should call 500adc782daSMatt Mackall * credit_entropy_bits if this is appropriate. 5011da177e4SLinus Torvalds * 5021da177e4SLinus Torvalds * The pool is stirred with a primitive polynomial of the appropriate 5031da177e4SLinus Torvalds * degree, and then twisted. We twist by three bits at a time because 5041da177e4SLinus Torvalds * it's cheap to do so and helps slightly in the expected case where 5051da177e4SLinus Torvalds * the entropy is concentrated in the low-order bits. 5061da177e4SLinus Torvalds */ 50700ce1db1STheodore Ts'o static void _mix_pool_bytes(struct entropy_store *r, const void *in, 50885608f8eSTheodore Ts'o int nbytes) 5091da177e4SLinus Torvalds { 51085608f8eSTheodore Ts'o unsigned long i, tap1, tap2, tap3, tap4, tap5; 511feee7697SMatt Mackall int input_rotate; 5121da177e4SLinus Torvalds int wordmask = r->poolinfo->poolwords - 1; 513e68e5b66SMatt Mackall const char *bytes = in; 5146d38b827SMatt Mackall __u32 w; 5151da177e4SLinus Torvalds 5161da177e4SLinus Torvalds tap1 = r->poolinfo->tap1; 5171da177e4SLinus Torvalds tap2 = r->poolinfo->tap2; 5181da177e4SLinus Torvalds tap3 = r->poolinfo->tap3; 5191da177e4SLinus Torvalds tap4 = r->poolinfo->tap4; 5201da177e4SLinus Torvalds tap5 = r->poolinfo->tap5; 5211da177e4SLinus Torvalds 52291fcb532STheodore Ts'o input_rotate = r->input_rotate; 52391fcb532STheodore Ts'o i = r->add_ptr; 5241da177e4SLinus Torvalds 525e68e5b66SMatt Mackall /* mix one byte at a time to simplify size handling and churn faster */ 526e68e5b66SMatt Mackall while (nbytes--) { 527c59974aeSTheodore Ts'o w = rol32(*bytes++, input_rotate); 528993ba211SMatt Mackall i = (i - 1) & wordmask; 5291da177e4SLinus Torvalds 5301da177e4SLinus Torvalds /* XOR in the various taps */ 531993ba211SMatt Mackall w ^= r->pool[i]; 5321da177e4SLinus Torvalds w ^= r->pool[(i + tap1) & wordmask]; 5331da177e4SLinus Torvalds w ^= r->pool[(i + tap2) & wordmask]; 5341da177e4SLinus Torvalds w ^= r->pool[(i + tap3) & wordmask]; 5351da177e4SLinus Torvalds w ^= r->pool[(i + tap4) & wordmask]; 5361da177e4SLinus Torvalds w ^= r->pool[(i + tap5) & wordmask]; 537993ba211SMatt Mackall 538993ba211SMatt Mackall /* Mix the result back in with a twist */ 5391da177e4SLinus Torvalds r->pool[i] = (w >> 3) ^ twist_table[w & 7]; 540feee7697SMatt Mackall 541feee7697SMatt Mackall /* 542feee7697SMatt Mackall * Normally, we add 7 bits of rotation to the pool. 543feee7697SMatt Mackall * At the beginning of the pool, add an extra 7 bits 544feee7697SMatt Mackall * rotation, so that successive passes spread the 545feee7697SMatt Mackall * input bits across the pool evenly. 546feee7697SMatt Mackall */ 547c59974aeSTheodore Ts'o input_rotate = (input_rotate + (i ? 7 : 14)) & 31; 5481da177e4SLinus Torvalds } 5491da177e4SLinus Torvalds 55091fcb532STheodore Ts'o r->input_rotate = input_rotate; 55191fcb532STheodore Ts'o r->add_ptr = i; 5521da177e4SLinus Torvalds } 5531da177e4SLinus Torvalds 55400ce1db1STheodore Ts'o static void __mix_pool_bytes(struct entropy_store *r, const void *in, 55585608f8eSTheodore Ts'o int nbytes) 55600ce1db1STheodore Ts'o { 55700ce1db1STheodore Ts'o trace_mix_pool_bytes_nolock(r->name, nbytes, _RET_IP_); 55885608f8eSTheodore Ts'o _mix_pool_bytes(r, in, nbytes); 55900ce1db1STheodore Ts'o } 56000ce1db1STheodore Ts'o 561902c098aSTheodore Ts'o static void mix_pool_bytes(struct entropy_store *r, const void *in, 56285608f8eSTheodore Ts'o int nbytes) 5631da177e4SLinus Torvalds { 564902c098aSTheodore Ts'o unsigned long flags; 565902c098aSTheodore Ts'o 56600ce1db1STheodore Ts'o trace_mix_pool_bytes(r->name, nbytes, _RET_IP_); 567902c098aSTheodore Ts'o spin_lock_irqsave(&r->lock, flags); 56885608f8eSTheodore Ts'o _mix_pool_bytes(r, in, nbytes); 569902c098aSTheodore Ts'o spin_unlock_irqrestore(&r->lock, flags); 5701da177e4SLinus Torvalds } 5711da177e4SLinus Torvalds 572775f4b29STheodore Ts'o struct fast_pool { 573775f4b29STheodore Ts'o __u32 pool[4]; 574775f4b29STheodore Ts'o unsigned long last; 575ee3e00e9STheodore Ts'o unsigned short reg_idx; 576840f9507STheodore Ts'o unsigned char count; 577775f4b29STheodore Ts'o }; 578775f4b29STheodore Ts'o 579775f4b29STheodore Ts'o /* 580775f4b29STheodore Ts'o * This is a fast mixing routine used by the interrupt randomness 581775f4b29STheodore Ts'o * collector. It's hardcoded for an 128 bit pool and assumes that any 582775f4b29STheodore Ts'o * locks that might be needed are taken by the caller. 583775f4b29STheodore Ts'o */ 58443759d4fSTheodore Ts'o static void fast_mix(struct fast_pool *f) 585775f4b29STheodore Ts'o { 58643759d4fSTheodore Ts'o __u32 a = f->pool[0], b = f->pool[1]; 58743759d4fSTheodore Ts'o __u32 c = f->pool[2], d = f->pool[3]; 588775f4b29STheodore Ts'o 58943759d4fSTheodore Ts'o a += b; c += d; 59019acc77aSGeorge Spelvin b = rol32(b, 6); d = rol32(d, 27); 59143759d4fSTheodore Ts'o d ^= a; b ^= c; 592655b2264STheodore Ts'o 59343759d4fSTheodore Ts'o a += b; c += d; 59419acc77aSGeorge Spelvin b = rol32(b, 16); d = rol32(d, 14); 59543759d4fSTheodore Ts'o d ^= a; b ^= c; 59643759d4fSTheodore Ts'o 59743759d4fSTheodore Ts'o a += b; c += d; 59819acc77aSGeorge Spelvin b = rol32(b, 6); d = rol32(d, 27); 59943759d4fSTheodore Ts'o d ^= a; b ^= c; 60043759d4fSTheodore Ts'o 60143759d4fSTheodore Ts'o a += b; c += d; 60219acc77aSGeorge Spelvin b = rol32(b, 16); d = rol32(d, 14); 60343759d4fSTheodore Ts'o d ^= a; b ^= c; 60443759d4fSTheodore Ts'o 60543759d4fSTheodore Ts'o f->pool[0] = a; f->pool[1] = b; 60643759d4fSTheodore Ts'o f->pool[2] = c; f->pool[3] = d; 607655b2264STheodore Ts'o f->count++; 608775f4b29STheodore Ts'o } 609775f4b29STheodore Ts'o 610205a525cSHerbert Xu static void process_random_ready_list(void) 611205a525cSHerbert Xu { 612205a525cSHerbert Xu unsigned long flags; 613205a525cSHerbert Xu struct random_ready_callback *rdy, *tmp; 614205a525cSHerbert Xu 615205a525cSHerbert Xu spin_lock_irqsave(&random_ready_list_lock, flags); 616205a525cSHerbert Xu list_for_each_entry_safe(rdy, tmp, &random_ready_list, list) { 617205a525cSHerbert Xu struct module *owner = rdy->owner; 618205a525cSHerbert Xu 619205a525cSHerbert Xu list_del_init(&rdy->list); 620205a525cSHerbert Xu rdy->func(rdy); 621205a525cSHerbert Xu module_put(owner); 622205a525cSHerbert Xu } 623205a525cSHerbert Xu spin_unlock_irqrestore(&random_ready_list_lock, flags); 624205a525cSHerbert Xu } 625205a525cSHerbert Xu 6261da177e4SLinus Torvalds /* 627a283b5c4SH. Peter Anvin * Credit (or debit) the entropy store with n bits of entropy. 628a283b5c4SH. Peter Anvin * Use credit_entropy_bits_safe() if the value comes from userspace 629a283b5c4SH. Peter Anvin * or otherwise should be checked for extreme values. 6301da177e4SLinus Torvalds */ 631adc782daSMatt Mackall static void credit_entropy_bits(struct entropy_store *r, int nbits) 6321da177e4SLinus Torvalds { 633902c098aSTheodore Ts'o int entropy_count, orig; 63430e37ec5SH. Peter Anvin const int pool_size = r->poolinfo->poolfracbits; 63530e37ec5SH. Peter Anvin int nfrac = nbits << ENTROPY_SHIFT; 6361da177e4SLinus Torvalds 637adc782daSMatt Mackall if (!nbits) 638adc782daSMatt Mackall return; 639adc782daSMatt Mackall 640902c098aSTheodore Ts'o retry: 641902c098aSTheodore Ts'o entropy_count = orig = ACCESS_ONCE(r->entropy_count); 64230e37ec5SH. Peter Anvin if (nfrac < 0) { 64330e37ec5SH. Peter Anvin /* Debit */ 64430e37ec5SH. Peter Anvin entropy_count += nfrac; 64530e37ec5SH. Peter Anvin } else { 64630e37ec5SH. Peter Anvin /* 64730e37ec5SH. Peter Anvin * Credit: we have to account for the possibility of 64830e37ec5SH. Peter Anvin * overwriting already present entropy. Even in the 64930e37ec5SH. Peter Anvin * ideal case of pure Shannon entropy, new contributions 65030e37ec5SH. Peter Anvin * approach the full value asymptotically: 65130e37ec5SH. Peter Anvin * 65230e37ec5SH. Peter Anvin * entropy <- entropy + (pool_size - entropy) * 65330e37ec5SH. Peter Anvin * (1 - exp(-add_entropy/pool_size)) 65430e37ec5SH. Peter Anvin * 65530e37ec5SH. Peter Anvin * For add_entropy <= pool_size/2 then 65630e37ec5SH. Peter Anvin * (1 - exp(-add_entropy/pool_size)) >= 65730e37ec5SH. Peter Anvin * (add_entropy/pool_size)*0.7869... 65830e37ec5SH. Peter Anvin * so we can approximate the exponential with 65930e37ec5SH. Peter Anvin * 3/4*add_entropy/pool_size and still be on the 66030e37ec5SH. Peter Anvin * safe side by adding at most pool_size/2 at a time. 66130e37ec5SH. Peter Anvin * 66230e37ec5SH. Peter Anvin * The use of pool_size-2 in the while statement is to 66330e37ec5SH. Peter Anvin * prevent rounding artifacts from making the loop 66430e37ec5SH. Peter Anvin * arbitrarily long; this limits the loop to log2(pool_size)*2 66530e37ec5SH. Peter Anvin * turns no matter how large nbits is. 66630e37ec5SH. Peter Anvin */ 66730e37ec5SH. Peter Anvin int pnfrac = nfrac; 66830e37ec5SH. Peter Anvin const int s = r->poolinfo->poolbitshift + ENTROPY_SHIFT + 2; 66930e37ec5SH. Peter Anvin /* The +2 corresponds to the /4 in the denominator */ 67030e37ec5SH. Peter Anvin 67130e37ec5SH. Peter Anvin do { 67230e37ec5SH. Peter Anvin unsigned int anfrac = min(pnfrac, pool_size/2); 67330e37ec5SH. Peter Anvin unsigned int add = 67430e37ec5SH. Peter Anvin ((pool_size - entropy_count)*anfrac*3) >> s; 67530e37ec5SH. Peter Anvin 67630e37ec5SH. Peter Anvin entropy_count += add; 67730e37ec5SH. Peter Anvin pnfrac -= anfrac; 67830e37ec5SH. Peter Anvin } while (unlikely(entropy_count < pool_size-2 && pnfrac)); 67930e37ec5SH. Peter Anvin } 68000ce1db1STheodore Ts'o 68179a84687SHannes Frederic Sowa if (unlikely(entropy_count < 0)) { 682f80bbd8bSTheodore Ts'o pr_warn("random: negative entropy/overflow: pool %s count %d\n", 683f80bbd8bSTheodore Ts'o r->name, entropy_count); 684f80bbd8bSTheodore Ts'o WARN_ON(1); 6858b76f46aSAndrew Morton entropy_count = 0; 68630e37ec5SH. Peter Anvin } else if (entropy_count > pool_size) 68730e37ec5SH. Peter Anvin entropy_count = pool_size; 688902c098aSTheodore Ts'o if (cmpxchg(&r->entropy_count, orig, entropy_count) != orig) 689902c098aSTheodore Ts'o goto retry; 6901da177e4SLinus Torvalds 691775f4b29STheodore Ts'o r->entropy_total += nbits; 6920891ad82SLinus Torvalds if (!r->initialized && r->entropy_total > 128) { 693775f4b29STheodore Ts'o r->initialized = 1; 6946265e169STheodore Ts'o r->entropy_total = 0; 695775f4b29STheodore Ts'o } 696775f4b29STheodore Ts'o 697a283b5c4SH. Peter Anvin trace_credit_entropy_bits(r->name, nbits, 698a283b5c4SH. Peter Anvin entropy_count >> ENTROPY_SHIFT, 69900ce1db1STheodore Ts'o r->entropy_total, _RET_IP_); 70000ce1db1STheodore Ts'o 7016265e169STheodore Ts'o if (r == &input_pool) { 7027d1b08c4SGreg Price int entropy_bits = entropy_count >> ENTROPY_SHIFT; 7036265e169STheodore Ts'o 704e192be9dSTheodore Ts'o if (crng_init < 2 && entropy_bits >= 128) { 705e192be9dSTheodore Ts'o crng_reseed(&primary_crng, r); 706e192be9dSTheodore Ts'o entropy_bits = r->entropy_count >> ENTROPY_SHIFT; 707e192be9dSTheodore Ts'o } 708e192be9dSTheodore Ts'o 70988c730daSMatt Mackall /* should we wake readers? */ 7102132a96fSGreg Price if (entropy_bits >= random_read_wakeup_bits) { 71188c730daSMatt Mackall wake_up_interruptible(&random_read_wait); 7129a6f70bbSJeff Dike kill_fasync(&fasync, SIGIO, POLL_IN); 7139a6f70bbSJeff Dike } 7146265e169STheodore Ts'o /* If the input pool is getting full, send some 715e192be9dSTheodore Ts'o * entropy to the blocking pool until it is 75% full. 7166265e169STheodore Ts'o */ 7172132a96fSGreg Price if (entropy_bits > random_write_wakeup_bits && 7186265e169STheodore Ts'o r->initialized && 7192132a96fSGreg Price r->entropy_total >= 2*random_read_wakeup_bits) { 7206265e169STheodore Ts'o struct entropy_store *other = &blocking_pool; 7216265e169STheodore Ts'o 7226265e169STheodore Ts'o if (other->entropy_count <= 723e192be9dSTheodore Ts'o 3 * other->poolinfo->poolfracbits / 4) { 724e192be9dSTheodore Ts'o schedule_work(&other->push_work); 7256265e169STheodore Ts'o r->entropy_total = 0; 7266265e169STheodore Ts'o } 7276265e169STheodore Ts'o } 7286265e169STheodore Ts'o } 7291da177e4SLinus Torvalds } 7301da177e4SLinus Torvalds 73186a574deSTheodore Ts'o static int credit_entropy_bits_safe(struct entropy_store *r, int nbits) 732a283b5c4SH. Peter Anvin { 733a283b5c4SH. Peter Anvin const int nbits_max = (int)(~0U >> (ENTROPY_SHIFT + 1)); 734a283b5c4SH. Peter Anvin 73586a574deSTheodore Ts'o if (nbits < 0) 73686a574deSTheodore Ts'o return -EINVAL; 73786a574deSTheodore Ts'o 738a283b5c4SH. Peter Anvin /* Cap the value to avoid overflows */ 739a283b5c4SH. Peter Anvin nbits = min(nbits, nbits_max); 740a283b5c4SH. Peter Anvin 741a283b5c4SH. Peter Anvin credit_entropy_bits(r, nbits); 74286a574deSTheodore Ts'o return 0; 7431da177e4SLinus Torvalds } 7441da177e4SLinus Torvalds 7451da177e4SLinus Torvalds /********************************************************************* 7461da177e4SLinus Torvalds * 747e192be9dSTheodore Ts'o * CRNG using CHACHA20 748e192be9dSTheodore Ts'o * 749e192be9dSTheodore Ts'o *********************************************************************/ 750e192be9dSTheodore Ts'o 751e192be9dSTheodore Ts'o #define CRNG_RESEED_INTERVAL (300*HZ) 752e192be9dSTheodore Ts'o 753e192be9dSTheodore Ts'o static DECLARE_WAIT_QUEUE_HEAD(crng_init_wait); 754e192be9dSTheodore Ts'o 7551e7f583aSTheodore Ts'o #ifdef CONFIG_NUMA 7561e7f583aSTheodore Ts'o /* 7571e7f583aSTheodore Ts'o * Hack to deal with crazy userspace progams when they are all trying 7581e7f583aSTheodore Ts'o * to access /dev/urandom in parallel. The programs are almost 7591e7f583aSTheodore Ts'o * certainly doing something terribly wrong, but we'll work around 7601e7f583aSTheodore Ts'o * their brain damage. 7611e7f583aSTheodore Ts'o */ 7621e7f583aSTheodore Ts'o static struct crng_state **crng_node_pool __read_mostly; 7631e7f583aSTheodore Ts'o #endif 7641e7f583aSTheodore Ts'o 765e192be9dSTheodore Ts'o static void crng_initialize(struct crng_state *crng) 766e192be9dSTheodore Ts'o { 767e192be9dSTheodore Ts'o int i; 768e192be9dSTheodore Ts'o unsigned long rv; 769e192be9dSTheodore Ts'o 770e192be9dSTheodore Ts'o memcpy(&crng->state[0], "expand 32-byte k", 16); 771e192be9dSTheodore Ts'o if (crng == &primary_crng) 772e192be9dSTheodore Ts'o _extract_entropy(&input_pool, &crng->state[4], 773e192be9dSTheodore Ts'o sizeof(__u32) * 12, 0); 774e192be9dSTheodore Ts'o else 775e192be9dSTheodore Ts'o get_random_bytes(&crng->state[4], sizeof(__u32) * 12); 776e192be9dSTheodore Ts'o for (i = 4; i < 16; i++) { 777e192be9dSTheodore Ts'o if (!arch_get_random_seed_long(&rv) && 778e192be9dSTheodore Ts'o !arch_get_random_long(&rv)) 779e192be9dSTheodore Ts'o rv = random_get_entropy(); 780e192be9dSTheodore Ts'o crng->state[i] ^= rv; 781e192be9dSTheodore Ts'o } 782e192be9dSTheodore Ts'o crng->init_time = jiffies - CRNG_RESEED_INTERVAL - 1; 783e192be9dSTheodore Ts'o } 784e192be9dSTheodore Ts'o 785e192be9dSTheodore Ts'o static int crng_fast_load(const char *cp, size_t len) 786e192be9dSTheodore Ts'o { 787e192be9dSTheodore Ts'o unsigned long flags; 788e192be9dSTheodore Ts'o char *p; 789e192be9dSTheodore Ts'o 790e192be9dSTheodore Ts'o if (!spin_trylock_irqsave(&primary_crng.lock, flags)) 791e192be9dSTheodore Ts'o return 0; 792e192be9dSTheodore Ts'o if (crng_ready()) { 793e192be9dSTheodore Ts'o spin_unlock_irqrestore(&primary_crng.lock, flags); 794e192be9dSTheodore Ts'o return 0; 795e192be9dSTheodore Ts'o } 796e192be9dSTheodore Ts'o p = (unsigned char *) &primary_crng.state[4]; 797e192be9dSTheodore Ts'o while (len > 0 && crng_init_cnt < CRNG_INIT_CNT_THRESH) { 798e192be9dSTheodore Ts'o p[crng_init_cnt % CHACHA20_KEY_SIZE] ^= *cp; 799e192be9dSTheodore Ts'o cp++; crng_init_cnt++; len--; 800e192be9dSTheodore Ts'o } 801e192be9dSTheodore Ts'o if (crng_init_cnt >= CRNG_INIT_CNT_THRESH) { 802e192be9dSTheodore Ts'o crng_init = 1; 803e192be9dSTheodore Ts'o wake_up_interruptible(&crng_init_wait); 804e192be9dSTheodore Ts'o pr_notice("random: fast init done\n"); 805e192be9dSTheodore Ts'o } 806e192be9dSTheodore Ts'o spin_unlock_irqrestore(&primary_crng.lock, flags); 807e192be9dSTheodore Ts'o return 1; 808e192be9dSTheodore Ts'o } 809e192be9dSTheodore Ts'o 810e192be9dSTheodore Ts'o static void crng_reseed(struct crng_state *crng, struct entropy_store *r) 811e192be9dSTheodore Ts'o { 812e192be9dSTheodore Ts'o unsigned long flags; 813e192be9dSTheodore Ts'o int i, num; 814e192be9dSTheodore Ts'o union { 815e192be9dSTheodore Ts'o __u8 block[CHACHA20_BLOCK_SIZE]; 816e192be9dSTheodore Ts'o __u32 key[8]; 817e192be9dSTheodore Ts'o } buf; 818e192be9dSTheodore Ts'o 819e192be9dSTheodore Ts'o if (r) { 820e192be9dSTheodore Ts'o num = extract_entropy(r, &buf, 32, 16, 0); 821e192be9dSTheodore Ts'o if (num == 0) 822e192be9dSTheodore Ts'o return; 823c92e040dSTheodore Ts'o } else { 8241e7f583aSTheodore Ts'o _extract_crng(&primary_crng, buf.block); 825c92e040dSTheodore Ts'o _crng_backtrack_protect(&primary_crng, buf.block, 826c92e040dSTheodore Ts'o CHACHA20_KEY_SIZE); 827c92e040dSTheodore Ts'o } 828e192be9dSTheodore Ts'o spin_lock_irqsave(&primary_crng.lock, flags); 829e192be9dSTheodore Ts'o for (i = 0; i < 8; i++) { 830e192be9dSTheodore Ts'o unsigned long rv; 831e192be9dSTheodore Ts'o if (!arch_get_random_seed_long(&rv) && 832e192be9dSTheodore Ts'o !arch_get_random_long(&rv)) 833e192be9dSTheodore Ts'o rv = random_get_entropy(); 834e192be9dSTheodore Ts'o crng->state[i+4] ^= buf.key[i] ^ rv; 835e192be9dSTheodore Ts'o } 836e192be9dSTheodore Ts'o memzero_explicit(&buf, sizeof(buf)); 837e192be9dSTheodore Ts'o crng->init_time = jiffies; 838e192be9dSTheodore Ts'o if (crng == &primary_crng && crng_init < 2) { 839e192be9dSTheodore Ts'o crng_init = 2; 840e192be9dSTheodore Ts'o process_random_ready_list(); 841e192be9dSTheodore Ts'o wake_up_interruptible(&crng_init_wait); 842e192be9dSTheodore Ts'o pr_notice("random: crng init done\n"); 843e192be9dSTheodore Ts'o } 844e192be9dSTheodore Ts'o spin_unlock_irqrestore(&primary_crng.lock, flags); 845e192be9dSTheodore Ts'o } 846e192be9dSTheodore Ts'o 847e192be9dSTheodore Ts'o static inline void crng_wait_ready(void) 848e192be9dSTheodore Ts'o { 849e192be9dSTheodore Ts'o wait_event_interruptible(crng_init_wait, crng_ready()); 850e192be9dSTheodore Ts'o } 851e192be9dSTheodore Ts'o 8521e7f583aSTheodore Ts'o static void _extract_crng(struct crng_state *crng, 8531e7f583aSTheodore Ts'o __u8 out[CHACHA20_BLOCK_SIZE]) 854e192be9dSTheodore Ts'o { 855e192be9dSTheodore Ts'o unsigned long v, flags; 856e192be9dSTheodore Ts'o 857e192be9dSTheodore Ts'o if (crng_init > 1 && 858e192be9dSTheodore Ts'o time_after(jiffies, crng->init_time + CRNG_RESEED_INTERVAL)) 8591e7f583aSTheodore Ts'o crng_reseed(crng, crng == &primary_crng ? &input_pool : NULL); 860e192be9dSTheodore Ts'o spin_lock_irqsave(&crng->lock, flags); 861e192be9dSTheodore Ts'o if (arch_get_random_long(&v)) 862e192be9dSTheodore Ts'o crng->state[14] ^= v; 863e192be9dSTheodore Ts'o chacha20_block(&crng->state[0], out); 864e192be9dSTheodore Ts'o if (crng->state[12] == 0) 865e192be9dSTheodore Ts'o crng->state[13]++; 866e192be9dSTheodore Ts'o spin_unlock_irqrestore(&crng->lock, flags); 867e192be9dSTheodore Ts'o } 868e192be9dSTheodore Ts'o 8691e7f583aSTheodore Ts'o static void extract_crng(__u8 out[CHACHA20_BLOCK_SIZE]) 8701e7f583aSTheodore Ts'o { 8711e7f583aSTheodore Ts'o struct crng_state *crng = NULL; 8721e7f583aSTheodore Ts'o 8731e7f583aSTheodore Ts'o #ifdef CONFIG_NUMA 8741e7f583aSTheodore Ts'o if (crng_node_pool) 8751e7f583aSTheodore Ts'o crng = crng_node_pool[numa_node_id()]; 8761e7f583aSTheodore Ts'o if (crng == NULL) 8771e7f583aSTheodore Ts'o #endif 8781e7f583aSTheodore Ts'o crng = &primary_crng; 8791e7f583aSTheodore Ts'o _extract_crng(crng, out); 8801e7f583aSTheodore Ts'o } 8811e7f583aSTheodore Ts'o 882c92e040dSTheodore Ts'o /* 883c92e040dSTheodore Ts'o * Use the leftover bytes from the CRNG block output (if there is 884c92e040dSTheodore Ts'o * enough) to mutate the CRNG key to provide backtracking protection. 885c92e040dSTheodore Ts'o */ 886c92e040dSTheodore Ts'o static void _crng_backtrack_protect(struct crng_state *crng, 887c92e040dSTheodore Ts'o __u8 tmp[CHACHA20_BLOCK_SIZE], int used) 888c92e040dSTheodore Ts'o { 889c92e040dSTheodore Ts'o unsigned long flags; 890c92e040dSTheodore Ts'o __u32 *s, *d; 891c92e040dSTheodore Ts'o int i; 892c92e040dSTheodore Ts'o 893c92e040dSTheodore Ts'o used = round_up(used, sizeof(__u32)); 894c92e040dSTheodore Ts'o if (used + CHACHA20_KEY_SIZE > CHACHA20_BLOCK_SIZE) { 895c92e040dSTheodore Ts'o extract_crng(tmp); 896c92e040dSTheodore Ts'o used = 0; 897c92e040dSTheodore Ts'o } 898c92e040dSTheodore Ts'o spin_lock_irqsave(&crng->lock, flags); 899c92e040dSTheodore Ts'o s = (__u32 *) &tmp[used]; 900c92e040dSTheodore Ts'o d = &crng->state[4]; 901c92e040dSTheodore Ts'o for (i=0; i < 8; i++) 902c92e040dSTheodore Ts'o *d++ ^= *s++; 903c92e040dSTheodore Ts'o spin_unlock_irqrestore(&crng->lock, flags); 904c92e040dSTheodore Ts'o } 905c92e040dSTheodore Ts'o 906c92e040dSTheodore Ts'o static void crng_backtrack_protect(__u8 tmp[CHACHA20_BLOCK_SIZE], int used) 907c92e040dSTheodore Ts'o { 908c92e040dSTheodore Ts'o struct crng_state *crng = NULL; 909c92e040dSTheodore Ts'o 910c92e040dSTheodore Ts'o #ifdef CONFIG_NUMA 911c92e040dSTheodore Ts'o if (crng_node_pool) 912c92e040dSTheodore Ts'o crng = crng_node_pool[numa_node_id()]; 913c92e040dSTheodore Ts'o if (crng == NULL) 914c92e040dSTheodore Ts'o #endif 915c92e040dSTheodore Ts'o crng = &primary_crng; 916c92e040dSTheodore Ts'o _crng_backtrack_protect(crng, tmp, used); 917c92e040dSTheodore Ts'o } 918c92e040dSTheodore Ts'o 919e192be9dSTheodore Ts'o static ssize_t extract_crng_user(void __user *buf, size_t nbytes) 920e192be9dSTheodore Ts'o { 921c92e040dSTheodore Ts'o ssize_t ret = 0, i = CHACHA20_BLOCK_SIZE; 922e192be9dSTheodore Ts'o __u8 tmp[CHACHA20_BLOCK_SIZE]; 923e192be9dSTheodore Ts'o int large_request = (nbytes > 256); 924e192be9dSTheodore Ts'o 925e192be9dSTheodore Ts'o while (nbytes) { 926e192be9dSTheodore Ts'o if (large_request && need_resched()) { 927e192be9dSTheodore Ts'o if (signal_pending(current)) { 928e192be9dSTheodore Ts'o if (ret == 0) 929e192be9dSTheodore Ts'o ret = -ERESTARTSYS; 930e192be9dSTheodore Ts'o break; 931e192be9dSTheodore Ts'o } 932e192be9dSTheodore Ts'o schedule(); 933e192be9dSTheodore Ts'o } 934e192be9dSTheodore Ts'o 935e192be9dSTheodore Ts'o extract_crng(tmp); 936e192be9dSTheodore Ts'o i = min_t(int, nbytes, CHACHA20_BLOCK_SIZE); 937e192be9dSTheodore Ts'o if (copy_to_user(buf, tmp, i)) { 938e192be9dSTheodore Ts'o ret = -EFAULT; 939e192be9dSTheodore Ts'o break; 940e192be9dSTheodore Ts'o } 941e192be9dSTheodore Ts'o 942e192be9dSTheodore Ts'o nbytes -= i; 943e192be9dSTheodore Ts'o buf += i; 944e192be9dSTheodore Ts'o ret += i; 945e192be9dSTheodore Ts'o } 946c92e040dSTheodore Ts'o crng_backtrack_protect(tmp, i); 947e192be9dSTheodore Ts'o 948e192be9dSTheodore Ts'o /* Wipe data just written to memory */ 949e192be9dSTheodore Ts'o memzero_explicit(tmp, sizeof(tmp)); 950e192be9dSTheodore Ts'o 951e192be9dSTheodore Ts'o return ret; 952e192be9dSTheodore Ts'o } 953e192be9dSTheodore Ts'o 954e192be9dSTheodore Ts'o 955e192be9dSTheodore Ts'o /********************************************************************* 956e192be9dSTheodore Ts'o * 9571da177e4SLinus Torvalds * Entropy input management 9581da177e4SLinus Torvalds * 9591da177e4SLinus Torvalds *********************************************************************/ 9601da177e4SLinus Torvalds 9611da177e4SLinus Torvalds /* There is one of these per entropy source */ 9621da177e4SLinus Torvalds struct timer_rand_state { 9631da177e4SLinus Torvalds cycles_t last_time; 9641da177e4SLinus Torvalds long last_delta, last_delta2; 9651da177e4SLinus Torvalds unsigned dont_count_entropy:1; 9661da177e4SLinus Torvalds }; 9671da177e4SLinus Torvalds 968644008dfSTheodore Ts'o #define INIT_TIMER_RAND_STATE { INITIAL_JIFFIES, }; 969644008dfSTheodore Ts'o 970a2080a67SLinus Torvalds /* 971e192be9dSTheodore Ts'o * Add device- or boot-specific data to the input pool to help 972e192be9dSTheodore Ts'o * initialize it. 973a2080a67SLinus Torvalds * 974e192be9dSTheodore Ts'o * None of this adds any entropy; it is meant to avoid the problem of 975e192be9dSTheodore Ts'o * the entropy pool having similar initial state across largely 976e192be9dSTheodore Ts'o * identical devices. 977a2080a67SLinus Torvalds */ 978a2080a67SLinus Torvalds void add_device_randomness(const void *buf, unsigned int size) 979a2080a67SLinus Torvalds { 98061875f30STheodore Ts'o unsigned long time = random_get_entropy() ^ jiffies; 9813ef4cb2dSTheodore Ts'o unsigned long flags; 982a2080a67SLinus Torvalds 9835910895fSTheodore Ts'o trace_add_device_randomness(size, _RET_IP_); 9843ef4cb2dSTheodore Ts'o spin_lock_irqsave(&input_pool.lock, flags); 98585608f8eSTheodore Ts'o _mix_pool_bytes(&input_pool, buf, size); 98685608f8eSTheodore Ts'o _mix_pool_bytes(&input_pool, &time, sizeof(time)); 9873ef4cb2dSTheodore Ts'o spin_unlock_irqrestore(&input_pool.lock, flags); 988a2080a67SLinus Torvalds } 989a2080a67SLinus Torvalds EXPORT_SYMBOL(add_device_randomness); 990a2080a67SLinus Torvalds 991644008dfSTheodore Ts'o static struct timer_rand_state input_timer_state = INIT_TIMER_RAND_STATE; 9923060d6feSYinghai Lu 9931da177e4SLinus Torvalds /* 9941da177e4SLinus Torvalds * This function adds entropy to the entropy "pool" by using timing 9951da177e4SLinus Torvalds * delays. It uses the timer_rand_state structure to make an estimate 9961da177e4SLinus Torvalds * of how many bits of entropy this call has added to the pool. 9971da177e4SLinus Torvalds * 9981da177e4SLinus Torvalds * The number "num" is also added to the pool - it should somehow describe 9991da177e4SLinus Torvalds * the type of event which just happened. This is currently 0-255 for 10001da177e4SLinus Torvalds * keyboard scan codes, and 256 upwards for interrupts. 10011da177e4SLinus Torvalds * 10021da177e4SLinus Torvalds */ 10031da177e4SLinus Torvalds static void add_timer_randomness(struct timer_rand_state *state, unsigned num) 10041da177e4SLinus Torvalds { 100540db23e5STheodore Ts'o struct entropy_store *r; 10061da177e4SLinus Torvalds struct { 10071da177e4SLinus Torvalds long jiffies; 1008cf833d0bSLinus Torvalds unsigned cycles; 10091da177e4SLinus Torvalds unsigned num; 10101da177e4SLinus Torvalds } sample; 10111da177e4SLinus Torvalds long delta, delta2, delta3; 10121da177e4SLinus Torvalds 10131da177e4SLinus Torvalds preempt_disable(); 10141da177e4SLinus Torvalds 10151da177e4SLinus Torvalds sample.jiffies = jiffies; 101661875f30STheodore Ts'o sample.cycles = random_get_entropy(); 10171da177e4SLinus Torvalds sample.num = num; 1018e192be9dSTheodore Ts'o r = &input_pool; 101985608f8eSTheodore Ts'o mix_pool_bytes(r, &sample, sizeof(sample)); 10201da177e4SLinus Torvalds 10211da177e4SLinus Torvalds /* 10221da177e4SLinus Torvalds * Calculate number of bits of randomness we probably added. 10231da177e4SLinus Torvalds * We take into account the first, second and third-order deltas 10241da177e4SLinus Torvalds * in order to make our estimate. 10251da177e4SLinus Torvalds */ 10261da177e4SLinus Torvalds 10271da177e4SLinus Torvalds if (!state->dont_count_entropy) { 10281da177e4SLinus Torvalds delta = sample.jiffies - state->last_time; 10291da177e4SLinus Torvalds state->last_time = sample.jiffies; 10301da177e4SLinus Torvalds 10311da177e4SLinus Torvalds delta2 = delta - state->last_delta; 10321da177e4SLinus Torvalds state->last_delta = delta; 10331da177e4SLinus Torvalds 10341da177e4SLinus Torvalds delta3 = delta2 - state->last_delta2; 10351da177e4SLinus Torvalds state->last_delta2 = delta2; 10361da177e4SLinus Torvalds 10371da177e4SLinus Torvalds if (delta < 0) 10381da177e4SLinus Torvalds delta = -delta; 10391da177e4SLinus Torvalds if (delta2 < 0) 10401da177e4SLinus Torvalds delta2 = -delta2; 10411da177e4SLinus Torvalds if (delta3 < 0) 10421da177e4SLinus Torvalds delta3 = -delta3; 10431da177e4SLinus Torvalds if (delta > delta2) 10441da177e4SLinus Torvalds delta = delta2; 10451da177e4SLinus Torvalds if (delta > delta3) 10461da177e4SLinus Torvalds delta = delta3; 10471da177e4SLinus Torvalds 10481da177e4SLinus Torvalds /* 10491da177e4SLinus Torvalds * delta is now minimum absolute delta. 10501da177e4SLinus Torvalds * Round down by 1 bit on general principles, 10511da177e4SLinus Torvalds * and limit entropy entimate to 12 bits. 10521da177e4SLinus Torvalds */ 105340db23e5STheodore Ts'o credit_entropy_bits(r, min_t(int, fls(delta>>1), 11)); 10541da177e4SLinus Torvalds } 10551da177e4SLinus Torvalds preempt_enable(); 10561da177e4SLinus Torvalds } 10571da177e4SLinus Torvalds 1058d251575aSStephen Hemminger void add_input_randomness(unsigned int type, unsigned int code, 10591da177e4SLinus Torvalds unsigned int value) 10601da177e4SLinus Torvalds { 10611da177e4SLinus Torvalds static unsigned char last_value; 10621da177e4SLinus Torvalds 10631da177e4SLinus Torvalds /* ignore autorepeat and the like */ 10641da177e4SLinus Torvalds if (value == last_value) 10651da177e4SLinus Torvalds return; 10661da177e4SLinus Torvalds 10671da177e4SLinus Torvalds last_value = value; 10681da177e4SLinus Torvalds add_timer_randomness(&input_timer_state, 10691da177e4SLinus Torvalds (type << 4) ^ code ^ (code >> 4) ^ value); 1070f80bbd8bSTheodore Ts'o trace_add_input_randomness(ENTROPY_BITS(&input_pool)); 10711da177e4SLinus Torvalds } 107280fc9f53SDmitry Torokhov EXPORT_SYMBOL_GPL(add_input_randomness); 10731da177e4SLinus Torvalds 1074775f4b29STheodore Ts'o static DEFINE_PER_CPU(struct fast_pool, irq_randomness); 1075775f4b29STheodore Ts'o 107643759d4fSTheodore Ts'o #ifdef ADD_INTERRUPT_BENCH 107743759d4fSTheodore Ts'o static unsigned long avg_cycles, avg_deviation; 107843759d4fSTheodore Ts'o 107943759d4fSTheodore Ts'o #define AVG_SHIFT 8 /* Exponential average factor k=1/256 */ 108043759d4fSTheodore Ts'o #define FIXED_1_2 (1 << (AVG_SHIFT-1)) 108143759d4fSTheodore Ts'o 108243759d4fSTheodore Ts'o static void add_interrupt_bench(cycles_t start) 108343759d4fSTheodore Ts'o { 108443759d4fSTheodore Ts'o long delta = random_get_entropy() - start; 108543759d4fSTheodore Ts'o 108643759d4fSTheodore Ts'o /* Use a weighted moving average */ 108743759d4fSTheodore Ts'o delta = delta - ((avg_cycles + FIXED_1_2) >> AVG_SHIFT); 108843759d4fSTheodore Ts'o avg_cycles += delta; 108943759d4fSTheodore Ts'o /* And average deviation */ 109043759d4fSTheodore Ts'o delta = abs(delta) - ((avg_deviation + FIXED_1_2) >> AVG_SHIFT); 109143759d4fSTheodore Ts'o avg_deviation += delta; 109243759d4fSTheodore Ts'o } 109343759d4fSTheodore Ts'o #else 109443759d4fSTheodore Ts'o #define add_interrupt_bench(x) 109543759d4fSTheodore Ts'o #endif 109643759d4fSTheodore Ts'o 1097ee3e00e9STheodore Ts'o static __u32 get_reg(struct fast_pool *f, struct pt_regs *regs) 1098ee3e00e9STheodore Ts'o { 1099ee3e00e9STheodore Ts'o __u32 *ptr = (__u32 *) regs; 1100*9dfa7bbaSMichael Schmitz unsigned long flags; 1101ee3e00e9STheodore Ts'o 1102ee3e00e9STheodore Ts'o if (regs == NULL) 1103ee3e00e9STheodore Ts'o return 0; 1104*9dfa7bbaSMichael Schmitz local_irq_save(flags); 1105ee3e00e9STheodore Ts'o if (f->reg_idx >= sizeof(struct pt_regs) / sizeof(__u32)) 1106ee3e00e9STheodore Ts'o f->reg_idx = 0; 1107*9dfa7bbaSMichael Schmitz ptr += f->reg_idx++; 1108*9dfa7bbaSMichael Schmitz local_irq_restore(flags); 1109*9dfa7bbaSMichael Schmitz return *ptr; 1110ee3e00e9STheodore Ts'o } 1111ee3e00e9STheodore Ts'o 1112775f4b29STheodore Ts'o void add_interrupt_randomness(int irq, int irq_flags) 11131da177e4SLinus Torvalds { 1114775f4b29STheodore Ts'o struct entropy_store *r; 11151b2a1a7eSChristoph Lameter struct fast_pool *fast_pool = this_cpu_ptr(&irq_randomness); 1116775f4b29STheodore Ts'o struct pt_regs *regs = get_irq_regs(); 1117775f4b29STheodore Ts'o unsigned long now = jiffies; 1118655b2264STheodore Ts'o cycles_t cycles = random_get_entropy(); 111943759d4fSTheodore Ts'o __u32 c_high, j_high; 1120655b2264STheodore Ts'o __u64 ip; 112183664a69SH. Peter Anvin unsigned long seed; 112291fcb532STheodore Ts'o int credit = 0; 11233060d6feSYinghai Lu 1124ee3e00e9STheodore Ts'o if (cycles == 0) 1125ee3e00e9STheodore Ts'o cycles = get_reg(fast_pool, regs); 1126655b2264STheodore Ts'o c_high = (sizeof(cycles) > 4) ? cycles >> 32 : 0; 1127655b2264STheodore Ts'o j_high = (sizeof(now) > 4) ? now >> 32 : 0; 112843759d4fSTheodore Ts'o fast_pool->pool[0] ^= cycles ^ j_high ^ irq; 112943759d4fSTheodore Ts'o fast_pool->pool[1] ^= now ^ c_high; 1130655b2264STheodore Ts'o ip = regs ? instruction_pointer(regs) : _RET_IP_; 113143759d4fSTheodore Ts'o fast_pool->pool[2] ^= ip; 1132ee3e00e9STheodore Ts'o fast_pool->pool[3] ^= (sizeof(ip) > 4) ? ip >> 32 : 1133ee3e00e9STheodore Ts'o get_reg(fast_pool, regs); 11343060d6feSYinghai Lu 113543759d4fSTheodore Ts'o fast_mix(fast_pool); 113643759d4fSTheodore Ts'o add_interrupt_bench(cycles); 1137775f4b29STheodore Ts'o 1138e192be9dSTheodore Ts'o if (!crng_ready()) { 1139e192be9dSTheodore Ts'o if ((fast_pool->count >= 64) && 1140e192be9dSTheodore Ts'o crng_fast_load((char *) fast_pool->pool, 1141e192be9dSTheodore Ts'o sizeof(fast_pool->pool))) { 1142e192be9dSTheodore Ts'o fast_pool->count = 0; 1143e192be9dSTheodore Ts'o fast_pool->last = now; 1144e192be9dSTheodore Ts'o } 1145e192be9dSTheodore Ts'o return; 1146e192be9dSTheodore Ts'o } 1147e192be9dSTheodore Ts'o 1148840f9507STheodore Ts'o if ((fast_pool->count < 64) && 1149840f9507STheodore Ts'o !time_after(now, fast_pool->last + HZ)) 11501da177e4SLinus Torvalds return; 1151840f9507STheodore Ts'o 1152e192be9dSTheodore Ts'o r = &input_pool; 1153840f9507STheodore Ts'o if (!spin_trylock(&r->lock)) 11541da177e4SLinus Torvalds return; 11551da177e4SLinus Torvalds 1156775f4b29STheodore Ts'o fast_pool->last = now; 115785608f8eSTheodore Ts'o __mix_pool_bytes(r, &fast_pool->pool, sizeof(fast_pool->pool)); 115883664a69SH. Peter Anvin 115983664a69SH. Peter Anvin /* 116083664a69SH. Peter Anvin * If we have architectural seed generator, produce a seed and 116148d6be95STheodore Ts'o * add it to the pool. For the sake of paranoia don't let the 116248d6be95STheodore Ts'o * architectural seed generator dominate the input from the 116348d6be95STheodore Ts'o * interrupt noise. 116483664a69SH. Peter Anvin */ 116583664a69SH. Peter Anvin if (arch_get_random_seed_long(&seed)) { 116685608f8eSTheodore Ts'o __mix_pool_bytes(r, &seed, sizeof(seed)); 116748d6be95STheodore Ts'o credit = 1; 116883664a69SH. Peter Anvin } 116991fcb532STheodore Ts'o spin_unlock(&r->lock); 117083664a69SH. Peter Anvin 1171ee3e00e9STheodore Ts'o fast_pool->count = 0; 1172840f9507STheodore Ts'o 1173ee3e00e9STheodore Ts'o /* award one bit for the contents of the fast pool */ 1174ee3e00e9STheodore Ts'o credit_entropy_bits(r, credit + 1); 11751da177e4SLinus Torvalds } 11764b44f2d1SStephan Mueller EXPORT_SYMBOL_GPL(add_interrupt_randomness); 11771da177e4SLinus Torvalds 11789361401eSDavid Howells #ifdef CONFIG_BLOCK 11791da177e4SLinus Torvalds void add_disk_randomness(struct gendisk *disk) 11801da177e4SLinus Torvalds { 11811da177e4SLinus Torvalds if (!disk || !disk->random) 11821da177e4SLinus Torvalds return; 11831da177e4SLinus Torvalds /* first major is 1, so we get >= 0x200 here */ 1184f331c029STejun Heo add_timer_randomness(disk->random, 0x100 + disk_devt(disk)); 1185f80bbd8bSTheodore Ts'o trace_add_disk_randomness(disk_devt(disk), ENTROPY_BITS(&input_pool)); 11861da177e4SLinus Torvalds } 1187bdcfa3e5SChristoph Hellwig EXPORT_SYMBOL_GPL(add_disk_randomness); 11889361401eSDavid Howells #endif 11891da177e4SLinus Torvalds 11901da177e4SLinus Torvalds /********************************************************************* 11911da177e4SLinus Torvalds * 11921da177e4SLinus Torvalds * Entropy extraction routines 11931da177e4SLinus Torvalds * 11941da177e4SLinus Torvalds *********************************************************************/ 11951da177e4SLinus Torvalds 11961da177e4SLinus Torvalds /* 119725985edcSLucas De Marchi * This utility inline function is responsible for transferring entropy 11981da177e4SLinus Torvalds * from the primary pool to the secondary extraction pool. We make 11991da177e4SLinus Torvalds * sure we pull enough for a 'catastrophic reseed'. 12001da177e4SLinus Torvalds */ 12016265e169STheodore Ts'o static void _xfer_secondary_pool(struct entropy_store *r, size_t nbytes); 12021da177e4SLinus Torvalds static void xfer_secondary_pool(struct entropy_store *r, size_t nbytes) 12031da177e4SLinus Torvalds { 1204cff85031STheodore Ts'o if (!r->pull || 1205cff85031STheodore Ts'o r->entropy_count >= (nbytes << (ENTROPY_SHIFT + 3)) || 1206cff85031STheodore Ts'o r->entropy_count > r->poolinfo->poolfracbits) 1207cff85031STheodore Ts'o return; 1208cff85031STheodore Ts'o 12096265e169STheodore Ts'o _xfer_secondary_pool(r, nbytes); 12106265e169STheodore Ts'o } 12116265e169STheodore Ts'o 12126265e169STheodore Ts'o static void _xfer_secondary_pool(struct entropy_store *r, size_t nbytes) 12136265e169STheodore Ts'o { 12141da177e4SLinus Torvalds __u32 tmp[OUTPUT_POOL_WORDS]; 12151da177e4SLinus Torvalds 12165a021e9fSMatt Mackall int bytes = nbytes; 12175a021e9fSMatt Mackall 12182132a96fSGreg Price /* pull at least as much as a wakeup */ 12192132a96fSGreg Price bytes = max_t(int, bytes, random_read_wakeup_bits / 8); 12205a021e9fSMatt Mackall /* but never more than the buffer size */ 1221d2e7c96aSH. Peter Anvin bytes = min_t(int, bytes, sizeof(tmp)); 12221da177e4SLinus Torvalds 1223f80bbd8bSTheodore Ts'o trace_xfer_secondary_pool(r->name, bytes * 8, nbytes * 8, 1224f80bbd8bSTheodore Ts'o ENTROPY_BITS(r), ENTROPY_BITS(r->pull)); 1225d2e7c96aSH. Peter Anvin bytes = extract_entropy(r->pull, tmp, bytes, 122643d8a72cSStephan Müller random_read_wakeup_bits / 8, 0); 122785608f8eSTheodore Ts'o mix_pool_bytes(r, tmp, bytes); 1228adc782daSMatt Mackall credit_entropy_bits(r, bytes*8); 12291da177e4SLinus Torvalds } 12306265e169STheodore Ts'o 12316265e169STheodore Ts'o /* 12326265e169STheodore Ts'o * Used as a workqueue function so that when the input pool is getting 12336265e169STheodore Ts'o * full, we can "spill over" some entropy to the output pools. That 12346265e169STheodore Ts'o * way the output pools can store some of the excess entropy instead 12356265e169STheodore Ts'o * of letting it go to waste. 12366265e169STheodore Ts'o */ 12376265e169STheodore Ts'o static void push_to_pool(struct work_struct *work) 12386265e169STheodore Ts'o { 12396265e169STheodore Ts'o struct entropy_store *r = container_of(work, struct entropy_store, 12406265e169STheodore Ts'o push_work); 12416265e169STheodore Ts'o BUG_ON(!r); 12422132a96fSGreg Price _xfer_secondary_pool(r, random_read_wakeup_bits/8); 12436265e169STheodore Ts'o trace_push_to_pool(r->name, r->entropy_count >> ENTROPY_SHIFT, 12446265e169STheodore Ts'o r->pull->entropy_count >> ENTROPY_SHIFT); 12451da177e4SLinus Torvalds } 12461da177e4SLinus Torvalds 12471da177e4SLinus Torvalds /* 124819fa5be1SGreg Price * This function decides how many bytes to actually take from the 124919fa5be1SGreg Price * given pool, and also debits the entropy count accordingly. 12501da177e4SLinus Torvalds */ 12511da177e4SLinus Torvalds static size_t account(struct entropy_store *r, size_t nbytes, int min, 12521da177e4SLinus Torvalds int reserved) 12531da177e4SLinus Torvalds { 125443d8a72cSStephan Müller int entropy_count, orig, have_bytes; 125579a84687SHannes Frederic Sowa size_t ibytes, nfrac; 12561da177e4SLinus Torvalds 1257a283b5c4SH. Peter Anvin BUG_ON(r->entropy_count > r->poolinfo->poolfracbits); 12581da177e4SLinus Torvalds 12591da177e4SLinus Torvalds /* Can we pull enough? */ 126010b3a32dSJiri Kosina retry: 126110b3a32dSJiri Kosina entropy_count = orig = ACCESS_ONCE(r->entropy_count); 1262a283b5c4SH. Peter Anvin ibytes = nbytes; 126343d8a72cSStephan Müller /* never pull more than available */ 126443d8a72cSStephan Müller have_bytes = entropy_count >> (ENTROPY_SHIFT + 3); 1265e33ba5faSTheodore Ts'o 1266e33ba5faSTheodore Ts'o if ((have_bytes -= reserved) < 0) 1267e33ba5faSTheodore Ts'o have_bytes = 0; 1268e33ba5faSTheodore Ts'o ibytes = min_t(size_t, ibytes, have_bytes); 12690fb7a01aSGreg Price if (ibytes < min) 12700fb7a01aSGreg Price ibytes = 0; 127179a84687SHannes Frederic Sowa 127279a84687SHannes Frederic Sowa if (unlikely(entropy_count < 0)) { 127379a84687SHannes Frederic Sowa pr_warn("random: negative entropy count: pool %s count %d\n", 127479a84687SHannes Frederic Sowa r->name, entropy_count); 127579a84687SHannes Frederic Sowa WARN_ON(1); 127679a84687SHannes Frederic Sowa entropy_count = 0; 127779a84687SHannes Frederic Sowa } 127879a84687SHannes Frederic Sowa nfrac = ibytes << (ENTROPY_SHIFT + 3); 127979a84687SHannes Frederic Sowa if ((size_t) entropy_count > nfrac) 128079a84687SHannes Frederic Sowa entropy_count -= nfrac; 128179a84687SHannes Frederic Sowa else 1282e33ba5faSTheodore Ts'o entropy_count = 0; 1283f9c6d498STheodore Ts'o 128410b3a32dSJiri Kosina if (cmpxchg(&r->entropy_count, orig, entropy_count) != orig) 128510b3a32dSJiri Kosina goto retry; 12861da177e4SLinus Torvalds 1287f80bbd8bSTheodore Ts'o trace_debit_entropy(r->name, 8 * ibytes); 12880fb7a01aSGreg Price if (ibytes && 12892132a96fSGreg Price (r->entropy_count >> ENTROPY_SHIFT) < random_write_wakeup_bits) { 1290b9809552STheodore Ts'o wake_up_interruptible(&random_write_wait); 1291b9809552STheodore Ts'o kill_fasync(&fasync, SIGIO, POLL_OUT); 1292b9809552STheodore Ts'o } 1293b9809552STheodore Ts'o 1294a283b5c4SH. Peter Anvin return ibytes; 12951da177e4SLinus Torvalds } 12961da177e4SLinus Torvalds 129719fa5be1SGreg Price /* 129819fa5be1SGreg Price * This function does the actual extraction for extract_entropy and 129919fa5be1SGreg Price * extract_entropy_user. 130019fa5be1SGreg Price * 130119fa5be1SGreg Price * Note: we assume that .poolwords is a multiple of 16 words. 130219fa5be1SGreg Price */ 13031da177e4SLinus Torvalds static void extract_buf(struct entropy_store *r, __u8 *out) 13041da177e4SLinus Torvalds { 1305602b6aeeSMatt Mackall int i; 1306d2e7c96aSH. Peter Anvin union { 1307d2e7c96aSH. Peter Anvin __u32 w[5]; 130885a1f777STheodore Ts'o unsigned long l[LONGS(20)]; 1309d2e7c96aSH. Peter Anvin } hash; 1310d2e7c96aSH. Peter Anvin __u32 workspace[SHA_WORKSPACE_WORDS]; 1311902c098aSTheodore Ts'o unsigned long flags; 13121da177e4SLinus Torvalds 13131da177e4SLinus Torvalds /* 1314dfd38750SGreg Price * If we have an architectural hardware random number 131546884442STheodore Ts'o * generator, use it for SHA's initial vector 131685a1f777STheodore Ts'o */ 131746884442STheodore Ts'o sha_init(hash.w); 131885a1f777STheodore Ts'o for (i = 0; i < LONGS(20); i++) { 131985a1f777STheodore Ts'o unsigned long v; 132085a1f777STheodore Ts'o if (!arch_get_random_long(&v)) 132185a1f777STheodore Ts'o break; 132246884442STheodore Ts'o hash.l[i] = v; 132385a1f777STheodore Ts'o } 132485a1f777STheodore Ts'o 132546884442STheodore Ts'o /* Generate a hash across the pool, 16 words (512 bits) at a time */ 132646884442STheodore Ts'o spin_lock_irqsave(&r->lock, flags); 132746884442STheodore Ts'o for (i = 0; i < r->poolinfo->poolwords; i += 16) 132846884442STheodore Ts'o sha_transform(hash.w, (__u8 *)(r->pool + i), workspace); 132946884442STheodore Ts'o 133085a1f777STheodore Ts'o /* 13311c0ad3d4SMatt Mackall * We mix the hash back into the pool to prevent backtracking 13321c0ad3d4SMatt Mackall * attacks (where the attacker knows the state of the pool 13331c0ad3d4SMatt Mackall * plus the current outputs, and attempts to find previous 13341c0ad3d4SMatt Mackall * ouputs), unless the hash function can be inverted. By 13351c0ad3d4SMatt Mackall * mixing at least a SHA1 worth of hash data back, we make 13361c0ad3d4SMatt Mackall * brute-forcing the feedback as hard as brute-forcing the 13371c0ad3d4SMatt Mackall * hash. 13381da177e4SLinus Torvalds */ 133985608f8eSTheodore Ts'o __mix_pool_bytes(r, hash.w, sizeof(hash.w)); 1340902c098aSTheodore Ts'o spin_unlock_irqrestore(&r->lock, flags); 13411c0ad3d4SMatt Mackall 1342d4c5efdbSDaniel Borkmann memzero_explicit(workspace, sizeof(workspace)); 13431da177e4SLinus Torvalds 13441da177e4SLinus Torvalds /* 13451c0ad3d4SMatt Mackall * In case the hash function has some recognizable output 13461c0ad3d4SMatt Mackall * pattern, we fold it in half. Thus, we always feed back 13471c0ad3d4SMatt Mackall * twice as much data as we output. 13481da177e4SLinus Torvalds */ 1349d2e7c96aSH. Peter Anvin hash.w[0] ^= hash.w[3]; 1350d2e7c96aSH. Peter Anvin hash.w[1] ^= hash.w[4]; 1351d2e7c96aSH. Peter Anvin hash.w[2] ^= rol32(hash.w[2], 16); 1352d2e7c96aSH. Peter Anvin 1353d2e7c96aSH. Peter Anvin memcpy(out, &hash, EXTRACT_SIZE); 1354d4c5efdbSDaniel Borkmann memzero_explicit(&hash, sizeof(hash)); 13551da177e4SLinus Torvalds } 13561da177e4SLinus Torvalds 1357e192be9dSTheodore Ts'o static ssize_t _extract_entropy(struct entropy_store *r, void *buf, 1358e192be9dSTheodore Ts'o size_t nbytes, int fips) 1359e192be9dSTheodore Ts'o { 1360e192be9dSTheodore Ts'o ssize_t ret = 0, i; 1361e192be9dSTheodore Ts'o __u8 tmp[EXTRACT_SIZE]; 1362e192be9dSTheodore Ts'o unsigned long flags; 1363e192be9dSTheodore Ts'o 1364e192be9dSTheodore Ts'o while (nbytes) { 1365e192be9dSTheodore Ts'o extract_buf(r, tmp); 1366e192be9dSTheodore Ts'o 1367e192be9dSTheodore Ts'o if (fips) { 1368e192be9dSTheodore Ts'o spin_lock_irqsave(&r->lock, flags); 1369e192be9dSTheodore Ts'o if (!memcmp(tmp, r->last_data, EXTRACT_SIZE)) 1370e192be9dSTheodore Ts'o panic("Hardware RNG duplicated output!\n"); 1371e192be9dSTheodore Ts'o memcpy(r->last_data, tmp, EXTRACT_SIZE); 1372e192be9dSTheodore Ts'o spin_unlock_irqrestore(&r->lock, flags); 1373e192be9dSTheodore Ts'o } 1374e192be9dSTheodore Ts'o i = min_t(int, nbytes, EXTRACT_SIZE); 1375e192be9dSTheodore Ts'o memcpy(buf, tmp, i); 1376e192be9dSTheodore Ts'o nbytes -= i; 1377e192be9dSTheodore Ts'o buf += i; 1378e192be9dSTheodore Ts'o ret += i; 1379e192be9dSTheodore Ts'o } 1380e192be9dSTheodore Ts'o 1381e192be9dSTheodore Ts'o /* Wipe data just returned from memory */ 1382e192be9dSTheodore Ts'o memzero_explicit(tmp, sizeof(tmp)); 1383e192be9dSTheodore Ts'o 1384e192be9dSTheodore Ts'o return ret; 1385e192be9dSTheodore Ts'o } 1386e192be9dSTheodore Ts'o 138719fa5be1SGreg Price /* 138819fa5be1SGreg Price * This function extracts randomness from the "entropy pool", and 138919fa5be1SGreg Price * returns it in a buffer. 139019fa5be1SGreg Price * 139119fa5be1SGreg Price * The min parameter specifies the minimum amount we can pull before 139219fa5be1SGreg Price * failing to avoid races that defeat catastrophic reseeding while the 139319fa5be1SGreg Price * reserved parameter indicates how much entropy we must leave in the 139419fa5be1SGreg Price * pool after each pull to avoid starving other readers. 139519fa5be1SGreg Price */ 13961da177e4SLinus Torvalds static ssize_t extract_entropy(struct entropy_store *r, void *buf, 13971da177e4SLinus Torvalds size_t nbytes, int min, int reserved) 13981da177e4SLinus Torvalds { 13991da177e4SLinus Torvalds __u8 tmp[EXTRACT_SIZE]; 14001e7e2e05SJarod Wilson unsigned long flags; 14011da177e4SLinus Torvalds 1402ec8f02daSJarod Wilson /* if last_data isn't primed, we need EXTRACT_SIZE extra bytes */ 14031e7e2e05SJarod Wilson if (fips_enabled) { 14041e7e2e05SJarod Wilson spin_lock_irqsave(&r->lock, flags); 14051e7e2e05SJarod Wilson if (!r->last_data_init) { 1406c59974aeSTheodore Ts'o r->last_data_init = 1; 14071e7e2e05SJarod Wilson spin_unlock_irqrestore(&r->lock, flags); 14081e7e2e05SJarod Wilson trace_extract_entropy(r->name, EXTRACT_SIZE, 1409a283b5c4SH. Peter Anvin ENTROPY_BITS(r), _RET_IP_); 14101e7e2e05SJarod Wilson xfer_secondary_pool(r, EXTRACT_SIZE); 14111e7e2e05SJarod Wilson extract_buf(r, tmp); 14121e7e2e05SJarod Wilson spin_lock_irqsave(&r->lock, flags); 14131e7e2e05SJarod Wilson memcpy(r->last_data, tmp, EXTRACT_SIZE); 14141e7e2e05SJarod Wilson } 14151e7e2e05SJarod Wilson spin_unlock_irqrestore(&r->lock, flags); 14161e7e2e05SJarod Wilson } 1417ec8f02daSJarod Wilson 1418a283b5c4SH. Peter Anvin trace_extract_entropy(r->name, nbytes, ENTROPY_BITS(r), _RET_IP_); 14191da177e4SLinus Torvalds xfer_secondary_pool(r, nbytes); 14201da177e4SLinus Torvalds nbytes = account(r, nbytes, min, reserved); 14211da177e4SLinus Torvalds 1422e192be9dSTheodore Ts'o return _extract_entropy(r, buf, nbytes, fips_enabled); 14231da177e4SLinus Torvalds } 14241da177e4SLinus Torvalds 142519fa5be1SGreg Price /* 142619fa5be1SGreg Price * This function extracts randomness from the "entropy pool", and 142719fa5be1SGreg Price * returns it in a userspace buffer. 142819fa5be1SGreg Price */ 14291da177e4SLinus Torvalds static ssize_t extract_entropy_user(struct entropy_store *r, void __user *buf, 14301da177e4SLinus Torvalds size_t nbytes) 14311da177e4SLinus Torvalds { 14321da177e4SLinus Torvalds ssize_t ret = 0, i; 14331da177e4SLinus Torvalds __u8 tmp[EXTRACT_SIZE]; 1434c6e9d6f3STheodore Ts'o int large_request = (nbytes > 256); 14351da177e4SLinus Torvalds 1436a283b5c4SH. Peter Anvin trace_extract_entropy_user(r->name, nbytes, ENTROPY_BITS(r), _RET_IP_); 14371da177e4SLinus Torvalds xfer_secondary_pool(r, nbytes); 14381da177e4SLinus Torvalds nbytes = account(r, nbytes, 0, 0); 14391da177e4SLinus Torvalds 14401da177e4SLinus Torvalds while (nbytes) { 1441c6e9d6f3STheodore Ts'o if (large_request && need_resched()) { 14421da177e4SLinus Torvalds if (signal_pending(current)) { 14431da177e4SLinus Torvalds if (ret == 0) 14441da177e4SLinus Torvalds ret = -ERESTARTSYS; 14451da177e4SLinus Torvalds break; 14461da177e4SLinus Torvalds } 14471da177e4SLinus Torvalds schedule(); 14481da177e4SLinus Torvalds } 14491da177e4SLinus Torvalds 14501da177e4SLinus Torvalds extract_buf(r, tmp); 14511da177e4SLinus Torvalds i = min_t(int, nbytes, EXTRACT_SIZE); 14521da177e4SLinus Torvalds if (copy_to_user(buf, tmp, i)) { 14531da177e4SLinus Torvalds ret = -EFAULT; 14541da177e4SLinus Torvalds break; 14551da177e4SLinus Torvalds } 14561da177e4SLinus Torvalds 14571da177e4SLinus Torvalds nbytes -= i; 14581da177e4SLinus Torvalds buf += i; 14591da177e4SLinus Torvalds ret += i; 14601da177e4SLinus Torvalds } 14611da177e4SLinus Torvalds 14621da177e4SLinus Torvalds /* Wipe data just returned from memory */ 1463d4c5efdbSDaniel Borkmann memzero_explicit(tmp, sizeof(tmp)); 14641da177e4SLinus Torvalds 14651da177e4SLinus Torvalds return ret; 14661da177e4SLinus Torvalds } 14671da177e4SLinus Torvalds 14681da177e4SLinus Torvalds /* 14691da177e4SLinus Torvalds * This function is the exported kernel interface. It returns some 1470c2557a30STheodore Ts'o * number of good random numbers, suitable for key generation, seeding 147118e9cea7SGreg Price * TCP sequence numbers, etc. It does not rely on the hardware random 147218e9cea7SGreg Price * number generator. For random bytes direct from the hardware RNG 147318e9cea7SGreg Price * (when available), use get_random_bytes_arch(). 14741da177e4SLinus Torvalds */ 14751da177e4SLinus Torvalds void get_random_bytes(void *buf, int nbytes) 14761da177e4SLinus Torvalds { 1477e192be9dSTheodore Ts'o __u8 tmp[CHACHA20_BLOCK_SIZE]; 1478e192be9dSTheodore Ts'o 1479392a546dSTheodore Ts'o #if DEBUG_RANDOM_BOOT > 0 1480e192be9dSTheodore Ts'o if (!crng_ready()) 1481392a546dSTheodore Ts'o printk(KERN_NOTICE "random: %pF get_random_bytes called " 1482e192be9dSTheodore Ts'o "with crng_init = %d\n", (void *) _RET_IP_, crng_init); 1483392a546dSTheodore Ts'o #endif 14845910895fSTheodore Ts'o trace_get_random_bytes(nbytes, _RET_IP_); 1485e192be9dSTheodore Ts'o 1486e192be9dSTheodore Ts'o while (nbytes >= CHACHA20_BLOCK_SIZE) { 1487e192be9dSTheodore Ts'o extract_crng(buf); 1488e192be9dSTheodore Ts'o buf += CHACHA20_BLOCK_SIZE; 1489e192be9dSTheodore Ts'o nbytes -= CHACHA20_BLOCK_SIZE; 1490e192be9dSTheodore Ts'o } 1491e192be9dSTheodore Ts'o 1492e192be9dSTheodore Ts'o if (nbytes > 0) { 1493e192be9dSTheodore Ts'o extract_crng(tmp); 1494e192be9dSTheodore Ts'o memcpy(buf, tmp, nbytes); 1495c92e040dSTheodore Ts'o crng_backtrack_protect(tmp, nbytes); 1496c92e040dSTheodore Ts'o } else 1497c92e040dSTheodore Ts'o crng_backtrack_protect(tmp, CHACHA20_BLOCK_SIZE); 1498c92e040dSTheodore Ts'o memzero_explicit(tmp, sizeof(tmp)); 1499c2557a30STheodore Ts'o } 1500c2557a30STheodore Ts'o EXPORT_SYMBOL(get_random_bytes); 1501c2557a30STheodore Ts'o 1502c2557a30STheodore Ts'o /* 1503205a525cSHerbert Xu * Add a callback function that will be invoked when the nonblocking 1504205a525cSHerbert Xu * pool is initialised. 1505205a525cSHerbert Xu * 1506205a525cSHerbert Xu * returns: 0 if callback is successfully added 1507205a525cSHerbert Xu * -EALREADY if pool is already initialised (callback not called) 1508205a525cSHerbert Xu * -ENOENT if module for callback is not alive 1509205a525cSHerbert Xu */ 1510205a525cSHerbert Xu int add_random_ready_callback(struct random_ready_callback *rdy) 1511205a525cSHerbert Xu { 1512205a525cSHerbert Xu struct module *owner; 1513205a525cSHerbert Xu unsigned long flags; 1514205a525cSHerbert Xu int err = -EALREADY; 1515205a525cSHerbert Xu 1516e192be9dSTheodore Ts'o if (crng_ready()) 1517205a525cSHerbert Xu return err; 1518205a525cSHerbert Xu 1519205a525cSHerbert Xu owner = rdy->owner; 1520205a525cSHerbert Xu if (!try_module_get(owner)) 1521205a525cSHerbert Xu return -ENOENT; 1522205a525cSHerbert Xu 1523205a525cSHerbert Xu spin_lock_irqsave(&random_ready_list_lock, flags); 1524e192be9dSTheodore Ts'o if (crng_ready()) 1525205a525cSHerbert Xu goto out; 1526205a525cSHerbert Xu 1527205a525cSHerbert Xu owner = NULL; 1528205a525cSHerbert Xu 1529205a525cSHerbert Xu list_add(&rdy->list, &random_ready_list); 1530205a525cSHerbert Xu err = 0; 1531205a525cSHerbert Xu 1532205a525cSHerbert Xu out: 1533205a525cSHerbert Xu spin_unlock_irqrestore(&random_ready_list_lock, flags); 1534205a525cSHerbert Xu 1535205a525cSHerbert Xu module_put(owner); 1536205a525cSHerbert Xu 1537205a525cSHerbert Xu return err; 1538205a525cSHerbert Xu } 1539205a525cSHerbert Xu EXPORT_SYMBOL(add_random_ready_callback); 1540205a525cSHerbert Xu 1541205a525cSHerbert Xu /* 1542205a525cSHerbert Xu * Delete a previously registered readiness callback function. 1543205a525cSHerbert Xu */ 1544205a525cSHerbert Xu void del_random_ready_callback(struct random_ready_callback *rdy) 1545205a525cSHerbert Xu { 1546205a525cSHerbert Xu unsigned long flags; 1547205a525cSHerbert Xu struct module *owner = NULL; 1548205a525cSHerbert Xu 1549205a525cSHerbert Xu spin_lock_irqsave(&random_ready_list_lock, flags); 1550205a525cSHerbert Xu if (!list_empty(&rdy->list)) { 1551205a525cSHerbert Xu list_del_init(&rdy->list); 1552205a525cSHerbert Xu owner = rdy->owner; 1553205a525cSHerbert Xu } 1554205a525cSHerbert Xu spin_unlock_irqrestore(&random_ready_list_lock, flags); 1555205a525cSHerbert Xu 1556205a525cSHerbert Xu module_put(owner); 1557205a525cSHerbert Xu } 1558205a525cSHerbert Xu EXPORT_SYMBOL(del_random_ready_callback); 1559205a525cSHerbert Xu 1560205a525cSHerbert Xu /* 1561c2557a30STheodore Ts'o * This function will use the architecture-specific hardware random 1562c2557a30STheodore Ts'o * number generator if it is available. The arch-specific hw RNG will 1563c2557a30STheodore Ts'o * almost certainly be faster than what we can do in software, but it 1564c2557a30STheodore Ts'o * is impossible to verify that it is implemented securely (as 1565c2557a30STheodore Ts'o * opposed, to, say, the AES encryption of a sequence number using a 1566c2557a30STheodore Ts'o * key known by the NSA). So it's useful if we need the speed, but 1567c2557a30STheodore Ts'o * only if we're willing to trust the hardware manufacturer not to 1568c2557a30STheodore Ts'o * have put in a back door. 1569c2557a30STheodore Ts'o */ 1570c2557a30STheodore Ts'o void get_random_bytes_arch(void *buf, int nbytes) 1571c2557a30STheodore Ts'o { 157263d77173SH. Peter Anvin char *p = buf; 157363d77173SH. Peter Anvin 15745910895fSTheodore Ts'o trace_get_random_bytes_arch(nbytes, _RET_IP_); 157563d77173SH. Peter Anvin while (nbytes) { 157663d77173SH. Peter Anvin unsigned long v; 157763d77173SH. Peter Anvin int chunk = min(nbytes, (int)sizeof(unsigned long)); 157863d77173SH. Peter Anvin 157963d77173SH. Peter Anvin if (!arch_get_random_long(&v)) 158063d77173SH. Peter Anvin break; 158163d77173SH. Peter Anvin 1582bd29e568SLuck, Tony memcpy(p, &v, chunk); 158363d77173SH. Peter Anvin p += chunk; 158463d77173SH. Peter Anvin nbytes -= chunk; 158563d77173SH. Peter Anvin } 158663d77173SH. Peter Anvin 1587c2557a30STheodore Ts'o if (nbytes) 1588e192be9dSTheodore Ts'o get_random_bytes(p, nbytes); 15891da177e4SLinus Torvalds } 1590c2557a30STheodore Ts'o EXPORT_SYMBOL(get_random_bytes_arch); 1591c2557a30STheodore Ts'o 15921da177e4SLinus Torvalds 15931da177e4SLinus Torvalds /* 15941da177e4SLinus Torvalds * init_std_data - initialize pool with system data 15951da177e4SLinus Torvalds * 15961da177e4SLinus Torvalds * @r: pool to initialize 15971da177e4SLinus Torvalds * 15981da177e4SLinus Torvalds * This function clears the pool's entropy count and mixes some system 15991da177e4SLinus Torvalds * data into the pool to prepare it for use. The pool is not cleared 16001da177e4SLinus Torvalds * as that can only decrease the entropy in the pool. 16011da177e4SLinus Torvalds */ 16021da177e4SLinus Torvalds static void init_std_data(struct entropy_store *r) 16031da177e4SLinus Torvalds { 16043e88bdffSTheodore Ts'o int i; 1605902c098aSTheodore Ts'o ktime_t now = ktime_get_real(); 1606902c098aSTheodore Ts'o unsigned long rv; 16071da177e4SLinus Torvalds 1608f5c2742cSTheodore Ts'o r->last_pulled = jiffies; 160985608f8eSTheodore Ts'o mix_pool_bytes(r, &now, sizeof(now)); 16109ed17b70SH. Peter Anvin for (i = r->poolinfo->poolbytes; i > 0; i -= sizeof(rv)) { 161183664a69SH. Peter Anvin if (!arch_get_random_seed_long(&rv) && 161283664a69SH. Peter Anvin !arch_get_random_long(&rv)) 1613ae9ecd92STheodore Ts'o rv = random_get_entropy(); 161485608f8eSTheodore Ts'o mix_pool_bytes(r, &rv, sizeof(rv)); 16153e88bdffSTheodore Ts'o } 161685608f8eSTheodore Ts'o mix_pool_bytes(r, utsname(), sizeof(*(utsname()))); 16171da177e4SLinus Torvalds } 16181da177e4SLinus Torvalds 1619cbc96b75STony Luck /* 1620cbc96b75STony Luck * Note that setup_arch() may call add_device_randomness() 1621cbc96b75STony Luck * long before we get here. This allows seeding of the pools 1622cbc96b75STony Luck * with some platform dependent data very early in the boot 1623cbc96b75STony Luck * process. But it limits our options here. We must use 1624cbc96b75STony Luck * statically allocated structures that already have all 1625cbc96b75STony Luck * initializations complete at compile time. We should also 1626cbc96b75STony Luck * take care not to overwrite the precious per platform data 1627cbc96b75STony Luck * we were given. 1628cbc96b75STony Luck */ 162953c3f63eSMatt Mackall static int rand_initialize(void) 16301da177e4SLinus Torvalds { 16311e7f583aSTheodore Ts'o #ifdef CONFIG_NUMA 16321e7f583aSTheodore Ts'o int i; 16331e7f583aSTheodore Ts'o struct crng_state *crng; 16341e7f583aSTheodore Ts'o struct crng_state **pool; 16351e7f583aSTheodore Ts'o #endif 16361e7f583aSTheodore Ts'o 16371da177e4SLinus Torvalds init_std_data(&input_pool); 16381da177e4SLinus Torvalds init_std_data(&blocking_pool); 1639e192be9dSTheodore Ts'o crng_initialize(&primary_crng); 16401e7f583aSTheodore Ts'o 16411e7f583aSTheodore Ts'o #ifdef CONFIG_NUMA 1642dd0f0cf5SMichael Ellerman pool = kcalloc(nr_node_ids, sizeof(*pool), GFP_KERNEL|__GFP_NOFAIL); 164359b8d4f1STheodore Ts'o for_each_online_node(i) { 16441e7f583aSTheodore Ts'o crng = kmalloc_node(sizeof(struct crng_state), 16451e7f583aSTheodore Ts'o GFP_KERNEL | __GFP_NOFAIL, i); 16461e7f583aSTheodore Ts'o spin_lock_init(&crng->lock); 16471e7f583aSTheodore Ts'o crng_initialize(crng); 16481e7f583aSTheodore Ts'o pool[i] = crng; 16491e7f583aSTheodore Ts'o } 16501e7f583aSTheodore Ts'o mb(); 16511e7f583aSTheodore Ts'o crng_node_pool = pool; 16521e7f583aSTheodore Ts'o #endif 16531da177e4SLinus Torvalds return 0; 16541da177e4SLinus Torvalds } 1655ae9ecd92STheodore Ts'o early_initcall(rand_initialize); 16561da177e4SLinus Torvalds 16579361401eSDavid Howells #ifdef CONFIG_BLOCK 16581da177e4SLinus Torvalds void rand_initialize_disk(struct gendisk *disk) 16591da177e4SLinus Torvalds { 16601da177e4SLinus Torvalds struct timer_rand_state *state; 16611da177e4SLinus Torvalds 16621da177e4SLinus Torvalds /* 1663f8595815SEric Dumazet * If kzalloc returns null, we just won't use that entropy 16641da177e4SLinus Torvalds * source. 16651da177e4SLinus Torvalds */ 1666f8595815SEric Dumazet state = kzalloc(sizeof(struct timer_rand_state), GFP_KERNEL); 1667644008dfSTheodore Ts'o if (state) { 1668644008dfSTheodore Ts'o state->last_time = INITIAL_JIFFIES; 16691da177e4SLinus Torvalds disk->random = state; 16701da177e4SLinus Torvalds } 1671644008dfSTheodore Ts'o } 16729361401eSDavid Howells #endif 16731da177e4SLinus Torvalds 16741da177e4SLinus Torvalds static ssize_t 1675c6e9d6f3STheodore Ts'o _random_read(int nonblock, char __user *buf, size_t nbytes) 16761da177e4SLinus Torvalds { 167712ff3a51SGreg Price ssize_t n; 16781da177e4SLinus Torvalds 16791da177e4SLinus Torvalds if (nbytes == 0) 16801da177e4SLinus Torvalds return 0; 16811da177e4SLinus Torvalds 168212ff3a51SGreg Price nbytes = min_t(size_t, nbytes, SEC_XFER_SIZE); 168312ff3a51SGreg Price while (1) { 168412ff3a51SGreg Price n = extract_entropy_user(&blocking_pool, buf, nbytes); 168512ff3a51SGreg Price if (n < 0) 168612ff3a51SGreg Price return n; 1687f80bbd8bSTheodore Ts'o trace_random_read(n*8, (nbytes-n)*8, 1688f80bbd8bSTheodore Ts'o ENTROPY_BITS(&blocking_pool), 1689f80bbd8bSTheodore Ts'o ENTROPY_BITS(&input_pool)); 169012ff3a51SGreg Price if (n > 0) 169112ff3a51SGreg Price return n; 1692331c6490SH. Peter Anvin 169312ff3a51SGreg Price /* Pool is (near) empty. Maybe wait and retry. */ 1694c6e9d6f3STheodore Ts'o if (nonblock) 169512ff3a51SGreg Price return -EAGAIN; 16961da177e4SLinus Torvalds 16971da177e4SLinus Torvalds wait_event_interruptible(random_read_wait, 1698a283b5c4SH. Peter Anvin ENTROPY_BITS(&input_pool) >= 16992132a96fSGreg Price random_read_wakeup_bits); 170012ff3a51SGreg Price if (signal_pending(current)) 170112ff3a51SGreg Price return -ERESTARTSYS; 17021da177e4SLinus Torvalds } 17031da177e4SLinus Torvalds } 17041da177e4SLinus Torvalds 17051da177e4SLinus Torvalds static ssize_t 1706c6e9d6f3STheodore Ts'o random_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos) 1707c6e9d6f3STheodore Ts'o { 1708c6e9d6f3STheodore Ts'o return _random_read(file->f_flags & O_NONBLOCK, buf, nbytes); 1709c6e9d6f3STheodore Ts'o } 1710c6e9d6f3STheodore Ts'o 1711c6e9d6f3STheodore Ts'o static ssize_t 171290b75ee5SMatt Mackall urandom_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos) 17131da177e4SLinus Torvalds { 1714e192be9dSTheodore Ts'o unsigned long flags; 17159b4d0087STheodore Ts'o static int maxwarn = 10; 1716301f0595STheodore Ts'o int ret; 1717301f0595STheodore Ts'o 1718e192be9dSTheodore Ts'o if (!crng_ready() && maxwarn > 0) { 17199b4d0087STheodore Ts'o maxwarn--; 17209b4d0087STheodore Ts'o printk(KERN_NOTICE "random: %s: uninitialized urandom read " 1721e192be9dSTheodore Ts'o "(%zd bytes read)\n", 1722e192be9dSTheodore Ts'o current->comm, nbytes); 1723e192be9dSTheodore Ts'o spin_lock_irqsave(&primary_crng.lock, flags); 1724e192be9dSTheodore Ts'o crng_init_cnt = 0; 1725e192be9dSTheodore Ts'o spin_unlock_irqrestore(&primary_crng.lock, flags); 17269b4d0087STheodore Ts'o } 172779a84687SHannes Frederic Sowa nbytes = min_t(size_t, nbytes, INT_MAX >> (ENTROPY_SHIFT + 3)); 1728e192be9dSTheodore Ts'o ret = extract_crng_user(buf, nbytes); 1729e192be9dSTheodore Ts'o trace_urandom_read(8 * nbytes, 0, ENTROPY_BITS(&input_pool)); 1730f80bbd8bSTheodore Ts'o return ret; 17311da177e4SLinus Torvalds } 17321da177e4SLinus Torvalds 17331da177e4SLinus Torvalds static unsigned int 17341da177e4SLinus Torvalds random_poll(struct file *file, poll_table * wait) 17351da177e4SLinus Torvalds { 17361da177e4SLinus Torvalds unsigned int mask; 17371da177e4SLinus Torvalds 17381da177e4SLinus Torvalds poll_wait(file, &random_read_wait, wait); 17391da177e4SLinus Torvalds poll_wait(file, &random_write_wait, wait); 17401da177e4SLinus Torvalds mask = 0; 17412132a96fSGreg Price if (ENTROPY_BITS(&input_pool) >= random_read_wakeup_bits) 17421da177e4SLinus Torvalds mask |= POLLIN | POLLRDNORM; 17432132a96fSGreg Price if (ENTROPY_BITS(&input_pool) < random_write_wakeup_bits) 17441da177e4SLinus Torvalds mask |= POLLOUT | POLLWRNORM; 17451da177e4SLinus Torvalds return mask; 17461da177e4SLinus Torvalds } 17471da177e4SLinus Torvalds 17487f397dcdSMatt Mackall static int 17497f397dcdSMatt Mackall write_pool(struct entropy_store *r, const char __user *buffer, size_t count) 17507f397dcdSMatt Mackall { 17517f397dcdSMatt Mackall size_t bytes; 17527f397dcdSMatt Mackall __u32 buf[16]; 17537f397dcdSMatt Mackall const char __user *p = buffer; 17547f397dcdSMatt Mackall 17557f397dcdSMatt Mackall while (count > 0) { 17567f397dcdSMatt Mackall bytes = min(count, sizeof(buf)); 17577f397dcdSMatt Mackall if (copy_from_user(&buf, p, bytes)) 17587f397dcdSMatt Mackall return -EFAULT; 17597f397dcdSMatt Mackall 17607f397dcdSMatt Mackall count -= bytes; 17617f397dcdSMatt Mackall p += bytes; 17627f397dcdSMatt Mackall 176385608f8eSTheodore Ts'o mix_pool_bytes(r, buf, bytes); 176491f3f1e3SMatt Mackall cond_resched(); 17657f397dcdSMatt Mackall } 17667f397dcdSMatt Mackall 17677f397dcdSMatt Mackall return 0; 17687f397dcdSMatt Mackall } 17697f397dcdSMatt Mackall 177090b75ee5SMatt Mackall static ssize_t random_write(struct file *file, const char __user *buffer, 17711da177e4SLinus Torvalds size_t count, loff_t *ppos) 17721da177e4SLinus Torvalds { 17737f397dcdSMatt Mackall size_t ret; 17747f397dcdSMatt Mackall 1775e192be9dSTheodore Ts'o ret = write_pool(&input_pool, buffer, count); 17767f397dcdSMatt Mackall if (ret) 17777f397dcdSMatt Mackall return ret; 17787f397dcdSMatt Mackall 17797f397dcdSMatt Mackall return (ssize_t)count; 17801da177e4SLinus Torvalds } 17811da177e4SLinus Torvalds 178243ae4860SMatt Mackall static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg) 17831da177e4SLinus Torvalds { 17841da177e4SLinus Torvalds int size, ent_count; 17851da177e4SLinus Torvalds int __user *p = (int __user *)arg; 17861da177e4SLinus Torvalds int retval; 17871da177e4SLinus Torvalds 17881da177e4SLinus Torvalds switch (cmd) { 17891da177e4SLinus Torvalds case RNDGETENTCNT: 179043ae4860SMatt Mackall /* inherently racy, no point locking */ 1791a283b5c4SH. Peter Anvin ent_count = ENTROPY_BITS(&input_pool); 1792a283b5c4SH. Peter Anvin if (put_user(ent_count, p)) 17931da177e4SLinus Torvalds return -EFAULT; 17941da177e4SLinus Torvalds return 0; 17951da177e4SLinus Torvalds case RNDADDTOENTCNT: 17961da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 17971da177e4SLinus Torvalds return -EPERM; 17981da177e4SLinus Torvalds if (get_user(ent_count, p)) 17991da177e4SLinus Torvalds return -EFAULT; 180086a574deSTheodore Ts'o return credit_entropy_bits_safe(&input_pool, ent_count); 18011da177e4SLinus Torvalds case RNDADDENTROPY: 18021da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 18031da177e4SLinus Torvalds return -EPERM; 18041da177e4SLinus Torvalds if (get_user(ent_count, p++)) 18051da177e4SLinus Torvalds return -EFAULT; 18061da177e4SLinus Torvalds if (ent_count < 0) 18071da177e4SLinus Torvalds return -EINVAL; 18081da177e4SLinus Torvalds if (get_user(size, p++)) 18091da177e4SLinus Torvalds return -EFAULT; 18107f397dcdSMatt Mackall retval = write_pool(&input_pool, (const char __user *)p, 18117f397dcdSMatt Mackall size); 18121da177e4SLinus Torvalds if (retval < 0) 18131da177e4SLinus Torvalds return retval; 181486a574deSTheodore Ts'o return credit_entropy_bits_safe(&input_pool, ent_count); 18151da177e4SLinus Torvalds case RNDZAPENTCNT: 18161da177e4SLinus Torvalds case RNDCLEARPOOL: 1817ae9ecd92STheodore Ts'o /* 1818ae9ecd92STheodore Ts'o * Clear the entropy pool counters. We no longer clear 1819ae9ecd92STheodore Ts'o * the entropy pool, as that's silly. 1820ae9ecd92STheodore Ts'o */ 18211da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 18221da177e4SLinus Torvalds return -EPERM; 1823ae9ecd92STheodore Ts'o input_pool.entropy_count = 0; 1824ae9ecd92STheodore Ts'o blocking_pool.entropy_count = 0; 18251da177e4SLinus Torvalds return 0; 18261da177e4SLinus Torvalds default: 18271da177e4SLinus Torvalds return -EINVAL; 18281da177e4SLinus Torvalds } 18291da177e4SLinus Torvalds } 18301da177e4SLinus Torvalds 18319a6f70bbSJeff Dike static int random_fasync(int fd, struct file *filp, int on) 18329a6f70bbSJeff Dike { 18339a6f70bbSJeff Dike return fasync_helper(fd, filp, on, &fasync); 18349a6f70bbSJeff Dike } 18359a6f70bbSJeff Dike 18362b8693c0SArjan van de Ven const struct file_operations random_fops = { 18371da177e4SLinus Torvalds .read = random_read, 18381da177e4SLinus Torvalds .write = random_write, 18391da177e4SLinus Torvalds .poll = random_poll, 184043ae4860SMatt Mackall .unlocked_ioctl = random_ioctl, 18419a6f70bbSJeff Dike .fasync = random_fasync, 18426038f373SArnd Bergmann .llseek = noop_llseek, 18431da177e4SLinus Torvalds }; 18441da177e4SLinus Torvalds 18452b8693c0SArjan van de Ven const struct file_operations urandom_fops = { 18461da177e4SLinus Torvalds .read = urandom_read, 18471da177e4SLinus Torvalds .write = random_write, 184843ae4860SMatt Mackall .unlocked_ioctl = random_ioctl, 18499a6f70bbSJeff Dike .fasync = random_fasync, 18506038f373SArnd Bergmann .llseek = noop_llseek, 18511da177e4SLinus Torvalds }; 18521da177e4SLinus Torvalds 1853c6e9d6f3STheodore Ts'o SYSCALL_DEFINE3(getrandom, char __user *, buf, size_t, count, 1854c6e9d6f3STheodore Ts'o unsigned int, flags) 1855c6e9d6f3STheodore Ts'o { 1856c6e9d6f3STheodore Ts'o if (flags & ~(GRND_NONBLOCK|GRND_RANDOM)) 1857c6e9d6f3STheodore Ts'o return -EINVAL; 1858c6e9d6f3STheodore Ts'o 1859c6e9d6f3STheodore Ts'o if (count > INT_MAX) 1860c6e9d6f3STheodore Ts'o count = INT_MAX; 1861c6e9d6f3STheodore Ts'o 1862c6e9d6f3STheodore Ts'o if (flags & GRND_RANDOM) 1863c6e9d6f3STheodore Ts'o return _random_read(flags & GRND_NONBLOCK, buf, count); 1864c6e9d6f3STheodore Ts'o 1865e192be9dSTheodore Ts'o if (!crng_ready()) { 1866c6e9d6f3STheodore Ts'o if (flags & GRND_NONBLOCK) 1867c6e9d6f3STheodore Ts'o return -EAGAIN; 1868e192be9dSTheodore Ts'o crng_wait_ready(); 1869c6e9d6f3STheodore Ts'o if (signal_pending(current)) 1870c6e9d6f3STheodore Ts'o return -ERESTARTSYS; 1871c6e9d6f3STheodore Ts'o } 1872c6e9d6f3STheodore Ts'o return urandom_read(NULL, buf, count, NULL); 1873c6e9d6f3STheodore Ts'o } 1874c6e9d6f3STheodore Ts'o 18751da177e4SLinus Torvalds /******************************************************************** 18761da177e4SLinus Torvalds * 18771da177e4SLinus Torvalds * Sysctl interface 18781da177e4SLinus Torvalds * 18791da177e4SLinus Torvalds ********************************************************************/ 18801da177e4SLinus Torvalds 18811da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 18821da177e4SLinus Torvalds 18831da177e4SLinus Torvalds #include <linux/sysctl.h> 18841da177e4SLinus Torvalds 18851da177e4SLinus Torvalds static int min_read_thresh = 8, min_write_thresh; 18868c2aa339SGreg Price static int max_read_thresh = OUTPUT_POOL_WORDS * 32; 18871da177e4SLinus Torvalds static int max_write_thresh = INPUT_POOL_WORDS * 32; 1888db61ffe3SFabio Estevam static int random_min_urandom_seed = 60; 18891da177e4SLinus Torvalds static char sysctl_bootid[16]; 18901da177e4SLinus Torvalds 18911da177e4SLinus Torvalds /* 1892f22052b2SGreg Price * This function is used to return both the bootid UUID, and random 18931da177e4SLinus Torvalds * UUID. The difference is in whether table->data is NULL; if it is, 18941da177e4SLinus Torvalds * then a new UUID is generated and returned to the user. 18951da177e4SLinus Torvalds * 1896f22052b2SGreg Price * If the user accesses this via the proc interface, the UUID will be 1897f22052b2SGreg Price * returned as an ASCII string in the standard UUID format; if via the 1898f22052b2SGreg Price * sysctl system call, as 16 bytes of binary data. 18991da177e4SLinus Torvalds */ 1900a151427eSJoe Perches static int proc_do_uuid(struct ctl_table *table, int write, 19011da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 19021da177e4SLinus Torvalds { 1903a151427eSJoe Perches struct ctl_table fake_table; 19041da177e4SLinus Torvalds unsigned char buf[64], tmp_uuid[16], *uuid; 19051da177e4SLinus Torvalds 19061da177e4SLinus Torvalds uuid = table->data; 19071da177e4SLinus Torvalds if (!uuid) { 19081da177e4SLinus Torvalds uuid = tmp_uuid; 19091da177e4SLinus Torvalds generate_random_uuid(uuid); 191044e4360fSMathieu Desnoyers } else { 191144e4360fSMathieu Desnoyers static DEFINE_SPINLOCK(bootid_spinlock); 191244e4360fSMathieu Desnoyers 191344e4360fSMathieu Desnoyers spin_lock(&bootid_spinlock); 191444e4360fSMathieu Desnoyers if (!uuid[8]) 191544e4360fSMathieu Desnoyers generate_random_uuid(uuid); 191644e4360fSMathieu Desnoyers spin_unlock(&bootid_spinlock); 191744e4360fSMathieu Desnoyers } 19181da177e4SLinus Torvalds 191935900771SJoe Perches sprintf(buf, "%pU", uuid); 192035900771SJoe Perches 19211da177e4SLinus Torvalds fake_table.data = buf; 19221da177e4SLinus Torvalds fake_table.maxlen = sizeof(buf); 19231da177e4SLinus Torvalds 19248d65af78SAlexey Dobriyan return proc_dostring(&fake_table, write, buffer, lenp, ppos); 19251da177e4SLinus Torvalds } 19261da177e4SLinus Torvalds 1927a283b5c4SH. Peter Anvin /* 1928a283b5c4SH. Peter Anvin * Return entropy available scaled to integral bits 1929a283b5c4SH. Peter Anvin */ 19305eb10d91SJoe Perches static int proc_do_entropy(struct ctl_table *table, int write, 1931a283b5c4SH. Peter Anvin void __user *buffer, size_t *lenp, loff_t *ppos) 1932a283b5c4SH. Peter Anvin { 19335eb10d91SJoe Perches struct ctl_table fake_table; 1934a283b5c4SH. Peter Anvin int entropy_count; 1935a283b5c4SH. Peter Anvin 1936a283b5c4SH. Peter Anvin entropy_count = *(int *)table->data >> ENTROPY_SHIFT; 1937a283b5c4SH. Peter Anvin 1938a283b5c4SH. Peter Anvin fake_table.data = &entropy_count; 1939a283b5c4SH. Peter Anvin fake_table.maxlen = sizeof(entropy_count); 1940a283b5c4SH. Peter Anvin 1941a283b5c4SH. Peter Anvin return proc_dointvec(&fake_table, write, buffer, lenp, ppos); 1942a283b5c4SH. Peter Anvin } 1943a283b5c4SH. Peter Anvin 19441da177e4SLinus Torvalds static int sysctl_poolsize = INPUT_POOL_WORDS * 32; 1945a151427eSJoe Perches extern struct ctl_table random_table[]; 1946a151427eSJoe Perches struct ctl_table random_table[] = { 19471da177e4SLinus Torvalds { 19481da177e4SLinus Torvalds .procname = "poolsize", 19491da177e4SLinus Torvalds .data = &sysctl_poolsize, 19501da177e4SLinus Torvalds .maxlen = sizeof(int), 19511da177e4SLinus Torvalds .mode = 0444, 19526d456111SEric W. Biederman .proc_handler = proc_dointvec, 19531da177e4SLinus Torvalds }, 19541da177e4SLinus Torvalds { 19551da177e4SLinus Torvalds .procname = "entropy_avail", 19561da177e4SLinus Torvalds .maxlen = sizeof(int), 19571da177e4SLinus Torvalds .mode = 0444, 1958a283b5c4SH. Peter Anvin .proc_handler = proc_do_entropy, 19591da177e4SLinus Torvalds .data = &input_pool.entropy_count, 19601da177e4SLinus Torvalds }, 19611da177e4SLinus Torvalds { 19621da177e4SLinus Torvalds .procname = "read_wakeup_threshold", 19632132a96fSGreg Price .data = &random_read_wakeup_bits, 19641da177e4SLinus Torvalds .maxlen = sizeof(int), 19651da177e4SLinus Torvalds .mode = 0644, 19666d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 19671da177e4SLinus Torvalds .extra1 = &min_read_thresh, 19681da177e4SLinus Torvalds .extra2 = &max_read_thresh, 19691da177e4SLinus Torvalds }, 19701da177e4SLinus Torvalds { 19711da177e4SLinus Torvalds .procname = "write_wakeup_threshold", 19722132a96fSGreg Price .data = &random_write_wakeup_bits, 19731da177e4SLinus Torvalds .maxlen = sizeof(int), 19741da177e4SLinus Torvalds .mode = 0644, 19756d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 19761da177e4SLinus Torvalds .extra1 = &min_write_thresh, 19771da177e4SLinus Torvalds .extra2 = &max_write_thresh, 19781da177e4SLinus Torvalds }, 19791da177e4SLinus Torvalds { 1980f5c2742cSTheodore Ts'o .procname = "urandom_min_reseed_secs", 1981f5c2742cSTheodore Ts'o .data = &random_min_urandom_seed, 1982f5c2742cSTheodore Ts'o .maxlen = sizeof(int), 1983f5c2742cSTheodore Ts'o .mode = 0644, 1984f5c2742cSTheodore Ts'o .proc_handler = proc_dointvec, 1985f5c2742cSTheodore Ts'o }, 1986f5c2742cSTheodore Ts'o { 19871da177e4SLinus Torvalds .procname = "boot_id", 19881da177e4SLinus Torvalds .data = &sysctl_bootid, 19891da177e4SLinus Torvalds .maxlen = 16, 19901da177e4SLinus Torvalds .mode = 0444, 19916d456111SEric W. Biederman .proc_handler = proc_do_uuid, 19921da177e4SLinus Torvalds }, 19931da177e4SLinus Torvalds { 19941da177e4SLinus Torvalds .procname = "uuid", 19951da177e4SLinus Torvalds .maxlen = 16, 19961da177e4SLinus Torvalds .mode = 0444, 19976d456111SEric W. Biederman .proc_handler = proc_do_uuid, 19981da177e4SLinus Torvalds }, 199943759d4fSTheodore Ts'o #ifdef ADD_INTERRUPT_BENCH 200043759d4fSTheodore Ts'o { 200143759d4fSTheodore Ts'o .procname = "add_interrupt_avg_cycles", 200243759d4fSTheodore Ts'o .data = &avg_cycles, 200343759d4fSTheodore Ts'o .maxlen = sizeof(avg_cycles), 200443759d4fSTheodore Ts'o .mode = 0444, 200543759d4fSTheodore Ts'o .proc_handler = proc_doulongvec_minmax, 200643759d4fSTheodore Ts'o }, 200743759d4fSTheodore Ts'o { 200843759d4fSTheodore Ts'o .procname = "add_interrupt_avg_deviation", 200943759d4fSTheodore Ts'o .data = &avg_deviation, 201043759d4fSTheodore Ts'o .maxlen = sizeof(avg_deviation), 201143759d4fSTheodore Ts'o .mode = 0444, 201243759d4fSTheodore Ts'o .proc_handler = proc_doulongvec_minmax, 201343759d4fSTheodore Ts'o }, 201443759d4fSTheodore Ts'o #endif 2015894d2491SEric W. Biederman { } 20161da177e4SLinus Torvalds }; 20171da177e4SLinus Torvalds #endif /* CONFIG_SYSCTL */ 20181da177e4SLinus Torvalds 2019f5b98461SJason A. Donenfeld struct batched_entropy { 2020f5b98461SJason A. Donenfeld union { 2021c440408cSJason A. Donenfeld u64 entropy_u64[CHACHA20_BLOCK_SIZE / sizeof(u64)]; 2022c440408cSJason A. Donenfeld u32 entropy_u32[CHACHA20_BLOCK_SIZE / sizeof(u32)]; 2023f5b98461SJason A. Donenfeld }; 2024f5b98461SJason A. Donenfeld unsigned int position; 2025f5b98461SJason A. Donenfeld }; 2026b1132deaSEric Biggers 20271da177e4SLinus Torvalds /* 2028f5b98461SJason A. Donenfeld * Get a random word for internal kernel use only. The quality of the random 2029f5b98461SJason A. Donenfeld * number is either as good as RDRAND or as good as /dev/urandom, with the 2030f5b98461SJason A. Donenfeld * goal of being quite fast and not depleting entropy. 20311da177e4SLinus Torvalds */ 2032c440408cSJason A. Donenfeld static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_u64); 2033c440408cSJason A. Donenfeld u64 get_random_u64(void) 2034ec9ee4acSDaniel Cashman { 2035c440408cSJason A. Donenfeld u64 ret; 2036f5b98461SJason A. Donenfeld struct batched_entropy *batch; 2037ec9ee4acSDaniel Cashman 2038c440408cSJason A. Donenfeld #if BITS_PER_LONG == 64 2039c440408cSJason A. Donenfeld if (arch_get_random_long((unsigned long *)&ret)) 2040ec9ee4acSDaniel Cashman return ret; 2041c440408cSJason A. Donenfeld #else 2042c440408cSJason A. Donenfeld if (arch_get_random_long((unsigned long *)&ret) && 2043c440408cSJason A. Donenfeld arch_get_random_long((unsigned long *)&ret + 1)) 2044c440408cSJason A. Donenfeld return ret; 2045c440408cSJason A. Donenfeld #endif 2046ec9ee4acSDaniel Cashman 2047c440408cSJason A. Donenfeld batch = &get_cpu_var(batched_entropy_u64); 2048c440408cSJason A. Donenfeld if (batch->position % ARRAY_SIZE(batch->entropy_u64) == 0) { 2049c440408cSJason A. Donenfeld extract_crng((u8 *)batch->entropy_u64); 2050f5b98461SJason A. Donenfeld batch->position = 0; 2051f5b98461SJason A. Donenfeld } 2052c440408cSJason A. Donenfeld ret = batch->entropy_u64[batch->position++]; 2053c440408cSJason A. Donenfeld put_cpu_var(batched_entropy_u64); 2054ec9ee4acSDaniel Cashman return ret; 2055ec9ee4acSDaniel Cashman } 2056c440408cSJason A. Donenfeld EXPORT_SYMBOL(get_random_u64); 2057ec9ee4acSDaniel Cashman 2058c440408cSJason A. Donenfeld static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_u32); 2059c440408cSJason A. Donenfeld u32 get_random_u32(void) 2060f5b98461SJason A. Donenfeld { 2061c440408cSJason A. Donenfeld u32 ret; 2062f5b98461SJason A. Donenfeld struct batched_entropy *batch; 2063f5b98461SJason A. Donenfeld 2064f5b98461SJason A. Donenfeld if (arch_get_random_int(&ret)) 2065f5b98461SJason A. Donenfeld return ret; 2066f5b98461SJason A. Donenfeld 2067c440408cSJason A. Donenfeld batch = &get_cpu_var(batched_entropy_u32); 2068c440408cSJason A. Donenfeld if (batch->position % ARRAY_SIZE(batch->entropy_u32) == 0) { 2069c440408cSJason A. Donenfeld extract_crng((u8 *)batch->entropy_u32); 2070f5b98461SJason A. Donenfeld batch->position = 0; 2071f5b98461SJason A. Donenfeld } 2072c440408cSJason A. Donenfeld ret = batch->entropy_u32[batch->position++]; 2073c440408cSJason A. Donenfeld put_cpu_var(batched_entropy_u32); 2074f5b98461SJason A. Donenfeld return ret; 2075f5b98461SJason A. Donenfeld } 2076c440408cSJason A. Donenfeld EXPORT_SYMBOL(get_random_u32); 2077f5b98461SJason A. Donenfeld 207899fdafdeSJason Cooper /** 207999fdafdeSJason Cooper * randomize_page - Generate a random, page aligned address 208099fdafdeSJason Cooper * @start: The smallest acceptable address the caller will take. 208199fdafdeSJason Cooper * @range: The size of the area, starting at @start, within which the 208299fdafdeSJason Cooper * random address must fall. 208399fdafdeSJason Cooper * 208499fdafdeSJason Cooper * If @start + @range would overflow, @range is capped. 208599fdafdeSJason Cooper * 208699fdafdeSJason Cooper * NOTE: Historical use of randomize_range, which this replaces, presumed that 208799fdafdeSJason Cooper * @start was already page aligned. We now align it regardless. 208899fdafdeSJason Cooper * 208999fdafdeSJason Cooper * Return: A page aligned address within [start, start + range). On error, 209099fdafdeSJason Cooper * @start is returned. 209199fdafdeSJason Cooper */ 209299fdafdeSJason Cooper unsigned long 209399fdafdeSJason Cooper randomize_page(unsigned long start, unsigned long range) 209499fdafdeSJason Cooper { 209599fdafdeSJason Cooper if (!PAGE_ALIGNED(start)) { 209699fdafdeSJason Cooper range -= PAGE_ALIGN(start) - start; 209799fdafdeSJason Cooper start = PAGE_ALIGN(start); 209899fdafdeSJason Cooper } 209999fdafdeSJason Cooper 210099fdafdeSJason Cooper if (start > ULONG_MAX - range) 210199fdafdeSJason Cooper range = ULONG_MAX - start; 210299fdafdeSJason Cooper 210399fdafdeSJason Cooper range >>= PAGE_SHIFT; 210499fdafdeSJason Cooper 210599fdafdeSJason Cooper if (range == 0) 210699fdafdeSJason Cooper return start; 210799fdafdeSJason Cooper 210899fdafdeSJason Cooper return start + (get_random_long() % range << PAGE_SHIFT); 210999fdafdeSJason Cooper } 211099fdafdeSJason Cooper 2111c84dbf61STorsten Duwe /* Interface for in-kernel drivers of true hardware RNGs. 2112c84dbf61STorsten Duwe * Those devices may produce endless random bits and will be throttled 2113c84dbf61STorsten Duwe * when our pool is full. 2114c84dbf61STorsten Duwe */ 2115c84dbf61STorsten Duwe void add_hwgenerator_randomness(const char *buffer, size_t count, 2116c84dbf61STorsten Duwe size_t entropy) 2117c84dbf61STorsten Duwe { 2118c84dbf61STorsten Duwe struct entropy_store *poolp = &input_pool; 2119c84dbf61STorsten Duwe 2120e192be9dSTheodore Ts'o if (!crng_ready()) { 2121e192be9dSTheodore Ts'o crng_fast_load(buffer, count); 2122e192be9dSTheodore Ts'o return; 21233371f3daSTheodore Ts'o } 2124e192be9dSTheodore Ts'o 2125e192be9dSTheodore Ts'o /* Suspend writing if we're above the trickle threshold. 2126e192be9dSTheodore Ts'o * We'll be woken up again once below random_write_wakeup_thresh, 2127e192be9dSTheodore Ts'o * or when the calling thread is about to terminate. 2128e192be9dSTheodore Ts'o */ 2129e192be9dSTheodore Ts'o wait_event_interruptible(random_write_wait, kthread_should_stop() || 2130e192be9dSTheodore Ts'o ENTROPY_BITS(&input_pool) <= random_write_wakeup_bits); 2131c84dbf61STorsten Duwe mix_pool_bytes(poolp, buffer, count); 2132c84dbf61STorsten Duwe credit_entropy_bits(poolp, entropy); 2133c84dbf61STorsten Duwe } 2134c84dbf61STorsten Duwe EXPORT_SYMBOL_GPL(add_hwgenerator_randomness); 2135