/src/pidgin/libpurple/ciphers/rc4.c
Line | Count | Source |
1 | | /* |
2 | | * purple |
3 | | * |
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. |
7 | | * |
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. |
12 | | * |
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. |
17 | | * |
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 |
21 | | */ |
22 | | #include <cipher.h> |
23 | | #include <util.h> |
24 | | |
25 | | struct RC4Context { |
26 | | guchar state[256]; |
27 | | guchar x; |
28 | | guchar y; |
29 | | gint key_len; |
30 | | }; |
31 | | |
32 | | static void |
33 | 0 | rc4_init(PurpleCipherContext *context, void *extra) { |
34 | 0 | struct RC4Context *rc4_ctx; |
35 | 0 | rc4_ctx = g_new0(struct RC4Context, 1); |
36 | 0 | purple_cipher_context_set_data(context, rc4_ctx); |
37 | 0 | purple_cipher_context_reset(context, extra); |
38 | 0 | } |
39 | | |
40 | | |
41 | | static void |
42 | 0 | rc4_reset(PurpleCipherContext *context, void *extra) { |
43 | 0 | struct RC4Context *rc4_ctx; |
44 | 0 | guint i; |
45 | |
|
46 | 0 | rc4_ctx = purple_cipher_context_get_data(context); |
47 | |
|
48 | 0 | g_return_if_fail(rc4_ctx); |
49 | | |
50 | 0 | for(i = 0; i < 256; i++) |
51 | 0 | rc4_ctx->state[i] = i; |
52 | 0 | rc4_ctx->x = 0; |
53 | 0 | rc4_ctx->y = 0; |
54 | | |
55 | | /* default is 5 bytes (40bit key) */ |
56 | 0 | rc4_ctx->key_len = 5; |
57 | |
|
58 | 0 | } |
59 | | |
60 | | static void |
61 | 0 | rc4_uninit(PurpleCipherContext *context) { |
62 | 0 | struct RC4Context *rc4_ctx; |
63 | |
|
64 | 0 | rc4_ctx = purple_cipher_context_get_data(context); |
65 | 0 | memset(rc4_ctx, 0, sizeof(*rc4_ctx)); |
66 | |
|
67 | 0 | g_free(rc4_ctx); |
68 | 0 | rc4_ctx = NULL; |
69 | 0 | } |
70 | | |
71 | | |
72 | | |
73 | | static void |
74 | 0 | rc4_set_key (PurpleCipherContext *context, const guchar * key) { |
75 | 0 | struct RC4Context *ctx; |
76 | 0 | guchar *state; |
77 | 0 | guchar temp_swap; |
78 | 0 | guchar x, y; |
79 | 0 | guint i; |
80 | |
|
81 | 0 | ctx = purple_cipher_context_get_data(context); |
82 | |
|
83 | 0 | x = 0; |
84 | 0 | y = 0; |
85 | 0 | state = &ctx->state[0]; |
86 | 0 | for(i = 0; i < 256; i++) |
87 | 0 | { |
88 | 0 | y = (key[x] + state[i] + y) % 256; |
89 | 0 | temp_swap = state[i]; |
90 | 0 | state[i] = state[y]; |
91 | 0 | state[y] = temp_swap; |
92 | 0 | x = (x + 1) % ctx->key_len; |
93 | 0 | } |
94 | 0 | } |
95 | | |
96 | | static void |
97 | 0 | rc4_set_opt(PurpleCipherContext *context, const gchar *name, void *value) { |
98 | 0 | struct RC4Context *ctx; |
99 | |
|
100 | 0 | ctx = purple_cipher_context_get_data(context); |
101 | |
|
102 | 0 | if(purple_strequal(name, "key_len")) { |
103 | 0 | ctx->key_len = GPOINTER_TO_INT(value); |
104 | 0 | } |
105 | 0 | } |
106 | | |
107 | | static size_t |
108 | | rc4_get_key_size (PurpleCipherContext *context) |
109 | 0 | { |
110 | 0 | struct RC4Context *ctx; |
111 | |
|
112 | 0 | g_return_val_if_fail(context, -1); |
113 | | |
114 | 0 | ctx = purple_cipher_context_get_data(context); |
115 | |
|
116 | 0 | g_return_val_if_fail(ctx, -1); |
117 | | |
118 | 0 | return ctx->key_len; |
119 | 0 | } |
120 | | |
121 | | static void * |
122 | 0 | rc4_get_opt(PurpleCipherContext *context, const gchar *name) { |
123 | 0 | struct RC4Context *ctx; |
124 | |
|
125 | 0 | ctx = purple_cipher_context_get_data(context); |
126 | |
|
127 | 0 | if(purple_strequal(name, "key_len")) { |
128 | 0 | return GINT_TO_POINTER(ctx->key_len); |
129 | 0 | } |
130 | | |
131 | 0 | return NULL; |
132 | 0 | } |
133 | | |
134 | | static gint |
135 | | rc4_encrypt(PurpleCipherContext *context, const guchar data[], |
136 | 0 | size_t len, guchar output[], size_t *outlen) { |
137 | 0 | struct RC4Context *ctx; |
138 | 0 | guchar temp_swap; |
139 | 0 | guchar x, y, z; |
140 | 0 | guchar *state; |
141 | 0 | guint i; |
142 | |
|
143 | 0 | ctx = purple_cipher_context_get_data(context); |
144 | |
|
145 | 0 | x = ctx->x; |
146 | 0 | y = ctx->y; |
147 | 0 | state = &ctx->state[0]; |
148 | |
|
149 | 0 | for(i = 0; i < len; i++) |
150 | 0 | { |
151 | 0 | x = (x + 1) % 256; |
152 | 0 | y = (state[x] + y) % 256; |
153 | 0 | temp_swap = state[x]; |
154 | 0 | state[x] = state[y]; |
155 | 0 | state[y] = temp_swap; |
156 | 0 | z = state[x] + (state[y]) % 256; |
157 | 0 | output[i] = data[i] ^ state[z]; |
158 | 0 | } |
159 | 0 | ctx->x = x; |
160 | 0 | ctx->y = y; |
161 | 0 | if(outlen) |
162 | 0 | *outlen = len; |
163 | |
|
164 | 0 | return 0; |
165 | 0 | } |
166 | | |
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 */ |
186 | | }; |
187 | | |
188 | | PurpleCipherOps * |
189 | 0 | purple_rc4_cipher_get_ops(void) { |
190 | 0 | return &RC4Ops; |
191 | 0 | } |
192 | | |