css.c

来自「君正早期ucos系统(只有早期的才不没有打包成库),MPLAYER,文件系统,图」· C语言 代码 · 共 1,697 行 · 第 1/4 页

C
1,697
字号
        i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse;        p_tmp1[i] = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term;    }    p_tmp1[4] ^= p_tmp1[0];    for( i = 5, i_term = 0 ; --i >= 0 ; i_term = p_tmp1[i] )    {        i_index = p_bits[20 + i] ^ p_tmp1[i];        i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse;        p_tmp2[i] = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term;    }    p_tmp2[4] ^= p_tmp2[0];    for( i = 5, i_term = 0 ; --i >= 0 ; i_term = p_tmp2[i] )    {        i_index = p_bits[15 + i] ^ p_tmp2[i];        i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse;        i_index = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term;        p_tmp1[i] = p_crypt_tab0[i_index] ^ p_crypt_tab2[i_index];    }    p_tmp1[4] ^= p_tmp1[0];    for( i = 5, i_term = 0 ; --i >= 0 ; i_term = p_tmp1[i] )    {        i_index = p_bits[10 + i] ^ p_tmp1[i];        i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse;        i_index = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term;        p_tmp2[i] = p_crypt_tab0[i_index] ^ p_crypt_tab2[i_index];    }    p_tmp2[4] ^= p_tmp2[0];    for( i = 5, i_term = 0 ; --i >= 0 ; i_term = p_tmp2[i] )    {        i_index = p_bits[5 + i] ^ p_tmp2[i];        i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse;        p_tmp1[i] = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term;    }    p_tmp1[4] ^= p_tmp1[0];    for(i = 5, i_term = 0 ; --i >= 0 ; i_term = p_tmp1[i] )    {        i_index = p_bits[i] ^ p_tmp1[i];        i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse;        p_key[i] = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term;    }    return;}/***************************************************************************** * DecryptKey: decrypt p_crypted with p_key. ***************************************************************************** * Used to decrypt the disc key, with a player key, after requesting it * in _dvdcss_disckey and to decrypt title keys, with a disc key, requested * in _dvdcss_titlekey. * The player keys and the resulting disc key are only used as KEKs * (key encryption keys). * Decryption is slightly dependant on the type of key: *  -for disc key, invert is 0x00, *  -for title key, invert if 0xff. *****************************************************************************/static void DecryptKey( uint8_t invert, uint8_t const *p_key,                        uint8_t const *p_crypted, uint8_t *p_result ){    unsigned int    i_lfsr1_lo;    unsigned int    i_lfsr1_hi;    unsigned int    i_lfsr0;    unsigned int    i_combined;    uint8_t         o_lfsr0;    uint8_t         o_lfsr1;    uint8_t         k[5];    int             i;    i_lfsr1_lo = p_key[0] | 0x100;    i_lfsr1_hi = p_key[1];    i_lfsr0    = ( ( p_key[4] << 17 )                 | ( p_key[3] << 9 )                 | ( p_key[2] << 1 ) )                 + 8 - ( p_key[2] & 7 );    i_lfsr0    = ( p_css_tab4[i_lfsr0 & 0xff] << 24 ) |                 ( p_css_tab4[( i_lfsr0 >> 8 ) & 0xff] << 16 ) |                 ( p_css_tab4[( i_lfsr0 >> 16 ) & 0xff] << 8 ) |                   p_css_tab4[( i_lfsr0 >> 24 ) & 0xff];    i_combined = 0;    for( i = 0 ; i < KEY_SIZE ; ++i )    {        o_lfsr1     = p_css_tab2[i_lfsr1_hi] ^ p_css_tab3[i_lfsr1_lo];        i_lfsr1_hi  = i_lfsr1_lo >> 1;        i_lfsr1_lo  = ( ( i_lfsr1_lo & 1 ) << 8 ) ^ o_lfsr1;        o_lfsr1     = p_css_tab4[o_lfsr1];        o_lfsr0 = ((((((( i_lfsr0 >> 8 ) ^ i_lfsr0 ) >> 1 )                        ^ i_lfsr0 ) >> 3 ) ^ i_lfsr0 ) >> 7 );        i_lfsr0 = ( i_lfsr0 >> 8 ) | ( o_lfsr0 << 24 );        i_combined += ( o_lfsr0 ^ invert ) + o_lfsr1;        k[i] = i_combined & 0xff;        i_combined >>= 8;    }    p_result[4] = k[4] ^ p_css_tab1[p_crypted[4]] ^ p_crypted[3];    p_result[3] = k[3] ^ p_css_tab1[p_crypted[3]] ^ p_crypted[2];    p_result[2] = k[2] ^ p_css_tab1[p_crypted[2]] ^ p_crypted[1];    p_result[1] = k[1] ^ p_css_tab1[p_crypted[1]] ^ p_crypted[0];    p_result[0] = k[0] ^ p_css_tab1[p_crypted[0]] ^ p_result[4];    p_result[4] = k[4] ^ p_css_tab1[p_result[4]] ^ p_result[3];    p_result[3] = k[3] ^ p_css_tab1[p_result[3]] ^ p_result[2];    p_result[2] = k[2] ^ p_css_tab1[p_result[2]] ^ p_result[1];    p_result[1] = k[1] ^ p_css_tab1[p_result[1]] ^ p_result[0];    p_result[0] = k[0] ^ p_css_tab1[p_result[0]];    return;}/***************************************************************************** * player_keys: alternate DVD player keys ***************************************************************************** * These player keys were generated using Frank A. Stevenson's PlayerKey * cracker. A copy of his article can be found here: * http://www-2.cs.cmu.edu/~dst/DeCSS/FrankStevenson/mail2.txt *****************************************************************************/static const dvd_key_t player_keys[] ={    { 0x01, 0xaf, 0xe3, 0x12, 0x80 },    { 0x12, 0x11, 0xca, 0x04, 0x3b },    { 0x14, 0x0c, 0x9e, 0xd0, 0x09 },    { 0x14, 0x71, 0x35, 0xba, 0xe2 },    { 0x1a, 0xa4, 0x33, 0x21, 0xa6 },    { 0x26, 0xec, 0xc4, 0xa7, 0x4e },    { 0x2c, 0xb2, 0xc1, 0x09, 0xee },    { 0x2f, 0x25, 0x9e, 0x96, 0xdd },    { 0x33, 0x2f, 0x49, 0x6c, 0xe0 },    { 0x35, 0x5b, 0xc1, 0x31, 0x0f },    { 0x36, 0x67, 0xb2, 0xe3, 0x85 },    { 0x39, 0x3d, 0xf1, 0xf1, 0xbd },    { 0x3b, 0x31, 0x34, 0x0d, 0x91 },    { 0x45, 0xed, 0x28, 0xeb, 0xd3 },    { 0x48, 0xb7, 0x6c, 0xce, 0x69 },    { 0x4b, 0x65, 0x0d, 0xc1, 0xee },    { 0x4c, 0xbb, 0xf5, 0x5b, 0x23 },    { 0x51, 0x67, 0x67, 0xc5, 0xe0 },    { 0x53, 0x94, 0xe1, 0x75, 0xbf },    { 0x57, 0x2c, 0x8b, 0x31, 0xae },    { 0x63, 0xdb, 0x4c, 0x5b, 0x4a },    { 0x7b, 0x1e, 0x5e, 0x2b, 0x57 },    { 0x85, 0xf3, 0x85, 0xa0, 0xe0 },    { 0xab, 0x1e, 0xe7, 0x7b, 0x72 },    { 0xab, 0x36, 0xe3, 0xeb, 0x76 },    { 0xb1, 0xb8, 0xf9, 0x38, 0x03 },    { 0xb8, 0x5d, 0xd8, 0x53, 0xbd },    { 0xbf, 0x92, 0xc3, 0xb0, 0xe2 },    { 0xcf, 0x1a, 0xb2, 0xf8, 0x0a },    { 0xec, 0xa0, 0xcf, 0xb3, 0xff },    { 0xfc, 0x95, 0xa9, 0x87, 0x35 }};/***************************************************************************** * DecryptDiscKey ***************************************************************************** * Decryption of the disc key with player keys: try to decrypt the disc key * from every position with every player key. * p_struct_disckey: the 2048 byte DVD_STRUCT_DISCKEY data * p_disc_key: result, the 5 byte disc key *****************************************************************************/static int DecryptDiscKey( dvdcss_t dvdcss, uint8_t const *p_struct_disckey,                           dvd_key_t p_disc_key ){    uint8_t p_verify[KEY_SIZE];    unsigned int i, n = 0;    /* Decrypt disc key with the above player keys */    for( n = 0; n < sizeof(player_keys) / sizeof(dvd_key_t); n++ )    {        PrintKey( dvdcss, "trying player key ", player_keys[n] );        for( i = 1; i < 409; i++ )        {            /* Check if player key n is the right key for position i. */            DecryptKey( 0, player_keys[n], p_struct_disckey + 5 * i,                        p_disc_key );            /* The first part in the struct_disckey block is the             * 'disc key' encrypted with itself.  Using this we             * can check if we decrypted the correct key. */            DecryptKey( 0, p_disc_key, p_struct_disckey, p_verify );            /* If the position / player key pair worked then return. */            if( memcmp( p_disc_key, p_verify, KEY_SIZE ) == 0 )            {                return 0;            }        }    }    /* Have tried all combinations of positions and keys,     * and we still didn't succeed. */    memset( p_disc_key, 0, KEY_SIZE );    return -1;}/***************************************************************************** * DecryptTitleKey ***************************************************************************** * Decrypt the title key using the disc key. * p_disc_key: result, the 5 byte disc key * p_titlekey: the encrypted title key, gets overwritten by the decrypted key *****************************************************************************/static void DecryptTitleKey( dvd_key_t p_disc_key, dvd_key_t p_titlekey ){    DecryptKey( 0xff, p_disc_key, p_titlekey, p_titlekey );}/***************************************************************************** * CrackDiscKey: brute force disc key * CSS hash reversal function designed by Frank Stevenson ***************************************************************************** * This function uses a big amount of memory to crack the disc key from the * disc key hash, if player keys are not available. *****************************************************************************/#define K1TABLEWIDTH 10/* * Simple function to test if a candidate key produces the given hash */static int investigate( unsigned char *hash, unsigned char *ckey ){    unsigned char key[KEY_SIZE];    DecryptKey( 0, ckey, hash, key );    return memcmp( key, ckey, KEY_SIZE );}static int CrackDiscKey( dvdcss_t dvdcss, uint8_t *p_disc_key ){    unsigned char B[5] = { 0,0,0,0,0 }; /* Second Stage of mangle cipher */    unsigned char C[5] = { 0,0,0,0,0 }; /* Output Stage of mangle cipher                                         * IntermediateKey */    unsigned char k[5] = { 0,0,0,0,0 }; /* Mangling cipher key                                         * Also output from CSS( C ) */    unsigned char out1[5];              /* five first output bytes of LFSR1 */    unsigned char out2[5];              /* five first output bytes of LFSR2 */    unsigned int lfsr1a;                /* upper 9 bits of LFSR1 */    unsigned int lfsr1b;                /* lower 8 bits of LFSR1 */    unsigned int tmp, tmp2, tmp3, tmp4,tmp5;    int i,j;    unsigned int nStepA;        /* iterator for LFSR1 start state */    unsigned int nStepB;        /* iterator for possible B[0]     */    unsigned int nTry;          /* iterator for K[1] possibilities */    unsigned int nPossibleK1;   /* #of possible K[1] values */    unsigned char* K1table;     /* Lookup table for possible K[1] */    unsigned int*  BigTable;    /* LFSR2 startstate indexed by                                 * 1,2,5 output byte */    /*     * Prepare tables for hash reversal     */    /* initialize lookup tables for k[1] */    K1table = malloc( 65536 * K1TABLEWIDTH );    memset( K1table, 0 , 65536 * K1TABLEWIDTH );    if( K1table == NULL )    {        return -1;    }    tmp = p_disc_key[0] ^ p_css_tab1[ p_disc_key[1] ];    for( i = 0 ; i < 256 ; i++ ) /* k[1] */    {        tmp2 = p_css_tab1[ tmp ^ i ]; /* p_css_tab1[ B[1] ]*/        for( j = 0 ; j < 256 ; j++ ) /* B[0] */        {            tmp3 = j ^ tmp2 ^ i; /* C[1] */            tmp4 = K1table[ K1TABLEWIDTH * ( 256 * j + tmp3 ) ]; /* count of entries  here */            tmp4++;/*            if( tmp4 == K1TABLEWIDTH )            {                print_debug( dvdcss, "Table disaster %d", tmp4 );            }*/            if( tmp4 < K1TABLEWIDTH )            {                K1table[ K1TABLEWIDTH * ( 256 * j + tmp3 ) +    tmp4 ] = i;            }            K1table[ K1TABLEWIDTH * ( 256 * j + tmp3 ) ] = tmp4;        }    }    /* Initing our Really big table */    BigTable = malloc( 16777216 * sizeof(int) );    memset( BigTable, 0 , 16777216 * sizeof(int) );    if( BigTable == NULL )    {        return -1;    }    tmp3 = 0;    print_debug( dvdcss, "initializing the big table" );    for( i = 0 ; i < 16777216 ; i++ )    {        tmp = (( i + i ) & 0x1fffff0 ) | 0x8 | ( i & 0x7 );        for( j = 0 ; j < 5 ; j++ )        {            tmp2=((((((( tmp >> 3 ) ^ tmp ) >> 1 ) ^ tmp ) >> 8 )                                    ^ tmp ) >> 5 ) & 0xff;            tmp = ( tmp << 8) | tmp2;            out2[j] = p_css_tab4[ tmp2 ];        }        j = ( out2[0] << 16 ) | ( out2[1] << 8 ) | out2[4];        BigTable[j] = i;    }    /*     * We are done initing, now reverse hash     */    tmp5 = p_disc_key[0] ^ p_css_tab1[ p_disc_key[1] ];    for( nStepA = 0 ; nStepA < 65536 ; nStepA ++ )    {        lfsr1a = 0x100 | ( nStepA >> 8 );        lfsr1b = nStepA & 0xff;        /* Generate 5 first output bytes from lfsr1 */        for( i = 0 ; i < 5 ; i++ )        {            tmp = p_css_tab2[ lfsr1b ] ^ p_css_tab3[ lfsr1a ];            lfsr1b = lfsr1a >> 1;            lfsr1a = ((lfsr1a&1)<<8) ^ tmp;            out1[ i ] = p_css_tab4[ tmp ];        }        /* cumpute and cache some variables */        C[0] = nStepA >> 8;        C[1] = nStepA & 0xff;        tmp = p_disc_key[3] ^ p_css_tab1[ p_disc_key[4] ];        tmp2 = p_css_tab1[ p_disc_key[0] ];        /* Search through all possible B[0] */        for( nStepB = 0 ; nStepB < 256 ; nStepB++ )        {            /* reverse parts of the mangling cipher */            B[0] = nStepB;            k[0] = p_css_tab1[ B[0] ] ^ C[0];            B[4] = B[0] ^ k[0] ^ tmp2;            k[4] = B[4] ^ tmp;            nPossibleK1 = K1table[ K1TABLEWIDTH * (256 * B[0] + C[1]) ];            /* Try out all possible values for k[1] */            for( nTry = 0 ; nTry < nPossibleK1 ; nTry++ )            {                k[1] = K1table[ K1TABLEWIDTH * (256 * B[0] + C[1]) + nTry + 1 ];                B[1] = tmp5 ^ k[1];                /* reconstruct output from LFSR2 */                tmp3 = ( 0x100 + k[0] - out1[0] );                out2[0] = tmp3 & 0xff;                tmp3 = tmp3 & 0x100 ? 0x100 : 0xff;                tmp3 = ( tmp3 + k[1] - out1[1] );                out2[1] = tmp3 & 0xff;                tmp3 = ( 0x100 + k[4] - out1[4] );                out2[4] = tmp3 & 0xff;  /* Can be 1 off  */                /* test first possible out2[4] */                tmp4 = ( out2[0] << 16 ) | ( out2[1] << 8 ) | out2[4];                tmp4 = BigTable[ tmp4 ];                C[2] = tmp4 & 0xff;                C[3] = ( tmp4 >> 8 ) & 0xff;                C[4] = ( tmp4 >> 16 ) & 0xff;                B[3] = p_css_tab1[ B[4] ] ^ k[4] ^ C[4];                k[3] = p_disc_key[2] ^ p_css_tab1[ p_disc_key[3] ] ^ B[3];                B[2] = p_css_tab1[ B[3] ] ^ k[3] ^ C[3];                k[2] = p_disc_key[1] ^ p_css_tab1[ p_disc_key[2] ] ^ B[2];                if( ( B[1] ^ p_css_tab1[ B[2] ] ^ k[ 2 ]  ) == C[ 2 ] )                {                    if( ! investigate( &p_disc_key[0] , &C[0] ) )                    {                        goto end;                    }                }                /* Test second possible out2[4] */                out2[4] = ( out2[4] + 0xff ) & 0xff;                tmp4 = ( out2[0] << 16 ) | ( out2[1] << 8 ) | out2[4];                tmp4 = BigTable[ tmp4 ];                C[2] = tmp4 & 0xff;                C[3] = ( tmp4 >> 8 ) & 0xff;                C[4] = ( tmp4 >> 16 ) & 0xff;                B[3] = p_css_tab1[ B[4] ] ^ k[4] ^ C[4];                k[3] = p_disc_key[2] ^ p_css_tab1[ p_disc_key[3] ] ^ B[3];                B[2] = p_css_tab1[ B[3] ] ^ k[3] ^ C[3];                k[2] = p_disc_key[1] ^ p_css_tab1[ p_disc_key[2] ] ^ B[2];                if( ( B[1] ^ p_css_tab1[ B[2] ] ^ k[ 2 ]  ) == C[ 2 ] )                {                    if( ! investigate( &p_disc_key[0] , &C[0] ) )                    {                        goto end;                    }                }            }        }    }end:    memcpy( p_disc_key, &C[0], KEY_SIZE );    free( K1table );

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?