📄 drms.c
字号:
/***************************************************************************** * drms.c : DRMS ***************************************************************************** * Copyright (C) 2004 VideoLAN * $Id: drms.c,v 1.1 2006/02/23 14:32:29 kevin-fu Exp $ * * Author: Jon Lech Johansen <jon-vl@nanocrew.net> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. *****************************************************************************/#include <stdlib.h> /* malloc(), free() */#include "mp4ffint.h"#ifdef ITUNES_DRM#ifdef _WIN32#include <tchar.h>#include <shlobj.h>#include <windows.h>#endif#include "drms.h"#include "drmstables.h"static __inline uint32_t U32_AT( void * _p ){ uint8_t * p = (uint8_t *)_p; return ( ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) | ((uint32_t)p[2] << 8) | p[3] );}#define TAOS_INIT( tmp, i ) \ memset( tmp, 0, sizeof(tmp) ); \ tmp[ i + 0 ] = 0x67452301; \ tmp[ i + 1 ] = 0xEFCDAB89; \ tmp[ i + 2 ] = 0x98BADCFE; \ tmp[ i + 3 ] = 0x10325476;#define ROR( x, n ) (((x) << (32-(n))) | ((x) >> (n)))static void init_ctx( uint32_t *p_ctx, uint32_t *p_input ){ uint32_t i; uint32_t p_tmp[ 6 ]; p_ctx[ 0 ] = sizeof(*p_input); memset( &p_ctx[ 1 + 4 ], 0, sizeof(*p_input) * 4 ); memcpy( &p_ctx[ 1 + 0 ], p_input, sizeof(*p_input) * 4 ); p_tmp[ 0 ] = p_ctx[ 1 + 3 ]; for( i = 0; i < sizeof(p_drms_tab1)/sizeof(p_drms_tab1[ 0 ]); i++ ) { p_tmp[ 0 ] = ROR( p_tmp[ 0 ], 8 );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], 8 ) ^ ROR( p_tmp[ 2 ], 24 ); p_ctx[ 1 + 4 + 64 + (i - 4) ] = p_tmp[ 3 ] ^ p_tmp[ 4 ] ^ p_tmp[ 1 ] ^ p_tmp[ 0 ]; }}static void ctx_xor( uint32_t *p_ctx, uint32_t *p_in, uint32_t *p_out, uint32_t p_table1[ 256 ], uint32_t p_table2[ 256 ] ){ uint32_t i, x, y; uint32_t p_tmp1[ 4 ]; uint32_t p_tmp2[ 4 ]; i = p_ctx[ 0 ] * 4; p_tmp1[ 0 ] = p_ctx[ 1 + i + 24 ] ^ p_in[ 0 ]; p_tmp1[ 1 ] = p_ctx[ 1 + i + 25 ] ^ p_in[ 1 ]; p_tmp1[ 2 ] = p_ctx[ 1 + i + 26 ] ^ p_in[ 2 ]; p_tmp1[ 3 ] = p_ctx[ 1 + i + 27 ] ^ p_in[ 3 ]; i += 84;#define XOR_ROR( p_table, p_tmp, i_ctx ) \ p_table[ (p_tmp[ y > 2 ? y - 3 : y + 1 ] >> 24) & 0xFF ] \ ^ ROR( p_table[ (p_tmp[ y > 1 ? y - 2 : y + 2 ] >> 16) & 0xFF ], 8 ) \ ^ ROR( p_table[ (p_tmp[ y > 0 ? y - 1 : y + 3 ] >> 8) & 0xFF ], 16 ) \ ^ ROR( p_table[ p_tmp[ y ] & 0xFF ], 24 ) \ ^ p_ctx[ i_ctx ] for( x = 0; x < 1; x++ ) { memcpy( p_tmp2, p_tmp1, sizeof(p_tmp1) ); for( y = 0; y < 4; y++ ) { p_tmp1[ y ] = XOR_ROR( p_table1, p_tmp2, 1 + i - x + y ); } } for( ; x < 9; x++ ) { memcpy( p_tmp2, p_tmp1, sizeof(p_tmp1) ); for( y = 0; y < 4; y++ ) { p_tmp1[ y ] = XOR_ROR( p_table1, p_tmp2, 1 + i - x - ((x * 3) - y) ); } } for( y = 0; y < 4; y++ ) { p_out[ y ] = XOR_ROR( p_table2, p_tmp1, 1 + i - x - ((x * 3) - y) ); }#undef XOR_ROR}static void taos( uint32_t *p_buffer, uint32_t *p_input ){ uint32_t i; uint32_t x = 0; uint32_t p_tmp1[ 4 ]; uint32_t p_tmp2[ 4 ]; memcpy( p_tmp1, p_buffer, sizeof(p_tmp1) ); p_tmp2[ 0 ] = ((~p_tmp1[ 1 ] & p_tmp1[ 3 ]) | (p_tmp1[ 2 ] & p_tmp1[ 1 ])) + p_input[ x ]; p_tmp1[ 0 ] = p_tmp2[ 0 ] + p_tmp1[ 0 ] + p_drms_tab_taos[ x++ ]; for( i = 0; i < 4; i++ ) { p_tmp2[ 0 ] = ((p_tmp1[ 0 ] >> 0x19) | (p_tmp1[ 0 ] << 0x7)) + p_tmp1[ 1 ]; p_tmp2[ 1 ] = ((~p_tmp2[ 0 ] & p_tmp1[ 2 ]) | (p_tmp1[ 1 ] & p_tmp2[ 0 ])) + p_input[ x ]; p_tmp2[ 1 ] += p_tmp1[ 3 ] + p_drms_tab_taos[ x++ ]; p_tmp1[ 3 ] = ((p_tmp2[ 1 ] >> 0x14) | (p_tmp2[ 1 ] << 0xC)) + p_tmp2[ 0 ]; p_tmp2[ 1 ] = ((~p_tmp1[ 3 ] & p_tmp1[ 1 ]) | (p_tmp1[ 3 ] & p_tmp2[ 0 ])) + p_input[ x ]; p_tmp2[ 1 ] += p_tmp1[ 2 ] + p_drms_tab_taos[ x++ ]; p_tmp1[ 2 ] = ((p_tmp2[ 1 ] >> 0xF) | (p_tmp2[ 1 ] << 0x11)) + p_tmp1[ 3 ]; p_tmp2[ 1 ] = ((~p_tmp1[ 2 ] & p_tmp2[ 0 ]) | (p_tmp1[ 3 ] & p_tmp1[ 2 ])) + p_input[ x ]; p_tmp2[ 2 ] = p_tmp2[ 1 ] + p_tmp1[ 1 ] + p_drms_tab_taos[ x++ ]; p_tmp1[ 1 ] = ((p_tmp2[ 2 ] << 0x16) | (p_tmp2[ 2 ] >> 0xA)) + p_tmp1[ 2 ]; if( i == 3 ) { p_tmp2[ 1 ] = ((~p_tmp1[ 3 ] & p_tmp1[ 2 ]) | (p_tmp1[ 3 ] & p_tmp1[ 1 ])) + p_input[ 1 ]; } else { p_tmp2[ 1 ] = ((~p_tmp1[ 1 ] & p_tmp1[ 3 ]) | (p_tmp1[ 2 ] & p_tmp1[ 1 ])) + p_input[ x ]; } p_tmp1[ 0 ] = p_tmp2[ 0 ] + p_tmp2[ 1 ] + p_drms_tab_taos[ x++ ]; } for( i = 0; i < 4; i++ ) { uint8_t p_table[ 4 ][ 4 ] = { { 6, 11, 0, 5 }, { 10, 15, 4, 9 }, { 14, 3, 8, 13 }, { 2, 7, 12, 5 } }; p_tmp2[ 0 ] = ((p_tmp1[ 0 ] >> 0x1B) | (p_tmp1[ 0 ] << 0x5)) + p_tmp1[ 1 ]; p_tmp2[ 1 ] = ((~p_tmp1[ 2 ] & p_tmp1[ 1 ]) | (p_tmp1[ 2 ] & p_tmp2[ 0 ])) + p_input[ p_table[ i ][ 0 ] ]; p_tmp2[ 1 ] += p_tmp1[ 3 ] + p_drms_tab_taos[ x++ ]; p_tmp1[ 3 ] = ((p_tmp2[ 1 ] >> 0x17) | (p_tmp2[ 1 ] << 0x9)) + p_tmp2[ 0 ]; p_tmp2[ 1 ] = ((~p_tmp1[ 1 ] & p_tmp2[ 0 ]) | (p_tmp1[ 3 ] & p_tmp1[ 1 ])) + p_input[ p_table[ i ][ 1 ] ]; p_tmp2[ 1 ] += p_tmp1[ 2 ] + p_drms_tab_taos[ x++ ]; p_tmp1[ 2 ] = ((p_tmp2[ 1 ] >> 0x12) | (p_tmp2[ 1 ] << 0xE)) + p_tmp1[ 3 ]; p_tmp2[ 1 ] = ((~p_tmp2[ 0 ] & p_tmp1[ 3 ]) | (p_tmp1[ 2 ] & p_tmp2[ 0 ])) + p_input[ p_table[ i ][ 2 ] ]; p_tmp2[ 1 ] += p_tmp1[ 1 ] + p_drms_tab_taos[ x++ ]; p_tmp1[ 1 ] = ((p_tmp2[ 1 ] << 0x14) | (p_tmp2[ 1 ] >> 0xC)) + p_tmp1[ 2 ]; if( i == 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_tmp1[ 2 ]; if( i == 3 ) { p_tmp2[ 1 ] = ((~p_tmp1[ 3 ] | p_tmp1[ 1 ]) ^ p_tmp1[ 2 ]) + p_input[ p_table[ i ][ 3 ] ]; } else { p_tmp2[ 1 ] = (p_tmp1[ 3 ] ^ p_tmp1[ 2 ] ^ p_tmp1[ 1 ]) + p_input[ p_table[ i ][ 3 ] ]; } p_tmp1[ 0 ] = p_tmp2[ 0 ] + p_tmp2[ 1 ] + p_drms_tab_taos[ x++ ]; } for( i = 0; i < 4; i++ ) { uint8_t p_table[ 4 ][ 4 ] = { { 7, 14, 5, 12 }, { 3, 10, 1, 8 }, { 15, 6, 13, 4 }, { 11, 2, 9, 0 } }; p_tmp2[ 0 ] = ((p_tmp1[ 0 ] >> 0x1A) | (p_tmp1[ 0 ] << 0x6)) + p_tmp1[ 1 ]; p_tmp2[ 1 ] = ((~p_tmp1[ 2 ] | p_tmp2[ 0 ]) ^ p_tmp1[ 1 ]) + p_input[ p_table[ i ][ 0 ] ]; p_tmp2[ 1 ] += p_tmp1[ 3 ] + p_drms_tab_taos[ x++ ]; p_tmp1[ 3 ] = ((p_tmp2[ 1 ] >> 0x16) | (p_tmp2[ 1 ] << 0xA)) + p_tmp2[ 0 ]; p_tmp2[ 1 ] = ((~p_tmp1[ 1 ] | p_tmp1[ 3 ]) ^ p_tmp2[ 0 ]) + p_input[ p_table[ i ][ 1 ] ]; p_tmp2[ 1 ] += p_tmp1[ 2 ] + p_drms_tab_taos[ x++ ]; p_tmp1[ 2 ] = ((p_tmp2[ 1 ] >> 0x11) | (p_tmp2[ 1 ] << 0xF)) + p_tmp1[ 3 ]; p_tmp2[ 1 ] = ((~p_tmp2[ 0 ] | p_tmp1[ 2 ]) ^ p_tmp1[ 3 ]) + p_input[ p_table[ i ][ 2 ] ]; p_tmp2[ 1 ] += p_tmp1[ 1 ] + p_drms_tab_taos[ x++ ]; p_tmp1[ 1 ] = ((p_tmp2[ 1 ] << 0x15) | (p_tmp2[ 1 ] >> 0xB)) + p_tmp1[ 2 ]; if( i < 3 ) { p_tmp2[ 1 ] = ((~p_tmp1[ 3 ] | p_tmp1[ 1 ]) ^ p_tmp1[ 2 ]) + p_input[ p_table[ i ][ 3 ] ]; p_tmp1[ 0 ] = p_tmp2[ 0 ] + p_tmp2[ 1 ] + p_drms_tab_taos[ x++ ]; } } p_buffer[ 0 ] += p_tmp2[ 0 ]; p_buffer[ 1 ] += p_tmp1[ 1 ]; p_buffer[ 2 ] += p_tmp1[ 2 ]; p_buffer[ 3 ] += p_tmp1[ 3 ];}static void taos_add1( uint32_t *p_buffer, uint8_t *p_in, uint32_t i_len ){ uint32_t i; uint32_t x, y; uint32_t p_tmp[ 16 ]; uint32_t i_offset = 0; x = p_buffer[ 6 ] & 63; y = 64 - x; p_buffer[ 6 ] += i_len; if( i_len < y ) { memcpy( &((uint8_t *)p_buffer)[ 48 + x ], p_in, i_len ); } else { if( x ) { memcpy( &((uint8_t *)p_buffer)[ 48 + x ], p_in, y ); taos( &p_buffer[ 8 ], &p_buffer[ 12 ] ); i_offset = y; i_len -= y; } if( i_len >= 64 ) { for( i = 0; i < i_len / 64; i++ ) { memcpy( p_tmp, &p_in[ i_offset ], sizeof(p_tmp) ); taos( &p_buffer[ 8 ], p_tmp ); i_offset += 64; i_len -= 64; } } if( i_len ) { memcpy( &p_buffer[ 12 ], &p_in[ i_offset ], i_len ); } }}static void taos_end1( uint32_t *p_buffer, uint32_t *p_out ){ uint32_t x, y; x = p_buffer[ 6 ] & 63; y = 63 - x; ((uint8_t *)p_buffer)[ 48 + x++ ] = 128; if( y < 8 ) { memset( &((uint8_t *)p_buffer)[ 48 + x ], 0, y ); taos( &p_buffer[ 8 ], &p_buffer[ 12 ] ); y = 64; x = 0; } memset( &((uint8_t *)p_buffer)[ 48 + x ], 0, y ); p_buffer[ 26 ] = p_buffer[ 6 ] * 8; p_buffer[ 27 ] = p_buffer[ 6 ] >> 29; taos( &p_buffer[ 8 ], &p_buffer[ 12 ] ); memcpy( p_out, &p_buffer[ 8 ], sizeof(*p_out) * 4 );}static void taos_add2( uint32_t *p_buffer, uint8_t *p_in, uint32_t i_len ){ uint32_t i, x; uint32_t p_tmp[ 16 ]; x = (p_buffer[ 0 ] / 8) & 63; i = p_buffer[ 0 ] + i_len * 8; if( i < p_buffer[ 0 ] ) { p_buffer[ 1 ] += 1; } p_buffer[ 0 ] = i; p_buffer[ 1 ] += i_len >> 29; for( i = 0; i < i_len; i++ ) { ((uint8_t *)p_buffer)[ 24 + x++ ] = p_in[ i ]; if( x != 64 ) continue; memcpy( p_tmp, &p_buffer[ 6 ], sizeof(p_tmp) ); taos( &p_buffer[ 2 ], p_tmp ); }}static void taos_add2e( uint32_t *p_buffer, uint32_t *p_in, uint32_t i_len ){ uint32_t i, x, y; uint32_t p_tmp[ 32 ]; if( i_len ) { for( x = i_len; x; x -= y ) { y = x > 32 ? 32 : x; for( i = 0; i < y; i++ ) { p_tmp[ i ] = U32_AT(&p_in[ i ]); } } } taos_add2( p_buffer, (uint8_t *)p_tmp, i_len * sizeof(p_tmp[ 0 ]) );}static void taos_end2( uint32_t *p_buffer ){ uint32_t x; uint32_t p_tmp[ 16 ]; p_tmp[ 14 ] = p_buffer[ 0 ]; p_tmp[ 15 ] = p_buffer[ 1 ]; x = (p_buffer[ 0 ] / 8) & 63; taos_add2( p_buffer, p_drms_tab_tend, 56 - x ); memcpy( p_tmp, &p_buffer[ 6 ], 56 ); taos( &p_buffer[ 2 ], p_tmp ); memcpy( &p_buffer[ 22 ], &p_buffer[ 2 ], sizeof(*p_buffer) * 4 );}static void taos_add3( uint32_t *p_buffer, uint8_t *p_key, uint32_t i_len ){ uint32_t x, y; uint32_t i = 0; x = (p_buffer[ 4 ] / 8) & 63; p_buffer[ 4 ] += i_len * 8; if( p_buffer[ 4 ] < i_len * 8 ) p_buffer[ 5 ] += 1; p_buffer[ 5 ] += i_len >> 29; y = 64 - x; if( i_len >= y ) { memcpy( &((uint8_t *)p_buffer)[ 24 + x ], p_key, y ); taos( p_buffer, &p_buffer[ 6 ] ); i = y; y += 63; if( y < i_len ) { for( ; y < i_len; y += 64, i += 64 ) { taos( p_buffer, (uint32_t *)&p_key[y - 63] ); } } else { x = 0; } } memcpy( &((uint8_t *)p_buffer)[ 24 + x ], &p_key[ i ], i_len - i );}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -