1 | #define MODULE_LOG_PREFIX "gbox"
|
---|
2 |
|
---|
3 | #include "globals.h"
|
---|
4 |
|
---|
5 | #ifdef MODULE_GBOX
|
---|
6 | #include "minilzo/minilzo.h"
|
---|
7 | #include "oscam-string.h"
|
---|
8 |
|
---|
9 |
|
---|
10 | uint16_t gbox_get_caid(uint32_t caprovid)
|
---|
11 | {
|
---|
12 | if ((caprovid >> 24) == 0x05)
|
---|
13 | { return 0x0500; }
|
---|
14 | else
|
---|
15 | { return caprovid >> 16; }
|
---|
16 | }
|
---|
17 |
|
---|
18 | uint32_t gbox_get_provid(uint32_t caprovid)
|
---|
19 | {
|
---|
20 | uint32_t provid = 0;
|
---|
21 |
|
---|
22 | switch(caprovid >> 24)
|
---|
23 | {
|
---|
24 | case 0x05: // ViXS
|
---|
25 | provid = caprovid & 0xFFFFFF;
|
---|
26 | break;
|
---|
27 |
|
---|
28 | case 0x0D: // Cryptoworx
|
---|
29 | provid = (caprovid >> 8) & 0xFF;
|
---|
30 | break;
|
---|
31 |
|
---|
32 | default:
|
---|
33 | provid = caprovid & 0xFFFF;
|
---|
34 | break;
|
---|
35 | }
|
---|
36 | return provid;
|
---|
37 | }
|
---|
38 |
|
---|
39 | uint32_t gbox_get_caprovid(uint16_t caid, uint32_t prid)
|
---|
40 | {
|
---|
41 | uint32_t caprovid = 0;
|
---|
42 |
|
---|
43 | switch(caid >> 8)
|
---|
44 | {
|
---|
45 | case 0x05: // ViXS
|
---|
46 | caprovid = (caid >> 8) << 24 | (prid & 0xFFFFFF);
|
---|
47 | break;
|
---|
48 |
|
---|
49 | case 0x0D: // Cryptoworks
|
---|
50 | caprovid = (caid >> 8) << 24 | (caid & 0xFF) << 16 | ((prid << 8) & 0xFF00);
|
---|
51 | break;
|
---|
52 |
|
---|
53 | case 0x18: // Nagra
|
---|
54 | caprovid = (caid >> 8) << 24 | (caid & 0xFF) << 16;
|
---|
55 | break;
|
---|
56 |
|
---|
57 | default:
|
---|
58 | caprovid = (caid >> 8) << 24 | (caid & 0xFF) << 16 | (prid & 0xFFFF);
|
---|
59 | break;
|
---|
60 | }
|
---|
61 | return caprovid;
|
---|
62 | }
|
---|
63 |
|
---|
64 | static void gbox_convert_pw(uint8_t *password, uint32_t pw)
|
---|
65 | {
|
---|
66 | int32_t i;
|
---|
67 | for(i = 3; i >= 0; i--)
|
---|
68 | {
|
---|
69 | password[3 - i] = (pw >> (8 * i)) & 0xff;
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | uint32_t gbox_get_checksum(uint8_t *buf, uint16_t buflen)
|
---|
74 | {
|
---|
75 | uint8_t checksum[4];
|
---|
76 | int32_t counter;
|
---|
77 |
|
---|
78 | checksum[3] = buf[0];
|
---|
79 | checksum[2] = buf[1];
|
---|
80 | checksum[1] = buf[2];
|
---|
81 | checksum[0] = buf[3];
|
---|
82 |
|
---|
83 | for(counter = 1; counter < (buflen / 4) - 4; counter++)
|
---|
84 | {
|
---|
85 | checksum[3] ^= buf[counter * 4];
|
---|
86 | checksum[2] ^= buf[counter * 4 + 1];
|
---|
87 | checksum[1] ^= buf[counter * 4 + 2];
|
---|
88 | checksum[0] ^= buf[counter * 4 + 3];
|
---|
89 | }
|
---|
90 |
|
---|
91 | return checksum[3] << 24 | checksum[2] << 16 | checksum[1] << 8 | checksum[0];
|
---|
92 | }
|
---|
93 |
|
---|
94 | ////////////////////////////////////////////////////////////////////////////////
|
---|
95 | // GBOX BUFFER ENCRYPTION/DECRYPTION (thanks to dvbcrypt@gmail.com)
|
---|
96 | ////////////////////////////////////////////////////////////////////////////////
|
---|
97 |
|
---|
98 | static uint8_t Lookup_Table[0x40] =
|
---|
99 | {
|
---|
100 | 0x25, 0x38, 0xD4, 0xCD, 0x17, 0x7A, 0x5E, 0x6C, 0x52, 0x42, 0xFE, 0x68, 0xAB, 0x3F, 0xF7, 0xBE,
|
---|
101 | 0x47, 0x57, 0x71, 0xB0, 0x23, 0xC1, 0x26, 0x6C, 0x41, 0xCE, 0x94, 0x37, 0x45, 0x04, 0xA2, 0xEA,
|
---|
102 | 0x07, 0x58, 0x35, 0x55, 0x08, 0x2A, 0x0F, 0xE7, 0xAC, 0x76, 0xF0, 0xC1, 0xE6, 0x09, 0x10, 0xDD,
|
---|
103 | 0xC5, 0x8D, 0x2E, 0xD9, 0x03, 0x9C, 0x3D, 0x2C, 0x4D, 0x41, 0x0C, 0x5E, 0xDE, 0xE4, 0x90, 0xAE
|
---|
104 | };
|
---|
105 |
|
---|
106 | static void gbox_encrypt8(uint8_t *buffer, uint8_t *pass)
|
---|
107 | {
|
---|
108 | int passcounter;
|
---|
109 | int bufcounter;
|
---|
110 | uint8_t temp;
|
---|
111 |
|
---|
112 | for(passcounter = 0; passcounter < 4; passcounter++)
|
---|
113 | {
|
---|
114 | for(bufcounter = 7; bufcounter >= 0; bufcounter--)
|
---|
115 | {
|
---|
116 | temp = pass[3];
|
---|
117 | pass[3] = (pass[3] / 2) + (pass[2] & 1) * 0x80;
|
---|
118 | pass[2] = (pass[2] / 2) + (pass[1] & 1) * 0x80;
|
---|
119 | pass[1] = (pass[1] / 2) + (pass[0] & 1) * 0x80;
|
---|
120 | pass[0] = (pass[0] / 2) + (temp & 1) * 0x80;
|
---|
121 | buffer[(bufcounter + 1) & 7] = buffer[(bufcounter + 1) & 7 ] - Lookup_Table[(buffer[bufcounter] >> 2) & 0x3F ];
|
---|
122 | buffer[(bufcounter + 1) & 7] = Lookup_Table[(buffer[bufcounter] - pass[(bufcounter + 1) & 3]) & 0x3F ] ^ buffer[(bufcounter + 1) & 7 ];
|
---|
123 | buffer[(bufcounter + 1) & 7] = buffer[(bufcounter + 1) & 7 ] - pass[(bufcounter & 3)];
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | static void gbox_decrypt8(uint8_t *buffer, uint8_t *pass)
|
---|
129 | {
|
---|
130 | uint8_t temp;
|
---|
131 | int bufcounter;
|
---|
132 | int passcounter;
|
---|
133 |
|
---|
134 | for(passcounter = 3; passcounter >= 0; passcounter--)
|
---|
135 | {
|
---|
136 | for(bufcounter = 0; bufcounter <= 7; bufcounter++)
|
---|
137 | {
|
---|
138 | buffer[(bufcounter + 1) & 7] = pass[bufcounter & 3] + buffer[(bufcounter + 1) & 7];
|
---|
139 | temp = buffer[bufcounter] - pass[(bufcounter + 1) & 3];
|
---|
140 | buffer[(bufcounter + 1) & 7] = Lookup_Table[temp & 0x3F] ^ buffer[(bufcounter + 1) & 7];
|
---|
141 | temp = buffer[bufcounter] >> 2;
|
---|
142 | buffer[(bufcounter + 1) & 7] = Lookup_Table[temp & 0x3F] + buffer[(bufcounter + 1) & 7];
|
---|
143 | temp = pass[0] & 0x80;
|
---|
144 | pass[0] = ((pass[1] & 0x80) >> 7) + (pass[0] << 1);
|
---|
145 | pass[1] = ((pass[2] & 0x80) >> 7) + (pass[1] << 1);
|
---|
146 | pass[2] = ((pass[3] & 0x80) >> 7) + (pass[2] << 1);
|
---|
147 | pass[3] = (temp >> 7) + (pass[3] << 1);
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | static void gbox_decryptB(uint8_t *buffer, int bufsize, uint8_t *localkey)
|
---|
153 | {
|
---|
154 | int counter;
|
---|
155 | gbox_encrypt8(&buffer[bufsize - 9], localkey);
|
---|
156 | gbox_decrypt8(buffer, localkey);
|
---|
157 | for(counter = bufsize - 2; counter >= 0; counter--)
|
---|
158 | { buffer[counter] = buffer[counter + 1] ^ buffer[counter]; }
|
---|
159 | }
|
---|
160 |
|
---|
161 | static void gbox_encryptB(uint8_t *buffer, int bufsize, uint8_t *key)
|
---|
162 | {
|
---|
163 | int counter;
|
---|
164 | for(counter = 0; counter < (bufsize - 1); counter++)
|
---|
165 | { buffer[counter] = buffer[counter + 1] ^ buffer[counter]; }
|
---|
166 | gbox_encrypt8(buffer, key);
|
---|
167 | gbox_decrypt8(&buffer[bufsize - 9], key);
|
---|
168 | }
|
---|
169 |
|
---|
170 | static void gbox_encryptA(uint8_t *buffer, uint8_t *pass)
|
---|
171 | {
|
---|
172 | int counter;
|
---|
173 | uint8_t temp;
|
---|
174 |
|
---|
175 | for(counter = 0x1F; counter >= 0; counter--)
|
---|
176 | {
|
---|
177 | temp = pass[3] & 1;
|
---|
178 | pass[3] = ((pass[2] & 1) << 7) + (pass[3] >> 1);
|
---|
179 | pass[2] = ((pass[1] & 1) << 7) + (pass[2] >> 1);
|
---|
180 | pass[1] = ((pass[0] & 1) << 7) + (pass[1] >> 1);
|
---|
181 | pass[0] = (temp << 7) + (pass[0] >> 1);
|
---|
182 | temp = (pass[(counter + 1) & 3] ^ buffer[counter & 7]) >> 2;
|
---|
183 | buffer[(counter + 1) & 7] = Lookup_Table[temp & 0x3F] * 2 + buffer[(counter + 1) & 7 ];
|
---|
184 | temp = buffer[counter & 7] - pass[(counter + 1) & 3];
|
---|
185 | buffer[(counter + 1) & 7] = Lookup_Table[temp & 0x3F] ^ buffer[(counter + 1) & 7];
|
---|
186 | buffer[(counter + 1) & 7] = pass[counter & 3] + buffer[(counter + 1) & 7];
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | static void gbox_decryptA(uint8_t *buffer, uint8_t *pass)
|
---|
191 | {
|
---|
192 | int counter;
|
---|
193 | uint8_t temp;
|
---|
194 |
|
---|
195 | for(counter = 0; counter <= 0x1F; counter++)
|
---|
196 | {
|
---|
197 | buffer[(counter + 1) & 7] = buffer[(counter + 1) & 7] - pass[counter & 3];
|
---|
198 | temp = buffer[counter & 7] - pass[(counter + 1) & 3];
|
---|
199 | buffer[(counter + 1) & 7] = Lookup_Table[temp & 0x3F] ^ buffer[(counter + 1) & 7];
|
---|
200 | temp = (pass[(counter + 1) & 3] ^ buffer[counter & 7]) >> 2;
|
---|
201 | buffer[(counter + 1) & 7] = buffer[(counter + 1) & 7] - Lookup_Table[temp & 0x3F] * 2;
|
---|
202 | temp = pass[0] & 0x80;
|
---|
203 | pass[0] = ((pass[1] & 0x80) >> 7) + (pass[0] << 1);
|
---|
204 | pass[1] = ((pass[2] & 0x80) >> 7) + (pass[1] << 1);
|
---|
205 | pass[2] = ((pass[3] & 0x80) >> 7) + (pass[2] << 1);
|
---|
206 | pass[3] = (temp >> 7) + (pass[3] << 1);
|
---|
207 | }
|
---|
208 | }
|
---|
209 |
|
---|
210 | void gbox_encrypt(uint8_t *buffer, int bufsize, uint32_t key)
|
---|
211 | {
|
---|
212 | uint8_t pass[4];
|
---|
213 | gbox_convert_pw(&pass[0], key);
|
---|
214 | gbox_encryptA(buffer, &pass[0]);
|
---|
215 | gbox_encryptB(buffer, bufsize, &pass[0]);
|
---|
216 | }
|
---|
217 |
|
---|
218 | void gbox_decrypt(uint8_t *buffer, int bufsize, uint32_t localkey)
|
---|
219 | {
|
---|
220 | uint8_t pass[4];
|
---|
221 | gbox_convert_pw(&pass[0], localkey);
|
---|
222 | gbox_decryptB(buffer, bufsize, &pass[0]);
|
---|
223 | gbox_decryptA(buffer, &pass[0]);
|
---|
224 | }
|
---|
225 |
|
---|
226 | void gbox_compress(uint8_t *buf, int32_t unpacked_len, int32_t *packed_len)
|
---|
227 | {
|
---|
228 | uint8_t *tmp, *tmp2;
|
---|
229 | lzo_voidp wrkmem;
|
---|
230 |
|
---|
231 | if(!cs_malloc(&tmp, 0x40000))
|
---|
232 | {
|
---|
233 | return;
|
---|
234 | }
|
---|
235 |
|
---|
236 | if(!cs_malloc(&tmp2, 0x40000))
|
---|
237 | {
|
---|
238 | NULLFREE(tmp);
|
---|
239 | return;
|
---|
240 | }
|
---|
241 |
|
---|
242 | if(!cs_malloc(&wrkmem, unpacked_len * 0x1000))
|
---|
243 | {
|
---|
244 | NULLFREE(tmp);
|
---|
245 | NULLFREE(tmp2);
|
---|
246 | return;
|
---|
247 | }
|
---|
248 |
|
---|
249 | unpacked_len -= 12;
|
---|
250 | memcpy(tmp2, buf + 12, unpacked_len);
|
---|
251 | lzo_init();
|
---|
252 | lzo_uint pl = 0;
|
---|
253 |
|
---|
254 | if(lzo1x_1_compress(tmp2, unpacked_len, tmp, &pl, wrkmem) != LZO_E_OK)
|
---|
255 | { cs_log("compression failed!"); }
|
---|
256 |
|
---|
257 | memcpy(buf + 12, tmp, pl);
|
---|
258 | pl += 12;
|
---|
259 | NULLFREE(tmp);
|
---|
260 | NULLFREE(tmp2);
|
---|
261 | NULLFREE(wrkmem);
|
---|
262 | *packed_len = pl;
|
---|
263 | }
|
---|
264 |
|
---|
265 | void gbox_decompress(uint8_t *buf, int32_t *unpacked_len)
|
---|
266 | {
|
---|
267 | uint8_t *tmp;
|
---|
268 | if(!cs_malloc(&tmp, 0x40000))
|
---|
269 | { return; }
|
---|
270 |
|
---|
271 | int err;
|
---|
272 | int len = *unpacked_len - 12;
|
---|
273 | *unpacked_len = 0x40000;
|
---|
274 | lzo_init();
|
---|
275 |
|
---|
276 | if((err = lzo1x_decompress_safe(buf + 12, len, tmp, (lzo_uint *)unpacked_len, NULL)) != LZO_E_OK)
|
---|
277 | { cs_log_dbg(D_READER, "gbox: decompression failed! errno=%d", err); }
|
---|
278 |
|
---|
279 | memcpy(buf + 12, tmp, *unpacked_len);
|
---|
280 | *unpacked_len += 12;
|
---|
281 | NULLFREE(tmp);
|
---|
282 | }
|
---|
283 | #endif
|
---|