/soc/2013/ankitkv/gobjectification: 5c2f894fe4d6: GObjectified t...
Ankit Vani
a at nevitus.org
Fri Jun 14 12:33:45 EDT 2013
Changeset: 5c2f894fe4d6032a1eddd7c3d8547d51457ef5f0
Author: Ankit Vani <a at nevitus.org>
Date: 2013-06-14 22:03 +0530
Branch: soc.2013.gobjectification
URL: https://hg.pidgin.im/soc/2013/ankitkv/gobjectification/rev/5c2f894fe4d6
Description:
GObjectified the AES cipher as PurpleAESCipher
diffstat:
libpurple/ciphers/aes.c | 403 +++++++++++++++++++++++++++++------------------
libpurple/ciphers/aes.h | 67 +++++++
2 files changed, 311 insertions(+), 159 deletions(-)
diffs (truncated from 760 to 300 lines):
diff --git a/libpurple/ciphers/aes.c b/libpurple/ciphers/aes.c
--- a/libpurple/ciphers/aes.c
+++ b/libpurple/ciphers/aes.c
@@ -22,11 +22,11 @@
* Written by Tomek Wasilczyk <tomkiewicz at cpw.pidgin.im>
*/
-#include "internal.h"
-#include "cipher.h"
-#include "ciphers.h"
+#include "aes.h"
#include "debug.h"
+#include <string.h>
+
#if defined(HAVE_GNUTLS)
# define PURPLE_AES_USE_GNUTLS 1
# include <gnutls/gnutls.h>
@@ -43,102 +43,93 @@
/* 128bit */
#define PURPLE_AES_BLOCK_SIZE 16
-typedef struct
-{
+/******************************************************************************
+ * Structs
+ *****************************************************************************/
+#define PURPLE_AES_CIPHER_GET_PRIVATE(obj) \
+ (G_TYPE_INSTANCE_GET_PRIVATE((obj), PURPLE_TYPE_AES_CIPHER, PurpleAESCipherPrivate))
+
+typedef struct {
guchar iv[PURPLE_AES_BLOCK_SIZE];
guchar key[32];
guint key_size;
gboolean failure;
-} AESContext;
+} PurpleAESCipherPrivate;
-typedef gboolean (*purple_aes_crypt_func)(
+/******************************************************************************
+ * Enums
+ *****************************************************************************/
+enum {
+ PROP_NONE,
+ PROP_BATCH_MODE,
+ PROP_IV,
+ PROP_KEY,
+ PROP_LAST,
+};
+
+/******************************************************************************
+ * Cipher Stuff
+ *****************************************************************************/
+
+typedef gboolean (*purple_aes_cipher_crypt_func)(
const guchar *input, guchar *output, size_t len,
guchar iv[PURPLE_AES_BLOCK_SIZE], guchar key[32], guint key_size);
static void
-purple_aes_init(PurpleCipherContext *context, void *extra)
+purple_aes_cipher_reset(PurpleCipher *cipher)
{
- AESContext *ctx_data;
+ PurpleAESCipherPrivate *priv = PURPLE_AES_CIPHER_GET_PRIVATE(cipher);
- ctx_data = g_new0(AESContext, 1);
- purple_cipher_context_set_data(context, ctx_data);
+ g_return_if_fail(priv != NULL);
- purple_cipher_context_reset(context, extra);
+ memset(priv->iv, 0, sizeof(priv->iv));
+ memset(priv->key, 0, sizeof(priv->key));
+ priv->key_size = 32; /* 256bit */
+ priv->failure = FALSE;
}
static void
-purple_aes_uninit(PurpleCipherContext *context)
+purple_aes_cipher_set_iv(PurpleCipher *cipher, guchar *iv, size_t len)
{
- AESContext *ctx_data;
-
- purple_cipher_context_reset(context, NULL);
-
- ctx_data = purple_cipher_context_get_data(context);
- g_free(ctx_data);
- purple_cipher_context_set_data(context, NULL);
-}
-
-static void
-purple_aes_reset(PurpleCipherContext *context, void *extra)
-{
- AESContext *ctx_data = purple_cipher_context_get_data(context);
-
- g_return_if_fail(ctx_data != NULL);
-
- memset(ctx_data->iv, 0, sizeof(ctx_data->iv));
- memset(ctx_data->key, 0, sizeof(ctx_data->key));
- ctx_data->key_size = 32; /* 256bit */
- ctx_data->failure = FALSE;
-}
-
-static void
-purple_aes_set_option(PurpleCipherContext *context, const gchar *name,
- void *value)
-{
- AESContext *ctx_data = purple_cipher_context_get_data(context);
-
- purple_debug_error("cipher-aes", "set_option not supported\n");
- ctx_data->failure = TRUE;
-}
-
-static void
-purple_aes_set_iv(PurpleCipherContext *context, guchar *iv, size_t len)
-{
- AESContext *ctx_data = purple_cipher_context_get_data(context);
+ PurpleAESCipherPrivate *priv = PURPLE_AES_CIPHER_GET_PRIVATE(cipher);
if ((len > 0 && iv == NULL) ||
- (len != 0 && len != sizeof(ctx_data->iv))) {
+ (len != 0 && len != sizeof(priv->iv))) {
purple_debug_error("cipher-aes", "invalid IV length\n");
- ctx_data->failure = TRUE;
+ priv->failure = TRUE;
return;
}
if (len == 0)
- memset(ctx_data->iv, 0, sizeof(ctx_data->iv));
+ memset(priv->iv, 0, sizeof(priv->iv));
else
- memcpy(ctx_data->iv, iv, len);
+ memcpy(priv->iv, iv, len);
+
+ g_object_notify(G_OBJECT(cipher), "iv");
}
static void
-purple_aes_set_key(PurpleCipherContext *context, const guchar *key, size_t len)
+purple_aes_cipher_set_key(PurpleCipher *cipher, const guchar *key, size_t len)
{
- AESContext *ctx_data = purple_cipher_context_get_data(context);
+ PurpleAESCipherPrivate *priv = PURPLE_AES_CIPHER_GET_PRIVATE(cipher);
if ((len > 0 && key == NULL) ||
(len != 0 && len != 16 && len != 24 && len != 32)) {
purple_debug_error("cipher-aes", "invalid key length\n");
- ctx_data->failure = TRUE;
+ priv->failure = TRUE;
return;
}
- ctx_data->key_size = len;
- memset(ctx_data->key, 0, sizeof(ctx_data->key));
+ priv->key_size = len;
+ memset(priv->key, 0, sizeof(priv->key));
if (len > 0)
- memcpy(ctx_data->key, key, len);
+ memcpy(priv->key, key, len);
+
+ g_object_notify(G_OBJECT(cipher), "key");
}
static guchar *
-purple_aes_pad_pkcs7(const guchar input[], size_t in_len, size_t *out_len)
+purple_aes_cipher_pad_pkcs7(const guchar input[], size_t in_len, size_t *out_len)
{
int padding_len, total_len;
guchar *padded;
@@ -160,7 +151,7 @@ purple_aes_pad_pkcs7(const guchar input[
}
static ssize_t
-purple_aes_unpad_pkcs7(guchar input[], size_t in_len)
+purple_aes_cipher_unpad_pkcs7(guchar input[], size_t in_len)
{
int padding_len, i;
size_t out_len;
@@ -172,7 +163,7 @@ purple_aes_unpad_pkcs7(guchar input[], s
if (padding_len <= 0 || padding_len > PURPLE_AES_BLOCK_SIZE ||
padding_len > in_len) {
purple_debug_warning("cipher-aes",
- "Invalid padding length: %d (total %d) - "
+ "Invalid padding length: %d (total %lu) - "
"most probably, the key was invalid\n",
padding_len, in_len);
return -1;
@@ -197,7 +188,7 @@ purple_aes_unpad_pkcs7(guchar input[], s
#ifdef PURPLE_AES_USE_GNUTLS
static gnutls_cipher_hd_t
-purple_aes_crypt_gnutls_init(guchar iv[PURPLE_AES_BLOCK_SIZE], guchar key[32],
+purple_aes_cipher_gnutls_crypt_init(guchar iv[PURPLE_AES_BLOCK_SIZE], guchar key[32],
guint key_size)
{
gnutls_cipher_hd_t handle;
@@ -231,17 +222,17 @@ purple_aes_crypt_gnutls_init(guchar iv[P
}
static gboolean
-purple_aes_encrypt_gnutls(const guchar *input, guchar *output, size_t len,
+purple_aes_cipher_gnutls_encrypt(const guchar *input, guchar *output, size_t len,
guchar iv[PURPLE_AES_BLOCK_SIZE], guchar key[32], guint key_size)
{
gnutls_cipher_hd_t handle;
int ret;
- handle = purple_aes_crypt_gnutls_init(iv, key, key_size);
+ handle = purple_aes_cipher_gnutls_crypt_init(iv, key, key_size);
if (handle == NULL)
return FALSE;
- ret = gnutls_cipher_encrypt2(handle, input, len, output, len);
+ ret = gnutls_cipher_encrypt2(handle, (guchar *) input, len, output, len);
gnutls_cipher_deinit(handle);
if (ret != 0) {
@@ -254,13 +245,13 @@ purple_aes_encrypt_gnutls(const guchar *
}
static gboolean
-purple_aes_decrypt_gnutls(const guchar *input, guchar *output, size_t len,
+purple_aes_cipher_gnutls_decrypt(const guchar *input, guchar *output, size_t len,
guchar iv[PURPLE_AES_BLOCK_SIZE], guchar key[32], guint key_size)
{
gnutls_cipher_hd_t handle;
int ret;
- handle = purple_aes_crypt_gnutls_init(iv, key, key_size);
+ handle = purple_aes_cipher_gnutls_crypt_init(iv, key, key_size);
if (handle == NULL)
return FALSE;
@@ -280,44 +271,43 @@ purple_aes_decrypt_gnutls(const guchar *
#ifdef PURPLE_AES_USE_NSS
-typedef struct
-{
+typedef struct {
PK11SlotInfo *slot;
PK11SymKey *sym_key;
SECItem *sec_param;
PK11Context *enc_context;
-} purple_aes_encrypt_nss_context;
+} PurpleAESCipherNSSContext;
static void
-purple_aes_encrypt_nss_context_cleanup(purple_aes_encrypt_nss_context *ctx)
+purple_aes_cipher_nss_cleanup(PurpleAESCipherNSSContext *context)
{
- g_return_if_fail(ctx != NULL);
+ g_return_if_fail(context != NULL);
- if (ctx->enc_context != NULL)
- PK11_DestroyContext(ctx->enc_context, TRUE);
- if (ctx->sec_param != NULL)
- SECITEM_FreeItem(ctx->sec_param, TRUE);
- if (ctx->sym_key != NULL)
- PK11_FreeSymKey(ctx->sym_key);
- if (ctx->slot != NULL)
- PK11_FreeSlot(ctx->slot);
+ if (context->enc_context != NULL)
+ PK11_DestroyContext(context->enc_context, TRUE);
+ if (context->sec_param != NULL)
+ SECITEM_FreeItem(context->sec_param, TRUE);
+ if (context->sym_key != NULL)
+ PK11_FreeSymKey(context->sym_key);
+ if (context->slot != NULL)
+ PK11_FreeSlot(context->slot);
- memset(ctx, 0, sizeof(purple_aes_encrypt_nss_context));
+ memset(context, 0, sizeof(PurpleAESCipherNSSContext));
}
static gboolean
-purple_aes_crypt_nss(const guchar *input, guchar *output, size_t len,
+purple_aes_cipher_nss_crypt(const guchar *input, guchar *output, size_t len,
guchar iv[PURPLE_AES_BLOCK_SIZE], guchar key[32], guint key_size,
CK_ATTRIBUTE_TYPE operation)
{
- purple_aes_encrypt_nss_context ctx;
+ PurpleAESCipherNSSContext context;
CK_MECHANISM_TYPE cipher_mech = CKM_AES_CBC;
SECItem key_item, iv_item;
SECStatus ret;
int outlen = 0;
unsigned int outlen_tmp = 0;
- memset(&ctx, 0, sizeof(purple_aes_encrypt_nss_context));
+ memset(&context, 0, sizeof(PurpleAESCipherNSSContext));
if (NSS_NoDB_Init(NULL) != SECSuccess) {
purple_debug_error("cipher-aes",
@@ -325,8 +315,8 @@ purple_aes_crypt_nss(const guchar *input
return FALSE;
}
- ctx.slot = PK11_GetBestSlot(cipher_mech, NULL);
- if (ctx.slot == NULL) {
More information about the Commits
mailing list