/pidgin/main: 5749f2724b12: ciphers cleanup: get key size

Tomasz Wasilczyk tomkiewicz at cpw.pidgin.im
Sun May 5 09:37:33 EDT 2013


Changeset: 5749f2724b12d6da18cd3a86698f40f634833ff4
Author:	 Tomasz Wasilczyk <tomkiewicz at cpw.pidgin.im>
Date:	 2013-05-05 15:37 +0200
Branch:	 default
URL: https://hg.pidgin.im/pidgin/main/rev/5749f2724b12

Description:

ciphers cleanup: get key size

diffstat:

 libpurple/cipher.c            |  21 +++++++++++++++++++
 libpurple/cipher.h            |  47 +++++++++++++++++++++++++++---------------
 libpurple/ciphers/des.c       |  14 +++++++++++-
 libpurple/ciphers/gchecksum.c |   1 +
 libpurple/ciphers/hmac.c      |   1 +
 libpurple/ciphers/md4.c       |   1 +
 libpurple/ciphers/rc4.c       |   1 +
 7 files changed, 68 insertions(+), 18 deletions(-)

diffs (199 lines):

diff --git a/libpurple/cipher.c b/libpurple/cipher.c
--- a/libpurple/cipher.c
+++ b/libpurple/cipher.c
@@ -108,6 +108,8 @@ purple_cipher_get_capabilities(PurpleCip
 		caps |= PURPLE_CIPHER_CAPS_GET_SALT_SIZE;
 	if(ops->set_key)
 		caps |= PURPLE_CIPHER_CAPS_SET_KEY;
+	if (ops->get_key_size)
+		caps |= PURPLE_CIPHER_CAPS_GET_KEY_SIZE;
 	if(ops->set_batch_mode)
 		caps |= PURPLE_CIPHER_CAPS_SET_BATCH_MODE;
 	if(ops->get_batch_mode)
@@ -570,6 +572,25 @@ purple_cipher_context_set_key(PurpleCiph
 						"set_key operation\n", cipher->name);
 }
 
+size_t
+purple_cipher_context_get_key_size(PurpleCipherContext *context) {
+	PurpleCipher *cipher = NULL;
+
+	g_return_val_if_fail(context, 0);
+
+	cipher = context->cipher;
+	g_return_val_if_fail(cipher, 0);
+
+	if (cipher->ops && cipher->ops->get_key_size)
+		return cipher->ops->get_key_size(context);
+	else {
+		purple_debug_warning("cipher", "the %s cipher does not support "
+			"the get_key_size operation\n", cipher->name);
+
+		return 0;
+	}
+}
+
 void
 purple_cipher_context_set_batch_mode(PurpleCipherContext *context,
                                      PurpleCipherBatchMode mode)
diff --git a/libpurple/cipher.h b/libpurple/cipher.h
--- a/libpurple/cipher.h
+++ b/libpurple/cipher.h
@@ -50,24 +50,25 @@ typedef enum {
  * The operation flags for a cipher
  */
 typedef enum {
-	PURPLE_CIPHER_CAPS_SET_OPT          = 1 << 1,   /**< Set option flag	*/
-	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_SET_OPT          = 1 << 1,   /**< Set option flag */
+	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_SET_BATCH_MODE   = 1 << 15,  /**< Set batch mode flag */
-	PURPLE_CIPHER_CAPS_GET_BATCH_MODE   = 1 << 16,  /**< Get batch mode flag */
-	PURPLE_CIPHER_CAPS_GET_BLOCK_SIZE   = 1 << 17,  /**< The get block size flag */
-	PURPLE_CIPHER_CAPS_UNKNOWN          = 1 << 18   /**< Unknown			*/
+	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 */
 } PurpleCipherCaps;
 
 /**
@@ -116,6 +117,9 @@ struct _PurpleCipherOps {
 	/** The set key function */
 	void (*set_key)(PurpleCipherContext *context, const guchar *key, size_t len);
 
+	/** The get key size function */
+	size_t (*get_key_size)(PurpleCipherContext *context);
+
 	/** The set batch mode function */
 	void (*set_batch_mode)(PurpleCipherContext *context, PurpleCipherBatchMode mode);
 
@@ -394,6 +398,15 @@ size_t purple_cipher_context_get_salt_si
 void purple_cipher_context_set_key(PurpleCipherContext *context, const guchar *key, size_t len);
 
 /**
+ * Gets the size of the key if the cipher supports it
+ *
+ * @param context The context whose key size to get
+ *
+ * @return The size of the key
+ */
+size_t purple_cipher_context_get_key_size(PurpleCipherContext *context);
+
+/**
  * Sets the batch mode of a context
  *
  * @param context The context whose batch mode to set
diff --git a/libpurple/ciphers/des.c b/libpurple/ciphers/des.c
--- a/libpurple/ciphers/des.c
+++ b/libpurple/ciphers/des.c
@@ -351,6 +351,11 @@ des_set_key (PurpleCipherContext *contex
 	}
 }
 
+static size_t
+des_get_key_size(PurpleCipherContext *context)
+{
+	return 8;
+}
 
 /*
  *  Electronic Codebook Mode DES encryption/decryption of data according
@@ -477,7 +482,7 @@ static PurpleCipherOps DESOps = {
 	NULL,              /* set salt */
 	NULL,              /* get salt size */
 	des_set_key,       /* set key */
-	NULL,              /* get key size */
+	des_get_key_size,  /* get key size */
 	NULL,              /* set batch mode */
 	NULL,              /* get batch mode */
 	NULL,              /* get block size */
@@ -527,6 +532,12 @@ des3_set_key(PurpleCipherContext *contex
 	}
 }
 
+static size_t
+des3_get_key_size(PurpleCipherContext *context)
+{
+	return 24;
+}
+
 static gint
 des3_ecb_encrypt(struct _des3_ctx *ctx, const guchar data[],
                  size_t len, guchar output[], size_t *outlen)
@@ -830,6 +841,7 @@ static PurpleCipherOps DES3Ops = {
 	NULL,              /* set salt */
 	NULL,              /* get salt size */
 	des3_set_key,      /* set key */
+	des3_get_key_size, /* get_key_size */
 	des3_set_batch,    /* set batch mode */
 	des3_get_batch,    /* get batch mode */
 	NULL,              /* get block size */
diff --git a/libpurple/ciphers/gchecksum.c b/libpurple/ciphers/gchecksum.c
--- a/libpurple/ciphers/gchecksum.c
+++ b/libpurple/ciphers/gchecksum.c
@@ -121,6 +121,7 @@ purple_g_checksum_digest(PurpleCipherCon
 		NULL,                     /* set salt */         \
 		NULL,                     /* get salt size */    \
 		NULL,                     /* set key */          \
+		NULL,                     /* get key size */     \
 		NULL,                     /* set batch mode */   \
 		NULL,                     /* get batch mode */   \
 		lower##_get_block_size,   /* get block size */   \
diff --git a/libpurple/ciphers/hmac.c b/libpurple/ciphers/hmac.c
--- a/libpurple/ciphers/hmac.c
+++ b/libpurple/ciphers/hmac.c
@@ -216,6 +216,7 @@ static PurpleCipherOps HMACOps = {
 	NULL,                   /* set salt */
 	NULL,                   /* get salt size */
 	hmac_set_key,           /* set key */
+	NULL,                   /* get key size */
 	NULL,                   /* set batch mode */
 	NULL,                   /* get batch mode */
 	hmac_get_block_size,    /* get block size */
diff --git a/libpurple/ciphers/md4.c b/libpurple/ciphers/md4.c
--- a/libpurple/ciphers/md4.c
+++ b/libpurple/ciphers/md4.c
@@ -289,6 +289,7 @@ static PurpleCipherOps MD4Ops = {
 	NULL,                   /* set salt */
 	NULL,                   /* get salt size */
 	NULL,                   /* set key */
+	NULL,                   /* get key size */
 	NULL,                   /* set batch mode */
 	NULL,                   /* get batch mode */
 	md4_get_block_size,     /* get block size */
diff --git a/libpurple/ciphers/rc4.c b/libpurple/ciphers/rc4.c
--- a/libpurple/ciphers/rc4.c
+++ b/libpurple/ciphers/rc4.c
@@ -143,6 +143,7 @@ static PurpleCipherOps RC4Ops = {
 	NULL,          /* set salt      */
 	NULL,          /* get salt size */
 	rc4_set_key,   /* set key       */
+	NULL,          /* get key size */
 	NULL,          /* set batch mode */
 	NULL,          /* get batch mode */
 	NULL,          /* get block size */



More information about the Commits mailing list