3 * The alleged RC4 stream cipher
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Notes on RC4 ------------------------------------------------------*
30 * RC4 is a stream cipher desgigned by Ron Rivest. For a while RC4 was a
31 * trade secret of RSA Data Security, Inc., but somehow source code for a
32 * cipher which interworks with RC4 was posted to the Cypherpunks mailing
35 * RC4 has some quite bad biases, and its use for cryptographic purposes is
36 * no longer recommended.
39 #ifndef CATACOMB_RC4_H
40 #define CATACOMB_RC4_H
46 /*----- Header files ------------------------------------------------------*/
50 #include <mLib/bits.h>
52 #ifndef CATACOMB_GCIPHER_H
56 #ifndef CATACOMB_GRAND_H
60 /*----- Data structures ---------------------------------------------------*/
62 typedef struct rc4_ctx {
63 unsigned i, j; /* Indices into the @S@ table */
64 unsigned f; /* Flags word */
65 octet s[256]; /* The ever-changing @S@ table */
70 /*----- Macros ------------------------------------------------------------*/
72 /* --- @RC4_OPEN@ --- *
74 * Arguments: @ctx@ = pointer to an RC4 context
75 * @guts@ = code to perform within the RC4 context
77 * Use: Performs some code within an RC4 context. Some of the
78 * parameters are extracted from the context and held in local
79 * variables for speed. Multiple calls to @RC4_BYTE@ may be
80 * made within the open context. A context must only be
81 * opened once at a time.
84 #define RC4_OPEN(ctx, guts) do { \
85 unsigned _rc4_i = (ctx)->i; \
86 unsigned _rc4_j = (ctx)->j; \
87 octet *_rc4_s = (ctx)->s; \
89 assert(((void)"RC4 context may only be opened once at a time", \
90 ((ctx)->f & RC4F_OPEN) == 0)); \
91 (ctx)->f |= RC4F_OPEN; \
95 (ctx)->f &= ~RC4F_OPEN; \
100 /* --- @RC4_BYTE@ --- *
102 * Arguments: @x@ = output variable to set
104 * Use: Extracts an octet from the lexically innermost open RC4
105 * context and places it in the variable @x@.
108 #define RC4_BYTE(x) do { \
110 _rc4_i = (_rc4_i + 1) & 0xff; \
111 _si = _rc4_s[_rc4_i]; \
112 _rc4_j = (_rc4_j + _si) & 0xff; \
113 _sj = _rc4_s[_rc4_j]; \
114 _rc4_s[_rc4_i] = _sj; \
115 _rc4_s[_rc4_j] = _si; \
116 (x) = _rc4_s[(_si + _sj) & 0xff]; \
119 /*----- Functions provided ------------------------------------------------*/
121 /* --- @rc4_addkey@ --- *
123 * Arguments: @rc4_ctx *ctx@ = pointer to context to key
124 * @const void *k@ = pointer to key data to use
125 * @size_t sz@ = size of the key data
129 * Use: Mixes key data with an RC4 context. The RC4 context is not
130 * reset before mixing. This may be used to mix new key
131 * material with an existing RC4 context.
134 extern void rc4_addkey(rc4_ctx */*ctx*/, const void */*k*/, size_t /*sz*/);
136 /* --- @rc4_init@ --- *
138 * Arguments: @rc4_ctx *ctx@ = pointer to context to initialize
139 * @const void *k@ = pointer to key data to use
140 * @size_t sz@ = size of the key data
144 * Use: Initializes an RC4 context ready for use.
147 extern void rc4_init(rc4_ctx */*ctx*/, const void */*k*/, size_t /*sz*/);
149 /* --- @rc4_encrypt@ --- *
151 * Arguments: @rc4_ctx *ctx@ = pointer to context to use
152 * @const void *src@ = pointer to the source block
153 * @void *dest@ = pointer to the destination block
154 * @size_t sz@ = size of the block
158 * Use: Encrypts or decrypts a block of data. The destination may
159 * be null to just grind the generator around for a while. It's
160 * recommended that you say `@rc4_encrypt(&ctx, 0, 0, 1024)@'
161 * after initializing a new context, to prevent keystream
162 * guessing attacks. The source may be null to just extract a
163 * big lump of data from the generator.
166 extern void rc4_encrypt(rc4_ctx */*ctx*/,
167 const void */*src*/, void */*dest*/,
170 /*----- Generic cipher interface ------------------------------------------*/
173 extern const octet rc4_keysz[];
175 extern const gccipher rc4;
177 /*----- Generic random number generator interface -------------------------*/
179 /* --- @rc4_rand@ --- *
181 * Arguments: @const void *k@ = pointer to key material
182 * @size_t sz@ = size of key material
184 * Returns: Pointer to generic random number generator interface.
186 * Use: Creates a random number interface wrapper around the RC4
190 extern grand *rc4_rand(const void */*k*/, size_t /*sz*/);
192 /*----- That's all, folks -------------------------------------------------*/