The CSS Cipher¶
The Content Scramble System (CSS) is the encryption that protected movies on commercial DVD-Video discs. It was introduced around 1996, when DVDs first went on sale. It’s a stream cipher built from two LFSRs, one with 17 bits and one with 25 bits, seeded from a 40-bit key.
A DVD player doesn’t just hold one key. Each licensed player has its own player key. The player uses it to unlock a disc key stored on the disc, uses the disc key to unlock title keys, and uses those to unscramble the video. Each of these steps uses the same cipher, built from two LFSRs.
What makes CSS interesting is how it combines the two LFSRs. On the previous page you saw that LFSRsum, which combines two LFSRs with XOR, falls to a system of linear equations. CSS combines them with addition with carry instead, so the key bits no longer satisfy linear equations.
Addition with Carry¶
XOR is addition without carrying (see Binary Operations). CSS uses ordinary addition, carries included:
babyCSS¶
To see how this works, we’ll use a small version called babyCSS. It uses exactly the same LFSRs and key as the LFSRsum example:
LFSR-B: bits , taps and (characteristic polynomial ).
LFSR-C: bits , taps and (characteristic polynomial ).
Key: 7 bits, . The seeds are for LFSR-B and for LFSR-C.
The only difference from LFSRsum is step 2: the outputs are added with carry, not XOR-ed.
Example of babyCSS¶
You receive the ciphertext 11111100 00001110, enciphered with babyCSS and the key 0011001. Decipher it.
Solution: The key gives the same seeds as the LFSRsum example, 0011 and 10011, so the registers produce the same output streams:
LFSR-B output: 11000100 11010111
LFSR-C output: 11001111 10001101Add the first pair of bytes. In decimal this is , which needs 9 bits:
The first keystream byte is 10010011, and the carry out bit, 1, moves on to the next pair. Add the second pair of bytes, plus that carry: .
The second keystream byte is 01100101. (Its carry out would go into a third byte, if the message were longer.) Finally, XOR the ciphertext with the keystream:
ciphertext: 11111100 00001110
keystream: ⊕ 10010011 01100101
-----------------
plaintext: 01101111 01101011 ('ok')The plaintext is ok. Compare this with the LFSRsum example, where the same key and plaintext gave the keystream 00001011 01011010. The registers are the same, but the carries change 9 of the 16 keystream bits.
babyCSS in Python¶
The function below follows the steps above. It splits each LFSR’s output into bytes, adds them one pair at a time, and carries into the next pair. Since enciphering and deciphering are the same XOR, one function does both.
def babycss_keystream(key, num_bytes):
num_bits = 8 * num_bytes
out_b = lfsr(int(key[:3] + '1', 2), [1, 0], 4, num_bits) # seed k1 k2 k3 1
out_c = lfsr(int(key[3:] + '1', 2), [2, 0], 5, num_bits) # seed k4 k5 k6 k7 1
keystream = []
carry = 0
for i in range(num_bytes):
shift = 8 * (num_bytes - 1 - i) # byte i, first byte = most significant
a = (out_b >> shift) & 0b11111111
b = (out_c >> shift) & 0b11111111
total = a + b + carry
keystream.append(total & 0b11111111) # keep the right-most 8 bits
carry = total >> 8 # the carry out bit goes to the next byte
return keystream
def babycss(text, key):
keystream = babycss_keystream(key, len(text))
return ''.join(chr(ord(char) ^ k) for char, k in zip(text, keystream))print([format(k, '08b') for k in babycss_keystream('0011001', 2)])
ciphertext = chr(0b11111100) + chr(0b00001110)
print(ascii(ciphertext))
print(babycss(ciphertext, '0011001'))
print(ascii(babycss('ok', '0011001')))['10010011', '01100101']
'\xfc\x0e'
ok
'\xfc\x0e'
Why the Carry Makes the System Nonlinear¶
When you add two bits and , there is a carry only when both are 1. So the carry is , a product. In LFSRsum every keystream bit was a sum of key bits. With addition with carry, products of key bits appear.
To see this, write the output bits of both registers in terms of the key, using the tables on the LFSR Sum page. The first byte of keystream comes from ticks 1 to 8, and tick 8 gives its right-most bit. Work from the right-most bit to the left, since tick 8 is the only bit with no carry coming in:
Tick 8. LFSR-B outputs and LFSR-C outputs . The keystream bit is their sum, , a linear equation just like in LFSRsum. But the carry out is their product:
Tick 7. LFSR-B outputs , LFSR-C outputs , and the carry from tick 8 comes in. The keystream bit is the sum of all three:
which contains products of key bits. That is a nonlinear equation. In LFSRsum, the same keystream bit was simply .
Tick 6. The carry out of three bits is 1 when at least two of them are 1, so the products pile up. The keystream bit for tick 6 has 28 terms, some of them products of three key bits.
Each step to the left adds a longer carry. By tick 1, the keystream bit has 29 terms, and one of them is the product of all seven key bits. You can check the equations above with the key 0011001 and the keystream byte 10010011: tick 8 gives , and tick 7 gives .
Row reduction only solves linear systems. There is no comparably fast general method for nonlinear systems like this one, so the attack that broke LFSRsum no longer works.
Breaking babyCSS¶
So what options remain to break babyCSS? With a 7-bit key, there are only keys, so an attacker who knows the plaintext starts with ok can simply try them all:
ciphertext = chr(0b11111100) + chr(0b00001110)
for k in range(2**7):
key = format(k, '07b')
if babycss(ciphertext, key) == 'ok':
print(key)0011001
There’s an even faster way, and it’s the idea behind one of the real attacks on CSS. Instead of guessing the whole key, guess only LFSR-B’s 3 key bits. For each guess:
Only 8 guesses are needed instead of 128:
known_keystream = babycss_keystream('0011001', 2) # in an attack, this comes from known plaintext
for g in range(2**3):
guess = format(g, '03b')
out_b = lfsr(int(guess + '1', 2), [1, 0], 4, 16)
out_b_bytes = [out_b >> 8, out_b & 0b11111111]
carry = 0
out_c_bytes = []
for a, s in zip(out_b_bytes, known_keystream):
b = (s - a - carry) % 256 # undo the addition
out_c_bytes.append(b)
carry = (a + b + carry) >> 8 # the carry that addition made
out_c = format(out_c_bytes[0], '08b') + format(out_c_bytes[1], '08b')
seed_c = out_c[:5][::-1] # first 5 output bits, reversed
works = seed_c[-1] == '1' and format(lfsr(int(seed_c, 2), [2, 0], 5, 16), '016b') == out_c
print(guess, seed_c, works)000 10000 False
001 10011 True
010 00111 False
011 01101 False
100 11111 False
101 11101 False
110 01011 False
111 00101 False
Only the guess 001 works, and it gives LFSR-C’s seed 10011. So the key is 001 followed by 1001: 0011001.
Breaking the Real CSS¶
The real CSS uses the same ideas at a larger size:
Two LFSRs, with 17 and 25 bits.
A 40-bit key. Two bytes (16 bits) seed the 17-bit LFSR and three bytes (24 bits) seed the 25-bit LFSR. Each register also gets one bit fixed at 1, so it can never be all 0’s, just like babyCSS.
The output bytes are combined by 8-bit addition with carry, with the carry going into the next byte.
That gives keys. Why only 40 bits? At the time CSS was designed, US export rules limited the encryption in products sold abroad to 40-bit keys. The limit was on export: using stronger encryption inside the US was legal. Since DVD players were sold around the world, CSS was built to fit the export limit.
CSS fell anyway, in two ways.
The design didn’t stay secret. The CSS algorithm was only shared with licensed manufacturers. In October 1999, a program called DeCSS appeared on the internet. It used a player key taken from a software DVD player whose maker hadn’t protected it, and soon the whole algorithm was public. This is Kerckhoffs’s principle in action: a cipher whose security depends on keeping the design secret is not secure.
The design was weak. In November 1999, the cryptographer Frank Stevenson published an analysis of CSS with attacks far faster than trying all 240 keys. One attack is the guess-one-register idea above: with 6 known output bytes, guess the 16 unknown seed bits of the 17-bit register (only guesses), subtract to get the 25-bit register’s output, and check. Another recovers a disc key from information stored on the disc with about 225 steps, which took less than 18 seconds on a 450 MHz Pentium III computer of the time.
The carry did make CSS nonlinear, and that does stop the attack that broke LFSRsum. But the small key, the secret design and the guess-one-register attack each broke it anyway. A cipher is only as strong as its weakest part.
Check Your Understanding¶
Solution to Exercise 1
, so the keystream byte is 00011101 and the carry out is 1.
Solution to Exercise 2
The seeds have free bits, so there are keys.
Solution to Exercise 3
Nothing is added to the right of it, so there is no carry coming in. Its keystream bit is just the sum of the two output bits, mod 2. Its carry out is already a product, though, so the nonlinear terms start with the bit to its left.