linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* How to use /dev/random
@ 2005-09-22  9:11 HIToC
  2005-09-22  9:42 ` Hagen Paul Pfeifer
  2005-09-22  9:53 ` Steve Graegert
  0 siblings, 2 replies; 6+ messages in thread
From: HIToC @ 2005-09-22  9:11 UTC (permalink / raw)
  To: linux-c-programming

Hello list!
	Is there anyone who can explain how to use /dev/random in
order to obtain random numbers?
I don't know if this is the best manner whitin the Linux Kernel to
generate random numbers, but if someone has better ideas, they
will be greatly appreciated.
Thanks.

HIToC

-- 
With regards,


					HIToC
					hitoc_mail@yahoo.it

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to use /dev/random
  2005-09-22  9:11 How to use /dev/random HIToC
@ 2005-09-22  9:42 ` Hagen Paul Pfeifer
  2005-09-22  9:53 ` Steve Graegert
  1 sibling, 0 replies; 6+ messages in thread
From: Hagen Paul Pfeifer @ 2005-09-22  9:42 UTC (permalink / raw)
  To: linux-c-programming

* HIToC | 2005-09-22 11:11:07 [+0200]:

>Hello list!
>	Is there anyone who can explain how to use /dev/random in
>order to obtain random numbers?

One of the best source out there is gnupg - especially
cipher/rndlinux.c.

There you will find everything you wan't! ;-)

>I don't know if this is the best manner whitin the Linux Kernel to
>generate random numbers, but if someone has better ideas, they
>will be greatly appreciated.

It is, except you own a cryptographic rnd device ...

>HIToC

HGN

-- 
A computer is like an Old Testament god, with a lot of rules  and
no mercy. - Joseph Campbell

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to use /dev/random
  2005-09-22  9:11 How to use /dev/random HIToC
  2005-09-22  9:42 ` Hagen Paul Pfeifer
@ 2005-09-22  9:53 ` Steve Graegert
  2005-09-23  9:16   ` HIToC
  1 sibling, 1 reply; 6+ messages in thread
From: Steve Graegert @ 2005-09-22  9:53 UTC (permalink / raw)
  To: HIToC; +Cc: linux-c-programming

On 9/22/05, HIToC <hitoc_mail@yahoo.it> wrote:
> Hello list!
>         Is there anyone who can explain how to use /dev/random in
> order to obtain random numbers?

There are two types:

1. /dev/random generates high quality entropy.  It's relatively slow
since it uses other information like mouse clicks, key strokes and
interrupt times to produce random numbers.  For this reason, some
applications can block for some time when reading from /dev/random.

2. /dev/urandom is much faster with lower quality sequences of random
numbers but it should be sufficient for most applications.

> I don't know if this is the best manner whitin the Linux Kernel to
> generate random numbers, but if someone has better ideas, they
> will be greatly appreciated.

You can read from /dev/(u)random using the read system call:

        int randnum, fd;

        if ((fd  = open ("/dev/urandom", O_RDONLY)) != -1) {
            read(fd, &randnum, 4 );
            /* call abs() to produces positives */
            printf("random number read : %d\n",randnum);
            close(fd);
         }

You can also read a number of random bytes from /dev/(u)random with dd:

        dd if=/dev/urandom of=/tmp/randnums bs=1 count=500

This reads a sequence of 500 bytes from /dev/urandom and writes them
to /tmp/randnums.

Regards

	\Steve

--

Steve Graegert <graegerts@gmail.com>
Software Consultancy {C/C++ && Java && .NET}
Mobile: +49 (176)  21248869
Office: +49 (9131) 7126409

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to use /dev/random
  2005-09-22  9:53 ` Steve Graegert
@ 2005-09-23  9:16   ` HIToC
  2005-09-23 15:44     ` Glynn Clements
  0 siblings, 1 reply; 6+ messages in thread
From: HIToC @ 2005-09-23  9:16 UTC (permalink / raw)
  To: Steve Graegert; +Cc: linux-c-programming

On Thursday 22 September 2005 11:53, Steve Graegert wrote:
> You can also read a number of random bytes from /dev/(u)random with dd:
>
>         dd if=/dev/urandom of=/tmp/randnums bs=1 count=500
>
> This reads a sequence of 500 bytes from /dev/urandom and writes them
> to /tmp/randnums.

I have read that the problem of /dev/random is mainly on boot, when there
are few iteration from the user. For this reason is useful first save on a
temporary_random file a 512 bytes block on shutdown of the system.
Then on the new booting, to put the 512 bytes saved on the
temporary_random file in the /dev/random device.
With this procedure we have the random device ready
afterwards boot.

I have two questions:
1) the size of /dev/random is 512 bytes, or the number of bytes
	increase over the new iteration of the user (like mouse clicks,
	key strokes, ecc..)?
2) Where can I put the scripts to read the random number on shutdown
	or to write the previously saved bytes, on boot?


Thanks for every suggestion.
HIToC

-- 
With regards,


					HIToC
					hitoc_mail@yahoo.it

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to use /dev/random
  2005-09-23  9:16   ` HIToC
@ 2005-09-23 15:44     ` Glynn Clements
  2005-09-23 16:31       ` HIToC
  0 siblings, 1 reply; 6+ messages in thread
From: Glynn Clements @ 2005-09-23 15:44 UTC (permalink / raw)
  To: HIToC; +Cc: Steve Graegert, linux-c-programming


HIToC wrote:

> > You can also read a number of random bytes from /dev/(u)random with dd:
> >
> >         dd if=/dev/urandom of=/tmp/randnums bs=1 count=500
> >
> > This reads a sequence of 500 bytes from /dev/urandom and writes them
> > to /tmp/randnums.
> 
> I have read that the problem of /dev/random is mainly on boot, when there
> are few iteration from the user. For this reason is useful first save on a
> temporary_random file a 512 bytes block on shutdown of the system.
> Then on the new booting, to put the 512 bytes saved on the
> temporary_random file in the /dev/random device.
> With this procedure we have the random device ready
> afterwards boot.
> 
> I have two questions:
> 1) the size of /dev/random is 512 bytes, or the number of bytes
> 	increase over the new iteration of the user (like mouse clicks,
> 	key strokes, ecc..)?

The size of the entropy pool is fixed at 512 bytes.

> 2) Where can I put the scripts to read the random number on shutdown
> 	or to write the previously saved bytes, on boot?

Typically such scripts live in /etc/init.d. The format of the script
needs to be compatible with your OS distribution. Look at existing
scripts for clues. Most distributions already contain a suitable
script (e.g. on Gentoo, it's /etc/init.d/urandom).

There are a number of comments regarding saving and restoring entropy
in drivers/char/random.c in the Linux kernel source code.

-- 
Glynn Clements <glynn@gclements.plus.com>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to use /dev/random
  2005-09-23 15:44     ` Glynn Clements
@ 2005-09-23 16:31       ` HIToC
  0 siblings, 0 replies; 6+ messages in thread
From: HIToC @ 2005-09-23 16:31 UTC (permalink / raw)
  To: Glynn Clements; +Cc: Steve Graegert, linux-c-programming

On Friday 23 September 2005 17:44, Glynn Clements wrote:
> HIToC wrote:
> > > You can also read a number of random bytes from /dev/(u)random with dd:
> > >
> > >         dd if=/dev/urandom of=/tmp/randnums bs=1 count=500
> > >
> > > This reads a sequence of 500 bytes from /dev/urandom and writes them
> > > to /tmp/randnums.
> >
> > I have read that the problem of /dev/random is mainly on boot, when there
> > are few iteration from the user. For this reason is useful first save on
> > a temporary_random file a 512 bytes block on shutdown of the system. Then
> > on the new booting, to put the 512 bytes saved on the
> > temporary_random file in the /dev/random device.
> > With this procedure we have the random device ready
> > afterwards boot.
> >
> > I have two questions:
> > 1) the size of /dev/random is 512 bytes, or the number of bytes
> > 	increase over the new iteration of the user (like mouse clicks,
> > 	key strokes, ecc..)?
>
> The size of the entropy pool is fixed at 512 bytes.
>
> > 2) Where can I put the scripts to read the random number on shutdown
> > 	or to write the previously saved bytes, on boot?
>
> Typically such scripts live in /etc/init.d. The format of the script
> needs to be compatible with your OS distribution. Look at existing
> scripts for clues. Most distributions already contain a suitable
> script (e.g. on Gentoo, it's /etc/init.d/urandom).
>
> There are a number of comments regarding saving and restoring entropy
> in drivers/char/random.c in the Linux kernel source code.

Thanks all for your useful help!!
-- 
With regards,


					HIToC
					hitoc_mail@yahoo.it

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2005-09-23 16:31 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-09-22  9:11 How to use /dev/random HIToC
2005-09-22  9:42 ` Hagen Paul Pfeifer
2005-09-22  9:53 ` Steve Graegert
2005-09-23  9:16   ` HIToC
2005-09-23 15:44     ` Glynn Clements
2005-09-23 16:31       ` HIToC

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).