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