Mercurial > mplayer.hg
annotate libdvdcss/css.c @ 36030:7efddc4d726f
Move variable declarations where they belong.
author | ib |
---|---|
date | Sun, 31 Mar 2013 22:41:03 +0000 |
parents | 691431d2289e |
children |
rev | line source |
---|---|
20613 | 1 /***************************************************************************** |
2 * css.c: Functions for DVD authentication and descrambling | |
3 ***************************************************************************** | |
27462 | 4 * Copyright (C) 1999-2008 VideoLAN |
20613 | 5 * |
27442 | 6 * Authors: Stéphane Borel <stef@via.ecp.fr> |
7 * Håkan Hjort <d95hjort@dtek.chalmers.se> | |
20613 | 8 * |
9 * based on: | |
10 * - css-auth by Derek Fawcus <derek@spider.com> | |
11 * - DVD CSS ioctls example program by Andrew T. Veliath <andrewtv@usa.net> | |
12 * - The Divide and conquer attack by Frank A. Stevenson <frank@funcom.com> | |
13 * (see http://www-2.cs.cmu.edu/~dst/DeCSS/FrankStevenson/index.html) | |
14 * - DeCSSPlus by Ethan Hawke | |
15 * - DecVOB | |
16 * see http://www.lemuria.org/DeCSS/ by Tom Vogt for more information. | |
17 * | |
31098
9e9595c779cf
libdvdcss: cosmetics: Fix FSF address and program name in license headers.
diego
parents:
31052
diff
changeset
|
18 * This library is free software; you can redistribute it and/or modify |
20613 | 19 * it under the terms of the GNU General Public License as published by |
20 * the Free Software Foundation; either version 2 of the License, or | |
21 * (at your option) any later version. | |
22 * | |
31098
9e9595c779cf
libdvdcss: cosmetics: Fix FSF address and program name in license headers.
diego
parents:
31052
diff
changeset
|
23 * This library is distributed in the hope that it will be useful, |
20613 | 24 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
26 * GNU General Public License for more details. | |
27 * | |
31098
9e9595c779cf
libdvdcss: cosmetics: Fix FSF address and program name in license headers.
diego
parents:
31052
diff
changeset
|
28 * You should have received a copy of the GNU General Public License along |
9e9595c779cf
libdvdcss: cosmetics: Fix FSF address and program name in license headers.
diego
parents:
31052
diff
changeset
|
29 * with this library; if not, write to the Free Software Foundation, Inc., |
9e9595c779cf
libdvdcss: cosmetics: Fix FSF address and program name in license headers.
diego
parents:
31052
diff
changeset
|
30 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
20613 | 31 *****************************************************************************/ |
32 | |
33 /***************************************************************************** | |
34 * Preamble | |
35 *****************************************************************************/ | |
36 #include "config.h" | |
37 | |
38 #include <stdio.h> | |
39 #include <stdlib.h> | |
40 #include <string.h> | |
41 #include <sys/types.h> | |
42 #include <sys/stat.h> | |
43 #ifdef HAVE_SYS_PARAM_H | |
44 # include <sys/param.h> | |
45 #endif | |
46 #ifdef HAVE_UNISTD_H | |
47 # include <unistd.h> | |
48 #endif | |
49 #include <fcntl.h> | |
50 | |
51 #ifdef HAVE_LIMITS_H | |
52 # include <limits.h> | |
53 #endif | |
54 | |
55 #include "dvdcss/dvdcss.h" | |
56 | |
57 #include "common.h" | |
58 #include "css.h" | |
59 #include "libdvdcss.h" | |
60 #include "csstables.h" | |
61 #include "ioctl.h" | |
62 #include "device.h" | |
63 | |
35952 | 64 #define PSZ_KEY_SIZE (KEY_SIZE * 3) |
65 | |
20613 | 66 /***************************************************************************** |
67 * Local prototypes | |
68 *****************************************************************************/ | |
69 static void PrintKey ( dvdcss_t, char *, uint8_t const * ); | |
70 | |
71 static int GetBusKey ( dvdcss_t ); | |
72 static int GetASF ( dvdcss_t ); | |
73 | |
74 static void CryptKey ( int, int, uint8_t const *, uint8_t * ); | |
75 static void DecryptKey ( uint8_t, | |
76 uint8_t const *, uint8_t const *, uint8_t * ); | |
77 | |
78 static int DecryptDiscKey ( dvdcss_t, uint8_t const *, dvd_key_t ); | |
79 static int CrackDiscKey ( dvdcss_t, uint8_t * ); | |
80 | |
81 static void DecryptTitleKey ( dvd_key_t, dvd_key_t ); | |
82 static int RecoverTitleKey ( int, uint8_t const *, | |
83 uint8_t const *, uint8_t const *, uint8_t * ); | |
84 static int CrackTitleKey ( dvdcss_t, int, int, dvd_key_t ); | |
85 | |
86 static int AttackPattern ( uint8_t const[], int, uint8_t * ); | |
87 #if 0 | |
88 static int AttackPadding ( uint8_t const[], int, uint8_t * ); | |
89 #endif | |
90 | |
35952 | 91 static int _dvdcss_titlekey ( dvdcss_t, int , dvd_key_t ); |
92 | |
20613 | 93 /***************************************************************************** |
94 * _dvdcss_test: check if the disc is encrypted or not | |
31100
45159a4b8815
libdvdcss: Fix potential format string crash; check RPC status on disc access.
diego
parents:
31098
diff
changeset
|
95 ***************************************************************************** |
35952 | 96 * Return values: |
97 * 1: DVD is scrambled but can be read | |
98 * 0: DVD is not scrambled and can be read | |
99 * -1: could not get "copyright" information | |
100 * -2: could not get RPC information (reading the disc might be possible) | |
101 * -3: drive is RPC-II, region is not set, and DVD is scrambled: the RPC | |
102 * scheme will prevent us from reading the scrambled data | |
20613 | 103 *****************************************************************************/ |
35952 | 104 int _dvdcss_test( dvdcss_t dvdcss ) |
20613 | 105 { |
31100
45159a4b8815
libdvdcss: Fix potential format string crash; check RPC status on disc access.
diego
parents:
31098
diff
changeset
|
106 char const *psz_type, *psz_rpc; |
45159a4b8815
libdvdcss: Fix potential format string crash; check RPC status on disc access.
diego
parents:
31098
diff
changeset
|
107 int i_ret, i_copyright, i_type, i_mask, i_rpc; |
20613 | 108 |
109 i_ret = ioctl_ReadCopyright( dvdcss->i_fd, 0 /* i_layer */, &i_copyright ); | |
110 | |
35952 | 111 #ifdef WIN32 |
20613 | 112 if( i_ret < 0 ) |
113 { | |
114 /* Maybe we didn't have enough privileges to read the copyright | |
115 * (see ioctl_ReadCopyright comments). | |
116 * Apparently, on unencrypted DVDs _dvdcss_disckey() always fails, so | |
117 * we can check this as a workaround. */ | |
118 i_ret = 0; | |
119 i_copyright = 1; | |
120 if( _dvdcss_disckey( dvdcss ) < 0 ) | |
121 { | |
122 i_copyright = 0; | |
123 } | |
124 } | |
35952 | 125 #endif |
126 | |
127 if( i_ret < 0 ) | |
128 { | |
129 /* Since it's the first ioctl we try to issue, we add a notice */ | |
130 print_error( dvdcss, "css error: could not get \"copyright\"" | |
131 " information, make sure there is a DVD in the drive," | |
132 " and that you have used the correct device node." ); | |
133 | |
134 return -1; | |
135 } | |
20613 | 136 |
31100
45159a4b8815
libdvdcss: Fix potential format string crash; check RPC status on disc access.
diego
parents:
31098
diff
changeset
|
137 print_debug( dvdcss, "disc reports copyright information 0x%x", |
45159a4b8815
libdvdcss: Fix potential format string crash; check RPC status on disc access.
diego
parents:
31098
diff
changeset
|
138 i_copyright ); |
45159a4b8815
libdvdcss: Fix potential format string crash; check RPC status on disc access.
diego
parents:
31098
diff
changeset
|
139 |
45159a4b8815
libdvdcss: Fix potential format string crash; check RPC status on disc access.
diego
parents:
31098
diff
changeset
|
140 i_ret = ioctl_ReportRPC( dvdcss->i_fd, &i_type, &i_mask, &i_rpc); |
45159a4b8815
libdvdcss: Fix potential format string crash; check RPC status on disc access.
diego
parents:
31098
diff
changeset
|
141 |
45159a4b8815
libdvdcss: Fix potential format string crash; check RPC status on disc access.
diego
parents:
31098
diff
changeset
|
142 if( i_ret < 0 ) |
45159a4b8815
libdvdcss: Fix potential format string crash; check RPC status on disc access.
diego
parents:
31098
diff
changeset
|
143 { |
35263 | 144 print_error( dvdcss, "css error: could not get RPC status. Assuming RPC-I drive." ); |
145 i_type = i_mask = i_rpc = 0; | |
20613 | 146 } |
147 | |
31100
45159a4b8815
libdvdcss: Fix potential format string crash; check RPC status on disc access.
diego
parents:
31098
diff
changeset
|
148 switch( i_rpc ) |
45159a4b8815
libdvdcss: Fix potential format string crash; check RPC status on disc access.
diego
parents:
31098
diff
changeset
|
149 { |
45159a4b8815
libdvdcss: Fix potential format string crash; check RPC status on disc access.
diego
parents:
31098
diff
changeset
|
150 case 0: psz_rpc = "RPC-I"; break; |
45159a4b8815
libdvdcss: Fix potential format string crash; check RPC status on disc access.
diego
parents:
31098
diff
changeset
|
151 case 1: psz_rpc = "RPC-II"; break; |
45159a4b8815
libdvdcss: Fix potential format string crash; check RPC status on disc access.
diego
parents:
31098
diff
changeset
|
152 default: psz_rpc = "unknown RPC scheme"; break; |
45159a4b8815
libdvdcss: Fix potential format string crash; check RPC status on disc access.
diego
parents:
31098
diff
changeset
|
153 } |
45159a4b8815
libdvdcss: Fix potential format string crash; check RPC status on disc access.
diego
parents:
31098
diff
changeset
|
154 |
45159a4b8815
libdvdcss: Fix potential format string crash; check RPC status on disc access.
diego
parents:
31098
diff
changeset
|
155 switch( i_type ) |
45159a4b8815
libdvdcss: Fix potential format string crash; check RPC status on disc access.
diego
parents:
31098
diff
changeset
|
156 { |
45159a4b8815
libdvdcss: Fix potential format string crash; check RPC status on disc access.
diego
parents:
31098
diff
changeset
|
157 case 0: psz_type = "no region code set"; break; |
45159a4b8815
libdvdcss: Fix potential format string crash; check RPC status on disc access.
diego
parents:
31098
diff
changeset
|
158 case 1: psz_type = "region code set"; break; |
45159a4b8815
libdvdcss: Fix potential format string crash; check RPC status on disc access.
diego
parents:
31098
diff
changeset
|
159 case 2: psz_type = "one region change remaining"; break; |
45159a4b8815
libdvdcss: Fix potential format string crash; check RPC status on disc access.
diego
parents:
31098
diff
changeset
|
160 case 3: psz_type = "region code set permanently"; break; |
45159a4b8815
libdvdcss: Fix potential format string crash; check RPC status on disc access.
diego
parents:
31098
diff
changeset
|
161 default: psz_type = "unknown status"; break; |
45159a4b8815
libdvdcss: Fix potential format string crash; check RPC status on disc access.
diego
parents:
31098
diff
changeset
|
162 } |
45159a4b8815
libdvdcss: Fix potential format string crash; check RPC status on disc access.
diego
parents:
31098
diff
changeset
|
163 |
31253 | 164 print_debug( dvdcss, "drive region mask 0x%x, %s, %s", |
31100
45159a4b8815
libdvdcss: Fix potential format string crash; check RPC status on disc access.
diego
parents:
31098
diff
changeset
|
165 i_mask, psz_rpc, psz_type ); |
45159a4b8815
libdvdcss: Fix potential format string crash; check RPC status on disc access.
diego
parents:
31098
diff
changeset
|
166 |
45159a4b8815
libdvdcss: Fix potential format string crash; check RPC status on disc access.
diego
parents:
31098
diff
changeset
|
167 if( i_copyright && i_rpc == 1 && i_type == 0 ) |
45159a4b8815
libdvdcss: Fix potential format string crash; check RPC status on disc access.
diego
parents:
31098
diff
changeset
|
168 { |
45159a4b8815
libdvdcss: Fix potential format string crash; check RPC status on disc access.
diego
parents:
31098
diff
changeset
|
169 print_error( dvdcss, "css error: drive will prevent access to " |
45159a4b8815
libdvdcss: Fix potential format string crash; check RPC status on disc access.
diego
parents:
31098
diff
changeset
|
170 "scrambled data" ); |
35952 | 171 return -3; |
31100
45159a4b8815
libdvdcss: Fix potential format string crash; check RPC status on disc access.
diego
parents:
31098
diff
changeset
|
172 } |
35952 | 173 |
174 return i_copyright ? 1 : 0; | |
20613 | 175 } |
176 | |
177 /***************************************************************************** | |
178 * _dvdcss_title: crack or decrypt the current title key if needed | |
179 ***************************************************************************** | |
180 * This function should only be called by dvdcss->pf_seek and should eventually | |
181 * not be external if possible. | |
182 *****************************************************************************/ | |
183 int _dvdcss_title ( dvdcss_t dvdcss, int i_block ) | |
184 { | |
185 dvd_title_t *p_title; | |
186 dvd_title_t *p_newtitle; | |
187 dvd_key_t p_title_key; | |
188 int i_fd, i_ret = -1, b_cache = 0; | |
189 | |
190 if( ! dvdcss->b_scrambled ) | |
191 { | |
192 return 0; | |
193 } | |
194 | |
195 /* Check if we've already cracked this key */ | |
196 p_title = dvdcss->p_titles; | |
197 while( p_title != NULL | |
198 && p_title->p_next != NULL | |
199 && p_title->p_next->i_startlb <= i_block ) | |
200 { | |
201 p_title = p_title->p_next; | |
202 } | |
203 | |
204 if( p_title != NULL | |
205 && p_title->i_startlb == i_block ) | |
206 { | |
207 /* We've already cracked this key, nothing to do */ | |
208 memcpy( dvdcss->css.p_title_key, p_title->p_key, sizeof(dvd_key_t) ); | |
209 return 0; | |
210 } | |
211 | |
212 /* Check whether the key is in our disk cache */ | |
213 if( dvdcss->psz_cachefile[0] ) | |
214 { | |
215 /* XXX: be careful, we use sprintf and not snprintf */ | |
216 sprintf( dvdcss->psz_block, "%.10x", i_block ); | |
217 i_fd = open( dvdcss->psz_cachefile, O_RDONLY ); | |
218 b_cache = 1; | |
219 | |
220 if( i_fd >= 0 ) | |
221 { | |
35952 | 222 char psz_key[PSZ_KEY_SIZE]; |
20613 | 223 unsigned int k0, k1, k2, k3, k4; |
224 | |
35952 | 225 psz_key[PSZ_KEY_SIZE - 1] = '\0'; |
20613 | 226 |
35952 | 227 if( read( i_fd, psz_key, PSZ_KEY_SIZE - 1 ) == PSZ_KEY_SIZE - 1 |
20613 | 228 && sscanf( psz_key, "%x:%x:%x:%x:%x", |
229 &k0, &k1, &k2, &k3, &k4 ) == 5 ) | |
230 { | |
231 p_title_key[0] = k0; | |
232 p_title_key[1] = k1; | |
233 p_title_key[2] = k2; | |
234 p_title_key[3] = k3; | |
235 p_title_key[4] = k4; | |
236 PrintKey( dvdcss, "title key found in cache ", p_title_key ); | |
237 | |
238 /* Don't try to save it again */ | |
239 b_cache = 0; | |
240 i_ret = 1; | |
241 } | |
242 | |
243 close( i_fd ); | |
244 } | |
245 } | |
246 | |
247 /* Crack or decrypt CSS title key for current VTS */ | |
248 if( i_ret < 0 ) | |
249 { | |
250 i_ret = _dvdcss_titlekey( dvdcss, i_block, p_title_key ); | |
251 | |
252 if( i_ret < 0 ) | |
253 { | |
35952 | 254 print_error( dvdcss, "fatal error in VTS CSS key" ); |
20613 | 255 return i_ret; |
256 } | |
257 | |
258 if( i_ret == 0 ) | |
259 { | |
260 print_debug( dvdcss, "unencrypted title" ); | |
261 /* We cache this anyway, so we don't need to check again. */ | |
262 } | |
263 } | |
264 | |
265 /* Key is valid, we store it on disk. */ | |
266 if( dvdcss->psz_cachefile[0] && b_cache ) | |
267 { | |
268 i_fd = open( dvdcss->psz_cachefile, O_RDWR|O_CREAT, 0644 ); | |
269 if( i_fd >= 0 ) | |
270 { | |
35952 | 271 char psz_key[PSZ_KEY_SIZE + 2]; |
20613 | 272 |
273 sprintf( psz_key, "%02x:%02x:%02x:%02x:%02x\r\n", | |
274 p_title_key[0], p_title_key[1], p_title_key[2], | |
275 p_title_key[3], p_title_key[4] ); | |
276 | |
35952 | 277 if( write( i_fd, psz_key, PSZ_KEY_SIZE + 1 ) < PSZ_KEY_SIZE + 1 ) |
278 { | |
279 print_error( dvdcss, | |
280 "Error caching key on disk, continuing..\n" ); | |
281 } | |
20613 | 282 close( i_fd ); |
283 } | |
284 } | |
285 | |
286 /* Find our spot in the list */ | |
287 p_newtitle = NULL; | |
288 p_title = dvdcss->p_titles; | |
289 while( ( p_title != NULL ) && ( p_title->i_startlb < i_block ) ) | |
290 { | |
291 p_newtitle = p_title; | |
292 p_title = p_title->p_next; | |
293 } | |
294 | |
295 /* Save the found title */ | |
296 p_title = p_newtitle; | |
297 | |
298 /* Write in the new title and its key */ | |
299 p_newtitle = malloc( sizeof( dvd_title_t ) ); | |
300 p_newtitle->i_startlb = i_block; | |
301 memcpy( p_newtitle->p_key, p_title_key, KEY_SIZE ); | |
302 | |
303 /* Link it at the head of the (possibly empty) list */ | |
304 if( p_title == NULL ) | |
305 { | |
306 p_newtitle->p_next = dvdcss->p_titles; | |
307 dvdcss->p_titles = p_newtitle; | |
308 } | |
309 /* Link the new title inside the list */ | |
310 else | |
311 { | |
312 p_newtitle->p_next = p_title->p_next; | |
313 p_title->p_next = p_newtitle; | |
314 } | |
315 | |
316 memcpy( dvdcss->css.p_title_key, p_title_key, KEY_SIZE ); | |
317 return 0; | |
318 } | |
319 | |
320 /***************************************************************************** | |
321 * _dvdcss_disckey: get disc key. | |
322 ***************************************************************************** | |
323 * This function should only be called if DVD ioctls are present. | |
324 * It will set dvdcss->i_method = DVDCSS_METHOD_TITLE if it fails to find | |
325 * a valid disc key. | |
326 * Two decryption methods are offered: | |
327 * -disc key hash crack, | |
328 * -decryption with player keys if they are available. | |
329 *****************************************************************************/ | |
330 int _dvdcss_disckey( dvdcss_t dvdcss ) | |
331 { | |
332 unsigned char p_buffer[ DVD_DISCKEY_SIZE ]; | |
333 dvd_key_t p_disc_key; | |
334 int i; | |
335 | |
336 if( GetBusKey( dvdcss ) < 0 ) | |
337 { | |
338 return -1; | |
339 } | |
340 | |
341 /* Get encrypted disc key */ | |
342 if( ioctl_ReadDiscKey( dvdcss->i_fd, &dvdcss->css.i_agid, p_buffer ) < 0 ) | |
343 { | |
344 print_error( dvdcss, "ioctl ReadDiscKey failed" ); | |
345 return -1; | |
346 } | |
347 | |
35952 | 348 /* This should have invalidated the AGID and got us ASF=1. */ |
20613 | 349 if( GetASF( dvdcss ) != 1 ) |
350 { | |
351 /* Region mismatch (or region not set) is the most likely source. */ | |
352 print_error( dvdcss, | |
353 "ASF not 1 after reading disc key (region mismatch?)" ); | |
354 ioctl_InvalidateAgid( dvdcss->i_fd, &dvdcss->css.i_agid ); | |
355 return -1; | |
356 } | |
357 | |
358 /* Shuffle disc key using bus key */ | |
359 for( i = 0 ; i < DVD_DISCKEY_SIZE ; i++ ) | |
360 { | |
361 p_buffer[ i ] ^= dvdcss->css.p_bus_key[ 4 - (i % KEY_SIZE) ]; | |
362 } | |
363 | |
364 /* Decrypt disc key */ | |
365 switch( dvdcss->i_method ) | |
366 { | |
367 case DVDCSS_METHOD_KEY: | |
368 | |
369 /* Decrypt disc key with player key. */ | |
370 PrintKey( dvdcss, "decrypting disc key ", p_buffer ); | |
371 if( ! DecryptDiscKey( dvdcss, p_buffer, p_disc_key ) ) | |
372 { | |
373 PrintKey( dvdcss, "decrypted disc key is ", p_disc_key ); | |
374 break; | |
375 } | |
376 print_debug( dvdcss, "failed to decrypt the disc key, " | |
377 "faulty drive/kernel? " | |
378 "cracking title keys instead" ); | |
379 | |
380 /* Fallback, but not to DISC as the disc key might be faulty */ | |
27442 | 381 memset( p_disc_key, 0, KEY_SIZE ); |
20613 | 382 dvdcss->i_method = DVDCSS_METHOD_TITLE; |
383 break; | |
384 | |
385 case DVDCSS_METHOD_DISC: | |
386 | |
387 /* Crack Disc key to be able to use it */ | |
388 memcpy( p_disc_key, p_buffer, KEY_SIZE ); | |
389 PrintKey( dvdcss, "cracking disc key ", p_disc_key ); | |
390 if( ! CrackDiscKey( dvdcss, p_disc_key ) ) | |
391 { | |
392 PrintKey( dvdcss, "cracked disc key is ", p_disc_key ); | |
393 break; | |
394 } | |
395 print_debug( dvdcss, "failed to crack the disc key" ); | |
396 memset( p_disc_key, 0, KEY_SIZE ); | |
397 dvdcss->i_method = DVDCSS_METHOD_TITLE; | |
398 break; | |
399 | |
400 default: | |
401 | |
402 print_debug( dvdcss, "disc key needs not be decrypted" ); | |
403 memset( p_disc_key, 0, KEY_SIZE ); | |
404 break; | |
405 } | |
406 | |
407 memcpy( dvdcss->css.p_disc_key, p_disc_key, KEY_SIZE ); | |
408 | |
409 return 0; | |
410 } | |
411 | |
412 | |
413 /***************************************************************************** | |
414 * _dvdcss_titlekey: get title key. | |
415 *****************************************************************************/ | |
35952 | 416 static int _dvdcss_titlekey( dvdcss_t dvdcss, int i_pos, dvd_key_t p_title_key ) |
20613 | 417 { |
418 static uint8_t p_garbage[ DVDCSS_BLOCK_SIZE ]; /* we never read it back */ | |
419 uint8_t p_key[ KEY_SIZE ]; | |
420 int i, i_ret = 0; | |
421 | |
422 if( dvdcss->b_ioctls && ( dvdcss->i_method == DVDCSS_METHOD_KEY || | |
423 dvdcss->i_method == DVDCSS_METHOD_DISC ) ) | |
424 { | |
425 /* We have a decrypted Disc key and the ioctls are available, | |
426 * read the title key and decrypt it. | |
427 */ | |
428 | |
429 print_debug( dvdcss, "getting title key at block %i the classic way", | |
430 i_pos ); | |
431 | |
432 /* We need to authenticate again every time to get a new session key */ | |
433 if( GetBusKey( dvdcss ) < 0 ) | |
434 { | |
31052
a2525319f373
Try other methods also if GetBusKey fails instead of failing completely.
reimar
parents:
27462
diff
changeset
|
435 i_ret = -1; |
20613 | 436 } |
437 | |
438 /* Get encrypted title key */ | |
439 if( ioctl_ReadTitleKey( dvdcss->i_fd, &dvdcss->css.i_agid, | |
440 i_pos, p_key ) < 0 ) | |
441 { | |
442 print_debug( dvdcss, | |
443 "ioctl ReadTitleKey failed (region mismatch?)" ); | |
444 i_ret = -1; | |
445 } | |
446 | |
447 /* Test ASF, it will be reset to 0 if we got a Region error */ | |
448 switch( GetASF( dvdcss ) ) | |
449 { | |
450 case -1: | |
451 /* An error getting the ASF status, something must be wrong. */ | |
452 print_debug( dvdcss, "lost ASF requesting title key" ); | |
453 ioctl_InvalidateAgid( dvdcss->i_fd, &dvdcss->css.i_agid ); | |
454 i_ret = -1; | |
455 break; | |
456 | |
457 case 0: | |
458 /* This might either be a title that has no key, | |
459 * or we encountered a region error. */ | |
460 print_debug( dvdcss, "lost ASF requesting title key" ); | |
461 break; | |
462 | |
463 case 1: | |
35952 | 464 /* Drive status is OK. */ |
20613 | 465 /* If the title key request failed, but we did not loose ASF, |
35952 | 466 * we might still have the AGID. Other code assumes that we |
20613 | 467 * will not after this so invalidate it(?). */ |
468 if( i_ret < 0 ) | |
469 { | |
470 ioctl_InvalidateAgid( dvdcss->i_fd, &dvdcss->css.i_agid ); | |
471 } | |
472 break; | |
473 } | |
474 | |
475 if( !( i_ret < 0 ) ) | |
476 { | |
477 /* Decrypt title key using the bus key */ | |
478 for( i = 0 ; i < KEY_SIZE ; i++ ) | |
479 { | |
480 p_key[ i ] ^= dvdcss->css.p_bus_key[ 4 - (i % KEY_SIZE) ]; | |
481 } | |
482 | |
483 /* If p_key is all zero then there really wasn't any key present | |
484 * even though we got to read it without an error. */ | |
485 if( !( p_key[0] | p_key[1] | p_key[2] | p_key[3] | p_key[4] ) ) | |
486 { | |
487 i_ret = 0; | |
488 } | |
489 else | |
490 { | |
491 PrintKey( dvdcss, "initial disc key ", dvdcss->css.p_disc_key ); | |
492 DecryptTitleKey( dvdcss->css.p_disc_key, p_key ); | |
493 PrintKey( dvdcss, "decrypted title key ", p_key ); | |
494 i_ret = 1; | |
495 } | |
496 | |
497 /* All went well either there wasn't a key or we have it now. */ | |
498 memcpy( p_title_key, p_key, KEY_SIZE ); | |
499 PrintKey( dvdcss, "title key is ", p_title_key ); | |
500 | |
501 return i_ret; | |
502 } | |
503 | |
504 /* The title key request failed */ | |
505 print_debug( dvdcss, "resetting drive and cracking title key" ); | |
506 | |
507 /* Read an unscrambled sector and reset the drive */ | |
508 dvdcss->pf_seek( dvdcss, 0 ); | |
509 dvdcss->pf_read( dvdcss, p_garbage, 1 ); | |
510 dvdcss->pf_seek( dvdcss, 0 ); | |
511 _dvdcss_disckey( dvdcss ); | |
512 | |
513 /* Fallback */ | |
514 } | |
515 | |
516 /* METHOD is TITLE, we can't use the ioctls or requesting the title key | |
517 * failed above. For these cases we try to crack the key instead. */ | |
518 | |
35952 | 519 /* For now, the read limit is 9GB / 2048 = 4718592 sectors. */ |
20613 | 520 i_ret = CrackTitleKey( dvdcss, i_pos, 4718592, p_key ); |
521 | |
522 memcpy( p_title_key, p_key, KEY_SIZE ); | |
523 PrintKey( dvdcss, "title key is ", p_title_key ); | |
524 | |
525 return i_ret; | |
526 } | |
527 | |
528 /***************************************************************************** | |
529 * _dvdcss_unscramble: does the actual descrambling of data | |
530 ***************************************************************************** | |
531 * sec : sector to unscramble | |
532 * key : title key for this sector | |
533 *****************************************************************************/ | |
534 int _dvdcss_unscramble( dvd_key_t p_key, uint8_t *p_sec ) | |
535 { | |
536 unsigned int i_t1, i_t2, i_t3, i_t4, i_t5, i_t6; | |
537 uint8_t *p_end = p_sec + DVDCSS_BLOCK_SIZE; | |
538 | |
539 /* PES_scrambling_control */ | |
540 if( !(p_sec[0x14] & 0x30) ) | |
541 { | |
542 return 0; | |
543 } | |
544 | |
545 i_t1 = (p_key[0] ^ p_sec[0x54]) | 0x100; | |
546 i_t2 = p_key[1] ^ p_sec[0x55]; | |
547 i_t3 = (p_key[2] | (p_key[3] << 8) | | |
548 (p_key[4] << 16)) ^ (p_sec[0x56] | | |
549 (p_sec[0x57] << 8) | (p_sec[0x58] << 16)); | |
550 i_t4 = i_t3 & 7; | |
551 i_t3 = i_t3 * 2 + 8 - i_t4; | |
552 p_sec += 0x80; | |
553 i_t5 = 0; | |
554 | |
555 while( p_sec != p_end ) | |
556 { | |
557 i_t4 = p_css_tab2[i_t2] ^ p_css_tab3[i_t1]; | |
558 i_t2 = i_t1>>1; | |
559 i_t1 = ( ( i_t1 & 1 ) << 8 ) ^ i_t4; | |
560 i_t4 = p_css_tab5[i_t4]; | |
561 i_t6 = ((((((( i_t3 >> 3 ) ^ i_t3 ) >> 1 ) ^ | |
562 i_t3 ) >> 8 ) ^ i_t3 ) >> 5 ) & 0xff; | |
563 i_t3 = (i_t3 << 8 ) | i_t6; | |
564 i_t6 = p_css_tab4[i_t6]; | |
565 i_t5 += i_t6 + i_t4; | |
566 *p_sec = p_css_tab1[*p_sec] ^ ( i_t5 & 0xff ); | |
567 p_sec++; | |
568 i_t5 >>= 8; | |
569 } | |
570 | |
571 return 0; | |
572 } | |
573 | |
574 /* Following functions are local */ | |
575 | |
576 /***************************************************************************** | |
577 * GetBusKey : Go through the CSS Authentication process | |
578 ***************************************************************************** | |
579 * It simulates the mutual authentication between logical unit and host, | |
580 * and stops when a session key (called bus key) has been established. | |
581 * Always do the full auth sequence. Some drives seem to lie and always | |
35952 | 582 * respond with ASF=1. For instance the old DVD-ROMs on Compaq Armada says |
20613 | 583 * that ASF=1 from the start and then later fail with a 'read of scrambled |
584 * block without authentication' error. | |
585 *****************************************************************************/ | |
586 static int GetBusKey( dvdcss_t dvdcss ) | |
587 { | |
588 uint8_t p_buffer[10]; | |
589 uint8_t p_challenge[2*KEY_SIZE]; | |
590 dvd_key_t p_key1; | |
591 dvd_key_t p_key2; | |
592 dvd_key_t p_key_check; | |
593 uint8_t i_variant = 0; | |
594 int i_ret = -1; | |
595 int i; | |
596 | |
597 print_debug( dvdcss, "requesting AGID" ); | |
598 i_ret = ioctl_ReportAgid( dvdcss->i_fd, &dvdcss->css.i_agid ); | |
599 | |
600 /* We might have to reset hung authentication processes in the drive | |
601 * by invalidating the corresponding AGID'. As long as we haven't got | |
602 * an AGID, invalidate one (in sequence) and try again. */ | |
603 for( i = 0; i_ret == -1 && i < 4 ; ++i ) | |
604 { | |
605 print_debug( dvdcss, "ioctl ReportAgid failed, " | |
606 "invalidating AGID %d", i ); | |
607 | |
608 /* This is really _not good_, should be handled by the OS. | |
609 * Invalidating an AGID could make another process fail somewhere | |
610 * in its authentication process. */ | |
611 dvdcss->css.i_agid = i; | |
612 ioctl_InvalidateAgid( dvdcss->i_fd, &dvdcss->css.i_agid ); | |
613 | |
614 print_debug( dvdcss, "requesting AGID" ); | |
615 i_ret = ioctl_ReportAgid( dvdcss->i_fd, &dvdcss->css.i_agid ); | |
616 } | |
617 | |
618 /* Unable to authenticate without AGID */ | |
619 if( i_ret == -1 ) | |
620 { | |
621 print_error( dvdcss, "ioctl ReportAgid failed, fatal" ); | |
622 return -1; | |
623 } | |
624 | |
625 /* Setup a challenge, any values should work */ | |
626 for( i = 0 ; i < 10; ++i ) | |
627 { | |
628 p_challenge[i] = i; | |
629 } | |
630 | |
631 /* Get challenge from host */ | |
632 for( i = 0 ; i < 10 ; ++i ) | |
633 { | |
634 p_buffer[9-i] = p_challenge[i]; | |
635 } | |
636 | |
637 /* Send challenge to LU */ | |
638 if( ioctl_SendChallenge( dvdcss->i_fd, | |
639 &dvdcss->css.i_agid, p_buffer ) < 0 ) | |
640 { | |
641 print_error( dvdcss, "ioctl SendChallenge failed" ); | |
642 ioctl_InvalidateAgid( dvdcss->i_fd, &dvdcss->css.i_agid ); | |
643 return -1; | |
644 } | |
645 | |
646 /* Get key1 from LU */ | |
647 if( ioctl_ReportKey1( dvdcss->i_fd, &dvdcss->css.i_agid, p_buffer ) < 0) | |
648 { | |
649 print_error( dvdcss, "ioctl ReportKey1 failed" ); | |
650 ioctl_InvalidateAgid( dvdcss->i_fd, &dvdcss->css.i_agid ); | |
651 return -1; | |
652 } | |
653 | |
654 /* Send key1 to host */ | |
655 for( i = 0 ; i < KEY_SIZE ; i++ ) | |
656 { | |
657 p_key1[i] = p_buffer[4-i]; | |
658 } | |
659 | |
660 for( i = 0 ; i < 32 ; ++i ) | |
661 { | |
662 CryptKey( 0, i, p_challenge, p_key_check ); | |
663 | |
664 if( memcmp( p_key_check, p_key1, KEY_SIZE ) == 0 ) | |
665 { | |
666 print_debug( dvdcss, "drive authenticated, using variant %d", i ); | |
667 i_variant = i; | |
668 break; | |
669 } | |
670 } | |
671 | |
672 if( i == 32 ) | |
673 { | |
674 print_error( dvdcss, "drive would not authenticate" ); | |
675 ioctl_InvalidateAgid( dvdcss->i_fd, &dvdcss->css.i_agid ); | |
676 return -1; | |
677 } | |
678 | |
679 /* Get challenge from LU */ | |
680 if( ioctl_ReportChallenge( dvdcss->i_fd, | |
681 &dvdcss->css.i_agid, p_buffer ) < 0 ) | |
682 { | |
683 print_error( dvdcss, "ioctl ReportKeyChallenge failed" ); | |
684 ioctl_InvalidateAgid( dvdcss->i_fd, &dvdcss->css.i_agid ); | |
685 return -1; | |
686 } | |
687 | |
688 /* Send challenge to host */ | |
689 for( i = 0 ; i < 10 ; ++i ) | |
690 { | |
691 p_challenge[i] = p_buffer[9-i]; | |
692 } | |
693 | |
694 CryptKey( 1, i_variant, p_challenge, p_key2 ); | |
695 | |
696 /* Get key2 from host */ | |
697 for( i = 0 ; i < KEY_SIZE ; ++i ) | |
698 { | |
699 p_buffer[4-i] = p_key2[i]; | |
700 } | |
701 | |
702 /* Send key2 to LU */ | |
703 if( ioctl_SendKey2( dvdcss->i_fd, &dvdcss->css.i_agid, p_buffer ) < 0 ) | |
704 { | |
705 print_error( dvdcss, "ioctl SendKey2 failed" ); | |
706 ioctl_InvalidateAgid( dvdcss->i_fd, &dvdcss->css.i_agid ); | |
707 return -1; | |
708 } | |
709 | |
710 /* The drive has accepted us as authentic. */ | |
711 print_debug( dvdcss, "authentication established" ); | |
712 | |
713 memcpy( p_challenge, p_key1, KEY_SIZE ); | |
714 memcpy( p_challenge + KEY_SIZE, p_key2, KEY_SIZE ); | |
715 | |
716 CryptKey( 2, i_variant, p_challenge, dvdcss->css.p_bus_key ); | |
717 | |
718 return 0; | |
719 } | |
720 | |
721 /***************************************************************************** | |
722 * PrintKey : debug function that dumps a key value | |
723 *****************************************************************************/ | |
724 static void PrintKey( dvdcss_t dvdcss, char *prefix, uint8_t const *data ) | |
725 { | |
726 print_debug( dvdcss, "%s%02x:%02x:%02x:%02x:%02x", prefix, | |
727 data[0], data[1], data[2], data[3], data[4] ); | |
728 } | |
729 | |
730 /***************************************************************************** | |
731 * GetASF : Get Authentication success flag | |
732 ***************************************************************************** | |
733 * Returns : | |
734 * -1 on ioctl error, | |
735 * 0 if the device needs to be authenticated, | |
736 * 1 either. | |
737 *****************************************************************************/ | |
738 static int GetASF( dvdcss_t dvdcss ) | |
739 { | |
740 int i_asf = 0; | |
741 | |
742 if( ioctl_ReportASF( dvdcss->i_fd, NULL, &i_asf ) != 0 ) | |
743 { | |
744 /* The ioctl process has failed */ | |
745 print_error( dvdcss, "GetASF fatal error" ); | |
746 return -1; | |
747 } | |
748 | |
749 if( i_asf ) | |
750 { | |
751 print_debug( dvdcss, "GetASF authenticated, ASF=1" ); | |
752 } | |
753 else | |
754 { | |
755 print_debug( dvdcss, "GetASF not authenticated, ASF=0" ); | |
756 } | |
757 | |
758 return i_asf; | |
759 } | |
760 | |
761 /***************************************************************************** | |
762 * CryptKey : shuffles bits and unencrypt keys. | |
763 ***************************************************************************** | |
35952 | 764 * Used during authentication and disc key negotiation in GetBusKey. |
20613 | 765 * i_key_type : 0->key1, 1->key2, 2->buskey. |
766 * i_variant : between 0 and 31. | |
767 *****************************************************************************/ | |
768 static void CryptKey( int i_key_type, int i_variant, | |
769 uint8_t const *p_challenge, uint8_t *p_key ) | |
770 { | |
771 /* Permutation table for challenge */ | |
35952 | 772 static const uint8_t pp_perm_challenge[3][10] = |
20613 | 773 { { 1, 3, 0, 7, 5, 2, 9, 6, 4, 8 }, |
774 { 6, 1, 9, 3, 8, 5, 7, 4, 0, 2 }, | |
775 { 4, 0, 3, 5, 7, 2, 8, 6, 1, 9 } }; | |
776 | |
777 /* Permutation table for variant table for key2 and buskey */ | |
35952 | 778 static const uint8_t pp_perm_variant[2][32] = |
20613 | 779 { { 0x0a, 0x08, 0x0e, 0x0c, 0x0b, 0x09, 0x0f, 0x0d, |
780 0x1a, 0x18, 0x1e, 0x1c, 0x1b, 0x19, 0x1f, 0x1d, | |
781 0x02, 0x00, 0x06, 0x04, 0x03, 0x01, 0x07, 0x05, | |
782 0x12, 0x10, 0x16, 0x14, 0x13, 0x11, 0x17, 0x15 }, | |
783 { 0x12, 0x1a, 0x16, 0x1e, 0x02, 0x0a, 0x06, 0x0e, | |
784 0x10, 0x18, 0x14, 0x1c, 0x00, 0x08, 0x04, 0x0c, | |
785 0x13, 0x1b, 0x17, 0x1f, 0x03, 0x0b, 0x07, 0x0f, | |
786 0x11, 0x19, 0x15, 0x1d, 0x01, 0x09, 0x05, 0x0d } }; | |
787 | |
35952 | 788 static const uint8_t p_variants[32] = |
20613 | 789 { 0xB7, 0x74, 0x85, 0xD0, 0xCC, 0xDB, 0xCA, 0x73, |
790 0x03, 0xFE, 0x31, 0x03, 0x52, 0xE0, 0xB7, 0x42, | |
791 0x63, 0x16, 0xF2, 0x2A, 0x79, 0x52, 0xFF, 0x1B, | |
792 0x7A, 0x11, 0xCA, 0x1A, 0x9B, 0x40, 0xAD, 0x01 }; | |
793 | |
794 /* The "secret" key */ | |
35952 | 795 static const uint8_t p_secret[5] = { 0x55, 0xD6, 0xC4, 0xC5, 0x28 }; |
20613 | 796 |
797 uint8_t p_bits[30], p_scratch[10], p_tmp1[5], p_tmp2[5]; | |
798 uint8_t i_lfsr0_o; /* 1 bit used */ | |
799 uint8_t i_lfsr1_o; /* 1 bit used */ | |
800 uint8_t i_css_variant, i_cse, i_index, i_combined, i_carry; | |
801 uint8_t i_val = 0; | |
802 uint32_t i_lfsr0, i_lfsr1; | |
803 int i_term = 0; | |
804 int i_bit; | |
805 int i; | |
806 | |
807 for (i = 9; i >= 0; --i) | |
808 p_scratch[i] = p_challenge[pp_perm_challenge[i_key_type][i]]; | |
809 | |
810 i_css_variant = ( i_key_type == 0 ) ? i_variant : | |
811 pp_perm_variant[i_key_type-1][i_variant]; | |
812 | |
813 /* | |
814 * This encryption engine implements one of 32 variations | |
815 * one the same theme depending upon the choice in the | |
816 * variant parameter (0 - 31). | |
817 * | |
818 * The algorithm itself manipulates a 40 bit input into | |
819 * a 40 bit output. | |
820 * The parameter 'input' is 80 bits. It consists of | |
821 * the 40 bit input value that is to be encrypted followed | |
822 * by a 40 bit seed value for the pseudo random number | |
823 * generators. | |
824 */ | |
825 | |
826 /* Feed the secret into the input values such that | |
827 * we alter the seed to the LFSR's used above, then | |
828 * generate the bits to play with. | |
829 */ | |
830 for( i = 5 ; --i >= 0 ; ) | |
831 { | |
832 p_tmp1[i] = p_scratch[5 + i] ^ p_secret[i] ^ p_crypt_tab2[i]; | |
833 } | |
834 | |
835 /* | |
836 * We use two LFSR's (seeded from some of the input data bytes) to | |
837 * generate two streams of pseudo-random bits. These two bit streams | |
838 * are then combined by simply adding with carry to generate a final | |
839 * sequence of pseudo-random bits which is stored in the buffer that | |
840 * 'output' points to the end of - len is the size of this buffer. | |
841 * | |
842 * The first LFSR is of degree 25, and has a polynomial of: | |
843 * x^13 + x^5 + x^4 + x^1 + 1 | |
844 * | |
35952 | 845 * The second LFSR is of degree 17, and has a (primitive) polynomial of: |
20613 | 846 * x^15 + x^1 + 1 |
847 * | |
848 * I don't know if these polynomials are primitive modulo 2, and thus | |
849 * represent maximal-period LFSR's. | |
850 * | |
851 * | |
852 * Note that we take the output of each LFSR from the new shifted in | |
853 * bit, not the old shifted out bit. Thus for ease of use the LFSR's | |
854 * are implemented in bit reversed order. | |
855 * | |
856 */ | |
857 | |
858 /* In order to ensure that the LFSR works we need to ensure that the | |
35952 | 859 * initial values are non-zero. Thus when we initialize them from |
20613 | 860 * the seed, we ensure that a bit is set. |
861 */ | |
862 i_lfsr0 = ( p_tmp1[0] << 17 ) | ( p_tmp1[1] << 9 ) | | |
863 (( p_tmp1[2] & ~7 ) << 1 ) | 8 | ( p_tmp1[2] & 7 ); | |
864 i_lfsr1 = ( p_tmp1[3] << 9 ) | 0x100 | p_tmp1[4]; | |
865 | |
866 i_index = sizeof(p_bits); | |
867 i_carry = 0; | |
868 | |
869 do | |
870 { | |
871 for( i_bit = 0, i_val = 0 ; i_bit < 8 ; ++i_bit ) | |
872 { | |
873 | |
874 i_lfsr0_o = ( ( i_lfsr0 >> 24 ) ^ ( i_lfsr0 >> 21 ) ^ | |
875 ( i_lfsr0 >> 20 ) ^ ( i_lfsr0 >> 12 ) ) & 1; | |
876 i_lfsr0 = ( i_lfsr0 << 1 ) | i_lfsr0_o; | |
877 | |
878 i_lfsr1_o = ( ( i_lfsr1 >> 16 ) ^ ( i_lfsr1 >> 2 ) ) & 1; | |
879 i_lfsr1 = ( i_lfsr1 << 1 ) | i_lfsr1_o; | |
880 | |
881 i_combined = !i_lfsr1_o + i_carry + !i_lfsr0_o; | |
882 /* taking bit 1 */ | |
883 i_carry = ( i_combined >> 1 ) & 1; | |
884 i_val |= ( i_combined & 1 ) << i_bit; | |
885 } | |
886 | |
887 p_bits[--i_index] = i_val; | |
888 } while( i_index > 0 ); | |
889 | |
890 /* This term is used throughout the following to | |
891 * select one of 32 different variations on the | |
892 * algorithm. | |
893 */ | |
894 i_cse = p_variants[i_css_variant] ^ p_crypt_tab2[i_css_variant]; | |
895 | |
896 /* Now the actual blocks doing the encryption. Each | |
897 * of these works on 40 bits at a time and are quite | |
898 * similar. | |
899 */ | |
900 i_index = 0; | |
901 for( i = 5, i_term = 0 ; --i >= 0 ; i_term = p_scratch[i] ) | |
902 { | |
903 i_index = p_bits[25 + i] ^ p_scratch[i]; | |
904 i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse; | |
905 | |
906 p_tmp1[i] = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term; | |
907 } | |
908 p_tmp1[4] ^= p_tmp1[0]; | |
909 | |
910 for( i = 5, i_term = 0 ; --i >= 0 ; i_term = p_tmp1[i] ) | |
911 { | |
912 i_index = p_bits[20 + i] ^ p_tmp1[i]; | |
913 i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse; | |
914 | |
915 p_tmp2[i] = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term; | |
916 } | |
917 p_tmp2[4] ^= p_tmp2[0]; | |
918 | |
919 for( i = 5, i_term = 0 ; --i >= 0 ; i_term = p_tmp2[i] ) | |
920 { | |
921 i_index = p_bits[15 + i] ^ p_tmp2[i]; | |
922 i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse; | |
923 i_index = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term; | |
924 | |
925 p_tmp1[i] = p_crypt_tab0[i_index] ^ p_crypt_tab2[i_index]; | |
926 } | |
927 p_tmp1[4] ^= p_tmp1[0]; | |
928 | |
929 for( i = 5, i_term = 0 ; --i >= 0 ; i_term = p_tmp1[i] ) | |
930 { | |
931 i_index = p_bits[10 + i] ^ p_tmp1[i]; | |
932 i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse; | |
933 | |
934 i_index = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term; | |
935 | |
936 p_tmp2[i] = p_crypt_tab0[i_index] ^ p_crypt_tab2[i_index]; | |
937 } | |
938 p_tmp2[4] ^= p_tmp2[0]; | |
939 | |
940 for( i = 5, i_term = 0 ; --i >= 0 ; i_term = p_tmp2[i] ) | |
941 { | |
942 i_index = p_bits[5 + i] ^ p_tmp2[i]; | |
943 i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse; | |
944 | |
945 p_tmp1[i] = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term; | |
946 } | |
947 p_tmp1[4] ^= p_tmp1[0]; | |
948 | |
949 for(i = 5, i_term = 0 ; --i >= 0 ; i_term = p_tmp1[i] ) | |
950 { | |
951 i_index = p_bits[i] ^ p_tmp1[i]; | |
952 i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse; | |
953 | |
954 p_key[i] = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term; | |
955 } | |
956 | |
957 return; | |
958 } | |
959 | |
960 /***************************************************************************** | |
961 * DecryptKey: decrypt p_crypted with p_key. | |
962 ***************************************************************************** | |
963 * Used to decrypt the disc key, with a player key, after requesting it | |
964 * in _dvdcss_disckey and to decrypt title keys, with a disc key, requested | |
965 * in _dvdcss_titlekey. | |
966 * The player keys and the resulting disc key are only used as KEKs | |
967 * (key encryption keys). | |
35952 | 968 * Decryption is slightly dependent on the type of key: |
20613 | 969 * -for disc key, invert is 0x00, |
970 * -for title key, invert if 0xff. | |
971 *****************************************************************************/ | |
972 static void DecryptKey( uint8_t invert, uint8_t const *p_key, | |
973 uint8_t const *p_crypted, uint8_t *p_result ) | |
974 { | |
975 unsigned int i_lfsr1_lo; | |
976 unsigned int i_lfsr1_hi; | |
977 unsigned int i_lfsr0; | |
978 unsigned int i_combined; | |
979 uint8_t o_lfsr0; | |
980 uint8_t o_lfsr1; | |
981 uint8_t k[5]; | |
982 int i; | |
983 | |
984 i_lfsr1_lo = p_key[0] | 0x100; | |
985 i_lfsr1_hi = p_key[1]; | |
986 | |
987 i_lfsr0 = ( ( p_key[4] << 17 ) | |
988 | ( p_key[3] << 9 ) | |
989 | ( p_key[2] << 1 ) ) | |
990 + 8 - ( p_key[2] & 7 ); | |
991 i_lfsr0 = ( p_css_tab4[i_lfsr0 & 0xff] << 24 ) | | |
992 ( p_css_tab4[( i_lfsr0 >> 8 ) & 0xff] << 16 ) | | |
993 ( p_css_tab4[( i_lfsr0 >> 16 ) & 0xff] << 8 ) | | |
994 p_css_tab4[( i_lfsr0 >> 24 ) & 0xff]; | |
995 | |
996 i_combined = 0; | |
997 for( i = 0 ; i < KEY_SIZE ; ++i ) | |
998 { | |
999 o_lfsr1 = p_css_tab2[i_lfsr1_hi] ^ p_css_tab3[i_lfsr1_lo]; | |
1000 i_lfsr1_hi = i_lfsr1_lo >> 1; | |
1001 i_lfsr1_lo = ( ( i_lfsr1_lo & 1 ) << 8 ) ^ o_lfsr1; | |
1002 o_lfsr1 = p_css_tab4[o_lfsr1]; | |
1003 | |
1004 o_lfsr0 = ((((((( i_lfsr0 >> 8 ) ^ i_lfsr0 ) >> 1 ) | |
1005 ^ i_lfsr0 ) >> 3 ) ^ i_lfsr0 ) >> 7 ); | |
1006 i_lfsr0 = ( i_lfsr0 >> 8 ) | ( o_lfsr0 << 24 ); | |
1007 | |
1008 i_combined += ( o_lfsr0 ^ invert ) + o_lfsr1; | |
1009 k[i] = i_combined & 0xff; | |
1010 i_combined >>= 8; | |
1011 } | |
1012 | |
1013 p_result[4] = k[4] ^ p_css_tab1[p_crypted[4]] ^ p_crypted[3]; | |
1014 p_result[3] = k[3] ^ p_css_tab1[p_crypted[3]] ^ p_crypted[2]; | |
1015 p_result[2] = k[2] ^ p_css_tab1[p_crypted[2]] ^ p_crypted[1]; | |
1016 p_result[1] = k[1] ^ p_css_tab1[p_crypted[1]] ^ p_crypted[0]; | |
1017 p_result[0] = k[0] ^ p_css_tab1[p_crypted[0]] ^ p_result[4]; | |
1018 | |
1019 p_result[4] = k[4] ^ p_css_tab1[p_result[4]] ^ p_result[3]; | |
1020 p_result[3] = k[3] ^ p_css_tab1[p_result[3]] ^ p_result[2]; | |
1021 p_result[2] = k[2] ^ p_css_tab1[p_result[2]] ^ p_result[1]; | |
1022 p_result[1] = k[1] ^ p_css_tab1[p_result[1]] ^ p_result[0]; | |
1023 p_result[0] = k[0] ^ p_css_tab1[p_result[0]]; | |
1024 | |
1025 return; | |
1026 } | |
1027 | |
1028 /***************************************************************************** | |
1029 * player_keys: alternate DVD player keys | |
1030 ***************************************************************************** | |
1031 * These player keys were generated using Frank A. Stevenson's PlayerKey | |
1032 * cracker. A copy of his article can be found here: | |
1033 * http://www-2.cs.cmu.edu/~dst/DeCSS/FrankStevenson/mail2.txt | |
1034 *****************************************************************************/ | |
1035 static const dvd_key_t player_keys[] = | |
1036 { | |
1037 { 0x01, 0xaf, 0xe3, 0x12, 0x80 }, | |
1038 { 0x12, 0x11, 0xca, 0x04, 0x3b }, | |
1039 { 0x14, 0x0c, 0x9e, 0xd0, 0x09 }, | |
1040 { 0x14, 0x71, 0x35, 0xba, 0xe2 }, | |
1041 { 0x1a, 0xa4, 0x33, 0x21, 0xa6 }, | |
1042 { 0x26, 0xec, 0xc4, 0xa7, 0x4e }, | |
1043 { 0x2c, 0xb2, 0xc1, 0x09, 0xee }, | |
1044 { 0x2f, 0x25, 0x9e, 0x96, 0xdd }, | |
1045 { 0x33, 0x2f, 0x49, 0x6c, 0xe0 }, | |
1046 { 0x35, 0x5b, 0xc1, 0x31, 0x0f }, | |
1047 { 0x36, 0x67, 0xb2, 0xe3, 0x85 }, | |
1048 { 0x39, 0x3d, 0xf1, 0xf1, 0xbd }, | |
1049 { 0x3b, 0x31, 0x34, 0x0d, 0x91 }, | |
1050 { 0x45, 0xed, 0x28, 0xeb, 0xd3 }, | |
1051 { 0x48, 0xb7, 0x6c, 0xce, 0x69 }, | |
1052 { 0x4b, 0x65, 0x0d, 0xc1, 0xee }, | |
1053 { 0x4c, 0xbb, 0xf5, 0x5b, 0x23 }, | |
1054 { 0x51, 0x67, 0x67, 0xc5, 0xe0 }, | |
1055 { 0x53, 0x94, 0xe1, 0x75, 0xbf }, | |
1056 { 0x57, 0x2c, 0x8b, 0x31, 0xae }, | |
1057 { 0x63, 0xdb, 0x4c, 0x5b, 0x4a }, | |
1058 { 0x7b, 0x1e, 0x5e, 0x2b, 0x57 }, | |
1059 { 0x85, 0xf3, 0x85, 0xa0, 0xe0 }, | |
1060 { 0xab, 0x1e, 0xe7, 0x7b, 0x72 }, | |
1061 { 0xab, 0x36, 0xe3, 0xeb, 0x76 }, | |
1062 { 0xb1, 0xb8, 0xf9, 0x38, 0x03 }, | |
1063 { 0xb8, 0x5d, 0xd8, 0x53, 0xbd }, | |
1064 { 0xbf, 0x92, 0xc3, 0xb0, 0xe2 }, | |
1065 { 0xcf, 0x1a, 0xb2, 0xf8, 0x0a }, | |
1066 { 0xec, 0xa0, 0xcf, 0xb3, 0xff }, | |
1067 { 0xfc, 0x95, 0xa9, 0x87, 0x35 } | |
1068 }; | |
1069 | |
1070 /***************************************************************************** | |
1071 * DecryptDiscKey | |
1072 ***************************************************************************** | |
1073 * Decryption of the disc key with player keys: try to decrypt the disc key | |
1074 * from every position with every player key. | |
1075 * p_struct_disckey: the 2048 byte DVD_STRUCT_DISCKEY data | |
1076 * p_disc_key: result, the 5 byte disc key | |
1077 *****************************************************************************/ | |
1078 static int DecryptDiscKey( dvdcss_t dvdcss, uint8_t const *p_struct_disckey, | |
1079 dvd_key_t p_disc_key ) | |
1080 { | |
1081 uint8_t p_verify[KEY_SIZE]; | |
1082 unsigned int i, n = 0; | |
1083 | |
1084 /* Decrypt disc key with the above player keys */ | |
1085 for( n = 0; n < sizeof(player_keys) / sizeof(dvd_key_t); n++ ) | |
1086 { | |
1087 PrintKey( dvdcss, "trying player key ", player_keys[n] ); | |
1088 | |
1089 for( i = 1; i < 409; i++ ) | |
1090 { | |
1091 /* Check if player key n is the right key for position i. */ | |
1092 DecryptKey( 0, player_keys[n], p_struct_disckey + 5 * i, | |
1093 p_disc_key ); | |
1094 | |
1095 /* The first part in the struct_disckey block is the | |
1096 * 'disc key' encrypted with itself. Using this we | |
1097 * can check if we decrypted the correct key. */ | |
1098 DecryptKey( 0, p_disc_key, p_struct_disckey, p_verify ); | |
1099 | |
1100 /* If the position / player key pair worked then return. */ | |
1101 if( memcmp( p_disc_key, p_verify, KEY_SIZE ) == 0 ) | |
1102 { | |
1103 return 0; | |
1104 } | |
1105 } | |
1106 } | |
1107 | |
1108 /* Have tried all combinations of positions and keys, | |
1109 * and we still didn't succeed. */ | |
1110 memset( p_disc_key, 0, KEY_SIZE ); | |
1111 return -1; | |
1112 } | |
1113 | |
1114 /***************************************************************************** | |
1115 * DecryptTitleKey | |
1116 ***************************************************************************** | |
1117 * Decrypt the title key using the disc key. | |
1118 * p_disc_key: result, the 5 byte disc key | |
1119 * p_titlekey: the encrypted title key, gets overwritten by the decrypted key | |
1120 *****************************************************************************/ | |
1121 static void DecryptTitleKey( dvd_key_t p_disc_key, dvd_key_t p_titlekey ) | |
1122 { | |
1123 DecryptKey( 0xff, p_disc_key, p_titlekey, p_titlekey ); | |
1124 } | |
1125 | |
1126 /***************************************************************************** | |
1127 * CrackDiscKey: brute force disc key | |
1128 * CSS hash reversal function designed by Frank Stevenson | |
1129 ***************************************************************************** | |
1130 * This function uses a big amount of memory to crack the disc key from the | |
1131 * disc key hash, if player keys are not available. | |
1132 *****************************************************************************/ | |
1133 #define K1TABLEWIDTH 10 | |
1134 | |
1135 /* | |
1136 * Simple function to test if a candidate key produces the given hash | |
1137 */ | |
1138 static int investigate( unsigned char *hash, unsigned char *ckey ) | |
1139 { | |
1140 unsigned char key[KEY_SIZE]; | |
1141 | |
1142 DecryptKey( 0, ckey, hash, key ); | |
1143 | |
1144 return memcmp( key, ckey, KEY_SIZE ); | |
1145 } | |
1146 | |
1147 static int CrackDiscKey( dvdcss_t dvdcss, uint8_t *p_disc_key ) | |
1148 { | |
1149 unsigned char B[5] = { 0,0,0,0,0 }; /* Second Stage of mangle cipher */ | |
1150 unsigned char C[5] = { 0,0,0,0,0 }; /* Output Stage of mangle cipher | |
1151 * IntermediateKey */ | |
1152 unsigned char k[5] = { 0,0,0,0,0 }; /* Mangling cipher key | |
1153 * Also output from CSS( C ) */ | |
1154 unsigned char out1[5]; /* five first output bytes of LFSR1 */ | |
1155 unsigned char out2[5]; /* five first output bytes of LFSR2 */ | |
1156 unsigned int lfsr1a; /* upper 9 bits of LFSR1 */ | |
1157 unsigned int lfsr1b; /* lower 8 bits of LFSR1 */ | |
1158 unsigned int tmp, tmp2, tmp3, tmp4,tmp5; | |
1159 int i,j; | |
1160 unsigned int nStepA; /* iterator for LFSR1 start state */ | |
1161 unsigned int nStepB; /* iterator for possible B[0] */ | |
1162 unsigned int nTry; /* iterator for K[1] possibilities */ | |
1163 unsigned int nPossibleK1; /* #of possible K[1] values */ | |
1164 unsigned char* K1table; /* Lookup table for possible K[1] */ | |
1165 unsigned int* BigTable; /* LFSR2 startstate indexed by | |
1166 * 1,2,5 output byte */ | |
1167 | |
1168 /* | |
1169 * Prepare tables for hash reversal | |
1170 */ | |
1171 | |
1172 /* initialize lookup tables for k[1] */ | |
35267 | 1173 K1table = calloc( 65536, K1TABLEWIDTH ); |
20613 | 1174 if( K1table == NULL ) |
1175 { | |
1176 return -1; | |
1177 } | |
1178 | |
1179 tmp = p_disc_key[0] ^ p_css_tab1[ p_disc_key[1] ]; | |
1180 for( i = 0 ; i < 256 ; i++ ) /* k[1] */ | |
1181 { | |
1182 tmp2 = p_css_tab1[ tmp ^ i ]; /* p_css_tab1[ B[1] ]*/ | |
1183 | |
1184 for( j = 0 ; j < 256 ; j++ ) /* B[0] */ | |
1185 { | |
1186 tmp3 = j ^ tmp2 ^ i; /* C[1] */ | |
1187 tmp4 = K1table[ K1TABLEWIDTH * ( 256 * j + tmp3 ) ]; /* count of entries here */ | |
1188 tmp4++; | |
1189 /* | |
1190 if( tmp4 == K1TABLEWIDTH ) | |
1191 { | |
1192 print_debug( dvdcss, "Table disaster %d", tmp4 ); | |
1193 } | |
1194 */ | |
1195 if( tmp4 < K1TABLEWIDTH ) | |
1196 { | |
1197 K1table[ K1TABLEWIDTH * ( 256 * j + tmp3 ) + tmp4 ] = i; | |
1198 } | |
1199 K1table[ K1TABLEWIDTH * ( 256 * j + tmp3 ) ] = tmp4; | |
1200 } | |
1201 } | |
1202 | |
35952 | 1203 /* Initializing our really big table */ |
35267 | 1204 BigTable = calloc( 16777216, sizeof(int) ); |
20613 | 1205 if( BigTable == NULL ) |
1206 { | |
35263 | 1207 free( K1table ); |
20613 | 1208 return -1; |
1209 } | |
1210 | |
1211 tmp3 = 0; | |
1212 | |
1213 print_debug( dvdcss, "initializing the big table" ); | |
1214 | |
1215 for( i = 0 ; i < 16777216 ; i++ ) | |
1216 { | |
1217 tmp = (( i + i ) & 0x1fffff0 ) | 0x8 | ( i & 0x7 ); | |
1218 | |
1219 for( j = 0 ; j < 5 ; j++ ) | |
1220 { | |
1221 tmp2=((((((( tmp >> 3 ) ^ tmp ) >> 1 ) ^ tmp ) >> 8 ) | |
1222 ^ tmp ) >> 5 ) & 0xff; | |
1223 tmp = ( tmp << 8) | tmp2; | |
1224 out2[j] = p_css_tab4[ tmp2 ]; | |
1225 } | |
1226 | |
1227 j = ( out2[0] << 16 ) | ( out2[1] << 8 ) | out2[4]; | |
1228 BigTable[j] = i; | |
1229 } | |
1230 | |
1231 /* | |
35952 | 1232 * We are done initializing, now reverse hash |
20613 | 1233 */ |
1234 tmp5 = p_disc_key[0] ^ p_css_tab1[ p_disc_key[1] ]; | |
1235 | |
1236 for( nStepA = 0 ; nStepA < 65536 ; nStepA ++ ) | |
1237 { | |
1238 lfsr1a = 0x100 | ( nStepA >> 8 ); | |
1239 lfsr1b = nStepA & 0xff; | |
1240 | |
1241 /* Generate 5 first output bytes from lfsr1 */ | |
1242 for( i = 0 ; i < 5 ; i++ ) | |
1243 { | |
1244 tmp = p_css_tab2[ lfsr1b ] ^ p_css_tab3[ lfsr1a ]; | |
1245 lfsr1b = lfsr1a >> 1; | |
1246 lfsr1a = ((lfsr1a&1)<<8) ^ tmp; | |
1247 out1[ i ] = p_css_tab4[ tmp ]; | |
1248 } | |
1249 | |
35952 | 1250 /* compute and cache some variables */ |
20613 | 1251 C[0] = nStepA >> 8; |
1252 C[1] = nStepA & 0xff; | |
1253 tmp = p_disc_key[3] ^ p_css_tab1[ p_disc_key[4] ]; | |
1254 tmp2 = p_css_tab1[ p_disc_key[0] ]; | |
1255 | |
1256 /* Search through all possible B[0] */ | |
1257 for( nStepB = 0 ; nStepB < 256 ; nStepB++ ) | |
1258 { | |
1259 /* reverse parts of the mangling cipher */ | |
1260 B[0] = nStepB; | |
1261 k[0] = p_css_tab1[ B[0] ] ^ C[0]; | |
1262 B[4] = B[0] ^ k[0] ^ tmp2; | |
1263 k[4] = B[4] ^ tmp; | |
1264 nPossibleK1 = K1table[ K1TABLEWIDTH * (256 * B[0] + C[1]) ]; | |
1265 | |
1266 /* Try out all possible values for k[1] */ | |
1267 for( nTry = 0 ; nTry < nPossibleK1 ; nTry++ ) | |
1268 { | |
1269 k[1] = K1table[ K1TABLEWIDTH * (256 * B[0] + C[1]) + nTry + 1 ]; | |
1270 B[1] = tmp5 ^ k[1]; | |
1271 | |
1272 /* reconstruct output from LFSR2 */ | |
1273 tmp3 = ( 0x100 + k[0] - out1[0] ); | |
1274 out2[0] = tmp3 & 0xff; | |
1275 tmp3 = tmp3 & 0x100 ? 0x100 : 0xff; | |
1276 tmp3 = ( tmp3 + k[1] - out1[1] ); | |
1277 out2[1] = tmp3 & 0xff; | |
1278 tmp3 = ( 0x100 + k[4] - out1[4] ); | |
1279 out2[4] = tmp3 & 0xff; /* Can be 1 off */ | |
1280 | |
1281 /* test first possible out2[4] */ | |
1282 tmp4 = ( out2[0] << 16 ) | ( out2[1] << 8 ) | out2[4]; | |
1283 tmp4 = BigTable[ tmp4 ]; | |
1284 C[2] = tmp4 & 0xff; | |
1285 C[3] = ( tmp4 >> 8 ) & 0xff; | |
1286 C[4] = ( tmp4 >> 16 ) & 0xff; | |
1287 B[3] = p_css_tab1[ B[4] ] ^ k[4] ^ C[4]; | |
1288 k[3] = p_disc_key[2] ^ p_css_tab1[ p_disc_key[3] ] ^ B[3]; | |
1289 B[2] = p_css_tab1[ B[3] ] ^ k[3] ^ C[3]; | |
1290 k[2] = p_disc_key[1] ^ p_css_tab1[ p_disc_key[2] ] ^ B[2]; | |
1291 | |
1292 if( ( B[1] ^ p_css_tab1[ B[2] ] ^ k[ 2 ] ) == C[ 2 ] ) | |
1293 { | |
1294 if( ! investigate( &p_disc_key[0] , &C[0] ) ) | |
1295 { | |
1296 goto end; | |
1297 } | |
1298 } | |
1299 | |
1300 /* Test second possible out2[4] */ | |
1301 out2[4] = ( out2[4] + 0xff ) & 0xff; | |
1302 tmp4 = ( out2[0] << 16 ) | ( out2[1] << 8 ) | out2[4]; | |
1303 tmp4 = BigTable[ tmp4 ]; | |
1304 C[2] = tmp4 & 0xff; | |
1305 C[3] = ( tmp4 >> 8 ) & 0xff; | |
1306 C[4] = ( tmp4 >> 16 ) & 0xff; | |
1307 B[3] = p_css_tab1[ B[4] ] ^ k[4] ^ C[4]; | |
1308 k[3] = p_disc_key[2] ^ p_css_tab1[ p_disc_key[3] ] ^ B[3]; | |
1309 B[2] = p_css_tab1[ B[3] ] ^ k[3] ^ C[3]; | |
1310 k[2] = p_disc_key[1] ^ p_css_tab1[ p_disc_key[2] ] ^ B[2]; | |
1311 | |
1312 if( ( B[1] ^ p_css_tab1[ B[2] ] ^ k[ 2 ] ) == C[ 2 ] ) | |
1313 { | |
1314 if( ! investigate( &p_disc_key[0] , &C[0] ) ) | |
1315 { | |
1316 goto end; | |
1317 } | |
1318 } | |
1319 } | |
1320 } | |
1321 } | |
1322 | |
1323 end: | |
1324 | |
1325 memcpy( p_disc_key, &C[0], KEY_SIZE ); | |
1326 | |
1327 free( K1table ); | |
1328 free( BigTable ); | |
1329 | |
1330 return 0; | |
1331 } | |
1332 | |
1333 /***************************************************************************** | |
1334 * RecoverTitleKey: (title) key recovery from cipher and plain text | |
1335 * Function designed by Frank Stevenson | |
1336 ***************************************************************************** | |
1337 * Called from Attack* which are in turn called by CrackTitleKey. Given | |
1338 * a guessed(?) plain text and the cipher text. Returns -1 on failure. | |
1339 *****************************************************************************/ | |
1340 static int RecoverTitleKey( int i_start, uint8_t const *p_crypted, | |
1341 uint8_t const *p_decrypted, | |
1342 uint8_t const *p_sector_seed, uint8_t *p_key ) | |
1343 { | |
1344 uint8_t p_buffer[10]; | |
1345 unsigned int i_t1, i_t2, i_t3, i_t4, i_t5, i_t6; | |
1346 unsigned int i_try; | |
1347 unsigned int i_candidate; | |
1348 unsigned int i, j; | |
1349 int i_exit = -1; | |
1350 | |
1351 for( i = 0 ; i < 10 ; i++ ) | |
1352 { | |
1353 p_buffer[i] = p_css_tab1[p_crypted[i]] ^ p_decrypted[i]; | |
1354 } | |
1355 | |
1356 for( i_try = i_start ; i_try < 0x10000 ; i_try++ ) | |
1357 { | |
1358 i_t1 = i_try >> 8 | 0x100; | |
1359 i_t2 = i_try & 0xff; | |
1360 i_t3 = 0; /* not needed */ | |
1361 i_t5 = 0; | |
1362 | |
1363 /* iterate cipher 4 times to reconstruct LFSR2 */ | |
1364 for( i = 0 ; i < 4 ; i++ ) | |
1365 { | |
35952 | 1366 /* advance LFSR1 normally */ |
20613 | 1367 i_t4 = p_css_tab2[i_t2] ^ p_css_tab3[i_t1]; |
1368 i_t2 = i_t1 >> 1; | |
1369 i_t1 = ( ( i_t1 & 1 ) << 8 ) ^ i_t4; | |
1370 i_t4 = p_css_tab5[i_t4]; | |
1371 /* deduce i_t6 & i_t5 */ | |
1372 i_t6 = p_buffer[i]; | |
1373 if( i_t5 ) | |
1374 { | |
1375 i_t6 = ( i_t6 + 0xff ) & 0x0ff; | |
1376 } | |
1377 if( i_t6 < i_t4 ) | |
1378 { | |
1379 i_t6 += 0x100; | |
1380 } | |
1381 i_t6 -= i_t4; | |
1382 i_t5 += i_t6 + i_t4; | |
1383 i_t6 = p_css_tab4[ i_t6 ]; | |
1384 /* feed / advance i_t3 / i_t5 */ | |
1385 i_t3 = ( i_t3 << 8 ) | i_t6; | |
1386 i_t5 >>= 8; | |
1387 } | |
1388 | |
1389 i_candidate = i_t3; | |
1390 | |
1391 /* iterate 6 more times to validate candidate key */ | |
1392 for( ; i < 10 ; i++ ) | |
1393 { | |
1394 i_t4 = p_css_tab2[i_t2] ^ p_css_tab3[i_t1]; | |
1395 i_t2 = i_t1 >> 1; | |
1396 i_t1 = ( ( i_t1 & 1 ) << 8 ) ^ i_t4; | |
1397 i_t4 = p_css_tab5[i_t4]; | |
1398 i_t6 = ((((((( i_t3 >> 3 ) ^ i_t3 ) >> 1 ) ^ | |
1399 i_t3 ) >> 8 ) ^ i_t3 ) >> 5 ) & 0xff; | |
1400 i_t3 = ( i_t3 << 8 ) | i_t6; | |
1401 i_t6 = p_css_tab4[i_t6]; | |
1402 i_t5 += i_t6 + i_t4; | |
1403 if( ( i_t5 & 0xff ) != p_buffer[i] ) | |
1404 { | |
1405 break; | |
1406 } | |
1407 | |
1408 i_t5 >>= 8; | |
1409 } | |
1410 | |
1411 if( i == 10 ) | |
1412 { | |
1413 /* Do 4 backwards steps of iterating t3 to deduce initial state */ | |
1414 i_t3 = i_candidate; | |
1415 for( i = 0 ; i < 4 ; i++ ) | |
1416 { | |
1417 i_t1 = i_t3 & 0xff; | |
1418 i_t3 = ( i_t3 >> 8 ); | |
35952 | 1419 /* easy to code, and fast enough brute-force |
20613 | 1420 * search for byte shifted in */ |
1421 for( j = 0 ; j < 256 ; j++ ) | |
1422 { | |
1423 i_t3 = ( i_t3 & 0x1ffff ) | ( j << 17 ); | |
1424 i_t6 = ((((((( i_t3 >> 3 ) ^ i_t3 ) >> 1 ) ^ | |
1425 i_t3 ) >> 8 ) ^ i_t3 ) >> 5 ) & 0xff; | |
1426 if( i_t6 == i_t1 ) | |
1427 { | |
1428 break; | |
1429 } | |
1430 } | |
1431 } | |
1432 | |
1433 i_t4 = ( i_t3 >> 1 ) - 4; | |
1434 for( i_t5 = 0 ; i_t5 < 8; i_t5++ ) | |
1435 { | |
1436 if( ( ( i_t4 + i_t5 ) * 2 + 8 - ( (i_t4 + i_t5 ) & 7 ) ) | |
1437 == i_t3 ) | |
1438 { | |
1439 p_key[0] = i_try>>8; | |
1440 p_key[1] = i_try & 0xFF; | |
1441 p_key[2] = ( ( i_t4 + i_t5 ) >> 0 ) & 0xFF; | |
1442 p_key[3] = ( ( i_t4 + i_t5 ) >> 8 ) & 0xFF; | |
1443 p_key[4] = ( ( i_t4 + i_t5 ) >> 16 ) & 0xFF; | |
1444 i_exit = i_try + 1; | |
1445 } | |
1446 } | |
1447 } | |
1448 } | |
1449 | |
1450 if( i_exit >= 0 ) | |
1451 { | |
1452 p_key[0] ^= p_sector_seed[0]; | |
1453 p_key[1] ^= p_sector_seed[1]; | |
1454 p_key[2] ^= p_sector_seed[2]; | |
1455 p_key[3] ^= p_sector_seed[3]; | |
1456 p_key[4] ^= p_sector_seed[4]; | |
1457 } | |
1458 | |
1459 return i_exit; | |
1460 } | |
1461 | |
1462 | |
1463 /****************************************************************************** | |
1464 * Various pieces for the title crack engine. | |
1465 ****************************************************************************** | |
1466 * The length of the PES packet is located at 0x12-0x13. | |
35952 | 1467 * The the copyright protection bits are located at 0x14 (bits 0x20 and 0x10). |
20613 | 1468 * The data of the PES packet begins at 0x15 (if there isn't any PTS/DTS) |
1469 * or at 0x?? if there are both PTS and DTS's. | |
1470 * The seed value used with the unscrambling key is the 5 bytes at 0x54-0x58. | |
35952 | 1471 * The scrambled part of a sector begins at 0x80. |
20613 | 1472 *****************************************************************************/ |
1473 | |
1474 /* Statistics */ | |
1475 static int i_tries = 0, i_success = 0; | |
1476 | |
1477 /***************************************************************************** | |
1478 * CrackTitleKey: try to crack title key from the contents of a VOB. | |
1479 ***************************************************************************** | |
1480 * This function is called by _dvdcss_titlekey to find a title key, if we've | |
1481 * chosen to crack title key instead of decrypting it with the disc key. | |
1482 * The DVD should have been opened and be in an authenticated state. | |
1483 * i_pos is the starting sector, i_len is the maximum number of sectors to read | |
1484 *****************************************************************************/ | |
1485 static int CrackTitleKey( dvdcss_t dvdcss, int i_pos, int i_len, | |
1486 dvd_key_t p_titlekey ) | |
1487 { | |
1488 uint8_t p_buf[ DVDCSS_BLOCK_SIZE ]; | |
1489 const uint8_t p_packstart[4] = { 0x00, 0x00, 0x01, 0xba }; | |
1490 int i_reads = 0; | |
1491 int i_encrypted = 0; | |
1492 int b_stop_scanning = 0; | |
1493 int b_read_error = 0; | |
1494 int i_ret; | |
1495 | |
1496 print_debug( dvdcss, "cracking title key at block %i", i_pos ); | |
1497 | |
1498 i_tries = 0; | |
1499 i_success = 0; | |
1500 | |
1501 do | |
1502 { | |
1503 i_ret = dvdcss->pf_seek( dvdcss, i_pos ); | |
1504 | |
1505 if( i_ret != i_pos ) | |
1506 { | |
1507 print_error( dvdcss, "seek failed" ); | |
1508 } | |
1509 | |
1510 i_ret = dvdcss_read( dvdcss, p_buf, 1, DVDCSS_NOFLAGS ); | |
1511 | |
1512 /* Either we are at the end of the physical device or the auth | |
1513 * have failed / were not done and we got a read error. */ | |
1514 if( i_ret <= 0 ) | |
1515 { | |
1516 if( i_ret == 0 ) | |
1517 { | |
1518 print_debug( dvdcss, "read returned 0 (end of device?)" ); | |
1519 } | |
1520 else if( !b_read_error ) | |
1521 { | |
1522 print_debug( dvdcss, "read error at block %i, resorting to " | |
1523 "secret arcanes to recover", i_pos ); | |
1524 | |
1525 /* Reset the drive before trying to continue */ | |
1526 _dvdcss_close( dvdcss ); | |
1527 _dvdcss_open( dvdcss ); | |
1528 | |
1529 b_read_error = 1; | |
1530 continue; | |
1531 } | |
1532 break; | |
1533 } | |
1534 | |
1535 /* Stop when we find a non MPEG stream block. | |
1536 * (We must have reached the end of the stream). | |
1537 * For now, allow all blocks that begin with a start code. */ | |
1538 if( memcmp( p_buf, p_packstart, 3 ) ) | |
1539 { | |
1540 print_debug( dvdcss, "non MPEG block found at block %i " | |
1541 "(end of title)", i_pos ); | |
1542 break; | |
1543 } | |
1544 | |
1545 if( p_buf[0x0d] & 0x07 ) | |
1546 print_debug( dvdcss, "stuffing in pack header" ); | |
1547 | |
1548 /* PES_scrambling_control does not exist in a system_header, | |
1549 * a padding_stream or a private_stream2 (and others?). */ | |
1550 if( p_buf[0x14] & 0x30 && ! ( p_buf[0x11] == 0xbb | |
1551 || p_buf[0x11] == 0xbe | |
1552 || p_buf[0x11] == 0xbf ) ) | |
1553 { | |
1554 i_encrypted++; | |
1555 | |
1556 if( AttackPattern(p_buf, i_reads, p_titlekey) > 0 ) | |
1557 { | |
1558 b_stop_scanning = 1; | |
1559 } | |
1560 #if 0 | |
1561 if( AttackPadding(p_buf, i_reads, p_titlekey) > 0 ) | |
1562 { | |
1563 b_stop_scanning = 1; | |
1564 } | |
1565 #endif | |
1566 } | |
1567 | |
1568 i_pos++; | |
1569 i_len--; | |
1570 i_reads++; | |
1571 | |
1572 /* Emit a progress indication now and then. */ | |
1573 if( !( i_reads & 0xfff ) ) | |
1574 { | |
1575 print_debug( dvdcss, "at block %i, still cracking...", i_pos ); | |
1576 } | |
1577 | |
1578 /* Stop after 2000 blocks if we haven't seen any encrypted blocks. */ | |
1579 if( i_reads >= 2000 && i_encrypted == 0 ) break; | |
1580 | |
1581 } while( !b_stop_scanning && i_len > 0); | |
1582 | |
1583 if( !b_stop_scanning ) | |
1584 { | |
1585 print_debug( dvdcss, "end of title reached" ); | |
1586 } | |
1587 | |
1588 /* Print some statistics. */ | |
1589 print_debug( dvdcss, "successful attempts %d/%d, scrambled blocks %d/%d", | |
1590 i_success, i_tries, i_encrypted, i_reads ); | |
1591 | |
1592 if( i_success > 0 /* b_stop_scanning */ ) | |
1593 { | |
1594 print_debug( dvdcss, "vts key initialized" ); | |
1595 return 1; | |
1596 } | |
1597 | |
1598 if( i_encrypted == 0 && i_reads > 0 ) | |
1599 { | |
1600 memset( p_titlekey, 0, KEY_SIZE ); | |
1601 print_debug( dvdcss, "no scrambled sectors found" ); | |
1602 return 0; | |
1603 } | |
1604 | |
1605 memset( p_titlekey, 0, KEY_SIZE ); | |
1606 return -1; | |
1607 } | |
1608 | |
1609 | |
1610 /****************************************************************************** | |
1611 * The original Ethan Hawke (DeCSSPlus) attack (modified). | |
1612 ****************************************************************************** | |
1613 * Tries to find a repeating pattern just before the encrypted part starts. | |
1614 * Then it guesses that the plain text for first encrypted bytes are | |
35952 | 1615 * a continuation of that pattern. |
20613 | 1616 *****************************************************************************/ |
1617 static int AttackPattern( uint8_t const p_sec[ DVDCSS_BLOCK_SIZE ], | |
1618 int i_pos, uint8_t *p_key ) | |
1619 { | |
1620 unsigned int i_best_plen = 0; | |
1621 unsigned int i_best_p = 0; | |
1622 unsigned int i, j; | |
1623 | |
1624 /* For all cycle length from 2 to 48 */ | |
1625 for( i = 2 ; i < 0x30 ; i++ ) | |
1626 { | |
1627 /* Find the number of bytes that repeats in cycles. */ | |
1628 for( j = i + 1; | |
1629 j < 0x80 && ( p_sec[0x7F - (j%i)] == p_sec[0x7F - j] ); | |
1630 j++ ) | |
1631 { | |
1632 /* We have found j repeating bytes with a cycle length i. */ | |
1633 if( j > i_best_plen ) | |
1634 { | |
1635 i_best_plen = j; | |
1636 i_best_p = i; | |
1637 } | |
1638 } | |
1639 } | |
1640 | |
1641 /* We need at most 10 plain text bytes?, so a make sure that we | |
1642 * have at least 20 repeated bytes and that they have cycled at | |
1643 * least one time. */ | |
1644 if( ( i_best_plen > 3 ) && ( i_best_plen / i_best_p >= 2) ) | |
1645 { | |
1646 int res; | |
1647 | |
1648 i_tries++; | |
1649 memset( p_key, 0, KEY_SIZE ); | |
1650 res = RecoverTitleKey( 0, &p_sec[0x80], | |
1651 &p_sec[ 0x80 - (i_best_plen / i_best_p) * i_best_p ], | |
1652 &p_sec[0x54] /* key_seed */, p_key ); | |
1653 i_success += ( res >= 0 ); | |
1654 #if 0 | |
1655 if( res >= 0 ) | |
1656 { | |
1657 fprintf( stderr, "key is %02x:%02x:%02x:%02x:%02x ", | |
1658 p_key[0], p_key[1], p_key[2], p_key[3], p_key[4] ); | |
1659 fprintf( stderr, "at block %5d pattern len %3d period %3d %s\n", | |
1660 i_pos, i_best_plen, i_best_p, (res>=0?"y":"n") ); | |
1661 } | |
1662 #endif | |
1663 return ( res >= 0 ); | |
1664 } | |
1665 | |
1666 return 0; | |
1667 } | |
1668 | |
1669 | |
1670 #if 0 | |
1671 /****************************************************************************** | |
1672 * Encrypted Padding_stream attack. | |
1673 ****************************************************************************** | |
1674 * DVD specifies that there must only be one type of data in every sector. | |
1675 * Every sector is one pack and so must obviously be 2048 bytes long. | |
35952 | 1676 * For the last piece of video data before a VOBU boundary there might not |
20613 | 1677 * be exactly the right amount of data to fill a sector. Then one has to |
1678 * pad the pack to 2048 bytes. For just a few bytes this is done in the | |
1679 * header but for any large amount you insert a PES packet from the | |
1680 * Padding stream. This looks like 0x00 00 01 be xx xx ff ff ... | |
1681 * where xx xx is the length of the padding stream. | |
1682 *****************************************************************************/ | |
1683 static int AttackPadding( uint8_t const p_sec[ DVDCSS_BLOCK_SIZE ], | |
1684 int i_pos, uint8_t *p_key ) | |
1685 { | |
1686 unsigned int i_pes_length; | |
1687 /*static int i_tries = 0, i_success = 0;*/ | |
1688 | |
1689 i_pes_length = (p_sec[0x12]<<8) | p_sec[0x13]; | |
1690 | |
35952 | 1691 /* Covered by the test below but useful for debugging. */ |
20613 | 1692 if( i_pes_length == DVDCSS_BLOCK_SIZE - 0x14 ) return 0; |
1693 | |
1694 /* There must be room for at least 4? bytes of padding stream, | |
1695 * and it must be encrypted. | |
1696 * sector size - pack/pes header - padding startcode - padding length */ | |
1697 if( ( DVDCSS_BLOCK_SIZE - 0x14 - 4 - 2 - i_pes_length < 4 ) || | |
1698 ( p_sec[0x14 + i_pes_length + 0] == 0x00 && | |
1699 p_sec[0x14 + i_pes_length + 1] == 0x00 && | |
1700 p_sec[0x14 + i_pes_length + 2] == 0x01 ) ) | |
1701 { | |
1702 fprintf( stderr, "plain %d %02x:%02x:%02x:%02x (type %02x sub %02x)\n", | |
1703 DVDCSS_BLOCK_SIZE - 0x14 - 4 - 2 - i_pes_length, | |
1704 p_sec[0x14 + i_pes_length + 0], | |
1705 p_sec[0x14 + i_pes_length + 1], | |
1706 p_sec[0x14 + i_pes_length + 2], | |
1707 p_sec[0x14 + i_pes_length + 3], | |
1708 p_sec[0x11], p_sec[0x17 + p_sec[0x16]]); | |
1709 return 0; | |
1710 } | |
1711 | |
1712 /* If we are here we know that there is a where in the pack a | |
1713 encrypted PES header is (startcode + length). It's never more | |
1714 than two packets in the pack, so we 'know' the length. The | |
1715 plaintext at offset (0x14 + i_pes_length) will then be | |
1716 00 00 01 e0/bd/be xx xx, in the case of be the following bytes | |
1717 are also known. */ | |
1718 | |
1719 /* An encrypted SPU PES packet with another encrypted PES packet following. | |
35952 | 1720 Normally if the following was a padding stream that would be in plain |
20613 | 1721 text. So it will be another SPU PES packet. */ |
1722 if( p_sec[0x11] == 0xbd && | |
1723 p_sec[0x17 + p_sec[0x16]] >= 0x20 && | |
1724 p_sec[0x17 + p_sec[0x16]] <= 0x3f ) | |
1725 { | |
1726 i_tries++; | |
1727 } | |
1728 | |
1729 /* A Video PES packet with another encrypted PES packet following. | |
35952 | 1730 * No reason except for time stamps to break the data into two packets. |
20613 | 1731 * So it's likely that the following PES packet is a padding stream. */ |
1732 if( p_sec[0x11] == 0xe0 ) | |
1733 { | |
1734 i_tries++; | |
1735 } | |
1736 | |
1737 if( 1 ) | |
1738 { | |
1739 /*fprintf( stderr, "key is %02x:%02x:%02x:%02x:%02x ", | |
1740 p_key[0], p_key[1], p_key[2], p_key[3], p_key[4] );*/ | |
1741 fprintf( stderr, "at block %5d padding len %4d " | |
1742 "type %02x sub %02x\n", i_pos, i_pes_length, | |
1743 p_sec[0x11], p_sec[0x17 + p_sec[0x16]]); | |
1744 } | |
1745 | |
1746 return 0; | |
1747 } | |
1748 #endif |