/pidgin/main: a924aacd5a37: ciphers cleanup: encryption and decr...

Tomasz Wasilczyk tomkiewicz at cpw.pidgin.im
Sun May 5 10:29:22 EDT 2013


Changeset: a924aacd5a371b9d7417bc2c5c808b0a5abd328b
Author:	 Tomasz Wasilczyk <tomkiewicz at cpw.pidgin.im>
Date:	 2013-05-05 16:29 +0200
Branch:	 default
URL: https://hg.pidgin.im/pidgin/main/rev/a924aacd5a37

Description:

ciphers cleanup: encryption and decryption lengths

diffstat:

 libpurple/cipher.c                    |   31 ++---
 libpurple/cipher.h                    |   32 +++---
 libpurple/ciphers/des.c               |  178 ++++++++++++++++++++-------------
 libpurple/ciphers/gchecksum.c         |    1 +
 libpurple/ciphers/hmac.c              |    2 +
 libpurple/ciphers/md4.c               |    2 +
 libpurple/ciphers/rc4.c               |   17 +-
 libpurple/ntlm.c                      |    3 +-
 libpurple/protocols/msn/nexus.c       |    5 +-
 libpurple/protocols/myspace/myspace.c |    4 +-
 10 files changed, 155 insertions(+), 120 deletions(-)

diffs (truncated from 629 to 300 lines):

diff --git a/libpurple/cipher.c b/libpurple/cipher.c
--- a/libpurple/cipher.c
+++ b/libpurple/cipher.c
@@ -473,50 +473,49 @@ purple_cipher_context_get_digest_size(Pu
 	}
 }
 
-gint
-purple_cipher_context_encrypt(PurpleCipherContext *context, const guchar data[],
-							size_t len, guchar output[], size_t *outlen)
+ssize_t
+purple_cipher_context_encrypt(PurpleCipherContext *context,
+	const guchar input[], size_t in_len, guchar output[], size_t out_size)
 {
 	PurpleCipher *cipher = NULL;
 
-	g_return_val_if_fail(context, -1);
+	g_return_val_if_fail(context != NULL, -1);
+	g_return_val_if_fail(input != NULL, -1);
+	g_return_val_if_fail(output != NULL, -1);
+	g_return_val_if_fail(out_size < in_len, -1);
 
 	cipher = context->cipher;
 	g_return_val_if_fail(cipher, -1);
 
 	if(cipher->ops && cipher->ops->encrypt)
-		return cipher->ops->encrypt(context, data, len, output, outlen);
+		return cipher->ops->encrypt(context, input, in_len, output, out_size);
 	else {
 		purple_debug_warning("cipher", "the %s cipher does not support the encrypt"
 						"operation\n", cipher->name);
 
-		if(outlen)
-			*outlen = -1;
-
 		return -1;
 	}
 }
 
-gint
-purple_cipher_context_decrypt(PurpleCipherContext *context, const guchar data[],
-							size_t len, guchar output[], size_t *outlen)
+ssize_t
+purple_cipher_context_decrypt(PurpleCipherContext *context,
+	const guchar input[], size_t in_len, guchar output[], size_t out_size)
 {
 	PurpleCipher *cipher = NULL;
 
-	g_return_val_if_fail(context, -1);
+	g_return_val_if_fail(context != NULL, -1);
+	g_return_val_if_fail(input != NULL, -1);
+	g_return_val_if_fail(output != NULL, -1);
 
 	cipher = context->cipher;
 	g_return_val_if_fail(cipher, -1);
 
 	if(cipher->ops && cipher->ops->decrypt)
-		return cipher->ops->decrypt(context, data, len, output, outlen);
+		return cipher->ops->decrypt(context, input, in_len, output, out_size);
 	else {
 		purple_debug_warning("cipher", "the %s cipher does not support the decrypt"
 						"operation\n", cipher->name);
 
-		if(outlen)
-			*outlen = -1;
-
 		return -1;
 	}
 }
diff --git a/libpurple/cipher.h b/libpurple/cipher.h
--- a/libpurple/cipher.h
+++ b/libpurple/cipher.h
@@ -103,10 +103,10 @@ struct _PurpleCipherOps {
 	size_t (*get_digest_size)(PurpleCipherContext *context);
 
 	/** The encrypt function */
-	int (*encrypt)(PurpleCipherContext *context, const guchar data[], size_t len, guchar output[], size_t *outlen);
+	ssize_t (*encrypt)(PurpleCipherContext *context, const guchar input[], size_t in_len, guchar output[], size_t out_size);
 
 	/** The decrypt function */
-	int (*decrypt)(PurpleCipherContext *context, const guchar data[], size_t len, guchar output[], size_t *outlen);
+	ssize_t (*decrypt)(PurpleCipherContext *context, const guchar input[], size_t in_len, guchar output[], size_t out_size);
 
 	/** The set salt function */
 	void (*set_salt)(PurpleCipherContext *context, guchar *salt, size_t len);
@@ -347,28 +347,28 @@ size_t purple_cipher_context_get_digest_
 /**
  * Encrypts data using the context
  *
- * @param context The context
- * @param data    The data to encrypt
- * @param len     The length of the data
- * @param output  The output buffer
- * @param outlen  The len of data that was outputed
+ * @param context  The context
+ * @param input    The data to encrypt
+ * @param in_len   The length of the data
+ * @param output   The output buffer
+ * @param out_size The size of the output buffer
  *
- * @return A cipher specific status code
+ * @return A length of data that was outputed or -1, if failed
  */
-gint purple_cipher_context_encrypt(PurpleCipherContext *context, const guchar data[], size_t len, guchar output[], size_t *outlen);
+ssize_t purple_cipher_context_encrypt(PurpleCipherContext *context, const guchar input[], size_t in_len, guchar output[], size_t out_size);
 
 /**
  * Decrypts data using the context
  *
- * @param context The context
- * @param data    The data to encrypt
- * @param len     The length of the returned value
- * @param output  The output buffer
- * @param outlen  The len of data that was outputed
+ * @param context  The context
+ * @param input    The data to encrypt
+ * @param in_len   The length of the returned value
+ * @param output   The output buffer
+ * @param out_size The size of the output buffer
  *
- * @return A cipher specific status code
+ * @return A length of data that was outputed or -1, if failed
  */
-gint purple_cipher_context_decrypt(PurpleCipherContext *context, const guchar data[], size_t len, guchar output[], size_t *outlen);
+ssize_t purple_cipher_context_decrypt(PurpleCipherContext *context, const guchar input[], size_t in_len, guchar output[], size_t out_size);
 
 /**
  * Sets the salt on a context
diff --git a/libpurple/ciphers/des.c b/libpurple/ciphers/des.c
--- a/libpurple/ciphers/des.c
+++ b/libpurple/ciphers/des.c
@@ -33,6 +33,8 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
  */
+
+#include "internal.h"
 #include <cipher.h>
 #include "ciphers.h"
 
@@ -387,27 +389,32 @@ des_ecb_crypt (struct _des_ctx *ctx, con
 	return 0;
 }
 
-static gint
-des_encrypt(PurpleCipherContext *context, const guchar data[],
-            size_t len, guchar output[], size_t *outlen)
+static ssize_t
+des_encrypt(PurpleCipherContext *context, const guchar input[], size_t in_len,
+	guchar output[], size_t out_size)
 {
 	int offset = 0;
 	int i = 0;
 	int tmp;
 	guint8 buf[8] = {0,0,0,0,0,0,0,0};
-	while(offset+8<=len) {
+	ssize_t out_len;
+
+	g_return_val_if_fail(out_size < in_len, -1);
+
+	while(offset+8<=in_len) {
 		des_ecb_crypt(purple_cipher_context_get_data(context),
-		              data+offset,
+		              input+offset,
 		              output+offset,
 		              0);
 		offset+=8;
 	}
-	*outlen = len;
-	if(offset<len) {
-		*outlen += len - offset;
+	out_len = in_len;
+	if(offset<in_len) {
+		out_len += in_len - offset;
+		g_return_val_if_fail(out_size < out_len, -1);
 		tmp = offset;
-		while(tmp<len) {
-			buf[i++] = data[tmp];
+		while(tmp<in_len) {
+			buf[i++] = input[tmp];
 			tmp++;
 		}
 		des_ecb_crypt(purple_cipher_context_get_data(context),
@@ -415,30 +422,35 @@ des_encrypt(PurpleCipherContext *context
 		              output+offset,
 		              0);
 	}
-	return 0;
+	return out_len;
 }
 
 static gint
-des_decrypt(PurpleCipherContext *context, const guchar data[],
-            size_t len, guchar output[], size_t *outlen)
+des_decrypt(PurpleCipherContext *context, const guchar input[], size_t in_len,
+	guchar output[], size_t out_size)
 {
 	int offset = 0;
 	int i = 0;
 	int tmp;
 	guint8 buf[8] = {0,0,0,0,0,0,0,0};
-	while(offset+8<=len) {
+	ssize_t out_len;
+
+	g_return_val_if_fail(out_size < in_len, -1);
+
+	while(offset+8<=in_len) {
 		des_ecb_crypt(purple_cipher_context_get_data(context),
-		              data+offset,
+		              input+offset,
 		              output+offset,
 		              1);
 		offset+=8;
 	}
-	*outlen = len;
-	if(offset<len) {
-		*outlen += len - offset;
+	out_len = in_len;
+	if(offset<in_len) {
+		out_len += in_len - offset;
+		g_return_val_if_fail(out_size < out_len, -1);
 		tmp = offset;
-		while(tmp<len) {
-			buf[i++] = data[tmp];
+		while(tmp<in_len) {
+			buf[i++] = input[tmp];
 			tmp++;
 		}
 		des_ecb_crypt(purple_cipher_context_get_data(context),
@@ -446,7 +458,7 @@ des_decrypt(PurpleCipherContext *context
 		              output+offset,
 		              1);
 	}
-	return 0;
+	return out_len;
 }
 
 static void
@@ -538,17 +550,21 @@ des3_get_key_size(PurpleCipherContext *c
 	return 24;
 }
 
-static gint
-des3_ecb_encrypt(struct _des3_ctx *ctx, const guchar data[],
-                 size_t len, guchar output[], size_t *outlen)
+static ssize_t
+des3_ecb_encrypt(struct _des3_ctx *ctx, const guchar input[], size_t in_len,
+	guchar output[], size_t out_size)
 {
 	int offset = 0;
 	int i = 0;
 	int tmp;
 	guint8 buf[8] = {0,0,0,0,0,0,0,0};
-	while (offset + 8 <= len) {
+	ssize_t out_len;
+
+	g_return_val_if_fail(out_size < in_len, -1);
+
+	while (offset + 8 <= in_len) {
 		des_ecb_crypt(&ctx->key1,
-		              data+offset,
+		              input+offset,
 		              output+offset,
 		              0);
 		des_ecb_crypt(&ctx->key2,
@@ -561,13 +577,14 @@ des3_ecb_encrypt(struct _des3_ctx *ctx, 
 		              0);
 		offset += 8;
 	}
-	*outlen = len;
-	if (offset < len) {
-		*outlen += len - offset;
+	out_len = in_len;
+	if (offset < in_len) {
+		out_len += in_len - offset;
+		g_return_val_if_fail(out_size < out_len, -1);
 		tmp = offset;
 		memset(buf, 0, 8);
-		while (tmp < len) {
-			buf[i++] = data[tmp];
+		while (tmp < in_len) {
+			buf[i++] = input[tmp];
 			tmp++;
 		}
 		des_ecb_crypt(&ctx->key1,
@@ -583,21 +600,25 @@ des3_ecb_encrypt(struct _des3_ctx *ctx, 
 		              output+offset,
 		              0);
 	}
-	return 0;
+	return out_len;
 }
 
-static gint
-des3_cbc_encrypt(struct _des3_ctx *ctx, const guchar data[],
-                 size_t len, guchar output[], size_t *outlen)
+static ssize_t
+des3_cbc_encrypt(struct _des3_ctx *ctx, const guchar input[], size_t in_len,
+	guchar output[], size_t out_size)
 {



More information about the Commits mailing list