hc
2024-01-05 071106ecf68c401173c58808b1cf5f68cc50d390
kernel/include/linux/prandom.h
....@@ -10,26 +10,28 @@
1010
1111 #include <linux/types.h>
1212 #include <linux/percpu.h>
13
+#include <linux/siphash.h>
1314
1415 u32 prandom_u32(void);
1516 void prandom_bytes(void *buf, size_t nbytes);
1617 void prandom_seed(u32 seed);
1718 void prandom_reseed_late(void);
1819
20
+DECLARE_PER_CPU(unsigned long, net_rand_noise);
21
+
22
+#define PRANDOM_ADD_NOISE(a, b, c, d) \
23
+ prandom_u32_add_noise((unsigned long)(a), (unsigned long)(b), \
24
+ (unsigned long)(c), (unsigned long)(d))
25
+
1926 #if BITS_PER_LONG == 64
2027 /*
2128 * The core SipHash round function. Each line can be executed in
2229 * parallel given enough CPU resources.
2330 */
24
-#define PRND_SIPROUND(v0, v1, v2, v3) ( \
25
- v0 += v1, v1 = rol64(v1, 13), v2 += v3, v3 = rol64(v3, 16), \
26
- v1 ^= v0, v0 = rol64(v0, 32), v3 ^= v2, \
27
- v0 += v3, v3 = rol64(v3, 21), v2 += v1, v1 = rol64(v1, 17), \
28
- v3 ^= v0, v1 ^= v2, v2 = rol64(v2, 32) \
29
-)
31
+#define PRND_SIPROUND(v0, v1, v2, v3) SIPHASH_PERMUTATION(v0, v1, v2, v3)
3032
31
-#define PRND_K0 (0x736f6d6570736575 ^ 0x6c7967656e657261)
32
-#define PRND_K1 (0x646f72616e646f6d ^ 0x7465646279746573)
33
+#define PRND_K0 (SIPHASH_CONST_0 ^ SIPHASH_CONST_2)
34
+#define PRND_K1 (SIPHASH_CONST_1 ^ SIPHASH_CONST_3)
3335
3436 #elif BITS_PER_LONG == 32
3537 /*
....@@ -37,18 +39,25 @@
3739 * This is weaker, but 32-bit machines are not used for high-traffic
3840 * applications, so there is less output for an attacker to analyze.
3941 */
40
-#define PRND_SIPROUND(v0, v1, v2, v3) ( \
41
- v0 += v1, v1 = rol32(v1, 5), v2 += v3, v3 = rol32(v3, 8), \
42
- v1 ^= v0, v0 = rol32(v0, 16), v3 ^= v2, \
43
- v0 += v3, v3 = rol32(v3, 7), v2 += v1, v1 = rol32(v1, 13), \
44
- v3 ^= v0, v1 ^= v2, v2 = rol32(v2, 16) \
45
-)
46
-#define PRND_K0 0x6c796765
47
-#define PRND_K1 0x74656462
42
+#define PRND_SIPROUND(v0, v1, v2, v3) HSIPHASH_PERMUTATION(v0, v1, v2, v3)
43
+#define PRND_K0 (HSIPHASH_CONST_0 ^ HSIPHASH_CONST_2)
44
+#define PRND_K1 (HSIPHASH_CONST_1 ^ HSIPHASH_CONST_3)
4845
4946 #else
5047 #error Unsupported BITS_PER_LONG
5148 #endif
49
+
50
+static inline void prandom_u32_add_noise(unsigned long a, unsigned long b,
51
+ unsigned long c, unsigned long d)
52
+{
53
+ /*
54
+ * This is not used cryptographically; it's just
55
+ * a convenient 4-word hash function. (3 xor, 2 add, 2 rol)
56
+ */
57
+ a ^= raw_cpu_read(net_rand_noise);
58
+ PRND_SIPROUND(a, b, c, d);
59
+ raw_cpu_write(net_rand_noise, d);
60
+}
5261
5362 struct rnd_state {
5463 __u32 s1, s2, s3, s4;
....@@ -99,6 +108,7 @@
99108 state->s2 = __seed(i, 8U);
100109 state->s3 = __seed(i, 16U);
101110 state->s4 = __seed(i, 128U);
111
+ PRANDOM_ADD_NOISE(state, i, 0, 0);
102112 }
103113
104114 /* Pseudo random number generator from numerical recipes. */