Block Ciphers¶
Every cipher in the book so far has worked on one small piece of the message at a time. Caesar, affine and Vigenère change one letter at a time. The LFSR ciphers change one bit at a time. Change one letter or one bit of the plaintext, and exactly one letter or bit of the ciphertext changes.
Most encryption used today works differently. A block cipher takes a whole group of bits, called a block, and scrambles the block as a unit. Change one bit of the plaintext, and about half of the bits in the ciphertext block change. This chapter shows how block ciphers do that, from a 4-bit toy you can run by hand up to the Advanced Encryption Standard (AES), which protects most of the internet.
This page covers block ciphers in general: confusion, diffusion, rounds and Feistel networks.
In This Chapter¶
A World War I cipher that splits each letter in two.
A tiny Feistel cipher, the real cipher it imitates, and how to encrypt messages longer than one block.
A 16-bit version of AES small enough to do by hand.
The 128-bit block cipher that protects most of the internet.
Stream Ciphers and Block Ciphers¶
All of the ciphers in this chapter are symmetric: the same key encrypts and decrypts. Symmetric ciphers come in two families.
In this course we also call the classical ciphers that work one letter at a time, like Caesar and Vigenère, stream ciphers. That’s a course convention. Many books save the name “stream cipher” for ciphers that combine the plaintext with a keystream of bits.
| Cipher | Block size |
|---|---|
| BabyBlock (this chapter) | 4 bits |
| Simplified AES (this chapter) | 16 bits |
| DES (1977) | 64 bits |
| AES (2001) | 128 bits |
A message rarely fills a whole number of blocks. The sender adds extra bits, called padding, to fill the last block, and the padding must be easy for the receiver to recognize and remove. One common rule for byte-based ciphers, called PKCS #7 padding, adds bytes that each have the value . For example, a message that is 3 bytes short of a full block gets three bytes of 03 added to the end.
Confusion and Diffusion¶
In 1949 Claude Shannon published “Communication Theory of Secrecy Systems,” one of the first mathematical studies of cryptography. He named two properties that a strong cipher needs.
Substitution provides confusion. You’ve used it since the Caesar cipher.
None of the ciphers you’ve seen so far do this.
The goal for diffusion is this: change one symbol of the plaintext, and about half of the ciphertext symbols should change. A strong cipher needs both confusion and diffusion.
A Vigenère cipher has no diffusion¶
Encrypt TOLL with the keyword MATH, then change the first letter to P and encrypt again.
print(vigenere('TOLL', 'MATH'))
print(vigenere('POLL', 'MATH'))FOES
BOES
One plaintext letter changed, and only the matching ciphertext letter changed. Each ciphertext letter has a hidden relationship with its plaintext letter, so there’s confusion, but there’s no diffusion.
An XOR stream cipher has no diffusion¶
The same thing happens with bits. Encrypt 01010000 with the keystream 11011100, then flip the first plaintext bit and encrypt again.
keystream = '11011100'
ciphertext1 = xor('01010000', keystream)
ciphertext2 = xor('11010000', keystream)
print(ciphertext1)
print(ciphertext2)
bits_changed = sum(1 for a, b in zip(ciphertext1, ciphertext2) if a != b)
print('ciphertext bits changed:', bits_changed, 'of 8')10001100
00001100
ciphertext bits changed: 1 of 8
A block cipher has diffusion¶
Here is the same experiment with an 8-bit block cipher. The plaintext 00101011 encrypts to 10111001. Changing the third bit gives the plaintext 00001011, which encrypts to 01101100.
ciphertext1 = '10111001'
ciphertext2 = '01101100'
bits_changed = sum(1 for a, b in zip(ciphertext1, ciphertext2) if a != b)
print('ciphertext bits changed:', bits_changed, 'of 8')ciphertext bits changed: 5 of 8
One plaintext bit changed, and 5 of the 8 ciphertext bits changed. That’s close to half, which is the goal. When a small change in the input causes a large change in the output, people often call it the avalanche effect.
Why Diffusion Matters¶
Without diffusion, an attacker who knows what a message looks like can change it without knowing the key.
Suppose a bank sends the message PAY $100 with an XOR stream cipher. The keystream below comes from the 8-bit LFSR lfsr8 from the LFSR page. The attacker sees only the ciphertext. They know the message format, though, so they XOR the ciphertext with the difference between PAY $100 and PAY $900.
plaintext = text_to_binary('PAY $100')
keystream = format(lfsr8(0b10110101, 64), '064b')
ciphertext = xor(plaintext, keystream)
# The attacker never sees the keystream. They only XOR in the difference between the two messages.
difference = xor(text_to_binary('PAY $100'), text_to_binary('PAY $900'))
print('difference:', difference)
tampered = xor(ciphertext, difference)
print('the receiver decrypts:', binary_to_text(xor(tampered, keystream)))difference: 0000000000000000000000000000000000000000000010000000000000000000
the receiver decrypts: PAY $900
The characters 1 (00110001) and 9 (00111001) differ in one bit, so the attacker flipped a single ciphertext bit. The receiver decrypts PAY $900, and nothing looks wrong.
A block cipher with good diffusion stops this kind of precise change. Flipping one ciphertext bit changes about half the bits of the decrypted block, so the attacker can’t predict what the receiver will see. On the next pages you’ll measure this: BabyBlock changes about 2 of its 4 bits on average, and AES changes about 64 of its 128 bits. Diffusion alone doesn’t prove that a message wasn’t changed, though. The last section of the BabyBlock page describes modes that add a tag to catch tampering.
The Structure of a Block Cipher¶
Most block ciphers turn a block of plaintext into a block of ciphertext of the same size by repeating a step called the round function several times. Each repetition is a round. Each round usually has:
a substitution stage, such as an S-box (a lookup table), which provides confusion, and
a permutation or mixing stage, which moves or combines bits and provides diffusion.
Each round also uses a round key. The round keys are all made from the one secret key by a rule called the key schedule, so that every round is different.
There are two common designs. A substitution-permutation network puts the whole block through every step in every round. Simplified AES and AES work this way. A Feistel network changes only half of the block in each round. BabyBlock and DES work this way.
Feistel Networks¶
The Feistel network is named after Horst Feistel, who designed the Lucifer cipher at IBM in the early 1970s. Lucifer became the starting point for DES. Many other ciphers use the same plan, including the Soviet GOST cipher, Blowfish and Twofish.
Here is one round of a Feistel network.
The diagram shows the same round. The old right half is copied across to become the new left half, and the XOR result becomes the new right half. That’s the swap.
Then repeat for the next round with a new round key. For rounds with round keys , round is
The function is where each cipher puts its confusion and diffusion, and every cipher has its own .
Decryption runs the same network backwards¶
Look at what this needs. To undo a round you compute forwards again, on a value you already have. You never have to undo . So doesn’t need to be invertible. It can be as messy as the designer likes, and the cipher can still be decrypted. That freedom is the big advantage of a Feistel network.
Most Feistel ciphers, including DES and BabyBlock, leave out the swap at the end of the last round. Then decrypting is exactly the same algorithm as encrypting, run with the round keys in reverse order.
A Feistel network in Python¶
The function below runs a Feistel network on two halves stored as integers. Each round XORs of the right half into the left half and swaps. At the end it undoes the last swap, so that the same function also decrypts.
def feistel(left, right, round_keys, f):
# Run a Feistel network. Each round: new left = old right, new right = old left XOR f(old right, key).
for key in round_keys:
left, right = right, left ^ f(right, key)
# Leave out the swap at the end of the last round
return right, leftTo show that doesn’t need to be invertible, here’s a round function that is very far from invertible. It works on 4-bit halves (the numbers 0 to 15), and it only ever outputs 0, 1, 4 or 9, because those are the only squares mod 16.
def f(half, key):
return (half + key) ** 2 % 16
[f(half, 3) for half in range(16)][9, 0, 9, 4, 1, 0, 1, 4, 9, 0, 9, 4, 1, 0, 1, 4]Many different inputs give the same output, so there’s no way to undo . The network still decrypts. Encrypt the 8-bit block 10110110 with three round keys, then run the same function on the ciphertext with the round keys reversed.
round_keys = [3, 10, 7]
left, right = 0b1011, 0b0110
c_left, c_right = feistel(left, right, round_keys, f)
print('ciphertext:', format(c_left, '04b'), format(c_right, '04b'))
p_left, p_right = feistel(c_left, c_right, round_keys[::-1], f)
print('decrypted: ', format(p_left, '04b'), format(p_right, '04b'))ciphertext: 0011 0110
decrypted: 1011 0110
The decrypted block is the original plaintext, even though itself can’t be undone.
Exercise for the Reader¶
Solution to Exercise 1
, which is 0001. So = 0010 and = 1100 ⊕ 0001 = 1101. The output is 0010 1101.
To undo the round, = 0010 and = 1101 ⊕ 0001 = 1100. That’s the block you started with.
What’s Next¶
The BabyBlock cipher is a 4-bit Feistel cipher with an S-box as its function . That page’s first two sections, How BabyBlock Works and Decrypting BabyBlock, only use ideas from this page, so you can read them right away.
Before BabyBlock, the next page looks at the ADFGVX cipher from World War I. It isn’t a block cipher, but it was one of the first ciphers to spread each letter across the ciphertext.