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> 2521da177e4SLinus Torvalds #include <linux/spinlock.h> 2531da177e4SLinus Torvalds #include <linux/percpu.h> 2541da177e4SLinus Torvalds #include <linux/cryptohash.h> 2555b739ef8SNeil Horman #include <linux/fips.h> 256775f4b29STheodore Ts'o #include <linux/ptrace.h> 257e6d4947bSTheodore Ts'o #include <linux/kmemcheck.h> 2581da177e4SLinus Torvalds 259d178a1ebSYinghai Lu #ifdef CONFIG_GENERIC_HARDIRQS 260d178a1ebSYinghai Lu # include <linux/irq.h> 261d178a1ebSYinghai Lu #endif 262d178a1ebSYinghai Lu 2631da177e4SLinus Torvalds #include <asm/processor.h> 2641da177e4SLinus Torvalds #include <asm/uaccess.h> 2651da177e4SLinus Torvalds #include <asm/irq.h> 266775f4b29STheodore Ts'o #include <asm/irq_regs.h> 2671da177e4SLinus Torvalds #include <asm/io.h> 2681da177e4SLinus Torvalds 26900ce1db1STheodore Ts'o #define CREATE_TRACE_POINTS 27000ce1db1STheodore Ts'o #include <trace/events/random.h> 27100ce1db1STheodore Ts'o 2721da177e4SLinus Torvalds /* 2731da177e4SLinus Torvalds * Configuration information 2741da177e4SLinus Torvalds */ 2751da177e4SLinus Torvalds #define INPUT_POOL_WORDS 128 2761da177e4SLinus Torvalds #define OUTPUT_POOL_WORDS 32 2771da177e4SLinus Torvalds #define SEC_XFER_SIZE 512 278e954bc91SMatt Mackall #define EXTRACT_SIZE 10 2791da177e4SLinus Torvalds 280d2e7c96aSH. Peter Anvin #define LONGS(x) (((x) + sizeof(unsigned long) - 1)/sizeof(unsigned long)) 281d2e7c96aSH. Peter Anvin 2821da177e4SLinus Torvalds /* 2831da177e4SLinus Torvalds * The minimum number of bits of entropy before we wake up a read on 2841da177e4SLinus Torvalds * /dev/random. Should be enough to do a significant reseed. 2851da177e4SLinus Torvalds */ 2861da177e4SLinus Torvalds static int random_read_wakeup_thresh = 64; 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds /* 2891da177e4SLinus Torvalds * If the entropy count falls under this number of bits, then we 2901da177e4SLinus Torvalds * should wake up processes which are selecting or polling on write 2911da177e4SLinus Torvalds * access to /dev/random. 2921da177e4SLinus Torvalds */ 2931da177e4SLinus Torvalds static int random_write_wakeup_thresh = 128; 2941da177e4SLinus Torvalds 2951da177e4SLinus Torvalds /* 2961da177e4SLinus Torvalds * When the input pool goes over trickle_thresh, start dropping most 2971da177e4SLinus Torvalds * samples to avoid wasting CPU time and reduce lock contention. 2981da177e4SLinus Torvalds */ 2991da177e4SLinus Torvalds 3006c036527SChristoph Lameter static int trickle_thresh __read_mostly = INPUT_POOL_WORDS * 28; 3011da177e4SLinus Torvalds 30290b75ee5SMatt Mackall static DEFINE_PER_CPU(int, trickle_count); 3031da177e4SLinus Torvalds 3041da177e4SLinus Torvalds /* 3051da177e4SLinus Torvalds * A pool of size .poolwords is stirred with a primitive polynomial 3061da177e4SLinus Torvalds * of degree .poolwords over GF(2). The taps for various sizes are 3071da177e4SLinus Torvalds * defined below. They are chosen to be evenly spaced (minimum RMS 3081da177e4SLinus Torvalds * distance from evenly spaced; the numbers in the comments are a 3091da177e4SLinus Torvalds * scaled squared error sum) except for the last tap, which is 1 to 3101da177e4SLinus Torvalds * get the twisting happening as fast as possible. 3111da177e4SLinus Torvalds */ 3121da177e4SLinus Torvalds static struct poolinfo { 3131da177e4SLinus Torvalds int poolwords; 3141da177e4SLinus Torvalds int tap1, tap2, tap3, tap4, tap5; 3151da177e4SLinus Torvalds } poolinfo_table[] = { 3161da177e4SLinus Torvalds /* x^128 + x^103 + x^76 + x^51 +x^25 + x + 1 -- 105 */ 3171da177e4SLinus Torvalds { 128, 103, 76, 51, 25, 1 }, 3181da177e4SLinus Torvalds /* x^32 + x^26 + x^20 + x^14 + x^7 + x + 1 -- 15 */ 3191da177e4SLinus Torvalds { 32, 26, 20, 14, 7, 1 }, 3201da177e4SLinus Torvalds #if 0 3211da177e4SLinus Torvalds /* x^2048 + x^1638 + x^1231 + x^819 + x^411 + x + 1 -- 115 */ 3221da177e4SLinus Torvalds { 2048, 1638, 1231, 819, 411, 1 }, 3231da177e4SLinus Torvalds 3241da177e4SLinus Torvalds /* x^1024 + x^817 + x^615 + x^412 + x^204 + x + 1 -- 290 */ 3251da177e4SLinus Torvalds { 1024, 817, 615, 412, 204, 1 }, 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds /* x^1024 + x^819 + x^616 + x^410 + x^207 + x^2 + 1 -- 115 */ 3281da177e4SLinus Torvalds { 1024, 819, 616, 410, 207, 2 }, 3291da177e4SLinus Torvalds 3301da177e4SLinus Torvalds /* x^512 + x^411 + x^308 + x^208 + x^104 + x + 1 -- 225 */ 3311da177e4SLinus Torvalds { 512, 411, 308, 208, 104, 1 }, 3321da177e4SLinus Torvalds 3331da177e4SLinus Torvalds /* x^512 + x^409 + x^307 + x^206 + x^102 + x^2 + 1 -- 95 */ 3341da177e4SLinus Torvalds { 512, 409, 307, 206, 102, 2 }, 3351da177e4SLinus Torvalds /* x^512 + x^409 + x^309 + x^205 + x^103 + x^2 + 1 -- 95 */ 3361da177e4SLinus Torvalds { 512, 409, 309, 205, 103, 2 }, 3371da177e4SLinus Torvalds 3381da177e4SLinus Torvalds /* x^256 + x^205 + x^155 + x^101 + x^52 + x + 1 -- 125 */ 3391da177e4SLinus Torvalds { 256, 205, 155, 101, 52, 1 }, 3401da177e4SLinus Torvalds 3411da177e4SLinus Torvalds /* x^128 + x^103 + x^78 + x^51 + x^27 + x^2 + 1 -- 70 */ 3421da177e4SLinus Torvalds { 128, 103, 78, 51, 27, 2 }, 3431da177e4SLinus Torvalds 3441da177e4SLinus Torvalds /* x^64 + x^52 + x^39 + x^26 + x^14 + x + 1 -- 15 */ 3451da177e4SLinus Torvalds { 64, 52, 39, 26, 14, 1 }, 3461da177e4SLinus Torvalds #endif 3471da177e4SLinus Torvalds }; 3481da177e4SLinus Torvalds 3491da177e4SLinus Torvalds #define POOLBITS poolwords*32 3501da177e4SLinus Torvalds #define POOLBYTES poolwords*4 3511da177e4SLinus Torvalds 3521da177e4SLinus Torvalds /* 3531da177e4SLinus Torvalds * For the purposes of better mixing, we use the CRC-32 polynomial as 3541da177e4SLinus Torvalds * well to make a twisted Generalized Feedback Shift Reigster 3551da177e4SLinus Torvalds * 3561da177e4SLinus Torvalds * (See M. Matsumoto & Y. Kurita, 1992. Twisted GFSR generators. ACM 3571da177e4SLinus Torvalds * Transactions on Modeling and Computer Simulation 2(3):179-194. 3581da177e4SLinus Torvalds * Also see M. Matsumoto & Y. Kurita, 1994. Twisted GFSR generators 3591da177e4SLinus Torvalds * II. ACM Transactions on Mdeling and Computer Simulation 4:254-266) 3601da177e4SLinus Torvalds * 3611da177e4SLinus Torvalds * Thanks to Colin Plumb for suggesting this. 3621da177e4SLinus Torvalds * 3631da177e4SLinus Torvalds * We have not analyzed the resultant polynomial to prove it primitive; 3641da177e4SLinus Torvalds * in fact it almost certainly isn't. Nonetheless, the irreducible factors 3651da177e4SLinus Torvalds * of a random large-degree polynomial over GF(2) are more than large enough 3661da177e4SLinus Torvalds * that periodicity is not a concern. 3671da177e4SLinus Torvalds * 3681da177e4SLinus Torvalds * The input hash is much less sensitive than the output hash. All 3691da177e4SLinus Torvalds * that we want of it is that it be a good non-cryptographic hash; 3701da177e4SLinus Torvalds * i.e. it not produce collisions when fed "random" data of the sort 3711da177e4SLinus Torvalds * we expect to see. As long as the pool state differs for different 3721da177e4SLinus Torvalds * inputs, we have preserved the input entropy and done a good job. 3731da177e4SLinus Torvalds * The fact that an intelligent attacker can construct inputs that 3741da177e4SLinus Torvalds * will produce controlled alterations to the pool's state is not 3751da177e4SLinus Torvalds * important because we don't consider such inputs to contribute any 3761da177e4SLinus Torvalds * randomness. The only property we need with respect to them is that 3771da177e4SLinus Torvalds * the attacker can't increase his/her knowledge of the pool's state. 3781da177e4SLinus Torvalds * Since all additions are reversible (knowing the final state and the 3791da177e4SLinus Torvalds * input, you can reconstruct the initial state), if an attacker has 3801da177e4SLinus Torvalds * any uncertainty about the initial state, he/she can only shuffle 3811da177e4SLinus Torvalds * that uncertainty about, but never cause any collisions (which would 3821da177e4SLinus Torvalds * decrease the uncertainty). 3831da177e4SLinus Torvalds * 3841da177e4SLinus Torvalds * The chosen system lets the state of the pool be (essentially) the input 3851da177e4SLinus Torvalds * modulo the generator polymnomial. Now, for random primitive polynomials, 3861da177e4SLinus Torvalds * this is a universal class of hash functions, meaning that the chance 3871da177e4SLinus Torvalds * of a collision is limited by the attacker's knowledge of the generator 3881da177e4SLinus Torvalds * polynomail, so if it is chosen at random, an attacker can never force 3891da177e4SLinus Torvalds * a collision. Here, we use a fixed polynomial, but we *can* assume that 3901da177e4SLinus Torvalds * ###--> it is unknown to the processes generating the input entropy. <-### 3911da177e4SLinus Torvalds * Because of this important property, this is a good, collision-resistant 3921da177e4SLinus Torvalds * hash; hash collisions will occur no more often than chance. 3931da177e4SLinus Torvalds */ 3941da177e4SLinus Torvalds 3951da177e4SLinus Torvalds /* 3961da177e4SLinus Torvalds * Static global variables 3971da177e4SLinus Torvalds */ 3981da177e4SLinus Torvalds static DECLARE_WAIT_QUEUE_HEAD(random_read_wait); 3991da177e4SLinus Torvalds static DECLARE_WAIT_QUEUE_HEAD(random_write_wait); 4009a6f70bbSJeff Dike static struct fasync_struct *fasync; 4011da177e4SLinus Torvalds 40290ab5ee9SRusty Russell static bool debug; 4031da177e4SLinus Torvalds module_param(debug, bool, 0644); 40490b75ee5SMatt Mackall #define DEBUG_ENT(fmt, arg...) do { \ 40590b75ee5SMatt Mackall if (debug) \ 4061da177e4SLinus Torvalds printk(KERN_DEBUG "random %04d %04d %04d: " \ 4071da177e4SLinus Torvalds fmt,\ 4081da177e4SLinus Torvalds input_pool.entropy_count,\ 4091da177e4SLinus Torvalds blocking_pool.entropy_count,\ 4101da177e4SLinus Torvalds nonblocking_pool.entropy_count,\ 4111da177e4SLinus Torvalds ## arg); } while (0) 4121da177e4SLinus Torvalds 4131da177e4SLinus Torvalds /********************************************************************** 4141da177e4SLinus Torvalds * 4151da177e4SLinus Torvalds * OS independent entropy store. Here are the functions which handle 4161da177e4SLinus Torvalds * storing entropy in an entropy pool. 4171da177e4SLinus Torvalds * 4181da177e4SLinus Torvalds **********************************************************************/ 4191da177e4SLinus Torvalds 4201da177e4SLinus Torvalds struct entropy_store; 4211da177e4SLinus Torvalds struct entropy_store { 42243358209SMatt Mackall /* read-only data: */ 4231da177e4SLinus Torvalds struct poolinfo *poolinfo; 4241da177e4SLinus Torvalds __u32 *pool; 4251da177e4SLinus Torvalds const char *name; 4261da177e4SLinus Torvalds struct entropy_store *pull; 4274015d9a8SRichard Kennedy int limit; 4281da177e4SLinus Torvalds 4291da177e4SLinus Torvalds /* read-write data: */ 43043358209SMatt Mackall spinlock_t lock; 4311da177e4SLinus Torvalds unsigned add_ptr; 432902c098aSTheodore Ts'o unsigned input_rotate; 433cda796a3SMatt Mackall int entropy_count; 434775f4b29STheodore Ts'o int entropy_total; 435775f4b29STheodore Ts'o unsigned int initialized:1; 436ec8f02daSJarod Wilson bool last_data_init; 437e954bc91SMatt Mackall __u8 last_data[EXTRACT_SIZE]; 4381da177e4SLinus Torvalds }; 4391da177e4SLinus Torvalds 4401da177e4SLinus Torvalds static __u32 input_pool_data[INPUT_POOL_WORDS]; 4411da177e4SLinus Torvalds static __u32 blocking_pool_data[OUTPUT_POOL_WORDS]; 4421da177e4SLinus Torvalds static __u32 nonblocking_pool_data[OUTPUT_POOL_WORDS]; 4431da177e4SLinus Torvalds 4441da177e4SLinus Torvalds static struct entropy_store input_pool = { 4451da177e4SLinus Torvalds .poolinfo = &poolinfo_table[0], 4461da177e4SLinus Torvalds .name = "input", 4471da177e4SLinus Torvalds .limit = 1, 448eece09ecSThomas Gleixner .lock = __SPIN_LOCK_UNLOCKED(input_pool.lock), 4491da177e4SLinus Torvalds .pool = input_pool_data 4501da177e4SLinus Torvalds }; 4511da177e4SLinus Torvalds 4521da177e4SLinus Torvalds static struct entropy_store blocking_pool = { 4531da177e4SLinus Torvalds .poolinfo = &poolinfo_table[1], 4541da177e4SLinus Torvalds .name = "blocking", 4551da177e4SLinus Torvalds .limit = 1, 4561da177e4SLinus Torvalds .pull = &input_pool, 457eece09ecSThomas Gleixner .lock = __SPIN_LOCK_UNLOCKED(blocking_pool.lock), 4581da177e4SLinus Torvalds .pool = blocking_pool_data 4591da177e4SLinus Torvalds }; 4601da177e4SLinus Torvalds 4611da177e4SLinus Torvalds static struct entropy_store nonblocking_pool = { 4621da177e4SLinus Torvalds .poolinfo = &poolinfo_table[1], 4631da177e4SLinus Torvalds .name = "nonblocking", 4641da177e4SLinus Torvalds .pull = &input_pool, 465eece09ecSThomas Gleixner .lock = __SPIN_LOCK_UNLOCKED(nonblocking_pool.lock), 4661da177e4SLinus Torvalds .pool = nonblocking_pool_data 4671da177e4SLinus Torvalds }; 4681da177e4SLinus Torvalds 469775f4b29STheodore Ts'o static __u32 const twist_table[8] = { 470775f4b29STheodore Ts'o 0x00000000, 0x3b6e20c8, 0x76dc4190, 0x4db26158, 471775f4b29STheodore Ts'o 0xedb88320, 0xd6d6a3e8, 0x9b64c2b0, 0xa00ae278 }; 472775f4b29STheodore Ts'o 4731da177e4SLinus Torvalds /* 474e68e5b66SMatt Mackall * This function adds bytes into the entropy "pool". It does not 4751da177e4SLinus Torvalds * update the entropy estimate. The caller should call 476adc782daSMatt Mackall * credit_entropy_bits if this is appropriate. 4771da177e4SLinus Torvalds * 4781da177e4SLinus Torvalds * The pool is stirred with a primitive polynomial of the appropriate 4791da177e4SLinus Torvalds * degree, and then twisted. We twist by three bits at a time because 4801da177e4SLinus Torvalds * it's cheap to do so and helps slightly in the expected case where 4811da177e4SLinus Torvalds * the entropy is concentrated in the low-order bits. 4821da177e4SLinus Torvalds */ 48300ce1db1STheodore Ts'o static void _mix_pool_bytes(struct entropy_store *r, const void *in, 484e68e5b66SMatt Mackall int nbytes, __u8 out[64]) 4851da177e4SLinus Torvalds { 486993ba211SMatt Mackall unsigned long i, j, tap1, tap2, tap3, tap4, tap5; 487feee7697SMatt Mackall int input_rotate; 4881da177e4SLinus Torvalds int wordmask = r->poolinfo->poolwords - 1; 489e68e5b66SMatt Mackall const char *bytes = in; 4906d38b827SMatt Mackall __u32 w; 4911da177e4SLinus Torvalds 4921da177e4SLinus Torvalds tap1 = r->poolinfo->tap1; 4931da177e4SLinus Torvalds tap2 = r->poolinfo->tap2; 4941da177e4SLinus Torvalds tap3 = r->poolinfo->tap3; 4951da177e4SLinus Torvalds tap4 = r->poolinfo->tap4; 4961da177e4SLinus Torvalds tap5 = r->poolinfo->tap5; 4971da177e4SLinus Torvalds 498902c098aSTheodore Ts'o smp_rmb(); 499902c098aSTheodore Ts'o input_rotate = ACCESS_ONCE(r->input_rotate); 500902c098aSTheodore Ts'o i = ACCESS_ONCE(r->add_ptr); 5011da177e4SLinus Torvalds 502e68e5b66SMatt Mackall /* mix one byte at a time to simplify size handling and churn faster */ 503e68e5b66SMatt Mackall while (nbytes--) { 504e68e5b66SMatt Mackall w = rol32(*bytes++, input_rotate & 31); 505993ba211SMatt Mackall i = (i - 1) & wordmask; 5061da177e4SLinus Torvalds 5071da177e4SLinus Torvalds /* XOR in the various taps */ 508993ba211SMatt Mackall w ^= r->pool[i]; 5091da177e4SLinus Torvalds w ^= r->pool[(i + tap1) & wordmask]; 5101da177e4SLinus Torvalds w ^= r->pool[(i + tap2) & wordmask]; 5111da177e4SLinus Torvalds w ^= r->pool[(i + tap3) & wordmask]; 5121da177e4SLinus Torvalds w ^= r->pool[(i + tap4) & wordmask]; 5131da177e4SLinus Torvalds w ^= r->pool[(i + tap5) & wordmask]; 514993ba211SMatt Mackall 515993ba211SMatt Mackall /* Mix the result back in with a twist */ 5161da177e4SLinus Torvalds r->pool[i] = (w >> 3) ^ twist_table[w & 7]; 517feee7697SMatt Mackall 518feee7697SMatt Mackall /* 519feee7697SMatt Mackall * Normally, we add 7 bits of rotation to the pool. 520feee7697SMatt Mackall * At the beginning of the pool, add an extra 7 bits 521feee7697SMatt Mackall * rotation, so that successive passes spread the 522feee7697SMatt Mackall * input bits across the pool evenly. 523feee7697SMatt Mackall */ 524feee7697SMatt Mackall input_rotate += i ? 7 : 14; 5251da177e4SLinus Torvalds } 5261da177e4SLinus Torvalds 527902c098aSTheodore Ts'o ACCESS_ONCE(r->input_rotate) = input_rotate; 528902c098aSTheodore Ts'o ACCESS_ONCE(r->add_ptr) = i; 529902c098aSTheodore Ts'o smp_wmb(); 5301da177e4SLinus Torvalds 531993ba211SMatt Mackall if (out) 532993ba211SMatt Mackall for (j = 0; j < 16; j++) 533e68e5b66SMatt Mackall ((__u32 *)out)[j] = r->pool[(i - j) & wordmask]; 5341da177e4SLinus Torvalds } 5351da177e4SLinus Torvalds 53600ce1db1STheodore Ts'o static void __mix_pool_bytes(struct entropy_store *r, const void *in, 53700ce1db1STheodore Ts'o int nbytes, __u8 out[64]) 53800ce1db1STheodore Ts'o { 53900ce1db1STheodore Ts'o trace_mix_pool_bytes_nolock(r->name, nbytes, _RET_IP_); 54000ce1db1STheodore Ts'o _mix_pool_bytes(r, in, nbytes, out); 54100ce1db1STheodore Ts'o } 54200ce1db1STheodore Ts'o 543902c098aSTheodore Ts'o static void mix_pool_bytes(struct entropy_store *r, const void *in, 544902c098aSTheodore Ts'o int nbytes, __u8 out[64]) 5451da177e4SLinus Torvalds { 546902c098aSTheodore Ts'o unsigned long flags; 547902c098aSTheodore Ts'o 54800ce1db1STheodore Ts'o trace_mix_pool_bytes(r->name, nbytes, _RET_IP_); 549902c098aSTheodore Ts'o spin_lock_irqsave(&r->lock, flags); 55000ce1db1STheodore Ts'o _mix_pool_bytes(r, in, nbytes, out); 551902c098aSTheodore Ts'o spin_unlock_irqrestore(&r->lock, flags); 5521da177e4SLinus Torvalds } 5531da177e4SLinus Torvalds 554775f4b29STheodore Ts'o struct fast_pool { 555775f4b29STheodore Ts'o __u32 pool[4]; 556775f4b29STheodore Ts'o unsigned long last; 557775f4b29STheodore Ts'o unsigned short count; 558775f4b29STheodore Ts'o unsigned char rotate; 559775f4b29STheodore Ts'o unsigned char last_timer_intr; 560775f4b29STheodore Ts'o }; 561775f4b29STheodore Ts'o 562775f4b29STheodore Ts'o /* 563775f4b29STheodore Ts'o * This is a fast mixing routine used by the interrupt randomness 564775f4b29STheodore Ts'o * collector. It's hardcoded for an 128 bit pool and assumes that any 565775f4b29STheodore Ts'o * locks that might be needed are taken by the caller. 566775f4b29STheodore Ts'o */ 567775f4b29STheodore Ts'o static void fast_mix(struct fast_pool *f, const void *in, int nbytes) 568775f4b29STheodore Ts'o { 569775f4b29STheodore Ts'o const char *bytes = in; 570775f4b29STheodore Ts'o __u32 w; 571775f4b29STheodore Ts'o unsigned i = f->count; 572775f4b29STheodore Ts'o unsigned input_rotate = f->rotate; 573775f4b29STheodore Ts'o 574775f4b29STheodore Ts'o while (nbytes--) { 575775f4b29STheodore Ts'o w = rol32(*bytes++, input_rotate & 31) ^ f->pool[i & 3] ^ 576775f4b29STheodore Ts'o f->pool[(i + 1) & 3]; 577775f4b29STheodore Ts'o f->pool[i & 3] = (w >> 3) ^ twist_table[w & 7]; 578775f4b29STheodore Ts'o input_rotate += (i++ & 3) ? 7 : 14; 579775f4b29STheodore Ts'o } 580775f4b29STheodore Ts'o f->count = i; 581775f4b29STheodore Ts'o f->rotate = input_rotate; 582775f4b29STheodore Ts'o } 583775f4b29STheodore Ts'o 5841da177e4SLinus Torvalds /* 5851da177e4SLinus Torvalds * Credit (or debit) the entropy store with n bits of entropy 5861da177e4SLinus Torvalds */ 587adc782daSMatt Mackall static void credit_entropy_bits(struct entropy_store *r, int nbits) 5881da177e4SLinus Torvalds { 589902c098aSTheodore Ts'o int entropy_count, orig; 5901da177e4SLinus Torvalds 591adc782daSMatt Mackall if (!nbits) 592adc782daSMatt Mackall return; 593adc782daSMatt Mackall 594adc782daSMatt Mackall DEBUG_ENT("added %d entropy credits to %s\n", nbits, r->name); 595902c098aSTheodore Ts'o retry: 596902c098aSTheodore Ts'o entropy_count = orig = ACCESS_ONCE(r->entropy_count); 5978b76f46aSAndrew Morton entropy_count += nbits; 59800ce1db1STheodore Ts'o 5998b76f46aSAndrew Morton if (entropy_count < 0) { 600adc782daSMatt Mackall DEBUG_ENT("negative entropy/overflow\n"); 6018b76f46aSAndrew Morton entropy_count = 0; 6028b76f46aSAndrew Morton } else if (entropy_count > r->poolinfo->POOLBITS) 6038b76f46aSAndrew Morton entropy_count = r->poolinfo->POOLBITS; 604902c098aSTheodore Ts'o if (cmpxchg(&r->entropy_count, orig, entropy_count) != orig) 605902c098aSTheodore Ts'o goto retry; 6061da177e4SLinus Torvalds 607775f4b29STheodore Ts'o if (!r->initialized && nbits > 0) { 608775f4b29STheodore Ts'o r->entropy_total += nbits; 609775f4b29STheodore Ts'o if (r->entropy_total > 128) 610775f4b29STheodore Ts'o r->initialized = 1; 611775f4b29STheodore Ts'o } 612775f4b29STheodore Ts'o 61300ce1db1STheodore Ts'o trace_credit_entropy_bits(r->name, nbits, entropy_count, 61400ce1db1STheodore Ts'o r->entropy_total, _RET_IP_); 61500ce1db1STheodore Ts'o 61688c730daSMatt Mackall /* should we wake readers? */ 6178b76f46aSAndrew Morton if (r == &input_pool && entropy_count >= random_read_wakeup_thresh) { 61888c730daSMatt Mackall wake_up_interruptible(&random_read_wait); 6199a6f70bbSJeff Dike kill_fasync(&fasync, SIGIO, POLL_IN); 6209a6f70bbSJeff Dike } 6211da177e4SLinus Torvalds } 6221da177e4SLinus Torvalds 6231da177e4SLinus Torvalds /********************************************************************* 6241da177e4SLinus Torvalds * 6251da177e4SLinus Torvalds * Entropy input management 6261da177e4SLinus Torvalds * 6271da177e4SLinus Torvalds *********************************************************************/ 6281da177e4SLinus Torvalds 6291da177e4SLinus Torvalds /* There is one of these per entropy source */ 6301da177e4SLinus Torvalds struct timer_rand_state { 6311da177e4SLinus Torvalds cycles_t last_time; 6321da177e4SLinus Torvalds long last_delta, last_delta2; 6331da177e4SLinus Torvalds unsigned dont_count_entropy:1; 6341da177e4SLinus Torvalds }; 6351da177e4SLinus Torvalds 636a2080a67SLinus Torvalds /* 637a2080a67SLinus Torvalds * Add device- or boot-specific data to the input and nonblocking 638a2080a67SLinus Torvalds * pools to help initialize them to unique values. 639a2080a67SLinus Torvalds * 640a2080a67SLinus Torvalds * None of this adds any entropy, it is meant to avoid the 641a2080a67SLinus Torvalds * problem of the nonblocking pool having similar initial state 642a2080a67SLinus Torvalds * across largely identical devices. 643a2080a67SLinus Torvalds */ 644a2080a67SLinus Torvalds void add_device_randomness(const void *buf, unsigned int size) 645a2080a67SLinus Torvalds { 646*61875f30STheodore Ts'o unsigned long time = random_get_entropy() ^ jiffies; 647a2080a67SLinus Torvalds 648a2080a67SLinus Torvalds mix_pool_bytes(&input_pool, buf, size, NULL); 649a2080a67SLinus Torvalds mix_pool_bytes(&input_pool, &time, sizeof(time), NULL); 650a2080a67SLinus Torvalds mix_pool_bytes(&nonblocking_pool, buf, size, NULL); 651a2080a67SLinus Torvalds mix_pool_bytes(&nonblocking_pool, &time, sizeof(time), NULL); 652a2080a67SLinus Torvalds } 653a2080a67SLinus Torvalds EXPORT_SYMBOL(add_device_randomness); 654a2080a67SLinus Torvalds 6553060d6feSYinghai Lu static struct timer_rand_state input_timer_state; 6563060d6feSYinghai Lu 6571da177e4SLinus Torvalds /* 6581da177e4SLinus Torvalds * This function adds entropy to the entropy "pool" by using timing 6591da177e4SLinus Torvalds * delays. It uses the timer_rand_state structure to make an estimate 6601da177e4SLinus Torvalds * of how many bits of entropy this call has added to the pool. 6611da177e4SLinus Torvalds * 6621da177e4SLinus Torvalds * The number "num" is also added to the pool - it should somehow describe 6631da177e4SLinus Torvalds * the type of event which just happened. This is currently 0-255 for 6641da177e4SLinus Torvalds * keyboard scan codes, and 256 upwards for interrupts. 6651da177e4SLinus Torvalds * 6661da177e4SLinus Torvalds */ 6671da177e4SLinus Torvalds static void add_timer_randomness(struct timer_rand_state *state, unsigned num) 6681da177e4SLinus Torvalds { 6691da177e4SLinus Torvalds struct { 6701da177e4SLinus Torvalds long jiffies; 671cf833d0bSLinus Torvalds unsigned cycles; 6721da177e4SLinus Torvalds unsigned num; 6731da177e4SLinus Torvalds } sample; 6741da177e4SLinus Torvalds long delta, delta2, delta3; 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds preempt_disable(); 6771da177e4SLinus Torvalds /* if over the trickle threshold, use only 1 in 4096 samples */ 6781da177e4SLinus Torvalds if (input_pool.entropy_count > trickle_thresh && 679b29c617aSChristoph Lameter ((__this_cpu_inc_return(trickle_count) - 1) & 0xfff)) 6801da177e4SLinus Torvalds goto out; 6811da177e4SLinus Torvalds 6821da177e4SLinus Torvalds sample.jiffies = jiffies; 683*61875f30STheodore Ts'o sample.cycles = random_get_entropy(); 6841da177e4SLinus Torvalds sample.num = num; 685902c098aSTheodore Ts'o mix_pool_bytes(&input_pool, &sample, sizeof(sample), NULL); 6861da177e4SLinus Torvalds 6871da177e4SLinus Torvalds /* 6881da177e4SLinus Torvalds * Calculate number of bits of randomness we probably added. 6891da177e4SLinus Torvalds * We take into account the first, second and third-order deltas 6901da177e4SLinus Torvalds * in order to make our estimate. 6911da177e4SLinus Torvalds */ 6921da177e4SLinus Torvalds 6931da177e4SLinus Torvalds if (!state->dont_count_entropy) { 6941da177e4SLinus Torvalds delta = sample.jiffies - state->last_time; 6951da177e4SLinus Torvalds state->last_time = sample.jiffies; 6961da177e4SLinus Torvalds 6971da177e4SLinus Torvalds delta2 = delta - state->last_delta; 6981da177e4SLinus Torvalds state->last_delta = delta; 6991da177e4SLinus Torvalds 7001da177e4SLinus Torvalds delta3 = delta2 - state->last_delta2; 7011da177e4SLinus Torvalds state->last_delta2 = delta2; 7021da177e4SLinus Torvalds 7031da177e4SLinus Torvalds if (delta < 0) 7041da177e4SLinus Torvalds delta = -delta; 7051da177e4SLinus Torvalds if (delta2 < 0) 7061da177e4SLinus Torvalds delta2 = -delta2; 7071da177e4SLinus Torvalds if (delta3 < 0) 7081da177e4SLinus Torvalds delta3 = -delta3; 7091da177e4SLinus Torvalds if (delta > delta2) 7101da177e4SLinus Torvalds delta = delta2; 7111da177e4SLinus Torvalds if (delta > delta3) 7121da177e4SLinus Torvalds delta = delta3; 7131da177e4SLinus Torvalds 7141da177e4SLinus Torvalds /* 7151da177e4SLinus Torvalds * delta is now minimum absolute delta. 7161da177e4SLinus Torvalds * Round down by 1 bit on general principles, 7171da177e4SLinus Torvalds * and limit entropy entimate to 12 bits. 7181da177e4SLinus Torvalds */ 719adc782daSMatt Mackall credit_entropy_bits(&input_pool, 7201da177e4SLinus Torvalds min_t(int, fls(delta>>1), 11)); 7211da177e4SLinus Torvalds } 7221da177e4SLinus Torvalds out: 7231da177e4SLinus Torvalds preempt_enable(); 7241da177e4SLinus Torvalds } 7251da177e4SLinus Torvalds 726d251575aSStephen Hemminger void add_input_randomness(unsigned int type, unsigned int code, 7271da177e4SLinus Torvalds unsigned int value) 7281da177e4SLinus Torvalds { 7291da177e4SLinus Torvalds static unsigned char last_value; 7301da177e4SLinus Torvalds 7311da177e4SLinus Torvalds /* ignore autorepeat and the like */ 7321da177e4SLinus Torvalds if (value == last_value) 7331da177e4SLinus Torvalds return; 7341da177e4SLinus Torvalds 7351da177e4SLinus Torvalds DEBUG_ENT("input event\n"); 7361da177e4SLinus Torvalds last_value = value; 7371da177e4SLinus Torvalds add_timer_randomness(&input_timer_state, 7381da177e4SLinus Torvalds (type << 4) ^ code ^ (code >> 4) ^ value); 7391da177e4SLinus Torvalds } 74080fc9f53SDmitry Torokhov EXPORT_SYMBOL_GPL(add_input_randomness); 7411da177e4SLinus Torvalds 742775f4b29STheodore Ts'o static DEFINE_PER_CPU(struct fast_pool, irq_randomness); 743775f4b29STheodore Ts'o 744775f4b29STheodore Ts'o void add_interrupt_randomness(int irq, int irq_flags) 7451da177e4SLinus Torvalds { 746775f4b29STheodore Ts'o struct entropy_store *r; 747775f4b29STheodore Ts'o struct fast_pool *fast_pool = &__get_cpu_var(irq_randomness); 748775f4b29STheodore Ts'o struct pt_regs *regs = get_irq_regs(); 749775f4b29STheodore Ts'o unsigned long now = jiffies; 750*61875f30STheodore Ts'o __u32 input[4], cycles = random_get_entropy(); 7513060d6feSYinghai Lu 752775f4b29STheodore Ts'o input[0] = cycles ^ jiffies; 753775f4b29STheodore Ts'o input[1] = irq; 754775f4b29STheodore Ts'o if (regs) { 755775f4b29STheodore Ts'o __u64 ip = instruction_pointer(regs); 756775f4b29STheodore Ts'o input[2] = ip; 757775f4b29STheodore Ts'o input[3] = ip >> 32; 758775f4b29STheodore Ts'o } 7593060d6feSYinghai Lu 760775f4b29STheodore Ts'o fast_mix(fast_pool, input, sizeof(input)); 761775f4b29STheodore Ts'o 762775f4b29STheodore Ts'o if ((fast_pool->count & 1023) && 763775f4b29STheodore Ts'o !time_after(now, fast_pool->last + HZ)) 7641da177e4SLinus Torvalds return; 7651da177e4SLinus Torvalds 766775f4b29STheodore Ts'o fast_pool->last = now; 767775f4b29STheodore Ts'o 768775f4b29STheodore Ts'o r = nonblocking_pool.initialized ? &input_pool : &nonblocking_pool; 769902c098aSTheodore Ts'o __mix_pool_bytes(r, &fast_pool->pool, sizeof(fast_pool->pool), NULL); 770775f4b29STheodore Ts'o /* 771775f4b29STheodore Ts'o * If we don't have a valid cycle counter, and we see 772775f4b29STheodore Ts'o * back-to-back timer interrupts, then skip giving credit for 773775f4b29STheodore Ts'o * any entropy. 774775f4b29STheodore Ts'o */ 775775f4b29STheodore Ts'o if (cycles == 0) { 776775f4b29STheodore Ts'o if (irq_flags & __IRQF_TIMER) { 777775f4b29STheodore Ts'o if (fast_pool->last_timer_intr) 778775f4b29STheodore Ts'o return; 779775f4b29STheodore Ts'o fast_pool->last_timer_intr = 1; 780775f4b29STheodore Ts'o } else 781775f4b29STheodore Ts'o fast_pool->last_timer_intr = 0; 782775f4b29STheodore Ts'o } 783775f4b29STheodore Ts'o credit_entropy_bits(r, 1); 7841da177e4SLinus Torvalds } 7851da177e4SLinus Torvalds 7869361401eSDavid Howells #ifdef CONFIG_BLOCK 7871da177e4SLinus Torvalds void add_disk_randomness(struct gendisk *disk) 7881da177e4SLinus Torvalds { 7891da177e4SLinus Torvalds if (!disk || !disk->random) 7901da177e4SLinus Torvalds return; 7911da177e4SLinus Torvalds /* first major is 1, so we get >= 0x200 here */ 792f331c029STejun Heo DEBUG_ENT("disk event %d:%d\n", 793f331c029STejun Heo MAJOR(disk_devt(disk)), MINOR(disk_devt(disk))); 7941da177e4SLinus Torvalds 795f331c029STejun Heo add_timer_randomness(disk->random, 0x100 + disk_devt(disk)); 7961da177e4SLinus Torvalds } 7979361401eSDavid Howells #endif 7981da177e4SLinus Torvalds 7991da177e4SLinus Torvalds /********************************************************************* 8001da177e4SLinus Torvalds * 8011da177e4SLinus Torvalds * Entropy extraction routines 8021da177e4SLinus Torvalds * 8031da177e4SLinus Torvalds *********************************************************************/ 8041da177e4SLinus Torvalds 8051da177e4SLinus Torvalds static ssize_t extract_entropy(struct entropy_store *r, void *buf, 8061da177e4SLinus Torvalds size_t nbytes, int min, int rsvd); 8071da177e4SLinus Torvalds 8081da177e4SLinus Torvalds /* 80925985edcSLucas De Marchi * This utility inline function is responsible for transferring entropy 8101da177e4SLinus Torvalds * from the primary pool to the secondary extraction pool. We make 8111da177e4SLinus Torvalds * sure we pull enough for a 'catastrophic reseed'. 8121da177e4SLinus Torvalds */ 8131da177e4SLinus Torvalds static void xfer_secondary_pool(struct entropy_store *r, size_t nbytes) 8141da177e4SLinus Torvalds { 8151da177e4SLinus Torvalds __u32 tmp[OUTPUT_POOL_WORDS]; 8161da177e4SLinus Torvalds 8171da177e4SLinus Torvalds if (r->pull && r->entropy_count < nbytes * 8 && 8181da177e4SLinus Torvalds r->entropy_count < r->poolinfo->POOLBITS) { 8195a021e9fSMatt Mackall /* If we're limited, always leave two wakeup worth's BITS */ 8201da177e4SLinus Torvalds int rsvd = r->limit ? 0 : random_read_wakeup_thresh/4; 8215a021e9fSMatt Mackall int bytes = nbytes; 8225a021e9fSMatt Mackall 8235a021e9fSMatt Mackall /* pull at least as many as BYTES as wakeup BITS */ 8245a021e9fSMatt Mackall bytes = max_t(int, bytes, random_read_wakeup_thresh / 8); 8255a021e9fSMatt Mackall /* but never more than the buffer size */ 826d2e7c96aSH. Peter Anvin bytes = min_t(int, bytes, sizeof(tmp)); 8271da177e4SLinus Torvalds 8281da177e4SLinus Torvalds DEBUG_ENT("going to reseed %s with %d bits " 8298eb2ffbfSJiri Kosina "(%zu of %d requested)\n", 8301da177e4SLinus Torvalds r->name, bytes * 8, nbytes * 8, r->entropy_count); 8311da177e4SLinus Torvalds 832d2e7c96aSH. Peter Anvin bytes = extract_entropy(r->pull, tmp, bytes, 8331da177e4SLinus Torvalds random_read_wakeup_thresh / 8, rsvd); 834d2e7c96aSH. Peter Anvin mix_pool_bytes(r, tmp, bytes, NULL); 835adc782daSMatt Mackall credit_entropy_bits(r, bytes*8); 8361da177e4SLinus Torvalds } 8371da177e4SLinus Torvalds } 8381da177e4SLinus Torvalds 8391da177e4SLinus Torvalds /* 8401da177e4SLinus Torvalds * These functions extracts randomness from the "entropy pool", and 8411da177e4SLinus Torvalds * returns it in a buffer. 8421da177e4SLinus Torvalds * 8431da177e4SLinus Torvalds * The min parameter specifies the minimum amount we can pull before 8441da177e4SLinus Torvalds * failing to avoid races that defeat catastrophic reseeding while the 8451da177e4SLinus Torvalds * reserved parameter indicates how much entropy we must leave in the 8461da177e4SLinus Torvalds * pool after each pull to avoid starving other readers. 8471da177e4SLinus Torvalds * 8481da177e4SLinus Torvalds * Note: extract_entropy() assumes that .poolwords is a multiple of 16 words. 8491da177e4SLinus Torvalds */ 8501da177e4SLinus Torvalds 8511da177e4SLinus Torvalds static size_t account(struct entropy_store *r, size_t nbytes, int min, 8521da177e4SLinus Torvalds int reserved) 8531da177e4SLinus Torvalds { 8541da177e4SLinus Torvalds unsigned long flags; 855b9809552STheodore Ts'o int wakeup_write = 0; 8561da177e4SLinus Torvalds 8571da177e4SLinus Torvalds /* Hold lock while accounting */ 8581da177e4SLinus Torvalds spin_lock_irqsave(&r->lock, flags); 8591da177e4SLinus Torvalds 860cda796a3SMatt Mackall BUG_ON(r->entropy_count > r->poolinfo->POOLBITS); 8618eb2ffbfSJiri Kosina DEBUG_ENT("trying to extract %zu bits from %s\n", 8621da177e4SLinus Torvalds nbytes * 8, r->name); 8631da177e4SLinus Torvalds 8641da177e4SLinus Torvalds /* Can we pull enough? */ 8651da177e4SLinus Torvalds if (r->entropy_count / 8 < min + reserved) { 8661da177e4SLinus Torvalds nbytes = 0; 8671da177e4SLinus Torvalds } else { 86810b3a32dSJiri Kosina int entropy_count, orig; 86910b3a32dSJiri Kosina retry: 87010b3a32dSJiri Kosina entropy_count = orig = ACCESS_ONCE(r->entropy_count); 8711da177e4SLinus Torvalds /* If limited, never pull more than available */ 87210b3a32dSJiri Kosina if (r->limit && nbytes + reserved >= entropy_count / 8) 87310b3a32dSJiri Kosina nbytes = entropy_count/8 - reserved; 8741da177e4SLinus Torvalds 87510b3a32dSJiri Kosina if (entropy_count / 8 >= nbytes + reserved) { 87610b3a32dSJiri Kosina entropy_count -= nbytes*8; 87710b3a32dSJiri Kosina if (cmpxchg(&r->entropy_count, orig, entropy_count) != orig) 87810b3a32dSJiri Kosina goto retry; 87910b3a32dSJiri Kosina } else { 88010b3a32dSJiri Kosina entropy_count = reserved; 88110b3a32dSJiri Kosina if (cmpxchg(&r->entropy_count, orig, entropy_count) != orig) 88210b3a32dSJiri Kosina goto retry; 88310b3a32dSJiri Kosina } 8841da177e4SLinus Torvalds 88510b3a32dSJiri Kosina if (entropy_count < random_write_wakeup_thresh) 886b9809552STheodore Ts'o wakeup_write = 1; 8871da177e4SLinus Torvalds } 8881da177e4SLinus Torvalds 8898eb2ffbfSJiri Kosina DEBUG_ENT("debiting %zu entropy credits from %s%s\n", 8901da177e4SLinus Torvalds nbytes * 8, r->name, r->limit ? "" : " (unlimited)"); 8911da177e4SLinus Torvalds 8921da177e4SLinus Torvalds spin_unlock_irqrestore(&r->lock, flags); 8931da177e4SLinus Torvalds 894b9809552STheodore Ts'o if (wakeup_write) { 895b9809552STheodore Ts'o wake_up_interruptible(&random_write_wait); 896b9809552STheodore Ts'o kill_fasync(&fasync, SIGIO, POLL_OUT); 897b9809552STheodore Ts'o } 898b9809552STheodore Ts'o 8991da177e4SLinus Torvalds return nbytes; 9001da177e4SLinus Torvalds } 9011da177e4SLinus Torvalds 9021da177e4SLinus Torvalds static void extract_buf(struct entropy_store *r, __u8 *out) 9031da177e4SLinus Torvalds { 904602b6aeeSMatt Mackall int i; 905d2e7c96aSH. Peter Anvin union { 906d2e7c96aSH. Peter Anvin __u32 w[5]; 907d2e7c96aSH. Peter Anvin unsigned long l[LONGS(EXTRACT_SIZE)]; 908d2e7c96aSH. Peter Anvin } hash; 909d2e7c96aSH. Peter Anvin __u32 workspace[SHA_WORKSPACE_WORDS]; 910e68e5b66SMatt Mackall __u8 extract[64]; 911902c098aSTheodore Ts'o unsigned long flags; 9121da177e4SLinus Torvalds 9131c0ad3d4SMatt Mackall /* Generate a hash across the pool, 16 words (512 bits) at a time */ 914d2e7c96aSH. Peter Anvin sha_init(hash.w); 915902c098aSTheodore Ts'o spin_lock_irqsave(&r->lock, flags); 9161c0ad3d4SMatt Mackall for (i = 0; i < r->poolinfo->poolwords; i += 16) 917d2e7c96aSH. Peter Anvin sha_transform(hash.w, (__u8 *)(r->pool + i), workspace); 9181da177e4SLinus Torvalds 9191da177e4SLinus Torvalds /* 9201c0ad3d4SMatt Mackall * We mix the hash back into the pool to prevent backtracking 9211c0ad3d4SMatt Mackall * attacks (where the attacker knows the state of the pool 9221c0ad3d4SMatt Mackall * plus the current outputs, and attempts to find previous 9231c0ad3d4SMatt Mackall * ouputs), unless the hash function can be inverted. By 9241c0ad3d4SMatt Mackall * mixing at least a SHA1 worth of hash data back, we make 9251c0ad3d4SMatt Mackall * brute-forcing the feedback as hard as brute-forcing the 9261c0ad3d4SMatt Mackall * hash. 9271da177e4SLinus Torvalds */ 928d2e7c96aSH. Peter Anvin __mix_pool_bytes(r, hash.w, sizeof(hash.w), extract); 929902c098aSTheodore Ts'o spin_unlock_irqrestore(&r->lock, flags); 9301c0ad3d4SMatt Mackall 9311c0ad3d4SMatt Mackall /* 9321c0ad3d4SMatt Mackall * To avoid duplicates, we atomically extract a portion of the 9331c0ad3d4SMatt Mackall * pool while mixing, and hash one final time. 9341c0ad3d4SMatt Mackall */ 935d2e7c96aSH. Peter Anvin sha_transform(hash.w, extract, workspace); 936ffd8d3faSMatt Mackall memset(extract, 0, sizeof(extract)); 937ffd8d3faSMatt Mackall memset(workspace, 0, sizeof(workspace)); 9381da177e4SLinus Torvalds 9391da177e4SLinus Torvalds /* 9401c0ad3d4SMatt Mackall * In case the hash function has some recognizable output 9411c0ad3d4SMatt Mackall * pattern, we fold it in half. Thus, we always feed back 9421c0ad3d4SMatt Mackall * twice as much data as we output. 9431da177e4SLinus Torvalds */ 944d2e7c96aSH. Peter Anvin hash.w[0] ^= hash.w[3]; 945d2e7c96aSH. Peter Anvin hash.w[1] ^= hash.w[4]; 946d2e7c96aSH. Peter Anvin hash.w[2] ^= rol32(hash.w[2], 16); 947d2e7c96aSH. Peter Anvin 948d2e7c96aSH. Peter Anvin /* 949d2e7c96aSH. Peter Anvin * If we have a architectural hardware random number 950d2e7c96aSH. Peter Anvin * generator, mix that in, too. 951d2e7c96aSH. Peter Anvin */ 952d2e7c96aSH. Peter Anvin for (i = 0; i < LONGS(EXTRACT_SIZE); i++) { 953d2e7c96aSH. Peter Anvin unsigned long v; 954d2e7c96aSH. Peter Anvin if (!arch_get_random_long(&v)) 955d2e7c96aSH. Peter Anvin break; 956d2e7c96aSH. Peter Anvin hash.l[i] ^= v; 957d2e7c96aSH. Peter Anvin } 958d2e7c96aSH. Peter Anvin 959d2e7c96aSH. Peter Anvin memcpy(out, &hash, EXTRACT_SIZE); 960d2e7c96aSH. Peter Anvin memset(&hash, 0, sizeof(hash)); 9611da177e4SLinus Torvalds } 9621da177e4SLinus Torvalds 9631da177e4SLinus Torvalds static ssize_t extract_entropy(struct entropy_store *r, void *buf, 9641da177e4SLinus Torvalds size_t nbytes, int min, int reserved) 9651da177e4SLinus Torvalds { 9661da177e4SLinus Torvalds ssize_t ret = 0, i; 9671da177e4SLinus Torvalds __u8 tmp[EXTRACT_SIZE]; 9681e7e2e05SJarod Wilson unsigned long flags; 9691da177e4SLinus Torvalds 970ec8f02daSJarod Wilson /* if last_data isn't primed, we need EXTRACT_SIZE extra bytes */ 9711e7e2e05SJarod Wilson if (fips_enabled) { 9721e7e2e05SJarod Wilson spin_lock_irqsave(&r->lock, flags); 9731e7e2e05SJarod Wilson if (!r->last_data_init) { 9741e7e2e05SJarod Wilson r->last_data_init = true; 9751e7e2e05SJarod Wilson spin_unlock_irqrestore(&r->lock, flags); 9761e7e2e05SJarod Wilson trace_extract_entropy(r->name, EXTRACT_SIZE, 9771e7e2e05SJarod Wilson r->entropy_count, _RET_IP_); 9781e7e2e05SJarod Wilson xfer_secondary_pool(r, EXTRACT_SIZE); 9791e7e2e05SJarod Wilson extract_buf(r, tmp); 9801e7e2e05SJarod Wilson spin_lock_irqsave(&r->lock, flags); 9811e7e2e05SJarod Wilson memcpy(r->last_data, tmp, EXTRACT_SIZE); 9821e7e2e05SJarod Wilson } 9831e7e2e05SJarod Wilson spin_unlock_irqrestore(&r->lock, flags); 9841e7e2e05SJarod Wilson } 985ec8f02daSJarod Wilson 98600ce1db1STheodore Ts'o trace_extract_entropy(r->name, nbytes, r->entropy_count, _RET_IP_); 9871da177e4SLinus Torvalds xfer_secondary_pool(r, nbytes); 9881da177e4SLinus Torvalds nbytes = account(r, nbytes, min, reserved); 9891da177e4SLinus Torvalds 9901da177e4SLinus Torvalds while (nbytes) { 9911da177e4SLinus Torvalds extract_buf(r, tmp); 9925b739ef8SNeil Horman 993e954bc91SMatt Mackall if (fips_enabled) { 9945b739ef8SNeil Horman spin_lock_irqsave(&r->lock, flags); 9955b739ef8SNeil Horman if (!memcmp(tmp, r->last_data, EXTRACT_SIZE)) 9965b739ef8SNeil Horman panic("Hardware RNG duplicated output!\n"); 9975b739ef8SNeil Horman memcpy(r->last_data, tmp, EXTRACT_SIZE); 9985b739ef8SNeil Horman spin_unlock_irqrestore(&r->lock, flags); 9995b739ef8SNeil Horman } 10001da177e4SLinus Torvalds i = min_t(int, nbytes, EXTRACT_SIZE); 10011da177e4SLinus Torvalds memcpy(buf, tmp, i); 10021da177e4SLinus Torvalds nbytes -= i; 10031da177e4SLinus Torvalds buf += i; 10041da177e4SLinus Torvalds ret += i; 10051da177e4SLinus Torvalds } 10061da177e4SLinus Torvalds 10071da177e4SLinus Torvalds /* Wipe data just returned from memory */ 10081da177e4SLinus Torvalds memset(tmp, 0, sizeof(tmp)); 10091da177e4SLinus Torvalds 10101da177e4SLinus Torvalds return ret; 10111da177e4SLinus Torvalds } 10121da177e4SLinus Torvalds 10131da177e4SLinus Torvalds static ssize_t extract_entropy_user(struct entropy_store *r, void __user *buf, 10141da177e4SLinus Torvalds size_t nbytes) 10151da177e4SLinus Torvalds { 10161da177e4SLinus Torvalds ssize_t ret = 0, i; 10171da177e4SLinus Torvalds __u8 tmp[EXTRACT_SIZE]; 10181da177e4SLinus Torvalds 101900ce1db1STheodore Ts'o trace_extract_entropy_user(r->name, nbytes, r->entropy_count, _RET_IP_); 10201da177e4SLinus Torvalds xfer_secondary_pool(r, nbytes); 10211da177e4SLinus Torvalds nbytes = account(r, nbytes, 0, 0); 10221da177e4SLinus Torvalds 10231da177e4SLinus Torvalds while (nbytes) { 10241da177e4SLinus Torvalds if (need_resched()) { 10251da177e4SLinus Torvalds if (signal_pending(current)) { 10261da177e4SLinus Torvalds if (ret == 0) 10271da177e4SLinus Torvalds ret = -ERESTARTSYS; 10281da177e4SLinus Torvalds break; 10291da177e4SLinus Torvalds } 10301da177e4SLinus Torvalds schedule(); 10311da177e4SLinus Torvalds } 10321da177e4SLinus Torvalds 10331da177e4SLinus Torvalds extract_buf(r, tmp); 10341da177e4SLinus Torvalds i = min_t(int, nbytes, EXTRACT_SIZE); 10351da177e4SLinus Torvalds if (copy_to_user(buf, tmp, i)) { 10361da177e4SLinus Torvalds ret = -EFAULT; 10371da177e4SLinus Torvalds break; 10381da177e4SLinus Torvalds } 10391da177e4SLinus Torvalds 10401da177e4SLinus Torvalds nbytes -= i; 10411da177e4SLinus Torvalds buf += i; 10421da177e4SLinus Torvalds ret += i; 10431da177e4SLinus Torvalds } 10441da177e4SLinus Torvalds 10451da177e4SLinus Torvalds /* Wipe data just returned from memory */ 10461da177e4SLinus Torvalds memset(tmp, 0, sizeof(tmp)); 10471da177e4SLinus Torvalds 10481da177e4SLinus Torvalds return ret; 10491da177e4SLinus Torvalds } 10501da177e4SLinus Torvalds 10511da177e4SLinus Torvalds /* 10521da177e4SLinus Torvalds * This function is the exported kernel interface. It returns some 1053c2557a30STheodore Ts'o * number of good random numbers, suitable for key generation, seeding 1054c2557a30STheodore Ts'o * TCP sequence numbers, etc. It does not use the hw random number 1055c2557a30STheodore Ts'o * generator, if available; use get_random_bytes_arch() for that. 10561da177e4SLinus Torvalds */ 10571da177e4SLinus Torvalds void get_random_bytes(void *buf, int nbytes) 10581da177e4SLinus Torvalds { 1059c2557a30STheodore Ts'o extract_entropy(&nonblocking_pool, buf, nbytes, 0, 0); 1060c2557a30STheodore Ts'o } 1061c2557a30STheodore Ts'o EXPORT_SYMBOL(get_random_bytes); 1062c2557a30STheodore Ts'o 1063c2557a30STheodore Ts'o /* 1064c2557a30STheodore Ts'o * This function will use the architecture-specific hardware random 1065c2557a30STheodore Ts'o * number generator if it is available. The arch-specific hw RNG will 1066c2557a30STheodore Ts'o * almost certainly be faster than what we can do in software, but it 1067c2557a30STheodore Ts'o * is impossible to verify that it is implemented securely (as 1068c2557a30STheodore Ts'o * opposed, to, say, the AES encryption of a sequence number using a 1069c2557a30STheodore Ts'o * key known by the NSA). So it's useful if we need the speed, but 1070c2557a30STheodore Ts'o * only if we're willing to trust the hardware manufacturer not to 1071c2557a30STheodore Ts'o * have put in a back door. 1072c2557a30STheodore Ts'o */ 1073c2557a30STheodore Ts'o void get_random_bytes_arch(void *buf, int nbytes) 1074c2557a30STheodore Ts'o { 107563d77173SH. Peter Anvin char *p = buf; 107663d77173SH. Peter Anvin 107700ce1db1STheodore Ts'o trace_get_random_bytes(nbytes, _RET_IP_); 107863d77173SH. Peter Anvin while (nbytes) { 107963d77173SH. Peter Anvin unsigned long v; 108063d77173SH. Peter Anvin int chunk = min(nbytes, (int)sizeof(unsigned long)); 108163d77173SH. Peter Anvin 108263d77173SH. Peter Anvin if (!arch_get_random_long(&v)) 108363d77173SH. Peter Anvin break; 108463d77173SH. Peter Anvin 1085bd29e568SLuck, Tony memcpy(p, &v, chunk); 108663d77173SH. Peter Anvin p += chunk; 108763d77173SH. Peter Anvin nbytes -= chunk; 108863d77173SH. Peter Anvin } 108963d77173SH. Peter Anvin 1090c2557a30STheodore Ts'o if (nbytes) 109163d77173SH. Peter Anvin extract_entropy(&nonblocking_pool, p, nbytes, 0, 0); 10921da177e4SLinus Torvalds } 1093c2557a30STheodore Ts'o EXPORT_SYMBOL(get_random_bytes_arch); 1094c2557a30STheodore Ts'o 10951da177e4SLinus Torvalds 10961da177e4SLinus Torvalds /* 10971da177e4SLinus Torvalds * init_std_data - initialize pool with system data 10981da177e4SLinus Torvalds * 10991da177e4SLinus Torvalds * @r: pool to initialize 11001da177e4SLinus Torvalds * 11011da177e4SLinus Torvalds * This function clears the pool's entropy count and mixes some system 11021da177e4SLinus Torvalds * data into the pool to prepare it for use. The pool is not cleared 11031da177e4SLinus Torvalds * as that can only decrease the entropy in the pool. 11041da177e4SLinus Torvalds */ 11051da177e4SLinus Torvalds static void init_std_data(struct entropy_store *r) 11061da177e4SLinus Torvalds { 11073e88bdffSTheodore Ts'o int i; 1108902c098aSTheodore Ts'o ktime_t now = ktime_get_real(); 1109902c098aSTheodore Ts'o unsigned long rv; 11101da177e4SLinus Torvalds 11111da177e4SLinus Torvalds r->entropy_count = 0; 1112775f4b29STheodore Ts'o r->entropy_total = 0; 1113ec8f02daSJarod Wilson r->last_data_init = false; 1114902c098aSTheodore Ts'o mix_pool_bytes(r, &now, sizeof(now), NULL); 1115902c098aSTheodore Ts'o for (i = r->poolinfo->POOLBYTES; i > 0; i -= sizeof(rv)) { 1116902c098aSTheodore Ts'o if (!arch_get_random_long(&rv)) 11173e88bdffSTheodore Ts'o break; 1118902c098aSTheodore Ts'o mix_pool_bytes(r, &rv, sizeof(rv), NULL); 11193e88bdffSTheodore Ts'o } 1120902c098aSTheodore Ts'o mix_pool_bytes(r, utsname(), sizeof(*(utsname())), NULL); 11211da177e4SLinus Torvalds } 11221da177e4SLinus Torvalds 1123cbc96b75STony Luck /* 1124cbc96b75STony Luck * Note that setup_arch() may call add_device_randomness() 1125cbc96b75STony Luck * long before we get here. This allows seeding of the pools 1126cbc96b75STony Luck * with some platform dependent data very early in the boot 1127cbc96b75STony Luck * process. But it limits our options here. We must use 1128cbc96b75STony Luck * statically allocated structures that already have all 1129cbc96b75STony Luck * initializations complete at compile time. We should also 1130cbc96b75STony Luck * take care not to overwrite the precious per platform data 1131cbc96b75STony Luck * we were given. 1132cbc96b75STony Luck */ 113353c3f63eSMatt Mackall static int rand_initialize(void) 11341da177e4SLinus Torvalds { 11351da177e4SLinus Torvalds init_std_data(&input_pool); 11361da177e4SLinus Torvalds init_std_data(&blocking_pool); 11371da177e4SLinus Torvalds init_std_data(&nonblocking_pool); 11381da177e4SLinus Torvalds return 0; 11391da177e4SLinus Torvalds } 11401da177e4SLinus Torvalds module_init(rand_initialize); 11411da177e4SLinus Torvalds 11429361401eSDavid Howells #ifdef CONFIG_BLOCK 11431da177e4SLinus Torvalds void rand_initialize_disk(struct gendisk *disk) 11441da177e4SLinus Torvalds { 11451da177e4SLinus Torvalds struct timer_rand_state *state; 11461da177e4SLinus Torvalds 11471da177e4SLinus Torvalds /* 1148f8595815SEric Dumazet * If kzalloc returns null, we just won't use that entropy 11491da177e4SLinus Torvalds * source. 11501da177e4SLinus Torvalds */ 1151f8595815SEric Dumazet state = kzalloc(sizeof(struct timer_rand_state), GFP_KERNEL); 1152f8595815SEric Dumazet if (state) 11531da177e4SLinus Torvalds disk->random = state; 11541da177e4SLinus Torvalds } 11559361401eSDavid Howells #endif 11561da177e4SLinus Torvalds 11571da177e4SLinus Torvalds static ssize_t 11581da177e4SLinus Torvalds random_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos) 11591da177e4SLinus Torvalds { 11601da177e4SLinus Torvalds ssize_t n, retval = 0, count = 0; 11611da177e4SLinus Torvalds 11621da177e4SLinus Torvalds if (nbytes == 0) 11631da177e4SLinus Torvalds return 0; 11641da177e4SLinus Torvalds 11651da177e4SLinus Torvalds while (nbytes > 0) { 11661da177e4SLinus Torvalds n = nbytes; 11671da177e4SLinus Torvalds if (n > SEC_XFER_SIZE) 11681da177e4SLinus Torvalds n = SEC_XFER_SIZE; 11691da177e4SLinus Torvalds 11708eb2ffbfSJiri Kosina DEBUG_ENT("reading %zu bits\n", n*8); 11711da177e4SLinus Torvalds 11721da177e4SLinus Torvalds n = extract_entropy_user(&blocking_pool, buf, n); 11731da177e4SLinus Torvalds 11748eb2ffbfSJiri Kosina if (n < 0) { 11758eb2ffbfSJiri Kosina retval = n; 11768eb2ffbfSJiri Kosina break; 11778eb2ffbfSJiri Kosina } 11788eb2ffbfSJiri Kosina 11798eb2ffbfSJiri Kosina DEBUG_ENT("read got %zd bits (%zd still needed)\n", 11801da177e4SLinus Torvalds n*8, (nbytes-n)*8); 11811da177e4SLinus Torvalds 11821da177e4SLinus Torvalds if (n == 0) { 11831da177e4SLinus Torvalds if (file->f_flags & O_NONBLOCK) { 11841da177e4SLinus Torvalds retval = -EAGAIN; 11851da177e4SLinus Torvalds break; 11861da177e4SLinus Torvalds } 11871da177e4SLinus Torvalds 11881da177e4SLinus Torvalds DEBUG_ENT("sleeping?\n"); 11891da177e4SLinus Torvalds 11901da177e4SLinus Torvalds wait_event_interruptible(random_read_wait, 11911da177e4SLinus Torvalds input_pool.entropy_count >= 11921da177e4SLinus Torvalds random_read_wakeup_thresh); 11931da177e4SLinus Torvalds 11941da177e4SLinus Torvalds DEBUG_ENT("awake\n"); 11951da177e4SLinus Torvalds 11961da177e4SLinus Torvalds if (signal_pending(current)) { 11971da177e4SLinus Torvalds retval = -ERESTARTSYS; 11981da177e4SLinus Torvalds break; 11991da177e4SLinus Torvalds } 12001da177e4SLinus Torvalds 12011da177e4SLinus Torvalds continue; 12021da177e4SLinus Torvalds } 12031da177e4SLinus Torvalds 12041da177e4SLinus Torvalds count += n; 12051da177e4SLinus Torvalds buf += n; 12061da177e4SLinus Torvalds nbytes -= n; 12071da177e4SLinus Torvalds break; /* This break makes the device work */ 12081da177e4SLinus Torvalds /* like a named pipe */ 12091da177e4SLinus Torvalds } 12101da177e4SLinus Torvalds 12111da177e4SLinus Torvalds return (count ? count : retval); 12121da177e4SLinus Torvalds } 12131da177e4SLinus Torvalds 12141da177e4SLinus Torvalds static ssize_t 121590b75ee5SMatt Mackall urandom_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos) 12161da177e4SLinus Torvalds { 12171da177e4SLinus Torvalds return extract_entropy_user(&nonblocking_pool, buf, nbytes); 12181da177e4SLinus Torvalds } 12191da177e4SLinus Torvalds 12201da177e4SLinus Torvalds static unsigned int 12211da177e4SLinus Torvalds random_poll(struct file *file, poll_table * wait) 12221da177e4SLinus Torvalds { 12231da177e4SLinus Torvalds unsigned int mask; 12241da177e4SLinus Torvalds 12251da177e4SLinus Torvalds poll_wait(file, &random_read_wait, wait); 12261da177e4SLinus Torvalds poll_wait(file, &random_write_wait, wait); 12271da177e4SLinus Torvalds mask = 0; 12281da177e4SLinus Torvalds if (input_pool.entropy_count >= random_read_wakeup_thresh) 12291da177e4SLinus Torvalds mask |= POLLIN | POLLRDNORM; 12301da177e4SLinus Torvalds if (input_pool.entropy_count < random_write_wakeup_thresh) 12311da177e4SLinus Torvalds mask |= POLLOUT | POLLWRNORM; 12321da177e4SLinus Torvalds return mask; 12331da177e4SLinus Torvalds } 12341da177e4SLinus Torvalds 12357f397dcdSMatt Mackall static int 12367f397dcdSMatt Mackall write_pool(struct entropy_store *r, const char __user *buffer, size_t count) 12377f397dcdSMatt Mackall { 12387f397dcdSMatt Mackall size_t bytes; 12397f397dcdSMatt Mackall __u32 buf[16]; 12407f397dcdSMatt Mackall const char __user *p = buffer; 12417f397dcdSMatt Mackall 12427f397dcdSMatt Mackall while (count > 0) { 12437f397dcdSMatt Mackall bytes = min(count, sizeof(buf)); 12447f397dcdSMatt Mackall if (copy_from_user(&buf, p, bytes)) 12457f397dcdSMatt Mackall return -EFAULT; 12467f397dcdSMatt Mackall 12477f397dcdSMatt Mackall count -= bytes; 12487f397dcdSMatt Mackall p += bytes; 12497f397dcdSMatt Mackall 1250902c098aSTheodore Ts'o mix_pool_bytes(r, buf, bytes, NULL); 125191f3f1e3SMatt Mackall cond_resched(); 12527f397dcdSMatt Mackall } 12537f397dcdSMatt Mackall 12547f397dcdSMatt Mackall return 0; 12557f397dcdSMatt Mackall } 12567f397dcdSMatt Mackall 125790b75ee5SMatt Mackall static ssize_t random_write(struct file *file, const char __user *buffer, 12581da177e4SLinus Torvalds size_t count, loff_t *ppos) 12591da177e4SLinus Torvalds { 12607f397dcdSMatt Mackall size_t ret; 12617f397dcdSMatt Mackall 12627f397dcdSMatt Mackall ret = write_pool(&blocking_pool, buffer, count); 12637f397dcdSMatt Mackall if (ret) 12647f397dcdSMatt Mackall return ret; 12657f397dcdSMatt Mackall ret = write_pool(&nonblocking_pool, buffer, count); 12667f397dcdSMatt Mackall if (ret) 12677f397dcdSMatt Mackall return ret; 12687f397dcdSMatt Mackall 12697f397dcdSMatt Mackall return (ssize_t)count; 12701da177e4SLinus Torvalds } 12711da177e4SLinus Torvalds 127243ae4860SMatt Mackall static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg) 12731da177e4SLinus Torvalds { 12741da177e4SLinus Torvalds int size, ent_count; 12751da177e4SLinus Torvalds int __user *p = (int __user *)arg; 12761da177e4SLinus Torvalds int retval; 12771da177e4SLinus Torvalds 12781da177e4SLinus Torvalds switch (cmd) { 12791da177e4SLinus Torvalds case RNDGETENTCNT: 128043ae4860SMatt Mackall /* inherently racy, no point locking */ 128143ae4860SMatt Mackall if (put_user(input_pool.entropy_count, p)) 12821da177e4SLinus Torvalds return -EFAULT; 12831da177e4SLinus Torvalds return 0; 12841da177e4SLinus Torvalds case RNDADDTOENTCNT: 12851da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 12861da177e4SLinus Torvalds return -EPERM; 12871da177e4SLinus Torvalds if (get_user(ent_count, p)) 12881da177e4SLinus Torvalds return -EFAULT; 1289adc782daSMatt Mackall credit_entropy_bits(&input_pool, ent_count); 12901da177e4SLinus Torvalds return 0; 12911da177e4SLinus Torvalds case RNDADDENTROPY: 12921da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 12931da177e4SLinus Torvalds return -EPERM; 12941da177e4SLinus Torvalds if (get_user(ent_count, p++)) 12951da177e4SLinus Torvalds return -EFAULT; 12961da177e4SLinus Torvalds if (ent_count < 0) 12971da177e4SLinus Torvalds return -EINVAL; 12981da177e4SLinus Torvalds if (get_user(size, p++)) 12991da177e4SLinus Torvalds return -EFAULT; 13007f397dcdSMatt Mackall retval = write_pool(&input_pool, (const char __user *)p, 13017f397dcdSMatt Mackall size); 13021da177e4SLinus Torvalds if (retval < 0) 13031da177e4SLinus Torvalds return retval; 1304adc782daSMatt Mackall credit_entropy_bits(&input_pool, ent_count); 13051da177e4SLinus Torvalds return 0; 13061da177e4SLinus Torvalds case RNDZAPENTCNT: 13071da177e4SLinus Torvalds case RNDCLEARPOOL: 13081da177e4SLinus Torvalds /* Clear the entropy pool counters. */ 13091da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 13101da177e4SLinus Torvalds return -EPERM; 131153c3f63eSMatt Mackall rand_initialize(); 13121da177e4SLinus Torvalds return 0; 13131da177e4SLinus Torvalds default: 13141da177e4SLinus Torvalds return -EINVAL; 13151da177e4SLinus Torvalds } 13161da177e4SLinus Torvalds } 13171da177e4SLinus Torvalds 13189a6f70bbSJeff Dike static int random_fasync(int fd, struct file *filp, int on) 13199a6f70bbSJeff Dike { 13209a6f70bbSJeff Dike return fasync_helper(fd, filp, on, &fasync); 13219a6f70bbSJeff Dike } 13229a6f70bbSJeff Dike 13232b8693c0SArjan van de Ven const struct file_operations random_fops = { 13241da177e4SLinus Torvalds .read = random_read, 13251da177e4SLinus Torvalds .write = random_write, 13261da177e4SLinus Torvalds .poll = random_poll, 132743ae4860SMatt Mackall .unlocked_ioctl = random_ioctl, 13289a6f70bbSJeff Dike .fasync = random_fasync, 13296038f373SArnd Bergmann .llseek = noop_llseek, 13301da177e4SLinus Torvalds }; 13311da177e4SLinus Torvalds 13322b8693c0SArjan van de Ven const struct file_operations urandom_fops = { 13331da177e4SLinus Torvalds .read = urandom_read, 13341da177e4SLinus Torvalds .write = random_write, 133543ae4860SMatt Mackall .unlocked_ioctl = random_ioctl, 13369a6f70bbSJeff Dike .fasync = random_fasync, 13376038f373SArnd Bergmann .llseek = noop_llseek, 13381da177e4SLinus Torvalds }; 13391da177e4SLinus Torvalds 13401da177e4SLinus Torvalds /*************************************************************** 13411da177e4SLinus Torvalds * Random UUID interface 13421da177e4SLinus Torvalds * 13431da177e4SLinus Torvalds * Used here for a Boot ID, but can be useful for other kernel 13441da177e4SLinus Torvalds * drivers. 13451da177e4SLinus Torvalds ***************************************************************/ 13461da177e4SLinus Torvalds 13471da177e4SLinus Torvalds /* 13481da177e4SLinus Torvalds * Generate random UUID 13491da177e4SLinus Torvalds */ 13501da177e4SLinus Torvalds void generate_random_uuid(unsigned char uuid_out[16]) 13511da177e4SLinus Torvalds { 13521da177e4SLinus Torvalds get_random_bytes(uuid_out, 16); 1353c41b20e7SAdam Buchbinder /* Set UUID version to 4 --- truly random generation */ 13541da177e4SLinus Torvalds uuid_out[6] = (uuid_out[6] & 0x0F) | 0x40; 13551da177e4SLinus Torvalds /* Set the UUID variant to DCE */ 13561da177e4SLinus Torvalds uuid_out[8] = (uuid_out[8] & 0x3F) | 0x80; 13571da177e4SLinus Torvalds } 13581da177e4SLinus Torvalds EXPORT_SYMBOL(generate_random_uuid); 13591da177e4SLinus Torvalds 13601da177e4SLinus Torvalds /******************************************************************** 13611da177e4SLinus Torvalds * 13621da177e4SLinus Torvalds * Sysctl interface 13631da177e4SLinus Torvalds * 13641da177e4SLinus Torvalds ********************************************************************/ 13651da177e4SLinus Torvalds 13661da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 13671da177e4SLinus Torvalds 13681da177e4SLinus Torvalds #include <linux/sysctl.h> 13691da177e4SLinus Torvalds 13701da177e4SLinus Torvalds static int min_read_thresh = 8, min_write_thresh; 13711da177e4SLinus Torvalds static int max_read_thresh = INPUT_POOL_WORDS * 32; 13721da177e4SLinus Torvalds static int max_write_thresh = INPUT_POOL_WORDS * 32; 13731da177e4SLinus Torvalds static char sysctl_bootid[16]; 13741da177e4SLinus Torvalds 13751da177e4SLinus Torvalds /* 13761da177e4SLinus Torvalds * These functions is used to return both the bootid UUID, and random 13771da177e4SLinus Torvalds * UUID. The difference is in whether table->data is NULL; if it is, 13781da177e4SLinus Torvalds * then a new UUID is generated and returned to the user. 13791da177e4SLinus Torvalds * 13801da177e4SLinus Torvalds * If the user accesses this via the proc interface, it will be returned 13811da177e4SLinus Torvalds * as an ASCII string in the standard UUID format. If accesses via the 13821da177e4SLinus Torvalds * sysctl system call, it is returned as 16 bytes of binary data. 13831da177e4SLinus Torvalds */ 1384a151427eSJoe Perches static int proc_do_uuid(struct ctl_table *table, int write, 13851da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 13861da177e4SLinus Torvalds { 1387a151427eSJoe Perches struct ctl_table fake_table; 13881da177e4SLinus Torvalds unsigned char buf[64], tmp_uuid[16], *uuid; 13891da177e4SLinus Torvalds 13901da177e4SLinus Torvalds uuid = table->data; 13911da177e4SLinus Torvalds if (!uuid) { 13921da177e4SLinus Torvalds uuid = tmp_uuid; 13931da177e4SLinus Torvalds generate_random_uuid(uuid); 139444e4360fSMathieu Desnoyers } else { 139544e4360fSMathieu Desnoyers static DEFINE_SPINLOCK(bootid_spinlock); 139644e4360fSMathieu Desnoyers 139744e4360fSMathieu Desnoyers spin_lock(&bootid_spinlock); 139844e4360fSMathieu Desnoyers if (!uuid[8]) 139944e4360fSMathieu Desnoyers generate_random_uuid(uuid); 140044e4360fSMathieu Desnoyers spin_unlock(&bootid_spinlock); 140144e4360fSMathieu Desnoyers } 14021da177e4SLinus Torvalds 140335900771SJoe Perches sprintf(buf, "%pU", uuid); 140435900771SJoe Perches 14051da177e4SLinus Torvalds fake_table.data = buf; 14061da177e4SLinus Torvalds fake_table.maxlen = sizeof(buf); 14071da177e4SLinus Torvalds 14088d65af78SAlexey Dobriyan return proc_dostring(&fake_table, write, buffer, lenp, ppos); 14091da177e4SLinus Torvalds } 14101da177e4SLinus Torvalds 14111da177e4SLinus Torvalds static int sysctl_poolsize = INPUT_POOL_WORDS * 32; 1412a151427eSJoe Perches extern struct ctl_table random_table[]; 1413a151427eSJoe Perches struct ctl_table random_table[] = { 14141da177e4SLinus Torvalds { 14151da177e4SLinus Torvalds .procname = "poolsize", 14161da177e4SLinus Torvalds .data = &sysctl_poolsize, 14171da177e4SLinus Torvalds .maxlen = sizeof(int), 14181da177e4SLinus Torvalds .mode = 0444, 14196d456111SEric W. Biederman .proc_handler = proc_dointvec, 14201da177e4SLinus Torvalds }, 14211da177e4SLinus Torvalds { 14221da177e4SLinus Torvalds .procname = "entropy_avail", 14231da177e4SLinus Torvalds .maxlen = sizeof(int), 14241da177e4SLinus Torvalds .mode = 0444, 14256d456111SEric W. Biederman .proc_handler = proc_dointvec, 14261da177e4SLinus Torvalds .data = &input_pool.entropy_count, 14271da177e4SLinus Torvalds }, 14281da177e4SLinus Torvalds { 14291da177e4SLinus Torvalds .procname = "read_wakeup_threshold", 14301da177e4SLinus Torvalds .data = &random_read_wakeup_thresh, 14311da177e4SLinus Torvalds .maxlen = sizeof(int), 14321da177e4SLinus Torvalds .mode = 0644, 14336d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 14341da177e4SLinus Torvalds .extra1 = &min_read_thresh, 14351da177e4SLinus Torvalds .extra2 = &max_read_thresh, 14361da177e4SLinus Torvalds }, 14371da177e4SLinus Torvalds { 14381da177e4SLinus Torvalds .procname = "write_wakeup_threshold", 14391da177e4SLinus Torvalds .data = &random_write_wakeup_thresh, 14401da177e4SLinus Torvalds .maxlen = sizeof(int), 14411da177e4SLinus Torvalds .mode = 0644, 14426d456111SEric W. Biederman .proc_handler = proc_dointvec_minmax, 14431da177e4SLinus Torvalds .extra1 = &min_write_thresh, 14441da177e4SLinus Torvalds .extra2 = &max_write_thresh, 14451da177e4SLinus Torvalds }, 14461da177e4SLinus Torvalds { 14471da177e4SLinus Torvalds .procname = "boot_id", 14481da177e4SLinus Torvalds .data = &sysctl_bootid, 14491da177e4SLinus Torvalds .maxlen = 16, 14501da177e4SLinus Torvalds .mode = 0444, 14516d456111SEric W. Biederman .proc_handler = proc_do_uuid, 14521da177e4SLinus Torvalds }, 14531da177e4SLinus Torvalds { 14541da177e4SLinus Torvalds .procname = "uuid", 14551da177e4SLinus Torvalds .maxlen = 16, 14561da177e4SLinus Torvalds .mode = 0444, 14576d456111SEric W. Biederman .proc_handler = proc_do_uuid, 14581da177e4SLinus Torvalds }, 1459894d2491SEric W. Biederman { } 14601da177e4SLinus Torvalds }; 14611da177e4SLinus Torvalds #endif /* CONFIG_SYSCTL */ 14621da177e4SLinus Torvalds 14636e5714eaSDavid S. Miller static u32 random_int_secret[MD5_MESSAGE_BYTES / 4] ____cacheline_aligned; 14641da177e4SLinus Torvalds 146547d06e53STheodore Ts'o int random_int_secret_init(void) 14661da177e4SLinus Torvalds { 14676e5714eaSDavid S. Miller get_random_bytes(random_int_secret, sizeof(random_int_secret)); 14681da177e4SLinus Torvalds return 0; 14691da177e4SLinus Torvalds } 14701da177e4SLinus Torvalds 14711da177e4SLinus Torvalds /* 14721da177e4SLinus Torvalds * Get a random word for internal kernel use only. Similar to urandom but 14731da177e4SLinus Torvalds * with the goal of minimal entropy pool depletion. As a result, the random 14741da177e4SLinus Torvalds * value is not cryptographically secure but for several uses the cost of 14751da177e4SLinus Torvalds * depleting entropy is too high 14761da177e4SLinus Torvalds */ 147774feec5dSTheodore Ts'o static DEFINE_PER_CPU(__u32 [MD5_DIGEST_WORDS], get_random_int_hash); 14781da177e4SLinus Torvalds unsigned int get_random_int(void) 14791da177e4SLinus Torvalds { 148063d77173SH. Peter Anvin __u32 *hash; 14816e5714eaSDavid S. Miller unsigned int ret; 14828a0a9bd4SLinus Torvalds 148363d77173SH. Peter Anvin if (arch_get_random_int(&ret)) 148463d77173SH. Peter Anvin return ret; 148563d77173SH. Peter Anvin 148663d77173SH. Peter Anvin hash = get_cpu_var(get_random_int_hash); 14878a0a9bd4SLinus Torvalds 1488*61875f30STheodore Ts'o hash[0] += current->pid + jiffies + random_get_entropy(); 14896e5714eaSDavid S. Miller md5_transform(hash, random_int_secret); 14906e5714eaSDavid S. Miller ret = hash[0]; 14918a0a9bd4SLinus Torvalds put_cpu_var(get_random_int_hash); 14928a0a9bd4SLinus Torvalds 14938a0a9bd4SLinus Torvalds return ret; 14941da177e4SLinus Torvalds } 149516c7fa05SAndy Shevchenko EXPORT_SYMBOL(get_random_int); 14961da177e4SLinus Torvalds 14971da177e4SLinus Torvalds /* 14981da177e4SLinus Torvalds * randomize_range() returns a start address such that 14991da177e4SLinus Torvalds * 15001da177e4SLinus Torvalds * [...... <range> .....] 15011da177e4SLinus Torvalds * start end 15021da177e4SLinus Torvalds * 15031da177e4SLinus Torvalds * a <range> with size "len" starting at the return value is inside in the 15041da177e4SLinus Torvalds * area defined by [start, end], but is otherwise randomized. 15051da177e4SLinus Torvalds */ 15061da177e4SLinus Torvalds unsigned long 15071da177e4SLinus Torvalds randomize_range(unsigned long start, unsigned long end, unsigned long len) 15081da177e4SLinus Torvalds { 15091da177e4SLinus Torvalds unsigned long range = end - len - start; 15101da177e4SLinus Torvalds 15111da177e4SLinus Torvalds if (end <= start + len) 15121da177e4SLinus Torvalds return 0; 15131da177e4SLinus Torvalds return PAGE_ALIGN(get_random_int() % range + start); 15141da177e4SLinus Torvalds } 1515