1 /*****************************************************************************
2 * css.c: Functions for DVD authentification and unscrambling
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: css.c,v 1.20 2002/01/14 22:06:57 stef Exp $
7 * Author: Stéphane Borel <stef@via.ecp.fr>
8 * Håkan Hjort <d95hjort@dtek.chalmers.se>
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
16 * see http://www.lemuria.org/DeCSS/ by Tom Vogt for more information.
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.
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.
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 *****************************************************************************/
33 /*****************************************************************************
35 *****************************************************************************/
41 #include <videolan/vlc.h>
43 #include "videolan/dvdcss.h"
44 #include "libdvdcss.h"
46 #include "csstables.h"
54 /*****************************************************************************
56 *****************************************************************************/
57 static int CSSGetASF ( dvdcss_handle dvdcss );
58 static void CSSCryptKey ( int i_key_type, int i_varient,
59 u8 const * p_challenge, u8* p_key );
60 static void CSSDecryptKey( u8* p_crypted, u8* p_key, u8 );
61 static int CSSDiscCrack ( dvdcss_handle dvdcss, u8 * p_disc_key );
62 static int CSSTitleCrack( int i_start, unsigned char * p_crypted,
63 unsigned char * p_decrypted,
64 dvd_key_t * p_sector_key, dvd_key_t * p_key );
66 /*****************************************************************************
67 * CSSTest : check if the disc is encrypted or not
68 *****************************************************************************/
69 int CSSTest( dvdcss_handle dvdcss )
71 int i_ret, i_copyright;
73 i_ret = ioctl_ReadCopyright( dvdcss->i_fd, 0 /* i_layer */, &i_copyright );
77 /* Since it's the first ioctl we try to issue, we add a notice */
78 _dvdcss_error( dvdcss, "css error: ioctl_ReadCopyright failed, "
79 "make sure there is a DVD in the drive, and that "
80 "DVD ioctls were compiled in this libdvdcss version" );
88 /*****************************************************************************
89 * CSSAuth : CSS Structure initialisation and DVD authentication.
90 *****************************************************************************
91 * It simulates the mutual authentication between logical unit and host.
92 * Since we don't need the disc key to find the title key, we just run the
93 * basic unavoidable commands to authenticate device and disc.
94 *****************************************************************************/
95 int CSSAuth( dvdcss_handle dvdcss )
97 /* structures defined in cdrom.h or dvdio.h */
98 unsigned char p_buffer[10];
103 dvdcss->css.i_agid = 0;
105 /* Test authentication success */
106 switch( CSSGetASF( dvdcss ) )
112 _dvdcss_debug( dvdcss, "already authenticated" );
116 _dvdcss_debug( dvdcss, "need to authenticate" );
120 /* Init sequence, request AGID */
121 for( i = 1; i < 4 ; ++i )
123 snprintf( psz_warning, sizeof(psz_warning), "requesting AGID %d", i );
124 _dvdcss_debug( dvdcss, psz_warning );
126 i_ret = ioctl_ReportAgid( dvdcss->i_fd, &dvdcss->css.i_agid );
130 /* No error during ioctl: we know the device is authenticated */
134 _dvdcss_error( dvdcss, "ioctl_ReportAgid failed, invalidating" );
136 dvdcss->css.i_agid = 0;
137 ioctl_InvalidateAgid( dvdcss->i_fd, &dvdcss->css.i_agid );
140 /* Unable to authenticate without AGID */
143 _dvdcss_error( dvdcss, "ioctl_ReportAgid failed, fatal" );
147 for( i = 0 ; i < 10; ++i )
149 dvdcss->css.disc.p_challenge[i] = i;
152 /* Get challenge from host */
153 for( i = 0 ; i < 10 ; ++i )
155 p_buffer[9-i] = dvdcss->css.disc.p_challenge[i];
158 /* Send challenge to LU */
159 if( ioctl_SendChallenge( dvdcss->i_fd, &dvdcss->css.i_agid, p_buffer ) < 0 )
161 _dvdcss_error( dvdcss, "ioctl_SendChallenge failed" );
165 /* Get key1 from LU */
166 if( ioctl_ReportKey1( dvdcss->i_fd, &dvdcss->css.i_agid, p_buffer ) < 0)
168 _dvdcss_error( dvdcss, "ioctl_ReportKey1 failed" );
172 /* Send key1 to host */
173 for( i = 0 ; i < KEY_SIZE ; i++ )
175 dvdcss->css.disc.p_key1[i] = p_buffer[4-i];
178 for( i = 0 ; i < 32 ; ++i )
180 CSSCryptKey( 0, i, dvdcss->css.disc.p_challenge,
181 dvdcss->css.disc.p_key_check );
183 if( memcmp( dvdcss->css.disc.p_key_check,
184 dvdcss->css.disc.p_key1, KEY_SIZE ) == 0 )
186 snprintf( psz_warning, sizeof(psz_warning),
187 "drive authentic, using variant %d", i );
188 _dvdcss_debug( dvdcss, psz_warning );
189 dvdcss->css.disc.i_varient = i;
196 _dvdcss_error( dvdcss, "drive would not authenticate" );
200 /* Get challenge from LU */
201 if( ioctl_ReportChallenge( dvdcss->i_fd, &dvdcss->css.i_agid, p_buffer ) < 0 )
203 _dvdcss_error( dvdcss, "ioctl_ReportKeyChallenge failed" );
207 /* Send challenge to host */
208 for( i = 0 ; i < 10 ; ++i )
210 dvdcss->css.disc.p_challenge[i] = p_buffer[9-i];
213 CSSCryptKey( 1, dvdcss->css.disc.i_varient,
214 dvdcss->css.disc.p_challenge,
215 dvdcss->css.disc.p_key2 );
217 /* Get key2 from host */
218 for( i = 0 ; i < KEY_SIZE ; ++i )
220 p_buffer[4-i] = dvdcss->css.disc.p_key2[i];
223 /* Send key2 to LU */
224 if( ioctl_SendKey2( dvdcss->i_fd, &dvdcss->css.i_agid, p_buffer ) < 0 )
226 _dvdcss_error( dvdcss, "ioctl_SendKey2 failed" );
230 _dvdcss_debug( dvdcss, "authentication established" );
232 memcpy( dvdcss->css.disc.p_challenge,
233 dvdcss->css.disc.p_key1, KEY_SIZE );
234 memcpy( dvdcss->css.disc.p_challenge+KEY_SIZE,
235 dvdcss->css.disc.p_key2, KEY_SIZE );
237 CSSCryptKey( 2, dvdcss->css.disc.i_varient,
238 dvdcss->css.disc.p_challenge,
239 dvdcss->css.disc.p_key_check );
241 _dvdcss_debug( dvdcss, "received session key" );
243 if( dvdcss->css.i_agid < 0 )
248 /* Test authentication success */
249 switch( CSSGetASF( dvdcss ) )
255 _dvdcss_debug( dvdcss, "already authenticated" );
259 _dvdcss_debug( dvdcss, "need to get disc key" );
266 /*****************************************************************************
267 * CSSGetDiscKey : get disc key and optionnaly decrypts it.
268 *****************************************************************************
269 * This function should only be called if DVD ioctls are present.
270 * Two decryption methods are then offered:
271 * -disc key hash crack,
272 * -decryption with player keys if they are available.
273 *****************************************************************************/
274 int CSSGetDiscKey( dvdcss_handle dvdcss )
276 unsigned char p_buffer[2048 + 4 + 1];
283 if( CSSAuth( dvdcss ) )
288 /* Get encrypted disc key */
289 if( ioctl_ReadDiscKey( dvdcss->i_fd, &dvdcss->css.i_agid, p_buffer ) < 0 )
291 _dvdcss_error( dvdcss, "ioctl_ReadDiscKey failed" );
295 /* Unencrypt disc key using bus key */
296 for( i = 0 ; i < 2048 ; i++ )
298 p_buffer[ i ] ^= dvdcss->css.disc.p_key_check[ 4 - (i % KEY_SIZE) ];
300 memcpy( dvdcss->css.disc.p_disc_key, p_buffer, 2048 );
302 switch( dvdcss->i_method )
304 case DVDCSS_METHOD_KEY:
306 /* Decrypt disc key with player keys from csskeys.h */
307 _dvdcss_debug( dvdcss, "decrypting disc key with player keys" );
311 /* Take encrypted disc key and decrypt it */
313 dvdcss->css.disc.p_disc_key
314 + playerkeys[i].i_offset,
316 CSSDecryptKey( disc_key, playerkeys[i].p_key, 0 );
318 /* Encrypt disc key hash with disc key to
319 * check we have disc key */
320 memcpy( test_key, dvdcss->css.disc.p_disc_key, KEY_SIZE );
321 CSSDecryptKey( test_key, disc_key, 0);
325 } while( ( playerkeys[i].i_offset != -1 ) &&
326 ( memcmp( test_key, disc_key, KEY_SIZE ) ) );
328 /* The decrypted disk key will replace the disk key hash */
329 memcpy( dvdcss->css.disc.p_disc_key, disc_key, KEY_SIZE );
332 dvdcss->i_method = DVDCSS_METHOD_DISC;
334 case DVDCSS_METHOD_DISC:
335 /* Crack Disc key to be able to use it */
336 _dvdcss_debug( dvdcss, "cracking disc key with key hash" );
337 _dvdcss_debug( dvdcss, "building 64MB table ... this will take some time" );
338 CSSDiscCrack( dvdcss, dvdcss->css.disc.p_disc_key );
342 _dvdcss_debug( dvdcss, "disc key won't be decrypted" );
349 /*****************************************************************************
350 * CSSGetTitleKey : get title key.
351 *****************************************************************************/
352 int CSSGetTitleKey( dvdcss_handle dvdcss, int i_pos )
357 if( ( dvdcss->i_method == DVDCSS_METHOD_TITLE )
358 || ( dvdcss->b_ioctls == 0 ) )
361 * Title key cracking method from Ethan Hawke,
362 * with Frank A. Stevenson algorithm.
363 * Does not use any player key table and ioctls.
366 u8 p_packstart[4] = { 0x00, 0x00, 0x01, 0xba };
367 boolean_t b_encrypted;
368 boolean_t b_stop_scanning;
373 _dvdcss_debug( dvdcss, "cracking title key ... this may take some time" );
375 for( i = 0 ; i < KEY_SIZE ; i++ )
386 i_pos = _dvdcss_seek( dvdcss, i_pos );
387 if( _dvdcss_read( dvdcss, p_buf, 1 ) != 1 ) break;
389 /* Stop when we find a non MPEG stream block */
390 if( memcmp( p_buf, p_packstart, 4 ) )
392 /* The title is unencrypted */
395 /* dvdcss some times fail to find/crack the key,
396 hope that it's the same as the one in the next title
397 _dvdcss_debug( dvdcss, "no key found at end of title" );
401 /* PES_scrambling_control on and make sure that the packet type
402 is one that can be scrambled */
403 if( p_buf[0x14] & 0x30 && ! ( p_buf[0x11] == 0xbb
404 || p_buf[0x11] == 0xbe
405 || p_buf[0x11] == 0xbf ) )
411 for( i = 2 ; i < 0x30 ; i++ )
414 j < 0x80 && ( p_buf[0x7F - (j%i)] == p_buf[0x7F-j] );
417 if( j > i_best_plen )
425 if( ( i_best_plen > 20 ) && ( i_best_plen / i_best_p >= 2) )
427 i = CSSTitleCrack( 0, &p_buf[0x80],
428 &p_buf[0x80 - ( i_best_plen / i_best_p) *i_best_p],
429 (dvd_key_t*)&p_buf[0x54],
431 b_stop_scanning = ( i >= 0 );
438 /* If we haven't seen any encrypted ones after 3000 blocks stop */
439 if( !b_encrypted && i_blocks_read >= 1000 ) break;
441 } while( !b_stop_scanning );
443 if( b_stop_scanning )
445 memcpy( dvdcss->css.p_title_key, &p_key, sizeof(dvd_key_t) );
446 _dvdcss_debug( dvdcss, "vts key initialized" );
452 _dvdcss_debug( dvdcss, "file was unscrambled" );
461 * if we are here we have a decrypted disc key and ioctls are available
462 * so we can read the title key and decrypt it.
465 _dvdcss_debug( dvdcss, "decrypting title key with disc key" );
467 /* We need to authenticate again for every key
468 * (to get a new session key ?) */
471 /* Get encrypted title key */
472 if( ioctl_ReadTitleKey( dvdcss->i_fd, &dvdcss->css.i_agid,
475 _dvdcss_error( dvdcss, "ioctl_ReadTitleKey failed" );
479 if( memcmp( p_key, dvdcss->css.p_unenc_key, KEY_SIZE ) )
481 memcpy( dvdcss->css.p_unenc_key, p_key, KEY_SIZE );
483 /* Unencrypt title key using bus key */
484 for( i = 0 ; i < KEY_SIZE ; i++ )
486 p_key[ i ] ^= dvdcss->css.disc.p_key_check[ 4 - (i % KEY_SIZE ) ];
489 /* Title key decryption needs one inversion 0xff */
490 CSSDecryptKey( p_key, dvdcss->css.disc.p_disc_key, 0xff );
492 memcpy( dvdcss->css.p_title_key, p_key, KEY_SIZE );
496 } // (dvdcss->i_method == DVDCSS_METHOD_TITLE) || (dvdcss->b_ioctls == 0)
499 /*****************************************************************************
500 * CSSDescrambleSector: does the actual descrambling of data
501 *****************************************************************************
502 * sec : sector to descramble
503 * key : title key for this sector
504 *****************************************************************************/
505 int CSSDescrambleSector( dvd_key_t p_key, u8* p_sec )
507 unsigned int i_t1, i_t2, i_t3, i_t4, i_t5, i_t6;
508 u8* p_end = p_sec + 0x800;
510 /* PES_scrambling_control */
511 if( p_sec[0x14] & 0x30)
513 i_t1 = ((p_key)[0] ^ p_sec[0x54]) | 0x100;
514 i_t2 = (p_key)[1] ^ p_sec[0x55];
515 i_t3 = (((p_key)[2]) | ((p_key)[3] << 8) |
516 ((p_key)[4] << 16)) ^ ((p_sec[0x56]) |
517 (p_sec[0x57] << 8) | (p_sec[0x58] << 16));
519 i_t3 = i_t3 * 2 + 8 - i_t4;
523 while( p_sec != p_end )
525 i_t4 = p_css_tab2[i_t2] ^ p_css_tab3[i_t1];
527 i_t1 = ( ( i_t1 & 1 ) << 8 ) ^ i_t4;
528 i_t4 = p_css_tab5[i_t4];
529 i_t6 = ((((((( i_t3 >> 3 ) ^ i_t3 ) >> 1 ) ^
530 i_t3 ) >> 8 ) ^ i_t3 ) >> 5 ) & 0xff;
531 i_t3 = (i_t3 << 8 ) | i_t6;
532 i_t6 = p_css_tab4[i_t6];
534 *p_sec = p_css_tab1[*p_sec] ^( i_t5 & 0xff );
543 /* Following functions are local */
545 /*****************************************************************************
546 * CSSGetASF : Get Authentification success flag
547 *****************************************************************************
550 * 0 if the device needs to be authenticated,
552 *****************************************************************************/
553 static int CSSGetASF( dvdcss_handle dvdcss )
558 for( i_agid = 0 ; i_agid < 4 ; i_agid++ )
560 if( ioctl_ReportASF( dvdcss->i_fd, &i_agid, &i_asf ) == 0 )
564 _dvdcss_debug( dvdcss, "GetASF authenticated" );
568 _dvdcss_debug( dvdcss, "GetASF not authenticated" );
575 /* The ioctl process has failed */
576 _dvdcss_error( dvdcss, "GetASF fatal error" );
580 /*****************************************************************************
581 * CSSCryptKey : shuffles bits and unencrypt keys.
582 *****************************************************************************
583 * Used during authentication and disc key negociation in CSSAuth.
584 * i_key_type : 0->key1, 1->key2, 2->buskey.
585 * i_varient : between 0 and 31.
586 *****************************************************************************/
587 static void CSSCryptKey( int i_key_type, int i_varient,
588 u8 const * p_challenge, u8* p_key )
590 /* Permutation table for challenge */
591 u8 pp_perm_challenge[3][10] =
592 { { 1, 3, 0, 7, 5, 2, 9, 6, 4, 8 },
593 { 6, 1, 9, 3, 8, 5, 7, 4, 0, 2 },
594 { 4, 0, 3, 5, 7, 2, 8, 6, 1, 9 } };
596 /* Permutation table for varient table for key2 and buskey */
597 u8 pp_perm_varient[2][32] =
598 { { 0x0a, 0x08, 0x0e, 0x0c, 0x0b, 0x09, 0x0f, 0x0d,
599 0x1a, 0x18, 0x1e, 0x1c, 0x1b, 0x19, 0x1f, 0x1d,
600 0x02, 0x00, 0x06, 0x04, 0x03, 0x01, 0x07, 0x05,
601 0x12, 0x10, 0x16, 0x14, 0x13, 0x11, 0x17, 0x15 },
602 { 0x12, 0x1a, 0x16, 0x1e, 0x02, 0x0a, 0x06, 0x0e,
603 0x10, 0x18, 0x14, 0x1c, 0x00, 0x08, 0x04, 0x0c,
604 0x13, 0x1b, 0x17, 0x1f, 0x03, 0x0b, 0x07, 0x0f,
605 0x11, 0x19, 0x15, 0x1d, 0x01, 0x09, 0x05, 0x0d } };
608 { 0xB7, 0x74, 0x85, 0xD0, 0xCC, 0xDB, 0xCA, 0x73,
609 0x03, 0xFE, 0x31, 0x03, 0x52, 0xE0, 0xB7, 0x42,
610 0x63, 0x16, 0xF2, 0x2A, 0x79, 0x52, 0xFF, 0x1B,
611 0x7A, 0x11, 0xCA, 0x1A, 0x9B, 0x40, 0xAD, 0x01 };
613 /* The "secret" key */
614 u8 p_secret[5] = { 0x55, 0xD6, 0xC4, 0xC5, 0x28 };
620 u8 i_lfsr0_o; /* 1 bit used */
621 u8 i_lfsr1_o; /* 1 bit used */
634 for (i = 9; i >= 0; --i)
635 p_scratch[i] = p_challenge[pp_perm_challenge[i_key_type][i]];
637 i_css_varient = ( i_key_type == 0 ) ? i_varient :
638 pp_perm_varient[i_key_type-1][i_varient];
641 * This encryption engine implements one of 32 variations
642 * one the same theme depending upon the choice in the
643 * varient parameter (0 - 31).
645 * The algorithm itself manipulates a 40 bit input into
647 * The parameter 'input' is 80 bits. It consists of
648 * the 40 bit input value that is to be encrypted followed
649 * by a 40 bit seed value for the pseudo random number
653 /* Feed the secret into the input values such that
654 * we alter the seed to the LFSR's used above, then
655 * generate the bits to play with.
657 for( i = 5 ; --i >= 0 ; )
659 p_tmp1[i] = p_scratch[5 + i] ^ p_secret[i] ^ p_crypt_tab2[i];
663 * We use two LFSR's (seeded from some of the input data bytes) to
664 * generate two streams of pseudo-random bits. These two bit streams
665 * are then combined by simply adding with carry to generate a final
666 * sequence of pseudo-random bits which is stored in the buffer that
667 * 'output' points to the end of - len is the size of this buffer.
669 * The first LFSR is of degree 25, and has a polynomial of:
670 * x^13 + x^5 + x^4 + x^1 + 1
672 * The second LSFR is of degree 17, and has a (primitive) polynomial of:
675 * I don't know if these polynomials are primitive modulo 2, and thus
676 * represent maximal-period LFSR's.
679 * Note that we take the output of each LFSR from the new shifted in
680 * bit, not the old shifted out bit. Thus for ease of use the LFSR's
681 * are implemented in bit reversed order.
685 /* In order to ensure that the LFSR works we need to ensure that the
686 * initial values are non-zero. Thus when we initialise them from
687 * the seed, we ensure that a bit is set.
689 i_lfsr0 = ( p_tmp1[0] << 17 ) | ( p_tmp1[1] << 9 ) |
690 (( p_tmp1[2] & ~7 ) << 1 ) | 8 | ( p_tmp1[2] & 7 );
691 i_lfsr1 = ( p_tmp1[3] << 9 ) | 0x100 | p_tmp1[4];
693 i_index = sizeof(p_bits);
698 for( i_bit = 0, i_val = 0 ; i_bit < 8 ; ++i_bit )
701 i_lfsr0_o = ( ( i_lfsr0 >> 24 ) ^ ( i_lfsr0 >> 21 ) ^
702 ( i_lfsr0 >> 20 ) ^ ( i_lfsr0 >> 12 ) ) & 1;
703 i_lfsr0 = ( i_lfsr0 << 1 ) | i_lfsr0_o;
705 i_lfsr1_o = ( ( i_lfsr1 >> 16 ) ^ ( i_lfsr1 >> 2 ) ) & 1;
706 i_lfsr1 = ( i_lfsr1 << 1 ) | i_lfsr1_o;
708 i_combined = !i_lfsr1_o + i_carry + !i_lfsr0_o;
710 i_carry = ( i_combined >> 1 ) & 1;
711 i_val |= ( i_combined & 1 ) << i_bit;
714 p_bits[--i_index] = i_val;
715 } while( i_index > 0 );
717 /* This term is used throughout the following to
718 * select one of 32 different variations on the
721 i_cse = p_varients[i_css_varient] ^ p_crypt_tab2[i_css_varient];
723 /* Now the actual blocks doing the encryption. Each
724 * of these works on 40 bits at a time and are quite
728 for( i = 5, i_term = 0 ; --i >= 0 ; i_term = p_scratch[i] )
730 i_index = p_bits[25 + i] ^ p_scratch[i];
731 i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse;
733 p_tmp1[i] = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term;
735 p_tmp1[4] ^= p_tmp1[0];
737 for( i = 5, i_term = 0 ; --i >= 0 ; i_term = p_tmp1[i] )
739 i_index = p_bits[20 + i] ^ p_tmp1[i];
740 i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse;
742 p_tmp2[i] = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term;
744 p_tmp2[4] ^= p_tmp2[0];
746 for( i = 5, i_term = 0 ; --i >= 0 ; i_term = p_tmp2[i] )
748 i_index = p_bits[15 + i] ^ p_tmp2[i];
749 i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse;
750 i_index = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term;
752 p_tmp1[i] = p_crypt_tab0[i_index] ^ p_crypt_tab2[i_index];
754 p_tmp1[4] ^= p_tmp1[0];
756 for( i = 5, i_term = 0 ; --i >= 0 ; i_term = p_tmp1[i] )
758 i_index = p_bits[10 + i] ^ p_tmp1[i];
759 i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse;
761 i_index = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term;
763 p_tmp2[i] = p_crypt_tab0[i_index] ^ p_crypt_tab2[i_index];
765 p_tmp2[4] ^= p_tmp2[0];
767 for( i = 5, i_term = 0 ; --i >= 0 ; i_term = p_tmp2[i] )
769 i_index = p_bits[5 + i] ^ p_tmp2[i];
770 i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse;
772 p_tmp1[i] = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term;
774 p_tmp1[4] ^= p_tmp1[0];
776 for(i = 5, i_term = 0 ; --i >= 0 ; i_term = p_tmp1[i] )
778 i_index = p_bits[i] ^ p_tmp1[i];
779 i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse;
781 p_key[i] = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term;
787 /*****************************************************************************
788 * CSSDecryptKey: decrypt p_crypted with p_key.
789 *****************************************************************************
790 * Decryption is slightly dependant on the type of key:
791 * -for disc key, invert is 0x00,
792 * -for title key, invert if 0xff.
793 *****************************************************************************/
794 static void CSSDecryptKey( u8* p_crypted, u8* p_key, u8 invert )
796 unsigned int i_lfsr1_lo;
797 unsigned int i_lfsr1_hi;
798 unsigned int i_lfsr0;
799 unsigned int i_combined;
805 i_lfsr1_lo = p_key[0] | 0x100;
806 i_lfsr1_hi = p_key[1];
808 i_lfsr0 = ( ( p_key[4] << 17 )
810 | ( p_key[2] << 1 ) )
811 + 8 - ( p_key[2] & 7 );
812 i_lfsr0 = ( p_css_tab4[i_lfsr0 & 0xff] << 24 ) |
813 ( p_css_tab4[( i_lfsr0 >> 8 ) & 0xff] << 16 ) |
814 ( p_css_tab4[( i_lfsr0 >> 16 ) & 0xff] << 8 ) |
815 p_css_tab4[( i_lfsr0 >> 24 ) & 0xff];
818 for( i = 0 ; i < KEY_SIZE ; ++i )
820 o_lfsr1 = p_css_tab2[i_lfsr1_hi] ^ p_css_tab3[i_lfsr1_lo];
821 i_lfsr1_hi = i_lfsr1_lo >> 1;
822 i_lfsr1_lo = ( ( i_lfsr1_lo & 1 ) << 8 ) ^ o_lfsr1;
823 o_lfsr1 = p_css_tab4[o_lfsr1];
825 o_lfsr0 = ((((((( i_lfsr0 >> 8 ) ^ i_lfsr0 ) >> 1 )
826 ^ i_lfsr0 ) >> 3 ) ^ i_lfsr0 ) >> 7 );
827 i_lfsr0 = ( i_lfsr0 >> 8 ) | ( o_lfsr0 << 24 );
829 i_combined += ( o_lfsr0 ^ invert ) + o_lfsr1;
830 k[i] = i_combined & 0xff;
834 p_crypted[4] = k[4] ^ p_css_tab1[p_crypted[4]] ^ p_crypted[3];
835 p_crypted[3] = k[3] ^ p_css_tab1[p_crypted[3]] ^ p_crypted[2];
836 p_crypted[2] = k[2] ^ p_css_tab1[p_crypted[2]] ^ p_crypted[1];
837 p_crypted[1] = k[1] ^ p_css_tab1[p_crypted[1]] ^ p_crypted[0];
838 p_crypted[0] = k[0] ^ p_css_tab1[p_crypted[0]] ^ p_crypted[4];
840 p_crypted[4] = k[4] ^ p_css_tab1[p_crypted[4]] ^ p_crypted[3];
841 p_crypted[3] = k[3] ^ p_css_tab1[p_crypted[3]] ^ p_crypted[2];
842 p_crypted[2] = k[2] ^ p_css_tab1[p_crypted[2]] ^ p_crypted[1];
843 p_crypted[1] = k[1] ^ p_css_tab1[p_crypted[1]] ^ p_crypted[0];
844 p_crypted[0] = k[0] ^ p_css_tab1[p_crypted[0]];
849 /*****************************************************************************
850 * CSSDiscCrack: brute force disc key
851 * CSS hash reversal function designed by Frank Stevenson
852 *****************************************************************************
853 * This function uses a big amount of memory to crack the disc key from the
854 * disc key hash, if player keys are not available.
855 *****************************************************************************/
856 #define K1TABLEWIDTH 10
859 * Simple function to test if a candidate key produces the given hash
861 static int investigate( unsigned char* hash, unsigned char *ckey )
863 unsigned char key[5];
864 unsigned char pkey[5];
866 memcpy( key, hash, 5 );
867 memcpy( pkey, ckey, 5 );
869 CSSDecryptKey( key, pkey, 0 );
871 return memcmp( key, pkey, 5 );
874 static int CSSDiscCrack( dvdcss_handle dvdcss, u8 * p_disc_key )
876 unsigned char B[5] = { 0,0,0,0,0 }; /* Second Stage of mangle cipher */
877 unsigned char C[5] = { 0,0,0,0,0 }; /* Output Stage of mangle cipher
879 unsigned char k[5] = { 0,0,0,0,0 }; /* Mangling cipher key
880 * Also output from CSS( C ) */
881 unsigned char out1[5]; /* five first output bytes of LFSR1 */
882 unsigned char out2[5]; /* five first output bytes of LFSR2 */
883 unsigned int lfsr1a; /* upper 9 bits of LFSR1 */
884 unsigned int lfsr1b; /* lower 8 bits of LFSR1 */
885 unsigned int tmp, tmp2, tmp3, tmp4,tmp5;
887 unsigned int nStepA; /* iterator for LFSR1 start state */
888 unsigned int nStepB; /* iterator for possible B[0] */
889 unsigned int nTry; /* iterator for K[1] possibilities */
890 unsigned int nPossibleK1; /* #of possible K[1] values */
891 unsigned char* K1table; /* Lookup table for possible K[1] */
892 unsigned int* BigTable; /* LFSR2 startstate indexed by
893 * 1,2,5 output byte */
896 * Prepare tables for hash reversal
900 /* initialize lookup tables for k[1] */
901 K1table = malloc( 65536 * K1TABLEWIDTH );
902 memset( K1table, 0 , 65536 * K1TABLEWIDTH );
903 if( K1table == NULL )
908 tmp = p_disc_key[0] ^ p_css_tab1[ p_disc_key[1] ];
909 for( i = 0 ; i < 256 ; i++ ) /* k[1] */
911 tmp2 = p_css_tab1[ tmp ^ i ]; /* p_css_tab1[ B[1] ]*/
913 for( j = 0 ; j < 256 ; j++ ) /* B[0] */
915 tmp3 = j ^ tmp2 ^ i; /* C[1] */
916 tmp4 = K1table[ K1TABLEWIDTH * ( 256 * j + tmp3 ) ]; /* count of entries here */
919 if( tmp4 == K1TABLEWIDTH )
921 _dvdcss_debug( dvdcss, "Table disaster %d", tmp4 );
924 if( tmp4 < K1TABLEWIDTH )
926 K1table[ K1TABLEWIDTH * ( 256 * j + tmp3 ) + tmp4 ] = i;
928 K1table[ K1TABLEWIDTH * ( 256 * j + tmp3 ) ] = tmp4;
932 /* Initing our Really big table */
933 BigTable = malloc( 16777216 * sizeof(int) );
934 memset( BigTable, 0 , 16777216 * sizeof(int) );
935 if( BigTable == NULL )
942 _dvdcss_debug( dvdcss, "initializing the big table" );
944 for( i = 0 ; i < 16777216 ; i++ )
947 if( ( i & 0x07ffff ) == 0 )
949 fprintf( stderr, "#" );
952 tmp = (( i + i ) & 0x1fffff0 ) | 0x8 | ( i & 0x7 );
954 for( j = 0 ; j < 5 ; j++ )
956 tmp2=((((((( tmp >> 3 ) ^ tmp ) >> 1 ) ^ tmp ) >> 8 )
957 ^ tmp ) >> 5 ) & 0xff;
958 tmp = ( tmp << 8) | tmp2;
959 out2[j] = p_css_tab4[ tmp2 ];
962 j = ( out2[0] << 16 ) | ( out2[1] << 8 ) | out2[4];
966 /* fprintf( stderr, "\n" ); */
969 * We are done initing, now reverse hash
971 tmp5 = p_disc_key[0] ^ p_css_tab1[ p_disc_key[1] ];
973 for( nStepA = 0 ; nStepA < 65536 ; nStepA ++ )
975 lfsr1a = 0x100 | ( nStepA >> 8 );
976 lfsr1b = nStepA & 0xff;
978 /* Generate 5 first output bytes from lfsr1 */
979 for( i = 0 ; i < 5 ; i++ )
981 tmp = p_css_tab2[ lfsr1b ] ^ p_css_tab3[ lfsr1a ];
982 lfsr1b = lfsr1a >> 1;
983 lfsr1a = ((lfsr1a&1)<<8) ^ tmp;
984 out1[ i ] = p_css_tab4[ tmp ];
987 /* cumpute and cache some variables */
989 C[1] = nStepA & 0xff;
990 tmp = p_disc_key[3] ^ p_css_tab1[ p_disc_key[4] ];
991 tmp2 = p_css_tab1[ p_disc_key[0] ];
993 /* Search through all possible B[0] */
994 for( nStepB = 0 ; nStepB < 256 ; nStepB++ )
996 /* reverse parts of the mangling cipher */
998 k[0] = p_css_tab1[ B[0] ] ^ C[0];
999 B[4] = B[0] ^ k[0] ^ tmp2;
1001 nPossibleK1 = K1table[ K1TABLEWIDTH * (256 * B[0] + C[1]) ];
1003 /* Try out all possible values for k[1] */
1004 for( nTry = 0 ; nTry < nPossibleK1 ; nTry++ )
1006 k[1] = K1table[ K1TABLEWIDTH * (256 * B[0] + C[1]) + nTry + 1 ];
1009 /* reconstruct output from LFSR2 */
1010 tmp3 = ( 0x100 + k[0] - out1[0] );
1011 out2[0] = tmp3 & 0xff;
1012 tmp3 = tmp3 & 0x100 ? 0x100 : 0xff;
1013 tmp3 = ( tmp3 + k[1] - out1[1] );
1014 out2[1] = tmp3 & 0xff;
1015 tmp3 = ( 0x100 + k[4] - out1[4] );
1016 out2[4] = tmp3 & 0xff; /* Can be 1 off */
1018 /* test first possible out2[4] */
1019 tmp4 = ( out2[0] << 16 ) | ( out2[1] << 8 ) | out2[4];
1020 tmp4 = BigTable[ tmp4 ];
1022 C[3] = ( tmp4 >> 8 ) & 0xff;
1023 C[4] = ( tmp4 >> 16 ) & 0xff;
1024 B[3] = p_css_tab1[ B[4] ] ^ k[4] ^ C[4];
1025 k[3] = p_disc_key[2] ^ p_css_tab1[ p_disc_key[3] ] ^ B[3];
1026 B[2] = p_css_tab1[ B[3] ] ^ k[3] ^ C[3];
1027 k[2] = p_disc_key[1] ^ p_css_tab1[ p_disc_key[2] ] ^ B[2];
1029 if( ( B[1] ^ p_css_tab1[ B[2] ] ^ k[ 2 ] ) == C[ 2 ] )
1031 if( ! investigate( &p_disc_key[0] , &C[0] ) )
1037 /* Test second possible out2[4] */
1038 out2[4] = ( out2[4] + 0xff ) & 0xff;
1039 tmp4 = ( out2[0] << 16 ) | ( out2[1] << 8 ) | out2[4];
1040 tmp4 = BigTable[ tmp4 ];
1042 C[3] = ( tmp4 >> 8 ) & 0xff;
1043 C[4] = ( tmp4 >> 16 ) & 0xff;
1044 B[3] = p_css_tab1[ B[4] ] ^ k[4] ^ C[4];
1045 k[3] = p_disc_key[2] ^ p_css_tab1[ p_disc_key[3] ] ^ B[3];
1046 B[2] = p_css_tab1[ B[3] ] ^ k[3] ^ C[3];
1047 k[2] = p_disc_key[1] ^ p_css_tab1[ p_disc_key[2] ] ^ B[2];
1049 if( ( B[1] ^ p_css_tab1[ B[2] ] ^ k[ 2 ] ) == C[ 2 ] )
1051 if( ! investigate( &p_disc_key[0] , &C[0] ) )
1062 memcpy( p_disc_key, &C[0], KEY_SIZE );
1070 /*****************************************************************************
1071 * CSSTitleCrack : title key decryption by cracking
1072 * Function designed by Frank Stevenson
1073 *****************************************************************************
1074 * This function is called by CSSGetTitleKey to find a title key, if we've
1075 * chosen to crack title key instead of decrypting it with the disc key.
1076 *****************************************************************************/
1077 static int CSSTitleCrack( int i_start,
1078 unsigned char * p_crypted,
1079 unsigned char * p_decrypted,
1080 dvd_key_t * p_sector_key,
1083 unsigned char p_buffer[10];
1084 unsigned int i_t1, i_t2, i_t3, i_t4, i_t5, i_t6;
1086 unsigned int i_candidate;
1091 for( i = 0 ; i < 10 ; i++ )
1093 p_buffer[i] = p_css_tab1[p_crypted[i]] ^ p_decrypted[i];
1096 for( i_try = i_start ; i_try < 0x10000 ; i_try++ )
1098 i_t1 = i_try >> 8 | 0x100;
1099 i_t2 = i_try & 0xff;
1100 i_t3 = 0; /* not needed */
1103 /* iterate cipher 4 times to reconstruct LFSR2 */
1104 for( i = 0 ; i < 4 ; i++ )
1106 /* advance LFSR1 normaly */
1107 i_t4 = p_css_tab2[i_t2] ^ p_css_tab3[i_t1];
1109 i_t1 = ( ( i_t1 & 1 ) << 8 ) ^ i_t4;
1110 i_t4 = p_css_tab5[i_t4];
1111 /* deduce i_t6 & i_t5 */
1115 i_t6 = ( i_t6 + 0xff ) & 0x0ff;
1122 i_t5 += i_t6 + i_t4;
1123 i_t6 = p_css_tab4[ i_t6 ];
1124 /* feed / advance i_t3 / i_t5 */
1125 i_t3 = ( i_t3 << 8 ) | i_t6;
1131 /* iterate 6 more times to validate candidate key */
1132 for( ; i < 10 ; i++ )
1134 i_t4 = p_css_tab2[i_t2] ^ p_css_tab3[i_t1];
1136 i_t1 = ( ( i_t1 & 1 ) << 8 ) ^ i_t4;
1137 i_t4 = p_css_tab5[i_t4];
1138 i_t6 = ((((((( i_t3 >> 3 ) ^ i_t3 ) >> 1 ) ^
1139 i_t3 ) >> 8 ) ^ i_t3 ) >> 5 ) & 0xff;
1140 i_t3 = ( i_t3 << 8 ) | i_t6;
1141 i_t6 = p_css_tab4[i_t6];
1142 i_t5 += i_t6 + i_t4;
1143 if( ( i_t5 & 0xff ) != p_buffer[i] )
1153 /* Do 4 backwards steps of iterating t3 to deduce initial state */
1155 for( i = 0 ; i < 4 ; i++ )
1158 i_t3 = ( i_t3 >> 8 );
1159 /* easy to code, and fast enough bruteforce
1160 * search for byte shifted in */
1161 for( j = 0 ; j < 256 ; j++ )
1163 i_t3 = ( i_t3 & 0x1ffff) | ( j << 17 );
1164 i_t6 = ((((((( i_t3 >> 3 ) ^ i_t3 ) >> 1 ) ^
1165 i_t3 ) >> 8 ) ^ i_t3 ) >> 5 ) & 0xff;
1173 i_t4 = ( i_t3 >> 1 ) - 4;
1174 for( i_t5 = 0 ; i_t5 < 8; i_t5++ )
1176 if( ( ( i_t4 + i_t5 ) * 2 + 8 - ( (i_t4 + i_t5 ) & 7 ) )
1179 (*p_key)[0] = i_try>>8;
1180 (*p_key)[1] = i_try & 0xFF;
1181 (*p_key)[2] = ( ( i_t4 + i_t5 ) >> 0) & 0xFF;
1182 (*p_key)[3] = ( ( i_t4 + i_t5 ) >> 8) & 0xFF;
1183 (*p_key)[4] = ( ( i_t4 + i_t5 ) >> 16) & 0xFF;
1192 (*p_key)[0] ^= (*p_sector_key)[0];
1193 (*p_key)[1] ^= (*p_sector_key)[1];
1194 (*p_key)[2] ^= (*p_sector_key)[2];
1195 (*p_key)[3] ^= (*p_sector_key)[3];
1196 (*p_key)[4] ^= (*p_sector_key)[4];