From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 3768 invoked by alias); 21 Feb 2005 12:03:43 -0000 Mailing-List: contact gsl-discuss-help@sources.redhat.com; run by ezmlm Precedence: bulk List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gsl-discuss-owner@sources.redhat.com Received: (qmail 3736 invoked from network); 21 Feb 2005 12:03:34 -0000 Received: from unknown (HELO outmail128153.authsmtp.com) (62.13.128.153) by sourceware.org with SMTP; 21 Feb 2005 12:03:34 -0000 Received: from squirrel.dmpriest.net.uk (secure.authsmtp.com [62.13.128.25]) by punt-mx0.dmpriest.net.uk (8.12.11/8.12.11/Kp) with ESMTP id j1LC3YsS063693 for ; Mon, 21 Feb 2005 12:03:34 GMT Received: from nan.dnsalias.org ([194.117.6.181]) (authenticated bits=0) by squirrel.dmpriest.net.uk (8.13.1/8.13.1/Kp) with ESMTP id j1LC3XQY080815 for ; Mon, 21 Feb 2005 12:03:34 GMT (envelope-from jgmbenoit@wanadoo.fr) Received: from localhost ([127.0.0.1]) by nan.dnsalias.org with esmtp (Exim 4.34) id 1D3CHk-00020o-NU for gsl-discuss@sources.redhat.com; Mon, 21 Feb 2005 12:03:33 +0000 Message-ID: <4219CE14.5050901@wanadoo.fr> Date: Mon, 21 Feb 2005 12:03:00 -0000 From: Jerome BENOIT Reply-To: jgmbenoit@wanadoo.fr Organization: none User-Agent: Debian Thunderbird 1.0 (X11/20050116) MIME-Version: 1.0 CC: gsl-discuss@sources.redhat.com Subject: Re: Random Number Seed References: <4219CC24.90200@physik.uni-bielefeld.de> In-Reply-To: <4219CC24.90200@physik.uni-bielefeld.de> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Score: -2.8 (--) X-Spam-Report: Spam detection software, running on the system "nan.dnsalias.org", has identified this incoming email as possible spam. The original message has been attached to this so you can view it (if it isn't spam) or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: Hello, Olaf Lenz wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Hello! > > A few months ago, you suggested the following code snippet for seeding > the RNG from /dev/random: > > - > #include > #include > > unsigned long int random_seed() > { > > ~ unsigned int seed; > ~ struct timeval tv; > ~ FILE *devrandom; > > ~ if ((devrandom = fopen("/dev/random","r")) == NULL) { > ~ gettimeofday(&tv,0); > ~ seed = tv.tv_sec + tv.tv_usec; > ~ if(verbose == D_SEED) printf("Got seed %u from gettimeofday()\n",seed); > ~ } else { > ~ fread(&seed,sizeof(seed),1,devrandom); > ~ if(verbose == D_SEED) printf("Got seed %u from /dev/random\n",seed); > ~ fclose(devrandom); > ~ } > > ~ return(seed); > } > - > > I've used the code for quite a while now and only today I noticed a big > problem with it. The code tests, if /dev/random can be opened, but it > does NOT test if the fread has actually read any number. [...] Content analysis details: (-2.8 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -2.8 ALL_TRUSTED Did not pass through any untrusted hosts X-Authentic-SMTP: 61633132333330.squirrel.dmpriest.net.uk:Kp X-Powered-By: AuthSMTP - http://www.authsmtp.com - Authenticated SMTP Mail Relay X-Report-SPAM: If SPAM / abuse - report it at: http://www.authsmtp.com/abuse X-Virus-Status: No virus detected - but ensure you scan with your own anti-virus system! X-SW-Source: 2005-q1/txt/msg00091.txt.bz2 Hello, Olaf Lenz wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Hello! > > A few months ago, you suggested the following code snippet for seeding > the RNG from /dev/random: > > - ------------------------------------------------- > #include > #include > > unsigned long int random_seed() > { > > ~ unsigned int seed; > ~ struct timeval tv; > ~ FILE *devrandom; > > ~ if ((devrandom = fopen("/dev/random","r")) == NULL) { > ~ gettimeofday(&tv,0); > ~ seed = tv.tv_sec + tv.tv_usec; > ~ if(verbose == D_SEED) printf("Got seed %u from gettimeofday()\n",seed); > ~ } else { > ~ fread(&seed,sizeof(seed),1,devrandom); > ~ if(verbose == D_SEED) printf("Got seed %u from /dev/random\n",seed); > ~ fclose(devrandom); > ~ } > > ~ return(seed); > } > - ------------------------------------------------- > > I've used the code for quite a while now and only today I noticed a big > problem with it. The code tests, if /dev/random can be opened, but it > does NOT test if the fread has actually read any number. On recent kernel (2.4.x,2.6.x), you can easily know if there are available random numbers by reading /proc/sys/kernel/random/entropy_avail (see the Documentation distributed with the kernel source). Second, you can read /dev/urandom Third, if you have an apropriate hard ware, you can use the rng-tools to feed with true random number your /dev/random. hth, Jerome > > In my case, this resulted in the fact that the seed was not seeded at > all and all processes used the same seed.... P-( > > So to all who have been using the code, I would recommend to check their > results. For the future, I would recommend to use the following code: > > - ------------------------------------------------- > #include > #include > > unsigned long int random_seed() > { > > ~ unsigned int seed; > ~ struct timeval tv; > ~ FILE *devrandom; > > ~ if ((devrandom = fopen("/dev/random","r")) == NULL) { > ~ gettimeofday(&tv,0); > ~ seed = tv.tv_sec + tv.tv_usec; > ~ if(verbose == D_SEED) printf("Got seed %u from gettimeofday()\n",seed); > ~ } else { > ~ if (fread(&seed,sizeof(seed),1,devrandom) == 1) { > ~ if(verbose == D_SEED) printf("Got seed %u from /dev/random\n",seed); > ~ fclose(devrandom); > ~ } else { > ~ gettimeofday(&tv,0); > ~ seed = tv.tv_sec + tv.tv_usec; > ~ if(verbose == D_SEED) printf("Got seed %u from > gettimeofday()\n",seed); > > ~ } > ~ } > > ~ return(seed); > > } > - ------------------------------------------------- > > Cheers > Olaf > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.2.4 (GNU/Linux) > Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org > > iD8DBQFCGcwjtQ3riQ3oo/oRAsjeAKC3CIz3kxxt/ZJUiuYzemIU1IqVdgCffoYW > vXr8SEcXH69ulMzTfBwWuHw= > =2RKb > -----END PGP SIGNATURE----- > -- Dr. Jerome BENOIT room A2-26 Complexo Interdisciplinar da U. L. Av. Prof. Gama Pinto, 2 P-1649-003 Lisboa, Portugal email: jgmbenoit@wanadoo.fr or benoit@cii.fc.ul.pt -- If you are convinced by the necessity of a European research initiative, please visit http://fer.apinc.org