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