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