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