diff options
Diffstat (limited to 'libpurple')
-rw-r--r-- | libpurple/cipher.c | 80 | ||||
-rw-r--r-- | libpurple/cipher.h | 13 | ||||
-rw-r--r-- | libpurple/ciphers/Makefile.am | 1 | ||||
-rw-r--r-- | libpurple/ciphers/gchecksum.c | 132 | ||||
-rw-r--r-- | libpurple/ciphers/md5.c | 53 | ||||
-rw-r--r-- | libpurple/ciphers/sha1.c | 53 | ||||
-rw-r--r-- | libpurple/ciphers/sha256.c | 54 |
7 files changed, 165 insertions, 221 deletions
diff --git a/libpurple/cipher.c b/libpurple/cipher.c index 601602630d..d048644d6f 100644 --- a/libpurple/cipher.c +++ b/libpurple/cipher.c @@ -40,86 +40,6 @@ #include "signals.h" #include "value.h" -#if GLIB_CHECK_VERSION(2,16,0) -void -purple_g_checksum_init(PurpleCipherContext *context, GChecksumType type) -{ - GChecksum *checksum; - - checksum = g_checksum_new(type); - purple_cipher_context_set_data(context, checksum); -} - -void -purple_g_checksum_reset(PurpleCipherContext *context, GChecksumType type) -{ - GChecksum *checksum; - - checksum = purple_cipher_context_get_data(context); - g_return_if_fail(checksum != NULL); - -#if GLIB_CHECK_VERSION(2,18,0) - g_checksum_reset(checksum); -#else - g_checksum_free(checksum); - checksum = g_checksum_new(type); - purple_cipher_context_set_data(context, checksum); -#endif -} - -void -purple_g_checksum_uninit(PurpleCipherContext *context) -{ - GChecksum *checksum; - - checksum = purple_cipher_context_get_data(context); - g_return_if_fail(checksum != NULL); - - g_checksum_free(checksum); -} - -void -purple_g_checksum_append(PurpleCipherContext *context, const guchar *data, - gsize len) -{ - GChecksum *checksum; - - checksum = purple_cipher_context_get_data(context); - g_return_if_fail(checksum != NULL); - - while (len >= G_MAXSSIZE) { - g_checksum_update(checksum, data, G_MAXSSIZE); - len -= G_MAXSSIZE; - data += G_MAXSSIZE; - } - - if (len) - g_checksum_update(checksum, data, len); -} - -gboolean -purple_g_checksum_digest(PurpleCipherContext *context, GChecksumType type, - gsize len, guchar *digest, gsize *out_len) -{ - GChecksum *checksum; - const gssize required_length = g_checksum_type_get_length(type); - - checksum = purple_cipher_context_get_data(context); - - g_return_val_if_fail(len >= required_length, FALSE); - g_return_val_if_fail(checksum != NULL, FALSE); - - g_checksum_get_digest(checksum, digest, &len); - - purple_cipher_context_reset(context, NULL); - - if (out_len) - *out_len = len; - - return TRUE; -} -#endif - /******************************************************************************* * Structs ******************************************************************************/ diff --git a/libpurple/cipher.h b/libpurple/cipher.h index 6826a83983..b6479c69db 100644 --- a/libpurple/cipher.h +++ b/libpurple/cipher.h @@ -496,19 +496,6 @@ gchar *purple_cipher_http_digest_calculate_response( const gchar *session_key); /*@}*/ -/*****************************************************************************/ -/** @name Purple Cipher GChecksum compatibility */ -/*****************************************************************************/ -/*@{*/ -#if GLIB_CHECK_VERSION(2,16,0) -void purple_g_checksum_init(PurpleCipherContext *context, GChecksumType type); -void purple_g_checksum_reset(PurpleCipherContext *context, GChecksumType type); -void purple_g_checksum_uninit(PurpleCipherContext *context); -void purple_g_checksum_append(PurpleCipherContext *context, const guchar *data, gsize len); -gboolean purple_g_checksum_digest(PurpleCipherContext *context, GChecksumType type, gsize len, guchar *digest, gsize *out_len); - -#endif /* GLIB_CHECK_VERSION(2,16,0) */ -/*@}*/ G_END_DECLS diff --git a/libpurple/ciphers/Makefile.am b/libpurple/ciphers/Makefile.am index ec53734a74..332f8eb62f 100644 --- a/libpurple/ciphers/Makefile.am +++ b/libpurple/ciphers/Makefile.am @@ -2,6 +2,7 @@ noinst_LTLIBRARIES=libpurple-ciphers.la libpurple_ciphers_la_SOURCES=\ des.c \ + gchecksum.c \ hmac.c \ md4.c \ md5.c \ diff --git a/libpurple/ciphers/gchecksum.c b/libpurple/ciphers/gchecksum.c new file mode 100644 index 0000000000..299c803401 --- /dev/null +++ b/libpurple/ciphers/gchecksum.c @@ -0,0 +1,132 @@ +#include <cipher.h> + +#if GLIB_CHECK_VERSION(2,16,0) + +void +purple_g_checksum_init(PurpleCipherContext *context, GChecksumType type) +{ + GChecksum *checksum; + + checksum = g_checksum_new(type); + purple_cipher_context_set_data(context, checksum); +} + +void +purple_g_checksum_reset(PurpleCipherContext *context, GChecksumType type) +{ + GChecksum *checksum; + + checksum = purple_cipher_context_get_data(context); + g_return_if_fail(checksum != NULL); + +#if GLIB_CHECK_VERSION(2,18,0) + g_checksum_reset(checksum); +#else + g_checksum_free(checksum); + checksum = g_checksum_new(type); + purple_cipher_context_set_data(context, checksum); +#endif +} + +void +purple_g_checksum_uninit(PurpleCipherContext *context) +{ + GChecksum *checksum; + + checksum = purple_cipher_context_get_data(context); + g_return_if_fail(checksum != NULL); + + g_checksum_free(checksum); +} + +void +purple_g_checksum_append(PurpleCipherContext *context, const guchar *data, + gsize len) +{ + GChecksum *checksum; + + checksum = purple_cipher_context_get_data(context); + g_return_if_fail(checksum != NULL); + + while (len >= G_MAXSSIZE) { + g_checksum_update(checksum, data, G_MAXSSIZE); + len -= G_MAXSSIZE; + data += G_MAXSSIZE; + } + + if (len) + g_checksum_update(checksum, data, len); +} + +gboolean +purple_g_checksum_digest(PurpleCipherContext *context, GChecksumType type, + gsize len, guchar *digest, gsize *out_len) +{ + GChecksum *checksum; + const gssize required_length = g_checksum_type_get_length(type); + + checksum = purple_cipher_context_get_data(context); + + g_return_val_if_fail(len >= required_length, FALSE); + g_return_val_if_fail(checksum != NULL, FALSE); + + g_checksum_get_digest(checksum, digest, &len); + + purple_cipher_context_reset(context, NULL); + + if (out_len) + *out_len = len; + + return TRUE; +} + +/****************************************************************************** + * Macros + *****************************************************************************/ +#define PURPLE_G_CHECKSUM_IMPLEMENTATION(lower, camel, type, block_size) \ + static size_t \ + lower##_get_block_size(PurpleCipherContext *context) { \ + return (block_size); \ + } \ + \ + static void \ + lower##_init(PurpleCipherContext *context, gpointer extra) { \ + purple_g_checksum_init(context, (type)); \ + } \ + \ + static void \ + lower##_reset(PurpleCipherContext *context, gpointer extra) { \ + purple_g_checksum_reset(context, (type)); \ + } \ + \ + static gboolean \ + lower##_digest(PurpleCipherContext *context, gsize in_len, \ + guchar digest[], size_t *out_len) \ + { \ + return purple_g_checksum_digest(context, (type), in_len, digest, \ + out_len); \ + } \ + \ + static PurpleCipherOps camel##Ops = { \ + .init = lower##_init, \ + .reset = lower##_reset, \ + .uninit = purple_g_checksum_uninit, \ + .append = purple_g_checksum_append, \ + .digest = lower##_digest, \ + .get_block_size = lower##_get_block_size, \ + }; \ + \ + PurpleCipherOps * \ + purple_##lower##_cipher_get_ops(void) { \ + return &camel##Ops; \ + } + +/****************************************************************************** + * Macro Expansion + *****************************************************************************/ +PURPLE_G_CHECKSUM_IMPLEMENTATION(md5, MD5, G_CHECKSUM_MD5, 64); +PURPLE_G_CHECKSUM_IMPLEMENTATION(sha1, SHA1, G_CHECKSUM_SHA1, 64); +PURPLE_G_CHECKSUM_IMPLEMENTATION(sha256, SHA256, G_CHECKSUM_SHA256, 64); + +#endif /* GLIB_CHECK_VERSION(2,16,0) */ + diff --git a/libpurple/ciphers/md5.c b/libpurple/ciphers/md5.c index 06986cf86a..fcf9107c31 100644 --- a/libpurple/ciphers/md5.c +++ b/libpurple/ciphers/md5.c @@ -24,47 +24,9 @@ */ #include <cipher.h> -#define MD5_HMAC_BLOCK_SIZE 64 - -static size_t -md5_get_block_size(PurpleCipherContext *context) -{ - /* This does not change (in this case) */ - return MD5_HMAC_BLOCK_SIZE; -} - -#if GLIB_CHECK_VERSION(2,16,0) - -static void -md5_init(PurpleCipherContext *context, void *extra) -{ - purple_g_checksum_init(context, G_CHECKSUM_MD5); -} - -static void -md5_reset(PurpleCipherContext *context, void *extra) -{ - purple_g_checksum_reset(context, G_CHECKSUM_MD5); -} - -static gboolean -md5_digest(PurpleCipherContext *context, gsize in_len, guchar digest[16], - size_t *out_len) -{ - return purple_g_checksum_digest(context, G_CHECKSUM_MD5, in_len, - digest, out_len); -} - -static PurpleCipherOps MD5Ops = { - .init = md5_init, - .reset = md5_reset, - .uninit = purple_g_checksum_uninit, - .append = purple_g_checksum_append, - .digest = md5_digest, - .get_block_size = md5_get_block_size, -}; +#if !GLIB_CHECK_VERSION(2,16,0) -#else /* GLIB_CHECK_VERSION(2,16,0) */ +#define MD5_HMAC_BLOCK_SIZE 64 struct MD5Context { guint32 total[2]; @@ -85,6 +47,13 @@ struct MD5Context { (b)[(i) + 3] = (guchar)((n) >> 24); \ } +static size_t +md5_get_block_size(PurpleCipherContext *context) +{ + /* This does not change (in this case) */ + return MD5_HMAC_BLOCK_SIZE; +} + static void md5_init(PurpleCipherContext *context, gpointer extra) { struct MD5Context *md5_context; @@ -336,10 +305,10 @@ static PurpleCipherOps MD5Ops = { .get_block_size = md5_get_block_size, }; -#endif /* GLIB_CHECK_VERSION(2,16,0) */ - PurpleCipherOps * purple_md5_cipher_get_ops(void) { return &MD5Ops; } +#endif /* !GLIB_CHECK_VERSION(2,16,0) */ + diff --git a/libpurple/ciphers/sha1.c b/libpurple/ciphers/sha1.c index 689009452b..219fa9c00f 100644 --- a/libpurple/ciphers/sha1.c +++ b/libpurple/ciphers/sha1.c @@ -21,47 +21,7 @@ */ #include <cipher.h> -#define SHA1_HMAC_BLOCK_SIZE 64 - -static size_t -sha1_get_block_size(PurpleCipherContext *context) -{ - /* This does not change (in this case) */ - return SHA1_HMAC_BLOCK_SIZE; -} - -#if GLIB_CHECK_VERSION(2,16,0) - -static void -sha1_init(PurpleCipherContext *context, void *extra) -{ - purple_g_checksum_init(context, G_CHECKSUM_SHA1); -} - -static void -sha1_reset(PurpleCipherContext *context, void *extra) -{ - purple_g_checksum_reset(context, G_CHECKSUM_SHA1); -} - -static gboolean -sha1_digest(PurpleCipherContext *context, gsize in_len, guchar digest[20], - gsize *out_len) -{ - return purple_g_checksum_digest(context, G_CHECKSUM_SHA1, in_len, - digest, out_len); -} - -static PurpleCipherOps SHA1Ops = { - .init = sha1_init, - .reset = sha1_reset, - .uninit = purple_g_checksum_uninit, - .append = purple_g_checksum_append, - .digest = sha1_digest, - .get_block_size = sha1_get_block_size, -}; - -#else /* GLIB_CHECK_VERSION(2,16,0) */ +#if !GLIB_CHECK_VERSION(2,16,0) #define SHA1_HMAC_BLOCK_SIZE 64 #define SHA1_ROTL(X,n) ((((X) << (n)) | ((X) >> (32-(n)))) & 0xFFFFFFFF) @@ -76,6 +36,13 @@ struct SHA1Context { guint32 sizeLo; }; +static size_t +sha1_get_block_size(PurpleCipherContext *context) +{ + /* This does not change (in this case) */ + return SHA1_HMAC_BLOCK_SIZE; +} + static void sha1_hash_block(struct SHA1Context *sha1_ctx) { gint i; @@ -294,10 +261,10 @@ static PurpleCipherOps SHA1Ops = { .get_block_size = sha1_get_block_size, }; -#endif /* GLIB_CHECK_VERSION(2,16,0) */ - PurpleCipherOps * purple_sha1_cipher_get_ops(void) { return &SHA1Ops; } +#endif /* !GLIB_CHECK_VERSION(2,16,0) */ + diff --git a/libpurple/ciphers/sha256.c b/libpurple/ciphers/sha256.c index 970f4c78f7..cb878d1a2c 100644 --- a/libpurple/ciphers/sha256.c +++ b/libpurple/ciphers/sha256.c @@ -21,48 +21,9 @@ */ #include <cipher.h> -#define SHA256_HMAC_BLOCK_SIZE 64 - -static size_t -sha256_get_block_size(PurpleCipherContext *context) -{ - /* This does not change (in this case) */ - return SHA256_HMAC_BLOCK_SIZE; -} - -#if GLIB_CHECK_VERSION(2,16,0) - -static void -sha256_init(PurpleCipherContext *context, void *extra) -{ - purple_g_checksum_init(context, G_CHECKSUM_SHA256); -} - -static void -sha256_reset(PurpleCipherContext *context, void *extra) -{ - purple_g_checksum_reset(context, G_CHECKSUM_SHA256); -} - -static gboolean -sha256_digest(PurpleCipherContext *context, gsize in_len, guchar digest[20], - gsize *out_len) -{ - return purple_g_checksum_digest(context, G_CHECKSUM_SHA256, in_len, - digest, out_len); -} - -static PurpleCipherOps SHA256Ops = { - .init = sha256_init, - .reset = sha256_reset, - .uninit = purple_g_checksum_uninit, - .append = purple_g_checksum_append, - .digest = sha256_digest, - .get_block_size = sha256_get_block_size, -}; - -#else /* GLIB_CHECK_VERSION(2,16,0) */ +#if !GLIB_CHECK_VERSION(2,16,0) +#define SHA256_HMAC_BLOCK_SIZE 64 #define SHA256_ROTR(X,n) ((((X) >> (n)) | ((X) << (32-(n)))) & 0xFFFFFFFF) static const guint32 sha256_K[64] = @@ -87,6 +48,13 @@ struct SHA256Context { guint32 sizeLo; }; +static size_t +sha256_get_block_size(PurpleCipherContext *context) +{ + /* This does not change (in this case) */ + return SHA256_HMAC_BLOCK_SIZE; +} + static void sha256_hash_block(struct SHA256Context *sha256_ctx) { gint i; @@ -296,10 +264,10 @@ static PurpleCipherOps SHA256Ops = { .get_block_size = sha256_get_block_size, }; -#endif /* GLIB_CHECK_VERSION(2,16,0) */ - PurpleCipherOps * purple_sha256_cipher_get_ops(void) { return &SHA256Ops; } +#endif /* !GLIB_CHECK_VERSION(2,16,0) */ + |