Mercurial > mplayer.hg
annotate libmpdvdkit2/css.c @ 9051:da9d39c59294
bunkus: Fixed and updated the translations
author | mosu |
---|---|
date | Tue, 21 Jan 2003 10:42:18 +0000 |
parents | 0211de3039eb |
children | f0f0f176d298 |
rev | line source |
---|---|
7027 | 1 /***************************************************************************** |
2 * css.c: Functions for DVD authentication and descrambling | |
3 ***************************************************************************** | |
4 * Copyright (C) 1999-2001 VideoLAN | |
5 * $Id$ | |
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 "config.h" | |
37 | |
38 #include <stdio.h> | |
39 #include <stdlib.h> | |
40 #include <string.h> | |
7032 | 41 #include <sys/types.h> |
42 #include <sys/stat.h> | |
43 #include <fcntl.h> | |
8637
0211de3039eb
update libdvdcss in libmpdvdkit to latest version (1.2.4)
arpi
parents:
8123
diff
changeset
|
44 #include <unistd.h> |
7027 | 45 |
7033 | 46 #include "dvdcss.h" |
7027 | 47 |
48 #include "common.h" | |
49 #include "css.h" | |
50 #include "libdvdcss.h" | |
51 #include "csstables.h" | |
52 #include "ioctl.h" | |
53 #include "device.h" | |
54 | |
55 /***************************************************************************** | |
56 * Local prototypes | |
57 *****************************************************************************/ | |
58 static int GetBusKey ( dvdcss_t ); | |
59 static int GetASF ( dvdcss_t ); | |
60 | |
61 static void CryptKey ( int, int, u8 const *, u8 * ); | |
62 static void DecryptKey ( u8, u8 const *, u8 const *, u8 * ); | |
63 | |
64 static int DecryptDiscKey ( u8 const *, dvd_key_t ); | |
65 static int CrackDiscKey ( dvdcss_t, u8 * ); | |
66 | |
67 static void DecryptTitleKey ( dvd_key_t, dvd_key_t ); | |
68 static int RecoverTitleKey ( int, u8 const *, u8 const *, u8 const *, u8 * ); | |
69 static int CrackTitleKey ( dvdcss_t, int, int, dvd_key_t ); | |
70 | |
71 static int AttackPattern ( u8 const[], int, u8 * ); | |
72 #if 0 | |
73 static int AttackPadding ( u8 const[], int, u8 * ); | |
74 #endif | |
75 | |
76 /***************************************************************************** | |
77 * _dvdcss_test: check if the disc is encrypted or not | |
78 *****************************************************************************/ | |
79 int _dvdcss_test( dvdcss_t dvdcss ) | |
80 { | |
81 int i_ret, i_copyright; | |
82 | |
83 i_ret = ioctl_ReadCopyright( dvdcss->i_fd, 0 /* i_layer */, &i_copyright ); | |
84 | |
8637
0211de3039eb
update libdvdcss in libmpdvdkit to latest version (1.2.4)
arpi
parents:
8123
diff
changeset
|
85 #ifdef WIN32 |
0211de3039eb
update libdvdcss in libmpdvdkit to latest version (1.2.4)
arpi
parents:
8123
diff
changeset
|
86 if( i_ret < 0 ) |
0211de3039eb
update libdvdcss in libmpdvdkit to latest version (1.2.4)
arpi
parents:
8123
diff
changeset
|
87 { |
0211de3039eb
update libdvdcss in libmpdvdkit to latest version (1.2.4)
arpi
parents:
8123
diff
changeset
|
88 /* Maybe we didn't have enough priviledges to read the copyright |
0211de3039eb
update libdvdcss in libmpdvdkit to latest version (1.2.4)
arpi
parents:
8123
diff
changeset
|
89 * (see ioctl_ReadCopyright comments). |
0211de3039eb
update libdvdcss in libmpdvdkit to latest version (1.2.4)
arpi
parents:
8123
diff
changeset
|
90 * Apparently, on unencrypted DVDs _dvdcss_disckey() always fails, so |
0211de3039eb
update libdvdcss in libmpdvdkit to latest version (1.2.4)
arpi
parents:
8123
diff
changeset
|
91 * we can check this as a work-around. */ |
0211de3039eb
update libdvdcss in libmpdvdkit to latest version (1.2.4)
arpi
parents:
8123
diff
changeset
|
92 i_ret = 0; |
0211de3039eb
update libdvdcss in libmpdvdkit to latest version (1.2.4)
arpi
parents:
8123
diff
changeset
|
93 if( _dvdcss_disckey( dvdcss ) < 0 ) |
0211de3039eb
update libdvdcss in libmpdvdkit to latest version (1.2.4)
arpi
parents:
8123
diff
changeset
|
94 i_copyright = 0; |
0211de3039eb
update libdvdcss in libmpdvdkit to latest version (1.2.4)
arpi
parents:
8123
diff
changeset
|
95 else |
0211de3039eb
update libdvdcss in libmpdvdkit to latest version (1.2.4)
arpi
parents:
8123
diff
changeset
|
96 i_copyright = 1; |
0211de3039eb
update libdvdcss in libmpdvdkit to latest version (1.2.4)
arpi
parents:
8123
diff
changeset
|
97 } |
0211de3039eb
update libdvdcss in libmpdvdkit to latest version (1.2.4)
arpi
parents:
8123
diff
changeset
|
98 #endif |
0211de3039eb
update libdvdcss in libmpdvdkit to latest version (1.2.4)
arpi
parents:
8123
diff
changeset
|
99 |
7027 | 100 if( i_ret < 0 ) |
101 { | |
102 /* Since it's the first ioctl we try to issue, we add a notice */ | |
103 _dvdcss_error( dvdcss, "css error: ioctl_ReadCopyright failed, " | |
104 "make sure there is a DVD in the drive, and that " | |
8637
0211de3039eb
update libdvdcss in libmpdvdkit to latest version (1.2.4)
arpi
parents:
8123
diff
changeset
|
105 "you have used the correct device node." ); |
7027 | 106 |
107 return i_ret; | |
108 } | |
109 | |
110 return i_copyright; | |
111 } | |
112 | |
113 /***************************************************************************** | |
114 * GetBusKey : Go through the CSS Authentication process | |
115 ***************************************************************************** | |
116 * It simulates the mutual authentication between logical unit and host, | |
117 * and stops when a session key (called bus key) has been established. | |
118 * Always do the full auth sequence. Some drives seem to lie and always | |
119 * respond with ASF=1. For instance the old DVD roms on Compaq Armada says | |
120 * that ASF=1 from the start and then later fail with a 'read of scrambled | |
121 * block without authentication' error. | |
122 *****************************************************************************/ | |
123 static int GetBusKey( dvdcss_t dvdcss ) | |
124 { | |
125 u8 p_buffer[10]; | |
126 u8 p_challenge[2*KEY_SIZE]; | |
127 dvd_key_t p_key1; | |
128 dvd_key_t p_key2; | |
129 dvd_key_t p_key_check; | |
130 u8 i_variant = 0; | |
131 char psz_warning[80]; | |
132 int i_ret = -1; | |
133 int i; | |
134 | |
135 _dvdcss_debug( dvdcss, "requesting AGID" ); | |
136 i_ret = ioctl_ReportAgid( dvdcss->i_fd, &dvdcss->css.i_agid ); | |
137 | |
138 /* We might have to reset hung authentication processes in the drive | |
139 by invalidating the corresponding AGID'. As long as we haven't got | |
140 an AGID, invalidate one (in sequence) and try again. */ | |
141 for( i = 0; i_ret == -1 && i < 4 ; ++i ) | |
142 { | |
143 sprintf( psz_warning, | |
144 "ioctl ReportAgid failed, invalidating AGID %d", i ); | |
145 _dvdcss_debug( dvdcss, psz_warning ); | |
146 | |
147 /* This is really _not good_, should be handled by the OS. | |
148 Invalidating an AGID could make another process fail some | |
149 where in it's authentication process. */ | |
150 dvdcss->css.i_agid = i; | |
151 ioctl_InvalidateAgid( dvdcss->i_fd, &dvdcss->css.i_agid ); | |
152 | |
153 _dvdcss_debug( dvdcss, "requesting AGID" ); | |
154 i_ret = ioctl_ReportAgid( dvdcss->i_fd, &dvdcss->css.i_agid ); | |
155 } | |
156 | |
157 /* Unable to authenticate without AGID */ | |
158 if( i_ret == -1 ) | |
159 { | |
160 _dvdcss_error( dvdcss, "ioctl ReportAgid failed, fatal" ); | |
161 return -1; | |
162 } | |
163 | |
164 /* Setup a challenge, any values should work */ | |
165 for( i = 0 ; i < 10; ++i ) | |
166 { | |
167 p_challenge[i] = i; | |
168 } | |
169 | |
170 /* Get challenge from host */ | |
171 for( i = 0 ; i < 10 ; ++i ) | |
172 { | |
173 p_buffer[9-i] = p_challenge[i]; | |
174 } | |
175 | |
176 /* Send challenge to LU */ | |
177 if( ioctl_SendChallenge( dvdcss->i_fd, | |
178 &dvdcss->css.i_agid, p_buffer ) < 0 ) | |
179 { | |
180 _dvdcss_error( dvdcss, "ioctl SendChallenge failed" ); | |
181 ioctl_InvalidateAgid( dvdcss->i_fd, &dvdcss->css.i_agid ); | |
182 return -1; | |
183 } | |
184 | |
185 /* Get key1 from LU */ | |
186 if( ioctl_ReportKey1( dvdcss->i_fd, &dvdcss->css.i_agid, p_buffer ) < 0) | |
187 { | |
188 _dvdcss_error( dvdcss, "ioctl ReportKey1 failed" ); | |
189 ioctl_InvalidateAgid( dvdcss->i_fd, &dvdcss->css.i_agid ); | |
190 return -1; | |
191 } | |
192 | |
193 /* Send key1 to host */ | |
194 for( i = 0 ; i < KEY_SIZE ; i++ ) | |
195 { | |
196 p_key1[i] = p_buffer[4-i]; | |
197 } | |
198 | |
199 for( i = 0 ; i < 32 ; ++i ) | |
200 { | |
201 CryptKey( 0, i, p_challenge, p_key_check ); | |
202 | |
203 if( memcmp( p_key_check, p_key1, KEY_SIZE ) == 0 ) | |
204 { | |
205 snprintf( psz_warning, sizeof(psz_warning), | |
206 "drive authenticated, using variant %d", i ); | |
207 _dvdcss_debug( dvdcss, psz_warning ); | |
208 i_variant = i; | |
209 break; | |
210 } | |
211 } | |
212 | |
213 if( i == 32 ) | |
214 { | |
215 _dvdcss_error( dvdcss, "drive would not authenticate" ); | |
216 ioctl_InvalidateAgid( dvdcss->i_fd, &dvdcss->css.i_agid ); | |
217 return -1; | |
218 } | |
219 | |
220 /* Get challenge from LU */ | |
221 if( ioctl_ReportChallenge( dvdcss->i_fd, | |
222 &dvdcss->css.i_agid, p_buffer ) < 0 ) | |
223 { | |
224 _dvdcss_error( dvdcss, "ioctl ReportKeyChallenge failed" ); | |
225 ioctl_InvalidateAgid( dvdcss->i_fd, &dvdcss->css.i_agid ); | |
226 return -1; | |
227 } | |
228 | |
229 /* Send challenge to host */ | |
230 for( i = 0 ; i < 10 ; ++i ) | |
231 { | |
232 p_challenge[i] = p_buffer[9-i]; | |
233 } | |
234 | |
235 CryptKey( 1, i_variant, p_challenge, p_key2 ); | |
236 | |
237 /* Get key2 from host */ | |
238 for( i = 0 ; i < KEY_SIZE ; ++i ) | |
239 { | |
240 p_buffer[4-i] = p_key2[i]; | |
241 } | |
242 | |
243 /* Send key2 to LU */ | |
244 if( ioctl_SendKey2( dvdcss->i_fd, &dvdcss->css.i_agid, p_buffer ) < 0 ) | |
245 { | |
246 _dvdcss_error( dvdcss, "ioctl SendKey2 failed" ); | |
247 ioctl_InvalidateAgid( dvdcss->i_fd, &dvdcss->css.i_agid ); | |
248 return -1; | |
249 } | |
250 | |
251 /* The drive has accepted us as authentic. */ | |
252 _dvdcss_debug( dvdcss, "authentication established" ); | |
253 | |
254 memcpy( p_challenge, p_key1, KEY_SIZE ); | |
255 memcpy( p_challenge + KEY_SIZE, p_key2, KEY_SIZE ); | |
256 | |
257 CryptKey( 2, i_variant, p_challenge, dvdcss->css.p_bus_key ); | |
258 | |
259 return 0; | |
260 } | |
261 | |
262 /***************************************************************************** | |
263 * PrintKey : debug function that dumps a key value | |
264 *****************************************************************************/ | |
265 static void PrintKey( dvdcss_t dvdcss, char *prefix, u8 const *data ) | |
266 { | |
267 char psz_output[80]; | |
268 | |
269 sprintf( psz_output, "%s%02x:%02x:%02x:%02x:%02x", prefix, | |
270 data[0], data[1], data[2], data[3], data[4] ); | |
271 _dvdcss_debug( dvdcss, psz_output ); | |
272 } | |
273 | |
274 /***************************************************************************** | |
275 * _dvdcss_title: crack or decrypt the current title key if needed | |
276 ***************************************************************************** | |
8637
0211de3039eb
update libdvdcss in libmpdvdkit to latest version (1.2.4)
arpi
parents:
8123
diff
changeset
|
277 * This function should only be called by dvdcss->pf_seek and should eventually |
7027 | 278 * not be external if possible. |
279 *****************************************************************************/ | |
280 int _dvdcss_title ( dvdcss_t dvdcss, int i_block ) | |
281 { | |
282 dvd_title_t *p_title; | |
283 dvd_title_t *p_newtitle; | |
284 dvd_key_t p_title_key; | |
7032 | 285 int i_ret=-1; |
286 char* key_file=NULL; | |
7027 | 287 |
288 if( ! dvdcss->b_scrambled ) | |
289 { | |
290 return 0; | |
291 } | |
292 | |
293 /* Check if we've already cracked this key */ | |
294 p_title = dvdcss->p_titles; | |
295 while( p_title != NULL | |
296 && p_title->p_next != NULL | |
297 && p_title->p_next->i_startlb <= i_block ) | |
298 { | |
299 p_title = p_title->p_next; | |
300 } | |
301 | |
302 if( p_title != NULL | |
303 && p_title->i_startlb == i_block ) | |
304 { | |
305 /* We've already cracked this key, nothing to do */ | |
306 memcpy( dvdcss->css.p_title_key, p_title->p_key, sizeof(dvd_key_t) ); | |
307 return 0; | |
308 } | |
309 | |
7032 | 310 /* check teh CSS Key cache, if available: */ |
311 if(dvdcss->psz_cache){ | |
312 int fd; | |
313 key_file=malloc(strlen(dvdcss->psz_cache)+12+4); | |
8123
9fc45fe0d444
*HUGE* set of compiler warning fixes, unused variables removal
arpi
parents:
7033
diff
changeset
|
314 sprintf(key_file,"%s/%.10x",dvdcss->psz_cache,i_block); |
7032 | 315 if ( (fd=open( key_file,O_RDONLY ) ) > 0 ){ |
316 if(read(fd, p_title_key, 5)==5){ | |
317 // success! | |
318 free(key_file); key_file=NULL; | |
319 i_ret=1; | |
320 _dvdcss_debug( dvdcss, "key found in cache" ); | |
321 } | |
322 close(fd); | |
323 } | |
324 } | |
325 | |
7027 | 326 /* Crack or decrypt CSS title key for current VTS */ |
7032 | 327 if(i_ret<0) i_ret = _dvdcss_titlekey( dvdcss, i_block, p_title_key ); |
7027 | 328 |
329 if( i_ret < 0 ) | |
330 { | |
331 _dvdcss_error( dvdcss, "fatal error in vts css key" ); | |
332 return i_ret; | |
333 } | |
334 else if( i_ret == 0 ) | |
335 { | |
336 _dvdcss_debug( dvdcss, "unencrypted title" ); | |
337 /* Still store this in the cache, so we don't need to check again. */ | |
338 } | |
339 | |
7032 | 340 /* store in key-cache */ |
341 if(key_file){ | |
342 int fd; | |
343 if ( (fd=open( key_file,O_RDWR|O_CREAT|O_EXCL,0644 ) ) > 0 ){ | |
344 write(fd, p_title_key, 5); | |
345 close(fd); | |
346 } | |
347 free(key_file); | |
348 } | |
349 | |
7027 | 350 /* Find our spot in the list */ |
351 p_newtitle = NULL; | |
352 p_title = dvdcss->p_titles; | |
353 while( ( p_title != NULL ) && ( p_title->i_startlb < i_block ) ) | |
354 { | |
355 p_newtitle = p_title; | |
356 p_title = p_title->p_next; | |
357 } | |
358 | |
359 /* Save the found title */ | |
360 p_title = p_newtitle; | |
361 | |
362 /* Write in the new title and its key */ | |
363 p_newtitle = malloc( sizeof( dvd_title_t ) ); | |
364 p_newtitle->i_startlb = i_block; | |
365 memcpy( p_newtitle->p_key, p_title_key, KEY_SIZE ); | |
366 | |
367 /* Link it at the head of the (possibly empty) list */ | |
368 if( p_title == NULL ) | |
369 { | |
370 p_newtitle->p_next = dvdcss->p_titles; | |
371 dvdcss->p_titles = p_newtitle; | |
372 } | |
373 /* Link the new title inside the list */ | |
374 else | |
375 { | |
376 p_newtitle->p_next = p_title->p_next; | |
377 p_title->p_next = p_newtitle; | |
378 } | |
379 | |
380 memcpy( dvdcss->css.p_title_key, p_title_key, KEY_SIZE ); | |
381 return 0; | |
382 } | |
383 | |
384 /***************************************************************************** | |
385 * _dvdcss_disckey: get disc key. | |
386 ***************************************************************************** | |
387 * This function should only be called if DVD ioctls are present. | |
388 * It will set dvdcss->i_method = DVDCSS_METHOD_TITLE if it fails to find | |
389 * a valid disc key. | |
390 * Two decryption methods are offered: | |
391 * -disc key hash crack, | |
392 * -decryption with player keys if they are available. | |
393 *****************************************************************************/ | |
394 int _dvdcss_disckey( dvdcss_t dvdcss ) | |
395 { | |
396 unsigned char p_buffer[2048]; | |
397 dvd_key_t p_disc_key; | |
398 int i; | |
399 | |
400 if( GetBusKey( dvdcss ) < 0 ) | |
401 { | |
402 return -1; | |
403 } | |
404 | |
405 /* Get encrypted disc key */ | |
406 if( ioctl_ReadDiscKey( dvdcss->i_fd, &dvdcss->css.i_agid, p_buffer ) < 0 ) | |
407 { | |
408 _dvdcss_error( dvdcss, "ioctl ReadDiscKey failed" ); | |
409 return -1; | |
410 } | |
411 | |
412 /* This should have invaidated the AGID and got us ASF=1. */ | |
413 if( GetASF( dvdcss ) != 1 ) | |
414 { | |
415 /* Region mismatch (or region not set) is the most likely source. */ | |
416 _dvdcss_error( dvdcss, | |
417 "ASF not 1 after reading disc key (region mismatch?)" ); | |
418 ioctl_InvalidateAgid( dvdcss->i_fd, &dvdcss->css.i_agid ); | |
419 return -1; | |
420 } | |
421 | |
422 /* Decrypt disc key using bus key */ | |
423 for( i = 0 ; i < 2048 ; i++ ) | |
424 { | |
425 p_buffer[ i ] ^= dvdcss->css.p_bus_key[ 4 - (i % KEY_SIZE) ]; | |
426 } | |
427 | |
428 switch( dvdcss->i_method ) | |
429 { | |
430 case DVDCSS_METHOD_KEY: | |
431 | |
432 /* Decrypt disc key with player key. */ | |
433 _dvdcss_debug( dvdcss, "decrypting disc key with player keys" ); | |
434 if( ! DecryptDiscKey( p_buffer, p_disc_key ) ) | |
435 { | |
436 PrintKey( dvdcss, "decrypted disc key is ", p_disc_key ); | |
437 break; | |
438 } | |
439 _dvdcss_debug( dvdcss, "failed to decrypt the disc key, " | |
8637
0211de3039eb
update libdvdcss in libmpdvdkit to latest version (1.2.4)
arpi
parents:
8123
diff
changeset
|
440 "faulty drive/kernel? " |
0211de3039eb
update libdvdcss in libmpdvdkit to latest version (1.2.4)
arpi
parents:
8123
diff
changeset
|
441 "cracking title keys instead" ); |
7027 | 442 |
8637
0211de3039eb
update libdvdcss in libmpdvdkit to latest version (1.2.4)
arpi
parents:
8123
diff
changeset
|
443 /* Fallback, but not to DISC as the disc key might be faulty */ |
0211de3039eb
update libdvdcss in libmpdvdkit to latest version (1.2.4)
arpi
parents:
8123
diff
changeset
|
444 dvdcss->i_method = DVDCSS_METHOD_TITLE; |
0211de3039eb
update libdvdcss in libmpdvdkit to latest version (1.2.4)
arpi
parents:
8123
diff
changeset
|
445 break; |
7027 | 446 |
447 case DVDCSS_METHOD_DISC: | |
448 | |
449 /* Crack Disc key to be able to use it */ | |
450 _dvdcss_debug( dvdcss, "cracking disc key from key hash ..." | |
451 " this will take some time" ); | |
452 memcpy( p_disc_key, p_buffer, KEY_SIZE ); | |
453 if( ! CrackDiscKey( dvdcss, p_disc_key ) ) | |
454 { | |
455 PrintKey( dvdcss, "cracked disc key is ", p_disc_key ); | |
456 break; | |
457 } | |
458 _dvdcss_debug( dvdcss, "failed to crack the disc key" ); | |
8637
0211de3039eb
update libdvdcss in libmpdvdkit to latest version (1.2.4)
arpi
parents:
8123
diff
changeset
|
459 memset( p_disc_key, 0, KEY_SIZE ); |
7027 | 460 dvdcss->i_method = DVDCSS_METHOD_TITLE; |
461 break; | |
462 | |
463 default: | |
464 | |
465 _dvdcss_debug( dvdcss, "disc key needs not be decrypted" ); | |
466 memset( p_disc_key, 0, KEY_SIZE ); | |
467 break; | |
468 } | |
469 | |
470 memcpy( dvdcss->css.p_disc_key, p_disc_key, KEY_SIZE ); | |
471 | |
472 return 0; | |
473 } | |
474 | |
475 | |
476 /***************************************************************************** | |
477 * _dvdcss_titlekey: get title key. | |
478 *****************************************************************************/ | |
479 int _dvdcss_titlekey( dvdcss_t dvdcss, int i_pos, dvd_key_t p_title_key ) | |
480 { | |
481 static u8 p_garbage[ 2048 ]; /* static because we never read it */ | |
482 u8 p_key[KEY_SIZE]; | |
483 int i, i_ret = 0; | |
484 | |
485 if( dvdcss->b_ioctls && ( dvdcss->i_method == DVDCSS_METHOD_KEY || | |
486 dvdcss->i_method == DVDCSS_METHOD_DISC ) ) | |
487 { | |
488 /* We have a decrypted Disc key and the ioctls are available, | |
489 * read the title key and decrypt it. | |
490 */ | |
491 | |
492 _dvdcss_debug( dvdcss, "getting title key the classic way" ); | |
493 | |
494 /* We need to authenticate again every time to get a new session key */ | |
495 if( GetBusKey( dvdcss ) < 0 ) | |
496 { | |
497 return -1; | |
498 } | |
499 | |
500 /* Get encrypted title key */ | |
501 if( ioctl_ReadTitleKey( dvdcss->i_fd, &dvdcss->css.i_agid, | |
502 i_pos, p_key ) < 0 ) | |
503 { | |
504 _dvdcss_debug( dvdcss, | |
505 "ioctl ReadTitleKey failed (region mismatch?)" ); | |
506 i_ret = -1; | |
507 } | |
508 | |
509 /* Test ASF, it will be reset to 0 if we got a Region error */ | |
510 switch( GetASF( dvdcss ) ) | |
511 { | |
512 case -1: | |
513 /* An error getting the ASF status, something must be wrong. */ | |
514 _dvdcss_debug( dvdcss, "lost ASF reqesting title key" ); | |
515 ioctl_InvalidateAgid( dvdcss->i_fd, &dvdcss->css.i_agid ); | |
516 i_ret = -1; | |
517 break; | |
518 | |
519 case 0: | |
520 /* This might either be a title that has no key, | |
521 * or we encountered a region error. */ | |
522 _dvdcss_debug( dvdcss, "lost ASF reqesting title key" ); | |
523 break; | |
524 | |
525 case 1: | |
526 /* Drive status is ok. */ | |
527 /* If the title key request failed, but we did not loose ASF, | |
528 * we might stil have the AGID. Other code assume that we | |
529 * will not after this so invalidate it(?). */ | |
530 if( i_ret < 0 ) | |
531 { | |
532 ioctl_InvalidateAgid( dvdcss->i_fd, &dvdcss->css.i_agid ); | |
533 } | |
534 break; | |
535 } | |
536 | |
537 if( !( i_ret < 0 ) ) | |
538 { | |
539 /* Decrypt title key using the bus key */ | |
540 for( i = 0 ; i < KEY_SIZE ; i++ ) | |
541 { | |
542 p_key[ i ] ^= dvdcss->css.p_bus_key[ 4 - (i % KEY_SIZE) ]; | |
543 } | |
544 | |
545 /* If p_key is all zero then there realy wasn't any key pressent | |
546 * even though we got to read it without an error. */ | |
547 if( !( p_key[0] | p_key[1] | p_key[2] | p_key[3] | p_key[4] ) ) | |
548 { | |
549 i_ret = 0; | |
550 } | |
551 else | |
552 { | |
553 DecryptTitleKey( dvdcss->css.p_disc_key, p_key ); | |
554 i_ret = 1; | |
555 } | |
556 | |
557 /* All went well either there wasn't a key or we have it now. */ | |
558 memcpy( p_title_key, p_key, KEY_SIZE ); | |
559 PrintKey( dvdcss, "title key is ", p_title_key ); | |
560 | |
561 return i_ret; | |
562 } | |
563 | |
564 /* The title key request failed */ | |
565 _dvdcss_debug( dvdcss, "resetting drive and cracking title key" ); | |
566 | |
567 /* Read an unscrambled sector and reset the drive */ | |
8637
0211de3039eb
update libdvdcss in libmpdvdkit to latest version (1.2.4)
arpi
parents:
8123
diff
changeset
|
568 dvdcss->pf_seek( dvdcss, 0 ); |
0211de3039eb
update libdvdcss in libmpdvdkit to latest version (1.2.4)
arpi
parents:
8123
diff
changeset
|
569 dvdcss->pf_read( dvdcss, p_garbage, 1 ); |
0211de3039eb
update libdvdcss in libmpdvdkit to latest version (1.2.4)
arpi
parents:
8123
diff
changeset
|
570 dvdcss->pf_seek( dvdcss, 0 ); |
7027 | 571 _dvdcss_disckey( dvdcss ); |
572 | |
573 /* Fallback */ | |
574 } | |
575 | |
576 /* METHOD is TITLE, we can't use the ioctls or requesting the title key | |
577 * failed above. For these cases we try to crack the key instead. */ | |
578 | |
579 /* For now, the read limit is 9Gb / 2048 = 4718592 sectors. */ | |
580 i_ret = CrackTitleKey( dvdcss, i_pos, 4718592, p_key ); | |
581 | |
582 memcpy( p_title_key, p_key, KEY_SIZE ); | |
583 PrintKey( dvdcss, "title key is ", p_title_key ); | |
584 | |
585 return i_ret; | |
586 } | |
587 | |
588 /***************************************************************************** | |
589 * _dvdcss_unscramble: does the actual descrambling of data | |
590 ***************************************************************************** | |
591 * sec : sector to unscramble | |
592 * key : title key for this sector | |
593 *****************************************************************************/ | |
594 int _dvdcss_unscramble( dvd_key_t p_key, u8 *p_sec ) | |
595 { | |
596 unsigned int i_t1, i_t2, i_t3, i_t4, i_t5, i_t6; | |
597 u8 *p_end = p_sec + 0x800; | |
598 | |
599 /* PES_scrambling_control */ | |
600 if( p_sec[0x14] & 0x30) | |
601 { | |
602 i_t1 = (p_key[0] ^ p_sec[0x54]) | 0x100; | |
603 i_t2 = p_key[1] ^ p_sec[0x55]; | |
604 i_t3 = (p_key[2] | (p_key[3] << 8) | | |
605 (p_key[4] << 16)) ^ (p_sec[0x56] | | |
606 (p_sec[0x57] << 8) | (p_sec[0x58] << 16)); | |
607 i_t4 = i_t3 & 7; | |
608 i_t3 = i_t3 * 2 + 8 - i_t4; | |
609 p_sec += 0x80; | |
610 i_t5 = 0; | |
611 | |
612 while( p_sec != p_end ) | |
613 { | |
614 i_t4 = p_css_tab2[i_t2] ^ p_css_tab3[i_t1]; | |
615 i_t2 = i_t1>>1; | |
616 i_t1 = ( ( i_t1 & 1 ) << 8 ) ^ i_t4; | |
617 i_t4 = p_css_tab5[i_t4]; | |
618 i_t6 = ((((((( i_t3 >> 3 ) ^ i_t3 ) >> 1 ) ^ | |
619 i_t3 ) >> 8 ) ^ i_t3 ) >> 5 ) & 0xff; | |
620 i_t3 = (i_t3 << 8 ) | i_t6; | |
621 i_t6 = p_css_tab4[i_t6]; | |
622 i_t5 += i_t6 + i_t4; | |
623 *p_sec = p_css_tab1[*p_sec] ^ ( i_t5 & 0xff ); | |
624 p_sec++; | |
625 i_t5 >>= 8; | |
626 } | |
627 } | |
628 | |
629 return 0; | |
630 } | |
631 | |
632 /* Following functions are local */ | |
633 | |
634 /***************************************************************************** | |
635 * GetASF : Get Authentication success flag | |
636 ***************************************************************************** | |
637 * Returns : | |
638 * -1 on ioctl error, | |
639 * 0 if the device needs to be authenticated, | |
640 * 1 either. | |
641 *****************************************************************************/ | |
642 static int GetASF( dvdcss_t dvdcss ) | |
643 { | |
644 int i_asf = 0; | |
645 | |
646 if( ioctl_ReportASF( dvdcss->i_fd, NULL, &i_asf ) != 0 ) | |
647 { | |
648 /* The ioctl process has failed */ | |
649 _dvdcss_error( dvdcss, "GetASF fatal error" ); | |
650 return -1; | |
651 } | |
652 | |
653 if( i_asf ) | |
654 { | |
655 _dvdcss_debug( dvdcss, "GetASF authenticated, ASF=1" ); | |
656 } | |
657 else | |
658 { | |
659 _dvdcss_debug( dvdcss, "GetASF not authenticated, ASF=0" ); | |
660 } | |
661 | |
662 return i_asf; | |
663 } | |
664 | |
665 /***************************************************************************** | |
666 * CryptKey : shuffles bits and unencrypt keys. | |
667 ***************************************************************************** | |
668 * Used during authentication and disc key negociation in GetBusKey. | |
669 * i_key_type : 0->key1, 1->key2, 2->buskey. | |
670 * i_variant : between 0 and 31. | |
671 *****************************************************************************/ | |
672 static void CryptKey( int i_key_type, int i_variant, | |
673 u8 const *p_challenge, u8 *p_key ) | |
674 { | |
675 /* Permutation table for challenge */ | |
676 u8 pp_perm_challenge[3][10] = | |
677 { { 1, 3, 0, 7, 5, 2, 9, 6, 4, 8 }, | |
678 { 6, 1, 9, 3, 8, 5, 7, 4, 0, 2 }, | |
679 { 4, 0, 3, 5, 7, 2, 8, 6, 1, 9 } }; | |
680 | |
681 /* Permutation table for variant table for key2 and buskey */ | |
682 u8 pp_perm_variant[2][32] = | |
683 { { 0x0a, 0x08, 0x0e, 0x0c, 0x0b, 0x09, 0x0f, 0x0d, | |
684 0x1a, 0x18, 0x1e, 0x1c, 0x1b, 0x19, 0x1f, 0x1d, | |
685 0x02, 0x00, 0x06, 0x04, 0x03, 0x01, 0x07, 0x05, | |
686 0x12, 0x10, 0x16, 0x14, 0x13, 0x11, 0x17, 0x15 }, | |
687 { 0x12, 0x1a, 0x16, 0x1e, 0x02, 0x0a, 0x06, 0x0e, | |
688 0x10, 0x18, 0x14, 0x1c, 0x00, 0x08, 0x04, 0x0c, | |
689 0x13, 0x1b, 0x17, 0x1f, 0x03, 0x0b, 0x07, 0x0f, | |
690 0x11, 0x19, 0x15, 0x1d, 0x01, 0x09, 0x05, 0x0d } }; | |
691 | |
692 u8 p_variants[32] = | |
693 { 0xB7, 0x74, 0x85, 0xD0, 0xCC, 0xDB, 0xCA, 0x73, | |
694 0x03, 0xFE, 0x31, 0x03, 0x52, 0xE0, 0xB7, 0x42, | |
695 0x63, 0x16, 0xF2, 0x2A, 0x79, 0x52, 0xFF, 0x1B, | |
696 0x7A, 0x11, 0xCA, 0x1A, 0x9B, 0x40, 0xAD, 0x01 }; | |
697 | |
698 /* The "secret" key */ | |
699 u8 p_secret[5] = { 0x55, 0xD6, 0xC4, 0xC5, 0x28 }; | |
700 | |
701 u8 p_bits[30], p_scratch[10], p_tmp1[5], p_tmp2[5]; | |
702 u8 i_lfsr0_o; /* 1 bit used */ | |
703 u8 i_lfsr1_o; /* 1 bit used */ | |
704 u32 i_lfsr0, i_lfsr1; | |
705 u8 i_css_variant, i_cse, i_index, i_combined, i_carry; | |
706 u8 i_val = 0; | |
707 int i_term = 0; | |
708 int i_bit; | |
709 int i; | |
710 | |
711 for (i = 9; i >= 0; --i) | |
712 p_scratch[i] = p_challenge[pp_perm_challenge[i_key_type][i]]; | |
713 | |
714 i_css_variant = ( i_key_type == 0 ) ? i_variant : | |
715 pp_perm_variant[i_key_type-1][i_variant]; | |
716 | |
717 /* | |
718 * This encryption engine implements one of 32 variations | |
719 * one the same theme depending upon the choice in the | |
720 * variant parameter (0 - 31). | |
721 * | |
722 * The algorithm itself manipulates a 40 bit input into | |
723 * a 40 bit output. | |
724 * The parameter 'input' is 80 bits. It consists of | |
725 * the 40 bit input value that is to be encrypted followed | |
726 * by a 40 bit seed value for the pseudo random number | |
727 * generators. | |
728 */ | |
729 | |
730 /* Feed the secret into the input values such that | |
731 * we alter the seed to the LFSR's used above, then | |
732 * generate the bits to play with. | |
733 */ | |
734 for( i = 5 ; --i >= 0 ; ) | |
735 { | |
736 p_tmp1[i] = p_scratch[5 + i] ^ p_secret[i] ^ p_crypt_tab2[i]; | |
737 } | |
738 | |
739 /* | |
740 * We use two LFSR's (seeded from some of the input data bytes) to | |
741 * generate two streams of pseudo-random bits. These two bit streams | |
742 * are then combined by simply adding with carry to generate a final | |
743 * sequence of pseudo-random bits which is stored in the buffer that | |
744 * 'output' points to the end of - len is the size of this buffer. | |
745 * | |
746 * The first LFSR is of degree 25, and has a polynomial of: | |
747 * x^13 + x^5 + x^4 + x^1 + 1 | |
748 * | |
749 * The second LSFR is of degree 17, and has a (primitive) polynomial of: | |
750 * x^15 + x^1 + 1 | |
751 * | |
752 * I don't know if these polynomials are primitive modulo 2, and thus | |
753 * represent maximal-period LFSR's. | |
754 * | |
755 * | |
756 * Note that we take the output of each LFSR from the new shifted in | |
757 * bit, not the old shifted out bit. Thus for ease of use the LFSR's | |
758 * are implemented in bit reversed order. | |
759 * | |
760 */ | |
761 | |
762 /* In order to ensure that the LFSR works we need to ensure that the | |
763 * initial values are non-zero. Thus when we initialise them from | |
764 * the seed, we ensure that a bit is set. | |
765 */ | |
766 i_lfsr0 = ( p_tmp1[0] << 17 ) | ( p_tmp1[1] << 9 ) | | |
767 (( p_tmp1[2] & ~7 ) << 1 ) | 8 | ( p_tmp1[2] & 7 ); | |
768 i_lfsr1 = ( p_tmp1[3] << 9 ) | 0x100 | p_tmp1[4]; | |
769 | |
770 i_index = sizeof(p_bits); | |
771 i_carry = 0; | |
772 | |
773 do | |
774 { | |
775 for( i_bit = 0, i_val = 0 ; i_bit < 8 ; ++i_bit ) | |
776 { | |
777 | |
778 i_lfsr0_o = ( ( i_lfsr0 >> 24 ) ^ ( i_lfsr0 >> 21 ) ^ | |
779 ( i_lfsr0 >> 20 ) ^ ( i_lfsr0 >> 12 ) ) & 1; | |
780 i_lfsr0 = ( i_lfsr0 << 1 ) | i_lfsr0_o; | |
781 | |
782 i_lfsr1_o = ( ( i_lfsr1 >> 16 ) ^ ( i_lfsr1 >> 2 ) ) & 1; | |
783 i_lfsr1 = ( i_lfsr1 << 1 ) | i_lfsr1_o; | |
784 | |
785 i_combined = !i_lfsr1_o + i_carry + !i_lfsr0_o; | |
786 /* taking bit 1 */ | |
787 i_carry = ( i_combined >> 1 ) & 1; | |
788 i_val |= ( i_combined & 1 ) << i_bit; | |
789 } | |
790 | |
791 p_bits[--i_index] = i_val; | |
792 } while( i_index > 0 ); | |
793 | |
794 /* This term is used throughout the following to | |
795 * select one of 32 different variations on the | |
796 * algorithm. | |
797 */ | |
798 i_cse = p_variants[i_css_variant] ^ p_crypt_tab2[i_css_variant]; | |
799 | |
800 /* Now the actual blocks doing the encryption. Each | |
801 * of these works on 40 bits at a time and are quite | |
802 * similar. | |
803 */ | |
804 i_index = 0; | |
805 for( i = 5, i_term = 0 ; --i >= 0 ; i_term = p_scratch[i] ) | |
806 { | |
807 i_index = p_bits[25 + i] ^ p_scratch[i]; | |
808 i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse; | |
809 | |
810 p_tmp1[i] = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term; | |
811 } | |
812 p_tmp1[4] ^= p_tmp1[0]; | |
813 | |
814 for( i = 5, i_term = 0 ; --i >= 0 ; i_term = p_tmp1[i] ) | |
815 { | |
816 i_index = p_bits[20 + i] ^ p_tmp1[i]; | |
817 i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse; | |
818 | |
819 p_tmp2[i] = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term; | |
820 } | |
821 p_tmp2[4] ^= p_tmp2[0]; | |
822 | |
823 for( i = 5, i_term = 0 ; --i >= 0 ; i_term = p_tmp2[i] ) | |
824 { | |
825 i_index = p_bits[15 + i] ^ p_tmp2[i]; | |
826 i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse; | |
827 i_index = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term; | |
828 | |
829 p_tmp1[i] = p_crypt_tab0[i_index] ^ p_crypt_tab2[i_index]; | |
830 } | |
831 p_tmp1[4] ^= p_tmp1[0]; | |
832 | |
833 for( i = 5, i_term = 0 ; --i >= 0 ; i_term = p_tmp1[i] ) | |
834 { | |
835 i_index = p_bits[10 + i] ^ p_tmp1[i]; | |
836 i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse; | |
837 | |
838 i_index = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term; | |
839 | |
840 p_tmp2[i] = p_crypt_tab0[i_index] ^ p_crypt_tab2[i_index]; | |
841 } | |
842 p_tmp2[4] ^= p_tmp2[0]; | |
843 | |
844 for( i = 5, i_term = 0 ; --i >= 0 ; i_term = p_tmp2[i] ) | |
845 { | |
846 i_index = p_bits[5 + i] ^ p_tmp2[i]; | |
847 i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse; | |
848 | |
849 p_tmp1[i] = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term; | |
850 } | |
851 p_tmp1[4] ^= p_tmp1[0]; | |
852 | |
853 for(i = 5, i_term = 0 ; --i >= 0 ; i_term = p_tmp1[i] ) | |
854 { | |
855 i_index = p_bits[i] ^ p_tmp1[i]; | |
856 i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse; | |
857 | |
858 p_key[i] = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term; | |
859 } | |
860 | |
861 return; | |
862 } | |
863 | |
864 /***************************************************************************** | |
865 * DecryptKey: decrypt p_crypted with p_key. | |
866 ***************************************************************************** | |
867 * Used to decrypt the disc key, with a player key, after requesting it | |
868 * in _dvdcss_disckey and to decrypt title keys, with a disc key, requested | |
869 * in _dvdcss_titlekey. | |
870 * The player keys and the resulting disc key are only used as KEKs | |
871 * (key encryption keys). | |
872 * Decryption is slightly dependant on the type of key: | |
873 * -for disc key, invert is 0x00, | |
874 * -for title key, invert if 0xff. | |
875 *****************************************************************************/ | |
876 static void DecryptKey( u8 invert, u8 const *p_key, | |
877 u8 const *p_crypted, u8 *p_result ) | |
878 { | |
879 unsigned int i_lfsr1_lo; | |
880 unsigned int i_lfsr1_hi; | |
881 unsigned int i_lfsr0; | |
882 unsigned int i_combined; | |
883 u8 o_lfsr0; | |
884 u8 o_lfsr1; | |
885 u8 k[5]; | |
886 int i; | |
887 | |
888 i_lfsr1_lo = p_key[0] | 0x100; | |
889 i_lfsr1_hi = p_key[1]; | |
890 | |
891 i_lfsr0 = ( ( p_key[4] << 17 ) | |
892 | ( p_key[3] << 9 ) | |
893 | ( p_key[2] << 1 ) ) | |
894 + 8 - ( p_key[2] & 7 ); | |
895 i_lfsr0 = ( p_css_tab4[i_lfsr0 & 0xff] << 24 ) | | |
896 ( p_css_tab4[( i_lfsr0 >> 8 ) & 0xff] << 16 ) | | |
897 ( p_css_tab4[( i_lfsr0 >> 16 ) & 0xff] << 8 ) | | |
898 p_css_tab4[( i_lfsr0 >> 24 ) & 0xff]; | |
899 | |
900 i_combined = 0; | |
901 for( i = 0 ; i < KEY_SIZE ; ++i ) | |
902 { | |
903 o_lfsr1 = p_css_tab2[i_lfsr1_hi] ^ p_css_tab3[i_lfsr1_lo]; | |
904 i_lfsr1_hi = i_lfsr1_lo >> 1; | |
905 i_lfsr1_lo = ( ( i_lfsr1_lo & 1 ) << 8 ) ^ o_lfsr1; | |
906 o_lfsr1 = p_css_tab4[o_lfsr1]; | |
907 | |
908 o_lfsr0 = ((((((( i_lfsr0 >> 8 ) ^ i_lfsr0 ) >> 1 ) | |
909 ^ i_lfsr0 ) >> 3 ) ^ i_lfsr0 ) >> 7 ); | |
910 i_lfsr0 = ( i_lfsr0 >> 8 ) | ( o_lfsr0 << 24 ); | |
911 | |
912 i_combined += ( o_lfsr0 ^ invert ) + o_lfsr1; | |
913 k[i] = i_combined & 0xff; | |
914 i_combined >>= 8; | |
915 } | |
916 | |
917 p_result[4] = k[4] ^ p_css_tab1[p_crypted[4]] ^ p_crypted[3]; | |
918 p_result[3] = k[3] ^ p_css_tab1[p_crypted[3]] ^ p_crypted[2]; | |
919 p_result[2] = k[2] ^ p_css_tab1[p_crypted[2]] ^ p_crypted[1]; | |
920 p_result[1] = k[1] ^ p_css_tab1[p_crypted[1]] ^ p_crypted[0]; | |
921 p_result[0] = k[0] ^ p_css_tab1[p_crypted[0]] ^ p_result[4]; | |
922 | |
923 p_result[4] = k[4] ^ p_css_tab1[p_result[4]] ^ p_result[3]; | |
924 p_result[3] = k[3] ^ p_css_tab1[p_result[3]] ^ p_result[2]; | |
925 p_result[2] = k[2] ^ p_css_tab1[p_result[2]] ^ p_result[1]; | |
926 p_result[1] = k[1] ^ p_css_tab1[p_result[1]] ^ p_result[0]; | |
927 p_result[0] = k[0] ^ p_css_tab1[p_result[0]]; | |
928 | |
929 return; | |
930 } | |
931 | |
932 /***************************************************************************** | |
933 * DecryptDiscKey | |
934 ***************************************************************************** | |
935 * Decryption of the disc key with player keys if they are available. | |
936 * Try to decrypt the disc key from every position with every player key. | |
937 * p_struct_disckey: the 2048 byte DVD_STRUCT_DISCKEY data | |
938 * p_disc_key: result, the 5 byte disc key | |
939 *****************************************************************************/ | |
940 static int DecryptDiscKey( u8 const *p_struct_disckey, dvd_key_t p_disc_key ) | |
941 { | |
942 u8 p_verify[KEY_SIZE]; | |
943 int i, n = 0; | |
944 | |
945 static const dvd_key_t player_keys[] = | |
946 { | |
947 { 0x01, 0xaf, 0xe3, 0x12, 0x80 }, | |
948 { 0x12, 0x11, 0xca, 0x04, 0x3b }, | |
949 { 0x14, 0x0c, 0x9e, 0xd0, 0x09 }, | |
950 { 0x14, 0x71, 0x35, 0xba, 0xe2 }, | |
951 { 0x1a, 0xa4, 0x33, 0x21, 0xa6 }, | |
952 { 0x26, 0xec, 0xc4, 0xa7, 0x4e }, | |
953 { 0x2c, 0xb2, 0xc1, 0x09, 0xee }, | |
954 { 0x2f, 0x25, 0x9e, 0x96, 0xdd }, | |
955 { 0x33, 0x2f, 0x49, 0x6c, 0xe0 }, | |
956 { 0x35, 0x5b, 0xc1, 0x31, 0x0f }, | |
957 { 0x36, 0x67, 0xb2, 0xe3, 0x85 }, | |
958 { 0x39, 0x3d, 0xf1, 0xf1, 0xbd }, | |
959 { 0x3b, 0x31, 0x34, 0x0d, 0x91 }, | |
960 { 0x45, 0xed, 0x28, 0xeb, 0xd3 }, | |
961 { 0x48, 0xb7, 0x6c, 0xce, 0x69 }, | |
962 { 0x4b, 0x65, 0x0d, 0xc1, 0xee }, | |
963 { 0x4c, 0xbb, 0xf5, 0x5b, 0x23 }, | |
964 { 0x51, 0x67, 0x67, 0xc5, 0xe0 }, | |
965 { 0x53, 0x94, 0xe1, 0x75, 0xbf }, | |
966 { 0x57, 0x2c, 0x8b, 0x31, 0xae }, | |
967 { 0x63, 0xdb, 0x4c, 0x5b, 0x4a }, | |
968 { 0x7b, 0x1e, 0x5e, 0x2b, 0x57 }, | |
969 { 0x85, 0xf3, 0x85, 0xa0, 0xe0 }, | |
970 { 0xab, 0x1e, 0xe7, 0x7b, 0x72 }, | |
971 { 0xab, 0x36, 0xe3, 0xeb, 0x76 }, | |
972 { 0xb1, 0xb8, 0xf9, 0x38, 0x03 }, | |
973 { 0xb8, 0x5d, 0xd8, 0x53, 0xbd }, | |
974 { 0xbf, 0x92, 0xc3, 0xb0, 0xe2 }, | |
975 { 0xcf, 0x1a, 0xb2, 0xf8, 0x0a }, | |
976 { 0xec, 0xa0, 0xcf, 0xb3, 0xff }, | |
977 { 0xfc, 0x95, 0xa9, 0x87, 0x35 } | |
978 }; | |
979 | |
980 /* Decrypt disc key with player keys from csskeys.h */ | |
981 while( n < sizeof(player_keys) / sizeof(dvd_key_t) ) | |
982 { | |
983 for( i = 1; i < 409; i++ ) | |
984 { | |
985 /* Check if player key n is the right key for position i. */ | |
986 DecryptKey( 0, player_keys[n], p_struct_disckey + 5 * i, | |
987 p_disc_key ); | |
988 | |
989 /* The first part in the struct_disckey block is the | |
990 * 'disc key' encrypted with it self. Using this we | |
991 * can check if we decrypted the correct key. */ | |
992 DecryptKey( 0, p_disc_key, p_struct_disckey, p_verify ); | |
993 | |
994 /* If the position / player key pair worked then return. */ | |
8637
0211de3039eb
update libdvdcss in libmpdvdkit to latest version (1.2.4)
arpi
parents:
8123
diff
changeset
|
995 if( memcmp( p_disc_key, p_verify, KEY_SIZE ) == 0 ) |
7027 | 996 { |
997 return 0; | |
998 } | |
999 } | |
1000 n++; | |
1001 } | |
1002 | |
1003 /* Have tried all combinations of positions and keys, | |
1004 * and we still didn't succeed. */ | |
8637
0211de3039eb
update libdvdcss in libmpdvdkit to latest version (1.2.4)
arpi
parents:
8123
diff
changeset
|
1005 memset( p_disc_key, 0, KEY_SIZE ); |
7027 | 1006 return -1; |
1007 } | |
1008 | |
1009 /***************************************************************************** | |
1010 * DecryptTitleKey | |
1011 ***************************************************************************** | |
1012 * Decrypt the title key using the disc key. | |
1013 * p_disc_key: result, the 5 byte disc key | |
1014 * p_titlekey: the encrypted title key, gets overwritten by the decrypted key | |
1015 *****************************************************************************/ | |
1016 static void DecryptTitleKey( dvd_key_t p_disc_key, dvd_key_t p_titlekey ) | |
1017 { | |
1018 DecryptKey( 0xff, p_disc_key, p_titlekey, p_titlekey ); | |
1019 } | |
1020 | |
1021 /***************************************************************************** | |
1022 * CrackDiscKey: brute force disc key | |
1023 * CSS hash reversal function designed by Frank Stevenson | |
1024 ***************************************************************************** | |
1025 * This function uses a big amount of memory to crack the disc key from the | |
1026 * disc key hash, if player keys are not available. | |
1027 *****************************************************************************/ | |
1028 #define K1TABLEWIDTH 10 | |
1029 | |
1030 /* | |
1031 * Simple function to test if a candidate key produces the given hash | |
1032 */ | |
1033 static int investigate( unsigned char *hash, unsigned char *ckey ) | |
1034 { | |
1035 unsigned char key[KEY_SIZE]; | |
1036 | |
1037 DecryptKey( 0, ckey, hash, key ); | |
1038 | |
1039 return memcmp( key, ckey, KEY_SIZE ); | |
1040 } | |
1041 | |
1042 static int CrackDiscKey( dvdcss_t dvdcss, u8 *p_disc_key ) | |
1043 { | |
1044 unsigned char B[5] = { 0,0,0,0,0 }; /* Second Stage of mangle cipher */ | |
1045 unsigned char C[5] = { 0,0,0,0,0 }; /* Output Stage of mangle cipher | |
1046 * IntermediateKey */ | |
1047 unsigned char k[5] = { 0,0,0,0,0 }; /* Mangling cipher key | |
1048 * Also output from CSS( C ) */ | |
1049 unsigned char out1[5]; /* five first output bytes of LFSR1 */ | |
1050 unsigned char out2[5]; /* five first output bytes of LFSR2 */ | |
1051 unsigned int lfsr1a; /* upper 9 bits of LFSR1 */ | |
1052 unsigned int lfsr1b; /* lower 8 bits of LFSR1 */ | |
1053 unsigned int tmp, tmp2, tmp3, tmp4,tmp5; | |
1054 int i,j; | |
1055 unsigned int nStepA; /* iterator for LFSR1 start state */ | |
1056 unsigned int nStepB; /* iterator for possible B[0] */ | |
1057 unsigned int nTry; /* iterator for K[1] possibilities */ | |
1058 unsigned int nPossibleK1; /* #of possible K[1] values */ | |
1059 unsigned char* K1table; /* Lookup table for possible K[1] */ | |
1060 unsigned int* BigTable; /* LFSR2 startstate indexed by | |
1061 * 1,2,5 output byte */ | |
1062 | |
1063 _dvdcss_debug( dvdcss, "cracking disc key" ); | |
1064 | |
1065 /* | |
1066 * Prepare tables for hash reversal | |
1067 */ | |
1068 | |
1069 | |
1070 /* initialize lookup tables for k[1] */ | |
1071 K1table = malloc( 65536 * K1TABLEWIDTH ); | |
1072 memset( K1table, 0 , 65536 * K1TABLEWIDTH ); | |
1073 if( K1table == NULL ) | |
1074 { | |
1075 return -1; | |
1076 } | |
1077 | |
1078 tmp = p_disc_key[0] ^ p_css_tab1[ p_disc_key[1] ]; | |
1079 for( i = 0 ; i < 256 ; i++ ) /* k[1] */ | |
1080 { | |
1081 tmp2 = p_css_tab1[ tmp ^ i ]; /* p_css_tab1[ B[1] ]*/ | |
1082 | |
1083 for( j = 0 ; j < 256 ; j++ ) /* B[0] */ | |
1084 { | |
1085 tmp3 = j ^ tmp2 ^ i; /* C[1] */ | |
1086 tmp4 = K1table[ K1TABLEWIDTH * ( 256 * j + tmp3 ) ]; /* count of entries here */ | |
1087 tmp4++; | |
1088 /* | |
1089 if( tmp4 == K1TABLEWIDTH ) | |
1090 { | |
1091 _dvdcss_debug( dvdcss, "Table disaster %d", tmp4 ); | |
1092 } | |
1093 */ | |
1094 if( tmp4 < K1TABLEWIDTH ) | |
1095 { | |
1096 K1table[ K1TABLEWIDTH * ( 256 * j + tmp3 ) + tmp4 ] = i; | |
1097 } | |
1098 K1table[ K1TABLEWIDTH * ( 256 * j + tmp3 ) ] = tmp4; | |
1099 } | |
1100 } | |
1101 | |
1102 /* Initing our Really big table */ | |
1103 BigTable = malloc( 16777216 * sizeof(int) ); | |
1104 memset( BigTable, 0 , 16777216 * sizeof(int) ); | |
1105 if( BigTable == NULL ) | |
1106 { | |
1107 return -1; | |
1108 } | |
1109 | |
1110 tmp3 = 0; | |
1111 | |
1112 _dvdcss_debug( dvdcss, "initializing the big table" ); | |
1113 | |
1114 for( i = 0 ; i < 16777216 ; i++ ) | |
1115 { | |
1116 tmp = (( i + i ) & 0x1fffff0 ) | 0x8 | ( i & 0x7 ); | |
1117 | |
1118 for( j = 0 ; j < 5 ; j++ ) | |
1119 { | |
1120 tmp2=((((((( tmp >> 3 ) ^ tmp ) >> 1 ) ^ tmp ) >> 8 ) | |
1121 ^ tmp ) >> 5 ) & 0xff; | |
1122 tmp = ( tmp << 8) | tmp2; | |
1123 out2[j] = p_css_tab4[ tmp2 ]; | |
1124 } | |
1125 | |
1126 j = ( out2[0] << 16 ) | ( out2[1] << 8 ) | out2[4]; | |
1127 BigTable[j] = i; | |
1128 } | |
1129 | |
1130 /* | |
1131 * We are done initing, now reverse hash | |
1132 */ | |
1133 tmp5 = p_disc_key[0] ^ p_css_tab1[ p_disc_key[1] ]; | |
1134 | |
1135 for( nStepA = 0 ; nStepA < 65536 ; nStepA ++ ) | |
1136 { | |
1137 lfsr1a = 0x100 | ( nStepA >> 8 ); | |
1138 lfsr1b = nStepA & 0xff; | |
1139 | |
1140 /* Generate 5 first output bytes from lfsr1 */ | |
1141 for( i = 0 ; i < 5 ; i++ ) | |
1142 { | |
1143 tmp = p_css_tab2[ lfsr1b ] ^ p_css_tab3[ lfsr1a ]; | |
1144 lfsr1b = lfsr1a >> 1; | |
1145 lfsr1a = ((lfsr1a&1)<<8) ^ tmp; | |
1146 out1[ i ] = p_css_tab4[ tmp ]; | |
1147 } | |
1148 | |
1149 /* cumpute and cache some variables */ | |
1150 C[0] = nStepA >> 8; | |
1151 C[1] = nStepA & 0xff; | |
1152 tmp = p_disc_key[3] ^ p_css_tab1[ p_disc_key[4] ]; | |
1153 tmp2 = p_css_tab1[ p_disc_key[0] ]; | |
1154 | |
1155 /* Search through all possible B[0] */ | |
1156 for( nStepB = 0 ; nStepB < 256 ; nStepB++ ) | |
1157 { | |
1158 /* reverse parts of the mangling cipher */ | |
1159 B[0] = nStepB; | |
1160 k[0] = p_css_tab1[ B[0] ] ^ C[0]; | |
1161 B[4] = B[0] ^ k[0] ^ tmp2; | |
1162 k[4] = B[4] ^ tmp; | |
1163 nPossibleK1 = K1table[ K1TABLEWIDTH * (256 * B[0] + C[1]) ]; | |
1164 | |
1165 /* Try out all possible values for k[1] */ | |
1166 for( nTry = 0 ; nTry < nPossibleK1 ; nTry++ ) | |
1167 { | |
1168 k[1] = K1table[ K1TABLEWIDTH * (256 * B[0] + C[1]) + nTry + 1 ]; | |
1169 B[1] = tmp5 ^ k[1]; | |
1170 | |
1171 /* reconstruct output from LFSR2 */ | |
1172 tmp3 = ( 0x100 + k[0] - out1[0] ); | |
1173 out2[0] = tmp3 & 0xff; | |
1174 tmp3 = tmp3 & 0x100 ? 0x100 : 0xff; | |
1175 tmp3 = ( tmp3 + k[1] - out1[1] ); | |
1176 out2[1] = tmp3 & 0xff; | |
1177 tmp3 = ( 0x100 + k[4] - out1[4] ); | |
1178 out2[4] = tmp3 & 0xff; /* Can be 1 off */ | |
1179 | |
1180 /* test first possible out2[4] */ | |
1181 tmp4 = ( out2[0] << 16 ) | ( out2[1] << 8 ) | out2[4]; | |
1182 tmp4 = BigTable[ tmp4 ]; | |
1183 C[2] = tmp4 & 0xff; | |
1184 C[3] = ( tmp4 >> 8 ) & 0xff; | |
1185 C[4] = ( tmp4 >> 16 ) & 0xff; | |
1186 B[3] = p_css_tab1[ B[4] ] ^ k[4] ^ C[4]; | |
1187 k[3] = p_disc_key[2] ^ p_css_tab1[ p_disc_key[3] ] ^ B[3]; | |
1188 B[2] = p_css_tab1[ B[3] ] ^ k[3] ^ C[3]; | |
1189 k[2] = p_disc_key[1] ^ p_css_tab1[ p_disc_key[2] ] ^ B[2]; | |
1190 | |
1191 if( ( B[1] ^ p_css_tab1[ B[2] ] ^ k[ 2 ] ) == C[ 2 ] ) | |
1192 { | |
1193 if( ! investigate( &p_disc_key[0] , &C[0] ) ) | |
1194 { | |
1195 goto end; | |
1196 } | |
1197 } | |
1198 | |
1199 /* Test second possible out2[4] */ | |
1200 out2[4] = ( out2[4] + 0xff ) & 0xff; | |
1201 tmp4 = ( out2[0] << 16 ) | ( out2[1] << 8 ) | out2[4]; | |
1202 tmp4 = BigTable[ tmp4 ]; | |
1203 C[2] = tmp4 & 0xff; | |
1204 C[3] = ( tmp4 >> 8 ) & 0xff; | |
1205 C[4] = ( tmp4 >> 16 ) & 0xff; | |
1206 B[3] = p_css_tab1[ B[4] ] ^ k[4] ^ C[4]; | |
1207 k[3] = p_disc_key[2] ^ p_css_tab1[ p_disc_key[3] ] ^ B[3]; | |
1208 B[2] = p_css_tab1[ B[3] ] ^ k[3] ^ C[3]; | |
1209 k[2] = p_disc_key[1] ^ p_css_tab1[ p_disc_key[2] ] ^ B[2]; | |
1210 | |
1211 if( ( B[1] ^ p_css_tab1[ B[2] ] ^ k[ 2 ] ) == C[ 2 ] ) | |
1212 { | |
1213 if( ! investigate( &p_disc_key[0] , &C[0] ) ) | |
1214 { | |
1215 goto end; | |
1216 } | |
1217 } | |
1218 } | |
1219 } | |
1220 } | |
1221 | |
1222 end: | |
1223 | |
1224 memcpy( p_disc_key, &C[0], KEY_SIZE ); | |
1225 | |
1226 free( K1table ); | |
1227 free( BigTable ); | |
1228 | |
1229 return 0; | |
1230 } | |
1231 | |
1232 /***************************************************************************** | |
1233 * RecoverTitleKey: (title) key recovery from cipher and plain text | |
1234 * Function designed by Frank Stevenson | |
1235 ***************************************************************************** | |
1236 * Called from Attack* which are in turn called by CrackTitleKey. Given | |
1237 * a guessed(?) plain text and the chiper text. Returns -1 on failure. | |
1238 *****************************************************************************/ | |
1239 static int RecoverTitleKey( int i_start, u8 const *p_crypted, | |
1240 u8 const *p_decrypted, | |
1241 u8 const *p_sector_seed, u8 *p_key ) | |
1242 { | |
1243 u8 p_buffer[10]; | |
1244 unsigned int i_t1, i_t2, i_t3, i_t4, i_t5, i_t6; | |
1245 unsigned int i_try; | |
1246 unsigned int i_candidate; | |
1247 unsigned int i, j; | |
1248 int i_exit = -1; | |
1249 | |
1250 for( i = 0 ; i < 10 ; i++ ) | |
1251 { | |
1252 p_buffer[i] = p_css_tab1[p_crypted[i]] ^ p_decrypted[i]; | |
1253 } | |
1254 | |
1255 for( i_try = i_start ; i_try < 0x10000 ; i_try++ ) | |
1256 { | |
1257 i_t1 = i_try >> 8 | 0x100; | |
1258 i_t2 = i_try & 0xff; | |
1259 i_t3 = 0; /* not needed */ | |
1260 i_t5 = 0; | |
1261 | |
1262 /* iterate cipher 4 times to reconstruct LFSR2 */ | |
1263 for( i = 0 ; i < 4 ; i++ ) | |
1264 { | |
1265 /* advance LFSR1 normaly */ | |
1266 i_t4 = p_css_tab2[i_t2] ^ p_css_tab3[i_t1]; | |
1267 i_t2 = i_t1 >> 1; | |
1268 i_t1 = ( ( i_t1 & 1 ) << 8 ) ^ i_t4; | |
1269 i_t4 = p_css_tab5[i_t4]; | |
1270 /* deduce i_t6 & i_t5 */ | |
1271 i_t6 = p_buffer[i]; | |
1272 if( i_t5 ) | |
1273 { | |
1274 i_t6 = ( i_t6 + 0xff ) & 0x0ff; | |
1275 } | |
1276 if( i_t6 < i_t4 ) | |
1277 { | |
1278 i_t6 += 0x100; | |
1279 } | |
1280 i_t6 -= i_t4; | |
1281 i_t5 += i_t6 + i_t4; | |
1282 i_t6 = p_css_tab4[ i_t6 ]; | |
1283 /* feed / advance i_t3 / i_t5 */ | |
1284 i_t3 = ( i_t3 << 8 ) | i_t6; | |
1285 i_t5 >>= 8; | |
1286 } | |
1287 | |
1288 i_candidate = i_t3; | |
1289 | |
1290 /* iterate 6 more times to validate candidate key */ | |
1291 for( ; i < 10 ; i++ ) | |
1292 { | |
1293 i_t4 = p_css_tab2[i_t2] ^ p_css_tab3[i_t1]; | |
1294 i_t2 = i_t1 >> 1; | |
1295 i_t1 = ( ( i_t1 & 1 ) << 8 ) ^ i_t4; | |
1296 i_t4 = p_css_tab5[i_t4]; | |
1297 i_t6 = ((((((( i_t3 >> 3 ) ^ i_t3 ) >> 1 ) ^ | |
1298 i_t3 ) >> 8 ) ^ i_t3 ) >> 5 ) & 0xff; | |
1299 i_t3 = ( i_t3 << 8 ) | i_t6; | |
1300 i_t6 = p_css_tab4[i_t6]; | |
1301 i_t5 += i_t6 + i_t4; | |
1302 if( ( i_t5 & 0xff ) != p_buffer[i] ) | |
1303 { | |
1304 break; | |
1305 } | |
1306 | |
1307 i_t5 >>= 8; | |
1308 } | |
1309 | |
1310 if( i == 10 ) | |
1311 { | |
1312 /* Do 4 backwards steps of iterating t3 to deduce initial state */ | |
1313 i_t3 = i_candidate; | |
1314 for( i = 0 ; i < 4 ; i++ ) | |
1315 { | |
1316 i_t1 = i_t3 & 0xff; | |
1317 i_t3 = ( i_t3 >> 8 ); | |
1318 /* easy to code, and fast enough bruteforce | |
1319 * search for byte shifted in */ | |
1320 for( j = 0 ; j < 256 ; j++ ) | |
1321 { | |
1322 i_t3 = ( i_t3 & 0x1ffff ) | ( j << 17 ); | |
1323 i_t6 = ((((((( i_t3 >> 3 ) ^ i_t3 ) >> 1 ) ^ | |
1324 i_t3 ) >> 8 ) ^ i_t3 ) >> 5 ) & 0xff; | |
1325 if( i_t6 == i_t1 ) | |
1326 { | |
1327 break; | |
1328 } | |
1329 } | |
1330 } | |
1331 | |
1332 i_t4 = ( i_t3 >> 1 ) - 4; | |
1333 for( i_t5 = 0 ; i_t5 < 8; i_t5++ ) | |
1334 { | |
1335 if( ( ( i_t4 + i_t5 ) * 2 + 8 - ( (i_t4 + i_t5 ) & 7 ) ) | |
1336 == i_t3 ) | |
1337 { | |
1338 p_key[0] = i_try>>8; | |
1339 p_key[1] = i_try & 0xFF; | |
1340 p_key[2] = ( ( i_t4 + i_t5 ) >> 0 ) & 0xFF; | |
1341 p_key[3] = ( ( i_t4 + i_t5 ) >> 8 ) & 0xFF; | |
1342 p_key[4] = ( ( i_t4 + i_t5 ) >> 16 ) & 0xFF; | |
1343 i_exit = i_try + 1; | |
1344 } | |
1345 } | |
1346 } | |
1347 } | |
1348 | |
1349 if( i_exit >= 0 ) | |
1350 { | |
1351 p_key[0] ^= p_sector_seed[0]; | |
1352 p_key[1] ^= p_sector_seed[1]; | |
1353 p_key[2] ^= p_sector_seed[2]; | |
1354 p_key[3] ^= p_sector_seed[3]; | |
1355 p_key[4] ^= p_sector_seed[4]; | |
1356 } | |
1357 | |
1358 return i_exit; | |
1359 } | |
1360 | |
1361 | |
1362 /****************************************************************************** | |
1363 * Various pices for the title crack engine. | |
1364 ****************************************************************************** | |
1365 * The length of the PES packet is located at 0x12-0x13. | |
1366 * The the copyrigth protection bits are located at 0x14 (bits 0x20 and 0x10). | |
1367 * The data of the PES packet begins at 0x15 (if there isn't any PTS/DTS) | |
1368 * or at 0x?? if there are both PTS and DTS's. | |
1369 * The seed value used with the unscrambling key is the 5 bytes at 0x54-0x58. | |
1370 * The scrabled part of a sector begins at 0x80. | |
1371 *****************************************************************************/ | |
1372 | |
1373 /* Statistics */ | |
1374 static int i_tries = 0, i_success = 0; | |
1375 | |
1376 /***************************************************************************** | |
1377 * CrackTitleKey: try to crack title key from the contents of a VOB. | |
1378 ***************************************************************************** | |
1379 * This function is called by _dvdcss_titlekey to find a title key, if we've | |
1380 * chosen to crack title key instead of decrypting it with the disc key. | |
1381 * The DVD should have been opened and be in an authenticated state. | |
1382 * i_pos is the starting sector, i_len is the maximum number of sectors to read | |
1383 *****************************************************************************/ | |
1384 static int CrackTitleKey( dvdcss_t dvdcss, int i_pos, int i_len, | |
1385 dvd_key_t p_titlekey ) | |
1386 { | |
1387 u8 p_buf[0x800]; | |
1388 const u8 p_packstart[4] = { 0x00, 0x00, 0x01, 0xba }; | |
1389 int i_reads = 0; | |
1390 int i_encrypted = 0; | |
1391 int b_stop_scanning = 0; | |
1392 int i_ret; | |
1393 | |
1394 _dvdcss_debug( dvdcss, "cracking title key" ); | |
1395 | |
1396 i_tries = 0; | |
1397 i_success = 0; | |
1398 | |
1399 do | |
1400 { | |
8637
0211de3039eb
update libdvdcss in libmpdvdkit to latest version (1.2.4)
arpi
parents:
8123
diff
changeset
|
1401 i_ret = dvdcss->pf_seek( dvdcss, i_pos ); |
7027 | 1402 |
1403 if( i_ret != i_pos ) | |
1404 { | |
1405 _dvdcss_error( dvdcss, "seek failed" ); | |
1406 } | |
1407 | |
1408 i_ret = dvdcss_read( dvdcss, p_buf, 1, DVDCSS_NOFLAGS ); | |
1409 | |
1410 /* Either we are at the end of the physical device or the auth | |
1411 * have failed / were not done and we got a read error. */ | |
1412 if( i_ret <= 0 ) | |
1413 { | |
1414 if( i_ret == 0 ) | |
1415 { | |
1416 _dvdcss_debug( dvdcss, "read returned 0 (end of device?)" ); | |
1417 } | |
1418 break; | |
1419 } | |
1420 | |
1421 /* Stop when we find a non MPEG stream block. | |
1422 * (We must have reached the end of the stream). | |
1423 * For now, allow all blocks that begin with a start code. */ | |
1424 if( memcmp( p_buf, p_packstart, 3 ) ) | |
1425 { | |
1426 _dvdcss_debug( dvdcss, "non MPEG block found (end of title)" ); | |
1427 break; | |
1428 } | |
1429 | |
1430 if( p_buf[0x0d] & 0x07 ) | |
1431 _dvdcss_debug( dvdcss, "stuffing in pack header" ); | |
1432 | |
1433 /* PES_scrambling_control does not exist in a system_header, | |
1434 * a padding_stream or a private_stream2 (and others?). */ | |
1435 if( p_buf[0x14] & 0x30 && ! ( p_buf[0x11] == 0xbb | |
1436 || p_buf[0x11] == 0xbe | |
1437 || p_buf[0x11] == 0xbf ) ) | |
1438 { | |
1439 i_encrypted++; | |
1440 | |
1441 if( AttackPattern(p_buf, i_reads, p_titlekey) > 0 ) | |
1442 { | |
1443 b_stop_scanning = 1; | |
1444 } | |
1445 #if 0 | |
1446 if( AttackPadding(p_buf, i_reads, p_titlekey) > 0 ) | |
1447 { | |
1448 b_stop_scanning = 1; | |
1449 } | |
1450 #endif | |
1451 } | |
1452 | |
1453 i_pos++; | |
1454 i_len--; | |
1455 i_reads++; | |
1456 | |
1457 /* Emit a progress indication now and then. */ | |
1458 if( !( i_reads & 0xfff ) ) | |
1459 { | |
1460 _dvdcss_debug( dvdcss, "still cracking..." ); | |
1461 } | |
1462 | |
1463 /* Stop after 2000 blocks if we haven't seen any encrypted blocks. */ | |
1464 if( i_reads >= 2000 && i_encrypted == 0 ) break; | |
1465 | |
1466 } while( !b_stop_scanning && i_len > 0); | |
1467 | |
1468 if( i_len <= 0 ) | |
1469 _dvdcss_debug( dvdcss, "end of title reached" ); | |
1470 | |
1471 { /* Print some statistics. */ | |
1472 char psz_info[128]; | |
1473 snprintf( psz_info, sizeof(psz_info), | |
1474 "%d of %d attempts successful, %d of %d blocks scrambled", | |
1475 i_success, i_tries, i_encrypted, i_reads ); | |
1476 _dvdcss_debug( dvdcss, psz_info ); | |
1477 } | |
1478 | |
1479 if( i_success > 0 /* b_stop_scanning */ ) | |
1480 { | |
1481 _dvdcss_debug( dvdcss, "vts key initialized" ); | |
1482 return 1; | |
1483 } | |
1484 | |
7032 | 1485 if( i_encrypted == 0 && i_reads>0 ) |
7027 | 1486 { |
1487 memset( p_titlekey, 0, KEY_SIZE ); | |
1488 _dvdcss_debug( dvdcss, "file was unscrambled" ); | |
1489 return 0; | |
1490 } | |
1491 | |
1492 memset( p_titlekey, 0, KEY_SIZE ); | |
1493 return -1; | |
1494 } | |
1495 | |
1496 | |
1497 /****************************************************************************** | |
1498 * The original Ethan Hawke (DeCSSPlus) attack (modified). | |
1499 ****************************************************************************** | |
1500 * Tries to find a repeating pattern just before the encrypted part starts. | |
1501 * Then it guesses that the plain text for first encrypted bytes are | |
1502 * a contiuation of that pattern. | |
1503 *****************************************************************************/ | |
1504 static int AttackPattern( u8 const p_sec[0x800], int i_pos, u8 *p_key ) | |
1505 { | |
1506 unsigned int i_best_plen = 0; | |
1507 unsigned int i_best_p = 0; | |
1508 unsigned int i, j; | |
1509 | |
1510 /* For all cycle length from 2 to 48 */ | |
1511 for( i = 2 ; i < 0x30 ; i++ ) | |
1512 { | |
1513 /* Find the number of bytes that repeats in cycles. */ | |
1514 for( j = i + 1; | |
1515 j < 0x80 && ( p_sec[0x7F - (j%i)] == p_sec[0x7F - j] ); | |
1516 j++ ) | |
1517 { | |
1518 /* We have found j repeating bytes with a cycle length i. */ | |
1519 if( j > i_best_plen ) | |
1520 { | |
1521 i_best_plen = j; | |
1522 i_best_p = i; | |
1523 } | |
1524 } | |
1525 } | |
1526 | |
1527 /* We need at most 10 plain text bytes?, so a make sure that we | |
1528 * have at least 20 repeated bytes and that they have cycled at | |
1529 * least one time. */ | |
1530 if( ( i_best_plen > 3 ) && ( i_best_plen / i_best_p >= 2) ) | |
1531 { | |
1532 int res; | |
1533 | |
1534 i_tries++; | |
1535 memset( p_key, 0, KEY_SIZE ); | |
1536 res = RecoverTitleKey( 0, &p_sec[0x80], | |
1537 &p_sec[ 0x80 - (i_best_plen / i_best_p) * i_best_p ], | |
1538 &p_sec[0x54] /* key_seed */, p_key ); | |
1539 i_success += ( res >= 0 ); | |
1540 #if 0 | |
1541 if( res >= 0 ) | |
1542 { | |
1543 fprintf( stderr, "key is %02x:%02x:%02x:%02x:%02x ", | |
1544 p_key[0], p_key[1], p_key[2], p_key[3], p_key[4] ); | |
1545 fprintf( stderr, "at block %5d pattern len %3d period %3d %s\n", | |
1546 i_pos, i_best_plen, i_best_p, (res>=0?"y":"n") ); | |
1547 } | |
1548 #endif | |
1549 return ( res >= 0 ); | |
1550 } | |
1551 | |
1552 return 0; | |
1553 } | |
1554 | |
1555 | |
1556 #if 0 | |
1557 /****************************************************************************** | |
1558 * Encrypted Padding_stream attack. | |
1559 ****************************************************************************** | |
1560 * DVD specifies that there must only be one type of data in every sector. | |
1561 * Every sector is one pack and so must obviously be 2048 bytes long. | |
1562 * For the last pice of video data before a VOBU boundary there might not | |
1563 * be exactly the right amount of data to fill a sector. They one has to | |
1564 * pad the pack to 2048 bytes. For just a few bytes this is doen in the | |
1565 * header but for any large amount you insert a PES packet from the | |
1566 * Padding stream. This looks like 0x00 00 01 be xx xx ff ff ... | |
1567 * where xx xx is the length of the padding stream. | |
1568 *****************************************************************************/ | |
1569 static int AttackPadding( u8 const p_sec[0x800], int i_pos, u8 *p_key ) | |
1570 { | |
1571 unsigned int i_pes_length; | |
1572 /*static int i_tries = 0, i_success = 0;*/ | |
1573 | |
1574 i_pes_length = (p_sec[0x12]<<8) | p_sec[0x13]; | |
1575 | |
1576 /* Coverd by the test below but usfull for debuging. */ | |
1577 if( i_pes_length == 0x800 - 0x14 ) return 0; | |
1578 | |
1579 /* There must be room for at least 4? bytes of padding stream, | |
1580 * and it must be encrypted. | |
1581 * sector size - pack/pes header - padding startcode - padding length */ | |
1582 if( ( 0x800 - 0x14 - 4 - 2 - i_pes_length < 4 ) || | |
1583 ( p_sec[0x14 + i_pes_length + 0] == 0x00 && | |
1584 p_sec[0x14 + i_pes_length + 1] == 0x00 && | |
1585 p_sec[0x14 + i_pes_length + 2] == 0x01 ) ) | |
1586 { | |
1587 fprintf( stderr, "plain %d %02x:%02x:%02x:%02x (type %02x sub %02x)\n", | |
1588 0x800 - 0x14 - 4 - 2 - i_pes_length, | |
1589 p_sec[0x14 + i_pes_length + 0], | |
1590 p_sec[0x14 + i_pes_length + 1], | |
1591 p_sec[0x14 + i_pes_length + 2], | |
1592 p_sec[0x14 + i_pes_length + 3], | |
1593 p_sec[0x11], p_sec[0x17 + p_sec[0x16]]); | |
1594 return 0; | |
1595 } | |
1596 | |
1597 /* If we are here we know that there is a where in the pack a | |
1598 encrypted PES header is (startcode + lenght). It's never more | |
1599 than two packets in the pack, so we 'know' the length. The | |
1600 plaintext at offset (0x14 + i_pes_length) will then be | |
1601 00 00 01 e0/bd/be xx xx, in the case of be the following bytes | |
1602 are also known. */ | |
1603 | |
1604 /* An encrypted SPU PES packet with another encrypted PES packet following. | |
1605 Normaly if the following was a padding stream that would be in plain | |
1606 text. So it will be another SPU PES packet. */ | |
1607 if( p_sec[0x11] == 0xbd && | |
1608 p_sec[0x17 + p_sec[0x16]] >= 0x20 && | |
1609 p_sec[0x17 + p_sec[0x16]] <= 0x3f ) | |
1610 { | |
1611 i_tries++; | |
1612 } | |
1613 | |
1614 /* A Video PES packet with another encrypted PES packet following. | |
1615 * No reason execpt for time stamps to break the data into two packets. | |
1616 * So it's likely that the following PES packet is a padding stream. */ | |
1617 if( p_sec[0x11] == 0xe0 ) | |
1618 { | |
1619 i_tries++; | |
1620 } | |
1621 | |
1622 if( 1 ) | |
1623 { | |
1624 /*fprintf( stderr, "key is %02x:%02x:%02x:%02x:%02x ", | |
1625 p_key[0], p_key[1], p_key[2], p_key[3], p_key[4] );*/ | |
1626 fprintf( stderr, "at block %5d padding len %4d " | |
1627 "type %02x sub %02x\n", i_pos, i_pes_length, | |
1628 p_sec[0x11], p_sec[0x17 + p_sec[0x16]]); | |
1629 } | |
1630 | |
1631 return 0; | |
1632 } | |
1633 #endif |