Ch.22: Algorithms: How AES Encryption Actually Works

Outline

Transcript

0:00 AES solves a secrecy problem in plain sight: encrypt data with a key, then recover it with that same key. An attacker can read every instruction. What they don't get is the key. So publishing the instructions doesn't publish the key. What does AES actually change in the data? It changes byte values and their positions, 16 bytes at a time. We'll follow one round with numbers small enough to check, keeping one changing value recognizable. We'll reverse the operations to recover our input, then calculate where the round keys came from.

0:32 Those 16 bytes make one block. A file can contain many blocks, but keeping one on the workbench will let us see what each operation contributes. AES means Advanced Encryption Standard. We'll use AES 1:20-eight, where the number describes the secret key's size in bits. Before following the block, start with a byte: 8 bits, each zero or one. XOR compares matching bit positions: equal bits give zero; different bits give one. There's no carry into neighboring positions. Take a data bit of zero and a key bit of one.

1:05 They differ, giving one. Apply that same key bit again. What survives? One XOR one gives zero. Your starting data returns. A one in the key flips its matching data bit; twice restores it. A zero leaves that position alone. Apply those comparisons at all eight positions for a byte. We'll speak ordinary decimal numbers, so the example doesn't become a Wi-Fi password. Now take the test block and key we've chosen to keep the early arithmetic small. The operations are exactly AES. The official AES specification defines them; we'll put it and our checked example in the description.

1:42 Arrange the 16 input bytes in four rows and four columns, filling down each column before moving right. AES calls this working square the state. Each cell holds 1 byte. Before round one, XOR each state byte with the key byte in the same position. That's Add Round Key, though this is XOR, not ordinary addition. Our top-left input is 10; its key byte is three. 10 has the eight and 2 bits set. Three has the two and 1 bits set. The twos cancel, leaving eight and 1: nine. That's our first marker. All 16 positions receive their own key bytes.

2:19 If XOR already introduces the secret and reverses itself, why add more operations? That shortcut exposes relationships if we reuse the same key bits. XOR two blocks encrypted that way and the key bits cancel, leaving the XOR of the original blocks. The extra operations must change those predictable relationships. Next comes Sub Bytes. Replace each byte using a fixed public lookup table. Our marker goes from nine to one. Wait, anyone can read that table, including its inverse. Why would a public replacement help keep anything secret?

2:53 It changes how differences travel. For example, zero and one differ in their rightmost bit. Their table outputs differ in 5 bit positions. Oh, that breaks my one-flip, one-change picture. Do two and three, differing in that same input position, flip those same five output bits too? No. Only two output bits change in that case. The effect depends on where we started. That's the nonlinear behavior AES needs, rather than a fixed response to each input-bit change. Imagine a lighting scene control: one press changes five lamps in one starting scene, but two in another.

3:29 A fixed toggle would affect the same lamps every time. Yeah, same control, but which lamps change depends on their starting scene. AES also keeps every input distinct: each possible byte has a different output. Its inverse table maps one back to nine. So the table isn't a secret codebook. The unknown key changes what enters these public transformations. No second secret needs to be shared. And substitution still acts on each position separately. Our one hasn't combined with another byte yet. Row movement will choose which neighbors it meets.

4:01 With the replacements complete, Shift Rows leaves the top row alone. It rotates the second row left once, the 3rd twice, and the bottom row three times. Rotate means a byte leaving the left edge returns at the right. Take another byte, also one, in the second row's second column. Where does one leftward move put it? In that row's first column. Its value is still one; only its location changed. Our top-left one stays put because its row doesn't move. Follow the traveling one into its new column: it now sits below that stationary one.

4:35 The lower rows rotate by different amounts, changing their column partners too. Our new first column is one, one, one and zero. We chose this input to keep the arithmetic small; AES still processes every position. So we keep one as a marker, but cannot treat it as a tiny message being encrypted alone. Mixing will spread its contribution through the column. I'm not sure how combining four values can preserve each original input. What stops the combination from losing information? So we keep enough outputs for recovery.

5:06 Take a pair of bits, one and zero. Keep the first bit and also keep their XOR. The outputs are one and one. Oh, we can recover both. XOR those two ones and the zero returns; we also kept the original one. Mix Columns uses a different rule, with four linked outputs for four input bytes. Its inverse needs the complete output column. Let's calculate its top result. Double the top input, triple the second, and take one copy each of the 3rd and 4th. XOR those contributions. Our top one doubles to two.

5:39 The second input is also one. To triple it, double it to two, then XOR with the original one. That gives three. The 3rd input contributes one; the last contributes zero. Two XOR three gives one. XOR the next one and we get zero; the final zero changes nothing. If I put that result into the input column now, won't the next calculation read my new value? Yes. All four calculations need the original column. Save the output separately; finish the calculations before replacing an input. Otherwise you'd be mixing a result into the next result.

6:16 We still have the original one, one, one and zero in place. For the second output, do the multipliers move down while the inputs stay put? Right. Double the second row's one, triple the 3rd row's one, and take the other two unchanged. Each output doubles its own original input and triples the next one down, wrapping at the bottom. Those inputs are both one again, so this gives zero too. Keep all four results: zero, zero, two and three. Those zeroes don't mean information vanished. Together the values let us recover the original column.

6:49 Our doubles fit inside a byte. What if doubling needs more than 8 bits? AES has a fixed overflow rule. Doubling shifts bits left once. If the old leftmost bit was one, keep eight positions and XOR with 27. Doubling 128 would give 256. That needs 9 bits. Do we just drop the extra one? Drop it, then apply the correction. The retained bits are zero, so XOR with 27 gives 27. That correction is specified by AES; it isn't another secret. So our small column needed no correction, but later calculations may.

7:26 Tripling still means doubled value XOR original. The mixed result is ready; when does the key come back? Now the state needs its next derived key. These round keys come from the original secret by a fixed recipe; we'll work through that after recovery. For our corner, key one contributes two. Our mixed top-left value was zero. Zero XOR the new key byte, two, gives two. XOR the matching round-key byte into every other state position too. That finishes one complete round, still with 16 bytes. That result already combines information from several inputs and key bytes.

8:02 It isn't an independently encrypted version of our starting 10. With 1 round understood, the full AES 1:20-eight schedule starts with the original key, called key zero, then performs nine full rounds: substitute, shift, mix and add the next round key. Round 10 substitutes, shifts and adds key 10, but omits Mix Columns. Adding a final mix would produce the wrong result. That last key addition finishes the ciphertext block, the encrypted output. Our complete calculation agrees with a separate AES implementation, beyond those early values we followed.

8:36 Across rounds, substitution changes byte relationships while movement and mixing spread information between positions. The derived keys bring the secret into that changing state repeatedly. My first thought was, if we can scramble it and get it back, we're done. What distinguishes being able to recover data from keeping it secret? Recoverability means we can get the data back. Reverse 16 numbered cards and anyone can restore their order. Secrecy asks what outsiders can learn without the key. So a scrambled-looking result isn't a security test.

9:06 Matching the checked result establishes that we performed the specified calculation; it doesn't prove what an outsider can learn. Before we undo it, one size detail: AES one 92 uses 12 rounds; AES 2:50-six uses 14. Both keep 16-byte blocks. Key size counts bits in the secret. Block size measures how much data one operation handles. A longer door code doesn't widen the doorway. Seeing the 16-byte block doesn't tell me which variant was used; I'd need the key length to choose its schedule. Now take the final encrypted block and test recovery.

9:40 XOR removed itself when repeated. Could we just run AES encryption a second time? No. Use inverse operations in reverse order. First remove key 10, then undo the final row shift and substitution. There was no final column mix to reverse. That returns the state to the output of round nine, after its key was added. So the next thing to remove is key nine. Then undo that round's mixing, row movement and substitutions. Work backward through the earlier rounds. At round one's output, our top-left marker is two.

10:12 Removing key one's matching byte, two, restores zero. Can an inverse table turn that zero straight back into our earlier one? No, mixing combined neighbors. Take the complete column: zero, zero, two and three. We need the inverse column calculation. We knew which inputs to double and triple going forward. How do we know what multipliers to use backward? AES specifies a fixed inverse recipe too; these numbers aren't extra keys. For the top result, the first two outputs are zero, so their contributions are zero.

10:41 Multiply the 3rd by 13 and the 4th by nine, then XOR the contributions. Our 3rd output is two. Two times 13 is the same as doubling 13: 26. The 4th output is three. For nine times three, double nine to 18, then XOR with nine. That's 27. The two products differ only in the bit worth one. 26 XOR 27 restores our one. That undoes the column mix at the top. Recover the other rows too, then undo Shift Rows across the whole square, rotating right by the earlier offsets. Now the substituted bytes are back in their old positions.

11:18 Our top-left one never moved. Its inverse substitution gives nine. Finally, nine XOR the original key byte, three, recovers 10. Nice, there's our starting value again. And the receiver didn't need our saved intermediate states. It derives the keys and reverses the specified transformations. So the zeros in that mixed column weren't lost data. The outputs still identified all four inputs. Now we can account for those round keys. Key expansion makes 11 from one secret, including the original as key zero.

11:49 Let's build key one now. Think of each 16-byte key as four words, each 4 bytes long. Keep the four old words in place. We'll build four new words from left to right. For the first new word, start with the last old word, rotate its first byte to the back, then substitute all 4 bytes. In our key, that word contains one, 82, zero, zero. Rotation brings 82 to the front; substitution makes it zero. We'll follow that leading position while all four positions are calculated. Then XOR in the round constant.

12:24 For this first expansion, its leading byte is one and its other bytes are zero. Our leading zero becomes one. We've just used the last old word. Do we combine this transformed result with that same word again, or with the first? The first old word. It starts with three. One XOR three gives two, at the front of the first new word. There's the two we used to finish the data round. For the second word, do we repeat that rotation and table lookup? No. XOR the word just made with the next original word.

12:54 Our first result starts with two; the second old word has two in that same position. Two XOR two gives zero. That starts the second new word. Continue through the remaining original words, using each freshly made result. Every XOR matches all four byte positions. That's key one. And for key two, do we go back to the original key? Use our completed key as the old key and start its first word the same way. The round constant's leading byte doubles using our byte rule; its other bytes remain zero. With the block and its keys accounted for, return to a file.

13:28 Suppose blocks one and three contain exactly the same 16 bytes. Encrypting those blocks independently with the same AES key produces identical encrypted blocks. An observer can see which pieces repeat, even though our rounds are correct. One working lock doesn't secure an entire building. Likewise, the AES block operation is one component of protecting a complete message. Right, the receiver also needs to detect changes. That's authenticity; secrecy hides what the message says. Think of one envelope: hiding its note and checking that it arrived unchanged are different jobs.

14:02 AES-GCM is one way to protect both, with its own requirements for correct use. For an application, use a vetted implementation's documented message-encryption interface. Our calculations explain the core algorithm, from changing the data to recovering the original bytes exactly. Thanks for listening to Learning Podcasts.