3 * $Id: rc4.c,v 1.3 1999/12/13 15:34:01 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 /*----- Revision history --------------------------------------------------*
33 * Revision 1.3 1999/12/13 15:34:01 mdw
34 * Add support for seeding from a generic pseudorandom source.
36 * Revision 1.2 1999/12/10 23:27:35 mdw
37 * Generic cipher and RNG interfaces.
39 * Revision 1.1 1999/09/03 08:41:12 mdw
44 /*----- Header files ------------------------------------------------------*/
50 #include <mLib/bits.h>
57 /*----- Main code ---------------------------------------------------------*/
59 /* --- @rc4_init@ --- *
61 * Arguments: @rc4_ctx *ctx@ = pointer to context to initialize
62 * @const void *k@ = pointer to key data to use
63 * @size_t sz@ = size of the key data
67 * Use: Initializes an RC4 context ready for use.
70 void rc4_init(rc4_ctx
*ctx
, const void *k
, size_t sz
)
73 const octet
*p
= k
, *q
= p
+ sz
;
75 assert(((void)"RC4 does not support zero length keys", sz
!= 0));
77 for (i
= 0; i
< 256; i
++)
82 for (i
= j
= 0; i
< 256; i
++) {
83 unsigned si
= ctx
->s
[i
];
84 j
= (j
+ si
+ *p
++) & 0xff;
85 ctx
->s
[i
] = ctx
->s
[j
];
92 /* --- @rc4_encrypt@ --- *
94 * Arguments: @rc4_ctx *ctx@ = pointer to context to use
95 * @const void *src@ = pointer to the source block
96 * @void *dest@ = pointer to the destination block
97 * @size_t sz@ = size of the block
101 * Use: Encrypts or decrypts a block of data. The destination may
102 * be null to just grind the generator around for a while. It's
103 * recommended that you say `@rc4_encrypt(&ctx, 0, 0, 1024)@'
104 * after initializing a new context, to prevent keystream
105 * guessing attacks. The source may be null to just extract a
106 * big lump of data from the generator.
109 void rc4_encrypt(rc4_ctx
*ctx
, const void *src
, void *dest
, size_t sz
)
111 const octet
*s
= src
;
115 RC4_OPEN(ctx
, while (sz
) { unsigned x
; RC4_BYTE(x
); sz
--; });
117 RC4_OPEN(ctx
, while (sz
) { RC4_BYTE(*d
++); sz
--; });
120 while (sz
) { unsigned x
; RC4_BYTE(x
); *d
++ = *s
++ ^ x
; sz
--; });
123 /*----- Generic cipher interface ------------------------------------------*/
125 typedef struct gctx
{
130 static const gcipher_ops gops
;
132 static gcipher
*ginit(const void *k
, size_t sz
)
134 gctx
*g
= CREATE(gctx
);
136 rc4_init(&g
->rc4
, k
, sz
);
140 static void gencrypt(gcipher
*c
, const void *s
, void *t
, size_t sz
)
143 rc4_encrypt(&g
->rc4
, s
, t
, sz
);
146 static void gdestroy(gcipher
*c
)
152 static const gcipher_ops gops
= {
154 gencrypt
, gencrypt
, gdestroy
, 0, 0
157 const gccipher rc4
= {
162 /*----- Generic random number generator interface -------------------------*/
164 typedef struct grctx
{
169 static void grdestroy(grand
*r
)
171 grctx
*g
= (grctx
*)r
;
175 static int grmisc(grand
*r
, unsigned op
, ...)
177 grctx
*g
= (grctx
*)r
;
185 switch (va_arg(ap
, unsigned)) {
188 case GRAND_SEEDUINT32
:
189 case GRAND_SEEDBLOCK
:
199 STORE32(buf
, va_arg(ap
, unsigned));
200 rc4_init(&g
->rc4
, buf
, sizeof(buf
));
202 case GRAND_SEEDUINT32
:
203 STORE32(buf
, va_arg(ap
, uint32
));
204 rc4_init(&g
->rc4
, buf
, sizeof(buf
));
206 case GRAND_SEEDBLOCK
: {
207 const void *p
= va_arg(ap
, const void *);
208 size_t sz
= va_arg(ap
, size_t);
209 rc4_init(&g
->rc4
, p
, sz
);
211 case GRAND_SEEDRAND
: {
212 grand
*rr
= va_arg(ap
, grand
*);
214 rr
->ops
->fill(rr
, buf
, sizeof(buf
));
215 rc4_init(&g
->rc4
, buf
, sizeof(buf
));
226 static octet
grbyte(grand
*r
)
228 grctx
*g
= (grctx
*)r
;
230 RC4_OPEN(&g
->rc4
, RC4_BYTE(o
););
234 static uint32
grword(grand
*r
)
236 grctx
*g
= (grctx
*)r
;
240 for (i
= 0; i
< sizeof(b
); i
++)
245 static void grfill(grand
*r
, void *p
, size_t sz
)
247 grctx
*g
= (grctx
*)r
;
248 rc4_encrypt(&g
->rc4
, 0, p
, sz
);
251 static const grand_ops grops
= {
255 grword
, grbyte
, grword
, grand_range
, grfill
258 /* --- @rc4_rand@ --- *
260 * Arguments: @const void *k@ = pointer to key material
261 * @size_t sz@ = size of key material
263 * Returns: Pointer to generic random number generator interface.
265 * Use: Creates a random number interface wrapper around an
266 * OFB-mode block cipher.
269 grand
*rc4_rand(const void *k
, size_t sz
)
271 grctx
*g
= CREATE(grctx
);
273 rc4_init(&g
->rc4
, k
, sz
);
277 /*----- Test rig ----------------------------------------------------------*/
284 #include <mLib/quis.h>
285 #include <mLib/testrig.h>
287 static int v_encrypt(dstr
*v
)
293 rc4_init(&ctx
, v
[0].buf
, v
[0].len
);
294 dstr_ensure(&d
, v
[1].len
);
296 rc4_encrypt(&ctx
, v
[1].buf
, d
.buf
, d
.len
);
298 if (memcmp(v
[2].buf
, d
.buf
, d
.len
) != 0) {
300 printf("\nfail encryption:"
302 type_hex
.dump(&v
[0], stdout
);
303 printf("\n\tplaintext = "); type_hex
.dump(&v
[1], stdout
);
304 printf("\n\texpected = "); type_hex
.dump(&v
[2], stdout
);
305 printf("\n\tcalculated = "); type_hex
.dump(&d
, stdout
);
312 static int v_generate(dstr
*v
)
318 rc4_init(&ctx
, v
[0].buf
, v
[0].len
);
319 rc4_encrypt(&ctx
, 0, 0, *(int *)v
[1].buf
);
320 dstr_ensure(&d
, v
[2].len
);
322 rc4_encrypt(&ctx
, 0, d
.buf
, d
.len
);
324 if (memcmp(v
[2].buf
, d
.buf
, d
.len
) != 0) {
326 printf("\nfail generation:"
328 type_hex
.dump(&v
[0], stdout
);
329 printf("\n\tskip len = %i", *(int *)v
[1].buf
);
330 printf("\n\texpected = "); type_hex
.dump(&v
[2], stdout
);
331 printf("\n\tcalculated = "); type_hex
.dump(&d
, stdout
);
338 static test_chunk defs
[] = {
339 { "rc4-encrypt", v_encrypt
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
340 { "rc4-generate", v_generate
, { &type_hex
, &type_int
, &type_hex
, 0 } },
344 int main(int argc
, char *argv
[])
346 test_run(argc
, argv
, defs
, SRCDIR
"/tests/rc4");
352 /*----- That's all, folks -------------------------------------------------*/