/soc/2013/ankitkv/gobjectification: f8fd1c60c22a: Merged PurpleC...
Ankit Vani
a at nevitus.org
Thu Jun 13 10:10:04 EDT 2013
Changeset: f8fd1c60c22ae8bacc488ce76cbed77b0c1892c2
Author: Ankit Vani <a at nevitus.org>
Date: 2013-06-13 19:39 +0530
Branch: soc.2013.gobjectification
URL: https://hg.pidgin.im/soc/2013/ankitkv/gobjectification/rev/f8fd1c60c22a
Description:
Merged PurpleCipher structure with the one in default branch
diffstat:
libpurple/cipher.c | 133 +++++++++++++++++++++++++++++-----------------------
libpurple/cipher.h | 34 +++++++-----
2 files changed, 92 insertions(+), 75 deletions(-)
diffs (truncated from 352 to 300 lines):
diff --git a/libpurple/cipher.c b/libpurple/cipher.c
--- a/libpurple/cipher.c
+++ b/libpurple/cipher.c
@@ -56,9 +56,11 @@ purple_cipher_class_init(PurpleCipherCla
GObjectClass *obj_class = G_OBJECT_CLASS(klass);
klass->reset = NULL;
+ klass->reset_state = NULL;
klass->set_iv = NULL;
klass->append = NULL;
klass->digest = NULL;
+ klass->get_digest_size = NULL;
klass->encrypt = NULL;
klass->decrypt = NULL;
klass->set_salt = NULL;
@@ -68,7 +70,6 @@ purple_cipher_class_init(PurpleCipherCla
klass->set_batch_mode = NULL;
klass->get_batch_mode = NULL;
klass->get_block_size = NULL;
- klass->set_key_with_len = NULL;
klass->get_name = NULL;
parent_class = g_type_class_peek_parent(klass);
@@ -156,6 +157,29 @@ purple_cipher_reset(PurpleCipher *cipher
}
/**
+ * 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.
+ */
+void
+purple_cipher_reset_state(PurpleCipher *cipher) {
+ PurpleCipherClass *klass = NULL;
+
+ g_return_if_fail(PURPLE_IS_CIPHER(cipher));
+
+ klass = PURPLE_CIPHER_GET_CLASS(cipher);
+
+ if(klass && klass->reset_state)
+ klass->reset_state(cipher);
+ else
+ purple_debug_warning("cipher", "the %s cipher does not implement the "
+ "reset_state method\n",
+ klass->get_name ? klass->get_name(cipher) : "");
+}
+
+/**
* purple_cipher_set_iv:
* @cipher: The cipher to set the IV to
* @iv: The initialization vector to set
@@ -221,8 +245,7 @@ purple_cipher_append(PurpleCipher *ciphe
* Return Value: TRUE if the digest was successful, FALSE otherwise.
*/
gboolean
-purple_cipher_digest(PurpleCipher *cipher, size_t in_len,
- guchar digest[], size_t *out_len)
+purple_cipher_digest(PurpleCipher *cipher, guchar digest[], size_t len)
{
PurpleCipherClass *klass = NULL;
@@ -231,7 +254,7 @@ purple_cipher_digest(PurpleCipher *ciphe
klass = PURPLE_CIPHER_GET_CLASS(cipher);
if(klass && klass->digest)
- return klass->digest(cipher, in_len, digest, out_len);
+ return klass->digest(cipher, digest, len);
else
purple_debug_warning("cipher", "the %s cipher does not implement the "
"digest method\n",
@@ -252,33 +275,51 @@ purple_cipher_digest(PurpleCipher *ciphe
* Return Value: TRUE if the digest was successful, FALSE otherwise.
*/
gboolean
-purple_cipher_digest_to_str(PurpleCipher *cipher, size_t in_len,
- gchar digest_s[], size_t *out_len)
+purple_cipher_digest_to_str(PurpleCipher *cipher, gchar digest_s[], size_t len)
{
/* 8k is a bit excessive, will tweak later. */
guchar digest[BUF_LEN * 4];
gint n = 0;
- size_t dlen = 0;
+ size_t digest_size;
g_return_val_if_fail(cipher, FALSE);
g_return_val_if_fail(digest_s, FALSE);
- if(!purple_cipher_digest(cipher, sizeof(digest), digest, &dlen))
+ digest_size = purple_cipher_get_digest_size(cipher);
+
+ g_return_val_if_fail(digest_size <= BUF_LEN * 4, FALSE);
+
+ if(!purple_cipher_digest(cipher, digest, sizeof(digest)))
return FALSE;
- /* in_len must be greater than dlen * 2 so we have room for the NUL. */
- if(in_len <= dlen * 2)
- return FALSE;
+ /* Every digest byte occupies 2 chars + the NUL at the end. */
+ g_return_val_if_fail(digest_size * 2 + 1 <= len, FALSE);
- for(n = 0; n < dlen; n++)
+ for(n = 0; n < digest_size; n++)
sprintf(digest_s + (n * 2), "%02x", digest[n]);
digest_s[n * 2] = '\0';
- if(out_len)
- *out_len = dlen * 2;
+ return TRUE;
+}
- return TRUE;
+size_t
+purple_cipher_get_digest_size(PurpleCipher *cipher)
+{
+ PurpleCipherClass *klass = NULL;
+
+ g_return_val_if_fail(PURPLE_IS_CIPHER(cipher), FALSE);
+
+ klass = PURPLE_CIPHER_GET_CLASS(cipher);
+
+ if(klass && klass->purple_cipher_get_digest_size)
+ return klass->purple_cipher_get_digest_size(cipher);
+ else
+ purple_debug_warning("cipher", "the %s cipher does not implement the "
+ "get_digest_size method\n",
+ klass->get_name ? klass->get_name(cipher) : "");
+
+ return FALSE;
}
/**
@@ -293,26 +334,26 @@ purple_cipher_digest_to_str(PurpleCipher
*
* Return Value: A cipher specific status code
*/
-gint
-purple_cipher_encrypt(PurpleCipher *cipher, const guchar data[],
- size_t len, guchar output[], size_t *outlen)
+ssize_t
+purple_cipher_encrypt(PurpleCipher *cipher, const guchar input[],
+ size_t in_len, guchar output[], size_t out_size)
{
PurpleCipherClass *klass = NULL;
g_return_val_if_fail(PURPLE_IS_CIPHER(cipher), -1);
+ g_return_val_if_fail(input != NULL, -1);
+ g_return_val_if_fail(output != NULL, -1);
+ g_return_val_if_fail(out_size >= in_len, -1);
klass = PURPLE_CIPHER_GET_CLASS(cipher);
if(klass && klass->encrypt)
- return klass->encrypt(cipher, data, len, output, outlen);
+ return klass->encrypt(cipher, input, in_len, output, out_size);
else
purple_debug_warning("cipher", "the %s cipher does not implement the "
"encrypt method\n",
klass->get_name ? klass->get_name(cipher) : "");
- if(outlen)
- *outlen = -1;
-
return -1;
}
@@ -328,26 +369,25 @@ purple_cipher_encrypt(PurpleCipher *ciph
*
* Return Value: A cipher specific status code
*/
-gint
-purple_cipher_decrypt(PurpleCipher *cipher, const guchar data[],
- size_t len, guchar output[], size_t *outlen)
+ssize_t
+purple_cipher_decrypt(PurpleCipher *cipher, const guchar input[],
+ size_t in_len, guchar output[], size_t out_size)
{
PurpleCipherClass *klass = NULL;
g_return_val_if_fail(PURPLE_IS_CIPHER(cipher), -1);
+ g_return_val_if_fail(input != NULL, -1);
+ g_return_val_if_fail(output != NULL, -1);
klass = PURPLE_CIPHER_GET_CLASS(cipher);
if(klass && klass->decrypt)
- return klass->decrypt(cipher, data, len, output, outlen);
+ return klass->decrypt(cipher, input, in_len, output, out_size);
else
purple_debug_warning("cipher", "the %s cipher does not implement the "
"decrypt method\n",
klass->get_name ? klass->get_name(cipher) : "");
- if(outlen)
- *outlen = -1;
-
return -1;
}
@@ -359,7 +399,7 @@ purple_cipher_decrypt(PurpleCipher *ciph
* Sets the salt on a cipher
*/
void
-purple_cipher_set_salt(PurpleCipher *cipher, guchar *salt) {
+purple_cipher_set_salt(PurpleCipher *cipher, const guchar *salt, size_t len) {
PurpleCipherClass *klass = NULL;
g_return_if_fail(PURPLE_IS_CIPHER(cipher));
@@ -367,7 +407,7 @@ purple_cipher_set_salt(PurpleCipher *cip
klass = PURPLE_CIPHER_GET_CLASS(cipher);
if(klass && klass->set_salt)
- klass->set_salt(cipher, salt);
+ klass->set_salt(cipher, salt, len);
else
purple_debug_warning("cipher", "the %s cipher does not implement the "
"set_salt method\n",
@@ -408,7 +448,7 @@ purple_cipher_get_salt_size(PurpleCipher
* Sets the key on a cipher
*/
void
-purple_cipher_set_key(PurpleCipher *cipher, const guchar *key) {
+purple_cipher_set_key(PurpleCipher *cipher, const guchar *key, size_t len) {
PurpleCipherClass *klass = NULL;
g_return_if_fail(PURPLE_IS_CIPHER(cipher));
@@ -416,7 +456,7 @@ purple_cipher_set_key(PurpleCipher *ciph
klass = PURPLE_CIPHER_GET_CLASS(cipher);
if(klass && klass->set_key)
- klass->set_key(cipher, key);
+ klass->set_key(cipher, key, len);
else
purple_debug_warning("cipher", "the %s cipher does not implement the "
"set_key method\n",
@@ -527,30 +567,3 @@ purple_cipher_get_block_size(PurpleCiphe
return -1;
}
-
-/**
- * purple_cipher_set_key_with_len:
- * @cipher: The cipher whose key to set
- * @key: The key
- * @len: The length of the key
- *
- * Sets the key with a given length on a cipher
- */
-void
-purple_cipher_set_key_with_len(PurpleCipher *cipher,
- const guchar *key, size_t len)
-{
- PurpleCipherClass *klass = NULL;
-
- g_return_if_fail(PURPLE_IS_CIPHER(cipher));
- g_return_if_fail(key);
-
- klass = PURPLE_CIPHER_GET_CLASS(cipher);
-
- if(klass && klass->set_key_with_len)
- klass->set_key_with_len(cipher, key, len);
- else
- purple_debug_warning("cipher", "the %s cipher does not implement the "
- "set_key_with_len method\n",
- klass->get_name ? klass->get_name(cipher) : "");
-}
diff --git a/libpurple/cipher.h b/libpurple/cipher.h
--- a/libpurple/cipher.h
+++ b/libpurple/cipher.h
@@ -83,6 +83,9 @@ struct _PurpleCipherClass {
/** The reset function */
void (*reset)(PurpleCipher *cipher);
+ /** The reset state function */
+ void (*reset_state)(PurpleCipher *cipher);
+
/** The set initialization vector function */
void (*set_iv)(PurpleCipher *cipher, guchar *iv, size_t len);
@@ -90,22 +93,25 @@ struct _PurpleCipherClass {
void (*append)(PurpleCipher *cipher, const guchar *data, size_t len);
/** The digest function */
- gboolean (*digest)(PurpleCipher *cipher, size_t in_len, guchar digest[], size_t *out_len);
+ gboolean (*digest)(PurpleCipher *cipher, guchar digest[], size_t len);
+
+ /** The get digest size function */
+ size_t (*get_digest_size)(PurpleCipher *cipher);
/** The encrypt function */
- gint (*encrypt)(PurpleCipher *cipher, const guchar data[], size_t len, guchar output[], size_t *outlen);
+ ssize_t (*encrypt)(PurpleCipher *cipher, const guchar input[], size_t in_len, guchar output[], size_t out_size);
/** The decrypt function */
- gint (*decrypt)(PurpleCipher *cipher, const guchar data[], size_t len, guchar output[], size_t *outlen);
+ ssize_t (*decrypt)(PurpleCipher *cipher, const guchar input[], size_t in_len, guchar output[], size_t out_size);
More information about the Commits
mailing list