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