@kasperd The question asks (in the title) how to read N random characters from /dev/urandom but in the body it asks for reading bytes. I agree there is an inconsistency. I came to this question from Google for reading characters and I think that it may help the future visitors as an option. – Umur Kontacı Jan 4. Anyway, you could use something like dd if=/dev/zero of=/dev/sdb, but obviously that isn't going to be random, it will just erase much faster. Another option might be to use this method /sbin/badblocks -c 10240 -s -w -t random -v /dev/sdb it is faster then urandom, but the badblocks PRNG is less random.
|Special device files|
In Unix-likeoperating systems, /dev/random, /dev/urandom and /dev/arandom are special files that serve as pseudorandom number generators. They allow access to environmental noise collected from device drivers and other sources./dev/random typically blocks if there is less entropy available than requested; /dev/urandom typically never blocks, even if the pseudorandom number generator seed was not fully initialized with entropy since boot. /dev/arandom blocks after boot until the seed has been securely initialized with enough entropy, and then never blocks again. Not all operating systems implement the same methods for /dev/random and /dev/urandom and only a few provide /dev/arandom.
This shell script is a random printable character generator, slightly biased:
Random number generation in kernel space was implemented for the first time for Linux in 1994 by Theodore Ts'o.The implementation used secure hashes rather than ciphers,[clarification needed] to avoid cryptography export restrictions that were in place when the generator was originally designed. The implementation was also designed with the assumption that any given hash or cipher might eventually be found to be weak, and so the design is durable in the face of any such weaknesses. Fast recovery from pool compromise is not considered a requirement, because the requirements for pool compromise are sufficient for much easier and more direct attacks on unrelated parts of the operating system.
In Ts'o's implementation, the generator keeps an estimate of the number of bits of noise in the entropy pool. From this entropy pool random numbers are created. When read, the /dev/random device will only return random bytes within the estimated number of bits of noise in the entropy pool. When the entropy pool is empty, reads from /dev/random will block until additional environmental noise is gathered. The intent is to serve as a cryptographically secure pseudorandom number generator, delivering output with entropy as large as possible. This is suggested by the authors for use in generating cryptographic keys for high-value or long-term protection.
A counterpart to /dev/random is /dev/urandom ('unlimited'/non-blocking random source) which reuses the internal pool to produce more pseudo-random bits. This means that the call will not block, but the output may contain less entropy than the corresponding read from /dev/random. While /dev/urandom is still intended as a pseudorandom number generator suitable for most cryptographic purposes, the authors of the corresponding man page note that, theoretically, there may exist an as-yet-unpublished attack on the algorithm used by /dev/urandom, and that users concerned about such an attack should use /dev/random instead. However such an attack is unlikely to come into existence, because once the entropy pool is unpredictable it doesn't leak security by a reduced number of bits.
It is also possible to write to /dev/random. This allows any user to mix random data into the pool. Non-random data is harmless, because only a privileged user can issue the ioctl needed to increase the entropy estimate. The current amount of entropy and the size of the Linux kernel entropy pool, both measured in bits, are available in /proc/sys/kernel/random/ and can be displayed by the command cat /proc/sys/kernel/random/entropy_avail and cat /proc/sys/kernel/random/poolsize respectively.
Gutterman, Pinkas, & Reinman in March 2006 published a detailed cryptographic analysis of the Linux random number generator in which they describe several weaknesses. Perhaps the most severe issue they report is with embedded or Live CD systems, such as routers and diskless clients, for which the bootup state is predictable and the available supply of entropy from the environment may be limited. For a system with non-volatile memory, they recommend saving some state from the RNG at shutdown so that it can be included in the RNG state on the next reboot. In the case of a router for which network traffic represents the primary available source of entropy, they note that saving state across reboots 'would require potential attackers to either eavesdrop on all network traffic' from when the router is first put into service, or obtain direct access to the router's internal state. This issue, they note, is particularly critical in the case of a wireless router whose network traffic can be captured from a distance, and which may be using the RNG to generate keys for data encryption.
The Linux kernel provides support for several hardware random number generators, should they be installed. The raw output of such a device may be obtained from /dev/hwrng.
With Linux kernel 3.16 and newer, the kernel itself mixes data from hardware random number generators into /dev/random on a sliding scale based on the definable entropy estimation quality of the HWRNG. This means that no userspace daemon, such as rngd from rng-tools, is needed to do that job. With Linux kernel 3.17+, the VirtIO RNG was modified to have a default quality defined above 0, and as such, is currently the only HWRNG mixed into /dev/random by default.
The entropy pool can be improved by programs like timer_entropyd, haveged, randomsound etc. With rng-tools, hardware random number generators like Entropy Key, etc. can write to /dev/random. The diehard tests programs dieharder, diehard and ent can test these random number generators.
In January 2014, Daniel J. Bernstein published a critique of how Linux mixes different sources of entropy. He outlines an attack in which one source of entropy capable of monitoring the other sources of entropy could modify its output to nullify the randomness of the other sources of entropy. Consider the function H(x,y,z) where H is a hash function and x, y, and z are sources of entropy with z being the output of a CPU based malicious HRNG Z:
Bernstein estimated that an attacker would need to repeat H(x,y,r) 16 times to compromise DSA and ECDSA. This is possible because Linux reseeds H on an ongoing basis instead of using a single high quality seed.
In October 2016, with the release of Linux kernel version 4.8, the kernel's /dev/urandom was switched over to a ChaCha20-based cryptographic pseudorandom number generator (CPRNG) implementation by Theodore Ts'o, based on Bernstein's well-regarded stream cipherChaCha20.
In 2020, the Linux kernel version 5.6 /dev/random implementation was also switched to the ChaCha20-based CPRNG, which blocks only when the CPRNG hasn't initialized. Once initialized, /dev/random and /dev/urandom behave the same.
The FreeBSD operating system provides /dev/urandom for compatibility but the behavior is very different from that of Linux. On FreeBSD, /dev/urandom is just a link to /dev/random and blocks only until properly seeded. FreeBSD's PRNG (Fortuna) reseeds regularly but does not attempt to estimate entropy. On a system with small amount of network and disk activity, reseeding is done after a fraction of a second.
While entropy pool–based methods are completely secure if implemented correctly, if they overestimate their entropy they may become less secure than well-seeded PRNGs. In some cases an attacker may have a considerable amount of control over the entropy; for example, a diskless server may get almost all of it from the network, rendering it potentially vulnerable to man-in-the-middle attacks.
Since OpenBSD 5.1 (May 1, 2012) /dev/random and /dev/arandom use an algorithm based on RC4 but renamed, because of intellectual property reasons, ARC4. While random number generation here uses system entropy gathered in several ways, the ARC4 algorithm provides a fail-safe, ensuring that a rapid and high quality pseudo-random number stream is provided even when the pool is in a low entropy state. The system automatically uses hardware random number generators (such as those provided on some Intel PCI hubs) if they are available, through the OpenBSD Cryptographic Framework.
As of OpenBSD 5.5 (May 1, 2014), the
arc4random() call used for OpenBSD's random devices no longer uses ARC4, but ChaCha20 (arc4random name might be reconsidered as A Replacement Call for Random).NetBSD's implementation of the legacy
arc4random() API has also been switched over to ChaCha20 as well.Traktor scratch pro 3 free download.
macOS uses 160-bit Yarrow based on SHA-1. There is no difference between /dev/random and /dev/urandom; both behave identically.Apple's iOS also uses Yarrow.
/dev/random and /dev/urandom are also available on Solaris,NetBSD,Tru64 UNIX 5.1B, AIX 5.2 and HP-UX 11i v2. As with FreeBSD, AIX implements its own Yarrow-based design, however AIX uses considerably fewer entropy sources than the standard /dev/random implementation and stops refilling the pool when it thinks it contains enough entropy.
In Windows NT, similar functionality is delivered by ksecdd.sys, but reading the special file DeviceKsecDD does not work as in UNIX. The documented methods to generate cryptographically random bytes are CryptGenRandom and RtlGenRandom.
While DOS does not naturally provide such functionality, there is an open-source third-party driver called noise.sys, which functions similarly in that it creates two devices, RANDOM$ and URANDOM$, which are also accessible as /DEV/RANDOM$ and /DEV/URANDOM$, that programs can access for random data.
The Linux emulator Cygwin on Windows provide implementations of both /dev/random and /dev/urandom, which can be used in scripts and programs.
random(4)– Linux Programmer's Manual – Special Files
random_read_unlimited'. 1995-11-04. Retrieved 2013-11-21.
arc4random(3)– OpenBSD Library Functions Manual
ChaCha based random number generator for OpenBSD.
Legacy arc4random(3) API from OpenBSD reimplemented using the ChaCha20 PRF, with per-thread state.
random(4)– Darwin and macOS Kernel Interfaces Manual
rnd(4)– NetBSD Kernel Interfaces Manual