Skip to contentSkip to conversation

How to use /dev/random

DISCUSSION HIToC 4 participants 6 messages Sep 22, 2005 – Sep 23, 2005

lore
Share & tools
lore

HIToC

author
#0 lore raw

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

Signature · 5 lines
With regards,


					HIToC
					[email protected]
#1 lore raw
Quoted 3 lines · * 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! ;-)

Quoted 3 lines

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 ...

Quoted 1 line

HIToC

HGN

Signature · 2 lines
A computer is like an Old Testament god, with a lot of rules  and
no mercy. - Joseph Campbell
#2 lore raw
In reply to HIToC
Quoted 3 lines · HIToC

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.

Quoted 3 lines

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 <[email protected]>
Software Consultancy {C/C++ && Java && .NET}
Mobile: +49 (176) 21248869
Office: +49 (9131) 7126409

author
#3 lore raw
Quoted 6 lines · Steve Graegert

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

Signature · 5 lines
With regards,


					HIToC
					[email protected]
#4 lore raw
Quoted 15 lines · HIToC
Quoted 6 lines

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.

Quoted 4 lines

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.

Quoted 2 lines

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.

Signature · 1 line
Glynn Clements <[email protected]>
author
#5 lore raw
Quoted 26 lines · Glynn Clements
Quoted 15 lines · HIToC
Quoted 6 lines

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.

Quoted 4 lines

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.

Quoted 2 lines

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

Quoted 7 lines

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!!

Signature · 5 lines
With regards,


					HIToC
					[email protected]