mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2025-01-01 10:43:43 +00:00
a98814cef8
Since we use prandom*() functions quite often in networking code i.e. in UDP port selection, netfilter code, etc, upgrade the PRNG from Pierre L'Ecuyer's original paper "Maximally Equidistributed Combined Tausworthe Generators", Mathematics of Computation, 65, 213 (1996), 203--213 to the version published in his errata paper [1]. The Tausworthe generator is a maximally-equidistributed generator, that is fast and has good statistical properties [1]. The version presented there upgrades the 3 state LFSR to a 4 state LFSR with increased periodicity from about 2^88 to 2^113. The algorithm is presented in [1] by the very same author who also designed the original algorithm in [2]. Also, by increasing the state, we make it a bit harder for attackers to "guess" the PRNGs internal state. See also discussion in [3]. Now, as we use this sort of weak initialization discussed in [3] only between core_initcall() until late_initcall() time [*] for prandom32*() users, namely in prandom_init(), it is less relevant from late_initcall() onwards as we overwrite seeds through prandom_reseed() anyways with a seed source of higher entropy, that is, get_random_bytes(). In other words, a exhaustive keysearch of 96 bit would be needed. Now, with the help of this patch, this state-search increases further to 128 bit. Initialization needs to make sure that s1 > 1, s2 > 7, s3 > 15, s4 > 127. taus88 and taus113 algorithm is also part of GSL. I added a test case in the next patch to verify internal behaviour of this patch with GSL and ran tests with the dieharder 3.31.1 RNG test suite: $ dieharder -g 052 -a -m 10 -s 1 -S 4137730333 #taus88 $ dieharder -g 054 -a -m 10 -s 1 -S 4137730333 #taus113 With this seed configuration, in order to compare both, we get the following differences: algorithm taus88 taus113 rands/second [**] 1.61e+08 1.37e+08 sts_serial(4, 1st run) WEAK PASSED sts_serial(9, 2nd run) WEAK PASSED rgb_lagged_sum(31) WEAK PASSED We took out diehard_sums test as according to the authors it is considered broken and unusable [4]. Despite that and the slight decrease in performance (which is acceptable), taus113 here passes all 113 tests (only rgb_minimum_distance_5 in WEAK, the rest PASSED). In general, taus/taus113 is considered "very good" by the authors of dieharder [5]. The papers [1][2] states a single warm-up step is sufficient by running quicktaus once on each state to ensure proper initialization of ~s_{0}: Our selection of (s) according to Table 1 of [1] row 1 holds the condition L - k <= r - s, that is, (32 32 32 32) - (31 29 28 25) <= (25 27 15 22) - (18 2 7 13) with r = k - q and q = (6 2 13 3) as also stated by the paper. So according to [2] we are safe with one round of quicktaus for initialization. However we decided to include the warm-up phase of the PRNG as done in GSL in every case as a safety net. We also use the warm up phase to make the output of the RNG easier to verify by the GSL output. In prandom_init(), we also mix random_get_entropy() into it, just like drivers/char/random.c does it, jiffies ^ random_get_entropy(). random-get_entropy() is get_cycles(). xor is entropy preserving so it is fine if it is not implemented by some architectures. Note, this PRNG is *not* used for cryptography in the kernel, but rather as a fast PRNG for various randomizations i.e. in the networking code, or elsewhere for debugging purposes, for example. [*]: In order to generate some "sort of pseduo-randomness", since get_random_bytes() is not yet available for us, we use jiffies and initialize states s1 - s3 with a simple linear congruential generator (LCG), that is x <- x * 69069; and derive s2, s3, from the 32bit initialization from s1. So the above quote from [3] accounts only for the time from core to late initcall, not afterwards. [**] Single threaded run on MacBook Air w/ Intel Core i5-3317U [1] http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps [2] http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps [3] http://thread.gmane.org/gmane.comp.encryption.general/12103/ [4] http://code.google.com/p/dieharder/source/browse/trunk/libdieharder/diehard_sums.c?spec=svn490&r=490#20 [5] http://www.phy.duke.edu/~rgb/General/dieharder.php Joint work with Hannes Frederic Sowa. Cc: Florian Weimer <fweimer@redhat.com> Cc: Theodore Ts'o <tytso@mit.edu> Signed-off-by: Daniel Borkmann <dborkman@redhat.com> Signed-off-by: Hannes Frederic Sowa <hannes@stressinduktion.org> Signed-off-by: David S. Miller <davem@davemloft.net>
256 lines
6.7 KiB
C
256 lines
6.7 KiB
C
/*
|
|
This is a maximally equidistributed combined Tausworthe generator
|
|
based on code from GNU Scientific Library 1.5 (30 Jun 2004)
|
|
|
|
lfsr113 version:
|
|
|
|
x_n = (s1_n ^ s2_n ^ s3_n ^ s4_n)
|
|
|
|
s1_{n+1} = (((s1_n & 4294967294) << 18) ^ (((s1_n << 6) ^ s1_n) >> 13))
|
|
s2_{n+1} = (((s2_n & 4294967288) << 2) ^ (((s2_n << 2) ^ s2_n) >> 27))
|
|
s3_{n+1} = (((s3_n & 4294967280) << 7) ^ (((s3_n << 13) ^ s3_n) >> 21))
|
|
s4_{n+1} = (((s4_n & 4294967168) << 13) ^ (((s4_n << 3) ^ s4_n) >> 12))
|
|
|
|
The period of this generator is about 2^113 (see erratum paper).
|
|
|
|
From: P. L'Ecuyer, "Maximally Equidistributed Combined Tausworthe
|
|
Generators", Mathematics of Computation, 65, 213 (1996), 203--213:
|
|
http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
|
|
ftp://ftp.iro.umontreal.ca/pub/simulation/lecuyer/papers/tausme.ps
|
|
|
|
There is an erratum in the paper "Tables of Maximally
|
|
Equidistributed Combined LFSR Generators", Mathematics of
|
|
Computation, 68, 225 (1999), 261--269:
|
|
http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
|
|
|
|
... the k_j most significant bits of z_j must be non-
|
|
zero, for each j. (Note: this restriction also applies to the
|
|
computer code given in [4], but was mistakenly not mentioned in
|
|
that paper.)
|
|
|
|
This affects the seeding procedure by imposing the requirement
|
|
s1 > 1, s2 > 7, s3 > 15, s4 > 127.
|
|
|
|
*/
|
|
|
|
#include <linux/types.h>
|
|
#include <linux/percpu.h>
|
|
#include <linux/export.h>
|
|
#include <linux/jiffies.h>
|
|
#include <linux/random.h>
|
|
|
|
static DEFINE_PER_CPU(struct rnd_state, net_rand_state);
|
|
|
|
/**
|
|
* prandom_u32_state - seeded pseudo-random number generator.
|
|
* @state: pointer to state structure holding seeded state.
|
|
*
|
|
* This is used for pseudo-randomness with no outside seeding.
|
|
* For more random results, use prandom_u32().
|
|
*/
|
|
u32 prandom_u32_state(struct rnd_state *state)
|
|
{
|
|
#define TAUSWORTHE(s,a,b,c,d) ((s&c)<<d) ^ (((s <<a) ^ s)>>b)
|
|
|
|
state->s1 = TAUSWORTHE(state->s1, 6U, 13U, 4294967294U, 18U);
|
|
state->s2 = TAUSWORTHE(state->s2, 2U, 27U, 4294967288U, 2U);
|
|
state->s3 = TAUSWORTHE(state->s3, 13U, 21U, 4294967280U, 7U);
|
|
state->s4 = TAUSWORTHE(state->s4, 3U, 12U, 4294967168U, 13U);
|
|
|
|
return (state->s1 ^ state->s2 ^ state->s3 ^ state->s4);
|
|
}
|
|
EXPORT_SYMBOL(prandom_u32_state);
|
|
|
|
/**
|
|
* prandom_u32 - pseudo random number generator
|
|
*
|
|
* A 32 bit pseudo-random number is generated using a fast
|
|
* algorithm suitable for simulation. This algorithm is NOT
|
|
* considered safe for cryptographic use.
|
|
*/
|
|
u32 prandom_u32(void)
|
|
{
|
|
unsigned long r;
|
|
struct rnd_state *state = &get_cpu_var(net_rand_state);
|
|
r = prandom_u32_state(state);
|
|
put_cpu_var(state);
|
|
return r;
|
|
}
|
|
EXPORT_SYMBOL(prandom_u32);
|
|
|
|
/*
|
|
* prandom_bytes_state - get the requested number of pseudo-random bytes
|
|
*
|
|
* @state: pointer to state structure holding seeded state.
|
|
* @buf: where to copy the pseudo-random bytes to
|
|
* @bytes: the requested number of bytes
|
|
*
|
|
* This is used for pseudo-randomness with no outside seeding.
|
|
* For more random results, use prandom_bytes().
|
|
*/
|
|
void prandom_bytes_state(struct rnd_state *state, void *buf, int bytes)
|
|
{
|
|
unsigned char *p = buf;
|
|
int i;
|
|
|
|
for (i = 0; i < round_down(bytes, sizeof(u32)); i += sizeof(u32)) {
|
|
u32 random = prandom_u32_state(state);
|
|
int j;
|
|
|
|
for (j = 0; j < sizeof(u32); j++) {
|
|
p[i + j] = random;
|
|
random >>= BITS_PER_BYTE;
|
|
}
|
|
}
|
|
if (i < bytes) {
|
|
u32 random = prandom_u32_state(state);
|
|
|
|
for (; i < bytes; i++) {
|
|
p[i] = random;
|
|
random >>= BITS_PER_BYTE;
|
|
}
|
|
}
|
|
}
|
|
EXPORT_SYMBOL(prandom_bytes_state);
|
|
|
|
/**
|
|
* prandom_bytes - get the requested number of pseudo-random bytes
|
|
* @buf: where to copy the pseudo-random bytes to
|
|
* @bytes: the requested number of bytes
|
|
*/
|
|
void prandom_bytes(void *buf, int bytes)
|
|
{
|
|
struct rnd_state *state = &get_cpu_var(net_rand_state);
|
|
|
|
prandom_bytes_state(state, buf, bytes);
|
|
put_cpu_var(state);
|
|
}
|
|
EXPORT_SYMBOL(prandom_bytes);
|
|
|
|
static void prandom_warmup(struct rnd_state *state)
|
|
{
|
|
/* Calling RNG ten times to satify recurrence condition */
|
|
prandom_u32_state(state);
|
|
prandom_u32_state(state);
|
|
prandom_u32_state(state);
|
|
prandom_u32_state(state);
|
|
prandom_u32_state(state);
|
|
prandom_u32_state(state);
|
|
prandom_u32_state(state);
|
|
prandom_u32_state(state);
|
|
prandom_u32_state(state);
|
|
prandom_u32_state(state);
|
|
}
|
|
|
|
/**
|
|
* prandom_seed - add entropy to pseudo random number generator
|
|
* @seed: seed value
|
|
*
|
|
* Add some additional seeding to the prandom pool.
|
|
*/
|
|
void prandom_seed(u32 entropy)
|
|
{
|
|
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, 2U);
|
|
prandom_warmup(state);
|
|
}
|
|
}
|
|
EXPORT_SYMBOL(prandom_seed);
|
|
|
|
/*
|
|
* Generate some initially weak seeding values to allow
|
|
* to start the prandom_u32() engine.
|
|
*/
|
|
static int __init prandom_init(void)
|
|
{
|
|
int i;
|
|
|
|
for_each_possible_cpu(i) {
|
|
struct rnd_state *state = &per_cpu(net_rand_state,i);
|
|
|
|
#define LCG(x) ((x) * 69069U) /* super-duper LCG */
|
|
state->s1 = __seed(LCG((i + jiffies) ^ random_get_entropy()), 2U);
|
|
state->s2 = __seed(LCG(state->s1), 8U);
|
|
state->s3 = __seed(LCG(state->s2), 16U);
|
|
state->s4 = __seed(LCG(state->s3), 128U);
|
|
|
|
prandom_warmup(state);
|
|
}
|
|
return 0;
|
|
}
|
|
core_initcall(prandom_init);
|
|
|
|
static void __prandom_timer(unsigned long dontcare);
|
|
static DEFINE_TIMER(seed_timer, __prandom_timer, 0, 0);
|
|
|
|
static void __prandom_timer(unsigned long dontcare)
|
|
{
|
|
u32 entropy;
|
|
|
|
get_random_bytes(&entropy, sizeof(entropy));
|
|
prandom_seed(entropy);
|
|
/* reseed every ~60 seconds, in [40 .. 80) interval with slack */
|
|
seed_timer.expires = jiffies + (40 * HZ + (prandom_u32() % (40 * HZ)));
|
|
add_timer(&seed_timer);
|
|
}
|
|
|
|
static void prandom_start_seed_timer(void)
|
|
{
|
|
set_timer_slack(&seed_timer, HZ);
|
|
seed_timer.expires = jiffies + 40 * HZ;
|
|
add_timer(&seed_timer);
|
|
}
|
|
|
|
/*
|
|
* Generate better values after random number generator
|
|
* is fully initialized.
|
|
*/
|
|
static void __prandom_reseed(bool late)
|
|
{
|
|
int i;
|
|
unsigned long flags;
|
|
static bool latch = false;
|
|
static DEFINE_SPINLOCK(lock);
|
|
|
|
/* only allow initial seeding (late == false) once */
|
|
spin_lock_irqsave(&lock, flags);
|
|
if (latch && !late)
|
|
goto out;
|
|
latch = true;
|
|
|
|
for_each_possible_cpu(i) {
|
|
struct rnd_state *state = &per_cpu(net_rand_state,i);
|
|
u32 seeds[4];
|
|
|
|
get_random_bytes(&seeds, sizeof(seeds));
|
|
state->s1 = __seed(seeds[0], 2U);
|
|
state->s2 = __seed(seeds[1], 8U);
|
|
state->s3 = __seed(seeds[2], 16U);
|
|
state->s4 = __seed(seeds[3], 128U);
|
|
|
|
prandom_warmup(state);
|
|
}
|
|
out:
|
|
spin_unlock_irqrestore(&lock, flags);
|
|
}
|
|
|
|
void prandom_reseed_late(void)
|
|
{
|
|
__prandom_reseed(true);
|
|
}
|
|
|
|
static int __init prandom_reseed(void)
|
|
{
|
|
__prandom_reseed(false);
|
|
prandom_start_seed_timer();
|
|
return 0;
|
|
}
|
|
late_initcall(prandom_reseed);
|