CSS411 Cryptography

Simplified DES

Slide deck 3. S-DES: the five encryption functions, the function f_K and F, S-boxes, key generation and a full worked encryption.

27 min read

S-DES overviewSearch on YouTube

Inputs and outputsSearch on YouTube

The S-DES encryption algorithm takes two inputs:

  • an 8-bit block of plaintext (for example 1011110110111101)
  • a 10-bit key

It produces an 8-bit block of ciphertext as output.

The S-DES decryption algorithm takes an 8-bit block of ciphertext and the same 10-bit key. It produces the original 8-bit block of plaintext.

Comparison with DESSearch on YouTube

S-DES is a teaching cipher with the same structure as DES but small parameters. Professor Edward Schaefer of Santa Clara University developed it. It is not secure.

ParameterS-DESDES
Block8 bits64 bits
Key10 bits56 bits
Subkeys2 subkeys of 8 bits16 subkeys of 48 bits
Rounds216
S-boxes2 boxes, 4 bits in, 2 bits out8 boxes, 6 bits in, 4 bits out

Think of S-DES as DES in a toy box. Every permutation, S-box and shift in S-DES has a bigger version in DES.

Five encryption functionsSearch on YouTube

The encryption algorithm involves five functions, in this order:

  1. An initial permutation (IP).
  2. A complex function fKf_K. It does both permutation and substitution and depends on a key input.
  3. A simple permutation function SW that switches the two halves of the data.
  4. The function fKf_K again.
  5. A permutation function that is the inverse of the initial permutation (IP−1\text{IP}^{-1}).

The slide "Simplified DES Encryption Detail" shows the two fKf_K blocks. Each fKf_K block contains the function FF, with E/P, XOR with the subkey, S0, S1 and P4.

Encryption (8-bit block)Plaintext8 bitsIPfK1​SWfK2​IP-1​Cipher8 bitsKey generation (10-bit key)Key10 bitsP10ShiftLS-1P8K1ShiftLS-2P8K2The second shift works on the output of the first shift.
S-DES: IP, then f with K1, then the switch, then f with K2, then the inverse IP. The key schedule makes K1 and K2.

Composition of functionsSearch on YouTube

The slide writes the algorithm as a composition of functions:

ciphertext=IP−1(fK2(SW(fK1(IP(plaintext)))))\text{ciphertext} = \text{IP}^{-1}\Big(f_{K_2}\big(\text{SW}(f_{K_1}(\text{IP}(\text{plaintext})))\big)\Big)

where

K1=P8(Shift(P10(key))),K2=P8(Shift(Shift(P10(key))))K_1 = \text{P8}(\text{Shift}(\text{P10}(\text{key}))), \qquad K_2 = \text{P8}(\text{Shift}(\text{Shift}(\text{P10}(\text{key}))))

Decryption is essentially the reverse of encryption:

plaintext=IP−1(fK1(SW(fK2(IP(ciphertext)))))\text{plaintext} = \text{IP}^{-1}\Big(f_{K_1}\big(\text{SW}(f_{K_2}(\text{IP}(\text{ciphertext})))\big)\Big)

Thus there are:

  • Two permutation functions, IP and IP−1\text{IP}^{-1}, with IP−1(IP(X))=X\text{IP}^{-1}(\text{IP}(X)) = X.
  • Two permutation/substitution functions fKf_K. Each uses one subkey.
  • One switch function SW. It interchanges the left and right 4 bits, so the second fKf_K operates on a different set of bits.

Pipeline: IP, fK1f_{K_1}, SW, fK2f_{K_2}, IP−1\text{IP}^{-1}. Decryption uses the same functions with the subkey order reversed: K2K_2 first, then K1K_1.

Initial and final permutationsSearch on YouTube

Each table entry names the input bit that goes to that output position.

IP26314857
IP−1\text{IP}^{-1}41357286

Slide example:

OperationInputOutput
IP01001101010011011100011011000110
IP−1\text{IP}^{-1}11000110110001100100110101001101

The result is IP−1(IP(X))=X\text{IP}^{-1}(\text{IP}(X)) = X.

The round functionSearch on YouTube

The function f_KSearch on YouTube

Let L be the left 4 bits and R the right 4 bits of the 8-bit input. Let SK be a subkey. Then:

fK(L,R)=(L⊕F(R,SK),  R)f_K(L, R) = (L \oplus F(R, SK), \; R)

Here ⊕\oplus is the bit-by-bit exclusive-OR. Thus fKf_K changes the left half only. The right half passes through unchanged.

Slide example: assume the output of the IP stage is 1011110110111101. Then L,R=1011,1101L, R = 1011, 1101. Assume F(1101,SK)=1110F(1101, SK) = 1110 for some subkey. This value changes with the subkey.

fK(1011,1101)=(1011⊕1110,1101)=(0101,1101)f_K(1011, 1101) = (1011 \oplus 1110, 1101) = (0101, 1101)

Inside FSearch on YouTube

FF takes the right 4 bits and an 8-bit subkey:

StepInOut
E/P (expansion/permutation)4 bits8 bits
XOR with the subkey8 bits8 bits
S0 (left 4 bits) and S1 (right 4 bits)4 bits each2 bits each
P4 (permutation)4 bits4 bits

Expansion and key mixingSearch on YouTube

E/P maps the 4 input bits n1,n2,n3,n4n_1, n_2, n_3, n_4 to 8 bits:

E/P41232341

The slide arranges the 8 bits in two rows:

n4    n1    n2    n3(left half)n2    n3    n4    n1(right half)\begin{aligned} &n_4 \;\; n_1 \;\; n_2 \;\; n_3 \quad \text{(left half)} \\ &n_2 \;\; n_3 \;\; n_4 \;\; n_1 \quad \text{(right half)} \end{aligned}

XOR the 8-bit subkey K1=k11k12…k18K_1 = k_{11} k_{12} \ldots k_{18} into these bits:

n4⊕k11,    n1⊕k12,    n2⊕k13,    n3⊕k14n2⊕k15,    n3⊕k16,    n4⊕k17,    n1⊕k18\begin{aligned} &n_4 \oplus k_{11}, \;\; n_1 \oplus k_{12}, \;\; n_2 \oplus k_{13}, \;\; n_3 \oplus k_{14} \\ &n_2 \oplus k_{15}, \;\; n_3 \oplus k_{16}, \;\; n_4 \oplus k_{17}, \;\; n_1 \oplus k_{18} \end{aligned}

Rename these 8 bits:

Row 1p0,0p_{0,0}p0,1p_{0,1}p0,2p_{0,2}p0,3p_{0,3}
Row 2p1,0p_{1,0}p1,1p_{1,1}p1,2p_{1,2}p1,3p_{1,3}

Row 1 goes into S-box S0. Row 2 goes into S-box S1. Each S-box gives a 2-bit output.

S-boxesSearch on YouTube

The slide gives the S-boxes in decimal:

S0S_0col 0col 1col 2col 3
row 01032
row 13210
row 20213
row 33132
S1S_1col 0col 1col 2col 3
row 00123
row 12013
row 23010
row 32103

To use an S-box:

  1. Use the first and fourth input bits as a 2-bit row number.
  2. Use the second and third input bits as a 2-bit column number.
  3. Write the entry at that row and column as 2 bits. This is the output.

Slide example: S0 entry (0,0)(0,0) is 1=011 = 01. S1 entry (1,1)(1,1) is 0=000 = 00.

Row = outer bits (1 and 4). Column = inner bits (2 and 3). S0 gives the left 2 bits. S1 gives the right 2 bits.

P4 and the switchSearch on YouTube

P4 permutes the 2 bits from S0 and the 2 bits from S1:

P42431

A 4-bit input a b c da\,b\,c\,d leaves P4 as b d c ab\,d\,c\,a. The P4 output is the output of FF. Then fKf_K XORs it into the left half.

The switch function SW interchanges the left and right halves. Thus the second fKf_K, with subkey K2K_2, operates on different data. The second fKf_K works the same as the first. Decryption uses all the same functions, run backwards.

F(R,SK)F(R, SK): E/P expands 4 bits to 8 bits. The subkey is XORed in. S0 and S1 each change 4 bits into 2 bits. P4 permutes the 4 result bits.

Key generationSearch on YouTube

Key schedule stepsSearch on YouTube

S-DES uses a 10-bit key shared between sender and receiver. The key schedule produces two 8-bit subkeys for particular stages of encryption and decryption.

  1. Apply P10 to the 10-bit key.
  2. Split the result into two 5-bit halves.
  3. Do a circular left shift by 1 (LS-1) on each half.
  4. Apply P8 to select and permute 8 of the 10 bits. The result is K1K_1.
  5. Do a circular left shift by 2 more (LS-2) on each half from step 3.
  6. Apply P8 again. The result is K2K_2.
P1035274101986
P8637485109

Subkey K1Search on YouTube

Slide example key: 10100000101010000010.

P10 takes bits (3,5,2,7,4,10,1,9,8,6)(3,5,2,7,4,10,1,9,8,6) of 10100000101010000010. The result is 10000  0110010000\;01100.

OperationFirst halfSecond half
After P1010000100000110001100
After LS-100001000011100011000

P8 takes bits (6,3,7,4,8,5,10,9)(6,3,7,4,8,5,10,9) of 00001110000000111000:

K1=10100100K_1 = 10100100

Subkey K2Search on YouTube

Take the output of LS-1 and shift each half by 2 more positions:

OperationFirst halfSecond half
After LS-100001000011100011000
After LS-200100001000001100011

P8 on 00100000110010000011 gives:

K2=01000011K_2 = 01000011
K1=P8(LS-1(P10(key))),K2=P8(LS-2(LS-1(P10(key))))K_1 = \text{P8}(\text{LS-1}(\text{P10}(\text{key}))), \qquad K_2 = \text{P8}(\text{LS-2}(\text{LS-1}(\text{P10}(\text{key}))))

Shift pattern: 1 bit, then 2 more bits. Each shift is circular and applies to each 5-bit half separately.

Tables to memorize: IP = 2 6 3 1 4 8 5 7. IP−1\text{IP}^{-1} = 4 1 3 5 7 2 8 6. P10 = 3 5 2 7 4 10 1 9 8 6. P8 = 6 3 7 4 8 5 10 9. E/P = 4 1 2 3 2 3 4 1. P4 = 2 4 3 1.

Worked encryptionSearch on YouTube

Round 1Search on YouTube

The last two slides run one full encryption. Key: 10100000101010000010, so K1=10100100K_1 = 10100100 and K2=01000011K_2 = 01000011. Plaintext: 1111001111110011.

StepValue
Plaintext1111001111110011
After IP1011 ∣ 11011011 \,\vert\, 1101, so L=1011L = 1011 and R=1101R = 1101
E/P of RR1110101111101011
XOR K1K_111101011⊕10100100=0100111111101011 \oplus 10100100 = 01001111
S0 on 01000100row 00=000 = 0, column 10=210 = 2, entry 33, output 1111
S1 on 11111111row 11=311 = 3, column 11=311 = 3, entry 33, output 1111
P4 of 1111111111111111
XOR with LL1011⊕1111=01001011 \oplus 1111 = 0100
fK1f_{K_1} output0100 ∣ 11010100 \,\vert\, 1101

Switch and round 2Search on YouTube

SW gives L=1101L = 1101 and R=0100R = 0100.

StepValue
E/P of R=0100R = 01000010100000101000
XOR K2K_200101000⊕01000011=0110101100101000 \oplus 01000011 = 01101011
S0 on 01100110row 00=000 = 0, column 11=311 = 3, entry 22, output 1010
S1 on 10111011row 11=311 = 3, column 01=101 = 1, entry 11, output 0101
P4 of 1001100101010101
XOR with L=1101L = 11011101⊕0101=10001101 \oplus 0101 = 1000
Add the right side ("+RS")1000010010000100

Final permutationSearch on YouTube

IP−1\text{IP}^{-1} takes bits (4,1,3,5,7,2,8,6)(4,1,3,5,7,2,8,6) of 1000010010000100. These bits are (0,1,0,0,0,0,0,1)(0,1,0,0,0,0,0,1). So:

ciphertext=01000001\text{ciphertext} = 01000001

Short trace: IP gives 1011110110111101. fK1f_{K_1} gives 0100110101001101. SW gives 1101010011010100. fK2f_{K_2} gives 1000010010000100. IP−1\text{IP}^{-1} gives 0100000101000001.

DecryptionSearch on YouTube

Reverse subkey orderSearch on YouTube

Decryption uses the same functions, with the subkeys in reverse order:

  1. Apply IP to the ciphertext.
  2. Apply fKf_K with K2K_2.
  3. Apply SW.
  4. Apply fKf_K with K1K_1.
  5. Apply IP−1\text{IP}^{-1}.
DirectionFirst fKf_K keySecond fKf_K key
EncryptionK1K_1K2K_2
DecryptionK2K_2K1K_1

Decryption of 0100000101000001 with this key gives 1111001111110011 again.

The exam can ask for a small encryption by hand. Write all the tables at the top of your rough sheet first. Then check your answer: decrypt it and compare with the plaintext.

Role of each partSearch on YouTube

PartTypeJob
IP, IP−1\text{IP}^{-1}PermutationMix the bits at the start and undo the mix at the end
E/PExpansionGive each S-box bits from the whole 4-bit half
XOR with subkeyKey mixingMake the output depend on the key
S0, S1SubstitutionThe only nonlinear step
P4PermutationSpread the S-box output bits
SWPermutationMake the second fKf_K act on the other half

Quick self-testSearch on YouTube

What are the block size and key size of S-DES?

The block is 8 bits. The key is 10 bits. The key schedule makes two 8-bit subkeys, K1K_1 and K2K_2.

Name the five functions of S-DES encryption in order.

IP, fKf_K with K1K_1, SW, fKf_K with K2K_2, and IP−1\text{IP}^{-1}. So ciphertext =IP−1(fK2(SW(fK1(IP(plaintext)))))= \text{IP}^{-1}(f_{K_2}(\text{SW}(f_{K_1}(\text{IP}(\text{plaintext}))))).

Give the IP and inverse IP tables.

IP = 2 6 3 1 4 8 5 7. IP−1\text{IP}^{-1} = 4 1 3 5 7 2 8 6. The slide example maps 0100110101001101 to 1100011011000110 and back.

Define f_K and say what happens to each half.

fK(L,R)=(L⊕F(R,SK),R)f_K(L, R) = (L \oplus F(R, SK), R). The left half is XORed with the output of FF. The right half does not change.

How is F(R, SK) built?

E/P (4 1 2 3 2 3 4 1) expands 4 bits to 8 bits. XOR with the 8-bit subkey. The left 4 bits go to S0 and the right 4 bits go to S1. Each S-box gives 2 bits. P4 (2 4 3 1) permutes the 4 bits.

How do you read an S-box?

Bits 1 and 4 give the row. Bits 2 and 3 give the column. The entry, written as 2 bits, is the output.

Produce K1 and K2 from key 1010000010.

P10 gives 10000  0110010000\;01100. LS-1 gives 00001  1100000001\;11000. P8 gives K1=10100100K_1 = 10100100. LS-2 gives 00100  0001100100\;00011. P8 gives K2=01000011K_2 = 01000011.

Encrypt plaintext 11110011 with key 1010000010.

IP gives 1011110110111101. Round 1: E/P 1110101111101011, XOR K1K_1 0100111101001111, S0 1111, S1 1111, P4 11111111, XOR LL gives 01000100. SW gives 1101  01001101\;0100. Round 2: E/P 0010100000101000, XOR K2K_2 0110101101101011, S0 1010, S1 0101, P4 01010101, XOR LL gives 10001000. IP−1(10000100)=01000001\text{IP}^{-1}(10000100) = 01000001.

How does decryption differ from encryption?

The functions are the same. The subkey order is reversed: the first fKf_K uses K2K_2 and the second fKf_K uses K1K_1.

  • S-DES: 8-bit block, 10-bit key, two 8-bit subkeys. The structure is the same as DES.
  • Encryption: IP, fK1f_{K_1}, SW, fK2f_{K_2}, IP−1\text{IP}^{-1}. fK(L,R)=(L⊕F(R,SK),R)f_K(L,R) = (L \oplus F(R, SK), R).
  • FF: E/P, XOR with the subkey, S0 and S1 (outer bits = row, inner bits = column), then P4.
  • Key schedule: P10, LS-1, P8 gives K1K_1. LS-2, P8 gives K2K_2. Key 10100000101010000010 gives K1=10100100K_1 = 10100100 and K2=01000011K_2 = 01000011.
  • Plaintext 1111001111110011 with this key encrypts to 0100000101000001. Decryption uses K2K_2 first, then K1K_1.