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