BabyBlock, DES and Modes of Operation¶
BabyBlock is a block cipher made for learning. It has the same structure as a real Feistel cipher, but it’s small enough to run by hand. It’s not secure!
It encrypts 4-bit blocks, so a message is first turned into binary (8-bit ASCII for text) and cut into blocks of 4 bits.
The key is a 4-bit number.
Each block goes through 3 rounds of encryption, and each round uses a different round key.
This page shows how BabyBlock works, then looks at DES, the real cipher it imitates, and at how to encrypt a message that is longer than one block.
How BabyBlock Works¶
BabyBlock is a Feistel network. Its function is an S-box, a lookup table. The S-box takes 6 bits in (the 4-bit round key and the right 2 bits of the block) and gives 2 bits out, so it’s sometimes called a 6-bit S-box.
To use the table, find the row for the round key and the column for the right 2 bits of the block.
| 10 | 01 | 00 | 11 | |
|---|---|---|---|---|
| 0000 | 10 | 10 | 00 | 11 |
| 0001 | 00 | 11 | 10 | 00 |
| 0010 | 00 | 10 | 01 | 00 |
| 0011 | 01 | 00 | 11 | 11 |
| 0100 | 11 | 00 | 10 | 01 |
| 0101 | 10 | 11 | 01 | 10 |
| 0110 | 11 | 01 | 11 | 10 |
| 0111 | 10 | 01 | 00 | 01 |
| 1000 | 00 | 01 | 11 | 10 |
| 1001 | 01 | 00 | 01 | 11 |
| 1010 | 11 | 11 | 00 | 00 |
| 1011 | 11 | 10 | 01 | 01 |
| 1100 | 01 | 11 | 10 | 10 |
| 1101 | 00 | 01 | 11 | 00 |
| 1110 | 10 | 00 | 00 | 01 |
| 1111 | 01 | 10 | 10 | 11 |
One round, step by step¶
Each round takes a 4-bit input block and a 4-bit round key.
Example: encrypting 0010 with the key 0100¶
Round 1. The input block is 0010, so = 00 and = 10. The round key is 0100. The S-box row 0100, column 10 gives 11. Then = 00 ⊕ 11 = 11, so the muddled block is 1110. Swapping the halves gives 1011, the input for round 2.
Round 2. The round key is 0100 rotated, which is 1000. The input 1011 has = 10 and = 11. The S-box row 1000, column 11 gives 10. Then 10 ⊕ 10 = 00, so the muddled block is 0011, and swapping gives 1100.
Round 3. The round key is 1000 rotated, which is 0001. The input 1100 has = 11 and = 00. The S-box row 0001, column 00 gives 10. Then 11 ⊕ 10 = 01, so the muddled block is 0100. This is the last round, so there’s no swap.
| Round | Input block | Round key | L | R | S-box output | L ⊕ S | Muddled | Output block |
|---|---|---|---|---|---|---|---|---|
| 1 | 0010 | 0100 | 00 | 10 | 11 | 11 | 1110 | 1011 |
| 2 | 1011 | 1000 | 10 | 11 | 10 | 00 | 0011 | 1100 |
| 3 | 1100 | 0001 | 11 | 00 | 10 | 01 | 0100 | 0100 |
The ciphertext block is 0100.
Decrypting BabyBlock¶
BabyBlock decrypts with exactly the same steps, using the round keys in reverse order. For the key 0100, the round keys were 0100, 1000, 0001, so decryption uses 0001, then 1000, then 0100.
Here is the decryption of 0100.
| Round | Input block | Round key | L | R | S-box output | L ⊕ S | Muddled | Output block |
|---|---|---|---|---|---|---|---|---|
| 1 | 0100 | 0001 | 01 | 00 | 10 | 11 | 1100 | 0011 |
| 2 | 0011 | 1000 | 00 | 11 | 10 | 10 | 1011 | 1110 |
| 3 | 1110 | 0100 | 11 | 10 | 11 | 00 | 0010 | 0010 |
The result is 0010, the original plaintext block. Here’s why it works.
To unmuddle, muddle again. Muddling doesn’t change the right half, so the S-box gets the same key and the same column and gives the same output. XORing that output into the left half a second time cancels it.
To unswap, swap again.
Running the rounds with the keys in reverse order undoes the last round first, then the middle round, then the first.
All Feistel networks have this property, as shown on the previous page.
Solution to Exercise 1
| Round | Input block | Round key | L | R | S-box output | L ⊕ S | Muddled | Output block |
|---|---|---|---|---|---|---|---|---|
| 1 | 1011 | 1010 | 10 | 11 | 00 | 10 | 1011 | 1110 |
| 2 | 1110 | 0101 | 11 | 10 | 10 | 01 | 0110 | 1001 |
| 3 | 1001 | 1010 | 10 | 01 | 11 | 01 | 0101 | 0101 |
BabyBlock in Python¶
In the code below, blocks and keys are strings of 0’s and 1’s, like the binary strings in the binary operations section, and xor comes from the book’s toolkit.
# Columns are the right 2 bits of the block, in the order printed above: 10, 01, 00, 11
COLUMNS = ['10', '01', '00', '11']
# Each row: the round key, then the S-box outputs for the columns 10, 01, 00, 11
S_BOX_ROWS = '''0000 10 10 00 11
0001 00 11 10 00
0010 00 10 01 00
0011 01 00 11 11
0100 11 00 10 01
0101 10 11 01 10
0110 11 01 11 10
0111 10 01 00 01
1000 00 01 11 10
1001 01 00 01 11
1010 11 11 00 00
1011 11 10 01 01
1100 01 11 10 10
1101 00 01 11 00
1110 10 00 00 01
1111 01 10 10 11'''
# S_BOX[(round key, right 2 bits)] is the 2-bit output
S_BOX = {}
for row in S_BOX_ROWS.split('\n'):
parts = row.split()
key = parts[0]
for column, output in zip(COLUMNS, parts[1:]):
S_BOX[(key, column)] = output
def print_s_box():
print(' Last 2 bits of block')
print('key ' + ' '.join(COLUMNS))
for row in S_BOX_ROWS.split('\n'):
key = row.split()[0]
print(key + ' ' + ' '.join(S_BOX[(key, column)] for column in COLUMNS))
print_s_box() Last 2 bits of block
key 10 01 00 11
0000 10 10 00 11
0001 00 11 10 00
0010 00 10 01 00
0011 01 00 11 11
0100 11 00 10 01
0101 10 11 01 10
0110 11 01 11 10
0111 10 01 00 01
1000 00 01 11 10
1001 01 00 01 11
1010 11 11 00 00
1011 11 10 01 01
1100 01 11 10 10
1101 00 01 11 00
1110 10 00 00 01
1111 01 10 10 11
def rotate_key(key):
# Move every bit one place left; the bit that falls off the left goes on the right
return key[1:] + key[0]
def round_keys(key, rounds=3):
keys = [key]
while len(keys) < rounds:
keys.append(rotate_key(keys[-1]))
return keys
def muddle(block, key):
# XOR the S-box output into the left half; the right half passes through unchanged
left, right = block[:2], block[2:]
return xor(left, S_BOX[(key, right)]) + right
def swap(block):
return block[2:] + block[:2]
def babyblock(block, key, decrypt=False, trace=False):
keys = round_keys(key)
if decrypt:
keys = keys[::-1]
if trace:
print('round input key L R S L⊕S muddled output')
for i in range(len(keys)):
muddled = muddle(block, keys[i])
if i == len(keys) - 1:
output = muddled # the last round doesn't swap
else:
output = swap(muddled)
if trace:
left, right = block[:2], block[2:]
s = S_BOX[(keys[i], right)]
print(f' {i + 1} {block} {keys[i]} {left} {right} {s} {xor(left, s)} {muddled} {output}')
block = output
return blockHere are the examples from above. Each trace shows the same columns as the tables.
print(babyblock('0010', '0100', trace=True))
print()
print(babyblock('0100', '0100', decrypt=True, trace=True))
print()
print(babyblock('1011', '1010', trace=True))round input key L R S L⊕S muddled output
1 0010 0100 00 10 11 11 1110 1011
2 1011 1000 10 11 10 00 0011 1100
3 1100 0001 11 00 10 01 0100 0100
0100
round input key L R S L⊕S muddled output
1 0100 0001 01 00 10 11 1100 0011
2 0011 1000 00 11 10 10 1011 1110
3 1110 0100 11 10 11 00 0010 0010
0010
round input key L R S L⊕S muddled output
1 1011 1010 10 11 00 10 1011 1110
2 1110 0101 11 10 10 01 0110 1001
3 1001 1010 10 01 11 01 0101 0101
0101
BabyBlock should decrypt every block correctly with every key. There are only pairs of a block and a key, so we can check all of them.
blocks = [format(n, '04b') for n in range(16)] # '0000', '0001', ..., '1111'
all_correct = True
for key in blocks:
for block in blocks:
if babyblock(babyblock(block, key), key, decrypt=True) != block:
all_correct = False
print('decryption undoes encryption for all 256 pairs:', all_correct)decryption undoes encryption for all 256 pairs: True
How much diffusion does BabyBlock have?¶
Flip each of the 4 bits of each block, for every key, and count how many ciphertext bits change.
changes = []
for key in blocks:
for block in blocks:
ciphertext = babyblock(block, key)
for i in range(4):
flipped = block[:i] + ('1' if block[i] == '0' else '0') + block[i + 1:]
new_ciphertext = babyblock(flipped, key)
changes.append(sum(1 for a, b in zip(ciphertext, new_ciphertext) if a != b))
print('average ciphertext bits changed:', round(sum(changes) / len(changes), 2), 'of 4')average ciphertext bits changed: 2.12 of 4
On average about 2 of the 4 bits change, which is the diffusion goal of about half.
Breaking BabyBlock¶
BabyBlock’s design is sound, but it’s far too small. There are only keys, so an attacker can simply try them all. Suppose Eve knows that the plaintext block 1100 encrypted to 0010. She tries every key.
matches = [key for key in blocks if babyblock('1100', key) == '0010']
print('keys that encrypt 1100 to 0010:', matches)keys that encrypt 1100 to 0010: ['1000', '1010', '1011', '1111']
One known block narrows 16 keys down to 4. A second known block, 0000 encrypting to 1000, settles it.
matches = [key for key in blocks if babyblock('1100', key) == '0010' and babyblock('0000', key) == '1000']
print('keys that fit both blocks:', matches)keys that fit both blocks: ['1000']
There’s an even simpler attack. With only 16 possible blocks, Eve doesn’t need the key at all if she can collect the ciphertext for every plaintext block. Then she has the whole cipher as a lookup table.
The weakness is the size of the key and the block, not the Feistel structure. A real cipher needs so many keys that trying them all is impossible, and blocks so large that nobody could ever list them.
The Data Encryption Standard (DES)¶
DES is a real Feistel cipher, and BabyBlock is a tiny copy of it.
History¶
On 15 May 1973, the US National Bureau of Standards (NBS, now NIST) asked for proposals for a standard cipher to protect sensitive government data. None of the first proposals were good enough, so NBS asked again on 27 August 1974.
IBM’s submission was based on Lucifer, the cipher Horst Feistel’s team had designed.
The National Security Agency (NSA) reviewed the design and two changes caused suspicion. The S-boxes were changed, and the reasons were kept secret. And the key was shortened. Lucifer had used a 128-bit key, and DES ended up with 56 bits after NSA input. (Accounts differ on the details. One says the NSA argued for 48 bits, IBM wanted 64, and they settled on 56.)
DES was published as a federal standard, FIPS PUB 46, on 15 January 1977.
The algorithm itself was public from the start, so DES didn’t break Kerckhoffs’s principle: its security depended only on the key. What stayed secret were the reasons for the design. Around 1990, Eli Biham and Adi Shamir published an attack called differential cryptanalysis. DES held up against it surprisingly well. In 1994, Don Coppersmith of IBM explained why: IBM had known about the attack in the 1970s and had designed the S-boxes to resist it.
How DES compares with BabyBlock¶
| BabyBlock | DES | |
|---|---|---|
| Block size | 4 bits | 64 bits |
| Key | 4 bits | 56 bits (stored as 64 bits, with 8 parity bits) |
| Number of keys | ||
| Rounds | 3 | 16 |
| Structure | Feistel | Feistel |
| The function | one S-box: 4 key bits and 2 block bits in, 2 bits out | expand the 32-bit right half to 48 bits, XOR a 48-bit round key, use eight S-boxes (6 bits in, 4 bits out each), then permute the 32 bits |
| Key schedule | rotate left 1 bit | split the 56 bits into two halves of 28, rotate each half left by 1 or 2 places, then pick 48 of the bits |
DES works on 264 = 18,446,744,073,709,551,616 possible plaintext blocks. Like BabyBlock, its function’s output is XORed with the left half of the block, and its last round doesn’t swap, so decryption is the same algorithm with the round keys reversed. Unlike BabyBlock, its function permutes bits as well as substituting them.
Why DES was replaced¶
DES’s design held up well for decades. Its key didn’t. A 56-bit key was hard to search in 1977, but computers kept getting faster.
In June 1997, a volunteer project called DESCHALL broke a DES-encrypted message in a public contest by trying keys on thousands of computers.
In July 1998, the Electronic Frontier Foundation’s machine Deep Crack, which cost about $250,000, found a DES key by brute force in 56 hours.
In January 1999, Deep Crack and the volunteer network distributed.net found a key in 22 hours and 15 minutes.
In 1999 the government reaffirmed DES once more, but recommended Triple DES, which runs DES three times with two or three different keys. The Advanced Encryption Standard (AES) was published on 26 November 2001 and replaced DES as the standard on 26 May 2002. The DES standard was withdrawn on 19 May 2005.
Modes of Operation¶
A block cipher encrypts one block. A real message has many blocks.
The mode matters as much as the cipher.
Electronic Codebook (ECB)¶
In ECB mode, each block is encrypted on its own with the same key. With the key fixed, the cipher acts like a codebook: each plaintext block always turns into the same ciphertext block. Here is BabyBlock’s whole codebook for the key 1000.
key = '1000'
print('plaintext ', ' '.join(blocks))
print('ciphertext ', ' '.join(babyblock(block, key) for block in blocks))plaintext 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
ciphertext 1000 0110 0100 1011 1100 0011 1101 1111 0111 0101 1010 1001 0010 0001 1110 0000
Encrypt the message GO with the key 1000. In 8-bit ASCII, GO is 01000111 01001111, which is four 4-bit blocks.
def split_blocks(bits, size=4):
return [bits[i:i + size] for i in range(0, len(bits), size)]
def ecb_encrypt(bits, key):
return ' '.join(babyblock(block, key) for block in split_blocks(bits))
plaintext = text_to_binary('GO')
print('plaintext: ', ' '.join(split_blocks(plaintext)))
print('ECB: ', ecb_encrypt(plaintext, '1000'))plaintext: 0100 0111 0100 1111
ECB: 1100 1111 1100 0000
Plaintext blocks 1 and 3 are both 0100, so ciphertext blocks 1 and 3 are both 1100. An eavesdropper who doesn’t know the key can still see that the message repeats itself. ECB gives diffusion within each block, but none between blocks, so patterns in the message survive.
This isn’t just a toy problem. In 2013, attackers stole Adobe’s database of about 150 million user records. The passwords had been encrypted with Triple DES in ECB mode, not hashed. Users with the same password had the same ciphertext. The database also held each user’s password hint, so the hints of everyone who shared a ciphertext could be read together, and they often gave the password away. The most common ciphertext belonged to almost 2 million users, and it was 123456.
Cipher Block Chaining (CBC)¶
In CBC mode, each plaintext block is XORed with the previous ciphertext block before it’s encrypted. The first block has no previous block, so it’s XORed with an initialization vector (IV) instead. The IV doesn’t need to be secret, but each message should get a new one.
Here are the first two blocks as a diagram, where is the block cipher with the key .
Here is GO again with the key 1000 and the IV 0011.
| Block | 1 | 2 | 3 | 4 |
|---|---|---|---|---|
| Plaintext block | 0100 | 0111 | 0100 | 1111 |
| Previous block (IV, then ) | 0011 | 1111 | 0111 | 1011 |
| ⊕ previous | 0111 | 1000 | 0011 | 0100 |
| Ciphertext block | 1111 | 0111 | 1011 | 1100 |
def cbc_encrypt(bits, key, iv):
previous = iv
ciphertext = []
for block in split_blocks(bits):
previous = babyblock(xor(block, previous), key)
ciphertext.append(previous)
return ' '.join(ciphertext)
def cbc_decrypt(bits, key, iv):
previous = iv
plaintext = []
for block in split_blocks(bits):
plaintext.append(xor(babyblock(block, key, decrypt=True), previous))
previous = block
return ' '.join(plaintext)
ciphertext = cbc_encrypt(plaintext, '1000', '0011')
print('CBC: ', ciphertext)
decrypted = cbc_decrypt(ciphertext.replace(' ', ''), '1000', '0011')
print('decrypted:', decrypted, '=', binary_to_text(decrypted.replace(' ', '')))CBC: 1111 0111 1011 1100
decrypted: 0100 0111 0100 1111 = GO
The repeated plaintext block no longer shows. Block 1 was XORed with the IV and block 3 with , so the cipher was given two different inputs (0111 and 0011) and gave two different outputs. Every ciphertext block depends on all of the blocks before it.
Now suppose you send GO again tomorrow, with the same key and a new IV.
print('IV 0011:', cbc_encrypt(plaintext, '1000', '0011'))
print('IV 1011:', cbc_encrypt(plaintext, '1000', '1011'))IV 0011: 1111 0111 1011 1100
IV 1011: 0000 1111 1001 1101
The new IV changes the first block, and every later block depends on the one before it, so the whole ciphertext changes. An eavesdropper can’t even tell that the same message was sent twice. That’s why each message needs a new IV.
Modes used today¶
CBC was the most common mode for many years. Today two other modes are more common.
Counter mode (CTR) encrypts a counter (1, 2, 3, ...) combined with a one-time number, and XORs the results with the plaintext. This turns a block cipher into a stream cipher, and the blocks can be encrypted at the same time instead of one after another. The same counter values must never be reused with the same key.
Galois/Counter Mode (GCM) is counter mode plus an authentication tag, a short code computed from the ciphertext and the key. If anyone changes the ciphertext, the tag won’t match, and the receiver rejects the message. That stops tampering like the
PAY $100toPAY $900attack in the introduction.
The current version of the protocol behind secure websites, TLS 1.3, only allows modes that include a tag like this, such as AES in GCM mode.