We will follow one small piece of software from design to failure. A developer at a ticket-booking start-up needs password-reset tokens: a string emailed to a user, valid for an hour. She wants it unguessable. She reaches for the language’s ordinary random function, because that is what the tutorial used. Let us see where that goes.

Step 1: how ordinary random functions work

A standard pseudo-random number generator (PRNG) is a formula. It holds an internal state, and each call transforms the state and emits a number. Given the same starting state, called the seed, it produces the identical sequence every time. This is a feature for simulations and games, where you want repeatable results and speed.

Many default generators are also designed so that their state can be worked out from a modest number of observed outputs, because nobody designed them to resist an adversary. Their goal is a good statistical spread, not secrecy.

Step 2: the seed

Where does the seed come from? Often from the current time in seconds or milliseconds. That feels random; the clock keeps changing. But consider what an attacker knows. They request a reset for their own account and note when the email arrived, giving a time within a second or two. They also know roughly when the victim’s request was made, because they triggered it.

Count the possibilities. A day has 86,400 seconds. A year has 31,536,000, which is a little under 225 (33,554,432). So a seed taken from the timestamp in whole seconds carries at most about 25 bits of uncertainty for a given year, and an attacker who knows the hour carries about 12 bits (3,600 possibilities). A computer can try 3,600 seeds in a blink. The token looks like 32 random-looking characters, and still offers about the guessing resistance of a four-digit number.

The lesson sits right there: the strength of what you generate is limited by the entropy of the seed, not by the length of the output. Stretching a weak seed into a long string adds no secrecy.

Step 3: the real-world version

This failure has happened at serious scale. In 2008 it emerged that a change to Debian’s OpenSSL package, made in 2006, had accidentally removed most of the sources of entropy used when generating keys. Afterwards the process ID was effectively the only varying input, so key generation could yield only about 32,000 distinct keys for a given key type and size (CVE-2008-0166). Anyone could precompute all of them and check whether a server’s key was on the list. A widely used, mathematically sound algorithm was undone by what was fed into it.

Other recurring cases include embedded devices that generate keys at first boot, when they have collected almost no unpredictable events yet, and virtual machines cloned from a snapshot so that many copies share the same generator state.

Step 4: what an unpredictable source looks like

Operating systems gather entropy from unpredictable physical events: timing jitter of interrupts, disk and network arrival times, and on modern processors, hardware random number instructions. Many chips also include dedicated hardware generators based on physical noise. The OS mixes these into a pool and feeds a cryptographically secure PRNG (CSPRNG), a generator designed so that even if you see lots of outputs you cannot work out its state or predict the next value, and so that past outputs stay safe if the state is later exposed, in good designs.

Applications should not build this themselves. Every mainstream platform exposes the operating system’s generator through a security-oriented interface: the secure-random classes in Java and C#, the secrets module in Python, the Web Crypto API in browsers, and the system’s random device on Linux. NIST SP 800-90A describes approved deterministic generators, and SP 800-90B covers evaluating entropy sources.

Step 5: fixing the booking start-up’s tokens

The developer switches to the platform’s secure generator, produces 32 bytes (256 bits) per token, encodes them as text, stores only a hash of the token on the server, sets a one-hour expiry, and invalidates the token after first use. Length here is sensible because the source is now strong: 128 bits is generally enough, and 256 is comfortable.

Then she checks everything else that needs unpredictability, because the same mistake hides in several places:

  • Session identifiers and API keys.
  • Encryption keys and initialisation vectors or nonces where the algorithm requires unpredictable ones.
  • Salts for password hashing.
  • One-time passwords sent by SMS: six digits is only about 20 bits (106 is just under 220), so rate limiting and short expiry carry the security there, not the digits.
  • Coupon codes, invite links and booking references that grant access to something.

Sequential identifiers deserve a note. Booking number 10482 followed by 10483 is not random at all, and if the number itself grants access, anyone can walk through neighbours. That belongs to access control, but it shares the same root: predictability.

What remains hard

Even correct tools can be misused. Early-boot environments, cloned virtual machines and containers started from identical images still need care; check how your platform reseeds. Reviewing code for use of non-secure generators is something static analysis tools do reasonably well, which makes it a cheap review item. And nobody can look at a token and tell if it is secure. Only knowing how it was made tells you.

Figures and names in the examples are illustrative.

Leave a Reply

Trending