3 * $Id: rc4.c,v 1.6 2004/04/08 01:36:15 mdw Exp $
5 * The alleged RC4 stream cipher
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Header files ------------------------------------------------------*/
36 #include <mLib/bits.h>
45 /*----- Global variables --------------------------------------------------*/
47 const octet rc4_keysz[] = { KSZ_RANGE, RC4_KEYSZ, 1, 255, 1 };
49 /*----- Main code ---------------------------------------------------------*/
51 /* --- @rc4_addkey@ --- *
53 * Arguments: @rc4_ctx *ctx@ = pointer to context to key
54 * @const void *k@ = pointer to key data to use
55 * @size_t sz@ = size of the key data
59 * Use: Mixes key data with an RC4 context. The RC4 context is not
60 * reset before mixing. This may be used to mix new key
61 * material with an existing RC4 context.
64 void rc4_addkey(rc4_ctx *ctx, const void *k, size_t sz)
67 const octet *p = k, *q = p + sz;
71 for (i = j = 0; i < 256; i++) {
72 unsigned si = ctx->s[i];
73 j = (j + si + *p++) & 0xff;
74 ctx->s[i] = ctx->s[j];
83 /* --- @rc4_init@ --- *
85 * Arguments: @rc4_ctx *ctx@ = pointer to context to initialize
86 * @const void *k@ = pointer to key data to use
87 * @size_t sz@ = size of the key data
91 * Use: Initializes an RC4 context ready for use.
94 void rc4_init(rc4_ctx *ctx, const void *k, size_t sz)
98 for (i = 0; i < 256; i++)
101 rc4_addkey(ctx, k, sz);
104 /* --- @rc4_encrypt@ --- *
106 * Arguments: @rc4_ctx *ctx@ = pointer to context to use
107 * @const void *src@ = pointer to the source block
108 * @void *dest@ = pointer to the destination block
109 * @size_t sz@ = size of the block
113 * Use: Encrypts or decrypts a block of data. The destination may
114 * be null to just grind the generator around for a while. It's
115 * recommended that you say `@rc4_encrypt(&ctx, 0, 0, 1024)@'
116 * after initializing a new context, to prevent keystream
117 * guessing attacks. The source may be null to just extract a
118 * big lump of data from the generator.
121 void rc4_encrypt(rc4_ctx *ctx, const void *src, void *dest, size_t sz)
123 const octet *s = src;
127 RC4_OPEN(ctx, while (sz) { unsigned x; RC4_BYTE(x); sz--; });
129 RC4_OPEN(ctx, while (sz) { RC4_BYTE(*d++); sz--; });
132 while (sz) { unsigned x; RC4_BYTE(x); *d++ = *s++ ^ x; sz--; });
135 /*----- Generic cipher interface ------------------------------------------*/
137 typedef struct gctx {
142 static const gcipher_ops gops;
144 static gcipher *ginit(const void *k, size_t sz)
146 gctx *g = S_CREATE(gctx);
148 rc4_init(&g->rc4, k, sz);
152 static void gencrypt(gcipher *c, const void *s, void *t, size_t sz)
155 rc4_encrypt(&g->rc4, s, t, sz);
158 static void gdestroy(gcipher *c)
165 static const gcipher_ops gops = {
167 gencrypt, gencrypt, gdestroy, 0, 0
170 const gccipher rc4 = {
175 /*----- Generic random number generator interface -------------------------*/
177 typedef struct grctx {
182 static void grdestroy(grand *r)
184 grctx *g = (grctx *)r;
189 static int grmisc(grand *r, unsigned op, ...)
191 grctx *g = (grctx *)r;
200 switch (va_arg(ap, unsigned)) {
203 case GRAND_SEEDUINT32:
204 case GRAND_SEEDBLOCK:
214 i = va_arg(ap, unsigned);
216 rc4_addkey(&g->rc4, buf, sizeof(buf));
218 case GRAND_SEEDUINT32:
219 i = va_arg(ap, uint32);
221 rc4_addkey(&g->rc4, buf, sizeof(buf));
223 case GRAND_SEEDBLOCK: {
224 const void *p = va_arg(ap, const void *);
225 size_t sz = va_arg(ap, size_t);
226 rc4_addkey(&g->rc4, p, sz);
228 case GRAND_SEEDRAND: {
229 grand *rr = va_arg(ap, grand *);
231 rr->ops->fill(rr, buf, sizeof(buf));
232 rc4_addkey(&g->rc4, buf, sizeof(buf));
243 static octet grbyte(grand *r)
245 grctx *g = (grctx *)r;
247 RC4_OPEN(&g->rc4, RC4_BYTE(o););
251 static uint32 grword(grand *r)
253 grctx *g = (grctx *)r;
257 for (i = 0; i < sizeof(b); i++)
262 static void grfill(grand *r, void *p, size_t sz)
264 grctx *g = (grctx *)r;
265 rc4_encrypt(&g->rc4, 0, p, sz);
268 static const grand_ops grops = {
272 grword, grbyte, grword, grand_range, grfill
275 /* --- @rc4_rand@ --- *
277 * Arguments: @const void *k@ = pointer to key material
278 * @size_t sz@ = size of key material
280 * Returns: Pointer to generic random number generator interface.
282 * Use: Creates a random number interface wrapper around an
283 * OFB-mode block cipher.
286 grand *rc4_rand(const void *k, size_t sz)
288 grctx *g = S_CREATE(grctx);
290 rc4_init(&g->rc4, k, sz);
294 /*----- Test rig ----------------------------------------------------------*/
301 #include <mLib/quis.h>
302 #include <mLib/testrig.h>
304 static int v_encrypt(dstr *v)
310 rc4_init(&ctx, v[0].buf, v[0].len);
311 dstr_ensure(&d, v[1].len);
313 rc4_encrypt(&ctx, v[1].buf, d.buf, d.len);
315 if (memcmp(v[2].buf, d.buf, d.len) != 0) {
317 printf("\nfail encryption:"
319 type_hex.dump(&v[0], stdout);
320 printf("\n\tplaintext = "); type_hex.dump(&v[1], stdout);
321 printf("\n\texpected = "); type_hex.dump(&v[2], stdout);
322 printf("\n\tcalculated = "); type_hex.dump(&d, stdout);
329 static int v_generate(dstr *v)
335 rc4_init(&ctx, v[0].buf, v[0].len);
336 rc4_encrypt(&ctx, 0, 0, *(int *)v[1].buf);
337 dstr_ensure(&d, v[2].len);
339 rc4_encrypt(&ctx, 0, d.buf, d.len);
341 if (memcmp(v[2].buf, d.buf, d.len) != 0) {
343 printf("\nfail generation:"
345 type_hex.dump(&v[0], stdout);
346 printf("\n\tskip len = %i", *(int *)v[1].buf);
347 printf("\n\texpected = "); type_hex.dump(&v[2], stdout);
348 printf("\n\tcalculated = "); type_hex.dump(&d, stdout);
355 static test_chunk defs[] = {
356 { "rc4-encrypt", v_encrypt, { &type_hex, &type_hex, &type_hex, 0 } },
357 { "rc4-generate", v_generate, { &type_hex, &type_int, &type_hex, 0 } },
361 int main(int argc, char *argv[])
363 test_run(argc, argv, defs, SRCDIR"/tests/rc4");
369 /*----- That's all, folks -------------------------------------------------*/