Okay, this is a very good and complex question. The answer depends heavily on the Linux distribution, kernel version, the specific RNG source being used, and what you mean by "repeating." Let's break it down, covering the common sources and their characteristics. I'll give estimates, but these are estimates and can vary. I'll also discuss what "repeating" means in this context.
Understanding the Problem: RNGs and Periodicity
- Pseudo-Random Number Generators (PRNGs): These are algorithms that appear random but are deterministic. Given the same seed, they will produce the same sequence of numbers. They have a period – the length of the sequence before it starts repeating. Modern PRNGs have extremely long periods, but they will eventually repeat.
- True Random Number Generators (TRNGs): These rely on physical phenomena (like thermal noise, atmospheric noise, or hardware timers) to generate randomness. Ideally, they don't have a period. However, even TRNGs can have biases or weaknesses that can lead to predictability over very long timescales.
- Hybrid Approaches: Linux often uses a hybrid approach, combining TRNG sources to seed and periodically re-seed a PRNG. This aims to get the best of both worlds: good initial randomness and fast generation.
- "Repeating" Definition: What constitutes a "repeat" is important.
- Exact Repeat: The same sequence of numbers appearing again. This is what we're generally concerned with when talking about the period of a PRNG.
- Statistical Repeat: The output starts to show patterns or correlations that deviate from true randomness. This can happen before an exact repeat, especially if the RNG is poorly designed or seeded.
Common Linux RNG Sources and Their Speed/Periodicity
Here's a breakdown of the most common sources, along with estimates of how fast you can call them before seeing issues:
-
/dev/random and /dev/urandom
- Underlying Mechanism: These are the primary interfaces for accessing the kernel's RNG. Historically,
/dev/random blocked if the entropy pool was considered too low, while /dev/urandom never blocked. Modern kernels (especially since around 2016) have largely removed the blocking behavior from /dev/random in most cases, making it behave more like /dev/urandom.
- Entropy Pool: The kernel maintains an entropy pool, collecting randomness from various sources (interrupt timings, disk I/O, network activity, etc.). The entropy pool is used to seed the underlying PRNG.
- PRNG: The kernel uses a cryptographic PRNG, typically based on the ChaCha20 stream cipher (or previously, the Yarrow algorithm). ChaCha20 has a very large period (264).
- Speed: Very fast. You can read from
/dev/urandom at gigabytes per second on modern hardware. The speed is limited by your CPU and I/O bandwidth, not the RNG itself.
- Periodicity/Repeating:
- ChaCha20 Period: The theoretical period of ChaCha20 is 264. This is an astronomically large number. You would need to generate 264 bytes of data to see a repeat, which is far beyond practical limits.
- Reseeding: The kernel periodically reseeds the PRNG with entropy from the entropy pool. The frequency of reseeding depends on the kernel configuration and the availability of entropy. This is the more practical concern. If you exhaust the entropy pool and the kernel can't reseed quickly enough, you could start to see predictable output. However, this is difficult to do in practice on a well-configured system.
- Practical Limit: On a typical system, you could likely read terabytes per second from
/dev/urandom for a significant period (hours, days) before you'd start to see any noticeable degradation in randomness. The exact amount depends on how quickly the entropy pool is replenished.
-
/dev/hwrandom (Hardware RNG)
- Underlying Mechanism: This interface provides access to a hardware RNG, if one is present in your system (e.g., Intel's RDRAND instruction, AMD's RDRAND/RDSEED).
- TRNG: Hardware RNGs are based on physical phenomena and are designed to be truly random.
- Speed: The speed depends on the hardware RNG. RDRAND/RDSEED can generate random numbers very quickly (hundreds of megabytes per second or even gigabytes per second).
- Periodicity/Repeating: Ideally, a TRNG has no period. However:
- Hardware Limitations: Hardware RNGs can have biases or weaknesses. They may not produce perfectly uniform random numbers.
- Statistical Tests: You should always test the output of a hardware RNG with statistical tests (e.g., NIST Statistical Test Suite) to ensure its quality.
- Practical Limit: You can generally call
/dev/hwrandom very rapidly without encountering issues, as long as the hardware RNG is functioning correctly. The limiting factor is usually the speed of the hardware interface.
-
/proc/sys/kernel/random/entropy_avail
- Purpose: This file shows the estimated amount of entropy available in the kernel's entropy pool (in bits). It's not a source of random numbers itself, but it's a useful indicator of the health of the RNG system.
- Monitoring: You can monitor this value to see how quickly entropy is being consumed and replenished. If it consistently stays very low, it suggests that the system is not gathering enough entropy.
How to Test and Measure
- Read and Analyze: Write a program that reads a large amount of data from
/dev/urandom or /dev/hwrandom and then performs statistical tests on the output. Tools like the NIST Statistical Test Suite (STS) are commonly used for this purpose.
- Monitor Entropy: Use a script to continuously monitor
/proc/sys/kernel/random/entropy_avail. See how quickly it decreases when you read from /dev/urandom.
- Reseed Testing: Try to deliberately exhaust the entropy pool (e.g., by reading from
/dev/random rapidly) and see how long it takes for the kernel to recover.
- Repeated Seed Testing (for PRNGs): If you really want to test the period of the underlying PRNG, you can try to obtain the seed value (this is difficult and not generally recommended) and then run the PRNG algorithm yourself to see when it repeats. However, this is usually not necessary, as the period of ChaCha20 is so large.
Practical Recommendations
- Use
/dev/urandom: For almost all applications, /dev/urandom is the correct choice. It's fast, and the risk of running out of entropy is very low on modern systems.
- Consider
/dev/hwrandom: If your system has a hardware RNG and you need the highest possible level of randomness, /dev/hwrandom is a good option. But be sure to test its output with statistical tests.
- Monitor Entropy: Keep an eye on
/proc/sys/kernel/random/entropy_avail to ensure that the system is gathering enough entropy.
- Don't Roll Your Own: Avoid implementing your own RNG unless you are a cryptography expert. The kernel's RNG is well-tested and secure.
In summary: You can call /dev/urandom or /dev/hwrandom extremely fast without encountering issues in most practical scenarios. The period of the underlying PRNG (ChaCha20) is so large that it's not a concern. The more important consideration is ensuring that the entropy pool is adequately replenished. If you're concerned about the quality of the randomness, always test the output with statistical tests.