applied changes from fb4435d514398a0b1febebe8bf46339e2c2b52b6
[pidgin-git.git] / libpurple / ciphers / rc4.c
blob2918f245481bbf57ce54493668646a356839716b
1 /*
2 * purple
4 * Purple is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
6 * source distribution.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
22 #include <cipher.h>
23 #include <util.h>
25 struct RC4Context {
26 guchar state[256];
27 guchar x;
28 guchar y;
29 gint key_len;
32 static void
33 rc4_init(PurpleCipherContext *context, void *extra) {
34 struct RC4Context *rc4_ctx;
35 rc4_ctx = g_new0(struct RC4Context, 1);
36 purple_cipher_context_set_data(context, rc4_ctx);
37 purple_cipher_context_reset(context, extra);
41 static void
42 rc4_reset(PurpleCipherContext *context, void *extra) {
43 struct RC4Context *rc4_ctx;
44 guint i;
46 rc4_ctx = purple_cipher_context_get_data(context);
48 g_return_if_fail(rc4_ctx);
50 for(i = 0; i < 256; i++)
51 rc4_ctx->state[i] = i;
52 rc4_ctx->x = 0;
53 rc4_ctx->y = 0;
55 /* default is 5 bytes (40bit key) */
56 rc4_ctx->key_len = 5;
60 static void
61 rc4_uninit(PurpleCipherContext *context) {
62 struct RC4Context *rc4_ctx;
64 rc4_ctx = purple_cipher_context_get_data(context);
65 memset(rc4_ctx, 0, sizeof(*rc4_ctx));
67 g_free(rc4_ctx);
68 rc4_ctx = NULL;
73 static void
74 rc4_set_key (PurpleCipherContext *context, const guchar * key) {
75 struct RC4Context *ctx;
76 guchar *state;
77 guchar temp_swap;
78 guchar x, y;
79 guint i;
81 ctx = purple_cipher_context_get_data(context);
83 x = 0;
84 y = 0;
85 state = &ctx->state[0];
86 for(i = 0; i < 256; i++)
88 y = (key[x] + state[i] + y) % 256;
89 temp_swap = state[i];
90 state[i] = state[y];
91 state[y] = temp_swap;
92 x = (x + 1) % ctx->key_len;
96 static void
97 rc4_set_opt(PurpleCipherContext *context, const gchar *name, void *value) {
98 struct RC4Context *ctx;
100 ctx = purple_cipher_context_get_data(context);
102 if(purple_strequal(name, "key_len")) {
103 ctx->key_len = GPOINTER_TO_INT(value);
107 static size_t
108 rc4_get_key_size (PurpleCipherContext *context)
110 struct RC4Context *ctx;
112 g_return_val_if_fail(context, -1);
114 ctx = purple_cipher_context_get_data(context);
116 g_return_val_if_fail(ctx, -1);
118 return ctx->key_len;
121 static void *
122 rc4_get_opt(PurpleCipherContext *context, const gchar *name) {
123 struct RC4Context *ctx;
125 ctx = purple_cipher_context_get_data(context);
127 if(purple_strequal(name, "key_len")) {
128 return GINT_TO_POINTER(ctx->key_len);
131 return NULL;
134 static gint
135 rc4_encrypt(PurpleCipherContext *context, const guchar data[],
136 size_t len, guchar output[], size_t *outlen) {
137 struct RC4Context *ctx;
138 guchar temp_swap;
139 guchar x, y, z;
140 guchar *state;
141 guint i;
143 ctx = purple_cipher_context_get_data(context);
145 x = ctx->x;
146 y = ctx->y;
147 state = &ctx->state[0];
149 for(i = 0; i < len; i++)
151 x = (x + 1) % 256;
152 y = (state[x] + y) % 256;
153 temp_swap = state[x];
154 state[x] = state[y];
155 state[y] = temp_swap;
156 z = state[x] + (state[y]) % 256;
157 output[i] = data[i] ^ state[z];
159 ctx->x = x;
160 ctx->y = y;
161 if(outlen)
162 *outlen = len;
164 return 0;
167 static PurpleCipherOps RC4Ops = {
168 rc4_set_opt, /* Set Option */
169 rc4_get_opt, /* Get Option */
170 rc4_init, /* init */
171 rc4_reset, /* reset */
172 rc4_uninit, /* uninit */
173 NULL, /* set iv */
174 NULL, /* append */
175 NULL, /* digest */
176 rc4_encrypt, /* encrypt */
177 NULL, /* decrypt */
178 NULL, /* set salt */
179 NULL, /* get salt size */
180 rc4_set_key, /* set key */
181 rc4_get_key_size, /* get key size */
182 NULL, /* set batch mode */
183 NULL, /* get batch mode */
184 NULL, /* get block size */
185 NULL /* set key with len */
188 PurpleCipherOps *
189 purple_rc4_cipher_get_ops(void) {
190 return &RC4Ops;