GNU Linux-libre 4.19.264-gnu1
[releases.git] / include / linux / prandom.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * include/linux/prandom.h
4  *
5  * Include file for the fast pseudo-random 32-bit
6  * generation.
7  */
8 #ifndef _LINUX_PRANDOM_H
9 #define _LINUX_PRANDOM_H
10
11 #include <linux/types.h>
12 #include <linux/percpu.h>
13 #include <linux/siphash.h>
14
15 u32 prandom_u32(void);
16 void prandom_bytes(void *buf, size_t nbytes);
17 void prandom_seed(u32 seed);
18 void prandom_reseed_late(void);
19
20 #if BITS_PER_LONG == 64
21 /*
22  * The core SipHash round function.  Each line can be executed in
23  * parallel given enough CPU resources.
24  */
25 #define PRND_SIPROUND(v0, v1, v2, v3) SIPHASH_PERMUTATION(v0, v1, v2, v3)
26
27 #define PRND_K0 (SIPHASH_CONST_0 ^ SIPHASH_CONST_2)
28 #define PRND_K1 (SIPHASH_CONST_1 ^ SIPHASH_CONST_3)
29
30 #elif BITS_PER_LONG == 32
31 /*
32  * On 32-bit machines, we use HSipHash, a reduced-width version of SipHash.
33  * This is weaker, but 32-bit machines are not used for high-traffic
34  * applications, so there is less output for an attacker to analyze.
35  */
36 #define PRND_SIPROUND(v0, v1, v2, v3) HSIPHASH_PERMUTATION(v0, v1, v2, v3)
37 #define PRND_K0 (HSIPHASH_CONST_0 ^ HSIPHASH_CONST_2)
38 #define PRND_K1 (HSIPHASH_CONST_1 ^ HSIPHASH_CONST_3)
39
40 #else
41 #error Unsupported BITS_PER_LONG
42 #endif
43
44 struct rnd_state {
45         __u32 s1, s2, s3, s4;
46 };
47
48 u32 prandom_u32_state(struct rnd_state *state);
49 void prandom_bytes_state(struct rnd_state *state, void *buf, size_t nbytes);
50 void prandom_seed_full_state(struct rnd_state __percpu *pcpu_state);
51
52 #define prandom_init_once(pcpu_state)                   \
53         DO_ONCE(prandom_seed_full_state, (pcpu_state))
54
55 /**
56  * prandom_u32_max - returns a pseudo-random number in interval [0, ep_ro)
57  * @ep_ro: right open interval endpoint
58  *
59  * Returns a pseudo-random number that is in interval [0, ep_ro). Note
60  * that the result depends on PRNG being well distributed in [0, ~0U]
61  * u32 space. Here we use maximally equidistributed combined Tausworthe
62  * generator, that is, prandom_u32(). This is useful when requesting a
63  * random index of an array containing ep_ro elements, for example.
64  *
65  * Returns: pseudo-random number in interval [0, ep_ro)
66  */
67 static inline u32 prandom_u32_max(u32 ep_ro)
68 {
69         return (u32)(((u64) prandom_u32() * ep_ro) >> 32);
70 }
71
72 /*
73  * Handle minimum values for seeds
74  */
75 static inline u32 __seed(u32 x, u32 m)
76 {
77         return (x < m) ? x + m : x;
78 }
79
80 /**
81  * prandom_seed_state - set seed for prandom_u32_state().
82  * @state: pointer to state structure to receive the seed.
83  * @seed: arbitrary 64-bit value to use as a seed.
84  */
85 static inline void prandom_seed_state(struct rnd_state *state, u64 seed)
86 {
87         u32 i = ((seed >> 32) ^ (seed << 10) ^ seed) & 0xffffffffUL;
88
89         state->s1 = __seed(i,   2U);
90         state->s2 = __seed(i,   8U);
91         state->s3 = __seed(i,  16U);
92         state->s4 = __seed(i, 128U);
93 }
94
95 /* Pseudo random number generator from numerical recipes. */
96 static inline u32 next_pseudo_random32(u32 seed)
97 {
98         return seed * 1664525 + 1013904223;
99 }
100
101 #endif