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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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?
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.
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.
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