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