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