pidgin/android/libpurple2

a924aacd5a37
Parents 5749f2724b12
Children 01e0d716a28e
ciphers cleanup: encryption and decryption lengths
--- a/libpurple/cipher.c Sun May 05 15:37:25 2013 +0200
+++ b/libpurple/cipher.c Sun May 05 16:29:14 2013 +0200
@@ -473,50 +473,49 @@
}
}
-gint
-purple_cipher_context_encrypt(PurpleCipherContext *context, const guchar data[],
- size_t len, guchar output[], size_t *outlen)
+ssize_t
+purple_cipher_context_encrypt(PurpleCipherContext *context,
+ const guchar input[], size_t in_len, guchar output[], size_t out_size)
{
PurpleCipher *cipher = NULL;
- g_return_val_if_fail(context, -1);
+ g_return_val_if_fail(context != NULL, -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);
cipher = context->cipher;
g_return_val_if_fail(cipher, -1);
if(cipher->ops && cipher->ops->encrypt)
- return cipher->ops->encrypt(context, data, len, output, outlen);
+ return cipher->ops->encrypt(context, input, in_len, output, out_size);
else {
purple_debug_warning("cipher", "the %s cipher does not support the encrypt"
"operation\n", cipher->name);
- if(outlen)
- *outlen = -1;
-
return -1;
}
}
-gint
-purple_cipher_context_decrypt(PurpleCipherContext *context, const guchar data[],
- size_t len, guchar output[], size_t *outlen)
+ssize_t
+purple_cipher_context_decrypt(PurpleCipherContext *context,
+ const guchar input[], size_t in_len, guchar output[], size_t out_size)
{
PurpleCipher *cipher = NULL;
- g_return_val_if_fail(context, -1);
+ g_return_val_if_fail(context != NULL, -1);
+ g_return_val_if_fail(input != NULL, -1);
+ g_return_val_if_fail(output != NULL, -1);
cipher = context->cipher;
g_return_val_if_fail(cipher, -1);
if(cipher->ops && cipher->ops->decrypt)
- return cipher->ops->decrypt(context, data, len, output, outlen);
+ return cipher->ops->decrypt(context, input, in_len, output, out_size);
else {
purple_debug_warning("cipher", "the %s cipher does not support the decrypt"
"operation\n", cipher->name);
- if(outlen)
- *outlen = -1;
-
return -1;
}
}
--- a/libpurple/cipher.h Sun May 05 15:37:25 2013 +0200
+++ b/libpurple/cipher.h Sun May 05 16:29:14 2013 +0200
@@ -103,10 +103,10 @@
size_t (*get_digest_size)(PurpleCipherContext *context);
/** The encrypt function */
- int (*encrypt)(PurpleCipherContext *context, const guchar data[], size_t len, guchar output[], size_t *outlen);
+ ssize_t (*encrypt)(PurpleCipherContext *context, const guchar input[], size_t in_len, guchar output[], size_t out_size);
/** The decrypt function */
- int (*decrypt)(PurpleCipherContext *context, const guchar data[], size_t len, guchar output[], size_t *outlen);
+ ssize_t (*decrypt)(PurpleCipherContext *context, const guchar input[], size_t in_len, guchar output[], size_t out_size);
/** The set salt function */
void (*set_salt)(PurpleCipherContext *context, guchar *salt, size_t len);
@@ -347,28 +347,28 @@
/**
* Encrypts data using the context
*
- * @param context The context
- * @param data The data to encrypt
- * @param len The length of the data
- * @param output The output buffer
- * @param outlen The len of data that was outputed
+ * @param context The context
+ * @param input The data to encrypt
+ * @param in_len The length of the data
+ * @param output The output buffer
+ * @param out_size The size of the output buffer
*
- * @return A cipher specific status code
+ * @return A length of data that was outputed or -1, if failed
*/
-gint purple_cipher_context_encrypt(PurpleCipherContext *context, const guchar data[], size_t len, guchar output[], size_t *outlen);
+ssize_t purple_cipher_context_encrypt(PurpleCipherContext *context, const guchar input[], size_t in_len, guchar output[], size_t out_size);
/**
* Decrypts data using the context
*
- * @param context The context
- * @param data The data to encrypt
- * @param len The length of the returned value
- * @param output The output buffer
- * @param outlen The len of data that was outputed
+ * @param context The context
+ * @param input The data to encrypt
+ * @param in_len The length of the returned value
+ * @param output The output buffer
+ * @param out_size The size of the output buffer
*
- * @return A cipher specific status code
+ * @return A length of data that was outputed or -1, if failed
*/
-gint purple_cipher_context_decrypt(PurpleCipherContext *context, const guchar data[], size_t len, guchar output[], size_t *outlen);
+ssize_t purple_cipher_context_decrypt(PurpleCipherContext *context, const guchar input[], size_t in_len, guchar output[], size_t out_size);
/**
* Sets the salt on a context
--- a/libpurple/ciphers/des.c Sun May 05 15:37:25 2013 +0200
+++ b/libpurple/ciphers/des.c Sun May 05 16:29:14 2013 +0200
@@ -33,6 +33,8 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
+
+#include "internal.h"
#include <cipher.h>
#include "ciphers.h"
@@ -387,27 +389,32 @@
return 0;
}
-static gint
-des_encrypt(PurpleCipherContext *context, const guchar data[],
- size_t len, guchar output[], size_t *outlen)
+static ssize_t
+des_encrypt(PurpleCipherContext *context, const guchar input[], size_t in_len,
+ guchar output[], size_t out_size)
{
int offset = 0;
int i = 0;
int tmp;
guint8 buf[8] = {0,0,0,0,0,0,0,0};
- while(offset+8<=len) {
+ ssize_t out_len;
+
+ g_return_val_if_fail(out_size < in_len, -1);
+
+ while(offset+8<=in_len) {
des_ecb_crypt(purple_cipher_context_get_data(context),
- data+offset,
+ input+offset,
output+offset,
0);
offset+=8;
}
- *outlen = len;
- if(offset<len) {
- *outlen += len - offset;
+ out_len = in_len;
+ if(offset<in_len) {
+ out_len += in_len - offset;
+ g_return_val_if_fail(out_size < out_len, -1);
tmp = offset;
- while(tmp<len) {
- buf[i++] = data[tmp];
+ while(tmp<in_len) {
+ buf[i++] = input[tmp];
tmp++;
}
des_ecb_crypt(purple_cipher_context_get_data(context),
@@ -415,30 +422,35 @@
output+offset,
0);
}
- return 0;
+ return out_len;
}
static gint
-des_decrypt(PurpleCipherContext *context, const guchar data[],
- size_t len, guchar output[], size_t *outlen)
+des_decrypt(PurpleCipherContext *context, const guchar input[], size_t in_len,
+ guchar output[], size_t out_size)
{
int offset = 0;
int i = 0;
int tmp;
guint8 buf[8] = {0,0,0,0,0,0,0,0};
- while(offset+8<=len) {
+ ssize_t out_len;
+
+ g_return_val_if_fail(out_size < in_len, -1);
+
+ while(offset+8<=in_len) {
des_ecb_crypt(purple_cipher_context_get_data(context),
- data+offset,
+ input+offset,
output+offset,
1);
offset+=8;
}
- *outlen = len;
- if(offset<len) {
- *outlen += len - offset;
+ out_len = in_len;
+ if(offset<in_len) {
+ out_len += in_len - offset;
+ g_return_val_if_fail(out_size < out_len, -1);
tmp = offset;
- while(tmp<len) {
- buf[i++] = data[tmp];
+ while(tmp<in_len) {
+ buf[i++] = input[tmp];
tmp++;
}
des_ecb_crypt(purple_cipher_context_get_data(context),
@@ -446,7 +458,7 @@
output+offset,
1);
}
- return 0;
+ return out_len;
}
static void
@@ -538,17 +550,21 @@
return 24;
}
-static gint
-des3_ecb_encrypt(struct _des3_ctx *ctx, const guchar data[],
- size_t len, guchar output[], size_t *outlen)
+static ssize_t
+des3_ecb_encrypt(struct _des3_ctx *ctx, const guchar input[], size_t in_len,
+ guchar output[], size_t out_size)
{
int offset = 0;
int i = 0;
int tmp;
guint8 buf[8] = {0,0,0,0,0,0,0,0};
- while (offset + 8 <= len) {
+ ssize_t out_len;
+
+ g_return_val_if_fail(out_size < in_len, -1);
+
+ while (offset + 8 <= in_len) {
des_ecb_crypt(&ctx->key1,
- data+offset,
+ input+offset,
output+offset,
0);
des_ecb_crypt(&ctx->key2,
@@ -561,13 +577,14 @@
0);
offset += 8;
}
- *outlen = len;
- if (offset < len) {
- *outlen += len - offset;
+ out_len = in_len;
+ if (offset < in_len) {
+ out_len += in_len - offset;
+ g_return_val_if_fail(out_size < out_len, -1);
tmp = offset;
memset(buf, 0, 8);
- while (tmp < len) {
- buf[i++] = data[tmp];
+ while (tmp < in_len) {
+ buf[i++] = input[tmp];
tmp++;
}
des_ecb_crypt(&ctx->key1,
@@ -583,21 +600,25 @@
output+offset,
0);
}
- return 0;
+ return out_len;
}
-static gint
-des3_cbc_encrypt(struct _des3_ctx *ctx, const guchar data[],
- size_t len, guchar output[], size_t *outlen)
+static ssize_t
+des3_cbc_encrypt(struct _des3_ctx *ctx, const guchar input[], size_t in_len,
+ guchar output[], size_t out_size)
{
int offset = 0;
int i = 0;
int tmp;
guint8 buf[8];
memcpy(buf, ctx->iv, 8);
- while (offset + 8 <= len) {
+ ssize_t out_len;
+
+ g_return_val_if_fail(out_size < in_len, -1);
+
+ while (offset + 8 <= in_len) {
for (i = 0; i < 8; i++)
- buf[i] ^= data[offset + i];
+ buf[i] ^= input[offset + i];
des_ecb_crypt(&ctx->key1,
buf,
@@ -614,13 +635,14 @@
memcpy(buf, output+offset, 8);
offset += 8;
}
- *outlen = len;
- if (offset < len) {
- *outlen += len - offset;
+ out_len = in_len;
+ if (offset < in_len) {
+ out_len += in_len - offset;
+ g_return_val_if_fail(out_size < out_len, -1);
tmp = offset;
i = 0;
- while (tmp < len) {
- buf[i++] ^= data[tmp];
+ while (tmp < in_len) {
+ buf[i++] ^= input[tmp];
tmp++;
}
des_ecb_crypt(&ctx->key1,
@@ -636,19 +658,19 @@
output+offset,
0);
}
- return 0;
+ return out_len;
}
-static gint
-des3_encrypt(PurpleCipherContext *context, const guchar data[],
- size_t len, guchar output[], size_t *outlen)
+static ssize_t
+des3_encrypt(PurpleCipherContext *context, const guchar input[], size_t in_len,
+ guchar output[], size_t out_size)
{
struct _des3_ctx *ctx = purple_cipher_context_get_data(context);
if (ctx->mode == PURPLE_CIPHER_BATCH_MODE_ECB) {
- return des3_ecb_encrypt(ctx, data, len, output, outlen);
+ return des3_ecb_encrypt(ctx, input, in_len, output, out_size);
} else if (ctx->mode == PURPLE_CIPHER_BATCH_MODE_CBC) {
- return des3_cbc_encrypt(ctx, data, len, output, outlen);
+ return des3_cbc_encrypt(ctx, input, in_len, output, out_size);
} else {
g_return_val_if_reached(0);
}
@@ -657,17 +679,21 @@
}
static gint
-des3_ecb_decrypt(struct _des3_ctx *ctx, const guchar data[],
- size_t len, guchar output[], size_t *outlen)
+des3_ecb_decrypt(struct _des3_ctx *ctx, const guchar input[], size_t in_len,
+ guchar output[], size_t out_size)
{
int offset = 0;
int i = 0;
int tmp;
guint8 buf[8] = {0,0,0,0,0,0,0,0};
- while (offset + 8 <= len) {
+ ssize_t out_len;
+
+ g_return_val_if_fail(out_size < in_len, -1);
+
+ while (offset + 8 <= in_len) {
/* NOTE: Apply key in reverse */
des_ecb_crypt(&ctx->key3,
- data+offset,
+ input+offset,
output+offset,
1);
des_ecb_crypt(&ctx->key2,
@@ -680,13 +706,14 @@
1);
offset+=8;
}
- *outlen = len;
- if (offset < len) {
- *outlen += len - offset;
+ out_len = in_len;
+ if (offset < in_len) {
+ out_len += in_len - offset;
+ g_return_val_if_fail(out_size < out_len, -1);
tmp = offset;
memset(buf, 0, 8);
- while (tmp < len) {
- buf[i++] = data[tmp];
+ while (tmp < in_len) {
+ buf[i++] = input[tmp];
tmp++;
}
des_ecb_crypt(&ctx->key3,
@@ -702,22 +729,26 @@
output+offset,
1);
}
- return 0;
+ return out_len;
}
static gint
-des3_cbc_decrypt(struct _des3_ctx *ctx, const guchar data[],
- size_t len, guchar output[], size_t *outlen)
+des3_cbc_decrypt(struct _des3_ctx *ctx, const guchar input[], size_t in_len,
+ guchar output[], size_t out_size)
{
int offset = 0;
int i = 0;
int tmp;
guint8 buf[8] = {0,0,0,0,0,0,0,0};
guint8 link[8];
+ ssize_t out_len;
+
+ g_return_val_if_fail(out_size < in_len, -1);
+
memcpy(link, ctx->iv, 8);
- while (offset + 8 <= len) {
+ while (offset + 8 <= in_len) {
des_ecb_crypt(&ctx->key3,
- data+offset,
+ input+offset,
output+offset,
1);
des_ecb_crypt(&ctx->key2,
@@ -730,17 +761,18 @@
1);
for (i = 0; i < 8; i++)
output[offset + i] ^= link[i];
- memcpy(link, data + offset, 8);
+ memcpy(link, input + offset, 8);
offset+=8;
}
- *outlen = len;
- if(offset<len) {
- *outlen += len - offset;
+ out_len = in_len;
+ if(offset<in_len) {
+ out_len += in_len - offset;
+ g_return_val_if_fail(out_size < out_len, -1);
tmp = offset;
memset(buf, 0, 8);
i = 0;
- while(tmp<len) {
- buf[i++] = data[tmp];
+ while(tmp<in_len) {
+ buf[i++] = input[tmp];
tmp++;
}
des_ecb_crypt(&ctx->key3,
@@ -758,19 +790,19 @@
for (i = 0; i < 8; i++)
output[offset + i] ^= link[i];
}
- return 0;
+ return out_len;
}
static gint
-des3_decrypt(PurpleCipherContext *context, const guchar data[],
- size_t len, guchar output[], size_t *outlen)
+des3_decrypt(PurpleCipherContext *context, const guchar input[], size_t in_len,
+ guchar output[], size_t out_size)
{
struct _des3_ctx *ctx = purple_cipher_context_get_data(context);
if (ctx->mode == PURPLE_CIPHER_BATCH_MODE_ECB) {
- return des3_ecb_decrypt(ctx, data, len, output, outlen);
+ return des3_ecb_decrypt(ctx, input, in_len, output, out_size);
} else if (ctx->mode == PURPLE_CIPHER_BATCH_MODE_CBC) {
- return des3_cbc_decrypt(ctx, data, len, output, outlen);
+ return des3_cbc_decrypt(ctx, input, in_len, output, out_size);
} else {
g_return_val_if_reached(0);
}
--- a/libpurple/ciphers/gchecksum.c Sun May 05 15:37:25 2013 +0200
+++ b/libpurple/ciphers/gchecksum.c Sun May 05 16:29:14 2013 +0200
@@ -1,3 +1,4 @@
+#include "internal.h"
#include <cipher.h>
#include "ciphers.h"
--- a/libpurple/ciphers/hmac.c Sun May 05 15:37:25 2013 +0200
+++ b/libpurple/ciphers/hmac.c Sun May 05 16:29:14 2013 +0200
@@ -19,6 +19,8 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
+
+#include "internal.h"
#include <cipher.h>
#include "ciphers.h"
--- a/libpurple/ciphers/md4.c Sun May 05 15:37:25 2013 +0200
+++ b/libpurple/ciphers/md4.c Sun May 05 16:29:14 2013 +0200
@@ -33,6 +33,8 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
+
+#include "internal.h"
#include <cipher.h>
#include "ciphers.h"
--- a/libpurple/ciphers/rc4.c Sun May 05 15:37:25 2013 +0200
+++ b/libpurple/ciphers/rc4.c Sun May 05 16:29:14 2013 +0200
@@ -19,6 +19,8 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
+
+#include "internal.h"
#include <cipher.h>
#include "ciphers.h"
#include <util.h>
@@ -95,9 +97,10 @@
}
}
-static gint
-rc4_encrypt(PurpleCipherContext *context, const guchar data[],
- size_t len, guchar output[], size_t *outlen) {
+
+static ssize_t
+rc4_encrypt(PurpleCipherContext *context, const guchar input[], size_t in_len,
+ guchar output[], size_t out_size) {
struct RC4Context *ctx;
guchar temp_swap;
guchar x, y, z;
@@ -110,7 +113,7 @@
y = ctx->y;
state = &ctx->state[0];
- for(i = 0; i < len; i++)
+ for(i = 0; i < in_len; i++)
{
x = (x + 1) % 256;
y = (state[x] + y) % 256;
@@ -118,14 +121,12 @@
state[x] = state[y];
state[y] = temp_swap;
z = state[x] + (state[y]) % 256;
- output[i] = data[i] ^ state[z];
+ output[i] = input[i] ^ state[z];
}
ctx->x = x;
ctx->y = y;
- if(outlen)
- *outlen = len;
- return 0;
+ return in_len;
}
static PurpleCipherOps RC4Ops = {
--- a/libpurple/ntlm.c Sun May 05 15:37:25 2013 +0200
+++ b/libpurple/ntlm.c Sun May 05 16:29:14 2013 +0200
@@ -192,12 +192,11 @@
{
PurpleCipher *cipher;
PurpleCipherContext *context;
- size_t outlen;
cipher = purple_ciphers_find_cipher("des");
context = purple_cipher_context_new(cipher, NULL);
purple_cipher_context_set_key(context, key, 8);
- purple_cipher_context_encrypt(context, plaintext, 8, result, &outlen);
+ purple_cipher_context_encrypt(context, plaintext, 8, result, 8);
purple_cipher_context_destroy(context);
}
--- a/libpurple/protocols/msn/nexus.c Sun May 05 15:37:25 2013 +0200
+++ b/libpurple/protocols/msn/nexus.c Sun May 05 16:29:14 2013 +0200
@@ -147,7 +147,6 @@
{
PurpleCipherContext *des3;
char *out;
- size_t outlen;
des3 = purple_cipher_context_new_by_name("des3", NULL);
purple_cipher_context_set_key(des3, (guchar *)key, 24);
@@ -156,9 +155,9 @@
out = g_malloc(len);
if (decrypt)
- purple_cipher_context_decrypt(des3, (guchar *)data, len, (guchar *)out, &outlen);
+ purple_cipher_context_decrypt(des3, (guchar *)data, len, (guchar *)out, len);
else
- purple_cipher_context_encrypt(des3, (guchar *)data, len, (guchar *)out, &outlen);
+ purple_cipher_context_encrypt(des3, (guchar *)data, len, (guchar *)out, len);
purple_cipher_context_destroy(des3);
--- a/libpurple/protocols/myspace/myspace.c Sun May 05 15:37:25 2013 +0200
+++ b/libpurple/protocols/myspace/myspace.c Sun May 05 16:29:14 2013 +0200
@@ -640,8 +640,8 @@
data_out = g_new0(guchar, data->len);
- purple_cipher_context_encrypt(rc4, (const guchar *)data->str,
- data->len, data_out, &data_out_len);
+ data_out_len = purple_cipher_context_encrypt(rc4,
+ (const guchar *)data->str, data->len, data_out, data->len);
purple_cipher_context_destroy(rc4);
if (data_out_len != data->len) {