This page is still under construction.

Parts of this page are still being built. What you see may change.

DeCSS 7

Time limit1sMemory limit1024 MB

Summary
Given a partly known CSS keystream built from two LFSRs with a carry bit, find any 42-bit key that reproduces the known bytes.
Level

Hard8 of 10

Topics
Bit manipulation, Math, Brute force, Simulation
Solved
No attempts yet

Problem

Content Scramble System (CSS) is a method that encrypts the content on DVD media so that only licensed devices can access it. In the variant studied here, the key KK is a sequence of exactly 42 bits k1,k2,…,k42k_1, k_2, \ldots, k_{42}, and the content is a sequence of nn bytes. The content is encrypted by first generating a keystream T(K)T(K) from the key. The keystream is also nn bytes long. Then the bitwise exclusive OR is applied to each content byte and the corresponding keystream byte.

If the ciphertext and some bytes of the content are known, the corresponding keystream bytes can be determined. Your task is to find one possible key KK based on the partially known keystream T(K)T(K).

The keystream function T(K)T(K) is based on a Linear-feedback shift register (LFSR) circuit. The state of an LFSR consists of mm bits b1,b2,…,bmb_1, b_2, \ldots, b_m, and its behavior is defined by a set of feedback positions. In one cycle, the LFSR outputs one bit and updates its state as follows.

  1. Compute the output bit bb by summing the bits at the feedback positions. If the sum is even, bb is 0. Otherwise bb is 1. In other words, bb is the XOR of the bits at the feedback positions.
  2. Shift all state bits one position to the left and discard b1b_1. Place bb in the last position. The new state is b2,b3,…,bm,bb_2, b_3, \ldots, b_m, b.

One step of the LFSR consists of 8 cycles. The result is one byte made of the output bits of the cycles, read from right to left. If the output bits of the cycles are i1,i2,…,i8i_1, i_2, \ldots, i_8 in order, the result is the integer from 0 to 255 whose binary representation is (i8i7…i1)2(i_8 i_7 \ldots i_1)_2.

CSS uses two such circuits.

  • LFSR17 has 17 bits. Its feedback positions are 1 and 15. Its initial state is the key bits k1,k2,…,k17k_1, k_2, \ldots, k_{17}, in that order.
  • LFSR25 has 25 bits. Its feedback positions are 1, 4, 5, and 13. Its initial state is the key bits k18,k19,…,k42k_{18}, k_{19}, \ldots, k_{42}, in that order.

Figure 1: Keystream generation

The keystream T(K)T(K) is generated as follows.

  1. Set LFSR17 and LFSR25 to their initial states using the key KK, as described above.
  2. Set cc to 0.
  3. Repeat the following nn times.
    1. Perform one step of LFSR17 and let xx be the result.
    2. Perform one step of LFSR25 and let yy be the result.
    3. Compute z=x+y+cz = x + y + c.
    4. If z≥256z \ge 256, subtract 256 from zz and set cc to 1. Otherwise set cc to 0.
    5. The next keystream byte is zz.

You are given a keystream where some bytes are known and others are unknown. Find one key KK that produces a keystream matching the given bytes in the way described above.

Input

The first line contains the natural number nn, the length of the keystream. The second line contains nn integers t1,t2,…,tnt_1, t_2, \ldots, t_n, the keystream bytes. If the kk-th byte is unknown, then tk=−1t_k = -1. If it is known, then 0≤tk≤2550 \le t_k \le 255.

Output

Print the bits k1,k2,…,k42k_1, k_2, \ldots, k_{42} of the key on one line, without spaces.

A solution always exists, but it is not necessarily unique.

Explanation of the first example

The following table shows the details of generating the first four bytes of the keystream. The first row is the initial state. Every other row is the state right after the given cycle or step.

StepCycleLFSR17LFSR25xycz
Initial state0 1100 0110 0101 00101 1001 1000 0000 0000 0011 10110
11 1000 1100 1010 01001 0011 0000 0000 0000 0111 0110
21 0001 1001 0100 10000 0110 0000 0000 0000 1110 1101
30 0011 0010 1001 00010 1100 0000 0000 0001 1101 1011
40 0110 0101 0010 00101 1000 0000 0000 0011 1011 0110
50 1100 1010 0100 01001 0000 0000 0000 0111 0110 1101
61 1001 0100 1000 10010 0000 0000 0000 1110 1101 1011
71 0010 1001 0001 00110 0000 0000 0001 1101 1011 0110
80 0101 0010 0010 01110 0000 0000 0011 1011 0110 1101
12281821154
90 1010 0100 0100 11110 0000 0000 0111 0110 1101 1011
101 0100 1000 1001 11110 0000 0000 1110 1101 1011 0111
110 1001 0001 0011 11100 0000 0001 1101 1011 0110 1110
121 0010 0010 0111 11010 0000 0011 1011 0110 1101 1101
130 0100 0100 1111 10100 0000 0111 0110 1101 1011 1011
140 1000 1001 1111 01000 0000 1110 1101 1011 0111 0110
151 0001 0011 1110 10010 0001 1101 1011 0110 1110 1101
160 0010 0111 1101 00110 0011 1011 0110 1101 1101 1010
220391139
170 0100 1111 1010 01100 0111 0110 1101 1011 1011 0100
180 1001 1111 0100 11010 1110 1101 1011 0111 0110 1001
191 0011 1110 1001 10111 1101 1011 0110 1110 1101 0010
200 0111 1101 0011 01111 1011 0110 1101 1101 1010 0100
210 1111 1010 0110 11111 0110 1101 1011 1011 0100 1000
221 1111 0100 1101 11110 1101 1011 0111 0110 1001 0001
231 1110 1001 1011 11101 1011 0110 1110 1101 0010 0010
241 1101 0011 0111 11001 0110 1101 1101 1010 0100 0101
3621620225
251 1010 0110 1111 10000 1101 1011 1011 0100 1000 1011
261 0100 1101 1111 00011 1011 0111 0110 1001 0001 0110
270 1001 1011 1110 00111 0110 1110 1101 0010 0010 1101
281 0011 0111 1100 01100 1101 1101 1010 0100 0101 1011
290 0110 1111 1000 11001 1011 1011 0100 1000 1011 0111
300 1101 1111 0001 10011 0111 0110 1001 0001 0110 1111
311 1011 1110 0011 00100 1110 1101 0010 0010 1101 1110
321 0111 1100 0110 01011 1101 1010 0100 0101 1011 1101
4166189199

Examples1

  1. Example 1

    Input
    7
    154 39 225 99 151 145 -1
    
    Expected output
    011000110010100101100110000000000000111011