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