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