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