AES
The Advanced Encryption Standard. Rijndael structure, the four stages, GF(2^8) arithmetic, key expansion, decryption and implementation.
AES Explained (Advanced Encryption Standard) - ComputerphileComputerphile · AES rounds: SubBytes, ShiftRows, MixColumns, AddRoundKey, 14 min
One Encryption Standard to Rule Them All! - ComputerphileComputerphile · where AES came from: the Rijndael competition, 9 min
Introduction to Advanced Encryption Standard (AES)Neso Academy · AES structure, rounds versus key size, AES versus DES, 11 min
AES Encryption and DecryptionNeso Academy · AES encryption and decryption transformations per round, 13 minAES backgroundSearch on YouTube
OriginsSearch on YouTube
It was clear that DES needed a replacement:
- There are theoretical attacks that can break DES.
- Exhaustive key search attacks on DES have been demonstrated.
- Triple DES is possible, but it is slow and has small blocks.
The timeline of the selection:
- US NIST issued a call for ciphers in 1997.
- 15 candidates were accepted in June 1998.
- 5 were shortlisted in August 1999.
- Rijndael was selected as the AES in October 2000.
- AES was issued as the FIPS PUB 197 standard in November 2001.
AES timeline: one step each year from 1997 to 2001. Call (1997), 15 candidates (1998), 5 finalists (1999), Rijndael (2000), FIPS 197 (2001).
RijndaelSearch on YouTube
Rijmen and Daemen designed Rijndael in Belgium. It has 128/192/256-bit keys and a 128-bit data block.
AES is an iterative cipher, not a Feistel cipher:
- It processes data as a block of 4 columns of 4 bytes.
- It operates on the whole data block in every round.
The design goals:
- resistance against known attacks
- speed and code compactness on many CPUs
- design simplicity
A Feistel cipher such as DES changes only one half of the data in each round. AES changes all 16 bytes in every round. This is a main difference between the two designs.
Comments on AESSearch on YouTube
The deck lists these points about AES:
- It is an iterative cipher, not a Feistel cipher.
- The key expands into an array of 32-bit words. Four words make the round key for each round.
- It uses 4 different stages.
- It has a simple structure.
- Only AddRoundKey uses the key.
- AddRoundKey is a form of Vernam cipher.
- Each stage is easy to reverse.
- Decryption uses the keys in reverse order.
- Decryption recovers the plaintext.
- The final round has only 3 stages.
AES structureSearch on YouTube
Encryption processSearch on YouTube
The state is the data block of 4 columns of 4 bytes (16 bytes). The key expands into an array of words. The encryption process:
- The 16-byte plaintext becomes the input state.
- The initial transformation XORs the state with the round 0 key (AddRoundKey).
- Rounds 1 to each apply 4 transformations.
- Round applies only 3 transformations (it has no MixColumns).
- The final state is the 16-byte ciphertext.
Each round key has 16 bytes. The number of rounds depends on the key length:
| Key (bits) | Key length (bytes) | Number of rounds | Full rounds before the last round |
|---|---|---|---|
| 128 | 16 | 10 | 9 |
| 192 | 24 | 12 | 11 |
| 256 | 32 | 14 | 13 |
The deck says "9/11/13 rounds": these are the full rounds. The initial key XOR and the incomplete last round come in addition.
Round stagesSearch on YouTube
In each full round, the state goes through:
- Substitute bytes: byte substitution, with 1 S-box for every byte.
- Shift rows: a permutation of bytes between groups (columns).
- Mix columns: a substitution that uses matrix multiplication of groups.
- Add round key: an XOR of the state with key material.
You can see AES as two alternating steps: XOR with the key, then scramble the data bytes. The design gives fast implementations with XOR and table lookup.
Round order: Sub, Shift, Mix, Add ("S-S-M-A"). The last round drops Mix. Before round 1, there is one extra Add.
The AES roundSearch on YouTube
The AES round figure shows the 16 state bytes going through the stages:
- SubBytes: 16 S-boxes, one for each byte.
- ShiftRows: bytes move to other columns.
- MixColumns: four blocks, one for each 4-byte column.
- AddRoundKey: each byte XORs with one round-key byte .
Each stage is easy to reverse on its own. Decryption runs the inverse stages in reverse order. Learn the stage order and the inverse of each stage.
The four stagesSearch on YouTube
Substitute bytesSearch on YouTube
Substitute bytes is a simple substitution of each byte:
- It uses one table of 16 x 16 bytes. The table holds a permutation of all 256 8-bit values.
- Each byte of the state is replaced by the byte at row (left 4 bits) and column (right 4 bits).
- Example: byte is replaced by the byte in row 9, column 5, which has the value .
- The S-box is built with a defined transformation of values in .
- The S-box is designed to resist all known attacks.
Deck example of SubBytes on a full state:
| Input | Output | |||||||
|---|---|---|---|---|---|---|---|---|
| EA | 04 | 65 | 85 | 87 | F2 | 4D | 97 | |
| 83 | 45 | 5D | 96 | EC | 6E | 4C | 90 | |
| 5C | 33 | 98 | B0 | 4A | C3 | 46 | E7 | |
| F0 | 2D | AD | C5 | 8C | D8 | 95 | A6 |
Shift rowsSearch on YouTube
Shift rows is a circular byte shift:
- The 1st row does not change.
- The 2nd row does a 1-byte circular shift to the left.
- The 3rd row does a 2-byte circular shift to the left.
- The 4th row does a 3-byte circular shift to the left.
Decryption inverts this step with shifts to the right. The state is processed by columns, so this step moves bytes between the columns.
Deck example (input is the SubBytes output above):
| Input | Output | |||||||
|---|---|---|---|---|---|---|---|---|
| 87 | F2 | 4D | 97 | 87 | F2 | 4D | 97 | |
| EC | 6E | 4C | 90 | 6E | 4C | 90 | EC | |
| 4A | C3 | 46 | E7 | 46 | E7 | 4A | C3 | |
| 8C | D8 | 95 | A6 | A6 | 8C | D8 | 95 |
Shift amount = row number counted from zero. Rows 0, 1, 2, 3 shift left by 0, 1, 2, 3 bytes.
Mix columnsSearch on YouTube
Mix columns processes each column separately. Each byte is replaced by a value that depends on all 4 bytes in the column. The step is a matrix multiplication in with the prime (irreducible) polynomial:
Each column of the state is multiplied by this fixed matrix:
Deck example (input is the ShiftRows output above):
| Input | Output | |||||||
|---|---|---|---|---|---|---|---|---|
| 87 | F2 | 4D | 97 | 47 | 40 | A3 | 4C | |
| 6E | 4C | 90 | EC | 37 | D4 | 70 | 9F | |
| 46 | E7 | 4A | C3 | 94 | E4 | 3A | 42 | |
| A6 | 8C | D8 | 95 | ED | A5 | A6 | BC |
The four equations for the first column:
More facts about mix columns:
- You can write each column as 4 equations, one for each new byte in the column.
- Decryption uses the inverse matrix. Its coefficients are larger, so decryption is a little harder.
- An alternative view: each column is a 4-term polynomial with coefficients in . The polynomials are multiplied modulo .
- The coefficients are based on a linear code with maximal distance between codewords.
Add round keySearch on YouTube
Add round key XORs the state with 128 bits of the round key:
- The step works column by column: the four state columns XOR with the four key words . In effect, it is a series of byte operations.
- The inverse for decryption is the same step, because XOR is its own inverse. Decryption uses the round keys in reverse order.
- The step is designed to be as simple as possible. It is a form of Vernam cipher on the expanded key.
- The other stages give the complexity and security.
AES arithmeticSearch on YouTube
Finite fieldsSearch on YouTube
- A finite field is a field with a finite number of elements. It is also known as a Galois field.
- The number of elements is always a power of a prime number, written .
- is the set of integers with arithmetic modulo the prime .
- Addition, subtraction, multiplication and division stay inside the field .
- Example: is mod 2 arithmetic.
The deck also writes "GF(8) = mod 8 arithmetic". This is not correct: 8 is not a prime, and mod 8 arithmetic is not a field (2 has no inverse mod 8). uses polynomials over modulo an irreducible polynomial of degree 3.
An irreducible polynomial cannot be factored into polynomials of lower degree over the same field. Thus it is not a product of two non-constant polynomials of smaller degree.
Multiplication in GF(2^8)Search on YouTube
AES uses arithmetic in . Each element is an 8-bit byte. Each bit of the byte is a coefficient of a polynomial of degree 7 or less. Multiplication is done modulo the irreducible polynomial:
Example 1. :
Example 2. :
The equal terms cancel in pairs (, , ), so the result is:
The degree is less than 8, so no reduction is necessary.
Reduce only when the product has an term. A multiplication by is a left shift by one bit. If the bit that shifts out is 1, XOR the result with (the low 8 bits of ). If you forget this step, the MixColumns result is wrong.
Galois field rolesSearch on YouTube
The deck lists where AES uses Galois field operations:
- SubBytes (substitution layer): the S-box uses algebraic operations in the Galois field. This step adds non-linearity and confusion.
- ShiftRows (permutation layer): a change in one byte affects many bytes in the output. This increases diffusion.
- MixColumns (permutation layer): this step increases diffusion and makes the relation between bytes more complex.
- Key expansion: the key schedule uses modular addition, bitwise XOR and circular shifts on the key bytes.
- Round key addition: in each round, the round key XORs with the data block. XOR is a bitwise operation in the Galois field.
The Galois field operations give AES the confusion and diffusion that a strong cipher needs.
Key expansionSearch on YouTube
Expansion stepsSearch on YouTube
Key expansion takes the 128-bit (16-byte) key and expands it into an array of 44/52/60 32-bit words (for 128/192/256-bit keys).
- Copy the key into the first 4 words .
- Make each new word from the previous word and the word 4 places back.
- In 3 of 4 cases, XOR these two words.
- For the first word in each group of 4, apply the function to the previous word first. Then XOR the result with the word 4 places back.
The function does a 1-byte rotate (RotWord), an S-box substitution of each byte (SubWord) and an XOR with a round constant (Rcon).
For a 128-bit key:
Round constantsSearch on YouTube
The round constant is the 32-bit word . The byte doubles in each time: and , so .
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | |
|---|---|---|---|---|---|---|---|---|---|---|
| 01 | 02 | 04 | 08 | 10 | 20 | 40 | 80 | 1B | 36 |
After , the doubling needs a reduction: . AES-128 uses up to , because it needs 11 round keys.
Key expansion rationaleSearch on YouTube
The key expansion is designed to resist known attacks. The design criteria:
- knowledge of part of the key is not sufficient to find many more key bits
- an invertible transformation
- fast on a wide range of CPUs
- round constants to break symmetry
- diffusion of key bits into the round keys
- enough non-linearity to hinder analysis
- simplicity of description
Worked exampleSearch on YouTube
The deck expands the key . The first step makes :
| Step | Value |
|---|---|
| af 7f 67 98 | |
| 7f 67 98 af | |
| d2 85 46 79 | |
| 01 00 00 00 | |
| SubWord Rcon | d3 85 46 79 |
| dc 90 37 b0 | |
| 9b 49 df e9 | |
| 97 fe 72 3f | |
| 38 81 15 a7 |
The last round key is = b48ef352, ba98134e, 7f4d5920, 86261876.
With this key, the plaintext encrypts to the ciphertext .
Decryption and analysisSearch on YouTube
AES decryptionSearch on YouTube
AES decryption is not identical to encryption, because the steps are done in reverse order. But you can define an equivalent inverse cipher with the same step order as encryption:
- It uses the inverse of each step.
- It uses a different key schedule.
This works because the result does not change when you:
- swap byte substitution and shift rows
- swap mix columns and add (tweaked) round key
In the deck figure, the decryption rounds are: inverse sub bytes, inverse shift rows, inverse mix columns, add round key. The middle round keys ( to ) first go through inverse mix columns. Decryption starts with and ends with .
Avalanche exampleSearch on YouTube
The deck encrypts two plaintexts that differ in 1 bit, with the same key as above:
| Round | Number of bits that differ |
|---|---|
| Plaintext | 1 |
| 0 | 1 |
| 1 | 20 |
| 2 | 58 |
| 3 | 59 |
| 4 | 61 |
| 5 | 68 |
| 6 | 64 |
| 7 | 67 |
| 8 | 65 |
| 9 | 61 |
| 10 | 58 |
After round 2, about half of the 128 bits differ. The two ciphertexts are and .
Confusion and diffusionSearch on YouTube
- Confusion makes the relation between the key and the ciphertext as complex as possible. The ciphertext gives no clue about the plaintext.
- The goal of confusion: it must be very hard to find the key, even with many plaintext-ciphertext pairs from the same key. Each ciphertext bit must depend on the whole key. A change of one key bit must change the ciphertext completely.
- Diffusion spreads the statistical structure of the plaintext over the statistics of the ciphertext. If one plaintext bit changes, the ciphertext must change completely, in a pseudorandom way.
| Basis for comparison | Confusion | Diffusion |
|---|---|---|
| Basic | Used to make vague ciphertexts | Used to make obscure plaintexts |
| Seeks to | Make the relation between ciphertext statistics and the key as complex as possible | Make the statistical relation between plaintext and ciphertext as complex as possible |
| Achieved through | Substitution algorithm | Transposition algorithm |
| Used by | Block cipher only | Stream cipher and block cipher |
| Result in | Increased vagueness | Increased redundancy |
ImplementationSearch on YouTube
8-bit CPUSearch on YouTube
AES is efficient on an 8-bit CPU:
- Byte substitution works on bytes with a table of 256 entries.
- Shift rows is a simple byte shift.
- Add round key works with byte XORs.
- Mix columns needs a matrix multiplication in on byte values. You can simplify it to table lookups and byte XORs.
32-bit CPUSearch on YouTube
AES is also efficient on a 32-bit CPU:
- Redefine the steps to use 32-bit words.
- Precompute 4 tables of 256 words.
- Then each column in each round needs 4 table lookups and 4 XORs.
- The tables need 4 KB of storage.
The designers believe that this efficient implementation was a key factor in the selection of Rijndael as AES.
Quick self-testSearch on YouTube
Why did NIST need a replacement for DES?
There are theoretical attacks on DES, and exhaustive key search attacks were demonstrated. Triple DES fixed the key length, but it is slow and has small blocks.
How many rounds does AES use for each key size, and what is special about the last round?
10/12/14 rounds for 128/192/256-bit keys (9/11/13 full rounds and one last round). There is an initial AddRoundKey before round 1. The last round has only 3 stages: it has no MixColumns.
Which stage uses the key material?
Only AddRoundKey. It is a Vernam-style XOR, and XOR is its own inverse.
SubBytes: what does become, and how is the S-box built?
Row 9, column 5 gives . The table is a permutation of all 256 byte values. It is built with a defined transformation of values in .
Calculate .
The shift gives . XOR with gives .
What does ShiftRows do to each row?
Row 0 does not change. Row 1 rotates 1 byte left, row 2 rotates 2 bytes, and row 3 rotates 3 bytes. Decryption rotates to the right.
Why is MixColumns harder for decryption?
Decryption needs the inverse matrix over , and its coefficients are larger.
Key expansion: how many words for each key size, and how is the first word of each group made?
44/52/60 words for 128/192/256-bit keys. The first word of each group of 4 is . The function does RotWord, SubWord and an XOR with the round constant.
Give the difference between confusion and diffusion.
Confusion makes the relation between the key and the ciphertext complex, and substitution gives it. Diffusion spreads the plaintext statistics over the ciphertext, and transposition gives it.
How does the 32-bit implementation reduce the work in each round?
It uses 4 precomputed tables of 256 words. Each column in each round then needs 4 lookups and 4 XORs. The tables need 4 KB.
- AES (Rijndael, FIPS 197) uses a 128-bit block and 128/192/256-bit keys with 10/12/14 rounds. The state is 4 columns of 4 bytes.
- Each full round has four stages. SubBytes uses one S-box built in . ShiftRows rotates the rows by 0, 1, 2, 3 bytes.
- MixColumns multiplies each column by a matrix in modulo . AddRoundKey is an XOR, and it is the only stage that uses the key.
- An initial AddRoundKey comes before round 1. The last round has no MixColumns.
- Key expansion gives 44/52/60 words. The first word in each group of 4 uses RotWord, SubWord and Rcon.
- Decryption uses the inverse stages and the round keys in reverse order. An equivalent inverse cipher keeps the encryption step order.
- Fast 8-bit and 32-bit implementations were a key factor in the selection of Rijndael.