Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * random.c -- A strong random number generator | |
3 | * | |
9f9eff85 | 4 | * Copyright (C) 2017-2022 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. |
b169c13d | 5 | * |
9e95ce27 | 6 | * Copyright Matt Mackall <mpm@selenic.com>, 2003, 2004, 2005 |
1da177e4 LT |
7 | * |
8 | * Copyright Theodore Ts'o, 1994, 1995, 1996, 1997, 1998, 1999. All | |
9 | * rights reserved. | |
10 | * | |
11 | * Redistribution and use in source and binary forms, with or without | |
12 | * modification, are permitted provided that the following conditions | |
13 | * are met: | |
14 | * 1. Redistributions of source code must retain the above copyright | |
15 | * notice, and the entire permission notice in its entirety, | |
16 | * including the disclaimer of warranties. | |
17 | * 2. Redistributions in binary form must reproduce the above copyright | |
18 | * notice, this list of conditions and the following disclaimer in the | |
19 | * documentation and/or other materials provided with the distribution. | |
20 | * 3. The name of the author may not be used to endorse or promote | |
21 | * products derived from this software without specific prior | |
22 | * written permission. | |
23 | * | |
24 | * ALTERNATIVELY, this product may be distributed under the terms of | |
25 | * the GNU General Public License, in which case the provisions of the GPL are | |
26 | * required INSTEAD OF the above restrictions. (This clause is | |
27 | * necessary due to a potential bad interaction between the GPL and | |
28 | * the restrictions contained in a BSD-style copyright.) | |
29 | * | |
30 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED | |
31 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
32 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF | |
33 | * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE | |
34 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
35 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT | |
36 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR | |
37 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
38 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
39 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE | |
40 | * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH | |
41 | * DAMAGE. | |
42 | */ | |
43 | ||
44 | /* | |
1da177e4 LT |
45 | * Exported interfaces ---- output |
46 | * =============================== | |
47 | * | |
92e507d2 | 48 | * There are four exported interfaces; two for use within the kernel, |
c0a8a61e | 49 | * and two for use from userspace. |
1da177e4 | 50 | * |
92e507d2 GS |
51 | * Exported interfaces ---- userspace output |
52 | * ----------------------------------------- | |
1da177e4 | 53 | * |
92e507d2 | 54 | * The userspace interfaces are two character devices /dev/random and |
1da177e4 LT |
55 | * /dev/urandom. /dev/random is suitable for use when very high |
56 | * quality randomness is desired (for example, for key generation or | |
57 | * one-time pads), as it will only return a maximum of the number of | |
58 | * bits of randomness (as estimated by the random number generator) | |
59 | * contained in the entropy pool. | |
60 | * | |
61 | * The /dev/urandom device does not have this limit, and will return | |
62 | * as many bytes as are requested. As more and more random bytes are | |
63 | * requested without giving time for the entropy pool to recharge, | |
64 | * this will result in random numbers that are merely cryptographically | |
65 | * strong. For many applications, however, this is acceptable. | |
66 | * | |
92e507d2 GS |
67 | * Exported interfaces ---- kernel output |
68 | * -------------------------------------- | |
69 | * | |
70 | * The primary kernel interface is | |
71 | * | |
248045b8 | 72 | * void get_random_bytes(void *buf, int nbytes); |
92e507d2 GS |
73 | * |
74 | * This interface will return the requested number of random bytes, | |
75 | * and place it in the requested buffer. This is equivalent to a | |
76 | * read from /dev/urandom. | |
77 | * | |
78 | * For less critical applications, there are the functions: | |
79 | * | |
248045b8 JD |
80 | * u32 get_random_u32() |
81 | * u64 get_random_u64() | |
82 | * unsigned int get_random_int() | |
83 | * unsigned long get_random_long() | |
92e507d2 GS |
84 | * |
85 | * These are produced by a cryptographic RNG seeded from get_random_bytes, | |
86 | * and so do not deplete the entropy pool as much. These are recommended | |
87 | * for most in-kernel operations *if the result is going to be stored in | |
88 | * the kernel*. | |
89 | * | |
90 | * Specifically, the get_random_int() family do not attempt to do | |
91 | * "anti-backtracking". If you capture the state of the kernel (e.g. | |
92 | * by snapshotting the VM), you can figure out previous get_random_int() | |
93 | * return values. But if the value is stored in the kernel anyway, | |
94 | * this is not a problem. | |
95 | * | |
96 | * It *is* safe to expose get_random_int() output to attackers (e.g. as | |
97 | * network cookies); given outputs 1..n, it's not feasible to predict | |
98 | * outputs 0 or n+1. The only concern is an attacker who breaks into | |
99 | * the kernel later; the get_random_int() engine is not reseeded as | |
100 | * often as the get_random_bytes() one. | |
101 | * | |
102 | * get_random_bytes() is needed for keys that need to stay secret after | |
103 | * they are erased from the kernel. For example, any key that will | |
104 | * be wrapped and stored encrypted. And session encryption keys: we'd | |
105 | * like to know that after the session is closed and the keys erased, | |
106 | * the plaintext is unrecoverable to someone who recorded the ciphertext. | |
107 | * | |
108 | * But for network ports/cookies, stack canaries, PRNG seeds, address | |
109 | * space layout randomization, session *authentication* keys, or other | |
110 | * applications where the sensitive data is stored in the kernel in | |
111 | * plaintext for as long as it's sensitive, the get_random_int() family | |
112 | * is just fine. | |
113 | * | |
114 | * Consider ASLR. We want to keep the address space secret from an | |
115 | * outside attacker while the process is running, but once the address | |
116 | * space is torn down, it's of no use to an attacker any more. And it's | |
117 | * stored in kernel data structures as long as it's alive, so worrying | |
118 | * about an attacker's ability to extrapolate it from the get_random_int() | |
119 | * CRNG is silly. | |
120 | * | |
121 | * Even some cryptographic keys are safe to generate with get_random_int(). | |
122 | * In particular, keys for SipHash are generally fine. Here, knowledge | |
123 | * of the key authorizes you to do something to a kernel object (inject | |
124 | * packets to a network connection, or flood a hash table), and the | |
125 | * key is stored with the object being protected. Once it goes away, | |
126 | * we no longer care if anyone knows the key. | |
127 | * | |
128 | * prandom_u32() | |
129 | * ------------- | |
130 | * | |
131 | * For even weaker applications, see the pseudorandom generator | |
132 | * prandom_u32(), prandom_max(), and prandom_bytes(). If the random | |
133 | * numbers aren't security-critical at all, these are *far* cheaper. | |
134 | * Useful for self-tests, random error simulation, randomized backoffs, | |
135 | * and any other application where you trust that nobody is trying to | |
136 | * maliciously mess with you by guessing the "random" numbers. | |
137 | * | |
1da177e4 LT |
138 | * Exported interfaces ---- input |
139 | * ============================== | |
140 | * | |
141 | * The current exported interfaces for gathering environmental noise | |
142 | * from the devices are: | |
143 | * | |
a2080a67 | 144 | * void add_device_randomness(const void *buf, unsigned int size); |
248045b8 | 145 | * void add_input_randomness(unsigned int type, unsigned int code, |
1da177e4 | 146 | * unsigned int value); |
703f7066 | 147 | * void add_interrupt_randomness(int irq); |
248045b8 | 148 | * void add_disk_randomness(struct gendisk *disk); |
2b6c6e3d MB |
149 | * void add_hwgenerator_randomness(const char *buffer, size_t count, |
150 | * size_t entropy); | |
151 | * void add_bootloader_randomness(const void *buf, unsigned int size); | |
1da177e4 | 152 | * |
a2080a67 LT |
153 | * add_device_randomness() is for adding data to the random pool that |
154 | * is likely to differ between two devices (or possibly even per boot). | |
155 | * This would be things like MAC addresses or serial numbers, or the | |
156 | * read-out of the RTC. This does *not* add any actual entropy to the | |
157 | * pool, but it initializes the pool to different values for devices | |
158 | * that might otherwise be identical and have very little entropy | |
159 | * available to them (particularly common in the embedded world). | |
160 | * | |
1da177e4 LT |
161 | * add_input_randomness() uses the input layer interrupt timing, as well as |
162 | * the event type information from the hardware. | |
163 | * | |
775f4b29 TT |
164 | * add_interrupt_randomness() uses the interrupt timing as random |
165 | * inputs to the entropy pool. Using the cycle counters and the irq source | |
166 | * as inputs, it feeds the randomness roughly once a second. | |
442a4fff JW |
167 | * |
168 | * add_disk_randomness() uses what amounts to the seek time of block | |
169 | * layer request events, on a per-disk_devt basis, as input to the | |
170 | * entropy pool. Note that high-speed solid state drives with very low | |
171 | * seek times do not make for good sources of entropy, as their seek | |
172 | * times are usually fairly consistent. | |
1da177e4 LT |
173 | * |
174 | * All of these routines try to estimate how many bits of randomness a | |
175 | * particular randomness source. They do this by keeping track of the | |
176 | * first and second order deltas of the event timings. | |
177 | * | |
2b6c6e3d MB |
178 | * add_hwgenerator_randomness() is for true hardware RNGs, and will credit |
179 | * entropy as specified by the caller. If the entropy pool is full it will | |
180 | * block until more entropy is needed. | |
181 | * | |
182 | * add_bootloader_randomness() is the same as add_hwgenerator_randomness() or | |
183 | * add_device_randomness(), depending on whether or not the configuration | |
184 | * option CONFIG_RANDOM_TRUST_BOOTLOADER is set. | |
185 | * | |
1da177e4 LT |
186 | * Ensuring unpredictability at system startup |
187 | * ============================================ | |
188 | * | |
189 | * When any operating system starts up, it will go through a sequence | |
190 | * of actions that are fairly predictable by an adversary, especially | |
191 | * if the start-up does not involve interaction with a human operator. | |
192 | * This reduces the actual number of bits of unpredictability in the | |
193 | * entropy pool below the value in entropy_count. In order to | |
194 | * counteract this effect, it helps to carry information in the | |
195 | * entropy pool across shut-downs and start-ups. To do this, put the | |
196 | * following lines an appropriate script which is run during the boot | |
197 | * sequence: | |
198 | * | |
199 | * echo "Initializing random number generator..." | |
200 | * random_seed=/var/run/random-seed | |
201 | * # Carry a random seed from start-up to start-up | |
202 | * # Load and then save the whole entropy pool | |
203 | * if [ -f $random_seed ]; then | |
204 | * cat $random_seed >/dev/urandom | |
205 | * else | |
206 | * touch $random_seed | |
207 | * fi | |
208 | * chmod 600 $random_seed | |
209 | * dd if=/dev/urandom of=$random_seed count=1 bs=512 | |
210 | * | |
211 | * and the following lines in an appropriate script which is run as | |
212 | * the system is shutdown: | |
213 | * | |
214 | * # Carry a random seed from shut-down to start-up | |
215 | * # Save the whole entropy pool | |
216 | * echo "Saving random seed..." | |
217 | * random_seed=/var/run/random-seed | |
218 | * touch $random_seed | |
219 | * chmod 600 $random_seed | |
220 | * dd if=/dev/urandom of=$random_seed count=1 bs=512 | |
221 | * | |
222 | * For example, on most modern systems using the System V init | |
223 | * scripts, such code fragments would be found in | |
224 | * /etc/rc.d/init.d/random. On older Linux systems, the correct script | |
225 | * location might be in /etc/rcb.d/rc.local or /etc/rc.d/rc.0. | |
226 | * | |
227 | * Effectively, these commands cause the contents of the entropy pool | |
228 | * to be saved at shut-down time and reloaded into the entropy pool at | |
229 | * start-up. (The 'dd' in the addition to the bootup script is to | |
230 | * make sure that /etc/random-seed is different for every start-up, | |
231 | * even if the system crashes without executing rc.0.) Even with | |
232 | * complete knowledge of the start-up activities, predicting the state | |
233 | * of the entropy pool requires knowledge of the previous history of | |
234 | * the system. | |
235 | * | |
236 | * Configuring the /dev/random driver under Linux | |
237 | * ============================================== | |
238 | * | |
239 | * The /dev/random driver under Linux uses minor numbers 8 and 9 of | |
240 | * the /dev/mem major number (#1). So if your system does not have | |
241 | * /dev/random and /dev/urandom created already, they can be created | |
242 | * by using the commands: | |
243 | * | |
248045b8 JD |
244 | * mknod /dev/random c 1 8 |
245 | * mknod /dev/urandom c 1 9 | |
1da177e4 LT |
246 | */ |
247 | ||
12cd53af YL |
248 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
249 | ||
1da177e4 | 250 | #include <linux/utsname.h> |
1da177e4 LT |
251 | #include <linux/module.h> |
252 | #include <linux/kernel.h> | |
253 | #include <linux/major.h> | |
254 | #include <linux/string.h> | |
255 | #include <linux/fcntl.h> | |
256 | #include <linux/slab.h> | |
257 | #include <linux/random.h> | |
258 | #include <linux/poll.h> | |
259 | #include <linux/init.h> | |
260 | #include <linux/fs.h> | |
261 | #include <linux/genhd.h> | |
262 | #include <linux/interrupt.h> | |
27ac792c | 263 | #include <linux/mm.h> |
dd0f0cf5 | 264 | #include <linux/nodemask.h> |
1da177e4 | 265 | #include <linux/spinlock.h> |
c84dbf61 | 266 | #include <linux/kthread.h> |
1da177e4 | 267 | #include <linux/percpu.h> |
775f4b29 | 268 | #include <linux/ptrace.h> |
6265e169 | 269 | #include <linux/workqueue.h> |
0244ad00 | 270 | #include <linux/irq.h> |
4e00b339 | 271 | #include <linux/ratelimit.h> |
c6e9d6f3 TT |
272 | #include <linux/syscalls.h> |
273 | #include <linux/completion.h> | |
8da4b8c4 | 274 | #include <linux/uuid.h> |
1ca1b917 | 275 | #include <crypto/chacha.h> |
9f9eff85 | 276 | #include <crypto/blake2s.h> |
d178a1eb | 277 | |
1da177e4 | 278 | #include <asm/processor.h> |
7c0f6ba6 | 279 | #include <linux/uaccess.h> |
1da177e4 | 280 | #include <asm/irq.h> |
775f4b29 | 281 | #include <asm/irq_regs.h> |
1da177e4 LT |
282 | #include <asm/io.h> |
283 | ||
00ce1db1 TT |
284 | #define CREATE_TRACE_POINTS |
285 | #include <trace/events/random.h> | |
286 | ||
43759d4f TT |
287 | /* #define ADD_INTERRUPT_BENCH */ |
288 | ||
c5704490 | 289 | enum { |
6e8ec255 | 290 | POOL_BITS = BLAKE2S_HASH_SIZE * 8, |
c5704490 | 291 | POOL_MIN_BITS = POOL_BITS /* No point in settling for less. */ |
1da177e4 LT |
292 | }; |
293 | ||
1da177e4 LT |
294 | /* |
295 | * Static global variables | |
296 | */ | |
a11e1d43 | 297 | static DECLARE_WAIT_QUEUE_HEAD(random_write_wait); |
9a6f70bb | 298 | static struct fasync_struct *fasync; |
1da177e4 | 299 | |
205a525c HX |
300 | static DEFINE_SPINLOCK(random_ready_list_lock); |
301 | static LIST_HEAD(random_ready_list); | |
302 | ||
e192be9d | 303 | struct crng_state { |
248045b8 JD |
304 | u32 state[16]; |
305 | unsigned long init_time; | |
306 | spinlock_t lock; | |
e192be9d TT |
307 | }; |
308 | ||
764ed189 | 309 | static struct crng_state primary_crng = { |
e192be9d | 310 | .lock = __SPIN_LOCK_UNLOCKED(primary_crng.lock), |
96562f28 DB |
311 | .state[0] = CHACHA_CONSTANT_EXPA, |
312 | .state[1] = CHACHA_CONSTANT_ND_3, | |
313 | .state[2] = CHACHA_CONSTANT_2_BY, | |
314 | .state[3] = CHACHA_CONSTANT_TE_K, | |
e192be9d TT |
315 | }; |
316 | ||
317 | /* | |
318 | * crng_init = 0 --> Uninitialized | |
319 | * 1 --> Initialized | |
320 | * 2 --> Initialized from input_pool | |
321 | * | |
322 | * crng_init is protected by primary_crng->lock, and only increases | |
323 | * its value (from 0->1->2). | |
324 | */ | |
325 | static int crng_init = 0; | |
f7e67b8e | 326 | static bool crng_need_final_init = false; |
43838a23 | 327 | #define crng_ready() (likely(crng_init > 1)) |
e192be9d | 328 | static int crng_init_cnt = 0; |
d848e5f8 | 329 | static unsigned long crng_global_init_time = 0; |
248045b8 | 330 | #define CRNG_INIT_CNT_THRESH (2 * CHACHA_KEY_SIZE) |
d38bb085 | 331 | static void _extract_crng(struct crng_state *crng, u8 out[CHACHA_BLOCK_SIZE]); |
c92e040d | 332 | static void _crng_backtrack_protect(struct crng_state *crng, |
d38bb085 | 333 | u8 tmp[CHACHA_BLOCK_SIZE], int used); |
e192be9d | 334 | static void process_random_ready_list(void); |
eecabf56 | 335 | static void _get_random_bytes(void *buf, int nbytes); |
e192be9d | 336 | |
4e00b339 TT |
337 | static struct ratelimit_state unseeded_warning = |
338 | RATELIMIT_STATE_INIT("warn_unseeded_randomness", HZ, 3); | |
339 | static struct ratelimit_state urandom_warning = | |
340 | RATELIMIT_STATE_INIT("warn_urandom_randomness", HZ, 3); | |
341 | ||
342 | static int ratelimit_disable __read_mostly; | |
343 | ||
344 | module_param_named(ratelimit_disable, ratelimit_disable, int, 0644); | |
345 | MODULE_PARM_DESC(ratelimit_disable, "Disable random ratelimit suppression"); | |
346 | ||
1da177e4 LT |
347 | /********************************************************************** |
348 | * | |
349 | * OS independent entropy store. Here are the functions which handle | |
350 | * storing entropy in an entropy pool. | |
351 | * | |
352 | **********************************************************************/ | |
353 | ||
90ed1e67 | 354 | static struct { |
6e8ec255 | 355 | struct blake2s_state hash; |
43358209 | 356 | spinlock_t lock; |
cda796a3 | 357 | int entropy_count; |
90ed1e67 | 358 | } input_pool = { |
6e8ec255 JD |
359 | .hash.h = { BLAKE2S_IV0 ^ (0x01010000 | BLAKE2S_HASH_SIZE), |
360 | BLAKE2S_IV1, BLAKE2S_IV2, BLAKE2S_IV3, BLAKE2S_IV4, | |
361 | BLAKE2S_IV5, BLAKE2S_IV6, BLAKE2S_IV7 }, | |
362 | .hash.outlen = BLAKE2S_HASH_SIZE, | |
eece09ec | 363 | .lock = __SPIN_LOCK_UNLOCKED(input_pool.lock), |
1da177e4 LT |
364 | }; |
365 | ||
9c07f578 | 366 | static void extract_entropy(void *buf, size_t nbytes); |
90ed1e67 | 367 | |
5d58ea3a | 368 | static void crng_reseed(struct crng_state *crng); |
90ed1e67 | 369 | |
1da177e4 | 370 | /* |
e68e5b66 | 371 | * This function adds bytes into the entropy "pool". It does not |
1da177e4 | 372 | * update the entropy estimate. The caller should call |
adc782da | 373 | * credit_entropy_bits if this is appropriate. |
1da177e4 | 374 | */ |
90ed1e67 | 375 | static void _mix_pool_bytes(const void *in, int nbytes) |
1da177e4 | 376 | { |
6e8ec255 | 377 | blake2s_update(&input_pool.hash, in, nbytes); |
1da177e4 LT |
378 | } |
379 | ||
90ed1e67 | 380 | static void __mix_pool_bytes(const void *in, int nbytes) |
00ce1db1 | 381 | { |
90ed1e67 JD |
382 | trace_mix_pool_bytes_nolock(nbytes, _RET_IP_); |
383 | _mix_pool_bytes(in, nbytes); | |
00ce1db1 TT |
384 | } |
385 | ||
90ed1e67 | 386 | static void mix_pool_bytes(const void *in, int nbytes) |
1da177e4 | 387 | { |
902c098a TT |
388 | unsigned long flags; |
389 | ||
90ed1e67 JD |
390 | trace_mix_pool_bytes(nbytes, _RET_IP_); |
391 | spin_lock_irqsave(&input_pool.lock, flags); | |
392 | _mix_pool_bytes(in, nbytes); | |
393 | spin_unlock_irqrestore(&input_pool.lock, flags); | |
1da177e4 LT |
394 | } |
395 | ||
775f4b29 | 396 | struct fast_pool { |
248045b8 JD |
397 | u32 pool[4]; |
398 | unsigned long last; | |
399 | u16 reg_idx; | |
400 | u8 count; | |
775f4b29 TT |
401 | }; |
402 | ||
403 | /* | |
404 | * This is a fast mixing routine used by the interrupt randomness | |
405 | * collector. It's hardcoded for an 128 bit pool and assumes that any | |
406 | * locks that might be needed are taken by the caller. | |
407 | */ | |
43759d4f | 408 | static void fast_mix(struct fast_pool *f) |
775f4b29 | 409 | { |
d38bb085 JD |
410 | u32 a = f->pool[0], b = f->pool[1]; |
411 | u32 c = f->pool[2], d = f->pool[3]; | |
43759d4f TT |
412 | |
413 | a += b; c += d; | |
19acc77a | 414 | b = rol32(b, 6); d = rol32(d, 27); |
43759d4f TT |
415 | d ^= a; b ^= c; |
416 | ||
417 | a += b; c += d; | |
19acc77a | 418 | b = rol32(b, 16); d = rol32(d, 14); |
43759d4f TT |
419 | d ^= a; b ^= c; |
420 | ||
421 | a += b; c += d; | |
19acc77a | 422 | b = rol32(b, 6); d = rol32(d, 27); |
43759d4f TT |
423 | d ^= a; b ^= c; |
424 | ||
425 | a += b; c += d; | |
19acc77a | 426 | b = rol32(b, 16); d = rol32(d, 14); |
43759d4f TT |
427 | d ^= a; b ^= c; |
428 | ||
429 | f->pool[0] = a; f->pool[1] = b; | |
430 | f->pool[2] = c; f->pool[3] = d; | |
655b2264 | 431 | f->count++; |
775f4b29 TT |
432 | } |
433 | ||
205a525c HX |
434 | static void process_random_ready_list(void) |
435 | { | |
436 | unsigned long flags; | |
437 | struct random_ready_callback *rdy, *tmp; | |
438 | ||
439 | spin_lock_irqsave(&random_ready_list_lock, flags); | |
440 | list_for_each_entry_safe(rdy, tmp, &random_ready_list, list) { | |
441 | struct module *owner = rdy->owner; | |
442 | ||
443 | list_del_init(&rdy->list); | |
444 | rdy->func(rdy); | |
445 | module_put(owner); | |
446 | } | |
447 | spin_unlock_irqrestore(&random_ready_list_lock, flags); | |
448 | } | |
449 | ||
90ed1e67 | 450 | static void credit_entropy_bits(int nbits) |
1da177e4 | 451 | { |
9c07f578 | 452 | int entropy_count, orig; |
18263c4e | 453 | |
a49c010e | 454 | if (nbits <= 0) |
adc782da MM |
455 | return; |
456 | ||
a49c010e JD |
457 | nbits = min(nbits, POOL_BITS); |
458 | ||
c5704490 JD |
459 | do { |
460 | orig = READ_ONCE(input_pool.entropy_count); | |
461 | entropy_count = min(POOL_BITS, orig + nbits); | |
462 | } while (cmpxchg(&input_pool.entropy_count, orig, entropy_count) != orig); | |
1da177e4 | 463 | |
c5704490 | 464 | trace_credit_entropy_bits(nbits, entropy_count, _RET_IP_); |
00ce1db1 | 465 | |
c5704490 | 466 | if (crng_init < 2 && entropy_count >= POOL_MIN_BITS) |
5d58ea3a | 467 | crng_reseed(&primary_crng); |
1da177e4 LT |
468 | } |
469 | ||
e192be9d TT |
470 | /********************************************************************* |
471 | * | |
472 | * CRNG using CHACHA20 | |
473 | * | |
474 | *********************************************************************/ | |
475 | ||
248045b8 | 476 | #define CRNG_RESEED_INTERVAL (300 * HZ) |
e192be9d TT |
477 | |
478 | static DECLARE_WAIT_QUEUE_HEAD(crng_init_wait); | |
479 | ||
1e7f583a TT |
480 | /* |
481 | * Hack to deal with crazy userspace progams when they are all trying | |
482 | * to access /dev/urandom in parallel. The programs are almost | |
483 | * certainly doing something terribly wrong, but we'll work around | |
484 | * their brain damage. | |
485 | */ | |
486 | static struct crng_state **crng_node_pool __read_mostly; | |
1e7f583a | 487 | |
b169c13d | 488 | static void invalidate_batched_entropy(void); |
fe6f1a6a | 489 | static void numa_crng_init(void); |
b169c13d | 490 | |
9b254366 KC |
491 | static bool trust_cpu __ro_after_init = IS_ENABLED(CONFIG_RANDOM_TRUST_CPU); |
492 | static int __init parse_trust_cpu(char *arg) | |
493 | { | |
494 | return kstrtobool(arg, &trust_cpu); | |
495 | } | |
496 | early_param("random.trust_cpu", parse_trust_cpu); | |
497 | ||
5cbe0f13 | 498 | static bool crng_init_try_arch(struct crng_state *crng) |
e192be9d | 499 | { |
248045b8 JD |
500 | int i; |
501 | bool arch_init = true; | |
502 | unsigned long rv; | |
e192be9d | 503 | |
e192be9d TT |
504 | for (i = 4; i < 16; i++) { |
505 | if (!arch_get_random_seed_long(&rv) && | |
39a8883a | 506 | !arch_get_random_long(&rv)) { |
e192be9d | 507 | rv = random_get_entropy(); |
5cbe0f13 | 508 | arch_init = false; |
39a8883a | 509 | } |
e192be9d TT |
510 | crng->state[i] ^= rv; |
511 | } | |
5cbe0f13 MR |
512 | |
513 | return arch_init; | |
514 | } | |
515 | ||
ebf76063 | 516 | static bool __init crng_init_try_arch_early(void) |
253d3194 | 517 | { |
248045b8 JD |
518 | int i; |
519 | bool arch_init = true; | |
520 | unsigned long rv; | |
253d3194 MR |
521 | |
522 | for (i = 4; i < 16; i++) { | |
523 | if (!arch_get_random_seed_long_early(&rv) && | |
524 | !arch_get_random_long_early(&rv)) { | |
525 | rv = random_get_entropy(); | |
526 | arch_init = false; | |
527 | } | |
ebf76063 | 528 | primary_crng.state[i] ^= rv; |
253d3194 MR |
529 | } |
530 | ||
531 | return arch_init; | |
532 | } | |
533 | ||
7b873241 | 534 | static void crng_initialize_secondary(struct crng_state *crng) |
5cbe0f13 | 535 | { |
a181e0fd | 536 | chacha_init_consts(crng->state); |
d38bb085 | 537 | _get_random_bytes(&crng->state[4], sizeof(u32) * 12); |
5cbe0f13 MR |
538 | crng_init_try_arch(crng); |
539 | crng->init_time = jiffies - CRNG_RESEED_INTERVAL - 1; | |
540 | } | |
541 | ||
ebf76063 | 542 | static void __init crng_initialize_primary(void) |
5cbe0f13 | 543 | { |
9c07f578 | 544 | extract_entropy(&primary_crng.state[4], sizeof(u32) * 12); |
ebf76063 | 545 | if (crng_init_try_arch_early() && trust_cpu && crng_init < 2) { |
fe6f1a6a JD |
546 | invalidate_batched_entropy(); |
547 | numa_crng_init(); | |
39a8883a | 548 | crng_init = 2; |
161212c7 | 549 | pr_notice("crng init done (trusting CPU's manufacturer)\n"); |
39a8883a | 550 | } |
ebf76063 | 551 | primary_crng.init_time = jiffies - CRNG_RESEED_INTERVAL - 1; |
e192be9d TT |
552 | } |
553 | ||
9d5505f1 | 554 | static void crng_finalize_init(void) |
f7e67b8e | 555 | { |
f7e67b8e DB |
556 | if (!system_wq) { |
557 | /* We can't call numa_crng_init until we have workqueues, | |
558 | * so mark this for processing later. */ | |
559 | crng_need_final_init = true; | |
560 | return; | |
561 | } | |
562 | ||
563 | invalidate_batched_entropy(); | |
564 | numa_crng_init(); | |
565 | crng_init = 2; | |
9d5505f1 | 566 | crng_need_final_init = false; |
f7e67b8e DB |
567 | process_random_ready_list(); |
568 | wake_up_interruptible(&crng_init_wait); | |
569 | kill_fasync(&fasync, SIGIO, POLL_IN); | |
570 | pr_notice("crng init done\n"); | |
571 | if (unseeded_warning.missed) { | |
572 | pr_notice("%d get_random_xx warning(s) missed due to ratelimiting\n", | |
573 | unseeded_warning.missed); | |
574 | unseeded_warning.missed = 0; | |
575 | } | |
576 | if (urandom_warning.missed) { | |
577 | pr_notice("%d urandom warning(s) missed due to ratelimiting\n", | |
578 | urandom_warning.missed); | |
579 | urandom_warning.missed = 0; | |
580 | } | |
581 | } | |
582 | ||
6c1e851c | 583 | static void do_numa_crng_init(struct work_struct *work) |
8ef35c86 TT |
584 | { |
585 | int i; | |
586 | struct crng_state *crng; | |
587 | struct crng_state **pool; | |
588 | ||
248045b8 | 589 | pool = kcalloc(nr_node_ids, sizeof(*pool), GFP_KERNEL | __GFP_NOFAIL); |
8ef35c86 TT |
590 | for_each_online_node(i) { |
591 | crng = kmalloc_node(sizeof(struct crng_state), | |
592 | GFP_KERNEL | __GFP_NOFAIL, i); | |
593 | spin_lock_init(&crng->lock); | |
5cbe0f13 | 594 | crng_initialize_secondary(crng); |
8ef35c86 TT |
595 | pool[i] = crng; |
596 | } | |
5d73d1e3 EB |
597 | /* pairs with READ_ONCE() in select_crng() */ |
598 | if (cmpxchg_release(&crng_node_pool, NULL, pool) != NULL) { | |
8ef35c86 TT |
599 | for_each_node(i) |
600 | kfree(pool[i]); | |
601 | kfree(pool); | |
602 | } | |
603 | } | |
6c1e851c TT |
604 | |
605 | static DECLARE_WORK(numa_crng_init_work, do_numa_crng_init); | |
606 | ||
607 | static void numa_crng_init(void) | |
608 | { | |
7b873241 JD |
609 | if (IS_ENABLED(CONFIG_NUMA)) |
610 | schedule_work(&numa_crng_init_work); | |
6c1e851c | 611 | } |
5d73d1e3 EB |
612 | |
613 | static struct crng_state *select_crng(void) | |
614 | { | |
7b873241 JD |
615 | if (IS_ENABLED(CONFIG_NUMA)) { |
616 | struct crng_state **pool; | |
617 | int nid = numa_node_id(); | |
618 | ||
619 | /* pairs with cmpxchg_release() in do_numa_crng_init() */ | |
620 | pool = READ_ONCE(crng_node_pool); | |
621 | if (pool && pool[nid]) | |
622 | return pool[nid]; | |
623 | } | |
5d73d1e3 | 624 | |
5d73d1e3 EB |
625 | return &primary_crng; |
626 | } | |
8ef35c86 | 627 | |
dc12baac TT |
628 | /* |
629 | * crng_fast_load() can be called by code in the interrupt service | |
73c7733f JD |
630 | * path. So we can't afford to dilly-dally. Returns the number of |
631 | * bytes processed from cp. | |
dc12baac | 632 | */ |
d38bb085 | 633 | static size_t crng_fast_load(const u8 *cp, size_t len) |
e192be9d TT |
634 | { |
635 | unsigned long flags; | |
d38bb085 | 636 | u8 *p; |
73c7733f | 637 | size_t ret = 0; |
e192be9d TT |
638 | |
639 | if (!spin_trylock_irqsave(&primary_crng.lock, flags)) | |
640 | return 0; | |
43838a23 | 641 | if (crng_init != 0) { |
e192be9d TT |
642 | spin_unlock_irqrestore(&primary_crng.lock, flags); |
643 | return 0; | |
644 | } | |
248045b8 | 645 | p = (u8 *)&primary_crng.state[4]; |
e192be9d | 646 | while (len > 0 && crng_init_cnt < CRNG_INIT_CNT_THRESH) { |
1ca1b917 | 647 | p[crng_init_cnt % CHACHA_KEY_SIZE] ^= *cp; |
73c7733f | 648 | cp++; crng_init_cnt++; len--; ret++; |
e192be9d | 649 | } |
4a072c71 | 650 | spin_unlock_irqrestore(&primary_crng.lock, flags); |
e192be9d | 651 | if (crng_init_cnt >= CRNG_INIT_CNT_THRESH) { |
b169c13d | 652 | invalidate_batched_entropy(); |
e192be9d | 653 | crng_init = 1; |
12cd53af | 654 | pr_notice("fast init done\n"); |
e192be9d | 655 | } |
73c7733f | 656 | return ret; |
e192be9d TT |
657 | } |
658 | ||
dc12baac TT |
659 | /* |
660 | * crng_slow_load() is called by add_device_randomness, which has two | |
661 | * attributes. (1) We can't trust the buffer passed to it is | |
662 | * guaranteed to be unpredictable (so it might not have any entropy at | |
663 | * all), and (2) it doesn't have the performance constraints of | |
664 | * crng_fast_load(). | |
665 | * | |
666 | * So we do something more comprehensive which is guaranteed to touch | |
667 | * all of the primary_crng's state, and which uses a LFSR with a | |
668 | * period of 255 as part of the mixing algorithm. Finally, we do | |
669 | * *not* advance crng_init_cnt since buffer we may get may be something | |
670 | * like a fixed DMI table (for example), which might very well be | |
671 | * unique to the machine, but is otherwise unvarying. | |
672 | */ | |
d38bb085 | 673 | static int crng_slow_load(const u8 *cp, size_t len) |
dc12baac | 674 | { |
248045b8 JD |
675 | unsigned long flags; |
676 | static u8 lfsr = 1; | |
677 | u8 tmp; | |
678 | unsigned int i, max = CHACHA_KEY_SIZE; | |
679 | const u8 *src_buf = cp; | |
680 | u8 *dest_buf = (u8 *)&primary_crng.state[4]; | |
dc12baac TT |
681 | |
682 | if (!spin_trylock_irqsave(&primary_crng.lock, flags)) | |
683 | return 0; | |
684 | if (crng_init != 0) { | |
685 | spin_unlock_irqrestore(&primary_crng.lock, flags); | |
686 | return 0; | |
687 | } | |
688 | if (len > max) | |
689 | max = len; | |
690 | ||
248045b8 | 691 | for (i = 0; i < max; i++) { |
dc12baac TT |
692 | tmp = lfsr; |
693 | lfsr >>= 1; | |
694 | if (tmp & 1) | |
695 | lfsr ^= 0xE1; | |
1ca1b917 EB |
696 | tmp = dest_buf[i % CHACHA_KEY_SIZE]; |
697 | dest_buf[i % CHACHA_KEY_SIZE] ^= src_buf[i % len] ^ lfsr; | |
dc12baac TT |
698 | lfsr += (tmp << 3) | (tmp >> 5); |
699 | } | |
700 | spin_unlock_irqrestore(&primary_crng.lock, flags); | |
701 | return 1; | |
702 | } | |
703 | ||
5d58ea3a | 704 | static void crng_reseed(struct crng_state *crng) |
e192be9d | 705 | { |
248045b8 | 706 | unsigned long flags; |
6e8ec255 | 707 | int i; |
e192be9d | 708 | union { |
248045b8 JD |
709 | u8 block[CHACHA_BLOCK_SIZE]; |
710 | u32 key[8]; | |
e192be9d TT |
711 | } buf; |
712 | ||
5d58ea3a | 713 | if (crng == &primary_crng) { |
9c07f578 JD |
714 | int entropy_count; |
715 | do { | |
716 | entropy_count = READ_ONCE(input_pool.entropy_count); | |
c5704490 | 717 | if (entropy_count < POOL_MIN_BITS) |
9c07f578 JD |
718 | return; |
719 | } while (cmpxchg(&input_pool.entropy_count, entropy_count, 0) != entropy_count); | |
720 | extract_entropy(buf.key, sizeof(buf.key)); | |
489c7fc4 JD |
721 | wake_up_interruptible(&random_write_wait); |
722 | kill_fasync(&fasync, SIGIO, POLL_OUT); | |
c92e040d | 723 | } else { |
1e7f583a | 724 | _extract_crng(&primary_crng, buf.block); |
c92e040d | 725 | _crng_backtrack_protect(&primary_crng, buf.block, |
1ca1b917 | 726 | CHACHA_KEY_SIZE); |
c92e040d | 727 | } |
0bb29a84 | 728 | spin_lock_irqsave(&crng->lock, flags); |
e192be9d | 729 | for (i = 0; i < 8; i++) { |
248045b8 | 730 | unsigned long rv; |
e192be9d TT |
731 | if (!arch_get_random_seed_long(&rv) && |
732 | !arch_get_random_long(&rv)) | |
733 | rv = random_get_entropy(); | |
248045b8 | 734 | crng->state[i + 4] ^= buf.key[i] ^ rv; |
e192be9d TT |
735 | } |
736 | memzero_explicit(&buf, sizeof(buf)); | |
009ba856 | 737 | WRITE_ONCE(crng->init_time, jiffies); |
0bb29a84 | 738 | spin_unlock_irqrestore(&crng->lock, flags); |
9d5505f1 DB |
739 | if (crng == &primary_crng && crng_init < 2) |
740 | crng_finalize_init(); | |
e192be9d TT |
741 | } |
742 | ||
248045b8 | 743 | static void _extract_crng(struct crng_state *crng, u8 out[CHACHA_BLOCK_SIZE]) |
e192be9d | 744 | { |
2ee25b69 | 745 | unsigned long flags, init_time; |
009ba856 EB |
746 | |
747 | if (crng_ready()) { | |
748 | init_time = READ_ONCE(crng->init_time); | |
749 | if (time_after(READ_ONCE(crng_global_init_time), init_time) || | |
750 | time_after(jiffies, init_time + CRNG_RESEED_INTERVAL)) | |
5d58ea3a | 751 | crng_reseed(crng); |
009ba856 | 752 | } |
e192be9d | 753 | spin_lock_irqsave(&crng->lock, flags); |
e192be9d TT |
754 | chacha20_block(&crng->state[0], out); |
755 | if (crng->state[12] == 0) | |
756 | crng->state[13]++; | |
757 | spin_unlock_irqrestore(&crng->lock, flags); | |
758 | } | |
759 | ||
d38bb085 | 760 | static void extract_crng(u8 out[CHACHA_BLOCK_SIZE]) |
1e7f583a | 761 | { |
5d73d1e3 | 762 | _extract_crng(select_crng(), out); |
1e7f583a TT |
763 | } |
764 | ||
c92e040d TT |
765 | /* |
766 | * Use the leftover bytes from the CRNG block output (if there is | |
767 | * enough) to mutate the CRNG key to provide backtracking protection. | |
768 | */ | |
769 | static void _crng_backtrack_protect(struct crng_state *crng, | |
d38bb085 | 770 | u8 tmp[CHACHA_BLOCK_SIZE], int used) |
c92e040d | 771 | { |
248045b8 JD |
772 | unsigned long flags; |
773 | u32 *s, *d; | |
774 | int i; | |
c92e040d | 775 | |
d38bb085 | 776 | used = round_up(used, sizeof(u32)); |
1ca1b917 | 777 | if (used + CHACHA_KEY_SIZE > CHACHA_BLOCK_SIZE) { |
c92e040d TT |
778 | extract_crng(tmp); |
779 | used = 0; | |
780 | } | |
781 | spin_lock_irqsave(&crng->lock, flags); | |
248045b8 | 782 | s = (u32 *)&tmp[used]; |
c92e040d | 783 | d = &crng->state[4]; |
248045b8 | 784 | for (i = 0; i < 8; i++) |
c92e040d TT |
785 | *d++ ^= *s++; |
786 | spin_unlock_irqrestore(&crng->lock, flags); | |
787 | } | |
788 | ||
d38bb085 | 789 | static void crng_backtrack_protect(u8 tmp[CHACHA_BLOCK_SIZE], int used) |
c92e040d | 790 | { |
5d73d1e3 | 791 | _crng_backtrack_protect(select_crng(), tmp, used); |
c92e040d TT |
792 | } |
793 | ||
e192be9d TT |
794 | static ssize_t extract_crng_user(void __user *buf, size_t nbytes) |
795 | { | |
1ca1b917 | 796 | ssize_t ret = 0, i = CHACHA_BLOCK_SIZE; |
d38bb085 | 797 | u8 tmp[CHACHA_BLOCK_SIZE] __aligned(4); |
e192be9d TT |
798 | int large_request = (nbytes > 256); |
799 | ||
800 | while (nbytes) { | |
801 | if (large_request && need_resched()) { | |
802 | if (signal_pending(current)) { | |
803 | if (ret == 0) | |
804 | ret = -ERESTARTSYS; | |
805 | break; | |
806 | } | |
807 | schedule(); | |
808 | } | |
809 | ||
810 | extract_crng(tmp); | |
1ca1b917 | 811 | i = min_t(int, nbytes, CHACHA_BLOCK_SIZE); |
e192be9d TT |
812 | if (copy_to_user(buf, tmp, i)) { |
813 | ret = -EFAULT; | |
814 | break; | |
815 | } | |
816 | ||
817 | nbytes -= i; | |
818 | buf += i; | |
819 | ret += i; | |
820 | } | |
c92e040d | 821 | crng_backtrack_protect(tmp, i); |
e192be9d TT |
822 | |
823 | /* Wipe data just written to memory */ | |
824 | memzero_explicit(tmp, sizeof(tmp)); | |
825 | ||
826 | return ret; | |
827 | } | |
828 | ||
1da177e4 LT |
829 | /********************************************************************* |
830 | * | |
831 | * Entropy input management | |
832 | * | |
833 | *********************************************************************/ | |
834 | ||
835 | /* There is one of these per entropy source */ | |
836 | struct timer_rand_state { | |
837 | cycles_t last_time; | |
90b75ee5 | 838 | long last_delta, last_delta2; |
1da177e4 LT |
839 | }; |
840 | ||
644008df TT |
841 | #define INIT_TIMER_RAND_STATE { INITIAL_JIFFIES, }; |
842 | ||
a2080a67 | 843 | /* |
e192be9d TT |
844 | * Add device- or boot-specific data to the input pool to help |
845 | * initialize it. | |
a2080a67 | 846 | * |
e192be9d TT |
847 | * None of this adds any entropy; it is meant to avoid the problem of |
848 | * the entropy pool having similar initial state across largely | |
849 | * identical devices. | |
a2080a67 LT |
850 | */ |
851 | void add_device_randomness(const void *buf, unsigned int size) | |
852 | { | |
61875f30 | 853 | unsigned long time = random_get_entropy() ^ jiffies; |
3ef4cb2d | 854 | unsigned long flags; |
a2080a67 | 855 | |
dc12baac TT |
856 | if (!crng_ready() && size) |
857 | crng_slow_load(buf, size); | |
ee7998c5 | 858 | |
5910895f | 859 | trace_add_device_randomness(size, _RET_IP_); |
3ef4cb2d | 860 | spin_lock_irqsave(&input_pool.lock, flags); |
90ed1e67 JD |
861 | _mix_pool_bytes(buf, size); |
862 | _mix_pool_bytes(&time, sizeof(time)); | |
3ef4cb2d | 863 | spin_unlock_irqrestore(&input_pool.lock, flags); |
a2080a67 LT |
864 | } |
865 | EXPORT_SYMBOL(add_device_randomness); | |
866 | ||
644008df | 867 | static struct timer_rand_state input_timer_state = INIT_TIMER_RAND_STATE; |
3060d6fe | 868 | |
1da177e4 LT |
869 | /* |
870 | * This function adds entropy to the entropy "pool" by using timing | |
871 | * delays. It uses the timer_rand_state structure to make an estimate | |
872 | * of how many bits of entropy this call has added to the pool. | |
873 | * | |
874 | * The number "num" is also added to the pool - it should somehow describe | |
875 | * the type of event which just happened. This is currently 0-255 for | |
876 | * keyboard scan codes, and 256 upwards for interrupts. | |
877 | * | |
878 | */ | |
879 | static void add_timer_randomness(struct timer_rand_state *state, unsigned num) | |
880 | { | |
881 | struct { | |
1da177e4 | 882 | long jiffies; |
d38bb085 JD |
883 | unsigned int cycles; |
884 | unsigned int num; | |
1da177e4 LT |
885 | } sample; |
886 | long delta, delta2, delta3; | |
887 | ||
1da177e4 | 888 | sample.jiffies = jiffies; |
61875f30 | 889 | sample.cycles = random_get_entropy(); |
1da177e4 | 890 | sample.num = num; |
90ed1e67 | 891 | mix_pool_bytes(&sample, sizeof(sample)); |
1da177e4 LT |
892 | |
893 | /* | |
894 | * Calculate number of bits of randomness we probably added. | |
895 | * We take into account the first, second and third-order deltas | |
896 | * in order to make our estimate. | |
897 | */ | |
e00d996a QC |
898 | delta = sample.jiffies - READ_ONCE(state->last_time); |
899 | WRITE_ONCE(state->last_time, sample.jiffies); | |
5e747dd9 | 900 | |
e00d996a QC |
901 | delta2 = delta - READ_ONCE(state->last_delta); |
902 | WRITE_ONCE(state->last_delta, delta); | |
5e747dd9 | 903 | |
e00d996a QC |
904 | delta3 = delta2 - READ_ONCE(state->last_delta2); |
905 | WRITE_ONCE(state->last_delta2, delta2); | |
5e747dd9 RV |
906 | |
907 | if (delta < 0) | |
908 | delta = -delta; | |
909 | if (delta2 < 0) | |
910 | delta2 = -delta2; | |
911 | if (delta3 < 0) | |
912 | delta3 = -delta3; | |
913 | if (delta > delta2) | |
914 | delta = delta2; | |
915 | if (delta > delta3) | |
916 | delta = delta3; | |
1da177e4 | 917 | |
5e747dd9 RV |
918 | /* |
919 | * delta is now minimum absolute delta. | |
920 | * Round down by 1 bit on general principles, | |
727d499a | 921 | * and limit entropy estimate to 12 bits. |
5e747dd9 | 922 | */ |
248045b8 | 923 | credit_entropy_bits(min_t(int, fls(delta >> 1), 11)); |
1da177e4 LT |
924 | } |
925 | ||
d251575a | 926 | void add_input_randomness(unsigned int type, unsigned int code, |
248045b8 | 927 | unsigned int value) |
1da177e4 LT |
928 | { |
929 | static unsigned char last_value; | |
930 | ||
931 | /* ignore autorepeat and the like */ | |
932 | if (value == last_value) | |
933 | return; | |
934 | ||
1da177e4 LT |
935 | last_value = value; |
936 | add_timer_randomness(&input_timer_state, | |
937 | (type << 4) ^ code ^ (code >> 4) ^ value); | |
c5704490 | 938 | trace_add_input_randomness(input_pool.entropy_count); |
1da177e4 | 939 | } |
80fc9f53 | 940 | EXPORT_SYMBOL_GPL(add_input_randomness); |
1da177e4 | 941 | |
775f4b29 TT |
942 | static DEFINE_PER_CPU(struct fast_pool, irq_randomness); |
943 | ||
43759d4f TT |
944 | #ifdef ADD_INTERRUPT_BENCH |
945 | static unsigned long avg_cycles, avg_deviation; | |
946 | ||
248045b8 JD |
947 | #define AVG_SHIFT 8 /* Exponential average factor k=1/256 */ |
948 | #define FIXED_1_2 (1 << (AVG_SHIFT - 1)) | |
43759d4f TT |
949 | |
950 | static void add_interrupt_bench(cycles_t start) | |
951 | { | |
248045b8 | 952 | long delta = random_get_entropy() - start; |
43759d4f | 953 | |
248045b8 JD |
954 | /* Use a weighted moving average */ |
955 | delta = delta - ((avg_cycles + FIXED_1_2) >> AVG_SHIFT); | |
956 | avg_cycles += delta; | |
957 | /* And average deviation */ | |
958 | delta = abs(delta) - ((avg_deviation + FIXED_1_2) >> AVG_SHIFT); | |
959 | avg_deviation += delta; | |
43759d4f TT |
960 | } |
961 | #else | |
962 | #define add_interrupt_bench(x) | |
963 | #endif | |
964 | ||
d38bb085 | 965 | static u32 get_reg(struct fast_pool *f, struct pt_regs *regs) |
ee3e00e9 | 966 | { |
248045b8 | 967 | u32 *ptr = (u32 *)regs; |
92e75428 | 968 | unsigned int idx; |
ee3e00e9 TT |
969 | |
970 | if (regs == NULL) | |
971 | return 0; | |
92e75428 | 972 | idx = READ_ONCE(f->reg_idx); |
d38bb085 | 973 | if (idx >= sizeof(struct pt_regs) / sizeof(u32)) |
92e75428 TT |
974 | idx = 0; |
975 | ptr += idx++; | |
976 | WRITE_ONCE(f->reg_idx, idx); | |
9dfa7bba | 977 | return *ptr; |
ee3e00e9 TT |
978 | } |
979 | ||
703f7066 | 980 | void add_interrupt_randomness(int irq) |
1da177e4 | 981 | { |
248045b8 JD |
982 | struct fast_pool *fast_pool = this_cpu_ptr(&irq_randomness); |
983 | struct pt_regs *regs = get_irq_regs(); | |
984 | unsigned long now = jiffies; | |
985 | cycles_t cycles = random_get_entropy(); | |
986 | u32 c_high, j_high; | |
987 | u64 ip; | |
3060d6fe | 988 | |
ee3e00e9 TT |
989 | if (cycles == 0) |
990 | cycles = get_reg(fast_pool, regs); | |
655b2264 TT |
991 | c_high = (sizeof(cycles) > 4) ? cycles >> 32 : 0; |
992 | j_high = (sizeof(now) > 4) ? now >> 32 : 0; | |
43759d4f TT |
993 | fast_pool->pool[0] ^= cycles ^ j_high ^ irq; |
994 | fast_pool->pool[1] ^= now ^ c_high; | |
655b2264 | 995 | ip = regs ? instruction_pointer(regs) : _RET_IP_; |
43759d4f | 996 | fast_pool->pool[2] ^= ip; |
248045b8 JD |
997 | fast_pool->pool[3] ^= |
998 | (sizeof(ip) > 4) ? ip >> 32 : get_reg(fast_pool, regs); | |
3060d6fe | 999 | |
43759d4f | 1000 | fast_mix(fast_pool); |
43759d4f | 1001 | add_interrupt_bench(cycles); |
3060d6fe | 1002 | |
43838a23 | 1003 | if (unlikely(crng_init == 0)) { |
e192be9d | 1004 | if ((fast_pool->count >= 64) && |
d38bb085 | 1005 | crng_fast_load((u8 *)fast_pool->pool, sizeof(fast_pool->pool)) > 0) { |
e192be9d TT |
1006 | fast_pool->count = 0; |
1007 | fast_pool->last = now; | |
1008 | } | |
1009 | return; | |
1010 | } | |
1011 | ||
248045b8 | 1012 | if ((fast_pool->count < 64) && !time_after(now, fast_pool->last + HZ)) |
1da177e4 LT |
1013 | return; |
1014 | ||
90ed1e67 | 1015 | if (!spin_trylock(&input_pool.lock)) |
91fcb532 | 1016 | return; |
83664a69 | 1017 | |
91fcb532 | 1018 | fast_pool->last = now; |
90ed1e67 JD |
1019 | __mix_pool_bytes(&fast_pool->pool, sizeof(fast_pool->pool)); |
1020 | spin_unlock(&input_pool.lock); | |
83664a69 | 1021 | |
ee3e00e9 | 1022 | fast_pool->count = 0; |
83664a69 | 1023 | |
ee3e00e9 | 1024 | /* award one bit for the contents of the fast pool */ |
90ed1e67 | 1025 | credit_entropy_bits(1); |
1da177e4 | 1026 | } |
4b44f2d1 | 1027 | EXPORT_SYMBOL_GPL(add_interrupt_randomness); |
1da177e4 | 1028 | |
9361401e | 1029 | #ifdef CONFIG_BLOCK |
1da177e4 LT |
1030 | void add_disk_randomness(struct gendisk *disk) |
1031 | { | |
1032 | if (!disk || !disk->random) | |
1033 | return; | |
1034 | /* first major is 1, so we get >= 0x200 here */ | |
f331c029 | 1035 | add_timer_randomness(disk->random, 0x100 + disk_devt(disk)); |
c5704490 | 1036 | trace_add_disk_randomness(disk_devt(disk), input_pool.entropy_count); |
1da177e4 | 1037 | } |
bdcfa3e5 | 1038 | EXPORT_SYMBOL_GPL(add_disk_randomness); |
9361401e | 1039 | #endif |
1da177e4 | 1040 | |
1da177e4 LT |
1041 | /********************************************************************* |
1042 | * | |
1043 | * Entropy extraction routines | |
1044 | * | |
1045 | *********************************************************************/ | |
1046 | ||
19fa5be1 | 1047 | /* |
6e8ec255 JD |
1048 | * This is an HKDF-like construction for using the hashed collected entropy |
1049 | * as a PRF key, that's then expanded block-by-block. | |
19fa5be1 | 1050 | */ |
9c07f578 | 1051 | static void extract_entropy(void *buf, size_t nbytes) |
1da177e4 | 1052 | { |
902c098a | 1053 | unsigned long flags; |
6e8ec255 JD |
1054 | u8 seed[BLAKE2S_HASH_SIZE], next_key[BLAKE2S_HASH_SIZE]; |
1055 | struct { | |
1056 | unsigned long rdrand[32 / sizeof(long)]; | |
1057 | size_t counter; | |
1058 | } block; | |
1059 | size_t i; | |
1060 | ||
c5704490 | 1061 | trace_extract_entropy(nbytes, input_pool.entropy_count); |
9c07f578 | 1062 | |
6e8ec255 JD |
1063 | for (i = 0; i < ARRAY_SIZE(block.rdrand); ++i) { |
1064 | if (!arch_get_random_long(&block.rdrand[i])) | |
1065 | block.rdrand[i] = random_get_entropy(); | |
85a1f777 TT |
1066 | } |
1067 | ||
90ed1e67 | 1068 | spin_lock_irqsave(&input_pool.lock, flags); |
46884442 | 1069 | |
6e8ec255 JD |
1070 | /* seed = HASHPRF(last_key, entropy_input) */ |
1071 | blake2s_final(&input_pool.hash, seed); | |
1da177e4 | 1072 | |
6e8ec255 JD |
1073 | /* next_key = HASHPRF(seed, RDRAND || 0) */ |
1074 | block.counter = 0; | |
1075 | blake2s(next_key, (u8 *)&block, seed, sizeof(next_key), sizeof(block), sizeof(seed)); | |
1076 | blake2s_init_key(&input_pool.hash, BLAKE2S_HASH_SIZE, next_key, sizeof(next_key)); | |
1da177e4 | 1077 | |
6e8ec255 JD |
1078 | spin_unlock_irqrestore(&input_pool.lock, flags); |
1079 | memzero_explicit(next_key, sizeof(next_key)); | |
e192be9d TT |
1080 | |
1081 | while (nbytes) { | |
6e8ec255 JD |
1082 | i = min_t(size_t, nbytes, BLAKE2S_HASH_SIZE); |
1083 | /* output = HASHPRF(seed, RDRAND || ++counter) */ | |
1084 | ++block.counter; | |
1085 | blake2s(buf, (u8 *)&block, seed, i, sizeof(block), sizeof(seed)); | |
e192be9d TT |
1086 | nbytes -= i; |
1087 | buf += i; | |
e192be9d TT |
1088 | } |
1089 | ||
6e8ec255 JD |
1090 | memzero_explicit(seed, sizeof(seed)); |
1091 | memzero_explicit(&block, sizeof(block)); | |
e192be9d TT |
1092 | } |
1093 | ||
eecabf56 | 1094 | #define warn_unseeded_randomness(previous) \ |
248045b8 | 1095 | _warn_unseeded_randomness(__func__, (void *)_RET_IP_, (previous)) |
eecabf56 | 1096 | |
248045b8 | 1097 | static void _warn_unseeded_randomness(const char *func_name, void *caller, void **previous) |
eecabf56 TT |
1098 | { |
1099 | #ifdef CONFIG_WARN_ALL_UNSEEDED_RANDOM | |
1100 | const bool print_once = false; | |
1101 | #else | |
1102 | static bool print_once __read_mostly; | |
1103 | #endif | |
1104 | ||
248045b8 | 1105 | if (print_once || crng_ready() || |
eecabf56 TT |
1106 | (previous && (caller == READ_ONCE(*previous)))) |
1107 | return; | |
1108 | WRITE_ONCE(*previous, caller); | |
1109 | #ifndef CONFIG_WARN_ALL_UNSEEDED_RANDOM | |
1110 | print_once = true; | |
1111 | #endif | |
4e00b339 | 1112 | if (__ratelimit(&unseeded_warning)) |
248045b8 JD |
1113 | printk_deferred(KERN_NOTICE "random: %s called from %pS with crng_init=%d\n", |
1114 | func_name, caller, crng_init); | |
eecabf56 TT |
1115 | } |
1116 | ||
1da177e4 LT |
1117 | /* |
1118 | * This function is the exported kernel interface. It returns some | |
c2557a30 | 1119 | * number of good random numbers, suitable for key generation, seeding |
18e9cea7 GP |
1120 | * TCP sequence numbers, etc. It does not rely on the hardware random |
1121 | * number generator. For random bytes direct from the hardware RNG | |
e297a783 JD |
1122 | * (when available), use get_random_bytes_arch(). In order to ensure |
1123 | * that the randomness provided by this function is okay, the function | |
1124 | * wait_for_random_bytes() should be called and return 0 at least once | |
1125 | * at any point prior. | |
1da177e4 | 1126 | */ |
eecabf56 | 1127 | static void _get_random_bytes(void *buf, int nbytes) |
c2557a30 | 1128 | { |
d38bb085 | 1129 | u8 tmp[CHACHA_BLOCK_SIZE] __aligned(4); |
e192be9d | 1130 | |
5910895f | 1131 | trace_get_random_bytes(nbytes, _RET_IP_); |
e192be9d | 1132 | |
1ca1b917 | 1133 | while (nbytes >= CHACHA_BLOCK_SIZE) { |
e192be9d | 1134 | extract_crng(buf); |
1ca1b917 EB |
1135 | buf += CHACHA_BLOCK_SIZE; |
1136 | nbytes -= CHACHA_BLOCK_SIZE; | |
e192be9d TT |
1137 | } |
1138 | ||
1139 | if (nbytes > 0) { | |
1140 | extract_crng(tmp); | |
1141 | memcpy(buf, tmp, nbytes); | |
c92e040d TT |
1142 | crng_backtrack_protect(tmp, nbytes); |
1143 | } else | |
1ca1b917 | 1144 | crng_backtrack_protect(tmp, CHACHA_BLOCK_SIZE); |
c92e040d | 1145 | memzero_explicit(tmp, sizeof(tmp)); |
c2557a30 | 1146 | } |
eecabf56 TT |
1147 | |
1148 | void get_random_bytes(void *buf, int nbytes) | |
1149 | { | |
1150 | static void *previous; | |
1151 | ||
1152 | warn_unseeded_randomness(&previous); | |
1153 | _get_random_bytes(buf, nbytes); | |
1154 | } | |
c2557a30 TT |
1155 | EXPORT_SYMBOL(get_random_bytes); |
1156 | ||
50ee7529 LT |
1157 | /* |
1158 | * Each time the timer fires, we expect that we got an unpredictable | |
1159 | * jump in the cycle counter. Even if the timer is running on another | |
1160 | * CPU, the timer activity will be touching the stack of the CPU that is | |
1161 | * generating entropy.. | |
1162 | * | |
1163 | * Note that we don't re-arm the timer in the timer itself - we are | |
1164 | * happy to be scheduled away, since that just makes the load more | |
1165 | * complex, but we do not want the timer to keep ticking unless the | |
1166 | * entropy loop is running. | |
1167 | * | |
1168 | * So the re-arming always happens in the entropy loop itself. | |
1169 | */ | |
1170 | static void entropy_timer(struct timer_list *t) | |
1171 | { | |
90ed1e67 | 1172 | credit_entropy_bits(1); |
50ee7529 LT |
1173 | } |
1174 | ||
1175 | /* | |
1176 | * If we have an actual cycle counter, see if we can | |
1177 | * generate enough entropy with timing noise | |
1178 | */ | |
1179 | static void try_to_generate_entropy(void) | |
1180 | { | |
1181 | struct { | |
1182 | unsigned long now; | |
1183 | struct timer_list timer; | |
1184 | } stack; | |
1185 | ||
1186 | stack.now = random_get_entropy(); | |
1187 | ||
1188 | /* Slow counter - or none. Don't even bother */ | |
1189 | if (stack.now == random_get_entropy()) | |
1190 | return; | |
1191 | ||
1192 | timer_setup_on_stack(&stack.timer, entropy_timer, 0); | |
1193 | while (!crng_ready()) { | |
1194 | if (!timer_pending(&stack.timer)) | |
248045b8 | 1195 | mod_timer(&stack.timer, jiffies + 1); |
90ed1e67 | 1196 | mix_pool_bytes(&stack.now, sizeof(stack.now)); |
50ee7529 LT |
1197 | schedule(); |
1198 | stack.now = random_get_entropy(); | |
1199 | } | |
1200 | ||
1201 | del_timer_sync(&stack.timer); | |
1202 | destroy_timer_on_stack(&stack.timer); | |
90ed1e67 | 1203 | mix_pool_bytes(&stack.now, sizeof(stack.now)); |
50ee7529 LT |
1204 | } |
1205 | ||
e297a783 JD |
1206 | /* |
1207 | * Wait for the urandom pool to be seeded and thus guaranteed to supply | |
1208 | * cryptographically secure random numbers. This applies to: the /dev/urandom | |
1209 | * device, the get_random_bytes function, and the get_random_{u32,u64,int,long} | |
1210 | * family of functions. Using any of these functions without first calling | |
1211 | * this function forfeits the guarantee of security. | |
1212 | * | |
1213 | * Returns: 0 if the urandom pool has been seeded. | |
1214 | * -ERESTARTSYS if the function was interrupted by a signal. | |
1215 | */ | |
1216 | int wait_for_random_bytes(void) | |
1217 | { | |
1218 | if (likely(crng_ready())) | |
1219 | return 0; | |
50ee7529 LT |
1220 | |
1221 | do { | |
1222 | int ret; | |
1223 | ret = wait_event_interruptible_timeout(crng_init_wait, crng_ready(), HZ); | |
1224 | if (ret) | |
1225 | return ret > 0 ? 0 : ret; | |
1226 | ||
1227 | try_to_generate_entropy(); | |
1228 | } while (!crng_ready()); | |
1229 | ||
1230 | return 0; | |
e297a783 JD |
1231 | } |
1232 | EXPORT_SYMBOL(wait_for_random_bytes); | |
1233 | ||
9a47249d JD |
1234 | /* |
1235 | * Returns whether or not the urandom pool has been seeded and thus guaranteed | |
1236 | * to supply cryptographically secure random numbers. This applies to: the | |
1237 | * /dev/urandom device, the get_random_bytes function, and the get_random_{u32, | |
1238 | * ,u64,int,long} family of functions. | |
1239 | * | |
1240 | * Returns: true if the urandom pool has been seeded. | |
1241 | * false if the urandom pool has not been seeded. | |
1242 | */ | |
1243 | bool rng_is_initialized(void) | |
1244 | { | |
1245 | return crng_ready(); | |
1246 | } | |
1247 | EXPORT_SYMBOL(rng_is_initialized); | |
1248 | ||
205a525c HX |
1249 | /* |
1250 | * Add a callback function that will be invoked when the nonblocking | |
1251 | * pool is initialised. | |
1252 | * | |
1253 | * returns: 0 if callback is successfully added | |
1254 | * -EALREADY if pool is already initialised (callback not called) | |
1255 | * -ENOENT if module for callback is not alive | |
1256 | */ | |
1257 | int add_random_ready_callback(struct random_ready_callback *rdy) | |
1258 | { | |
1259 | struct module *owner; | |
1260 | unsigned long flags; | |
1261 | int err = -EALREADY; | |
1262 | ||
e192be9d | 1263 | if (crng_ready()) |
205a525c HX |
1264 | return err; |
1265 | ||
1266 | owner = rdy->owner; | |
1267 | if (!try_module_get(owner)) | |
1268 | return -ENOENT; | |
1269 | ||
1270 | spin_lock_irqsave(&random_ready_list_lock, flags); | |
e192be9d | 1271 | if (crng_ready()) |
205a525c HX |
1272 | goto out; |
1273 | ||
1274 | owner = NULL; | |
1275 | ||
1276 | list_add(&rdy->list, &random_ready_list); | |
1277 | err = 0; | |
1278 | ||
1279 | out: | |
1280 | spin_unlock_irqrestore(&random_ready_list_lock, flags); | |
1281 | ||
1282 | module_put(owner); | |
1283 | ||
1284 | return err; | |
1285 | } | |
1286 | EXPORT_SYMBOL(add_random_ready_callback); | |
1287 | ||
1288 | /* | |
1289 | * Delete a previously registered readiness callback function. | |
1290 | */ | |
1291 | void del_random_ready_callback(struct random_ready_callback *rdy) | |
1292 | { | |
1293 | unsigned long flags; | |
1294 | struct module *owner = NULL; | |
1295 | ||
1296 | spin_lock_irqsave(&random_ready_list_lock, flags); | |
1297 | if (!list_empty(&rdy->list)) { | |
1298 | list_del_init(&rdy->list); | |
1299 | owner = rdy->owner; | |
1300 | } | |
1301 | spin_unlock_irqrestore(&random_ready_list_lock, flags); | |
1302 | ||
1303 | module_put(owner); | |
1304 | } | |
1305 | EXPORT_SYMBOL(del_random_ready_callback); | |
1306 | ||
c2557a30 TT |
1307 | /* |
1308 | * This function will use the architecture-specific hardware random | |
1309 | * number generator if it is available. The arch-specific hw RNG will | |
1310 | * almost certainly be faster than what we can do in software, but it | |
1311 | * is impossible to verify that it is implemented securely (as | |
1312 | * opposed, to, say, the AES encryption of a sequence number using a | |
1313 | * key known by the NSA). So it's useful if we need the speed, but | |
1314 | * only if we're willing to trust the hardware manufacturer not to | |
1315 | * have put in a back door. | |
753d433b TH |
1316 | * |
1317 | * Return number of bytes filled in. | |
c2557a30 | 1318 | */ |
753d433b | 1319 | int __must_check get_random_bytes_arch(void *buf, int nbytes) |
1da177e4 | 1320 | { |
753d433b | 1321 | int left = nbytes; |
d38bb085 | 1322 | u8 *p = buf; |
63d77173 | 1323 | |
753d433b TH |
1324 | trace_get_random_bytes_arch(left, _RET_IP_); |
1325 | while (left) { | |
63d77173 | 1326 | unsigned long v; |
753d433b | 1327 | int chunk = min_t(int, left, sizeof(unsigned long)); |
c2557a30 | 1328 | |
63d77173 PA |
1329 | if (!arch_get_random_long(&v)) |
1330 | break; | |
8ddd6efa | 1331 | |
bd29e568 | 1332 | memcpy(p, &v, chunk); |
63d77173 | 1333 | p += chunk; |
753d433b | 1334 | left -= chunk; |
63d77173 PA |
1335 | } |
1336 | ||
753d433b | 1337 | return nbytes - left; |
1da177e4 | 1338 | } |
c2557a30 TT |
1339 | EXPORT_SYMBOL(get_random_bytes_arch); |
1340 | ||
1da177e4 LT |
1341 | /* |
1342 | * init_std_data - initialize pool with system data | |
1343 | * | |
1da177e4 LT |
1344 | * This function clears the pool's entropy count and mixes some system |
1345 | * data into the pool to prepare it for use. The pool is not cleared | |
1346 | * as that can only decrease the entropy in the pool. | |
1347 | */ | |
90ed1e67 | 1348 | static void __init init_std_data(void) |
1da177e4 | 1349 | { |
3e88bdff | 1350 | int i; |
902c098a TT |
1351 | ktime_t now = ktime_get_real(); |
1352 | unsigned long rv; | |
1da177e4 | 1353 | |
90ed1e67 | 1354 | mix_pool_bytes(&now, sizeof(now)); |
6e8ec255 | 1355 | for (i = BLAKE2S_BLOCK_SIZE; i > 0; i -= sizeof(rv)) { |
83664a69 PA |
1356 | if (!arch_get_random_seed_long(&rv) && |
1357 | !arch_get_random_long(&rv)) | |
ae9ecd92 | 1358 | rv = random_get_entropy(); |
90ed1e67 | 1359 | mix_pool_bytes(&rv, sizeof(rv)); |
3e88bdff | 1360 | } |
90ed1e67 | 1361 | mix_pool_bytes(utsname(), sizeof(*(utsname()))); |
1da177e4 LT |
1362 | } |
1363 | ||
cbc96b75 TL |
1364 | /* |
1365 | * Note that setup_arch() may call add_device_randomness() | |
1366 | * long before we get here. This allows seeding of the pools | |
1367 | * with some platform dependent data very early in the boot | |
1368 | * process. But it limits our options here. We must use | |
1369 | * statically allocated structures that already have all | |
1370 | * initializations complete at compile time. We should also | |
1371 | * take care not to overwrite the precious per platform data | |
1372 | * we were given. | |
1373 | */ | |
d5553523 | 1374 | int __init rand_initialize(void) |
1da177e4 | 1375 | { |
90ed1e67 | 1376 | init_std_data(); |
f7e67b8e | 1377 | if (crng_need_final_init) |
9d5505f1 | 1378 | crng_finalize_init(); |
ebf76063 | 1379 | crng_initialize_primary(); |
d848e5f8 | 1380 | crng_global_init_time = jiffies; |
4e00b339 TT |
1381 | if (ratelimit_disable) { |
1382 | urandom_warning.interval = 0; | |
1383 | unseeded_warning.interval = 0; | |
1384 | } | |
1da177e4 LT |
1385 | return 0; |
1386 | } | |
1da177e4 | 1387 | |
9361401e | 1388 | #ifdef CONFIG_BLOCK |
1da177e4 LT |
1389 | void rand_initialize_disk(struct gendisk *disk) |
1390 | { | |
1391 | struct timer_rand_state *state; | |
1392 | ||
1393 | /* | |
f8595815 | 1394 | * If kzalloc returns null, we just won't use that entropy |
1da177e4 LT |
1395 | * source. |
1396 | */ | |
f8595815 | 1397 | state = kzalloc(sizeof(struct timer_rand_state), GFP_KERNEL); |
644008df TT |
1398 | if (state) { |
1399 | state->last_time = INITIAL_JIFFIES; | |
1da177e4 | 1400 | disk->random = state; |
644008df | 1401 | } |
1da177e4 | 1402 | } |
9361401e | 1403 | #endif |
1da177e4 | 1404 | |
248045b8 JD |
1405 | static ssize_t urandom_read_nowarn(struct file *file, char __user *buf, |
1406 | size_t nbytes, loff_t *ppos) | |
c6f1deb1 AL |
1407 | { |
1408 | int ret; | |
1409 | ||
c5704490 | 1410 | nbytes = min_t(size_t, nbytes, INT_MAX >> 6); |
c6f1deb1 | 1411 | ret = extract_crng_user(buf, nbytes); |
c5704490 | 1412 | trace_urandom_read(8 * nbytes, 0, input_pool.entropy_count); |
c6f1deb1 AL |
1413 | return ret; |
1414 | } | |
1415 | ||
248045b8 JD |
1416 | static ssize_t urandom_read(struct file *file, char __user *buf, size_t nbytes, |
1417 | loff_t *ppos) | |
1da177e4 | 1418 | { |
9b4d0087 | 1419 | static int maxwarn = 10; |
301f0595 | 1420 | |
e192be9d | 1421 | if (!crng_ready() && maxwarn > 0) { |
9b4d0087 | 1422 | maxwarn--; |
4e00b339 | 1423 | if (__ratelimit(&urandom_warning)) |
12cd53af YL |
1424 | pr_notice("%s: uninitialized urandom read (%zd bytes read)\n", |
1425 | current->comm, nbytes); | |
9b4d0087 | 1426 | } |
c6f1deb1 AL |
1427 | |
1428 | return urandom_read_nowarn(file, buf, nbytes, ppos); | |
1da177e4 LT |
1429 | } |
1430 | ||
248045b8 JD |
1431 | static ssize_t random_read(struct file *file, char __user *buf, size_t nbytes, |
1432 | loff_t *ppos) | |
30c08efe AL |
1433 | { |
1434 | int ret; | |
1435 | ||
1436 | ret = wait_for_random_bytes(); | |
1437 | if (ret != 0) | |
1438 | return ret; | |
1439 | return urandom_read_nowarn(file, buf, nbytes, ppos); | |
1440 | } | |
1441 | ||
248045b8 | 1442 | static __poll_t random_poll(struct file *file, poll_table *wait) |
1da177e4 | 1443 | { |
a11e1d43 | 1444 | __poll_t mask; |
1da177e4 | 1445 | |
30c08efe | 1446 | poll_wait(file, &crng_init_wait, wait); |
a11e1d43 LT |
1447 | poll_wait(file, &random_write_wait, wait); |
1448 | mask = 0; | |
30c08efe | 1449 | if (crng_ready()) |
a9a08845 | 1450 | mask |= EPOLLIN | EPOLLRDNORM; |
489c7fc4 | 1451 | if (input_pool.entropy_count < POOL_MIN_BITS) |
a9a08845 | 1452 | mask |= EPOLLOUT | EPOLLWRNORM; |
1da177e4 LT |
1453 | return mask; |
1454 | } | |
1455 | ||
248045b8 | 1456 | static int write_pool(const char __user *buffer, size_t count) |
1da177e4 | 1457 | { |
1da177e4 | 1458 | size_t bytes; |
d38bb085 | 1459 | u32 t, buf[16]; |
1da177e4 | 1460 | const char __user *p = buffer; |
1da177e4 | 1461 | |
7f397dcd | 1462 | while (count > 0) { |
81e69df3 TT |
1463 | int b, i = 0; |
1464 | ||
7f397dcd MM |
1465 | bytes = min(count, sizeof(buf)); |
1466 | if (copy_from_user(&buf, p, bytes)) | |
1467 | return -EFAULT; | |
1da177e4 | 1468 | |
d38bb085 | 1469 | for (b = bytes; b > 0; b -= sizeof(u32), i++) { |
81e69df3 TT |
1470 | if (!arch_get_random_int(&t)) |
1471 | break; | |
1472 | buf[i] ^= t; | |
1473 | } | |
1474 | ||
7f397dcd | 1475 | count -= bytes; |
1da177e4 LT |
1476 | p += bytes; |
1477 | ||
90ed1e67 | 1478 | mix_pool_bytes(buf, bytes); |
91f3f1e3 | 1479 | cond_resched(); |
1da177e4 | 1480 | } |
7f397dcd MM |
1481 | |
1482 | return 0; | |
1483 | } | |
1484 | ||
90b75ee5 MM |
1485 | static ssize_t random_write(struct file *file, const char __user *buffer, |
1486 | size_t count, loff_t *ppos) | |
7f397dcd MM |
1487 | { |
1488 | size_t ret; | |
7f397dcd | 1489 | |
90ed1e67 | 1490 | ret = write_pool(buffer, count); |
7f397dcd MM |
1491 | if (ret) |
1492 | return ret; | |
1493 | ||
7f397dcd | 1494 | return (ssize_t)count; |
1da177e4 LT |
1495 | } |
1496 | ||
43ae4860 | 1497 | static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg) |
1da177e4 LT |
1498 | { |
1499 | int size, ent_count; | |
1500 | int __user *p = (int __user *)arg; | |
1501 | int retval; | |
1502 | ||
1503 | switch (cmd) { | |
1504 | case RNDGETENTCNT: | |
43ae4860 | 1505 | /* inherently racy, no point locking */ |
c5704490 | 1506 | if (put_user(input_pool.entropy_count, p)) |
1da177e4 LT |
1507 | return -EFAULT; |
1508 | return 0; | |
1509 | case RNDADDTOENTCNT: | |
1510 | if (!capable(CAP_SYS_ADMIN)) | |
1511 | return -EPERM; | |
1512 | if (get_user(ent_count, p)) | |
1513 | return -EFAULT; | |
a49c010e JD |
1514 | if (ent_count < 0) |
1515 | return -EINVAL; | |
1516 | credit_entropy_bits(ent_count); | |
1517 | return 0; | |
1da177e4 LT |
1518 | case RNDADDENTROPY: |
1519 | if (!capable(CAP_SYS_ADMIN)) | |
1520 | return -EPERM; | |
1521 | if (get_user(ent_count, p++)) | |
1522 | return -EFAULT; | |
1523 | if (ent_count < 0) | |
1524 | return -EINVAL; | |
1525 | if (get_user(size, p++)) | |
1526 | return -EFAULT; | |
90ed1e67 | 1527 | retval = write_pool((const char __user *)p, size); |
1da177e4 LT |
1528 | if (retval < 0) |
1529 | return retval; | |
a49c010e JD |
1530 | credit_entropy_bits(ent_count); |
1531 | return 0; | |
1da177e4 LT |
1532 | case RNDZAPENTCNT: |
1533 | case RNDCLEARPOOL: | |
ae9ecd92 TT |
1534 | /* |
1535 | * Clear the entropy pool counters. We no longer clear | |
1536 | * the entropy pool, as that's silly. | |
1537 | */ | |
1da177e4 LT |
1538 | if (!capable(CAP_SYS_ADMIN)) |
1539 | return -EPERM; | |
489c7fc4 | 1540 | if (xchg(&input_pool.entropy_count, 0)) { |
042e293e JD |
1541 | wake_up_interruptible(&random_write_wait); |
1542 | kill_fasync(&fasync, SIGIO, POLL_OUT); | |
1543 | } | |
1da177e4 | 1544 | return 0; |
d848e5f8 TT |
1545 | case RNDRESEEDCRNG: |
1546 | if (!capable(CAP_SYS_ADMIN)) | |
1547 | return -EPERM; | |
1548 | if (crng_init < 2) | |
1549 | return -ENODATA; | |
5d58ea3a | 1550 | crng_reseed(&primary_crng); |
009ba856 | 1551 | WRITE_ONCE(crng_global_init_time, jiffies - 1); |
d848e5f8 | 1552 | return 0; |
1da177e4 LT |
1553 | default: |
1554 | return -EINVAL; | |
1555 | } | |
1556 | } | |
1557 | ||
9a6f70bb JD |
1558 | static int random_fasync(int fd, struct file *filp, int on) |
1559 | { | |
1560 | return fasync_helper(fd, filp, on, &fasync); | |
1561 | } | |
1562 | ||
2b8693c0 | 1563 | const struct file_operations random_fops = { |
248045b8 | 1564 | .read = random_read, |
1da177e4 | 1565 | .write = random_write, |
248045b8 | 1566 | .poll = random_poll, |
43ae4860 | 1567 | .unlocked_ioctl = random_ioctl, |
507e4e2b | 1568 | .compat_ioctl = compat_ptr_ioctl, |
9a6f70bb | 1569 | .fasync = random_fasync, |
6038f373 | 1570 | .llseek = noop_llseek, |
1da177e4 LT |
1571 | }; |
1572 | ||
2b8693c0 | 1573 | const struct file_operations urandom_fops = { |
248045b8 | 1574 | .read = urandom_read, |
1da177e4 | 1575 | .write = random_write, |
43ae4860 | 1576 | .unlocked_ioctl = random_ioctl, |
4aa37c46 | 1577 | .compat_ioctl = compat_ptr_ioctl, |
9a6f70bb | 1578 | .fasync = random_fasync, |
6038f373 | 1579 | .llseek = noop_llseek, |
1da177e4 LT |
1580 | }; |
1581 | ||
248045b8 JD |
1582 | SYSCALL_DEFINE3(getrandom, char __user *, buf, size_t, count, unsigned int, |
1583 | flags) | |
c6e9d6f3 | 1584 | { |
e297a783 JD |
1585 | int ret; |
1586 | ||
248045b8 | 1587 | if (flags & ~(GRND_NONBLOCK | GRND_RANDOM | GRND_INSECURE)) |
75551dbf AL |
1588 | return -EINVAL; |
1589 | ||
1590 | /* | |
1591 | * Requesting insecure and blocking randomness at the same time makes | |
1592 | * no sense. | |
1593 | */ | |
248045b8 | 1594 | if ((flags & (GRND_INSECURE | GRND_RANDOM)) == (GRND_INSECURE | GRND_RANDOM)) |
c6e9d6f3 TT |
1595 | return -EINVAL; |
1596 | ||
1597 | if (count > INT_MAX) | |
1598 | count = INT_MAX; | |
1599 | ||
75551dbf | 1600 | if (!(flags & GRND_INSECURE) && !crng_ready()) { |
c6e9d6f3 TT |
1601 | if (flags & GRND_NONBLOCK) |
1602 | return -EAGAIN; | |
e297a783 JD |
1603 | ret = wait_for_random_bytes(); |
1604 | if (unlikely(ret)) | |
1605 | return ret; | |
c6e9d6f3 | 1606 | } |
c6f1deb1 | 1607 | return urandom_read_nowarn(NULL, buf, count, NULL); |
c6e9d6f3 TT |
1608 | } |
1609 | ||
1da177e4 LT |
1610 | /******************************************************************** |
1611 | * | |
1612 | * Sysctl interface | |
1613 | * | |
1614 | ********************************************************************/ | |
1615 | ||
1616 | #ifdef CONFIG_SYSCTL | |
1617 | ||
1618 | #include <linux/sysctl.h> | |
1619 | ||
db61ffe3 | 1620 | static int random_min_urandom_seed = 60; |
489c7fc4 JD |
1621 | static int random_write_wakeup_bits = POOL_MIN_BITS; |
1622 | static int sysctl_poolsize = POOL_BITS; | |
1da177e4 LT |
1623 | static char sysctl_bootid[16]; |
1624 | ||
1625 | /* | |
f22052b2 | 1626 | * This function is used to return both the bootid UUID, and random |
1da177e4 LT |
1627 | * UUID. The difference is in whether table->data is NULL; if it is, |
1628 | * then a new UUID is generated and returned to the user. | |
1629 | * | |
f22052b2 GP |
1630 | * If the user accesses this via the proc interface, the UUID will be |
1631 | * returned as an ASCII string in the standard UUID format; if via the | |
1632 | * sysctl system call, as 16 bytes of binary data. | |
1da177e4 | 1633 | */ |
248045b8 JD |
1634 | static int proc_do_uuid(struct ctl_table *table, int write, void *buffer, |
1635 | size_t *lenp, loff_t *ppos) | |
1da177e4 | 1636 | { |
a151427e | 1637 | struct ctl_table fake_table; |
1da177e4 LT |
1638 | unsigned char buf[64], tmp_uuid[16], *uuid; |
1639 | ||
1640 | uuid = table->data; | |
1641 | if (!uuid) { | |
1642 | uuid = tmp_uuid; | |
1da177e4 | 1643 | generate_random_uuid(uuid); |
44e4360f MD |
1644 | } else { |
1645 | static DEFINE_SPINLOCK(bootid_spinlock); | |
1646 | ||
1647 | spin_lock(&bootid_spinlock); | |
1648 | if (!uuid[8]) | |
1649 | generate_random_uuid(uuid); | |
1650 | spin_unlock(&bootid_spinlock); | |
1651 | } | |
1da177e4 | 1652 | |
35900771 JP |
1653 | sprintf(buf, "%pU", uuid); |
1654 | ||
1da177e4 LT |
1655 | fake_table.data = buf; |
1656 | fake_table.maxlen = sizeof(buf); | |
1657 | ||
8d65af78 | 1658 | return proc_dostring(&fake_table, write, buffer, lenp, ppos); |
1da177e4 LT |
1659 | } |
1660 | ||
5475e8f0 | 1661 | static struct ctl_table random_table[] = { |
1da177e4 | 1662 | { |
1da177e4 LT |
1663 | .procname = "poolsize", |
1664 | .data = &sysctl_poolsize, | |
1665 | .maxlen = sizeof(int), | |
1666 | .mode = 0444, | |
6d456111 | 1667 | .proc_handler = proc_dointvec, |
1da177e4 LT |
1668 | }, |
1669 | { | |
1da177e4 | 1670 | .procname = "entropy_avail", |
c5704490 | 1671 | .data = &input_pool.entropy_count, |
1da177e4 LT |
1672 | .maxlen = sizeof(int), |
1673 | .mode = 0444, | |
c5704490 | 1674 | .proc_handler = proc_dointvec, |
1da177e4 | 1675 | }, |
1da177e4 | 1676 | { |
1da177e4 | 1677 | .procname = "write_wakeup_threshold", |
2132a96f | 1678 | .data = &random_write_wakeup_bits, |
1da177e4 LT |
1679 | .maxlen = sizeof(int), |
1680 | .mode = 0644, | |
489c7fc4 | 1681 | .proc_handler = proc_dointvec, |
1da177e4 | 1682 | }, |
f5c2742c TT |
1683 | { |
1684 | .procname = "urandom_min_reseed_secs", | |
1685 | .data = &random_min_urandom_seed, | |
1686 | .maxlen = sizeof(int), | |
1687 | .mode = 0644, | |
1688 | .proc_handler = proc_dointvec, | |
1689 | }, | |
1da177e4 | 1690 | { |
1da177e4 LT |
1691 | .procname = "boot_id", |
1692 | .data = &sysctl_bootid, | |
1693 | .maxlen = 16, | |
1694 | .mode = 0444, | |
6d456111 | 1695 | .proc_handler = proc_do_uuid, |
1da177e4 LT |
1696 | }, |
1697 | { | |
1da177e4 LT |
1698 | .procname = "uuid", |
1699 | .maxlen = 16, | |
1700 | .mode = 0444, | |
6d456111 | 1701 | .proc_handler = proc_do_uuid, |
1da177e4 | 1702 | }, |
43759d4f TT |
1703 | #ifdef ADD_INTERRUPT_BENCH |
1704 | { | |
1705 | .procname = "add_interrupt_avg_cycles", | |
1706 | .data = &avg_cycles, | |
1707 | .maxlen = sizeof(avg_cycles), | |
1708 | .mode = 0444, | |
1709 | .proc_handler = proc_doulongvec_minmax, | |
1710 | }, | |
1711 | { | |
1712 | .procname = "add_interrupt_avg_deviation", | |
1713 | .data = &avg_deviation, | |
1714 | .maxlen = sizeof(avg_deviation), | |
1715 | .mode = 0444, | |
1716 | .proc_handler = proc_doulongvec_minmax, | |
1717 | }, | |
1718 | #endif | |
894d2491 | 1719 | { } |
1da177e4 | 1720 | }; |
5475e8f0 XN |
1721 | |
1722 | /* | |
1723 | * rand_initialize() is called before sysctl_init(), | |
1724 | * so we cannot call register_sysctl_init() in rand_initialize() | |
1725 | */ | |
1726 | static int __init random_sysctls_init(void) | |
1727 | { | |
1728 | register_sysctl_init("kernel/random", random_table); | |
1729 | return 0; | |
1730 | } | |
1731 | device_initcall(random_sysctls_init); | |
248045b8 | 1732 | #endif /* CONFIG_SYSCTL */ |
1da177e4 | 1733 | |
f5b98461 JD |
1734 | struct batched_entropy { |
1735 | union { | |
1ca1b917 EB |
1736 | u64 entropy_u64[CHACHA_BLOCK_SIZE / sizeof(u64)]; |
1737 | u32 entropy_u32[CHACHA_BLOCK_SIZE / sizeof(u32)]; | |
f5b98461 JD |
1738 | }; |
1739 | unsigned int position; | |
b7d5dc21 | 1740 | spinlock_t batch_lock; |
f5b98461 | 1741 | }; |
b1132dea | 1742 | |
1da177e4 | 1743 | /* |
f5b98461 | 1744 | * Get a random word for internal kernel use only. The quality of the random |
69efea71 JD |
1745 | * number is good as /dev/urandom, but there is no backtrack protection, with |
1746 | * the goal of being quite fast and not depleting entropy. In order to ensure | |
e297a783 | 1747 | * that the randomness provided by this function is okay, the function |
69efea71 JD |
1748 | * wait_for_random_bytes() should be called and return 0 at least once at any |
1749 | * point prior. | |
1da177e4 | 1750 | */ |
b7d5dc21 | 1751 | static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_u64) = { |
248045b8 | 1752 | .batch_lock = __SPIN_LOCK_UNLOCKED(batched_entropy_u64.lock), |
b7d5dc21 SAS |
1753 | }; |
1754 | ||
c440408c | 1755 | u64 get_random_u64(void) |
1da177e4 | 1756 | { |
c440408c | 1757 | u64 ret; |
b7d5dc21 | 1758 | unsigned long flags; |
f5b98461 | 1759 | struct batched_entropy *batch; |
eecabf56 | 1760 | static void *previous; |
8a0a9bd4 | 1761 | |
eecabf56 | 1762 | warn_unseeded_randomness(&previous); |
d06bfd19 | 1763 | |
b7d5dc21 SAS |
1764 | batch = raw_cpu_ptr(&batched_entropy_u64); |
1765 | spin_lock_irqsave(&batch->batch_lock, flags); | |
c440408c | 1766 | if (batch->position % ARRAY_SIZE(batch->entropy_u64) == 0) { |
a5e9f557 | 1767 | extract_crng((u8 *)batch->entropy_u64); |
f5b98461 JD |
1768 | batch->position = 0; |
1769 | } | |
c440408c | 1770 | ret = batch->entropy_u64[batch->position++]; |
b7d5dc21 | 1771 | spin_unlock_irqrestore(&batch->batch_lock, flags); |
8a0a9bd4 | 1772 | return ret; |
1da177e4 | 1773 | } |
c440408c | 1774 | EXPORT_SYMBOL(get_random_u64); |
1da177e4 | 1775 | |
b7d5dc21 | 1776 | static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_u32) = { |
248045b8 | 1777 | .batch_lock = __SPIN_LOCK_UNLOCKED(batched_entropy_u32.lock), |
b7d5dc21 | 1778 | }; |
c440408c | 1779 | u32 get_random_u32(void) |
f5b98461 | 1780 | { |
c440408c | 1781 | u32 ret; |
b7d5dc21 | 1782 | unsigned long flags; |
f5b98461 | 1783 | struct batched_entropy *batch; |
eecabf56 | 1784 | static void *previous; |
ec9ee4ac | 1785 | |
eecabf56 | 1786 | warn_unseeded_randomness(&previous); |
d06bfd19 | 1787 | |
b7d5dc21 SAS |
1788 | batch = raw_cpu_ptr(&batched_entropy_u32); |
1789 | spin_lock_irqsave(&batch->batch_lock, flags); | |
c440408c | 1790 | if (batch->position % ARRAY_SIZE(batch->entropy_u32) == 0) { |
a5e9f557 | 1791 | extract_crng((u8 *)batch->entropy_u32); |
f5b98461 JD |
1792 | batch->position = 0; |
1793 | } | |
c440408c | 1794 | ret = batch->entropy_u32[batch->position++]; |
b7d5dc21 | 1795 | spin_unlock_irqrestore(&batch->batch_lock, flags); |
ec9ee4ac DC |
1796 | return ret; |
1797 | } | |
c440408c | 1798 | EXPORT_SYMBOL(get_random_u32); |
ec9ee4ac | 1799 | |
b169c13d JD |
1800 | /* It's important to invalidate all potential batched entropy that might |
1801 | * be stored before the crng is initialized, which we can do lazily by | |
1802 | * simply resetting the counter to zero so that it's re-extracted on the | |
1803 | * next usage. */ | |
1804 | static void invalidate_batched_entropy(void) | |
1805 | { | |
1806 | int cpu; | |
1807 | unsigned long flags; | |
1808 | ||
248045b8 | 1809 | for_each_possible_cpu(cpu) { |
b7d5dc21 SAS |
1810 | struct batched_entropy *batched_entropy; |
1811 | ||
1812 | batched_entropy = per_cpu_ptr(&batched_entropy_u32, cpu); | |
1813 | spin_lock_irqsave(&batched_entropy->batch_lock, flags); | |
1814 | batched_entropy->position = 0; | |
1815 | spin_unlock(&batched_entropy->batch_lock); | |
1816 | ||
1817 | batched_entropy = per_cpu_ptr(&batched_entropy_u64, cpu); | |
1818 | spin_lock(&batched_entropy->batch_lock); | |
1819 | batched_entropy->position = 0; | |
1820 | spin_unlock_irqrestore(&batched_entropy->batch_lock, flags); | |
b169c13d | 1821 | } |
b169c13d JD |
1822 | } |
1823 | ||
99fdafde JC |
1824 | /** |
1825 | * randomize_page - Generate a random, page aligned address | |
1826 | * @start: The smallest acceptable address the caller will take. | |
1827 | * @range: The size of the area, starting at @start, within which the | |
1828 | * random address must fall. | |
1829 | * | |
1830 | * If @start + @range would overflow, @range is capped. | |
1831 | * | |
1832 | * NOTE: Historical use of randomize_range, which this replaces, presumed that | |
1833 | * @start was already page aligned. We now align it regardless. | |
1834 | * | |
1835 | * Return: A page aligned address within [start, start + range). On error, | |
1836 | * @start is returned. | |
1837 | */ | |
248045b8 | 1838 | unsigned long randomize_page(unsigned long start, unsigned long range) |
99fdafde JC |
1839 | { |
1840 | if (!PAGE_ALIGNED(start)) { | |
1841 | range -= PAGE_ALIGN(start) - start; | |
1842 | start = PAGE_ALIGN(start); | |
1843 | } | |
1844 | ||
1845 | if (start > ULONG_MAX - range) | |
1846 | range = ULONG_MAX - start; | |
1847 | ||
1848 | range >>= PAGE_SHIFT; | |
1849 | ||
1850 | if (range == 0) | |
1851 | return start; | |
1852 | ||
1853 | return start + (get_random_long() % range << PAGE_SHIFT); | |
1854 | } | |
1855 | ||
c84dbf61 TD |
1856 | /* Interface for in-kernel drivers of true hardware RNGs. |
1857 | * Those devices may produce endless random bits and will be throttled | |
1858 | * when our pool is full. | |
1859 | */ | |
1860 | void add_hwgenerator_randomness(const char *buffer, size_t count, | |
1861 | size_t entropy) | |
1862 | { | |
43838a23 | 1863 | if (unlikely(crng_init == 0)) { |
73c7733f | 1864 | size_t ret = crng_fast_load(buffer, count); |
90ed1e67 | 1865 | mix_pool_bytes(buffer, ret); |
73c7733f JD |
1866 | count -= ret; |
1867 | buffer += ret; | |
1868 | if (!count || crng_init == 0) | |
1869 | return; | |
3371f3da | 1870 | } |
e192be9d | 1871 | |
c321e907 | 1872 | /* Throttle writing if we're above the trickle threshold. |
489c7fc4 JD |
1873 | * We'll be woken up again once below POOL_MIN_BITS, when |
1874 | * the calling thread is about to terminate, or once | |
1875 | * CRNG_RESEED_INTERVAL has elapsed. | |
e192be9d | 1876 | */ |
c321e907 | 1877 | wait_event_interruptible_timeout(random_write_wait, |
f7e67b8e | 1878 | !system_wq || kthread_should_stop() || |
489c7fc4 | 1879 | input_pool.entropy_count < POOL_MIN_BITS, |
c321e907 | 1880 | CRNG_RESEED_INTERVAL); |
90ed1e67 JD |
1881 | mix_pool_bytes(buffer, count); |
1882 | credit_entropy_bits(entropy); | |
c84dbf61 TD |
1883 | } |
1884 | EXPORT_SYMBOL_GPL(add_hwgenerator_randomness); | |
428826f5 HYW |
1885 | |
1886 | /* Handle random seed passed by bootloader. | |
1887 | * If the seed is trustworthy, it would be regarded as hardware RNGs. Otherwise | |
1888 | * it would be regarded as device data. | |
1889 | * The decision is controlled by CONFIG_RANDOM_TRUST_BOOTLOADER. | |
1890 | */ | |
1891 | void add_bootloader_randomness(const void *buf, unsigned int size) | |
1892 | { | |
1893 | if (IS_ENABLED(CONFIG_RANDOM_TRUST_BOOTLOADER)) | |
1894 | add_hwgenerator_randomness(buf, size, size * 8); | |
1895 | else | |
1896 | add_device_randomness(buf, size); | |
1897 | } | |
3fd57e7a | 1898 | EXPORT_SYMBOL_GPL(add_bootloader_randomness); |