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, 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 ff 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.

10010011
000010100011
000100111000
001000100100
001101001111
010011001001
010110110110
011011011110
011110010001
100000011110
100101000111
101011110000
101111100101
110001111010
110100011100
111010000001
111101101011

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 LL = 00 and RR = 10. The round key is 0100. The S-box row 0100, column 10 gives 11. Then L⊕SL \oplus S = 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 LL = 10 and RR = 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 LL = 11 and RR = 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.

RoundInput blockRound keyLRS-box outputL ⊕ SMuddledOutput block
1001001000010111111101011
2101110001011100000111100
3110000011100100101000100

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.

RoundInput blockRound keyLRS-box outputL ⊕ SMuddledOutput block
1010000010100101111000011
2001110000011101010111110
3111001001110110000100010

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.

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.

       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

Here are the examples from above. Each trace shows the same columns as the tables.

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 16×16=25616 \times 16 = 256 pairs of a block and a key, so we can check all of them.

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.

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 24=162^4 = 16 keys, so an attacker can simply try them all. Suppose Eve knows that the plaintext block 1100 encrypted to 0010. She tries every key.

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.

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

BabyBlockDES
Block size4 bits64 bits
Key4 bits56 bits (stored as 64 bits, with 8 parity bits)
Number of keys24=162^4 = 16256≈7.2×10162^{56} \approx 7.2 \times 10^{16}
Rounds316
StructureFeistelFeistel
The function ffone S-box: 4 key bits and 2 block bits in, 2 bits outexpand 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 schedulerotate left 1 bitsplit 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 ff 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 ff 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.

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.

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 EE is the block cipher with the key kk.

Here is GO again with the key 1000 and the IV 0011.

Block1234
Plaintext block PP0100011101001111
Previous block (IV, then CC)0011111101111011
PP ⊕ previous0111100000110100
Ciphertext block CC1111011110111100
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 C2C_2, 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.

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 $100 to PAY $900 attack 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.