~ubuntu-branches/ubuntu/quantal/pidgin/quantal

« back to all changes in this revision

Viewing changes to libpurple/protocols/qq/qq_crypt.c

  • Committer: Bazaar Package Importer
  • Author(s): Ari Pollak
  • Date: 2011-06-13 21:17:20 UTC
  • mfrom: (1.3.19 upstream)
  • mto: This revision was merged to the branch mainline in revision 73.
  • Revision ID: james.westby@ubuntu.com-20110613211720-ke87vzmdcuaxams7
Tags: 2.8.0-1
* Imported Upstream version 2.8.0 (Closes: #630124)
* Remove SILC support since the library will be orphaned (Closes: #629222)
* Fix typo in libpurple-bin description (Closes: #625462)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 * @file qq_crypt.c
3
 
 *
4
 
 * purple
5
 
 *
6
 
 * Purple 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,  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 "debug.h"
46
 
#include "qq_crypt.h"
47
 
 
48
 
#if 0
49
 
void show_binary(char *psztitle, const guint8 *const buffer, gint bytes)
50
 
{
51
 
        printf("== %s %d ==\r\n", psztitle, bytes);
52
 
        gint i, j, ch;
53
 
        for (i = 0; i < bytes; i += 16) {
54
 
                /* length label */
55
 
                printf("%07x: ", i);
56
 
 
57
 
                /* dump hex value */
58
 
                for (j = 0; j < 16; j++) {
59
 
                        if (j == 8) {
60
 
                                printf(" -");
61
 
                        }
62
 
                        if ((i + j) < bytes)
63
 
                                printf(" %02x", buffer[i + j]);
64
 
                        else
65
 
                                printf("   ");
66
 
                }
67
 
 
68
 
                printf("  ");
69
 
 
70
 
 
71
 
                /* dump ascii value */
72
 
                for (j = 0; j < 16 && (i + j) < bytes; j++) {
73
 
                        ch = buffer[i + j] & 127;
74
 
                        if (ch < ' ' || ch == 127)
75
 
                                printf(".");
76
 
                        else
77
 
                                printf("%c", ch);
78
 
                }
79
 
                printf("\r\n");
80
 
        }
81
 
        printf("========\r\n");
82
 
}
83
 
#else
84
 
 
85
 
#define show_binary(args... )           /* nothing */
86
 
 
87
 
#endif
88
 
 
89
 
/********************************************************************
90
 
 * encryption
91
 
 *******************************************************************/
92
 
 
93
 
/* Tiny Encryption Algorithm (TEA) */
94
 
static inline void qq_encipher(guint32 *const v, const guint32 *const k, guint32 *const w)
95
 
{
96
 
        register guint32
97
 
                y = g_ntohl(v[0]),
98
 
                 z = g_ntohl(v[1]),
99
 
                 a = g_ntohl(k[0]),
100
 
                 b = g_ntohl(k[1]),
101
 
                 c = g_ntohl(k[2]),
102
 
                 d = g_ntohl(k[3]),
103
 
                 n = 0x10,
104
 
                 sum = 0,
105
 
                 delta = 0x9E3779B9;    /*  0x9E3779B9 - 0x100000000 = -0x61C88647 */
106
 
 
107
 
        while (n-- > 0) {
108
 
                sum += delta;
109
 
                y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b);
110
 
                z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d);
111
 
        }
112
 
 
113
 
        w[0] = g_htonl(y);
114
 
        w[1] = g_htonl(z);
115
 
}
116
 
 
117
 
/* it can be the real random seed function */
118
 
/* override with number, convenient for debug */
119
 
#ifdef DEBUG
120
 
static gint crypt_rand(void) {
121
 
        return 0xdead;
122
 
}
123
 
#else
124
 
#include <stdlib.h>
125
 
#define crypt_rand() rand()
126
 
#endif
127
 
 
128
 
/* 64-bit blocks and some kind of feedback mode of operation */
129
 
static inline void encrypt_out(guint8 *crypted, const gint crypted_len, const guint8 *key)
130
 
{
131
 
        /* ships in encipher */
132
 
        guint32 plain32[2];
133
 
        guint32 p32_prev[2];
134
 
        guint32 key32[4];
135
 
        guint32 crypted32[2];
136
 
        guint32 c32_prev[2];
137
 
 
138
 
        guint8 *crypted_ptr;
139
 
        gint count64;
140
 
 
141
 
        /* prepare at first */
142
 
        crypted_ptr = crypted;
143
 
 
144
 
        memcpy(crypted32, crypted_ptr, sizeof(crypted32));
145
 
        c32_prev[0] = crypted32[0]; c32_prev[1] = crypted32[1];
146
 
 
147
 
        p32_prev[0] = 0; p32_prev[1] = 0;
148
 
        plain32[0] = crypted32[0] ^ p32_prev[0]; plain32[1] = crypted32[1] ^ p32_prev[1];
149
 
 
150
 
        g_memmove(key32, key, 16);
151
 
        count64 = crypted_len / 8;
152
 
        while (count64-- > 0){
153
 
                /* encrypt it */
154
 
                qq_encipher(plain32, key32, crypted32);
155
 
 
156
 
                crypted32[0] ^= p32_prev[0]; crypted32[1] ^= p32_prev[1];
157
 
 
158
 
                /* store curr 64 bits crypted */
159
 
                g_memmove(crypted_ptr, crypted32, sizeof(crypted32));
160
 
 
161
 
                /* set prev */
162
 
                p32_prev[0] = plain32[0]; p32_prev[1] = plain32[1];
163
 
                c32_prev[0] = crypted32[0]; c32_prev[1] = crypted32[1];
164
 
 
165
 
                /* set next 64 bits want to crypt*/
166
 
                if (count64 > 0) {
167
 
                        crypted_ptr += 8;
168
 
                        memcpy(crypted32, crypted_ptr, sizeof(crypted32));
169
 
                        plain32[0] = crypted32[0] ^ c32_prev[0]; plain32[1] = crypted32[1] ^ c32_prev[1];
170
 
                }
171
 
        }
172
 
}
173
 
 
174
 
/* length of crypted buffer must be plain_len + 17*/
175
 
/*
176
 
 * The above comment used to say "plain_len + 16", but based on the
177
 
 * behavior of the function that is wrong.  If you give this function
178
 
 * a plain string with len%8 = 7 then the returned length is len+17
179
 
 */
180
 
gint qq_encrypt(guint8* crypted, const guint8* const plain, const gint plain_len, const guint8* const key)
181
 
{
182
 
        guint8 *crypted_ptr = crypted;          /* current position of dest */
183
 
        gint pos, padding;
184
 
 
185
 
        padding = (plain_len + 10) % 8;
186
 
        if (padding) {
187
 
                padding = 8 - padding;
188
 
        }
189
 
 
190
 
        pos = 0;
191
 
 
192
 
        /* set first byte as padding len */
193
 
        crypted_ptr[pos] = (rand() & 0xf8) | padding;
194
 
        pos++;
195
 
 
196
 
        /* extra 2 bytes */
197
 
        padding += 2;
198
 
 
199
 
        /* faster a little
200
 
        memset(crypted_ptr + pos, rand() & 0xff, padding);
201
 
        pos += padding;
202
 
        */
203
 
 
204
 
        /* more random */
205
 
        while (padding--) {
206
 
                crypted_ptr[pos++] = rand() & 0xff;
207
 
        }
208
 
 
209
 
        g_memmove(crypted_ptr + pos, plain, plain_len);
210
 
        pos += plain_len;
211
 
 
212
 
        /* header padding len + plain len must be multiple of 8
213
 
         * tail pading len is always 8 - (1st byte)
214
 
         */
215
 
        memset(crypted_ptr + pos, 0x00, 7);
216
 
        pos += 7;
217
 
 
218
 
        show_binary("After padding", crypted, pos);
219
 
 
220
 
        encrypt_out(crypted, pos, key);
221
 
 
222
 
        show_binary("Encrypted", crypted, pos);
223
 
        return pos;
224
 
}
225
 
 
226
 
/********************************************************************
227
 
 * decryption
228
 
 ********************************************************************/
229
 
 
230
 
static inline void qq_decipher(guint32 *const v, const guint32 *const k, guint32 *const w)
231
 
{
232
 
        register guint32
233
 
                y = g_ntohl(v[0]),
234
 
                z = g_ntohl(v[1]),
235
 
                a = g_ntohl(k[0]),
236
 
                b = g_ntohl(k[1]),
237
 
                c = g_ntohl(k[2]),
238
 
                d = g_ntohl(k[3]),
239
 
                n = 0x10,
240
 
                sum = 0xE3779B90,       /* why this ? must be related with n value */
241
 
                delta = 0x9E3779B9;
242
 
 
243
 
        /* sum = delta<<5, in general sum = delta * n */
244
 
        while (n-- > 0) {
245
 
                z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d);
246
 
                y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b);
247
 
                sum -= delta;
248
 
        }
249
 
 
250
 
        w[0] = g_htonl(y);
251
 
        w[1] = g_htonl(z);
252
 
}
253
 
 
254
 
static inline gint decrypt_out(guint8 *dest, gint crypted_len, const guint8* const key)
255
 
{
256
 
        gint plain_len;
257
 
        guint32 key32[4];
258
 
        guint32 crypted32[2];
259
 
        guint32 c32_prev[2];
260
 
        guint32 plain32[2];
261
 
        guint32 p32_prev[2];
262
 
        gint count64;
263
 
        gint padding;
264
 
        guint8 *crypted_ptr = dest;
265
 
 
266
 
        /* decrypt first 64 bit */
267
 
        memcpy(key32, key, sizeof(key32));
268
 
        memcpy(crypted32, crypted_ptr, sizeof(crypted32));
269
 
        c32_prev[0] = crypted32[0]; c32_prev[1] = crypted32[1];
270
 
 
271
 
        qq_decipher(crypted32, key32, p32_prev);
272
 
        memcpy(crypted_ptr, p32_prev, sizeof(p32_prev));
273
 
 
274
 
        /* check padding len */
275
 
        padding = 2 + (crypted_ptr[0] & 0x7);
276
 
        if (padding < 2) {
277
 
                padding += 8;
278
 
        }
279
 
        plain_len = crypted_len - 1 - padding - 7;
280
 
        if( plain_len < 0 )     {
281
 
                return -2;
282
 
        }
283
 
 
284
 
        count64 = crypted_len / 8;
285
 
        while (--count64 > 0){
286
 
                c32_prev[0] = crypted32[0]; c32_prev[1] = crypted32[1];
287
 
                crypted_ptr += 8;
288
 
 
289
 
                memcpy(crypted32, crypted_ptr, sizeof(crypted32));
290
 
                p32_prev[0] ^= crypted32[0]; p32_prev[1] ^= crypted32[1];
291
 
 
292
 
                qq_decipher(p32_prev, key32, p32_prev);
293
 
 
294
 
                plain32[0] = p32_prev[0] ^ c32_prev[0]; plain32[1] = p32_prev[1] ^ c32_prev[1];
295
 
                memcpy(crypted_ptr, plain32, sizeof(plain32));
296
 
        }
297
 
 
298
 
        return plain_len;
299
 
}
300
 
 
301
 
/* length of plain buffer must be equal to crypted_len */
302
 
gint qq_decrypt(guint8 *plain, const guint8* const crypted, const gint crypted_len, const guint8* const key)
303
 
{
304
 
        gint plain_len = 0;
305
 
        gint hdr_padding;
306
 
        gint pos;
307
 
 
308
 
        /* at least 16 bytes and %8 == 0 */
309
 
        if ((crypted_len % 8) || (crypted_len < 16)) {
310
 
                return -1;
311
 
        }
312
 
 
313
 
        memcpy(plain, crypted, crypted_len);
314
 
 
315
 
        plain_len = decrypt_out(plain, crypted_len, key);
316
 
        if (plain_len < 0) {
317
 
                return plain_len;       /* invalid first 64 bits */
318
 
        }
319
 
 
320
 
        show_binary("Decrypted with padding", plain, crypted_len);
321
 
 
322
 
        /* check last 7 bytes is zero or not? */
323
 
        for (pos = crypted_len - 1; pos > crypted_len - 8; pos--) {
324
 
                if (plain[pos] != 0) {
325
 
                        return -3;
326
 
                }
327
 
        }
328
 
        if (plain_len == 0) {
329
 
                return plain_len;
330
 
        }
331
 
 
332
 
        hdr_padding = crypted_len - plain_len - 7;
333
 
        g_memmove(plain, plain + hdr_padding, plain_len);
334
 
 
335
 
        return plain_len;
336
 
}
337