gobjectification: 82ceb602: cleaned up the hmaccipher, still needs s...

grim at pidgin.im grim at pidgin.im
Sun Jul 19 14:25:22 EDT 2009


-----------------------------------------------------------------
Revision: 82ceb602afedcfbe58a2408d01852e2a444c4720
Ancestor: 88563a3608d5eba4b2ba6e0a9388b21908307518
Author: grim at pidgin.im
Date: 2009-07-19T17:49:35
Branch: im.pidgin.gobjectification
URL: http://d.pidgin.im/viewmtn/revision/info/82ceb602afedcfbe58a2408d01852e2a444c4720

Modified files:
        libpurple/hmaccipher.c libpurple/hmaccipher.h

ChangeLog: 

cleaned up the hmaccipher, still needs some more work, and hasn't been tested yet


-------------- next part --------------
============================================================
--- libpurple/hmaccipher.c	37b7608a507ac520e523eeb6fa673b3f71192f6e
+++ libpurple/hmaccipher.c	50907282533399c5e3764b6d3551ea88d1364781
@@ -23,23 +23,23 @@
 
 #include <string.h>
 
+#define PURPLE_HMAC_CIPHER_GET_PRIVATE(obj) \
+	(G_TYPE_INSTANCE_GET_PRIVATE((obj), PURPLE_TYPE_HMAC_CIPHER, PurpleHMACCipherPrivate))
+
 /*******************************************************************************
  * Structs
  ******************************************************************************/
-struct _PurpleHMACCipherPriv {
+typedef struct {
 	PurpleCipher *hash;
-	int blocksize;
 	guchar *opad;
-};
+} PurpleHMACCipherPrivate;
 
 /******************************************************************************
  * Enums
  *****************************************************************************/
 enum {
 	PROP_NONE,
-	PROP_BLOCK_SIZE,
 	PROP_HASH,
-	PROP_KEY,
 	PROP_LAST,
 };
 
@@ -49,105 +49,135 @@ static GObjectClass *parent_class = NULL
 static GObjectClass *parent_class = NULL;
 
 /*******************************************************************************
- * Cipher Stuff
+ * Helpers
  ******************************************************************************/
 static void
-purple_hmac_cipher_reset(PurpleCipher *cipher)
+purple_hmac_cipher_set_hash(PurpleCipher *cipher,
+							PurpleCipher *hash)
 {
-	PurpleHMACCipher *hmac_cipher = PURPLE_HMAC_CIPHER(cipher);
+	PurpleHMACCipherPrivate *priv = PURPLE_HMAC_CIPHER_GET_PRIVATE(cipher);
 
-	if(hmac_cipher->priv->hash)
-	{
-		g_object_unref(G_OBJECT(hmac_cipher->priv->hash));
-		hmac_cipher->priv->hash = NULL;
-	}
+	priv->hash = g_object_ref(G_OBJECT(hash));
+}
 
-	hmac_cipher->priv->blocksize = 0;
+/*******************************************************************************
+ * Cipher Stuff
+ ******************************************************************************/
+static void
+purple_hmac_cipher_reset(PurpleCipher *cipher) {
+	PurpleHMACCipherPrivate *priv = PURPLE_HMAC_CIPHER_GET_PRIVATE(cipher);
 
-	if(hmac_cipher->priv->opad)
-	{
-		g_free(hmac_cipher->priv->opad);
-		hmac_cipher->priv->opad = NULL;
+	if(PURPLE_IS_CIPHER(priv->hash)) {
+		purple_cipher_reset(priv->hash);
+
+		g_object_unref(G_OBJECT(priv->hash));
+
+		priv->hash = NULL;
 	}
+
+	if(priv->opad) {
+		g_free(priv->opad);
+		priv->opad = NULL;
+	}
 }
 
 static void
-purple_hmac_cipher_append(PurpleCipher *cipher, const guchar *data, size_t len)
-{
-	PurpleHMACCipher *hmac_cipher = PURPLE_HMAC_CIPHER(cipher);
+purple_hmac_cipher_set_iv(PurpleCipher *cipher, guchar *iv, size_t len) {
+	PurpleHMACCipherPrivate *priv = PURPLE_HMAC_CIPHER_GET_PRIVATE(cipher);
 
-	g_return_if_fail(hmac_cipher->priv->hash);
+	if(PURPLE_IS_CIPHER(priv->hash))
+		purple_cipher_set_iv(priv->hash, iv, len);
+}
 
-	purple_cipher_append(hmac_cipher->priv->hash, data, len);
+static void
+purple_hmac_cipher_append(PurpleCipher *cipher, const guchar *d, size_t l) {
+	PurpleHMACCipherPrivate *priv = PURPLE_HMAC_CIPHER_GET_PRIVATE(cipher);
+
+	g_return_if_fail(PURPLE_IS_CIPHER(priv->hash));
+
+	purple_cipher_append(priv->hash, d, l);
 }
 
 static gboolean
 purple_hmac_cipher_digest(PurpleCipher *cipher, size_t in_len, guchar *out, size_t *out_len)
 {
-	PurpleHMACCipher *hmac_cipher = PURPLE_HMAC_CIPHER(cipher);
-	PurpleCipher *hash = hmac_cipher->priv->hash;
-	guchar *inner_hash;
-	size_t hash_len;
-	gboolean result;
+	PurpleHMACCipherPrivate *priv = PURPLE_HMAC_CIPHER_GET_PRIVATE(cipher);
+	guchar *digest = NULL;
+	size_t hash_len, block_size;
+	gboolean result = FALSE;
 
-	g_return_val_if_fail(hash, FALSE);
+	g_return_val_if_fail(PURPLE_IS_CIPHER(priv->hash), FALSE);
 
-	inner_hash = g_malloc(100); /* TODO: Should be enough for now... */
-	result = purple_cipher_digest(hash, 100, inner_hash, &hash_len);
+	block_size = purple_cipher_get_block_size(priv->hash);
 
-	purple_cipher_reset(hash);
+	/* TODO: add class method on PurpleCipher to give us this value */
+	digest = g_malloc(100);
 
-	purple_cipher_append(hash, hmac_cipher->priv->opad, hmac_cipher->priv->blocksize);
-	purple_cipher_append(hash, inner_hash, hash_len);
+	/* get the digest of the data */
+	result = purple_cipher_digest(priv->hash, 100, digest, &hash_len);
+	purple_cipher_reset(priv->hash);
 
-	g_free(inner_hash);
+	if(!result) {
+		g_free(digest);
 
-	result = result && purple_cipher_digest(hash, in_len, out, out_len);
+		return FALSE;
+	}
 
+	/* now append the opad and the digest from above */
+	purple_cipher_append(priv->hash, priv->opad, block_size);
+	purple_cipher_append(priv->hash, digest, hash_len);
+
+	/* do our last digest */
+	result = purple_cipher_digest(priv->hash, in_len, out, out_len);
+
+	/* cleanup */
+	g_free(digest);
+
 	return result;
 }
 
 static void
-purple_hmac_cipher_set_key_with_len(PurpleCipher *cipher, const guchar * key, size_t key_len)
+purple_hmac_cipher_set_key_with_len(PurpleCipher *cipher, const guchar *key,
+									size_t key_len)
 {
-	PurpleHMACCipher *hmac_cipher = PURPLE_HMAC_CIPHER(cipher);
-	int blocksize, i;
+	PurpleHMACCipherPrivate *priv = PURPLE_HMAC_CIPHER_GET_PRIVATE(cipher);
+	gint block_size, i;
 	guchar *ipad;
 	guchar *full_key;
 
-	g_return_if_fail(hmac_cipher->priv->hash);
+	g_return_if_fail(priv->hash);
 
-	g_free(hmac_cipher->priv->opad);
+	g_free(priv->opad);
 
-	blocksize = hmac_cipher->priv->blocksize;
-	ipad = g_malloc(blocksize);
-	hmac_cipher->priv->opad = g_malloc(blocksize);
+	block_size = purple_cipher_get_block_size(priv->hash);
+	ipad = g_malloc(block_size);
+	priv->opad = g_malloc(block_size);
 
-	if (key_len > blocksize) {
-		purple_cipher_reset(hmac_cipher->priv->hash);
-		purple_cipher_append(hmac_cipher->priv->hash, key, key_len);
+	if (key_len > block_size) {
+		purple_cipher_reset(priv->hash);
+		purple_cipher_append(priv->hash, key, key_len);
+
 		full_key = g_malloc(100); /* TODO: Should be enough for now... */
-		purple_cipher_digest(hmac_cipher->priv->hash, 100, full_key, &key_len);
-	} else
+		purple_cipher_digest(priv->hash, 100, full_key, &key_len);
+	} else {
 		full_key = g_memdup(key, key_len);
+	}
 
-    if (key_len < blocksize) {
-		full_key = g_realloc(full_key, blocksize);
-		memset(full_key + key_len, 0, blocksize - key_len);
+    if (key_len < block_size) {
+		full_key = g_realloc(full_key, block_size);
+		memset(full_key + key_len, 0, block_size - key_len);
     }
 
-	for(i = 0; i < blocksize; i++) {
+	for(i = 0; i < block_size; i++) {
 		ipad[i] = 0x36 ^ full_key[i];
-		hmac_cipher->priv->opad[i] = 0x5c ^ full_key[i];
+		priv->opad[i] = 0x5c ^ full_key[i];
 	}
 
 	g_free(full_key);
 
-	purple_cipher_reset(hmac_cipher->priv->hash);
-	purple_cipher_append(hmac_cipher->priv->hash, ipad, blocksize);
+	purple_cipher_reset(priv->hash);
+	purple_cipher_append(priv->hash, ipad, block_size);
 	g_free(ipad);
-
-	g_object_notify(G_OBJECT(hmac_cipher), "key");
 }
 
 static void
@@ -159,36 +189,15 @@ purple_hmac_cipher_get_block_size(Purple
 static size_t
 purple_hmac_cipher_get_block_size(PurpleCipher *cipher)
 {
-	PurpleHMACCipher *hmac_cipher = PURPLE_HMAC_CIPHER(cipher);
+	PurpleHMACCipherPrivate *priv = NULL;
 
-	return hmac_cipher->priv->blocksize;
-}
+	g_return_val_if_fail(PURPLE_IS_HMAC_CIPHER(cipher), 0);
 
-static void
-purple_hmac_cipher_set_hash(PurpleCipher *cipher,
-							PurpleCipher *hash)
-{
-	PurpleHMACCipher *hmac_cipher = PURPLE_HMAC_CIPHER(cipher);
+	priv = PURPLE_HMAC_CIPHER_GET_PRIVATE(cipher);
 
-#if GLIB_CHECK_VERSION(2,10,0)
-	hmac_cipher->priv->hash = g_object_ref_sink(hash);
-#else
-	hmac_cipher->priv->hash = g_object_ref(G_OBJECT(hash));
-#endif
-
-	hmac_cipher->priv->blocksize = purple_cipher_get_block_size(hash);
-
-	g_object_notify(G_OBJECT(hmac_cipher), "hash");
+	return purple_cipher_get_block_size(priv->hash);
 }
 
-static PurpleCipher *
-purple_hmac_cipher_get_hash(PurpleCipher *cipher)
-{
-	PurpleHMACCipher *hmac_cipher = PURPLE_HMAC_CIPHER(cipher);
-
-	return hmac_cipher->priv->hash;
-}
-
 /******************************************************************************
  * Object Stuff
  *****************************************************************************/
@@ -201,11 +210,8 @@ purple_hmac_cipher_set_property(GObject 
 
 	switch(param_id) {
 		case PROP_HASH:
-			purple_cipher_set_hash(cipher, g_value_get_object(value));
+			purple_hmac_cipher_set_hash(cipher, g_value_get_object(value));
 			break;
-		case PROP_KEY:
-			purple_cipher_set_key(cipher, (guchar *)g_value_get_string(value));
-			break;
 		default:
 			G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
 			break;
@@ -216,16 +222,12 @@ purple_hmac_cipher_get_property(GObject 
 purple_hmac_cipher_get_property(GObject *obj, guint param_id, GValue *value,
 								GParamSpec *pspec)
 {
-	PurpleCipher *cipher = PURPLE_CIPHER(obj);
+	PurpleHMACCipher *cipher = PURPLE_HMAC_CIPHER(obj);
 
 	switch(param_id) {
-		case PROP_BLOCK_SIZE:
-			g_value_set_int(value,
-							purple_cipher_get_block_size(cipher));
-			break;
 		case PROP_HASH:
 			g_value_set_object(value,
-							   purple_cipher_get_hash(cipher));
+							   purple_hmac_cipher_get_hash(cipher));
 			break;
 		default:
 			G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
@@ -236,12 +238,9 @@ purple_hmac_cipher_finalize(GObject *obj
 static void
 purple_hmac_cipher_finalize(GObject *obj) {
 	PurpleCipher *cipher = PURPLE_CIPHER(obj);
-	PurpleHMACCipher *hmac_cipher = PURPLE_HMAC_CIPHER(obj);
 
 	/* reset the cipher so we don't leave any data around... */
 	purple_hmac_cipher_reset(cipher);
-
-	g_free(hmac_cipher->priv);
 }
 
 static void
@@ -252,43 +251,26 @@ purple_hmac_cipher_class_init(PurpleHMAC
 
 	parent_class = g_type_class_peek_parent(klass);
 
+	g_type_class_add_private(klass, sizeof(PurpleHMACCipherPrivate));
+
 	obj_class->finalize = purple_hmac_cipher_finalize;
 	obj_class->get_property = purple_hmac_cipher_get_property;
 	obj_class->set_property = purple_hmac_cipher_set_property;
 
 	cipher_class->reset = purple_hmac_cipher_reset;
+	cipher_class->set_iv = purple_hmac_cipher_set_iv;
 	cipher_class->append = purple_hmac_cipher_append;
 	cipher_class->digest = purple_hmac_cipher_digest;
 
 	cipher_class->set_key = purple_hmac_cipher_set_key;
 	cipher_class->set_key_with_len = purple_hmac_cipher_set_key_with_len;
 	cipher_class->get_block_size = purple_hmac_cipher_get_block_size;
-	cipher_class->set_hash = purple_hmac_cipher_set_hash;
-	cipher_class->get_hash = purple_hmac_cipher_get_hash;
 
-	pspec = g_param_spec_int("blockSize", "blockSize", "blockSize",
-							 G_MININT, G_MAXINT, 0,
-							 G_PARAM_READABLE);
-	g_object_class_install_property(obj_class, PROP_BLOCK_SIZE, pspec);
-
 	pspec = g_param_spec_object("hash", "hash", "hash", PURPLE_TYPE_CIPHER,
-								G_PARAM_READWRITE);
+								G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
 	g_object_class_install_property(obj_class, PROP_HASH, pspec);
-
-	pspec = g_param_spec_string("key", "key", "key", NULL,
-								G_PARAM_WRITABLE);
-	g_object_class_install_property(obj_class, PROP_KEY, pspec);
 }
 
-static void
-purple_hmac_cipher_init(PurpleCipher *cipher) {
-	PurpleHMACCipher *hmac_cipher = PURPLE_HMAC_CIPHER(cipher);
-
-	hmac_cipher->priv = g_new0(PurpleHMACCipherPriv, 1);
-
-	purple_hmac_cipher_reset(cipher);
-}
-
 /******************************************************************************
  * API
  *****************************************************************************/
@@ -306,8 +288,8 @@ purple_hmac_cipher_get_gtype(void) {
 			NULL,
 			sizeof(PurpleHMACCipher),
 			0,
-			(GInstanceInitFunc)purple_hmac_cipher_init,
 			NULL,
+			NULL,
 		};
 
 		type = g_type_register_static(PURPLE_TYPE_CIPHER,
@@ -319,6 +301,25 @@ PurpleCipher *
 }
 
 PurpleCipher *
-purple_hmac_cipher_new(void) {
-	return g_object_new(PURPLE_TYPE_HMAC_CIPHER, NULL);
+purple_hmac_cipher_new(PurpleCipher *hash) {
+	g_return_val_if_fail(PURPLE_IS_CIPHER(hash), NULL);
+
+	return g_object_new(PURPLE_TYPE_HMAC_CIPHER,
+						"hash", hash,
+						NULL);
 }
+
+PurpleCipher *
+purple_hmac_cipher_get_hash(const PurpleHMACCipher *cipher) {
+	PurpleHMACCipherPrivate *priv = NULL;
+
+	g_return_val_if_fail(PURPLE_IS_HMAC_CIPHER(cipher), NULL);
+
+	priv = PURPLE_HMAC_CIPHER_GET_PRIVATE(cipher);
+
+	if(priv && priv->hash)
+		return priv->hash;
+
+	return NULL;
+}
+
============================================================
--- libpurple/hmaccipher.h	d298ee6316a0762841c2072d0f3d0d94da99e2d4
+++ libpurple/hmaccipher.h	5d65819b65ff70c2fea297236d7212d1353af9dd
@@ -36,14 +36,11 @@ typedef struct _PurpleHMACCipher				Purp
 #define PURPLE_HMAC_CIPHER_GET_CLASS(obj)	(G_TYPE_INSTANCE_GET_CLASS((obj), PURPLE_TYPE_HMAC_CIPHER, PurpleHMACCipherClass))
 
 typedef struct _PurpleHMACCipher				PurpleHMACCipher;
-typedef struct _PurpleHMACCipherPriv			PurpleHMACCipherPriv;
 typedef struct _PurpleHMACCipherClass			PurpleHMACCipherClass;
 
 struct _PurpleHMACCipher {
 	PurpleCipher gparent;
 
-	PurpleHMACCipherPriv *priv;
-
 	void (*_purple_reserved1)(void);
 	void (*_purple_reserved2)(void);
 	void (*_purple_reserved3)(void);
@@ -63,8 +60,10 @@ GType purple_hmac_cipher_get_gtype(void)
 
 GType purple_hmac_cipher_get_gtype(void);
 
-PurpleCipher *purple_hmac_cipher_new(void);
+PurpleCipher *purple_hmac_cipher_new(PurpleCipher *hash_cipher);
 
+PurpleCipher *purple_hmac_cipher_get_hash(const PurpleHMACCipher *cipher);
+
 G_END_DECLS
 
 #endif /* PURPLE_HMAC_CIPHER_H */


More information about the Commits mailing list