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