annotate libpurple/ciphers/rc4.c @ 31219:2d3c1197f930

included string.h in cipher.h since almost all of the ciphers use it.
author Gary Kramlich <grim@reaperworld.com>
date Mon, 14 Feb 2011 06:54:12 +0000
parents 1bdc5f464802
children 554ac1421c9c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
31216
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
1 /*
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
2 * purple
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
3 *
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
4 * Purple is the legal property of its developers, whose names are too numerous
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
5 * to list here. Please refer to the COPYRIGHT file distributed with this
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
6 * source distribution.
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
7 *
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
8 * This program is free software; you can redistribute it and/or modify
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
9 * it under the terms of the GNU General Public License as published by
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
10 * the Free Software Foundation; either version 2 of the License, or
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
11 * (at your option) any later version.
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
12 *
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
13 * This program is distributed in the hope that it will be useful,
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
16 * GNU General Public License for more details.
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
17 *
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
18 * You should have received a copy of the GNU General Public License
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
19 * along with this program; if not, write to the Free Software
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
21 */
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
22 #include <cipher.h>
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
23
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
24 struct RC4Context {
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
25 guchar state[256];
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
26 guchar x;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
27 guchar y;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
28 gint key_len;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
29 };
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
30
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
31 static void
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
32 rc4_init(PurpleCipherContext *context, void *extra) {
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
33 struct RC4Context *rc4_ctx;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
34 rc4_ctx = g_new0(struct RC4Context, 1);
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
35 purple_cipher_context_set_data(context, rc4_ctx);
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
36 purple_cipher_context_reset(context, extra);
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
37 }
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
38
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
39
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
40 static void
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
41 rc4_reset(PurpleCipherContext *context, void *extra) {
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
42 struct RC4Context *rc4_ctx;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
43 guint i;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
44
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
45 rc4_ctx = purple_cipher_context_get_data(context);
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
46
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
47 g_return_if_fail(rc4_ctx);
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
48
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
49 for(i = 0; i < 256; i++)
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
50 rc4_ctx->state[i] = i;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
51 rc4_ctx->x = 0;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
52 rc4_ctx->y = 0;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
53
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
54 /* default is 5 bytes (40bit key) */
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
55 rc4_ctx->key_len = 5;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
56
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
57 }
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
58
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
59 static void
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
60 rc4_uninit(PurpleCipherContext *context) {
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
61 struct RC4Context *rc4_ctx;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
62
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
63 rc4_ctx = purple_cipher_context_get_data(context);
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
64 memset(rc4_ctx, 0, sizeof(*rc4_ctx));
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
65
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
66 g_free(rc4_ctx);
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
67 rc4_ctx = NULL;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
68 }
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
69
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
70
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
71
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
72 static void
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
73 rc4_set_key (PurpleCipherContext *context, const guchar * key) {
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
74 struct RC4Context *ctx;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
75 guchar *state;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
76 guchar temp_swap;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
77 guchar x, y;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
78 guint i;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
79
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
80 ctx = purple_cipher_context_get_data(context);
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
81
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
82 x = 0;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
83 y = 0;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
84 state = &ctx->state[0];
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
85 for(i = 0; i < 256; i++)
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
86 {
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
87 y = (key[x] + state[i] + y) % 256;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
88 temp_swap = state[i];
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
89 state[i] = state[y];
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
90 state[y] = temp_swap;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
91 x = (x + 1) % ctx->key_len;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
92 }
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
93 }
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
94
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
95 static void
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
96 rc4_set_opt(PurpleCipherContext *context, const gchar *name, void *value) {
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
97 struct RC4Context *ctx;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
98
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
99 ctx = purple_cipher_context_get_data(context);
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
100
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
101 if(purple_strequal(name, "key_len")) {
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
102 ctx->key_len = GPOINTER_TO_INT(value);
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
103 }
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
104 }
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
105
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
106 static size_t
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
107 rc4_get_key_size (PurpleCipherContext *context)
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
108 {
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
109 struct RC4Context *ctx;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
110
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
111 g_return_val_if_fail(context, -1);
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
112
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
113 ctx = purple_cipher_context_get_data(context);
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
114
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
115 g_return_val_if_fail(ctx, -1);
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
116
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
117 return ctx->key_len;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
118 }
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
119
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
120 static void *
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
121 rc4_get_opt(PurpleCipherContext *context, const gchar *name) {
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
122 struct RC4Context *ctx;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
123
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
124 ctx = purple_cipher_context_get_data(context);
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
125
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
126 if(purple_strequal(name, "key_len")) {
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
127 return GINT_TO_POINTER(ctx->key_len);
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
128 }
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
129
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
130 return NULL;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
131 }
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
132
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
133 static gint
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
134 rc4_encrypt(PurpleCipherContext *context, const guchar data[],
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
135 size_t len, guchar output[], size_t *outlen) {
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
136 struct RC4Context *ctx;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
137 guchar temp_swap;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
138 guchar x, y, z;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
139 guchar *state;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
140 guint i;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
141
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
142 ctx = purple_cipher_context_get_data(context);
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
143
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
144 x = ctx->x;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
145 y = ctx->y;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
146 state = &ctx->state[0];
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
147
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
148 for(i = 0; i < len; i++)
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
149 {
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
150 x = (x + 1) % 256;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
151 y = (state[x] + y) % 256;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
152 temp_swap = state[x];
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
153 state[x] = state[y];
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
154 state[y] = temp_swap;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
155 z = state[x] + (state[y]) % 256;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
156 output[i] = data[i] ^ state[z];
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
157 }
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
158 ctx->x = x;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
159 ctx->y = y;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
160 if(outlen)
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
161 *outlen = len;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
162
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
163 return 0;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
164 }
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
165
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
166 static PurpleCipherOps RC4Ops = {
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
167 .set_option = rc4_set_opt,
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
168 .get_option = rc4_get_opt,
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
169 .init = rc4_init,
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
170 .reset = rc4_reset,
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
171 .uninit = rc4_uninit,
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
172 .encrypt = rc4_encrypt,
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
173 .set_key = rc4_set_key,
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
174 .get_key_size = rc4_get_key_size,
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
175 };
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
176
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
177 PurpleCipherOps *
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
178 purple_rc4_cipher_get_ops(void) {
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
179 return &RC4Ops;
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
180 }
1bdc5f464802 Moved rc4 to the ciphers sublibrary
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
181