Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

BabyBlock is small enough to do by hand, but it’s a Feistel cipher, and the Advanced Encryption Standard (AES) isn’t. AES is too big to do by hand. Simplified AES (S-AES) sits in between. It was designed for students in 2003 by Mohammad Musa, Edward Schaefer and Stephen Wedig, and it uses the same four steps as AES on a much smaller block.

BabyBlockS-AESAES
Block size4 bits16 bits128 bits
Key size4 bits16 bits128 bits (or 192 or 256)
Rounds3210 (or 12 or 14)
StructureFeistelsubstitute and mixsubstitute and mix
By hand?easyyes, carefullynot really

S-AES is built from four operations:

  1. Add round key: XOR the block with a round key.

  2. Nibble substitution: put each 4-bit piece through an S-box, like BabyBlock.

  3. Shift rows: move the pieces around.

  4. Mix columns: combine the pieces with a special kind of arithmetic.

Nibbles, Hexadecimal and the State

A nibble is 4 bits, which is exactly one hexadecimal digit (see positional number systems).

Hex0123456789ABCDEF
Bits0000000100100011010001010110011110001001101010111100110111101111

A 16-bit block is four nibbles, or four hex digits. For example, 0011 0110 0111 1001 is 3679.

S-AES arranges the four nibbles in a 2×2 grid called the state. The grid is filled down the columns: the 1st nibble goes top left, the 2nd bottom left, the 3rd top right and the 4th bottom right. So 3679 becomes

[3769]\begin{bmatrix} 3 & 7 \\ 6 & 9 \end{bmatrix}

with 3, 6 in the left column and 7, 9 in the right column. Every step changes this grid, and at the end you read it back out the same way, down the columns.

In Python, we’ll keep the state as a list of 4 nibbles in that order, [s0, s1, s2, s3], where s0 and s1 are the left column and s2 and s3 are the right column.

3 7
6 9

In Python, 0x3679 is the number written in hex, and the format code X prints a number in hex.

Our Example

This page follows one encryption all the way through.

  • plaintext 3679 = 0011 0110 0111 1001

  • key AD76 = 1010 1101 0111 0110

The key is used three times, as the round keys K0K_0, K1K_1 and K2K_2. All three come from the one 16-bit key. For this key they are K0K_0 = AD76 (the key itself), K1K_1 = A8DE and K2K_2 = 66B8. The section on the key schedule shows how to make them.

Here is the whole plan. Round 2 has no mix columns step.

Step 1: Add Round Key

XOR each nibble of the state with the matching nibble of the round key. This is the only step that uses the key.

[3769]⊕[A7D6]=[90BF]\begin{bmatrix} 3 & 7 \\ 6 & 9 \end{bmatrix} \oplus \begin{bmatrix} A & 7 \\ D & 6 \end{bmatrix} = \begin{bmatrix} 9 & 0 \\ B & F \end{bmatrix}

For example, the top left nibble is 0011 ⊕ 1010 = 1001 = 9. The new state is 9B0F.

9 0
B F

Step 2: Nibble Substitution

Replace every nibble with its entry in the S-box. Each nibble changes on its own, and nothing moves.

in0123456789ABCDEF
out94ABD1856203CEF7

Every output appears exactly once, so this step can be undone. For our state, S(9) = 2, S(B) = 3, S(0) = 9 and S(F) = 7, so 9B0F becomes 2397.

2 9
3 7

Why an S-box?

The LFSR ciphers fell to known-plaintext attacks because everything in them was linear: just XORs, so a system of equations could solve them (see LFSR security). The S-box isn’t linear. A linear function would satisfy S(a⊕b)=S(a)⊕S(b)S(a \oplus b) = S(a) \oplus S(b), and the S-box doesn’t.

S(1 ⊕ 2) = S(3) = B
S(1) ⊕ S(2) = 4 ⊕ A = E

The S-box is the confusion step from the introduction to block ciphers.

Step 3: Shift Rows

The top row stays put, and the bottom row swaps its two nibbles. After this step, each column holds nibbles that started in different columns.

[2937]⟶[2973]\begin{bmatrix} 2 & 9 \\ 3 & 7 \end{bmatrix} \longrightarrow \begin{bmatrix} 2 & 9 \\ 7 & 3 \end{bmatrix}

The state 2397 becomes 2793.

2 9
7 3

Step 4: Mix Columns

Mix columns combines the two nibbles in each column, so that each new nibble depends on both of the old ones:

new top=top+4⋅bottomnew bottom=4⋅top+bottom\begin{aligned} \text{new top} &= \text{top} + 4 \cdot \text{bottom} \\ \text{new bottom} &= 4 \cdot \text{top} + \text{bottom} \end{aligned}

But what do ++ and 4⋅4 \cdot mean for nibbles? Ordinary arithmetic doesn’t work, so we need a new kind.

The problem with ordinary multiplication

Ordinary multiplication gives answers that are too big: 4×9=364 \times 9 = 36 = 100100, which needs 6 bits. A nibble only holds 4.

You might try wrapping around, the way the Caesar cipher works mod 26. Here is 4x mod 164x \bmod 16 for every nibble xx.

x           0 1 2 3 4 5 6 7 8 9 A B C D E F
4x mod 16   0 4 8 C 0 4 8 C 0 4 8 C 0 4 8 C

All 16 inputs land on just 4 outputs. For example, 4⋅1=4⋅5=44 \cdot 1 = 4 \cdot 5 = 4, so the receiver couldn’t tell which one was sent and couldn’t decrypt. We need a multiplication that can always be undone.

Treat a nibble as a polynomial

Write each nibble as a polynomial whose coefficients are its bits, just like the characteristic polynomials of LFSRs:

bitsx3x^3x2x^2xx1
10111011

So 1011 =x3+x+1= x^3 + x + 1, 0100 =x2= x^2, and 1001 =x3+1= x^3 + 1. Every coefficient is 0 or 1.

Adding is XOR. Add matching coefficients mod 2. Since 1+1=01 + 1 = 0, matching terms cancel:

(x3+x)+(x+1)=x3+1,in bits: 1010⊕0011=1001(x^3 + x) + (x + 1) = x^3 + 1, \qquad \text{in bits: } 1010 \oplus 0011 = 1001

Multiplying by xx is a shift. Multiplying by xx raises every power by one, so the bits move one place to the left: 0101 =x2+1= x^2 + 1 times xx is x3+xx^3 + x = 1010. But sometimes a bit falls off the left end. For example, 1001 =x3+1= x^3 + 1 times xx is x4+xx^4 + x, and x4x^4 doesn’t fit in 4 bits.

The wrap-around rule: x4=x+1x^4 = x + 1

When we work mod 26, the number 26 counts as 0. Here, the polynomial x4+x+1x^4 + x + 1 counts as 0. So x4+x+1=0x^4 + x + 1 = 0, which means

x4=x+1x^4 = x + 1

(Subtracting is the same as adding when coefficients are mod 2.) In bits: if a 1 spills off the left end, drop it and XOR 0011. For example, 1001 times xx is 10010. A 1 spilled, so drop it to get 0010, then XOR 0011 to get 0001. Check with polynomials: x4+x=(x+1)+x=1x^4 + x = (x + 1) + x = 1.

The polynomial x4+x+1x^4 + x + 1 is a maximum-period 4-bit LFSR polynomial. That’s no accident: it can’t be factored, which is what makes every multiplication undoable.

1001 times x = 0001

Multiplying by 4

Since 4 = 0100 =x2= x^2, multiplying by 4 means multiplying by xx twice. For 4⋅94 \cdot 9:

  • 1001 → shift → 10010 → a 1 spilled, so drop it and XOR 0011 → 0001

  • 0001 → shift → 0010 (no spill)

So 4⋅9=24 \cdot 9 = 2. Check with polynomials: x2(x3+1)=x5+x2x^2(x^3 + 1) = x^5 + x^2, and x5=x⋅x4=x(x+1)=x2+xx^5 = x \cdot x^4 = x(x + 1) = x^2 + x, so the product is x2+x+x2=xx^2 + x + x^2 = x = 0010.

Here’s the whole times-4 table, next to the mod 16 version that failed.

x           0 1 2 3 4 5 6 7 8 9 A B C D E F
4x mod 16   0 4 8 C 0 4 8 C 0 4 8 C 0 4 8 C
4 · x (new) 0 4 8 C 3 7 B F 6 2 E A 5 1 D 9
different outputs: 16

The new row hits all 16 values, each exactly once, so multiplying by 4 can always be undone.

You only need two rules to use it: add with XOR, and replace x4x^4 with x+1x + 1.

Mix columns, worked out

For each column, with ⊕\oplus for addition and the times-4 table for multiplication:

new top=top⊕4⋅bottomnew bottom=4⋅top⊕bottom\begin{aligned} \text{new top} &= \text{top} \oplus 4 \cdot \text{bottom} \\ \text{new bottom} &= 4 \cdot \text{top} \oplus \text{bottom} \end{aligned}

If you’ve seen matrices, this is the matrix [1441]\begin{bmatrix} 1 & 4 \\ 4 & 1 \end{bmatrix} times the column.

Our state is 2793, with columns 2, 7 and 9, 3.

  • Left column: new top =2⊕4⋅7=2⊕F= 2 \oplus 4 \cdot 7 = 2 \oplus F = 0010 ⊕ 1111 = 1101 = D. New bottom =4⋅2⊕7=8⊕7= 4 \cdot 2 \oplus 7 = 8 \oplus 7 = 1000 ⊕ 0111 = 1111 = F.

  • Right column: new top =9⊕4⋅3=9⊕C= 9 \oplus 4 \cdot 3 = 9 \oplus C = 1001 ⊕ 1100 = 0101 = 5. New bottom =4⋅9⊕3=2⊕3= 4 \cdot 9 \oplus 3 = 2 \oplus 3 = 0010 ⊕ 0011 = 0001 = 1.

After mix columns the state is DF51.

D 5
F 1

Finishing the Example

Round 1 ends by adding round key K1K_1 = A8DE. Round 2 substitutes, shifts rows and adds K2K_2 = 66B8, with no mix columns.

after round 1:      778F
round 2 substitute: 5567
round 2 shift rows: 5765
ciphertext:         31DD

Why skip mix columns at the end?

Mix columns doesn’t use the key. It’s a fixed, public recipe. If the last round ended with mix columns, anyone could undo it from the ciphertext, without the key. It would cost work and add no security. Real AES also skips mix columns in its last round.

The Key Schedule

The round keys come from the key one byte (8 bits, two nibbles) at a time. Split the key into two bytes, w0w_0 and w1w_1. Together they are K0K_0. Then

w2=w0⊕g(w1)w3=w2⊕w1w4=w2⊕g(w3)w5=w4⊕w3\begin{aligned} w_2 &= w_0 \oplus g(w_1) & w_3 &= w_2 \oplus w_1 \\ w_4 &= w_2 \oplus g(w_3) & w_5 &= w_4 \oplus w_3 \end{aligned}

so K1=w2w3K_1 = w_2 w_3 and K2=w4w5K_2 = w_4 w_5. Everything depends on the function gg, which does three things to a byte:

The S-box is there for the same reason as before. Without it, the round keys would just be XORs of key bits, which is linear, like an LFSR.

The round constants are powers of xx, made with the same rule as mix columns. Round 1 uses x3x^3 = 1000, and round 2 uses x4=x+1x^4 = x + 1 = 0011. Each one is the last one times xx: 1000 → shift → 10000 → drop the 1 and XOR 0011 → 0011. The right nibble of each round constant is 0000. (AES does the same thing with bytes: 01, 02, 04, 08, ....)

For the key AD76, w0w_0 = AD and w1w_1 = 76.

StepBitsHex
Start: w1w_10111 011076
Rotate (swap nibbles)0110 011167
Substitute each nibble: S(6) = 8, S(7) = 51000 010185
XOR round constant1000 000080
g(w1)g(w_1)0000 010105
w2=w0⊕g(w1)w_2 = w_0 \oplus g(w_1)AD ⊕ 05A8
w3=w2⊕w1w_3 = w_2 \oplus w_1A8 ⊕ 76DE

So K1K_1 = A8DE. The same steps on w3w_3 = DE with the round 2 constant give K2K_2 = 66B8.

['AD76', 'A8DE', '66B8']

The Whole Cipher

Putting it all together:

plaintext                   3679   00110110 01111001
add round key K0            9B0F   10011011 00001111
round 1: substitute         2397   00100011 10010111
round 1: shift rows         2793   00100111 10010011
round 1: mix columns        DF51   11011111 01010001
round 1: add round key K1   778F   01110111 10001111
round 2: substitute         5567   01010101 01100111
round 2: shift rows         5765   01010111 01100101
round 2: add round key K2   31DD   00110001 11011101

The ciphertext is 31DD.

Decrypting S-AES

To decrypt, run every step backwards, using the round keys in reverse order.

  • Add round key: XOR the same round key again. It cancels.

  • Shift rows: swap the bottom row again.

  • Nibble substitution: use the inverse S-box (read the table from the bottom row up).

  • Mix columns: use the same recipe with 9 and 2 in place of 1 and 4. The matrix [9229]\begin{bmatrix} 9 & 2 \\ 2 & 9 \end{bmatrix} undoes [1441]\begin{bmatrix} 1 & 4 \\ 4 & 1 \end{bmatrix}.

BabyBlock, a Feistel cipher, decrypted by running its own steps again. S-AES needs an undo for every step.

inverse S-box: A 5 9 B 1 7 8 F 6 0 2 3 C 4 D E
[9 2; 2 9] undoes mix columns: True
3679

How Far Does One Change Spread?

Flip one bit of the plaintext, encrypt again with the same key, and compare the states after each step. The code below does this for 2,000 random plaintexts and keys and all 16 bit positions.

after        start    +K0    sub  shift    mix    +K1    sub  shift    +K2
bits changed   1.0    1.0    2.0    2.0    4.0    4.0    4.2    4.2    4.2
nibbles that differ after round 1: [(0, 1), (2, 3)]
number of ciphertext nibbles that differ: {2}

Follow the change through the steps.

  1. Adding a round key and substituting keep the change inside one nibble. The S-box can change more bits of that nibble, but nothing leaves it.

  2. Shift rows moves that nibble but doesn’t spread it.

  3. Mix columns spreads it to both nibbles of its column. After round 1, exactly 2 nibbles differ, always a whole column: nibbles 0 and 1 (the left column) or nibbles 2 and 3 (the right column).

  4. In round 2, substitution keeps the change in those 2 nibbles, and shift rows moves the bottom one into the other column.

  5. There’s no mix columns in round 2, so nothing spreads it further.

So one flipped plaintext bit always changes exactly 2 of the 4 ciphertext nibbles: the top of one column and the bottom of the other, on a diagonal. That’s about 4 of the 16 bits, short of the goal of about half (8 bits).

S-AES falls short because its two rounds contain only one mix columns step. If round 2 also mixed its columns, the two changed nibbles, now in different columns, would each spread through their own column, and all 4 nibbles would change. You can check that with the experiment below.

ciphertext nibbles that differ: {4}

Real AES has a bigger state, so a change needs two full rounds to reach every byte. After that it uses many more rounds as a safety margin. The next page shows how.

From S-AES to AES

AES uses the same four steps on a bigger state.

S-AESAES-128
Block16 bits128 bits
State2×2 grid of nibbles4×4 grid of bytes
S-box16 entries256 entries
Shift rowsswap the bottom rowshift the rows by 0, 1, 2 and 3 places
Mix columns1’s and 4’s, on nibbles1’s, 2’s and 3’s, on bytes
Rounds210
Last roundno mix columnsno mix columns

If you can do S-AES, you understand AES.

Source: M. Musa, E. Schaefer and S. Wedig, “A Simplified AES Algorithm and Its Linear and Differential Cryptanalyses,” Cryptologia 27(2), 2003.