]> git.sesse.net Git - vlc/blob - plugins/dvd/dvd_css.c
* Mandatory step for video output IV and the audio output quality
[vlc] / plugins / dvd / dvd_css.c
1 /*****************************************************************************
2  * dvd_css.c: Functions for DVD authentification and unscrambling
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: dvd_css.c,v 1.26 2001/04/22 00:08:25 stef Exp $
6  *
7  * Author: Stéphane Borel <stef@via.ecp.fr>
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  *  - DeCSSPlus by Ethan Hawke
14  *  - DecVOB
15  *  see http://www.lemuria.org/DeCSS/ by Tom Vogt for more information.
16  * 
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; either version 2 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
30  *****************************************************************************/
31
32 /*****************************************************************************
33  * Preamble
34  *****************************************************************************/
35 #include "defs.h"
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <string.h>
41
42 #include "common.h"
43
44 #include "intf_msg.h"
45
46 #include "dvd_css.h"
47 #ifdef HAVE_CSS
48 #include "dvd_csstables.h"
49 #endif /* HAVE_CSS */
50 #include "dvd_ioctl.h"
51 #include "dvd_ifo.h"
52
53 #include "input_dvd.h"
54
55 /*****************************************************************************
56  * Local prototypes
57  *****************************************************************************/
58 #ifdef HAVE_CSS
59 static int  CSSGetASF    ( int i_fd );
60 static void CSSCryptKey  ( int i_key_type, int i_varient,
61                            u8 const * pi_challenge, u8* pi_key );
62 static int  CSSCracker   ( int i_start, unsigned char * p_crypted,
63                            unsigned char * p_decrypted,
64                            dvd_key_t * p_sector_key, dvd_key_t * p_key );
65 #endif /* HAVE_CSS */
66
67 /*****************************************************************************
68  * CSSTest : check if the disc is encrypted or not
69  *****************************************************************************/
70 int CSSTest( int i_fd )
71 {
72     int i_ret, i_copyright;
73
74     i_ret = ioctl_ReadCopyright( i_fd, 0 /* i_layer */, &i_copyright );
75
76     if( i_ret < 0 )
77     {
78         /* Since it's the first ioctl we try to issue, we add a notice */
79         intf_ErrMsg( "css error: ioctl_ReadCopyright failed, "
80                      "make sure DVD ioctls were compiled in" );
81
82         return i_ret;
83     }
84
85     return i_copyright;
86 }
87
88 /*****************************************************************************
89  * CSSInit : 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 CSSInit( int i_fd, css_t * p_css )
96 {
97 #ifdef HAVE_CSS
98     /* structures defined in cdrom.h or dvdio.h */
99     char p_buffer[2048 + 4 + 1];
100     int  i_agid = 0;
101     int  i_ret = -1;
102     int  i;
103
104     /* Test authentication success */
105     switch( CSSGetASF( i_fd ) )
106     {
107         case -1:
108             return -1;
109
110         case 1:
111             intf_WarnMsg( 3, "css info: already authenticated" );
112             return 0;
113
114         case 0:
115             intf_WarnMsg( 3, "css info: need to authenticate" );
116     }
117
118     /* Init sequence, request AGID */
119     for( i = 1; i < 4 ; ++i )
120     {
121         intf_WarnMsg( 3, "css info: requesting AGID %d", i );
122
123         i_ret = ioctl_LUSendAgid( i_fd, &i_agid );
124
125         if( i_ret != -1 )
126         {
127             /* No error during ioctl: we know the device is authenticated */
128             break;
129         }
130
131         intf_ErrMsg( "css error: ioctl_LUSendAgid failed, invalidating" );
132
133         i_agid = 0;
134         ioctl_InvalidateAgid( i_fd, &i_agid );
135     }
136
137     /* Unable to authenticate without AGID */
138     if( i_ret == -1 )
139     {
140         intf_ErrMsg( "css error: ioctl_LUSendAgid failed, fatal" );
141         return -1;
142     }
143
144     for( i = 0 ; i < 10; ++i )
145     {
146         p_css->disc.pi_challenge[i] = i;
147     }
148
149     /* Get challenge from host */
150     for( i = 0 ; i < 10 ; ++i )
151     {
152         p_buffer[9-i] = p_css->disc.pi_challenge[i];
153     }
154
155     /* Send challenge to LU */
156     if( ioctl_HostSendChallenge( i_fd, &i_agid, p_buffer ) < 0 )
157     {
158         intf_ErrMsg( "css error: ioctl_HostSendChallenge failed" );
159         return -1;
160     }
161
162     /* Get key1 from LU */
163     if( ioctl_LUSendKey1( i_fd, &i_agid, p_buffer ) < 0)
164     {
165         intf_ErrMsg( "css error: ioctl_LUSendKey1 failed" );
166         return -1;
167     }
168
169     /* Send key1 to host */
170     for( i = 0 ; i < KEY_SIZE ; i++ )
171     {
172         p_css->disc.pi_key1[i] = p_buffer[4-i];
173     }
174
175     for( i = 0 ; i < 32 ; ++i )
176     {
177         CSSCryptKey( 0, i, p_css->disc.pi_challenge,
178                            p_css->disc.pi_key_check );
179
180         if( memcmp( p_css->disc.pi_key_check,
181                     p_css->disc.pi_key1, KEY_SIZE ) == 0 )
182         {
183             intf_WarnMsg( 3, "css info: drive authentic, using variant %d", i);
184             p_css->disc.i_varient = i;
185             break;
186         }
187     }
188
189     if( i == 32 )
190     {
191         intf_ErrMsg( "css error: drive would not authenticate" );
192         return -1;
193     }
194
195     /* Get challenge from LU */
196     if( ioctl_LUSendChallenge( i_fd, &i_agid, p_buffer ) < 0 )
197     {
198         intf_ErrMsg( "css error: ioctl_LUSendKeyChallenge failed" );
199         return -1;
200     }
201
202     /* Send challenge to host */
203     for( i = 0 ; i < 10 ; ++i )
204     {
205         p_css->disc.pi_challenge[i] = p_buffer[9-i];
206     }
207
208     CSSCryptKey( 1, p_css->disc.i_varient, p_css->disc.pi_challenge,
209                                                p_css->disc.pi_key2 );
210
211     /* Get key2 from host */
212     for( i = 0 ; i < KEY_SIZE ; ++i )
213     {
214         p_buffer[4-i] = p_css->disc.pi_key2[i];
215     }
216
217     /* Send key2 to LU */
218     if( ioctl_HostSendKey2( i_fd, &i_agid, p_buffer ) < 0 )
219     {
220         intf_ErrMsg( "css error: ioctl_HostSendKey2 failed" );
221         return -1;
222     }
223
224     intf_WarnMsg( 3, "css info: authentication established" );
225
226     memcpy( p_css->disc.pi_challenge, p_css->disc.pi_key1, KEY_SIZE );
227     memcpy( p_css->disc.pi_challenge+KEY_SIZE, p_css->disc.pi_key2, KEY_SIZE );
228
229     CSSCryptKey( 2, p_css->disc.i_varient, p_css->disc.pi_challenge,
230                                            p_css->disc.pi_key_check );
231
232     intf_WarnMsg( 1, "css info: received session key" );
233
234     if( i_agid < 0 )
235     {
236         return -1;
237     }
238
239     /* Test authentication success */
240     switch( CSSGetASF( i_fd ) )
241     {
242         case -1:
243             return -1;
244
245         case 1:
246             intf_WarnMsg( 3, "css info: already authenticated" );
247             return 0;
248
249         case 0:
250             intf_WarnMsg( 3, "css info: need to get disc key" );
251     }
252
253     /* Get encrypted disc key */
254     if( ioctl_ReadKey( i_fd, &i_agid, p_buffer ) < 0 )
255     {
256         intf_ErrMsg( "css error: ioctl_ReadKey failed" );
257         return -1;
258     }
259
260     /* Unencrypt disc key using bus key */
261     for( i = 0 ; i < 2048 ; i++ )
262     {
263         p_buffer[ i ] ^= p_css->disc.pi_key_check[ 4 - (i % KEY_SIZE) ];
264     }
265     memcpy( p_css->disc.pi_key_check, p_buffer, 2048 );
266
267     /* initialize title key to know it empty */
268     for( i = 0 ; i < KEY_SIZE ; i++ )
269     {
270         p_css->pi_title_key[i] = 0;
271     }
272
273     /* Test authentication success */
274     switch( CSSGetASF( i_fd ) )
275     {
276         case -1:
277             return -1;
278
279         case 1:
280             intf_WarnMsg( 3, "css info: successfully authenticated" );
281             return 0;
282
283         case 0:
284             intf_ErrMsg( "css error: no way to authenticate" );
285             return -1;
286     }
287
288 #else /* HAVE_CSS */
289     intf_ErrMsg( "css error: CSS decryption is disabled in this module" );
290
291 #endif /* HAVE_CSS */
292     return -1;
293
294 }
295
296 /*****************************************************************************
297  * CSSGetKey : get title key.
298  *****************************************************************************
299  * The DVD should have been opened and authenticated before.
300  *****************************************************************************/
301 int CSSGetKey( int i_fd, css_t * p_css )
302 {
303 #ifdef HAVE_CSS
304     /*
305      * Title key cracking method from Ethan Hawke,
306      * with Frank A. Stevenson algorithm.
307      * Does not use any player key table and ioctls.
308      */
309     u8          pi_buf[0x800];
310     dvd_key_t   pi_key;
311     off_t       i_pos;
312     boolean_t   b_encrypted;
313     boolean_t   b_stop_scanning;
314     int         i_bytes_read;
315     int         i_best_plen;
316     int         i_best_p;
317     int         i,j;
318
319     for( i = 0 ; i < KEY_SIZE ; i++ )
320     {
321         pi_key[i] = 0;
322     }
323
324     b_encrypted = 0;
325     b_stop_scanning = 0;
326
327     /* Position of the title on the disc */
328     i_pos = p_css->i_title_pos;
329
330     do {
331     i_pos = lseek( i_fd, i_pos, SEEK_SET );
332     i_bytes_read = read( i_fd, pi_buf, 0x800 );
333
334     /* PES_scrambling_control */
335     if( pi_buf[0x14] & 0x30 )
336     {
337         b_encrypted = 1;
338         i_best_plen = 0;
339         i_best_p = 0;
340
341         for( i = 2 ; i < 0x30 ; i++ )
342         {
343             for( j = i ; ( j < 0x80 ) &&
344                    ( pi_buf[0x7F - (j%i)] == pi_buf[0x7F-j] ) ; j++ );
345             {
346                 if( ( j > i_best_plen ) && ( j > i ) )
347                 {
348                     i_best_plen = j;
349                     i_best_p = i;
350                 }
351             }
352         }
353
354         if( ( i_best_plen > 20 ) && ( i_best_plen / i_best_p >= 2) )
355         {
356             i = CSSCracker( 0,  &pi_buf[0x80],
357                     &pi_buf[0x80 - ( i_best_plen / i_best_p) *i_best_p],
358                     (dvd_key_t*)&pi_buf[0x54],
359                     &pi_key );
360             b_stop_scanning = ( i >= 0 );
361         }
362     }
363
364     i_pos += i_bytes_read;
365     } while( i_bytes_read == 0x800 && !b_stop_scanning);
366
367     if( b_stop_scanning)
368     {
369             memcpy( p_css->pi_title_key,
370                     &pi_key, sizeof(dvd_key_t) );
371         intf_WarnMsg( 2, "css info: vts key initialized" );
372         return 0;
373     }
374
375     if( !b_encrypted )
376     {
377         intf_WarnMsg( 3, "css warning: this file was _NOT_ encrypted!" );
378         return 0;
379     }
380
381     return -1;
382
383 #else /* HAVE_CSS */
384     intf_ErrMsg( "css error: css decryption unavailable" );
385     return -1;
386
387 #endif /* HAVE_CSS */
388 }
389
390 /*****************************************************************************
391  * CSSDescrambleSector
392  *****************************************************************************
393  * sec : sector to descramble
394  * key : title key for this sector
395  *****************************************************************************/
396 int CSSDescrambleSector( dvd_key_t pi_key, u8* pi_sec )
397 {
398 #ifdef HAVE_CSS
399     unsigned int    i_t1, i_t2, i_t3, i_t4, i_t5, i_t6;
400     u8*             pi_end = pi_sec + 0x800;
401
402     /* PES_scrambling_control */
403     if( pi_sec[0x14] & 0x30)
404     {
405         i_t1 = ((pi_key)[0] ^ pi_sec[0x54]) | 0x100;
406         i_t2 = (pi_key)[1] ^ pi_sec[0x55];
407         i_t3 = (((pi_key)[2]) | ((pi_key)[3] << 8) |
408                ((pi_key)[4] << 16)) ^ ((pi_sec[0x56]) |
409                (pi_sec[0x57] << 8) | (pi_sec[0x58] << 16));
410         i_t4 = i_t3 & 7;
411         i_t3 = i_t3 * 2 + 8 - i_t4;
412         pi_sec += 0x80;
413         i_t5 = 0;
414
415         while( pi_sec != pi_end )
416         {
417             i_t4 = pi_css_tab2[i_t2] ^ pi_css_tab3[i_t1];
418             i_t2 = i_t1>>1;
419             i_t1 = ( ( i_t1 & 1 ) << 8 ) ^ i_t4;
420             i_t4 = pi_css_tab5[i_t4];
421             i_t6 = ((((((( i_t3 >> 3 ) ^ i_t3 ) >> 1 ) ^
422                                          i_t3 ) >> 8 ) ^ i_t3 ) >> 5) & 0xff;
423             i_t3 = (i_t3 << 8 ) | i_t6;
424             i_t6 = pi_css_tab4[i_t6];
425             i_t5 += i_t6 + i_t4;
426             *pi_sec = pi_css_tab1[*pi_sec] ^( i_t5 & 0xff );
427             pi_sec++;
428             i_t5 >>= 8;
429         }
430     }
431
432     return 0;
433
434 #else /* HAVE_CSS */
435     return 1;
436
437 #endif /* HAVE_CSS */
438 }
439
440 #ifdef HAVE_CSS
441
442 /* Following functions are local */
443
444 /*****************************************************************************
445  * CSSGetASF : Get Authentification success flag
446  *****************************************************************************
447  * Returns :
448  *  -1 on ioctl error,
449  *  0 if the device needs to be authenticated,
450  *  1 either.
451  *****************************************************************************/
452 static int CSSGetASF( int i_fd )
453 {
454     int i_agid;
455     int i_asf = 0;
456
457     for( i_agid = 0 ; i_agid < 4 ; i_agid++ )
458     {
459         if( ioctl_LUSendASF( i_fd, &i_agid, &i_asf ) == 0 )
460         {
461             intf_WarnMsg( 3, "css info: GetASF %sauthenticated",
462                           i_asf ? "":"not " );
463
464             return i_asf;
465         }
466     }
467
468     /* The ioctl process has failed */
469     intf_ErrMsg( "css error: GetASF fatal error" );
470     return -1;
471 }
472
473 /*****************************************************************************
474  * CSSCryptKey : shuffles bits and unencrypt keys.
475  *****************************************************************************
476  * Used during authentication and disc key negociation in CSSInit.
477  * i_key_type : 0->key1, 1->key2, 2->buskey.
478  * i_varient : between 0 and 31.
479  *****************************************************************************/
480 static void CSSCryptKey( int i_key_type, int i_varient,
481                          u8 const * pi_challenge, u8* pi_key )
482 {
483     /* Permutation table for challenge */
484     u8      ppi_perm_challenge[3][10] =
485             { { 1, 3, 0, 7, 5, 2, 9, 6, 4, 8 },
486               { 6, 1, 9, 3, 8, 5, 7, 4, 0, 2 },
487               { 4, 0, 3, 5, 7, 2, 8, 6, 1, 9 } };
488
489     /* Permutation table for varient table for key2 and buskey */
490     u8      ppi_perm_varient[2][32] =
491             { { 0x0a, 0x08, 0x0e, 0x0c, 0x0b, 0x09, 0x0f, 0x0d,
492                 0x1a, 0x18, 0x1e, 0x1c, 0x1b, 0x19, 0x1f, 0x1d,
493                 0x02, 0x00, 0x06, 0x04, 0x03, 0x01, 0x07, 0x05,
494                 0x12, 0x10, 0x16, 0x14, 0x13, 0x11, 0x17, 0x15 },
495               { 0x12, 0x1a, 0x16, 0x1e, 0x02, 0x0a, 0x06, 0x0e,
496                 0x10, 0x18, 0x14, 0x1c, 0x00, 0x08, 0x04, 0x0c,
497                 0x13, 0x1b, 0x17, 0x1f, 0x03, 0x0b, 0x07, 0x0f,
498                 0x11, 0x19, 0x15, 0x1d, 0x01, 0x09, 0x05, 0x0d } };
499
500     u8      pi_varients[32] =
501             {   0xB7, 0x74, 0x85, 0xD0, 0xCC, 0xDB, 0xCA, 0x73,
502                 0x03, 0xFE, 0x31, 0x03, 0x52, 0xE0, 0xB7, 0x42,
503                 0x63, 0x16, 0xF2, 0x2A, 0x79, 0x52, 0xFF, 0x1B,
504                 0x7A, 0x11, 0xCA, 0x1A, 0x9B, 0x40, 0xAD, 0x01 };
505
506     /* The "secret" key */
507     u8      pi_secret[5] = { 0x55, 0xD6, 0xC4, 0xC5, 0x28 };
508
509     u8      pi_bits[30];
510     u8      pi_scratch[10];
511     u8      pi_tmp1[5];
512     u8      pi_tmp2[5];
513     u8      i_lfsr0_o;  /* 1 bit used */
514     u8      i_lfsr1_o;  /* 1 bit used */
515     u32     i_lfsr0;
516     u32     i_lfsr1;
517     u8      i_css_varient;
518     u8      i_cse;
519     u8      i_index;
520     u8      i_combined;
521     u8      i_carry;
522     u8      i_val = 0;
523     int     i_term = 0;
524     int     i_bit;
525     int     i;
526
527     for (i = 9; i >= 0; --i)
528         pi_scratch[i] = pi_challenge[ppi_perm_challenge[i_key_type][i]];
529
530     i_css_varient = ( i_key_type == 0 ) ? i_varient :
531                     ppi_perm_varient[i_key_type-1][i_varient];
532
533     /*
534      * This encryption engine implements one of 32 variations
535      * one the same theme depending upon the choice in the
536      * varient parameter (0 - 31).
537      *
538      * The algorithm itself manipulates a 40 bit input into
539      * a 40 bit output.
540      * The parameter 'input' is 80 bits.  It consists of
541      * the 40 bit input value that is to be encrypted followed
542      * by a 40 bit seed value for the pseudo random number
543      * generators.
544      */
545
546     /* Feed the secret into the input values such that
547      * we alter the seed to the LFSR's used above,  then
548      * generate the bits to play with.
549      */
550     for( i = 5 ; --i >= 0 ; )
551     {
552         pi_tmp1[i] = pi_scratch[5 + i] ^ pi_secret[i] ^ pi_crypt_tab2[i];
553     }
554
555     /*
556      * We use two LFSR's (seeded from some of the input data bytes) to
557      * generate two streams of pseudo-random bits.  These two bit streams
558      * are then combined by simply adding with carry to generate a final
559      * sequence of pseudo-random bits which is stored in the buffer that
560      * 'output' points to the end of - len is the size of this buffer.
561      *
562      * The first LFSR is of degree 25,  and has a polynomial of:
563      * x^13 + x^5 + x^4 + x^1 + 1
564      *
565      * The second LSFR is of degree 17,  and has a (primitive) polynomial of:
566      * x^15 + x^1 + 1
567      *
568      * I don't know if these polynomials are primitive modulo 2,  and thus
569      * represent maximal-period LFSR's.
570      *
571      *
572      * Note that we take the output of each LFSR from the new shifted in
573      * bit,  not the old shifted out bit.  Thus for ease of use the LFSR's
574      * are implemented in bit reversed order.
575      *
576      */
577     
578     /* In order to ensure that the LFSR works we need to ensure that the
579      * initial values are non-zero.  Thus when we initialise them from
580      * the seed,  we ensure that a bit is set.
581      */
582     i_lfsr0 = ( pi_tmp1[0] << 17 ) | ( pi_tmp1[1] << 9 ) |
583               (( pi_tmp1[2] & ~7 ) << 1 ) | 8 | ( pi_tmp1[2] & 7 );
584     i_lfsr1 = ( pi_tmp1[3] << 9 ) | 0x100 | pi_tmp1[4];
585
586     i_index = sizeof(pi_bits);
587     i_carry = 0;
588
589     do
590     {
591         for( i_bit = 0, i_val = 0 ; i_bit < 8 ; ++i_bit )
592         {
593
594             i_lfsr0_o = ( ( i_lfsr0 >> 24 ) ^ ( i_lfsr0 >> 21 ) ^
595                         ( i_lfsr0 >> 20 ) ^ ( i_lfsr0 >> 12 ) ) & 1;
596             i_lfsr0 = ( i_lfsr0 << 1 ) | i_lfsr0_o;
597
598             i_lfsr1_o = ( ( i_lfsr1 >> 16 ) ^ ( i_lfsr1 >> 2 ) ) & 1;
599             i_lfsr1 = ( i_lfsr1 << 1 ) | i_lfsr1_o;
600
601             i_combined = !i_lfsr1_o + i_carry + !i_lfsr0_o;
602             /* taking bit 1 */
603             i_carry = ( i_combined >> 1 ) & 1;
604             i_val |= ( i_combined & 1 ) << i_bit;
605         }
606     
607         pi_bits[--i_index] = i_val;
608     } while( i_index > 0 );
609
610     /* This term is used throughout the following to
611      * select one of 32 different variations on the
612      * algorithm.
613      */
614     i_cse = pi_varients[i_css_varient] ^ pi_crypt_tab2[i_css_varient];
615
616     /* Now the actual blocks doing the encryption.  Each
617      * of these works on 40 bits at a time and are quite
618      * similar.
619      */
620     i_index = 0;
621     for( i = 5, i_term = 0 ; --i >= 0 ; i_term = pi_scratch[i] )
622     {
623         i_index = pi_bits[25 + i] ^ pi_scratch[i];
624         i_index = pi_crypt_tab1[i_index] ^ ~pi_crypt_tab2[i_index] ^ i_cse;
625
626         pi_tmp1[i] = pi_crypt_tab2[i_index] ^ pi_crypt_tab3[i_index] ^ i_term;
627     }
628     pi_tmp1[4] ^= pi_tmp1[0];
629
630     for( i = 5, i_term = 0 ; --i >= 0 ; i_term = pi_tmp1[i] )
631     {
632         i_index = pi_bits[20 + i] ^ pi_tmp1[i];
633         i_index = pi_crypt_tab1[i_index] ^ ~pi_crypt_tab2[i_index] ^ i_cse;
634
635         pi_tmp2[i] = pi_crypt_tab2[i_index] ^ pi_crypt_tab3[i_index] ^ i_term;
636     }
637     pi_tmp2[4] ^= pi_tmp2[0];
638
639     for( i = 5, i_term = 0 ; --i >= 0 ; i_term = pi_tmp2[i] )
640     {
641         i_index = pi_bits[15 + i] ^ pi_tmp2[i];
642         i_index = pi_crypt_tab1[i_index] ^ ~pi_crypt_tab2[i_index] ^ i_cse;
643         i_index = pi_crypt_tab2[i_index] ^ pi_crypt_tab3[i_index] ^ i_term;
644
645         pi_tmp1[i] = pi_crypt_tab0[i_index] ^ pi_crypt_tab2[i_index];
646     }
647     pi_tmp1[4] ^= pi_tmp1[0];
648
649     for( i = 5, i_term = 0 ; --i >= 0 ; i_term = pi_tmp1[i] )
650     {
651         i_index = pi_bits[10 + i] ^ pi_tmp1[i];
652         i_index = pi_crypt_tab1[i_index] ^ ~pi_crypt_tab2[i_index] ^ i_cse;
653
654         i_index = pi_crypt_tab2[i_index] ^ pi_crypt_tab3[i_index] ^ i_term;
655
656         pi_tmp2[i] = pi_crypt_tab0[i_index] ^ pi_crypt_tab2[i_index];
657     }
658     pi_tmp2[4] ^= pi_tmp2[0];
659
660     for( i = 5, i_term = 0 ; --i >= 0 ; i_term = pi_tmp2[i] )
661     {
662         i_index = pi_bits[5 + i] ^ pi_tmp2[i];
663         i_index = pi_crypt_tab1[i_index] ^ ~pi_crypt_tab2[i_index] ^ i_cse;
664
665         pi_tmp1[i] = pi_crypt_tab2[i_index] ^ pi_crypt_tab3[i_index] ^ i_term;
666     }
667     pi_tmp1[4] ^= pi_tmp1[0];
668
669     for(i = 5, i_term = 0 ; --i >= 0 ; i_term = pi_tmp1[i] )
670     {
671         i_index = pi_bits[i] ^ pi_tmp1[i];
672         i_index = pi_crypt_tab1[i_index] ^ ~pi_crypt_tab2[i_index] ^ i_cse;
673
674         pi_key[i] = pi_crypt_tab2[i_index] ^ pi_crypt_tab3[i_index] ^ i_term;
675     }
676
677     return;
678 }
679
680 /*****************************************************************************
681  * CSSCracker : title key decryption by cracking
682  *****************************************************************************
683  * This function is called by CSSGetKeys to find a key
684  *****************************************************************************/
685 static int CSSCracker( int i_start,
686                        unsigned char * p_crypted,
687                        unsigned char * p_decrypted,
688                        dvd_key_t * p_sector_key,
689                        dvd_key_t * p_key )
690 {
691     unsigned char pi_buffer[10];
692     unsigned int i_t1, i_t2, i_t3, i_t4, i_t5, i_t6;
693     unsigned int i_try;
694     unsigned int i_candidate;
695     unsigned int i, j;
696     int i_exit = -1;
697
698
699     for( i = 0 ; i < 10 ; i++ )
700     {
701         pi_buffer[i] = pi_css_tab1[p_crypted[i]] ^ p_decrypted[i];
702     }
703
704     for( i_try = i_start ; i_try < 0x10000 ; i_try++ )
705     {
706         i_t1 = i_try >> 8 | 0x100;
707         i_t2 = i_try & 0xff;
708         i_t3 = 0;               /* not needed */
709         i_t5 = 0;
710
711         /* iterate cipher 4 times to reconstruct LFSR2 */
712         for( i = 0 ; i < 4 ; i++ )
713         {
714             /* advance LFSR1 normaly */
715             i_t4 = pi_css_tab2[i_t2] ^ pi_css_tab3[i_t1];
716             i_t2 = i_t1 >> 1;
717             i_t1 = ( ( i_t1 & 1 ) << 8 ) ^ i_t4;
718             i_t4 = pi_css_tab5[i_t4];
719             /* deduce i_t6 & i_t5 */
720             i_t6 = pi_buffer[i];
721             if( i_t5 )
722             {
723                 i_t6 = ( i_t6 + 0xff ) & 0x0ff;
724             }
725             if( i_t6 < i_t4 )
726             {
727                 i_t6 += 0x100;
728             }
729             i_t6 -= i_t4;
730             i_t5 += i_t6 + i_t4;
731             i_t6 = pi_css_tab4[ i_t6 ];
732             /* feed / advance i_t3 / i_t5 */
733             i_t3 = ( i_t3 << 8 ) | i_t6;
734             i_t5 >>= 8;
735         }
736
737         i_candidate = i_t3;
738
739         /* iterate 6 more times to validate candidate key */
740         for( ; i < 10 ; i++ )
741         {
742             i_t4 = pi_css_tab2[i_t2] ^ pi_css_tab3[i_t1];
743             i_t2 = i_t1 >> 1;
744             i_t1 = ( ( i_t1 & 1 ) << 8 ) ^ i_t4;
745             i_t4 = pi_css_tab5[i_t4];
746             i_t6 = ((((((( i_t3 >> 3 ) ^ i_t3 ) >> 1 ) ^
747                                          i_t3 ) >> 8 ) ^ i_t3 ) >> 5 ) & 0xff;
748             i_t3 = ( i_t3 << 8 ) | i_t6;
749             i_t6 = pi_css_tab4[i_t6];
750             i_t5 += i_t6 + i_t4;
751             if( ( i_t5 & 0xff ) != pi_buffer[i] )
752             {
753                 break;
754             }
755
756             i_t5 >>= 8;
757         }
758
759         if( i == 10 )
760         {
761             /* Do 4 backwards steps of iterating t3 to deduce initial state */
762             i_t3 = i_candidate;
763             for( i = 0 ; i < 4 ; i++ )
764             {
765                 i_t1 = i_t3 & 0xff;
766                 i_t3 = ( i_t3 >> 8 );
767                 /* easy to code, and fast enough bruteforce
768                  * search for byte shifted in */
769                 for( j = 0 ; j < 256 ; j++ )
770                 {
771                     i_t3 = ( i_t3 & 0x1ffff) | ( j << 17 );
772                     i_t6 = ((((((( i_t3 >> 3 ) ^ i_t3 ) >> 1 ) ^
773                                    i_t3 ) >> 8 ) ^ i_t3 ) >> 5 ) & 0xff;
774                     if( i_t6 == i_t1 )
775                     {
776                         break;
777                     }
778                 }
779             }
780
781             i_t4 = ( i_t3 >> 1 ) - 4;
782             for( i_t5 = 0 ; i_t5 < 8; i_t5++ )
783             {
784                 if( ( ( i_t4 + i_t5 ) * 2 + 8 - ( (i_t4 + i_t5 ) & 7 ) )
785                                                                       == i_t3 )
786                 {
787                     (*p_key)[0] = i_try>>8;
788                     (*p_key)[1] = i_try & 0xFF;
789                     (*p_key)[2] = ( ( i_t4 + i_t5 ) >> 0) & 0xFF;
790                     (*p_key)[3] = ( ( i_t4 + i_t5 ) >> 8) & 0xFF;
791                     (*p_key)[4] = ( ( i_t4 + i_t5 ) >> 16) & 0xFF;
792                     i_exit = i_try + 1;
793                 }
794             }
795         }
796     }
797
798     if( i_exit >= 0 )
799     {
800         (*p_key)[0] ^= (*p_sector_key)[0];
801         (*p_key)[1] ^= (*p_sector_key)[1];
802         (*p_key)[2] ^= (*p_sector_key)[2];
803         (*p_key)[3] ^= (*p_sector_key)[3];
804         (*p_key)[4] ^= (*p_sector_key)[4];
805     }
806
807     return i_exit;
808 }
809
810 #endif /* HAVE_CSS */
811