Part I Chapter 1
Keys, bytes, and AES
Contents
The attacks in this guide never break the mathematics of AES. They target one step at the start of the cipher, where a byte of the secret key meets a byte of the message. To see why that step is the weak spot you need three things: how bytes are written, what XOR does, and what AES does in its first round.
A cipher and a secret key
A cipher turns a readable message, the plaintext, into unreadable ciphertext, using a key. AES is a symmetric cipher: the same key encrypts and decrypts, so two parties who share it can talk privately. The algorithm itself is public; all of the security rests on the key staying secret.
AES (Advanced Encryption Standard) was standardized by NIST in 2001 (FIPS-197), from the Rijndael design by Joan Daemen and Vincent Rijmen. It’s the most widely used block cipher today. The variant in this guide, AES-128, encrypts blocks of 16 bytes with a 16-byte key. Here is one real encryption, taken from one of the datasets used later:
plaintext = c549dd73446310a073b9f9c6bdc7e66d
key = 175cf2997a8583413c77dfac7e6c59d8
ciphertext = e70e50e7a0829e5f6691ac64dde13056
You can check it with any AES-128 calculator (ECB mode, no padding).
Knowing plaintexts and ciphertexts doesn’t help you find the key. There are 2128 possible keys, about 3.4 × 1038; testing a billion per second would take around 1022 years. No mathematical shortcut that beats that meaningfully is publicly known.
Bits, bytes, and hex
A bit is a 0 or a 1. A byte is 8 bits, so it can hold 28 = 256 values, from 0 to 255. Writing bytes in binary is long, so they’re written in hexadecimal: base 16, with digits 0–9 and a–f. Each hex digit stands for 4 bits, so one byte is exactly two hex digits. The prefix 0x marks a hex number:
0x8e = 1000 1110 = 142
0xc5 = 1100 0101 = 197
The plaintext above is 32 hex digits: 16 bytes. Its first byte is 0xc5; the key’s first byte is 0x17.
XOR
XOR (written ⊕) compares two bits and outputs 1 when they differ:
0 ⊕ 0 = 0 0 ⊕ 1 = 1
1 ⊕ 0 = 1 1 ⊕ 1 = 0
On bytes it works bit by bit. The first plaintext byte XOR the first key byte:
0xc5 = 1100 0101
⊕ 0x17 = 0001 0111
= 0xd2 = 1101 0010
Two properties matter later. XOR-ing the same value twice undoes it: (a ⊕ m) ⊕ m = a. And XOR-ing any byte with a uniformly random byte gives a uniformly random byte: the result says nothing about a unless you know m. Chapter 4 builds a whole countermeasure on that second property.
Inside AES-128
AES arranges the 16 bytes of the block in a 4 × 4 grid called the state and transforms it in rounds. First the key is XOR-ed into the state. Then come 10 rounds of four steps each:
- SubBytes replaces every byte with its entry in the S-box, a fixed public table of 256 bytes. It’s the only nonlinear step in AES: without it, the whole cipher would be a system of linear equations, easy to solve.
- ShiftRows rotates the rows of the grid by 0, 1, 2, and 3 positions.
- MixColumns mixes the four bytes of each column, so that every output byte depends on all four input bytes. (The last round skips this step.)
- AddRoundKey XORs in a round key, derived from the original key by a fixed procedure called the key schedule.
ShiftRows and MixColumns spread each byte across the block: after two rounds, every output bit depends on every input bit. That’s what makes the ciphertext useless for recovering the key, and it’s why attacks look before the mixing starts. For the full S-box table and a slower walk through one round, see AES-128 and the S-box.
The weak spot: the first S-box
At the very start, each plaintext byte is XOR-ed with one key byte and the result goes through the S-box. All 16 bytes do this side by side, each in its own cell of the state, and nothing mixes them until ShiftRows and MixColumns:
That S-box output is the intermediate value, and it’s the target of every attack in this guide:
intermediate value = Sbox[ plaintext[i] ⊕ key[i] ]
Two facts make it the weak spot:
- It depends on only one key byte, plus a plaintext byte the attacker knows. A key byte has 256 possible values, so the attacker can try them all.
- Each of the 16 key bytes can be attacked separately. That turns 2128 possible keys into 16 independent searches of 256 guesses each: 4,096 guesses in total.
Why after the S-box, and not right after the XOR? Look at guesses that are almost right. Each wrong guess below differs from the true key byte, 0x17, in a single bit:
Right after the XOR, a wrong guess predicts almost the same value as the right one, so the two are hard to tell apart. The S-box is nonlinear: it turns a one-bit miss into a value that looks unrelated, so a wrong guess predicts something clearly different and stands out as wrong.
All of this is useless if the chip only ever shows its ciphertext. The next chapter is about what else it shows.