1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
5 * $Id: drms.c,v 1.3 2004/01/11 15:52:18 menno Exp $
7 * Author: Jon Lech Johansen <jon-vl@nanocrew.net>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 #include <stdlib.h> /* malloc(), free() */
37 #include "drmstables.h"
39 static __inline uint32_t U32_AT( void * _p )
41 uint8_t * p = (uint8_t *)_p;
42 return ( ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16)
43 | ((uint32_t)p[2] << 8) | p[3] );
46 #define TAOS_INIT( tmp, i ) \
47 memset( tmp, 0, sizeof(tmp) ); \
48 tmp[ i + 0 ] = 0x67452301; \
49 tmp[ i + 1 ] = 0xEFCDAB89; \
50 tmp[ i + 2 ] = 0x98BADCFE; \
51 tmp[ i + 3 ] = 0x10325476;
53 #define ROR( x, n ) (((x) << (32-(n))) | ((x) >> (n)))
55 static void init_ctx( uint32_t *p_ctx, uint32_t *p_input )
60 p_ctx[ 0 ] = sizeof(*p_input);
62 memset( &p_ctx[ 1 + 4 ], 0, sizeof(*p_input) * 4 );
63 memcpy( &p_ctx[ 1 + 0 ], p_input, sizeof(*p_input) * 4 );
65 p_tmp[ 0 ] = p_ctx[ 1 + 3 ];
67 for( i = 0; i < sizeof(p_drms_tab1)/sizeof(p_drms_tab1[ 0 ]); i++ )
69 p_tmp[ 0 ] = ROR( p_tmp[ 0 ], 8 );
71 p_tmp[ 5 ] = p_drms_tab2[ (p_tmp[ 0 ] >> 24) & 0xFF ]
72 ^ ROR( p_drms_tab2[ (p_tmp[ 0 ] >> 16) & 0xFF ], 8 )
73 ^ ROR( p_drms_tab2[ (p_tmp[ 0 ] >> 8) & 0xFF ], 16 )
74 ^ ROR( p_drms_tab2[ p_tmp[ 0 ] & 0xFF ], 24 )
76 ^ p_ctx[ 1 + ((i + 1) * 4) - 4 ];
78 p_ctx[ 1 + ((i + 1) * 4) + 0 ] = p_tmp[ 5 ];
79 p_tmp[ 5 ] ^= p_ctx[ 1 + ((i + 1) * 4) - 3 ];
80 p_ctx[ 1 + ((i + 1) * 4) + 1 ] = p_tmp[ 5 ];
81 p_tmp[ 5 ] ^= p_ctx[ 1 + ((i + 1) * 4) - 2 ];
82 p_ctx[ 1 + ((i + 1) * 4) + 2 ] = p_tmp[ 5 ];
83 p_tmp[ 5 ] ^= p_ctx[ 1 + ((i + 1) * 4) - 1 ];
84 p_ctx[ 1 + ((i + 1) * 4) + 3 ] = p_tmp[ 5 ];
86 p_tmp[ 0 ] = p_tmp[ 5 ];
89 memcpy( &p_ctx[ 1 + 64 ], &p_ctx[ 1 ], sizeof(*p_ctx) * 4 );
91 for( i = 4; i < sizeof(p_drms_tab1); i++ )
93 p_tmp[ 2 ] = p_ctx[ 1 + 4 + (i - 4) ];
95 p_tmp[ 0 ] = (((p_tmp[ 2 ] >> 7) & 0x01010101) * 27)
96 ^ ((p_tmp[ 2 ] & 0xFF7F7F7F) << 1);
97 p_tmp[ 1 ] = (((p_tmp[ 0 ] >> 7) & 0x01010101) * 27)
98 ^ ((p_tmp[ 0 ] & 0xFF7F7F7F) << 1);
99 p_tmp[ 4 ] = (((p_tmp[ 1 ] >> 7) & 0x01010101) * 27)
100 ^ ((p_tmp[ 1 ] & 0xFF7F7F7F) << 1);
102 p_tmp[ 2 ] ^= p_tmp[ 4 ];
104 p_tmp[ 3 ] = ROR( p_tmp[ 1 ] ^ p_tmp[ 2 ], 16 )
105 ^ ROR( p_tmp[ 0 ] ^ p_tmp[ 2 ], 8 )
106 ^ ROR( p_tmp[ 2 ], 24 );
108 p_ctx[ 1 + 4 + 64 + (i - 4) ] = p_tmp[ 3 ] ^ p_tmp[ 4 ]
109 ^ p_tmp[ 1 ] ^ p_tmp[ 0 ];
113 static void ctx_xor( uint32_t *p_ctx, uint32_t *p_in, uint32_t *p_out,
114 uint32_t p_table1[ 256 ], uint32_t p_table2[ 256 ] )
117 uint32_t p_tmp1[ 4 ];
118 uint32_t p_tmp2[ 4 ];
122 p_tmp1[ 0 ] = p_ctx[ 1 + i + 24 ] ^ p_in[ 0 ];
123 p_tmp1[ 1 ] = p_ctx[ 1 + i + 25 ] ^ p_in[ 1 ];
124 p_tmp1[ 2 ] = p_ctx[ 1 + i + 26 ] ^ p_in[ 2 ];
125 p_tmp1[ 3 ] = p_ctx[ 1 + i + 27 ] ^ p_in[ 3 ];
129 #define XOR_ROR( p_table, p_tmp, i_ctx ) \
130 p_table[ (p_tmp[ y > 2 ? y - 3 : y + 1 ] >> 24) & 0xFF ] \
131 ^ ROR( p_table[ (p_tmp[ y > 1 ? y - 2 : y + 2 ] >> 16) & 0xFF ], 8 ) \
132 ^ ROR( p_table[ (p_tmp[ y > 0 ? y - 1 : y + 3 ] >> 8) & 0xFF ], 16 ) \
133 ^ ROR( p_table[ p_tmp[ y ] & 0xFF ], 24 ) \
136 for( x = 0; x < 1; x++ )
138 memcpy( p_tmp2, p_tmp1, sizeof(p_tmp1) );
140 for( y = 0; y < 4; y++ )
142 p_tmp1[ y ] = XOR_ROR( p_table1, p_tmp2, 1 + i - x + y );
148 memcpy( p_tmp2, p_tmp1, sizeof(p_tmp1) );
150 for( y = 0; y < 4; y++ )
152 p_tmp1[ y ] = XOR_ROR( p_table1, p_tmp2,
153 1 + i - x - ((x * 3) - y) );
157 for( y = 0; y < 4; y++ )
159 p_out[ y ] = XOR_ROR( p_table2, p_tmp1,
160 1 + i - x - ((x * 3) - y) );
166 static void taos( uint32_t *p_buffer, uint32_t *p_input )
170 uint32_t p_tmp1[ 4 ];
171 uint32_t p_tmp2[ 4 ];
173 memcpy( p_tmp1, p_buffer, sizeof(p_tmp1) );
175 p_tmp2[ 0 ] = ((~p_tmp1[ 1 ] & p_tmp1[ 3 ])
176 | (p_tmp1[ 2 ] & p_tmp1[ 1 ])) + p_input[ x ];
177 p_tmp1[ 0 ] = p_tmp2[ 0 ] + p_tmp1[ 0 ] + p_drms_tab_taos[ x++ ];
179 for( i = 0; i < 4; i++ )
181 p_tmp2[ 0 ] = ((p_tmp1[ 0 ] >> 0x19)
182 | (p_tmp1[ 0 ] << 0x7)) + p_tmp1[ 1 ];
183 p_tmp2[ 1 ] = ((~p_tmp2[ 0 ] & p_tmp1[ 2 ])
184 | (p_tmp1[ 1 ] & p_tmp2[ 0 ])) + p_input[ x ];
185 p_tmp2[ 1 ] += p_tmp1[ 3 ] + p_drms_tab_taos[ x++ ];
187 p_tmp1[ 3 ] = ((p_tmp2[ 1 ] >> 0x14)
188 | (p_tmp2[ 1 ] << 0xC)) + p_tmp2[ 0 ];
189 p_tmp2[ 1 ] = ((~p_tmp1[ 3 ] & p_tmp1[ 1 ])
190 | (p_tmp1[ 3 ] & p_tmp2[ 0 ])) + p_input[ x ];
191 p_tmp2[ 1 ] += p_tmp1[ 2 ] + p_drms_tab_taos[ x++ ];
193 p_tmp1[ 2 ] = ((p_tmp2[ 1 ] >> 0xF)
194 | (p_tmp2[ 1 ] << 0x11)) + p_tmp1[ 3 ];
195 p_tmp2[ 1 ] = ((~p_tmp1[ 2 ] & p_tmp2[ 0 ])
196 | (p_tmp1[ 3 ] & p_tmp1[ 2 ])) + p_input[ x ];
197 p_tmp2[ 2 ] = p_tmp2[ 1 ] + p_tmp1[ 1 ] + p_drms_tab_taos[ x++ ];
199 p_tmp1[ 1 ] = ((p_tmp2[ 2 ] << 0x16)
200 | (p_tmp2[ 2 ] >> 0xA)) + p_tmp1[ 2 ];
203 p_tmp2[ 1 ] = ((~p_tmp1[ 3 ] & p_tmp1[ 2 ])
204 | (p_tmp1[ 3 ] & p_tmp1[ 1 ])) + p_input[ 1 ];
208 p_tmp2[ 1 ] = ((~p_tmp1[ 1 ] & p_tmp1[ 3 ])
209 | (p_tmp1[ 2 ] & p_tmp1[ 1 ])) + p_input[ x ];
211 p_tmp1[ 0 ] = p_tmp2[ 0 ] + p_tmp2[ 1 ] + p_drms_tab_taos[ x++ ];
214 for( i = 0; i < 4; i++ )
216 uint8_t p_table[ 4 ][ 4 ] =
224 p_tmp2[ 0 ] = ((p_tmp1[ 0 ] >> 0x1B)
225 | (p_tmp1[ 0 ] << 0x5)) + p_tmp1[ 1 ];
226 p_tmp2[ 1 ] = ((~p_tmp1[ 2 ] & p_tmp1[ 1 ])
227 | (p_tmp1[ 2 ] & p_tmp2[ 0 ]))
228 + p_input[ p_table[ i ][ 0 ] ];
229 p_tmp2[ 1 ] += p_tmp1[ 3 ] + p_drms_tab_taos[ x++ ];
231 p_tmp1[ 3 ] = ((p_tmp2[ 1 ] >> 0x17)
232 | (p_tmp2[ 1 ] << 0x9)) + p_tmp2[ 0 ];
233 p_tmp2[ 1 ] = ((~p_tmp1[ 1 ] & p_tmp2[ 0 ])
234 | (p_tmp1[ 3 ] & p_tmp1[ 1 ]))
235 + p_input[ p_table[ i ][ 1 ] ];
236 p_tmp2[ 1 ] += p_tmp1[ 2 ] + p_drms_tab_taos[ x++ ];
238 p_tmp1[ 2 ] = ((p_tmp2[ 1 ] >> 0x12)
239 | (p_tmp2[ 1 ] << 0xE)) + p_tmp1[ 3 ];
240 p_tmp2[ 1 ] = ((~p_tmp2[ 0 ] & p_tmp1[ 3 ])
241 | (p_tmp1[ 2 ] & p_tmp2[ 0 ]))
242 + p_input[ p_table[ i ][ 2 ] ];
243 p_tmp2[ 1 ] += p_tmp1[ 1 ] + p_drms_tab_taos[ x++ ];
245 p_tmp1[ 1 ] = ((p_tmp2[ 1 ] << 0x14)
246 | (p_tmp2[ 1 ] >> 0xC)) + p_tmp1[ 2 ];
249 p_tmp2[ 1 ] = (p_tmp1[ 3 ] ^ p_tmp1[ 2 ] ^ p_tmp1[ 1 ])
250 + p_input[ p_table[ i ][ 3 ] ];
254 p_tmp2[ 1 ] = ((~p_tmp1[ 3 ] & p_tmp1[ 2 ])
255 | (p_tmp1[ 3 ] & p_tmp1[ 1 ]))
256 + p_input[ p_table[ i ][ 3 ] ];
258 p_tmp1[ 0 ] = p_tmp2[ 0 ] + p_tmp2[ 1 ] + p_drms_tab_taos[ x++ ];
261 for( i = 0; i < 4; i++ )
263 uint8_t p_table[ 4 ][ 4 ] =
271 p_tmp2[ 0 ] = ((p_tmp1[ 0 ] >> 0x1C)
272 | (p_tmp1[ 0 ] << 0x4)) + p_tmp1[ 1 ];
273 p_tmp2[ 1 ] = (p_tmp1[ 2 ] ^ p_tmp1[ 1 ] ^ p_tmp2[ 0 ])
274 + p_input[ p_table[ i ][ 0 ] ];
275 p_tmp2[ 1 ] += p_tmp1[ 3 ] + p_drms_tab_taos[ x++ ];
277 p_tmp1[ 3 ] = ((p_tmp2[ 1 ] >> 0x15)
278 | (p_tmp2[ 1 ] << 0xB)) + p_tmp2[ 0 ];
279 p_tmp2[ 1 ] = (p_tmp1[ 3 ] ^ p_tmp1[ 1 ] ^ p_tmp2[ 0 ])
280 + p_input[ p_table[ i ][ 1 ] ];
281 p_tmp2[ 1 ] += p_tmp1[ 2 ] + p_drms_tab_taos[ x++ ];
283 p_tmp1[ 2 ] = ((p_tmp2[ 1 ] >> 0x10)
284 | (p_tmp2[ 1 ] << 0x10)) + p_tmp1[ 3 ];
285 p_tmp2[ 1 ] = (p_tmp1[ 3 ] ^ p_tmp1[ 2 ] ^ p_tmp2[ 0 ])
286 + p_input[ p_table[ i ][ 2 ] ];
287 p_tmp2[ 1 ] += p_tmp1[ 1 ] + p_drms_tab_taos[ x++ ];
289 p_tmp1[ 1 ] = ((p_tmp2[ 1 ] << 0x17)
290 | (p_tmp2[ 1 ] >> 0x9)) + p_tmp1[ 2 ];
293 p_tmp2[ 1 ] = ((~p_tmp1[ 3 ] | p_tmp1[ 1 ]) ^ p_tmp1[ 2 ])
294 + p_input[ p_table[ i ][ 3 ] ];
298 p_tmp2[ 1 ] = (p_tmp1[ 3 ] ^ p_tmp1[ 2 ] ^ p_tmp1[ 1 ])
299 + p_input[ p_table[ i ][ 3 ] ];
301 p_tmp1[ 0 ] = p_tmp2[ 0 ] + p_tmp2[ 1 ] + p_drms_tab_taos[ x++ ];
304 for( i = 0; i < 4; i++ )
306 uint8_t p_table[ 4 ][ 4 ] =
314 p_tmp2[ 0 ] = ((p_tmp1[ 0 ] >> 0x1A)
315 | (p_tmp1[ 0 ] << 0x6)) + p_tmp1[ 1 ];
316 p_tmp2[ 1 ] = ((~p_tmp1[ 2 ] | p_tmp2[ 0 ]) ^ p_tmp1[ 1 ])
317 + p_input[ p_table[ i ][ 0 ] ];
318 p_tmp2[ 1 ] += p_tmp1[ 3 ] + p_drms_tab_taos[ x++ ];
320 p_tmp1[ 3 ] = ((p_tmp2[ 1 ] >> 0x16)
321 | (p_tmp2[ 1 ] << 0xA)) + p_tmp2[ 0 ];
322 p_tmp2[ 1 ] = ((~p_tmp1[ 1 ] | p_tmp1[ 3 ]) ^ p_tmp2[ 0 ])
323 + p_input[ p_table[ i ][ 1 ] ];
324 p_tmp2[ 1 ] += p_tmp1[ 2 ] + p_drms_tab_taos[ x++ ];
326 p_tmp1[ 2 ] = ((p_tmp2[ 1 ] >> 0x11)
327 | (p_tmp2[ 1 ] << 0xF)) + p_tmp1[ 3 ];
328 p_tmp2[ 1 ] = ((~p_tmp2[ 0 ] | p_tmp1[ 2 ]) ^ p_tmp1[ 3 ])
329 + p_input[ p_table[ i ][ 2 ] ];
330 p_tmp2[ 1 ] += p_tmp1[ 1 ] + p_drms_tab_taos[ x++ ];
332 p_tmp1[ 1 ] = ((p_tmp2[ 1 ] << 0x15)
333 | (p_tmp2[ 1 ] >> 0xB)) + p_tmp1[ 2 ];
337 p_tmp2[ 1 ] = ((~p_tmp1[ 3 ] | p_tmp1[ 1 ]) ^ p_tmp1[ 2 ])
338 + p_input[ p_table[ i ][ 3 ] ];
339 p_tmp1[ 0 ] = p_tmp2[ 0 ] + p_tmp2[ 1 ] + p_drms_tab_taos[ x++ ];
343 p_buffer[ 0 ] += p_tmp2[ 0 ];
344 p_buffer[ 1 ] += p_tmp1[ 1 ];
345 p_buffer[ 2 ] += p_tmp1[ 2 ];
346 p_buffer[ 3 ] += p_tmp1[ 3 ];
349 static void taos_add1( uint32_t *p_buffer,
350 uint8_t *p_in, uint32_t i_len )
354 uint32_t p_tmp[ 16 ];
355 uint32_t i_offset = 0;
357 x = p_buffer[ 6 ] & 63;
360 p_buffer[ 6 ] += i_len;
364 memcpy( &((uint8_t *)p_buffer)[ 48 + x ], p_in, i_len );
370 memcpy( &((uint8_t *)p_buffer)[ 48 + x ], p_in, y );
371 taos( &p_buffer[ 8 ], &p_buffer[ 12 ] );
378 for( i = 0; i < i_len / 64; i++ )
380 memcpy( p_tmp, &p_in[ i_offset ], sizeof(p_tmp) );
381 taos( &p_buffer[ 8 ], p_tmp );
389 memcpy( &p_buffer[ 12 ], &p_in[ i_offset ], i_len );
394 static void taos_end1( uint32_t *p_buffer, uint32_t *p_out )
398 x = p_buffer[ 6 ] & 63;
401 ((uint8_t *)p_buffer)[ 48 + x++ ] = 128;
405 memset( &((uint8_t *)p_buffer)[ 48 + x ], 0, y );
406 taos( &p_buffer[ 8 ], &p_buffer[ 12 ] );
411 memset( &((uint8_t *)p_buffer)[ 48 + x ], 0, y );
413 p_buffer[ 26 ] = p_buffer[ 6 ] * 8;
414 p_buffer[ 27 ] = p_buffer[ 6 ] >> 29;
415 taos( &p_buffer[ 8 ], &p_buffer[ 12 ] );
417 memcpy( p_out, &p_buffer[ 8 ], sizeof(*p_out) * 4 );
420 static void taos_add2( uint32_t *p_buffer, uint8_t *p_in, uint32_t i_len )
423 uint32_t p_tmp[ 16 ];
425 x = (p_buffer[ 0 ] / 8) & 63;
426 i = p_buffer[ 0 ] + i_len * 8;
428 if( i < p_buffer[ 0 ] )
434 p_buffer[ 1 ] += i_len >> 29;
436 for( i = 0; i < i_len; i++ )
438 ((uint8_t *)p_buffer)[ 24 + x++ ] = p_in[ i ];
443 memcpy( p_tmp, &p_buffer[ 6 ], sizeof(p_tmp) );
444 taos( &p_buffer[ 2 ], p_tmp );
448 static void taos_add2e( uint32_t *p_buffer, uint32_t *p_in, uint32_t i_len )
451 uint32_t p_tmp[ 32 ];
455 for( x = i_len; x; x -= y )
459 for( i = 0; i < y; i++ )
461 p_tmp[ i ] = U32_AT(&p_in[ i ]);
466 taos_add2( p_buffer, (uint8_t *)p_tmp, i_len * sizeof(p_tmp[ 0 ]) );
469 static void taos_end2( uint32_t *p_buffer )
472 uint32_t p_tmp[ 16 ];
474 p_tmp[ 14 ] = p_buffer[ 0 ];
475 p_tmp[ 15 ] = p_buffer[ 1 ];
477 x = (p_buffer[ 0 ] / 8) & 63;
479 taos_add2( p_buffer, p_drms_tab_tend, 56 - x );
480 memcpy( p_tmp, &p_buffer[ 6 ], 56 );
481 taos( &p_buffer[ 2 ], p_tmp );
482 memcpy( &p_buffer[ 22 ], &p_buffer[ 2 ], sizeof(*p_buffer) * 4 );
485 static void taos_add3( uint32_t *p_buffer, uint8_t *p_key, uint32_t i_len )
490 x = (p_buffer[ 4 ] / 8) & 63;
491 p_buffer[ 4 ] += i_len * 8;
493 if( p_buffer[ 4 ] < i_len * 8 )
496 p_buffer[ 5 ] += i_len >> 29;
502 memcpy( &((uint8_t *)p_buffer)[ 24 + x ], p_key, y );
503 taos( p_buffer, &p_buffer[ 6 ] );
510 for( ; y < i_len; y += 64, i += 64 )
512 taos( p_buffer, (uint32_t *)&p_key[y - 63] );
521 memcpy( &((uint8_t *)p_buffer)[ 24 + x ], &p_key[ i ], i_len - i );
524 static int taos_osi( uint32_t *p_buffer )
535 static LPCTSTR p_reg_keys[ 3 ][ 2 ] =
538 _T("HARDWARE\\DESCRIPTION\\System"),
539 _T("SystemBiosVersion")
543 _T("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"),
544 _T("ProcessorNameString")
548 _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion"),
553 taos_add1( p_buffer, "cache-control", 13 );
554 taos_add1( p_buffer, "Ethernet", 8 );
556 GetVolumeInformation( _T("C:\\"), NULL, 0, &i_serial,
557 NULL, NULL, NULL, 0 );
558 taos_add1( p_buffer, (uint8_t *)&i_serial, 4 );
560 for( i = 0; i < sizeof(p_reg_keys)/sizeof(p_reg_keys[ 0 ]); i++ )
562 if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, p_reg_keys[ i ][ 0 ],
563 0, KEY_READ, &i_key ) == ERROR_SUCCESS )
565 if( RegQueryValueEx( i_key, p_reg_keys[ i ][ 1 ],
567 &i_size ) == ERROR_SUCCESS )
569 p_reg_buf = malloc( i_size );
571 if( p_reg_buf != NULL )
573 if( RegQueryValueEx( i_key, p_reg_keys[ i ][ 1 ],
574 NULL, NULL, p_reg_buf,
575 &i_size ) == ERROR_SUCCESS )
577 taos_add1( p_buffer, (uint8_t *)p_reg_buf,
585 RegCloseKey( i_key );
596 static int get_sci_data( uint32_t p_sci[ 11 ][ 4 ] )
602 DWORD i_size, i_read;
603 TCHAR p_path[ MAX_PATH ];
604 TCHAR *p_filename = _T("\\Apple Computer\\iTunes\\SC Info\\SC Info.sidb");
606 typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
609 HINSTANCE shfolder_dll = NULL;
610 SHGETFOLDERPATH dSHGetFolderPath = NULL;
612 if( ( shfolder_dll = LoadLibrary( _T("SHFolder.dll") ) ) != NULL )
615 (SHGETFOLDERPATH)GetProcAddress( shfolder_dll,
617 _T("SHGetFolderPathW") );
619 _T("SHGetFolderPathA") );
623 if( dSHGetFolderPath != NULL &&
624 SUCCEEDED( dSHGetFolderPath( NULL, /*CSIDL_COMMON_APPDATA*/ 0x0023,
625 NULL, 0, p_path ) ) )
627 _tcsncat( p_path, p_filename, min( _tcslen( p_filename ),
628 (MAX_PATH-1) - _tcslen( p_path ) ) );
630 i_file = CreateFile( p_path, GENERIC_READ, 0, NULL,
631 OPEN_EXISTING, 0, NULL );
632 if( i_file != INVALID_HANDLE_VALUE )
634 i_read = sizeof(p_sci[ 0 ]) * 11;
635 i_size = GetFileSize( i_file, NULL );
636 if( i_size != INVALID_FILE_SIZE && i_size >= i_read )
638 i_size = SetFilePointer( i_file, 4, NULL, FILE_BEGIN );
639 if( i_size != /*INVALID_SET_FILE_POINTER*/ ((DWORD)-1))
641 if( ReadFile( i_file, p_sci, i_read, &i_size, NULL ) &&
649 CloseHandle( i_file );
657 static void acei_taxs( uint32_t *p_acei, uint32_t i_val )
661 i = (i_val / 16) & 15;
662 x = (~(i_val & 15)) & 15;
664 if( (i_val & 768) == 768 )
669 p_acei[ 25 + i ] = p_acei[ 25 + ((16 - x) & 15) ]
670 + p_acei[ 25 + (15 - x) ];
672 else if( (i_val & 512) == 512 )
674 p_acei[ 25 + i ] ^= p_drms_tab_xor[ 15 - i ][ x ];
676 else if( (i_val & 256) == 256 )
678 p_acei[ 25 + i ] -= p_drms_tab_sub[ 15 - i ][ x ];
682 p_acei[ 25 + i ] += p_drms_tab_add[ 15 - i ][ x ];
686 static void acei( uint32_t *p_acei, uint8_t *p_buffer, uint32_t i_len )
689 uint32_t p_tmp[ 26 ];
691 for( i = 5; i < 25; i++ )
695 acei_taxs( p_acei, p_acei[ i ] );
699 TAOS_INIT( p_tmp, 2 );
700 taos_add2e( p_tmp, &p_acei[ 25 ], sizeof(*p_acei) * 4 );
703 x = i_len < 16 ? i_len : 16;
707 for( i = 0; i < x; i++ )
709 p_buffer[ i ] ^= ((uint8_t *)&p_tmp)[ 88 + i ];
714 static uint32_t ttov_calc( uint32_t *p_acei )
717 uint32_t p_tmp[ 26 ];
719 TAOS_INIT( p_tmp, 2 );
720 taos_add2e( p_tmp, &p_acei[ 0 ], 4 );
721 taos_add2e( p_tmp, &p_acei[ 4 ], 1 );
726 i_val = ((int32_t)U32_AT(&p_tmp[ 22 ])) % 1024;
728 return( i_val < 0 ? i_val * -1 : i_val );
731 static void acei_init( uint32_t *p_acei, uint32_t *p_sys_key )
735 for( i = 0; i < 4; i++ )
737 p_acei[ i ] = U32_AT(&p_sys_key[ i ]);
740 p_acei[ 4 ] = 0x5476212A;
742 for( i = 5; i < 25; i++ )
744 p_acei[ i ] = ttov_calc( p_acei );
747 p_acei[ 25 + 0 ] = p_acei[ 0 ];
748 p_acei[ 25 + 1 ] = 0x68723876;
749 p_acei[ 25 + 2 ] = 0x41617376;
750 p_acei[ 25 + 3 ] = 0x4D4B4F76;
752 p_acei[ 25 + 4 ] = p_acei[ 1 ];
753 p_acei[ 25 + 5 ] = 0x48556646;
754 p_acei[ 25 + 6 ] = 0x38393725;
755 p_acei[ 25 + 7 ] = 0x2E3B5B3D;
757 p_acei[ 25 + 8 ] = p_acei[ 2 ];
758 p_acei[ 25 + 9 ] = 0x37363866;
759 p_acei[ 25 + 10 ] = 0x30383637;
760 p_acei[ 25 + 11 ] = 0x34333661;
762 p_acei[ 25 + 12 ] = p_acei[ 3 ];
763 p_acei[ 25 + 13 ] = 0x37386162;
764 p_acei[ 25 + 14 ] = 0x494F6E66;
765 p_acei[ 25 + 15 ] = 0x2A282966;
768 static __inline void block_xor( uint32_t *p_in, uint32_t *p_key,
773 for( i = 0; i < 4; i++ )
775 p_out[ i ] = p_key[ i ] ^ p_in[ i ];
779 int drms_get_sys_key( uint32_t *p_sys_key )
781 uint32_t p_tmp[ 128 ];
782 uint32_t p_tmp_key[ 4 ];
784 TAOS_INIT( p_tmp, 8 );
785 if( taos_osi( p_tmp ) )
789 taos_end1( p_tmp, p_tmp_key );
791 TAOS_INIT( p_tmp, 2 );
792 taos_add2( p_tmp, "YuaFlafu", 8 );
793 taos_add2( p_tmp, (uint8_t *)p_tmp_key, 6 );
794 taos_add2( p_tmp, (uint8_t *)p_tmp_key, 6 );
795 taos_add2( p_tmp, (uint8_t *)p_tmp_key, 6 );
796 taos_add2( p_tmp, "zPif98ga", 8 );
799 memcpy( p_sys_key, &p_tmp[ 2 ], sizeof(*p_sys_key) * 4 );
804 int drms_get_user_key( uint32_t *p_sys_key, uint32_t *p_user_key )
809 uint32_t p_acei[ 41 ];
810 uint32_t p_ctx[ 128 ];
811 uint32_t p_sci[ 2 ][ 11 ][ 4 ];
813 uint32_t p_sci_key[ 4 ] =
815 0x6E66556D, 0x6E676F70, 0x67666461, 0x33373866
818 if( p_sys_key == NULL )
820 if( drms_get_sys_key( p_tmp ) )
828 if( get_sci_data( p_sci[ 0 ] ) )
833 init_ctx( p_ctx, p_sys_key );
835 for( i = 0, p_cur_key = p_sci_key;
836 i < sizeof(p_sci[ 0 ])/sizeof(p_sci[ 0 ][ 0 ]); i++ )
838 ctx_xor( p_ctx, &p_sci[ 0 ][ i ][ 0 ], &p_sci[ 1 ][ i ][ 0 ],
839 p_drms_tab3, p_drms_tab4 );
840 block_xor( &p_sci[ 1 ][ i ][ 0 ], p_cur_key, &p_sci[ 1 ][ i ][ 0 ] );
842 p_cur_key = &p_sci[ 0 ][ i ][ 0 ];
845 acei_init( p_acei, p_sys_key );
847 for( i = 0; i < sizeof(p_sci[ 1 ])/sizeof(p_sci[ 1 ][ 0 ]); i++ )
849 acei( p_acei, (uint8_t *)&p_sci[ 1 ][ i ][ 0 ],
850 sizeof(p_sci[ 1 ][ i ]) );
853 memcpy( p_user_key, &p_sci[ 1 ][ 10 ][ 0 ], sizeof(p_sci[ 1 ][ i ]) );
869 uint32_t p_ctx[ 128 ];
872 #define P_DRMS ((struct drms_s *)p_drms)
876 struct drms_s *p_drms;
878 p_drms = malloc( sizeof(struct drms_s) );
882 memset( p_drms, 0, sizeof(struct drms_s) );
884 p_drms->i_tmp_len = 1024;
885 p_drms->p_tmp = malloc( p_drms->i_tmp_len );
886 if( p_drms->p_tmp == NULL )
888 free( (void *)p_drms );
893 return( (void *)p_drms );
896 void drms_free( void *p_drms )
898 if( P_DRMS->p_name != NULL )
900 free( (void *)P_DRMS->p_name );
903 if( P_DRMS->p_iviv != NULL )
905 free( (void *)P_DRMS->p_iviv );
908 if( P_DRMS->p_tmp != NULL )
910 free( (void *)P_DRMS->p_tmp );
916 void drms_decrypt( void *p_drms, uint32_t *p_buffer, uint32_t i_len )
919 uint32_t *p_cur_key = P_DRMS->p_key;
921 x = (i_len / sizeof(P_DRMS->p_key)) * sizeof(P_DRMS->p_key);
923 if( P_DRMS->i_tmp_len < x )
925 free( (void *)P_DRMS->p_tmp );
927 P_DRMS->i_tmp_len = x;
928 P_DRMS->p_tmp = malloc( P_DRMS->i_tmp_len );
931 if( P_DRMS->p_tmp != NULL )
933 memcpy( P_DRMS->p_tmp, p_buffer, x );
935 for( i = 0, x /= sizeof(P_DRMS->p_key); i < x; i++ )
937 y = i * sizeof(*p_buffer);
939 ctx_xor( P_DRMS->p_ctx, P_DRMS->p_tmp + y, p_buffer + y,
940 p_drms_tab3, p_drms_tab4 );
941 block_xor( p_buffer + y, p_cur_key, p_buffer + y );
943 p_cur_key = P_DRMS->p_tmp + y;
948 int drms_init( void *p_drms, uint32_t i_type,
949 uint8_t *p_info, uint32_t i_len )
957 if( i_len != sizeof(P_DRMS->p_key) )
963 init_ctx( P_DRMS->p_ctx, (uint32_t *)p_info );
969 if( i_len != sizeof(P_DRMS->p_key) )
975 P_DRMS->p_iviv = malloc( i_len );
976 if( P_DRMS->p_iviv == NULL )
982 memcpy( P_DRMS->p_iviv, p_info, i_len );
983 P_DRMS->i_iviv_len = i_len;
989 P_DRMS->p_name = malloc( i_len );
990 if( P_DRMS->p_name == NULL )
996 memcpy( P_DRMS->p_name, p_info, i_len );
997 P_DRMS->i_name_len = i_len;
1001 case DRMS_INIT_PRIV:
1004 uint32_t p_priv[ 64 ];
1005 uint32_t p_tmp[ 128 ];
1013 TAOS_INIT( p_tmp, 0 );
1014 taos_add3( p_tmp, P_DRMS->p_name, P_DRMS->i_name_len );
1015 taos_add3( p_tmp, P_DRMS->p_iviv, P_DRMS->i_iviv_len );
1016 memcpy( p_priv, &p_tmp[ 4 ], sizeof(p_priv[ 0 ]) * 2 );
1017 i = (p_tmp[ 4 ] / 8) & 63;
1018 i = i >= 56 ? 120 - i : 56 - i;
1019 taos_add3( p_tmp, p_drms_tab_tend, i );
1020 taos_add3( p_tmp, (uint8_t *)p_priv, sizeof(p_priv[ 0 ]) * 2 );
1022 memcpy( p_priv, p_info, 64 );
1023 memcpy( P_DRMS->p_key, p_tmp, sizeof(P_DRMS->p_key) );
1024 drms_decrypt( p_drms, p_priv, sizeof(p_priv) );
1026 init_ctx( P_DRMS->p_ctx, &p_priv[ 6 ] );
1027 memcpy( P_DRMS->p_key, &p_priv[ 12 ], sizeof(P_DRMS->p_key) );
1029 free( (void *)P_DRMS->p_name );
1030 P_DRMS->p_name = NULL;
1031 free( (void *)P_DRMS->p_iviv );
1032 P_DRMS->p_iviv = NULL;