^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * random.c -- A strong random number generator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2017 Jason A. Donenfeld <Jason@zx2c4.com>. All
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright Matt Mackall <mpm@selenic.com>, 2003, 2004, 2005
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Copyright Theodore Ts'o, 1994, 1995, 1996, 1997, 1998, 1999. All
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * Redistribution and use in source and binary forms, with or without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * modification, are permitted provided that the following conditions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * are met:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * 1. Redistributions of source code must retain the above copyright
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * notice, and the entire permission notice in its entirety,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * including the disclaimer of warranties.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * 2. Redistributions in binary form must reproduce the above copyright
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * notice, this list of conditions and the following disclaimer in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * documentation and/or other materials provided with the distribution.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * 3. The name of the author may not be used to endorse or promote
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * products derived from this software without specific prior
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * written permission.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * ALTERNATIVELY, this product may be distributed under the terms of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * the GNU General Public License, in which case the provisions of the GPL are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * required INSTEAD OF the above restrictions. (This clause is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * necessary due to a potential bad interaction between the GPL and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * the restrictions contained in a BSD-style copyright.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * DAMAGE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * (now, with legal B.S. out of the way.....)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * This routine gathers environmental noise from device drivers, etc.,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * and returns good random numbers, suitable for cryptographic use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * Besides the obvious cryptographic uses, these numbers are also good
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * for seeding TCP sequence numbers, and other places where it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * desirable to have numbers which are not only random, but hard to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * predict by an attacker.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * Theory of operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * ===================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * Computers are very predictable devices. Hence it is extremely hard
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * to produce truly random numbers on a computer --- as opposed to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * pseudo-random numbers, which can easily generated by using a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * algorithm. Unfortunately, it is very easy for attackers to guess
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * the sequence of pseudo-random number generators, and for some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * applications this is not acceptable. So instead, we must try to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * gather "environmental noise" from the computer's environment, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * must be hard for outside attackers to observe, and use that to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * generate random numbers. In a Unix environment, this is best done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * from inside the kernel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * Sources of randomness from the environment include inter-keyboard
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * timings, inter-interrupt timings from some interrupts, and other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * events which are both (a) non-deterministic and (b) hard for an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * outside observer to measure. Randomness from these sources are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * added to an "entropy pool", which is mixed using a CRC-like function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * This is not cryptographically strong, but it is adequate assuming
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * the randomness is not chosen maliciously, and it is fast enough that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * the overhead of doing it on every interrupt is very reasonable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * As random bytes are mixed into the entropy pool, the routines keep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * an *estimate* of how many bits of randomness have been stored into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * the random number generator's internal state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * When random bytes are desired, they are obtained by taking the SHA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * hash of the contents of the "entropy pool". The SHA hash avoids
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * exposing the internal state of the entropy pool. It is believed to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * be computationally infeasible to derive any useful information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * about the input of SHA from its output. Even if it is possible to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * analyze SHA in some clever way, as long as the amount of data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * returned from the generator is less than the inherent entropy in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * the pool, the output data is totally unpredictable. For this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * reason, the routine decreases its internal estimate of how many
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * bits of "true randomness" are contained in the entropy pool as it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * outputs random numbers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * If this estimate goes to zero, the routine can still generate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * random numbers; however, an attacker may (at least in theory) be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * able to infer the future output of the generator from prior
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * outputs. This requires successful cryptanalysis of SHA, which is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * not believed to be feasible, but there is a remote possibility.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * Nonetheless, these numbers should be useful for the vast majority
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * of purposes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * Exported interfaces ---- output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * ===============================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * There are four exported interfaces; two for use within the kernel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * and two or use from userspace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * Exported interfaces ---- userspace output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * -----------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * The userspace interfaces are two character devices /dev/random and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * /dev/urandom. /dev/random is suitable for use when very high
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * quality randomness is desired (for example, for key generation or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * one-time pads), as it will only return a maximum of the number of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * bits of randomness (as estimated by the random number generator)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * contained in the entropy pool.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * The /dev/urandom device does not have this limit, and will return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * as many bytes as are requested. As more and more random bytes are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * requested without giving time for the entropy pool to recharge,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * this will result in random numbers that are merely cryptographically
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * strong. For many applications, however, this is acceptable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * Exported interfaces ---- kernel output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * --------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * The primary kernel interface is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * void get_random_bytes(void *buf, int nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * This interface will return the requested number of random bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * and place it in the requested buffer. This is equivalent to a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * read from /dev/urandom.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * For less critical applications, there are the functions:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * u32 get_random_u32()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * u64 get_random_u64()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * unsigned int get_random_int()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * unsigned long get_random_long()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * These are produced by a cryptographic RNG seeded from get_random_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * and so do not deplete the entropy pool as much. These are recommended
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * for most in-kernel operations *if the result is going to be stored in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * the kernel*.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * Specifically, the get_random_int() family do not attempt to do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * "anti-backtracking". If you capture the state of the kernel (e.g.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * by snapshotting the VM), you can figure out previous get_random_int()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * return values. But if the value is stored in the kernel anyway,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * this is not a problem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * It *is* safe to expose get_random_int() output to attackers (e.g. as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * network cookies); given outputs 1..n, it's not feasible to predict
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * outputs 0 or n+1. The only concern is an attacker who breaks into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * the kernel later; the get_random_int() engine is not reseeded as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * often as the get_random_bytes() one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * get_random_bytes() is needed for keys that need to stay secret after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * they are erased from the kernel. For example, any key that will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * be wrapped and stored encrypted. And session encryption keys: we'd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * like to know that after the session is closed and the keys erased,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * the plaintext is unrecoverable to someone who recorded the ciphertext.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * But for network ports/cookies, stack canaries, PRNG seeds, address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * space layout randomization, session *authentication* keys, or other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * applications where the sensitive data is stored in the kernel in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * plaintext for as long as it's sensitive, the get_random_int() family
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) * is just fine.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) * Consider ASLR. We want to keep the address space secret from an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) * outside attacker while the process is running, but once the address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) * space is torn down, it's of no use to an attacker any more. And it's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) * stored in kernel data structures as long as it's alive, so worrying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) * about an attacker's ability to extrapolate it from the get_random_int()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) * CRNG is silly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) * Even some cryptographic keys are safe to generate with get_random_int().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * In particular, keys for SipHash are generally fine. Here, knowledge
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * of the key authorizes you to do something to a kernel object (inject
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * packets to a network connection, or flood a hash table), and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) * key is stored with the object being protected. Once it goes away,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * we no longer care if anyone knows the key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) * prandom_u32()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * -------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) * For even weaker applications, see the pseudorandom generator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) * prandom_u32(), prandom_max(), and prandom_bytes(). If the random
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * numbers aren't security-critical at all, these are *far* cheaper.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) * Useful for self-tests, random error simulation, randomized backoffs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) * and any other application where you trust that nobody is trying to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * maliciously mess with you by guessing the "random" numbers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * Exported interfaces ---- input
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * ==============================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * The current exported interfaces for gathering environmental noise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * from the devices are:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * void add_device_randomness(const void *buf, unsigned int size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * void add_input_randomness(unsigned int type, unsigned int code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * unsigned int value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * void add_interrupt_randomness(int irq, int irq_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * void add_disk_randomness(struct gendisk *disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) * add_device_randomness() is for adding data to the random pool that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * is likely to differ between two devices (or possibly even per boot).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * This would be things like MAC addresses or serial numbers, or the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * read-out of the RTC. This does *not* add any actual entropy to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * pool, but it initializes the pool to different values for devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * that might otherwise be identical and have very little entropy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * available to them (particularly common in the embedded world).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) * add_input_randomness() uses the input layer interrupt timing, as well as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * the event type information from the hardware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) * add_interrupt_randomness() uses the interrupt timing as random
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * inputs to the entropy pool. Using the cycle counters and the irq source
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) * as inputs, it feeds the randomness roughly once a second.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) * add_disk_randomness() uses what amounts to the seek time of block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) * layer request events, on a per-disk_devt basis, as input to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) * entropy pool. Note that high-speed solid state drives with very low
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) * seek times do not make for good sources of entropy, as their seek
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * times are usually fairly consistent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) * All of these routines try to estimate how many bits of randomness a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) * particular randomness source. They do this by keeping track of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) * first and second order deltas of the event timings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) * Ensuring unpredictability at system startup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) * ============================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) * When any operating system starts up, it will go through a sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) * of actions that are fairly predictable by an adversary, especially
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) * if the start-up does not involve interaction with a human operator.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) * This reduces the actual number of bits of unpredictability in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) * entropy pool below the value in entropy_count. In order to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) * counteract this effect, it helps to carry information in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) * entropy pool across shut-downs and start-ups. To do this, put the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) * following lines an appropriate script which is run during the boot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) * sequence:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) * echo "Initializing random number generator..."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * random_seed=/var/run/random-seed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) * # Carry a random seed from start-up to start-up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) * # Load and then save the whole entropy pool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) * if [ -f $random_seed ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) * cat $random_seed >/dev/urandom
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * touch $random_seed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) * fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) * chmod 600 $random_seed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) * dd if=/dev/urandom of=$random_seed count=1 bs=512
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) * and the following lines in an appropriate script which is run as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) * the system is shutdown:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) * # Carry a random seed from shut-down to start-up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) * # Save the whole entropy pool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * echo "Saving random seed..."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) * random_seed=/var/run/random-seed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) * touch $random_seed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) * chmod 600 $random_seed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) * dd if=/dev/urandom of=$random_seed count=1 bs=512
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) * For example, on most modern systems using the System V init
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) * scripts, such code fragments would be found in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) * /etc/rc.d/init.d/random. On older Linux systems, the correct script
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) * location might be in /etc/rcb.d/rc.local or /etc/rc.d/rc.0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) * Effectively, these commands cause the contents of the entropy pool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) * to be saved at shut-down time and reloaded into the entropy pool at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) * start-up. (The 'dd' in the addition to the bootup script is to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) * make sure that /etc/random-seed is different for every start-up,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) * even if the system crashes without executing rc.0.) Even with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) * complete knowledge of the start-up activities, predicting the state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) * of the entropy pool requires knowledge of the previous history of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) * the system.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) * Configuring the /dev/random driver under Linux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) * ==============================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) * The /dev/random driver under Linux uses minor numbers 8 and 9 of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * the /dev/mem major number (#1). So if your system does not have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * /dev/random and /dev/urandom created already, they can be created
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) * by using the commands:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * mknod /dev/random c 1 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) * mknod /dev/urandom c 1 9
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) * Acknowledgements:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) * =================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) * Ideas for constructing this random number generator were derived
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) * from Pretty Good Privacy's random number generator, and from private
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) * discussions with Phil Karn. Colin Plumb provided a faster random
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) * number generator, which speed up the mixing function of the entropy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) * pool, taken from PGPfone. Dale Worley has also contributed many
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * useful ideas and suggestions to improve this driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) * Any flaws in the design are solely my responsibility, and should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * not be attributed to the Phil, Colin, or any of authors of PGP.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) * Further background information on this topic may be obtained from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) * RFC 1750, "Randomness Recommendations for Security", by Donald
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) * Eastlake, Steve Crocker, and Jeff Schiller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) #include <linux/utsname.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) #include <linux/major.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) #include <linux/fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) #include <linux/genhd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) #include <linux/nodemask.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) #include <linux/percpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) #include <linux/fips.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) #include <linux/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) #include <linux/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) #include <linux/ratelimit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) #include <linux/syscalls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) #include <linux/completion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) #include <linux/uuid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) #include <crypto/chacha.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) #include <crypto/sha.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) #include <asm/processor.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) #include <asm/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) #include <asm/irq_regs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) #include <asm/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) #define CREATE_TRACE_POINTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) #include <trace/events/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) /* #define ADD_INTERRUPT_BENCH */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) * Configuration information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) #define INPUT_POOL_SHIFT 12
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) #define INPUT_POOL_WORDS (1 << (INPUT_POOL_SHIFT-5))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) #define OUTPUT_POOL_SHIFT 10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) #define OUTPUT_POOL_WORDS (1 << (OUTPUT_POOL_SHIFT-5))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) #define EXTRACT_SIZE 10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) #define LONGS(x) (((x) + sizeof(unsigned long) - 1)/sizeof(unsigned long))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) * To allow fractional bits to be tracked, the entropy_count field is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) * denominated in units of 1/8th bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) * 2*(ENTROPY_SHIFT + poolbitshift) must <= 31, or the multiply in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) * credit_entropy_bits() needs to be 64 bits wide.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) #define ENTROPY_SHIFT 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) #define ENTROPY_BITS(r) ((r)->entropy_count >> ENTROPY_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) * If the entropy count falls under this number of bits, then we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) * should wake up processes which are selecting or polling on write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) * access to /dev/random.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) static int random_write_wakeup_bits = 28 * OUTPUT_POOL_WORDS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) * Originally, we used a primitive polynomial of degree .poolwords
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) * over GF(2). The taps for various sizes are defined below. They
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) * were chosen to be evenly spaced except for the last tap, which is 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) * to get the twisting happening as fast as possible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) * For the purposes of better mixing, we use the CRC-32 polynomial as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) * well to make a (modified) twisted Generalized Feedback Shift
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) * Register. (See M. Matsumoto & Y. Kurita, 1992. Twisted GFSR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) * generators. ACM Transactions on Modeling and Computer Simulation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) * 2(3):179-194. Also see M. Matsumoto & Y. Kurita, 1994. Twisted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) * GFSR generators II. ACM Transactions on Modeling and Computer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) * Simulation 4:254-266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) * Thanks to Colin Plumb for suggesting this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) * The mixing operation is much less sensitive than the output hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) * where we use SHA-1. All that we want of mixing operation is that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) * it be a good non-cryptographic hash; i.e. it not produce collisions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) * when fed "random" data of the sort we expect to see. As long as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) * the pool state differs for different inputs, we have preserved the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) * input entropy and done a good job. The fact that an intelligent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) * attacker can construct inputs that will produce controlled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) * alterations to the pool's state is not important because we don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) * consider such inputs to contribute any randomness. The only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) * property we need with respect to them is that the attacker can't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) * increase his/her knowledge of the pool's state. Since all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) * additions are reversible (knowing the final state and the input,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) * you can reconstruct the initial state), if an attacker has any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) * uncertainty about the initial state, he/she can only shuffle that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) * uncertainty about, but never cause any collisions (which would
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) * decrease the uncertainty).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) * Our mixing functions were analyzed by Lacharme, Roeck, Strubel, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) * Videau in their paper, "The Linux Pseudorandom Number Generator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) * Revisited" (see: http://eprint.iacr.org/2012/251.pdf). In their
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) * paper, they point out that we are not using a true Twisted GFSR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) * since Matsumoto & Kurita used a trinomial feedback polynomial (that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) * is, with only three taps, instead of the six that we are using).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) * As a result, the resulting polynomial is neither primitive nor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) * irreducible, and hence does not have a maximal period over
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) * GF(2**32). They suggest a slight change to the generator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) * polynomial which improves the resulting TGFSR polynomial to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) * irreducible, which we have made here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) static const struct poolinfo {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) int poolbitshift, poolwords, poolbytes, poolfracbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) #define S(x) ilog2(x)+5, (x), (x)*4, (x) << (ENTROPY_SHIFT+5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) int tap1, tap2, tap3, tap4, tap5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) } poolinfo_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) /* was: x^128 + x^103 + x^76 + x^51 +x^25 + x + 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) /* x^128 + x^104 + x^76 + x^51 +x^25 + x + 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) { S(128), 104, 76, 51, 25, 1 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) * Static global variables
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) static DECLARE_WAIT_QUEUE_HEAD(random_write_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) static struct fasync_struct *fasync;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) static DEFINE_SPINLOCK(random_ready_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) static LIST_HEAD(random_ready_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) struct crng_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) __u32 state[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) unsigned long init_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) static struct crng_state primary_crng = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) .lock = __SPIN_LOCK_UNLOCKED(primary_crng.lock),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) * crng_init = 0 --> Uninitialized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) * 1 --> Initialized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) * 2 --> Initialized from input_pool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) * crng_init is protected by primary_crng->lock, and only increases
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) * its value (from 0->1->2).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) static int crng_init = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) static bool crng_need_final_init = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) #define crng_ready() (likely(crng_init > 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) static int crng_init_cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) static unsigned long crng_global_init_time = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) #define CRNG_INIT_CNT_THRESH (2*CHACHA_KEY_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) static void _extract_crng(struct crng_state *crng, __u8 out[CHACHA_BLOCK_SIZE]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) static void _crng_backtrack_protect(struct crng_state *crng,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) __u8 tmp[CHACHA_BLOCK_SIZE], int used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) static void process_random_ready_list(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) static void _get_random_bytes(void *buf, int nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) static struct ratelimit_state unseeded_warning =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) RATELIMIT_STATE_INIT("warn_unseeded_randomness", HZ, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) static struct ratelimit_state urandom_warning =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) RATELIMIT_STATE_INIT("warn_urandom_randomness", HZ, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) static int ratelimit_disable __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) module_param_named(ratelimit_disable, ratelimit_disable, int, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) MODULE_PARM_DESC(ratelimit_disable, "Disable random ratelimit suppression");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) /**********************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) * OS independent entropy store. Here are the functions which handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) * storing entropy in an entropy pool.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) **********************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) struct entropy_store;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) struct entropy_store {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) /* read-only data: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) const struct poolinfo *poolinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) __u32 *pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) /* read-write data: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) unsigned short add_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) unsigned short input_rotate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) int entropy_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) unsigned int initialized:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) unsigned int last_data_init:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) __u8 last_data[EXTRACT_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) static ssize_t extract_entropy(struct entropy_store *r, void *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) size_t nbytes, int min, int rsvd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) static ssize_t _extract_entropy(struct entropy_store *r, void *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) size_t nbytes, int fips);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) static void crng_reseed(struct crng_state *crng, struct entropy_store *r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) static __u32 input_pool_data[INPUT_POOL_WORDS] __latent_entropy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) static struct entropy_store input_pool = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) .poolinfo = &poolinfo_table[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) .name = "input",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) .lock = __SPIN_LOCK_UNLOCKED(input_pool.lock),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) .pool = input_pool_data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) static __u32 const twist_table[8] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 0x00000000, 0x3b6e20c8, 0x76dc4190, 0x4db26158,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 0xedb88320, 0xd6d6a3e8, 0x9b64c2b0, 0xa00ae278 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) * This function adds bytes into the entropy "pool". It does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) * update the entropy estimate. The caller should call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) * credit_entropy_bits if this is appropriate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) * The pool is stirred with a primitive polynomial of the appropriate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) * degree, and then twisted. We twist by three bits at a time because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) * it's cheap to do so and helps slightly in the expected case where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) * the entropy is concentrated in the low-order bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) static void _mix_pool_bytes(struct entropy_store *r, const void *in,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) int nbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) unsigned long i, tap1, tap2, tap3, tap4, tap5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) int input_rotate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) int wordmask = r->poolinfo->poolwords - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) const char *bytes = in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) __u32 w;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) tap1 = r->poolinfo->tap1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) tap2 = r->poolinfo->tap2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) tap3 = r->poolinfo->tap3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) tap4 = r->poolinfo->tap4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) tap5 = r->poolinfo->tap5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) input_rotate = r->input_rotate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) i = r->add_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) /* mix one byte at a time to simplify size handling and churn faster */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) while (nbytes--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) w = rol32(*bytes++, input_rotate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) i = (i - 1) & wordmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) /* XOR in the various taps */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) w ^= r->pool[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) w ^= r->pool[(i + tap1) & wordmask];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) w ^= r->pool[(i + tap2) & wordmask];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) w ^= r->pool[(i + tap3) & wordmask];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) w ^= r->pool[(i + tap4) & wordmask];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) w ^= r->pool[(i + tap5) & wordmask];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) /* Mix the result back in with a twist */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) r->pool[i] = (w >> 3) ^ twist_table[w & 7];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) * Normally, we add 7 bits of rotation to the pool.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) * At the beginning of the pool, add an extra 7 bits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) * rotation, so that successive passes spread the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) * input bits across the pool evenly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) input_rotate = (input_rotate + (i ? 7 : 14)) & 31;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) r->input_rotate = input_rotate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) r->add_ptr = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) static void __mix_pool_bytes(struct entropy_store *r, const void *in,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) int nbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) trace_mix_pool_bytes_nolock(r->name, nbytes, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) _mix_pool_bytes(r, in, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) static void mix_pool_bytes(struct entropy_store *r, const void *in,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) int nbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) trace_mix_pool_bytes(r->name, nbytes, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) spin_lock_irqsave(&r->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) _mix_pool_bytes(r, in, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) spin_unlock_irqrestore(&r->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) struct fast_pool {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) __u32 pool[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) unsigned long last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) unsigned short reg_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) unsigned char count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) * This is a fast mixing routine used by the interrupt randomness
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) * collector. It's hardcoded for an 128 bit pool and assumes that any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) * locks that might be needed are taken by the caller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) static void fast_mix(struct fast_pool *f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) __u32 a = f->pool[0], b = f->pool[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) __u32 c = f->pool[2], d = f->pool[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) a += b; c += d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) b = rol32(b, 6); d = rol32(d, 27);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) d ^= a; b ^= c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) a += b; c += d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) b = rol32(b, 16); d = rol32(d, 14);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) d ^= a; b ^= c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) a += b; c += d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) b = rol32(b, 6); d = rol32(d, 27);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) d ^= a; b ^= c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) a += b; c += d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) b = rol32(b, 16); d = rol32(d, 14);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) d ^= a; b ^= c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) f->pool[0] = a; f->pool[1] = b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) f->pool[2] = c; f->pool[3] = d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) f->count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) static void process_random_ready_list(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) struct random_ready_callback *rdy, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) spin_lock_irqsave(&random_ready_list_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) list_for_each_entry_safe(rdy, tmp, &random_ready_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) struct module *owner = rdy->owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) list_del_init(&rdy->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) rdy->func(rdy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) module_put(owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) spin_unlock_irqrestore(&random_ready_list_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) * Credit (or debit) the entropy store with n bits of entropy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) * Use credit_entropy_bits_safe() if the value comes from userspace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) * or otherwise should be checked for extreme values.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) static void credit_entropy_bits(struct entropy_store *r, int nbits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) int entropy_count, orig, has_initialized = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) const int pool_size = r->poolinfo->poolfracbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) int nfrac = nbits << ENTROPY_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) if (!nbits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) entropy_count = orig = READ_ONCE(r->entropy_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) if (nfrac < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) /* Debit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) entropy_count += nfrac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) * Credit: we have to account for the possibility of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) * overwriting already present entropy. Even in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) * ideal case of pure Shannon entropy, new contributions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) * approach the full value asymptotically:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) * entropy <- entropy + (pool_size - entropy) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) * (1 - exp(-add_entropy/pool_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) * For add_entropy <= pool_size/2 then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) * (1 - exp(-add_entropy/pool_size)) >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) * (add_entropy/pool_size)*0.7869...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) * so we can approximate the exponential with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) * 3/4*add_entropy/pool_size and still be on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) * safe side by adding at most pool_size/2 at a time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) * The use of pool_size-2 in the while statement is to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) * prevent rounding artifacts from making the loop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) * arbitrarily long; this limits the loop to log2(pool_size)*2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) * turns no matter how large nbits is.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) int pnfrac = nfrac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) const int s = r->poolinfo->poolbitshift + ENTROPY_SHIFT + 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) /* The +2 corresponds to the /4 in the denominator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) unsigned int anfrac = min(pnfrac, pool_size/2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) unsigned int add =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) ((pool_size - entropy_count)*anfrac*3) >> s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) entropy_count += add;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) pnfrac -= anfrac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) } while (unlikely(entropy_count < pool_size-2 && pnfrac));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) if (WARN_ON(entropy_count < 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) pr_warn("negative entropy/overflow: pool %s count %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) r->name, entropy_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) entropy_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) } else if (entropy_count > pool_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) entropy_count = pool_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) if (cmpxchg(&r->entropy_count, orig, entropy_count) != orig)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) if (has_initialized) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) r->initialized = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) kill_fasync(&fasync, SIGIO, POLL_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) trace_credit_entropy_bits(r->name, nbits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) entropy_count >> ENTROPY_SHIFT, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) if (r == &input_pool) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) int entropy_bits = entropy_count >> ENTROPY_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) if (crng_init < 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) if (entropy_bits < 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) crng_reseed(&primary_crng, r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) entropy_bits = ENTROPY_BITS(r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) static int credit_entropy_bits_safe(struct entropy_store *r, int nbits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) const int nbits_max = r->poolinfo->poolwords * 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) if (nbits < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) /* Cap the value to avoid overflows */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) nbits = min(nbits, nbits_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) credit_entropy_bits(r, nbits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) /*********************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) * CRNG using CHACHA20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) *********************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) #define CRNG_RESEED_INTERVAL (300*HZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) static DECLARE_WAIT_QUEUE_HEAD(crng_init_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) #ifdef CONFIG_NUMA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) * Hack to deal with crazy userspace progams when they are all trying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) * to access /dev/urandom in parallel. The programs are almost
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) * certainly doing something terribly wrong, but we'll work around
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) * their brain damage.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) static struct crng_state **crng_node_pool __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) static void invalidate_batched_entropy(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) static void numa_crng_init(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) static bool trust_cpu __ro_after_init = IS_ENABLED(CONFIG_RANDOM_TRUST_CPU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) static int __init parse_trust_cpu(char *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) return kstrtobool(arg, &trust_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) early_param("random.trust_cpu", parse_trust_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) static bool crng_init_try_arch(struct crng_state *crng)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) bool arch_init = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) unsigned long rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) for (i = 4; i < 16; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) if (!arch_get_random_seed_long(&rv) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) !arch_get_random_long(&rv)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) rv = random_get_entropy();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) arch_init = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) crng->state[i] ^= rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) return arch_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) static bool __init crng_init_try_arch_early(struct crng_state *crng)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) bool arch_init = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) unsigned long rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) for (i = 4; i < 16; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) if (!arch_get_random_seed_long_early(&rv) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) !arch_get_random_long_early(&rv)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) rv = random_get_entropy();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) arch_init = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) crng->state[i] ^= rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) return arch_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) static void __maybe_unused crng_initialize_secondary(struct crng_state *crng)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) chacha_init_consts(crng->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) _get_random_bytes(&crng->state[4], sizeof(__u32) * 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) crng_init_try_arch(crng);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) crng->init_time = jiffies - CRNG_RESEED_INTERVAL - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) static void __init crng_initialize_primary(struct crng_state *crng)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) chacha_init_consts(crng->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) _extract_entropy(&input_pool, &crng->state[4], sizeof(__u32) * 12, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) if (crng_init_try_arch_early(crng) && trust_cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) invalidate_batched_entropy();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) numa_crng_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) crng_init = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) pr_notice("crng done (trusting CPU's manufacturer)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) crng->init_time = jiffies - CRNG_RESEED_INTERVAL - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) static void crng_finalize_init(struct crng_state *crng)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) if (crng != &primary_crng || crng_init >= 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) if (!system_wq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) /* We can't call numa_crng_init until we have workqueues,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) * so mark this for processing later. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) crng_need_final_init = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) invalidate_batched_entropy();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) numa_crng_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) crng_init = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) process_random_ready_list();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) wake_up_interruptible(&crng_init_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) kill_fasync(&fasync, SIGIO, POLL_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) pr_notice("crng init done\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) if (unseeded_warning.missed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) pr_notice("%d get_random_xx warning(s) missed due to ratelimiting\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) unseeded_warning.missed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) unseeded_warning.missed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) if (urandom_warning.missed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) pr_notice("%d urandom warning(s) missed due to ratelimiting\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) urandom_warning.missed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) urandom_warning.missed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) #ifdef CONFIG_NUMA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) static void do_numa_crng_init(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) struct crng_state *crng;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) struct crng_state **pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) pool = kcalloc(nr_node_ids, sizeof(*pool), GFP_KERNEL|__GFP_NOFAIL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) for_each_online_node(i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) crng = kmalloc_node(sizeof(struct crng_state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) GFP_KERNEL | __GFP_NOFAIL, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) spin_lock_init(&crng->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) crng_initialize_secondary(crng);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) pool[i] = crng;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) /* pairs with READ_ONCE() in select_crng() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) if (cmpxchg_release(&crng_node_pool, NULL, pool) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) for_each_node(i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) kfree(pool[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) kfree(pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) static DECLARE_WORK(numa_crng_init_work, do_numa_crng_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) static void numa_crng_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) schedule_work(&numa_crng_init_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) static struct crng_state *select_crng(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) struct crng_state **pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) int nid = numa_node_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) /* pairs with cmpxchg_release() in do_numa_crng_init() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) pool = READ_ONCE(crng_node_pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) if (pool && pool[nid])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) return pool[nid];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) return &primary_crng;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) static void numa_crng_init(void) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) static struct crng_state *select_crng(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) return &primary_crng;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) * crng_fast_load() can be called by code in the interrupt service
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) * path. So we can't afford to dilly-dally. Returns the number of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) * bytes processed from cp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) static size_t crng_fast_load(const char *cp, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) size_t ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) if (!spin_trylock_irqsave(&primary_crng.lock, flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) if (crng_init != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) spin_unlock_irqrestore(&primary_crng.lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) p = (unsigned char *) &primary_crng.state[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) while (len > 0 && crng_init_cnt < CRNG_INIT_CNT_THRESH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) p[crng_init_cnt % CHACHA_KEY_SIZE] ^= *cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) cp++; crng_init_cnt++; len--; ret++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) spin_unlock_irqrestore(&primary_crng.lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) if (crng_init_cnt >= CRNG_INIT_CNT_THRESH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) invalidate_batched_entropy();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) crng_init = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) pr_notice("fast init done\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) * crng_slow_load() is called by add_device_randomness, which has two
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) * attributes. (1) We can't trust the buffer passed to it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) * guaranteed to be unpredictable (so it might not have any entropy at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) * all), and (2) it doesn't have the performance constraints of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) * crng_fast_load().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) * So we do something more comprehensive which is guaranteed to touch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) * all of the primary_crng's state, and which uses a LFSR with a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) * period of 255 as part of the mixing algorithm. Finally, we do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) * *not* advance crng_init_cnt since buffer we may get may be something
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) * like a fixed DMI table (for example), which might very well be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) * unique to the machine, but is otherwise unvarying.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) static int crng_slow_load(const char *cp, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) static unsigned char lfsr = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) unsigned char tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) unsigned i, max = CHACHA_KEY_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) const char * src_buf = cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) char * dest_buf = (char *) &primary_crng.state[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) if (!spin_trylock_irqsave(&primary_crng.lock, flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) if (crng_init != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) spin_unlock_irqrestore(&primary_crng.lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) if (len > max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) max = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) for (i = 0; i < max ; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) tmp = lfsr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) lfsr >>= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) if (tmp & 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) lfsr ^= 0xE1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) tmp = dest_buf[i % CHACHA_KEY_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) dest_buf[i % CHACHA_KEY_SIZE] ^= src_buf[i % len] ^ lfsr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) lfsr += (tmp << 3) | (tmp >> 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) spin_unlock_irqrestore(&primary_crng.lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) static void crng_reseed(struct crng_state *crng, struct entropy_store *r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) int i, num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) __u8 block[CHACHA_BLOCK_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) __u32 key[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) } buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) num = extract_entropy(r, &buf, 32, 16, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) if (num == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) _extract_crng(&primary_crng, buf.block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) _crng_backtrack_protect(&primary_crng, buf.block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) CHACHA_KEY_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) spin_lock_irqsave(&crng->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) for (i = 0; i < 8; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) unsigned long rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) if (!arch_get_random_seed_long(&rv) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) !arch_get_random_long(&rv))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) rv = random_get_entropy();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) crng->state[i+4] ^= buf.key[i] ^ rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) memzero_explicit(&buf, sizeof(buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) WRITE_ONCE(crng->init_time, jiffies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) spin_unlock_irqrestore(&crng->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) crng_finalize_init(crng);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) static void _extract_crng(struct crng_state *crng,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) __u8 out[CHACHA_BLOCK_SIZE])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) unsigned long v, flags, init_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) if (crng_ready()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) init_time = READ_ONCE(crng->init_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) if (time_after(READ_ONCE(crng_global_init_time), init_time) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) time_after(jiffies, init_time + CRNG_RESEED_INTERVAL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) crng_reseed(crng, crng == &primary_crng ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) &input_pool : NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) spin_lock_irqsave(&crng->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) if (arch_get_random_long(&v))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) crng->state[14] ^= v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) chacha20_block(&crng->state[0], out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) if (crng->state[12] == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) crng->state[13]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) spin_unlock_irqrestore(&crng->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) static void extract_crng(__u8 out[CHACHA_BLOCK_SIZE])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) _extract_crng(select_crng(), out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) * Use the leftover bytes from the CRNG block output (if there is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) * enough) to mutate the CRNG key to provide backtracking protection.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) static void _crng_backtrack_protect(struct crng_state *crng,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) __u8 tmp[CHACHA_BLOCK_SIZE], int used)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) __u32 *s, *d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) used = round_up(used, sizeof(__u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) if (used + CHACHA_KEY_SIZE > CHACHA_BLOCK_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) extract_crng(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) used = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) spin_lock_irqsave(&crng->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) s = (__u32 *) &tmp[used];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) d = &crng->state[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) for (i=0; i < 8; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) *d++ ^= *s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) spin_unlock_irqrestore(&crng->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) static void crng_backtrack_protect(__u8 tmp[CHACHA_BLOCK_SIZE], int used)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) _crng_backtrack_protect(select_crng(), tmp, used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) static ssize_t extract_crng_user(void __user *buf, size_t nbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) ssize_t ret = 0, i = CHACHA_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) __u8 tmp[CHACHA_BLOCK_SIZE] __aligned(4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) int large_request = (nbytes > 256);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) while (nbytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) if (large_request && need_resched()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) if (signal_pending(current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) ret = -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) extract_crng(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) i = min_t(int, nbytes, CHACHA_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) if (copy_to_user(buf, tmp, i)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) nbytes -= i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) buf += i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) ret += i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) crng_backtrack_protect(tmp, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) /* Wipe data just written to memory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) memzero_explicit(tmp, sizeof(tmp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) /*********************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) * Entropy input management
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) *********************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) /* There is one of these per entropy source */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) struct timer_rand_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) cycles_t last_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) long last_delta, last_delta2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) #define INIT_TIMER_RAND_STATE { INITIAL_JIFFIES, };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) * Add device- or boot-specific data to the input pool to help
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) * initialize it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) * None of this adds any entropy; it is meant to avoid the problem of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) * the entropy pool having similar initial state across largely
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) * identical devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) void add_device_randomness(const void *buf, unsigned int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) unsigned long time = random_get_entropy() ^ jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) if (!crng_ready() && size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) crng_slow_load(buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) trace_add_device_randomness(size, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) spin_lock_irqsave(&input_pool.lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) _mix_pool_bytes(&input_pool, buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) _mix_pool_bytes(&input_pool, &time, sizeof(time));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) spin_unlock_irqrestore(&input_pool.lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) EXPORT_SYMBOL(add_device_randomness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) static struct timer_rand_state input_timer_state = INIT_TIMER_RAND_STATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) * This function adds entropy to the entropy "pool" by using timing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) * delays. It uses the timer_rand_state structure to make an estimate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) * of how many bits of entropy this call has added to the pool.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) * The number "num" is also added to the pool - it should somehow describe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) * the type of event which just happened. This is currently 0-255 for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) * keyboard scan codes, and 256 upwards for interrupts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) static void add_timer_randomness(struct timer_rand_state *state, unsigned num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) struct entropy_store *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) long jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) unsigned cycles;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) unsigned num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) } sample;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) long delta, delta2, delta3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) sample.jiffies = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) sample.cycles = random_get_entropy();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) sample.num = num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) r = &input_pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) mix_pool_bytes(r, &sample, sizeof(sample));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) * Calculate number of bits of randomness we probably added.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) * We take into account the first, second and third-order deltas
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) * in order to make our estimate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) delta = sample.jiffies - READ_ONCE(state->last_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) WRITE_ONCE(state->last_time, sample.jiffies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) delta2 = delta - READ_ONCE(state->last_delta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) WRITE_ONCE(state->last_delta, delta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) delta3 = delta2 - READ_ONCE(state->last_delta2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) WRITE_ONCE(state->last_delta2, delta2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) if (delta < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) delta = -delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) if (delta2 < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) delta2 = -delta2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) if (delta3 < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) delta3 = -delta3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) if (delta > delta2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) delta = delta2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) if (delta > delta3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) delta = delta3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) * delta is now minimum absolute delta.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) * Round down by 1 bit on general principles,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) * and limit entropy estimate to 12 bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) credit_entropy_bits(r, min_t(int, fls(delta>>1), 11));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) void add_input_randomness(unsigned int type, unsigned int code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) unsigned int value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) static unsigned char last_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) /* ignore autorepeat and the like */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) if (value == last_value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) last_value = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) add_timer_randomness(&input_timer_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) (type << 4) ^ code ^ (code >> 4) ^ value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) trace_add_input_randomness(ENTROPY_BITS(&input_pool));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) EXPORT_SYMBOL_GPL(add_input_randomness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) static DEFINE_PER_CPU(struct fast_pool, irq_randomness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) #ifdef ADD_INTERRUPT_BENCH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) static unsigned long avg_cycles, avg_deviation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) #define AVG_SHIFT 8 /* Exponential average factor k=1/256 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) #define FIXED_1_2 (1 << (AVG_SHIFT-1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) static void add_interrupt_bench(cycles_t start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) long delta = random_get_entropy() - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) /* Use a weighted moving average */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) delta = delta - ((avg_cycles + FIXED_1_2) >> AVG_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) avg_cycles += delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) /* And average deviation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) delta = abs(delta) - ((avg_deviation + FIXED_1_2) >> AVG_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) avg_deviation += delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) #define add_interrupt_bench(x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) static __u32 get_reg(struct fast_pool *f, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) __u32 *ptr = (__u32 *) regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) unsigned int idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) if (regs == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) idx = READ_ONCE(f->reg_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) if (idx >= sizeof(struct pt_regs) / sizeof(__u32))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) ptr += idx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) WRITE_ONCE(f->reg_idx, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) return *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) void add_interrupt_randomness(int irq, int irq_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) struct entropy_store *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) struct fast_pool *fast_pool = this_cpu_ptr(&irq_randomness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) struct pt_regs *regs = get_irq_regs();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) unsigned long now = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) cycles_t cycles = random_get_entropy();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) __u32 c_high, j_high;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) __u64 ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) unsigned long seed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) int credit = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) if (cycles == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) cycles = get_reg(fast_pool, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) c_high = (sizeof(cycles) > 4) ? cycles >> 32 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) j_high = (sizeof(now) > 4) ? now >> 32 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) fast_pool->pool[0] ^= cycles ^ j_high ^ irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) fast_pool->pool[1] ^= now ^ c_high;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) ip = regs ? instruction_pointer(regs) : _RET_IP_;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) fast_pool->pool[2] ^= ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) fast_pool->pool[3] ^= (sizeof(ip) > 4) ? ip >> 32 :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) get_reg(fast_pool, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) fast_mix(fast_pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) add_interrupt_bench(cycles);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) if (unlikely(crng_init == 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) if ((fast_pool->count >= 64) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) crng_fast_load((char *) fast_pool->pool,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) sizeof(fast_pool->pool)) > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) fast_pool->count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) fast_pool->last = now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) if ((fast_pool->count < 64) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) !time_after(now, fast_pool->last + HZ))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) r = &input_pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) if (!spin_trylock(&r->lock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) fast_pool->last = now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) __mix_pool_bytes(r, &fast_pool->pool, sizeof(fast_pool->pool));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) * If we have architectural seed generator, produce a seed and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) * add it to the pool. For the sake of paranoia don't let the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) * architectural seed generator dominate the input from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) * interrupt noise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) if (arch_get_random_seed_long(&seed)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) __mix_pool_bytes(r, &seed, sizeof(seed));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) credit = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) spin_unlock(&r->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) fast_pool->count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) /* award one bit for the contents of the fast pool */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) credit_entropy_bits(r, credit + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) EXPORT_SYMBOL_GPL(add_interrupt_randomness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) #ifdef CONFIG_BLOCK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) void add_disk_randomness(struct gendisk *disk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) if (!disk || !disk->random)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) /* first major is 1, so we get >= 0x200 here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) add_timer_randomness(disk->random, 0x100 + disk_devt(disk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) trace_add_disk_randomness(disk_devt(disk), ENTROPY_BITS(&input_pool));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) EXPORT_SYMBOL_GPL(add_disk_randomness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) /*********************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) * Entropy extraction routines
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) *********************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) * This function decides how many bytes to actually take from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) * given pool, and also debits the entropy count accordingly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) static size_t account(struct entropy_store *r, size_t nbytes, int min,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) int reserved)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) int entropy_count, orig, have_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) size_t ibytes, nfrac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) BUG_ON(r->entropy_count > r->poolinfo->poolfracbits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) /* Can we pull enough? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) entropy_count = orig = READ_ONCE(r->entropy_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) ibytes = nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) /* never pull more than available */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) have_bytes = entropy_count >> (ENTROPY_SHIFT + 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) if ((have_bytes -= reserved) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) have_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) ibytes = min_t(size_t, ibytes, have_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) if (ibytes < min)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) ibytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) if (WARN_ON(entropy_count < 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) pr_warn("negative entropy count: pool %s count %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) r->name, entropy_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) entropy_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) nfrac = ibytes << (ENTROPY_SHIFT + 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) if ((size_t) entropy_count > nfrac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) entropy_count -= nfrac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) entropy_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) if (cmpxchg(&r->entropy_count, orig, entropy_count) != orig)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) trace_debit_entropy(r->name, 8 * ibytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) if (ibytes && ENTROPY_BITS(r) < random_write_wakeup_bits) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) wake_up_interruptible(&random_write_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) kill_fasync(&fasync, SIGIO, POLL_OUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) return ibytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) * This function does the actual extraction for extract_entropy and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) * extract_entropy_user.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) * Note: we assume that .poolwords is a multiple of 16 words.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) static void extract_buf(struct entropy_store *r, __u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) __u32 w[5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) unsigned long l[LONGS(20)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) } hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) __u32 workspace[SHA1_WORKSPACE_WORDS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) * If we have an architectural hardware random number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) * generator, use it for SHA's initial vector
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) sha1_init(hash.w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) for (i = 0; i < LONGS(20); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) unsigned long v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) if (!arch_get_random_long(&v))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) hash.l[i] = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) /* Generate a hash across the pool, 16 words (512 bits) at a time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) spin_lock_irqsave(&r->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) for (i = 0; i < r->poolinfo->poolwords; i += 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) sha1_transform(hash.w, (__u8 *)(r->pool + i), workspace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) * We mix the hash back into the pool to prevent backtracking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) * attacks (where the attacker knows the state of the pool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) * plus the current outputs, and attempts to find previous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) * ouputs), unless the hash function can be inverted. By
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) * mixing at least a SHA1 worth of hash data back, we make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) * brute-forcing the feedback as hard as brute-forcing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) * hash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) __mix_pool_bytes(r, hash.w, sizeof(hash.w));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) spin_unlock_irqrestore(&r->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) memzero_explicit(workspace, sizeof(workspace));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) * In case the hash function has some recognizable output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) * pattern, we fold it in half. Thus, we always feed back
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) * twice as much data as we output.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) hash.w[0] ^= hash.w[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) hash.w[1] ^= hash.w[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) hash.w[2] ^= rol32(hash.w[2], 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) memcpy(out, &hash, EXTRACT_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) memzero_explicit(&hash, sizeof(hash));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) static ssize_t _extract_entropy(struct entropy_store *r, void *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) size_t nbytes, int fips)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) ssize_t ret = 0, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) __u8 tmp[EXTRACT_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) while (nbytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) extract_buf(r, tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) if (fips) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) spin_lock_irqsave(&r->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) if (!memcmp(tmp, r->last_data, EXTRACT_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) panic("Hardware RNG duplicated output!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) memcpy(r->last_data, tmp, EXTRACT_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) spin_unlock_irqrestore(&r->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) i = min_t(int, nbytes, EXTRACT_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) memcpy(buf, tmp, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) nbytes -= i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) buf += i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) ret += i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) /* Wipe data just returned from memory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) memzero_explicit(tmp, sizeof(tmp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) * This function extracts randomness from the "entropy pool", and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) * returns it in a buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) * The min parameter specifies the minimum amount we can pull before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) * failing to avoid races that defeat catastrophic reseeding while the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) * reserved parameter indicates how much entropy we must leave in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) * pool after each pull to avoid starving other readers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) static ssize_t extract_entropy(struct entropy_store *r, void *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) size_t nbytes, int min, int reserved)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) __u8 tmp[EXTRACT_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) /* if last_data isn't primed, we need EXTRACT_SIZE extra bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) if (fips_enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) spin_lock_irqsave(&r->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) if (!r->last_data_init) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) r->last_data_init = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) spin_unlock_irqrestore(&r->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) trace_extract_entropy(r->name, EXTRACT_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) ENTROPY_BITS(r), _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) extract_buf(r, tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) spin_lock_irqsave(&r->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) memcpy(r->last_data, tmp, EXTRACT_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) spin_unlock_irqrestore(&r->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) trace_extract_entropy(r->name, nbytes, ENTROPY_BITS(r), _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) nbytes = account(r, nbytes, min, reserved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) return _extract_entropy(r, buf, nbytes, fips_enabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) #define warn_unseeded_randomness(previous) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) _warn_unseeded_randomness(__func__, (void *) _RET_IP_, (previous))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) static void _warn_unseeded_randomness(const char *func_name, void *caller,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) void **previous)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) #ifdef CONFIG_WARN_ALL_UNSEEDED_RANDOM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) const bool print_once = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) static bool print_once __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) if (print_once ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) crng_ready() ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) (previous && (caller == READ_ONCE(*previous))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) WRITE_ONCE(*previous, caller);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) #ifndef CONFIG_WARN_ALL_UNSEEDED_RANDOM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) print_once = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) if (__ratelimit(&unseeded_warning))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) printk_deferred(KERN_NOTICE "random: %s called from %pS "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) "with crng_init=%d\n", func_name, caller,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) crng_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) * This function is the exported kernel interface. It returns some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) * number of good random numbers, suitable for key generation, seeding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) * TCP sequence numbers, etc. It does not rely on the hardware random
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) * number generator. For random bytes direct from the hardware RNG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) * (when available), use get_random_bytes_arch(). In order to ensure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) * that the randomness provided by this function is okay, the function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) * wait_for_random_bytes() should be called and return 0 at least once
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) * at any point prior.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) static void _get_random_bytes(void *buf, int nbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) __u8 tmp[CHACHA_BLOCK_SIZE] __aligned(4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) trace_get_random_bytes(nbytes, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) while (nbytes >= CHACHA_BLOCK_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) extract_crng(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) buf += CHACHA_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) nbytes -= CHACHA_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) if (nbytes > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) extract_crng(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) memcpy(buf, tmp, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) crng_backtrack_protect(tmp, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) crng_backtrack_protect(tmp, CHACHA_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) memzero_explicit(tmp, sizeof(tmp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) void get_random_bytes(void *buf, int nbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) static void *previous;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) warn_unseeded_randomness(&previous);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) _get_random_bytes(buf, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) EXPORT_SYMBOL(get_random_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) * Each time the timer fires, we expect that we got an unpredictable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) * jump in the cycle counter. Even if the timer is running on another
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) * CPU, the timer activity will be touching the stack of the CPU that is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) * generating entropy..
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) * Note that we don't re-arm the timer in the timer itself - we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) * happy to be scheduled away, since that just makes the load more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) * complex, but we do not want the timer to keep ticking unless the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) * entropy loop is running.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) * So the re-arming always happens in the entropy loop itself.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) static void entropy_timer(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) credit_entropy_bits(&input_pool, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) * If we have an actual cycle counter, see if we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) * generate enough entropy with timing noise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) static void try_to_generate_entropy(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) unsigned long now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) struct timer_list timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) } stack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) stack.now = random_get_entropy();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) /* Slow counter - or none. Don't even bother */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) if (stack.now == random_get_entropy())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) timer_setup_on_stack(&stack.timer, entropy_timer, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) while (!crng_ready()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) if (!timer_pending(&stack.timer))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) mod_timer(&stack.timer, jiffies+1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) mix_pool_bytes(&input_pool, &stack.now, sizeof(stack.now));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) stack.now = random_get_entropy();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) del_timer_sync(&stack.timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) destroy_timer_on_stack(&stack.timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) mix_pool_bytes(&input_pool, &stack.now, sizeof(stack.now));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) * Wait for the urandom pool to be seeded and thus guaranteed to supply
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) * cryptographically secure random numbers. This applies to: the /dev/urandom
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) * device, the get_random_bytes function, and the get_random_{u32,u64,int,long}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) * family of functions. Using any of these functions without first calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) * this function forfeits the guarantee of security.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) * Returns: 0 if the urandom pool has been seeded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) * -ERESTARTSYS if the function was interrupted by a signal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) int wait_for_random_bytes(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) if (likely(crng_ready()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) ret = wait_event_interruptible_timeout(crng_init_wait, crng_ready(), HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) return ret > 0 ? 0 : ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) try_to_generate_entropy();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) } while (!crng_ready());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) EXPORT_SYMBOL(wait_for_random_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) * Returns whether or not the urandom pool has been seeded and thus guaranteed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) * to supply cryptographically secure random numbers. This applies to: the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) * /dev/urandom device, the get_random_bytes function, and the get_random_{u32,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) * ,u64,int,long} family of functions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) * Returns: true if the urandom pool has been seeded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) * false if the urandom pool has not been seeded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) bool rng_is_initialized(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) return crng_ready();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) EXPORT_SYMBOL(rng_is_initialized);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) * Add a callback function that will be invoked when the nonblocking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) * pool is initialised.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) * returns: 0 if callback is successfully added
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) * -EALREADY if pool is already initialised (callback not called)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) * -ENOENT if module for callback is not alive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) int add_random_ready_callback(struct random_ready_callback *rdy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) struct module *owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) int err = -EALREADY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) if (crng_ready())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) owner = rdy->owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) if (!try_module_get(owner))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) spin_lock_irqsave(&random_ready_list_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) if (crng_ready())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) owner = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) list_add(&rdy->list, &random_ready_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) spin_unlock_irqrestore(&random_ready_list_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) module_put(owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) EXPORT_SYMBOL(add_random_ready_callback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) * Delete a previously registered readiness callback function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) void del_random_ready_callback(struct random_ready_callback *rdy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) struct module *owner = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) spin_lock_irqsave(&random_ready_list_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) if (!list_empty(&rdy->list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) list_del_init(&rdy->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) owner = rdy->owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) spin_unlock_irqrestore(&random_ready_list_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) module_put(owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) EXPORT_SYMBOL(del_random_ready_callback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) * This function will use the architecture-specific hardware random
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) * number generator if it is available. The arch-specific hw RNG will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) * almost certainly be faster than what we can do in software, but it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) * is impossible to verify that it is implemented securely (as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) * opposed, to, say, the AES encryption of a sequence number using a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) * key known by the NSA). So it's useful if we need the speed, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) * only if we're willing to trust the hardware manufacturer not to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) * have put in a back door.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) * Return number of bytes filled in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) int __must_check get_random_bytes_arch(void *buf, int nbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) int left = nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) char *p = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) trace_get_random_bytes_arch(left, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) while (left) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) unsigned long v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) int chunk = min_t(int, left, sizeof(unsigned long));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) if (!arch_get_random_long(&v))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) memcpy(p, &v, chunk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) p += chunk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) left -= chunk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) return nbytes - left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) EXPORT_SYMBOL(get_random_bytes_arch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) * init_std_data - initialize pool with system data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) * @r: pool to initialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) * This function clears the pool's entropy count and mixes some system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) * data into the pool to prepare it for use. The pool is not cleared
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) * as that can only decrease the entropy in the pool.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) static void __init init_std_data(struct entropy_store *r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) ktime_t now = ktime_get_real();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) unsigned long rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) mix_pool_bytes(r, &now, sizeof(now));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) for (i = r->poolinfo->poolbytes; i > 0; i -= sizeof(rv)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) if (!arch_get_random_seed_long(&rv) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) !arch_get_random_long(&rv))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) rv = random_get_entropy();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) mix_pool_bytes(r, &rv, sizeof(rv));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) mix_pool_bytes(r, utsname(), sizeof(*(utsname())));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) * Note that setup_arch() may call add_device_randomness()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) * long before we get here. This allows seeding of the pools
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) * with some platform dependent data very early in the boot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) * process. But it limits our options here. We must use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) * statically allocated structures that already have all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) * initializations complete at compile time. We should also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) * take care not to overwrite the precious per platform data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) * we were given.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) int __init rand_initialize(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) init_std_data(&input_pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) if (crng_need_final_init)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) crng_finalize_init(&primary_crng);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) crng_initialize_primary(&primary_crng);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) crng_global_init_time = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) if (ratelimit_disable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) urandom_warning.interval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) unseeded_warning.interval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) #ifdef CONFIG_BLOCK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) void rand_initialize_disk(struct gendisk *disk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) struct timer_rand_state *state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) * If kzalloc returns null, we just won't use that entropy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) * source.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) state = kzalloc(sizeof(struct timer_rand_state), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) if (state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) state->last_time = INITIAL_JIFFIES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) disk->random = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) urandom_read_nowarn(struct file *file, char __user *buf, size_t nbytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) nbytes = min_t(size_t, nbytes, INT_MAX >> (ENTROPY_SHIFT + 3));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) ret = extract_crng_user(buf, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) trace_urandom_read(8 * nbytes, 0, ENTROPY_BITS(&input_pool));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) urandom_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) static int maxwarn = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) if (!crng_ready() && maxwarn > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) maxwarn--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) if (__ratelimit(&urandom_warning))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) pr_notice("%s: uninitialized urandom read (%zd bytes read)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) current->comm, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) spin_lock_irqsave(&primary_crng.lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) crng_init_cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) spin_unlock_irqrestore(&primary_crng.lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) return urandom_read_nowarn(file, buf, nbytes, ppos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) random_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) ret = wait_for_random_bytes();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) return urandom_read_nowarn(file, buf, nbytes, ppos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) static __poll_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) random_poll(struct file *file, poll_table * wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) __poll_t mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) poll_wait(file, &crng_init_wait, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) poll_wait(file, &random_write_wait, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) if (crng_ready())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) mask |= EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) if (ENTROPY_BITS(&input_pool) < random_write_wakeup_bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) mask |= EPOLLOUT | EPOLLWRNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) return mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) write_pool(struct entropy_store *r, const char __user *buffer, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) size_t bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) __u32 t, buf[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) const char __user *p = buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) while (count > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) int b, i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) bytes = min(count, sizeof(buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) if (copy_from_user(&buf, p, bytes))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) for (b = bytes ; b > 0 ; b -= sizeof(__u32), i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) if (!arch_get_random_int(&t))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) buf[i] ^= t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) count -= bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) p += bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) mix_pool_bytes(r, buf, bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) static ssize_t random_write(struct file *file, const char __user *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) size_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) ret = write_pool(&input_pool, buffer, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) return (ssize_t)count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) int size, ent_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) int __user *p = (int __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) case RNDGETENTCNT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) /* inherently racy, no point locking */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) ent_count = ENTROPY_BITS(&input_pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) if (put_user(ent_count, p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) case RNDADDTOENTCNT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) if (get_user(ent_count, p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) return credit_entropy_bits_safe(&input_pool, ent_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) case RNDADDENTROPY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) if (get_user(ent_count, p++))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) if (ent_count < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) if (get_user(size, p++))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) retval = write_pool(&input_pool, (const char __user *)p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) if (retval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) return credit_entropy_bits_safe(&input_pool, ent_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) case RNDZAPENTCNT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) case RNDCLEARPOOL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) * Clear the entropy pool counters. We no longer clear
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) * the entropy pool, as that's silly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) if (xchg(&input_pool.entropy_count, 0) && random_write_wakeup_bits) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) wake_up_interruptible(&random_write_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) kill_fasync(&fasync, SIGIO, POLL_OUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) case RNDRESEEDCRNG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) if (crng_init < 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) crng_reseed(&primary_crng, &input_pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) WRITE_ONCE(crng_global_init_time, jiffies - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) static int random_fasync(int fd, struct file *filp, int on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) return fasync_helper(fd, filp, on, &fasync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) const struct file_operations random_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) .read = random_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) .write = random_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) .poll = random_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) .unlocked_ioctl = random_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) .compat_ioctl = compat_ptr_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) .fasync = random_fasync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) .llseek = noop_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) const struct file_operations urandom_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) .read = urandom_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) .write = random_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) .unlocked_ioctl = random_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) .compat_ioctl = compat_ptr_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) .fasync = random_fasync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) .llseek = noop_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) SYSCALL_DEFINE3(getrandom, char __user *, buf, size_t, count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) unsigned int, flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) if (flags & ~(GRND_NONBLOCK|GRND_RANDOM|GRND_INSECURE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) * Requesting insecure and blocking randomness at the same time makes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) * no sense.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) if ((flags & (GRND_INSECURE|GRND_RANDOM)) == (GRND_INSECURE|GRND_RANDOM))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) if (count > INT_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) count = INT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) if (!(flags & GRND_INSECURE) && !crng_ready()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) if (flags & GRND_NONBLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) ret = wait_for_random_bytes();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) if (unlikely(ret))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) return urandom_read_nowarn(NULL, buf, count, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) /********************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) * Sysctl interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) ********************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) #ifdef CONFIG_SYSCTL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) #include <linux/sysctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) static int min_write_thresh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) static int max_write_thresh = INPUT_POOL_WORDS * 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) static int random_min_urandom_seed = 60;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) static char sysctl_bootid[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) * This function is used to return both the bootid UUID, and random
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) * UUID. The difference is in whether table->data is NULL; if it is,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) * then a new UUID is generated and returned to the user.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) * If the user accesses this via the proc interface, the UUID will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) * returned as an ASCII string in the standard UUID format; if via the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) * sysctl system call, as 16 bytes of binary data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) static int proc_do_uuid(struct ctl_table *table, int write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) void *buffer, size_t *lenp, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) struct ctl_table fake_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) unsigned char buf[64], tmp_uuid[16], *uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) uuid = table->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) if (!uuid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) uuid = tmp_uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) generate_random_uuid(uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) static DEFINE_SPINLOCK(bootid_spinlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) spin_lock(&bootid_spinlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) if (!uuid[8])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) generate_random_uuid(uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) spin_unlock(&bootid_spinlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) sprintf(buf, "%pU", uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) fake_table.data = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) fake_table.maxlen = sizeof(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) return proc_dostring(&fake_table, write, buffer, lenp, ppos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) * Return entropy available scaled to integral bits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) static int proc_do_entropy(struct ctl_table *table, int write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) void *buffer, size_t *lenp, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) struct ctl_table fake_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) int entropy_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) entropy_count = *(int *)table->data >> ENTROPY_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) fake_table.data = &entropy_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) fake_table.maxlen = sizeof(entropy_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) return proc_dointvec(&fake_table, write, buffer, lenp, ppos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) static int sysctl_poolsize = INPUT_POOL_WORDS * 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) extern struct ctl_table random_table[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) struct ctl_table random_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) .procname = "poolsize",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) .data = &sysctl_poolsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) .maxlen = sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) .mode = 0444,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) .proc_handler = proc_dointvec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) .procname = "entropy_avail",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) .maxlen = sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) .mode = 0444,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) .proc_handler = proc_do_entropy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) .data = &input_pool.entropy_count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) .procname = "write_wakeup_threshold",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) .data = &random_write_wakeup_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) .maxlen = sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) .mode = 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) .proc_handler = proc_dointvec_minmax,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) .extra1 = &min_write_thresh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) .extra2 = &max_write_thresh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) .procname = "urandom_min_reseed_secs",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) .data = &random_min_urandom_seed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) .maxlen = sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) .mode = 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) .proc_handler = proc_dointvec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) .procname = "boot_id",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) .data = &sysctl_bootid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) .maxlen = 16,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) .mode = 0444,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) .proc_handler = proc_do_uuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) .procname = "uuid",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) .maxlen = 16,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) .mode = 0444,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) .proc_handler = proc_do_uuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) #ifdef ADD_INTERRUPT_BENCH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) .procname = "add_interrupt_avg_cycles",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) .data = &avg_cycles,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) .maxlen = sizeof(avg_cycles),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) .mode = 0444,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) .proc_handler = proc_doulongvec_minmax,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) .procname = "add_interrupt_avg_deviation",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) .data = &avg_deviation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) .maxlen = sizeof(avg_deviation),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) .mode = 0444,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) .proc_handler = proc_doulongvec_minmax,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) #endif /* CONFIG_SYSCTL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) struct batched_entropy {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) u64 entropy_u64[CHACHA_BLOCK_SIZE / sizeof(u64)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) u32 entropy_u32[CHACHA_BLOCK_SIZE / sizeof(u32)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) unsigned int position;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) spinlock_t batch_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) * Get a random word for internal kernel use only. The quality of the random
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) * number is good as /dev/urandom, but there is no backtrack protection, with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) * the goal of being quite fast and not depleting entropy. In order to ensure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) * that the randomness provided by this function is okay, the function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) * wait_for_random_bytes() should be called and return 0 at least once at any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) * point prior.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_u64) = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) .batch_lock = __SPIN_LOCK_UNLOCKED(batched_entropy_u64.lock),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) u64 get_random_u64(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) u64 ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) struct batched_entropy *batch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) static void *previous;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) warn_unseeded_randomness(&previous);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) batch = raw_cpu_ptr(&batched_entropy_u64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) spin_lock_irqsave(&batch->batch_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) if (batch->position % ARRAY_SIZE(batch->entropy_u64) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) extract_crng((u8 *)batch->entropy_u64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) batch->position = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) ret = batch->entropy_u64[batch->position++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) spin_unlock_irqrestore(&batch->batch_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) EXPORT_SYMBOL(get_random_u64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_u32) = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) .batch_lock = __SPIN_LOCK_UNLOCKED(batched_entropy_u32.lock),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) u32 get_random_u32(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) u32 ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) struct batched_entropy *batch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) static void *previous;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) warn_unseeded_randomness(&previous);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) batch = raw_cpu_ptr(&batched_entropy_u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) spin_lock_irqsave(&batch->batch_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) if (batch->position % ARRAY_SIZE(batch->entropy_u32) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) extract_crng((u8 *)batch->entropy_u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) batch->position = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) ret = batch->entropy_u32[batch->position++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) spin_unlock_irqrestore(&batch->batch_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) EXPORT_SYMBOL(get_random_u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) /* It's important to invalidate all potential batched entropy that might
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) * be stored before the crng is initialized, which we can do lazily by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) * simply resetting the counter to zero so that it's re-extracted on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) * next usage. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) static void invalidate_batched_entropy(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) for_each_possible_cpu (cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) struct batched_entropy *batched_entropy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) batched_entropy = per_cpu_ptr(&batched_entropy_u32, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) spin_lock_irqsave(&batched_entropy->batch_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) batched_entropy->position = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) spin_unlock(&batched_entropy->batch_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) batched_entropy = per_cpu_ptr(&batched_entropy_u64, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) spin_lock(&batched_entropy->batch_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) batched_entropy->position = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) spin_unlock_irqrestore(&batched_entropy->batch_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) * randomize_page - Generate a random, page aligned address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) * @start: The smallest acceptable address the caller will take.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) * @range: The size of the area, starting at @start, within which the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) * random address must fall.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) * If @start + @range would overflow, @range is capped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) * NOTE: Historical use of randomize_range, which this replaces, presumed that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) * @start was already page aligned. We now align it regardless.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) * Return: A page aligned address within [start, start + range). On error,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) * @start is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) unsigned long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) randomize_page(unsigned long start, unsigned long range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) if (!PAGE_ALIGNED(start)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) range -= PAGE_ALIGN(start) - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) start = PAGE_ALIGN(start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) if (start > ULONG_MAX - range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) range = ULONG_MAX - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) range >>= PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) if (range == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) return start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) return start + (get_random_long() % range << PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) /* Interface for in-kernel drivers of true hardware RNGs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) * Those devices may produce endless random bits and will be throttled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) * when our pool is full.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) void add_hwgenerator_randomness(const char *buffer, size_t count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) size_t entropy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) struct entropy_store *poolp = &input_pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) if (unlikely(crng_init == 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) size_t ret = crng_fast_load(buffer, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) count -= ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) buffer += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) if (!count || crng_init == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) /* Suspend writing if we're above the trickle threshold.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) * We'll be woken up again once below random_write_wakeup_thresh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) * or when the calling thread is about to terminate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) wait_event_interruptible(random_write_wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) !system_wq || kthread_should_stop() ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) ENTROPY_BITS(&input_pool) <= random_write_wakeup_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) mix_pool_bytes(poolp, buffer, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) credit_entropy_bits(poolp, entropy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) EXPORT_SYMBOL_GPL(add_hwgenerator_randomness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) /* Handle random seed passed by bootloader.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) * If the seed is trustworthy, it would be regarded as hardware RNGs. Otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) * it would be regarded as device data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) * The decision is controlled by CONFIG_RANDOM_TRUST_BOOTLOADER.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) void add_bootloader_randomness(const void *buf, unsigned int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) if (IS_ENABLED(CONFIG_RANDOM_TRUST_BOOTLOADER))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) add_hwgenerator_randomness(buf, size, size * 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) add_device_randomness(buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) EXPORT_SYMBOL_GPL(add_bootloader_randomness);