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
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.
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.
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
25
* QQ encryption algorithm
26
* Convert from ASM code provided by PerlOICQ
28
* Puzzlebird, Nov-Dec 2002
31
/* Notes: (QQ uses 16 rounds, and modified something...)
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].
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.
40
0x61C88647 is what we can track on the ASM codes.!!
49
void show_binary(char *psztitle, const guint8 *const buffer, gint bytes)
51
printf("== %s %d ==\r\n", psztitle, bytes);
53
for (i = 0; i < bytes; i += 16) {
58
for (j = 0; j < 16; j++) {
63
printf(" %02x", buffer[i + j]);
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)
81
printf("========\r\n");
85
#define show_binary(args... ) /* nothing */
89
/********************************************************************
91
*******************************************************************/
93
/* Tiny Encryption Algorithm (TEA) */
94
static inline void qq_encipher(guint32 *const v, const guint32 *const k, guint32 *const w)
105
delta = 0x9E3779B9; /* 0x9E3779B9 - 0x100000000 = -0x61C88647 */
109
y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b);
110
z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d);
117
/* it can be the real random seed function */
118
/* override with number, convenient for debug */
120
static gint crypt_rand(void) {
125
#define crypt_rand() rand()
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)
131
/* ships in encipher */
135
guint32 crypted32[2];
141
/* prepare at first */
142
crypted_ptr = crypted;
144
memcpy(crypted32, crypted_ptr, sizeof(crypted32));
145
c32_prev[0] = crypted32[0]; c32_prev[1] = crypted32[1];
147
p32_prev[0] = 0; p32_prev[1] = 0;
148
plain32[0] = crypted32[0] ^ p32_prev[0]; plain32[1] = crypted32[1] ^ p32_prev[1];
150
g_memmove(key32, key, 16);
151
count64 = crypted_len / 8;
152
while (count64-- > 0){
154
qq_encipher(plain32, key32, crypted32);
156
crypted32[0] ^= p32_prev[0]; crypted32[1] ^= p32_prev[1];
158
/* store curr 64 bits crypted */
159
g_memmove(crypted_ptr, crypted32, sizeof(crypted32));
162
p32_prev[0] = plain32[0]; p32_prev[1] = plain32[1];
163
c32_prev[0] = crypted32[0]; c32_prev[1] = crypted32[1];
165
/* set next 64 bits want to crypt*/
168
memcpy(crypted32, crypted_ptr, sizeof(crypted32));
169
plain32[0] = crypted32[0] ^ c32_prev[0]; plain32[1] = crypted32[1] ^ c32_prev[1];
174
/* length of crypted buffer must be plain_len + 17*/
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
180
gint qq_encrypt(guint8* crypted, const guint8* const plain, const gint plain_len, const guint8* const key)
182
guint8 *crypted_ptr = crypted; /* current position of dest */
185
padding = (plain_len + 10) % 8;
187
padding = 8 - padding;
192
/* set first byte as padding len */
193
crypted_ptr[pos] = (rand() & 0xf8) | padding;
200
memset(crypted_ptr + pos, rand() & 0xff, padding);
206
crypted_ptr[pos++] = rand() & 0xff;
209
g_memmove(crypted_ptr + pos, plain, plain_len);
212
/* header padding len + plain len must be multiple of 8
213
* tail pading len is always 8 - (1st byte)
215
memset(crypted_ptr + pos, 0x00, 7);
218
show_binary("After padding", crypted, pos);
220
encrypt_out(crypted, pos, key);
222
show_binary("Encrypted", crypted, pos);
226
/********************************************************************
228
********************************************************************/
230
static inline void qq_decipher(guint32 *const v, const guint32 *const k, guint32 *const w)
240
sum = 0xE3779B90, /* why this ? must be related with n value */
243
/* sum = delta<<5, in general sum = delta * n */
245
z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d);
246
y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b);
254
static inline gint decrypt_out(guint8 *dest, gint crypted_len, const guint8* const key)
258
guint32 crypted32[2];
264
guint8 *crypted_ptr = dest;
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];
271
qq_decipher(crypted32, key32, p32_prev);
272
memcpy(crypted_ptr, p32_prev, sizeof(p32_prev));
274
/* check padding len */
275
padding = 2 + (crypted_ptr[0] & 0x7);
279
plain_len = crypted_len - 1 - padding - 7;
280
if( plain_len < 0 ) {
284
count64 = crypted_len / 8;
285
while (--count64 > 0){
286
c32_prev[0] = crypted32[0]; c32_prev[1] = crypted32[1];
289
memcpy(crypted32, crypted_ptr, sizeof(crypted32));
290
p32_prev[0] ^= crypted32[0]; p32_prev[1] ^= crypted32[1];
292
qq_decipher(p32_prev, key32, p32_prev);
294
plain32[0] = p32_prev[0] ^ c32_prev[0]; plain32[1] = p32_prev[1] ^ c32_prev[1];
295
memcpy(crypted_ptr, plain32, sizeof(plain32));
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)
308
/* at least 16 bytes and %8 == 0 */
309
if ((crypted_len % 8) || (crypted_len < 16)) {
313
memcpy(plain, crypted, crypted_len);
315
plain_len = decrypt_out(plain, crypted_len, key);
317
return plain_len; /* invalid first 64 bits */
320
show_binary("Decrypted with padding", plain, crypted_len);
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) {
328
if (plain_len == 0) {
332
hdr_padding = crypted_len - plain_len - 7;
333
g_memmove(plain, plain + hdr_padding, plain_len);