Skip to content
CryptoVault

Chapter II

Ground rules

What modern cryptography promises, who it defends against, and the raw material it works with: bits, XOR and randomness.

  1. II.1The four goals
  2. II.2Kerckhoffs’s principle
  3. II.3Attackers and threat models
  4. II.4Bits of security
  5. II.5Bits, bytes and XOR
  6. II.6Encoding is not encryption
  7. II.7Randomness

The four goals

Confidentiality, integrity, authenticity, non-repudiation: four different promises, and encryption alone keeps only the first.

encrypthashno keyMACsignConfidentialityEve reads itIntegrityMallory alters itaccidentsAuthenticityMallory poses as AliceNon-repudiationAlice denies sending it
A hash catches accidents only: whoever can replace a file can replace the hash printed beside it.
Real systems need the first three together, which is why modern ciphers authenticate as they encrypt (chapter IV).

Kerckhoffs’s principle

Assume the enemy knows the whole system. Only the key is secret.

Kerckhoffs’s second requirement: the system “must not require secrecy, and must be able to fall into the enemy’s hands without inconvenience.”

keythe only secretplaintextAESpublishedciphertextseesknowsEEveif the key leaksif a secret design leakschange the keyin minutesevery card exposedMIFARE Classic, 2008
Publish the design, protect the key. If explaining how your system works would break it, it is already broken.

Attackers and threat models

Secure means nothing until you say against whom: what the attacker can see, and what they can make you do.

Eve listens; Mallory also alters and injects messages. What they can obtain sets the attack model:

stronger attackerCiphertext onlyfrequency analysisEencryptcKnown plaintextEnigma cribsEencryptm and cChosen plaintextMidway, 1942MencryptmcChosen ciphertextBleichenbacher, 1998Mdecryptc′m′ or error
Eve only intercepts (dashed); Mallory gets the system to encrypt or decrypt inputs of her choice.
Modern encryption must survive the last rung: an attacker who can get any other ciphertext decrypted learns nothing about yours (IND-CCA).

Bits of security

An n-bit key means 2ⁿ guesses. At 128 bits, brute force is physically out of reach.

A key of nn bits can take 2n2^n values, and each extra bit doubles the attacker’s work. Strength is counted in bits: the base-2 logarithm of the effort.

DES, 56 bits72 millisecondsAES, 128 bits1.1 × 1013 years781 ages of the universe256 bits3.7 × 1051 years01 second: 60 bits1 year: 85 bitsage of the universe: 118 bits
At 1018 guesses per second, a billion machines testing a billion keys each, every key in the red stretch falls within a year.
128 bits is the modern floor, 256 the comfortable margin.

Bits, bytes and XOR

XOR flips the bits where the key has a 1; the same key flips them back.

XOR, written ⊕, is the workhorse on bits. With a uniformly random key, the result is uniformly random whatever the message says: the one-time pad of chapter I, on bits, and the core of every stream cipher (chapter IV).

0 ⊕ 0 = 00 ⊕ 1 = 11 ⊕ 0 = 11 ⊕ 1 = 0m0100100001101001Hi⊕ k00111100101001013c a5c011101001100110074 cc⊕ k00111100101001013c a5m0100100001101001Hi
Where the key has a 1, the bit flips (heavy cells); the same key flips it back.
PitfallA short key repeated over a long message is the Vigenère cipher in binary, and falls to the same counting.

Encoding is not encryption

Hex and Base64 change how bytes look, not who can read them.

Bytes often cross channels made for text, such as URLs and headers. Hex writes each byte as two characters. Base64 reads six bits at a time, each picking one of 64 letters, digits and signs, and pads the end with =.

M4da61n6e01001101011000010110111019T22W5F46uBasic QWxhZGRpbjpvcGVuIHNlc2FtZQ==decode, no keyAladdin:open sesame
Three bytes read six bits at a time become four characters. RFC 7617’s Basic header decodes just as easily.
PitfallHTTP Basic authentication sends the password this way: only the TLS around it (chapter VII) keeps it secret.

Randomness

Keys, IVs and salts come from the operating system’s secure generator, a CSPRNG, never from Math.random. Nonces only have to be unique.

the OS CSPRNGa counter0, 1, 2…key16 or 32 bytessecret,uniformly randomIV16 bytesunpredictable(for CBC)salt16 bytesuniqueper passwordnonce12 bytesnever repeatsunder one key

The generator is the operating system’s: crypto.getRandomValues() in browsers, Node.js and Bun, getrandom() on Linux. Math.random() is not one: browsers use xorshift128+, whose state a few outputs rebuild. The simplest generators give themselves away after one.

x → (1103515245·x + 12345) mod 231LCG, glibc TYPE_0seen1784350339steppredictedactual13599948801359994880436375609436375609161264191816126419183 of 3 predictedthe OS CSPRNGseen85148920steppredictedactual147692692965505334235550418212742188342916380715970824590 of 3 predicted
The LCG outputs its whole state, so one number gives away the rest; the CSPRNG’s outputs reveal nothing of its state.
One call to the CSPRNG for every secret or unique value. Chapter VIII shows what happens when that source goes wrong.

In one breath

  1. The four goals. Confidentiality, integrity, authenticity, non-repudiation: four different promises, and encryption alone keeps only the first.
  2. Kerckhoffs’s principle. Assume the enemy knows the whole system. Only the key is secret.
  3. Attackers and threat models. Secure means nothing until you say against whom: what the attacker can see, and what they can make you do.
  4. Bits of security. An n-bit key means 2ⁿ guesses. At 128 bits, brute force is physically out of reach.
  5. Bits, bytes and XOR. XOR flips the bits where the key has a 1; the same key flips them back.
  6. Encoding is not encryption. Hex and Base64 change how bytes look, not who can read them.
  7. Randomness. Keys, IVs and salts come from the operating system’s secure generator, a CSPRNG, never from Math.random. Nonces only have to be unique.