pidgin/android/libpurple

ciphers cleanup: add reset state callback

2013-05-06, Tomasz Wasilczyk
9effc94565d8
Parents 01e0d716a28e
Children 2ae6a9601d0f
ciphers cleanup: add reset state callback
--- a/libpurple/cipher.c Sun May 05 18:25:31 2013 +0200
+++ b/libpurple/cipher.c Mon May 06 11:13:33 2013 +0200
@@ -88,6 +88,8 @@
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 @@
}
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;
--- a/libpurple/cipher.h Sun May 05 18:25:31 2013 +0200
+++ b/libpurple/cipher.h Mon May 06 11:13:33 2013 +0200
@@ -54,21 +54,22 @@
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 @@
/** 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 @@
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
--- a/libpurple/ciphers/des.c Sun May 05 18:25:31 2013 +0200
+++ b/libpurple/ciphers/des.c Mon May 06 11:13:33 2013 +0200
@@ -484,6 +484,7 @@
NULL, /* Get option */
des_init, /* init */
NULL, /* reset */
+ NULL, /* reset state */
des_uninit, /* uninit */
NULL, /* set iv */
NULL, /* append */
@@ -863,6 +864,7 @@
NULL, /* Get option */
des3_init, /* init */
NULL, /* reset */
+ NULL, /* reset state */
des3_uninit, /* uninit */
des3_set_iv, /* set iv */
NULL, /* append */
--- a/libpurple/ciphers/gchecksum.c Sun May 05 18:25:31 2013 +0200
+++ b/libpurple/ciphers/gchecksum.c Mon May 06 11:13:33 2013 +0200
@@ -112,6 +112,7 @@
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 */ \
--- a/libpurple/ciphers/hmac.c Sun May 05 18:25:31 2013 +0200
+++ b/libpurple/ciphers/hmac.c Mon May 06 11:13:33 2013 +0200
@@ -60,6 +60,17 @@
}
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 @@
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 */
--- a/libpurple/ciphers/md4.c Sun May 05 18:25:31 2013 +0200
+++ b/libpurple/ciphers/md4.c Mon May 06 11:13:33 2013 +0200
@@ -281,6 +281,7 @@
NULL, /* Get option */
md4_init, /* init */
md4_reset, /* reset */
+ md4_reset, /* reset state */
md4_uninit, /* uninit */
NULL, /* set iv */
md4_append, /* append */
--- a/libpurple/ciphers/rc4.c Sun May 05 18:25:31 2013 +0200
+++ b/libpurple/ciphers/rc4.c Mon May 06 11:13:33 2013 +0200
@@ -134,6 +134,7 @@
NULL, /* Get Option */
rc4_init, /* init */
rc4_reset, /* reset */
+ NULL, /* reset state */
rc4_uninit, /* uninit */
NULL, /* set iv */
NULL, /* append */
@@ -144,7 +145,7 @@
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 */
--- a/libpurple/protocols/msn/nexus.c Sun May 05 18:25:31 2013 +0200
+++ b/libpurple/protocols/msn/nexus.c Mon May 06 11:13:33 2013 +0200
@@ -104,30 +104,24 @@
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);