comparison libgaim/protocols/qq/crypt.c @ 14192:60b1bc8dbf37

[gaim-migrate @ 16863] Renamed 'core' to 'libgaim' committer: Tailor Script <tailor@pidgin.im>
author Evan Schoenberg <evan.s@dreskin.net>
date Sat, 19 Aug 2006 01:50:10 +0000
parents
children b7f17fdded6f
comparison
equal deleted inserted replaced
14191:009db0b357b5 14192:60b1bc8dbf37
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"
46 #include "debug.h"
47
48 /********************************************************************
49 * encryption
50 *******************************************************************/
51
52 /* TODO: convert these data types to proper glib ones */
53 static void qq_encipher(unsigned long *const v, const unsigned long *const k, unsigned long *const w)
54 {
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 */
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);
73 }
74
75 static int rand(void) { /* it can be the real random seed function */
76 return 0xdead;
77 } /* override with number, convenient for debug */
78
79 /* we encrypt every eight byte block */
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 {
83 /* prepare plain text */
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 }
91 /* encrypt it */
92 qq_encipher((unsigned long *) plain, (unsigned long *) key, (unsigned long *) *crypted);
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 }
97 memcpy(plain_pre_8, plain, 8); /* prepare next */
98
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 */
105
106
107 static void qq_encrypt(unsigned char *instr, int instrlen, unsigned char *key,
108 unsigned char *outstr, int *outstrlen_prt)
109 {
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 */
119
120 pos_in_byte = (instrlen + 0x0a) % 8; /* header padding decided by instrlen */
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
131 padding = 1; /* pad some stuff in header */
132 while (padding <= 2) { /* at most two bytes */
133 if (pos_in_byte < 8) {
134 plain[pos_in_byte++] = rand() & 0xff;
135 padding++;
136 }
137 if (pos_in_byte == 8) {
138 encrypt_every_8_byte(plain, plain_pre_8, &crypted, &crypted_pre_8, key, &count, &pos_in_byte, &is_header);
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) {
149 encrypt_every_8_byte(plain, plain_pre_8, &crypted, &crypted_pre_8, key, &count, &pos_in_byte, &is_header);
150 }
151 }
152
153 padding = 1; /* pad some stuff in tail */
154 while (padding <= 7) { /* at most seven bytes */
155 if (pos_in_byte < 8) {
156 plain[pos_in_byte++] = 0x00;
157 padding++;
158 }
159 if (pos_in_byte == 8) {
160 encrypt_every_8_byte(plain, plain_pre_8, &crypted, &crypted_pre_8, key, &count, &pos_in_byte, &is_header);
161 }
162 }
163
164 *outstrlen_prt = count;
165 }
166
167
168 /********************************************************************
169 * decryption
170 ********************************************************************/
171
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,
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] = 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
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)
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
219 /* at least 16 bytes and %8 == 0 */
220 if ((instrlen % 8) || (instrlen < 16)) {
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);
223 return 0;
224 }
225 /* get information from header */
226 qq_decipher((unsigned long *) instr, (unsigned long *) key, (unsigned long *) decrypted);
227 pos_in_byte = decrypted[0] & 0x7;
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 */
230 if (*outstrlen_ptr < count || count < 0) {
231 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Buffer len %d is less than real len %d", *outstrlen_ptr, count);
232 return 0;
233 }
234
235 memset(m, 0, 8);
236 crypt_buff_pre_8 = m;
237 *outstrlen_ptr = count; /* everything is ok! set return string length */
238
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 */
242
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 */
246 pos_in_byte++;
247 padding++;
248 }
249 if (pos_in_byte == 8) {
250 crypt_buff_pre_8 = instr;
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");
253 return 0;
254 }
255 }
256 }
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;
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");
270 return 0;
271 }
272 }
273 }
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;
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");
285 return 0;
286 }
287 }
288 }
289 return 1;
290 }
291
292 /* This is the Public Function */
293 /* return 1 is succeed, otherwise return 0 */
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);
301 else
302 return 0;
303
304 return 1;
305 }