]> git.sesse.net Git - vlc/blob - extras/libdvdcss/css.c
* ./BUGS: added a list of known bugs. Please add your findings!
[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.19 2002/01/04 14:01:34 sam Exp $
6  *
7  * Author: Stéphane Borel <stef@via.ecp.fr>
8  *         Håkan Hjort <d95hjort@dtek.chalmers.se>
9  *
10  * based on:
11  *  - css-auth by Derek Fawcus <derek@spider.com>
12  *  - DVD CSS ioctls example program by Andrew T. Veliath <andrewtv@usa.net>
13  *  - The Divide and conquer attack by Frank A. Stevenson <frank@funcom.com>
14  *  - DeCSSPlus by Ethan Hawke
15  *  - DecVOB
16  *  see http://www.lemuria.org/DeCSS/ by Tom Vogt for more information.
17  * 
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
31  *****************************************************************************/
32
33 /*****************************************************************************
34  * Preamble
35  *****************************************************************************/
36 #include <stdio.h>
37 #include <stdlib.h>
38
39 #include <string.h>
40
41 #include <videolan/vlc.h>
42
43 #include "videolan/dvdcss.h"
44 #include "libdvdcss.h"
45
46 #include "csstables.h"
47 #include "ioctl.h"
48
49 #ifdef HAVE_CSSKEYS
50 #  include "csskeys.h"
51 #endif
52
53
54 /*****************************************************************************
55  * Local prototypes
56  *****************************************************************************/
57 static int  CSSGetASF    ( dvdcss_handle dvdcss );
58 static void CSSCryptKey  ( int i_key_type, int i_varient,
59                            u8 const * p_challenge, u8* p_key );
60 static void CSSDecryptKey( u8* p_crypted, u8* p_key, u8 );
61 static int  CSSDiscCrack ( dvdcss_handle dvdcss, u8 * p_disc_key );
62 static int  CSSTitleCrack( 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
66 /*****************************************************************************
67  * CSSTest : check if the disc is encrypted or not
68  *****************************************************************************/
69 int CSSTest( dvdcss_handle dvdcss )
70 {
71     int i_ret, i_copyright;
72
73     i_ret = ioctl_ReadCopyright( dvdcss->i_fd, 0 /* i_layer */, &i_copyright );
74
75     if( i_ret < 0 )
76     {
77         /* Since it's the first ioctl we try to issue, we add a notice */
78         _dvdcss_error( dvdcss, "css error: ioctl_ReadCopyright failed, "
79                        "make sure there is a DVD in the drive, and that "
80                        "DVD ioctls were compiled in this libdvdcss version" );
81
82         return i_ret;
83     }
84
85     return i_copyright;
86 }
87
88 /*****************************************************************************
89  * CSSAuth : CSS Structure initialisation and DVD authentication.
90  *****************************************************************************
91  * It simulates the mutual authentication between logical unit and host.
92  * Since we don't need the disc key to find the title key, we just run the
93  * basic unavoidable commands to authenticate device and disc.
94  *****************************************************************************/
95 int CSSAuth( dvdcss_handle dvdcss )
96 {
97     /* structures defined in cdrom.h or dvdio.h */
98     unsigned char p_buffer[10];
99     char psz_warning[48];
100     int  i_ret = -1;
101     int  i;
102
103     dvdcss->css.i_agid = 0;
104
105     /* Test authentication success */
106     switch( CSSGetASF( dvdcss ) )
107     {
108         case -1:
109             return -1;
110
111         case 1:
112             _dvdcss_debug( dvdcss, "already authenticated" );
113             break;
114
115         case 0:
116             _dvdcss_debug( dvdcss, "need to authenticate" );
117             break;
118     }
119
120     /* Init sequence, request AGID */
121     for( i = 1; i < 4 ; ++i )
122     {
123         snprintf( psz_warning, sizeof(psz_warning), "requesting AGID %d", i );
124         _dvdcss_debug( dvdcss, psz_warning );
125
126         i_ret = ioctl_ReportAgid( dvdcss->i_fd, &dvdcss->css.i_agid );
127
128         if( i_ret != -1 )
129         {
130             /* No error during ioctl: we know the device is authenticated */
131             break;
132         }
133
134         _dvdcss_error( dvdcss, "ioctl_ReportAgid failed, invalidating" );
135
136         dvdcss->css.i_agid = 0;
137         ioctl_InvalidateAgid( dvdcss->i_fd, &dvdcss->css.i_agid );
138     }
139
140     /* Unable to authenticate without AGID */
141     if( i_ret == -1 )
142     {
143         _dvdcss_error( dvdcss, "ioctl_ReportAgid failed, fatal" );
144         return -1;
145     }
146
147     for( i = 0 ; i < 10; ++i )
148     {
149         dvdcss->css.disc.p_challenge[i] = i;
150     }
151
152     /* Get challenge from host */
153     for( i = 0 ; i < 10 ; ++i )
154     {
155         p_buffer[9-i] = dvdcss->css.disc.p_challenge[i];
156     }
157
158     /* Send challenge to LU */
159     if( ioctl_SendChallenge( dvdcss->i_fd, &dvdcss->css.i_agid, p_buffer ) < 0 )
160     {
161         _dvdcss_error( dvdcss, "ioctl_SendChallenge failed" );
162         return -1;
163     }
164
165     /* Get key1 from LU */
166     if( ioctl_ReportKey1( dvdcss->i_fd, &dvdcss->css.i_agid, p_buffer ) < 0)
167     {
168         _dvdcss_error( dvdcss, "ioctl_ReportKey1 failed" );
169         return -1;
170     }
171
172     /* Send key1 to host */
173     for( i = 0 ; i < KEY_SIZE ; i++ )
174     {
175         dvdcss->css.disc.p_key1[i] = p_buffer[4-i];
176     }
177
178     for( i = 0 ; i < 32 ; ++i )
179     {
180         CSSCryptKey( 0, i, dvdcss->css.disc.p_challenge,
181                            dvdcss->css.disc.p_key_check );
182
183         if( memcmp( dvdcss->css.disc.p_key_check,
184                     dvdcss->css.disc.p_key1, KEY_SIZE ) == 0 )
185         {
186             snprintf( psz_warning, sizeof(psz_warning),
187                       "drive authentic, using variant %d", i );
188             _dvdcss_debug( dvdcss, psz_warning );
189             dvdcss->css.disc.i_varient = i;
190             break;
191         }
192     }
193
194     if( i == 32 )
195     {
196         _dvdcss_error( dvdcss, "drive would not authenticate" );
197         return -1;
198     }
199
200     /* Get challenge from LU */
201     if( ioctl_ReportChallenge( dvdcss->i_fd, &dvdcss->css.i_agid, p_buffer ) < 0 )
202     {
203         _dvdcss_error( dvdcss, "ioctl_ReportKeyChallenge failed" );
204         return -1;
205     }
206
207     /* Send challenge to host */
208     for( i = 0 ; i < 10 ; ++i )
209     {
210         dvdcss->css.disc.p_challenge[i] = p_buffer[9-i];
211     }
212
213     CSSCryptKey( 1, dvdcss->css.disc.i_varient,
214                     dvdcss->css.disc.p_challenge,
215                     dvdcss->css.disc.p_key2 );
216
217     /* Get key2 from host */
218     for( i = 0 ; i < KEY_SIZE ; ++i )
219     {
220         p_buffer[4-i] = dvdcss->css.disc.p_key2[i];
221     }
222
223     /* Send key2 to LU */
224     if( ioctl_SendKey2( dvdcss->i_fd, &dvdcss->css.i_agid, p_buffer ) < 0 )
225     {
226         _dvdcss_error( dvdcss, "ioctl_SendKey2 failed" );
227         return -1;
228     }
229
230     _dvdcss_debug( dvdcss, "authentication established" );
231
232     memcpy( dvdcss->css.disc.p_challenge,
233             dvdcss->css.disc.p_key1, KEY_SIZE );
234     memcpy( dvdcss->css.disc.p_challenge+KEY_SIZE,
235             dvdcss->css.disc.p_key2, KEY_SIZE );
236
237     CSSCryptKey( 2, dvdcss->css.disc.i_varient,
238                     dvdcss->css.disc.p_challenge,
239                     dvdcss->css.disc.p_key_check );
240
241     _dvdcss_debug( dvdcss, "received session key" );
242
243     if( dvdcss->css.i_agid < 0 )
244     {
245         return -1;
246     }
247
248     /* Test authentication success */
249     switch( CSSGetASF( dvdcss ) )
250     {
251         case -1:
252             return -1;
253
254         case 1:
255             _dvdcss_debug( dvdcss, "already authenticated" );
256             return 0;
257
258         case 0:
259             _dvdcss_debug( dvdcss, "need to get disc key" );
260             return 0;
261     }
262
263     return -1;
264 }
265
266 /*****************************************************************************
267  * CSSGetDiscKey : get disc key and optionnaly decrypts it.
268  *****************************************************************************
269  * This function should only be called if DVD ioctls are present.
270  * Two decryption methods are then offered:
271  *  -disc key hash crack,
272  *  -decryption with player keys if they are available.
273  *****************************************************************************/
274 int CSSGetDiscKey( dvdcss_handle dvdcss )
275 {
276     unsigned char   p_buffer[2048 + 4 + 1];
277 #ifdef HAVE_CSSKEYS
278     dvd_key_t       disc_key;
279     dvd_key_t       test_key;
280 #endif
281     int i;
282
283     if( CSSAuth( dvdcss ) )
284     {
285         return -1;
286     }
287
288     /* Get encrypted disc key */
289     if( ioctl_ReadDiscKey( dvdcss->i_fd, &dvdcss->css.i_agid, p_buffer ) < 0 )
290     {
291         _dvdcss_error( dvdcss, "ioctl_ReadDiscKey failed" );
292         return -1;
293     }
294
295     /* Unencrypt disc key using bus key */
296     for( i = 0 ; i < 2048 ; i++ )
297     {
298         p_buffer[ i ] ^= dvdcss->css.disc.p_key_check[ 4 - (i % KEY_SIZE) ];
299     }
300     memcpy( dvdcss->css.disc.p_disc_key, p_buffer, 2048 );
301
302     switch( dvdcss->i_method )
303     {
304         case DVDCSS_METHOD_KEY:
305 #ifdef HAVE_CSSKEYS
306             /* Decrypt disc key with player keys from csskeys.h */
307             _dvdcss_debug( dvdcss, "decrypting disc key with player keys" );
308             i = 0;
309             do
310             {
311                 /* Take encrypted disc key and decrypt it */
312                 memcpy( disc_key,
313                         dvdcss->css.disc.p_disc_key
314                       + playerkeys[i].i_offset,
315                         KEY_SIZE );
316                 CSSDecryptKey( disc_key, playerkeys[i].p_key, 0 );
317
318                 /* Encrypt disc key hash with disc key to
319                  * check we have disc key */
320                 memcpy( test_key, dvdcss->css.disc.p_disc_key, KEY_SIZE );
321                 CSSDecryptKey( test_key, disc_key, 0);
322
323                 i++;
324
325             } while( ( playerkeys[i].i_offset != -1 ) &&
326                      ( memcmp( test_key, disc_key, KEY_SIZE ) ) );
327
328             /* The decrypted disk key will replace the disk key hash */
329             memcpy( dvdcss->css.disc.p_disc_key, disc_key, KEY_SIZE );
330             break;
331 #else
332             dvdcss->i_method = DVDCSS_METHOD_DISC;            
333 #endif
334         case DVDCSS_METHOD_DISC:
335             /* Crack Disc key to be able to use it */
336             _dvdcss_debug( dvdcss, "cracking disc key with key hash" );
337             _dvdcss_debug( dvdcss, "building 64MB table ... this will take some time" );
338             CSSDiscCrack( dvdcss, dvdcss->css.disc.p_disc_key );
339             break;
340
341         default:
342             _dvdcss_debug( dvdcss, "disc key won't be decrypted" );
343     }
344
345     return 0;
346 }
347
348
349 /*****************************************************************************
350  * CSSGetTitleKey : get title key.
351  *****************************************************************************/
352 int CSSGetTitleKey( dvdcss_handle dvdcss, int i_pos )
353 {
354     dvd_key_t   p_key;
355     int         i,j;
356
357     if( ( dvdcss->i_method == DVDCSS_METHOD_TITLE )
358         || ( dvdcss->b_ioctls == 0 ) )
359     {
360         /*
361          * Title key cracking method from Ethan Hawke,
362          * with Frank A. Stevenson algorithm.
363          * Does not use any player key table and ioctls.
364          */
365         u8          p_buf[0x800];
366         u8          p_packstart[4] = { 0x00, 0x00, 0x01, 0xba };
367         boolean_t   b_encrypted;
368         boolean_t   b_stop_scanning;
369         int         i_blocks_read;
370         int         i_best_plen;
371         int         i_best_p;
372
373         _dvdcss_debug( dvdcss, "cracking title key ... this may take some time" );
374
375         for( i = 0 ; i < KEY_SIZE ; i++ )
376         {
377             p_key[i] = 0;
378         }
379
380         b_encrypted = 0;
381         b_stop_scanning = 0;
382         i_blocks_read = 0;
383
384         do
385         {
386             i_pos = _dvdcss_seek( dvdcss, i_pos );
387             if( _dvdcss_read( dvdcss, p_buf, 1 ) != 1 ) break;
388
389             /* Stop when we find a non MPEG stream block */
390             if( memcmp( p_buf, p_packstart, 4 ) )
391             {
392                 /* The title is unencrypted */
393                 if( !b_encrypted )
394                     break;
395                 /* dvdcss some times fail to find/crack the key, 
396                    hope that it's the same as the one in the next title
397                    _dvdcss_debug( dvdcss, "no key found at end of title" );
398                 */
399             }
400
401             /* PES_scrambling_control on and make sure that the packet type 
402                is one that can be scrambled */
403             if( p_buf[0x14] & 0x30  && ! ( p_buf[0x11] == 0xbb 
404                                            || p_buf[0x11] == 0xbe  
405                                            || p_buf[0x11] == 0xbf ) )
406             {
407                 b_encrypted = 1;
408                 i_best_plen = 0;
409                 i_best_p = 0;
410
411                 for( i = 2 ; i < 0x30 ; i++ )
412                 {
413                     for( j = i+1 ;
414                          j < 0x80 && ( p_buf[0x7F - (j%i)] == p_buf[0x7F-j] );
415                          j++ );
416                     {
417                         if( j > i_best_plen )
418                         {
419                             i_best_plen = j;
420                             i_best_p = i;
421                         }
422                     }
423                 }
424
425                 if( ( i_best_plen > 20 ) && ( i_best_plen / i_best_p >= 2) )
426                 {
427                     i = CSSTitleCrack( 0,  &p_buf[0x80],
428                             &p_buf[0x80 - ( i_best_plen / i_best_p) *i_best_p],
429                             (dvd_key_t*)&p_buf[0x54],
430                             &p_key );
431                     b_stop_scanning = ( i >= 0 );
432                 }
433             }
434
435             i_pos += 1;
436             i_blocks_read += 1;
437
438             /* If we haven't seen any encrypted ones after 3000 blocks stop */
439             if( !b_encrypted && i_blocks_read >= 1000 ) break;
440
441         } while( !b_stop_scanning );
442
443         if( b_stop_scanning )
444         {
445             memcpy( dvdcss->css.p_title_key, &p_key, sizeof(dvd_key_t) );
446             _dvdcss_debug( dvdcss, "vts key initialized" );
447             return 0;
448         }
449
450         if( !b_encrypted )
451         {
452             _dvdcss_debug( dvdcss, "file was unscrambled" );
453             return 0;
454         }
455
456         return -1;
457     }
458     else
459     {
460         /* 
461          * if we are here we have a decrypted disc key and ioctls are available
462          * so we can read the title key and decrypt it.
463          */
464
465         _dvdcss_debug( dvdcss, "decrypting title key with disc key" );
466         
467         /* We need to authenticate again for every key
468          * (to get a new session key ?) */
469         CSSAuth( dvdcss );
470
471         /* Get encrypted title key */
472         if( ioctl_ReadTitleKey( dvdcss->i_fd, &dvdcss->css.i_agid,
473                                 i_pos, p_key ) < 0 )
474         {
475             _dvdcss_error( dvdcss, "ioctl_ReadTitleKey failed" );
476             return -1;
477         }
478         /* Unencrypt title key using bus key */
479         for( i = 0 ; i < KEY_SIZE ; i++ )
480         {
481             p_key[ i ] ^= dvdcss->css.disc.p_key_check[ 4 - (i % KEY_SIZE) ];
482         }
483
484         /* Title key decryption needs one inversion 0xff */
485         CSSDecryptKey( p_key, dvdcss->css.disc.p_disc_key, 0xff );
486
487         memcpy( dvdcss->css.p_title_key, p_key, sizeof(dvd_key_t) );
488
489         return 0;
490     } // (dvdcss->i_method == DVDCSS_METHOD_TITLE) || (dvdcss->b_ioctls == 0)
491 }
492
493 /*****************************************************************************
494  * CSSDescrambleSector: does the actual descrambling of data
495  *****************************************************************************
496  * sec : sector to descramble
497  * key : title key for this sector
498  *****************************************************************************/
499 int CSSDescrambleSector( dvd_key_t p_key, u8* p_sec )
500 {
501     unsigned int    i_t1, i_t2, i_t3, i_t4, i_t5, i_t6;
502     u8*             p_end = p_sec + 0x800;
503
504     /* PES_scrambling_control */
505     if( p_sec[0x14] & 0x30)
506     {
507         i_t1 = ((p_key)[0] ^ p_sec[0x54]) | 0x100;
508         i_t2 = (p_key)[1] ^ p_sec[0x55];
509         i_t3 = (((p_key)[2]) | ((p_key)[3] << 8) |
510                ((p_key)[4] << 16)) ^ ((p_sec[0x56]) |
511                (p_sec[0x57] << 8) | (p_sec[0x58] << 16));
512         i_t4 = i_t3 & 7;
513         i_t3 = i_t3 * 2 + 8 - i_t4;
514         p_sec += 0x80;
515         i_t5 = 0;
516
517         while( p_sec != p_end )
518         {
519             i_t4 = p_css_tab2[i_t2] ^ p_css_tab3[i_t1];
520             i_t2 = i_t1>>1;
521             i_t1 = ( ( i_t1 & 1 ) << 8 ) ^ i_t4;
522             i_t4 = p_css_tab5[i_t4];
523             i_t6 = ((((((( i_t3 >> 3 ) ^ i_t3 ) >> 1 ) ^
524                                          i_t3 ) >> 8 ) ^ i_t3 ) >> 5 ) & 0xff;
525             i_t3 = (i_t3 << 8 ) | i_t6;
526             i_t6 = p_css_tab4[i_t6];
527             i_t5 += i_t6 + i_t4;
528             *p_sec = p_css_tab1[*p_sec] ^( i_t5 & 0xff );
529             p_sec++;
530             i_t5 >>= 8;
531         }
532     }
533
534     return 0;
535 }
536
537 /* Following functions are local */
538
539 /*****************************************************************************
540  * CSSGetASF : Get Authentification success flag
541  *****************************************************************************
542  * Returns :
543  *  -1 on ioctl error,
544  *  0 if the device needs to be authenticated,
545  *  1 either.
546  *****************************************************************************/
547 static int CSSGetASF( dvdcss_handle dvdcss )
548 {
549     int i_agid;
550     int i_asf = 0;
551
552     for( i_agid = 0 ; i_agid < 4 ; i_agid++ )
553     {
554         if( ioctl_ReportASF( dvdcss->i_fd, &i_agid, &i_asf ) == 0 )
555         {
556             if( i_asf )
557             {
558                 _dvdcss_debug( dvdcss, "GetASF authenticated" );
559             }
560             else
561             {
562                 _dvdcss_debug( dvdcss, "GetASF not authenticated" );
563             }
564
565             return i_asf;
566         }
567     }
568
569     /* The ioctl process has failed */
570     _dvdcss_error( dvdcss, "GetASF fatal error" );
571     return -1;
572 }
573
574 /*****************************************************************************
575  * CSSCryptKey : shuffles bits and unencrypt keys.
576  *****************************************************************************
577  * Used during authentication and disc key negociation in CSSAuth.
578  * i_key_type : 0->key1, 1->key2, 2->buskey.
579  * i_varient : between 0 and 31.
580  *****************************************************************************/
581 static void CSSCryptKey( int i_key_type, int i_varient,
582                          u8 const * p_challenge, u8* p_key )
583 {
584     /* Permutation table for challenge */
585     u8      pp_perm_challenge[3][10] =
586             { { 1, 3, 0, 7, 5, 2, 9, 6, 4, 8 },
587               { 6, 1, 9, 3, 8, 5, 7, 4, 0, 2 },
588               { 4, 0, 3, 5, 7, 2, 8, 6, 1, 9 } };
589
590     /* Permutation table for varient table for key2 and buskey */
591     u8      pp_perm_varient[2][32] =
592             { { 0x0a, 0x08, 0x0e, 0x0c, 0x0b, 0x09, 0x0f, 0x0d,
593                 0x1a, 0x18, 0x1e, 0x1c, 0x1b, 0x19, 0x1f, 0x1d,
594                 0x02, 0x00, 0x06, 0x04, 0x03, 0x01, 0x07, 0x05,
595                 0x12, 0x10, 0x16, 0x14, 0x13, 0x11, 0x17, 0x15 },
596               { 0x12, 0x1a, 0x16, 0x1e, 0x02, 0x0a, 0x06, 0x0e,
597                 0x10, 0x18, 0x14, 0x1c, 0x00, 0x08, 0x04, 0x0c,
598                 0x13, 0x1b, 0x17, 0x1f, 0x03, 0x0b, 0x07, 0x0f,
599                 0x11, 0x19, 0x15, 0x1d, 0x01, 0x09, 0x05, 0x0d } };
600
601     u8      p_varients[32] =
602             {   0xB7, 0x74, 0x85, 0xD0, 0xCC, 0xDB, 0xCA, 0x73,
603                 0x03, 0xFE, 0x31, 0x03, 0x52, 0xE0, 0xB7, 0x42,
604                 0x63, 0x16, 0xF2, 0x2A, 0x79, 0x52, 0xFF, 0x1B,
605                 0x7A, 0x11, 0xCA, 0x1A, 0x9B, 0x40, 0xAD, 0x01 };
606
607     /* The "secret" key */
608     u8      p_secret[5] = { 0x55, 0xD6, 0xC4, 0xC5, 0x28 };
609
610     u8      p_bits[30];
611     u8      p_scratch[10];
612     u8      p_tmp1[5];
613     u8      p_tmp2[5];
614     u8      i_lfsr0_o;  /* 1 bit used */
615     u8      i_lfsr1_o;  /* 1 bit used */
616     u32     i_lfsr0;
617     u32     i_lfsr1;
618     u8      i_css_varient;
619     u8      i_cse;
620     u8      i_index;
621     u8      i_combined;
622     u8      i_carry;
623     u8      i_val = 0;
624     int     i_term = 0;
625     int     i_bit;
626     int     i;
627
628     for (i = 9; i >= 0; --i)
629         p_scratch[i] = p_challenge[pp_perm_challenge[i_key_type][i]];
630
631     i_css_varient = ( i_key_type == 0 ) ? i_varient :
632                     pp_perm_varient[i_key_type-1][i_varient];
633
634     /*
635      * This encryption engine implements one of 32 variations
636      * one the same theme depending upon the choice in the
637      * varient parameter (0 - 31).
638      *
639      * The algorithm itself manipulates a 40 bit input into
640      * a 40 bit output.
641      * The parameter 'input' is 80 bits.  It consists of
642      * the 40 bit input value that is to be encrypted followed
643      * by a 40 bit seed value for the pseudo random number
644      * generators.
645      */
646
647     /* Feed the secret into the input values such that
648      * we alter the seed to the LFSR's used above,  then
649      * generate the bits to play with.
650      */
651     for( i = 5 ; --i >= 0 ; )
652     {
653         p_tmp1[i] = p_scratch[5 + i] ^ p_secret[i] ^ p_crypt_tab2[i];
654     }
655
656     /*
657      * We use two LFSR's (seeded from some of the input data bytes) to
658      * generate two streams of pseudo-random bits.  These two bit streams
659      * are then combined by simply adding with carry to generate a final
660      * sequence of pseudo-random bits which is stored in the buffer that
661      * 'output' points to the end of - len is the size of this buffer.
662      *
663      * The first LFSR is of degree 25,  and has a polynomial of:
664      * x^13 + x^5 + x^4 + x^1 + 1
665      *
666      * The second LSFR is of degree 17,  and has a (primitive) polynomial of:
667      * x^15 + x^1 + 1
668      *
669      * I don't know if these polynomials are primitive modulo 2,  and thus
670      * represent maximal-period LFSR's.
671      *
672      *
673      * Note that we take the output of each LFSR from the new shifted in
674      * bit,  not the old shifted out bit.  Thus for ease of use the LFSR's
675      * are implemented in bit reversed order.
676      *
677      */
678     
679     /* In order to ensure that the LFSR works we need to ensure that the
680      * initial values are non-zero.  Thus when we initialise them from
681      * the seed,  we ensure that a bit is set.
682      */
683     i_lfsr0 = ( p_tmp1[0] << 17 ) | ( p_tmp1[1] << 9 ) |
684               (( p_tmp1[2] & ~7 ) << 1 ) | 8 | ( p_tmp1[2] & 7 );
685     i_lfsr1 = ( p_tmp1[3] << 9 ) | 0x100 | p_tmp1[4];
686
687     i_index = sizeof(p_bits);
688     i_carry = 0;
689
690     do
691     {
692         for( i_bit = 0, i_val = 0 ; i_bit < 8 ; ++i_bit )
693         {
694
695             i_lfsr0_o = ( ( i_lfsr0 >> 24 ) ^ ( i_lfsr0 >> 21 ) ^
696                         ( i_lfsr0 >> 20 ) ^ ( i_lfsr0 >> 12 ) ) & 1;
697             i_lfsr0 = ( i_lfsr0 << 1 ) | i_lfsr0_o;
698
699             i_lfsr1_o = ( ( i_lfsr1 >> 16 ) ^ ( i_lfsr1 >> 2 ) ) & 1;
700             i_lfsr1 = ( i_lfsr1 << 1 ) | i_lfsr1_o;
701
702             i_combined = !i_lfsr1_o + i_carry + !i_lfsr0_o;
703             /* taking bit 1 */
704             i_carry = ( i_combined >> 1 ) & 1;
705             i_val |= ( i_combined & 1 ) << i_bit;
706         }
707     
708         p_bits[--i_index] = i_val;
709     } while( i_index > 0 );
710
711     /* This term is used throughout the following to
712      * select one of 32 different variations on the
713      * algorithm.
714      */
715     i_cse = p_varients[i_css_varient] ^ p_crypt_tab2[i_css_varient];
716
717     /* Now the actual blocks doing the encryption.  Each
718      * of these works on 40 bits at a time and are quite
719      * similar.
720      */
721     i_index = 0;
722     for( i = 5, i_term = 0 ; --i >= 0 ; i_term = p_scratch[i] )
723     {
724         i_index = p_bits[25 + i] ^ p_scratch[i];
725         i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse;
726
727         p_tmp1[i] = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term;
728     }
729     p_tmp1[4] ^= p_tmp1[0];
730
731     for( i = 5, i_term = 0 ; --i >= 0 ; i_term = p_tmp1[i] )
732     {
733         i_index = p_bits[20 + i] ^ p_tmp1[i];
734         i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse;
735
736         p_tmp2[i] = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term;
737     }
738     p_tmp2[4] ^= p_tmp2[0];
739
740     for( i = 5, i_term = 0 ; --i >= 0 ; i_term = p_tmp2[i] )
741     {
742         i_index = p_bits[15 + i] ^ p_tmp2[i];
743         i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse;
744         i_index = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term;
745
746         p_tmp1[i] = p_crypt_tab0[i_index] ^ p_crypt_tab2[i_index];
747     }
748     p_tmp1[4] ^= p_tmp1[0];
749
750     for( i = 5, i_term = 0 ; --i >= 0 ; i_term = p_tmp1[i] )
751     {
752         i_index = p_bits[10 + i] ^ p_tmp1[i];
753         i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse;
754
755         i_index = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term;
756
757         p_tmp2[i] = p_crypt_tab0[i_index] ^ p_crypt_tab2[i_index];
758     }
759     p_tmp2[4] ^= p_tmp2[0];
760
761     for( i = 5, i_term = 0 ; --i >= 0 ; i_term = p_tmp2[i] )
762     {
763         i_index = p_bits[5 + i] ^ p_tmp2[i];
764         i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse;
765
766         p_tmp1[i] = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term;
767     }
768     p_tmp1[4] ^= p_tmp1[0];
769
770     for(i = 5, i_term = 0 ; --i >= 0 ; i_term = p_tmp1[i] )
771     {
772         i_index = p_bits[i] ^ p_tmp1[i];
773         i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse;
774
775         p_key[i] = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term;
776     }
777
778     return;
779 }
780
781 /*****************************************************************************
782  * CSSDecryptKey: decrypt p_crypted with p_key.
783  *****************************************************************************
784  * Decryption is slightly dependant on the type of key:
785  *  -for disc key, invert is 0x00,
786  *  -for title key, invert if 0xff. 
787  *****************************************************************************/
788 static void CSSDecryptKey( u8* p_crypted, u8* p_key, u8 invert )
789 {
790     unsigned int    i_lfsr1_lo;
791     unsigned int    i_lfsr1_hi;
792     unsigned int    i_lfsr0;
793     unsigned int    i_combined;
794     byte_t          o_lfsr0;
795     byte_t          o_lfsr1;
796     byte_t          k[5];
797     int             i;
798
799     i_lfsr1_lo = p_key[0] | 0x100;
800     i_lfsr1_hi = p_key[1];
801
802     i_lfsr0    = ( ( p_key[4] << 17 )
803                  | ( p_key[3] << 9 )
804                  | ( p_key[2] << 1 ) )
805                  + 8 - ( p_key[2] & 7 );
806     i_lfsr0    = ( p_css_tab4[i_lfsr0 & 0xff] << 24 ) |
807                  ( p_css_tab4[( i_lfsr0 >> 8 ) & 0xff] << 16 ) |
808                  ( p_css_tab4[( i_lfsr0 >> 16 ) & 0xff] << 8 ) |
809                    p_css_tab4[( i_lfsr0 >> 24 ) & 0xff];
810
811     i_combined = 0;
812     for( i = 0 ; i < KEY_SIZE ; ++i )
813     {
814         o_lfsr1     = p_css_tab2[i_lfsr1_hi] ^ p_css_tab3[i_lfsr1_lo];
815         i_lfsr1_hi  = i_lfsr1_lo >> 1;
816         i_lfsr1_lo  = ( ( i_lfsr1_lo & 1 ) << 8 ) ^ o_lfsr1;
817         o_lfsr1     = p_css_tab4[o_lfsr1];
818
819         o_lfsr0 = ((((((( i_lfsr0 >> 8 ) ^ i_lfsr0 ) >> 1 )
820                         ^ i_lfsr0 ) >> 3 ) ^ i_lfsr0 ) >> 7 );
821         i_lfsr0 = ( i_lfsr0 >> 8 ) | ( o_lfsr0 << 24 );
822
823         i_combined += ( o_lfsr0 ^ invert ) + o_lfsr1;
824         k[i] = i_combined & 0xff;
825         i_combined >>= 8;
826     }
827
828     p_crypted[4] = k[4] ^ p_css_tab1[p_crypted[4]] ^ p_crypted[3];
829     p_crypted[3] = k[3] ^ p_css_tab1[p_crypted[3]] ^ p_crypted[2];
830     p_crypted[2] = k[2] ^ p_css_tab1[p_crypted[2]] ^ p_crypted[1];
831     p_crypted[1] = k[1] ^ p_css_tab1[p_crypted[1]] ^ p_crypted[0];
832     p_crypted[0] = k[0] ^ p_css_tab1[p_crypted[0]] ^ p_crypted[4];
833
834     p_crypted[4] = k[4] ^ p_css_tab1[p_crypted[4]] ^ p_crypted[3];
835     p_crypted[3] = k[3] ^ p_css_tab1[p_crypted[3]] ^ p_crypted[2];
836     p_crypted[2] = k[2] ^ p_css_tab1[p_crypted[2]] ^ p_crypted[1];
837     p_crypted[1] = k[1] ^ p_css_tab1[p_crypted[1]] ^ p_crypted[0];
838     p_crypted[0] = k[0] ^ p_css_tab1[p_crypted[0]];
839
840     return;
841 }
842
843 /*****************************************************************************
844  * CSSDiscCrack: brute force disc key
845  * CSS hash reversal function designed by Frank Stevenson
846  *****************************************************************************
847  * This function uses a big amount of memory to crack the disc key from the   
848  * disc key hash, if player keys are not available.
849  *****************************************************************************/
850 #define K1TABLEWIDTH 10
851
852 /*
853  * Simple function to test if a candidate key produces the given hash
854  */
855 static int investigate( unsigned char* hash, unsigned char *ckey )
856 {
857     unsigned char key[5];
858     unsigned char pkey[5];
859
860     memcpy( key, hash, 5 );
861     memcpy( pkey, ckey, 5 );
862
863     CSSDecryptKey( key, pkey, 0 );
864
865     return memcmp( key, pkey, 5 );
866 }
867
868 static int CSSDiscCrack( dvdcss_handle dvdcss, u8 * p_disc_key )
869 {
870     unsigned char B[5] = { 0,0,0,0,0 }; /* Second Stage of mangle cipher */
871     unsigned char C[5] = { 0,0,0,0,0 }; /* Output Stage of mangle cipher
872                                          * IntermediateKey */
873     unsigned char k[5] = { 0,0,0,0,0 }; /* Mangling cipher key
874                                          * Also output from CSS( C ) */
875     unsigned char out1[5];              /* five first output bytes of LFSR1 */
876     unsigned char out2[5];              /* five first output bytes of LFSR2 */
877     unsigned int lfsr1a;                /* upper 9 bits of LFSR1 */
878     unsigned int lfsr1b;                /* lower 8 bits of LFSR1 */
879     unsigned int tmp, tmp2, tmp3, tmp4,tmp5;
880     int i,j;
881     unsigned int nStepA;        /* iterator for LFSR1 start state */
882     unsigned int nStepB;        /* iterator for possible B[0]     */
883     unsigned int nTry;          /* iterator for K[1] possibilities */
884     unsigned int nPossibleK1;   /* #of possible K[1] values */
885     unsigned char* K1table;     /* Lookup table for possible K[1] */
886     unsigned int*  BigTable;    /* LFSR2 startstate indexed by 
887                                  * 1,2,5 output byte */
888
889     /*
890      * Prepare tables for hash reversal
891      */
892
893     
894     /* initialize lookup tables for k[1] */
895     K1table = malloc( 65536 * K1TABLEWIDTH );
896     memset( K1table, 0 , 65536 * K1TABLEWIDTH );
897     if( K1table == NULL )
898     {
899         return -1;
900     }
901
902     tmp = p_disc_key[0] ^ p_css_tab1[ p_disc_key[1] ];
903     for( i = 0 ; i < 256 ; i++ ) /* k[1] */
904     {
905         tmp2 = p_css_tab1[ tmp ^ i ]; /* p_css_tab1[ B[1] ]*/
906
907         for( j = 0 ; j < 256 ; j++ ) /* B[0] */
908         {
909             tmp3 = j ^ tmp2 ^ i; /* C[1] */
910             tmp4 = K1table[ K1TABLEWIDTH * ( 256 * j + tmp3 ) ]; /* count of entries  here */
911             tmp4++;
912 /*
913             if( tmp4 == K1TABLEWIDTH )
914             {
915                 _dvdcss_debug( dvdcss, "Table disaster %d", tmp4 );
916             }
917 */
918             if( tmp4 < K1TABLEWIDTH )
919             {
920                 K1table[ K1TABLEWIDTH * ( 256 * j + tmp3 ) +    tmp4 ] = i;
921             }
922             K1table[ K1TABLEWIDTH * ( 256 * j + tmp3 ) ] = tmp4;
923         }
924     }
925
926     /* Initing our Really big table */
927     BigTable = malloc( 16777216 * sizeof(int) );
928     memset( BigTable, 0 , 16777216 * sizeof(int) );
929     if( BigTable == NULL )
930     {
931         return -1;
932     }
933
934     tmp3 = 0;
935
936     _dvdcss_debug( dvdcss, "initializing the big table" );
937
938     for( i = 0 ; i < 16777216 ; i++ )
939     {
940 /*
941         if( ( i & 0x07ffff ) == 0 )
942         {
943             fprintf( stderr, "#" );
944         }
945 */
946         tmp = (( i + i ) & 0x1fffff0 ) | 0x8 | ( i & 0x7 );
947
948         for( j = 0 ; j < 5 ; j++ )
949         {
950             tmp2=((((((( tmp >> 3 ) ^ tmp ) >> 1 ) ^ tmp ) >> 8 )
951                                     ^ tmp ) >> 5 ) & 0xff;
952             tmp = ( tmp << 8) | tmp2;
953             out2[j] = p_css_tab4[ tmp2 ];
954         }
955
956         j = ( out2[0] << 16 ) | ( out2[1] << 8 ) | out2[4];
957         BigTable[j] = i;
958     }
959
960 /*    fprintf( stderr, "\n" ); */
961
962     /*
963      * We are done initing, now reverse hash
964      */
965     tmp5 = p_disc_key[0] ^ p_css_tab1[ p_disc_key[1] ];
966
967     for( nStepA = 0 ; nStepA < 65536 ; nStepA ++ )
968     {
969         lfsr1a = 0x100 | ( nStepA >> 8 );
970         lfsr1b = nStepA & 0xff;
971
972         /* Generate 5 first output bytes from lfsr1 */
973         for( i = 0 ; i < 5 ; i++ )
974         {
975             tmp = p_css_tab2[ lfsr1b ] ^ p_css_tab3[ lfsr1a ];
976             lfsr1b = lfsr1a >> 1;
977             lfsr1a = ((lfsr1a&1)<<8) ^ tmp;
978             out1[ i ] = p_css_tab4[ tmp ];
979         }
980
981         /* cumpute and cache some variables */
982         C[0] = nStepA >> 8;
983         C[1] = nStepA & 0xff;
984         tmp = p_disc_key[3] ^ p_css_tab1[ p_disc_key[4] ];
985         tmp2 = p_css_tab1[ p_disc_key[0] ];
986
987         /* Search through all possible B[0] */
988         for( nStepB = 0 ; nStepB < 256 ; nStepB++ )
989         {
990             /* reverse parts of the mangling cipher */
991             B[0] = nStepB;
992             k[0] = p_css_tab1[ B[0] ] ^ C[0];
993             B[4] = B[0] ^ k[0] ^ tmp2;
994             k[4] = B[4] ^ tmp;
995             nPossibleK1 = K1table[ K1TABLEWIDTH * (256 * B[0] + C[1]) ];
996
997             /* Try out all possible values for k[1] */
998             for( nTry = 0 ; nTry < nPossibleK1 ; nTry++ )
999             {
1000                 k[1] = K1table[ K1TABLEWIDTH * (256 * B[0] + C[1]) + nTry + 1 ];
1001                 B[1] = tmp5 ^ k[1];
1002
1003                 /* reconstruct output from LFSR2 */
1004                 tmp3 = ( 0x100 + k[0] - out1[0] );
1005                 out2[0] = tmp3 & 0xff;
1006                 tmp3 = tmp3 & 0x100 ? 0x100 : 0xff;
1007                 tmp3 = ( tmp3 + k[1] - out1[1] );
1008                 out2[1] = tmp3 & 0xff;
1009                 tmp3 = ( 0x100 + k[4] - out1[4] );
1010                 out2[4] = tmp3 & 0xff;  /* Can be 1 off  */
1011
1012                 /* test first possible out2[4] */
1013                 tmp4 = ( out2[0] << 16 ) | ( out2[1] << 8 ) | out2[4];
1014                 tmp4 = BigTable[ tmp4 ];
1015                 C[2] = tmp4 & 0xff;
1016                 C[3] = ( tmp4 >> 8 ) & 0xff;
1017                 C[4] = ( tmp4 >> 16 ) & 0xff;
1018                 B[3] = p_css_tab1[ B[4] ] ^ k[4] ^ C[4];
1019                 k[3] = p_disc_key[2] ^ p_css_tab1[ p_disc_key[3] ] ^ B[3];
1020                 B[2] = p_css_tab1[ B[3] ] ^ k[3] ^ C[3];
1021                 k[2] = p_disc_key[1] ^ p_css_tab1[ p_disc_key[2] ] ^ B[2];
1022
1023                 if( ( B[1] ^ p_css_tab1[ B[2] ] ^ k[ 2 ]  ) == C[ 2 ] )
1024                 {
1025                     if( ! investigate( &p_disc_key[0] , &C[0] ) )
1026                     {
1027                         goto end;
1028                     }
1029                 }
1030
1031                 /* Test second possible out2[4] */
1032                 out2[4] = ( out2[4] + 0xff ) & 0xff;
1033                 tmp4 = ( out2[0] << 16 ) | ( out2[1] << 8 ) | out2[4];
1034                 tmp4 = BigTable[ tmp4 ];
1035                 C[2] = tmp4 & 0xff;
1036                 C[3] = ( tmp4 >> 8 ) & 0xff;
1037                 C[4] = ( tmp4 >> 16 ) & 0xff;
1038                 B[3] = p_css_tab1[ B[4] ] ^ k[4] ^ C[4];
1039                 k[3] = p_disc_key[2] ^ p_css_tab1[ p_disc_key[3] ] ^ B[3];
1040                 B[2] = p_css_tab1[ B[3] ] ^ k[3] ^ C[3];
1041                 k[2] = p_disc_key[1] ^ p_css_tab1[ p_disc_key[2] ] ^ B[2];
1042
1043                 if( ( B[1] ^ p_css_tab1[ B[2] ] ^ k[ 2 ]  ) == C[ 2 ] )
1044                 {
1045                     if( ! investigate( &p_disc_key[0] , &C[0] ) )
1046                     {
1047                         goto end;
1048                     }
1049                 }
1050             }
1051         }
1052     }
1053
1054 end:
1055
1056     memcpy( p_disc_key, &C[0], KEY_SIZE );
1057
1058     free( K1table );
1059     free( BigTable );
1060
1061     return( 0 );
1062 }
1063
1064 /*****************************************************************************
1065  * CSSTitleCrack : title key decryption by cracking
1066  * Function designed by Frank Stevenson
1067  *****************************************************************************
1068  * This function is called by CSSGetTitleKey to find a title key, if we've
1069  * chosen to crack title key instead of decrypting it with the disc key.
1070  *****************************************************************************/
1071 static int CSSTitleCrack( int i_start,
1072                           unsigned char * p_crypted,
1073                           unsigned char * p_decrypted,
1074                           dvd_key_t * p_sector_key,
1075                           dvd_key_t * p_key )
1076 {
1077     unsigned char p_buffer[10];
1078     unsigned int i_t1, i_t2, i_t3, i_t4, i_t5, i_t6;
1079     unsigned int i_try;
1080     unsigned int i_candidate;
1081     unsigned int i, j;
1082     int i_exit = -1;
1083
1084
1085     for( i = 0 ; i < 10 ; i++ )
1086     {
1087         p_buffer[i] = p_css_tab1[p_crypted[i]] ^ p_decrypted[i];
1088     }
1089
1090     for( i_try = i_start ; i_try < 0x10000 ; i_try++ )
1091     {
1092         i_t1 = i_try >> 8 | 0x100;
1093         i_t2 = i_try & 0xff;
1094         i_t3 = 0;               /* not needed */
1095         i_t5 = 0;
1096
1097         /* iterate cipher 4 times to reconstruct LFSR2 */
1098         for( i = 0 ; i < 4 ; i++ )
1099         {
1100             /* advance LFSR1 normaly */
1101             i_t4 = p_css_tab2[i_t2] ^ p_css_tab3[i_t1];
1102             i_t2 = i_t1 >> 1;
1103             i_t1 = ( ( i_t1 & 1 ) << 8 ) ^ i_t4;
1104             i_t4 = p_css_tab5[i_t4];
1105             /* deduce i_t6 & i_t5 */
1106             i_t6 = p_buffer[i];
1107             if( i_t5 )
1108             {
1109                 i_t6 = ( i_t6 + 0xff ) & 0x0ff;
1110             }
1111             if( i_t6 < i_t4 )
1112             {
1113                 i_t6 += 0x100;
1114             }
1115             i_t6 -= i_t4;
1116             i_t5 += i_t6 + i_t4;
1117             i_t6 = p_css_tab4[ i_t6 ];
1118             /* feed / advance i_t3 / i_t5 */
1119             i_t3 = ( i_t3 << 8 ) | i_t6;
1120             i_t5 >>= 8;
1121         }
1122
1123         i_candidate = i_t3;
1124
1125         /* iterate 6 more times to validate candidate key */
1126         for( ; i < 10 ; i++ )
1127         {
1128             i_t4 = p_css_tab2[i_t2] ^ p_css_tab3[i_t1];
1129             i_t2 = i_t1 >> 1;
1130             i_t1 = ( ( i_t1 & 1 ) << 8 ) ^ i_t4;
1131             i_t4 = p_css_tab5[i_t4];
1132             i_t6 = ((((((( i_t3 >> 3 ) ^ i_t3 ) >> 1 ) ^
1133                                          i_t3 ) >> 8 ) ^ i_t3 ) >> 5 ) & 0xff;
1134             i_t3 = ( i_t3 << 8 ) | i_t6;
1135             i_t6 = p_css_tab4[i_t6];
1136             i_t5 += i_t6 + i_t4;
1137             if( ( i_t5 & 0xff ) != p_buffer[i] )
1138             {
1139                 break;
1140             }
1141
1142             i_t5 >>= 8;
1143         }
1144
1145         if( i == 10 )
1146         {
1147             /* Do 4 backwards steps of iterating t3 to deduce initial state */
1148             i_t3 = i_candidate;
1149             for( i = 0 ; i < 4 ; i++ )
1150             {
1151                 i_t1 = i_t3 & 0xff;
1152                 i_t3 = ( i_t3 >> 8 );
1153                 /* easy to code, and fast enough bruteforce
1154                  * search for byte shifted in */
1155                 for( j = 0 ; j < 256 ; j++ )
1156                 {
1157                     i_t3 = ( i_t3 & 0x1ffff) | ( j << 17 );
1158                     i_t6 = ((((((( i_t3 >> 3 ) ^ i_t3 ) >> 1 ) ^
1159                                    i_t3 ) >> 8 ) ^ i_t3 ) >> 5 ) & 0xff;
1160                     if( i_t6 == i_t1 )
1161                     {
1162                         break;
1163                     }
1164                 }
1165             }
1166
1167             i_t4 = ( i_t3 >> 1 ) - 4;
1168             for( i_t5 = 0 ; i_t5 < 8; i_t5++ )
1169             {
1170                 if( ( ( i_t4 + i_t5 ) * 2 + 8 - ( (i_t4 + i_t5 ) & 7 ) )
1171                                                                       == i_t3 )
1172                 {
1173                     (*p_key)[0] = i_try>>8;
1174                     (*p_key)[1] = i_try & 0xFF;
1175                     (*p_key)[2] = ( ( i_t4 + i_t5 ) >> 0) & 0xFF;
1176                     (*p_key)[3] = ( ( i_t4 + i_t5 ) >> 8) & 0xFF;
1177                     (*p_key)[4] = ( ( i_t4 + i_t5 ) >> 16) & 0xFF;
1178                     i_exit = i_try + 1;
1179                 }
1180             }
1181         }
1182     }
1183
1184     if( i_exit >= 0 )
1185     {
1186         (*p_key)[0] ^= (*p_sector_key)[0];
1187         (*p_key)[1] ^= (*p_sector_key)[1];
1188         (*p_key)[2] ^= (*p_sector_key)[2];
1189         (*p_key)[3] ^= (*p_sector_key)[3];
1190         (*p_key)[4] ^= (*p_sector_key)[4];
1191     }
1192
1193     return i_exit;
1194 }