Attempt to count descramblers used properly
[oscam.git] / module-gbox-helper.c
blobf453975b4cb7f6718f15b179cb912753af49d814
1 #define MODULE_LOG_PREFIX "gbox"
3 #include "globals.h"
5 #ifdef MODULE_GBOX
6 #include "minilzo/minilzo.h"
7 #include "oscam-string.h"
10 uint16_t gbox_get_caid(uint32_t caprovid)
12 if ((caprovid >> 24) == 0x05)
13 { return 0x0500; }
14 else
15 { return caprovid >> 16; }
18 uint32_t gbox_get_provid(uint32_t caprovid)
20 uint32_t provid = 0;
22 switch(caprovid >> 24)
24 case 0x05: // ViXS
25 provid = caprovid & 0xFFFFFF;
26 break;
28 case 0x0D: // Cryptoworx
29 provid = (caprovid >> 8) & 0xFF;
30 break;
32 default:
33 provid = caprovid & 0xFFFF;
34 break;
36 return provid;
39 uint32_t gbox_get_caprovid(uint16_t caid, uint32_t prid)
41 uint32_t caprovid = 0;
43 switch(caid >> 8)
45 case 0x05: // ViXS
46 caprovid = (caid >> 8) << 24 | (prid & 0xFFFFFF);
47 break;
49 case 0x0D: // Cryptoworks
50 caprovid = (caid >> 8) << 24 | (caid & 0xFF) << 16 | ((prid << 8) & 0xFF00);
51 break;
53 case 0x18: // Nagra
54 caprovid = (caid >> 8) << 24 | (caid & 0xFF) << 16;
55 break;
57 default:
58 caprovid = (caid >> 8) << 24 | (caid & 0xFF) << 16 | (prid & 0xFFFF);
59 break;
61 return caprovid;
64 static void gbox_convert_pw(uint8_t *password, uint32_t pw)
66 int32_t i;
67 for(i = 3; i >= 0; i--)
69 password[3 - i] = (pw >> (8 * i)) & 0xff;
73 uint32_t gbox_get_checksum(uint8_t *buf, uint16_t buflen)
75 uint8_t checksum[4];
76 int32_t counter;
78 checksum[3] = buf[0];
79 checksum[2] = buf[1];
80 checksum[1] = buf[2];
81 checksum[0] = buf[3];
83 for(counter = 1; counter < (buflen / 4) - 4; counter++)
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];
91 return checksum[3] << 24 | checksum[2] << 16 | checksum[1] << 8 | checksum[0];
94 ////////////////////////////////////////////////////////////////////////////////
95 // GBOX BUFFER ENCRYPTION/DECRYPTION (thanks to dvbcrypt@gmail.com)
96 ////////////////////////////////////////////////////////////////////////////////
98 static uint8_t Lookup_Table[0x40] =
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
106 static void gbox_encrypt8(uint8_t *buffer, uint8_t *pass)
108 int passcounter;
109 int bufcounter;
110 uint8_t temp;
112 for(passcounter = 0; passcounter < 4; passcounter++)
114 for(bufcounter = 7; bufcounter >= 0; bufcounter--)
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)];
128 static void gbox_decrypt8(uint8_t *buffer, uint8_t *pass)
130 uint8_t temp;
131 int bufcounter;
132 int passcounter;
134 for(passcounter = 3; passcounter >= 0; passcounter--)
136 for(bufcounter = 0; bufcounter <= 7; bufcounter++)
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);
152 static void gbox_decryptB(uint8_t *buffer, int bufsize, uint8_t *localkey)
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]; }
161 static void gbox_encryptB(uint8_t *buffer, int bufsize, uint8_t *key)
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);
170 static void gbox_encryptA(uint8_t *buffer, uint8_t *pass)
172 int counter;
173 uint8_t temp;
175 for(counter = 0x1F; counter >= 0; counter--)
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];
190 static void gbox_decryptA(uint8_t *buffer, uint8_t *pass)
192 int counter;
193 uint8_t temp;
195 for(counter = 0; counter <= 0x1F; counter++)
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);
210 void gbox_encrypt(uint8_t *buffer, int bufsize, uint32_t key)
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]);
218 void gbox_decrypt(uint8_t *buffer, int bufsize, uint32_t localkey)
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]);
226 void gbox_compress(uint8_t *buf, int32_t unpacked_len, int32_t *packed_len)
228 uint8_t *tmp, *tmp2;
229 lzo_voidp wrkmem;
231 if(!cs_malloc(&tmp, 0x40000))
233 return;
236 if(!cs_malloc(&tmp2, 0x40000))
238 NULLFREE(tmp);
239 return;
242 if(!cs_malloc(&wrkmem, unpacked_len * 0x1000))
244 NULLFREE(tmp);
245 NULLFREE(tmp2);
246 return;
249 unpacked_len -= 12;
250 memcpy(tmp2, buf + 12, unpacked_len);
251 lzo_init();
252 lzo_uint pl = 0;
254 if(lzo1x_1_compress(tmp2, unpacked_len, tmp, &pl, wrkmem) != LZO_E_OK)
255 { cs_log("compression failed!"); }
257 memcpy(buf + 12, tmp, pl);
258 pl += 12;
259 NULLFREE(tmp);
260 NULLFREE(tmp2);
261 NULLFREE(wrkmem);
262 *packed_len = pl;
265 void gbox_decompress(uint8_t *buf, int32_t *unpacked_len)
267 uint8_t *tmp;
268 if(!cs_malloc(&tmp, 0x40000))
269 { return; }
271 int err;
272 int len = *unpacked_len - 12;
273 *unpacked_len = 0x40000;
274 lzo_init();
275 cs_log_dbg(D_READER, "-> data decompressing %d bytes", len);
277 if((err = lzo1x_decompress_safe(buf + 12, len, tmp, (lzo_uint *)unpacked_len, NULL)) != LZO_E_OK)
278 { cs_log_dbg(D_READER, "gbox: decompression failed! errno=%d", err); }
280 memcpy(buf + 12, tmp, *unpacked_len);
281 *unpacked_len += 12;
282 NULLFREE(tmp);
284 #endif