Generic cipher and RNG interfaces.
[u/mdw/catacomb] / rc4.c
CommitLineData
d03ab969 1/* -*-c-*-
2 *
41f91aa3 3 * $Id: rc4.c,v 1.2 1999/12/10 23:27:35 mdw Exp $
d03ab969 4 *
5 * The alleged RC4 stream cipher
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: rc4.c,v $
41f91aa3 33 * Revision 1.2 1999/12/10 23:27:35 mdw
34 * Generic cipher and RNG interfaces.
35 *
d03ab969 36 * Revision 1.1 1999/09/03 08:41:12 mdw
37 * Initial import.
38 *
39 */
40
41/*----- Header files ------------------------------------------------------*/
42
43#include <assert.h>
41f91aa3 44#include <stdarg.h>
d03ab969 45#include <stdio.h>
46
47#include <mLib/bits.h>
41f91aa3 48#include <mLib/sub.h>
d03ab969 49
41f91aa3 50#include "gcipher.h"
51#include "grand.h"
d03ab969 52#include "rc4.h"
53
54/*----- Main code ---------------------------------------------------------*/
55
56/* --- @rc4_init@ --- *
57 *
58 * Arguments: @rc4_ctx *ctx@ = pointer to context to initialize
59 * @const void *k@ = pointer to key data to use
60 * @size_t sz@ = size of the key data
61 *
62 * Returns: ---
63 *
64 * Use: Initializes an RC4 context ready for use.
65 */
66
67void rc4_init(rc4_ctx *ctx, const void *k, size_t sz)
68{
69 unsigned i, j;
70 const octet *p = k, *q = p + sz;
71
72 assert(((void)"RC4 does not support zero length keys", sz != 0));
73
74 for (i = 0; i < 256; i++)
75 ctx->s[i] = i;
76 ctx->f = 0;
77 ctx->i = ctx->j = 0;
78
79 for (i = j = 0; i < 256; i++) {
80 unsigned si = ctx->s[i];
81 j = (j + si + *p++) & 0xff;
82 ctx->s[i] = ctx->s[j];
83 ctx->s[j] = si;
84 if (p == q)
85 p = k;
86 }
d03ab969 87}
88
89/* --- @rc4_encrypt@ --- *
90 *
91 * Arguments: @rc4_ctx *ctx@ = pointer to context to use
92 * @const void *src@ = pointer to the source block
93 * @void *dest@ = pointer to the destination block
94 * @size_t sz@ = size of the block
95 *
96 * Returns: ---
97 *
98 * Use: Encrypts or decrypts a block of data. The destination may
99 * be null to just grind the generator around for a while. It's
100 * recommended that you say `@rc4_encrypt(&ctx, 0, 0, 1024)@'
101 * after initializing a new context, to prevent keystream
102 * guessing attacks. The source may be null to just extract a
103 * big lump of data from the generator.
104 */
105
106void rc4_encrypt(rc4_ctx *ctx, const void *src, void *dest, size_t sz)
107{
108 const octet *s = src;
109 octet *d = dest;
110
111 if (!d)
112 RC4_OPEN(ctx, while (sz) { unsigned x; RC4_BYTE(x); sz--; });
113 else if (!s)
114 RC4_OPEN(ctx, while (sz) { RC4_BYTE(*d++); sz--; });
115 else
116 RC4_OPEN(ctx,
117 while (sz) { unsigned x; RC4_BYTE(x); *d++ = *s++ ^ x; sz--; });
118}
119
41f91aa3 120/*----- Generic cipher interface ------------------------------------------*/
121
122typedef struct gctx {
123 gcipher c;
124 rc4_ctx rc4;
125} gctx;
126
127static const gcipher_ops gops;
128
129static gcipher *ginit(const void *k, size_t sz)
130{
131 gctx *g = CREATE(gctx);
132 g->c.ops = &gops;
133 rc4_init(&g->rc4, k, sz);
134 return (&g->c);
135}
136
137static void gencrypt(gcipher *c, const void *s, void *t, size_t sz)
138{
139 gctx *g = (gctx *)c;
140 rc4_encrypt(&g->rc4, s, t, sz);
141}
142
143static void gdestroy(gcipher *c)
144{
145 gctx *g = (gctx *)c;
146 DESTROY(g);
147}
148
149static const gcipher_ops gops = {
150 &rc4.b,
151 gencrypt, gencrypt, gdestroy, 0, 0
152};
153
154const gccipher rc4 = {
155 { "rc4", 0, 0 },
156 ginit
157};
158
159/*----- Generic random number generator interface -------------------------*/
160
161typedef struct grctx {
162 grand r;
163 rc4_ctx rc4;
164} grctx;
165
166static void grdestroy(grand *r)
167{
168 grctx *g = (grctx *)r;
169 DESTROY(g);
170}
171
172static int grmisc(grand *r, unsigned op, ...)
173{
174 grctx *g = (grctx *)r;
175 va_list ap;
176 int rc = 0;
177 octet buf[4];
178 va_start(ap, op);
179
180 switch (op) {
181 case GRAND_CHECK:
182 switch (va_arg(ap, unsigned)) {
183 case GRAND_CHECK:
184 case GRAND_SEEDINT:
185 case GRAND_SEEDUINT32:
186 case GRAND_SEEDBLOCK:
187 rc = 1;
188 break;
189 default:
190 rc = 0;
191 break;
192 }
193 break;
194 case GRAND_SEEDINT:
195 STORE32(buf, va_arg(ap, unsigned));
196 rc4_init(&g->rc4, buf, sizeof(buf));
197 break;
198 case GRAND_SEEDUINT32:
199 STORE32(buf, va_arg(ap, uint32));
200 rc4_init(&g->rc4, buf, sizeof(buf));
201 break;
202 case GRAND_SEEDBLOCK: {
203 const void *p = va_arg(ap, const void *);
204 size_t sz = va_arg(ap, size_t);
205 rc4_init(&g->rc4, p, sz);
206 } break;
207 }
208
209 va_end(ap);
210 return (rc);
211}
212
213static octet grbyte(grand *r)
214{
215 grctx *g = (grctx *)r;
216 octet o;
217 RC4_OPEN(&g->rc4, RC4_BYTE(o););
218 return (o);
219}
220
221static uint32 grword(grand *r)
222{
223 grctx *g = (grctx *)r;
224 octet b[4];
225 int i;
226 RC4_OPEN(&g->rc4,
227 for (i = 0; i < sizeof(b); i++)
228 RC4_BYTE(b[i]););
229 return (LOAD32(b));
230}
231
232static void grfill(grand *r, void *p, size_t sz)
233{
234 grctx *g = (grctx *)r;
235 rc4_encrypt(&g->rc4, 0, p, sz);
236}
237
238static const grand_ops grops = {
239 "rc4",
240 0,
241 grmisc, grdestroy,
242 grword, grbyte, grword, grand_range, grfill
243};
244
245/* --- @rc4_rand@ --- *
246 *
247 * Arguments: @const void *k@ = pointer to key material
248 * @size_t sz@ = size of key material
249 *
250 * Returns: Pointer to generic random number generator interface.
251 *
252 * Use: Creates a random number interface wrapper around an
253 * OFB-mode block cipher.
254 */
255
256grand *rc4_rand(const void *k, size_t sz)
257{
258 grctx *g = CREATE(grctx);
259 g->r.ops = &grops;
260 rc4_init(&g->rc4, k, sz);
261 return (&g->r);
262}
263
d03ab969 264/*----- Test rig ----------------------------------------------------------*/
265
266#ifdef TEST_RIG
267
268#include <stdio.h>
269#include <string.h>
270
271#include <mLib/quis.h>
272#include <mLib/testrig.h>
273
274static int v_encrypt(dstr *v)
275{
276 rc4_ctx ctx;
277 dstr d = DSTR_INIT;
278 int ok = 1;
279
280 rc4_init(&ctx, v[0].buf, v[0].len);
281 dstr_ensure(&d, v[1].len);
282 d.len = v[1].len;
283 rc4_encrypt(&ctx, v[1].buf, d.buf, d.len);
284
285 if (memcmp(v[2].buf, d.buf, d.len) != 0) {
286 ok = 0;
287 printf("\nfail encryption:"
288 "\n\tkey = ");
289 type_hex.dump(&v[0], stdout);
290 printf("\n\tplaintext = "); type_hex.dump(&v[1], stdout);
291 printf("\n\texpected = "); type_hex.dump(&v[2], stdout);
292 printf("\n\tcalculated = "); type_hex.dump(&d, stdout);
293 putchar('\n');
294 }
295
296 return (ok);
297}
298
299static int v_generate(dstr *v)
300{
301 rc4_ctx ctx;
302 dstr d = DSTR_INIT;
303 int ok = 1;
304
305 rc4_init(&ctx, v[0].buf, v[0].len);
306 rc4_encrypt(&ctx, 0, 0, *(int *)v[1].buf);
307 dstr_ensure(&d, v[2].len);
308 d.len = v[2].len;
309 rc4_encrypt(&ctx, 0, d.buf, d.len);
310
311 if (memcmp(v[2].buf, d.buf, d.len) != 0) {
312 ok = 0;
313 printf("\nfail generation:"
314 "\n\tkey = ");
315 type_hex.dump(&v[0], stdout);
316 printf("\n\tskip len = %i", *(int *)v[1].buf);
317 printf("\n\texpected = "); type_hex.dump(&v[2], stdout);
318 printf("\n\tcalculated = "); type_hex.dump(&d, stdout);
319 putchar('\n');
320 }
321
322 return (ok);
323}
324
325static test_chunk defs[] = {
326 { "rc4-encrypt", v_encrypt, { &type_hex, &type_hex, &type_hex, 0 } },
327 { "rc4-generate", v_generate, { &type_hex, &type_int, &type_hex, 0 } },
328 { 0, 0, { 0 } }
329};
330
331int main(int argc, char *argv[])
332{
333 test_run(argc, argv, defs, SRCDIR"/tests/rc4");
334 return (0);
335}
336
337#endif
338
339/*----- That's all, folks -------------------------------------------------*/