Skip to content
Trapdoor·Lab

Randomness

1Toy

Bytes straight from the system generator

Every key, nonce and salt starts with random bytes. The browser provides them through crypto.getRandomValues — a cryptographically secure generator backed by the operating system.

Random generator

All zeros for now. Draw to see real bytes.

Drawn bytes 32 B · 256 bits
00 00000000000000000000000000000000
10 00000000000000000000000000000000
1bit 1 — filled square 0bit 0 — outline
hex0000000000000000000000000000000000000000000000000000000000000000
2Break it

A seed from the clock

A generator that is not cryptographic computes its whole output from the seed: whoever knows the seed knows the key. If the seed is the time in seconds, there are only as many possible keys as there are seconds in the window an attacker can guess. Libbitcoin Explorer (bx seed) seeded MT19937 with just 32 bits of system time, so according to the authors of the “Milk Sad” disclosure (CVE-2023-39910) going through all of the roughly 4.29 billion seeds takes a few days on an average gaming PC at most. In the toy you are first the developer who creates a key this way, then the attacker who recovers it.

A key from the clock

Toy implementation — do not use in productionMT19937 seeded with the clock is predictable. Real keys: crypto.getRandomValues.

The generator is waiting for the clock.

3In the real world

Where randomness matters

  • TLS 1.3The random fields of ClientHello and ServerHello are 32 bytes each from a secure generator, and RFC 9846 recommends an existing generator — from the operating system or a cryptographic library — over writing your own.Source (TLS 1.3)
  • Linux getrandom()The system call fills a buffer with random bytes from the kernel — by default from the same source as /dev/urandom — fit for cryptographic purposes.Source (Linux getrandom())
  • Cloudflare LavaRandA camera films a wall of lava lamps, and the video becomes an additional entropy source that Cloudflare’s servers mix into their pool after boot (on Linux, by writing to /dev/random).Source (Cloudflare LavaRand)
  • Debian OpenSSL (2008)A Debian patch kept entropy out of OpenSSL’s generator pool, leaving the process ID as the only seed — just 32,767 possible streams per architecture, so weak SSH and TLS keys could be generated in advance (CVE-2008-0166).Source (Debian OpenSSL (2008))
  • Milk Sad (2023)bx seed in Libbitcoin Explorer 3.x created wallet seeds with an MT19937 generator seeded with only 32 bits of system time; attackers went through all the seeds and stole funds from the wallets (CVE-2023-39910).Source (Milk Sad (2023))
  • Bitcoin wallets on Android (2013)Weaknesses in the Android component that generates secure random numbers left every wallet generated by an Android app up to then vulnerable to theft.Source (Bitcoin wallets on Android (2013))
4Take it to your project

Draw from a cryptographic generator

Good practice

Draw keys and tokens in the browser with crypto.getRandomValues, identifiers with crypto.randomUUID(), and on a .NET server with RandomNumberGenerator.GetBytes.

JavaScript
const key = crypto.getRandomValues(new Uint8Array(32));
const id = crypto.randomUUID();
C#
byte[] key = RandomNumberGenerator.GetBytes(32);
Anti-pattern

Math.random() in V8 is xorshift128+, which the V8 team says is not cryptographically secure, and anyone who guesses the time can reproduce new Random(seed) seeded from the clock.

JavaScript
// ✕ predictable generator
const token = Math.random().toString(36).slice(2);
C#
// ✕ seed from the clock
var rng = new Random((int)DateTime.UtcNow.Ticks);