changeset 14565:ca943d7fb274

[gaim-migrate @ 17289] This is easier to read and slightly more efficient. committer: Tailor Script <tailor@pidgin.im>
author Mark Huetsch <markhuetsch>
date Sat, 16 Sep 2006 20:16:47 +0000
parents 4c14862f7fcc
children d2de045b077f
files libgaim/protocols/qq/crypt.c
diffstat 1 files changed, 70 insertions(+), 74 deletions(-) [+]
line wrap: on
line diff
--- a/libgaim/protocols/qq/crypt.c	Sat Sep 16 05:38:33 2006 +0000
+++ b/libgaim/protocols/qq/crypt.c	Sat Sep 16 20:16:47 2006 +0000
@@ -20,13 +20,13 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  *
- * OICQ encryption algorithm
+ * QQ encryption algorithm
  * Convert from ASM code provided by PerlOICQ
  * 
  * Puzzlebird, Nov-Dec 2002
  */
 
-/*Notes: (OICQ uses 0x10 iterations, and modified something...)
+/*Notes: (QQ uses 16 rounds, and modified something...)
 
 IN : 64  bits of data in v[0] - v[1].
 OUT: 64  bits of data in w[0] - w[1].
@@ -53,6 +53,7 @@
  * encryption 
  *******************************************************************/
 
+/* Tiny Encryption Algorithm (TEA) */
 static void qq_encipher(guint32 *const v, const guint32 *const k, guint32 *const w)
 {
 	register guint32 y = ntohl(v[0]), 
@@ -79,97 +80,92 @@
 	return 0xdead;
 }			/* override with number, convenient for debug */
 
-/* we encrypt every eight byte chunk */
-static void encrypt_every_8_byte(guint8 *plain, guint8 *plain_pre_8, guint8 **crypted, 
+/* 64-bit blocks and some kind of feedback mode of operation */
+static void encrypt_block(guint8 *plain, guint8 *plain_pre_8, guint8 **crypted, 
 		guint8 **crypted_pre_8, const guint8 *const key, gint *count, 
-		gint *pos_in_byte, gint *is_header) 
+		gint *pos_in_block, gint *is_header) 
 {
-	/* prepare plain text */
-	for (*pos_in_byte = 0; *pos_in_byte < 8; (*pos_in_byte)++) {
-		if (*is_header) {
-			plain[*pos_in_byte] ^= plain_pre_8[*pos_in_byte];
-		} else {
-			plain[*pos_in_byte] ^= (*crypted_pre_8)[*pos_in_byte];
-		}
-	}
+	/* prepare input text */
+	if (!*is_header)
+		*(guint64 *) plain ^= **(guint64 **) crypted_pre_8;
+
 	/* encrypt it */
 	qq_encipher((guint32 *) plain, (guint32 *) key, (guint32 *) *crypted);
 
-	for (*pos_in_byte = 0; *pos_in_byte < 8; (*pos_in_byte)++) {
-		(*crypted)[*pos_in_byte] ^= plain_pre_8[*pos_in_byte];
-	}
+	**(guint64 **) crypted ^= *(guint64 *) plain_pre_8;
+
 	memcpy(plain_pre_8, plain, 8);	/* prepare next */
 
 	*crypted_pre_8 = *crypted;	/* store position of previous 8 byte */
 	*crypted += 8;			/* prepare next output */
 	*count += 8;			/* outstrlen increase by 8 */
-	*pos_in_byte = 0;		/* back to start */
+	*pos_in_block = 0;		/* back to start */
 	*is_header = 0;			/* and exit header */
-}					/* encrypt_every_8_byte */
-
+}					/* encrypt_block */
 
 static void qq_encrypt(const guint8 *const instr, gint instrlen, 
 		const guint8 *const key, 
-		guint8 *outstr, gint *outstrlen_prt)
+		guint8 *outstr, gint *outstrlen_ptr)
 {
 	guint8 plain[8],		/* plain text buffer */
 		plain_pre_8[8],		/* plain text buffer, previous 8 bytes */
 		*crypted,		/* crypted text */
-		*crypted_pre_8;		/* crypted test, previous 8 bytes */
+		*crypted_pre_8;		/* crypted text, previous 8 bytes */
 	const guint8 *inp;		/* current position in instr */
-	gint pos_in_byte = 1,		/* loop in the byte */
+	gint pos_in_block = 1,		/* loop in the byte */
 		is_header = 1,		/* header is one byte */
 		count = 0,		/* number of bytes being crypted */
 		padding = 0;		/* number of padding stuff */
 
-	pos_in_byte = (instrlen + 0x0a) % 8;	/* header padding decided by instrlen */
-	if (pos_in_byte) {
-		pos_in_byte = 8 - pos_in_byte;
-	}
-	plain[0] = (rand() & 0xf8) | pos_in_byte;
+	pos_in_block = (instrlen + 0x0a) % 8;	/* header padding decided by instrlen */
+	if (pos_in_block)
+		pos_in_block = 8 - pos_in_block;
 
-	memset(plain + 1, rand() & 0xff, pos_in_byte++);
+	/* initialization vector */
+	plain[0] = (rand() & 0xf8) | pos_in_block;
+	memset(plain + 1, rand() & 0xff, pos_in_block++);
+
 	memset(plain_pre_8, 0x00, sizeof(plain_pre_8));
 
 	crypted = crypted_pre_8 = outstr;
 
 	padding = 1;		/* pad some stuff in header */
 	while (padding <= 2) {	/* at most two bytes */
-		if (pos_in_byte < 8) {
-			plain[pos_in_byte++] = rand() & 0xff;
+		if (pos_in_block < 8) {
+			plain[pos_in_block++] = rand() & 0xff;
 			padding++;
 		}
-		if (pos_in_byte == 8) {
-			encrypt_every_8_byte(plain, plain_pre_8, &crypted, &crypted_pre_8, 
-					key, &count, &pos_in_byte, &is_header);
+		if (pos_in_block == 8) {
+			encrypt_block(plain, plain_pre_8, &crypted, &crypted_pre_8, 
+					key, &count, &pos_in_block, &is_header);
 		}
 	}
 
 	inp = instr;
 	while (instrlen > 0) {
-		if (pos_in_byte < 8) {
-			plain[pos_in_byte++] = *(inp++);
+		if (pos_in_block < 8) {
+			plain[pos_in_block++] = *(inp++);
 			instrlen--;
 		}
-		if (pos_in_byte == 8) {
-			encrypt_every_8_byte(plain, plain_pre_8, &crypted, &crypted_pre_8, 
-					key, &count, &pos_in_byte, &is_header);
+		if (pos_in_block == 8) {
+			encrypt_block(plain, plain_pre_8, &crypted, &crypted_pre_8, 
+					key, &count, &pos_in_block, &is_header);
 		}
 	}
 
 	padding = 1;		/* pad some stuff in tail */
 	while (padding <= 7) {	/* at most seven bytes */
-		if (pos_in_byte < 8) {
-			plain[pos_in_byte++] = 0x00;
+		if (pos_in_block < 8) {
+			plain[pos_in_block++] = 0x00;
 			padding++;
 		}
-		if (pos_in_byte == 8) {
-			encrypt_every_8_byte(plain, plain_pre_8, &crypted, &crypted_pre_8, 
-					key, &count, &pos_in_byte, &is_header);
+		if (pos_in_block == 8) {
+			encrypt_block(plain, plain_pre_8, &crypted, &crypted_pre_8, 
+					key, &count, &pos_in_block, &is_header);
 		}
 	}
 
-	*outstrlen_prt = count;
+	*outstrlen_ptr = count;
 }
 
 
@@ -200,20 +196,20 @@
 	w[1] = htonl(z);
 }
 
-static gint decrypt_every_8_byte(const guint8 **crypt_buff, const gint instrlen, 
+static gint decrypt_block(const guint8 **crypt_buff, const gint instrlen, 
 		const guint8 *const key, gint *context_start, 
-		guint8 *decrypted, gint *pos_in_byte)
+		guint8 *decrypted, gint *pos_in_block)
 {
-	for (*pos_in_byte = 0; *pos_in_byte < 8; (*pos_in_byte)++) {
-		if (*context_start + *pos_in_byte >= instrlen)
-			return 1;
-		decrypted[*pos_in_byte] ^= (*crypt_buff)[*pos_in_byte];
-	}
+	if (*context_start == instrlen)
+		return 1;
+
+	*(guint64 *) decrypted ^= **(guint64 **) crypt_buff;
+
 	qq_decipher((guint32 *) decrypted, (guint32 *) key, (guint32 *) decrypted);
 
 	*context_start += 8;
 	*crypt_buff += 8;
-	*pos_in_byte = 0;
+	*pos_in_block = 0;
 
 	return 1;
 }
@@ -225,19 +221,19 @@
 {
 	guint8 decrypted[8], m[8], *outp;
 	const guint8 *crypt_buff, *crypt_buff_pre_8;
-	gint count, context_start, pos_in_byte, padding;
+	gint count, context_start, pos_in_block, padding;
 
 	/* at least 16 bytes and %8 == 0 */
 	if ((instrlen % 8) || (instrlen < 16)) { 
 		gaim_debug(GAIM_DEBUG_ERROR, "QQ", 
-			"Packet len is either too short or not a multiple of 8 bytes, read %d bytes\n", 
+			"Ciphertext len is either too short or not a multiple of 8 bytes, read %d bytes\n", 
 			instrlen);
 		return 0;
 	}
 	/* get information from header */
 	qq_decipher((guint32 *) instr, (guint32 *) key, (guint32 *) decrypted);
-	pos_in_byte = decrypted[0] & 0x7;
-	count = instrlen - pos_in_byte - 10;	/* this is the plaintext length */
+	pos_in_block = decrypted[0] & 0x7;
+	count = instrlen - pos_in_block - 10;	/* this is the plaintext length */
 	/* return if outstr buffer is not large enough or error plaintext length */
 	if (*outstrlen_ptr < count || count < 0) {
 		gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Buffer len %d is less than real len %d", 
@@ -251,18 +247,18 @@
 
 	crypt_buff = instr + 8;	/* address of real data start */
 	context_start = 8;	/* context is at the second block of 8 bytes */
-	pos_in_byte++;		/* start of paddng stuff */
+	pos_in_block++;		/* start of paddng stuff */
 
 	padding = 1;		/* at least one in header */
 	while (padding <= 2) {	/* there are 2 byte padding stuff in header */
-		if (pos_in_byte < 8) {	/* bypass the padding stuff, it's nonsense data */
-			pos_in_byte++;
+		if (pos_in_block < 8) {	/* bypass the padding stuff, it's nonsense data */
+			pos_in_block++;
 			padding++;
 		}
-		if (pos_in_byte == 8) {
+		if (pos_in_block == 8) {
 			crypt_buff_pre_8 = instr;
-			if (!decrypt_every_8_byte(&crypt_buff, instrlen, key, 
-						&context_start, decrypted, &pos_in_byte)) {
+			if (!decrypt_block(&crypt_buff, instrlen, key, 
+						&context_start, decrypted, &pos_in_block)) {
 				gaim_debug(GAIM_DEBUG_ERROR, "QQ", "decrypt every 8 bytes error A");
 				return 0;
 			}
@@ -271,16 +267,16 @@
 
 	outp = outstr;
 	while (count != 0) {
-		if (pos_in_byte < 8) {
-			*outp = crypt_buff_pre_8[pos_in_byte] ^ decrypted[pos_in_byte];
+		if (pos_in_block < 8) {
+			*outp = crypt_buff_pre_8[pos_in_block] ^ decrypted[pos_in_block];
 			outp++;
 			count--;
-			pos_in_byte++;
+			pos_in_block++;
 		}
-		if (pos_in_byte == 8) {
+		if (pos_in_block == 8) {
 			crypt_buff_pre_8 = crypt_buff - 8;
-			if (!decrypt_every_8_byte(&crypt_buff, instrlen, key, 
-						&context_start, decrypted, &pos_in_byte)) {
+			if (!decrypt_block(&crypt_buff, instrlen, key, 
+						&context_start, decrypted, &pos_in_block)) {
 				gaim_debug(GAIM_DEBUG_ERROR, "QQ", "decrypt every 8 bytes error B");
 				return 0;
 			}
@@ -288,15 +284,15 @@
 	}
 
 	for (padding = 1; padding < 8; padding++) {
-		if (pos_in_byte < 8) {
-			if (crypt_buff_pre_8[pos_in_byte] ^ decrypted[pos_in_byte])
+		if (pos_in_block < 8) {
+			if (crypt_buff_pre_8[pos_in_block] ^ decrypted[pos_in_block])
 				return 0;
-			pos_in_byte++;
+			pos_in_block++;
 		}
-		if (pos_in_byte == 8) {
+		if (pos_in_block == 8) {
 			crypt_buff_pre_8 = crypt_buff;
-			if (!decrypt_every_8_byte(&crypt_buff, instrlen, key, 
-						&context_start, decrypted, &pos_in_byte)) {
+			if (!decrypt_block(&crypt_buff, instrlen, key, 
+						&context_start, decrypted, &pos_in_block)) {
 				gaim_debug(GAIM_DEBUG_ERROR, "QQ", "decrypt every 8 bytes error C");
 				return 0;
 			}