annotate libdvdcss/css.c @ 31716:dcd1791cabb3

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