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