/soc/2013/ankitkv/gobjectification: 5d7a7966a23c: GObjectified P...
Ankit Vani
a at nevitus.org
Fri Jun 14 09:32:04 EDT 2013
Changeset: 5d7a7966a23c6e9fa4e2a145ed62d930efe90db9
Author: Ankit Vani <a at nevitus.org>
Date: 2013-06-14 19:01 +0530
Branch: soc.2013.gobjectification
URL: https://hg.pidgin.im/soc/2013/ankitkv/gobjectification/rev/5d7a7966a23c
Description:
GObjectified PBKDF2 cipher as PurplePBKDF2Cipher
diffstat:
libpurple/ciphers/pbkdf2.c | 445 ++++++++++++++++++++++++++------------------
libpurple/ciphers/pbkdf2.h | 70 +++++++
2 files changed, 335 insertions(+), 180 deletions(-)
diffs (truncated from 636 to 300 lines):
diff --git a/libpurple/ciphers/pbkdf2.c b/libpurple/ciphers/pbkdf2.c
--- a/libpurple/ciphers/pbkdf2.c
+++ b/libpurple/ciphers/pbkdf2.c
@@ -22,17 +22,21 @@
* Written by Tomek Wasilczyk <tomkiewicz at cpw.pidgin.im>
*/
-#include "internal.h"
-#include "cipher.h"
-#include "ciphers.h"
+#include "pbkdf2.h"
+#include "hmac.h"
#include "debug.h"
/* 1024bit */
#define PBKDF2_HASH_MAX_LEN 128
-typedef struct
-{
- gchar *hash_func;
+/******************************************************************************
+ * Structs
+ *****************************************************************************/
+#define PURPLE_PBKDF2_CIPHER_GET_PRIVATE(obj) \
+ (G_TYPE_INSTANCE_GET_PRIVATE((obj), PURPLE_TYPE_PBKDF2_CIPHER, PurplePBKDF2CipherPrivate))
+
+typedef struct {
+ PurpleCipher *hash;
guint iter_count;
size_t out_len;
@@ -40,218 +44,174 @@ typedef struct
size_t salt_len;
guchar *passphrase;
size_t passphrase_len;
-} Pbkdf2Context;
+} PurplePBKDF2CipherPrivate;
+/******************************************************************************
+ * Enums
+ *****************************************************************************/
+enum {
+ PROP_NONE,
+ PROP_HASH,
+ PROP_ITER_COUNT,
+ PROP_OUT_LEN,
+ PROP_LAST,
+};
+
+/*******************************************************************************
+ * Globals
+ ******************************************************************************/
+static GObjectClass *parent_class = NULL;
+
+/*******************************************************************************
+ * Helpers
+ ******************************************************************************/
static void
-purple_pbkdf2_init(PurpleCipherContext *context, void *extra)
+purple_pbkdf2_cipher_set_hash(PurpleCipher *cipher,
+ PurpleCipher *hash)
{
- Pbkdf2Context *ctx_data;
+ PurplePBKDF2CipherPrivate *priv = PURPLE_PBKDF2_CIPHER_GET_PRIVATE(cipher);
- ctx_data = g_new0(Pbkdf2Context, 1);
- purple_cipher_context_set_data(context, ctx_data);
+ priv->hash = g_object_ref(G_OBJECT(hash));
+}
- purple_cipher_context_reset(context, extra);
+/******************************************************************************
+ * Cipher Stuff
+ *****************************************************************************/
+static void
+purple_pbkdf2_cipher_reset(PurpleCipher *cipher)
+{
+ PurplePBKDF2CipherPrivate *priv = PURPLE_PBKDF2_CIPHER_GET_PRIVATE(cipher);
+
+ g_return_if_fail(priv != NULL);
+
+ g_object_unref(priv->hash);
+ priv->hash = NULL;
+ priv->iter_count = 1;
+ priv->out_len = 256;
+
+ purple_cipher_reset_state(cipher);
}
static void
-purple_pbkdf2_uninit(PurpleCipherContext *context)
+purple_pbkdf2_cipher_reset_state(PurpleCipher *cipher)
{
- Pbkdf2Context *ctx_data;
+ PurplePBKDF2CipherPrivate *priv = PURPLE_PBKDF2_CIPHER_GET_PRIVATE(cipher);
- purple_cipher_context_reset(context, NULL);
+ g_return_if_fail(priv != NULL);
- ctx_data = purple_cipher_context_get_data(context);
- g_free(ctx_data);
- purple_cipher_context_set_data(context, NULL);
+ purple_cipher_set_salt(cipher, NULL, 0);
+ purple_cipher_set_key(cipher, NULL, 0);
+}
+
+static size_t
+purple_pbkdf2_cipher_get_digest_size(PurpleCipher *cipher)
+{
+ PurplePBKDF2CipherPrivate *priv = PURPLE_PBKDF2_CIPHER_GET_PRIVATE(cipher);
+
+ g_return_val_if_fail(priv != NULL, 0);
+
+ return priv->out_len;
}
static void
-purple_pbkdf2_reset(PurpleCipherContext *context, void *extra)
+purple_pbkdf2_cipher_set_salt(PurpleCipher *cipher, const guchar *salt, size_t len)
{
- Pbkdf2Context *ctx_data = purple_cipher_context_get_data(context);
+ PurplePBKDF2CipherPrivate *priv = PURPLE_PBKDF2_CIPHER_GET_PRIVATE(cipher);
- g_return_if_fail(ctx_data != NULL);
+ g_return_if_fail(priv != NULL);
- g_free(ctx_data->hash_func);
- ctx_data->hash_func = NULL;
- ctx_data->iter_count = 1;
- ctx_data->out_len = 256;
-
- purple_cipher_context_reset_state(context, extra);
-}
-
-static void
-purple_pbkdf2_reset_state(PurpleCipherContext *context, void *extra)
-{
- Pbkdf2Context *ctx_data = purple_cipher_context_get_data(context);
-
- g_return_if_fail(ctx_data != NULL);
-
- purple_cipher_context_set_salt(context, NULL, 0);
- purple_cipher_context_set_key(context, NULL, 0);
-}
-
-static void
-purple_pbkdf2_set_option(PurpleCipherContext *context, const gchar *name,
- void *value)
-{
- Pbkdf2Context *ctx_data = purple_cipher_context_get_data(context);
-
- g_return_if_fail(ctx_data != NULL);
-
- if (g_strcmp0(name, "hash") == 0) {
- g_free(ctx_data->hash_func);
- ctx_data->hash_func = g_strdup(value);
- return;
- }
-
- if (g_strcmp0(name, "iter_count") == 0) {
- ctx_data->iter_count = GPOINTER_TO_UINT(value);
- return;
- }
-
- if (g_strcmp0(name, "out_len") == 0) {
- ctx_data->out_len = GPOINTER_TO_UINT(value);
- return;
- }
-
- purple_debug_warning("pbkdf2", "Unknown option: %s\n",
- name ? name : "(null)");
-}
-
-static void *
-purple_pbkdf2_get_option(PurpleCipherContext *context, const gchar *name)
-{
- Pbkdf2Context *ctx_data = purple_cipher_context_get_data(context);
-
- g_return_val_if_fail(ctx_data != NULL, NULL);
-
- if (g_strcmp0(name, "hash") == 0)
- return ctx_data->hash_func;
-
- if (g_strcmp0(name, "iter_count") == 0)
- return GUINT_TO_POINTER(ctx_data->iter_count);
-
- if (g_strcmp0(name, "out_len") == 0)
- return GUINT_TO_POINTER(ctx_data->out_len);
-
- purple_debug_warning("pbkdf2", "Unknown option: %s\n",
- name ? name : "(null)");
- return NULL;
-}
-
-static size_t
-purple_pbkdf2_get_digest_size(PurpleCipherContext *context)
-{
- Pbkdf2Context *ctx_data = purple_cipher_context_get_data(context);
-
- g_return_val_if_fail(ctx_data != NULL, 0);
-
- return ctx_data->out_len;
-}
-
-static void
-purple_pbkdf2_set_salt(PurpleCipherContext *context, const guchar *salt, size_t len)
-{
- Pbkdf2Context *ctx_data = purple_cipher_context_get_data(context);
-
- g_return_if_fail(ctx_data != NULL);
-
- g_free(ctx_data->salt);
- ctx_data->salt = NULL;
- ctx_data->salt_len = 0;
+ g_free(priv->salt);
+ priv->salt = NULL;
+ priv->salt_len = 0;
if (len == 0)
return;
g_return_if_fail(salt != NULL);
- ctx_data->salt = g_memdup(salt, len);
- ctx_data->salt_len = len;
+ priv->salt = g_memdup(salt, len);
+ priv->salt_len = len;
}
static void
-purple_pbkdf2_set_key(PurpleCipherContext *context, const guchar *key,
+purple_pbkdf2_cipher_set_key(PurpleCipher *cipher, const guchar *key,
size_t len)
{
- Pbkdf2Context *ctx_data = purple_cipher_context_get_data(context);
+ PurplePBKDF2CipherPrivate *priv = PURPLE_PBKDF2_CIPHER_GET_PRIVATE(cipher);
- g_return_if_fail(ctx_data != NULL);
+ g_return_if_fail(priv != NULL);
- if (ctx_data->passphrase != NULL) {
- memset(ctx_data->passphrase, 0, ctx_data->passphrase_len);
- g_free(ctx_data->passphrase);
- ctx_data->passphrase = NULL;
+ if (priv->passphrase != NULL) {
+ memset(priv->passphrase, 0, priv->passphrase_len);
+ g_free(priv->passphrase);
+ priv->passphrase = NULL;
}
- ctx_data->passphrase_len = 0;
+ priv->passphrase_len = 0;
if (len == 0)
return;
g_return_if_fail(key != NULL);
- ctx_data->passphrase = g_memdup(key, len);
- ctx_data->passphrase_len = len;
+ priv->passphrase = g_memdup(key, len);
+ priv->passphrase_len = len;
}
/* inspired by gnutls 3.1.10, pbkdf2-sha1.c */
static gboolean
-purple_pbkdf2_digest(PurpleCipherContext *context, guchar digest[], size_t len)
+purple_pbkdf2_cipher_digest(PurpleCipher *cipher, guchar digest[], size_t len)
{
- Pbkdf2Context *ctx_data = purple_cipher_context_get_data(context);
+ PurplePBKDF2CipherPrivate *priv = PURPLE_PBKDF2_CIPHER_GET_PRIVATE(cipher);
+
guchar halfkey[PBKDF2_HASH_MAX_LEN], halfkey_hash[PBKDF2_HASH_MAX_LEN];
guint halfkey_len, halfkey_count, halfkey_pad, halfkey_no;
guchar *salt_ext;
size_t salt_ext_len;
guint iter_no;
- PurpleCipherContext *hash;
+ PurpleCipher *hash;
- g_return_val_if_fail(ctx_data != NULL, FALSE);
+ g_return_val_if_fail(priv != NULL, FALSE);
g_return_val_if_fail(digest != NULL, FALSE);
- g_return_val_if_fail(len >= ctx_data->out_len, FALSE);
+ g_return_val_if_fail(len >= priv->out_len, FALSE);
- g_return_val_if_fail(ctx_data->hash_func != NULL, FALSE);
- g_return_val_if_fail(ctx_data->iter_count > 0, FALSE);
- g_return_val_if_fail(ctx_data->passphrase != NULL ||
- ctx_data->passphrase_len == 0, FALSE);
- g_return_val_if_fail(ctx_data->salt != NULL || ctx_data->salt_len == 0,
+ g_return_val_if_fail(priv->hash != NULL, FALSE);
+ g_return_val_if_fail(priv->iter_count > 0, FALSE);
+ g_return_val_if_fail(priv->passphrase != NULL ||
+ priv->passphrase_len == 0, FALSE);
+ g_return_val_if_fail(priv->salt != NULL || priv->salt_len == 0,
FALSE);
- g_return_val_if_fail(ctx_data->out_len > 0, FALSE);
- g_return_val_if_fail(ctx_data->out_len < 0xFFFFFFFFU, FALSE);
+ g_return_val_if_fail(priv->out_len > 0, FALSE);
+ g_return_val_if_fail(priv->out_len < 0xFFFFFFFFU, FALSE);
- salt_ext_len = ctx_data->salt_len + 4;
More information about the Commits
mailing list