-
Notifications
You must be signed in to change notification settings - Fork 74
/
decss.bf
211 lines (174 loc) · 9.78 KB
/
decss.bf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
(Please bear with me: I can't use any periods or commas or
hyphens in this introduction)
This is an implementation of CSS decryption in the Brainfuck
programming language
It consumes exactly 2053 bytes of input and produces exactly
2048 bytes of output: the input is the five byte title key
followed by a 2048 byte sector to decrypt and the output is
the decrypted sector (or the original sector if it wasn't
encrypted)
You can't decrypt more than one sector at a time because BF
doesn't provide any way to detect the end of the input;
rather than make an infinite loop I decided to stop after
one iteration
CSS is the encryption used on DVDs and Brainfuck is a Turing
complete programming language which actually resembles a
Turing machine to no small degree: in particular it uses a
linear workspace accessed through a "read/write head" which
can only move by one cell at a time
It's a lot of fun to watch this program in action if you
have a BF interpreter which shows the movement of the head
(and if you don't have one write one: it takes about five
minutes)
Now on with the show
____________________________________________________________
read the key
,>,>,>,>,>
copy the first 21 bytes of the sector unchanged
+++[->+++++++[->,.<]<]
convert the 21st byte to binary
>>[->>[>]+<[-<]<]
save bit 4 (the encryption bit); nuke the rest
>>[-]>[-]>[-]>[-]>[-<<<<<<<+>>>>>>>]>[-]>[-]>[-]<<<<<<<<<<
if it's set:
<+>[-
copy another 64 bytes
<+++++++[->++++++++[->,.<]<]
xor the most recent byte from input with the first key byte
<<<<<[->>>>>>+<<<<<<]>>>>>>
[->>>>[>>]+<<[-<<]<<]>
[->>>>[>>]+<<[-<<]<<]>
>>[>[-<->]<[->+<]]>>[>[-<->]<[->+<]]
>>[>[-<->]<[->+<]]>>[>[-<->]<[->+<]]
>>[>[-<->]<[->+<]]>>[>[-<->]<[->+<]]
>>[>[-<->]<[->+<]]>>[>[-<->]<[->+<]]
fill in the always set bits in the LFSRs
>>>>>>>>>>+>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<<
move the xor'ed byte to its final location (in the 17 bit LFSR)
<[->>>>>>>>+<<<<<<<<]<
<[->>>>>>>>>+<<<<<<<<<]<
<[->>>>>>>>>>+<<<<<<<<<<]<
<[->>>>>>>>>>>+<<<<<<<<<<<]<
<[->>>>>>>>>>>>+<<<<<<<<<<<<]<
<[->>>>>>>>>>>>>+<<<<<<<<<<<<<]<
<[->>>>>>>>>>>>>>+<<<<<<<<<<<<<<]<
<[->>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<]<
same thing for the next four bytes (yuck)
<<<<<<<<<[->>>>>+<<<<<]>>>>>>,.<
[->>>>[>>]+<<[-<<]<<]>[->>>>[>>]+<<[-<<]<<]>
>>[>[-<->]<[->+<]]>>[>[-<->]<[->+<]]
>>[>[-<->]<[->+<]]>>[>[-<->]<[->+<]]
>>[>[-<->]<[->+<]]>>[>[-<->]<[->+<]]
>>[>[-<->]<[->+<]]>>[>[-<->]<[->+<]]
>[->>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<]<
<[->>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<]<
<[->>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<]<
<[->>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<]<
<[->>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<]<
<[->>>>>>>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<<<<<<<]<
<[->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<]<
<[->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<]<
<[->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<]<
<[->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<]<
<[->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<]<
<[->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<]<
<[->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<]<
<<<<<<<<
copy the remaining 39 bytes of the header
+++[->+++++++++++++[->,.<]<] <<
main decryption loop: for each of 1920 bytes:
+++++[->++++++[->++++++++[->++++++++[->
read a byte and convert it to binary
,[->>>[>>]+<<[-<<]<]
perform the mangling step (bit ordering is AaBbCcDdEeFfGgHh)
>>>>>>+>>+>>>>+>>>>+>
[-<<<<<->>>>>] <<[-<<<<<<<<<<<+>>>>>>>>>>>] F =!h; B = g;
++<<[->>->>+<<<<<<<<<<<->>>>>>>]<< [->>>>->>+<-<<<<<]<< C =!f; H =!e; g=2 minus e minus f; h=e plus f
[-<<<<<<<+>>>>>>>]<< [->>>+<<<] A = d; E = c;
++<<[->>->-<<<]<< [->>>>->>>>>>>+<<<<<<<<<<<] D =!b; G = a; c=2 minus a minus b;
>>>>[[-]<<<<+<[->-<]>[-<+>]>>>>>>+>[-<->]<[->+<]<<] A ^=!!c; E ^=!!c; (!!c == !(a&b))
>>>>>>>>[[-]<<<<<<<<<<+<[->-<]>[-<+>]>>>>>>>>>>] B ^=!!g; (!!g == !(e&f))
>>[[-]<<<<+<[->-<]>[-<+>]>>>>]<<<<<<<<<<<<<<< F ^=!!h; (!!h == (e|f))
[->>[->+>[-<->]<[->+<]>>+>[-<->]<[->+<]<<+<]>[-<+>]<<+<]>[-<+>] C ^= A&B; D ^= A&B;
>>>>>>>>>>++<<<[->+>>->>+<<<<<]>[-<+>]>[-<+>>->>+<<<]<[->+<] f = 2 minus E minus F; g = E plus F;
>>[[-]+>[-<->]<[->+<]]>>[[-]+>[-<->]<[->+<]] G ^=!!f; H ^=!!g;
generate eight cipher bits
>>++++++++[>
clock the 17 bit LFSR and add end bit to previous carry
>>+>> >>>>>>>>>>>>>>>>
[-<<<<<<<<<<<<<<<<<+>>>>>>>>>>>>>>>>>]<
[->+<]<[->+<]<[->+<]<[->+<]<[->+<]<[->+<]<
[->+<]<[->+<]<[->+<]<[->+<]<[->+<]<[->+<]<
[->+<]<[->+<<<<+>>>]<[->+<]<[->+<]<
[->+<<->[->-<<+>]]
clock the 25 bit LFSR and add end bit to total
>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>
[-<<<<<<<<<<<<<<<<<<<<<<<<<+>>>>>>>>>>>>>>>>>>>>>>>>>]<
[->+<]<[->+<]<
[->+<<<<<<<<<<<<<<<<<<<<<<<+>>>>>>>>>>>>>>>>>>>>>>]<
[->+<<<<<<<<<<<<<<<<<<<<<<+>>>>>>>>>>>>>>>>>>>>>]<
[->+<]<[->+<]<[->+<]<[->+<]<[->+<]<[->+<]<[->+<]<
[->+<<<<<<<<<<<<<<+>>>>>>>>>>>>>]<
[->+<]<[->+<]<[->+<]<[->+<]<[->+<]<[->+<]<
[->+<]<[->+<]<[->+<]<[->+<]<[->+<]<[->+<]<
[->+<<+>[->-<<->[->+<<+>[->-<<->]]]]<
[-<<<<<<<<<<<<<<<<<<<+>>>>>>>>>>>>>>>>>>>]<<<<<<<<<<<<<<<<<<<
split total into low bit (result) and high bit (new carry)
[->+<]>
[-<<[<]+>[->]>]
<<<[->>+<<]
shift in new result bit
<<<<<<<<<<<<<<<<<[-]
>>[-<<+>>]>>[-<<+>>]>>[-<<+>>]>>[-<<+>>]
>>[-<<+>>]>>[-<<+>>]>>[-<<+>>]>>>>[-<<<<+>>>>]
<<-]
xor cipher byte with sector byte
<[<[->-<]>[-<+>]]< <[<[->-<]>[-<+>]]<
<[<[->-<]>[-<+>]]< <[<[->-<]>[-<+>]]<
<[<[->-<]>[-<+>]]< <[<[->-<]>[-<+>]]<
<[<[->-<]>[-<+>]]< <[<[->-<]>[-<+>]]
combine bits into output byte
> [-<<++>>] >>>> [-<<++>>] >>>> [-<<++>>] >>>> [-<<++>>]
<< [-<<<<++++>>>>] <<<<<<<< [-<<<<++++>>>>]
>>>> [-<<<<<<<<++++++++++++++++>>>>>>>>]
and write it out
<<<<<<<<.[-]
end of decryption loop
<<]<]<]<] >>>>>
end of decryption; if the encryption bit was clear:
]<[
then just copy the rest of the sector (2027 bytes)
>+++++[->+++++[->+++++++++[->+++++++++[->,.<]<]<]<],.,.>
end of plaintext copy
]