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