comparison libpurple/protocols/qq/crypt.c @ 15373:5fe8042783c1

Rename gtk/ and libgaim/ to pidgin/ and libpurple/
author Sean Egan <seanegan@gmail.com>
date Sat, 20 Jan 2007 02:32:10 +0000
parents
children 32c366eeeb99
comparison
equal deleted inserted replaced
15372:f79e0f4df793 15373:5fe8042783c1
1 /**
2 * @file crypt.c
3 *
4 * gaim
5 *
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.
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 *
25 * QQ encryption algorithm
26 * Convert from ASM code provided by PerlOICQ
27 *
28 * Puzzlebird, Nov-Dec 2002
29 */
30
31 /*Notes: (QQ uses 16 rounds, and modified something...)
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
52 /* Tiny Encryption Algorithm (TEA) */
53 static void qq_encipher(guint32 *const v, const guint32 *const k, guint32 *const w)
54 {
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]),
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
71 w[0] = g_htonl(y);
72 w[1] = g_htonl(z);
73 }
74
75 static gint rand(void) { /* it can be the real random seed function */
76 return 0xdead;
77 } /* override with number, convenient for debug */
78
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,
81 guint8 **crypted_pre_8, const guint8 *const key, gint *count,
82 gint *pos_in_block, gint *is_header)
83 {
84 /* prepare input text */
85 if (!*is_header)
86 *(guint64 *) plain ^= **(guint64 **) crypted_pre_8;
87
88 /* encrypt it */
89 qq_encipher((guint32 *) plain, (guint32 *) key, (guint32 *) *crypted);
90
91 **(guint64 **) crypted ^= *(guint64 *) plain_pre_8;
92
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 */
98 *pos_in_block = 0; /* back to start */
99 *is_header = 0; /* and exit header */
100 } /* encrypt_block */
101
102 static void qq_encrypt(const guint8 *const instr, gint instrlen,
103 const guint8 *const key,
104 guint8 *outstr, gint *outstrlen_ptr)
105 {
106 guint8 plain[8], /* plain text buffer */
107 plain_pre_8[8], /* plain text buffer, previous 8 bytes */
108 *crypted, /* crypted text */
109 *crypted_pre_8; /* crypted text, previous 8 bytes */
110 const guint8 *inp; /* current position in instr */
111 gint pos_in_block = 1, /* loop in the byte */
112 is_header = 1, /* header is one byte */
113 count = 0, /* number of bytes being crypted */
114 padding = 0; /* number of padding stuff */
115
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;
119
120 /* initialization vector */
121 plain[0] = (rand() & 0xf8) | pos_in_block;
122 memset(plain + 1, rand() & 0xff, pos_in_block++);
123
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 */
130 if (pos_in_block < 8) {
131 plain[pos_in_block++] = rand() & 0xff;
132 padding++;
133 }
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);
137 }
138 }
139
140 inp = instr;
141 while (instrlen > 0) {
142 if (pos_in_block < 8) {
143 plain[pos_in_block++] = *(inp++);
144 instrlen--;
145 }
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);
149 }
150 }
151
152 padding = 1; /* pad some stuff in tail */
153 while (padding <= 7) { /* at most seven bytes */
154 if (pos_in_block < 8) {
155 plain[pos_in_block++] = 0x00;
156 padding++;
157 }
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);
161 }
162 }
163
164 *outstrlen_ptr = count;
165 }
166
167
168 /********************************************************************
169 * decryption
170 ********************************************************************/
171
172 static void qq_decipher(guint32 *const v, const guint32 *const k, guint32 *const w)
173 {
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]),
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
191 w[0] = g_htonl(y);
192 w[1] = g_htonl(z);
193 }
194
195 static gint decrypt_block(const guint8 **crypt_buff, const gint instrlen,
196 const guint8 *const key, gint *context_start,
197 guint8 *decrypted, gint *pos_in_block)
198 {
199 if (*context_start == instrlen)
200 return 1;
201
202 *(guint64 *) decrypted ^= **(guint64 **) crypt_buff;
203
204 qq_decipher((guint32 *) decrypted, (guint32 *) key, (guint32 *) decrypted);
205
206 *context_start += 8;
207 *crypt_buff += 8;
208 *pos_in_block = 0;
209
210 return 1;
211 }
212
213 /* return 0 if failed, 1 otherwise */
214 static gint qq_decrypt(const guint8 *const instr, gint instrlen,
215 const guint8 *const key,
216 guint8 *outstr, gint *outstrlen_ptr)
217 {
218 guint8 decrypted[8], m[8], *outp;
219 const guint8 *crypt_buff, *crypt_buff_pre_8;
220 gint count, context_start, pos_in_block, padding;
221
222 /* at least 16 bytes and %8 == 0 */
223 if ((instrlen % 8) || (instrlen < 16)) {
224 gaim_debug(GAIM_DEBUG_ERROR, "QQ",
225 "Ciphertext len is either too short or not a multiple of 8 bytes, read %d bytes\n",
226 instrlen);
227 return 0;
228 }
229 /* get information from header */
230 qq_decipher((guint32 *) instr, (guint32 *) key, (guint32 *) decrypted);
231 pos_in_block = decrypted[0] & 0x7;
232 count = instrlen - pos_in_block - 10; /* this is the plaintext length */
233 /* return if outstr buffer is not large enough or error plaintext length */
234 if (*outstrlen_ptr < count || count < 0) {
235 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Buffer len %d is less than real len %d",
236 *outstrlen_ptr, count);
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 */
246 pos_in_block++; /* start of paddng stuff */
247
248 padding = 1; /* at least one in header */
249 while (padding <= 2) { /* there are 2 byte padding stuff in header */
250 if (pos_in_block < 8) { /* bypass the padding stuff, it's nonsense data */
251 pos_in_block++;
252 padding++;
253 }
254 if (pos_in_block == 8) {
255 crypt_buff_pre_8 = instr;
256 if (!decrypt_block(&crypt_buff, instrlen, key,
257 &context_start, decrypted, &pos_in_block)) {
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) {
266 if (pos_in_block < 8) {
267 *outp = crypt_buff_pre_8[pos_in_block] ^ decrypted[pos_in_block];
268 outp++;
269 count--;
270 pos_in_block++;
271 }
272 if (pos_in_block == 8) {
273 crypt_buff_pre_8 = crypt_buff - 8;
274 if (!decrypt_block(&crypt_buff, instrlen, key,
275 &context_start, decrypted, &pos_in_block)) {
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++) {
283 if (pos_in_block < 8) {
284 if (crypt_buff_pre_8[pos_in_block] ^ decrypted[pos_in_block])
285 return 0;
286 pos_in_block++;
287 }
288 if (pos_in_block == 8) {
289 crypt_buff_pre_8 = crypt_buff;
290 if (!decrypt_block(&crypt_buff, instrlen, key,
291 &context_start, decrypted, &pos_in_block)) {
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 */
301 gint qq_crypt(gint flag,
302 const guint8 *const instr, gint instrlen,
303 const guint8 *const key,
304 guint8 *outstr, gint *outstrlen_ptr)
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 }