13870
|
1 /**
|
|
2 * The QQ2003C protocol plugin
|
|
3 *
|
|
4 * for gaim
|
|
5 *
|
|
6 * Copyright (C) 2004 Puzzlebird
|
|
7 *
|
|
8 * This program is free software; you can redistribute it and/or modify
|
|
9 * it under the terms of the GNU General Public License as published by
|
|
10 * the Free Software Foundation; either version 2 of the License, or
|
|
11 * (at your option) any later version.
|
|
12 *
|
|
13 * This program is distributed in the hope that it will be useful,
|
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16 * GNU General Public License for more details.
|
|
17 *
|
|
18 * You should have received a copy of the GNU General Public License
|
|
19 * along with this program; if not, write to the Free Software
|
|
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
21 *
|
|
22 *
|
|
23 * OICQ encryption algorithm
|
|
24 * Convert from ASM code provided by PerlOICQ
|
|
25 *
|
|
26 * Puzzlebird, Nov-Dec 2002
|
|
27 */
|
|
28
|
|
29 // START OF FILE
|
|
30 /*****************************************************************************/
|
|
31 /*Notes: (OICQ uses 0x10 iterations, and modified something...)
|
|
32
|
|
33 IN : 64 bits of data in v[0] - v[1].
|
|
34 OUT: 64 bits of data in w[0] - w[1].
|
|
35 KEY: 128 bits of key in k[0] - k[3].
|
|
36
|
|
37 delta is chosen to be the real part of
|
|
38 the golden ratio: Sqrt(5/4) - 1/2 ~ 0.618034 multiplied by 2^32.
|
|
39
|
|
40 0x61C88647 is what we can track on the ASM codes.!!
|
|
41 */
|
|
42
|
|
43 #ifndef _WIN32
|
|
44 #include <arpa/inet.h>
|
|
45 #else
|
|
46 #include "win32dep.h"
|
|
47 #endif
|
|
48
|
|
49 #include <string.h>
|
|
50
|
|
51 #include "crypt.h"
|
|
52 #include "debug.h" // gaim_debug, by gfhuang
|
|
53
|
|
54 /*****************************************************************************/
|
|
55 static void qq_encipher(unsigned long *const v, const unsigned long *const k, unsigned long *const w)
|
|
56 {
|
|
57 register unsigned long y = ntohl(v[0]), z = ntohl(v[1]), a = ntohl(k[0]), b = ntohl(k[1]), c = ntohl(k[2]), d = ntohl(k[3]), n = 0x10, sum = 0, delta = 0x9E3779B9; /* 0x9E3779B9 - 0x100000000 = -0x61C88647 */
|
|
58
|
|
59 while (n-- > 0) {
|
|
60 sum += delta;
|
|
61 y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b);
|
|
62 z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d);
|
|
63 } // while
|
|
64
|
|
65 w[0] = htonl(y);
|
|
66 w[1] = htonl(z);
|
|
67 } // qq_enciper
|
|
68
|
|
69 /*****************************************************************************/
|
|
70 static void qq_decipher(unsigned long *const v, const unsigned long *const k, unsigned long *const w)
|
|
71 {
|
|
72 register unsigned long y = ntohl(v[0]), z = ntohl(v[1]), a = ntohl(k[0]), b = ntohl(k[1]), c = ntohl(k[2]), d = ntohl(k[3]), n = 0x10, sum = 0xE3779B90, // why this ? must be related with n value
|
|
73 delta = 0x9E3779B9;
|
|
74
|
|
75 /* sum = delta<<5, in general sum = delta * n */
|
|
76 while (n-- > 0) {
|
|
77 z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d);
|
|
78 y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b);
|
|
79 sum -= delta;
|
|
80 }
|
|
81
|
|
82 w[0] = htonl(y);
|
|
83 w[1] = htonl(z);
|
|
84 } // qq_decipher
|
|
85
|
|
86 /********************************************************************
|
|
87 * encrypt part
|
|
88 *******************************************************************/
|
|
89
|
|
90 static void qq_encrypt(unsigned char *instr, int instrlen, unsigned char *key, unsigned char *outstr, int *outstrlen_prt)
|
|
91 {
|
|
92 unsigned char plain[8], // plain text buffer
|
|
93 plain_pre_8[8], // plain text buffer, previous 8 bytes
|
|
94 *crypted, // crypted text
|
|
95 *crypted_pre_8, // crypted test, previous 8 bytes
|
|
96 *inp; // current position in instr
|
|
97 int pos_in_byte = 1, // loop in the byte
|
|
98 is_header = 1, // header is one byte
|
|
99 count = 0, // number of bytes being crypted
|
|
100 padding = 0; // number of padding stuff
|
|
101
|
|
102 int rand(void) { // it can be the real random seed function
|
|
103 return 0xdead;
|
|
104 } // override with number, convenient for debug
|
|
105
|
|
106 /*** we encrypt every eight byte ***/
|
|
107 void encrypt_every_8_byte(void) {
|
|
108 for (pos_in_byte = 0; pos_in_byte < 8; pos_in_byte++) {
|
|
109 if (is_header) {
|
|
110 plain[pos_in_byte] ^= plain_pre_8[pos_in_byte];
|
|
111 } else {
|
|
112 plain[pos_in_byte] ^= crypted_pre_8[pos_in_byte];
|
|
113 }
|
|
114 } // prepare plain text
|
|
115 qq_encipher((unsigned long *) plain, (unsigned long *) key, (unsigned long *) crypted); // encrypt it
|
|
116
|
|
117 for (pos_in_byte = 0; pos_in_byte < 8; pos_in_byte++) {
|
|
118 crypted[pos_in_byte] ^= plain_pre_8[pos_in_byte];
|
|
119 }
|
|
120 memcpy(plain_pre_8, plain, 8); // prepare next
|
|
121
|
|
122 crypted_pre_8 = crypted; // store position of previous 8 byte
|
|
123 crypted += 8; // prepare next output
|
|
124 count += 8; // outstrlen increase by 8
|
|
125 pos_in_byte = 0; // back to start
|
|
126 is_header = 0; // and exit header
|
|
127 } // encrypt_every_8_byte
|
|
128
|
|
129 pos_in_byte = (instrlen + 0x0a) % 8; // header padding decided by instrlen
|
|
130 if (pos_in_byte) {
|
|
131 pos_in_byte = 8 - pos_in_byte;
|
|
132 }
|
|
133 plain[0] = (rand() & 0xf8) | pos_in_byte;
|
|
134
|
|
135 memset(plain + 1, rand() & 0xff, pos_in_byte++);
|
|
136 memset(plain_pre_8, 0x00, sizeof(plain_pre_8));
|
|
137
|
|
138 crypted = crypted_pre_8 = outstr;
|
|
139
|
|
140 padding = 1; // pad some stuff in header
|
|
141 while (padding <= 2) { // at most two byte
|
|
142 if (pos_in_byte < 8) {
|
|
143 plain[pos_in_byte++] = rand() & 0xff;
|
|
144 padding++;
|
|
145 }
|
|
146 if (pos_in_byte == 8) {
|
|
147 encrypt_every_8_byte();
|
|
148 }
|
|
149 }
|
|
150
|
|
151 inp = instr;
|
|
152 while (instrlen > 0) {
|
|
153 if (pos_in_byte < 8) {
|
|
154 plain[pos_in_byte++] = *(inp++);
|
|
155 instrlen--;
|
|
156 }
|
|
157 if (pos_in_byte == 8) {
|
|
158 encrypt_every_8_byte();
|
|
159 }
|
|
160 }
|
|
161
|
|
162 padding = 1; // pad some stuff in tailer
|
|
163 while (padding <= 7) { // at most sever byte
|
|
164 if (pos_in_byte < 8) {
|
|
165 plain[pos_in_byte++] = 0x00;
|
|
166 padding++;
|
|
167 }
|
|
168 if (pos_in_byte == 8) {
|
|
169 encrypt_every_8_byte();
|
|
170 }
|
|
171 }
|
|
172
|
|
173 *outstrlen_prt = count;
|
|
174 } // qq_encrypt
|
|
175
|
|
176
|
|
177 /********************************************************************
|
|
178 * [decrypt part]
|
|
179 * return 0 if failed, otherwise return 1
|
|
180 ********************************************************************/
|
|
181
|
|
182 static int qq_decrypt(unsigned char *instr, int instrlen, unsigned char *key, unsigned char *outstr, int *outstrlen_ptr)
|
|
183 {
|
|
184 unsigned char decrypted[8], m[8], *crypt_buff, *crypt_buff_pre_8, *outp;
|
|
185 int count, context_start, pos_in_byte, padding;
|
|
186
|
|
187 int decrypt_every_8_byte(void) {
|
|
188 for (pos_in_byte = 0; pos_in_byte < 8; pos_in_byte++) {
|
|
189 if (context_start + pos_in_byte >= instrlen)
|
|
190 return 1;
|
|
191 decrypted[pos_in_byte] ^= crypt_buff[pos_in_byte];
|
|
192 }
|
|
193 qq_decipher((unsigned long *) decrypted, (unsigned long *) key, (unsigned long *) decrypted);
|
|
194
|
|
195 context_start += 8;
|
|
196 crypt_buff += 8;
|
|
197 pos_in_byte = 0;
|
|
198 return 1;
|
|
199 } // decrypt_every_8_byte
|
|
200
|
|
201 // at least 16 bytes and %8 == 0
|
|
202 if ((instrlen % 8) || (instrlen < 16)) {
|
|
203 //debug info by gfhuang
|
|
204 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Packet len is not times of 8 bytes, read %d bytes\n", instrlen);
|
|
205 return 0;
|
|
206 }
|
|
207 // get information from header
|
|
208 qq_decipher((unsigned long *) instr, (unsigned long *) key, (unsigned long *) decrypted);
|
|
209 pos_in_byte = decrypted[0] & 0x7;
|
|
210 count = instrlen - pos_in_byte - 10; // this is the plaintext length
|
|
211 // return if outstr buffer is not large enought or error plaintext length
|
|
212 if (*outstrlen_ptr < count || count < 0) {
|
|
213 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Buffer len %d is less than real len %d", *outstrlen_ptr, count);
|
|
214 return 0;
|
|
215 }
|
|
216
|
|
217 memset(m, 0, 8);
|
|
218 crypt_buff_pre_8 = m;
|
|
219 *outstrlen_ptr = count; // everything is ok! set return string length
|
|
220
|
|
221 crypt_buff = instr + 8; // address of real data start
|
|
222 context_start = 8; // context is at the second 8 byte
|
|
223 pos_in_byte++; // start of paddng stuff
|
|
224
|
|
225 padding = 1; // at least one in header
|
|
226 while (padding <= 2) { // there are 2 byte padding stuff in header
|
|
227 if (pos_in_byte < 8) { // bypass the padding stuff, none sense data
|
|
228 pos_in_byte++;
|
|
229 padding++;
|
|
230 }
|
|
231 if (pos_in_byte == 8) {
|
|
232 crypt_buff_pre_8 = instr;
|
|
233 if (!decrypt_every_8_byte()) {
|
|
234 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "decrypt every 8 bytes error A");
|
|
235 return 0;
|
|
236 }
|
|
237 }
|
|
238 } // while
|
|
239
|
|
240 outp = outstr;
|
|
241 while (count != 0) {
|
|
242 if (pos_in_byte < 8) {
|
|
243 *outp = crypt_buff_pre_8[pos_in_byte] ^ decrypted[pos_in_byte];
|
|
244 outp++;
|
|
245 count--;
|
|
246 pos_in_byte++;
|
|
247 }
|
|
248 if (pos_in_byte == 8) {
|
|
249 crypt_buff_pre_8 = crypt_buff - 8;
|
|
250 if (!decrypt_every_8_byte()) {
|
|
251 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "decrypt every 8 bytes error B");
|
|
252 return 0;
|
|
253 }
|
|
254 }
|
|
255 } // while
|
|
256
|
|
257 for (padding = 1; padding < 8; padding++) {
|
|
258 if (pos_in_byte < 8) {
|
|
259 if (crypt_buff_pre_8[pos_in_byte] ^ decrypted[pos_in_byte])
|
|
260 return 0;
|
|
261 pos_in_byte++;
|
|
262 }
|
|
263 if (pos_in_byte == 8) {
|
|
264 crypt_buff_pre_8 = crypt_buff;
|
|
265 if (!decrypt_every_8_byte()) {
|
|
266 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "decrypt every 8 bytes error C");
|
|
267 return 0;
|
|
268 }
|
|
269 }
|
|
270 } // for
|
|
271 return 1;
|
|
272 } // qq_decrypt
|
|
273
|
|
274 /*****************************************************************************/
|
|
275 /* This is the Public Function */
|
|
276 // return 1 is succeed, otherwise return 0
|
|
277 int qq_crypt(unsigned char flag,
|
|
278 unsigned char *instr, int instrlen, unsigned char *key, unsigned char *outstr, int *outstrlen_ptr)
|
|
279 {
|
|
280 if (flag == DECRYPT)
|
|
281 return qq_decrypt(instr, instrlen, key, outstr, outstrlen_ptr);
|
|
282 else if (flag == ENCRYPT)
|
|
283 qq_encrypt(instr, instrlen, key, outstr, outstrlen_ptr);
|
|
284
|
|
285 return 1; // flag must be DECRYPT or ENCRYPT
|
|
286 } // qq_crypt
|
|
287
|
|
288 /*****************************************************************************/
|
|
289 // END OF FILE
|