/pidgin/main: 9effc94565d8: ciphers cleanup: add reset state cal...

Tomasz Wasilczyk tomkiewicz at cpw.pidgin.im
Mon May 6 05:13:43 EDT 2013


Changeset: 9effc94565d8aa2a5a01f3197b354389837db9dc
Author:	 Tomasz Wasilczyk <tomkiewicz at cpw.pidgin.im>
Date:	 2013-05-06 11:13 +0200
Branch:	 default
URL: https://hg.pidgin.im/pidgin/main/rev/9effc94565d8

Description:

ciphers cleanup: add reset state callback

diffstat:

 libpurple/cipher.c              |  22 +++++++++++++++++++
 libpurple/cipher.h              |  46 +++++++++++++++++++++++++++-------------
 libpurple/ciphers/des.c         |   2 +
 libpurple/ciphers/gchecksum.c   |   1 +
 libpurple/ciphers/hmac.c        |  22 +++++++++++++++----
 libpurple/ciphers/md4.c         |   1 +
 libpurple/ciphers/rc4.c         |   3 +-
 libpurple/protocols/msn/nexus.c |  14 +++--------
 8 files changed, 80 insertions(+), 31 deletions(-)

diffs (248 lines):

diff --git a/libpurple/cipher.c b/libpurple/cipher.c
--- a/libpurple/cipher.c
+++ b/libpurple/cipher.c
@@ -88,6 +88,8 @@ purple_cipher_get_capabilities(PurpleCip
 		caps |= PURPLE_CIPHER_CAPS_INIT;
 	if(ops->reset)
 		caps |= PURPLE_CIPHER_CAPS_RESET;
+	if(ops->reset_state)
+		caps |= PURPLE_CIPHER_CAPS_RESET_STATE;
 	if(ops->uninit)
 		caps |= PURPLE_CIPHER_CAPS_UNINIT;
 	if(ops->set_iv)
@@ -359,6 +361,26 @@ purple_cipher_context_reset(PurpleCipher
 }
 
 void
+purple_cipher_context_reset_state(PurpleCipherContext *context, void *extra) {
+	PurpleCipher *cipher = NULL;
+
+	g_return_if_fail(context);
+
+	cipher = context->cipher;
+	g_return_if_fail(cipher);
+	g_return_if_fail(cipher->ops);
+
+	if (cipher->ops->reset_state) {
+		context->cipher->ops->reset_state(context, extra);
+		return;
+	}
+
+	purple_debug_warning("cipher", "the %s cipher does not support the "
+		"reset_state operation\n", cipher->name);
+	purple_cipher_context_reset(context, extra);
+}
+
+void
 purple_cipher_context_destroy(PurpleCipherContext *context) {
 	PurpleCipher *cipher = NULL;
 
diff --git a/libpurple/cipher.h b/libpurple/cipher.h
--- a/libpurple/cipher.h
+++ b/libpurple/cipher.h
@@ -54,21 +54,22 @@ typedef enum {
 	PURPLE_CIPHER_CAPS_GET_OPT          = 1 << 2,   /**< Get option flag */
 	PURPLE_CIPHER_CAPS_INIT             = 1 << 3,   /**< Init flag */
 	PURPLE_CIPHER_CAPS_RESET            = 1 << 4,   /**< Reset flag */
-	PURPLE_CIPHER_CAPS_UNINIT           = 1 << 5,   /**< Uninit flag */
-	PURPLE_CIPHER_CAPS_SET_IV           = 1 << 6,   /**< Set IV flag */
-	PURPLE_CIPHER_CAPS_APPEND           = 1 << 7,   /**< Append flag */
-	PURPLE_CIPHER_CAPS_DIGEST           = 1 << 8,   /**< Digest flag */
-	PURPLE_CIPHER_CAPS_GET_DIGEST_SIZE  = 1 << 9,   /**< The get digest size flag */
-	PURPLE_CIPHER_CAPS_ENCRYPT          = 1 << 10,  /**< Encrypt flag */
-	PURPLE_CIPHER_CAPS_DECRYPT          = 1 << 11,  /**< Decrypt flag */
-	PURPLE_CIPHER_CAPS_SET_SALT         = 1 << 12,  /**< Set salt flag */
-	PURPLE_CIPHER_CAPS_GET_SALT_SIZE    = 1 << 13,  /**< Get salt size flag */
-	PURPLE_CIPHER_CAPS_SET_KEY          = 1 << 14,  /**< Set key flag */
-	PURPLE_CIPHER_CAPS_GET_KEY_SIZE     = 1 << 15,  /**< Get key size flag */
-	PURPLE_CIPHER_CAPS_SET_BATCH_MODE   = 1 << 16,  /**< Set batch mode flag */
-	PURPLE_CIPHER_CAPS_GET_BATCH_MODE   = 1 << 17,  /**< Get batch mode flag */
-	PURPLE_CIPHER_CAPS_GET_BLOCK_SIZE   = 1 << 18,  /**< The get block size flag */
-	PURPLE_CIPHER_CAPS_UNKNOWN          = 1 << 19   /**< Unknown */
+	PURPLE_CIPHER_CAPS_RESET_STATE      = 1 << 5,   /**< Reset state flag */
+	PURPLE_CIPHER_CAPS_UNINIT           = 1 << 6,   /**< Uninit flag */
+	PURPLE_CIPHER_CAPS_SET_IV           = 1 << 7,   /**< Set IV flag */
+	PURPLE_CIPHER_CAPS_APPEND           = 1 << 8,   /**< Append flag */
+	PURPLE_CIPHER_CAPS_DIGEST           = 1 << 9,   /**< Digest flag */
+	PURPLE_CIPHER_CAPS_GET_DIGEST_SIZE  = 1 << 10,   /**< The get digest size flag */
+	PURPLE_CIPHER_CAPS_ENCRYPT          = 1 << 11,  /**< Encrypt flag */
+	PURPLE_CIPHER_CAPS_DECRYPT          = 1 << 12,  /**< Decrypt flag */
+	PURPLE_CIPHER_CAPS_SET_SALT         = 1 << 13,  /**< Set salt flag */
+	PURPLE_CIPHER_CAPS_GET_SALT_SIZE    = 1 << 14,  /**< Get salt size flag */
+	PURPLE_CIPHER_CAPS_SET_KEY          = 1 << 15,  /**< Set key flag */
+	PURPLE_CIPHER_CAPS_GET_KEY_SIZE     = 1 << 16,  /**< Get key size flag */
+	PURPLE_CIPHER_CAPS_SET_BATCH_MODE   = 1 << 17,  /**< Set batch mode flag */
+	PURPLE_CIPHER_CAPS_GET_BATCH_MODE   = 1 << 18,  /**< Get batch mode flag */
+	PURPLE_CIPHER_CAPS_GET_BLOCK_SIZE   = 1 << 19,  /**< The get block size flag */
+	PURPLE_CIPHER_CAPS_UNKNOWN          = 1 << 20   /**< Unknown */
 } PurpleCipherCaps;
 
 /**
@@ -87,6 +88,9 @@ struct _PurpleCipherOps {
 	/** The reset function */
 	void (*reset)(PurpleCipherContext *context, void *extra);
 
+	/** The reset state function */
+	void (*reset_state)(PurpleCipherContext *context, void *extra);
+
 	/** The uninit function */
 	void (*uninit)(PurpleCipherContext *context);
 
@@ -292,6 +296,18 @@ PurpleCipherContext *purple_cipher_conte
 void purple_cipher_context_reset(PurpleCipherContext *context, gpointer extra);
 
 /**
+ * Resets a cipher state to it's default value, but doesn't touch stateless
+ * configuration.
+ *
+ * That means, IV and digest context will be wiped out, but keys, ops or salt
+ * will remain untouched.
+ *
+ * @param context The context to reset
+ * @param extra   Extra data for the specific cipher
+ */
+void purple_cipher_context_reset_state(PurpleCipherContext *context, gpointer extra);
+
+/**
  * Destorys a cipher context and deinitializes it
  *
  * @param context The cipher context to destory
diff --git a/libpurple/ciphers/des.c b/libpurple/ciphers/des.c
--- a/libpurple/ciphers/des.c
+++ b/libpurple/ciphers/des.c
@@ -484,6 +484,7 @@ static PurpleCipherOps DESOps = {
 	NULL,              /* Get option */
 	des_init,          /* init */
  	NULL,              /* reset */
+ 	NULL,              /* reset state */
 	des_uninit,        /* uninit */
 	NULL,              /* set iv */
 	NULL,              /* append */
@@ -863,6 +864,7 @@ static PurpleCipherOps DES3Ops = {
 	NULL,              /* Get option */
 	des3_init,         /* init */
 	NULL,              /* reset */
+	NULL,              /* reset state */
 	des3_uninit,       /* uninit */
 	des3_set_iv,       /* set iv */
 	NULL,              /* append */
diff --git a/libpurple/ciphers/gchecksum.c b/libpurple/ciphers/gchecksum.c
--- a/libpurple/ciphers/gchecksum.c
+++ b/libpurple/ciphers/gchecksum.c
@@ -112,6 +112,7 @@ purple_g_checksum_digest(PurpleCipherCon
 		NULL,                     /* Get option */       \
 		lower##_init,             /* init */             \
 		lower##_reset,            /* reset */            \
+		lower##_reset,            /* reset state */      \
 		purple_g_checksum_uninit, /* uninit */           \
 		NULL,                     /* set iv */           \
 		purple_g_checksum_append, /* append */           \
diff --git a/libpurple/ciphers/hmac.c b/libpurple/ciphers/hmac.c
--- a/libpurple/ciphers/hmac.c
+++ b/libpurple/ciphers/hmac.c
@@ -60,6 +60,17 @@ hmac_reset(PurpleCipherContext *context,
 }
 
 	static void
+hmac_reset_state(PurpleCipherContext *context, gpointer extra)
+{
+	struct HMAC_Context *hctx;
+
+	hctx = purple_cipher_context_get_data(context);
+
+	if (hctx->hash)
+		purple_cipher_context_reset_state(hctx->hash, extra);
+}
+
+	static void
 hmac_set_opt(PurpleCipherContext *context, const gchar *name, void *value)
 {
 	struct HMAC_Context *hctx;
@@ -206,12 +217,13 @@ hmac_get_block_size(PurpleCipherContext 
 static PurpleCipherOps HMACOps = {
 	hmac_set_opt,           /* Set option */
 	hmac_get_opt,           /* Get option */
-	hmac_init,               /* init */
-	hmac_reset,              /* reset */
-	hmac_uninit,             /* uninit */
+	hmac_init,              /* init */
+	hmac_reset,             /* reset */
+	hmac_reset_state,       /* reset state */
+	hmac_uninit,            /* uninit */
 	NULL,                   /* set iv */
-	hmac_append,             /* append */
-	hmac_digest,             /* digest */
+	hmac_append,            /* append */
+	hmac_digest,            /* digest */
 	hmac_get_digest_size,   /* get digest size */
 	NULL,                   /* encrypt */
 	NULL,                   /* decrypt */
diff --git a/libpurple/ciphers/md4.c b/libpurple/ciphers/md4.c
--- a/libpurple/ciphers/md4.c
+++ b/libpurple/ciphers/md4.c
@@ -281,6 +281,7 @@ static PurpleCipherOps MD4Ops = {
 	NULL,                   /* Get option */
 	md4_init,               /* init */
 	md4_reset,              /* reset */
+	md4_reset,              /* reset state */
 	md4_uninit,             /* uninit */
 	NULL,                   /* set iv */
 	md4_append,             /* append */
diff --git a/libpurple/ciphers/rc4.c b/libpurple/ciphers/rc4.c
--- a/libpurple/ciphers/rc4.c
+++ b/libpurple/ciphers/rc4.c
@@ -134,6 +134,7 @@ static PurpleCipherOps RC4Ops = {
 	NULL,          /* Get Option    */
 	rc4_init,      /* init          */
 	rc4_reset,     /* reset         */
+	NULL,          /* reset state   */
 	rc4_uninit,    /* uninit        */
 	NULL,          /* set iv        */
 	NULL,          /* append        */
@@ -144,7 +145,7 @@ static PurpleCipherOps RC4Ops = {
 	NULL,          /* set salt      */
 	NULL,          /* get salt size */
 	rc4_set_key,   /* set key       */
-	NULL,          /* get key size */
+	NULL,          /* get key size  */
 	NULL,          /* set batch mode */
 	NULL,          /* get batch mode */
 	NULL,          /* get block size */
diff --git a/libpurple/protocols/msn/nexus.c b/libpurple/protocols/msn/nexus.c
--- a/libpurple/protocols/msn/nexus.c
+++ b/libpurple/protocols/msn/nexus.c
@@ -104,30 +104,24 @@ rps_create_key(const char *key, int key_
 	char *result;
 
 	hmac = purple_cipher_context_new_by_name("hmac", NULL);
-
 	purple_cipher_context_set_option(hmac, "hash", "sha1");
 	purple_cipher_context_set_key(hmac, (guchar *)key, key_len);
+
 	purple_cipher_context_append(hmac, magic, magic_len);
 	purple_cipher_context_append(hmac, (guchar *)data, data_len);
 	purple_cipher_context_digest(hmac, hash1, sizeof(hash1));
 
-	purple_cipher_context_reset(hmac, NULL);
-	purple_cipher_context_set_option(hmac, "hash", "sha1");
-	purple_cipher_context_set_key(hmac, (guchar *)key, key_len);
+	purple_cipher_context_reset_state(hmac, NULL);
 	purple_cipher_context_append(hmac, hash1, 20);
 	purple_cipher_context_append(hmac, magic, magic_len);
 	purple_cipher_context_append(hmac, (guchar *)data, data_len);
 	purple_cipher_context_digest(hmac, hash2, sizeof(hash2));
 
-	purple_cipher_context_reset(hmac, NULL);
-	purple_cipher_context_set_option(hmac, "hash", "sha1");
-	purple_cipher_context_set_key(hmac, (guchar *)key, key_len);
+	purple_cipher_context_reset_state(hmac, NULL);
 	purple_cipher_context_append(hmac, hash1, 20);
 	purple_cipher_context_digest(hmac, hash3, sizeof(hash3));
 
-	purple_cipher_context_reset(hmac, NULL);
-	purple_cipher_context_set_option(hmac, "hash", "sha1");
-	purple_cipher_context_set_key(hmac, (guchar *)key, key_len);
+	purple_cipher_context_reset_state(hmac, NULL);
 	purple_cipher_context_append(hmac, hash3, sizeof(hash3));
 	purple_cipher_context_append(hmac, magic, magic_len);
 	purple_cipher_context_append(hmac, (guchar *)data, data_len);



More information about the Commits mailing list