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