A 128-bit key has more possible values than there are grains of sand on Earth. But what if the key was made in a way that only ever produces a few thousand of them?
A key is only as good as its randomness
Lesson 1.1 showed that a good key is long, random and secret. Length is easy to measure. Randomness is easy to get wrong, and a mistake doesn't show: a weak key looks exactly like a strong one.
What matters is how unpredictable the key-making process was, not how long the key is. That unpredictability is called entropy. For example, a generator whose only secret input is a uniformly chosen 16-bit seed can produce at most 65,536 different keys, even if each output is 128 bits long; an attacker has 65,536 candidates to try, not 2128. Entropy isn't simply a count of possible outputs, though: a process can have many possible outputs and still make some of them far more likely than others, which helps an attacker just as much.
Where good randomness comes from
Computers are built to be predictable, so randomness needs care. Secure systems use a two-stage design:
- An entropy source measures something physically unpredictable, such as tiny variations in electrical noise or timing, inside a chip or the operating system.
- A cryptographic random-number generator takes that entropy as seed material and expands it into pseudorandom output, within limits on how much it may produce before reseeding with fresh entropy. A well-designed one is unpredictable to anyone who doesn't know its internal state. Producing more output doesn't add entropy: everything it produces depends on the entropy the seed supplied.
Ordinary random functions in programming languages, built for games or simulations, are not designed for this. Their output can often be predicted from a few earlier values. Keys must come from a cryptographic generator: in a browser, crypto.getRandomValues(), which this course's demos use.
Try it: a weak seed vs. a strong one
Both buttons make a 128-bit key. The first builds it from a 16-bit seed, as a careless program might; the second uses your browser's cryptographic random-number generator. Then let an attacker try every seed. Nothing is sent or stored.
Make a key to start.
The attacker knows exactly how the weak key was made, as Kerckhoffs's principle says they should be assumed to. Only the seed is unknown, and there are just 65,536 seeds.
When randomness fails
This isn't hypothetical. In 2008, it emerged that a 2006 change to the OpenSSL package in the Debian Linux distribution had removed most of its entropy. For a given processor architecture, key type and size, and the conditions under which keys were generated, affected versions could produce only about 32,768 different keys, few enough to list in advance. Keys generated by affected versions had to be replaced; the date range alone doesn't identify a vulnerable key.
Signatures are vulnerable too. Every ECDSA signature uses a secret per-signature value, its nonce, as lesson 1.5 explained. Reusing the same nonce with the same private key for two different message hashes, or using a predictable nonce, can expose the private key. (Deterministic ECDSA safely reproduces the same signature when signing the identical message again.) Weak randomness can therefore leak a key that was itself generated perfectly.
Keeping the key secret
A perfectly random secret or private key is still only safe while it stays secret. (Public keys, by design, don't need this.) Software can be copied, memory can be read by malware, and backups can leak. Strong protection comes from keeping the key inside dedicated hardware:
- Secure elements are the chips inside smart cards and many phones. They're designed to resist physical and electronic attacks, and can generate a key pair inside the chip.
- Hardware security modules (HSMs) do the same job for servers and certificate authorities, at larger scale.
Several choices are involved, and they're separate. Where the key is generated: on the device itself, or elsewhere and loaded in. Export policy: a key marked non-exportable can be used for signing or decrypting, but there's no command to read it out in plaintext. Backup policy: some HSMs support protected backup or replication to other HSMs, while still preventing plaintext export; a smart card usually allows no backup at all. A PIN or other authorisation can also be required before the key is used. If the key exists only on one device and its policy permits no backup, losing the device means losing the key. Recovery requires issuing a replacement.
Try it: a key that won't come out
Your browser's cryptography API can mark a key as non-exportable, a small software version of the rule a secure element enforces. This makes a signing key pair with a non-exportable private key, signs with it, and then asks for the key itself.
The browser enforces this as a restriction in its cryptography API. The demo doesn't establish where the key is kept, or whether hardware protects it. A secure element enforces the same rule in hardware designed to resist attack.
Trusting the hardware
Because you can't look inside a secure chip, independent testing matters. Cryptographic modules can be tested against FIPS 140-3, the security standard for cryptographic modules used by the joint U.S. and Canadian validation programme, which covers how keys are generated, stored and protected. Smart-card chips are also commonly evaluated under the international Common Criteria scheme. These certifications are a large part of why government programmes such as PIV specify certified cards and modules.
Go deeperOptional technical detail for readers who want more
The NIST SP 800-90 series
NIST describes random-bit generation in three parts: SP 800-90A specifies deterministic random bit generators (the "cryptographic generator" stage), including limits on how much output may be produced between reseeds; SP 800-90B covers entropy sources and how to test them; and SP 800-90C covers how to combine the two into a complete generator.
How the demo's weak key is made
The weak generator takes a 16-bit seed and stretches it into 128 bits with a simple, non-cryptographic mixing function. The output looks random, but this generator can produce at most 65,536 keys. The attacker runs the same function on every seed and compares, which takes a fraction of a second. A key from the cryptographic generator isn't among those 65,536 outputs; finding it would mean searching a space of 2128 possible values, about 2127 guesses on average.
Key generation on smart cards
PIV cards can generate their key pairs on the card, and FIPS 201 requires certain PIV keys to be generated and kept on the card. The card then exports only the public key, which goes into a certificate request.
Sources
Draft lesson, awaiting technical review · Last updated 19 November 2026