aboutsummaryrefslogtreecommitdiffstats
path: root/lib/random32.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/random32.c')
-rw-r--r--lib/random32.c97
1 files changed, 73 insertions, 24 deletions
diff --git a/lib/random32.c b/lib/random32.c
index 938bde5876ac..52280d5526be 100644
--- a/lib/random32.c
+++ b/lib/random32.c
@@ -42,13 +42,13 @@
42static DEFINE_PER_CPU(struct rnd_state, net_rand_state); 42static DEFINE_PER_CPU(struct rnd_state, net_rand_state);
43 43
44/** 44/**
45 * prandom32 - seeded pseudo-random number generator. 45 * prandom_u32_state - seeded pseudo-random number generator.
46 * @state: pointer to state structure holding seeded state. 46 * @state: pointer to state structure holding seeded state.
47 * 47 *
48 * This is used for pseudo-randomness with no outside seeding. 48 * This is used for pseudo-randomness with no outside seeding.
49 * For more random results, use random32(). 49 * For more random results, use prandom_u32().
50 */ 50 */
51u32 prandom32(struct rnd_state *state) 51u32 prandom_u32_state(struct rnd_state *state)
52{ 52{
53#define TAUSWORTHE(s,a,b,c,d) ((s&c)<<d) ^ (((s <<a) ^ s)>>b) 53#define TAUSWORTHE(s,a,b,c,d) ((s&c)<<d) ^ (((s <<a) ^ s)>>b)
54 54
@@ -58,32 +58,81 @@ u32 prandom32(struct rnd_state *state)
58 58
59 return (state->s1 ^ state->s2 ^ state->s3); 59 return (state->s1 ^ state->s2 ^ state->s3);
60} 60}
61EXPORT_SYMBOL(prandom32); 61EXPORT_SYMBOL(prandom_u32_state);
62 62
63/** 63/**
64 * random32 - pseudo random number generator 64 * prandom_u32 - pseudo random number generator
65 * 65 *
66 * A 32 bit pseudo-random number is generated using a fast 66 * A 32 bit pseudo-random number is generated using a fast
67 * algorithm suitable for simulation. This algorithm is NOT 67 * algorithm suitable for simulation. This algorithm is NOT
68 * considered safe for cryptographic use. 68 * considered safe for cryptographic use.
69 */ 69 */
70u32 random32(void) 70u32 prandom_u32(void)
71{ 71{
72 unsigned long r; 72 unsigned long r;
73 struct rnd_state *state = &get_cpu_var(net_rand_state); 73 struct rnd_state *state = &get_cpu_var(net_rand_state);
74 r = prandom32(state); 74 r = prandom_u32_state(state);
75 put_cpu_var(state); 75 put_cpu_var(state);
76 return r; 76 return r;
77} 77}
78EXPORT_SYMBOL(random32); 78EXPORT_SYMBOL(prandom_u32);
79
80/*
81 * prandom_bytes_state - get the requested number of pseudo-random bytes
82 *
83 * @state: pointer to state structure holding seeded state.
84 * @buf: where to copy the pseudo-random bytes to
85 * @bytes: the requested number of bytes
86 *
87 * This is used for pseudo-randomness with no outside seeding.
88 * For more random results, use prandom_bytes().
89 */
90void prandom_bytes_state(struct rnd_state *state, void *buf, int bytes)
91{
92 unsigned char *p = buf;
93 int i;
94
95 for (i = 0; i < round_down(bytes, sizeof(u32)); i += sizeof(u32)) {
96 u32 random = prandom_u32_state(state);
97 int j;
98
99 for (j = 0; j < sizeof(u32); j++) {
100 p[i + j] = random;
101 random >>= BITS_PER_BYTE;
102 }
103 }
104 if (i < bytes) {
105 u32 random = prandom_u32_state(state);
106
107 for (; i < bytes; i++) {
108 p[i] = random;
109 random >>= BITS_PER_BYTE;
110 }
111 }
112}
113EXPORT_SYMBOL(prandom_bytes_state);
114
115/**
116 * prandom_bytes - get the requested number of pseudo-random bytes
117 * @buf: where to copy the pseudo-random bytes to
118 * @bytes: the requested number of bytes
119 */
120void prandom_bytes(void *buf, int bytes)
121{
122 struct rnd_state *state = &get_cpu_var(net_rand_state);
123
124 prandom_bytes_state(state, buf, bytes);
125 put_cpu_var(state);
126}
127EXPORT_SYMBOL(prandom_bytes);
79 128
80/** 129/**
81 * srandom32 - add entropy to pseudo random number generator 130 * prandom_seed - add entropy to pseudo random number generator
82 * @seed: seed value 131 * @seed: seed value
83 * 132 *
84 * Add some additional seeding to the random32() pool. 133 * Add some additional seeding to the prandom pool.
85 */ 134 */
86void srandom32(u32 entropy) 135void prandom_seed(u32 entropy)
87{ 136{
88 int i; 137 int i;
89 /* 138 /*
@@ -95,13 +144,13 @@ void srandom32(u32 entropy)
95 state->s1 = __seed(state->s1 ^ entropy, 1); 144 state->s1 = __seed(state->s1 ^ entropy, 1);
96 } 145 }
97} 146}
98EXPORT_SYMBOL(srandom32); 147EXPORT_SYMBOL(prandom_seed);
99 148
100/* 149/*
101 * Generate some initially weak seeding values to allow 150 * Generate some initially weak seeding values to allow
102 * to start the random32() engine. 151 * to start the prandom_u32() engine.
103 */ 152 */
104static int __init random32_init(void) 153static int __init prandom_init(void)
105{ 154{
106 int i; 155 int i;
107 156
@@ -114,22 +163,22 @@ static int __init random32_init(void)
114 state->s3 = __seed(LCG(state->s2), 15); 163 state->s3 = __seed(LCG(state->s2), 15);
115 164
116 /* "warm it up" */ 165 /* "warm it up" */
117 prandom32(state); 166 prandom_u32_state(state);
118 prandom32(state); 167 prandom_u32_state(state);
119 prandom32(state); 168 prandom_u32_state(state);
120 prandom32(state); 169 prandom_u32_state(state);
121 prandom32(state); 170 prandom_u32_state(state);
122 prandom32(state); 171 prandom_u32_state(state);
123 } 172 }
124 return 0; 173 return 0;
125} 174}
126core_initcall(random32_init); 175core_initcall(prandom_init);
127 176
128/* 177/*
129 * Generate better values after random number generator 178 * Generate better values after random number generator
130 * is fully initialized. 179 * is fully initialized.
131 */ 180 */
132static int __init random32_reseed(void) 181static int __init prandom_reseed(void)
133{ 182{
134 int i; 183 int i;
135 184
@@ -143,8 +192,8 @@ static int __init random32_reseed(void)
143 state->s3 = __seed(seeds[2], 15); 192 state->s3 = __seed(seeds[2], 15);
144 193
145 /* mix it in */ 194 /* mix it in */
146 prandom32(state); 195 prandom_u32_state(state);
147 } 196 }
148 return 0; 197 return 0;
149} 198}
150late_initcall(random32_reseed); 199late_initcall(prandom_reseed);