1 #define MODULE_LOG_PREFIX "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)
15 { return caprovid
>> 16; }
18 uint32_t gbox_get_provid(uint32_t caprovid
)
22 switch(caprovid
>> 24)
26 provid
= caprovid
& 0xFFFFFF;
30 provid
= (caprovid
>> 8) & 0xFF;
33 provid
= caprovid
& 0xFFFF;
39 uint32_t gbox_get_caprovid(uint16_t caid
, uint32_t prid
)
41 uint32_t caprovid
= 0;
47 caprovid
= (caid
>> 8) << 24 | (prid
& 0xFFFFFF);
51 caprovid
= (caid
>> 8) << 24 | (caid
& 0xFF) << 16 |
52 ((prid
<< 8) & 0xFF00);
56 caprovid
= (caid
>> 8) << 24 | (caid
& 0xFF) << 16;
59 caprovid
= (caid
>> 8) << 24 | (caid
& 0xFF) << 16 |
66 static void gbox_convert_pw(uchar
*password
, uint32_t pw
)
69 for(i
= 3; i
>= 0; i
--)
71 password
[3 - i
] = (pw
>> (8 * i
)) & 0xff;
75 uint32_t gbox_get_checksum(uchar
*buf
, uint16_t buflen
)
85 for(counter
= 1; counter
< (buflen
/ 4) - 4; counter
++)
87 checksum
[3] ^= buf
[counter
* 4];
88 checksum
[2] ^= buf
[counter
* 4 + 1];
89 checksum
[1] ^= buf
[counter
* 4 + 2];
90 checksum
[0] ^= buf
[counter
* 4 + 3];
93 return checksum
[3] << 24 | checksum
[2] << 16 | checksum
[1] << 8 | checksum
[0];
96 ////////////////////////////////////////////////////////////////////////////////
97 // GBOX BUFFER ENCRYPTION/DECRYPTION (thanks to dvbcrypt@gmail.com)
98 ////////////////////////////////////////////////////////////////////////////////
99 static unsigned char Lookup_Table
[0x40] =
101 0x25, 0x38, 0xD4, 0xCD, 0x17, 0x7A, 0x5E, 0x6C, 0x52, 0x42, 0xFE, 0x68, 0xAB, 0x3F, 0xF7, 0xBE,
102 0x47, 0x57, 0x71, 0xB0, 0x23, 0xC1, 0x26, 0x6C, 0x41, 0xCE, 0x94, 0x37, 0x45, 0x04, 0xA2, 0xEA,
103 0x07, 0x58, 0x35, 0x55, 0x08, 0x2A, 0x0F, 0xE7, 0xAC, 0x76, 0xF0, 0xC1, 0xE6, 0x09, 0x10, 0xDD,
104 0xC5, 0x8D, 0x2E, 0xD9, 0x03, 0x9C, 0x3D, 0x2C, 0x4D, 0x41, 0x0C, 0x5E, 0xDE, 0xE4, 0x90, 0xAE
107 static void gbox_encrypt8(unsigned char *buffer
, unsigned char *pass
)
112 for(passcounter
= 0; passcounter
< 4; passcounter
++)
113 for(bufcounter
= 7; bufcounter
>= 0; bufcounter
--)
116 pass
[3] = (pass
[3] / 2) + (pass
[2] & 1) * 0x80;
117 pass
[2] = (pass
[2] / 2) + (pass
[1] & 1) * 0x80;
118 pass
[1] = (pass
[1] / 2) + (pass
[0] & 1) * 0x80;
119 pass
[0] = (pass
[0] / 2) + (temp
& 1) * 0x80;
120 buffer
[(bufcounter
+ 1) & 7] = buffer
[(bufcounter
+ 1) & 7 ] - Lookup_Table
[(buffer
[bufcounter
] >> 2) & 0x3F ];
121 buffer
[(bufcounter
+ 1) & 7] = Lookup_Table
[(buffer
[bufcounter
] - pass
[(bufcounter
+ 1) & 3]) & 0x3F ] ^ buffer
[(bufcounter
+ 1) & 7 ];
122 buffer
[(bufcounter
+ 1) & 7] = buffer
[(bufcounter
+ 1) & 7 ] - pass
[(bufcounter
& 3)];
126 static void gbox_decrypt8(unsigned char *buffer
, unsigned char *pass
)
131 for(passcounter
= 3; passcounter
>= 0; passcounter
--)
132 for(bufcounter
= 0; bufcounter
<= 7; bufcounter
++)
134 buffer
[(bufcounter
+ 1) & 7] = pass
[bufcounter
& 3] + buffer
[(bufcounter
+ 1) & 7];
135 temp
= buffer
[bufcounter
] - pass
[(bufcounter
+ 1) & 3];
136 buffer
[(bufcounter
+ 1) & 7] = Lookup_Table
[temp
& 0x3F] ^ buffer
[(bufcounter
+ 1) & 7];
137 temp
= buffer
[bufcounter
] >> 2;
138 buffer
[(bufcounter
+ 1) & 7] = Lookup_Table
[temp
& 0x3F] + buffer
[(bufcounter
+ 1) & 7];
139 temp
= pass
[0] & 0x80;
140 pass
[0] = ((pass
[1] & 0x80) >> 7) + (pass
[0] << 1);
141 pass
[1] = ((pass
[2] & 0x80) >> 7) + (pass
[1] << 1);
142 pass
[2] = ((pass
[3] & 0x80) >> 7) + (pass
[2] << 1);
143 pass
[3] = (temp
>> 7) + (pass
[3] << 1);
147 static void gbox_decryptB(unsigned char *buffer
, int bufsize
, uchar
*localkey
)
150 gbox_encrypt8(&buffer
[bufsize
- 9], localkey
);
151 gbox_decrypt8(buffer
, localkey
);
152 for(counter
= bufsize
- 2; counter
>= 0; counter
--)
153 { buffer
[counter
] = buffer
[counter
+ 1] ^ buffer
[counter
]; }
156 static void gbox_encryptB(unsigned char *buffer
, int bufsize
, uchar
*key
)
159 for(counter
= 0; counter
< (bufsize
- 1); counter
++)
160 { buffer
[counter
] = buffer
[counter
+ 1] ^ buffer
[counter
]; }
161 gbox_encrypt8(buffer
, key
);
162 gbox_decrypt8(&buffer
[bufsize
- 9], key
);
165 static void gbox_encryptA(unsigned char *buffer
, unsigned char *pass
)
169 for(counter
= 0x1F; counter
>= 0; counter
--)
172 pass
[3] = ((pass
[2] & 1) << 7) + (pass
[3] >> 1);
173 pass
[2] = ((pass
[1] & 1) << 7) + (pass
[2] >> 1);
174 pass
[1] = ((pass
[0] & 1) << 7) + (pass
[1] >> 1);
175 pass
[0] = (temp
<< 7) + (pass
[0] >> 1);
176 temp
= (pass
[(counter
+ 1) & 3] ^ buffer
[counter
& 7]) >> 2;
177 buffer
[(counter
+ 1) & 7] = Lookup_Table
[temp
& 0x3F] * 2 + buffer
[(counter
+ 1) & 7 ];
178 temp
= buffer
[counter
& 7] - pass
[(counter
+ 1) & 3];
179 buffer
[(counter
+ 1) & 7] = Lookup_Table
[temp
& 0x3F] ^ buffer
[(counter
+ 1) & 7];
180 buffer
[(counter
+ 1) & 7] = pass
[counter
& 3] + buffer
[(counter
+ 1) & 7];
184 static void gbox_decryptA(unsigned char *buffer
, unsigned char *pass
)
188 for(counter
= 0; counter
<= 0x1F; counter
++)
190 buffer
[(counter
+ 1) & 7] = buffer
[(counter
+ 1) & 7] - pass
[counter
& 3];
191 temp
= buffer
[counter
& 7] - pass
[(counter
+ 1) & 3];
192 buffer
[(counter
+ 1) & 7] = Lookup_Table
[temp
& 0x3F] ^ buffer
[(counter
+ 1) & 7];
193 temp
= (pass
[(counter
+ 1) & 3] ^ buffer
[counter
& 7]) >> 2;
194 buffer
[(counter
+ 1) & 7] = buffer
[(counter
+ 1) & 7] - Lookup_Table
[temp
& 0x3F] * 2;
195 temp
= pass
[0] & 0x80;
196 pass
[0] = ((pass
[1] & 0x80) >> 7) + (pass
[0] << 1);
197 pass
[1] = ((pass
[2] & 0x80) >> 7) + (pass
[1] << 1);
198 pass
[2] = ((pass
[3] & 0x80) >> 7) + (pass
[2] << 1);
199 pass
[3] = (temp
>> 7) + (pass
[3] << 1);
203 void gbox_encrypt(uchar
*buffer
, int bufsize
, uint32_t key
)
206 gbox_convert_pw(&pass
[0], key
);
207 gbox_encryptA(buffer
, &pass
[0]);
208 gbox_encryptB(buffer
, bufsize
, &pass
[0]);
211 void gbox_decrypt(uchar
*buffer
, int bufsize
, uint32_t localkey
)
214 gbox_convert_pw(&pass
[0], localkey
);
215 gbox_decryptB(buffer
, bufsize
, &pass
[0]);
216 gbox_decryptA(buffer
, &pass
[0]);
219 void gbox_compress(uchar
*buf
, int32_t unpacked_len
, int32_t *packed_len
)
221 unsigned char *tmp
, *tmp2
;
223 if(!cs_malloc(&tmp
, 0x40000))
227 if(!cs_malloc(&tmp2
, 0x40000))
232 if(!cs_malloc(&wrkmem
, unpacked_len
* 0x1000))
239 memcpy(tmp2
, buf
+ 12, unpacked_len
);
242 if(lzo1x_1_compress(tmp2
, unpacked_len
, tmp
, &pl
, wrkmem
) != LZO_E_OK
)
243 { cs_log("compression failed!"); }
244 memcpy(buf
+ 12, tmp
, pl
);
252 void gbox_decompress(uchar
*buf
, int32_t *unpacked_len
)
255 if(!cs_malloc(&tmp
, 0x40000))
258 int len
= *unpacked_len
- 12;
259 *unpacked_len
= 0x40000;
261 cs_log_dbg(D_READER
, "-> data decompressing %d bytes", len
);
262 if((err
= lzo1x_decompress_safe(buf
+ 12, len
, tmp
, (lzo_uint
*)unpacked_len
, NULL
)) != LZO_E_OK
)
263 { cs_log_dbg(D_READER
, "gbox: decompression failed! errno=%d", err
); }
264 memcpy(buf
+ 12, tmp
, *unpacked_len
);