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