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 * 1281da177e4SLinus Torvalds * void add_input_randomness(unsigned int type, unsigned int code, 1291da177e4SLinus Torvalds * unsigned int value); 1301da177e4SLinus Torvalds * void add_interrupt_randomness(int irq); 131442a4fffSJarod Wilson * void add_disk_randomness(struct gendisk *disk); 1321da177e4SLinus Torvalds * 1331da177e4SLinus Torvalds * add_input_randomness() uses the input layer interrupt timing, as well as 1341da177e4SLinus Torvalds * the event type information from the hardware. 1351da177e4SLinus Torvalds * 1361da177e4SLinus Torvalds * add_interrupt_randomness() uses the inter-interrupt timing as random 1371da177e4SLinus Torvalds * inputs to the entropy pool. Note that not all interrupts are good 1381da177e4SLinus Torvalds * sources of randomness! For example, the timer interrupts is not a 1391da177e4SLinus Torvalds * good choice, because the periodicity of the interrupts is too 140442a4fffSJarod Wilson * regular, and hence predictable to an attacker. Network Interface 141442a4fffSJarod Wilson * Controller interrupts are a better measure, since the timing of the 142442a4fffSJarod Wilson * NIC interrupts are more unpredictable. 143442a4fffSJarod Wilson * 144442a4fffSJarod Wilson * add_disk_randomness() uses what amounts to the seek time of block 145442a4fffSJarod Wilson * layer request events, on a per-disk_devt basis, as input to the 146442a4fffSJarod Wilson * entropy pool. Note that high-speed solid state drives with very low 147442a4fffSJarod Wilson * seek times do not make for good sources of entropy, as their seek 148442a4fffSJarod Wilson * times are usually fairly consistent. 1491da177e4SLinus Torvalds * 1501da177e4SLinus Torvalds * All of these routines try to estimate how many bits of randomness a 1511da177e4SLinus Torvalds * particular randomness source. They do this by keeping track of the 1521da177e4SLinus Torvalds * first and second order deltas of the event timings. 1531da177e4SLinus Torvalds * 1541da177e4SLinus Torvalds * Ensuring unpredictability at system startup 1551da177e4SLinus Torvalds * ============================================ 1561da177e4SLinus Torvalds * 1571da177e4SLinus Torvalds * When any operating system starts up, it will go through a sequence 1581da177e4SLinus Torvalds * of actions that are fairly predictable by an adversary, especially 1591da177e4SLinus Torvalds * if the start-up does not involve interaction with a human operator. 1601da177e4SLinus Torvalds * This reduces the actual number of bits of unpredictability in the 1611da177e4SLinus Torvalds * entropy pool below the value in entropy_count. In order to 1621da177e4SLinus Torvalds * counteract this effect, it helps to carry information in the 1631da177e4SLinus Torvalds * entropy pool across shut-downs and start-ups. To do this, put the 1641da177e4SLinus Torvalds * following lines an appropriate script which is run during the boot 1651da177e4SLinus Torvalds * sequence: 1661da177e4SLinus Torvalds * 1671da177e4SLinus Torvalds * echo "Initializing random number generator..." 1681da177e4SLinus Torvalds * random_seed=/var/run/random-seed 1691da177e4SLinus Torvalds * # Carry a random seed from start-up to start-up 1701da177e4SLinus Torvalds * # Load and then save the whole entropy pool 1711da177e4SLinus Torvalds * if [ -f $random_seed ]; then 1721da177e4SLinus Torvalds * cat $random_seed >/dev/urandom 1731da177e4SLinus Torvalds * else 1741da177e4SLinus Torvalds * touch $random_seed 1751da177e4SLinus Torvalds * fi 1761da177e4SLinus Torvalds * chmod 600 $random_seed 1771da177e4SLinus Torvalds * dd if=/dev/urandom of=$random_seed count=1 bs=512 1781da177e4SLinus Torvalds * 1791da177e4SLinus Torvalds * and the following lines in an appropriate script which is run as 1801da177e4SLinus Torvalds * the system is shutdown: 1811da177e4SLinus Torvalds * 1821da177e4SLinus Torvalds * # Carry a random seed from shut-down to start-up 1831da177e4SLinus Torvalds * # Save the whole entropy pool 1841da177e4SLinus Torvalds * echo "Saving random seed..." 1851da177e4SLinus Torvalds * random_seed=/var/run/random-seed 1861da177e4SLinus Torvalds * touch $random_seed 1871da177e4SLinus Torvalds * chmod 600 $random_seed 1881da177e4SLinus Torvalds * dd if=/dev/urandom of=$random_seed count=1 bs=512 1891da177e4SLinus Torvalds * 1901da177e4SLinus Torvalds * For example, on most modern systems using the System V init 1911da177e4SLinus Torvalds * scripts, such code fragments would be found in 1921da177e4SLinus Torvalds * /etc/rc.d/init.d/random. On older Linux systems, the correct script 1931da177e4SLinus Torvalds * location might be in /etc/rcb.d/rc.local or /etc/rc.d/rc.0. 1941da177e4SLinus Torvalds * 1951da177e4SLinus Torvalds * Effectively, these commands cause the contents of the entropy pool 1961da177e4SLinus Torvalds * to be saved at shut-down time and reloaded into the entropy pool at 1971da177e4SLinus Torvalds * start-up. (The 'dd' in the addition to the bootup script is to 1981da177e4SLinus Torvalds * make sure that /etc/random-seed is different for every start-up, 1991da177e4SLinus Torvalds * even if the system crashes without executing rc.0.) Even with 2001da177e4SLinus Torvalds * complete knowledge of the start-up activities, predicting the state 2011da177e4SLinus Torvalds * of the entropy pool requires knowledge of the previous history of 2021da177e4SLinus Torvalds * the system. 2031da177e4SLinus Torvalds * 2041da177e4SLinus Torvalds * Configuring the /dev/random driver under Linux 2051da177e4SLinus Torvalds * ============================================== 2061da177e4SLinus Torvalds * 2071da177e4SLinus Torvalds * The /dev/random driver under Linux uses minor numbers 8 and 9 of 2081da177e4SLinus Torvalds * the /dev/mem major number (#1). So if your system does not have 2091da177e4SLinus Torvalds * /dev/random and /dev/urandom created already, they can be created 2101da177e4SLinus Torvalds * by using the commands: 2111da177e4SLinus Torvalds * 2121da177e4SLinus Torvalds * mknod /dev/random c 1 8 2131da177e4SLinus Torvalds * mknod /dev/urandom c 1 9 2141da177e4SLinus Torvalds * 2151da177e4SLinus Torvalds * Acknowledgements: 2161da177e4SLinus Torvalds * ================= 2171da177e4SLinus Torvalds * 2181da177e4SLinus Torvalds * Ideas for constructing this random number generator were derived 2191da177e4SLinus Torvalds * from Pretty Good Privacy's random number generator, and from private 2201da177e4SLinus Torvalds * discussions with Phil Karn. Colin Plumb provided a faster random 2211da177e4SLinus Torvalds * number generator, which speed up the mixing function of the entropy 2221da177e4SLinus Torvalds * pool, taken from PGPfone. Dale Worley has also contributed many 2231da177e4SLinus Torvalds * useful ideas and suggestions to improve this driver. 2241da177e4SLinus Torvalds * 2251da177e4SLinus Torvalds * Any flaws in the design are solely my responsibility, and should 2261da177e4SLinus Torvalds * not be attributed to the Phil, Colin, or any of authors of PGP. 2271da177e4SLinus Torvalds * 2281da177e4SLinus Torvalds * Further background information on this topic may be obtained from 2291da177e4SLinus Torvalds * RFC 1750, "Randomness Recommendations for Security", by Donald 2301da177e4SLinus Torvalds * Eastlake, Steve Crocker, and Jeff Schiller. 2311da177e4SLinus Torvalds */ 2321da177e4SLinus Torvalds 2331da177e4SLinus Torvalds #include <linux/utsname.h> 2341da177e4SLinus Torvalds #include <linux/module.h> 2351da177e4SLinus Torvalds #include <linux/kernel.h> 2361da177e4SLinus Torvalds #include <linux/major.h> 2371da177e4SLinus Torvalds #include <linux/string.h> 2381da177e4SLinus Torvalds #include <linux/fcntl.h> 2391da177e4SLinus Torvalds #include <linux/slab.h> 2401da177e4SLinus Torvalds #include <linux/random.h> 2411da177e4SLinus Torvalds #include <linux/poll.h> 2421da177e4SLinus Torvalds #include <linux/init.h> 2431da177e4SLinus Torvalds #include <linux/fs.h> 2441da177e4SLinus Torvalds #include <linux/genhd.h> 2451da177e4SLinus Torvalds #include <linux/interrupt.h> 24627ac792cSAndrea Righi #include <linux/mm.h> 2471da177e4SLinus Torvalds #include <linux/spinlock.h> 2481da177e4SLinus Torvalds #include <linux/percpu.h> 2491da177e4SLinus Torvalds #include <linux/cryptohash.h> 2505b739ef8SNeil Horman #include <linux/fips.h> 2511da177e4SLinus Torvalds 252d178a1ebSYinghai Lu #ifdef CONFIG_GENERIC_HARDIRQS 253d178a1ebSYinghai Lu # include <linux/irq.h> 254d178a1ebSYinghai Lu #endif 255d178a1ebSYinghai Lu 2561da177e4SLinus Torvalds #include <asm/processor.h> 2571da177e4SLinus Torvalds #include <asm/uaccess.h> 2581da177e4SLinus Torvalds #include <asm/irq.h> 2591da177e4SLinus Torvalds #include <asm/io.h> 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds /* 2621da177e4SLinus Torvalds * Configuration information 2631da177e4SLinus Torvalds */ 2641da177e4SLinus Torvalds #define INPUT_POOL_WORDS 128 2651da177e4SLinus Torvalds #define OUTPUT_POOL_WORDS 32 2661da177e4SLinus Torvalds #define SEC_XFER_SIZE 512 267e954bc91SMatt Mackall #define EXTRACT_SIZE 10 2681da177e4SLinus Torvalds 2691da177e4SLinus Torvalds /* 2701da177e4SLinus Torvalds * The minimum number of bits of entropy before we wake up a read on 2711da177e4SLinus Torvalds * /dev/random. Should be enough to do a significant reseed. 2721da177e4SLinus Torvalds */ 2731da177e4SLinus Torvalds static int random_read_wakeup_thresh = 64; 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds /* 2761da177e4SLinus Torvalds * If the entropy count falls under this number of bits, then we 2771da177e4SLinus Torvalds * should wake up processes which are selecting or polling on write 2781da177e4SLinus Torvalds * access to /dev/random. 2791da177e4SLinus Torvalds */ 2801da177e4SLinus Torvalds static int random_write_wakeup_thresh = 128; 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds /* 2831da177e4SLinus Torvalds * When the input pool goes over trickle_thresh, start dropping most 2841da177e4SLinus Torvalds * samples to avoid wasting CPU time and reduce lock contention. 2851da177e4SLinus Torvalds */ 2861da177e4SLinus Torvalds 2876c036527SChristoph Lameter static int trickle_thresh __read_mostly = INPUT_POOL_WORDS * 28; 2881da177e4SLinus Torvalds 28990b75ee5SMatt Mackall static DEFINE_PER_CPU(int, trickle_count); 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds /* 2921da177e4SLinus Torvalds * A pool of size .poolwords is stirred with a primitive polynomial 2931da177e4SLinus Torvalds * of degree .poolwords over GF(2). The taps for various sizes are 2941da177e4SLinus Torvalds * defined below. They are chosen to be evenly spaced (minimum RMS 2951da177e4SLinus Torvalds * distance from evenly spaced; the numbers in the comments are a 2961da177e4SLinus Torvalds * scaled squared error sum) except for the last tap, which is 1 to 2971da177e4SLinus Torvalds * get the twisting happening as fast as possible. 2981da177e4SLinus Torvalds */ 2991da177e4SLinus Torvalds static struct poolinfo { 3001da177e4SLinus Torvalds int poolwords; 3011da177e4SLinus Torvalds int tap1, tap2, tap3, tap4, tap5; 3021da177e4SLinus Torvalds } poolinfo_table[] = { 3031da177e4SLinus Torvalds /* x^128 + x^103 + x^76 + x^51 +x^25 + x + 1 -- 105 */ 3041da177e4SLinus Torvalds { 128, 103, 76, 51, 25, 1 }, 3051da177e4SLinus Torvalds /* x^32 + x^26 + x^20 + x^14 + x^7 + x + 1 -- 15 */ 3061da177e4SLinus Torvalds { 32, 26, 20, 14, 7, 1 }, 3071da177e4SLinus Torvalds #if 0 3081da177e4SLinus Torvalds /* x^2048 + x^1638 + x^1231 + x^819 + x^411 + x + 1 -- 115 */ 3091da177e4SLinus Torvalds { 2048, 1638, 1231, 819, 411, 1 }, 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds /* x^1024 + x^817 + x^615 + x^412 + x^204 + x + 1 -- 290 */ 3121da177e4SLinus Torvalds { 1024, 817, 615, 412, 204, 1 }, 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds /* x^1024 + x^819 + x^616 + x^410 + x^207 + x^2 + 1 -- 115 */ 3151da177e4SLinus Torvalds { 1024, 819, 616, 410, 207, 2 }, 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds /* x^512 + x^411 + x^308 + x^208 + x^104 + x + 1 -- 225 */ 3181da177e4SLinus Torvalds { 512, 411, 308, 208, 104, 1 }, 3191da177e4SLinus Torvalds 3201da177e4SLinus Torvalds /* x^512 + x^409 + x^307 + x^206 + x^102 + x^2 + 1 -- 95 */ 3211da177e4SLinus Torvalds { 512, 409, 307, 206, 102, 2 }, 3221da177e4SLinus Torvalds /* x^512 + x^409 + x^309 + x^205 + x^103 + x^2 + 1 -- 95 */ 3231da177e4SLinus Torvalds { 512, 409, 309, 205, 103, 2 }, 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds /* x^256 + x^205 + x^155 + x^101 + x^52 + x + 1 -- 125 */ 3261da177e4SLinus Torvalds { 256, 205, 155, 101, 52, 1 }, 3271da177e4SLinus Torvalds 3281da177e4SLinus Torvalds /* x^128 + x^103 + x^78 + x^51 + x^27 + x^2 + 1 -- 70 */ 3291da177e4SLinus Torvalds { 128, 103, 78, 51, 27, 2 }, 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds /* x^64 + x^52 + x^39 + x^26 + x^14 + x + 1 -- 15 */ 3321da177e4SLinus Torvalds { 64, 52, 39, 26, 14, 1 }, 3331da177e4SLinus Torvalds #endif 3341da177e4SLinus Torvalds }; 3351da177e4SLinus Torvalds 3361da177e4SLinus Torvalds #define POOLBITS poolwords*32 3371da177e4SLinus Torvalds #define POOLBYTES poolwords*4 3381da177e4SLinus Torvalds 3391da177e4SLinus Torvalds /* 3401da177e4SLinus Torvalds * For the purposes of better mixing, we use the CRC-32 polynomial as 3411da177e4SLinus Torvalds * well to make a twisted Generalized Feedback Shift Reigster 3421da177e4SLinus Torvalds * 3431da177e4SLinus Torvalds * (See M. Matsumoto & Y. Kurita, 1992. Twisted GFSR generators. ACM 3441da177e4SLinus Torvalds * Transactions on Modeling and Computer Simulation 2(3):179-194. 3451da177e4SLinus Torvalds * Also see M. Matsumoto & Y. Kurita, 1994. Twisted GFSR generators 3461da177e4SLinus Torvalds * II. ACM Transactions on Mdeling and Computer Simulation 4:254-266) 3471da177e4SLinus Torvalds * 3481da177e4SLinus Torvalds * Thanks to Colin Plumb for suggesting this. 3491da177e4SLinus Torvalds * 3501da177e4SLinus Torvalds * We have not analyzed the resultant polynomial to prove it primitive; 3511da177e4SLinus Torvalds * in fact it almost certainly isn't. Nonetheless, the irreducible factors 3521da177e4SLinus Torvalds * of a random large-degree polynomial over GF(2) are more than large enough 3531da177e4SLinus Torvalds * that periodicity is not a concern. 3541da177e4SLinus Torvalds * 3551da177e4SLinus Torvalds * The input hash is much less sensitive than the output hash. All 3561da177e4SLinus Torvalds * that we want of it is that it be a good non-cryptographic hash; 3571da177e4SLinus Torvalds * i.e. it not produce collisions when fed "random" data of the sort 3581da177e4SLinus Torvalds * we expect to see. As long as the pool state differs for different 3591da177e4SLinus Torvalds * inputs, we have preserved the input entropy and done a good job. 3601da177e4SLinus Torvalds * The fact that an intelligent attacker can construct inputs that 3611da177e4SLinus Torvalds * will produce controlled alterations to the pool's state is not 3621da177e4SLinus Torvalds * important because we don't consider such inputs to contribute any 3631da177e4SLinus Torvalds * randomness. The only property we need with respect to them is that 3641da177e4SLinus Torvalds * the attacker can't increase his/her knowledge of the pool's state. 3651da177e4SLinus Torvalds * Since all additions are reversible (knowing the final state and the 3661da177e4SLinus Torvalds * input, you can reconstruct the initial state), if an attacker has 3671da177e4SLinus Torvalds * any uncertainty about the initial state, he/she can only shuffle 3681da177e4SLinus Torvalds * that uncertainty about, but never cause any collisions (which would 3691da177e4SLinus Torvalds * decrease the uncertainty). 3701da177e4SLinus Torvalds * 3711da177e4SLinus Torvalds * The chosen system lets the state of the pool be (essentially) the input 3721da177e4SLinus Torvalds * modulo the generator polymnomial. Now, for random primitive polynomials, 3731da177e4SLinus Torvalds * this is a universal class of hash functions, meaning that the chance 3741da177e4SLinus Torvalds * of a collision is limited by the attacker's knowledge of the generator 3751da177e4SLinus Torvalds * polynomail, so if it is chosen at random, an attacker can never force 3761da177e4SLinus Torvalds * a collision. Here, we use a fixed polynomial, but we *can* assume that 3771da177e4SLinus Torvalds * ###--> it is unknown to the processes generating the input entropy. <-### 3781da177e4SLinus Torvalds * Because of this important property, this is a good, collision-resistant 3791da177e4SLinus Torvalds * hash; hash collisions will occur no more often than chance. 3801da177e4SLinus Torvalds */ 3811da177e4SLinus Torvalds 3821da177e4SLinus Torvalds /* 3831da177e4SLinus Torvalds * Static global variables 3841da177e4SLinus Torvalds */ 3851da177e4SLinus Torvalds static DECLARE_WAIT_QUEUE_HEAD(random_read_wait); 3861da177e4SLinus Torvalds static DECLARE_WAIT_QUEUE_HEAD(random_write_wait); 3879a6f70bbSJeff Dike static struct fasync_struct *fasync; 3881da177e4SLinus Torvalds 3891da177e4SLinus Torvalds #if 0 39090b75ee5SMatt Mackall static int debug; 3911da177e4SLinus Torvalds module_param(debug, bool, 0644); 39290b75ee5SMatt Mackall #define DEBUG_ENT(fmt, arg...) do { \ 39390b75ee5SMatt Mackall if (debug) \ 3941da177e4SLinus Torvalds printk(KERN_DEBUG "random %04d %04d %04d: " \ 3951da177e4SLinus Torvalds fmt,\ 3961da177e4SLinus Torvalds input_pool.entropy_count,\ 3971da177e4SLinus Torvalds blocking_pool.entropy_count,\ 3981da177e4SLinus Torvalds nonblocking_pool.entropy_count,\ 3991da177e4SLinus Torvalds ## arg); } while (0) 4001da177e4SLinus Torvalds #else 4011da177e4SLinus Torvalds #define DEBUG_ENT(fmt, arg...) do {} while (0) 4021da177e4SLinus Torvalds #endif 4031da177e4SLinus Torvalds 4041da177e4SLinus Torvalds /********************************************************************** 4051da177e4SLinus Torvalds * 4061da177e4SLinus Torvalds * OS independent entropy store. Here are the functions which handle 4071da177e4SLinus Torvalds * storing entropy in an entropy pool. 4081da177e4SLinus Torvalds * 4091da177e4SLinus Torvalds **********************************************************************/ 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds struct entropy_store; 4121da177e4SLinus Torvalds struct entropy_store { 41343358209SMatt Mackall /* read-only data: */ 4141da177e4SLinus Torvalds struct poolinfo *poolinfo; 4151da177e4SLinus Torvalds __u32 *pool; 4161da177e4SLinus Torvalds const char *name; 4171da177e4SLinus Torvalds struct entropy_store *pull; 4184015d9a8SRichard Kennedy int limit; 4191da177e4SLinus Torvalds 4201da177e4SLinus Torvalds /* read-write data: */ 42143358209SMatt Mackall spinlock_t lock; 4221da177e4SLinus Torvalds unsigned add_ptr; 423cda796a3SMatt Mackall int entropy_count; 4241da177e4SLinus Torvalds int input_rotate; 425e954bc91SMatt Mackall __u8 last_data[EXTRACT_SIZE]; 4261da177e4SLinus Torvalds }; 4271da177e4SLinus Torvalds 4281da177e4SLinus Torvalds static __u32 input_pool_data[INPUT_POOL_WORDS]; 4291da177e4SLinus Torvalds static __u32 blocking_pool_data[OUTPUT_POOL_WORDS]; 4301da177e4SLinus Torvalds static __u32 nonblocking_pool_data[OUTPUT_POOL_WORDS]; 4311da177e4SLinus Torvalds 4321da177e4SLinus Torvalds static struct entropy_store input_pool = { 4331da177e4SLinus Torvalds .poolinfo = &poolinfo_table[0], 4341da177e4SLinus Torvalds .name = "input", 4351da177e4SLinus Torvalds .limit = 1, 436e4d91918SIngo Molnar .lock = __SPIN_LOCK_UNLOCKED(&input_pool.lock), 4371da177e4SLinus Torvalds .pool = input_pool_data 4381da177e4SLinus Torvalds }; 4391da177e4SLinus Torvalds 4401da177e4SLinus Torvalds static struct entropy_store blocking_pool = { 4411da177e4SLinus Torvalds .poolinfo = &poolinfo_table[1], 4421da177e4SLinus Torvalds .name = "blocking", 4431da177e4SLinus Torvalds .limit = 1, 4441da177e4SLinus Torvalds .pull = &input_pool, 445e4d91918SIngo Molnar .lock = __SPIN_LOCK_UNLOCKED(&blocking_pool.lock), 4461da177e4SLinus Torvalds .pool = blocking_pool_data 4471da177e4SLinus Torvalds }; 4481da177e4SLinus Torvalds 4491da177e4SLinus Torvalds static struct entropy_store nonblocking_pool = { 4501da177e4SLinus Torvalds .poolinfo = &poolinfo_table[1], 4511da177e4SLinus Torvalds .name = "nonblocking", 4521da177e4SLinus Torvalds .pull = &input_pool, 453e4d91918SIngo Molnar .lock = __SPIN_LOCK_UNLOCKED(&nonblocking_pool.lock), 4541da177e4SLinus Torvalds .pool = nonblocking_pool_data 4551da177e4SLinus Torvalds }; 4561da177e4SLinus Torvalds 4571da177e4SLinus Torvalds /* 458e68e5b66SMatt Mackall * This function adds bytes into the entropy "pool". It does not 4591da177e4SLinus Torvalds * update the entropy estimate. The caller should call 460adc782daSMatt Mackall * credit_entropy_bits if this is appropriate. 4611da177e4SLinus Torvalds * 4621da177e4SLinus Torvalds * The pool is stirred with a primitive polynomial of the appropriate 4631da177e4SLinus Torvalds * degree, and then twisted. We twist by three bits at a time because 4641da177e4SLinus Torvalds * it's cheap to do so and helps slightly in the expected case where 4651da177e4SLinus Torvalds * the entropy is concentrated in the low-order bits. 4661da177e4SLinus Torvalds */ 467e68e5b66SMatt Mackall static void mix_pool_bytes_extract(struct entropy_store *r, const void *in, 468e68e5b66SMatt Mackall int nbytes, __u8 out[64]) 4691da177e4SLinus Torvalds { 4701da177e4SLinus Torvalds static __u32 const twist_table[8] = { 4711da177e4SLinus Torvalds 0x00000000, 0x3b6e20c8, 0x76dc4190, 0x4db26158, 4721da177e4SLinus Torvalds 0xedb88320, 0xd6d6a3e8, 0x9b64c2b0, 0xa00ae278 }; 473993ba211SMatt Mackall unsigned long i, j, tap1, tap2, tap3, tap4, tap5; 474feee7697SMatt Mackall int input_rotate; 4751da177e4SLinus Torvalds int wordmask = r->poolinfo->poolwords - 1; 476e68e5b66SMatt Mackall const char *bytes = in; 4776d38b827SMatt Mackall __u32 w; 4781da177e4SLinus Torvalds unsigned long flags; 4791da177e4SLinus Torvalds 4801da177e4SLinus Torvalds /* Taps are constant, so we can load them without holding r->lock. */ 4811da177e4SLinus Torvalds tap1 = r->poolinfo->tap1; 4821da177e4SLinus Torvalds tap2 = r->poolinfo->tap2; 4831da177e4SLinus Torvalds tap3 = r->poolinfo->tap3; 4841da177e4SLinus Torvalds tap4 = r->poolinfo->tap4; 4851da177e4SLinus Torvalds tap5 = r->poolinfo->tap5; 4861da177e4SLinus Torvalds 4871da177e4SLinus Torvalds spin_lock_irqsave(&r->lock, flags); 4881da177e4SLinus Torvalds input_rotate = r->input_rotate; 489993ba211SMatt Mackall i = r->add_ptr; 4901da177e4SLinus Torvalds 491e68e5b66SMatt Mackall /* mix one byte at a time to simplify size handling and churn faster */ 492e68e5b66SMatt Mackall while (nbytes--) { 493e68e5b66SMatt Mackall w = rol32(*bytes++, input_rotate & 31); 494993ba211SMatt Mackall i = (i - 1) & wordmask; 4951da177e4SLinus Torvalds 4961da177e4SLinus Torvalds /* XOR in the various taps */ 497993ba211SMatt Mackall w ^= r->pool[i]; 4981da177e4SLinus Torvalds w ^= r->pool[(i + tap1) & wordmask]; 4991da177e4SLinus Torvalds w ^= r->pool[(i + tap2) & wordmask]; 5001da177e4SLinus Torvalds w ^= r->pool[(i + tap3) & wordmask]; 5011da177e4SLinus Torvalds w ^= r->pool[(i + tap4) & wordmask]; 5021da177e4SLinus Torvalds w ^= r->pool[(i + tap5) & wordmask]; 503993ba211SMatt Mackall 504993ba211SMatt Mackall /* Mix the result back in with a twist */ 5051da177e4SLinus Torvalds r->pool[i] = (w >> 3) ^ twist_table[w & 7]; 506feee7697SMatt Mackall 507feee7697SMatt Mackall /* 508feee7697SMatt Mackall * Normally, we add 7 bits of rotation to the pool. 509feee7697SMatt Mackall * At the beginning of the pool, add an extra 7 bits 510feee7697SMatt Mackall * rotation, so that successive passes spread the 511feee7697SMatt Mackall * input bits across the pool evenly. 512feee7697SMatt Mackall */ 513feee7697SMatt Mackall input_rotate += i ? 7 : 14; 5141da177e4SLinus Torvalds } 5151da177e4SLinus Torvalds 5161da177e4SLinus Torvalds r->input_rotate = input_rotate; 517993ba211SMatt Mackall r->add_ptr = i; 5181da177e4SLinus Torvalds 519993ba211SMatt Mackall if (out) 520993ba211SMatt Mackall for (j = 0; j < 16; j++) 521e68e5b66SMatt Mackall ((__u32 *)out)[j] = r->pool[(i - j) & wordmask]; 5221da177e4SLinus Torvalds 5231da177e4SLinus Torvalds spin_unlock_irqrestore(&r->lock, flags); 5241da177e4SLinus Torvalds } 5251da177e4SLinus Torvalds 526e68e5b66SMatt Mackall static void mix_pool_bytes(struct entropy_store *r, const void *in, int bytes) 5271da177e4SLinus Torvalds { 528e68e5b66SMatt Mackall mix_pool_bytes_extract(r, in, bytes, NULL); 5291da177e4SLinus Torvalds } 5301da177e4SLinus Torvalds 5311da177e4SLinus Torvalds /* 5321da177e4SLinus Torvalds * Credit (or debit) the entropy store with n bits of entropy 5331da177e4SLinus Torvalds */ 534adc782daSMatt Mackall static void credit_entropy_bits(struct entropy_store *r, int nbits) 5351da177e4SLinus Torvalds { 5361da177e4SLinus Torvalds unsigned long flags; 5378b76f46aSAndrew Morton int entropy_count; 5381da177e4SLinus Torvalds 539adc782daSMatt Mackall if (!nbits) 540adc782daSMatt Mackall return; 541adc782daSMatt Mackall 5421da177e4SLinus Torvalds spin_lock_irqsave(&r->lock, flags); 5431da177e4SLinus Torvalds 544adc782daSMatt Mackall DEBUG_ENT("added %d entropy credits to %s\n", nbits, r->name); 5458b76f46aSAndrew Morton entropy_count = r->entropy_count; 5468b76f46aSAndrew Morton entropy_count += nbits; 5478b76f46aSAndrew Morton if (entropy_count < 0) { 548adc782daSMatt Mackall DEBUG_ENT("negative entropy/overflow\n"); 5498b76f46aSAndrew Morton entropy_count = 0; 5508b76f46aSAndrew Morton } else if (entropy_count > r->poolinfo->POOLBITS) 5518b76f46aSAndrew Morton entropy_count = r->poolinfo->POOLBITS; 5528b76f46aSAndrew Morton r->entropy_count = entropy_count; 5531da177e4SLinus Torvalds 55488c730daSMatt Mackall /* should we wake readers? */ 5558b76f46aSAndrew Morton if (r == &input_pool && entropy_count >= random_read_wakeup_thresh) { 55688c730daSMatt Mackall wake_up_interruptible(&random_read_wait); 5579a6f70bbSJeff Dike kill_fasync(&fasync, SIGIO, POLL_IN); 5589a6f70bbSJeff Dike } 5591da177e4SLinus Torvalds spin_unlock_irqrestore(&r->lock, flags); 5601da177e4SLinus Torvalds } 5611da177e4SLinus Torvalds 5621da177e4SLinus Torvalds /********************************************************************* 5631da177e4SLinus Torvalds * 5641da177e4SLinus Torvalds * Entropy input management 5651da177e4SLinus Torvalds * 5661da177e4SLinus Torvalds *********************************************************************/ 5671da177e4SLinus Torvalds 5681da177e4SLinus Torvalds /* There is one of these per entropy source */ 5691da177e4SLinus Torvalds struct timer_rand_state { 5701da177e4SLinus Torvalds cycles_t last_time; 5711da177e4SLinus Torvalds long last_delta, last_delta2; 5721da177e4SLinus Torvalds unsigned dont_count_entropy:1; 5731da177e4SLinus Torvalds }; 5741da177e4SLinus Torvalds 575d7e51e66SYinghai Lu #ifndef CONFIG_GENERIC_HARDIRQS 5762f983570SYinghai Lu 5772f983570SYinghai Lu static struct timer_rand_state *irq_timer_state[NR_IRQS]; 5782f983570SYinghai Lu 5792f983570SYinghai Lu static struct timer_rand_state *get_timer_rand_state(unsigned int irq) 5802f983570SYinghai Lu { 5812f983570SYinghai Lu return irq_timer_state[irq]; 5822f983570SYinghai Lu } 5832f983570SYinghai Lu 5842f983570SYinghai Lu static void set_timer_rand_state(unsigned int irq, 5852f983570SYinghai Lu struct timer_rand_state *state) 5862f983570SYinghai Lu { 5872f983570SYinghai Lu irq_timer_state[irq] = state; 5882f983570SYinghai Lu } 5892f983570SYinghai Lu 5902f983570SYinghai Lu #else 5912f983570SYinghai Lu 5922f983570SYinghai Lu static struct timer_rand_state *get_timer_rand_state(unsigned int irq) 5932f983570SYinghai Lu { 5942f983570SYinghai Lu struct irq_desc *desc; 5952f983570SYinghai Lu 5962f983570SYinghai Lu desc = irq_to_desc(irq); 5972f983570SYinghai Lu 5982f983570SYinghai Lu return desc->timer_rand_state; 5992f983570SYinghai Lu } 6002f983570SYinghai Lu 6012f983570SYinghai Lu static void set_timer_rand_state(unsigned int irq, 6022f983570SYinghai Lu struct timer_rand_state *state) 6032f983570SYinghai Lu { 6042f983570SYinghai Lu struct irq_desc *desc; 6052f983570SYinghai Lu 6062f983570SYinghai Lu desc = irq_to_desc(irq); 6072f983570SYinghai Lu 6082f983570SYinghai Lu desc->timer_rand_state = state; 6092f983570SYinghai Lu } 6100b8f1efaSYinghai Lu #endif 6113060d6feSYinghai Lu 6123060d6feSYinghai Lu static struct timer_rand_state input_timer_state; 6133060d6feSYinghai Lu 6141da177e4SLinus Torvalds /* 6151da177e4SLinus Torvalds * This function adds entropy to the entropy "pool" by using timing 6161da177e4SLinus Torvalds * delays. It uses the timer_rand_state structure to make an estimate 6171da177e4SLinus Torvalds * of how many bits of entropy this call has added to the pool. 6181da177e4SLinus Torvalds * 6191da177e4SLinus Torvalds * The number "num" is also added to the pool - it should somehow describe 6201da177e4SLinus Torvalds * the type of event which just happened. This is currently 0-255 for 6211da177e4SLinus Torvalds * keyboard scan codes, and 256 upwards for interrupts. 6221da177e4SLinus Torvalds * 6231da177e4SLinus Torvalds */ 6241da177e4SLinus Torvalds static void add_timer_randomness(struct timer_rand_state *state, unsigned num) 6251da177e4SLinus Torvalds { 6261da177e4SLinus Torvalds struct { 6271da177e4SLinus Torvalds cycles_t cycles; 6281da177e4SLinus Torvalds long jiffies; 6291da177e4SLinus Torvalds unsigned num; 6301da177e4SLinus Torvalds } sample; 6311da177e4SLinus Torvalds long delta, delta2, delta3; 6321da177e4SLinus Torvalds 6331da177e4SLinus Torvalds preempt_disable(); 6341da177e4SLinus Torvalds /* if over the trickle threshold, use only 1 in 4096 samples */ 6351da177e4SLinus Torvalds if (input_pool.entropy_count > trickle_thresh && 636b29c617aSChristoph Lameter ((__this_cpu_inc_return(trickle_count) - 1) & 0xfff)) 6371da177e4SLinus Torvalds goto out; 6381da177e4SLinus Torvalds 6391da177e4SLinus Torvalds sample.jiffies = jiffies; 6401da177e4SLinus Torvalds sample.cycles = get_cycles(); 6411da177e4SLinus Torvalds sample.num = num; 642e68e5b66SMatt Mackall mix_pool_bytes(&input_pool, &sample, sizeof(sample)); 6431da177e4SLinus Torvalds 6441da177e4SLinus Torvalds /* 6451da177e4SLinus Torvalds * Calculate number of bits of randomness we probably added. 6461da177e4SLinus Torvalds * We take into account the first, second and third-order deltas 6471da177e4SLinus Torvalds * in order to make our estimate. 6481da177e4SLinus Torvalds */ 6491da177e4SLinus Torvalds 6501da177e4SLinus Torvalds if (!state->dont_count_entropy) { 6511da177e4SLinus Torvalds delta = sample.jiffies - state->last_time; 6521da177e4SLinus Torvalds state->last_time = sample.jiffies; 6531da177e4SLinus Torvalds 6541da177e4SLinus Torvalds delta2 = delta - state->last_delta; 6551da177e4SLinus Torvalds state->last_delta = delta; 6561da177e4SLinus Torvalds 6571da177e4SLinus Torvalds delta3 = delta2 - state->last_delta2; 6581da177e4SLinus Torvalds state->last_delta2 = delta2; 6591da177e4SLinus Torvalds 6601da177e4SLinus Torvalds if (delta < 0) 6611da177e4SLinus Torvalds delta = -delta; 6621da177e4SLinus Torvalds if (delta2 < 0) 6631da177e4SLinus Torvalds delta2 = -delta2; 6641da177e4SLinus Torvalds if (delta3 < 0) 6651da177e4SLinus Torvalds delta3 = -delta3; 6661da177e4SLinus Torvalds if (delta > delta2) 6671da177e4SLinus Torvalds delta = delta2; 6681da177e4SLinus Torvalds if (delta > delta3) 6691da177e4SLinus Torvalds delta = delta3; 6701da177e4SLinus Torvalds 6711da177e4SLinus Torvalds /* 6721da177e4SLinus Torvalds * delta is now minimum absolute delta. 6731da177e4SLinus Torvalds * Round down by 1 bit on general principles, 6741da177e4SLinus Torvalds * and limit entropy entimate to 12 bits. 6751da177e4SLinus Torvalds */ 676adc782daSMatt Mackall credit_entropy_bits(&input_pool, 6771da177e4SLinus Torvalds min_t(int, fls(delta>>1), 11)); 6781da177e4SLinus Torvalds } 6791da177e4SLinus Torvalds out: 6801da177e4SLinus Torvalds preempt_enable(); 6811da177e4SLinus Torvalds } 6821da177e4SLinus Torvalds 683d251575aSStephen Hemminger void add_input_randomness(unsigned int type, unsigned int code, 6841da177e4SLinus Torvalds unsigned int value) 6851da177e4SLinus Torvalds { 6861da177e4SLinus Torvalds static unsigned char last_value; 6871da177e4SLinus Torvalds 6881da177e4SLinus Torvalds /* ignore autorepeat and the like */ 6891da177e4SLinus Torvalds if (value == last_value) 6901da177e4SLinus Torvalds return; 6911da177e4SLinus Torvalds 6921da177e4SLinus Torvalds DEBUG_ENT("input event\n"); 6931da177e4SLinus Torvalds last_value = value; 6941da177e4SLinus Torvalds add_timer_randomness(&input_timer_state, 6951da177e4SLinus Torvalds (type << 4) ^ code ^ (code >> 4) ^ value); 6961da177e4SLinus Torvalds } 69780fc9f53SDmitry Torokhov EXPORT_SYMBOL_GPL(add_input_randomness); 6981da177e4SLinus Torvalds 6991da177e4SLinus Torvalds void add_interrupt_randomness(int irq) 7001da177e4SLinus Torvalds { 7013060d6feSYinghai Lu struct timer_rand_state *state; 7023060d6feSYinghai Lu 7033060d6feSYinghai Lu state = get_timer_rand_state(irq); 7043060d6feSYinghai Lu 7053060d6feSYinghai Lu if (state == NULL) 7061da177e4SLinus Torvalds return; 7071da177e4SLinus Torvalds 7081da177e4SLinus Torvalds DEBUG_ENT("irq event %d\n", irq); 7093060d6feSYinghai Lu add_timer_randomness(state, 0x100 + irq); 7101da177e4SLinus Torvalds } 7111da177e4SLinus Torvalds 7129361401eSDavid Howells #ifdef CONFIG_BLOCK 7131da177e4SLinus Torvalds void add_disk_randomness(struct gendisk *disk) 7141da177e4SLinus Torvalds { 7151da177e4SLinus Torvalds if (!disk || !disk->random) 7161da177e4SLinus Torvalds return; 7171da177e4SLinus Torvalds /* first major is 1, so we get >= 0x200 here */ 718f331c029STejun Heo DEBUG_ENT("disk event %d:%d\n", 719f331c029STejun Heo MAJOR(disk_devt(disk)), MINOR(disk_devt(disk))); 7201da177e4SLinus Torvalds 721f331c029STejun Heo add_timer_randomness(disk->random, 0x100 + disk_devt(disk)); 7221da177e4SLinus Torvalds } 7239361401eSDavid Howells #endif 7241da177e4SLinus Torvalds 7251da177e4SLinus Torvalds /********************************************************************* 7261da177e4SLinus Torvalds * 7271da177e4SLinus Torvalds * Entropy extraction routines 7281da177e4SLinus Torvalds * 7291da177e4SLinus Torvalds *********************************************************************/ 7301da177e4SLinus Torvalds 7311da177e4SLinus Torvalds static ssize_t extract_entropy(struct entropy_store *r, void *buf, 7321da177e4SLinus Torvalds size_t nbytes, int min, int rsvd); 7331da177e4SLinus Torvalds 7341da177e4SLinus Torvalds /* 73525985edcSLucas De Marchi * This utility inline function is responsible for transferring entropy 7361da177e4SLinus Torvalds * from the primary pool to the secondary extraction pool. We make 7371da177e4SLinus Torvalds * sure we pull enough for a 'catastrophic reseed'. 7381da177e4SLinus Torvalds */ 7391da177e4SLinus Torvalds static void xfer_secondary_pool(struct entropy_store *r, size_t nbytes) 7401da177e4SLinus Torvalds { 7411da177e4SLinus Torvalds __u32 tmp[OUTPUT_POOL_WORDS]; 7421da177e4SLinus Torvalds 7431da177e4SLinus Torvalds if (r->pull && r->entropy_count < nbytes * 8 && 7441da177e4SLinus Torvalds r->entropy_count < r->poolinfo->POOLBITS) { 7455a021e9fSMatt Mackall /* If we're limited, always leave two wakeup worth's BITS */ 7461da177e4SLinus Torvalds int rsvd = r->limit ? 0 : random_read_wakeup_thresh/4; 7475a021e9fSMatt Mackall int bytes = nbytes; 7485a021e9fSMatt Mackall 7495a021e9fSMatt Mackall /* pull at least as many as BYTES as wakeup BITS */ 7505a021e9fSMatt Mackall bytes = max_t(int, bytes, random_read_wakeup_thresh / 8); 7515a021e9fSMatt Mackall /* but never more than the buffer size */ 7525a021e9fSMatt Mackall bytes = min_t(int, bytes, sizeof(tmp)); 7531da177e4SLinus Torvalds 7541da177e4SLinus Torvalds DEBUG_ENT("going to reseed %s with %d bits " 7551da177e4SLinus Torvalds "(%d of %d requested)\n", 7561da177e4SLinus Torvalds r->name, bytes * 8, nbytes * 8, r->entropy_count); 7571da177e4SLinus Torvalds 7581da177e4SLinus Torvalds bytes = extract_entropy(r->pull, tmp, bytes, 7591da177e4SLinus Torvalds random_read_wakeup_thresh / 8, rsvd); 760e68e5b66SMatt Mackall mix_pool_bytes(r, tmp, bytes); 761adc782daSMatt Mackall credit_entropy_bits(r, bytes*8); 7621da177e4SLinus Torvalds } 7631da177e4SLinus Torvalds } 7641da177e4SLinus Torvalds 7651da177e4SLinus Torvalds /* 7661da177e4SLinus Torvalds * These functions extracts randomness from the "entropy pool", and 7671da177e4SLinus Torvalds * returns it in a buffer. 7681da177e4SLinus Torvalds * 7691da177e4SLinus Torvalds * The min parameter specifies the minimum amount we can pull before 7701da177e4SLinus Torvalds * failing to avoid races that defeat catastrophic reseeding while the 7711da177e4SLinus Torvalds * reserved parameter indicates how much entropy we must leave in the 7721da177e4SLinus Torvalds * pool after each pull to avoid starving other readers. 7731da177e4SLinus Torvalds * 7741da177e4SLinus Torvalds * Note: extract_entropy() assumes that .poolwords is a multiple of 16 words. 7751da177e4SLinus Torvalds */ 7761da177e4SLinus Torvalds 7771da177e4SLinus Torvalds static size_t account(struct entropy_store *r, size_t nbytes, int min, 7781da177e4SLinus Torvalds int reserved) 7791da177e4SLinus Torvalds { 7801da177e4SLinus Torvalds unsigned long flags; 7811da177e4SLinus Torvalds 7821da177e4SLinus Torvalds /* Hold lock while accounting */ 7831da177e4SLinus Torvalds spin_lock_irqsave(&r->lock, flags); 7841da177e4SLinus Torvalds 785cda796a3SMatt Mackall BUG_ON(r->entropy_count > r->poolinfo->POOLBITS); 7861da177e4SLinus Torvalds DEBUG_ENT("trying to extract %d bits from %s\n", 7871da177e4SLinus Torvalds nbytes * 8, r->name); 7881da177e4SLinus Torvalds 7891da177e4SLinus Torvalds /* Can we pull enough? */ 7901da177e4SLinus Torvalds if (r->entropy_count / 8 < min + reserved) { 7911da177e4SLinus Torvalds nbytes = 0; 7921da177e4SLinus Torvalds } else { 7931da177e4SLinus Torvalds /* If limited, never pull more than available */ 7941da177e4SLinus Torvalds if (r->limit && nbytes + reserved >= r->entropy_count / 8) 7951da177e4SLinus Torvalds nbytes = r->entropy_count/8 - reserved; 7961da177e4SLinus Torvalds 7971da177e4SLinus Torvalds if (r->entropy_count / 8 >= nbytes + reserved) 7981da177e4SLinus Torvalds r->entropy_count -= nbytes*8; 7991da177e4SLinus Torvalds else 8001da177e4SLinus Torvalds r->entropy_count = reserved; 8011da177e4SLinus Torvalds 8029a6f70bbSJeff Dike if (r->entropy_count < random_write_wakeup_thresh) { 8031da177e4SLinus Torvalds wake_up_interruptible(&random_write_wait); 8049a6f70bbSJeff Dike kill_fasync(&fasync, SIGIO, POLL_OUT); 8059a6f70bbSJeff Dike } 8061da177e4SLinus Torvalds } 8071da177e4SLinus Torvalds 8081da177e4SLinus Torvalds DEBUG_ENT("debiting %d entropy credits from %s%s\n", 8091da177e4SLinus Torvalds nbytes * 8, r->name, r->limit ? "" : " (unlimited)"); 8101da177e4SLinus Torvalds 8111da177e4SLinus Torvalds spin_unlock_irqrestore(&r->lock, flags); 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds return nbytes; 8141da177e4SLinus Torvalds } 8151da177e4SLinus Torvalds 8161da177e4SLinus Torvalds static void extract_buf(struct entropy_store *r, __u8 *out) 8171da177e4SLinus Torvalds { 818602b6aeeSMatt Mackall int i; 819e68e5b66SMatt Mackall __u32 hash[5], workspace[SHA_WORKSPACE_WORDS]; 820e68e5b66SMatt Mackall __u8 extract[64]; 8211da177e4SLinus Torvalds 8221c0ad3d4SMatt Mackall /* Generate a hash across the pool, 16 words (512 bits) at a time */ 823ffd8d3faSMatt Mackall sha_init(hash); 8241c0ad3d4SMatt Mackall for (i = 0; i < r->poolinfo->poolwords; i += 16) 825ffd8d3faSMatt Mackall sha_transform(hash, (__u8 *)(r->pool + i), workspace); 8261da177e4SLinus Torvalds 8271da177e4SLinus Torvalds /* 8281c0ad3d4SMatt Mackall * We mix the hash back into the pool to prevent backtracking 8291c0ad3d4SMatt Mackall * attacks (where the attacker knows the state of the pool 8301c0ad3d4SMatt Mackall * plus the current outputs, and attempts to find previous 8311c0ad3d4SMatt Mackall * ouputs), unless the hash function can be inverted. By 8321c0ad3d4SMatt Mackall * mixing at least a SHA1 worth of hash data back, we make 8331c0ad3d4SMatt Mackall * brute-forcing the feedback as hard as brute-forcing the 8341c0ad3d4SMatt Mackall * hash. 8351da177e4SLinus Torvalds */ 836e68e5b66SMatt Mackall mix_pool_bytes_extract(r, hash, sizeof(hash), extract); 8371c0ad3d4SMatt Mackall 8381c0ad3d4SMatt Mackall /* 8391c0ad3d4SMatt Mackall * To avoid duplicates, we atomically extract a portion of the 8401c0ad3d4SMatt Mackall * pool while mixing, and hash one final time. 8411c0ad3d4SMatt Mackall */ 842e68e5b66SMatt Mackall sha_transform(hash, extract, workspace); 843ffd8d3faSMatt Mackall memset(extract, 0, sizeof(extract)); 844ffd8d3faSMatt Mackall memset(workspace, 0, sizeof(workspace)); 8451da177e4SLinus Torvalds 8461da177e4SLinus Torvalds /* 8471c0ad3d4SMatt Mackall * In case the hash function has some recognizable output 8481c0ad3d4SMatt Mackall * pattern, we fold it in half. Thus, we always feed back 8491c0ad3d4SMatt Mackall * twice as much data as we output. 8501da177e4SLinus Torvalds */ 851ffd8d3faSMatt Mackall hash[0] ^= hash[3]; 852ffd8d3faSMatt Mackall hash[1] ^= hash[4]; 853ffd8d3faSMatt Mackall hash[2] ^= rol32(hash[2], 16); 854ffd8d3faSMatt Mackall memcpy(out, hash, EXTRACT_SIZE); 855ffd8d3faSMatt Mackall memset(hash, 0, sizeof(hash)); 8561da177e4SLinus Torvalds } 8571da177e4SLinus Torvalds 8581da177e4SLinus Torvalds static ssize_t extract_entropy(struct entropy_store *r, void *buf, 8591da177e4SLinus Torvalds size_t nbytes, int min, int reserved) 8601da177e4SLinus Torvalds { 8611da177e4SLinus Torvalds ssize_t ret = 0, i; 8621da177e4SLinus Torvalds __u8 tmp[EXTRACT_SIZE]; 8635b739ef8SNeil Horman unsigned long flags; 8641da177e4SLinus Torvalds 8651da177e4SLinus Torvalds xfer_secondary_pool(r, nbytes); 8661da177e4SLinus Torvalds nbytes = account(r, nbytes, min, reserved); 8671da177e4SLinus Torvalds 8681da177e4SLinus Torvalds while (nbytes) { 8691da177e4SLinus Torvalds extract_buf(r, tmp); 8705b739ef8SNeil Horman 871e954bc91SMatt Mackall if (fips_enabled) { 8725b739ef8SNeil Horman spin_lock_irqsave(&r->lock, flags); 8735b739ef8SNeil Horman if (!memcmp(tmp, r->last_data, EXTRACT_SIZE)) 8745b739ef8SNeil Horman panic("Hardware RNG duplicated output!\n"); 8755b739ef8SNeil Horman memcpy(r->last_data, tmp, EXTRACT_SIZE); 8765b739ef8SNeil Horman spin_unlock_irqrestore(&r->lock, flags); 8775b739ef8SNeil Horman } 8781da177e4SLinus Torvalds i = min_t(int, nbytes, EXTRACT_SIZE); 8791da177e4SLinus Torvalds memcpy(buf, tmp, i); 8801da177e4SLinus Torvalds nbytes -= i; 8811da177e4SLinus Torvalds buf += i; 8821da177e4SLinus Torvalds ret += i; 8831da177e4SLinus Torvalds } 8841da177e4SLinus Torvalds 8851da177e4SLinus Torvalds /* Wipe data just returned from memory */ 8861da177e4SLinus Torvalds memset(tmp, 0, sizeof(tmp)); 8871da177e4SLinus Torvalds 8881da177e4SLinus Torvalds return ret; 8891da177e4SLinus Torvalds } 8901da177e4SLinus Torvalds 8911da177e4SLinus Torvalds static ssize_t extract_entropy_user(struct entropy_store *r, void __user *buf, 8921da177e4SLinus Torvalds size_t nbytes) 8931da177e4SLinus Torvalds { 8941da177e4SLinus Torvalds ssize_t ret = 0, i; 8951da177e4SLinus Torvalds __u8 tmp[EXTRACT_SIZE]; 8961da177e4SLinus Torvalds 8971da177e4SLinus Torvalds xfer_secondary_pool(r, nbytes); 8981da177e4SLinus Torvalds nbytes = account(r, nbytes, 0, 0); 8991da177e4SLinus Torvalds 9001da177e4SLinus Torvalds while (nbytes) { 9011da177e4SLinus Torvalds if (need_resched()) { 9021da177e4SLinus Torvalds if (signal_pending(current)) { 9031da177e4SLinus Torvalds if (ret == 0) 9041da177e4SLinus Torvalds ret = -ERESTARTSYS; 9051da177e4SLinus Torvalds break; 9061da177e4SLinus Torvalds } 9071da177e4SLinus Torvalds schedule(); 9081da177e4SLinus Torvalds } 9091da177e4SLinus Torvalds 9101da177e4SLinus Torvalds extract_buf(r, tmp); 9111da177e4SLinus Torvalds i = min_t(int, nbytes, EXTRACT_SIZE); 9121da177e4SLinus Torvalds if (copy_to_user(buf, tmp, i)) { 9131da177e4SLinus Torvalds ret = -EFAULT; 9141da177e4SLinus Torvalds break; 9151da177e4SLinus Torvalds } 9161da177e4SLinus Torvalds 9171da177e4SLinus Torvalds nbytes -= i; 9181da177e4SLinus Torvalds buf += i; 9191da177e4SLinus Torvalds ret += i; 9201da177e4SLinus Torvalds } 9211da177e4SLinus Torvalds 9221da177e4SLinus Torvalds /* Wipe data just returned from memory */ 9231da177e4SLinus Torvalds memset(tmp, 0, sizeof(tmp)); 9241da177e4SLinus Torvalds 9251da177e4SLinus Torvalds return ret; 9261da177e4SLinus Torvalds } 9271da177e4SLinus Torvalds 9281da177e4SLinus Torvalds /* 9291da177e4SLinus Torvalds * This function is the exported kernel interface. It returns some 9301da177e4SLinus Torvalds * number of good random numbers, suitable for seeding TCP sequence 9311da177e4SLinus Torvalds * numbers, etc. 9321da177e4SLinus Torvalds */ 9331da177e4SLinus Torvalds void get_random_bytes(void *buf, int nbytes) 9341da177e4SLinus Torvalds { 93563d77173SH. Peter Anvin char *p = buf; 93663d77173SH. Peter Anvin 93763d77173SH. Peter Anvin while (nbytes) { 93863d77173SH. Peter Anvin unsigned long v; 93963d77173SH. Peter Anvin int chunk = min(nbytes, (int)sizeof(unsigned long)); 94063d77173SH. Peter Anvin 94163d77173SH. Peter Anvin if (!arch_get_random_long(&v)) 94263d77173SH. Peter Anvin break; 94363d77173SH. Peter Anvin 944*bd29e568SLuck, Tony memcpy(p, &v, chunk); 94563d77173SH. Peter Anvin p += chunk; 94663d77173SH. Peter Anvin nbytes -= chunk; 94763d77173SH. Peter Anvin } 94863d77173SH. Peter Anvin 94963d77173SH. Peter Anvin extract_entropy(&nonblocking_pool, p, nbytes, 0, 0); 9501da177e4SLinus Torvalds } 9511da177e4SLinus Torvalds EXPORT_SYMBOL(get_random_bytes); 9521da177e4SLinus Torvalds 9531da177e4SLinus Torvalds /* 9541da177e4SLinus Torvalds * init_std_data - initialize pool with system data 9551da177e4SLinus Torvalds * 9561da177e4SLinus Torvalds * @r: pool to initialize 9571da177e4SLinus Torvalds * 9581da177e4SLinus Torvalds * This function clears the pool's entropy count and mixes some system 9591da177e4SLinus Torvalds * data into the pool to prepare it for use. The pool is not cleared 9601da177e4SLinus Torvalds * as that can only decrease the entropy in the pool. 9611da177e4SLinus Torvalds */ 9621da177e4SLinus Torvalds static void init_std_data(struct entropy_store *r) 9631da177e4SLinus Torvalds { 964f8595815SEric Dumazet ktime_t now; 9651da177e4SLinus Torvalds unsigned long flags; 9661da177e4SLinus Torvalds 9671da177e4SLinus Torvalds spin_lock_irqsave(&r->lock, flags); 9681da177e4SLinus Torvalds r->entropy_count = 0; 9691da177e4SLinus Torvalds spin_unlock_irqrestore(&r->lock, flags); 9701da177e4SLinus Torvalds 971f8595815SEric Dumazet now = ktime_get_real(); 972e68e5b66SMatt Mackall mix_pool_bytes(r, &now, sizeof(now)); 973e68e5b66SMatt Mackall mix_pool_bytes(r, utsname(), sizeof(*(utsname()))); 9741da177e4SLinus Torvalds } 9751da177e4SLinus Torvalds 97653c3f63eSMatt Mackall static int rand_initialize(void) 9771da177e4SLinus Torvalds { 9781da177e4SLinus Torvalds init_std_data(&input_pool); 9791da177e4SLinus Torvalds init_std_data(&blocking_pool); 9801da177e4SLinus Torvalds init_std_data(&nonblocking_pool); 9811da177e4SLinus Torvalds return 0; 9821da177e4SLinus Torvalds } 9831da177e4SLinus Torvalds module_init(rand_initialize); 9841da177e4SLinus Torvalds 9851da177e4SLinus Torvalds void rand_initialize_irq(int irq) 9861da177e4SLinus Torvalds { 9871da177e4SLinus Torvalds struct timer_rand_state *state; 9881da177e4SLinus Torvalds 9893060d6feSYinghai Lu state = get_timer_rand_state(irq); 9903060d6feSYinghai Lu 9913060d6feSYinghai Lu if (state) 9921da177e4SLinus Torvalds return; 9931da177e4SLinus Torvalds 9941da177e4SLinus Torvalds /* 995f8595815SEric Dumazet * If kzalloc returns null, we just won't use that entropy 9961da177e4SLinus Torvalds * source. 9971da177e4SLinus Torvalds */ 998f8595815SEric Dumazet state = kzalloc(sizeof(struct timer_rand_state), GFP_KERNEL); 999f8595815SEric Dumazet if (state) 10003060d6feSYinghai Lu set_timer_rand_state(irq, state); 10011da177e4SLinus Torvalds } 10021da177e4SLinus Torvalds 10039361401eSDavid Howells #ifdef CONFIG_BLOCK 10041da177e4SLinus Torvalds void rand_initialize_disk(struct gendisk *disk) 10051da177e4SLinus Torvalds { 10061da177e4SLinus Torvalds struct timer_rand_state *state; 10071da177e4SLinus Torvalds 10081da177e4SLinus Torvalds /* 1009f8595815SEric Dumazet * If kzalloc returns null, we just won't use that entropy 10101da177e4SLinus Torvalds * source. 10111da177e4SLinus Torvalds */ 1012f8595815SEric Dumazet state = kzalloc(sizeof(struct timer_rand_state), GFP_KERNEL); 1013f8595815SEric Dumazet if (state) 10141da177e4SLinus Torvalds disk->random = state; 10151da177e4SLinus Torvalds } 10169361401eSDavid Howells #endif 10171da177e4SLinus Torvalds 10181da177e4SLinus Torvalds static ssize_t 10191da177e4SLinus Torvalds random_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos) 10201da177e4SLinus Torvalds { 10211da177e4SLinus Torvalds ssize_t n, retval = 0, count = 0; 10221da177e4SLinus Torvalds 10231da177e4SLinus Torvalds if (nbytes == 0) 10241da177e4SLinus Torvalds return 0; 10251da177e4SLinus Torvalds 10261da177e4SLinus Torvalds while (nbytes > 0) { 10271da177e4SLinus Torvalds n = nbytes; 10281da177e4SLinus Torvalds if (n > SEC_XFER_SIZE) 10291da177e4SLinus Torvalds n = SEC_XFER_SIZE; 10301da177e4SLinus Torvalds 10311da177e4SLinus Torvalds DEBUG_ENT("reading %d bits\n", n*8); 10321da177e4SLinus Torvalds 10331da177e4SLinus Torvalds n = extract_entropy_user(&blocking_pool, buf, n); 10341da177e4SLinus Torvalds 10351da177e4SLinus Torvalds DEBUG_ENT("read got %d bits (%d still needed)\n", 10361da177e4SLinus Torvalds n*8, (nbytes-n)*8); 10371da177e4SLinus Torvalds 10381da177e4SLinus Torvalds if (n == 0) { 10391da177e4SLinus Torvalds if (file->f_flags & O_NONBLOCK) { 10401da177e4SLinus Torvalds retval = -EAGAIN; 10411da177e4SLinus Torvalds break; 10421da177e4SLinus Torvalds } 10431da177e4SLinus Torvalds 10441da177e4SLinus Torvalds DEBUG_ENT("sleeping?\n"); 10451da177e4SLinus Torvalds 10461da177e4SLinus Torvalds wait_event_interruptible(random_read_wait, 10471da177e4SLinus Torvalds input_pool.entropy_count >= 10481da177e4SLinus Torvalds random_read_wakeup_thresh); 10491da177e4SLinus Torvalds 10501da177e4SLinus Torvalds DEBUG_ENT("awake\n"); 10511da177e4SLinus Torvalds 10521da177e4SLinus Torvalds if (signal_pending(current)) { 10531da177e4SLinus Torvalds retval = -ERESTARTSYS; 10541da177e4SLinus Torvalds break; 10551da177e4SLinus Torvalds } 10561da177e4SLinus Torvalds 10571da177e4SLinus Torvalds continue; 10581da177e4SLinus Torvalds } 10591da177e4SLinus Torvalds 10601da177e4SLinus Torvalds if (n < 0) { 10611da177e4SLinus Torvalds retval = n; 10621da177e4SLinus Torvalds break; 10631da177e4SLinus Torvalds } 10641da177e4SLinus Torvalds count += n; 10651da177e4SLinus Torvalds buf += n; 10661da177e4SLinus Torvalds nbytes -= n; 10671da177e4SLinus Torvalds break; /* This break makes the device work */ 10681da177e4SLinus Torvalds /* like a named pipe */ 10691da177e4SLinus Torvalds } 10701da177e4SLinus Torvalds 10711da177e4SLinus Torvalds return (count ? count : retval); 10721da177e4SLinus Torvalds } 10731da177e4SLinus Torvalds 10741da177e4SLinus Torvalds static ssize_t 107590b75ee5SMatt Mackall urandom_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos) 10761da177e4SLinus Torvalds { 10771da177e4SLinus Torvalds return extract_entropy_user(&nonblocking_pool, buf, nbytes); 10781da177e4SLinus Torvalds } 10791da177e4SLinus Torvalds 10801da177e4SLinus Torvalds static unsigned int 10811da177e4SLinus Torvalds random_poll(struct file *file, poll_table * wait) 10821da177e4SLinus Torvalds { 10831da177e4SLinus Torvalds unsigned int mask; 10841da177e4SLinus Torvalds 10851da177e4SLinus Torvalds poll_wait(file, &random_read_wait, wait); 10861da177e4SLinus Torvalds poll_wait(file, &random_write_wait, wait); 10871da177e4SLinus Torvalds mask = 0; 10881da177e4SLinus Torvalds if (input_pool.entropy_count >= random_read_wakeup_thresh) 10891da177e4SLinus Torvalds mask |= POLLIN | POLLRDNORM; 10901da177e4SLinus Torvalds if (input_pool.entropy_count < random_write_wakeup_thresh) 10911da177e4SLinus Torvalds mask |= POLLOUT | POLLWRNORM; 10921da177e4SLinus Torvalds return mask; 10931da177e4SLinus Torvalds } 10941da177e4SLinus Torvalds 10957f397dcdSMatt Mackall static int 10967f397dcdSMatt Mackall write_pool(struct entropy_store *r, const char __user *buffer, size_t count) 10977f397dcdSMatt Mackall { 10987f397dcdSMatt Mackall size_t bytes; 10997f397dcdSMatt Mackall __u32 buf[16]; 11007f397dcdSMatt Mackall const char __user *p = buffer; 11017f397dcdSMatt Mackall 11027f397dcdSMatt Mackall while (count > 0) { 11037f397dcdSMatt Mackall bytes = min(count, sizeof(buf)); 11047f397dcdSMatt Mackall if (copy_from_user(&buf, p, bytes)) 11057f397dcdSMatt Mackall return -EFAULT; 11067f397dcdSMatt Mackall 11077f397dcdSMatt Mackall count -= bytes; 11087f397dcdSMatt Mackall p += bytes; 11097f397dcdSMatt Mackall 1110e68e5b66SMatt Mackall mix_pool_bytes(r, buf, bytes); 111191f3f1e3SMatt Mackall cond_resched(); 11127f397dcdSMatt Mackall } 11137f397dcdSMatt Mackall 11147f397dcdSMatt Mackall return 0; 11157f397dcdSMatt Mackall } 11167f397dcdSMatt Mackall 111790b75ee5SMatt Mackall static ssize_t random_write(struct file *file, const char __user *buffer, 11181da177e4SLinus Torvalds size_t count, loff_t *ppos) 11191da177e4SLinus Torvalds { 11207f397dcdSMatt Mackall size_t ret; 11217f397dcdSMatt Mackall 11227f397dcdSMatt Mackall ret = write_pool(&blocking_pool, buffer, count); 11237f397dcdSMatt Mackall if (ret) 11247f397dcdSMatt Mackall return ret; 11257f397dcdSMatt Mackall ret = write_pool(&nonblocking_pool, buffer, count); 11267f397dcdSMatt Mackall if (ret) 11277f397dcdSMatt Mackall return ret; 11287f397dcdSMatt Mackall 11297f397dcdSMatt Mackall return (ssize_t)count; 11301da177e4SLinus Torvalds } 11311da177e4SLinus Torvalds 113243ae4860SMatt Mackall static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg) 11331da177e4SLinus Torvalds { 11341da177e4SLinus Torvalds int size, ent_count; 11351da177e4SLinus Torvalds int __user *p = (int __user *)arg; 11361da177e4SLinus Torvalds int retval; 11371da177e4SLinus Torvalds 11381da177e4SLinus Torvalds switch (cmd) { 11391da177e4SLinus Torvalds case RNDGETENTCNT: 114043ae4860SMatt Mackall /* inherently racy, no point locking */ 114143ae4860SMatt Mackall if (put_user(input_pool.entropy_count, p)) 11421da177e4SLinus Torvalds return -EFAULT; 11431da177e4SLinus Torvalds return 0; 11441da177e4SLinus Torvalds case RNDADDTOENTCNT: 11451da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 11461da177e4SLinus Torvalds return -EPERM; 11471da177e4SLinus Torvalds if (get_user(ent_count, p)) 11481da177e4SLinus Torvalds return -EFAULT; 1149adc782daSMatt Mackall credit_entropy_bits(&input_pool, ent_count); 11501da177e4SLinus Torvalds return 0; 11511da177e4SLinus Torvalds case RNDADDENTROPY: 11521da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 11531da177e4SLinus Torvalds return -EPERM; 11541da177e4SLinus Torvalds if (get_user(ent_count, p++)) 11551da177e4SLinus Torvalds return -EFAULT; 11561da177e4SLinus Torvalds if (ent_count < 0) 11571da177e4SLinus Torvalds return -EINVAL; 11581da177e4SLinus Torvalds if (get_user(size, p++)) 11591da177e4SLinus Torvalds return -EFAULT; 11607f397dcdSMatt Mackall retval = write_pool(&input_pool, (const char __user *)p, 11617f397dcdSMatt Mackall size); 11621da177e4SLinus Torvalds if (retval < 0) 11631da177e4SLinus Torvalds return retval; 1164adc782daSMatt Mackall credit_entropy_bits(&input_pool, ent_count); 11651da177e4SLinus Torvalds return 0; 11661da177e4SLinus Torvalds case RNDZAPENTCNT: 11671da177e4SLinus Torvalds case RNDCLEARPOOL: 11681da177e4SLinus Torvalds /* Clear the entropy pool counters. */ 11691da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 11701da177e4SLinus Torvalds return -EPERM; 117153c3f63eSMatt Mackall rand_initialize(); 11721da177e4SLinus Torvalds return 0; 11731da177e4SLinus Torvalds default: 11741da177e4SLinus Torvalds return -EINVAL; 11751da177e4SLinus Torvalds } 11761da177e4SLinus Torvalds } 11771da177e4SLinus Torvalds 11789a6f70bbSJeff Dike static int random_fasync(int fd, struct file *filp, int on) 11799a6f70bbSJeff Dike { 11809a6f70bbSJeff Dike return fasync_helper(fd, filp, on, &fasync); 11819a6f70bbSJeff Dike } 11829a6f70bbSJeff Dike 11832b8693c0SArjan van de Ven const struct file_operations random_fops = { 11841da177e4SLinus Torvalds .read = random_read, 11851da177e4SLinus Torvalds .write = random_write, 11861da177e4SLinus Torvalds .poll = random_poll, 118743ae4860SMatt Mackall .unlocked_ioctl = random_ioctl, 11889a6f70bbSJeff Dike .fasync = random_fasync, 11896038f373SArnd Bergmann .llseek = noop_llseek, 11901da177e4SLinus Torvalds }; 11911da177e4SLinus Torvalds 11922b8693c0SArjan van de Ven const struct file_operations urandom_fops = { 11931da177e4SLinus Torvalds .read = urandom_read, 11941da177e4SLinus Torvalds .write = random_write, 119543ae4860SMatt Mackall .unlocked_ioctl = random_ioctl, 11969a6f70bbSJeff Dike .fasync = random_fasync, 11976038f373SArnd Bergmann .llseek = noop_llseek, 11981da177e4SLinus Torvalds }; 11991da177e4SLinus Torvalds 12001da177e4SLinus Torvalds /*************************************************************** 12011da177e4SLinus Torvalds * Random UUID interface 12021da177e4SLinus Torvalds * 12031da177e4SLinus Torvalds * Used here for a Boot ID, but can be useful for other kernel 12041da177e4SLinus Torvalds * drivers. 12051da177e4SLinus Torvalds ***************************************************************/ 12061da177e4SLinus Torvalds 12071da177e4SLinus Torvalds /* 12081da177e4SLinus Torvalds * Generate random UUID 12091da177e4SLinus Torvalds */ 12101da177e4SLinus Torvalds void generate_random_uuid(unsigned char uuid_out[16]) 12111da177e4SLinus Torvalds { 12121da177e4SLinus Torvalds get_random_bytes(uuid_out, 16); 1213c41b20e7SAdam Buchbinder /* Set UUID version to 4 --- truly random generation */ 12141da177e4SLinus Torvalds uuid_out[6] = (uuid_out[6] & 0x0F) | 0x40; 12151da177e4SLinus Torvalds /* Set the UUID variant to DCE */ 12161da177e4SLinus Torvalds uuid_out[8] = (uuid_out[8] & 0x3F) | 0x80; 12171da177e4SLinus Torvalds } 12181da177e4SLinus Torvalds EXPORT_SYMBOL(generate_random_uuid); 12191da177e4SLinus Torvalds 12201da177e4SLinus Torvalds /******************************************************************** 12211da177e4SLinus Torvalds * 12221da177e4SLinus Torvalds * Sysctl interface 12231da177e4SLinus Torvalds * 12241da177e4SLinus Torvalds ********************************************************************/ 12251da177e4SLinus Torvalds 12261da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 12271da177e4SLinus Torvalds 12281da177e4SLinus Torvalds #include <linux/sysctl.h> 12291da177e4SLinus Torvalds 12301da177e4SLinus Torvalds static int min_read_thresh = 8, min_write_thresh; 12311da177e4SLinus Torvalds static int max_read_thresh = INPUT_POOL_WORDS * 32; 12321da177e4SLinus Torvalds static int max_write_thresh = INPUT_POOL_WORDS * 32; 12331da177e4SLinus Torvalds static char sysctl_bootid[16]; 12341da177e4SLinus Torvalds 12351da177e4SLinus Torvalds /* 12361da177e4SLinus Torvalds * These functions is used to return both the bootid UUID, and random 12371da177e4SLinus Torvalds * UUID. The difference is in whether table->data is NULL; if it is, 12381da177e4SLinus Torvalds * then a new UUID is generated and returned to the user. 12391da177e4SLinus Torvalds * 12401da177e4SLinus Torvalds * If the user accesses this via the proc interface, it will be returned 12411da177e4SLinus Torvalds * as an ASCII string in the standard UUID format. If accesses via the 12421da177e4SLinus Torvalds * sysctl system call, it is returned as 16 bytes of binary data. 12431da177e4SLinus Torvalds */ 12448d65af78SAlexey Dobriyan static int proc_do_uuid(ctl_table *table, int write, 12451da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 12461da177e4SLinus Torvalds { 12471da177e4SLinus Torvalds ctl_table fake_table; 12481da177e4SLinus Torvalds unsigned char buf[64], tmp_uuid[16], *uuid; 12491da177e4SLinus Torvalds 12501da177e4SLinus Torvalds uuid = table->data; 12511da177e4SLinus Torvalds if (!uuid) { 12521da177e4SLinus Torvalds uuid = tmp_uuid; 12531da177e4SLinus Torvalds uuid[8] = 0; 12541da177e4SLinus Torvalds } 12551da177e4SLinus Torvalds if (uuid[8] == 0) 12561da177e4SLinus Torvalds generate_random_uuid(uuid); 12571da177e4SLinus Torvalds 125835900771SJoe Perches sprintf(buf, "%pU", uuid); 125935900771SJoe Perches 12601da177e4SLinus Torvalds fake_table.data = buf; 12611da177e4SLinus Torvalds fake_table.maxlen = sizeof(buf); 12621da177e4SLinus Torvalds 12638d65af78SAlexey Dobriyan return proc_dostring(&fake_table, write, buffer, lenp, ppos); 12641da177e4SLinus Torvalds } 12651da177e4SLinus Torvalds 12661da177e4SLinus Torvalds static int sysctl_poolsize = INPUT_POOL_WORDS * 32; 12671da177e4SLinus Torvalds ctl_table random_table[] = { 12681da177e4SLinus Torvalds { 12691da177e4SLinus Torvalds .procname = "poolsize", 12701da177e4SLinus Torvalds .data = &sysctl_poolsize, 12711da177e4SLinus Torvalds .maxlen = sizeof(int), 12721da177e4SLinus Torvalds .mode = 0444, 12736d456111SEric W. Biederman .proc_handler = proc_dointvec, 12741da177e4SLinus Torvalds }, 12751da177e4SLinus Torvalds { 12761da177e4SLinus Torvalds .procname = "entropy_avail", 12771da177e4SLinus Torvalds .maxlen = sizeof(int), 12781da177e4SLinus Torvalds .mode = 0444, 12796d456111SEric W. Biederman .proc_handler = proc_dointvec, 12801da177e4SLinus Torvalds .data = &input_pool.entropy_count, 12811da177e4SLinus Torvalds }, 12821da177e4SLinus Torvalds { 12831da177e4SLinus Torvalds .procname = "read_wakeup_threshold", 12841da177e4SLinus Torvalds .data = &random_read_wakeup_thresh, 12851da177e4SLinus Torvalds .maxlen = sizeof(int), 12861da177e4SLinus Torvalds .mode = 0644, 12876d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 12881da177e4SLinus Torvalds .extra1 = &min_read_thresh, 12891da177e4SLinus Torvalds .extra2 = &max_read_thresh, 12901da177e4SLinus Torvalds }, 12911da177e4SLinus Torvalds { 12921da177e4SLinus Torvalds .procname = "write_wakeup_threshold", 12931da177e4SLinus Torvalds .data = &random_write_wakeup_thresh, 12941da177e4SLinus Torvalds .maxlen = sizeof(int), 12951da177e4SLinus Torvalds .mode = 0644, 12966d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 12971da177e4SLinus Torvalds .extra1 = &min_write_thresh, 12981da177e4SLinus Torvalds .extra2 = &max_write_thresh, 12991da177e4SLinus Torvalds }, 13001da177e4SLinus Torvalds { 13011da177e4SLinus Torvalds .procname = "boot_id", 13021da177e4SLinus Torvalds .data = &sysctl_bootid, 13031da177e4SLinus Torvalds .maxlen = 16, 13041da177e4SLinus Torvalds .mode = 0444, 13056d456111SEric W. Biederman .proc_handler = proc_do_uuid, 13061da177e4SLinus Torvalds }, 13071da177e4SLinus Torvalds { 13081da177e4SLinus Torvalds .procname = "uuid", 13091da177e4SLinus Torvalds .maxlen = 16, 13101da177e4SLinus Torvalds .mode = 0444, 13116d456111SEric W. Biederman .proc_handler = proc_do_uuid, 13121da177e4SLinus Torvalds }, 1313894d2491SEric W. Biederman { } 13141da177e4SLinus Torvalds }; 13151da177e4SLinus Torvalds #endif /* CONFIG_SYSCTL */ 13161da177e4SLinus Torvalds 13176e5714eaSDavid S. Miller static u32 random_int_secret[MD5_MESSAGE_BYTES / 4] ____cacheline_aligned; 13181da177e4SLinus Torvalds 13196e5714eaSDavid S. Miller static int __init random_int_secret_init(void) 13201da177e4SLinus Torvalds { 13216e5714eaSDavid S. Miller get_random_bytes(random_int_secret, sizeof(random_int_secret)); 13221da177e4SLinus Torvalds return 0; 13231da177e4SLinus Torvalds } 13246e5714eaSDavid S. Miller late_initcall(random_int_secret_init); 13251da177e4SLinus Torvalds 13261da177e4SLinus Torvalds /* 13271da177e4SLinus Torvalds * Get a random word for internal kernel use only. Similar to urandom but 13281da177e4SLinus Torvalds * with the goal of minimal entropy pool depletion. As a result, the random 13291da177e4SLinus Torvalds * value is not cryptographically secure but for several uses the cost of 13301da177e4SLinus Torvalds * depleting entropy is too high 13311da177e4SLinus Torvalds */ 13326e5714eaSDavid S. Miller DEFINE_PER_CPU(__u32 [MD5_DIGEST_WORDS], get_random_int_hash); 13331da177e4SLinus Torvalds unsigned int get_random_int(void) 13341da177e4SLinus Torvalds { 133563d77173SH. Peter Anvin __u32 *hash; 13366e5714eaSDavid S. Miller unsigned int ret; 13378a0a9bd4SLinus Torvalds 133863d77173SH. Peter Anvin if (arch_get_random_int(&ret)) 133963d77173SH. Peter Anvin return ret; 134063d77173SH. Peter Anvin 134163d77173SH. Peter Anvin hash = get_cpu_var(get_random_int_hash); 13428a0a9bd4SLinus Torvalds 134326a9a418SLinus Torvalds hash[0] += current->pid + jiffies + get_cycles(); 13446e5714eaSDavid S. Miller md5_transform(hash, random_int_secret); 13456e5714eaSDavid S. Miller ret = hash[0]; 13468a0a9bd4SLinus Torvalds put_cpu_var(get_random_int_hash); 13478a0a9bd4SLinus Torvalds 13488a0a9bd4SLinus Torvalds return ret; 13491da177e4SLinus Torvalds } 13501da177e4SLinus Torvalds 13511da177e4SLinus Torvalds /* 13521da177e4SLinus Torvalds * randomize_range() returns a start address such that 13531da177e4SLinus Torvalds * 13541da177e4SLinus Torvalds * [...... <range> .....] 13551da177e4SLinus Torvalds * start end 13561da177e4SLinus Torvalds * 13571da177e4SLinus Torvalds * a <range> with size "len" starting at the return value is inside in the 13581da177e4SLinus Torvalds * area defined by [start, end], but is otherwise randomized. 13591da177e4SLinus Torvalds */ 13601da177e4SLinus Torvalds unsigned long 13611da177e4SLinus Torvalds randomize_range(unsigned long start, unsigned long end, unsigned long len) 13621da177e4SLinus Torvalds { 13631da177e4SLinus Torvalds unsigned long range = end - len - start; 13641da177e4SLinus Torvalds 13651da177e4SLinus Torvalds if (end <= start + len) 13661da177e4SLinus Torvalds return 0; 13671da177e4SLinus Torvalds return PAGE_ALIGN(get_random_int() % range + start); 13681da177e4SLinus Torvalds } 1369