X-Git-Url: http://ftp.safe.ca/?a=blobdiff_plain;f=lib%2Frandom32.c;h=217d5c4b666d22e4f309ad363c157580c2810bf8;hb=e0c8233622cbd49d171bc57b60e725f2fb748750;hp=4a15ce51cea78f4843106221f3db13745b1eb4d7;hpb=aaa248f6c9c81b2683db7dbb0689cd5ed1c86d88;p=safe%2Fjmp%2Flinux-2.6 diff --git a/lib/random32.c b/lib/random32.c index 4a15ce5..217d5c4 100644 --- a/lib/random32.c +++ b/lib/random32.c @@ -36,6 +36,7 @@ #include #include #include +#include #include struct rnd_state { @@ -55,23 +56,12 @@ static u32 __random32(struct rnd_state *state) return (state->s1 ^ state->s2 ^ state->s3); } -static void __set_random32(struct rnd_state *state, unsigned long s) +/* + * Handle minimum values for seeds + */ +static inline u32 __seed(u32 x, u32 m) { - if (s == 0) - s = 1; /* default seed is 1 */ - -#define LCG(n) (69069 * n) - state->s1 = LCG(s); - state->s2 = LCG(state->s1); - state->s3 = LCG(state->s2); - - /* "warm it up" */ - __random32(state); - __random32(state); - __random32(state); - __random32(state); - __random32(state); - __random32(state); + return (x < m) ? x + m : x; } /** @@ -96,13 +86,18 @@ EXPORT_SYMBOL(random32); * @seed: seed value * * Add some additional seeding to the random32() pool. - * Note: this pool is per cpu so it only affects current CPU. */ void srandom32(u32 entropy) { - struct rnd_state *state = &get_cpu_var(net_rand_state); - __set_random32(state, state->s1 ^ entropy); - put_cpu_var(state); + int i; + /* + * No locking on the CPUs, but then somewhat random results are, well, + * expected. + */ + for_each_possible_cpu (i) { + struct rnd_state *state = &per_cpu(net_rand_state, i); + state->s1 = __seed(state->s1 ^ entropy, 1); + } } EXPORT_SYMBOL(srandom32); @@ -116,7 +111,19 @@ static int __init random32_init(void) for_each_possible_cpu(i) { struct rnd_state *state = &per_cpu(net_rand_state,i); - __set_random32(state, i + jiffies); + +#define LCG(x) ((x) * 69069) /* super-duper LCG */ + state->s1 = __seed(LCG(i + jiffies), 1); + state->s2 = __seed(LCG(state->s1), 7); + state->s3 = __seed(LCG(state->s2), 15); + + /* "warm it up" */ + __random32(state); + __random32(state); + __random32(state); + __random32(state); + __random32(state); + __random32(state); } return 0; } @@ -129,13 +136,18 @@ core_initcall(random32_init); static int __init random32_reseed(void) { int i; - unsigned long seed; for_each_possible_cpu(i) { struct rnd_state *state = &per_cpu(net_rand_state,i); + u32 seeds[3]; + + get_random_bytes(&seeds, sizeof(seeds)); + state->s1 = __seed(seeds[0], 1); + state->s2 = __seed(seeds[1], 7); + state->s3 = __seed(seeds[2], 15); - get_random_bytes(&seed, sizeof(seed)); - __set_random32(state, seed); + /* mix it in */ + __random32(state); } return 0; }