14192
|
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 /*Notes: (OICQ uses 0x10 iterations, and modified something...)
|
|
30
|
|
31 IN : 64 bits of data in v[0] - v[1].
|
|
32 OUT: 64 bits of data in w[0] - w[1].
|
|
33 KEY: 128 bits of key in k[0] - k[3].
|
|
34
|
|
35 delta is chosen to be the real part of
|
|
36 the golden ratio: Sqrt(5/4) - 1/2 ~ 0.618034 multiplied by 2^32.
|
|
37
|
|
38 0x61C88647 is what we can track on the ASM codes.!!
|
|
39 */
|
|
40
|
|
41 #include <arpa/inet.h>
|
|
42
|
|
43 #include <string.h>
|
|
44
|
|
45 #include "crypt.h"
|
|
46 #include "debug.h"
|
|
47
|
|
48 /********************************************************************
|
|
49 * encryption
|
|
50 *******************************************************************/
|
|
51
|
14236
|
52 static void qq_encipher(guint32 *const v, const guint32 *const k, guint32 *const w)
|
14192
|
53 {
|
14236
|
54 register guint32 y = ntohl(v[0]),
|
14192
|
55 z = ntohl(v[1]),
|
|
56 a = ntohl(k[0]),
|
|
57 b = ntohl(k[1]),
|
|
58 c = ntohl(k[2]),
|
|
59 d = ntohl(k[3]),
|
|
60 n = 0x10,
|
|
61 sum = 0,
|
|
62 delta = 0x9E3779B9; /* 0x9E3779B9 - 0x100000000 = -0x61C88647 */
|
|
63
|
|
64 while (n-- > 0) {
|
|
65 sum += delta;
|
|
66 y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b);
|
|
67 z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d);
|
|
68 }
|
|
69
|
|
70 w[0] = htonl(y);
|
|
71 w[1] = htonl(z);
|
|
72 }
|
|
73
|
14236
|
74 static gint rand(void) { /* it can be the real random seed function */
|
14192
|
75 return 0xdead;
|
|
76 } /* override with number, convenient for debug */
|
|
77
|
14236
|
78 /* we encrypt every eight byte chunk */
|
|
79 static void encrypt_every_8_byte(guint8 *plain, guint8 *plain_pre_8, guint8 **crypted,
|
|
80 guint8 **crypted_pre_8, const guint8 *const key, gint *count,
|
|
81 gint *pos_in_byte, gint *is_header)
|
14192
|
82 {
|
|
83 /* prepare plain text */
|
|
84 for (*pos_in_byte = 0; *pos_in_byte < 8; (*pos_in_byte)++) {
|
|
85 if (*is_header) {
|
|
86 plain[*pos_in_byte] ^= plain_pre_8[*pos_in_byte];
|
|
87 } else {
|
|
88 plain[*pos_in_byte] ^= (*crypted_pre_8)[*pos_in_byte];
|
|
89 }
|
|
90 }
|
|
91 /* encrypt it */
|
14236
|
92 qq_encipher((guint32 *) plain, (guint32 *) key, (guint32 *) *crypted);
|
14192
|
93
|
|
94 for (*pos_in_byte = 0; *pos_in_byte < 8; (*pos_in_byte)++) {
|
|
95 (*crypted)[*pos_in_byte] ^= plain_pre_8[*pos_in_byte];
|
|
96 }
|
|
97 memcpy(plain_pre_8, plain, 8); /* prepare next */
|
|
98
|
|
99 *crypted_pre_8 = *crypted; /* store position of previous 8 byte */
|
|
100 *crypted += 8; /* prepare next output */
|
|
101 *count += 8; /* outstrlen increase by 8 */
|
|
102 *pos_in_byte = 0; /* back to start */
|
|
103 *is_header = 0; /* and exit header */
|
|
104 } /* encrypt_every_8_byte */
|
|
105
|
|
106
|
14236
|
107 static void qq_encrypt(const guint8 *const instr, gint instrlen,
|
|
108 const guint8 *const key,
|
|
109 guint8 *outstr, gint *outstrlen_prt)
|
14192
|
110 {
|
14236
|
111 guint8 plain[8], /* plain text buffer */
|
14192
|
112 plain_pre_8[8], /* plain text buffer, previous 8 bytes */
|
|
113 *crypted, /* crypted text */
|
14236
|
114 *crypted_pre_8; /* crypted test, previous 8 bytes */
|
|
115 const guint8 *inp; /* current position in instr */
|
|
116 gint pos_in_byte = 1, /* loop in the byte */
|
14192
|
117 is_header = 1, /* header is one byte */
|
|
118 count = 0, /* number of bytes being crypted */
|
|
119 padding = 0; /* number of padding stuff */
|
|
120
|
|
121 pos_in_byte = (instrlen + 0x0a) % 8; /* header padding decided by instrlen */
|
|
122 if (pos_in_byte) {
|
|
123 pos_in_byte = 8 - pos_in_byte;
|
|
124 }
|
|
125 plain[0] = (rand() & 0xf8) | pos_in_byte;
|
|
126
|
|
127 memset(plain + 1, rand() & 0xff, pos_in_byte++);
|
|
128 memset(plain_pre_8, 0x00, sizeof(plain_pre_8));
|
|
129
|
|
130 crypted = crypted_pre_8 = outstr;
|
|
131
|
|
132 padding = 1; /* pad some stuff in header */
|
|
133 while (padding <= 2) { /* at most two bytes */
|
|
134 if (pos_in_byte < 8) {
|
|
135 plain[pos_in_byte++] = rand() & 0xff;
|
|
136 padding++;
|
|
137 }
|
|
138 if (pos_in_byte == 8) {
|
14236
|
139 encrypt_every_8_byte(plain, plain_pre_8, &crypted, &crypted_pre_8,
|
|
140 key, &count, &pos_in_byte, &is_header);
|
14192
|
141 }
|
|
142 }
|
|
143
|
|
144 inp = instr;
|
|
145 while (instrlen > 0) {
|
|
146 if (pos_in_byte < 8) {
|
|
147 plain[pos_in_byte++] = *(inp++);
|
|
148 instrlen--;
|
|
149 }
|
|
150 if (pos_in_byte == 8) {
|
14236
|
151 encrypt_every_8_byte(plain, plain_pre_8, &crypted, &crypted_pre_8,
|
|
152 key, &count, &pos_in_byte, &is_header);
|
14192
|
153 }
|
|
154 }
|
|
155
|
|
156 padding = 1; /* pad some stuff in tail */
|
|
157 while (padding <= 7) { /* at most seven bytes */
|
|
158 if (pos_in_byte < 8) {
|
|
159 plain[pos_in_byte++] = 0x00;
|
|
160 padding++;
|
|
161 }
|
|
162 if (pos_in_byte == 8) {
|
14236
|
163 encrypt_every_8_byte(plain, plain_pre_8, &crypted, &crypted_pre_8,
|
|
164 key, &count, &pos_in_byte, &is_header);
|
14192
|
165 }
|
|
166 }
|
|
167
|
|
168 *outstrlen_prt = count;
|
|
169 }
|
|
170
|
|
171
|
|
172 /********************************************************************
|
|
173 * decryption
|
|
174 ********************************************************************/
|
|
175
|
14236
|
176 static void qq_decipher(guint32 *const v, const guint32 *const k, guint32 *const w)
|
14192
|
177 {
|
14236
|
178 register guint32 y = ntohl(v[0]),
|
14192
|
179 z = ntohl(v[1]),
|
|
180 a = ntohl(k[0]),
|
|
181 b = ntohl(k[1]),
|
|
182 c = ntohl(k[2]),
|
|
183 d = ntohl(k[3]),
|
|
184 n = 0x10,
|
|
185 sum = 0xE3779B90, /* why this ? must be related with n value */
|
|
186 delta = 0x9E3779B9;
|
|
187
|
|
188 /* sum = delta<<5, in general sum = delta * n */
|
|
189 while (n-- > 0) {
|
|
190 z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d);
|
|
191 y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b);
|
|
192 sum -= delta;
|
|
193 }
|
|
194
|
|
195 w[0] = htonl(y);
|
|
196 w[1] = htonl(z);
|
|
197 }
|
|
198
|
14236
|
199 static gint decrypt_every_8_byte(const guint8 **crypt_buff, const gint instrlen,
|
|
200 const guint8 *const key, gint *context_start,
|
|
201 guint8 *decrypted, gint *pos_in_byte)
|
14192
|
202 {
|
|
203 for (*pos_in_byte = 0; *pos_in_byte < 8; (*pos_in_byte)++) {
|
|
204 if (*context_start + *pos_in_byte >= instrlen)
|
|
205 return 1;
|
|
206 decrypted[*pos_in_byte] ^= (*crypt_buff)[*pos_in_byte];
|
|
207 }
|
14236
|
208 qq_decipher((guint32 *) decrypted, (guint32 *) key, (guint32 *) decrypted);
|
14192
|
209
|
|
210 *context_start += 8;
|
|
211 *crypt_buff += 8;
|
|
212 *pos_in_byte = 0;
|
|
213
|
|
214 return 1;
|
|
215 }
|
|
216
|
|
217 /* return 0 if failed, 1 otherwise */
|
14236
|
218 static gint qq_decrypt(const guint8 *const instr, gint instrlen,
|
|
219 const guint8 *const key,
|
|
220 guint8 *outstr, gint *outstrlen_ptr)
|
14192
|
221 {
|
14236
|
222 guint8 decrypted[8], m[8], *outp;
|
|
223 const guint8 *crypt_buff, *crypt_buff_pre_8;
|
|
224 gint count, context_start, pos_in_byte, padding;
|
14192
|
225
|
|
226 /* at least 16 bytes and %8 == 0 */
|
|
227 if ((instrlen % 8) || (instrlen < 16)) {
|
|
228 gaim_debug(GAIM_DEBUG_ERROR, "QQ",
|
14236
|
229 "Packet len is either too short or not a multiple of 8 bytes, read %d bytes\n",
|
|
230 instrlen);
|
14192
|
231 return 0;
|
|
232 }
|
|
233 /* get information from header */
|
14236
|
234 qq_decipher((guint32 *) instr, (guint32 *) key, (guint32 *) decrypted);
|
14192
|
235 pos_in_byte = decrypted[0] & 0x7;
|
|
236 count = instrlen - pos_in_byte - 10; /* this is the plaintext length */
|
|
237 /* return if outstr buffer is not large enough or error plaintext length */
|
|
238 if (*outstrlen_ptr < count || count < 0) {
|
14236
|
239 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Buffer len %d is less than real len %d",
|
|
240 *outstrlen_ptr, count);
|
14192
|
241 return 0;
|
|
242 }
|
|
243
|
|
244 memset(m, 0, 8);
|
|
245 crypt_buff_pre_8 = m;
|
|
246 *outstrlen_ptr = count; /* everything is ok! set return string length */
|
|
247
|
|
248 crypt_buff = instr + 8; /* address of real data start */
|
|
249 context_start = 8; /* context is at the second block of 8 bytes */
|
|
250 pos_in_byte++; /* start of paddng stuff */
|
|
251
|
|
252 padding = 1; /* at least one in header */
|
|
253 while (padding <= 2) { /* there are 2 byte padding stuff in header */
|
|
254 if (pos_in_byte < 8) { /* bypass the padding stuff, it's nonsense data */
|
|
255 pos_in_byte++;
|
|
256 padding++;
|
|
257 }
|
|
258 if (pos_in_byte == 8) {
|
|
259 crypt_buff_pre_8 = instr;
|
14236
|
260 if (!decrypt_every_8_byte(&crypt_buff, instrlen, key,
|
|
261 &context_start, decrypted, &pos_in_byte)) {
|
14192
|
262 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "decrypt every 8 bytes error A");
|
|
263 return 0;
|
|
264 }
|
|
265 }
|
|
266 }
|
|
267
|
|
268 outp = outstr;
|
|
269 while (count != 0) {
|
|
270 if (pos_in_byte < 8) {
|
|
271 *outp = crypt_buff_pre_8[pos_in_byte] ^ decrypted[pos_in_byte];
|
|
272 outp++;
|
|
273 count--;
|
|
274 pos_in_byte++;
|
|
275 }
|
|
276 if (pos_in_byte == 8) {
|
|
277 crypt_buff_pre_8 = crypt_buff - 8;
|
14236
|
278 if (!decrypt_every_8_byte(&crypt_buff, instrlen, key,
|
|
279 &context_start, decrypted, &pos_in_byte)) {
|
14192
|
280 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "decrypt every 8 bytes error B");
|
|
281 return 0;
|
|
282 }
|
|
283 }
|
|
284 }
|
|
285
|
|
286 for (padding = 1; padding < 8; padding++) {
|
|
287 if (pos_in_byte < 8) {
|
|
288 if (crypt_buff_pre_8[pos_in_byte] ^ decrypted[pos_in_byte])
|
|
289 return 0;
|
|
290 pos_in_byte++;
|
|
291 }
|
|
292 if (pos_in_byte == 8) {
|
|
293 crypt_buff_pre_8 = crypt_buff;
|
14236
|
294 if (!decrypt_every_8_byte(&crypt_buff, instrlen, key,
|
|
295 &context_start, decrypted, &pos_in_byte)) {
|
14192
|
296 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "decrypt every 8 bytes error C");
|
|
297 return 0;
|
|
298 }
|
|
299 }
|
|
300 }
|
|
301 return 1;
|
|
302 }
|
|
303
|
|
304 /* return 1 is succeed, otherwise return 0 */
|
14236
|
305 gint qq_crypt(gint flag,
|
|
306 const guint8 *const instr, gint instrlen,
|
|
307 const guint8 *const key,
|
|
308 guint8 *outstr, gint *outstrlen_ptr)
|
14192
|
309 {
|
|
310 if (flag == DECRYPT)
|
|
311 return qq_decrypt(instr, instrlen, key, outstr, outstrlen_ptr);
|
|
312 else if (flag == ENCRYPT)
|
|
313 qq_encrypt(instr, instrlen, key, outstr, outstrlen_ptr);
|
|
314 else
|
|
315 return 0;
|
|
316
|
|
317 return 1;
|
|
318 }
|