Notes

Inside AES

How do you send a private note?

Alice writes “Meet me at noon.” She and Sam share one secret. Follow the note and see what a stranger could read along the way.

Overview: the 16-byte note Meet me at noon. goes into AES-128 with key K and comes out as 16 scrambled bytes.PLAINTEXTMeet me at noon.AES-128ENCRYPTKey KCIPHERTEXTbe c5 b5 f7 2f ca …16 BYTES16 BYTES

A box with two keys

Suppose Alice puts her note in a metal box and locks it. A courier carries the box across town. Sam already has a copy of Alice's key, so he can open it and read the note. Without the key, the courier can't open the box to read the meeting time.

AES does the same job for computers. The key is a secret number that Alice and Sam both have. Alice's computer uses it to scramble the note into unreadable data. That's called encryption. Sam's computer uses the same key to unscramble it. That's decryption. Press the button and follow the note.

Block diagram. Plaintext goes into AES-128 encrypt with key K, crosses a public channel watched by an observer, then AES-128 decrypt with the same key K gives the plaintext back.PUBLIC CHANNELMeet me at noon.be c5 b5 f7 2f …PLAINTEXTMeet meat noon.AES-128ENCRYPTAES-128DECRYPTPLAINTEXT—KEY K · 128 BITS00 01 02 03 … 0fSAME KEYOBSERVER SEESnothing yet
Alice's computer holds the note as plain bytes. Alice and Sam each hold the same 128-bit key, K.

The courier could learn every step of AES. The steps are public. What's missing is the particular key Alice used. The rest of this article opens the box and looks at those steps, one at a time.

Letters are numbers

A computer can't store a letter as a shape. It stores numbers. A shared table called ASCII gives every character a number: capital M is 77, lowercase e is 101, and even a space has one, 32.

Inside the machine, each number is a row of eight switches called bits. A switch is either off (0) or on (1). The switch on the right is worth 1, the next one 2, then 4, doubling each time. Add up the switches that are on and you get the number. Eight bits make a byte.

The character M is number 77, hexadecimal 4d, bits 01001101.THE NOTEMeet␣me␣at␣noon.MCHARACTERASCII77NUMBERHEX4dTWO HEX DIGITSBITS1280641320160814120114d64 + 8 + 4 + 1 = 77
“M” is number 77 in the ASCII table. Its eight switches are 01001101, written 4d in hexadecimal: one digit for each group of four switches.

Engineers often write a byte as two digits that run from 0 to 9 and then a to f. This is called hexadecimal, and 77 becomes 4d. It's a shorter way to write the same eight switches: each digit stands for four of them.

Sixteen bytes, one block

AES doesn't scramble a message letter by letter. It always works on a chunk of exactly 16 bytes, called a block. That's 16 × 8 = 128 bits.

Alice's note happens to be 16 characters long, spaces and full stop included, so it fills exactly one block. A longer note is cut into several blocks. How those blocks are chained together is a separate choice, called a mode. We'll meet one at the end.

Alice's 16-character note fills exactly one block of 16 bytes. A second block stays empty.BLOCK 1M4de65e65t74␣20m6de65␣20a61t74␣20n6eo6fo6fn6e.2e16 BYTES · 128 BITSBLOCK 2ONLY FOR LONGER NOTES
Alice's note is exactly 16 characters, so it fills one block.

Sixteen slots in a tray

Picture a tray with four rows and four columns. AES places the 16 bytes into it, filling the first column from top to bottom, then the second, and so on. The tray and whatever it holds is called the state.

Everything that follows changes the state. Some steps move bytes to other slots. Others change the numbers inside them.

Alice's note as 16 input bytes, each shown with its character and hexadecimal value, above an empty 4 by 4 state grid.000102030405060708090a0b0c0d0e0fc000010203c104050607c208090a0bc30c0d0e0fr0r1r2r3M4de65e65t74␣20m6de65␣20a61t74␣20n6eo6fo6fn6e.2eINPUT · 16 BYTES · ASCII128 BITSSTATE · 4 × 4 BYTES4 ROWSs[r,c] = in[r + 4c]COLUMN-MAJOR ORDER
Each character is one byte: its letter on top, its value in hexadecimal below. The faint numbers in the grid say which byte goes where.

What a key is

The key is also 16 bytes, so 128 bits. In this article Alice's key is the simple sequence 00, 01, 02 … 0f, so you can follow it. A real key is chosen at random.

Why 128 bits? Someone who doesn't know the key could try every possible one. Each extra bit doubles the number of keys to try. Press the button and watch the count grow as the bits are added.

Alice's 16-byte key, and a bar of 128 bits with DES's 56 bits marked. Each extra bit doubles the number of keys to try.KEY K · THIS ARTICLE'S EXAMPLE000102030405060708090a0b0c0d0e0f16 BYTES · 128 BITSBITS IN THE KEYDES · 56AES · 128BITS128POSSIBLE KEYS3.4 × 10³⁸TRY THEM ALL10.8 trillion yearsat a billion billion (10¹⁸) guesses per second
Each extra bit doubles the number of keys. 56 bits fall in a fraction of a second; 128 bits would take about 780 times the age of the universe.

The older standard, DES, used 56 bits. A machine that tries a billion billion keys per second would go through all of them in under a tenth of a second. At the same speed, 128 bits would take about 10 trillion years, roughly 780 times the age of the universe.

Move 1: replace each byte

Now the scrambling. AES works on the state in rounds, and each round makes four moves. The first move replaces every byte with a different one, taken from a fixed table of 256 entries called the S-box.

To look up a byte, split its two hex digits: the first picks the row, the second picks the column. For 4d you go to row 4, column d, and find e3, which is 227. The same table serves every byte in every round. Engineers call this move SubBytes.

Each byte is swapped for its entry in a fixed 256-entry table, the S-box. The highlighted byte is 77 (hex 4d): row 4, column d of the table holds 227 (e3). No byte changes position.c0r0c1r1c2r2c3r3STATEROUND 1 · SUBBYTES774d227e3100646743103671338511977245f5362454361046869459963251fb3927204cc10569249f91257d255ff422a229e510165774d9963251fb9862170aa9660208d03321253fdS-BOX · 256 ENTRIES00112233445566778899aabbccddeeffS[4d] = e377 → 227
Each byte is swapped for its entry in a fixed 256-entry table, the S-box. The highlighted byte is 77 (hex 4d): row 4, column d of the table holds 227 (e3). No byte changes position.

The table was designed so that its output has no simple relationship with its input. Neighboring numbers going in come out far apart, which leaves no shortcut to follow.

Move 2: slide the rows

The second move leaves the values alone and moves them. Row 0 stays put. Row 1 slides one place to the left, row 2 two places, row 3 three places. A byte that falls off the left edge comes back in on the right. Engineers call this ShiftRows.

Row r rotates left by r places. In row 1, the byte 67 starts at the left edge, so it wraps around to the right end. Values stay the same; only positions change.c0r0c1r1c2r2c3r3STATEROUND 1 · SHIFTROWS227e36945229e5253fd5436255ff208d0245f5249f9170aa13385204cc251fb6743251fb774drotl 0rotl 1rotl 2rotl 3RULErow r rotates left by rr = 0, 1, 2, 3positions changevalues unchangedundo: rotate right by r
Row r rotates left by r places. In row 1, the byte 67 starts at the left edge, so it wraps around to the right end. Values stay the same; only positions change.

Why bother? Before the slide, each column holds bytes that started in the same column. After it, each column holds one byte from each of the four columns. The next move mixes columns, so this is how bytes from across the block meet.

Move 3: mix each column

The third move takes one column at a time and turns its four bytes into four new ones. Each new byte depends on all four old ones. The recipe is a fixed table of small numbers. Engineers call this move MixColumns.

Each column is multiplied by a fixed matrix. All four bytes of column 0 combine to make its new top byte, 10 (hex 0a). The other three columns go through the same mix.c0r0c1r1c2r2c3r3STATEROUND 1 · MIXCOLUMNS227e3100a6945160a0229e51076b253fd1277f54368353255ff774d208d011876245f513284249f96945170aa238ee13385130d204cc188bc251fb1589e67433826251fb13082774d5234COLUMN 0 · FIXED MATRIX02030101010203010101020303010102·=e30a45a0e56bfd7frow 0 · column = 0a = 10× and + are byte arithmetic, GF(2⁸)
Each column is multiplied by a fixed matrix. All four bytes of column 0 combine to make its new top byte, 10 (hex 0a). The other three columns go through the same mix.

The arithmetic isn't ordinary multiplication and addition. It's a version made for bytes, where results always stay between 0 and 255 and every step can be undone. You don't need the details to see the effect: change any single byte of a column, and all four bytes of that column change.

Move 4: add the key

Only the fourth move uses a key. AES combines each byte of the state with the matching byte of a key for this round. The combination is called XOR: compare the two bytes bit by bit and, wherever the key has a 1, flip the state's bit.

Each byte is combined with the matching byte of round key 1 using XOR: wherever the key has a 1 bit, the state bit flips. Here 10 ⊕ 214 = 220.c0r0c1r1c2r2c3r3STATEROUND 1 · ADDROUNDKEY100a220dc160a0100a1076b311f1277f13082835312981774d226e211876404132841267e69451599f238ee7248130d11775188bc774d1589e724838261418d13082244f45234202caROUND KEY 1⊕214170116253210175114250218166120241214171118254 00001010 10⊕ 11010110 214= 11011100 220
Each byte is combined with the matching byte of round key 1 using XOR: wherever the key has a 1 bit, the state bit flips. Here 10 ⊕ 214 = 220.

XOR has a useful property. Doing it twice with the same key gives back what you started with, the way flipping a switch twice leaves it where it was. That's how Sam will undo this move. Engineers call it AddRoundKey.

A new key every round

If every round added the same 16 bytes, the rounds would repeat one pattern, and repeated patterns are what attackers look for. So AES stretches Alice's key into eleven round keys: one to add at the very start and one for each of the ten rounds.

Each round key is calculated from the one before it, using the same S-box and a small constant that changes every round. Sam can repeat the exact calculation, because he starts from the same key.

Eleven round keys, K0 to K10, each derived from the one above it.ROUND KEYSK000 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0fK1d6 aa 74 fd d2 af 72 fa da a6 78 f1 d6 ab 76 feK2b6 92 cf 0b 64 3d bd f1 be 9b c5 00 68 30 b3 feK3b6 ff 74 4e d2 c2 c9 bf 6c 59 0c bf 04 69 bf 41K447 f7 f7 bc 95 35 3e 03 f9 6c 32 bc fd 05 8d fdK53c aa a3 e8 a9 9f 9d eb 50 f3 af 57 ad f6 22 aaK65e 39 0f 7d f7 a6 92 96 a7 55 3d c1 0a a3 1f 6bK714 f9 70 1a e3 5f e2 8c 44 0a df 4d 4e a9 c0 26K847 43 87 35 a4 1c 65 b9 e0 16 ba f4 ae bf 7a d2K954 99 32 d1 f0 85 57 68 10 93 ed 9c be 2c 97 4eK1013 11 1d 7f e3 94 4a 17 f3 07 a7 8b 4d 2b 30 c5From the key above:1. take its last 4 bytes2. rotate them by one place3. replace them with the S-box4. add a small round constant5. XOR them through all 16 bytesK0 is Alice's key.K1 is used in round 1,K2 in round 2, and so on.
K0 is the key Alice and Sam share. Every other row is calculated from the row above it, so both of them can rebuild all eleven.

Ten rounds

Put together, AES-128 adds round key 0 once, then runs ten rounds of the four moves: replace, slide, mix and add the next round key. The last round skips the mix, which would add no extra protection at the very end.

Step through the stages and watch the state. After a single round, nothing about the bytes still looks like “Meet me at noon.” The versions with longer keys, AES-192 and AES-256, run 12 and 14 rounds.

AES round viewer: the 16 state bytes in hexadecimal, the current stage, and a timeline of the twelve stages from input to round 10.STATE · HEX4d656574206d65206174206e6f6f6e2eSTAGEInput blockplaintext bytes, no key yetKey · 00 01 02 … 0fRounds · 10 (AES-128)ink012345678910
Input block
Each stage is one full pass of the four moves. Watch the readable bytes of the note disappear after the first round.

Running it backwards

Sam receives the 16 scrambled bytes. To read the note, his computer undoes every move, in the opposite order. Each move has an exact opposite: a reverse table for the replace, a slide to the right, an inverse mix, and the same XOR with the same round key.

One round of encryption on the left, and its undo on the right, read from bottom to top. Each move has an exact opposite.ENCRYPT · ONE ROUNDDECRYPT · BOTTOM TO TOPReplace4d → e3Replace backe3 → 4dUNDOESSlide leftrow r by rSlide rightrow r by rUNDOESMix× fixed matrixUnmix× inverse matrixUNDOESAdd key K110 ⊕ 214 = 220Add key K1220 ⊕ 214 = 10THEN UNDOIN REVERSE
Sam's computer undoes the last move first. Adding the same key a second time cancels it: 220 ⊕ 214 gives back 10.

Because every move can be undone, nothing is lost on the way. With the right key, Sam gets back every bit of Alice's note. With a wrong key, the result is just as scrambled as the bytes he received.

Change one letter

Alice tries again with a lowercase m in meet. To her computer, the difference between capital M and lowercase m is one switch. A switch like this is called a bit. If she uses the same key both times, how many switches will differ in the two encrypted notes?

A map of the 128 bit positions, 16 bytes across and 8 bits down, showing where the two encryptions differ, with a bar chart of how many bits differ after each round.000102030405060708090a0b0c0d0e0fb7b6b5b4b3b2b1b0INPUT1 / 128 BITS DIFFERM → M64 · COIN FLIPBITS THAT DIFFER, PER ROUNDin12345678910
Capital M and lowercase m differ in a single bit: byte 00, bit 5. Each square is one of the 128 bit positions in the block.

For these two notes, 68 of 128 bits differ at the end. A change that began in the first letter has spread across the block. This is why comparing the two encrypted notes gives the courier no obvious clue about which letter Alice changed. Cryptographers call this spreading change the avalanche effect.

Can Sam trust the note?

Hiding the meeting time solves one problem. Sam also wants to know whether anyone changed the message on its way to him.

Picture a paper seal across the lid of the box. Opening the lid tears the seal, so Sam knows to ask Alice for a fresh note. One way for software to do a similar check is AES-GCM. It can encrypt a whole message and produce a short check value called a tag. If someone edits the encrypted message in transit, Sam's app checks the tag and rejects the message when the check fails.

AES-GCM check: Sam recomputes the tag from the received ciphertext and compares it with the tag T that came with it.RECEIVED04b3e227b9f6c3ac82fd5a060feedeb3051 BIT FLIPPEDCIPHERTEXT C · 16 BYTESa0 f5 23 f3 04 83 …TAG T · SENT WITH CRECOMPUTE TAGGHASH(C) ⊕ E(K, J0)T′ · COMPUTED BY SAMa0 f5 23 f3 04 83 …fd ec 6b 3a 28 fc …=≠ACCEPTdecrypt & deliverREJECTdiscard, don't decrypt
Accepted. The recomputed tag matches T, so Sam's app decrypts: “Meet me at noon.”

There's one more detail about the box. Sam already had his key before the courier arrived. A messaging app also needs a way for the two devices to agree on a secret key. AES can then use that shared key to keep the words private.

Where AES came from

AES stands for Advanced Encryption Standard. Before it, the United States standard was DES, published in 1977. DES used a 56-bit key. By 1998 a machine built for the job could try every possible key and find the right one in 56 hours.

In 1997 the US standards agency, NIST, asked cryptographers everywhere for a replacement. It published every proposal so that anyone could try to break them. Fifteen designs entered. In 2000 NIST chose Rijndael, created by two Belgian researchers, Joan Daemen and Vincent Rijmen. It became AES in 2001.

How AES came to be+20 y1977DES becomes the US standard56-bit key1997NIST opens a public contestfor a successor to DES1998A DES key is found in 56 hoursby a machine built to try every key2000Rijndael winsJoan Daemen and Vincent Rijmen, Belgium+9 y2001Published as AESNIST FIPS 1972010AES instructions in processorsencryption at almost no costTodayHTTPS, Wi-Fi, phones, laptopsAES-GCM behind the browser padlock
AES replaced a standard whose key had become too short to resist a machine built to guess it.

Today AES protects much of what you send and store. When your browser shows a padlock, the page usually travels encrypted with AES-GCM, the version you just saw checking Sam's delivery. Wi-Fi networks protected with WPA2 or WPA3 use AES. Phones and laptops encrypt their storage with it. Since 2010 most processors include instructions built for AES, so encrypting costs almost nothing.

Filed under Security