Dev Urandom C
- I'm looking for ways to use /dev/random (or /dev/urandom) from the command line. In particular, I'd like to know how to use such a stream as stdin to write streams of random numbers to stdout (one.
- Oct 02, 2019 Contribute to torvalds/linux development by creating an account on GitHub. Skip to content. Torvalds / linux. mknod /dev/random c 1 8. mknod /dev/urandom c 1 9. Acknowledgements:. Ideas for constructing this random number generator were derived.
- FreeBSD and OS X do away with the distinction between urandom and /dev/random; the two devices behave identically. Unfortunately, the man page does a poor job of explaining why this is, and perpetuates the myth that Linux urandom is scary. FreeBSD’s kernel crypto RNG doesn’t block regardless of whether you use /dev/random or urandom.
Is there an alternative to /dev/urandom? Ask Question Asked 10 years, 10 months ago. Active 5 years ago. Viewed 45k times 21. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.
Introduction
srandom is a Linux kernel module that can be used to replace the built-in /dev/urandom & /dev/random device files. It is secure and VERY fast. My tests show it over 150x faster then /dev/urandom. It should compile and install on any Linux 3.10+ kernel. It passes all the randomness tests using the dieharder tests.
srandom was created as an improvement to the built-in random number generators. I wanted a much faster random number generator to wipe ssd disks. Through many hours of testing and trial-and-error, I came up with an algorithm that is many times faster than urandom, but still produces excellent random numbers. You can wipe multiple SSDs at the same time. The built-in generators (/dev/random and /dev/urandom) are technically not flawed. /dev/random (the true random number generator) is BLOCKED most of the time waiting for more entropy. If you are running your Linux in a VM, /dev/random is basically unusable. /dev/urandom is unblocked, but still very slow.
Dev/urandom Cryptographically Secure
What is the most important part of random numbers? Unpredictability!
srandom includes all these features to make it's generator produce the most unpredictable/random numbers.
- It uses two separate and different 64bit PRNGs.
- There are two different algorithms to XOR the the 64bit PSRNGs together.
- srandom seeds and re-seeds the three separate seeds using nano timer.
- The module seeds the PSRNGs twice on module init.
- It uses 16 x 512byte buffers and outputs them randomly.
- There is a separate kernel thread that constantly updates the buffers and seeds.
- srandom throws away a small amount of data.
The best part of srandom is it's efficiency and very high speed.. I tested many PSRNGs and found two that worked very fast and had a good distribution of numbers. Two or three 64bit numbers are XORed. The results is unpredictable and very high speed generation of numbers.
Why do I need this?
The best use-case is disk wiping. However you could use srandom to provide your applications with the fastest and unpredictable source of random numbers. Why would you want to block your applications while waiting for random numbers? Run 'lsof grep random', just to see how many applications have the random number device open.. Any security type applications rely heavily on random numbers. For example, Apache SSL (Secure Socket Level), PGP (Pretty Good Privacy), VPN (Virtual Private Networks). All types of Encryption, Password seeds, Tokens would rely on a source of random number. There is many examples at https://www.random.org/testimonials/.
Compile and installation
To build & compile the kernel module. A pre-req is 'kernel-devel'. Use yum or apt to install.
To load the kernel module into the running kernel (temporary).
To unload the kernel module from the running kernel.
To install the kernel module on your system (persistent on reboot).
To uninstall the kernel module from your system.
Usage
You can load the kernel module temporary, or you can install the kernel module to be persistent on reboot.
- If you want to just test the kernel module, you should run 'make load'. This will load the kernel module into the running kernel and create a /dev/srandom accessible to root only. It can be removed with 'make unload'. You can monitor the load process in /var/log/messages.
- When you run 'make install', the srandom kernel module is moved to /usr/lib/modules/../kernel/drivers/. If you run 'make load' or reboot, the kernel module will be loaded into the running kernel, but now will replace the /dev/urandom device file. The old /dev/urandom device is renamed (keeping it's inode number). This allows any running process that had /dev/urandom to continue running without issues. All new requests for /dev/urandom will use the srandom kernel module.
- Once the kernel module is loaded, you can access the module information through the /proc filesystem. For example:
- Use the /usr/bin/srandom tool to set srandom as the system PRNG, set the system back to default PRNG, or get the status.
- To completely remove the srandom module, use 'make uninstall'. Depending if there is processes accessing /dev/srandom, you may not be able to remove the module from the running kernel. Try 'make unload', if the module is busy, then a reboot is required.
Testing & performance
A simple dd command to read from the /dev/srandom device will show performance of the generator. The results below are typical from my system. Of course, your performance will vary.
The 'Improved' srandom number generator
The 'Non-Blocking' urandom number generator
/dev/urandom Command Not Found
The 'Blocking' random number generator. ( I pressed [CTRL-C] after 5 minutes and got 35 bytes! If you really NEED to test this, You might need to leave this running for days..)
Testing randomness
The most important part of the random number device file is that is produces random/unpredictable numbers. The golden standard of testing randomness is the dieharder test suite (http://www.phy.duke.edu/~rgb/General/dieharder.php). The dieharder tool with easily detect flawed random number generators. After you install dieharder, use the following command to put /dev/srandom through the battery of tests.
Just a note about some tests assessments that can randomly show as 'WEAK'.. If the test is repeatedly 'FAILED' or 'WEAK',then that is a problem. So, please retest a few times to verify if it passes.
How to configure your apps
If you installed the kernel module to load on reboot, then you do not need to modify any applications to use the srandom kernel module. It will be linked to /dev/urandom, so all applications will use it automatically. However, if you do not want to link /dev/srandom to /dev/urandom, then you can configure your applications to use whichever device you want. Here is a few examples..
Java: Use the following command line argument to tell Java to use the new random device
Java: To make the setting as default, add the following line to the configuration file. ($JAVA_HOME/jre/lib/security/java.security)
https: (Apache SSL), Configure /etc/httpd/conf.d/ssl.conf
Postfix: Change the following line in /etc/postfix/main.cf
PHP: Change the following line in PHP config file.
OpenLDAP: Change the following line in /etc/openldap/slapd.conf
Using /dev/srandom to securely wipe SSD disks.
Dev Urandom C++
*** This will DESTROY DATA! Use with caution! ***
*** Replace /dev/sdXX with your disk device you want to wipe. Nexus 2 vst crack download.
License
Copyright (C) 2019 Jonathan Senkerik
This program is free software: you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation, either version 3 of the License, or(at your option) any later version.
This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.
You should have received a copy of the GNU General Public Licensealong with this program. If not, see http://www.gnu.org/licenses/.
Hi everyone and good morning :) .
i want to simply read from the linux file 'urandom' a random number and print it out on screen.just simple as that.
this function should be in a file called myrandom.c ( myrandom.h must be made also, which has my function dekleration ) . i will have to compile it later to a static libray and to dynamic library, and then write a main program which prints out 5 random numbers using the static library .
and another main program which prints out 5 random numbers using the dynamic library.
i wrote the myrandom.c code and myrandom.h code i dont know if they are correct .
i tested myrandom.c inside a main program and it gives me random number , but its very big .
ist that the right way to grap a random number from the urandom file?
many thanks
Cat Dev Urandom
- 5 Contributors
- forum 10 Replies
- 1,190 Views
- 1 Day Discussion Span
- commentLatest Postby zxzLatest Post
Recommended Answers
You're also using printf
incorrectly. When you are printing out the value of an int, printf does not want the address of that int. It wants the int itself.
printf('%d', &rNumber);
is wrong.printf('%d', rNumber);
is right.
WTF is going on with this?
It's still wrong. str
is effectively an int*
, so &str
is an int**
, so *(&str)
is an int*
. So you're doing exactly what you were doing before (wrongly providing an int*
when you …
All 10 Replies
sepp2k378
You need to #include <stdio.h>
to get the declaration of fopen
. As it is now you're implicitly declaring fopen
in a way that is incompatible with its definition. You should be getting a warning about that (and in C99 and later you should instead be getting an error that you're using a function without a declaration).
Once you do that, you should be getting another warning that you're implicitly converting the result of fopen
from FILE*
to int
. That's because fopen
doesn't read anything from the file nor does it return an int. fopen
opens the file and then returns a file pointer. That file pointer can then be given to other file IO functions (like fread
, fwrite
, fgets
, fscanf
, fprintf
etc.), which will actually read from or write to the file. Once you're done working with the file, you need to close it by passing the file pointer to fclose
.
So no, you're not currently doing it correctly. The number you're printing is simply the numeric value of whichever pointer was returned by fopen
. It has nothing to do with the contents of the file.