0Pricing
Cryptology Academy · Lesson

Cryptographically Secure PRNGs

Explore /dev/urandom, ChaCha20-CSPRNG, and Fortuna.

Cryptographically Secure PRNGs is a free Cryptology Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Cryptology Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

CSPRNG Requirements Recap

A CSPRNG must satisfy: (1) next-bit unpredictability — knowing all previous bits, the next bit cannot be predicted with probability > 1/2 + negligible. (2) state compromise extension resistance — learning the current state reveals nothing about past output.

/dev/urandom and getrandom()

Linux /dev/urandom uses a CSPRNG seeded from the entropy pool. After initial seeding (128 bits of entropy), it is safe to use even when the pool is "empty" — the CSPRNG maintains its own internal state. getrandom() is the modern syscall equivalent.

ChaCha20-Based CSPRNG

Linux kernel 5.17+ replaced its CSPRNG with a ChaCha20-based design. ChaCha20 is fast (no hardware acceleration needed), provides 256-bit security, and has a well-understood security proof. Nonce is the counter; key comes from the entropy pool.

Fortuna

Fortuna (Ferguson & Schneier, 2003) is a CSPRNG design that maintains 32 entropy pools and accumulates entropy continuously. The generator is reseeded from pools as they fill. Resistant to RNG reset and partial state compromise attacks.

DUAL_EC_DRBG Backdoor

NIST SP 800-90A originally included DUAL_EC_DRBG, based on elliptic curves. Snowden documents revealed the NSA inserted a backdoor: the EC points were chosen so the NSA could predict output from any seed. Never use DUAL_EC_DRBG. NIST removed it in 2014.

CTR_DRBG

CTR_DRBG (NIST SP 800-90A) uses AES in counter mode as the CSPRNG. Seed = 256-bit key + 128-bit nonce. Each call advances the counter. Widely implemented in OpenSSL, BoringSSL, and Windows BCrypt. Hardware-accelerated on AES-NI CPUs.

HASH_DRBG and HMAC_DRBG

HASH_DRBG uses a hash function (SHA-256) iteratively with a seed value. HMAC_DRBG uses HMAC for mixing. HMAC_DRBG is simpler to implement correctly and is used in many embedded systems (mbedTLS, WolfSSL).

Reseeding and Entropy Injection

Even CSPRNGs must reseed periodically. CTR_DRBG has a reseed interval of 2^48 requests. Fortuna reseeds from its entropy pools. Manual reseeding: add fresh entropy (getrandom bytes) to the CSPRNG state at startup and after fork().

The Fork Problem

When a process forks, both parent and child inherit the same CSPRNG state — they will produce identical output. Fix: call getrandom() to reseed immediately after fork(). OpenSSL handles this automatically via pthread_atfork() in recent versions.

CSPRNG in Practice: Python

import secrets key = secrets.token_bytes(32) # 256-bit key nonce = secrets.token_bytes(12) # 96-bit nonce for AES-GCM pin = secrets.randbelow(10**6) # 6-digit PIN # Never use random module for cryptography

Browser Crypto

window.crypto.getRandomValues(typedArray) is the browser's CSPRNG. It calls the OS entropy source. Used by WebCrypto API for key generation. Never use Math.random() for security — it is a non-cryptographic PRNG seeded from system time.

Quick Check

Which Python module should you use to generate cryptographic random bytes?

Recap

CSPRNGs like ChaCha20, CTR_DRBG, and Fortuna generate unpredictable output from entropy seeds. Avoid DUAL_EC_DRBG, random.random(), and Math.random(). Use secrets (Python) or window.crypto (browser). Next: real-world entropy failures.

Frequently asked questions

Is the “Cryptographically Secure PRNGs” lesson free?

Yes — the full text of “Cryptographically Secure PRNGs” is free to read here on the web, and the Cryptology Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Cryptology Academy course, upgrade to CoddyKit PRO.

What will I learn in “Cryptographically Secure PRNGs”?

Explore /dev/urandom, ChaCha20-CSPRNG, and Fortuna. You practise Cryptology Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Cryptology Academy?

No prior experience is required. Cryptology Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Cryptographically Secure PRNGs” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Cryptology Academy lesson?

Yes. Every Cryptology Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. True Randomness vs Pseudorandomness
  2. Cryptographically Secure PRNGs
  3. Entropy Starvation & Weak-Key Bugs
  4. Generating Keys, Nonces & IVs Safely
← Back to Cryptology Academy