All of lore.kernel.org
 help / color / mirror / Atom feed
From: Eric Dumazet <dada1@cosmosbay.com>
To: Alan Cox <alan@lxorguk.ukuu.org.uk>
Cc: Adrian Bunk <bunk@kernel.org>,
	Marc Haber <mh+linux-kernel@zugschlus.de>,
	linux-kernel@vger.kernel.org,
	"David S. Miller" <davem@davemloft.net>
Subject: Re: Why does reading from /dev/urandom deplete entropy so much?
Date: Tue, 04 Dec 2007 19:17:58 +0100	[thread overview]
Message-ID: <475599D6.4030008@cosmosbay.com> (raw)
In-Reply-To: <20071204164720.6e4dc2c4@the-village.bc.nu>

[-- Attachment #1: Type: text/plain, Size: 1084 bytes --]

Alan Cox a écrit :
>> No matter what you consider as being better, changing a 12 years old and 
>> widely used userspace interface like /dev/urandom is simply not an 
>> option.
>>     
>
> Fixing it to be more efficient in its use of entropy and also fixing the
> fact its not actually a good random number source would be worth looking
> at however.
>   
Yes, since current behavior on network irq is very pessimistic.

If you have some trafic, (ie more than HZ/2  interrupts per second), 
then add_timer_randomness() feeds
 some entropy but gives no credit (calling credit_entropy_store() with 
nbits=0)

This is because we take into account only the jiffies difference, and 
not the get_cycles() that should give
 us more entropy on most plaforms.

In this patch, I suggest that we feed only one u32 word of entropy, 
combination of the previous distinct
words (with some of them being constant or so), so that the nbits 
estimation is less pessimistic, but also to
avoid injecting false entropy.

Signed-off-by: Eric Dumazet <dada1@cosmosbay.com>



[-- Attachment #2: random.patch --]
[-- Type: text/plain, Size: 1808 bytes --]

diff --git a/drivers/char/random.c b/drivers/char/random.c
index 5fee056..6eccfc9 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -550,8 +550,8 @@ static void credit_entropy_store(struct entropy_store *r, int nbits)
 
 /* There is one of these per entropy source */
 struct timer_rand_state {
-	cycles_t last_time;
-	long last_delta,last_delta2;
+	u32      last_word;
+	int last_delta,last_delta2;
 	unsigned dont_count_entropy:1;
 };
 
@@ -570,12 +570,17 @@ static struct timer_rand_state *irq_timer_state[NR_IRQS];
  */
 static void add_timer_randomness(struct timer_rand_state *state, unsigned num)
 {
-	struct {
-		cycles_t cycles;
-		long jiffies;
-		unsigned num;
+	union {
+		struct {
+			cycles_t cycles;
+			long jiffies;
+			unsigned num;
+		};
+		u32 words[1];
 	} sample;
-	long delta, delta2, delta3;
+	u32 word;
+	unsigned int ui;
+	int delta, delta2, delta3;
 
 	preempt_disable();
 	/* if over the trickle threshold, use only 1 in 4096 samples */
@@ -586,7 +591,12 @@ static void add_timer_randomness(struct timer_rand_state *state, unsigned num)
 	sample.jiffies = jiffies;
 	sample.cycles = get_cycles();
 	sample.num = num;
-	add_entropy_words(&input_pool, (u32 *)&sample, sizeof(sample)/4);
+
+	word = sample.words[0];
+	for (ui = 1; ui < sizeof(sample)/4; ui++)
+		word += sample.words[ui];
+
+	add_entropy_words(&input_pool, &word, 1);
 
 	/*
 	 * Calculate number of bits of randomness we probably added.
@@ -595,8 +605,8 @@ static void add_timer_randomness(struct timer_rand_state *state, unsigned num)
 	 */
 
 	if (!state->dont_count_entropy) {
-		delta = sample.jiffies - state->last_time;
-		state->last_time = sample.jiffies;
+		delta = word - state->last_word;
+		state->last_word = word;
 
 		delta2 = delta - state->last_delta;
 		state->last_delta = delta;

  reply	other threads:[~2007-12-04 18:18 UTC|newest]

Thread overview: 77+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-12-04 11:41 Why does reading from /dev/urandom deplete entropy so much? Marc Haber
2007-12-04 14:16 ` Eric Dumazet
2007-12-04 16:18 ` Adrian Bunk
2007-12-04 16:47   ` Alan Cox
2007-12-04 18:17     ` Eric Dumazet [this message]
2007-12-05 21:26       ` Matt Mackall
2007-12-06  7:02         ` Eric Dumazet
2007-12-06 16:09           ` Matt Mackall
2007-12-09 12:42         ` Marc Haber
2007-12-09 16:16           ` Matt Mackall
2007-12-10 23:06             ` Marc Haber
2007-12-10 23:35               ` Matt Mackall
2007-12-11  1:34                 ` Theodore Tso
2007-12-11 19:46                   ` Phillip Susi
2007-12-11 20:02                     ` Ray Lee
2007-12-12  5:34                     ` David Schwartz
2007-12-04 16:54   ` Ray Lee
2007-12-04 16:55     ` Alan Cox
2007-12-04 18:02       ` Matt Mackall
2007-12-04 19:50         ` Theodore Tso
2007-12-04 20:36           ` Matt Mackall
2007-12-04 20:40           ` Alan Cox
2007-12-04 20:48             ` Mike McGrath
2007-12-04 21:54               ` Matt Mackall
2007-12-04 22:03               ` Theodore Tso
2007-12-04 22:12                 ` Mike McGrath
2007-12-04 22:28                   ` Matt Mackall
2007-12-04 21:08             ` Matt Mackall
2007-12-04 21:18               ` Mike McGrath
2007-12-04 22:15                 ` Matt Mackall
2007-12-04 22:23                   ` Mike McGrath
2007-12-04 22:33                     ` Matt Mackall
2007-12-05 14:26                       ` Mike McGrath
2007-12-05 14:49                         ` Theodore Tso
2007-12-08  7:38                           ` Jon Masters
2007-12-08 17:32                             ` Theodore Tso
2007-12-08 17:33                               ` Mike McGrath
2007-12-08 17:49                                 ` Theodore Tso
2007-12-08 17:54                                   ` Jon Masters
2007-12-08 18:15                                   ` Matt Mackall
2007-12-08 18:24                                     ` Theodore Tso
2007-12-08 19:36                                     ` entropy gathering (was Re: Why does reading from /dev/urandom deplete entropy so much?) Jeff Garzik
2007-12-08 19:53                                       ` Matt Mackall
2007-12-08 20:04                                         ` Jeff Garzik
2007-12-08 20:19                                           ` Matt Mackall
2007-12-08 21:07                                             ` Willy Tarreau
2007-12-08 20:31                                           ` Theodore Tso
2007-12-08 20:47                                             ` Jeff Garzik
2007-12-08 20:42                                       ` Willy Tarreau
2007-12-08 23:47                                         ` Theodore Tso
2007-12-09  1:07                                           ` Jon Masters
2007-12-08 18:31                                   ` Why does reading from /dev/urandom deplete entropy so much? Jeff Garzik
2007-12-08 20:26                                     ` David Schwartz
2007-12-08 17:43                               ` Matt Mackall
2007-12-08 17:47                                 ` Jon Masters
2007-12-08 18:05                                 ` Theodore Tso
2007-12-08 17:45                               ` Jon Masters
2007-12-10 16:37                           ` Pavel Machek
2007-12-04 18:01     ` Matt Mackall
2007-12-06 20:08       ` Bill Davidsen
2007-12-05 12:23     ` Marc Haber
2007-12-05 12:29   ` Marc Haber
2007-12-05 13:33     ` Theodore Tso
2007-12-05 15:10       ` Marc Haber
2007-12-06 19:32   ` Bill Davidsen
2007-12-08 22:03     ` Adrian Bunk
2007-12-08 22:10       ` Ismail Dönmez
2007-12-08 23:46         ` Theodore Tso
2007-12-09  5:21           ` Willy Tarreau
2007-12-09  6:52             ` Jon Masters
2007-12-09  6:21           ` Ismail Dönmez
2007-12-09 12:31             ` Theodore Tso
2007-12-09 14:06               ` Ismail Dönmez
2007-12-11 15:42       ` Bill Davidsen
2007-12-20 22:27         ` Marc Haber
2007-12-26 18:27           ` Phillip Susi
2007-12-04 18:49 ` Russ Dill

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=475599D6.4030008@cosmosbay.com \
    --to=dada1@cosmosbay.com \
    --cc=alan@lxorguk.ukuu.org.uk \
    --cc=bunk@kernel.org \
    --cc=davem@davemloft.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mh+linux-kernel@zugschlus.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.