3 * $Id: rc4.c,v 1.1 1999/09/03 08:41:12 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.1 1999/09/03 08:41:12 mdw
38 /*----- Header files ------------------------------------------------------*/
43 #include <mLib/bits.h>
47 /*----- Main code ---------------------------------------------------------*/
49 /* --- @rc4_init@ --- *
51 * Arguments: @rc4_ctx *ctx@ = pointer to context to initialize
52 * @const void *k@ = pointer to key data to use
53 * @size_t sz@ = size of the key data
57 * Use: Initializes an RC4 context ready for use.
60 void rc4_init(rc4_ctx
*ctx
, const void *k
, size_t sz
)
63 const octet
*p
= k
, *q
= p
+ sz
;
65 assert(((void)"RC4 does not support zero length keys", sz
!= 0));
67 for (i
= 0; i
< 256; i
++)
72 for (i
= j
= 0; i
< 256; i
++) {
73 unsigned si
= ctx
->s
[i
];
74 j
= (j
+ si
+ *p
++) & 0xff;
75 ctx
->s
[i
] = ctx
->s
[j
];
82 for (i
= 0; i
< 256; i
+= 16) {
84 for (j
= i
; j
< i
+ 16; j
++)
85 printf(" %02x", ctx
->s
[j
]);
91 /* --- @rc4_encrypt@ --- *
93 * Arguments: @rc4_ctx *ctx@ = pointer to context to use
94 * @const void *src@ = pointer to the source block
95 * @void *dest@ = pointer to the destination block
96 * @size_t sz@ = size of the block
100 * Use: Encrypts or decrypts a block of data. The destination may
101 * be null to just grind the generator around for a while. It's
102 * recommended that you say `@rc4_encrypt(&ctx, 0, 0, 1024)@'
103 * after initializing a new context, to prevent keystream
104 * guessing attacks. The source may be null to just extract a
105 * big lump of data from the generator.
108 void rc4_encrypt(rc4_ctx
*ctx
, const void *src
, void *dest
, size_t sz
)
110 const octet
*s
= src
;
114 RC4_OPEN(ctx
, while (sz
) { unsigned x
; RC4_BYTE(x
); sz
--; });
116 RC4_OPEN(ctx
, while (sz
) { RC4_BYTE(*d
++); sz
--; });
119 while (sz
) { unsigned x
; RC4_BYTE(x
); *d
++ = *s
++ ^ x
; sz
--; });
122 /*----- Test rig ----------------------------------------------------------*/
129 #include <mLib/quis.h>
130 #include <mLib/testrig.h>
132 static int v_encrypt(dstr
*v
)
138 rc4_init(&ctx
, v
[0].buf
, v
[0].len
);
139 dstr_ensure(&d
, v
[1].len
);
141 rc4_encrypt(&ctx
, v
[1].buf
, d
.buf
, d
.len
);
143 if (memcmp(v
[2].buf
, d
.buf
, d
.len
) != 0) {
145 printf("\nfail encryption:"
147 type_hex
.dump(&v
[0], stdout
);
148 printf("\n\tplaintext = "); type_hex
.dump(&v
[1], stdout
);
149 printf("\n\texpected = "); type_hex
.dump(&v
[2], stdout
);
150 printf("\n\tcalculated = "); type_hex
.dump(&d
, stdout
);
157 static int v_generate(dstr
*v
)
163 rc4_init(&ctx
, v
[0].buf
, v
[0].len
);
164 rc4_encrypt(&ctx
, 0, 0, *(int *)v
[1].buf
);
165 dstr_ensure(&d
, v
[2].len
);
167 rc4_encrypt(&ctx
, 0, d
.buf
, d
.len
);
169 if (memcmp(v
[2].buf
, d
.buf
, d
.len
) != 0) {
171 printf("\nfail generation:"
173 type_hex
.dump(&v
[0], stdout
);
174 printf("\n\tskip len = %i", *(int *)v
[1].buf
);
175 printf("\n\texpected = "); type_hex
.dump(&v
[2], stdout
);
176 printf("\n\tcalculated = "); type_hex
.dump(&d
, stdout
);
183 static test_chunk defs
[] = {
184 { "rc4-encrypt", v_encrypt
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
185 { "rc4-generate", v_generate
, { &type_hex
, &type_int
, &type_hex
, 0 } },
189 int main(int argc
, char *argv
[])
191 test_run(argc
, argv
, defs
, SRCDIR
"/tests/rc4");
197 /*----- That's all, folks -------------------------------------------------*/